2 * Copyright (c) 2003 Matthew N. Dodd
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Copyright (c) 1992, 1993, University of Vermont and State
28 * Agricultural College.
29 * Copyright (c) 1992, 1993, Garrett A. Wollman.
30 * Copyright (c) 1990, 1991, William F. Jolitz
31 * Copyright (c) 1990, The Regents of the University of California
32 * Copyright (c) 1993, 1994, Charles M. Hannum
33 * Copyright (c) 1993, 1994, 1995, Rodney W. Grimes
34 * Copyright (c) 1997, Aaron C. Smith
36 * See if_ie.c for applicable license.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/socket.h>
50 #include <sys/module.h>
53 #include <machine/bus.h>
54 #include <machine/resource.h>
57 #include <machine/md_var.h>
60 #include <net/if_media.h>
62 #include <isa/isavar.h>
63 #include <isa/pnpvar.h>
65 #include <i386/isa/elink.h>
67 #include <dev/ic/i82586.h>
68 #include <dev/ie/if_ie507.h>
69 #include <dev/ie/if_iee16.h>
70 #include <dev/ie/if_iereg.h>
71 #include <dev/ie/if_ievar.h>
73 static int ie_modevent (module_t, int, void *);
75 static void ie_isa_3C507_identify (driver_t *, device_t);
76 static int ie_isa_3C507_probe (device_t);
77 static int ie_isa_3C507_attach (device_t);
78 static int ie_3C507_port_check (u_int32_t);
80 static void ie_isa_ee16_identify (driver_t *, device_t);
81 static int ie_isa_ee16_probe (device_t);
82 static int ie_isa_ee16_attach (device_t);
83 static int ie_isa_ee16_shutdown (device_t);
84 static int ie_ee16_port_check (u_int32_t port);
85 static u_int16_t ie_ee16_hw_read_eeprom (u_int32_t port, int loc);
87 static int ie_isa_sl_probe (device_t);
88 static int ie_isa_sl_attach (device_t);
89 static enum ie_hardware ie_isa_sl_get_hard_type (u_int32_t);
92 * 3Com 3C507 Etherlink 16
94 #define IE_3C507_IOBASE_LOW 0x200
95 #define IE_3C507_IOBASE_HIGH 0x3e0
96 #define IE_3C507_IOSIZE 16
98 #define IE_3C507_IRQ_MASK 0x0f
100 #define IE_3C507_MADDR_HIGH 0x20
101 #define IE_3C507_MADDR_MASK 0x1c
102 #define IE_3C507_MADDR_BASE 0xc0000
103 #define IE_3C507_MADDR_SHIFT 12
105 #define IE_3C507_MSIZE_MASK 3
106 #define IE_3C507_MSIZE_SHIFT 14
109 ie_isa_3C507_identify (driver_t *driver, device_t parent)
111 char * desc = "3Com 3C507 Etherlink 16";
113 u_int32_t port, maddr, msize;
117 /* Reset and put card in CONFIG state without changing address. */
119 elink_idseq(ELINK_507_POLY);
120 elink_idseq(ELINK_507_POLY);
121 outb(ELINK_ID_PORT, 0xff);
123 for (port = IE_3C507_IOBASE_LOW;
124 port <= IE_3C507_IOBASE_HIGH;
125 port += IE_3C507_IOSIZE) {
127 if (ie_3C507_port_check(port)) {
130 device_printf(parent,
131 "(if_ie) (3C507) not found at port %#x\n",
138 outb(port + IE507_CTRL, EL_CTRL_NRST);
140 data = inb(port + IE507_IRQ);
141 irq = data & IE_3C507_IRQ_MASK;
143 data = inb(port + IE507_MADDR);
145 if (data & IE_3C507_MADDR_HIGH) {
147 device_printf(parent,
148 "(if_ie) can't map 3C507 RAM in high memory\n");
153 maddr = IE_3C507_MADDR_BASE +
154 ((data & IE_3C507_MADDR_MASK)
155 << IE_3C507_MADDR_SHIFT);
156 msize = ((data & IE_3C507_MSIZE_MASK) + 1)
157 << IE_3C507_MSIZE_SHIFT;
159 child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ie", -1);
160 device_set_desc_copy(child, desc);
161 device_set_driver(child, driver);
163 error = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
165 device_printf(parent, "(if_ie) Unable to set IRQ resource %d.\n",
167 error = device_delete_child(parent, child);
171 error = bus_set_resource(child, SYS_RES_IOPORT, 0, port, IE_3C507_IOSIZE);
173 device_printf(parent, "(if_ie) Unable to set IOPORT resource %#x-%#x.\n",
174 port, port+IE_3C507_IOSIZE);
175 error = device_delete_child(parent, child);
179 error = bus_set_resource(child, SYS_RES_MEMORY, 0, maddr, msize);
181 device_printf(parent, "(if_ie) Unable to set MEMORY resource %#x-%#x.\n",
183 error = device_delete_child(parent, child);
188 device_printf(parent,
189 "(if_ie) <%s> at port %#x-%#x irq %d iomem %#lx-%#lx (%dKB)\n",
191 port, (port + IE_3C507_IOSIZE) - 1,
193 (u_long)maddr, (u_long)(maddr + msize) - 1,
198 /* go to RUN state */
199 outb(ELINK_ID_PORT, 0x00);
200 elink_idseq(ELINK_507_POLY);
201 outb(ELINK_ID_PORT, 0x00);
207 ie_isa_3C507_probe (device_t dev)
211 /* No ISA-PnP support */
212 if (isa_get_vendorid(dev)) {
216 /* No ISA-HINT support */
217 if (!device_get_desc(dev)) {
221 /* Have we at least an ioport? */
222 if ((iobase = bus_get_resource_start(dev, SYS_RES_IOPORT, 0)) == 0) {
226 /* Is this thing really a 3c507? */
227 if (ie_3C507_port_check(iobase)) {
235 ie_isa_3C507_attach (device_t dev)
237 struct ie_softc * sc;
240 sc = device_get_softc(dev);
246 error = ie_alloc_resources(dev);
252 sc->ie_reset_586 = el_reset_586;
253 sc->ie_chan_attn = el_chan_attn;
254 sc->hard_type = IE_3C507;
257 outb(PORT(sc) + IE507_CTRL, EL_CTRL_NORMAL);
259 if (!check_ie_present(sc)) {
264 sl_read_ether(sc, sc->enaddr);
266 /* Clear the interrupt latch just in case. */
267 outb(PORT(sc) + IE507_ICTRL, 1);
269 error = ie_attach(dev);
271 device_printf(dev, "ie_attach() failed.\n");
277 ie_release_resources(dev);
283 * If a 3c507 is present, return 0
287 ie_3C507_port_check (u_int32_t port)
289 u_char * signature = "*3COM*";
292 for (i = 0; i < 6; i++)
293 if (inb(port + i) != signature[i])
300 * Intel EtherExpress 16
302 #define IE_EE16_ID_PORT 0x0f
303 #define IE_EE16_ID 0xbaba
304 #define IE_EE16_EEPROM_CONFIG1 0x00
305 #define IE_EE16_EEPROM_IRQ_MASK 0xe000
306 #define IE_EE16_EEPROM_IRQ_SHIFT 13
307 #define IE_EE16_EEPROM_MEMCFG 0x06
308 #define IE_EE16_IOSIZE 16
312 * Test for 8/16 bit mode.
313 * Test for invalid mem sizes.
316 ie_isa_ee16_identify (driver_t *driver, device_t parent)
318 char * desc = "Intel EtherExpress 16";
320 u_int16_t ports[] = {
321 0x300, 0x310, 0x320, 0x330,
322 0x340, 0x350, 0x360, 0x370,
323 0x200, 0x210, 0x220, 0x230,
324 0x240, 0x250, 0x260, 0x270,
327 u_int16_t irqs[] = { 0, 0x09, 0x03, 0x04, 0x05, 0x0a, 0x0b, 0 };
328 u_int32_t port, maddr, msize;
333 for (i = 0; ports[i]; i++) {
336 if (ie_ee16_port_check(port)) {
339 device_printf(parent,
340 "if_ie: (EE16) not found at port %#x\n",
347 /* reset any ee16 at the current iobase */
348 outb(port + IEE16_ECTRL, IEE16_RESET_ASIC);
349 outb(port + IEE16_ECTRL, 0);
352 data = ie_ee16_hw_read_eeprom(port, IE_EE16_EEPROM_CONFIG1);
353 irq = irqs[((data & IE_EE16_EEPROM_IRQ_MASK)
354 >> IE_EE16_EEPROM_IRQ_SHIFT)];
356 data = ie_ee16_hw_read_eeprom(port, IE_EE16_EEPROM_MEMCFG);
357 maddr = 0xc0000 + ((ffs(data & 0x00ff) - 1) * 0x4000);
358 msize = (fls((data & 0x00ff) >> (ffs(data & 0x00ff) - 1)))
361 child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ie", -1);
362 device_set_desc_copy(child, desc);
363 device_set_driver(child, driver);
365 error = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
367 device_printf(parent, "(if_ie) Unable to set IRQ resource %d.\n",
369 error = device_delete_child(parent, child);
373 error = bus_set_resource(child, SYS_RES_IOPORT, 0, port, IE_EE16_IOSIZE);
375 device_printf(parent, "(if_ie) Unable to set IOPORT resource %#x-%#x.\n",
376 port, port+IE_EE16_IOSIZE);
377 error = device_delete_child(parent, child);
381 error = bus_set_resource(child, SYS_RES_MEMORY, 0, maddr, msize);
383 device_printf(parent, "(if_ie) Unable to set MEMORY resource %#x-%#x.\n",
385 error = device_delete_child(parent, child);
390 device_printf(parent,
391 "if_ie: <%s> at port %#x-%#x irq %d iomem %#lx-%#lx (%dKB)\n",
393 port, (port + IE_EE16_IOSIZE) - 1,
395 (u_long)maddr, (u_long)(maddr + msize) - 1,
404 ie_isa_ee16_probe (device_t dev)
408 /* No ISA-PnP support */
409 if (isa_get_vendorid(dev))
412 /* No ISA-HINT support */
413 if (!device_get_desc(dev))
416 /* Have we at least an ioport? */
417 if ((iobase = bus_get_resource_start(dev, SYS_RES_IOPORT, 0)) == 0)
420 /* Is this really an EE16? */
421 if (ie_ee16_port_check(iobase))
428 ie_isa_ee16_attach (device_t dev)
430 struct ie_softc * sc;
433 u_short eaddrtemp, pg, adjust, decode, edecode;
436 sc = device_get_softc(dev);
442 error = ie_alloc_resources(dev);
448 sc->ie_reset_586 = ee16_reset_586;
449 sc->ie_chan_attn = ee16_chan_attn;
450 sc->hard_type = IE_EE16;
454 /* reset any ee16 at the current iobase */
455 outb(PORT(sc) + IEE16_ECTRL, IEE16_RESET_ASIC);
456 outb(PORT(sc) + IEE16_ECTRL, 0);
459 /* Is this really an EE16? */
460 if (ie_ee16_port_check(PORT(sc))) {
461 device_printf(dev, "ie_ee16_port_check() failed\n");
466 /* need to put the 586 in RESET while we access the eeprom. */
467 outb(PORT(sc) + IEE16_ECTRL, IEE16_RESET_586);
469 /* read the eeprom and checksum it, should == IE_E16_ID */
471 for (i = 0; i < 0x40; i++)
472 checksum += ie_ee16_hw_read_eeprom(PORT(sc), i);
474 if (checksum != IE_EE16_ID) {
475 device_printf(dev, "invalid eeprom checksum: %x\n", checksum);
480 if ((kvtop(sc->iomembot) < 0xC0000) ||
481 (kvtop(sc->iomembot) + sc->iosize > 0xF0000)) {
482 device_printf(sc->dev, "mapped memory location %p out of range\n",
483 (void *)sc->iomembot);
488 pg = ((kvtop(sc->iomembot)) & 0x3C000) >> 14;
489 adjust = IEE16_MCTRL_FMCS16 | (pg & 0x3) << 2;
490 decode = ((1 << (sc->iosize / 16384)) - 1) << pg;
491 edecode = ((~decode >> 4) & 0xF0) | (decode >> 8);
493 /* ZZZ This should be checked against eeprom location 6, low byte */
494 outb(PORT(sc) + IEE16_MEMDEC, decode & 0xFF);
495 /* ZZZ This should be checked against eeprom location 1, low byte */
496 outb(PORT(sc) + IEE16_MCTRL, adjust);
497 /* ZZZ Now if I could find this one I would have it made */
498 outb(PORT(sc) + IEE16_MPCTRL, (~decode & 0xFF));
499 /* ZZZ I think this is location 6, high byte */
500 outb(PORT(sc) + IEE16_MECTRL, edecode); /* XXX disable Exxx */
503 (void) kvtop(sc->iomembot);
507 * first prime the stupid bart DRAM controller so that it works,
508 * then zero out all of memory.
510 bzero(sc->iomembot, 32);
511 bzero(sc->iomembot, sc->iosize);
513 /* Get the encoded interrupt number from the EEPROM */
514 sc->irq_encoded = ie_ee16_hw_read_eeprom(PORT(sc),
515 IE_EE16_EEPROM_CONFIG1);
516 sc->irq_encoded = (sc->irq_encoded & IE_EE16_EEPROM_IRQ_MASK) >>
517 IE_EE16_EEPROM_IRQ_SHIFT;
520 * Get the hardware ethernet address from the EEPROM and save it in
521 * the softc for use by the 586 setup code.
523 eaddrtemp = ie_ee16_hw_read_eeprom(PORT(sc), IEE16_EEPROM_ENET_HIGH);
524 sc->enaddr[1] = eaddrtemp & 0xFF;
525 sc->enaddr[0] = eaddrtemp >> 8;
526 eaddrtemp = ie_ee16_hw_read_eeprom(PORT(sc), IEE16_EEPROM_ENET_MID);
527 sc->enaddr[3] = eaddrtemp & 0xFF;
528 sc->enaddr[2] = eaddrtemp >> 8;
529 eaddrtemp = ie_ee16_hw_read_eeprom(PORT(sc), IEE16_EEPROM_ENET_LOW);
530 sc->enaddr[5] = eaddrtemp & 0xFF;
531 sc->enaddr[4] = eaddrtemp >> 8;
533 /* disable the board interrupts */
534 outb(PORT(sc) + IEE16_IRQ, sc->irq_encoded);
536 /* enable loopback to keep bad packets off the wire */
537 bart_config = inb(PORT(sc) + IEE16_CONFIG);
538 bart_config |= IEE16_BART_LOOPBACK;
539 bart_config |= IEE16_BART_MCS16_TEST;/* inb doesn't get bit! */
540 outb(PORT(sc) + IEE16_CONFIG, bart_config);
541 bart_config = inb(PORT(sc) + IEE16_CONFIG);
543 /* take the board out of reset state */
544 outb(PORT(sc) + IEE16_ECTRL, 0);
547 if (!check_ie_present(sc)) {
548 device_printf(dev, "check_ie_present() returned false.\n");
553 error = ie_attach(dev);
555 device_printf(dev, "ie_attach() failed.\n");
561 ie_release_resources(dev);
567 ie_isa_ee16_shutdown(device_t dev)
569 struct ie_softc * sc;
571 sc = device_get_softc(dev);
580 * If an EE16 is present, return 0
584 ie_ee16_port_check (u_int32_t port)
591 for (i = 0; i < 4; i++) {
592 data = inb(port + IE_EE16_ID_PORT);
593 board_id |= ((data >> 4) << ((data & 0x03) << 2));
596 if (board_id != IE_EE16_ID)
603 ie_ee16_hw_eeprom_clock (u_int32_t port, int state)
607 ectrl = inb(port + IEE16_ECTRL);
608 ectrl &= ~(IEE16_RESET_ASIC | IEE16_ECTRL_EESK);
611 ectrl |= IEE16_ECTRL_EESK;
613 outb(port + IEE16_ECTRL, ectrl);
614 DELAY(9); /* EESK must be stable for 8.38 uSec */
618 ie_ee16_hw_eeprom_out (u_int32_t port, u_int16_t edata, int count)
623 ectrl = inb(port + IEE16_ECTRL);
624 ectrl &= ~IEE16_RESET_ASIC;
626 for (i = count - 1; i >= 0; i--) {
627 ectrl &= ~IEE16_ECTRL_EEDI;
628 if (edata & (1 << i)) {
629 ectrl |= IEE16_ECTRL_EEDI;
631 outb(port + IEE16_ECTRL, ectrl);
632 DELAY(1); /* eeprom data must be setup for 0.4 uSec */
633 ie_ee16_hw_eeprom_clock(port, 1);
634 ie_ee16_hw_eeprom_clock(port, 0);
636 ectrl &= ~IEE16_ECTRL_EEDI;
637 outb(port + IEE16_ECTRL, ectrl);
638 DELAY(1); /* eeprom data must be held for 0.4 uSec */
644 ie_ee16_hw_eeprom_in (u_int32_t port)
650 ectrl = inb(port + IEE16_ECTRL);
651 ectrl &= ~IEE16_RESET_ASIC;
653 for (edata = 0, i = 0; i < 16; i++) {
655 ie_ee16_hw_eeprom_clock(port, 1);
656 ectrl = inb(port + IEE16_ECTRL);
657 if (ectrl & IEE16_ECTRL_EEDO) {
660 ie_ee16_hw_eeprom_clock(port, 0);
666 ie_ee16_hw_read_eeprom (u_int32_t port, int loc)
671 ectrl = inb(port + IEE16_ECTRL);
672 ectrl &= IEE16_ECTRL_MASK;
673 ectrl |= IEE16_ECTRL_EECS;
674 outb(port + IEE16_ECTRL, ectrl);
676 ie_ee16_hw_eeprom_out(port, IEE16_EEPROM_READ, IEE16_EEPROM_OPSIZE1);
677 ie_ee16_hw_eeprom_out(port, loc, IEE16_EEPROM_ADDR_SIZE);
678 edata = ie_ee16_hw_eeprom_in(port);
680 ectrl = inb(port + IEE16_ECTRL);
681 ectrl &= ~(IEE16_RESET_ASIC | IEE16_ECTRL_EEDI | IEE16_ECTRL_EECS);
682 outb(port + IEE16_ECTRL, ectrl);
684 ie_ee16_hw_eeprom_clock(port, 1);
685 ie_ee16_hw_eeprom_clock(port, 0);
695 ie_isa_sl_probe (device_t dev)
699 /* No ISA-PnP support */
700 if (isa_get_vendorid(dev))
703 /* ISA-HINT support only! */
704 if (device_get_desc(dev))
707 /* Have we at least an ioport? */
708 if ((iobase = bus_get_resource_start(dev, SYS_RES_IOPORT, 0)) == 0)
711 /* Is this really an SL board? */
712 if (ie_isa_sl_get_hard_type(iobase) == IE_NONE)
719 ie_isa_sl_attach (device_t dev)
721 struct ie_softc * sc;
724 sc = device_get_softc(dev);
730 error = ie_alloc_resources(dev);
735 /* Is this really an SL board? */
736 if ((sc->hard_type = ie_isa_sl_get_hard_type(PORT(sc))) == IE_NONE) {
741 sc->hard_vers = SL_REV(inb(PORT(sc) + IEATT_REVISION));
742 if (sc->hard_type == IE_NI5210) {
748 sc->ie_reset_586 = sl_reset_586;
749 sc->ie_chan_attn = sl_chan_attn;
751 if (!check_ie_present(sc)) {
756 switch (sc->hard_type) {
761 sl_read_ether(sc, sc->enaddr);
765 device_printf(sc->dev, "unknown AT&T board type code %d\n", sc->hard_type);
771 error = ie_attach(dev);
773 device_printf(dev, "ie_attach() failed.\n");
779 ie_release_resources(dev);
784 static enum ie_hardware
785 ie_isa_sl_get_hard_type (u_int32_t port)
788 enum ie_hardware retval;
790 c = inb(port + IEATT_REVISION);
791 switch (SL_BOARD(c)) {
793 if (inb(port + IEATT_ATTRIB) != NI5210_BOARD)
798 retval = IE_STARLAN10;
812 static devclass_t ie_devclass;
814 static device_method_t ie_isa_3C507_methods[] = {
815 DEVMETHOD(device_identify, ie_isa_3C507_identify),
816 DEVMETHOD(device_probe, ie_isa_3C507_probe),
817 DEVMETHOD(device_attach, ie_isa_3C507_attach),
818 DEVMETHOD(device_detach, ie_detach),
822 static driver_t ie_isa_3C507_driver = {
824 ie_isa_3C507_methods,
825 sizeof(struct ie_softc),
828 DRIVER_MODULE(ie_3C507, isa, ie_isa_3C507_driver, ie_devclass, ie_modevent, 0);
829 MODULE_DEPEND(ie_3C507, elink, 1, 1, 1);
831 static device_method_t ie_isa_ee16_methods[] = {
832 DEVMETHOD(device_identify, ie_isa_ee16_identify),
833 DEVMETHOD(device_probe, ie_isa_ee16_probe),
834 DEVMETHOD(device_attach, ie_isa_ee16_attach),
835 DEVMETHOD(device_shutdown, ie_isa_ee16_shutdown),
836 DEVMETHOD(device_detach, ie_detach),
840 static driver_t ie_isa_ee16_driver = {
843 sizeof(struct ie_softc),
846 DRIVER_MODULE(ie, isa, ie_isa_ee16_driver, ie_devclass, ie_modevent, 0);
848 static device_method_t ie_isa_sl_methods[] = {
849 DEVMETHOD(device_probe, ie_isa_sl_probe),
850 DEVMETHOD(device_attach, ie_isa_sl_attach),
851 DEVMETHOD(device_detach, ie_detach),
855 static driver_t ie_isa_sl_driver = {
858 sizeof(struct ie_softc),
861 DRIVER_MODULE(ie_SL, isa, ie_isa_sl_driver, ie_devclass, ie_modevent, 0);
864 ie_modevent (mod, what, arg)
877 devclass_get_devices(ie_devclass, &devs, &count);
878 for (i = 0; i < count; i++)
879 device_delete_child(device_get_parent(devs[i]), devs[i]);