xref: /openbsd-src/sys/dev/tc/tc.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: tc.c,v 1.14 2003/04/27 11:22:54 ho 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 #include <dev/tc/tcdevs.h>
38 
39 
40 /* Definition of the driver for autoconfig. */
41 int	tcmatch(struct device *, void *, void *);
42 void	tcattach(struct device *, struct device *, void *);
43 
44 struct cfattach tc_ca = {
45 	sizeof(struct tc_softc), tcmatch, tcattach
46 };
47 
48 struct cfdriver tc_cd = {
49 	NULL, "tc", DV_DULL
50 };
51 
52 int	tcprint(void *, const char *);
53 int	tcsubmatch(struct device *, void *, void *);
54 int	tc_checkslot(tc_addr_t, char *);
55 void	tc_devinfo(const char *, char *, size_t);
56 
57 int
58 tcmatch(parent, vcf, aux)
59 	struct device *parent;
60 	void *vcf, *aux;
61 {
62 	struct tcbus_attach_args *tba = aux;
63 	struct cfdata *cf = vcf;
64 
65 	if (strcmp(tba->tba_busname, cf->cf_driver->cd_name))
66 		return (0);
67 
68 	return (1);
69 }
70 
71 void
72 tcattach(parent, self, aux)
73 	struct device *parent;
74 	struct device *self;
75 	void *aux;
76 {
77 	struct tc_softc *sc = (struct tc_softc *)self;
78 	struct tcbus_attach_args *tba = aux;
79 	struct tc_attach_args ta;
80 	const struct tc_builtin *builtin;
81 	struct tc_slotdesc *slot;
82 	tc_addr_t tcaddr;
83 	int i;
84 
85 	printf(": %s MHz clock\n",
86 	    tba->tba_speed == TC_SPEED_25_MHZ ? "25" : "12.5");
87 
88 	/*
89 	 * Save important CPU/chipset information.
90 	 */
91 	sc->sc_speed = tba->tba_speed;
92 	sc->sc_nslots = tba->tba_nslots;
93 	sc->sc_slots = tba->tba_slots;
94 	sc->sc_intr_evcnt = tba->tba_intr_evcnt;
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
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
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
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 const struct evcnt *
266 tc_intr_evcnt(struct device *dev, void *cookie)
267 {
268 	struct tc_softc *sc = tc_cd.cd_devs[0];
269 
270 	return ((*sc->sc_intr_evcnt)(dev, cookie));
271 }
272 
273 void
274 tc_intr_establish(dev, cookie, level, handler, arg)
275 	struct device *dev;
276 	void *cookie, *arg;
277 	int level;
278 	int (*handler)(void *);
279 {
280 	struct tc_softc *sc = tc_cd.cd_devs[0];
281 
282 	(*sc->sc_intr_establish)(dev, cookie, level, handler, arg);
283 }
284 
285 void
286 tc_intr_disestablish(dev, cookie)
287 	struct device *dev;
288 	void *cookie;
289 {
290 	struct tc_softc *sc = tc_cd.cd_devs[0];
291 
292 	(*sc->sc_intr_disestablish)(dev, cookie);
293 }
294 
295 #ifdef TCVERBOSE
296 /*
297  * Descriptions of of known devices.
298  */
299 struct tc_knowndev {
300 	const char *id, *driver, *description;
301 };
302 
303 #include <dev/tc/tcdevs_data.h>
304 #endif /* TCVERBOSE */
305 
306 void
307 tc_devinfo(const char *id, char *cp, size_t cp_len)
308 {
309 	const char *driver;
310 #ifdef TCVERBOSE
311 	struct tc_knowndev *tdp;
312 	int match;
313 	const char *description;
314 #endif
315 
316 	driver = NULL;
317 
318 #ifdef TCVERBOSE
319 	/* find the device in the table, if possible. */
320 	description = NULL;
321 	tdp = tc_knowndevs;
322 	while (tdp->id != NULL) {
323 		/* check this entry for a match */
324 		match = !strcmp(tdp->id, id);
325 		if (match) {
326 			driver = tdp->driver;
327 			description = tdp->description;
328 			break;
329 		}
330 		tdp++;
331 	}
332 	if (driver != NULL)
333 		snprintf(cp, cp_len, "%s (%s)", driver, description);
334 	else
335 #endif
336 		snprintf(cp, cp_len, "%s", id);
337 }
338