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