sa1100_ir.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  1. /*
  2. * linux/drivers/net/irda/sa1100_ir.c
  3. *
  4. * Copyright (C) 2000-2001 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Infra-red driver for the StrongARM SA1100 embedded microprocessor
  11. *
  12. * Note that we don't have to worry about the SA1111's DMA bugs in here,
  13. * so we use the straight forward dma_map_* functions with a null pointer.
  14. *
  15. * This driver takes one kernel command line parameter, sa1100ir=, with
  16. * the following options:
  17. * max_rate:baudrate - set the maximum baud rate
  18. * power_level:level - set the transmitter power level
  19. * tx_lpm:0|1 - set transmit low power mode
  20. */
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/types.h>
  24. #include <linux/init.h>
  25. #include <linux/errno.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/slab.h>
  28. #include <linux/rtnetlink.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/dmaengine.h>
  34. #include <linux/sa11x0-dma.h>
  35. #include <net/irda/irda.h>
  36. #include <net/irda/wrapper.h>
  37. #include <net/irda/irda_device.h>
  38. #include <mach/hardware.h>
  39. #include <linux/platform_data/irda-sa11x0.h>
  40. static int power_level = 3;
  41. static int tx_lpm;
  42. static int max_rate = 4000000;
  43. struct sa1100_buf {
  44. struct device *dev;
  45. struct sk_buff *skb;
  46. struct scatterlist sg;
  47. struct dma_chan *chan;
  48. dma_cookie_t cookie;
  49. };
  50. struct sa1100_irda {
  51. unsigned char utcr4;
  52. unsigned char power;
  53. unsigned char open;
  54. int speed;
  55. int newspeed;
  56. struct sa1100_buf dma_rx;
  57. struct sa1100_buf dma_tx;
  58. struct device *dev;
  59. struct irda_platform_data *pdata;
  60. struct irlap_cb *irlap;
  61. struct qos_info qos;
  62. iobuff_t tx_buff;
  63. iobuff_t rx_buff;
  64. int (*tx_start)(struct sk_buff *, struct net_device *, struct sa1100_irda *);
  65. irqreturn_t (*irq)(struct net_device *, struct sa1100_irda *);
  66. };
  67. static int sa1100_irda_set_speed(struct sa1100_irda *, int);
  68. #define IS_FIR(si) ((si)->speed >= 4000000)
  69. #define HPSIR_MAX_RXLEN 2047
  70. static struct dma_slave_config sa1100_irda_sir_tx = {
  71. .direction = DMA_TO_DEVICE,
  72. .dst_addr = __PREG(Ser2UTDR),
  73. .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  74. .dst_maxburst = 4,
  75. };
  76. static struct dma_slave_config sa1100_irda_fir_rx = {
  77. .direction = DMA_FROM_DEVICE,
  78. .src_addr = __PREG(Ser2HSDR),
  79. .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  80. .src_maxburst = 8,
  81. };
  82. static struct dma_slave_config sa1100_irda_fir_tx = {
  83. .direction = DMA_TO_DEVICE,
  84. .dst_addr = __PREG(Ser2HSDR),
  85. .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  86. .dst_maxburst = 8,
  87. };
  88. static unsigned sa1100_irda_dma_xferred(struct sa1100_buf *buf)
  89. {
  90. struct dma_chan *chan = buf->chan;
  91. struct dma_tx_state state;
  92. enum dma_status status;
  93. status = chan->device->device_tx_status(chan, buf->cookie, &state);
  94. if (status != DMA_PAUSED)
  95. return 0;
  96. return sg_dma_len(&buf->sg) - state.residue;
  97. }
  98. static int sa1100_irda_dma_request(struct device *dev, struct sa1100_buf *buf,
  99. const char *name, struct dma_slave_config *cfg)
  100. {
  101. dma_cap_mask_t m;
  102. int ret;
  103. dma_cap_zero(m);
  104. dma_cap_set(DMA_SLAVE, m);
  105. buf->chan = dma_request_channel(m, sa11x0_dma_filter_fn, (void *)name);
  106. if (!buf->chan) {
  107. dev_err(dev, "unable to request DMA channel for %s\n",
  108. name);
  109. return -ENOENT;
  110. }
  111. ret = dmaengine_slave_config(buf->chan, cfg);
  112. if (ret)
  113. dev_warn(dev, "DMA slave_config for %s returned %d\n",
  114. name, ret);
  115. buf->dev = buf->chan->device->dev;
  116. return 0;
  117. }
  118. static void sa1100_irda_dma_start(struct sa1100_buf *buf,
  119. enum dma_transfer_direction dir, dma_async_tx_callback cb, void *cb_p)
  120. {
  121. struct dma_async_tx_descriptor *desc;
  122. struct dma_chan *chan = buf->chan;
  123. desc = dmaengine_prep_slave_sg(chan, &buf->sg, 1, dir,
  124. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  125. if (desc) {
  126. desc->callback = cb;
  127. desc->callback_param = cb_p;
  128. buf->cookie = dmaengine_submit(desc);
  129. dma_async_issue_pending(chan);
  130. }
  131. }
  132. /*
  133. * Allocate and map the receive buffer, unless it is already allocated.
  134. */
  135. static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
  136. {
  137. if (si->dma_rx.skb)
  138. return 0;
  139. si->dma_rx.skb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
  140. if (!si->dma_rx.skb) {
  141. printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
  142. return -ENOMEM;
  143. }
  144. /*
  145. * Align any IP headers that may be contained
  146. * within the frame.
  147. */
  148. skb_reserve(si->dma_rx.skb, 1);
  149. sg_set_buf(&si->dma_rx.sg, si->dma_rx.skb->data, HPSIR_MAX_RXLEN);
  150. if (dma_map_sg(si->dma_rx.dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE) == 0) {
  151. dev_kfree_skb_any(si->dma_rx.skb);
  152. return -ENOMEM;
  153. }
  154. return 0;
  155. }
  156. /*
  157. * We want to get here as soon as possible, and get the receiver setup.
  158. * We use the existing buffer.
  159. */
  160. static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
  161. {
  162. if (!si->dma_rx.skb) {
  163. printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
  164. return;
  165. }
  166. /*
  167. * First empty receive FIFO
  168. */
  169. Ser2HSCR0 = HSCR0_HSSP;
  170. /*
  171. * Enable the DMA, receiver and receive interrupt.
  172. */
  173. dmaengine_terminate_all(si->dma_rx.chan);
  174. sa1100_irda_dma_start(&si->dma_rx, DMA_DEV_TO_MEM, NULL, NULL);
  175. Ser2HSCR0 = HSCR0_HSSP | HSCR0_RXE;
  176. }
  177. static void sa1100_irda_check_speed(struct sa1100_irda *si)
  178. {
  179. if (si->newspeed) {
  180. sa1100_irda_set_speed(si, si->newspeed);
  181. si->newspeed = 0;
  182. }
  183. }
  184. /*
  185. * HP-SIR format support.
  186. */
  187. static void sa1100_irda_sirtxdma_irq(void *id)
  188. {
  189. struct net_device *dev = id;
  190. struct sa1100_irda *si = netdev_priv(dev);
  191. dma_unmap_sg(si->dma_tx.dev, &si->dma_tx.sg, 1, DMA_TO_DEVICE);
  192. dev_kfree_skb(si->dma_tx.skb);
  193. si->dma_tx.skb = NULL;
  194. dev->stats.tx_packets++;
  195. dev->stats.tx_bytes += sg_dma_len(&si->dma_tx.sg);
  196. /* We need to ensure that the transmitter has finished. */
  197. do
  198. rmb();
  199. while (Ser2UTSR1 & UTSR1_TBY);
  200. /*
  201. * Ok, we've finished transmitting. Now enable the receiver.
  202. * Sometimes we get a receive IRQ immediately after a transmit...
  203. */
  204. Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
  205. Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
  206. sa1100_irda_check_speed(si);
  207. /* I'm hungry! */
  208. netif_wake_queue(dev);
  209. }
  210. static int sa1100_irda_sir_tx_start(struct sk_buff *skb, struct net_device *dev,
  211. struct sa1100_irda *si)
  212. {
  213. si->tx_buff.data = si->tx_buff.head;
  214. si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
  215. si->tx_buff.truesize);
  216. si->dma_tx.skb = skb;
  217. sg_set_buf(&si->dma_tx.sg, si->tx_buff.data, si->tx_buff.len);
  218. if (dma_map_sg(si->dma_tx.dev, &si->dma_tx.sg, 1, DMA_TO_DEVICE) == 0) {
  219. si->dma_tx.skb = NULL;
  220. netif_wake_queue(dev);
  221. dev->stats.tx_dropped++;
  222. return NETDEV_TX_OK;
  223. }
  224. sa1100_irda_dma_start(&si->dma_tx, DMA_MEM_TO_DEV, sa1100_irda_sirtxdma_irq, dev);
  225. /*
  226. * The mean turn-around time is enforced by XBOF padding,
  227. * so we don't have to do anything special here.
  228. */
  229. Ser2UTCR3 = UTCR3_TXE;
  230. return NETDEV_TX_OK;
  231. }
  232. static irqreturn_t sa1100_irda_sir_irq(struct net_device *dev, struct sa1100_irda *si)
  233. {
  234. int status;
  235. status = Ser2UTSR0;
  236. /*
  237. * Deal with any receive errors first. The bytes in error may be
  238. * the only bytes in the receive FIFO, so we do this first.
  239. */
  240. while (status & UTSR0_EIF) {
  241. int stat, data;
  242. stat = Ser2UTSR1;
  243. data = Ser2UTDR;
  244. if (stat & (UTSR1_FRE | UTSR1_ROR)) {
  245. dev->stats.rx_errors++;
  246. if (stat & UTSR1_FRE)
  247. dev->stats.rx_frame_errors++;
  248. if (stat & UTSR1_ROR)
  249. dev->stats.rx_fifo_errors++;
  250. } else
  251. async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
  252. status = Ser2UTSR0;
  253. }
  254. /*
  255. * We must clear certain bits.
  256. */
  257. Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
  258. if (status & UTSR0_RFS) {
  259. /*
  260. * There are at least 4 bytes in the FIFO. Read 3 bytes
  261. * and leave the rest to the block below.
  262. */
  263. async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
  264. async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
  265. async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
  266. }
  267. if (status & (UTSR0_RFS | UTSR0_RID)) {
  268. /*
  269. * Fifo contains more than 1 character.
  270. */
  271. do {
  272. async_unwrap_char(dev, &dev->stats, &si->rx_buff,
  273. Ser2UTDR);
  274. } while (Ser2UTSR1 & UTSR1_RNE);
  275. }
  276. return IRQ_HANDLED;
  277. }
  278. /*
  279. * FIR format support.
  280. */
  281. static void sa1100_irda_firtxdma_irq(void *id)
  282. {
  283. struct net_device *dev = id;
  284. struct sa1100_irda *si = netdev_priv(dev);
  285. struct sk_buff *skb;
  286. /*
  287. * Wait for the transmission to complete. Unfortunately,
  288. * the hardware doesn't give us an interrupt to indicate
  289. * "end of frame".
  290. */
  291. do
  292. rmb();
  293. while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
  294. /*
  295. * Clear the transmit underrun bit.
  296. */
  297. Ser2HSSR0 = HSSR0_TUR;
  298. /*
  299. * Do we need to change speed? Note that we're lazy
  300. * here - we don't free the old dma_rx.skb. We don't need
  301. * to allocate a buffer either.
  302. */
  303. sa1100_irda_check_speed(si);
  304. /*
  305. * Start reception. This disables the transmitter for
  306. * us. This will be using the existing RX buffer.
  307. */
  308. sa1100_irda_rx_dma_start(si);
  309. /* Account and free the packet. */
  310. skb = si->dma_tx.skb;
  311. if (skb) {
  312. dma_unmap_sg(si->dma_tx.dev, &si->dma_tx.sg, 1,
  313. DMA_TO_DEVICE);
  314. dev->stats.tx_packets ++;
  315. dev->stats.tx_bytes += skb->len;
  316. dev_kfree_skb_irq(skb);
  317. si->dma_tx.skb = NULL;
  318. }
  319. /*
  320. * Make sure that the TX queue is available for sending
  321. * (for retries). TX has priority over RX at all times.
  322. */
  323. netif_wake_queue(dev);
  324. }
  325. static int sa1100_irda_fir_tx_start(struct sk_buff *skb, struct net_device *dev,
  326. struct sa1100_irda *si)
  327. {
  328. int mtt = irda_get_mtt(skb);
  329. si->dma_tx.skb = skb;
  330. sg_set_buf(&si->dma_tx.sg, skb->data, skb->len);
  331. if (dma_map_sg(si->dma_tx.dev, &si->dma_tx.sg, 1, DMA_TO_DEVICE) == 0) {
  332. si->dma_tx.skb = NULL;
  333. netif_wake_queue(dev);
  334. dev->stats.tx_dropped++;
  335. dev_kfree_skb(skb);
  336. return NETDEV_TX_OK;
  337. }
  338. sa1100_irda_dma_start(&si->dma_tx, DMA_MEM_TO_DEV, sa1100_irda_firtxdma_irq, dev);
  339. /*
  340. * If we have a mean turn-around time, impose the specified
  341. * specified delay. We could shorten this by timing from
  342. * the point we received the packet.
  343. */
  344. if (mtt)
  345. udelay(mtt);
  346. Ser2HSCR0 = HSCR0_HSSP | HSCR0_TXE;
  347. return NETDEV_TX_OK;
  348. }
  349. static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
  350. {
  351. struct sk_buff *skb = si->dma_rx.skb;
  352. unsigned int len, stat, data;
  353. if (!skb) {
  354. printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
  355. return;
  356. }
  357. /*
  358. * Get the current data position.
  359. */
  360. len = sa1100_irda_dma_xferred(&si->dma_rx);
  361. if (len > HPSIR_MAX_RXLEN)
  362. len = HPSIR_MAX_RXLEN;
  363. dma_unmap_sg(si->dma_rx.dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE);
  364. do {
  365. /*
  366. * Read Status, and then Data.
  367. */
  368. stat = Ser2HSSR1;
  369. rmb();
  370. data = Ser2HSDR;
  371. if (stat & (HSSR1_CRE | HSSR1_ROR)) {
  372. dev->stats.rx_errors++;
  373. if (stat & HSSR1_CRE)
  374. dev->stats.rx_crc_errors++;
  375. if (stat & HSSR1_ROR)
  376. dev->stats.rx_frame_errors++;
  377. } else
  378. skb->data[len++] = data;
  379. /*
  380. * If we hit the end of frame, there's
  381. * no point in continuing.
  382. */
  383. if (stat & HSSR1_EOF)
  384. break;
  385. } while (Ser2HSSR0 & HSSR0_EIF);
  386. if (stat & HSSR1_EOF) {
  387. si->dma_rx.skb = NULL;
  388. skb_put(skb, len);
  389. skb->dev = dev;
  390. skb_reset_mac_header(skb);
  391. skb->protocol = htons(ETH_P_IRDA);
  392. dev->stats.rx_packets++;
  393. dev->stats.rx_bytes += len;
  394. /*
  395. * Before we pass the buffer up, allocate a new one.
  396. */
  397. sa1100_irda_rx_alloc(si);
  398. netif_rx(skb);
  399. } else {
  400. /*
  401. * Remap the buffer - it was previously mapped, and we
  402. * hope that this succeeds.
  403. */
  404. dma_map_sg(si->dma_rx.dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE);
  405. }
  406. }
  407. /*
  408. * We only have to handle RX events here; transmit events go via the TX
  409. * DMA handler. We disable RX, process, and the restart RX.
  410. */
  411. static irqreturn_t sa1100_irda_fir_irq(struct net_device *dev, struct sa1100_irda *si)
  412. {
  413. /*
  414. * Stop RX DMA
  415. */
  416. dmaengine_pause(si->dma_rx.chan);
  417. /*
  418. * Framing error - we throw away the packet completely.
  419. * Clearing RXE flushes the error conditions and data
  420. * from the fifo.
  421. */
  422. if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
  423. dev->stats.rx_errors++;
  424. if (Ser2HSSR0 & HSSR0_FRE)
  425. dev->stats.rx_frame_errors++;
  426. /*
  427. * Clear out the DMA...
  428. */
  429. Ser2HSCR0 = HSCR0_HSSP;
  430. /*
  431. * Clear selected status bits now, so we
  432. * don't miss them next time around.
  433. */
  434. Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
  435. }
  436. /*
  437. * Deal with any receive errors. The any of the lowest
  438. * 8 bytes in the FIFO may contain an error. We must read
  439. * them one by one. The "error" could even be the end of
  440. * packet!
  441. */
  442. if (Ser2HSSR0 & HSSR0_EIF)
  443. sa1100_irda_fir_error(si, dev);
  444. /*
  445. * No matter what happens, we must restart reception.
  446. */
  447. sa1100_irda_rx_dma_start(si);
  448. return IRQ_HANDLED;
  449. }
  450. /*
  451. * Set the IrDA communications speed.
  452. */
  453. static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
  454. {
  455. unsigned long flags;
  456. int brd, ret = -EINVAL;
  457. switch (speed) {
  458. case 9600: case 19200: case 38400:
  459. case 57600: case 115200:
  460. brd = 3686400 / (16 * speed) - 1;
  461. /* Stop the receive DMA, and configure transmit. */
  462. if (IS_FIR(si)) {
  463. dmaengine_terminate_all(si->dma_rx.chan);
  464. dmaengine_slave_config(si->dma_tx.chan,
  465. &sa1100_irda_sir_tx);
  466. }
  467. local_irq_save(flags);
  468. Ser2UTCR3 = 0;
  469. Ser2HSCR0 = HSCR0_UART;
  470. Ser2UTCR1 = brd >> 8;
  471. Ser2UTCR2 = brd;
  472. /*
  473. * Clear status register
  474. */
  475. Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
  476. Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
  477. if (si->pdata->set_speed)
  478. si->pdata->set_speed(si->dev, speed);
  479. si->speed = speed;
  480. si->tx_start = sa1100_irda_sir_tx_start;
  481. si->irq = sa1100_irda_sir_irq;
  482. local_irq_restore(flags);
  483. ret = 0;
  484. break;
  485. case 4000000:
  486. if (!IS_FIR(si))
  487. dmaengine_slave_config(si->dma_tx.chan,
  488. &sa1100_irda_fir_tx);
  489. local_irq_save(flags);
  490. Ser2HSSR0 = 0xff;
  491. Ser2HSCR0 = HSCR0_HSSP;
  492. Ser2UTCR3 = 0;
  493. si->speed = speed;
  494. si->tx_start = sa1100_irda_fir_tx_start;
  495. si->irq = sa1100_irda_fir_irq;
  496. if (si->pdata->set_speed)
  497. si->pdata->set_speed(si->dev, speed);
  498. sa1100_irda_rx_alloc(si);
  499. sa1100_irda_rx_dma_start(si);
  500. local_irq_restore(flags);
  501. break;
  502. default:
  503. break;
  504. }
  505. return ret;
  506. }
  507. /*
  508. * Control the power state of the IrDA transmitter.
  509. * State:
  510. * 0 - off
  511. * 1 - short range, lowest power
  512. * 2 - medium range, medium power
  513. * 3 - maximum range, high power
  514. *
  515. * Currently, only assabet is known to support this.
  516. */
  517. static int
  518. __sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
  519. {
  520. int ret = 0;
  521. if (si->pdata->set_power)
  522. ret = si->pdata->set_power(si->dev, state);
  523. return ret;
  524. }
  525. static inline int
  526. sa1100_set_power(struct sa1100_irda *si, unsigned int state)
  527. {
  528. int ret;
  529. ret = __sa1100_irda_set_power(si, state);
  530. if (ret == 0)
  531. si->power = state;
  532. return ret;
  533. }
  534. static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
  535. {
  536. struct net_device *dev = dev_id;
  537. struct sa1100_irda *si = netdev_priv(dev);
  538. return si->irq(dev, si);
  539. }
  540. static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  541. {
  542. struct sa1100_irda *si = netdev_priv(dev);
  543. int speed = irda_get_next_speed(skb);
  544. /*
  545. * Does this packet contain a request to change the interface
  546. * speed? If so, remember it until we complete the transmission
  547. * of this frame.
  548. */
  549. if (speed != si->speed && speed != -1)
  550. si->newspeed = speed;
  551. /* If this is an empty frame, we can bypass a lot. */
  552. if (skb->len == 0) {
  553. sa1100_irda_check_speed(si);
  554. dev_kfree_skb(skb);
  555. return NETDEV_TX_OK;
  556. }
  557. netif_stop_queue(dev);
  558. /* We must not already have a skb to transmit... */
  559. BUG_ON(si->dma_tx.skb);
  560. return si->tx_start(skb, dev, si);
  561. }
  562. static int
  563. sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
  564. {
  565. struct if_irda_req *rq = (struct if_irda_req *)ifreq;
  566. struct sa1100_irda *si = netdev_priv(dev);
  567. int ret = -EOPNOTSUPP;
  568. switch (cmd) {
  569. case SIOCSBANDWIDTH:
  570. if (capable(CAP_NET_ADMIN)) {
  571. /*
  572. * We are unable to set the speed if the
  573. * device is not running.
  574. */
  575. if (si->open) {
  576. ret = sa1100_irda_set_speed(si,
  577. rq->ifr_baudrate);
  578. } else {
  579. printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
  580. ret = 0;
  581. }
  582. }
  583. break;
  584. case SIOCSMEDIABUSY:
  585. ret = -EPERM;
  586. if (capable(CAP_NET_ADMIN)) {
  587. irda_device_set_media_busy(dev, TRUE);
  588. ret = 0;
  589. }
  590. break;
  591. case SIOCGRECEIVING:
  592. rq->ifr_receiving = IS_FIR(si) ? 0
  593. : si->rx_buff.state != OUTSIDE_FRAME;
  594. break;
  595. default:
  596. break;
  597. }
  598. return ret;
  599. }
  600. static int sa1100_irda_startup(struct sa1100_irda *si)
  601. {
  602. int ret;
  603. /*
  604. * Ensure that the ports for this device are setup correctly.
  605. */
  606. if (si->pdata->startup) {
  607. ret = si->pdata->startup(si->dev);
  608. if (ret)
  609. return ret;
  610. }
  611. /*
  612. * Configure PPC for IRDA - we want to drive TXD2 low.
  613. * We also want to drive this pin low during sleep.
  614. */
  615. PPSR &= ~PPC_TXD2;
  616. PSDR &= ~PPC_TXD2;
  617. PPDR |= PPC_TXD2;
  618. /*
  619. * Enable HP-SIR modulation, and ensure that the port is disabled.
  620. */
  621. Ser2UTCR3 = 0;
  622. Ser2HSCR0 = HSCR0_UART;
  623. Ser2UTCR4 = si->utcr4;
  624. Ser2UTCR0 = UTCR0_8BitData;
  625. Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
  626. /*
  627. * Clear status register
  628. */
  629. Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
  630. ret = sa1100_irda_set_speed(si, si->speed = 9600);
  631. if (ret) {
  632. Ser2UTCR3 = 0;
  633. Ser2HSCR0 = 0;
  634. if (si->pdata->shutdown)
  635. si->pdata->shutdown(si->dev);
  636. }
  637. return ret;
  638. }
  639. static void sa1100_irda_shutdown(struct sa1100_irda *si)
  640. {
  641. /*
  642. * Stop all DMA activity.
  643. */
  644. dmaengine_terminate_all(si->dma_rx.chan);
  645. dmaengine_terminate_all(si->dma_tx.chan);
  646. /* Disable the port. */
  647. Ser2UTCR3 = 0;
  648. Ser2HSCR0 = 0;
  649. if (si->pdata->shutdown)
  650. si->pdata->shutdown(si->dev);
  651. }
  652. static int sa1100_irda_start(struct net_device *dev)
  653. {
  654. struct sa1100_irda *si = netdev_priv(dev);
  655. int err;
  656. si->speed = 9600;
  657. err = sa1100_irda_dma_request(si->dev, &si->dma_rx, "Ser2ICPRc",
  658. &sa1100_irda_fir_rx);
  659. if (err)
  660. goto err_rx_dma;
  661. err = sa1100_irda_dma_request(si->dev, &si->dma_tx, "Ser2ICPTr",
  662. &sa1100_irda_sir_tx);
  663. if (err)
  664. goto err_tx_dma;
  665. /*
  666. * Setup the serial port for the specified speed.
  667. */
  668. err = sa1100_irda_startup(si);
  669. if (err)
  670. goto err_startup;
  671. /*
  672. * Open a new IrLAP layer instance.
  673. */
  674. si->irlap = irlap_open(dev, &si->qos, "sa1100");
  675. err = -ENOMEM;
  676. if (!si->irlap)
  677. goto err_irlap;
  678. err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
  679. if (err)
  680. goto err_irq;
  681. /*
  682. * Now enable the interrupt and start the queue
  683. */
  684. si->open = 1;
  685. sa1100_set_power(si, power_level); /* low power mode */
  686. netif_start_queue(dev);
  687. return 0;
  688. err_irq:
  689. irlap_close(si->irlap);
  690. err_irlap:
  691. si->open = 0;
  692. sa1100_irda_shutdown(si);
  693. err_startup:
  694. dma_release_channel(si->dma_tx.chan);
  695. err_tx_dma:
  696. dma_release_channel(si->dma_rx.chan);
  697. err_rx_dma:
  698. return err;
  699. }
  700. static int sa1100_irda_stop(struct net_device *dev)
  701. {
  702. struct sa1100_irda *si = netdev_priv(dev);
  703. struct sk_buff *skb;
  704. netif_stop_queue(dev);
  705. si->open = 0;
  706. sa1100_irda_shutdown(si);
  707. /*
  708. * If we have been doing any DMA activity, make sure we
  709. * tidy that up cleanly.
  710. */
  711. skb = si->dma_rx.skb;
  712. if (skb) {
  713. dma_unmap_sg(si->dma_rx.dev, &si->dma_rx.sg, 1,
  714. DMA_FROM_DEVICE);
  715. dev_kfree_skb(skb);
  716. si->dma_rx.skb = NULL;
  717. }
  718. skb = si->dma_tx.skb;
  719. if (skb) {
  720. dma_unmap_sg(si->dma_tx.dev, &si->dma_tx.sg, 1,
  721. DMA_TO_DEVICE);
  722. dev_kfree_skb(skb);
  723. si->dma_tx.skb = NULL;
  724. }
  725. /* Stop IrLAP */
  726. if (si->irlap) {
  727. irlap_close(si->irlap);
  728. si->irlap = NULL;
  729. }
  730. /*
  731. * Free resources
  732. */
  733. dma_release_channel(si->dma_tx.chan);
  734. dma_release_channel(si->dma_rx.chan);
  735. free_irq(dev->irq, dev);
  736. sa1100_set_power(si, 0);
  737. return 0;
  738. }
  739. static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
  740. {
  741. io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
  742. if (io->head != NULL) {
  743. io->truesize = size;
  744. io->in_frame = FALSE;
  745. io->state = OUTSIDE_FRAME;
  746. io->data = io->head;
  747. }
  748. return io->head ? 0 : -ENOMEM;
  749. }
  750. static const struct net_device_ops sa1100_irda_netdev_ops = {
  751. .ndo_open = sa1100_irda_start,
  752. .ndo_stop = sa1100_irda_stop,
  753. .ndo_start_xmit = sa1100_irda_hard_xmit,
  754. .ndo_do_ioctl = sa1100_irda_ioctl,
  755. };
  756. static int sa1100_irda_probe(struct platform_device *pdev)
  757. {
  758. struct net_device *dev;
  759. struct sa1100_irda *si;
  760. unsigned int baudrate_mask;
  761. int err, irq;
  762. if (!pdev->dev.platform_data)
  763. return -EINVAL;
  764. irq = platform_get_irq(pdev, 0);
  765. if (irq <= 0)
  766. return irq < 0 ? irq : -ENXIO;
  767. err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
  768. if (err)
  769. goto err_mem_1;
  770. err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
  771. if (err)
  772. goto err_mem_2;
  773. err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
  774. if (err)
  775. goto err_mem_3;
  776. dev = alloc_irdadev(sizeof(struct sa1100_irda));
  777. if (!dev) {
  778. err = -ENOMEM;
  779. goto err_mem_4;
  780. }
  781. SET_NETDEV_DEV(dev, &pdev->dev);
  782. si = netdev_priv(dev);
  783. si->dev = &pdev->dev;
  784. si->pdata = pdev->dev.platform_data;
  785. sg_init_table(&si->dma_rx.sg, 1);
  786. sg_init_table(&si->dma_tx.sg, 1);
  787. /*
  788. * Initialise the HP-SIR buffers
  789. */
  790. err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
  791. if (err)
  792. goto err_mem_5;
  793. err = sa1100_irda_init_iobuf(&si->tx_buff, IRDA_SIR_MAX_FRAME);
  794. if (err)
  795. goto err_mem_5;
  796. dev->netdev_ops = &sa1100_irda_netdev_ops;
  797. dev->irq = irq;
  798. irda_init_max_qos_capabilies(&si->qos);
  799. /*
  800. * We support original IRDA up to 115k2. (we don't currently
  801. * support 4Mbps). Min Turn Time set to 1ms or greater.
  802. */
  803. baudrate_mask = IR_9600;
  804. switch (max_rate) {
  805. case 4000000: baudrate_mask |= IR_4000000 << 8;
  806. case 115200: baudrate_mask |= IR_115200;
  807. case 57600: baudrate_mask |= IR_57600;
  808. case 38400: baudrate_mask |= IR_38400;
  809. case 19200: baudrate_mask |= IR_19200;
  810. }
  811. si->qos.baud_rate.bits &= baudrate_mask;
  812. si->qos.min_turn_time.bits = 7;
  813. irda_qos_bits_to_value(&si->qos);
  814. si->utcr4 = UTCR4_HPSIR;
  815. if (tx_lpm)
  816. si->utcr4 |= UTCR4_Z1_6us;
  817. /*
  818. * Initially enable HP-SIR modulation, and ensure that the port
  819. * is disabled.
  820. */
  821. Ser2UTCR3 = 0;
  822. Ser2UTCR4 = si->utcr4;
  823. Ser2HSCR0 = HSCR0_UART;
  824. err = register_netdev(dev);
  825. if (err == 0)
  826. platform_set_drvdata(pdev, dev);
  827. if (err) {
  828. err_mem_5:
  829. kfree(si->tx_buff.head);
  830. kfree(si->rx_buff.head);
  831. free_netdev(dev);
  832. err_mem_4:
  833. release_mem_region(__PREG(Ser2HSCR2), 0x04);
  834. err_mem_3:
  835. release_mem_region(__PREG(Ser2HSCR0), 0x1c);
  836. err_mem_2:
  837. release_mem_region(__PREG(Ser2UTCR0), 0x24);
  838. }
  839. err_mem_1:
  840. return err;
  841. }
  842. static int sa1100_irda_remove(struct platform_device *pdev)
  843. {
  844. struct net_device *dev = platform_get_drvdata(pdev);
  845. if (dev) {
  846. struct sa1100_irda *si = netdev_priv(dev);
  847. unregister_netdev(dev);
  848. kfree(si->tx_buff.head);
  849. kfree(si->rx_buff.head);
  850. free_netdev(dev);
  851. }
  852. release_mem_region(__PREG(Ser2HSCR2), 0x04);
  853. release_mem_region(__PREG(Ser2HSCR0), 0x1c);
  854. release_mem_region(__PREG(Ser2UTCR0), 0x24);
  855. return 0;
  856. }
  857. #ifdef CONFIG_PM
  858. /*
  859. * Suspend the IrDA interface.
  860. */
  861. static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
  862. {
  863. struct net_device *dev = platform_get_drvdata(pdev);
  864. struct sa1100_irda *si;
  865. if (!dev)
  866. return 0;
  867. si = netdev_priv(dev);
  868. if (si->open) {
  869. /*
  870. * Stop the transmit queue
  871. */
  872. netif_device_detach(dev);
  873. disable_irq(dev->irq);
  874. sa1100_irda_shutdown(si);
  875. __sa1100_irda_set_power(si, 0);
  876. }
  877. return 0;
  878. }
  879. /*
  880. * Resume the IrDA interface.
  881. */
  882. static int sa1100_irda_resume(struct platform_device *pdev)
  883. {
  884. struct net_device *dev = platform_get_drvdata(pdev);
  885. struct sa1100_irda *si;
  886. if (!dev)
  887. return 0;
  888. si = netdev_priv(dev);
  889. if (si->open) {
  890. /*
  891. * If we missed a speed change, initialise at the new speed
  892. * directly. It is debatable whether this is actually
  893. * required, but in the interests of continuing from where
  894. * we left off it is desirable. The converse argument is
  895. * that we should re-negotiate at 9600 baud again.
  896. */
  897. if (si->newspeed) {
  898. si->speed = si->newspeed;
  899. si->newspeed = 0;
  900. }
  901. sa1100_irda_startup(si);
  902. __sa1100_irda_set_power(si, si->power);
  903. enable_irq(dev->irq);
  904. /*
  905. * This automatically wakes up the queue
  906. */
  907. netif_device_attach(dev);
  908. }
  909. return 0;
  910. }
  911. #else
  912. #define sa1100_irda_suspend NULL
  913. #define sa1100_irda_resume NULL
  914. #endif
  915. static struct platform_driver sa1100ir_driver = {
  916. .probe = sa1100_irda_probe,
  917. .remove = sa1100_irda_remove,
  918. .suspend = sa1100_irda_suspend,
  919. .resume = sa1100_irda_resume,
  920. .driver = {
  921. .name = "sa11x0-ir",
  922. },
  923. };
  924. static int __init sa1100_irda_init(void)
  925. {
  926. /*
  927. * Limit power level a sensible range.
  928. */
  929. if (power_level < 1)
  930. power_level = 1;
  931. if (power_level > 3)
  932. power_level = 3;
  933. return platform_driver_register(&sa1100ir_driver);
  934. }
  935. static void __exit sa1100_irda_exit(void)
  936. {
  937. platform_driver_unregister(&sa1100ir_driver);
  938. }
  939. module_init(sa1100_irda_init);
  940. module_exit(sa1100_irda_exit);
  941. module_param(power_level, int, 0);
  942. module_param(tx_lpm, int, 0);
  943. module_param(max_rate, int, 0);
  944. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  945. MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
  946. MODULE_LICENSE("GPL");
  947. MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
  948. MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
  949. MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
  950. MODULE_ALIAS("platform:sa11x0-ir");