fdt_support.c 34 KB

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