2 * linux/drivers/net/irda/pxaficp_ir.c
4 * Based on sa1100_ir.c by Russell King
6 * Changes copyright (C) 2003-2005 MontaVista Software, Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * Infra-red driver (SIR/FIR) for the PXA2xx embedded microprocessor
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/init.h>
19 #include <linux/errno.h>
20 #include <linux/netdevice.h>
21 #include <linux/slab.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/interrupt.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/platform_device.h>
28 #include <net/irda/irda.h>
29 #include <net/irda/irmod.h>
30 #include <net/irda/wrapper.h>
31 #include <net/irda/irda_device.h>
35 #include <asm/delay.h>
36 #include <asm/hardware.h>
37 #include <asm/arch/irda.h>
38 #include <asm/arch/pxa-regs.h>
40 #ifdef CONFIG_MACH_MAINSTONE
41 #include <asm/arch/mainstone.h>
44 #define IrSR_RXPL_NEG_IS_ZERO (1<<4)
45 #define IrSR_RXPL_POS_IS_ZERO 0x0
46 #define IrSR_TXPL_NEG_IS_ZERO (1<<3)
47 #define IrSR_TXPL_POS_IS_ZERO 0x0
48 #define IrSR_XMODE_PULSE_1_6 (1<<2)
49 #define IrSR_XMODE_PULSE_3_16 0x0
50 #define IrSR_RCVEIR_IR_MODE (1<<1)
51 #define IrSR_RCVEIR_UART_MODE 0x0
52 #define IrSR_XMITIR_IR_MODE (1<<0)
53 #define IrSR_XMITIR_UART_MODE 0x0
55 #define IrSR_IR_RECEIVE_ON (\
56 IrSR_RXPL_NEG_IS_ZERO | \
57 IrSR_TXPL_POS_IS_ZERO | \
58 IrSR_XMODE_PULSE_3_16 | \
59 IrSR_RCVEIR_IR_MODE | \
60 IrSR_XMITIR_UART_MODE)
62 #define IrSR_IR_TRANSMIT_ON (\
63 IrSR_RXPL_NEG_IS_ZERO | \
64 IrSR_TXPL_POS_IS_ZERO | \
65 IrSR_XMODE_PULSE_3_16 | \
66 IrSR_RCVEIR_UART_MODE | \
72 unsigned long last_oscr;
74 unsigned char *dma_rx_buff;
75 unsigned char *dma_tx_buff;
76 dma_addr_t dma_rx_buff_phy;
77 dma_addr_t dma_tx_buff_phy;
78 unsigned int dma_tx_buff_len;
82 struct net_device_stats stats;
83 struct irlap_cb *irlap;
90 struct pxaficp_platform_data *pdata;
94 #define IS_FIR(si) ((si)->speed >= 4000000)
95 #define IRDA_FRAME_SIZE_LIMIT 2047
97 inline static void pxa_irda_fir_dma_rx_start(struct pxa_irda *si)
99 DCSR(si->rxdma) = DCSR_NODESC;
100 DSADR(si->rxdma) = __PREG(ICDR);
101 DTADR(si->rxdma) = si->dma_rx_buff_phy;
102 DCMD(si->rxdma) = DCMD_INCTRGADDR | DCMD_FLOWSRC | DCMD_WIDTH1 | DCMD_BURST32 | IRDA_FRAME_SIZE_LIMIT;
103 DCSR(si->rxdma) |= DCSR_RUN;
106 inline static void pxa_irda_fir_dma_tx_start(struct pxa_irda *si)
108 DCSR(si->txdma) = DCSR_NODESC;
109 DSADR(si->txdma) = si->dma_tx_buff_phy;
110 DTADR(si->txdma) = __PREG(ICDR);
111 DCMD(si->txdma) = DCMD_INCSRCADDR | DCMD_FLOWTRG | DCMD_ENDIRQEN | DCMD_WIDTH1 | DCMD_BURST32 | si->dma_tx_buff_len;
112 DCSR(si->txdma) |= DCSR_RUN;
116 * Set the IrDA communications speed.
118 static int pxa_irda_set_speed(struct pxa_irda *si, int speed)
121 unsigned int divisor;
124 case 9600: case 19200: case 38400:
125 case 57600: case 115200:
127 /* refer to PXA250/210 Developer's Manual 10-7 */
128 /* BaudRate = 14.7456 MHz / (16*Divisor) */
129 divisor = 14745600 / (16 * speed);
131 local_irq_save(flags);
135 DCSR(si->rxdma) &= ~DCSR_RUN;
138 pxa_set_cken(CKEN13_FICP, 0);
140 /* set board transceiver to SIR mode */
141 si->pdata->transceiver_mode(si->dev, IR_SIRMODE);
143 /* configure GPIO46/47 */
144 pxa_gpio_mode(GPIO46_STRXD_MD);
145 pxa_gpio_mode(GPIO47_STTXD_MD);
147 /* enable the STUART clock */
148 pxa_set_cken(CKEN5_STUART, 1);
151 /* disable STUART first */
154 /* access DLL & DLH */
156 STDLL = divisor & 0xff;
157 STDLH = divisor >> 8;
161 STISR = IrSR_IR_RECEIVE_ON | IrSR_XMODE_PULSE_1_6;
162 STIER = IER_UUE | IER_RLSE | IER_RAVIE | IER_RTIOE;
164 local_irq_restore(flags);
168 local_irq_save(flags);
173 pxa_set_cken(CKEN5_STUART, 0);
175 /* disable FICP first */
178 /* set board transceiver to FIR mode */
179 si->pdata->transceiver_mode(si->dev, IR_FIRMODE);
181 /* configure GPIO46/47 */
182 pxa_gpio_mode(GPIO46_ICPRXD_MD);
183 pxa_gpio_mode(GPIO47_ICPTXD_MD);
185 /* enable the FICP clock */
186 pxa_set_cken(CKEN13_FICP, 1);
189 pxa_irda_fir_dma_rx_start(si);
190 ICCR0 = ICCR0_ITR | ICCR0_RXE;
192 local_irq_restore(flags);
202 /* SIR interrupt service routine. */
203 static irqreturn_t pxa_irda_sir_irq(int irq, void *dev_id, struct pt_regs *regs)
205 struct net_device *dev = dev_id;
206 struct pxa_irda *si = netdev_priv(dev);
211 switch (iir & 0x0F) {
212 case 0x06: /* Receiver Line Status */
214 while (lsr & LSR_FIFOE) {
216 if (lsr & (LSR_OE | LSR_PE | LSR_FE | LSR_BI)) {
217 printk(KERN_DEBUG "pxa_ir: sir receiving error\n");
218 si->stats.rx_errors++;
220 si->stats.rx_frame_errors++;
222 si->stats.rx_fifo_errors++;
224 si->stats.rx_bytes++;
225 async_unwrap_char(dev, &si->stats, &si->rx_buff, data);
229 dev->last_rx = jiffies;
230 si->last_oscr = OSCR;
233 case 0x04: /* Received Data Available */
236 case 0x0C: /* Character Timeout Indication */
238 si->stats.rx_bytes++;
239 async_unwrap_char(dev, &si->stats, &si->rx_buff, STRBR);
240 } while (STLSR & LSR_DR);
241 dev->last_rx = jiffies;
242 si->last_oscr = OSCR;
245 case 0x02: /* Transmit FIFO Data Request */
246 while ((si->tx_buff.len) && (STLSR & LSR_TDRQ)) {
247 STTHR = *si->tx_buff.data++;
248 si->tx_buff.len -= 1;
251 if (si->tx_buff.len == 0) {
252 si->stats.tx_packets++;
253 si->stats.tx_bytes += si->tx_buff.data -
256 /* We need to ensure that the transmitter has finished. */
257 while ((STLSR & LSR_TEMT) == 0)
259 si->last_oscr = OSCR;
262 * Ok, we've finished transmitting. Now enable
263 * the receiver. Sometimes we get a receive IRQ
264 * immediately after a transmit...
267 pxa_irda_set_speed(si, si->newspeed);
270 /* enable IR Receiver, disable IR Transmitter */
271 STISR = IrSR_IR_RECEIVE_ON | IrSR_XMODE_PULSE_1_6;
272 /* enable STUART and receive interrupts */
273 STIER = IER_UUE | IER_RLSE | IER_RAVIE | IER_RTIOE;
276 netif_wake_queue(dev);
284 /* FIR Receive DMA interrupt handler */
285 static void pxa_irda_fir_dma_rx_irq(int channel, void *data, struct pt_regs *regs)
287 int dcsr = DCSR(channel);
289 DCSR(channel) = dcsr & ~DCSR_RUN;
291 printk(KERN_DEBUG "pxa_ir: fir rx dma bus error %#x\n", dcsr);
294 /* FIR Transmit DMA interrupt handler */
295 static void pxa_irda_fir_dma_tx_irq(int channel, void *data, struct pt_regs *regs)
297 struct net_device *dev = data;
298 struct pxa_irda *si = netdev_priv(dev);
301 dcsr = DCSR(channel);
302 DCSR(channel) = dcsr & ~DCSR_RUN;
304 if (dcsr & DCSR_ENDINTR) {
305 si->stats.tx_packets++;
306 si->stats.tx_bytes += si->dma_tx_buff_len;
308 si->stats.tx_errors++;
311 while (ICSR1 & ICSR1_TBY)
313 si->last_oscr = OSCR;
316 * HACK: It looks like the TBY bit is dropped too soon.
317 * Without this delay things break.
322 pxa_irda_set_speed(si, si->newspeed);
326 pxa_irda_fir_dma_rx_start(si);
327 ICCR0 = ICCR0_ITR | ICCR0_RXE;
329 netif_wake_queue(dev);
332 /* EIF(Error in FIFO/End in Frame) handler for FIR */
333 static void pxa_irda_fir_irq_eif(struct pxa_irda *si, struct net_device *dev)
335 unsigned int len, stat, data;
337 /* Get the current data position. */
338 len = DTADR(si->rxdma) - si->dma_rx_buff_phy;
341 /* Read Status, and then Data. */
346 if (stat & (ICSR1_CRE | ICSR1_ROR)) {
347 si->stats.rx_errors++;
348 if (stat & ICSR1_CRE) {
349 printk(KERN_DEBUG "pxa_ir: fir receive CRC error\n");
350 si->stats.rx_crc_errors++;
352 if (stat & ICSR1_ROR) {
353 printk(KERN_DEBUG "pxa_ir: fir receive overrun\n");
354 si->stats.rx_frame_errors++;
357 si->dma_rx_buff[len++] = data;
359 /* If we hit the end of frame, there's no point in continuing. */
360 if (stat & ICSR1_EOF)
362 } while (ICSR0 & ICSR0_EIF);
364 if (stat & ICSR1_EOF) {
366 struct sk_buff *skb = alloc_skb(len+1,GFP_ATOMIC);
368 printk(KERN_ERR "pxa_ir: fir out of memory for receive skb\n");
369 si->stats.rx_dropped++;
373 /* Align IP header to 20 bytes */
375 memcpy(skb->data, si->dma_rx_buff, len);
378 /* Feed it to IrLAP */
380 skb->mac.raw = skb->data;
381 skb->protocol = htons(ETH_P_IRDA);
384 si->stats.rx_packets++;
385 si->stats.rx_bytes += len;
387 dev->last_rx = jiffies;
391 /* FIR interrupt handler */
392 static irqreturn_t pxa_irda_fir_irq(int irq, void *dev_id, struct pt_regs *regs)
394 struct net_device *dev = dev_id;
395 struct pxa_irda *si = netdev_priv(dev);
399 DCSR(si->rxdma) &= ~DCSR_RUN;
400 si->last_oscr = OSCR;
403 if (icsr0 & (ICSR0_FRE | ICSR0_RAB)) {
404 if (icsr0 & ICSR0_FRE) {
405 printk(KERN_DEBUG "pxa_ir: fir receive frame error\n");
406 si->stats.rx_frame_errors++;
408 printk(KERN_DEBUG "pxa_ir: fir receive abort\n");
409 si->stats.rx_errors++;
411 ICSR0 = icsr0 & (ICSR0_FRE | ICSR0_RAB);
414 if (icsr0 & ICSR0_EIF) {
415 /* An error in FIFO occured, or there is a end of frame */
416 pxa_irda_fir_irq_eif(si, dev);
420 pxa_irda_fir_dma_rx_start(si);
421 ICCR0 = ICCR0_ITR | ICCR0_RXE;
426 /* hard_xmit interface of irda device */
427 static int pxa_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
429 struct pxa_irda *si = netdev_priv(dev);
430 int speed = irda_get_next_speed(skb);
433 * Does this packet contain a request to change the interface
434 * speed? If so, remember it until we complete the transmission
437 if (speed != si->speed && speed != -1)
438 si->newspeed = speed;
441 * If this is an empty frame, we can bypass a lot.
446 pxa_irda_set_speed(si, speed);
452 netif_stop_queue(dev);
455 si->tx_buff.data = si->tx_buff.head;
456 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data, si->tx_buff.truesize);
458 /* Disable STUART interrupts and switch to transmit mode. */
460 STISR = IrSR_IR_TRANSMIT_ON | IrSR_XMODE_PULSE_1_6;
462 /* enable STUART and transmit interrupts */
463 STIER = IER_UUE | IER_TIE;
465 unsigned long mtt = irda_get_mtt(skb);
467 si->dma_tx_buff_len = skb->len;
468 memcpy(si->dma_tx_buff, skb->data, skb->len);
471 while ((unsigned)(OSCR - si->last_oscr)/4 < mtt)
474 /* stop RX DMA, disable FICP */
475 DCSR(si->rxdma) &= ~DCSR_RUN;
478 pxa_irda_fir_dma_tx_start(si);
479 ICCR0 = ICCR0_ITR | ICCR0_TXE;
483 dev->trans_start = jiffies;
487 static int pxa_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
489 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
490 struct pxa_irda *si = netdev_priv(dev);
496 if (capable(CAP_NET_ADMIN)) {
498 * We are unable to set the speed if the
499 * device is not running.
501 if (netif_running(dev)) {
502 ret = pxa_irda_set_speed(si,
505 printk(KERN_INFO "pxa_ir: SIOCSBANDWIDTH: !netif_running\n");
513 if (capable(CAP_NET_ADMIN)) {
514 irda_device_set_media_busy(dev, TRUE);
521 rq->ifr_receiving = IS_FIR(si) ? 0
522 : si->rx_buff.state != OUTSIDE_FRAME;
533 static struct net_device_stats *pxa_irda_stats(struct net_device *dev)
535 struct pxa_irda *si = netdev_priv(dev);
539 static void pxa_irda_startup(struct pxa_irda *si)
541 /* Disable STUART interrupts */
543 /* enable STUART interrupt to the processor */
545 /* configure SIR frame format: StartBit - Data 7 ... Data 0 - Stop Bit */
546 STLCR = LCR_WLS0 | LCR_WLS1;
547 /* enable FIFO, we use FIFO to improve performance */
548 STFCR = FCR_TRFIFOE | FCR_ITL_32;
552 /* configure FICP ICCR2 */
553 ICCR2 = ICCR2_TXP | ICCR2_TRIG_32;
556 DRCMR17 = si->rxdma | DRCMR_MAPVLD;
557 DRCMR18 = si->txdma | DRCMR_MAPVLD;
559 /* force SIR reinitialization */
561 pxa_irda_set_speed(si, 9600);
563 printk(KERN_DEBUG "pxa_ir: irda startup\n");
566 static void pxa_irda_shutdown(struct pxa_irda *si)
570 local_irq_save(flags);
572 /* disable STUART and interrupt */
574 /* disable STUART SIR mode */
576 /* disable the STUART clock */
577 pxa_set_cken(CKEN5_STUART, 0);
580 DCSR(si->txdma) &= ~DCSR_RUN;
581 DCSR(si->rxdma) &= ~DCSR_RUN;
584 /* disable the FICP clock */
585 pxa_set_cken(CKEN13_FICP, 0);
590 local_irq_restore(flags);
592 /* power off board transceiver */
593 si->pdata->transceiver_mode(si->dev, IR_OFF);
595 printk(KERN_DEBUG "pxa_ir: irda shutdown\n");
598 static int pxa_irda_start(struct net_device *dev)
600 struct pxa_irda *si = netdev_priv(dev);
605 err = request_irq(IRQ_STUART, pxa_irda_sir_irq, 0, dev->name, dev);
609 err = request_irq(IRQ_ICP, pxa_irda_fir_irq, 0, dev->name, dev);
614 * The interrupt must remain disabled for now.
616 disable_irq(IRQ_STUART);
617 disable_irq(IRQ_ICP);
620 si->rxdma = pxa_request_dma("FICP_RX",DMA_PRIO_LOW, pxa_irda_fir_dma_rx_irq, dev);
624 si->txdma = pxa_request_dma("FICP_TX",DMA_PRIO_LOW, pxa_irda_fir_dma_tx_irq, dev);
629 si->dma_rx_buff = dma_alloc_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT,
630 &si->dma_rx_buff_phy, GFP_KERNEL );
631 if (!si->dma_rx_buff)
632 goto err_dma_rx_buff;
634 si->dma_tx_buff = dma_alloc_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT,
635 &si->dma_tx_buff_phy, GFP_KERNEL );
636 if (!si->dma_tx_buff)
637 goto err_dma_tx_buff;
639 /* Setup the serial port for the initial speed. */
640 pxa_irda_startup(si);
643 * Open a new IrLAP layer instance.
645 si->irlap = irlap_open(dev, &si->qos, "pxa");
651 * Now enable the interrupt and start the queue
653 enable_irq(IRQ_STUART);
655 netif_start_queue(dev);
657 printk(KERN_DEBUG "pxa_ir: irda driver opened\n");
662 pxa_irda_shutdown(si);
663 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_tx_buff, si->dma_tx_buff_phy);
665 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_rx_buff, si->dma_rx_buff_phy);
667 pxa_free_dma(si->txdma);
669 pxa_free_dma(si->rxdma);
671 free_irq(IRQ_ICP, dev);
673 free_irq(IRQ_STUART, dev);
679 static int pxa_irda_stop(struct net_device *dev)
681 struct pxa_irda *si = netdev_priv(dev);
683 netif_stop_queue(dev);
685 pxa_irda_shutdown(si);
689 irlap_close(si->irlap);
693 free_irq(IRQ_STUART, dev);
694 free_irq(IRQ_ICP, dev);
696 pxa_free_dma(si->rxdma);
697 pxa_free_dma(si->txdma);
700 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_tx_buff, si->dma_tx_buff_phy);
702 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_rx_buff, si->dma_rx_buff_phy);
704 printk(KERN_DEBUG "pxa_ir: irda driver closed\n");
708 static int pxa_irda_suspend(struct device *_dev, pm_message_t state)
710 struct net_device *dev = dev_get_drvdata(_dev);
713 if (dev && netif_running(dev)) {
714 si = netdev_priv(dev);
715 netif_device_detach(dev);
716 pxa_irda_shutdown(si);
722 static int pxa_irda_resume(struct device *_dev)
724 struct net_device *dev = dev_get_drvdata(_dev);
727 if (dev && netif_running(dev)) {
728 si = netdev_priv(dev);
729 pxa_irda_startup(si);
730 netif_device_attach(dev);
731 netif_wake_queue(dev);
738 static int pxa_irda_init_iobuf(iobuff_t *io, int size)
740 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
741 if (io->head != NULL) {
743 io->in_frame = FALSE;
744 io->state = OUTSIDE_FRAME;
747 return io->head ? 0 : -ENOMEM;
750 static int pxa_irda_probe(struct device *_dev)
752 struct platform_device *pdev = to_platform_device(_dev);
753 struct net_device *dev;
755 unsigned int baudrate_mask;
758 if (!pdev->dev.platform_data)
761 err = request_mem_region(__PREG(STUART), 0x24, "IrDA") ? 0 : -EBUSY;
765 err = request_mem_region(__PREG(FICP), 0x1c, "IrDA") ? 0 : -EBUSY;
769 dev = alloc_irdadev(sizeof(struct pxa_irda));
773 si = netdev_priv(dev);
774 si->dev = &pdev->dev;
775 si->pdata = pdev->dev.platform_data;
778 * Initialise the SIR buffers
780 err = pxa_irda_init_iobuf(&si->rx_buff, 14384);
783 err = pxa_irda_init_iobuf(&si->tx_buff, 4000);
787 dev->hard_start_xmit = pxa_irda_hard_xmit;
788 dev->open = pxa_irda_start;
789 dev->stop = pxa_irda_stop;
790 dev->do_ioctl = pxa_irda_ioctl;
791 dev->get_stats = pxa_irda_stats;
793 irda_init_max_qos_capabilies(&si->qos);
796 if (si->pdata->transceiver_cap & IR_SIRMODE)
797 baudrate_mask |= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
798 if (si->pdata->transceiver_cap & IR_FIRMODE)
799 baudrate_mask |= IR_4000000 << 8;
801 si->qos.baud_rate.bits &= baudrate_mask;
802 si->qos.min_turn_time.bits = 7; /* 1ms or more */
804 irda_qos_bits_to_value(&si->qos);
806 err = register_netdev(dev);
809 dev_set_drvdata(&pdev->dev, dev);
812 kfree(si->tx_buff.head);
814 kfree(si->rx_buff.head);
818 release_mem_region(__PREG(FICP), 0x1c);
820 release_mem_region(__PREG(STUART), 0x24);
826 static int pxa_irda_remove(struct device *_dev)
828 struct net_device *dev = dev_get_drvdata(_dev);
831 struct pxa_irda *si = netdev_priv(dev);
832 unregister_netdev(dev);
833 kfree(si->tx_buff.head);
834 kfree(si->rx_buff.head);
838 release_mem_region(__PREG(STUART), 0x24);
839 release_mem_region(__PREG(FICP), 0x1c);
844 static struct device_driver pxa_ir_driver = {
846 .bus = &platform_bus_type,
847 .probe = pxa_irda_probe,
848 .remove = pxa_irda_remove,
849 .suspend = pxa_irda_suspend,
850 .resume = pxa_irda_resume,
853 static int __init pxa_irda_init(void)
855 return driver_register(&pxa_ir_driver);
858 static void __exit pxa_irda_exit(void)
860 driver_unregister(&pxa_ir_driver);
863 module_init(pxa_irda_init);
864 module_exit(pxa_irda_exit);
866 MODULE_LICENSE("GPL");