📄 nubus.c
字号:
/* * Macintosh Nubus Interface Code * * Originally by Alan Cox * * Mostly rewritten by David Huggins-Daines, C. Scott Ananian, * and others. */#include <linux/config.h>#include <linux/ptrace.h>#include <linux/types.h>#include <linux/kernel.h>#include <linux/string.h>#include <linux/nubus.h>#include <linux/errno.h>#include <linux/init.h>#include <linux/delay.h>#include <asm/setup.h>#include <asm/system.h>#include <asm/page.h>#include <asm/hwtest.h>#include <linux/proc_fs.h>#include <asm/mac_via.h>#include <asm/mac_oss.h>extern void via_nubus_init(void);extern void oss_nubus_init(void);/* Constants *//* This is, of course, the size in bytelanes, rather than the size in actual bytes */#define FORMAT_BLOCK_SIZE 20#define ROM_DIR_OFFSET 0x24#define NUBUS_TEST_PATTERN 0x5A932BC7/* Define this if you like to live dangerously - it is known not to work on pretty much every machine except the Quadra 630 and the LC III. */#undef I_WANT_TO_PROBE_SLOT_ZERO/* This sometimes helps combat failure to boot */#undef TRY_TO_DODGE_WSOD/* Globals */struct nubus_dev* nubus_devices;struct nubus_board* nubus_boards;/* Meaning of "bytelanes": The card ROM may appear on any or all bytes of each long word in NuBus memory. The low 4 bits of the "map" value found in the format block (at the top of the slot address space, as well as at the top of the MacOS ROM) tells us which bytelanes, i.e. which byte offsets within each longword, are valid. Thus: A map of 0x0f, as found in the MacOS ROM, means that all bytelanes are valid. A map of 0xf0 means that no bytelanes are valid (We pray that we will never encounter this, but stranger things have happened) A map of 0xe1 means that only the MSB of each long word is actually part of the card ROM. (We hope to never encounter NuBus on a little-endian machine. Again, stranger things have happened) A map of 0x78 means that only the LSB of each long word is valid. Etcetera, etcetera. Hopefully this clears up some confusion over what the following code actually does. */ extern inline int not_useful(void *p, int map){ unsigned long pv=(unsigned long)p; pv &= 3; if(map & (1<<pv)) return 0; return 1;} static unsigned long nubus_get_rom(unsigned char **ptr, int len, int map){ /* This will hold the result */ unsigned long v = 0; unsigned char *p = *ptr; while(len) { v <<= 8; while(not_useful(p,map)) p++; v |= *p++; len--; } *ptr = p; return v;}static void nubus_rewind(unsigned char **ptr, int len, int map){ unsigned char *p=*ptr; /* Sanity check */ if(len > 65536) printk(KERN_ERR "rewind of 0x%08x!\n", len); while(len) { do { p--; } while(not_useful(p, map)); len--; } *ptr=p;}static void nubus_advance(unsigned char **ptr, int len, int map){ unsigned char *p = *ptr; if(len>65536) printk(KERN_ERR "advance of 0x%08x!\n", len); while(len) { while(not_useful(p,map)) p++; p++; len--; } *ptr = p;}static void nubus_move(unsigned char **ptr, int len, int map){ if(len > 0) nubus_advance(ptr, len, map); else if(len < 0) nubus_rewind(ptr, -len, map);}/* Now, functions to read the sResource tree *//* Each sResource entry consists of a 1-byte ID and a 3-byte data field. If that data field contains an offset, then obviously we have to expand it from a 24-bit signed number to a 32-bit signed number. */extern inline long nubus_expand32(long foo){ if(foo & 0x00800000) /* 24bit negative */ foo |= 0xFF000000; return foo;}extern inline void *nubus_rom_addr(int slot){ /* * Returns the first byte after the card. We then walk * backwards to get the lane register and the config */ return (void *)(0xF1000000+(slot<<24));}static unsigned char *nubus_dirptr(const struct nubus_dirent *nd){ unsigned char *p = nd->base; /* Essentially, just step over the bytelanes using whatever offset we might have found */ nubus_move(&p, nubus_expand32(nd->data), nd->mask); /* And return the value */ return p;}/* These two are for pulling resource data blocks (i.e. stuff that's pointed to with offsets) out of the card ROM. */void nubus_get_rsrc_mem(void *dest, const struct nubus_dirent* dirent, int len){ unsigned char *t = (unsigned char *)dest; unsigned char *p = nubus_dirptr(dirent); while(len) { *t++ = nubus_get_rom(&p, 1, dirent->mask); len--; }}void nubus_get_rsrc_str(void *dest, const struct nubus_dirent* dirent, int len){ unsigned char *t=(unsigned char *)dest; unsigned char *p = nubus_dirptr(dirent); while(len) { *t = nubus_get_rom(&p, 1, dirent->mask); if(!*t++) break; len--; }}int nubus_get_root_dir(const struct nubus_board* board, struct nubus_dir* dir){ dir->ptr = dir->base = board->directory; dir->done = 0; dir->mask = board->lanes; return 0;}/* This is a slyly renamed version of the above */int nubus_get_func_dir(const struct nubus_dev* dev, struct nubus_dir* dir){ dir->ptr = dir->base = dev->directory; dir->done = 0; dir->mask = dev->board->lanes; return 0;}int nubus_get_board_dir(const struct nubus_board* board, struct nubus_dir* dir){ struct nubus_dirent ent; dir->ptr = dir->base = board->directory; dir->done = 0; dir->mask = board->lanes; /* Now dereference it (the first directory is always the board directory) */ if (nubus_readdir(dir, &ent) == -1) return -1; if (nubus_get_subdir(&ent, dir) == -1) return -1; return 0;}int nubus_get_subdir(const struct nubus_dirent *ent, struct nubus_dir *dir){ dir->ptr = dir->base = nubus_dirptr(ent); dir->done = 0; dir->mask = ent->mask; return 0;}int nubus_readdir(struct nubus_dir *nd, struct nubus_dirent *ent){ u32 resid; if (nd->done) return -1; /* Do this first, otherwise nubus_rewind & co are off by 4 */ ent->base = nd->ptr; /* This moves nd->ptr forward */ resid = nubus_get_rom(&nd->ptr, 4, nd->mask); /* EOL marker, as per the Apple docs */ if((resid&0xff000000) == 0xff000000) { /* Mark it as done */ nd->done = 1; return -1; } /* First byte is the resource ID */ ent->type = resid >> 24; /* Low 3 bytes might contain data (or might not) */ ent->data = resid & 0xffffff; ent->mask = nd->mask; return 0;}int nubus_rewinddir(struct nubus_dir* dir){ dir->ptr = dir->base; return 0;}/* Driver interface functions, more or less like in pci.c */struct nubus_dev*nubus_find_device(unsigned short category, unsigned short type, unsigned short dr_hw, unsigned short dr_sw, const struct nubus_dev* from){ struct nubus_dev* itor = from ? from->next : nubus_devices; while (itor) { if (itor->category == category && itor->type == type && itor->dr_hw == dr_hw && itor->dr_sw == dr_sw) return itor; itor = itor->next; } return NULL;}struct nubus_dev*nubus_find_type(unsigned short category, unsigned short type, const struct nubus_dev* from){ struct nubus_dev* itor = from ? from->next : nubus_devices; while (itor) { if (itor->category == category && itor->type == type) return itor; itor = itor->next; } return NULL;}struct nubus_dev*nubus_find_slot(unsigned int slot, const struct nubus_dev* from){ struct nubus_dev* itor = from ? from->next : nubus_devices; while (itor) { if (itor->board->slot == slot) return itor; itor = itor->next; } return NULL;}intnubus_find_rsrc(struct nubus_dir* dir, unsigned char rsrc_type, struct nubus_dirent* ent){ while (nubus_readdir(dir, ent) != -1) { if (ent->type == rsrc_type) return 0; } return -1;}/* Initialization functions - decide which slots contain stuff worth looking at, and print out lots and lots of information from the resource blocks. *//* FIXME: A lot of this stuff will eventually be useful after initializaton, for intelligently probing Ethernet and video chips, among other things. The rest of it should go in the /proc code. For now, we just use it to give verbose boot logs. */static int __init nubus_show_display_resource(struct nubus_dev* dev, const struct nubus_dirent* ent){ switch (ent->type) { case NUBUS_RESID_GAMMADIR: printk(KERN_INFO " gamma directory offset: 0x%06x\n", ent->data); break; case 0x0080 ... 0x0085: printk(KERN_INFO " mode %02X info offset: 0x%06x\n", ent->type, ent->data); break; default: printk(KERN_INFO " unknown resource %02X, data 0x%06x\n", ent->type, ent->data); } return 0;}static int __init nubus_show_network_resource(struct nubus_dev* dev, const struct nubus_dirent* ent){ switch (ent->type) { case NUBUS_RESID_MAC_ADDRESS: { char addr[6]; int i; nubus_get_rsrc_mem(addr, ent, 6); printk(KERN_INFO " MAC address: "); for (i = 0; i < 6; i++) printk("%02x%s", addr[i] & 0xff, i == 5 ? "" : ":"); printk("\n"); break; } default: printk(KERN_INFO " unknown resource %02X, data 0x%06x\n", ent->type, ent->data); } return 0;}static int __init nubus_show_cpu_resource(struct nubus_dev* dev, const struct nubus_dirent* ent){ switch (ent->type) { case NUBUS_RESID_MEMINFO: { unsigned long meminfo[2]; nubus_get_rsrc_mem(&meminfo, ent, 8); printk(KERN_INFO " memory: [ 0x%08lx 0x%08lx ]\n", meminfo[0], meminfo[1]); break; } case NUBUS_RESID_ROMINFO: { unsigned long rominfo[2]; nubus_get_rsrc_mem(&rominfo, ent, 8); printk(KERN_INFO " ROM: [ 0x%08lx 0x%08lx ]\n", rominfo[0], rominfo[1]); break; } default: printk(KERN_INFO " unknown resource %02X, data 0x%06x\n", ent->type, ent->data); } return 0;}static int __init nubus_show_private_resource(struct nubus_dev* dev, const struct nubus_dirent* ent){ switch (dev->category) { case NUBUS_CAT_DISPLAY: nubus_show_display_resource(dev, ent); break; case NUBUS_CAT_NETWORK: nubus_show_network_resource(dev, ent); break; case NUBUS_CAT_CPU: nubus_show_cpu_resource(dev, ent); break; default: printk(KERN_INFO " unknown resource %02X, data 0x%06x\n", ent->type, ent->data); } return 0;}static struct nubus_dev* __init nubus_get_functional_resource(struct nubus_board* board, int slot, const struct nubus_dirent* parent){ struct nubus_dir dir; struct nubus_dirent ent; struct nubus_dev* dev; printk(KERN_INFO " Function 0x%02x:\n", parent->type); nubus_get_subdir(parent, &dir); /* Apple seems to have botched the ROM on the IIx */ if (slot == 0 && (unsigned long)dir.base % 2) dir.base += 1; if (console_loglevel >= 10) printk(KERN_DEBUG "nubus_get_functional_resource: parent is 0x%p, dir is 0x%p\n", parent->base, dir.base); /* Actually we should probably panic if this fails */ if ((dev = kmalloc(sizeof(*dev), GFP_ATOMIC)) == NULL) return NULL; memset(dev, 0, sizeof(*dev)); dev->resid = parent->type; dev->directory = dir.base; dev->board = board; while (nubus_readdir(&dir, &ent) != -1) { switch(ent.type) { case NUBUS_RESID_TYPE: { unsigned short nbtdata[4]; nubus_get_rsrc_mem(nbtdata, &ent, 8); dev->category = nbtdata[0]; dev->type = nbtdata[1]; dev->dr_sw = nbtdata[2]; dev->dr_hw = nbtdata[3]; printk(KERN_INFO " type: [cat 0x%x type 0x%x hw 0x%x sw 0x%x]\n", nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]); break; } case NUBUS_RESID_NAME: { nubus_get_rsrc_str(dev->name, &ent, 64); printk(KERN_INFO " name: %s\n", dev->name); break; } case NUBUS_RESID_DRVRDIR: { /* MacOS driver. If we were NetBSD we might use this :-) */ struct nubus_dir drvr_dir; struct nubus_dirent drvr_ent; nubus_get_subdir(&ent, &drvr_dir); nubus_readdir(&drvr_dir, &drvr_ent); dev->driver = nubus_dirptr(&drvr_ent); printk(KERN_INFO " driver at: 0x%p\n", dev->driver); break; } case NUBUS_RESID_MINOR_BASEOS: /* We will need this in order to support multiple framebuffers. It might be handy for Ethernet as well */ nubus_get_rsrc_mem(&dev->iobase, &ent, 4); printk(KERN_INFO " memory offset: 0x%08lx\n", dev->iobase); break; case NUBUS_RESID_MINOR_LENGTH:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -