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