1/* $OpenBSD: if_sm_pcmcia.c,v 1.41 2024/05/26 08:46:28 jsg Exp $ */ 2/* $NetBSD: if_sm_pcmcia.c,v 1.11 1998/08/15 20:47:32 thorpej Exp $ */ 3 4/*- 5 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/timeout.h> 37#include <sys/device.h> 38 39#include <net/if.h> 40#include <net/if_media.h> 41 42#include <netinet/in.h> 43#include <netinet/if_ether.h> 44 45#include <machine/intr.h> 46#include <machine/bus.h> 47 48#include <dev/mii/miivar.h> 49 50#include <dev/ic/smc91cxxvar.h> 51 52#include <dev/pcmcia/pcmciareg.h> 53#include <dev/pcmcia/pcmciavar.h> 54#include <dev/pcmcia/pcmciadevs.h> 55 56int sm_pcmcia_match(struct device *, void *, void *); 57void sm_pcmcia_attach(struct device *, struct device *, void *); 58int sm_pcmcia_detach(struct device *, int); 59int sm_pcmcia_activate(struct device *, int); 60 61struct sm_pcmcia_softc { 62 struct smc91cxx_softc sc_smc; /* real "smc" softc */ 63 64 /* PCMCIA-specific goo. */ 65 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */ 66 int sc_io_window; /* our i/o window */ 67 void *sc_ih; /* interrupt cookie */ 68 struct pcmcia_function *sc_pf; /* our PCMCIA function */ 69}; 70 71const struct cfattach sm_pcmcia_ca = { 72 sizeof(struct sm_pcmcia_softc), sm_pcmcia_match, sm_pcmcia_attach, 73 sm_pcmcia_detach, sm_pcmcia_activate 74}; 75 76int sm_pcmcia_enable(struct smc91cxx_softc *); 77void sm_pcmcia_disable(struct smc91cxx_softc *); 78 79int sm_pcmcia_ascii_enaddr(const char *, u_int8_t *); 80int sm_pcmcia_funce_enaddr(struct device *, u_int8_t *); 81 82int sm_pcmcia_lannid_ciscallback(struct pcmcia_tuple *, void *); 83 84struct sm_pcmcia_product { 85 u_int16_t spp_vendor; /* vendor ID */ 86 u_int16_t spp_product; /* product ID */ 87 int spp_expfunc; /* expected function */ 88} sm_pcmcia_prod[] = { 89 { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_XJACK, 90 0 }, 91 { PCMCIA_VENDOR_MEGAHERTZ2, PCMCIA_PRODUCT_MEGAHERTZ2_XJEM1144, 92 0 }, 93 { PCMCIA_VENDOR_NEWMEDIA, PCMCIA_PRODUCT_NEWMEDIA_BASICS, 94 0 }, 95 { PCMCIA_VENDOR_SMC, PCMCIA_PRODUCT_SMC_8020, 96 0 }, 97 { PCMCIA_VENDOR_PSION, PCMCIA_PRODUCT_PSION_GOLDCARD, 98 0 } 99}; 100 101int 102sm_pcmcia_match(struct device *parent, void *match, void *aux) 103{ 104 struct pcmcia_attach_args *pa = aux; 105 int i; 106 107 for (i = 0; i < nitems(sm_pcmcia_prod); i++) 108 if (pa->manufacturer == sm_pcmcia_prod[i].spp_vendor && 109 pa->product == sm_pcmcia_prod[i].spp_product && 110 pa->pf->number == sm_pcmcia_prod[i].spp_expfunc) 111 return (1); 112 return (0); 113} 114 115void 116sm_pcmcia_attach(struct device *parent, struct device *self, void *aux) 117{ 118 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)self; 119 struct smc91cxx_softc *sc = &psc->sc_smc; 120 struct pcmcia_attach_args *pa = aux; 121 struct pcmcia_config_entry *cfe; 122 u_int8_t myla[ETHER_ADDR_LEN], *enaddr = NULL; 123 const char *intrstr; 124 125 psc->sc_pf = pa->pf; 126 cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head); 127 128 /* Enable the card. */ 129 pcmcia_function_init(pa->pf, cfe); 130 if (pcmcia_function_enable(pa->pf)) { 131 printf(": function enable failed\n"); 132 return; 133 } 134 135 /* XXX sanity check number of mem and i/o spaces */ 136 137 /* Allocate and map i/o space for the card. */ 138 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length, 139 cfe->iospace[0].length, &psc->sc_pcioh)) { 140 printf(": can't allocate i/o space\n"); 141 return; 142 } 143 144 sc->sc_bst = psc->sc_pcioh.iot; 145 sc->sc_bsh = psc->sc_pcioh.ioh; 146 147#ifdef notyet 148 sc->sc_enable = sm_pcmcia_enable; 149 sc->sc_disable = sm_pcmcia_disable; 150#endif 151 sc->sc_enabled = 1; 152 153 if (pcmcia_io_map(pa->pf, (cfe->flags & PCMCIA_CFE_IO16) ? 154 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8, 0, cfe->iospace[0].length, 155 &psc->sc_pcioh, &psc->sc_io_window)) { 156 printf(": can't map i/o space\n"); 157 return; 158 } 159 160 printf(" port 0x%lx/%lu", psc->sc_pcioh.addr, 161 (u_long)psc->sc_pcioh.size); 162 163 /* 164 * First try to get the Ethernet address from FUNCE/LANNID tuple. 165 */ 166 if (sm_pcmcia_funce_enaddr(parent, myla)) 167 enaddr = myla; 168 169 /* 170 * If that failed, try one of the CIS info strings. 171 */ 172 if (enaddr == NULL) { 173 char *cisstr = NULL; 174 175 switch (pa->manufacturer) { 176 case PCMCIA_VENDOR_MEGAHERTZ2: 177 cisstr = pa->pf->sc->card.cis1_info[3]; 178 break; 179 case PCMCIA_VENDOR_SMC: 180 cisstr = pa->pf->sc->card.cis1_info[2]; 181 break; 182 } 183 if (cisstr != NULL && sm_pcmcia_ascii_enaddr(cisstr, myla)) 184 enaddr = myla; 185 } 186 187 if (enaddr == NULL) 188 printf(", unable to get Ethernet address\n"); 189 190 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, 191 smc91cxx_intr, sc, sc->sc_dev.dv_xname); 192 intrstr = pcmcia_intr_string(psc->sc_pf, psc->sc_ih); 193 if (*intrstr) 194 printf(", %s", intrstr); 195 196 /* Perform generic initialization. */ 197 smc91cxx_attach(sc, enaddr); 198 199#ifdef notyet 200 pcmcia_function_disable(pa->pf); 201#endif 202} 203 204int 205sm_pcmcia_detach(struct device *dev, int flags) 206{ 207 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)dev; 208 struct ifnet *ifp = &psc->sc_smc.sc_arpcom.ac_if; 209 int rv = 0; 210 211 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window); 212 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh); 213 214 ether_ifdetach(ifp); 215 if_detach(ifp); 216 217 return (rv); 218} 219 220int 221sm_pcmcia_activate(struct device *dev, int act) 222{ 223 struct sm_pcmcia_softc *sc = (struct sm_pcmcia_softc *)dev; 224 struct ifnet *ifp = &sc->sc_smc.sc_arpcom.ac_if; 225 226 switch (act) { 227 case DVACT_DEACTIVATE: 228 ifp->if_timer = 0; 229 if (ifp->if_flags & IFF_RUNNING) 230 smc91cxx_stop(&sc->sc_smc); 231 if (sc->sc_ih) 232 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih); 233 sc->sc_ih = NULL; 234 pcmcia_function_disable(sc->sc_pf); 235 break; 236 } 237 return (0); 238} 239 240int 241sm_pcmcia_ascii_enaddr(const char *cisstr, u_int8_t *myla) 242{ 243 char enaddr_str[12]; 244 int i, j; 245 246 if (strlen(cisstr) != 12) { 247 /* Bogus address! */ 248 return (0); 249 } 250 bcopy(cisstr, enaddr_str, sizeof enaddr_str); 251 for (i = 0; i < 6; i++) { 252 for (j = 0; j < 2; j++) { 253 /* Convert to upper case. */ 254 if (enaddr_str[(i * 2) + j] >= 'a' && 255 enaddr_str[(i * 2) + j] <= 'z') 256 enaddr_str[(i * 2) + j] -= 'a' - 'A'; 257 258 /* Parse the digit. */ 259 if (enaddr_str[(i * 2) + j] >= '0' && 260 enaddr_str[(i * 2) + j] <= '9') 261 myla[i] |= enaddr_str[(i * 2) + j] 262 - '0'; 263 else if (enaddr_str[(i * 2) + j] >= 'A' && 264 enaddr_str[(i * 2) + j] <= 'F') 265 myla[i] |= enaddr_str[(i * 2) + j] 266 - 'A' + 10; 267 else { 268 /* Bogus digit!! */ 269 return (0); 270 } 271 272 /* Compensate for ordering of digits. */ 273 if (j == 0) 274 myla[i] <<= 4; 275 } 276 } 277 278 return (1); 279} 280 281int 282sm_pcmcia_funce_enaddr(struct device *parent, u_int8_t *myla) 283{ 284 285 return (pcmcia_scan_cis(parent, sm_pcmcia_lannid_ciscallback, myla)); 286} 287 288int 289sm_pcmcia_lannid_ciscallback(struct pcmcia_tuple *tuple, void *arg) 290{ 291 u_int8_t *myla = arg; 292 int i; 293 294 if (tuple->code == PCMCIA_CISTPL_FUNCE || tuple->code == 295 PCMCIA_CISTPL_SPCL) { 296 /* subcode, length */ 297 if (tuple->length < 2) 298 return (0); 299 300 if ((pcmcia_tuple_read_1(tuple, 0) != 301 PCMCIA_TPLFE_TYPE_LAN_NID) || 302 (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN)) 303 return (0); 304 305 for (i = 0; i < ETHER_ADDR_LEN; i++) 306 myla[i] = pcmcia_tuple_read_1(tuple, i + 2); 307 return (1); 308 } 309 return (0); 310} 311 312int 313sm_pcmcia_enable(struct smc91cxx_softc *sc) 314{ 315 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc; 316 317 /* Establish the interrupt handler. */ 318 psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, smc91cxx_intr, 319 sc, sc->sc_dev.dv_xname); 320 if (psc->sc_ih == NULL) { 321 printf("%s: couldn't establish interrupt handler\n", 322 sc->sc_dev.dv_xname); 323 return (1); 324 } 325 326 return (pcmcia_function_enable(psc->sc_pf)); 327} 328 329void 330sm_pcmcia_disable(struct smc91cxx_softc *sc) 331{ 332 struct sm_pcmcia_softc *psc = (struct sm_pcmcia_softc *)sc; 333 334 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih); 335 pcmcia_function_disable(psc->sc_pf); 336} 337