nubus.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Macintosh Nubus Interface Code
  4. *
  5. * Originally by Alan Cox
  6. *
  7. * Mostly rewritten by David Huggins-Daines, C. Scott Ananian,
  8. * and others.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/string.h>
  13. #include <linux/nubus.h>
  14. #include <linux/errno.h>
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/slab.h>
  19. #include <asm/setup.h>
  20. #include <asm/page.h>
  21. #include <asm/hwtest.h>
  22. /* Constants */
  23. /* This is, of course, the size in bytelanes, rather than the size in
  24. actual bytes */
  25. #define FORMAT_BLOCK_SIZE 20
  26. #define ROM_DIR_OFFSET 0x24
  27. #define NUBUS_TEST_PATTERN 0x5A932BC7
  28. /* Globals */
  29. LIST_HEAD(nubus_func_rsrcs);
  30. /* Meaning of "bytelanes":
  31. The card ROM may appear on any or all bytes of each long word in
  32. NuBus memory. The low 4 bits of the "map" value found in the
  33. format block (at the top of the slot address space, as well as at
  34. the top of the MacOS ROM) tells us which bytelanes, i.e. which byte
  35. offsets within each longword, are valid. Thus:
  36. A map of 0x0f, as found in the MacOS ROM, means that all bytelanes
  37. are valid.
  38. A map of 0xf0 means that no bytelanes are valid (We pray that we
  39. will never encounter this, but stranger things have happened)
  40. A map of 0xe1 means that only the MSB of each long word is actually
  41. part of the card ROM. (We hope to never encounter NuBus on a
  42. little-endian machine. Again, stranger things have happened)
  43. A map of 0x78 means that only the LSB of each long word is valid.
  44. Etcetera, etcetera. Hopefully this clears up some confusion over
  45. what the following code actually does. */
  46. static inline int not_useful(void *p, int map)
  47. {
  48. unsigned long pv = (unsigned long)p;
  49. pv &= 3;
  50. if (map & (1 << pv))
  51. return 0;
  52. return 1;
  53. }
  54. static unsigned long nubus_get_rom(unsigned char **ptr, int len, int map)
  55. {
  56. /* This will hold the result */
  57. unsigned long v = 0;
  58. unsigned char *p = *ptr;
  59. while (len) {
  60. v <<= 8;
  61. while (not_useful(p, map))
  62. p++;
  63. v |= *p++;
  64. len--;
  65. }
  66. *ptr = p;
  67. return v;
  68. }
  69. static void nubus_rewind(unsigned char **ptr, int len, int map)
  70. {
  71. unsigned char *p = *ptr;
  72. while (len) {
  73. do {
  74. p--;
  75. } while (not_useful(p, map));
  76. len--;
  77. }
  78. *ptr = p;
  79. }
  80. static void nubus_advance(unsigned char **ptr, int len, int map)
  81. {
  82. unsigned char *p = *ptr;
  83. while (len) {
  84. while (not_useful(p, map))
  85. p++;
  86. p++;
  87. len--;
  88. }
  89. *ptr = p;
  90. }
  91. static void nubus_move(unsigned char **ptr, int len, int map)
  92. {
  93. unsigned long slot_space = (unsigned long)*ptr & 0xFF000000;
  94. if (len > 0)
  95. nubus_advance(ptr, len, map);
  96. else if (len < 0)
  97. nubus_rewind(ptr, -len, map);
  98. if (((unsigned long)*ptr & 0xFF000000) != slot_space)
  99. pr_err("%s: moved out of slot address space!\n", __func__);
  100. }
  101. /* Now, functions to read the sResource tree */
  102. /* Each sResource entry consists of a 1-byte ID and a 3-byte data
  103. field. If that data field contains an offset, then obviously we
  104. have to expand it from a 24-bit signed number to a 32-bit signed
  105. number. */
  106. static inline long nubus_expand32(long foo)
  107. {
  108. if (foo & 0x00800000) /* 24bit negative */
  109. foo |= 0xFF000000;
  110. return foo;
  111. }
  112. static inline void *nubus_rom_addr(int slot)
  113. {
  114. /*
  115. * Returns the first byte after the card. We then walk
  116. * backwards to get the lane register and the config
  117. */
  118. return (void *)(0xF1000000 + (slot << 24));
  119. }
  120. unsigned char *nubus_dirptr(const struct nubus_dirent *nd)
  121. {
  122. unsigned char *p = nd->base;
  123. /* Essentially, just step over the bytelanes using whatever
  124. offset we might have found */
  125. nubus_move(&p, nubus_expand32(nd->data), nd->mask);
  126. /* And return the value */
  127. return p;
  128. }
  129. /* These two are for pulling resource data blocks (i.e. stuff that's
  130. pointed to with offsets) out of the card ROM. */
  131. void nubus_get_rsrc_mem(void *dest, const struct nubus_dirent *dirent,
  132. unsigned int len)
  133. {
  134. unsigned char *t = (unsigned char *)dest;
  135. unsigned char *p = nubus_dirptr(dirent);
  136. while (len) {
  137. *t++ = nubus_get_rom(&p, 1, dirent->mask);
  138. len--;
  139. }
  140. }
  141. EXPORT_SYMBOL(nubus_get_rsrc_mem);
  142. unsigned int nubus_get_rsrc_str(char *dest, const struct nubus_dirent *dirent,
  143. unsigned int len)
  144. {
  145. char *t = dest;
  146. unsigned char *p = nubus_dirptr(dirent);
  147. while (len > 1) {
  148. unsigned char c = nubus_get_rom(&p, 1, dirent->mask);
  149. if (!c)
  150. break;
  151. *t++ = c;
  152. len--;
  153. }
  154. if (len > 0)
  155. *t = '\0';
  156. return t - dest;
  157. }
  158. EXPORT_SYMBOL(nubus_get_rsrc_str);
  159. void nubus_seq_write_rsrc_mem(struct seq_file *m,
  160. const struct nubus_dirent *dirent,
  161. unsigned int len)
  162. {
  163. unsigned long buf[32];
  164. unsigned int buf_size = sizeof(buf);
  165. unsigned char *p = nubus_dirptr(dirent);
  166. /* If possible, write out full buffers */
  167. while (len >= buf_size) {
  168. unsigned int i;
  169. for (i = 0; i < ARRAY_SIZE(buf); i++)
  170. buf[i] = nubus_get_rom(&p, sizeof(buf[0]),
  171. dirent->mask);
  172. seq_write(m, buf, buf_size);
  173. len -= buf_size;
  174. }
  175. /* If not, write out individual bytes */
  176. while (len--)
  177. seq_putc(m, nubus_get_rom(&p, 1, dirent->mask));
  178. }
  179. int nubus_get_root_dir(const struct nubus_board *board,
  180. struct nubus_dir *dir)
  181. {
  182. dir->ptr = dir->base = board->directory;
  183. dir->done = 0;
  184. dir->mask = board->lanes;
  185. return 0;
  186. }
  187. EXPORT_SYMBOL(nubus_get_root_dir);
  188. /* This is a slyly renamed version of the above */
  189. int nubus_get_func_dir(const struct nubus_rsrc *fres, struct nubus_dir *dir)
  190. {
  191. dir->ptr = dir->base = fres->directory;
  192. dir->done = 0;
  193. dir->mask = fres->board->lanes;
  194. return 0;
  195. }
  196. EXPORT_SYMBOL(nubus_get_func_dir);
  197. int nubus_get_board_dir(const struct nubus_board *board,
  198. struct nubus_dir *dir)
  199. {
  200. struct nubus_dirent ent;
  201. dir->ptr = dir->base = board->directory;
  202. dir->done = 0;
  203. dir->mask = board->lanes;
  204. /* Now dereference it (the first directory is always the board
  205. directory) */
  206. if (nubus_readdir(dir, &ent) == -1)
  207. return -1;
  208. if (nubus_get_subdir(&ent, dir) == -1)
  209. return -1;
  210. return 0;
  211. }
  212. EXPORT_SYMBOL(nubus_get_board_dir);
  213. int nubus_get_subdir(const struct nubus_dirent *ent,
  214. struct nubus_dir *dir)
  215. {
  216. dir->ptr = dir->base = nubus_dirptr(ent);
  217. dir->done = 0;
  218. dir->mask = ent->mask;
  219. return 0;
  220. }
  221. EXPORT_SYMBOL(nubus_get_subdir);
  222. int nubus_readdir(struct nubus_dir *nd, struct nubus_dirent *ent)
  223. {
  224. u32 resid;
  225. if (nd->done)
  226. return -1;
  227. /* Do this first, otherwise nubus_rewind & co are off by 4 */
  228. ent->base = nd->ptr;
  229. /* This moves nd->ptr forward */
  230. resid = nubus_get_rom(&nd->ptr, 4, nd->mask);
  231. /* EOL marker, as per the Apple docs */
  232. if ((resid & 0xff000000) == 0xff000000) {
  233. /* Mark it as done */
  234. nd->done = 1;
  235. return -1;
  236. }
  237. /* First byte is the resource ID */
  238. ent->type = resid >> 24;
  239. /* Low 3 bytes might contain data (or might not) */
  240. ent->data = resid & 0xffffff;
  241. ent->mask = nd->mask;
  242. return 0;
  243. }
  244. EXPORT_SYMBOL(nubus_readdir);
  245. int nubus_rewinddir(struct nubus_dir *dir)
  246. {
  247. dir->ptr = dir->base;
  248. dir->done = 0;
  249. return 0;
  250. }
  251. EXPORT_SYMBOL(nubus_rewinddir);
  252. /* Driver interface functions, more or less like in pci.c */
  253. struct nubus_rsrc *nubus_first_rsrc_or_null(void)
  254. {
  255. return list_first_entry_or_null(&nubus_func_rsrcs, struct nubus_rsrc,
  256. list);
  257. }
  258. EXPORT_SYMBOL(nubus_first_rsrc_or_null);
  259. struct nubus_rsrc *nubus_next_rsrc_or_null(struct nubus_rsrc *from)
  260. {
  261. if (list_is_last(&from->list, &nubus_func_rsrcs))
  262. return NULL;
  263. return list_next_entry(from, list);
  264. }
  265. EXPORT_SYMBOL(nubus_next_rsrc_or_null);
  266. int
  267. nubus_find_rsrc(struct nubus_dir *dir, unsigned char rsrc_type,
  268. struct nubus_dirent *ent)
  269. {
  270. while (nubus_readdir(dir, ent) != -1) {
  271. if (ent->type == rsrc_type)
  272. return 0;
  273. }
  274. return -1;
  275. }
  276. EXPORT_SYMBOL(nubus_find_rsrc);
  277. /* Initialization functions - decide which slots contain stuff worth
  278. looking at, and print out lots and lots of information from the
  279. resource blocks. */
  280. static int __init nubus_get_block_rsrc_dir(struct nubus_board *board,
  281. struct proc_dir_entry *procdir,
  282. const struct nubus_dirent *parent)
  283. {
  284. struct nubus_dir dir;
  285. struct nubus_dirent ent;
  286. nubus_get_subdir(parent, &dir);
  287. dir.procdir = nubus_proc_add_rsrc_dir(procdir, parent, board);
  288. while (nubus_readdir(&dir, &ent) != -1) {
  289. u32 size;
  290. nubus_get_rsrc_mem(&size, &ent, 4);
  291. pr_debug(" block (0x%x), size %d\n", ent.type, size);
  292. nubus_proc_add_rsrc_mem(dir.procdir, &ent, size);
  293. }
  294. return 0;
  295. }
  296. static int __init nubus_get_display_vidmode(struct nubus_board *board,
  297. struct proc_dir_entry *procdir,
  298. const struct nubus_dirent *parent)
  299. {
  300. struct nubus_dir dir;
  301. struct nubus_dirent ent;
  302. nubus_get_subdir(parent, &dir);
  303. dir.procdir = nubus_proc_add_rsrc_dir(procdir, parent, board);
  304. while (nubus_readdir(&dir, &ent) != -1) {
  305. switch (ent.type) {
  306. case 1: /* mVidParams */
  307. case 2: /* mTable */
  308. {
  309. u32 size;
  310. nubus_get_rsrc_mem(&size, &ent, 4);
  311. pr_debug(" block (0x%x), size %d\n", ent.type,
  312. size);
  313. nubus_proc_add_rsrc_mem(dir.procdir, &ent, size);
  314. break;
  315. }
  316. default:
  317. pr_debug(" unknown resource 0x%02x, data 0x%06x\n",
  318. ent.type, ent.data);
  319. nubus_proc_add_rsrc_mem(dir.procdir, &ent, 0);
  320. }
  321. }
  322. return 0;
  323. }
  324. static int __init nubus_get_display_resource(struct nubus_rsrc *fres,
  325. struct proc_dir_entry *procdir,
  326. const struct nubus_dirent *ent)
  327. {
  328. switch (ent->type) {
  329. case NUBUS_RESID_GAMMADIR:
  330. pr_debug(" gamma directory offset: 0x%06x\n", ent->data);
  331. nubus_get_block_rsrc_dir(fres->board, procdir, ent);
  332. break;
  333. case 0x0080 ... 0x0085:
  334. pr_debug(" mode 0x%02x info offset: 0x%06x\n",
  335. ent->type, ent->data);
  336. nubus_get_display_vidmode(fres->board, procdir, ent);
  337. break;
  338. default:
  339. pr_debug(" unknown resource 0x%02x, data 0x%06x\n",
  340. ent->type, ent->data);
  341. nubus_proc_add_rsrc_mem(procdir, ent, 0);
  342. }
  343. return 0;
  344. }
  345. static int __init nubus_get_network_resource(struct nubus_rsrc *fres,
  346. struct proc_dir_entry *procdir,
  347. const struct nubus_dirent *ent)
  348. {
  349. switch (ent->type) {
  350. case NUBUS_RESID_MAC_ADDRESS:
  351. {
  352. char addr[6];
  353. nubus_get_rsrc_mem(addr, ent, 6);
  354. pr_debug(" MAC address: %pM\n", addr);
  355. nubus_proc_add_rsrc_mem(procdir, ent, 6);
  356. break;
  357. }
  358. default:
  359. pr_debug(" unknown resource 0x%02x, data 0x%06x\n",
  360. ent->type, ent->data);
  361. nubus_proc_add_rsrc_mem(procdir, ent, 0);
  362. }
  363. return 0;
  364. }
  365. static int __init nubus_get_cpu_resource(struct nubus_rsrc *fres,
  366. struct proc_dir_entry *procdir,
  367. const struct nubus_dirent *ent)
  368. {
  369. switch (ent->type) {
  370. case NUBUS_RESID_MEMINFO:
  371. {
  372. unsigned long meminfo[2];
  373. nubus_get_rsrc_mem(&meminfo, ent, 8);
  374. pr_debug(" memory: [ 0x%08lx 0x%08lx ]\n",
  375. meminfo[0], meminfo[1]);
  376. nubus_proc_add_rsrc_mem(procdir, ent, 8);
  377. break;
  378. }
  379. case NUBUS_RESID_ROMINFO:
  380. {
  381. unsigned long rominfo[2];
  382. nubus_get_rsrc_mem(&rominfo, ent, 8);
  383. pr_debug(" ROM: [ 0x%08lx 0x%08lx ]\n",
  384. rominfo[0], rominfo[1]);
  385. nubus_proc_add_rsrc_mem(procdir, ent, 8);
  386. break;
  387. }
  388. default:
  389. pr_debug(" unknown resource 0x%02x, data 0x%06x\n",
  390. ent->type, ent->data);
  391. nubus_proc_add_rsrc_mem(procdir, ent, 0);
  392. }
  393. return 0;
  394. }
  395. static int __init nubus_get_private_resource(struct nubus_rsrc *fres,
  396. struct proc_dir_entry *procdir,
  397. const struct nubus_dirent *ent)
  398. {
  399. switch (fres->category) {
  400. case NUBUS_CAT_DISPLAY:
  401. nubus_get_display_resource(fres, procdir, ent);
  402. break;
  403. case NUBUS_CAT_NETWORK:
  404. nubus_get_network_resource(fres, procdir, ent);
  405. break;
  406. case NUBUS_CAT_CPU:
  407. nubus_get_cpu_resource(fres, procdir, ent);
  408. break;
  409. default:
  410. pr_debug(" unknown resource 0x%02x, data 0x%06x\n",
  411. ent->type, ent->data);
  412. nubus_proc_add_rsrc_mem(procdir, ent, 0);
  413. }
  414. return 0;
  415. }
  416. static struct nubus_rsrc * __init
  417. nubus_get_functional_resource(struct nubus_board *board, int slot,
  418. const struct nubus_dirent *parent)
  419. {
  420. struct nubus_dir dir;
  421. struct nubus_dirent ent;
  422. struct nubus_rsrc *fres;
  423. pr_debug(" Functional resource 0x%02x:\n", parent->type);
  424. nubus_get_subdir(parent, &dir);
  425. dir.procdir = nubus_proc_add_rsrc_dir(board->procdir, parent, board);
  426. /* Actually we should probably panic if this fails */
  427. fres = kzalloc(sizeof(*fres), GFP_ATOMIC);
  428. if (!fres)
  429. return NULL;
  430. fres->resid = parent->type;
  431. fres->directory = dir.base;
  432. fres->board = board;
  433. while (nubus_readdir(&dir, &ent) != -1) {
  434. switch (ent.type) {
  435. case NUBUS_RESID_TYPE:
  436. {
  437. unsigned short nbtdata[4];
  438. nubus_get_rsrc_mem(nbtdata, &ent, 8);
  439. fres->category = nbtdata[0];
  440. fres->type = nbtdata[1];
  441. fres->dr_sw = nbtdata[2];
  442. fres->dr_hw = nbtdata[3];
  443. pr_debug(" type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
  444. nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
  445. nubus_proc_add_rsrc_mem(dir.procdir, &ent, 8);
  446. break;
  447. }
  448. case NUBUS_RESID_NAME:
  449. {
  450. char name[64];
  451. unsigned int len;
  452. len = nubus_get_rsrc_str(name, &ent, sizeof(name));
  453. pr_debug(" name: %s\n", name);
  454. nubus_proc_add_rsrc_mem(dir.procdir, &ent, len + 1);
  455. break;
  456. }
  457. case NUBUS_RESID_DRVRDIR:
  458. {
  459. /* MacOS driver. If we were NetBSD we might
  460. use this :-) */
  461. pr_debug(" driver directory offset: 0x%06x\n",
  462. ent.data);
  463. nubus_get_block_rsrc_dir(board, dir.procdir, &ent);
  464. break;
  465. }
  466. case NUBUS_RESID_MINOR_BASEOS:
  467. {
  468. /* We will need this in order to support
  469. multiple framebuffers. It might be handy
  470. for Ethernet as well */
  471. u32 base_offset;
  472. nubus_get_rsrc_mem(&base_offset, &ent, 4);
  473. pr_debug(" memory offset: 0x%08x\n", base_offset);
  474. nubus_proc_add_rsrc_mem(dir.procdir, &ent, 4);
  475. break;
  476. }
  477. case NUBUS_RESID_MINOR_LENGTH:
  478. {
  479. /* Ditto */
  480. u32 length;
  481. nubus_get_rsrc_mem(&length, &ent, 4);
  482. pr_debug(" memory length: 0x%08x\n", length);
  483. nubus_proc_add_rsrc_mem(dir.procdir, &ent, 4);
  484. break;
  485. }
  486. case NUBUS_RESID_FLAGS:
  487. pr_debug(" flags: 0x%06x\n", ent.data);
  488. nubus_proc_add_rsrc(dir.procdir, &ent);
  489. break;
  490. case NUBUS_RESID_HWDEVID:
  491. pr_debug(" hwdevid: 0x%06x\n", ent.data);
  492. nubus_proc_add_rsrc(dir.procdir, &ent);
  493. break;
  494. default:
  495. /* Local/Private resources have their own
  496. function */
  497. nubus_get_private_resource(fres, dir.procdir, &ent);
  498. }
  499. }
  500. return fres;
  501. }
  502. /* This is *really* cool. */
  503. static int __init nubus_get_icon(struct nubus_board *board,
  504. struct proc_dir_entry *procdir,
  505. const struct nubus_dirent *ent)
  506. {
  507. /* Should be 32x32 if my memory serves me correctly */
  508. u32 icon[32];
  509. int i;
  510. nubus_get_rsrc_mem(&icon, ent, 128);
  511. pr_debug(" icon:\n");
  512. for (i = 0; i < 8; i++)
  513. pr_debug(" %08x %08x %08x %08x\n",
  514. icon[i * 4 + 0], icon[i * 4 + 1],
  515. icon[i * 4 + 2], icon[i * 4 + 3]);
  516. nubus_proc_add_rsrc_mem(procdir, ent, 128);
  517. return 0;
  518. }
  519. static int __init nubus_get_vendorinfo(struct nubus_board *board,
  520. struct proc_dir_entry *procdir,
  521. const struct nubus_dirent *parent)
  522. {
  523. struct nubus_dir dir;
  524. struct nubus_dirent ent;
  525. static char *vendor_fields[6] = { "ID", "serial", "revision",
  526. "part", "date", "unknown field" };
  527. pr_debug(" vendor info:\n");
  528. nubus_get_subdir(parent, &dir);
  529. dir.procdir = nubus_proc_add_rsrc_dir(procdir, parent, board);
  530. while (nubus_readdir(&dir, &ent) != -1) {
  531. char name[64];
  532. unsigned int len;
  533. /* These are all strings, we think */
  534. len = nubus_get_rsrc_str(name, &ent, sizeof(name));
  535. if (ent.type < 1 || ent.type > 5)
  536. ent.type = 5;
  537. pr_debug(" %s: %s\n", vendor_fields[ent.type - 1], name);
  538. nubus_proc_add_rsrc_mem(dir.procdir, &ent, len + 1);
  539. }
  540. return 0;
  541. }
  542. static int __init nubus_get_board_resource(struct nubus_board *board, int slot,
  543. const struct nubus_dirent *parent)
  544. {
  545. struct nubus_dir dir;
  546. struct nubus_dirent ent;
  547. pr_debug(" Board resource 0x%02x:\n", parent->type);
  548. nubus_get_subdir(parent, &dir);
  549. dir.procdir = nubus_proc_add_rsrc_dir(board->procdir, parent, board);
  550. while (nubus_readdir(&dir, &ent) != -1) {
  551. switch (ent.type) {
  552. case NUBUS_RESID_TYPE:
  553. {
  554. unsigned short nbtdata[4];
  555. /* This type is always the same, and is not
  556. useful except insofar as it tells us that
  557. we really are looking at a board resource. */
  558. nubus_get_rsrc_mem(nbtdata, &ent, 8);
  559. pr_debug(" type: [cat 0x%x type 0x%x sw 0x%x hw 0x%x]\n",
  560. nbtdata[0], nbtdata[1], nbtdata[2], nbtdata[3]);
  561. if (nbtdata[0] != 1 || nbtdata[1] != 0 ||
  562. nbtdata[2] != 0 || nbtdata[3] != 0)
  563. pr_err("Slot %X: sResource is not a board resource!\n",
  564. slot);
  565. nubus_proc_add_rsrc_mem(dir.procdir, &ent, 8);
  566. break;
  567. }
  568. case NUBUS_RESID_NAME:
  569. {
  570. unsigned int len;
  571. len = nubus_get_rsrc_str(board->name, &ent,
  572. sizeof(board->name));
  573. pr_debug(" name: %s\n", board->name);
  574. nubus_proc_add_rsrc_mem(dir.procdir, &ent, len + 1);
  575. break;
  576. }
  577. case NUBUS_RESID_ICON:
  578. nubus_get_icon(board, dir.procdir, &ent);
  579. break;
  580. case NUBUS_RESID_BOARDID:
  581. pr_debug(" board id: 0x%x\n", ent.data);
  582. nubus_proc_add_rsrc(dir.procdir, &ent);
  583. break;
  584. case NUBUS_RESID_PRIMARYINIT:
  585. pr_debug(" primary init offset: 0x%06x\n", ent.data);
  586. nubus_proc_add_rsrc(dir.procdir, &ent);
  587. break;
  588. case NUBUS_RESID_VENDORINFO:
  589. nubus_get_vendorinfo(board, dir.procdir, &ent);
  590. break;
  591. case NUBUS_RESID_FLAGS:
  592. pr_debug(" flags: 0x%06x\n", ent.data);
  593. nubus_proc_add_rsrc(dir.procdir, &ent);
  594. break;
  595. case NUBUS_RESID_HWDEVID:
  596. pr_debug(" hwdevid: 0x%06x\n", ent.data);
  597. nubus_proc_add_rsrc(dir.procdir, &ent);
  598. break;
  599. case NUBUS_RESID_SECONDINIT:
  600. pr_debug(" secondary init offset: 0x%06x\n",
  601. ent.data);
  602. nubus_proc_add_rsrc(dir.procdir, &ent);
  603. break;
  604. /* WTF isn't this in the functional resources? */
  605. case NUBUS_RESID_VIDNAMES:
  606. pr_debug(" vidnames directory offset: 0x%06x\n",
  607. ent.data);
  608. nubus_get_block_rsrc_dir(board, dir.procdir, &ent);
  609. break;
  610. /* Same goes for this */
  611. case NUBUS_RESID_VIDMODES:
  612. pr_debug(" video mode parameter directory offset: 0x%06x\n",
  613. ent.data);
  614. nubus_proc_add_rsrc(dir.procdir, &ent);
  615. break;
  616. default:
  617. pr_debug(" unknown resource 0x%02x, data 0x%06x\n",
  618. ent.type, ent.data);
  619. nubus_proc_add_rsrc_mem(dir.procdir, &ent, 0);
  620. }
  621. }
  622. return 0;
  623. }
  624. static void __init nubus_add_board(int slot, int bytelanes)
  625. {
  626. struct nubus_board *board;
  627. unsigned char *rp;
  628. unsigned long dpat;
  629. struct nubus_dir dir;
  630. struct nubus_dirent ent;
  631. int prev_resid = -1;
  632. /* Move to the start of the format block */
  633. rp = nubus_rom_addr(slot);
  634. nubus_rewind(&rp, FORMAT_BLOCK_SIZE, bytelanes);
  635. /* Actually we should probably panic if this fails */
  636. if ((board = kzalloc(sizeof(*board), GFP_ATOMIC)) == NULL)
  637. return;
  638. board->fblock = rp;
  639. /* Dump the format block for debugging purposes */
  640. pr_debug("Slot %X, format block at 0x%p:\n", slot, rp);
  641. pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
  642. pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
  643. pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
  644. pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
  645. pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
  646. pr_debug("%08lx\n", nubus_get_rom(&rp, 4, bytelanes));
  647. pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
  648. pr_debug("%02lx\n", nubus_get_rom(&rp, 1, bytelanes));
  649. rp = board->fblock;
  650. board->slot = slot;
  651. board->slot_addr = (unsigned long)nubus_slot_addr(slot);
  652. board->doffset = nubus_get_rom(&rp, 4, bytelanes);
  653. /* rom_length is *supposed* to be the total length of the
  654. * ROM. In practice it is the "amount of ROM used to compute
  655. * the CRC." So some jokers decide to set it to zero and
  656. * set the crc to zero so they don't have to do any math.
  657. * See the Performa 460 ROM, for example. Those Apple "engineers".
  658. */
  659. board->rom_length = nubus_get_rom(&rp, 4, bytelanes);
  660. board->crc = nubus_get_rom(&rp, 4, bytelanes);
  661. board->rev = nubus_get_rom(&rp, 1, bytelanes);
  662. board->format = nubus_get_rom(&rp, 1, bytelanes);
  663. board->lanes = bytelanes;
  664. /* Directory offset should be small and negative... */
  665. if (!(board->doffset & 0x00FF0000))
  666. pr_warn("Slot %X: Dodgy doffset!\n", slot);
  667. dpat = nubus_get_rom(&rp, 4, bytelanes);
  668. if (dpat != NUBUS_TEST_PATTERN)
  669. pr_warn("Slot %X: Wrong test pattern %08lx!\n", slot, dpat);
  670. /*
  671. * I wonder how the CRC is meant to work -
  672. * any takers ?
  673. * CSA: According to MAC docs, not all cards pass the CRC anyway,
  674. * since the initial Macintosh ROM releases skipped the check.
  675. */
  676. /* Set up the directory pointer */
  677. board->directory = board->fblock;
  678. nubus_move(&board->directory, nubus_expand32(board->doffset),
  679. board->lanes);
  680. nubus_get_root_dir(board, &dir);
  681. /* We're ready to rock */
  682. pr_debug("Slot %X resources:\n", slot);
  683. /* Each slot should have one board resource and any number of
  684. * functional resources. So we'll fill in some fields in the
  685. * struct nubus_board from the board resource, then walk down
  686. * the list of functional resources, spinning out a nubus_rsrc
  687. * for each of them.
  688. */
  689. if (nubus_readdir(&dir, &ent) == -1) {
  690. /* We can't have this! */
  691. pr_err("Slot %X: Board resource not found!\n", slot);
  692. kfree(board);
  693. return;
  694. }
  695. if (ent.type < 1 || ent.type > 127)
  696. pr_warn("Slot %X: Board resource ID is invalid!\n", slot);
  697. board->procdir = nubus_proc_add_board(board);
  698. nubus_get_board_resource(board, slot, &ent);
  699. while (nubus_readdir(&dir, &ent) != -1) {
  700. struct nubus_rsrc *fres;
  701. fres = nubus_get_functional_resource(board, slot, &ent);
  702. if (fres == NULL)
  703. continue;
  704. /* Resources should appear in ascending ID order. This sanity
  705. * check prevents duplicate resource IDs.
  706. */
  707. if (fres->resid <= prev_resid) {
  708. kfree(fres);
  709. continue;
  710. }
  711. prev_resid = fres->resid;
  712. list_add_tail(&fres->list, &nubus_func_rsrcs);
  713. }
  714. if (nubus_device_register(board))
  715. put_device(&board->dev);
  716. }
  717. static void __init nubus_probe_slot(int slot)
  718. {
  719. unsigned char dp;
  720. unsigned char *rp;
  721. int i;
  722. rp = nubus_rom_addr(slot);
  723. for (i = 4; i; i--) {
  724. rp--;
  725. if (!hwreg_present(rp))
  726. continue;
  727. dp = *rp;
  728. /* The last byte of the format block consists of two
  729. nybbles which are "mirror images" of each other.
  730. These show us the valid bytelanes */
  731. if ((((dp >> 4) ^ dp) & 0x0F) != 0x0F)
  732. continue;
  733. /* Check that this value is actually *on* one of the
  734. bytelanes it claims are valid! */
  735. if (not_useful(rp, dp))
  736. continue;
  737. /* Looks promising. Let's put it on the list. */
  738. nubus_add_board(slot, dp);
  739. return;
  740. }
  741. }
  742. static void __init nubus_scan_bus(void)
  743. {
  744. int slot;
  745. pr_info("NuBus: Scanning NuBus slots.\n");
  746. for (slot = 9; slot < 15; slot++) {
  747. nubus_probe_slot(slot);
  748. }
  749. }
  750. static int __init nubus_init(void)
  751. {
  752. int err;
  753. if (!MACH_IS_MAC)
  754. return 0;
  755. nubus_proc_init();
  756. err = nubus_parent_device_register();
  757. if (err)
  758. return err;
  759. nubus_scan_bus();
  760. return 0;
  761. }
  762. subsys_initcall(nubus_init);