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