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