prom_32.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. struct device_node *of_find_node_by_phandle(phandle handle)
  27. {
  28. struct device_node *np;
  29. for (np = allnodes; np != 0; np = np->allnext)
  30. if (np->node == handle)
  31. break;
  32. return np;
  33. }
  34. EXPORT_SYMBOL(of_find_node_by_phandle);
  35. int of_getintprop_default(struct device_node *np, const char *name, int def)
  36. {
  37. struct property *prop;
  38. int len;
  39. prop = of_find_property(np, name, &len);
  40. if (!prop || len != 4)
  41. return def;
  42. return *(int *) prop->value;
  43. }
  44. EXPORT_SYMBOL(of_getintprop_default);
  45. DEFINE_MUTEX(of_set_property_mutex);
  46. EXPORT_SYMBOL(of_set_property_mutex);
  47. int of_set_property(struct device_node *dp, const char *name, void *val, int len)
  48. {
  49. struct property **prevp;
  50. void *new_val;
  51. int err;
  52. new_val = kmalloc(len, GFP_KERNEL);
  53. if (!new_val)
  54. return -ENOMEM;
  55. memcpy(new_val, val, len);
  56. err = -ENODEV;
  57. write_lock(&devtree_lock);
  58. prevp = &dp->properties;
  59. while (*prevp) {
  60. struct property *prop = *prevp;
  61. if (!strcasecmp(prop->name, name)) {
  62. void *old_val = prop->value;
  63. int ret;
  64. mutex_lock(&of_set_property_mutex);
  65. ret = prom_setprop(dp->node, (char *) name, val, len);
  66. mutex_unlock(&of_set_property_mutex);
  67. err = -EINVAL;
  68. if (ret >= 0) {
  69. prop->value = new_val;
  70. prop->length = len;
  71. if (OF_IS_DYNAMIC(prop))
  72. kfree(old_val);
  73. OF_MARK_DYNAMIC(prop);
  74. err = 0;
  75. }
  76. break;
  77. }
  78. prevp = &(*prevp)->next;
  79. }
  80. write_unlock(&devtree_lock);
  81. /* XXX Upate procfs if necessary... */
  82. return err;
  83. }
  84. EXPORT_SYMBOL(of_set_property);
  85. int of_find_in_proplist(const char *list, const char *match, int len)
  86. {
  87. while (len > 0) {
  88. int l;
  89. if (!strcmp(list, match))
  90. return 1;
  91. l = strlen(list) + 1;
  92. list += l;
  93. len -= l;
  94. }
  95. return 0;
  96. }
  97. EXPORT_SYMBOL(of_find_in_proplist);
  98. static unsigned int prom_early_allocated;
  99. static void * __init prom_early_alloc(unsigned long size)
  100. {
  101. void *ret;
  102. ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
  103. if (ret != NULL)
  104. memset(ret, 0, size);
  105. prom_early_allocated += size;
  106. return ret;
  107. }
  108. static int is_root_node(const struct device_node *dp)
  109. {
  110. if (!dp)
  111. return 0;
  112. return (dp->parent == NULL);
  113. }
  114. /* The following routines deal with the black magic of fully naming a
  115. * node.
  116. *
  117. * Certain well known named nodes are just the simple name string.
  118. *
  119. * Actual devices have an address specifier appended to the base name
  120. * string, like this "foo@addr". The "addr" can be in any number of
  121. * formats, and the platform plus the type of the node determine the
  122. * format and how it is constructed.
  123. *
  124. * For children of the ROOT node, the naming convention is fixed and
  125. * determined by whether this is a sun4u or sun4v system.
  126. *
  127. * For children of other nodes, it is bus type specific. So
  128. * we walk up the tree until we discover a "device_type" property
  129. * we recognize and we go from there.
  130. */
  131. static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
  132. {
  133. struct linux_prom_registers *regs;
  134. struct property *rprop;
  135. rprop = of_find_property(dp, "reg", NULL);
  136. if (!rprop)
  137. return;
  138. regs = rprop->value;
  139. sprintf(tmp_buf, "%s@%x,%x",
  140. dp->name,
  141. regs->which_io, regs->phys_addr);
  142. }
  143. /* "name@slot,offset" */
  144. static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
  145. {
  146. struct linux_prom_registers *regs;
  147. struct property *prop;
  148. prop = of_find_property(dp, "reg", NULL);
  149. if (!prop)
  150. return;
  151. regs = prop->value;
  152. sprintf(tmp_buf, "%s@%x,%x",
  153. dp->name,
  154. regs->which_io,
  155. regs->phys_addr);
  156. }
  157. /* "name@devnum[,func]" */
  158. static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
  159. {
  160. struct linux_prom_pci_registers *regs;
  161. struct property *prop;
  162. unsigned int devfn;
  163. prop = of_find_property(dp, "reg", NULL);
  164. if (!prop)
  165. return;
  166. regs = prop->value;
  167. devfn = (regs->phys_hi >> 8) & 0xff;
  168. if (devfn & 0x07) {
  169. sprintf(tmp_buf, "%s@%x,%x",
  170. dp->name,
  171. devfn >> 3,
  172. devfn & 0x07);
  173. } else {
  174. sprintf(tmp_buf, "%s@%x",
  175. dp->name,
  176. devfn >> 3);
  177. }
  178. }
  179. /* "name@addrhi,addrlo" */
  180. static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
  181. {
  182. struct linux_prom_registers *regs;
  183. struct property *prop;
  184. prop = of_find_property(dp, "reg", NULL);
  185. if (!prop)
  186. return;
  187. regs = prop->value;
  188. sprintf(tmp_buf, "%s@%x,%x",
  189. dp->name,
  190. regs->which_io, regs->phys_addr);
  191. }
  192. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  193. {
  194. struct device_node *parent = dp->parent;
  195. if (parent != NULL) {
  196. if (!strcmp(parent->type, "pci") ||
  197. !strcmp(parent->type, "pciex"))
  198. return pci_path_component(dp, tmp_buf);
  199. if (!strcmp(parent->type, "sbus"))
  200. return sbus_path_component(dp, tmp_buf);
  201. if (!strcmp(parent->type, "ebus"))
  202. return ebus_path_component(dp, tmp_buf);
  203. /* "isa" is handled with platform naming */
  204. }
  205. /* Use platform naming convention. */
  206. return sparc32_path_component(dp, tmp_buf);
  207. }
  208. static char * __init build_path_component(struct device_node *dp)
  209. {
  210. char tmp_buf[64], *n;
  211. tmp_buf[0] = '\0';
  212. __build_path_component(dp, tmp_buf);
  213. if (tmp_buf[0] == '\0')
  214. strcpy(tmp_buf, dp->name);
  215. n = prom_early_alloc(strlen(tmp_buf) + 1);
  216. strcpy(n, tmp_buf);
  217. return n;
  218. }
  219. static char * __init build_full_name(struct device_node *dp)
  220. {
  221. int len, ourlen, plen;
  222. char *n;
  223. plen = strlen(dp->parent->full_name);
  224. ourlen = strlen(dp->path_component_name);
  225. len = ourlen + plen + 2;
  226. n = prom_early_alloc(len);
  227. strcpy(n, dp->parent->full_name);
  228. if (!is_root_node(dp->parent)) {
  229. strcpy(n + plen, "/");
  230. plen++;
  231. }
  232. strcpy(n + plen, dp->path_component_name);
  233. return n;
  234. }
  235. static unsigned int unique_id;
  236. static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
  237. {
  238. static struct property *tmp = NULL;
  239. struct property *p;
  240. int len;
  241. const char *name;
  242. if (tmp) {
  243. p = tmp;
  244. memset(p, 0, sizeof(*p) + 32);
  245. tmp = NULL;
  246. } else {
  247. p = prom_early_alloc(sizeof(struct property) + 32);
  248. p->unique_id = unique_id++;
  249. }
  250. p->name = (char *) (p + 1);
  251. if (special_name) {
  252. strcpy(p->name, special_name);
  253. p->length = special_len;
  254. p->value = prom_early_alloc(special_len);
  255. memcpy(p->value, special_val, special_len);
  256. } else {
  257. if (prev == NULL) {
  258. name = prom_firstprop(node, NULL);
  259. } else {
  260. name = prom_nextprop(node, prev, NULL);
  261. }
  262. if (strlen(name) == 0) {
  263. tmp = p;
  264. return NULL;
  265. }
  266. strcpy(p->name, name);
  267. p->length = prom_getproplen(node, p->name);
  268. if (p->length <= 0) {
  269. p->length = 0;
  270. } else {
  271. p->value = prom_early_alloc(p->length + 1);
  272. len = prom_getproperty(node, p->name, p->value,
  273. p->length);
  274. if (len <= 0)
  275. p->length = 0;
  276. ((unsigned char *)p->value)[p->length] = '\0';
  277. }
  278. }
  279. return p;
  280. }
  281. static struct property * __init build_prop_list(phandle node)
  282. {
  283. struct property *head, *tail;
  284. head = tail = build_one_prop(node, NULL,
  285. ".node", &node, sizeof(node));
  286. tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
  287. tail = tail->next;
  288. while(tail) {
  289. tail->next = build_one_prop(node, tail->name,
  290. NULL, NULL, 0);
  291. tail = tail->next;
  292. }
  293. return head;
  294. }
  295. static char * __init get_one_property(phandle node, char *name)
  296. {
  297. char *buf = "<NULL>";
  298. int len;
  299. len = prom_getproplen(node, name);
  300. if (len > 0) {
  301. buf = prom_early_alloc(len);
  302. len = prom_getproperty(node, name, buf, len);
  303. }
  304. return buf;
  305. }
  306. static struct device_node * __init create_node(phandle node)
  307. {
  308. struct device_node *dp;
  309. if (!node)
  310. return NULL;
  311. dp = prom_early_alloc(sizeof(*dp));
  312. dp->unique_id = unique_id++;
  313. kref_init(&dp->kref);
  314. dp->name = get_one_property(node, "name");
  315. dp->type = get_one_property(node, "device_type");
  316. dp->node = node;
  317. /* Build interrupts later... */
  318. dp->properties = build_prop_list(node);
  319. return dp;
  320. }
  321. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  322. {
  323. struct device_node *dp;
  324. dp = create_node(node);
  325. if (dp) {
  326. *(*nextp) = dp;
  327. *nextp = &dp->allnext;
  328. dp->parent = parent;
  329. dp->path_component_name = build_path_component(dp);
  330. dp->full_name = build_full_name(dp);
  331. dp->child = build_tree(dp, prom_getchild(node), nextp);
  332. dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
  333. }
  334. return dp;
  335. }
  336. struct device_node *of_console_device;
  337. EXPORT_SYMBOL(of_console_device);
  338. char *of_console_path;
  339. EXPORT_SYMBOL(of_console_path);
  340. char *of_console_options;
  341. EXPORT_SYMBOL(of_console_options);
  342. extern void restore_current(void);
  343. static void __init of_console_init(void)
  344. {
  345. char *msg = "OF stdout device is: %s\n";
  346. struct device_node *dp;
  347. unsigned long flags;
  348. const char *type;
  349. phandle node;
  350. int skip, tmp, fd;
  351. of_console_path = prom_early_alloc(256);
  352. switch (prom_vers) {
  353. case PROM_V0:
  354. skip = 0;
  355. switch (*romvec->pv_stdout) {
  356. case PROMDEV_SCREEN:
  357. type = "display";
  358. break;
  359. case PROMDEV_TTYB:
  360. skip = 1;
  361. /* FALLTHRU */
  362. case PROMDEV_TTYA:
  363. type = "serial";
  364. break;
  365. default:
  366. prom_printf("Invalid PROM_V0 stdout value %u\n",
  367. *romvec->pv_stdout);
  368. prom_halt();
  369. }
  370. tmp = skip;
  371. for_each_node_by_type(dp, type) {
  372. if (!tmp--)
  373. break;
  374. }
  375. if (!dp) {
  376. prom_printf("Cannot find PROM_V0 console node.\n");
  377. prom_halt();
  378. }
  379. of_console_device = dp;
  380. strcpy(of_console_path, dp->full_name);
  381. if (!strcmp(type, "serial")) {
  382. strcat(of_console_path,
  383. (skip ? ":b" : ":a"));
  384. }
  385. break;
  386. default:
  387. case PROM_V2:
  388. case PROM_V3:
  389. fd = *romvec->pv_v2bootargs.fd_stdout;
  390. spin_lock_irqsave(&prom_lock, flags);
  391. node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
  392. restore_current();
  393. spin_unlock_irqrestore(&prom_lock, flags);
  394. if (!node) {
  395. prom_printf("Cannot resolve stdout node from "
  396. "instance %08x.\n", fd);
  397. prom_halt();
  398. }
  399. dp = of_find_node_by_phandle(node);
  400. type = of_get_property(dp, "device_type", NULL);
  401. if (!type) {
  402. prom_printf("Console stdout lacks "
  403. "device_type property.\n");
  404. prom_halt();
  405. }
  406. if (strcmp(type, "display") && strcmp(type, "serial")) {
  407. prom_printf("Console device_type is neither display "
  408. "nor serial.\n");
  409. prom_halt();
  410. }
  411. of_console_device = dp;
  412. if (prom_vers == PROM_V2) {
  413. strcpy(of_console_path, dp->full_name);
  414. switch (*romvec->pv_stdout) {
  415. case PROMDEV_TTYA:
  416. strcat(of_console_path, ":a");
  417. break;
  418. case PROMDEV_TTYB:
  419. strcat(of_console_path, ":b");
  420. break;
  421. }
  422. } else {
  423. const char *path;
  424. dp = of_find_node_by_path("/");
  425. path = of_get_property(dp, "stdout-path", NULL);
  426. if (!path) {
  427. prom_printf("No stdout-path in root node.\n");
  428. prom_halt();
  429. }
  430. strcpy(of_console_path, path);
  431. }
  432. break;
  433. }
  434. of_console_options = strrchr(of_console_path, ':');
  435. if (of_console_options) {
  436. of_console_options++;
  437. if (*of_console_options == '\0')
  438. of_console_options = NULL;
  439. }
  440. prom_printf(msg, of_console_path);
  441. printk(msg, of_console_path);
  442. }
  443. void __init prom_build_devicetree(void)
  444. {
  445. struct device_node **nextp;
  446. allnodes = create_node(prom_root_node);
  447. allnodes->path_component_name = "";
  448. allnodes->full_name = "/";
  449. nextp = &allnodes->allnext;
  450. allnodes->child = build_tree(allnodes,
  451. prom_getchild(allnodes->node),
  452. &nextp);
  453. of_console_init();
  454. printk("PROM: Built device tree with %u bytes of memory.\n",
  455. prom_early_allocated);
  456. }