2 * Macintosh Nubus Interface Code
4 * Originally by Alan Cox
6 * Mostly rewritten by David Huggins-Daines, C. Scott Ananian,
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>
20 #include <asm/hwtest.h>
21 #include <asm/mac_via.h>
22 #include <asm/mac_oss.h>
24 extern void via_nubus_init(void);
25 extern void oss_nubus_init(void);
29 /* This is, of course, the size in bytelanes, rather than the size in
31 #define FORMAT_BLOCK_SIZE 20
32 #define ROM_DIR_OFFSET 0x24
34 #define NUBUS_TEST_PATTERN 0x5A932BC7
38 struct nubus_dev
*nubus_devices
;
39 struct nubus_board
*nubus_boards
;
41 /* Meaning of "bytelanes":
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:
49 A map of 0x0f, as found in the MacOS ROM, means that all bytelanes
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)
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)
59 A map of 0x78 means that only the LSB of each long word is valid.
61 Etcetera, etcetera. Hopefully this clears up some confusion over
62 what the following code actually does. */
64 static inline int not_useful(void *p
, int map
)
66 unsigned long pv
= (unsigned long)p
;
74 static unsigned long nubus_get_rom(unsigned char **ptr
, int len
, int map
)
76 /* This will hold the result */
78 unsigned char *p
= *ptr
;
82 while (not_useful(p
, map
))
91 static void nubus_rewind(unsigned char **ptr
, int len
, int map
)
93 unsigned char *p
= *ptr
;
98 } while (not_useful(p
, map
));
104 static void nubus_advance(unsigned char **ptr
, int len
, int map
)
106 unsigned char *p
= *ptr
;
109 while (not_useful(p
, map
))
117 static void nubus_move(unsigned char **ptr
, int len
, int map
)
119 unsigned long slot_space
= (unsigned long)*ptr
& 0xFF000000;
122 nubus_advance(ptr
, len
, map
);
124 nubus_rewind(ptr
, -len
, map
);
126 if (((unsigned long)*ptr
& 0xFF000000) != slot_space
)
127 pr_err("%s: moved out of slot address space!\n", __func__
);
130 /* Now, functions to read the sResource tree */
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
137 static inline long nubus_expand32(long foo
)
139 if (foo
& 0x00800000) /* 24bit negative */
144 static inline void *nubus_rom_addr(int slot
)
147 * Returns the first byte after the card. We then walk
148 * backwards to get the lane register and the config
150 return (void *)(0xF1000000 + (slot
<< 24));
153 static unsigned char *nubus_dirptr(const struct nubus_dirent
*nd
)
155 unsigned char *p
= nd
->base
;
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 */
164 /* These two are for pulling resource data blocks (i.e. stuff that's
165 pointed to with offsets) out of the card ROM. */
167 void nubus_get_rsrc_mem(void *dest
, const struct nubus_dirent
*dirent
,
170 unsigned char *t
= (unsigned char *)dest
;
171 unsigned char *p
= nubus_dirptr(dirent
);
174 *t
++ = nubus_get_rom(&p
, 1, dirent
->mask
);
178 EXPORT_SYMBOL(nubus_get_rsrc_mem
);
180 void nubus_get_rsrc_str(void *dest
, const struct nubus_dirent
*dirent
,
183 unsigned char *t
= (unsigned char *)dest
;
184 unsigned char *p
= nubus_dirptr(dirent
);
187 *t
= nubus_get_rom(&p
, 1, dirent
->mask
);
193 EXPORT_SYMBOL(nubus_get_rsrc_str
);
195 int nubus_get_root_dir(const struct nubus_board
*board
,
196 struct nubus_dir
*dir
)
198 dir
->ptr
= dir
->base
= board
->directory
;
200 dir
->mask
= board
->lanes
;
203 EXPORT_SYMBOL(nubus_get_root_dir
);
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
)
209 dir
->ptr
= dir
->base
= dev
->directory
;
211 dir
->mask
= dev
->board
->lanes
;
214 EXPORT_SYMBOL(nubus_get_func_dir
);
216 int nubus_get_board_dir(const struct nubus_board
*board
,
217 struct nubus_dir
*dir
)
219 struct nubus_dirent ent
;
221 dir
->ptr
= dir
->base
= board
->directory
;
223 dir
->mask
= board
->lanes
;
225 /* Now dereference it (the first directory is always the board
227 if (nubus_readdir(dir
, &ent
) == -1)
229 if (nubus_get_subdir(&ent
, dir
) == -1)
233 EXPORT_SYMBOL(nubus_get_board_dir
);
235 int nubus_get_subdir(const struct nubus_dirent
*ent
,
236 struct nubus_dir
*dir
)
238 dir
->ptr
= dir
->base
= nubus_dirptr(ent
);
240 dir
->mask
= ent
->mask
;
243 EXPORT_SYMBOL(nubus_get_subdir
);
245 int nubus_readdir(struct nubus_dir
*nd
, struct nubus_dirent
*ent
)
252 /* Do this first, otherwise nubus_rewind & co are off by 4 */
255 /* This moves nd->ptr forward */
256 resid
= nubus_get_rom(&nd
->ptr
, 4, nd
->mask
);
258 /* EOL marker, as per the Apple docs */
259 if ((resid
& 0xff000000) == 0xff000000) {
260 /* Mark it as done */
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
;
272 EXPORT_SYMBOL(nubus_readdir
);
274 int nubus_rewinddir(struct nubus_dir
*dir
)
276 dir
->ptr
= dir
->base
;
280 EXPORT_SYMBOL(nubus_rewinddir
);
282 /* Driver interface functions, more or less like in pci.c */
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
)
289 struct nubus_dev
*itor
= from
? from
->next
: nubus_devices
;
292 if (itor
->category
== category
&& itor
->type
== type
&&
293 itor
->dr_hw
== dr_hw
&& itor
->dr_sw
== dr_sw
)
299 EXPORT_SYMBOL(nubus_find_device
);
302 nubus_find_type(unsigned short category
, unsigned short type
,
303 const struct nubus_dev
*from
)
305 struct nubus_dev
*itor
= from
? from
->next
: nubus_devices
;
308 if (itor
->category
== category
&& itor
->type
== type
)
314 EXPORT_SYMBOL(nubus_find_type
);
317 nubus_find_slot(unsigned int slot
, const struct nubus_dev
*from
)
319 struct nubus_dev
*itor
= from
? from
->next
: nubus_devices
;
322 if (itor
->board
->slot
== slot
)
328 EXPORT_SYMBOL(nubus_find_slot
);
331 nubus_find_rsrc(struct nubus_dir
*dir
, unsigned char rsrc_type
,
332 struct nubus_dirent
*ent
)
334 while (nubus_readdir(dir
, ent
) != -1) {
335 if (ent
->type
== rsrc_type
)
340 EXPORT_SYMBOL(nubus_find_rsrc
);
342 /* Initialization functions - decide which slots contain stuff worth
343 looking at, and print out lots and lots of information from the
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. */
351 static int __init
nubus_show_display_resource(struct nubus_dev
*dev
,
352 const struct nubus_dirent
*ent
)
355 case NUBUS_RESID_GAMMADIR
:
356 pr_info(" gamma directory offset: 0x%06x\n", ent
->data
);
358 case 0x0080 ... 0x0085:
359 pr_info(" mode %02X info offset: 0x%06x\n",
360 ent
->type
, ent
->data
);
363 pr_info(" unknown resource %02X, data 0x%06x\n",
364 ent
->type
, ent
->data
);
369 static int __init
nubus_show_network_resource(struct nubus_dev
*dev
,
370 const struct nubus_dirent
*ent
)
373 case NUBUS_RESID_MAC_ADDRESS
:
377 nubus_get_rsrc_mem(addr
, ent
, 6);
378 pr_info(" MAC address: %pM\n", addr
);
382 pr_info(" unknown resource %02X, data 0x%06x\n",
383 ent
->type
, ent
->data
);
388 static int __init
nubus_show_cpu_resource(struct nubus_dev
*dev
,
389 const struct nubus_dirent
*ent
)
392 case NUBUS_RESID_MEMINFO
:
394 unsigned long meminfo
[2];
396 nubus_get_rsrc_mem(&meminfo
, ent
, 8);
397 pr_info(" memory: [ 0x%08lx 0x%08lx ]\n",
398 meminfo
[0], meminfo
[1]);
401 case NUBUS_RESID_ROMINFO
:
403 unsigned long rominfo
[2];
405 nubus_get_rsrc_mem(&rominfo
, ent
, 8);
406 pr_info(" ROM: [ 0x%08lx 0x%08lx ]\n",
407 rominfo
[0], rominfo
[1]);
411 pr_info(" unknown resource %02X, data 0x%06x\n",
412 ent
->type
, ent
->data
);
417 static int __init
nubus_show_private_resource(struct nubus_dev
*dev
,
418 const struct nubus_dirent
*ent
)
420 switch (dev
->category
) {
421 case NUBUS_CAT_DISPLAY
:
422 nubus_show_display_resource(dev
, ent
);
424 case NUBUS_CAT_NETWORK
:
425 nubus_show_network_resource(dev
, ent
);
428 nubus_show_cpu_resource(dev
, ent
);
431 pr_info(" unknown resource %02X, data 0x%06x\n",
432 ent
->type
, ent
->data
);
437 static struct nubus_dev
* __init
438 nubus_get_functional_resource(struct nubus_board
*board
, int slot
,
439 const struct nubus_dirent
*parent
)
441 struct nubus_dir dir
;
442 struct nubus_dirent ent
;
443 struct nubus_dev
*dev
;
445 pr_info(" Function 0x%02x:\n", parent
->type
);
446 nubus_get_subdir(parent
, &dir
);
448 pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
449 __func__
, parent
->base
, dir
.base
);
451 /* Actually we should probably panic if this fails */
452 if ((dev
= kzalloc(sizeof(*dev
), GFP_ATOMIC
)) == NULL
)
454 dev
->resid
= parent
->type
;
455 dev
->directory
= dir
.base
;
458 while (nubus_readdir(&dir
, &ent
) != -1) {
460 case NUBUS_RESID_TYPE
:
462 unsigned short nbtdata
[4];
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]);
473 case NUBUS_RESID_NAME
:
475 nubus_get_rsrc_str(dev
->name
, &ent
, 64);
476 pr_info(" name: %s\n", dev
->name
);
479 case NUBUS_RESID_DRVRDIR
:
481 /* MacOS driver. If we were NetBSD we might
483 struct nubus_dir drvr_dir
;
484 struct nubus_dirent drvr_ent
;
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
);
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
);
499 case NUBUS_RESID_MINOR_LENGTH
:
501 nubus_get_rsrc_mem(&dev
->iosize
, &ent
, 4);
502 pr_info(" memory length: 0x%08lx\n", dev
->iosize
);
504 case NUBUS_RESID_FLAGS
:
505 dev
->flags
= ent
.data
;
506 pr_info(" flags: 0x%06x\n", dev
->flags
);
508 case NUBUS_RESID_HWDEVID
:
509 dev
->hwdevid
= ent
.data
;
510 pr_info(" hwdevid: 0x%06x\n", dev
->hwdevid
);
513 /* Local/Private resources have their own
515 nubus_show_private_resource(dev
, &ent
);
523 static int __init
nubus_get_vidnames(struct nubus_board
*board
,
524 const struct nubus_dirent
*parent
)
526 struct nubus_dir dir
;
527 struct nubus_dirent ent
;
529 /* FIXME: obviously we want to put this in a header file soon */
532 /* Don't know what this is yet */
534 /* Longest one I've seen so far is 26 characters */
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
);
543 while (nubus_readdir(&dir
, &ent
) != -1) {
547 /* First get the length */
548 nubus_get_rsrc_mem(&size
, &ent
, 4);
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
,
561 /* This is *really* cool. */
562 static int __init
nubus_get_icon(struct nubus_board
*board
,
563 const struct nubus_dirent
*ent
)
565 /* Should be 32x32 if my memory serves me correctly */
566 unsigned char icon
[128];
569 nubus_get_rsrc_mem(&icon
, ent
, 128);
572 /* We should actually plot these somewhere in the framebuffer
573 init. This is just to demonstrate that they do, in fact,
575 for (y
= 0; y
< 32; y
++) {
577 for (x
= 0; x
< 32; x
++) {
578 if (icon
[y
* 4 + x
/ 8] & (0x80 >> (x
% 8)))
588 static int __init
nubus_get_vendorinfo(struct nubus_board
*board
,
589 const struct nubus_dirent
*parent
)
591 struct nubus_dir dir
;
592 struct nubus_dirent ent
;
593 static char *vendor_fields
[6] = { "ID", "serial", "revision",
594 "part", "date", "unknown field" };
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
);
601 while (nubus_readdir(&dir
, &ent
) != -1) {
604 /* These are all strings, we think */
605 nubus_get_rsrc_str(name
, &ent
, 64);
608 pr_info(" %s: %s\n", vendor_fields
[ent
.type
- 1], name
);
613 static int __init
nubus_get_board_resource(struct nubus_board
*board
, int slot
,
614 const struct nubus_dirent
*parent
)
616 struct nubus_dir dir
;
617 struct nubus_dirent ent
;
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
);
623 while (nubus_readdir(&dir
, &ent
) != -1) {
625 case NUBUS_RESID_TYPE
:
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");
639 case NUBUS_RESID_NAME
:
640 nubus_get_rsrc_str(board
->name
, &ent
, 64);
641 pr_info(" name: %s\n", board
->name
);
643 case NUBUS_RESID_ICON
:
644 nubus_get_icon(board
, &ent
);
646 case NUBUS_RESID_BOARDID
:
647 pr_info(" board id: 0x%x\n", ent
.data
);
649 case NUBUS_RESID_PRIMARYINIT
:
650 pr_info(" primary init offset: 0x%06x\n", ent
.data
);
652 case NUBUS_RESID_VENDORINFO
:
653 nubus_get_vendorinfo(board
, &ent
);
655 case NUBUS_RESID_FLAGS
:
656 pr_info(" flags: 0x%06x\n", ent
.data
);
658 case NUBUS_RESID_HWDEVID
:
659 pr_info(" hwdevid: 0x%06x\n", ent
.data
);
661 case NUBUS_RESID_SECONDINIT
:
662 pr_info(" secondary init offset: 0x%06x\n", ent
.data
);
664 /* WTF isn't this in the functional resources? */
665 case NUBUS_RESID_VIDNAMES
:
666 nubus_get_vidnames(board
, &ent
);
668 /* Same goes for this */
669 case NUBUS_RESID_VIDMODES
:
670 pr_info(" video mode parameter directory offset: 0x%06x\n",
674 pr_info(" unknown resource %02X, data 0x%06x\n",
681 /* Add a board (might be many devices) to the list */
682 static struct nubus_board
* __init
nubus_add_board(int slot
, int bytelanes
)
684 struct nubus_board
*board
;
685 struct nubus_board
**boardp
;
688 struct nubus_dir dir
;
689 struct nubus_dirent ent
;
691 /* Move to the start of the format block */
692 rp
= nubus_rom_addr(slot
);
693 nubus_rewind(&rp
, FORMAT_BLOCK_SIZE
, bytelanes
);
695 /* Actually we should probably panic if this fails */
696 if ((board
= kzalloc(sizeof(*board
), GFP_ATOMIC
)) == NULL
)
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
));
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".
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
;
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
);
735 * I wonder how the CRC is meant to work -
737 * CSA: According to MAC docs, not all cards pass the CRC anyway,
738 * since the initial Macintosh ROM releases skipped the check.
741 /* Set up the directory pointer */
742 board
->directory
= board
->fblock
;
743 nubus_move(&board
->directory
, nubus_expand32(board
->doffset
),
746 nubus_get_root_dir(board
, &dir
);
748 /* We're ready to rock */
749 pr_info("Slot %X:\n", slot
);
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
756 if (nubus_readdir(&dir
, &ent
) == -1) {
757 /* We can't have this! */
758 pr_err("Board resource not found!\n");
761 pr_info(" Board resource:\n");
762 nubus_get_board_resource(board
, slot
, &ent
);
765 while (nubus_readdir(&dir
, &ent
) != -1) {
766 struct nubus_dev
*dev
;
767 struct nubus_dev
**devp
;
769 dev
= nubus_get_functional_resource(board
, slot
, &ent
);
773 /* We zeroed this out above */
774 if (board
->first_dev
== NULL
)
775 board
->first_dev
= dev
;
777 /* Put it on the global NuBus device chain. Keep entries in order. */
778 for (devp
= &nubus_devices
; *devp
!= NULL
;
779 devp
= &((*devp
)->next
))
785 /* Put it on the global NuBus board chain. Keep entries in order. */
786 for (boardp
= &nubus_boards
; *boardp
!= NULL
;
787 boardp
= &((*boardp
)->next
))
795 void __init
nubus_probe_slot(int slot
)
801 rp
= nubus_rom_addr(slot
);
802 for (i
= 4; i
; i
--) {
806 card_present
= hwreg_present(rp
);
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)
817 /* Check that this value is actually *on* one of the
818 bytelanes it claims are valid! */
819 if (not_useful(rp
, dp
))
822 /* Looks promising. Let's put it on the list. */
823 nubus_add_board(slot
, dp
);
829 void __init
nubus_scan_bus(void)
833 for (slot
= 9; slot
< 15; slot
++) {
834 nubus_probe_slot(slot
);
838 static int __init
nubus_init(void)
843 /* Initialize the NuBus interrupts */
851 pr_info("NuBus: Scanning NuBus slots.\n");
852 nubus_devices
= NULL
;
859 subsys_initcall(nubus_init
);