Home | History | Annotate | Line # | Download | only in tc
tc.c revision 1.16
      1  1.16       cgd /*	$NetBSD: tc.c,v 1.16 1996/05/17 23:39:19 cgd Exp $	*/
      2   1.1       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright (c) 1994, 1995 Carnegie-Mellon University.
      5   1.1       cgd  * All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Author: Chris G. Demetriou
      8  1.14       cgd  *
      9   1.1       cgd  * Permission to use, copy, modify and distribute this software and
     10   1.1       cgd  * its documentation is hereby granted, provided that both the copyright
     11   1.1       cgd  * notice and this permission notice appear in all copies of the
     12   1.1       cgd  * software, derivative works or modified versions, and any portions
     13   1.1       cgd  * thereof, and that both notices appear in supporting documentation.
     14  1.14       cgd  *
     15  1.14       cgd  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  1.14       cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17   1.1       cgd  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  1.14       cgd  *
     19   1.1       cgd  * Carnegie Mellon requests users of this software to return to
     20   1.1       cgd  *
     21   1.1       cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22   1.1       cgd  *  School of Computer Science
     23   1.1       cgd  *  Carnegie Mellon University
     24   1.1       cgd  *  Pittsburgh PA 15213-3890
     25   1.1       cgd  *
     26   1.1       cgd  * any improvements or extensions that they make and grant Carnegie the
     27   1.1       cgd  * rights to redistribute these changes.
     28   1.1       cgd  */
     29   1.1       cgd 
     30   1.1       cgd #include <sys/param.h>
     31  1.13  jonathan #include <sys/systm.h>
     32   1.1       cgd #include <sys/device.h>
     33   1.1       cgd 
     34   1.1       cgd #include <dev/tc/tcreg.h>
     35   1.1       cgd #include <dev/tc/tcvar.h>
     36   1.8       cgd #include <dev/tc/tcdevs.h>
     37   1.1       cgd 
     38   1.1       cgd struct tc_softc {
     39   1.1       cgd 	struct	device sc_dv;
     40   1.1       cgd 
     41   1.2       cgd 	int	sc_speed;
     42   1.1       cgd 	int	sc_nslots;
     43   1.1       cgd 	struct tc_slotdesc *sc_slots;
     44   1.1       cgd 
     45   1.1       cgd 	void	(*sc_intr_establish) __P((struct device *, void *,
     46   1.1       cgd 		    tc_intrlevel_t, int (*)(void *), void *));
     47   1.1       cgd 	void	(*sc_intr_disestablish) __P((struct device *, void *));
     48   1.1       cgd };
     49   1.1       cgd 
     50   1.1       cgd /* Definition of the driver for autoconfig. */
     51   1.1       cgd int	tcmatch __P((struct device *, void *, void *));
     52   1.1       cgd void	tcattach __P((struct device *, struct device *, void *));
     53  1.11   thorpej 
     54  1.11   thorpej struct cfattach tc_ca = {
     55  1.11   thorpej 	sizeof(struct tc_softc), tcmatch, tcattach
     56  1.11   thorpej };
     57  1.11   thorpej 
     58  1.11   thorpej struct cfdriver tc_cd = {
     59  1.11   thorpej 	NULL, "tc", DV_DULL
     60  1.11   thorpej };
     61   1.1       cgd 
     62   1.1       cgd int	tcprint __P((void *, char *));
     63   1.2       cgd int	tcsubmatch __P((struct device *, void *, void *));
     64   1.1       cgd int	tc_checkslot __P((tc_addr_t, char *));
     65   1.8       cgd void	tc_devinfo __P((const char *, char *));
     66   1.1       cgd 
     67   1.1       cgd int
     68   1.1       cgd tcmatch(parent, cfdata, aux)
     69   1.1       cgd 	struct device *parent;
     70   1.1       cgd 	void *cfdata;
     71   1.1       cgd 	void *aux;
     72   1.1       cgd {
     73   1.2       cgd 	struct cfdata *cf = cfdata;
     74   1.2       cgd 	struct tcbus_attach_args *tba = aux;
     75   1.2       cgd 
     76   1.2       cgd 	if (strcmp(tba->tba_busname, cf->cf_driver->cd_name))
     77   1.2       cgd 		return (0);
     78   1.2       cgd 
     79   1.1       cgd 	return (1);
     80   1.1       cgd }
     81   1.1       cgd 
     82   1.1       cgd void
     83   1.1       cgd tcattach(parent, self, aux)
     84   1.1       cgd 	struct device *parent;
     85   1.1       cgd 	struct device *self;
     86   1.1       cgd 	void *aux;
     87   1.1       cgd {
     88   1.1       cgd 	struct tc_softc *sc = (struct tc_softc *)self;
     89   1.2       cgd 	struct tcbus_attach_args *tba = aux;
     90   1.2       cgd 	struct tc_attach_args ta;
     91   1.1       cgd 	const struct tc_builtin *builtin;
     92   1.1       cgd 	struct tc_slotdesc *slot;
     93   1.1       cgd 	tc_addr_t tcaddr;
     94   1.1       cgd 	int i;
     95   1.1       cgd 
     96  1.15       cgd 	printf(": %s MHz clock\n",
     97   1.3       cgd 	    tba->tba_speed == TC_SPEED_25_MHZ ? "25" : "12.5");
     98   1.1       cgd 
     99   1.1       cgd 	/*
    100   1.1       cgd 	 * Save important CPU/chipset information.
    101   1.1       cgd 	 */
    102   1.2       cgd 	sc->sc_speed = tba->tba_speed;
    103   1.2       cgd 	sc->sc_nslots = tba->tba_nslots;
    104   1.2       cgd 	sc->sc_slots = tba->tba_slots;
    105   1.2       cgd 	sc->sc_intr_establish = tba->tba_intr_establish;
    106   1.2       cgd 	sc->sc_intr_disestablish = tba->tba_intr_disestablish;
    107   1.1       cgd 
    108   1.1       cgd 	/*
    109   1.1       cgd 	 * Try to configure each built-in device
    110   1.1       cgd 	 */
    111   1.2       cgd 	for (i = 0; i < tba->tba_nbuiltins; i++) {
    112   1.2       cgd 		builtin = &tba->tba_builtins[i];
    113   1.1       cgd 
    114   1.1       cgd 		/* sanity check! */
    115   1.1       cgd 		if (builtin->tcb_slot > sc->sc_nslots)
    116   1.1       cgd 			panic("tcattach: builtin %d slot > nslots", i);
    117   1.1       cgd 
    118   1.1       cgd 		/*
    119   1.1       cgd 		 * Make sure device is really there, because some
    120   1.1       cgd 		 * built-in devices are really optional.
    121   1.1       cgd 		 */
    122   1.1       cgd 		tcaddr = sc->sc_slots[builtin->tcb_slot].tcs_addr +
    123   1.1       cgd 		    builtin->tcb_offset;
    124   1.1       cgd 		if (tc_badaddr(tcaddr))
    125   1.1       cgd 			continue;
    126   1.1       cgd 
    127   1.1       cgd 		/*
    128   1.1       cgd 		 * Set up the device attachment information.
    129   1.1       cgd 		 */
    130   1.2       cgd 		strncpy(ta.ta_modname, builtin->tcb_modname, TC_ROM_LLEN);
    131  1.16       cgd #ifdef __alpha__ /* XXX */
    132  1.16       cgd 		ta.ta_bc = tba->tba_bc;
    133  1.16       cgd #endif
    134   1.2       cgd 		ta.ta_modname[TC_ROM_LLEN] = '\0';
    135   1.2       cgd 		ta.ta_slot = builtin->tcb_slot;
    136   1.2       cgd 		ta.ta_offset = builtin->tcb_offset;
    137   1.2       cgd 		ta.ta_addr = tcaddr;
    138   1.2       cgd 		ta.ta_cookie = builtin->tcb_cookie;
    139   1.2       cgd 		ta.ta_busspeed = sc->sc_speed;
    140  1.14       cgd 
    141   1.1       cgd 		/*
    142   1.1       cgd 		 * Mark the slot as used, so we don't check it later.
    143   1.1       cgd 		 */
    144   1.1       cgd 		sc->sc_slots[builtin->tcb_slot].tcs_used = 1;
    145   1.1       cgd 
    146   1.1       cgd 		/*
    147   1.7       cgd 		 * Attach the device.
    148   1.1       cgd 		 */
    149   1.7       cgd 		config_found_sm(self, &ta, tcprint, tcsubmatch);
    150   1.1       cgd 	}
    151   1.1       cgd 
    152   1.1       cgd 	/*
    153   1.1       cgd 	 * Try to configure each unused slot, last to first.
    154   1.1       cgd 	 */
    155   1.1       cgd 	for (i = sc->sc_nslots - 1; i >= 0; i--) {
    156   1.1       cgd 		slot = &sc->sc_slots[i];
    157   1.1       cgd 
    158   1.1       cgd 		/* If already checked above, don't look again now. */
    159   1.1       cgd 		if (slot->tcs_used)
    160   1.1       cgd 			continue;
    161   1.1       cgd 
    162   1.1       cgd 		/*
    163   1.1       cgd 		 * Make sure something is there, and find out what it is.
    164   1.1       cgd 		 */
    165   1.1       cgd 		tcaddr = slot->tcs_addr;
    166   1.1       cgd 		if (tc_badaddr(tcaddr))
    167   1.1       cgd 			continue;
    168   1.2       cgd 		if (tc_checkslot(tcaddr, ta.ta_modname) == 0)
    169   1.1       cgd 			continue;
    170   1.1       cgd 
    171   1.1       cgd 		/*
    172   1.1       cgd 		 * Set up the rest of the attachment information.
    173   1.1       cgd 		 */
    174   1.2       cgd 		ta.ta_slot = i;
    175   1.2       cgd 		ta.ta_offset = 0;
    176   1.2       cgd 		ta.ta_addr = tcaddr;
    177   1.2       cgd 		ta.ta_cookie = slot->tcs_cookie;
    178   1.1       cgd 
    179   1.1       cgd 		/*
    180   1.1       cgd 		 * Mark the slot as used.
    181   1.1       cgd 		 */
    182   1.1       cgd 		slot->tcs_used = 1;
    183   1.1       cgd 
    184   1.1       cgd 		/*
    185   1.7       cgd 		 * Attach the device.
    186   1.1       cgd 		 */
    187   1.7       cgd 		config_found_sm(self, &ta, tcprint, tcsubmatch);
    188   1.1       cgd 	}
    189   1.1       cgd }
    190   1.1       cgd 
    191   1.1       cgd int
    192   1.1       cgd tcprint(aux, pnp)
    193   1.1       cgd 	void *aux;
    194   1.1       cgd 	char *pnp;
    195   1.1       cgd {
    196   1.2       cgd 	struct tc_attach_args *ta = aux;
    197   1.8       cgd 	char devinfo[256];
    198   1.1       cgd 
    199   1.8       cgd 	if (pnp) {
    200   1.8       cgd 		tc_devinfo(ta->ta_modname, devinfo);
    201  1.14       cgd 		printf("%s at %s", devinfo, pnp);
    202   1.8       cgd 	}
    203   1.8       cgd 	printf(" slot %d offset 0x%lx", ta->ta_slot,
    204   1.2       cgd 	    (long)ta->ta_offset);
    205   1.8       cgd 	return (UNCONF);
    206   1.1       cgd }
    207   1.1       cgd 
    208  1.14       cgd int
    209  1.14       cgd tcsubmatch(parent, match, aux)
    210  1.14       cgd 	struct device *parent;
    211  1.14       cgd 	void *match, *aux;
    212   1.1       cgd {
    213   1.2       cgd 	struct cfdata *cf = match;
    214   1.2       cgd 	struct tc_attach_args *d = aux;
    215   1.2       cgd 
    216   1.2       cgd 	if ((cf->tccf_slot != TCCF_SLOT_UNKNOWN) &&
    217   1.2       cgd 	    (cf->tccf_slot != d->ta_slot))
    218   1.2       cgd 		return 0;
    219   1.2       cgd 	if ((cf->tccf_offset != TCCF_SLOT_UNKNOWN) &&
    220   1.2       cgd 	    (cf->tccf_offset != d->ta_offset))
    221   1.2       cgd 		return 0;
    222   1.1       cgd 
    223  1.12  jonathan 	return ((*cf->cf_attach->ca_match)(parent, match, aux));
    224   1.1       cgd }
    225   1.1       cgd 
    226   1.1       cgd 
    227   1.1       cgd #define	NTC_ROMOFFS	2
    228   1.1       cgd static tc_offset_t tc_slot_romoffs[NTC_ROMOFFS] = {
    229   1.1       cgd 	TC_SLOT_ROM,
    230   1.1       cgd 	TC_SLOT_PROTOROM,
    231   1.1       cgd };
    232   1.1       cgd 
    233   1.1       cgd int
    234   1.1       cgd tc_checkslot(slotbase, namep)
    235   1.1       cgd 	tc_addr_t slotbase;
    236   1.1       cgd 	char *namep;
    237   1.1       cgd {
    238   1.1       cgd 	struct tc_rommap *romp;
    239   1.1       cgd 	int i, j;
    240   1.1       cgd 
    241   1.1       cgd 	for (i = 0; i < NTC_ROMOFFS; i++) {
    242   1.1       cgd 		romp = (struct tc_rommap *)
    243   1.1       cgd 		    (slotbase + tc_slot_romoffs[i]);
    244   1.1       cgd 
    245   1.1       cgd 		switch (romp->tcr_width.v) {
    246   1.1       cgd 		case 1:
    247   1.1       cgd 		case 2:
    248   1.1       cgd 		case 4:
    249   1.1       cgd 			break;
    250   1.1       cgd 
    251   1.1       cgd 		default:
    252   1.1       cgd 			continue;
    253   1.1       cgd 		}
    254   1.1       cgd 
    255   1.1       cgd 		if (romp->tcr_stride.v != 4)
    256   1.1       cgd 			continue;
    257   1.1       cgd 
    258   1.1       cgd 		for (j = 0; j < 4; j++)
    259   1.1       cgd 			if (romp->tcr_test[j+0*romp->tcr_stride.v] != 0x55 ||
    260   1.1       cgd 			    romp->tcr_test[j+1*romp->tcr_stride.v] != 0x00 ||
    261   1.1       cgd 			    romp->tcr_test[j+2*romp->tcr_stride.v] != 0xaa ||
    262   1.1       cgd 			    romp->tcr_test[j+3*romp->tcr_stride.v] != 0xff)
    263   1.1       cgd 				continue;
    264   1.1       cgd 
    265   1.1       cgd 		for (j = 0; j < TC_ROM_LLEN; j++)
    266   1.1       cgd 			namep[j] = romp->tcr_modname[j].v;
    267   1.1       cgd 		namep[j] = '\0';
    268   1.1       cgd 		return (1);
    269   1.1       cgd 	}
    270   1.1       cgd 	return (0);
    271   1.1       cgd }
    272   1.1       cgd 
    273   1.1       cgd void
    274   1.1       cgd tc_intr_establish(dev, cookie, level, handler, arg)
    275   1.1       cgd 	struct device *dev;
    276   1.1       cgd 	void *cookie, *arg;
    277   1.1       cgd 	tc_intrlevel_t level;
    278   1.1       cgd 	int (*handler) __P((void *));
    279   1.1       cgd {
    280   1.1       cgd 	struct tc_softc *sc = (struct tc_softc *)dev;
    281   1.1       cgd 
    282  1.14       cgd 	(*sc->sc_intr_establish)(sc->sc_dv.dv_parent, cookie, level,
    283   1.1       cgd 	    handler, arg);
    284   1.1       cgd }
    285   1.1       cgd 
    286   1.1       cgd void
    287   1.1       cgd tc_intr_disestablish(dev, cookie)
    288   1.1       cgd 	struct device *dev;
    289   1.1       cgd 	void *cookie;
    290   1.1       cgd {
    291   1.1       cgd 	struct tc_softc *sc = (struct tc_softc *)dev;
    292   1.1       cgd 
    293   1.1       cgd 	(*sc->sc_intr_disestablish)(sc->sc_dv.dv_parent, cookie);
    294   1.8       cgd }
    295   1.8       cgd 
    296   1.8       cgd #ifdef TCVERBOSE
    297  1.14       cgd /*
    298   1.8       cgd  * Descriptions of of known devices.
    299  1.14       cgd  */
    300   1.8       cgd struct tc_knowndev {
    301  1.10       cgd 	const char *id, *driver, *description;
    302  1.14       cgd };
    303   1.8       cgd 
    304   1.8       cgd #include <dev/tc/tcdevs_data.h>
    305   1.8       cgd #endif /* TCVERBOSE */
    306   1.8       cgd 
    307   1.8       cgd void
    308   1.8       cgd tc_devinfo(id, cp)
    309   1.8       cgd 	const char *id;
    310   1.8       cgd 	char *cp;
    311   1.8       cgd {
    312  1.10       cgd 	const char *driver, *description;
    313   1.8       cgd #ifdef TCVERBOSE
    314   1.8       cgd 	struct tc_knowndev *tdp;
    315   1.8       cgd 	int match;
    316   1.9       cgd 	const char *unmatched = "unknown ";
    317   1.8       cgd #else
    318   1.8       cgd 	const char *unmatched = "";
    319   1.8       cgd #endif
    320   1.8       cgd 
    321  1.10       cgd 	driver = NULL;
    322  1.10       cgd 	description = id;
    323   1.8       cgd 
    324   1.8       cgd #ifdef TCVERBOSE
    325   1.8       cgd 	/* find the device in the table, if possible. */
    326   1.8       cgd 	tdp = tc_knowndevs;
    327   1.8       cgd 	while (tdp->id != NULL) {
    328   1.8       cgd 		/* check this entry for a match */
    329   1.8       cgd 		match = !strcmp(tdp->id, id);
    330   1.8       cgd 		if (match) {
    331  1.10       cgd 			driver = tdp->driver;
    332  1.10       cgd 			description = tdp->description;
    333   1.8       cgd 			break;
    334   1.8       cgd 		}
    335   1.8       cgd 		tdp++;
    336   1.8       cgd 	}
    337   1.8       cgd #endif
    338   1.8       cgd 
    339  1.10       cgd 	if (driver == NULL)
    340   1.9       cgd 		cp += sprintf(cp, "%sdevice %s", unmatched, id);
    341   1.8       cgd 	else
    342  1.10       cgd 		cp += sprintf(cp, "%s (%s)", driver, description);
    343   1.1       cgd }
    344