blob: 3669194f10630c7da9919645dadf4902f7e781c5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
22static 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/delay.h>
34
35#include <asm/system.h>
36#include <asm/io.h>
37
38#include "8390.h"
39
40#define DRV_NAME "hp"
41
42/* A zero-terminated list of I/O addresses to be probed. */
43static 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
60static int hp_probe1(struct net_device *dev, int ioaddr);
61
62static int hp_open(struct net_device *dev);
63static int hp_close(struct net_device *dev);
64static void hp_reset_8390(struct net_device *dev);
65static void hp_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
66 int ring_page);
67static void hp_block_input(struct net_device *dev, int count,
68 struct sk_buff *skb , int ring_offset);
69static void hp_block_output(struct net_device *dev, int count,
70 const unsigned char *buf, int start_page);
71
72static void hp_init_card(struct net_device *dev);
73
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 */
76static char irqmap[16] __initdata= { 0, 0, 4, 6, 8,10, 0,14, 0, 4, 2,12,0,0,0,0};
77
Jeff Garzik6aa20a22006-09-13 13:24:59 -040078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Probe for an HP LAN adaptor.
80 Also initialize the card and fill in STATION_ADDR with the station
81 address. */
82
83static int __init do_hp_probe(struct net_device *dev)
84{
85 int i;
86 int base_addr = dev->base_addr;
87 int irq = dev->irq;
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (base_addr > 0x1ff) /* Check a single specified location. */
90 return hp_probe1(dev, base_addr);
91 else if (base_addr != 0) /* Don't probe at all. */
92 return -ENXIO;
93
94 for (i = 0; hppclan_portlist[i]; i++) {
95 if (hp_probe1(dev, hppclan_portlist[i]) == 0)
96 return 0;
97 dev->irq = irq;
98 }
99
100 return -ENODEV;
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103#ifndef MODULE
104struct net_device * __init hp_probe(int unit)
105{
Alan Cox055e5112008-07-03 23:43:12 -0700106 struct net_device *dev = alloc_eip_netdev();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int err;
108
109 if (!dev)
110 return ERR_PTR(-ENOMEM);
111
112 sprintf(dev->name, "eth%d", unit);
113 netdev_boot_setup_check(dev);
114
115 err = do_hp_probe(dev);
116 if (err)
117 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119out:
120 free_netdev(dev);
121 return ERR_PTR(err);
122}
123#endif
124
125static int __init hp_probe1(struct net_device *dev, int ioaddr)
126{
127 int i, retval, board_id, wordmode;
128 const char *name;
129 static unsigned version_printed;
130
131 if (!request_region(ioaddr, HP_IO_EXTENT, DRV_NAME))
132 return -EBUSY;
133
134 /* Check for the HP physical address, 08 00 09 xx xx xx. */
135 /* This really isn't good enough: we may pick up HP LANCE boards
136 also! Avoid the lance 0x5757 signature. */
137 if (inb(ioaddr) != 0x08
138 || inb(ioaddr+1) != 0x00
139 || inb(ioaddr+2) != 0x09
140 || inb(ioaddr+14) == 0x57) {
141 retval = -ENODEV;
142 goto out;
143 }
144
145 /* Set up the parameters based on the board ID.
146 If you have additional mappings, please mail them to me -djb. */
147 if ((board_id = inb(ioaddr + HP_ID)) & 0x80) {
148 name = "HP27247";
149 wordmode = 1;
150 } else {
151 name = "HP27250";
152 wordmode = 0;
153 }
154
155 if (ei_debug && version_printed++ == 0)
156 printk(version);
157
158 printk("%s: %s (ID %02x) at %#3x,", dev->name, name, board_id, ioaddr);
159
160 for(i = 0; i < ETHER_ADDR_LEN; i++)
Joe Perches0795af52007-10-03 17:59:30 -0700161 dev->dev_addr[i] = inb(ioaddr + i);
162
Johannes Berge1749612008-10-27 15:59:26 -0700163 printk(" %pM", dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 /* Snarf the interrupt now. Someday this could be moved to open(). */
166 if (dev->irq < 2) {
167 int irq_16list[] = { 11, 10, 5, 3, 4, 7, 9, 0};
168 int irq_8list[] = { 7, 5, 3, 4, 9, 0};
169 int *irqp = wordmode ? irq_16list : irq_8list;
170 do {
171 int irq = *irqp;
172 if (request_irq (irq, NULL, 0, "bogus", NULL) != -EBUSY) {
173 unsigned long cookie = probe_irq_on();
174 /* Twinkle the interrupt, and check if it's seen. */
175 outb_p(irqmap[irq] | HP_RUN, ioaddr + HP_CONFIGURE);
176 outb_p( 0x00 | HP_RUN, ioaddr + HP_CONFIGURE);
177 if (irq == probe_irq_off(cookie) /* It's a good IRQ line! */
Alan Cox055e5112008-07-03 23:43:12 -0700178 && request_irq (irq, eip_interrupt, 0, DRV_NAME, dev) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 printk(" selecting IRQ %d.\n", irq);
180 dev->irq = *irqp;
181 break;
182 }
183 }
184 } while (*++irqp);
185 if (*irqp == 0) {
186 printk(" no free IRQ lines.\n");
187 retval = -EBUSY;
188 goto out;
189 }
190 } else {
191 if (dev->irq == 2)
192 dev->irq = 9;
Alan Cox055e5112008-07-03 23:43:12 -0700193 if ((retval = request_irq(dev->irq, eip_interrupt, 0, DRV_NAME, dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 printk (" unable to get IRQ %d.\n", dev->irq);
195 goto out;
196 }
197 }
198
199 /* Set the base address to point to the NIC, not the "real" base! */
200 dev->base_addr = ioaddr + NIC_OFFSET;
201 dev->open = &hp_open;
202 dev->stop = &hp_close;
203#ifdef CONFIG_NET_POLL_CONTROLLER
Alan Cox055e5112008-07-03 23:43:12 -0700204 dev->poll_controller = eip_poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205#endif
206
207 ei_status.name = name;
208 ei_status.word16 = wordmode;
209 ei_status.tx_start_page = HP_START_PG;
210 ei_status.rx_start_page = HP_START_PG + TX_PAGES;
211 ei_status.stop_page = wordmode ? HP_16BSTOP_PG : HP_8BSTOP_PG;
212
213 ei_status.reset_8390 = &hp_reset_8390;
214 ei_status.get_8390_hdr = &hp_get_8390_hdr;
215 ei_status.block_input = &hp_block_input;
216 ei_status.block_output = &hp_block_output;
217 hp_init_card(dev);
218
b1fc5502005-05-12 20:11:55 -0400219 retval = register_netdev(dev);
220 if (retval)
221 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return 0;
b1fc5502005-05-12 20:11:55 -0400223out1:
224 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225out:
226 release_region(ioaddr, HP_IO_EXTENT);
227 return retval;
228}
229
230static int
231hp_open(struct net_device *dev)
232{
Alan Cox055e5112008-07-03 23:43:12 -0700233 eip_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return 0;
235}
236
237static int
238hp_close(struct net_device *dev)
239{
Alan Cox055e5112008-07-03 23:43:12 -0700240 eip_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return 0;
242}
243
244static void
245hp_reset_8390(struct net_device *dev)
246{
247 int hp_base = dev->base_addr - NIC_OFFSET;
248 int saved_config = inb_p(hp_base + HP_CONFIGURE);
249
250 if (ei_debug > 1) printk("resetting the 8390 time=%ld...", jiffies);
251 outb_p(0x00, hp_base + HP_CONFIGURE);
252 ei_status.txing = 0;
253 /* Pause just a few cycles for the hardware reset to take place. */
254 udelay(5);
255
256 outb_p(saved_config, hp_base + HP_CONFIGURE);
257 udelay(5);
258
259 if ((inb_p(hp_base+NIC_OFFSET+EN0_ISR) & ENISR_RESET) == 0)
260 printk("%s: hp_reset_8390() did not complete.\n", dev->name);
261
262 if (ei_debug > 1) printk("8390 reset done (%ld).", jiffies);
263 return;
264}
265
266static void
267hp_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
268{
269 int nic_base = dev->base_addr;
270 int saved_config = inb_p(nic_base - NIC_OFFSET + HP_CONFIGURE);
271
272 outb_p(saved_config | HP_DATAON, nic_base - NIC_OFFSET + HP_CONFIGURE);
273 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base);
274 outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
275 outb_p(0, nic_base + EN0_RCNTHI);
276 outb_p(0, nic_base + EN0_RSARLO); /* On page boundary */
277 outb_p(ring_page, nic_base + EN0_RSARHI);
278 outb_p(E8390_RREAD+E8390_START, nic_base);
279
280 if (ei_status.word16)
281 insw(nic_base - NIC_OFFSET + HP_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
282 else
283 insb(nic_base - NIC_OFFSET + HP_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr));
284
285 outb_p(saved_config & (~HP_DATAON), nic_base - NIC_OFFSET + HP_CONFIGURE);
286}
287
288/* Block input and output, similar to the Crynwr packet driver. If you are
289 porting to a new ethercard look at the packet driver source for hints.
290 The HP LAN doesn't use shared memory -- we put the packet
291 out through the "remote DMA" dataport. */
292
293static void
294hp_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
295{
296 int nic_base = dev->base_addr;
297 int saved_config = inb_p(nic_base - NIC_OFFSET + HP_CONFIGURE);
298 int xfer_count = count;
299 char *buf = skb->data;
300
301 outb_p(saved_config | HP_DATAON, nic_base - NIC_OFFSET + HP_CONFIGURE);
302 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base);
303 outb_p(count & 0xff, nic_base + EN0_RCNTLO);
304 outb_p(count >> 8, nic_base + EN0_RCNTHI);
305 outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
306 outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
307 outb_p(E8390_RREAD+E8390_START, nic_base);
308 if (ei_status.word16) {
309 insw(nic_base - NIC_OFFSET + HP_DATAPORT,buf,count>>1);
310 if (count & 0x01)
311 buf[count-1] = inb(nic_base - NIC_OFFSET + HP_DATAPORT), xfer_count++;
312 } else {
313 insb(nic_base - NIC_OFFSET + HP_DATAPORT, buf, count);
314 }
315 /* This is for the ALPHA version only, remove for later releases. */
316 if (ei_debug > 0) { /* DMA termination address check... */
317 int high = inb_p(nic_base + EN0_RSARHI);
318 int low = inb_p(nic_base + EN0_RSARLO);
319 int addr = (high << 8) + low;
320 /* Check only the lower 8 bits so we can ignore ring wrap. */
321 if (((ring_offset + xfer_count) & 0xff) != (addr & 0xff))
322 printk("%s: RX transfer address mismatch, %#4.4x vs. %#4.4x (actual).\n",
323 dev->name, ring_offset + xfer_count, addr);
324 }
325 outb_p(saved_config & (~HP_DATAON), nic_base - NIC_OFFSET + HP_CONFIGURE);
326}
327
328static void
329hp_block_output(struct net_device *dev, int count,
330 const unsigned char *buf, int start_page)
331{
332 int nic_base = dev->base_addr;
333 int saved_config = inb_p(nic_base - NIC_OFFSET + HP_CONFIGURE);
334
335 outb_p(saved_config | HP_DATAON, nic_base - NIC_OFFSET + HP_CONFIGURE);
336 /* Round the count up for word writes. Do we need to do this?
337 What effect will an odd byte count have on the 8390?
338 I should check someday. */
339 if (ei_status.word16 && (count & 0x01))
340 count++;
341 /* We should already be in page 0, but to be safe... */
342 outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base);
343
344#ifdef NE8390_RW_BUGFIX
345 /* Handle the read-before-write bug the same way as the
346 Crynwr packet driver -- the NatSemi method doesn't work. */
347 outb_p(0x42, nic_base + EN0_RCNTLO);
348 outb_p(0, nic_base + EN0_RCNTHI);
349 outb_p(0xff, nic_base + EN0_RSARLO);
350 outb_p(0x00, nic_base + EN0_RSARHI);
351#define NE_CMD 0x00
352 outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
353 /* Make certain that the dummy read has occurred. */
354 inb_p(0x61);
355 inb_p(0x61);
356#endif
357
358 outb_p(count & 0xff, nic_base + EN0_RCNTLO);
359 outb_p(count >> 8, nic_base + EN0_RCNTHI);
360 outb_p(0x00, nic_base + EN0_RSARLO);
361 outb_p(start_page, nic_base + EN0_RSARHI);
362
363 outb_p(E8390_RWRITE+E8390_START, nic_base);
364 if (ei_status.word16) {
365 /* Use the 'rep' sequence for 16 bit boards. */
366 outsw(nic_base - NIC_OFFSET + HP_DATAPORT, buf, count>>1);
367 } else {
368 outsb(nic_base - NIC_OFFSET + HP_DATAPORT, buf, count);
369 }
370
371 /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here -- it's broken! */
372
373 /* This is for the ALPHA version only, remove for later releases. */
374 if (ei_debug > 0) { /* DMA termination address check... */
375 int high = inb_p(nic_base + EN0_RSARHI);
376 int low = inb_p(nic_base + EN0_RSARLO);
377 int addr = (high << 8) + low;
378 if ((start_page << 8) + count != addr)
379 printk("%s: TX Transfer address mismatch, %#4.4x vs. %#4.4x.\n",
380 dev->name, (start_page << 8) + count, addr);
381 }
382 outb_p(saved_config & (~HP_DATAON), nic_base - NIC_OFFSET + HP_CONFIGURE);
383 return;
384}
385
386/* This function resets the ethercard if something screws up. */
Randy Dunlap29f9f6d2006-06-10 13:32:12 -0700387static void __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388hp_init_card(struct net_device *dev)
389{
390 int irq = dev->irq;
Ingo Molnarf0084a32008-07-22 09:23:34 +0200391 NS8390p_init(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 outb_p(irqmap[irq&0x0f] | HP_RUN,
393 dev->base_addr - NIC_OFFSET + HP_CONFIGURE);
394 return;
395}
396
397#ifdef MODULE
398#define MAX_HP_CARDS 4 /* Max number of HP cards per module */
399static struct net_device *dev_hp[MAX_HP_CARDS];
400static int io[MAX_HP_CARDS];
401static int irq[MAX_HP_CARDS];
402
403module_param_array(io, int, NULL, 0);
404module_param_array(irq, int, NULL, 0);
405MODULE_PARM_DESC(io, "I/O base address(es)");
406MODULE_PARM_DESC(irq, "IRQ number(s) (assigned)");
407MODULE_DESCRIPTION("HP PC-LAN ISA ethernet driver");
408MODULE_LICENSE("GPL");
409
410/* This is set up so that only a single autoprobe takes place per call.
411ISA device autoprobes on a running machine are not recommended. */
Randy Dunlap29f9f6d2006-06-10 13:32:12 -0700412int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413init_module(void)
414{
415 struct net_device *dev;
416 int this_dev, found = 0;
417
418 for (this_dev = 0; this_dev < MAX_HP_CARDS; this_dev++) {
419 if (io[this_dev] == 0) {
420 if (this_dev != 0) break; /* only autoprobe 1st one */
421 printk(KERN_NOTICE "hp.c: Presently autoprobing (not recommended) for a single card.\n");
422 }
Alan Cox055e5112008-07-03 23:43:12 -0700423 dev = alloc_eip_netdev();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 if (!dev)
425 break;
426 dev->irq = irq[this_dev];
427 dev->base_addr = io[this_dev];
428 if (do_hp_probe(dev) == 0) {
b1fc5502005-05-12 20:11:55 -0400429 dev_hp[found++] = dev;
430 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432 free_netdev(dev);
433 printk(KERN_WARNING "hp.c: No HP card found (i/o = 0x%x).\n", io[this_dev]);
434 break;
435 }
436 if (found)
437 return 0;
438 return -ENXIO;
439}
440
Denis Vlasenko64916f12006-01-05 22:45:47 -0800441static void cleanup_card(struct net_device *dev)
442{
443 free_irq(dev->irq, dev);
444 release_region(dev->base_addr - NIC_OFFSET, HP_IO_EXTENT);
445}
446
Al Viroafc8eb42006-06-14 18:50:53 -0400447void __exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448cleanup_module(void)
449{
450 int this_dev;
451
452 for (this_dev = 0; this_dev < MAX_HP_CARDS; this_dev++) {
453 struct net_device *dev = dev_hp[this_dev];
454 if (dev) {
455 unregister_netdev(dev);
456 cleanup_card(dev);
457 free_netdev(dev);
458 }
459 }
460}
461#endif /* MODULE */