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