xref: /netbsd/sys/dev/tc/tc.c (revision bf9ec67e)
1 /*	$NetBSD: tc.c,v 1.29 2001/11/13 06:26:10 lukem 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.29 2001/11/13 06:26:10 lukem 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 void	tcattach __P((struct device *, struct device *, void *));
47 
48 struct cfattach tc_ca = {
49 	sizeof(struct tc_softc), tcmatch, tcattach
50 };
51 extern struct cfdriver tc_cd;
52 
53 int	tcprint __P((void *, const char *));
54 int	tcsubmatch __P((struct device *, struct cfdata *, void *));
55 int	tc_checkslot __P((tc_addr_t, char *));
56 void	tc_devinfo __P((const char *, char *));
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_driver->cd_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 
86 	printf(": %s MHz clock\n",
87 	    tba->tba_speed == TC_SPEED_25_MHZ ? "25" : "12.5");
88 
89 	/*
90 	 * Save important CPU/chipset information.
91 	 */
92 	sc->sc_speed = tba->tba_speed;
93 	sc->sc_nslots = tba->tba_nslots;
94 	sc->sc_slots = tba->tba_slots;
95 	sc->sc_intr_evcnt = tba->tba_intr_evcnt;
96 	sc->sc_intr_establish = tba->tba_intr_establish;
97 	sc->sc_intr_disestablish = tba->tba_intr_disestablish;
98 	sc->sc_get_dma_tag = tba->tba_get_dma_tag;
99 
100 	/*
101 	 * Try to configure each built-in device
102 	 */
103 	for (i = 0; i < tba->tba_nbuiltins; i++) {
104 		builtin = &tba->tba_builtins[i];
105 
106 		/* sanity check! */
107 		if (builtin->tcb_slot > sc->sc_nslots)
108 			panic("tcattach: builtin %d slot > nslots", i);
109 
110 		/*
111 		 * Make sure device is really there, because some
112 		 * built-in devices are really optional.
113 		 */
114 		tcaddr = sc->sc_slots[builtin->tcb_slot].tcs_addr +
115 		    builtin->tcb_offset;
116 		if (tc_badaddr(tcaddr))
117 			continue;
118 
119 		/*
120 		 * Set up the device attachment information.
121 		 */
122 		strncpy(ta.ta_modname, builtin->tcb_modname, TC_ROM_LLEN);
123 		ta.ta_memt = tba->tba_memt;
124 		ta.ta_dmat = (*sc->sc_get_dma_tag)(builtin->tcb_slot);
125 		ta.ta_modname[TC_ROM_LLEN] = '\0';
126 		ta.ta_slot = builtin->tcb_slot;
127 		ta.ta_offset = builtin->tcb_offset;
128 		ta.ta_addr = tcaddr;
129 		ta.ta_cookie = builtin->tcb_cookie;
130 		ta.ta_busspeed = sc->sc_speed;
131 
132 		/*
133 		 * Mark the slot as used, so we don't check it later.
134 		 */
135 		sc->sc_slots[builtin->tcb_slot].tcs_used = 1;
136 
137 		/*
138 		 * Attach the device.
139 		 */
140 		config_found_sm(self, &ta, tcprint, tcsubmatch);
141 	}
142 
143 	/*
144 	 * Try to configure each unused slot, last to first.
145 	 */
146 	for (i = sc->sc_nslots - 1; i >= 0; i--) {
147 		slot = &sc->sc_slots[i];
148 
149 		/* If already checked above, don't look again now. */
150 		if (slot->tcs_used)
151 			continue;
152 
153 		/*
154 		 * Make sure something is there, and find out what it is.
155 		 */
156 		tcaddr = slot->tcs_addr;
157 		if (tc_badaddr(tcaddr))
158 			continue;
159 		if (tc_checkslot(tcaddr, ta.ta_modname) == 0)
160 			continue;
161 
162 		/*
163 		 * Set up the rest of the attachment information.
164 		 */
165 		ta.ta_memt = tba->tba_memt;
166 		ta.ta_dmat = (*sc->sc_get_dma_tag)(i);
167 		ta.ta_slot = i;
168 		ta.ta_offset = 0;
169 		ta.ta_addr = tcaddr;
170 		ta.ta_cookie = slot->tcs_cookie;
171 
172 		/*
173 		 * Mark the slot as used.
174 		 */
175 		slot->tcs_used = 1;
176 
177 		/*
178 		 * Attach the device.
179 		 */
180 		config_found_sm(self, &ta, tcprint, tcsubmatch);
181 	}
182 }
183 
184 int
185 tcprint(aux, pnp)
186 	void *aux;
187 	const char *pnp;
188 {
189 	struct tc_attach_args *ta = aux;
190 	char devinfo[256];
191 
192 	if (pnp) {
193 		tc_devinfo(ta->ta_modname, devinfo);
194 		printf("%s at %s", devinfo, pnp);
195 	}
196 	printf(" slot %d offset 0x%lx", ta->ta_slot,
197 	    (long)ta->ta_offset);
198 	return (UNCONF);
199 }
200 
201 int
202 tcsubmatch(parent, cf, aux)
203 	struct device *parent;
204 	struct cfdata *cf;
205 	void *aux;
206 {
207 	struct tc_attach_args *d = aux;
208 
209 	if ((cf->tccf_slot != TCCF_SLOT_UNKNOWN) &&
210 	    (cf->tccf_slot != d->ta_slot))
211 		return 0;
212 	if ((cf->tccf_offset != TCCF_SLOT_UNKNOWN) &&
213 	    (cf->tccf_offset != d->ta_offset))
214 		return 0;
215 
216 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
217 }
218 
219 
220 #define	NTC_ROMOFFS	2
221 static tc_offset_t tc_slot_romoffs[NTC_ROMOFFS] = {
222 	TC_SLOT_ROM,
223 	TC_SLOT_PROTOROM,
224 };
225 
226 int
227 tc_checkslot(slotbase, namep)
228 	tc_addr_t slotbase;
229 	char *namep;
230 {
231 	struct tc_rommap *romp;
232 	int i, j;
233 
234 	for (i = 0; i < NTC_ROMOFFS; i++) {
235 		romp = (struct tc_rommap *)
236 		    (slotbase + tc_slot_romoffs[i]);
237 
238 		switch (romp->tcr_width.v) {
239 		case 1:
240 		case 2:
241 		case 4:
242 			break;
243 
244 		default:
245 			continue;
246 		}
247 
248 		if (romp->tcr_stride.v != 4)
249 			continue;
250 
251 		for (j = 0; j < 4; j++)
252 			if (romp->tcr_test[j+0*romp->tcr_stride.v] != 0x55 ||
253 			    romp->tcr_test[j+1*romp->tcr_stride.v] != 0x00 ||
254 			    romp->tcr_test[j+2*romp->tcr_stride.v] != 0xaa ||
255 			    romp->tcr_test[j+3*romp->tcr_stride.v] != 0xff)
256 				continue;
257 
258 		for (j = 0; j < TC_ROM_LLEN; j++)
259 			namep[j] = romp->tcr_modname[j].v;
260 		namep[j] = '\0';
261 		return (1);
262 	}
263 	return (0);
264 }
265 
266 const struct evcnt *
267 tc_intr_evcnt(struct device *dev, void *cookie)
268 {
269 	struct tc_softc *sc = tc_cd.cd_devs[0];
270 
271 	return ((*sc->sc_intr_evcnt)(dev, cookie));
272 }
273 
274 void
275 tc_intr_establish(dev, cookie, level, handler, arg)
276 	struct device *dev;
277 	void *cookie, *arg;
278 	int level;
279 	int (*handler) __P((void *));
280 {
281 	struct tc_softc *sc = tc_cd.cd_devs[0];
282 
283 	(*sc->sc_intr_establish)(dev, cookie, level, 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 = tc_cd.cd_devs[0];
292 
293 	(*sc->sc_intr_disestablish)(dev, 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