fdt_support.c 34 KB

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