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/config.h>
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/types.h>
25 #include <linux/init.h>
26 #include <linux/errno.h>
27 #include <linux/netdevice.h>
28 #include <linux/slab.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include <linux/platform_device.h>
33 #include <linux/dma-mapping.h>
35 #include <net/irda/irda.h>
36 #include <net/irda/wrapper.h>
37 #include <net/irda/irda_device.h>
41 #include <asm/hardware.h>
42 #include <asm/mach/irda.h>
44 static int power_level = 3;
46 static int max_rate = 4000000;
57 struct sk_buff *txskb;
58 struct sk_buff *rxskb;
64 struct net_device_stats stats;
66 struct irda_platform_data *pdata;
67 struct irlap_cb *irlap;
74 #define IS_FIR(si) ((si)->speed >= 4000000)
76 #define HPSIR_MAX_RXLEN 2047
79 * Allocate and map the receive buffer, unless it is already allocated.
81 static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
86 si->rxskb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
89 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
94 * Align any IP headers that may be contained
97 skb_reserve(si->rxskb, 1);
99 si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
106 * We want to get here as soon as possible, and get the receiver setup.
107 * We use the existing buffer.
109 static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
112 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
117 * First empty receive FIFO
119 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
122 * Enable the DMA, receiver and receive interrupt.
124 sa1100_clear_dma(si->rxdma);
125 sa1100_start_dma(si->rxdma, si->rxbuf_dma, HPSIR_MAX_RXLEN);
126 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
130 * Set the IrDA communications speed.
132 static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
135 int brd, ret = -EINVAL;
138 case 9600: case 19200: case 38400:
139 case 57600: case 115200:
140 brd = 3686400 / (16 * speed) - 1;
143 * Stop the receive DMA.
146 sa1100_stop_dma(si->rxdma);
148 local_irq_save(flags);
151 Ser2HSCR0 = HSCR0_UART;
153 Ser2UTCR1 = brd >> 8;
157 * Clear status register
159 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
160 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
162 if (si->pdata->set_speed)
163 si->pdata->set_speed(si->dev, speed);
167 local_irq_restore(flags);
172 local_irq_save(flags);
177 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
182 if (si->pdata->set_speed)
183 si->pdata->set_speed(si->dev, speed);
185 sa1100_irda_rx_alloc(si);
186 sa1100_irda_rx_dma_start(si);
188 local_irq_restore(flags);
200 * Control the power state of the IrDA transmitter.
203 * 1 - short range, lowest power
204 * 2 - medium range, medium power
205 * 3 - maximum range, high power
207 * Currently, only assabet is known to support this.
210 __sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
213 if (si->pdata->set_power)
214 ret = si->pdata->set_power(si->dev, state);
219 sa1100_set_power(struct sa1100_irda *si, unsigned int state)
223 ret = __sa1100_irda_set_power(si, state);
230 static int sa1100_irda_startup(struct sa1100_irda *si)
235 * Ensure that the ports for this device are setup correctly.
237 if (si->pdata->startup)
238 si->pdata->startup(si->dev);
241 * Configure PPC for IRDA - we want to drive TXD2 low.
242 * We also want to drive this pin low during sleep.
249 * Enable HP-SIR modulation, and ensure that the port is disabled.
252 Ser2HSCR0 = HSCR0_UART;
253 Ser2UTCR4 = si->utcr4;
254 Ser2UTCR0 = UTCR0_8BitData;
255 Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
258 * Clear status register
260 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
262 ret = sa1100_irda_set_speed(si, si->speed = 9600);
267 if (si->pdata->shutdown)
268 si->pdata->shutdown(si->dev);
274 static void sa1100_irda_shutdown(struct sa1100_irda *si)
277 * Stop all DMA activity.
279 sa1100_stop_dma(si->rxdma);
280 sa1100_stop_dma(si->txdma);
282 /* Disable the port. */
286 if (si->pdata->shutdown)
287 si->pdata->shutdown(si->dev);
292 * Suspend the IrDA interface.
294 static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
296 struct net_device *dev = platform_get_drvdata(pdev);
297 struct sa1100_irda *si;
305 * Stop the transmit queue
307 netif_device_detach(dev);
308 disable_irq(dev->irq);
309 sa1100_irda_shutdown(si);
310 __sa1100_irda_set_power(si, 0);
317 * Resume the IrDA interface.
319 static int sa1100_irda_resume(struct platform_device *pdev)
321 struct net_device *dev = platform_get_drvdata(pdev);
322 struct sa1100_irda *si;
330 * If we missed a speed change, initialise at the new speed
331 * directly. It is debatable whether this is actually
332 * required, but in the interests of continuing from where
333 * we left off it is desireable. The converse argument is
334 * that we should re-negotiate at 9600 baud again.
337 si->speed = si->newspeed;
341 sa1100_irda_startup(si);
342 __sa1100_irda_set_power(si, si->power);
343 enable_irq(dev->irq);
346 * This automatically wakes up the queue
348 netif_device_attach(dev);
354 #define sa1100_irda_suspend NULL
355 #define sa1100_irda_resume NULL
359 * HP-SIR format interrupt service routines.
361 static void sa1100_irda_hpsir_irq(struct net_device *dev)
363 struct sa1100_irda *si = dev->priv;
369 * Deal with any receive errors first. The bytes in error may be
370 * the only bytes in the receive FIFO, so we do this first.
372 while (status & UTSR0_EIF) {
378 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
379 si->stats.rx_errors++;
380 if (stat & UTSR1_FRE)
381 si->stats.rx_frame_errors++;
382 if (stat & UTSR1_ROR)
383 si->stats.rx_fifo_errors++;
385 async_unwrap_char(dev, &si->stats, &si->rx_buff, data);
391 * We must clear certain bits.
393 Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
395 if (status & UTSR0_RFS) {
397 * There are at least 4 bytes in the FIFO. Read 3 bytes
398 * and leave the rest to the block below.
400 async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);
401 async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);
402 async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);
405 if (status & (UTSR0_RFS | UTSR0_RID)) {
407 * Fifo contains more than 1 character.
410 async_unwrap_char(dev, &si->stats, &si->rx_buff,
412 } while (Ser2UTSR1 & UTSR1_RNE);
414 dev->last_rx = jiffies;
417 if (status & UTSR0_TFS && si->tx_buff.len) {
419 * Transmitter FIFO is not full
422 Ser2UTDR = *si->tx_buff.data++;
423 si->tx_buff.len -= 1;
424 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
426 if (si->tx_buff.len == 0) {
427 si->stats.tx_packets++;
428 si->stats.tx_bytes += si->tx_buff.data -
432 * We need to ensure that the transmitter has
437 while (Ser2UTSR1 & UTSR1_TBY);
440 * Ok, we've finished transmitting. Now enable
441 * the receiver. Sometimes we get a receive IRQ
442 * immediately after a transmit...
444 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
445 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
448 sa1100_irda_set_speed(si, si->newspeed);
453 netif_wake_queue(dev);
458 static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
460 struct sk_buff *skb = si->rxskb;
462 unsigned int len, stat, data;
465 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
470 * Get the current data position.
472 dma_addr = sa1100_get_dma_pos(si->rxdma);
473 len = dma_addr - si->rxbuf_dma;
474 if (len > HPSIR_MAX_RXLEN)
475 len = HPSIR_MAX_RXLEN;
476 dma_unmap_single(si->dev, si->rxbuf_dma, len, DMA_FROM_DEVICE);
480 * Read Status, and then Data.
486 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
487 si->stats.rx_errors++;
488 if (stat & HSSR1_CRE)
489 si->stats.rx_crc_errors++;
490 if (stat & HSSR1_ROR)
491 si->stats.rx_frame_errors++;
493 skb->data[len++] = data;
496 * If we hit the end of frame, there's
497 * no point in continuing.
499 if (stat & HSSR1_EOF)
501 } while (Ser2HSSR0 & HSSR0_EIF);
503 if (stat & HSSR1_EOF) {
508 skb->mac.raw = skb->data;
509 skb->protocol = htons(ETH_P_IRDA);
510 si->stats.rx_packets++;
511 si->stats.rx_bytes += len;
514 * Before we pass the buffer up, allocate a new one.
516 sa1100_irda_rx_alloc(si);
519 dev->last_rx = jiffies;
524 si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
531 * FIR format interrupt service routine. We only have to
532 * handle RX events; transmit events go via the TX DMA handler.
534 * No matter what, we disable RX, process, and the restart RX.
536 static void sa1100_irda_fir_irq(struct net_device *dev)
538 struct sa1100_irda *si = dev->priv;
543 sa1100_stop_dma(si->rxdma);
546 * Framing error - we throw away the packet completely.
547 * Clearing RXE flushes the error conditions and data
550 if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
551 si->stats.rx_errors++;
553 if (Ser2HSSR0 & HSSR0_FRE)
554 si->stats.rx_frame_errors++;
557 * Clear out the DMA...
559 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
562 * Clear selected status bits now, so we
563 * don't miss them next time around.
565 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
569 * Deal with any receive errors. The any of the lowest
570 * 8 bytes in the FIFO may contain an error. We must read
571 * them one by one. The "error" could even be the end of
574 if (Ser2HSSR0 & HSSR0_EIF)
575 sa1100_irda_fir_error(si, dev);
578 * No matter what happens, we must restart reception.
580 sa1100_irda_rx_dma_start(si);
583 static irqreturn_t sa1100_irda_irq(int irq, void *dev_id, struct pt_regs *regs)
585 struct net_device *dev = dev_id;
586 if (IS_FIR(((struct sa1100_irda *)dev->priv)))
587 sa1100_irda_fir_irq(dev);
589 sa1100_irda_hpsir_irq(dev);
594 * TX DMA completion handler.
596 static void sa1100_irda_txdma_irq(void *id)
598 struct net_device *dev = id;
599 struct sa1100_irda *si = dev->priv;
600 struct sk_buff *skb = si->txskb;
605 * Wait for the transmission to complete. Unfortunately,
606 * the hardware doesn't give us an interrupt to indicate
611 while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
614 * Clear the transmit underrun bit.
616 Ser2HSSR0 = HSSR0_TUR;
619 * Do we need to change speed? Note that we're lazy
620 * here - we don't free the old rxskb. We don't need
621 * to allocate a buffer either.
624 sa1100_irda_set_speed(si, si->newspeed);
629 * Start reception. This disables the transmitter for
630 * us. This will be using the existing RX buffer.
632 sa1100_irda_rx_dma_start(si);
635 * Account and free the packet.
638 dma_unmap_single(si->dev, si->txbuf_dma, skb->len, DMA_TO_DEVICE);
639 si->stats.tx_packets ++;
640 si->stats.tx_bytes += skb->len;
641 dev_kfree_skb_irq(skb);
645 * Make sure that the TX queue is available for sending
646 * (for retries). TX has priority over RX at all times.
648 netif_wake_queue(dev);
651 static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
653 struct sa1100_irda *si = dev->priv;
654 int speed = irda_get_next_speed(skb);
657 * Does this packet contain a request to change the interface
658 * speed? If so, remember it until we complete the transmission
661 if (speed != si->speed && speed != -1)
662 si->newspeed = speed;
665 * If this is an empty frame, we can bypass a lot.
670 sa1100_irda_set_speed(si, speed);
677 netif_stop_queue(dev);
679 si->tx_buff.data = si->tx_buff.head;
680 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
681 si->tx_buff.truesize);
684 * Set the transmit interrupt enable. This will fire
685 * off an interrupt immediately. Note that we disable
686 * the receiver so we won't get spurious characteres
689 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
693 int mtt = irda_get_mtt(skb);
696 * We must not be transmitting...
700 netif_stop_queue(dev);
703 si->txbuf_dma = dma_map_single(si->dev, skb->data,
704 skb->len, DMA_TO_DEVICE);
706 sa1100_start_dma(si->txdma, si->txbuf_dma, skb->len);
709 * If we have a mean turn-around time, impose the specified
710 * specified delay. We could shorten this by timing from
711 * the point we received the packet.
716 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
719 dev->trans_start = jiffies;
725 sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
727 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
728 struct sa1100_irda *si = dev->priv;
729 int ret = -EOPNOTSUPP;
733 if (capable(CAP_NET_ADMIN)) {
735 * We are unable to set the speed if the
736 * device is not running.
739 ret = sa1100_irda_set_speed(si,
742 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
750 if (capable(CAP_NET_ADMIN)) {
751 irda_device_set_media_busy(dev, TRUE);
757 rq->ifr_receiving = IS_FIR(si) ? 0
758 : si->rx_buff.state != OUTSIDE_FRAME;
768 static struct net_device_stats *sa1100_irda_stats(struct net_device *dev)
770 struct sa1100_irda *si = dev->priv;
774 static int sa1100_irda_start(struct net_device *dev)
776 struct sa1100_irda *si = dev->priv;
781 err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
785 err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
786 NULL, NULL, &si->rxdma);
790 err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
791 sa1100_irda_txdma_irq, dev, &si->txdma);
796 * The interrupt must remain disabled for now.
798 disable_irq(dev->irq);
801 * Setup the serial port for the specified speed.
803 err = sa1100_irda_startup(si);
808 * Open a new IrLAP layer instance.
810 si->irlap = irlap_open(dev, &si->qos, "sa1100");
816 * Now enable the interrupt and start the queue
819 sa1100_set_power(si, power_level); /* low power mode */
820 enable_irq(dev->irq);
821 netif_start_queue(dev);
826 sa1100_irda_shutdown(si);
828 sa1100_free_dma(si->txdma);
830 sa1100_free_dma(si->rxdma);
832 free_irq(dev->irq, dev);
837 static int sa1100_irda_stop(struct net_device *dev)
839 struct sa1100_irda *si = dev->priv;
841 disable_irq(dev->irq);
842 sa1100_irda_shutdown(si);
845 * If we have been doing DMA receive, make sure we
846 * tidy that up cleanly.
849 dma_unmap_single(si->dev, si->rxbuf_dma, HPSIR_MAX_RXLEN,
851 dev_kfree_skb(si->rxskb);
857 irlap_close(si->irlap);
861 netif_stop_queue(dev);
867 sa1100_free_dma(si->txdma);
868 sa1100_free_dma(si->rxdma);
869 free_irq(dev->irq, dev);
871 sa1100_set_power(si, 0);
876 static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
878 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
879 if (io->head != NULL) {
881 io->in_frame = FALSE;
882 io->state = OUTSIDE_FRAME;
885 return io->head ? 0 : -ENOMEM;
888 static int sa1100_irda_probe(struct platform_device *pdev)
890 struct net_device *dev;
891 struct sa1100_irda *si;
892 unsigned int baudrate_mask;
895 if (!pdev->dev.platform_data)
898 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
901 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
904 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
908 dev = alloc_irdadev(sizeof(struct sa1100_irda));
913 si->dev = &pdev->dev;
914 si->pdata = pdev->dev.platform_data;
917 * Initialise the HP-SIR buffers
919 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
922 err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
926 dev->hard_start_xmit = sa1100_irda_hard_xmit;
927 dev->open = sa1100_irda_start;
928 dev->stop = sa1100_irda_stop;
929 dev->do_ioctl = sa1100_irda_ioctl;
930 dev->get_stats = sa1100_irda_stats;
931 dev->irq = IRQ_Ser2ICP;
933 irda_init_max_qos_capabilies(&si->qos);
936 * We support original IRDA up to 115k2. (we don't currently
937 * support 4Mbps). Min Turn Time set to 1ms or greater.
939 baudrate_mask = IR_9600;
942 case 4000000: baudrate_mask |= IR_4000000 << 8;
943 case 115200: baudrate_mask |= IR_115200;
944 case 57600: baudrate_mask |= IR_57600;
945 case 38400: baudrate_mask |= IR_38400;
946 case 19200: baudrate_mask |= IR_19200;
949 si->qos.baud_rate.bits &= baudrate_mask;
950 si->qos.min_turn_time.bits = 7;
952 irda_qos_bits_to_value(&si->qos);
954 si->utcr4 = UTCR4_HPSIR;
956 si->utcr4 |= UTCR4_Z1_6us;
959 * Initially enable HP-SIR modulation, and ensure that the port
963 Ser2UTCR4 = si->utcr4;
964 Ser2HSCR0 = HSCR0_UART;
966 err = register_netdev(dev);
968 platform_set_drvdata(pdev, dev);
972 kfree(si->tx_buff.head);
973 kfree(si->rx_buff.head);
976 release_mem_region(__PREG(Ser2HSCR2), 0x04);
978 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
980 release_mem_region(__PREG(Ser2UTCR0), 0x24);
986 static int sa1100_irda_remove(struct platform_device *pdev)
988 struct net_device *dev = platform_get_drvdata(pdev);
991 struct sa1100_irda *si = dev->priv;
992 unregister_netdev(dev);
993 kfree(si->tx_buff.head);
994 kfree(si->rx_buff.head);
998 release_mem_region(__PREG(Ser2HSCR2), 0x04);
999 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
1000 release_mem_region(__PREG(Ser2UTCR0), 0x24);
1005 static struct platform_driver sa1100ir_driver = {
1006 .probe = sa1100_irda_probe,
1007 .remove = sa1100_irda_remove,
1008 .suspend = sa1100_irda_suspend,
1009 .resume = sa1100_irda_resume,
1011 .name = "sa11x0-ir",
1015 static int __init sa1100_irda_init(void)
1018 * Limit power level a sensible range.
1020 if (power_level < 1)
1022 if (power_level > 3)
1025 return platform_driver_register(&sa1100ir_driver);
1028 static void __exit sa1100_irda_exit(void)
1030 platform_driver_unregister(&sa1100ir_driver);
1033 module_init(sa1100_irda_init);
1034 module_exit(sa1100_irda_exit);
1035 module_param(power_level, int, 0);
1036 module_param(tx_lpm, int, 0);
1037 module_param(max_rate, int, 0);
1039 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1040 MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1041 MODULE_LICENSE("GPL");
1042 MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1043 MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1044 MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");