fdt_support.c 18 KB

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