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