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;
63 struct net_device_stats stats;
65 struct irda_platform_data *pdata;
66 struct irlap_cb *irlap;
73 #define IS_FIR(si) ((si)->speed >= 4000000)
75 #define HPSIR_MAX_RXLEN 2047
78 * Allocate and map the receive buffer, unless it is already allocated.
80 static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
85 si->rxskb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
88 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
93 * Align any IP headers that may be contained
96 skb_reserve(si->rxskb, 1);
98 si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
105 * We want to get here as soon as possible, and get the receiver setup.
106 * We use the existing buffer.
108 static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
111 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
116 * First empty receive FIFO
118 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
121 * Enable the DMA, receiver and receive interrupt.
123 sa1100_clear_dma(si->rxdma);
124 sa1100_start_dma(si->rxdma, si->rxbuf_dma, HPSIR_MAX_RXLEN);
125 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
129 * Set the IrDA communications speed.
131 static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
134 int brd, ret = -EINVAL;
137 case 9600: case 19200: case 38400:
138 case 57600: case 115200:
139 brd = 3686400 / (16 * speed) - 1;
142 * Stop the receive DMA.
145 sa1100_stop_dma(si->rxdma);
147 local_irq_save(flags);
150 Ser2HSCR0 = HSCR0_UART;
152 Ser2UTCR1 = brd >> 8;
156 * Clear status register
158 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
159 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
161 if (si->pdata->set_speed)
162 si->pdata->set_speed(si->dev, speed);
166 local_irq_restore(flags);
171 local_irq_save(flags);
176 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
181 if (si->pdata->set_speed)
182 si->pdata->set_speed(si->dev, speed);
184 sa1100_irda_rx_alloc(si);
185 sa1100_irda_rx_dma_start(si);
187 local_irq_restore(flags);
199 * Control the power state of the IrDA transmitter.
202 * 1 - short range, lowest power
203 * 2 - medium range, medium power
204 * 3 - maximum range, high power
206 * Currently, only assabet is known to support this.
209 __sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
212 if (si->pdata->set_power)
213 ret = si->pdata->set_power(si->dev, state);
218 sa1100_set_power(struct sa1100_irda *si, unsigned int state)
222 ret = __sa1100_irda_set_power(si, state);
229 static int sa1100_irda_startup(struct sa1100_irda *si)
234 * Ensure that the ports for this device are setup correctly.
236 if (si->pdata->startup)
237 si->pdata->startup(si->dev);
240 * Configure PPC for IRDA - we want to drive TXD2 low.
241 * We also want to drive this pin low during sleep.
248 * Enable HP-SIR modulation, and ensure that the port is disabled.
251 Ser2HSCR0 = HSCR0_UART;
252 Ser2UTCR4 = si->utcr4;
253 Ser2UTCR0 = UTCR0_8BitData;
254 Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
257 * Clear status register
259 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
261 ret = sa1100_irda_set_speed(si, si->speed = 9600);
266 if (si->pdata->shutdown)
267 si->pdata->shutdown(si->dev);
273 static void sa1100_irda_shutdown(struct sa1100_irda *si)
276 * Stop all DMA activity.
278 sa1100_stop_dma(si->rxdma);
279 sa1100_stop_dma(si->txdma);
281 /* Disable the port. */
285 if (si->pdata->shutdown)
286 si->pdata->shutdown(si->dev);
291 * Suspend the IrDA interface.
293 static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
295 struct net_device *dev = platform_get_drvdata(pdev);
296 struct sa1100_irda *si;
301 si = netdev_priv(dev);
304 * Stop the transmit queue
306 netif_device_detach(dev);
307 disable_irq(dev->irq);
308 sa1100_irda_shutdown(si);
309 __sa1100_irda_set_power(si, 0);
316 * Resume the IrDA interface.
318 static int sa1100_irda_resume(struct platform_device *pdev)
320 struct net_device *dev = platform_get_drvdata(pdev);
321 struct sa1100_irda *si;
326 si = netdev_priv(dev);
329 * If we missed a speed change, initialise at the new speed
330 * directly. It is debatable whether this is actually
331 * required, but in the interests of continuing from where
332 * we left off it is desireable. The converse argument is
333 * that we should re-negotiate at 9600 baud again.
336 si->speed = si->newspeed;
340 sa1100_irda_startup(si);
341 __sa1100_irda_set_power(si, si->power);
342 enable_irq(dev->irq);
345 * This automatically wakes up the queue
347 netif_device_attach(dev);
353 #define sa1100_irda_suspend NULL
354 #define sa1100_irda_resume NULL
358 * HP-SIR format interrupt service routines.
360 static void sa1100_irda_hpsir_irq(struct net_device *dev)
362 struct sa1100_irda *si = netdev_priv(dev);
368 * Deal with any receive errors first. The bytes in error may be
369 * the only bytes in the receive FIFO, so we do this first.
371 while (status & UTSR0_EIF) {
377 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
378 si->stats.rx_errors++;
379 if (stat & UTSR1_FRE)
380 si->stats.rx_frame_errors++;
381 if (stat & UTSR1_ROR)
382 si->stats.rx_fifo_errors++;
384 async_unwrap_char(dev, &si->stats, &si->rx_buff, data);
390 * We must clear certain bits.
392 Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
394 if (status & UTSR0_RFS) {
396 * There are at least 4 bytes in the FIFO. Read 3 bytes
397 * and leave the rest to the block below.
399 async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);
400 async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);
401 async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);
404 if (status & (UTSR0_RFS | UTSR0_RID)) {
406 * Fifo contains more than 1 character.
409 async_unwrap_char(dev, &si->stats, &si->rx_buff,
411 } while (Ser2UTSR1 & UTSR1_RNE);
415 if (status & UTSR0_TFS && si->tx_buff.len) {
417 * Transmitter FIFO is not full
420 Ser2UTDR = *si->tx_buff.data++;
421 si->tx_buff.len -= 1;
422 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
424 if (si->tx_buff.len == 0) {
425 si->stats.tx_packets++;
426 si->stats.tx_bytes += si->tx_buff.data -
430 * We need to ensure that the transmitter has
435 while (Ser2UTSR1 & UTSR1_TBY);
438 * Ok, we've finished transmitting. Now enable
439 * the receiver. Sometimes we get a receive IRQ
440 * immediately after a transmit...
442 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
443 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
446 sa1100_irda_set_speed(si, si->newspeed);
451 netif_wake_queue(dev);
456 static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
458 struct sk_buff *skb = si->rxskb;
460 unsigned int len, stat, data;
463 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
468 * Get the current data position.
470 dma_addr = sa1100_get_dma_pos(si->rxdma);
471 len = dma_addr - si->rxbuf_dma;
472 if (len > HPSIR_MAX_RXLEN)
473 len = HPSIR_MAX_RXLEN;
474 dma_unmap_single(si->dev, si->rxbuf_dma, len, DMA_FROM_DEVICE);
478 * Read Status, and then Data.
484 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
485 si->stats.rx_errors++;
486 if (stat & HSSR1_CRE)
487 si->stats.rx_crc_errors++;
488 if (stat & HSSR1_ROR)
489 si->stats.rx_frame_errors++;
491 skb->data[len++] = data;
494 * If we hit the end of frame, there's
495 * no point in continuing.
497 if (stat & HSSR1_EOF)
499 } while (Ser2HSSR0 & HSSR0_EIF);
501 if (stat & HSSR1_EOF) {
506 skb_reset_mac_header(skb);
507 skb->protocol = htons(ETH_P_IRDA);
508 si->stats.rx_packets++;
509 si->stats.rx_bytes += len;
512 * Before we pass the buffer up, allocate a new one.
514 sa1100_irda_rx_alloc(si);
521 si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
528 * FIR format interrupt service routine. We only have to
529 * handle RX events; transmit events go via the TX DMA handler.
531 * No matter what, we disable RX, process, and the restart RX.
533 static void sa1100_irda_fir_irq(struct net_device *dev)
535 struct sa1100_irda *si = netdev_priv(dev);
540 sa1100_stop_dma(si->rxdma);
543 * Framing error - we throw away the packet completely.
544 * Clearing RXE flushes the error conditions and data
547 if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
548 si->stats.rx_errors++;
550 if (Ser2HSSR0 & HSSR0_FRE)
551 si->stats.rx_frame_errors++;
554 * Clear out the DMA...
556 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
559 * Clear selected status bits now, so we
560 * don't miss them next time around.
562 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
566 * Deal with any receive errors. The any of the lowest
567 * 8 bytes in the FIFO may contain an error. We must read
568 * them one by one. The "error" could even be the end of
571 if (Ser2HSSR0 & HSSR0_EIF)
572 sa1100_irda_fir_error(si, dev);
575 * No matter what happens, we must restart reception.
577 sa1100_irda_rx_dma_start(si);
580 static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
582 struct net_device *dev = dev_id;
583 if (IS_FIR(((struct sa1100_irda *)netdev_priv(dev))))
584 sa1100_irda_fir_irq(dev);
586 sa1100_irda_hpsir_irq(dev);
591 * TX DMA completion handler.
593 static void sa1100_irda_txdma_irq(void *id)
595 struct net_device *dev = id;
596 struct sa1100_irda *si = netdev_priv(dev);
597 struct sk_buff *skb = si->txskb;
602 * Wait for the transmission to complete. Unfortunately,
603 * the hardware doesn't give us an interrupt to indicate
608 while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
611 * Clear the transmit underrun bit.
613 Ser2HSSR0 = HSSR0_TUR;
616 * Do we need to change speed? Note that we're lazy
617 * here - we don't free the old rxskb. We don't need
618 * to allocate a buffer either.
621 sa1100_irda_set_speed(si, si->newspeed);
626 * Start reception. This disables the transmitter for
627 * us. This will be using the existing RX buffer.
629 sa1100_irda_rx_dma_start(si);
632 * Account and free the packet.
635 dma_unmap_single(si->dev, si->txbuf_dma, skb->len, DMA_TO_DEVICE);
636 si->stats.tx_packets ++;
637 si->stats.tx_bytes += skb->len;
638 dev_kfree_skb_irq(skb);
642 * Make sure that the TX queue is available for sending
643 * (for retries). TX has priority over RX at all times.
645 netif_wake_queue(dev);
648 static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
650 struct sa1100_irda *si = netdev_priv(dev);
651 int speed = irda_get_next_speed(skb);
654 * Does this packet contain a request to change the interface
655 * speed? If so, remember it until we complete the transmission
658 if (speed != si->speed && speed != -1)
659 si->newspeed = speed;
662 * If this is an empty frame, we can bypass a lot.
667 sa1100_irda_set_speed(si, speed);
674 netif_stop_queue(dev);
676 si->tx_buff.data = si->tx_buff.head;
677 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
678 si->tx_buff.truesize);
681 * Set the transmit interrupt enable. This will fire
682 * off an interrupt immediately. Note that we disable
683 * the receiver so we won't get spurious characteres
686 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
690 int mtt = irda_get_mtt(skb);
693 * We must not be transmitting...
697 netif_stop_queue(dev);
700 si->txbuf_dma = dma_map_single(si->dev, skb->data,
701 skb->len, DMA_TO_DEVICE);
703 sa1100_start_dma(si->txdma, si->txbuf_dma, skb->len);
706 * If we have a mean turn-around time, impose the specified
707 * specified delay. We could shorten this by timing from
708 * the point we received the packet.
713 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
716 dev->trans_start = jiffies;
722 sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
724 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
725 struct sa1100_irda *si = netdev_priv(dev);
726 int ret = -EOPNOTSUPP;
730 if (capable(CAP_NET_ADMIN)) {
732 * We are unable to set the speed if the
733 * device is not running.
736 ret = sa1100_irda_set_speed(si,
739 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
747 if (capable(CAP_NET_ADMIN)) {
748 irda_device_set_media_busy(dev, TRUE);
754 rq->ifr_receiving = IS_FIR(si) ? 0
755 : si->rx_buff.state != OUTSIDE_FRAME;
765 static struct net_device_stats *sa1100_irda_stats(struct net_device *dev)
767 struct sa1100_irda *si = netdev_priv(dev);
771 static int sa1100_irda_start(struct net_device *dev)
773 struct sa1100_irda *si = netdev_priv(dev);
778 err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
782 err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
783 NULL, NULL, &si->rxdma);
787 err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
788 sa1100_irda_txdma_irq, dev, &si->txdma);
793 * The interrupt must remain disabled for now.
795 disable_irq(dev->irq);
798 * Setup the serial port for the specified speed.
800 err = sa1100_irda_startup(si);
805 * Open a new IrLAP layer instance.
807 si->irlap = irlap_open(dev, &si->qos, "sa1100");
813 * Now enable the interrupt and start the queue
816 sa1100_set_power(si, power_level); /* low power mode */
817 enable_irq(dev->irq);
818 netif_start_queue(dev);
823 sa1100_irda_shutdown(si);
825 sa1100_free_dma(si->txdma);
827 sa1100_free_dma(si->rxdma);
829 free_irq(dev->irq, dev);
834 static int sa1100_irda_stop(struct net_device *dev)
836 struct sa1100_irda *si = netdev_priv(dev);
838 disable_irq(dev->irq);
839 sa1100_irda_shutdown(si);
842 * If we have been doing DMA receive, make sure we
843 * tidy that up cleanly.
846 dma_unmap_single(si->dev, si->rxbuf_dma, HPSIR_MAX_RXLEN,
848 dev_kfree_skb(si->rxskb);
854 irlap_close(si->irlap);
858 netif_stop_queue(dev);
864 sa1100_free_dma(si->txdma);
865 sa1100_free_dma(si->rxdma);
866 free_irq(dev->irq, dev);
868 sa1100_set_power(si, 0);
873 static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
875 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
876 if (io->head != NULL) {
878 io->in_frame = FALSE;
879 io->state = OUTSIDE_FRAME;
882 return io->head ? 0 : -ENOMEM;
885 static int sa1100_irda_probe(struct platform_device *pdev)
887 struct net_device *dev;
888 struct sa1100_irda *si;
889 unsigned int baudrate_mask;
892 if (!pdev->dev.platform_data)
895 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
898 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
901 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
905 dev = alloc_irdadev(sizeof(struct sa1100_irda));
909 si = netdev_priv(dev);
910 si->dev = &pdev->dev;
911 si->pdata = pdev->dev.platform_data;
914 * Initialise the HP-SIR buffers
916 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
919 err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
923 dev->hard_start_xmit = sa1100_irda_hard_xmit;
924 dev->open = sa1100_irda_start;
925 dev->stop = sa1100_irda_stop;
926 dev->do_ioctl = sa1100_irda_ioctl;
927 dev->get_stats = sa1100_irda_stats;
928 dev->irq = IRQ_Ser2ICP;
930 irda_init_max_qos_capabilies(&si->qos);
933 * We support original IRDA up to 115k2. (we don't currently
934 * support 4Mbps). Min Turn Time set to 1ms or greater.
936 baudrate_mask = IR_9600;
939 case 4000000: baudrate_mask |= IR_4000000 << 8;
940 case 115200: baudrate_mask |= IR_115200;
941 case 57600: baudrate_mask |= IR_57600;
942 case 38400: baudrate_mask |= IR_38400;
943 case 19200: baudrate_mask |= IR_19200;
946 si->qos.baud_rate.bits &= baudrate_mask;
947 si->qos.min_turn_time.bits = 7;
949 irda_qos_bits_to_value(&si->qos);
951 si->utcr4 = UTCR4_HPSIR;
953 si->utcr4 |= UTCR4_Z1_6us;
956 * Initially enable HP-SIR modulation, and ensure that the port
960 Ser2UTCR4 = si->utcr4;
961 Ser2HSCR0 = HSCR0_UART;
963 err = register_netdev(dev);
965 platform_set_drvdata(pdev, dev);
969 kfree(si->tx_buff.head);
970 kfree(si->rx_buff.head);
973 release_mem_region(__PREG(Ser2HSCR2), 0x04);
975 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
977 release_mem_region(__PREG(Ser2UTCR0), 0x24);
983 static int sa1100_irda_remove(struct platform_device *pdev)
985 struct net_device *dev = platform_get_drvdata(pdev);
988 struct sa1100_irda *si = netdev_priv(dev);
989 unregister_netdev(dev);
990 kfree(si->tx_buff.head);
991 kfree(si->rx_buff.head);
995 release_mem_region(__PREG(Ser2HSCR2), 0x04);
996 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
997 release_mem_region(__PREG(Ser2UTCR0), 0x24);
1002 static struct platform_driver sa1100ir_driver = {
1003 .probe = sa1100_irda_probe,
1004 .remove = sa1100_irda_remove,
1005 .suspend = sa1100_irda_suspend,
1006 .resume = sa1100_irda_resume,
1008 .name = "sa11x0-ir",
1009 .owner = THIS_MODULE,
1013 static int __init sa1100_irda_init(void)
1016 * Limit power level a sensible range.
1018 if (power_level < 1)
1020 if (power_level > 3)
1023 return platform_driver_register(&sa1100ir_driver);
1026 static void __exit sa1100_irda_exit(void)
1028 platform_driver_unregister(&sa1100ir_driver);
1031 module_init(sa1100_irda_init);
1032 module_exit(sa1100_irda_exit);
1033 module_param(power_level, int, 0);
1034 module_param(tx_lpm, int, 0);
1035 module_param(max_rate, int, 0);
1037 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1038 MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1039 MODULE_LICENSE("GPL");
1040 MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1041 MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1042 MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
1043 MODULE_ALIAS("platform:sa11x0-ir");