tc.c revision 1.9 1 /* $NetBSD: tc.c,v 1.9 1996/03/02 02:44:29 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/device.h>
32
33 #include <dev/tc/tcreg.h>
34 #include <dev/tc/tcvar.h>
35 #include <dev/tc/tcdevs.h>
36
37 struct tc_softc {
38 struct device sc_dv;
39
40 int sc_speed;
41 int sc_nslots;
42 struct tc_slotdesc *sc_slots;
43
44 void (*sc_intr_establish) __P((struct device *, void *,
45 tc_intrlevel_t, int (*)(void *), void *));
46 void (*sc_intr_disestablish) __P((struct device *, void *));
47 };
48
49 /* Definition of the driver for autoconfig. */
50 int tcmatch __P((struct device *, void *, void *));
51 void tcattach __P((struct device *, struct device *, void *));
52 struct cfdriver tccd =
53 { NULL, "tc", tcmatch, tcattach, DV_DULL, sizeof (struct tc_softc) };
54
55 int tcprint __P((void *, char *));
56 int tcsubmatch __P((struct device *, void *, void *));
57 int tc_checkslot __P((tc_addr_t, char *));
58 void tc_devinfo __P((const char *, char *));
59
60 int
61 tcmatch(parent, cfdata, aux)
62 struct device *parent;
63 void *cfdata;
64 void *aux;
65 {
66 struct cfdata *cf = cfdata;
67 struct tcbus_attach_args *tba = aux;
68
69 if (strcmp(tba->tba_busname, cf->cf_driver->cd_name))
70 return (0);
71
72 /* XXX check other indicators */
73
74 return (1);
75 }
76
77 void
78 tcattach(parent, self, aux)
79 struct device *parent;
80 struct device *self;
81 void *aux;
82 {
83 struct tc_softc *sc = (struct tc_softc *)self;
84 struct tcbus_attach_args *tba = aux;
85 struct tc_attach_args ta;
86 const struct tc_builtin *builtin;
87 struct tc_slotdesc *slot;
88 tc_addr_t tcaddr;
89 void *match;
90 int i;
91
92 printf("%s MHz clock\n",
93 tba->tba_speed == TC_SPEED_25_MHZ ? "25" : "12.5");
94
95 /*
96 * Save important CPU/chipset information.
97 */
98 sc->sc_speed = tba->tba_speed;
99 sc->sc_nslots = tba->tba_nslots;
100 sc->sc_slots = tba->tba_slots;
101 sc->sc_intr_establish = tba->tba_intr_establish;
102 sc->sc_intr_disestablish = tba->tba_intr_disestablish;
103
104 /*
105 * Try to configure each built-in device
106 */
107 for (i = 0; i < tba->tba_nbuiltins; i++) {
108 builtin = &tba->tba_builtins[i];
109
110 /* sanity check! */
111 if (builtin->tcb_slot > sc->sc_nslots)
112 panic("tcattach: builtin %d slot > nslots", i);
113
114 /*
115 * Make sure device is really there, because some
116 * built-in devices are really optional.
117 */
118 tcaddr = sc->sc_slots[builtin->tcb_slot].tcs_addr +
119 builtin->tcb_offset;
120 if (tc_badaddr(tcaddr))
121 continue;
122
123 /*
124 * Set up the device attachment information.
125 */
126 strncpy(ta.ta_modname, builtin->tcb_modname, TC_ROM_LLEN);
127 ta.ta_modname[TC_ROM_LLEN] = '\0';
128 ta.ta_slot = builtin->tcb_slot;
129 ta.ta_offset = builtin->tcb_offset;
130 ta.ta_addr = tcaddr;
131 ta.ta_cookie = builtin->tcb_cookie;
132 ta.ta_busspeed = sc->sc_speed;
133
134 /*
135 * Mark the slot as used, so we don't check it later.
136 */
137 sc->sc_slots[builtin->tcb_slot].tcs_used = 1;
138
139 /*
140 * Attach the device.
141 */
142 config_found_sm(self, &ta, tcprint, tcsubmatch);
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_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 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, match, aux)
203 struct device *parent;
204 void *match, *aux;
205 {
206 struct cfdata *cf = match;
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_driver->cd_match)(parent, match, 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 void
267 tc_intr_establish(dev, cookie, level, handler, arg)
268 struct device *dev;
269 void *cookie, *arg;
270 tc_intrlevel_t level;
271 int (*handler) __P((void *));
272 {
273 struct tc_softc *sc = (struct tc_softc *)dev;
274
275 (*sc->sc_intr_establish)(sc->sc_dv.dv_parent, cookie, level,
276 handler, arg);
277 }
278
279 void
280 tc_intr_disestablish(dev, cookie)
281 struct device *dev;
282 void *cookie;
283 {
284 struct tc_softc *sc = (struct tc_softc *)dev;
285
286 (*sc->sc_intr_disestablish)(sc->sc_dv.dv_parent, cookie);
287 }
288
289 #ifdef TCVERBOSE
290 /*
291 * Descriptions of of known devices.
292 */
293 struct tc_knowndev {
294 const char *id, *name;
295 };
296
297 #include <dev/tc/tcdevs_data.h>
298 #endif /* TCVERBOSE */
299
300 void
301 tc_devinfo(id, cp)
302 const char *id;
303 char *cp;
304 {
305 const char *name;
306 #ifdef TCVERBOSE
307 struct tc_knowndev *tdp;
308 int match;
309 const char *unmatched = "unknown ";
310 #else
311 const char *unmatched = "";
312 #endif
313
314 name = NULL;
315
316 #ifdef TCVERBOSE
317 /* find the device in the table, if possible. */
318 tdp = tc_knowndevs;
319 while (tdp->id != NULL) {
320 /* check this entry for a match */
321 match = !strcmp(tdp->id, id);
322 if (match) {
323 name = tdp->name;
324 break;
325 }
326 tdp++;
327 }
328 #endif
329
330 if (name == NULL)
331 cp += sprintf(cp, "%sdevice %s", unmatched, id);
332 else
333 cp += sprintf(cp, "%s (%s)", name, id);
334 }
335