prom_32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Procedures for creating, accessing and interpreting the device tree.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * Adapted for sparc32 by David S. Miller davem@davemloft.net
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <linux/mm.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/module.h>
  23. #include <asm/prom.h>
  24. #include <asm/oplib.h>
  25. #include "prom.h"
  26. static unsigned int prom_early_allocated;
  27. void * __init prom_early_alloc(unsigned long size)
  28. {
  29. void *ret;
  30. ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
  31. if (ret != NULL)
  32. memset(ret, 0, size);
  33. prom_early_allocated += size;
  34. return ret;
  35. }
  36. static int is_root_node(const struct device_node *dp)
  37. {
  38. if (!dp)
  39. return 0;
  40. return (dp->parent == NULL);
  41. }
  42. /* The following routines deal with the black magic of fully naming a
  43. * node.
  44. *
  45. * Certain well known named nodes are just the simple name string.
  46. *
  47. * Actual devices have an address specifier appended to the base name
  48. * string, like this "foo@addr". The "addr" can be in any number of
  49. * formats, and the platform plus the type of the node determine the
  50. * format and how it is constructed.
  51. *
  52. * For children of the ROOT node, the naming convention is fixed and
  53. * determined by whether this is a sun4u or sun4v system.
  54. *
  55. * For children of other nodes, it is bus type specific. So
  56. * we walk up the tree until we discover a "device_type" property
  57. * we recognize and we go from there.
  58. */
  59. static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
  60. {
  61. struct linux_prom_registers *regs;
  62. struct property *rprop;
  63. rprop = of_find_property(dp, "reg", NULL);
  64. if (!rprop)
  65. return;
  66. regs = rprop->value;
  67. sprintf(tmp_buf, "%s@%x,%x",
  68. dp->name,
  69. regs->which_io, regs->phys_addr);
  70. }
  71. /* "name@slot,offset" */
  72. static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
  73. {
  74. struct linux_prom_registers *regs;
  75. struct property *prop;
  76. prop = of_find_property(dp, "reg", NULL);
  77. if (!prop)
  78. return;
  79. regs = prop->value;
  80. sprintf(tmp_buf, "%s@%x,%x",
  81. dp->name,
  82. regs->which_io,
  83. regs->phys_addr);
  84. }
  85. /* "name@devnum[,func]" */
  86. static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
  87. {
  88. struct linux_prom_pci_registers *regs;
  89. struct property *prop;
  90. unsigned int devfn;
  91. prop = of_find_property(dp, "reg", NULL);
  92. if (!prop)
  93. return;
  94. regs = prop->value;
  95. devfn = (regs->phys_hi >> 8) & 0xff;
  96. if (devfn & 0x07) {
  97. sprintf(tmp_buf, "%s@%x,%x",
  98. dp->name,
  99. devfn >> 3,
  100. devfn & 0x07);
  101. } else {
  102. sprintf(tmp_buf, "%s@%x",
  103. dp->name,
  104. devfn >> 3);
  105. }
  106. }
  107. /* "name@addrhi,addrlo" */
  108. static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
  109. {
  110. struct linux_prom_registers *regs;
  111. struct property *prop;
  112. prop = of_find_property(dp, "reg", NULL);
  113. if (!prop)
  114. return;
  115. regs = prop->value;
  116. sprintf(tmp_buf, "%s@%x,%x",
  117. dp->name,
  118. regs->which_io, regs->phys_addr);
  119. }
  120. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  121. {
  122. struct device_node *parent = dp->parent;
  123. if (parent != NULL) {
  124. if (!strcmp(parent->type, "pci") ||
  125. !strcmp(parent->type, "pciex"))
  126. return pci_path_component(dp, tmp_buf);
  127. if (!strcmp(parent->type, "sbus"))
  128. return sbus_path_component(dp, tmp_buf);
  129. if (!strcmp(parent->type, "ebus"))
  130. return ebus_path_component(dp, tmp_buf);
  131. /* "isa" is handled with platform naming */
  132. }
  133. /* Use platform naming convention. */
  134. return sparc32_path_component(dp, tmp_buf);
  135. }
  136. static char * __init build_path_component(struct device_node *dp)
  137. {
  138. char tmp_buf[64], *n;
  139. tmp_buf[0] = '\0';
  140. __build_path_component(dp, tmp_buf);
  141. if (tmp_buf[0] == '\0')
  142. strcpy(tmp_buf, dp->name);
  143. n = prom_early_alloc(strlen(tmp_buf) + 1);
  144. strcpy(n, tmp_buf);
  145. return n;
  146. }
  147. static char * __init build_full_name(struct device_node *dp)
  148. {
  149. int len, ourlen, plen;
  150. char *n;
  151. plen = strlen(dp->parent->full_name);
  152. ourlen = strlen(dp->path_component_name);
  153. len = ourlen + plen + 2;
  154. n = prom_early_alloc(len);
  155. strcpy(n, dp->parent->full_name);
  156. if (!is_root_node(dp->parent)) {
  157. strcpy(n + plen, "/");
  158. plen++;
  159. }
  160. strcpy(n + plen, dp->path_component_name);
  161. return n;
  162. }
  163. static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
  164. {
  165. static struct property *tmp = NULL;
  166. struct property *p;
  167. int len;
  168. const char *name;
  169. if (tmp) {
  170. p = tmp;
  171. memset(p, 0, sizeof(*p) + 32);
  172. tmp = NULL;
  173. } else {
  174. p = prom_early_alloc(sizeof(struct property) + 32);
  175. p->unique_id = prom_unique_id++;
  176. }
  177. p->name = (char *) (p + 1);
  178. if (special_name) {
  179. strcpy(p->name, special_name);
  180. p->length = special_len;
  181. p->value = prom_early_alloc(special_len);
  182. memcpy(p->value, special_val, special_len);
  183. } else {
  184. if (prev == NULL) {
  185. name = prom_firstprop(node, NULL);
  186. } else {
  187. name = prom_nextprop(node, prev, NULL);
  188. }
  189. if (strlen(name) == 0) {
  190. tmp = p;
  191. return NULL;
  192. }
  193. strcpy(p->name, name);
  194. p->length = prom_getproplen(node, p->name);
  195. if (p->length <= 0) {
  196. p->length = 0;
  197. } else {
  198. p->value = prom_early_alloc(p->length + 1);
  199. len = prom_getproperty(node, p->name, p->value,
  200. p->length);
  201. if (len <= 0)
  202. p->length = 0;
  203. ((unsigned char *)p->value)[p->length] = '\0';
  204. }
  205. }
  206. return p;
  207. }
  208. static struct property * __init build_prop_list(phandle node)
  209. {
  210. struct property *head, *tail;
  211. head = tail = build_one_prop(node, NULL,
  212. ".node", &node, sizeof(node));
  213. tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
  214. tail = tail->next;
  215. while(tail) {
  216. tail->next = build_one_prop(node, tail->name,
  217. NULL, NULL, 0);
  218. tail = tail->next;
  219. }
  220. return head;
  221. }
  222. static char * __init get_one_property(phandle node, char *name)
  223. {
  224. char *buf = "<NULL>";
  225. int len;
  226. len = prom_getproplen(node, name);
  227. if (len > 0) {
  228. buf = prom_early_alloc(len);
  229. len = prom_getproperty(node, name, buf, len);
  230. }
  231. return buf;
  232. }
  233. static struct device_node * __init create_node(phandle node)
  234. {
  235. struct device_node *dp;
  236. if (!node)
  237. return NULL;
  238. dp = prom_early_alloc(sizeof(*dp));
  239. dp->unique_id = prom_unique_id++;
  240. kref_init(&dp->kref);
  241. dp->name = get_one_property(node, "name");
  242. dp->type = get_one_property(node, "device_type");
  243. dp->node = node;
  244. /* Build interrupts later... */
  245. dp->properties = build_prop_list(node);
  246. return dp;
  247. }
  248. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  249. {
  250. struct device_node *dp;
  251. dp = create_node(node);
  252. if (dp) {
  253. *(*nextp) = dp;
  254. *nextp = &dp->allnext;
  255. dp->parent = parent;
  256. dp->path_component_name = build_path_component(dp);
  257. dp->full_name = build_full_name(dp);
  258. dp->child = build_tree(dp, prom_getchild(node), nextp);
  259. dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
  260. }
  261. return dp;
  262. }
  263. struct device_node *of_console_device;
  264. EXPORT_SYMBOL(of_console_device);
  265. char *of_console_path;
  266. EXPORT_SYMBOL(of_console_path);
  267. char *of_console_options;
  268. EXPORT_SYMBOL(of_console_options);
  269. extern void restore_current(void);
  270. static void __init of_console_init(void)
  271. {
  272. char *msg = "OF stdout device is: %s\n";
  273. struct device_node *dp;
  274. unsigned long flags;
  275. const char *type;
  276. phandle node;
  277. int skip, tmp, fd;
  278. of_console_path = prom_early_alloc(256);
  279. switch (prom_vers) {
  280. case PROM_V0:
  281. skip = 0;
  282. switch (*romvec->pv_stdout) {
  283. case PROMDEV_SCREEN:
  284. type = "display";
  285. break;
  286. case PROMDEV_TTYB:
  287. skip = 1;
  288. /* FALLTHRU */
  289. case PROMDEV_TTYA:
  290. type = "serial";
  291. break;
  292. default:
  293. prom_printf("Invalid PROM_V0 stdout value %u\n",
  294. *romvec->pv_stdout);
  295. prom_halt();
  296. }
  297. tmp = skip;
  298. for_each_node_by_type(dp, type) {
  299. if (!tmp--)
  300. break;
  301. }
  302. if (!dp) {
  303. prom_printf("Cannot find PROM_V0 console node.\n");
  304. prom_halt();
  305. }
  306. of_console_device = dp;
  307. strcpy(of_console_path, dp->full_name);
  308. if (!strcmp(type, "serial")) {
  309. strcat(of_console_path,
  310. (skip ? ":b" : ":a"));
  311. }
  312. break;
  313. default:
  314. case PROM_V2:
  315. case PROM_V3:
  316. fd = *romvec->pv_v2bootargs.fd_stdout;
  317. spin_lock_irqsave(&prom_lock, flags);
  318. node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
  319. restore_current();
  320. spin_unlock_irqrestore(&prom_lock, flags);
  321. if (!node) {
  322. prom_printf("Cannot resolve stdout node from "
  323. "instance %08x.\n", fd);
  324. prom_halt();
  325. }
  326. dp = of_find_node_by_phandle(node);
  327. type = of_get_property(dp, "device_type", NULL);
  328. if (!type) {
  329. prom_printf("Console stdout lacks "
  330. "device_type property.\n");
  331. prom_halt();
  332. }
  333. if (strcmp(type, "display") && strcmp(type, "serial")) {
  334. prom_printf("Console device_type is neither display "
  335. "nor serial.\n");
  336. prom_halt();
  337. }
  338. of_console_device = dp;
  339. if (prom_vers == PROM_V2) {
  340. strcpy(of_console_path, dp->full_name);
  341. switch (*romvec->pv_stdout) {
  342. case PROMDEV_TTYA:
  343. strcat(of_console_path, ":a");
  344. break;
  345. case PROMDEV_TTYB:
  346. strcat(of_console_path, ":b");
  347. break;
  348. }
  349. } else {
  350. const char *path;
  351. dp = of_find_node_by_path("/");
  352. path = of_get_property(dp, "stdout-path", NULL);
  353. if (!path) {
  354. prom_printf("No stdout-path in root node.\n");
  355. prom_halt();
  356. }
  357. strcpy(of_console_path, path);
  358. }
  359. break;
  360. }
  361. of_console_options = strrchr(of_console_path, ':');
  362. if (of_console_options) {
  363. of_console_options++;
  364. if (*of_console_options == '\0')
  365. of_console_options = NULL;
  366. }
  367. prom_printf(msg, of_console_path);
  368. printk(msg, of_console_path);
  369. }
  370. void __init prom_build_devicetree(void)
  371. {
  372. struct device_node **nextp;
  373. allnodes = create_node(prom_root_node);
  374. allnodes->path_component_name = "";
  375. allnodes->full_name = "/";
  376. nextp = &allnodes->allnext;
  377. allnodes->child = build_tree(allnodes,
  378. prom_getchild(allnodes->node),
  379. &nextp);
  380. of_console_init();
  381. printk("PROM: Built device tree with %u bytes of memory.\n",
  382. prom_early_allocated);
  383. }