dio.c revision 1.16 1 /* $NetBSD: dio.c,v 1.16 2002/03/15 05:55:35 gmcgarry Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Autoconfiguration and mapping support for the DIO bus.
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: dio.c,v 1.16 2002/03/15 05:55:35 gmcgarry Exp $");
45
46 #define _HP300_INTR_H_PRIVATE
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/device.h>
52
53 #include <machine/autoconf.h>
54 #include <machine/cpu.h>
55 #include <machine/hp300spu.h>
56
57 #include <hp300/dev/dmavar.h>
58
59 #include <hp300/dev/dioreg.h>
60 #include <hp300/dev/diovar.h>
61
62 #include <hp300/dev/diodevs.h>
63 #include <hp300/dev/diodevs_data.h>
64
65 extern caddr_t internalhpib;
66
67 int dio_scodesize __P((struct dio_attach_args *));
68 char *dio_devinfo __P((struct dio_attach_args *, char *, size_t));
69
70 int diomatch __P((struct device *, struct cfdata *, void *));
71 void dioattach __P((struct device *, struct device *, void *));
72 int dioprint __P((void *, const char *));
73 int diosubmatch __P((struct device *, struct cfdata *, void *));
74
75 struct cfattach dio_ca = {
76 sizeof(struct device), diomatch, dioattach
77 };
78
79 int
80 diomatch(parent, match, aux)
81 struct device *parent;
82 struct cfdata *match;
83 void *aux;
84 {
85 static int dio_matched = 0;
86
87 /* Allow only one instance. */
88 if (dio_matched)
89 return (0);
90
91 dio_matched = 1;
92 return (1);
93 }
94
95 void
96 dioattach(parent, self, aux)
97 struct device *parent, *self;
98 void *aux;
99 {
100 struct dio_attach_args da;
101 caddr_t pa, va;
102 int scode, scmax, didmap, scodesize;
103
104 scmax = DIO_SCMAX(machineid);
105 printf(": ");
106 dmainit();
107
108 for (scode = 0; scode < scmax; ) {
109 if (DIO_INHOLE(scode)) {
110 scode++;
111 continue;
112 }
113
114 didmap = 0;
115
116 /*
117 * Temporarily map the space corresponding to
118 * the current select code unless:
119 * - this is the internal hpib select code,
120 */
121 pa = dio_scodetopa(scode);
122 if ((scode == 7) && internalhpib)
123 va = internalhpib = (caddr_t)IIOV(pa);
124 else {
125 va = iomap(pa, NBPG);
126 if (va == NULL) {
127 printf("%s: can't map scode %d\n",
128 self->dv_xname, scode);
129 scode++;
130 continue;
131 }
132 didmap = 1;
133 }
134
135 /* Check for hardware. */
136 if (badaddr(va)) {
137 if (didmap)
138 iounmap(va, NBPG);
139 scode++;
140 continue;
141 }
142
143 /* Fill out attach args. */
144 memset(&da, 0, sizeof(da));
145 da.da_bst = HP300_BUS_SPACE_DIO;
146 da.da_scode = scode;
147 /*
148 * Internal HP-IB doesn't always return a device ID,
149 * so we rely on the sysflags.
150 */
151 if ((scode == 7) && internalhpib)
152 da.da_id = DIO_DEVICE_ID_IHPIB;
153 else
154 da.da_id = DIO_ID(va);
155
156 if (DIO_ISFRAMEBUFFER(da.da_id))
157 da.da_secid = DIO_SECID(va);
158
159 da.da_size = DIO_SIZE(scode, va);
160 scodesize = dio_scodesize(&da);
161 if (DIO_ISDIO(scode))
162 da.da_size *= scodesize;
163
164 /* No longer need the device to be mapped. */
165 if (didmap)
166 iounmap(va, NBPG);
167
168 /* Attach matching device. */
169 config_found_sm(self, &da, dioprint, diosubmatch);
170 scode += scodesize;
171 }
172 }
173
174 int
175 diosubmatch(parent, cf, aux)
176 struct device *parent;
177 struct cfdata *cf;
178 void *aux;
179 {
180 struct dio_attach_args *da = aux;
181
182 if (cf->diocf_scode != DIOCF_SCODE_DEFAULT &&
183 cf->diocf_scode != da->da_scode)
184 return (0);
185
186 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
187 }
188
189 int
190 dioprint(aux, pnp)
191 void *aux;
192 const char *pnp;
193 {
194 struct dio_attach_args *da = aux;
195 char buf[128];
196
197 if (pnp)
198 printf("%s at %s", dio_devinfo(da, buf, sizeof(buf)), pnp);
199 printf(" scode %d", da->da_scode);
200 return (UNCONF);
201 }
202
203 /*
204 * Convert a select code to a system physical address.
205 */
206 void *
207 dio_scodetopa(scode)
208 int scode;
209 {
210 u_long rval;
211
212 if (scode == 7 && internalhpib)
213 rval = DIO_IHPIBADDR;
214 else if (DIO_ISDIO(scode))
215 rval = DIO_BASE + (scode * DIO_DEVSIZE);
216 else if (DIO_ISDIOII(scode))
217 rval = DIOII_BASE + ((scode - DIOII_SCBASE) * DIOII_DEVSIZE);
218 else
219 rval = 0;
220
221 return ((void *)rval);
222 }
223
224 /*
225 * Return the select code size for this device, defaulting to 1
226 * if we don't know what kind of device we have.
227 */
228 int
229 dio_scodesize(da)
230 struct dio_attach_args *da;
231 {
232 int i;
233
234 /*
235 * Deal with lame internal HP-IB controllers which don't have
236 * consistent/reliable device ids.
237 */
238 if (da->da_scode == 7 && internalhpib)
239 return (1);
240
241 /*
242 * Find the dio_devdata matchind the primary id.
243 * If we're a framebuffer, we also check the secondary id.
244 */
245 for (i = 0; i < DIO_NDEVICES; i++) {
246 if (da->da_id == dio_devdatas[i].dd_id) {
247 if (DIO_ISFRAMEBUFFER(da->da_id)) {
248 if (da->da_secid == dio_devdatas[i].dd_secid) {
249 goto foundit;
250 }
251 } else {
252 foundit:
253 return (dio_devdatas[i].dd_nscode);
254 }
255 }
256 }
257
258 /*
259 * Device is unknown. Print a warning and assume a default.
260 */
261 printf("WARNING: select code size unknown for id = 0x%x secid = 0x%x\n",
262 da->da_id, da->da_secid);
263 return (1);
264 }
265
266 /*
267 * Return a reasonable description of a DIO device.
268 */
269 char *
270 dio_devinfo(da, buf, buflen)
271 struct dio_attach_args *da;
272 char *buf;
273 size_t buflen;
274 {
275 #ifdef DIOVERBOSE
276 int i;
277 #endif
278
279 memset(buf, 0, buflen);
280
281 /*
282 * Deal with lame internal HP-IB controllers which don't have
283 * consistent/reliable device ids.
284 */
285 if (da->da_scode == 7 && internalhpib) {
286 sprintf(buf, DIO_DEVICE_DESC_IHPIB);
287 return (buf);
288 }
289
290 #ifdef DIOVERBOSE
291 /*
292 * Find the description matching our primary id.
293 * If we're a framebuffer, we also check the secondary id.
294 */
295 for (i = 0; i < DIO_NDEVICES; i++) {
296 if (da->da_id == dio_devdescs[i].dd_id) {
297 if (DIO_ISFRAMEBUFFER(da->da_id)) {
298 if (da->da_secid == dio_devdescs[i].dd_secid) {
299 goto foundit;
300 }
301 } else {
302 foundit:
303 sprintf(buf, "%s", dio_devdescs[i].dd_desc);
304 return (buf);
305 }
306 }
307 }
308 #endif /* DIOVERBOSE */
309
310 /*
311 * Device is unknown. Construct something reasonable.
312 */
313 sprintf(buf, "device id = 0x%x secid = 0x%x",
314 da->da_id, da->da_secid);
315 return (buf);
316 }
317
318 /*
319 * Establish an interrupt handler for a DIO device.
320 */
321 void *
322 dio_intr_establish(func, arg, ipl, priority)
323 int (*func) __P((void *));
324 void *arg;
325 int ipl;
326 int priority;
327 {
328 void *ih;
329
330 ih = intr_establish(func, arg, ipl, priority);
331
332 if (priority == IPL_BIO)
333 dmacomputeipl();
334
335 return (ih);
336 }
337
338 /*
339 * Remove an interrupt handler for a DIO device.
340 */
341 void
342 dio_intr_disestablish(arg)
343 void *arg;
344 {
345 struct hp300_intrhand *ih = arg;
346 int priority = ih->ih_priority;
347
348 intr_disestablish(arg);
349
350 if (priority == IPL_BIO)
351 dmacomputeipl();
352 }
353