ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / hp.c
1 /* hp.c: A HP LAN ethernet driver for linux. */
2 /*
3         Written 1993-94 by Donald Becker.
4
5         Copyright 1993 United States Government as represented by the
6         Director, National Security Agency.
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         This is a driver for the HP PC-LAN adaptors.
17
18         Sources:
19           The Crynwr packet driver.
20 */
21
22 static const char version[] =
23         "hp.c:v1.10 9/23/94 Donald Becker (becker@cesdis.gsfc.nasa.gov)\n";
24
25
26 #include <linux/module.h>
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 hppclan_portlist[] __initdata =
42 { 0x300, 0x320, 0x340, 0x280, 0x2C0, 0x200, 0x240, 0};
43
44 #define HP_IO_EXTENT    32
45
46 #define HP_DATAPORT             0x0c    /* "Remote DMA" data port. */
47 #define HP_ID                   0x07
48 #define HP_CONFIGURE    0x08    /* Configuration register. */
49 #define  HP_RUN                 0x01    /* 1 == Run, 0 == reset. */
50 #define  HP_IRQ                 0x0E    /* Mask for software-configured IRQ line. */
51 #define  HP_DATAON              0x10    /* Turn on dataport */
52 #define NIC_OFFSET              0x10    /* Offset the 8390 registers. */
53
54 #define HP_START_PG             0x00    /* First page of TX buffer */
55 #define HP_8BSTOP_PG    0x80    /* Last page +1 of RX ring */
56 #define HP_16BSTOP_PG   0xFF    /* Same, for 16 bit cards. */
57
58 static int hp_probe1(struct net_device *dev, int ioaddr);
59
60 static int hp_open(struct net_device *dev);
61 static int hp_close(struct net_device *dev);
62 static void hp_reset_8390(struct net_device *dev);
63 static void hp_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
64                                         int ring_page);
65 static void hp_block_input(struct net_device *dev, int count,
66                                         struct sk_buff *skb , int ring_offset);
67 static void hp_block_output(struct net_device *dev, int count,
68                                                         const unsigned char *buf, int start_page);
69
70 static void hp_init_card(struct net_device *dev);
71
72 /* The map from IRQ number to HP_CONFIGURE register setting. */
73 /* My default is IRQ5                0  1  2  3  4  5  6  7  8  9 10 11 */
74 static char irqmap[16] __initdata= { 0, 0, 4, 6, 8,10, 0,14, 0, 4, 2,12,0,0,0,0};
75
76 \f
77 /*      Probe for an HP LAN adaptor.
78         Also initialize the card and fill in STATION_ADDR with the station
79         address. */
80
81 static int __init do_hp_probe(struct net_device *dev)
82 {
83         int i;
84         int base_addr = dev->base_addr;
85         int irq = dev->irq;
86
87         SET_MODULE_OWNER(dev);
88
89         if (base_addr > 0x1ff)          /* Check a single specified location. */
90                 return hp_probe1(dev, base_addr);
91         else if (base_addr != 0)        /* Don't probe at all. */
92                 return -ENXIO;
93
94         for (i = 0; hppclan_portlist[i]; i++) {
95                 if (hp_probe1(dev, hppclan_portlist[i]) == 0)
96                         return 0;
97                 dev->irq = irq;
98         }
99
100         return -ENODEV;
101 }
102
103 static void cleanup_card(struct net_device *dev)
104 {
105         free_irq(dev->irq, dev);
106         release_region(dev->base_addr - NIC_OFFSET, HP_IO_EXTENT);
107 }
108
109 struct net_device * __init hp_probe(int unit)
110 {
111         struct net_device *dev = alloc_ei_netdev();
112         int err;
113
114         if (!dev)
115                 return ERR_PTR(-ENOMEM);
116
117         sprintf(dev->name, "eth%d", unit);
118         netdev_boot_setup_check(dev);
119
120         err = do_hp_probe(dev);
121         if (err)
122                 goto out;
123         err = register_netdev(dev);
124         if (err)
125                 goto out1;
126         return dev;
127 out1:
128         cleanup_card(dev);
129 out:
130         free_netdev(dev);
131         return ERR_PTR(err);
132 }
133
134 static int __init hp_probe1(struct net_device *dev, int ioaddr)
135 {
136         int i, retval, board_id, wordmode;
137         const char *name;
138         static unsigned version_printed;
139
140         if (!request_region(ioaddr, HP_IO_EXTENT, dev->name))
141                 return -EBUSY;
142
143         /* Check for the HP physical address, 08 00 09 xx xx xx. */
144         /* This really isn't good enough: we may pick up HP LANCE boards
145            also!  Avoid the lance 0x5757 signature. */
146         if (inb(ioaddr) != 0x08
147                 || inb(ioaddr+1) != 0x00
148                 || inb(ioaddr+2) != 0x09
149                 || inb(ioaddr+14) == 0x57) {
150                 retval = -ENODEV;
151                 goto out;
152         }
153
154         /* Set up the parameters based on the board ID.
155            If you have additional mappings, please mail them to me -djb. */
156         if ((board_id = inb(ioaddr + HP_ID)) & 0x80) {
157                 name = "HP27247";
158                 wordmode = 1;
159         } else {
160                 name = "HP27250";
161                 wordmode = 0;
162         }
163
164         if (ei_debug  &&  version_printed++ == 0)
165                 printk(version);
166
167         printk("%s: %s (ID %02x) at %#3x,", dev->name, name, board_id, ioaddr);
168
169         for(i = 0; i < ETHER_ADDR_LEN; i++)
170                 printk(" %2.2x", dev->dev_addr[i] = inb(ioaddr + i));
171
172         /* Snarf the interrupt now.  Someday this could be moved to open(). */
173         if (dev->irq < 2) {
174                 int irq_16list[] = { 11, 10, 5, 3, 4, 7, 9, 0};
175                 int irq_8list[] = { 7, 5, 3, 4, 9, 0};
176                 int *irqp = wordmode ? irq_16list : irq_8list;
177                 do {
178                         int irq = *irqp;
179                         if (request_irq (irq, NULL, 0, "bogus", NULL) != -EBUSY) {
180                                 unsigned long cookie = probe_irq_on();
181                                 /* Twinkle the interrupt, and check if it's seen. */
182                                 outb_p(irqmap[irq] | HP_RUN, ioaddr + HP_CONFIGURE);
183                                 outb_p( 0x00 | HP_RUN, ioaddr + HP_CONFIGURE);
184                                 if (irq == probe_irq_off(cookie)                 /* It's a good IRQ line! */
185                                         && request_irq (irq, ei_interrupt, 0, dev->name, dev) == 0) {
186                                         printk(" selecting IRQ %d.\n", irq);
187                                         dev->irq = *irqp;
188                                         break;
189                                 }
190                         }
191                 } while (*++irqp);
192                 if (*irqp == 0) {
193                         printk(" no free IRQ lines.\n");
194                         retval = -EBUSY;
195                         goto out;
196                 }
197         } else {
198                 if (dev->irq == 2)
199                         dev->irq = 9;
200                 if ((retval = request_irq(dev->irq, ei_interrupt, 0, dev->name, dev))) {
201                         printk (" unable to get IRQ %d.\n", dev->irq);
202                         goto out;
203                 }
204         }
205
206         /* Set the base address to point to the NIC, not the "real" base! */
207         dev->base_addr = ioaddr + NIC_OFFSET;
208         dev->open = &hp_open;
209         dev->stop = &hp_close;
210 #ifdef CONFIG_NET_POLL_CONTROLLER
211         dev->poll_controller = ei_poll;
212 #endif
213
214         ei_status.name = name;
215         ei_status.word16 = wordmode;
216         ei_status.tx_start_page = HP_START_PG;
217         ei_status.rx_start_page = HP_START_PG + TX_PAGES;
218         ei_status.stop_page = wordmode ? HP_16BSTOP_PG : HP_8BSTOP_PG;
219
220         ei_status.reset_8390 = &hp_reset_8390;
221         ei_status.get_8390_hdr = &hp_get_8390_hdr;
222         ei_status.block_input = &hp_block_input;
223         ei_status.block_output = &hp_block_output;
224         hp_init_card(dev);
225
226         return 0;
227 out:
228         release_region(ioaddr, HP_IO_EXTENT);
229         return retval;
230 }
231
232 static int
233 hp_open(struct net_device *dev)
234 {
235         ei_open(dev);
236         return 0;
237 }
238
239 static int
240 hp_close(struct net_device *dev)
241 {
242         ei_close(dev);
243         return 0;
244 }
245
246 static void
247 hp_reset_8390(struct net_device *dev)
248 {
249         int hp_base = dev->base_addr - NIC_OFFSET;
250         int saved_config = inb_p(hp_base + HP_CONFIGURE);
251
252         if (ei_debug > 1) printk("resetting the 8390 time=%ld...", jiffies);
253         outb_p(0x00, hp_base + HP_CONFIGURE);
254         ei_status.txing = 0;
255         /* Pause just a few cycles for the hardware reset to take place. */
256         udelay(5);
257
258         outb_p(saved_config, hp_base + HP_CONFIGURE);
259         udelay(5);
260
261         if ((inb_p(hp_base+NIC_OFFSET+EN0_ISR) & ENISR_RESET) == 0)
262                 printk("%s: hp_reset_8390() did not complete.\n", dev->name);
263
264         if (ei_debug > 1) printk("8390 reset done (%ld).", jiffies);
265         return;
266 }
267
268 static void
269 hp_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr, int ring_page)
270 {
271         int nic_base = dev->base_addr;
272         int saved_config = inb_p(nic_base - NIC_OFFSET + HP_CONFIGURE);
273
274         outb_p(saved_config | HP_DATAON, nic_base - NIC_OFFSET + HP_CONFIGURE);
275         outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base);
276         outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
277         outb_p(0, nic_base + EN0_RCNTHI);
278         outb_p(0, nic_base + EN0_RSARLO);       /* On page boundary */
279         outb_p(ring_page, nic_base + EN0_RSARHI);
280         outb_p(E8390_RREAD+E8390_START, nic_base);
281
282         if (ei_status.word16)
283           insw(nic_base - NIC_OFFSET + HP_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr)>>1);
284         else
285           insb(nic_base - NIC_OFFSET + HP_DATAPORT, hdr, sizeof(struct e8390_pkt_hdr));
286
287         outb_p(saved_config & (~HP_DATAON), nic_base - NIC_OFFSET + HP_CONFIGURE);
288 }
289
290 /* Block input and output, similar to the Crynwr packet driver. If you are
291    porting to a new ethercard look at the packet driver source for hints.
292    The HP LAN doesn't use shared memory -- we put the packet
293    out through the "remote DMA" dataport. */
294
295 static void
296 hp_block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
297 {
298         int nic_base = dev->base_addr;
299         int saved_config = inb_p(nic_base - NIC_OFFSET + HP_CONFIGURE);
300         int xfer_count = count;
301         char *buf = skb->data;
302
303         outb_p(saved_config | HP_DATAON, nic_base - NIC_OFFSET + HP_CONFIGURE);
304         outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base);
305         outb_p(count & 0xff, nic_base + EN0_RCNTLO);
306         outb_p(count >> 8, nic_base + EN0_RCNTHI);
307         outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
308         outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
309         outb_p(E8390_RREAD+E8390_START, nic_base);
310         if (ei_status.word16) {
311           insw(nic_base - NIC_OFFSET + HP_DATAPORT,buf,count>>1);
312           if (count & 0x01)
313                 buf[count-1] = inb(nic_base - NIC_OFFSET + HP_DATAPORT), xfer_count++;
314         } else {
315                 insb(nic_base - NIC_OFFSET + HP_DATAPORT, buf, count);
316         }
317         /* This is for the ALPHA version only, remove for later releases. */
318         if (ei_debug > 0) {                     /* DMA termination address check... */
319           int high = inb_p(nic_base + EN0_RSARHI);
320           int low = inb_p(nic_base + EN0_RSARLO);
321           int addr = (high << 8) + low;
322           /* Check only the lower 8 bits so we can ignore ring wrap. */
323           if (((ring_offset + xfer_count) & 0xff) != (addr & 0xff))
324                 printk("%s: RX transfer address mismatch, %#4.4x vs. %#4.4x (actual).\n",
325                            dev->name, ring_offset + xfer_count, addr);
326         }
327         outb_p(saved_config & (~HP_DATAON), nic_base - NIC_OFFSET + HP_CONFIGURE);
328 }
329
330 static void
331 hp_block_output(struct net_device *dev, int count,
332                                 const unsigned char *buf, int start_page)
333 {
334         int nic_base = dev->base_addr;
335         int saved_config = inb_p(nic_base - NIC_OFFSET + HP_CONFIGURE);
336
337         outb_p(saved_config | HP_DATAON, nic_base - NIC_OFFSET + HP_CONFIGURE);
338         /* Round the count up for word writes.  Do we need to do this?
339            What effect will an odd byte count have on the 8390?
340            I should check someday. */
341         if (ei_status.word16 && (count & 0x01))
342           count++;
343         /* We should already be in page 0, but to be safe... */
344         outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base);
345
346 #ifdef NE8390_RW_BUGFIX
347         /* Handle the read-before-write bug the same way as the
348            Crynwr packet driver -- the NatSemi method doesn't work. */
349         outb_p(0x42, nic_base + EN0_RCNTLO);
350         outb_p(0,       nic_base + EN0_RCNTHI);
351         outb_p(0xff, nic_base + EN0_RSARLO);
352         outb_p(0x00, nic_base + EN0_RSARHI);
353 #define NE_CMD          0x00
354         outb_p(E8390_RREAD+E8390_START, nic_base + NE_CMD);
355         /* Make certain that the dummy read has occurred. */
356         inb_p(0x61);
357         inb_p(0x61);
358 #endif
359
360         outb_p(count & 0xff, nic_base + EN0_RCNTLO);
361         outb_p(count >> 8,       nic_base + EN0_RCNTHI);
362         outb_p(0x00, nic_base + EN0_RSARLO);
363         outb_p(start_page, nic_base + EN0_RSARHI);
364
365         outb_p(E8390_RWRITE+E8390_START, nic_base);
366         if (ei_status.word16) {
367                 /* Use the 'rep' sequence for 16 bit boards. */
368                 outsw(nic_base - NIC_OFFSET + HP_DATAPORT, buf, count>>1);
369         } else {
370                 outsb(nic_base - NIC_OFFSET + HP_DATAPORT, buf, count);
371         }
372
373         /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here -- it's broken! */
374
375         /* This is for the ALPHA version only, remove for later releases. */
376         if (ei_debug > 0) {                     /* DMA termination address check... */
377           int high = inb_p(nic_base + EN0_RSARHI);
378           int low  = inb_p(nic_base + EN0_RSARLO);
379           int addr = (high << 8) + low;
380           if ((start_page << 8) + count != addr)
381                 printk("%s: TX Transfer address mismatch, %#4.4x vs. %#4.4x.\n",
382                            dev->name, (start_page << 8) + count, addr);
383         }
384         outb_p(saved_config & (~HP_DATAON), nic_base - NIC_OFFSET + HP_CONFIGURE);
385         return;
386 }
387
388 /* This function resets the ethercard if something screws up. */
389 static void
390 hp_init_card(struct net_device *dev)
391 {
392         int irq = dev->irq;
393         NS8390_init(dev, 0);
394         outb_p(irqmap[irq&0x0f] | HP_RUN,
395                    dev->base_addr - NIC_OFFSET + HP_CONFIGURE);
396         return;
397 }
398
399 #ifdef MODULE
400 #define MAX_HP_CARDS    4       /* Max number of HP cards per module */
401 static struct net_device *dev_hp[MAX_HP_CARDS];
402 static int io[MAX_HP_CARDS];
403 static int irq[MAX_HP_CARDS];
404
405 MODULE_PARM(io, "1-" __MODULE_STRING(MAX_HP_CARDS) "i");
406 MODULE_PARM(irq, "1-" __MODULE_STRING(MAX_HP_CARDS) "i");
407 MODULE_PARM_DESC(io, "I/O base address(es)");
408 MODULE_PARM_DESC(irq, "IRQ number(s) (assigned)");
409 MODULE_DESCRIPTION("HP PC-LAN ISA ethernet driver");
410 MODULE_LICENSE("GPL");
411
412 /* This is set up so that only a single autoprobe takes place per call.
413 ISA device autoprobes on a running machine are not recommended. */
414 int
415 init_module(void)
416 {
417         struct net_device *dev;
418         int this_dev, found = 0;
419
420         for (this_dev = 0; this_dev < MAX_HP_CARDS; this_dev++) {
421                 if (io[this_dev] == 0)  {
422                         if (this_dev != 0) break; /* only autoprobe 1st one */
423                         printk(KERN_NOTICE "hp.c: Presently autoprobing (not recommended) for a single card.\n");
424                 }
425                 dev = alloc_ei_netdev();
426                 if (!dev)
427                         break;
428                 dev->irq = irq[this_dev];
429                 dev->base_addr = io[this_dev];
430                 if (do_hp_probe(dev) == 0) {
431                         if (register_netdev(dev) == 0) {
432                                 dev_hp[found++] = dev;
433                                 continue;
434                         }
435                         cleanup_card(dev);
436                 }
437                 free_netdev(dev);
438                 printk(KERN_WARNING "hp.c: No HP card found (i/o = 0x%x).\n", io[this_dev]);
439                 break;
440         }
441         if (found)
442                 return 0;
443         return -ENXIO;
444 }
445
446 void
447 cleanup_module(void)
448 {
449         int this_dev;
450
451         for (this_dev = 0; this_dev < MAX_HP_CARDS; this_dev++) {
452                 struct net_device *dev = dev_hp[this_dev];
453                 if (dev) {
454                         unregister_netdev(dev);
455                         cleanup_card(dev);
456                         free_netdev(dev);
457                 }
458         }
459 }
460 #endif /* MODULE */