1 /* hp.c: A HP LAN ethernet driver for linux. */
2 /*
3 	Written 1993-94 by Donald Becker.
4 
5 	Copyright 1993 United States Government as represented by the
6 	Director, National Security Agency.
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 	The author may be reached as becker@scyld.com, or C/O
12 	Scyld Computing Corporation
13 	410 Severn Ave., Suite 210
14 	Annapolis MD 21403
15 
16 	This is a driver for the HP PC-LAN adaptors.
17 
18 	Sources:
19 	  The Crynwr packet driver.
20 */
21 
22 static const char version[] =
23 	"hp.c:v1.10 9/23/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
24 
25 
26 #include <linux/module.h>
27 
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/errno.h>
31 #include <linux/ioport.h>
32 #include <linux/netdevice.h>
33 #include <linux/etherdevice.h>
34 #include <linux/init.h>
35 #include <linux/delay.h>
36 
37 #include <asm/system.h>
38 #include <asm/io.h>
39 
40 #include "8390.h"
41 
42 /* A zero-terminated list of I/O addresses to be probed. */
43 static unsigned int hppclan_portlist[] __initdata =
44 { 0x300, 0x320, 0x340, 0x280, 0x2C0, 0x200, 0x240, 0};
45 
46 #define HP_IO_EXTENT	32
47 
48 #define HP_DATAPORT		0x0c	/* "Remote DMA" data port. */
49 #define HP_ID			0x07
50 #define HP_CONFIGURE	0x08	/* Configuration register. */
51 #define	 HP_RUN			0x01	/* 1 == Run, 0 == reset. */
52 #define	 HP_IRQ			0x0E	/* Mask for software-configured IRQ line. */
53 #define	 HP_DATAON		0x10	/* Turn on dataport */
54 #define NIC_OFFSET		0x10	/* Offset the 8390 registers. */
55 
56 #define HP_START_PG		0x00	/* First page of TX buffer */
57 #define HP_8BSTOP_PG	0x80	/* Last page +1 of RX ring */
58 #define HP_16BSTOP_PG	0xFF	/* Same, for 16 bit cards. */
59 
60 int hp_probe(struct net_device *dev);
61 static int hp_probe1(struct net_device *dev, int ioaddr);
62 
63 static int hp_open(struct net_device *dev);
64 static int hp_close(struct net_device *dev);
65 static void hp_reset_8390(struct net_device *dev);
66 static void hp_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
67 					int ring_page);
68 static void hp_block_input(struct net_device *dev, int count,
69 					struct sk_buff *skb , int ring_offset);
70 static void hp_block_output(struct net_device *dev, int count,
71 							const unsigned char *buf, int start_page);
72 
73 static void hp_init_card(struct net_device *dev);
74 
75 /* The map from IRQ number to HP_CONFIGURE register setting. */
76 /* My default is IRQ5	             0  1  2  3  4  5  6  7  8  9 10 11 */
77 static char irqmap[16] __initdata= { 0, 0, 4, 6, 8,10, 0,14, 0, 4, 2,12,0,0,0,0};
78 
79 
80 /*	Probe for an HP LAN adaptor.
81 	Also initialize the card and fill in STATION_ADDR with the station
82 	address. */
83 
hp_probe(struct net_device * dev)84 int __init hp_probe(struct net_device *dev)
85 {
86 	int i;
87 	int base_addr = dev->base_addr;
88 
89 	SET_MODULE_OWNER(dev);
90 
91 	if (base_addr > 0x1ff)		/* Check a single specified location. */
92 		return hp_probe1(dev, base_addr);
93 	else if (base_addr != 0)	/* Don't probe at all. */
94 		return -ENXIO;
95 
96 	for (i = 0; hppclan_portlist[i]; i++)
97 		if (hp_probe1(dev, hppclan_portlist[i]) == 0)
98 			return 0;
99 
100 	return -ENODEV;
101 }
102 
hp_probe1(struct net_device * dev,int ioaddr)103 static int __init hp_probe1(struct net_device *dev, int ioaddr)
104 {
105 	int i, retval, board_id, wordmode;
106 	const char *name;
107 	static unsigned version_printed;
108 
109 	if (!request_region(ioaddr, HP_IO_EXTENT, dev->name))
110 		return -EBUSY;
111 
112 	/* Check for the HP physical address, 08 00 09 xx xx xx. */
113 	/* This really isn't good enough: we may pick up HP LANCE boards
114 	   also!  Avoid the lance 0x5757 signature. */
115 	if (inb(ioaddr) != 0x08
116 		|| inb(ioaddr+1) != 0x00
117 		|| inb(ioaddr+2) != 0x09
118 		|| inb(ioaddr+14) == 0x57) {
119 		retval = -ENODEV;
120 		goto out;
121 	}
122 
123 	/* Set up the parameters based on the board ID.
124 	   If you have additional mappings, please mail them to me -djb. */
125 	if ((board_id = inb(ioaddr + HP_ID)) & 0x80) {
126 		name = "HP27247";
127 		wordmode = 1;
128 	} else {
129 		name = "HP27250";
130 		wordmode = 0;
131 	}
132 
133 	if (ei_debug  &&  version_printed++ == 0)
134 		printk(version);
135 
136 	/* Allocate dev->priv and fill in 8390 specific dev fields. */
137 	if (ethdev_init(dev)) {
138 		printk (" unable to get memory for dev->priv.\n");
139 		retval = -ENOMEM;
140 		goto out;
141 	}
142 
143 	printk("%s: %s (ID %02x) at %#3x,", dev->name, name, board_id, ioaddr);
144 
145 	for(i = 0; i < ETHER_ADDR_LEN; i++)
146 		printk(" %2.2x", dev->dev_addr[i] = inb(ioaddr + i));
147 
148 	/* Snarf the interrupt now.  Someday this could be moved to open(). */
149 	if (dev->irq < 2) {
150 		int irq_16list[] = { 11, 10, 5, 3, 4, 7, 9, 0};
151 		int irq_8list[] = { 7, 5, 3, 4, 9, 0};
152 		int *irqp = wordmode ? irq_16list : irq_8list;
153 		do {
154 			int irq = *irqp;
155 			if (request_irq (irq, NULL, 0, "bogus", NULL) != -EBUSY) {
156 				unsigned long cookie = probe_irq_on();
157 				/* Twinkle the interrupt, and check if it's seen. */
158 				outb_p(irqmap[irq] | HP_RUN, ioaddr + HP_CONFIGURE);
159 				outb_p( 0x00 | HP_RUN, ioaddr + HP_CONFIGURE);
160 				if (irq == probe_irq_off(cookie)		 /* It's a good IRQ line! */
161 					&& request_irq (irq, ei_interrupt, 0, dev->name, dev) == 0) {
162 					printk(" selecting IRQ %d.\n", irq);
163 					dev->irq = *irqp;
164 					break;
165 				}
166 			}
167 		} while (*++irqp);
168 		if (*irqp == 0) {
169 			printk(" no free IRQ lines.\n");
170 			retval = -EBUSY;
171 			goto out1;
172 		}
173 	} else {
174 		if (dev->irq == 2)
175 			dev->irq = 9;
176 		if ((retval = request_irq(dev->irq, ei_interrupt, 0, dev->name, dev))) {
177 			printk (" unable to get IRQ %d.\n", dev->irq);
178 			goto out1;
179 		}
180 	}
181 
182 	/* Set the base address to point to the NIC, not the "real" base! */
183 	dev->base_addr = ioaddr + NIC_OFFSET;
184 	dev->open = &hp_open;
185 	dev->stop = &hp_close;
186 
187 	ei_status.name = name;
188 	ei_status.word16 = wordmode;
189 	ei_status.tx_start_page = HP_START_PG;
190 	ei_status.rx_start_page = HP_START_PG + TX_PAGES;
191 	ei_status.stop_page = wordmode ? HP_16BSTOP_PG : HP_8BSTOP_PG;
192 
193 	ei_status.reset_8390 = &hp_reset_8390;
194 	ei_status.get_8390_hdr = &hp_get_8390_hdr;
195 	ei_status.block_input = &hp_block_input;
196 	ei_status.block_output = &hp_block_output;
197 	hp_init_card(dev);
198 
199 	return 0;
200 out1:
201 	kfree(dev->priv);
202 	dev->priv = NULL;
203 out:
204 	release_region(ioaddr, HP_IO_EXTENT);
205 	return retval;
206 }
207 
208 static int
hp_open(struct net_device * dev)209 hp_open(struct net_device *dev)
210 {
211 	ei_open(dev);
212 	return 0;
213 }
214 
215 static int
hp_close(struct net_device * dev)216 hp_close(struct net_device *dev)
217 {
218 	ei_close(dev);
219 	return 0;
220 }
221 
222 static void
hp_reset_8390(struct net_device * dev)223 hp_reset_8390(struct net_device *dev)
224 {
225 	int hp_base = dev->base_addr - NIC_OFFSET;
226 	int saved_config = inb_p(hp_base + HP_CONFIGURE);
227 
228 	if (ei_debug > 1) printk("resetting the 8390 time=%ld...", jiffies);
229 	outb_p(0x00, hp_base + HP_CONFIGURE);
230 	ei_status.txing = 0;
231 	/* Pause just a few cycles for the hardware reset to take place. */
232 	udelay(5);
233 
234 	outb_p(saved_config, hp_base + HP_CONFIGURE);
235 	udelay(5);
236 
237 	if ((inb_p(hp_base+NIC_OFFSET+EN0_ISR) & ENISR_RESET) == 0)
238 		printk("%s: hp_reset_8390() did not complete.\n", dev->name);
239 
240 	if (ei_debug > 1) printk("8390 reset done (%ld).", jiffies);
241 	return;
242 }
243 
244 static void
hp_get_8390_hdr(struct net_device * dev,struct e8390_pkt_hdr * hdr,int ring_page)245 hp_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
246 {
247 	int nic_base = dev->base_addr;
248 	int saved_config = inb_p(nic_base - NIC_OFFSET + HP_CONFIGURE);
249 
250 	outb_p(saved_config | HP_DATAON, nic_base - NIC_OFFSET + HP_CONFIGURE);
251 	outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base);
252 	outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
253 	outb_p(0, nic_base + EN0_RCNTHI);
254 	outb_p(0, nic_base + EN0_RSARLO);	/* On page boundary */
255 	outb_p(ring_page, nic_base + EN0_RSARHI);
256 	outb_p(E8390_RREAD+E8390_START, nic_base);
257 
258 	if (ei_status.word16)
259 	  insw(nic_base - NIC_OFFSET + HP_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
260 	else
261 	  insb(nic_base - NIC_OFFSET + HP_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr));
262 
263 	outb_p(saved_config & (~HP_DATAON), nic_base - NIC_OFFSET + HP_CONFIGURE);
264 }
265 
266 /* Block input and output, similar to the Crynwr packet driver. If you are
267    porting to a new ethercard look at the packet driver source for hints.
268    The HP LAN doesn't use shared memory -- we put the packet
269    out through the "remote DMA" dataport. */
270 
271 static void
hp_block_input(struct net_device * dev,int count,struct sk_buff * skb,int ring_offset)272 hp_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
273 {
274 	int nic_base = dev->base_addr;
275 	int saved_config = inb_p(nic_base - NIC_OFFSET + HP_CONFIGURE);
276 	int xfer_count = count;
277 	char *buf = skb->data;
278 
279 	outb_p(saved_config | HP_DATAON, nic_base - NIC_OFFSET + HP_CONFIGURE);
280 	outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base);
281 	outb_p(count & 0xff, nic_base + EN0_RCNTLO);
282 	outb_p(count >> 8, nic_base + EN0_RCNTHI);
283 	outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
284 	outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
285 	outb_p(E8390_RREAD+E8390_START, nic_base);
286 	if (ei_status.word16) {
287 	  insw(nic_base - NIC_OFFSET + HP_DATAPORT,buf,count>>1);
288 	  if (count & 0x01)
289 		buf[count-1] = inb(nic_base - NIC_OFFSET + HP_DATAPORT), xfer_count++;
290 	} else {
291 		insb(nic_base - NIC_OFFSET + HP_DATAPORT, buf, count);
292 	}
293 	/* This is for the ALPHA version only, remove for later releases. */
294 	if (ei_debug > 0) {			/* DMA termination address check... */
295 	  int high = inb_p(nic_base + EN0_RSARHI);
296 	  int low = inb_p(nic_base + EN0_RSARLO);
297 	  int addr = (high << 8) + low;
298 	  /* Check only the lower 8 bits so we can ignore ring wrap. */
299 	  if (((ring_offset + xfer_count) & 0xff) != (addr & 0xff))
300 		printk("%s: RX transfer address mismatch, %#4.4x vs. %#4.4x (actual).\n",
301 			   dev->name, ring_offset + xfer_count, addr);
302 	}
303 	outb_p(saved_config & (~HP_DATAON), nic_base - NIC_OFFSET + HP_CONFIGURE);
304 }
305 
306 static void
hp_block_output(struct net_device * dev,int count,const unsigned char * buf,int start_page)307 hp_block_output(struct net_device *dev, int count,
308 				const unsigned char *buf, int start_page)
309 {
310 	int nic_base = dev->base_addr;
311 	int saved_config = inb_p(nic_base - NIC_OFFSET + HP_CONFIGURE);
312 
313 	outb_p(saved_config | HP_DATAON, nic_base - NIC_OFFSET + HP_CONFIGURE);
314 	/* Round the count up for word writes.	Do we need to do this?
315 	   What effect will an odd byte count have on the 8390?
316 	   I should check someday. */
317 	if (ei_status.word16 && (count & 0x01))
318 	  count++;
319 	/* We should already be in page 0, but to be safe... */
320 	outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base);
321 
322 #ifdef NE8390_RW_BUGFIX
323 	/* Handle the read-before-write bug the same way as the
324 	   Crynwr packet driver -- the NatSemi method doesn't work. */
325 	outb_p(0x42, nic_base + EN0_RCNTLO);
326 	outb_p(0,	nic_base + EN0_RCNTHI);
327 	outb_p(0xff, nic_base + EN0_RSARLO);
328 	outb_p(0x00, nic_base + EN0_RSARHI);
329 #define NE_CMD	 	0x00
330 	outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
331 	/* Make certain that the dummy read has occurred. */
332 	inb_p(0x61);
333 	inb_p(0x61);
334 #endif
335 
336 	outb_p(count & 0xff, nic_base + EN0_RCNTLO);
337 	outb_p(count >> 8,	 nic_base + EN0_RCNTHI);
338 	outb_p(0x00, nic_base + EN0_RSARLO);
339 	outb_p(start_page, nic_base + EN0_RSARHI);
340 
341 	outb_p(E8390_RWRITE+E8390_START, nic_base);
342 	if (ei_status.word16) {
343 		/* Use the 'rep' sequence for 16 bit boards. */
344 		outsw(nic_base - NIC_OFFSET + HP_DATAPORT, buf, count>>1);
345 	} else {
346 		outsb(nic_base - NIC_OFFSET + HP_DATAPORT, buf, count);
347 	}
348 
349 	/* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here -- it's broken! */
350 
351 	/* This is for the ALPHA version only, remove for later releases. */
352 	if (ei_debug > 0) {			/* DMA termination address check... */
353 	  int high = inb_p(nic_base + EN0_RSARHI);
354 	  int low  = inb_p(nic_base + EN0_RSARLO);
355 	  int addr = (high << 8) + low;
356 	  if ((start_page << 8) + count != addr)
357 		printk("%s: TX Transfer address mismatch, %#4.4x vs. %#4.4x.\n",
358 			   dev->name, (start_page << 8) + count, addr);
359 	}
360 	outb_p(saved_config & (~HP_DATAON), nic_base - NIC_OFFSET + HP_CONFIGURE);
361 	return;
362 }
363 
364 /* This function resets the ethercard if something screws up. */
365 static void
hp_init_card(struct net_device * dev)366 hp_init_card(struct net_device *dev)
367 {
368 	int irq = dev->irq;
369 	NS8390_init(dev, 0);
370 	outb_p(irqmap[irq&0x0f] | HP_RUN,
371 		   dev->base_addr - NIC_OFFSET + HP_CONFIGURE);
372 	return;
373 }
374 
375 #ifdef MODULE
376 #define MAX_HP_CARDS	4	/* Max number of HP cards per module */
377 static struct net_device dev_hp[MAX_HP_CARDS];
378 static int io[MAX_HP_CARDS];
379 static int irq[MAX_HP_CARDS];
380 
381 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HP_CARDS) "i");
382 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_HP_CARDS) "i");
383 MODULE_PARM_DESC(io, "I/O base address(es)");
384 MODULE_PARM_DESC(irq, "IRQ number(s) (assigned)");
385 MODULE_DESCRIPTION("HP PC-LAN ISA ethernet driver");
386 MODULE_LICENSE("GPL");
387 
388 /* This is set up so that only a single autoprobe takes place per call.
389 ISA device autoprobes on a running machine are not recommended. */
390 int
init_module(void)391 init_module(void)
392 {
393 	int this_dev, found = 0;
394 
395 	for (this_dev = 0; this_dev < MAX_HP_CARDS; this_dev++) {
396 		struct net_device *dev = &dev_hp[this_dev];
397 		dev->irq = irq[this_dev];
398 		dev->base_addr = io[this_dev];
399 		dev->init = hp_probe;
400 		if (io[this_dev] == 0)  {
401 			if (this_dev != 0) break; /* only autoprobe 1st one */
402 			printk(KERN_NOTICE "hp.c: Presently autoprobing (not recommended) for a single card.\n");
403 		}
404 		if (register_netdev(dev) != 0) {
405 			printk(KERN_WARNING "hp.c: No HP card found (i/o = 0x%x).\n", io[this_dev]);
406 			if (found != 0) {	/* Got at least one. */
407 				return 0;
408 			}
409 			return -ENXIO;
410 		}
411 		found++;
412 	}
413 	return 0;
414 }
415 
416 void
cleanup_module(void)417 cleanup_module(void)
418 {
419 	int this_dev;
420 
421 	for (this_dev = 0; this_dev < MAX_HP_CARDS; this_dev++) {
422 		struct net_device *dev = &dev_hp[this_dev];
423 		if (dev->priv != NULL) {
424 			int ioaddr = dev->base_addr - NIC_OFFSET;
425 			void *priv = dev->priv;
426 			free_irq(dev->irq, dev);
427 			release_region(ioaddr, HP_IO_EXTENT);
428 			unregister_netdev(dev);
429 			kfree(priv);
430 		}
431 	}
432 }
433 #endif /* MODULE */
434 
435 
436 /*
437  * Local variables:
438  * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c hp.c"
439  * version-control: t
440  * kept-new-versions: 5
441  * tab-width: 4
442  * c-indent-level: 4
443  * End:
444  */
445