dio.c revision 1.3 1 /* $NetBSD: dio.c,v 1.3 1997/01/30 09:18:37 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1996 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 REGENTS OR CONTRIBUTORS BE
30 * 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/param.h>
44 #include <sys/systm.h>
45 #include <sys/device.h>
46 #include <sys/kernel.h>
47 #include <sys/device.h>
48
49 #include <machine/autoconf.h>
50 #include <machine/cpu.h>
51
52 #include <hp300/dev/dioreg.h>
53 #include <hp300/dev/diovar.h>
54
55 #include <hp300/dev/diodevs.h>
56 #include <hp300/dev/diodevs_data.h>
57
58 extern caddr_t internalhpib;
59
60 int dio_scodesize __P((struct dio_attach_args *));
61 char *dio_devinfo __P((struct dio_attach_args *, char *, size_t));
62
63 int diomatch __P((struct device *, struct cfdata *, void *));
64 void dioattach __P((struct device *, struct device *, void *));
65 int dioprint __P((void *, const char *));
66 int diosubmatch __P((struct device *, struct cfdata *, void *));
67
68 struct cfattach dio_ca = {
69 sizeof(struct device), diomatch, dioattach
70 };
71
72 struct cfdriver dio_cd = {
73 NULL, "dio", DV_DULL
74 };
75
76 int
77 diomatch(parent, match, aux)
78 struct device *parent;
79 struct cfdata *match;
80 void *aux;
81 {
82 static int dio_matched = 0;
83
84 /* Allow only one instance. */
85 if (dio_matched)
86 return (0);
87
88 dio_matched = 1;
89 return (1);
90 }
91
92 void
93 dioattach(parent, self, aux)
94 struct device *parent, *self;
95 void *aux;
96 {
97 struct dio_attach_args da;
98 caddr_t pa, va;
99 int scode, scmax, didmap, scodesize;
100
101 scmax = DIO_SCMAX(machineid);
102 printf("\n");
103
104 for (scode = 0; scode < scmax; ) {
105 if (DIO_INHOLE(scode)) {
106 scode++;
107 continue;
108 }
109
110 didmap = 0;
111
112 /*
113 * Temporarily map the space corresponding to
114 * the current select code unless:
115 * - this is the internal hpib select code,
116 * - this is the console select code.
117 */
118 pa = dio_scodetopa(scode);
119 if (scode == conscode)
120 va = conaddr;
121 else if ((scode == 7) && internalhpib)
122 va = internalhpib = (caddr_t)IIOV(pa);
123 else {
124 va = iomap(pa, NBPG);
125 if (va == NULL) {
126 printf("%s: can't map scode %d\n", scode);
127 scode++;
128 continue;
129 }
130 didmap = 1;
131 }
132
133 /* Check for hardware. */
134 if (badaddr(va)) {
135 if (didmap)
136 iounmap(va, NBPG);
137 scode++;
138 continue;
139 }
140
141 /* Fill out attach args. */
142 bzero(&da, sizeof(da));
143 da.da_scode = scode;
144 da.da_id = DIO_ID(va);
145
146 if (DIO_ISFRAMEBUFFER(da.da_id))
147 da.da_secid = DIO_SECID(va);
148
149 da.da_size = DIO_SIZE(scode, va);
150 scodesize = dio_scodesize(&da);
151 if (DIO_ISDIO(scode))
152 da.da_size *= scodesize;
153
154 /* No longer need the device to be mapped. */
155 if (didmap)
156 iounmap(va, NBPG);
157
158 /* Attach matching device. */
159 config_found_sm(self, &da, dioprint, diosubmatch);
160 scode += scodesize;
161 }
162 }
163
164 int
165 diosubmatch(parent, cf, aux)
166 struct device *parent;
167 struct cfdata *cf;
168 void *aux;
169 {
170 struct dio_attach_args *da = aux;
171
172 if (cf->diocf_scode != DIO_UNKNOWN_SCODE &&
173 cf->diocf_scode != da->da_scode)
174 return (0);
175
176 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
177 }
178
179 int
180 dioprint(aux, pnp)
181 void *aux;
182 const char *pnp;
183 {
184 struct dio_attach_args *da = aux;
185 char buf[128];
186
187 if (pnp)
188 printf("%s at %s", dio_devinfo(da, buf, sizeof(buf)), pnp);
189 printf(" scode %d", da->da_scode);
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 (scode == 7 && internalhpib)
203 rval = DIO_IHPIBADDR;
204 else if (DIO_ISDIO(scode))
205 rval = DIO_BASE + (scode * DIO_DEVSIZE);
206 else if (DIO_ISDIOII(scode))
207 rval = DIOII_BASE + ((scode - DIOII_SCBASE) * DIOII_DEVSIZE);
208 else
209 rval = 0;
210
211 return ((void *)rval);
212 }
213
214 /*
215 * Return the select code size for this device, defaulting to 1
216 * if we don't know what kind of device we have.
217 */
218 int
219 dio_scodesize(da)
220 struct dio_attach_args *da;
221 {
222 int i;
223
224 /*
225 * Deal with lame internal HP-IB controllers which don't have
226 * consistent/reliable device ids.
227 */
228 if (da->da_scode == 7 && internalhpib)
229 return (1);
230
231 /*
232 * Find the dio_devdata matchind the primary id.
233 * If we're a framebuffer, we also check the secondary id.
234 */
235 for (i = 0; i < DIO_NDEVICES; i++) {
236 if (da->da_id == dio_devdatas[i].dd_id) {
237 if (DIO_ISFRAMEBUFFER(da->da_id)) {
238 if (da->da_secid == dio_devdatas[i].dd_secid) {
239 goto foundit;
240 }
241 } else {
242 foundit:
243 return (dio_devdatas[i].dd_nscode);
244 }
245 }
246 }
247
248 /*
249 * Device is unknown. Print a warning and assume a default.
250 */
251 printf("WARNING: select code size unknown for id = 0x%x secid = 0x%x\n",
252 da->da_id, da->da_secid);
253 return (1);
254 }
255
256 /*
257 * Return a reasonable description of a DIO device.
258 */
259 char *
260 dio_devinfo(da, buf, buflen)
261 struct dio_attach_args *da;
262 char *buf;
263 size_t buflen;
264 {
265 int i;
266
267 bzero(buf, buflen);
268
269 /*
270 * Deal with lame internal HP-IB controllers which don't have
271 * consistent/reliable device ids.
272 */
273 if (da->da_scode == 7 && internalhpib) {
274 sprintf(buf, DIO_DEVICE_DESC_IHPIB);
275 return (buf);
276 }
277
278 #ifdef DIOVERBOSE
279 /*
280 * Find the description matching our primary id.
281 * If we're a framebuffer, we also check the secondary id.
282 */
283 for (i = 0; i < DIO_NDEVICES; i++) {
284 if (da->da_id == dio_devdescs[i].dd_id) {
285 if (DIO_ISFRAMEBUFFER(da->da_id)) {
286 if (da->da_secid == dio_devdescs[i].dd_secid) {
287 goto foundit;
288 }
289 } else {
290 foundit:
291 sprintf(buf, "%s", dio_devdescs[i].dd_desc);
292 return (buf);
293 }
294 }
295 }
296 #endif /* DIOVERBOSE */
297
298 /*
299 * Device is unknown. Construct something reasonable.
300 */
301 sprintf(buf, "device id = 0x%x secid = 0x%x",
302 da->da_id, da->da_secid);
303 return (buf);
304 }
305