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