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