blob: b2b0a96218ca9a339e11500f7da6c2a9cc732a7a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* e2100.c: A Cabletron E2100 series ethernet driver for linux. */
2/*
3 Written 1993-1994 by Donald Becker.
4
5 Copyright 1994 by Donald Becker.
6 Copyright 1993 United States Government as represented by the
7 Director, National Security Agency. This software may be used and
8 distributed according to the terms of the GNU General Public License,
9 incorporated herein by reference.
10
11 This is a driver for the Cabletron E2100 series ethercards.
12
13 The Author may be reached as becker@scyld.com, or C/O
14 Scyld Computing Corporation
15 410 Severn Ave., Suite 210
16 Annapolis MD 21403
17
18 The E2100 series ethercard is a fairly generic shared memory 8390
19 implementation. The only unusual aspect is the way the shared memory
20 registers are set: first you do an inb() in what is normally the
21 station address region, and the low three bits of next outb() *address*
22 is used as the write value for that register. Either someone wasn't
23 too used to dem bit en bites, or they were trying to obfuscate the
24 programming interface.
25
26 There is an additional complication when setting the window on the packet
27 buffer. You must first do a read into the packet buffer region with the
28 low 8 address bits the address setting the page for the start of the packet
29 buffer window, and then do the above operation. See mem_on() for details.
30
31 One bug on the chip is that even a hard reset won't disable the memory
32 window, usually resulting in a hung machine if mem_off() isn't called.
33 If this happens, you must power down the machine for about 30 seconds.
34*/
35
36static const char version[] =
37 "e2100.c:v1.01 7/21/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
38
39#include <linux/module.h>
40#include <linux/kernel.h>
41#include <linux/errno.h>
42#include <linux/string.h>
43#include <linux/ioport.h>
44#include <linux/netdevice.h>
45#include <linux/etherdevice.h>
46#include <linux/init.h>
47#include <linux/delay.h>
48
49#include <asm/io.h>
50#include <asm/system.h>
51
52#include "8390.h"
53
54#define DRV_NAME "e2100"
55
56static int e21_probe_list[] = {0x300, 0x280, 0x380, 0x220, 0};
57
58/* Offsets from the base_addr.
59 Read from the ASIC register, and the low three bits of the next outb()
60 address is used to set the corresponding register. */
61#define E21_NIC_OFFSET 0 /* Offset to the 8390 NIC. */
62#define E21_ASIC 0x10
63#define E21_MEM_ENABLE 0x10
64#define E21_MEM_ON 0x05 /* Enable memory in 16 bit mode. */
65#define E21_MEM_ON_8 0x07 /* Enable memory in 8 bit mode. */
66#define E21_MEM_BASE 0x11
67#define E21_IRQ_LOW 0x12 /* The low three bits of the IRQ number. */
68#define E21_IRQ_HIGH 0x14 /* The high IRQ bit and media select ... */
69#define E21_MEDIA 0x14 /* (alias). */
70#define E21_ALT_IFPORT 0x02 /* Set to use the other (BNC,AUI) port. */
71#define E21_BIG_MEM 0x04 /* Use a bigger (64K) buffer (we don't) */
72#define E21_SAPROM 0x10 /* Offset to station address data. */
73#define E21_IO_EXTENT 0x20
74
75static inline void mem_on(short port, volatile char __iomem *mem_base,
76 unsigned char start_page )
77{
78 /* This is a little weird: set the shared memory window by doing a
79 read. The low address bits specify the starting page. */
80 readb(mem_base+start_page);
81 inb(port + E21_MEM_ENABLE);
82 outb(E21_MEM_ON, port + E21_MEM_ENABLE + E21_MEM_ON);
83}
84
85static inline void mem_off(short port)
86{
87 inb(port + E21_MEM_ENABLE);
88 outb(0x00, port + E21_MEM_ENABLE);
89}
90
91/* In other drivers I put the TX pages first, but the E2100 window circuitry
92 is designed to have a 4K Tx region last. The windowing circuitry wraps the
93 window at 0x2fff->0x0000 so that the packets at e.g. 0x2f00 in the RX ring
94 appear contiguously in the window. */
95#define E21_RX_START_PG 0x00 /* First page of RX buffer */
96#define E21_RX_STOP_PG 0x30 /* Last page +1 of RX ring */
97#define E21_BIG_RX_STOP_PG 0xF0 /* Last page +1 of RX ring */
98#define E21_TX_START_PG E21_RX_STOP_PG /* First page of TX buffer */
99
100static int e21_probe1(struct net_device *dev, int ioaddr);
101
102static int e21_open(struct net_device *dev);
103static void e21_reset_8390(struct net_device *dev);
104static void e21_block_input(struct net_device *dev, int count,
105 struct sk_buff *skb, int ring_offset);
106static void e21_block_output(struct net_device *dev, int count,
107 const unsigned char *buf, int start_page);
108static void e21_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
109 int ring_page);
110
111static int e21_close(struct net_device *dev);
112
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/* Probe for the E2100 series ethercards. These cards have an 8390 at the
115 base address and the station address at both offset 0x10 and 0x18. I read
116 the station address from offset 0x18 to avoid the dataport of NE2000
117 ethercards, and look for Ctron's unique ID (first three octets of the
118 station address).
119 */
120
121static int __init do_e2100_probe(struct net_device *dev)
122{
123 int *port;
124 int base_addr = dev->base_addr;
125 int irq = dev->irq;
126
127 SET_MODULE_OWNER(dev);
128
129 if (base_addr > 0x1ff) /* Check a single specified location. */
130 return e21_probe1(dev, base_addr);
131 else if (base_addr != 0) /* Don't probe at all. */
132 return -ENXIO;
133
134 for (port = e21_probe_list; *port; port++) {
135 dev->irq = irq;
136 if (e21_probe1(dev, *port) == 0)
137 return 0;
138 }
139
140 return -ENODEV;
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#ifndef MODULE
144struct net_device * __init e2100_probe(int unit)
145{
146 struct net_device *dev = alloc_ei_netdev();
147 int err;
148
149 if (!dev)
150 return ERR_PTR(-ENOMEM);
151
152 sprintf(dev->name, "eth%d", unit);
153 netdev_boot_setup_check(dev);
154
155 err = do_e2100_probe(dev);
156 if (err)
157 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159out:
160 free_netdev(dev);
161 return ERR_PTR(err);
162}
163#endif
164
165static int __init e21_probe1(struct net_device *dev, int ioaddr)
166{
167 int i, status, retval;
168 unsigned char *station_addr = dev->dev_addr;
169 static unsigned version_printed;
170
171 if (!request_region(ioaddr, E21_IO_EXTENT, DRV_NAME))
172 return -EBUSY;
173
174 /* First check the station address for the Ctron prefix. */
175 if (inb(ioaddr + E21_SAPROM + 0) != 0x00
176 || inb(ioaddr + E21_SAPROM + 1) != 0x00
177 || inb(ioaddr + E21_SAPROM + 2) != 0x1d) {
178 retval = -ENODEV;
179 goto out;
180 }
181
182 /* Verify by making certain that there is a 8390 at there. */
183 outb(E8390_NODMA + E8390_STOP, ioaddr);
184 udelay(1); /* we want to delay one I/O cycle - which is 2MHz */
185 status = inb(ioaddr);
186 if (status != 0x21 && status != 0x23) {
187 retval = -ENODEV;
188 goto out;
189 }
190
191 /* Read the station address PROM. */
192 for (i = 0; i < 6; i++)
193 station_addr[i] = inb(ioaddr + E21_SAPROM + i);
194
195 inb(ioaddr + E21_MEDIA); /* Point to media selection. */
196 outb(0, ioaddr + E21_ASIC); /* and disable the secondary interface. */
197
198 if (ei_debug && version_printed++ == 0)
199 printk(version);
200
201 for (i = 0; i < 6; i++)
202 printk(" %02X", station_addr[i]);
203
204 if (dev->irq < 2) {
205 int irqlist[] = {15,11,10,12,5,9,3,4}, i;
206 for (i = 0; i < 8; i++)
207 if (request_irq (irqlist[i], NULL, 0, "bogus", NULL) != -EBUSY) {
208 dev->irq = irqlist[i];
209 break;
210 }
211 if (i >= 8) {
212 printk(" unable to get IRQ %d.\n", dev->irq);
213 retval = -EAGAIN;
214 goto out;
215 }
216 } else if (dev->irq == 2) /* Fixup luser bogosity: IRQ2 is really IRQ9 */
217 dev->irq = 9;
218
219 /* The 8390 is at the base address. */
220 dev->base_addr = ioaddr;
221
222 ei_status.name = "E2100";
223 ei_status.word16 = 1;
224 ei_status.tx_start_page = E21_TX_START_PG;
225 ei_status.rx_start_page = E21_RX_START_PG;
226 ei_status.stop_page = E21_RX_STOP_PG;
227 ei_status.saved_irq = dev->irq;
228
229 /* Check the media port used. The port can be passed in on the
230 low mem_end bits. */
231 if (dev->mem_end & 15)
232 dev->if_port = dev->mem_end & 7;
233 else {
234 dev->if_port = 0;
235 inb(ioaddr + E21_MEDIA); /* Turn automatic media detection on. */
236 for(i = 0; i < 6; i++)
237 if (station_addr[i] != inb(ioaddr + E21_SAPROM + 8 + i)) {
238 dev->if_port = 1;
239 break;
240 }
241 }
242
243 /* Never map in the E21 shared memory unless you are actively using it.
244 Also, the shared memory has effective only one setting -- spread all
245 over the 128K region! */
246 if (dev->mem_start == 0)
247 dev->mem_start = 0xd0000;
248
249 ei_status.mem = ioremap(dev->mem_start, 2*1024);
250 if (!ei_status.mem) {
251 printk("unable to remap memory\n");
252 retval = -EAGAIN;
253 goto out;
254 }
255
256#ifdef notdef
257 /* These values are unused. The E2100 has a 2K window into the packet
258 buffer. The window can be set to start on any page boundary. */
259 ei_status.rmem_start = dev->mem_start + TX_PAGES*256;
260 dev->mem_end = ei_status.rmem_end = dev->mem_start + 2*1024;
261#endif
262
263 printk(", IRQ %d, %s media, memory @ %#lx.\n", dev->irq,
264 dev->if_port ? "secondary" : "primary", dev->mem_start);
265
266 ei_status.reset_8390 = &e21_reset_8390;
267 ei_status.block_input = &e21_block_input;
268 ei_status.block_output = &e21_block_output;
269 ei_status.get_8390_hdr = &e21_get_8390_hdr;
270 dev->open = &e21_open;
271 dev->stop = &e21_close;
272#ifdef CONFIG_NET_POLL_CONTROLLER
273 dev->poll_controller = ei_poll;
274#endif
275 NS8390_init(dev, 0);
276
b1fc5502005-05-12 20:11:55 -0400277 retval = register_netdev(dev);
278 if (retval)
279 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return 0;
281out:
282 release_region(ioaddr, E21_IO_EXTENT);
283 return retval;
284}
285
286static int
287e21_open(struct net_device *dev)
288{
289 short ioaddr = dev->base_addr;
290 int retval;
291
292 if ((retval = request_irq(dev->irq, ei_interrupt, 0, dev->name, dev)))
293 return retval;
294
295 /* Set the interrupt line and memory base on the hardware. */
296 inb(ioaddr + E21_IRQ_LOW);
297 outb(0, ioaddr + E21_ASIC + (dev->irq & 7));
298 inb(ioaddr + E21_IRQ_HIGH); /* High IRQ bit, and if_port. */
299 outb(0, ioaddr + E21_ASIC + (dev->irq > 7 ? 1:0)
300 + (dev->if_port ? E21_ALT_IFPORT : 0));
301 inb(ioaddr + E21_MEM_BASE);
302 outb(0, ioaddr + E21_ASIC + ((dev->mem_start >> 17) & 7));
303
304 ei_open(dev);
305 return 0;
306}
307
308static void
309e21_reset_8390(struct net_device *dev)
310{
311 short ioaddr = dev->base_addr;
312
313 outb(0x01, ioaddr);
314 if (ei_debug > 1) printk("resetting the E2180x3 t=%ld...", jiffies);
315 ei_status.txing = 0;
316
317 /* Set up the ASIC registers, just in case something changed them. */
318
319 if (ei_debug > 1) printk("reset done\n");
320 return;
321}
322
323/* Grab the 8390 specific header. We put the 2k window so the header page
324 appears at the start of the shared memory. */
325
326static void
327e21_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
328{
329
330 short ioaddr = dev->base_addr;
331 char __iomem *shared_mem = ei_status.mem;
332
333 mem_on(ioaddr, shared_mem, ring_page);
334
335#ifdef notdef
336 /* Officially this is what we are doing, but the readl() is faster */
337 memcpy_fromio(hdr, shared_mem, sizeof(struct e8390_pkt_hdr));
338#else
339 ((unsigned int*)hdr)[0] = readl(shared_mem);
340#endif
341
342 /* Turn off memory access: we would need to reprogram the window anyway. */
343 mem_off(ioaddr);
344
345}
346
347/* Block input and output are easy on shared memory ethercards.
348 The E21xx makes block_input() especially easy by wrapping the top
349 ring buffer to the bottom automatically. */
350static void
351e21_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
352{
353 short ioaddr = dev->base_addr;
354 char __iomem *shared_mem = ei_status.mem;
355
356 mem_on(ioaddr, shared_mem, (ring_offset>>8));
357
Al Viro4ec03112007-02-09 16:38:30 +0000358 memcpy_fromio(skb->data, ei_status.mem + (ring_offset & 0xff), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 mem_off(ioaddr);
361}
362
363static void
364e21_block_output(struct net_device *dev, int count, const unsigned char *buf,
365 int start_page)
366{
367 short ioaddr = dev->base_addr;
368 volatile char __iomem *shared_mem = ei_status.mem;
369
370 /* Set the shared memory window start by doing a read, with the low address
371 bits specifying the starting page. */
372 readb(shared_mem + start_page);
373 mem_on(ioaddr, shared_mem, start_page);
374
375 memcpy_toio(shared_mem, buf, count);
376 mem_off(ioaddr);
377}
378
379static int
380e21_close(struct net_device *dev)
381{
382 short ioaddr = dev->base_addr;
383
384 if (ei_debug > 1)
385 printk("%s: Shutting down ethercard.\n", dev->name);
386
387 free_irq(dev->irq, dev);
388 dev->irq = ei_status.saved_irq;
389
390 /* Shut off the interrupt line and secondary interface. */
391 inb(ioaddr + E21_IRQ_LOW);
392 outb(0, ioaddr + E21_ASIC);
393 inb(ioaddr + E21_IRQ_HIGH); /* High IRQ bit, and if_port. */
394 outb(0, ioaddr + E21_ASIC);
395
396 ei_close(dev);
397
398 /* Double-check that the memory has been turned off, because really
399 really bad things happen if it isn't. */
400 mem_off(ioaddr);
401
402 return 0;
403}
404
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406#ifdef MODULE
407#define MAX_E21_CARDS 4 /* Max number of E21 cards per module */
408static struct net_device *dev_e21[MAX_E21_CARDS];
409static int io[MAX_E21_CARDS];
410static int irq[MAX_E21_CARDS];
411static int mem[MAX_E21_CARDS];
412static int xcvr[MAX_E21_CARDS]; /* choose int. or ext. xcvr */
413
414module_param_array(io, int, NULL, 0);
415module_param_array(irq, int, NULL, 0);
416module_param_array(mem, int, NULL, 0);
417module_param_array(xcvr, int, NULL, 0);
418MODULE_PARM_DESC(io, "I/O base address(es)");
419MODULE_PARM_DESC(irq, "IRQ number(s)");
420MODULE_PARM_DESC(mem, " memory base address(es)");
421MODULE_PARM_DESC(xcvr, "transceiver(s) (0=internal, 1=external)");
422MODULE_DESCRIPTION("Cabletron E2100 ISA ethernet driver");
423MODULE_LICENSE("GPL");
424
425/* This is set up so that only a single autoprobe takes place per call.
426ISA device autoprobes on a running machine are not recommended. */
Andrew Mortond543cbb2006-08-14 23:00:00 -0700427
428int __init init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 struct net_device *dev;
431 int this_dev, found = 0;
432
433 for (this_dev = 0; this_dev < MAX_E21_CARDS; this_dev++) {
434 if (io[this_dev] == 0) {
435 if (this_dev != 0) break; /* only autoprobe 1st one */
436 printk(KERN_NOTICE "e2100.c: Presently autoprobing (not recommended) for a single card.\n");
437 }
438 dev = alloc_ei_netdev();
439 if (!dev)
440 break;
441 dev->irq = irq[this_dev];
442 dev->base_addr = io[this_dev];
443 dev->mem_start = mem[this_dev];
444 dev->mem_end = xcvr[this_dev]; /* low 4bits = xcvr sel. */
445 if (do_e2100_probe(dev) == 0) {
b1fc5502005-05-12 20:11:55 -0400446 dev_e21[found++] = dev;
447 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449 free_netdev(dev);
450 printk(KERN_WARNING "e2100.c: No E2100 card found (i/o = 0x%x).\n", io[this_dev]);
451 break;
452 }
453 if (found)
454 return 0;
455 return -ENXIO;
456}
457
Denis Vlasenko64916f12006-01-05 22:45:47 -0800458static void cleanup_card(struct net_device *dev)
459{
460 /* NB: e21_close() handles free_irq */
461 iounmap(ei_status.mem);
462 release_region(dev->base_addr, E21_IO_EXTENT);
463}
464
Al Viroafc8eb42006-06-14 18:50:53 -0400465void __exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466cleanup_module(void)
467{
468 int this_dev;
469
470 for (this_dev = 0; this_dev < MAX_E21_CARDS; this_dev++) {
471 struct net_device *dev = dev_e21[this_dev];
472 if (dev) {
473 unregister_netdev(dev);
474 cleanup_card(dev);
475 free_netdev(dev);
476 }
477 }
478}
479#endif /* MODULE */