prom.c 12 KB

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