tc.c revision 1.57.2.1 1 /* $NetBSD: tc.c,v 1.57.2.1 2021/05/13 00:47:32 thorpej 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.57.2.1 2021/05/13 00:47:32 thorpej 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 <machine/cpu.h> /* for badaddr */
40
41 #include <dev/tc/tcreg.h>
42 #include <dev/tc/tcvar.h>
43 #include <dev/tc/tcdevs.h>
44
45 #include "locators.h"
46
47 /* Definition of the driver for autoconfig. */
48 static int tcmatch(device_t, cfdata_t, void *);
49
50 CFATTACH_DECL_NEW(tc, sizeof(struct tc_softc),
51 tcmatch, tcattach, NULL, NULL);
52
53 extern struct cfdriver tc_cd;
54
55 static int tcprint(void *, const char *);
56 static void tc_devinfo(const char *, char *, size_t);
57
58 static int
59 tcmatch(device_t parent, cfdata_t cf, void *aux)
60 {
61 struct tcbus_attach_args *tba = aux;
62
63 if (strcmp(tba->tba_busname, cf->cf_name))
64 return (0);
65
66 return (1);
67 }
68
69 void
70 tcattach(device_t parent, device_t self, void *aux)
71 {
72 struct tc_softc *sc = device_private(self);
73 struct tcbus_attach_args *tba = aux;
74 struct tc_attach_args ta;
75 const struct tc_builtin *builtin;
76 const struct tc_slotdesc *slot;
77 tc_addr_t tcaddr;
78 int i;
79 int locs[TCCF_NLOCS];
80
81 sc->sc_dev = self;
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_evcnt = tba->tba_intr_evcnt;
93 sc->sc_intr_establish = tba->tba_intr_establish;
94 sc->sc_intr_disestablish = tba->tba_intr_disestablish;
95 sc->sc_get_dma_tag = tba->tba_get_dma_tag;
96
97 /*
98 * Try to configure each built-in device
99 */
100 for (i = 0; i < tba->tba_nbuiltins; i++) {
101 builtin = &tba->tba_builtins[i];
102
103 /* sanity check! */
104 if (builtin->tcb_slot > sc->sc_nslots)
105 panic("tcattach: builtin %d slot > nslots", i);
106
107 /*
108 * Make sure device is really there, because some
109 * built-in devices are really optional.
110 */
111 tcaddr = sc->sc_slots[builtin->tcb_slot].tcs_addr +
112 builtin->tcb_offset;
113 if (tc_badaddr(tcaddr))
114 continue;
115
116 /*
117 * Set up the device attachment information.
118 */
119 strncpy(ta.ta_modname, builtin->tcb_modname, TC_ROM_LLEN);
120 ta.ta_memt = tba->tba_memt;
121 ta.ta_dmat = (*sc->sc_get_dma_tag)(builtin->tcb_slot);
122 ta.ta_modname[TC_ROM_LLEN] = '\0';
123 ta.ta_slot = builtin->tcb_slot;
124 ta.ta_offset = builtin->tcb_offset;
125 ta.ta_addr = tcaddr;
126 ta.ta_cookie = builtin->tcb_cookie;
127 ta.ta_busspeed = sc->sc_speed;
128
129 /*
130 * Mark the slot as used, so we don't check it later.
131 */
132 KASSERT(builtin->tcb_slot >=0 && builtin->tcb_slot <= 31);
133 sc->sc_slots_used |= __BIT(builtin->tcb_slot);
134
135 locs[TCCF_SLOT] = builtin->tcb_slot;
136 locs[TCCF_OFFSET] = builtin->tcb_offset;
137 /*
138 * Attach the device.
139 */
140 config_found(self, &ta, tcprint,
141 CFARG_SUBMATCH, config_stdsubmatch,
142 CFARG_LOCATORS, locs,
143 CFARG_EOL);
144 }
145
146 /*
147 * Try to configure each unused slot, last to first.
148 */
149 for (i = sc->sc_nslots - 1; i >= 0; i--) {
150 slot = &sc->sc_slots[i];
151
152 /* If already checked above, don't look again now. */
153 if (sc->sc_slots_used & __BIT(i))
154 continue;
155
156 /*
157 * Make sure something is there, and find out what it is.
158 */
159 tcaddr = slot->tcs_addr;
160 if (tc_badaddr(tcaddr))
161 continue;
162 if (tc_checkslot(tcaddr, ta.ta_modname, NULL) == 0)
163 continue;
164
165 /*
166 * Set up the rest of the attachment information.
167 */
168 ta.ta_memt = tba->tba_memt;
169 ta.ta_dmat = (*sc->sc_get_dma_tag)(i);
170 ta.ta_slot = i;
171 ta.ta_offset = 0;
172 ta.ta_addr = tcaddr;
173 ta.ta_cookie = slot->tcs_cookie;
174 ta.ta_busspeed = sc->sc_speed;
175
176 /*
177 * Mark the slot as used.
178 */
179 sc->sc_slots_used |= __BIT(i);
180
181 locs[TCCF_SLOT] = i;
182 locs[TCCF_OFFSET] = 0;
183 /*
184 * Attach the device.
185 */
186 config_found(self, &ta, tcprint,
187 CFARG_SUBMATCH, config_stdsubmatch,
188 CFARG_LOCATORS, locs,
189 CFARG_EOL);
190 }
191 }
192
193 static int
194 tcprint(void *aux, 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
208 static const tc_offset_t tc_slot_romoffs[] = {
209 TC_SLOT_ROM,
210 TC_SLOT_PROTOROM,
211 };
212
213 static int
214 tc_check_romp(const struct tc_rommap *romp)
215 {
216
217 switch (romp->tcr_width.v) {
218 case 1:
219 case 2:
220 case 4:
221 break;
222
223 default:
224 return 0;
225 }
226
227 if (romp->tcr_stride.v != 4)
228 return 0;
229
230 for (size_t j = 0; j < romp->tcr_width.v; j++) {
231 if (romp->tcr_test[j + 0 * romp->tcr_stride.v] != 0x55 ||
232 romp->tcr_test[j + 1 * romp->tcr_stride.v] != 0x00 ||
233 romp->tcr_test[j + 2 * romp->tcr_stride.v] != 0xaa ||
234 romp->tcr_test[j + 3 * romp->tcr_stride.v] != 0xff)
235 return 0;
236 }
237 return 1;
238 }
239
240 int
241 tc_checkslot(tc_addr_t slotbase, char *namep, struct tc_rommap **rompp)
242 {
243 struct tc_rommap *romp;
244 int i, j;
245
246 for (i = 0; i < __arraycount(tc_slot_romoffs); i++) {
247 romp = (struct tc_rommap *)
248 (slotbase + tc_slot_romoffs[i]);
249
250 if (!tc_check_romp(romp))
251 continue;
252
253 if (namep != NULL) {
254 for (j = 0; j < TC_ROM_LLEN; j++)
255 namep[j] = romp->tcr_modname[j].v;
256 namep[j] = '\0';
257 }
258 if (rompp != NULL)
259 *rompp = romp;
260 return (1);
261 }
262 return (0);
263 }
264
265 const struct evcnt *
266 tc_intr_evcnt(device_t dev, void *cookie)
267 {
268 struct tc_softc *sc = device_lookup_private(&tc_cd, 0);
269
270 return ((*sc->sc_intr_evcnt)(dev, cookie));
271 }
272
273 void
274 tc_intr_establish(device_t dev, void *cookie, int level,
275 int (*handler)(void *), void *arg)
276 {
277 struct tc_softc *sc = device_lookup_private(&tc_cd, 0);
278
279 (*sc->sc_intr_establish)(dev, cookie, level, handler, arg);
280 }
281
282 void
283 tc_intr_disestablish(device_t dev, void *cookie)
284 {
285 struct tc_softc *sc = device_lookup_private(&tc_cd, 0);
286
287 (*sc->sc_intr_disestablish)(dev, cookie);
288 }
289
290 #ifdef TCVERBOSE
291 /*
292 * Descriptions of of known devices.
293 */
294 struct tc_knowndev {
295 const char *id, *driver, *description;
296 };
297
298 #include <dev/tc/tcdevs_data.h>
299 #endif /* TCVERBOSE */
300
301 static void
302 tc_devinfo(const char *id, char *cp, size_t l)
303 {
304 const char *driver, *description;
305 #ifdef TCVERBOSE
306 const struct tc_knowndev *tdp;
307 int match;
308 const char *unmatched = "unknown ";
309 #else
310 const char *unmatched = "";
311 #endif
312
313 driver = NULL;
314 description = id;
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 driver = tdp->driver;
324 description = tdp->description;
325 break;
326 }
327 tdp++;
328 }
329 #endif
330
331 if (driver == NULL)
332 snprintf(cp, l, "%sdevice %s", unmatched, id);
333 else
334 snprintf(cp, l, "%s (%s)", driver, description);
335 }
336