1 // SPDX-License-Identifier: GPL-2.0
3 * Macintosh Nubus Interface Code
5 * Originally by Alan Cox
7 * Mostly rewritten by David Huggins-Daines, C. Scott Ananian,
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>
21 #include <asm/hwtest.h>
22 #include <asm/mac_via.h>
23 #include <asm/mac_oss.h>
25 extern void via_nubus_init(void);
26 extern void oss_nubus_init(void);
30 /* This is, of course, the size in bytelanes, rather than the size in
32 #define FORMAT_BLOCK_SIZE 20
33 #define ROM_DIR_OFFSET 0x24
35 #define NUBUS_TEST_PATTERN 0x5A932BC7
39 struct nubus_dev *nubus_devices;
40 struct nubus_board *nubus_boards;
42 /* Meaning of "bytelanes":
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:
50 A map of 0x0f, as found in the MacOS ROM, means that all bytelanes
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)
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)
60 A map of 0x78 means that only the LSB of each long word is valid.
62 Etcetera, etcetera. Hopefully this clears up some confusion over
63 what the following code actually does. */
65 static inline int not_useful(void *p, int map)
67 unsigned long pv = (unsigned long)p;
75 static unsigned long nubus_get_rom(unsigned char **ptr, int len, int map)
77 /* This will hold the result */
79 unsigned char *p = *ptr;
83 while (not_useful(p, map))
92 static void nubus_rewind(unsigned char **ptr, int len, int map)
94 unsigned char *p = *ptr;
99 } while (not_useful(p, map));
105 static void nubus_advance(unsigned char **ptr, int len, int map)
107 unsigned char *p = *ptr;
110 while (not_useful(p, map))
118 static void nubus_move(unsigned char **ptr, int len, int map)
120 unsigned long slot_space = (unsigned long)*ptr & 0xFF000000;
123 nubus_advance(ptr, len, map);
125 nubus_rewind(ptr, -len, map);
127 if (((unsigned long)*ptr & 0xFF000000) != slot_space)
128 pr_err("%s: moved out of slot address space!\n", __func__);
131 /* Now, functions to read the sResource tree */
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
138 static inline long nubus_expand32(long foo)
140 if (foo & 0x00800000) /* 24bit negative */
145 static inline void *nubus_rom_addr(int slot)
148 * Returns the first byte after the card. We then walk
149 * backwards to get the lane register and the config
151 return (void *)(0xF1000000 + (slot << 24));
154 static unsigned char *nubus_dirptr(const struct nubus_dirent *nd)
156 unsigned char *p = nd->base;
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 */
165 /* These two are for pulling resource data blocks (i.e. stuff that's
166 pointed to with offsets) out of the card ROM. */
168 void nubus_get_rsrc_mem(void *dest, const struct nubus_dirent *dirent,
171 unsigned char *t = (unsigned char *)dest;
172 unsigned char *p = nubus_dirptr(dirent);
175 *t++ = nubus_get_rom(&p, 1, dirent->mask);
179 EXPORT_SYMBOL(nubus_get_rsrc_mem);
181 void nubus_get_rsrc_str(void *dest, const struct nubus_dirent *dirent,
184 unsigned char *t = (unsigned char *)dest;
185 unsigned char *p = nubus_dirptr(dirent);
188 *t = nubus_get_rom(&p, 1, dirent->mask);
194 EXPORT_SYMBOL(nubus_get_rsrc_str);
196 int nubus_get_root_dir(const struct nubus_board *board,
197 struct nubus_dir *dir)
199 dir->ptr = dir->base = board->directory;
201 dir->mask = board->lanes;
204 EXPORT_SYMBOL(nubus_get_root_dir);
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)
210 dir->ptr = dir->base = dev->directory;
212 dir->mask = dev->board->lanes;
215 EXPORT_SYMBOL(nubus_get_func_dir);
217 int nubus_get_board_dir(const struct nubus_board *board,
218 struct nubus_dir *dir)
220 struct nubus_dirent ent;
222 dir->ptr = dir->base = board->directory;
224 dir->mask = board->lanes;
226 /* Now dereference it (the first directory is always the board
228 if (nubus_readdir(dir, &ent) == -1)
230 if (nubus_get_subdir(&ent, dir) == -1)
234 EXPORT_SYMBOL(nubus_get_board_dir);
236 int nubus_get_subdir(const struct nubus_dirent *ent,
237 struct nubus_dir *dir)
239 dir->ptr = dir->base = nubus_dirptr(ent);
241 dir->mask = ent->mask;
244 EXPORT_SYMBOL(nubus_get_subdir);
246 int nubus_readdir(struct nubus_dir *nd, struct nubus_dirent *ent)
253 /* Do this first, otherwise nubus_rewind & co are off by 4 */
256 /* This moves nd->ptr forward */
257 resid = nubus_get_rom(&nd->ptr, 4, nd->mask);
259 /* EOL marker, as per the Apple docs */
260 if ((resid & 0xff000000) == 0xff000000) {
261 /* Mark it as done */
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;
273 EXPORT_SYMBOL(nubus_readdir);
275 int nubus_rewinddir(struct nubus_dir *dir)
277 dir->ptr = dir->base;
281 EXPORT_SYMBOL(nubus_rewinddir);
283 /* Driver interface functions, more or less like in pci.c */
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)
290 struct nubus_dev *itor = from ? from->next : nubus_devices;
293 if (itor->category == category && itor->type == type &&
294 itor->dr_hw == dr_hw && itor->dr_sw == dr_sw)
300 EXPORT_SYMBOL(nubus_find_device);
303 nubus_find_type(unsigned short category, unsigned short type,
304 const struct nubus_dev *from)
306 struct nubus_dev *itor = from ? from->next : nubus_devices;
309 if (itor->category == category && itor->type == type)
315 EXPORT_SYMBOL(nubus_find_type);
318 nubus_find_slot(unsigned int slot, const struct nubus_dev *from)
320 struct nubus_dev *itor = from ? from->next : nubus_devices;
323 if (itor->board->slot == slot)
329 EXPORT_SYMBOL(nubus_find_slot);
332 nubus_find_rsrc(struct nubus_dir *dir, unsigned char rsrc_type,
333 struct nubus_dirent *ent)
335 while (nubus_readdir(dir, ent) != -1) {
336 if (ent->type == rsrc_type)
341 EXPORT_SYMBOL(nubus_find_rsrc);
343 /* Initialization functions - decide which slots contain stuff worth
344 looking at, and print out lots and lots of information from the
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. */
352 static int __init nubus_show_display_resource(struct nubus_dev *dev,
353 const struct nubus_dirent *ent)
356 case NUBUS_RESID_GAMMADIR:
357 pr_info(" gamma directory offset: 0x%06x\n", ent->data);
359 case 0x0080 ... 0x0085:
360 pr_info(" mode %02X info offset: 0x%06x\n",
361 ent->type, ent->data);
364 pr_info(" unknown resource %02X, data 0x%06x\n",
365 ent->type, ent->data);
370 static int __init nubus_show_network_resource(struct nubus_dev *dev,
371 const struct nubus_dirent *ent)
374 case NUBUS_RESID_MAC_ADDRESS:
378 nubus_get_rsrc_mem(addr, ent, 6);
379 pr_info(" MAC address: %pM\n", addr);
383 pr_info(" unknown resource %02X, data 0x%06x\n",
384 ent->type, ent->data);
389 static int __init nubus_show_cpu_resource(struct nubus_dev *dev,
390 const struct nubus_dirent *ent)
393 case NUBUS_RESID_MEMINFO:
395 unsigned long meminfo[2];
397 nubus_get_rsrc_mem(&meminfo, ent, 8);
398 pr_info(" memory: [ 0x%08lx 0x%08lx ]\n",
399 meminfo[0], meminfo[1]);
402 case NUBUS_RESID_ROMINFO:
404 unsigned long rominfo[2];
406 nubus_get_rsrc_mem(&rominfo, ent, 8);
407 pr_info(" ROM: [ 0x%08lx 0x%08lx ]\n",
408 rominfo[0], rominfo[1]);
412 pr_info(" unknown resource %02X, data 0x%06x\n",
413 ent->type, ent->data);
418 static int __init nubus_show_private_resource(struct nubus_dev *dev,
419 const struct nubus_dirent *ent)
421 switch (dev->category) {
422 case NUBUS_CAT_DISPLAY:
423 nubus_show_display_resource(dev, ent);
425 case NUBUS_CAT_NETWORK:
426 nubus_show_network_resource(dev, ent);
429 nubus_show_cpu_resource(dev, ent);
432 pr_info(" unknown resource %02X, data 0x%06x\n",
433 ent->type, ent->data);
438 static struct nubus_dev * __init
439 nubus_get_functional_resource(struct nubus_board *board, int slot,
440 const struct nubus_dirent *parent)
442 struct nubus_dir dir;
443 struct nubus_dirent ent;
444 struct nubus_dev *dev;
446 pr_info(" Function 0x%02x:\n", parent->type);
447 nubus_get_subdir(parent, &dir);
449 pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
450 __func__, parent->base, dir.base);
452 /* Actually we should probably panic if this fails */
453 if ((dev = kzalloc(sizeof(*dev), GFP_ATOMIC)) == NULL)
455 dev->resid = parent->type;
456 dev->directory = dir.base;
459 while (nubus_readdir(&dir, &ent) != -1) {
461 case NUBUS_RESID_TYPE:
463 unsigned short nbtdata[4];
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]);
474 case NUBUS_RESID_NAME:
476 nubus_get_rsrc_str(dev->name, &ent, 64);
477 pr_info(" name: %s\n", dev->name);
480 case NUBUS_RESID_DRVRDIR:
482 /* MacOS driver. If we were NetBSD we might
484 struct nubus_dir drvr_dir;
485 struct nubus_dirent drvr_ent;
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);
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);
500 case NUBUS_RESID_MINOR_LENGTH:
502 nubus_get_rsrc_mem(&dev->iosize, &ent, 4);
503 pr_info(" memory length: 0x%08lx\n", dev->iosize);
505 case NUBUS_RESID_FLAGS:
506 dev->flags = ent.data;
507 pr_info(" flags: 0x%06x\n", dev->flags);
509 case NUBUS_RESID_HWDEVID:
510 dev->hwdevid = ent.data;
511 pr_info(" hwdevid: 0x%06x\n", dev->hwdevid);
514 /* Local/Private resources have their own
516 nubus_show_private_resource(dev, &ent);
524 static int __init nubus_get_vidnames(struct nubus_board *board,
525 const struct nubus_dirent *parent)
527 struct nubus_dir dir;
528 struct nubus_dirent ent;
530 /* FIXME: obviously we want to put this in a header file soon */
533 /* Don't know what this is yet */
535 /* Longest one I've seen so far is 26 characters */
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);
544 while (nubus_readdir(&dir, &ent) != -1) {
548 /* First get the length */
549 nubus_get_rsrc_mem(&size, &ent, 4);
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,
562 /* This is *really* cool. */
563 static int __init nubus_get_icon(struct nubus_board *board,
564 const struct nubus_dirent *ent)
566 /* Should be 32x32 if my memory serves me correctly */
567 unsigned char icon[128];
570 nubus_get_rsrc_mem(&icon, ent, 128);
573 /* We should actually plot these somewhere in the framebuffer
574 init. This is just to demonstrate that they do, in fact,
576 for (y = 0; y < 32; y++) {
578 for (x = 0; x < 32; x++) {
579 if (icon[y * 4 + x / 8] & (0x80 >> (x % 8)))
589 static int __init nubus_get_vendorinfo(struct nubus_board *board,
590 const struct nubus_dirent *parent)
592 struct nubus_dir dir;
593 struct nubus_dirent ent;
594 static char *vendor_fields[6] = { "ID", "serial", "revision",
595 "part", "date", "unknown field" };
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);
602 while (nubus_readdir(&dir, &ent) != -1) {
605 /* These are all strings, we think */
606 nubus_get_rsrc_str(name, &ent, 64);
609 pr_info(" %s: %s\n", vendor_fields[ent.type - 1], name);
614 static int __init nubus_get_board_resource(struct nubus_board *board, int slot,
615 const struct nubus_dirent *parent)
617 struct nubus_dir dir;
618 struct nubus_dirent ent;
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);
624 while (nubus_readdir(&dir, &ent) != -1) {
626 case NUBUS_RESID_TYPE:
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");
640 case NUBUS_RESID_NAME:
641 nubus_get_rsrc_str(board->name, &ent, 64);
642 pr_info(" name: %s\n", board->name);
644 case NUBUS_RESID_ICON:
645 nubus_get_icon(board, &ent);
647 case NUBUS_RESID_BOARDID:
648 pr_info(" board id: 0x%x\n", ent.data);
650 case NUBUS_RESID_PRIMARYINIT:
651 pr_info(" primary init offset: 0x%06x\n", ent.data);
653 case NUBUS_RESID_VENDORINFO:
654 nubus_get_vendorinfo(board, &ent);
656 case NUBUS_RESID_FLAGS:
657 pr_info(" flags: 0x%06x\n", ent.data);
659 case NUBUS_RESID_HWDEVID:
660 pr_info(" hwdevid: 0x%06x\n", ent.data);
662 case NUBUS_RESID_SECONDINIT:
663 pr_info(" secondary init offset: 0x%06x\n", ent.data);
665 /* WTF isn't this in the functional resources? */
666 case NUBUS_RESID_VIDNAMES:
667 nubus_get_vidnames(board, &ent);
669 /* Same goes for this */
670 case NUBUS_RESID_VIDMODES:
671 pr_info(" video mode parameter directory offset: 0x%06x\n",
675 pr_info(" unknown resource %02X, data 0x%06x\n",
682 /* Add a board (might be many devices) to the list */
683 static struct nubus_board * __init nubus_add_board(int slot, int bytelanes)
685 struct nubus_board *board;
686 struct nubus_board **boardp;
689 struct nubus_dir dir;
690 struct nubus_dirent ent;
692 /* Move to the start of the format block */
693 rp = nubus_rom_addr(slot);
694 nubus_rewind(&rp, FORMAT_BLOCK_SIZE, bytelanes);
696 /* Actually we should probably panic if this fails */
697 if ((board = kzalloc(sizeof(*board), GFP_ATOMIC)) == NULL)
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));
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".
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;
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);
736 * I wonder how the CRC is meant to work -
738 * CSA: According to MAC docs, not all cards pass the CRC anyway,
739 * since the initial Macintosh ROM releases skipped the check.
742 /* Set up the directory pointer */
743 board->directory = board->fblock;
744 nubus_move(&board->directory, nubus_expand32(board->doffset),
747 nubus_get_root_dir(board, &dir);
749 /* We're ready to rock */
750 pr_info("Slot %X:\n", slot);
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
757 if (nubus_readdir(&dir, &ent) == -1) {
758 /* We can't have this! */
759 pr_err("Board resource not found!\n");
762 pr_info(" Board resource:\n");
763 nubus_get_board_resource(board, slot, &ent);
766 while (nubus_readdir(&dir, &ent) != -1) {
767 struct nubus_dev *dev;
768 struct nubus_dev **devp;
770 dev = nubus_get_functional_resource(board, slot, &ent);
774 /* We zeroed this out above */
775 if (board->first_dev == NULL)
776 board->first_dev = dev;
778 /* Put it on the global NuBus device chain. Keep entries in order. */
779 for (devp = &nubus_devices; *devp != NULL;
780 devp = &((*devp)->next))
786 /* Put it on the global NuBus board chain. Keep entries in order. */
787 for (boardp = &nubus_boards; *boardp != NULL;
788 boardp = &((*boardp)->next))
796 void __init nubus_probe_slot(int slot)
802 rp = nubus_rom_addr(slot);
803 for (i = 4; i; i--) {
807 card_present = hwreg_present(rp);
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)
818 /* Check that this value is actually *on* one of the
819 bytelanes it claims are valid! */
820 if (not_useful(rp, dp))
823 /* Looks promising. Let's put it on the list. */
824 nubus_add_board(slot, dp);
830 void __init nubus_scan_bus(void)
834 for (slot = 9; slot < 15; slot++) {
835 nubus_probe_slot(slot);
839 static int __init nubus_init(void)
844 /* Initialize the NuBus interrupts */
852 pr_info("NuBus: Scanning NuBus slots.\n");
853 nubus_devices = NULL;
860 subsys_initcall(nubus_init);