Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / net / ethernet / amd / depca.c
blobc771de71612a8f75d82dc11e2ca629015c7c78da
1 /* depca.c: A DIGITAL DEPCA & EtherWORKS ethernet driver for linux.
3 Written 1994, 1995 by David C. Davies.
6 Copyright 1994 David C. Davies
7 and
8 United States Government
9 (as represented by the Director, National Security Agency).
11 Copyright 1995 Digital Equipment Corporation.
14 This software may be used and distributed according to the terms of
15 the GNU General Public License, incorporated herein by reference.
17 This driver is written for the Digital Equipment Corporation series
18 of DEPCA and EtherWORKS ethernet cards:
20 DEPCA (the original)
21 DE100
22 DE101
23 DE200 Turbo
24 DE201 Turbo
25 DE202 Turbo (TP BNC)
26 DE210
27 DE422 (EISA)
29 The driver has been tested on DE100, DE200 and DE202 cards in a
30 relatively busy network. The DE422 has been tested a little.
32 This driver will NOT work for the DE203, DE204 and DE205 series of
33 cards, since they have a new custom ASIC in place of the AMD LANCE
34 chip. See the 'ewrk3.c' driver in the Linux source tree for running
35 those cards.
37 I have benchmarked the driver with a DE100 at 595kB/s to (542kB/s from)
38 a DECstation 5000/200.
40 The author may be reached at davies@maniac.ultranet.com
42 =========================================================================
44 The driver was originally based on the 'lance.c' driver from Donald
45 Becker which is included with the standard driver distribution for
46 linux. V0.4 is a complete re-write with only the kernel interface
47 remaining from the original code.
49 1) Lance.c code in /linux/drivers/net/
50 2) "Ethernet/IEEE 802.3 Family. 1992 World Network Data Book/Handbook",
51 AMD, 1992 [(800) 222-9323].
52 3) "Am79C90 CMOS Local Area Network Controller for Ethernet (C-LANCE)",
53 AMD, Pub. #17881, May 1993.
54 4) "Am79C960 PCnet-ISA(tm), Single-Chip Ethernet Controller for ISA",
55 AMD, Pub. #16907, May 1992
56 5) "DEC EtherWORKS LC Ethernet Controller Owners Manual",
57 Digital Equipment corporation, 1990, Pub. #EK-DE100-OM.003
58 6) "DEC EtherWORKS Turbo Ethernet Controller Owners Manual",
59 Digital Equipment corporation, 1990, Pub. #EK-DE200-OM.003
60 7) "DEPCA Hardware Reference Manual", Pub. #EK-DEPCA-PR
61 Digital Equipment Corporation, 1989
62 8) "DEC EtherWORKS Turbo_(TP BNC) Ethernet Controller Owners Manual",
63 Digital Equipment corporation, 1991, Pub. #EK-DE202-OM.001
66 Peter Bauer's depca.c (V0.5) was referred to when debugging V0.1 of this
67 driver.
69 The original DEPCA card requires that the ethernet ROM address counter
70 be enabled to count and has an 8 bit NICSR. The ROM counter enabling is
71 only done when a 0x08 is read as the first address octet (to minimise
72 the chances of writing over some other hardware's I/O register). The
73 NICSR accesses have been changed to byte accesses for all the cards
74 supported by this driver, since there is only one useful bit in the MSB
75 (remote boot timeout) and it is not used. Also, there is a maximum of
76 only 48kB network RAM for this card. My thanks to Torbjorn Lindh for
77 help debugging all this (and holding my feet to the fire until I got it
78 right).
80 The DE200 series boards have on-board 64kB RAM for use as a shared
81 memory network buffer. Only the DE100 cards make use of a 2kB buffer
82 mode which has not been implemented in this driver (only the 32kB and
83 64kB modes are supported [16kB/48kB for the original DEPCA]).
85 At the most only 2 DEPCA cards can be supported on the ISA bus because
86 there is only provision for two I/O base addresses on each card (0x300
87 and 0x200). The I/O address is detected by searching for a byte sequence
88 in the Ethernet station address PROM at the expected I/O address for the
89 Ethernet PROM. The shared memory base address is 'autoprobed' by
90 looking for the self test PROM and detecting the card name. When a
91 second DEPCA is detected, information is placed in the base_addr
92 variable of the next device structure (which is created if necessary),
93 thus enabling ethif_probe initialization for the device. More than 2
94 EISA cards can be supported, but care will be needed assigning the
95 shared memory to ensure that each slot has the correct IRQ, I/O address
96 and shared memory address assigned.
98 ************************************************************************
100 NOTE: If you are using two ISA DEPCAs, it is important that you assign
101 the base memory addresses correctly. The driver autoprobes I/O 0x300
102 then 0x200. The base memory address for the first device must be less
103 than that of the second so that the auto probe will correctly assign the
104 I/O and memory addresses on the same card. I can't think of a way to do
105 this unambiguously at the moment, since there is nothing on the cards to
106 tie I/O and memory information together.
108 I am unable to test 2 cards together for now, so this code is
109 unchecked. All reports, good or bad, are welcome.
111 ************************************************************************
113 The board IRQ setting must be at an unused IRQ which is auto-probed
114 using Donald Becker's autoprobe routines. DEPCA and DE100 board IRQs are
115 {2,3,4,5,7}, whereas the DE200 is at {5,9,10,11,15}. Note that IRQ2 is
116 really IRQ9 in machines with 16 IRQ lines.
118 No 16MB memory limitation should exist with this driver as DMA is not
119 used and the common memory area is in low memory on the network card (my
120 current system has 20MB and I've not had problems yet).
122 The ability to load this driver as a loadable module has been added. To
123 utilise this ability, you have to do <8 things:
125 0) have a copy of the loadable modules code installed on your system.
126 1) copy depca.c from the /linux/drivers/net directory to your favourite
127 temporary directory.
128 2) if you wish, edit the source code near line 1530 to reflect the I/O
129 address and IRQ you're using (see also 5).
130 3) compile depca.c, but include -DMODULE in the command line to ensure
131 that the correct bits are compiled (see end of source code).
132 4) if you are wanting to add a new card, goto 5. Otherwise, recompile a
133 kernel with the depca configuration turned off and reboot.
134 5) insmod depca.o [irq=7] [io=0x200] [mem=0xd0000] [adapter_name=DE100]
135 [Alan Cox: Changed the code to allow command line irq/io assignments]
136 [Dave Davies: Changed the code to allow command line mem/name
137 assignments]
138 6) run the net startup bits for your eth?? interface manually
139 (usually /etc/rc.inet[12] at boot time).
140 7) enjoy!
142 Note that autoprobing is not allowed in loadable modules - the system is
143 already up and running and you're messing with interrupts.
145 To unload a module, turn off the associated interface
146 'ifconfig eth?? down' then 'rmmod depca'.
148 To assign a base memory address for the shared memory when running as a
149 loadable module, see 5 above. To include the adapter name (if you have
150 no PROM but know the card name) also see 5 above. Note that this last
151 option will not work with kernel built-in depca's.
153 The shared memory assignment for a loadable module makes sense to avoid
154 the 'memory autoprobe' picking the wrong shared memory (for the case of
155 2 depca's in a PC).
157 ************************************************************************
158 Support for MCA EtherWORKS cards added 11-3-98. (MCA since deleted)
159 Verified to work with up to 2 DE212 cards in a system (although not
160 fully stress-tested).
162 Revision History
163 ----------------
165 Version Date Description
167 0.1 25-jan-94 Initial writing.
168 0.2 27-jan-94 Added LANCE TX hardware buffer chaining.
169 0.3 1-feb-94 Added multiple DEPCA support.
170 0.31 4-feb-94 Added DE202 recognition.
171 0.32 19-feb-94 Tidy up. Improve multi-DEPCA support.
172 0.33 25-feb-94 Fix DEPCA ethernet ROM counter enable.
173 Add jabber packet fix from murf@perftech.com
174 and becker@super.org
175 0.34 7-mar-94 Fix DEPCA max network memory RAM & NICSR access.
176 0.35 8-mar-94 Added DE201 recognition. Tidied up.
177 0.351 30-apr-94 Added EISA support. Added DE422 recognition.
178 0.36 16-may-94 DE422 fix released.
179 0.37 22-jul-94 Added MODULE support
180 0.38 15-aug-94 Added DBR ROM switch in depca_close().
181 Multi DEPCA bug fix.
182 0.38axp 15-sep-94 Special version for Alpha AXP Linux V1.0.
183 0.381 12-dec-94 Added DE101 recognition, fix multicast bug.
184 0.382 9-feb-95 Fix recognition bug reported by <bkm@star.rl.ac.uk>.
185 0.383 22-feb-95 Fix for conflict with VESA SCSI reported by
186 <stromain@alf.dec.com>
187 0.384 17-mar-95 Fix a ring full bug reported by <bkm@star.rl.ac.uk>
188 0.385 3-apr-95 Fix a recognition bug reported by
189 <ryan.niemi@lastfrontier.com>
190 0.386 21-apr-95 Fix the last fix...sorry, must be galloping senility
191 0.40 25-May-95 Rewrite for portability & updated.
192 ALPHA support from <jestabro@amt.tay1.dec.com>
193 0.41 26-Jun-95 Added verify_area() calls in depca_ioctl() from
194 suggestion by <heiko@colossus.escape.de>
195 0.42 27-Dec-95 Add 'mem' shared memory assignment for loadable
196 modules.
197 Add 'adapter_name' for loadable modules when no PROM.
198 Both above from a suggestion by
199 <pchen@woodruffs121.residence.gatech.edu>.
200 Add new multicasting code.
201 0.421 22-Apr-96 Fix alloc_device() bug <jari@markkus2.fimr.fi>
202 0.422 29-Apr-96 Fix depca_hw_init() bug <jari@markkus2.fimr.fi>
203 0.423 7-Jun-96 Fix module load bug <kmg@barco.be>
204 0.43 16-Aug-96 Update alloc_device() to conform to de4x5.c
205 0.44 1-Sep-97 Fix *_probe() to test check_region() first - bug
206 reported by <mmogilvi@elbert.uccs.edu>
207 0.45 3-Nov-98 Added support for MCA EtherWORKS (DE210/DE212) cards
208 by <tymm@computer.org>
209 0.451 5-Nov-98 Fixed mca stuff cuz I'm a dummy. <tymm@computer.org>
210 0.5 14-Nov-98 Re-spin for 2.1.x kernels.
211 0.51 27-Jun-99 Correct received packet length for CRC from
212 report by <worm@dkik.dk>
213 0.52 16-Oct-00 Fixes for 2.3 io memory accesses
214 Fix show-stopper (ints left masked) in depca_interrupt
215 by <peterd@pnd-pc.demon.co.uk>
216 0.53 12-Jan-01 Release resources on failure, bss tidbits
217 by acme@conectiva.com.br
218 0.54 08-Nov-01 use library crc32 functions
219 by Matt_Domsch@dell.com
220 0.55 01-Mar-03 Use EISA/sysfs framework <maz@wild-wind.fr.eu.org>
222 =========================================================================
225 #include <linux/module.h>
226 #include <linux/kernel.h>
227 #include <linux/sched.h>
228 #include <linux/string.h>
229 #include <linux/errno.h>
230 #include <linux/ioport.h>
231 #include <linux/slab.h>
232 #include <linux/interrupt.h>
233 #include <linux/delay.h>
234 #include <linux/init.h>
235 #include <linux/crc32.h>
236 #include <linux/netdevice.h>
237 #include <linux/etherdevice.h>
238 #include <linux/skbuff.h>
239 #include <linux/time.h>
240 #include <linux/types.h>
241 #include <linux/unistd.h>
242 #include <linux/ctype.h>
243 #include <linux/moduleparam.h>
244 #include <linux/platform_device.h>
245 #include <linux/bitops.h>
247 #include <asm/uaccess.h>
248 #include <asm/io.h>
249 #include <asm/dma.h>
251 #ifdef CONFIG_EISA
252 #include <linux/eisa.h>
253 #endif
255 #include "depca.h"
257 static char version[] __initdata = "depca.c:v0.53 2001/1/12 davies@maniac.ultranet.com\n";
259 #ifdef DEPCA_DEBUG
260 static int depca_debug = DEPCA_DEBUG;
261 #else
262 static int depca_debug = 1;
263 #endif
265 #define DEPCA_NDA 0xffe0 /* No Device Address */
267 #define TX_TIMEOUT (1*HZ)
270 ** Ethernet PROM defines
272 #define PROBE_LENGTH 32
273 #define ETH_PROM_SIG 0xAA5500FFUL
276 ** Set the number of Tx and Rx buffers. Ensure that the memory requested
277 ** here is <= to the amount of shared memory set up by the board switches.
278 ** The number of descriptors MUST BE A POWER OF 2.
280 ** total_memory = NUM_RX_DESC*(8+RX_BUFF_SZ) + NUM_TX_DESC*(8+TX_BUFF_SZ)
282 #define NUM_RX_DESC 8 /* Number of RX descriptors */
283 #define NUM_TX_DESC 8 /* Number of TX descriptors */
284 #define RX_BUFF_SZ 1536 /* Buffer size for each Rx buffer */
285 #define TX_BUFF_SZ 1536 /* Buffer size for each Tx buffer */
288 ** EISA bus defines
290 #define DEPCA_EISA_IO_PORTS 0x0c00 /* I/O port base address, slot 0 */
293 ** ISA Bus defines
295 #define DEPCA_RAM_BASE_ADDRESSES {0xc0000,0xd0000,0xe0000,0x00000}
296 #define DEPCA_TOTAL_SIZE 0x10
298 static struct {
299 u_long iobase;
300 struct platform_device *device;
301 } depca_io_ports[] = {
302 { 0x300, NULL },
303 { 0x200, NULL },
304 { 0 , NULL },
308 ** Name <-> Adapter mapping
310 #define DEPCA_SIGNATURE {"DEPCA",\
311 "DE100","DE101",\
312 "DE200","DE201","DE202",\
313 "DE210","DE212",\
314 "DE422",\
317 static char* __initdata depca_signature[] = DEPCA_SIGNATURE;
319 enum depca_type {
320 DEPCA, de100, de101, de200, de201, de202, de210, de212, de422, unknown
323 static char depca_string[] = "depca";
325 static int depca_device_remove (struct device *device);
327 #ifdef CONFIG_EISA
328 static struct eisa_device_id depca_eisa_ids[] = {
329 { "DEC4220", de422 },
330 { "" }
332 MODULE_DEVICE_TABLE(eisa, depca_eisa_ids);
334 static int depca_eisa_probe (struct device *device);
336 static struct eisa_driver depca_eisa_driver = {
337 .id_table = depca_eisa_ids,
338 .driver = {
339 .name = depca_string,
340 .probe = depca_eisa_probe,
341 .remove = __devexit_p (depca_device_remove)
344 #endif
346 static int depca_isa_probe (struct platform_device *);
348 static int __devexit depca_isa_remove(struct platform_device *pdev)
350 return depca_device_remove(&pdev->dev);
353 static struct platform_driver depca_isa_driver = {
354 .probe = depca_isa_probe,
355 .remove = __devexit_p(depca_isa_remove),
356 .driver = {
357 .name = depca_string,
362 ** Miscellaneous info...
364 #define DEPCA_STRLEN 16
367 ** Memory Alignment. Each descriptor is 4 longwords long. To force a
368 ** particular alignment on the TX descriptor, adjust DESC_SKIP_LEN and
369 ** DESC_ALIGN. DEPCA_ALIGN aligns the start address of the private memory area
370 ** and hence the RX descriptor ring's first entry.
372 #define DEPCA_ALIGN4 ((u_long)4 - 1) /* 1 longword align */
373 #define DEPCA_ALIGN8 ((u_long)8 - 1) /* 2 longword (quadword) align */
374 #define DEPCA_ALIGN DEPCA_ALIGN8 /* Keep the LANCE happy... */
377 ** The DEPCA Rx and Tx ring descriptors.
379 struct depca_rx_desc {
380 volatile s32 base;
381 s16 buf_length; /* This length is negative 2's complement! */
382 s16 msg_length; /* This length is "normal". */
385 struct depca_tx_desc {
386 volatile s32 base;
387 s16 length; /* This length is negative 2's complement! */
388 s16 misc; /* Errors and TDR info */
391 #define LA_MASK 0x0000ffff /* LANCE address mask for mapping network RAM
392 to LANCE memory address space */
395 ** The Lance initialization block, described in databook, in common memory.
397 struct depca_init {
398 u16 mode; /* Mode register */
399 u8 phys_addr[ETH_ALEN]; /* Physical ethernet address */
400 u8 mcast_table[8]; /* Multicast Hash Table. */
401 u32 rx_ring; /* Rx ring base pointer & ring length */
402 u32 tx_ring; /* Tx ring base pointer & ring length */
405 #define DEPCA_PKT_STAT_SZ 16
406 #define DEPCA_PKT_BIN_SZ 128 /* Should be >=100 unless you
407 increase DEPCA_PKT_STAT_SZ */
408 struct depca_private {
409 char adapter_name[DEPCA_STRLEN]; /* /proc/ioports string */
410 enum depca_type adapter; /* Adapter type */
411 enum {
412 DEPCA_BUS_ISA = 1,
413 DEPCA_BUS_EISA,
414 } depca_bus; /* type of bus */
415 struct depca_init init_block; /* Shadow Initialization block */
416 /* CPU address space fields */
417 struct depca_rx_desc __iomem *rx_ring; /* Pointer to start of RX descriptor ring */
418 struct depca_tx_desc __iomem *tx_ring; /* Pointer to start of TX descriptor ring */
419 void __iomem *rx_buff[NUM_RX_DESC]; /* CPU virt address of sh'd memory buffs */
420 void __iomem *tx_buff[NUM_TX_DESC]; /* CPU virt address of sh'd memory buffs */
421 void __iomem *sh_mem; /* CPU mapped virt address of device RAM */
422 u_long mem_start; /* Bus address of device RAM (before remap) */
423 u_long mem_len; /* device memory size */
424 /* Device address space fields */
425 u_long device_ram_start; /* Start of RAM in device addr space */
426 /* Offsets used in both address spaces */
427 u_long rx_ring_offset; /* Offset from start of RAM to rx_ring */
428 u_long tx_ring_offset; /* Offset from start of RAM to tx_ring */
429 u_long buffs_offset; /* LANCE Rx and Tx buffers start address. */
430 /* Kernel-only (not device) fields */
431 int rx_new, tx_new; /* The next free ring entry */
432 int rx_old, tx_old; /* The ring entries to be free()ed. */
433 spinlock_t lock;
434 struct { /* Private stats counters */
435 u32 bins[DEPCA_PKT_STAT_SZ];
436 u32 unicast;
437 u32 multicast;
438 u32 broadcast;
439 u32 excessive_collisions;
440 u32 tx_underruns;
441 u32 excessive_underruns;
442 } pktStats;
443 int txRingMask; /* TX ring mask */
444 int rxRingMask; /* RX ring mask */
445 s32 rx_rlen; /* log2(rxRingMask+1) for the descriptors */
446 s32 tx_rlen; /* log2(txRingMask+1) for the descriptors */
450 ** The transmit ring full condition is described by the tx_old and tx_new
451 ** pointers by:
452 ** tx_old = tx_new Empty ring
453 ** tx_old = tx_new+1 Full ring
454 ** tx_old+txRingMask = tx_new Full ring (wrapped condition)
456 #define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?\
457 lp->tx_old+lp->txRingMask-lp->tx_new:\
458 lp->tx_old -lp->tx_new-1)
461 ** Public Functions
463 static int depca_open(struct net_device *dev);
464 static netdev_tx_t depca_start_xmit(struct sk_buff *skb,
465 struct net_device *dev);
466 static irqreturn_t depca_interrupt(int irq, void *dev_id);
467 static int depca_close(struct net_device *dev);
468 static int depca_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
469 static void depca_tx_timeout(struct net_device *dev);
470 static void set_multicast_list(struct net_device *dev);
473 ** Private functions
475 static void depca_init_ring(struct net_device *dev);
476 static int depca_rx(struct net_device *dev);
477 static int depca_tx(struct net_device *dev);
479 static void LoadCSRs(struct net_device *dev);
480 static int InitRestartDepca(struct net_device *dev);
481 static int DepcaSignature(char *name, u_long paddr);
482 static int DevicePresent(u_long ioaddr);
483 static int get_hw_addr(struct net_device *dev);
484 static void SetMulticastFilter(struct net_device *dev);
485 static int load_packet(struct net_device *dev, struct sk_buff *skb);
486 static void depca_dbg_open(struct net_device *dev);
488 static u_char de1xx_irq[] __initdata = { 2, 3, 4, 5, 7, 9, 0 };
489 static u_char de2xx_irq[] __initdata = { 5, 9, 10, 11, 15, 0 };
490 static u_char de422_irq[] __initdata = { 5, 9, 10, 11, 0 };
491 static u_char *depca_irq;
493 static int irq;
494 static int io;
495 static char *adapter_name;
496 static int mem; /* For loadable module assignment
497 use insmod mem=0x????? .... */
498 module_param (irq, int, 0);
499 module_param (io, int, 0);
500 module_param (adapter_name, charp, 0);
501 module_param (mem, int, 0);
502 MODULE_PARM_DESC(irq, "DEPCA IRQ number");
503 MODULE_PARM_DESC(io, "DEPCA I/O base address");
504 MODULE_PARM_DESC(adapter_name, "DEPCA adapter name");
505 MODULE_PARM_DESC(mem, "DEPCA shared memory address");
506 MODULE_LICENSE("GPL");
509 ** Miscellaneous defines...
511 #define STOP_DEPCA \
512 outw(CSR0, DEPCA_ADDR);\
513 outw(STOP, DEPCA_DATA)
515 static const struct net_device_ops depca_netdev_ops = {
516 .ndo_open = depca_open,
517 .ndo_start_xmit = depca_start_xmit,
518 .ndo_stop = depca_close,
519 .ndo_set_rx_mode = set_multicast_list,
520 .ndo_do_ioctl = depca_ioctl,
521 .ndo_tx_timeout = depca_tx_timeout,
522 .ndo_change_mtu = eth_change_mtu,
523 .ndo_set_mac_address = eth_mac_addr,
524 .ndo_validate_addr = eth_validate_addr,
527 static int __init depca_hw_init (struct net_device *dev, struct device *device)
529 struct depca_private *lp;
530 int i, j, offset, netRAM, mem_len, status = 0;
531 s16 nicsr;
532 u_long ioaddr;
533 u_long mem_start;
536 * We are now supposed to enter this function with the
537 * following fields filled with proper values :
539 * dev->base_addr
540 * lp->mem_start
541 * lp->depca_bus
542 * lp->adapter
544 * dev->irq can be set if known from device configuration (on
545 * MCA or EISA) or module option. Otherwise, it will be auto
546 * detected.
549 ioaddr = dev->base_addr;
551 STOP_DEPCA;
553 nicsr = inb(DEPCA_NICSR);
554 nicsr = ((nicsr & ~SHE & ~RBE & ~IEN) | IM);
555 outb(nicsr, DEPCA_NICSR);
557 if (inw(DEPCA_DATA) != STOP) {
558 return -ENXIO;
561 lp = netdev_priv(dev);
562 mem_start = lp->mem_start;
564 if (!mem_start || lp->adapter < DEPCA || lp->adapter >=unknown)
565 return -ENXIO;
567 printk("%s: %s at 0x%04lx",
568 dev_name(device), depca_signature[lp->adapter], ioaddr);
570 switch (lp->depca_bus) {
571 #ifdef CONFIG_EISA
572 case DEPCA_BUS_EISA:
573 printk(" (EISA slot %d)", to_eisa_device(device)->slot);
574 break;
575 #endif
577 case DEPCA_BUS_ISA:
578 break;
580 default:
581 printk("Unknown DEPCA bus %d\n", lp->depca_bus);
582 return -ENXIO;
585 printk(", h/w address ");
586 status = get_hw_addr(dev);
587 printk("%pM", dev->dev_addr);
588 if (status != 0) {
589 printk(" which has an Ethernet PROM CRC error.\n");
590 return -ENXIO;
593 /* Set up the maximum amount of network RAM(kB) */
594 netRAM = ((lp->adapter != DEPCA) ? 64 : 48);
595 if ((nicsr & _128KB) && (lp->adapter == de422))
596 netRAM = 128;
598 /* Shared Memory Base Address */
599 if (nicsr & BUF) {
600 nicsr &= ~BS; /* DEPCA RAM in top 32k */
601 netRAM -= 32;
602 mem_start += 0x8000;
605 if ((mem_len = (NUM_RX_DESC * (sizeof(struct depca_rx_desc) + RX_BUFF_SZ) + NUM_TX_DESC * (sizeof(struct depca_tx_desc) + TX_BUFF_SZ) + sizeof(struct depca_init)))
606 > (netRAM << 10)) {
607 printk(",\n requests %dkB RAM: only %dkB is available!\n", (mem_len >> 10), netRAM);
608 return -ENXIO;
611 printk(",\n has %dkB RAM at 0x%.5lx", netRAM, mem_start);
613 /* Enable the shadow RAM. */
614 if (lp->adapter != DEPCA) {
615 nicsr |= SHE;
616 outb(nicsr, DEPCA_NICSR);
619 spin_lock_init(&lp->lock);
620 sprintf(lp->adapter_name, "%s (%s)",
621 depca_signature[lp->adapter], dev_name(device));
622 status = -EBUSY;
624 /* Initialisation Block */
625 if (!request_mem_region (mem_start, mem_len, lp->adapter_name)) {
626 printk(KERN_ERR "depca: cannot request ISA memory, aborting\n");
627 goto out_priv;
630 status = -EIO;
631 lp->sh_mem = ioremap(mem_start, mem_len);
632 if (lp->sh_mem == NULL) {
633 printk(KERN_ERR "depca: cannot remap ISA memory, aborting\n");
634 goto out1;
637 lp->mem_start = mem_start;
638 lp->mem_len = mem_len;
639 lp->device_ram_start = mem_start & LA_MASK;
641 offset = 0;
642 offset += sizeof(struct depca_init);
644 /* Tx & Rx descriptors (aligned to a quadword boundary) */
645 offset = (offset + DEPCA_ALIGN) & ~DEPCA_ALIGN;
646 lp->rx_ring = lp->sh_mem + offset;
647 lp->rx_ring_offset = offset;
649 offset += (sizeof(struct depca_rx_desc) * NUM_RX_DESC);
650 lp->tx_ring = lp->sh_mem + offset;
651 lp->tx_ring_offset = offset;
653 offset += (sizeof(struct depca_tx_desc) * NUM_TX_DESC);
655 lp->buffs_offset = offset;
657 /* Finish initialising the ring information. */
658 lp->rxRingMask = NUM_RX_DESC - 1;
659 lp->txRingMask = NUM_TX_DESC - 1;
661 /* Calculate Tx/Rx RLEN size for the descriptors. */
662 for (i = 0, j = lp->rxRingMask; j > 0; i++) {
663 j >>= 1;
665 lp->rx_rlen = (s32) (i << 29);
666 for (i = 0, j = lp->txRingMask; j > 0; i++) {
667 j >>= 1;
669 lp->tx_rlen = (s32) (i << 29);
671 /* Load the initialisation block */
672 depca_init_ring(dev);
674 /* Initialise the control and status registers */
675 LoadCSRs(dev);
677 /* Enable DEPCA board interrupts for autoprobing */
678 nicsr = ((nicsr & ~IM) | IEN);
679 outb(nicsr, DEPCA_NICSR);
681 /* To auto-IRQ we enable the initialization-done and DMA err,
682 interrupts. For now we will always get a DMA error. */
683 if (dev->irq < 2) {
684 unsigned char irqnum;
685 unsigned long irq_mask, delay;
687 irq_mask = probe_irq_on();
689 /* Assign the correct irq list */
690 switch (lp->adapter) {
691 case DEPCA:
692 case de100:
693 case de101:
694 depca_irq = de1xx_irq;
695 break;
696 case de200:
697 case de201:
698 case de202:
699 case de210:
700 case de212:
701 depca_irq = de2xx_irq;
702 break;
703 case de422:
704 depca_irq = de422_irq;
705 break;
707 default:
708 break; /* Not reached */
711 /* Trigger an initialization just for the interrupt. */
712 outw(INEA | INIT, DEPCA_DATA);
714 delay = jiffies + HZ/50;
715 while (time_before(jiffies, delay))
716 yield();
718 irqnum = probe_irq_off(irq_mask);
720 status = -ENXIO;
721 if (!irqnum) {
722 printk(" and failed to detect IRQ line.\n");
723 goto out2;
724 } else {
725 for (dev->irq = 0, i = 0; (depca_irq[i]) && (!dev->irq); i++)
726 if (irqnum == depca_irq[i]) {
727 dev->irq = irqnum;
728 printk(" and uses IRQ%d.\n", dev->irq);
731 if (!dev->irq) {
732 printk(" but incorrect IRQ line detected.\n");
733 goto out2;
736 } else {
737 printk(" and assigned IRQ%d.\n", dev->irq);
740 if (depca_debug > 1) {
741 printk(version);
744 /* The DEPCA-specific entries in the device structure. */
745 dev->netdev_ops = &depca_netdev_ops;
746 dev->watchdog_timeo = TX_TIMEOUT;
748 dev->mem_start = 0;
750 dev_set_drvdata(device, dev);
751 SET_NETDEV_DEV (dev, device);
753 status = register_netdev(dev);
754 if (status == 0)
755 return 0;
756 out2:
757 iounmap(lp->sh_mem);
758 out1:
759 release_mem_region (mem_start, mem_len);
760 out_priv:
761 return status;
765 static int depca_open(struct net_device *dev)
767 struct depca_private *lp = netdev_priv(dev);
768 u_long ioaddr = dev->base_addr;
769 s16 nicsr;
770 int status = 0;
772 STOP_DEPCA;
773 nicsr = inb(DEPCA_NICSR);
775 /* Make sure the shadow RAM is enabled */
776 if (lp->adapter != DEPCA) {
777 nicsr |= SHE;
778 outb(nicsr, DEPCA_NICSR);
781 /* Re-initialize the DEPCA... */
782 depca_init_ring(dev);
783 LoadCSRs(dev);
785 depca_dbg_open(dev);
787 if (request_irq(dev->irq, depca_interrupt, 0, lp->adapter_name, dev)) {
788 printk("depca_open(): Requested IRQ%d is busy\n", dev->irq);
789 status = -EAGAIN;
790 } else {
792 /* Enable DEPCA board interrupts and turn off LED */
793 nicsr = ((nicsr & ~IM & ~LED) | IEN);
794 outb(nicsr, DEPCA_NICSR);
795 outw(CSR0, DEPCA_ADDR);
797 netif_start_queue(dev);
799 status = InitRestartDepca(dev);
801 if (depca_debug > 1) {
802 printk("CSR0: 0x%4.4x\n", inw(DEPCA_DATA));
803 printk("nicsr: 0x%02x\n", inb(DEPCA_NICSR));
806 return status;
809 /* Initialize the lance Rx and Tx descriptor rings. */
810 static void depca_init_ring(struct net_device *dev)
812 struct depca_private *lp = netdev_priv(dev);
813 u_int i;
814 u_long offset;
816 /* Lock out other processes whilst setting up the hardware */
817 netif_stop_queue(dev);
819 lp->rx_new = lp->tx_new = 0;
820 lp->rx_old = lp->tx_old = 0;
822 /* Initialize the base address and length of each buffer in the ring */
823 for (i = 0; i <= lp->rxRingMask; i++) {
824 offset = lp->buffs_offset + i * RX_BUFF_SZ;
825 writel((lp->device_ram_start + offset) | R_OWN, &lp->rx_ring[i].base);
826 writew(-RX_BUFF_SZ, &lp->rx_ring[i].buf_length);
827 lp->rx_buff[i] = lp->sh_mem + offset;
830 for (i = 0; i <= lp->txRingMask; i++) {
831 offset = lp->buffs_offset + (i + lp->rxRingMask + 1) * TX_BUFF_SZ;
832 writel((lp->device_ram_start + offset) & 0x00ffffff, &lp->tx_ring[i].base);
833 lp->tx_buff[i] = lp->sh_mem + offset;
836 /* Set up the initialization block */
837 lp->init_block.rx_ring = (lp->device_ram_start + lp->rx_ring_offset) | lp->rx_rlen;
838 lp->init_block.tx_ring = (lp->device_ram_start + lp->tx_ring_offset) | lp->tx_rlen;
840 SetMulticastFilter(dev);
842 for (i = 0; i < ETH_ALEN; i++) {
843 lp->init_block.phys_addr[i] = dev->dev_addr[i];
846 lp->init_block.mode = 0x0000; /* Enable the Tx and Rx */
850 static void depca_tx_timeout(struct net_device *dev)
852 u_long ioaddr = dev->base_addr;
854 printk("%s: transmit timed out, status %04x, resetting.\n", dev->name, inw(DEPCA_DATA));
856 STOP_DEPCA;
857 depca_init_ring(dev);
858 LoadCSRs(dev);
859 dev->trans_start = jiffies; /* prevent tx timeout */
860 netif_wake_queue(dev);
861 InitRestartDepca(dev);
866 ** Writes a socket buffer to TX descriptor ring and starts transmission
868 static netdev_tx_t depca_start_xmit(struct sk_buff *skb,
869 struct net_device *dev)
871 struct depca_private *lp = netdev_priv(dev);
872 u_long ioaddr = dev->base_addr;
873 int status = 0;
875 /* Transmitter timeout, serious problems. */
876 if (skb->len < 1)
877 goto out;
879 if (skb_padto(skb, ETH_ZLEN))
880 goto out;
882 netif_stop_queue(dev);
884 if (TX_BUFFS_AVAIL) { /* Fill in a Tx ring entry */
885 status = load_packet(dev, skb);
887 if (!status) {
888 /* Trigger an immediate send demand. */
889 outw(CSR0, DEPCA_ADDR);
890 outw(INEA | TDMD, DEPCA_DATA);
892 dev_kfree_skb(skb);
894 if (TX_BUFFS_AVAIL)
895 netif_start_queue(dev);
896 } else
897 status = NETDEV_TX_LOCKED;
899 out:
900 return status;
904 ** The DEPCA interrupt handler.
906 static irqreturn_t depca_interrupt(int irq, void *dev_id)
908 struct net_device *dev = dev_id;
909 struct depca_private *lp;
910 s16 csr0, nicsr;
911 u_long ioaddr;
913 if (dev == NULL) {
914 printk("depca_interrupt(): irq %d for unknown device.\n", irq);
915 return IRQ_NONE;
918 lp = netdev_priv(dev);
919 ioaddr = dev->base_addr;
921 spin_lock(&lp->lock);
923 /* mask the DEPCA board interrupts and turn on the LED */
924 nicsr = inb(DEPCA_NICSR);
925 nicsr |= (IM | LED);
926 outb(nicsr, DEPCA_NICSR);
928 outw(CSR0, DEPCA_ADDR);
929 csr0 = inw(DEPCA_DATA);
931 /* Acknowledge all of the current interrupt sources ASAP. */
932 outw(csr0 & INTE, DEPCA_DATA);
934 if (csr0 & RINT) /* Rx interrupt (packet arrived) */
935 depca_rx(dev);
937 if (csr0 & TINT) /* Tx interrupt (packet sent) */
938 depca_tx(dev);
940 /* Any resources available? */
941 if ((TX_BUFFS_AVAIL >= 0) && netif_queue_stopped(dev)) {
942 netif_wake_queue(dev);
945 /* Unmask the DEPCA board interrupts and turn off the LED */
946 nicsr = (nicsr & ~IM & ~LED);
947 outb(nicsr, DEPCA_NICSR);
949 spin_unlock(&lp->lock);
950 return IRQ_HANDLED;
953 /* Called with lp->lock held */
954 static int depca_rx(struct net_device *dev)
956 struct depca_private *lp = netdev_priv(dev);
957 int i, entry;
958 s32 status;
960 for (entry = lp->rx_new; !(readl(&lp->rx_ring[entry].base) & R_OWN); entry = lp->rx_new) {
961 status = readl(&lp->rx_ring[entry].base) >> 16;
962 if (status & R_STP) { /* Remember start of frame */
963 lp->rx_old = entry;
965 if (status & R_ENP) { /* Valid frame status */
966 if (status & R_ERR) { /* There was an error. */
967 dev->stats.rx_errors++; /* Update the error stats. */
968 if (status & R_FRAM)
969 dev->stats.rx_frame_errors++;
970 if (status & R_OFLO)
971 dev->stats.rx_over_errors++;
972 if (status & R_CRC)
973 dev->stats.rx_crc_errors++;
974 if (status & R_BUFF)
975 dev->stats.rx_fifo_errors++;
976 } else {
977 short len, pkt_len = readw(&lp->rx_ring[entry].msg_length) - 4;
978 struct sk_buff *skb;
980 skb = netdev_alloc_skb(dev, pkt_len + 2);
981 if (skb != NULL) {
982 unsigned char *buf;
983 skb_reserve(skb, 2); /* 16 byte align the IP header */
984 buf = skb_put(skb, pkt_len);
985 if (entry < lp->rx_old) { /* Wrapped buffer */
986 len = (lp->rxRingMask - lp->rx_old + 1) * RX_BUFF_SZ;
987 memcpy_fromio(buf, lp->rx_buff[lp->rx_old], len);
988 memcpy_fromio(buf + len, lp->rx_buff[0], pkt_len - len);
989 } else { /* Linear buffer */
990 memcpy_fromio(buf, lp->rx_buff[lp->rx_old], pkt_len);
994 ** Notify the upper protocol layers that there is another
995 ** packet to handle
997 skb->protocol = eth_type_trans(skb, dev);
998 netif_rx(skb);
1001 ** Update stats
1003 dev->stats.rx_packets++;
1004 dev->stats.rx_bytes += pkt_len;
1005 for (i = 1; i < DEPCA_PKT_STAT_SZ - 1; i++) {
1006 if (pkt_len < (i * DEPCA_PKT_BIN_SZ)) {
1007 lp->pktStats.bins[i]++;
1008 i = DEPCA_PKT_STAT_SZ;
1011 if (is_multicast_ether_addr(buf)) {
1012 if (is_broadcast_ether_addr(buf)) {
1013 lp->pktStats.broadcast++;
1014 } else {
1015 lp->pktStats.multicast++;
1017 } else if (ether_addr_equal(buf,
1018 dev->dev_addr)) {
1019 lp->pktStats.unicast++;
1022 lp->pktStats.bins[0]++; /* Duplicates stats.rx_packets */
1023 if (lp->pktStats.bins[0] == 0) { /* Reset counters */
1024 memset((char *) &lp->pktStats, 0, sizeof(lp->pktStats));
1026 } else {
1027 printk("%s: Memory squeeze, deferring packet.\n", dev->name);
1028 dev->stats.rx_dropped++; /* Really, deferred. */
1029 break;
1032 /* Change buffer ownership for this last frame, back to the adapter */
1033 for (; lp->rx_old != entry; lp->rx_old = (lp->rx_old + 1) & lp->rxRingMask) {
1034 writel(readl(&lp->rx_ring[lp->rx_old].base) | R_OWN, &lp->rx_ring[lp->rx_old].base);
1036 writel(readl(&lp->rx_ring[entry].base) | R_OWN, &lp->rx_ring[entry].base);
1040 ** Update entry information
1042 lp->rx_new = (lp->rx_new + 1) & lp->rxRingMask;
1045 return 0;
1049 ** Buffer sent - check for buffer errors.
1050 ** Called with lp->lock held
1052 static int depca_tx(struct net_device *dev)
1054 struct depca_private *lp = netdev_priv(dev);
1055 int entry;
1056 s32 status;
1057 u_long ioaddr = dev->base_addr;
1059 for (entry = lp->tx_old; entry != lp->tx_new; entry = lp->tx_old) {
1060 status = readl(&lp->tx_ring[entry].base) >> 16;
1062 if (status < 0) { /* Packet not yet sent! */
1063 break;
1064 } else if (status & T_ERR) { /* An error occurred. */
1065 status = readl(&lp->tx_ring[entry].misc);
1066 dev->stats.tx_errors++;
1067 if (status & TMD3_RTRY)
1068 dev->stats.tx_aborted_errors++;
1069 if (status & TMD3_LCAR)
1070 dev->stats.tx_carrier_errors++;
1071 if (status & TMD3_LCOL)
1072 dev->stats.tx_window_errors++;
1073 if (status & TMD3_UFLO)
1074 dev->stats.tx_fifo_errors++;
1075 if (status & (TMD3_BUFF | TMD3_UFLO)) {
1076 /* Trigger an immediate send demand. */
1077 outw(CSR0, DEPCA_ADDR);
1078 outw(INEA | TDMD, DEPCA_DATA);
1080 } else if (status & (T_MORE | T_ONE)) {
1081 dev->stats.collisions++;
1082 } else {
1083 dev->stats.tx_packets++;
1086 /* Update all the pointers */
1087 lp->tx_old = (lp->tx_old + 1) & lp->txRingMask;
1090 return 0;
1093 static int depca_close(struct net_device *dev)
1095 struct depca_private *lp = netdev_priv(dev);
1096 s16 nicsr;
1097 u_long ioaddr = dev->base_addr;
1099 netif_stop_queue(dev);
1101 outw(CSR0, DEPCA_ADDR);
1103 if (depca_debug > 1) {
1104 printk("%s: Shutting down ethercard, status was %2.2x.\n", dev->name, inw(DEPCA_DATA));
1108 ** We stop the DEPCA here -- it occasionally polls
1109 ** memory if we don't.
1111 outw(STOP, DEPCA_DATA);
1114 ** Give back the ROM in case the user wants to go to DOS
1116 if (lp->adapter != DEPCA) {
1117 nicsr = inb(DEPCA_NICSR);
1118 nicsr &= ~SHE;
1119 outb(nicsr, DEPCA_NICSR);
1123 ** Free the associated irq
1125 free_irq(dev->irq, dev);
1126 return 0;
1129 static void LoadCSRs(struct net_device *dev)
1131 struct depca_private *lp = netdev_priv(dev);
1132 u_long ioaddr = dev->base_addr;
1134 outw(CSR1, DEPCA_ADDR); /* initialisation block address LSW */
1135 outw((u16) lp->device_ram_start, DEPCA_DATA);
1136 outw(CSR2, DEPCA_ADDR); /* initialisation block address MSW */
1137 outw((u16) (lp->device_ram_start >> 16), DEPCA_DATA);
1138 outw(CSR3, DEPCA_ADDR); /* ALE control */
1139 outw(ACON, DEPCA_DATA);
1141 outw(CSR0, DEPCA_ADDR); /* Point back to CSR0 */
1144 static int InitRestartDepca(struct net_device *dev)
1146 struct depca_private *lp = netdev_priv(dev);
1147 u_long ioaddr = dev->base_addr;
1148 int i, status = 0;
1150 /* Copy the shadow init_block to shared memory */
1151 memcpy_toio(lp->sh_mem, &lp->init_block, sizeof(struct depca_init));
1153 outw(CSR0, DEPCA_ADDR); /* point back to CSR0 */
1154 outw(INIT, DEPCA_DATA); /* initialize DEPCA */
1156 /* wait for lance to complete initialisation */
1157 for (i = 0; (i < 100) && !(inw(DEPCA_DATA) & IDON); i++);
1159 if (i != 100) {
1160 /* clear IDON by writing a "1", enable interrupts and start lance */
1161 outw(IDON | INEA | STRT, DEPCA_DATA);
1162 if (depca_debug > 2) {
1163 printk("%s: DEPCA open after %d ticks, init block 0x%08lx csr0 %4.4x.\n", dev->name, i, lp->mem_start, inw(DEPCA_DATA));
1165 } else {
1166 printk("%s: DEPCA unopen after %d ticks, init block 0x%08lx csr0 %4.4x.\n", dev->name, i, lp->mem_start, inw(DEPCA_DATA));
1167 status = -1;
1170 return status;
1174 ** Set or clear the multicast filter for this adaptor.
1176 static void set_multicast_list(struct net_device *dev)
1178 struct depca_private *lp = netdev_priv(dev);
1179 u_long ioaddr = dev->base_addr;
1181 netif_stop_queue(dev);
1182 while (lp->tx_old != lp->tx_new); /* Wait for the ring to empty */
1184 STOP_DEPCA; /* Temporarily stop the depca. */
1185 depca_init_ring(dev); /* Initialize the descriptor rings */
1187 if (dev->flags & IFF_PROMISC) { /* Set promiscuous mode */
1188 lp->init_block.mode |= PROM;
1189 } else {
1190 SetMulticastFilter(dev);
1191 lp->init_block.mode &= ~PROM; /* Unset promiscuous mode */
1194 LoadCSRs(dev); /* Reload CSR3 */
1195 InitRestartDepca(dev); /* Resume normal operation. */
1196 netif_start_queue(dev); /* Unlock the TX ring */
1200 ** Calculate the hash code and update the logical address filter
1201 ** from a list of ethernet multicast addresses.
1202 ** Big endian crc one liner is mine, all mine, ha ha ha ha!
1203 ** LANCE calculates its hash codes big endian.
1205 static void SetMulticastFilter(struct net_device *dev)
1207 struct depca_private *lp = netdev_priv(dev);
1208 struct netdev_hw_addr *ha;
1209 int i, j, bit, byte;
1210 u16 hashcode;
1211 u32 crc;
1213 if (dev->flags & IFF_ALLMULTI) { /* Set all multicast bits */
1214 for (i = 0; i < (HASH_TABLE_LEN >> 3); i++) {
1215 lp->init_block.mcast_table[i] = (char) 0xff;
1217 } else {
1218 for (i = 0; i < (HASH_TABLE_LEN >> 3); i++) { /* Clear the multicast table */
1219 lp->init_block.mcast_table[i] = 0;
1221 /* Add multicast addresses */
1222 netdev_for_each_mc_addr(ha, dev) {
1223 crc = ether_crc(ETH_ALEN, ha->addr);
1224 hashcode = (crc & 1); /* hashcode is 6 LSb of CRC ... */
1225 for (j = 0; j < 5; j++) { /* ... in reverse order. */
1226 hashcode = (hashcode << 1) | ((crc >>= 1) & 1);
1229 byte = hashcode >> 3; /* bit[3-5] -> byte in filter */
1230 bit = 1 << (hashcode & 0x07); /* bit[0-2] -> bit in byte */
1231 lp->init_block.mcast_table[byte] |= bit;
1236 static int __init depca_common_init (u_long ioaddr, struct net_device **devp)
1238 int status = 0;
1240 if (!request_region (ioaddr, DEPCA_TOTAL_SIZE, depca_string)) {
1241 status = -EBUSY;
1242 goto out;
1245 if (DevicePresent(ioaddr)) {
1246 status = -ENODEV;
1247 goto out_release;
1250 if (!(*devp = alloc_etherdev (sizeof (struct depca_private)))) {
1251 status = -ENOMEM;
1252 goto out_release;
1255 return 0;
1257 out_release:
1258 release_region (ioaddr, DEPCA_TOTAL_SIZE);
1259 out:
1260 return status;
1264 ** ISA bus I/O device probe
1267 static void __init depca_platform_probe (void)
1269 int i;
1270 struct platform_device *pldev;
1272 for (i = 0; depca_io_ports[i].iobase; i++) {
1273 depca_io_ports[i].device = NULL;
1275 /* if an address has been specified on the command
1276 * line, use it (if valid) */
1277 if (io && io != depca_io_ports[i].iobase)
1278 continue;
1280 pldev = platform_device_alloc(depca_string, i);
1281 if (!pldev)
1282 continue;
1284 pldev->dev.platform_data = (void *) depca_io_ports[i].iobase;
1285 depca_io_ports[i].device = pldev;
1287 if (platform_device_add(pldev)) {
1288 depca_io_ports[i].device = NULL;
1289 pldev->dev.platform_data = NULL;
1290 platform_device_put(pldev);
1291 continue;
1294 if (!pldev->dev.driver) {
1295 /* The driver was not bound to this device, there was
1296 * no hardware at this address. Unregister it, as the
1297 * release function will take care of freeing the
1298 * allocated structure */
1300 depca_io_ports[i].device = NULL;
1301 pldev->dev.platform_data = NULL;
1302 platform_device_unregister (pldev);
1307 static enum depca_type __init depca_shmem_probe (ulong *mem_start)
1309 u_long mem_base[] = DEPCA_RAM_BASE_ADDRESSES;
1310 enum depca_type adapter = unknown;
1311 int i;
1313 for (i = 0; mem_base[i]; i++) {
1314 *mem_start = mem ? mem : mem_base[i];
1315 adapter = DepcaSignature (adapter_name, *mem_start);
1316 if (adapter != unknown)
1317 break;
1320 return adapter;
1323 static int __devinit depca_isa_probe (struct platform_device *device)
1325 struct net_device *dev;
1326 struct depca_private *lp;
1327 u_long ioaddr, mem_start = 0;
1328 enum depca_type adapter = unknown;
1329 int status = 0;
1331 ioaddr = (u_long) device->dev.platform_data;
1333 if ((status = depca_common_init (ioaddr, &dev)))
1334 goto out;
1336 adapter = depca_shmem_probe (&mem_start);
1338 if (adapter == unknown) {
1339 status = -ENODEV;
1340 goto out_free;
1343 dev->base_addr = ioaddr;
1344 dev->irq = irq; /* Use whatever value the user gave
1345 * us, and 0 if he didn't. */
1346 lp = netdev_priv(dev);
1347 lp->depca_bus = DEPCA_BUS_ISA;
1348 lp->adapter = adapter;
1349 lp->mem_start = mem_start;
1351 if ((status = depca_hw_init(dev, &device->dev)))
1352 goto out_free;
1354 return 0;
1356 out_free:
1357 free_netdev (dev);
1358 release_region (ioaddr, DEPCA_TOTAL_SIZE);
1359 out:
1360 return status;
1364 ** EISA callbacks from sysfs.
1367 #ifdef CONFIG_EISA
1368 static int __init depca_eisa_probe (struct device *device)
1370 enum depca_type adapter = unknown;
1371 struct eisa_device *edev;
1372 struct net_device *dev;
1373 struct depca_private *lp;
1374 u_long ioaddr, mem_start;
1375 int status = 0;
1377 edev = to_eisa_device (device);
1378 ioaddr = edev->base_addr + DEPCA_EISA_IO_PORTS;
1380 if ((status = depca_common_init (ioaddr, &dev)))
1381 goto out;
1383 /* It would have been nice to get card configuration from the
1384 * card. Unfortunately, this register is write-only (shares
1385 * it's address with the ethernet prom)... As we don't parse
1386 * the EISA configuration structures (yet... :-), just rely on
1387 * the ISA probing to sort it out... */
1389 adapter = depca_shmem_probe (&mem_start);
1390 if (adapter == unknown) {
1391 status = -ENODEV;
1392 goto out_free;
1395 dev->base_addr = ioaddr;
1396 dev->irq = irq;
1397 lp = netdev_priv(dev);
1398 lp->depca_bus = DEPCA_BUS_EISA;
1399 lp->adapter = edev->id.driver_data;
1400 lp->mem_start = mem_start;
1402 if ((status = depca_hw_init(dev, device)))
1403 goto out_free;
1405 return 0;
1407 out_free:
1408 free_netdev (dev);
1409 release_region (ioaddr, DEPCA_TOTAL_SIZE);
1410 out:
1411 return status;
1413 #endif
1415 static int __devexit depca_device_remove (struct device *device)
1417 struct net_device *dev;
1418 struct depca_private *lp;
1419 int bus;
1421 dev = dev_get_drvdata(device);
1422 lp = netdev_priv(dev);
1424 unregister_netdev (dev);
1425 iounmap (lp->sh_mem);
1426 release_mem_region (lp->mem_start, lp->mem_len);
1427 release_region (dev->base_addr, DEPCA_TOTAL_SIZE);
1428 bus = lp->depca_bus;
1429 free_netdev (dev);
1431 return 0;
1435 ** Look for a particular board name in the on-board Remote Diagnostics
1436 ** and Boot (readb) ROM. This will also give us a clue to the network RAM
1437 ** base address.
1439 static int __init DepcaSignature(char *name, u_long base_addr)
1441 u_int i, j, k;
1442 void __iomem *ptr;
1443 char tmpstr[16];
1444 u_long prom_addr = base_addr + 0xc000;
1445 u_long mem_addr = base_addr + 0x8000; /* 32KB */
1447 /* Can't reserve the prom region, it is already marked as
1448 * used, at least on x86. Instead, reserve a memory region a
1449 * board would certainly use. If it works, go ahead. If not,
1450 * run like hell... */
1452 if (!request_mem_region (mem_addr, 16, depca_string))
1453 return unknown;
1455 /* Copy the first 16 bytes of ROM */
1457 ptr = ioremap(prom_addr, 16);
1458 if (ptr == NULL) {
1459 printk(KERN_ERR "depca: I/O remap failed at %lx\n", prom_addr);
1460 return unknown;
1462 for (i = 0; i < 16; i++) {
1463 tmpstr[i] = readb(ptr + i);
1465 iounmap(ptr);
1467 release_mem_region (mem_addr, 16);
1469 /* Check if PROM contains a valid string */
1470 for (i = 0; *depca_signature[i] != '\0'; i++) {
1471 for (j = 0, k = 0; j < 16 && k < strlen(depca_signature[i]); j++) {
1472 if (depca_signature[i][k] == tmpstr[j]) { /* track signature */
1473 k++;
1474 } else { /* lost signature; begin search again */
1475 k = 0;
1478 if (k == strlen(depca_signature[i]))
1479 break;
1482 /* Check if name string is valid, provided there's no PROM */
1483 if (name && *name && (i == unknown)) {
1484 for (i = 0; *depca_signature[i] != '\0'; i++) {
1485 if (strcmp(name, depca_signature[i]) == 0)
1486 break;
1490 return i;
1494 ** Look for a special sequence in the Ethernet station address PROM that
1495 ** is common across all DEPCA products. Note that the original DEPCA needs
1496 ** its ROM address counter to be initialized and enabled. Only enable
1497 ** if the first address octet is a 0x08 - this minimises the chances of
1498 ** messing around with some other hardware, but it assumes that this DEPCA
1499 ** card initialized itself correctly.
1501 ** Search the Ethernet address ROM for the signature. Since the ROM address
1502 ** counter can start at an arbitrary point, the search must include the entire
1503 ** probe sequence length plus the (length_of_the_signature - 1).
1504 ** Stop the search IMMEDIATELY after the signature is found so that the
1505 ** PROM address counter is correctly positioned at the start of the
1506 ** ethernet address for later read out.
1508 static int __init DevicePresent(u_long ioaddr)
1510 union {
1511 struct {
1512 u32 a;
1513 u32 b;
1514 } llsig;
1515 char Sig[sizeof(u32) << 1];
1517 dev;
1518 short sigLength = 0;
1519 s8 data;
1520 s16 nicsr;
1521 int i, j, status = 0;
1523 data = inb(DEPCA_PROM); /* clear counter on DEPCA */
1524 data = inb(DEPCA_PROM); /* read data */
1526 if (data == 0x08) { /* Enable counter on DEPCA */
1527 nicsr = inb(DEPCA_NICSR);
1528 nicsr |= AAC;
1529 outb(nicsr, DEPCA_NICSR);
1532 dev.llsig.a = ETH_PROM_SIG;
1533 dev.llsig.b = ETH_PROM_SIG;
1534 sigLength = sizeof(u32) << 1;
1536 for (i = 0, j = 0; j < sigLength && i < PROBE_LENGTH + sigLength - 1; i++) {
1537 data = inb(DEPCA_PROM);
1538 if (dev.Sig[j] == data) { /* track signature */
1539 j++;
1540 } else { /* lost signature; begin search again */
1541 if (data == dev.Sig[0]) { /* rare case.... */
1542 j = 1;
1543 } else {
1544 j = 0;
1549 if (j != sigLength) {
1550 status = -ENODEV; /* search failed */
1553 return status;
1557 ** The DE100 and DE101 PROM accesses were made non-standard for some bizarre
1558 ** reason: access the upper half of the PROM with x=0; access the lower half
1559 ** with x=1.
1561 static int __init get_hw_addr(struct net_device *dev)
1563 u_long ioaddr = dev->base_addr;
1564 struct depca_private *lp = netdev_priv(dev);
1565 int i, k, tmp, status = 0;
1566 u_short j, x, chksum;
1568 x = (((lp->adapter == de100) || (lp->adapter == de101)) ? 1 : 0);
1570 for (i = 0, k = 0, j = 0; j < 3; j++) {
1571 k <<= 1;
1572 if (k > 0xffff)
1573 k -= 0xffff;
1575 k += (u_char) (tmp = inb(DEPCA_PROM + x));
1576 dev->dev_addr[i++] = (u_char) tmp;
1577 k += (u_short) ((tmp = inb(DEPCA_PROM + x)) << 8);
1578 dev->dev_addr[i++] = (u_char) tmp;
1580 if (k > 0xffff)
1581 k -= 0xffff;
1583 if (k == 0xffff)
1584 k = 0;
1586 chksum = (u_char) inb(DEPCA_PROM + x);
1587 chksum |= (u_short) (inb(DEPCA_PROM + x) << 8);
1588 if (k != chksum)
1589 status = -1;
1591 return status;
1595 ** Load a packet into the shared memory
1597 static int load_packet(struct net_device *dev, struct sk_buff *skb)
1599 struct depca_private *lp = netdev_priv(dev);
1600 int i, entry, end, len, status = NETDEV_TX_OK;
1602 entry = lp->tx_new; /* Ring around buffer number. */
1603 end = (entry + (skb->len - 1) / TX_BUFF_SZ) & lp->txRingMask;
1604 if (!(readl(&lp->tx_ring[end].base) & T_OWN)) { /* Enough room? */
1606 ** Caution: the write order is important here... don't set up the
1607 ** ownership rights until all the other information is in place.
1609 if (end < entry) { /* wrapped buffer */
1610 len = (lp->txRingMask - entry + 1) * TX_BUFF_SZ;
1611 memcpy_toio(lp->tx_buff[entry], skb->data, len);
1612 memcpy_toio(lp->tx_buff[0], skb->data + len, skb->len - len);
1613 } else { /* linear buffer */
1614 memcpy_toio(lp->tx_buff[entry], skb->data, skb->len);
1617 /* set up the buffer descriptors */
1618 len = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
1619 for (i = entry; i != end; i = (i+1) & lp->txRingMask) {
1620 /* clean out flags */
1621 writel(readl(&lp->tx_ring[i].base) & ~T_FLAGS, &lp->tx_ring[i].base);
1622 writew(0x0000, &lp->tx_ring[i].misc); /* clears other error flags */
1623 writew(-TX_BUFF_SZ, &lp->tx_ring[i].length); /* packet length in buffer */
1624 len -= TX_BUFF_SZ;
1626 /* clean out flags */
1627 writel(readl(&lp->tx_ring[end].base) & ~T_FLAGS, &lp->tx_ring[end].base);
1628 writew(0x0000, &lp->tx_ring[end].misc); /* clears other error flags */
1629 writew(-len, &lp->tx_ring[end].length); /* packet length in last buff */
1631 /* start of packet */
1632 writel(readl(&lp->tx_ring[entry].base) | T_STP, &lp->tx_ring[entry].base);
1633 /* end of packet */
1634 writel(readl(&lp->tx_ring[end].base) | T_ENP, &lp->tx_ring[end].base);
1636 for (i = end; i != entry; --i) {
1637 /* ownership of packet */
1638 writel(readl(&lp->tx_ring[i].base) | T_OWN, &lp->tx_ring[i].base);
1639 if (i == 0)
1640 i = lp->txRingMask + 1;
1642 writel(readl(&lp->tx_ring[entry].base) | T_OWN, &lp->tx_ring[entry].base);
1644 lp->tx_new = (++end) & lp->txRingMask; /* update current pointers */
1645 } else {
1646 status = NETDEV_TX_LOCKED;
1649 return status;
1652 static void depca_dbg_open(struct net_device *dev)
1654 struct depca_private *lp = netdev_priv(dev);
1655 u_long ioaddr = dev->base_addr;
1656 struct depca_init *p = &lp->init_block;
1657 int i;
1659 if (depca_debug > 1) {
1660 /* Do not copy the shadow init block into shared memory */
1661 /* Debugging should not affect normal operation! */
1662 /* The shadow init block will get copied across during InitRestartDepca */
1663 printk("%s: depca open with irq %d\n", dev->name, dev->irq);
1664 printk("Descriptor head addresses (CPU):\n");
1665 printk(" 0x%lx 0x%lx\n", (u_long) lp->rx_ring, (u_long) lp->tx_ring);
1666 printk("Descriptor addresses (CPU):\nRX: ");
1667 for (i = 0; i < lp->rxRingMask; i++) {
1668 if (i < 3) {
1669 printk("%p ", &lp->rx_ring[i].base);
1672 printk("...%p\n", &lp->rx_ring[i].base);
1673 printk("TX: ");
1674 for (i = 0; i < lp->txRingMask; i++) {
1675 if (i < 3) {
1676 printk("%p ", &lp->tx_ring[i].base);
1679 printk("...%p\n", &lp->tx_ring[i].base);
1680 printk("\nDescriptor buffers (Device):\nRX: ");
1681 for (i = 0; i < lp->rxRingMask; i++) {
1682 if (i < 3) {
1683 printk("0x%8.8x ", readl(&lp->rx_ring[i].base));
1686 printk("...0x%8.8x\n", readl(&lp->rx_ring[i].base));
1687 printk("TX: ");
1688 for (i = 0; i < lp->txRingMask; i++) {
1689 if (i < 3) {
1690 printk("0x%8.8x ", readl(&lp->tx_ring[i].base));
1693 printk("...0x%8.8x\n", readl(&lp->tx_ring[i].base));
1694 printk("Initialisation block at 0x%8.8lx(Phys)\n", lp->mem_start);
1695 printk(" mode: 0x%4.4x\n", p->mode);
1696 printk(" physical address: %pM\n", p->phys_addr);
1697 printk(" multicast hash table: ");
1698 for (i = 0; i < (HASH_TABLE_LEN >> 3) - 1; i++) {
1699 printk("%2.2x:", p->mcast_table[i]);
1701 printk("%2.2x\n", p->mcast_table[i]);
1702 printk(" rx_ring at: 0x%8.8x\n", p->rx_ring);
1703 printk(" tx_ring at: 0x%8.8x\n", p->tx_ring);
1704 printk("buffers (Phys): 0x%8.8lx\n", lp->mem_start + lp->buffs_offset);
1705 printk("Ring size:\nRX: %d Log2(rxRingMask): 0x%8.8x\n", (int) lp->rxRingMask + 1, lp->rx_rlen);
1706 printk("TX: %d Log2(txRingMask): 0x%8.8x\n", (int) lp->txRingMask + 1, lp->tx_rlen);
1707 outw(CSR2, DEPCA_ADDR);
1708 printk("CSR2&1: 0x%4.4x", inw(DEPCA_DATA));
1709 outw(CSR1, DEPCA_ADDR);
1710 printk("%4.4x\n", inw(DEPCA_DATA));
1711 outw(CSR3, DEPCA_ADDR);
1712 printk("CSR3: 0x%4.4x\n", inw(DEPCA_DATA));
1717 ** Perform IOCTL call functions here. Some are privileged operations and the
1718 ** effective uid is checked in those cases.
1719 ** All multicast IOCTLs will not work here and are for testing purposes only.
1721 static int depca_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1723 struct depca_private *lp = netdev_priv(dev);
1724 struct depca_ioctl *ioc = (struct depca_ioctl *) &rq->ifr_ifru;
1725 int i, status = 0;
1726 u_long ioaddr = dev->base_addr;
1727 union {
1728 u8 addr[(HASH_TABLE_LEN * ETH_ALEN)];
1729 u16 sval[(HASH_TABLE_LEN * ETH_ALEN) >> 1];
1730 u32 lval[(HASH_TABLE_LEN * ETH_ALEN) >> 2];
1731 } tmp;
1732 unsigned long flags;
1733 void *buf;
1735 switch (ioc->cmd) {
1736 case DEPCA_GET_HWADDR: /* Get the hardware address */
1737 for (i = 0; i < ETH_ALEN; i++) {
1738 tmp.addr[i] = dev->dev_addr[i];
1740 ioc->len = ETH_ALEN;
1741 if (copy_to_user(ioc->data, tmp.addr, ioc->len))
1742 return -EFAULT;
1743 break;
1745 case DEPCA_SET_HWADDR: /* Set the hardware address */
1746 if (!capable(CAP_NET_ADMIN))
1747 return -EPERM;
1748 if (copy_from_user(tmp.addr, ioc->data, ETH_ALEN))
1749 return -EFAULT;
1750 for (i = 0; i < ETH_ALEN; i++) {
1751 dev->dev_addr[i] = tmp.addr[i];
1753 netif_stop_queue(dev);
1754 while (lp->tx_old != lp->tx_new)
1755 cpu_relax(); /* Wait for the ring to empty */
1757 STOP_DEPCA; /* Temporarily stop the depca. */
1758 depca_init_ring(dev); /* Initialize the descriptor rings */
1759 LoadCSRs(dev); /* Reload CSR3 */
1760 InitRestartDepca(dev); /* Resume normal operation. */
1761 netif_start_queue(dev); /* Unlock the TX ring */
1762 break;
1764 case DEPCA_SET_PROM: /* Set Promiscuous Mode */
1765 if (!capable(CAP_NET_ADMIN))
1766 return -EPERM;
1767 netif_stop_queue(dev);
1768 while (lp->tx_old != lp->tx_new)
1769 cpu_relax(); /* Wait for the ring to empty */
1771 STOP_DEPCA; /* Temporarily stop the depca. */
1772 depca_init_ring(dev); /* Initialize the descriptor rings */
1773 lp->init_block.mode |= PROM; /* Set promiscuous mode */
1775 LoadCSRs(dev); /* Reload CSR3 */
1776 InitRestartDepca(dev); /* Resume normal operation. */
1777 netif_start_queue(dev); /* Unlock the TX ring */
1778 break;
1780 case DEPCA_CLR_PROM: /* Clear Promiscuous Mode */
1781 if (!capable(CAP_NET_ADMIN))
1782 return -EPERM;
1783 netif_stop_queue(dev);
1784 while (lp->tx_old != lp->tx_new)
1785 cpu_relax(); /* Wait for the ring to empty */
1787 STOP_DEPCA; /* Temporarily stop the depca. */
1788 depca_init_ring(dev); /* Initialize the descriptor rings */
1789 lp->init_block.mode &= ~PROM; /* Clear promiscuous mode */
1791 LoadCSRs(dev); /* Reload CSR3 */
1792 InitRestartDepca(dev); /* Resume normal operation. */
1793 netif_start_queue(dev); /* Unlock the TX ring */
1794 break;
1796 case DEPCA_SAY_BOO: /* Say "Boo!" to the kernel log file */
1797 if(!capable(CAP_NET_ADMIN))
1798 return -EPERM;
1799 printk("%s: Boo!\n", dev->name);
1800 break;
1802 case DEPCA_GET_MCA: /* Get the multicast address table */
1803 ioc->len = (HASH_TABLE_LEN >> 3);
1804 if (copy_to_user(ioc->data, lp->init_block.mcast_table, ioc->len))
1805 return -EFAULT;
1806 break;
1808 case DEPCA_SET_MCA: /* Set a multicast address */
1809 if (!capable(CAP_NET_ADMIN))
1810 return -EPERM;
1811 if (ioc->len >= HASH_TABLE_LEN)
1812 return -EINVAL;
1813 if (copy_from_user(tmp.addr, ioc->data, ETH_ALEN * ioc->len))
1814 return -EFAULT;
1815 set_multicast_list(dev);
1816 break;
1818 case DEPCA_CLR_MCA: /* Clear all multicast addresses */
1819 if (!capable(CAP_NET_ADMIN))
1820 return -EPERM;
1821 set_multicast_list(dev);
1822 break;
1824 case DEPCA_MCA_EN: /* Enable pass all multicast addressing */
1825 if (!capable(CAP_NET_ADMIN))
1826 return -EPERM;
1827 set_multicast_list(dev);
1828 break;
1830 case DEPCA_GET_STATS: /* Get the driver statistics */
1831 ioc->len = sizeof(lp->pktStats);
1832 buf = kmalloc(ioc->len, GFP_KERNEL);
1833 if(!buf)
1834 return -ENOMEM;
1835 spin_lock_irqsave(&lp->lock, flags);
1836 memcpy(buf, &lp->pktStats, ioc->len);
1837 spin_unlock_irqrestore(&lp->lock, flags);
1838 if (copy_to_user(ioc->data, buf, ioc->len))
1839 status = -EFAULT;
1840 kfree(buf);
1841 break;
1843 case DEPCA_CLR_STATS: /* Zero out the driver statistics */
1844 if (!capable(CAP_NET_ADMIN))
1845 return -EPERM;
1846 spin_lock_irqsave(&lp->lock, flags);
1847 memset(&lp->pktStats, 0, sizeof(lp->pktStats));
1848 spin_unlock_irqrestore(&lp->lock, flags);
1849 break;
1851 case DEPCA_GET_REG: /* Get the DEPCA Registers */
1852 i = 0;
1853 tmp.sval[i++] = inw(DEPCA_NICSR);
1854 outw(CSR0, DEPCA_ADDR); /* status register */
1855 tmp.sval[i++] = inw(DEPCA_DATA);
1856 memcpy(&tmp.sval[i], &lp->init_block, sizeof(struct depca_init));
1857 ioc->len = i + sizeof(struct depca_init);
1858 if (copy_to_user(ioc->data, tmp.addr, ioc->len))
1859 return -EFAULT;
1860 break;
1862 default:
1863 return -EOPNOTSUPP;
1866 return status;
1869 static int __init depca_module_init (void)
1871 int err = 0;
1873 #ifdef CONFIG_EISA
1874 err = eisa_driver_register(&depca_eisa_driver);
1875 if (err)
1876 goto err_eisa;
1877 #endif
1878 err = platform_driver_register(&depca_isa_driver);
1879 if (err)
1880 goto err_eisa;
1882 depca_platform_probe();
1883 return 0;
1885 err_eisa:
1886 #ifdef CONFIG_EISA
1887 eisa_driver_unregister(&depca_eisa_driver);
1888 #endif
1889 return err;
1892 static void __exit depca_module_exit (void)
1894 int i;
1895 #ifdef CONFIG_EISA
1896 eisa_driver_unregister (&depca_eisa_driver);
1897 #endif
1898 platform_driver_unregister (&depca_isa_driver);
1900 for (i = 0; depca_io_ports[i].iobase; i++) {
1901 if (depca_io_ports[i].device) {
1902 depca_io_ports[i].device->dev.platform_data = NULL;
1903 platform_device_unregister (depca_io_ports[i].device);
1904 depca_io_ports[i].device = NULL;
1909 module_init (depca_module_init);
1910 module_exit (depca_module_exit);