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