fdt_support.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. { 0x0301, 4, 24, 0xbfe, 0x03ab0ebf }, /* SEC 3.1 */
  479. { 0x0303, 4, 24, 0x97c, 0x03a30abf }, /* SEC 3.3 */
  480. };
  481. char compat_strlist[ARRAY_SIZE(sec_rev_prop_list) *
  482. sizeof("fsl,secX.Y")];
  483. int crypto_node, sec_idx, err;
  484. char *p;
  485. u32 val;
  486. /* locate crypto node based on lowest common compatible */
  487. crypto_node = fdt_node_offset_by_compatible(blob, -1, "fsl,sec2.0");
  488. if (crypto_node == -FDT_ERR_NOTFOUND)
  489. return;
  490. /* delete it if not on an E-processor */
  491. if (crypto_node > 0 && !sec_rev) {
  492. fdt_del_node(blob, crypto_node);
  493. return;
  494. }
  495. /* else we got called for possible uprev */
  496. for (sec_idx = 0; sec_idx < ARRAY_SIZE(sec_rev_prop_list); sec_idx++)
  497. if (sec_rev_prop_list[sec_idx].sec_rev == sec_rev)
  498. break;
  499. if (sec_idx == ARRAY_SIZE(sec_rev_prop_list)) {
  500. puts("warning: unknown SEC revision number\n");
  501. return;
  502. }
  503. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].num_channels);
  504. err = fdt_setprop(blob, crypto_node, "fsl,num-channels", &val, 4);
  505. if (err < 0)
  506. printf("WARNING: could not set crypto property: %s\n",
  507. fdt_strerror(err));
  508. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].descriptor_types_mask);
  509. err = fdt_setprop(blob, crypto_node, "fsl,descriptor-types-mask", &val, 4);
  510. if (err < 0)
  511. printf("WARNING: could not set crypto property: %s\n",
  512. fdt_strerror(err));
  513. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].exec_units_mask);
  514. err = fdt_setprop(blob, crypto_node, "fsl,exec-units-mask", &val, 4);
  515. if (err < 0)
  516. printf("WARNING: could not set crypto property: %s\n",
  517. fdt_strerror(err));
  518. val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].channel_fifo_len);
  519. err = fdt_setprop(blob, crypto_node, "fsl,channel-fifo-len", &val, 4);
  520. if (err < 0)
  521. printf("WARNING: could not set crypto property: %s\n",
  522. fdt_strerror(err));
  523. val = 0;
  524. while (sec_idx >= 0) {
  525. p = compat_strlist + val;
  526. val += sprintf(p, "fsl,sec%d.%d",
  527. (sec_rev_prop_list[sec_idx].sec_rev & 0xff00) >> 8,
  528. sec_rev_prop_list[sec_idx].sec_rev & 0x00ff) + 1;
  529. sec_idx--;
  530. }
  531. err = fdt_setprop(blob, crypto_node, "compatible", &compat_strlist, val);
  532. if (err < 0)
  533. printf("WARNING: could not set crypto property: %s\n",
  534. fdt_strerror(err));
  535. }
  536. #endif /* defined(CONFIG_MPC83xx) || defined(CONFIG_MPC85xx) */
  537. /* Resize the fdt to its actual size + a bit of padding */
  538. int fdt_resize(void *blob)
  539. {
  540. int i;
  541. uint64_t addr, size;
  542. int total, ret;
  543. uint actualsize;
  544. if (!blob)
  545. return 0;
  546. total = fdt_num_mem_rsv(blob);
  547. for (i = 0; i < total; i++) {
  548. fdt_get_mem_rsv(blob, i, &addr, &size);
  549. if (addr == (uint64_t)(u32)blob) {
  550. fdt_del_mem_rsv(blob, i);
  551. break;
  552. }
  553. }
  554. /*
  555. * Calculate the actual size of the fdt
  556. * plus the size needed for two fdt_add_mem_rsv, one
  557. * for the fdt itself and one for a possible initrd
  558. */
  559. actualsize = fdt_off_dt_strings(blob) +
  560. fdt_size_dt_strings(blob) + 2*sizeof(struct fdt_reserve_entry);
  561. /* Make it so the fdt ends on a page boundary */
  562. actualsize = ALIGN(actualsize + ((uint)blob & 0xfff), 0x1000);
  563. actualsize = actualsize - ((uint)blob & 0xfff);
  564. /* Change the fdt header to reflect the correct size */
  565. fdt_set_totalsize(blob, actualsize);
  566. /* Add the new reservation */
  567. ret = fdt_add_mem_rsv(blob, (uint)blob, actualsize);
  568. if (ret < 0)
  569. return ret;
  570. return actualsize;
  571. }
  572. #ifdef CONFIG_PCI
  573. #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
  574. #define FDT_PCI_PREFETCH (0x40000000)
  575. #define FDT_PCI_MEM32 (0x02000000)
  576. #define FDT_PCI_IO (0x01000000)
  577. #define FDT_PCI_MEM64 (0x03000000)
  578. int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
  579. int addrcell, sizecell, len, r;
  580. u32 *dma_range;
  581. /* sized based on pci addr cells, size-cells, & address-cells */
  582. u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
  583. addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
  584. sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
  585. dma_range = &dma_ranges[0];
  586. for (r = 0; r < hose->region_count; r++) {
  587. u64 bus_start, phys_start, size;
  588. /* skip if !PCI_REGION_SYS_MEMORY */
  589. if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
  590. continue;
  591. bus_start = (u64)hose->regions[r].bus_start;
  592. phys_start = (u64)hose->regions[r].phys_start;
  593. size = (u64)hose->regions[r].size;
  594. dma_range[0] = 0;
  595. if (size >= 0x100000000ull)
  596. dma_range[0] |= FDT_PCI_MEM64;
  597. else
  598. dma_range[0] |= FDT_PCI_MEM32;
  599. if (hose->regions[r].flags & PCI_REGION_PREFETCH)
  600. dma_range[0] |= FDT_PCI_PREFETCH;
  601. #ifdef CONFIG_SYS_PCI_64BIT
  602. dma_range[1] = bus_start >> 32;
  603. #else
  604. dma_range[1] = 0;
  605. #endif
  606. dma_range[2] = bus_start & 0xffffffff;
  607. if (addrcell == 2) {
  608. dma_range[3] = phys_start >> 32;
  609. dma_range[4] = phys_start & 0xffffffff;
  610. } else {
  611. dma_range[3] = phys_start & 0xffffffff;
  612. }
  613. if (sizecell == 2) {
  614. dma_range[3 + addrcell + 0] = size >> 32;
  615. dma_range[3 + addrcell + 1] = size & 0xffffffff;
  616. } else {
  617. dma_range[3 + addrcell + 0] = size & 0xffffffff;
  618. }
  619. dma_range += (3 + addrcell + sizecell);
  620. }
  621. len = dma_range - &dma_ranges[0];
  622. if (len)
  623. fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
  624. return 0;
  625. }
  626. #endif
  627. #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
  628. /*
  629. * This function can be used to update the size in the "reg" property
  630. * of the NOR FLASH device nodes. This is necessary for boards with
  631. * non-fixed NOR FLASH sizes.
  632. */
  633. int fdt_fixup_nor_flash_size(void *blob, int cs, u32 size)
  634. {
  635. char compat[][16] = { "cfi-flash", "jedec-flash" };
  636. int off;
  637. int len;
  638. struct fdt_property *prop;
  639. u32 *reg;
  640. int i;
  641. for (i = 0; i < 2; i++) {
  642. off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
  643. while (off != -FDT_ERR_NOTFOUND) {
  644. /*
  645. * Found one compatible node, now check if this one
  646. * has the correct CS
  647. */
  648. prop = fdt_get_property_w(blob, off, "reg", &len);
  649. if (prop) {
  650. reg = (u32 *)&prop->data[0];
  651. if (reg[0] == cs) {
  652. reg[2] = size;
  653. fdt_setprop(blob, off, "reg", reg,
  654. 3 * sizeof(u32));
  655. return 0;
  656. }
  657. }
  658. /* Move to next compatible node */
  659. off = fdt_node_offset_by_compatible(blob, off,
  660. compat[i]);
  661. }
  662. }
  663. return -1;
  664. }
  665. #endif
  666. #ifdef CONFIG_FDT_FIXUP_PARTITIONS
  667. #include <jffs2/load_kernel.h>
  668. #include <mtd_node.h>
  669. struct reg_cell {
  670. unsigned int r0;
  671. unsigned int r1;
  672. };
  673. int fdt_del_subnodes(const void *blob, int parent_offset)
  674. {
  675. int off, ndepth;
  676. int ret;
  677. for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
  678. (off >= 0) && (ndepth > 0);
  679. off = fdt_next_node(blob, off, &ndepth)) {
  680. if (ndepth == 1) {
  681. debug("delete %s: offset: %x\n",
  682. fdt_get_name(blob, off, 0), off);
  683. ret = fdt_del_node((void *)blob, off);
  684. if (ret < 0) {
  685. printf("Can't delete node: %s\n",
  686. fdt_strerror(ret));
  687. return ret;
  688. } else {
  689. ndepth = 0;
  690. off = parent_offset;
  691. }
  692. }
  693. }
  694. return 0;
  695. }
  696. int fdt_increase_size(void *fdt, int add_len)
  697. {
  698. int newlen;
  699. newlen = fdt_totalsize(fdt) + add_len;
  700. /* Open in place with a new len */
  701. return fdt_open_into(fdt, fdt, newlen);
  702. }
  703. int fdt_del_partitions(void *blob, int parent_offset)
  704. {
  705. const void *prop;
  706. int ndepth = 0;
  707. int off;
  708. int ret;
  709. off = fdt_next_node(blob, parent_offset, &ndepth);
  710. if (off > 0 && ndepth == 1) {
  711. prop = fdt_getprop(blob, off, "label", NULL);
  712. if (prop == NULL) {
  713. /*
  714. * Could not find label property, nand {}; node?
  715. * Check subnode, delete partitions there if any.
  716. */
  717. return fdt_del_partitions(blob, off);
  718. } else {
  719. ret = fdt_del_subnodes(blob, parent_offset);
  720. if (ret < 0) {
  721. printf("Can't remove subnodes: %s\n",
  722. fdt_strerror(ret));
  723. return ret;
  724. }
  725. }
  726. }
  727. return 0;
  728. }
  729. int fdt_node_set_part_info(void *blob, int parent_offset,
  730. struct mtd_device *dev)
  731. {
  732. struct list_head *pentry;
  733. struct part_info *part;
  734. struct reg_cell cell;
  735. int off, ndepth = 0;
  736. int part_num, ret;
  737. char buf[64];
  738. ret = fdt_del_partitions(blob, parent_offset);
  739. if (ret < 0)
  740. return ret;
  741. /*
  742. * Check if it is nand {}; subnode, adjust
  743. * the offset in this case
  744. */
  745. off = fdt_next_node(blob, parent_offset, &ndepth);
  746. if (off > 0 && ndepth == 1)
  747. parent_offset = off;
  748. part_num = 0;
  749. list_for_each_prev(pentry, &dev->parts) {
  750. int newoff;
  751. part = list_entry(pentry, struct part_info, link);
  752. debug("%2d: %-20s0x%08x\t0x%08x\t%d\n",
  753. part_num, part->name, part->size,
  754. part->offset, part->mask_flags);
  755. sprintf(buf, "partition@%x", part->offset);
  756. add_sub:
  757. ret = fdt_add_subnode(blob, parent_offset, buf);
  758. if (ret == -FDT_ERR_NOSPACE) {
  759. ret = fdt_increase_size(blob, 512);
  760. if (!ret)
  761. goto add_sub;
  762. else
  763. goto err_size;
  764. } else if (ret < 0) {
  765. printf("Can't add partition node: %s\n",
  766. fdt_strerror(ret));
  767. return ret;
  768. }
  769. newoff = ret;
  770. /* Check MTD_WRITEABLE_CMD flag */
  771. if (part->mask_flags & 1) {
  772. add_ro:
  773. ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
  774. if (ret == -FDT_ERR_NOSPACE) {
  775. ret = fdt_increase_size(blob, 512);
  776. if (!ret)
  777. goto add_ro;
  778. else
  779. goto err_size;
  780. } else if (ret < 0)
  781. goto err_prop;
  782. }
  783. cell.r0 = cpu_to_fdt32(part->offset);
  784. cell.r1 = cpu_to_fdt32(part->size);
  785. add_reg:
  786. ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
  787. if (ret == -FDT_ERR_NOSPACE) {
  788. ret = fdt_increase_size(blob, 512);
  789. if (!ret)
  790. goto add_reg;
  791. else
  792. goto err_size;
  793. } else if (ret < 0)
  794. goto err_prop;
  795. add_label:
  796. ret = fdt_setprop_string(blob, newoff, "label", part->name);
  797. if (ret == -FDT_ERR_NOSPACE) {
  798. ret = fdt_increase_size(blob, 512);
  799. if (!ret)
  800. goto add_label;
  801. else
  802. goto err_size;
  803. } else if (ret < 0)
  804. goto err_prop;
  805. part_num++;
  806. }
  807. return 0;
  808. err_size:
  809. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  810. return ret;
  811. err_prop:
  812. printf("Can't add property: %s\n", fdt_strerror(ret));
  813. return ret;
  814. }
  815. /*
  816. * Update partitions in nor/nand nodes using info from
  817. * mtdparts environment variable. The nodes to update are
  818. * specified by node_info structure which contains mtd device
  819. * type and compatible string: E. g. the board code in
  820. * ft_board_setup() could use:
  821. *
  822. * struct node_info nodes[] = {
  823. * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
  824. * { "cfi-flash", MTD_DEV_TYPE_NOR, },
  825. * };
  826. *
  827. * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
  828. */
  829. void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
  830. {
  831. struct node_info *ni = node_info;
  832. struct mtd_device *dev;
  833. char *parts;
  834. int i, idx;
  835. int noff;
  836. parts = getenv("mtdparts");
  837. if (!parts)
  838. return;
  839. if (mtdparts_init() != 0)
  840. return;
  841. for (i = 0; i < node_info_size; i++) {
  842. idx = 0;
  843. noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
  844. while (noff != -FDT_ERR_NOTFOUND) {
  845. debug("%s: %s, mtd dev type %d\n",
  846. fdt_get_name(blob, noff, 0),
  847. ni[i].compat, ni[i].type);
  848. dev = device_find(ni[i].type, idx++);
  849. if (dev) {
  850. if (fdt_node_set_part_info(blob, noff, dev))
  851. return; /* return on error */
  852. }
  853. /* Jump to next flash node */
  854. noff = fdt_node_offset_by_compatible(blob, noff,
  855. ni[i].compat);
  856. }
  857. }
  858. }
  859. #endif
  860. void fdt_del_node_and_alias(void *blob, const char *alias)
  861. {
  862. int off = fdt_path_offset(blob, alias);
  863. if (off < 0)
  864. return;
  865. fdt_del_node(blob, off);
  866. off = fdt_path_offset(blob, "/aliases");
  867. fdt_delprop(blob, off, alias);
  868. }