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