if_ep_isa.c revision 52549
151673Smdodd/* 251673Smdodd * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca> 351673Smdodd * All rights reserved. 451673Smdodd * 551673Smdodd * Redistribution and use in source and binary forms, with or without 651673Smdodd * modification, are permitted provided that the following conditions 751673Smdodd * are met: 851673Smdodd * 1. Redistributions of source code must retain the above copyright 951673Smdodd * notice, this list of conditions and the following disclaimer. 1051673Smdodd * 2. Redistributions in binary form must reproduce the above copyright 1151673Smdodd * notice, this list of conditions and the following disclaimer in the 1251673Smdodd * documentation and/or other materials provided with the distribution. 1351673Smdodd * 3. All advertising materials mentioning features or use of this software 1451673Smdodd * must display the following acknowledgement: 1551673Smdodd * This product includes software developed by Herb Peyerl. 1651673Smdodd * 4. The name of Herb Peyerl may not be used to endorse or promote products 1751673Smdodd * derived from this software without specific prior written permission. 1851673Smdodd * 1951673Smdodd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 2051673Smdodd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 2151673Smdodd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 2251673Smdodd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 2351673Smdodd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2451673Smdodd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2551673Smdodd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2651673Smdodd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2751673Smdodd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2851673Smdodd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2951673Smdodd * 3051673Smdodd * $FreeBSD: head/sys/dev/ep/if_ep_isa.c 52549 1999-10-27 06:25:16Z mdodd $ 3151673Smdodd */ 3251673Smdodd 3351673Smdodd#include <sys/param.h> 3452549Smdodd#include <sys/systm.h> 3551673Smdodd#include <sys/kernel.h> 3651673Smdodd#include <sys/socket.h> 3751673Smdodd 3852549Smdodd#include <sys/module.h> 3952549Smdodd#include <sys/bus.h> 4052549Smdodd 4152549Smdodd#include <machine/bus.h> 4252549Smdodd#include <machine/resource.h> 4352549Smdodd#include <sys/rman.h> 4452549Smdodd 4551673Smdodd#include <net/if.h> 4652549Smdodd#include <net/if_arp.h> 4752549Smdodd#include <net/if_media.h> 4851673Smdodd 4951673Smdodd#include <machine/clock.h> 5051673Smdodd 5152549Smdodd#include <isa/isavar.h> 5252549Smdodd#include <isa/pnpvar.h> 5351673Smdodd 5451673Smdodd#include <dev/ep/if_epreg.h> 5551673Smdodd#include <dev/ep/if_epvar.h> 5651673Smdodd#include <i386/isa/elink.h> 5751673Smdodd 5852549Smdoddstatic int get_eeprom_data (int, int); 5951673Smdodd 6052549Smdoddstatic void ep_isa_identify (driver_t *, device_t); 6152549Smdoddstatic int ep_isa_probe (device_t); 6252549Smdoddstatic int ep_isa_attach (device_t); 6351673Smdodd 6452549Smdoddstruct isa_ident { 6552549Smdodd u_int32_t id; 6652549Smdodd char * name; 6751673Smdodd}; 6852549Smdoddconst char * ep_isa_match_id (u_int32_t, struct isa_ident *); 6951673Smdodd 7052549Smdodd#define ISA_ID_3C509_TP 0x506d5090 7152549Smdodd#define ISA_ID_3C509_BNC 0x506d5091 7252549Smdodd#define ISA_ID_3C509_COMBO 0x506d5094 7352549Smdodd#define ISA_ID_3C509_TPO 0x506d5095 7451673Smdodd 7552549Smdoddstatic struct isa_ident ep_isa_devs[] = { 7652549Smdodd { ISA_ID_3C509_TP, "3Com EtherLink III (3c509-TP)" }, 7752549Smdodd { ISA_ID_3C509_BNC, "3Com EtherLink III (3c509-BNC)" }, 7852549Smdodd { ISA_ID_3C509_COMBO, "3Com EtherLink III (3c509-Combo)" }, 7952549Smdodd { ISA_ID_3C509_TPO, "3Com EtherLink III (3c509-TPO)" }, 8052549Smdodd { 0, NULL }, 8152549Smdodd}; 8251673Smdodd 8352549Smdoddstatic struct isa_pnp_id ep_ids[] = { 8452549Smdodd { 0x90506d50, NULL }, /* TCM5090 */ 8552549Smdodd { 0x91506d50, NULL }, /* TCM5091 */ 8652549Smdodd { 0x94506d50, NULL }, /* TCM5094 */ 8752549Smdodd { 0x95506d50, NULL }, /* TCM5095 */ 8852549Smdodd { 0xf780d041, NULL }, /* PNP80f7 */ 8952549Smdodd { 0, NULL }, 9052549Smdodd}; 9151673Smdodd 9252549Smdodd/* 9352549Smdodd * We get eeprom data from the id_port given an offset into the eeprom. 9452549Smdodd * Basically; after the ID_sequence is sent to all of the cards; they enter 9552549Smdodd * the ID_CMD state where they will accept command requests. 0x80-0xbf loads 9652549Smdodd * the eeprom data. We then read the port 16 times and with every read; the 9752549Smdodd * cards check for contention (ie: if one card writes a 0 bit and another 9852549Smdodd * writes a 1 bit then the host sees a 0. At the end of the cycle; each card 9952549Smdodd * compares the data on the bus; if there is a difference then that card goes 10052549Smdodd * into ID_WAIT state again). In the meantime; one bit of data is returned in 10152549Smdodd * the AX register which is conveniently returned to us by inb(). Hence; we 10252549Smdodd * read 16 times getting one bit of data with each read. 10352549Smdodd */ 10451673Smdodd 10552549Smdoddstatic int 10652549Smdoddget_eeprom_data(id_port, offset) 10752549Smdodd int id_port; 10852549Smdodd int offset; 10952549Smdodd{ 11052549Smdodd int i, data = 0; 11152549Smdodd outb(id_port, 0x80 + offset); 11252549Smdodd for (i = 0; i < 16; i++) { 11352549Smdodd DELAY(BIT_DELAY_MULTIPLE * 1000); 11452549Smdodd data = (data << 1) | (inw(id_port) & 1); 11551673Smdodd } 11652549Smdodd return (data); 11752549Smdodd} 11851673Smdodd 11952549Smdoddconst char * 12052549Smdoddep_isa_match_id (id, isa_devs) 12152549Smdodd u_int32_t id; 12252549Smdodd struct isa_ident * isa_devs; 12352549Smdodd{ 12452549Smdodd struct isa_ident * i = isa_devs; 12552549Smdodd while(i->name != NULL) { 12652549Smdodd if (id == i->id) 12752549Smdodd return (i->name); 12852549Smdodd i++; 12952549Smdodd } 13052549Smdodd return (NULL); 13152549Smdodd} 13251673Smdodd 13352549Smdoddstatic void 13452549Smdoddep_isa_identify (driver_t *driver, device_t parent) 13552549Smdodd{ 13652549Smdodd int tag = EP_LAST_TAG; 13752549Smdodd int found = 0; 13852549Smdodd int i; 13952549Smdodd int j; 14052549Smdodd const char * desc; 14152549Smdodd u_int32_t data; 14252549Smdodd u_int32_t irq; 14352549Smdodd u_int32_t ioport; 14452549Smdodd u_int32_t isa_id; 14552549Smdodd device_t child; 14651673Smdodd 14752549Smdodd outb(ELINK_ID_PORT, 0); 14852549Smdodd outb(ELINK_ID_PORT, 0); 14951673Smdodd 15052549Smdodd elink_idseq(ELINK_509_POLY); 15152549Smdodd elink_reset(); 15251673Smdodd 15352549Smdodd DELAY(DELAY_MULTIPLE * 10000); 15451673Smdodd 15552549Smdodd for (i = 0; i < EP_MAX_BOARDS; i++) { 15651673Smdodd 15752549Smdodd outb(ELINK_ID_PORT, 0); 15852549Smdodd outb(ELINK_ID_PORT, 0); 15952549Smdodd elink_idseq(0xCF); 16051673Smdodd 16152549Smdodd /* For the first probe, clear all 16252549Smdodd * board's tag registers. 16352549Smdodd * Otherwise kill off already-found 16452549Smdodd * boards. -- linux 3c509.c 16552549Smdodd */ 16652549Smdodd if (i == 0) { 16752549Smdodd outb(ELINK_ID_PORT, 0xd0); 16852549Smdodd } else { 16952549Smdodd outb(ELINK_ID_PORT, 0xd8); 17052549Smdodd } 17151673Smdodd 17252549Smdodd /* 17352549Smdodd * Construct an 'isa_id' in 'EISA' 17452549Smdodd * format. 17552549Smdodd */ 17652549Smdodd data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID); 17752549Smdodd isa_id = (htons(data) << 16); 17852549Smdodd data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID); 17952549Smdodd isa_id |= htons(data); 18051673Smdodd 18152549Smdodd /* Find known ISA boards */ 18252549Smdodd desc = ep_isa_match_id(isa_id, ep_isa_devs); 18352549Smdodd if (!desc) { 18452549Smdodd if (bootverbose) { 18552549Smdodd device_printf(parent, "if_ep: unknown ID 0x%08x\n", 18652549Smdodd isa_id); 18752549Smdodd } 18852549Smdodd break; 18952549Smdodd } 19051673Smdodd 19152549Smdodd /* resolve contention using the Ethernet address */ 19252549Smdodd for (j = 0; j < 3; j++) { 19352549Smdodd get_eeprom_data(ELINK_ID_PORT, j); 19452549Smdodd } 19551673Smdodd 19652549Smdodd /* Retreive IRQ */ 19752549Smdodd data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG); 19852549Smdodd irq = (data >> 12); 19951673Smdodd 20052549Smdodd /* Retreive IOPORT */ 20152549Smdodd data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG); 20252549Smdodd#ifdef PC98 20352549Smdodd ioport = ((data * 0x100) + 0x40d0); 20452549Smdodd#else 20552549Smdodd ioport = ((data << 4) + 0x200); 20652549Smdodd#endif 20751673Smdodd 20852549Smdodd /* Set the adaptor tag so that the next card can be found. */ 20952549Smdodd outb(ELINK_ID_PORT, tag--); 21051673Smdodd 21152549Smdodd /* Activate the adaptor at the EEPROM location. */ 21252549Smdodd outb(ELINK_ID_PORT, ((ioport >> 4) | 0xe0)); 21351673Smdodd 21452549Smdodd /* Test for an adapter in PnP mode */ 21552549Smdodd data = inw(ioport + EP_W0_EEPROM_COMMAND); 21652549Smdodd if (data & EEPROM_TST_MODE) { 21752549Smdodd device_printf(parent, "if_ep: Adapter at 0x%03x in PnP mode!\n", 21852549Smdodd ioport); 21952549Smdodd continue; 22052549Smdodd } 22151673Smdodd 22252549Smdodd child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1); 22352549Smdodd device_set_desc_copy(child, desc); 22452549Smdodd device_set_driver(child, driver); 22552549Smdodd bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1); 22652549Smdodd bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE); 22751673Smdodd 22852549Smdodd if (bootverbose) { 22952549Smdodd device_printf(parent, "if_ep: <%s> at port 0x%03x-0x%03x irq %d\n", 23052549Smdodd desc, ioport, ioport + EP_IOSIZE, irq); 23152549Smdodd } 23251673Smdodd 23352549Smdodd found++; 23452549Smdodd } 23551673Smdodd 23652549Smdodd return; 23752549Smdodd} 23851673Smdodd 23952549Smdoddstatic int 24052549Smdoddep_isa_probe (device_t dev) 24152549Smdodd{ 24252549Smdodd int error = 0; 24351673Smdodd 24452549Smdodd /* Check isapnp ids */ 24552549Smdodd error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids); 24651673Smdodd 24752549Smdodd /* If the card had a PnP ID that didn't match any we know about */ 24852549Smdodd if (error == ENXIO) { 24952549Smdodd return (error); 25052549Smdodd } 25151673Smdodd 25252549Smdodd /* If we had some other problem. */ 25352549Smdodd if (!(error == 0 || error == ENOENT)) { 25452549Smdodd return (error); 25552549Smdodd } 25651673Smdodd 25752549Smdodd /* If we have the resources we need then we're good to go. */ 25852549Smdodd if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) && 25952549Smdodd (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) { 26052549Smdodd return (0); 26152549Smdodd } 26251673Smdodd 26352549Smdodd return (ENXIO); 26452549Smdodd} 26551673Smdodd 26652549Smdoddstatic int 26752549Smdoddep_isa_attach (device_t dev) 26852549Smdodd{ 26952549Smdodd struct ep_softc * sc = device_get_softc(dev); 27052549Smdodd int error = 0; 27151673Smdodd 27252549Smdodd if ((error = ep_alloc(dev))) { 27352549Smdodd device_printf(dev, "ep_alloc() failed! (%d)\n", error); 27452549Smdodd goto bad; 27552549Smdodd } 27651673Smdodd 27752549Smdodd ep_get_media(sc); 27851673Smdodd 27952549Smdodd GO_WINDOW(0); 28052549Smdodd SET_IRQ(BASE, rman_get_start(sc->irq)); 28151673Smdodd 28252549Smdodd if ((error = ep_attach(sc))) { 28352549Smdodd device_printf(dev, "ep_attach() failed! (%d)\n", error); 28452549Smdodd goto bad; 28552549Smdodd } 28651673Smdodd 28752549Smdodd if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr, 28852549Smdodd sc, &sc->ep_intrhand))) { 28952549Smdodd device_printf(dev, "bus_setup_intr() failed! (%d)\n", error); 29052549Smdodd goto bad; 29152549Smdodd } 29251673Smdodd 29352549Smdodd return (0); 29452549Smdoddbad: 29552549Smdodd ep_free(dev); 29652549Smdodd return (error); 29751673Smdodd} 29851673Smdodd 29952549Smdoddstatic device_method_t ep_isa_methods[] = { 30052549Smdodd /* Device interface */ 30152549Smdodd DEVMETHOD(device_identify, ep_isa_identify), 30252549Smdodd DEVMETHOD(device_probe, ep_isa_probe), 30352549Smdodd DEVMETHOD(device_attach, ep_isa_attach), 30451673Smdodd 30552549Smdodd { 0, 0 } 30652549Smdodd}; 30751673Smdodd 30852549Smdoddstatic driver_t ep_isa_driver = { 30952549Smdodd "ep", 31052549Smdodd ep_isa_methods, 31152549Smdodd sizeof(struct ep_softc), 31252549Smdodd}; 31351673Smdodd 31452549Smdoddextern devclass_t ep_devclass; 31551673Smdodd 31652549SmdoddDRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0); 317