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