if_ep_pccard.c revision 52589
1227825Stheraven/* 2227825Stheraven * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca> 3227825Stheraven * All rights reserved. 4227825Stheraven * 5227825Stheraven * Redistribution and use in source and binary forms, with or without 6227825Stheraven * modification, are permitted provided that the following conditions 7227825Stheraven * are met: 8227825Stheraven * 1. Redistributions of source code must retain the above copyright 9227825Stheraven * notice, this list of conditions and the following disclaimer. 10227825Stheraven * 2. Redistributions in binary form must reproduce the above copyright 11227825Stheraven * notice, this list of conditions and the following disclaimer in the 12227825Stheraven * documentation and/or other materials provided with the distribution. 13227825Stheraven * 3. All advertising materials mentioning features or use of this software 14227825Stheraven * must display the following acknowledgement: 15227825Stheraven * This product includes software developed by Herb Peyerl. 16227825Stheraven * 4. The name of Herb Peyerl may not be used to endorse or promote products 17227825Stheraven * derived from this software without specific prior written permission. 18227825Stheraven * 19227825Stheraven * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20227825Stheraven * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21227825Stheraven * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22227825Stheraven * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23227825Stheraven * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24227825Stheraven * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25227825Stheraven * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26227825Stheraven * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27227825Stheraven * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28227825Stheraven * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29227825Stheraven * 30227825Stheraven * $FreeBSD: head/sys/dev/ep/if_ep_pccard.c 52589 1999-10-28 06:12:58Z imp $ 31227825Stheraven */ 32227825Stheraven 33227825Stheraven/* 34227825Stheraven * Pccard support for 3C589 by: 35227825Stheraven * HAMADA Naoki 36227825Stheraven * nao@tom-yam.or.jp 37227825Stheraven */ 38227825Stheraven 39227825Stheraven#include <sys/param.h> 40227825Stheraven#include <sys/systm.h> 41227825Stheraven#include <sys/kernel.h> 42227825Stheraven#include <sys/socket.h> 43227825Stheraven 44227825Stheraven#include <sys/module.h> 45227825Stheraven#include <sys/bus.h> 46227825Stheraven 47227825Stheraven#include <machine/bus.h> 48227825Stheraven#include <machine/resource.h> 49227825Stheraven#include <sys/rman.h> 50227825Stheraven 51227825Stheraven#include <net/if.h> 52227825Stheraven#include <net/if_arp.h> 53227825Stheraven#include <net/if_media.h> 54227825Stheraven 55227825Stheraven#include <machine/clock.h> 56227825Stheraven 57227825Stheraven#include <dev/ep/if_epreg.h> 58227825Stheraven#include <dev/ep/if_epvar.h> 59227825Stheraven 60227825Stheraven/* XXX should die XXX */ 61227825Stheraven#include <sys/select.h> 62227825Stheraven#include <sys/module.h> 63227825Stheraven#include <pccard/cardinfo.h> 64227825Stheraven#include <pccard/slot.h> 65227825Stheraven 66227825Stheravenstatic const char *ep_pccard_identify(u_short id); 67227825Stheraven 68227825Stheraven/* 69262801Sdim * Initialize the device - called from Slot manager. 70262801Sdim */ 71262801Sdimstatic int 72262801Sdimep_pccard_probe(device_t dev) 73262801Sdim{ 74227825Stheraven struct ep_softc * sc = device_get_softc(dev); 75227825Stheraven struct ep_board * epb = &sc->epb; 76227825Stheraven const char * desc; 77227825Stheraven int error; 78227825Stheraven 79227825Stheraven error = ep_alloc(dev); 80227825Stheraven if (error) 81227825Stheraven return error; 82227825Stheraven 83227825Stheraven /* 84227825Stheraven * XXX - Certain (newer?) 3Com cards need epb->cmd_off == 85227825Stheraven * 2. Sadly, you need to have a correct cmd_off in order to 86227825Stheraven * identify the card. So we have to hit it with both and 87227825Stheraven * cross our virtual fingers. There's got to be a better way 88227825Stheraven * to do this. jyoung@accessus.net 09/11/1999 89227825Stheraven */ 90227825Stheraven 91227825Stheraven epb->cmd_off = 0; 92227825Stheraven epb->prod_id = get_e(sc, EEPROM_PROD_ID); 93227825Stheraven if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) { 94227825Stheraven if (bootverbose) 95227825Stheraven device_printf(dev, "Pass 1 of 2 detection " 96227825Stheraven "failed (nonfatal)\n"); 97227825Stheraven epb->cmd_off = 2; 98227825Stheraven epb->prod_id = get_e(sc, EEPROM_PROD_ID); 99227825Stheraven if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) { 100227825Stheraven device_printf(dev, "Unit failed to come ready or " 101227825Stheraven "product ID unknown! (id 0x%x)\n", epb->prod_id); 102227825Stheraven ep_free(dev); 103227825Stheraven return (ENXIO); 104227825Stheraven } 105227825Stheraven } 106227825Stheraven device_set_desc(dev, desc); 107227825Stheraven ep_free(dev); 108227825Stheraven return (0); 109227825Stheraven} 110227825Stheraven 111227825Stheravenstatic const char * 112227825Stheravenep_pccard_identify(u_short id) 113227825Stheraven{ 114227825Stheraven /* Determine device type and associated MII capabilities */ 115227825Stheraven switch (id) { 116227825Stheraven case 0x6055: /* 3C556 */ 117227825Stheraven return ("3Com 3C556"); 118227825Stheraven case 0x4057: /* 3C574 */ 119227825Stheraven return ("3Com 3C574"); 120227825Stheraven case 0x4b57: /* 3C574B */ 121227825Stheraven return ("3Com 3C574B, Megahertz 3CCFE574BT or " 122227825Stheraven "Fast Etherlink 3C574-TX"); 123227825Stheraven case 0x9058: /* 3C589 */ 124227825Stheraven return ("3Com Etherlink III 3C589"); 125227825Stheraven } 126227825Stheraven return (NULL); 127227825Stheraven} 128227825Stheraven 129227825Stheravenstatic int 130227825Stheravenep_pccard_card_attach(struct ep_board *epb) 131227825Stheraven{ 132227825Stheraven /* Determine device type and associated MII capabilities */ 133227825Stheraven switch (epb->prod_id) { 134227825Stheraven case 0x6055: /* 3C556 */ 135227825Stheraven epb->mii_trans = 1; 136227825Stheraven return (1); 137262801Sdim case 0x4057: /* 3C574 */ 138262801Sdim epb->mii_trans = 1; 139262801Sdim return (1); 140262801Sdim case 0x4b57: /* 3C574B */ 141262801Sdim epb->mii_trans = 1; 142262801Sdim return (1); 143262801Sdim case 0x9058: /* 3C589 */ 144227825Stheraven epb->mii_trans = 0; 145227825Stheraven return (1); 146227825Stheraven } 147262801Sdim return (0); 148262801Sdim} 149262801Sdim 150262801Sdimstatic int 151262801Sdimep_pccard_attach(device_t dev) 152227825Stheraven{ 153227825Stheraven struct ep_softc * sc = device_get_softc(dev); 154262801Sdim int error = 0; 155262801Sdim 156262801Sdim if ((error = ep_alloc(dev))) { 157262801Sdim device_printf(dev, "ep_alloc() failed! (%d)\n", error); 158227825Stheraven goto bad; 159227825Stheraven } 160262801Sdim 161262801Sdim sc->epb.cmd_off = 0; 162262801Sdim sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID); 163262801Sdim if (!ep_pccard_card_attach(&sc->epb)) { 164227825Stheraven sc->epb.cmd_off = 2; 165227825Stheraven sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID); 166227825Stheraven sc->epb.res_cfg = get_e(sc, EEPROM_RESOURCE_CFG); 167227825Stheraven if (!ep_pccard_card_attach(&sc->epb)) { 168227825Stheraven device_printf(dev, 169227825Stheraven "Probe found ID, attach failed so ignore card!\n"); 170227825Stheraven error = ENXIO; 171227825Stheraven goto bad; 172227825Stheraven } 173227825Stheraven } 174227825Stheraven 175227825Stheraven /* ROM size = 0, ROM base = 0 */ 176227825Stheraven /* For now, ignore AUTO SELECT feature of 3C589B and later. */ 177227825Stheraven outw(BASE + EP_W0_ADDRESS_CFG, get_e(sc, EEPROM_ADDR_CFG) & 0xc000); 178227825Stheraven 179227825Stheraven /* Fake IRQ must be 3 */ 180227825Stheraven outw(BASE + EP_W0_RESOURCE_CFG, (sc->epb.res_cfg & 0x0fff) | 0x3000); 181227825Stheraven 182227825Stheraven outw(BASE + EP_W0_PRODUCT_ID, sc->epb.prod_id); 183227825Stheraven 184227825Stheraven if (sc->epb.mii_trans) { 185227825Stheraven /* 186227825Stheraven * turn on the MII transciever 187227825Stheraven */ 188227825Stheraven GO_WINDOW(3); 189227825Stheraven outw(BASE + EP_W3_OPTIONS, 0x8040); 190227825Stheraven DELAY(1000); 191227825Stheraven outw(BASE + EP_W3_OPTIONS, 0xc040); 192227825Stheraven outw(BASE + EP_COMMAND, RX_RESET); 193227825Stheraven outw(BASE + EP_COMMAND, TX_RESET); 194227825Stheraven while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS); 195227825Stheraven DELAY(1000); 196227825Stheraven outw(BASE + EP_W3_OPTIONS, 0x8040); 197227825Stheraven } else { 198227825Stheraven ep_get_media(sc); 199227825Stheraven } 200227825Stheraven 201227825Stheraven if ((error = ep_attach(sc))) { 202227825Stheraven device_printf(dev, "ep_attach() failed! (%d)\n", error); 203227825Stheraven goto bad; 204227825Stheraven } 205227825Stheraven 206227825Stheraven if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr, 207227825Stheraven sc, &sc->ep_intrhand))) { 208227825Stheraven device_printf(dev, "bus_setup_intr() failed! (%d)\n", error); 209227825Stheraven goto bad; 210227825Stheraven } 211227825Stheraven 212227825Stheraven return (0); 213227825Stheravenbad: 214227825Stheraven ep_free(dev); 215227825Stheraven return (error); 216227825Stheraven} 217227825Stheraven 218227825Stheravenstatic void 219227825Stheravenep_pccard_detach(device_t dev) 220227825Stheraven{ 221227825Stheraven struct ep_softc *sc = device_get_softc(dev); 222227825Stheraven 223227825Stheraven printf("detach\n"); 224227825Stheraven 225227825Stheraven if (sc->gone) { 226227825Stheraven device_printf(dev, "already unloaded\n"); 227227825Stheraven return; 228227825Stheraven } 229227825Stheraven sc->arpcom.ac_if.if_flags &= ~IFF_RUNNING; 230227825Stheraven if_down(&sc->arpcom.ac_if); 231227825Stheraven sc->gone = 1; 232227825Stheraven bus_teardown_intr(dev, sc->irq, sc->ep_intrhand); 233227825Stheraven ep_free(dev); 234227825Stheraven device_printf(dev, "unload\n"); 235227825Stheraven} 236227825Stheraven 237227825Stheravenstatic device_method_t ep_pccard_methods[] = { 238227825Stheraven /* Device interface */ 239227825Stheraven DEVMETHOD(device_probe, ep_pccard_probe), 240227825Stheraven DEVMETHOD(device_attach, ep_pccard_attach), 241227825Stheraven DEVMETHOD(device_detach, ep_pccard_detach), 242227825Stheraven 243227825Stheraven { 0, 0 } 244227825Stheraven}; 245227825Stheraven 246227825Stheravenstatic driver_t ep_pccard_driver = { 247227825Stheraven "ep", 248227825Stheraven ep_pccard_methods, 249227825Stheraven sizeof(struct ep_softc), 250262801Sdim}; 251262801Sdim 252262801Sdimextern devclass_t ep_devclass; 253262801Sdim 254262801SdimDRIVER_MODULE(ep, pccard, ep_pccard_driver, ep_devclass, 0, 0); 255227825Stheraven