ofisa.c revision 1.4 1 /* $NetBSD: ofisa.c,v 1.4 1998/06/10 16:48:21 tv 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, self, aux)
99 struct device *parent, *self;
100 void *aux;
101 {
102 struct ofbus_attach_args *oba = aux;
103 struct isabus_attach_args iba;
104 struct ofisa_attach_args aa;
105 int child;
106
107 if (ofisa_get_isabus_data(oba->oba_phandle, &iba) < 0) {
108 printf(": couldn't get essential bus data\n");
109 return;
110 }
111
112 printf("\n");
113
114 /*
115 * Initialize our DMA state.
116 */
117 isa_dmainit(iba.iba_ic, iba.iba_iot, iba.iba_dmat, self);
118
119 for (child = OF_child(oba->oba_phandle); child;
120 child = OF_peer(child)) {
121 if (ofisa_ignore_child(oba->oba_phandle, child))
122 continue;
123
124 bzero(&aa, sizeof aa);
125
126 aa.oba.oba_busname = "ofw"; /* XXX */
127 aa.oba.oba_phandle = child;
128 aa.iot = iba.iba_iot;
129 aa.memt = iba.iba_memt;
130 aa.dmat = iba.iba_dmat;
131 aa.ic = iba.iba_ic;
132
133 config_found(self, &aa, ofisaprint);
134 }
135 }
136
137 int
138 ofisa_reg_count(phandle)
139 int phandle;
140 {
141 int len;
142
143 len = OF_getproplen(phandle, "reg");
144
145 /* nonexistant 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(phandle, descp, ndescs)
153 int phandle;
154 struct ofisa_reg_desc *descp;
155 int ndescs;
156 {
157 char *buf, *bp;
158 int i, allocated, rv;
159
160 i = ofisa_reg_count(phandle);
161 if (i < 0)
162 return (-1);
163 ndescs = min(ndescs, i);
164
165 i = ndescs * 12;
166 if (i > OFW_MAX_STACK_BUF_SIZE) {
167 buf = malloc(i, M_TEMP, M_WAITOK);
168 allocated = 1;
169 } else {
170 buf = alloca(i);
171 allocated = 0;
172 }
173
174 if (OF_getprop(phandle, "reg", buf, i) != i) {
175 rv = -1;
176 goto out;
177 }
178
179 for (i = 0, bp = buf; i < ndescs; i++, bp += 12) {
180 if (of_decode_int(&bp[0]) & 1)
181 descp[i].type = OFISA_REG_TYPE_IO;
182 else
183 descp[i].type = OFISA_REG_TYPE_MEM;
184 descp[i].addr = of_decode_int(&bp[4]);
185 descp[i].len = of_decode_int(&bp[8]);
186 }
187 rv = i; /* number of descriptors processed (== ndescs) */
188
189 out:
190 if (allocated)
191 free(buf, M_TEMP);
192 return (rv);
193 }
194
195 void
196 ofisa_reg_print(descp, ndescs)
197 struct ofisa_reg_desc *descp;
198 int ndescs;
199 {
200 int i;
201
202 if (ndescs == 0) {
203 printf("none");
204 return;
205 }
206
207 for (i = 0; i < ndescs; i++) {
208 printf("%s%s 0x%lx/%ld", i ? ", " : "",
209 descp[i].type == OFISA_REG_TYPE_IO ? "io" : "mem",
210 (long)descp[i].addr, (long)descp[i].len);
211 }
212 }
213
214 int
215 ofisa_intr_count(phandle)
216 int phandle;
217 {
218 int len;
219
220 len = OF_getproplen(phandle, "interrupts");
221
222 /* nonexistant or obviously malformed "reg" property */
223 if (len < 0 || (len % 8) != 0)
224 return (-1);
225 return (len / 8);
226 }
227
228 int
229 ofisa_intr_get(phandle, descp, ndescs)
230 int phandle;
231 struct ofisa_intr_desc *descp;
232 int ndescs;
233 {
234 char *buf, *bp;
235 int i, allocated, rv;
236
237 i = ofisa_intr_count(phandle);
238 if (i < 0)
239 return (-1);
240 ndescs = min(ndescs, i);
241
242 i = ndescs * 8;
243 if (i > OFW_MAX_STACK_BUF_SIZE) {
244 buf = malloc(i, M_TEMP, M_WAITOK);
245 allocated = 1;
246 } else {
247 buf = alloca(i);
248 allocated = 0;
249 }
250
251 if (OF_getprop(phandle, "interrupts", buf, i) != i) {
252 rv = -1;
253 goto out;
254 }
255
256 for (i = 0, bp = buf; i < ndescs; i++, bp += 8) {
257 descp[i].irq = of_decode_int(&bp[0]);
258 switch (of_decode_int(&bp[4])) {
259 case 0:
260 case 1:
261 descp[i].share = IST_LEVEL;
262 break;
263 case 2:
264 case 3:
265 descp[i].share = IST_EDGE;
266 break;
267 #ifdef DIAGNOSTIC
268 default:
269 /* Dunno what to do, so fail. */
270 printf("ofisa_intr_get: unknown intrerrupt type %d\n",
271 of_decode_int(&bp[4]));
272 rv = -1;
273 goto out;
274 #endif
275 }
276 }
277 rv = i; /* number of descriptors processed (== ndescs) */
278
279 out:
280 if (allocated)
281 free(buf, M_TEMP);
282 return (rv);
283 }
284
285 void
286 ofisa_intr_print(descp, ndescs)
287 struct ofisa_intr_desc *descp;
288 int ndescs;
289 {
290 int i;
291
292 if (ndescs == 0) {
293 printf("none");
294 return;
295 }
296
297 for (i = 0; i < ndescs; i++) {
298 printf("%s%d (%s)", i ? ", " : "", descp[i].irq,
299 descp[i].share == IST_LEVEL ? "level" : "edge");
300 }
301 }
302