fdt_support.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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_chosen(void *fdt, ulong initrd_start, ulong initrd_end, int force)
  91. {
  92. int nodeoffset;
  93. int err;
  94. u32 tmp; /* used to set 32 bit integer properties */
  95. char *str; /* used to set string properties */
  96. const char *path;
  97. err = fdt_check_header(fdt);
  98. if (err < 0) {
  99. printf("fdt_chosen: %s\n", fdt_strerror(err));
  100. return err;
  101. }
  102. if (initrd_start && initrd_end) {
  103. uint64_t addr, size;
  104. int total = fdt_num_mem_rsv(fdt);
  105. int j;
  106. /*
  107. * Look for an existing entry and update it. If we don't find
  108. * the entry, we will j be the next available slot.
  109. */
  110. for (j = 0; j < total; j++) {
  111. err = fdt_get_mem_rsv(fdt, j, &addr, &size);
  112. if (addr == initrd_start) {
  113. fdt_del_mem_rsv(fdt, j);
  114. break;
  115. }
  116. }
  117. err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1);
  118. if (err < 0) {
  119. printf("fdt_chosen: %s\n", fdt_strerror(err));
  120. return err;
  121. }
  122. }
  123. /*
  124. * Find the "chosen" node.
  125. */
  126. nodeoffset = fdt_path_offset (fdt, "/chosen");
  127. /*
  128. * If there is no "chosen" node in the blob, create it.
  129. */
  130. if (nodeoffset < 0) {
  131. /*
  132. * Create a new node "/chosen" (offset 0 is root level)
  133. */
  134. nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
  135. if (nodeoffset < 0) {
  136. printf("WARNING: could not create /chosen %s.\n",
  137. fdt_strerror(nodeoffset));
  138. return nodeoffset;
  139. }
  140. }
  141. /*
  142. * Create /chosen properites that don't exist in the fdt.
  143. * If the property exists, update it only if the "force" parameter
  144. * is true.
  145. */
  146. str = getenv("bootargs");
  147. if (str != NULL) {
  148. path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
  149. if ((path == NULL) || force) {
  150. err = fdt_setprop(fdt, nodeoffset,
  151. "bootargs", str, strlen(str)+1);
  152. if (err < 0)
  153. printf("WARNING: could not set bootargs %s.\n",
  154. fdt_strerror(err));
  155. }
  156. }
  157. if (initrd_start && initrd_end) {
  158. path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
  159. if ((path == NULL) || force) {
  160. tmp = __cpu_to_be32(initrd_start);
  161. err = fdt_setprop(fdt, nodeoffset,
  162. "linux,initrd-start", &tmp, sizeof(tmp));
  163. if (err < 0)
  164. printf("WARNING: "
  165. "could not set linux,initrd-start %s.\n",
  166. fdt_strerror(err));
  167. tmp = __cpu_to_be32(initrd_end);
  168. err = fdt_setprop(fdt, nodeoffset,
  169. "linux,initrd-end", &tmp, sizeof(tmp));
  170. if (err < 0)
  171. printf("WARNING: could not set linux,initrd-end %s.\n",
  172. fdt_strerror(err));
  173. }
  174. }
  175. #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
  176. path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
  177. if ((path == NULL) || force)
  178. err = fdt_fixup_stdout(fdt, nodeoffset);
  179. #endif
  180. #ifdef OF_STDOUT_PATH
  181. path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
  182. if ((path == NULL) || force) {
  183. err = fdt_setprop(fdt, nodeoffset,
  184. "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
  185. if (err < 0)
  186. printf("WARNING: could not set linux,stdout-path %s.\n",
  187. fdt_strerror(err));
  188. }
  189. #endif
  190. return err;
  191. }
  192. void do_fixup_by_path(void *fdt, const char *path, const char *prop,
  193. const void *val, int len, int create)
  194. {
  195. #if defined(DEBUG)
  196. int i;
  197. debug("Updating property '%s/%s' = ", path, prop);
  198. for (i = 0; i < len; i++)
  199. debug(" %.2x", *(u8*)(val+i));
  200. debug("\n");
  201. #endif
  202. int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
  203. if (rc)
  204. printf("Unable to update property %s:%s, err=%s\n",
  205. path, prop, fdt_strerror(rc));
  206. }
  207. void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
  208. u32 val, int create)
  209. {
  210. val = cpu_to_fdt32(val);
  211. do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
  212. }
  213. void do_fixup_by_prop(void *fdt,
  214. const char *pname, const void *pval, int plen,
  215. const char *prop, const void *val, int len,
  216. int create)
  217. {
  218. int off;
  219. #if defined(DEBUG)
  220. int i;
  221. debug("Updating property '%s' = ", prop);
  222. for (i = 0; i < len; i++)
  223. debug(" %.2x", *(u8*)(val+i));
  224. debug("\n");
  225. #endif
  226. off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
  227. while (off != -FDT_ERR_NOTFOUND) {
  228. if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
  229. fdt_setprop(fdt, off, prop, val, len);
  230. off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
  231. }
  232. }
  233. void do_fixup_by_prop_u32(void *fdt,
  234. const char *pname, const void *pval, int plen,
  235. const char *prop, u32 val, int create)
  236. {
  237. val = cpu_to_fdt32(val);
  238. do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
  239. }
  240. void do_fixup_by_compat(void *fdt, const char *compat,
  241. const char *prop, const void *val, int len, int create)
  242. {
  243. int off = -1;
  244. #if defined(DEBUG)
  245. int i;
  246. debug("Updating property '%s' = ", prop);
  247. for (i = 0; i < len; i++)
  248. debug(" %.2x", *(u8*)(val+i));
  249. debug("\n");
  250. #endif
  251. off = fdt_node_offset_by_compatible(fdt, -1, compat);
  252. while (off != -FDT_ERR_NOTFOUND) {
  253. if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
  254. fdt_setprop(fdt, off, prop, val, len);
  255. off = fdt_node_offset_by_compatible(fdt, off, compat);
  256. }
  257. }
  258. void do_fixup_by_compat_u32(void *fdt, const char *compat,
  259. const char *prop, u32 val, int create)
  260. {
  261. val = cpu_to_fdt32(val);
  262. do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
  263. }
  264. int fdt_fixup_memory(void *blob, u64 start, u64 size)
  265. {
  266. int err, nodeoffset, len = 0;
  267. u8 tmp[16];
  268. const u32 *addrcell, *sizecell;
  269. err = fdt_check_header(blob);
  270. if (err < 0) {
  271. printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
  272. return err;
  273. }
  274. /* update, or add and update /memory node */
  275. nodeoffset = fdt_path_offset(blob, "/memory");
  276. if (nodeoffset < 0) {
  277. nodeoffset = fdt_add_subnode(blob, 0, "memory");
  278. if (nodeoffset < 0)
  279. printf("WARNING: could not create /memory: %s.\n",
  280. fdt_strerror(nodeoffset));
  281. return nodeoffset;
  282. }
  283. err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
  284. sizeof("memory"));
  285. if (err < 0) {
  286. printf("WARNING: could not set %s %s.\n", "device_type",
  287. fdt_strerror(err));
  288. return err;
  289. }
  290. addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
  291. /* use shifts and mask to ensure endianness */
  292. if ((addrcell) && (*addrcell == 2)) {
  293. tmp[0] = (start >> 56) & 0xff;
  294. tmp[1] = (start >> 48) & 0xff;
  295. tmp[2] = (start >> 40) & 0xff;
  296. tmp[3] = (start >> 32) & 0xff;
  297. tmp[4] = (start >> 24) & 0xff;
  298. tmp[5] = (start >> 16) & 0xff;
  299. tmp[6] = (start >> 8) & 0xff;
  300. tmp[7] = (start ) & 0xff;
  301. len = 8;
  302. } else {
  303. tmp[0] = (start >> 24) & 0xff;
  304. tmp[1] = (start >> 16) & 0xff;
  305. tmp[2] = (start >> 8) & 0xff;
  306. tmp[3] = (start ) & 0xff;
  307. len = 4;
  308. }
  309. sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
  310. /* use shifts and mask to ensure endianness */
  311. if ((sizecell) && (*sizecell == 2)) {
  312. tmp[0+len] = (size >> 56) & 0xff;
  313. tmp[1+len] = (size >> 48) & 0xff;
  314. tmp[2+len] = (size >> 40) & 0xff;
  315. tmp[3+len] = (size >> 32) & 0xff;
  316. tmp[4+len] = (size >> 24) & 0xff;
  317. tmp[5+len] = (size >> 16) & 0xff;
  318. tmp[6+len] = (size >> 8) & 0xff;
  319. tmp[7+len] = (size ) & 0xff;
  320. len += 8;
  321. } else {
  322. tmp[0+len] = (size >> 24) & 0xff;
  323. tmp[1+len] = (size >> 16) & 0xff;
  324. tmp[2+len] = (size >> 8) & 0xff;
  325. tmp[3+len] = (size ) & 0xff;
  326. len += 4;
  327. }
  328. err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
  329. if (err < 0) {
  330. printf("WARNING: could not set %s %s.\n",
  331. "reg", fdt_strerror(err));
  332. return err;
  333. }
  334. return 0;
  335. }
  336. void fdt_fixup_ethernet(void *fdt)
  337. {
  338. int node, i, j;
  339. char enet[16], *tmp, *end;
  340. char mac[16] = "ethaddr";
  341. const char *path;
  342. unsigned char mac_addr[6];
  343. node = fdt_path_offset(fdt, "/aliases");
  344. if (node < 0)
  345. return;
  346. i = 0;
  347. while ((tmp = getenv(mac)) != NULL) {
  348. sprintf(enet, "ethernet%d", i);
  349. path = fdt_getprop(fdt, node, enet, NULL);
  350. if (!path) {
  351. debug("No alias for %s\n", enet);
  352. sprintf(mac, "eth%daddr", ++i);
  353. continue;
  354. }
  355. for (j = 0; j < 6; j++) {
  356. mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  357. if (tmp)
  358. tmp = (*end) ? end+1 : end;
  359. }
  360. do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
  361. do_fixup_by_path(fdt, path, "local-mac-address",
  362. &mac_addr, 6, 1);
  363. sprintf(mac, "eth%daddr", ++i);
  364. }
  365. }
  366. #ifdef CONFIG_HAS_FSL_DR_USB
  367. void fdt_fixup_dr_usb(void *blob, bd_t *bd)
  368. {
  369. char *mode;
  370. const char *compat = "fsl-usb2-dr";
  371. const char *prop = "dr_mode";
  372. int node_offset;
  373. int err;
  374. mode = getenv("usb_dr_mode");
  375. if (!mode)
  376. return;
  377. node_offset = fdt_node_offset_by_compatible(blob, 0, compat);
  378. if (node_offset < 0)
  379. printf("WARNING: could not find compatible node %s: %s.\n",
  380. compat, fdt_strerror(node_offset));
  381. err = fdt_setprop(blob, node_offset, prop, mode, strlen(mode) + 1);
  382. if (err < 0)
  383. printf("WARNING: could not set %s for %s: %s.\n",
  384. prop, compat, fdt_strerror(err));
  385. }
  386. #endif /* CONFIG_HAS_FSL_DR_USB */
  387. #if defined(CONFIG_MPC83XX) || defined(CONFIG_MPC85xx)
  388. /*
  389. * update crypto node properties to a specified revision of the SEC
  390. * called with sec_rev == 0 if not on an mpc8xxxE processor
  391. */
  392. void fdt_fixup_crypto_node(void *blob, int sec_rev)
  393. {
  394. const struct sec_rev_prop {
  395. u32 sec_rev;
  396. u32 num_channels;
  397. u32 channel_fifo_len;
  398. u32 exec_units_mask;
  399. u32 descriptor_types_mask;
  400. } sec_rev_prop_list [] = {
  401. { 0x0200, 4, 24, 0x07e, 0x01010ebf }, /* SEC 2.0 */
  402. { 0x0201, 4, 24, 0x0fe, 0x012b0ebf }, /* SEC 2.1 */
  403. { 0x0202, 1, 24, 0x04c, 0x0122003f }, /* SEC 2.2 */
  404. { 0x0204, 4, 24, 0x07e, 0x012b0ebf }, /* SEC 2.4 */
  405. { 0x0300, 4, 24, 0x9fe, 0x03ab0ebf }, /* SEC 3.0 */
  406. { 0x0303, 4, 24, 0x97c, 0x03ab0abf }, /* SEC 3.3 */
  407. };
  408. char compat_strlist[ARRAY_SIZE(sec_rev_prop_list) *
  409. sizeof("fsl,secX.Y")];
  410. int crypto_node, sec_idx, err;
  411. char *p;
  412. u32 val;
  413. /* locate crypto node based on lowest common compatible */
  414. crypto_node = fdt_node_offset_by_compatible(blob, -1, "fsl,sec2.0");
  415. if (crypto_node == -FDT_ERR_NOTFOUND)
  416. return;
  417. /* delete it if not on an E-processor */
  418. if (crypto_node > 0 && !sec_rev) {
  419. fdt_del_node(blob, crypto_node);
  420. return;
  421. }
  422. /* else we got called for possible uprev */
  423. for (sec_idx = 0; sec_idx < ARRAY_SIZE(sec_rev_prop_list); sec_idx++)
  424. if (sec_rev_prop_list[sec_idx].sec_rev == sec_rev)
  425. break;
  426. if (sec_idx == ARRAY_SIZE(sec_rev_prop_list)) {
  427. puts("warning: unknown SEC revision number\n");
  428. return;
  429. }
  430. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].num_channels);
  431. err = fdt_setprop(blob, crypto_node, "fsl,num-channels", &val, 4);
  432. if (err < 0)
  433. printf("WARNING: could not set crypto property: %s\n",
  434. fdt_strerror(err));
  435. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].descriptor_types_mask);
  436. err = fdt_setprop(blob, crypto_node, "fsl,descriptor-types-mask", &val, 4);
  437. if (err < 0)
  438. printf("WARNING: could not set crypto property: %s\n",
  439. fdt_strerror(err));
  440. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].exec_units_mask);
  441. err = fdt_setprop(blob, crypto_node, "fsl,exec-units-mask", &val, 4);
  442. if (err < 0)
  443. printf("WARNING: could not set crypto property: %s\n",
  444. fdt_strerror(err));
  445. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].channel_fifo_len);
  446. err = fdt_setprop(blob, crypto_node, "fsl,channel-fifo-len", &val, 4);
  447. if (err < 0)
  448. printf("WARNING: could not set crypto property: %s\n",
  449. fdt_strerror(err));
  450. val = 0;
  451. while (sec_idx >= 0) {
  452. p = compat_strlist + val;
  453. val += sprintf(p, "fsl,sec%d.%d",
  454. (sec_rev_prop_list[sec_idx].sec_rev & 0xff00) >> 8,
  455. sec_rev_prop_list[sec_idx].sec_rev & 0x00ff) + 1;
  456. sec_idx--;
  457. }
  458. err = fdt_setprop(blob, crypto_node, "compatible", &compat_strlist, val);
  459. if (err < 0)
  460. printf("WARNING: could not set crypto property: %s\n",
  461. fdt_strerror(err));
  462. }
  463. #endif /* defined(CONFIG_MPC83XX) || defined(CONFIG_MPC85xx) */