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