2 * linux/drivers/net/irda/sa1100_ir.c
4 * Copyright (C) 2000-2001 Russell King
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Infra-red driver for the StrongARM SA1100 embedded microprocessor
12 * Note that we don't have to worry about the SA1111's DMA bugs in here,
13 * so we use the straight forward dma_map_* functions with a null pointer.
15 * This driver takes one kernel command line parameter, sa1100ir=, with
16 * the following options:
17 * max_rate:baudrate - set the maximum baud rate
18 * power_leve:level - set the transmitter power level
19 * tx_lpm:0|1 - set transmit low power mode
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/types.h>
24 #include <linux/init.h>
25 #include <linux/errno.h>
26 #include <linux/netdevice.h>
27 #include <linux/slab.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <linux/platform_device.h>
32 #include <linux/dma-mapping.h>
34 #include <net/irda/irda.h>
35 #include <net/irda/wrapper.h>
36 #include <net/irda/irda_device.h>
40 #include <mach/hardware.h>
41 #include <asm/mach/irda.h>
43 static int power_level = 3;
45 static int max_rate = 4000000;
56 struct sk_buff *txskb;
57 struct sk_buff *rxskb;
64 struct irda_platform_data *pdata;
65 struct irlap_cb *irlap;
72 #define IS_FIR(si) ((si)->speed >= 4000000)
74 #define HPSIR_MAX_RXLEN 2047
77 * Allocate and map the receive buffer, unless it is already allocated.
79 static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
84 si->rxskb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
87 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
92 * Align any IP headers that may be contained
95 skb_reserve(si->rxskb, 1);
97 si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
104 * We want to get here as soon as possible, and get the receiver setup.
105 * We use the existing buffer.
107 static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
110 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
115 * First empty receive FIFO
117 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
120 * Enable the DMA, receiver and receive interrupt.
122 sa1100_clear_dma(si->rxdma);
123 sa1100_start_dma(si->rxdma, si->rxbuf_dma, HPSIR_MAX_RXLEN);
124 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
128 * Set the IrDA communications speed.
130 static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
133 int brd, ret = -EINVAL;
136 case 9600: case 19200: case 38400:
137 case 57600: case 115200:
138 brd = 3686400 / (16 * speed) - 1;
141 * Stop the receive DMA.
144 sa1100_stop_dma(si->rxdma);
146 local_irq_save(flags);
149 Ser2HSCR0 = HSCR0_UART;
151 Ser2UTCR1 = brd >> 8;
155 * Clear status register
157 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
158 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
160 if (si->pdata->set_speed)
161 si->pdata->set_speed(si->dev, speed);
165 local_irq_restore(flags);
170 local_irq_save(flags);
175 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
180 if (si->pdata->set_speed)
181 si->pdata->set_speed(si->dev, speed);
183 sa1100_irda_rx_alloc(si);
184 sa1100_irda_rx_dma_start(si);
186 local_irq_restore(flags);
198 * Control the power state of the IrDA transmitter.
201 * 1 - short range, lowest power
202 * 2 - medium range, medium power
203 * 3 - maximum range, high power
205 * Currently, only assabet is known to support this.
208 __sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
211 if (si->pdata->set_power)
212 ret = si->pdata->set_power(si->dev, state);
217 sa1100_set_power(struct sa1100_irda *si, unsigned int state)
221 ret = __sa1100_irda_set_power(si, state);
228 static int sa1100_irda_startup(struct sa1100_irda *si)
233 * Ensure that the ports for this device are setup correctly.
235 if (si->pdata->startup)
236 si->pdata->startup(si->dev);
239 * Configure PPC for IRDA - we want to drive TXD2 low.
240 * We also want to drive this pin low during sleep.
247 * Enable HP-SIR modulation, and ensure that the port is disabled.
250 Ser2HSCR0 = HSCR0_UART;
251 Ser2UTCR4 = si->utcr4;
252 Ser2UTCR0 = UTCR0_8BitData;
253 Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
256 * Clear status register
258 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
260 ret = sa1100_irda_set_speed(si, si->speed = 9600);
265 if (si->pdata->shutdown)
266 si->pdata->shutdown(si->dev);
272 static void sa1100_irda_shutdown(struct sa1100_irda *si)
275 * Stop all DMA activity.
277 sa1100_stop_dma(si->rxdma);
278 sa1100_stop_dma(si->txdma);
280 /* Disable the port. */
284 if (si->pdata->shutdown)
285 si->pdata->shutdown(si->dev);
290 * Suspend the IrDA interface.
292 static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
294 struct net_device *dev = platform_get_drvdata(pdev);
295 struct sa1100_irda *si;
300 si = netdev_priv(dev);
303 * Stop the transmit queue
305 netif_device_detach(dev);
306 disable_irq(dev->irq);
307 sa1100_irda_shutdown(si);
308 __sa1100_irda_set_power(si, 0);
315 * Resume the IrDA interface.
317 static int sa1100_irda_resume(struct platform_device *pdev)
319 struct net_device *dev = platform_get_drvdata(pdev);
320 struct sa1100_irda *si;
325 si = netdev_priv(dev);
328 * If we missed a speed change, initialise at the new speed
329 * directly. It is debatable whether this is actually
330 * required, but in the interests of continuing from where
331 * we left off it is desireable. The converse argument is
332 * that we should re-negotiate at 9600 baud again.
335 si->speed = si->newspeed;
339 sa1100_irda_startup(si);
340 __sa1100_irda_set_power(si, si->power);
341 enable_irq(dev->irq);
344 * This automatically wakes up the queue
346 netif_device_attach(dev);
352 #define sa1100_irda_suspend NULL
353 #define sa1100_irda_resume NULL
357 * HP-SIR format interrupt service routines.
359 static void sa1100_irda_hpsir_irq(struct net_device *dev)
361 struct sa1100_irda *si = netdev_priv(dev);
367 * Deal with any receive errors first. The bytes in error may be
368 * the only bytes in the receive FIFO, so we do this first.
370 while (status & UTSR0_EIF) {
376 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
377 dev->stats.rx_errors++;
378 if (stat & UTSR1_FRE)
379 dev->stats.rx_frame_errors++;
380 if (stat & UTSR1_ROR)
381 dev->stats.rx_fifo_errors++;
383 async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
389 * We must clear certain bits.
391 Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
393 if (status & UTSR0_RFS) {
395 * There are at least 4 bytes in the FIFO. Read 3 bytes
396 * and leave the rest to the block below.
398 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
399 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
400 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
403 if (status & (UTSR0_RFS | UTSR0_RID)) {
405 * Fifo contains more than 1 character.
408 async_unwrap_char(dev, &dev->stats, &si->rx_buff,
410 } while (Ser2UTSR1 & UTSR1_RNE);
414 if (status & UTSR0_TFS && si->tx_buff.len) {
416 * Transmitter FIFO is not full
419 Ser2UTDR = *si->tx_buff.data++;
420 si->tx_buff.len -= 1;
421 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
423 if (si->tx_buff.len == 0) {
424 dev->stats.tx_packets++;
425 dev->stats.tx_bytes += si->tx_buff.data -
429 * We need to ensure that the transmitter has
434 while (Ser2UTSR1 & UTSR1_TBY);
437 * Ok, we've finished transmitting. Now enable
438 * the receiver. Sometimes we get a receive IRQ
439 * immediately after a transmit...
441 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
442 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
445 sa1100_irda_set_speed(si, si->newspeed);
450 netif_wake_queue(dev);
455 static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
457 struct sk_buff *skb = si->rxskb;
459 unsigned int len, stat, data;
462 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
467 * Get the current data position.
469 dma_addr = sa1100_get_dma_pos(si->rxdma);
470 len = dma_addr - si->rxbuf_dma;
471 if (len > HPSIR_MAX_RXLEN)
472 len = HPSIR_MAX_RXLEN;
473 dma_unmap_single(si->dev, si->rxbuf_dma, len, DMA_FROM_DEVICE);
477 * Read Status, and then Data.
483 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
484 dev->stats.rx_errors++;
485 if (stat & HSSR1_CRE)
486 dev->stats.rx_crc_errors++;
487 if (stat & HSSR1_ROR)
488 dev->stats.rx_frame_errors++;
490 skb->data[len++] = data;
493 * If we hit the end of frame, there's
494 * no point in continuing.
496 if (stat & HSSR1_EOF)
498 } while (Ser2HSSR0 & HSSR0_EIF);
500 if (stat & HSSR1_EOF) {
505 skb_reset_mac_header(skb);
506 skb->protocol = htons(ETH_P_IRDA);
507 dev->stats.rx_packets++;
508 dev->stats.rx_bytes += len;
511 * Before we pass the buffer up, allocate a new one.
513 sa1100_irda_rx_alloc(si);
520 si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
527 * FIR format interrupt service routine. We only have to
528 * handle RX events; transmit events go via the TX DMA handler.
530 * No matter what, we disable RX, process, and the restart RX.
532 static void sa1100_irda_fir_irq(struct net_device *dev)
534 struct sa1100_irda *si = netdev_priv(dev);
539 sa1100_stop_dma(si->rxdma);
542 * Framing error - we throw away the packet completely.
543 * Clearing RXE flushes the error conditions and data
546 if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
547 dev->stats.rx_errors++;
549 if (Ser2HSSR0 & HSSR0_FRE)
550 dev->stats.rx_frame_errors++;
553 * Clear out the DMA...
555 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
558 * Clear selected status bits now, so we
559 * don't miss them next time around.
561 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
565 * Deal with any receive errors. The any of the lowest
566 * 8 bytes in the FIFO may contain an error. We must read
567 * them one by one. The "error" could even be the end of
570 if (Ser2HSSR0 & HSSR0_EIF)
571 sa1100_irda_fir_error(si, dev);
574 * No matter what happens, we must restart reception.
576 sa1100_irda_rx_dma_start(si);
579 static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
581 struct net_device *dev = dev_id;
582 if (IS_FIR(((struct sa1100_irda *)netdev_priv(dev))))
583 sa1100_irda_fir_irq(dev);
585 sa1100_irda_hpsir_irq(dev);
590 * TX DMA completion handler.
592 static void sa1100_irda_txdma_irq(void *id)
594 struct net_device *dev = id;
595 struct sa1100_irda *si = netdev_priv(dev);
596 struct sk_buff *skb = si->txskb;
601 * Wait for the transmission to complete. Unfortunately,
602 * the hardware doesn't give us an interrupt to indicate
607 while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
610 * Clear the transmit underrun bit.
612 Ser2HSSR0 = HSSR0_TUR;
615 * Do we need to change speed? Note that we're lazy
616 * here - we don't free the old rxskb. We don't need
617 * to allocate a buffer either.
620 sa1100_irda_set_speed(si, si->newspeed);
625 * Start reception. This disables the transmitter for
626 * us. This will be using the existing RX buffer.
628 sa1100_irda_rx_dma_start(si);
631 * Account and free the packet.
634 dma_unmap_single(si->dev, si->txbuf_dma, skb->len, DMA_TO_DEVICE);
635 dev->stats.tx_packets ++;
636 dev->stats.tx_bytes += skb->len;
637 dev_kfree_skb_irq(skb);
641 * Make sure that the TX queue is available for sending
642 * (for retries). TX has priority over RX at all times.
644 netif_wake_queue(dev);
647 static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
649 struct sa1100_irda *si = netdev_priv(dev);
650 int speed = irda_get_next_speed(skb);
653 * Does this packet contain a request to change the interface
654 * speed? If so, remember it until we complete the transmission
657 if (speed != si->speed && speed != -1)
658 si->newspeed = speed;
661 * If this is an empty frame, we can bypass a lot.
666 sa1100_irda_set_speed(si, speed);
673 netif_stop_queue(dev);
675 si->tx_buff.data = si->tx_buff.head;
676 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
677 si->tx_buff.truesize);
680 * Set the transmit interrupt enable. This will fire
681 * off an interrupt immediately. Note that we disable
682 * the receiver so we won't get spurious characteres
685 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
689 int mtt = irda_get_mtt(skb);
692 * We must not be transmitting...
696 netif_stop_queue(dev);
699 si->txbuf_dma = dma_map_single(si->dev, skb->data,
700 skb->len, DMA_TO_DEVICE);
702 sa1100_start_dma(si->txdma, si->txbuf_dma, skb->len);
705 * If we have a mean turn-around time, impose the specified
706 * specified delay. We could shorten this by timing from
707 * the point we received the packet.
712 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
715 dev->trans_start = jiffies;
721 sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
723 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
724 struct sa1100_irda *si = netdev_priv(dev);
725 int ret = -EOPNOTSUPP;
729 if (capable(CAP_NET_ADMIN)) {
731 * We are unable to set the speed if the
732 * device is not running.
735 ret = sa1100_irda_set_speed(si,
738 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
746 if (capable(CAP_NET_ADMIN)) {
747 irda_device_set_media_busy(dev, TRUE);
753 rq->ifr_receiving = IS_FIR(si) ? 0
754 : si->rx_buff.state != OUTSIDE_FRAME;
764 static int sa1100_irda_start(struct net_device *dev)
766 struct sa1100_irda *si = netdev_priv(dev);
771 err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
775 err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
776 NULL, NULL, &si->rxdma);
780 err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
781 sa1100_irda_txdma_irq, dev, &si->txdma);
786 * The interrupt must remain disabled for now.
788 disable_irq(dev->irq);
791 * Setup the serial port for the specified speed.
793 err = sa1100_irda_startup(si);
798 * Open a new IrLAP layer instance.
800 si->irlap = irlap_open(dev, &si->qos, "sa1100");
806 * Now enable the interrupt and start the queue
809 sa1100_set_power(si, power_level); /* low power mode */
810 enable_irq(dev->irq);
811 netif_start_queue(dev);
816 sa1100_irda_shutdown(si);
818 sa1100_free_dma(si->txdma);
820 sa1100_free_dma(si->rxdma);
822 free_irq(dev->irq, dev);
827 static int sa1100_irda_stop(struct net_device *dev)
829 struct sa1100_irda *si = netdev_priv(dev);
831 disable_irq(dev->irq);
832 sa1100_irda_shutdown(si);
835 * If we have been doing DMA receive, make sure we
836 * tidy that up cleanly.
839 dma_unmap_single(si->dev, si->rxbuf_dma, HPSIR_MAX_RXLEN,
841 dev_kfree_skb(si->rxskb);
847 irlap_close(si->irlap);
851 netif_stop_queue(dev);
857 sa1100_free_dma(si->txdma);
858 sa1100_free_dma(si->rxdma);
859 free_irq(dev->irq, dev);
861 sa1100_set_power(si, 0);
866 static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
868 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
869 if (io->head != NULL) {
871 io->in_frame = FALSE;
872 io->state = OUTSIDE_FRAME;
875 return io->head ? 0 : -ENOMEM;
878 static int sa1100_irda_probe(struct platform_device *pdev)
880 struct net_device *dev;
881 struct sa1100_irda *si;
882 unsigned int baudrate_mask;
885 if (!pdev->dev.platform_data)
888 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
891 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
894 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
898 dev = alloc_irdadev(sizeof(struct sa1100_irda));
902 si = netdev_priv(dev);
903 si->dev = &pdev->dev;
904 si->pdata = pdev->dev.platform_data;
907 * Initialise the HP-SIR buffers
909 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
912 err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
916 dev->hard_start_xmit = sa1100_irda_hard_xmit;
917 dev->open = sa1100_irda_start;
918 dev->stop = sa1100_irda_stop;
919 dev->do_ioctl = sa1100_irda_ioctl;
920 dev->irq = IRQ_Ser2ICP;
922 irda_init_max_qos_capabilies(&si->qos);
925 * We support original IRDA up to 115k2. (we don't currently
926 * support 4Mbps). Min Turn Time set to 1ms or greater.
928 baudrate_mask = IR_9600;
931 case 4000000: baudrate_mask |= IR_4000000 << 8;
932 case 115200: baudrate_mask |= IR_115200;
933 case 57600: baudrate_mask |= IR_57600;
934 case 38400: baudrate_mask |= IR_38400;
935 case 19200: baudrate_mask |= IR_19200;
938 si->qos.baud_rate.bits &= baudrate_mask;
939 si->qos.min_turn_time.bits = 7;
941 irda_qos_bits_to_value(&si->qos);
943 si->utcr4 = UTCR4_HPSIR;
945 si->utcr4 |= UTCR4_Z1_6us;
948 * Initially enable HP-SIR modulation, and ensure that the port
952 Ser2UTCR4 = si->utcr4;
953 Ser2HSCR0 = HSCR0_UART;
955 err = register_netdev(dev);
957 platform_set_drvdata(pdev, dev);
961 kfree(si->tx_buff.head);
962 kfree(si->rx_buff.head);
965 release_mem_region(__PREG(Ser2HSCR2), 0x04);
967 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
969 release_mem_region(__PREG(Ser2UTCR0), 0x24);
975 static int sa1100_irda_remove(struct platform_device *pdev)
977 struct net_device *dev = platform_get_drvdata(pdev);
980 struct sa1100_irda *si = netdev_priv(dev);
981 unregister_netdev(dev);
982 kfree(si->tx_buff.head);
983 kfree(si->rx_buff.head);
987 release_mem_region(__PREG(Ser2HSCR2), 0x04);
988 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
989 release_mem_region(__PREG(Ser2UTCR0), 0x24);
994 static struct platform_driver sa1100ir_driver = {
995 .probe = sa1100_irda_probe,
996 .remove = sa1100_irda_remove,
997 .suspend = sa1100_irda_suspend,
998 .resume = sa1100_irda_resume,
1000 .name = "sa11x0-ir",
1001 .owner = THIS_MODULE,
1005 static int __init sa1100_irda_init(void)
1008 * Limit power level a sensible range.
1010 if (power_level < 1)
1012 if (power_level > 3)
1015 return platform_driver_register(&sa1100ir_driver);
1018 static void __exit sa1100_irda_exit(void)
1020 platform_driver_unregister(&sa1100ir_driver);
1023 module_init(sa1100_irda_init);
1024 module_exit(sa1100_irda_exit);
1025 module_param(power_level, int, 0);
1026 module_param(tx_lpm, int, 0);
1027 module_param(max_rate, int, 0);
1029 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1030 MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1031 MODULE_LICENSE("GPL");
1032 MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1033 MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1034 MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
1035 MODULE_ALIAS("platform:sa11x0-ir");