nubus.c revision 1.31 1 1.31 scottr /* $NetBSD: nubus.c,v 1.31 1997/02/24 06:20:06 scottr Exp $ */
2 1.8 cgd
3 1.11 briggs /*
4 1.29 scottr * Copyright (c) 1995, 1996 Allen Briggs. All rights reserved.
5 1.1 briggs *
6 1.1 briggs * Redistribution and use in source and binary forms, with or without
7 1.1 briggs * modification, are permitted provided that the following conditions
8 1.1 briggs * are met:
9 1.1 briggs * 1. Redistributions of source code must retain the above copyright
10 1.1 briggs * notice, this list of conditions and the following disclaimer.
11 1.1 briggs * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 briggs * notice, this list of conditions and the following disclaimer in the
13 1.1 briggs * documentation and/or other materials provided with the distribution.
14 1.1 briggs * 3. All advertising materials mentioning features or use of this software
15 1.1 briggs * must display the following acknowledgement:
16 1.11 briggs * This product includes software developed by Allen Briggs.
17 1.11 briggs * 4. The name of the author may not be used to endorse or promote products
18 1.11 briggs * derived from this software without specific prior written permission.
19 1.1 briggs *
20 1.11 briggs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 1.1 briggs * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 1.1 briggs * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 1.11 briggs * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.1 briggs * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 1.1 briggs * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 1.1 briggs * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 1.1 briggs * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 1.11 briggs * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 1.11 briggs * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 briggs */
31 1.1 briggs
32 1.4 briggs #include <sys/param.h>
33 1.4 briggs #include <sys/systm.h>
34 1.10 briggs #include <sys/device.h>
35 1.29 scottr #include <sys/buf.h>
36 1.29 scottr #include <sys/conf.h>
37 1.29 scottr #include <sys/dmap.h>
38 1.29 scottr
39 1.29 scottr #include <vm/vm.h>
40 1.29 scottr #include <vm/vm_kern.h>
41 1.29 scottr #include <vm/vm_map.h>
42 1.10 briggs
43 1.19 briggs #include <machine/autoconf.h>
44 1.30 scottr #include <machine/bus.h>
45 1.29 scottr #include <machine/vmparam.h>
46 1.29 scottr #include <machine/param.h>
47 1.31 scottr #include <machine/bus.h>
48 1.4 briggs #include <machine/cpu.h>
49 1.29 scottr #include <machine/pte.h>
50 1.1 briggs
51 1.12 briggs #include <vm/vm.h>
52 1.12 briggs
53 1.1 briggs #include "nubus.h"
54 1.1 briggs
55 1.23 scottr #ifdef DEBUG
56 1.12 briggs static int nubus_debug = 0x01;
57 1.12 briggs #define NDB_PROBE 0x1
58 1.12 briggs #define NDB_FOLLOW 0x2
59 1.12 briggs #define NDB_ARITH 0x4
60 1.12 briggs #endif
61 1.12 briggs
62 1.28 scottr static int nubus_print __P((void *, const char *));
63 1.28 scottr static int nubus_match __P((struct device *, struct cfdata *, void *));
64 1.28 scottr static void nubus_attach __P((struct device *, struct device *, void *));
65 1.28 scottr int nubus_video_resource __P((int));
66 1.11 briggs
67 1.11 briggs static int probe_slot __P((int slot, nubus_slot *fmt));
68 1.11 briggs static u_long IncPtr __P((nubus_slot *fmt, u_long base, long amt));
69 1.11 briggs static u_long nubus_calc_CRC __P((nubus_slot *fmt));
70 1.11 briggs static u_char GetByte __P((nubus_slot *fmt, u_long ptr));
71 1.17 briggs #ifdef notyet
72 1.17 briggs /* unused */ static u_short GetWord __P((nubus_slot *fmt, u_long ptr));
73 1.17 briggs #endif
74 1.11 briggs static u_long GetLong __P((nubus_slot *fmt, u_long ptr));
75 1.18 scottr
76 1.29 scottr static int nubus_peek __P((vm_offset_t, int));
77 1.29 scottr static char *nubus_mapin __P((int, int));
78 1.29 scottr
79 1.16 thorpej struct cfattach nubus_ca = {
80 1.28 scottr sizeof(struct nubus_softc), nubus_match, nubus_attach
81 1.16 thorpej };
82 1.16 thorpej
83 1.16 thorpej struct cfdriver nubus_cd = {
84 1.28 scottr NULL, "nubus", DV_DULL,
85 1.11 briggs };
86 1.1 briggs
87 1.19 briggs static int
88 1.28 scottr nubus_match(parent, cf, aux)
89 1.19 briggs struct device *parent;
90 1.28 scottr struct cfdata *cf;
91 1.28 scottr void *aux;
92 1.19 briggs {
93 1.29 scottr static int nubus_matched = 0;
94 1.19 briggs
95 1.29 scottr /* Allow only one instance. */
96 1.29 scottr if (nubus_matched)
97 1.29 scottr return (0);
98 1.29 scottr
99 1.29 scottr nubus_matched = 1;
100 1.29 scottr return (1);
101 1.19 briggs }
102 1.19 briggs
103 1.11 briggs static void
104 1.28 scottr nubus_attach(parent, self, aux)
105 1.28 scottr struct device *parent, *self;
106 1.28 scottr void *aux;
107 1.11 briggs {
108 1.28 scottr struct nubus_attach_args na_args;
109 1.28 scottr nubus_slot fmtblock;
110 1.28 scottr nubus_dir dir;
111 1.28 scottr nubus_dirent dirent;
112 1.28 scottr nubus_type slottype;
113 1.28 scottr int i, rsrcid;
114 1.1 briggs
115 1.26 christos printf("\n");
116 1.1 briggs
117 1.19 briggs for (i = NUBUS_MIN_SLOT; i <= NUBUS_MAX_SLOT; i++) {
118 1.28 scottr if (probe_slot(i, &fmtblock) <= 0)
119 1.28 scottr continue;
120 1.28 scottr
121 1.28 scottr if ((rsrcid = nubus_video_resource(i)) == (-1))
122 1.28 scottr rsrcid = 0x80;
123 1.28 scottr
124 1.28 scottr nubus_get_main_dir(&fmtblock, &dir);
125 1.28 scottr
126 1.28 scottr if (nubus_find_rsrc(&fmtblock, &dir, rsrcid, &dirent) <= 0)
127 1.28 scottr continue;
128 1.28 scottr
129 1.28 scottr nubus_get_dir_from_rsrc(&fmtblock, &dirent, &dir);
130 1.28 scottr
131 1.28 scottr if (nubus_find_rsrc(&fmtblock, &dir, NUBUS_RSRC_TYPE,
132 1.28 scottr &dirent) <= 0)
133 1.28 scottr continue;
134 1.28 scottr
135 1.28 scottr if (nubus_get_ind_data(&fmtblock, &dirent,
136 1.28 scottr (caddr_t) &slottype, sizeof(nubus_type)) <= 0)
137 1.28 scottr continue;
138 1.28 scottr
139 1.28 scottr na_args.slot = i;
140 1.28 scottr na_args.rsrcid = rsrcid;
141 1.28 scottr na_args.category = slottype.category;
142 1.28 scottr na_args.type = slottype.type;
143 1.28 scottr na_args.drsw = slottype.drsw;
144 1.28 scottr na_args.drhw = slottype.drhw;
145 1.28 scottr na_args.fmt = &fmtblock;
146 1.28 scottr
147 1.28 scottr config_found(self, &na_args, nubus_print);
148 1.12 briggs }
149 1.1 briggs }
150 1.1 briggs
151 1.10 briggs static int
152 1.28 scottr nubus_print(aux, name)
153 1.28 scottr void *aux;
154 1.28 scottr const char *name;
155 1.11 briggs {
156 1.28 scottr struct nubus_attach_args *na = (struct nubus_attach_args *) aux;
157 1.1 briggs
158 1.11 briggs if (name) {
159 1.28 scottr printf("%s: slot %x: %s ", name, na->fmt->slot,
160 1.28 scottr nubus_get_card_name(na->fmt));
161 1.26 christos printf("(Vendor: %s, ",
162 1.28 scottr nubus_get_vendor(na->fmt, NUBUS_RSRC_VEND_ID));
163 1.28 scottr printf("Part: %s)",
164 1.28 scottr nubus_get_vendor(na->fmt, NUBUS_RSRC_VEND_PART));
165 1.1 briggs }
166 1.11 briggs return (UNCONF);
167 1.28 scottr }
168 1.28 scottr
169 1.28 scottr int
170 1.28 scottr nubus_video_resource(slot)
171 1.28 scottr int slot;
172 1.28 scottr {
173 1.28 scottr extern u_int16_t mac68k_vrsrc_vec[];
174 1.28 scottr int i;
175 1.28 scottr
176 1.28 scottr for (i = 0 ; i < 6 ; i++)
177 1.28 scottr if ((mac68k_vrsrc_vec[i] & 0xff) == slot)
178 1.28 scottr return ((mac68k_vrsrc_vec[i] >> 8) & 0xff);
179 1.28 scottr return (-1);
180 1.1 briggs }
181 1.1 briggs
182 1.11 briggs /*
183 1.11 briggs * Probe a given nubus slot. If a card is there and we can get the
184 1.11 briggs * format block from it's clutching decl. ROMs, fill the format block
185 1.11 briggs * and return non-zero. If we can't find a card there with a valid
186 1.11 briggs * decl. ROM, return 0.
187 1.11 briggs *
188 1.11 briggs * First, we check to see if we can access the memory at the tail
189 1.11 briggs * end of the slot. If so, then we check for a bytelanes byte. We
190 1.11 briggs * could probably just return a failure status if we bus error on
191 1.11 briggs * the first try, but there really is little reason not to go ahead
192 1.11 briggs * and check the other three locations in case there's a wierd card
193 1.11 briggs * out there.
194 1.11 briggs *
195 1.11 briggs * Checking for a card involves locating the "bytelanes" byte which
196 1.11 briggs * tells us how to interpret the declaration ROM's data. The format
197 1.11 briggs * block is at the top of the card's standard memory space and the
198 1.11 briggs * bytelanes byte is at the end of that block.
199 1.11 briggs *
200 1.11 briggs * After some inspection of the bytelanes byte, it appears that it
201 1.11 briggs * takes the form 0xXY where Y is a bitmask of the bytelanes in use
202 1.11 briggs * and X is a bitmask of the lanes to ignore. Hence, (X ^ Y) == 0
203 1.11 briggs * and (less obviously), Y will have the upper N bits clear if it is
204 1.11 briggs * found N bytes from the last possible location. Both that and
205 1.11 briggs * the exclusive-or check are made.
206 1.11 briggs *
207 1.11 briggs * If a valid
208 1.11 briggs */
209 1.11 briggs static u_char nbits[]={0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
210 1.11 briggs static int
211 1.11 briggs probe_slot(slot, fmt)
212 1.11 briggs int slot;
213 1.11 briggs nubus_slot *fmt;
214 1.1 briggs {
215 1.11 briggs caddr_t rom_probe;
216 1.19 briggs vm_offset_t hdr;
217 1.23 scottr #ifdef DEBUG
218 1.23 scottr vm_offset_t pa;
219 1.23 scottr #endif
220 1.19 briggs u_int data;
221 1.11 briggs int hdr_size, i;
222 1.10 briggs
223 1.11 briggs fmt->bytelanes = 0;
224 1.11 briggs fmt->slot = (u_long)slot;
225 1.10 briggs
226 1.31 scottr rom_probe = (caddr_t) (NUBUS_SLOT2PA(fmt->slot) + NBMEMSIZE);
227 1.10 briggs
228 1.12 briggs #ifdef DEBUG
229 1.12 briggs if (nubus_debug & NDB_PROBE) {
230 1.23 scottr pa = pmap_extract(pmap_kernel(), (vm_offset_t) rom_probe - 1);
231 1.27 scottr printf("probing slot %d, first probe at 0x%p (PA 0x%lx).\n",
232 1.23 scottr slot, rom_probe - 1, pa);
233 1.12 briggs }
234 1.12 briggs #endif
235 1.12 briggs
236 1.11 briggs for (i = 4; i && (fmt->bytelanes == 0); i--) {
237 1.10 briggs
238 1.11 briggs rom_probe--;
239 1.10 briggs
240 1.29 scottr data = nubus_peek((vm_offset_t) rom_probe, 1);
241 1.19 briggs if (data == -1)
242 1.11 briggs continue;
243 1.10 briggs
244 1.19 briggs if (data == 0)
245 1.11 briggs continue;
246 1.1 briggs
247 1.11 briggs if ( ((((data & 0xf0) >> 4) ^ (data & 0x0f)) == 0x0f)
248 1.11 briggs && ((data & 0x0f) < (1 << i)) ) {
249 1.11 briggs fmt->bytelanes = data;
250 1.11 briggs fmt->step = nbits[(data & 0x0f)];
251 1.11 briggs }
252 1.1 briggs }
253 1.11 briggs #ifdef DEBUG
254 1.12 briggs if (nubus_debug & NDB_PROBE)
255 1.12 briggs if (fmt->bytelanes == 0)
256 1.26 christos printf("bytelanes not found for slot 0x%x.\n", slot);
257 1.11 briggs #endif
258 1.11 briggs
259 1.11 briggs if (fmt->bytelanes == 0)
260 1.11 briggs return 0;
261 1.11 briggs
262 1.11 briggs #ifdef DEBUG
263 1.12 briggs if (nubus_debug & NDB_PROBE)
264 1.26 christos printf("bytelanes of 0x%x found for slot 0x%x.\n",
265 1.20 briggs fmt->bytelanes, slot);
266 1.11 briggs #endif
267 1.1 briggs
268 1.11 briggs hdr_size = 20;
269 1.10 briggs
270 1.11 briggs /*
271 1.11 briggs * Go ahead and attempt to load format header.
272 1.11 briggs * First, we need to find the first byte beyond memory that
273 1.11 briggs * would be valid. This is necessary for NUBUS_ROM_offset()
274 1.11 briggs * to work.
275 1.11 briggs */
276 1.19 briggs hdr = (vm_offset_t)
277 1.31 scottr nubus_mapin(NUBUS_SLOT2PA(fmt->slot), NBMEMSIZE);
278 1.19 briggs if (hdr == NULL) {
279 1.26 christos printf("Failed to map %d bytes for NuBUS slot %d probe. ",
280 1.22 briggs NBMEMSIZE, fmt->slot);
281 1.26 christos printf("Physical slot address %x\n",
282 1.31 scottr (unsigned int) NUBUS_SLOT2PA(fmt->slot));
283 1.19 briggs }
284 1.19 briggs fmt->virtual_base = hdr;
285 1.19 briggs hdr += NBMEMSIZE;
286 1.19 briggs
287 1.11 briggs i = 0x10 | (fmt->bytelanes & 0x0f);
288 1.11 briggs while ((i & 1) == 0) {
289 1.11 briggs hdr++;
290 1.11 briggs i >>= 1;
291 1.11 briggs }
292 1.11 briggs fmt->top = hdr;
293 1.11 briggs hdr = IncPtr(fmt, hdr, -hdr_size);
294 1.11 briggs #ifdef DEBUG
295 1.12 briggs if (nubus_debug & NDB_PROBE)
296 1.27 scottr printf("fmt->top is 0x%lx, that minus 0x%x puts us at 0x%lx.\n",
297 1.12 briggs fmt->top, hdr_size, hdr);
298 1.11 briggs #if 0
299 1.11 briggs for (i=1 ; i < 8 ; i++) {
300 1.26 christos printf("0x%x - 0x%x = 0x%x, + 0x%x = 0x%x.\n",
301 1.11 briggs hdr, i, IncPtr(fmt, hdr, -i),
302 1.11 briggs i, IncPtr(fmt, hdr, i));
303 1.11 briggs }
304 1.11 briggs #endif
305 1.11 briggs #endif
306 1.11 briggs
307 1.11 briggs fmt->directory_offset = 0xff000000 | GetLong(fmt, hdr);
308 1.11 briggs hdr = IncPtr(fmt, hdr, 4);
309 1.11 briggs fmt->length = GetLong(fmt, hdr);
310 1.11 briggs hdr = IncPtr(fmt, hdr, 4);
311 1.11 briggs fmt->crc = GetLong(fmt, hdr);
312 1.11 briggs hdr = IncPtr(fmt, hdr, 4);
313 1.11 briggs fmt->revision_level = GetByte(fmt, hdr);
314 1.11 briggs hdr = IncPtr(fmt, hdr, 1);
315 1.11 briggs fmt->format = GetByte(fmt, hdr);
316 1.11 briggs hdr = IncPtr(fmt, hdr, 1);
317 1.11 briggs fmt->test_pattern = GetLong(fmt, hdr);
318 1.11 briggs
319 1.23 scottr #ifdef DEBUG
320 1.12 briggs if (nubus_debug & NDB_PROBE) {
321 1.26 christos printf("Directory offset 0x%x\t", fmt->directory_offset);
322 1.26 christos printf("Length 0x%x\t", fmt->length);
323 1.26 christos printf("CRC 0x%x\n", fmt->crc);
324 1.26 christos printf("Revision level 0x%x\t", fmt->revision_level);
325 1.26 christos printf("Format 0x%x\t", fmt->format);
326 1.26 christos printf("Test Pattern 0x%x\n", fmt->test_pattern);
327 1.12 briggs }
328 1.11 briggs #endif
329 1.11 briggs
330 1.12 briggs if ((fmt->directory_offset & 0x00ff0000) == 0) {
331 1.26 christos printf("Invalid looking directory offset (0x%x)!\n",
332 1.12 briggs fmt->directory_offset);
333 1.12 briggs return 0;
334 1.12 briggs }
335 1.11 briggs if (fmt->test_pattern != NUBUS_ROM_TEST_PATTERN) {
336 1.26 christos printf("Nubus--test pattern invalid:\n");
337 1.26 christos printf(" slot 0x%x, bytelanes 0x%x?\n",
338 1.11 briggs fmt->slot, fmt->bytelanes);
339 1.26 christos printf(" read test 0x%x, compare with 0x%x.\n",
340 1.11 briggs fmt->test_pattern, NUBUS_ROM_TEST_PATTERN);
341 1.11 briggs return 0;
342 1.11 briggs }
343 1.11 briggs
344 1.11 briggs /* Perform CRC */
345 1.11 briggs if (fmt->crc != nubus_calc_CRC(fmt)) {
346 1.26 christos printf("Nubus--crc check failed, slot 0x%x.\n",
347 1.11 briggs fmt->slot);
348 1.11 briggs return 0;
349 1.1 briggs }
350 1.10 briggs
351 1.11 briggs return 1;
352 1.1 briggs }
353 1.1 briggs
354 1.11 briggs /*
355 1.11 briggs * Compute byte offset on card, taking into account bytelanes.
356 1.11 briggs * Base must be on a valid bytelane for this function to work.
357 1.11 briggs * Return the new address.
358 1.11 briggs *
359 1.11 briggs * XXX -- There has GOT to be a better way to do this.
360 1.11 briggs */
361 1.11 briggs static u_long
362 1.11 briggs IncPtr(fmt, base, amt)
363 1.11 briggs nubus_slot *fmt;
364 1.11 briggs u_long base;
365 1.11 briggs long amt;
366 1.11 briggs {
367 1.11 briggs u_char b, t;
368 1.11 briggs
369 1.11 briggs if (!amt)
370 1.11 briggs return base;
371 1.11 briggs
372 1.11 briggs if (amt < 0) {
373 1.11 briggs amt = -amt;
374 1.11 briggs b = fmt->bytelanes;
375 1.11 briggs t = (b << 4);
376 1.11 briggs b <<= (3 - (base & 0x3));
377 1.11 briggs while (amt) {
378 1.11 briggs b <<= 1;
379 1.11 briggs if (b == t)
380 1.11 briggs b = fmt->bytelanes;
381 1.11 briggs if (b & 0x08)
382 1.11 briggs amt--;
383 1.11 briggs base--;
384 1.11 briggs }
385 1.11 briggs return base;
386 1.11 briggs }
387 1.10 briggs
388 1.11 briggs t = (fmt->bytelanes & 0xf) | 0x10;
389 1.11 briggs b = t >> (base & 0x3);
390 1.11 briggs while (amt) {
391 1.11 briggs b >>= 1;
392 1.11 briggs if (b == 1)
393 1.11 briggs b = t;
394 1.11 briggs if (b & 1)
395 1.11 briggs amt--;
396 1.11 briggs base++;
397 1.11 briggs }
398 1.11 briggs
399 1.11 briggs return base;
400 1.11 briggs }
401 1.11 briggs
402 1.11 briggs static u_long
403 1.11 briggs nubus_calc_CRC(fmt)
404 1.11 briggs nubus_slot *fmt;
405 1.11 briggs {
406 1.11 briggs #if 0
407 1.11 briggs u_long base, ptr, crc_loc, sum;
408 1.11 briggs int i;
409 1.11 briggs
410 1.11 briggs base = fmt->top;
411 1.11 briggs crc_loc = NUBUS_ROM_offset(fmt, base, -12);
412 1.11 briggs ptr = NUBUS_ROM_offset(fmt, base, -fmt->length);
413 1.11 briggs
414 1.11 briggs sum = 0;
415 1.11 briggs while (ptr < base)
416 1.11 briggs roll #1, sum
417 1.11 briggs if (ptr == crc_loc) {
418 1.11 briggs roll #3, sum
419 1.11 briggs ptr = IncPtr(fmt, ptr, 3);
420 1.11 briggs } else {
421 1.11 briggs sum += GetByte(fmt, ptr);
422 1.11 briggs }
423 1.11 briggs ptr = IncPtr(fmt, ptr, 1);
424 1.1 briggs }
425 1.10 briggs
426 1.11 briggs return sum;
427 1.11 briggs #endif
428 1.11 briggs return fmt->crc;
429 1.1 briggs }
430 1.1 briggs
431 1.11 briggs static u_char
432 1.11 briggs GetByte(fmt, ptr)
433 1.11 briggs nubus_slot *fmt;
434 1.11 briggs u_long ptr;
435 1.1 briggs {
436 1.11 briggs return *(caddr_t)ptr;
437 1.1 briggs }
438 1.1 briggs
439 1.17 briggs #ifdef notyet
440 1.17 briggs /* Nothing uses this, yet */
441 1.11 briggs static u_short
442 1.11 briggs GetWord(fmt, ptr)
443 1.11 briggs nubus_slot *fmt;
444 1.11 briggs u_long ptr;
445 1.1 briggs {
446 1.11 briggs u_short s;
447 1.10 briggs
448 1.11 briggs s = (GetByte(fmt, ptr) << 8);
449 1.11 briggs ptr = IncPtr(fmt, ptr, 1);
450 1.11 briggs s |= GetByte(fmt, ptr);
451 1.11 briggs return s;
452 1.11 briggs }
453 1.17 briggs #endif
454 1.1 briggs
455 1.11 briggs static u_long
456 1.11 briggs GetLong(fmt, ptr)
457 1.11 briggs nubus_slot *fmt;
458 1.11 briggs u_long ptr;
459 1.11 briggs {
460 1.11 briggs register u_long l;
461 1.11 briggs register int i;
462 1.10 briggs
463 1.11 briggs l = 0;
464 1.11 briggs for ( i = 0; i < 4; i++) {
465 1.11 briggs l = (l << 8) | GetByte(fmt, ptr);
466 1.11 briggs ptr = IncPtr(fmt, ptr, 1);
467 1.11 briggs }
468 1.11 briggs return l;
469 1.11 briggs }
470 1.10 briggs
471 1.11 briggs void
472 1.11 briggs nubus_get_main_dir(slot, dir_return)
473 1.11 briggs nubus_slot *slot;
474 1.11 briggs nubus_dir *dir_return;
475 1.11 briggs {
476 1.23 scottr #ifdef DEBUG
477 1.12 briggs if (nubus_debug & NDB_FOLLOW)
478 1.26 christos printf("nubus_get_main_dir(0x%x, 0x%x)\n",
479 1.12 briggs (u_int) slot, (u_int) dir_return);
480 1.12 briggs #endif
481 1.11 briggs dir_return->dirbase = IncPtr(slot, slot->top,
482 1.11 briggs slot->directory_offset - 20);
483 1.11 briggs dir_return->curr_ent = dir_return->dirbase;
484 1.1 briggs }
485 1.1 briggs
486 1.11 briggs int
487 1.11 briggs nubus_find_rsrc(slot, dir, rsrcid, dirent_return)
488 1.11 briggs nubus_slot *slot;
489 1.11 briggs nubus_dir *dir;
490 1.11 briggs u_int8_t rsrcid;
491 1.11 briggs nubus_dirent *dirent_return;
492 1.1 briggs {
493 1.11 briggs u_long entry;
494 1.11 briggs u_char byte;
495 1.10 briggs
496 1.23 scottr #ifdef DEBUG
497 1.12 briggs if (nubus_debug & NDB_FOLLOW)
498 1.26 christos printf("nubus_find_rsrc(0x%x, 0x%x, 0x%x, 0x%x)\n",
499 1.12 briggs (u_int) slot, (u_int) dir, (u_int) rsrcid,
500 1.12 briggs (u_int) dirent_return);
501 1.11 briggs #endif
502 1.12 briggs if (slot->test_pattern != NUBUS_ROM_TEST_PATTERN)
503 1.12 briggs return -1;
504 1.12 briggs
505 1.12 briggs entry = dir->curr_ent;
506 1.11 briggs do {
507 1.11 briggs byte = GetByte(slot, entry);
508 1.23 scottr #ifdef DEBUG
509 1.12 briggs if (nubus_debug & NDB_FOLLOW)
510 1.26 christos printf("\tFound rsrc 0x%x.\n", byte);
511 1.11 briggs #endif
512 1.11 briggs if (byte == rsrcid) {
513 1.11 briggs dirent_return->myloc = entry;
514 1.11 briggs dirent_return->rsrc_id = rsrcid;
515 1.11 briggs entry = GetLong(slot, entry);
516 1.11 briggs dirent_return->offset = (entry & 0x00ffffff);
517 1.11 briggs return 1;
518 1.11 briggs }
519 1.11 briggs if (byte == 0xff) {
520 1.11 briggs entry = dir->dirbase;
521 1.11 briggs } else {
522 1.11 briggs entry = IncPtr(slot, entry, 4);
523 1.11 briggs }
524 1.11 briggs } while (entry != (u_long) dir->curr_ent);
525 1.11 briggs return 0;
526 1.1 briggs }
527 1.1 briggs
528 1.11 briggs void
529 1.11 briggs nubus_get_dir_from_rsrc(slot, dirent, dir_return)
530 1.11 briggs nubus_slot *slot;
531 1.11 briggs nubus_dirent *dirent;
532 1.11 briggs nubus_dir *dir_return;
533 1.1 briggs {
534 1.11 briggs u_long loc;
535 1.10 briggs
536 1.23 scottr #ifdef DEBUG
537 1.12 briggs if (nubus_debug & NDB_FOLLOW)
538 1.26 christos printf("nubus_get_dir_from_rsrc(0x%x, 0x%x, 0x%x).\n",
539 1.12 briggs (u_int) slot, (u_int) dirent, (u_int) dir_return);
540 1.12 briggs #endif
541 1.11 briggs if ((loc = dirent->offset) & 0x800000) {
542 1.11 briggs loc |= 0xff000000;
543 1.1 briggs }
544 1.11 briggs dir_return->dirbase = IncPtr(slot, dirent->myloc, loc);
545 1.11 briggs dir_return->curr_ent = dir_return->dirbase;
546 1.1 briggs }
547 1.1 briggs
548 1.11 briggs int
549 1.11 briggs nubus_get_ind_data(slot, dirent, data_return, nbytes)
550 1.11 briggs nubus_slot *slot;
551 1.11 briggs nubus_dirent *dirent;
552 1.11 briggs caddr_t data_return;
553 1.11 briggs int nbytes;
554 1.11 briggs {
555 1.11 briggs u_long loc;
556 1.1 briggs
557 1.23 scottr #ifdef DEBUG
558 1.12 briggs if (nubus_debug & NDB_FOLLOW)
559 1.26 christos printf("nubus_get_ind_data(0x%x, 0x%x, 0x%x, %d).\n",
560 1.12 briggs (u_int) slot, (u_int) dirent, (u_int) data_return,
561 1.12 briggs nbytes);
562 1.12 briggs #endif
563 1.11 briggs if ((loc = dirent->offset) & 0x800000) {
564 1.11 briggs loc |= 0xff000000;
565 1.11 briggs }
566 1.11 briggs loc = IncPtr(slot, dirent->myloc, loc);
567 1.1 briggs
568 1.11 briggs while (nbytes--) {
569 1.11 briggs *data_return++ = GetByte(slot, loc);
570 1.11 briggs loc = IncPtr(slot, loc, 1);
571 1.1 briggs }
572 1.11 briggs return 1;
573 1.1 briggs }
574 1.1 briggs
575 1.11 briggs int
576 1.11 briggs nubus_get_c_string(slot, dirent, data_return, max_bytes)
577 1.11 briggs nubus_slot *slot;
578 1.11 briggs nubus_dirent *dirent;
579 1.11 briggs caddr_t data_return;
580 1.11 briggs int max_bytes;
581 1.1 briggs {
582 1.11 briggs u_long loc;
583 1.10 briggs
584 1.23 scottr #ifdef DEBUG
585 1.12 briggs if (nubus_debug & NDB_FOLLOW)
586 1.26 christos printf("nubus_get_c_string(0x%x, 0x%x, 0x%x, %d).\n",
587 1.12 briggs (u_int) slot, (u_int) dirent, (u_int) data_return,
588 1.12 briggs max_bytes);
589 1.12 briggs #endif
590 1.11 briggs if ((loc = dirent->offset) & 0x800000) {
591 1.11 briggs loc |= 0xff000000;
592 1.11 briggs }
593 1.11 briggs loc = IncPtr(slot, dirent->myloc, loc);
594 1.10 briggs
595 1.11 briggs *data_return = '\0';
596 1.11 briggs while (max_bytes--) {
597 1.11 briggs if ((*data_return++ = GetByte(slot, loc)) == 0)
598 1.11 briggs return 1;
599 1.11 briggs loc = IncPtr(slot, loc, 1);
600 1.11 briggs }
601 1.11 briggs return 0;
602 1.1 briggs }
603 1.2 briggs
604 1.11 briggs static char *huh = "???";
605 1.3 briggs
606 1.11 briggs char *
607 1.11 briggs nubus_get_vendor(slot, rsrc)
608 1.11 briggs nubus_slot *slot;
609 1.11 briggs int rsrc;
610 1.3 briggs {
611 1.11 briggs static char str_ret[64];
612 1.11 briggs nubus_dir dir;
613 1.11 briggs nubus_dirent ent;
614 1.3 briggs
615 1.23 scottr #ifdef DEBUG
616 1.12 briggs if (nubus_debug & NDB_FOLLOW)
617 1.26 christos printf("nubus_get_vendor(0x%x, 0x%x).\n", (u_int) slot, rsrc);
618 1.12 briggs #endif
619 1.11 briggs nubus_get_main_dir(slot, &dir);
620 1.11 briggs if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
621 1.11 briggs return huh;
622 1.11 briggs nubus_get_dir_from_rsrc(slot, &ent, &dir);
623 1.2 briggs
624 1.11 briggs if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_VENDORINFO, &ent) <= 0)
625 1.11 briggs return huh;
626 1.11 briggs nubus_get_dir_from_rsrc(slot, &ent, &dir);
627 1.10 briggs
628 1.11 briggs if (nubus_find_rsrc(slot, &dir, rsrc, &ent) <= 0)
629 1.11 briggs return huh;
630 1.10 briggs
631 1.11 briggs nubus_get_c_string(slot, &ent, str_ret, 64);
632 1.2 briggs
633 1.11 briggs return str_ret;
634 1.2 briggs }
635 1.2 briggs
636 1.11 briggs char *
637 1.11 briggs nubus_get_card_name(slot)
638 1.11 briggs nubus_slot *slot;
639 1.2 briggs {
640 1.11 briggs static char name_ret[64];
641 1.11 briggs nubus_dir dir;
642 1.11 briggs nubus_dirent ent;
643 1.2 briggs
644 1.23 scottr #ifdef DEBUG
645 1.12 briggs if (nubus_debug & NDB_FOLLOW)
646 1.26 christos printf("nubus_get_card_name(0x%lx).\n", (u_long) slot);
647 1.12 briggs #endif
648 1.11 briggs nubus_get_main_dir(slot, &dir);
649 1.2 briggs
650 1.11 briggs if (nubus_find_rsrc(slot, &dir, 1, &ent) <= 0)
651 1.11 briggs return huh;
652 1.2 briggs
653 1.11 briggs nubus_get_dir_from_rsrc(slot, &ent, &dir);
654 1.2 briggs
655 1.11 briggs if (nubus_find_rsrc(slot, &dir, NUBUS_RSRC_NAME, &ent) <= 0)
656 1.11 briggs return huh;
657 1.2 briggs
658 1.11 briggs nubus_get_c_string(slot, &ent, name_ret, 64);
659 1.2 briggs
660 1.11 briggs return name_ret;
661 1.2 briggs }
662 1.29 scottr
663 1.29 scottr /*
664 1.29 scottr * bus_*() functions adapted from sun3 generic "bus" support
665 1.29 scottr * by Allen Briggs.
666 1.29 scottr */
667 1.29 scottr
668 1.29 scottr vm_offset_t tmp_vpages[1];
669 1.29 scottr
670 1.29 scottr /*
671 1.29 scottr * Read addr with size len (1,2,4) into val.
672 1.29 scottr * If this generates a bus error, return -1
673 1.29 scottr *
674 1.29 scottr * Create a temporary mapping,
675 1.29 scottr * Try the access using peek_*
676 1.29 scottr * Clean up temp. mapping
677 1.29 scottr */
678 1.29 scottr static int
679 1.29 scottr nubus_peek(paddr, sz)
680 1.29 scottr vm_offset_t paddr;
681 1.29 scottr int sz;
682 1.29 scottr {
683 1.29 scottr int off, pte, rv;
684 1.29 scottr vm_offset_t pgva;
685 1.29 scottr caddr_t va;
686 1.29 scottr
687 1.29 scottr off = paddr & PGOFSET;
688 1.29 scottr paddr -= off;
689 1.29 scottr pte = (paddr & PG_FRAME) | (PG_V | PG_W | PG_CI);
690 1.29 scottr
691 1.29 scottr pgva = tmp_vpages[0];
692 1.29 scottr va = (caddr_t)pgva + off;
693 1.29 scottr
694 1.29 scottr mac68k_set_pte(pgva, pte);
695 1.29 scottr TBIS(pgva);
696 1.29 scottr
697 1.29 scottr /*
698 1.29 scottr * OK, try the access using one of the assembly routines
699 1.29 scottr * that will set pcb_onfault and catch any bus errors.
700 1.29 scottr */
701 1.29 scottr rv = -1;
702 1.29 scottr switch (sz) {
703 1.29 scottr case 1:
704 1.29 scottr if (!badbaddr(va))
705 1.29 scottr rv = *((u_char *) va);
706 1.29 scottr break;
707 1.29 scottr case 2:
708 1.29 scottr if (!badwaddr(va))
709 1.29 scottr rv = *((u_int16_t *) va);
710 1.29 scottr break;
711 1.29 scottr case 4:
712 1.29 scottr if (!badladdr(va))
713 1.29 scottr rv = *((u_int32_t *) va);
714 1.29 scottr break;
715 1.29 scottr default:
716 1.29 scottr printf("bus_peek: invalid size=%d\n", sz);
717 1.29 scottr rv = -1;
718 1.29 scottr }
719 1.29 scottr
720 1.29 scottr mac68k_set_pte(pgva, PG_NV);
721 1.29 scottr TBIS(pgva);
722 1.29 scottr
723 1.29 scottr return rv;
724 1.29 scottr }
725 1.29 scottr
726 1.29 scottr static char *
727 1.29 scottr nubus_mapin(paddr, sz)
728 1.29 scottr int paddr, sz;
729 1.29 scottr {
730 1.29 scottr int off, pa, pmt=0;
731 1.29 scottr vm_offset_t va, retval;
732 1.29 scottr
733 1.29 scottr off = paddr & PGOFSET;
734 1.29 scottr pa = paddr - off;
735 1.29 scottr sz += off;
736 1.29 scottr sz = mac68k_round_page(sz);
737 1.29 scottr
738 1.29 scottr /* Get some kernel virtual address space. */
739 1.29 scottr va = kmem_alloc_wait(kernel_map, sz);
740 1.29 scottr if (va == 0)
741 1.29 scottr panic("bus_mapin");
742 1.29 scottr retval = va + off;
743 1.29 scottr
744 1.29 scottr /* Map it to the specified bus. */
745 1.29 scottr #if 0 /* XXX */
746 1.29 scottr /* This has a problem with wrap-around... */
747 1.29 scottr pmap_map((int)va, pa | pmt, pa + sz, VM_PROT_ALL);
748 1.29 scottr #else
749 1.29 scottr do {
750 1.29 scottr pmap_enter(pmap_kernel(), va, pa | pmt, VM_PROT_ALL, FALSE);
751 1.29 scottr va += NBPG;
752 1.29 scottr pa += NBPG;
753 1.29 scottr } while ((sz -= NBPG) > 0);
754 1.29 scottr #endif
755 1.29 scottr
756 1.29 scottr return ((char*)retval);
757 1.29 scottr }
758