if_ep_pccard.c revision 52621
1276428Sneel/* 2336190Saraujo * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca> 3336190Saraujo * All rights reserved. 4276428Sneel * 5276428Sneel * Redistribution and use in source and binary forms, with or without 6276428Sneel * modification, are permitted provided that the following conditions 7276428Sneel * are met: 8276428Sneel * 1. Redistributions of source code must retain the above copyright 9276428Sneel * notice, this list of conditions and the following disclaimer. 10276428Sneel * 2. Redistributions in binary form must reproduce the above copyright 11276428Sneel * notice, this list of conditions and the following disclaimer in the 12276428Sneel * documentation and/or other materials provided with the distribution. 13276428Sneel * 3. All advertising materials mentioning features or use of this software 14276428Sneel * must display the following acknowledgement: 15276428Sneel * This product includes software developed by Herb Peyerl. 16276428Sneel * 4. The name of Herb Peyerl may not be used to endorse or promote products 17276428Sneel * derived from this software without specific prior written permission. 18276428Sneel * 19276428Sneel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20276428Sneel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21276428Sneel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22276428Sneel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23276428Sneel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24276428Sneel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25276428Sneel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26276428Sneel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27276428Sneel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28276428Sneel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29276428Sneel * 30276428Sneel * $FreeBSD: head/sys/dev/ep/if_ep_pccard.c 52621 1999-10-29 06:27:07Z imp $ 31276428Sneel */ 32276428Sneel 33276428Sneel/* 34276428Sneel * Pccard support for 3C589 by: 35276428Sneel * HAMADA Naoki 36276428Sneel * nao@tom-yam.or.jp 37276428Sneel */ 38276428Sneel 39276428Sneel#include <sys/param.h> 40276428Sneel#include <sys/systm.h> 41276428Sneel#include <sys/kernel.h> 42276428Sneel#include <sys/socket.h> 43276428Sneel 44276428Sneel#include <sys/module.h> 45276428Sneel#include <sys/bus.h> 46276428Sneel 47276428Sneel#include <machine/bus.h> 48276428Sneel#include <machine/resource.h> 49276428Sneel#include <sys/rman.h> 50276428Sneel 51276428Sneel#include <net/if.h> 52276428Sneel#include <net/if_arp.h> 53#include <net/if_media.h> 54 55#include <machine/clock.h> 56 57#include <dev/ep/if_epreg.h> 58#include <dev/ep/if_epvar.h> 59 60/* XXX should die XXX */ 61#include <sys/select.h> 62#include <sys/module.h> 63#include <pccard/cardinfo.h> 64#include <pccard/slot.h> 65 66static const char *ep_pccard_identify(u_short id); 67 68/* 69 * Initialize the device - called from Slot manager. 70 */ 71static int 72ep_pccard_probe(device_t dev) 73{ 74 struct ep_softc * sc = device_get_softc(dev); 75 struct ep_board * epb = &sc->epb; 76 const char * desc; 77 int error; 78 79 error = ep_alloc(dev); 80 if (error) 81 return error; 82 83 /* 84 * XXX - Certain (newer?) 3Com cards need epb->cmd_off == 85 * 2. Sadly, you need to have a correct cmd_off in order to 86 * identify the card. So we have to hit it with both and 87 * cross our virtual fingers. There's got to be a better way 88 * to do this. jyoung@accessus.net 09/11/1999 89 */ 90 91 epb->cmd_off = 0; 92 epb->prod_id = get_e(sc, EEPROM_PROD_ID); 93 if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) { 94 if (bootverbose) 95 device_printf(dev, "Pass 1 of 2 detection " 96 "failed (nonfatal)\n"); 97 epb->cmd_off = 2; 98 epb->prod_id = get_e(sc, EEPROM_PROD_ID); 99 if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) { 100 device_printf(dev, "Unit failed to come ready or " 101 "product ID unknown! (id 0x%x)\n", epb->prod_id); 102 ep_free(dev); 103 return (ENXIO); 104 } 105 } 106 device_set_desc(dev, desc); 107 ep_free(dev); 108 return (0); 109} 110 111static const char * 112ep_pccard_identify(u_short id) 113{ 114 /* Determine device type and associated MII capabilities */ 115 switch (id) { 116 case 0x6055: /* 3C556 */ 117 return ("3Com 3C556"); 118 case 0x4057: /* 3C574 */ 119 return ("3Com 3C574"); 120 case 0x4b57: /* 3C574B */ 121 return ("3Com 3C574B, Megahertz 3CCFE574BT or " 122 "Fast Etherlink 3C574-TX"); 123 case 0x9058: /* 3C589 */ 124 return ("3Com Etherlink III 3C589"); 125 case 0x2056: /* 3C562/3C563 */ 126 return ("3Com 3C562D/3C563D"); 127 } 128 return (NULL); 129} 130 131static int 132ep_pccard_card_attach(struct ep_board *epb) 133{ 134 /* Determine device type and associated MII capabilities */ 135 switch (epb->prod_id) { 136 case 0x6055: /* 3C556 */ 137 epb->mii_trans = 1; 138 return (1); 139 case 0x4057: /* 3C574 */ 140 epb->mii_trans = 1; 141 return (1); 142 case 0x4b57: /* 3C574B */ 143 epb->mii_trans = 1; 144 return (1); 145 case 0x2056: /* 3C562D/3C563D */ 146 case 0x9058: /* 3C589 */ 147 epb->mii_trans = 0; 148 return (1); 149 } 150 return (0); 151} 152 153static int 154ep_pccard_attach(device_t dev) 155{ 156 struct ep_softc * sc = device_get_softc(dev); 157 int error = 0; 158 159 if ((error = ep_alloc(dev))) { 160 device_printf(dev, "ep_alloc() failed! (%d)\n", error); 161 goto bad; 162 } 163 164 sc->epb.cmd_off = 0; 165 sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID); 166 if (!ep_pccard_card_attach(&sc->epb)) { 167 sc->epb.cmd_off = 2; 168 sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID); 169 sc->epb.res_cfg = get_e(sc, EEPROM_RESOURCE_CFG); 170 if (!ep_pccard_card_attach(&sc->epb)) { 171 device_printf(dev, 172 "Probe found ID, attach failed so ignore card!\n"); 173 error = ENXIO; 174 goto bad; 175 } 176 } 177 178 /* ROM size = 0, ROM base = 0 */ 179 /* For now, ignore AUTO SELECT feature of 3C589B and later. */ 180 outw(BASE + EP_W0_ADDRESS_CFG, get_e(sc, EEPROM_ADDR_CFG) & 0xc000); 181 182 /* Fake IRQ must be 3 */ 183 outw(BASE + EP_W0_RESOURCE_CFG, (sc->epb.res_cfg & 0x0fff) | 0x3000); 184 185 outw(BASE + EP_W0_PRODUCT_ID, sc->epb.prod_id); 186 187 if (sc->epb.mii_trans) { 188 /* 189 * turn on the MII transciever 190 */ 191 GO_WINDOW(3); 192 outw(BASE + EP_W3_OPTIONS, 0x8040); 193 DELAY(1000); 194 outw(BASE + EP_W3_OPTIONS, 0xc040); 195 outw(BASE + EP_COMMAND, RX_RESET); 196 outw(BASE + EP_COMMAND, TX_RESET); 197 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS); 198 DELAY(1000); 199 outw(BASE + EP_W3_OPTIONS, 0x8040); 200 } else { 201 ep_get_media(sc); 202 } 203 204 if ((error = ep_attach(sc))) { 205 device_printf(dev, "ep_attach() failed! (%d)\n", error); 206 goto bad; 207 } 208 209 if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr, 210 sc, &sc->ep_intrhand))) { 211 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error); 212 goto bad; 213 } 214 215 return (0); 216bad: 217 ep_free(dev); 218 return (error); 219} 220 221static void 222ep_pccard_detach(device_t dev) 223{ 224 struct ep_softc *sc = device_get_softc(dev); 225 226 printf("detach\n"); 227 228 if (sc->gone) { 229 device_printf(dev, "already unloaded\n"); 230 return; 231 } 232 sc->arpcom.ac_if.if_flags &= ~IFF_RUNNING; 233 if_down(&sc->arpcom.ac_if); 234 sc->gone = 1; 235 bus_teardown_intr(dev, sc->irq, sc->ep_intrhand); 236 ep_free(dev); 237 device_printf(dev, "unload\n"); 238} 239 240static device_method_t ep_pccard_methods[] = { 241 /* Device interface */ 242 DEVMETHOD(device_probe, ep_pccard_probe), 243 DEVMETHOD(device_attach, ep_pccard_attach), 244 DEVMETHOD(device_detach, ep_pccard_detach), 245 246 { 0, 0 } 247}; 248 249static driver_t ep_pccard_driver = { 250 "ep", 251 ep_pccard_methods, 252 sizeof(struct ep_softc), 253}; 254 255extern devclass_t ep_devclass; 256 257DRIVER_MODULE(ep, pccard, ep_pccard_driver, ep_devclass, 0, 0); 258