1 /* $NetBSD: tc.c,v 1.50 2009/05/12 14:47:04 cegger 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 <sys/cdefs.h> 31 __KERNEL_RCSID(0, "$NetBSD: tc.c,v 1.50 2009/05/12 14:47:04 cegger Exp $"); 32 33 #include "opt_tcverbose.h" 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/device.h> 38 39 #include <machine/cpu.h> /* for badaddr */ 40 41 #include <dev/tc/tcreg.h> 42 #include <dev/tc/tcvar.h> 43 #include <dev/tc/tcdevs.h> 44 45 #include "locators.h" 46 47 /* Definition of the driver for autoconfig. */ 48 static int tcmatch(device_t, cfdata_t, void *); 49 50 CFATTACH_DECL(tc, sizeof(struct tc_softc), 51 tcmatch, tcattach, NULL, NULL); 52 53 extern struct cfdriver tc_cd; 54 55 static int tcprint(void *, const char *); 56 static void tc_devinfo(const char *, char *, size_t); 57 58 static int 59 tcmatch(device_t parent, cfdata_t cf, void *aux) 60 { 61 struct tcbus_attach_args *tba = aux; 62 63 if (strcmp(tba->tba_busname, cf->cf_name)) 64 return (0); 65 66 return (1); 67 } 68 69 void 70 tcattach(device_t parent, device_t self, void *aux) 71 { 72 struct tc_softc *sc = device_private(self); 73 struct tcbus_attach_args *tba = aux; 74 struct tc_attach_args ta; 75 const struct tc_builtin *builtin; 76 struct tc_slotdesc *slot; 77 tc_addr_t tcaddr; 78 int i; 79 int locs[TCCF_NLOCS]; 80 81 printf(": %s MHz clock\n", 82 tba->tba_speed == TC_SPEED_25_MHZ ? "25" : "12.5"); 83 84 /* 85 * Save important CPU/chipset information. 86 */ 87 sc->sc_speed = tba->tba_speed; 88 sc->sc_nslots = tba->tba_nslots; 89 sc->sc_slots = tba->tba_slots; 90 sc->sc_intr_evcnt = tba->tba_intr_evcnt; 91 sc->sc_intr_establish = tba->tba_intr_establish; 92 sc->sc_intr_disestablish = tba->tba_intr_disestablish; 93 sc->sc_get_dma_tag = tba->tba_get_dma_tag; 94 95 /* 96 * Try to configure each built-in device 97 */ 98 for (i = 0; i < tba->tba_nbuiltins; i++) { 99 builtin = &tba->tba_builtins[i]; 100 101 /* sanity check! */ 102 if (builtin->tcb_slot > sc->sc_nslots) 103 panic("tcattach: builtin %d slot > nslots", i); 104 105 /* 106 * Make sure device is really there, because some 107 * built-in devices are really optional. 108 */ 109 tcaddr = sc->sc_slots[builtin->tcb_slot].tcs_addr + 110 builtin->tcb_offset; 111 if (tc_badaddr(tcaddr)) 112 continue; 113 114 /* 115 * Set up the device attachment information. 116 */ 117 strncpy(ta.ta_modname, builtin->tcb_modname, TC_ROM_LLEN); 118 ta.ta_memt = tba->tba_memt; 119 ta.ta_dmat = (*sc->sc_get_dma_tag)(builtin->tcb_slot); 120 ta.ta_modname[TC_ROM_LLEN] = '\0'; 121 ta.ta_slot = builtin->tcb_slot; 122 ta.ta_offset = builtin->tcb_offset; 123 ta.ta_addr = tcaddr; 124 ta.ta_cookie = builtin->tcb_cookie; 125 ta.ta_busspeed = sc->sc_speed; 126 127 /* 128 * Mark the slot as used, so we don't check it later. 129 */ 130 sc->sc_slots[builtin->tcb_slot].tcs_used = 1; 131 132 locs[TCCF_SLOT] = builtin->tcb_slot; 133 locs[TCCF_OFFSET] = builtin->tcb_offset; 134 /* 135 * Attach the device. 136 */ 137 config_found_sm_loc(self, "tc", locs, &ta, 138 tcprint, config_stdsubmatch); 139 } 140 141 /* 142 * Try to configure each unused slot, last to first. 143 */ 144 for (i = sc->sc_nslots - 1; i >= 0; i--) { 145 slot = &sc->sc_slots[i]; 146 147 /* If already checked above, don't look again now. */ 148 if (slot->tcs_used) 149 continue; 150 151 /* 152 * Make sure something is there, and find out what it is. 153 */ 154 tcaddr = slot->tcs_addr; 155 if (tc_badaddr(tcaddr)) 156 continue; 157 if (tc_checkslot(tcaddr, ta.ta_modname) == 0) 158 continue; 159 160 /* 161 * Set up the rest of the attachment information. 162 */ 163 ta.ta_memt = tba->tba_memt; 164 ta.ta_dmat = (*sc->sc_get_dma_tag)(i); 165 ta.ta_slot = i; 166 ta.ta_offset = 0; 167 ta.ta_addr = tcaddr; 168 ta.ta_cookie = slot->tcs_cookie; 169 170 /* 171 * Mark the slot as used. 172 */ 173 slot->tcs_used = 1; 174 175 locs[TCCF_SLOT] = i; 176 locs[TCCF_OFFSET] = 0; 177 /* 178 * Attach the device. 179 */ 180 config_found_sm_loc(self, "tc", locs, &ta, 181 tcprint, config_stdsubmatch); 182 } 183 } 184 185 static int 186 tcprint(void *aux, const char *pnp) 187 { 188 struct tc_attach_args *ta = aux; 189 char devinfo[256]; 190 191 if (pnp) { 192 tc_devinfo(ta->ta_modname, devinfo, sizeof(devinfo)); 193 aprint_normal("%s at %s", devinfo, pnp); 194 } 195 aprint_normal(" slot %d offset 0x%x", ta->ta_slot, ta->ta_offset); 196 return (UNCONF); 197 } 198 199 200 static const tc_offset_t tc_slot_romoffs[] = { 201 TC_SLOT_ROM, 202 #ifndef __vax__ 203 TC_SLOT_PROTOROM, 204 #endif 205 }; 206 207 int 208 tc_checkslot(tc_addr_t slotbase, char *namep) 209 { 210 struct tc_rommap *romp; 211 int i, j; 212 213 for (i = 0; i < __arraycount(tc_slot_romoffs); i++) { 214 romp = (struct tc_rommap *) 215 (slotbase + tc_slot_romoffs[i]); 216 217 switch (romp->tcr_width.v) { 218 case 1: 219 case 2: 220 case 4: 221 break; 222 223 default: 224 continue; 225 } 226 227 if (romp->tcr_stride.v != 4) 228 continue; 229 230 for (j = 0; j < 4; j++) 231 if (romp->tcr_test[j+0*romp->tcr_stride.v] != 0x55 || 232 romp->tcr_test[j+1*romp->tcr_stride.v] != 0x00 || 233 romp->tcr_test[j+2*romp->tcr_stride.v] != 0xaa || 234 romp->tcr_test[j+3*romp->tcr_stride.v] != 0xff) 235 continue; 236 237 for (j = 0; j < TC_ROM_LLEN; j++) 238 namep[j] = romp->tcr_modname[j].v; 239 namep[j] = '\0'; 240 return (1); 241 } 242 return (0); 243 } 244 245 const struct evcnt * 246 tc_intr_evcnt(device_t dev, void *cookie) 247 { 248 struct tc_softc *sc = device_lookup_private(&tc_cd, 0); 249 250 return ((*sc->sc_intr_evcnt)(dev, cookie)); 251 } 252 253 void 254 tc_intr_establish(device_t dev, void *cookie, int level, 255 int (*handler)(void *), void *arg) 256 { 257 struct tc_softc *sc = device_lookup_private(&tc_cd, 0); 258 259 (*sc->sc_intr_establish)(dev, cookie, level, handler, arg); 260 } 261 262 void 263 tc_intr_disestablish(device_t dev, void *cookie) 264 { 265 struct tc_softc *sc = device_lookup_private(&tc_cd, 0); 266 267 (*sc->sc_intr_disestablish)(dev, cookie); 268 } 269 270 #ifdef TCVERBOSE 271 /* 272 * Descriptions of of known devices. 273 */ 274 struct tc_knowndev { 275 const char *id, *driver, *description; 276 }; 277 278 #include <dev/tc/tcdevs_data.h> 279 #endif /* TCVERBOSE */ 280 281 static void 282 tc_devinfo(const char *id, char *cp, size_t l) 283 { 284 const char *driver, *description; 285 #ifdef TCVERBOSE 286 const struct tc_knowndev *tdp; 287 int match; 288 const char *unmatched = "unknown "; 289 #else 290 const char *unmatched = ""; 291 #endif 292 293 driver = NULL; 294 description = id; 295 296 #ifdef TCVERBOSE 297 /* find the device in the table, if possible. */ 298 tdp = tc_knowndevs; 299 while (tdp->id != NULL) { 300 /* check this entry for a match */ 301 match = !strcmp(tdp->id, id); 302 if (match) { 303 driver = tdp->driver; 304 description = tdp->description; 305 break; 306 } 307 tdp++; 308 } 309 #endif 310 311 if (driver == NULL) 312 snprintf(cp, l, "%sdevice %s", unmatched, id); 313 else 314 snprintf(cp, l, "%s (%s)", driver, description); 315 } 316