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