Home | History | Annotate | Line # | Download | only in tc
tc.c revision 1.7
      1 /*	$NetBSD: tc.c,v 1.7 1996/02/27 21:48:52 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_sm(self, &ta, tcprint, tcsubmatch);
    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 		 * Attach the device.
    177 		 */
    178 		config_found_sm(self, &ta, tcprint, tcsubmatch);
    179 	}
    180 }
    181 
    182 int
    183 tcprint(aux, pnp)
    184 	void *aux;
    185 	char *pnp;
    186 {
    187 	struct tc_attach_args *ta = aux;
    188 
    189         if (pnp)
    190                 printf("%s at %s", ta->ta_modname, pnp);	/* XXX */
    191         printf(" slot %d offset 0x%lx", ta->ta_slot,
    192 	    (long)ta->ta_offset);
    193         return (UNCONF);
    194 }
    195 
    196 int
    197 tcsubmatch(parent, match, aux)
    198         struct device *parent;
    199         void *match, *aux;
    200 {
    201 	struct cfdata *cf = match;
    202 	struct tc_attach_args *d = aux;
    203 
    204 	if ((cf->tccf_slot != TCCF_SLOT_UNKNOWN) &&
    205 	    (cf->tccf_slot != d->ta_slot))
    206 		return 0;
    207 	if ((cf->tccf_offset != TCCF_SLOT_UNKNOWN) &&
    208 	    (cf->tccf_offset != d->ta_offset))
    209 		return 0;
    210 
    211 	return ((*cf->cf_driver->cd_match)(parent, match, aux));
    212 }
    213 
    214 
    215 #define	NTC_ROMOFFS	2
    216 static tc_offset_t tc_slot_romoffs[NTC_ROMOFFS] = {
    217 	TC_SLOT_ROM,
    218 	TC_SLOT_PROTOROM,
    219 };
    220 
    221 int
    222 tc_checkslot(slotbase, namep)
    223 	tc_addr_t slotbase;
    224 	char *namep;
    225 {
    226 	struct tc_rommap *romp;
    227 	int i, j;
    228 
    229 	for (i = 0; i < NTC_ROMOFFS; i++) {
    230 		romp = (struct tc_rommap *)
    231 		    (slotbase + tc_slot_romoffs[i]);
    232 
    233 		switch (romp->tcr_width.v) {
    234 		case 1:
    235 		case 2:
    236 		case 4:
    237 			break;
    238 
    239 		default:
    240 			continue;
    241 		}
    242 
    243 		if (romp->tcr_stride.v != 4)
    244 			continue;
    245 
    246 		for (j = 0; j < 4; j++)
    247 			if (romp->tcr_test[j+0*romp->tcr_stride.v] != 0x55 ||
    248 			    romp->tcr_test[j+1*romp->tcr_stride.v] != 0x00 ||
    249 			    romp->tcr_test[j+2*romp->tcr_stride.v] != 0xaa ||
    250 			    romp->tcr_test[j+3*romp->tcr_stride.v] != 0xff)
    251 				continue;
    252 
    253 		for (j = 0; j < TC_ROM_LLEN; j++)
    254 			namep[j] = romp->tcr_modname[j].v;
    255 		namep[j] = '\0';
    256 		return (1);
    257 	}
    258 	return (0);
    259 }
    260 
    261 void
    262 tc_intr_establish(dev, cookie, level, handler, arg)
    263 	struct device *dev;
    264 	void *cookie, *arg;
    265 	tc_intrlevel_t level;
    266 	int (*handler) __P((void *));
    267 {
    268 	struct tc_softc *sc = (struct tc_softc *)dev;
    269 
    270 	(*sc->sc_intr_establish)(sc->sc_dv.dv_parent, cookie, level,
    271 	    handler, arg);
    272 }
    273 
    274 void
    275 tc_intr_disestablish(dev, cookie)
    276 	struct device *dev;
    277 	void *cookie;
    278 {
    279 	struct tc_softc *sc = (struct tc_softc *)dev;
    280 
    281 	(*sc->sc_intr_disestablish)(sc->sc_dv.dv_parent, cookie);
    282 }
    283