2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version
5 * 2 of the License, or (at your option) any later version.
7 * (c) Copyright 1998 Alan Cox <alan@lxorguk.ukuu.org.uk>
8 * (c) Copyright 2000, 2001 Red Hat Inc
10 * Development of this driver was funded by Equiinet Ltd
11 * http://www.equiinet.com
15 * Asynchronous mode dropped for 2.2. For 2.5 we will attempt the
16 * unification of all the Z85x30 asynchronous drivers for real.
18 * DMA now uses get_free_page as kmalloc buffers may span a 64K
21 * Modified for SMP safety and SMP locking by Alan Cox
22 * <alan@lxorguk.ukuu.org.uk>
27 * Non DMA you want a 486DX50 or better to do 64Kbits. 9600 baud
28 * X.25 is not unrealistic on all machines. DMA mode can in theory
29 * handle T1/E1 quite nicely. In practice the limit seems to be about
30 * 512Kbit->1Mbit depending on motherboard.
33 * 64K will take DMA, 9600 baud X.25 should be ok.
36 * Synchronous mode without DMA is unlikely to pass about 2400 baud.
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #include <linux/module.h>
42 #include <linux/kernel.h>
44 #include <linux/net.h>
45 #include <linux/skbuff.h>
46 #include <linux/netdevice.h>
47 #include <linux/if_arp.h>
48 #include <linux/delay.h>
49 #include <linux/hdlc.h>
50 #include <linux/ioport.h>
51 #include <linux/init.h>
52 #include <linux/gfp.h>
57 #include <linux/spinlock.h>
63 * z8530_read_port - Architecture specific interface function
66 * Provided port access methods. The Comtrol SV11 requires no delays
67 * between accesses and uses PC I/O. Some drivers may need a 5uS delay
69 * In the longer term this should become an architecture specific
70 * section so that this can become a generic driver interface for all
71 * platforms. For now we only handle PC I/O ports with or without the
72 * dread 5uS sanity delay.
74 * The caller must hold sufficient locks to avoid violating the horrible
78 static inline int z8530_read_port(unsigned long p
)
80 u8 r
=inb(Z8530_PORT_OF(p
));
81 if(p
&Z8530_PORT_SLEEP
) /* gcc should figure this out efficiently ! */
87 * z8530_write_port - Architecture specific interface function
91 * Write a value to a port with delays if need be. Note that the
92 * caller must hold locks to avoid read/writes from other contexts
93 * violating the 5uS rule
95 * In the longer term this should become an architecture specific
96 * section so that this can become a generic driver interface for all
97 * platforms. For now we only handle PC I/O ports with or without the
98 * dread 5uS sanity delay.
102 static inline void z8530_write_port(unsigned long p
, u8 d
)
104 outb(d
,Z8530_PORT_OF(p
));
105 if(p
&Z8530_PORT_SLEEP
)
111 static void z8530_rx_done(struct z8530_channel
*c
);
112 static void z8530_tx_done(struct z8530_channel
*c
);
116 * read_zsreg - Read a register from a Z85230
117 * @c: Z8530 channel to read from (2 per chip)
118 * @reg: Register to read
119 * FIXME: Use a spinlock.
121 * Most of the Z8530 registers are indexed off the control registers.
122 * A read is done by writing to the control register and reading the
123 * register back. The caller must hold the lock
126 static inline u8
read_zsreg(struct z8530_channel
*c
, u8 reg
)
129 z8530_write_port(c
->ctrlio
, reg
);
130 return z8530_read_port(c
->ctrlio
);
134 * read_zsdata - Read the data port of a Z8530 channel
135 * @c: The Z8530 channel to read the data port from
137 * The data port provides fast access to some things. We still
138 * have all the 5uS delays to worry about.
141 static inline u8
read_zsdata(struct z8530_channel
*c
)
144 r
=z8530_read_port(c
->dataio
);
149 * write_zsreg - Write to a Z8530 channel register
150 * @c: The Z8530 channel
151 * @reg: Register number
152 * @val: Value to write
154 * Write a value to an indexed register. The caller must hold the lock
155 * to honour the irritating delay rules. We know about register 0
156 * being fast to access.
158 * Assumes c->lock is held.
160 static inline void write_zsreg(struct z8530_channel
*c
, u8 reg
, u8 val
)
163 z8530_write_port(c
->ctrlio
, reg
);
164 z8530_write_port(c
->ctrlio
, val
);
169 * write_zsctrl - Write to a Z8530 control register
170 * @c: The Z8530 channel
171 * @val: Value to write
173 * Write directly to the control register on the Z8530
176 static inline void write_zsctrl(struct z8530_channel
*c
, u8 val
)
178 z8530_write_port(c
->ctrlio
, val
);
182 * write_zsdata - Write to a Z8530 control register
183 * @c: The Z8530 channel
184 * @val: Value to write
186 * Write directly to the data register on the Z8530
190 static inline void write_zsdata(struct z8530_channel
*c
, u8 val
)
192 z8530_write_port(c
->dataio
, val
);
196 * Register loading parameters for a dead port
199 u8 z8530_dead_port
[]=
204 EXPORT_SYMBOL(z8530_dead_port
);
207 * Register loading parameters for currently supported circuit types
212 * Data clocked by telco end. This is the correct data for the UK
213 * "kilostream" service, and most other similar services.
216 u8 z8530_hdlc_kilostream
[]=
218 4, SYNC_ENAB
|SDLC
|X1CLK
,
219 2, 0, /* No vector */
221 3, ENT_HM
|RxCRC_ENAB
|Rx8
,
222 5, TxCRC_ENAB
|RTS
|TxENAB
|Tx8
|DTR
,
223 9, 0, /* Disable interrupts */
226 10, ABUNDER
|NRZ
|CRCPS
,/*MARKIDLE ??*/
229 15, DCDIE
|SYNCIE
|CTSIE
|TxUIE
|BRKIE
,
230 1, EXT_INT_ENAB
|TxINT_ENAB
|INT_ALL_Rx
,
235 EXPORT_SYMBOL(z8530_hdlc_kilostream
);
238 * As above but for enhanced chips.
241 u8 z8530_hdlc_kilostream_85230
[]=
243 4, SYNC_ENAB
|SDLC
|X1CLK
,
244 2, 0, /* No vector */
246 3, ENT_HM
|RxCRC_ENAB
|Rx8
,
247 5, TxCRC_ENAB
|RTS
|TxENAB
|Tx8
|DTR
,
248 9, 0, /* Disable interrupts */
251 10, ABUNDER
|NRZ
|CRCPS
, /* MARKIDLE?? */
254 15, DCDIE
|SYNCIE
|CTSIE
|TxUIE
|BRKIE
,
255 1, EXT_INT_ENAB
|TxINT_ENAB
|INT_ALL_Rx
,
257 23, 3, /* Extended mode AUTO TX and EOM*/
262 EXPORT_SYMBOL(z8530_hdlc_kilostream_85230
);
265 * z8530_flush_fifo - Flush on chip RX FIFO
266 * @c: Channel to flush
268 * Flush the receive FIFO. There is no specific option for this, we
269 * blindly read bytes and discard them. Reading when there is no data
270 * is harmless. The 8530 has a 4 byte FIFO, the 85230 has 8 bytes.
272 * All locking is handled for the caller. On return data may still be
273 * present if it arrived during the flush.
276 static void z8530_flush_fifo(struct z8530_channel
*c
)
282 if(c
->dev
->type
==Z85230
)
292 * z8530_rtsdtr - Control the outgoing DTS/RTS line
293 * @c: The Z8530 channel to control;
294 * @set: 1 to set, 0 to clear
296 * Sets or clears DTR/RTS on the requested line. All locking is handled
297 * by the caller. For now we assume all boards use the actual RTS/DTR
298 * on the chip. Apparently one or two don't. We'll scream about them
302 static void z8530_rtsdtr(struct z8530_channel
*c
, int set
)
305 c
->regs
[5] |= (RTS
| DTR
);
307 c
->regs
[5] &= ~(RTS
| DTR
);
308 write_zsreg(c
, R5
, c
->regs
[5]);
312 * z8530_rx - Handle a PIO receive event
313 * @c: Z8530 channel to process
315 * Receive handler for receiving in PIO mode. This is much like the
316 * async one but not quite the same or as complex
318 * Note: Its intended that this handler can easily be separated from
319 * the main code to run realtime. That'll be needed for some machines
320 * (eg to ever clock 64kbits on a sparc ;)).
322 * The RT_LOCK macros don't do anything now. Keep the code covered
323 * by them as short as possible in all circumstances - clocks cost
324 * baud. The interrupt handler is assumed to be atomic w.r.t. to
325 * other code - this is true in the RT case too.
327 * We only cover the sync cases for this. If you want 2Mbit async
328 * do it yourself but consider medical assistance first. This non DMA
329 * synchronous mode is portable code. The DMA mode assumes PCI like
332 * Called with the device lock held
335 static void z8530_rx(struct z8530_channel
*c
)
342 if(!(read_zsreg(c
, R0
)&1))
345 stat
=read_zsreg(c
, R1
);
350 if(c
->count
< c
->max
)
362 if(stat
&(Rx_OVR
|CRC_ERR
))
364 /* Rewind the buffer and return */
366 c
->dptr
=c
->skb
->data
;
370 pr_warn("%s: overrun\n", c
->dev
->name
);
376 /* printk("crc error\n"); */
378 /* Shove the frame upstream */
383 * Drop the lock for RX processing, or
384 * there are deadlocks
387 write_zsctrl(c
, RES_Rx_CRC
);
394 write_zsctrl(c
, ERR_RES
);
395 write_zsctrl(c
, RES_H_IUS
);
400 * z8530_tx - Handle a PIO transmit event
401 * @c: Z8530 channel to process
403 * Z8530 transmit interrupt handler for the PIO mode. The basic
404 * idea is to attempt to keep the FIFO fed. We fill as many bytes
405 * in as possible, its quite possible that we won't keep up with the
406 * data rate otherwise.
409 static void z8530_tx(struct z8530_channel
*c
)
413 if(!(read_zsreg(c
, R0
)&4))
417 * Shovel out the byte
419 write_zsreg(c
, R8
, *c
->tx_ptr
++);
420 write_zsctrl(c
, RES_H_IUS
);
421 /* We are about to underflow */
424 write_zsctrl(c
, RES_EOM_L
);
425 write_zsreg(c
, R10
, c
->regs
[10]&~ABUNDER
);
431 * End of frame TX - fire another one
434 write_zsctrl(c
, RES_Tx_P
);
437 write_zsctrl(c
, RES_H_IUS
);
441 * z8530_status - Handle a PIO status exception
442 * @chan: Z8530 channel to process
444 * A status event occurred in PIO synchronous mode. There are several
445 * reasons the chip will bother us here. A transmit underrun means we
446 * failed to feed the chip fast enough and just broke a packet. A DCD
447 * change is a line up or down.
450 static void z8530_status(struct z8530_channel
*chan
)
454 status
= read_zsreg(chan
, R0
);
455 altered
= chan
->status
^ status
;
457 chan
->status
= status
;
459 if (status
& TxEOM
) {
460 /* printk("%s: Tx underrun.\n", chan->dev->name); */
461 chan
->netdevice
->stats
.tx_fifo_errors
++;
462 write_zsctrl(chan
, ERR_RES
);
466 if (altered
& chan
->dcdcheck
)
468 if (status
& chan
->dcdcheck
) {
469 pr_info("%s: DCD raised\n", chan
->dev
->name
);
470 write_zsreg(chan
, R3
, chan
->regs
[3] | RxENABLE
);
472 netif_carrier_on(chan
->netdevice
);
474 pr_info("%s: DCD lost\n", chan
->dev
->name
);
475 write_zsreg(chan
, R3
, chan
->regs
[3] & ~RxENABLE
);
476 z8530_flush_fifo(chan
);
478 netif_carrier_off(chan
->netdevice
);
482 write_zsctrl(chan
, RES_EXT_INT
);
483 write_zsctrl(chan
, RES_H_IUS
);
486 struct z8530_irqhandler z8530_sync
=
493 EXPORT_SYMBOL(z8530_sync
);
496 * z8530_dma_rx - Handle a DMA RX event
497 * @chan: Channel to handle
499 * Non bus mastering DMA interfaces for the Z8x30 devices. This
500 * is really pretty PC specific. The DMA mode means that most receive
501 * events are handled by the DMA hardware. We get a kick here only if
505 static void z8530_dma_rx(struct z8530_channel
*chan
)
509 /* Special condition check only */
512 read_zsreg(chan
, R7
);
513 read_zsreg(chan
, R6
);
515 status
=read_zsreg(chan
, R1
);
519 z8530_rx_done(chan
); /* Fire up the next one */
521 write_zsctrl(chan
, ERR_RES
);
522 write_zsctrl(chan
, RES_H_IUS
);
526 /* DMA is off right now, drain the slow way */
532 * z8530_dma_tx - Handle a DMA TX event
533 * @chan: The Z8530 channel to handle
535 * We have received an interrupt while doing DMA transmissions. It
536 * shouldn't happen. Scream loudly if it does.
539 static void z8530_dma_tx(struct z8530_channel
*chan
)
543 pr_warn("Hey who turned the DMA off?\n");
547 /* This shouldn't occur in DMA mode */
548 pr_err("DMA tx - bogus event!\n");
553 * z8530_dma_status - Handle a DMA status exception
554 * @chan: Z8530 channel to process
556 * A status event occurred on the Z8530. We receive these for two reasons
557 * when in DMA mode. Firstly if we finished a packet transfer we get one
558 * and kick the next packet out. Secondly we may see a DCD change.
562 static void z8530_dma_status(struct z8530_channel
*chan
)
566 status
=read_zsreg(chan
, R0
);
567 altered
=chan
->status
^status
;
578 flags
=claim_dma_lock();
579 disable_dma(chan
->txdma
);
580 clear_dma_ff(chan
->txdma
);
582 release_dma_lock(flags
);
587 if (altered
& chan
->dcdcheck
)
589 if (status
& chan
->dcdcheck
) {
590 pr_info("%s: DCD raised\n", chan
->dev
->name
);
591 write_zsreg(chan
, R3
, chan
->regs
[3] | RxENABLE
);
593 netif_carrier_on(chan
->netdevice
);
595 pr_info("%s: DCD lost\n", chan
->dev
->name
);
596 write_zsreg(chan
, R3
, chan
->regs
[3] & ~RxENABLE
);
597 z8530_flush_fifo(chan
);
599 netif_carrier_off(chan
->netdevice
);
603 write_zsctrl(chan
, RES_EXT_INT
);
604 write_zsctrl(chan
, RES_H_IUS
);
607 static struct z8530_irqhandler z8530_dma_sync
= {
613 static struct z8530_irqhandler z8530_txdma_sync
= {
620 * z8530_rx_clear - Handle RX events from a stopped chip
621 * @c: Z8530 channel to shut up
623 * Receive interrupt vectors for a Z8530 that is in 'parked' mode.
624 * For machines with PCI Z85x30 cards, or level triggered interrupts
625 * (eg the MacII) we must clear the interrupt cause or die.
629 static void z8530_rx_clear(struct z8530_channel
*c
)
632 * Data and status bytes
637 stat
=read_zsreg(c
, R1
);
640 write_zsctrl(c
, RES_Rx_CRC
);
644 write_zsctrl(c
, ERR_RES
);
645 write_zsctrl(c
, RES_H_IUS
);
649 * z8530_tx_clear - Handle TX events from a stopped chip
650 * @c: Z8530 channel to shut up
652 * Transmit interrupt vectors for a Z8530 that is in 'parked' mode.
653 * For machines with PCI Z85x30 cards, or level triggered interrupts
654 * (eg the MacII) we must clear the interrupt cause or die.
657 static void z8530_tx_clear(struct z8530_channel
*c
)
659 write_zsctrl(c
, RES_Tx_P
);
660 write_zsctrl(c
, RES_H_IUS
);
664 * z8530_status_clear - Handle status events from a stopped chip
665 * @chan: Z8530 channel to shut up
667 * Status interrupt vectors for a Z8530 that is in 'parked' mode.
668 * For machines with PCI Z85x30 cards, or level triggered interrupts
669 * (eg the MacII) we must clear the interrupt cause or die.
672 static void z8530_status_clear(struct z8530_channel
*chan
)
674 u8 status
=read_zsreg(chan
, R0
);
676 write_zsctrl(chan
, ERR_RES
);
677 write_zsctrl(chan
, RES_EXT_INT
);
678 write_zsctrl(chan
, RES_H_IUS
);
681 struct z8530_irqhandler z8530_nop
=
689 EXPORT_SYMBOL(z8530_nop
);
692 * z8530_interrupt - Handle an interrupt from a Z8530
693 * @irq: Interrupt number
694 * @dev_id: The Z8530 device that is interrupting.
696 * A Z85[2]30 device has stuck its hand in the air for attention.
697 * We scan both the channels on the chip for events and then call
698 * the channel specific call backs for each channel that has events.
699 * We have to use callback functions because the two channels can be
700 * in different modes.
702 * Locking is done for the handlers. Note that locking is done
703 * at the chip level (the 5uS delay issue is per chip not per
704 * channel). c->lock for both channels points to dev->lock
707 irqreturn_t
z8530_interrupt(int irq
, void *dev_id
)
709 struct z8530_dev
*dev
=dev_id
;
710 u8
uninitialized_var(intr
);
711 static volatile int locker
=0;
713 struct z8530_irqhandler
*irqs
;
717 pr_err("IRQ re-enter\n");
722 spin_lock(&dev
->lock
);
727 intr
= read_zsreg(&dev
->chanA
, R3
);
728 if(!(intr
& (CHARxIP
|CHATxIP
|CHAEXT
|CHBRxIP
|CHBTxIP
|CHBEXT
)))
731 /* This holds the IRQ status. On the 8530 you must read it from chan
732 A even though it applies to the whole chip */
734 /* Now walk the chip and see what it is wanting - it may be
735 an IRQ for someone else remember */
737 irqs
=dev
->chanA
.irqs
;
739 if(intr
& (CHARxIP
|CHATxIP
|CHAEXT
))
742 irqs
->rx(&dev
->chanA
);
744 irqs
->tx(&dev
->chanA
);
746 irqs
->status(&dev
->chanA
);
749 irqs
=dev
->chanB
.irqs
;
751 if(intr
& (CHBRxIP
|CHBTxIP
|CHBEXT
))
754 irqs
->rx(&dev
->chanB
);
756 irqs
->tx(&dev
->chanB
);
758 irqs
->status(&dev
->chanB
);
761 spin_unlock(&dev
->lock
);
763 pr_err("%s: interrupt jammed - abort(0x%X)!\n",
770 EXPORT_SYMBOL(z8530_interrupt
);
772 static const u8 reg_init
[16]=
782 * z8530_sync_open - Open a Z8530 channel for PIO
783 * @dev: The network interface we are using
784 * @c: The Z8530 channel to open in synchronous PIO mode
786 * Switch a Z8530 into synchronous mode without DMA assist. We
787 * raise the RTS/DTR and commence network operation.
790 int z8530_sync_open(struct net_device
*dev
, struct z8530_channel
*c
)
794 spin_lock_irqsave(c
->lock
, flags
);
797 c
->mtu
= dev
->mtu
+64;
801 c
->irqs
= &z8530_sync
;
803 /* This loads the double buffer up */
804 z8530_rx_done(c
); /* Load the frame ring */
805 z8530_rx_done(c
); /* Load the backup frame */
808 c
->regs
[R1
]|=TxINT_ENAB
;
809 write_zsreg(c
, R1
, c
->regs
[R1
]);
810 write_zsreg(c
, R3
, c
->regs
[R3
]|RxENABLE
);
812 spin_unlock_irqrestore(c
->lock
, flags
);
817 EXPORT_SYMBOL(z8530_sync_open
);
820 * z8530_sync_close - Close a PIO Z8530 channel
821 * @dev: Network device to close
822 * @c: Z8530 channel to disassociate and move to idle
824 * Close down a Z8530 interface and switch its interrupt handlers
825 * to discard future events.
828 int z8530_sync_close(struct net_device
*dev
, struct z8530_channel
*c
)
833 spin_lock_irqsave(c
->lock
, flags
);
834 c
->irqs
= &z8530_nop
;
838 chk
=read_zsreg(c
,R0
);
839 write_zsreg(c
, R3
, c
->regs
[R3
]);
842 spin_unlock_irqrestore(c
->lock
, flags
);
846 EXPORT_SYMBOL(z8530_sync_close
);
849 * z8530_sync_dma_open - Open a Z8530 for DMA I/O
850 * @dev: The network device to attach
851 * @c: The Z8530 channel to configure in sync DMA mode.
853 * Set up a Z85x30 device for synchronous DMA in both directions. Two
854 * ISA DMA channels must be available for this to work. We assume ISA
855 * DMA driven I/O and PC limits on access.
858 int z8530_sync_dma_open(struct net_device
*dev
, struct z8530_channel
*c
)
860 unsigned long cflags
, dflags
;
863 c
->mtu
= dev
->mtu
+64;
868 * Load the DMA interfaces up
874 * Allocate the DMA flip buffers. Limit by page size.
875 * Everyone runs 1500 mtu or less on wan links so this
879 if(c
->mtu
> PAGE_SIZE
/2)
882 c
->rx_buf
[0]=(void *)get_zeroed_page(GFP_KERNEL
|GFP_DMA
);
883 if(c
->rx_buf
[0]==NULL
)
885 c
->rx_buf
[1]=c
->rx_buf
[0]+PAGE_SIZE
/2;
887 c
->tx_dma_buf
[0]=(void *)get_zeroed_page(GFP_KERNEL
|GFP_DMA
);
888 if(c
->tx_dma_buf
[0]==NULL
)
890 free_page((unsigned long)c
->rx_buf
[0]);
894 c
->tx_dma_buf
[1]=c
->tx_dma_buf
[0]+PAGE_SIZE
/2;
902 * Enable DMA control mode
905 spin_lock_irqsave(c
->lock
, cflags
);
911 c
->regs
[R14
]|= DTRREQ
;
912 write_zsreg(c
, R14
, c
->regs
[R14
]);
914 c
->regs
[R1
]&= ~TxINT_ENAB
;
915 write_zsreg(c
, R1
, c
->regs
[R1
]);
921 c
->regs
[R1
]|= WT_FN_RDYFN
;
922 c
->regs
[R1
]|= WT_RDY_RT
;
923 c
->regs
[R1
]|= INT_ERR_Rx
;
924 c
->regs
[R1
]&= ~TxINT_ENAB
;
925 write_zsreg(c
, R1
, c
->regs
[R1
]);
926 c
->regs
[R1
]|= WT_RDY_ENAB
;
927 write_zsreg(c
, R1
, c
->regs
[R1
]);
934 * Set up the DMA configuration
937 dflags
=claim_dma_lock();
939 disable_dma(c
->rxdma
);
940 clear_dma_ff(c
->rxdma
);
941 set_dma_mode(c
->rxdma
, DMA_MODE_READ
|0x10);
942 set_dma_addr(c
->rxdma
, virt_to_bus(c
->rx_buf
[0]));
943 set_dma_count(c
->rxdma
, c
->mtu
);
944 enable_dma(c
->rxdma
);
946 disable_dma(c
->txdma
);
947 clear_dma_ff(c
->txdma
);
948 set_dma_mode(c
->txdma
, DMA_MODE_WRITE
);
949 disable_dma(c
->txdma
);
951 release_dma_lock(dflags
);
954 * Select the DMA interrupt handlers
961 c
->irqs
= &z8530_dma_sync
;
963 write_zsreg(c
, R3
, c
->regs
[R3
]|RxENABLE
);
965 spin_unlock_irqrestore(c
->lock
, cflags
);
970 EXPORT_SYMBOL(z8530_sync_dma_open
);
973 * z8530_sync_dma_close - Close down DMA I/O
974 * @dev: Network device to detach
975 * @c: Z8530 channel to move into discard mode
977 * Shut down a DMA mode synchronous interface. Halt the DMA, and
981 int z8530_sync_dma_close(struct net_device
*dev
, struct z8530_channel
*c
)
986 c
->irqs
= &z8530_nop
;
991 * Disable the PC DMA channels
994 flags
=claim_dma_lock();
995 disable_dma(c
->rxdma
);
996 clear_dma_ff(c
->rxdma
);
1000 disable_dma(c
->txdma
);
1001 clear_dma_ff(c
->txdma
);
1002 release_dma_lock(flags
);
1007 spin_lock_irqsave(c
->lock
, flags
);
1010 * Disable DMA control mode
1013 c
->regs
[R1
]&= ~WT_RDY_ENAB
;
1014 write_zsreg(c
, R1
, c
->regs
[R1
]);
1015 c
->regs
[R1
]&= ~(WT_RDY_RT
|WT_FN_RDYFN
|INT_ERR_Rx
);
1016 c
->regs
[R1
]|= INT_ALL_Rx
;
1017 write_zsreg(c
, R1
, c
->regs
[R1
]);
1018 c
->regs
[R14
]&= ~DTRREQ
;
1019 write_zsreg(c
, R14
, c
->regs
[R14
]);
1023 free_page((unsigned long)c
->rx_buf
[0]);
1026 if(c
->tx_dma_buf
[0])
1028 free_page((unsigned long)c
->tx_dma_buf
[0]);
1029 c
->tx_dma_buf
[0]=NULL
;
1031 chk
=read_zsreg(c
,R0
);
1032 write_zsreg(c
, R3
, c
->regs
[R3
]);
1035 spin_unlock_irqrestore(c
->lock
, flags
);
1040 EXPORT_SYMBOL(z8530_sync_dma_close
);
1043 * z8530_sync_txdma_open - Open a Z8530 for TX driven DMA
1044 * @dev: The network device to attach
1045 * @c: The Z8530 channel to configure in sync DMA mode.
1047 * Set up a Z85x30 device for synchronous DMA tranmission. One
1048 * ISA DMA channel must be available for this to work. The receive
1049 * side is run in PIO mode, but then it has the bigger FIFO.
1052 int z8530_sync_txdma_open(struct net_device
*dev
, struct z8530_channel
*c
)
1054 unsigned long cflags
, dflags
;
1056 printk("Opening sync interface for TX-DMA\n");
1058 c
->mtu
= dev
->mtu
+64;
1064 * Allocate the DMA flip buffers. Limit by page size.
1065 * Everyone runs 1500 mtu or less on wan links so this
1069 if(c
->mtu
> PAGE_SIZE
/2)
1072 c
->tx_dma_buf
[0]=(void *)get_zeroed_page(GFP_KERNEL
|GFP_DMA
);
1073 if(c
->tx_dma_buf
[0]==NULL
)
1076 c
->tx_dma_buf
[1] = c
->tx_dma_buf
[0] + PAGE_SIZE
/2;
1079 spin_lock_irqsave(c
->lock
, cflags
);
1082 * Load the PIO receive ring
1089 * Load the DMA interfaces up
1101 * Enable DMA control mode
1105 * TX DMA via DIR/REQ
1107 c
->regs
[R14
]|= DTRREQ
;
1108 write_zsreg(c
, R14
, c
->regs
[R14
]);
1110 c
->regs
[R1
]&= ~TxINT_ENAB
;
1111 write_zsreg(c
, R1
, c
->regs
[R1
]);
1114 * Set up the DMA configuration
1117 dflags
= claim_dma_lock();
1119 disable_dma(c
->txdma
);
1120 clear_dma_ff(c
->txdma
);
1121 set_dma_mode(c
->txdma
, DMA_MODE_WRITE
);
1122 disable_dma(c
->txdma
);
1124 release_dma_lock(dflags
);
1127 * Select the DMA interrupt handlers
1134 c
->irqs
= &z8530_txdma_sync
;
1136 write_zsreg(c
, R3
, c
->regs
[R3
]|RxENABLE
);
1137 spin_unlock_irqrestore(c
->lock
, cflags
);
1142 EXPORT_SYMBOL(z8530_sync_txdma_open
);
1145 * z8530_sync_txdma_close - Close down a TX driven DMA channel
1146 * @dev: Network device to detach
1147 * @c: Z8530 channel to move into discard mode
1149 * Shut down a DMA/PIO split mode synchronous interface. Halt the DMA,
1150 * and free the buffers.
1153 int z8530_sync_txdma_close(struct net_device
*dev
, struct z8530_channel
*c
)
1155 unsigned long dflags
, cflags
;
1159 spin_lock_irqsave(c
->lock
, cflags
);
1161 c
->irqs
= &z8530_nop
;
1166 * Disable the PC DMA channels
1169 dflags
= claim_dma_lock();
1171 disable_dma(c
->txdma
);
1172 clear_dma_ff(c
->txdma
);
1176 release_dma_lock(dflags
);
1179 * Disable DMA control mode
1182 c
->regs
[R1
]&= ~WT_RDY_ENAB
;
1183 write_zsreg(c
, R1
, c
->regs
[R1
]);
1184 c
->regs
[R1
]&= ~(WT_RDY_RT
|WT_FN_RDYFN
|INT_ERR_Rx
);
1185 c
->regs
[R1
]|= INT_ALL_Rx
;
1186 write_zsreg(c
, R1
, c
->regs
[R1
]);
1187 c
->regs
[R14
]&= ~DTRREQ
;
1188 write_zsreg(c
, R14
, c
->regs
[R14
]);
1190 if(c
->tx_dma_buf
[0])
1192 free_page((unsigned long)c
->tx_dma_buf
[0]);
1193 c
->tx_dma_buf
[0]=NULL
;
1195 chk
=read_zsreg(c
,R0
);
1196 write_zsreg(c
, R3
, c
->regs
[R3
]);
1199 spin_unlock_irqrestore(c
->lock
, cflags
);
1204 EXPORT_SYMBOL(z8530_sync_txdma_close
);
1208 * Name strings for Z8530 chips. SGI claim to have a 130, Zilog deny
1212 static const char *z8530_type_name
[]={
1219 * z8530_describe - Uniformly describe a Z8530 port
1220 * @dev: Z8530 device to describe
1221 * @mapping: string holding mapping type (eg "I/O" or "Mem")
1222 * @io: the port value in question
1224 * Describe a Z8530 in a standard format. We must pass the I/O as
1225 * the port offset isn't predictable. The main reason for this function
1226 * is to try and get a common format of report.
1229 void z8530_describe(struct z8530_dev
*dev
, char *mapping
, unsigned long io
)
1231 pr_info("%s: %s found at %s 0x%lX, IRQ %d\n",
1233 z8530_type_name
[dev
->type
],
1239 EXPORT_SYMBOL(z8530_describe
);
1242 * Locked operation part of the z8530 init code
1245 static inline int do_z8530_init(struct z8530_dev
*dev
)
1247 /* NOP the interrupt handlers first - we might get a
1248 floating IRQ transition when we reset the chip */
1249 dev
->chanA
.irqs
=&z8530_nop
;
1250 dev
->chanB
.irqs
=&z8530_nop
;
1251 dev
->chanA
.dcdcheck
=DCD
;
1252 dev
->chanB
.dcdcheck
=DCD
;
1254 /* Reset the chip */
1255 write_zsreg(&dev
->chanA
, R9
, 0xC0);
1257 /* Now check its valid */
1258 write_zsreg(&dev
->chanA
, R12
, 0xAA);
1259 if(read_zsreg(&dev
->chanA
, R12
)!=0xAA)
1261 write_zsreg(&dev
->chanA
, R12
, 0x55);
1262 if(read_zsreg(&dev
->chanA
, R12
)!=0x55)
1268 * See the application note.
1271 write_zsreg(&dev
->chanA
, R15
, 0x01);
1274 * If we can set the low bit of R15 then
1275 * the chip is enhanced.
1278 if(read_zsreg(&dev
->chanA
, R15
)==0x01)
1280 /* This C30 versus 230 detect is from Klaus Kudielka's dmascc */
1281 /* Put a char in the fifo */
1282 write_zsreg(&dev
->chanA
, R8
, 0);
1283 if(read_zsreg(&dev
->chanA
, R0
)&Tx_BUF_EMP
)
1284 dev
->type
= Z85230
; /* Has a FIFO */
1286 dev
->type
= Z85C30
; /* Z85C30, 1 byte FIFO */
1290 * The code assumes R7' and friends are
1291 * off. Use write_zsext() for these and keep
1295 write_zsreg(&dev
->chanA
, R15
, 0);
1298 * At this point it looks like the chip is behaving
1301 memcpy(dev
->chanA
.regs
, reg_init
, 16);
1302 memcpy(dev
->chanB
.regs
, reg_init
,16);
1308 * z8530_init - Initialise a Z8530 device
1309 * @dev: Z8530 device to initialise.
1311 * Configure up a Z8530/Z85C30 or Z85230 chip. We check the device
1312 * is present, identify the type and then program it to hopefully
1313 * keep quite and behave. This matters a lot, a Z8530 in the wrong
1314 * state will sometimes get into stupid modes generating 10Khz
1315 * interrupt streams and the like.
1317 * We set the interrupt handler up to discard any events, in case
1318 * we get them during reset or setp.
1320 * Return 0 for success, or a negative value indicating the problem
1324 int z8530_init(struct z8530_dev
*dev
)
1326 unsigned long flags
;
1329 /* Set up the chip level lock */
1330 spin_lock_init(&dev
->lock
);
1331 dev
->chanA
.lock
= &dev
->lock
;
1332 dev
->chanB
.lock
= &dev
->lock
;
1334 spin_lock_irqsave(&dev
->lock
, flags
);
1335 ret
= do_z8530_init(dev
);
1336 spin_unlock_irqrestore(&dev
->lock
, flags
);
1342 EXPORT_SYMBOL(z8530_init
);
1345 * z8530_shutdown - Shutdown a Z8530 device
1346 * @dev: The Z8530 chip to shutdown
1348 * We set the interrupt handlers to silence any interrupts. We then
1349 * reset the chip and wait 100uS to be sure the reset completed. Just
1350 * in case the caller then tries to do stuff.
1352 * This is called without the lock held
1355 int z8530_shutdown(struct z8530_dev
*dev
)
1357 unsigned long flags
;
1358 /* Reset the chip */
1360 spin_lock_irqsave(&dev
->lock
, flags
);
1361 dev
->chanA
.irqs
=&z8530_nop
;
1362 dev
->chanB
.irqs
=&z8530_nop
;
1363 write_zsreg(&dev
->chanA
, R9
, 0xC0);
1364 /* We must lock the udelay, the chip is offlimits here */
1366 spin_unlock_irqrestore(&dev
->lock
, flags
);
1370 EXPORT_SYMBOL(z8530_shutdown
);
1373 * z8530_channel_load - Load channel data
1374 * @c: Z8530 channel to configure
1375 * @rtable: table of register, value pairs
1376 * FIXME: ioctl to allow user uploaded tables
1378 * Load a Z8530 channel up from the system data. We use +16 to
1379 * indicate the "prime" registers. The value 255 terminates the
1383 int z8530_channel_load(struct z8530_channel
*c
, u8
*rtable
)
1385 unsigned long flags
;
1387 spin_lock_irqsave(c
->lock
, flags
);
1393 write_zsreg(c
, R15
, c
->regs
[15]|1);
1394 write_zsreg(c
, reg
&0x0F, *rtable
);
1396 write_zsreg(c
, R15
, c
->regs
[15]&~1);
1397 c
->regs
[reg
]=*rtable
++;
1399 c
->rx_function
=z8530_null_rx
;
1402 c
->tx_next_skb
=NULL
;
1406 c
->status
=read_zsreg(c
, R0
);
1408 write_zsreg(c
, R3
, c
->regs
[R3
]|RxENABLE
);
1410 spin_unlock_irqrestore(c
->lock
, flags
);
1414 EXPORT_SYMBOL(z8530_channel_load
);
1418 * z8530_tx_begin - Begin packet transmission
1419 * @c: The Z8530 channel to kick
1421 * This is the speed sensitive side of transmission. If we are called
1422 * and no buffer is being transmitted we commence the next buffer. If
1423 * nothing is queued we idle the sync.
1425 * Note: We are handling this code path in the interrupt path, keep it
1426 * fast or bad things will happen.
1428 * Called with the lock held.
1431 static void z8530_tx_begin(struct z8530_channel
*c
)
1433 unsigned long flags
;
1437 c
->tx_skb
=c
->tx_next_skb
;
1438 c
->tx_next_skb
=NULL
;
1439 c
->tx_ptr
=c
->tx_next_ptr
;
1446 flags
=claim_dma_lock();
1447 disable_dma(c
->txdma
);
1449 * Check if we crapped out.
1451 if (get_dma_residue(c
->txdma
))
1453 c
->netdevice
->stats
.tx_dropped
++;
1454 c
->netdevice
->stats
.tx_fifo_errors
++;
1456 release_dma_lock(flags
);
1462 c
->txcount
=c
->tx_skb
->len
;
1468 * FIXME. DMA is broken for the original 8530,
1469 * on the older parts we need to set a flag and
1470 * wait for a further TX interrupt to fire this
1474 flags
=claim_dma_lock();
1475 disable_dma(c
->txdma
);
1478 * These two are needed by the 8530/85C30
1479 * and must be issued when idling.
1482 if(c
->dev
->type
!=Z85230
)
1484 write_zsctrl(c
, RES_Tx_CRC
);
1485 write_zsctrl(c
, RES_EOM_L
);
1487 write_zsreg(c
, R10
, c
->regs
[10]&~ABUNDER
);
1488 clear_dma_ff(c
->txdma
);
1489 set_dma_addr(c
->txdma
, virt_to_bus(c
->tx_ptr
));
1490 set_dma_count(c
->txdma
, c
->txcount
);
1491 enable_dma(c
->txdma
);
1492 release_dma_lock(flags
);
1493 write_zsctrl(c
, RES_EOM_L
);
1494 write_zsreg(c
, R5
, c
->regs
[R5
]|TxENAB
);
1500 write_zsreg(c
, R10
, c
->regs
[10]);
1501 write_zsctrl(c
, RES_Tx_CRC
);
1503 while(c
->txcount
&& (read_zsreg(c
,R0
)&Tx_BUF_EMP
))
1505 write_zsreg(c
, R8
, *c
->tx_ptr
++);
1512 * Since we emptied tx_skb we can ask for more
1514 netif_wake_queue(c
->netdevice
);
1518 * z8530_tx_done - TX complete callback
1519 * @c: The channel that completed a transmit.
1521 * This is called when we complete a packet send. We wake the queue,
1522 * start the next packet going and then free the buffer of the existing
1523 * packet. This code is fairly timing sensitive.
1525 * Called with the register lock held.
1528 static void z8530_tx_done(struct z8530_channel
*c
)
1530 struct sk_buff
*skb
;
1532 /* Actually this can happen.*/
1533 if (c
->tx_skb
== NULL
)
1539 c
->netdevice
->stats
.tx_packets
++;
1540 c
->netdevice
->stats
.tx_bytes
+= skb
->len
;
1541 dev_kfree_skb_irq(skb
);
1545 * z8530_null_rx - Discard a packet
1546 * @c: The channel the packet arrived on
1549 * We point the receive handler at this function when idle. Instead
1550 * of processing the frames we get to throw them away.
1553 void z8530_null_rx(struct z8530_channel
*c
, struct sk_buff
*skb
)
1555 dev_kfree_skb_any(skb
);
1558 EXPORT_SYMBOL(z8530_null_rx
);
1561 * z8530_rx_done - Receive completion callback
1562 * @c: The channel that completed a receive
1564 * A new packet is complete. Our goal here is to get back into receive
1565 * mode as fast as possible. On the Z85230 we could change to using
1566 * ESCC mode, but on the older chips we have no choice. We flip to the
1567 * new buffer immediately in DMA mode so that the DMA of the next
1568 * frame can occur while we are copying the previous buffer to an sk_buff
1570 * Called with the lock held
1573 static void z8530_rx_done(struct z8530_channel
*c
)
1575 struct sk_buff
*skb
;
1579 * Is our receive engine in DMA mode
1585 * Save the ready state and the buffer currently
1586 * being used as the DMA target
1589 int ready
=c
->dma_ready
;
1590 unsigned char *rxb
=c
->rx_buf
[c
->dma_num
];
1591 unsigned long flags
;
1594 * Complete this DMA. Necessary to find the length
1597 flags
=claim_dma_lock();
1599 disable_dma(c
->rxdma
);
1600 clear_dma_ff(c
->rxdma
);
1602 ct
=c
->mtu
-get_dma_residue(c
->rxdma
);
1604 ct
=2; /* Shit happens.. */
1608 * Normal case: the other slot is free, start the next DMA
1609 * into it immediately.
1615 set_dma_mode(c
->rxdma
, DMA_MODE_READ
|0x10);
1616 set_dma_addr(c
->rxdma
, virt_to_bus(c
->rx_buf
[c
->dma_num
]));
1617 set_dma_count(c
->rxdma
, c
->mtu
);
1619 enable_dma(c
->rxdma
);
1620 /* Stop any frames that we missed the head of
1622 write_zsreg(c
, R0
, RES_Rx_CRC
);
1625 /* Can't occur as we dont reenable the DMA irq until
1626 after the flip is done */
1627 netdev_warn(c
->netdevice
, "DMA flip overrun!\n");
1629 release_dma_lock(flags
);
1632 * Shove the old buffer into an sk_buff. We can't DMA
1633 * directly into one on a PC - it might be above the 16Mb
1634 * boundary. Optimisation - we could check to see if we
1635 * can avoid the copy. Optimisation 2 - make the memcpy
1639 skb
= dev_alloc_skb(ct
);
1641 c
->netdevice
->stats
.rx_dropped
++;
1642 netdev_warn(c
->netdevice
, "Memory squeeze\n");
1645 skb_copy_to_linear_data(skb
, rxb
, ct
);
1646 c
->netdevice
->stats
.rx_packets
++;
1647 c
->netdevice
->stats
.rx_bytes
+= ct
;
1655 * The game we play for non DMA is similar. We want to
1656 * get the controller set up for the next packet as fast
1657 * as possible. We potentially only have one byte + the
1658 * fifo length for this. Thus we want to flip to the new
1659 * buffer and then mess around copying and allocating
1660 * things. For the current case it doesn't matter but
1661 * if you build a system where the sync irq isn't blocked
1662 * by the kernel IRQ disable then you need only block the
1663 * sync IRQ for the RT_LOCK area.
1672 c
->dptr
= c
->skb
->data
;
1680 c
->skb2
= dev_alloc_skb(c
->mtu
);
1681 if (c
->skb2
== NULL
)
1682 netdev_warn(c
->netdevice
, "memory squeeze\n");
1684 skb_put(c
->skb2
, c
->mtu
);
1685 c
->netdevice
->stats
.rx_packets
++;
1686 c
->netdevice
->stats
.rx_bytes
+= ct
;
1689 * If we received a frame we must now process it.
1693 c
->rx_function(c
, skb
);
1695 c
->netdevice
->stats
.rx_dropped
++;
1696 netdev_err(c
->netdevice
, "Lost a frame\n");
1701 * spans_boundary - Check a packet can be ISA DMA'd
1702 * @skb: The buffer to check
1704 * Returns true if the buffer cross a DMA boundary on a PC. The poor
1705 * thing can only DMA within a 64K block not across the edges of it.
1708 static inline int spans_boundary(struct sk_buff
*skb
)
1710 unsigned long a
=(unsigned long)skb
->data
;
1712 if(a
&0x00010000) /* If the 64K bit is different.. */
1718 * z8530_queue_xmit - Queue a packet
1719 * @c: The channel to use
1720 * @skb: The packet to kick down the channel
1722 * Queue a packet for transmission. Because we have rather
1723 * hard to hit interrupt latencies for the Z85230 per packet
1724 * even in DMA mode we do the flip to DMA buffer if needed here
1727 * Called from the network code. The lock is not held at this
1731 netdev_tx_t
z8530_queue_xmit(struct z8530_channel
*c
, struct sk_buff
*skb
)
1733 unsigned long flags
;
1735 netif_stop_queue(c
->netdevice
);
1737 return NETDEV_TX_BUSY
;
1740 /* PC SPECIFIC - DMA limits */
1743 * If we will DMA the transmit and its gone over the ISA bus
1744 * limit, then copy to the flip buffer
1747 if(c
->dma_tx
&& ((unsigned long)(virt_to_bus(skb
->data
+skb
->len
))>=16*1024*1024 || spans_boundary(skb
)))
1750 * Send the flip buffer, and flip the flippy bit.
1751 * We don't care which is used when just so long as
1752 * we never use the same buffer twice in a row. Since
1753 * only one buffer can be going out at a time the other
1756 c
->tx_next_ptr
=c
->tx_dma_buf
[c
->tx_dma_used
];
1757 c
->tx_dma_used
^=1; /* Flip temp buffer */
1758 skb_copy_from_linear_data(skb
, c
->tx_next_ptr
, skb
->len
);
1761 c
->tx_next_ptr
=skb
->data
;
1766 spin_lock_irqsave(c
->lock
, flags
);
1768 spin_unlock_irqrestore(c
->lock
, flags
);
1770 return NETDEV_TX_OK
;
1773 EXPORT_SYMBOL(z8530_queue_xmit
);
1778 static const char banner
[] __initconst
=
1779 KERN_INFO
"Generic Z85C30/Z85230 interface driver v0.02\n";
1781 static int __init
z85230_init_driver(void)
1786 module_init(z85230_init_driver
);
1788 static void __exit
z85230_cleanup_driver(void)
1791 module_exit(z85230_cleanup_driver
);
1793 MODULE_AUTHOR("Red Hat Inc.");
1794 MODULE_DESCRIPTION("Z85x30 synchronous driver core");
1795 MODULE_LICENSE("GPL");