au1k_ir.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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. #else
  39. #error au1k_ir: unsupported board
  40. #endif
  41. #include <net/irda/irda.h>
  42. #include <net/irda/irmod.h>
  43. #include <net/irda/wrapper.h>
  44. #include <net/irda/irda_device.h>
  45. #include "au1000_ircc.h"
  46. static int au1k_irda_net_init(struct net_device *);
  47. static int au1k_irda_start(struct net_device *);
  48. static int au1k_irda_stop(struct net_device *dev);
  49. static int au1k_irda_hard_xmit(struct sk_buff *, struct net_device *);
  50. static int au1k_irda_rx(struct net_device *);
  51. static void au1k_irda_interrupt(int, void *);
  52. static void au1k_tx_timeout(struct net_device *);
  53. static struct net_device_stats *au1k_irda_stats(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 int au1k_irda_net_init(struct net_device *dev)
  175. {
  176. struct au1k_private *aup = netdev_priv(dev);
  177. int i, retval = 0, err;
  178. db_dest_t *pDB, *pDBfree;
  179. dma_addr_t temp;
  180. err = au1k_irda_init_iobuf(&aup->rx_buff, 14384);
  181. if (err)
  182. goto out1;
  183. dev->open = au1k_irda_start;
  184. dev->hard_start_xmit = au1k_irda_hard_xmit;
  185. dev->stop = au1k_irda_stop;
  186. dev->get_stats = au1k_irda_stats;
  187. dev->do_ioctl = au1k_irda_ioctl;
  188. dev->tx_timeout = au1k_tx_timeout;
  189. irda_init_max_qos_capabilies(&aup->qos);
  190. /* The only value we must override it the baudrate */
  191. aup->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
  192. IR_115200|IR_576000 |(IR_4000000 << 8);
  193. aup->qos.min_turn_time.bits = qos_mtt_bits;
  194. irda_qos_bits_to_value(&aup->qos);
  195. retval = -ENOMEM;
  196. /* Tx ring follows rx ring + 512 bytes */
  197. /* we need a 1k aligned buffer */
  198. aup->rx_ring[0] = (ring_dest_t *)
  199. dma_alloc(2*MAX_NUM_IR_DESC*(sizeof(ring_dest_t)), &temp);
  200. if (!aup->rx_ring[0])
  201. goto out2;
  202. /* allocate the data buffers */
  203. aup->db[0].vaddr =
  204. (void *)dma_alloc(MAX_BUF_SIZE * 2*NUM_IR_DESC, &temp);
  205. if (!aup->db[0].vaddr)
  206. goto out3;
  207. setup_hw_rings(aup, (u32)aup->rx_ring[0], (u32)aup->rx_ring[0] + 512);
  208. pDBfree = NULL;
  209. pDB = aup->db;
  210. for (i=0; i<(2*NUM_IR_DESC); i++) {
  211. pDB->pnext = pDBfree;
  212. pDBfree = pDB;
  213. pDB->vaddr =
  214. (u32 *)((unsigned)aup->db[0].vaddr + MAX_BUF_SIZE*i);
  215. pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
  216. pDB++;
  217. }
  218. aup->pDBfree = pDBfree;
  219. /* attach a data buffer to each descriptor */
  220. for (i=0; i<NUM_IR_DESC; i++) {
  221. pDB = GetFreeDB(aup);
  222. if (!pDB) goto out;
  223. aup->rx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
  224. aup->rx_ring[i]->addr_1 = (u8)((pDB->dma_addr>>8) & 0xff);
  225. aup->rx_ring[i]->addr_2 = (u8)((pDB->dma_addr>>16) & 0xff);
  226. aup->rx_ring[i]->addr_3 = (u8)((pDB->dma_addr>>24) & 0xff);
  227. aup->rx_db_inuse[i] = pDB;
  228. }
  229. for (i=0; i<NUM_IR_DESC; i++) {
  230. pDB = GetFreeDB(aup);
  231. if (!pDB) goto out;
  232. aup->tx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
  233. aup->tx_ring[i]->addr_1 = (u8)((pDB->dma_addr>>8) & 0xff);
  234. aup->tx_ring[i]->addr_2 = (u8)((pDB->dma_addr>>16) & 0xff);
  235. aup->tx_ring[i]->addr_3 = (u8)((pDB->dma_addr>>24) & 0xff);
  236. aup->tx_ring[i]->count_0 = 0;
  237. aup->tx_ring[i]->count_1 = 0;
  238. aup->tx_ring[i]->flags = 0;
  239. aup->tx_db_inuse[i] = pDB;
  240. }
  241. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  242. /* power on */
  243. bcsr->resets &= ~BCSR_RESETS_IRDA_MODE_MASK;
  244. bcsr->resets |= BCSR_RESETS_IRDA_MODE_FULL;
  245. au_sync();
  246. #endif
  247. return 0;
  248. out3:
  249. dma_free((void *)aup->rx_ring[0],
  250. 2 * MAX_NUM_IR_DESC*(sizeof(ring_dest_t)));
  251. out2:
  252. kfree(aup->rx_buff.head);
  253. out1:
  254. printk(KERN_ERR "au1k_init_module failed. Returns %d\n", retval);
  255. return retval;
  256. }
  257. static int au1k_init(struct net_device *dev)
  258. {
  259. struct au1k_private *aup = netdev_priv(dev);
  260. int i;
  261. u32 control;
  262. u32 ring_address;
  263. /* bring the device out of reset */
  264. control = 0xe; /* coherent, clock enable, one half system clock */
  265. #ifndef CONFIG_CPU_LITTLE_ENDIAN
  266. control |= 1;
  267. #endif
  268. aup->tx_head = 0;
  269. aup->tx_tail = 0;
  270. aup->rx_head = 0;
  271. for (i=0; i<NUM_IR_DESC; i++) {
  272. aup->rx_ring[i]->flags = AU_OWN;
  273. }
  274. writel(control, IR_INTERFACE_CONFIG);
  275. au_sync_delay(10);
  276. writel(read_ir_reg(IR_ENABLE) & ~0x8000, IR_ENABLE); /* disable PHY */
  277. au_sync_delay(1);
  278. writel(MAX_BUF_SIZE, IR_MAX_PKT_LEN);
  279. ring_address = (u32)virt_to_phys((void *)aup->rx_ring[0]);
  280. writel(ring_address >> 26, IR_RING_BASE_ADDR_H);
  281. writel((ring_address >> 10) & 0xffff, IR_RING_BASE_ADDR_L);
  282. writel(RING_SIZE_64<<8 | RING_SIZE_64<<12, IR_RING_SIZE);
  283. writel(1<<2 | IR_ONE_PIN, IR_CONFIG_2); /* 48MHz */
  284. writel(0, IR_RING_ADDR_CMPR);
  285. au1k_irda_set_speed(dev, 9600);
  286. return 0;
  287. }
  288. static int au1k_irda_start(struct net_device *dev)
  289. {
  290. int retval;
  291. char hwname[32];
  292. struct au1k_private *aup = netdev_priv(dev);
  293. if ((retval = au1k_init(dev))) {
  294. printk(KERN_ERR "%s: error in au1k_init\n", dev->name);
  295. return retval;
  296. }
  297. if ((retval = request_irq(AU1000_IRDA_TX_INT, &au1k_irda_interrupt,
  298. 0, dev->name, dev))) {
  299. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  300. dev->name, dev->irq);
  301. return retval;
  302. }
  303. if ((retval = request_irq(AU1000_IRDA_RX_INT, &au1k_irda_interrupt,
  304. 0, dev->name, dev))) {
  305. free_irq(AU1000_IRDA_TX_INT, dev);
  306. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  307. dev->name, dev->irq);
  308. return retval;
  309. }
  310. /* Give self a hardware name */
  311. sprintf(hwname, "Au1000 SIR/FIR");
  312. aup->irlap = irlap_open(dev, &aup->qos, hwname);
  313. netif_start_queue(dev);
  314. writel(read_ir_reg(IR_CONFIG_2) | 1<<8, IR_CONFIG_2); /* int enable */
  315. aup->timer.expires = RUN_AT((3*HZ));
  316. aup->timer.data = (unsigned long)dev;
  317. return 0;
  318. }
  319. static int au1k_irda_stop(struct net_device *dev)
  320. {
  321. struct au1k_private *aup = netdev_priv(dev);
  322. /* disable interrupts */
  323. writel(read_ir_reg(IR_CONFIG_2) & ~(1<<8), IR_CONFIG_2);
  324. writel(0, IR_CONFIG_1);
  325. writel(0, IR_INTERFACE_CONFIG); /* disable clock */
  326. au_sync();
  327. if (aup->irlap) {
  328. irlap_close(aup->irlap);
  329. aup->irlap = NULL;
  330. }
  331. netif_stop_queue(dev);
  332. del_timer(&aup->timer);
  333. /* disable the interrupt */
  334. free_irq(AU1000_IRDA_TX_INT, dev);
  335. free_irq(AU1000_IRDA_RX_INT, dev);
  336. return 0;
  337. }
  338. static void __exit au1k_irda_exit(void)
  339. {
  340. struct net_device *dev = ir_devs[0];
  341. struct au1k_private *aup = netdev_priv(dev);
  342. unregister_netdev(dev);
  343. dma_free((void *)aup->db[0].vaddr,
  344. MAX_BUF_SIZE * 2*NUM_IR_DESC);
  345. dma_free((void *)aup->rx_ring[0],
  346. 2 * MAX_NUM_IR_DESC*(sizeof(ring_dest_t)));
  347. kfree(aup->rx_buff.head);
  348. free_netdev(dev);
  349. }
  350. static inline void
  351. update_tx_stats(struct net_device *dev, u32 status, u32 pkt_len)
  352. {
  353. struct au1k_private *aup = netdev_priv(dev);
  354. struct net_device_stats *ps = &aup->stats;
  355. ps->tx_packets++;
  356. ps->tx_bytes += pkt_len;
  357. if (status & IR_TX_ERROR) {
  358. ps->tx_errors++;
  359. ps->tx_aborted_errors++;
  360. }
  361. }
  362. static void au1k_tx_ack(struct net_device *dev)
  363. {
  364. struct au1k_private *aup = netdev_priv(dev);
  365. volatile ring_dest_t *ptxd;
  366. ptxd = aup->tx_ring[aup->tx_tail];
  367. while (!(ptxd->flags & AU_OWN) && (aup->tx_tail != aup->tx_head)) {
  368. update_tx_stats(dev, ptxd->flags,
  369. ptxd->count_1<<8 | ptxd->count_0);
  370. ptxd->count_0 = 0;
  371. ptxd->count_1 = 0;
  372. au_sync();
  373. aup->tx_tail = (aup->tx_tail + 1) & (NUM_IR_DESC - 1);
  374. ptxd = aup->tx_ring[aup->tx_tail];
  375. if (aup->tx_full) {
  376. aup->tx_full = 0;
  377. netif_wake_queue(dev);
  378. }
  379. }
  380. if (aup->tx_tail == aup->tx_head) {
  381. if (aup->newspeed) {
  382. au1k_irda_set_speed(dev, aup->newspeed);
  383. aup->newspeed = 0;
  384. }
  385. else {
  386. writel(read_ir_reg(IR_CONFIG_1) & ~IR_TX_ENABLE,
  387. IR_CONFIG_1);
  388. au_sync();
  389. writel(read_ir_reg(IR_CONFIG_1) | IR_RX_ENABLE,
  390. IR_CONFIG_1);
  391. writel(0, IR_RING_PROMPT);
  392. au_sync();
  393. }
  394. }
  395. }
  396. /*
  397. * Au1000 transmit routine.
  398. */
  399. static int au1k_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  400. {
  401. struct au1k_private *aup = netdev_priv(dev);
  402. int speed = irda_get_next_speed(skb);
  403. volatile ring_dest_t *ptxd;
  404. u32 len;
  405. u32 flags;
  406. db_dest_t *pDB;
  407. if (speed != aup->speed && speed != -1) {
  408. aup->newspeed = speed;
  409. }
  410. if ((skb->len == 0) && (aup->newspeed)) {
  411. if (aup->tx_tail == aup->tx_head) {
  412. au1k_irda_set_speed(dev, speed);
  413. aup->newspeed = 0;
  414. }
  415. dev_kfree_skb(skb);
  416. return 0;
  417. }
  418. ptxd = aup->tx_ring[aup->tx_head];
  419. flags = ptxd->flags;
  420. if (flags & AU_OWN) {
  421. printk(KERN_DEBUG "%s: tx_full\n", dev->name);
  422. netif_stop_queue(dev);
  423. aup->tx_full = 1;
  424. return 1;
  425. }
  426. else if (((aup->tx_head + 1) & (NUM_IR_DESC - 1)) == aup->tx_tail) {
  427. printk(KERN_DEBUG "%s: tx_full\n", dev->name);
  428. netif_stop_queue(dev);
  429. aup->tx_full = 1;
  430. return 1;
  431. }
  432. pDB = aup->tx_db_inuse[aup->tx_head];
  433. #if 0
  434. if (read_ir_reg(IR_RX_BYTE_CNT) != 0) {
  435. printk("tx warning: rx byte cnt %x\n",
  436. read_ir_reg(IR_RX_BYTE_CNT));
  437. }
  438. #endif
  439. if (aup->speed == 4000000) {
  440. /* FIR */
  441. memcpy((void *)pDB->vaddr, skb->data, skb->len);
  442. ptxd->count_0 = skb->len & 0xff;
  443. ptxd->count_1 = (skb->len >> 8) & 0xff;
  444. }
  445. else {
  446. /* SIR */
  447. len = async_wrap_skb(skb, (u8 *)pDB->vaddr, MAX_BUF_SIZE);
  448. ptxd->count_0 = len & 0xff;
  449. ptxd->count_1 = (len >> 8) & 0xff;
  450. ptxd->flags |= IR_DIS_CRC;
  451. au_writel(au_readl(0xae00000c) & ~(1<<13), 0xae00000c);
  452. }
  453. ptxd->flags |= AU_OWN;
  454. au_sync();
  455. writel(read_ir_reg(IR_CONFIG_1) | IR_TX_ENABLE, IR_CONFIG_1);
  456. writel(0, IR_RING_PROMPT);
  457. au_sync();
  458. dev_kfree_skb(skb);
  459. aup->tx_head = (aup->tx_head + 1) & (NUM_IR_DESC - 1);
  460. dev->trans_start = jiffies;
  461. return 0;
  462. }
  463. static inline void
  464. update_rx_stats(struct net_device *dev, u32 status, u32 count)
  465. {
  466. struct au1k_private *aup = netdev_priv(dev);
  467. struct net_device_stats *ps = &aup->stats;
  468. ps->rx_packets++;
  469. if (status & IR_RX_ERROR) {
  470. ps->rx_errors++;
  471. if (status & (IR_PHY_ERROR|IR_FIFO_OVER))
  472. ps->rx_missed_errors++;
  473. if (status & IR_MAX_LEN)
  474. ps->rx_length_errors++;
  475. if (status & IR_CRC_ERROR)
  476. ps->rx_crc_errors++;
  477. }
  478. else
  479. ps->rx_bytes += count;
  480. }
  481. /*
  482. * Au1000 receive routine.
  483. */
  484. static int au1k_irda_rx(struct net_device *dev)
  485. {
  486. struct au1k_private *aup = netdev_priv(dev);
  487. struct sk_buff *skb;
  488. volatile ring_dest_t *prxd;
  489. u32 flags, count;
  490. db_dest_t *pDB;
  491. prxd = aup->rx_ring[aup->rx_head];
  492. flags = prxd->flags;
  493. while (!(flags & AU_OWN)) {
  494. pDB = aup->rx_db_inuse[aup->rx_head];
  495. count = prxd->count_1<<8 | prxd->count_0;
  496. if (!(flags & IR_RX_ERROR)) {
  497. /* good frame */
  498. update_rx_stats(dev, flags, count);
  499. skb=alloc_skb(count+1,GFP_ATOMIC);
  500. if (skb == NULL) {
  501. aup->stats.rx_dropped++;
  502. continue;
  503. }
  504. skb_reserve(skb, 1);
  505. if (aup->speed == 4000000)
  506. skb_put(skb, count);
  507. else
  508. skb_put(skb, count-2);
  509. memcpy(skb->data, (void *)pDB->vaddr, count-2);
  510. skb->dev = dev;
  511. skb->mac.raw = skb->data;
  512. skb->protocol = htons(ETH_P_IRDA);
  513. netif_rx(skb);
  514. prxd->count_0 = 0;
  515. prxd->count_1 = 0;
  516. }
  517. prxd->flags |= AU_OWN;
  518. aup->rx_head = (aup->rx_head + 1) & (NUM_IR_DESC - 1);
  519. writel(0, IR_RING_PROMPT);
  520. au_sync();
  521. /* next descriptor */
  522. prxd = aup->rx_ring[aup->rx_head];
  523. flags = prxd->flags;
  524. dev->last_rx = jiffies;
  525. }
  526. return 0;
  527. }
  528. void au1k_irda_interrupt(int irq, void *dev_id)
  529. {
  530. struct net_device *dev = (struct net_device *) dev_id;
  531. if (dev == NULL) {
  532. printk(KERN_ERR "%s: isr: null dev ptr\n", dev->name);
  533. return;
  534. }
  535. writel(0, IR_INT_CLEAR); /* ack irda interrupts */
  536. au1k_irda_rx(dev);
  537. au1k_tx_ack(dev);
  538. }
  539. /*
  540. * The Tx ring has been full longer than the watchdog timeout
  541. * value. The transmitter must be hung?
  542. */
  543. static void au1k_tx_timeout(struct net_device *dev)
  544. {
  545. u32 speed;
  546. struct au1k_private *aup = netdev_priv(dev);
  547. printk(KERN_ERR "%s: tx timeout\n", dev->name);
  548. speed = aup->speed;
  549. aup->speed = 0;
  550. au1k_irda_set_speed(dev, speed);
  551. aup->tx_full = 0;
  552. netif_wake_queue(dev);
  553. }
  554. /*
  555. * Set the IrDA communications speed.
  556. */
  557. static int
  558. au1k_irda_set_speed(struct net_device *dev, int speed)
  559. {
  560. unsigned long flags;
  561. struct au1k_private *aup = netdev_priv(dev);
  562. u32 control;
  563. int ret = 0, timeout = 10, i;
  564. volatile ring_dest_t *ptxd;
  565. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  566. unsigned long irda_resets;
  567. #endif
  568. if (speed == aup->speed)
  569. return ret;
  570. spin_lock_irqsave(&ir_lock, flags);
  571. /* disable PHY first */
  572. writel(read_ir_reg(IR_ENABLE) & ~0x8000, IR_ENABLE);
  573. /* disable RX/TX */
  574. writel(read_ir_reg(IR_CONFIG_1) & ~(IR_RX_ENABLE|IR_TX_ENABLE),
  575. IR_CONFIG_1);
  576. au_sync_delay(1);
  577. while (read_ir_reg(IR_ENABLE) & (IR_RX_STATUS | IR_TX_STATUS)) {
  578. mdelay(1);
  579. if (!timeout--) {
  580. printk(KERN_ERR "%s: rx/tx disable timeout\n",
  581. dev->name);
  582. break;
  583. }
  584. }
  585. /* disable DMA */
  586. writel(read_ir_reg(IR_CONFIG_1) & ~IR_DMA_ENABLE, IR_CONFIG_1);
  587. au_sync_delay(1);
  588. /*
  589. * After we disable tx/rx. the index pointers
  590. * go back to zero.
  591. */
  592. aup->tx_head = aup->tx_tail = aup->rx_head = 0;
  593. for (i=0; i<NUM_IR_DESC; i++) {
  594. ptxd = aup->tx_ring[i];
  595. ptxd->flags = 0;
  596. ptxd->count_0 = 0;
  597. ptxd->count_1 = 0;
  598. }
  599. for (i=0; i<NUM_IR_DESC; i++) {
  600. ptxd = aup->rx_ring[i];
  601. ptxd->count_0 = 0;
  602. ptxd->count_1 = 0;
  603. ptxd->flags = AU_OWN;
  604. }
  605. if (speed == 4000000) {
  606. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  607. bcsr->resets |= BCSR_RESETS_FIR_SEL;
  608. #else /* Pb1000 and Pb1100 */
  609. writel(1<<13, CPLD_AUX1);
  610. #endif
  611. }
  612. else {
  613. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  614. bcsr->resets &= ~BCSR_RESETS_FIR_SEL;
  615. #else /* Pb1000 and Pb1100 */
  616. writel(readl(CPLD_AUX1) & ~(1<<13), CPLD_AUX1);
  617. #endif
  618. }
  619. switch (speed) {
  620. case 9600:
  621. writel(11<<10 | 12<<5, IR_WRITE_PHY_CONFIG);
  622. writel(IR_SIR_MODE, IR_CONFIG_1);
  623. break;
  624. case 19200:
  625. writel(5<<10 | 12<<5, IR_WRITE_PHY_CONFIG);
  626. writel(IR_SIR_MODE, IR_CONFIG_1);
  627. break;
  628. case 38400:
  629. writel(2<<10 | 12<<5, IR_WRITE_PHY_CONFIG);
  630. writel(IR_SIR_MODE, IR_CONFIG_1);
  631. break;
  632. case 57600:
  633. writel(1<<10 | 12<<5, IR_WRITE_PHY_CONFIG);
  634. writel(IR_SIR_MODE, IR_CONFIG_1);
  635. break;
  636. case 115200:
  637. writel(12<<5, IR_WRITE_PHY_CONFIG);
  638. writel(IR_SIR_MODE, IR_CONFIG_1);
  639. break;
  640. case 4000000:
  641. writel(0xF, IR_WRITE_PHY_CONFIG);
  642. writel(IR_FIR|IR_DMA_ENABLE|IR_RX_ENABLE, IR_CONFIG_1);
  643. break;
  644. default:
  645. printk(KERN_ERR "%s unsupported speed %x\n", dev->name, speed);
  646. ret = -EINVAL;
  647. break;
  648. }
  649. aup->speed = speed;
  650. writel(read_ir_reg(IR_ENABLE) | 0x8000, IR_ENABLE);
  651. au_sync();
  652. control = read_ir_reg(IR_ENABLE);
  653. writel(0, IR_RING_PROMPT);
  654. au_sync();
  655. if (control & (1<<14)) {
  656. printk(KERN_ERR "%s: configuration error\n", dev->name);
  657. }
  658. else {
  659. if (control & (1<<11))
  660. printk(KERN_DEBUG "%s Valid SIR config\n", dev->name);
  661. if (control & (1<<12))
  662. printk(KERN_DEBUG "%s Valid MIR config\n", dev->name);
  663. if (control & (1<<13))
  664. printk(KERN_DEBUG "%s Valid FIR config\n", dev->name);
  665. if (control & (1<<10))
  666. printk(KERN_DEBUG "%s TX enabled\n", dev->name);
  667. if (control & (1<<9))
  668. printk(KERN_DEBUG "%s RX enabled\n", dev->name);
  669. }
  670. spin_unlock_irqrestore(&ir_lock, flags);
  671. return ret;
  672. }
  673. static int
  674. au1k_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
  675. {
  676. struct if_irda_req *rq = (struct if_irda_req *)ifreq;
  677. struct au1k_private *aup = netdev_priv(dev);
  678. int ret = -EOPNOTSUPP;
  679. switch (cmd) {
  680. case SIOCSBANDWIDTH:
  681. if (capable(CAP_NET_ADMIN)) {
  682. /*
  683. * We are unable to set the speed if the
  684. * device is not running.
  685. */
  686. if (aup->open)
  687. ret = au1k_irda_set_speed(dev,
  688. rq->ifr_baudrate);
  689. else {
  690. printk(KERN_ERR "%s ioctl: !netif_running\n",
  691. dev->name);
  692. ret = 0;
  693. }
  694. }
  695. break;
  696. case SIOCSMEDIABUSY:
  697. ret = -EPERM;
  698. if (capable(CAP_NET_ADMIN)) {
  699. irda_device_set_media_busy(dev, TRUE);
  700. ret = 0;
  701. }
  702. break;
  703. case SIOCGRECEIVING:
  704. rq->ifr_receiving = 0;
  705. break;
  706. default:
  707. break;
  708. }
  709. return ret;
  710. }
  711. static struct net_device_stats *au1k_irda_stats(struct net_device *dev)
  712. {
  713. struct au1k_private *aup = netdev_priv(dev);
  714. return &aup->stats;
  715. }
  716. MODULE_AUTHOR("Pete Popov <ppopov@mvista.com>");
  717. MODULE_DESCRIPTION("Au1000 IrDA Device Driver");
  718. module_init(au1k_irda_init);
  719. module_exit(au1k_irda_exit);