1 /*
2 	lne390.c
3 
4 	Linux driver for Mylex LNE390 EISA Network Adapter
5 
6 	Copyright (C) 1996-1998, Paul Gortmaker.
7 
8 	This software may be used and distributed according to the terms
9 	of the GNU General Public License, incorporated herein by reference.
10 
11 	Information and Code Sources:
12 
13 	1) Based upon framework of es3210 driver.
14 	2) The existing myriad of other Linux 8390 drivers by Donald Becker.
15 	3) Russ Nelson's asm packet driver provided additional info.
16 	4) Info for getting IRQ and sh-mem gleaned from the EISA cfg files.
17 
18 	The LNE390 is an EISA shared memory NS8390 implementation. Note
19 	that all memory copies to/from the board must be 32bit transfers.
20 	There are two versions of the card: the lne390a and the lne390b.
21 	Going by the EISA cfg files, the "a" has jumpers to select between
22 	BNC/AUI, but the "b" also has RJ-45 and selection is via the SCU.
23 	The shared memory address selection is also slightly different.
24 	Note that shared memory address > 1MB are supported with this driver.
25 
26 	You can try <http://www.mylex.com> if you want more info, as I've
27 	never even seen one of these cards.  :)
28 
29 	Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 2000/09/01
30 	- get rid of check_region
31 	- no need to check if dev == NULL in lne390_probe1
32 */
33 
34 static const char *version =
35 	"lne390.c: Driver revision v0.99.1, 01/09/2000\n";
36 
37 #include <linux/module.h>
38 #include <linux/kernel.h>
39 #include <linux/sched.h>
40 #include <linux/errno.h>
41 #include <linux/string.h>
42 #include <linux/delay.h>
43 #include <linux/init.h>
44 
45 #include <asm/io.h>
46 #include <asm/system.h>
47 
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include "8390.h"
51 
52 int lne390_probe(struct net_device *dev);
53 static int lne390_probe1(struct net_device *dev, int ioaddr);
54 
55 static int lne390_open(struct net_device *dev);
56 static int lne390_close(struct net_device *dev);
57 
58 static void lne390_reset_8390(struct net_device *dev);
59 
60 static void lne390_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page);
61 static void lne390_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset);
62 static void lne390_block_output(struct net_device *dev, int count, const unsigned char *buf, const int start_page);
63 
64 #define LNE390_START_PG		0x00    /* First page of TX buffer	*/
65 #define LNE390_STOP_PG		0x80    /* Last page +1 of RX ring	*/
66 
67 #define LNE390_ID_PORT		0xc80	/* Same for all EISA cards 	*/
68 #define LNE390_IO_EXTENT	0x20
69 #define LNE390_SA_PROM		0x16	/* Start of e'net addr.		*/
70 #define LNE390_RESET_PORT	0xc84	/* From the pkt driver source	*/
71 #define LNE390_NIC_OFFSET	0x00	/* Hello, the 8390 is *here*	*/
72 
73 #define LNE390_ADDR0		0x00	/* 3 byte vendor prefix		*/
74 #define LNE390_ADDR1		0x80
75 #define LNE390_ADDR2		0xe5
76 
77 #define LNE390_ID0	0x10009835	/* 0x3598 = 01101 01100 11000 = mlx */
78 #define LNE390_ID1	0x11009835	/* above is the 390A, this is 390B  */
79 
80 #define LNE390_CFG1		0xc84	/* NB: 0xc84 is also "reset" port. */
81 #define LNE390_CFG2		0xc90
82 
83 /*
84  *	You can OR any of the following bits together and assign it
85  *	to LNE390_DEBUG to get verbose driver info during operation.
86  *	Currently only the probe one is implemented.
87  */
88 
89 #define LNE390_D_PROBE	0x01
90 #define LNE390_D_RX_PKT	0x02
91 #define LNE390_D_TX_PKT	0x04
92 #define LNE390_D_IRQ	0x08
93 
94 #define LNE390_DEBUG	0
95 
96 static unsigned char irq_map[] __initdata = {15, 12, 11, 10, 9, 7, 5, 3};
97 static unsigned int shmem_mapA[] __initdata = {0xff, 0xfe, 0xfd, 0xfff, 0xffe, 0xffc, 0x0d, 0x0};
98 static unsigned int shmem_mapB[] __initdata = {0xff, 0xfe, 0x0e, 0xfff, 0xffe, 0xffc, 0x0d, 0x0};
99 
100 /*
101  *	Probe for the card. The best way is to read the EISA ID if it
102  *	is known. Then we can check the prefix of the station address
103  *	PROM for a match against the value assigned to Mylex.
104  */
105 
lne390_probe(struct net_device * dev)106 int __init lne390_probe(struct net_device *dev)
107 {
108 	unsigned short ioaddr = dev->base_addr;
109 	int ret;
110 
111 	SET_MODULE_OWNER(dev);
112 
113 	if (ioaddr > 0x1ff) {		/* Check a single specified location. */
114 		if (!request_region(ioaddr, LNE390_IO_EXTENT, dev->name))
115 			return -EBUSY;
116 		ret = lne390_probe1(dev, ioaddr);
117 		if (ret)
118 			release_region(ioaddr, LNE390_IO_EXTENT);
119 		return ret;
120 	}
121 	else if (ioaddr > 0)		/* Don't probe at all. */
122 		return -ENXIO;
123 
124 	if (!EISA_bus) {
125 #if LNE390_DEBUG & LNE390_D_PROBE
126 		printk("lne390-debug: Not an EISA bus. Not probing high ports.\n");
127 #endif
128 		return -ENXIO;
129 	}
130 
131 	/* EISA spec allows for up to 16 slots, but 8 is typical. */
132 	for (ioaddr = 0x1000; ioaddr < 0x9000; ioaddr += 0x1000) {
133 		if (!request_region(ioaddr, LNE390_IO_EXTENT, dev->name))
134 			continue;
135 		if (lne390_probe1(dev, ioaddr) == 0)
136 			return 0;
137 		release_region(ioaddr, LNE390_IO_EXTENT);
138 	}
139 
140 	return -ENODEV;
141 }
142 
lne390_probe1(struct net_device * dev,int ioaddr)143 static int __init lne390_probe1(struct net_device *dev, int ioaddr)
144 {
145 	int i, revision, ret;
146 	unsigned long eisa_id;
147 
148 	if (inb_p(ioaddr + LNE390_ID_PORT) == 0xff) return -ENODEV;
149 
150 #if LNE390_DEBUG & LNE390_D_PROBE
151 	printk("lne390-debug: probe at %#x, ID %#8x\n", ioaddr, inl(ioaddr + LNE390_ID_PORT));
152 	printk("lne390-debug: config regs: %#x %#x\n",
153 		inb(ioaddr + LNE390_CFG1), inb(ioaddr + LNE390_CFG2));
154 #endif
155 
156 
157 /*	Check the EISA ID of the card. */
158 	eisa_id = inl(ioaddr + LNE390_ID_PORT);
159 	if ((eisa_id != LNE390_ID0) && (eisa_id != LNE390_ID1)) {
160 		return -ENODEV;
161 	}
162 
163 	revision = (eisa_id >> 24) & 0x01;	/* 0 = rev A, 1 rev B */
164 
165 #if 0
166 /*	Check the Mylex vendor ID as well. Not really required. */
167 	if (inb(ioaddr + LNE390_SA_PROM + 0) != LNE390_ADDR0
168 		|| inb(ioaddr + LNE390_SA_PROM + 1) != LNE390_ADDR1
169 		|| inb(ioaddr + LNE390_SA_PROM + 2) != LNE390_ADDR2 ) {
170 		printk("lne390.c: card not found");
171 		for(i = 0; i < ETHER_ADDR_LEN; i++)
172 			printk(" %02x", inb(ioaddr + LNE390_SA_PROM + i));
173 		printk(" (invalid prefix).\n");
174 		return -ENODEV;
175 	}
176 #endif
177 	/* Allocate dev->priv and fill in 8390 specific dev fields. */
178 	if (ethdev_init(dev)) {
179 		printk ("lne390.c: unable to allocate memory for dev->priv!\n");
180 		return -ENOMEM;
181 	}
182 
183 	printk("lne390.c: LNE390%X in EISA slot %d, address", 0xa+revision, ioaddr/0x1000);
184 	for(i = 0; i < ETHER_ADDR_LEN; i++)
185 		printk(" %02x", (dev->dev_addr[i] = inb(ioaddr + LNE390_SA_PROM + i)));
186 	printk(".\nlne390.c: ");
187 
188 	/* Snarf the interrupt now. CFG file has them all listed as `edge' with share=NO */
189 	if (dev->irq == 0) {
190 		unsigned char irq_reg = inb(ioaddr + LNE390_CFG2) >> 3;
191 		dev->irq = irq_map[irq_reg & 0x07];
192 		printk("using");
193 	} else {
194 		/* This is useless unless we reprogram the card here too */
195 		if (dev->irq == 2) dev->irq = 9;	/* Doh! */
196 		printk("assigning");
197 	}
198 	printk(" IRQ %d,", dev->irq);
199 
200 	if ((ret = request_irq(dev->irq, ei_interrupt, 0, dev->name, dev))) {
201 		printk (" unable to get IRQ %d.\n", dev->irq);
202 		kfree(dev->priv);
203 		dev->priv = NULL;
204 		return ret;
205 	}
206 
207 	if (dev->mem_start == 0) {
208 		unsigned char mem_reg = inb(ioaddr + LNE390_CFG2) & 0x07;
209 
210 		if (revision)	/* LNE390B */
211 			dev->mem_start = shmem_mapB[mem_reg] * 0x10000;
212 		else		/* LNE390A */
213 			dev->mem_start = shmem_mapA[mem_reg] * 0x10000;
214 		printk(" using ");
215 	} else {
216 		/* Should check for value in shmem_map and reprogram the card to use it */
217 		dev->mem_start &= 0xfff0000;
218 		printk(" assigning ");
219 	}
220 
221 	printk("%dkB memory at physical address %#lx\n",
222 			LNE390_STOP_PG/4, dev->mem_start);
223 
224 	/*
225 	   BEWARE!! Some dain-bramaged EISA SCUs will allow you to put
226 	   the card mem within the region covered by `normal' RAM  !!!
227 	*/
228 	if (dev->mem_start > 1024*1024) {	/* phys addr > 1MB */
229 		if (dev->mem_start < virt_to_bus(high_memory)) {
230 			printk(KERN_CRIT "lne390.c: Card RAM overlaps with normal memory!!!\n");
231 			printk(KERN_CRIT "lne390.c: Use EISA SCU to set card memory below 1MB,\n");
232 			printk(KERN_CRIT "lne390.c: or to an address above 0x%lx.\n", virt_to_bus(high_memory));
233 			printk(KERN_CRIT "lne390.c: Driver NOT installed.\n");
234 			ret = -EINVAL;
235 			goto cleanup;
236 		}
237 		dev->mem_start = (unsigned long)ioremap(dev->mem_start, LNE390_STOP_PG*0x100);
238 		if (dev->mem_start == 0) {
239 			printk(KERN_ERR "lne390.c: Unable to remap card memory above 1MB !!\n");
240 			printk(KERN_ERR "lne390.c: Try using EISA SCU to set memory below 1MB.\n");
241 			printk(KERN_ERR "lne390.c: Driver NOT installed.\n");
242 			ret = -EAGAIN;
243 			goto cleanup;
244 		}
245 		ei_status.reg0 = 1;	/* Use as remap flag */
246 		printk("lne390.c: remapped %dkB card memory to virtual address %#lx\n",
247 				LNE390_STOP_PG/4, dev->mem_start);
248 	}
249 
250 	dev->mem_end = dev->rmem_end = dev->mem_start
251 		+ (LNE390_STOP_PG - LNE390_START_PG)*256;
252 	dev->rmem_start = dev->mem_start + TX_PAGES*256;
253 
254 	/* The 8390 offset is zero for the LNE390 */
255 	dev->base_addr = ioaddr;
256 
257 	ei_status.name = "LNE390";
258 	ei_status.tx_start_page = LNE390_START_PG;
259 	ei_status.rx_start_page = LNE390_START_PG + TX_PAGES;
260 	ei_status.stop_page = LNE390_STOP_PG;
261 	ei_status.word16 = 1;
262 
263 	if (ei_debug > 0)
264 		printk(version);
265 
266 	ei_status.reset_8390 = &lne390_reset_8390;
267 	ei_status.block_input = &lne390_block_input;
268 	ei_status.block_output = &lne390_block_output;
269 	ei_status.get_8390_hdr = &lne390_get_8390_hdr;
270 
271 	dev->open = &lne390_open;
272 	dev->stop = &lne390_close;
273 	NS8390_init(dev, 0);
274 	return 0;
275 cleanup:
276 	free_irq(dev->irq, dev);
277 	kfree(dev->priv);
278 	dev->priv = NULL;
279 	return ret;
280 }
281 
282 /*
283  *	Reset as per the packet driver method. Judging by the EISA cfg
284  *	file, this just toggles the "Board Enable" bits (bit 2 and 0).
285  */
286 
lne390_reset_8390(struct net_device * dev)287 static void lne390_reset_8390(struct net_device *dev)
288 {
289 	unsigned short ioaddr = dev->base_addr;
290 
291 	outb(0x04, ioaddr + LNE390_RESET_PORT);
292 	if (ei_debug > 1) printk("%s: resetting the LNE390...", dev->name);
293 
294 	mdelay(2);
295 
296 	ei_status.txing = 0;
297 	outb(0x01, ioaddr + LNE390_RESET_PORT);
298 	if (ei_debug > 1) printk("reset done\n");
299 
300 	return;
301 }
302 
303 /*
304  *	Note: In the following three functions is the implicit assumption
305  *	that the associated memcpy will only use "rep; movsl" as long as
306  *	we keep the counts as some multiple of doublewords. This is a
307  *	requirement of the hardware, and also prevents us from using
308  *	eth_io_copy_and_sum() since we can't guarantee it will limit
309  *	itself to doubleword access.
310  */
311 
312 /*
313  *	Grab the 8390 specific header. Similar to the block_input routine, but
314  *	we don't need to be concerned with ring wrap as the header will be at
315  *	the start of a page, so we optimize accordingly. (A single doubleword.)
316  */
317 
318 static void
lne390_get_8390_hdr(struct net_device * dev,struct e8390_pkt_hdr * hdr,int ring_page)319 lne390_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
320 {
321 	unsigned long hdr_start = dev->mem_start + ((ring_page - LNE390_START_PG)<<8);
322 	isa_memcpy_fromio(hdr, hdr_start, sizeof(struct e8390_pkt_hdr));
323 	hdr->count = (hdr->count + 3) & ~3;     /* Round up allocation. */
324 }
325 
326 /*
327  *	Block input and output are easy on shared memory ethercards, the only
328  *	complication is when the ring buffer wraps. The count will already
329  *	be rounded up to a doubleword value via lne390_get_8390_hdr() above.
330  */
331 
lne390_block_input(struct net_device * dev,int count,struct sk_buff * skb,int ring_offset)332 static void lne390_block_input(struct net_device *dev, int count, struct sk_buff *skb,
333 						  int ring_offset)
334 {
335 	unsigned long xfer_start = dev->mem_start + ring_offset - (LNE390_START_PG<<8);
336 
337 	if (xfer_start + count > dev->rmem_end) {
338 		/* Packet wraps over end of ring buffer. */
339 		int semi_count = dev->rmem_end - xfer_start;
340 		isa_memcpy_fromio(skb->data, xfer_start, semi_count);
341 		count -= semi_count;
342 		isa_memcpy_fromio(skb->data + semi_count, dev->rmem_start, count);
343 	} else {
344 		/* Packet is in one chunk. */
345 		isa_memcpy_fromio(skb->data, xfer_start, count);
346 	}
347 }
348 
lne390_block_output(struct net_device * dev,int count,const unsigned char * buf,int start_page)349 static void lne390_block_output(struct net_device *dev, int count,
350 				const unsigned char *buf, int start_page)
351 {
352 	unsigned long shmem = dev->mem_start + ((start_page - LNE390_START_PG)<<8);
353 
354 	count = (count + 3) & ~3;     /* Round up to doubleword */
355 	isa_memcpy_toio(shmem, buf, count);
356 }
357 
lne390_open(struct net_device * dev)358 static int lne390_open(struct net_device *dev)
359 {
360 	ei_open(dev);
361 	return 0;
362 }
363 
lne390_close(struct net_device * dev)364 static int lne390_close(struct net_device *dev)
365 {
366 
367 	if (ei_debug > 1)
368 		printk("%s: Shutting down ethercard.\n", dev->name);
369 
370 	ei_close(dev);
371 	return 0;
372 }
373 
374 #ifdef MODULE
375 #define MAX_LNE_CARDS	4	/* Max number of LNE390 cards per module */
376 static struct net_device dev_lne[MAX_LNE_CARDS];
377 static int io[MAX_LNE_CARDS];
378 static int irq[MAX_LNE_CARDS];
379 static int mem[MAX_LNE_CARDS];
380 
381 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_LNE_CARDS) "i");
382 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_LNE_CARDS) "i");
383 MODULE_PARM(mem, "1-" __MODULE_STRING(MAX_LNE_CARDS) "i");
384 MODULE_PARM_DESC(io, "I/O base address(es)");
385 MODULE_PARM_DESC(irq, "IRQ number(s)");
386 MODULE_PARM_DESC(mem, "memory base address(es)");
387 MODULE_DESCRIPTION("Mylex LNE390A/B EISA Ethernet driver");
388 MODULE_LICENSE("GPL");
389 
init_module(void)390 int init_module(void)
391 {
392 	int this_dev, found = 0;
393 
394 	for (this_dev = 0; this_dev < MAX_LNE_CARDS; this_dev++) {
395 		struct net_device *dev = &dev_lne[this_dev];
396 		dev->irq = irq[this_dev];
397 		dev->base_addr = io[this_dev];
398 		dev->mem_start = mem[this_dev];
399 		dev->init = lne390_probe;
400 		/* Default is to only install one card. */
401 		if (io[this_dev] == 0 && this_dev != 0) break;
402 		if (register_netdev(dev) != 0) {
403 			printk(KERN_WARNING "lne390.c: No LNE390 card found (i/o = 0x%x).\n", io[this_dev]);
404 			if (found != 0) {	/* Got at least one. */
405 				return 0;
406 			}
407 			return -ENXIO;
408 		}
409 		found++;
410 	}
411 	return 0;
412 }
413 
cleanup_module(void)414 void cleanup_module(void)
415 {
416 	int this_dev;
417 
418 	for (this_dev = 0; this_dev < MAX_LNE_CARDS; this_dev++) {
419 		struct net_device *dev = &dev_lne[this_dev];
420 		if (dev->priv != NULL) {
421 			void *priv = dev->priv;
422 			free_irq(dev->irq, dev);
423 			release_region(dev->base_addr, LNE390_IO_EXTENT);
424 			if (ei_status.reg0)
425 				iounmap((void *)dev->mem_start);
426 			unregister_netdev(dev);
427 			kfree(priv);
428 		}
429 	}
430 }
431 #endif /* MODULE */
432 
433