sa1100_ir.c 23 KB

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