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