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