[PATCH] aic7xxx_osm build fix
[cris-mirror.git] / drivers / net / irda / w83977af_ir.c
blob0ea65c4c6f8543b9972bd7b8d398f4aacd856563
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>
12 * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>
13 * Copyright (c) 1998-1999 Rebel.com
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.
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.
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.
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!
32 * __u8 bank;
34 * bank = inb( iobase+BSR);
36 * do_your_stuff_here();
38 * outb( bank, iobase+BSR);
40 ********************************************************************/
42 #include <linux/module.h>
43 #include <linux/config.h>
44 #include <linux/kernel.h>
45 #include <linux/types.h>
46 #include <linux/skbuff.h>
47 #include <linux/netdevice.h>
48 #include <linux/ioport.h>
49 #include <linux/delay.h>
50 #include <linux/slab.h>
51 #include <linux/init.h>
52 #include <linux/rtnetlink.h>
53 #include <linux/dma-mapping.h>
55 #include <asm/io.h>
56 #include <asm/dma.h>
57 #include <asm/byteorder.h>
59 #include <net/irda/irda.h>
60 #include <net/irda/wrapper.h>
61 #include <net/irda/irda_device.h>
62 #include "w83977af.h"
63 #include "w83977af_ir.h"
65 #ifdef CONFIG_ARCH_NETWINDER /* Adjust to NetWinder differences */
66 #undef CONFIG_NETWINDER_TX_DMA_PROBLEMS /* Not needed */
67 #define CONFIG_NETWINDER_RX_DMA_PROBLEMS /* Must have this one! */
68 #endif
69 #undef CONFIG_USE_INTERNAL_TIMER /* Just cannot make that timer work */
70 #define CONFIG_USE_W977_PNP /* Currently needed */
71 #define PIO_MAX_SPEED 115200
73 static char *driver_name = "w83977af_ir";
74 static int qos_mtt_bits = 0x07; /* 1 ms or more */
76 #define CHIP_IO_EXTENT 8
78 static unsigned int io[] = { 0x180, ~0, ~0, ~0 };
79 #ifdef CONFIG_ARCH_NETWINDER /* Adjust to NetWinder differences */
80 static unsigned int irq[] = { 6, 0, 0, 0 };
81 #else
82 static unsigned int irq[] = { 11, 0, 0, 0 };
83 #endif
84 static unsigned int dma[] = { 1, 0, 0, 0 };
85 static unsigned int efbase[] = { W977_EFIO_BASE, W977_EFIO2_BASE };
86 static unsigned int efio = W977_EFIO_BASE;
88 static struct w83977af_ir *dev_self[] = { NULL, NULL, NULL, NULL};
90 /* Some prototypes */
91 static int w83977af_open(int i, unsigned int iobase, unsigned int irq,
92 unsigned int dma);
93 static int w83977af_close(struct w83977af_ir *self);
94 static int w83977af_probe(int iobase, int irq, int dma);
95 static int w83977af_dma_receive(struct w83977af_ir *self);
96 static int w83977af_dma_receive_complete(struct w83977af_ir *self);
97 static int w83977af_hard_xmit(struct sk_buff *skb, struct net_device *dev);
98 static int w83977af_pio_write(int iobase, __u8 *buf, int len, int fifo_size);
99 static void w83977af_dma_write(struct w83977af_ir *self, int iobase);
100 static void w83977af_change_speed(struct w83977af_ir *self, __u32 speed);
101 static int w83977af_is_receiving(struct w83977af_ir *self);
103 static int w83977af_net_open(struct net_device *dev);
104 static int w83977af_net_close(struct net_device *dev);
105 static int w83977af_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
106 static struct net_device_stats *w83977af_net_get_stats(struct net_device *dev);
109 * Function w83977af_init ()
111 * Initialize chip. Just try to find out how many chips we are dealing with
112 * and where they are
114 static int __init w83977af_init(void)
116 int i;
118 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
120 for (i=0; (io[i] < 2000) && (i < 4); i++) {
121 if (w83977af_open(i, io[i], irq[i], dma[i]) == 0)
122 return 0;
124 return -ENODEV;
128 * Function w83977af_cleanup ()
130 * Close all configured chips
133 static void __exit w83977af_cleanup(void)
135 int i;
137 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
139 for (i=0; i < 4; i++) {
140 if (dev_self[i])
141 w83977af_close(dev_self[i]);
146 * Function w83977af_open (iobase, irq)
148 * Open driver instance
151 int w83977af_open(int i, unsigned int iobase, unsigned int irq,
152 unsigned int dma)
154 struct net_device *dev;
155 struct w83977af_ir *self;
156 int err;
158 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
160 /* Lock the port that we need */
161 if (!request_region(iobase, CHIP_IO_EXTENT, driver_name)) {
162 IRDA_DEBUG(0, "%s(), can't get iobase of 0x%03x\n",
163 __FUNCTION__ , iobase);
164 return -ENODEV;
167 if (w83977af_probe(iobase, irq, dma) == -1) {
168 err = -1;
169 goto err_out;
172 * Allocate new instance of the driver
174 dev = alloc_irdadev(sizeof(struct w83977af_ir));
175 if (dev == NULL) {
176 printk( KERN_ERR "IrDA: Can't allocate memory for "
177 "IrDA control block!\n");
178 err = -ENOMEM;
179 goto err_out;
182 self = dev->priv;
183 spin_lock_init(&self->lock);
186 /* Initialize IO */
187 self->io.fir_base = iobase;
188 self->io.irq = irq;
189 self->io.fir_ext = CHIP_IO_EXTENT;
190 self->io.dma = dma;
191 self->io.fifo_size = 32;
193 /* Initialize QoS for this device */
194 irda_init_max_qos_capabilies(&self->qos);
196 /* The only value we must override it the baudrate */
198 /* FIXME: The HP HDLS-1100 does not support 1152000! */
199 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
200 IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8);
202 /* The HP HDLS-1100 needs 1 ms according to the specs */
203 self->qos.min_turn_time.bits = qos_mtt_bits;
204 irda_qos_bits_to_value(&self->qos);
206 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
207 self->rx_buff.truesize = 14384;
208 self->tx_buff.truesize = 4000;
210 /* Allocate memory if needed */
211 self->rx_buff.head =
212 dma_alloc_coherent(NULL, self->rx_buff.truesize,
213 &self->rx_buff_dma, GFP_KERNEL);
214 if (self->rx_buff.head == NULL) {
215 err = -ENOMEM;
216 goto err_out1;
219 memset(self->rx_buff.head, 0, self->rx_buff.truesize);
221 self->tx_buff.head =
222 dma_alloc_coherent(NULL, self->tx_buff.truesize,
223 &self->tx_buff_dma, GFP_KERNEL);
224 if (self->tx_buff.head == NULL) {
225 err = -ENOMEM;
226 goto err_out2;
228 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
230 self->rx_buff.in_frame = FALSE;
231 self->rx_buff.state = OUTSIDE_FRAME;
232 self->tx_buff.data = self->tx_buff.head;
233 self->rx_buff.data = self->rx_buff.head;
234 self->netdev = dev;
236 /* Keep track of module usage */
237 SET_MODULE_OWNER(dev);
239 /* Override the network functions we need to use */
240 dev->hard_start_xmit = w83977af_hard_xmit;
241 dev->open = w83977af_net_open;
242 dev->stop = w83977af_net_close;
243 dev->do_ioctl = w83977af_net_ioctl;
244 dev->get_stats = w83977af_net_get_stats;
246 err = register_netdev(dev);
247 if (err) {
248 IRDA_ERROR("%s(), register_netdevice() failed!\n", __FUNCTION__);
249 goto err_out3;
251 IRDA_MESSAGE("IrDA: Registered device %s\n", dev->name);
253 /* Need to store self somewhere */
254 dev_self[i] = self;
256 return 0;
257 err_out3:
258 dma_free_coherent(NULL, self->tx_buff.truesize,
259 self->tx_buff.head, self->tx_buff_dma);
260 err_out2:
261 dma_free_coherent(NULL, self->rx_buff.truesize,
262 self->rx_buff.head, self->rx_buff_dma);
263 err_out1:
264 free_netdev(dev);
265 err_out:
266 release_region(iobase, CHIP_IO_EXTENT);
267 return err;
271 * Function w83977af_close (self)
273 * Close driver instance
276 static int w83977af_close(struct w83977af_ir *self)
278 int iobase;
280 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
282 iobase = self->io.fir_base;
284 #ifdef CONFIG_USE_W977_PNP
285 /* enter PnP configuration mode */
286 w977_efm_enter(efio);
288 w977_select_device(W977_DEVICE_IR, efio);
290 /* Deactivate device */
291 w977_write_reg(0x30, 0x00, efio);
293 w977_efm_exit(efio);
294 #endif /* CONFIG_USE_W977_PNP */
296 /* Remove netdevice */
297 unregister_netdev(self->netdev);
299 /* Release the PORT that this driver is using */
300 IRDA_DEBUG(0 , "%s(), Releasing Region %03x\n",
301 __FUNCTION__ , self->io.fir_base);
302 release_region(self->io.fir_base, self->io.fir_ext);
304 if (self->tx_buff.head)
305 dma_free_coherent(NULL, self->tx_buff.truesize,
306 self->tx_buff.head, self->tx_buff_dma);
308 if (self->rx_buff.head)
309 dma_free_coherent(NULL, self->rx_buff.truesize,
310 self->rx_buff.head, self->rx_buff_dma);
312 free_netdev(self->netdev);
314 return 0;
317 int w83977af_probe( int iobase, int irq, int dma)
319 int version;
320 int i;
322 for (i=0; i < 2; i++) {
323 IRDA_DEBUG( 0, "%s()\n", __FUNCTION__ );
324 #ifdef CONFIG_USE_W977_PNP
325 /* Enter PnP configuration mode */
326 w977_efm_enter(efbase[i]);
328 w977_select_device(W977_DEVICE_IR, efbase[i]);
330 /* Configure PnP port, IRQ, and DMA channel */
331 w977_write_reg(0x60, (iobase >> 8) & 0xff, efbase[i]);
332 w977_write_reg(0x61, (iobase) & 0xff, efbase[i]);
334 w977_write_reg(0x70, irq, efbase[i]);
335 #ifdef CONFIG_ARCH_NETWINDER
336 /* Netwinder uses 1 higher than Linux */
337 w977_write_reg(0x74, dma+1, efbase[i]);
338 #else
339 w977_write_reg(0x74, dma, efbase[i]);
340 #endif /*CONFIG_ARCH_NETWINDER */
341 w977_write_reg(0x75, 0x04, efbase[i]); /* Disable Tx DMA */
343 /* Set append hardware CRC, enable IR bank selection */
344 w977_write_reg(0xf0, APEDCRC|ENBNKSEL, efbase[i]);
346 /* Activate device */
347 w977_write_reg(0x30, 0x01, efbase[i]);
349 w977_efm_exit(efbase[i]);
350 #endif /* CONFIG_USE_W977_PNP */
351 /* Disable Advanced mode */
352 switch_bank(iobase, SET2);
353 outb(iobase+2, 0x00);
355 /* Turn on UART (global) interrupts */
356 switch_bank(iobase, SET0);
357 outb(HCR_EN_IRQ, iobase+HCR);
359 /* Switch to advanced mode */
360 switch_bank(iobase, SET2);
361 outb(inb(iobase+ADCR1) | ADCR1_ADV_SL, iobase+ADCR1);
363 /* Set default IR-mode */
364 switch_bank(iobase, SET0);
365 outb(HCR_SIR, iobase+HCR);
367 /* Read the Advanced IR ID */
368 switch_bank(iobase, SET3);
369 version = inb(iobase+AUID);
371 /* Should be 0x1? */
372 if (0x10 == (version & 0xf0)) {
373 efio = efbase[i];
375 /* Set FIFO size to 32 */
376 switch_bank(iobase, SET2);
377 outb(ADCR2_RXFS32|ADCR2_TXFS32, iobase+ADCR2);
379 /* Set FIFO threshold to TX17, RX16 */
380 switch_bank(iobase, SET0);
381 outb(UFR_RXTL|UFR_TXTL|UFR_TXF_RST|UFR_RXF_RST|
382 UFR_EN_FIFO,iobase+UFR);
384 /* Receiver frame length */
385 switch_bank(iobase, SET4);
386 outb(2048 & 0xff, iobase+6);
387 outb((2048 >> 8) & 0x1f, iobase+7);
390 * Init HP HSDL-1100 transceiver.
392 * Set IRX_MSL since we have 2 * receive paths IRRX,
393 * and IRRXH. Clear IRSL0D since we want IRSL0 * to
394 * be a input pin used for IRRXH
396 * IRRX pin 37 connected to receiver
397 * IRTX pin 38 connected to transmitter
398 * FIRRX pin 39 connected to receiver (IRSL0)
399 * CIRRX pin 40 connected to pin 37
401 switch_bank(iobase, SET7);
402 outb(0x40, iobase+7);
404 IRDA_MESSAGE("W83977AF (IR) driver loaded. "
405 "Version: 0x%02x\n", version);
407 return 0;
408 } else {
409 /* Try next extented function register address */
410 IRDA_DEBUG( 0, "%s(), Wrong chip version", __FUNCTION__ );
413 return -1;
416 void w83977af_change_speed(struct w83977af_ir *self, __u32 speed)
418 int ir_mode = HCR_SIR;
419 int iobase;
420 __u8 set;
422 iobase = self->io.fir_base;
424 /* Update accounting for new speed */
425 self->io.speed = speed;
427 /* Save current bank */
428 set = inb(iobase+SSR);
430 /* Disable interrupts */
431 switch_bank(iobase, SET0);
432 outb(0, iobase+ICR);
434 /* Select Set 2 */
435 switch_bank(iobase, SET2);
436 outb(0x00, iobase+ABHL);
438 switch (speed) {
439 case 9600: outb(0x0c, iobase+ABLL); break;
440 case 19200: outb(0x06, iobase+ABLL); break;
441 case 38400: outb(0x03, iobase+ABLL); break;
442 case 57600: outb(0x02, iobase+ABLL); break;
443 case 115200: outb(0x01, iobase+ABLL); break;
444 case 576000:
445 ir_mode = HCR_MIR_576;
446 IRDA_DEBUG(0, "%s(), handling baud of 576000\n", __FUNCTION__ );
447 break;
448 case 1152000:
449 ir_mode = HCR_MIR_1152;
450 IRDA_DEBUG(0, "%s(), handling baud of 1152000\n", __FUNCTION__ );
451 break;
452 case 4000000:
453 ir_mode = HCR_FIR;
454 IRDA_DEBUG(0, "%s(), handling baud of 4000000\n", __FUNCTION__ );
455 break;
456 default:
457 ir_mode = HCR_FIR;
458 IRDA_DEBUG(0, "%s(), unknown baud rate of %d\n", __FUNCTION__ , speed);
459 break;
462 /* Set speed mode */
463 switch_bank(iobase, SET0);
464 outb(ir_mode, iobase+HCR);
466 /* set FIFO size to 32 */
467 switch_bank(iobase, SET2);
468 outb(ADCR2_RXFS32|ADCR2_TXFS32, iobase+ADCR2);
470 /* set FIFO threshold to TX17, RX16 */
471 switch_bank(iobase, SET0);
472 outb(0x00, iobase+UFR); /* Reset */
473 outb(UFR_EN_FIFO, iobase+UFR); /* First we must enable FIFO */
474 outb(0xa7, iobase+UFR);
476 netif_wake_queue(self->netdev);
478 /* Enable some interrupts so we can receive frames */
479 switch_bank(iobase, SET0);
480 if (speed > PIO_MAX_SPEED) {
481 outb(ICR_EFSFI, iobase+ICR);
482 w83977af_dma_receive(self);
483 } else
484 outb(ICR_ERBRI, iobase+ICR);
486 /* Restore SSR */
487 outb(set, iobase+SSR);
491 * Function w83977af_hard_xmit (skb, dev)
493 * Sets up a DMA transfer to send the current frame.
496 int w83977af_hard_xmit(struct sk_buff *skb, struct net_device *dev)
498 struct w83977af_ir *self;
499 __s32 speed;
500 int iobase;
501 __u8 set;
502 int mtt;
504 self = (struct w83977af_ir *) dev->priv;
506 iobase = self->io.fir_base;
508 IRDA_DEBUG(4, "%s(%ld), skb->len=%d\n", __FUNCTION__ , jiffies,
509 (int) skb->len);
511 /* Lock transmit buffer */
512 netif_stop_queue(dev);
514 /* Check if we need to change the speed */
515 speed = irda_get_next_speed(skb);
516 if ((speed != self->io.speed) && (speed != -1)) {
517 /* Check for empty frame */
518 if (!skb->len) {
519 w83977af_change_speed(self, speed);
520 dev->trans_start = jiffies;
521 dev_kfree_skb(skb);
522 return 0;
523 } else
524 self->new_speed = speed;
527 /* Save current set */
528 set = inb(iobase+SSR);
530 /* Decide if we should use PIO or DMA transfer */
531 if (self->io.speed > PIO_MAX_SPEED) {
532 self->tx_buff.data = self->tx_buff.head;
533 memcpy(self->tx_buff.data, skb->data, skb->len);
534 self->tx_buff.len = skb->len;
536 mtt = irda_get_mtt(skb);
537 #ifdef CONFIG_USE_INTERNAL_TIMER
538 if (mtt > 50) {
539 /* Adjust for timer resolution */
540 mtt /= 1000+1;
542 /* Setup timer */
543 switch_bank(iobase, SET4);
544 outb(mtt & 0xff, iobase+TMRL);
545 outb((mtt >> 8) & 0x0f, iobase+TMRH);
547 /* Start timer */
548 outb(IR_MSL_EN_TMR, iobase+IR_MSL);
549 self->io.direction = IO_XMIT;
551 /* Enable timer interrupt */
552 switch_bank(iobase, SET0);
553 outb(ICR_ETMRI, iobase+ICR);
554 } else {
555 #endif
556 IRDA_DEBUG(4, "%s(%ld), mtt=%d\n", __FUNCTION__ , jiffies, mtt);
557 if (mtt)
558 udelay(mtt);
560 /* Enable DMA interrupt */
561 switch_bank(iobase, SET0);
562 outb(ICR_EDMAI, iobase+ICR);
563 w83977af_dma_write(self, iobase);
564 #ifdef CONFIG_USE_INTERNAL_TIMER
566 #endif
567 } else {
568 self->tx_buff.data = self->tx_buff.head;
569 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
570 self->tx_buff.truesize);
572 /* Add interrupt on tx low level (will fire immediately) */
573 switch_bank(iobase, SET0);
574 outb(ICR_ETXTHI, iobase+ICR);
576 dev->trans_start = jiffies;
577 dev_kfree_skb(skb);
579 /* Restore set register */
580 outb(set, iobase+SSR);
582 return 0;
586 * Function w83977af_dma_write (self, iobase)
588 * Send frame using DMA
591 static void w83977af_dma_write(struct w83977af_ir *self, int iobase)
593 __u8 set;
594 #ifdef CONFIG_NETWINDER_TX_DMA_PROBLEMS
595 unsigned long flags;
596 __u8 hcr;
597 #endif
598 IRDA_DEBUG(4, "%s(), len=%d\n", __FUNCTION__ , self->tx_buff.len);
600 /* Save current set */
601 set = inb(iobase+SSR);
603 /* Disable DMA */
604 switch_bank(iobase, SET0);
605 outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
607 /* Choose transmit DMA channel */
608 switch_bank(iobase, SET2);
609 outb(ADCR1_D_CHSW|/*ADCR1_DMA_F|*/ADCR1_ADV_SL, iobase+ADCR1);
610 #ifdef CONFIG_NETWINDER_TX_DMA_PROBLEMS
611 spin_lock_irqsave(&self->lock, flags);
613 disable_dma(self->io.dma);
614 clear_dma_ff(self->io.dma);
615 set_dma_mode(self->io.dma, DMA_MODE_READ);
616 set_dma_addr(self->io.dma, self->tx_buff_dma);
617 set_dma_count(self->io.dma, self->tx_buff.len);
618 #else
619 irda_setup_dma(self->io.dma, self->tx_buff_dma, self->tx_buff.len,
620 DMA_MODE_WRITE);
621 #endif
622 self->io.direction = IO_XMIT;
624 /* Enable DMA */
625 switch_bank(iobase, SET0);
626 #ifdef CONFIG_NETWINDER_TX_DMA_PROBLEMS
627 hcr = inb(iobase+HCR);
628 outb(hcr | HCR_EN_DMA, iobase+HCR);
629 enable_dma(self->io.dma);
630 spin_unlock_irqrestore(&self->lock, flags);
631 #else
632 outb(inb(iobase+HCR) | HCR_EN_DMA | HCR_TX_WT, iobase+HCR);
633 #endif
635 /* Restore set register */
636 outb(set, iobase+SSR);
640 * Function w83977af_pio_write (iobase, buf, len, fifo_size)
645 static int w83977af_pio_write(int iobase, __u8 *buf, int len, int fifo_size)
647 int actual = 0;
648 __u8 set;
650 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
652 /* Save current bank */
653 set = inb(iobase+SSR);
655 switch_bank(iobase, SET0);
656 if (!(inb_p(iobase+USR) & USR_TSRE)) {
657 IRDA_DEBUG(4,
658 "%s(), warning, FIFO not empty yet!\n", __FUNCTION__ );
660 fifo_size -= 17;
661 IRDA_DEBUG(4, "%s(), %d bytes left in tx fifo\n",
662 __FUNCTION__ , fifo_size);
665 /* Fill FIFO with current frame */
666 while ((fifo_size-- > 0) && (actual < len)) {
667 /* Transmit next byte */
668 outb(buf[actual++], iobase+TBR);
671 IRDA_DEBUG(4, "%s(), fifo_size %d ; %d sent of %d\n",
672 __FUNCTION__ , fifo_size, actual, len);
674 /* Restore bank */
675 outb(set, iobase+SSR);
677 return actual;
681 * Function w83977af_dma_xmit_complete (self)
683 * The transfer of a frame in finished. So do the necessary things
687 static void w83977af_dma_xmit_complete(struct w83977af_ir *self)
689 int iobase;
690 __u8 set;
692 IRDA_DEBUG(4, "%s(%ld)\n", __FUNCTION__ , jiffies);
694 IRDA_ASSERT(self != NULL, return;);
696 iobase = self->io.fir_base;
698 /* Save current set */
699 set = inb(iobase+SSR);
701 /* Disable DMA */
702 switch_bank(iobase, SET0);
703 outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
705 /* Check for underrrun! */
706 if (inb(iobase+AUDR) & AUDR_UNDR) {
707 IRDA_DEBUG(0, "%s(), Transmit underrun!\n", __FUNCTION__ );
709 self->stats.tx_errors++;
710 self->stats.tx_fifo_errors++;
712 /* Clear bit, by writing 1 to it */
713 outb(AUDR_UNDR, iobase+AUDR);
714 } else
715 self->stats.tx_packets++;
718 if (self->new_speed) {
719 w83977af_change_speed(self, self->new_speed);
720 self->new_speed = 0;
723 /* Unlock tx_buff and request another frame */
724 /* Tell the network layer, that we want more frames */
725 netif_wake_queue(self->netdev);
727 /* Restore set */
728 outb(set, iobase+SSR);
732 * Function w83977af_dma_receive (self)
734 * Get ready for receiving a frame. The device will initiate a DMA
735 * if it starts to receive a frame.
738 int w83977af_dma_receive(struct w83977af_ir *self)
740 int iobase;
741 __u8 set;
742 #ifdef CONFIG_NETWINDER_RX_DMA_PROBLEMS
743 unsigned long flags;
744 __u8 hcr;
745 #endif
746 IRDA_ASSERT(self != NULL, return -1;);
748 IRDA_DEBUG(4, "%s\n", __FUNCTION__ );
750 iobase= self->io.fir_base;
752 /* Save current set */
753 set = inb(iobase+SSR);
755 /* Disable DMA */
756 switch_bank(iobase, SET0);
757 outb(inb(iobase+HCR) & ~HCR_EN_DMA, iobase+HCR);
759 /* Choose DMA Rx, DMA Fairness, and Advanced mode */
760 switch_bank(iobase, SET2);
761 outb((inb(iobase+ADCR1) & ~ADCR1_D_CHSW)/*|ADCR1_DMA_F*/|ADCR1_ADV_SL,
762 iobase+ADCR1);
764 self->io.direction = IO_RECV;
765 self->rx_buff.data = self->rx_buff.head;
767 #ifdef CONFIG_NETWINDER_RX_DMA_PROBLEMS
768 spin_lock_irqsave(&self->lock, flags);
770 disable_dma(self->io.dma);
771 clear_dma_ff(self->io.dma);
772 set_dma_mode(self->io.dma, DMA_MODE_READ);
773 set_dma_addr(self->io.dma, self->rx_buff_dma);
774 set_dma_count(self->io.dma, self->rx_buff.truesize);
775 #else
776 irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
777 DMA_MODE_READ);
778 #endif
780 * Reset Rx FIFO. This will also flush the ST_FIFO, it's very
781 * important that we don't reset the Tx FIFO since it might not
782 * be finished transmitting yet
784 switch_bank(iobase, SET0);
785 outb(UFR_RXTL|UFR_TXTL|UFR_RXF_RST|UFR_EN_FIFO, iobase+UFR);
786 self->st_fifo.len = self->st_fifo.tail = self->st_fifo.head = 0;
788 /* Enable DMA */
789 switch_bank(iobase, SET0);
790 #ifdef CONFIG_NETWINDER_RX_DMA_PROBLEMS
791 hcr = inb(iobase+HCR);
792 outb(hcr | HCR_EN_DMA, iobase+HCR);
793 enable_dma(self->io.dma);
794 spin_unlock_irqrestore(&self->lock, flags);
795 #else
796 outb(inb(iobase+HCR) | HCR_EN_DMA, iobase+HCR);
797 #endif
798 /* Restore set */
799 outb(set, iobase+SSR);
801 return 0;
805 * Function w83977af_receive_complete (self)
807 * Finished with receiving a frame
810 int w83977af_dma_receive_complete(struct w83977af_ir *self)
812 struct sk_buff *skb;
813 struct st_fifo *st_fifo;
814 int len;
815 int iobase;
816 __u8 set;
817 __u8 status;
819 IRDA_DEBUG(4, "%s\n", __FUNCTION__ );
821 st_fifo = &self->st_fifo;
823 iobase = self->io.fir_base;
825 /* Save current set */
826 set = inb(iobase+SSR);
828 iobase = self->io.fir_base;
830 /* Read status FIFO */
831 switch_bank(iobase, SET5);
832 while ((status = inb(iobase+FS_FO)) & FS_FO_FSFDR) {
833 st_fifo->entries[st_fifo->tail].status = status;
835 st_fifo->entries[st_fifo->tail].len = inb(iobase+RFLFL);
836 st_fifo->entries[st_fifo->tail].len |= inb(iobase+RFLFH) << 8;
838 st_fifo->tail++;
839 st_fifo->len++;
842 while (st_fifo->len) {
843 /* Get first entry */
844 status = st_fifo->entries[st_fifo->head].status;
845 len = st_fifo->entries[st_fifo->head].len;
846 st_fifo->head++;
847 st_fifo->len--;
849 /* Check for errors */
850 if (status & FS_FO_ERR_MSK) {
851 if (status & FS_FO_LST_FR) {
852 /* Add number of lost frames to stats */
853 self->stats.rx_errors += len;
854 } else {
855 /* Skip frame */
856 self->stats.rx_errors++;
858 self->rx_buff.data += len;
860 if (status & FS_FO_MX_LEX)
861 self->stats.rx_length_errors++;
863 if (status & FS_FO_PHY_ERR)
864 self->stats.rx_frame_errors++;
866 if (status & FS_FO_CRC_ERR)
867 self->stats.rx_crc_errors++;
869 /* The errors below can be reported in both cases */
870 if (status & FS_FO_RX_OV)
871 self->stats.rx_fifo_errors++;
873 if (status & FS_FO_FSF_OV)
874 self->stats.rx_fifo_errors++;
876 } else {
877 /* Check if we have transferred all data to memory */
878 switch_bank(iobase, SET0);
879 if (inb(iobase+USR) & USR_RDR) {
880 #ifdef CONFIG_USE_INTERNAL_TIMER
881 /* Put this entry back in fifo */
882 st_fifo->head--;
883 st_fifo->len++;
884 st_fifo->entries[st_fifo->head].status = status;
885 st_fifo->entries[st_fifo->head].len = len;
887 /* Restore set register */
888 outb(set, iobase+SSR);
890 return FALSE; /* I'll be back! */
891 #else
892 udelay(80); /* Should be enough!? */
893 #endif
896 skb = dev_alloc_skb(len+1);
897 if (skb == NULL) {
898 printk(KERN_INFO
899 "%s(), memory squeeze, dropping frame.\n", __FUNCTION__);
900 /* Restore set register */
901 outb(set, iobase+SSR);
903 return FALSE;
906 /* Align to 20 bytes */
907 skb_reserve(skb, 1);
909 /* Copy frame without CRC */
910 if (self->io.speed < 4000000) {
911 skb_put(skb, len-2);
912 memcpy(skb->data, self->rx_buff.data, len-2);
913 } else {
914 skb_put(skb, len-4);
915 memcpy(skb->data, self->rx_buff.data, len-4);
918 /* Move to next frame */
919 self->rx_buff.data += len;
920 self->stats.rx_packets++;
922 skb->dev = self->netdev;
923 skb->mac.raw = skb->data;
924 skb->protocol = htons(ETH_P_IRDA);
925 netif_rx(skb);
926 self->netdev->last_rx = jiffies;
929 /* Restore set register */
930 outb(set, iobase+SSR);
932 return TRUE;
936 * Function pc87108_pio_receive (self)
938 * Receive all data in receiver FIFO
941 static void w83977af_pio_receive(struct w83977af_ir *self)
943 __u8 byte = 0x00;
944 int iobase;
946 IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
948 IRDA_ASSERT(self != NULL, return;);
950 iobase = self->io.fir_base;
952 /* Receive all characters in Rx FIFO */
953 do {
954 byte = inb(iobase+RBR);
955 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
956 byte);
957 } while (inb(iobase+USR) & USR_RDR); /* Data available */
961 * Function w83977af_sir_interrupt (self, eir)
963 * Handle SIR interrupt
966 static __u8 w83977af_sir_interrupt(struct w83977af_ir *self, int isr)
968 int actual;
969 __u8 new_icr = 0;
970 __u8 set;
971 int iobase;
973 IRDA_DEBUG(4, "%s(), isr=%#x\n", __FUNCTION__ , isr);
975 iobase = self->io.fir_base;
976 /* Transmit FIFO low on data */
977 if (isr & ISR_TXTH_I) {
978 /* Write data left in transmit buffer */
979 actual = w83977af_pio_write(self->io.fir_base,
980 self->tx_buff.data,
981 self->tx_buff.len,
982 self->io.fifo_size);
984 self->tx_buff.data += actual;
985 self->tx_buff.len -= actual;
987 self->io.direction = IO_XMIT;
989 /* Check if finished */
990 if (self->tx_buff.len > 0) {
991 new_icr |= ICR_ETXTHI;
992 } else {
993 set = inb(iobase+SSR);
994 switch_bank(iobase, SET0);
995 outb(AUDR_SFEND, iobase+AUDR);
996 outb(set, iobase+SSR);
998 self->stats.tx_packets++;
1000 /* Feed me more packets */
1001 netif_wake_queue(self->netdev);
1002 new_icr |= ICR_ETBREI;
1005 /* Check if transmission has completed */
1006 if (isr & ISR_TXEMP_I) {
1007 /* Check if we need to change the speed? */
1008 if (self->new_speed) {
1009 IRDA_DEBUG(2,
1010 "%s(), Changing speed!\n", __FUNCTION__ );
1011 w83977af_change_speed(self, self->new_speed);
1012 self->new_speed = 0;
1015 /* Turn around and get ready to receive some data */
1016 self->io.direction = IO_RECV;
1017 new_icr |= ICR_ERBRI;
1020 /* Rx FIFO threshold or timeout */
1021 if (isr & ISR_RXTH_I) {
1022 w83977af_pio_receive(self);
1024 /* Keep receiving */
1025 new_icr |= ICR_ERBRI;
1027 return new_icr;
1031 * Function pc87108_fir_interrupt (self, eir)
1033 * Handle MIR/FIR interrupt
1036 static __u8 w83977af_fir_interrupt(struct w83977af_ir *self, int isr)
1038 __u8 new_icr = 0;
1039 __u8 set;
1040 int iobase;
1042 iobase = self->io.fir_base;
1043 set = inb(iobase+SSR);
1045 /* End of frame detected in FIFO */
1046 if (isr & (ISR_FEND_I|ISR_FSF_I)) {
1047 if (w83977af_dma_receive_complete(self)) {
1049 /* Wait for next status FIFO interrupt */
1050 new_icr |= ICR_EFSFI;
1051 } else {
1052 /* DMA not finished yet */
1054 /* Set timer value, resolution 1 ms */
1055 switch_bank(iobase, SET4);
1056 outb(0x01, iobase+TMRL); /* 1 ms */
1057 outb(0x00, iobase+TMRH);
1059 /* Start timer */
1060 outb(IR_MSL_EN_TMR, iobase+IR_MSL);
1062 new_icr |= ICR_ETMRI;
1065 /* Timer finished */
1066 if (isr & ISR_TMR_I) {
1067 /* Disable timer */
1068 switch_bank(iobase, SET4);
1069 outb(0, iobase+IR_MSL);
1071 /* Clear timer event */
1072 /* switch_bank(iobase, SET0); */
1073 /* outb(ASCR_CTE, iobase+ASCR); */
1075 /* Check if this is a TX timer interrupt */
1076 if (self->io.direction == IO_XMIT) {
1077 w83977af_dma_write(self, iobase);
1079 new_icr |= ICR_EDMAI;
1080 } else {
1081 /* Check if DMA has now finished */
1082 w83977af_dma_receive_complete(self);
1084 new_icr |= ICR_EFSFI;
1087 /* Finished with DMA */
1088 if (isr & ISR_DMA_I) {
1089 w83977af_dma_xmit_complete(self);
1091 /* Check if there are more frames to be transmitted */
1092 /* if (irda_device_txqueue_empty(self)) { */
1094 /* Prepare for receive
1096 * ** Netwinder Tx DMA likes that we do this anyway **
1098 w83977af_dma_receive(self);
1099 new_icr = ICR_EFSFI;
1100 /* } */
1103 /* Restore set */
1104 outb(set, iobase+SSR);
1106 return new_icr;
1110 * Function w83977af_interrupt (irq, dev_id, regs)
1112 * An interrupt from the chip has arrived. Time to do some work
1115 static irqreturn_t w83977af_interrupt(int irq, void *dev_id,
1116 struct pt_regs *regs)
1118 struct net_device *dev = (struct net_device *) dev_id;
1119 struct w83977af_ir *self;
1120 __u8 set, icr, isr;
1121 int iobase;
1123 if (!dev) {
1124 printk(KERN_WARNING "%s: irq %d for unknown device.\n",
1125 driver_name, irq);
1126 return IRQ_NONE;
1128 self = (struct w83977af_ir *) dev->priv;
1130 iobase = self->io.fir_base;
1132 /* Save current bank */
1133 set = inb(iobase+SSR);
1134 switch_bank(iobase, SET0);
1136 icr = inb(iobase+ICR);
1137 isr = inb(iobase+ISR) & icr; /* Mask out the interesting ones */
1139 outb(0, iobase+ICR); /* Disable interrupts */
1141 if (isr) {
1142 /* Dispatch interrupt handler for the current speed */
1143 if (self->io.speed > PIO_MAX_SPEED )
1144 icr = w83977af_fir_interrupt(self, isr);
1145 else
1146 icr = w83977af_sir_interrupt(self, isr);
1149 outb(icr, iobase+ICR); /* Restore (new) interrupts */
1150 outb(set, iobase+SSR); /* Restore bank register */
1151 return IRQ_RETVAL(isr);
1155 * Function w83977af_is_receiving (self)
1157 * Return TRUE is we are currently receiving a frame
1160 static int w83977af_is_receiving(struct w83977af_ir *self)
1162 int status = FALSE;
1163 int iobase;
1164 __u8 set;
1166 IRDA_ASSERT(self != NULL, return FALSE;);
1168 if (self->io.speed > 115200) {
1169 iobase = self->io.fir_base;
1171 /* Check if rx FIFO is not empty */
1172 set = inb(iobase+SSR);
1173 switch_bank(iobase, SET2);
1174 if ((inb(iobase+RXFDTH) & 0x3f) != 0) {
1175 /* We are receiving something */
1176 status = TRUE;
1178 outb(set, iobase+SSR);
1179 } else
1180 status = (self->rx_buff.state != OUTSIDE_FRAME);
1182 return status;
1186 * Function w83977af_net_open (dev)
1188 * Start the device
1191 static int w83977af_net_open(struct net_device *dev)
1193 struct w83977af_ir *self;
1194 int iobase;
1195 char hwname[32];
1196 __u8 set;
1198 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
1200 IRDA_ASSERT(dev != NULL, return -1;);
1201 self = (struct w83977af_ir *) dev->priv;
1203 IRDA_ASSERT(self != NULL, return 0;);
1205 iobase = self->io.fir_base;
1207 if (request_irq(self->io.irq, w83977af_interrupt, 0, dev->name,
1208 (void *) dev)) {
1209 return -EAGAIN;
1212 * Always allocate the DMA channel after the IRQ,
1213 * and clean up on failure.
1215 if (request_dma(self->io.dma, dev->name)) {
1216 free_irq(self->io.irq, self);
1217 return -EAGAIN;
1220 /* Save current set */
1221 set = inb(iobase+SSR);
1223 /* Enable some interrupts so we can receive frames again */
1224 switch_bank(iobase, SET0);
1225 if (self->io.speed > 115200) {
1226 outb(ICR_EFSFI, iobase+ICR);
1227 w83977af_dma_receive(self);
1228 } else
1229 outb(ICR_ERBRI, iobase+ICR);
1231 /* Restore bank register */
1232 outb(set, iobase+SSR);
1234 /* Ready to play! */
1235 netif_start_queue(dev);
1237 /* Give self a hardware name */
1238 sprintf(hwname, "w83977af @ 0x%03x", self->io.fir_base);
1241 * Open new IrLAP layer instance, now that everything should be
1242 * initialized properly
1244 self->irlap = irlap_open(dev, &self->qos, hwname);
1246 return 0;
1250 * Function w83977af_net_close (dev)
1252 * Stop the device
1255 static int w83977af_net_close(struct net_device *dev)
1257 struct w83977af_ir *self;
1258 int iobase;
1259 __u8 set;
1261 IRDA_DEBUG(0, "%s()\n", __FUNCTION__ );
1263 IRDA_ASSERT(dev != NULL, return -1;);
1265 self = (struct w83977af_ir *) dev->priv;
1267 IRDA_ASSERT(self != NULL, return 0;);
1269 iobase = self->io.fir_base;
1271 /* Stop device */
1272 netif_stop_queue(dev);
1274 /* Stop and remove instance of IrLAP */
1275 if (self->irlap)
1276 irlap_close(self->irlap);
1277 self->irlap = NULL;
1279 disable_dma(self->io.dma);
1281 /* Save current set */
1282 set = inb(iobase+SSR);
1284 /* Disable interrupts */
1285 switch_bank(iobase, SET0);
1286 outb(0, iobase+ICR);
1288 free_irq(self->io.irq, dev);
1289 free_dma(self->io.dma);
1291 /* Restore bank register */
1292 outb(set, iobase+SSR);
1294 return 0;
1298 * Function w83977af_net_ioctl (dev, rq, cmd)
1300 * Process IOCTL commands for this device
1303 static int w83977af_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1305 struct if_irda_req *irq = (struct if_irda_req *) rq;
1306 struct w83977af_ir *self;
1307 unsigned long flags;
1308 int ret = 0;
1310 IRDA_ASSERT(dev != NULL, return -1;);
1312 self = dev->priv;
1314 IRDA_ASSERT(self != NULL, return -1;);
1316 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__ , dev->name, cmd);
1318 spin_lock_irqsave(&self->lock, flags);
1320 switch (cmd) {
1321 case SIOCSBANDWIDTH: /* Set bandwidth */
1322 if (!capable(CAP_NET_ADMIN)) {
1323 ret = -EPERM;
1324 goto out;
1326 w83977af_change_speed(self, irq->ifr_baudrate);
1327 break;
1328 case SIOCSMEDIABUSY: /* Set media busy */
1329 if (!capable(CAP_NET_ADMIN)) {
1330 ret = -EPERM;
1331 goto out;
1333 irda_device_set_media_busy(self->netdev, TRUE);
1334 break;
1335 case SIOCGRECEIVING: /* Check if we are receiving right now */
1336 irq->ifr_receiving = w83977af_is_receiving(self);
1337 break;
1338 default:
1339 ret = -EOPNOTSUPP;
1341 out:
1342 spin_unlock_irqrestore(&self->lock, flags);
1343 return ret;
1346 static struct net_device_stats *w83977af_net_get_stats(struct net_device *dev)
1348 struct w83977af_ir *self = (struct w83977af_ir *) dev->priv;
1350 return &self->stats;
1353 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1354 MODULE_DESCRIPTION("Winbond W83977AF IrDA Device Driver");
1355 MODULE_LICENSE("GPL");
1358 module_param(qos_mtt_bits, int, 0);
1359 MODULE_PARM_DESC(qos_mtt_bits, "Mimimum Turn Time");
1360 module_param_array(io, int, NULL, 0);
1361 MODULE_PARM_DESC(io, "Base I/O addresses");
1362 module_param_array(irq, int, NULL, 0);
1363 MODULE_PARM_DESC(irq, "IRQ lines");
1366 * Function init_module (void)
1371 module_init(w83977af_init);
1374 * Function cleanup_module (void)
1379 module_exit(w83977af_cleanup);