w83977af_ir.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303
  1. /*********************************************************************
  2. *
  3. * Filename: w83977af_ir.c
  4. * Version: 1.0
  5. * Description: FIR driver for the Winbond W83977AF Super I/O chip
  6. * Status: Experimental.
  7. * Author: Paul VanderSpek
  8. * Created at: Wed Nov 4 11:46:16 1998
  9. * Modified at: Fri Jan 28 12:10:59 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>
  13. * Copyright (c) 1998-1999 Rebel.com
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * Neither Paul VanderSpek nor Rebel.com admit liability nor provide
  21. * warranty for any of this software. This material is provided "AS-IS"
  22. * and at no charge.
  23. *
  24. * If you find bugs in this file, its very likely that the same bug
  25. * will also be in pc87108.c since the implementations are quite
  26. * similar.
  27. *
  28. * Notice that all functions that needs to access the chip in _any_
  29. * way, must save BSR register on entry, and restore it on exit.
  30. * It is _very_ important to follow this policy!
  31. *
  32. * __u8 bank;
  33. *
  34. * bank = inb( iobase+BSR);
  35. *
  36. * do_your_stuff_here();
  37. *
  38. * outb( bank, iobase+BSR);
  39. *
  40. ********************************************************************/
  41. #include <linux/module.h>
  42. #include <linux/kernel.h>
  43. #include <linux/types.h>
  44. #include <linux/skbuff.h>
  45. #include <linux/netdevice.h>
  46. #include <linux/ioport.h>
  47. #include <linux/delay.h>
  48. #include <linux/init.h>
  49. #include <linux/interrupt.h>
  50. #include <linux/rtnetlink.h>
  51. #include <linux/dma-mapping.h>
  52. #include <linux/gfp.h>
  53. #include <asm/io.h>
  54. #include <asm/dma.h>
  55. #include <asm/byteorder.h>
  56. #include <net/irda/irda.h>
  57. #include <net/irda/wrapper.h>
  58. #include <net/irda/irda_device.h>
  59. #include "w83977af.h"
  60. #include "w83977af_ir.h"
  61. #define CONFIG_USE_W977_PNP /* Currently needed */
  62. #define PIO_MAX_SPEED 115200
  63. static char *driver_name = "w83977af_ir";
  64. static int qos_mtt_bits = 0x07; /* 1 ms or more */
  65. #define CHIP_IO_EXTENT 8
  66. static unsigned int io[] = { 0x180, ~0, ~0, ~0 };
  67. #ifdef CONFIG_ARCH_NETWINDER /* Adjust to NetWinder differences */
  68. static unsigned int irq[] = { 6, 0, 0, 0 };
  69. #else
  70. static unsigned int irq[] = { 11, 0, 0, 0 };
  71. #endif
  72. static unsigned int dma[] = { 1, 0, 0, 0 };
  73. static unsigned int efbase[] = { W977_EFIO_BASE, W977_EFIO2_BASE };
  74. static unsigned int efio = W977_EFIO_BASE;
  75. static struct w83977af_ir *dev_self[] = { NULL, NULL, NULL, NULL};
  76. /* Some prototypes */
  77. static int w83977af_open(int i, unsigned int iobase, unsigned int irq,
  78. unsigned int dma);
  79. static int w83977af_close(struct w83977af_ir *self);
  80. static int w83977af_probe(int iobase, int irq, int dma);
  81. static int w83977af_dma_receive(struct w83977af_ir *self);
  82. static int w83977af_dma_receive_complete(struct w83977af_ir *self);
  83. static netdev_tx_t w83977af_hard_xmit(struct sk_buff *skb,
  84. struct net_device *dev);
  85. static int w83977af_pio_write(int iobase, __u8 *buf, int len, int fifo_size);
  86. static void w83977af_dma_write(struct w83977af_ir *self, int iobase);
  87. static void w83977af_change_speed(struct w83977af_ir *self, __u32 speed);
  88. static int w83977af_is_receiving(struct w83977af_ir *self);
  89. static int w83977af_net_open(struct net_device *dev);
  90. static int w83977af_net_close(struct net_device *dev);
  91. static int w83977af_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  92. /*
  93. * Function w83977af_init ()
  94. *
  95. * Initialize chip. Just try to find out how many chips we are dealing with
  96. * and where they are
  97. */
  98. static int __init w83977af_init(void)
  99. {
  100. int i;
  101. IRDA_DEBUG(0, "%s()\n", __func__ );
  102. for (i=0; i < ARRAY_SIZE(dev_self) && io[i] < 2000; i++) {
  103. if (w83977af_open(i, io[i], irq[i], dma[i]) == 0)
  104. return 0;
  105. }
  106. return -ENODEV;
  107. }
  108. /*
  109. * Function w83977af_cleanup ()
  110. *
  111. * Close all configured chips
  112. *
  113. */
  114. static void __exit w83977af_cleanup(void)
  115. {
  116. int i;
  117. IRDA_DEBUG(4, "%s()\n", __func__ );
  118. for (i=0; i < ARRAY_SIZE(dev_self); i++) {
  119. if (dev_self[i])
  120. w83977af_close(dev_self[i]);
  121. }
  122. }
  123. static const struct net_device_ops w83977_netdev_ops = {
  124. .ndo_open = w83977af_net_open,
  125. .ndo_stop = w83977af_net_close,
  126. .ndo_start_xmit = w83977af_hard_xmit,
  127. .ndo_do_ioctl = w83977af_net_ioctl,
  128. };
  129. /*
  130. * Function w83977af_open (iobase, irq)
  131. *
  132. * Open driver instance
  133. *
  134. */
  135. static int w83977af_open(int i, unsigned int iobase, unsigned int irq,
  136. unsigned int dma)
  137. {
  138. struct net_device *dev;
  139. struct w83977af_ir *self;
  140. int err;
  141. IRDA_DEBUG(0, "%s()\n", __func__ );
  142. /* Lock the port that we need */
  143. if (!request_region(iobase, CHIP_IO_EXTENT, driver_name)) {
  144. IRDA_DEBUG(0, "%s(), can't get iobase of 0x%03x\n",
  145. __func__ , iobase);
  146. return -ENODEV;
  147. }
  148. if (w83977af_probe(iobase, irq, dma) == -1) {
  149. err = -1;
  150. goto err_out;
  151. }
  152. /*
  153. * Allocate new instance of the driver
  154. */
  155. dev = alloc_irdadev(sizeof(struct w83977af_ir));
  156. if (dev == NULL) {
  157. printk( KERN_ERR "IrDA: Can't allocate memory for "
  158. "IrDA control block!\n");
  159. err = -ENOMEM;
  160. goto err_out;
  161. }
  162. self = netdev_priv(dev);
  163. spin_lock_init(&self->lock);
  164. /* Initialize IO */
  165. self->io.fir_base = iobase;
  166. self->io.irq = irq;
  167. self->io.fir_ext = CHIP_IO_EXTENT;
  168. self->io.dma = dma;
  169. self->io.fifo_size = 32;
  170. /* Initialize QoS for this device */
  171. irda_init_max_qos_capabilies(&self->qos);
  172. /* The only value we must override it the baudrate */
  173. /* FIXME: The HP HDLS-1100 does not support 1152000! */
  174. self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
  175. IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8);
  176. /* The HP HDLS-1100 needs 1 ms according to the specs */
  177. self->qos.min_turn_time.bits = qos_mtt_bits;
  178. irda_qos_bits_to_value(&self->qos);
  179. /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
  180. self->rx_buff.truesize = 14384;
  181. self->tx_buff.truesize = 4000;
  182. /* Allocate memory if needed */
  183. self->rx_buff.head =
  184. dma_zalloc_coherent(NULL, self->rx_buff.truesize,
  185. &self->rx_buff_dma, GFP_KERNEL);
  186. if (self->rx_buff.head == NULL) {
  187. err = -ENOMEM;
  188. goto err_out1;
  189. }
  190. self->tx_buff.head =
  191. dma_zalloc_coherent(NULL, self->tx_buff.truesize,
  192. &self->tx_buff_dma, GFP_KERNEL);
  193. if (self->tx_buff.head == NULL) {
  194. err = -ENOMEM;
  195. goto err_out2;
  196. }
  197. self->rx_buff.in_frame = FALSE;
  198. self->rx_buff.state = OUTSIDE_FRAME;
  199. self->tx_buff.data = self->tx_buff.head;
  200. self->rx_buff.data = self->rx_buff.head;
  201. self->netdev = dev;
  202. dev->netdev_ops = &w83977_netdev_ops;
  203. err = register_netdev(dev);
  204. if (err) {
  205. IRDA_ERROR("%s(), register_netdevice() failed!\n", __func__);
  206. goto err_out3;
  207. }
  208. IRDA_MESSAGE("IrDA: Registered device %s\n", dev->name);
  209. /* Need to store self somewhere */
  210. dev_self[i] = self;
  211. return 0;
  212. err_out3:
  213. dma_free_coherent(NULL, self->tx_buff.truesize,
  214. self->tx_buff.head, self->tx_buff_dma);
  215. err_out2:
  216. dma_free_coherent(NULL, self->rx_buff.truesize,
  217. self->rx_buff.head, self->rx_buff_dma);
  218. err_out1:
  219. free_netdev(dev);
  220. err_out:
  221. release_region(iobase, CHIP_IO_EXTENT);
  222. return err;
  223. }
  224. /*
  225. * Function w83977af_close (self)
  226. *
  227. * Close driver instance
  228. *
  229. */
  230. static int w83977af_close(struct w83977af_ir *self)
  231. {
  232. int iobase;
  233. IRDA_DEBUG(0, "%s()\n", __func__ );
  234. iobase = self->io.fir_base;
  235. #ifdef CONFIG_USE_W977_PNP
  236. /* enter PnP configuration mode */
  237. w977_efm_enter(efio);
  238. w977_select_device(W977_DEVICE_IR, efio);
  239. /* Deactivate device */
  240. w977_write_reg(0x30, 0x00, efio);
  241. w977_efm_exit(efio);
  242. #endif /* CONFIG_USE_W977_PNP */
  243. /* Remove netdevice */
  244. unregister_netdev(self->netdev);
  245. /* Release the PORT that this driver is using */
  246. IRDA_DEBUG(0 , "%s(), Releasing Region %03x\n",
  247. __func__ , self->io.fir_base);
  248. release_region(self->io.fir_base, self->io.fir_ext);
  249. if (self->tx_buff.head)
  250. dma_free_coherent(NULL, self->tx_buff.truesize,
  251. self->tx_buff.head, self->tx_buff_dma);
  252. if (self->rx_buff.head)
  253. dma_free_coherent(NULL, self->rx_buff.truesize,
  254. self->rx_buff.head, self->rx_buff_dma);
  255. free_netdev(self->netdev);
  256. return 0;
  257. }
  258. static int w83977af_probe(int iobase, int irq, int dma)
  259. {
  260. int version;
  261. int i;
  262. for (i=0; i < 2; i++) {
  263. IRDA_DEBUG( 0, "%s()\n", __func__ );
  264. #ifdef CONFIG_USE_W977_PNP
  265. /* Enter PnP configuration mode */
  266. w977_efm_enter(efbase[i]);
  267. w977_select_device(W977_DEVICE_IR, efbase[i]);
  268. /* Configure PnP port, IRQ, and DMA channel */
  269. w977_write_reg(0x60, (iobase >> 8) & 0xff, efbase[i]);
  270. w977_write_reg(0x61, (iobase) & 0xff, efbase[i]);
  271. w977_write_reg(0x70, irq, efbase[i]);
  272. #ifdef CONFIG_ARCH_NETWINDER
  273. /* Netwinder uses 1 higher than Linux */
  274. w977_write_reg(0x74, dma+1, efbase[i]);
  275. #else
  276. w977_write_reg(0x74, dma, efbase[i]);
  277. #endif /* CONFIG_ARCH_NETWINDER */
  278. w977_write_reg(0x75, 0x04, efbase[i]); /* Disable Tx DMA */
  279. /* Set append hardware CRC, enable IR bank selection */
  280. w977_write_reg(0xf0, APEDCRC|ENBNKSEL, efbase[i]);
  281. /* Activate device */
  282. w977_write_reg(0x30, 0x01, efbase[i]);
  283. w977_efm_exit(efbase[i]);
  284. #endif /* CONFIG_USE_W977_PNP */
  285. /* Disable Advanced mode */
  286. switch_bank(iobase, SET2);
  287. outb(iobase+2, 0x00);
  288. /* Turn on UART (global) interrupts */
  289. switch_bank(iobase, SET0);
  290. outb(HCR_EN_IRQ, iobase+HCR);
  291. /* Switch to advanced mode */
  292. switch_bank(iobase, SET2);
  293. outb(inb(iobase+ADCR1) | ADCR1_ADV_SL, iobase+ADCR1);
  294. /* Set default IR-mode */
  295. switch_bank(iobase, SET0);
  296. outb(HCR_SIR, iobase+HCR);
  297. /* Read the Advanced IR ID */
  298. switch_bank(iobase, SET3);
  299. version = inb(iobase+AUID);
  300. /* Should be 0x1? */
  301. if (0x10 == (version & 0xf0)) {
  302. efio = efbase[i];
  303. /* Set FIFO size to 32 */
  304. switch_bank(iobase, SET2);
  305. outb(ADCR2_RXFS32|ADCR2_TXFS32, iobase+ADCR2);
  306. /* Set FIFO threshold to TX17, RX16 */
  307. switch_bank(iobase, SET0);
  308. outb(UFR_RXTL|UFR_TXTL|UFR_TXF_RST|UFR_RXF_RST|
  309. UFR_EN_FIFO,iobase+UFR);
  310. /* Receiver frame length */
  311. switch_bank(iobase, SET4);
  312. outb(2048 & 0xff, iobase+6);
  313. outb((2048 >> 8) & 0x1f, iobase+7);
  314. /*
  315. * Init HP HSDL-1100 transceiver.
  316. *
  317. * Set IRX_MSL since we have 2 * receive paths IRRX,
  318. * and IRRXH. Clear IRSL0D since we want IRSL0 * to
  319. * be a input pin used for IRRXH
  320. *
  321. * IRRX pin 37 connected to receiver
  322. * IRTX pin 38 connected to transmitter
  323. * FIRRX pin 39 connected to receiver (IRSL0)
  324. * CIRRX pin 40 connected to pin 37
  325. */
  326. switch_bank(iobase, SET7);
  327. outb(0x40, iobase+7);
  328. IRDA_MESSAGE("W83977AF (IR) driver loaded. "
  329. "Version: 0x%02x\n", version);
  330. return 0;
  331. } else {
  332. /* Try next extented function register address */
  333. IRDA_DEBUG( 0, "%s(), Wrong chip version", __func__ );
  334. }
  335. }
  336. return -1;
  337. }
  338. static void w83977af_change_speed(struct w83977af_ir *self, __u32 speed)
  339. {
  340. int ir_mode = HCR_SIR;
  341. int iobase;
  342. __u8 set;
  343. iobase = self->io.fir_base;
  344. /* Update accounting for new speed */
  345. self->io.speed = speed;
  346. /* Save current bank */
  347. set = inb(iobase+SSR);
  348. /* Disable interrupts */
  349. switch_bank(iobase, SET0);
  350. outb(0, iobase+ICR);
  351. /* Select Set 2 */
  352. switch_bank(iobase, SET2);
  353. outb(0x00, iobase+ABHL);
  354. switch (speed) {
  355. case 9600: outb(0x0c, iobase+ABLL); break;
  356. case 19200: outb(0x06, iobase+ABLL); break;
  357. case 38400: outb(0x03, iobase+ABLL); break;
  358. case 57600: outb(0x02, iobase+ABLL); break;
  359. case 115200: outb(0x01, iobase+ABLL); break;
  360. case 576000:
  361. ir_mode = HCR_MIR_576;
  362. IRDA_DEBUG(0, "%s(), handling baud of 576000\n", __func__ );
  363. break;
  364. case 1152000:
  365. ir_mode = HCR_MIR_1152;
  366. IRDA_DEBUG(0, "%s(), handling baud of 1152000\n", __func__ );
  367. break;
  368. case 4000000:
  369. ir_mode = HCR_FIR;
  370. IRDA_DEBUG(0, "%s(), handling baud of 4000000\n", __func__ );
  371. break;
  372. default:
  373. ir_mode = HCR_FIR;
  374. IRDA_DEBUG(0, "%s(), unknown baud rate of %d\n", __func__ , speed);
  375. break;
  376. }
  377. /* Set speed mode */
  378. switch_bank(iobase, SET0);
  379. outb(ir_mode, iobase+HCR);
  380. /* set FIFO size to 32 */
  381. switch_bank(iobase, SET2);
  382. outb(ADCR2_RXFS32|ADCR2_TXFS32, iobase+ADCR2);
  383. /* set FIFO threshold to TX17, RX16 */
  384. switch_bank(iobase, SET0);
  385. outb(0x00, iobase+UFR); /* Reset */
  386. outb(UFR_EN_FIFO, iobase+UFR); /* First we must enable FIFO */
  387. outb(0xa7, iobase+UFR);
  388. netif_wake_queue(self->netdev);
  389. /* Enable some interrupts so we can receive frames */
  390. switch_bank(iobase, SET0);
  391. if (speed > PIO_MAX_SPEED) {
  392. outb(ICR_EFSFI, iobase+ICR);
  393. w83977af_dma_receive(self);
  394. } else
  395. outb(ICR_ERBRI, iobase+ICR);
  396. /* Restore SSR */
  397. outb(set, iobase+SSR);
  398. }
  399. /*
  400. * Function w83977af_hard_xmit (skb, dev)
  401. *
  402. * Sets up a DMA transfer to send the current frame.
  403. *
  404. */
  405. static netdev_tx_t w83977af_hard_xmit(struct sk_buff *skb,
  406. struct net_device *dev)
  407. {
  408. struct w83977af_ir *self;
  409. __s32 speed;
  410. int iobase;
  411. __u8 set;
  412. int mtt;
  413. self = netdev_priv(dev);
  414. iobase = self->io.fir_base;
  415. IRDA_DEBUG(4, "%s(%ld), skb->len=%d\n", __func__ , jiffies,
  416. (int) skb->len);
  417. /* Lock transmit buffer */
  418. netif_stop_queue(dev);
  419. /* Check if we need to change the speed */
  420. speed = irda_get_next_speed(skb);
  421. if ((speed != self->io.speed) && (speed != -1)) {
  422. /* Check for empty frame */
  423. if (!skb->len) {
  424. w83977af_change_speed(self, speed);
  425. dev_kfree_skb(skb);
  426. return NETDEV_TX_OK;
  427. } else
  428. self->new_speed = speed;
  429. }
  430. /* Save current set */
  431. set = inb(iobase+SSR);
  432. /* Decide if we should use PIO or DMA transfer */
  433. if (self->io.speed > PIO_MAX_SPEED) {
  434. self->tx_buff.data = self->tx_buff.head;
  435. skb_copy_from_linear_data(skb, self->tx_buff.data, skb->len);
  436. self->tx_buff.len = skb->len;
  437. mtt = irda_get_mtt(skb);
  438. IRDA_DEBUG(4, "%s(%ld), mtt=%d\n", __func__ , jiffies, mtt);
  439. if (mtt)
  440. udelay(mtt);
  441. /* Enable DMA interrupt */
  442. switch_bank(iobase, SET0);
  443. outb(ICR_EDMAI, iobase+ICR);
  444. w83977af_dma_write(self, iobase);
  445. } else {
  446. self->tx_buff.data = self->tx_buff.head;
  447. self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
  448. self->tx_buff.truesize);
  449. /* Add interrupt on tx low level (will fire immediately) */
  450. switch_bank(iobase, SET0);
  451. outb(ICR_ETXTHI, iobase+ICR);
  452. }
  453. dev_kfree_skb(skb);
  454. /* Restore set register */
  455. outb(set, iobase+SSR);
  456. return NETDEV_TX_OK;
  457. }
  458. /*
  459. * Function w83977af_dma_write (self, iobase)
  460. *
  461. * Send frame using DMA
  462. *
  463. */
  464. static void w83977af_dma_write(struct w83977af_ir *self, int iobase)
  465. {
  466. __u8 set;
  467. IRDA_DEBUG(4, "%s(), len=%d\n", __func__ , self->tx_buff.len);
  468. /* Save current set */
  469. set = inb(iobase+SSR);
  470. /* Disable DMA */
  471. switch_bank(iobase, SET0);
  472. outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
  473. /* Choose transmit DMA channel */
  474. switch_bank(iobase, SET2);
  475. outb(ADCR1_D_CHSW|/*ADCR1_DMA_F|*/ADCR1_ADV_SL, iobase+ADCR1);
  476. irda_setup_dma(self->io.dma, self->tx_buff_dma, self->tx_buff.len,
  477. DMA_MODE_WRITE);
  478. self->io.direction = IO_XMIT;
  479. /* Enable DMA */
  480. switch_bank(iobase, SET0);
  481. outb(inb(iobase+HCR) | HCR_EN_DMA | HCR_TX_WT, iobase+HCR);
  482. /* Restore set register */
  483. outb(set, iobase+SSR);
  484. }
  485. /*
  486. * Function w83977af_pio_write (iobase, buf, len, fifo_size)
  487. *
  488. *
  489. *
  490. */
  491. static int w83977af_pio_write(int iobase, __u8 *buf, int len, int fifo_size)
  492. {
  493. int actual = 0;
  494. __u8 set;
  495. IRDA_DEBUG(4, "%s()\n", __func__ );
  496. /* Save current bank */
  497. set = inb(iobase+SSR);
  498. switch_bank(iobase, SET0);
  499. if (!(inb_p(iobase+USR) & USR_TSRE)) {
  500. IRDA_DEBUG(4,
  501. "%s(), warning, FIFO not empty yet!\n", __func__ );
  502. fifo_size -= 17;
  503. IRDA_DEBUG(4, "%s(), %d bytes left in tx fifo\n",
  504. __func__ , fifo_size);
  505. }
  506. /* Fill FIFO with current frame */
  507. while ((fifo_size-- > 0) && (actual < len)) {
  508. /* Transmit next byte */
  509. outb(buf[actual++], iobase+TBR);
  510. }
  511. IRDA_DEBUG(4, "%s(), fifo_size %d ; %d sent of %d\n",
  512. __func__ , fifo_size, actual, len);
  513. /* Restore bank */
  514. outb(set, iobase+SSR);
  515. return actual;
  516. }
  517. /*
  518. * Function w83977af_dma_xmit_complete (self)
  519. *
  520. * The transfer of a frame in finished. So do the necessary things
  521. *
  522. *
  523. */
  524. static void w83977af_dma_xmit_complete(struct w83977af_ir *self)
  525. {
  526. int iobase;
  527. __u8 set;
  528. IRDA_DEBUG(4, "%s(%ld)\n", __func__ , jiffies);
  529. IRDA_ASSERT(self != NULL, return;);
  530. iobase = self->io.fir_base;
  531. /* Save current set */
  532. set = inb(iobase+SSR);
  533. /* Disable DMA */
  534. switch_bank(iobase, SET0);
  535. outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
  536. /* Check for underrun! */
  537. if (inb(iobase+AUDR) & AUDR_UNDR) {
  538. IRDA_DEBUG(0, "%s(), Transmit underrun!\n", __func__ );
  539. self->netdev->stats.tx_errors++;
  540. self->netdev->stats.tx_fifo_errors++;
  541. /* Clear bit, by writing 1 to it */
  542. outb(AUDR_UNDR, iobase+AUDR);
  543. } else
  544. self->netdev->stats.tx_packets++;
  545. if (self->new_speed) {
  546. w83977af_change_speed(self, self->new_speed);
  547. self->new_speed = 0;
  548. }
  549. /* Unlock tx_buff and request another frame */
  550. /* Tell the network layer, that we want more frames */
  551. netif_wake_queue(self->netdev);
  552. /* Restore set */
  553. outb(set, iobase+SSR);
  554. }
  555. /*
  556. * Function w83977af_dma_receive (self)
  557. *
  558. * Get ready for receiving a frame. The device will initiate a DMA
  559. * if it starts to receive a frame.
  560. *
  561. */
  562. static int w83977af_dma_receive(struct w83977af_ir *self)
  563. {
  564. int iobase;
  565. __u8 set;
  566. #ifdef CONFIG_ARCH_NETWINDER
  567. unsigned long flags;
  568. __u8 hcr;
  569. #endif
  570. IRDA_ASSERT(self != NULL, return -1;);
  571. IRDA_DEBUG(4, "%s\n", __func__ );
  572. iobase= self->io.fir_base;
  573. /* Save current set */
  574. set = inb(iobase+SSR);
  575. /* Disable DMA */
  576. switch_bank(iobase, SET0);
  577. outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
  578. /* Choose DMA Rx, DMA Fairness, and Advanced mode */
  579. switch_bank(iobase, SET2);
  580. outb((inb(iobase+ADCR1) & ~ADCR1_D_CHSW)/*|ADCR1_DMA_F*/|ADCR1_ADV_SL,
  581. iobase+ADCR1);
  582. self->io.direction = IO_RECV;
  583. self->rx_buff.data = self->rx_buff.head;
  584. #ifdef CONFIG_ARCH_NETWINDER
  585. spin_lock_irqsave(&self->lock, flags);
  586. disable_dma(self->io.dma);
  587. clear_dma_ff(self->io.dma);
  588. set_dma_mode(self->io.dma, DMA_MODE_READ);
  589. set_dma_addr(self->io.dma, self->rx_buff_dma);
  590. set_dma_count(self->io.dma, self->rx_buff.truesize);
  591. #else
  592. irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
  593. DMA_MODE_READ);
  594. #endif
  595. /*
  596. * Reset Rx FIFO. This will also flush the ST_FIFO, it's very
  597. * important that we don't reset the Tx FIFO since it might not
  598. * be finished transmitting yet
  599. */
  600. switch_bank(iobase, SET0);
  601. outb(UFR_RXTL|UFR_TXTL|UFR_RXF_RST|UFR_EN_FIFO, iobase+UFR);
  602. self->st_fifo.len = self->st_fifo.tail = self->st_fifo.head = 0;
  603. /* Enable DMA */
  604. switch_bank(iobase, SET0);
  605. #ifdef CONFIG_ARCH_NETWINDER
  606. hcr = inb(iobase+HCR);
  607. outb(hcr | HCR_EN_DMA, iobase+HCR);
  608. enable_dma(self->io.dma);
  609. spin_unlock_irqrestore(&self->lock, flags);
  610. #else
  611. outb(inb(iobase+HCR) | HCR_EN_DMA, iobase+HCR);
  612. #endif
  613. /* Restore set */
  614. outb(set, iobase+SSR);
  615. return 0;
  616. }
  617. /*
  618. * Function w83977af_receive_complete (self)
  619. *
  620. * Finished with receiving a frame
  621. *
  622. */
  623. static int w83977af_dma_receive_complete(struct w83977af_ir *self)
  624. {
  625. struct sk_buff *skb;
  626. struct st_fifo *st_fifo;
  627. int len;
  628. int iobase;
  629. __u8 set;
  630. __u8 status;
  631. IRDA_DEBUG(4, "%s\n", __func__ );
  632. st_fifo = &self->st_fifo;
  633. iobase = self->io.fir_base;
  634. /* Save current set */
  635. set = inb(iobase+SSR);
  636. iobase = self->io.fir_base;
  637. /* Read status FIFO */
  638. switch_bank(iobase, SET5);
  639. while ((status = inb(iobase+FS_FO)) & FS_FO_FSFDR) {
  640. st_fifo->entries[st_fifo->tail].status = status;
  641. st_fifo->entries[st_fifo->tail].len = inb(iobase+RFLFL);
  642. st_fifo->entries[st_fifo->tail].len |= inb(iobase+RFLFH) << 8;
  643. st_fifo->tail++;
  644. st_fifo->len++;
  645. }
  646. while (st_fifo->len) {
  647. /* Get first entry */
  648. status = st_fifo->entries[st_fifo->head].status;
  649. len = st_fifo->entries[st_fifo->head].len;
  650. st_fifo->head++;
  651. st_fifo->len--;
  652. /* Check for errors */
  653. if (status & FS_FO_ERR_MSK) {
  654. if (status & FS_FO_LST_FR) {
  655. /* Add number of lost frames to stats */
  656. self->netdev->stats.rx_errors += len;
  657. } else {
  658. /* Skip frame */
  659. self->netdev->stats.rx_errors++;
  660. self->rx_buff.data += len;
  661. if (status & FS_FO_MX_LEX)
  662. self->netdev->stats.rx_length_errors++;
  663. if (status & FS_FO_PHY_ERR)
  664. self->netdev->stats.rx_frame_errors++;
  665. if (status & FS_FO_CRC_ERR)
  666. self->netdev->stats.rx_crc_errors++;
  667. }
  668. /* The errors below can be reported in both cases */
  669. if (status & FS_FO_RX_OV)
  670. self->netdev->stats.rx_fifo_errors++;
  671. if (status & FS_FO_FSF_OV)
  672. self->netdev->stats.rx_fifo_errors++;
  673. } else {
  674. /* Check if we have transferred all data to memory */
  675. switch_bank(iobase, SET0);
  676. if (inb(iobase+USR) & USR_RDR) {
  677. udelay(80); /* Should be enough!? */
  678. }
  679. skb = dev_alloc_skb(len+1);
  680. if (skb == NULL) {
  681. printk(KERN_INFO
  682. "%s(), memory squeeze, dropping frame.\n", __func__);
  683. /* Restore set register */
  684. outb(set, iobase+SSR);
  685. return FALSE;
  686. }
  687. /* Align to 20 bytes */
  688. skb_reserve(skb, 1);
  689. /* Copy frame without CRC */
  690. if (self->io.speed < 4000000) {
  691. skb_put(skb, len-2);
  692. skb_copy_to_linear_data(skb,
  693. self->rx_buff.data,
  694. len - 2);
  695. } else {
  696. skb_put(skb, len-4);
  697. skb_copy_to_linear_data(skb,
  698. self->rx_buff.data,
  699. len - 4);
  700. }
  701. /* Move to next frame */
  702. self->rx_buff.data += len;
  703. self->netdev->stats.rx_packets++;
  704. skb->dev = self->netdev;
  705. skb_reset_mac_header(skb);
  706. skb->protocol = htons(ETH_P_IRDA);
  707. netif_rx(skb);
  708. }
  709. }
  710. /* Restore set register */
  711. outb(set, iobase+SSR);
  712. return TRUE;
  713. }
  714. /*
  715. * Function pc87108_pio_receive (self)
  716. *
  717. * Receive all data in receiver FIFO
  718. *
  719. */
  720. static void w83977af_pio_receive(struct w83977af_ir *self)
  721. {
  722. __u8 byte = 0x00;
  723. int iobase;
  724. IRDA_DEBUG(4, "%s()\n", __func__ );
  725. IRDA_ASSERT(self != NULL, return;);
  726. iobase = self->io.fir_base;
  727. /* Receive all characters in Rx FIFO */
  728. do {
  729. byte = inb(iobase+RBR);
  730. async_unwrap_char(self->netdev, &self->netdev->stats, &self->rx_buff,
  731. byte);
  732. } while (inb(iobase+USR) & USR_RDR); /* Data available */
  733. }
  734. /*
  735. * Function w83977af_sir_interrupt (self, eir)
  736. *
  737. * Handle SIR interrupt
  738. *
  739. */
  740. static __u8 w83977af_sir_interrupt(struct w83977af_ir *self, int isr)
  741. {
  742. int actual;
  743. __u8 new_icr = 0;
  744. __u8 set;
  745. int iobase;
  746. IRDA_DEBUG(4, "%s(), isr=%#x\n", __func__ , isr);
  747. iobase = self->io.fir_base;
  748. /* Transmit FIFO low on data */
  749. if (isr & ISR_TXTH_I) {
  750. /* Write data left in transmit buffer */
  751. actual = w83977af_pio_write(self->io.fir_base,
  752. self->tx_buff.data,
  753. self->tx_buff.len,
  754. self->io.fifo_size);
  755. self->tx_buff.data += actual;
  756. self->tx_buff.len -= actual;
  757. self->io.direction = IO_XMIT;
  758. /* Check if finished */
  759. if (self->tx_buff.len > 0) {
  760. new_icr |= ICR_ETXTHI;
  761. } else {
  762. set = inb(iobase+SSR);
  763. switch_bank(iobase, SET0);
  764. outb(AUDR_SFEND, iobase+AUDR);
  765. outb(set, iobase+SSR);
  766. self->netdev->stats.tx_packets++;
  767. /* Feed me more packets */
  768. netif_wake_queue(self->netdev);
  769. new_icr |= ICR_ETBREI;
  770. }
  771. }
  772. /* Check if transmission has completed */
  773. if (isr & ISR_TXEMP_I) {
  774. /* Check if we need to change the speed? */
  775. if (self->new_speed) {
  776. IRDA_DEBUG(2,
  777. "%s(), Changing speed!\n", __func__ );
  778. w83977af_change_speed(self, self->new_speed);
  779. self->new_speed = 0;
  780. }
  781. /* Turn around and get ready to receive some data */
  782. self->io.direction = IO_RECV;
  783. new_icr |= ICR_ERBRI;
  784. }
  785. /* Rx FIFO threshold or timeout */
  786. if (isr & ISR_RXTH_I) {
  787. w83977af_pio_receive(self);
  788. /* Keep receiving */
  789. new_icr |= ICR_ERBRI;
  790. }
  791. return new_icr;
  792. }
  793. /*
  794. * Function pc87108_fir_interrupt (self, eir)
  795. *
  796. * Handle MIR/FIR interrupt
  797. *
  798. */
  799. static __u8 w83977af_fir_interrupt(struct w83977af_ir *self, int isr)
  800. {
  801. __u8 new_icr = 0;
  802. __u8 set;
  803. int iobase;
  804. iobase = self->io.fir_base;
  805. set = inb(iobase+SSR);
  806. /* End of frame detected in FIFO */
  807. if (isr & (ISR_FEND_I|ISR_FSF_I)) {
  808. if (w83977af_dma_receive_complete(self)) {
  809. /* Wait for next status FIFO interrupt */
  810. new_icr |= ICR_EFSFI;
  811. } else {
  812. /* DMA not finished yet */
  813. /* Set timer value, resolution 1 ms */
  814. switch_bank(iobase, SET4);
  815. outb(0x01, iobase+TMRL); /* 1 ms */
  816. outb(0x00, iobase+TMRH);
  817. /* Start timer */
  818. outb(IR_MSL_EN_TMR, iobase+IR_MSL);
  819. new_icr |= ICR_ETMRI;
  820. }
  821. }
  822. /* Timer finished */
  823. if (isr & ISR_TMR_I) {
  824. /* Disable timer */
  825. switch_bank(iobase, SET4);
  826. outb(0, iobase+IR_MSL);
  827. /* Clear timer event */
  828. /* switch_bank(iobase, SET0); */
  829. /* outb(ASCR_CTE, iobase+ASCR); */
  830. /* Check if this is a TX timer interrupt */
  831. if (self->io.direction == IO_XMIT) {
  832. w83977af_dma_write(self, iobase);
  833. new_icr |= ICR_EDMAI;
  834. } else {
  835. /* Check if DMA has now finished */
  836. w83977af_dma_receive_complete(self);
  837. new_icr |= ICR_EFSFI;
  838. }
  839. }
  840. /* Finished with DMA */
  841. if (isr & ISR_DMA_I) {
  842. w83977af_dma_xmit_complete(self);
  843. /* Check if there are more frames to be transmitted */
  844. /* if (irda_device_txqueue_empty(self)) { */
  845. /* Prepare for receive
  846. *
  847. * ** Netwinder Tx DMA likes that we do this anyway **
  848. */
  849. w83977af_dma_receive(self);
  850. new_icr = ICR_EFSFI;
  851. /* } */
  852. }
  853. /* Restore set */
  854. outb(set, iobase+SSR);
  855. return new_icr;
  856. }
  857. /*
  858. * Function w83977af_interrupt (irq, dev_id, regs)
  859. *
  860. * An interrupt from the chip has arrived. Time to do some work
  861. *
  862. */
  863. static irqreturn_t w83977af_interrupt(int irq, void *dev_id)
  864. {
  865. struct net_device *dev = dev_id;
  866. struct w83977af_ir *self;
  867. __u8 set, icr, isr;
  868. int iobase;
  869. self = netdev_priv(dev);
  870. iobase = self->io.fir_base;
  871. /* Save current bank */
  872. set = inb(iobase+SSR);
  873. switch_bank(iobase, SET0);
  874. icr = inb(iobase+ICR);
  875. isr = inb(iobase+ISR) & icr; /* Mask out the interesting ones */
  876. outb(0, iobase+ICR); /* Disable interrupts */
  877. if (isr) {
  878. /* Dispatch interrupt handler for the current speed */
  879. if (self->io.speed > PIO_MAX_SPEED )
  880. icr = w83977af_fir_interrupt(self, isr);
  881. else
  882. icr = w83977af_sir_interrupt(self, isr);
  883. }
  884. outb(icr, iobase+ICR); /* Restore (new) interrupts */
  885. outb(set, iobase+SSR); /* Restore bank register */
  886. return IRQ_RETVAL(isr);
  887. }
  888. /*
  889. * Function w83977af_is_receiving (self)
  890. *
  891. * Return TRUE is we are currently receiving a frame
  892. *
  893. */
  894. static int w83977af_is_receiving(struct w83977af_ir *self)
  895. {
  896. int status = FALSE;
  897. int iobase;
  898. __u8 set;
  899. IRDA_ASSERT(self != NULL, return FALSE;);
  900. if (self->io.speed > 115200) {
  901. iobase = self->io.fir_base;
  902. /* Check if rx FIFO is not empty */
  903. set = inb(iobase+SSR);
  904. switch_bank(iobase, SET2);
  905. if ((inb(iobase+RXFDTH) & 0x3f) != 0) {
  906. /* We are receiving something */
  907. status = TRUE;
  908. }
  909. outb(set, iobase+SSR);
  910. } else
  911. status = (self->rx_buff.state != OUTSIDE_FRAME);
  912. return status;
  913. }
  914. /*
  915. * Function w83977af_net_open (dev)
  916. *
  917. * Start the device
  918. *
  919. */
  920. static int w83977af_net_open(struct net_device *dev)
  921. {
  922. struct w83977af_ir *self;
  923. int iobase;
  924. char hwname[32];
  925. __u8 set;
  926. IRDA_DEBUG(0, "%s()\n", __func__ );
  927. IRDA_ASSERT(dev != NULL, return -1;);
  928. self = netdev_priv(dev);
  929. IRDA_ASSERT(self != NULL, return 0;);
  930. iobase = self->io.fir_base;
  931. if (request_irq(self->io.irq, w83977af_interrupt, 0, dev->name,
  932. (void *) dev)) {
  933. return -EAGAIN;
  934. }
  935. /*
  936. * Always allocate the DMA channel after the IRQ,
  937. * and clean up on failure.
  938. */
  939. if (request_dma(self->io.dma, dev->name)) {
  940. free_irq(self->io.irq, dev);
  941. return -EAGAIN;
  942. }
  943. /* Save current set */
  944. set = inb(iobase+SSR);
  945. /* Enable some interrupts so we can receive frames again */
  946. switch_bank(iobase, SET0);
  947. if (self->io.speed > 115200) {
  948. outb(ICR_EFSFI, iobase+ICR);
  949. w83977af_dma_receive(self);
  950. } else
  951. outb(ICR_ERBRI, iobase+ICR);
  952. /* Restore bank register */
  953. outb(set, iobase+SSR);
  954. /* Ready to play! */
  955. netif_start_queue(dev);
  956. /* Give self a hardware name */
  957. sprintf(hwname, "w83977af @ 0x%03x", self->io.fir_base);
  958. /*
  959. * Open new IrLAP layer instance, now that everything should be
  960. * initialized properly
  961. */
  962. self->irlap = irlap_open(dev, &self->qos, hwname);
  963. return 0;
  964. }
  965. /*
  966. * Function w83977af_net_close (dev)
  967. *
  968. * Stop the device
  969. *
  970. */
  971. static int w83977af_net_close(struct net_device *dev)
  972. {
  973. struct w83977af_ir *self;
  974. int iobase;
  975. __u8 set;
  976. IRDA_DEBUG(0, "%s()\n", __func__ );
  977. IRDA_ASSERT(dev != NULL, return -1;);
  978. self = netdev_priv(dev);
  979. IRDA_ASSERT(self != NULL, return 0;);
  980. iobase = self->io.fir_base;
  981. /* Stop device */
  982. netif_stop_queue(dev);
  983. /* Stop and remove instance of IrLAP */
  984. if (self->irlap)
  985. irlap_close(self->irlap);
  986. self->irlap = NULL;
  987. disable_dma(self->io.dma);
  988. /* Save current set */
  989. set = inb(iobase+SSR);
  990. /* Disable interrupts */
  991. switch_bank(iobase, SET0);
  992. outb(0, iobase+ICR);
  993. free_irq(self->io.irq, dev);
  994. free_dma(self->io.dma);
  995. /* Restore bank register */
  996. outb(set, iobase+SSR);
  997. return 0;
  998. }
  999. /*
  1000. * Function w83977af_net_ioctl (dev, rq, cmd)
  1001. *
  1002. * Process IOCTL commands for this device
  1003. *
  1004. */
  1005. static int w83977af_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  1006. {
  1007. struct if_irda_req *irq = (struct if_irda_req *) rq;
  1008. struct w83977af_ir *self;
  1009. unsigned long flags;
  1010. int ret = 0;
  1011. IRDA_ASSERT(dev != NULL, return -1;);
  1012. self = netdev_priv(dev);
  1013. IRDA_ASSERT(self != NULL, return -1;);
  1014. IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __func__ , dev->name, cmd);
  1015. spin_lock_irqsave(&self->lock, flags);
  1016. switch (cmd) {
  1017. case SIOCSBANDWIDTH: /* Set bandwidth */
  1018. if (!capable(CAP_NET_ADMIN)) {
  1019. ret = -EPERM;
  1020. goto out;
  1021. }
  1022. w83977af_change_speed(self, irq->ifr_baudrate);
  1023. break;
  1024. case SIOCSMEDIABUSY: /* Set media busy */
  1025. if (!capable(CAP_NET_ADMIN)) {
  1026. ret = -EPERM;
  1027. goto out;
  1028. }
  1029. irda_device_set_media_busy(self->netdev, TRUE);
  1030. break;
  1031. case SIOCGRECEIVING: /* Check if we are receiving right now */
  1032. irq->ifr_receiving = w83977af_is_receiving(self);
  1033. break;
  1034. default:
  1035. ret = -EOPNOTSUPP;
  1036. }
  1037. out:
  1038. spin_unlock_irqrestore(&self->lock, flags);
  1039. return ret;
  1040. }
  1041. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  1042. MODULE_DESCRIPTION("Winbond W83977AF IrDA Device Driver");
  1043. MODULE_LICENSE("GPL");
  1044. module_param(qos_mtt_bits, int, 0);
  1045. MODULE_PARM_DESC(qos_mtt_bits, "Mimimum Turn Time");
  1046. module_param_array(io, int, NULL, 0);
  1047. MODULE_PARM_DESC(io, "Base I/O addresses");
  1048. module_param_array(irq, int, NULL, 0);
  1049. MODULE_PARM_DESC(irq, "IRQ lines");
  1050. /*
  1051. * Function init_module (void)
  1052. *
  1053. *
  1054. *
  1055. */
  1056. module_init(w83977af_init);
  1057. /*
  1058. * Function cleanup_module (void)
  1059. *
  1060. *
  1061. *
  1062. */
  1063. module_exit(w83977af_cleanup);