Home | History | Annotate | Line # | Download | only in tc
tc.c revision 1.3
      1 /*	$NetBSD: tc.c,v 1.3 1996/02/27 01:37:32 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 		 * Attach the device.
    139 		 */
    140 		config_found(self, &ta, tcprint);
    141 	}
    142 
    143 	/*
    144 	 * Try to configure each unused slot, last to first.
    145 	 */
    146 	for (i = sc->sc_nslots - 1; i >= 0; i--) {
    147 		slot = &sc->sc_slots[i];
    148 
    149 		/* If already checked above, don't look again now. */
    150 		if (slot->tcs_used)
    151 			continue;
    152 
    153 		/*
    154 		 * Make sure something is there, and find out what it is.
    155 		 */
    156 		tcaddr = slot->tcs_addr;
    157 		if (tc_badaddr(tcaddr))
    158 			continue;
    159 		if (tc_checkslot(tcaddr, ta.ta_modname) == 0)
    160 			continue;
    161 
    162 		/*
    163 		 * Set up the rest of the attachment information.
    164 		 */
    165 		ta.ta_slot = i;
    166 		ta.ta_offset = 0;
    167 		ta.ta_addr = tcaddr;
    168 		ta.ta_cookie = slot->tcs_cookie;
    169 
    170 		/*
    171 		 * Mark the slot as used.
    172 		 */
    173 		slot->tcs_used = 1;
    174 
    175 		/*
    176 		 * Find and attach the device.
    177 		 * XXX a close relative of config_found()
    178 		 */
    179 		if ((match = config_search(tcsubmatch, parent, aux)) != NULL)
    180 	                config_attach(parent, match, aux, tcprint);
    181 		else {
    182 			tcprint(aux, parent->dv_xname);
    183 			printf(" not configured\n");
    184 		}
    185 	}
    186 }
    187 
    188 int
    189 tcprint(aux, pnp)
    190 	void *aux;
    191 	char *pnp;
    192 {
    193 	struct tc_attach_args *ta = aux;
    194 
    195         if (pnp)
    196                 printf("%s at %s", ta->ta_modname, pnp);	/* XXX */
    197         printf(" slot %d offset 0x%lx", ta->ta_slot,
    198 	    (long)ta->ta_offset);
    199         return (UNCONF);
    200 }
    201 
    202 int
    203 tcsubmatch(parent, match, aux)
    204         struct device *parent;
    205         void *match, *aux;
    206 {
    207 	struct cfdata *cf = match;
    208 	struct tc_attach_args *d = aux;
    209 
    210 	if ((cf->tccf_slot != TCCF_SLOT_UNKNOWN) &&
    211 	    (cf->tccf_slot != d->ta_slot))
    212 		return 0;
    213 	if ((cf->tccf_offset != TCCF_SLOT_UNKNOWN) &&
    214 	    (cf->tccf_offset != d->ta_offset))
    215 		return 0;
    216 
    217 	return ((*cf->cf_driver->cd_match)(parent, match, aux));
    218 }
    219 
    220 
    221 #define	NTC_ROMOFFS	2
    222 static tc_offset_t tc_slot_romoffs[NTC_ROMOFFS] = {
    223 	TC_SLOT_ROM,
    224 	TC_SLOT_PROTOROM,
    225 };
    226 
    227 int
    228 tc_checkslot(slotbase, namep)
    229 	tc_addr_t slotbase;
    230 	char *namep;
    231 {
    232 	struct tc_rommap *romp;
    233 	int i, j;
    234 
    235 	for (i = 0; i < NTC_ROMOFFS; i++) {
    236 		romp = (struct tc_rommap *)
    237 		    (slotbase + tc_slot_romoffs[i]);
    238 
    239 		switch (romp->tcr_width.v) {
    240 		case 1:
    241 		case 2:
    242 		case 4:
    243 			break;
    244 
    245 		default:
    246 			continue;
    247 		}
    248 
    249 		if (romp->tcr_stride.v != 4)
    250 			continue;
    251 
    252 		for (j = 0; j < 4; j++)
    253 			if (romp->tcr_test[j+0*romp->tcr_stride.v] != 0x55 ||
    254 			    romp->tcr_test[j+1*romp->tcr_stride.v] != 0x00 ||
    255 			    romp->tcr_test[j+2*romp->tcr_stride.v] != 0xaa ||
    256 			    romp->tcr_test[j+3*romp->tcr_stride.v] != 0xff)
    257 				continue;
    258 
    259 		for (j = 0; j < TC_ROM_LLEN; j++)
    260 			namep[j] = romp->tcr_modname[j].v;
    261 		namep[j] = '\0';
    262 		return (1);
    263 	}
    264 	return (0);
    265 }
    266 
    267 void
    268 tc_intr_establish(dev, cookie, level, handler, arg)
    269 	struct device *dev;
    270 	void *cookie, *arg;
    271 	tc_intrlevel_t level;
    272 	int (*handler) __P((void *));
    273 {
    274 	struct tc_softc *sc = (struct tc_softc *)dev;
    275 
    276 	(*sc->sc_intr_establish)(sc->sc_dv.dv_parent, cookie, level,
    277 	    handler, arg);
    278 }
    279 
    280 void
    281 tc_intr_disestablish(dev, cookie)
    282 	struct device *dev;
    283 	void *cookie;
    284 {
    285 	struct tc_softc *sc = (struct tc_softc *)dev;
    286 
    287 	(*sc->sc_intr_disestablish)(sc->sc_dv.dv_parent, cookie);
    288 }
    289