* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / net / hp.c
blob24b3e9d83c06c2aee17d6ed9057441492a34b72c
1 /* hp.c: A HP LAN ethernet driver for linux. */
2 /*
3 Written 1993-94 by Donald Becker.
5 Copyright 1993 United States Government as represented by the
6 Director, National Security Agency.
8 This software may be used and distributed according to the terms
9 of the GNU Public License, incorporated herein by reference.
11 The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
12 Center of Excellence in Space Data and Information Sciences
13 Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
15 This is a driver for the HP PC-LAN adaptors.
17 Sources:
18 The Crynwr packet driver.
21 static const char *version =
22 "hp.c:v1.10 9/23/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
25 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/errno.h>
30 #include <linux/ioport.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/init.h>
34 #include <linux/delay.h>
36 #include <asm/system.h>
37 #include <asm/io.h>
39 #include "8390.h"
41 /* A zero-terminated list of I/O addresses to be probed. */
42 static unsigned int hppclan_portlist[] __initdata =
43 { 0x300, 0x320, 0x340, 0x280, 0x2C0, 0x200, 0x240, 0};
45 #define HP_IO_EXTENT 32
47 #define HP_DATAPORT 0x0c /* "Remote DMA" data port. */
48 #define HP_ID 0x07
49 #define HP_CONFIGURE 0x08 /* Configuration register. */
50 #define HP_RUN 0x01 /* 1 == Run, 0 == reset. */
51 #define HP_IRQ 0x0E /* Mask for software-configured IRQ line. */
52 #define HP_DATAON 0x10 /* Turn on dataport */
53 #define NIC_OFFSET 0x10 /* Offset the 8390 registers. */
55 #define HP_START_PG 0x00 /* First page of TX buffer */
56 #define HP_8BSTOP_PG 0x80 /* Last page +1 of RX ring */
57 #define HP_16BSTOP_PG 0xFF /* Same, for 16 bit cards. */
59 int hp_probe(struct net_device *dev);
60 int hp_probe1(struct net_device *dev, int ioaddr);
62 static int hp_open(struct net_device *dev);
63 static int hp_close(struct net_device *dev);
64 static void hp_reset_8390(struct net_device *dev);
65 static void hp_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
66 int ring_page);
67 static void hp_block_input(struct net_device *dev, int count,
68 struct sk_buff *skb , int ring_offset);
69 static void hp_block_output(struct net_device *dev, int count,
70 const unsigned char *buf, int start_page);
72 static void hp_init_card(struct net_device *dev);
74 /* The map from IRQ number to HP_CONFIGURE register setting. */
75 /* My default is IRQ5 0 1 2 3 4 5 6 7 8 9 10 11 */
76 static char irqmap[16] __initdata= { 0, 0, 4, 6, 8,10, 0,14, 0, 4, 2,12,0,0,0,0};
79 /* Probe for an HP LAN adaptor.
80 Also initialize the card and fill in STATION_ADDR with the station
81 address. */
82 #ifdef HAVE_DEVLIST
83 struct netdev_entry netcard_drv =
84 {"hp", hp_probe1, HP_IO_EXTENT, hppclan_portlist};
85 #else
87 int __init hp_probe(struct net_device *dev)
89 int i;
90 int base_addr = dev ? dev->base_addr : 0;
92 if (base_addr > 0x1ff) /* Check a single specified location. */
93 return hp_probe1(dev, base_addr);
94 else if (base_addr != 0) /* Don't probe at all. */
95 return ENXIO;
97 for (i = 0; hppclan_portlist[i]; i++) {
98 int ioaddr = hppclan_portlist[i];
99 if (check_region(ioaddr, HP_IO_EXTENT))
100 continue;
101 if (hp_probe1(dev, ioaddr) == 0)
102 return 0;
105 return ENODEV;
107 #endif
109 int __init hp_probe1(struct net_device *dev, int ioaddr)
111 int i, board_id, wordmode;
112 const char *name;
113 static unsigned version_printed = 0;
115 /* Check for the HP physical address, 08 00 09 xx xx xx. */
116 /* This really isn't good enough: we may pick up HP LANCE boards
117 also! Avoid the lance 0x5757 signature. */
118 if (inb(ioaddr) != 0x08
119 || inb(ioaddr+1) != 0x00
120 || inb(ioaddr+2) != 0x09
121 || inb(ioaddr+14) == 0x57)
122 return ENODEV;
124 /* Set up the parameters based on the board ID.
125 If you have additional mappings, please mail them to me -djb. */
126 if ((board_id = inb(ioaddr + HP_ID)) & 0x80) {
127 name = "HP27247";
128 wordmode = 1;
129 } else {
130 name = "HP27250";
131 wordmode = 0;
134 if (load_8390_module("hp.c"))
135 return -ENOSYS;
137 /* We should have a "dev" from Space.c or the static module table. */
138 if (dev == NULL) {
139 printk("hp.c: Passed a NULL device.\n");
140 dev = init_etherdev(0, 0);
143 if (ei_debug && version_printed++ == 0)
144 printk(version);
146 /* Allocate dev->priv and fill in 8390 specific dev fields. */
147 if (ethdev_init(dev)) {
148 printk (" unable to get memory for dev->priv.\n");
149 return -ENOMEM;
152 printk("%s: %s (ID %02x) at %#3x,", dev->name, name, board_id, ioaddr);
154 for(i = 0; i < ETHER_ADDR_LEN; i++)
155 printk(" %2.2x", dev->dev_addr[i] = inb(ioaddr + i));
157 /* Snarf the interrupt now. Someday this could be moved to open(). */
158 if (dev->irq < 2) {
159 int irq_16list[] = { 11, 10, 5, 3, 4, 7, 9, 0};
160 int irq_8list[] = { 7, 5, 3, 4, 9, 0};
161 int *irqp = wordmode ? irq_16list : irq_8list;
162 do {
163 int irq = *irqp;
164 if (request_irq (irq, NULL, 0, "bogus", NULL) != -EBUSY) {
165 autoirq_setup(0);
166 /* Twinkle the interrupt, and check if it's seen. */
167 outb_p(irqmap[irq] | HP_RUN, ioaddr + HP_CONFIGURE);
168 outb_p( 0x00 | HP_RUN, ioaddr + HP_CONFIGURE);
169 if (irq == autoirq_report(0) /* It's a good IRQ line! */
170 && request_irq (irq, ei_interrupt, 0, "hp", dev) == 0) {
171 printk(" selecting IRQ %d.\n", irq);
172 dev->irq = *irqp;
173 break;
176 } while (*++irqp);
177 if (*irqp == 0) {
178 printk(" no free IRQ lines.\n");
179 kfree(dev->priv);
180 dev->priv = NULL;
181 return EBUSY;
183 } else {
184 if (dev->irq == 2)
185 dev->irq = 9;
186 if (request_irq(dev->irq, ei_interrupt, 0, "hp", dev)) {
187 printk (" unable to get IRQ %d.\n", dev->irq);
188 kfree(dev->priv);
189 dev->priv = NULL;
190 return EBUSY;
194 /* Grab the region so we can find another board if something fails. */
195 request_region(ioaddr, HP_IO_EXTENT,"hp");
197 /* Set the base address to point to the NIC, not the "real" base! */
198 dev->base_addr = ioaddr + NIC_OFFSET;
199 dev->open = &hp_open;
200 dev->stop = &hp_close;
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;
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);
214 return 0;
217 static int
218 hp_open(struct net_device *dev)
220 ei_open(dev);
221 MOD_INC_USE_COUNT;
222 return 0;
225 static int
226 hp_close(struct net_device *dev)
228 ei_close(dev);
229 MOD_DEC_USE_COUNT;
230 return 0;
233 static void
234 hp_reset_8390(struct net_device *dev)
236 int hp_base = dev->base_addr - NIC_OFFSET;
237 int saved_config = inb_p(hp_base + HP_CONFIGURE);
239 if (ei_debug > 1) printk("resetting the 8390 time=%ld...", jiffies);
240 outb_p(0x00, hp_base + HP_CONFIGURE);
241 ei_status.txing = 0;
242 /* Pause just a few cycles for the hardware reset to take place. */
243 udelay(5);
245 outb_p(saved_config, hp_base + HP_CONFIGURE);
246 udelay(5);
248 if ((inb_p(hp_base+NIC_OFFSET+EN0_ISR) & ENISR_RESET) == 0)
249 printk("%s: hp_reset_8390() did not complete.\n", dev->name);
251 if (ei_debug > 1) printk("8390 reset done (%ld).", jiffies);
252 return;
255 static void
256 hp_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
258 int nic_base = dev->base_addr;
259 int saved_config = inb_p(nic_base - NIC_OFFSET + HP_CONFIGURE);
261 outb_p(saved_config | HP_DATAON, nic_base - NIC_OFFSET + HP_CONFIGURE);
262 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base);
263 outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
264 outb_p(0, nic_base + EN0_RCNTHI);
265 outb_p(0, nic_base + EN0_RSARLO); /* On page boundary */
266 outb_p(ring_page, nic_base + EN0_RSARHI);
267 outb_p(E8390_RREAD+E8390_START, nic_base);
269 if (ei_status.word16)
270 insw(nic_base - NIC_OFFSET + HP_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
271 else
272 insb(nic_base - NIC_OFFSET + HP_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr));
274 outb_p(saved_config & (~HP_DATAON), nic_base - NIC_OFFSET + HP_CONFIGURE);
277 /* Block input and output, similar to the Crynwr packet driver. If you are
278 porting to a new ethercard look at the packet driver source for hints.
279 The HP LAN doesn't use shared memory -- we put the packet
280 out through the "remote DMA" dataport. */
282 static void
283 hp_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
285 int nic_base = dev->base_addr;
286 int saved_config = inb_p(nic_base - NIC_OFFSET + HP_CONFIGURE);
287 int xfer_count = count;
288 char *buf = skb->data;
290 outb_p(saved_config | HP_DATAON, nic_base - NIC_OFFSET + HP_CONFIGURE);
291 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base);
292 outb_p(count & 0xff, nic_base + EN0_RCNTLO);
293 outb_p(count >> 8, nic_base + EN0_RCNTHI);
294 outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
295 outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
296 outb_p(E8390_RREAD+E8390_START, nic_base);
297 if (ei_status.word16) {
298 insw(nic_base - NIC_OFFSET + HP_DATAPORT,buf,count>>1);
299 if (count & 0x01)
300 buf[count-1] = inb(nic_base - NIC_OFFSET + HP_DATAPORT), xfer_count++;
301 } else {
302 insb(nic_base - NIC_OFFSET + HP_DATAPORT, buf, count);
304 /* This is for the ALPHA version only, remove for later releases. */
305 if (ei_debug > 0) { /* DMA termination address check... */
306 int high = inb_p(nic_base + EN0_RSARHI);
307 int low = inb_p(nic_base + EN0_RSARLO);
308 int addr = (high << 8) + low;
309 /* Check only the lower 8 bits so we can ignore ring wrap. */
310 if (((ring_offset + xfer_count) & 0xff) != (addr & 0xff))
311 printk("%s: RX transfer address mismatch, %#4.4x vs. %#4.4x (actual).\n",
312 dev->name, ring_offset + xfer_count, addr);
314 outb_p(saved_config & (~HP_DATAON), nic_base - NIC_OFFSET + HP_CONFIGURE);
317 static void
318 hp_block_output(struct net_device *dev, int count,
319 const unsigned char *buf, int start_page)
321 int nic_base = dev->base_addr;
322 int saved_config = inb_p(nic_base - NIC_OFFSET + HP_CONFIGURE);
324 outb_p(saved_config | HP_DATAON, nic_base - NIC_OFFSET + HP_CONFIGURE);
325 /* Round the count up for word writes. Do we need to do this?
326 What effect will an odd byte count have on the 8390?
327 I should check someday. */
328 if (ei_status.word16 && (count & 0x01))
329 count++;
330 /* We should already be in page 0, but to be safe... */
331 outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base);
333 #ifdef NE8390_RW_BUGFIX
334 /* Handle the read-before-write bug the same way as the
335 Crynwr packet driver -- the NatSemi method doesn't work. */
336 outb_p(0x42, nic_base + EN0_RCNTLO);
337 outb_p(0, nic_base + EN0_RCNTHI);
338 outb_p(0xff, nic_base + EN0_RSARLO);
339 outb_p(0x00, nic_base + EN0_RSARHI);
340 #define NE_CMD 0x00
341 outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
342 /* Make certain that the dummy read has occurred. */
343 inb_p(0x61);
344 inb_p(0x61);
345 #endif
347 outb_p(count & 0xff, nic_base + EN0_RCNTLO);
348 outb_p(count >> 8, nic_base + EN0_RCNTHI);
349 outb_p(0x00, nic_base + EN0_RSARLO);
350 outb_p(start_page, nic_base + EN0_RSARHI);
352 outb_p(E8390_RWRITE+E8390_START, nic_base);
353 if (ei_status.word16) {
354 /* Use the 'rep' sequence for 16 bit boards. */
355 outsw(nic_base - NIC_OFFSET + HP_DATAPORT, buf, count>>1);
356 } else {
357 outsb(nic_base - NIC_OFFSET + HP_DATAPORT, buf, count);
360 /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here -- it's broken! */
362 /* This is for the ALPHA version only, remove for later releases. */
363 if (ei_debug > 0) { /* DMA termination address check... */
364 int high = inb_p(nic_base + EN0_RSARHI);
365 int low = inb_p(nic_base + EN0_RSARLO);
366 int addr = (high << 8) + low;
367 if ((start_page << 8) + count != addr)
368 printk("%s: TX Transfer address mismatch, %#4.4x vs. %#4.4x.\n",
369 dev->name, (start_page << 8) + count, addr);
371 outb_p(saved_config & (~HP_DATAON), nic_base - NIC_OFFSET + HP_CONFIGURE);
372 return;
375 /* This function resets the ethercard if something screws up. */
376 static void
377 hp_init_card(struct net_device *dev)
379 int irq = dev->irq;
380 NS8390_init(dev, 0);
381 outb_p(irqmap[irq&0x0f] | HP_RUN,
382 dev->base_addr - NIC_OFFSET + HP_CONFIGURE);
383 return;
386 #ifdef MODULE
387 #define MAX_HP_CARDS 4 /* Max number of HP cards per module */
388 #define NAMELEN 8 /* # of chars for storing dev->name */
389 static char namelist[NAMELEN * MAX_HP_CARDS] = { 0, };
390 static struct net_device dev_hp[MAX_HP_CARDS] = {
392 NULL, /* assign a chunk of namelist[] below */
393 0, 0, 0, 0,
394 0, 0,
395 0, 0, 0, NULL, NULL
399 static int io[MAX_HP_CARDS] = { 0, };
400 static int irq[MAX_HP_CARDS] = { 0, };
402 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HP_CARDS) "i");
403 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_HP_CARDS) "i");
405 /* This is set up so that only a single autoprobe takes place per call.
406 ISA device autoprobes on a running machine are not recommended. */
408 init_module(void)
410 int this_dev, found = 0;
412 for (this_dev = 0; this_dev < MAX_HP_CARDS; this_dev++) {
413 struct net_device *dev = &dev_hp[this_dev];
414 dev->name = namelist+(NAMELEN*this_dev);
415 dev->irq = irq[this_dev];
416 dev->base_addr = io[this_dev];
417 dev->init = hp_probe;
418 if (io[this_dev] == 0) {
419 if (this_dev != 0) break; /* only autoprobe 1st one */
420 printk(KERN_NOTICE "hp.c: Presently autoprobing (not recommended) for a single card.\n");
422 if (register_netdev(dev) != 0) {
423 printk(KERN_WARNING "hp.c: No HP card found (i/o = 0x%x).\n", io[this_dev]);
424 if (found != 0) { /* Got at least one. */
425 lock_8390_module();
426 return 0;
428 return -ENXIO;
430 found++;
432 lock_8390_module();
433 return 0;
436 void
437 cleanup_module(void)
439 int this_dev;
441 for (this_dev = 0; this_dev < MAX_HP_CARDS; this_dev++) {
442 struct net_device *dev = &dev_hp[this_dev];
443 if (dev->priv != NULL) {
444 int ioaddr = dev->base_addr - NIC_OFFSET;
445 void *priv = dev->priv;
446 free_irq(dev->irq, dev);
447 release_region(ioaddr, HP_IO_EXTENT);
448 unregister_netdev(dev);
449 kfree(priv);
452 unlock_8390_module();
454 #endif /* MODULE */
457 * Local variables:
458 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c hp.c"
459 * version-control: t
460 * kept-new-versions: 5
461 * tab-width: 4
462 * c-indent-level: 4
463 * End: