fdt_support.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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 <stdio_dev.h>
  25. #include <linux/ctype.h>
  26. #include <linux/types.h>
  27. #include <asm/global_data.h>
  28. #include <fdt.h>
  29. #include <libfdt.h>
  30. #include <fdt_support.h>
  31. #include <exports.h>
  32. /*
  33. * Global data (for the gd->bd)
  34. */
  35. DECLARE_GLOBAL_DATA_PTR;
  36. /**
  37. * fdt_getprop_u32_default - Find a node and return it's property or a default
  38. *
  39. * @fdt: ptr to device tree
  40. * @path: path of node
  41. * @prop: property name
  42. * @dflt: default value if the property isn't found
  43. *
  44. * Convenience function to find a node and return it's property or a
  45. * default value if it doesn't exist.
  46. */
  47. u32 fdt_getprop_u32_default(void *fdt, const char *path, const char *prop,
  48. const u32 dflt)
  49. {
  50. const u32 *val;
  51. int off;
  52. off = fdt_path_offset(fdt, path);
  53. if (off < 0)
  54. return dflt;
  55. val = fdt_getprop(fdt, off, prop, NULL);
  56. if (val)
  57. return *val;
  58. else
  59. return dflt;
  60. }
  61. /**
  62. * fdt_find_and_setprop: Find a node and set it's property
  63. *
  64. * @fdt: ptr to device tree
  65. * @node: path of node
  66. * @prop: property name
  67. * @val: ptr to new value
  68. * @len: length of new property value
  69. * @create: flag to create the property if it doesn't exist
  70. *
  71. * Convenience function to directly set a property given the path to the node.
  72. */
  73. int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
  74. const void *val, int len, int create)
  75. {
  76. int nodeoff = fdt_path_offset(fdt, node);
  77. if (nodeoff < 0)
  78. return nodeoff;
  79. if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL))
  80. return 0; /* create flag not set; so exit quietly */
  81. return fdt_setprop(fdt, nodeoff, prop, val, len);
  82. }
  83. #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
  84. #ifdef CONFIG_SERIAL_MULTI
  85. static void fdt_fill_multisername(char *sername, size_t maxlen)
  86. {
  87. const char *outname = stdio_devices[stdout]->name;
  88. if (strcmp(outname, "serial") > 0)
  89. strncpy(sername, outname, maxlen);
  90. /* eserial? */
  91. if (strcmp(outname + 1, "serial") > 0)
  92. strncpy(sername, outname + 1, maxlen);
  93. }
  94. #else
  95. static inline void fdt_fill_multisername(char *sername, size_t maxlen) {}
  96. #endif /* CONFIG_SERIAL_MULTI */
  97. static int fdt_fixup_stdout(void *fdt, int chosenoff)
  98. {
  99. int err = 0;
  100. #ifdef CONFIG_CONS_INDEX
  101. int node;
  102. char sername[9] = { 0 };
  103. const char *path;
  104. fdt_fill_multisername(sername, sizeof(sername) - 1);
  105. if (!sername[0])
  106. sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
  107. err = node = fdt_path_offset(fdt, "/aliases");
  108. if (node >= 0) {
  109. int len;
  110. path = fdt_getprop(fdt, node, sername, &len);
  111. if (path) {
  112. char *p = malloc(len);
  113. err = -FDT_ERR_NOSPACE;
  114. if (p) {
  115. memcpy(p, path, len);
  116. err = fdt_setprop(fdt, chosenoff,
  117. "linux,stdout-path", p, len);
  118. free(p);
  119. }
  120. } else {
  121. err = len;
  122. }
  123. }
  124. #endif
  125. if (err < 0)
  126. printf("WARNING: could not set linux,stdout-path %s.\n",
  127. fdt_strerror(err));
  128. return err;
  129. }
  130. #endif
  131. int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
  132. {
  133. int nodeoffset;
  134. int err, j, total;
  135. u32 tmp;
  136. const char *path;
  137. uint64_t addr, size;
  138. /* Find the "chosen" node. */
  139. nodeoffset = fdt_path_offset (fdt, "/chosen");
  140. /* If there is no "chosen" node in the blob return */
  141. if (nodeoffset < 0) {
  142. printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
  143. return nodeoffset;
  144. }
  145. /* just return if initrd_start/end aren't valid */
  146. if ((initrd_start == 0) || (initrd_end == 0))
  147. return 0;
  148. total = fdt_num_mem_rsv(fdt);
  149. /*
  150. * Look for an existing entry and update it. If we don't find
  151. * the entry, we will j be the next available slot.
  152. */
  153. for (j = 0; j < total; j++) {
  154. err = fdt_get_mem_rsv(fdt, j, &addr, &size);
  155. if (addr == initrd_start) {
  156. fdt_del_mem_rsv(fdt, j);
  157. break;
  158. }
  159. }
  160. err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start + 1);
  161. if (err < 0) {
  162. printf("fdt_initrd: %s\n", fdt_strerror(err));
  163. return err;
  164. }
  165. path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
  166. if ((path == NULL) || force) {
  167. tmp = __cpu_to_be32(initrd_start);
  168. err = fdt_setprop(fdt, nodeoffset,
  169. "linux,initrd-start", &tmp, sizeof(tmp));
  170. if (err < 0) {
  171. printf("WARNING: "
  172. "could not set linux,initrd-start %s.\n",
  173. fdt_strerror(err));
  174. return err;
  175. }
  176. tmp = __cpu_to_be32(initrd_end);
  177. err = fdt_setprop(fdt, nodeoffset,
  178. "linux,initrd-end", &tmp, sizeof(tmp));
  179. if (err < 0) {
  180. printf("WARNING: could not set linux,initrd-end %s.\n",
  181. fdt_strerror(err));
  182. return err;
  183. }
  184. }
  185. return 0;
  186. }
  187. int fdt_chosen(void *fdt, int force)
  188. {
  189. int nodeoffset;
  190. int err;
  191. char *str; /* used to set string properties */
  192. const char *path;
  193. err = fdt_check_header(fdt);
  194. if (err < 0) {
  195. printf("fdt_chosen: %s\n", fdt_strerror(err));
  196. return err;
  197. }
  198. /*
  199. * Find the "chosen" node.
  200. */
  201. nodeoffset = fdt_path_offset (fdt, "/chosen");
  202. /*
  203. * If there is no "chosen" node in the blob, create it.
  204. */
  205. if (nodeoffset < 0) {
  206. /*
  207. * Create a new node "/chosen" (offset 0 is root level)
  208. */
  209. nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
  210. if (nodeoffset < 0) {
  211. printf("WARNING: could not create /chosen %s.\n",
  212. fdt_strerror(nodeoffset));
  213. return nodeoffset;
  214. }
  215. }
  216. /*
  217. * Create /chosen properites that don't exist in the fdt.
  218. * If the property exists, update it only if the "force" parameter
  219. * is true.
  220. */
  221. str = getenv("bootargs");
  222. if (str != NULL) {
  223. path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
  224. if ((path == NULL) || force) {
  225. err = fdt_setprop(fdt, nodeoffset,
  226. "bootargs", str, strlen(str)+1);
  227. if (err < 0)
  228. printf("WARNING: could not set bootargs %s.\n",
  229. fdt_strerror(err));
  230. }
  231. }
  232. #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
  233. path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
  234. if ((path == NULL) || force)
  235. err = fdt_fixup_stdout(fdt, nodeoffset);
  236. #endif
  237. #ifdef OF_STDOUT_PATH
  238. path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
  239. if ((path == NULL) || force) {
  240. err = fdt_setprop(fdt, nodeoffset,
  241. "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
  242. if (err < 0)
  243. printf("WARNING: could not set linux,stdout-path %s.\n",
  244. fdt_strerror(err));
  245. }
  246. #endif
  247. return err;
  248. }
  249. void do_fixup_by_path(void *fdt, const char *path, const char *prop,
  250. const void *val, int len, int create)
  251. {
  252. #if defined(DEBUG)
  253. int i;
  254. debug("Updating property '%s/%s' = ", path, prop);
  255. for (i = 0; i < len; i++)
  256. debug(" %.2x", *(u8*)(val+i));
  257. debug("\n");
  258. #endif
  259. int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
  260. if (rc)
  261. printf("Unable to update property %s:%s, err=%s\n",
  262. path, prop, fdt_strerror(rc));
  263. }
  264. void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
  265. u32 val, int create)
  266. {
  267. val = cpu_to_fdt32(val);
  268. do_fixup_by_path(fdt, path, prop, &val, sizeof(val), create);
  269. }
  270. void do_fixup_by_prop(void *fdt,
  271. const char *pname, const void *pval, int plen,
  272. const char *prop, const void *val, int len,
  273. int create)
  274. {
  275. int off;
  276. #if defined(DEBUG)
  277. int i;
  278. debug("Updating property '%s' = ", prop);
  279. for (i = 0; i < len; i++)
  280. debug(" %.2x", *(u8*)(val+i));
  281. debug("\n");
  282. #endif
  283. off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
  284. while (off != -FDT_ERR_NOTFOUND) {
  285. if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
  286. fdt_setprop(fdt, off, prop, val, len);
  287. off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
  288. }
  289. }
  290. void do_fixup_by_prop_u32(void *fdt,
  291. const char *pname, const void *pval, int plen,
  292. const char *prop, u32 val, int create)
  293. {
  294. val = cpu_to_fdt32(val);
  295. do_fixup_by_prop(fdt, pname, pval, plen, prop, &val, 4, create);
  296. }
  297. void do_fixup_by_compat(void *fdt, const char *compat,
  298. const char *prop, const void *val, int len, int create)
  299. {
  300. int off = -1;
  301. #if defined(DEBUG)
  302. int i;
  303. debug("Updating property '%s' = ", prop);
  304. for (i = 0; i < len; i++)
  305. debug(" %.2x", *(u8*)(val+i));
  306. debug("\n");
  307. #endif
  308. off = fdt_node_offset_by_compatible(fdt, -1, compat);
  309. while (off != -FDT_ERR_NOTFOUND) {
  310. if (create || (fdt_get_property(fdt, off, prop, 0) != NULL))
  311. fdt_setprop(fdt, off, prop, val, len);
  312. off = fdt_node_offset_by_compatible(fdt, off, compat);
  313. }
  314. }
  315. void do_fixup_by_compat_u32(void *fdt, const char *compat,
  316. const char *prop, u32 val, int create)
  317. {
  318. val = cpu_to_fdt32(val);
  319. do_fixup_by_compat(fdt, compat, prop, &val, 4, create);
  320. }
  321. int fdt_fixup_memory(void *blob, u64 start, u64 size)
  322. {
  323. int err, nodeoffset, len = 0;
  324. u8 tmp[16];
  325. const u32 *addrcell, *sizecell;
  326. err = fdt_check_header(blob);
  327. if (err < 0) {
  328. printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
  329. return err;
  330. }
  331. /* update, or add and update /memory node */
  332. nodeoffset = fdt_path_offset(blob, "/memory");
  333. if (nodeoffset < 0) {
  334. nodeoffset = fdt_add_subnode(blob, 0, "memory");
  335. if (nodeoffset < 0)
  336. printf("WARNING: could not create /memory: %s.\n",
  337. fdt_strerror(nodeoffset));
  338. return nodeoffset;
  339. }
  340. err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
  341. sizeof("memory"));
  342. if (err < 0) {
  343. printf("WARNING: could not set %s %s.\n", "device_type",
  344. fdt_strerror(err));
  345. return err;
  346. }
  347. addrcell = fdt_getprop(blob, 0, "#address-cells", NULL);
  348. /* use shifts and mask to ensure endianness */
  349. if ((addrcell) && (*addrcell == 2)) {
  350. tmp[0] = (start >> 56) & 0xff;
  351. tmp[1] = (start >> 48) & 0xff;
  352. tmp[2] = (start >> 40) & 0xff;
  353. tmp[3] = (start >> 32) & 0xff;
  354. tmp[4] = (start >> 24) & 0xff;
  355. tmp[5] = (start >> 16) & 0xff;
  356. tmp[6] = (start >> 8) & 0xff;
  357. tmp[7] = (start ) & 0xff;
  358. len = 8;
  359. } else {
  360. tmp[0] = (start >> 24) & 0xff;
  361. tmp[1] = (start >> 16) & 0xff;
  362. tmp[2] = (start >> 8) & 0xff;
  363. tmp[3] = (start ) & 0xff;
  364. len = 4;
  365. }
  366. sizecell = fdt_getprop(blob, 0, "#size-cells", NULL);
  367. /* use shifts and mask to ensure endianness */
  368. if ((sizecell) && (*sizecell == 2)) {
  369. tmp[0+len] = (size >> 56) & 0xff;
  370. tmp[1+len] = (size >> 48) & 0xff;
  371. tmp[2+len] = (size >> 40) & 0xff;
  372. tmp[3+len] = (size >> 32) & 0xff;
  373. tmp[4+len] = (size >> 24) & 0xff;
  374. tmp[5+len] = (size >> 16) & 0xff;
  375. tmp[6+len] = (size >> 8) & 0xff;
  376. tmp[7+len] = (size ) & 0xff;
  377. len += 8;
  378. } else {
  379. tmp[0+len] = (size >> 24) & 0xff;
  380. tmp[1+len] = (size >> 16) & 0xff;
  381. tmp[2+len] = (size >> 8) & 0xff;
  382. tmp[3+len] = (size ) & 0xff;
  383. len += 4;
  384. }
  385. err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
  386. if (err < 0) {
  387. printf("WARNING: could not set %s %s.\n",
  388. "reg", fdt_strerror(err));
  389. return err;
  390. }
  391. return 0;
  392. }
  393. void fdt_fixup_ethernet(void *fdt)
  394. {
  395. int node, i, j;
  396. char enet[16], *tmp, *end;
  397. char mac[16] = "ethaddr";
  398. const char *path;
  399. unsigned char mac_addr[6];
  400. node = fdt_path_offset(fdt, "/aliases");
  401. if (node < 0)
  402. return;
  403. i = 0;
  404. while ((tmp = getenv(mac)) != NULL) {
  405. sprintf(enet, "ethernet%d", i);
  406. path = fdt_getprop(fdt, node, enet, NULL);
  407. if (!path) {
  408. debug("No alias for %s\n", enet);
  409. sprintf(mac, "eth%daddr", ++i);
  410. continue;
  411. }
  412. for (j = 0; j < 6; j++) {
  413. mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  414. if (tmp)
  415. tmp = (*end) ? end+1 : end;
  416. }
  417. do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
  418. do_fixup_by_path(fdt, path, "local-mac-address",
  419. &mac_addr, 6, 1);
  420. sprintf(mac, "eth%daddr", ++i);
  421. }
  422. }
  423. #ifdef CONFIG_HAS_FSL_DR_USB
  424. void fdt_fixup_dr_usb(void *blob, bd_t *bd)
  425. {
  426. char *mode;
  427. char *type;
  428. const char *compat = "fsl-usb2-dr";
  429. const char *prop_mode = "dr_mode";
  430. const char *prop_type = "phy_type";
  431. int node_offset;
  432. int err;
  433. mode = getenv("usb_dr_mode");
  434. type = getenv("usb_phy_type");
  435. if (!mode && !type)
  436. return;
  437. node_offset = fdt_node_offset_by_compatible(blob, 0, compat);
  438. if (node_offset < 0) {
  439. printf("WARNING: could not find compatible node %s: %s.\n",
  440. compat, fdt_strerror(node_offset));
  441. return;
  442. }
  443. if (mode) {
  444. err = fdt_setprop(blob, node_offset, prop_mode, mode,
  445. strlen(mode) + 1);
  446. if (err < 0)
  447. printf("WARNING: could not set %s for %s: %s.\n",
  448. prop_mode, compat, fdt_strerror(err));
  449. }
  450. if (type) {
  451. err = fdt_setprop(blob, node_offset, prop_type, type,
  452. strlen(type) + 1);
  453. if (err < 0)
  454. printf("WARNING: could not set %s for %s: %s.\n",
  455. prop_type, compat, fdt_strerror(err));
  456. }
  457. }
  458. #endif /* CONFIG_HAS_FSL_DR_USB */
  459. #if defined(CONFIG_MPC83xx) || defined(CONFIG_MPC85xx)
  460. /*
  461. * update crypto node properties to a specified revision of the SEC
  462. * called with sec_rev == 0 if not on an mpc8xxxE processor
  463. */
  464. void fdt_fixup_crypto_node(void *blob, int sec_rev)
  465. {
  466. const struct sec_rev_prop {
  467. u32 sec_rev;
  468. u32 num_channels;
  469. u32 channel_fifo_len;
  470. u32 exec_units_mask;
  471. u32 descriptor_types_mask;
  472. } sec_rev_prop_list [] = {
  473. { 0x0200, 4, 24, 0x07e, 0x01010ebf }, /* SEC 2.0 */
  474. { 0x0201, 4, 24, 0x0fe, 0x012b0ebf }, /* SEC 2.1 */
  475. { 0x0202, 1, 24, 0x04c, 0x0122003f }, /* SEC 2.2 */
  476. { 0x0204, 4, 24, 0x07e, 0x012b0ebf }, /* SEC 2.4 */
  477. { 0x0300, 4, 24, 0x9fe, 0x03ab0ebf }, /* SEC 3.0 */
  478. { 0x0303, 4, 24, 0x97c, 0x03ab0abf }, /* SEC 3.3 */
  479. };
  480. char compat_strlist[ARRAY_SIZE(sec_rev_prop_list) *
  481. sizeof("fsl,secX.Y")];
  482. int crypto_node, sec_idx, err;
  483. char *p;
  484. u32 val;
  485. /* locate crypto node based on lowest common compatible */
  486. crypto_node = fdt_node_offset_by_compatible(blob, -1, "fsl,sec2.0");
  487. if (crypto_node == -FDT_ERR_NOTFOUND)
  488. return;
  489. /* delete it if not on an E-processor */
  490. if (crypto_node > 0 && !sec_rev) {
  491. fdt_del_node(blob, crypto_node);
  492. return;
  493. }
  494. /* else we got called for possible uprev */
  495. for (sec_idx = 0; sec_idx < ARRAY_SIZE(sec_rev_prop_list); sec_idx++)
  496. if (sec_rev_prop_list[sec_idx].sec_rev == sec_rev)
  497. break;
  498. if (sec_idx == ARRAY_SIZE(sec_rev_prop_list)) {
  499. puts("warning: unknown SEC revision number\n");
  500. return;
  501. }
  502. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].num_channels);
  503. err = fdt_setprop(blob, crypto_node, "fsl,num-channels", &val, 4);
  504. if (err < 0)
  505. printf("WARNING: could not set crypto property: %s\n",
  506. fdt_strerror(err));
  507. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].descriptor_types_mask);
  508. err = fdt_setprop(blob, crypto_node, "fsl,descriptor-types-mask", &val, 4);
  509. if (err < 0)
  510. printf("WARNING: could not set crypto property: %s\n",
  511. fdt_strerror(err));
  512. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].exec_units_mask);
  513. err = fdt_setprop(blob, crypto_node, "fsl,exec-units-mask", &val, 4);
  514. if (err < 0)
  515. printf("WARNING: could not set crypto property: %s\n",
  516. fdt_strerror(err));
  517. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].channel_fifo_len);
  518. err = fdt_setprop(blob, crypto_node, "fsl,channel-fifo-len", &val, 4);
  519. if (err < 0)
  520. printf("WARNING: could not set crypto property: %s\n",
  521. fdt_strerror(err));
  522. val = 0;
  523. while (sec_idx >= 0) {
  524. p = compat_strlist + val;
  525. val += sprintf(p, "fsl,sec%d.%d",
  526. (sec_rev_prop_list[sec_idx].sec_rev & 0xff00) >> 8,
  527. sec_rev_prop_list[sec_idx].sec_rev & 0x00ff) + 1;
  528. sec_idx--;
  529. }
  530. err = fdt_setprop(blob, crypto_node, "compatible", &compat_strlist, val);
  531. if (err < 0)
  532. printf("WARNING: could not set crypto property: %s\n",
  533. fdt_strerror(err));
  534. }
  535. #endif /* defined(CONFIG_MPC83xx) || defined(CONFIG_MPC85xx) */
  536. /* Resize the fdt to its actual size + a bit of padding */
  537. int fdt_resize(void *blob)
  538. {
  539. int i;
  540. uint64_t addr, size;
  541. int total, ret;
  542. uint actualsize;
  543. if (!blob)
  544. return 0;
  545. total = fdt_num_mem_rsv(blob);
  546. for (i = 0; i < total; i++) {
  547. fdt_get_mem_rsv(blob, i, &addr, &size);
  548. if (addr == (uint64_t)(u32)blob) {
  549. fdt_del_mem_rsv(blob, i);
  550. break;
  551. }
  552. }
  553. /*
  554. * Calculate the actual size of the fdt
  555. * plus the size needed for two fdt_add_mem_rsv, one
  556. * for the fdt itself and one for a possible initrd
  557. */
  558. actualsize = fdt_off_dt_strings(blob) +
  559. fdt_size_dt_strings(blob) + 2*sizeof(struct fdt_reserve_entry);
  560. /* Make it so the fdt ends on a page boundary */
  561. actualsize = ALIGN(actualsize + ((uint)blob & 0xfff), 0x1000);
  562. actualsize = actualsize - ((uint)blob & 0xfff);
  563. /* Change the fdt header to reflect the correct size */
  564. fdt_set_totalsize(blob, actualsize);
  565. /* Add the new reservation */
  566. ret = fdt_add_mem_rsv(blob, (uint)blob, actualsize);
  567. if (ret < 0)
  568. return ret;
  569. return actualsize;
  570. }
  571. #ifdef CONFIG_PCI
  572. #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
  573. #define FDT_PCI_PREFETCH (0x40000000)
  574. #define FDT_PCI_MEM32 (0x02000000)
  575. #define FDT_PCI_IO (0x01000000)
  576. #define FDT_PCI_MEM64 (0x03000000)
  577. int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
  578. int addrcell, sizecell, len, r;
  579. u32 *dma_range;
  580. /* sized based on pci addr cells, size-cells, & address-cells */
  581. u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
  582. addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
  583. sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
  584. dma_range = &dma_ranges[0];
  585. for (r = 0; r < hose->region_count; r++) {
  586. u64 bus_start, phys_start, size;
  587. /* skip if !PCI_REGION_SYS_MEMORY */
  588. if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
  589. continue;
  590. bus_start = (u64)hose->regions[r].bus_start;
  591. phys_start = (u64)hose->regions[r].phys_start;
  592. size = (u64)hose->regions[r].size;
  593. dma_range[0] = 0;
  594. if (size >= 0x100000000ull)
  595. dma_range[0] |= FDT_PCI_MEM64;
  596. else
  597. dma_range[0] |= FDT_PCI_MEM32;
  598. if (hose->regions[r].flags & PCI_REGION_PREFETCH)
  599. dma_range[0] |= FDT_PCI_PREFETCH;
  600. #ifdef CONFIG_SYS_PCI_64BIT
  601. dma_range[1] = bus_start >> 32;
  602. #else
  603. dma_range[1] = 0;
  604. #endif
  605. dma_range[2] = bus_start & 0xffffffff;
  606. if (addrcell == 2) {
  607. dma_range[3] = phys_start >> 32;
  608. dma_range[4] = phys_start & 0xffffffff;
  609. } else {
  610. dma_range[3] = phys_start & 0xffffffff;
  611. }
  612. if (sizecell == 2) {
  613. dma_range[3 + addrcell + 0] = size >> 32;
  614. dma_range[3 + addrcell + 1] = size & 0xffffffff;
  615. } else {
  616. dma_range[3 + addrcell + 0] = size & 0xffffffff;
  617. }
  618. dma_range += (3 + addrcell + sizecell);
  619. }
  620. len = dma_range - &dma_ranges[0];
  621. if (len)
  622. fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
  623. return 0;
  624. }
  625. #endif
  626. #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
  627. /*
  628. * This function can be used to update the size in the "reg" property
  629. * of the NOR FLASH device nodes. This is necessary for boards with
  630. * non-fixed NOR FLASH sizes.
  631. */
  632. int fdt_fixup_nor_flash_size(void *blob, int cs, u32 size)
  633. {
  634. char compat[][16] = { "cfi-flash", "jedec-flash" };
  635. int off;
  636. int len;
  637. struct fdt_property *prop;
  638. u32 *reg;
  639. int i;
  640. for (i = 0; i < 2; i++) {
  641. off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
  642. while (off != -FDT_ERR_NOTFOUND) {
  643. /*
  644. * Found one compatible node, now check if this one
  645. * has the correct CS
  646. */
  647. prop = fdt_get_property_w(blob, off, "reg", &len);
  648. if (prop) {
  649. reg = (u32 *)&prop->data[0];
  650. if (reg[0] == cs) {
  651. reg[2] = size;
  652. fdt_setprop(blob, off, "reg", reg,
  653. 3 * sizeof(u32));
  654. return 0;
  655. }
  656. }
  657. /* Move to next compatible node */
  658. off = fdt_node_offset_by_compatible(blob, off,
  659. compat[i]);
  660. }
  661. }
  662. return -1;
  663. }
  664. #endif
  665. #ifdef CONFIG_FDT_FIXUP_PARTITIONS
  666. #include <jffs2/load_kernel.h>
  667. #include <mtd_node.h>
  668. struct reg_cell {
  669. unsigned int r0;
  670. unsigned int r1;
  671. };
  672. int fdt_del_subnodes(const void *blob, int parent_offset)
  673. {
  674. int off, ndepth;
  675. int ret;
  676. for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
  677. (off >= 0) && (ndepth > 0);
  678. off = fdt_next_node(blob, off, &ndepth)) {
  679. if (ndepth == 1) {
  680. debug("delete %s: offset: %x\n",
  681. fdt_get_name(blob, off, 0), off);
  682. ret = fdt_del_node((void *)blob, off);
  683. if (ret < 0) {
  684. printf("Can't delete node: %s\n",
  685. fdt_strerror(ret));
  686. return ret;
  687. } else {
  688. ndepth = 0;
  689. off = parent_offset;
  690. }
  691. }
  692. }
  693. return 0;
  694. }
  695. int fdt_increase_size(void *fdt, int add_len)
  696. {
  697. int newlen;
  698. newlen = fdt_totalsize(fdt) + add_len;
  699. /* Open in place with a new len */
  700. return fdt_open_into(fdt, fdt, newlen);
  701. }
  702. int fdt_del_partitions(void *blob, int parent_offset)
  703. {
  704. const void *prop;
  705. int ndepth = 0;
  706. int off;
  707. int ret;
  708. off = fdt_next_node(blob, parent_offset, &ndepth);
  709. if (off > 0 && ndepth == 1) {
  710. prop = fdt_getprop(blob, off, "label", NULL);
  711. if (prop == NULL) {
  712. /*
  713. * Could not find label property, nand {}; node?
  714. * Check subnode, delete partitions there if any.
  715. */
  716. return fdt_del_partitions(blob, off);
  717. } else {
  718. ret = fdt_del_subnodes(blob, parent_offset);
  719. if (ret < 0) {
  720. printf("Can't remove subnodes: %s\n",
  721. fdt_strerror(ret));
  722. return ret;
  723. }
  724. }
  725. }
  726. return 0;
  727. }
  728. int fdt_node_set_part_info(void *blob, int parent_offset,
  729. struct mtd_device *dev)
  730. {
  731. struct list_head *pentry;
  732. struct part_info *part;
  733. struct reg_cell cell;
  734. int off, ndepth = 0;
  735. int part_num, ret;
  736. char buf[64];
  737. ret = fdt_del_partitions(blob, parent_offset);
  738. if (ret < 0)
  739. return ret;
  740. /*
  741. * Check if it is nand {}; subnode, adjust
  742. * the offset in this case
  743. */
  744. off = fdt_next_node(blob, parent_offset, &ndepth);
  745. if (off > 0 && ndepth == 1)
  746. parent_offset = off;
  747. part_num = 0;
  748. list_for_each_prev(pentry, &dev->parts) {
  749. int newoff;
  750. part = list_entry(pentry, struct part_info, link);
  751. debug("%2d: %-20s0x%08x\t0x%08x\t%d\n",
  752. part_num, part->name, part->size,
  753. part->offset, part->mask_flags);
  754. sprintf(buf, "partition@%x", part->offset);
  755. add_sub:
  756. ret = fdt_add_subnode(blob, parent_offset, buf);
  757. if (ret == -FDT_ERR_NOSPACE) {
  758. ret = fdt_increase_size(blob, 512);
  759. if (!ret)
  760. goto add_sub;
  761. else
  762. goto err_size;
  763. } else if (ret < 0) {
  764. printf("Can't add partition node: %s\n",
  765. fdt_strerror(ret));
  766. return ret;
  767. }
  768. newoff = ret;
  769. /* Check MTD_WRITEABLE_CMD flag */
  770. if (part->mask_flags & 1) {
  771. add_ro:
  772. ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
  773. if (ret == -FDT_ERR_NOSPACE) {
  774. ret = fdt_increase_size(blob, 512);
  775. if (!ret)
  776. goto add_ro;
  777. else
  778. goto err_size;
  779. } else if (ret < 0)
  780. goto err_prop;
  781. }
  782. cell.r0 = cpu_to_fdt32(part->offset);
  783. cell.r1 = cpu_to_fdt32(part->size);
  784. add_reg:
  785. ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
  786. if (ret == -FDT_ERR_NOSPACE) {
  787. ret = fdt_increase_size(blob, 512);
  788. if (!ret)
  789. goto add_reg;
  790. else
  791. goto err_size;
  792. } else if (ret < 0)
  793. goto err_prop;
  794. add_label:
  795. ret = fdt_setprop_string(blob, newoff, "label", part->name);
  796. if (ret == -FDT_ERR_NOSPACE) {
  797. ret = fdt_increase_size(blob, 512);
  798. if (!ret)
  799. goto add_label;
  800. else
  801. goto err_size;
  802. } else if (ret < 0)
  803. goto err_prop;
  804. part_num++;
  805. }
  806. return 0;
  807. err_size:
  808. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  809. return ret;
  810. err_prop:
  811. printf("Can't add property: %s\n", fdt_strerror(ret));
  812. return ret;
  813. }
  814. /*
  815. * Update partitions in nor/nand nodes using info from
  816. * mtdparts environment variable. The nodes to update are
  817. * specified by node_info structure which contains mtd device
  818. * type and compatible string: E. g. the board code in
  819. * ft_board_setup() could use:
  820. *
  821. * struct node_info nodes[] = {
  822. * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
  823. * { "cfi-flash", MTD_DEV_TYPE_NOR, },
  824. * };
  825. *
  826. * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
  827. */
  828. void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
  829. {
  830. struct node_info *ni = node_info;
  831. struct mtd_device *dev;
  832. char *parts;
  833. int i, idx;
  834. int noff;
  835. parts = getenv("mtdparts");
  836. if (!parts)
  837. return;
  838. if (mtdparts_init() != 0)
  839. return;
  840. for (i = 0; i < node_info_size; i++) {
  841. idx = 0;
  842. noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
  843. while (noff != -FDT_ERR_NOTFOUND) {
  844. debug("%s: %s, mtd dev type %d\n",
  845. fdt_get_name(blob, noff, 0),
  846. ni[i].compat, ni[i].type);
  847. dev = device_find(ni[i].type, idx++);
  848. if (dev) {
  849. if (fdt_node_set_part_info(blob, noff, dev))
  850. return; /* return on error */
  851. }
  852. /* Jump to next flash node */
  853. noff = fdt_node_offset_by_compatible(blob, noff,
  854. ni[i].compat);
  855. }
  856. }
  857. }
  858. #endif