fdt_support.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. * (C) Copyright 2007
  3. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <linux/ctype.h>
  25. #include <linux/types.h>
  26. #include <asm/global_data.h>
  27. #include <fdt.h>
  28. #include <libfdt.h>
  29. #include <fdt_support.h>
  30. #include <exports.h>
  31. /*
  32. * Global data (for the gd->bd)
  33. */
  34. DECLARE_GLOBAL_DATA_PTR;
  35. /*
  36. * fdt points to our working device tree.
  37. */
  38. struct fdt_header *fdt;
  39. /********************************************************************/
  40. /**
  41. * fdt_find_and_setprop: Find a node and set it's property
  42. *
  43. * @fdt: ptr to device tree
  44. * @node: path of node
  45. * @prop: property name
  46. * @val: ptr to new value
  47. * @len: length of new property value
  48. * @create: flag to create the property if it doesn't exist
  49. *
  50. * Convenience function to directly set a property given the path to the node.
  51. */
  52. int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
  53. const void *val, int len, int create)
  54. {
  55. int nodeoff = fdt_path_offset(fdt, node);
  56. if (nodeoff < 0)
  57. return nodeoff;
  58. if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL))
  59. return 0; /* create flag not set; so exit quietly */
  60. return fdt_setprop(fdt, nodeoff, prop, val, len);
  61. }
  62. #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
  63. static int fdt_fixup_stdout(void *fdt, int choosenoff)
  64. {
  65. int err = 0;
  66. #ifdef CONFIG_CONS_INDEX
  67. int node;
  68. char sername[9] = { 0 };
  69. const char *path;
  70. sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
  71. err = node = fdt_path_offset(fdt, "/aliases");
  72. if (node >= 0) {
  73. int len;
  74. path = fdt_getprop(fdt, node, sername, &len);
  75. if (path) {
  76. char *p = malloc(len);
  77. err = -FDT_ERR_NOSPACE;
  78. if (p) {
  79. memcpy(p, path, len);
  80. err = fdt_setprop(fdt, choosenoff,
  81. "linux,stdout-path", p, len);
  82. free(p);
  83. }
  84. } else {
  85. err = len;
  86. }
  87. }
  88. #endif
  89. if (err < 0)
  90. printf("WARNING: could not set linux,stdout-path %s.\n",
  91. fdt_strerror(err));
  92. return err;
  93. }
  94. #endif
  95. int fdt_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
  96. {
  97. int nodeoffset;
  98. int err;
  99. u32 tmp; /* used to set 32 bit integer properties */
  100. char *str; /* used to set string properties */
  101. const char *path;
  102. err = fdt_check_header(fdt);
  103. if (err < 0) {
  104. printf("fdt_chosen: %s\n", fdt_strerror(err));
  105. return err;
  106. }
  107. if (initrd_start && initrd_end) {
  108. uint64_t addr, size;
  109. int total = fdt_num_mem_rsv(fdt);
  110. int j;
  111. /*
  112. * Look for an existing entry and update it. If we don't find
  113. * the entry, we will j be the next available slot.
  114. */
  115. for (j = 0; j < total; j++) {
  116. err = fdt_get_mem_rsv(fdt, j, &addr, &size);
  117. if (addr == initrd_start) {
  118. fdt_del_mem_rsv(fdt, j);
  119. break;
  120. }
  121. }
  122. err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1);
  123. if (err < 0) {
  124. printf("fdt_chosen: %s\n", fdt_strerror(err));
  125. return err;
  126. }
  127. }
  128. /*
  129. * Find the "chosen" node.
  130. */
  131. nodeoffset = fdt_path_offset (fdt, "/chosen");
  132. /*
  133. * If there is no "chosen" node in the blob, create it.
  134. */
  135. if (nodeoffset < 0) {
  136. /*
  137. * Create a new node "/chosen" (offset 0 is root level)
  138. */
  139. nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
  140. if (nodeoffset < 0) {
  141. printf("WARNING: could not create /chosen %s.\n",
  142. fdt_strerror(nodeoffset));
  143. return nodeoffset;
  144. }
  145. }
  146. /*
  147. * Create /chosen properites that don't exist in the fdt.
  148. * If the property exists, update it only if the "force" parameter
  149. * is true.
  150. */
  151. str = getenv("bootargs");
  152. if (str != NULL) {
  153. path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
  154. if ((path == NULL) || force) {
  155. err = fdt_setprop(fdt, nodeoffset,
  156. "bootargs", str, strlen(str)+1);
  157. if (err < 0)
  158. printf("WARNING: could not set bootargs %s.\n",
  159. fdt_strerror(err));
  160. }
  161. }
  162. if (initrd_start && initrd_end) {
  163. path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
  164. if ((path == NULL) || force) {
  165. tmp = __cpu_to_be32(initrd_start);
  166. err = fdt_setprop(fdt, nodeoffset,
  167. "linux,initrd-start", &tmp, sizeof(tmp));
  168. if (err < 0)
  169. printf("WARNING: "
  170. "could not set linux,initrd-start %s.\n",
  171. fdt_strerror(err));
  172. tmp = __cpu_to_be32(initrd_end);
  173. err = fdt_setprop(fdt, nodeoffset,
  174. "linux,initrd-end", &tmp, sizeof(tmp));
  175. if (err < 0)
  176. printf("WARNING: could not set linux,initrd-end %s.\n",
  177. fdt_strerror(err));
  178. }
  179. }
  180. #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
  181. path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
  182. if ((path == NULL) || force)
  183. err = fdt_fixup_stdout(fdt, nodeoffset);
  184. #endif
  185. #ifdef OF_STDOUT_PATH
  186. path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
  187. if ((path == NULL) || force) {
  188. err = fdt_setprop(fdt, nodeoffset,
  189. "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
  190. if (err < 0)
  191. printf("WARNING: could not set linux,stdout-path %s.\n",
  192. fdt_strerror(err));
  193. }
  194. #endif
  195. return err;
  196. }
  197. /********************************************************************/
  198. #ifdef CONFIG_OF_HAS_UBOOT_ENV
  199. /* Function that returns a character from the environment */
  200. extern uchar(*env_get_char) (int);
  201. int fdt_env(void *fdt)
  202. {
  203. int nodeoffset;
  204. int err;
  205. int k, nxt;
  206. int i;
  207. static char tmpenv[256];
  208. err = fdt_check_header(fdt);
  209. if (err < 0) {
  210. printf("fdt_env: %s\n", fdt_strerror(err));
  211. return err;
  212. }
  213. /*
  214. * See if we already have a "u-boot-env" node, delete it if so.
  215. * Then create a new empty node.
  216. */
  217. nodeoffset = fdt_path_offset (fdt, "/u-boot-env");
  218. if (nodeoffset >= 0) {
  219. err = fdt_del_node(fdt, nodeoffset);
  220. if (err < 0) {
  221. printf("fdt_env: %s\n", fdt_strerror(err));
  222. return err;
  223. }
  224. }
  225. /*
  226. * Create a new node "/u-boot-env" (offset 0 is root level)
  227. */
  228. nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
  229. if (nodeoffset < 0) {
  230. printf("WARNING: could not create /u-boot-env %s.\n",
  231. fdt_strerror(nodeoffset));
  232. return nodeoffset;
  233. }
  234. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  235. char *s, *lval, *rval;
  236. /*
  237. * Find the end of the name=definition
  238. */
  239. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
  240. ;
  241. s = tmpenv;
  242. for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
  243. *s++ = env_get_char(k);
  244. *s++ = '\0';
  245. lval = tmpenv;
  246. /*
  247. * Find the first '=': it separates the name from the value
  248. */
  249. s = strchr(tmpenv, '=');
  250. if (s != NULL) {
  251. *s++ = '\0';
  252. rval = s;
  253. } else
  254. continue;
  255. err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
  256. if (err < 0) {
  257. printf("WARNING: could not set %s %s.\n",
  258. lval, fdt_strerror(err));
  259. return err;
  260. }
  261. }
  262. return 0;
  263. }
  264. #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
  265. /********************************************************************/
  266. #ifdef CONFIG_OF_HAS_BD_T
  267. #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
  268. static const struct {
  269. const char *name;
  270. int offset;
  271. } bd_map[] = {
  272. BDM(memstart),
  273. BDM(memsize),
  274. BDM(flashstart),
  275. BDM(flashsize),
  276. BDM(flashoffset),
  277. BDM(sramstart),
  278. BDM(sramsize),
  279. #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
  280. || defined(CONFIG_E500)
  281. BDM(immr_base),
  282. #endif
  283. #if defined(CONFIG_MPC5xxx)
  284. BDM(mbar_base),
  285. #endif
  286. #if defined(CONFIG_MPC83XX)
  287. BDM(immrbar),
  288. #endif
  289. #if defined(CONFIG_MPC8220)
  290. BDM(mbar_base),
  291. BDM(inpfreq),
  292. BDM(pcifreq),
  293. BDM(pevfreq),
  294. BDM(flbfreq),
  295. BDM(vcofreq),
  296. #endif
  297. BDM(bootflags),
  298. BDM(ip_addr),
  299. BDM(intfreq),
  300. BDM(busfreq),
  301. #ifdef CONFIG_CPM2
  302. BDM(cpmfreq),
  303. BDM(brgfreq),
  304. BDM(sccfreq),
  305. BDM(vco),
  306. #endif
  307. #if defined(CONFIG_MPC5xxx)
  308. BDM(ipbfreq),
  309. BDM(pcifreq),
  310. #endif
  311. BDM(baudrate),
  312. };
  313. int fdt_bd_t(void *fdt)
  314. {
  315. bd_t *bd = gd->bd;
  316. int nodeoffset;
  317. int err;
  318. u32 tmp; /* used to set 32 bit integer properties */
  319. int i;
  320. err = fdt_check_header(fdt);
  321. if (err < 0) {
  322. printf("fdt_bd_t: %s\n", fdt_strerror(err));
  323. return err;
  324. }
  325. /*
  326. * See if we already have a "bd_t" node, delete it if so.
  327. * Then create a new empty node.
  328. */
  329. nodeoffset = fdt_path_offset (fdt, "/bd_t");
  330. if (nodeoffset >= 0) {
  331. err = fdt_del_node(fdt, nodeoffset);
  332. if (err < 0) {
  333. printf("fdt_bd_t: %s\n", fdt_strerror(err));
  334. return err;
  335. }
  336. }
  337. /*
  338. * Create a new node "/bd_t" (offset 0 is root level)
  339. */
  340. nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
  341. if (nodeoffset < 0) {
  342. printf("WARNING: could not create /bd_t %s.\n",
  343. fdt_strerror(nodeoffset));
  344. printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset));
  345. return nodeoffset;
  346. }
  347. /*
  348. * Use the string/pointer structure to create the entries...
  349. */
  350. for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
  351. tmp = cpu_to_be32(getenv("bootargs"));
  352. err = fdt_setprop(fdt, nodeoffset,
  353. bd_map[i].name, &tmp, sizeof(tmp));
  354. if (err < 0)
  355. printf("WARNING: could not set %s %s.\n",
  356. bd_map[i].name, fdt_strerror(err));
  357. }
  358. /*
  359. * Add a couple of oddball entries...
  360. */
  361. err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
  362. if (err < 0)
  363. printf("WARNING: could not set enetaddr %s.\n",
  364. fdt_strerror(err));
  365. err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
  366. if (err < 0)
  367. printf("WARNING: could not set ethspeed %s.\n",
  368. fdt_strerror(err));
  369. return 0;
  370. }
  371. #endif /* ifdef CONFIG_OF_HAS_BD_T */
  372. void do_fixup_by_path(void *fdt, const char *path, const char *prop,
  373. const void *val, int len, int create)
  374. {
  375. #if defined(DEBUG)
  376. int i;
  377. debug("Updating property '%s/%s' = ", path, prop);
  378. for (i = 0; i < len; i++)
  379. debug(" %.2x", *(u8*)(val+i));
  380. debug("\n");
  381. #endif
  382. int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
  383. if (rc)
  384. printf("Unable to update property %s:%s, err=%s\n",
  385. path, prop, fdt_strerror(rc));
  386. }
  387. void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
  388. u32 val, int create)
  389. {
  390. val = cpu_to_fdt32(val);
  391. do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
  392. }
  393. void do_fixup_by_prop(void *fdt,
  394. const char *pname, const void *pval, int plen,
  395. const char *prop, const void *val, int len,
  396. int create)
  397. {
  398. int off;
  399. #if defined(DEBUG)
  400. int i;
  401. debug("Updating property '%s' = ", prop);
  402. for (i = 0; i < len; i++)
  403. debug(" %.2x", *(u8*)(val+i));
  404. debug("\n");
  405. #endif
  406. off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
  407. while (off != -FDT_ERR_NOTFOUND) {
  408. if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
  409. fdt_setprop(fdt, off, prop, val, len);
  410. off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
  411. }
  412. }
  413. void do_fixup_by_prop_u32(void *fdt,
  414. const char *pname, const void *pval, int plen,
  415. const char *prop, u32 val, int create)
  416. {
  417. val = cpu_to_fdt32(val);
  418. do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
  419. }
  420. void do_fixup_by_compat(void *fdt, const char *compat,
  421. const char *prop, const void *val, int len, int create)
  422. {
  423. int off = -1;
  424. #if defined(DEBUG)
  425. int i;
  426. debug("Updating property '%s' = ", prop);
  427. for (i = 0; i < len; i++)
  428. debug(" %.2x", *(u8*)(val+i));
  429. debug("\n");
  430. #endif
  431. off = fdt_node_offset_by_compatible(fdt, -1, compat);
  432. while (off != -FDT_ERR_NOTFOUND) {
  433. if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
  434. fdt_setprop(fdt, off, prop, val, len);
  435. off = fdt_node_offset_by_compatible(fdt, off, compat);
  436. }
  437. }
  438. void do_fixup_by_compat_u32(void *fdt, const char *compat,
  439. const char *prop, u32 val, int create)
  440. {
  441. val = cpu_to_fdt32(val);
  442. do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
  443. }
  444. int fdt_fixup_memory(void *blob, u64 start, u64 size)
  445. {
  446. int err, nodeoffset, len = 0;
  447. u8 tmp[16];
  448. const u32 *addrcell, *sizecell;
  449. err = fdt_check_header(blob);
  450. if (err < 0) {
  451. printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
  452. return err;
  453. }
  454. /* update, or add and update /memory node */
  455. nodeoffset = fdt_path_offset(blob, "/memory");
  456. if (nodeoffset < 0) {
  457. nodeoffset = fdt_add_subnode(blob, 0, "memory");
  458. if (nodeoffset < 0)
  459. printf("WARNING: could not create /memory: %s.\n",
  460. fdt_strerror(nodeoffset));
  461. return nodeoffset;
  462. }
  463. err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
  464. sizeof("memory"));
  465. if (err < 0) {
  466. printf("WARNING: could not set %s %s.\n", "device_type",
  467. fdt_strerror(err));
  468. return err;
  469. }
  470. addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
  471. /* use shifts and mask to ensure endianness */
  472. if ((addrcell) && (*addrcell == 2)) {
  473. tmp[0] = (start >> 56) & 0xff;
  474. tmp[1] = (start >> 48) & 0xff;
  475. tmp[2] = (start >> 40) & 0xff;
  476. tmp[3] = (start >> 32) & 0xff;
  477. tmp[4] = (start >> 24) & 0xff;
  478. tmp[5] = (start >> 16) & 0xff;
  479. tmp[6] = (start >> 8) & 0xff;
  480. tmp[7] = (start ) & 0xff;
  481. len = 8;
  482. } else {
  483. tmp[0] = (start >> 24) & 0xff;
  484. tmp[1] = (start >> 16) & 0xff;
  485. tmp[2] = (start >> 8) & 0xff;
  486. tmp[3] = (start ) & 0xff;
  487. len = 4;
  488. }
  489. sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
  490. /* use shifts and mask to ensure endianness */
  491. if ((sizecell) && (*sizecell == 2)) {
  492. tmp[0+len] = (size >> 56) & 0xff;
  493. tmp[1+len] = (size >> 48) & 0xff;
  494. tmp[2+len] = (size >> 40) & 0xff;
  495. tmp[3+len] = (size >> 32) & 0xff;
  496. tmp[4+len] = (size >> 24) & 0xff;
  497. tmp[5+len] = (size >> 16) & 0xff;
  498. tmp[6+len] = (size >> 8) & 0xff;
  499. tmp[7+len] = (size ) & 0xff;
  500. len += 8;
  501. } else {
  502. tmp[0+len] = (size >> 24) & 0xff;
  503. tmp[1+len] = (size >> 16) & 0xff;
  504. tmp[2+len] = (size >> 8) & 0xff;
  505. tmp[3+len] = (size ) & 0xff;
  506. len += 4;
  507. }
  508. err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
  509. if (err < 0) {
  510. printf("WARNING: could not set %s %s.\n",
  511. "reg", fdt_strerror(err));
  512. return err;
  513. }
  514. return 0;
  515. }
  516. #if defined(CONFIG_HAS_ETH0) || defined(CONFIG_HAS_ETH1) ||\
  517. defined(CONFIG_HAS_ETH2) || defined(CONFIG_HAS_ETH3)
  518. void fdt_fixup_ethernet(void *fdt, bd_t *bd)
  519. {
  520. int node;
  521. const char *path;
  522. node = fdt_path_offset(fdt, "/aliases");
  523. if (node >= 0) {
  524. #if defined(CONFIG_HAS_ETH0)
  525. path = fdt_getprop(fdt, node, "ethernet0", NULL);
  526. if (path) {
  527. do_fixup_by_path(fdt, path, "mac-address",
  528. bd->bi_enetaddr, 6, 0);
  529. do_fixup_by_path(fdt, path, "local-mac-address",
  530. bd->bi_enetaddr, 6, 1);
  531. }
  532. #endif
  533. #if defined(CONFIG_HAS_ETH1)
  534. path = fdt_getprop(fdt, node, "ethernet1", NULL);
  535. if (path) {
  536. do_fixup_by_path(fdt, path, "mac-address",
  537. bd->bi_enet1addr, 6, 0);
  538. do_fixup_by_path(fdt, path, "local-mac-address",
  539. bd->bi_enet1addr, 6, 1);
  540. }
  541. #endif
  542. #if defined(CONFIG_HAS_ETH2)
  543. path = fdt_getprop(fdt, node, "ethernet2", NULL);
  544. if (path) {
  545. do_fixup_by_path(fdt, path, "mac-address",
  546. bd->bi_enet2addr, 6, 0);
  547. do_fixup_by_path(fdt, path, "local-mac-address",
  548. bd->bi_enet2addr, 6, 1);
  549. }
  550. #endif
  551. #if defined(CONFIG_HAS_ETH3)
  552. path = fdt_getprop(fdt, node, "ethernet3", NULL);
  553. if (path) {
  554. do_fixup_by_path(fdt, path, "mac-address",
  555. bd->bi_enet3addr, 6, 0);
  556. do_fixup_by_path(fdt, path, "local-mac-address",
  557. bd->bi_enet3addr, 6, 1);
  558. }
  559. #endif
  560. }
  561. }
  562. #endif