pxaficp_ir.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*
  2. * linux/drivers/net/irda/pxaficp_ir.c
  3. *
  4. * Based on sa1100_ir.c by Russell King
  5. *
  6. * Changes copyright (C) 2003-2005 MontaVista Software, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Infra-red driver (SIR/FIR) for the PXA2xx embedded microprocessor
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/clk.h>
  19. #include <net/irda/irda.h>
  20. #include <net/irda/irmod.h>
  21. #include <net/irda/wrapper.h>
  22. #include <net/irda/irda_device.h>
  23. #include <asm/dma.h>
  24. #include <mach/irda.h>
  25. #include <mach/pxa-regs.h>
  26. #define IrSR_RXPL_NEG_IS_ZERO (1<<4)
  27. #define IrSR_RXPL_POS_IS_ZERO 0x0
  28. #define IrSR_TXPL_NEG_IS_ZERO (1<<3)
  29. #define IrSR_TXPL_POS_IS_ZERO 0x0
  30. #define IrSR_XMODE_PULSE_1_6 (1<<2)
  31. #define IrSR_XMODE_PULSE_3_16 0x0
  32. #define IrSR_RCVEIR_IR_MODE (1<<1)
  33. #define IrSR_RCVEIR_UART_MODE 0x0
  34. #define IrSR_XMITIR_IR_MODE (1<<0)
  35. #define IrSR_XMITIR_UART_MODE 0x0
  36. #define IrSR_IR_RECEIVE_ON (\
  37. IrSR_RXPL_NEG_IS_ZERO | \
  38. IrSR_TXPL_POS_IS_ZERO | \
  39. IrSR_XMODE_PULSE_3_16 | \
  40. IrSR_RCVEIR_IR_MODE | \
  41. IrSR_XMITIR_UART_MODE)
  42. #define IrSR_IR_TRANSMIT_ON (\
  43. IrSR_RXPL_NEG_IS_ZERO | \
  44. IrSR_TXPL_POS_IS_ZERO | \
  45. IrSR_XMODE_PULSE_3_16 | \
  46. IrSR_RCVEIR_UART_MODE | \
  47. IrSR_XMITIR_IR_MODE)
  48. struct pxa_irda {
  49. int speed;
  50. int newspeed;
  51. unsigned long last_oscr;
  52. unsigned char *dma_rx_buff;
  53. unsigned char *dma_tx_buff;
  54. dma_addr_t dma_rx_buff_phy;
  55. dma_addr_t dma_tx_buff_phy;
  56. unsigned int dma_tx_buff_len;
  57. int txdma;
  58. int rxdma;
  59. struct net_device_stats stats;
  60. struct irlap_cb *irlap;
  61. struct qos_info qos;
  62. iobuff_t tx_buff;
  63. iobuff_t rx_buff;
  64. struct device *dev;
  65. struct pxaficp_platform_data *pdata;
  66. struct clk *fir_clk;
  67. struct clk *sir_clk;
  68. struct clk *cur_clk;
  69. };
  70. static inline void pxa_irda_disable_clk(struct pxa_irda *si)
  71. {
  72. if (si->cur_clk)
  73. clk_disable(si->cur_clk);
  74. si->cur_clk = NULL;
  75. }
  76. static inline void pxa_irda_enable_firclk(struct pxa_irda *si)
  77. {
  78. si->cur_clk = si->fir_clk;
  79. clk_enable(si->fir_clk);
  80. }
  81. static inline void pxa_irda_enable_sirclk(struct pxa_irda *si)
  82. {
  83. si->cur_clk = si->sir_clk;
  84. clk_enable(si->sir_clk);
  85. }
  86. #define IS_FIR(si) ((si)->speed >= 4000000)
  87. #define IRDA_FRAME_SIZE_LIMIT 2047
  88. inline static void pxa_irda_fir_dma_rx_start(struct pxa_irda *si)
  89. {
  90. DCSR(si->rxdma) = DCSR_NODESC;
  91. DSADR(si->rxdma) = __PREG(ICDR);
  92. DTADR(si->rxdma) = si->dma_rx_buff_phy;
  93. DCMD(si->rxdma) = DCMD_INCTRGADDR | DCMD_FLOWSRC | DCMD_WIDTH1 | DCMD_BURST32 | IRDA_FRAME_SIZE_LIMIT;
  94. DCSR(si->rxdma) |= DCSR_RUN;
  95. }
  96. inline static void pxa_irda_fir_dma_tx_start(struct pxa_irda *si)
  97. {
  98. DCSR(si->txdma) = DCSR_NODESC;
  99. DSADR(si->txdma) = si->dma_tx_buff_phy;
  100. DTADR(si->txdma) = __PREG(ICDR);
  101. DCMD(si->txdma) = DCMD_INCSRCADDR | DCMD_FLOWTRG | DCMD_ENDIRQEN | DCMD_WIDTH1 | DCMD_BURST32 | si->dma_tx_buff_len;
  102. DCSR(si->txdma) |= DCSR_RUN;
  103. }
  104. /*
  105. * Set the IrDA communications speed.
  106. */
  107. static int pxa_irda_set_speed(struct pxa_irda *si, int speed)
  108. {
  109. unsigned long flags;
  110. unsigned int divisor;
  111. switch (speed) {
  112. case 9600: case 19200: case 38400:
  113. case 57600: case 115200:
  114. /* refer to PXA250/210 Developer's Manual 10-7 */
  115. /* BaudRate = 14.7456 MHz / (16*Divisor) */
  116. divisor = 14745600 / (16 * speed);
  117. local_irq_save(flags);
  118. if (IS_FIR(si)) {
  119. /* stop RX DMA */
  120. DCSR(si->rxdma) &= ~DCSR_RUN;
  121. /* disable FICP */
  122. ICCR0 = 0;
  123. pxa_irda_disable_clk(si);
  124. /* set board transceiver to SIR mode */
  125. si->pdata->transceiver_mode(si->dev, IR_SIRMODE);
  126. /* enable the STUART clock */
  127. pxa_irda_enable_sirclk(si);
  128. }
  129. /* disable STUART first */
  130. STIER = 0;
  131. /* access DLL & DLH */
  132. STLCR |= LCR_DLAB;
  133. STDLL = divisor & 0xff;
  134. STDLH = divisor >> 8;
  135. STLCR &= ~LCR_DLAB;
  136. si->speed = speed;
  137. STISR = IrSR_IR_RECEIVE_ON | IrSR_XMODE_PULSE_1_6;
  138. STIER = IER_UUE | IER_RLSE | IER_RAVIE | IER_RTIOE;
  139. local_irq_restore(flags);
  140. break;
  141. case 4000000:
  142. local_irq_save(flags);
  143. /* disable STUART */
  144. STIER = 0;
  145. STISR = 0;
  146. pxa_irda_disable_clk(si);
  147. /* disable FICP first */
  148. ICCR0 = 0;
  149. /* set board transceiver to FIR mode */
  150. si->pdata->transceiver_mode(si->dev, IR_FIRMODE);
  151. /* enable the FICP clock */
  152. pxa_irda_enable_firclk(si);
  153. si->speed = speed;
  154. pxa_irda_fir_dma_rx_start(si);
  155. ICCR0 = ICCR0_ITR | ICCR0_RXE;
  156. local_irq_restore(flags);
  157. break;
  158. default:
  159. return -EINVAL;
  160. }
  161. return 0;
  162. }
  163. /* SIR interrupt service routine. */
  164. static irqreturn_t pxa_irda_sir_irq(int irq, void *dev_id)
  165. {
  166. struct net_device *dev = dev_id;
  167. struct pxa_irda *si = netdev_priv(dev);
  168. int iir, lsr, data;
  169. iir = STIIR;
  170. switch (iir & 0x0F) {
  171. case 0x06: /* Receiver Line Status */
  172. lsr = STLSR;
  173. while (lsr & LSR_FIFOE) {
  174. data = STRBR;
  175. if (lsr & (LSR_OE | LSR_PE | LSR_FE | LSR_BI)) {
  176. printk(KERN_DEBUG "pxa_ir: sir receiving error\n");
  177. si->stats.rx_errors++;
  178. if (lsr & LSR_FE)
  179. si->stats.rx_frame_errors++;
  180. if (lsr & LSR_OE)
  181. si->stats.rx_fifo_errors++;
  182. } else {
  183. si->stats.rx_bytes++;
  184. async_unwrap_char(dev, &si->stats, &si->rx_buff, data);
  185. }
  186. lsr = STLSR;
  187. }
  188. dev->last_rx = jiffies;
  189. si->last_oscr = OSCR;
  190. break;
  191. case 0x04: /* Received Data Available */
  192. /* forth through */
  193. case 0x0C: /* Character Timeout Indication */
  194. do {
  195. si->stats.rx_bytes++;
  196. async_unwrap_char(dev, &si->stats, &si->rx_buff, STRBR);
  197. } while (STLSR & LSR_DR);
  198. dev->last_rx = jiffies;
  199. si->last_oscr = OSCR;
  200. break;
  201. case 0x02: /* Transmit FIFO Data Request */
  202. while ((si->tx_buff.len) && (STLSR & LSR_TDRQ)) {
  203. STTHR = *si->tx_buff.data++;
  204. si->tx_buff.len -= 1;
  205. }
  206. if (si->tx_buff.len == 0) {
  207. si->stats.tx_packets++;
  208. si->stats.tx_bytes += si->tx_buff.data -
  209. si->tx_buff.head;
  210. /* We need to ensure that the transmitter has finished. */
  211. while ((STLSR & LSR_TEMT) == 0)
  212. cpu_relax();
  213. si->last_oscr = OSCR;
  214. /*
  215. * Ok, we've finished transmitting. Now enable
  216. * the receiver. Sometimes we get a receive IRQ
  217. * immediately after a transmit...
  218. */
  219. if (si->newspeed) {
  220. pxa_irda_set_speed(si, si->newspeed);
  221. si->newspeed = 0;
  222. } else {
  223. /* enable IR Receiver, disable IR Transmitter */
  224. STISR = IrSR_IR_RECEIVE_ON | IrSR_XMODE_PULSE_1_6;
  225. /* enable STUART and receive interrupts */
  226. STIER = IER_UUE | IER_RLSE | IER_RAVIE | IER_RTIOE;
  227. }
  228. /* I'm hungry! */
  229. netif_wake_queue(dev);
  230. }
  231. break;
  232. }
  233. return IRQ_HANDLED;
  234. }
  235. /* FIR Receive DMA interrupt handler */
  236. static void pxa_irda_fir_dma_rx_irq(int channel, void *data)
  237. {
  238. int dcsr = DCSR(channel);
  239. DCSR(channel) = dcsr & ~DCSR_RUN;
  240. printk(KERN_DEBUG "pxa_ir: fir rx dma bus error %#x\n", dcsr);
  241. }
  242. /* FIR Transmit DMA interrupt handler */
  243. static void pxa_irda_fir_dma_tx_irq(int channel, void *data)
  244. {
  245. struct net_device *dev = data;
  246. struct pxa_irda *si = netdev_priv(dev);
  247. int dcsr;
  248. dcsr = DCSR(channel);
  249. DCSR(channel) = dcsr & ~DCSR_RUN;
  250. if (dcsr & DCSR_ENDINTR) {
  251. si->stats.tx_packets++;
  252. si->stats.tx_bytes += si->dma_tx_buff_len;
  253. } else {
  254. si->stats.tx_errors++;
  255. }
  256. while (ICSR1 & ICSR1_TBY)
  257. cpu_relax();
  258. si->last_oscr = OSCR;
  259. /*
  260. * HACK: It looks like the TBY bit is dropped too soon.
  261. * Without this delay things break.
  262. */
  263. udelay(120);
  264. if (si->newspeed) {
  265. pxa_irda_set_speed(si, si->newspeed);
  266. si->newspeed = 0;
  267. } else {
  268. int i = 64;
  269. ICCR0 = 0;
  270. pxa_irda_fir_dma_rx_start(si);
  271. while ((ICSR1 & ICSR1_RNE) && i--)
  272. (void)ICDR;
  273. ICCR0 = ICCR0_ITR | ICCR0_RXE;
  274. if (i < 0)
  275. printk(KERN_ERR "pxa_ir: cannot clear Rx FIFO!\n");
  276. }
  277. netif_wake_queue(dev);
  278. }
  279. /* EIF(Error in FIFO/End in Frame) handler for FIR */
  280. static void pxa_irda_fir_irq_eif(struct pxa_irda *si, struct net_device *dev, int icsr0)
  281. {
  282. unsigned int len, stat, data;
  283. /* Get the current data position. */
  284. len = DTADR(si->rxdma) - si->dma_rx_buff_phy;
  285. do {
  286. /* Read Status, and then Data. */
  287. stat = ICSR1;
  288. rmb();
  289. data = ICDR;
  290. if (stat & (ICSR1_CRE | ICSR1_ROR)) {
  291. si->stats.rx_errors++;
  292. if (stat & ICSR1_CRE) {
  293. printk(KERN_DEBUG "pxa_ir: fir receive CRC error\n");
  294. si->stats.rx_crc_errors++;
  295. }
  296. if (stat & ICSR1_ROR) {
  297. printk(KERN_DEBUG "pxa_ir: fir receive overrun\n");
  298. si->stats.rx_over_errors++;
  299. }
  300. } else {
  301. si->dma_rx_buff[len++] = data;
  302. }
  303. /* If we hit the end of frame, there's no point in continuing. */
  304. if (stat & ICSR1_EOF)
  305. break;
  306. } while (ICSR0 & ICSR0_EIF);
  307. if (stat & ICSR1_EOF) {
  308. /* end of frame. */
  309. struct sk_buff *skb;
  310. if (icsr0 & ICSR0_FRE) {
  311. printk(KERN_ERR "pxa_ir: dropping erroneous frame\n");
  312. si->stats.rx_dropped++;
  313. return;
  314. }
  315. skb = alloc_skb(len+1,GFP_ATOMIC);
  316. if (!skb) {
  317. printk(KERN_ERR "pxa_ir: fir out of memory for receive skb\n");
  318. si->stats.rx_dropped++;
  319. return;
  320. }
  321. /* Align IP header to 20 bytes */
  322. skb_reserve(skb, 1);
  323. skb_copy_to_linear_data(skb, si->dma_rx_buff, len);
  324. skb_put(skb, len);
  325. /* Feed it to IrLAP */
  326. skb->dev = dev;
  327. skb_reset_mac_header(skb);
  328. skb->protocol = htons(ETH_P_IRDA);
  329. netif_rx(skb);
  330. si->stats.rx_packets++;
  331. si->stats.rx_bytes += len;
  332. dev->last_rx = jiffies;
  333. }
  334. }
  335. /* FIR interrupt handler */
  336. static irqreturn_t pxa_irda_fir_irq(int irq, void *dev_id)
  337. {
  338. struct net_device *dev = dev_id;
  339. struct pxa_irda *si = netdev_priv(dev);
  340. int icsr0, i = 64;
  341. /* stop RX DMA */
  342. DCSR(si->rxdma) &= ~DCSR_RUN;
  343. si->last_oscr = OSCR;
  344. icsr0 = ICSR0;
  345. if (icsr0 & (ICSR0_FRE | ICSR0_RAB)) {
  346. if (icsr0 & ICSR0_FRE) {
  347. printk(KERN_DEBUG "pxa_ir: fir receive frame error\n");
  348. si->stats.rx_frame_errors++;
  349. } else {
  350. printk(KERN_DEBUG "pxa_ir: fir receive abort\n");
  351. si->stats.rx_errors++;
  352. }
  353. ICSR0 = icsr0 & (ICSR0_FRE | ICSR0_RAB);
  354. }
  355. if (icsr0 & ICSR0_EIF) {
  356. /* An error in FIFO occured, or there is a end of frame */
  357. pxa_irda_fir_irq_eif(si, dev, icsr0);
  358. }
  359. ICCR0 = 0;
  360. pxa_irda_fir_dma_rx_start(si);
  361. while ((ICSR1 & ICSR1_RNE) && i--)
  362. (void)ICDR;
  363. ICCR0 = ICCR0_ITR | ICCR0_RXE;
  364. if (i < 0)
  365. printk(KERN_ERR "pxa_ir: cannot clear Rx FIFO!\n");
  366. return IRQ_HANDLED;
  367. }
  368. /* hard_xmit interface of irda device */
  369. static int pxa_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  370. {
  371. struct pxa_irda *si = netdev_priv(dev);
  372. int speed = irda_get_next_speed(skb);
  373. /*
  374. * Does this packet contain a request to change the interface
  375. * speed? If so, remember it until we complete the transmission
  376. * of this frame.
  377. */
  378. if (speed != si->speed && speed != -1)
  379. si->newspeed = speed;
  380. /*
  381. * If this is an empty frame, we can bypass a lot.
  382. */
  383. if (skb->len == 0) {
  384. if (si->newspeed) {
  385. si->newspeed = 0;
  386. pxa_irda_set_speed(si, speed);
  387. }
  388. dev_kfree_skb(skb);
  389. return 0;
  390. }
  391. netif_stop_queue(dev);
  392. if (!IS_FIR(si)) {
  393. si->tx_buff.data = si->tx_buff.head;
  394. si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data, si->tx_buff.truesize);
  395. /* Disable STUART interrupts and switch to transmit mode. */
  396. STIER = 0;
  397. STISR = IrSR_IR_TRANSMIT_ON | IrSR_XMODE_PULSE_1_6;
  398. /* enable STUART and transmit interrupts */
  399. STIER = IER_UUE | IER_TIE;
  400. } else {
  401. unsigned long mtt = irda_get_mtt(skb);
  402. si->dma_tx_buff_len = skb->len;
  403. skb_copy_from_linear_data(skb, si->dma_tx_buff, skb->len);
  404. if (mtt)
  405. while ((unsigned)(OSCR - si->last_oscr)/4 < mtt)
  406. cpu_relax();
  407. /* stop RX DMA, disable FICP */
  408. DCSR(si->rxdma) &= ~DCSR_RUN;
  409. ICCR0 = 0;
  410. pxa_irda_fir_dma_tx_start(si);
  411. ICCR0 = ICCR0_ITR | ICCR0_TXE;
  412. }
  413. dev_kfree_skb(skb);
  414. dev->trans_start = jiffies;
  415. return 0;
  416. }
  417. static int pxa_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
  418. {
  419. struct if_irda_req *rq = (struct if_irda_req *)ifreq;
  420. struct pxa_irda *si = netdev_priv(dev);
  421. int ret;
  422. switch (cmd) {
  423. case SIOCSBANDWIDTH:
  424. ret = -EPERM;
  425. if (capable(CAP_NET_ADMIN)) {
  426. /*
  427. * We are unable to set the speed if the
  428. * device is not running.
  429. */
  430. if (netif_running(dev)) {
  431. ret = pxa_irda_set_speed(si,
  432. rq->ifr_baudrate);
  433. } else {
  434. printk(KERN_INFO "pxa_ir: SIOCSBANDWIDTH: !netif_running\n");
  435. ret = 0;
  436. }
  437. }
  438. break;
  439. case SIOCSMEDIABUSY:
  440. ret = -EPERM;
  441. if (capable(CAP_NET_ADMIN)) {
  442. irda_device_set_media_busy(dev, TRUE);
  443. ret = 0;
  444. }
  445. break;
  446. case SIOCGRECEIVING:
  447. ret = 0;
  448. rq->ifr_receiving = IS_FIR(si) ? 0
  449. : si->rx_buff.state != OUTSIDE_FRAME;
  450. break;
  451. default:
  452. ret = -EOPNOTSUPP;
  453. break;
  454. }
  455. return ret;
  456. }
  457. static struct net_device_stats *pxa_irda_stats(struct net_device *dev)
  458. {
  459. struct pxa_irda *si = netdev_priv(dev);
  460. return &si->stats;
  461. }
  462. static void pxa_irda_startup(struct pxa_irda *si)
  463. {
  464. /* Disable STUART interrupts */
  465. STIER = 0;
  466. /* enable STUART interrupt to the processor */
  467. STMCR = MCR_OUT2;
  468. /* configure SIR frame format: StartBit - Data 7 ... Data 0 - Stop Bit */
  469. STLCR = LCR_WLS0 | LCR_WLS1;
  470. /* enable FIFO, we use FIFO to improve performance */
  471. STFCR = FCR_TRFIFOE | FCR_ITL_32;
  472. /* disable FICP */
  473. ICCR0 = 0;
  474. /* configure FICP ICCR2 */
  475. ICCR2 = ICCR2_TXP | ICCR2_TRIG_32;
  476. /* configure DMAC */
  477. DRCMR(17) = si->rxdma | DRCMR_MAPVLD;
  478. DRCMR(18) = si->txdma | DRCMR_MAPVLD;
  479. /* force SIR reinitialization */
  480. si->speed = 4000000;
  481. pxa_irda_set_speed(si, 9600);
  482. printk(KERN_DEBUG "pxa_ir: irda startup\n");
  483. }
  484. static void pxa_irda_shutdown(struct pxa_irda *si)
  485. {
  486. unsigned long flags;
  487. local_irq_save(flags);
  488. /* disable STUART and interrupt */
  489. STIER = 0;
  490. /* disable STUART SIR mode */
  491. STISR = 0;
  492. /* disable DMA */
  493. DCSR(si->txdma) &= ~DCSR_RUN;
  494. DCSR(si->rxdma) &= ~DCSR_RUN;
  495. /* disable FICP */
  496. ICCR0 = 0;
  497. /* disable the STUART or FICP clocks */
  498. pxa_irda_disable_clk(si);
  499. DRCMR(17) = 0;
  500. DRCMR(18) = 0;
  501. local_irq_restore(flags);
  502. /* power off board transceiver */
  503. si->pdata->transceiver_mode(si->dev, IR_OFF);
  504. printk(KERN_DEBUG "pxa_ir: irda shutdown\n");
  505. }
  506. static int pxa_irda_start(struct net_device *dev)
  507. {
  508. struct pxa_irda *si = netdev_priv(dev);
  509. int err;
  510. si->speed = 9600;
  511. err = request_irq(IRQ_STUART, pxa_irda_sir_irq, 0, dev->name, dev);
  512. if (err)
  513. goto err_irq1;
  514. err = request_irq(IRQ_ICP, pxa_irda_fir_irq, 0, dev->name, dev);
  515. if (err)
  516. goto err_irq2;
  517. /*
  518. * The interrupt must remain disabled for now.
  519. */
  520. disable_irq(IRQ_STUART);
  521. disable_irq(IRQ_ICP);
  522. err = -EBUSY;
  523. si->rxdma = pxa_request_dma("FICP_RX",DMA_PRIO_LOW, pxa_irda_fir_dma_rx_irq, dev);
  524. if (si->rxdma < 0)
  525. goto err_rx_dma;
  526. si->txdma = pxa_request_dma("FICP_TX",DMA_PRIO_LOW, pxa_irda_fir_dma_tx_irq, dev);
  527. if (si->txdma < 0)
  528. goto err_tx_dma;
  529. err = -ENOMEM;
  530. si->dma_rx_buff = dma_alloc_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT,
  531. &si->dma_rx_buff_phy, GFP_KERNEL );
  532. if (!si->dma_rx_buff)
  533. goto err_dma_rx_buff;
  534. si->dma_tx_buff = dma_alloc_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT,
  535. &si->dma_tx_buff_phy, GFP_KERNEL );
  536. if (!si->dma_tx_buff)
  537. goto err_dma_tx_buff;
  538. /* Setup the serial port for the initial speed. */
  539. pxa_irda_startup(si);
  540. /*
  541. * Open a new IrLAP layer instance.
  542. */
  543. si->irlap = irlap_open(dev, &si->qos, "pxa");
  544. err = -ENOMEM;
  545. if (!si->irlap)
  546. goto err_irlap;
  547. /*
  548. * Now enable the interrupt and start the queue
  549. */
  550. enable_irq(IRQ_STUART);
  551. enable_irq(IRQ_ICP);
  552. netif_start_queue(dev);
  553. printk(KERN_DEBUG "pxa_ir: irda driver opened\n");
  554. return 0;
  555. err_irlap:
  556. pxa_irda_shutdown(si);
  557. dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_tx_buff, si->dma_tx_buff_phy);
  558. err_dma_tx_buff:
  559. dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_rx_buff, si->dma_rx_buff_phy);
  560. err_dma_rx_buff:
  561. pxa_free_dma(si->txdma);
  562. err_tx_dma:
  563. pxa_free_dma(si->rxdma);
  564. err_rx_dma:
  565. free_irq(IRQ_ICP, dev);
  566. err_irq2:
  567. free_irq(IRQ_STUART, dev);
  568. err_irq1:
  569. return err;
  570. }
  571. static int pxa_irda_stop(struct net_device *dev)
  572. {
  573. struct pxa_irda *si = netdev_priv(dev);
  574. netif_stop_queue(dev);
  575. pxa_irda_shutdown(si);
  576. /* Stop IrLAP */
  577. if (si->irlap) {
  578. irlap_close(si->irlap);
  579. si->irlap = NULL;
  580. }
  581. free_irq(IRQ_STUART, dev);
  582. free_irq(IRQ_ICP, dev);
  583. pxa_free_dma(si->rxdma);
  584. pxa_free_dma(si->txdma);
  585. if (si->dma_rx_buff)
  586. dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_tx_buff, si->dma_tx_buff_phy);
  587. if (si->dma_tx_buff)
  588. dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_rx_buff, si->dma_rx_buff_phy);
  589. printk(KERN_DEBUG "pxa_ir: irda driver closed\n");
  590. return 0;
  591. }
  592. static int pxa_irda_suspend(struct platform_device *_dev, pm_message_t state)
  593. {
  594. struct net_device *dev = platform_get_drvdata(_dev);
  595. struct pxa_irda *si;
  596. if (dev && netif_running(dev)) {
  597. si = netdev_priv(dev);
  598. netif_device_detach(dev);
  599. pxa_irda_shutdown(si);
  600. }
  601. return 0;
  602. }
  603. static int pxa_irda_resume(struct platform_device *_dev)
  604. {
  605. struct net_device *dev = platform_get_drvdata(_dev);
  606. struct pxa_irda *si;
  607. if (dev && netif_running(dev)) {
  608. si = netdev_priv(dev);
  609. pxa_irda_startup(si);
  610. netif_device_attach(dev);
  611. netif_wake_queue(dev);
  612. }
  613. return 0;
  614. }
  615. static int pxa_irda_init_iobuf(iobuff_t *io, int size)
  616. {
  617. io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
  618. if (io->head != NULL) {
  619. io->truesize = size;
  620. io->in_frame = FALSE;
  621. io->state = OUTSIDE_FRAME;
  622. io->data = io->head;
  623. }
  624. return io->head ? 0 : -ENOMEM;
  625. }
  626. static int pxa_irda_probe(struct platform_device *pdev)
  627. {
  628. struct net_device *dev;
  629. struct pxa_irda *si;
  630. unsigned int baudrate_mask;
  631. int err;
  632. if (!pdev->dev.platform_data)
  633. return -ENODEV;
  634. err = request_mem_region(__PREG(STUART), 0x24, "IrDA") ? 0 : -EBUSY;
  635. if (err)
  636. goto err_mem_1;
  637. err = request_mem_region(__PREG(FICP), 0x1c, "IrDA") ? 0 : -EBUSY;
  638. if (err)
  639. goto err_mem_2;
  640. dev = alloc_irdadev(sizeof(struct pxa_irda));
  641. if (!dev)
  642. goto err_mem_3;
  643. si = netdev_priv(dev);
  644. si->dev = &pdev->dev;
  645. si->pdata = pdev->dev.platform_data;
  646. si->sir_clk = clk_get(&pdev->dev, "UARTCLK");
  647. si->fir_clk = clk_get(&pdev->dev, "FICPCLK");
  648. if (IS_ERR(si->sir_clk) || IS_ERR(si->fir_clk)) {
  649. err = PTR_ERR(IS_ERR(si->sir_clk) ? si->sir_clk : si->fir_clk);
  650. goto err_mem_4;
  651. }
  652. /*
  653. * Initialise the SIR buffers
  654. */
  655. err = pxa_irda_init_iobuf(&si->rx_buff, 14384);
  656. if (err)
  657. goto err_mem_4;
  658. err = pxa_irda_init_iobuf(&si->tx_buff, 4000);
  659. if (err)
  660. goto err_mem_5;
  661. if (si->pdata->startup)
  662. err = si->pdata->startup(si->dev);
  663. if (err)
  664. goto err_startup;
  665. dev->hard_start_xmit = pxa_irda_hard_xmit;
  666. dev->open = pxa_irda_start;
  667. dev->stop = pxa_irda_stop;
  668. dev->do_ioctl = pxa_irda_ioctl;
  669. dev->get_stats = pxa_irda_stats;
  670. irda_init_max_qos_capabilies(&si->qos);
  671. baudrate_mask = 0;
  672. if (si->pdata->transceiver_cap & IR_SIRMODE)
  673. baudrate_mask |= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
  674. if (si->pdata->transceiver_cap & IR_FIRMODE)
  675. baudrate_mask |= IR_4000000 << 8;
  676. si->qos.baud_rate.bits &= baudrate_mask;
  677. si->qos.min_turn_time.bits = 7; /* 1ms or more */
  678. irda_qos_bits_to_value(&si->qos);
  679. err = register_netdev(dev);
  680. if (err == 0)
  681. dev_set_drvdata(&pdev->dev, dev);
  682. if (err) {
  683. if (si->pdata->shutdown)
  684. si->pdata->shutdown(si->dev);
  685. err_startup:
  686. kfree(si->tx_buff.head);
  687. err_mem_5:
  688. kfree(si->rx_buff.head);
  689. err_mem_4:
  690. if (si->sir_clk && !IS_ERR(si->sir_clk))
  691. clk_put(si->sir_clk);
  692. if (si->fir_clk && !IS_ERR(si->fir_clk))
  693. clk_put(si->fir_clk);
  694. free_netdev(dev);
  695. err_mem_3:
  696. release_mem_region(__PREG(FICP), 0x1c);
  697. err_mem_2:
  698. release_mem_region(__PREG(STUART), 0x24);
  699. }
  700. err_mem_1:
  701. return err;
  702. }
  703. static int pxa_irda_remove(struct platform_device *_dev)
  704. {
  705. struct net_device *dev = platform_get_drvdata(_dev);
  706. if (dev) {
  707. struct pxa_irda *si = netdev_priv(dev);
  708. unregister_netdev(dev);
  709. if (si->pdata->shutdown)
  710. si->pdata->shutdown(si->dev);
  711. kfree(si->tx_buff.head);
  712. kfree(si->rx_buff.head);
  713. clk_put(si->fir_clk);
  714. clk_put(si->sir_clk);
  715. free_netdev(dev);
  716. }
  717. release_mem_region(__PREG(STUART), 0x24);
  718. release_mem_region(__PREG(FICP), 0x1c);
  719. return 0;
  720. }
  721. static struct platform_driver pxa_ir_driver = {
  722. .driver = {
  723. .name = "pxa2xx-ir",
  724. .owner = THIS_MODULE,
  725. },
  726. .probe = pxa_irda_probe,
  727. .remove = pxa_irda_remove,
  728. .suspend = pxa_irda_suspend,
  729. .resume = pxa_irda_resume,
  730. };
  731. static int __init pxa_irda_init(void)
  732. {
  733. return platform_driver_register(&pxa_ir_driver);
  734. }
  735. static void __exit pxa_irda_exit(void)
  736. {
  737. platform_driver_unregister(&pxa_ir_driver);
  738. }
  739. module_init(pxa_irda_init);
  740. module_exit(pxa_irda_exit);
  741. MODULE_LICENSE("GPL");
  742. MODULE_ALIAS("platform:pxa2xx-ir");