au1k_ir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. /*
  2. * Alchemy Semi Au1000 IrDA driver
  3. *
  4. * Copyright 2001 MontaVista Software Inc.
  5. * Author: MontaVista Software, Inc.
  6. * ppopov@mvista.com or source@mvista.com
  7. *
  8. * This program is free software; you can distribute it and/or modify it
  9. * under the terms of the GNU General Public License (Version 2) as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  15. * for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/slab.h>
  25. #include <linux/time.h>
  26. #include <linux/types.h>
  27. #include <linux/ioport.h>
  28. #include <net/irda/irda.h>
  29. #include <net/irda/irmod.h>
  30. #include <net/irda/wrapper.h>
  31. #include <net/irda/irda_device.h>
  32. #include <asm/mach-au1x00/au1000.h>
  33. /* registers */
  34. #define IR_RING_PTR_STATUS 0x00
  35. #define IR_RING_BASE_ADDR_H 0x04
  36. #define IR_RING_BASE_ADDR_L 0x08
  37. #define IR_RING_SIZE 0x0C
  38. #define IR_RING_PROMPT 0x10
  39. #define IR_RING_ADDR_CMPR 0x14
  40. #define IR_INT_CLEAR 0x18
  41. #define IR_CONFIG_1 0x20
  42. #define IR_SIR_FLAGS 0x24
  43. #define IR_STATUS 0x28
  44. #define IR_READ_PHY_CONFIG 0x2C
  45. #define IR_WRITE_PHY_CONFIG 0x30
  46. #define IR_MAX_PKT_LEN 0x34
  47. #define IR_RX_BYTE_CNT 0x38
  48. #define IR_CONFIG_2 0x3C
  49. #define IR_ENABLE 0x40
  50. /* Config1 */
  51. #define IR_RX_INVERT_LED (1 << 0)
  52. #define IR_TX_INVERT_LED (1 << 1)
  53. #define IR_ST (1 << 2)
  54. #define IR_SF (1 << 3)
  55. #define IR_SIR (1 << 4)
  56. #define IR_MIR (1 << 5)
  57. #define IR_FIR (1 << 6)
  58. #define IR_16CRC (1 << 7)
  59. #define IR_TD (1 << 8)
  60. #define IR_RX_ALL (1 << 9)
  61. #define IR_DMA_ENABLE (1 << 10)
  62. #define IR_RX_ENABLE (1 << 11)
  63. #define IR_TX_ENABLE (1 << 12)
  64. #define IR_LOOPBACK (1 << 14)
  65. #define IR_SIR_MODE (IR_SIR | IR_DMA_ENABLE | \
  66. IR_RX_ALL | IR_RX_ENABLE | IR_SF | \
  67. IR_16CRC)
  68. /* ir_status */
  69. #define IR_RX_STATUS (1 << 9)
  70. #define IR_TX_STATUS (1 << 10)
  71. #define IR_PHYEN (1 << 15)
  72. /* ir_write_phy_config */
  73. #define IR_BR(x) (((x) & 0x3f) << 10) /* baud rate */
  74. #define IR_PW(x) (((x) & 0x1f) << 5) /* pulse width */
  75. #define IR_P(x) ((x) & 0x1f) /* preamble bits */
  76. /* Config2 */
  77. #define IR_MODE_INV (1 << 0)
  78. #define IR_ONE_PIN (1 << 1)
  79. #define IR_PHYCLK_40MHZ (0 << 2)
  80. #define IR_PHYCLK_48MHZ (1 << 2)
  81. #define IR_PHYCLK_56MHZ (2 << 2)
  82. #define IR_PHYCLK_64MHZ (3 << 2)
  83. #define IR_DP (1 << 4)
  84. #define IR_DA (1 << 5)
  85. #define IR_FLT_HIGH (0 << 6)
  86. #define IR_FLT_MEDHI (1 << 6)
  87. #define IR_FLT_MEDLO (2 << 6)
  88. #define IR_FLT_LO (3 << 6)
  89. #define IR_IEN (1 << 8)
  90. /* ir_enable */
  91. #define IR_HC (1 << 3) /* divide SBUS clock by 2 */
  92. #define IR_CE (1 << 2) /* clock enable */
  93. #define IR_C (1 << 1) /* coherency bit */
  94. #define IR_BE (1 << 0) /* set in big endian mode */
  95. #define NUM_IR_DESC 64
  96. #define RING_SIZE_4 0x0
  97. #define RING_SIZE_16 0x3
  98. #define RING_SIZE_64 0xF
  99. #define MAX_NUM_IR_DESC 64
  100. #define MAX_BUF_SIZE 2048
  101. /* Ring descriptor flags */
  102. #define AU_OWN (1 << 7) /* tx,rx */
  103. #define IR_DIS_CRC (1 << 6) /* tx */
  104. #define IR_BAD_CRC (1 << 5) /* tx */
  105. #define IR_NEED_PULSE (1 << 4) /* tx */
  106. #define IR_FORCE_UNDER (1 << 3) /* tx */
  107. #define IR_DISABLE_TX (1 << 2) /* tx */
  108. #define IR_HW_UNDER (1 << 0) /* tx */
  109. #define IR_TX_ERROR (IR_DIS_CRC | IR_BAD_CRC | IR_HW_UNDER)
  110. #define IR_PHY_ERROR (1 << 6) /* rx */
  111. #define IR_CRC_ERROR (1 << 5) /* rx */
  112. #define IR_MAX_LEN (1 << 4) /* rx */
  113. #define IR_FIFO_OVER (1 << 3) /* rx */
  114. #define IR_SIR_ERROR (1 << 2) /* rx */
  115. #define IR_RX_ERROR (IR_PHY_ERROR | IR_CRC_ERROR | \
  116. IR_MAX_LEN | IR_FIFO_OVER | IR_SIR_ERROR)
  117. struct db_dest {
  118. struct db_dest *pnext;
  119. volatile u32 *vaddr;
  120. dma_addr_t dma_addr;
  121. };
  122. struct ring_dest {
  123. u8 count_0; /* 7:0 */
  124. u8 count_1; /* 12:8 */
  125. u8 reserved;
  126. u8 flags;
  127. u8 addr_0; /* 7:0 */
  128. u8 addr_1; /* 15:8 */
  129. u8 addr_2; /* 23:16 */
  130. u8 addr_3; /* 31:24 */
  131. };
  132. /* Private data for each instance */
  133. struct au1k_private {
  134. void __iomem *iobase;
  135. int irq_rx, irq_tx;
  136. struct db_dest *pDBfree;
  137. struct db_dest db[2 * NUM_IR_DESC];
  138. volatile struct ring_dest *rx_ring[NUM_IR_DESC];
  139. volatile struct ring_dest *tx_ring[NUM_IR_DESC];
  140. struct db_dest *rx_db_inuse[NUM_IR_DESC];
  141. struct db_dest *tx_db_inuse[NUM_IR_DESC];
  142. u32 rx_head;
  143. u32 tx_head;
  144. u32 tx_tail;
  145. u32 tx_full;
  146. iobuff_t rx_buff;
  147. struct net_device *netdev;
  148. struct timeval stamp;
  149. struct timeval now;
  150. struct qos_info qos;
  151. struct irlap_cb *irlap;
  152. u8 open;
  153. u32 speed;
  154. u32 newspeed;
  155. struct timer_list timer;
  156. struct resource *ioarea;
  157. struct au1k_irda_platform_data *platdata;
  158. };
  159. static int qos_mtt_bits = 0x07; /* 1 ms or more */
  160. #define RUN_AT(x) (jiffies + (x))
  161. static void au1k_irda_plat_set_phy_mode(struct au1k_private *p, int mode)
  162. {
  163. if (p->platdata && p->platdata->set_phy_mode)
  164. p->platdata->set_phy_mode(mode);
  165. }
  166. static inline unsigned long irda_read(struct au1k_private *p,
  167. unsigned long ofs)
  168. {
  169. /*
  170. * IrDA peripheral bug. You have to read the register
  171. * twice to get the right value.
  172. */
  173. (void)__raw_readl(p->iobase + ofs);
  174. return __raw_readl(p->iobase + ofs);
  175. }
  176. static inline void irda_write(struct au1k_private *p, unsigned long ofs,
  177. unsigned long val)
  178. {
  179. __raw_writel(val, p->iobase + ofs);
  180. wmb();
  181. }
  182. /*
  183. * Buffer allocation/deallocation routines. The buffer descriptor returned
  184. * has the virtual and dma address of a buffer suitable for
  185. * both, receive and transmit operations.
  186. */
  187. static struct db_dest *GetFreeDB(struct au1k_private *aup)
  188. {
  189. struct db_dest *db;
  190. db = aup->pDBfree;
  191. if (db)
  192. aup->pDBfree = db->pnext;
  193. return db;
  194. }
  195. /*
  196. DMA memory allocation, derived from pci_alloc_consistent.
  197. However, the Au1000 data cache is coherent (when programmed
  198. so), therefore we return KSEG0 address, not KSEG1.
  199. */
  200. static void *dma_alloc(size_t size, dma_addr_t *dma_handle)
  201. {
  202. void *ret;
  203. int gfp = GFP_ATOMIC | GFP_DMA;
  204. ret = (void *)__get_free_pages(gfp, get_order(size));
  205. if (ret != NULL) {
  206. memset(ret, 0, size);
  207. *dma_handle = virt_to_bus(ret);
  208. ret = (void *)KSEG0ADDR(ret);
  209. }
  210. return ret;
  211. }
  212. static void dma_free(void *vaddr, size_t size)
  213. {
  214. vaddr = (void *)KSEG0ADDR(vaddr);
  215. free_pages((unsigned long) vaddr, get_order(size));
  216. }
  217. static void setup_hw_rings(struct au1k_private *aup, u32 rx_base, u32 tx_base)
  218. {
  219. int i;
  220. for (i = 0; i < NUM_IR_DESC; i++) {
  221. aup->rx_ring[i] = (volatile struct ring_dest *)
  222. (rx_base + sizeof(struct ring_dest) * i);
  223. }
  224. for (i = 0; i < NUM_IR_DESC; i++) {
  225. aup->tx_ring[i] = (volatile struct ring_dest *)
  226. (tx_base + sizeof(struct ring_dest) * i);
  227. }
  228. }
  229. static int au1k_irda_init_iobuf(iobuff_t *io, int size)
  230. {
  231. io->head = kmalloc(size, GFP_KERNEL);
  232. if (io->head != NULL) {
  233. io->truesize = size;
  234. io->in_frame = FALSE;
  235. io->state = OUTSIDE_FRAME;
  236. io->data = io->head;
  237. }
  238. return io->head ? 0 : -ENOMEM;
  239. }
  240. /*
  241. * Set the IrDA communications speed.
  242. */
  243. static int au1k_irda_set_speed(struct net_device *dev, int speed)
  244. {
  245. struct au1k_private *aup = netdev_priv(dev);
  246. volatile struct ring_dest *ptxd;
  247. unsigned long control;
  248. int ret = 0, timeout = 10, i;
  249. if (speed == aup->speed)
  250. return ret;
  251. /* disable PHY first */
  252. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_OFF);
  253. irda_write(aup, IR_STATUS, irda_read(aup, IR_STATUS) & ~IR_PHYEN);
  254. /* disable RX/TX */
  255. irda_write(aup, IR_CONFIG_1,
  256. irda_read(aup, IR_CONFIG_1) & ~(IR_RX_ENABLE | IR_TX_ENABLE));
  257. msleep(20);
  258. while (irda_read(aup, IR_STATUS) & (IR_RX_STATUS | IR_TX_STATUS)) {
  259. msleep(20);
  260. if (!timeout--) {
  261. printk(KERN_ERR "%s: rx/tx disable timeout\n",
  262. dev->name);
  263. break;
  264. }
  265. }
  266. /* disable DMA */
  267. irda_write(aup, IR_CONFIG_1,
  268. irda_read(aup, IR_CONFIG_1) & ~IR_DMA_ENABLE);
  269. msleep(20);
  270. /* After we disable tx/rx. the index pointers go back to zero. */
  271. aup->tx_head = aup->tx_tail = aup->rx_head = 0;
  272. for (i = 0; i < NUM_IR_DESC; i++) {
  273. ptxd = aup->tx_ring[i];
  274. ptxd->flags = 0;
  275. ptxd->count_0 = 0;
  276. ptxd->count_1 = 0;
  277. }
  278. for (i = 0; i < NUM_IR_DESC; i++) {
  279. ptxd = aup->rx_ring[i];
  280. ptxd->count_0 = 0;
  281. ptxd->count_1 = 0;
  282. ptxd->flags = AU_OWN;
  283. }
  284. if (speed == 4000000)
  285. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_FIR);
  286. else
  287. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_SIR);
  288. switch (speed) {
  289. case 9600:
  290. irda_write(aup, IR_WRITE_PHY_CONFIG, IR_BR(11) | IR_PW(12));
  291. irda_write(aup, IR_CONFIG_1, IR_SIR_MODE);
  292. break;
  293. case 19200:
  294. irda_write(aup, IR_WRITE_PHY_CONFIG, IR_BR(5) | IR_PW(12));
  295. irda_write(aup, IR_CONFIG_1, IR_SIR_MODE);
  296. break;
  297. case 38400:
  298. irda_write(aup, IR_WRITE_PHY_CONFIG, IR_BR(2) | IR_PW(12));
  299. irda_write(aup, IR_CONFIG_1, IR_SIR_MODE);
  300. break;
  301. case 57600:
  302. irda_write(aup, IR_WRITE_PHY_CONFIG, IR_BR(1) | IR_PW(12));
  303. irda_write(aup, IR_CONFIG_1, IR_SIR_MODE);
  304. break;
  305. case 115200:
  306. irda_write(aup, IR_WRITE_PHY_CONFIG, IR_PW(12));
  307. irda_write(aup, IR_CONFIG_1, IR_SIR_MODE);
  308. break;
  309. case 4000000:
  310. irda_write(aup, IR_WRITE_PHY_CONFIG, IR_P(15));
  311. irda_write(aup, IR_CONFIG_1, IR_FIR | IR_DMA_ENABLE |
  312. IR_RX_ENABLE);
  313. break;
  314. default:
  315. printk(KERN_ERR "%s unsupported speed %x\n", dev->name, speed);
  316. ret = -EINVAL;
  317. break;
  318. }
  319. aup->speed = speed;
  320. irda_write(aup, IR_STATUS, irda_read(aup, IR_STATUS) | IR_PHYEN);
  321. control = irda_read(aup, IR_STATUS);
  322. irda_write(aup, IR_RING_PROMPT, 0);
  323. if (control & (1 << 14)) {
  324. printk(KERN_ERR "%s: configuration error\n", dev->name);
  325. } else {
  326. if (control & (1 << 11))
  327. printk(KERN_DEBUG "%s Valid SIR config\n", dev->name);
  328. if (control & (1 << 12))
  329. printk(KERN_DEBUG "%s Valid MIR config\n", dev->name);
  330. if (control & (1 << 13))
  331. printk(KERN_DEBUG "%s Valid FIR config\n", dev->name);
  332. if (control & (1 << 10))
  333. printk(KERN_DEBUG "%s TX enabled\n", dev->name);
  334. if (control & (1 << 9))
  335. printk(KERN_DEBUG "%s RX enabled\n", dev->name);
  336. }
  337. return ret;
  338. }
  339. static void update_rx_stats(struct net_device *dev, u32 status, u32 count)
  340. {
  341. struct net_device_stats *ps = &dev->stats;
  342. ps->rx_packets++;
  343. if (status & IR_RX_ERROR) {
  344. ps->rx_errors++;
  345. if (status & (IR_PHY_ERROR | IR_FIFO_OVER))
  346. ps->rx_missed_errors++;
  347. if (status & IR_MAX_LEN)
  348. ps->rx_length_errors++;
  349. if (status & IR_CRC_ERROR)
  350. ps->rx_crc_errors++;
  351. } else
  352. ps->rx_bytes += count;
  353. }
  354. static void update_tx_stats(struct net_device *dev, u32 status, u32 pkt_len)
  355. {
  356. struct net_device_stats *ps = &dev->stats;
  357. ps->tx_packets++;
  358. ps->tx_bytes += pkt_len;
  359. if (status & IR_TX_ERROR) {
  360. ps->tx_errors++;
  361. ps->tx_aborted_errors++;
  362. }
  363. }
  364. static void au1k_tx_ack(struct net_device *dev)
  365. {
  366. struct au1k_private *aup = netdev_priv(dev);
  367. volatile struct ring_dest *ptxd;
  368. ptxd = aup->tx_ring[aup->tx_tail];
  369. while (!(ptxd->flags & AU_OWN) && (aup->tx_tail != aup->tx_head)) {
  370. update_tx_stats(dev, ptxd->flags,
  371. (ptxd->count_1 << 8) | ptxd->count_0);
  372. ptxd->count_0 = 0;
  373. ptxd->count_1 = 0;
  374. wmb();
  375. aup->tx_tail = (aup->tx_tail + 1) & (NUM_IR_DESC - 1);
  376. ptxd = aup->tx_ring[aup->tx_tail];
  377. if (aup->tx_full) {
  378. aup->tx_full = 0;
  379. netif_wake_queue(dev);
  380. }
  381. }
  382. if (aup->tx_tail == aup->tx_head) {
  383. if (aup->newspeed) {
  384. au1k_irda_set_speed(dev, aup->newspeed);
  385. aup->newspeed = 0;
  386. } else {
  387. irda_write(aup, IR_CONFIG_1,
  388. irda_read(aup, IR_CONFIG_1) & ~IR_TX_ENABLE);
  389. irda_write(aup, IR_CONFIG_1,
  390. irda_read(aup, IR_CONFIG_1) | IR_RX_ENABLE);
  391. irda_write(aup, IR_RING_PROMPT, 0);
  392. }
  393. }
  394. }
  395. static int au1k_irda_rx(struct net_device *dev)
  396. {
  397. struct au1k_private *aup = netdev_priv(dev);
  398. volatile struct ring_dest *prxd;
  399. struct sk_buff *skb;
  400. struct db_dest *pDB;
  401. u32 flags, count;
  402. prxd = aup->rx_ring[aup->rx_head];
  403. flags = prxd->flags;
  404. while (!(flags & AU_OWN)) {
  405. pDB = aup->rx_db_inuse[aup->rx_head];
  406. count = (prxd->count_1 << 8) | prxd->count_0;
  407. if (!(flags & IR_RX_ERROR)) {
  408. /* good frame */
  409. update_rx_stats(dev, flags, count);
  410. skb = alloc_skb(count + 1, GFP_ATOMIC);
  411. if (skb == NULL) {
  412. dev->stats.rx_dropped++;
  413. continue;
  414. }
  415. skb_reserve(skb, 1);
  416. if (aup->speed == 4000000)
  417. skb_put(skb, count);
  418. else
  419. skb_put(skb, count - 2);
  420. skb_copy_to_linear_data(skb, (void *)pDB->vaddr,
  421. count - 2);
  422. skb->dev = dev;
  423. skb_reset_mac_header(skb);
  424. skb->protocol = htons(ETH_P_IRDA);
  425. netif_rx(skb);
  426. prxd->count_0 = 0;
  427. prxd->count_1 = 0;
  428. }
  429. prxd->flags |= AU_OWN;
  430. aup->rx_head = (aup->rx_head + 1) & (NUM_IR_DESC - 1);
  431. irda_write(aup, IR_RING_PROMPT, 0);
  432. /* next descriptor */
  433. prxd = aup->rx_ring[aup->rx_head];
  434. flags = prxd->flags;
  435. }
  436. return 0;
  437. }
  438. static irqreturn_t au1k_irda_interrupt(int dummy, void *dev_id)
  439. {
  440. struct net_device *dev = dev_id;
  441. struct au1k_private *aup = netdev_priv(dev);
  442. irda_write(aup, IR_INT_CLEAR, 0); /* ack irda interrupts */
  443. au1k_irda_rx(dev);
  444. au1k_tx_ack(dev);
  445. return IRQ_HANDLED;
  446. }
  447. static int au1k_init(struct net_device *dev)
  448. {
  449. struct au1k_private *aup = netdev_priv(dev);
  450. u32 enable, ring_address;
  451. int i;
  452. enable = IR_HC | IR_CE | IR_C;
  453. #ifndef CONFIG_CPU_LITTLE_ENDIAN
  454. enable |= IR_BE;
  455. #endif
  456. aup->tx_head = 0;
  457. aup->tx_tail = 0;
  458. aup->rx_head = 0;
  459. for (i = 0; i < NUM_IR_DESC; i++)
  460. aup->rx_ring[i]->flags = AU_OWN;
  461. irda_write(aup, IR_ENABLE, enable);
  462. msleep(20);
  463. /* disable PHY */
  464. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_OFF);
  465. irda_write(aup, IR_STATUS, irda_read(aup, IR_STATUS) & ~IR_PHYEN);
  466. msleep(20);
  467. irda_write(aup, IR_MAX_PKT_LEN, MAX_BUF_SIZE);
  468. ring_address = (u32)virt_to_phys((void *)aup->rx_ring[0]);
  469. irda_write(aup, IR_RING_BASE_ADDR_H, ring_address >> 26);
  470. irda_write(aup, IR_RING_BASE_ADDR_L, (ring_address >> 10) & 0xffff);
  471. irda_write(aup, IR_RING_SIZE,
  472. (RING_SIZE_64 << 8) | (RING_SIZE_64 << 12));
  473. irda_write(aup, IR_CONFIG_2, IR_PHYCLK_48MHZ | IR_ONE_PIN);
  474. irda_write(aup, IR_RING_ADDR_CMPR, 0);
  475. au1k_irda_set_speed(dev, 9600);
  476. return 0;
  477. }
  478. static int au1k_irda_start(struct net_device *dev)
  479. {
  480. struct au1k_private *aup = netdev_priv(dev);
  481. char hwname[32];
  482. int retval;
  483. retval = au1k_init(dev);
  484. if (retval) {
  485. printk(KERN_ERR "%s: error in au1k_init\n", dev->name);
  486. return retval;
  487. }
  488. retval = request_irq(aup->irq_tx, &au1k_irda_interrupt, 0,
  489. dev->name, dev);
  490. if (retval) {
  491. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  492. dev->name, dev->irq);
  493. return retval;
  494. }
  495. retval = request_irq(aup->irq_rx, &au1k_irda_interrupt, 0,
  496. dev->name, dev);
  497. if (retval) {
  498. free_irq(aup->irq_tx, dev);
  499. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  500. dev->name, dev->irq);
  501. return retval;
  502. }
  503. /* Give self a hardware name */
  504. sprintf(hwname, "Au1000 SIR/FIR");
  505. aup->irlap = irlap_open(dev, &aup->qos, hwname);
  506. netif_start_queue(dev);
  507. /* int enable */
  508. irda_write(aup, IR_CONFIG_2, irda_read(aup, IR_CONFIG_2) | IR_IEN);
  509. /* power up */
  510. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_SIR);
  511. aup->timer.expires = RUN_AT((3 * HZ));
  512. aup->timer.data = (unsigned long)dev;
  513. return 0;
  514. }
  515. static int au1k_irda_stop(struct net_device *dev)
  516. {
  517. struct au1k_private *aup = netdev_priv(dev);
  518. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_OFF);
  519. /* disable interrupts */
  520. irda_write(aup, IR_CONFIG_2, irda_read(aup, IR_CONFIG_2) & ~IR_IEN);
  521. irda_write(aup, IR_CONFIG_1, 0);
  522. irda_write(aup, IR_ENABLE, 0); /* disable clock */
  523. if (aup->irlap) {
  524. irlap_close(aup->irlap);
  525. aup->irlap = NULL;
  526. }
  527. netif_stop_queue(dev);
  528. del_timer(&aup->timer);
  529. /* disable the interrupt */
  530. free_irq(aup->irq_tx, dev);
  531. free_irq(aup->irq_rx, dev);
  532. return 0;
  533. }
  534. /*
  535. * Au1000 transmit routine.
  536. */
  537. static int au1k_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  538. {
  539. struct au1k_private *aup = netdev_priv(dev);
  540. int speed = irda_get_next_speed(skb);
  541. volatile struct ring_dest *ptxd;
  542. struct db_dest *pDB;
  543. u32 len, flags;
  544. if (speed != aup->speed && speed != -1)
  545. aup->newspeed = speed;
  546. if ((skb->len == 0) && (aup->newspeed)) {
  547. if (aup->tx_tail == aup->tx_head) {
  548. au1k_irda_set_speed(dev, speed);
  549. aup->newspeed = 0;
  550. }
  551. dev_kfree_skb(skb);
  552. return NETDEV_TX_OK;
  553. }
  554. ptxd = aup->tx_ring[aup->tx_head];
  555. flags = ptxd->flags;
  556. if (flags & AU_OWN) {
  557. printk(KERN_DEBUG "%s: tx_full\n", dev->name);
  558. netif_stop_queue(dev);
  559. aup->tx_full = 1;
  560. return 1;
  561. } else if (((aup->tx_head + 1) & (NUM_IR_DESC - 1)) == aup->tx_tail) {
  562. printk(KERN_DEBUG "%s: tx_full\n", dev->name);
  563. netif_stop_queue(dev);
  564. aup->tx_full = 1;
  565. return 1;
  566. }
  567. pDB = aup->tx_db_inuse[aup->tx_head];
  568. #if 0
  569. if (irda_read(aup, IR_RX_BYTE_CNT) != 0) {
  570. printk(KERN_DEBUG "tx warning: rx byte cnt %x\n",
  571. irda_read(aup, IR_RX_BYTE_CNT));
  572. }
  573. #endif
  574. if (aup->speed == 4000000) {
  575. /* FIR */
  576. skb_copy_from_linear_data(skb, (void *)pDB->vaddr, skb->len);
  577. ptxd->count_0 = skb->len & 0xff;
  578. ptxd->count_1 = (skb->len >> 8) & 0xff;
  579. } else {
  580. /* SIR */
  581. len = async_wrap_skb(skb, (u8 *)pDB->vaddr, MAX_BUF_SIZE);
  582. ptxd->count_0 = len & 0xff;
  583. ptxd->count_1 = (len >> 8) & 0xff;
  584. ptxd->flags |= IR_DIS_CRC;
  585. }
  586. ptxd->flags |= AU_OWN;
  587. wmb();
  588. irda_write(aup, IR_CONFIG_1,
  589. irda_read(aup, IR_CONFIG_1) | IR_TX_ENABLE);
  590. irda_write(aup, IR_RING_PROMPT, 0);
  591. dev_kfree_skb(skb);
  592. aup->tx_head = (aup->tx_head + 1) & (NUM_IR_DESC - 1);
  593. return NETDEV_TX_OK;
  594. }
  595. /*
  596. * The Tx ring has been full longer than the watchdog timeout
  597. * value. The transmitter must be hung?
  598. */
  599. static void au1k_tx_timeout(struct net_device *dev)
  600. {
  601. u32 speed;
  602. struct au1k_private *aup = netdev_priv(dev);
  603. printk(KERN_ERR "%s: tx timeout\n", dev->name);
  604. speed = aup->speed;
  605. aup->speed = 0;
  606. au1k_irda_set_speed(dev, speed);
  607. aup->tx_full = 0;
  608. netif_wake_queue(dev);
  609. }
  610. static int au1k_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
  611. {
  612. struct if_irda_req *rq = (struct if_irda_req *)ifreq;
  613. struct au1k_private *aup = netdev_priv(dev);
  614. int ret = -EOPNOTSUPP;
  615. switch (cmd) {
  616. case SIOCSBANDWIDTH:
  617. if (capable(CAP_NET_ADMIN)) {
  618. /*
  619. * We are unable to set the speed if the
  620. * device is not running.
  621. */
  622. if (aup->open)
  623. ret = au1k_irda_set_speed(dev,
  624. rq->ifr_baudrate);
  625. else {
  626. printk(KERN_ERR "%s ioctl: !netif_running\n",
  627. dev->name);
  628. ret = 0;
  629. }
  630. }
  631. break;
  632. case SIOCSMEDIABUSY:
  633. ret = -EPERM;
  634. if (capable(CAP_NET_ADMIN)) {
  635. irda_device_set_media_busy(dev, TRUE);
  636. ret = 0;
  637. }
  638. break;
  639. case SIOCGRECEIVING:
  640. rq->ifr_receiving = 0;
  641. break;
  642. default:
  643. break;
  644. }
  645. return ret;
  646. }
  647. static const struct net_device_ops au1k_irda_netdev_ops = {
  648. .ndo_open = au1k_irda_start,
  649. .ndo_stop = au1k_irda_stop,
  650. .ndo_start_xmit = au1k_irda_hard_xmit,
  651. .ndo_tx_timeout = au1k_tx_timeout,
  652. .ndo_do_ioctl = au1k_irda_ioctl,
  653. };
  654. static int au1k_irda_net_init(struct net_device *dev)
  655. {
  656. struct au1k_private *aup = netdev_priv(dev);
  657. struct db_dest *pDB, *pDBfree;
  658. int i, err, retval = 0;
  659. dma_addr_t temp;
  660. err = au1k_irda_init_iobuf(&aup->rx_buff, 14384);
  661. if (err)
  662. goto out1;
  663. dev->netdev_ops = &au1k_irda_netdev_ops;
  664. irda_init_max_qos_capabilies(&aup->qos);
  665. /* The only value we must override it the baudrate */
  666. aup->qos.baud_rate.bits = IR_9600 | IR_19200 | IR_38400 |
  667. IR_57600 | IR_115200 | IR_576000 | (IR_4000000 << 8);
  668. aup->qos.min_turn_time.bits = qos_mtt_bits;
  669. irda_qos_bits_to_value(&aup->qos);
  670. retval = -ENOMEM;
  671. /* Tx ring follows rx ring + 512 bytes */
  672. /* we need a 1k aligned buffer */
  673. aup->rx_ring[0] = (struct ring_dest *)
  674. dma_alloc(2 * MAX_NUM_IR_DESC * (sizeof(struct ring_dest)),
  675. &temp);
  676. if (!aup->rx_ring[0])
  677. goto out2;
  678. /* allocate the data buffers */
  679. aup->db[0].vaddr =
  680. dma_alloc(MAX_BUF_SIZE * 2 * NUM_IR_DESC, &temp);
  681. if (!aup->db[0].vaddr)
  682. goto out3;
  683. setup_hw_rings(aup, (u32)aup->rx_ring[0], (u32)aup->rx_ring[0] + 512);
  684. pDBfree = NULL;
  685. pDB = aup->db;
  686. for (i = 0; i < (2 * NUM_IR_DESC); i++) {
  687. pDB->pnext = pDBfree;
  688. pDBfree = pDB;
  689. pDB->vaddr =
  690. (u32 *)((unsigned)aup->db[0].vaddr + (MAX_BUF_SIZE * i));
  691. pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
  692. pDB++;
  693. }
  694. aup->pDBfree = pDBfree;
  695. /* attach a data buffer to each descriptor */
  696. for (i = 0; i < NUM_IR_DESC; i++) {
  697. pDB = GetFreeDB(aup);
  698. if (!pDB)
  699. goto out3;
  700. aup->rx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
  701. aup->rx_ring[i]->addr_1 = (u8)((pDB->dma_addr >> 8) & 0xff);
  702. aup->rx_ring[i]->addr_2 = (u8)((pDB->dma_addr >> 16) & 0xff);
  703. aup->rx_ring[i]->addr_3 = (u8)((pDB->dma_addr >> 24) & 0xff);
  704. aup->rx_db_inuse[i] = pDB;
  705. }
  706. for (i = 0; i < NUM_IR_DESC; i++) {
  707. pDB = GetFreeDB(aup);
  708. if (!pDB)
  709. goto out3;
  710. aup->tx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
  711. aup->tx_ring[i]->addr_1 = (u8)((pDB->dma_addr >> 8) & 0xff);
  712. aup->tx_ring[i]->addr_2 = (u8)((pDB->dma_addr >> 16) & 0xff);
  713. aup->tx_ring[i]->addr_3 = (u8)((pDB->dma_addr >> 24) & 0xff);
  714. aup->tx_ring[i]->count_0 = 0;
  715. aup->tx_ring[i]->count_1 = 0;
  716. aup->tx_ring[i]->flags = 0;
  717. aup->tx_db_inuse[i] = pDB;
  718. }
  719. return 0;
  720. out3:
  721. dma_free((void *)aup->rx_ring[0],
  722. 2 * MAX_NUM_IR_DESC * (sizeof(struct ring_dest)));
  723. out2:
  724. kfree(aup->rx_buff.head);
  725. out1:
  726. printk(KERN_ERR "au1k_irda_net_init() failed. Returns %d\n", retval);
  727. return retval;
  728. }
  729. static int au1k_irda_probe(struct platform_device *pdev)
  730. {
  731. struct au1k_private *aup;
  732. struct net_device *dev;
  733. struct resource *r;
  734. int err;
  735. dev = alloc_irdadev(sizeof(struct au1k_private));
  736. if (!dev)
  737. return -ENOMEM;
  738. aup = netdev_priv(dev);
  739. aup->platdata = pdev->dev.platform_data;
  740. err = -EINVAL;
  741. r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  742. if (!r)
  743. goto out;
  744. aup->irq_tx = r->start;
  745. r = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  746. if (!r)
  747. goto out;
  748. aup->irq_rx = r->start;
  749. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  750. if (!r)
  751. goto out;
  752. err = -EBUSY;
  753. aup->ioarea = request_mem_region(r->start, resource_size(r),
  754. pdev->name);
  755. if (!aup->ioarea)
  756. goto out;
  757. aup->iobase = ioremap_nocache(r->start, resource_size(r));
  758. if (!aup->iobase)
  759. goto out2;
  760. dev->irq = aup->irq_rx;
  761. err = au1k_irda_net_init(dev);
  762. if (err)
  763. goto out3;
  764. err = register_netdev(dev);
  765. if (err)
  766. goto out4;
  767. platform_set_drvdata(pdev, dev);
  768. printk(KERN_INFO "IrDA: Registered device %s\n", dev->name);
  769. return 0;
  770. out4:
  771. dma_free((void *)aup->db[0].vaddr,
  772. MAX_BUF_SIZE * 2 * NUM_IR_DESC);
  773. dma_free((void *)aup->rx_ring[0],
  774. 2 * MAX_NUM_IR_DESC * (sizeof(struct ring_dest)));
  775. kfree(aup->rx_buff.head);
  776. out3:
  777. iounmap(aup->iobase);
  778. out2:
  779. release_resource(aup->ioarea);
  780. kfree(aup->ioarea);
  781. out:
  782. free_netdev(dev);
  783. return err;
  784. }
  785. static int au1k_irda_remove(struct platform_device *pdev)
  786. {
  787. struct net_device *dev = platform_get_drvdata(pdev);
  788. struct au1k_private *aup = netdev_priv(dev);
  789. unregister_netdev(dev);
  790. dma_free((void *)aup->db[0].vaddr,
  791. MAX_BUF_SIZE * 2 * NUM_IR_DESC);
  792. dma_free((void *)aup->rx_ring[0],
  793. 2 * MAX_NUM_IR_DESC * (sizeof(struct ring_dest)));
  794. kfree(aup->rx_buff.head);
  795. iounmap(aup->iobase);
  796. release_resource(aup->ioarea);
  797. kfree(aup->ioarea);
  798. free_netdev(dev);
  799. return 0;
  800. }
  801. static struct platform_driver au1k_irda_driver = {
  802. .driver = {
  803. .name = "au1000-irda",
  804. .owner = THIS_MODULE,
  805. },
  806. .probe = au1k_irda_probe,
  807. .remove = au1k_irda_remove,
  808. };
  809. module_platform_driver(au1k_irda_driver);
  810. MODULE_AUTHOR("Pete Popov <ppopov@mvista.com>");
  811. MODULE_DESCRIPTION("Au1000 IrDA Device Driver");