fdt_support.c 34 KB

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