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