xref: /linux/drivers/nubus/nubus.c (revision 2f828fb21df42058084b16d5e07cecdc30dbc3a5)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	Macintosh Nubus Interface Code
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *      Originally by Alan Cox
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *      Mostly rewritten by David Huggins-Daines, C. Scott Ananian,
81da177e4SLinus Torvalds  *      and others.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/types.h>
121da177e4SLinus Torvalds #include <linux/kernel.h>
131da177e4SLinus Torvalds #include <linux/string.h>
141da177e4SLinus Torvalds #include <linux/nubus.h>
151da177e4SLinus Torvalds #include <linux/errno.h>
161da177e4SLinus Torvalds #include <linux/init.h>
1799ffab81SAdrian Bunk #include <linux/module.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
191da177e4SLinus Torvalds #include <asm/setup.h>
201da177e4SLinus Torvalds #include <asm/page.h>
211da177e4SLinus Torvalds #include <asm/hwtest.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds /* Constants */
241da177e4SLinus Torvalds 
251da177e4SLinus Torvalds /* This is, of course, the size in bytelanes, rather than the size in
261da177e4SLinus Torvalds    actual bytes */
271da177e4SLinus Torvalds #define FORMAT_BLOCK_SIZE 20
281da177e4SLinus Torvalds #define ROM_DIR_OFFSET 0x24
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #define NUBUS_TEST_PATTERN 0x5A932BC7
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds /* Globals */
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds struct nubus_dev *nubus_devices;
351da177e4SLinus Torvalds struct nubus_board *nubus_boards;
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds /* Meaning of "bytelanes":
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds    The card ROM may appear on any or all bytes of each long word in
401da177e4SLinus Torvalds    NuBus memory.  The low 4 bits of the "map" value found in the
411da177e4SLinus Torvalds    format block (at the top of the slot address space, as well as at
421da177e4SLinus Torvalds    the top of the MacOS ROM) tells us which bytelanes, i.e. which byte
431da177e4SLinus Torvalds    offsets within each longword, are valid.  Thus:
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds    A map of 0x0f, as found in the MacOS ROM, means that all bytelanes
461da177e4SLinus Torvalds    are valid.
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds    A map of 0xf0 means that no bytelanes are valid (We pray that we
491da177e4SLinus Torvalds    will never encounter this, but stranger things have happened)
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds    A map of 0xe1 means that only the MSB of each long word is actually
521da177e4SLinus Torvalds    part of the card ROM.  (We hope to never encounter NuBus on a
531da177e4SLinus Torvalds    little-endian machine.  Again, stranger things have happened)
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds    A map of 0x78 means that only the LSB of each long word is valid.
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds    Etcetera, etcetera.  Hopefully this clears up some confusion over
581da177e4SLinus Torvalds    what the following code actually does.  */
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds static inline int not_useful(void *p, int map)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	unsigned long pv = (unsigned long)p;
63f42e5550SFinn Thain 
641da177e4SLinus Torvalds 	pv &= 3;
651da177e4SLinus Torvalds 	if (map & (1 << pv))
661da177e4SLinus Torvalds 		return 0;
671da177e4SLinus Torvalds 	return 1;
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds static unsigned long nubus_get_rom(unsigned char **ptr, int len, int map)
711da177e4SLinus Torvalds {
721da177e4SLinus Torvalds 	/* This will hold the result */
731da177e4SLinus Torvalds 	unsigned long v = 0;
741da177e4SLinus Torvalds 	unsigned char *p = *ptr;
751da177e4SLinus Torvalds 
76f42e5550SFinn Thain 	while (len) {
771da177e4SLinus Torvalds 		v <<= 8;
781da177e4SLinus Torvalds 		while (not_useful(p, map))
791da177e4SLinus Torvalds 			p++;
801da177e4SLinus Torvalds 		v |= *p++;
811da177e4SLinus Torvalds 		len--;
821da177e4SLinus Torvalds 	}
831da177e4SLinus Torvalds 	*ptr = p;
841da177e4SLinus Torvalds 	return v;
851da177e4SLinus Torvalds }
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds static void nubus_rewind(unsigned char **ptr, int len, int map)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	unsigned char *p = *ptr;
901da177e4SLinus Torvalds 
91f42e5550SFinn Thain 	while (len) {
92f42e5550SFinn Thain 		do {
931da177e4SLinus Torvalds 			p--;
94f42e5550SFinn Thain 		} while (not_useful(p, map));
951da177e4SLinus Torvalds 		len--;
961da177e4SLinus Torvalds 	}
971da177e4SLinus Torvalds 	*ptr = p;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static void nubus_advance(unsigned char **ptr, int len, int map)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	unsigned char *p = *ptr;
103f42e5550SFinn Thain 
104f42e5550SFinn Thain 	while (len) {
1051da177e4SLinus Torvalds 		while (not_useful(p, map))
1061da177e4SLinus Torvalds 			p++;
1071da177e4SLinus Torvalds 		p++;
1081da177e4SLinus Torvalds 		len--;
1091da177e4SLinus Torvalds 	}
1101da177e4SLinus Torvalds 	*ptr = p;
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds static void nubus_move(unsigned char **ptr, int len, int map)
1141da177e4SLinus Torvalds {
11585cc313aSFinn Thain 	unsigned long slot_space = (unsigned long)*ptr & 0xFF000000;
11685cc313aSFinn Thain 
1171da177e4SLinus Torvalds 	if (len > 0)
1181da177e4SLinus Torvalds 		nubus_advance(ptr, len, map);
1191da177e4SLinus Torvalds 	else if (len < 0)
1201da177e4SLinus Torvalds 		nubus_rewind(ptr, -len, map);
12185cc313aSFinn Thain 
12285cc313aSFinn Thain 	if (((unsigned long)*ptr & 0xFF000000) != slot_space)
12385cc313aSFinn Thain 		pr_err("%s: moved out of slot address space!\n", __func__);
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /* Now, functions to read the sResource tree */
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /* Each sResource entry consists of a 1-byte ID and a 3-byte data
1291da177e4SLinus Torvalds    field.  If that data field contains an offset, then obviously we
1301da177e4SLinus Torvalds    have to expand it from a 24-bit signed number to a 32-bit signed
1311da177e4SLinus Torvalds    number. */
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds static inline long nubus_expand32(long foo)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	if (foo & 0x00800000)	/* 24bit negative */
1361da177e4SLinus Torvalds 		foo |= 0xFF000000;
1371da177e4SLinus Torvalds 	return foo;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds static inline void *nubus_rom_addr(int slot)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	/*
1431da177e4SLinus Torvalds 	 *	Returns the first byte after the card. We then walk
1441da177e4SLinus Torvalds 	 *	backwards to get the lane register and the config
1451da177e4SLinus Torvalds 	 */
1461da177e4SLinus Torvalds 	return (void *)(0xF1000000 + (slot << 24));
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static unsigned char *nubus_dirptr(const struct nubus_dirent *nd)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	unsigned char *p = nd->base;
152f42e5550SFinn Thain 
1531da177e4SLinus Torvalds 	/* Essentially, just step over the bytelanes using whatever
1541da177e4SLinus Torvalds 	   offset we might have found */
1551da177e4SLinus Torvalds 	nubus_move(&p, nubus_expand32(nd->data), nd->mask);
1561da177e4SLinus Torvalds 	/* And return the value */
1571da177e4SLinus Torvalds 	return p;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds /* These two are for pulling resource data blocks (i.e. stuff that's
1611da177e4SLinus Torvalds    pointed to with offsets) out of the card ROM. */
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds void nubus_get_rsrc_mem(void *dest, const struct nubus_dirent *dirent,
164*2f828fb2SFinn Thain 			unsigned int len)
1651da177e4SLinus Torvalds {
1661da177e4SLinus Torvalds 	unsigned char *t = (unsigned char *)dest;
1671da177e4SLinus Torvalds 	unsigned char *p = nubus_dirptr(dirent);
168f42e5550SFinn Thain 
169f42e5550SFinn Thain 	while (len) {
1701da177e4SLinus Torvalds 		*t++ = nubus_get_rom(&p, 1, dirent->mask);
1711da177e4SLinus Torvalds 		len--;
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds }
17499ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_rsrc_mem);
1751da177e4SLinus Torvalds 
176*2f828fb2SFinn Thain void nubus_get_rsrc_str(char *dest, const struct nubus_dirent *dirent,
177*2f828fb2SFinn Thain 			unsigned int len)
1781da177e4SLinus Torvalds {
179*2f828fb2SFinn Thain 	char *t = dest;
1801da177e4SLinus Torvalds 	unsigned char *p = nubus_dirptr(dirent);
181f42e5550SFinn Thain 
182*2f828fb2SFinn Thain 	while (len > 1) {
183*2f828fb2SFinn Thain 		unsigned char c = nubus_get_rom(&p, 1, dirent->mask);
184*2f828fb2SFinn Thain 
185*2f828fb2SFinn Thain 		if (!c)
1861da177e4SLinus Torvalds 			break;
187*2f828fb2SFinn Thain 		*t++ = c;
1881da177e4SLinus Torvalds 		len--;
1891da177e4SLinus Torvalds 	}
190*2f828fb2SFinn Thain 	if (len > 0)
191*2f828fb2SFinn Thain 		*t = '\0';
1921da177e4SLinus Torvalds }
19399ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_rsrc_str);
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds int nubus_get_root_dir(const struct nubus_board *board,
1961da177e4SLinus Torvalds 		       struct nubus_dir *dir)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	dir->ptr = dir->base = board->directory;
1991da177e4SLinus Torvalds 	dir->done = 0;
2001da177e4SLinus Torvalds 	dir->mask = board->lanes;
2011da177e4SLinus Torvalds 	return 0;
2021da177e4SLinus Torvalds }
20399ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_root_dir);
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds /* This is a slyly renamed version of the above */
2061da177e4SLinus Torvalds int nubus_get_func_dir(const struct nubus_dev *dev,
2071da177e4SLinus Torvalds 		       struct nubus_dir *dir)
2081da177e4SLinus Torvalds {
2091da177e4SLinus Torvalds 	dir->ptr = dir->base = dev->directory;
2101da177e4SLinus Torvalds 	dir->done = 0;
2111da177e4SLinus Torvalds 	dir->mask = dev->board->lanes;
2121da177e4SLinus Torvalds 	return 0;
2131da177e4SLinus Torvalds }
21499ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_func_dir);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds int nubus_get_board_dir(const struct nubus_board *board,
2171da177e4SLinus Torvalds 			struct nubus_dir *dir)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	struct nubus_dirent ent;
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	dir->ptr = dir->base = board->directory;
2221da177e4SLinus Torvalds 	dir->done = 0;
2231da177e4SLinus Torvalds 	dir->mask = board->lanes;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	/* Now dereference it (the first directory is always the board
2261da177e4SLinus Torvalds 	   directory) */
2271da177e4SLinus Torvalds 	if (nubus_readdir(dir, &ent) == -1)
2281da177e4SLinus Torvalds 		return -1;
2291da177e4SLinus Torvalds 	if (nubus_get_subdir(&ent, dir) == -1)
2301da177e4SLinus Torvalds 		return -1;
2311da177e4SLinus Torvalds 	return 0;
2321da177e4SLinus Torvalds }
23399ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_board_dir);
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds int nubus_get_subdir(const struct nubus_dirent *ent,
2361da177e4SLinus Torvalds 		     struct nubus_dir *dir)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	dir->ptr = dir->base = nubus_dirptr(ent);
2391da177e4SLinus Torvalds 	dir->done = 0;
2401da177e4SLinus Torvalds 	dir->mask = ent->mask;
2411da177e4SLinus Torvalds 	return 0;
2421da177e4SLinus Torvalds }
24399ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_get_subdir);
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds int nubus_readdir(struct nubus_dir *nd, struct nubus_dirent *ent)
2461da177e4SLinus Torvalds {
2471da177e4SLinus Torvalds 	u32 resid;
248f42e5550SFinn Thain 
2491da177e4SLinus Torvalds 	if (nd->done)
2501da177e4SLinus Torvalds 		return -1;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	/* Do this first, otherwise nubus_rewind & co are off by 4 */
2531da177e4SLinus Torvalds 	ent->base = nd->ptr;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	/* This moves nd->ptr forward */
2561da177e4SLinus Torvalds 	resid = nubus_get_rom(&nd->ptr, 4, nd->mask);
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	/* EOL marker, as per the Apple docs */
259f42e5550SFinn Thain 	if ((resid & 0xff000000) == 0xff000000) {
2601da177e4SLinus Torvalds 		/* Mark it as done */
2611da177e4SLinus Torvalds 		nd->done = 1;
2621da177e4SLinus Torvalds 		return -1;
2631da177e4SLinus Torvalds 	}
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	/* First byte is the resource ID */
2661da177e4SLinus Torvalds 	ent->type = resid >> 24;
2671da177e4SLinus Torvalds 	/* Low 3 bytes might contain data (or might not) */
2681da177e4SLinus Torvalds 	ent->data = resid & 0xffffff;
2691da177e4SLinus Torvalds 	ent->mask = nd->mask;
2701da177e4SLinus Torvalds 	return 0;
2711da177e4SLinus Torvalds }
27299ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_readdir);
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds int nubus_rewinddir(struct nubus_dir *dir)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	dir->ptr = dir->base;
277e36b9913SDavid Huggins-Daines 	dir->done = 0;
2781da177e4SLinus Torvalds 	return 0;
2791da177e4SLinus Torvalds }
28099ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_rewinddir);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds /* Driver interface functions, more or less like in pci.c */
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds struct nubus_dev*
285f42e5550SFinn Thain nubus_find_device(unsigned short category, unsigned short type,
286f42e5550SFinn Thain 		  unsigned short dr_hw, unsigned short dr_sw,
2871da177e4SLinus Torvalds 		  const struct nubus_dev *from)
2881da177e4SLinus Torvalds {
289f42e5550SFinn Thain 	struct nubus_dev *itor = from ? from->next : nubus_devices;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	while (itor) {
292f42e5550SFinn Thain 		if (itor->category == category && itor->type == type &&
293f42e5550SFinn Thain 		    itor->dr_hw == dr_hw && itor->dr_sw == dr_sw)
2941da177e4SLinus Torvalds 			return itor;
2951da177e4SLinus Torvalds 		itor = itor->next;
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 	return NULL;
2981da177e4SLinus Torvalds }
29999ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_find_device);
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds struct nubus_dev*
302f42e5550SFinn Thain nubus_find_type(unsigned short category, unsigned short type,
3031da177e4SLinus Torvalds 		const struct nubus_dev *from)
3041da177e4SLinus Torvalds {
305f42e5550SFinn Thain 	struct nubus_dev *itor = from ? from->next : nubus_devices;
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	while (itor) {
308f42e5550SFinn Thain 		if (itor->category == category && itor->type == type)
3091da177e4SLinus Torvalds 			return itor;
3101da177e4SLinus Torvalds 		itor = itor->next;
3111da177e4SLinus Torvalds 	}
3121da177e4SLinus Torvalds 	return NULL;
3131da177e4SLinus Torvalds }
31499ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_find_type);
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds struct nubus_dev*
317f42e5550SFinn Thain nubus_find_slot(unsigned int slot, const struct nubus_dev *from)
3181da177e4SLinus Torvalds {
319f42e5550SFinn Thain 	struct nubus_dev *itor = from ? from->next : nubus_devices;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	while (itor) {
3221da177e4SLinus Torvalds 		if (itor->board->slot == slot)
3231da177e4SLinus Torvalds 			return itor;
3241da177e4SLinus Torvalds 		itor = itor->next;
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds 	return NULL;
3271da177e4SLinus Torvalds }
32899ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_find_slot);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds int
3311da177e4SLinus Torvalds nubus_find_rsrc(struct nubus_dir *dir, unsigned char rsrc_type,
3321da177e4SLinus Torvalds 		struct nubus_dirent *ent)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	while (nubus_readdir(dir, ent) != -1) {
3351da177e4SLinus Torvalds 		if (ent->type == rsrc_type)
3361da177e4SLinus Torvalds 			return 0;
3371da177e4SLinus Torvalds 	}
3381da177e4SLinus Torvalds 	return -1;
3391da177e4SLinus Torvalds }
34099ffab81SAdrian Bunk EXPORT_SYMBOL(nubus_find_rsrc);
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds /* Initialization functions - decide which slots contain stuff worth
3431da177e4SLinus Torvalds    looking at, and print out lots and lots of information from the
3441da177e4SLinus Torvalds    resource blocks. */
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds /* FIXME: A lot of this stuff will eventually be useful after
347081985acSJoe Perches    initialization, for intelligently probing Ethernet and video chips,
3481da177e4SLinus Torvalds    among other things.  The rest of it should go in the /proc code.
3491da177e4SLinus Torvalds    For now, we just use it to give verbose boot logs. */
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds static int __init nubus_show_display_resource(struct nubus_dev *dev,
3521da177e4SLinus Torvalds 					      const struct nubus_dirent *ent)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	switch (ent->type) {
3551da177e4SLinus Torvalds 	case NUBUS_RESID_GAMMADIR:
35671ae40e4SDavid Huggins-Daines 		pr_info("    gamma directory offset: 0x%06x\n", ent->data);
3571da177e4SLinus Torvalds 		break;
3581da177e4SLinus Torvalds 	case 0x0080 ... 0x0085:
35971ae40e4SDavid Huggins-Daines 		pr_info("    mode %02X info offset: 0x%06x\n",
3601da177e4SLinus Torvalds 		       ent->type, ent->data);
3611da177e4SLinus Torvalds 		break;
3621da177e4SLinus Torvalds 	default:
36371ae40e4SDavid Huggins-Daines 		pr_info("    unknown resource %02X, data 0x%06x\n",
3641da177e4SLinus Torvalds 		       ent->type, ent->data);
3651da177e4SLinus Torvalds 	}
3661da177e4SLinus Torvalds 	return 0;
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds static int __init nubus_show_network_resource(struct nubus_dev *dev,
3701da177e4SLinus Torvalds 					      const struct nubus_dirent *ent)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	switch (ent->type) {
3731da177e4SLinus Torvalds 	case NUBUS_RESID_MAC_ADDRESS:
3741da177e4SLinus Torvalds 	{
3751da177e4SLinus Torvalds 		char addr[6];
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 		nubus_get_rsrc_mem(addr, ent, 6);
37871ae40e4SDavid Huggins-Daines 		pr_info("    MAC address: %pM\n", addr);
3791da177e4SLinus Torvalds 		break;
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 	default:
38271ae40e4SDavid Huggins-Daines 		pr_info("    unknown resource %02X, data 0x%06x\n",
3831da177e4SLinus Torvalds 		       ent->type, ent->data);
3841da177e4SLinus Torvalds 	}
3851da177e4SLinus Torvalds 	return 0;
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds static int __init nubus_show_cpu_resource(struct nubus_dev *dev,
3891da177e4SLinus Torvalds 					  const struct nubus_dirent *ent)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds 	switch (ent->type) {
3921da177e4SLinus Torvalds 	case NUBUS_RESID_MEMINFO:
3931da177e4SLinus Torvalds 	{
3941da177e4SLinus Torvalds 		unsigned long meminfo[2];
395f42e5550SFinn Thain 
3961da177e4SLinus Torvalds 		nubus_get_rsrc_mem(&meminfo, ent, 8);
39771ae40e4SDavid Huggins-Daines 		pr_info("    memory: [ 0x%08lx 0x%08lx ]\n",
3981da177e4SLinus Torvalds 		       meminfo[0], meminfo[1]);
3991da177e4SLinus Torvalds 		break;
4001da177e4SLinus Torvalds 	}
4011da177e4SLinus Torvalds 	case NUBUS_RESID_ROMINFO:
4021da177e4SLinus Torvalds 	{
4031da177e4SLinus Torvalds 		unsigned long rominfo[2];
404f42e5550SFinn Thain 
4051da177e4SLinus Torvalds 		nubus_get_rsrc_mem(&rominfo, ent, 8);
40671ae40e4SDavid Huggins-Daines 		pr_info("    ROM:    [ 0x%08lx 0x%08lx ]\n",
4071da177e4SLinus Torvalds 		       rominfo[0], rominfo[1]);
4081da177e4SLinus Torvalds 		break;
4091da177e4SLinus Torvalds 	}
4101da177e4SLinus Torvalds 	default:
41171ae40e4SDavid Huggins-Daines 		pr_info("    unknown resource %02X, data 0x%06x\n",
4121da177e4SLinus Torvalds 		       ent->type, ent->data);
4131da177e4SLinus Torvalds 	}
4141da177e4SLinus Torvalds 	return 0;
4151da177e4SLinus Torvalds }
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds static int __init nubus_show_private_resource(struct nubus_dev *dev,
4181da177e4SLinus Torvalds 					      const struct nubus_dirent *ent)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds 	switch (dev->category) {
4211da177e4SLinus Torvalds 	case NUBUS_CAT_DISPLAY:
4221da177e4SLinus Torvalds 		nubus_show_display_resource(dev, ent);
4231da177e4SLinus Torvalds 		break;
4241da177e4SLinus Torvalds 	case NUBUS_CAT_NETWORK:
4251da177e4SLinus Torvalds 		nubus_show_network_resource(dev, ent);
4261da177e4SLinus Torvalds 		break;
4271da177e4SLinus Torvalds 	case NUBUS_CAT_CPU:
4281da177e4SLinus Torvalds 		nubus_show_cpu_resource(dev, ent);
4291da177e4SLinus Torvalds 		break;
4301da177e4SLinus Torvalds 	default:
43171ae40e4SDavid Huggins-Daines 		pr_info("    unknown resource %02X, data 0x%06x\n",
4321da177e4SLinus Torvalds 		       ent->type, ent->data);
4331da177e4SLinus Torvalds 	}
4341da177e4SLinus Torvalds 	return 0;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds static struct nubus_dev * __init
438f42e5550SFinn Thain nubus_get_functional_resource(struct nubus_board *board, int slot,
4391da177e4SLinus Torvalds 			      const struct nubus_dirent *parent)
4401da177e4SLinus Torvalds {
4411da177e4SLinus Torvalds 	struct nubus_dir dir;
4421da177e4SLinus Torvalds 	struct nubus_dirent ent;
4431da177e4SLinus Torvalds 	struct nubus_dev *dev;
4441da177e4SLinus Torvalds 
44571ae40e4SDavid Huggins-Daines 	pr_info("  Function 0x%02x:\n", parent->type);
4461da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
4471da177e4SLinus Torvalds 
44871ae40e4SDavid Huggins-Daines 	pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
44971ae40e4SDavid Huggins-Daines 	         __func__, parent->base, dir.base);
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	/* Actually we should probably panic if this fails */
452dd00cc48SYoann Padioleau 	if ((dev = kzalloc(sizeof(*dev), GFP_ATOMIC)) == NULL)
4531da177e4SLinus Torvalds 		return NULL;
4541da177e4SLinus Torvalds 	dev->resid = parent->type;
4551da177e4SLinus Torvalds 	dev->directory = dir.base;
4561da177e4SLinus Torvalds 	dev->board = board;
4571da177e4SLinus Torvalds 
458f42e5550SFinn Thain 	while (nubus_readdir(&dir, &ent) != -1) {
459f42e5550SFinn Thain 		switch (ent.type) {
4601da177e4SLinus Torvalds 		case NUBUS_RESID_TYPE:
4611da177e4SLinus Torvalds 		{
4621da177e4SLinus Torvalds 			unsigned short nbtdata[4];
463f42e5550SFinn Thain 
4641da177e4SLinus Torvalds 			nubus_get_rsrc_mem(nbtdata, &ent, 8);
4651da177e4SLinus Torvalds 			dev->category = nbtdata[0];
4661da177e4SLinus Torvalds 			dev->type     = nbtdata[1];
4671da177e4SLinus Torvalds 			dev->dr_sw    = nbtdata[2];
4681da177e4SLinus Torvalds 			dev->dr_hw    = nbtdata[3];
46971ae40e4SDavid Huggins-Daines 			pr_info("    type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
4701da177e4SLinus Torvalds 			        nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
4711da177e4SLinus Torvalds 			break;
4721da177e4SLinus Torvalds 		}
4731da177e4SLinus Torvalds 		case NUBUS_RESID_NAME:
4741da177e4SLinus Torvalds 		{
475*2f828fb2SFinn Thain 			nubus_get_rsrc_str(dev->name, &ent, sizeof(dev->name));
47671ae40e4SDavid Huggins-Daines 			pr_info("    name: %s\n", dev->name);
4771da177e4SLinus Torvalds 			break;
4781da177e4SLinus Torvalds 		}
4791da177e4SLinus Torvalds 		case NUBUS_RESID_DRVRDIR:
4801da177e4SLinus Torvalds 		{
4811da177e4SLinus Torvalds 			/* MacOS driver.  If we were NetBSD we might
4821da177e4SLinus Torvalds 			   use this :-) */
4831da177e4SLinus Torvalds 			struct nubus_dir drvr_dir;
4841da177e4SLinus Torvalds 			struct nubus_dirent drvr_ent;
485f42e5550SFinn Thain 
4861da177e4SLinus Torvalds 			nubus_get_subdir(&ent, &drvr_dir);
4871da177e4SLinus Torvalds 			nubus_readdir(&drvr_dir, &drvr_ent);
4881da177e4SLinus Torvalds 			dev->driver = nubus_dirptr(&drvr_ent);
48971ae40e4SDavid Huggins-Daines 			pr_info("    driver at: 0x%p\n", dev->driver);
4901da177e4SLinus Torvalds 			break;
4911da177e4SLinus Torvalds 		}
4921da177e4SLinus Torvalds 		case NUBUS_RESID_MINOR_BASEOS:
4931da177e4SLinus Torvalds 			/* We will need this in order to support
4941da177e4SLinus Torvalds 			   multiple framebuffers.  It might be handy
4951da177e4SLinus Torvalds 			   for Ethernet as well */
4961da177e4SLinus Torvalds 			nubus_get_rsrc_mem(&dev->iobase, &ent, 4);
49771ae40e4SDavid Huggins-Daines 			pr_info("    memory offset: 0x%08lx\n", dev->iobase);
4981da177e4SLinus Torvalds 			break;
4991da177e4SLinus Torvalds 		case NUBUS_RESID_MINOR_LENGTH:
5001da177e4SLinus Torvalds 			/* Ditto */
5011da177e4SLinus Torvalds 			nubus_get_rsrc_mem(&dev->iosize, &ent, 4);
50271ae40e4SDavid Huggins-Daines 			pr_info("    memory length: 0x%08lx\n", dev->iosize);
5031da177e4SLinus Torvalds 			break;
5041da177e4SLinus Torvalds 		case NUBUS_RESID_FLAGS:
5051da177e4SLinus Torvalds 			dev->flags = ent.data;
50671ae40e4SDavid Huggins-Daines 			pr_info("    flags: 0x%06x\n", dev->flags);
5071da177e4SLinus Torvalds 			break;
5081da177e4SLinus Torvalds 		case NUBUS_RESID_HWDEVID:
5091da177e4SLinus Torvalds 			dev->hwdevid = ent.data;
51071ae40e4SDavid Huggins-Daines 			pr_info("    hwdevid: 0x%06x\n", dev->hwdevid);
5111da177e4SLinus Torvalds 			break;
5121da177e4SLinus Torvalds 		default:
5131da177e4SLinus Torvalds 			/* Local/Private resources have their own
5141da177e4SLinus Torvalds 			   function */
5151da177e4SLinus Torvalds 			nubus_show_private_resource(dev, &ent);
5161da177e4SLinus Torvalds 		}
5171da177e4SLinus Torvalds 	}
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	return dev;
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds /* This is cool. */
5231da177e4SLinus Torvalds static int __init nubus_get_vidnames(struct nubus_board *board,
5241da177e4SLinus Torvalds 				     const struct nubus_dirent *parent)
5251da177e4SLinus Torvalds {
5261da177e4SLinus Torvalds 	struct nubus_dir dir;
5271da177e4SLinus Torvalds 	struct nubus_dirent ent;
528f42e5550SFinn Thain 
5291da177e4SLinus Torvalds 	/* FIXME: obviously we want to put this in a header file soon */
5301da177e4SLinus Torvalds 	struct vidmode {
5311da177e4SLinus Torvalds 		u32 size;
5321da177e4SLinus Torvalds 		/* Don't know what this is yet */
5331da177e4SLinus Torvalds 		u16 id;
5341da177e4SLinus Torvalds 		/* Longest one I've seen so far is 26 characters */
535*2f828fb2SFinn Thain 		char name[36];
5361da177e4SLinus Torvalds 	};
5371da177e4SLinus Torvalds 
53871ae40e4SDavid Huggins-Daines 	pr_info("    video modes supported:\n");
5391da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
54071ae40e4SDavid Huggins-Daines 	pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
54171ae40e4SDavid Huggins-Daines 	         __func__, parent->base, dir.base);
5421da177e4SLinus Torvalds 
543f42e5550SFinn Thain 	while (nubus_readdir(&dir, &ent) != -1) {
5441da177e4SLinus Torvalds 		struct vidmode mode;
5451da177e4SLinus Torvalds 		u32 size;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 		/* First get the length */
5481da177e4SLinus Torvalds 		nubus_get_rsrc_mem(&size, &ent, 4);
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 		/* Now clobber the whole thing */
5511da177e4SLinus Torvalds 		if (size > sizeof(mode) - 1)
5521da177e4SLinus Torvalds 			size = sizeof(mode) - 1;
5531da177e4SLinus Torvalds 		memset(&mode, 0, sizeof(mode));
5541da177e4SLinus Torvalds 		nubus_get_rsrc_mem(&mode, &ent, size);
55571ae40e4SDavid Huggins-Daines 		pr_info("      %02X: (%02X) %s\n", ent.type,
5561da177e4SLinus Torvalds 			mode.id, mode.name);
5571da177e4SLinus Torvalds 	}
5581da177e4SLinus Torvalds 	return 0;
5591da177e4SLinus Torvalds }
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds /* This is *really* cool. */
5621da177e4SLinus Torvalds static int __init nubus_get_icon(struct nubus_board *board,
5631da177e4SLinus Torvalds 				 const struct nubus_dirent *ent)
5641da177e4SLinus Torvalds {
5651da177e4SLinus Torvalds 	/* Should be 32x32 if my memory serves me correctly */
5661da177e4SLinus Torvalds 	unsigned char icon[128];
5671da177e4SLinus Torvalds 	int x, y;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	nubus_get_rsrc_mem(&icon, ent, 128);
57071ae40e4SDavid Huggins-Daines 	pr_info("    icon:\n");
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	/* We should actually plot these somewhere in the framebuffer
5731da177e4SLinus Torvalds 	   init.  This is just to demonstrate that they do, in fact,
5741da177e4SLinus Torvalds 	   exist */
5751da177e4SLinus Torvalds 	for (y = 0; y < 32; y++) {
57671ae40e4SDavid Huggins-Daines 		pr_info("      ");
5771da177e4SLinus Torvalds 		for (x = 0; x < 32; x++) {
578f42e5550SFinn Thain 			if (icon[y * 4 + x / 8] & (0x80 >> (x % 8)))
57971ae40e4SDavid Huggins-Daines 				pr_cont("*");
5801da177e4SLinus Torvalds 			else
58171ae40e4SDavid Huggins-Daines 				pr_cont(" ");
5821da177e4SLinus Torvalds 		}
58371ae40e4SDavid Huggins-Daines 		pr_cont("\n");
5841da177e4SLinus Torvalds 	}
5851da177e4SLinus Torvalds 	return 0;
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds static int __init nubus_get_vendorinfo(struct nubus_board *board,
5891da177e4SLinus Torvalds 				       const struct nubus_dirent *parent)
5901da177e4SLinus Torvalds {
5911da177e4SLinus Torvalds 	struct nubus_dir dir;
5921da177e4SLinus Torvalds 	struct nubus_dirent ent;
5931da177e4SLinus Torvalds 	static char *vendor_fields[6] = { "ID", "serial", "revision",
5941da177e4SLinus Torvalds 	                                  "part", "date", "unknown field" };
5951da177e4SLinus Torvalds 
59671ae40e4SDavid Huggins-Daines 	pr_info("    vendor info:\n");
5971da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
59871ae40e4SDavid Huggins-Daines 	pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
59971ae40e4SDavid Huggins-Daines 	         __func__, parent->base, dir.base);
6001da177e4SLinus Torvalds 
601f42e5550SFinn Thain 	while (nubus_readdir(&dir, &ent) != -1) {
6021da177e4SLinus Torvalds 		char name[64];
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 		/* These are all strings, we think */
605*2f828fb2SFinn Thain 		nubus_get_rsrc_str(name, &ent, sizeof(name));
606*2f828fb2SFinn Thain 		if (ent.type < 1 || ent.type > 5)
6071da177e4SLinus Torvalds 			ent.type = 5;
60871ae40e4SDavid Huggins-Daines 		pr_info("    %s: %s\n", vendor_fields[ent.type - 1], name);
6091da177e4SLinus Torvalds 	}
6101da177e4SLinus Torvalds 	return 0;
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds static int __init nubus_get_board_resource(struct nubus_board *board, int slot,
6141da177e4SLinus Torvalds 					   const struct nubus_dirent *parent)
6151da177e4SLinus Torvalds {
6161da177e4SLinus Torvalds 	struct nubus_dir dir;
6171da177e4SLinus Torvalds 	struct nubus_dirent ent;
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 	nubus_get_subdir(parent, &dir);
62071ae40e4SDavid Huggins-Daines 	pr_debug("%s: parent is 0x%p, dir is 0x%p\n",
62171ae40e4SDavid Huggins-Daines 	         __func__, parent->base, dir.base);
6221da177e4SLinus Torvalds 
623f42e5550SFinn Thain 	while (nubus_readdir(&dir, &ent) != -1) {
6241da177e4SLinus Torvalds 		switch (ent.type) {
6251da177e4SLinus Torvalds 		case NUBUS_RESID_TYPE:
6261da177e4SLinus Torvalds 		{
6271da177e4SLinus Torvalds 			unsigned short nbtdata[4];
6281da177e4SLinus Torvalds 			/* This type is always the same, and is not
6291da177e4SLinus Torvalds 			   useful except insofar as it tells us that
6301da177e4SLinus Torvalds 			   we really are looking at a board resource. */
6311da177e4SLinus Torvalds 			nubus_get_rsrc_mem(nbtdata, &ent, 8);
63271ae40e4SDavid Huggins-Daines 			pr_info("    type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
63371ae40e4SDavid Huggins-Daines 			        nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
6341da177e4SLinus Torvalds 			if (nbtdata[0] != 1 || nbtdata[1] != 0 ||
6351da177e4SLinus Torvalds 			    nbtdata[2] != 0 || nbtdata[3] != 0)
63671ae40e4SDavid Huggins-Daines 				pr_err("this sResource is not a board resource!\n");
6371da177e4SLinus Torvalds 			break;
6381da177e4SLinus Torvalds 		}
6391da177e4SLinus Torvalds 		case NUBUS_RESID_NAME:
640*2f828fb2SFinn Thain 			nubus_get_rsrc_str(board->name, &ent,
641*2f828fb2SFinn Thain 					   sizeof(board->name));
64271ae40e4SDavid Huggins-Daines 			pr_info("    name: %s\n", board->name);
6431da177e4SLinus Torvalds 			break;
6441da177e4SLinus Torvalds 		case NUBUS_RESID_ICON:
6451da177e4SLinus Torvalds 			nubus_get_icon(board, &ent);
6461da177e4SLinus Torvalds 			break;
6471da177e4SLinus Torvalds 		case NUBUS_RESID_BOARDID:
64871ae40e4SDavid Huggins-Daines 			pr_info("    board id: 0x%x\n", ent.data);
6491da177e4SLinus Torvalds 			break;
6501da177e4SLinus Torvalds 		case NUBUS_RESID_PRIMARYINIT:
65171ae40e4SDavid Huggins-Daines 			pr_info("    primary init offset: 0x%06x\n", ent.data);
6521da177e4SLinus Torvalds 			break;
6531da177e4SLinus Torvalds 		case NUBUS_RESID_VENDORINFO:
6541da177e4SLinus Torvalds 			nubus_get_vendorinfo(board, &ent);
6551da177e4SLinus Torvalds 			break;
6561da177e4SLinus Torvalds 		case NUBUS_RESID_FLAGS:
65771ae40e4SDavid Huggins-Daines 			pr_info("    flags: 0x%06x\n", ent.data);
6581da177e4SLinus Torvalds 			break;
6591da177e4SLinus Torvalds 		case NUBUS_RESID_HWDEVID:
66071ae40e4SDavid Huggins-Daines 			pr_info("    hwdevid: 0x%06x\n", ent.data);
6611da177e4SLinus Torvalds 			break;
6621da177e4SLinus Torvalds 		case NUBUS_RESID_SECONDINIT:
66371ae40e4SDavid Huggins-Daines 			pr_info("    secondary init offset: 0x%06x\n", ent.data);
6641da177e4SLinus Torvalds 			break;
6651da177e4SLinus Torvalds 			/* WTF isn't this in the functional resources? */
6661da177e4SLinus Torvalds 		case NUBUS_RESID_VIDNAMES:
6671da177e4SLinus Torvalds 			nubus_get_vidnames(board, &ent);
6681da177e4SLinus Torvalds 			break;
6691da177e4SLinus Torvalds 			/* Same goes for this */
6701da177e4SLinus Torvalds 		case NUBUS_RESID_VIDMODES:
67171ae40e4SDavid Huggins-Daines 			pr_info("    video mode parameter directory offset: 0x%06x\n",
6721da177e4SLinus Torvalds 			       ent.data);
6731da177e4SLinus Torvalds 			break;
6741da177e4SLinus Torvalds 		default:
67571ae40e4SDavid Huggins-Daines 			pr_info("    unknown resource %02X, data 0x%06x\n",
6761da177e4SLinus Torvalds 			       ent.type, ent.data);
6771da177e4SLinus Torvalds 		}
6781da177e4SLinus Torvalds 	}
6791da177e4SLinus Torvalds 	return 0;
6801da177e4SLinus Torvalds }
6811da177e4SLinus Torvalds 
6821da177e4SLinus Torvalds /* Add a board (might be many devices) to the list */
6831da177e4SLinus Torvalds static struct nubus_board * __init nubus_add_board(int slot, int bytelanes)
6841da177e4SLinus Torvalds {
6851da177e4SLinus Torvalds 	struct nubus_board *board;
6861da177e4SLinus Torvalds 	struct nubus_board **boardp;
6871da177e4SLinus Torvalds 	unsigned char *rp;
6881da177e4SLinus Torvalds 	unsigned long dpat;
6891da177e4SLinus Torvalds 	struct nubus_dir dir;
6901da177e4SLinus Torvalds 	struct nubus_dirent ent;
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	/* Move to the start of the format block */
6931da177e4SLinus Torvalds 	rp = nubus_rom_addr(slot);
6941da177e4SLinus Torvalds 	nubus_rewind(&rp, FORMAT_BLOCK_SIZE, bytelanes);
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	/* Actually we should probably panic if this fails */
697dd00cc48SYoann Padioleau 	if ((board = kzalloc(sizeof(*board), GFP_ATOMIC)) == NULL)
6981da177e4SLinus Torvalds 		return NULL;
6991da177e4SLinus Torvalds 	board->fblock = rp;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	/* Dump the format block for debugging purposes */
70271ae40e4SDavid Huggins-Daines 	pr_debug("Slot %X, format block at 0x%p:\n", slot, rp);
70371ae40e4SDavid Huggins-Daines 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
70471ae40e4SDavid Huggins-Daines 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
70571ae40e4SDavid Huggins-Daines 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
70671ae40e4SDavid Huggins-Daines 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
70771ae40e4SDavid Huggins-Daines 	pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
70871ae40e4SDavid Huggins-Daines 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
70971ae40e4SDavid Huggins-Daines 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
71071ae40e4SDavid Huggins-Daines 	pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
7111da177e4SLinus Torvalds 	rp = board->fblock;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	board->slot = slot;
7141da177e4SLinus Torvalds 	board->slot_addr = (unsigned long)nubus_slot_addr(slot);
7151da177e4SLinus Torvalds 	board->doffset = nubus_get_rom(&rp, 4, bytelanes);
7161da177e4SLinus Torvalds 	/* rom_length is *supposed* to be the total length of the
7171da177e4SLinus Torvalds 	 * ROM.  In practice it is the "amount of ROM used to compute
7181da177e4SLinus Torvalds 	 * the CRC."  So some jokers decide to set it to zero and
7191da177e4SLinus Torvalds 	 * set the crc to zero so they don't have to do any math.
7201da177e4SLinus Torvalds 	 * See the Performa 460 ROM, for example.  Those Apple "engineers".
7211da177e4SLinus Torvalds 	 */
7221da177e4SLinus Torvalds 	board->rom_length = nubus_get_rom(&rp, 4, bytelanes);
7231da177e4SLinus Torvalds 	board->crc = nubus_get_rom(&rp, 4, bytelanes);
7241da177e4SLinus Torvalds 	board->rev = nubus_get_rom(&rp, 1, bytelanes);
7251da177e4SLinus Torvalds 	board->format = nubus_get_rom(&rp, 1, bytelanes);
7261da177e4SLinus Torvalds 	board->lanes = bytelanes;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	/* Directory offset should be small and negative... */
7291da177e4SLinus Torvalds 	if (!(board->doffset & 0x00FF0000))
73071ae40e4SDavid Huggins-Daines 		pr_warn("Dodgy doffset!\n");
7311da177e4SLinus Torvalds 	dpat = nubus_get_rom(&rp, 4, bytelanes);
7321da177e4SLinus Torvalds 	if (dpat != NUBUS_TEST_PATTERN)
73371ae40e4SDavid Huggins-Daines 		pr_warn("Wrong test pattern %08lx!\n", dpat);
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	/*
7361da177e4SLinus Torvalds 	 *	I wonder how the CRC is meant to work -
7371da177e4SLinus Torvalds 	 *		any takers ?
7381da177e4SLinus Torvalds 	 * CSA: According to MAC docs, not all cards pass the CRC anyway,
7391da177e4SLinus Torvalds 	 * since the initial Macintosh ROM releases skipped the check.
7401da177e4SLinus Torvalds 	 */
7411da177e4SLinus Torvalds 
742475e6e15SDavid Huggins-Daines 	/* Set up the directory pointer */
743475e6e15SDavid Huggins-Daines 	board->directory = board->fblock;
744475e6e15SDavid Huggins-Daines 	nubus_move(&board->directory, nubus_expand32(board->doffset),
745475e6e15SDavid Huggins-Daines 	           board->lanes);
746475e6e15SDavid Huggins-Daines 
7471da177e4SLinus Torvalds 	nubus_get_root_dir(board, &dir);
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	/* We're ready to rock */
75071ae40e4SDavid Huggins-Daines 	pr_info("Slot %X:\n", slot);
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	/* Each slot should have one board resource and any number of
7531da177e4SLinus Torvalds 	   functional resources.  So we'll fill in some fields in the
7541da177e4SLinus Torvalds 	   struct nubus_board from the board resource, then walk down
7551da177e4SLinus Torvalds 	   the list of functional resources, spinning out a nubus_dev
7561da177e4SLinus Torvalds 	   for each of them. */
7571da177e4SLinus Torvalds 	if (nubus_readdir(&dir, &ent) == -1) {
7581da177e4SLinus Torvalds 		/* We can't have this! */
75971ae40e4SDavid Huggins-Daines 		pr_err("Board resource not found!\n");
7601da177e4SLinus Torvalds 		return NULL;
7611da177e4SLinus Torvalds 	} else {
76271ae40e4SDavid Huggins-Daines 		pr_info("  Board resource:\n");
7631da177e4SLinus Torvalds 		nubus_get_board_resource(board, slot, &ent);
7641da177e4SLinus Torvalds 	}
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	while (nubus_readdir(&dir, &ent) != -1) {
7671da177e4SLinus Torvalds 		struct nubus_dev *dev;
7681da177e4SLinus Torvalds 		struct nubus_dev **devp;
769f42e5550SFinn Thain 
7701da177e4SLinus Torvalds 		dev = nubus_get_functional_resource(board, slot, &ent);
7711da177e4SLinus Torvalds 		if (dev == NULL)
7721da177e4SLinus Torvalds 			continue;
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds 		/* We zeroed this out above */
7751da177e4SLinus Torvalds 		if (board->first_dev == NULL)
7761da177e4SLinus Torvalds 			board->first_dev = dev;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 		/* Put it on the global NuBus device chain. Keep entries in order. */
779f42e5550SFinn Thain 		for (devp = &nubus_devices; *devp != NULL;
780f42e5550SFinn Thain 		     devp = &((*devp)->next))
7811da177e4SLinus Torvalds 			/* spin */;
7821da177e4SLinus Torvalds 		*devp = dev;
7831da177e4SLinus Torvalds 		dev->next = NULL;
7841da177e4SLinus Torvalds 	}
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	/* Put it on the global NuBus board chain. Keep entries in order. */
787f42e5550SFinn Thain 	for (boardp = &nubus_boards; *boardp != NULL;
788f42e5550SFinn Thain 	     boardp = &((*boardp)->next))
7891da177e4SLinus Torvalds 		/* spin */;
7901da177e4SLinus Torvalds 	*boardp = board;
7911da177e4SLinus Torvalds 	board->next = NULL;
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	return board;
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds void __init nubus_probe_slot(int slot)
7971da177e4SLinus Torvalds {
7981da177e4SLinus Torvalds 	unsigned char dp;
7991da177e4SLinus Torvalds 	unsigned char *rp;
8001da177e4SLinus Torvalds 	int i;
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	rp = nubus_rom_addr(slot);
803f42e5550SFinn Thain 	for (i = 4; i; i--) {
8041da177e4SLinus Torvalds 		int card_present;
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 		rp--;
8071da177e4SLinus Torvalds 		card_present = hwreg_present(rp);
8081da177e4SLinus Torvalds 		if (!card_present)
8091da177e4SLinus Torvalds 			continue;
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 		dp = *rp;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 		/* The last byte of the format block consists of two
8141da177e4SLinus Torvalds 		   nybbles which are "mirror images" of each other.
8151da177e4SLinus Torvalds 		   These show us the valid bytelanes */
8161da177e4SLinus Torvalds 		if ((((dp >> 4) ^ dp) & 0x0F) != 0x0F)
8171da177e4SLinus Torvalds 			continue;
8181da177e4SLinus Torvalds 		/* Check that this value is actually *on* one of the
8191da177e4SLinus Torvalds 		   bytelanes it claims are valid! */
82085cc313aSFinn Thain 		if (not_useful(rp, dp))
8211da177e4SLinus Torvalds 			continue;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 		/* Looks promising.  Let's put it on the list. */
8241da177e4SLinus Torvalds 		nubus_add_board(slot, dp);
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 		return;
8271da177e4SLinus Torvalds 	}
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds void __init nubus_scan_bus(void)
8311da177e4SLinus Torvalds {
8321da177e4SLinus Torvalds 	int slot;
833f42e5550SFinn Thain 
834f42e5550SFinn Thain 	for (slot = 9; slot < 15; slot++) {
8351da177e4SLinus Torvalds 		nubus_probe_slot(slot);
8361da177e4SLinus Torvalds 	}
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds static int __init nubus_init(void)
8401da177e4SLinus Torvalds {
8411da177e4SLinus Torvalds 	if (!MACH_IS_MAC)
8421da177e4SLinus Torvalds 		return 0;
8431da177e4SLinus Torvalds 
84471ae40e4SDavid Huggins-Daines 	pr_info("NuBus: Scanning NuBus slots.\n");
8451da177e4SLinus Torvalds 	nubus_devices = NULL;
8461da177e4SLinus Torvalds 	nubus_boards = NULL;
8471da177e4SLinus Torvalds 	nubus_scan_bus();
8481da177e4SLinus Torvalds 	nubus_proc_init();
8491da177e4SLinus Torvalds 	return 0;
8501da177e4SLinus Torvalds }
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds subsys_initcall(nubus_init);
853