fdt_support.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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, ulong initrd_start, ulong initrd_end, 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. fdt_initrd(fdt, initrd_start, initrd_end, force);
  192. #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
  193. path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
  194. if ((path == NULL) || force)
  195. err = fdt_fixup_stdout(fdt, nodeoffset);
  196. #endif
  197. #ifdef OF_STDOUT_PATH
  198. path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
  199. if ((path == NULL) || force) {
  200. err = fdt_setprop(fdt, nodeoffset,
  201. "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
  202. if (err < 0)
  203. printf("WARNING: could not set linux,stdout-path %s.\n",
  204. fdt_strerror(err));
  205. }
  206. #endif
  207. return err;
  208. }
  209. void do_fixup_by_path(void *fdt, const char *path, const char *prop,
  210. const void *val, int len, int create)
  211. {
  212. #if defined(DEBUG)
  213. int i;
  214. debug("Updating property '%s/%s' = ", path, prop);
  215. for (i = 0; i < len; i++)
  216. debug(" %.2x", *(u8*)(val+i));
  217. debug("\n");
  218. #endif
  219. int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
  220. if (rc)
  221. printf("Unable to update property %s:%s, err=%s\n",
  222. path, prop, fdt_strerror(rc));
  223. }
  224. void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
  225. u32 val, int create)
  226. {
  227. val = cpu_to_fdt32(val);
  228. do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
  229. }
  230. void do_fixup_by_prop(void *fdt,
  231. const char *pname, const void *pval, int plen,
  232. const char *prop, const void *val, int len,
  233. int create)
  234. {
  235. int off;
  236. #if defined(DEBUG)
  237. int i;
  238. debug("Updating property '%s' = ", prop);
  239. for (i = 0; i < len; i++)
  240. debug(" %.2x", *(u8*)(val+i));
  241. debug("\n");
  242. #endif
  243. off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
  244. while (off != -FDT_ERR_NOTFOUND) {
  245. if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
  246. fdt_setprop(fdt, off, prop, val, len);
  247. off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
  248. }
  249. }
  250. void do_fixup_by_prop_u32(void *fdt,
  251. const char *pname, const void *pval, int plen,
  252. const char *prop, u32 val, int create)
  253. {
  254. val = cpu_to_fdt32(val);
  255. do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
  256. }
  257. void do_fixup_by_compat(void *fdt, const char *compat,
  258. const char *prop, const void *val, int len, int create)
  259. {
  260. int off = -1;
  261. #if defined(DEBUG)
  262. int i;
  263. debug("Updating property '%s' = ", prop);
  264. for (i = 0; i < len; i++)
  265. debug(" %.2x", *(u8*)(val+i));
  266. debug("\n");
  267. #endif
  268. off = fdt_node_offset_by_compatible(fdt, -1, compat);
  269. while (off != -FDT_ERR_NOTFOUND) {
  270. if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
  271. fdt_setprop(fdt, off, prop, val, len);
  272. off = fdt_node_offset_by_compatible(fdt, off, compat);
  273. }
  274. }
  275. void do_fixup_by_compat_u32(void *fdt, const char *compat,
  276. const char *prop, u32 val, int create)
  277. {
  278. val = cpu_to_fdt32(val);
  279. do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
  280. }
  281. int fdt_fixup_memory(void *blob, u64 start, u64 size)
  282. {
  283. int err, nodeoffset, len = 0;
  284. u8 tmp[16];
  285. const u32 *addrcell, *sizecell;
  286. err = fdt_check_header(blob);
  287. if (err < 0) {
  288. printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
  289. return err;
  290. }
  291. /* update, or add and update /memory node */
  292. nodeoffset = fdt_path_offset(blob, "/memory");
  293. if (nodeoffset < 0) {
  294. nodeoffset = fdt_add_subnode(blob, 0, "memory");
  295. if (nodeoffset < 0)
  296. printf("WARNING: could not create /memory: %s.\n",
  297. fdt_strerror(nodeoffset));
  298. return nodeoffset;
  299. }
  300. err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
  301. sizeof("memory"));
  302. if (err < 0) {
  303. printf("WARNING: could not set %s %s.\n", "device_type",
  304. fdt_strerror(err));
  305. return err;
  306. }
  307. addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
  308. /* use shifts and mask to ensure endianness */
  309. if ((addrcell) && (*addrcell == 2)) {
  310. tmp[0] = (start >> 56) & 0xff;
  311. tmp[1] = (start >> 48) & 0xff;
  312. tmp[2] = (start >> 40) & 0xff;
  313. tmp[3] = (start >> 32) & 0xff;
  314. tmp[4] = (start >> 24) & 0xff;
  315. tmp[5] = (start >> 16) & 0xff;
  316. tmp[6] = (start >> 8) & 0xff;
  317. tmp[7] = (start ) & 0xff;
  318. len = 8;
  319. } else {
  320. tmp[0] = (start >> 24) & 0xff;
  321. tmp[1] = (start >> 16) & 0xff;
  322. tmp[2] = (start >> 8) & 0xff;
  323. tmp[3] = (start ) & 0xff;
  324. len = 4;
  325. }
  326. sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
  327. /* use shifts and mask to ensure endianness */
  328. if ((sizecell) && (*sizecell == 2)) {
  329. tmp[0+len] = (size >> 56) & 0xff;
  330. tmp[1+len] = (size >> 48) & 0xff;
  331. tmp[2+len] = (size >> 40) & 0xff;
  332. tmp[3+len] = (size >> 32) & 0xff;
  333. tmp[4+len] = (size >> 24) & 0xff;
  334. tmp[5+len] = (size >> 16) & 0xff;
  335. tmp[6+len] = (size >> 8) & 0xff;
  336. tmp[7+len] = (size ) & 0xff;
  337. len += 8;
  338. } else {
  339. tmp[0+len] = (size >> 24) & 0xff;
  340. tmp[1+len] = (size >> 16) & 0xff;
  341. tmp[2+len] = (size >> 8) & 0xff;
  342. tmp[3+len] = (size ) & 0xff;
  343. len += 4;
  344. }
  345. err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
  346. if (err < 0) {
  347. printf("WARNING: could not set %s %s.\n",
  348. "reg", fdt_strerror(err));
  349. return err;
  350. }
  351. return 0;
  352. }
  353. void fdt_fixup_ethernet(void *fdt)
  354. {
  355. int node, i, j;
  356. char enet[16], *tmp, *end;
  357. char mac[16] = "ethaddr";
  358. const char *path;
  359. unsigned char mac_addr[6];
  360. node = fdt_path_offset(fdt, "/aliases");
  361. if (node < 0)
  362. return;
  363. i = 0;
  364. while ((tmp = getenv(mac)) != NULL) {
  365. sprintf(enet, "ethernet%d", i);
  366. path = fdt_getprop(fdt, node, enet, NULL);
  367. if (!path) {
  368. debug("No alias for %s\n", enet);
  369. sprintf(mac, "eth%daddr", ++i);
  370. continue;
  371. }
  372. for (j = 0; j < 6; j++) {
  373. mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  374. if (tmp)
  375. tmp = (*end) ? end+1 : end;
  376. }
  377. do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
  378. do_fixup_by_path(fdt, path, "local-mac-address",
  379. &mac_addr, 6, 1);
  380. sprintf(mac, "eth%daddr", ++i);
  381. }
  382. }
  383. #ifdef CONFIG_HAS_FSL_DR_USB
  384. void fdt_fixup_dr_usb(void *blob, bd_t *bd)
  385. {
  386. char *mode;
  387. char *type;
  388. const char *compat = "fsl-usb2-dr";
  389. const char *prop_mode = "dr_mode";
  390. const char *prop_type = "phy_type";
  391. int node_offset;
  392. int err;
  393. mode = getenv("usb_dr_mode");
  394. type = getenv("usb_phy_type");
  395. if (!mode && !type)
  396. return;
  397. node_offset = fdt_node_offset_by_compatible(blob, 0, compat);
  398. if (node_offset < 0) {
  399. printf("WARNING: could not find compatible node %s: %s.\n",
  400. compat, fdt_strerror(node_offset));
  401. return;
  402. }
  403. if (mode) {
  404. err = fdt_setprop(blob, node_offset, prop_mode, mode,
  405. strlen(mode) + 1);
  406. if (err < 0)
  407. printf("WARNING: could not set %s for %s: %s.\n",
  408. prop_mode, compat, fdt_strerror(err));
  409. }
  410. if (type) {
  411. err = fdt_setprop(blob, node_offset, prop_type, type,
  412. strlen(type) + 1);
  413. if (err < 0)
  414. printf("WARNING: could not set %s for %s: %s.\n",
  415. prop_type, compat, fdt_strerror(err));
  416. }
  417. }
  418. #endif /* CONFIG_HAS_FSL_DR_USB */
  419. #if defined(CONFIG_MPC83XX) || defined(CONFIG_MPC85xx)
  420. /*
  421. * update crypto node properties to a specified revision of the SEC
  422. * called with sec_rev == 0 if not on an mpc8xxxE processor
  423. */
  424. void fdt_fixup_crypto_node(void *blob, int sec_rev)
  425. {
  426. const struct sec_rev_prop {
  427. u32 sec_rev;
  428. u32 num_channels;
  429. u32 channel_fifo_len;
  430. u32 exec_units_mask;
  431. u32 descriptor_types_mask;
  432. } sec_rev_prop_list [] = {
  433. { 0x0200, 4, 24, 0x07e, 0x01010ebf }, /* SEC 2.0 */
  434. { 0x0201, 4, 24, 0x0fe, 0x012b0ebf }, /* SEC 2.1 */
  435. { 0x0202, 1, 24, 0x04c, 0x0122003f }, /* SEC 2.2 */
  436. { 0x0204, 4, 24, 0x07e, 0x012b0ebf }, /* SEC 2.4 */
  437. { 0x0300, 4, 24, 0x9fe, 0x03ab0ebf }, /* SEC 3.0 */
  438. { 0x0303, 4, 24, 0x97c, 0x03ab0abf }, /* SEC 3.3 */
  439. };
  440. char compat_strlist[ARRAY_SIZE(sec_rev_prop_list) *
  441. sizeof("fsl,secX.Y")];
  442. int crypto_node, sec_idx, err;
  443. char *p;
  444. u32 val;
  445. /* locate crypto node based on lowest common compatible */
  446. crypto_node = fdt_node_offset_by_compatible(blob, -1, "fsl,sec2.0");
  447. if (crypto_node == -FDT_ERR_NOTFOUND)
  448. return;
  449. /* delete it if not on an E-processor */
  450. if (crypto_node > 0 && !sec_rev) {
  451. fdt_del_node(blob, crypto_node);
  452. return;
  453. }
  454. /* else we got called for possible uprev */
  455. for (sec_idx = 0; sec_idx < ARRAY_SIZE(sec_rev_prop_list); sec_idx++)
  456. if (sec_rev_prop_list[sec_idx].sec_rev == sec_rev)
  457. break;
  458. if (sec_idx == ARRAY_SIZE(sec_rev_prop_list)) {
  459. puts("warning: unknown SEC revision number\n");
  460. return;
  461. }
  462. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].num_channels);
  463. err = fdt_setprop(blob, crypto_node, "fsl,num-channels", &val, 4);
  464. if (err < 0)
  465. printf("WARNING: could not set crypto property: %s\n",
  466. fdt_strerror(err));
  467. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].descriptor_types_mask);
  468. err = fdt_setprop(blob, crypto_node, "fsl,descriptor-types-mask", &val, 4);
  469. if (err < 0)
  470. printf("WARNING: could not set crypto property: %s\n",
  471. fdt_strerror(err));
  472. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].exec_units_mask);
  473. err = fdt_setprop(blob, crypto_node, "fsl,exec-units-mask", &val, 4);
  474. if (err < 0)
  475. printf("WARNING: could not set crypto property: %s\n",
  476. fdt_strerror(err));
  477. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].channel_fifo_len);
  478. err = fdt_setprop(blob, crypto_node, "fsl,channel-fifo-len", &val, 4);
  479. if (err < 0)
  480. printf("WARNING: could not set crypto property: %s\n",
  481. fdt_strerror(err));
  482. val = 0;
  483. while (sec_idx >= 0) {
  484. p = compat_strlist + val;
  485. val += sprintf(p, "fsl,sec%d.%d",
  486. (sec_rev_prop_list[sec_idx].sec_rev & 0xff00) >> 8,
  487. sec_rev_prop_list[sec_idx].sec_rev & 0x00ff) + 1;
  488. sec_idx--;
  489. }
  490. err = fdt_setprop(blob, crypto_node, "compatible", &compat_strlist, val);
  491. if (err < 0)
  492. printf("WARNING: could not set crypto property: %s\n",
  493. fdt_strerror(err));
  494. }
  495. #endif /* defined(CONFIG_MPC83XX) || defined(CONFIG_MPC85xx) */
  496. /* Resize the fdt to its actual size + a bit of padding */
  497. int fdt_resize(void *blob)
  498. {
  499. int i;
  500. uint64_t addr, size;
  501. int total, ret;
  502. uint actualsize;
  503. if (!blob)
  504. return 0;
  505. total = fdt_num_mem_rsv(blob);
  506. for (i = 0; i < total; i++) {
  507. fdt_get_mem_rsv(blob, i, &addr, &size);
  508. if (addr == (uint64_t)(u32)blob) {
  509. fdt_del_mem_rsv(blob, i);
  510. break;
  511. }
  512. }
  513. /* Calculate the actual size of the fdt */
  514. actualsize = fdt_off_dt_strings(blob) +
  515. fdt_size_dt_strings(blob);
  516. /* Make it so the fdt ends on a page boundary */
  517. actualsize = ALIGN(actualsize, 0x1000);
  518. actualsize = actualsize - ((uint)blob & 0xfff);
  519. /* Change the fdt header to reflect the correct size */
  520. fdt_set_totalsize(blob, actualsize);
  521. /* Add the new reservation */
  522. ret = fdt_add_mem_rsv(blob, (uint)blob, actualsize);
  523. if (ret < 0)
  524. return ret;
  525. return actualsize;
  526. }