linux 2.6.16.38 w/ vs2.0.3-rc1
[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 #define DRV_NAME "hp-plus"
41
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};
45
46 /*
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)
57
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).
63
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
68    register.
69 */
70
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
79
80 #define HP_START_PG             0x00    /* First page of TX buffer */
81 #define HP_STOP_PG              0x80    /* Last page +1 of RX ring */
82
83 /* The register set selected in HP_PAGING. */
84 enum PageName {
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. */
89         ID_Page = 6 };
90
91 /* The bit definitions for the HPP_OPTION register. */
92 enum HP_Option {
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, };
96
97 static int hpp_probe1(struct net_device *dev, int ioaddr);
98
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,
107                                                   int ring_page);
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,
113                                                   int ring_page);
114
115 \f
116 /*      Probe a list of addresses for an HP LAN+ adaptor.
117         This routine is almost boilerplate. */
118
119 static int __init do_hpp_probe(struct net_device *dev)
120 {
121         int i;
122         int base_addr = dev->base_addr;
123         int irq = dev->irq;
124
125         SET_MODULE_OWNER(dev);
126
127         if (base_addr > 0x1ff)          /* Check a single specified location. */
128                 return hpp_probe1(dev, base_addr);
129         else if (base_addr != 0)        /* Don't probe at all. */
130                 return -ENXIO;
131
132         for (i = 0; hpplus_portlist[i]; i++) {
133                 if (hpp_probe1(dev, hpplus_portlist[i]) == 0)
134                         return 0;
135                 dev->irq = irq;
136         }
137
138         return -ENODEV;
139 }
140
141 #ifndef MODULE
142 struct net_device * __init hp_plus_probe(int unit)
143 {
144         struct net_device *dev = alloc_ei_netdev();
145         int err;
146
147         if (!dev)
148                 return ERR_PTR(-ENOMEM);
149
150         sprintf(dev->name, "eth%d", unit);
151         netdev_boot_setup_check(dev);
152
153         err = do_hpp_probe(dev);
154         if (err)
155                 goto out;
156         return dev;
157 out:
158         free_netdev(dev);
159         return ERR_PTR(err);
160 }
161 #endif
162
163 /* Do the interesting part of the probe at a single address. */
164 static int __init hpp_probe1(struct net_device *dev, int ioaddr)
165 {
166         int i, retval;
167         unsigned char checksum = 0;
168         const char name[] = "HP-PC-LAN+";
169         int mem_start;
170         static unsigned version_printed;
171
172         if (!request_region(ioaddr, HP_IO_EXTENT, DRV_NAME))
173                 return -EBUSY;
174
175         /* Check for the HP+ signature, 50 48 0x 53. */
176         if (inw(ioaddr + HP_ID) != 0x4850
177                 || (inw(ioaddr + HP_PAGING) & 0xfff0) != 0x5300) {
178                 retval = -ENODEV;
179                 goto out;
180         }
181
182         if (ei_debug  &&  version_printed++ == 0)
183                 printk(version);
184
185         printk("%s: %s at %#3x,", dev->name, name, ioaddr);
186
187         /* Retrieve and checksum the station address. */
188         outw(MAC_Page, ioaddr + HP_PAGING);
189
190         for(i = 0; i < ETHER_ADDR_LEN; i++) {
191                 unsigned char inval = inb(ioaddr + 8 + i);
192                 dev->dev_addr[i] = inval;
193                 checksum += inval;
194                 printk(" %2.2x", inval);
195         }
196         checksum += inb(ioaddr + 14);
197
198         if (checksum != 0xff) {
199                 printk(" bad checksum %2.2x.\n", checksum);
200                 retval = -ENODEV;
201                 goto out;
202         } else {
203                 /* Point at the Software Configuration Flags. */
204                 outw(ID_Page, ioaddr + HP_PAGING);
205                 printk(" ID %4.4x", inw(ioaddr + 12));
206         }
207
208         /* Read the IRQ line. */
209         outw(HW_Page, ioaddr + HP_PAGING);
210         {
211                 int irq = inb(ioaddr + 13) & 0x0f;
212                 int option = inw(ioaddr + HPP_OPTION);
213
214                 dev->irq = irq;
215                 if (option & MemEnable) {
216                         mem_start = inw(ioaddr + 9) << 8;
217                         printk(", IRQ %d, memory address %#x.\n", irq, mem_start);
218                 } else {
219                         mem_start = 0;
220                         printk(", IRQ %d, programmed-I/O mode.\n", irq);
221                 }
222         }
223
224         /* Set the wrap registers for string I/O reads.   */
225         outw((HP_START_PG + TX_PAGES/2) | ((HP_STOP_PG - 1) << 8), ioaddr + 14);
226
227         /* Set the base address to point to the NIC, not the "real" base! */
228         dev->base_addr = ioaddr + NIC_OFFSET;
229
230         dev->open = &hpp_open;
231         dev->stop = &hpp_close;
232 #ifdef CONFIG_NET_POLL_CONTROLLER
233         dev->poll_controller = ei_poll;
234 #endif
235
236         ei_status.name = name;
237         ei_status.word16 = 0;           /* Agggghhhhh! Debug time: 2 days! */
238         ei_status.tx_start_page = HP_START_PG;
239         ei_status.rx_start_page = HP_START_PG + TX_PAGES/2;
240         ei_status.stop_page = HP_STOP_PG;
241
242         ei_status.reset_8390 = &hpp_reset_8390;
243         ei_status.block_input = &hpp_io_block_input;
244         ei_status.block_output = &hpp_io_block_output;
245         ei_status.get_8390_hdr = &hpp_io_get_8390_hdr;
246
247         /* Check if the memory_enable flag is set in the option register. */
248         if (mem_start) {
249                 ei_status.block_input = &hpp_mem_block_input;
250                 ei_status.block_output = &hpp_mem_block_output;
251                 ei_status.get_8390_hdr = &hpp_mem_get_8390_hdr;
252                 dev->mem_start = mem_start;
253                 ei_status.rmem_start = dev->mem_start + TX_PAGES/2*256;
254                 dev->mem_end = ei_status.rmem_end
255                         = dev->mem_start + (HP_STOP_PG - HP_START_PG)*256;
256         }
257
258         outw(Perf_Page, ioaddr + HP_PAGING);
259         NS8390_init(dev, 0);
260         /* Leave the 8390 and HP chip reset. */
261         outw(inw(ioaddr + HPP_OPTION) & ~EnableIRQ, ioaddr + HPP_OPTION);
262
263         retval = register_netdev(dev);
264         if (retval)
265                 goto out;
266         return 0;
267 out:
268         release_region(ioaddr, HP_IO_EXTENT);
269         return retval;
270 }
271
272 static int
273 hpp_open(struct net_device *dev)
274 {
275         int ioaddr = dev->base_addr - NIC_OFFSET;
276         int option_reg;
277         int retval;
278
279         if ((retval = request_irq(dev->irq, ei_interrupt, 0, dev->name, dev))) {
280             return retval;
281         }
282
283         /* Reset the 8390 and HP chip. */
284         option_reg = inw(ioaddr + HPP_OPTION);
285         outw(option_reg & ~(NICReset + ChipReset), ioaddr + HPP_OPTION);
286         udelay(5);
287         /* Unreset the board and enable interrupts. */
288         outw(option_reg | (EnableIRQ + NICReset + ChipReset), ioaddr + HPP_OPTION);
289
290         /* Set the wrap registers for programmed-I/O operation.   */
291         outw(HW_Page, ioaddr + HP_PAGING);
292         outw((HP_START_PG + TX_PAGES/2) | ((HP_STOP_PG - 1) << 8), ioaddr + 14);
293
294         /* Select the operational page. */
295         outw(Perf_Page, ioaddr + HP_PAGING);
296
297         ei_open(dev);
298         return 0;
299 }
300
301 static int
302 hpp_close(struct net_device *dev)
303 {
304         int ioaddr = dev->base_addr - NIC_OFFSET;
305         int option_reg = inw(ioaddr + HPP_OPTION);
306
307         free_irq(dev->irq, dev);
308         ei_close(dev);
309         outw((option_reg & ~EnableIRQ) | MemDisable | NICReset | ChipReset,
310                  ioaddr + HPP_OPTION);
311
312         return 0;
313 }
314
315 static void
316 hpp_reset_8390(struct net_device *dev)
317 {
318         int ioaddr = dev->base_addr - NIC_OFFSET;
319         int option_reg = inw(ioaddr + HPP_OPTION);
320
321         if (ei_debug > 1) printk("resetting the 8390 time=%ld...", jiffies);
322
323         outw(option_reg & ~(NICReset + ChipReset), ioaddr + HPP_OPTION);
324         /* Pause a few cycles for the hardware reset to take place. */
325         udelay(5);
326         ei_status.txing = 0;
327         outw(option_reg | (EnableIRQ + NICReset + ChipReset), ioaddr + HPP_OPTION);
328
329         udelay(5);
330
331
332         if ((inb_p(ioaddr+NIC_OFFSET+EN0_ISR) & ENISR_RESET) == 0)
333                 printk("%s: hp_reset_8390() did not complete.\n", dev->name);
334
335         if (ei_debug > 1) printk("8390 reset done (%ld).", jiffies);
336         return;
337 }
338
339 /* The programmed-I/O version of reading the 4 byte 8390 specific header.
340    Note that transfer with the EtherTwist+ must be on word boundaries. */
341
342 static void
343 hpp_io_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
344 {
345         int ioaddr = dev->base_addr - NIC_OFFSET;
346
347         outw((ring_page<<8), ioaddr + HPP_IN_ADDR);
348         insw(ioaddr + HP_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
349 }
350
351 /* Block input and output, similar to the Crynwr packet driver. */
352
353 static void
354 hpp_io_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
355 {
356         int ioaddr = dev->base_addr - NIC_OFFSET;
357         char *buf = skb->data;
358
359         outw(ring_offset, ioaddr + HPP_IN_ADDR);
360         insw(ioaddr + HP_DATAPORT, buf, count>>1);
361         if (count & 0x01)
362         buf[count-1] = inw(ioaddr + HP_DATAPORT);
363 }
364
365 /* The corresponding shared memory versions of the above 2 functions. */
366
367 static void
368 hpp_mem_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
369 {
370         int ioaddr = dev->base_addr - NIC_OFFSET;
371         int option_reg = inw(ioaddr + HPP_OPTION);
372
373         outw((ring_page<<8), ioaddr + HPP_IN_ADDR);
374         outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
375         isa_memcpy_fromio(hdr, dev->mem_start, sizeof(struct e8390_pkt_hdr));
376         outw(option_reg, ioaddr + HPP_OPTION);
377         hdr->count = (le16_to_cpu(hdr->count) + 3) & ~3;        /* Round up allocation. */
378 }
379
380 static void
381 hpp_mem_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
382 {
383         int ioaddr = dev->base_addr - NIC_OFFSET;
384         int option_reg = inw(ioaddr + HPP_OPTION);
385
386         outw(ring_offset, ioaddr + HPP_IN_ADDR);
387
388         outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
389
390         /* Caution: this relies on get_8390_hdr() rounding up count!
391            Also note that we *can't* use eth_io_copy_and_sum() because
392            it will not always copy "count" bytes (e.g. padded IP).  */
393
394         isa_memcpy_fromio(skb->data, dev->mem_start, count);
395         outw(option_reg, ioaddr + HPP_OPTION);
396 }
397
398 /* A special note: we *must* always transfer >=16 bit words.
399    It's always safe to round up, so we do. */
400 static void
401 hpp_io_block_output(struct net_device *dev, int count,
402                                         const unsigned char *buf, int start_page)
403 {
404         int ioaddr = dev->base_addr - NIC_OFFSET;
405         outw(start_page << 8, ioaddr + HPP_OUT_ADDR);
406         outsl(ioaddr + HP_DATAPORT, buf, (count+3)>>2);
407         return;
408 }
409
410 static void
411 hpp_mem_block_output(struct net_device *dev, int count,
412                                 const unsigned char *buf, int start_page)
413 {
414         int ioaddr = dev->base_addr - NIC_OFFSET;
415         int option_reg = inw(ioaddr + HPP_OPTION);
416
417         outw(start_page << 8, ioaddr + HPP_OUT_ADDR);
418         outw(option_reg & ~(MemDisable + BootROMEnb), ioaddr + HPP_OPTION);
419         isa_memcpy_toio(dev->mem_start, buf, (count + 3) & ~3);
420         outw(option_reg, ioaddr + HPP_OPTION);
421
422         return;
423 }
424
425 \f
426 #ifdef MODULE
427 #define MAX_HPP_CARDS   4       /* Max number of HPP cards per module */
428 static struct net_device *dev_hpp[MAX_HPP_CARDS];
429 static int io[MAX_HPP_CARDS];
430 static int irq[MAX_HPP_CARDS];
431
432 module_param_array(io, int, NULL, 0);
433 module_param_array(irq, int, NULL, 0);
434 MODULE_PARM_DESC(io, "I/O port address(es)");
435 MODULE_PARM_DESC(irq, "IRQ number(s); ignored if properly detected");
436 MODULE_DESCRIPTION("HP PC-LAN+ ISA ethernet driver");
437 MODULE_LICENSE("GPL");
438
439 /* This is set up so that only a single autoprobe takes place per call.
440 ISA device autoprobes on a running machine are not recommended. */
441 int
442 init_module(void)
443 {
444         struct net_device *dev;
445         int this_dev, found = 0;
446
447         for (this_dev = 0; this_dev < MAX_HPP_CARDS; this_dev++) {
448                 if (io[this_dev] == 0)  {
449                         if (this_dev != 0) break; /* only autoprobe 1st one */
450                         printk(KERN_NOTICE "hp-plus.c: Presently autoprobing (not recommended) for a single card.\n");
451                 }
452                 dev = alloc_ei_netdev();
453                 if (!dev)
454                         break;
455                 dev->irq = irq[this_dev];
456                 dev->base_addr = io[this_dev];
457                 if (do_hpp_probe(dev) == 0) {
458                         dev_hpp[found++] = dev;
459                         continue;
460                 }
461                 free_netdev(dev);
462                 printk(KERN_WARNING "hp-plus.c: No HP-Plus card found (i/o = 0x%x).\n", io[this_dev]);
463                 break;
464         }
465         if (found)
466                 return 0;
467         return -ENXIO;
468 }
469
470 static void cleanup_card(struct net_device *dev)
471 {
472         /* NB: hpp_close() handles free_irq */
473         release_region(dev->base_addr - NIC_OFFSET, HP_IO_EXTENT);
474 }
475
476 void
477 cleanup_module(void)
478 {
479         int this_dev;
480
481         for (this_dev = 0; this_dev < MAX_HPP_CARDS; this_dev++) {
482                 struct net_device *dev = dev_hpp[this_dev];
483                 if (dev) {
484                         unregister_netdev(dev);
485                         cleanup_card(dev);
486                         free_netdev(dev);
487                 }
488         }
489 }
490 #endif /* MODULE */