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