1 /* $NetBSD: isa.c,v 1.118 2004/08/30 15:05:19 drochner Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum; by Jason R. Thorpe of Wasabi Systems, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: isa.c,v 1.118 2004/08/30 15:05:19 drochner Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/malloc.h> 46 #include <sys/device.h> 47 48 #include <machine/intr.h> 49 50 #include <dev/isa/isareg.h> 51 #include <dev/isa/isavar.h> 52 #include <dev/isa/isadmareg.h> 53 54 #include "isadma.h" 55 56 #include "isapnp.h" 57 #if NISAPNP > 0 58 #include <dev/isapnp/isapnpreg.h> 59 #include <dev/isapnp/isapnpvar.h> 60 #endif 61 62 int isamatch(struct device *, struct cfdata *, void *); 63 void isaattach(struct device *, struct device *, void *); 64 int isarescan(struct device *, const char *, const int *); 65 void isachilddetached(struct device *, struct device *); 66 int isaprint(void *, const char *); 67 68 CFATTACH_DECL2(isa, sizeof(struct isa_softc), 69 isamatch, isaattach, NULL, NULL, isarescan, isachilddetached); 70 71 void isa_attach_knowndevs(struct isa_softc *); 72 void isa_free_knowndevs(struct isa_softc *); 73 74 int isasubmatch(struct device *, struct cfdata *, const locdesc_t *, void *); 75 int isasearch(struct device *, struct cfdata *, const locdesc_t *, void *); 76 77 int 78 isamatch(struct device *parent, struct cfdata *cf, void *aux) 79 { 80 /* XXX check other indicators */ 81 82 return (1); 83 } 84 85 void 86 isaattach(struct device *parent, struct device *self, void *aux) 87 { 88 struct isa_softc *sc = (struct isa_softc *)self; 89 struct isabus_attach_args *iba = aux; 90 static const int wildcard[7] = { 91 ISACF_PORT_DEFAULT, ISACF_SIZE_DEFAULT, 92 ISACF_IOMEM_DEFAULT, ISACF_IOSIZ_DEFAULT, 93 ISACF_IRQ_DEFAULT, ISACF_DRQ_DEFAULT, ISACF_DRQ2_DEFAULT 94 }; 95 96 TAILQ_INIT(&sc->sc_knowndevs); 97 sc->sc_dynamicdevs = 0; 98 99 isa_attach_hook(parent, self, iba); 100 printf("\n"); 101 102 /* XXX Add code to fetch known-devices. */ 103 104 sc->sc_iot = iba->iba_iot; 105 sc->sc_memt = iba->iba_memt; 106 sc->sc_dmat = iba->iba_dmat; 107 sc->sc_ic = iba->iba_ic; 108 109 #if NISAPNP > 0 110 /* 111 * Reset isapnp cards that the bios configured for us 112 */ 113 isapnp_isa_attach_hook(sc); 114 #endif 115 116 #if NISADMA > 0 117 /* 118 * Initialize our DMA state. 119 */ 120 isa_dmainit(sc->sc_ic, sc->sc_iot, sc->sc_dmat, self); 121 #endif 122 123 /* Attach all direct-config children. */ 124 isa_attach_knowndevs(sc); 125 126 /* 127 * If we don't support dynamic hello/goodbye of devices, 128 * then free the knowndevs info now. 129 */ 130 if (sc->sc_dynamicdevs == 0) 131 isa_free_knowndevs(sc); 132 133 /* Attach all indrect-config children. */ 134 isarescan(self, "isa", wildcard); 135 } 136 137 int 138 isarescan(struct device *self, const char *ifattr, const int *locators) 139 { 140 int help[8]; 141 locdesc_t *ldesc = (void *)help; /* XXX */; 142 143 ldesc->len = 7; 144 memcpy(&ldesc->locs, locators, 7 * sizeof(int)); 145 146 /* 147 * XXX Bus independant code calling this function does not 148 * know the locator default values. It assumes "-1" for now. 149 * (should be made available by "config" one day) 150 * So fixup where the "-1" is not correct. 151 */ 152 if (ldesc->locs[ISACF_SIZE] == -1) 153 ldesc->locs[ISACF_SIZE] = ISACF_SIZE_DEFAULT; 154 if (ldesc->locs[ISACF_IOSIZ] == -1) 155 ldesc->locs[ISACF_IOSIZ] = ISACF_IOSIZ_DEFAULT; 156 157 config_search_loc(isasearch, self, ifattr, ldesc, NULL); 158 return (0); 159 } 160 161 void 162 isachilddetached(struct device *self, struct device *child) 163 { 164 /* nothing to do */ 165 } 166 167 void 168 isa_attach_knowndevs(struct isa_softc *sc) 169 { 170 struct isa_attach_args ia; 171 struct isa_knowndev *ik; 172 173 if (TAILQ_EMPTY(&sc->sc_knowndevs)) 174 return; 175 176 TAILQ_FOREACH(ik, &sc->sc_knowndevs, ik_list) { 177 if (ik->ik_claimed != NULL) 178 continue; 179 180 ia.ia_iot = sc->sc_iot; 181 ia.ia_memt = sc->sc_memt; 182 ia.ia_dmat = sc->sc_dmat; 183 ia.ia_ic = sc->sc_ic; 184 185 ia.ia_pnpname = ik->ik_pnpname; 186 ia.ia_pnpcompatnames = ik->ik_pnpcompatnames; 187 188 ia.ia_io = ik->ik_io; 189 ia.ia_nio = ik->ik_nio; 190 191 ia.ia_iomem = ik->ik_iomem; 192 ia.ia_niomem = ik->ik_niomem; 193 194 ia.ia_irq = ik->ik_irq; 195 ia.ia_nirq = ik->ik_nirq; 196 197 ia.ia_drq = ik->ik_drq; 198 ia.ia_ndrq = ik->ik_ndrq; 199 200 ia.ia_aux = NULL; 201 202 /* XXX should setup locator array */ 203 204 ik->ik_claimed = config_found_sm_loc(&sc->sc_dev, 205 "isa", 0, &ia, isaprint, isasubmatch); 206 } 207 } 208 209 void 210 isa_free_knowndevs(struct isa_softc *sc) 211 { 212 struct isa_knowndev *ik; 213 struct isa_pnpname *ipn; 214 215 #define FREEIT(x) if (x != NULL) free(x, M_DEVBUF) 216 217 while ((ik = TAILQ_FIRST(&sc->sc_knowndevs)) != NULL) { 218 TAILQ_REMOVE(&sc->sc_knowndevs, ik, ik_list); 219 FREEIT(ik->ik_pnpname); 220 while ((ipn = ik->ik_pnpcompatnames) != NULL) { 221 ik->ik_pnpcompatnames = ipn->ipn_next; 222 free(ipn->ipn_name, M_DEVBUF); 223 free(ipn, M_DEVBUF); 224 } 225 FREEIT(ik->ik_io); 226 FREEIT(ik->ik_iomem); 227 FREEIT(ik->ik_irq); 228 FREEIT(ik->ik_drq); 229 free(ik, M_DEVBUF); 230 } 231 232 #undef FREEIT 233 } 234 235 static int 236 checkattachargs(struct isa_attach_args *ia, const int *loc) 237 { 238 int i; 239 240 if (ia->ia_nio == 0) { 241 if (loc[ISACF_PORT] != ISACF_PORT_DEFAULT) 242 return (0); 243 } else { 244 if (loc[ISACF_PORT] != ISACF_PORT_DEFAULT && 245 loc[ISACF_PORT] != ia->ia_io[0].ir_addr) 246 return (0); 247 } 248 249 if (ia->ia_niomem == 0) { 250 if (loc[ISACF_IOMEM] != ISACF_IOMEM_DEFAULT) 251 return (0); 252 } else { 253 if (loc[ISACF_IOMEM] != ISACF_IOMEM_DEFAULT && 254 loc[ISACF_IOMEM] != ia->ia_iomem[0].ir_addr) 255 return (0); 256 } 257 258 if (ia->ia_nirq == 0) { 259 if (loc[ISACF_IRQ] != ISACF_IRQ_DEFAULT) 260 return (0); 261 } else { 262 if (loc[ISACF_IRQ] != ISACF_IRQ_DEFAULT && 263 loc[ISACF_IRQ] != ia->ia_irq[0].ir_irq) 264 return (0); 265 } 266 267 if (ia->ia_ndrq == 0) { 268 if (loc[ISACF_DRQ] != ISACF_DRQ_DEFAULT) 269 return (0); 270 if (loc[ISACF_DRQ2] != ISACF_DRQ2_DEFAULT) 271 return (0); 272 } else { 273 for (i = 0; i < 2; i++) { 274 if (i == ia->ia_ndrq) 275 break; 276 if (loc[ISACF_DRQ + i] != ISACF_DRQ_DEFAULT && 277 loc[ISACF_DRQ + i] != ia->ia_drq[i].ir_drq) 278 return (0); 279 } 280 for (; i < 2; i++) { 281 if (loc[ISACF_DRQ + i] != ISACF_DRQ_DEFAULT) 282 return (0); 283 } 284 } 285 286 return (1); 287 } 288 289 int 290 isasubmatch(struct device *parent, struct cfdata *cf, 291 const locdesc_t *ldesc, void *aux) 292 { 293 struct isa_attach_args *ia = aux; 294 295 if (!checkattachargs(ia, cf->cf_loc)) 296 return (0); 297 298 return (config_match(parent, cf, aux)); 299 } 300 301 int 302 isaprint(void *aux, const char *isa) 303 { 304 struct isa_attach_args *ia = aux; 305 const char *sep; 306 int i; 307 308 /* 309 * This block of code only fires if we have a direct-config'd 310 * device for which there is no driver match. 311 */ 312 if (isa != NULL) { 313 struct isa_pnpname *ipn; 314 315 if (ia->ia_pnpname != NULL) 316 aprint_normal("%s", ia->ia_pnpname); 317 if ((ipn = ia->ia_pnpcompatnames) != NULL) { 318 aprint_normal(" ("); /* ) */ 319 for (sep = ""; ipn != NULL; 320 ipn = ipn->ipn_next, sep = " ") { 321 aprint_normal("%s%s", sep, ipn->ipn_name); 322 } 323 /* ( */ aprint_normal(")"); 324 } 325 aprint_normal(" at %s", isa); 326 } 327 328 if (ia->ia_nio) { 329 sep = ""; 330 aprint_normal(" port "); 331 for (i = 0; i < ia->ia_nio; i++) { 332 if (ia->ia_io[i].ir_size == 0) 333 continue; 334 aprint_normal("%s0x%x", sep, ia->ia_io[i].ir_addr); 335 if (ia->ia_io[i].ir_size > 1) 336 aprint_normal("-0x%x", ia->ia_io[i].ir_addr + 337 ia->ia_io[i].ir_size - 1); 338 sep = ","; 339 } 340 } 341 342 if (ia->ia_niomem) { 343 sep = ""; 344 aprint_normal(" iomem "); 345 for (i = 0; i < ia->ia_niomem; i++) { 346 if (ia->ia_iomem[i].ir_size == 0) 347 continue; 348 aprint_normal("%s0x%x", sep, ia->ia_iomem[i].ir_addr); 349 if (ia->ia_iomem[i].ir_size > 1) 350 aprint_normal("-0x%x", ia->ia_iomem[i].ir_addr + 351 ia->ia_iomem[i].ir_size - 1); 352 sep = ","; 353 } 354 } 355 356 if (ia->ia_nirq) { 357 sep = ""; 358 aprint_normal(" irq "); 359 for (i = 0; i < ia->ia_nirq; i++) { 360 if (ia->ia_irq[i].ir_irq == ISACF_IRQ_DEFAULT) 361 continue; 362 aprint_normal("%s%d", sep, ia->ia_irq[i].ir_irq); 363 sep = ","; 364 } 365 } 366 367 if (ia->ia_ndrq) { 368 sep = ""; 369 aprint_normal(" drq "); 370 for (i = 0; i < ia->ia_ndrq; i++) { 371 if (ia->ia_drq[i].ir_drq == ISACF_DRQ_DEFAULT) 372 continue; 373 aprint_normal("%s%d", sep, ia->ia_drq[i].ir_drq); 374 sep = ","; 375 } 376 } 377 378 return (UNCONF); 379 } 380 381 int 382 isasearch(struct device *parent, struct cfdata *cf, 383 const locdesc_t *ldesc, void *aux) 384 { 385 struct isa_io res_io[1]; 386 struct isa_iomem res_mem[1]; 387 struct isa_irq res_irq[1]; 388 struct isa_drq res_drq[2]; 389 struct isa_softc *sc = (struct isa_softc *)parent; 390 struct isa_attach_args ia; 391 int help[8]; 392 locdesc_t *locs = (void *)&help; /* XXX */ 393 int tryagain; 394 395 do { 396 ia.ia_pnpname = NULL; 397 ia.ia_pnpcompatnames = NULL; 398 399 res_io[0].ir_addr = cf->cf_loc[ISACF_PORT]; 400 res_io[0].ir_size = 0; 401 402 res_mem[0].ir_addr = cf->cf_loc[ISACF_IOMEM]; 403 res_mem[0].ir_size = cf->cf_loc[ISACF_IOSIZ]; 404 405 res_irq[0].ir_irq = 406 cf->cf_loc[ISACF_IRQ] == 2 ? 9 : cf->cf_loc[ISACF_IRQ]; 407 408 res_drq[0].ir_drq = cf->cf_loc[ISACF_DRQ]; 409 res_drq[1].ir_drq = cf->cf_loc[ISACF_DRQ2]; 410 411 ia.ia_iot = sc->sc_iot; 412 ia.ia_memt = sc->sc_memt; 413 ia.ia_dmat = sc->sc_dmat; 414 ia.ia_ic = sc->sc_ic; 415 416 ia.ia_io = res_io; 417 ia.ia_nio = 1; 418 419 ia.ia_iomem = res_mem; 420 ia.ia_niomem = 1; 421 422 ia.ia_irq = res_irq; 423 ia.ia_nirq = 1; 424 425 ia.ia_drq = res_drq; 426 ia.ia_ndrq = 2; 427 428 if (!checkattachargs(&ia, ldesc->locs)) 429 return (0); 430 431 tryagain = 0; 432 if (config_match(parent, cf, &ia) > 0) { 433 /* 434 * This is not necessary for detach, but might 435 * still be useful to collect device information. 436 */ 437 locs->len = 7; 438 locs->locs[ISACF_PORT] = ia.ia_io[0].ir_addr; 439 locs->locs[ISACF_SIZE] = ia.ia_io[0].ir_size; 440 locs->locs[ISACF_IOMEM] = ia.ia_iomem[0].ir_addr; 441 locs->locs[ISACF_IOSIZ] = ia.ia_iomem[0].ir_size; 442 locs->locs[ISACF_IRQ] = ia.ia_irq[0].ir_irq; 443 locs->locs[ISACF_DRQ] = ia.ia_drq[0].ir_drq; 444 locs->locs[ISACF_DRQ2] = ia.ia_drq[1].ir_drq; 445 config_attach_loc(parent, cf, locs, &ia, isaprint); 446 tryagain = (cf->cf_fstate == FSTATE_STAR); 447 } 448 } while (tryagain); 449 450 return (0); 451 } 452 453 char * 454 isa_intr_typename(int type) 455 { 456 457 switch (type) { 458 case IST_NONE : 459 return ("none"); 460 case IST_PULSE: 461 return ("pulsed"); 462 case IST_EDGE: 463 return ("edge-triggered"); 464 case IST_LEVEL: 465 return ("level-triggered"); 466 default: 467 panic("isa_intr_typename: invalid type %d", type); 468 } 469 } 470