1 /* $Id: cardbus.c,v 1.1.2.1 1999/02/16 16:44:35 haya Exp $ */ 2 3 /* 4 * Copyright (c) 1997 and 1998 HAYAKAWA Koichi. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by HAYAKAWA Koichi. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 /* FreeBSD/newconfig version. UCHIYAMA Yasushi 1999 */ 34 /* $FreeBSD$ */ 35 #define CARDBUS_DEBUG 36 37 #include <sys/types.h> 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/device.h> 41 #include <sys/malloc.h> 42 #include <sys/kernel.h> 43 #include <sys/syslog.h> 44 45 #include <machine/bus.h> 46 47 #include <dev/cardbus/pccardcis.h> 48 49 #include <dev/cardbus/cardbusreg.h> 50 #include <dev/cardbus/cardbusvar.h> 51 52 #include <dev/pcmcia/pcmciareg.h> 53 #include <dev/pcmcia/pcmciavar.h> 54 55 #include <dev/ic/i82365reg.h> 56 #include <dev/ic/i82365reg.h> 57 58 #include <dev/pci/pccbbreg.h> 59 #include <dev/pci/pccbbvar.h> 60 61 #if defined CARDBUS_DEBUG 62 #define STATIC 63 #define DPRINTF(a) printf a 64 #define DDELAY(x) delay((x)*1000*1000) 65 #else 66 #define STATIC static 67 #define DPRINTF(a) 68 #endif 69 70 STATIC void cardbusattach __P((struct device *, struct device *, void *)); 71 STATIC int cardbusmatch __P((struct device *, struct cfdata *, void *)); 72 static int cardbussubmatch __P((struct device *, struct cfdata *, void *)); 73 static int cardbusprint __P((void *, const char *)); 74 75 static u_int8_t *decode_tuple __P((u_int8_t *)); 76 static int decode_tuples __P((u_int8_t *, int)); 77 static char *tuple_name __P((int)); 78 79 struct cfattach cardbus_ca = { 80 sizeof(struct cardbus_softc), cardbusmatch, cardbusattach 81 }; 82 83 STATIC int 84 cardbusmatch(parent, cf, aux) 85 struct device *parent; 86 struct cfdata *cf; 87 void *aux; 88 { 89 struct cbslot_attach_args *cba = aux; 90 91 /* which slot? */ 92 if (cf->cbslotcf_slot != CBSLOT_UNK_SLOT && 93 cf->cbslotcf_slot != cba->cba_function) { 94 95 DPRINTF(("cardbusmatch: function differs %d <=> %d\n", 96 cf->cbslotcf_slot, cba->cba_function)); 97 98 return 0; 99 } 100 101 if (cba->cba_function < 0 || cba->cba_function > 255) { 102 return 0; 103 } 104 105 return 1; 106 } 107 108 void 109 cardslot_if_setup (struct cardbus_softc *csc) 110 { 111 csc->sc_if.if_card_attach = cardbus_attach_card; 112 } 113 114 STATIC void 115 cardbusattach(parent, self, aux) 116 struct device *parent; 117 struct device *self; 118 void *aux; 119 { 120 struct pccbb_softc *psc = (struct pccbb_softc *)parent; 121 struct cardbus_softc *sc = (void *)self; 122 struct cbslot_attach_args *cba = aux; 123 int cdstatus; 124 125 sc->sc_bus = cba->cba_bus; 126 sc->sc_device = cba->cba_function; 127 sc->sc_intrline = cba->cba_intrline; 128 129 printf(" bus %d device %d\n", sc->sc_bus, sc->sc_device); 130 131 sc->sc_iot = cba->cba_iot; /* CardBus I/O space tag */ 132 sc->sc_memt = cba->cba_memt; /* CardBus MEM space tag */ 133 sc->sc_dmat = cba->cba_dmat; /* DMA tag */ 134 135 sc->sc_cc = cba->cba_cc; 136 sc->sc_cf = cba->cba_cf; 137 cardslot_if_setup (sc); 138 cdstatus = 0; 139 140 if ((cdstatus = (sc->sc_cf->cardbus_ctrl)(sc->sc_cc, CARDBUS_CD))) { 141 DPRINTF(("cardbusattach: CardBus card found [0x%x]\n", cdstatus)); 142 psc->sc_cbdev = cardbus_attach_card(sc); 143 } 144 } 145 146 /********************************************************************** 147 * int cardbus_attach_card(struct cardbus_softc *sc) 148 * This functions attaches the card on the slot: turns on power, 149 * reads and analyses tuple, sets consifuration index. 150 ***********************************************************************/ 151 struct device * 152 cardbus_attach_card(sc) 153 struct cardbus_softc *sc; 154 { 155 struct device *attached_device = NULL; 156 cardbus_chipset_tag_t cc; 157 cardbus_function_tag_t cf; 158 int cdstatus; 159 cardbustag_t tag; 160 cardbusreg_t id, class, cis_ptr, bhlcr; 161 u_int8_t tuple[2048]; 162 int function, max_func, device; 163 164 cc = sc->sc_cc; 165 cf = sc->sc_cf; 166 167 DPRINTF(("cardbus_attach_card: cb%d start\n", sc->sc_dev.dv_unit)); 168 169 /* inspect initial voltage */ 170 if (0 == (cdstatus = (cf->cardbus_ctrl)(cc, CARDBUS_CD))) { 171 DPRINTF(("cardbusattach: no CardBus card on cb%d\n", sc->sc_dev.dv_unit)); 172 return 0; 173 } 174 175 if (cdstatus & CARDBUS_3V_CARD) { 176 cf->cardbus_power(cc, CARDBUS_VCC_3V); 177 } 178 (cf->cardbus_ctrl)(cc, CARDBUS_RESET); 179 180 device = 0; /* Only one card can attach cardbus slot */ 181 function = 0; 182 183 tag = cardbus_make_tag (cc, cf, sc->sc_bus, device, function); 184 185 bhlcr = (cf->cardbus_conf_read)(cc, tag, CARDBUS_BHLC_REG); 186 max_func = CARDBUS_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1; 187 188 for (function = 0; function < max_func; function++) { 189 if (function) 190 tag = cardbus_make_tag (cc, cf, sc->sc_bus, device, function); 191 192 id = (cf->cardbus_conf_read)(cc, tag, CARDBUS_ID_REG); 193 if (CARDBUS_VENDOR(id) == 0xffff || CARDBUS_VENDOR(id) == 0) { 194 cardbus_free_tag (cc, cf, tag); 195 continue; 196 } 197 198 class = (cf->cardbus_conf_read)(cc, tag, CARDBUS_CLASS_REG); 199 cis_ptr = (cf->cardbus_conf_read)(cc, tag, CARDBUS_CIS_REG); 200 201 DPRINTF(("cardbus_attach_card: Vendor 0x%x, Product 0x%x, CIS 0x%x\n", 202 CARDBUS_VENDOR(id), CARDBUS_PRODUCT(id), cis_ptr)); 203 204 bzero(tuple, 2048); 205 206 if (0 == (cis_ptr & CARDBUS_CIS_ASIMASK)) { 207 int i = cis_ptr & CARDBUS_CIS_ADDRMASK; 208 int j = 0; 209 210 for (; i < 0xff; i += 4) { 211 u_int32_t e = (cf->cardbus_conf_read)(cc, tag, i); 212 tuple[j] = 0xff & e; 213 e >>= 8; 214 tuple[j + 1] = 0xff & e; 215 e >>= 8; 216 tuple[j + 2] = 0xff & e; 217 e >>= 8; 218 tuple[j + 3] = 0xff & e; 219 j += 4; 220 } 221 } 222 223 decode_tuples(tuple, 2048); 224 if (function == 0) { 225 struct cardbus_attach_args ca; 226 cardbusreg_t intr = cardbus_conf_read(cc, cf, tag, CARDBUS_INTERRUPT_REG); 227 228 ca.ca_unit = sc->sc_dev.dv_unit; 229 ca.ca_cc = sc->sc_cc; 230 ca.ca_cf = sc->sc_cf; 231 232 ca.ca_iot = sc->sc_iot; 233 ca.ca_memt = sc->sc_memt; 234 ca.ca_dmat = sc->sc_dmat; 235 236 ca.ca_tag = tag; 237 ca.ca_device = device; 238 ca.ca_function = function; 239 ca.ca_id = id; 240 ca.ca_class = class; 241 242 ca.ca_intrline = sc->sc_intrline; 243 244 attached_device = config_found_sm((void *)sc, &ca, cardbusprint, cardbussubmatch); 245 } else { 246 printf ("cardbus_attach_card: XXX Multi-function can't handle. function 0 only.\n"); 247 } 248 cardbus_free_tag (cc, cf, tag); 249 } 250 if (!attached_device) 251 cf->cardbus_power(cc, CARDBUS_VCC_0V); 252 return attached_device; 253 } 254 255 static int 256 cardbussubmatch(parent, cf, aux) 257 struct device *parent; 258 struct cfdata *cf; 259 void *aux; 260 { 261 struct cardbus_attach_args *ca = aux; 262 263 if (cf->cardbuscf_dev != CARDBUS_UNK_DEV && 264 cf->cardbuscf_dev != ca->ca_unit) { 265 return 0; 266 } 267 if (cf->cardbuscf_function != CARDBUS_UNK_FUNCTION && 268 cf->cardbuscf_function != ca->ca_function) { 269 return 0; 270 } 271 272 return ((*cf->cf_attach->ca_match)(parent, cf, aux)); 273 } 274 275 static int 276 cardbusprint(aux, pnp) 277 void *aux; 278 const char *pnp; 279 { 280 register struct cardbus_attach_args *ca = aux; 281 char devinfo[256]; 282 283 if (pnp) { 284 printf("vendor 0x%04x id 0x%04x at %s", 285 CARDBUS_VENDOR(ca->ca_id), CARDBUS_PRODUCT(ca->ca_id), pnp); 286 } 287 printf(" dev %d function %d", ca->ca_device, ca->ca_function); 288 return UNCONF; 289 } 290 291 /********************************************************************** 292 * void *cardbus_intr_establish(cc, cf, irq, level, func, arg) 293 * Interrupt handler of pccard. 294 * args: 295 * cardbus_chipset_tag_t *cc 296 * int irq: 297 **********************************************************************/ 298 void * 299 cardbus_intr_establish(cc, cf, irq, level, func, arg) 300 cardbus_chipset_tag_t cc; 301 cardbus_function_tag_t cf; 302 cardbus_intr_handle_t irq; 303 int level; 304 int (*func) __P((void *)); 305 void *arg; 306 { 307 DPRINTF(("- cardbus_intr_establish: irq %d\n", irq)); 308 309 return (*cf->cardbus_intr_establish)(cc, irq, level, func, arg); 310 } 311 312 /********************************************************************** 313 * void cardbus_intr_disestablish(cc, cf, handler) 314 * Interrupt handler of pccard. 315 * args: 316 * cardbus_chipset_tag_t *cc 317 **********************************************************************/ 318 void 319 cardbus_intr_disestablish(cc, cf, handler) 320 cardbus_chipset_tag_t cc; 321 cardbus_function_tag_t cf; 322 void *handler; 323 { 324 DPRINTF(("- cardbus_intr_disestablish\n")); 325 326 (*cf->cardbus_intr_disestablish)(cc, handler); 327 return; 328 } 329 330 /********************************************************************** 331 * below this line, there are some functions for decoding tuples. 332 * They should go out from this file. 333 **********************************************************************/ 334 static int 335 decode_tuples(tuple, buflen) 336 u_int8_t *tuple; 337 int buflen; 338 { 339 u_int8_t *tp = tuple; 340 341 if (CISTPL_LINKTARGET != *tuple) { 342 DPRINTF(("WRONG TUPLE\n")); 343 return 0; 344 } 345 346 while (NULL != (tp = decode_tuple(tp))) { 347 if (tuple + buflen < tp) { 348 break; 349 } 350 } 351 352 return 1; 353 } 354 355 static u_int8_t * 356 decode_tuple(tuple) 357 u_int8_t *tuple; 358 { 359 u_int8_t type; 360 u_int8_t len; 361 int i; 362 363 type = tuple[0]; 364 len = tuple[1] + 2; 365 366 printf("tuple: %s len %d\n", tuple_name(type), len); 367 if (CISTPL_END == type) { 368 return NULL; 369 } 370 371 for (i = 0; i < len; ++i) { 372 if (i % 16 == 0) { 373 printf(" 0x%2x:", i); 374 } 375 printf(" %x",tuple[i]); 376 if (i % 16 == 15) { 377 printf("\n"); 378 } 379 } 380 if (i % 16 != 0) { 381 printf("\n"); 382 } 383 384 return tuple + len; 385 } 386 387 static char * 388 tuple_name(type) 389 int type; 390 { 391 static char *tuple_name_s [] = { 392 "TPL_NULL", "TPL_DEVICE", "Reserved", "Reserved", /* 0-3 */ 393 "CONFIG_CB", "CFTABLE_ENTRY_CB", "Reserved", "BAR", /* 4-7 */ 394 "Reserved", "Reserved", "Reserved", "Reserved", /* 8-B */ 395 "Reserved", "Reserved", "Reserved", "Reserved", /* C-F */ 396 "CHECKSUM", "LONGLINK_A", "LONGLINK_C", "LINKTARGET", /* 10-13 */ 397 "NO_LINK", "VERS_1", "ALTSTR", "DEVICE_A", 398 "JEDEC_C", "JEDEC_A", "CONFIG", "CFTABLE_ENTRY", 399 "DEVICE_OC", "DEVICE_OA", "DEVICE_GEO", "DEVICE_GEO_A", 400 "MANFID", "FUNCID", "FUNCE", "SWIL", /* 20-23 */ 401 "Reserved", "Reserved", "Reserved", "Reserved", /* 24-27 */ 402 "Reserved", "Reserved", "Reserved", "Reserved", /* 28-2B */ 403 "Reserved", "Reserved", "Reserved", "Reserved", /* 2C-2F */ 404 "Reserved", "Reserved", "Reserved", "Reserved", /* 30-33 */ 405 "Reserved", "Reserved", "Reserved", "Reserved", /* 34-37 */ 406 "Reserved", "Reserved", "Reserved", "Reserved", /* 38-3B */ 407 "Reserved", "Reserved", "Reserved", "Reserved", /* 3C-3F */ 408 "VERS_2", "FORMAT", "GEOMETRY", "BYTEORDER", 409 "DATE", "BATTERY", "ORG" 410 }; 411 #define NAME_LEN(x) (sizeof x / sizeof(x[0])) 412 413 if (type > 0 && type < NAME_LEN(tuple_name_s)) { 414 return tuple_name_s[type]; 415 } else if (0xff == type) { 416 return "END"; 417 } else { 418 return "Reserved"; 419 } 420 } 421