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