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