au1k_ir.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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/config.h>
  22. #include <linux/module.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/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 *, struct pt_regs *);
  53. static void au1k_tx_timeout(struct net_device *);
  54. static struct net_device_stats *au1k_irda_stats(struct net_device *);
  55. static int au1k_irda_ioctl(struct net_device *, struct ifreq *, int);
  56. static int au1k_irda_set_speed(struct net_device *dev, int speed);
  57. static void *dma_alloc(size_t, dma_addr_t *);
  58. static void dma_free(void *, size_t);
  59. static int qos_mtt_bits = 0x07; /* 1 ms or more */
  60. static struct net_device *ir_devs[NUM_IR_IFF];
  61. static char version[] __devinitdata =
  62. "au1k_ircc:1.2 ppopov@mvista.com\n";
  63. #define RUN_AT(x) (jiffies + (x))
  64. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  65. static BCSR * const bcsr = (BCSR *)0xAE000000;
  66. #endif
  67. static DEFINE_SPINLOCK(ir_lock);
  68. /*
  69. * IrDA peripheral bug. You have to read the register
  70. * twice to get the right value.
  71. */
  72. u32 read_ir_reg(u32 addr)
  73. {
  74. readl(addr);
  75. return readl(addr);
  76. }
  77. /*
  78. * Buffer allocation/deallocation routines. The buffer descriptor returned
  79. * has the virtual and dma address of a buffer suitable for
  80. * both, receive and transmit operations.
  81. */
  82. static db_dest_t *GetFreeDB(struct au1k_private *aup)
  83. {
  84. db_dest_t *pDB;
  85. pDB = aup->pDBfree;
  86. if (pDB) {
  87. aup->pDBfree = pDB->pnext;
  88. }
  89. return pDB;
  90. }
  91. static void ReleaseDB(struct au1k_private *aup, db_dest_t *pDB)
  92. {
  93. db_dest_t *pDBfree = aup->pDBfree;
  94. if (pDBfree)
  95. pDBfree->pnext = pDB;
  96. aup->pDBfree = pDB;
  97. }
  98. /*
  99. DMA memory allocation, derived from pci_alloc_consistent.
  100. However, the Au1000 data cache is coherent (when programmed
  101. so), therefore we return KSEG0 address, not KSEG1.
  102. */
  103. static void *dma_alloc(size_t size, dma_addr_t * dma_handle)
  104. {
  105. void *ret;
  106. int gfp = GFP_ATOMIC | GFP_DMA;
  107. ret = (void *) __get_free_pages(gfp, get_order(size));
  108. if (ret != NULL) {
  109. memset(ret, 0, size);
  110. *dma_handle = virt_to_bus(ret);
  111. ret = (void *)KSEG0ADDR(ret);
  112. }
  113. return ret;
  114. }
  115. static void dma_free(void *vaddr, size_t size)
  116. {
  117. vaddr = (void *)KSEG0ADDR(vaddr);
  118. free_pages((unsigned long) vaddr, get_order(size));
  119. }
  120. static void
  121. setup_hw_rings(struct au1k_private *aup, u32 rx_base, u32 tx_base)
  122. {
  123. int i;
  124. for (i=0; i<NUM_IR_DESC; i++) {
  125. aup->rx_ring[i] = (volatile ring_dest_t *)
  126. (rx_base + sizeof(ring_dest_t)*i);
  127. }
  128. for (i=0; i<NUM_IR_DESC; i++) {
  129. aup->tx_ring[i] = (volatile ring_dest_t *)
  130. (tx_base + sizeof(ring_dest_t)*i);
  131. }
  132. }
  133. static int au1k_irda_init(void)
  134. {
  135. static unsigned version_printed = 0;
  136. struct au1k_private *aup;
  137. struct net_device *dev;
  138. int err;
  139. if (version_printed++ == 0) printk(version);
  140. dev = alloc_irdadev(sizeof(struct au1k_private));
  141. if (!dev)
  142. return -ENOMEM;
  143. dev->irq = AU1000_IRDA_RX_INT; /* TX has its own interrupt */
  144. err = au1k_irda_net_init(dev);
  145. if (err)
  146. goto out;
  147. err = register_netdev(dev);
  148. if (err)
  149. goto out1;
  150. ir_devs[0] = dev;
  151. printk(KERN_INFO "IrDA: Registered device %s\n", dev->name);
  152. return 0;
  153. out1:
  154. aup = netdev_priv(dev);
  155. dma_free((void *)aup->db[0].vaddr,
  156. MAX_BUF_SIZE * 2*NUM_IR_DESC);
  157. dma_free((void *)aup->rx_ring[0],
  158. 2 * MAX_NUM_IR_DESC*(sizeof(ring_dest_t)));
  159. kfree(aup->rx_buff.head);
  160. out:
  161. free_netdev(dev);
  162. return err;
  163. }
  164. static int au1k_irda_init_iobuf(iobuff_t *io, int size)
  165. {
  166. io->head = kmalloc(size, GFP_KERNEL);
  167. if (io->head != NULL) {
  168. io->truesize = size;
  169. io->in_frame = FALSE;
  170. io->state = OUTSIDE_FRAME;
  171. io->data = io->head;
  172. }
  173. return io->head ? 0 : -ENOMEM;
  174. }
  175. static int au1k_irda_net_init(struct net_device *dev)
  176. {
  177. struct au1k_private *aup = netdev_priv(dev);
  178. int i, retval = 0, err;
  179. db_dest_t *pDB, *pDBfree;
  180. dma_addr_t temp;
  181. err = au1k_irda_init_iobuf(&aup->rx_buff, 14384);
  182. if (err)
  183. goto out1;
  184. dev->open = au1k_irda_start;
  185. dev->hard_start_xmit = au1k_irda_hard_xmit;
  186. dev->stop = au1k_irda_stop;
  187. dev->get_stats = au1k_irda_stats;
  188. dev->do_ioctl = au1k_irda_ioctl;
  189. dev->tx_timeout = au1k_tx_timeout;
  190. irda_init_max_qos_capabilies(&aup->qos);
  191. /* The only value we must override it the baudrate */
  192. aup->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
  193. IR_115200|IR_576000 |(IR_4000000 << 8);
  194. aup->qos.min_turn_time.bits = qos_mtt_bits;
  195. irda_qos_bits_to_value(&aup->qos);
  196. retval = -ENOMEM;
  197. /* Tx ring follows rx ring + 512 bytes */
  198. /* we need a 1k aligned buffer */
  199. aup->rx_ring[0] = (ring_dest_t *)
  200. dma_alloc(2*MAX_NUM_IR_DESC*(sizeof(ring_dest_t)), &temp);
  201. if (!aup->rx_ring[0])
  202. goto out2;
  203. /* allocate the data buffers */
  204. aup->db[0].vaddr =
  205. (void *)dma_alloc(MAX_BUF_SIZE * 2*NUM_IR_DESC, &temp);
  206. if (!aup->db[0].vaddr)
  207. goto out3;
  208. setup_hw_rings(aup, (u32)aup->rx_ring[0], (u32)aup->rx_ring[0] + 512);
  209. pDBfree = NULL;
  210. pDB = aup->db;
  211. for (i=0; i<(2*NUM_IR_DESC); i++) {
  212. pDB->pnext = pDBfree;
  213. pDBfree = pDB;
  214. pDB->vaddr =
  215. (u32 *)((unsigned)aup->db[0].vaddr + MAX_BUF_SIZE*i);
  216. pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
  217. pDB++;
  218. }
  219. aup->pDBfree = pDBfree;
  220. /* attach a data buffer to each descriptor */
  221. for (i=0; i<NUM_IR_DESC; i++) {
  222. pDB = GetFreeDB(aup);
  223. if (!pDB) goto out;
  224. aup->rx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
  225. aup->rx_ring[i]->addr_1 = (u8)((pDB->dma_addr>>8) & 0xff);
  226. aup->rx_ring[i]->addr_2 = (u8)((pDB->dma_addr>>16) & 0xff);
  227. aup->rx_ring[i]->addr_3 = (u8)((pDB->dma_addr>>24) & 0xff);
  228. aup->rx_db_inuse[i] = pDB;
  229. }
  230. for (i=0; i<NUM_IR_DESC; i++) {
  231. pDB = GetFreeDB(aup);
  232. if (!pDB) goto out;
  233. aup->tx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
  234. aup->tx_ring[i]->addr_1 = (u8)((pDB->dma_addr>>8) & 0xff);
  235. aup->tx_ring[i]->addr_2 = (u8)((pDB->dma_addr>>16) & 0xff);
  236. aup->tx_ring[i]->addr_3 = (u8)((pDB->dma_addr>>24) & 0xff);
  237. aup->tx_ring[i]->count_0 = 0;
  238. aup->tx_ring[i]->count_1 = 0;
  239. aup->tx_ring[i]->flags = 0;
  240. aup->tx_db_inuse[i] = pDB;
  241. }
  242. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  243. /* power on */
  244. bcsr->resets &= ~BCSR_RESETS_IRDA_MODE_MASK;
  245. bcsr->resets |= BCSR_RESETS_IRDA_MODE_FULL;
  246. au_sync();
  247. #endif
  248. return 0;
  249. out3:
  250. dma_free((void *)aup->rx_ring[0],
  251. 2 * MAX_NUM_IR_DESC*(sizeof(ring_dest_t)));
  252. out2:
  253. kfree(aup->rx_buff.head);
  254. out1:
  255. printk(KERN_ERR "au1k_init_module failed. Returns %d\n", retval);
  256. return retval;
  257. }
  258. static int au1k_init(struct net_device *dev)
  259. {
  260. struct au1k_private *aup = netdev_priv(dev);
  261. int i;
  262. u32 control;
  263. u32 ring_address;
  264. /* bring the device out of reset */
  265. control = 0xe; /* coherent, clock enable, one half system clock */
  266. #ifndef CONFIG_CPU_LITTLE_ENDIAN
  267. control |= 1;
  268. #endif
  269. aup->tx_head = 0;
  270. aup->tx_tail = 0;
  271. aup->rx_head = 0;
  272. for (i=0; i<NUM_IR_DESC; i++) {
  273. aup->rx_ring[i]->flags = AU_OWN;
  274. }
  275. writel(control, IR_INTERFACE_CONFIG);
  276. au_sync_delay(10);
  277. writel(read_ir_reg(IR_ENABLE) & ~0x8000, IR_ENABLE); /* disable PHY */
  278. au_sync_delay(1);
  279. writel(MAX_BUF_SIZE, IR_MAX_PKT_LEN);
  280. ring_address = (u32)virt_to_phys((void *)aup->rx_ring[0]);
  281. writel(ring_address >> 26, IR_RING_BASE_ADDR_H);
  282. writel((ring_address >> 10) & 0xffff, IR_RING_BASE_ADDR_L);
  283. writel(RING_SIZE_64<<8 | RING_SIZE_64<<12, IR_RING_SIZE);
  284. writel(1<<2 | IR_ONE_PIN, IR_CONFIG_2); /* 48MHz */
  285. writel(0, IR_RING_ADDR_CMPR);
  286. au1k_irda_set_speed(dev, 9600);
  287. return 0;
  288. }
  289. static int au1k_irda_start(struct net_device *dev)
  290. {
  291. int retval;
  292. char hwname[32];
  293. struct au1k_private *aup = netdev_priv(dev);
  294. if ((retval = au1k_init(dev))) {
  295. printk(KERN_ERR "%s: error in au1k_init\n", dev->name);
  296. return retval;
  297. }
  298. if ((retval = request_irq(AU1000_IRDA_TX_INT, &au1k_irda_interrupt,
  299. 0, dev->name, dev))) {
  300. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  301. dev->name, dev->irq);
  302. return retval;
  303. }
  304. if ((retval = request_irq(AU1000_IRDA_RX_INT, &au1k_irda_interrupt,
  305. 0, dev->name, dev))) {
  306. free_irq(AU1000_IRDA_TX_INT, dev);
  307. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  308. dev->name, dev->irq);
  309. return retval;
  310. }
  311. /* Give self a hardware name */
  312. sprintf(hwname, "Au1000 SIR/FIR");
  313. aup->irlap = irlap_open(dev, &aup->qos, hwname);
  314. netif_start_queue(dev);
  315. writel(read_ir_reg(IR_CONFIG_2) | 1<<8, IR_CONFIG_2); /* int enable */
  316. aup->timer.expires = RUN_AT((3*HZ));
  317. aup->timer.data = (unsigned long)dev;
  318. return 0;
  319. }
  320. static int au1k_irda_stop(struct net_device *dev)
  321. {
  322. struct au1k_private *aup = netdev_priv(dev);
  323. /* disable interrupts */
  324. writel(read_ir_reg(IR_CONFIG_2) & ~(1<<8), IR_CONFIG_2);
  325. writel(0, IR_CONFIG_1);
  326. writel(0, IR_INTERFACE_CONFIG); /* disable clock */
  327. au_sync();
  328. if (aup->irlap) {
  329. irlap_close(aup->irlap);
  330. aup->irlap = NULL;
  331. }
  332. netif_stop_queue(dev);
  333. del_timer(&aup->timer);
  334. /* disable the interrupt */
  335. free_irq(AU1000_IRDA_TX_INT, dev);
  336. free_irq(AU1000_IRDA_RX_INT, dev);
  337. return 0;
  338. }
  339. static void __exit au1k_irda_exit(void)
  340. {
  341. struct net_device *dev = ir_devs[0];
  342. struct au1k_private *aup = netdev_priv(dev);
  343. unregister_netdev(dev);
  344. dma_free((void *)aup->db[0].vaddr,
  345. MAX_BUF_SIZE * 2*NUM_IR_DESC);
  346. dma_free((void *)aup->rx_ring[0],
  347. 2 * MAX_NUM_IR_DESC*(sizeof(ring_dest_t)));
  348. kfree(aup->rx_buff.head);
  349. free_netdev(dev);
  350. }
  351. static inline void
  352. update_tx_stats(struct net_device *dev, u32 status, u32 pkt_len)
  353. {
  354. struct au1k_private *aup = netdev_priv(dev);
  355. struct net_device_stats *ps = &aup->stats;
  356. ps->tx_packets++;
  357. ps->tx_bytes += pkt_len;
  358. if (status & IR_TX_ERROR) {
  359. ps->tx_errors++;
  360. ps->tx_aborted_errors++;
  361. }
  362. }
  363. static void au1k_tx_ack(struct net_device *dev)
  364. {
  365. struct au1k_private *aup = netdev_priv(dev);
  366. volatile ring_dest_t *ptxd;
  367. ptxd = aup->tx_ring[aup->tx_tail];
  368. while (!(ptxd->flags & AU_OWN) && (aup->tx_tail != aup->tx_head)) {
  369. update_tx_stats(dev, ptxd->flags,
  370. ptxd->count_1<<8 | ptxd->count_0);
  371. ptxd->count_0 = 0;
  372. ptxd->count_1 = 0;
  373. au_sync();
  374. aup->tx_tail = (aup->tx_tail + 1) & (NUM_IR_DESC - 1);
  375. ptxd = aup->tx_ring[aup->tx_tail];
  376. if (aup->tx_full) {
  377. aup->tx_full = 0;
  378. netif_wake_queue(dev);
  379. }
  380. }
  381. if (aup->tx_tail == aup->tx_head) {
  382. if (aup->newspeed) {
  383. au1k_irda_set_speed(dev, aup->newspeed);
  384. aup->newspeed = 0;
  385. }
  386. else {
  387. writel(read_ir_reg(IR_CONFIG_1) & ~IR_TX_ENABLE,
  388. IR_CONFIG_1);
  389. au_sync();
  390. writel(read_ir_reg(IR_CONFIG_1) | IR_RX_ENABLE,
  391. IR_CONFIG_1);
  392. writel(0, IR_RING_PROMPT);
  393. au_sync();
  394. }
  395. }
  396. }
  397. /*
  398. * Au1000 transmit routine.
  399. */
  400. static int au1k_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  401. {
  402. struct au1k_private *aup = netdev_priv(dev);
  403. int speed = irda_get_next_speed(skb);
  404. volatile ring_dest_t *ptxd;
  405. u32 len;
  406. u32 flags;
  407. db_dest_t *pDB;
  408. if (speed != aup->speed && speed != -1) {
  409. aup->newspeed = speed;
  410. }
  411. if ((skb->len == 0) && (aup->newspeed)) {
  412. if (aup->tx_tail == aup->tx_head) {
  413. au1k_irda_set_speed(dev, speed);
  414. aup->newspeed = 0;
  415. }
  416. dev_kfree_skb(skb);
  417. return 0;
  418. }
  419. ptxd = aup->tx_ring[aup->tx_head];
  420. flags = ptxd->flags;
  421. if (flags & AU_OWN) {
  422. printk(KERN_DEBUG "%s: tx_full\n", dev->name);
  423. netif_stop_queue(dev);
  424. aup->tx_full = 1;
  425. return 1;
  426. }
  427. else if (((aup->tx_head + 1) & (NUM_IR_DESC - 1)) == aup->tx_tail) {
  428. printk(KERN_DEBUG "%s: tx_full\n", dev->name);
  429. netif_stop_queue(dev);
  430. aup->tx_full = 1;
  431. return 1;
  432. }
  433. pDB = aup->tx_db_inuse[aup->tx_head];
  434. #if 0
  435. if (read_ir_reg(IR_RX_BYTE_CNT) != 0) {
  436. printk("tx warning: rx byte cnt %x\n",
  437. read_ir_reg(IR_RX_BYTE_CNT));
  438. }
  439. #endif
  440. if (aup->speed == 4000000) {
  441. /* FIR */
  442. memcpy((void *)pDB->vaddr, skb->data, skb->len);
  443. ptxd->count_0 = skb->len & 0xff;
  444. ptxd->count_1 = (skb->len >> 8) & 0xff;
  445. }
  446. else {
  447. /* SIR */
  448. len = async_wrap_skb(skb, (u8 *)pDB->vaddr, MAX_BUF_SIZE);
  449. ptxd->count_0 = len & 0xff;
  450. ptxd->count_1 = (len >> 8) & 0xff;
  451. ptxd->flags |= IR_DIS_CRC;
  452. au_writel(au_readl(0xae00000c) & ~(1<<13), 0xae00000c);
  453. }
  454. ptxd->flags |= AU_OWN;
  455. au_sync();
  456. writel(read_ir_reg(IR_CONFIG_1) | IR_TX_ENABLE, IR_CONFIG_1);
  457. writel(0, IR_RING_PROMPT);
  458. au_sync();
  459. dev_kfree_skb(skb);
  460. aup->tx_head = (aup->tx_head + 1) & (NUM_IR_DESC - 1);
  461. dev->trans_start = jiffies;
  462. return 0;
  463. }
  464. static inline void
  465. update_rx_stats(struct net_device *dev, u32 status, u32 count)
  466. {
  467. struct au1k_private *aup = netdev_priv(dev);
  468. struct net_device_stats *ps = &aup->stats;
  469. ps->rx_packets++;
  470. if (status & IR_RX_ERROR) {
  471. ps->rx_errors++;
  472. if (status & (IR_PHY_ERROR|IR_FIFO_OVER))
  473. ps->rx_missed_errors++;
  474. if (status & IR_MAX_LEN)
  475. ps->rx_length_errors++;
  476. if (status & IR_CRC_ERROR)
  477. ps->rx_crc_errors++;
  478. }
  479. else
  480. ps->rx_bytes += count;
  481. }
  482. /*
  483. * Au1000 receive routine.
  484. */
  485. static int au1k_irda_rx(struct net_device *dev)
  486. {
  487. struct au1k_private *aup = netdev_priv(dev);
  488. struct sk_buff *skb;
  489. volatile ring_dest_t *prxd;
  490. u32 flags, count;
  491. db_dest_t *pDB;
  492. prxd = aup->rx_ring[aup->rx_head];
  493. flags = prxd->flags;
  494. while (!(flags & AU_OWN)) {
  495. pDB = aup->rx_db_inuse[aup->rx_head];
  496. count = prxd->count_1<<8 | prxd->count_0;
  497. if (!(flags & IR_RX_ERROR)) {
  498. /* good frame */
  499. update_rx_stats(dev, flags, count);
  500. skb=alloc_skb(count+1,GFP_ATOMIC);
  501. if (skb == NULL) {
  502. aup->stats.rx_dropped++;
  503. continue;
  504. }
  505. skb_reserve(skb, 1);
  506. if (aup->speed == 4000000)
  507. skb_put(skb, count);
  508. else
  509. skb_put(skb, count-2);
  510. memcpy(skb->data, (void *)pDB->vaddr, count-2);
  511. skb->dev = dev;
  512. skb->mac.raw = skb->data;
  513. skb->protocol = htons(ETH_P_IRDA);
  514. netif_rx(skb);
  515. prxd->count_0 = 0;
  516. prxd->count_1 = 0;
  517. }
  518. prxd->flags |= AU_OWN;
  519. aup->rx_head = (aup->rx_head + 1) & (NUM_IR_DESC - 1);
  520. writel(0, IR_RING_PROMPT);
  521. au_sync();
  522. /* next descriptor */
  523. prxd = aup->rx_ring[aup->rx_head];
  524. flags = prxd->flags;
  525. dev->last_rx = jiffies;
  526. }
  527. return 0;
  528. }
  529. void au1k_irda_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  530. {
  531. struct net_device *dev = (struct net_device *) dev_id;
  532. if (dev == NULL) {
  533. printk(KERN_ERR "%s: isr: null dev ptr\n", dev->name);
  534. return;
  535. }
  536. writel(0, IR_INT_CLEAR); /* ack irda interrupts */
  537. au1k_irda_rx(dev);
  538. au1k_tx_ack(dev);
  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. static struct net_device_stats *au1k_irda_stats(struct net_device *dev)
  713. {
  714. struct au1k_private *aup = netdev_priv(dev);
  715. return &aup->stats;
  716. }
  717. MODULE_AUTHOR("Pete Popov <ppopov@mvista.com>");
  718. MODULE_DESCRIPTION("Au1000 IrDA Device Driver");
  719. module_init(au1k_irda_init);
  720. module_exit(au1k_irda_exit);