fdt_support.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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, addr_cell_len);
  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, addr_cell_len);
  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. }
  361. err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
  362. sizeof("memory"));
  363. if (err < 0) {
  364. printf("WARNING: could not set %s %s.\n", "device_type",
  365. fdt_strerror(err));
  366. return err;
  367. }
  368. addr_cell_len = get_cells_len(blob, "#address-cells");
  369. size_cell_len = get_cells_len(blob, "#size-cells");
  370. for (bank = 0, len = 0; bank < banks; bank++) {
  371. write_cell(tmp + len, start[bank], addr_cell_len);
  372. len += addr_cell_len;
  373. write_cell(tmp + len, size[bank], size_cell_len);
  374. len += size_cell_len;
  375. }
  376. err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
  377. if (err < 0) {
  378. printf("WARNING: could not set %s %s.\n",
  379. "reg", fdt_strerror(err));
  380. return err;
  381. }
  382. return 0;
  383. }
  384. int fdt_fixup_memory(void *blob, u64 start, u64 size)
  385. {
  386. return fdt_fixup_memory_banks(blob, &start, &size, 1);
  387. }
  388. void fdt_fixup_ethernet(void *fdt)
  389. {
  390. int node, i, j;
  391. char enet[16], *tmp, *end;
  392. char mac[16];
  393. const char *path;
  394. unsigned char mac_addr[6];
  395. node = fdt_path_offset(fdt, "/aliases");
  396. if (node < 0)
  397. return;
  398. i = 0;
  399. strcpy(mac, "ethaddr");
  400. while ((tmp = getenv(mac)) != NULL) {
  401. sprintf(enet, "ethernet%d", i);
  402. path = fdt_getprop(fdt, node, enet, NULL);
  403. if (!path) {
  404. debug("No alias for %s\n", enet);
  405. sprintf(mac, "eth%daddr", ++i);
  406. continue;
  407. }
  408. for (j = 0; j < 6; j++) {
  409. mac_addr[j] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  410. if (tmp)
  411. tmp = (*end) ? end+1 : end;
  412. }
  413. do_fixup_by_path(fdt, path, "mac-address", &mac_addr, 6, 0);
  414. do_fixup_by_path(fdt, path, "local-mac-address",
  415. &mac_addr, 6, 1);
  416. sprintf(mac, "eth%daddr", ++i);
  417. }
  418. }
  419. /* Resize the fdt to its actual size + a bit of padding */
  420. int fdt_resize(void *blob)
  421. {
  422. int i;
  423. uint64_t addr, size;
  424. int total, ret;
  425. uint actualsize;
  426. if (!blob)
  427. return 0;
  428. total = fdt_num_mem_rsv(blob);
  429. for (i = 0; i < total; i++) {
  430. fdt_get_mem_rsv(blob, i, &addr, &size);
  431. if (addr == (uintptr_t)blob) {
  432. fdt_del_mem_rsv(blob, i);
  433. break;
  434. }
  435. }
  436. /*
  437. * Calculate the actual size of the fdt
  438. * plus the size needed for 5 fdt_add_mem_rsv, one
  439. * for the fdt itself and 4 for a possible initrd
  440. * ((initrd-start + initrd-end) * 2 (name & value))
  441. */
  442. actualsize = fdt_off_dt_strings(blob) +
  443. fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
  444. /* Make it so the fdt ends on a page boundary */
  445. actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
  446. actualsize = actualsize - ((uintptr_t)blob & 0xfff);
  447. /* Change the fdt header to reflect the correct size */
  448. fdt_set_totalsize(blob, actualsize);
  449. /* Add the new reservation */
  450. ret = fdt_add_mem_rsv(blob, (uintptr_t)blob, actualsize);
  451. if (ret < 0)
  452. return ret;
  453. return actualsize;
  454. }
  455. #ifdef CONFIG_PCI
  456. #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
  457. #define FDT_PCI_PREFETCH (0x40000000)
  458. #define FDT_PCI_MEM32 (0x02000000)
  459. #define FDT_PCI_IO (0x01000000)
  460. #define FDT_PCI_MEM64 (0x03000000)
  461. int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
  462. int addrcell, sizecell, len, r;
  463. u32 *dma_range;
  464. /* sized based on pci addr cells, size-cells, & address-cells */
  465. u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
  466. addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
  467. sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
  468. dma_range = &dma_ranges[0];
  469. for (r = 0; r < hose->region_count; r++) {
  470. u64 bus_start, phys_start, size;
  471. /* skip if !PCI_REGION_SYS_MEMORY */
  472. if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
  473. continue;
  474. bus_start = (u64)hose->regions[r].bus_start;
  475. phys_start = (u64)hose->regions[r].phys_start;
  476. size = (u64)hose->regions[r].size;
  477. dma_range[0] = 0;
  478. if (size >= 0x100000000ull)
  479. dma_range[0] |= FDT_PCI_MEM64;
  480. else
  481. dma_range[0] |= FDT_PCI_MEM32;
  482. if (hose->regions[r].flags & PCI_REGION_PREFETCH)
  483. dma_range[0] |= FDT_PCI_PREFETCH;
  484. #ifdef CONFIG_SYS_PCI_64BIT
  485. dma_range[1] = bus_start >> 32;
  486. #else
  487. dma_range[1] = 0;
  488. #endif
  489. dma_range[2] = bus_start & 0xffffffff;
  490. if (addrcell == 2) {
  491. dma_range[3] = phys_start >> 32;
  492. dma_range[4] = phys_start & 0xffffffff;
  493. } else {
  494. dma_range[3] = phys_start & 0xffffffff;
  495. }
  496. if (sizecell == 2) {
  497. dma_range[3 + addrcell + 0] = size >> 32;
  498. dma_range[3 + addrcell + 1] = size & 0xffffffff;
  499. } else {
  500. dma_range[3 + addrcell + 0] = size & 0xffffffff;
  501. }
  502. dma_range += (3 + addrcell + sizecell);
  503. }
  504. len = dma_range - &dma_ranges[0];
  505. if (len)
  506. fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
  507. return 0;
  508. }
  509. #endif
  510. #ifdef CONFIG_FDT_FIXUP_NOR_FLASH_SIZE
  511. /*
  512. * Provide a weak default function to return the flash bank size.
  513. * There might be multiple non-identical flash chips connected to one
  514. * chip-select, so we need to pass an index as well.
  515. */
  516. u32 __flash_get_bank_size(int cs, int idx)
  517. {
  518. extern flash_info_t flash_info[];
  519. /*
  520. * As default, a simple 1:1 mapping is provided. Boards with
  521. * a different mapping need to supply a board specific mapping
  522. * routine.
  523. */
  524. return flash_info[cs].size;
  525. }
  526. u32 flash_get_bank_size(int cs, int idx)
  527. __attribute__((weak, alias("__flash_get_bank_size")));
  528. /*
  529. * This function can be used to update the size in the "reg" property
  530. * of all NOR FLASH device nodes. This is necessary for boards with
  531. * non-fixed NOR FLASH sizes.
  532. */
  533. int fdt_fixup_nor_flash_size(void *blob)
  534. {
  535. char compat[][16] = { "cfi-flash", "jedec-flash" };
  536. int off;
  537. int len;
  538. struct fdt_property *prop;
  539. u32 *reg, *reg2;
  540. int i;
  541. for (i = 0; i < 2; i++) {
  542. off = fdt_node_offset_by_compatible(blob, -1, compat[i]);
  543. while (off != -FDT_ERR_NOTFOUND) {
  544. int idx;
  545. /*
  546. * Found one compatible node, so fixup the size
  547. * int its reg properties
  548. */
  549. prop = fdt_get_property_w(blob, off, "reg", &len);
  550. if (prop) {
  551. int tuple_size = 3 * sizeof(reg);
  552. /*
  553. * There might be multiple reg-tuples,
  554. * so loop through them all
  555. */
  556. reg = reg2 = (u32 *)&prop->data[0];
  557. for (idx = 0; idx < (len / tuple_size); idx++) {
  558. /*
  559. * Update size in reg property
  560. */
  561. reg[2] = flash_get_bank_size(reg[0],
  562. idx);
  563. /*
  564. * Point to next reg tuple
  565. */
  566. reg += 3;
  567. }
  568. fdt_setprop(blob, off, "reg", reg2, len);
  569. }
  570. /* Move to next compatible node */
  571. off = fdt_node_offset_by_compatible(blob, off,
  572. compat[i]);
  573. }
  574. }
  575. return 0;
  576. }
  577. #endif
  578. int fdt_increase_size(void *fdt, int add_len)
  579. {
  580. int newlen;
  581. newlen = fdt_totalsize(fdt) + add_len;
  582. /* Open in place with a new len */
  583. return fdt_open_into(fdt, fdt, newlen);
  584. }
  585. #ifdef CONFIG_FDT_FIXUP_PARTITIONS
  586. #include <jffs2/load_kernel.h>
  587. #include <mtd_node.h>
  588. struct reg_cell {
  589. unsigned int r0;
  590. unsigned int r1;
  591. };
  592. int fdt_del_subnodes(const void *blob, int parent_offset)
  593. {
  594. int off, ndepth;
  595. int ret;
  596. for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
  597. (off >= 0) && (ndepth > 0);
  598. off = fdt_next_node(blob, off, &ndepth)) {
  599. if (ndepth == 1) {
  600. debug("delete %s: offset: %x\n",
  601. fdt_get_name(blob, off, 0), off);
  602. ret = fdt_del_node((void *)blob, off);
  603. if (ret < 0) {
  604. printf("Can't delete node: %s\n",
  605. fdt_strerror(ret));
  606. return ret;
  607. } else {
  608. ndepth = 0;
  609. off = parent_offset;
  610. }
  611. }
  612. }
  613. return 0;
  614. }
  615. int fdt_del_partitions(void *blob, int parent_offset)
  616. {
  617. const void *prop;
  618. int ndepth = 0;
  619. int off;
  620. int ret;
  621. off = fdt_next_node(blob, parent_offset, &ndepth);
  622. if (off > 0 && ndepth == 1) {
  623. prop = fdt_getprop(blob, off, "label", NULL);
  624. if (prop == NULL) {
  625. /*
  626. * Could not find label property, nand {}; node?
  627. * Check subnode, delete partitions there if any.
  628. */
  629. return fdt_del_partitions(blob, off);
  630. } else {
  631. ret = fdt_del_subnodes(blob, parent_offset);
  632. if (ret < 0) {
  633. printf("Can't remove subnodes: %s\n",
  634. fdt_strerror(ret));
  635. return ret;
  636. }
  637. }
  638. }
  639. return 0;
  640. }
  641. int fdt_node_set_part_info(void *blob, int parent_offset,
  642. struct mtd_device *dev)
  643. {
  644. struct list_head *pentry;
  645. struct part_info *part;
  646. struct reg_cell cell;
  647. int off, ndepth = 0;
  648. int part_num, ret;
  649. char buf[64];
  650. ret = fdt_del_partitions(blob, parent_offset);
  651. if (ret < 0)
  652. return ret;
  653. /*
  654. * Check if it is nand {}; subnode, adjust
  655. * the offset in this case
  656. */
  657. off = fdt_next_node(blob, parent_offset, &ndepth);
  658. if (off > 0 && ndepth == 1)
  659. parent_offset = off;
  660. part_num = 0;
  661. list_for_each_prev(pentry, &dev->parts) {
  662. int newoff;
  663. part = list_entry(pentry, struct part_info, link);
  664. debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
  665. part_num, part->name, part->size,
  666. part->offset, part->mask_flags);
  667. sprintf(buf, "partition@%llx", part->offset);
  668. add_sub:
  669. ret = fdt_add_subnode(blob, parent_offset, buf);
  670. if (ret == -FDT_ERR_NOSPACE) {
  671. ret = fdt_increase_size(blob, 512);
  672. if (!ret)
  673. goto add_sub;
  674. else
  675. goto err_size;
  676. } else if (ret < 0) {
  677. printf("Can't add partition node: %s\n",
  678. fdt_strerror(ret));
  679. return ret;
  680. }
  681. newoff = ret;
  682. /* Check MTD_WRITEABLE_CMD flag */
  683. if (part->mask_flags & 1) {
  684. add_ro:
  685. ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
  686. if (ret == -FDT_ERR_NOSPACE) {
  687. ret = fdt_increase_size(blob, 512);
  688. if (!ret)
  689. goto add_ro;
  690. else
  691. goto err_size;
  692. } else if (ret < 0)
  693. goto err_prop;
  694. }
  695. cell.r0 = cpu_to_fdt32(part->offset);
  696. cell.r1 = cpu_to_fdt32(part->size);
  697. add_reg:
  698. ret = fdt_setprop(blob, newoff, "reg", &cell, sizeof(cell));
  699. if (ret == -FDT_ERR_NOSPACE) {
  700. ret = fdt_increase_size(blob, 512);
  701. if (!ret)
  702. goto add_reg;
  703. else
  704. goto err_size;
  705. } else if (ret < 0)
  706. goto err_prop;
  707. add_label:
  708. ret = fdt_setprop_string(blob, newoff, "label", part->name);
  709. if (ret == -FDT_ERR_NOSPACE) {
  710. ret = fdt_increase_size(blob, 512);
  711. if (!ret)
  712. goto add_label;
  713. else
  714. goto err_size;
  715. } else if (ret < 0)
  716. goto err_prop;
  717. part_num++;
  718. }
  719. return 0;
  720. err_size:
  721. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  722. return ret;
  723. err_prop:
  724. printf("Can't add property: %s\n", fdt_strerror(ret));
  725. return ret;
  726. }
  727. /*
  728. * Update partitions in nor/nand nodes using info from
  729. * mtdparts environment variable. The nodes to update are
  730. * specified by node_info structure which contains mtd device
  731. * type and compatible string: E. g. the board code in
  732. * ft_board_setup() could use:
  733. *
  734. * struct node_info nodes[] = {
  735. * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
  736. * { "cfi-flash", MTD_DEV_TYPE_NOR, },
  737. * };
  738. *
  739. * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
  740. */
  741. void fdt_fixup_mtdparts(void *blob, void *node_info, int node_info_size)
  742. {
  743. struct node_info *ni = node_info;
  744. struct mtd_device *dev;
  745. char *parts;
  746. int i, idx;
  747. int noff;
  748. parts = getenv("mtdparts");
  749. if (!parts)
  750. return;
  751. if (mtdparts_init() != 0)
  752. return;
  753. for (i = 0; i < node_info_size; i++) {
  754. idx = 0;
  755. noff = fdt_node_offset_by_compatible(blob, -1, ni[i].compat);
  756. while (noff != -FDT_ERR_NOTFOUND) {
  757. debug("%s: %s, mtd dev type %d\n",
  758. fdt_get_name(blob, noff, 0),
  759. ni[i].compat, ni[i].type);
  760. dev = device_find(ni[i].type, idx++);
  761. if (dev) {
  762. if (fdt_node_set_part_info(blob, noff, dev))
  763. return; /* return on error */
  764. }
  765. /* Jump to next flash node */
  766. noff = fdt_node_offset_by_compatible(blob, noff,
  767. ni[i].compat);
  768. }
  769. }
  770. }
  771. #endif
  772. void fdt_del_node_and_alias(void *blob, const char *alias)
  773. {
  774. int off = fdt_path_offset(blob, alias);
  775. if (off < 0)
  776. return;
  777. fdt_del_node(blob, off);
  778. off = fdt_path_offset(blob, "/aliases");
  779. fdt_delprop(blob, off, alias);
  780. }
  781. /* Helper to read a big number; size is in cells (not bytes) */
  782. static inline u64 of_read_number(const fdt32_t *cell, int size)
  783. {
  784. u64 r = 0;
  785. while (size--)
  786. r = (r << 32) | fdt32_to_cpu(*(cell++));
  787. return r;
  788. }
  789. #define PRu64 "%llx"
  790. /* Max address size we deal with */
  791. #define OF_MAX_ADDR_CELLS 4
  792. #define OF_BAD_ADDR ((u64)-1)
  793. #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
  794. (ns) > 0)
  795. /* Debug utility */
  796. #ifdef DEBUG
  797. static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
  798. {
  799. printf("%s", s);
  800. while(na--)
  801. printf(" %08x", *(addr++));
  802. printf("\n");
  803. }
  804. #else
  805. static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
  806. #endif
  807. /* Callbacks for bus specific translators */
  808. struct of_bus {
  809. const char *name;
  810. const char *addresses;
  811. void (*count_cells)(void *blob, int parentoffset,
  812. int *addrc, int *sizec);
  813. u64 (*map)(fdt32_t *addr, const fdt32_t *range,
  814. int na, int ns, int pna);
  815. int (*translate)(fdt32_t *addr, u64 offset, int na);
  816. };
  817. /* Default translator (generic bus) */
  818. static void of_bus_default_count_cells(void *blob, int parentoffset,
  819. int *addrc, int *sizec)
  820. {
  821. const fdt32_t *prop;
  822. if (addrc) {
  823. prop = fdt_getprop(blob, parentoffset, "#address-cells", NULL);
  824. if (prop)
  825. *addrc = be32_to_cpup(prop);
  826. else
  827. *addrc = 2;
  828. }
  829. if (sizec) {
  830. prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
  831. if (prop)
  832. *sizec = be32_to_cpup(prop);
  833. else
  834. *sizec = 1;
  835. }
  836. }
  837. static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
  838. int na, int ns, int pna)
  839. {
  840. u64 cp, s, da;
  841. cp = of_read_number(range, na);
  842. s = of_read_number(range + na + pna, ns);
  843. da = of_read_number(addr, na);
  844. debug("OF: default map, cp="PRu64", s="PRu64", da="PRu64"\n",
  845. cp, s, da);
  846. if (da < cp || da >= (cp + s))
  847. return OF_BAD_ADDR;
  848. return da - cp;
  849. }
  850. static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
  851. {
  852. u64 a = of_read_number(addr, na);
  853. memset(addr, 0, na * 4);
  854. a += offset;
  855. if (na > 1)
  856. addr[na - 2] = cpu_to_fdt32(a >> 32);
  857. addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
  858. return 0;
  859. }
  860. /* Array of bus specific translators */
  861. static struct of_bus of_busses[] = {
  862. /* Default */
  863. {
  864. .name = "default",
  865. .addresses = "reg",
  866. .count_cells = of_bus_default_count_cells,
  867. .map = of_bus_default_map,
  868. .translate = of_bus_default_translate,
  869. },
  870. };
  871. static int of_translate_one(void * blob, int parent, struct of_bus *bus,
  872. struct of_bus *pbus, fdt32_t *addr,
  873. int na, int ns, int pna, const char *rprop)
  874. {
  875. const fdt32_t *ranges;
  876. int rlen;
  877. int rone;
  878. u64 offset = OF_BAD_ADDR;
  879. /* Normally, an absence of a "ranges" property means we are
  880. * crossing a non-translatable boundary, and thus the addresses
  881. * below the current not cannot be converted to CPU physical ones.
  882. * Unfortunately, while this is very clear in the spec, it's not
  883. * what Apple understood, and they do have things like /uni-n or
  884. * /ht nodes with no "ranges" property and a lot of perfectly
  885. * useable mapped devices below them. Thus we treat the absence of
  886. * "ranges" as equivalent to an empty "ranges" property which means
  887. * a 1:1 translation at that level. It's up to the caller not to try
  888. * to translate addresses that aren't supposed to be translated in
  889. * the first place. --BenH.
  890. */
  891. ranges = fdt_getprop(blob, parent, rprop, &rlen);
  892. if (ranges == NULL || rlen == 0) {
  893. offset = of_read_number(addr, na);
  894. memset(addr, 0, pna * 4);
  895. debug("OF: no ranges, 1:1 translation\n");
  896. goto finish;
  897. }
  898. debug("OF: walking ranges...\n");
  899. /* Now walk through the ranges */
  900. rlen /= 4;
  901. rone = na + pna + ns;
  902. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  903. offset = bus->map(addr, ranges, na, ns, pna);
  904. if (offset != OF_BAD_ADDR)
  905. break;
  906. }
  907. if (offset == OF_BAD_ADDR) {
  908. debug("OF: not found !\n");
  909. return 1;
  910. }
  911. memcpy(addr, ranges + na, 4 * pna);
  912. finish:
  913. of_dump_addr("OF: parent translation for:", addr, pna);
  914. debug("OF: with offset: "PRu64"\n", offset);
  915. /* Translate it into parent bus space */
  916. return pbus->translate(addr, offset, pna);
  917. }
  918. /*
  919. * Translate an address from the device-tree into a CPU physical address,
  920. * this walks up the tree and applies the various bus mappings on the
  921. * way.
  922. *
  923. * Note: We consider that crossing any level with #size-cells == 0 to mean
  924. * that translation is impossible (that is we are not dealing with a value
  925. * that can be mapped to a cpu physical address). This is not really specified
  926. * that way, but this is traditionally the way IBM at least do things
  927. */
  928. static u64 __of_translate_address(void *blob, int node_offset, const fdt32_t *in_addr,
  929. const char *rprop)
  930. {
  931. int parent;
  932. struct of_bus *bus, *pbus;
  933. fdt32_t addr[OF_MAX_ADDR_CELLS];
  934. int na, ns, pna, pns;
  935. u64 result = OF_BAD_ADDR;
  936. debug("OF: ** translation for device %s **\n",
  937. fdt_get_name(blob, node_offset, NULL));
  938. /* Get parent & match bus type */
  939. parent = fdt_parent_offset(blob, node_offset);
  940. if (parent < 0)
  941. goto bail;
  942. bus = &of_busses[0];
  943. /* Cound address cells & copy address locally */
  944. bus->count_cells(blob, parent, &na, &ns);
  945. if (!OF_CHECK_COUNTS(na, ns)) {
  946. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  947. fdt_get_name(blob, node_offset, NULL));
  948. goto bail;
  949. }
  950. memcpy(addr, in_addr, na * 4);
  951. debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  952. bus->name, na, ns, fdt_get_name(blob, parent, NULL));
  953. of_dump_addr("OF: translating address:", addr, na);
  954. /* Translate */
  955. for (;;) {
  956. /* Switch to parent bus */
  957. node_offset = parent;
  958. parent = fdt_parent_offset(blob, node_offset);
  959. /* If root, we have finished */
  960. if (parent < 0) {
  961. debug("OF: reached root node\n");
  962. result = of_read_number(addr, na);
  963. break;
  964. }
  965. /* Get new parent bus and counts */
  966. pbus = &of_busses[0];
  967. pbus->count_cells(blob, parent, &pna, &pns);
  968. if (!OF_CHECK_COUNTS(pna, pns)) {
  969. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  970. fdt_get_name(blob, node_offset, NULL));
  971. break;
  972. }
  973. debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  974. pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
  975. /* Apply bus translation */
  976. if (of_translate_one(blob, node_offset, bus, pbus,
  977. addr, na, ns, pna, rprop))
  978. break;
  979. /* Complete the move up one level */
  980. na = pna;
  981. ns = pns;
  982. bus = pbus;
  983. of_dump_addr("OF: one level translation:", addr, na);
  984. }
  985. bail:
  986. return result;
  987. }
  988. u64 fdt_translate_address(void *blob, int node_offset, const fdt32_t *in_addr)
  989. {
  990. return __of_translate_address(blob, node_offset, in_addr, "ranges");
  991. }
  992. /**
  993. * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
  994. * who's reg property matches a physical cpu address
  995. *
  996. * @blob: ptr to device tree
  997. * @compat: compatiable string to match
  998. * @compat_off: property name
  999. *
  1000. */
  1001. int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
  1002. phys_addr_t compat_off)
  1003. {
  1004. int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
  1005. while (off != -FDT_ERR_NOTFOUND) {
  1006. const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
  1007. if (reg) {
  1008. if (compat_off == fdt_translate_address(blob, off, reg))
  1009. return off;
  1010. }
  1011. off = fdt_node_offset_by_compatible(blob, off, compat);
  1012. }
  1013. return -FDT_ERR_NOTFOUND;
  1014. }
  1015. /**
  1016. * fdt_alloc_phandle: Return next free phandle value
  1017. *
  1018. * @blob: ptr to device tree
  1019. */
  1020. int fdt_alloc_phandle(void *blob)
  1021. {
  1022. int offset, phandle = 0;
  1023. for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
  1024. offset = fdt_next_node(blob, offset, NULL)) {
  1025. phandle = max(phandle, fdt_get_phandle(blob, offset));
  1026. }
  1027. return phandle + 1;
  1028. }
  1029. /*
  1030. * fdt_set_phandle: Create a phandle property for the given node
  1031. *
  1032. * @fdt: ptr to device tree
  1033. * @nodeoffset: node to update
  1034. * @phandle: phandle value to set (must be unique)
  1035. */
  1036. int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
  1037. {
  1038. int ret;
  1039. #ifdef DEBUG
  1040. int off = fdt_node_offset_by_phandle(fdt, phandle);
  1041. if ((off >= 0) && (off != nodeoffset)) {
  1042. char buf[64];
  1043. fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
  1044. printf("Trying to update node %s with phandle %u ",
  1045. buf, phandle);
  1046. fdt_get_path(fdt, off, buf, sizeof(buf));
  1047. printf("that already exists in node %s.\n", buf);
  1048. return -FDT_ERR_BADPHANDLE;
  1049. }
  1050. #endif
  1051. ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
  1052. if (ret < 0)
  1053. return ret;
  1054. /*
  1055. * For now, also set the deprecated "linux,phandle" property, so that we
  1056. * don't break older kernels.
  1057. */
  1058. ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
  1059. return ret;
  1060. }
  1061. /*
  1062. * fdt_create_phandle: Create a phandle property for the given node
  1063. *
  1064. * @fdt: ptr to device tree
  1065. * @nodeoffset: node to update
  1066. */
  1067. unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
  1068. {
  1069. /* see if there is a phandle already */
  1070. int phandle = fdt_get_phandle(fdt, nodeoffset);
  1071. /* if we got 0, means no phandle so create one */
  1072. if (phandle == 0) {
  1073. int ret;
  1074. phandle = fdt_alloc_phandle(fdt);
  1075. ret = fdt_set_phandle(fdt, nodeoffset, phandle);
  1076. if (ret < 0) {
  1077. printf("Can't set phandle %u: %s\n", phandle,
  1078. fdt_strerror(ret));
  1079. return 0;
  1080. }
  1081. }
  1082. return phandle;
  1083. }
  1084. /*
  1085. * fdt_set_node_status: Set status for the given node
  1086. *
  1087. * @fdt: ptr to device tree
  1088. * @nodeoffset: node to update
  1089. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
  1090. * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
  1091. * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
  1092. */
  1093. int fdt_set_node_status(void *fdt, int nodeoffset,
  1094. enum fdt_status status, unsigned int error_code)
  1095. {
  1096. char buf[16];
  1097. int ret = 0;
  1098. if (nodeoffset < 0)
  1099. return nodeoffset;
  1100. switch (status) {
  1101. case FDT_STATUS_OKAY:
  1102. ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
  1103. break;
  1104. case FDT_STATUS_DISABLED:
  1105. ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
  1106. break;
  1107. case FDT_STATUS_FAIL:
  1108. ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
  1109. break;
  1110. case FDT_STATUS_FAIL_ERROR_CODE:
  1111. sprintf(buf, "fail-%d", error_code);
  1112. ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
  1113. break;
  1114. default:
  1115. printf("Invalid fdt status: %x\n", status);
  1116. ret = -1;
  1117. break;
  1118. }
  1119. return ret;
  1120. }
  1121. /*
  1122. * fdt_set_status_by_alias: Set status for the given node given an alias
  1123. *
  1124. * @fdt: ptr to device tree
  1125. * @alias: alias of node to update
  1126. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
  1127. * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
  1128. * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
  1129. */
  1130. int fdt_set_status_by_alias(void *fdt, const char* alias,
  1131. enum fdt_status status, unsigned int error_code)
  1132. {
  1133. int offset = fdt_path_offset(fdt, alias);
  1134. return fdt_set_node_status(fdt, offset, status, error_code);
  1135. }
  1136. #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
  1137. int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
  1138. {
  1139. int noff;
  1140. int ret;
  1141. noff = fdt_node_offset_by_compatible(blob, -1, compat);
  1142. if (noff != -FDT_ERR_NOTFOUND) {
  1143. debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
  1144. add_edid:
  1145. ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
  1146. if (ret == -FDT_ERR_NOSPACE) {
  1147. ret = fdt_increase_size(blob, 512);
  1148. if (!ret)
  1149. goto add_edid;
  1150. else
  1151. goto err_size;
  1152. } else if (ret < 0) {
  1153. printf("Can't add property: %s\n", fdt_strerror(ret));
  1154. return ret;
  1155. }
  1156. }
  1157. return 0;
  1158. err_size:
  1159. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  1160. return ret;
  1161. }
  1162. #endif
  1163. /*
  1164. * Verify the physical address of device tree node for a given alias
  1165. *
  1166. * This function locates the device tree node of a given alias, and then
  1167. * verifies that the physical address of that device matches the given
  1168. * parameter. It displays a message if there is a mismatch.
  1169. *
  1170. * Returns 1 on success, 0 on failure
  1171. */
  1172. int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
  1173. {
  1174. const char *path;
  1175. const fdt32_t *reg;
  1176. int node, len;
  1177. u64 dt_addr;
  1178. path = fdt_getprop(fdt, anode, alias, NULL);
  1179. if (!path) {
  1180. /* If there's no such alias, then it's not a failure */
  1181. return 1;
  1182. }
  1183. node = fdt_path_offset(fdt, path);
  1184. if (node < 0) {
  1185. printf("Warning: device tree alias '%s' points to invalid "
  1186. "node %s.\n", alias, path);
  1187. return 0;
  1188. }
  1189. reg = fdt_getprop(fdt, node, "reg", &len);
  1190. if (!reg) {
  1191. printf("Warning: device tree node '%s' has no address.\n",
  1192. path);
  1193. return 0;
  1194. }
  1195. dt_addr = fdt_translate_address(fdt, node, reg);
  1196. if (addr != dt_addr) {
  1197. printf("Warning: U-Boot configured device %s at address %llx,\n"
  1198. " but the device tree has it address %llx.\n",
  1199. alias, addr, dt_addr);
  1200. return 0;
  1201. }
  1202. return 1;
  1203. }
  1204. /*
  1205. * Returns the base address of an SOC or PCI node
  1206. */
  1207. u64 fdt_get_base_address(void *fdt, int node)
  1208. {
  1209. int size;
  1210. u32 naddr;
  1211. const fdt32_t *prop;
  1212. prop = fdt_getprop(fdt, node, "#address-cells", &size);
  1213. if (prop && size == 4)
  1214. naddr = be32_to_cpup(prop);
  1215. else
  1216. naddr = 2;
  1217. prop = fdt_getprop(fdt, node, "ranges", &size);
  1218. return prop ? fdt_translate_address(fdt, node, prop + naddr) : 0;
  1219. }