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