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