tc.c revision 1.14 1 /* $NetBSD: tc.c,v 1.14 1996/04/18 01:12:41 cgd 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/param.h>
31 #include <sys/systm.h>
32 #include <sys/device.h>
33
34 #include <dev/tc/tcreg.h>
35 #include <dev/tc/tcvar.h>
36 #include <dev/tc/tcdevs.h>
37
38 struct tc_softc {
39 struct device sc_dv;
40
41 int sc_speed;
42 int sc_nslots;
43 struct tc_slotdesc *sc_slots;
44
45 void (*sc_intr_establish) __P((struct device *, void *,
46 tc_intrlevel_t, int (*)(void *), void *));
47 void (*sc_intr_disestablish) __P((struct device *, void *));
48 };
49
50 /* Definition of the driver for autoconfig. */
51 int tcmatch __P((struct device *, void *, void *));
52 void tcattach __P((struct device *, struct device *, void *));
53
54 struct cfattach tc_ca = {
55 sizeof(struct tc_softc), tcmatch, tcattach
56 };
57
58 struct cfdriver tc_cd = {
59 NULL, "tc", DV_DULL
60 };
61
62 int tcprint __P((void *, char *));
63 int tcsubmatch __P((struct device *, void *, void *));
64 int tc_checkslot __P((tc_addr_t, char *));
65 void tc_devinfo __P((const char *, char *));
66
67 int
68 tcmatch(parent, cfdata, aux)
69 struct device *parent;
70 void *cfdata;
71 void *aux;
72 {
73 struct cfdata *cf = cfdata;
74 struct tcbus_attach_args *tba = aux;
75
76 if (strcmp(tba->tba_busname, cf->cf_driver->cd_name))
77 return (0);
78
79 return (1);
80 }
81
82 void
83 tcattach(parent, self, aux)
84 struct device *parent;
85 struct device *self;
86 void *aux;
87 {
88 struct tc_softc *sc = (struct tc_softc *)self;
89 struct tcbus_attach_args *tba = aux;
90 struct tc_attach_args ta;
91 const struct tc_builtin *builtin;
92 struct tc_slotdesc *slot;
93 tc_addr_t tcaddr;
94 int i;
95
96 printf("%s MHz clock\n",
97 tba->tba_speed == TC_SPEED_25_MHZ ? "25" : "12.5");
98
99 /*
100 * Save important CPU/chipset information.
101 */
102 sc->sc_speed = tba->tba_speed;
103 sc->sc_nslots = tba->tba_nslots;
104 sc->sc_slots = tba->tba_slots;
105 sc->sc_intr_establish = tba->tba_intr_establish;
106 sc->sc_intr_disestablish = tba->tba_intr_disestablish;
107
108 /*
109 * Try to configure each built-in device
110 */
111 for (i = 0; i < tba->tba_nbuiltins; i++) {
112 builtin = &tba->tba_builtins[i];
113
114 /* sanity check! */
115 if (builtin->tcb_slot > sc->sc_nslots)
116 panic("tcattach: builtin %d slot > nslots", i);
117
118 /*
119 * Make sure device is really there, because some
120 * built-in devices are really optional.
121 */
122 tcaddr = sc->sc_slots[builtin->tcb_slot].tcs_addr +
123 builtin->tcb_offset;
124 if (tc_badaddr(tcaddr))
125 continue;
126
127 /*
128 * Set up the device attachment information.
129 */
130 strncpy(ta.ta_modname, builtin->tcb_modname, TC_ROM_LLEN);
131 ta.ta_modname[TC_ROM_LLEN] = '\0';
132 ta.ta_slot = builtin->tcb_slot;
133 ta.ta_offset = builtin->tcb_offset;
134 ta.ta_addr = tcaddr;
135 ta.ta_cookie = builtin->tcb_cookie;
136 ta.ta_busspeed = sc->sc_speed;
137
138 /*
139 * Mark the slot as used, so we don't check it later.
140 */
141 sc->sc_slots[builtin->tcb_slot].tcs_used = 1;
142
143 /*
144 * Attach the device.
145 */
146 config_found_sm(self, &ta, 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_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 /*
182 * Attach the device.
183 */
184 config_found_sm(self, &ta, tcprint, tcsubmatch);
185 }
186 }
187
188 int
189 tcprint(aux, pnp)
190 void *aux;
191 char *pnp;
192 {
193 struct tc_attach_args *ta = aux;
194 char devinfo[256];
195
196 if (pnp) {
197 tc_devinfo(ta->ta_modname, devinfo);
198 printf("%s at %s", devinfo, pnp);
199 }
200 printf(" slot %d offset 0x%lx", ta->ta_slot,
201 (long)ta->ta_offset);
202 return (UNCONF);
203 }
204
205 int
206 tcsubmatch(parent, match, aux)
207 struct device *parent;
208 void *match, *aux;
209 {
210 struct cfdata *cf = match;
211 struct tc_attach_args *d = aux;
212
213 if ((cf->tccf_slot != TCCF_SLOT_UNKNOWN) &&
214 (cf->tccf_slot != d->ta_slot))
215 return 0;
216 if ((cf->tccf_offset != TCCF_SLOT_UNKNOWN) &&
217 (cf->tccf_offset != d->ta_offset))
218 return 0;
219
220 return ((*cf->cf_attach->ca_match)(parent, match, aux));
221 }
222
223
224 #define NTC_ROMOFFS 2
225 static tc_offset_t tc_slot_romoffs[NTC_ROMOFFS] = {
226 TC_SLOT_ROM,
227 TC_SLOT_PROTOROM,
228 };
229
230 int
231 tc_checkslot(slotbase, namep)
232 tc_addr_t slotbase;
233 char *namep;
234 {
235 struct tc_rommap *romp;
236 int i, j;
237
238 for (i = 0; i < NTC_ROMOFFS; i++) {
239 romp = (struct tc_rommap *)
240 (slotbase + tc_slot_romoffs[i]);
241
242 switch (romp->tcr_width.v) {
243 case 1:
244 case 2:
245 case 4:
246 break;
247
248 default:
249 continue;
250 }
251
252 if (romp->tcr_stride.v != 4)
253 continue;
254
255 for (j = 0; j < 4; j++)
256 if (romp->tcr_test[j+0*romp->tcr_stride.v] != 0x55 ||
257 romp->tcr_test[j+1*romp->tcr_stride.v] != 0x00 ||
258 romp->tcr_test[j+2*romp->tcr_stride.v] != 0xaa ||
259 romp->tcr_test[j+3*romp->tcr_stride.v] != 0xff)
260 continue;
261
262 for (j = 0; j < TC_ROM_LLEN; j++)
263 namep[j] = romp->tcr_modname[j].v;
264 namep[j] = '\0';
265 return (1);
266 }
267 return (0);
268 }
269
270 void
271 tc_intr_establish(dev, cookie, level, handler, arg)
272 struct device *dev;
273 void *cookie, *arg;
274 tc_intrlevel_t level;
275 int (*handler) __P((void *));
276 {
277 struct tc_softc *sc = (struct tc_softc *)dev;
278
279 (*sc->sc_intr_establish)(sc->sc_dv.dv_parent, cookie, level,
280 handler, arg);
281 }
282
283 void
284 tc_intr_disestablish(dev, cookie)
285 struct device *dev;
286 void *cookie;
287 {
288 struct tc_softc *sc = (struct tc_softc *)dev;
289
290 (*sc->sc_intr_disestablish)(sc->sc_dv.dv_parent, cookie);
291 }
292
293 #ifdef TCVERBOSE
294 /*
295 * Descriptions of of known devices.
296 */
297 struct tc_knowndev {
298 const char *id, *driver, *description;
299 };
300
301 #include <dev/tc/tcdevs_data.h>
302 #endif /* TCVERBOSE */
303
304 void
305 tc_devinfo(id, cp)
306 const char *id;
307 char *cp;
308 {
309 const char *driver, *description;
310 #ifdef TCVERBOSE
311 struct tc_knowndev *tdp;
312 int match;
313 const char *unmatched = "unknown ";
314 #else
315 const char *unmatched = "";
316 #endif
317
318 driver = NULL;
319 description = id;
320
321 #ifdef TCVERBOSE
322 /* find the device in the table, if possible. */
323 tdp = tc_knowndevs;
324 while (tdp->id != NULL) {
325 /* check this entry for a match */
326 match = !strcmp(tdp->id, id);
327 if (match) {
328 driver = tdp->driver;
329 description = tdp->description;
330 break;
331 }
332 tdp++;
333 }
334 #endif
335
336 if (driver == NULL)
337 cp += sprintf(cp, "%sdevice %s", unmatched, id);
338 else
339 cp += sprintf(cp, "%s (%s)", driver, description);
340 }
341