dio.c revision 1.23 1 /* $NetBSD: dio.c,v 1.23 2003/05/24 06:21:22 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.23 2003/05/24 06:21:22 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/bus.h>
54
55 #include <uvm/uvm_extern.h>
56
57 #include <machine/autoconf.h>
58 #include <machine/cpu.h>
59 #include <machine/hp300spu.h>
60
61 #include <hp300/dev/dmavar.h>
62
63 #include <hp300/dev/dioreg.h>
64 #include <hp300/dev/diovar.h>
65
66 #include <hp300/dev/diodevs.h>
67 #include <hp300/dev/diodevs_data.h>
68
69 #include "locators.h"
70 #define diocf_scode cf_loc[DIOCF_SCODE]
71
72
73 static int dio_scodesize __P((struct dio_attach_args *));
74 char *dio_devinfo __P((struct dio_attach_args *, char *, size_t));
75
76 int diomatch __P((struct device *, struct cfdata *, void *));
77 void dioattach __P((struct device *, struct device *, void *));
78 int dioprint __P((void *, const char *));
79 int diosubmatch __P((struct device *, struct cfdata *, void *));
80
81 CFATTACH_DECL(dio, sizeof(struct device),
82 diomatch, dioattach, NULL, NULL);
83
84 int
85 diomatch(parent, match, aux)
86 struct device *parent;
87 struct cfdata *match;
88 void *aux;
89 {
90 static int dio_matched = 0;
91
92 /* Allow only one instance. */
93 if (dio_matched)
94 return (0);
95
96 dio_matched = 1;
97 return (1);
98 }
99
100 void
101 dioattach(parent, self, aux)
102 struct device *parent, *self;
103 void *aux;
104 {
105 struct dio_attach_args da;
106 caddr_t pa, va;
107 int scode, scmax, scodesize;
108
109 printf("\n");
110
111 scmax = DIO_SCMAX(machineid);
112
113 for (scode = 0; scode < scmax; ) {
114 if (DIO_INHOLE(scode)) {
115 scode++;
116 continue;
117 }
118
119 /*
120 * Temporarily map the space corresponding to
121 * the current select code unless:
122 */
123 pa = dio_scodetopa(scode);
124 va = iomap(pa, PAGE_SIZE);
125 if (va == NULL) {
126 printf("%s: can't map scode %d\n",
127 self->dv_xname, scode);
128 scode++;
129 continue;
130 }
131
132 /* Check for hardware. */
133 if (badaddr(va)) {
134 iounmap(va, PAGE_SIZE);
135 scode++;
136 continue;
137 }
138
139 /* Fill out attach args. */
140 memset(&da, 0, sizeof(da));
141 da.da_bst = HP300_BUS_SPACE_DIO;
142 da.da_scode = scode;
143
144 da.da_id = DIO_ID(va);
145 if (DIO_ISFRAMEBUFFER(da.da_id))
146 da.da_secid = DIO_SECID(va);
147 da.da_addr = (bus_addr_t)dio_scodetopa(scode);
148 da.da_size = DIO_SIZE(scode, va);
149 scodesize = dio_scodesize(&da);
150 if (DIO_ISDIO(scode))
151 da.da_size *= scodesize;
152 da.da_ipl = DIO_IPL(va);
153
154 /* No longer need the device to be mapped. */
155 iounmap(va, PAGE_SIZE);
156
157 /* Attach matching device. */
158 config_found_sm(self, &da, dioprint, diosubmatch);
159 scode += scodesize;
160 }
161 }
162
163 int
164 diosubmatch(parent, cf, aux)
165 struct device *parent;
166 struct cfdata *cf;
167 void *aux;
168 {
169 struct dio_attach_args *da = aux;
170
171 if (cf->diocf_scode != DIOCF_SCODE_DEFAULT &&
172 cf->diocf_scode != da->da_scode)
173 return (0);
174
175 return (config_match(parent, cf, aux));
176 }
177
178 int
179 dioprint(aux, pnp)
180 void *aux;
181 const char *pnp;
182 {
183 struct dio_attach_args *da = aux;
184 char buf[128];
185
186 if (pnp)
187 aprint_normal("%s at %s",
188 dio_devinfo(da, buf, sizeof(buf)), pnp);
189 aprint_normal(" scode %d ipl %d", da->da_scode, da->da_ipl);
190 return (UNCONF);
191 }
192
193 /*
194 * Convert a select code to a system physical address.
195 */
196 void *
197 dio_scodetopa(scode)
198 int scode;
199 {
200 u_long rval;
201
202 if (DIO_ISDIO(scode))
203 rval = DIO_BASE + (scode * DIO_DEVSIZE);
204 else if (DIO_ISDIOII(scode))
205 rval = DIOII_BASE + ((scode - DIOII_SCBASE) * DIOII_DEVSIZE);
206 else
207 rval = 0;
208
209 return ((void *)rval);
210 }
211
212 /*
213 * Return the select code size for this device, defaulting to 1
214 * if we don't know what kind of device we have.
215 */
216 static int
217 dio_scodesize(da)
218 struct dio_attach_args *da;
219 {
220 int i;
221
222 /*
223 * Find the dio_devdata matchind the primary id.
224 * If we're a framebuffer, we also check the secondary id.
225 */
226 for (i = 0; i < DIO_NDEVICES; i++) {
227 if (da->da_id == dio_devdatas[i].dd_id) {
228 if (DIO_ISFRAMEBUFFER(da->da_id)) {
229 if (da->da_secid == dio_devdatas[i].dd_secid) {
230 goto foundit;
231 }
232 } else {
233 foundit:
234 return (dio_devdatas[i].dd_nscode);
235 }
236 }
237 }
238
239 /*
240 * Device is unknown. Print a warning and assume a default.
241 */
242 printf("WARNING: select code size unknown for id = 0x%x secid = 0x%x\n",
243 da->da_id, da->da_secid);
244 return (1);
245 }
246
247 /*
248 * Return a reasonable description of a DIO device.
249 */
250 char *
251 dio_devinfo(da, buf, buflen)
252 struct dio_attach_args *da;
253 char *buf;
254 size_t buflen;
255 {
256 #ifdef DIOVERBOSE
257 int i;
258 #endif
259
260 memset(buf, 0, buflen);
261
262 #ifdef DIOVERBOSE
263 /*
264 * Find the description matching our primary id.
265 * If we're a framebuffer, we also check the secondary id.
266 */
267 for (i = 0; i < DIO_NDEVICES; i++) {
268 if (da->da_id == dio_devdescs[i].dd_id) {
269 if (DIO_ISFRAMEBUFFER(da->da_id)) {
270 if (da->da_secid == dio_devdescs[i].dd_secid) {
271 goto foundit;
272 }
273 } else {
274 foundit:
275 sprintf(buf, "%s", dio_devdescs[i].dd_desc);
276 return (buf);
277 }
278 }
279 }
280 #endif /* DIOVERBOSE */
281
282 /*
283 * Device is unknown. Construct something reasonable.
284 */
285 sprintf(buf, "device id = 0x%x secid = 0x%x",
286 da->da_id, da->da_secid);
287 return (buf);
288 }
289
290 /*
291 * Establish an interrupt handler for a DIO device.
292 */
293 void *
294 dio_intr_establish(func, arg, ipl, priority)
295 int (*func) __P((void *));
296 void *arg;
297 int ipl;
298 int priority;
299 {
300 void *ih;
301
302 ih = intr_establish(func, arg, ipl, priority);
303
304 if (priority == IPL_BIO)
305 dmacomputeipl();
306
307 return (ih);
308 }
309
310 /*
311 * Remove an interrupt handler for a DIO device.
312 */
313 void
314 dio_intr_disestablish(arg)
315 void *arg;
316 {
317 struct hp300_intrhand *ih = arg;
318 int priority = ih->ih_priority;
319
320 intr_disestablish(arg);
321
322 if (priority == IPL_BIO)
323 dmacomputeipl();
324 }
325