Home | History | Annotate | Line # | Download | only in tc
tc.c revision 1.22
      1 /*	$NetBSD: tc.c,v 1.22 1997/07/22 03:44:32 jonathan 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 int	tcmatch __P((struct device *, struct cfdata *, void *));
     52 void	tcattach __P((struct device *, struct device *, void *));
     53 
     54 struct cfattach tc_ca = {
     55 	sizeof(struct tc_softc), tcmatch, tcattach
     56 };
     57 
     58 struct cfdriver tc_cd = {
     59 	NULL, "tc", DV_DULL
     60 };
     61 
     62 int	tcprint __P((void *, const char *));
     63 int	tcsubmatch __P((struct device *, struct cfdata *, void *));
     64 int	tc_checkslot __P((tc_addr_t, char *));
     65 void	tc_devinfo __P((const char *, char *));
     66 
     67 int
     68 tcmatch(parent, cf, aux)
     69 	struct device *parent;
     70 	struct cfdata *cf;
     71 	void *aux;
     72 {
     73 	struct tcbus_attach_args *tba = aux;
     74 
     75 	if (strcmp(tba->tba_busname, cf->cf_driver->cd_name))
     76 		return (0);
     77 
     78 	return (1);
     79 }
     80 
     81 void
     82 tcattach(parent, self, aux)
     83 	struct device *parent;
     84 	struct device *self;
     85 	void *aux;
     86 {
     87 	struct tc_softc *sc = (struct tc_softc *)self;
     88 	struct tcbus_attach_args *tba = aux;
     89 	struct tc_attach_args ta;
     90 	const struct tc_builtin *builtin;
     91 	struct tc_slotdesc *slot;
     92 	tc_addr_t tcaddr;
     93 	int i;
     94 
     95 	printf(": %s MHz clock\n",
     96 	    tba->tba_speed == TC_SPEED_25_MHZ ? "25" : "12.5");
     97 
     98 	/*
     99 	 * Save important CPU/chipset information.
    100 	 */
    101 	sc->sc_speed = tba->tba_speed;
    102 	sc->sc_nslots = tba->tba_nslots;
    103 	sc->sc_slots = tba->tba_slots;
    104 	sc->sc_intr_establish = tba->tba_intr_establish;
    105 	sc->sc_intr_disestablish = tba->tba_intr_disestablish;
    106 
    107 	/*
    108 	 * Try to configure each built-in device
    109 	 */
    110 	for (i = 0; i < tba->tba_nbuiltins; i++) {
    111 		builtin = &tba->tba_builtins[i];
    112 
    113 		/* sanity check! */
    114 		if (builtin->tcb_slot > sc->sc_nslots)
    115 			panic("tcattach: builtin %d slot > nslots", i);
    116 
    117 		/*
    118 		 * Make sure device is really there, because some
    119 		 * built-in devices are really optional.
    120 		 */
    121 		tcaddr = sc->sc_slots[builtin->tcb_slot].tcs_addr +
    122 		    builtin->tcb_offset;
    123 		if (tc_badaddr(tcaddr))
    124 			continue;
    125 
    126 		/*
    127 		 * Set up the device attachment information.
    128 		 */
    129 		strncpy(ta.ta_modname, builtin->tcb_modname, TC_ROM_LLEN);
    130 #ifdef __alpha__ /* XXX */
    131 		ta.ta_memt = tba->tba_memt;
    132 #endif
    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 	const 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, cf, aux)
    209 	struct device *parent;
    210 	struct cfdata *cf;
    211 	void *aux;
    212 {
    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_attach->ca_match)(parent, cf, 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