xref: /openbsd-src/sys/dev/tc/tc.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: tc.c,v 1.9 1996/12/08 01:03:05 niklas Exp $	*/
2 /*	$NetBSD: tc.c,v 1.20 1996/10/22 21:37:29 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1994, 1995 Carnegie-Mellon University.
6  * All rights reserved.
7  *
8  * Author: Chris G. Demetriou
9  *
10  * Permission to use, copy, modify and distribute this software and
11  * its documentation is hereby granted, provided that both the copyright
12  * notice and this permission notice appear in all copies of the
13  * software, derivative works or modified versions, and any portions
14  * thereof, and that both notices appear in supporting documentation.
15  *
16  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
18  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19  *
20  * Carnegie Mellon requests users of this software to return to
21  *
22  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
23  *  School of Computer Science
24  *  Carnegie Mellon University
25  *  Pittsburgh PA 15213-3890
26  *
27  * any improvements or extensions that they make and grant Carnegie the
28  * rights to redistribute these changes.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/device.h>
34 
35 #include <dev/tc/tcreg.h>
36 #include <dev/tc/tcvar.h>
37 #include <dev/tc/tcdevs.h>
38 
39 #include <machine/autoconf.h>	/* for the proto of badaddr() */
40 
41 struct tc_softc {
42 	struct	device sc_dv;
43 
44 	int	sc_speed;
45 	int	sc_nslots;
46 	struct tc_slotdesc *sc_slots;
47 
48 	void	(*sc_intr_establish) __P((struct device *, void *,
49 		    tc_intrlevel_t, int (*)(void *), void *));
50 	void	(*sc_intr_disestablish) __P((struct device *, void *));
51 };
52 
53 /* Definition of the driver for autoconfig. */
54 int	tcmatch __P((struct device *, void *, void *));
55 void	tcattach __P((struct device *, struct device *, void *));
56 
57 struct cfattach tc_ca = {
58 	sizeof(struct tc_softc), tcmatch, tcattach
59 };
60 
61 struct cfdriver tc_cd = {
62 	NULL, "tc", DV_DULL
63 };
64 
65 int	tcprint __P((void *, const char *));
66 int	tcsubmatch __P((struct device *, void *, void *));
67 int	tc_checkslot __P((tc_addr_t, char *));
68 void	tc_devinfo __P((const char *, char *));
69 
70 int
71 tcmatch(parent, cfdata, aux)
72 	struct device *parent;
73 	void *cfdata;
74 	void *aux;
75 {
76 	struct cfdata *cf = cfdata;
77 	struct tcbus_attach_args *tba = aux;
78 
79 	if (strcmp(tba->tba_busname, cf->cf_driver->cd_name))
80 		return (0);
81 
82 	return (1);
83 }
84 
85 void
86 tcattach(parent, self, aux)
87 	struct device *parent;
88 	struct device *self;
89 	void *aux;
90 {
91 	struct tc_softc *sc = (struct tc_softc *)self;
92 	struct tcbus_attach_args *tba = aux;
93 	struct tc_attach_args ta;
94 	const struct tc_builtin *builtin;
95 	struct tc_slotdesc *slot;
96 	tc_addr_t tcaddr;
97 	int i;
98 
99 	printf(": %s MHz clock\n",
100 	    tba->tba_speed == TC_SPEED_25_MHZ ? "25" : "12.5");
101 
102 	/*
103 	 * Save important CPU/chipset information.
104 	 */
105 	sc->sc_speed = tba->tba_speed;
106 	sc->sc_nslots = tba->tba_nslots;
107 	sc->sc_slots = tba->tba_slots;
108 	sc->sc_intr_establish = tba->tba_intr_establish;
109 	sc->sc_intr_disestablish = tba->tba_intr_disestablish;
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 #endif
137 		ta.ta_modname[TC_ROM_LLEN] = '\0';
138 		ta.ta_slot = builtin->tcb_slot;
139 		ta.ta_offset = builtin->tcb_offset;
140 		ta.ta_addr = tcaddr;
141 		ta.ta_cookie = builtin->tcb_cookie;
142 		ta.ta_busspeed = sc->sc_speed;
143 
144 		/*
145 		 * Mark the slot as used, so we don't check it later.
146 		 */
147 		sc->sc_slots[builtin->tcb_slot].tcs_used = 1;
148 
149 		/*
150 		 * Attach the device.
151 		 */
152 		config_found_sm(self, &ta, tcprint, tcsubmatch);
153 	}
154 
155 	/*
156 	 * Try to configure each unused slot, last to first.
157 	 */
158 	for (i = sc->sc_nslots - 1; i >= 0; i--) {
159 		slot = &sc->sc_slots[i];
160 
161 		/* If already checked above, don't look again now. */
162 		if (slot->tcs_used)
163 			continue;
164 
165 		/*
166 		 * Make sure something is there, and find out what it is.
167 		 */
168 		tcaddr = slot->tcs_addr;
169 		if (tc_badaddr(tcaddr))
170 			continue;
171 		if (tc_checkslot(tcaddr, ta.ta_modname) == 0)
172 			continue;
173 
174 		/*
175 		 * Set up the rest of the attachment information.
176 		 */
177 		ta.ta_slot = i;
178 		ta.ta_offset = 0;
179 		ta.ta_addr = tcaddr;
180 		ta.ta_cookie = slot->tcs_cookie;
181 
182 		/*
183 		 * Mark the slot as used.
184 		 */
185 		slot->tcs_used = 1;
186 
187 		/*
188 		 * Attach the device.
189 		 */
190 		config_found_sm(self, &ta, tcprint, tcsubmatch);
191 	}
192 }
193 
194 int
195 tcprint(aux, pnp)
196 	void *aux;
197 	const char *pnp;
198 {
199 	struct tc_attach_args *ta = aux;
200 	char devinfo[256];
201 
202 	if (pnp) {
203 		tc_devinfo(ta->ta_modname, devinfo);
204 		printf("%s at %s", devinfo, pnp);
205 	}
206 	printf(" slot %d offset 0x%lx", ta->ta_slot,
207 	    (long)ta->ta_offset);
208 	return (UNCONF);
209 }
210 
211 int
212 tcsubmatch(parent, match, aux)
213 	struct device *parent;
214 	void *match, *aux;
215 {
216 	struct cfdata *cf = match;
217 	struct tc_attach_args *d = aux;
218 
219 	if ((cf->tccf_slot != TCCF_SLOT_UNKNOWN) &&
220 	    (cf->tccf_slot != d->ta_slot))
221 		return 0;
222 	if ((cf->tccf_offset != TCCF_SLOT_UNKNOWN) &&
223 	    (cf->tccf_offset != d->ta_offset))
224 		return 0;
225 
226 	return ((*cf->cf_attach->ca_match)(parent, match, aux));
227 }
228 
229 
230 #define	NTC_ROMOFFS	2
231 static tc_offset_t tc_slot_romoffs[NTC_ROMOFFS] = {
232 	TC_SLOT_ROM,
233 	TC_SLOT_PROTOROM,
234 };
235 
236 int
237 tc_checkslot(slotbase, namep)
238 	tc_addr_t slotbase;
239 	char *namep;
240 {
241 	struct tc_rommap *romp;
242 	int i, j;
243 
244 	for (i = 0; i < NTC_ROMOFFS; i++) {
245 		romp = (struct tc_rommap *)
246 		    (slotbase + tc_slot_romoffs[i]);
247 
248 		switch (romp->tcr_width.v) {
249 		case 1:
250 		case 2:
251 		case 4:
252 			break;
253 
254 		default:
255 			continue;
256 		}
257 
258 		if (romp->tcr_stride.v != 4)
259 			continue;
260 
261 		for (j = 0; j < 4; j++)
262 			if (romp->tcr_test[j+0*romp->tcr_stride.v] != 0x55 ||
263 			    romp->tcr_test[j+1*romp->tcr_stride.v] != 0x00 ||
264 			    romp->tcr_test[j+2*romp->tcr_stride.v] != 0xaa ||
265 			    romp->tcr_test[j+3*romp->tcr_stride.v] != 0xff)
266 				continue;
267 
268 		for (j = 0; j < TC_ROM_LLEN; j++)
269 			namep[j] = romp->tcr_modname[j].v;
270 		namep[j] = '\0';
271 		return (1);
272 	}
273 	return (0);
274 }
275 
276 void
277 tc_intr_establish(dev, cookie, level, handler, arg)
278 	struct device *dev;
279 	void *cookie, *arg;
280 	tc_intrlevel_t level;
281 	int (*handler) __P((void *));
282 {
283 	struct tc_softc *sc = (struct tc_softc *)dev;
284 
285 	(*sc->sc_intr_establish)(sc->sc_dv.dv_parent, cookie, level,
286 	    handler, arg);
287 }
288 
289 void
290 tc_intr_disestablish(dev, cookie)
291 	struct device *dev;
292 	void *cookie;
293 {
294 	struct tc_softc *sc = (struct tc_softc *)dev;
295 
296 	(*sc->sc_intr_disestablish)(sc->sc_dv.dv_parent, cookie);
297 }
298 
299 #ifdef TCVERBOSE
300 /*
301  * Descriptions of of known devices.
302  */
303 struct tc_knowndev {
304 	const char *id, *driver, *description;
305 };
306 
307 #include <dev/tc/tcdevs_data.h>
308 #endif /* TCVERBOSE */
309 
310 void
311 tc_devinfo(id, cp)
312 	const char *id;
313 	char *cp;
314 {
315 	const char *driver, *description;
316 #ifdef TCVERBOSE
317 	struct tc_knowndev *tdp;
318 	int match;
319 	const char *unmatched = "unknown ";
320 #else
321 	const char *unmatched = "";
322 #endif
323 
324 	driver = NULL;
325 	description = id;
326 
327 #ifdef TCVERBOSE
328 	/* find the device in the table, if possible. */
329 	tdp = tc_knowndevs;
330 	while (tdp->id != NULL) {
331 		/* check this entry for a match */
332 		match = !strcmp(tdp->id, id);
333 		if (match) {
334 			driver = tdp->driver;
335 			description = tdp->description;
336 			break;
337 		}
338 		tdp++;
339 	}
340 #endif
341 
342 	if (driver == NULL)
343 		cp += sprintf(cp, "%sdevice %s", unmatched, id);
344 	else
345 		cp += sprintf(cp, "%s (%s)", driver, description);
346 }
347