fdt_support.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  1. /*
  2. * (C) Copyright 2007
  3. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
  4. *
  5. * Copyright 2010-2011 Freescale Semiconductor, Inc.
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <stdio_dev.h>
  11. #include <linux/ctype.h>
  12. #include <linux/types.h>
  13. #include <asm/global_data.h>
  14. #include <libfdt.h>
  15. #include <fdt_support.h>
  16. #include <exports.h>
  17. /*
  18. * Global data (for the gd->bd)
  19. */
  20. DECLARE_GLOBAL_DATA_PTR;
  21. /*
  22. * Get cells len in bytes
  23. * if #NNNN-cells property is 2 then len is 8
  24. * otherwise len is 4
  25. */
  26. static int get_cells_len(void *blob, char *nr_cells_name)
  27. {
  28. const fdt32_t *cell;
  29. cell = fdt_getprop(blob, 0, nr_cells_name, NULL);
  30. if (cell && fdt32_to_cpu(*cell) == 2)
  31. return 8;
  32. return 4;
  33. }
  34. /*
  35. * Write a 4 or 8 byte big endian cell
  36. */
  37. static void write_cell(u8 *addr, u64 val, int size)
  38. {
  39. int shift = (size - 1) * 8;
  40. while (size-- > 0) {
  41. *addr++ = (val >> shift) & 0xff;
  42. shift -= 8;
  43. }
  44. }
  45. /**
  46. * fdt_getprop_u32_default - Find a node and return it's property or a default
  47. *
  48. * @fdt: ptr to device tree
  49. * @path: path of node
  50. * @prop: property name
  51. * @dflt: default value if the property isn't found
  52. *
  53. * Convenience function to find a node and return it's property or a
  54. * default value if it doesn't exist.
  55. */
  56. u32 fdt_getprop_u32_default(const void *fdt, const char *path,
  57. const char *prop, const u32 dflt)
  58. {
  59. const fdt32_t *val;
  60. int off;
  61. off = fdt_path_offset(fdt, path);
  62. if (off < 0)
  63. return dflt;
  64. val = fdt_getprop(fdt, off, prop, NULL);
  65. if (val)
  66. return fdt32_to_cpu(*val);
  67. else
  68. return dflt;
  69. }
  70. /**
  71. * fdt_find_and_setprop: Find a node and set it's property
  72. *
  73. * @fdt: ptr to device tree
  74. * @node: path of node
  75. * @prop: property name
  76. * @val: ptr to new value
  77. * @len: length of new property value
  78. * @create: flag to create the property if it doesn't exist
  79. *
  80. * Convenience function to directly set a property given the path to the node.
  81. */
  82. int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
  83. const void *val, int len, int create)
  84. {
  85. int nodeoff = fdt_path_offset(fdt, node);
  86. if (nodeoff < 0)
  87. return nodeoff;
  88. if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
  89. return 0; /* create flag not set; so exit quietly */
  90. return fdt_setprop(fdt, nodeoff, prop, val, len);
  91. }
  92. #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
  93. #ifdef CONFIG_CONS_INDEX
  94. static void fdt_fill_multisername(char *sername, size_t maxlen)
  95. {
  96. const char *outname = stdio_devices[stdout]->name;
  97. if (strcmp(outname, "serial") > 0)
  98. strncpy(sername, outname, maxlen);
  99. /* eserial? */
  100. if (strcmp(outname + 1, "serial") > 0)
  101. strncpy(sername, outname + 1, maxlen);
  102. }
  103. #endif
  104. static int fdt_fixup_stdout(void *fdt, int chosenoff)
  105. {
  106. int err = 0;
  107. #ifdef CONFIG_CONS_INDEX
  108. int node;
  109. char sername[9] = { 0 };
  110. const char *path;
  111. fdt_fill_multisername(sername, sizeof(sername) - 1);
  112. if (!sername[0])
  113. sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
  114. err = node = fdt_path_offset(fdt, "/aliases");
  115. if (node >= 0) {
  116. int len;
  117. path = fdt_getprop(fdt, node, sername, &len);
  118. if (path) {
  119. char *p = malloc(len);
  120. err = -FDT_ERR_NOSPACE;
  121. if (p) {
  122. memcpy(p, path, len);
  123. err = fdt_setprop(fdt, chosenoff,
  124. "linux,stdout-path", p, len);
  125. free(p);
  126. }
  127. } else {
  128. err = len;
  129. }
  130. }
  131. #endif
  132. if (err < 0)
  133. printf("WARNING: could not set linux,stdout-path %s.\n",
  134. fdt_strerror(err));
  135. return err;
  136. }
  137. #endif
  138. int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end, int force)
  139. {
  140. int nodeoffset, addr_cell_len;
  141. int err, j, total;
  142. fdt64_t tmp;
  143. const char *path;
  144. uint64_t addr, size;
  145. /* Find the "chosen" node. */
  146. nodeoffset = fdt_path_offset (fdt, "/chosen");
  147. /* If there is no "chosen" node in the blob return */
  148. if (nodeoffset < 0) {
  149. printf("fdt_initrd: %s\n", fdt_strerror(nodeoffset));
  150. return nodeoffset;
  151. }
  152. /* just return if initrd_start/end aren't valid */
  153. if ((initrd_start == 0) || (initrd_end == 0))
  154. return 0;
  155. total = fdt_num_mem_rsv(fdt);
  156. /*
  157. * Look for an existing entry and update it. If we don't find
  158. * the entry, we will j be the next available slot.
  159. */
  160. for (j = 0; j < total; j++) {
  161. err = fdt_get_mem_rsv(fdt, j, &addr, &size);
  162. if (addr == initrd_start) {
  163. fdt_del_mem_rsv(fdt, j);
  164. break;
  165. }
  166. }
  167. err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
  168. if (err < 0) {
  169. printf("fdt_initrd: %s\n", fdt_strerror(err));
  170. return err;
  171. }
  172. addr_cell_len = get_cells_len(fdt, "#address-cells");
  173. path = fdt_getprop(fdt, nodeoffset, "linux,initrd-start", NULL);
  174. if ((path == NULL) || force) {
  175. write_cell((u8 *)&tmp, initrd_start, addr_cell_len);
  176. err = fdt_setprop(fdt, nodeoffset,
  177. "linux,initrd-start", &tmp, sizeof(tmp));
  178. if (err < 0) {
  179. printf("WARNING: "
  180. "could not set linux,initrd-start %s.\n",
  181. fdt_strerror(err));
  182. return err;
  183. }
  184. write_cell((u8 *)&tmp, initrd_end, addr_cell_len);
  185. err = fdt_setprop(fdt, nodeoffset,
  186. "linux,initrd-end", &tmp, sizeof(tmp));
  187. if (err < 0) {
  188. printf("WARNING: could not set linux,initrd-end %s.\n",
  189. fdt_strerror(err));
  190. return err;
  191. }
  192. }
  193. return 0;
  194. }
  195. int fdt_chosen(void *fdt, int force)
  196. {
  197. int nodeoffset;
  198. int err;
  199. char *str; /* used to set string properties */
  200. const char *path;
  201. err = fdt_check_header(fdt);
  202. if (err < 0) {
  203. printf("fdt_chosen: %s\n", fdt_strerror(err));
  204. return err;
  205. }
  206. /*
  207. * Find the "chosen" node.
  208. */
  209. nodeoffset = fdt_path_offset (fdt, "/chosen");
  210. /*
  211. * If there is no "chosen" node in the blob, create it.
  212. */
  213. if (nodeoffset < 0) {
  214. /*
  215. * Create a new node "/chosen" (offset 0 is root level)
  216. */
  217. nodeoffset = fdt_add_subnode(fdt, 0, "chosen");
  218. if (nodeoffset < 0) {
  219. printf("WARNING: could not create /chosen %s.\n",
  220. fdt_strerror(nodeoffset));
  221. return nodeoffset;
  222. }
  223. }
  224. /*
  225. * Create /chosen properites that don't exist in the fdt.
  226. * If the property exists, update it only if the "force" parameter
  227. * is true.
  228. */
  229. str = getenv("bootargs");
  230. if (str != NULL) {
  231. path = fdt_getprop(fdt, nodeoffset, "bootargs", NULL);
  232. if ((path == NULL) || force) {
  233. err = fdt_setprop(fdt, nodeoffset,
  234. "bootargs", str, strlen(str)+1);
  235. if (err < 0)
  236. printf("WARNING: could not set bootargs %s.\n",
  237. fdt_strerror(err));
  238. }
  239. }
  240. #ifdef CONFIG_OF_STDOUT_VIA_ALIAS
  241. path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
  242. if ((path == NULL) || force)
  243. err = fdt_fixup_stdout(fdt, nodeoffset);
  244. #endif
  245. #ifdef OF_STDOUT_PATH
  246. path = fdt_getprop(fdt, nodeoffset, "linux,stdout-path", NULL);
  247. if ((path == NULL) || force) {
  248. err = fdt_setprop(fdt, nodeoffset,
  249. "linux,stdout-path", OF_STDOUT_PATH, strlen(OF_STDOUT_PATH)+1);
  250. if (err < 0)
  251. printf("WARNING: could not set linux,stdout-path %s.\n",
  252. fdt_strerror(err));
  253. }
  254. #endif
  255. return err;
  256. }
  257. void do_fixup_by_path(void *fdt, const char *path, const char *prop,
  258. const void *val, int len, int create)
  259. {
  260. #if defined(DEBUG)
  261. int i;
  262. debug("Updating property '%s/%s' = ", path, prop);
  263. for (i = 0; i < len; i++)
  264. debug(" %.2x", *(u8*)(val+i));
  265. debug("\n");
  266. #endif
  267. int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
  268. if (rc)
  269. printf("Unable to update property %s:%s, err=%s\n",
  270. path, prop, fdt_strerror(rc));
  271. }
  272. void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
  273. u32 val, int create)
  274. {
  275. fdt32_t tmp = cpu_to_fdt32(val);
  276. do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
  277. }
  278. void do_fixup_by_prop(void *fdt,
  279. const char *pname, const void *pval, int plen,
  280. const char *prop, const void *val, int len,
  281. int create)
  282. {
  283. int off;
  284. #if defined(DEBUG)
  285. int i;
  286. debug("Updating property '%s' = ", prop);
  287. for (i = 0; i < len; i++)
  288. debug(" %.2x", *(u8*)(val+i));
  289. debug("\n");
  290. #endif
  291. off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
  292. while (off != -FDT_ERR_NOTFOUND) {
  293. if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
  294. fdt_setprop(fdt, off, prop, val, len);
  295. off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
  296. }
  297. }
  298. void do_fixup_by_prop_u32(void *fdt,
  299. const char *pname, const void *pval, int plen,
  300. const char *prop, u32 val, int create)
  301. {
  302. fdt32_t tmp = cpu_to_fdt32(val);
  303. do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
  304. }
  305. void do_fixup_by_compat(void *fdt, const char *compat,
  306. const char *prop, const void *val, int len, int create)
  307. {
  308. int off = -1;
  309. #if defined(DEBUG)
  310. int i;
  311. debug("Updating property '%s' = ", prop);
  312. for (i = 0; i < len; i++)
  313. debug(" %.2x", *(u8*)(val+i));
  314. debug("\n");
  315. #endif
  316. off = fdt_node_offset_by_compatible(fdt, -1, compat);
  317. while (off != -FDT_ERR_NOTFOUND) {
  318. if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
  319. fdt_setprop(fdt, off, prop, val, len);
  320. off = fdt_node_offset_by_compatible(fdt, off, compat);
  321. }
  322. }
  323. void do_fixup_by_compat_u32(void *fdt, const char *compat,
  324. const char *prop, u32 val, int create)
  325. {
  326. fdt32_t tmp = cpu_to_fdt32(val);
  327. do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
  328. }
  329. #ifdef CONFIG_NR_DRAM_BANKS
  330. #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
  331. #else
  332. #define MEMORY_BANKS_MAX 4
  333. #endif
  334. int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
  335. {
  336. int err, nodeoffset;
  337. int addr_cell_len, size_cell_len, len;
  338. u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
  339. int bank;
  340. if (banks > MEMORY_BANKS_MAX) {
  341. printf("%s: num banks %d exceeds hardcoded limit %d."
  342. " Recompile with higher MEMORY_BANKS_MAX?\n",
  343. __FUNCTION__, banks, MEMORY_BANKS_MAX);
  344. return -1;
  345. }
  346. err = fdt_check_header(blob);
  347. if (err < 0) {
  348. printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
  349. return err;
  350. }
  351. /* update, or add and update /memory node */
  352. nodeoffset = fdt_path_offset(blob, "/memory");
  353. if (nodeoffset < 0) {
  354. nodeoffset = fdt_add_subnode(blob, 0, "memory");
  355. if (nodeoffset < 0)
  356. printf("WARNING: could not create /memory: %s.\n",
  357. fdt_strerror(nodeoffset));
  358. return nodeoffset;
  359. }
  360. err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
  361. sizeof("memory"));
  362. if (err < 0) {
  363. printf("WARNING: could not set %s %s.\n", "device_type",
  364. fdt_strerror(err));
  365. return err;
  366. }
  367. addr_cell_len = get_cells_len(blob, "#address-cells");
  368. size_cell_len = get_cells_len(blob, "#size-cells");
  369. for (bank = 0, len = 0; bank < banks; bank++) {
  370. write_cell(tmp + len, start[bank], addr_cell_len);
  371. len += addr_cell_len;
  372. write_cell(tmp + len, size[bank], size_cell_len);
  373. len += size_cell_len;
  374. }
  375. err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
  376. if (err < 0) {
  377. printf("WARNING: could not set %s %s.\n",
  378. "reg", fdt_strerror(err));
  379. return err;
  380. }
  381. return 0;
  382. }
  383. int fdt_fixup_memory(void *blob, u64 start, u64 size)
  384. {
  385. return fdt_fixup_memory_banks(blob, &start, &size, 1);
  386. }
  387. void fdt_fixup_ethernet(void *fdt)
  388. {
  389. int node, i, j;
  390. char enet[16], *tmp, *end;
  391. char mac[16];
  392. const char *path;
  393. unsigned char mac_addr[6];
  394. node = fdt_path_offset(fdt, "/aliases");
  395. if (node < 0)
  396. return;
  397. i = 0;
  398. strcpy(mac, "ethaddr");
  399. while ((tmp = getenv(mac)) != NULL) {
  400. sprintf(enet, "ethernet%d", i);
  401. path = fdt_getprop(fdt, node, enet, NULL);
  402. if (!path) {
  403. debug("No alias for %s\n", enet);
  404. sprintf(mac, "eth%daddr", ++i);
  405. continue;
  406. }
  407. for (j = 0; j < 6; j++) {
  408. mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  409. if (tmp)
  410. tmp = (*end) ? end+1 : end;
  411. }
  412. do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
  413. do_fixup_by_path(fdt, path, "local-mac-address",
  414. &mac_addr, 6, 1);
  415. sprintf(mac, "eth%daddr", ++i);
  416. }
  417. }
  418. /* Resize the fdt to its actual size + a bit of padding */
  419. int fdt_resize(void *blob)
  420. {
  421. int i;
  422. uint64_t addr, size;
  423. int total, ret;
  424. uint actualsize;
  425. if (!blob)
  426. return 0;
  427. total = fdt_num_mem_rsv(blob);
  428. for (i = 0; i < total; i++) {
  429. fdt_get_mem_rsv(blob, i, &addr, &size);
  430. if (addr == (uintptr_t)blob) {
  431. fdt_del_mem_rsv(blob, i);
  432. break;
  433. }
  434. }
  435. /*
  436. * Calculate the actual size of the fdt
  437. * plus the size needed for 5 fdt_add_mem_rsv, one
  438. * for the fdt itself and 4 for a possible initrd
  439. * ((initrd-start + initrd-end) * 2 (name & value))
  440. */
  441. actualsize = fdt_off_dt_strings(blob) +
  442. fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
  443. /* Make it so the fdt ends on a page boundary */
  444. actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
  445. actualsize = actualsize - ((uintptr_t)blob & 0xfff);
  446. /* Change the fdt header to reflect the correct size */
  447. fdt_set_totalsize(blob, actualsize);
  448. /* Add the new reservation */
  449. ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
  450. if (ret < 0)
  451. return ret;
  452. return actualsize;
  453. }
  454. #ifdef CONFIG_PCI
  455. #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
  456. #define FDT_PCI_PREFETCH (0x40000000)
  457. #define FDT_PCI_MEM32 (0x02000000)
  458. #define FDT_PCI_IO (0x01000000)
  459. #define FDT_PCI_MEM64 (0x03000000)
  460. int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
  461. int addrcell, sizecell, len, r;
  462. u32 *dma_range;
  463. /* sized based on pci addr cells, size-cells, & address-cells */
  464. u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
  465. addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
  466. sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
  467. dma_range = &dma_ranges[0];
  468. for (r = 0; r < hose->region_count; r++) {
  469. u64 bus_start, phys_start, size;
  470. /* skip if !PCI_REGION_SYS_MEMORY */
  471. if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
  472. continue;
  473. bus_start = (u64)hose->regions[r].bus_start;
  474. phys_start = (u64)hose->regions[r].phys_start;
  475. size = (u64)hose->regions[r].size;
  476. dma_range[0] = 0;
  477. if (size >= 0x100000000ull)
  478. dma_range[0] |= FDT_PCI_MEM64;
  479. else
  480. dma_range[0] |= FDT_PCI_MEM32;
  481. if (hose->regions[r].flags & PCI_REGION_PREFETCH)
  482. dma_range[0] |= FDT_PCI_PREFETCH;
  483. #ifdef CONFIG_SYS_PCI_64BIT
  484. dma_range[1] = bus_start >> 32;
  485. #else
  486. dma_range[1] = 0;
  487. #endif
  488. dma_range[2] = bus_start & 0xffffffff;
  489. if (addrcell == 2) {
  490. dma_range[3] = phys_start >> 32;
  491. dma_range[4] = phys_start & 0xffffffff;
  492. } else {
  493. dma_range[3] = phys_start & 0xffffffff;
  494. }
  495. if (sizecell == 2) {
  496. dma_range[3 + addrcell + 0] = size >> 32;
  497. dma_range[3 + addrcell + 1] = size & 0xffffffff;
  498. } else {
  499. dma_range[3 + addrcell + 0] = size & 0xffffffff;
  500. }
  501. dma_range += (3 + addrcell + sizecell);
  502. }
  503. len = dma_range - &dma_ranges[0];
  504. if (len)
  505. fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
  506. return 0;
  507. }
  508. #endif
  509. #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
  510. /*
  511. * Provide a weak default function to return the flash bank size.
  512. * There might be multiple non-identical flash chips connected to one
  513. * chip-select, so we need to pass an index as well.
  514. */
  515. u32 __flash_get_bank_size(int cs, int idx)
  516. {
  517. extern flash_info_t flash_info[];
  518. /*
  519. * As default, a simple 1:1 mapping is provided. Boards with
  520. * a different mapping need to supply a board specific mapping
  521. * routine.
  522. */
  523. return flash_info[cs].size;
  524. }
  525. u32 flash_get_bank_size(int cs, int idx)
  526. __attribute__((weak, alias("__flash_get_bank_size")));
  527. /*
  528. * This function can be used to update the size in the "reg" property
  529. * of all NOR FLASH device nodes. This is necessary for boards with
  530. * non-fixed NOR FLASH sizes.
  531. */
  532. int fdt_fixup_nor_flash_size(void *blob)
  533. {
  534. char compat[][16] = { "cfi-flash", "jedec-flash" };
  535. int off;
  536. int len;
  537. struct fdt_property *prop;
  538. u32 *reg, *reg2;
  539. int i;
  540. for (i = 0; i < 2; i++) {
  541. off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
  542. while (off != -FDT_ERR_NOTFOUND) {
  543. int idx;
  544. /*
  545. * Found one compatible node, so fixup the size
  546. * int its reg properties
  547. */
  548. prop = fdt_get_property_w(blob, off, "reg", &len);
  549. if (prop) {
  550. int tuple_size = 3 * sizeof(reg);
  551. /*
  552. * There might be multiple reg-tuples,
  553. * so loop through them all
  554. */
  555. reg = reg2 = (u32 *)&prop->data[0];
  556. for (idx = 0; idx < (len / tuple_size); idx++) {
  557. /*
  558. * Update size in reg property
  559. */
  560. reg[2] = flash_get_bank_size(reg[0],
  561. idx);
  562. /*
  563. * Point to next reg tuple
  564. */
  565. reg += 3;
  566. }
  567. fdt_setprop(blob, off, "reg", reg2, len);
  568. }
  569. /* Move to next compatible node */
  570. off = fdt_node_offset_by_compatible(blob, off,
  571. compat[i]);
  572. }
  573. }
  574. return 0;
  575. }
  576. #endif
  577. int fdt_increase_size(void *fdt, int add_len)
  578. {
  579. int newlen;
  580. newlen = fdt_totalsize(fdt) + add_len;
  581. /* Open in place with a new len */
  582. return fdt_open_into(fdt, fdt, newlen);
  583. }
  584. #ifdef CONFIG_FDT_FIXUP_PARTITIONS
  585. #include <jffs2/load_kernel.h>
  586. #include <mtd_node.h>
  587. struct reg_cell {
  588. unsigned int r0;
  589. unsigned int r1;
  590. };
  591. int fdt_del_subnodes(const void *blob, int parent_offset)
  592. {
  593. int off, ndepth;
  594. int ret;
  595. for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
  596. (off >= 0) && (ndepth > 0);
  597. off = fdt_next_node(blob, off, &ndepth)) {
  598. if (ndepth == 1) {
  599. debug("delete %s: offset: %x\n",
  600. fdt_get_name(blob, off, 0), off);
  601. ret = fdt_del_node((void *)blob, off);
  602. if (ret < 0) {
  603. printf("Can't delete node: %s\n",
  604. fdt_strerror(ret));
  605. return ret;
  606. } else {
  607. ndepth = 0;
  608. off = parent_offset;
  609. }
  610. }
  611. }
  612. return 0;
  613. }
  614. int fdt_del_partitions(void *blob, int parent_offset)
  615. {
  616. const void *prop;
  617. int ndepth = 0;
  618. int off;
  619. int ret;
  620. off = fdt_next_node(blob, parent_offset, &ndepth);
  621. if (off > 0 && ndepth == 1) {
  622. prop = fdt_getprop(blob, off, "label", NULL);
  623. if (prop == NULL) {
  624. /*
  625. * Could not find label property, nand {}; node?
  626. * Check subnode, delete partitions there if any.
  627. */
  628. return fdt_del_partitions(blob, off);
  629. } else {
  630. ret = fdt_del_subnodes(blob, parent_offset);
  631. if (ret < 0) {
  632. printf("Can't remove subnodes: %s\n",
  633. fdt_strerror(ret));
  634. return ret;
  635. }
  636. }
  637. }
  638. return 0;
  639. }
  640. int fdt_node_set_part_info(void *blob, int parent_offset,
  641. struct mtd_device *dev)
  642. {
  643. struct list_head *pentry;
  644. struct part_info *part;
  645. struct reg_cell cell;
  646. int off, ndepth = 0;
  647. int part_num, ret;
  648. char buf[64];
  649. ret = fdt_del_partitions(blob, parent_offset);
  650. if (ret < 0)
  651. return ret;
  652. /*
  653. * Check if it is nand {}; subnode, adjust
  654. * the offset in this case
  655. */
  656. off = fdt_next_node(blob, parent_offset, &ndepth);
  657. if (off > 0 && ndepth == 1)
  658. parent_offset = off;
  659. part_num = 0;
  660. list_for_each_prev(pentry, &dev->parts) {
  661. int newoff;
  662. part = list_entry(pentry, struct part_info, link);
  663. debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
  664. part_num, part->name, part->size,
  665. part->offset, part->mask_flags);
  666. sprintf(buf, "partition@%llx", part->offset);
  667. add_sub:
  668. ret = fdt_add_subnode(blob, parent_offset, buf);
  669. if (ret == -FDT_ERR_NOSPACE) {
  670. ret = fdt_increase_size(blob, 512);
  671. if (!ret)
  672. goto add_sub;
  673. else
  674. goto err_size;
  675. } else if (ret < 0) {
  676. printf("Can't add partition node: %s\n",
  677. fdt_strerror(ret));
  678. return ret;
  679. }
  680. newoff = ret;
  681. /* Check MTD_WRITEABLE_CMD flag */
  682. if (part->mask_flags & 1) {
  683. add_ro:
  684. ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
  685. if (ret == -FDT_ERR_NOSPACE) {
  686. ret = fdt_increase_size(blob, 512);
  687. if (!ret)
  688. goto add_ro;
  689. else
  690. goto err_size;
  691. } else if (ret < 0)
  692. goto err_prop;
  693. }
  694. cell.r0 = cpu_to_fdt32(part->offset);
  695. cell.r1 = cpu_to_fdt32(part->size);
  696. add_reg:
  697. ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
  698. if (ret == -FDT_ERR_NOSPACE) {
  699. ret = fdt_increase_size(blob, 512);
  700. if (!ret)
  701. goto add_reg;
  702. else
  703. goto err_size;
  704. } else if (ret < 0)
  705. goto err_prop;
  706. add_label:
  707. ret = fdt_setprop_string(blob, newoff, "label", part->name);
  708. if (ret == -FDT_ERR_NOSPACE) {
  709. ret = fdt_increase_size(blob, 512);
  710. if (!ret)
  711. goto add_label;
  712. else
  713. goto err_size;
  714. } else if (ret < 0)
  715. goto err_prop;
  716. part_num++;
  717. }
  718. return 0;
  719. err_size:
  720. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  721. return ret;
  722. err_prop:
  723. printf("Can't add property: %s\n", fdt_strerror(ret));
  724. return ret;
  725. }
  726. /*
  727. * Update partitions in nor/nand nodes using info from
  728. * mtdparts environment variable. The nodes to update are
  729. * specified by node_info structure which contains mtd device
  730. * type and compatible string: E. g. the board code in
  731. * ft_board_setup() could use:
  732. *
  733. * struct node_info nodes[] = {
  734. * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
  735. * { "cfi-flash", MTD_DEV_TYPE_NOR, },
  736. * };
  737. *
  738. * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
  739. */
  740. void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
  741. {
  742. struct node_info *ni = node_info;
  743. struct mtd_device *dev;
  744. char *parts;
  745. int i, idx;
  746. int noff;
  747. parts = getenv("mtdparts");
  748. if (!parts)
  749. return;
  750. if (mtdparts_init() != 0)
  751. return;
  752. for (i = 0; i < node_info_size; i++) {
  753. idx = 0;
  754. noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
  755. while (noff != -FDT_ERR_NOTFOUND) {
  756. debug("%s: %s, mtd dev type %d\n",
  757. fdt_get_name(blob, noff, 0),
  758. ni[i].compat, ni[i].type);
  759. dev = device_find(ni[i].type, idx++);
  760. if (dev) {
  761. if (fdt_node_set_part_info(blob, noff, dev))
  762. return; /* return on error */
  763. }
  764. /* Jump to next flash node */
  765. noff = fdt_node_offset_by_compatible(blob, noff,
  766. ni[i].compat);
  767. }
  768. }
  769. }
  770. #endif
  771. void fdt_del_node_and_alias(void *blob, const char *alias)
  772. {
  773. int off = fdt_path_offset(blob, alias);
  774. if (off < 0)
  775. return;
  776. fdt_del_node(blob, off);
  777. off = fdt_path_offset(blob, "/aliases");
  778. fdt_delprop(blob, off, alias);
  779. }
  780. /* Helper to read a big number; size is in cells (not bytes) */
  781. static inline u64 of_read_number(const fdt32_t *cell, int size)
  782. {
  783. u64 r = 0;
  784. while (size--)
  785. r = (r << 32) | fdt32_to_cpu(*(cell++));
  786. return r;
  787. }
  788. #define PRu64 "%llx"
  789. /* Max address size we deal with */
  790. #define OF_MAX_ADDR_CELLS 4
  791. #define OF_BAD_ADDR ((u64)-1)
  792. #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
  793. (ns) > 0)
  794. /* Debug utility */
  795. #ifdef DEBUG
  796. static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
  797. {
  798. printf("%s", s);
  799. while(na--)
  800. printf(" %08x", *(addr++));
  801. printf("\n");
  802. }
  803. #else
  804. static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
  805. #endif
  806. /* Callbacks for bus specific translators */
  807. struct of_bus {
  808. const char *name;
  809. const char *addresses;
  810. void (*count_cells)(void *blob, int parentoffset,
  811. int *addrc, int *sizec);
  812. u64 (*map)(fdt32_t *addr, const fdt32_t *range,
  813. int na, int ns, int pna);
  814. int (*translate)(fdt32_t *addr, u64 offset, int na);
  815. };
  816. /* Default translator (generic bus) */
  817. static void of_bus_default_count_cells(void *blob, int parentoffset,
  818. int *addrc, int *sizec)
  819. {
  820. const fdt32_t *prop;
  821. if (addrc) {
  822. prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
  823. if (prop)
  824. *addrc = be32_to_cpup(prop);
  825. else
  826. *addrc = 2;
  827. }
  828. if (sizec) {
  829. prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
  830. if (prop)
  831. *sizec = be32_to_cpup(prop);
  832. else
  833. *sizec = 1;
  834. }
  835. }
  836. static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
  837. int na, int ns, int pna)
  838. {
  839. u64 cp, s, da;
  840. cp = of_read_number(range, na);
  841. s = of_read_number(range + na + pna, ns);
  842. da = of_read_number(addr, na);
  843. debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
  844. cp, s, da);
  845. if (da < cp || da >= (cp + s))
  846. return OF_BAD_ADDR;
  847. return da - cp;
  848. }
  849. static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
  850. {
  851. u64 a = of_read_number(addr, na);
  852. memset(addr, 0, na * 4);
  853. a += offset;
  854. if (na > 1)
  855. addr[na - 2] = cpu_to_fdt32(a >> 32);
  856. addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
  857. return 0;
  858. }
  859. /* Array of bus specific translators */
  860. static struct of_bus of_busses[] = {
  861. /* Default */
  862. {
  863. .name = "default",
  864. .addresses = "reg",
  865. .count_cells = of_bus_default_count_cells,
  866. .map = of_bus_default_map,
  867. .translate = of_bus_default_translate,
  868. },
  869. };
  870. static int of_translate_one(void * blob, int parent, struct of_bus *bus,
  871. struct of_bus *pbus, fdt32_t *addr,
  872. int na, int ns, int pna, const char *rprop)
  873. {
  874. const fdt32_t *ranges;
  875. int rlen;
  876. int rone;
  877. u64 offset = OF_BAD_ADDR;
  878. /* Normally, an absence of a "ranges" property means we are
  879. * crossing a non-translatable boundary, and thus the addresses
  880. * below the current not cannot be converted to CPU physical ones.
  881. * Unfortunately, while this is very clear in the spec, it's not
  882. * what Apple understood, and they do have things like /uni-n or
  883. * /ht nodes with no "ranges" property and a lot of perfectly
  884. * useable mapped devices below them. Thus we treat the absence of
  885. * "ranges" as equivalent to an empty "ranges" property which means
  886. * a 1:1 translation at that level. It's up to the caller not to try
  887. * to translate addresses that aren't supposed to be translated in
  888. * the first place. --BenH.
  889. */
  890. ranges = fdt_getprop(blob, parent, rprop, &rlen);
  891. if (ranges == NULL || rlen == 0) {
  892. offset = of_read_number(addr, na);
  893. memset(addr, 0, pna * 4);
  894. debug("OF: no ranges, 1:1 translation\n");
  895. goto finish;
  896. }
  897. debug("OF: walking ranges...\n");
  898. /* Now walk through the ranges */
  899. rlen /= 4;
  900. rone = na + pna + ns;
  901. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  902. offset = bus->map(addr, ranges, na, ns, pna);
  903. if (offset != OF_BAD_ADDR)
  904. break;
  905. }
  906. if (offset == OF_BAD_ADDR) {
  907. debug("OF: not found !\n");
  908. return 1;
  909. }
  910. memcpy(addr, ranges + na, 4 * pna);
  911. finish:
  912. of_dump_addr("OF: parent translation for:", addr, pna);
  913. debug("OF: with offset: "PRu64"\n", offset);
  914. /* Translate it into parent bus space */
  915. return pbus->translate(addr, offset, pna);
  916. }
  917. /*
  918. * Translate an address from the device-tree into a CPU physical address,
  919. * this walks up the tree and applies the various bus mappings on the
  920. * way.
  921. *
  922. * Note: We consider that crossing any level with #size-cells == 0 to mean
  923. * that translation is impossible (that is we are not dealing with a value
  924. * that can be mapped to a cpu physical address). This is not really specified
  925. * that way, but this is traditionally the way IBM at least do things
  926. */
  927. static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr,
  928. const char *rprop)
  929. {
  930. int parent;
  931. struct of_bus *bus, *pbus;
  932. fdt32_t addr[OF_MAX_ADDR_CELLS];
  933. int na, ns, pna, pns;
  934. u64 result = OF_BAD_ADDR;
  935. debug("OF: ** translation for device %s **\n",
  936. fdt_get_name(blob, node_offset, NULL));
  937. /* Get parent & match bus type */
  938. parent = fdt_parent_offset(blob, node_offset);
  939. if (parent < 0)
  940. goto bail;
  941. bus = &of_busses[0];
  942. /* Cound address cells & copy address locally */
  943. bus->count_cells(blob, parent, &na, &ns);
  944. if (!OF_CHECK_COUNTS(na, ns)) {
  945. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  946. fdt_get_name(blob, node_offset, NULL));
  947. goto bail;
  948. }
  949. memcpy(addr, in_addr, na * 4);
  950. debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  951. bus->name, na, ns, fdt_get_name(blob, parent, NULL));
  952. of_dump_addr("OF: translating address:", addr, na);
  953. /* Translate */
  954. for (;;) {
  955. /* Switch to parent bus */
  956. node_offset = parent;
  957. parent = fdt_parent_offset(blob, node_offset);
  958. /* If root, we have finished */
  959. if (parent < 0) {
  960. debug("OF: reached root node\n");
  961. result = of_read_number(addr, na);
  962. break;
  963. }
  964. /* Get new parent bus and counts */
  965. pbus = &of_busses[0];
  966. pbus->count_cells(blob, parent, &pna, &pns);
  967. if (!OF_CHECK_COUNTS(pna, pns)) {
  968. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  969. fdt_get_name(blob, node_offset, NULL));
  970. break;
  971. }
  972. debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  973. pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
  974. /* Apply bus translation */
  975. if (of_translate_one(blob, node_offset, bus, pbus,
  976. addr, na, ns, pna, rprop))
  977. break;
  978. /* Complete the move up one level */
  979. na = pna;
  980. ns = pns;
  981. bus = pbus;
  982. of_dump_addr("OF: one level translation:", addr, na);
  983. }
  984. bail:
  985. return result;
  986. }
  987. u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr)
  988. {
  989. return __of_translate_address(blob, node_offset, in_addr, "ranges");
  990. }
  991. /**
  992. * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
  993. * who's reg property matches a physical cpu address
  994. *
  995. * @blob: ptr to device tree
  996. * @compat: compatiable string to match
  997. * @compat_off: property name
  998. *
  999. */
  1000. int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
  1001. phys_addr_t compat_off)
  1002. {
  1003. int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
  1004. while (off != -FDT_ERR_NOTFOUND) {
  1005. const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
  1006. if (reg) {
  1007. if (compat_off == fdt_translate_address(blob, off, reg))
  1008. return off;
  1009. }
  1010. off = fdt_node_offset_by_compatible(blob, off, compat);
  1011. }
  1012. return -FDT_ERR_NOTFOUND;
  1013. }
  1014. /**
  1015. * fdt_alloc_phandle: Return next free phandle value
  1016. *
  1017. * @blob: ptr to device tree
  1018. */
  1019. int fdt_alloc_phandle(void *blob)
  1020. {
  1021. int offset, phandle = 0;
  1022. for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
  1023. offset = fdt_next_node(blob, offset, NULL)) {
  1024. phandle = max(phandle, fdt_get_phandle(blob, offset));
  1025. }
  1026. return phandle + 1;
  1027. }
  1028. /*
  1029. * fdt_set_phandle: Create a phandle property for the given node
  1030. *
  1031. * @fdt: ptr to device tree
  1032. * @nodeoffset: node to update
  1033. * @phandle: phandle value to set (must be unique)
  1034. */
  1035. int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
  1036. {
  1037. int ret;
  1038. #ifdef DEBUG
  1039. int off = fdt_node_offset_by_phandle(fdt, phandle);
  1040. if ((off >= 0) && (off != nodeoffset)) {
  1041. char buf[64];
  1042. fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
  1043. printf("Trying to update node %s with phandle %u ",
  1044. buf, phandle);
  1045. fdt_get_path(fdt, off, buf, sizeof(buf));
  1046. printf("that already exists in node %s.\n", buf);
  1047. return -FDT_ERR_BADPHANDLE;
  1048. }
  1049. #endif
  1050. ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
  1051. if (ret < 0)
  1052. return ret;
  1053. /*
  1054. * For now, also set the deprecated "linux,phandle" property, so that we
  1055. * don't break older kernels.
  1056. */
  1057. ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
  1058. return ret;
  1059. }
  1060. /*
  1061. * fdt_create_phandle: Create a phandle property for the given node
  1062. *
  1063. * @fdt: ptr to device tree
  1064. * @nodeoffset: node to update
  1065. */
  1066. unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
  1067. {
  1068. /* see if there is a phandle already */
  1069. int phandle = fdt_get_phandle(fdt, nodeoffset);
  1070. /* if we got 0, means no phandle so create one */
  1071. if (phandle == 0) {
  1072. int ret;
  1073. phandle = fdt_alloc_phandle(fdt);
  1074. ret = fdt_set_phandle(fdt, nodeoffset, phandle);
  1075. if (ret < 0) {
  1076. printf("Can't set phandle %u: %s\n", phandle,
  1077. fdt_strerror(ret));
  1078. return 0;
  1079. }
  1080. }
  1081. return phandle;
  1082. }
  1083. /*
  1084. * fdt_set_node_status: Set status for the given node
  1085. *
  1086. * @fdt: ptr to device tree
  1087. * @nodeoffset: node to update
  1088. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
  1089. * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
  1090. * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
  1091. */
  1092. int fdt_set_node_status(void *fdt, int nodeoffset,
  1093. enum fdt_status status, unsigned int error_code)
  1094. {
  1095. char buf[16];
  1096. int ret = 0;
  1097. if (nodeoffset < 0)
  1098. return nodeoffset;
  1099. switch (status) {
  1100. case FDT_STATUS_OKAY:
  1101. ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
  1102. break;
  1103. case FDT_STATUS_DISABLED:
  1104. ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
  1105. break;
  1106. case FDT_STATUS_FAIL:
  1107. ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
  1108. break;
  1109. case FDT_STATUS_FAIL_ERROR_CODE:
  1110. sprintf(buf, "fail-%d", error_code);
  1111. ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
  1112. break;
  1113. default:
  1114. printf("Invalid fdt status: %x\n", status);
  1115. ret = -1;
  1116. break;
  1117. }
  1118. return ret;
  1119. }
  1120. /*
  1121. * fdt_set_status_by_alias: Set status for the given node given an alias
  1122. *
  1123. * @fdt: ptr to device tree
  1124. * @alias: alias of node to update
  1125. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
  1126. * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
  1127. * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
  1128. */
  1129. int fdt_set_status_by_alias(void *fdt, const char* alias,
  1130. enum fdt_status status, unsigned int error_code)
  1131. {
  1132. int offset = fdt_path_offset(fdt, alias);
  1133. return fdt_set_node_status(fdt, offset, status, error_code);
  1134. }
  1135. #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
  1136. int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
  1137. {
  1138. int noff;
  1139. int ret;
  1140. noff = fdt_node_offset_by_compatible(blob, -1, compat);
  1141. if (noff != -FDT_ERR_NOTFOUND) {
  1142. debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
  1143. add_edid:
  1144. ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
  1145. if (ret == -FDT_ERR_NOSPACE) {
  1146. ret = fdt_increase_size(blob, 512);
  1147. if (!ret)
  1148. goto add_edid;
  1149. else
  1150. goto err_size;
  1151. } else if (ret < 0) {
  1152. printf("Can't add property: %s\n", fdt_strerror(ret));
  1153. return ret;
  1154. }
  1155. }
  1156. return 0;
  1157. err_size:
  1158. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  1159. return ret;
  1160. }
  1161. #endif
  1162. /*
  1163. * Verify the physical address of device tree node for a given alias
  1164. *
  1165. * This function locates the device tree node of a given alias, and then
  1166. * verifies that the physical address of that device matches the given
  1167. * parameter. It displays a message if there is a mismatch.
  1168. *
  1169. * Returns 1 on success, 0 on failure
  1170. */
  1171. int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
  1172. {
  1173. const char *path;
  1174. const fdt32_t *reg;
  1175. int node, len;
  1176. u64 dt_addr;
  1177. path = fdt_getprop(fdt, anode, alias, NULL);
  1178. if (!path) {
  1179. /* If there's no such alias, then it's not a failure */
  1180. return 1;
  1181. }
  1182. node = fdt_path_offset(fdt, path);
  1183. if (node < 0) {
  1184. printf("Warning: device tree alias '%s' points to invalid "
  1185. "node %s.\n", alias, path);
  1186. return 0;
  1187. }
  1188. reg = fdt_getprop(fdt, node, "reg", &len);
  1189. if (!reg) {
  1190. printf("Warning: device tree node '%s' has no address.\n",
  1191. path);
  1192. return 0;
  1193. }
  1194. dt_addr = fdt_translate_address(fdt, node, reg);
  1195. if (addr != dt_addr) {
  1196. printf("Warning: U-Boot configured device %s at address %llx,\n"
  1197. " but the device tree has it address %llx.\n",
  1198. alias, addr, dt_addr);
  1199. return 0;
  1200. }
  1201. return 1;
  1202. }
  1203. /*
  1204. * Returns the base address of an SOC or PCI node
  1205. */
  1206. u64 fdt_get_base_address(void *fdt, int node)
  1207. {
  1208. int size;
  1209. u32 naddr;
  1210. const fdt32_t *prop;
  1211. prop = fdt_getprop(fdt, node, "#address-cells", &size);
  1212. if (prop && size == 4)
  1213. naddr = be32_to_cpup(prop);
  1214. else
  1215. naddr = 2;
  1216. prop = fdt_getprop(fdt, node, "ranges", &size);
  1217. return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
  1218. }