au1k_ir.c 20 KB

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