ofisa.c revision 1.3 1 /* $NetBSD: ofisa.c,v 1.3 1998/06/09 00:02:43 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/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/malloc.h>
40 #include <machine/bus.h>
41 #include <machine/intr.h>
42
43 #include <dev/ofw/openfirm.h>
44 #include <dev/isa/isavar.h>
45 #include <dev/ofisa/ofisavar.h>
46
47 #define OFW_MAX_STACK_BUF_SIZE 256
48
49 static int ofisamatch __P((struct device *, struct cfdata *, void *));
50 static void ofisaattach __P((struct device *, struct device *, void *));
51
52 struct cfattach ofisa_ca = {
53 sizeof(struct device), ofisamatch, ofisaattach
54 };
55
56 extern struct cfdriver ofisa_cd;
57
58 static int ofisaprint __P((void *, const char *));
59
60 static int
61 ofisaprint(aux, pnp)
62 void *aux;
63 const char *pnp;
64 {
65 struct ofbus_attach_args *oba = aux;
66 char name[64];
67
68 (void)of_packagename(oba->oba_phandle, name, sizeof name);
69 if (pnp)
70 printf("%s at %s", name, pnp);
71 else
72 printf(" (%s)", name);
73 return UNCONF;
74 }
75
76 int
77 ofisamatch(parent, cf, aux)
78 struct device *parent;
79 struct cfdata *cf;
80 void *aux;
81 {
82 struct ofbus_attach_args *oba = aux;
83 const char *compatible_strings[] = { "pnpPNP,a00", NULL };
84 int rv = 0;
85
86 if (of_compatible(oba->oba_phandle, compatible_strings) != -1)
87 rv = 5;
88
89 #ifdef _OFISA_MD_MATCH
90 if (!rv)
91 rv = ofisa_md_match(parent, cf, aux);
92 #endif
93
94 return (rv);
95 }
96
97 void
98 ofisaattach(parent, dev, aux)
99 struct device *parent, *dev;
100 void *aux;
101 {
102
103 struct ofbus_attach_args *oba = aux;
104 struct isabus_attach_args iba;
105 struct ofisa_attach_args aa;
106 int child;
107
108 if (ofisa_get_isabus_data(oba->oba_phandle, &iba) < 0) {
109 printf(": couldn't get essential bus data\n");
110 return;
111 }
112
113 printf("\n");
114
115 /*
116 * Initialize our DMA state.
117 */
118 isa_dmainit(iba.iba_ic, iba.iba_iot, iba.iba_dmat, self);
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(dev, &aa, ofisaprint);
135 }
136 }
137
138 int
139 ofisa_reg_count(phandle)
140 int phandle;
141 {
142 int len;
143
144 len = OF_getproplen(phandle, "reg");
145
146 /* nonexistant or obviously malformed "reg" property */
147 if (len < 0 || (len % 12) != 0)
148 return (-1);
149 return (len / 12);
150 }
151
152 int
153 ofisa_reg_get(phandle, descp, ndescs)
154 int phandle;
155 struct ofisa_reg_desc *descp;
156 int ndescs;
157 {
158 char *buf, *bp;
159 int i, allocated, rv;
160
161 i = ofisa_reg_count(phandle);
162 if (i < 0)
163 return (-1);
164 ndescs = min(ndescs, i);
165
166 i = ndescs * 12;
167 if (i > OFW_MAX_STACK_BUF_SIZE) {
168 buf = malloc(i, M_TEMP, M_WAITOK);
169 allocated = 1;
170 } else {
171 buf = alloca(i);
172 allocated = 0;
173 }
174
175 if (OF_getprop(phandle, "reg", buf, i) != i) {
176 rv = -1;
177 goto out;
178 }
179
180 for (i = 0, bp = buf; i < ndescs; i++, bp += 12) {
181 if (of_decode_int(&bp[0]) & 1)
182 descp[i].type = OFISA_REG_TYPE_IO;
183 else
184 descp[i].type = OFISA_REG_TYPE_MEM;
185 descp[i].addr = of_decode_int(&bp[4]);
186 descp[i].len = of_decode_int(&bp[8]);
187 }
188 rv = i; /* number of descriptors processed (== ndescs) */
189
190 out:
191 if (allocated)
192 free(buf, M_TEMP);
193 return (rv);
194 }
195
196 void
197 ofisa_reg_print(descp, ndescs)
198 struct ofisa_reg_desc *descp;
199 int ndescs;
200 {
201 int i;
202
203 if (ndescs == 0) {
204 printf("none");
205 return;
206 }
207
208 for (i = 0; i < ndescs; i++) {
209 printf("%s%s 0x%lx/%ld", i ? ", " : "",
210 descp[i].type == OFISA_REG_TYPE_IO ? "io" : "mem",
211 (long)descp[i].addr, (long)descp[i].len);
212 }
213 }
214
215 int
216 ofisa_intr_count(phandle)
217 int phandle;
218 {
219 int len;
220
221 len = OF_getproplen(phandle, "interrupts");
222
223 /* nonexistant or obviously malformed "reg" property */
224 if (len < 0 || (len % 8) != 0)
225 return (-1);
226 return (len / 8);
227 }
228
229 int
230 ofisa_intr_get(phandle, descp, ndescs)
231 int phandle;
232 struct ofisa_intr_desc *descp;
233 int ndescs;
234 {
235 char *buf, *bp;
236 int i, allocated, rv;
237
238 i = ofisa_intr_count(phandle);
239 if (i < 0)
240 return (-1);
241 ndescs = min(ndescs, i);
242
243 i = ndescs * 8;
244 if (i > OFW_MAX_STACK_BUF_SIZE) {
245 buf = malloc(i, M_TEMP, M_WAITOK);
246 allocated = 1;
247 } else {
248 buf = alloca(i);
249 allocated = 0;
250 }
251
252 if (OF_getprop(phandle, "interrupts", buf, i) != i) {
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 intrerrupt 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 (allocated)
282 free(buf, M_TEMP);
283 return (rv);
284 }
285
286 void
287 ofisa_intr_print(descp, ndescs)
288 struct ofisa_intr_desc *descp;
289 int ndescs;
290 {
291 int i;
292
293 if (ndescs == 0) {
294 printf("none");
295 return;
296 }
297
298 for (i = 0; i < ndescs; i++) {
299 printf("%s%d (%s)", i ? ", " : "", descp[i].irq,
300 descp[i].share == IST_LEVEL ? "level" : "edge");
301 }
302 }
303