1 /*
2 	ne3210.c
3 
4 	Linux driver for Novell NE3210 EISA Network Adapter
5 
6 	Copyright (C) 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 my other EISA 8390 drivers (lne390, es3210, smc-ultra32)
14 	2) The existing myriad of other Linux 8390 drivers by Donald Becker.
15 	3) Info for getting IRQ and sh-mem gleaned from the EISA cfg file
16 
17 	The NE3210 is an EISA shared memory NS8390 implementation.  Shared
18 	memory address > 1MB should work with this driver.
19 
20 	Note that the .cfg file (3/11/93, v1.0) has AUI and BNC switched
21 	around (or perhaps there are some defective/backwards cards ???)
22 
23 	This driver WILL NOT WORK FOR THE NE3200 - it is completely different
24 	and does not use an 8390 at all.
25 
26 */
27 
28 static const char *version =
29 	"ne3210.c: Driver revision v0.03, 30/09/98\n";
30 
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/errno.h>
35 #include <linux/string.h>
36 #include <linux/delay.h>
37 #include <linux/init.h>
38 
39 #include <asm/io.h>
40 #include <asm/system.h>
41 
42 #include <linux/netdevice.h>
43 #include <linux/etherdevice.h>
44 #include "8390.h"
45 
46 int ne3210_probe(struct net_device *dev);
47 static int ne3210_probe1(struct net_device *dev, int ioaddr);
48 
49 static int ne3210_open(struct net_device *dev);
50 static int ne3210_close(struct net_device *dev);
51 
52 static void ne3210_reset_8390(struct net_device *dev);
53 
54 static void ne3210_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page);
55 static void ne3210_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset);
56 static void ne3210_block_output(struct net_device *dev, int count, const unsigned char *buf, const int start_page);
57 
58 #define NE3210_START_PG		0x00    /* First page of TX buffer	*/
59 #define NE3210_STOP_PG		0x80    /* Last page +1 of RX ring	*/
60 
61 #define NE3210_ID_PORT		0xc80	/* Same for all EISA cards 	*/
62 #define NE3210_IO_EXTENT	0x20
63 #define NE3210_SA_PROM		0x16	/* Start of e'net addr.		*/
64 #define NE3210_RESET_PORT	0xc84
65 #define NE3210_NIC_OFFSET	0x00	/* Hello, the 8390 is *here*	*/
66 
67 #define NE3210_ADDR0		0x00	/* 3 byte vendor prefix		*/
68 #define NE3210_ADDR1		0x00
69 #define NE3210_ADDR2		0x1b
70 
71 #define NE3210_ID	0x0118cc3a	/* 0x3acc = 1110 10110 01100 =  nvl */
72 
73 #define NE3210_CFG1		0xc84	/* NB: 0xc84 is also "reset" port. */
74 #define NE3210_CFG2		0xc90
75 
76 /*
77  *	You can OR any of the following bits together and assign it
78  *	to NE3210_DEBUG to get verbose driver info during operation.
79  *	Currently only the probe one is implemented.
80  */
81 
82 #define NE3210_D_PROBE	0x01
83 #define NE3210_D_RX_PKT	0x02
84 #define NE3210_D_TX_PKT	0x04
85 #define NE3210_D_IRQ	0x08
86 
87 #define NE3210_DEBUG	0x0
88 
89 static unsigned char irq_map[] __initdata = {15, 12, 11, 10, 9, 7, 5, 3};
90 static unsigned int shmem_map[] __initdata = {0xff0, 0xfe0, 0xfff0, 0xd8, 0xffe0, 0xffc0, 0xd0, 0x0};
91 
92 /*
93  *	Probe for the card. The best way is to read the EISA ID if it
94  *	is known. Then we can check the prefix of the station address
95  *	PROM for a match against the value assigned to Novell.
96  */
97 
ne3210_probe(struct net_device * dev)98 int __init ne3210_probe(struct net_device *dev)
99 {
100 	unsigned short ioaddr = dev->base_addr;
101 
102 	SET_MODULE_OWNER(dev);
103 
104 	if (ioaddr > 0x1ff)		/* Check a single specified location. */
105 		return ne3210_probe1(dev, ioaddr);
106 	else if (ioaddr > 0)		/* Don't probe at all. */
107 		return -ENXIO;
108 
109 	if (!EISA_bus) {
110 #if NE3210_DEBUG & NE3210_D_PROBE
111 		printk("ne3210-debug: Not an EISA bus. Not probing high ports.\n");
112 #endif
113 		return -ENXIO;
114 	}
115 
116 	/* EISA spec allows for up to 16 slots, but 8 is typical. */
117 	for (ioaddr = 0x1000; ioaddr < 0x9000; ioaddr += 0x1000)
118 		if (ne3210_probe1(dev, ioaddr) == 0)
119 			return 0;
120 
121 	return -ENODEV;
122 }
123 
ne3210_probe1(struct net_device * dev,int ioaddr)124 static int __init ne3210_probe1(struct net_device *dev, int ioaddr)
125 {
126 	int i, retval;
127 	unsigned long eisa_id;
128 	const char *ifmap[] = {"UTP", "?", "BNC", "AUI"};
129 
130 	if (!request_region(dev->base_addr, NE3210_IO_EXTENT, dev->name))
131 		return -EBUSY;
132 
133 	if (inb_p(ioaddr + NE3210_ID_PORT) == 0xff) {
134 		retval = -ENODEV;
135 		goto out;
136 	}
137 
138 #if NE3210_DEBUG & NE3210_D_PROBE
139 	printk("ne3210-debug: probe at %#x, ID %#8x\n", ioaddr, inl(ioaddr + NE3210_ID_PORT));
140 	printk("ne3210-debug: config regs: %#x %#x\n",
141 		inb(ioaddr + NE3210_CFG1), inb(ioaddr + NE3210_CFG2));
142 #endif
143 
144 
145 /*	Check the EISA ID of the card. */
146 	eisa_id = inl(ioaddr + NE3210_ID_PORT);
147 	if (eisa_id != NE3210_ID) {
148 		retval = -ENODEV;
149 		goto out;
150 	}
151 
152 
153 #if 0
154 /*	Check the vendor ID as well. Not really required. */
155 	if (inb(ioaddr + NE3210_SA_PROM + 0) != NE3210_ADDR0
156 		|| inb(ioaddr + NE3210_SA_PROM + 1) != NE3210_ADDR1
157 		|| inb(ioaddr + NE3210_SA_PROM + 2) != NE3210_ADDR2 ) {
158 		printk("ne3210.c: card not found");
159 		for(i = 0; i < ETHER_ADDR_LEN; i++)
160 			printk(" %02x", inb(ioaddr + NE3210_SA_PROM + i));
161 		printk(" (invalid prefix).\n");
162 		retval = -ENODEV;
163 		goto out;
164 	}
165 #endif
166 
167 	/* Allocate dev->priv and fill in 8390 specific dev fields. */
168 	if (ethdev_init(dev)) {
169 		printk ("ne3210.c: unable to allocate memory for dev->priv!\n");
170 		retval = -ENOMEM;
171 		goto out;
172 	}
173 
174 	printk("ne3210.c: NE3210 in EISA slot %d, media: %s, addr:",
175 		ioaddr/0x1000, ifmap[inb(ioaddr + NE3210_CFG2) >> 6]);
176 	for(i = 0; i < ETHER_ADDR_LEN; i++)
177 		printk(" %02x", (dev->dev_addr[i] = inb(ioaddr + NE3210_SA_PROM + i)));
178 	printk(".\nne3210.c: ");
179 
180 	/* Snarf the interrupt now. CFG file has them all listed as `edge' with share=NO */
181 	if (dev->irq == 0) {
182 		unsigned char irq_reg = inb(ioaddr + NE3210_CFG2) >> 3;
183 		dev->irq = irq_map[irq_reg & 0x07];
184 		printk("using");
185 	} else {
186 		/* This is useless unless we reprogram the card here too */
187 		if (dev->irq == 2) dev->irq = 9;	/* Doh! */
188 		printk("assigning");
189 	}
190 	printk(" IRQ %d,", dev->irq);
191 
192 	retval = request_irq(dev->irq, ei_interrupt, 0, dev->name, dev);
193 	if (retval) {
194 		printk (" unable to get IRQ %d.\n", dev->irq);
195 		goto out1;
196 	}
197 
198 	if (dev->mem_start == 0) {
199 		unsigned char mem_reg = inb(ioaddr + NE3210_CFG2) & 0x07;
200 		dev->mem_start = shmem_map[mem_reg] * 0x1000;
201 		printk(" using ");
202 	} else {
203 		/* Should check for value in shmem_map and reprogram the card to use it */
204 		dev->mem_start &= 0xfff8000;
205 		printk(" assigning ");
206 	}
207 
208 	printk("%dkB memory at physical address %#lx\n",
209 			NE3210_STOP_PG/4, dev->mem_start);
210 
211 	/*
212 	   BEWARE!! Some dain-bramaged EISA SCUs will allow you to put
213 	   the card mem within the region covered by `normal' RAM  !!!
214 	*/
215 	if (dev->mem_start > 1024*1024) {	/* phys addr > 1MB */
216 		if (dev->mem_start < virt_to_bus(high_memory)) {
217 			printk(KERN_CRIT "ne3210.c: Card RAM overlaps with normal memory!!!\n");
218 			printk(KERN_CRIT "ne3210.c: Use EISA SCU to set card memory below 1MB,\n");
219 			printk(KERN_CRIT "ne3210.c: or to an address above 0x%lx.\n", virt_to_bus(high_memory));
220 			printk(KERN_CRIT "ne3210.c: Driver NOT installed.\n");
221 			retval = -EINVAL;
222 			goto out2;
223 		}
224 		dev->mem_start = (unsigned long)ioremap(dev->mem_start, NE3210_STOP_PG*0x100);
225 		if (dev->mem_start == 0) {
226 			printk(KERN_ERR "ne3210.c: Unable to remap card memory above 1MB !!\n");
227 			printk(KERN_ERR "ne3210.c: Try using EISA SCU to set memory below 1MB.\n");
228 			printk(KERN_ERR "ne3210.c: Driver NOT installed.\n");
229 			retval = -EAGAIN;
230 			goto out2;
231 		}
232 		ei_status.reg0 = 1;	/* Use as remap flag */
233 		printk("ne3210.c: remapped %dkB card memory to virtual address %#lx\n",
234 				NE3210_STOP_PG/4, dev->mem_start);
235 	}
236 
237 	dev->mem_end = dev->rmem_end = dev->mem_start
238 		+ (NE3210_STOP_PG - NE3210_START_PG)*256;
239 	dev->rmem_start = dev->mem_start + TX_PAGES*256;
240 
241 	/* The 8390 offset is zero for the NE3210 */
242 	dev->base_addr = ioaddr;
243 
244 	ei_status.name = "NE3210";
245 	ei_status.tx_start_page = NE3210_START_PG;
246 	ei_status.rx_start_page = NE3210_START_PG + TX_PAGES;
247 	ei_status.stop_page = NE3210_STOP_PG;
248 	ei_status.word16 = 1;
249 
250 	if (ei_debug > 0)
251 		printk(version);
252 
253 	ei_status.reset_8390 = &ne3210_reset_8390;
254 	ei_status.block_input = &ne3210_block_input;
255 	ei_status.block_output = &ne3210_block_output;
256 	ei_status.get_8390_hdr = &ne3210_get_8390_hdr;
257 
258 	dev->open = &ne3210_open;
259 	dev->stop = &ne3210_close;
260 	NS8390_init(dev, 0);
261 	return 0;
262 out2:
263 	free_irq(dev->irq, dev);
264 out1:
265 	kfree(dev->priv);
266 	dev->priv = NULL;
267 out:
268 	release_region(ioaddr, NE3210_IO_EXTENT);
269 	return retval;
270 }
271 
272 /*
273  *	Reset by toggling the "Board Enable" bits (bit 2 and 0).
274  */
275 
ne3210_reset_8390(struct net_device * dev)276 static void ne3210_reset_8390(struct net_device *dev)
277 {
278 	unsigned short ioaddr = dev->base_addr;
279 
280 	outb(0x04, ioaddr + NE3210_RESET_PORT);
281 	if (ei_debug > 1) printk("%s: resetting the NE3210...", dev->name);
282 
283 	mdelay(2);
284 
285 	ei_status.txing = 0;
286 	outb(0x01, ioaddr + NE3210_RESET_PORT);
287 	if (ei_debug > 1) printk("reset done\n");
288 
289 	return;
290 }
291 
292 /*
293  *	Note: In the following three functions is the implicit assumption
294  *	that the associated memcpy will only use "rep; movsl" as long as
295  *	we keep the counts as some multiple of doublewords. This is a
296  *	requirement of the hardware, and also prevents us from using
297  *	eth_io_copy_and_sum() since we can't guarantee it will limit
298  *	itself to doubleword access.
299  */
300 
301 /*
302  *	Grab the 8390 specific header. Similar to the block_input routine, but
303  *	we don't need to be concerned with ring wrap as the header will be at
304  *	the start of a page, so we optimize accordingly. (A single doubleword.)
305  */
306 
307 static void
ne3210_get_8390_hdr(struct net_device * dev,struct e8390_pkt_hdr * hdr,int ring_page)308 ne3210_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
309 {
310 	unsigned long hdr_start = dev->mem_start + ((ring_page - NE3210_START_PG)<<8);
311 	isa_memcpy_fromio(hdr, hdr_start, sizeof(struct e8390_pkt_hdr));
312 	hdr->count = (hdr->count + 3) & ~3;     /* Round up allocation. */
313 }
314 
315 /*
316  *	Block input and output are easy on shared memory ethercards, the only
317  *	complication is when the ring buffer wraps. The count will already
318  *	be rounded up to a doubleword value via ne3210_get_8390_hdr() above.
319  */
320 
ne3210_block_input(struct net_device * dev,int count,struct sk_buff * skb,int ring_offset)321 static void ne3210_block_input(struct net_device *dev, int count, struct sk_buff *skb,
322 						  int ring_offset)
323 {
324 	unsigned long xfer_start = dev->mem_start + ring_offset - (NE3210_START_PG<<8);
325 
326 	if (xfer_start + count > dev->rmem_end) {
327 		/* Packet wraps over end of ring buffer. */
328 		int semi_count = dev->rmem_end - xfer_start;
329 		isa_memcpy_fromio(skb->data, xfer_start, semi_count);
330 		count -= semi_count;
331 		isa_memcpy_fromio(skb->data + semi_count, dev->rmem_start, count);
332 	} else {
333 		/* Packet is in one chunk. */
334 		isa_memcpy_fromio(skb->data, xfer_start, count);
335 	}
336 }
337 
ne3210_block_output(struct net_device * dev,int count,const unsigned char * buf,int start_page)338 static void ne3210_block_output(struct net_device *dev, int count,
339 				const unsigned char *buf, int start_page)
340 {
341 	unsigned long shmem = dev->mem_start + ((start_page - NE3210_START_PG)<<8);
342 
343 	count = (count + 3) & ~3;     /* Round up to doubleword */
344 	isa_memcpy_toio(shmem, buf, count);
345 }
346 
ne3210_open(struct net_device * dev)347 static int ne3210_open(struct net_device *dev)
348 {
349 	ei_open(dev);
350 	return 0;
351 }
352 
ne3210_close(struct net_device * dev)353 static int ne3210_close(struct net_device *dev)
354 {
355 
356 	if (ei_debug > 1)
357 		printk("%s: Shutting down ethercard.\n", dev->name);
358 
359 	ei_close(dev);
360 	return 0;
361 }
362 
363 #ifdef MODULE
364 #define MAX_NE3210_CARDS	4	/* Max number of NE3210 cards per module */
365 static struct net_device dev_ne3210[MAX_NE3210_CARDS];
366 static int io[MAX_NE3210_CARDS];
367 static int irq[MAX_NE3210_CARDS];
368 static int mem[MAX_NE3210_CARDS];
369 
370 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_NE3210_CARDS) "i");
371 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_NE3210_CARDS) "i");
372 MODULE_PARM(mem, "1-" __MODULE_STRING(MAX_NE3210_CARDS) "i");
373 MODULE_PARM_DESC(io, "I/O base address(es)");
374 MODULE_PARM_DESC(irq, "IRQ number(s)");
375 MODULE_PARM_DESC(mem, "memory base address(es)");
376 MODULE_DESCRIPTION("NE3210 EISA Ethernet driver");
377 MODULE_LICENSE("GPL");
378 
init_module(void)379 int init_module(void)
380 {
381 	int this_dev, found = 0;
382 
383 	for (this_dev = 0; this_dev < MAX_NE3210_CARDS; this_dev++) {
384 		struct net_device *dev = &dev_ne3210[this_dev];
385 		dev->irq = irq[this_dev];
386 		dev->base_addr = io[this_dev];
387 		dev->mem_start = mem[this_dev];
388 		dev->init = ne3210_probe;
389 		/* Default is to only install one card. */
390 		if (io[this_dev] == 0 && this_dev != 0) break;
391 		if (register_netdev(dev) != 0) {
392 			printk(KERN_WARNING "ne3210.c: No NE3210 card found (i/o = 0x%x).\n", io[this_dev]);
393 			if (found != 0) {	/* Got at least one. */
394 				return 0;
395 			}
396 			return -ENXIO;
397 		}
398 		found++;
399 	}
400 	return 0;
401 }
402 
cleanup_module(void)403 void cleanup_module(void)
404 {
405 	int this_dev;
406 
407 	for (this_dev = 0; this_dev < MAX_NE3210_CARDS; this_dev++) {
408 		struct net_device *dev = &dev_ne3210[this_dev];
409 		if (dev->priv != NULL) {
410 			free_irq(dev->irq, dev);
411 			release_region(dev->base_addr, NE3210_IO_EXTENT);
412 			if (ei_status.reg0)
413 				iounmap((void *)dev->mem_start);
414 			unregister_netdev(dev);
415 			kfree(dev->priv);
416 			dev->priv = NULL;
417 		}
418 	}
419 }
420 
421 #endif /* MODULE */
422 
423