xref: /netbsd-src/sys/dev/tc/tc.c (revision 3b435a73967be44dfb4a27315acd72bfacde430c)
1 /*	$NetBSD: tc.c,v 1.26 1998/05/22 21:15:48 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 	bus_dma_tag_t (*sc_get_dma_tag) __P((int));
51 };
52 
53 /* Definition of the driver for autoconfig. */
54 int	tcmatch __P((struct device *, struct cfdata *, 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 int	tcprint __P((void *, const char *));
62 int	tcsubmatch __P((struct device *, struct cfdata *, void *));
63 int	tc_checkslot __P((tc_addr_t, char *));
64 void	tc_devinfo __P((const char *, char *));
65 
66 int
67 tcmatch(parent, cf, aux)
68 	struct device *parent;
69 	struct cfdata *cf;
70 	void *aux;
71 {
72 	struct tcbus_attach_args *tba = aux;
73 
74 	if (strcmp(tba->tba_busname, cf->cf_driver->cd_name))
75 		return (0);
76 
77 	return (1);
78 }
79 
80 void
81 tcattach(parent, self, aux)
82 	struct device *parent;
83 	struct device *self;
84 	void *aux;
85 {
86 	struct tc_softc *sc = (struct tc_softc *)self;
87 	struct tcbus_attach_args *tba = aux;
88 	struct tc_attach_args ta;
89 	const struct tc_builtin *builtin;
90 	struct tc_slotdesc *slot;
91 	tc_addr_t tcaddr;
92 	int i;
93 
94 	printf(": %s MHz clock\n",
95 	    tba->tba_speed == TC_SPEED_25_MHZ ? "25" : "12.5");
96 
97 	/*
98 	 * Save important CPU/chipset information.
99 	 */
100 	sc->sc_speed = tba->tba_speed;
101 	sc->sc_nslots = tba->tba_nslots;
102 	sc->sc_slots = tba->tba_slots;
103 	sc->sc_intr_establish = tba->tba_intr_establish;
104 	sc->sc_intr_disestablish = tba->tba_intr_disestablish;
105 	sc->sc_get_dma_tag = tba->tba_get_dma_tag;
106 
107 	/*
108 	 * Try to configure each built-in device
109 	 */
110 	for (i = 0; i < tba->tba_nbuiltins; i++) {
111 		builtin = &tba->tba_builtins[i];
112 
113 		/* sanity check! */
114 		if (builtin->tcb_slot > sc->sc_nslots)
115 			panic("tcattach: builtin %d slot > nslots", i);
116 
117 		/*
118 		 * Make sure device is really there, because some
119 		 * built-in devices are really optional.
120 		 */
121 		tcaddr = sc->sc_slots[builtin->tcb_slot].tcs_addr +
122 		    builtin->tcb_offset;
123 		if (tc_badaddr(tcaddr))
124 			continue;
125 
126 		/*
127 		 * Set up the device attachment information.
128 		 */
129 		strncpy(ta.ta_modname, builtin->tcb_modname, TC_ROM_LLEN);
130 		ta.ta_memt = tba->tba_memt;
131 		ta.ta_dmat = (*sc->sc_get_dma_tag)(builtin->tcb_slot);
132 		ta.ta_modname[TC_ROM_LLEN] = '\0';
133 		ta.ta_slot = builtin->tcb_slot;
134 		ta.ta_offset = builtin->tcb_offset;
135 		ta.ta_addr = tcaddr;
136 		ta.ta_cookie = builtin->tcb_cookie;
137 		ta.ta_busspeed = sc->sc_speed;
138 
139 		/*
140 		 * Mark the slot as used, so we don't check it later.
141 		 */
142 		sc->sc_slots[builtin->tcb_slot].tcs_used = 1;
143 
144 		/*
145 		 * Attach the device.
146 		 */
147 		config_found_sm(self, &ta, tcprint, tcsubmatch);
148 	}
149 
150 	/*
151 	 * Try to configure each unused slot, last to first.
152 	 */
153 	for (i = sc->sc_nslots - 1; i >= 0; i--) {
154 		slot = &sc->sc_slots[i];
155 
156 		/* If already checked above, don't look again now. */
157 		if (slot->tcs_used)
158 			continue;
159 
160 		/*
161 		 * Make sure something is there, and find out what it is.
162 		 */
163 		tcaddr = slot->tcs_addr;
164 		if (tc_badaddr(tcaddr))
165 			continue;
166 		if (tc_checkslot(tcaddr, ta.ta_modname) == 0)
167 			continue;
168 
169 		/*
170 		 * Set up the rest of the attachment information.
171 		 */
172 		ta.ta_memt = tba->tba_memt;
173 		ta.ta_dmat = (*sc->sc_get_dma_tag)(i);
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, cf, aux)
210 	struct device *parent;
211 	struct cfdata *cf;
212 	void *aux;
213 {
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, cf, 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