fdt_support.c 34 KB

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