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. */ 48static int tcmatch(device_t, cfdata_t, void *); 49 50CFATTACH_DECL_NEW(tc, sizeof(struct tc_softc), 51 tcmatch, tcattach, NULL, NULL); 52 53extern struct cfdriver tc_cd; 54 55static int tcprint(void *, const char *); 56static void tc_devinfo(const char *, char *, size_t); 57 58static int 59tcmatch(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 69void 70tcattach(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 sc->sc_dev = self; 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 locs[TCCF_SLOT] = builtin->tcb_slot; 135 locs[TCCF_OFFSET] = builtin->tcb_offset; 136 /* 137 * Attach the device. 138 */ 139 config_found_sm_loc(self, "tc", locs, &ta, 140 tcprint, config_stdsubmatch); 141 } 142 143 /* 144 * Try to configure each unused slot, last to first. 145 */ 146 for (i = sc->sc_nslots - 1; i >= 0; i--) { 147 slot = &sc->sc_slots[i]; 148 149 /* If already checked above, don't look again now. */ 150 if (slot->tcs_used) 151 continue; 152 153 /* 154 * Make sure something is there, and find out what it is. 155 */ 156 tcaddr = slot->tcs_addr; 157 if (tc_badaddr(tcaddr)) 158 continue; 159 if (tc_checkslot(tcaddr, ta.ta_modname) == 0) 160 continue; 161 162 /* 163 * Set up the rest of the attachment information. 164 */ 165 ta.ta_memt = tba->tba_memt; 166 ta.ta_dmat = (*sc->sc_get_dma_tag)(i); 167 ta.ta_slot = i; 168 ta.ta_offset = 0; 169 ta.ta_addr = tcaddr; 170 ta.ta_cookie = slot->tcs_cookie; 171 172 /* 173 * Mark the slot as used. 174 */ 175 slot->tcs_used = 1; 176 177 locs[TCCF_SLOT] = i; 178 locs[TCCF_OFFSET] = 0; 179 /* 180 * Attach the device. 181 */ 182 config_found_sm_loc(self, "tc", locs, &ta, 183 tcprint, config_stdsubmatch); 184 } 185} 186 187static int 188tcprint(void *aux, const char *pnp) 189{ 190 struct tc_attach_args *ta = aux; 191 char devinfo[256]; 192 193 if (pnp) { 194 tc_devinfo(ta->ta_modname, devinfo, sizeof(devinfo)); 195 aprint_normal("%s at %s", devinfo, pnp); 196 } 197 aprint_normal(" slot %d offset 0x%x", ta->ta_slot, ta->ta_offset); 198 return (UNCONF); 199} 200 201 202static const tc_offset_t tc_slot_romoffs[] = { 203 TC_SLOT_ROM, 204#ifndef __vax__ 205 TC_SLOT_PROTOROM, 206#endif 207}; 208 209int 210tc_checkslot(tc_addr_t slotbase, char *namep) 211{ 212 struct tc_rommap *romp; 213 int i, j; 214 215 for (i = 0; i < __arraycount(tc_slot_romoffs); i++) { 216 romp = (struct tc_rommap *) 217 (slotbase + tc_slot_romoffs[i]); 218 219 switch (romp->tcr_width.v) { 220 case 1: 221 case 2: 222 case 4: 223 break; 224 225 default: 226 continue; 227 } 228 229 if (romp->tcr_stride.v != 4) 230 continue; 231 232 for (j = 0; j < 4; j++) 233 if (romp->tcr_test[j+0*romp->tcr_stride.v] != 0x55 || 234 romp->tcr_test[j+1*romp->tcr_stride.v] != 0x00 || 235 romp->tcr_test[j+2*romp->tcr_stride.v] != 0xaa || 236 romp->tcr_test[j+3*romp->tcr_stride.v] != 0xff) 237 continue; 238 239 for (j = 0; j < TC_ROM_LLEN; j++) 240 namep[j] = romp->tcr_modname[j].v; 241 namep[j] = '\0'; 242 return (1); 243 } 244 return (0); 245} 246 247const struct evcnt * 248tc_intr_evcnt(device_t dev, void *cookie) 249{ 250 struct tc_softc *sc = device_lookup_private(&tc_cd, 0); 251 252 return ((*sc->sc_intr_evcnt)(dev, cookie)); 253} 254 255void 256tc_intr_establish(device_t dev, void *cookie, int level, 257 int (*handler)(void *), void *arg) 258{ 259 struct tc_softc *sc = device_lookup_private(&tc_cd, 0); 260 261 (*sc->sc_intr_establish)(dev, cookie, level, handler, arg); 262} 263 264void 265tc_intr_disestablish(device_t dev, void *cookie) 266{ 267 struct tc_softc *sc = device_lookup_private(&tc_cd, 0); 268 269 (*sc->sc_intr_disestablish)(dev, cookie); 270} 271 272#ifdef TCVERBOSE 273/* 274 * Descriptions of of known devices. 275 */ 276struct tc_knowndev { 277 const char *id, *driver, *description; 278}; 279 280#include <dev/tc/tcdevs_data.h> 281#endif /* TCVERBOSE */ 282 283static void 284tc_devinfo(const char *id, char *cp, size_t l) 285{ 286 const char *driver, *description; 287#ifdef TCVERBOSE 288 const struct tc_knowndev *tdp; 289 int match; 290 const char *unmatched = "unknown "; 291#else 292 const char *unmatched = ""; 293#endif 294 295 driver = NULL; 296 description = id; 297 298#ifdef TCVERBOSE 299 /* find the device in the table, if possible. */ 300 tdp = tc_knowndevs; 301 while (tdp->id != NULL) { 302 /* check this entry for a match */ 303 match = !strcmp(tdp->id, id); 304 if (match) { 305 driver = tdp->driver; 306 description = tdp->description; 307 break; 308 } 309 tdp++; 310 } 311#endif 312 313 if (driver == NULL) 314 snprintf(cp, l, "%sdevice %s", unmatched, id); 315 else 316 snprintf(cp, l, "%s (%s)", driver, description); 317} 318