prom_32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. static 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 unsigned int unique_id;
  164. static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
  165. {
  166. static struct property *tmp = NULL;
  167. struct property *p;
  168. int len;
  169. const char *name;
  170. if (tmp) {
  171. p = tmp;
  172. memset(p, 0, sizeof(*p) + 32);
  173. tmp = NULL;
  174. } else {
  175. p = prom_early_alloc(sizeof(struct property) + 32);
  176. p->unique_id = unique_id++;
  177. }
  178. p->name = (char *) (p + 1);
  179. if (special_name) {
  180. strcpy(p->name, special_name);
  181. p->length = special_len;
  182. p->value = prom_early_alloc(special_len);
  183. memcpy(p->value, special_val, special_len);
  184. } else {
  185. if (prev == NULL) {
  186. name = prom_firstprop(node, NULL);
  187. } else {
  188. name = prom_nextprop(node, prev, NULL);
  189. }
  190. if (strlen(name) == 0) {
  191. tmp = p;
  192. return NULL;
  193. }
  194. strcpy(p->name, name);
  195. p->length = prom_getproplen(node, p->name);
  196. if (p->length <= 0) {
  197. p->length = 0;
  198. } else {
  199. p->value = prom_early_alloc(p->length + 1);
  200. len = prom_getproperty(node, p->name, p->value,
  201. p->length);
  202. if (len <= 0)
  203. p->length = 0;
  204. ((unsigned char *)p->value)[p->length] = '\0';
  205. }
  206. }
  207. return p;
  208. }
  209. static struct property * __init build_prop_list(phandle node)
  210. {
  211. struct property *head, *tail;
  212. head = tail = build_one_prop(node, NULL,
  213. ".node", &node, sizeof(node));
  214. tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
  215. tail = tail->next;
  216. while(tail) {
  217. tail->next = build_one_prop(node, tail->name,
  218. NULL, NULL, 0);
  219. tail = tail->next;
  220. }
  221. return head;
  222. }
  223. static char * __init get_one_property(phandle node, char *name)
  224. {
  225. char *buf = "<NULL>";
  226. int len;
  227. len = prom_getproplen(node, name);
  228. if (len > 0) {
  229. buf = prom_early_alloc(len);
  230. len = prom_getproperty(node, name, buf, len);
  231. }
  232. return buf;
  233. }
  234. static struct device_node * __init create_node(phandle node)
  235. {
  236. struct device_node *dp;
  237. if (!node)
  238. return NULL;
  239. dp = prom_early_alloc(sizeof(*dp));
  240. dp->unique_id = unique_id++;
  241. kref_init(&dp->kref);
  242. dp->name = get_one_property(node, "name");
  243. dp->type = get_one_property(node, "device_type");
  244. dp->node = node;
  245. /* Build interrupts later... */
  246. dp->properties = build_prop_list(node);
  247. return dp;
  248. }
  249. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  250. {
  251. struct device_node *dp;
  252. dp = create_node(node);
  253. if (dp) {
  254. *(*nextp) = dp;
  255. *nextp = &dp->allnext;
  256. dp->parent = parent;
  257. dp->path_component_name = build_path_component(dp);
  258. dp->full_name = build_full_name(dp);
  259. dp->child = build_tree(dp, prom_getchild(node), nextp);
  260. dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
  261. }
  262. return dp;
  263. }
  264. struct device_node *of_console_device;
  265. EXPORT_SYMBOL(of_console_device);
  266. char *of_console_path;
  267. EXPORT_SYMBOL(of_console_path);
  268. char *of_console_options;
  269. EXPORT_SYMBOL(of_console_options);
  270. extern void restore_current(void);
  271. static void __init of_console_init(void)
  272. {
  273. char *msg = "OF stdout device is: %s\n";
  274. struct device_node *dp;
  275. unsigned long flags;
  276. const char *type;
  277. phandle node;
  278. int skip, tmp, fd;
  279. of_console_path = prom_early_alloc(256);
  280. switch (prom_vers) {
  281. case PROM_V0:
  282. skip = 0;
  283. switch (*romvec->pv_stdout) {
  284. case PROMDEV_SCREEN:
  285. type = "display";
  286. break;
  287. case PROMDEV_TTYB:
  288. skip = 1;
  289. /* FALLTHRU */
  290. case PROMDEV_TTYA:
  291. type = "serial";
  292. break;
  293. default:
  294. prom_printf("Invalid PROM_V0 stdout value %u\n",
  295. *romvec->pv_stdout);
  296. prom_halt();
  297. }
  298. tmp = skip;
  299. for_each_node_by_type(dp, type) {
  300. if (!tmp--)
  301. break;
  302. }
  303. if (!dp) {
  304. prom_printf("Cannot find PROM_V0 console node.\n");
  305. prom_halt();
  306. }
  307. of_console_device = dp;
  308. strcpy(of_console_path, dp->full_name);
  309. if (!strcmp(type, "serial")) {
  310. strcat(of_console_path,
  311. (skip ? ":b" : ":a"));
  312. }
  313. break;
  314. default:
  315. case PROM_V2:
  316. case PROM_V3:
  317. fd = *romvec->pv_v2bootargs.fd_stdout;
  318. spin_lock_irqsave(&prom_lock, flags);
  319. node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
  320. restore_current();
  321. spin_unlock_irqrestore(&prom_lock, flags);
  322. if (!node) {
  323. prom_printf("Cannot resolve stdout node from "
  324. "instance %08x.\n", fd);
  325. prom_halt();
  326. }
  327. dp = of_find_node_by_phandle(node);
  328. type = of_get_property(dp, "device_type", NULL);
  329. if (!type) {
  330. prom_printf("Console stdout lacks "
  331. "device_type property.\n");
  332. prom_halt();
  333. }
  334. if (strcmp(type, "display") && strcmp(type, "serial")) {
  335. prom_printf("Console device_type is neither display "
  336. "nor serial.\n");
  337. prom_halt();
  338. }
  339. of_console_device = dp;
  340. if (prom_vers == PROM_V2) {
  341. strcpy(of_console_path, dp->full_name);
  342. switch (*romvec->pv_stdout) {
  343. case PROMDEV_TTYA:
  344. strcat(of_console_path, ":a");
  345. break;
  346. case PROMDEV_TTYB:
  347. strcat(of_console_path, ":b");
  348. break;
  349. }
  350. } else {
  351. const char *path;
  352. dp = of_find_node_by_path("/");
  353. path = of_get_property(dp, "stdout-path", NULL);
  354. if (!path) {
  355. prom_printf("No stdout-path in root node.\n");
  356. prom_halt();
  357. }
  358. strcpy(of_console_path, path);
  359. }
  360. break;
  361. }
  362. of_console_options = strrchr(of_console_path, ':');
  363. if (of_console_options) {
  364. of_console_options++;
  365. if (*of_console_options == '\0')
  366. of_console_options = NULL;
  367. }
  368. prom_printf(msg, of_console_path);
  369. printk(msg, of_console_path);
  370. }
  371. void __init prom_build_devicetree(void)
  372. {
  373. struct device_node **nextp;
  374. allnodes = create_node(prom_root_node);
  375. allnodes->path_component_name = "";
  376. allnodes->full_name = "/";
  377. nextp = &allnodes->allnext;
  378. allnodes->child = build_tree(allnodes,
  379. prom_getchild(allnodes->node),
  380. &nextp);
  381. of_console_init();
  382. printk("PROM: Built device tree with %u bytes of memory.\n",
  383. prom_early_allocated);
  384. }