fdt_support.c 34 KB

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