fdt_support.c 14 KB

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