au1k_ir.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 int au1k_irda_ioctl(struct net_device *, struct ifreq *, int);
  54. static int au1k_irda_set_speed(struct net_device *dev, int speed);
  55. static void *dma_alloc(size_t, dma_addr_t *);
  56. static void dma_free(void *, size_t);
  57. static int qos_mtt_bits = 0x07; /* 1 ms or more */
  58. static struct net_device *ir_devs[NUM_IR_IFF];
  59. static char version[] __devinitdata =
  60. "au1k_ircc:1.2 ppopov@mvista.com\n";
  61. #define RUN_AT(x) (jiffies + (x))
  62. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  63. static BCSR * const bcsr = (BCSR *)0xAE000000;
  64. #endif
  65. static DEFINE_SPINLOCK(ir_lock);
  66. /*
  67. * IrDA peripheral bug. You have to read the register
  68. * twice to get the right value.
  69. */
  70. u32 read_ir_reg(u32 addr)
  71. {
  72. readl(addr);
  73. return readl(addr);
  74. }
  75. /*
  76. * Buffer allocation/deallocation routines. The buffer descriptor returned
  77. * has the virtual and dma address of a buffer suitable for
  78. * both, receive and transmit operations.
  79. */
  80. static db_dest_t *GetFreeDB(struct au1k_private *aup)
  81. {
  82. db_dest_t *pDB;
  83. pDB = aup->pDBfree;
  84. if (pDB) {
  85. aup->pDBfree = pDB->pnext;
  86. }
  87. return pDB;
  88. }
  89. static void ReleaseDB(struct au1k_private *aup, db_dest_t *pDB)
  90. {
  91. db_dest_t *pDBfree = aup->pDBfree;
  92. if (pDBfree)
  93. pDBfree->pnext = pDB;
  94. aup->pDBfree = pDB;
  95. }
  96. /*
  97. DMA memory allocation, derived from pci_alloc_consistent.
  98. However, the Au1000 data cache is coherent (when programmed
  99. so), therefore we return KSEG0 address, not KSEG1.
  100. */
  101. static void *dma_alloc(size_t size, dma_addr_t * dma_handle)
  102. {
  103. void *ret;
  104. int gfp = GFP_ATOMIC | GFP_DMA;
  105. ret = (void *) __get_free_pages(gfp, get_order(size));
  106. if (ret != NULL) {
  107. memset(ret, 0, size);
  108. *dma_handle = virt_to_bus(ret);
  109. ret = (void *)KSEG0ADDR(ret);
  110. }
  111. return ret;
  112. }
  113. static void dma_free(void *vaddr, size_t size)
  114. {
  115. vaddr = (void *)KSEG0ADDR(vaddr);
  116. free_pages((unsigned long) vaddr, get_order(size));
  117. }
  118. static void
  119. setup_hw_rings(struct au1k_private *aup, u32 rx_base, u32 tx_base)
  120. {
  121. int i;
  122. for (i=0; i<NUM_IR_DESC; i++) {
  123. aup->rx_ring[i] = (volatile ring_dest_t *)
  124. (rx_base + sizeof(ring_dest_t)*i);
  125. }
  126. for (i=0; i<NUM_IR_DESC; i++) {
  127. aup->tx_ring[i] = (volatile ring_dest_t *)
  128. (tx_base + sizeof(ring_dest_t)*i);
  129. }
  130. }
  131. static int au1k_irda_init(void)
  132. {
  133. static unsigned version_printed = 0;
  134. struct au1k_private *aup;
  135. struct net_device *dev;
  136. int err;
  137. if (version_printed++ == 0) printk(version);
  138. dev = alloc_irdadev(sizeof(struct au1k_private));
  139. if (!dev)
  140. return -ENOMEM;
  141. dev->irq = AU1000_IRDA_RX_INT; /* TX has its own interrupt */
  142. err = au1k_irda_net_init(dev);
  143. if (err)
  144. goto out;
  145. err = register_netdev(dev);
  146. if (err)
  147. goto out1;
  148. ir_devs[0] = dev;
  149. printk(KERN_INFO "IrDA: Registered device %s\n", dev->name);
  150. return 0;
  151. out1:
  152. aup = netdev_priv(dev);
  153. dma_free((void *)aup->db[0].vaddr,
  154. MAX_BUF_SIZE * 2*NUM_IR_DESC);
  155. dma_free((void *)aup->rx_ring[0],
  156. 2 * MAX_NUM_IR_DESC*(sizeof(ring_dest_t)));
  157. kfree(aup->rx_buff.head);
  158. out:
  159. free_netdev(dev);
  160. return err;
  161. }
  162. static int au1k_irda_init_iobuf(iobuff_t *io, int size)
  163. {
  164. io->head = kmalloc(size, GFP_KERNEL);
  165. if (io->head != NULL) {
  166. io->truesize = size;
  167. io->in_frame = FALSE;
  168. io->state = OUTSIDE_FRAME;
  169. io->data = io->head;
  170. }
  171. return io->head ? 0 : -ENOMEM;
  172. }
  173. static int au1k_irda_net_init(struct net_device *dev)
  174. {
  175. struct au1k_private *aup = netdev_priv(dev);
  176. int i, retval = 0, err;
  177. db_dest_t *pDB, *pDBfree;
  178. dma_addr_t temp;
  179. err = au1k_irda_init_iobuf(&aup->rx_buff, 14384);
  180. if (err)
  181. goto out1;
  182. dev->open = au1k_irda_start;
  183. dev->hard_start_xmit = au1k_irda_hard_xmit;
  184. dev->stop = au1k_irda_stop;
  185. dev->do_ioctl = au1k_irda_ioctl;
  186. dev->tx_timeout = au1k_tx_timeout;
  187. irda_init_max_qos_capabilies(&aup->qos);
  188. /* The only value we must override it the baudrate */
  189. aup->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
  190. IR_115200|IR_576000 |(IR_4000000 << 8);
  191. aup->qos.min_turn_time.bits = qos_mtt_bits;
  192. irda_qos_bits_to_value(&aup->qos);
  193. retval = -ENOMEM;
  194. /* Tx ring follows rx ring + 512 bytes */
  195. /* we need a 1k aligned buffer */
  196. aup->rx_ring[0] = (ring_dest_t *)
  197. dma_alloc(2*MAX_NUM_IR_DESC*(sizeof(ring_dest_t)), &temp);
  198. if (!aup->rx_ring[0])
  199. goto out2;
  200. /* allocate the data buffers */
  201. aup->db[0].vaddr =
  202. (void *)dma_alloc(MAX_BUF_SIZE * 2*NUM_IR_DESC, &temp);
  203. if (!aup->db[0].vaddr)
  204. goto out3;
  205. setup_hw_rings(aup, (u32)aup->rx_ring[0], (u32)aup->rx_ring[0] + 512);
  206. pDBfree = NULL;
  207. pDB = aup->db;
  208. for (i=0; i<(2*NUM_IR_DESC); i++) {
  209. pDB->pnext = pDBfree;
  210. pDBfree = pDB;
  211. pDB->vaddr =
  212. (u32 *)((unsigned)aup->db[0].vaddr + MAX_BUF_SIZE*i);
  213. pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
  214. pDB++;
  215. }
  216. aup->pDBfree = pDBfree;
  217. /* attach a data buffer to each descriptor */
  218. for (i=0; i<NUM_IR_DESC; i++) {
  219. pDB = GetFreeDB(aup);
  220. if (!pDB) goto out;
  221. aup->rx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
  222. aup->rx_ring[i]->addr_1 = (u8)((pDB->dma_addr>>8) & 0xff);
  223. aup->rx_ring[i]->addr_2 = (u8)((pDB->dma_addr>>16) & 0xff);
  224. aup->rx_ring[i]->addr_3 = (u8)((pDB->dma_addr>>24) & 0xff);
  225. aup->rx_db_inuse[i] = pDB;
  226. }
  227. for (i=0; i<NUM_IR_DESC; i++) {
  228. pDB = GetFreeDB(aup);
  229. if (!pDB) goto out;
  230. aup->tx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
  231. aup->tx_ring[i]->addr_1 = (u8)((pDB->dma_addr>>8) & 0xff);
  232. aup->tx_ring[i]->addr_2 = (u8)((pDB->dma_addr>>16) & 0xff);
  233. aup->tx_ring[i]->addr_3 = (u8)((pDB->dma_addr>>24) & 0xff);
  234. aup->tx_ring[i]->count_0 = 0;
  235. aup->tx_ring[i]->count_1 = 0;
  236. aup->tx_ring[i]->flags = 0;
  237. aup->tx_db_inuse[i] = pDB;
  238. }
  239. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  240. /* power on */
  241. bcsr->resets &= ~BCSR_RESETS_IRDA_MODE_MASK;
  242. bcsr->resets |= BCSR_RESETS_IRDA_MODE_FULL;
  243. au_sync();
  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 0;
  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 1;
  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 1;
  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. dev->trans_start = jiffies;
  459. return 0;
  460. }
  461. static inline void
  462. update_rx_stats(struct net_device *dev, u32 status, u32 count)
  463. {
  464. struct au1k_private *aup = netdev_priv(dev);
  465. struct net_device_stats *ps = &aup->stats;
  466. ps->rx_packets++;
  467. if (status & IR_RX_ERROR) {
  468. ps->rx_errors++;
  469. if (status & (IR_PHY_ERROR|IR_FIFO_OVER))
  470. ps->rx_missed_errors++;
  471. if (status & IR_MAX_LEN)
  472. ps->rx_length_errors++;
  473. if (status & IR_CRC_ERROR)
  474. ps->rx_crc_errors++;
  475. }
  476. else
  477. ps->rx_bytes += count;
  478. }
  479. /*
  480. * Au1000 receive routine.
  481. */
  482. static int au1k_irda_rx(struct net_device *dev)
  483. {
  484. struct au1k_private *aup = netdev_priv(dev);
  485. struct sk_buff *skb;
  486. volatile ring_dest_t *prxd;
  487. u32 flags, count;
  488. db_dest_t *pDB;
  489. prxd = aup->rx_ring[aup->rx_head];
  490. flags = prxd->flags;
  491. while (!(flags & AU_OWN)) {
  492. pDB = aup->rx_db_inuse[aup->rx_head];
  493. count = prxd->count_1<<8 | prxd->count_0;
  494. if (!(flags & IR_RX_ERROR)) {
  495. /* good frame */
  496. update_rx_stats(dev, flags, count);
  497. skb=alloc_skb(count+1,GFP_ATOMIC);
  498. if (skb == NULL) {
  499. aup->netdev->stats.rx_dropped++;
  500. continue;
  501. }
  502. skb_reserve(skb, 1);
  503. if (aup->speed == 4000000)
  504. skb_put(skb, count);
  505. else
  506. skb_put(skb, count-2);
  507. skb_copy_to_linear_data(skb, pDB->vaddr, count - 2);
  508. skb->dev = dev;
  509. skb_reset_mac_header(skb);
  510. skb->protocol = htons(ETH_P_IRDA);
  511. netif_rx(skb);
  512. prxd->count_0 = 0;
  513. prxd->count_1 = 0;
  514. }
  515. prxd->flags |= AU_OWN;
  516. aup->rx_head = (aup->rx_head + 1) & (NUM_IR_DESC - 1);
  517. writel(0, IR_RING_PROMPT);
  518. au_sync();
  519. /* next descriptor */
  520. prxd = aup->rx_ring[aup->rx_head];
  521. flags = prxd->flags;
  522. }
  523. return 0;
  524. }
  525. static irqreturn_t au1k_irda_interrupt(int dummy, void *dev_id)
  526. {
  527. struct net_device *dev = dev_id;
  528. writel(0, IR_INT_CLEAR); /* ack irda interrupts */
  529. au1k_irda_rx(dev);
  530. au1k_tx_ack(dev);
  531. return IRQ_HANDLED;
  532. }
  533. /*
  534. * The Tx ring has been full longer than the watchdog timeout
  535. * value. The transmitter must be hung?
  536. */
  537. static void au1k_tx_timeout(struct net_device *dev)
  538. {
  539. u32 speed;
  540. struct au1k_private *aup = netdev_priv(dev);
  541. printk(KERN_ERR "%s: tx timeout\n", dev->name);
  542. speed = aup->speed;
  543. aup->speed = 0;
  544. au1k_irda_set_speed(dev, speed);
  545. aup->tx_full = 0;
  546. netif_wake_queue(dev);
  547. }
  548. /*
  549. * Set the IrDA communications speed.
  550. */
  551. static int
  552. au1k_irda_set_speed(struct net_device *dev, int speed)
  553. {
  554. unsigned long flags;
  555. struct au1k_private *aup = netdev_priv(dev);
  556. u32 control;
  557. int ret = 0, timeout = 10, i;
  558. volatile ring_dest_t *ptxd;
  559. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  560. unsigned long irda_resets;
  561. #endif
  562. if (speed == aup->speed)
  563. return ret;
  564. spin_lock_irqsave(&ir_lock, flags);
  565. /* disable PHY first */
  566. writel(read_ir_reg(IR_ENABLE) & ~0x8000, IR_ENABLE);
  567. /* disable RX/TX */
  568. writel(read_ir_reg(IR_CONFIG_1) & ~(IR_RX_ENABLE|IR_TX_ENABLE),
  569. IR_CONFIG_1);
  570. au_sync_delay(1);
  571. while (read_ir_reg(IR_ENABLE) & (IR_RX_STATUS | IR_TX_STATUS)) {
  572. mdelay(1);
  573. if (!timeout--) {
  574. printk(KERN_ERR "%s: rx/tx disable timeout\n",
  575. dev->name);
  576. break;
  577. }
  578. }
  579. /* disable DMA */
  580. writel(read_ir_reg(IR_CONFIG_1) & ~IR_DMA_ENABLE, IR_CONFIG_1);
  581. au_sync_delay(1);
  582. /*
  583. * After we disable tx/rx. the index pointers
  584. * go back to zero.
  585. */
  586. aup->tx_head = aup->tx_tail = aup->rx_head = 0;
  587. for (i=0; i<NUM_IR_DESC; i++) {
  588. ptxd = aup->tx_ring[i];
  589. ptxd->flags = 0;
  590. ptxd->count_0 = 0;
  591. ptxd->count_1 = 0;
  592. }
  593. for (i=0; i<NUM_IR_DESC; i++) {
  594. ptxd = aup->rx_ring[i];
  595. ptxd->count_0 = 0;
  596. ptxd->count_1 = 0;
  597. ptxd->flags = AU_OWN;
  598. }
  599. if (speed == 4000000) {
  600. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  601. bcsr->resets |= BCSR_RESETS_FIR_SEL;
  602. #else /* Pb1000 and Pb1100 */
  603. writel(1<<13, CPLD_AUX1);
  604. #endif
  605. }
  606. else {
  607. #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
  608. bcsr->resets &= ~BCSR_RESETS_FIR_SEL;
  609. #else /* Pb1000 and Pb1100 */
  610. writel(readl(CPLD_AUX1) & ~(1<<13), CPLD_AUX1);
  611. #endif
  612. }
  613. switch (speed) {
  614. case 9600:
  615. writel(11<<10 | 12<<5, IR_WRITE_PHY_CONFIG);
  616. writel(IR_SIR_MODE, IR_CONFIG_1);
  617. break;
  618. case 19200:
  619. writel(5<<10 | 12<<5, IR_WRITE_PHY_CONFIG);
  620. writel(IR_SIR_MODE, IR_CONFIG_1);
  621. break;
  622. case 38400:
  623. writel(2<<10 | 12<<5, IR_WRITE_PHY_CONFIG);
  624. writel(IR_SIR_MODE, IR_CONFIG_1);
  625. break;
  626. case 57600:
  627. writel(1<<10 | 12<<5, IR_WRITE_PHY_CONFIG);
  628. writel(IR_SIR_MODE, IR_CONFIG_1);
  629. break;
  630. case 115200:
  631. writel(12<<5, IR_WRITE_PHY_CONFIG);
  632. writel(IR_SIR_MODE, IR_CONFIG_1);
  633. break;
  634. case 4000000:
  635. writel(0xF, IR_WRITE_PHY_CONFIG);
  636. writel(IR_FIR|IR_DMA_ENABLE|IR_RX_ENABLE, IR_CONFIG_1);
  637. break;
  638. default:
  639. printk(KERN_ERR "%s unsupported speed %x\n", dev->name, speed);
  640. ret = -EINVAL;
  641. break;
  642. }
  643. aup->speed = speed;
  644. writel(read_ir_reg(IR_ENABLE) | 0x8000, IR_ENABLE);
  645. au_sync();
  646. control = read_ir_reg(IR_ENABLE);
  647. writel(0, IR_RING_PROMPT);
  648. au_sync();
  649. if (control & (1<<14)) {
  650. printk(KERN_ERR "%s: configuration error\n", dev->name);
  651. }
  652. else {
  653. if (control & (1<<11))
  654. printk(KERN_DEBUG "%s Valid SIR config\n", dev->name);
  655. if (control & (1<<12))
  656. printk(KERN_DEBUG "%s Valid MIR config\n", dev->name);
  657. if (control & (1<<13))
  658. printk(KERN_DEBUG "%s Valid FIR config\n", dev->name);
  659. if (control & (1<<10))
  660. printk(KERN_DEBUG "%s TX enabled\n", dev->name);
  661. if (control & (1<<9))
  662. printk(KERN_DEBUG "%s RX enabled\n", dev->name);
  663. }
  664. spin_unlock_irqrestore(&ir_lock, flags);
  665. return ret;
  666. }
  667. static int
  668. au1k_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
  669. {
  670. struct if_irda_req *rq = (struct if_irda_req *)ifreq;
  671. struct au1k_private *aup = netdev_priv(dev);
  672. int ret = -EOPNOTSUPP;
  673. switch (cmd) {
  674. case SIOCSBANDWIDTH:
  675. if (capable(CAP_NET_ADMIN)) {
  676. /*
  677. * We are unable to set the speed if the
  678. * device is not running.
  679. */
  680. if (aup->open)
  681. ret = au1k_irda_set_speed(dev,
  682. rq->ifr_baudrate);
  683. else {
  684. printk(KERN_ERR "%s ioctl: !netif_running\n",
  685. dev->name);
  686. ret = 0;
  687. }
  688. }
  689. break;
  690. case SIOCSMEDIABUSY:
  691. ret = -EPERM;
  692. if (capable(CAP_NET_ADMIN)) {
  693. irda_device_set_media_busy(dev, TRUE);
  694. ret = 0;
  695. }
  696. break;
  697. case SIOCGRECEIVING:
  698. rq->ifr_receiving = 0;
  699. break;
  700. default:
  701. break;
  702. }
  703. return ret;
  704. }
  705. MODULE_AUTHOR("Pete Popov <ppopov@mvista.com>");
  706. MODULE_DESCRIPTION("Au1000 IrDA Device Driver");
  707. module_init(au1k_irda_init);
  708. module_exit(au1k_irda_exit);