w83977af_ir.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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)
  436. udelay(mtt);
  437. /* Enable DMA interrupt */
  438. switch_bank(iobase, SET0);
  439. outb(ICR_EDMAI, iobase+ICR);
  440. w83977af_dma_write(self, iobase);
  441. } else {
  442. self->tx_buff.data = self->tx_buff.head;
  443. self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
  444. self->tx_buff.truesize);
  445. /* Add interrupt on tx low level (will fire immediately) */
  446. switch_bank(iobase, SET0);
  447. outb(ICR_ETXTHI, iobase+ICR);
  448. }
  449. dev_kfree_skb(skb);
  450. /* Restore set register */
  451. outb(set, iobase+SSR);
  452. return NETDEV_TX_OK;
  453. }
  454. /*
  455. * Function w83977af_dma_write (self, iobase)
  456. *
  457. * Send frame using DMA
  458. *
  459. */
  460. static void w83977af_dma_write(struct w83977af_ir *self, int iobase)
  461. {
  462. __u8 set;
  463. pr_debug("%s(), len=%d\n", __func__ , self->tx_buff.len);
  464. /* Save current set */
  465. set = inb(iobase+SSR);
  466. /* Disable DMA */
  467. switch_bank(iobase, SET0);
  468. outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
  469. /* Choose transmit DMA channel */
  470. switch_bank(iobase, SET2);
  471. outb(ADCR1_D_CHSW|/*ADCR1_DMA_F|*/ADCR1_ADV_SL, iobase+ADCR1);
  472. irda_setup_dma(self->io.dma, self->tx_buff_dma, self->tx_buff.len,
  473. DMA_MODE_WRITE);
  474. self->io.direction = IO_XMIT;
  475. /* Enable DMA */
  476. switch_bank(iobase, SET0);
  477. outb(inb(iobase+HCR) | HCR_EN_DMA | HCR_TX_WT, iobase+HCR);
  478. /* Restore set register */
  479. outb(set, iobase+SSR);
  480. }
  481. /*
  482. * Function w83977af_pio_write (iobase, buf, len, fifo_size)
  483. *
  484. *
  485. *
  486. */
  487. static int w83977af_pio_write(int iobase, __u8 *buf, int len, int fifo_size)
  488. {
  489. int actual = 0;
  490. __u8 set;
  491. /* Save current bank */
  492. set = inb(iobase+SSR);
  493. switch_bank(iobase, SET0);
  494. if (!(inb_p(iobase+USR) & USR_TSRE)) {
  495. pr_debug("%s(), warning, FIFO not empty yet!\n", __func__);
  496. fifo_size -= 17;
  497. pr_debug("%s(), %d bytes left in tx fifo\n",
  498. __func__ , fifo_size);
  499. }
  500. /* Fill FIFO with current frame */
  501. while ((fifo_size-- > 0) && (actual < len)) {
  502. /* Transmit next byte */
  503. outb(buf[actual++], iobase+TBR);
  504. }
  505. pr_debug("%s(), fifo_size %d ; %d sent of %d\n",
  506. __func__ , fifo_size, actual, len);
  507. /* Restore bank */
  508. outb(set, iobase+SSR);
  509. return actual;
  510. }
  511. /*
  512. * Function w83977af_dma_xmit_complete (self)
  513. *
  514. * The transfer of a frame in finished. So do the necessary things
  515. *
  516. *
  517. */
  518. static void w83977af_dma_xmit_complete(struct w83977af_ir *self)
  519. {
  520. int iobase;
  521. __u8 set;
  522. pr_debug("%s(%ld)\n", __func__ , jiffies);
  523. IRDA_ASSERT(self != NULL, return;);
  524. iobase = self->io.fir_base;
  525. /* Save current set */
  526. set = inb(iobase+SSR);
  527. /* Disable DMA */
  528. switch_bank(iobase, SET0);
  529. outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
  530. /* Check for underrun! */
  531. if (inb(iobase+AUDR) & AUDR_UNDR) {
  532. pr_debug("%s(), Transmit underrun!\n", __func__);
  533. self->netdev->stats.tx_errors++;
  534. self->netdev->stats.tx_fifo_errors++;
  535. /* Clear bit, by writing 1 to it */
  536. outb(AUDR_UNDR, iobase+AUDR);
  537. } else
  538. self->netdev->stats.tx_packets++;
  539. if (self->new_speed) {
  540. w83977af_change_speed(self, self->new_speed);
  541. self->new_speed = 0;
  542. }
  543. /* Unlock tx_buff and request another frame */
  544. /* Tell the network layer, that we want more frames */
  545. netif_wake_queue(self->netdev);
  546. /* Restore set */
  547. outb(set, iobase+SSR);
  548. }
  549. /*
  550. * Function w83977af_dma_receive (self)
  551. *
  552. * Get ready for receiving a frame. The device will initiate a DMA
  553. * if it starts to receive a frame.
  554. *
  555. */
  556. static int w83977af_dma_receive(struct w83977af_ir *self)
  557. {
  558. int iobase;
  559. __u8 set;
  560. #ifdef CONFIG_ARCH_NETWINDER
  561. unsigned long flags;
  562. __u8 hcr;
  563. #endif
  564. IRDA_ASSERT(self != NULL, return -1;);
  565. pr_debug("%s\n", __func__);
  566. iobase= self->io.fir_base;
  567. /* Save current set */
  568. set = inb(iobase+SSR);
  569. /* Disable DMA */
  570. switch_bank(iobase, SET0);
  571. outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
  572. /* Choose DMA Rx, DMA Fairness, and Advanced mode */
  573. switch_bank(iobase, SET2);
  574. outb((inb(iobase+ADCR1) & ~ADCR1_D_CHSW)/*|ADCR1_DMA_F*/|ADCR1_ADV_SL,
  575. iobase+ADCR1);
  576. self->io.direction = IO_RECV;
  577. self->rx_buff.data = self->rx_buff.head;
  578. #ifdef CONFIG_ARCH_NETWINDER
  579. spin_lock_irqsave(&self->lock, flags);
  580. disable_dma(self->io.dma);
  581. clear_dma_ff(self->io.dma);
  582. set_dma_mode(self->io.dma, DMA_MODE_READ);
  583. set_dma_addr(self->io.dma, self->rx_buff_dma);
  584. set_dma_count(self->io.dma, self->rx_buff.truesize);
  585. #else
  586. irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
  587. DMA_MODE_READ);
  588. #endif
  589. /*
  590. * Reset Rx FIFO. This will also flush the ST_FIFO, it's very
  591. * important that we don't reset the Tx FIFO since it might not
  592. * be finished transmitting yet
  593. */
  594. switch_bank(iobase, SET0);
  595. outb(UFR_RXTL|UFR_TXTL|UFR_RXF_RST|UFR_EN_FIFO, iobase+UFR);
  596. self->st_fifo.len = self->st_fifo.tail = self->st_fifo.head = 0;
  597. /* Enable DMA */
  598. switch_bank(iobase, SET0);
  599. #ifdef CONFIG_ARCH_NETWINDER
  600. hcr = inb(iobase+HCR);
  601. outb(hcr | HCR_EN_DMA, iobase+HCR);
  602. enable_dma(self->io.dma);
  603. spin_unlock_irqrestore(&self->lock, flags);
  604. #else
  605. outb(inb(iobase+HCR) | HCR_EN_DMA, iobase+HCR);
  606. #endif
  607. /* Restore set */
  608. outb(set, iobase+SSR);
  609. return 0;
  610. }
  611. /*
  612. * Function w83977af_receive_complete (self)
  613. *
  614. * Finished with receiving a frame
  615. *
  616. */
  617. static int w83977af_dma_receive_complete(struct w83977af_ir *self)
  618. {
  619. struct sk_buff *skb;
  620. struct st_fifo *st_fifo;
  621. int len;
  622. int iobase;
  623. __u8 set;
  624. __u8 status;
  625. pr_debug("%s\n", __func__);
  626. st_fifo = &self->st_fifo;
  627. iobase = self->io.fir_base;
  628. /* Save current set */
  629. set = inb(iobase+SSR);
  630. iobase = self->io.fir_base;
  631. /* Read status FIFO */
  632. switch_bank(iobase, SET5);
  633. while ((status = inb(iobase+FS_FO)) & FS_FO_FSFDR) {
  634. st_fifo->entries[st_fifo->tail].status = status;
  635. st_fifo->entries[st_fifo->tail].len = inb(iobase+RFLFL);
  636. st_fifo->entries[st_fifo->tail].len |= inb(iobase+RFLFH) << 8;
  637. st_fifo->tail++;
  638. st_fifo->len++;
  639. }
  640. while (st_fifo->len) {
  641. /* Get first entry */
  642. status = st_fifo->entries[st_fifo->head].status;
  643. len = st_fifo->entries[st_fifo->head].len;
  644. st_fifo->head++;
  645. st_fifo->len--;
  646. /* Check for errors */
  647. if (status & FS_FO_ERR_MSK) {
  648. if (status & FS_FO_LST_FR) {
  649. /* Add number of lost frames to stats */
  650. self->netdev->stats.rx_errors += len;
  651. } else {
  652. /* Skip frame */
  653. self->netdev->stats.rx_errors++;
  654. self->rx_buff.data += len;
  655. if (status & FS_FO_MX_LEX)
  656. self->netdev->stats.rx_length_errors++;
  657. if (status & FS_FO_PHY_ERR)
  658. self->netdev->stats.rx_frame_errors++;
  659. if (status & FS_FO_CRC_ERR)
  660. self->netdev->stats.rx_crc_errors++;
  661. }
  662. /* The errors below can be reported in both cases */
  663. if (status & FS_FO_RX_OV)
  664. self->netdev->stats.rx_fifo_errors++;
  665. if (status & FS_FO_FSF_OV)
  666. self->netdev->stats.rx_fifo_errors++;
  667. } else {
  668. /* Check if we have transferred all data to memory */
  669. switch_bank(iobase, SET0);
  670. if (inb(iobase+USR) & USR_RDR) {
  671. udelay(80); /* Should be enough!? */
  672. }
  673. skb = dev_alloc_skb(len+1);
  674. if (skb == NULL) {
  675. printk(KERN_INFO
  676. "%s(), memory squeeze, dropping frame.\n", __func__);
  677. /* Restore set register */
  678. outb(set, iobase+SSR);
  679. return FALSE;
  680. }
  681. /* Align to 20 bytes */
  682. skb_reserve(skb, 1);
  683. /* Copy frame without CRC */
  684. if (self->io.speed < 4000000) {
  685. skb_put(skb, len-2);
  686. skb_copy_to_linear_data(skb,
  687. self->rx_buff.data,
  688. len - 2);
  689. } else {
  690. skb_put(skb, len-4);
  691. skb_copy_to_linear_data(skb,
  692. self->rx_buff.data,
  693. len - 4);
  694. }
  695. /* Move to next frame */
  696. self->rx_buff.data += len;
  697. self->netdev->stats.rx_packets++;
  698. skb->dev = self->netdev;
  699. skb_reset_mac_header(skb);
  700. skb->protocol = htons(ETH_P_IRDA);
  701. netif_rx(skb);
  702. }
  703. }
  704. /* Restore set register */
  705. outb(set, iobase+SSR);
  706. return TRUE;
  707. }
  708. /*
  709. * Function pc87108_pio_receive (self)
  710. *
  711. * Receive all data in receiver FIFO
  712. *
  713. */
  714. static void w83977af_pio_receive(struct w83977af_ir *self)
  715. {
  716. __u8 byte = 0x00;
  717. int iobase;
  718. IRDA_ASSERT(self != NULL, return;);
  719. iobase = self->io.fir_base;
  720. /* Receive all characters in Rx FIFO */
  721. do {
  722. byte = inb(iobase+RBR);
  723. async_unwrap_char(self->netdev, &self->netdev->stats, &self->rx_buff,
  724. byte);
  725. } while (inb(iobase+USR) & USR_RDR); /* Data available */
  726. }
  727. /*
  728. * Function w83977af_sir_interrupt (self, eir)
  729. *
  730. * Handle SIR interrupt
  731. *
  732. */
  733. static __u8 w83977af_sir_interrupt(struct w83977af_ir *self, int isr)
  734. {
  735. int actual;
  736. __u8 new_icr = 0;
  737. __u8 set;
  738. int iobase;
  739. pr_debug("%s(), isr=%#x\n", __func__ , isr);
  740. iobase = self->io.fir_base;
  741. /* Transmit FIFO low on data */
  742. if (isr & ISR_TXTH_I) {
  743. /* Write data left in transmit buffer */
  744. actual = w83977af_pio_write(self->io.fir_base,
  745. self->tx_buff.data,
  746. self->tx_buff.len,
  747. self->io.fifo_size);
  748. self->tx_buff.data += actual;
  749. self->tx_buff.len -= actual;
  750. self->io.direction = IO_XMIT;
  751. /* Check if finished */
  752. if (self->tx_buff.len > 0) {
  753. new_icr |= ICR_ETXTHI;
  754. } else {
  755. set = inb(iobase+SSR);
  756. switch_bank(iobase, SET0);
  757. outb(AUDR_SFEND, iobase+AUDR);
  758. outb(set, iobase+SSR);
  759. self->netdev->stats.tx_packets++;
  760. /* Feed me more packets */
  761. netif_wake_queue(self->netdev);
  762. new_icr |= ICR_ETBREI;
  763. }
  764. }
  765. /* Check if transmission has completed */
  766. if (isr & ISR_TXEMP_I) {
  767. /* Check if we need to change the speed? */
  768. if (self->new_speed) {
  769. pr_debug("%s(), Changing speed!\n", __func__);
  770. w83977af_change_speed(self, self->new_speed);
  771. self->new_speed = 0;
  772. }
  773. /* Turn around and get ready to receive some data */
  774. self->io.direction = IO_RECV;
  775. new_icr |= ICR_ERBRI;
  776. }
  777. /* Rx FIFO threshold or timeout */
  778. if (isr & ISR_RXTH_I) {
  779. w83977af_pio_receive(self);
  780. /* Keep receiving */
  781. new_icr |= ICR_ERBRI;
  782. }
  783. return new_icr;
  784. }
  785. /*
  786. * Function pc87108_fir_interrupt (self, eir)
  787. *
  788. * Handle MIR/FIR interrupt
  789. *
  790. */
  791. static __u8 w83977af_fir_interrupt(struct w83977af_ir *self, int isr)
  792. {
  793. __u8 new_icr = 0;
  794. __u8 set;
  795. int iobase;
  796. iobase = self->io.fir_base;
  797. set = inb(iobase+SSR);
  798. /* End of frame detected in FIFO */
  799. if (isr & (ISR_FEND_I|ISR_FSF_I)) {
  800. if (w83977af_dma_receive_complete(self)) {
  801. /* Wait for next status FIFO interrupt */
  802. new_icr |= ICR_EFSFI;
  803. } else {
  804. /* DMA not finished yet */
  805. /* Set timer value, resolution 1 ms */
  806. switch_bank(iobase, SET4);
  807. outb(0x01, iobase+TMRL); /* 1 ms */
  808. outb(0x00, iobase+TMRH);
  809. /* Start timer */
  810. outb(IR_MSL_EN_TMR, iobase+IR_MSL);
  811. new_icr |= ICR_ETMRI;
  812. }
  813. }
  814. /* Timer finished */
  815. if (isr & ISR_TMR_I) {
  816. /* Disable timer */
  817. switch_bank(iobase, SET4);
  818. outb(0, iobase+IR_MSL);
  819. /* Clear timer event */
  820. /* switch_bank(iobase, SET0); */
  821. /* outb(ASCR_CTE, iobase+ASCR); */
  822. /* Check if this is a TX timer interrupt */
  823. if (self->io.direction == IO_XMIT) {
  824. w83977af_dma_write(self, iobase);
  825. new_icr |= ICR_EDMAI;
  826. } else {
  827. /* Check if DMA has now finished */
  828. w83977af_dma_receive_complete(self);
  829. new_icr |= ICR_EFSFI;
  830. }
  831. }
  832. /* Finished with DMA */
  833. if (isr & ISR_DMA_I) {
  834. w83977af_dma_xmit_complete(self);
  835. /* Check if there are more frames to be transmitted */
  836. /* if (irda_device_txqueue_empty(self)) { */
  837. /* Prepare for receive
  838. *
  839. * ** Netwinder Tx DMA likes that we do this anyway **
  840. */
  841. w83977af_dma_receive(self);
  842. new_icr = ICR_EFSFI;
  843. /* } */
  844. }
  845. /* Restore set */
  846. outb(set, iobase+SSR);
  847. return new_icr;
  848. }
  849. /*
  850. * Function w83977af_interrupt (irq, dev_id, regs)
  851. *
  852. * An interrupt from the chip has arrived. Time to do some work
  853. *
  854. */
  855. static irqreturn_t w83977af_interrupt(int irq, void *dev_id)
  856. {
  857. struct net_device *dev = dev_id;
  858. struct w83977af_ir *self;
  859. __u8 set, icr, isr;
  860. int iobase;
  861. self = netdev_priv(dev);
  862. iobase = self->io.fir_base;
  863. /* Save current bank */
  864. set = inb(iobase+SSR);
  865. switch_bank(iobase, SET0);
  866. icr = inb(iobase+ICR);
  867. isr = inb(iobase+ISR) & icr; /* Mask out the interesting ones */
  868. outb(0, iobase+ICR); /* Disable interrupts */
  869. if (isr) {
  870. /* Dispatch interrupt handler for the current speed */
  871. if (self->io.speed > PIO_MAX_SPEED )
  872. icr = w83977af_fir_interrupt(self, isr);
  873. else
  874. icr = w83977af_sir_interrupt(self, isr);
  875. }
  876. outb(icr, iobase+ICR); /* Restore (new) interrupts */
  877. outb(set, iobase+SSR); /* Restore bank register */
  878. return IRQ_RETVAL(isr);
  879. }
  880. /*
  881. * Function w83977af_is_receiving (self)
  882. *
  883. * Return TRUE is we are currently receiving a frame
  884. *
  885. */
  886. static int w83977af_is_receiving(struct w83977af_ir *self)
  887. {
  888. int status = FALSE;
  889. int iobase;
  890. __u8 set;
  891. IRDA_ASSERT(self != NULL, return FALSE;);
  892. if (self->io.speed > 115200) {
  893. iobase = self->io.fir_base;
  894. /* Check if rx FIFO is not empty */
  895. set = inb(iobase+SSR);
  896. switch_bank(iobase, SET2);
  897. if ((inb(iobase+RXFDTH) & 0x3f) != 0) {
  898. /* We are receiving something */
  899. status = TRUE;
  900. }
  901. outb(set, iobase+SSR);
  902. } else
  903. status = (self->rx_buff.state != OUTSIDE_FRAME);
  904. return status;
  905. }
  906. /*
  907. * Function w83977af_net_open (dev)
  908. *
  909. * Start the device
  910. *
  911. */
  912. static int w83977af_net_open(struct net_device *dev)
  913. {
  914. struct w83977af_ir *self;
  915. int iobase;
  916. char hwname[32];
  917. __u8 set;
  918. IRDA_ASSERT(dev != NULL, return -1;);
  919. self = netdev_priv(dev);
  920. IRDA_ASSERT(self != NULL, return 0;);
  921. iobase = self->io.fir_base;
  922. if (request_irq(self->io.irq, w83977af_interrupt, 0, dev->name,
  923. (void *) dev)) {
  924. return -EAGAIN;
  925. }
  926. /*
  927. * Always allocate the DMA channel after the IRQ,
  928. * and clean up on failure.
  929. */
  930. if (request_dma(self->io.dma, dev->name)) {
  931. free_irq(self->io.irq, dev);
  932. return -EAGAIN;
  933. }
  934. /* Save current set */
  935. set = inb(iobase+SSR);
  936. /* Enable some interrupts so we can receive frames again */
  937. switch_bank(iobase, SET0);
  938. if (self->io.speed > 115200) {
  939. outb(ICR_EFSFI, iobase+ICR);
  940. w83977af_dma_receive(self);
  941. } else
  942. outb(ICR_ERBRI, iobase+ICR);
  943. /* Restore bank register */
  944. outb(set, iobase+SSR);
  945. /* Ready to play! */
  946. netif_start_queue(dev);
  947. /* Give self a hardware name */
  948. sprintf(hwname, "w83977af @ 0x%03x", self->io.fir_base);
  949. /*
  950. * Open new IrLAP layer instance, now that everything should be
  951. * initialized properly
  952. */
  953. self->irlap = irlap_open(dev, &self->qos, hwname);
  954. return 0;
  955. }
  956. /*
  957. * Function w83977af_net_close (dev)
  958. *
  959. * Stop the device
  960. *
  961. */
  962. static int w83977af_net_close(struct net_device *dev)
  963. {
  964. struct w83977af_ir *self;
  965. int iobase;
  966. __u8 set;
  967. IRDA_ASSERT(dev != NULL, return -1;);
  968. self = netdev_priv(dev);
  969. IRDA_ASSERT(self != NULL, return 0;);
  970. iobase = self->io.fir_base;
  971. /* Stop device */
  972. netif_stop_queue(dev);
  973. /* Stop and remove instance of IrLAP */
  974. if (self->irlap)
  975. irlap_close(self->irlap);
  976. self->irlap = NULL;
  977. disable_dma(self->io.dma);
  978. /* Save current set */
  979. set = inb(iobase+SSR);
  980. /* Disable interrupts */
  981. switch_bank(iobase, SET0);
  982. outb(0, iobase+ICR);
  983. free_irq(self->io.irq, dev);
  984. free_dma(self->io.dma);
  985. /* Restore bank register */
  986. outb(set, iobase+SSR);
  987. return 0;
  988. }
  989. /*
  990. * Function w83977af_net_ioctl (dev, rq, cmd)
  991. *
  992. * Process IOCTL commands for this device
  993. *
  994. */
  995. static int w83977af_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  996. {
  997. struct if_irda_req *irq = (struct if_irda_req *) rq;
  998. struct w83977af_ir *self;
  999. unsigned long flags;
  1000. int ret = 0;
  1001. IRDA_ASSERT(dev != NULL, return -1;);
  1002. self = netdev_priv(dev);
  1003. IRDA_ASSERT(self != NULL, return -1;);
  1004. pr_debug("%s(), %s, (cmd=0x%X)\n", __func__ , dev->name, cmd);
  1005. spin_lock_irqsave(&self->lock, flags);
  1006. switch (cmd) {
  1007. case SIOCSBANDWIDTH: /* Set bandwidth */
  1008. if (!capable(CAP_NET_ADMIN)) {
  1009. ret = -EPERM;
  1010. goto out;
  1011. }
  1012. w83977af_change_speed(self, irq->ifr_baudrate);
  1013. break;
  1014. case SIOCSMEDIABUSY: /* Set media busy */
  1015. if (!capable(CAP_NET_ADMIN)) {
  1016. ret = -EPERM;
  1017. goto out;
  1018. }
  1019. irda_device_set_media_busy(self->netdev, TRUE);
  1020. break;
  1021. case SIOCGRECEIVING: /* Check if we are receiving right now */
  1022. irq->ifr_receiving = w83977af_is_receiving(self);
  1023. break;
  1024. default:
  1025. ret = -EOPNOTSUPP;
  1026. }
  1027. out:
  1028. spin_unlock_irqrestore(&self->lock, flags);
  1029. return ret;
  1030. }
  1031. MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
  1032. MODULE_DESCRIPTION("Winbond W83977AF IrDA Device Driver");
  1033. MODULE_LICENSE("GPL");
  1034. module_param(qos_mtt_bits, int, 0);
  1035. MODULE_PARM_DESC(qos_mtt_bits, "Mimimum Turn Time");
  1036. module_param_array(io, int, NULL, 0);
  1037. MODULE_PARM_DESC(io, "Base I/O addresses");
  1038. module_param_array(irq, int, NULL, 0);
  1039. MODULE_PARM_DESC(irq, "IRQ lines");
  1040. /*
  1041. * Function init_module (void)
  1042. *
  1043. *
  1044. *
  1045. */
  1046. module_init(w83977af_init);
  1047. /*
  1048. * Function cleanup_module (void)
  1049. *
  1050. *
  1051. *
  1052. */
  1053. module_exit(w83977af_cleanup);