nubus.c 25 KB

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