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