17 #include <linux/module.h>
19 #include <linux/slab.h>
22 #include <asm/clock.h>
24 #define DRIVER_NAME "sh_sir"
26 #define RX_PHASE (1 << 0)
27 #define TX_PHASE (1 << 1)
28 #define TX_COMP_PHASE (1 << 2)
29 #define NONE_PHASE (1 << 31)
31 #define IRIF_RINTCLR 0x0016
32 #define IRIF_TINTCLR 0x0018
33 #define IRIF_SIR0 0x0020
34 #define IRIF_SIR1 0x0022
35 #define IRIF_SIR2 0x0024
36 #define IRIF_SIR3 0x0026
37 #define IRIF_SIR_FRM 0x0028
38 #define IRIF_SIR_EOF 0x002A
39 #define IRIF_SIR_FLG 0x002C
40 #define IRIF_UART_STS2 0x002E
41 #define IRIF_UART0 0x0030
42 #define IRIF_UART1 0x0032
43 #define IRIF_UART2 0x0034
44 #define IRIF_UART3 0x0036
45 #define IRIF_UART4 0x0038
46 #define IRIF_UART5 0x003A
47 #define IRIF_UART6 0x003C
48 #define IRIF_UART7 0x003E
49 #define IRIF_CRC0 0x0040
50 #define IRIF_CRC1 0x0042
51 #define IRIF_CRC2 0x0044
52 #define IRIF_CRC3 0x0046
53 #define IRIF_CRC4 0x0048
56 #define IRTPW (1 << 1)
57 #define IRERRC (1 << 0)
60 #define IRERR (1 << 0)
68 #define IRSME (1 << 6)
69 #define IROVE (1 << 5)
70 #define IRFRE (1 << 4)
71 #define IRPRE (1 << 3)
79 #define URSME (1 << 6)
80 #define UROVE (1 << 5)
81 #define URFRE (1 << 4)
82 #define URPRE (1 << 3)
86 #define TBCOMP (TSBE | TBE)
89 #define RSEIM (1 << 6)
90 #define RBFIM (1 << 2)
91 #define TSBEIM (1 << 1)
92 #define TBEIM (1 << 0)
93 #define RX_MASK (RSEIM | RBFIM)
96 #define CRC_RST (1 << 15)
97 #define CRC_CT_MASK 0x0FFF
134 return ioread16(self->membase + offset);
142 old = sh_sir_read(
self, offset);
143 new = (old & ~mask) | data;
145 sh_sir_write(
self, offset,
new);
155 static void sh_sir_crc_reset(
struct sh_sir_self *
self)
160 static void sh_sir_crc_add(
struct sh_sir_self *
self,
u8 data)
175 static int sh_sir_crc_init(
struct sh_sir_self *
self)
181 sh_sir_crc_reset(
self);
183 sh_sir_crc_add(
self, 0xCC);
184 sh_sir_crc_add(
self, 0xF5);
185 sh_sir_crc_add(
self, 0xF1);
186 sh_sir_crc_add(
self, 0xA7);
188 val = sh_sir_crc_cnt(
self);
190 dev_err(dev,
"CRC count error %x\n", val);
194 val = sh_sir_crc_out(
self);
196 dev_err(dev,
"CRC result error%x\n", val);
204 sh_sir_crc_reset(
self);
215 #define SCLK_BASE 1843200
217 static u32 sh_sir_find_sclk(
struct clk *irda_clk)
250 #define ERR_ROUNDING(a) ((a + 5000) / 10000)
254 struct device *dev = &
self->ndev->dev;
262 u32 rate_err_array[] = {
264 2500, 3125, 3750, 4375,
265 5000, 5625, 6250, 6875,
266 7500, 8125, 8750, 9375,
278 dev_err(dev,
"un-supported baudrate %d\n", baudrate);
284 dev_err(dev,
"can not get irda_clk\n");
292 dev_dbg(dev,
"selected sclk = %d\n", rate);
309 for (i = 0; i <
ARRAY_SIZE(rate_err_array); i++) {
310 tmp =
abs(rate_err_array[i] - rerr);
317 tmp = rate / (irbc +
ERR_ROUNDING(rate_err_array[irbca]));
319 dev_warn(dev,
"IrDA freq error margin over %d\n", tmp);
321 dev_dbg(dev,
"target = %d, result = %d, infrared = %d.%d\n",
322 SCLK_BASE, tmp, irbc, rate_err_array[irbca]);
324 irbca = (irbca & 0xF) << 4;
325 irbc = (irbc - 1) & 0xF;
328 dev_err(dev,
"sh_sir can not set 0 in IRIF_SIR2\n");
343 uabc = (uabc / 16) - 1;
344 uabc = (uabc + 1) * 16;
353 for (i = 0; i <
ARRAY_SIZE(rate_err_array); i++) {
354 tmp =
abs(rate_err_array[i] - rerr);
361 tmp = rate / (uabc +
ERR_ROUNDING(rate_err_array[uabca]));
362 if ((baudrate / 100) <
abs(tmp - baudrate))
363 dev_warn(dev,
"UART freq error margin over %d\n", tmp);
365 dev_dbg(dev,
"target = %d, result = %d, uart = %d.%d\n",
367 uabc, rate_err_array[uabca]);
369 uabca = (uabca & 0xF) << 4;
370 uabc = (uabc / 16) - 1;
399 static void sh_sir_remove_iobuf(
struct sh_sir_self *
self)
401 kfree(self->rx_buff.head);
402 kfree(self->tx_buff.head);
404 self->rx_buff.head =
NULL;
405 self->tx_buff.head =
NULL;
408 static int sh_sir_init_iobuf(
struct sh_sir_self *
self,
int rxsize,
int txsize)
412 if (self->rx_buff.head ||
413 self->tx_buff.head) {
414 dev_err(&self->ndev->dev,
"iobuff has already existed.");
418 err = __sh_sir_init_iobuf(&self->rx_buff, rxsize);
422 err = __sh_sir_init_iobuf(&self->tx_buff, txsize);
426 sh_sir_remove_iobuf(
self);
438 static void sh_sir_clear_all_err(
struct sh_sir_self *
self)
476 static int sh_sir_is_which_phase(
struct sh_sir_self *
self)
492 static void sh_sir_tx(
struct sh_sir_self *
self,
int phase)
496 if (0 >= self->tx_buff.len) {
499 sh_sir_write(
self,
IRIF_UART3, self->tx_buff.data[0]);
501 self->tx_buff.data++;
506 netif_wake_queue(self->ndev);
509 dev_err(&self->ndev->dev,
"should not happen\n");
514 static int sh_sir_read_data(
struct sh_sir_self *
self)
533 dev_err(&self->ndev->dev,
"UART1 %04x : STATUS %04x\n",
548 data = sh_sir_read_data(
self);
553 &self->rx_buff, (
u8)data);
566 struct device *dev = &
self->ndev->dev;
567 int phase = sh_sir_is_which_phase(
self);
572 sh_sir_tx(
self, phase);
576 dev_err(dev,
"rcv pulse width error occurred\n");
579 sh_sir_clear_all_err(
self);
582 dev_err(dev,
"unknown interrupt\n");
598 int speed = irda_get_next_speed(skb);
602 dev_err(&ndev->
dev,
"support 9600 only (%d)\n", speed);
606 netif_stop_queue(ndev);
608 self->tx_buff.data =
self->tx_buff.head;
609 self->tx_buff.len = 0;
612 self->tx_buff.truesize);
635 return &
self->ndev->stats;
638 static int sh_sir_open(
struct net_device *ndev)
644 err = sh_sir_crc_init(
self);
648 sh_sir_set_baudrate(
self, 9600);
664 netif_start_queue(ndev);
666 dev_info(&self->ndev->dev,
"opened\n");
676 static int sh_sir_stop(
struct net_device *ndev)
686 netif_stop_queue(ndev);
694 .ndo_open = sh_sir_open,
695 .ndo_stop = sh_sir_stop,
696 .ndo_start_xmit = sh_sir_hard_xmit,
697 .ndo_do_ioctl = sh_sir_ioctl,
698 .ndo_get_stats = sh_sir_stats,
719 if (!res || irq < 0) {
720 dev_err(&pdev->
dev,
"Not enough platform resources.\n");
728 self = netdev_priv(ndev);
730 if (!self->membase) {
740 snprintf(clk_name,
sizeof(clk_name),
"irda%d", pdev->
id);
742 if (IS_ERR(self->clk)) {
743 dev_err(&pdev->
dev,
"cannot get clock \"%s\"\n", clk_name);
754 self->qos.baud_rate.bits &=
IR_9600;
755 self->qos.min_turn_time.bits = 1;
763 platform_set_drvdata(pdev, ndev);
766 dev_warn(&pdev->
dev,
"Unable to attach sh_sir interrupt\n");
777 sh_sir_remove_iobuf(
self);
788 struct net_device *ndev = platform_get_drvdata(pdev);
796 sh_sir_remove_iobuf(
self);
799 platform_set_drvdata(pdev,
NULL);
805 .probe = sh_sir_probe,