xref: /openbsd-src/sys/dev/tc/tc.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: tc.c,v 1.18 2008/08/09 16:42:30 miod 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 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
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
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 	printf(": %s MHz clock\n",
85 	    tba->tba_speed == TC_SPEED_25_MHZ ? "25" : "12.5");
86 
87 	/*
88 	 * Save important CPU/chipset information.
89 	 */
90 	sc->sc_speed = tba->tba_speed;
91 	sc->sc_nslots = tba->tba_nslots;
92 	sc->sc_slots = tba->tba_slots;
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 		sc->sc_slots[builtin->tcb_slot].tcs_used = 1;
133 
134 		/*
135 		 * Attach the device.
136 		 */
137 		config_found_sm(self, &ta, tcprint, tcsubmatch);
138 	}
139 
140 	/*
141 	 * Try to configure each unused slot, last to first.
142 	 */
143 	for (i = sc->sc_nslots - 1; i >= 0; i--) {
144 		slot = &sc->sc_slots[i];
145 
146 		/* If already checked above, don't look again now. */
147 		if (slot->tcs_used)
148 			continue;
149 
150 		/*
151 		 * Make sure something is there, and find out what it is.
152 		 */
153 		tcaddr = slot->tcs_addr;
154 		if (tc_badaddr(tcaddr))
155 			continue;
156 		if (tc_checkslot(tcaddr, ta.ta_modname) == 0)
157 			continue;
158 
159 		/*
160 		 * Set up the rest of the attachment information.
161 		 */
162 		ta.ta_memt = tba->tba_memt;
163 		ta.ta_dmat = (*sc->sc_get_dma_tag)(i);
164 		ta.ta_slot = i;
165 		ta.ta_offset = 0;
166 		ta.ta_addr = tcaddr;
167 		ta.ta_cookie = slot->tcs_cookie;
168 
169 		/*
170 		 * Mark the slot as used.
171 		 */
172 		slot->tcs_used = 1;
173 
174 		/*
175 		 * Attach the device.
176 		 */
177 		config_found_sm(self, &ta, tcprint, tcsubmatch);
178 	}
179 }
180 
181 int
182 tcprint(aux, pnp)
183 	void *aux;
184 	const char *pnp;
185 {
186 	struct tc_attach_args *ta = aux;
187 	char devinfo[256];
188 
189 	if (pnp) {
190 		tc_devinfo(ta->ta_modname, devinfo, sizeof devinfo);
191 		printf("%s at %s", devinfo, pnp);
192 	}
193 	printf(" slot %d offset 0x%lx", ta->ta_slot,
194 	    (long)ta->ta_offset);
195 	return (UNCONF);
196 }
197 
198 int
199 tcsubmatch(parent, vcf, aux)
200 	struct device *parent;
201 	void *vcf, *aux;
202 {
203 	struct tc_attach_args *d = aux;
204 	struct cfdata *cf = vcf;
205 
206 	if ((cf->tccf_slot != TCCF_SLOT_UNKNOWN) &&
207 	    (cf->tccf_slot != d->ta_slot))
208 		return 0;
209 	if ((cf->tccf_offset != TCCF_SLOT_UNKNOWN) &&
210 	    (cf->tccf_offset != d->ta_offset))
211 		return 0;
212 
213 	return ((*cf->cf_attach->ca_match)(parent, cf, aux));
214 }
215 
216 
217 #define	NTC_ROMOFFS	2
218 static tc_offset_t tc_slot_romoffs[NTC_ROMOFFS] = {
219 	TC_SLOT_ROM,
220 	TC_SLOT_PROTOROM,
221 };
222 
223 int
224 tc_checkslot(slotbase, namep)
225 	tc_addr_t slotbase;
226 	char *namep;
227 {
228 	struct tc_rommap *romp;
229 	int i, j;
230 
231 	for (i = 0; i < NTC_ROMOFFS; i++) {
232 		romp = (struct tc_rommap *)
233 		    (slotbase + tc_slot_romoffs[i]);
234 
235 		switch (romp->tcr_width.v) {
236 		case 1:
237 		case 2:
238 		case 4:
239 			break;
240 
241 		default:
242 			continue;
243 		}
244 
245 		if (romp->tcr_stride.v != 4)
246 			continue;
247 
248 		for (j = 0; j < 4; j++)
249 			if (romp->tcr_test[j+0*romp->tcr_stride.v] != 0x55 ||
250 			    romp->tcr_test[j+1*romp->tcr_stride.v] != 0x00 ||
251 			    romp->tcr_test[j+2*romp->tcr_stride.v] != 0xaa ||
252 			    romp->tcr_test[j+3*romp->tcr_stride.v] != 0xff)
253 				continue;
254 
255 		for (j = 0; j < TC_ROM_LLEN; j++)
256 			namep[j] = romp->tcr_modname[j].v;
257 		namep[j] = '\0';
258 		return (1);
259 	}
260 	return (0);
261 }
262 
263 void
264 tc_intr_establish(dev, cookie, level, handler, arg, name)
265 	struct device *dev;
266 	void *cookie, *arg;
267 	int level;
268 	int (*handler)(void *);
269 	const char *name;
270 {
271 	struct tc_softc *sc = tc_cd.cd_devs[0];
272 
273 	(*sc->sc_intr_establish)(dev, cookie, level, handler, arg, name);
274 }
275 
276 void
277 tc_intr_disestablish(dev, cookie)
278 	struct device *dev;
279 	void *cookie;
280 {
281 	struct tc_softc *sc = tc_cd.cd_devs[0];
282 
283 	(*sc->sc_intr_disestablish)(dev, cookie);
284 }
285 
286 #ifdef TCVERBOSE
287 /*
288  * Descriptions of known devices.
289  */
290 struct tc_knowndev {
291 	const char *id, *description;
292 };
293 
294 #include <dev/tc/tcdevs_data.h>
295 #endif /* TCVERBOSE */
296 
297 void
298 tc_devinfo(const char *id, char *cp, size_t cp_len)
299 {
300 #ifdef TCVERBOSE
301 	struct tc_knowndev *tdp;
302 	const char *description;
303 
304 	/* find the device in the table, if possible. */
305 	description = NULL;
306 	for (tdp = tc_knowndevs; tdp->id != NULL; tdp++) {
307 		/* check this entry for a match */
308 		if (strcmp(tdp->id, id) == 0) {
309 			description = tdp->description;
310 			break;
311 		}
312 	}
313 	if (description != NULL)
314 		snprintf(cp, cp_len, "\"%s\" (%s)", id, description);
315 	else
316 #endif
317 		snprintf(cp, cp_len, "\"%s\"", id);
318 }
319