w83977af_ir.c 30 KB

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