blob: 29917363ebfb04d534c293703df69964f987b6b0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* hp-plus.c: A HP PCLAN/plus ethernet driver for linux. */
2/*
3 Written 1994 by Donald Becker.
4
5 This driver is for the Hewlett Packard PC LAN (27***) plus ethercards.
6 These cards are sold under several model numbers, usually 2724*.
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 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.
19*/
20
21static const char version[] =
22"hp-plus.c:v1.10 9/24/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
23
24#include <linux/module.h>
25
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>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000033#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#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-plus"
42
43/* A zero-terminated list of I/O addresses to be probed. */
44static unsigned int hpplus_portlist[] __initdata =
45{0x200, 0x240, 0x280, 0x2C0, 0x300, 0x320, 0x340, 0};
46
47/*
48 The HP EtherTwist chip implementation is a fairly routine DP8390
49 implementation. It allows both shared memory and programmed-I/O buffer
50 access, using a custom interface for both. The programmed-I/O mode is
51 entirely implemented in the HP EtherTwist chip, bypassing the problem
52 ridden built-in 8390 facilities used on NE2000 designs. The shared
53 memory mode is likewise special, with an offset register used to make
54 packets appear at the shared memory base. Both modes use a base and bounds
55 page register to hide the Rx ring buffer wrap -- a packet that spans the
56 end of physical buffer memory appears continuous to the driver. (c.f. the
57 3c503 and Cabletron E2100)
58
59 A special note: the internal buffer of the board is only 8 bits wide.
60 This lays several nasty traps for the unaware:
61 - the 8390 must be programmed for byte-wide operations
62 - all I/O and memory operations must work on whole words (the access
63 latches are serially preloaded and have no byte-swapping ability).
64
65 This board is laid out in I/O space much like the earlier HP boards:
66 the first 16 locations are for the board registers, and the second 16 are
67 for the 8390. The board is easy to identify, with both a dedicated 16 bit
68 ID register and a constant 0x530* value in the upper bits of the paging
69 register.
70*/
71
72#define HP_ID 0x00 /* ID register, always 0x4850. */
73#define HP_PAGING 0x02 /* Registers visible @ 8-f, see PageName. */
74#define HPP_OPTION 0x04 /* Bitmapped options, see HP_Option. */
75#define HPP_OUT_ADDR 0x08 /* I/O output location in Perf_Page. */
76#define HPP_IN_ADDR 0x0A /* I/O input location in Perf_Page. */
77#define HP_DATAPORT 0x0c /* I/O data transfer in Perf_Page. */
78#define NIC_OFFSET 0x10 /* Offset to the 8390 registers. */
79#define HP_IO_EXTENT 32
80
81#define HP_START_PG 0x00 /* First page of TX buffer */
82#define HP_STOP_PG 0x80 /* Last page +1 of RX ring */
83
84/* The register set selected in HP_PAGING. */
85enum PageName {
86 Perf_Page = 0, /* Normal operation. */
87 MAC_Page = 1, /* The ethernet address (+checksum). */
88 HW_Page = 2, /* EEPROM-loaded hardware parameters. */
89 LAN_Page = 4, /* Transceiver selection, testing, etc. */
90 ID_Page = 6 };
91
92/* The bit definitions for the HPP_OPTION register. */
93enum HP_Option {
94 NICReset = 1, ChipReset = 2, /* Active low, really UNreset. */
95 EnableIRQ = 4, FakeIntr = 8, BootROMEnb = 0x10, IOEnb = 0x20,
96 MemEnable = 0x40, ZeroWait = 0x80, MemDisable = 0x1000, };
97
98static int hpp_probe1(struct net_device *dev, int ioaddr);
99
100static void hpp_reset_8390(struct net_device *dev);
101static int hpp_open(struct net_device *dev);
102static int hpp_close(struct net_device *dev);
103static void hpp_mem_block_input(struct net_device *dev, int count,
104 struct sk_buff *skb, int ring_offset);
105static void hpp_mem_block_output(struct net_device *dev, int count,
106 const unsigned char *buf, int start_page);
107static void hpp_mem_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
108 int ring_page);
109static void hpp_io_block_input(struct net_device *dev, int count,
110 struct sk_buff *skb, int ring_offset);
111static void hpp_io_block_output(struct net_device *dev, int count,
112 const unsigned char *buf, int start_page);
113static void hpp_io_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
114 int ring_page);
115
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* Probe a list of addresses for an HP LAN+ adaptor.
118 This routine is almost boilerplate. */
119
120static int __init do_hpp_probe(struct net_device *dev)
121{
122 int i;
123 int base_addr = dev->base_addr;
124 int irq = dev->irq;
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 if (base_addr > 0x1ff) /* Check a single specified location. */
127 return hpp_probe1(dev, base_addr);
128 else if (base_addr != 0) /* Don't probe at all. */
129 return -ENXIO;
130
131 for (i = 0; hpplus_portlist[i]; i++) {
132 if (hpp_probe1(dev, hpplus_portlist[i]) == 0)
133 return 0;
134 dev->irq = irq;
135 }
136
137 return -ENODEV;
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140#ifndef MODULE
141struct net_device * __init hp_plus_probe(int unit)
142{
Stephen Hemminger49f276b2008-09-16 16:04:12 -0700143 struct net_device *dev = alloc_eip_netdev();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int err;
145
146 if (!dev)
147 return ERR_PTR(-ENOMEM);
148
149 sprintf(dev->name, "eth%d", unit);
150 netdev_boot_setup_check(dev);
151
152 err = do_hpp_probe(dev);
153 if (err)
154 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156out:
157 free_netdev(dev);
158 return ERR_PTR(err);
159}
160#endif
161
Stephen Hemmingerd4a97d42008-11-25 18:22:42 -0800162static const struct net_device_ops hpp_netdev_ops = {
163 .ndo_open = hpp_open,
164 .ndo_stop = hpp_close,
David S. Miller5b9ab2e2008-11-26 23:48:40 -0800165 .ndo_start_xmit = eip_start_xmit,
166 .ndo_tx_timeout = eip_tx_timeout,
167 .ndo_get_stats = eip_get_stats,
168 .ndo_set_multicast_list = eip_set_multicast_list,
Stephen Hemmingerd4a97d42008-11-25 18:22:42 -0800169 .ndo_validate_addr = eth_validate_addr,
Stephen Hemmingerfe96aaa2009-01-09 11:13:14 +0000170 .ndo_set_mac_address = eth_mac_addr,
Stephen Hemmingerd4a97d42008-11-25 18:22:42 -0800171 .ndo_change_mtu = eth_change_mtu,
172#ifdef CONFIG_NET_POLL_CONTROLLER
David S. Miller5b9ab2e2008-11-26 23:48:40 -0800173 .ndo_poll_controller = eip_poll,
Stephen Hemmingerd4a97d42008-11-25 18:22:42 -0800174#endif
175};
176
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* Do the interesting part of the probe at a single address. */
179static int __init hpp_probe1(struct net_device *dev, int ioaddr)
180{
181 int i, retval;
182 unsigned char checksum = 0;
183 const char name[] = "HP-PC-LAN+";
184 int mem_start;
185 static unsigned version_printed;
186
187 if (!request_region(ioaddr, HP_IO_EXTENT, DRV_NAME))
188 return -EBUSY;
189
190 /* Check for the HP+ signature, 50 48 0x 53. */
Joe Perches8e95a202009-12-03 07:58:21 +0000191 if (inw(ioaddr + HP_ID) != 0x4850 ||
192 (inw(ioaddr + HP_PAGING) & 0xfff0) != 0x5300) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 retval = -ENODEV;
194 goto out;
195 }
196
197 if (ei_debug && version_printed++ == 0)
198 printk(version);
199
Joe Perches0795af52007-10-03 17:59:30 -0700200 printk("%s: %s at %#3x, ", dev->name, name, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /* Retrieve and checksum the station address. */
203 outw(MAC_Page, ioaddr + HP_PAGING);
204
205 for(i = 0; i < ETHER_ADDR_LEN; i++) {
206 unsigned char inval = inb(ioaddr + 8 + i);
207 dev->dev_addr[i] = inval;
208 checksum += inval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210 checksum += inb(ioaddr + 14);
211
Johannes Berge1749612008-10-27 15:59:26 -0700212 printk("%pM", dev->dev_addr);
Joe Perches0795af52007-10-03 17:59:30 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (checksum != 0xff) {
215 printk(" bad checksum %2.2x.\n", checksum);
216 retval = -ENODEV;
217 goto out;
218 } else {
219 /* Point at the Software Configuration Flags. */
220 outw(ID_Page, ioaddr + HP_PAGING);
221 printk(" ID %4.4x", inw(ioaddr + 12));
222 }
223
224 /* Read the IRQ line. */
225 outw(HW_Page, ioaddr + HP_PAGING);
226 {
227 int irq = inb(ioaddr + 13) & 0x0f;
228 int option = inw(ioaddr + HPP_OPTION);
229
230 dev->irq = irq;
231 if (option & MemEnable) {
232 mem_start = inw(ioaddr + 9) << 8;
233 printk(", IRQ %d, memory address %#x.\n", irq, mem_start);
234 } else {
235 mem_start = 0;
236 printk(", IRQ %d, programmed-I/O mode.\n", irq);
237 }
238 }
239
240 /* Set the wrap registers for string I/O reads. */
241 outw((HP_START_PG + TX_PAGES/2) | ((HP_STOP_PG - 1) << 8), ioaddr + 14);
242
243 /* Set the base address to point to the NIC, not the "real" base! */
244 dev->base_addr = ioaddr + NIC_OFFSET;
245
Stephen Hemmingerd4a97d42008-11-25 18:22:42 -0800246 dev->netdev_ops = &hpp_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 ei_status.name = name;
249 ei_status.word16 = 0; /* Agggghhhhh! Debug time: 2 days! */
250 ei_status.tx_start_page = HP_START_PG;
251 ei_status.rx_start_page = HP_START_PG + TX_PAGES/2;
252 ei_status.stop_page = HP_STOP_PG;
253
254 ei_status.reset_8390 = &hpp_reset_8390;
255 ei_status.block_input = &hpp_io_block_input;
256 ei_status.block_output = &hpp_io_block_output;
257 ei_status.get_8390_hdr = &hpp_io_get_8390_hdr;
258
259 /* Check if the memory_enable flag is set in the option register. */
260 if (mem_start) {
261 ei_status.block_input = &hpp_mem_block_input;
262 ei_status.block_output = &hpp_mem_block_output;
263 ei_status.get_8390_hdr = &hpp_mem_get_8390_hdr;
264 dev->mem_start = mem_start;
Al Virob336cea2006-03-24 03:15:40 -0800265 ei_status.mem = ioremap(mem_start,
266 (HP_STOP_PG - HP_START_PG)*256);
267 if (!ei_status.mem) {
268 retval = -ENOMEM;
269 goto out;
270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 ei_status.rmem_start = dev->mem_start + TX_PAGES/2*256;
272 dev->mem_end = ei_status.rmem_end
273 = dev->mem_start + (HP_STOP_PG - HP_START_PG)*256;
274 }
275
276 outw(Perf_Page, ioaddr + HP_PAGING);
Ingo Molnarf0084a32008-07-22 09:23:34 +0200277 NS8390p_init(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /* Leave the 8390 and HP chip reset. */
279 outw(inw(ioaddr + HPP_OPTION) & ~EnableIRQ, ioaddr + HPP_OPTION);
280
b1fc5502005-05-12 20:11:55 -0400281 retval = register_netdev(dev);
282 if (retval)
Al Virob336cea2006-03-24 03:15:40 -0800283 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0;
Al Virob336cea2006-03-24 03:15:40 -0800285out1:
286 iounmap(ei_status.mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287out:
288 release_region(ioaddr, HP_IO_EXTENT);
289 return retval;
290}
291
292static int
293hpp_open(struct net_device *dev)
294{
295 int ioaddr = dev->base_addr - NIC_OFFSET;
296 int option_reg;
297 int retval;
298
Stephen Hemminger49f276b2008-09-16 16:04:12 -0700299 if ((retval = request_irq(dev->irq, eip_interrupt, 0, dev->name, dev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return retval;
301 }
302
303 /* Reset the 8390 and HP chip. */
304 option_reg = inw(ioaddr + HPP_OPTION);
305 outw(option_reg & ~(NICReset + ChipReset), ioaddr + HPP_OPTION);
306 udelay(5);
307 /* Unreset the board and enable interrupts. */
308 outw(option_reg | (EnableIRQ + NICReset + ChipReset), ioaddr + HPP_OPTION);
309
310 /* Set the wrap registers for programmed-I/O operation. */
311 outw(HW_Page, ioaddr + HP_PAGING);
312 outw((HP_START_PG + TX_PAGES/2) | ((HP_STOP_PG - 1) << 8), ioaddr + 14);
313
314 /* Select the operational page. */
315 outw(Perf_Page, ioaddr + HP_PAGING);
316
Stephen Hemmingerd4a97d42008-11-25 18:22:42 -0800317 return eip_open(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320static int
321hpp_close(struct net_device *dev)
322{
323 int ioaddr = dev->base_addr - NIC_OFFSET;
324 int option_reg = inw(ioaddr + HPP_OPTION);
325
326 free_irq(dev->irq, dev);
Stephen Hemminger49f276b2008-09-16 16:04:12 -0700327 eip_close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 outw((option_reg & ~EnableIRQ) | MemDisable | NICReset | ChipReset,
329 ioaddr + HPP_OPTION);
330
331 return 0;
332}
333
334static void
335hpp_reset_8390(struct net_device *dev)
336{
337 int ioaddr = dev->base_addr - NIC_OFFSET;
338 int option_reg = inw(ioaddr + HPP_OPTION);
339
340 if (ei_debug > 1) printk("resetting the 8390 time=%ld...", jiffies);
341
342 outw(option_reg & ~(NICReset + ChipReset), ioaddr + HPP_OPTION);
343 /* Pause a few cycles for the hardware reset to take place. */
344 udelay(5);
345 ei_status.txing = 0;
346 outw(option_reg | (EnableIRQ + NICReset + ChipReset), ioaddr + HPP_OPTION);
347
348 udelay(5);
349
350
351 if ((inb_p(ioaddr+NIC_OFFSET+EN0_ISR) & ENISR_RESET) == 0)
352 printk("%s: hp_reset_8390() did not complete.\n", dev->name);
353
354 if (ei_debug > 1) printk("8390 reset done (%ld).", jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357/* The programmed-I/O version of reading the 4 byte 8390 specific header.
358 Note that transfer with the EtherTwist+ must be on word boundaries. */
359
360static void
361hpp_io_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
362{
363 int ioaddr = dev->base_addr - NIC_OFFSET;
364
365 outw((ring_page<<8), ioaddr + HPP_IN_ADDR);
366 insw(ioaddr + HP_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
367}
368
369/* Block input and output, similar to the Crynwr packet driver. */
370
371static void
372hpp_io_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
373{
374 int ioaddr = dev->base_addr - NIC_OFFSET;
375 char *buf = skb->data;
376
377 outw(ring_offset, ioaddr + HPP_IN_ADDR);
378 insw(ioaddr + HP_DATAPORT, buf, count>>1);
379 if (count & 0x01)
380 buf[count-1] = inw(ioaddr + HP_DATAPORT);
381}
382
383/* The corresponding shared memory versions of the above 2 functions. */
384
385static void
386hpp_mem_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
387{
388 int ioaddr = dev->base_addr - NIC_OFFSET;
389 int option_reg = inw(ioaddr + HPP_OPTION);
390
391 outw((ring_page<<8), ioaddr + HPP_IN_ADDR);
392 outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
Al Virob336cea2006-03-24 03:15:40 -0800393 memcpy_fromio(hdr, ei_status.mem, sizeof(struct e8390_pkt_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 outw(option_reg, ioaddr + HPP_OPTION);
395 hdr->count = (le16_to_cpu(hdr->count) + 3) & ~3; /* Round up allocation. */
396}
397
398static void
399hpp_mem_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
400{
401 int ioaddr = dev->base_addr - NIC_OFFSET;
402 int option_reg = inw(ioaddr + HPP_OPTION);
403
404 outw(ring_offset, ioaddr + HPP_IN_ADDR);
405
406 outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
407
408 /* Caution: this relies on get_8390_hdr() rounding up count!
409 Also note that we *can't* use eth_io_copy_and_sum() because
410 it will not always copy "count" bytes (e.g. padded IP). */
411
Al Virob336cea2006-03-24 03:15:40 -0800412 memcpy_fromio(skb->data, ei_status.mem, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 outw(option_reg, ioaddr + HPP_OPTION);
414}
415
416/* A special note: we *must* always transfer >=16 bit words.
417 It's always safe to round up, so we do. */
418static void
419hpp_io_block_output(struct net_device *dev, int count,
420 const unsigned char *buf, int start_page)
421{
422 int ioaddr = dev->base_addr - NIC_OFFSET;
423 outw(start_page << 8, ioaddr + HPP_OUT_ADDR);
424 outsl(ioaddr + HP_DATAPORT, buf, (count+3)>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427static void
428hpp_mem_block_output(struct net_device *dev, int count,
429 const unsigned char *buf, int start_page)
430{
431 int ioaddr = dev->base_addr - NIC_OFFSET;
432 int option_reg = inw(ioaddr + HPP_OPTION);
433
434 outw(start_page << 8, ioaddr + HPP_OUT_ADDR);
435 outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
Al Virob336cea2006-03-24 03:15:40 -0800436 memcpy_toio(ei_status.mem, buf, (count + 3) & ~3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 outw(option_reg, ioaddr + HPP_OPTION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441#ifdef MODULE
442#define MAX_HPP_CARDS 4 /* Max number of HPP cards per module */
443static struct net_device *dev_hpp[MAX_HPP_CARDS];
444static int io[MAX_HPP_CARDS];
445static int irq[MAX_HPP_CARDS];
446
447module_param_array(io, int, NULL, 0);
448module_param_array(irq, int, NULL, 0);
449MODULE_PARM_DESC(io, "I/O port address(es)");
450MODULE_PARM_DESC(irq, "IRQ number(s); ignored if properly detected");
451MODULE_DESCRIPTION("HP PC-LAN+ ISA ethernet driver");
452MODULE_LICENSE("GPL");
453
454/* This is set up so that only a single autoprobe takes place per call.
455ISA device autoprobes on a running machine are not recommended. */
Randy Dunlap29f9f6d2006-06-10 13:32:12 -0700456int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457init_module(void)
458{
459 struct net_device *dev;
460 int this_dev, found = 0;
461
462 for (this_dev = 0; this_dev < MAX_HPP_CARDS; this_dev++) {
463 if (io[this_dev] == 0) {
464 if (this_dev != 0) break; /* only autoprobe 1st one */
465 printk(KERN_NOTICE "hp-plus.c: Presently autoprobing (not recommended) for a single card.\n");
466 }
Randy Dunlap9b580272009-02-26 21:02:19 -0800467 dev = alloc_eip_netdev();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!dev)
469 break;
470 dev->irq = irq[this_dev];
471 dev->base_addr = io[this_dev];
472 if (do_hpp_probe(dev) == 0) {
b1fc5502005-05-12 20:11:55 -0400473 dev_hpp[found++] = dev;
474 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476 free_netdev(dev);
477 printk(KERN_WARNING "hp-plus.c: No HP-Plus card found (i/o = 0x%x).\n", io[this_dev]);
478 break;
479 }
480 if (found)
481 return 0;
482 return -ENXIO;
483}
484
Denis Vlasenko64916f12006-01-05 22:45:47 -0800485static void cleanup_card(struct net_device *dev)
486{
487 /* NB: hpp_close() handles free_irq */
Al Virob336cea2006-03-24 03:15:40 -0800488 iounmap(ei_status.mem);
Denis Vlasenko64916f12006-01-05 22:45:47 -0800489 release_region(dev->base_addr - NIC_OFFSET, HP_IO_EXTENT);
490}
491
Al Viroafc8eb42006-06-14 18:50:53 -0400492void __exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493cleanup_module(void)
494{
495 int this_dev;
496
497 for (this_dev = 0; this_dev < MAX_HPP_CARDS; this_dev++) {
498 struct net_device *dev = dev_hpp[this_dev];
499 if (dev) {
500 unregister_netdev(dev);
501 cleanup_card(dev);
502 free_netdev(dev);
503 }
504 }
505}
506#endif /* MODULE */