1 /* $NetBSD: ofisa.c,v 1.35 2022/01/22 11:49:17 thorpej Exp $ */
2
3 /*
4 * Copyright 1997, 1998
5 * Digital Equipment Corporation. All rights reserved.
6 *
7 * This software is furnished under license and may be used and
8 * copied only in accordance with the following terms and conditions.
9 * Subject to these conditions, you may download, copy, install,
10 * use, modify and distribute this software in source and/or binary
11 * form. No title or ownership is transferred hereby.
12 *
13 * 1) Any source code used, modified or distributed must reproduce
14 * and retain this copyright notice and list of conditions as
15 * they appear in the source file.
16 *
17 * 2) No right is granted to use any trade name, trademark, or logo of
18 * Digital Equipment Corporation. Neither the "Digital Equipment
19 * Corporation" name nor any trademark or logo of Digital Equipment
20 * Corporation may be used to endorse or promote products derived
21 * from this software without the prior written permission of
22 * Digital Equipment Corporation.
23 *
24 * 3) This software is provided "AS-IS" and any express or implied
25 * warranties, including but not limited to, any implied warranties
26 * of merchantability, fitness for a particular purpose, or
27 * non-infringement are disclaimed. In no event shall DIGITAL be
28 * liable for any damages whatsoever, and in particular, DIGITAL
29 * shall not be liable for special, indirect, consequential, or
30 * incidental damages or damages for lost profits, loss of
31 * revenue or loss of use, whether such damages arise in contract,
32 * negligence, tort, under statute, in equity, at law or otherwise,
33 * even if advised of the possibility of such damage.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ofisa.c,v 1.35 2022/01/22 11:49:17 thorpej Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
43 #include <sys/bus.h>
44 #include <sys/intr.h>
45
46 #include <dev/ofw/openfirm.h>
47 #include <dev/isa/isavar.h>
48 #include <dev/ofisa/ofisavar.h>
49
50 #include "isadma.h"
51
52 #define OFW_MAX_STACK_BUF_SIZE 256
53
54 static int ofisamatch(device_t, cfdata_t, void *);
55 static void ofisaattach(device_t, device_t, void *);
56
57 static int ofisa_subclass_match(device_t, cfdata_t, void *);
58
59 CFATTACH_DECL_NEW(ofisa, 0,
60 ofisamatch, ofisaattach, NULL, NULL);
61
62 CFATTACH_DECL_NEW(ofisa_subclass, 0,
63 ofisa_subclass_match, ofisaattach, NULL, NULL);
64
65 extern struct cfdriver ofisa_cd;
66
67 int
ofisaprint(void * aux,const char * pnp)68 ofisaprint(void *aux, const char *pnp)
69 {
70 struct ofbus_attach_args *oba = aux;
71 char name[64];
72
73 (void)of_packagename(oba->oba_phandle, name, sizeof name);
74 if (pnp)
75 aprint_normal("%s at %s", name, pnp);
76 else
77 aprint_normal(" (%s)", name);
78 return UNCONF;
79 }
80
81 static const struct device_compatible_entry compat_data[] = {
82 { .compat = "pnpPNP,a00" },
83 DEVICE_COMPAT_EOL
84 };
85
86 int
ofisamatch(device_t parent,cfdata_t cf,void * aux)87 ofisamatch(device_t parent, cfdata_t cf, void *aux)
88 {
89 struct ofbus_attach_args *oba = aux;
90 int rv;
91
92 rv = of_compatible_match(oba->oba_phandle, compat_data) ? 5 : 0;
93 #ifdef _OFISA_MD_MATCH
94 if (!rv)
95 rv = ofisa_md_match(parent, cf, aux);
96 #endif
97
98 return (rv);
99 }
100
101 int
ofisa_subclass_match(device_t parent,cfdata_t cf,void * aux)102 ofisa_subclass_match(device_t parent, cfdata_t cf, void *aux)
103 {
104 /* We're attaching "ofisa" to something that knows what it's doing. */
105 return 5;
106 }
107
108 void
ofisaattach(device_t parent,device_t self,void * aux)109 ofisaattach(device_t parent, device_t self, void *aux)
110 {
111 struct ofbus_attach_args *oba = aux;
112 struct isabus_attach_args iba;
113 struct ofisa_attach_args aa;
114 int child;
115
116 if (ofisa_get_isabus_data(oba->oba_phandle, &iba) < 0) {
117 printf(": couldn't get essential bus data\n");
118 return;
119 }
120
121 printf("\n");
122
123 #if NISADMA > 0
124 /*
125 * Initialize our DMA state.
126 */
127 isa_dmainit(iba.iba_ic, iba.iba_iot, iba.iba_dmat, self);
128 #endif
129
130 devhandle_t selfh = device_handle(self);
131 for (child = OF_child(oba->oba_phandle); child;
132 child = OF_peer(child)) {
133 if (ofisa_ignore_child(oba->oba_phandle, child))
134 continue;
135
136 memset(&aa, 0, sizeof aa);
137
138 aa.oba.oba_busname = "ofisa";
139 aa.oba.oba_phandle = child;
140 aa.iot = iba.iba_iot;
141 aa.memt = iba.iba_memt;
142 aa.dmat = iba.iba_dmat;
143 aa.ic = iba.iba_ic;
144
145 config_found(self, &aa, ofisaprint,
146 CFARGS(.devhandle = devhandle_from_of(selfh, child)));
147 }
148 }
149
150 int
ofisa_reg_count(int phandle)151 ofisa_reg_count(int phandle)
152 {
153 int len;
154
155 len = OF_getproplen(phandle, "reg");
156
157 /* nonexistent or obviously malformed "reg" property */
158 if (len < 0 || (len % 12) != 0)
159 return (-1);
160 return (len / 12);
161 }
162
163 int
ofisa_reg_get(int phandle,struct ofisa_reg_desc * descp,int ndescs)164 ofisa_reg_get(int phandle, struct ofisa_reg_desc *descp, int ndescs)
165 {
166 char *buf, *bp, small[OFW_MAX_STACK_BUF_SIZE];
167 int i, proplen, rv;
168
169 i = ofisa_reg_count(phandle);
170 if (i < 0)
171 return (-1);
172 proplen = i * 12;
173 ndescs = uimin(ndescs, i);
174
175 i = ndescs * 12;
176 if (i > OFW_MAX_STACK_BUF_SIZE) {
177 buf = malloc(i, M_TEMP, M_WAITOK);
178 } else {
179 buf = small;
180 }
181
182 if (OF_getprop(phandle, "reg", buf, i) != proplen) {
183 rv = -1;
184 goto out;
185 }
186
187 for (i = 0, bp = buf; i < ndescs; i++, bp += 12) {
188 if (of_decode_int(&bp[0]) & 1)
189 descp[i].type = OFISA_REG_TYPE_IO;
190 else
191 descp[i].type = OFISA_REG_TYPE_MEM;
192 descp[i].addr = of_decode_int(&bp[4]);
193 descp[i].len = of_decode_int(&bp[8]);
194 }
195 rv = i; /* number of descriptors processed (== ndescs) */
196
197 out:
198 if (buf != small)
199 free(buf, M_TEMP);
200 return (rv);
201 }
202
203 void
ofisa_reg_print(struct ofisa_reg_desc * descp,int ndescs)204 ofisa_reg_print(struct ofisa_reg_desc *descp, int ndescs)
205 {
206 int i;
207
208 if (ndescs == 0) {
209 printf("none");
210 return;
211 }
212
213 for (i = 0; i < ndescs; i++) {
214 printf("%s%s 0x%lx/%ld", i ? ", " : "",
215 descp[i].type == OFISA_REG_TYPE_IO ? "io" : "mem",
216 (long)descp[i].addr, (long)descp[i].len);
217 }
218 }
219
220 int
ofisa_intr_count(int phandle)221 ofisa_intr_count(int phandle)
222 {
223 int len;
224
225 len = OF_getproplen(phandle, "interrupts");
226
227 /* nonexistent or obviously malformed "reg" property */
228 if (len < 0 || (len % 8) != 0)
229 return (-1);
230 return (len / 8);
231 }
232
233 int
ofisa_intr_get(int phandle,struct ofisa_intr_desc * descp,int ndescs)234 ofisa_intr_get(int phandle, struct ofisa_intr_desc *descp, int ndescs)
235 {
236 char *buf, *bp, small[OFW_MAX_STACK_BUF_SIZE];
237 int i, proplen, rv;
238
239 i = ofisa_intr_count(phandle);
240 if (i < 0)
241 return (-1);
242 proplen = i * 8;
243 ndescs = uimin(ndescs, i);
244
245 i = ndescs * 8;
246 if (i > OFW_MAX_STACK_BUF_SIZE) {
247 buf = malloc(i, M_TEMP, M_WAITOK);
248 } else {
249 buf = small;
250 }
251
252 if (OF_getprop(phandle, "interrupts", buf, i) != proplen) {
253 rv = -1;
254 goto out;
255 }
256
257 for (i = 0, bp = buf; i < ndescs; i++, bp += 8) {
258 descp[i].irq = of_decode_int(&bp[0]);
259 switch (of_decode_int(&bp[4])) {
260 case 0:
261 case 1:
262 descp[i].share = IST_LEVEL;
263 break;
264 case 2:
265 case 3:
266 descp[i].share = IST_EDGE;
267 break;
268 #ifdef DIAGNOSTIC
269 default:
270 /* Dunno what to do, so fail. */
271 printf("ofisa_intr_get: unknown interrupt type %d\n",
272 of_decode_int(&bp[4]));
273 rv = -1;
274 goto out;
275 #endif
276 }
277 }
278 rv = i; /* number of descriptors processed (== ndescs) */
279
280 out:
281 if (buf != small)
282 free(buf, M_TEMP);
283 return (rv);
284 }
285
286 void
ofisa_intr_print(struct ofisa_intr_desc * descp,int ndescs)287 ofisa_intr_print(struct ofisa_intr_desc *descp, int ndescs)
288 {
289 int i;
290
291 if (ndescs == 0) {
292 printf("none");
293 return;
294 }
295
296 for (i = 0; i < ndescs; i++) {
297 printf("%s%d (%s)", i ? ", " : "", descp[i].irq,
298 descp[i].share == IST_LEVEL ? "level" : "edge");
299 }
300 }
301
302 int
ofisa_dma_count(int phandle)303 ofisa_dma_count(int phandle)
304 {
305 int len;
306
307 len = OF_getproplen(phandle, "dma");
308
309 /* nonexistent or obviously malformed "reg" property */
310 if (len < 0 || (len % 20) != 0)
311 return (-1);
312 return (len / 20);
313 }
314
315 int
ofisa_dma_get(int phandle,struct ofisa_dma_desc * descp,int ndescs)316 ofisa_dma_get(int phandle, struct ofisa_dma_desc *descp, int ndescs)
317 {
318 char *buf, *bp, small[OFW_MAX_STACK_BUF_SIZE];
319 int i, proplen, rv;
320
321 i = ofisa_dma_count(phandle);
322 if (i < 0)
323 return (-1);
324 proplen = i * 20;
325 ndescs = uimin(ndescs, i);
326
327 i = ndescs * 20;
328 if (i > OFW_MAX_STACK_BUF_SIZE) {
329 buf = malloc(i, M_TEMP, M_WAITOK);
330 } else {
331 buf = small;
332 }
333
334 if (OF_getprop(phandle, "dma", buf, i) != proplen) {
335 rv = -1;
336 goto out;
337 }
338
339 for (i = 0, bp = buf; i < ndescs; i++, bp += 20) {
340 descp[i].drq = of_decode_int(&bp[0]);
341 descp[i].mode = of_decode_int(&bp[4]);
342 descp[i].width = of_decode_int(&bp[8]);
343 descp[i].countwidth = of_decode_int(&bp[12]);
344 descp[i].busmaster = of_decode_int(&bp[16]);
345 }
346 rv = i; /* number of descriptors processed (== ndescs) */
347
348 out:
349 if (buf != small)
350 free(buf, M_TEMP);
351 return (rv);
352 }
353
354 void
ofisa_dma_print(struct ofisa_dma_desc * descp,int ndescs)355 ofisa_dma_print(struct ofisa_dma_desc *descp, int ndescs)
356 {
357 char unkmode[16];
358 const char *modestr;
359 int i;
360
361 if (ndescs == 0) {
362 printf("none");
363 return;
364 }
365
366 for (i = 0; i < ndescs; i++) {
367 switch (descp[i].mode) {
368 case OFISA_DMA_MODE_COMPAT:
369 modestr = "compat";
370 break;
371 case OFISA_DMA_MODE_A:
372 modestr = "A";
373 break;
374 case OFISA_DMA_MODE_B:
375 modestr = "B";
376 break;
377 case OFISA_DMA_MODE_F:
378 modestr = "F";
379 break;
380 case OFISA_DMA_MODE_C:
381 modestr = "C";
382 break;
383 default:
384 snprintf(unkmode, sizeof(unkmode), "??? (%d)",
385 descp[i].mode);
386 modestr = unkmode;
387 break;
388 }
389
390 printf("%s%d %s mode %d-bit (%d-bit count)%s", i ? ", " : "",
391 descp[i].drq, modestr, descp[i].width,
392 descp[i].countwidth,
393 descp[i].busmaster ? " busmaster" : "");
394
395 }
396 }
397
398 void
ofisa_print_model(device_t self,int phandle)399 ofisa_print_model(device_t self, int phandle)
400 {
401 char *model, small[OFW_MAX_STACK_BUF_SIZE];
402 int n = OF_getproplen(phandle, "model");
403
404 if (n <= 0)
405 return;
406
407 if (n > OFW_MAX_STACK_BUF_SIZE) {
408 model = malloc(n, M_TEMP, M_WAITOK);
409 } else {
410 model = small;
411 }
412
413 if (OF_getprop(phandle, "model", model, n) != n)
414 goto out;
415
416 aprint_normal(": %s\n", model);
417 if (self)
418 aprint_normal_dev(self, "");
419 out:
420 if (model != small)
421 free(model, M_TEMP);
422 }
423