Home | History | Annotate | Line # | Download | only in tc
tc.c revision 1.40
      1  1.40     perry /*	$NetBSD: tc.c,v 1.40 2005/02/04 02:10:48 perry 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.29     lukem 
     30  1.29     lukem #include <sys/cdefs.h>
     31  1.40     perry __KERNEL_RCSID(0, "$NetBSD: tc.c,v 1.40 2005/02/04 02:10:48 perry Exp $");
     32  1.23     enami 
     33  1.23     enami #include "opt_tcverbose.h"
     34   1.1       cgd 
     35   1.1       cgd #include <sys/param.h>
     36  1.13  jonathan #include <sys/systm.h>
     37   1.1       cgd #include <sys/device.h>
     38   1.1       cgd 
     39   1.1       cgd #include <dev/tc/tcreg.h>
     40   1.1       cgd #include <dev/tc/tcvar.h>
     41   1.8       cgd #include <dev/tc/tcdevs.h>
     42   1.1       cgd 
     43  1.39  drochner #include "locators.h"
     44   1.1       cgd 
     45   1.1       cgd /* Definition of the driver for autoconfig. */
     46  1.40     perry int	tcmatch(struct device *, struct cfdata *, void *);
     47  1.11   thorpej 
     48  1.33   thorpej CFATTACH_DECL(tc, sizeof(struct tc_softc),
     49  1.34   thorpej     tcmatch, tcattach, NULL, NULL);
     50  1.33   thorpej 
     51  1.27  nisimura extern struct cfdriver tc_cd;
     52   1.1       cgd 
     53  1.40     perry int	tcprint(void *, const char *);
     54  1.40     perry int	tcsubmatch(struct device *, struct cfdata *,
     55  1.40     perry 			const locdesc_t *, void *);
     56  1.40     perry void	tc_devinfo(const char *, char *, size_t);
     57   1.1       cgd 
     58   1.1       cgd int
     59  1.21       cgd tcmatch(parent, cf, aux)
     60   1.1       cgd 	struct device *parent;
     61  1.21       cgd 	struct cfdata *cf;
     62   1.1       cgd 	void *aux;
     63   1.1       cgd {
     64   1.2       cgd 	struct tcbus_attach_args *tba = aux;
     65   1.2       cgd 
     66  1.30   thorpej 	if (strcmp(tba->tba_busname, cf->cf_name))
     67   1.2       cgd 		return (0);
     68   1.2       cgd 
     69   1.1       cgd 	return (1);
     70   1.1       cgd }
     71   1.1       cgd 
     72   1.1       cgd void
     73   1.1       cgd tcattach(parent, self, aux)
     74   1.1       cgd 	struct device *parent;
     75   1.1       cgd 	struct device *self;
     76   1.1       cgd 	void *aux;
     77   1.1       cgd {
     78   1.1       cgd 	struct tc_softc *sc = (struct tc_softc *)self;
     79   1.2       cgd 	struct tcbus_attach_args *tba = aux;
     80   1.2       cgd 	struct tc_attach_args ta;
     81   1.1       cgd 	const struct tc_builtin *builtin;
     82   1.1       cgd 	struct tc_slotdesc *slot;
     83   1.1       cgd 	tc_addr_t tcaddr;
     84   1.1       cgd 	int i;
     85  1.39  drochner 	int help[3];
     86  1.39  drochner 	locdesc_t *ldesc = (void *)&help; /* XXX */
     87   1.1       cgd 
     88  1.19  christos 	printf(": %s MHz clock\n",
     89   1.3       cgd 	    tba->tba_speed == TC_SPEED_25_MHZ ? "25" : "12.5");
     90   1.1       cgd 
     91   1.1       cgd 	/*
     92   1.1       cgd 	 * Save important CPU/chipset information.
     93   1.1       cgd 	 */
     94   1.2       cgd 	sc->sc_speed = tba->tba_speed;
     95   1.2       cgd 	sc->sc_nslots = tba->tba_nslots;
     96   1.2       cgd 	sc->sc_slots = tba->tba_slots;
     97  1.28       cgd 	sc->sc_intr_evcnt = tba->tba_intr_evcnt;
     98   1.2       cgd 	sc->sc_intr_establish = tba->tba_intr_establish;
     99   1.2       cgd 	sc->sc_intr_disestablish = tba->tba_intr_disestablish;
    100  1.25   thorpej 	sc->sc_get_dma_tag = tba->tba_get_dma_tag;
    101   1.1       cgd 
    102   1.1       cgd 	/*
    103   1.1       cgd 	 * Try to configure each built-in device
    104   1.1       cgd 	 */
    105   1.2       cgd 	for (i = 0; i < tba->tba_nbuiltins; i++) {
    106   1.2       cgd 		builtin = &tba->tba_builtins[i];
    107   1.1       cgd 
    108   1.1       cgd 		/* sanity check! */
    109   1.1       cgd 		if (builtin->tcb_slot > sc->sc_nslots)
    110   1.1       cgd 			panic("tcattach: builtin %d slot > nslots", i);
    111   1.1       cgd 
    112   1.1       cgd 		/*
    113   1.1       cgd 		 * Make sure device is really there, because some
    114   1.1       cgd 		 * built-in devices are really optional.
    115   1.1       cgd 		 */
    116   1.1       cgd 		tcaddr = sc->sc_slots[builtin->tcb_slot].tcs_addr +
    117   1.1       cgd 		    builtin->tcb_offset;
    118   1.1       cgd 		if (tc_badaddr(tcaddr))
    119   1.1       cgd 			continue;
    120   1.1       cgd 
    121   1.1       cgd 		/*
    122   1.1       cgd 		 * Set up the device attachment information.
    123   1.1       cgd 		 */
    124   1.2       cgd 		strncpy(ta.ta_modname, builtin->tcb_modname, TC_ROM_LLEN);
    125  1.20       cgd 		ta.ta_memt = tba->tba_memt;
    126  1.25   thorpej 		ta.ta_dmat = (*sc->sc_get_dma_tag)(builtin->tcb_slot);
    127   1.2       cgd 		ta.ta_modname[TC_ROM_LLEN] = '\0';
    128   1.2       cgd 		ta.ta_slot = builtin->tcb_slot;
    129   1.2       cgd 		ta.ta_offset = builtin->tcb_offset;
    130   1.2       cgd 		ta.ta_addr = tcaddr;
    131   1.2       cgd 		ta.ta_cookie = builtin->tcb_cookie;
    132   1.2       cgd 		ta.ta_busspeed = sc->sc_speed;
    133  1.14       cgd 
    134   1.1       cgd 		/*
    135   1.1       cgd 		 * Mark the slot as used, so we don't check it later.
    136   1.1       cgd 		 */
    137   1.1       cgd 		sc->sc_slots[builtin->tcb_slot].tcs_used = 1;
    138   1.1       cgd 
    139  1.39  drochner 		ldesc->len = 2;
    140  1.39  drochner 		ldesc->locs[TCCF_SLOT] = builtin->tcb_slot;
    141  1.39  drochner 		ldesc->locs[TCCF_OFFSET] = builtin->tcb_offset;
    142   1.1       cgd 		/*
    143   1.7       cgd 		 * Attach the device.
    144   1.1       cgd 		 */
    145  1.39  drochner 		config_found_sm_loc(self, "tc", ldesc, &ta,
    146  1.39  drochner 				    tcprint, tcsubmatch);
    147   1.1       cgd 	}
    148   1.1       cgd 
    149   1.1       cgd 	/*
    150   1.1       cgd 	 * Try to configure each unused slot, last to first.
    151   1.1       cgd 	 */
    152   1.1       cgd 	for (i = sc->sc_nslots - 1; i >= 0; i--) {
    153   1.1       cgd 		slot = &sc->sc_slots[i];
    154   1.1       cgd 
    155   1.1       cgd 		/* If already checked above, don't look again now. */
    156   1.1       cgd 		if (slot->tcs_used)
    157   1.1       cgd 			continue;
    158   1.1       cgd 
    159   1.1       cgd 		/*
    160   1.1       cgd 		 * Make sure something is there, and find out what it is.
    161   1.1       cgd 		 */
    162   1.1       cgd 		tcaddr = slot->tcs_addr;
    163   1.1       cgd 		if (tc_badaddr(tcaddr))
    164   1.1       cgd 			continue;
    165   1.2       cgd 		if (tc_checkslot(tcaddr, ta.ta_modname) == 0)
    166   1.1       cgd 			continue;
    167   1.1       cgd 
    168   1.1       cgd 		/*
    169   1.1       cgd 		 * Set up the rest of the attachment information.
    170   1.1       cgd 		 */
    171  1.25   thorpej 		ta.ta_memt = tba->tba_memt;
    172  1.25   thorpej 		ta.ta_dmat = (*sc->sc_get_dma_tag)(i);
    173   1.2       cgd 		ta.ta_slot = i;
    174   1.2       cgd 		ta.ta_offset = 0;
    175   1.2       cgd 		ta.ta_addr = tcaddr;
    176   1.2       cgd 		ta.ta_cookie = slot->tcs_cookie;
    177   1.1       cgd 
    178   1.1       cgd 		/*
    179   1.1       cgd 		 * Mark the slot as used.
    180   1.1       cgd 		 */
    181   1.1       cgd 		slot->tcs_used = 1;
    182   1.1       cgd 
    183  1.39  drochner 		ldesc->len = 2;
    184  1.39  drochner 		ldesc->locs[TCCF_SLOT] = i;
    185  1.39  drochner 		ldesc->locs[TCCF_OFFSET] = 0;
    186   1.1       cgd 		/*
    187   1.7       cgd 		 * Attach the device.
    188   1.1       cgd 		 */
    189  1.39  drochner 		config_found_sm_loc(self, "tc", ldesc, &ta,
    190  1.39  drochner 				    tcprint, tcsubmatch);
    191   1.1       cgd 	}
    192   1.1       cgd }
    193   1.1       cgd 
    194   1.1       cgd int
    195   1.1       cgd tcprint(aux, pnp)
    196   1.1       cgd 	void *aux;
    197  1.17       cgd 	const char *pnp;
    198   1.1       cgd {
    199   1.2       cgd 	struct tc_attach_args *ta = aux;
    200   1.8       cgd 	char devinfo[256];
    201   1.1       cgd 
    202   1.8       cgd 	if (pnp) {
    203  1.37    itojun 		tc_devinfo(ta->ta_modname, devinfo, sizeof(devinfo));
    204  1.35   thorpej 		aprint_normal("%s at %s", devinfo, pnp);
    205   1.8       cgd 	}
    206  1.36   tsutsui 	aprint_normal(" slot %d offset 0x%x", ta->ta_slot, ta->ta_offset);
    207   1.8       cgd 	return (UNCONF);
    208   1.1       cgd }
    209   1.1       cgd 
    210  1.14       cgd int
    211  1.39  drochner tcsubmatch(parent, cf, ldesc, aux)
    212  1.14       cgd 	struct device *parent;
    213  1.21       cgd 	struct cfdata *cf;
    214  1.39  drochner 	const locdesc_t *ldesc;
    215  1.21       cgd 	void *aux;
    216   1.1       cgd {
    217   1.2       cgd 
    218  1.39  drochner 	if ((cf->cf_loc[TCCF_SLOT] != TCCF_SLOT_DEFAULT) &&
    219  1.39  drochner 	    (cf->cf_loc[TCCF_SLOT] != ldesc->locs[TCCF_SLOT]))
    220   1.2       cgd 		return 0;
    221  1.39  drochner 	if ((cf->cf_loc[TCCF_OFFSET] != TCCF_OFFSET_DEFAULT) &&
    222  1.39  drochner 	    (cf->cf_loc[TCCF_OFFSET] != ldesc->locs[TCCF_OFFSET]))
    223   1.2       cgd 		return 0;
    224   1.1       cgd 
    225  1.31   thorpej 	return (config_match(parent, cf, aux));
    226   1.1       cgd }
    227   1.1       cgd 
    228   1.1       cgd 
    229   1.1       cgd #define	NTC_ROMOFFS	2
    230   1.1       cgd static tc_offset_t tc_slot_romoffs[NTC_ROMOFFS] = {
    231   1.1       cgd 	TC_SLOT_ROM,
    232   1.1       cgd 	TC_SLOT_PROTOROM,
    233   1.1       cgd };
    234   1.1       cgd 
    235   1.1       cgd int
    236   1.1       cgd tc_checkslot(slotbase, namep)
    237   1.1       cgd 	tc_addr_t slotbase;
    238   1.1       cgd 	char *namep;
    239   1.1       cgd {
    240   1.1       cgd 	struct tc_rommap *romp;
    241   1.1       cgd 	int i, j;
    242   1.1       cgd 
    243   1.1       cgd 	for (i = 0; i < NTC_ROMOFFS; i++) {
    244   1.1       cgd 		romp = (struct tc_rommap *)
    245   1.1       cgd 		    (slotbase + tc_slot_romoffs[i]);
    246   1.1       cgd 
    247   1.1       cgd 		switch (romp->tcr_width.v) {
    248   1.1       cgd 		case 1:
    249   1.1       cgd 		case 2:
    250   1.1       cgd 		case 4:
    251   1.1       cgd 			break;
    252   1.1       cgd 
    253   1.1       cgd 		default:
    254   1.1       cgd 			continue;
    255   1.1       cgd 		}
    256   1.1       cgd 
    257   1.1       cgd 		if (romp->tcr_stride.v != 4)
    258   1.1       cgd 			continue;
    259   1.1       cgd 
    260   1.1       cgd 		for (j = 0; j < 4; j++)
    261   1.1       cgd 			if (romp->tcr_test[j+0*romp->tcr_stride.v] != 0x55 ||
    262   1.1       cgd 			    romp->tcr_test[j+1*romp->tcr_stride.v] != 0x00 ||
    263   1.1       cgd 			    romp->tcr_test[j+2*romp->tcr_stride.v] != 0xaa ||
    264   1.1       cgd 			    romp->tcr_test[j+3*romp->tcr_stride.v] != 0xff)
    265   1.1       cgd 				continue;
    266   1.1       cgd 
    267   1.1       cgd 		for (j = 0; j < TC_ROM_LLEN; j++)
    268   1.1       cgd 			namep[j] = romp->tcr_modname[j].v;
    269   1.1       cgd 		namep[j] = '\0';
    270   1.1       cgd 		return (1);
    271   1.1       cgd 	}
    272   1.1       cgd 	return (0);
    273  1.28       cgd }
    274  1.28       cgd 
    275  1.28       cgd const struct evcnt *
    276  1.28       cgd tc_intr_evcnt(struct device *dev, void *cookie)
    277  1.28       cgd {
    278  1.28       cgd 	struct tc_softc *sc = tc_cd.cd_devs[0];
    279  1.28       cgd 
    280  1.28       cgd 	return ((*sc->sc_intr_evcnt)(dev, cookie));
    281   1.1       cgd }
    282   1.1       cgd 
    283   1.1       cgd void
    284   1.1       cgd tc_intr_establish(dev, cookie, level, handler, arg)
    285   1.1       cgd 	struct device *dev;
    286   1.1       cgd 	void *cookie, *arg;
    287  1.27  nisimura 	int level;
    288  1.40     perry 	int (*handler)(void *);
    289   1.1       cgd {
    290  1.27  nisimura 	struct tc_softc *sc = tc_cd.cd_devs[0];
    291   1.1       cgd 
    292  1.27  nisimura 	(*sc->sc_intr_establish)(dev, cookie, level, handler, arg);
    293   1.1       cgd }
    294   1.1       cgd 
    295   1.1       cgd void
    296   1.1       cgd tc_intr_disestablish(dev, cookie)
    297   1.1       cgd 	struct device *dev;
    298   1.1       cgd 	void *cookie;
    299   1.1       cgd {
    300  1.27  nisimura 	struct tc_softc *sc = tc_cd.cd_devs[0];
    301   1.1       cgd 
    302  1.27  nisimura 	(*sc->sc_intr_disestablish)(dev, cookie);
    303   1.8       cgd }
    304   1.8       cgd 
    305   1.8       cgd #ifdef TCVERBOSE
    306  1.14       cgd /*
    307   1.8       cgd  * Descriptions of of known devices.
    308  1.14       cgd  */
    309   1.8       cgd struct tc_knowndev {
    310  1.10       cgd 	const char *id, *driver, *description;
    311  1.14       cgd };
    312   1.8       cgd 
    313   1.8       cgd #include <dev/tc/tcdevs_data.h>
    314   1.8       cgd #endif /* TCVERBOSE */
    315   1.8       cgd 
    316   1.8       cgd void
    317  1.37    itojun tc_devinfo(id, cp, l)
    318   1.8       cgd 	const char *id;
    319   1.8       cgd 	char *cp;
    320  1.37    itojun 	size_t l;
    321   1.8       cgd {
    322  1.10       cgd 	const char *driver, *description;
    323   1.8       cgd #ifdef TCVERBOSE
    324   1.8       cgd 	struct tc_knowndev *tdp;
    325   1.8       cgd 	int match;
    326   1.9       cgd 	const char *unmatched = "unknown ";
    327   1.8       cgd #else
    328   1.8       cgd 	const char *unmatched = "";
    329   1.8       cgd #endif
    330   1.8       cgd 
    331  1.10       cgd 	driver = NULL;
    332  1.10       cgd 	description = id;
    333   1.8       cgd 
    334   1.8       cgd #ifdef TCVERBOSE
    335   1.8       cgd 	/* find the device in the table, if possible. */
    336   1.8       cgd 	tdp = tc_knowndevs;
    337   1.8       cgd 	while (tdp->id != NULL) {
    338   1.8       cgd 		/* check this entry for a match */
    339   1.8       cgd 		match = !strcmp(tdp->id, id);
    340   1.8       cgd 		if (match) {
    341  1.10       cgd 			driver = tdp->driver;
    342  1.10       cgd 			description = tdp->description;
    343   1.8       cgd 			break;
    344   1.8       cgd 		}
    345   1.8       cgd 		tdp++;
    346   1.8       cgd 	}
    347   1.8       cgd #endif
    348   1.8       cgd 
    349  1.10       cgd 	if (driver == NULL)
    350  1.37    itojun 		snprintf(cp, l, "%sdevice %s", unmatched, id);
    351   1.8       cgd 	else
    352  1.37    itojun 		snprintf(cp, l, "%s (%s)", driver, description);
    353   1.1       cgd }
    354