ofisa.c revision 1.19 1 /* $NetBSD: ofisa.c,v 1.19 2009/03/14 15:36:19 dsl 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.19 2009/03/14 15:36:19 dsl 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(struct device *, struct cfdata *, void *);
55 static void ofisaattach(struct device *, struct device *, void *);
56
57 CFATTACH_DECL(ofisa, sizeof(struct device),
58 ofisamatch, ofisaattach, NULL, NULL);
59
60 extern struct cfdriver ofisa_cd;
61
62 static int ofisaprint(void *, const char *);
63
64 static int
65 ofisaprint(void *aux, const char *pnp)
66 {
67 struct ofbus_attach_args *oba = aux;
68 char name[64];
69
70 (void)of_packagename(oba->oba_phandle, name, sizeof name);
71 if (pnp)
72 aprint_normal("%s at %s", name, pnp);
73 else
74 aprint_normal(" (%s)", name);
75 return UNCONF;
76 }
77
78 int
79 ofisamatch(struct device *parent, struct cfdata *cf, void *aux)
80 {
81 struct ofbus_attach_args *oba = aux;
82 static const char *const compatible_strings[] = { "pnpPNP,a00", NULL };
83 int rv = 0;
84
85 if (of_compatible(oba->oba_phandle, compatible_strings) != -1)
86 rv = 5;
87
88 #ifdef _OFISA_MD_MATCH
89 if (!rv)
90 rv = ofisa_md_match(parent, cf, aux);
91 #endif
92
93 return (rv);
94 }
95
96 void
97 ofisaattach(parent, self, aux)
98 struct device *parent, *self;
99 void *aux;
100 {
101 struct ofbus_attach_args *oba = aux;
102 struct isabus_attach_args iba;
103 struct ofisa_attach_args aa;
104 int child;
105
106 if (ofisa_get_isabus_data(oba->oba_phandle, &iba) < 0) {
107 printf(": couldn't get essential bus data\n");
108 return;
109 }
110
111 printf("\n");
112
113 #if NISADMA > 0
114 /*
115 * Initialize our DMA state.
116 */
117 isa_dmainit(iba.iba_ic, iba.iba_iot, iba.iba_dmat, self);
118 #endif
119
120 for (child = OF_child(oba->oba_phandle); child;
121 child = OF_peer(child)) {
122 if (ofisa_ignore_child(oba->oba_phandle, child))
123 continue;
124
125 bzero(&aa, sizeof aa);
126
127 aa.oba.oba_busname = "ofw"; /* XXX */
128 aa.oba.oba_phandle = child;
129 aa.iot = iba.iba_iot;
130 aa.memt = iba.iba_memt;
131 aa.dmat = iba.iba_dmat;
132 aa.ic = iba.iba_ic;
133
134 config_found(self, &aa, ofisaprint);
135 }
136 }
137
138 int
139 ofisa_reg_count(int phandle)
140 {
141 int len;
142
143 len = OF_getproplen(phandle, "reg");
144
145 /* nonexistent or obviously malformed "reg" property */
146 if (len < 0 || (len % 12) != 0)
147 return (-1);
148 return (len / 12);
149 }
150
151 int
152 ofisa_reg_get(int phandle, struct ofisa_reg_desc *descp, int ndescs)
153 {
154 char *buf, *bp;
155 int i, proplen, allocated, rv;
156
157 i = ofisa_reg_count(phandle);
158 if (i < 0)
159 return (-1);
160 proplen = i * 12;
161 ndescs = min(ndescs, i);
162
163 i = ndescs * 12;
164 if (i > OFW_MAX_STACK_BUF_SIZE) {
165 buf = malloc(i, M_TEMP, M_WAITOK);
166 allocated = 1;
167 } else {
168 buf = alloca(i);
169 allocated = 0;
170 }
171
172 if (OF_getprop(phandle, "reg", buf, i) != proplen) {
173 rv = -1;
174 goto out;
175 }
176
177 for (i = 0, bp = buf; i < ndescs; i++, bp += 12) {
178 if (of_decode_int(&bp[0]) & 1)
179 descp[i].type = OFISA_REG_TYPE_IO;
180 else
181 descp[i].type = OFISA_REG_TYPE_MEM;
182 descp[i].addr = of_decode_int(&bp[4]);
183 descp[i].len = of_decode_int(&bp[8]);
184 }
185 rv = i; /* number of descriptors processed (== ndescs) */
186
187 out:
188 if (allocated)
189 free(buf, M_TEMP);
190 return (rv);
191 }
192
193 void
194 ofisa_reg_print(struct ofisa_reg_desc *descp, int ndescs)
195 {
196 int i;
197
198 if (ndescs == 0) {
199 printf("none");
200 return;
201 }
202
203 for (i = 0; i < ndescs; i++) {
204 printf("%s%s 0x%lx/%ld", i ? ", " : "",
205 descp[i].type == OFISA_REG_TYPE_IO ? "io" : "mem",
206 (long)descp[i].addr, (long)descp[i].len);
207 }
208 }
209
210 int
211 ofisa_intr_count(int phandle)
212 {
213 int len;
214
215 len = OF_getproplen(phandle, "interrupts");
216
217 /* nonexistent or obviously malformed "reg" property */
218 if (len < 0 || (len % 8) != 0)
219 return (-1);
220 return (len / 8);
221 }
222
223 int
224 ofisa_intr_get(int phandle, struct ofisa_intr_desc *descp, int ndescs)
225 {
226 char *buf, *bp;
227 int i, proplen, allocated, rv;
228
229 i = ofisa_intr_count(phandle);
230 if (i < 0)
231 return (-1);
232 proplen = i * 8;
233 ndescs = min(ndescs, i);
234
235 i = ndescs * 8;
236 if (i > OFW_MAX_STACK_BUF_SIZE) {
237 buf = malloc(i, M_TEMP, M_WAITOK);
238 allocated = 1;
239 } else {
240 buf = alloca(i);
241 allocated = 0;
242 }
243
244 if (OF_getprop(phandle, "interrupts", buf, i) != proplen) {
245 rv = -1;
246 goto out;
247 }
248
249 for (i = 0, bp = buf; i < ndescs; i++, bp += 8) {
250 descp[i].irq = of_decode_int(&bp[0]);
251 switch (of_decode_int(&bp[4])) {
252 case 0:
253 case 1:
254 descp[i].share = IST_LEVEL;
255 break;
256 case 2:
257 case 3:
258 descp[i].share = IST_EDGE;
259 break;
260 #ifdef DIAGNOSTIC
261 default:
262 /* Dunno what to do, so fail. */
263 printf("ofisa_intr_get: unknown interrupt type %d\n",
264 of_decode_int(&bp[4]));
265 rv = -1;
266 goto out;
267 #endif
268 }
269 }
270 rv = i; /* number of descriptors processed (== ndescs) */
271
272 out:
273 if (allocated)
274 free(buf, M_TEMP);
275 return (rv);
276 }
277
278 void
279 ofisa_intr_print(struct ofisa_intr_desc *descp, int ndescs)
280 {
281 int i;
282
283 if (ndescs == 0) {
284 printf("none");
285 return;
286 }
287
288 for (i = 0; i < ndescs; i++) {
289 printf("%s%d (%s)", i ? ", " : "", descp[i].irq,
290 descp[i].share == IST_LEVEL ? "level" : "edge");
291 }
292 }
293
294 int
295 ofisa_dma_count(int phandle)
296 {
297 int len;
298
299 len = OF_getproplen(phandle, "dma");
300
301 /* nonexistent or obviously malformed "reg" property */
302 if (len < 0 || (len % 20) != 0)
303 return (-1);
304 return (len / 20);
305 }
306
307 int
308 ofisa_dma_get(int phandle, struct ofisa_dma_desc *descp, int ndescs)
309 {
310 char *buf, *bp;
311 int i, proplen, allocated, rv;
312
313 i = ofisa_dma_count(phandle);
314 if (i < 0)
315 return (-1);
316 proplen = i * 20;
317 ndescs = min(ndescs, i);
318
319 i = ndescs * 20;
320 if (i > OFW_MAX_STACK_BUF_SIZE) {
321 buf = malloc(i, M_TEMP, M_WAITOK);
322 allocated = 1;
323 } else {
324 buf = alloca(i);
325 allocated = 0;
326 }
327
328 if (OF_getprop(phandle, "dma", buf, i) != proplen) {
329 rv = -1;
330 goto out;
331 }
332
333 for (i = 0, bp = buf; i < ndescs; i++, bp += 20) {
334 descp[i].drq = of_decode_int(&bp[0]);
335 descp[i].mode = of_decode_int(&bp[4]);
336 descp[i].width = of_decode_int(&bp[8]);
337 descp[i].countwidth = of_decode_int(&bp[12]);
338 descp[i].busmaster = of_decode_int(&bp[16]);
339 }
340 rv = i; /* number of descriptors processed (== ndescs) */
341
342 out:
343 if (allocated)
344 free(buf, M_TEMP);
345 return (rv);
346 }
347
348 void
349 ofisa_dma_print(struct ofisa_dma_desc *descp, int ndescs)
350 {
351 char unkmode[16];
352 const char *modestr;
353 int i;
354
355 if (ndescs == 0) {
356 printf("none");
357 return;
358 }
359
360 for (i = 0; i < ndescs; i++) {
361 switch (descp[i].mode) {
362 case OFISA_DMA_MODE_COMPAT:
363 modestr = "compat";
364 break;
365 case OFISA_DMA_MODE_A:
366 modestr = "A";
367 break;
368 case OFISA_DMA_MODE_B:
369 modestr = "B";
370 break;
371 case OFISA_DMA_MODE_F:
372 modestr = "F";
373 break;
374 case OFISA_DMA_MODE_C:
375 modestr = "C";
376 break;
377 default:
378 snprintf(unkmode, sizeof(unkmode), "??? (%d)",
379 descp[i].mode);
380 modestr = unkmode;
381 break;
382 }
383
384 printf("%s%d %s mode %d-bit (%d-bit count)%s", i ? ", " : "",
385 descp[i].drq, modestr, descp[i].width,
386 descp[i].countwidth,
387 descp[i].busmaster ? " busmaster" : "");
388
389 }
390 }
391