tc.c revision 1.48 1 1.48 tsutsui /* $NetBSD: tc.c,v 1.48 2008/06/11 16:46:11 tsutsui Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1994, 1995 Carnegie-Mellon University.
5 1.1 cgd * All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Author: Chris G. Demetriou
8 1.14 cgd *
9 1.1 cgd * Permission to use, copy, modify and distribute this software and
10 1.1 cgd * its documentation is hereby granted, provided that both the copyright
11 1.1 cgd * notice and this permission notice appear in all copies of the
12 1.1 cgd * software, derivative works or modified versions, and any portions
13 1.1 cgd * thereof, and that both notices appear in supporting documentation.
14 1.14 cgd *
15 1.14 cgd * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 1.14 cgd * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 1.1 cgd * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 1.14 cgd *
19 1.1 cgd * Carnegie Mellon requests users of this software to return to
20 1.1 cgd *
21 1.1 cgd * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 1.1 cgd * School of Computer Science
23 1.1 cgd * Carnegie Mellon University
24 1.1 cgd * Pittsburgh PA 15213-3890
25 1.1 cgd *
26 1.1 cgd * any improvements or extensions that they make and grant Carnegie the
27 1.1 cgd * rights to redistribute these changes.
28 1.1 cgd */
29 1.29 lukem
30 1.29 lukem #include <sys/cdefs.h>
31 1.48 tsutsui __KERNEL_RCSID(0, "$NetBSD: tc.c,v 1.48 2008/06/11 16:46:11 tsutsui Exp $");
32 1.23 enami
33 1.23 enami #include "opt_tcverbose.h"
34 1.1 cgd
35 1.1 cgd #include <sys/param.h>
36 1.13 jonathan #include <sys/systm.h>
37 1.1 cgd #include <sys/device.h>
38 1.1 cgd
39 1.47 matt #include <machine/cpu.h> /* for badaddr */
40 1.47 matt
41 1.1 cgd #include <dev/tc/tcreg.h>
42 1.1 cgd #include <dev/tc/tcvar.h>
43 1.8 cgd #include <dev/tc/tcdevs.h>
44 1.1 cgd
45 1.39 drochner #include "locators.h"
46 1.1 cgd
47 1.1 cgd /* Definition of the driver for autoconfig. */
48 1.45 thorpej static int tcmatch(struct device *, struct cfdata *, void *);
49 1.11 thorpej
50 1.33 thorpej CFATTACH_DECL(tc, sizeof(struct tc_softc),
51 1.34 thorpej tcmatch, tcattach, NULL, NULL);
52 1.33 thorpej
53 1.27 nisimura extern struct cfdriver tc_cd;
54 1.1 cgd
55 1.45 thorpej static int tcprint(void *, const char *);
56 1.45 thorpej static void tc_devinfo(const char *, char *, size_t);
57 1.1 cgd
58 1.45 thorpej static int
59 1.45 thorpej tcmatch(struct device *parent, struct cfdata *cf, void *aux)
60 1.1 cgd {
61 1.2 cgd struct tcbus_attach_args *tba = aux;
62 1.2 cgd
63 1.30 thorpej if (strcmp(tba->tba_busname, cf->cf_name))
64 1.2 cgd return (0);
65 1.2 cgd
66 1.1 cgd return (1);
67 1.1 cgd }
68 1.1 cgd
69 1.1 cgd void
70 1.45 thorpej tcattach(struct device *parent, struct device *self, void *aux)
71 1.1 cgd {
72 1.44 thorpej struct tc_softc *sc = device_private(self);
73 1.2 cgd struct tcbus_attach_args *tba = aux;
74 1.2 cgd struct tc_attach_args ta;
75 1.1 cgd const struct tc_builtin *builtin;
76 1.1 cgd struct tc_slotdesc *slot;
77 1.1 cgd tc_addr_t tcaddr;
78 1.1 cgd int i;
79 1.41 drochner int locs[TCCF_NLOCS];
80 1.1 cgd
81 1.19 christos printf(": %s MHz clock\n",
82 1.3 cgd tba->tba_speed == TC_SPEED_25_MHZ ? "25" : "12.5");
83 1.1 cgd
84 1.1 cgd /*
85 1.1 cgd * Save important CPU/chipset information.
86 1.1 cgd */
87 1.2 cgd sc->sc_speed = tba->tba_speed;
88 1.2 cgd sc->sc_nslots = tba->tba_nslots;
89 1.2 cgd sc->sc_slots = tba->tba_slots;
90 1.28 cgd sc->sc_intr_evcnt = tba->tba_intr_evcnt;
91 1.2 cgd sc->sc_intr_establish = tba->tba_intr_establish;
92 1.2 cgd sc->sc_intr_disestablish = tba->tba_intr_disestablish;
93 1.25 thorpej sc->sc_get_dma_tag = tba->tba_get_dma_tag;
94 1.1 cgd
95 1.1 cgd /*
96 1.1 cgd * Try to configure each built-in device
97 1.1 cgd */
98 1.2 cgd for (i = 0; i < tba->tba_nbuiltins; i++) {
99 1.2 cgd builtin = &tba->tba_builtins[i];
100 1.1 cgd
101 1.1 cgd /* sanity check! */
102 1.1 cgd if (builtin->tcb_slot > sc->sc_nslots)
103 1.1 cgd panic("tcattach: builtin %d slot > nslots", i);
104 1.1 cgd
105 1.1 cgd /*
106 1.1 cgd * Make sure device is really there, because some
107 1.1 cgd * built-in devices are really optional.
108 1.1 cgd */
109 1.1 cgd tcaddr = sc->sc_slots[builtin->tcb_slot].tcs_addr +
110 1.1 cgd builtin->tcb_offset;
111 1.1 cgd if (tc_badaddr(tcaddr))
112 1.1 cgd continue;
113 1.1 cgd
114 1.1 cgd /*
115 1.1 cgd * Set up the device attachment information.
116 1.1 cgd */
117 1.2 cgd strncpy(ta.ta_modname, builtin->tcb_modname, TC_ROM_LLEN);
118 1.20 cgd ta.ta_memt = tba->tba_memt;
119 1.25 thorpej ta.ta_dmat = (*sc->sc_get_dma_tag)(builtin->tcb_slot);
120 1.2 cgd ta.ta_modname[TC_ROM_LLEN] = '\0';
121 1.2 cgd ta.ta_slot = builtin->tcb_slot;
122 1.2 cgd ta.ta_offset = builtin->tcb_offset;
123 1.2 cgd ta.ta_addr = tcaddr;
124 1.2 cgd ta.ta_cookie = builtin->tcb_cookie;
125 1.2 cgd ta.ta_busspeed = sc->sc_speed;
126 1.14 cgd
127 1.1 cgd /*
128 1.1 cgd * Mark the slot as used, so we don't check it later.
129 1.1 cgd */
130 1.1 cgd sc->sc_slots[builtin->tcb_slot].tcs_used = 1;
131 1.1 cgd
132 1.41 drochner locs[TCCF_SLOT] = builtin->tcb_slot;
133 1.41 drochner locs[TCCF_OFFSET] = builtin->tcb_offset;
134 1.1 cgd /*
135 1.7 cgd * Attach the device.
136 1.1 cgd */
137 1.41 drochner config_found_sm_loc(self, "tc", locs, &ta,
138 1.42 drochner tcprint, config_stdsubmatch);
139 1.1 cgd }
140 1.1 cgd
141 1.1 cgd /*
142 1.1 cgd * Try to configure each unused slot, last to first.
143 1.1 cgd */
144 1.1 cgd for (i = sc->sc_nslots - 1; i >= 0; i--) {
145 1.1 cgd slot = &sc->sc_slots[i];
146 1.1 cgd
147 1.1 cgd /* If already checked above, don't look again now. */
148 1.1 cgd if (slot->tcs_used)
149 1.1 cgd continue;
150 1.1 cgd
151 1.1 cgd /*
152 1.1 cgd * Make sure something is there, and find out what it is.
153 1.1 cgd */
154 1.1 cgd tcaddr = slot->tcs_addr;
155 1.1 cgd if (tc_badaddr(tcaddr))
156 1.1 cgd continue;
157 1.2 cgd if (tc_checkslot(tcaddr, ta.ta_modname) == 0)
158 1.1 cgd continue;
159 1.1 cgd
160 1.1 cgd /*
161 1.1 cgd * Set up the rest of the attachment information.
162 1.1 cgd */
163 1.25 thorpej ta.ta_memt = tba->tba_memt;
164 1.25 thorpej ta.ta_dmat = (*sc->sc_get_dma_tag)(i);
165 1.2 cgd ta.ta_slot = i;
166 1.2 cgd ta.ta_offset = 0;
167 1.2 cgd ta.ta_addr = tcaddr;
168 1.2 cgd ta.ta_cookie = slot->tcs_cookie;
169 1.1 cgd
170 1.1 cgd /*
171 1.1 cgd * Mark the slot as used.
172 1.1 cgd */
173 1.1 cgd slot->tcs_used = 1;
174 1.1 cgd
175 1.41 drochner locs[TCCF_SLOT] = i;
176 1.41 drochner locs[TCCF_OFFSET] = 0;
177 1.1 cgd /*
178 1.7 cgd * Attach the device.
179 1.1 cgd */
180 1.41 drochner config_found_sm_loc(self, "tc", locs, &ta,
181 1.42 drochner tcprint, config_stdsubmatch);
182 1.1 cgd }
183 1.1 cgd }
184 1.1 cgd
185 1.45 thorpej static int
186 1.45 thorpej tcprint(void *aux, const char *pnp)
187 1.1 cgd {
188 1.2 cgd struct tc_attach_args *ta = aux;
189 1.8 cgd char devinfo[256];
190 1.1 cgd
191 1.8 cgd if (pnp) {
192 1.37 itojun tc_devinfo(ta->ta_modname, devinfo, sizeof(devinfo));
193 1.35 thorpej aprint_normal("%s at %s", devinfo, pnp);
194 1.8 cgd }
195 1.36 tsutsui aprint_normal(" slot %d offset 0x%x", ta->ta_slot, ta->ta_offset);
196 1.8 cgd return (UNCONF);
197 1.1 cgd }
198 1.1 cgd
199 1.1 cgd
200 1.47 matt static const tc_offset_t tc_slot_romoffs[] = {
201 1.1 cgd TC_SLOT_ROM,
202 1.47 matt #ifndef __vax__
203 1.1 cgd TC_SLOT_PROTOROM,
204 1.47 matt #endif
205 1.1 cgd };
206 1.1 cgd
207 1.1 cgd int
208 1.45 thorpej tc_checkslot(tc_addr_t slotbase, char *namep)
209 1.1 cgd {
210 1.1 cgd struct tc_rommap *romp;
211 1.1 cgd int i, j;
212 1.1 cgd
213 1.47 matt for (i = 0; i < __arraycount(tc_slot_romoffs); i++) {
214 1.1 cgd romp = (struct tc_rommap *)
215 1.1 cgd (slotbase + tc_slot_romoffs[i]);
216 1.1 cgd
217 1.1 cgd switch (romp->tcr_width.v) {
218 1.1 cgd case 1:
219 1.1 cgd case 2:
220 1.1 cgd case 4:
221 1.1 cgd break;
222 1.1 cgd
223 1.1 cgd default:
224 1.1 cgd continue;
225 1.1 cgd }
226 1.1 cgd
227 1.1 cgd if (romp->tcr_stride.v != 4)
228 1.1 cgd continue;
229 1.1 cgd
230 1.1 cgd for (j = 0; j < 4; j++)
231 1.1 cgd if (romp->tcr_test[j+0*romp->tcr_stride.v] != 0x55 ||
232 1.1 cgd romp->tcr_test[j+1*romp->tcr_stride.v] != 0x00 ||
233 1.1 cgd romp->tcr_test[j+2*romp->tcr_stride.v] != 0xaa ||
234 1.1 cgd romp->tcr_test[j+3*romp->tcr_stride.v] != 0xff)
235 1.1 cgd continue;
236 1.1 cgd
237 1.1 cgd for (j = 0; j < TC_ROM_LLEN; j++)
238 1.1 cgd namep[j] = romp->tcr_modname[j].v;
239 1.1 cgd namep[j] = '\0';
240 1.1 cgd return (1);
241 1.1 cgd }
242 1.1 cgd return (0);
243 1.28 cgd }
244 1.28 cgd
245 1.28 cgd const struct evcnt *
246 1.28 cgd tc_intr_evcnt(struct device *dev, void *cookie)
247 1.28 cgd {
248 1.48 tsutsui struct tc_softc *sc = device_lookup_private(&tc_cd, 0);
249 1.28 cgd
250 1.28 cgd return ((*sc->sc_intr_evcnt)(dev, cookie));
251 1.1 cgd }
252 1.1 cgd
253 1.1 cgd void
254 1.45 thorpej tc_intr_establish(struct device *dev, void *cookie, int level,
255 1.45 thorpej int (*handler)(void *), void *arg)
256 1.1 cgd {
257 1.48 tsutsui struct tc_softc *sc = device_lookup_private(&tc_cd, 0);
258 1.1 cgd
259 1.27 nisimura (*sc->sc_intr_establish)(dev, cookie, level, handler, arg);
260 1.1 cgd }
261 1.1 cgd
262 1.1 cgd void
263 1.45 thorpej tc_intr_disestablish(struct device *dev, void *cookie)
264 1.1 cgd {
265 1.48 tsutsui struct tc_softc *sc = device_lookup_private(&tc_cd, 0);
266 1.1 cgd
267 1.27 nisimura (*sc->sc_intr_disestablish)(dev, cookie);
268 1.8 cgd }
269 1.8 cgd
270 1.8 cgd #ifdef TCVERBOSE
271 1.14 cgd /*
272 1.8 cgd * Descriptions of of known devices.
273 1.14 cgd */
274 1.8 cgd struct tc_knowndev {
275 1.10 cgd const char *id, *driver, *description;
276 1.14 cgd };
277 1.8 cgd
278 1.8 cgd #include <dev/tc/tcdevs_data.h>
279 1.8 cgd #endif /* TCVERBOSE */
280 1.8 cgd
281 1.45 thorpej static void
282 1.45 thorpej tc_devinfo(const char *id, char *cp, size_t l)
283 1.8 cgd {
284 1.10 cgd const char *driver, *description;
285 1.8 cgd #ifdef TCVERBOSE
286 1.46 matt const struct tc_knowndev *tdp;
287 1.8 cgd int match;
288 1.9 cgd const char *unmatched = "unknown ";
289 1.8 cgd #else
290 1.8 cgd const char *unmatched = "";
291 1.8 cgd #endif
292 1.8 cgd
293 1.10 cgd driver = NULL;
294 1.10 cgd description = id;
295 1.8 cgd
296 1.8 cgd #ifdef TCVERBOSE
297 1.8 cgd /* find the device in the table, if possible. */
298 1.8 cgd tdp = tc_knowndevs;
299 1.8 cgd while (tdp->id != NULL) {
300 1.8 cgd /* check this entry for a match */
301 1.8 cgd match = !strcmp(tdp->id, id);
302 1.8 cgd if (match) {
303 1.10 cgd driver = tdp->driver;
304 1.10 cgd description = tdp->description;
305 1.8 cgd break;
306 1.8 cgd }
307 1.8 cgd tdp++;
308 1.8 cgd }
309 1.8 cgd #endif
310 1.8 cgd
311 1.10 cgd if (driver == NULL)
312 1.37 itojun snprintf(cp, l, "%sdevice %s", unmatched, id);
313 1.8 cgd else
314 1.37 itojun snprintf(cp, l, "%s (%s)", driver, description);
315 1.1 cgd }
316