1 /* hp-plus.c: A HP PCLAN/plus ethernet driver for linux. */
3 Written 1994 by Donald Becker.
5 This driver is for the Hewlett Packard PC LAN (27***) plus ethercards.
6 These cards are sold under several model numbers, usually 2724*.
8 This software may be used and distributed according to the terms
9 of the GNU General Public License, incorporated herein by reference.
11 The author may be reached as becker@scyld.com, or C/O
12 Scyld Computing Corporation
13 410 Severn Ave., Suite 210
16 As is often the case, a great deal of credit is owed to Russ Nelson.
17 The Crynwr packet driver was my primary source of HP-specific
18 programming information.
21 static const char version[] =
22 "hp-plus.c:v1.10 9/24/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
24 #include <linux/module.h>
26 #include <linux/string.h> /* Important -- this inlines word moves. */
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>
35 #include <asm/system.h>
40 #define DRV_NAME "hp-plus"
42 /* A zero-terminated list of I/O addresses to be probed. */
43 static unsigned int hpplus_portlist[] __initdata =
44 {0x200, 0x240, 0x280, 0x2C0, 0x300, 0x320, 0x340, 0};
47 The HP EtherTwist chip implementation is a fairly routine DP8390
48 implementation. It allows both shared memory and programmed-I/O buffer
49 access, using a custom interface for both. The programmed-I/O mode is
50 entirely implemented in the HP EtherTwist chip, bypassing the problem
51 ridden built-in 8390 facilities used on NE2000 designs. The shared
52 memory mode is likewise special, with an offset register used to make
53 packets appear at the shared memory base. Both modes use a base and bounds
54 page register to hide the Rx ring buffer wrap -- a packet that spans the
55 end of physical buffer memory appears continuous to the driver. (c.f. the
56 3c503 and Cabletron E2100)
58 A special note: the internal buffer of the board is only 8 bits wide.
59 This lays several nasty traps for the unaware:
60 - the 8390 must be programmed for byte-wide operations
61 - all I/O and memory operations must work on whole words (the access
62 latches are serially preloaded and have no byte-swapping ability).
64 This board is laid out in I/O space much like the earlier HP boards:
65 the first 16 locations are for the board registers, and the second 16 are
66 for the 8390. The board is easy to identify, with both a dedicated 16 bit
67 ID register and a constant 0x530* value in the upper bits of the paging
71 #define HP_ID 0x00 /* ID register, always 0x4850. */
72 #define HP_PAGING 0x02 /* Registers visible @ 8-f, see PageName. */
73 #define HPP_OPTION 0x04 /* Bitmapped options, see HP_Option. */
74 #define HPP_OUT_ADDR 0x08 /* I/O output location in Perf_Page. */
75 #define HPP_IN_ADDR 0x0A /* I/O input location in Perf_Page. */
76 #define HP_DATAPORT 0x0c /* I/O data transfer in Perf_Page. */
77 #define NIC_OFFSET 0x10 /* Offset to the 8390 registers. */
78 #define HP_IO_EXTENT 32
80 #define HP_START_PG 0x00 /* First page of TX buffer */
81 #define HP_STOP_PG 0x80 /* Last page +1 of RX ring */
83 /* The register set selected in HP_PAGING. */
85 Perf_Page = 0, /* Normal operation. */
86 MAC_Page = 1, /* The ethernet address (+checksum). */
87 HW_Page = 2, /* EEPROM-loaded hardware parameters. */
88 LAN_Page = 4, /* Transceiver selection, testing, etc. */
91 /* The bit definitions for the HPP_OPTION register. */
93 NICReset = 1, ChipReset = 2, /* Active low, really UNreset. */
94 EnableIRQ = 4, FakeIntr = 8, BootROMEnb = 0x10, IOEnb = 0x20,
95 MemEnable = 0x40, ZeroWait = 0x80, MemDisable = 0x1000, };
97 static int hpp_probe1(struct net_device *dev, int ioaddr);
99 static void hpp_reset_8390(struct net_device *dev);
100 static int hpp_open(struct net_device *dev);
101 static int hpp_close(struct net_device *dev);
102 static void hpp_mem_block_input(struct net_device *dev, int count,
103 struct sk_buff *skb, int ring_offset);
104 static void hpp_mem_block_output(struct net_device *dev, int count,
105 const unsigned char *buf, int start_page);
106 static void hpp_mem_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
108 static void hpp_io_block_input(struct net_device *dev, int count,
109 struct sk_buff *skb, int ring_offset);
110 static void hpp_io_block_output(struct net_device *dev, int count,
111 const unsigned char *buf, int start_page);
112 static void hpp_io_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
116 /* Probe a list of addresses for an HP LAN+ adaptor.
117 This routine is almost boilerplate. */
119 static int __init do_hpp_probe(struct net_device *dev)
122 int base_addr = dev->base_addr;
125 if (base_addr > 0x1ff) /* Check a single specified location. */
126 return hpp_probe1(dev, base_addr);
127 else if (base_addr != 0) /* Don't probe at all. */
130 for (i = 0; hpplus_portlist[i]; i++) {
131 if (hpp_probe1(dev, hpplus_portlist[i]) == 0)
140 struct net_device * __init hp_plus_probe(int unit)
142 struct net_device *dev = alloc_eip_netdev();
146 return ERR_PTR(-ENOMEM);
148 sprintf(dev->name, "eth%d", unit);
149 netdev_boot_setup_check(dev);
151 err = do_hpp_probe(dev);
161 static const struct net_device_ops hpp_netdev_ops = {
162 .ndo_open = hpp_open,
163 .ndo_stop = hpp_close,
164 .ndo_start_xmit = eip_start_xmit,
165 .ndo_tx_timeout = eip_tx_timeout,
166 .ndo_get_stats = eip_get_stats,
167 .ndo_set_multicast_list = eip_set_multicast_list,
168 .ndo_validate_addr = eth_validate_addr,
169 .ndo_change_mtu = eth_change_mtu,
170 #ifdef CONFIG_NET_POLL_CONTROLLER
171 .ndo_poll_controller = eip_poll,
176 /* Do the interesting part of the probe at a single address. */
177 static int __init hpp_probe1(struct net_device *dev, int ioaddr)
180 unsigned char checksum = 0;
181 const char name[] = "HP-PC-LAN+";
183 static unsigned version_printed;
185 if (!request_region(ioaddr, HP_IO_EXTENT, DRV_NAME))
188 /* Check for the HP+ signature, 50 48 0x 53. */
189 if (inw(ioaddr + HP_ID) != 0x4850
190 || (inw(ioaddr + HP_PAGING) & 0xfff0) != 0x5300) {
195 if (ei_debug && version_printed++ == 0)
198 printk("%s: %s at %#3x, ", dev->name, name, ioaddr);
200 /* Retrieve and checksum the station address. */
201 outw(MAC_Page, ioaddr + HP_PAGING);
203 for(i = 0; i < ETHER_ADDR_LEN; i++) {
204 unsigned char inval = inb(ioaddr + 8 + i);
205 dev->dev_addr[i] = inval;
208 checksum += inb(ioaddr + 14);
210 printk("%pM", dev->dev_addr);
212 if (checksum != 0xff) {
213 printk(" bad checksum %2.2x.\n", checksum);
217 /* Point at the Software Configuration Flags. */
218 outw(ID_Page, ioaddr + HP_PAGING);
219 printk(" ID %4.4x", inw(ioaddr + 12));
222 /* Read the IRQ line. */
223 outw(HW_Page, ioaddr + HP_PAGING);
225 int irq = inb(ioaddr + 13) & 0x0f;
226 int option = inw(ioaddr + HPP_OPTION);
229 if (option & MemEnable) {
230 mem_start = inw(ioaddr + 9) << 8;
231 printk(", IRQ %d, memory address %#x.\n", irq, mem_start);
234 printk(", IRQ %d, programmed-I/O mode.\n", irq);
238 /* Set the wrap registers for string I/O reads. */
239 outw((HP_START_PG + TX_PAGES/2) | ((HP_STOP_PG - 1) << 8), ioaddr + 14);
241 /* Set the base address to point to the NIC, not the "real" base! */
242 dev->base_addr = ioaddr + NIC_OFFSET;
244 dev->netdev_ops = &hpp_netdev_ops;
246 ei_status.name = name;
247 ei_status.word16 = 0; /* Agggghhhhh! Debug time: 2 days! */
248 ei_status.tx_start_page = HP_START_PG;
249 ei_status.rx_start_page = HP_START_PG + TX_PAGES/2;
250 ei_status.stop_page = HP_STOP_PG;
252 ei_status.reset_8390 = &hpp_reset_8390;
253 ei_status.block_input = &hpp_io_block_input;
254 ei_status.block_output = &hpp_io_block_output;
255 ei_status.get_8390_hdr = &hpp_io_get_8390_hdr;
257 /* Check if the memory_enable flag is set in the option register. */
259 ei_status.block_input = &hpp_mem_block_input;
260 ei_status.block_output = &hpp_mem_block_output;
261 ei_status.get_8390_hdr = &hpp_mem_get_8390_hdr;
262 dev->mem_start = mem_start;
263 ei_status.mem = ioremap(mem_start,
264 (HP_STOP_PG - HP_START_PG)*256);
265 if (!ei_status.mem) {
269 ei_status.rmem_start = dev->mem_start + TX_PAGES/2*256;
270 dev->mem_end = ei_status.rmem_end
271 = dev->mem_start + (HP_STOP_PG - HP_START_PG)*256;
274 outw(Perf_Page, ioaddr + HP_PAGING);
275 NS8390p_init(dev, 0);
276 /* Leave the 8390 and HP chip reset. */
277 outw(inw(ioaddr + HPP_OPTION) & ~EnableIRQ, ioaddr + HPP_OPTION);
279 retval = register_netdev(dev);
284 iounmap(ei_status.mem);
286 release_region(ioaddr, HP_IO_EXTENT);
291 hpp_open(struct net_device *dev)
293 int ioaddr = dev->base_addr - NIC_OFFSET;
297 if ((retval = request_irq(dev->irq, eip_interrupt, 0, dev->name, dev))) {
301 /* Reset the 8390 and HP chip. */
302 option_reg = inw(ioaddr + HPP_OPTION);
303 outw(option_reg & ~(NICReset + ChipReset), ioaddr + HPP_OPTION);
305 /* Unreset the board and enable interrupts. */
306 outw(option_reg | (EnableIRQ + NICReset + ChipReset), ioaddr + HPP_OPTION);
308 /* Set the wrap registers for programmed-I/O operation. */
309 outw(HW_Page, ioaddr + HP_PAGING);
310 outw((HP_START_PG + TX_PAGES/2) | ((HP_STOP_PG - 1) << 8), ioaddr + 14);
312 /* Select the operational page. */
313 outw(Perf_Page, ioaddr + HP_PAGING);
315 return eip_open(dev);
319 hpp_close(struct net_device *dev)
321 int ioaddr = dev->base_addr - NIC_OFFSET;
322 int option_reg = inw(ioaddr + HPP_OPTION);
324 free_irq(dev->irq, dev);
326 outw((option_reg & ~EnableIRQ) | MemDisable | NICReset | ChipReset,
327 ioaddr + HPP_OPTION);
333 hpp_reset_8390(struct net_device *dev)
335 int ioaddr = dev->base_addr - NIC_OFFSET;
336 int option_reg = inw(ioaddr + HPP_OPTION);
338 if (ei_debug > 1) printk("resetting the 8390 time=%ld...", jiffies);
340 outw(option_reg & ~(NICReset + ChipReset), ioaddr + HPP_OPTION);
341 /* Pause a few cycles for the hardware reset to take place. */
344 outw(option_reg | (EnableIRQ + NICReset + ChipReset), ioaddr + HPP_OPTION);
349 if ((inb_p(ioaddr+NIC_OFFSET+EN0_ISR) & ENISR_RESET) == 0)
350 printk("%s: hp_reset_8390() did not complete.\n", dev->name);
352 if (ei_debug > 1) printk("8390 reset done (%ld).", jiffies);
356 /* The programmed-I/O version of reading the 4 byte 8390 specific header.
357 Note that transfer with the EtherTwist+ must be on word boundaries. */
360 hpp_io_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
362 int ioaddr = dev->base_addr - NIC_OFFSET;
364 outw((ring_page<<8), ioaddr + HPP_IN_ADDR);
365 insw(ioaddr + HP_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
368 /* Block input and output, similar to the Crynwr packet driver. */
371 hpp_io_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
373 int ioaddr = dev->base_addr - NIC_OFFSET;
374 char *buf = skb->data;
376 outw(ring_offset, ioaddr + HPP_IN_ADDR);
377 insw(ioaddr + HP_DATAPORT, buf, count>>1);
379 buf[count-1] = inw(ioaddr + HP_DATAPORT);
382 /* The corresponding shared memory versions of the above 2 functions. */
385 hpp_mem_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
387 int ioaddr = dev->base_addr - NIC_OFFSET;
388 int option_reg = inw(ioaddr + HPP_OPTION);
390 outw((ring_page<<8), ioaddr + HPP_IN_ADDR);
391 outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
392 memcpy_fromio(hdr, ei_status.mem, sizeof(struct e8390_pkt_hdr));
393 outw(option_reg, ioaddr + HPP_OPTION);
394 hdr->count = (le16_to_cpu(hdr->count) + 3) & ~3; /* Round up allocation. */
398 hpp_mem_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
400 int ioaddr = dev->base_addr - NIC_OFFSET;
401 int option_reg = inw(ioaddr + HPP_OPTION);
403 outw(ring_offset, ioaddr + HPP_IN_ADDR);
405 outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
407 /* Caution: this relies on get_8390_hdr() rounding up count!
408 Also note that we *can't* use eth_io_copy_and_sum() because
409 it will not always copy "count" bytes (e.g. padded IP). */
411 memcpy_fromio(skb->data, ei_status.mem, count);
412 outw(option_reg, ioaddr + HPP_OPTION);
415 /* A special note: we *must* always transfer >=16 bit words.
416 It's always safe to round up, so we do. */
418 hpp_io_block_output(struct net_device *dev, int count,
419 const unsigned char *buf, int start_page)
421 int ioaddr = dev->base_addr - NIC_OFFSET;
422 outw(start_page << 8, ioaddr + HPP_OUT_ADDR);
423 outsl(ioaddr + HP_DATAPORT, buf, (count+3)>>2);
428 hpp_mem_block_output(struct net_device *dev, int count,
429 const unsigned char *buf, int start_page)
431 int ioaddr = dev->base_addr - NIC_OFFSET;
432 int option_reg = inw(ioaddr + HPP_OPTION);
434 outw(start_page << 8, ioaddr + HPP_OUT_ADDR);
435 outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
436 memcpy_toio(ei_status.mem, buf, (count + 3) & ~3);
437 outw(option_reg, ioaddr + HPP_OPTION);
444 #define MAX_HPP_CARDS 4 /* Max number of HPP cards per module */
445 static struct net_device *dev_hpp[MAX_HPP_CARDS];
446 static int io[MAX_HPP_CARDS];
447 static int irq[MAX_HPP_CARDS];
449 module_param_array(io, int, NULL, 0);
450 module_param_array(irq, int, NULL, 0);
451 MODULE_PARM_DESC(io, "I/O port address(es)");
452 MODULE_PARM_DESC(irq, "IRQ number(s); ignored if properly detected");
453 MODULE_DESCRIPTION("HP PC-LAN+ ISA ethernet driver");
454 MODULE_LICENSE("GPL");
456 /* This is set up so that only a single autoprobe takes place per call.
457 ISA device autoprobes on a running machine are not recommended. */
461 struct net_device *dev;
462 int this_dev, found = 0;
464 for (this_dev = 0; this_dev < MAX_HPP_CARDS; this_dev++) {
465 if (io[this_dev] == 0) {
466 if (this_dev != 0) break; /* only autoprobe 1st one */
467 printk(KERN_NOTICE "hp-plus.c: Presently autoprobing (not recommended) for a single card.\n");
469 dev = alloc_ei_netdev();
472 dev->irq = irq[this_dev];
473 dev->base_addr = io[this_dev];
474 if (do_hpp_probe(dev) == 0) {
475 dev_hpp[found++] = dev;
479 printk(KERN_WARNING "hp-plus.c: No HP-Plus card found (i/o = 0x%x).\n", io[this_dev]);
487 static void cleanup_card(struct net_device *dev)
489 /* NB: hpp_close() handles free_irq */
490 iounmap(ei_status.mem);
491 release_region(dev->base_addr - NIC_OFFSET, HP_IO_EXTENT);
499 for (this_dev = 0; this_dev < MAX_HPP_CARDS; this_dev++) {
500 struct net_device *dev = dev_hpp[this_dev];
502 unregister_netdev(dev);