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