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/netdevice.h>
17 #include <linux/platform_device.h>
18 #include <linux/clk.h>
20 #include <net/irda/irda.h>
21 #include <net/irda/irmod.h>
22 #include <net/irda/wrapper.h>
23 #include <net/irda/irda_device.h>
26 #include <mach/irda.h>
27 #include <mach/pxa-regs.h>
29 #define IrSR_RXPL_NEG_IS_ZERO (1<<4)
30 #define IrSR_RXPL_POS_IS_ZERO 0x0
31 #define IrSR_TXPL_NEG_IS_ZERO (1<<3)
32 #define IrSR_TXPL_POS_IS_ZERO 0x0
33 #define IrSR_XMODE_PULSE_1_6 (1<<2)
34 #define IrSR_XMODE_PULSE_3_16 0x0
35 #define IrSR_RCVEIR_IR_MODE (1<<1)
36 #define IrSR_RCVEIR_UART_MODE 0x0
37 #define IrSR_XMITIR_IR_MODE (1<<0)
38 #define IrSR_XMITIR_UART_MODE 0x0
40 #define IrSR_IR_RECEIVE_ON (\
41 IrSR_RXPL_NEG_IS_ZERO | \
42 IrSR_TXPL_POS_IS_ZERO | \
43 IrSR_XMODE_PULSE_3_16 | \
44 IrSR_RCVEIR_IR_MODE | \
45 IrSR_XMITIR_UART_MODE)
47 #define IrSR_IR_TRANSMIT_ON (\
48 IrSR_RXPL_NEG_IS_ZERO | \
49 IrSR_TXPL_POS_IS_ZERO | \
50 IrSR_XMODE_PULSE_3_16 | \
51 IrSR_RCVEIR_UART_MODE | \
57 unsigned long last_oscr;
59 unsigned char *dma_rx_buff;
60 unsigned char *dma_tx_buff;
61 dma_addr_t dma_rx_buff_phy;
62 dma_addr_t dma_tx_buff_phy;
63 unsigned int dma_tx_buff_len;
67 struct net_device_stats stats;
68 struct irlap_cb *irlap;
75 struct pxaficp_platform_data *pdata;
81 static inline void pxa_irda_disable_clk(struct pxa_irda *si)
84 clk_disable(si->cur_clk);
88 static inline void pxa_irda_enable_firclk(struct pxa_irda *si)
90 si->cur_clk = si->fir_clk;
91 clk_enable(si->fir_clk);
94 static inline void pxa_irda_enable_sirclk(struct pxa_irda *si)
96 si->cur_clk = si->sir_clk;
97 clk_enable(si->sir_clk);
101 #define IS_FIR(si) ((si)->speed >= 4000000)
102 #define IRDA_FRAME_SIZE_LIMIT 2047
104 inline static void pxa_irda_fir_dma_rx_start(struct pxa_irda *si)
106 DCSR(si->rxdma) = DCSR_NODESC;
107 DSADR(si->rxdma) = __PREG(ICDR);
108 DTADR(si->rxdma) = si->dma_rx_buff_phy;
109 DCMD(si->rxdma) = DCMD_INCTRGADDR | DCMD_FLOWSRC | DCMD_WIDTH1 | DCMD_BURST32 | IRDA_FRAME_SIZE_LIMIT;
110 DCSR(si->rxdma) |= DCSR_RUN;
113 inline static void pxa_irda_fir_dma_tx_start(struct pxa_irda *si)
115 DCSR(si->txdma) = DCSR_NODESC;
116 DSADR(si->txdma) = si->dma_tx_buff_phy;
117 DTADR(si->txdma) = __PREG(ICDR);
118 DCMD(si->txdma) = DCMD_INCSRCADDR | DCMD_FLOWTRG | DCMD_ENDIRQEN | DCMD_WIDTH1 | DCMD_BURST32 | si->dma_tx_buff_len;
119 DCSR(si->txdma) |= DCSR_RUN;
123 * Set the IrDA communications speed.
125 static int pxa_irda_set_speed(struct pxa_irda *si, int speed)
128 unsigned int divisor;
131 case 9600: case 19200: case 38400:
132 case 57600: case 115200:
134 /* refer to PXA250/210 Developer's Manual 10-7 */
135 /* BaudRate = 14.7456 MHz / (16*Divisor) */
136 divisor = 14745600 / (16 * speed);
138 local_irq_save(flags);
142 DCSR(si->rxdma) &= ~DCSR_RUN;
145 pxa_irda_disable_clk(si);
147 /* set board transceiver to SIR mode */
148 si->pdata->transceiver_mode(si->dev, IR_SIRMODE);
150 /* enable the STUART clock */
151 pxa_irda_enable_sirclk(si);
154 /* disable STUART first */
157 /* access DLL & DLH */
159 STDLL = divisor & 0xff;
160 STDLH = divisor >> 8;
164 STISR = IrSR_IR_RECEIVE_ON | IrSR_XMODE_PULSE_1_6;
165 STIER = IER_UUE | IER_RLSE | IER_RAVIE | IER_RTIOE;
167 local_irq_restore(flags);
171 local_irq_save(flags);
176 pxa_irda_disable_clk(si);
178 /* disable FICP first */
181 /* set board transceiver to FIR mode */
182 si->pdata->transceiver_mode(si->dev, IR_FIRMODE);
184 /* enable the FICP clock */
185 pxa_irda_enable_firclk(si);
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 si->last_oscr = OSCR;
231 case 0x04: /* Received Data Available */
234 case 0x0C: /* Character Timeout Indication */
236 si->stats.rx_bytes++;
237 async_unwrap_char(dev, &si->stats, &si->rx_buff, STRBR);
238 } while (STLSR & LSR_DR);
239 si->last_oscr = OSCR;
242 case 0x02: /* Transmit FIFO Data Request */
243 while ((si->tx_buff.len) && (STLSR & LSR_TDRQ)) {
244 STTHR = *si->tx_buff.data++;
245 si->tx_buff.len -= 1;
248 if (si->tx_buff.len == 0) {
249 si->stats.tx_packets++;
250 si->stats.tx_bytes += si->tx_buff.data -
253 /* We need to ensure that the transmitter has finished. */
254 while ((STLSR & LSR_TEMT) == 0)
256 si->last_oscr = OSCR;
259 * Ok, we've finished transmitting. Now enable
260 * the receiver. Sometimes we get a receive IRQ
261 * immediately after a transmit...
264 pxa_irda_set_speed(si, si->newspeed);
267 /* enable IR Receiver, disable IR Transmitter */
268 STISR = IrSR_IR_RECEIVE_ON | IrSR_XMODE_PULSE_1_6;
269 /* enable STUART and receive interrupts */
270 STIER = IER_UUE | IER_RLSE | IER_RAVIE | IER_RTIOE;
273 netif_wake_queue(dev);
281 /* FIR Receive DMA interrupt handler */
282 static void pxa_irda_fir_dma_rx_irq(int channel, void *data)
284 int dcsr = DCSR(channel);
286 DCSR(channel) = dcsr & ~DCSR_RUN;
288 printk(KERN_DEBUG "pxa_ir: fir rx dma bus error %#x\n", dcsr);
291 /* FIR Transmit DMA interrupt handler */
292 static void pxa_irda_fir_dma_tx_irq(int channel, void *data)
294 struct net_device *dev = data;
295 struct pxa_irda *si = netdev_priv(dev);
298 dcsr = DCSR(channel);
299 DCSR(channel) = dcsr & ~DCSR_RUN;
301 if (dcsr & DCSR_ENDINTR) {
302 si->stats.tx_packets++;
303 si->stats.tx_bytes += si->dma_tx_buff_len;
305 si->stats.tx_errors++;
308 while (ICSR1 & ICSR1_TBY)
310 si->last_oscr = OSCR;
313 * HACK: It looks like the TBY bit is dropped too soon.
314 * Without this delay things break.
319 pxa_irda_set_speed(si, si->newspeed);
325 pxa_irda_fir_dma_rx_start(si);
326 while ((ICSR1 & ICSR1_RNE) && i--)
328 ICCR0 = ICCR0_ITR | ICCR0_RXE;
331 printk(KERN_ERR "pxa_ir: cannot clear Rx FIFO!\n");
333 netif_wake_queue(dev);
336 /* EIF(Error in FIFO/End in Frame) handler for FIR */
337 static void pxa_irda_fir_irq_eif(struct pxa_irda *si, struct net_device *dev, int icsr0)
339 unsigned int len, stat, data;
341 /* Get the current data position. */
342 len = DTADR(si->rxdma) - si->dma_rx_buff_phy;
345 /* Read Status, and then Data. */
350 if (stat & (ICSR1_CRE | ICSR1_ROR)) {
351 si->stats.rx_errors++;
352 if (stat & ICSR1_CRE) {
353 printk(KERN_DEBUG "pxa_ir: fir receive CRC error\n");
354 si->stats.rx_crc_errors++;
356 if (stat & ICSR1_ROR) {
357 printk(KERN_DEBUG "pxa_ir: fir receive overrun\n");
358 si->stats.rx_over_errors++;
361 si->dma_rx_buff[len++] = data;
363 /* If we hit the end of frame, there's no point in continuing. */
364 if (stat & ICSR1_EOF)
366 } while (ICSR0 & ICSR0_EIF);
368 if (stat & ICSR1_EOF) {
372 if (icsr0 & ICSR0_FRE) {
373 printk(KERN_ERR "pxa_ir: dropping erroneous frame\n");
374 si->stats.rx_dropped++;
378 skb = alloc_skb(len+1,GFP_ATOMIC);
380 printk(KERN_ERR "pxa_ir: fir out of memory for receive skb\n");
381 si->stats.rx_dropped++;
385 /* Align IP header to 20 bytes */
387 skb_copy_to_linear_data(skb, si->dma_rx_buff, len);
390 /* Feed it to IrLAP */
392 skb_reset_mac_header(skb);
393 skb->protocol = htons(ETH_P_IRDA);
396 si->stats.rx_packets++;
397 si->stats.rx_bytes += len;
401 /* FIR interrupt handler */
402 static irqreturn_t pxa_irda_fir_irq(int irq, void *dev_id)
404 struct net_device *dev = dev_id;
405 struct pxa_irda *si = netdev_priv(dev);
409 DCSR(si->rxdma) &= ~DCSR_RUN;
410 si->last_oscr = OSCR;
413 if (icsr0 & (ICSR0_FRE | ICSR0_RAB)) {
414 if (icsr0 & ICSR0_FRE) {
415 printk(KERN_DEBUG "pxa_ir: fir receive frame error\n");
416 si->stats.rx_frame_errors++;
418 printk(KERN_DEBUG "pxa_ir: fir receive abort\n");
419 si->stats.rx_errors++;
421 ICSR0 = icsr0 & (ICSR0_FRE | ICSR0_RAB);
424 if (icsr0 & ICSR0_EIF) {
425 /* An error in FIFO occured, or there is a end of frame */
426 pxa_irda_fir_irq_eif(si, dev, icsr0);
430 pxa_irda_fir_dma_rx_start(si);
431 while ((ICSR1 & ICSR1_RNE) && i--)
433 ICCR0 = ICCR0_ITR | ICCR0_RXE;
436 printk(KERN_ERR "pxa_ir: cannot clear Rx FIFO!\n");
441 /* hard_xmit interface of irda device */
442 static int pxa_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
444 struct pxa_irda *si = netdev_priv(dev);
445 int speed = irda_get_next_speed(skb);
448 * Does this packet contain a request to change the interface
449 * speed? If so, remember it until we complete the transmission
452 if (speed != si->speed && speed != -1)
453 si->newspeed = speed;
456 * If this is an empty frame, we can bypass a lot.
461 pxa_irda_set_speed(si, speed);
467 netif_stop_queue(dev);
470 si->tx_buff.data = si->tx_buff.head;
471 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data, si->tx_buff.truesize);
473 /* Disable STUART interrupts and switch to transmit mode. */
475 STISR = IrSR_IR_TRANSMIT_ON | IrSR_XMODE_PULSE_1_6;
477 /* enable STUART and transmit interrupts */
478 STIER = IER_UUE | IER_TIE;
480 unsigned long mtt = irda_get_mtt(skb);
482 si->dma_tx_buff_len = skb->len;
483 skb_copy_from_linear_data(skb, si->dma_tx_buff, skb->len);
486 while ((unsigned)(OSCR - si->last_oscr)/4 < mtt)
489 /* stop RX DMA, disable FICP */
490 DCSR(si->rxdma) &= ~DCSR_RUN;
493 pxa_irda_fir_dma_tx_start(si);
494 ICCR0 = ICCR0_ITR | ICCR0_TXE;
498 dev->trans_start = jiffies;
502 static int pxa_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
504 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
505 struct pxa_irda *si = netdev_priv(dev);
511 if (capable(CAP_NET_ADMIN)) {
513 * We are unable to set the speed if the
514 * device is not running.
516 if (netif_running(dev)) {
517 ret = pxa_irda_set_speed(si,
520 printk(KERN_INFO "pxa_ir: SIOCSBANDWIDTH: !netif_running\n");
528 if (capable(CAP_NET_ADMIN)) {
529 irda_device_set_media_busy(dev, TRUE);
536 rq->ifr_receiving = IS_FIR(si) ? 0
537 : si->rx_buff.state != OUTSIDE_FRAME;
548 static struct net_device_stats *pxa_irda_stats(struct net_device *dev)
550 struct pxa_irda *si = netdev_priv(dev);
554 static void pxa_irda_startup(struct pxa_irda *si)
556 /* Disable STUART interrupts */
558 /* enable STUART interrupt to the processor */
560 /* configure SIR frame format: StartBit - Data 7 ... Data 0 - Stop Bit */
561 STLCR = LCR_WLS0 | LCR_WLS1;
562 /* enable FIFO, we use FIFO to improve performance */
563 STFCR = FCR_TRFIFOE | FCR_ITL_32;
567 /* configure FICP ICCR2 */
568 ICCR2 = ICCR2_TXP | ICCR2_TRIG_32;
571 DRCMR(17) = si->rxdma | DRCMR_MAPVLD;
572 DRCMR(18) = si->txdma | DRCMR_MAPVLD;
574 /* force SIR reinitialization */
576 pxa_irda_set_speed(si, 9600);
578 printk(KERN_DEBUG "pxa_ir: irda startup\n");
581 static void pxa_irda_shutdown(struct pxa_irda *si)
585 local_irq_save(flags);
587 /* disable STUART and interrupt */
589 /* disable STUART SIR mode */
593 DCSR(si->txdma) &= ~DCSR_RUN;
594 DCSR(si->rxdma) &= ~DCSR_RUN;
598 /* disable the STUART or FICP clocks */
599 pxa_irda_disable_clk(si);
604 local_irq_restore(flags);
606 /* power off board transceiver */
607 si->pdata->transceiver_mode(si->dev, IR_OFF);
609 printk(KERN_DEBUG "pxa_ir: irda shutdown\n");
612 static int pxa_irda_start(struct net_device *dev)
614 struct pxa_irda *si = netdev_priv(dev);
619 err = request_irq(IRQ_STUART, pxa_irda_sir_irq, 0, dev->name, dev);
623 err = request_irq(IRQ_ICP, pxa_irda_fir_irq, 0, dev->name, dev);
628 * The interrupt must remain disabled for now.
630 disable_irq(IRQ_STUART);
631 disable_irq(IRQ_ICP);
634 si->rxdma = pxa_request_dma("FICP_RX",DMA_PRIO_LOW, pxa_irda_fir_dma_rx_irq, dev);
638 si->txdma = pxa_request_dma("FICP_TX",DMA_PRIO_LOW, pxa_irda_fir_dma_tx_irq, dev);
643 si->dma_rx_buff = dma_alloc_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT,
644 &si->dma_rx_buff_phy, GFP_KERNEL );
645 if (!si->dma_rx_buff)
646 goto err_dma_rx_buff;
648 si->dma_tx_buff = dma_alloc_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT,
649 &si->dma_tx_buff_phy, GFP_KERNEL );
650 if (!si->dma_tx_buff)
651 goto err_dma_tx_buff;
653 /* Setup the serial port for the initial speed. */
654 pxa_irda_startup(si);
657 * Open a new IrLAP layer instance.
659 si->irlap = irlap_open(dev, &si->qos, "pxa");
665 * Now enable the interrupt and start the queue
667 enable_irq(IRQ_STUART);
669 netif_start_queue(dev);
671 printk(KERN_DEBUG "pxa_ir: irda driver opened\n");
676 pxa_irda_shutdown(si);
677 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_tx_buff, si->dma_tx_buff_phy);
679 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_rx_buff, si->dma_rx_buff_phy);
681 pxa_free_dma(si->txdma);
683 pxa_free_dma(si->rxdma);
685 free_irq(IRQ_ICP, dev);
687 free_irq(IRQ_STUART, dev);
693 static int pxa_irda_stop(struct net_device *dev)
695 struct pxa_irda *si = netdev_priv(dev);
697 netif_stop_queue(dev);
699 pxa_irda_shutdown(si);
703 irlap_close(si->irlap);
707 free_irq(IRQ_STUART, dev);
708 free_irq(IRQ_ICP, dev);
710 pxa_free_dma(si->rxdma);
711 pxa_free_dma(si->txdma);
714 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_tx_buff, si->dma_tx_buff_phy);
716 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_rx_buff, si->dma_rx_buff_phy);
718 printk(KERN_DEBUG "pxa_ir: irda driver closed\n");
722 static int pxa_irda_suspend(struct platform_device *_dev, pm_message_t state)
724 struct net_device *dev = platform_get_drvdata(_dev);
727 if (dev && netif_running(dev)) {
728 si = netdev_priv(dev);
729 netif_device_detach(dev);
730 pxa_irda_shutdown(si);
736 static int pxa_irda_resume(struct platform_device *_dev)
738 struct net_device *dev = platform_get_drvdata(_dev);
741 if (dev && netif_running(dev)) {
742 si = netdev_priv(dev);
743 pxa_irda_startup(si);
744 netif_device_attach(dev);
745 netif_wake_queue(dev);
752 static int pxa_irda_init_iobuf(iobuff_t *io, int size)
754 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
755 if (io->head != NULL) {
757 io->in_frame = FALSE;
758 io->state = OUTSIDE_FRAME;
761 return io->head ? 0 : -ENOMEM;
764 static int pxa_irda_probe(struct platform_device *pdev)
766 struct net_device *dev;
768 unsigned int baudrate_mask;
771 if (!pdev->dev.platform_data)
774 err = request_mem_region(__PREG(STUART), 0x24, "IrDA") ? 0 : -EBUSY;
778 err = request_mem_region(__PREG(FICP), 0x1c, "IrDA") ? 0 : -EBUSY;
782 dev = alloc_irdadev(sizeof(struct pxa_irda));
786 si = netdev_priv(dev);
787 si->dev = &pdev->dev;
788 si->pdata = pdev->dev.platform_data;
790 si->sir_clk = clk_get(&pdev->dev, "UARTCLK");
791 si->fir_clk = clk_get(&pdev->dev, "FICPCLK");
792 if (IS_ERR(si->sir_clk) || IS_ERR(si->fir_clk)) {
793 err = PTR_ERR(IS_ERR(si->sir_clk) ? si->sir_clk : si->fir_clk);
798 * Initialise the SIR buffers
800 err = pxa_irda_init_iobuf(&si->rx_buff, 14384);
803 err = pxa_irda_init_iobuf(&si->tx_buff, 4000);
807 if (si->pdata->startup)
808 err = si->pdata->startup(si->dev);
812 dev->hard_start_xmit = pxa_irda_hard_xmit;
813 dev->open = pxa_irda_start;
814 dev->stop = pxa_irda_stop;
815 dev->do_ioctl = pxa_irda_ioctl;
816 dev->get_stats = pxa_irda_stats;
818 irda_init_max_qos_capabilies(&si->qos);
821 if (si->pdata->transceiver_cap & IR_SIRMODE)
822 baudrate_mask |= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
823 if (si->pdata->transceiver_cap & IR_FIRMODE)
824 baudrate_mask |= IR_4000000 << 8;
826 si->qos.baud_rate.bits &= baudrate_mask;
827 si->qos.min_turn_time.bits = 7; /* 1ms or more */
829 irda_qos_bits_to_value(&si->qos);
831 err = register_netdev(dev);
834 dev_set_drvdata(&pdev->dev, dev);
837 if (si->pdata->shutdown)
838 si->pdata->shutdown(si->dev);
840 kfree(si->tx_buff.head);
842 kfree(si->rx_buff.head);
844 if (si->sir_clk && !IS_ERR(si->sir_clk))
845 clk_put(si->sir_clk);
846 if (si->fir_clk && !IS_ERR(si->fir_clk))
847 clk_put(si->fir_clk);
850 release_mem_region(__PREG(FICP), 0x1c);
852 release_mem_region(__PREG(STUART), 0x24);
858 static int pxa_irda_remove(struct platform_device *_dev)
860 struct net_device *dev = platform_get_drvdata(_dev);
863 struct pxa_irda *si = netdev_priv(dev);
864 unregister_netdev(dev);
865 if (si->pdata->shutdown)
866 si->pdata->shutdown(si->dev);
867 kfree(si->tx_buff.head);
868 kfree(si->rx_buff.head);
869 clk_put(si->fir_clk);
870 clk_put(si->sir_clk);
874 release_mem_region(__PREG(STUART), 0x24);
875 release_mem_region(__PREG(FICP), 0x1c);
880 static struct platform_driver pxa_ir_driver = {
883 .owner = THIS_MODULE,
885 .probe = pxa_irda_probe,
886 .remove = pxa_irda_remove,
887 .suspend = pxa_irda_suspend,
888 .resume = pxa_irda_resume,
891 static int __init pxa_irda_init(void)
893 return platform_driver_register(&pxa_ir_driver);
896 static void __exit pxa_irda_exit(void)
898 platform_driver_unregister(&pxa_ir_driver);
901 module_init(pxa_irda_init);
902 module_exit(pxa_irda_exit);
904 MODULE_LICENSE("GPL");
905 MODULE_ALIAS("platform:pxa2xx-ir");