1 /* $NetBSD: ofisa.c,v 1.22 2009/05/12 13:17:15 cegger Exp $ */
5 * Digital Equipment Corporation. All rights reserved.
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.
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.
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.
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.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ofisa.c,v 1.22 2009/05/12 13:17:15 cegger Exp $");
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/device.h>
42 #include <sys/malloc.h>
46 #include <dev/ofw/openfirm.h>
47 #include <dev/isa/isavar.h>
48 #include <dev/ofisa/ofisavar.h>
52 #define OFW_MAX_STACK_BUF_SIZE 256
54 static int ofisamatch(device_t
, cfdata_t
, void *);
55 static void ofisaattach(device_t
, device_t
, void *);
57 CFATTACH_DECL(ofisa
, sizeof(struct device
),
58 ofisamatch
, ofisaattach
, NULL
, NULL
);
60 extern struct cfdriver ofisa_cd
;
62 static int ofisaprint(void *, const char *);
65 ofisaprint(void *aux
, const char *pnp
)
67 struct ofbus_attach_args
*oba
= aux
;
70 (void)of_packagename(oba
->oba_phandle
, name
, sizeof name
);
72 aprint_normal("%s at %s", name
, pnp
);
74 aprint_normal(" (%s)", name
);
79 ofisamatch(device_t parent
, cfdata_t cf
, void *aux
)
81 struct ofbus_attach_args
*oba
= aux
;
82 static const char *const compatible_strings
[] = { "pnpPNP,a00", NULL
};
85 if (of_compatible(oba
->oba_phandle
, compatible_strings
) != -1)
88 #ifdef _OFISA_MD_MATCH
90 rv
= ofisa_md_match(parent
, cf
, aux
);
97 ofisaattach(device_t parent
, device_t self
, void *aux
)
99 struct ofbus_attach_args
*oba
= aux
;
100 struct isabus_attach_args iba
;
101 struct ofisa_attach_args aa
;
104 if (ofisa_get_isabus_data(oba
->oba_phandle
, &iba
) < 0) {
105 printf(": couldn't get essential bus data\n");
113 * Initialize our DMA state.
115 isa_dmainit(iba
.iba_ic
, iba
.iba_iot
, iba
.iba_dmat
, self
);
118 for (child
= OF_child(oba
->oba_phandle
); child
;
119 child
= OF_peer(child
)) {
120 if (ofisa_ignore_child(oba
->oba_phandle
, child
))
123 memset(&aa
, 0, sizeof aa
);
125 aa
.oba
.oba_busname
= "ofw"; /* XXX */
126 aa
.oba
.oba_phandle
= child
;
127 aa
.iot
= iba
.iba_iot
;
128 aa
.memt
= iba
.iba_memt
;
129 aa
.dmat
= iba
.iba_dmat
;
132 config_found(self
, &aa
, ofisaprint
);
137 ofisa_reg_count(int phandle
)
141 len
= OF_getproplen(phandle
, "reg");
143 /* nonexistent or obviously malformed "reg" property */
144 if (len
< 0 || (len
% 12) != 0)
150 ofisa_reg_get(int phandle
, struct ofisa_reg_desc
*descp
, int ndescs
)
153 int i
, proplen
, allocated
, rv
;
155 i
= ofisa_reg_count(phandle
);
159 ndescs
= min(ndescs
, i
);
162 if (i
> OFW_MAX_STACK_BUF_SIZE
) {
163 buf
= malloc(i
, M_TEMP
, M_WAITOK
);
170 if (OF_getprop(phandle
, "reg", buf
, i
) != proplen
) {
175 for (i
= 0, bp
= buf
; i
< ndescs
; i
++, bp
+= 12) {
176 if (of_decode_int(&bp
[0]) & 1)
177 descp
[i
].type
= OFISA_REG_TYPE_IO
;
179 descp
[i
].type
= OFISA_REG_TYPE_MEM
;
180 descp
[i
].addr
= of_decode_int(&bp
[4]);
181 descp
[i
].len
= of_decode_int(&bp
[8]);
183 rv
= i
; /* number of descriptors processed (== ndescs) */
192 ofisa_reg_print(struct ofisa_reg_desc
*descp
, int ndescs
)
201 for (i
= 0; i
< ndescs
; i
++) {
202 printf("%s%s 0x%lx/%ld", i
? ", " : "",
203 descp
[i
].type
== OFISA_REG_TYPE_IO
? "io" : "mem",
204 (long)descp
[i
].addr
, (long)descp
[i
].len
);
209 ofisa_intr_count(int phandle
)
213 len
= OF_getproplen(phandle
, "interrupts");
215 /* nonexistent or obviously malformed "reg" property */
216 if (len
< 0 || (len
% 8) != 0)
222 ofisa_intr_get(int phandle
, struct ofisa_intr_desc
*descp
, int ndescs
)
225 int i
, proplen
, allocated
, rv
;
227 i
= ofisa_intr_count(phandle
);
231 ndescs
= min(ndescs
, i
);
234 if (i
> OFW_MAX_STACK_BUF_SIZE
) {
235 buf
= malloc(i
, M_TEMP
, M_WAITOK
);
242 if (OF_getprop(phandle
, "interrupts", buf
, i
) != proplen
) {
247 for (i
= 0, bp
= buf
; i
< ndescs
; i
++, bp
+= 8) {
248 descp
[i
].irq
= of_decode_int(&bp
[0]);
249 switch (of_decode_int(&bp
[4])) {
252 descp
[i
].share
= IST_LEVEL
;
256 descp
[i
].share
= IST_EDGE
;
260 /* Dunno what to do, so fail. */
261 printf("ofisa_intr_get: unknown interrupt type %d\n",
262 of_decode_int(&bp
[4]));
268 rv
= i
; /* number of descriptors processed (== ndescs) */
277 ofisa_intr_print(struct ofisa_intr_desc
*descp
, int ndescs
)
286 for (i
= 0; i
< ndescs
; i
++) {
287 printf("%s%d (%s)", i
? ", " : "", descp
[i
].irq
,
288 descp
[i
].share
== IST_LEVEL
? "level" : "edge");
293 ofisa_dma_count(int phandle
)
297 len
= OF_getproplen(phandle
, "dma");
299 /* nonexistent or obviously malformed "reg" property */
300 if (len
< 0 || (len
% 20) != 0)
306 ofisa_dma_get(int phandle
, struct ofisa_dma_desc
*descp
, int ndescs
)
309 int i
, proplen
, allocated
, rv
;
311 i
= ofisa_dma_count(phandle
);
315 ndescs
= min(ndescs
, i
);
318 if (i
> OFW_MAX_STACK_BUF_SIZE
) {
319 buf
= malloc(i
, M_TEMP
, M_WAITOK
);
326 if (OF_getprop(phandle
, "dma", buf
, i
) != proplen
) {
331 for (i
= 0, bp
= buf
; i
< ndescs
; i
++, bp
+= 20) {
332 descp
[i
].drq
= of_decode_int(&bp
[0]);
333 descp
[i
].mode
= of_decode_int(&bp
[4]);
334 descp
[i
].width
= of_decode_int(&bp
[8]);
335 descp
[i
].countwidth
= of_decode_int(&bp
[12]);
336 descp
[i
].busmaster
= of_decode_int(&bp
[16]);
338 rv
= i
; /* number of descriptors processed (== ndescs) */
347 ofisa_dma_print(struct ofisa_dma_desc
*descp
, int ndescs
)
358 for (i
= 0; i
< ndescs
; i
++) {
359 switch (descp
[i
].mode
) {
360 case OFISA_DMA_MODE_COMPAT
:
363 case OFISA_DMA_MODE_A
:
366 case OFISA_DMA_MODE_B
:
369 case OFISA_DMA_MODE_F
:
372 case OFISA_DMA_MODE_C
:
376 snprintf(unkmode
, sizeof(unkmode
), "??? (%d)",
382 printf("%s%d %s mode %d-bit (%d-bit count)%s", i
? ", " : "",
383 descp
[i
].drq
, modestr
, descp
[i
].width
,
385 descp
[i
].busmaster
? " busmaster" : "");