fdt_support.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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. err = fdt_check_header(fdt);
  102. if (err < 0) {
  103. printf("fdt_chosen: %s\n", fdt_strerror(err));
  104. return err;
  105. }
  106. if (initrd_start && initrd_end) {
  107. uint64_t addr, size;
  108. int total = fdt_num_mem_rsv(fdt);
  109. int j;
  110. /*
  111. * Look for an existing entry and update it. If we don't find
  112. * the entry, we will j be the next available slot.
  113. */
  114. for (j = 0; j < total; j++) {
  115. err = fdt_get_mem_rsv(fdt, j, &addr, &size);
  116. if (addr == initrd_start) {
  117. fdt_del_mem_rsv(fdt, j);
  118. break;
  119. }
  120. }
  121. err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1);
  122. if (err < 0) {
  123. printf("fdt_chosen: %s\n", fdt_strerror(err));
  124. return err;
  125. }
  126. }
  127. /*
  128. * Find the "chosen" node.
  129. */
  130. nodeoffset = fdt_path_offset (fdt, "/chosen");
  131. /*
  132. * If we have a "chosen" node already the "force the writing"
  133. * is not set, our job is done.
  134. */
  135. if ((nodeoffset >= 0) && !force)
  136. return 0;
  137. /*
  138. * No "chosen" node in the blob: create it.
  139. */
  140. if (nodeoffset < 0) {
  141. /*
  142. * Create a new node "/chosen" (offset 0 is root level)
  143. */
  144. nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
  145. if (nodeoffset < 0) {
  146. printf("WARNING: could not create /chosen %s.\n",
  147. fdt_strerror(nodeoffset));
  148. return nodeoffset;
  149. }
  150. }
  151. /*
  152. * Update pre-existing properties, create them if non-existant.
  153. */
  154. str = getenv("bootargs");
  155. if (str != NULL) {
  156. err = fdt_setprop(fdt, nodeoffset,
  157. "bootargs", str, strlen(str)+1);
  158. if (err < 0)
  159. printf("WARNING: could not set bootargs %s.\n",
  160. fdt_strerror(err));
  161. }
  162. if (initrd_start && initrd_end) {
  163. tmp = __cpu_to_be32(initrd_start);
  164. err = fdt_setprop(fdt, nodeoffset,
  165. "linux,initrd-start", &tmp, sizeof(tmp));
  166. if (err < 0)
  167. printf("WARNING: "
  168. "could not set linux,initrd-start %s.\n",
  169. fdt_strerror(err));
  170. tmp = __cpu_to_be32(initrd_end);
  171. err = fdt_setprop(fdt, nodeoffset,
  172. "linux,initrd-end", &tmp, sizeof(tmp));
  173. if (err < 0)
  174. printf("WARNING: could not set linux,initrd-end %s.\n",
  175. fdt_strerror(err));
  176. }
  177. #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
  178. err = fdt_fixup_stdout(fdt, nodeoffset);
  179. #endif
  180. #ifdef OF_STDOUT_PATH
  181. err = fdt_setprop(fdt, nodeoffset,
  182. "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
  183. if (err < 0)
  184. printf("WARNING: could not set linux,stdout-path %s.\n",
  185. fdt_strerror(err));
  186. #endif
  187. return err;
  188. }
  189. /********************************************************************/
  190. #ifdef CONFIG_OF_HAS_UBOOT_ENV
  191. /* Function that returns a character from the environment */
  192. extern uchar(*env_get_char) (int);
  193. int fdt_env(void *fdt)
  194. {
  195. int nodeoffset;
  196. int err;
  197. int k, nxt;
  198. int i;
  199. static char tmpenv[256];
  200. err = fdt_check_header(fdt);
  201. if (err < 0) {
  202. printf("fdt_env: %s\n", fdt_strerror(err));
  203. return err;
  204. }
  205. /*
  206. * See if we already have a "u-boot-env" node, delete it if so.
  207. * Then create a new empty node.
  208. */
  209. nodeoffset = fdt_path_offset (fdt, "/u-boot-env");
  210. if (nodeoffset >= 0) {
  211. err = fdt_del_node(fdt, nodeoffset);
  212. if (err < 0) {
  213. printf("fdt_env: %s\n", fdt_strerror(err));
  214. return err;
  215. }
  216. }
  217. /*
  218. * Create a new node "/u-boot-env" (offset 0 is root level)
  219. */
  220. nodeoffset = fdt_add_subnode(fdt, 0, "u-boot-env");
  221. if (nodeoffset < 0) {
  222. printf("WARNING: could not create /u-boot-env %s.\n",
  223. fdt_strerror(nodeoffset));
  224. return nodeoffset;
  225. }
  226. for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
  227. char *s, *lval, *rval;
  228. /*
  229. * Find the end of the name=definition
  230. */
  231. for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
  232. ;
  233. s = tmpenv;
  234. for (k = i; k < nxt && s < &tmpenv[sizeof(tmpenv) - 1]; ++k)
  235. *s++ = env_get_char(k);
  236. *s++ = '\0';
  237. lval = tmpenv;
  238. /*
  239. * Find the first '=': it separates the name from the value
  240. */
  241. s = strchr(tmpenv, '=');
  242. if (s != NULL) {
  243. *s++ = '\0';
  244. rval = s;
  245. } else
  246. continue;
  247. err = fdt_setprop(fdt, nodeoffset, lval, rval, strlen(rval)+1);
  248. if (err < 0) {
  249. printf("WARNING: could not set %s %s.\n",
  250. lval, fdt_strerror(err));
  251. return err;
  252. }
  253. }
  254. return 0;
  255. }
  256. #endif /* ifdef CONFIG_OF_HAS_UBOOT_ENV */
  257. /********************************************************************/
  258. #ifdef CONFIG_OF_HAS_BD_T
  259. #define BDM(x) { .name = #x, .offset = offsetof(bd_t, bi_ ##x ) }
  260. static const struct {
  261. const char *name;
  262. int offset;
  263. } bd_map[] = {
  264. BDM(memstart),
  265. BDM(memsize),
  266. BDM(flashstart),
  267. BDM(flashsize),
  268. BDM(flashoffset),
  269. BDM(sramstart),
  270. BDM(sramsize),
  271. #if defined(CONFIG_5xx) || defined(CONFIG_8xx) || defined(CONFIG_8260) \
  272. || defined(CONFIG_E500)
  273. BDM(immr_base),
  274. #endif
  275. #if defined(CONFIG_MPC5xxx)
  276. BDM(mbar_base),
  277. #endif
  278. #if defined(CONFIG_MPC83XX)
  279. BDM(immrbar),
  280. #endif
  281. #if defined(CONFIG_MPC8220)
  282. BDM(mbar_base),
  283. BDM(inpfreq),
  284. BDM(pcifreq),
  285. BDM(pevfreq),
  286. BDM(flbfreq),
  287. BDM(vcofreq),
  288. #endif
  289. BDM(bootflags),
  290. BDM(ip_addr),
  291. BDM(intfreq),
  292. BDM(busfreq),
  293. #ifdef CONFIG_CPM2
  294. BDM(cpmfreq),
  295. BDM(brgfreq),
  296. BDM(sccfreq),
  297. BDM(vco),
  298. #endif
  299. #if defined(CONFIG_MPC5xxx)
  300. BDM(ipbfreq),
  301. BDM(pcifreq),
  302. #endif
  303. BDM(baudrate),
  304. };
  305. int fdt_bd_t(void *fdt)
  306. {
  307. bd_t *bd = gd->bd;
  308. int nodeoffset;
  309. int err;
  310. u32 tmp; /* used to set 32 bit integer properties */
  311. int i;
  312. err = fdt_check_header(fdt);
  313. if (err < 0) {
  314. printf("fdt_bd_t: %s\n", fdt_strerror(err));
  315. return err;
  316. }
  317. /*
  318. * See if we already have a "bd_t" node, delete it if so.
  319. * Then create a new empty node.
  320. */
  321. nodeoffset = fdt_path_offset (fdt, "/bd_t");
  322. if (nodeoffset >= 0) {
  323. err = fdt_del_node(fdt, nodeoffset);
  324. if (err < 0) {
  325. printf("fdt_bd_t: %s\n", fdt_strerror(err));
  326. return err;
  327. }
  328. }
  329. /*
  330. * Create a new node "/bd_t" (offset 0 is root level)
  331. */
  332. nodeoffset = fdt_add_subnode(fdt, 0, "bd_t");
  333. if (nodeoffset < 0) {
  334. printf("WARNING: could not create /bd_t %s.\n",
  335. fdt_strerror(nodeoffset));
  336. printf("fdt_bd_t: %s\n", fdt_strerror(nodeoffset));
  337. return nodeoffset;
  338. }
  339. /*
  340. * Use the string/pointer structure to create the entries...
  341. */
  342. for (i = 0; i < sizeof(bd_map)/sizeof(bd_map[0]); i++) {
  343. tmp = cpu_to_be32(getenv("bootargs"));
  344. err = fdt_setprop(fdt, nodeoffset,
  345. bd_map[i].name, &tmp, sizeof(tmp));
  346. if (err < 0)
  347. printf("WARNING: could not set %s %s.\n",
  348. bd_map[i].name, fdt_strerror(err));
  349. }
  350. /*
  351. * Add a couple of oddball entries...
  352. */
  353. err = fdt_setprop(fdt, nodeoffset, "enetaddr", &bd->bi_enetaddr, 6);
  354. if (err < 0)
  355. printf("WARNING: could not set enetaddr %s.\n",
  356. fdt_strerror(err));
  357. err = fdt_setprop(fdt, nodeoffset, "ethspeed", &bd->bi_ethspeed, 4);
  358. if (err < 0)
  359. printf("WARNING: could not set ethspeed %s.\n",
  360. fdt_strerror(err));
  361. return 0;
  362. }
  363. #endif /* ifdef CONFIG_OF_HAS_BD_T */
  364. void do_fixup_by_path(void *fdt, const char *path, const char *prop,
  365. const void *val, int len, int create)
  366. {
  367. #if defined(DEBUG)
  368. int i;
  369. debug("Updating property '%s/%s' = ", node, prop);
  370. for (i = 0; i < len; i++)
  371. debug(" %.2x", *(u8*)(val+i));
  372. debug("\n");
  373. #endif
  374. int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
  375. if (rc)
  376. printf("Unable to update property %s:%s, err=%s\n",
  377. path, prop, fdt_strerror(rc));
  378. }
  379. void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
  380. u32 val, int create)
  381. {
  382. val = cpu_to_fdt32(val);
  383. do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
  384. }
  385. void do_fixup_by_prop(void *fdt,
  386. const char *pname, const void *pval, int plen,
  387. const char *prop, const void *val, int len,
  388. int create)
  389. {
  390. int off;
  391. #if defined(DEBUG)
  392. int i;
  393. debug("Updating property '%s/%s' = ", node, prop);
  394. for (i = 0; i < len; i++)
  395. debug(" %.2x", *(u8*)(val+i));
  396. debug("\n");
  397. #endif
  398. off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
  399. while (off != -FDT_ERR_NOTFOUND) {
  400. if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
  401. fdt_setprop(fdt, off, prop, val, len);
  402. off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
  403. }
  404. }
  405. void do_fixup_by_prop_u32(void *fdt,
  406. const char *pname, const void *pval, int plen,
  407. const char *prop, u32 val, int create)
  408. {
  409. val = cpu_to_fdt32(val);
  410. do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
  411. }
  412. void do_fixup_by_compat(void *fdt, const char *compat,
  413. const char *prop, const void *val, int len, int create)
  414. {
  415. int off = -1;
  416. #if defined(DEBUG)
  417. int i;
  418. debug("Updating property '%s/%s' = ", node, prop);
  419. for (i = 0; i < len; i++)
  420. debug(" %.2x", *(u8*)(val+i));
  421. debug("\n");
  422. #endif
  423. off = fdt_node_offset_by_compatible(fdt, -1, compat);
  424. while (off != -FDT_ERR_NOTFOUND) {
  425. if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
  426. fdt_setprop(fdt, off, prop, val, len);
  427. off = fdt_node_offset_by_compatible(fdt, off, compat);
  428. }
  429. }
  430. void do_fixup_by_compat_u32(void *fdt, const char *compat,
  431. const char *prop, u32 val, int create)
  432. {
  433. val = cpu_to_fdt32(val);
  434. do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
  435. }
  436. int fdt_fixup_memory(void *blob, u64 start, u64 size)
  437. {
  438. int err, nodeoffset, len = 0;
  439. u8 tmp[16];
  440. const u32 *addrcell, *sizecell;
  441. err = fdt_check_header(blob);
  442. if (err < 0) {
  443. printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
  444. return err;
  445. }
  446. /* update, or add and update /memory node */
  447. nodeoffset = fdt_path_offset(blob, "/memory");
  448. if (nodeoffset < 0) {
  449. nodeoffset = fdt_add_subnode(blob, 0, "memory");
  450. if (nodeoffset < 0)
  451. printf("WARNING: could not create /memory: %s.\n",
  452. fdt_strerror(nodeoffset));
  453. return nodeoffset;
  454. }
  455. err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
  456. sizeof("memory"));
  457. if (err < 0) {
  458. printf("WARNING: could not set %s %s.\n", "device_type",
  459. fdt_strerror(err));
  460. return err;
  461. }
  462. addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
  463. /* use shifts and mask to ensure endianness */
  464. if ((addrcell) && (*addrcell == 2)) {
  465. tmp[0] = (start >> 56) & 0xff;
  466. tmp[1] = (start >> 48) & 0xff;
  467. tmp[2] = (start >> 40) & 0xff;
  468. tmp[3] = (start >> 32) & 0xff;
  469. tmp[4] = (start >> 24) & 0xff;
  470. tmp[5] = (start >> 16) & 0xff;
  471. tmp[6] = (start >> 8) & 0xff;
  472. tmp[7] = (start ) & 0xff;
  473. len = 8;
  474. } else {
  475. tmp[0] = (start >> 24) & 0xff;
  476. tmp[1] = (start >> 16) & 0xff;
  477. tmp[2] = (start >> 8) & 0xff;
  478. tmp[3] = (start ) & 0xff;
  479. len = 4;
  480. }
  481. sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
  482. /* use shifts and mask to ensure endianness */
  483. if ((sizecell) && (*sizecell == 2)) {
  484. tmp[0+len] = (size >> 56) & 0xff;
  485. tmp[1+len] = (size >> 48) & 0xff;
  486. tmp[2+len] = (size >> 40) & 0xff;
  487. tmp[3+len] = (size >> 32) & 0xff;
  488. tmp[4+len] = (size >> 24) & 0xff;
  489. tmp[5+len] = (size >> 16) & 0xff;
  490. tmp[6+len] = (size >> 8) & 0xff;
  491. tmp[7+len] = (size ) & 0xff;
  492. len += 8;
  493. } else {
  494. tmp[0+len] = (size >> 24) & 0xff;
  495. tmp[1+len] = (size >> 16) & 0xff;
  496. tmp[2+len] = (size >> 8) & 0xff;
  497. tmp[3+len] = (size ) & 0xff;
  498. len += 4;
  499. }
  500. err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
  501. if (err < 0) {
  502. printf("WARNING: could not set %s %s.\n",
  503. "reg", fdt_strerror(err));
  504. return err;
  505. }
  506. return 0;
  507. }
  508. #if defined(CONFIG_HAS_ETH0) || defined(CONFIG_HAS_ETH1) ||\
  509. defined(CONFIG_HAS_ETH2) || defined(CONFIG_HAS_ETH3)
  510. void fdt_fixup_ethernet(void *fdt, bd_t *bd)
  511. {
  512. int node;
  513. const char *path;
  514. node = fdt_path_offset(fdt, "/aliases");
  515. if (node >= 0) {
  516. #if defined(CONFIG_HAS_ETH0)
  517. path = fdt_getprop(fdt, node, "ethernet0", NULL);
  518. if (path) {
  519. do_fixup_by_path(fdt, path, "mac-address",
  520. bd->bi_enetaddr, 6, 0);
  521. do_fixup_by_path(fdt, path, "local-mac-address",
  522. bd->bi_enetaddr, 6, 1);
  523. }
  524. #endif
  525. #if defined(CONFIG_HAS_ETH1)
  526. path = fdt_getprop(fdt, node, "ethernet1", NULL);
  527. if (path) {
  528. do_fixup_by_path(fdt, path, "mac-address",
  529. bd->bi_enet1addr, 6, 0);
  530. do_fixup_by_path(fdt, path, "local-mac-address",
  531. bd->bi_enet1addr, 6, 1);
  532. }
  533. #endif
  534. #if defined(CONFIG_HAS_ETH2)
  535. path = fdt_getprop(fdt, node, "ethernet2", NULL);
  536. if (path) {
  537. do_fixup_by_path(fdt, path, "mac-address",
  538. bd->bi_enet2addr, 6, 0);
  539. do_fixup_by_path(fdt, path, "local-mac-address",
  540. bd->bi_enet2addr, 6, 1);
  541. }
  542. #endif
  543. #if defined(CONFIG_HAS_ETH3)
  544. path = fdt_getprop(fdt, node, "ethernet3", NULL);
  545. if (path) {
  546. do_fixup_by_path(fdt, path, "mac-address",
  547. bd->bi_enet3addr, 6, 0);
  548. do_fixup_by_path(fdt, path, "local-mac-address",
  549. bd->bi_enet3addr, 6, 1);
  550. }
  551. #endif
  552. }
  553. }
  554. #endif