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