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/module.h>
16 #include <linux/types.h>
17 #include <linux/init.h>
18 #include <linux/errno.h>
19 #include <linux/netdevice.h>
20 #include <linux/slab.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/interrupt.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/platform_device.h>
27 #include <net/irda/irda.h>
28 #include <net/irda/irmod.h>
29 #include <net/irda/wrapper.h>
30 #include <net/irda/irda_device.h>
34 #include <asm/delay.h>
35 #include <asm/hardware.h>
36 #include <asm/arch/irda.h>
37 #include <asm/arch/pxa-regs.h>
39 #ifdef CONFIG_MACH_MAINSTONE
40 #include <asm/arch/mainstone.h>
43 #define IrSR_RXPL_NEG_IS_ZERO (1<<4)
44 #define IrSR_RXPL_POS_IS_ZERO 0x0
45 #define IrSR_TXPL_NEG_IS_ZERO (1<<3)
46 #define IrSR_TXPL_POS_IS_ZERO 0x0
47 #define IrSR_XMODE_PULSE_1_6 (1<<2)
48 #define IrSR_XMODE_PULSE_3_16 0x0
49 #define IrSR_RCVEIR_IR_MODE (1<<1)
50 #define IrSR_RCVEIR_UART_MODE 0x0
51 #define IrSR_XMITIR_IR_MODE (1<<0)
52 #define IrSR_XMITIR_UART_MODE 0x0
54 #define IrSR_IR_RECEIVE_ON (\
55 IrSR_RXPL_NEG_IS_ZERO | \
56 IrSR_TXPL_POS_IS_ZERO | \
57 IrSR_XMODE_PULSE_3_16 | \
58 IrSR_RCVEIR_IR_MODE | \
59 IrSR_XMITIR_UART_MODE)
61 #define IrSR_IR_TRANSMIT_ON (\
62 IrSR_RXPL_NEG_IS_ZERO | \
63 IrSR_TXPL_POS_IS_ZERO | \
64 IrSR_XMODE_PULSE_3_16 | \
65 IrSR_RCVEIR_UART_MODE | \
71 unsigned long last_oscr;
73 unsigned char *dma_rx_buff;
74 unsigned char *dma_tx_buff;
75 dma_addr_t dma_rx_buff_phy;
76 dma_addr_t dma_tx_buff_phy;
77 unsigned int dma_tx_buff_len;
81 struct net_device_stats stats;
82 struct irlap_cb *irlap;
89 struct pxaficp_platform_data *pdata;
93 #define IS_FIR(si) ((si)->speed >= 4000000)
94 #define IRDA_FRAME_SIZE_LIMIT 2047
96 inline static void pxa_irda_fir_dma_rx_start(struct pxa_irda *si)
98 DCSR(si->rxdma) = DCSR_NODESC;
99 DSADR(si->rxdma) = __PREG(ICDR);
100 DTADR(si->rxdma) = si->dma_rx_buff_phy;
101 DCMD(si->rxdma) = DCMD_INCTRGADDR | DCMD_FLOWSRC | DCMD_WIDTH1 | DCMD_BURST32 | IRDA_FRAME_SIZE_LIMIT;
102 DCSR(si->rxdma) |= DCSR_RUN;
105 inline static void pxa_irda_fir_dma_tx_start(struct pxa_irda *si)
107 DCSR(si->txdma) = DCSR_NODESC;
108 DSADR(si->txdma) = si->dma_tx_buff_phy;
109 DTADR(si->txdma) = __PREG(ICDR);
110 DCMD(si->txdma) = DCMD_INCSRCADDR | DCMD_FLOWTRG | DCMD_ENDIRQEN | DCMD_WIDTH1 | DCMD_BURST32 | si->dma_tx_buff_len;
111 DCSR(si->txdma) |= DCSR_RUN;
115 * Set the IrDA communications speed.
117 static int pxa_irda_set_speed(struct pxa_irda *si, int speed)
120 unsigned int divisor;
123 case 9600: case 19200: case 38400:
124 case 57600: case 115200:
126 /* refer to PXA250/210 Developer's Manual 10-7 */
127 /* BaudRate = 14.7456 MHz / (16*Divisor) */
128 divisor = 14745600 / (16 * speed);
130 local_irq_save(flags);
134 DCSR(si->rxdma) &= ~DCSR_RUN;
137 pxa_set_cken(CKEN_FICP, 0);
139 /* set board transceiver to SIR mode */
140 si->pdata->transceiver_mode(si->dev, IR_SIRMODE);
142 /* configure GPIO46/47 */
143 pxa_gpio_mode(GPIO46_STRXD_MD);
144 pxa_gpio_mode(GPIO47_STTXD_MD);
146 /* enable the STUART clock */
147 pxa_set_cken(CKEN_STUART, 1);
150 /* disable STUART first */
153 /* access DLL & DLH */
155 STDLL = divisor & 0xff;
156 STDLH = divisor >> 8;
160 STISR = IrSR_IR_RECEIVE_ON | IrSR_XMODE_PULSE_1_6;
161 STIER = IER_UUE | IER_RLSE | IER_RAVIE | IER_RTIOE;
163 local_irq_restore(flags);
167 local_irq_save(flags);
172 pxa_set_cken(CKEN_STUART, 0);
174 /* disable FICP first */
177 /* set board transceiver to FIR mode */
178 si->pdata->transceiver_mode(si->dev, IR_FIRMODE);
180 /* configure GPIO46/47 */
181 pxa_gpio_mode(GPIO46_ICPRXD_MD);
182 pxa_gpio_mode(GPIO47_ICPTXD_MD);
184 /* enable the FICP clock */
185 pxa_set_cken(CKEN_FICP, 1);
188 pxa_irda_fir_dma_rx_start(si);
189 ICCR0 = ICCR0_ITR | ICCR0_RXE;
191 local_irq_restore(flags);
201 /* SIR interrupt service routine. */
202 static irqreturn_t pxa_irda_sir_irq(int irq, void *dev_id)
204 struct net_device *dev = dev_id;
205 struct pxa_irda *si = netdev_priv(dev);
210 switch (iir & 0x0F) {
211 case 0x06: /* Receiver Line Status */
213 while (lsr & LSR_FIFOE) {
215 if (lsr & (LSR_OE | LSR_PE | LSR_FE | LSR_BI)) {
216 printk(KERN_DEBUG "pxa_ir: sir receiving error\n");
217 si->stats.rx_errors++;
219 si->stats.rx_frame_errors++;
221 si->stats.rx_fifo_errors++;
223 si->stats.rx_bytes++;
224 async_unwrap_char(dev, &si->stats, &si->rx_buff, data);
228 dev->last_rx = jiffies;
229 si->last_oscr = OSCR;
232 case 0x04: /* Received Data Available */
235 case 0x0C: /* Character Timeout Indication */
237 si->stats.rx_bytes++;
238 async_unwrap_char(dev, &si->stats, &si->rx_buff, STRBR);
239 } while (STLSR & LSR_DR);
240 dev->last_rx = jiffies;
241 si->last_oscr = OSCR;
244 case 0x02: /* Transmit FIFO Data Request */
245 while ((si->tx_buff.len) && (STLSR & LSR_TDRQ)) {
246 STTHR = *si->tx_buff.data++;
247 si->tx_buff.len -= 1;
250 if (si->tx_buff.len == 0) {
251 si->stats.tx_packets++;
252 si->stats.tx_bytes += si->tx_buff.data -
255 /* We need to ensure that the transmitter has finished. */
256 while ((STLSR & LSR_TEMT) == 0)
258 si->last_oscr = OSCR;
261 * Ok, we've finished transmitting. Now enable
262 * the receiver. Sometimes we get a receive IRQ
263 * immediately after a transmit...
266 pxa_irda_set_speed(si, si->newspeed);
269 /* enable IR Receiver, disable IR Transmitter */
270 STISR = IrSR_IR_RECEIVE_ON | IrSR_XMODE_PULSE_1_6;
271 /* enable STUART and receive interrupts */
272 STIER = IER_UUE | IER_RLSE | IER_RAVIE | IER_RTIOE;
275 netif_wake_queue(dev);
283 /* FIR Receive DMA interrupt handler */
284 static void pxa_irda_fir_dma_rx_irq(int channel, void *data)
286 int dcsr = DCSR(channel);
288 DCSR(channel) = dcsr & ~DCSR_RUN;
290 printk(KERN_DEBUG "pxa_ir: fir rx dma bus error %#x\n", dcsr);
293 /* FIR Transmit DMA interrupt handler */
294 static void pxa_irda_fir_dma_tx_irq(int channel, void *data)
296 struct net_device *dev = data;
297 struct pxa_irda *si = netdev_priv(dev);
300 dcsr = DCSR(channel);
301 DCSR(channel) = dcsr & ~DCSR_RUN;
303 if (dcsr & DCSR_ENDINTR) {
304 si->stats.tx_packets++;
305 si->stats.tx_bytes += si->dma_tx_buff_len;
307 si->stats.tx_errors++;
310 while (ICSR1 & ICSR1_TBY)
312 si->last_oscr = OSCR;
315 * HACK: It looks like the TBY bit is dropped too soon.
316 * Without this delay things break.
321 pxa_irda_set_speed(si, si->newspeed);
327 pxa_irda_fir_dma_rx_start(si);
328 while ((ICSR1 & ICSR1_RNE) && i--)
330 ICCR0 = ICCR0_ITR | ICCR0_RXE;
333 printk(KERN_ERR "pxa_ir: cannot clear Rx FIFO!\n");
335 netif_wake_queue(dev);
338 /* EIF(Error in FIFO/End in Frame) handler for FIR */
339 static void pxa_irda_fir_irq_eif(struct pxa_irda *si, struct net_device *dev, int icsr0)
341 unsigned int len, stat, data;
343 /* Get the current data position. */
344 len = DTADR(si->rxdma) - si->dma_rx_buff_phy;
347 /* Read Status, and then Data. */
352 if (stat & (ICSR1_CRE | ICSR1_ROR)) {
353 si->stats.rx_errors++;
354 if (stat & ICSR1_CRE) {
355 printk(KERN_DEBUG "pxa_ir: fir receive CRC error\n");
356 si->stats.rx_crc_errors++;
358 if (stat & ICSR1_ROR) {
359 printk(KERN_DEBUG "pxa_ir: fir receive overrun\n");
360 si->stats.rx_over_errors++;
363 si->dma_rx_buff[len++] = data;
365 /* If we hit the end of frame, there's no point in continuing. */
366 if (stat & ICSR1_EOF)
368 } while (ICSR0 & ICSR0_EIF);
370 if (stat & ICSR1_EOF) {
374 if (icsr0 & ICSR0_FRE) {
375 printk(KERN_ERR "pxa_ir: dropping erroneous frame\n");
376 si->stats.rx_dropped++;
380 skb = alloc_skb(len+1,GFP_ATOMIC);
382 printk(KERN_ERR "pxa_ir: fir out of memory for receive skb\n");
383 si->stats.rx_dropped++;
387 /* Align IP header to 20 bytes */
389 skb_copy_to_linear_data(skb, si->dma_rx_buff, len);
392 /* Feed it to IrLAP */
394 skb_reset_mac_header(skb);
395 skb->protocol = htons(ETH_P_IRDA);
398 si->stats.rx_packets++;
399 si->stats.rx_bytes += len;
401 dev->last_rx = jiffies;
405 /* FIR interrupt handler */
406 static irqreturn_t pxa_irda_fir_irq(int irq, void *dev_id)
408 struct net_device *dev = dev_id;
409 struct pxa_irda *si = netdev_priv(dev);
413 DCSR(si->rxdma) &= ~DCSR_RUN;
414 si->last_oscr = OSCR;
417 if (icsr0 & (ICSR0_FRE | ICSR0_RAB)) {
418 if (icsr0 & ICSR0_FRE) {
419 printk(KERN_DEBUG "pxa_ir: fir receive frame error\n");
420 si->stats.rx_frame_errors++;
422 printk(KERN_DEBUG "pxa_ir: fir receive abort\n");
423 si->stats.rx_errors++;
425 ICSR0 = icsr0 & (ICSR0_FRE | ICSR0_RAB);
428 if (icsr0 & ICSR0_EIF) {
429 /* An error in FIFO occured, or there is a end of frame */
430 pxa_irda_fir_irq_eif(si, dev, icsr0);
434 pxa_irda_fir_dma_rx_start(si);
435 while ((ICSR1 & ICSR1_RNE) && i--)
437 ICCR0 = ICCR0_ITR | ICCR0_RXE;
440 printk(KERN_ERR "pxa_ir: cannot clear Rx FIFO!\n");
445 /* hard_xmit interface of irda device */
446 static int pxa_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
448 struct pxa_irda *si = netdev_priv(dev);
449 int speed = irda_get_next_speed(skb);
452 * Does this packet contain a request to change the interface
453 * speed? If so, remember it until we complete the transmission
456 if (speed != si->speed && speed != -1)
457 si->newspeed = speed;
460 * If this is an empty frame, we can bypass a lot.
465 pxa_irda_set_speed(si, speed);
471 netif_stop_queue(dev);
474 si->tx_buff.data = si->tx_buff.head;
475 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data, si->tx_buff.truesize);
477 /* Disable STUART interrupts and switch to transmit mode. */
479 STISR = IrSR_IR_TRANSMIT_ON | IrSR_XMODE_PULSE_1_6;
481 /* enable STUART and transmit interrupts */
482 STIER = IER_UUE | IER_TIE;
484 unsigned long mtt = irda_get_mtt(skb);
486 si->dma_tx_buff_len = skb->len;
487 skb_copy_from_linear_data(skb, si->dma_tx_buff, skb->len);
490 while ((unsigned)(OSCR - si->last_oscr)/4 < mtt)
493 /* stop RX DMA, disable FICP */
494 DCSR(si->rxdma) &= ~DCSR_RUN;
497 pxa_irda_fir_dma_tx_start(si);
498 ICCR0 = ICCR0_ITR | ICCR0_TXE;
502 dev->trans_start = jiffies;
506 static int pxa_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
508 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
509 struct pxa_irda *si = netdev_priv(dev);
515 if (capable(CAP_NET_ADMIN)) {
517 * We are unable to set the speed if the
518 * device is not running.
520 if (netif_running(dev)) {
521 ret = pxa_irda_set_speed(si,
524 printk(KERN_INFO "pxa_ir: SIOCSBANDWIDTH: !netif_running\n");
532 if (capable(CAP_NET_ADMIN)) {
533 irda_device_set_media_busy(dev, TRUE);
540 rq->ifr_receiving = IS_FIR(si) ? 0
541 : si->rx_buff.state != OUTSIDE_FRAME;
552 static struct net_device_stats *pxa_irda_stats(struct net_device *dev)
554 struct pxa_irda *si = netdev_priv(dev);
558 static void pxa_irda_startup(struct pxa_irda *si)
560 /* Disable STUART interrupts */
562 /* enable STUART interrupt to the processor */
564 /* configure SIR frame format: StartBit - Data 7 ... Data 0 - Stop Bit */
565 STLCR = LCR_WLS0 | LCR_WLS1;
566 /* enable FIFO, we use FIFO to improve performance */
567 STFCR = FCR_TRFIFOE | FCR_ITL_32;
571 /* configure FICP ICCR2 */
572 ICCR2 = ICCR2_TXP | ICCR2_TRIG_32;
575 DRCMR17 = si->rxdma | DRCMR_MAPVLD;
576 DRCMR18 = si->txdma | DRCMR_MAPVLD;
578 /* force SIR reinitialization */
580 pxa_irda_set_speed(si, 9600);
582 printk(KERN_DEBUG "pxa_ir: irda startup\n");
585 static void pxa_irda_shutdown(struct pxa_irda *si)
589 local_irq_save(flags);
591 /* disable STUART and interrupt */
593 /* disable STUART SIR mode */
595 /* disable the STUART clock */
596 pxa_set_cken(CKEN_STUART, 0);
599 DCSR(si->txdma) &= ~DCSR_RUN;
600 DCSR(si->rxdma) &= ~DCSR_RUN;
603 /* disable the FICP clock */
604 pxa_set_cken(CKEN_FICP, 0);
609 local_irq_restore(flags);
611 /* power off board transceiver */
612 si->pdata->transceiver_mode(si->dev, IR_OFF);
614 printk(KERN_DEBUG "pxa_ir: irda shutdown\n");
617 static int pxa_irda_start(struct net_device *dev)
619 struct pxa_irda *si = netdev_priv(dev);
624 err = request_irq(IRQ_STUART, pxa_irda_sir_irq, 0, dev->name, dev);
628 err = request_irq(IRQ_ICP, pxa_irda_fir_irq, 0, dev->name, dev);
633 * The interrupt must remain disabled for now.
635 disable_irq(IRQ_STUART);
636 disable_irq(IRQ_ICP);
639 si->rxdma = pxa_request_dma("FICP_RX",DMA_PRIO_LOW, pxa_irda_fir_dma_rx_irq, dev);
643 si->txdma = pxa_request_dma("FICP_TX",DMA_PRIO_LOW, pxa_irda_fir_dma_tx_irq, dev);
648 si->dma_rx_buff = dma_alloc_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT,
649 &si->dma_rx_buff_phy, GFP_KERNEL );
650 if (!si->dma_rx_buff)
651 goto err_dma_rx_buff;
653 si->dma_tx_buff = dma_alloc_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT,
654 &si->dma_tx_buff_phy, GFP_KERNEL );
655 if (!si->dma_tx_buff)
656 goto err_dma_tx_buff;
658 /* Setup the serial port for the initial speed. */
659 pxa_irda_startup(si);
662 * Open a new IrLAP layer instance.
664 si->irlap = irlap_open(dev, &si->qos, "pxa");
670 * Now enable the interrupt and start the queue
672 enable_irq(IRQ_STUART);
674 netif_start_queue(dev);
676 printk(KERN_DEBUG "pxa_ir: irda driver opened\n");
681 pxa_irda_shutdown(si);
682 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_tx_buff, si->dma_tx_buff_phy);
684 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_rx_buff, si->dma_rx_buff_phy);
686 pxa_free_dma(si->txdma);
688 pxa_free_dma(si->rxdma);
690 free_irq(IRQ_ICP, dev);
692 free_irq(IRQ_STUART, dev);
698 static int pxa_irda_stop(struct net_device *dev)
700 struct pxa_irda *si = netdev_priv(dev);
702 netif_stop_queue(dev);
704 pxa_irda_shutdown(si);
708 irlap_close(si->irlap);
712 free_irq(IRQ_STUART, dev);
713 free_irq(IRQ_ICP, dev);
715 pxa_free_dma(si->rxdma);
716 pxa_free_dma(si->txdma);
719 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_tx_buff, si->dma_tx_buff_phy);
721 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_rx_buff, si->dma_rx_buff_phy);
723 printk(KERN_DEBUG "pxa_ir: irda driver closed\n");
727 static int pxa_irda_suspend(struct platform_device *_dev, pm_message_t state)
729 struct net_device *dev = platform_get_drvdata(_dev);
732 if (dev && netif_running(dev)) {
733 si = netdev_priv(dev);
734 netif_device_detach(dev);
735 pxa_irda_shutdown(si);
741 static int pxa_irda_resume(struct platform_device *_dev)
743 struct net_device *dev = platform_get_drvdata(_dev);
746 if (dev && netif_running(dev)) {
747 si = netdev_priv(dev);
748 pxa_irda_startup(si);
749 netif_device_attach(dev);
750 netif_wake_queue(dev);
757 static int pxa_irda_init_iobuf(iobuff_t *io, int size)
759 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
760 if (io->head != NULL) {
762 io->in_frame = FALSE;
763 io->state = OUTSIDE_FRAME;
766 return io->head ? 0 : -ENOMEM;
769 static int pxa_irda_probe(struct platform_device *pdev)
771 struct net_device *dev;
773 unsigned int baudrate_mask;
776 if (!pdev->dev.platform_data)
779 err = request_mem_region(__PREG(STUART), 0x24, "IrDA") ? 0 : -EBUSY;
783 err = request_mem_region(__PREG(FICP), 0x1c, "IrDA") ? 0 : -EBUSY;
787 dev = alloc_irdadev(sizeof(struct pxa_irda));
791 si = netdev_priv(dev);
792 si->dev = &pdev->dev;
793 si->pdata = pdev->dev.platform_data;
796 * Initialise the SIR buffers
798 err = pxa_irda_init_iobuf(&si->rx_buff, 14384);
801 err = pxa_irda_init_iobuf(&si->tx_buff, 4000);
805 dev->hard_start_xmit = pxa_irda_hard_xmit;
806 dev->open = pxa_irda_start;
807 dev->stop = pxa_irda_stop;
808 dev->do_ioctl = pxa_irda_ioctl;
809 dev->get_stats = pxa_irda_stats;
811 irda_init_max_qos_capabilies(&si->qos);
814 if (si->pdata->transceiver_cap & IR_SIRMODE)
815 baudrate_mask |= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
816 if (si->pdata->transceiver_cap & IR_FIRMODE)
817 baudrate_mask |= IR_4000000 << 8;
819 si->qos.baud_rate.bits &= baudrate_mask;
820 si->qos.min_turn_time.bits = 7; /* 1ms or more */
822 irda_qos_bits_to_value(&si->qos);
824 err = register_netdev(dev);
827 dev_set_drvdata(&pdev->dev, dev);
830 kfree(si->tx_buff.head);
832 kfree(si->rx_buff.head);
836 release_mem_region(__PREG(FICP), 0x1c);
838 release_mem_region(__PREG(STUART), 0x24);
844 static int pxa_irda_remove(struct platform_device *_dev)
846 struct net_device *dev = platform_get_drvdata(_dev);
849 struct pxa_irda *si = netdev_priv(dev);
850 unregister_netdev(dev);
851 kfree(si->tx_buff.head);
852 kfree(si->rx_buff.head);
856 release_mem_region(__PREG(STUART), 0x24);
857 release_mem_region(__PREG(FICP), 0x1c);
862 static struct platform_driver pxa_ir_driver = {
866 .probe = pxa_irda_probe,
867 .remove = pxa_irda_remove,
868 .suspend = pxa_irda_suspend,
869 .resume = pxa_irda_resume,
872 static int __init pxa_irda_init(void)
874 return platform_driver_register(&pxa_ir_driver);
877 static void __exit pxa_irda_exit(void)
879 platform_driver_unregister(&pxa_ir_driver);
882 module_init(pxa_irda_init);
883 module_exit(pxa_irda_exit);
885 MODULE_LICENSE("GPL");