4 * Copyright (C) 2010 Renesas Solutions Corp.
8 * Copyright (C) 2009 Renesas Solutions Corp.
9 * Copyright 2006-2009 Analog Devices Inc.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
19 * This driver is very simple.
20 * So, it doesn't have below support now
22 * - DMA transfer support
26 #include <linux/interrupt.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/clk.h>
31 #include <net/irda/wrapper.h>
32 #include <net/irda/irda_device.h>
34 #define DRIVER_NAME "sh_irda"
36 #define __IRDARAM_LEN 0x1039
38 #define IRTMR 0x1F00 /* Transfer mode */
39 #define IRCFR 0x1F02 /* Configuration */
40 #define IRCTR 0x1F04 /* IR control */
41 #define IRTFLR 0x1F20 /* Transmit frame length */
42 #define IRTCTR 0x1F22 /* Transmit control */
43 #define IRRFLR 0x1F40 /* Receive frame length */
44 #define IRRCTR 0x1F42 /* Receive control */
45 #define SIRISR 0x1F60 /* SIR-UART mode interrupt source */
46 #define SIRIMR 0x1F62 /* SIR-UART mode interrupt mask */
47 #define SIRICR 0x1F64 /* SIR-UART mode interrupt clear */
48 #define SIRBCR 0x1F68 /* SIR-UART mode baud rate count */
49 #define MFIRISR 0x1F70 /* MIR/FIR mode interrupt source */
50 #define MFIRIMR 0x1F72 /* MIR/FIR mode interrupt mask */
51 #define MFIRICR 0x1F74 /* MIR/FIR mode interrupt clear */
52 #define CRCCTR 0x1F80 /* CRC engine control */
53 #define CRCIR 0x1F86 /* CRC engine input data */
54 #define CRCCR 0x1F8A /* CRC engine calculation */
55 #define CRCOR 0x1F8E /* CRC engine output data */
56 #define FIFOCP 0x1FC0 /* FIFO current pointer */
57 #define FIFOFP 0x1FC2 /* FIFO follow pointer */
58 #define FIFORSMSK 0x1FC4 /* FIFO receive status mask */
59 #define FIFORSOR 0x1FC6 /* FIFO receive status OR */
60 #define FIFOSEL 0x1FC8 /* FIFO select */
61 #define FIFORS 0x1FCA /* FIFO receive status */
62 #define FIFORFL 0x1FCC /* FIFO receive frame length */
63 #define FIFORAMCP 0x1FCE /* FIFO RAM current pointer */
64 #define FIFORAMFP 0x1FD0 /* FIFO RAM follow pointer */
65 #define BIFCTL 0x1FD2 /* BUS interface control */
66 #define IRDARAM 0x0000 /* IrDA buffer RAM */
67 #define IRDARAM_LEN __IRDARAM_LEN /* - 8/16/32 (read-only for 32) */
70 #define TMD_MASK (0x3 << 14) /* Transfer Mode */
71 #define TMD_SIR (0x0 << 14)
72 #define TMD_MIR (0x3 << 14)
73 #define TMD_FIR (0x2 << 14)
75 #define FIFORIM (1 << 8) /* FIFO receive interrupt mask */
76 #define MIM (1 << 4) /* MIR/FIR Interrupt Mask */
77 #define SIM (1 << 0) /* SIR Interrupt Mask */
78 #define xIM_MASK (FIFORIM | MIM | SIM)
81 #define RTO_SHIFT 8 /* shift for Receive Timeout */
82 #define RTO (0x3 << RTO_SHIFT)
85 #define ARMOD (1 << 15) /* Auto-Receive Mode */
86 #define TE (1 << 0) /* Transmit Enable */
89 #define RFL_MASK (0x1FFF) /* mask for Receive Frame Length */
92 #define RE (1 << 0) /* Receive Enable */
95 * SIRISR, SIRIMR, SIRICR,
96 * MFIRISR, MFIRIMR, MFIRICR
98 #define FRE (1 << 15) /* Frame Receive End */
99 #define TROV (1 << 11) /* Transfer Area Overflow */
100 #define xIR_9 (1 << 9)
101 #define TOT xIR_9 /* for SIR Timeout */
102 #define ABTD xIR_9 /* for MIR/FIR Abort Detection */
103 #define xIR_8 (1 << 8)
104 #define FER xIR_8 /* for SIR Framing Error */
105 #define CRCER xIR_8 /* for MIR/FIR CRC error */
106 #define FTE (1 << 7) /* Frame Transmit End */
107 #define xIR_MASK (FRE | TROV | xIR_9 | xIR_8 | FTE)
110 #define BRC_MASK (0x3F) /* mask for Baud Rate Count */
113 #define CRC_RST (1 << 15) /* CRC Engine Reset */
114 #define CRC_CT_MASK 0x0FFF /* mask for CRC Engine Input Data Count */
117 #define CRC_IN_MASK 0x0FFF /* mask for CRC Engine Input Data */
119 /************************************************************************
125 ************************************************************************/
134 struct sh_irda_xir_func {
135 int (*xir_fre) (struct sh_irda_self *self);
136 int (*xir_trov) (struct sh_irda_self *self);
137 int (*xir_9) (struct sh_irda_self *self);
138 int (*xir_8) (struct sh_irda_self *self);
139 int (*xir_fte) (struct sh_irda_self *self);
142 struct sh_irda_self {
143 void __iomem *membase;
145 struct platform_device *pdev;
147 struct net_device *ndev;
149 struct irlap_cb *irlap;
155 enum sh_irda_mode mode;
158 struct sh_irda_xir_func *xir_func;
161 /************************************************************************
167 ************************************************************************/
168 static void sh_irda_write(struct sh_irda_self *self, u32 offset, u16 data)
172 spin_lock_irqsave(&self->lock, flags);
173 iowrite16(data, self->membase + offset);
174 spin_unlock_irqrestore(&self->lock, flags);
177 static u16 sh_irda_read(struct sh_irda_self *self, u32 offset)
182 spin_lock_irqsave(&self->lock, flags);
183 ret = ioread16(self->membase + offset);
184 spin_unlock_irqrestore(&self->lock, flags);
189 static void sh_irda_update_bits(struct sh_irda_self *self, u32 offset,
195 spin_lock_irqsave(&self->lock, flags);
196 old = ioread16(self->membase + offset);
197 new = (old & ~mask) | data;
199 iowrite16(data, self->membase + offset);
200 spin_unlock_irqrestore(&self->lock, flags);
203 /************************************************************************
209 ************************************************************************/
210 /*=====================================
214 *=====================================*/
215 static void sh_irda_rcv_ctrl(struct sh_irda_self *self, int enable)
217 struct device *dev = &self->ndev->dev;
219 sh_irda_update_bits(self, IRRCTR, RE, enable ? RE : 0);
220 dev_dbg(dev, "recv %s\n", enable ? "enable" : "disable");
223 static int sh_irda_set_timeout(struct sh_irda_self *self, int interval)
225 struct device *dev = &self->ndev->dev;
227 if (SH_IRDA_SIR != self->mode)
230 if (interval < 0 || interval > 2) {
231 dev_err(dev, "unsupported timeout interval\n");
235 sh_irda_update_bits(self, IRCFR, RTO, interval << RTO_SHIFT);
239 static int sh_irda_set_baudrate(struct sh_irda_self *self, int baudrate)
241 struct device *dev = &self->ndev->dev;
247 if (SH_IRDA_SIR != self->mode) {
248 dev_err(dev, "it is not SIR mode\n");
253 * Baud rate (bits/s) =
254 * (48 MHz / 26) / (baud rate counter value + 1) x 16
256 val = (48000000 / 26 / 16 / baudrate) - 1;
257 dev_dbg(dev, "baudrate = %d, val = 0x%02x\n", baudrate, val);
259 sh_irda_update_bits(self, SIRBCR, BRC_MASK, val);
264 static int sh_irda_get_rcv_length(struct sh_irda_self *self)
266 return RFL_MASK & sh_irda_read(self, IRRFLR);
269 /*=====================================
273 *=====================================*/
274 static int sh_irda_xir_fre(struct sh_irda_self *self)
276 struct device *dev = &self->ndev->dev;
277 dev_err(dev, "none mode: frame recv\n");
281 static int sh_irda_xir_trov(struct sh_irda_self *self)
283 struct device *dev = &self->ndev->dev;
284 dev_err(dev, "none mode: buffer ram over\n");
288 static int sh_irda_xir_9(struct sh_irda_self *self)
290 struct device *dev = &self->ndev->dev;
291 dev_err(dev, "none mode: time over\n");
295 static int sh_irda_xir_8(struct sh_irda_self *self)
297 struct device *dev = &self->ndev->dev;
298 dev_err(dev, "none mode: framing error\n");
302 static int sh_irda_xir_fte(struct sh_irda_self *self)
304 struct device *dev = &self->ndev->dev;
305 dev_err(dev, "none mode: frame transmit end\n");
309 static struct sh_irda_xir_func sh_irda_xir_func = {
310 .xir_fre = sh_irda_xir_fre,
311 .xir_trov = sh_irda_xir_trov,
312 .xir_9 = sh_irda_xir_9,
313 .xir_8 = sh_irda_xir_8,
314 .xir_fte = sh_irda_xir_fte,
317 /*=====================================
321 * MIR/FIR are not supported now
322 *=====================================*/
323 static struct sh_irda_xir_func sh_irda_mfir_func = {
324 .xir_fre = sh_irda_xir_fre,
325 .xir_trov = sh_irda_xir_trov,
326 .xir_9 = sh_irda_xir_9,
327 .xir_8 = sh_irda_xir_8,
328 .xir_fte = sh_irda_xir_fte,
331 /*=====================================
335 *=====================================*/
336 static int sh_irda_sir_fre(struct sh_irda_self *self)
338 struct device *dev = &self->ndev->dev;
340 u8 *data = (u8 *)&data16;
341 int len = sh_irda_get_rcv_length(self);
344 if (len > IRDARAM_LEN)
347 dev_dbg(dev, "frame recv length = %d\n", len);
349 for (i = 0; i < len; i++) {
352 data16 = sh_irda_read(self, IRDARAM + i);
354 async_unwrap_char(self->ndev, &self->ndev->stats,
355 &self->rx_buff, data[j]);
357 self->ndev->last_rx = jiffies;
359 sh_irda_rcv_ctrl(self, 1);
364 static int sh_irda_sir_trov(struct sh_irda_self *self)
366 struct device *dev = &self->ndev->dev;
368 dev_err(dev, "buffer ram over\n");
369 sh_irda_rcv_ctrl(self, 1);
373 static int sh_irda_sir_tot(struct sh_irda_self *self)
375 struct device *dev = &self->ndev->dev;
377 dev_err(dev, "time over\n");
378 sh_irda_set_baudrate(self, 9600);
379 sh_irda_rcv_ctrl(self, 1);
383 static int sh_irda_sir_fer(struct sh_irda_self *self)
385 struct device *dev = &self->ndev->dev;
387 dev_err(dev, "framing error\n");
388 sh_irda_rcv_ctrl(self, 1);
392 static int sh_irda_sir_fte(struct sh_irda_self *self)
394 struct device *dev = &self->ndev->dev;
396 dev_dbg(dev, "frame transmit end\n");
397 netif_wake_queue(self->ndev);
402 static struct sh_irda_xir_func sh_irda_sir_func = {
403 .xir_fre = sh_irda_sir_fre,
404 .xir_trov = sh_irda_sir_trov,
405 .xir_9 = sh_irda_sir_tot,
406 .xir_8 = sh_irda_sir_fer,
407 .xir_fte = sh_irda_sir_fte,
410 static void sh_irda_set_mode(struct sh_irda_self *self, enum sh_irda_mode mode)
412 struct device *dev = &self->ndev->dev;
413 struct sh_irda_xir_func *func;
421 func = &sh_irda_sir_func;
426 func = &sh_irda_mfir_func;
431 func = &sh_irda_mfir_func;
436 func = &sh_irda_xir_func;
441 self->xir_func = func;
442 sh_irda_update_bits(self, IRTMR, TMD_MASK, data);
444 dev_dbg(dev, "switch to %s mode", name);
447 /************************************************************************
453 ************************************************************************/
454 static void sh_irda_set_irq_mask(struct sh_irda_self *self)
460 sh_irda_update_bits(self, IRTMR, xIM_MASK, xIM_MASK);
461 sh_irda_update_bits(self, SIRIMR, xIR_MASK, xIR_MASK);
462 sh_irda_update_bits(self, MFIRIMR, xIR_MASK, xIR_MASK);
465 sh_irda_update_bits(self, SIRICR, xIR_MASK, xIR_MASK);
466 sh_irda_update_bits(self, MFIRICR, xIR_MASK, xIR_MASK);
468 switch (self->mode) {
486 sh_irda_update_bits(self, IRTMR, tmr_hole, 0);
487 sh_irda_update_bits(self, xir_reg, xIR_MASK, 0);
491 static irqreturn_t sh_irda_irq(int irq, void *dev_id)
493 struct sh_irda_self *self = dev_id;
494 struct sh_irda_xir_func *func = self->xir_func;
495 u16 isr = sh_irda_read(self, SIRISR);
498 sh_irda_write(self, SIRICR, isr);
503 func->xir_trov(self);
514 /************************************************************************
520 ************************************************************************/
521 static void sh_irda_crc_reset(struct sh_irda_self *self)
523 sh_irda_write(self, CRCCTR, CRC_RST);
526 static void sh_irda_crc_add(struct sh_irda_self *self, u16 data)
528 sh_irda_write(self, CRCIR, data & CRC_IN_MASK);
531 static u16 sh_irda_crc_cnt(struct sh_irda_self *self)
533 return CRC_CT_MASK & sh_irda_read(self, CRCCTR);
536 static u16 sh_irda_crc_out(struct sh_irda_self *self)
538 return sh_irda_read(self, CRCOR);
541 static int sh_irda_crc_init(struct sh_irda_self *self)
543 struct device *dev = &self->ndev->dev;
547 sh_irda_crc_reset(self);
549 sh_irda_crc_add(self, 0xCC);
550 sh_irda_crc_add(self, 0xF5);
551 sh_irda_crc_add(self, 0xF1);
552 sh_irda_crc_add(self, 0xA7);
554 val = sh_irda_crc_cnt(self);
556 dev_err(dev, "CRC count error %x\n", val);
560 val = sh_irda_crc_out(self);
562 dev_err(dev, "CRC result error%x\n", val);
570 sh_irda_crc_reset(self);
574 /************************************************************************
580 ************************************************************************/
581 static void sh_irda_remove_iobuf(struct sh_irda_self *self)
583 kfree(self->rx_buff.head);
585 self->tx_buff.head = NULL;
586 self->tx_buff.data = NULL;
587 self->rx_buff.head = NULL;
588 self->rx_buff.data = NULL;
591 static int sh_irda_init_iobuf(struct sh_irda_self *self, int rxsize, int txsize)
593 if (self->rx_buff.head ||
594 self->tx_buff.head) {
595 dev_err(&self->ndev->dev, "iobuff has already existed.");
600 self->rx_buff.head = kmalloc(rxsize, GFP_KERNEL);
601 if (!self->rx_buff.head)
604 self->rx_buff.truesize = rxsize;
605 self->rx_buff.in_frame = FALSE;
606 self->rx_buff.state = OUTSIDE_FRAME;
607 self->rx_buff.data = self->rx_buff.head;
610 self->tx_buff.head = self->membase + IRDARAM;
611 self->tx_buff.truesize = IRDARAM_LEN;
616 /************************************************************************
619 net_device_ops function
622 ************************************************************************/
623 static int sh_irda_hard_xmit(struct sk_buff *skb, struct net_device *ndev)
625 struct sh_irda_self *self = netdev_priv(ndev);
626 struct device *dev = &self->ndev->dev;
627 int speed = irda_get_next_speed(skb);
630 dev_dbg(dev, "hard xmit\n");
632 netif_stop_queue(ndev);
633 sh_irda_rcv_ctrl(self, 0);
635 ret = sh_irda_set_baudrate(self, speed);
637 goto sh_irda_hard_xmit_end;
639 self->tx_buff.len = 0;
643 spin_lock_irqsave(&self->lock, flags);
644 self->tx_buff.len = async_wrap_skb(skb,
646 self->tx_buff.truesize);
647 spin_unlock_irqrestore(&self->lock, flags);
649 if (self->tx_buff.len > self->tx_buff.truesize)
650 self->tx_buff.len = self->tx_buff.truesize;
652 sh_irda_write(self, IRTFLR, self->tx_buff.len);
653 sh_irda_write(self, IRTCTR, ARMOD | TE);
655 goto sh_irda_hard_xmit_end;
661 sh_irda_hard_xmit_end:
662 sh_irda_set_baudrate(self, 9600);
663 netif_wake_queue(self->ndev);
664 sh_irda_rcv_ctrl(self, 1);
671 static int sh_irda_ioctl(struct net_device *ndev, struct ifreq *ifreq, int cmd)
676 * This function is needed for irda framework.
677 * But nothing to do now
682 static struct net_device_stats *sh_irda_stats(struct net_device *ndev)
684 struct sh_irda_self *self = netdev_priv(ndev);
686 return &self->ndev->stats;
689 static int sh_irda_open(struct net_device *ndev)
691 struct sh_irda_self *self = netdev_priv(ndev);
694 pm_runtime_get_sync(&self->pdev->dev);
695 err = sh_irda_crc_init(self);
699 sh_irda_set_mode(self, SH_IRDA_SIR);
700 sh_irda_set_timeout(self, 2);
701 sh_irda_set_baudrate(self, 9600);
703 self->irlap = irlap_open(ndev, &self->qos, DRIVER_NAME);
709 netif_start_queue(ndev);
710 sh_irda_rcv_ctrl(self, 1);
711 sh_irda_set_irq_mask(self);
713 dev_info(&ndev->dev, "opened\n");
718 pm_runtime_put_sync(&self->pdev->dev);
723 static int sh_irda_stop(struct net_device *ndev)
725 struct sh_irda_self *self = netdev_priv(ndev);
729 irlap_close(self->irlap);
733 netif_stop_queue(ndev);
734 pm_runtime_put_sync(&self->pdev->dev);
736 dev_info(&ndev->dev, "stopped\n");
741 static const struct net_device_ops sh_irda_ndo = {
742 .ndo_open = sh_irda_open,
743 .ndo_stop = sh_irda_stop,
744 .ndo_start_xmit = sh_irda_hard_xmit,
745 .ndo_do_ioctl = sh_irda_ioctl,
746 .ndo_get_stats = sh_irda_stats,
749 /************************************************************************
752 platform_driver function
755 ************************************************************************/
756 static int sh_irda_probe(struct platform_device *pdev)
758 struct net_device *ndev;
759 struct sh_irda_self *self;
760 struct resource *res;
764 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
765 irq = platform_get_irq(pdev, 0);
766 if (!res || irq < 0) {
767 dev_err(&pdev->dev, "Not enough platform resources.\n");
771 ndev = alloc_irdadev(sizeof(*self));
775 self = netdev_priv(ndev);
776 self->membase = ioremap_nocache(res->start, resource_size(res));
777 if (!self->membase) {
779 dev_err(&pdev->dev, "Unable to ioremap.\n");
783 err = sh_irda_init_iobuf(self, IRDA_SKB_MAX_MTU, IRDA_SIR_MAX_FRAME);
788 pm_runtime_enable(&pdev->dev);
790 irda_init_max_qos_capabilies(&self->qos);
792 ndev->netdev_ops = &sh_irda_ndo;
796 self->qos.baud_rate.bits &= IR_9600; /* FIXME */
797 self->qos.min_turn_time.bits = 1; /* 10 ms or more */
798 spin_lock_init(&self->lock);
800 irda_qos_bits_to_value(&self->qos);
802 err = register_netdev(ndev);
806 platform_set_drvdata(pdev, ndev);
807 err = request_irq(irq, sh_irda_irq, IRQF_DISABLED, "sh_irda", self);
809 dev_warn(&pdev->dev, "Unable to attach sh_irda interrupt\n");
813 dev_info(&pdev->dev, "SuperH IrDA probed\n");
818 pm_runtime_disable(&pdev->dev);
819 sh_irda_remove_iobuf(self);
821 iounmap(self->membase);
828 static int sh_irda_remove(struct platform_device *pdev)
830 struct net_device *ndev = platform_get_drvdata(pdev);
831 struct sh_irda_self *self = netdev_priv(ndev);
836 unregister_netdev(ndev);
837 pm_runtime_disable(&pdev->dev);
838 sh_irda_remove_iobuf(self);
839 iounmap(self->membase);
845 static int sh_irda_runtime_nop(struct device *dev)
847 /* Runtime PM callback shared between ->runtime_suspend()
848 * and ->runtime_resume(). Simply returns success.
850 * This driver re-initializes all registers after
851 * pm_runtime_get_sync() anyway so there is no need
852 * to save and restore registers here.
857 static const struct dev_pm_ops sh_irda_pm_ops = {
858 .runtime_suspend = sh_irda_runtime_nop,
859 .runtime_resume = sh_irda_runtime_nop,
862 static struct platform_driver sh_irda_driver = {
863 .probe = sh_irda_probe,
864 .remove = sh_irda_remove,
867 .pm = &sh_irda_pm_ops,
871 module_platform_driver(sh_irda_driver);
874 MODULE_DESCRIPTION("SuperH IrDA driver");
875 MODULE_LICENSE("GPL");