ofisa.c revision 1.1 1 /* $NetBSD: ofisa.c,v 1.1 1998/02/07 00:46:52 cgd 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 struct cfdriver ofisa_cd = {
57 NULL, "ofisa", DV_DULL
58 };
59
60 static int ofisaprint __P((void *, const char *));
61
62 static int
63 ofisaprint(aux, pnp)
64 void *aux;
65 const char *pnp;
66 {
67 struct ofprobe *ofp = aux;
68 char name[64];
69
70 (void)of_packagename(ofp->phandle, name, sizeof name);
71 if (pnp)
72 printf("%s at %s", name, pnp);
73 else
74 printf(" (%s)", name);
75 return UNCONF;
76 }
77
78 int
79 ofisamatch(parent, cf, aux)
80 struct device *parent;
81 struct cfdata *cf;
82 void *aux;
83 {
84 struct ofprobe *ofp = aux;
85 const char *compatible_strings[] = { "pnpPNP,a00", NULL };
86 int rv = 0;
87
88 if (of_compatible(ofp->phandle, compatible_strings) != -1)
89 rv = 5;
90
91 #ifdef _OFISA_MD_MATCH
92 if (!rv)
93 rv = ofisa_md_match(parent, cf, aux);
94 #endif
95
96 return (rv);
97 }
98
99 void
100 ofisaattach(parent, dev, aux)
101 struct device *parent, *dev;
102 void *aux;
103 {
104
105 struct ofprobe *ofp = aux;
106 struct isabus_attach_args iba;
107 struct ofisa_attach_args aa;
108 int child;
109
110 if (ofisa_get_isabus_data(ofp->phandle, &iba) < 0) {
111 printf(": couldn't get essential bus data\n");
112 return;
113 }
114
115 printf("\n");
116
117 for (child = OF_child(ofp->phandle); child;
118 child = OF_peer(child)) {
119 if (ofisa_ignore_child(ofp->phandle, child))
120 continue;
121
122 bzero(&aa, sizeof aa);
123
124 aa.ofp.phandle = child;
125 aa.iot = iba.iba_iot;
126 aa.memt = iba.iba_memt;
127 aa.dmat = iba.iba_dmat;
128 aa.ic = iba.iba_ic;
129
130 config_found(dev, &aa, ofisaprint);
131 }
132 }
133
134 int
135 ofisa_reg_count(phandle)
136 int phandle;
137 {
138 int len;
139
140 len = OF_getproplen(phandle, "reg");
141
142 /* nonexistant or obviously malformed "reg" property */
143 if (len < 0 || (len % 12) != 0)
144 return (-1);
145 return (len / 12);
146 }
147
148 int
149 ofisa_reg_get(phandle, descp, ndescs)
150 int phandle;
151 struct ofisa_reg_desc *descp;
152 int ndescs;
153 {
154 char *buf, *bp;
155 int i, allocated, rv;
156
157 i = ofisa_reg_count(phandle);
158 if (i < 0)
159 return (-1);
160 ndescs = min(ndescs, i);
161
162 i = ndescs * 12;
163 if (i > OFW_MAX_STACK_BUF_SIZE) {
164 buf = malloc(i, M_TEMP, M_WAITOK);
165 allocated = 1;
166 } else {
167 buf = alloca(i);
168 allocated = 0;
169 }
170
171 if (OF_getprop(phandle, "reg", buf, i) != i) {
172 rv = -1;
173 goto out;
174 }
175
176 for (i = 0, bp = buf; i < ndescs; i++, bp += 12) {
177 if (of_decode_int(&bp[0]) & 1)
178 descp[i].type = OFISA_REG_TYPE_IO;
179 else
180 descp[i].type = OFISA_REG_TYPE_MEM;
181 descp[i].addr = of_decode_int(&bp[4]);
182 descp[i].len = of_decode_int(&bp[8]);
183 }
184 rv = i; /* number of descriptors processed (== ndescs) */
185
186 out:
187 if (allocated)
188 free(buf, M_TEMP);
189 return (rv);
190 }
191
192 void
193 ofisa_reg_print(descp, ndescs)
194 struct ofisa_reg_desc *descp;
195 int ndescs;
196 {
197 int i;
198
199 if (ndescs == 0) {
200 printf("none");
201 return;
202 }
203
204 for (i = 0; i < ndescs; i++) {
205 printf("%s%s 0x%lx/%ld", i ? ", " : "",
206 descp[i].type == OFISA_REG_TYPE_IO ? "io" : "mem",
207 (long)descp[i].addr, (long)descp[i].len);
208 }
209 }
210
211 int
212 ofisa_intr_count(phandle)
213 int phandle;
214 {
215 int len;
216
217 len = OF_getproplen(phandle, "interrupts");
218
219 /* nonexistant or obviously malformed "reg" property */
220 if (len < 0 || (len % 8) != 0)
221 return (-1);
222 return (len / 8);
223 }
224
225 int
226 ofisa_intr_get(phandle, descp, ndescs)
227 int phandle;
228 struct ofisa_intr_desc *descp;
229 int ndescs;
230 {
231 char *buf, *bp;
232 int i, allocated, rv;
233
234 i = ofisa_intr_count(phandle);
235 if (i < 0)
236 return (-1);
237 ndescs = min(ndescs, i);
238
239 i = ndescs * 8;
240 if (i > OFW_MAX_STACK_BUF_SIZE) {
241 buf = malloc(i, M_TEMP, M_WAITOK);
242 allocated = 1;
243 } else {
244 buf = alloca(i);
245 allocated = 0;
246 }
247
248 if (OF_getprop(phandle, "interrupts", buf, i) != i) {
249 rv = -1;
250 goto out;
251 }
252
253 for (i = 0, bp = buf; i < ndescs; i++, bp += 8) {
254 descp[i].irq = of_decode_int(&bp[0]);
255 switch (of_decode_int(&bp[4])) {
256 case 0:
257 case 1:
258 descp[i].share = IST_LEVEL;
259 break;
260 case 2:
261 case 3:
262 descp[i].share = IST_EDGE;
263 break;
264 #ifdef DIAGNOSTIC
265 default:
266 /* Dunno what to do, so fail. */
267 printf("ofisa_intr_get: unknown intrerrupt type %d\n",
268 of_decode_int(&bp[4]));
269 rv = -1;
270 goto out;
271 #endif
272 }
273 }
274 rv = i; /* number of descriptors processed (== ndescs) */
275
276 out:
277 if (allocated)
278 free(buf, M_TEMP);
279 return (rv);
280 }
281
282 void
283 ofisa_intr_print(descp, ndescs)
284 struct ofisa_intr_desc *descp;
285 int ndescs;
286 {
287 int i;
288
289 if (ndescs == 0) {
290 printf("none");
291 return;
292 }
293
294 for (i = 0; i < ndescs; i++) {
295 printf("%s%d (%s)", i ? ", " : "", descp[i].irq,
296 descp[i].share == IST_LEVEL ? "level" : "edge");
297 }
298 }
299