1 /* $NetBSD: if_ep_pcmcia.c,v 1.61 2008/04/28 20:23:56 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2000, 2004 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center, and by Charles M. Hannum. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1997 Marc Horowitz. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. All advertising materials mentioning features or use of this software 45 * must display the following acknowledgement: 46 * This product includes software developed by Marc Horowitz. 47 * 4. The name of the author may not be used to endorse or promote products 48 * derived from this software without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 52 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 53 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 54 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 55 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 56 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 57 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 59 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 60 */ 61 62 #include <sys/cdefs.h> 63 __KERNEL_RCSID(0, "$NetBSD: if_ep_pcmcia.c,v 1.61 2008/04/28 20:23:56 martin Exp $"); 64 65 #include <sys/param.h> 66 #include <sys/systm.h> 67 #include <sys/mbuf.h> 68 #include <sys/socket.h> 69 #include <sys/ioctl.h> 70 #include <sys/errno.h> 71 #include <sys/syslog.h> 72 #include <sys/select.h> 73 #include <sys/device.h> 74 75 #include <net/if.h> 76 #include <net/if_dl.h> 77 #include <net/if_ether.h> 78 #include <net/if_media.h> 79 80 #include <sys/cpu.h> 81 #include <sys/bus.h> 82 #include <sys/intr.h> 83 84 #include <dev/mii/miivar.h> 85 86 #include <dev/ic/elink3var.h> 87 #include <dev/ic/elink3reg.h> 88 89 #include <dev/pcmcia/pcmciareg.h> 90 #include <dev/pcmcia/pcmciavar.h> 91 #include <dev/pcmcia/pcmciadevs.h> 92 93 int ep_pcmcia_match(struct device *, struct cfdata *, void *); 94 void ep_pcmcia_attach(struct device *, struct device *, void *); 95 int ep_pcmcia_detach(struct device *, int); 96 97 int ep_pcmcia_get_enaddr(struct pcmcia_tuple *, void *); 98 int ep_pcmcia_enable(struct ep_softc *); 99 void ep_pcmcia_disable(struct ep_softc *); 100 101 void ep_pcmcia_disable1(struct ep_softc *); 102 103 struct ep_pcmcia_softc { 104 struct ep_softc sc_ep; /* real "ep" softc */ 105 106 /* PCMCIA-specific goo */ 107 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */ 108 int sc_io_window; /* our i/o window */ 109 struct pcmcia_function *sc_pf; /* our PCMCIA function */ 110 111 void *sc_powerhook; /* power management hook */ 112 }; 113 114 CFATTACH_DECL(ep_pcmcia, sizeof(struct ep_pcmcia_softc), 115 ep_pcmcia_match, ep_pcmcia_attach, ep_pcmcia_detach, ep_activate); 116 117 const struct ep_pcmcia_product { 118 struct pcmcia_product epp_product; 119 u_short epp_chipset; /* 3Com chipset used */ 120 int epp_flags; /* initial softc flags */ 121 } ep_pcmcia_products[] = { 122 { { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3C562, 123 PCMCIA_CIS_INVALID }, 124 ELINK_CHIPSET_3C509, 0 }, 125 126 { { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3C589, 127 PCMCIA_CIS_INVALID }, 128 ELINK_CHIPSET_3C509, 0 }, 129 130 { { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CXEM556, 131 PCMCIA_CIS_INVALID }, 132 ELINK_CHIPSET_3C509, 0 }, 133 134 { { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CXEM556INT, 135 PCMCIA_CIS_INVALID }, 136 ELINK_CHIPSET_3C509, 0 }, 137 138 { { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3C574, 139 PCMCIA_CIS_INVALID }, 140 ELINK_CHIPSET_ROADRUNNER, ELINK_FLAGS_MII }, 141 142 { { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3CCFEM556BI, 143 PCMCIA_CIS_INVALID }, 144 ELINK_CHIPSET_ROADRUNNER, ELINK_FLAGS_MII }, 145 146 { { PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3C1, 147 PCMCIA_CIS_INVALID }, 148 ELINK_CHIPSET_3C509, 0 }, 149 }; 150 const size_t ep_pcmcia_nproducts = 151 sizeof(ep_pcmcia_products) / sizeof(ep_pcmcia_products[0]); 152 153 int 154 ep_pcmcia_match(struct device *parent, struct cfdata *match, 155 void *aux) 156 { 157 struct pcmcia_attach_args *pa = aux; 158 159 /* This is to differentiate the serial function of some cards. */ 160 if (pa->pf->function != PCMCIA_FUNCTION_NETWORK) 161 return (0); 162 163 if (pcmcia_product_lookup(pa, ep_pcmcia_products, ep_pcmcia_nproducts, 164 sizeof(ep_pcmcia_products[0]), NULL)) 165 return (1); 166 return (0); 167 } 168 169 int 170 ep_pcmcia_enable(sc) 171 struct ep_softc *sc; 172 { 173 struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc; 174 struct pcmcia_function *pf = psc->sc_pf; 175 int error; 176 177 /* establish the interrupt. */ 178 sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, epintr, sc); 179 if (!sc->sc_ih) 180 return (EIO); 181 182 error = pcmcia_function_enable(pf); 183 if (error) { 184 pcmcia_intr_disestablish(pf, sc->sc_ih); 185 sc->sc_ih = 0; 186 return (error); 187 } 188 189 if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) || 190 (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) || 191 (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) { 192 int reg; 193 194 /* turn off the serial-disable bit */ 195 196 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION); 197 if (reg & 0x08) { 198 reg &= ~0x08; 199 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg); 200 } 201 202 } 203 204 return (0); 205 } 206 207 void 208 ep_pcmcia_disable(sc) 209 struct ep_softc *sc; 210 { 211 struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *) sc; 212 213 pcmcia_function_disable(psc->sc_pf); 214 pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih); 215 sc->sc_ih = 0; 216 } 217 218 void 219 ep_pcmcia_attach(parent, self, aux) 220 struct device *parent, *self; 221 void *aux; 222 { 223 struct ep_pcmcia_softc *psc = (void *) self; 224 struct ep_softc *sc = &psc->sc_ep; 225 struct pcmcia_attach_args *pa = aux; 226 struct pcmcia_config_entry *cfe; 227 const struct ep_pcmcia_product *epp; 228 u_int8_t myla[ETHER_ADDR_LEN]; 229 u_int8_t *enaddr = NULL; 230 int i; 231 int error; 232 233 psc->sc_pf = pa->pf; 234 235 SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) { 236 if (cfe->num_memspace != 0) 237 continue; 238 if (cfe->num_iospace != 1) 239 continue; 240 241 if (pa->product == PCMCIA_PRODUCT_3COM_3C562) { 242 /* 243 * the 3c562 can only use 0x??00-0x??7f 244 * according to the Linux driver 245 */ 246 247 /* 248 * 3c562 i/o may decodes address line not only A0-3 249 * but also A7. Anyway, we must sweep at most 250 * [0x0000, 0x0100). The address higher is given by a 251 * pcmcia bridge. But pcmcia bus-space allocation 252 * function implies cards will decode 10-bit address 253 * line. So we must search [0x0000, 0x0400). 254 * 255 * XXX: We must not check the bunch of I/O space range 256 * [0x400*n, 0x300 + 0x400*n) because they are 257 * reserved for legacy ISA devices and their alias 258 * images on PC/AT architecture. 259 */ 260 for (i = 0x0300; i < 0x0380; i += 0x10) { 261 if (pcmcia_io_alloc(pa->pf, i, 262 cfe->iospace[0].length, 263 cfe->iospace[0].length, 264 &psc->sc_pcioh) == 0) 265 break; 266 } 267 if (i != 0x0380) 268 break; 269 } else { 270 if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start, 271 cfe->iospace[0].length, cfe->iospace[0].length, 272 &psc->sc_pcioh) == 0) 273 break; 274 } 275 } 276 if (!cfe) { 277 aprint_error_dev(self, "failed to allocate I/O space\n"); 278 goto ioalloc_failed; 279 } 280 281 sc->sc_iot = psc->sc_pcioh.iot; 282 sc->sc_ioh = psc->sc_pcioh.ioh; 283 284 /* Enable the card. */ 285 pcmcia_function_init(pa->pf, cfe); 286 287 if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ? 288 PCMCIA_WIDTH_AUTO : PCMCIA_WIDTH_IO8), &psc->sc_pcioh, 289 &psc->sc_io_window)) { 290 aprint_error_dev(self, "can't map i/o space\n"); 291 goto iomap_failed; 292 } 293 294 error = ep_pcmcia_enable(sc); 295 if (error) 296 goto enable_failed; 297 sc->enabled = 1; 298 299 switch (pa->product) { 300 case PCMCIA_PRODUCT_3COM_3C562: 301 /* 302 * 3c562a-c use this; 3c562d does it in the regular way. 303 * we might want to check the revision and produce a warning 304 * in the future. 305 */ 306 /* FALLTHROUGH */ 307 case PCMCIA_PRODUCT_3COM_3C574: 308 case PCMCIA_PRODUCT_3COM_3CCFEM556BI: 309 /* 310 * Apparently, some 3c574s do it this way, as well. 311 */ 312 if (pcmcia_scan_cis(parent, ep_pcmcia_get_enaddr, myla)) 313 enaddr = myla; 314 break; 315 } 316 317 epp = pcmcia_product_lookup(pa, ep_pcmcia_products, ep_pcmcia_nproducts, 318 sizeof(ep_pcmcia_products[0]), NULL); 319 if (!epp) 320 panic("ep_pcmcia_attach: impossible"); 321 322 sc->bustype = ELINK_BUS_PCMCIA; 323 sc->ep_flags = epp->epp_flags; 324 325 sc->enable = ep_pcmcia_enable; 326 sc->disable = ep_pcmcia_disable; 327 328 if (epconfig(sc, epp->epp_chipset, enaddr)) 329 aprint_error_dev(self, "couldn't configure controller\n"); 330 331 psc->sc_powerhook = powerhook_establish(device_xname(self), ep_power, sc); 332 if (psc->sc_powerhook == NULL) 333 aprint_error_dev(self, "WARNING: unable to establish power hook\n"); 334 335 sc->enabled = 0; 336 ep_pcmcia_disable(sc); 337 return; 338 339 enable_failed: 340 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window); 341 iomap_failed: 342 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh); 343 ioalloc_failed: 344 psc->sc_io_window = -1; 345 } 346 347 int 348 ep_pcmcia_detach(self, flags) 349 struct device *self; 350 int flags; 351 { 352 struct ep_pcmcia_softc *psc = (struct ep_pcmcia_softc *)self; 353 int rv; 354 355 if (psc->sc_io_window == -1) 356 /* Nothing to detach. */ 357 return (0); 358 359 if (psc->sc_powerhook != NULL) 360 powerhook_disestablish(psc->sc_powerhook); 361 362 rv = ep_detach(self, flags); 363 if (rv != 0) 364 return (rv); 365 366 /* Unmap our i/o window. */ 367 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window); 368 369 /* Free our i/o space. */ 370 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh); 371 372 return (0); 373 } 374 375 int 376 ep_pcmcia_get_enaddr(tuple, arg) 377 struct pcmcia_tuple *tuple; 378 void *arg; 379 { 380 u_int8_t *myla = arg; 381 int i; 382 383 /* this is 3c562a-c magic */ 384 if (tuple->code == 0x88) { 385 if (tuple->length < ETHER_ADDR_LEN) 386 return (0); 387 388 for (i = 0; i < ETHER_ADDR_LEN; i += 2) { 389 myla[i] = pcmcia_tuple_read_1(tuple, i + 1); 390 myla[i + 1] = pcmcia_tuple_read_1(tuple, i); 391 } 392 393 return (1); 394 } 395 return (0); 396 } 397