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