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