ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / hp-plus.c
1 /* 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
21 static 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>
33 #include <linux/delay.h>
34
35 #include <asm/system.h>
36 #include <asm/io.h>
37
38 #include "8390.h"
39
40 /* A zero-terminated list of I/O addresses to be probed. */
41 static unsigned int hpplus_portlist[] __initdata =
42 {0x200, 0x240, 0x280, 0x2C0, 0x300, 0x320, 0x340, 0};
43
44 /*
45    The HP EtherTwist chip implementation is a fairly routine DP8390
46    implementation.  It allows both shared memory and programmed-I/O buffer
47    access, using a custom interface for both.  The programmed-I/O mode is
48    entirely implemented in the HP EtherTwist chip, bypassing the problem
49    ridden built-in 8390 facilities used on NE2000 designs.  The shared
50    memory mode is likewise special, with an offset register used to make
51    packets appear at the shared memory base.  Both modes use a base and bounds
52    page register to hide the Rx ring buffer wrap -- a packet that spans the
53    end of physical buffer memory appears continuous to the driver. (c.f. the
54    3c503 and Cabletron E2100)
55
56    A special note: the internal buffer of the board is only 8 bits wide.
57    This lays several nasty traps for the unaware:
58    - the 8390 must be programmed for byte-wide operations
59    - all I/O and memory operations must work on whole words (the access
60      latches are serially preloaded and have no byte-swapping ability).
61
62    This board is laid out in I/O space much like the earlier HP boards:
63    the first 16 locations are for the board registers, and the second 16 are
64    for the 8390.  The board is easy to identify, with both a dedicated 16 bit
65    ID register and a constant 0x530* value in the upper bits of the paging
66    register.
67 */
68
69 #define HP_ID                   0x00    /* ID register, always 0x4850. */
70 #define HP_PAGING               0x02    /* Registers visible @ 8-f, see PageName. */
71 #define HPP_OPTION              0x04    /* Bitmapped options, see HP_Option.    */
72 #define HPP_OUT_ADDR    0x08    /* I/O output location in Perf_Page.    */
73 #define HPP_IN_ADDR             0x0A    /* I/O input location in Perf_Page.             */
74 #define HP_DATAPORT             0x0c    /* I/O data transfer in Perf_Page.              */
75 #define NIC_OFFSET              0x10    /* Offset to the 8390 registers.                */
76 #define HP_IO_EXTENT    32
77
78 #define HP_START_PG             0x00    /* First page of TX buffer */
79 #define HP_STOP_PG              0x80    /* Last page +1 of RX ring */
80
81 /* The register set selected in HP_PAGING. */
82 enum PageName {
83         Perf_Page = 0,                          /* Normal operation. */
84         MAC_Page = 1,                           /* The ethernet address (+checksum). */
85         HW_Page = 2,                            /* EEPROM-loaded hardware parameters. */
86         LAN_Page = 4,                           /* Transceiver selection, testing, etc. */
87         ID_Page = 6 };
88
89 /* The bit definitions for the HPP_OPTION register. */
90 enum HP_Option {
91         NICReset = 1, ChipReset = 2,    /* Active low, really UNreset. */
92         EnableIRQ = 4, FakeIntr = 8, BootROMEnb = 0x10, IOEnb = 0x20,
93         MemEnable = 0x40, ZeroWait = 0x80, MemDisable = 0x1000, };
94
95 static int hpp_probe1(struct net_device *dev, int ioaddr);
96
97 static void hpp_reset_8390(struct net_device *dev);
98 static int hpp_open(struct net_device *dev);
99 static int hpp_close(struct net_device *dev);
100 static void hpp_mem_block_input(struct net_device *dev, int count,
101                                                   struct sk_buff *skb, int ring_offset);
102 static void hpp_mem_block_output(struct net_device *dev, int count,
103                                                         const unsigned char *buf, int start_page);
104 static void hpp_mem_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
105                                                   int ring_page);
106 static void hpp_io_block_input(struct net_device *dev, int count,
107                                                   struct sk_buff *skb, int ring_offset);
108 static void hpp_io_block_output(struct net_device *dev, int count,
109                                                         const unsigned char *buf, int start_page);
110 static void hpp_io_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
111                                                   int ring_page);
112
113 \f
114 /*      Probe a list of addresses for an HP LAN+ adaptor.
115         This routine is almost boilerplate. */
116
117 static int __init do_hpp_probe(struct net_device *dev)
118 {
119         int i;
120         int base_addr = dev->base_addr;
121         int irq = dev->irq;
122
123         SET_MODULE_OWNER(dev);
124
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. */
128                 return -ENXIO;
129
130         for (i = 0; hpplus_portlist[i]; i++) {
131                 if (hpp_probe1(dev, hpplus_portlist[i]) == 0)
132                         return 0;
133                 dev->irq = irq;
134         }
135
136         return -ENODEV;
137 }
138
139 static void cleanup_card(struct net_device *dev)
140 {
141         /* NB: hpp_close() handles free_irq */
142         release_region(dev->base_addr - NIC_OFFSET, HP_IO_EXTENT);
143 }
144
145 struct net_device * __init hp_plus_probe(int unit)
146 {
147         struct net_device *dev = alloc_ei_netdev();
148         int err;
149
150         if (!dev)
151                 return ERR_PTR(-ENOMEM);
152
153         sprintf(dev->name, "eth%d", unit);
154         netdev_boot_setup_check(dev);
155
156         err = do_hpp_probe(dev);
157         if (err)
158                 goto out;
159         err = register_netdev(dev);
160         if (err)
161                 goto out1;
162         return dev;
163 out1:
164         cleanup_card(dev);
165 out:
166         free_netdev(dev);
167         return ERR_PTR(err);
168 }
169
170 /* Do the interesting part of the probe at a single address. */
171 static int __init hpp_probe1(struct net_device *dev, int ioaddr)
172 {
173         int i, retval;
174         unsigned char checksum = 0;
175         const char name[] = "HP-PC-LAN+";
176         int mem_start;
177         static unsigned version_printed;
178
179         if (!request_region(ioaddr, HP_IO_EXTENT, dev->name))
180                 return -EBUSY;
181
182         /* Check for the HP+ signature, 50 48 0x 53. */
183         if (inw(ioaddr + HP_ID) != 0x4850
184                 || (inw(ioaddr + HP_PAGING) & 0xfff0) != 0x5300) {
185                 retval = -ENODEV;
186                 goto out;
187         }
188
189         if (ei_debug  &&  version_printed++ == 0)
190                 printk(version);
191
192         printk("%s: %s at %#3x,", dev->name, name, ioaddr);
193
194         /* Retrieve and checksum the station address. */
195         outw(MAC_Page, ioaddr + HP_PAGING);
196
197         for(i = 0; i < ETHER_ADDR_LEN; i++) {
198                 unsigned char inval = inb(ioaddr + 8 + i);
199                 dev->dev_addr[i] = inval;
200                 checksum += inval;
201                 printk(" %2.2x", inval);
202         }
203         checksum += inb(ioaddr + 14);
204
205         if (checksum != 0xff) {
206                 printk(" bad checksum %2.2x.\n", checksum);
207                 retval = -ENODEV;
208                 goto out;
209         } else {
210                 /* Point at the Software Configuration Flags. */
211                 outw(ID_Page, ioaddr + HP_PAGING);
212                 printk(" ID %4.4x", inw(ioaddr + 12));
213         }
214
215         /* Read the IRQ line. */
216         outw(HW_Page, ioaddr + HP_PAGING);
217         {
218                 int irq = inb(ioaddr + 13) & 0x0f;
219                 int option = inw(ioaddr + HPP_OPTION);
220
221                 dev->irq = irq;
222                 if (option & MemEnable) {
223                         mem_start = inw(ioaddr + 9) << 8;
224                         printk(", IRQ %d, memory address %#x.\n", irq, mem_start);
225                 } else {
226                         mem_start = 0;
227                         printk(", IRQ %d, programmed-I/O mode.\n", irq);
228                 }
229         }
230
231         /* Set the wrap registers for string I/O reads.   */
232         outw((HP_START_PG + TX_2X_PAGES) | ((HP_STOP_PG - 1) << 8), ioaddr + 14);
233
234         /* Set the base address to point to the NIC, not the "real" base! */
235         dev->base_addr = ioaddr + NIC_OFFSET;
236
237         dev->open = &hpp_open;
238         dev->stop = &hpp_close;
239 #ifdef CONFIG_NET_POLL_CONTROLLER
240         dev->poll_controller = ei_poll;
241 #endif
242
243         ei_status.name = name;
244         ei_status.word16 = 0;           /* Agggghhhhh! Debug time: 2 days! */
245         ei_status.tx_start_page = HP_START_PG;
246         ei_status.rx_start_page = HP_START_PG + TX_2X_PAGES;
247         ei_status.stop_page = HP_STOP_PG;
248
249         ei_status.reset_8390 = &hpp_reset_8390;
250         ei_status.block_input = &hpp_io_block_input;
251         ei_status.block_output = &hpp_io_block_output;
252         ei_status.get_8390_hdr = &hpp_io_get_8390_hdr;
253
254         /* Check if the memory_enable flag is set in the option register. */
255         if (mem_start) {
256                 ei_status.block_input = &hpp_mem_block_input;
257                 ei_status.block_output = &hpp_mem_block_output;
258                 ei_status.get_8390_hdr = &hpp_mem_get_8390_hdr;
259                 dev->mem_start = mem_start;
260                 ei_status.rmem_start = dev->mem_start + TX_2X_PAGES*256;
261                 dev->mem_end = ei_status.rmem_end
262                         = dev->mem_start + (HP_STOP_PG - HP_START_PG)*256;
263         }
264
265         outw(Perf_Page, ioaddr + HP_PAGING);
266         NS8390_init(dev, 0);
267         /* Leave the 8390 and HP chip reset. */
268         outw(inw(ioaddr + HPP_OPTION) & ~EnableIRQ, ioaddr + HPP_OPTION);
269
270         return 0;
271 out:
272         release_region(ioaddr, HP_IO_EXTENT);
273         return retval;
274 }
275
276 static int
277 hpp_open(struct net_device *dev)
278 {
279         int ioaddr = dev->base_addr - NIC_OFFSET;
280         int option_reg;
281         int retval;
282
283         if ((retval = request_irq(dev->irq, ei_interrupt, 0, dev->name, dev))) {
284             return retval;
285         }
286
287         /* Reset the 8390 and HP chip. */
288         option_reg = inw(ioaddr + HPP_OPTION);
289         outw(option_reg & ~(NICReset + ChipReset), ioaddr + HPP_OPTION);
290         udelay(5);
291         /* Unreset the board and enable interrupts. */
292         outw(option_reg | (EnableIRQ + NICReset + ChipReset), ioaddr + HPP_OPTION);
293
294         /* Set the wrap registers for programmed-I/O operation.   */
295         outw(HW_Page, ioaddr + HP_PAGING);
296         outw((HP_START_PG + TX_2X_PAGES) | ((HP_STOP_PG - 1) << 8), ioaddr + 14);
297
298         /* Select the operational page. */
299         outw(Perf_Page, ioaddr + HP_PAGING);
300
301         ei_open(dev);
302         return 0;
303 }
304
305 static int
306 hpp_close(struct net_device *dev)
307 {
308         int ioaddr = dev->base_addr - NIC_OFFSET;
309         int option_reg = inw(ioaddr + HPP_OPTION);
310
311         free_irq(dev->irq, dev);
312         ei_close(dev);
313         outw((option_reg & ~EnableIRQ) | MemDisable | NICReset | ChipReset,
314                  ioaddr + HPP_OPTION);
315
316         return 0;
317 }
318
319 static void
320 hpp_reset_8390(struct net_device *dev)
321 {
322         int ioaddr = dev->base_addr - NIC_OFFSET;
323         int option_reg = inw(ioaddr + HPP_OPTION);
324
325         if (ei_debug > 1) printk("resetting the 8390 time=%ld...", jiffies);
326
327         outw(option_reg & ~(NICReset + ChipReset), ioaddr + HPP_OPTION);
328         /* Pause a few cycles for the hardware reset to take place. */
329         udelay(5);
330         ei_status.txing = 0;
331         outw(option_reg | (EnableIRQ + NICReset + ChipReset), ioaddr + HPP_OPTION);
332
333         udelay(5);
334
335
336         if ((inb_p(ioaddr+NIC_OFFSET+EN0_ISR) & ENISR_RESET) == 0)
337                 printk("%s: hp_reset_8390() did not complete.\n", dev->name);
338
339         if (ei_debug > 1) printk("8390 reset done (%ld).", jiffies);
340         return;
341 }
342
343 /* The programmed-I/O version of reading the 4 byte 8390 specific header.
344    Note that transfer with the EtherTwist+ must be on word boundaries. */
345
346 static void
347 hpp_io_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
348 {
349         int ioaddr = dev->base_addr - NIC_OFFSET;
350
351         outw((ring_page<<8), ioaddr + HPP_IN_ADDR);
352         insw(ioaddr + HP_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
353 }
354
355 /* Block input and output, similar to the Crynwr packet driver. */
356
357 static void
358 hpp_io_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
359 {
360         int ioaddr = dev->base_addr - NIC_OFFSET;
361         char *buf = skb->data;
362
363         outw(ring_offset, ioaddr + HPP_IN_ADDR);
364         insw(ioaddr + HP_DATAPORT, buf, count>>1);
365         if (count & 0x01)
366         buf[count-1] = inw(ioaddr + HP_DATAPORT);
367 }
368
369 /* The corresponding shared memory versions of the above 2 functions. */
370
371 static void
372 hpp_mem_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
373 {
374         int ioaddr = dev->base_addr - NIC_OFFSET;
375         int option_reg = inw(ioaddr + HPP_OPTION);
376
377         outw((ring_page<<8), ioaddr + HPP_IN_ADDR);
378         outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
379         isa_memcpy_fromio(hdr, dev->mem_start, sizeof(struct e8390_pkt_hdr));
380         outw(option_reg, ioaddr + HPP_OPTION);
381         hdr->count = (le16_to_cpu(hdr->count) + 3) & ~3;        /* Round up allocation. */
382 }
383
384 static void
385 hpp_mem_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
386 {
387         int ioaddr = dev->base_addr - NIC_OFFSET;
388         int option_reg = inw(ioaddr + HPP_OPTION);
389
390         outw(ring_offset, ioaddr + HPP_IN_ADDR);
391
392         outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
393
394         /* Caution: this relies on get_8390_hdr() rounding up count!
395            Also note that we *can't* use eth_io_copy_and_sum() because
396            it will not always copy "count" bytes (e.g. padded IP).  */
397
398         isa_memcpy_fromio(skb->data, dev->mem_start, count);
399         outw(option_reg, ioaddr + HPP_OPTION);
400 }
401
402 /* A special note: we *must* always transfer >=16 bit words.
403    It's always safe to round up, so we do. */
404 static void
405 hpp_io_block_output(struct net_device *dev, int count,
406                                         const unsigned char *buf, int start_page)
407 {
408         int ioaddr = dev->base_addr - NIC_OFFSET;
409         outw(start_page << 8, ioaddr + HPP_OUT_ADDR);
410         outsl(ioaddr + HP_DATAPORT, buf, (count+3)>>2);
411         return;
412 }
413
414 static void
415 hpp_mem_block_output(struct net_device *dev, int count,
416                                 const unsigned char *buf, int start_page)
417 {
418         int ioaddr = dev->base_addr - NIC_OFFSET;
419         int option_reg = inw(ioaddr + HPP_OPTION);
420
421         outw(start_page << 8, ioaddr + HPP_OUT_ADDR);
422         outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
423         isa_memcpy_toio(dev->mem_start, buf, (count + 3) & ~3);
424         outw(option_reg, ioaddr + HPP_OPTION);
425
426         return;
427 }
428
429 \f
430 #ifdef MODULE
431 #define MAX_HPP_CARDS   4       /* Max number of HPP cards per module */
432 static struct net_device *dev_hpp[MAX_HPP_CARDS];
433 static int io[MAX_HPP_CARDS];
434 static int irq[MAX_HPP_CARDS];
435
436 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HPP_CARDS) "i");
437 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_HPP_CARDS) "i");
438 MODULE_PARM_DESC(io, "I/O port address(es)");
439 MODULE_PARM_DESC(irq, "IRQ number(s); ignored if properly detected");
440 MODULE_DESCRIPTION("HP PC-LAN+ ISA ethernet driver");
441 MODULE_LICENSE("GPL");
442
443 /* This is set up so that only a single autoprobe takes place per call.
444 ISA device autoprobes on a running machine are not recommended. */
445 int
446 init_module(void)
447 {
448         struct net_device *dev;
449         int this_dev, found = 0;
450
451         for (this_dev = 0; this_dev < MAX_HPP_CARDS; this_dev++) {
452                 if (io[this_dev] == 0)  {
453                         if (this_dev != 0) break; /* only autoprobe 1st one */
454                         printk(KERN_NOTICE "hp-plus.c: Presently autoprobing (not recommended) for a single card.\n");
455                 }
456                 dev = alloc_ei_netdev();
457                 if (!dev)
458                         break;
459                 dev->irq = irq[this_dev];
460                 dev->base_addr = io[this_dev];
461                 if (do_hpp_probe(dev) == 0) {
462                         if (register_netdev(dev) == 0) {
463                                 dev_hpp[found++] = dev;
464                                 continue;
465                         }
466                         cleanup_card(dev);
467                 }
468                 free_netdev(dev);
469                 printk(KERN_WARNING "hp-plus.c: No HP-Plus card found (i/o = 0x%x).\n", io[this_dev]);
470                 break;
471         }
472         if (found)
473                 return 0;
474         return -ENXIO;
475 }
476
477 void
478 cleanup_module(void)
479 {
480         int this_dev;
481
482         for (this_dev = 0; this_dev < MAX_HPP_CARDS; this_dev++) {
483                 struct net_device *dev = dev_hpp[this_dev];
484                 if (dev) {
485                         unregister_netdev(dev);
486                         cleanup_card(dev);
487                         free_netdev(dev);
488                 }
489         }
490 }
491 #endif /* MODULE */