]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/nubus/nubus.c
Merge tag 'mvebu-dt64-4.12-3' of git://git.infradead.org/linux-mvebu into fixes
[mirror_ubuntu-artful-kernel.git] / drivers / nubus / nubus.c
CommitLineData
1da177e4
LT
1/*
2 * Macintosh Nubus Interface Code
3 *
4 * Originally by Alan Cox
5 *
6 * Mostly rewritten by David Huggins-Daines, C. Scott Ananian,
7 * and others.
8 */
9
1da177e4
LT
10#include <linux/types.h>
11#include <linux/kernel.h>
12#include <linux/string.h>
13#include <linux/nubus.h>
14#include <linux/errno.h>
15#include <linux/init.h>
16#include <linux/delay.h>
99ffab81 17#include <linux/module.h>
5a0e3ad6 18#include <linux/slab.h>
1da177e4 19#include <asm/setup.h>
1da177e4
LT
20#include <asm/page.h>
21#include <asm/hwtest.h>
1da177e4
LT
22#include <asm/mac_via.h>
23#include <asm/mac_oss.h>
24
25extern void via_nubus_init(void);
26extern void oss_nubus_init(void);
27
28/* Constants */
29
30/* This is, of course, the size in bytelanes, rather than the size in
31 actual bytes */
32#define FORMAT_BLOCK_SIZE 20
33#define ROM_DIR_OFFSET 0x24
34
35#define NUBUS_TEST_PATTERN 0x5A932BC7
36
37/* Define this if you like to live dangerously - it is known not to
38 work on pretty much every machine except the Quadra 630 and the LC
39 III. */
40#undef I_WANT_TO_PROBE_SLOT_ZERO
41
42/* This sometimes helps combat failure to boot */
43#undef TRY_TO_DODGE_WSOD
44
45/* Globals */
46
f42e5550
FT
47struct nubus_dev *nubus_devices;
48struct nubus_board *nubus_boards;
1da177e4
LT
49
50/* Meaning of "bytelanes":
51
52 The card ROM may appear on any or all bytes of each long word in
53 NuBus memory. The low 4 bits of the "map" value found in the
54 format block (at the top of the slot address space, as well as at
55 the top of the MacOS ROM) tells us which bytelanes, i.e. which byte
56 offsets within each longword, are valid. Thus:
57
58 A map of 0x0f, as found in the MacOS ROM, means that all bytelanes
59 are valid.
60
61 A map of 0xf0 means that no bytelanes are valid (We pray that we
62 will never encounter this, but stranger things have happened)
63
64 A map of 0xe1 means that only the MSB of each long word is actually
65 part of the card ROM. (We hope to never encounter NuBus on a
66 little-endian machine. Again, stranger things have happened)
67
68 A map of 0x78 means that only the LSB of each long word is valid.
69
70 Etcetera, etcetera. Hopefully this clears up some confusion over
71 what the following code actually does. */
f42e5550 72
1da177e4
LT
73static inline int not_useful(void *p, int map)
74{
f42e5550
FT
75 unsigned long pv = (unsigned long)p;
76
1da177e4 77 pv &= 3;
f42e5550 78 if (map & (1 << pv))
1da177e4
LT
79 return 0;
80 return 1;
81}
f42e5550 82
1da177e4
LT
83static unsigned long nubus_get_rom(unsigned char **ptr, int len, int map)
84{
85 /* This will hold the result */
86 unsigned long v = 0;
87 unsigned char *p = *ptr;
88
f42e5550 89 while (len) {
1da177e4 90 v <<= 8;
f42e5550 91 while (not_useful(p, map))
1da177e4
LT
92 p++;
93 v |= *p++;
94 len--;
95 }
96 *ptr = p;
97 return v;
98}
99
100static void nubus_rewind(unsigned char **ptr, int len, int map)
101{
f42e5550 102 unsigned char *p = *ptr;
1da177e4
LT
103
104 /* Sanity check */
f42e5550 105 if (len > 65536)
71ae40e4 106 pr_err("rewind of 0x%08x!\n", len);
f42e5550
FT
107 while (len) {
108 do {
1da177e4 109 p--;
f42e5550 110 } while (not_useful(p, map));
1da177e4
LT
111 len--;
112 }
f42e5550 113 *ptr = p;
1da177e4
LT
114}
115
116static void nubus_advance(unsigned char **ptr, int len, int map)
117{
118 unsigned char *p = *ptr;
f42e5550
FT
119
120 if (len > 65536)
71ae40e4 121 pr_err("advance of 0x%08x!\n", len);
f42e5550
FT
122 while (len) {
123 while (not_useful(p, map))
1da177e4 124 p++;
2e0eb731 125 p++;
1da177e4
LT
126 len--;
127 }
128 *ptr = p;
129}
130
131static void nubus_move(unsigned char **ptr, int len, int map)
132{
f42e5550 133 if (len > 0)
1da177e4 134 nubus_advance(ptr, len, map);
f42e5550 135 else if (len < 0)
1da177e4
LT
136 nubus_rewind(ptr, -len, map);
137}
138
139/* Now, functions to read the sResource tree */
140
141/* Each sResource entry consists of a 1-byte ID and a 3-byte data
142 field. If that data field contains an offset, then obviously we
143 have to expand it from a 24-bit signed number to a 32-bit signed
144 number. */
145
146static inline long nubus_expand32(long foo)
147{
f42e5550 148 if (foo & 0x00800000) /* 24bit negative */
1da177e4
LT
149 foo |= 0xFF000000;
150 return foo;
151}
152
153static inline void *nubus_rom_addr(int slot)
f42e5550 154{
1da177e4
LT
155 /*
156 * Returns the first byte after the card. We then walk
157 * backwards to get the lane register and the config
158 */
f42e5550 159 return (void *)(0xF1000000 + (slot << 24));
1da177e4
LT
160}
161
162static unsigned char *nubus_dirptr(const struct nubus_dirent *nd)
163{
164 unsigned char *p = nd->base;
f42e5550 165
1da177e4
LT
166 /* Essentially, just step over the bytelanes using whatever
167 offset we might have found */
168 nubus_move(&p, nubus_expand32(nd->data), nd->mask);
169 /* And return the value */
170 return p;
171}
172
173/* These two are for pulling resource data blocks (i.e. stuff that's
174 pointed to with offsets) out of the card ROM. */
175
f42e5550 176void nubus_get_rsrc_mem(void *dest, const struct nubus_dirent *dirent,
1da177e4
LT
177 int len)
178{
179 unsigned char *t = (unsigned char *)dest;
180 unsigned char *p = nubus_dirptr(dirent);
f42e5550
FT
181
182 while (len) {
1da177e4
LT
183 *t++ = nubus_get_rom(&p, 1, dirent->mask);
184 len--;
185 }
186}
99ffab81 187EXPORT_SYMBOL(nubus_get_rsrc_mem);
1da177e4 188
f42e5550 189void nubus_get_rsrc_str(void *dest, const struct nubus_dirent *dirent,
1da177e4
LT
190 int len)
191{
f42e5550 192 unsigned char *t = (unsigned char *)dest;
1da177e4 193 unsigned char *p = nubus_dirptr(dirent);
f42e5550
FT
194
195 while (len) {
1da177e4 196 *t = nubus_get_rom(&p, 1, dirent->mask);
f42e5550 197 if (!*t++)
1da177e4
LT
198 break;
199 len--;
200 }
201}
99ffab81 202EXPORT_SYMBOL(nubus_get_rsrc_str);
1da177e4 203
f42e5550
FT
204int nubus_get_root_dir(const struct nubus_board *board,
205 struct nubus_dir *dir)
1da177e4
LT
206{
207 dir->ptr = dir->base = board->directory;
208 dir->done = 0;
209 dir->mask = board->lanes;
210 return 0;
211}
99ffab81 212EXPORT_SYMBOL(nubus_get_root_dir);
1da177e4
LT
213
214/* This is a slyly renamed version of the above */
f42e5550
FT
215int nubus_get_func_dir(const struct nubus_dev *dev,
216 struct nubus_dir *dir)
1da177e4
LT
217{
218 dir->ptr = dir->base = dev->directory;
219 dir->done = 0;
220 dir->mask = dev->board->lanes;
221 return 0;
222}
99ffab81 223EXPORT_SYMBOL(nubus_get_func_dir);
1da177e4 224
f42e5550
FT
225int nubus_get_board_dir(const struct nubus_board *board,
226 struct nubus_dir *dir)
1da177e4
LT
227{
228 struct nubus_dirent ent;
f42e5550 229
1da177e4
LT
230 dir->ptr = dir->base = board->directory;
231 dir->done = 0;
232 dir->mask = board->lanes;
233
234 /* Now dereference it (the first directory is always the board
235 directory) */
236 if (nubus_readdir(dir, &ent) == -1)
237 return -1;
238 if (nubus_get_subdir(&ent, dir) == -1)
239 return -1;
240 return 0;
241}
99ffab81 242EXPORT_SYMBOL(nubus_get_board_dir);
1da177e4
LT
243
244int nubus_get_subdir(const struct nubus_dirent *ent,
245 struct nubus_dir *dir)
246{
247 dir->ptr = dir->base = nubus_dirptr(ent);
248 dir->done = 0;
249 dir->mask = ent->mask;
250 return 0;
251}
99ffab81 252EXPORT_SYMBOL(nubus_get_subdir);
1da177e4
LT
253
254int nubus_readdir(struct nubus_dir *nd, struct nubus_dirent *ent)
255{
256 u32 resid;
f42e5550 257
1da177e4
LT
258 if (nd->done)
259 return -1;
260
261 /* Do this first, otherwise nubus_rewind & co are off by 4 */
262 ent->base = nd->ptr;
263
264 /* This moves nd->ptr forward */
265 resid = nubus_get_rom(&nd->ptr, 4, nd->mask);
266
267 /* EOL marker, as per the Apple docs */
f42e5550 268 if ((resid & 0xff000000) == 0xff000000) {
1da177e4
LT
269 /* Mark it as done */
270 nd->done = 1;
271 return -1;
272 }
273
274 /* First byte is the resource ID */
f42e5550 275 ent->type = resid >> 24;
1da177e4
LT
276 /* Low 3 bytes might contain data (or might not) */
277 ent->data = resid & 0xffffff;
f42e5550 278 ent->mask = nd->mask;
1da177e4
LT
279 return 0;
280}
99ffab81 281EXPORT_SYMBOL(nubus_readdir);
1da177e4 282
f42e5550 283int nubus_rewinddir(struct nubus_dir *dir)
1da177e4
LT
284{
285 dir->ptr = dir->base;
e36b9913 286 dir->done = 0;
1da177e4
LT
287 return 0;
288}
99ffab81 289EXPORT_SYMBOL(nubus_rewinddir);
1da177e4
LT
290
291/* Driver interface functions, more or less like in pci.c */
292
293struct nubus_dev*
f42e5550
FT
294nubus_find_device(unsigned short category, unsigned short type,
295 unsigned short dr_hw, unsigned short dr_sw,
296 const struct nubus_dev *from)
1da177e4 297{
f42e5550 298 struct nubus_dev *itor = from ? from->next : nubus_devices;
1da177e4
LT
299
300 while (itor) {
f42e5550
FT
301 if (itor->category == category && itor->type == type &&
302 itor->dr_hw == dr_hw && itor->dr_sw == dr_sw)
1da177e4
LT
303 return itor;
304 itor = itor->next;
305 }
306 return NULL;
307}
99ffab81 308EXPORT_SYMBOL(nubus_find_device);
1da177e4
LT
309
310struct nubus_dev*
f42e5550
FT
311nubus_find_type(unsigned short category, unsigned short type,
312 const struct nubus_dev *from)
1da177e4 313{
f42e5550 314 struct nubus_dev *itor = from ? from->next : nubus_devices;
1da177e4
LT
315
316 while (itor) {
f42e5550 317 if (itor->category == category && itor->type == type)
1da177e4
LT
318 return itor;
319 itor = itor->next;
320 }
321 return NULL;
322}
99ffab81 323EXPORT_SYMBOL(nubus_find_type);
1da177e4
LT
324
325struct nubus_dev*
f42e5550 326nubus_find_slot(unsigned int slot, const struct nubus_dev *from)
1da177e4 327{
f42e5550
FT
328 struct nubus_dev *itor = from ? from->next : nubus_devices;
329
1da177e4
LT
330 while (itor) {
331 if (itor->board->slot == slot)
332 return itor;
333 itor = itor->next;
334 }
335 return NULL;
336}
99ffab81 337EXPORT_SYMBOL(nubus_find_slot);
1da177e4
LT
338
339int
f42e5550
FT
340nubus_find_rsrc(struct nubus_dir *dir, unsigned char rsrc_type,
341 struct nubus_dirent *ent)
1da177e4
LT
342{
343 while (nubus_readdir(dir, ent) != -1) {
344 if (ent->type == rsrc_type)
345 return 0;
f42e5550 346 }
1da177e4
LT
347 return -1;
348}
99ffab81 349EXPORT_SYMBOL(nubus_find_rsrc);
1da177e4
LT
350
351/* Initialization functions - decide which slots contain stuff worth
352 looking at, and print out lots and lots of information from the
353 resource blocks. */
354
355/* FIXME: A lot of this stuff will eventually be useful after
081985ac 356 initialization, for intelligently probing Ethernet and video chips,
1da177e4
LT
357 among other things. The rest of it should go in the /proc code.
358 For now, we just use it to give verbose boot logs. */
359
f42e5550
FT
360static int __init nubus_show_display_resource(struct nubus_dev *dev,
361 const struct nubus_dirent *ent)
1da177e4
LT
362{
363 switch (ent->type) {
364 case NUBUS_RESID_GAMMADIR:
71ae40e4 365 pr_info(" gamma directory offset: 0x%06x\n", ent->data);
1da177e4
LT
366 break;
367 case 0x0080 ... 0x0085:
71ae40e4 368 pr_info(" mode %02X info offset: 0x%06x\n",
1da177e4
LT
369 ent->type, ent->data);
370 break;
371 default:
71ae40e4 372 pr_info(" unknown resource %02X, data 0x%06x\n",
1da177e4
LT
373 ent->type, ent->data);
374 }
375 return 0;
376}
377
f42e5550
FT
378static int __init nubus_show_network_resource(struct nubus_dev *dev,
379 const struct nubus_dirent *ent)
1da177e4
LT
380{
381 switch (ent->type) {
382 case NUBUS_RESID_MAC_ADDRESS:
383 {
384 char addr[6];
f42e5550 385
1da177e4 386 nubus_get_rsrc_mem(addr, ent, 6);
71ae40e4 387 pr_info(" MAC address: %pM\n", addr);
1da177e4
LT
388 break;
389 }
390 default:
71ae40e4 391 pr_info(" unknown resource %02X, data 0x%06x\n",
1da177e4
LT
392 ent->type, ent->data);
393 }
394 return 0;
395}
396
f42e5550
FT
397static int __init nubus_show_cpu_resource(struct nubus_dev *dev,
398 const struct nubus_dirent *ent)
1da177e4
LT
399{
400 switch (ent->type) {
401 case NUBUS_RESID_MEMINFO:
402 {
403 unsigned long meminfo[2];
f42e5550 404
1da177e4 405 nubus_get_rsrc_mem(&meminfo, ent, 8);
71ae40e4 406 pr_info(" memory: [ 0x%08lx 0x%08lx ]\n",
1da177e4
LT
407 meminfo[0], meminfo[1]);
408 break;
409 }
410 case NUBUS_RESID_ROMINFO:
411 {
412 unsigned long rominfo[2];
f42e5550 413
1da177e4 414 nubus_get_rsrc_mem(&rominfo, ent, 8);
71ae40e4 415 pr_info(" ROM: [ 0x%08lx 0x%08lx ]\n",
1da177e4
LT
416 rominfo[0], rominfo[1]);
417 break;
418 }
419 default:
71ae40e4 420 pr_info(" unknown resource %02X, data 0x%06x\n",
1da177e4
LT
421 ent->type, ent->data);
422 }
423 return 0;
424}
425
f42e5550
FT
426static int __init nubus_show_private_resource(struct nubus_dev *dev,
427 const struct nubus_dirent *ent)
1da177e4
LT
428{
429 switch (dev->category) {
430 case NUBUS_CAT_DISPLAY:
431 nubus_show_display_resource(dev, ent);
432 break;
433 case NUBUS_CAT_NETWORK:
434 nubus_show_network_resource(dev, ent);
435 break;
436 case NUBUS_CAT_CPU:
437 nubus_show_cpu_resource(dev, ent);
438 break;
439 default:
71ae40e4 440 pr_info(" unknown resource %02X, data 0x%06x\n",
1da177e4
LT
441 ent->type, ent->data);
442 }
443 return 0;
444}
445
f42e5550
FT
446static struct nubus_dev * __init
447nubus_get_functional_resource(struct nubus_board *board, int slot,
448 const struct nubus_dirent *parent)
1da177e4 449{
f42e5550 450 struct nubus_dir dir;
1da177e4 451 struct nubus_dirent ent;
f42e5550
FT
452 struct nubus_dev *dev;
453
71ae40e4 454 pr_info(" Function 0x%02x:\n", parent->type);
1da177e4
LT
455 nubus_get_subdir(parent, &dir);
456
457 /* Apple seems to have botched the ROM on the IIx */
458 if (slot == 0 && (unsigned long)dir.base % 2)
459 dir.base += 1;
f42e5550 460
71ae40e4
DHD
461 pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
462 __func__, parent->base, dir.base);
1da177e4
LT
463
464 /* Actually we should probably panic if this fails */
dd00cc48 465 if ((dev = kzalloc(sizeof(*dev), GFP_ATOMIC)) == NULL)
f42e5550 466 return NULL;
1da177e4
LT
467 dev->resid = parent->type;
468 dev->directory = dir.base;
469 dev->board = board;
f42e5550
FT
470
471 while (nubus_readdir(&dir, &ent) != -1) {
472 switch (ent.type) {
1da177e4
LT
473 case NUBUS_RESID_TYPE:
474 {
475 unsigned short nbtdata[4];
f42e5550 476
1da177e4
LT
477 nubus_get_rsrc_mem(nbtdata, &ent, 8);
478 dev->category = nbtdata[0];
479 dev->type = nbtdata[1];
480 dev->dr_sw = nbtdata[2];
481 dev->dr_hw = nbtdata[3];
71ae40e4
DHD
482 pr_info(" type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
483 nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
1da177e4
LT
484 break;
485 }
486 case NUBUS_RESID_NAME:
487 {
488 nubus_get_rsrc_str(dev->name, &ent, 64);
71ae40e4 489 pr_info(" name: %s\n", dev->name);
1da177e4
LT
490 break;
491 }
492 case NUBUS_RESID_DRVRDIR:
493 {
494 /* MacOS driver. If we were NetBSD we might
495 use this :-) */
496 struct nubus_dir drvr_dir;
497 struct nubus_dirent drvr_ent;
f42e5550 498
1da177e4
LT
499 nubus_get_subdir(&ent, &drvr_dir);
500 nubus_readdir(&drvr_dir, &drvr_ent);
501 dev->driver = nubus_dirptr(&drvr_ent);
71ae40e4 502 pr_info(" driver at: 0x%p\n", dev->driver);
1da177e4
LT
503 break;
504 }
505 case NUBUS_RESID_MINOR_BASEOS:
506 /* We will need this in order to support
507 multiple framebuffers. It might be handy
508 for Ethernet as well */
509 nubus_get_rsrc_mem(&dev->iobase, &ent, 4);
71ae40e4 510 pr_info(" memory offset: 0x%08lx\n", dev->iobase);
1da177e4
LT
511 break;
512 case NUBUS_RESID_MINOR_LENGTH:
513 /* Ditto */
514 nubus_get_rsrc_mem(&dev->iosize, &ent, 4);
71ae40e4 515 pr_info(" memory length: 0x%08lx\n", dev->iosize);
f42e5550 516 break;
1da177e4
LT
517 case NUBUS_RESID_FLAGS:
518 dev->flags = ent.data;
71ae40e4 519 pr_info(" flags: 0x%06x\n", dev->flags);
1da177e4
LT
520 break;
521 case NUBUS_RESID_HWDEVID:
522 dev->hwdevid = ent.data;
71ae40e4 523 pr_info(" hwdevid: 0x%06x\n", dev->hwdevid);
1da177e4
LT
524 break;
525 default:
526 /* Local/Private resources have their own
527 function */
528 nubus_show_private_resource(dev, &ent);
529 }
530 }
f42e5550 531
1da177e4
LT
532 return dev;
533}
534
535/* This is cool. */
f42e5550
FT
536static int __init nubus_get_vidnames(struct nubus_board *board,
537 const struct nubus_dirent *parent)
1da177e4 538{
f42e5550 539 struct nubus_dir dir;
1da177e4 540 struct nubus_dirent ent;
f42e5550 541
1da177e4
LT
542 /* FIXME: obviously we want to put this in a header file soon */
543 struct vidmode {
544 u32 size;
545 /* Don't know what this is yet */
546 u16 id;
547 /* Longest one I've seen so far is 26 characters */
548 char name[32];
549 };
550
71ae40e4 551 pr_info(" video modes supported:\n");
1da177e4 552 nubus_get_subdir(parent, &dir);
71ae40e4
DHD
553 pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
554 __func__, parent->base, dir.base);
1da177e4 555
f42e5550 556 while (nubus_readdir(&dir, &ent) != -1) {
1da177e4
LT
557 struct vidmode mode;
558 u32 size;
559
560 /* First get the length */
561 nubus_get_rsrc_mem(&size, &ent, 4);
f42e5550 562
1da177e4
LT
563 /* Now clobber the whole thing */
564 if (size > sizeof(mode) - 1)
565 size = sizeof(mode) - 1;
566 memset(&mode, 0, sizeof(mode));
567 nubus_get_rsrc_mem(&mode, &ent, size);
71ae40e4 568 pr_info(" %02X: (%02X) %s\n", ent.type,
1da177e4
LT
569 mode.id, mode.name);
570 }
571 return 0;
572}
573
574/* This is *really* cool. */
f42e5550
FT
575static int __init nubus_get_icon(struct nubus_board *board,
576 const struct nubus_dirent *ent)
1da177e4
LT
577{
578 /* Should be 32x32 if my memory serves me correctly */
579 unsigned char icon[128];
580 int x, y;
f42e5550 581
1da177e4 582 nubus_get_rsrc_mem(&icon, ent, 128);
71ae40e4 583 pr_info(" icon:\n");
1da177e4
LT
584
585 /* We should actually plot these somewhere in the framebuffer
586 init. This is just to demonstrate that they do, in fact,
587 exist */
588 for (y = 0; y < 32; y++) {
71ae40e4 589 pr_info(" ");
1da177e4 590 for (x = 0; x < 32; x++) {
f42e5550 591 if (icon[y * 4 + x / 8] & (0x80 >> (x % 8)))
71ae40e4 592 pr_cont("*");
1da177e4 593 else
71ae40e4 594 pr_cont(" ");
1da177e4 595 }
71ae40e4 596 pr_cont("\n");
1da177e4
LT
597 }
598 return 0;
599}
600
f42e5550
FT
601static int __init nubus_get_vendorinfo(struct nubus_board *board,
602 const struct nubus_dirent *parent)
1da177e4 603{
f42e5550 604 struct nubus_dir dir;
1da177e4 605 struct nubus_dirent ent;
f42e5550
FT
606 static char *vendor_fields[6] = { "ID", "serial", "revision",
607 "part", "date", "unknown field" };
1da177e4 608
71ae40e4 609 pr_info(" vendor info:\n");
1da177e4 610 nubus_get_subdir(parent, &dir);
71ae40e4
DHD
611 pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
612 __func__, parent->base, dir.base);
1da177e4 613
f42e5550 614 while (nubus_readdir(&dir, &ent) != -1) {
1da177e4 615 char name[64];
f42e5550 616
1da177e4
LT
617 /* These are all strings, we think */
618 nubus_get_rsrc_str(name, &ent, 64);
619 if (ent.type > 5)
620 ent.type = 5;
71ae40e4 621 pr_info(" %s: %s\n", vendor_fields[ent.type - 1], name);
1da177e4
LT
622 }
623 return 0;
624}
625
f42e5550
FT
626static int __init nubus_get_board_resource(struct nubus_board *board, int slot,
627 const struct nubus_dirent *parent)
1da177e4 628{
f42e5550 629 struct nubus_dir dir;
1da177e4 630 struct nubus_dirent ent;
f42e5550 631
1da177e4 632 nubus_get_subdir(parent, &dir);
71ae40e4
DHD
633 pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
634 __func__, parent->base, dir.base);
1da177e4 635
f42e5550 636 while (nubus_readdir(&dir, &ent) != -1) {
1da177e4
LT
637 switch (ent.type) {
638 case NUBUS_RESID_TYPE:
639 {
640 unsigned short nbtdata[4];
641 /* This type is always the same, and is not
642 useful except insofar as it tells us that
643 we really are looking at a board resource. */
644 nubus_get_rsrc_mem(nbtdata, &ent, 8);
71ae40e4
DHD
645 pr_info(" type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
646 nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
1da177e4
LT
647 if (nbtdata[0] != 1 || nbtdata[1] != 0 ||
648 nbtdata[2] != 0 || nbtdata[3] != 0)
71ae40e4 649 pr_err("this sResource is not a board resource!\n");
1da177e4
LT
650 break;
651 }
652 case NUBUS_RESID_NAME:
653 nubus_get_rsrc_str(board->name, &ent, 64);
71ae40e4 654 pr_info(" name: %s\n", board->name);
1da177e4
LT
655 break;
656 case NUBUS_RESID_ICON:
657 nubus_get_icon(board, &ent);
658 break;
659 case NUBUS_RESID_BOARDID:
71ae40e4 660 pr_info(" board id: 0x%x\n", ent.data);
1da177e4
LT
661 break;
662 case NUBUS_RESID_PRIMARYINIT:
71ae40e4 663 pr_info(" primary init offset: 0x%06x\n", ent.data);
1da177e4
LT
664 break;
665 case NUBUS_RESID_VENDORINFO:
666 nubus_get_vendorinfo(board, &ent);
667 break;
668 case NUBUS_RESID_FLAGS:
71ae40e4 669 pr_info(" flags: 0x%06x\n", ent.data);
1da177e4
LT
670 break;
671 case NUBUS_RESID_HWDEVID:
71ae40e4 672 pr_info(" hwdevid: 0x%06x\n", ent.data);
1da177e4
LT
673 break;
674 case NUBUS_RESID_SECONDINIT:
71ae40e4 675 pr_info(" secondary init offset: 0x%06x\n", ent.data);
1da177e4 676 break;
f42e5550 677 /* WTF isn't this in the functional resources? */
1da177e4
LT
678 case NUBUS_RESID_VIDNAMES:
679 nubus_get_vidnames(board, &ent);
680 break;
681 /* Same goes for this */
682 case NUBUS_RESID_VIDMODES:
71ae40e4 683 pr_info(" video mode parameter directory offset: 0x%06x\n",
1da177e4 684 ent.data);
f42e5550 685 break;
1da177e4 686 default:
71ae40e4 687 pr_info(" unknown resource %02X, data 0x%06x\n",
1da177e4
LT
688 ent.type, ent.data);
689 }
690 }
691 return 0;
692}
693
694/* Attempt to bypass the somewhat non-obvious arrangement of
695 sResources in the motherboard ROM */
696static void __init nubus_find_rom_dir(struct nubus_board* board)
697{
f42e5550
FT
698 unsigned char *rp;
699 unsigned char *romdir;
1da177e4
LT
700 struct nubus_dir dir;
701 struct nubus_dirent ent;
702
703 /* Check for the extra directory just under the format block */
704 rp = board->fblock;
705 nubus_rewind(&rp, 4, board->lanes);
706 if (nubus_get_rom(&rp, 4, board->lanes) != NUBUS_TEST_PATTERN) {
707 /* OK, the ROM was telling the truth */
708 board->directory = board->fblock;
709 nubus_move(&board->directory,
710 nubus_expand32(board->doffset),
711 board->lanes);
712 return;
713 }
714
715 /* On "slot zero", you have to walk down a few more
716 directories to get to the equivalent of a real card's root
717 directory. We don't know what they were smoking when they
718 came up with this. */
719 romdir = nubus_rom_addr(board->slot);
720 nubus_rewind(&romdir, ROM_DIR_OFFSET, board->lanes);
721 dir.base = dir.ptr = romdir;
722 dir.done = 0;
723 dir.mask = board->lanes;
724
725 /* This one points to an "Unknown Macintosh" directory */
726 if (nubus_readdir(&dir, &ent) == -1)
727 goto badrom;
728
a8fe19eb 729 if (console_loglevel >= CONSOLE_LOGLEVEL_DEBUG)
1da177e4
LT
730 printk(KERN_INFO "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
731 /* This one takes us to where we want to go. */
f42e5550 732 if (nubus_readdir(&dir, &ent) == -1)
1da177e4 733 goto badrom;
a8fe19eb 734 if (console_loglevel >= CONSOLE_LOGLEVEL_DEBUG)
1da177e4
LT
735 printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
736 nubus_get_subdir(&ent, &dir);
737
738 /* Resource ID 01, also an "Unknown Macintosh" */
f42e5550 739 if (nubus_readdir(&dir, &ent) == -1)
1da177e4 740 goto badrom;
a8fe19eb 741 if (console_loglevel >= CONSOLE_LOGLEVEL_DEBUG)
1da177e4
LT
742 printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
743
744 /* FIXME: the first one is *not* always the right one. We
745 suspect this has something to do with the ROM revision.
746 "The HORROR ROM" (LC-series) uses 0x7e, while "The HORROR
747 Continues" (Q630) uses 0x7b. The DAFB Macs evidently use
748 something else. Please run "Slots" on your Mac (see
749 include/linux/nubus.h for where to get this program) and
750 tell us where the 'SiDirPtr' for Slot 0 is. If you feel
751 brave, you should also use MacsBug to walk down the ROM
752 directories like this function does and try to find the
753 path to that address... */
754 if (nubus_readdir(&dir, &ent) == -1)
755 goto badrom;
a8fe19eb 756 if (console_loglevel >= CONSOLE_LOGLEVEL_DEBUG)
1da177e4 757 printk(KERN_DEBUG "nubus_get_rom_dir: entry %02x %06x\n", ent.type, ent.data);
f42e5550 758
1da177e4
LT
759 /* Bwahahahaha... */
760 nubus_get_subdir(&ent, &dir);
761 board->directory = dir.base;
762 return;
f42e5550 763
1da177e4
LT
764 /* Even more evil laughter... */
765 badrom:
766 board->directory = board->fblock;
767 nubus_move(&board->directory, nubus_expand32(board->doffset), board->lanes);
768 printk(KERN_ERR "nubus_get_rom_dir: ROM weirdness! Notify the developers...\n");
769}
770
771/* Add a board (might be many devices) to the list */
f42e5550 772static struct nubus_board * __init nubus_add_board(int slot, int bytelanes)
1da177e4 773{
f42e5550
FT
774 struct nubus_board *board;
775 struct nubus_board **boardp;
1da177e4
LT
776 unsigned char *rp;
777 unsigned long dpat;
778 struct nubus_dir dir;
779 struct nubus_dirent ent;
780
781 /* Move to the start of the format block */
f42e5550 782 rp = nubus_rom_addr(slot);
1da177e4
LT
783 nubus_rewind(&rp, FORMAT_BLOCK_SIZE, bytelanes);
784
785 /* Actually we should probably panic if this fails */
dd00cc48 786 if ((board = kzalloc(sizeof(*board), GFP_ATOMIC)) == NULL)
f42e5550 787 return NULL;
1da177e4
LT
788 board->fblock = rp;
789
790 /* Dump the format block for debugging purposes */
71ae40e4
DHD
791 pr_debug("Slot %X, format block at 0x%p:\n", slot, rp);
792 pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
793 pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
794 pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
795 pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
796 pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
797 pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
798 pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
799 pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
800 rp = board->fblock;
801
1da177e4 802 board->slot = slot;
f42e5550 803 board->slot_addr = (unsigned long)nubus_slot_addr(slot);
1da177e4
LT
804 board->doffset = nubus_get_rom(&rp, 4, bytelanes);
805 /* rom_length is *supposed* to be the total length of the
806 * ROM. In practice it is the "amount of ROM used to compute
807 * the CRC." So some jokers decide to set it to zero and
808 * set the crc to zero so they don't have to do any math.
809 * See the Performa 460 ROM, for example. Those Apple "engineers".
810 */
811 board->rom_length = nubus_get_rom(&rp, 4, bytelanes);
812 board->crc = nubus_get_rom(&rp, 4, bytelanes);
813 board->rev = nubus_get_rom(&rp, 1, bytelanes);
f42e5550 814 board->format = nubus_get_rom(&rp, 1, bytelanes);
1da177e4
LT
815 board->lanes = bytelanes;
816
817 /* Directory offset should be small and negative... */
f42e5550 818 if (!(board->doffset & 0x00FF0000))
71ae40e4 819 pr_warn("Dodgy doffset!\n");
1da177e4 820 dpat = nubus_get_rom(&rp, 4, bytelanes);
f42e5550 821 if (dpat != NUBUS_TEST_PATTERN)
71ae40e4 822 pr_warn("Wrong test pattern %08lx!\n", dpat);
f42e5550 823
1da177e4
LT
824 /*
825 * I wonder how the CRC is meant to work -
826 * any takers ?
827 * CSA: According to MAC docs, not all cards pass the CRC anyway,
828 * since the initial Macintosh ROM releases skipped the check.
829 */
830
831 /* Attempt to work around slot zero weirdness */
832 nubus_find_rom_dir(board);
f42e5550 833 nubus_get_root_dir(board, &dir);
1da177e4
LT
834
835 /* We're ready to rock */
71ae40e4 836 pr_info("Slot %X:\n", slot);
1da177e4
LT
837
838 /* Each slot should have one board resource and any number of
839 functional resources. So we'll fill in some fields in the
840 struct nubus_board from the board resource, then walk down
841 the list of functional resources, spinning out a nubus_dev
842 for each of them. */
843 if (nubus_readdir(&dir, &ent) == -1) {
844 /* We can't have this! */
71ae40e4 845 pr_err("Board resource not found!\n");
1da177e4
LT
846 return NULL;
847 } else {
71ae40e4 848 pr_info(" Board resource:\n");
1da177e4
LT
849 nubus_get_board_resource(board, slot, &ent);
850 }
851
852 /* Aaaarrrrgghh! The LC III motherboard has *two* board
853 resources. I have no idea WTF to do about this. */
854
855 while (nubus_readdir(&dir, &ent) != -1) {
f42e5550
FT
856 struct nubus_dev *dev;
857 struct nubus_dev **devp;
858
1da177e4
LT
859 dev = nubus_get_functional_resource(board, slot, &ent);
860 if (dev == NULL)
861 continue;
862
863 /* We zeroed this out above */
864 if (board->first_dev == NULL)
865 board->first_dev = dev;
f42e5550 866
1da177e4 867 /* Put it on the global NuBus device chain. Keep entries in order. */
f42e5550
FT
868 for (devp = &nubus_devices; *devp != NULL;
869 devp = &((*devp)->next))
1da177e4
LT
870 /* spin */;
871 *devp = dev;
f42e5550 872 dev->next = NULL;
1da177e4
LT
873 }
874
875 /* Put it on the global NuBus board chain. Keep entries in order. */
f42e5550
FT
876 for (boardp = &nubus_boards; *boardp != NULL;
877 boardp = &((*boardp)->next))
1da177e4
LT
878 /* spin */;
879 *boardp = board;
880 board->next = NULL;
f42e5550 881
1da177e4
LT
882 return board;
883}
884
885void __init nubus_probe_slot(int slot)
886{
887 unsigned char dp;
f42e5550 888 unsigned char *rp;
1da177e4
LT
889 int i;
890
f42e5550
FT
891 rp = nubus_rom_addr(slot);
892 for (i = 4; i; i--) {
1da177e4
LT
893 int card_present;
894
895 rp--;
1da177e4 896 card_present = hwreg_present(rp);
1da177e4
LT
897 if (!card_present)
898 continue;
899
1da177e4
LT
900 dp = *rp;
901 if(dp == 0)
902 continue;
903
904 /* The last byte of the format block consists of two
905 nybbles which are "mirror images" of each other.
906 These show us the valid bytelanes */
f42e5550 907 if ((((dp >> 4) ^ dp) & 0x0F) != 0x0F)
1da177e4
LT
908 continue;
909 /* Check that this value is actually *on* one of the
910 bytelanes it claims are valid! */
f42e5550 911 if ((dp & 0x0F) >= (1 << i))
1da177e4
LT
912 continue;
913
914 /* Looks promising. Let's put it on the list. */
915 nubus_add_board(slot, dp);
916
917 return;
918 }
919}
920
1da177e4
LT
921void __init nubus_scan_bus(void)
922{
923 int slot;
f42e5550 924
1da177e4
LT
925 /* This might not work on your machine */
926#ifdef I_WANT_TO_PROBE_SLOT_ZERO
927 nubus_probe_slot(0);
928#endif
f42e5550 929 for (slot = 9; slot < 15; slot++) {
1da177e4
LT
930 nubus_probe_slot(slot);
931 }
932}
933
934static int __init nubus_init(void)
935{
f42e5550 936 if (!MACH_IS_MAC)
1da177e4
LT
937 return 0;
938
939 /* Initialize the NuBus interrupts */
940 if (oss_present) {
941 oss_nubus_init();
942 } else {
943 via_nubus_init();
944 }
945
946#ifdef TRY_TO_DODGE_WSOD
947 /* Rogue Ethernet interrupts can kill the machine if we don't
948 do this. Obviously this is bogus. Hopefully the local VIA
949 gurus can fix the real cause of the problem. */
950 mdelay(1000);
951#endif
f42e5550 952
1da177e4 953 /* And probe */
71ae40e4 954 pr_info("NuBus: Scanning NuBus slots.\n");
1da177e4 955 nubus_devices = NULL;
f42e5550 956 nubus_boards = NULL;
1da177e4 957 nubus_scan_bus();
1da177e4 958 nubus_proc_init();
1da177e4
LT
959 return 0;
960}
961
962subsys_initcall(nubus_init);