au1k_ir.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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, write to the Free Software Foundation, Inc.,
  19. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/init.h>
  24. #include <linux/errno.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/slab.h>
  28. #include <linux/rtnetlink.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/pm.h>
  31. #include <linux/bitops.h>
  32. #include <asm/irq.h>
  33. #include <asm/io.h>
  34. #include <asm/au1000.h>
  35. #if defined(CONFIG_MIPS_PB1000) || defined(CONFIG_MIPS_PB1100)
  36. #include <asm/pb1000.h>
  37. #elif defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  38. #include <asm/db1x00.h>
  39. #else
  40. #error au1k_ir: unsupported board
  41. #endif
  42. #include <net/irda/irda.h>
  43. #include <net/irda/irmod.h>
  44. #include <net/irda/wrapper.h>
  45. #include <net/irda/irda_device.h>
  46. #include "au1000_ircc.h"
  47. static int au1k_irda_net_init(struct net_device *);
  48. static int au1k_irda_start(struct net_device *);
  49. static int au1k_irda_stop(struct net_device *dev);
  50. static int au1k_irda_hard_xmit(struct sk_buff *, struct net_device *);
  51. static int au1k_irda_rx(struct net_device *);
  52. static void au1k_irda_interrupt(int, void *);
  53. static void au1k_tx_timeout(struct net_device *);
  54. static int au1k_irda_ioctl(struct net_device *, struct ifreq *, int);
  55. static int au1k_irda_set_speed(struct net_device *dev, int speed);
  56. static void *dma_alloc(size_t, dma_addr_t *);
  57. static void dma_free(void *, size_t);
  58. static int qos_mtt_bits = 0x07; /* 1 ms or more */
  59. static struct net_device *ir_devs[NUM_IR_IFF];
  60. static char version[] __devinitdata =
  61. "au1k_ircc:1.2 ppopov@mvista.com\n";
  62. #define RUN_AT(x) (jiffies + (x))
  63. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  64. static BCSR * const bcsr = (BCSR *)0xAE000000;
  65. #endif
  66. static DEFINE_SPINLOCK(ir_lock);
  67. /*
  68. * IrDA peripheral bug. You have to read the register
  69. * twice to get the right value.
  70. */
  71. u32 read_ir_reg(u32 addr)
  72. {
  73. readl(addr);
  74. return readl(addr);
  75. }
  76. /*
  77. * Buffer allocation/deallocation routines. The buffer descriptor returned
  78. * has the virtual and dma address of a buffer suitable for
  79. * both, receive and transmit operations.
  80. */
  81. static db_dest_t *GetFreeDB(struct au1k_private *aup)
  82. {
  83. db_dest_t *pDB;
  84. pDB = aup->pDBfree;
  85. if (pDB) {
  86. aup->pDBfree = pDB->pnext;
  87. }
  88. return pDB;
  89. }
  90. static void ReleaseDB(struct au1k_private *aup, db_dest_t *pDB)
  91. {
  92. db_dest_t *pDBfree = aup->pDBfree;
  93. if (pDBfree)
  94. pDBfree->pnext = pDB;
  95. aup->pDBfree = pDB;
  96. }
  97. /*
  98. DMA memory allocation, derived from pci_alloc_consistent.
  99. However, the Au1000 data cache is coherent (when programmed
  100. so), therefore we return KSEG0 address, not KSEG1.
  101. */
  102. static void *dma_alloc(size_t size, dma_addr_t * dma_handle)
  103. {
  104. void *ret;
  105. int gfp = GFP_ATOMIC | GFP_DMA;
  106. ret = (void *) __get_free_pages(gfp, get_order(size));
  107. if (ret != NULL) {
  108. memset(ret, 0, size);
  109. *dma_handle = virt_to_bus(ret);
  110. ret = (void *)KSEG0ADDR(ret);
  111. }
  112. return ret;
  113. }
  114. static void dma_free(void *vaddr, size_t size)
  115. {
  116. vaddr = (void *)KSEG0ADDR(vaddr);
  117. free_pages((unsigned long) vaddr, get_order(size));
  118. }
  119. static void
  120. setup_hw_rings(struct au1k_private *aup, u32 rx_base, u32 tx_base)
  121. {
  122. int i;
  123. for (i=0; i<NUM_IR_DESC; i++) {
  124. aup->rx_ring[i] = (volatile ring_dest_t *)
  125. (rx_base + sizeof(ring_dest_t)*i);
  126. }
  127. for (i=0; i<NUM_IR_DESC; i++) {
  128. aup->tx_ring[i] = (volatile ring_dest_t *)
  129. (tx_base + sizeof(ring_dest_t)*i);
  130. }
  131. }
  132. static int au1k_irda_init(void)
  133. {
  134. static unsigned version_printed = 0;
  135. struct au1k_private *aup;
  136. struct net_device *dev;
  137. int err;
  138. if (version_printed++ == 0) printk(version);
  139. dev = alloc_irdadev(sizeof(struct au1k_private));
  140. if (!dev)
  141. return -ENOMEM;
  142. dev->irq = AU1000_IRDA_RX_INT; /* TX has its own interrupt */
  143. err = au1k_irda_net_init(dev);
  144. if (err)
  145. goto out;
  146. err = register_netdev(dev);
  147. if (err)
  148. goto out1;
  149. ir_devs[0] = dev;
  150. printk(KERN_INFO "IrDA: Registered device %s\n", dev->name);
  151. return 0;
  152. out1:
  153. aup = netdev_priv(dev);
  154. dma_free((void *)aup->db[0].vaddr,
  155. MAX_BUF_SIZE * 2*NUM_IR_DESC);
  156. dma_free((void *)aup->rx_ring[0],
  157. 2 * MAX_NUM_IR_DESC*(sizeof(ring_dest_t)));
  158. kfree(aup->rx_buff.head);
  159. out:
  160. free_netdev(dev);
  161. return err;
  162. }
  163. static int au1k_irda_init_iobuf(iobuff_t *io, int size)
  164. {
  165. io->head = kmalloc(size, GFP_KERNEL);
  166. if (io->head != NULL) {
  167. io->truesize = size;
  168. io->in_frame = FALSE;
  169. io->state = OUTSIDE_FRAME;
  170. io->data = io->head;
  171. }
  172. return io->head ? 0 : -ENOMEM;
  173. }
  174. static const struct net_device_ops au1k_irda_netdev_ops = {
  175. .ndo_open = au1k_irda_start,
  176. .ndo_stop = au1k_irda_stop,
  177. .ndo_start_xmit = au1k_irda_hard_xmit,
  178. .ndo_tx_timeout = au1k_tx_timeout,
  179. .ndo_do_ioctl = au1k_irda_ioctl,
  180. .ndo_change_mtu = eth_change_mtu,
  181. .ndo_validate_addr = eth_validate_addr,
  182. .ndo_set_mac_address = eth_mac_addr,
  183. };
  184. static int au1k_irda_net_init(struct net_device *dev)
  185. {
  186. struct au1k_private *aup = netdev_priv(dev);
  187. int i, retval = 0, err;
  188. db_dest_t *pDB, *pDBfree;
  189. dma_addr_t temp;
  190. err = au1k_irda_init_iobuf(&aup->rx_buff, 14384);
  191. if (err)
  192. goto out1;
  193. dev->netdev_ops = &au1k_irda_netdev_ops;
  194. irda_init_max_qos_capabilies(&aup->qos);
  195. /* The only value we must override it the baudrate */
  196. aup->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
  197. IR_115200|IR_576000 |(IR_4000000 << 8);
  198. aup->qos.min_turn_time.bits = qos_mtt_bits;
  199. irda_qos_bits_to_value(&aup->qos);
  200. retval = -ENOMEM;
  201. /* Tx ring follows rx ring + 512 bytes */
  202. /* we need a 1k aligned buffer */
  203. aup->rx_ring[0] = (ring_dest_t *)
  204. dma_alloc(2*MAX_NUM_IR_DESC*(sizeof(ring_dest_t)), &temp);
  205. if (!aup->rx_ring[0])
  206. goto out2;
  207. /* allocate the data buffers */
  208. aup->db[0].vaddr =
  209. (void *)dma_alloc(MAX_BUF_SIZE * 2*NUM_IR_DESC, &temp);
  210. if (!aup->db[0].vaddr)
  211. goto out3;
  212. setup_hw_rings(aup, (u32)aup->rx_ring[0], (u32)aup->rx_ring[0] + 512);
  213. pDBfree = NULL;
  214. pDB = aup->db;
  215. for (i=0; i<(2*NUM_IR_DESC); i++) {
  216. pDB->pnext = pDBfree;
  217. pDBfree = pDB;
  218. pDB->vaddr =
  219. (u32 *)((unsigned)aup->db[0].vaddr + MAX_BUF_SIZE*i);
  220. pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
  221. pDB++;
  222. }
  223. aup->pDBfree = pDBfree;
  224. /* attach a data buffer to each descriptor */
  225. for (i=0; i<NUM_IR_DESC; i++) {
  226. pDB = GetFreeDB(aup);
  227. if (!pDB) goto out;
  228. aup->rx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
  229. aup->rx_ring[i]->addr_1 = (u8)((pDB->dma_addr>>8) & 0xff);
  230. aup->rx_ring[i]->addr_2 = (u8)((pDB->dma_addr>>16) & 0xff);
  231. aup->rx_ring[i]->addr_3 = (u8)((pDB->dma_addr>>24) & 0xff);
  232. aup->rx_db_inuse[i] = pDB;
  233. }
  234. for (i=0; i<NUM_IR_DESC; i++) {
  235. pDB = GetFreeDB(aup);
  236. if (!pDB) goto out;
  237. aup->tx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
  238. aup->tx_ring[i]->addr_1 = (u8)((pDB->dma_addr>>8) & 0xff);
  239. aup->tx_ring[i]->addr_2 = (u8)((pDB->dma_addr>>16) & 0xff);
  240. aup->tx_ring[i]->addr_3 = (u8)((pDB->dma_addr>>24) & 0xff);
  241. aup->tx_ring[i]->count_0 = 0;
  242. aup->tx_ring[i]->count_1 = 0;
  243. aup->tx_ring[i]->flags = 0;
  244. aup->tx_db_inuse[i] = pDB;
  245. }
  246. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  247. /* power on */
  248. bcsr->resets &= ~BCSR_RESETS_IRDA_MODE_MASK;
  249. bcsr->resets |= BCSR_RESETS_IRDA_MODE_FULL;
  250. au_sync();
  251. #endif
  252. return 0;
  253. out3:
  254. dma_free((void *)aup->rx_ring[0],
  255. 2 * MAX_NUM_IR_DESC*(sizeof(ring_dest_t)));
  256. out2:
  257. kfree(aup->rx_buff.head);
  258. out1:
  259. printk(KERN_ERR "au1k_init_module failed. Returns %d\n", retval);
  260. return retval;
  261. }
  262. static int au1k_init(struct net_device *dev)
  263. {
  264. struct au1k_private *aup = netdev_priv(dev);
  265. int i;
  266. u32 control;
  267. u32 ring_address;
  268. /* bring the device out of reset */
  269. control = 0xe; /* coherent, clock enable, one half system clock */
  270. #ifndef CONFIG_CPU_LITTLE_ENDIAN
  271. control |= 1;
  272. #endif
  273. aup->tx_head = 0;
  274. aup->tx_tail = 0;
  275. aup->rx_head = 0;
  276. for (i=0; i<NUM_IR_DESC; i++) {
  277. aup->rx_ring[i]->flags = AU_OWN;
  278. }
  279. writel(control, IR_INTERFACE_CONFIG);
  280. au_sync_delay(10);
  281. writel(read_ir_reg(IR_ENABLE) & ~0x8000, IR_ENABLE); /* disable PHY */
  282. au_sync_delay(1);
  283. writel(MAX_BUF_SIZE, IR_MAX_PKT_LEN);
  284. ring_address = (u32)virt_to_phys((void *)aup->rx_ring[0]);
  285. writel(ring_address >> 26, IR_RING_BASE_ADDR_H);
  286. writel((ring_address >> 10) & 0xffff, IR_RING_BASE_ADDR_L);
  287. writel(RING_SIZE_64<<8 | RING_SIZE_64<<12, IR_RING_SIZE);
  288. writel(1<<2 | IR_ONE_PIN, IR_CONFIG_2); /* 48MHz */
  289. writel(0, IR_RING_ADDR_CMPR);
  290. au1k_irda_set_speed(dev, 9600);
  291. return 0;
  292. }
  293. static int au1k_irda_start(struct net_device *dev)
  294. {
  295. int retval;
  296. char hwname[32];
  297. struct au1k_private *aup = netdev_priv(dev);
  298. if ((retval = au1k_init(dev))) {
  299. printk(KERN_ERR "%s: error in au1k_init\n", dev->name);
  300. return retval;
  301. }
  302. if ((retval = request_irq(AU1000_IRDA_TX_INT, &au1k_irda_interrupt,
  303. 0, dev->name, dev))) {
  304. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  305. dev->name, dev->irq);
  306. return retval;
  307. }
  308. if ((retval = request_irq(AU1000_IRDA_RX_INT, &au1k_irda_interrupt,
  309. 0, dev->name, dev))) {
  310. free_irq(AU1000_IRDA_TX_INT, dev);
  311. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  312. dev->name, dev->irq);
  313. return retval;
  314. }
  315. /* Give self a hardware name */
  316. sprintf(hwname, "Au1000 SIR/FIR");
  317. aup->irlap = irlap_open(dev, &aup->qos, hwname);
  318. netif_start_queue(dev);
  319. writel(read_ir_reg(IR_CONFIG_2) | 1<<8, IR_CONFIG_2); /* int enable */
  320. aup->timer.expires = RUN_AT((3*HZ));
  321. aup->timer.data = (unsigned long)dev;
  322. return 0;
  323. }
  324. static int au1k_irda_stop(struct net_device *dev)
  325. {
  326. struct au1k_private *aup = netdev_priv(dev);
  327. /* disable interrupts */
  328. writel(read_ir_reg(IR_CONFIG_2) & ~(1<<8), IR_CONFIG_2);
  329. writel(0, IR_CONFIG_1);
  330. writel(0, IR_INTERFACE_CONFIG); /* disable clock */
  331. au_sync();
  332. if (aup->irlap) {
  333. irlap_close(aup->irlap);
  334. aup->irlap = NULL;
  335. }
  336. netif_stop_queue(dev);
  337. del_timer(&aup->timer);
  338. /* disable the interrupt */
  339. free_irq(AU1000_IRDA_TX_INT, dev);
  340. free_irq(AU1000_IRDA_RX_INT, dev);
  341. return 0;
  342. }
  343. static void __exit au1k_irda_exit(void)
  344. {
  345. struct net_device *dev = ir_devs[0];
  346. struct au1k_private *aup = netdev_priv(dev);
  347. unregister_netdev(dev);
  348. dma_free((void *)aup->db[0].vaddr,
  349. MAX_BUF_SIZE * 2*NUM_IR_DESC);
  350. dma_free((void *)aup->rx_ring[0],
  351. 2 * MAX_NUM_IR_DESC*(sizeof(ring_dest_t)));
  352. kfree(aup->rx_buff.head);
  353. free_netdev(dev);
  354. }
  355. static inline void
  356. update_tx_stats(struct net_device *dev, u32 status, u32 pkt_len)
  357. {
  358. struct au1k_private *aup = netdev_priv(dev);
  359. struct net_device_stats *ps = &aup->stats;
  360. ps->tx_packets++;
  361. ps->tx_bytes += pkt_len;
  362. if (status & IR_TX_ERROR) {
  363. ps->tx_errors++;
  364. ps->tx_aborted_errors++;
  365. }
  366. }
  367. static void au1k_tx_ack(struct net_device *dev)
  368. {
  369. struct au1k_private *aup = netdev_priv(dev);
  370. volatile ring_dest_t *ptxd;
  371. ptxd = aup->tx_ring[aup->tx_tail];
  372. while (!(ptxd->flags & AU_OWN) && (aup->tx_tail != aup->tx_head)) {
  373. update_tx_stats(dev, ptxd->flags,
  374. ptxd->count_1<<8 | ptxd->count_0);
  375. ptxd->count_0 = 0;
  376. ptxd->count_1 = 0;
  377. au_sync();
  378. aup->tx_tail = (aup->tx_tail + 1) & (NUM_IR_DESC - 1);
  379. ptxd = aup->tx_ring[aup->tx_tail];
  380. if (aup->tx_full) {
  381. aup->tx_full = 0;
  382. netif_wake_queue(dev);
  383. }
  384. }
  385. if (aup->tx_tail == aup->tx_head) {
  386. if (aup->newspeed) {
  387. au1k_irda_set_speed(dev, aup->newspeed);
  388. aup->newspeed = 0;
  389. }
  390. else {
  391. writel(read_ir_reg(IR_CONFIG_1) & ~IR_TX_ENABLE,
  392. IR_CONFIG_1);
  393. au_sync();
  394. writel(read_ir_reg(IR_CONFIG_1) | IR_RX_ENABLE,
  395. IR_CONFIG_1);
  396. writel(0, IR_RING_PROMPT);
  397. au_sync();
  398. }
  399. }
  400. }
  401. /*
  402. * Au1000 transmit routine.
  403. */
  404. static int au1k_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  405. {
  406. struct au1k_private *aup = netdev_priv(dev);
  407. int speed = irda_get_next_speed(skb);
  408. volatile ring_dest_t *ptxd;
  409. u32 len;
  410. u32 flags;
  411. db_dest_t *pDB;
  412. if (speed != aup->speed && speed != -1) {
  413. aup->newspeed = speed;
  414. }
  415. if ((skb->len == 0) && (aup->newspeed)) {
  416. if (aup->tx_tail == aup->tx_head) {
  417. au1k_irda_set_speed(dev, speed);
  418. aup->newspeed = 0;
  419. }
  420. dev_kfree_skb(skb);
  421. return 0;
  422. }
  423. ptxd = aup->tx_ring[aup->tx_head];
  424. flags = ptxd->flags;
  425. if (flags & AU_OWN) {
  426. printk(KERN_DEBUG "%s: tx_full\n", dev->name);
  427. netif_stop_queue(dev);
  428. aup->tx_full = 1;
  429. return NETDEV_TX_BUSY;
  430. }
  431. else if (((aup->tx_head + 1) & (NUM_IR_DESC - 1)) == aup->tx_tail) {
  432. printk(KERN_DEBUG "%s: tx_full\n", dev->name);
  433. netif_stop_queue(dev);
  434. aup->tx_full = 1;
  435. return NETDEV_TX_BUSY;
  436. }
  437. pDB = aup->tx_db_inuse[aup->tx_head];
  438. #if 0
  439. if (read_ir_reg(IR_RX_BYTE_CNT) != 0) {
  440. printk("tx warning: rx byte cnt %x\n",
  441. read_ir_reg(IR_RX_BYTE_CNT));
  442. }
  443. #endif
  444. if (aup->speed == 4000000) {
  445. /* FIR */
  446. skb_copy_from_linear_data(skb, pDB->vaddr, skb->len);
  447. ptxd->count_0 = skb->len & 0xff;
  448. ptxd->count_1 = (skb->len >> 8) & 0xff;
  449. }
  450. else {
  451. /* SIR */
  452. len = async_wrap_skb(skb, (u8 *)pDB->vaddr, MAX_BUF_SIZE);
  453. ptxd->count_0 = len & 0xff;
  454. ptxd->count_1 = (len >> 8) & 0xff;
  455. ptxd->flags |= IR_DIS_CRC;
  456. au_writel(au_readl(0xae00000c) & ~(1<<13), 0xae00000c);
  457. }
  458. ptxd->flags |= AU_OWN;
  459. au_sync();
  460. writel(read_ir_reg(IR_CONFIG_1) | IR_TX_ENABLE, IR_CONFIG_1);
  461. writel(0, IR_RING_PROMPT);
  462. au_sync();
  463. dev_kfree_skb(skb);
  464. aup->tx_head = (aup->tx_head + 1) & (NUM_IR_DESC - 1);
  465. dev->trans_start = jiffies;
  466. return 0;
  467. }
  468. static inline void
  469. update_rx_stats(struct net_device *dev, u32 status, u32 count)
  470. {
  471. struct au1k_private *aup = netdev_priv(dev);
  472. struct net_device_stats *ps = &aup->stats;
  473. ps->rx_packets++;
  474. if (status & IR_RX_ERROR) {
  475. ps->rx_errors++;
  476. if (status & (IR_PHY_ERROR|IR_FIFO_OVER))
  477. ps->rx_missed_errors++;
  478. if (status & IR_MAX_LEN)
  479. ps->rx_length_errors++;
  480. if (status & IR_CRC_ERROR)
  481. ps->rx_crc_errors++;
  482. }
  483. else
  484. ps->rx_bytes += count;
  485. }
  486. /*
  487. * Au1000 receive routine.
  488. */
  489. static int au1k_irda_rx(struct net_device *dev)
  490. {
  491. struct au1k_private *aup = netdev_priv(dev);
  492. struct sk_buff *skb;
  493. volatile ring_dest_t *prxd;
  494. u32 flags, count;
  495. db_dest_t *pDB;
  496. prxd = aup->rx_ring[aup->rx_head];
  497. flags = prxd->flags;
  498. while (!(flags & AU_OWN)) {
  499. pDB = aup->rx_db_inuse[aup->rx_head];
  500. count = prxd->count_1<<8 | prxd->count_0;
  501. if (!(flags & IR_RX_ERROR)) {
  502. /* good frame */
  503. update_rx_stats(dev, flags, count);
  504. skb=alloc_skb(count+1,GFP_ATOMIC);
  505. if (skb == NULL) {
  506. aup->netdev->stats.rx_dropped++;
  507. continue;
  508. }
  509. skb_reserve(skb, 1);
  510. if (aup->speed == 4000000)
  511. skb_put(skb, count);
  512. else
  513. skb_put(skb, count-2);
  514. skb_copy_to_linear_data(skb, pDB->vaddr, count - 2);
  515. skb->dev = dev;
  516. skb_reset_mac_header(skb);
  517. skb->protocol = htons(ETH_P_IRDA);
  518. netif_rx(skb);
  519. prxd->count_0 = 0;
  520. prxd->count_1 = 0;
  521. }
  522. prxd->flags |= AU_OWN;
  523. aup->rx_head = (aup->rx_head + 1) & (NUM_IR_DESC - 1);
  524. writel(0, IR_RING_PROMPT);
  525. au_sync();
  526. /* next descriptor */
  527. prxd = aup->rx_ring[aup->rx_head];
  528. flags = prxd->flags;
  529. }
  530. return 0;
  531. }
  532. static irqreturn_t au1k_irda_interrupt(int dummy, void *dev_id)
  533. {
  534. struct net_device *dev = dev_id;
  535. writel(0, IR_INT_CLEAR); /* ack irda interrupts */
  536. au1k_irda_rx(dev);
  537. au1k_tx_ack(dev);
  538. return IRQ_HANDLED;
  539. }
  540. /*
  541. * The Tx ring has been full longer than the watchdog timeout
  542. * value. The transmitter must be hung?
  543. */
  544. static void au1k_tx_timeout(struct net_device *dev)
  545. {
  546. u32 speed;
  547. struct au1k_private *aup = netdev_priv(dev);
  548. printk(KERN_ERR "%s: tx timeout\n", dev->name);
  549. speed = aup->speed;
  550. aup->speed = 0;
  551. au1k_irda_set_speed(dev, speed);
  552. aup->tx_full = 0;
  553. netif_wake_queue(dev);
  554. }
  555. /*
  556. * Set the IrDA communications speed.
  557. */
  558. static int
  559. au1k_irda_set_speed(struct net_device *dev, int speed)
  560. {
  561. unsigned long flags;
  562. struct au1k_private *aup = netdev_priv(dev);
  563. u32 control;
  564. int ret = 0, timeout = 10, i;
  565. volatile ring_dest_t *ptxd;
  566. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  567. unsigned long irda_resets;
  568. #endif
  569. if (speed == aup->speed)
  570. return ret;
  571. spin_lock_irqsave(&ir_lock, flags);
  572. /* disable PHY first */
  573. writel(read_ir_reg(IR_ENABLE) & ~0x8000, IR_ENABLE);
  574. /* disable RX/TX */
  575. writel(read_ir_reg(IR_CONFIG_1) & ~(IR_RX_ENABLE|IR_TX_ENABLE),
  576. IR_CONFIG_1);
  577. au_sync_delay(1);
  578. while (read_ir_reg(IR_ENABLE) & (IR_RX_STATUS | IR_TX_STATUS)) {
  579. mdelay(1);
  580. if (!timeout--) {
  581. printk(KERN_ERR "%s: rx/tx disable timeout\n",
  582. dev->name);
  583. break;
  584. }
  585. }
  586. /* disable DMA */
  587. writel(read_ir_reg(IR_CONFIG_1) & ~IR_DMA_ENABLE, IR_CONFIG_1);
  588. au_sync_delay(1);
  589. /*
  590. * After we disable tx/rx. the index pointers
  591. * go back to zero.
  592. */
  593. aup->tx_head = aup->tx_tail = aup->rx_head = 0;
  594. for (i=0; i<NUM_IR_DESC; i++) {
  595. ptxd = aup->tx_ring[i];
  596. ptxd->flags = 0;
  597. ptxd->count_0 = 0;
  598. ptxd->count_1 = 0;
  599. }
  600. for (i=0; i<NUM_IR_DESC; i++) {
  601. ptxd = aup->rx_ring[i];
  602. ptxd->count_0 = 0;
  603. ptxd->count_1 = 0;
  604. ptxd->flags = AU_OWN;
  605. }
  606. if (speed == 4000000) {
  607. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  608. bcsr->resets |= BCSR_RESETS_FIR_SEL;
  609. #else /* Pb1000 and Pb1100 */
  610. writel(1<<13, CPLD_AUX1);
  611. #endif
  612. }
  613. else {
  614. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  615. bcsr->resets &= ~BCSR_RESETS_FIR_SEL;
  616. #else /* Pb1000 and Pb1100 */
  617. writel(readl(CPLD_AUX1) & ~(1<<13), CPLD_AUX1);
  618. #endif
  619. }
  620. switch (speed) {
  621. case 9600:
  622. writel(11<<10 | 12<<5, IR_WRITE_PHY_CONFIG);
  623. writel(IR_SIR_MODE, IR_CONFIG_1);
  624. break;
  625. case 19200:
  626. writel(5<<10 | 12<<5, IR_WRITE_PHY_CONFIG);
  627. writel(IR_SIR_MODE, IR_CONFIG_1);
  628. break;
  629. case 38400:
  630. writel(2<<10 | 12<<5, IR_WRITE_PHY_CONFIG);
  631. writel(IR_SIR_MODE, IR_CONFIG_1);
  632. break;
  633. case 57600:
  634. writel(1<<10 | 12<<5, IR_WRITE_PHY_CONFIG);
  635. writel(IR_SIR_MODE, IR_CONFIG_1);
  636. break;
  637. case 115200:
  638. writel(12<<5, IR_WRITE_PHY_CONFIG);
  639. writel(IR_SIR_MODE, IR_CONFIG_1);
  640. break;
  641. case 4000000:
  642. writel(0xF, IR_WRITE_PHY_CONFIG);
  643. writel(IR_FIR|IR_DMA_ENABLE|IR_RX_ENABLE, IR_CONFIG_1);
  644. break;
  645. default:
  646. printk(KERN_ERR "%s unsupported speed %x\n", dev->name, speed);
  647. ret = -EINVAL;
  648. break;
  649. }
  650. aup->speed = speed;
  651. writel(read_ir_reg(IR_ENABLE) | 0x8000, IR_ENABLE);
  652. au_sync();
  653. control = read_ir_reg(IR_ENABLE);
  654. writel(0, IR_RING_PROMPT);
  655. au_sync();
  656. if (control & (1<<14)) {
  657. printk(KERN_ERR "%s: configuration error\n", dev->name);
  658. }
  659. else {
  660. if (control & (1<<11))
  661. printk(KERN_DEBUG "%s Valid SIR config\n", dev->name);
  662. if (control & (1<<12))
  663. printk(KERN_DEBUG "%s Valid MIR config\n", dev->name);
  664. if (control & (1<<13))
  665. printk(KERN_DEBUG "%s Valid FIR config\n", dev->name);
  666. if (control & (1<<10))
  667. printk(KERN_DEBUG "%s TX enabled\n", dev->name);
  668. if (control & (1<<9))
  669. printk(KERN_DEBUG "%s RX enabled\n", dev->name);
  670. }
  671. spin_unlock_irqrestore(&ir_lock, flags);
  672. return ret;
  673. }
  674. static int
  675. au1k_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
  676. {
  677. struct if_irda_req *rq = (struct if_irda_req *)ifreq;
  678. struct au1k_private *aup = netdev_priv(dev);
  679. int ret = -EOPNOTSUPP;
  680. switch (cmd) {
  681. case SIOCSBANDWIDTH:
  682. if (capable(CAP_NET_ADMIN)) {
  683. /*
  684. * We are unable to set the speed if the
  685. * device is not running.
  686. */
  687. if (aup->open)
  688. ret = au1k_irda_set_speed(dev,
  689. rq->ifr_baudrate);
  690. else {
  691. printk(KERN_ERR "%s ioctl: !netif_running\n",
  692. dev->name);
  693. ret = 0;
  694. }
  695. }
  696. break;
  697. case SIOCSMEDIABUSY:
  698. ret = -EPERM;
  699. if (capable(CAP_NET_ADMIN)) {
  700. irda_device_set_media_busy(dev, TRUE);
  701. ret = 0;
  702. }
  703. break;
  704. case SIOCGRECEIVING:
  705. rq->ifr_receiving = 0;
  706. break;
  707. default:
  708. break;
  709. }
  710. return ret;
  711. }
  712. MODULE_AUTHOR("Pete Popov <ppopov@mvista.com>");
  713. MODULE_DESCRIPTION("Au1000 IrDA Device Driver");
  714. module_init(au1k_irda_init);
  715. module_exit(au1k_irda_exit);