sh_sir.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * SuperH IrDA Driver
  3. *
  4. * Copyright (C) 2009 Renesas Solutions Corp.
  5. * Kuninori Morimoto <morimoto.kuninori@renesas.com>
  6. *
  7. * Based on bfin_sir.c
  8. * Copyright 2006-2009 Analog Devices Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/io.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <net/irda/wrapper.h>
  20. #include <net/irda/irda_device.h>
  21. #include <asm/clock.h>
  22. #define DRIVER_NAME "sh_sir"
  23. #define RX_PHASE (1 << 0)
  24. #define TX_PHASE (1 << 1)
  25. #define TX_COMP_PHASE (1 << 2) /* tx complete */
  26. #define NONE_PHASE (1 << 31)
  27. #define IRIF_RINTCLR 0x0016 /* DMA rx interrupt source clear */
  28. #define IRIF_TINTCLR 0x0018 /* DMA tx interrupt source clear */
  29. #define IRIF_SIR0 0x0020 /* IrDA-SIR10 control */
  30. #define IRIF_SIR1 0x0022 /* IrDA-SIR10 baudrate error correction */
  31. #define IRIF_SIR2 0x0024 /* IrDA-SIR10 baudrate count */
  32. #define IRIF_SIR3 0x0026 /* IrDA-SIR10 status */
  33. #define IRIF_SIR_FRM 0x0028 /* Hardware frame processing set */
  34. #define IRIF_SIR_EOF 0x002A /* EOF value */
  35. #define IRIF_SIR_FLG 0x002C /* Flag clear */
  36. #define IRIF_UART_STS2 0x002E /* UART status 2 */
  37. #define IRIF_UART0 0x0030 /* UART control */
  38. #define IRIF_UART1 0x0032 /* UART status */
  39. #define IRIF_UART2 0x0034 /* UART mode */
  40. #define IRIF_UART3 0x0036 /* UART transmit data */
  41. #define IRIF_UART4 0x0038 /* UART receive data */
  42. #define IRIF_UART5 0x003A /* UART interrupt mask */
  43. #define IRIF_UART6 0x003C /* UART baud rate error correction */
  44. #define IRIF_UART7 0x003E /* UART baud rate count set */
  45. #define IRIF_CRC0 0x0040 /* CRC engine control */
  46. #define IRIF_CRC1 0x0042 /* CRC engine input data */
  47. #define IRIF_CRC2 0x0044 /* CRC engine calculation */
  48. #define IRIF_CRC3 0x0046 /* CRC engine output data 1 */
  49. #define IRIF_CRC4 0x0048 /* CRC engine output data 2 */
  50. /* IRIF_SIR0 */
  51. #define IRTPW (1 << 1) /* transmit pulse width select */
  52. #define IRERRC (1 << 0) /* Clear receive pulse width error */
  53. /* IRIF_SIR3 */
  54. #define IRERR (1 << 0) /* received pulse width Error */
  55. /* IRIF_SIR_FRM */
  56. #define EOFD (1 << 9) /* EOF detection flag */
  57. #define FRER (1 << 8) /* Frame Error bit */
  58. #define FRP (1 << 0) /* Frame processing set */
  59. /* IRIF_UART_STS2 */
  60. #define IRSME (1 << 6) /* Receive Sum Error flag */
  61. #define IROVE (1 << 5) /* Receive Overrun Error flag */
  62. #define IRFRE (1 << 4) /* Receive Framing Error flag */
  63. #define IRPRE (1 << 3) /* Receive Parity Error flag */
  64. /* IRIF_UART0_*/
  65. #define TBEC (1 << 2) /* Transmit Data Clear */
  66. #define RIE (1 << 1) /* Receive Enable */
  67. #define TIE (1 << 0) /* Transmit Enable */
  68. /* IRIF_UART1 */
  69. #define URSME (1 << 6) /* Receive Sum Error Flag */
  70. #define UROVE (1 << 5) /* Receive Overrun Error Flag */
  71. #define URFRE (1 << 4) /* Receive Framing Error Flag */
  72. #define URPRE (1 << 3) /* Receive Parity Error Flag */
  73. #define RBF (1 << 2) /* Receive Buffer Full Flag */
  74. #define TSBE (1 << 1) /* Transmit Shift Buffer Empty Flag */
  75. #define TBE (1 << 0) /* Transmit Buffer Empty flag */
  76. #define TBCOMP (TSBE | TBE)
  77. /* IRIF_UART5 */
  78. #define RSEIM (1 << 6) /* Receive Sum Error Flag IRQ Mask */
  79. #define RBFIM (1 << 2) /* Receive Buffer Full Flag IRQ Mask */
  80. #define TSBEIM (1 << 1) /* Transmit Shift Buffer Empty Flag IRQ Mask */
  81. #define TBEIM (1 << 0) /* Transmit Buffer Empty Flag IRQ Mask */
  82. #define RX_MASK (RSEIM | RBFIM)
  83. /* IRIF_CRC0 */
  84. #define CRC_RST (1 << 15) /* CRC Engine Reset */
  85. #define CRC_CT_MASK 0x0FFF
  86. /************************************************************************
  87. structure
  88. ************************************************************************/
  89. struct sh_sir_self {
  90. void __iomem *membase;
  91. unsigned int irq;
  92. struct clk *clk;
  93. struct net_device *ndev;
  94. struct irlap_cb *irlap;
  95. struct qos_info qos;
  96. iobuff_t tx_buff;
  97. iobuff_t rx_buff;
  98. };
  99. /************************************************************************
  100. common function
  101. ************************************************************************/
  102. static void sh_sir_write(struct sh_sir_self *self, u32 offset, u16 data)
  103. {
  104. iowrite16(data, self->membase + offset);
  105. }
  106. static u16 sh_sir_read(struct sh_sir_self *self, u32 offset)
  107. {
  108. return ioread16(self->membase + offset);
  109. }
  110. static void sh_sir_update_bits(struct sh_sir_self *self, u32 offset,
  111. u16 mask, u16 data)
  112. {
  113. u16 old, new;
  114. old = sh_sir_read(self, offset);
  115. new = (old & ~mask) | data;
  116. if (old != new)
  117. sh_sir_write(self, offset, new);
  118. }
  119. /************************************************************************
  120. CRC function
  121. ************************************************************************/
  122. static void sh_sir_crc_reset(struct sh_sir_self *self)
  123. {
  124. sh_sir_write(self, IRIF_CRC0, CRC_RST);
  125. }
  126. static void sh_sir_crc_add(struct sh_sir_self *self, u8 data)
  127. {
  128. sh_sir_write(self, IRIF_CRC1, (u16)data);
  129. }
  130. static u16 sh_sir_crc_cnt(struct sh_sir_self *self)
  131. {
  132. return CRC_CT_MASK & sh_sir_read(self, IRIF_CRC0);
  133. }
  134. static u16 sh_sir_crc_out(struct sh_sir_self *self)
  135. {
  136. return sh_sir_read(self, IRIF_CRC4);
  137. }
  138. static int sh_sir_crc_init(struct sh_sir_self *self)
  139. {
  140. struct device *dev = &self->ndev->dev;
  141. int ret = -EIO;
  142. u16 val;
  143. sh_sir_crc_reset(self);
  144. sh_sir_crc_add(self, 0xCC);
  145. sh_sir_crc_add(self, 0xF5);
  146. sh_sir_crc_add(self, 0xF1);
  147. sh_sir_crc_add(self, 0xA7);
  148. val = sh_sir_crc_cnt(self);
  149. if (4 != val) {
  150. dev_err(dev, "CRC count error %x\n", val);
  151. goto crc_init_out;
  152. }
  153. val = sh_sir_crc_out(self);
  154. if (0x51DF != val) {
  155. dev_err(dev, "CRC result error%x\n", val);
  156. goto crc_init_out;
  157. }
  158. ret = 0;
  159. crc_init_out:
  160. sh_sir_crc_reset(self);
  161. return ret;
  162. }
  163. /************************************************************************
  164. baud rate functions
  165. ************************************************************************/
  166. #define SCLK_BASE 1843200 /* 1.8432MHz */
  167. static u32 sh_sir_find_sclk(struct clk *irda_clk)
  168. {
  169. struct cpufreq_frequency_table *freq_table = irda_clk->freq_table;
  170. struct cpufreq_frequency_table *pos;
  171. struct clk *pclk = clk_get(NULL, "peripheral_clk");
  172. u32 limit, min = 0xffffffff, tmp;
  173. int index = 0;
  174. limit = clk_get_rate(pclk);
  175. clk_put(pclk);
  176. /* IrDA can not set over peripheral_clk */
  177. cpufreq_for_each_valid_entry(pos, freq_table) {
  178. u32 freq = pos->frequency;
  179. /* IrDA should not over peripheral_clk */
  180. if (freq > limit)
  181. continue;
  182. tmp = freq % SCLK_BASE;
  183. if (tmp < min) {
  184. min = tmp;
  185. index = pos - freq_table;
  186. }
  187. }
  188. return freq_table[index].frequency;
  189. }
  190. #define ERR_ROUNDING(a) ((a + 5000) / 10000)
  191. static int sh_sir_set_baudrate(struct sh_sir_self *self, u32 baudrate)
  192. {
  193. struct clk *clk;
  194. struct device *dev = &self->ndev->dev;
  195. u32 rate;
  196. u16 uabca, uabc;
  197. u16 irbca, irbc;
  198. u32 min, rerr, tmp;
  199. int i;
  200. /* Baud Rate Error Correction x 10000 */
  201. u32 rate_err_array[] = {
  202. 0, 625, 1250, 1875,
  203. 2500, 3125, 3750, 4375,
  204. 5000, 5625, 6250, 6875,
  205. 7500, 8125, 8750, 9375,
  206. };
  207. /*
  208. * FIXME
  209. *
  210. * it support 9600 only now
  211. */
  212. switch (baudrate) {
  213. case 9600:
  214. break;
  215. default:
  216. dev_err(dev, "un-supported baudrate %d\n", baudrate);
  217. return -EIO;
  218. }
  219. clk = clk_get(NULL, "irda_clk");
  220. if (IS_ERR(clk)) {
  221. dev_err(dev, "can not get irda_clk\n");
  222. return -EIO;
  223. }
  224. clk_set_rate(clk, sh_sir_find_sclk(clk));
  225. rate = clk_get_rate(clk);
  226. clk_put(clk);
  227. dev_dbg(dev, "selected sclk = %d\n", rate);
  228. /*
  229. * CALCULATION
  230. *
  231. * 1843200 = system rate / (irbca + (irbc + 1))
  232. */
  233. irbc = rate / SCLK_BASE;
  234. tmp = rate - (SCLK_BASE * irbc);
  235. tmp *= 10000;
  236. rerr = tmp / SCLK_BASE;
  237. min = 0xffffffff;
  238. irbca = 0;
  239. for (i = 0; i < ARRAY_SIZE(rate_err_array); i++) {
  240. tmp = abs(rate_err_array[i] - rerr);
  241. if (min > tmp) {
  242. min = tmp;
  243. irbca = i;
  244. }
  245. }
  246. tmp = rate / (irbc + ERR_ROUNDING(rate_err_array[irbca]));
  247. if ((SCLK_BASE / 100) < abs(tmp - SCLK_BASE))
  248. dev_warn(dev, "IrDA freq error margin over %d\n", tmp);
  249. dev_dbg(dev, "target = %d, result = %d, infrared = %d.%d\n",
  250. SCLK_BASE, tmp, irbc, rate_err_array[irbca]);
  251. irbca = (irbca & 0xF) << 4;
  252. irbc = (irbc - 1) & 0xF;
  253. if (!irbc) {
  254. dev_err(dev, "sh_sir can not set 0 in IRIF_SIR2\n");
  255. return -EIO;
  256. }
  257. sh_sir_write(self, IRIF_SIR0, IRTPW | IRERRC);
  258. sh_sir_write(self, IRIF_SIR1, irbca);
  259. sh_sir_write(self, IRIF_SIR2, irbc);
  260. /*
  261. * CALCULATION
  262. *
  263. * BaudRate[bps] = system rate / (uabca + (uabc + 1) x 16)
  264. */
  265. uabc = rate / baudrate;
  266. uabc = (uabc / 16) - 1;
  267. uabc = (uabc + 1) * 16;
  268. tmp = rate - (uabc * baudrate);
  269. tmp *= 10000;
  270. rerr = tmp / baudrate;
  271. min = 0xffffffff;
  272. uabca = 0;
  273. for (i = 0; i < ARRAY_SIZE(rate_err_array); i++) {
  274. tmp = abs(rate_err_array[i] - rerr);
  275. if (min > tmp) {
  276. min = tmp;
  277. uabca = i;
  278. }
  279. }
  280. tmp = rate / (uabc + ERR_ROUNDING(rate_err_array[uabca]));
  281. if ((baudrate / 100) < abs(tmp - baudrate))
  282. dev_warn(dev, "UART freq error margin over %d\n", tmp);
  283. dev_dbg(dev, "target = %d, result = %d, uart = %d.%d\n",
  284. baudrate, tmp,
  285. uabc, rate_err_array[uabca]);
  286. uabca = (uabca & 0xF) << 4;
  287. uabc = (uabc / 16) - 1;
  288. sh_sir_write(self, IRIF_UART6, uabca);
  289. sh_sir_write(self, IRIF_UART7, uabc);
  290. return 0;
  291. }
  292. /************************************************************************
  293. iobuf function
  294. ************************************************************************/
  295. static int __sh_sir_init_iobuf(iobuff_t *io, int size)
  296. {
  297. io->head = kmalloc(size, GFP_KERNEL);
  298. if (!io->head)
  299. return -ENOMEM;
  300. io->truesize = size;
  301. io->in_frame = FALSE;
  302. io->state = OUTSIDE_FRAME;
  303. io->data = io->head;
  304. return 0;
  305. }
  306. static void sh_sir_remove_iobuf(struct sh_sir_self *self)
  307. {
  308. kfree(self->rx_buff.head);
  309. kfree(self->tx_buff.head);
  310. self->rx_buff.head = NULL;
  311. self->tx_buff.head = NULL;
  312. }
  313. static int sh_sir_init_iobuf(struct sh_sir_self *self, int rxsize, int txsize)
  314. {
  315. int err = -ENOMEM;
  316. if (self->rx_buff.head ||
  317. self->tx_buff.head) {
  318. dev_err(&self->ndev->dev, "iobuff has already existed.");
  319. return err;
  320. }
  321. err = __sh_sir_init_iobuf(&self->rx_buff, rxsize);
  322. if (err)
  323. goto iobuf_err;
  324. err = __sh_sir_init_iobuf(&self->tx_buff, txsize);
  325. iobuf_err:
  326. if (err)
  327. sh_sir_remove_iobuf(self);
  328. return err;
  329. }
  330. /************************************************************************
  331. status function
  332. ************************************************************************/
  333. static void sh_sir_clear_all_err(struct sh_sir_self *self)
  334. {
  335. /* Clear error flag for receive pulse width */
  336. sh_sir_update_bits(self, IRIF_SIR0, IRERRC, IRERRC);
  337. /* Clear frame / EOF error flag */
  338. sh_sir_write(self, IRIF_SIR_FLG, 0xffff);
  339. /* Clear all status error */
  340. sh_sir_write(self, IRIF_UART_STS2, 0);
  341. }
  342. static void sh_sir_set_phase(struct sh_sir_self *self, int phase)
  343. {
  344. u16 uart5 = 0;
  345. u16 uart0 = 0;
  346. switch (phase) {
  347. case TX_PHASE:
  348. uart5 = TBEIM;
  349. uart0 = TBEC | TIE;
  350. break;
  351. case TX_COMP_PHASE:
  352. uart5 = TSBEIM;
  353. uart0 = TIE;
  354. break;
  355. case RX_PHASE:
  356. uart5 = RX_MASK;
  357. uart0 = RIE;
  358. break;
  359. default:
  360. break;
  361. }
  362. sh_sir_write(self, IRIF_UART5, uart5);
  363. sh_sir_write(self, IRIF_UART0, uart0);
  364. }
  365. static int sh_sir_is_which_phase(struct sh_sir_self *self)
  366. {
  367. u16 val = sh_sir_read(self, IRIF_UART5);
  368. if (val & TBEIM)
  369. return TX_PHASE;
  370. if (val & TSBEIM)
  371. return TX_COMP_PHASE;
  372. if (val & RX_MASK)
  373. return RX_PHASE;
  374. return NONE_PHASE;
  375. }
  376. static void sh_sir_tx(struct sh_sir_self *self, int phase)
  377. {
  378. switch (phase) {
  379. case TX_PHASE:
  380. if (0 >= self->tx_buff.len) {
  381. sh_sir_set_phase(self, TX_COMP_PHASE);
  382. } else {
  383. sh_sir_write(self, IRIF_UART3, self->tx_buff.data[0]);
  384. self->tx_buff.len--;
  385. self->tx_buff.data++;
  386. }
  387. break;
  388. case TX_COMP_PHASE:
  389. sh_sir_set_phase(self, RX_PHASE);
  390. netif_wake_queue(self->ndev);
  391. break;
  392. default:
  393. dev_err(&self->ndev->dev, "should not happen\n");
  394. break;
  395. }
  396. }
  397. static int sh_sir_read_data(struct sh_sir_self *self)
  398. {
  399. u16 val = 0;
  400. int timeout = 1024;
  401. while (timeout--) {
  402. val = sh_sir_read(self, IRIF_UART1);
  403. /* data get */
  404. if (val & RBF) {
  405. if (val & (URSME | UROVE | URFRE | URPRE))
  406. break;
  407. return (int)sh_sir_read(self, IRIF_UART4);
  408. }
  409. udelay(1);
  410. }
  411. dev_err(&self->ndev->dev, "UART1 %04x : STATUS %04x\n",
  412. val, sh_sir_read(self, IRIF_UART_STS2));
  413. /* read data register for clear error */
  414. sh_sir_read(self, IRIF_UART4);
  415. return -1;
  416. }
  417. static void sh_sir_rx(struct sh_sir_self *self)
  418. {
  419. int timeout = 1024;
  420. int data;
  421. while (timeout--) {
  422. data = sh_sir_read_data(self);
  423. if (data < 0)
  424. break;
  425. async_unwrap_char(self->ndev, &self->ndev->stats,
  426. &self->rx_buff, (u8)data);
  427. self->ndev->last_rx = jiffies;
  428. if (EOFD & sh_sir_read(self, IRIF_SIR_FRM))
  429. continue;
  430. break;
  431. }
  432. }
  433. static irqreturn_t sh_sir_irq(int irq, void *dev_id)
  434. {
  435. struct sh_sir_self *self = dev_id;
  436. struct device *dev = &self->ndev->dev;
  437. int phase = sh_sir_is_which_phase(self);
  438. switch (phase) {
  439. case TX_COMP_PHASE:
  440. case TX_PHASE:
  441. sh_sir_tx(self, phase);
  442. break;
  443. case RX_PHASE:
  444. if (sh_sir_read(self, IRIF_SIR3))
  445. dev_err(dev, "rcv pulse width error occurred\n");
  446. sh_sir_rx(self);
  447. sh_sir_clear_all_err(self);
  448. break;
  449. default:
  450. dev_err(dev, "unknown interrupt\n");
  451. }
  452. return IRQ_HANDLED;
  453. }
  454. /************************************************************************
  455. net_device_ops function
  456. ************************************************************************/
  457. static int sh_sir_hard_xmit(struct sk_buff *skb, struct net_device *ndev)
  458. {
  459. struct sh_sir_self *self = netdev_priv(ndev);
  460. int speed = irda_get_next_speed(skb);
  461. if ((0 < speed) &&
  462. (9600 != speed)) {
  463. dev_err(&ndev->dev, "support 9600 only (%d)\n", speed);
  464. return -EIO;
  465. }
  466. netif_stop_queue(ndev);
  467. self->tx_buff.data = self->tx_buff.head;
  468. self->tx_buff.len = 0;
  469. if (skb->len)
  470. self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
  471. self->tx_buff.truesize);
  472. sh_sir_set_phase(self, TX_PHASE);
  473. dev_kfree_skb(skb);
  474. return 0;
  475. }
  476. static int sh_sir_ioctl(struct net_device *ndev, struct ifreq *ifreq, int cmd)
  477. {
  478. /*
  479. * FIXME
  480. *
  481. * This function is needed for irda framework.
  482. * But nothing to do now
  483. */
  484. return 0;
  485. }
  486. static struct net_device_stats *sh_sir_stats(struct net_device *ndev)
  487. {
  488. struct sh_sir_self *self = netdev_priv(ndev);
  489. return &self->ndev->stats;
  490. }
  491. static int sh_sir_open(struct net_device *ndev)
  492. {
  493. struct sh_sir_self *self = netdev_priv(ndev);
  494. int err;
  495. clk_enable(self->clk);
  496. err = sh_sir_crc_init(self);
  497. if (err)
  498. goto open_err;
  499. sh_sir_set_baudrate(self, 9600);
  500. self->irlap = irlap_open(ndev, &self->qos, DRIVER_NAME);
  501. if (!self->irlap) {
  502. err = -ENODEV;
  503. goto open_err;
  504. }
  505. /*
  506. * Now enable the interrupt then start the queue
  507. */
  508. sh_sir_update_bits(self, IRIF_SIR_FRM, FRP, FRP);
  509. sh_sir_read(self, IRIF_UART1); /* flag clear */
  510. sh_sir_read(self, IRIF_UART4); /* flag clear */
  511. sh_sir_set_phase(self, RX_PHASE);
  512. netif_start_queue(ndev);
  513. dev_info(&self->ndev->dev, "opened\n");
  514. return 0;
  515. open_err:
  516. clk_disable(self->clk);
  517. return err;
  518. }
  519. static int sh_sir_stop(struct net_device *ndev)
  520. {
  521. struct sh_sir_self *self = netdev_priv(ndev);
  522. /* Stop IrLAP */
  523. if (self->irlap) {
  524. irlap_close(self->irlap);
  525. self->irlap = NULL;
  526. }
  527. netif_stop_queue(ndev);
  528. dev_info(&ndev->dev, "stopped\n");
  529. return 0;
  530. }
  531. static const struct net_device_ops sh_sir_ndo = {
  532. .ndo_open = sh_sir_open,
  533. .ndo_stop = sh_sir_stop,
  534. .ndo_start_xmit = sh_sir_hard_xmit,
  535. .ndo_do_ioctl = sh_sir_ioctl,
  536. .ndo_get_stats = sh_sir_stats,
  537. };
  538. /************************************************************************
  539. platform_driver function
  540. ************************************************************************/
  541. static int sh_sir_probe(struct platform_device *pdev)
  542. {
  543. struct net_device *ndev;
  544. struct sh_sir_self *self;
  545. struct resource *res;
  546. char clk_name[8];
  547. int irq;
  548. int err = -ENOMEM;
  549. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  550. irq = platform_get_irq(pdev, 0);
  551. if (!res || irq < 0) {
  552. dev_err(&pdev->dev, "Not enough platform resources.\n");
  553. goto exit;
  554. }
  555. ndev = alloc_irdadev(sizeof(*self));
  556. if (!ndev)
  557. goto exit;
  558. self = netdev_priv(ndev);
  559. self->membase = ioremap_nocache(res->start, resource_size(res));
  560. if (!self->membase) {
  561. err = -ENXIO;
  562. dev_err(&pdev->dev, "Unable to ioremap.\n");
  563. goto err_mem_1;
  564. }
  565. err = sh_sir_init_iobuf(self, IRDA_SKB_MAX_MTU, IRDA_SIR_MAX_FRAME);
  566. if (err)
  567. goto err_mem_2;
  568. snprintf(clk_name, sizeof(clk_name), "irda%d", pdev->id);
  569. self->clk = clk_get(&pdev->dev, clk_name);
  570. if (IS_ERR(self->clk)) {
  571. dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
  572. err = -ENODEV;
  573. goto err_mem_3;
  574. }
  575. irda_init_max_qos_capabilies(&self->qos);
  576. ndev->netdev_ops = &sh_sir_ndo;
  577. ndev->irq = irq;
  578. self->ndev = ndev;
  579. self->qos.baud_rate.bits &= IR_9600; /* FIXME */
  580. self->qos.min_turn_time.bits = 1; /* 10 ms or more */
  581. irda_qos_bits_to_value(&self->qos);
  582. err = register_netdev(ndev);
  583. if (err)
  584. goto err_mem_4;
  585. platform_set_drvdata(pdev, ndev);
  586. err = devm_request_irq(&pdev->dev, irq, sh_sir_irq, 0, "sh_sir", self);
  587. if (err) {
  588. dev_warn(&pdev->dev, "Unable to attach sh_sir interrupt\n");
  589. goto err_mem_4;
  590. }
  591. dev_info(&pdev->dev, "SuperH IrDA probed\n");
  592. goto exit;
  593. err_mem_4:
  594. clk_put(self->clk);
  595. err_mem_3:
  596. sh_sir_remove_iobuf(self);
  597. err_mem_2:
  598. iounmap(self->membase);
  599. err_mem_1:
  600. free_netdev(ndev);
  601. exit:
  602. return err;
  603. }
  604. static int sh_sir_remove(struct platform_device *pdev)
  605. {
  606. struct net_device *ndev = platform_get_drvdata(pdev);
  607. struct sh_sir_self *self = netdev_priv(ndev);
  608. if (!self)
  609. return 0;
  610. unregister_netdev(ndev);
  611. clk_put(self->clk);
  612. sh_sir_remove_iobuf(self);
  613. iounmap(self->membase);
  614. free_netdev(ndev);
  615. return 0;
  616. }
  617. static struct platform_driver sh_sir_driver = {
  618. .probe = sh_sir_probe,
  619. .remove = sh_sir_remove,
  620. .driver = {
  621. .name = DRIVER_NAME,
  622. },
  623. };
  624. module_platform_driver(sh_sir_driver);
  625. MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
  626. MODULE_DESCRIPTION("SuperH IrDA driver");
  627. MODULE_LICENSE("GPL");