fdt_support.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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_find_and_setprop: Find a node and set it's property
  37. *
  38. * @fdt: ptr to device tree
  39. * @node: path of node
  40. * @prop: property name
  41. * @val: ptr to new value
  42. * @len: length of new property value
  43. * @create: flag to create the property if it doesn't exist
  44. *
  45. * Convenience function to directly set a property given the path to the node.
  46. */
  47. int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
  48. const void *val, int len, int create)
  49. {
  50. int nodeoff = fdt_path_offset(fdt, node);
  51. if (nodeoff < 0)
  52. return nodeoff;
  53. if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL))
  54. return 0; /* create flag not set; so exit quietly */
  55. return fdt_setprop(fdt, nodeoff, prop, val, len);
  56. }
  57. #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
  58. static int fdt_fixup_stdout(void *fdt, int chosenoff)
  59. {
  60. int err = 0;
  61. #ifdef CONFIG_CONS_INDEX
  62. int node;
  63. char sername[9] = { 0 };
  64. const char *path;
  65. sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
  66. err = node = fdt_path_offset(fdt, "/aliases");
  67. if (node >= 0) {
  68. int len;
  69. path = fdt_getprop(fdt, node, sername, &len);
  70. if (path) {
  71. char *p = malloc(len);
  72. err = -FDT_ERR_NOSPACE;
  73. if (p) {
  74. memcpy(p, path, len);
  75. err = fdt_setprop(fdt, chosenoff,
  76. "linux,stdout-path", p, len);
  77. free(p);
  78. }
  79. } else {
  80. err = len;
  81. }
  82. }
  83. #endif
  84. if (err < 0)
  85. printf("WARNING: could not set linux,stdout-path %s.\n",
  86. fdt_strerror(err));
  87. return err;
  88. }
  89. #endif
  90. int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
  91. {
  92. int nodeoffset;
  93. int err, j, total;
  94. u32 tmp;
  95. const char *path;
  96. uint64_t addr, size;
  97. /* Find the "chosen" node. */
  98. nodeoffset = fdt_path_offset (fdt, "/chosen");
  99. /* If there is no "chosen" node in the blob return */
  100. if (nodeoffset < 0) {
  101. printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
  102. return nodeoffset;
  103. }
  104. /* just return if initrd_start/end aren't valid */
  105. if ((initrd_start == 0) || (initrd_end == 0))
  106. return 0;
  107. total = fdt_num_mem_rsv(fdt);
  108. /*
  109. * Look for an existing entry and update it. If we don't find
  110. * the entry, we will j be the next available slot.
  111. */
  112. for (j = 0; j < total; j++) {
  113. err = fdt_get_mem_rsv(fdt, j, &addr, &size);
  114. if (addr == initrd_start) {
  115. fdt_del_mem_rsv(fdt, j);
  116. break;
  117. }
  118. }
  119. err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1);
  120. if (err < 0) {
  121. printf("fdt_initrd: %s\n", fdt_strerror(err));
  122. return err;
  123. }
  124. path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
  125. if ((path == NULL) || force) {
  126. tmp = __cpu_to_be32(initrd_start);
  127. err = fdt_setprop(fdt, nodeoffset,
  128. "linux,initrd-start", &tmp, sizeof(tmp));
  129. if (err < 0) {
  130. printf("WARNING: "
  131. "could not set linux,initrd-start %s.\n",
  132. fdt_strerror(err));
  133. return err;
  134. }
  135. tmp = __cpu_to_be32(initrd_end);
  136. err = fdt_setprop(fdt, nodeoffset,
  137. "linux,initrd-end", &tmp, sizeof(tmp));
  138. if (err < 0) {
  139. printf("WARNING: could not set linux,initrd-end %s.\n",
  140. fdt_strerror(err));
  141. return err;
  142. }
  143. }
  144. return 0;
  145. }
  146. int fdt_chosen(void *fdt, int force)
  147. {
  148. int nodeoffset;
  149. int err;
  150. char *str; /* used to set string properties */
  151. const char *path;
  152. err = fdt_check_header(fdt);
  153. if (err < 0) {
  154. printf("fdt_chosen: %s\n", fdt_strerror(err));
  155. return err;
  156. }
  157. /*
  158. * Find the "chosen" node.
  159. */
  160. nodeoffset = fdt_path_offset (fdt, "/chosen");
  161. /*
  162. * If there is no "chosen" node in the blob, create it.
  163. */
  164. if (nodeoffset < 0) {
  165. /*
  166. * Create a new node "/chosen" (offset 0 is root level)
  167. */
  168. nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
  169. if (nodeoffset < 0) {
  170. printf("WARNING: could not create /chosen %s.\n",
  171. fdt_strerror(nodeoffset));
  172. return nodeoffset;
  173. }
  174. }
  175. /*
  176. * Create /chosen properites that don't exist in the fdt.
  177. * If the property exists, update it only if the "force" parameter
  178. * is true.
  179. */
  180. str = getenv("bootargs");
  181. if (str != NULL) {
  182. path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
  183. if ((path == NULL) || force) {
  184. err = fdt_setprop(fdt, nodeoffset,
  185. "bootargs", str, strlen(str)+1);
  186. if (err < 0)
  187. printf("WARNING: could not set bootargs %s.\n",
  188. fdt_strerror(err));
  189. }
  190. }
  191. #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
  192. path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
  193. if ((path == NULL) || force)
  194. err = fdt_fixup_stdout(fdt, nodeoffset);
  195. #endif
  196. #ifdef OF_STDOUT_PATH
  197. path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
  198. if ((path == NULL) || force) {
  199. err = fdt_setprop(fdt, nodeoffset,
  200. "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
  201. if (err < 0)
  202. printf("WARNING: could not set linux,stdout-path %s.\n",
  203. fdt_strerror(err));
  204. }
  205. #endif
  206. return err;
  207. }
  208. void do_fixup_by_path(void *fdt, const char *path, const char *prop,
  209. const void *val, int len, int create)
  210. {
  211. #if defined(DEBUG)
  212. int i;
  213. debug("Updating property '%s/%s' = ", path, prop);
  214. for (i = 0; i < len; i++)
  215. debug(" %.2x", *(u8*)(val+i));
  216. debug("\n");
  217. #endif
  218. int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
  219. if (rc)
  220. printf("Unable to update property %s:%s, err=%s\n",
  221. path, prop, fdt_strerror(rc));
  222. }
  223. void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
  224. u32 val, int create)
  225. {
  226. val = cpu_to_fdt32(val);
  227. do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
  228. }
  229. void do_fixup_by_prop(void *fdt,
  230. const char *pname, const void *pval, int plen,
  231. const char *prop, const void *val, int len,
  232. int create)
  233. {
  234. int off;
  235. #if defined(DEBUG)
  236. int i;
  237. debug("Updating property '%s' = ", prop);
  238. for (i = 0; i < len; i++)
  239. debug(" %.2x", *(u8*)(val+i));
  240. debug("\n");
  241. #endif
  242. off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
  243. while (off != -FDT_ERR_NOTFOUND) {
  244. if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
  245. fdt_setprop(fdt, off, prop, val, len);
  246. off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
  247. }
  248. }
  249. void do_fixup_by_prop_u32(void *fdt,
  250. const char *pname, const void *pval, int plen,
  251. const char *prop, u32 val, int create)
  252. {
  253. val = cpu_to_fdt32(val);
  254. do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
  255. }
  256. void do_fixup_by_compat(void *fdt, const char *compat,
  257. const char *prop, const void *val, int len, int create)
  258. {
  259. int off = -1;
  260. #if defined(DEBUG)
  261. int i;
  262. debug("Updating property '%s' = ", prop);
  263. for (i = 0; i < len; i++)
  264. debug(" %.2x", *(u8*)(val+i));
  265. debug("\n");
  266. #endif
  267. off = fdt_node_offset_by_compatible(fdt, -1, compat);
  268. while (off != -FDT_ERR_NOTFOUND) {
  269. if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
  270. fdt_setprop(fdt, off, prop, val, len);
  271. off = fdt_node_offset_by_compatible(fdt, off, compat);
  272. }
  273. }
  274. void do_fixup_by_compat_u32(void *fdt, const char *compat,
  275. const char *prop, u32 val, int create)
  276. {
  277. val = cpu_to_fdt32(val);
  278. do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
  279. }
  280. int fdt_fixup_memory(void *blob, u64 start, u64 size)
  281. {
  282. int err, nodeoffset, len = 0;
  283. u8 tmp[16];
  284. const u32 *addrcell, *sizecell;
  285. err = fdt_check_header(blob);
  286. if (err < 0) {
  287. printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
  288. return err;
  289. }
  290. /* update, or add and update /memory node */
  291. nodeoffset = fdt_path_offset(blob, "/memory");
  292. if (nodeoffset < 0) {
  293. nodeoffset = fdt_add_subnode(blob, 0, "memory");
  294. if (nodeoffset < 0)
  295. printf("WARNING: could not create /memory: %s.\n",
  296. fdt_strerror(nodeoffset));
  297. return nodeoffset;
  298. }
  299. err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
  300. sizeof("memory"));
  301. if (err < 0) {
  302. printf("WARNING: could not set %s %s.\n", "device_type",
  303. fdt_strerror(err));
  304. return err;
  305. }
  306. addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
  307. /* use shifts and mask to ensure endianness */
  308. if ((addrcell) && (*addrcell == 2)) {
  309. tmp[0] = (start >> 56) & 0xff;
  310. tmp[1] = (start >> 48) & 0xff;
  311. tmp[2] = (start >> 40) & 0xff;
  312. tmp[3] = (start >> 32) & 0xff;
  313. tmp[4] = (start >> 24) & 0xff;
  314. tmp[5] = (start >> 16) & 0xff;
  315. tmp[6] = (start >> 8) & 0xff;
  316. tmp[7] = (start ) & 0xff;
  317. len = 8;
  318. } else {
  319. tmp[0] = (start >> 24) & 0xff;
  320. tmp[1] = (start >> 16) & 0xff;
  321. tmp[2] = (start >> 8) & 0xff;
  322. tmp[3] = (start ) & 0xff;
  323. len = 4;
  324. }
  325. sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
  326. /* use shifts and mask to ensure endianness */
  327. if ((sizecell) && (*sizecell == 2)) {
  328. tmp[0+len] = (size >> 56) & 0xff;
  329. tmp[1+len] = (size >> 48) & 0xff;
  330. tmp[2+len] = (size >> 40) & 0xff;
  331. tmp[3+len] = (size >> 32) & 0xff;
  332. tmp[4+len] = (size >> 24) & 0xff;
  333. tmp[5+len] = (size >> 16) & 0xff;
  334. tmp[6+len] = (size >> 8) & 0xff;
  335. tmp[7+len] = (size ) & 0xff;
  336. len += 8;
  337. } else {
  338. tmp[0+len] = (size >> 24) & 0xff;
  339. tmp[1+len] = (size >> 16) & 0xff;
  340. tmp[2+len] = (size >> 8) & 0xff;
  341. tmp[3+len] = (size ) & 0xff;
  342. len += 4;
  343. }
  344. err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
  345. if (err < 0) {
  346. printf("WARNING: could not set %s %s.\n",
  347. "reg", fdt_strerror(err));
  348. return err;
  349. }
  350. return 0;
  351. }
  352. void fdt_fixup_ethernet(void *fdt)
  353. {
  354. int node, i, j;
  355. char enet[16], *tmp, *end;
  356. char mac[16] = "ethaddr";
  357. const char *path;
  358. unsigned char mac_addr[6];
  359. node = fdt_path_offset(fdt, "/aliases");
  360. if (node < 0)
  361. return;
  362. i = 0;
  363. while ((tmp = getenv(mac)) != NULL) {
  364. sprintf(enet, "ethernet%d", i);
  365. path = fdt_getprop(fdt, node, enet, NULL);
  366. if (!path) {
  367. debug("No alias for %s\n", enet);
  368. sprintf(mac, "eth%daddr", ++i);
  369. continue;
  370. }
  371. for (j = 0; j < 6; j++) {
  372. mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  373. if (tmp)
  374. tmp = (*end) ? end+1 : end;
  375. }
  376. do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
  377. do_fixup_by_path(fdt, path, "local-mac-address",
  378. &mac_addr, 6, 1);
  379. sprintf(mac, "eth%daddr", ++i);
  380. }
  381. }
  382. #ifdef CONFIG_HAS_FSL_DR_USB
  383. void fdt_fixup_dr_usb(void *blob, bd_t *bd)
  384. {
  385. char *mode;
  386. char *type;
  387. const char *compat = "fsl-usb2-dr";
  388. const char *prop_mode = "dr_mode";
  389. const char *prop_type = "phy_type";
  390. int node_offset;
  391. int err;
  392. mode = getenv("usb_dr_mode");
  393. type = getenv("usb_phy_type");
  394. if (!mode && !type)
  395. return;
  396. node_offset = fdt_node_offset_by_compatible(blob, 0, compat);
  397. if (node_offset < 0) {
  398. printf("WARNING: could not find compatible node %s: %s.\n",
  399. compat, fdt_strerror(node_offset));
  400. return;
  401. }
  402. if (mode) {
  403. err = fdt_setprop(blob, node_offset, prop_mode, mode,
  404. strlen(mode) + 1);
  405. if (err < 0)
  406. printf("WARNING: could not set %s for %s: %s.\n",
  407. prop_mode, compat, fdt_strerror(err));
  408. }
  409. if (type) {
  410. err = fdt_setprop(blob, node_offset, prop_type, type,
  411. strlen(type) + 1);
  412. if (err < 0)
  413. printf("WARNING: could not set %s for %s: %s.\n",
  414. prop_type, compat, fdt_strerror(err));
  415. }
  416. }
  417. #endif /* CONFIG_HAS_FSL_DR_USB */
  418. #if defined(CONFIG_MPC83XX) || defined(CONFIG_MPC85xx)
  419. /*
  420. * update crypto node properties to a specified revision of the SEC
  421. * called with sec_rev == 0 if not on an mpc8xxxE processor
  422. */
  423. void fdt_fixup_crypto_node(void *blob, int sec_rev)
  424. {
  425. const struct sec_rev_prop {
  426. u32 sec_rev;
  427. u32 num_channels;
  428. u32 channel_fifo_len;
  429. u32 exec_units_mask;
  430. u32 descriptor_types_mask;
  431. } sec_rev_prop_list [] = {
  432. { 0x0200, 4, 24, 0x07e, 0x01010ebf }, /* SEC 2.0 */
  433. { 0x0201, 4, 24, 0x0fe, 0x012b0ebf }, /* SEC 2.1 */
  434. { 0x0202, 1, 24, 0x04c, 0x0122003f }, /* SEC 2.2 */
  435. { 0x0204, 4, 24, 0x07e, 0x012b0ebf }, /* SEC 2.4 */
  436. { 0x0300, 4, 24, 0x9fe, 0x03ab0ebf }, /* SEC 3.0 */
  437. { 0x0303, 4, 24, 0x97c, 0x03ab0abf }, /* SEC 3.3 */
  438. };
  439. char compat_strlist[ARRAY_SIZE(sec_rev_prop_list) *
  440. sizeof("fsl,secX.Y")];
  441. int crypto_node, sec_idx, err;
  442. char *p;
  443. u32 val;
  444. /* locate crypto node based on lowest common compatible */
  445. crypto_node = fdt_node_offset_by_compatible(blob, -1, "fsl,sec2.0");
  446. if (crypto_node == -FDT_ERR_NOTFOUND)
  447. return;
  448. /* delete it if not on an E-processor */
  449. if (crypto_node > 0 && !sec_rev) {
  450. fdt_del_node(blob, crypto_node);
  451. return;
  452. }
  453. /* else we got called for possible uprev */
  454. for (sec_idx = 0; sec_idx < ARRAY_SIZE(sec_rev_prop_list); sec_idx++)
  455. if (sec_rev_prop_list[sec_idx].sec_rev == sec_rev)
  456. break;
  457. if (sec_idx == ARRAY_SIZE(sec_rev_prop_list)) {
  458. puts("warning: unknown SEC revision number\n");
  459. return;
  460. }
  461. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].num_channels);
  462. err = fdt_setprop(blob, crypto_node, "fsl,num-channels", &val, 4);
  463. if (err < 0)
  464. printf("WARNING: could not set crypto property: %s\n",
  465. fdt_strerror(err));
  466. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].descriptor_types_mask);
  467. err = fdt_setprop(blob, crypto_node, "fsl,descriptor-types-mask", &val, 4);
  468. if (err < 0)
  469. printf("WARNING: could not set crypto property: %s\n",
  470. fdt_strerror(err));
  471. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].exec_units_mask);
  472. err = fdt_setprop(blob, crypto_node, "fsl,exec-units-mask", &val, 4);
  473. if (err < 0)
  474. printf("WARNING: could not set crypto property: %s\n",
  475. fdt_strerror(err));
  476. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].channel_fifo_len);
  477. err = fdt_setprop(blob, crypto_node, "fsl,channel-fifo-len", &val, 4);
  478. if (err < 0)
  479. printf("WARNING: could not set crypto property: %s\n",
  480. fdt_strerror(err));
  481. val = 0;
  482. while (sec_idx >= 0) {
  483. p = compat_strlist + val;
  484. val += sprintf(p, "fsl,sec%d.%d",
  485. (sec_rev_prop_list[sec_idx].sec_rev & 0xff00) >> 8,
  486. sec_rev_prop_list[sec_idx].sec_rev & 0x00ff) + 1;
  487. sec_idx--;
  488. }
  489. err = fdt_setprop(blob, crypto_node, "compatible", &compat_strlist, val);
  490. if (err < 0)
  491. printf("WARNING: could not set crypto property: %s\n",
  492. fdt_strerror(err));
  493. }
  494. #endif /* defined(CONFIG_MPC83XX) || defined(CONFIG_MPC85xx) */
  495. /* Resize the fdt to its actual size + a bit of padding */
  496. int fdt_resize(void *blob)
  497. {
  498. int i;
  499. uint64_t addr, size;
  500. int total, ret;
  501. uint actualsize;
  502. if (!blob)
  503. return 0;
  504. total = fdt_num_mem_rsv(blob);
  505. for (i = 0; i < total; i++) {
  506. fdt_get_mem_rsv(blob, i, &addr, &size);
  507. if (addr == (uint64_t)(u32)blob) {
  508. fdt_del_mem_rsv(blob, i);
  509. break;
  510. }
  511. }
  512. /* Calculate the actual size of the fdt */
  513. actualsize = fdt_off_dt_strings(blob) +
  514. fdt_size_dt_strings(blob);
  515. /* Make it so the fdt ends on a page boundary */
  516. actualsize = ALIGN(actualsize, 0x1000);
  517. actualsize = actualsize - ((uint)blob & 0xfff);
  518. /* Change the fdt header to reflect the correct size */
  519. fdt_set_totalsize(blob, actualsize);
  520. /* Add the new reservation */
  521. ret = fdt_add_mem_rsv(blob, (uint)blob, actualsize);
  522. if (ret < 0)
  523. return ret;
  524. return actualsize;
  525. }