pxaficp_ir.c 23 KB

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