1 /*********************************************************************
3 * Filename: w83977af_ir.c
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: Wed Aug 11 09:27:54 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>
13 * Copyright (c) 1998 Corel Computer Corp.
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 Corel Computer Corp. admit liability
21 * nor provide warranty for any of this software. This material is
22 * provided "AS-IS" 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 is quite
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!
34 * bank = inb( iobase+BSR);
36 * do_your_stuff_here();
38 * outb( bank, iobase+BSR);
40 ********************************************************************/
42 #include <linux/module.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/malloc.h>
51 #include <linux/init.h>
55 #include <asm/byteorder.h>
57 #include <net/irda/irda.h>
58 #include <net/irda/irmod.h>
59 #include <net/irda/wrapper.h>
60 #include <net/irda/irda_device.h>
61 #include <net/irda/w83977af.h>
62 #include <net/irda/w83977af_ir.h>
64 #define CONFIG_NETWINDER /* Adjust to NetWinder differences */
65 #undef CONFIG_NETWINDER_TX_DMA_PROBLEMS /* Not needed */
66 #define CONFIG_NETWINDER_RX_DMA_PROBLEMS /* Must have this one! */
67 #undef CONFIG_USE_INTERNAL_TIMER /* Just cannot make that timer work */
68 #define CONFIG_USE_W977_PNP /* Currently needed */
69 #define PIO_MAX_SPEED 115200
71 static char *driver_name
= "w83977af_ir";
72 static int qos_mtt_bits
= 0x07; /* 1 ms or more */
74 #define CHIP_IO_EXTENT 8
76 static unsigned int io
[] = { 0x180, ~0, ~0, ~0 };
77 static unsigned int irq
[] = { 6, 0, 0, 0 };
78 static unsigned int dma
[] =
81 static struct w83977af_ir
*dev_self
[] = { NULL
, NULL
, NULL
, NULL
};
84 static int w83977af_open(int i
, unsigned int iobase
, unsigned int irq
,
86 static int w83977af_close(struct irda_device
*idev
);
87 static int w83977af_probe(int iobase
, int irq
, int dma
);
88 static int w83977af_dma_receive(struct irda_device
*idev
);
89 static int w83977af_dma_receive_complete(struct irda_device
*idev
);
90 static int w83977af_hard_xmit(struct sk_buff
*skb
, struct net_device
*dev
);
91 static int w83977af_pio_write(int iobase
, __u8
*buf
, int len
, int fifo_size
);
92 static void w83977af_dma_write(struct irda_device
*idev
, int iobase
);
93 static void w83977af_change_speed(struct irda_device
*idev
, __u32 speed
);
94 static void w83977af_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
);
95 static void w83977af_wait_until_sent(struct irda_device
*idev
);
96 static int w83977af_is_receiving(struct irda_device
*idev
);
98 static int w83977af_net_init(struct net_device
*dev
);
99 static int w83977af_net_open(struct net_device
*dev
);
100 static int w83977af_net_close(struct net_device
*dev
);
103 * Function w83977af_init ()
105 * Initialize chip. Just try to find out how many chips we are dealing with
108 int __init
w83977af_init(void)
112 DEBUG(0, __FUNCTION__
"()\n");
114 for (i
=0; (io
[i
] < 2000) && (i
< 4); i
++) {
116 if (check_region(ioaddr
, CHIP_IO_EXTENT
) < 0)
118 if (w83977af_open( i
, io
[i
], irq
[i
], dma
[i
]) == 0)
125 * Function w83977af_cleanup ()
127 * Close all configured chips
131 void w83977af_cleanup(void)
135 DEBUG(4, __FUNCTION__
"()\n");
137 for (i
=0; i
< 4; i
++) {
139 w83977af_close(&(dev_self
[i
]->idev
));
145 * Function w83977af_open (iobase, irq)
147 * Open driver instance
150 int w83977af_open( int i
, unsigned int iobase
, unsigned int irq
,
153 struct irda_device
*idev
;
154 struct w83977af_ir
*self
;
157 DEBUG( 0, __FUNCTION__
"()\n");
159 if (w83977af_probe(iobase
, irq
, dma
) == -1)
163 * Allocate new instance of the driver
165 self
= kmalloc(sizeof(struct w83977af_ir
), GFP_KERNEL
);
167 printk( KERN_ERR
"IrDA: Can't allocate memory for "
168 "IrDA control block!\n");
171 memset(self
, 0, sizeof(struct w83977af_ir
));
173 /* Need to store self somewhere */
179 idev
->io
.iobase
= iobase
;
181 idev
->io
.io_ext
= CHIP_IO_EXTENT
;
183 idev
->io
.fifo_size
= 32;
185 /* Lock the port that we need */
186 ret
= check_region(idev
->io
.iobase
, idev
->io
.io_ext
);
188 DEBUG( 0, __FUNCTION__
"(), can't get iobase of 0x%03x\n",
190 /* w83977af_cleanup( self->idev); */
193 request_region(idev
->io
.iobase
, idev
->io
.io_ext
, idev
->name
);
195 /* Initialize QoS for this device */
196 irda_init_max_qos_capabilies(&idev
->qos
);
198 /* The only value we must override it the baudrate */
200 /* FIXME: The HP HDLS-1100 does not support 1152000! */
201 idev
->qos
.baud_rate
.bits
= IR_9600
|IR_19200
|IR_38400
|IR_57600
|
202 IR_115200
|IR_576000
|IR_1152000
|(IR_4000000
<< 8);
204 /* The HP HDLS-1100 needs 1 ms according to the specs */
205 idev
->qos
.min_turn_time
.bits
= qos_mtt_bits
;
206 irda_qos_bits_to_value(&idev
->qos
);
208 idev
->flags
= IFF_FIR
|IFF_MIR
|IFF_SIR
|IFF_DMA
|IFF_PIO
;
210 /* Specify which buffer allocation policy we need */
211 idev
->rx_buff
.flags
= GFP_KERNEL
| GFP_DMA
;
212 idev
->tx_buff
.flags
= GFP_KERNEL
| GFP_DMA
;
214 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
215 idev
->rx_buff
.truesize
= 14384;
216 idev
->tx_buff
.truesize
= 4000;
218 /* Initialize callbacks */
219 idev
->change_speed
= w83977af_change_speed
;
220 idev
->wait_until_sent
= w83977af_wait_until_sent
;
221 idev
->is_receiving
= w83977af_is_receiving
;
223 /* Override the network functions we need to use */
224 idev
->netdev
.init
= w83977af_net_init
;
225 idev
->netdev
.hard_start_xmit
= w83977af_hard_xmit
;
226 idev
->netdev
.open
= w83977af_net_open
;
227 idev
->netdev
.stop
= w83977af_net_close
;
229 /* Open the IrDA device */
230 irda_device_open(idev
, driver_name
, self
);
236 * Function w83977af_close (idev)
238 * Close driver instance
241 static int w83977af_close( struct irda_device
*idev
)
243 struct w83977af_ir
*self
;
246 DEBUG(0, __FUNCTION__
"()\n");
248 ASSERT(idev
!= NULL
, return -1;);
249 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return -1;);
251 iobase
= idev
->io
.iobase
;
252 self
= (struct w83977af_ir
*) idev
->priv
;
254 #ifdef CONFIG_USE_W977_PNP
255 /* enter PnP configuration mode */
258 w977_select_device(W977_DEVICE_IR
);
260 /* Deactivate device */
261 w977_write_reg(0x30, 0x00);
264 #endif /* CONFIG_USE_W977_PNP */
265 /* Release the PORT that this driver is using */
266 DEBUG(0 , __FUNCTION__
"(), Releasing Region %03x\n",
268 release_region(idev
->io
.iobase
, idev
->io
.io_ext
);
270 irda_device_close(idev
);
278 * Function w83977af_probe (iobase, irq, dma)
280 * Returns non-negative on success.
283 int w83977af_probe( int iobase
, int irq
, int dma
)
287 DEBUG( 0, __FUNCTION__
"()\n");
288 #ifdef CONFIG_USE_W977_PNP
289 /* Enter PnP configuration mode */
292 w977_select_device(W977_DEVICE_IR
);
294 /* Configure PnP port, IRQ, and DMA channel */
295 w977_write_reg(0x60, (iobase
>> 8) & 0xff);
296 w977_write_reg(0x61, (iobase
) & 0xff);
298 w977_write_reg(0x70, irq
);
299 #ifdef CONFIG_NETWINDER
300 w977_write_reg(0x74, dma
+1); /* Netwinder uses 1 higher than Linux */
302 w977_write_reg(0x74, dma
);
304 w977_write_reg(0x75, 0x04); /* Disable Tx DMA */
306 /* Set append hardware CRC, enable IR bank selection */
307 w977_write_reg(0xf0, APEDCRC
|ENBNKSEL
);
309 /* Activate device */
310 w977_write_reg(0x30, 0x01);
314 /* Disable Advanced mode */
315 switch_bank(iobase
, SET2
);
316 outb(iobase
+2, 0x00);
318 /* Turn on UART (global) interrupts */
319 switch_bank(iobase
, SET0
);
320 outb(HCR_EN_IRQ
, iobase
+HCR
);
322 /* Switch to advanced mode */
323 switch_bank(iobase
, SET2
);
324 outb(inb(iobase
+ADCR1
) | ADCR1_ADV_SL
, iobase
+ADCR1
);
326 /* Set default IR-mode */
327 switch_bank(iobase
, SET0
);
328 outb(HCR_SIR
, iobase
+HCR
);
330 /* Read the Advanced IR ID */
331 switch_bank(iobase
, SET3
);
332 version
= inb(iobase
+AUID
);
335 if (0x10 != (version
& 0xf0)) {
336 DEBUG( 0, __FUNCTION__
"(), Wrong chip version");
340 /* Set FIFO size to 32 */
341 switch_bank(iobase
, SET2
);
342 outb(ADCR2_RXFS32
|ADCR2_TXFS32
, iobase
+ADCR2
);
344 /* Set FIFO threshold to TX17, RX16 */
345 switch_bank(iobase
, SET0
);
346 outb(UFR_RXTL
|UFR_TXTL
|UFR_TXF_RST
|UFR_RXF_RST
|UFR_EN_FIFO
,iobase
+UFR
);
348 /* Receiver frame length */
349 switch_bank(iobase
, SET4
);
350 outb(2048 & 0xff, iobase
+6);
351 outb((2048 >> 8) & 0x1f, iobase
+7);
354 * Init HP HSDL-1100 transceiver.
356 * Set IRX_MSL since we have 2 * receive paths IRRX, and
357 * IRRXH. Clear IRSL0D since we want IRSL0 * to be a input pin used
360 * IRRX pin 37 connected to receiver
361 * IRTX pin 38 connected to transmitter
362 * FIRRX pin 39 connected to receiver (IRSL0)
363 * CIRRX pin 40 connected to pin 37
365 switch_bank(iobase
, SET7
);
366 outb(0x40, iobase
+7);
368 DEBUG(0, "W83977AF (IR) driver loaded. Version: 0x%02x\n", version
);
374 * Function w83977af_change_speed (idev, baud)
376 * Change the speed of the device
379 void w83977af_change_speed(struct irda_device
*idev
, __u32 speed
)
381 int ir_mode
= HCR_SIR
;
385 ASSERT(idev
!= NULL
, return;);
386 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return;);
388 iobase
= idev
->io
.iobase
;
390 /* Update accounting for new speed */
391 idev
->io
.baudrate
= speed
;
393 /* Save current bank */
394 set
= inb(iobase
+SSR
);
396 /* Disable interrupts */
397 switch_bank(iobase
, SET0
);
401 switch_bank(iobase
, SET2
);
402 outb(0x00, iobase
+ABHL
);
405 case 9600: outb(0x0c, iobase
+ABLL
); break;
406 case 19200: outb(0x06, iobase
+ABLL
); break;
407 case 37600: outb(0x03, iobase
+ABLL
); break;
408 case 57600: outb(0x02, iobase
+ABLL
); break;
409 case 115200: outb(0x01, iobase
+ABLL
); break;
411 ir_mode
= HCR_MIR_576
;
412 DEBUG(0, __FUNCTION__
"(), handling baud of 576000\n");
415 ir_mode
= HCR_MIR_1152
;
416 DEBUG(0, __FUNCTION__
"(), handling baud of 1152000\n");
420 DEBUG(0, __FUNCTION__
"(), handling baud of 4000000\n");
424 DEBUG(0, __FUNCTION__
"(), unknown baud rate of %d\n", speed
);
429 switch_bank(iobase
, SET0
);
430 outb(ir_mode
, iobase
+HCR
);
432 /* set FIFO size to 32 */
433 switch_bank(iobase
, SET2
);
434 outb(ADCR2_RXFS32
|ADCR2_TXFS32
, iobase
+ADCR2
);
436 /* set FIFO threshold to TX17, RX16 */
437 switch_bank(iobase
, SET0
);
439 outb(0x00, iobase
+UFR
); /* Reset */
440 outb(UFR_EN_FIFO
, iobase
+UFR
); /* First we must enable FIFO */
441 outb(0xa7, iobase
+UFR
);
443 idev
->netdev
.tbusy
= 0;
445 /* Enable some interrupts so we can receive frames */
446 switch_bank(iobase
, SET0
);
447 if (speed
> PIO_MAX_SPEED
) {
448 outb(ICR_EFSFI
, iobase
+ICR
);
449 w83977af_dma_receive(idev
);
451 outb(ICR_ERBRI
, iobase
+ICR
);
454 outb(set
, iobase
+SSR
);
458 * Function w83977af_hard_xmit (skb, dev)
460 * Sets up a DMA transfer to send the current frame.
463 int w83977af_hard_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
465 struct irda_device
*idev
;
470 idev
= (struct irda_device
*) dev
->priv
;
472 ASSERT(idev
!= NULL
, return 0;);
473 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return 0;);
475 iobase
= idev
->io
.iobase
;
477 DEBUG(4, __FUNCTION__
"(%ld), skb->len=%d\n", jiffies
, (int) skb
->len
);
479 /* Lock transmit buffer */
480 if (irda_lock((void *) &dev
->tbusy
) == FALSE
)
483 /* Save current set */
484 set
= inb(iobase
+SSR
);
486 /* Decide if we should use PIO or DMA transfer */
487 if (idev
->io
.baudrate
> PIO_MAX_SPEED
) {
488 idev
->tx_buff
.data
= idev
->tx_buff
.head
;
489 memcpy(idev
->tx_buff
.data
, skb
->data
, skb
->len
);
490 idev
->tx_buff
.len
= skb
->len
;
492 mtt
= irda_get_mtt(skb
);
493 #ifdef CONFIG_USE_INTERNAL_TIMER
495 /* Adjust for timer resolution */
499 switch_bank(iobase
, SET4
);
500 outb(mtt
& 0xff, iobase
+TMRL
);
501 outb((mtt
>> 8) & 0x0f, iobase
+TMRH
);
504 outb(IR_MSL_EN_TMR
, iobase
+IR_MSL
);
505 idev
->io
.direction
= IO_XMIT
;
507 /* Enable timer interrupt */
508 switch_bank(iobase
, SET0
);
509 outb(ICR_ETMRI
, iobase
+ICR
);
512 DEBUG(4,__FUNCTION__
"(%ld), mtt=%d\n", jiffies
, mtt
);
516 /* Enable DMA interrupt */
517 switch_bank(iobase
, SET0
);
518 outb(ICR_EDMAI
, iobase
+ICR
);
519 w83977af_dma_write(idev
, iobase
);
520 #ifdef CONFIG_USE_INTERNAL_TIMER
524 idev
->tx_buff
.data
= idev
->tx_buff
.head
;
525 idev
->tx_buff
.len
= async_wrap_skb(skb
, idev
->tx_buff
.data
,
526 idev
->tx_buff
.truesize
);
528 /* Add interrupt on tx low level (will fire immediately) */
529 switch_bank(iobase
, SET0
);
530 outb(ICR_ETXTHI
, iobase
+ICR
);
534 /* Restore set register */
535 outb(set
, iobase
+SSR
);
541 * Function w83977af_dma_write (idev, iobase)
543 * Send frame using DMA
546 static void w83977af_dma_write(struct irda_device
*idev
, int iobase
)
549 #ifdef CONFIG_NETWINDER_TX_DMA_PROBLEMS
553 DEBUG(4, __FUNCTION__
"(), len=%d\n", idev
->tx_buff
.len
);
555 /* Save current set */
556 set
= inb(iobase
+SSR
);
559 switch_bank(iobase
, SET0
);
560 outb(inb(iobase
+HCR
) & ~HCR_EN_DMA
, iobase
+HCR
);
562 /* Choose transmit DMA channel */
563 switch_bank(iobase
, SET2
);
564 outb(ADCR1_D_CHSW
|/*ADCR1_DMA_F|*/ADCR1_ADV_SL
, iobase
+ADCR1
);
565 #ifdef CONFIG_NETWINDER_TX_DMA_PROBLEMS
569 disable_dma(idev
->io
.dma
);
570 clear_dma_ff(idev
->io
.dma
);
571 set_dma_mode(idev
->io
.dma
, DMA_MODE_READ
);
572 set_dma_addr(idev
->io
.dma
, virt_to_bus(idev
->tx_buff
.data
));
573 set_dma_count(idev
->io
.dma
, idev
->tx_buff
.len
);
575 setup_dma(idev
->io
.dma
, idev
->tx_buff
.data
, idev
->tx_buff
.len
,
578 idev
->io
.direction
= IO_XMIT
;
581 switch_bank(iobase
, SET0
);
582 #ifdef CONFIG_NETWINDER_TX_DMA_PROBLEMS
583 hcr
= inb(iobase
+HCR
);
584 outb(hcr
| HCR_EN_DMA
, iobase
+HCR
);
585 enable_dma(idev
->io
.dma
);
586 restore_flags(flags
);
588 outb(inb(iobase
+HCR
) | HCR_EN_DMA
| HCR_TX_WT
, iobase
+HCR
);
591 /* Restore set register */
592 outb(set
, iobase
+SSR
);
596 * Function w83977af_pio_write (iobase, buf, len, fifo_size)
601 static int w83977af_pio_write(int iobase
, __u8
*buf
, int len
, int fifo_size
)
606 DEBUG(4, __FUNCTION__
"()\n");
608 /* Save current bank */
609 set
= inb(iobase
+SSR
);
611 switch_bank(iobase
, SET0
);
612 if (!(inb_p(iobase
+USR
) & USR_TSRE
)) {
613 DEBUG(4, __FUNCTION__
"(), warning, FIFO not empty yet!\n");
616 DEBUG(4, __FUNCTION__
"%d bytes left in tx fifo\n", fifo_size
);
619 /* Fill FIFO with current frame */
620 while ((fifo_size
-- > 0) && (actual
< len
)) {
621 /* Transmit next byte */
622 outb(buf
[actual
++], iobase
+TBR
);
625 DEBUG(4, __FUNCTION__
"(), fifo_size %d ; %d sent of %d\n",
626 fifo_size
, actual
, len
);
629 outb(set
, iobase
+SSR
);
635 * Function w83977af_dma_xmit_complete (idev)
637 * The transfer of a frame in finished. So do the necessary things
641 void w83977af_dma_xmit_complete(struct irda_device
*idev
)
646 DEBUG(4, __FUNCTION__
"(%ld)\n", jiffies
);
648 ASSERT(idev
!= NULL
, return;);
649 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return;);
651 iobase
= idev
->io
.iobase
;
653 /* Save current set */
654 set
= inb(iobase
+SSR
);
657 switch_bank(iobase
, SET0
);
658 outb(inb(iobase
+HCR
) & ~HCR_EN_DMA
, iobase
+HCR
);
660 /* Check for underrrun! */
661 if (inb(iobase
+AUDR
) & AUDR_UNDR
) {
662 DEBUG(0, __FUNCTION__
"(), Transmit underrun!\n");
664 idev
->stats
.tx_errors
++;
665 idev
->stats
.tx_fifo_errors
++;
667 /* Clear bit, by writing 1 to it */
668 outb(AUDR_UNDR
, iobase
+AUDR
);
670 idev
->stats
.tx_packets
++;
672 /* Unlock tx_buff and request another frame */
673 idev
->netdev
.tbusy
= 0; /* Unlock */
674 idev
->media_busy
= FALSE
;
676 /* Tell the network layer, that we want more frames */
680 outb(set
, iobase
+SSR
);
684 * Function w83977af_dma_receive (idev)
686 * Get ready for receiving a frame. The device will initiate a DMA
687 * if it starts to receive a frame.
690 int w83977af_dma_receive(struct irda_device
*idev
)
692 struct w83977af_ir
*self
;
695 #ifdef CONFIG_NETWINDER_RX_DMA_PROBLEMS
700 ASSERT(idev
!= NULL
, return -1;);
701 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return -1;);
703 DEBUG(4, __FUNCTION__
"\n");
706 iobase
= idev
->io
.iobase
;
708 /* Save current set */
709 set
= inb(iobase
+SSR
);
712 switch_bank(iobase
, SET0
);
713 outb(inb(iobase
+HCR
) & ~HCR_EN_DMA
, iobase
+HCR
);
715 /* Choose DMA Rx, DMA Fairness, and Advanced mode */
716 switch_bank(iobase
, SET2
);
717 outb((inb(iobase
+ADCR1
) & ~ADCR1_D_CHSW
)/*|ADCR1_DMA_F*/|ADCR1_ADV_SL
,
720 idev
->io
.direction
= IO_RECV
;
721 idev
->rx_buff
.data
= idev
->rx_buff
.head
;
723 #ifdef CONFIG_NETWINDER_RX_DMA_PROBLEMS
727 disable_dma(idev
->io
.dma
);
728 clear_dma_ff(idev
->io
.dma
);
729 set_dma_mode(idev
->io
.dma
, DMA_MODE_READ
);
730 set_dma_addr(idev
->io
.dma
, virt_to_bus(idev
->rx_buff
.data
));
731 set_dma_count(idev
->io
.dma
, idev
->rx_buff
.truesize
);
733 setup_dma(idev
->io
.dma
, idev
->rx_buff
.data
, idev
->rx_buff
.truesize
,
737 * Reset Rx FIFO. This will also flush the ST_FIFO, it's very
738 * important that we don't reset the Tx FIFO since it might not
739 * be finished transmitting yet
741 switch_bank(iobase
, SET0
);
742 outb(UFR_RXTL
|UFR_TXTL
|UFR_RXF_RST
|UFR_EN_FIFO
, iobase
+UFR
);
743 self
->st_fifo
.len
= self
->st_fifo
.tail
= self
->st_fifo
.head
= 0;
746 switch_bank(iobase
, SET0
);
747 #ifdef CONFIG_NETWINDER_RX_DMA_PROBLEMS
748 hcr
= inb(iobase
+HCR
);
749 outb(hcr
| HCR_EN_DMA
, iobase
+HCR
);
750 enable_dma(idev
->io
.dma
);
751 restore_flags(flags
);
753 outb(inb(iobase
+HCR
) | HCR_EN_DMA
, iobase
+HCR
);
756 outb(set
, iobase
+SSR
);
762 * Function w83977af_receive_complete (idev)
764 * Finished with receiving a frame
767 int w83977af_dma_receive_complete(struct irda_device
*idev
)
770 struct w83977af_ir
*self
;
771 struct st_fifo
*st_fifo
;
777 DEBUG(4, __FUNCTION__
"\n");
780 st_fifo
= &self
->st_fifo
;
782 iobase
= idev
->io
.iobase
;
784 /* Save current set */
785 set
= inb(iobase
+SSR
);
787 iobase
= idev
->io
.iobase
;
789 /* Read status FIFO */
790 switch_bank(iobase
, SET5
);
791 while ((status
= inb(iobase
+FS_FO
)) & FS_FO_FSFDR
) {
792 st_fifo
->entries
[st_fifo
->tail
].status
= status
;
794 st_fifo
->entries
[st_fifo
->tail
].len
= inb(iobase
+RFLFL
);
795 st_fifo
->entries
[st_fifo
->tail
].len
|= inb(iobase
+RFLFH
) << 8;
801 while (st_fifo
->len
) {
802 /* Get first entry */
803 status
= st_fifo
->entries
[st_fifo
->head
].status
;
804 len
= st_fifo
->entries
[st_fifo
->head
].len
;
808 /* Check for errors */
809 if (status
& FS_FO_ERR_MSK
) {
810 if (status
& FS_FO_LST_FR
) {
811 /* Add number of lost frames to stats */
812 idev
->stats
.rx_errors
+= len
;
815 idev
->stats
.rx_errors
++;
817 idev
->rx_buff
.data
+= len
;
819 if (status
& FS_FO_MX_LEX
)
820 idev
->stats
.rx_length_errors
++;
822 if (status
& FS_FO_PHY_ERR
)
823 idev
->stats
.rx_frame_errors
++;
825 if (status
& FS_FO_CRC_ERR
)
826 idev
->stats
.rx_crc_errors
++;
828 /* The errors below can be reported in both cases */
829 if (status
& FS_FO_RX_OV
)
830 idev
->stats
.rx_fifo_errors
++;
832 if (status
& FS_FO_FSF_OV
)
833 idev
->stats
.rx_fifo_errors
++;
836 /* Check if we have transfered all data to memory */
837 switch_bank(iobase
, SET0
);
838 if (inb(iobase
+USR
) & USR_RDR
) {
839 #ifdef CONFIG_USE_INTERNAL_TIMER
840 /* Put this entry back in fifo */
843 st_fifo
->entries
[st_fifo
->head
].status
= status
;
844 st_fifo
->entries
[st_fifo
->head
].len
= len
;
846 /* Restore set register */
847 outb(set
, iobase
+SSR
);
849 return FALSE
; /* I'll be back! */
851 udelay(80); /* Should be enough!? */
855 skb
= dev_alloc_skb(len
+1);
857 printk(KERN_INFO __FUNCTION__
858 "(), memory squeeze, dropping frame.\n");
859 /* Restore set register */
860 outb(set
, iobase
+SSR
);
865 /* Align to 20 bytes */
868 /* Copy frame without CRC */
869 if (idev
->io
.baudrate
< 4000000) {
871 memcpy(skb
->data
, idev
->rx_buff
.data
, len
-2);
874 memcpy(skb
->data
, idev
->rx_buff
.data
, len
-4);
877 /* Move to next frame */
878 idev
->rx_buff
.data
+= len
;
879 idev
->stats
.rx_packets
++;
881 skb
->dev
= &idev
->netdev
;
882 skb
->mac
.raw
= skb
->data
;
883 skb
->protocol
= htons(ETH_P_IRDA
);
887 /* Restore set register */
888 outb(set
, iobase
+SSR
);
894 * Function pc87108_pio_receive (idev)
896 * Receive all data in receiver FIFO
899 static void w83977af_pio_receive(struct irda_device
*idev
)
904 DEBUG(4, __FUNCTION__
"()\n");
906 ASSERT(idev
!= NULL
, return;);
907 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return;);
909 iobase
= idev
->io
.iobase
;
911 /* Receive all characters in Rx FIFO */
913 byte
= inb(iobase
+RBR
);
914 async_unwrap_char(idev
, byte
);
915 } while (inb(iobase
+USR
) & USR_RDR
); /* Data available */
919 * Function w83977af_sir_interrupt (idev, eir)
921 * Handle SIR interrupt
924 static __u8
w83977af_sir_interrupt(struct irda_device
*idev
, int isr
)
931 DEBUG(4, __FUNCTION__
"(), isr=%#x\n", isr
);
933 iobase
= idev
->io
.iobase
;
934 /* Transmit FIFO low on data */
935 if (isr
& ISR_TXTH_I
) {
936 /* Write data left in transmit buffer */
937 actual
= w83977af_pio_write(idev
->io
.iobase
,
942 idev
->tx_buff
.data
+= actual
;
943 idev
->tx_buff
.len
-= actual
;
945 idev
->io
.direction
= IO_XMIT
;
947 /* Check if finished */
948 if (idev
->tx_buff
.len
> 0) {
949 new_icr
|= ICR_ETXTHI
;
951 set
= inb(iobase
+SSR
);
952 switch_bank(iobase
, SET0
);
953 outb(AUDR_SFEND
, iobase
+AUDR
);
954 outb(set
, iobase
+SSR
);
956 idev
->netdev
.tbusy
= 0; /* Unlock */
957 idev
->stats
.tx_packets
++;
959 /* Schedule network layer */
962 new_icr
|= ICR_ETBREI
;
965 /* Check if transmission has completed */
966 if (isr
& ISR_TXEMP_I
) {
968 /* Turn around and get ready to receive some data */
969 idev
->io
.direction
= IO_RECV
;
970 new_icr
|= ICR_ERBRI
;
973 /* Rx FIFO threshold or timeout */
974 if (isr
& ISR_RXTH_I
) {
975 w83977af_pio_receive(idev
);
978 new_icr
|= ICR_ERBRI
;
984 * Function pc87108_fir_interrupt (idev, eir)
986 * Handle MIR/FIR interrupt
989 static __u8
w83977af_fir_interrupt(struct irda_device
*idev
, int isr
)
995 iobase
= idev
->io
.iobase
;
996 set
= inb(iobase
+SSR
);
998 /* End of frame detected in FIFO */
999 if (isr
& (ISR_FEND_I
|ISR_FSF_I
)) {
1000 if (w83977af_dma_receive_complete(idev
)) {
1002 /* Wait for next status FIFO interrupt */
1003 new_icr
|= ICR_EFSFI
;
1005 /* DMA not finished yet */
1007 /* Set timer value, resolution 1 ms */
1008 switch_bank(iobase
, SET4
);
1009 outb(0x01, iobase
+TMRL
); /* 1 ms */
1010 outb(0x00, iobase
+TMRH
);
1013 outb(IR_MSL_EN_TMR
, iobase
+IR_MSL
);
1015 new_icr
|= ICR_ETMRI
;
1018 /* Timer finished */
1019 if (isr
& ISR_TMR_I
) {
1021 switch_bank(iobase
, SET4
);
1022 outb(0, iobase
+IR_MSL
);
1024 /* Clear timer event */
1025 /* switch_bank(iobase, SET0); */
1026 /* outb(ASCR_CTE, iobase+ASCR); */
1028 /* Check if this is a TX timer interrupt */
1029 if (idev
->io
.direction
== IO_XMIT
) {
1030 w83977af_dma_write(idev
, iobase
);
1032 new_icr
|= ICR_EDMAI
;
1034 /* Check if DMA has now finished */
1035 w83977af_dma_receive_complete(idev
);
1037 new_icr
|= ICR_EFSFI
;
1040 /* Finished with DMA */
1041 if (isr
& ISR_DMA_I
) {
1042 w83977af_dma_xmit_complete(idev
);
1044 /* Check if there are more frames to be transmitted */
1045 /* if (irda_device_txqueue_empty(idev)) { */
1047 /* Prepare for receive
1049 * ** Netwinder Tx DMA likes that we do this anyway **
1051 w83977af_dma_receive(idev
);
1052 new_icr
= ICR_EFSFI
;
1057 outb(set
, iobase
+SSR
);
1063 * Function pc87108_interrupt (irq, dev_id, regs)
1065 * An interrupt from the chip has arrived. Time to do some work
1068 static void w83977af_interrupt(int irq
, void *dev_id
, struct pt_regs
*regs
)
1073 struct irda_device
*idev
= (struct irda_device
*) dev_id
;
1076 printk(KERN_WARNING
"%s: irq %d for unknown device.\n",
1081 idev
->netdev
.interrupt
= 1;
1083 iobase
= idev
->io
.iobase
;
1085 /* Save current bank */
1086 set
= inb(iobase
+SSR
);
1087 switch_bank(iobase
, SET0
);
1089 icr
= inb(iobase
+ICR
);
1090 isr
= inb(iobase
+ISR
) & icr
; /* Mask out the interesting ones */
1092 outb(0, iobase
+ICR
); /* Disable interrupts */
1095 /* Dispatch interrupt handler for the current speed */
1096 if (idev
->io
.baudrate
> PIO_MAX_SPEED
)
1097 icr
= w83977af_fir_interrupt(idev
, isr
);
1099 icr
= w83977af_sir_interrupt(idev
, isr
);
1102 outb(icr
, iobase
+ICR
); /* Restore (new) interrupts */
1103 outb(set
, iobase
+SSR
); /* Restore bank register */
1105 idev
->netdev
.interrupt
= 0;
1109 * Function w83977af_wait_until_sent (idev)
1111 * This function should put the current thread to sleep until all data
1112 * have been sent, so it is safe to f.eks. change the speed.
1114 static void w83977af_wait_until_sent(struct irda_device
*idev
)
1116 current
->state
= TASK_INTERRUPTIBLE
;
1117 schedule_timeout(60*HZ
/1000);
1121 * Function w83977af_is_receiving (idev)
1123 * Return TRUE is we are currently receiving a frame
1126 static int w83977af_is_receiving(struct irda_device
*idev
)
1132 ASSERT(idev
!= NULL
, return FALSE
;);
1133 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return FALSE
;);
1135 if (idev
->io
.baudrate
> 115200) {
1136 iobase
= idev
->io
.iobase
;
1138 /* Check if rx FIFO is not empty */
1139 set
= inb(iobase
+SSR
);
1140 switch_bank(iobase
, SET2
);
1141 if ((inb(iobase
+RXFDTH
) & 0x3f) != 0) {
1142 /* We are receiving something */
1145 outb(set
, iobase
+SSR
);
1147 status
= (idev
->rx_buff
.state
!= OUTSIDE_FRAME
);
1153 * Function w83977af_net_init (dev)
1158 static int w83977af_net_init(struct net_device
*dev
)
1160 DEBUG(0, __FUNCTION__
"()\n");
1162 /* Set up to be a normal IrDA network device driver */
1163 irda_device_setup(dev
);
1165 /* Insert overrides below this line! */
1172 * Function w83977af_net_open (dev)
1177 static int w83977af_net_open(struct net_device
*dev
)
1179 struct irda_device
*idev
;
1183 DEBUG(0, __FUNCTION__
"()\n");
1185 ASSERT(dev
!= NULL
, return -1;);
1186 idev
= (struct irda_device
*) dev
->priv
;
1188 ASSERT(idev
!= NULL
, return 0;);
1189 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return 0;);
1191 iobase
= idev
->io
.iobase
;
1193 if (request_irq(idev
->io
.irq
, w83977af_interrupt
, 0, idev
->name
,
1198 * Always allocate the DMA channel after the IRQ,
1199 * and clean up on failure.
1201 if (request_dma(idev
->io
.dma
, idev
->name
)) {
1202 free_irq(idev
->io
.irq
, idev
);
1206 /* Save current set */
1207 set
= inb(iobase
+SSR
);
1209 /* Enable some interrupts so we can receive frames again */
1210 switch_bank(iobase
, SET0
);
1211 if (idev
->io
.baudrate
> 115200) {
1212 outb(ICR_EFSFI
, iobase
+ICR
);
1213 w83977af_dma_receive(idev
);
1215 outb(ICR_ERBRI
, iobase
+ICR
);
1217 /* Restore bank register */
1218 outb(set
, iobase
+SSR
);
1220 /* Ready to play! */
1221 irda_device_net_open(dev
);
1229 * Function w83977af_net_close (dev)
1234 static int w83977af_net_close(struct net_device
*dev
)
1236 struct irda_device
*idev
;
1240 DEBUG(0, __FUNCTION__
"()\n");
1242 ASSERT(dev
!= NULL
, return -1;);
1244 idev
= (struct irda_device
*) dev
->priv
;
1246 ASSERT(idev
!= NULL
, return 0;);
1247 ASSERT(idev
->magic
== IRDA_DEVICE_MAGIC
, return 0;);
1249 iobase
= idev
->io
.iobase
;
1252 irda_device_net_close(dev
);
1254 disable_dma(idev
->io
.dma
);
1256 /* Save current set */
1257 set
= inb(iobase
+SSR
);
1259 /* Disable interrupts */
1260 switch_bank(iobase
, SET0
);
1261 outb(0, iobase
+ICR
);
1263 free_irq(idev
->io
.irq
, idev
);
1264 free_dma(idev
->io
.dma
);
1266 /* Restore bank register */
1267 outb(set
, iobase
+SSR
);
1276 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
1277 MODULE_DESCRIPTION("Winbond W83977AF IrDA Device Driver");
1279 MODULE_PARM(qos_mtt_bits
, "i");
1282 * Function init_module (void)
1287 int init_module(void)
1289 return w83977af_init();
1293 * Function cleanup_module (void)
1298 void cleanup_module(void)