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