1 /* $NetBSD: tc.c,v 1.28 2000/06/04 19:15:14 cgd Exp $ */ 2 3 /* 4 * Copyright (c) 1994, 1995 Carnegie-Mellon University. 5 * All rights reserved. 6 * 7 * Author: Chris G. Demetriou 8 * 9 * Permission to use, copy, modify and distribute this software and 10 * its documentation is hereby granted, provided that both the copyright 11 * notice and this permission notice appear in all copies of the 12 * software, derivative works or modified versions, and any portions 13 * thereof, and that both notices appear in supporting documentation. 14 * 15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 18 * 19 * Carnegie Mellon requests users of this software to return to 20 * 21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 22 * School of Computer Science 23 * Carnegie Mellon University 24 * Pittsburgh PA 15213-3890 25 * 26 * any improvements or extensions that they make and grant Carnegie the 27 * rights to redistribute these changes. 28 */ 29 30 #include "opt_tcverbose.h" 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/device.h> 35 36 #include <dev/tc/tcreg.h> 37 #include <dev/tc/tcvar.h> 38 #include <dev/tc/tcdevs.h> 39 40 41 /* Definition of the driver for autoconfig. */ 42 int tcmatch __P((struct device *, struct cfdata *, void *)); 43 void tcattach __P((struct device *, struct device *, void *)); 44 45 struct cfattach tc_ca = { 46 sizeof(struct tc_softc), tcmatch, tcattach 47 }; 48 extern struct cfdriver tc_cd; 49 50 int tcprint __P((void *, const char *)); 51 int tcsubmatch __P((struct device *, struct cfdata *, void *)); 52 int tc_checkslot __P((tc_addr_t, char *)); 53 void tc_devinfo __P((const char *, char *)); 54 55 int 56 tcmatch(parent, cf, aux) 57 struct device *parent; 58 struct cfdata *cf; 59 void *aux; 60 { 61 struct tcbus_attach_args *tba = aux; 62 63 if (strcmp(tba->tba_busname, cf->cf_driver->cd_name)) 64 return (0); 65 66 return (1); 67 } 68 69 void 70 tcattach(parent, self, aux) 71 struct device *parent; 72 struct device *self; 73 void *aux; 74 { 75 struct tc_softc *sc = (struct tc_softc *)self; 76 struct tcbus_attach_args *tba = aux; 77 struct tc_attach_args ta; 78 const struct tc_builtin *builtin; 79 struct tc_slotdesc *slot; 80 tc_addr_t tcaddr; 81 int i; 82 83 printf(": %s MHz clock\n", 84 tba->tba_speed == TC_SPEED_25_MHZ ? "25" : "12.5"); 85 86 /* 87 * Save important CPU/chipset information. 88 */ 89 sc->sc_speed = tba->tba_speed; 90 sc->sc_nslots = tba->tba_nslots; 91 sc->sc_slots = tba->tba_slots; 92 sc->sc_intr_evcnt = tba->tba_intr_evcnt; 93 sc->sc_intr_establish = tba->tba_intr_establish; 94 sc->sc_intr_disestablish = tba->tba_intr_disestablish; 95 sc->sc_get_dma_tag = tba->tba_get_dma_tag; 96 97 /* 98 * Try to configure each built-in device 99 */ 100 for (i = 0; i < tba->tba_nbuiltins; i++) { 101 builtin = &tba->tba_builtins[i]; 102 103 /* sanity check! */ 104 if (builtin->tcb_slot > sc->sc_nslots) 105 panic("tcattach: builtin %d slot > nslots", i); 106 107 /* 108 * Make sure device is really there, because some 109 * built-in devices are really optional. 110 */ 111 tcaddr = sc->sc_slots[builtin->tcb_slot].tcs_addr + 112 builtin->tcb_offset; 113 if (tc_badaddr(tcaddr)) 114 continue; 115 116 /* 117 * Set up the device attachment information. 118 */ 119 strncpy(ta.ta_modname, builtin->tcb_modname, TC_ROM_LLEN); 120 ta.ta_memt = tba->tba_memt; 121 ta.ta_dmat = (*sc->sc_get_dma_tag)(builtin->tcb_slot); 122 ta.ta_modname[TC_ROM_LLEN] = '\0'; 123 ta.ta_slot = builtin->tcb_slot; 124 ta.ta_offset = builtin->tcb_offset; 125 ta.ta_addr = tcaddr; 126 ta.ta_cookie = builtin->tcb_cookie; 127 ta.ta_busspeed = sc->sc_speed; 128 129 /* 130 * Mark the slot as used, so we don't check it later. 131 */ 132 sc->sc_slots[builtin->tcb_slot].tcs_used = 1; 133 134 /* 135 * Attach the device. 136 */ 137 config_found_sm(self, &ta, tcprint, tcsubmatch); 138 } 139 140 /* 141 * Try to configure each unused slot, last to first. 142 */ 143 for (i = sc->sc_nslots - 1; i >= 0; i--) { 144 slot = &sc->sc_slots[i]; 145 146 /* If already checked above, don't look again now. */ 147 if (slot->tcs_used) 148 continue; 149 150 /* 151 * Make sure something is there, and find out what it is. 152 */ 153 tcaddr = slot->tcs_addr; 154 if (tc_badaddr(tcaddr)) 155 continue; 156 if (tc_checkslot(tcaddr, ta.ta_modname) == 0) 157 continue; 158 159 /* 160 * Set up the rest of the attachment information. 161 */ 162 ta.ta_memt = tba->tba_memt; 163 ta.ta_dmat = (*sc->sc_get_dma_tag)(i); 164 ta.ta_slot = i; 165 ta.ta_offset = 0; 166 ta.ta_addr = tcaddr; 167 ta.ta_cookie = slot->tcs_cookie; 168 169 /* 170 * Mark the slot as used. 171 */ 172 slot->tcs_used = 1; 173 174 /* 175 * Attach the device. 176 */ 177 config_found_sm(self, &ta, tcprint, tcsubmatch); 178 } 179 } 180 181 int 182 tcprint(aux, pnp) 183 void *aux; 184 const char *pnp; 185 { 186 struct tc_attach_args *ta = aux; 187 char devinfo[256]; 188 189 if (pnp) { 190 tc_devinfo(ta->ta_modname, devinfo); 191 printf("%s at %s", devinfo, pnp); 192 } 193 printf(" slot %d offset 0x%lx", ta->ta_slot, 194 (long)ta->ta_offset); 195 return (UNCONF); 196 } 197 198 int 199 tcsubmatch(parent, cf, aux) 200 struct device *parent; 201 struct cfdata *cf; 202 void *aux; 203 { 204 struct tc_attach_args *d = aux; 205 206 if ((cf->tccf_slot != TCCF_SLOT_UNKNOWN) && 207 (cf->tccf_slot != d->ta_slot)) 208 return 0; 209 if ((cf->tccf_offset != TCCF_SLOT_UNKNOWN) && 210 (cf->tccf_offset != d->ta_offset)) 211 return 0; 212 213 return ((*cf->cf_attach->ca_match)(parent, cf, aux)); 214 } 215 216 217 #define NTC_ROMOFFS 2 218 static tc_offset_t tc_slot_romoffs[NTC_ROMOFFS] = { 219 TC_SLOT_ROM, 220 TC_SLOT_PROTOROM, 221 }; 222 223 int 224 tc_checkslot(slotbase, namep) 225 tc_addr_t slotbase; 226 char *namep; 227 { 228 struct tc_rommap *romp; 229 int i, j; 230 231 for (i = 0; i < NTC_ROMOFFS; i++) { 232 romp = (struct tc_rommap *) 233 (slotbase + tc_slot_romoffs[i]); 234 235 switch (romp->tcr_width.v) { 236 case 1: 237 case 2: 238 case 4: 239 break; 240 241 default: 242 continue; 243 } 244 245 if (romp->tcr_stride.v != 4) 246 continue; 247 248 for (j = 0; j < 4; j++) 249 if (romp->tcr_test[j+0*romp->tcr_stride.v] != 0x55 || 250 romp->tcr_test[j+1*romp->tcr_stride.v] != 0x00 || 251 romp->tcr_test[j+2*romp->tcr_stride.v] != 0xaa || 252 romp->tcr_test[j+3*romp->tcr_stride.v] != 0xff) 253 continue; 254 255 for (j = 0; j < TC_ROM_LLEN; j++) 256 namep[j] = romp->tcr_modname[j].v; 257 namep[j] = '\0'; 258 return (1); 259 } 260 return (0); 261 } 262 263 const struct evcnt * 264 tc_intr_evcnt(struct device *dev, void *cookie) 265 { 266 struct tc_softc *sc = tc_cd.cd_devs[0]; 267 268 return ((*sc->sc_intr_evcnt)(dev, cookie)); 269 } 270 271 void 272 tc_intr_establish(dev, cookie, level, handler, arg) 273 struct device *dev; 274 void *cookie, *arg; 275 int level; 276 int (*handler) __P((void *)); 277 { 278 struct tc_softc *sc = tc_cd.cd_devs[0]; 279 280 (*sc->sc_intr_establish)(dev, cookie, level, handler, arg); 281 } 282 283 void 284 tc_intr_disestablish(dev, cookie) 285 struct device *dev; 286 void *cookie; 287 { 288 struct tc_softc *sc = tc_cd.cd_devs[0]; 289 290 (*sc->sc_intr_disestablish)(dev, cookie); 291 } 292 293 #ifdef TCVERBOSE 294 /* 295 * Descriptions of of known devices. 296 */ 297 struct tc_knowndev { 298 const char *id, *driver, *description; 299 }; 300 301 #include <dev/tc/tcdevs_data.h> 302 #endif /* TCVERBOSE */ 303 304 void 305 tc_devinfo(id, cp) 306 const char *id; 307 char *cp; 308 { 309 const char *driver, *description; 310 #ifdef TCVERBOSE 311 struct tc_knowndev *tdp; 312 int match; 313 const char *unmatched = "unknown "; 314 #else 315 const char *unmatched = ""; 316 #endif 317 318 driver = NULL; 319 description = id; 320 321 #ifdef TCVERBOSE 322 /* find the device in the table, if possible. */ 323 tdp = tc_knowndevs; 324 while (tdp->id != NULL) { 325 /* check this entry for a match */ 326 match = !strcmp(tdp->id, id); 327 if (match) { 328 driver = tdp->driver; 329 description = tdp->description; 330 break; 331 } 332 tdp++; 333 } 334 #endif 335 336 if (driver == NULL) 337 cp += sprintf(cp, "%sdevice %s", unmatched, id); 338 else 339 cp += sprintf(cp, "%s (%s)", driver, description); 340 } 341