fdt_support.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2007
  4. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
  5. *
  6. * Copyright 2010-2011 Freescale Semiconductor, Inc.
  7. */
  8. #include <common.h>
  9. #include <abuf.h>
  10. #include <env.h>
  11. #include <log.h>
  12. #include <mapmem.h>
  13. #include <net.h>
  14. #include <stdio_dev.h>
  15. #include <dm/ofnode.h>
  16. #include <linux/ctype.h>
  17. #include <linux/types.h>
  18. #include <asm/global_data.h>
  19. #include <linux/libfdt.h>
  20. #include <fdt_support.h>
  21. #include <exports.h>
  22. #include <fdtdec.h>
  23. #include <version.h>
  24. /**
  25. * fdt_getprop_u32_default_node - Return a node's property or a default
  26. *
  27. * @fdt: ptr to device tree
  28. * @off: offset of node
  29. * @cell: cell offset in property
  30. * @prop: property name
  31. * @dflt: default value if the property isn't found
  32. *
  33. * Convenience function to return a node's property or a default value if
  34. * the property doesn't exist.
  35. */
  36. u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
  37. const char *prop, const u32 dflt)
  38. {
  39. const fdt32_t *val;
  40. int len;
  41. val = fdt_getprop(fdt, off, prop, &len);
  42. /* Check if property exists */
  43. if (!val)
  44. return dflt;
  45. /* Check if property is long enough */
  46. if (len < ((cell + 1) * sizeof(uint32_t)))
  47. return dflt;
  48. return fdt32_to_cpu(*val);
  49. }
  50. /**
  51. * fdt_getprop_u32_default - Find a node and return it's property or a default
  52. *
  53. * @fdt: ptr to device tree
  54. * @path: path of node
  55. * @prop: property name
  56. * @dflt: default value if the property isn't found
  57. *
  58. * Convenience function to find a node and return it's property or a
  59. * default value if it doesn't exist.
  60. */
  61. u32 fdt_getprop_u32_default(const void *fdt, const char *path,
  62. const char *prop, const u32 dflt)
  63. {
  64. int off;
  65. off = fdt_path_offset(fdt, path);
  66. if (off < 0)
  67. return dflt;
  68. return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
  69. }
  70. /**
  71. * fdt_find_and_setprop: Find a node and set it's property
  72. *
  73. * @fdt: ptr to device tree
  74. * @node: path of node
  75. * @prop: property name
  76. * @val: ptr to new value
  77. * @len: length of new property value
  78. * @create: flag to create the property if it doesn't exist
  79. *
  80. * Convenience function to directly set a property given the path to the node.
  81. */
  82. int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
  83. const void *val, int len, int create)
  84. {
  85. int nodeoff = fdt_path_offset(fdt, node);
  86. if (nodeoff < 0)
  87. return nodeoff;
  88. if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
  89. return 0; /* create flag not set; so exit quietly */
  90. return fdt_setprop(fdt, nodeoff, prop, val, len);
  91. }
  92. /**
  93. * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node
  94. *
  95. * @fdt: pointer to the device tree blob
  96. * @parentoffset: structure block offset of a node
  97. * @name: name of the subnode to locate
  98. *
  99. * fdt_subnode_offset() finds a subnode of the node with a given name.
  100. * If the subnode does not exist, it will be created.
  101. */
  102. int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name)
  103. {
  104. int offset;
  105. offset = fdt_subnode_offset(fdt, parentoffset, name);
  106. if (offset == -FDT_ERR_NOTFOUND)
  107. offset = fdt_add_subnode(fdt, parentoffset, name);
  108. if (offset < 0)
  109. printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
  110. return offset;
  111. }
  112. #if defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
  113. static int fdt_fixup_stdout(void *fdt, int chosenoff)
  114. {
  115. int err;
  116. int aliasoff;
  117. char sername[9] = { 0 };
  118. const void *path;
  119. int len;
  120. char tmp[256]; /* long enough */
  121. sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
  122. aliasoff = fdt_path_offset(fdt, "/aliases");
  123. if (aliasoff < 0) {
  124. err = aliasoff;
  125. goto noalias;
  126. }
  127. path = fdt_getprop(fdt, aliasoff, sername, &len);
  128. if (!path) {
  129. err = len;
  130. goto noalias;
  131. }
  132. /* fdt_setprop may break "path" so we copy it to tmp buffer */
  133. memcpy(tmp, path, len);
  134. err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
  135. if (err < 0)
  136. printf("WARNING: could not set linux,stdout-path %s.\n",
  137. fdt_strerror(err));
  138. return err;
  139. noalias:
  140. printf("WARNING: %s: could not read %s alias: %s\n",
  141. __func__, sername, fdt_strerror(err));
  142. return 0;
  143. }
  144. #else
  145. static int fdt_fixup_stdout(void *fdt, int chosenoff)
  146. {
  147. return 0;
  148. }
  149. #endif
  150. static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
  151. uint64_t val, int is_u64)
  152. {
  153. if (is_u64)
  154. return fdt_setprop_u64(fdt, nodeoffset, name, val);
  155. else
  156. return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
  157. }
  158. int fdt_root(void *fdt)
  159. {
  160. char *serial;
  161. int err;
  162. err = fdt_check_header(fdt);
  163. if (err < 0) {
  164. printf("fdt_root: %s\n", fdt_strerror(err));
  165. return err;
  166. }
  167. serial = env_get("serial#");
  168. if (serial) {
  169. err = fdt_setprop(fdt, 0, "serial-number", serial,
  170. strlen(serial) + 1);
  171. if (err < 0) {
  172. printf("WARNING: could not set serial-number %s.\n",
  173. fdt_strerror(err));
  174. return err;
  175. }
  176. }
  177. return 0;
  178. }
  179. int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
  180. {
  181. int nodeoffset;
  182. int err, j, total;
  183. int is_u64;
  184. uint64_t addr, size;
  185. /* just return if the size of initrd is zero */
  186. if (initrd_start == initrd_end)
  187. return 0;
  188. /* find or create "/chosen" node. */
  189. nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
  190. if (nodeoffset < 0)
  191. return nodeoffset;
  192. total = fdt_num_mem_rsv(fdt);
  193. /*
  194. * Look for an existing entry and update it. If we don't find
  195. * the entry, we will j be the next available slot.
  196. */
  197. for (j = 0; j < total; j++) {
  198. err = fdt_get_mem_rsv(fdt, j, &addr, &size);
  199. if (addr == initrd_start) {
  200. fdt_del_mem_rsv(fdt, j);
  201. break;
  202. }
  203. }
  204. err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
  205. if (err < 0) {
  206. printf("fdt_initrd: %s\n", fdt_strerror(err));
  207. return err;
  208. }
  209. is_u64 = (fdt_address_cells(fdt, 0) == 2);
  210. err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
  211. (uint64_t)initrd_start, is_u64);
  212. if (err < 0) {
  213. printf("WARNING: could not set linux,initrd-start %s.\n",
  214. fdt_strerror(err));
  215. return err;
  216. }
  217. err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
  218. (uint64_t)initrd_end, is_u64);
  219. if (err < 0) {
  220. printf("WARNING: could not set linux,initrd-end %s.\n",
  221. fdt_strerror(err));
  222. return err;
  223. }
  224. return 0;
  225. }
  226. /**
  227. * board_fdt_chosen_bootargs - boards may override this function to use
  228. * alternative kernel command line arguments
  229. */
  230. __weak char *board_fdt_chosen_bootargs(void)
  231. {
  232. return env_get("bootargs");
  233. }
  234. int fdt_chosen(void *fdt)
  235. {
  236. struct abuf buf = {};
  237. int nodeoffset;
  238. int err;
  239. char *str; /* used to set string properties */
  240. err = fdt_check_header(fdt);
  241. if (err < 0) {
  242. printf("fdt_chosen: %s\n", fdt_strerror(err));
  243. return err;
  244. }
  245. /* find or create "/chosen" node. */
  246. nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
  247. if (nodeoffset < 0)
  248. return nodeoffset;
  249. if (IS_ENABLED(CONFIG_BOARD_RNG_SEED) && !board_rng_seed(&buf)) {
  250. err = fdt_setprop(fdt, nodeoffset, "rng-seed",
  251. abuf_data(&buf), abuf_size(&buf));
  252. abuf_uninit(&buf);
  253. if (err < 0) {
  254. printf("WARNING: could not set rng-seed %s.\n",
  255. fdt_strerror(err));
  256. return err;
  257. }
  258. }
  259. str = board_fdt_chosen_bootargs();
  260. if (str) {
  261. err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
  262. strlen(str) + 1);
  263. if (err < 0) {
  264. printf("WARNING: could not set bootargs %s.\n",
  265. fdt_strerror(err));
  266. return err;
  267. }
  268. }
  269. /* add u-boot version */
  270. err = fdt_setprop(fdt, nodeoffset, "u-boot,version", PLAIN_VERSION,
  271. strlen(PLAIN_VERSION) + 1);
  272. if (err < 0) {
  273. printf("WARNING: could not set u-boot,version %s.\n",
  274. fdt_strerror(err));
  275. return err;
  276. }
  277. return fdt_fixup_stdout(fdt, nodeoffset);
  278. }
  279. void do_fixup_by_path(void *fdt, const char *path, const char *prop,
  280. const void *val, int len, int create)
  281. {
  282. #if defined(DEBUG)
  283. int i;
  284. debug("Updating property '%s/%s' = ", path, prop);
  285. for (i = 0; i < len; i++)
  286. debug(" %.2x", *(u8*)(val+i));
  287. debug("\n");
  288. #endif
  289. int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
  290. if (rc)
  291. printf("Unable to update property %s:%s, err=%s\n",
  292. path, prop, fdt_strerror(rc));
  293. }
  294. void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
  295. u32 val, int create)
  296. {
  297. fdt32_t tmp = cpu_to_fdt32(val);
  298. do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
  299. }
  300. void do_fixup_by_prop(void *fdt,
  301. const char *pname, const void *pval, int plen,
  302. const char *prop, const void *val, int len,
  303. int create)
  304. {
  305. int off;
  306. #if defined(DEBUG)
  307. int i;
  308. debug("Updating property '%s' = ", prop);
  309. for (i = 0; i < len; i++)
  310. debug(" %.2x", *(u8*)(val+i));
  311. debug("\n");
  312. #endif
  313. off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
  314. while (off != -FDT_ERR_NOTFOUND) {
  315. if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
  316. fdt_setprop(fdt, off, prop, val, len);
  317. off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
  318. }
  319. }
  320. void do_fixup_by_prop_u32(void *fdt,
  321. const char *pname, const void *pval, int plen,
  322. const char *prop, u32 val, int create)
  323. {
  324. fdt32_t tmp = cpu_to_fdt32(val);
  325. do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
  326. }
  327. void do_fixup_by_compat(void *fdt, const char *compat,
  328. const char *prop, const void *val, int len, int create)
  329. {
  330. int off = -1;
  331. #if defined(DEBUG)
  332. int i;
  333. debug("Updating property '%s' = ", prop);
  334. for (i = 0; i < len; i++)
  335. debug(" %.2x", *(u8*)(val+i));
  336. debug("\n");
  337. #endif
  338. fdt_for_each_node_by_compatible(off, fdt, -1, compat)
  339. if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
  340. fdt_setprop(fdt, off, prop, val, len);
  341. }
  342. void do_fixup_by_compat_u32(void *fdt, const char *compat,
  343. const char *prop, u32 val, int create)
  344. {
  345. fdt32_t tmp = cpu_to_fdt32(val);
  346. do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
  347. }
  348. #ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY
  349. /*
  350. * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
  351. */
  352. static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
  353. int n)
  354. {
  355. int i;
  356. int address_cells = fdt_address_cells(fdt, 0);
  357. int size_cells = fdt_size_cells(fdt, 0);
  358. char *p = buf;
  359. for (i = 0; i < n; i++) {
  360. if (address_cells == 2)
  361. *(fdt64_t *)p = cpu_to_fdt64(address[i]);
  362. else
  363. *(fdt32_t *)p = cpu_to_fdt32(address[i]);
  364. p += 4 * address_cells;
  365. if (size_cells == 2)
  366. *(fdt64_t *)p = cpu_to_fdt64(size[i]);
  367. else
  368. *(fdt32_t *)p = cpu_to_fdt32(size[i]);
  369. p += 4 * size_cells;
  370. }
  371. return p - (char *)buf;
  372. }
  373. #if CONFIG_NR_DRAM_BANKS > 4
  374. #define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
  375. #else
  376. #define MEMORY_BANKS_MAX 4
  377. #endif
  378. /**
  379. * fdt_fixup_memory_banks - Update DT memory node
  380. * @blob: Pointer to DT blob
  381. * @start: Pointer to memory start addresses array
  382. * @size: Pointer to memory sizes array
  383. * @banks: Number of memory banks
  384. *
  385. * Return: 0 on success, negative value on failure
  386. *
  387. * Based on the passed number of banks and arrays, the function is able to
  388. * update existing DT memory nodes to match run time detected/changed memory
  389. * configuration. Implementation is handling one specific case with only one
  390. * memory node where multiple tuples could be added/updated.
  391. * The case where multiple memory nodes with a single tuple (base, size) are
  392. * used, this function is only updating the first memory node without removing
  393. * others.
  394. */
  395. int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
  396. {
  397. int err, nodeoffset;
  398. int len, i;
  399. u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
  400. if (banks > MEMORY_BANKS_MAX) {
  401. printf("%s: num banks %d exceeds hardcoded limit %d."
  402. " Recompile with higher MEMORY_BANKS_MAX?\n",
  403. __FUNCTION__, banks, MEMORY_BANKS_MAX);
  404. return -1;
  405. }
  406. err = fdt_check_header(blob);
  407. if (err < 0) {
  408. printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
  409. return err;
  410. }
  411. /* find or create "/memory" node. */
  412. nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
  413. if (nodeoffset < 0)
  414. return nodeoffset;
  415. err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
  416. sizeof("memory"));
  417. if (err < 0) {
  418. printf("WARNING: could not set %s %s.\n", "device_type",
  419. fdt_strerror(err));
  420. return err;
  421. }
  422. for (i = 0; i < banks; i++) {
  423. if (start[i] == 0 && size[i] == 0)
  424. break;
  425. }
  426. banks = i;
  427. if (!banks)
  428. return 0;
  429. len = fdt_pack_reg(blob, tmp, start, size, banks);
  430. err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
  431. if (err < 0) {
  432. printf("WARNING: could not set %s %s.\n",
  433. "reg", fdt_strerror(err));
  434. return err;
  435. }
  436. return 0;
  437. }
  438. int fdt_set_usable_memory(void *blob, u64 start[], u64 size[], int areas)
  439. {
  440. int err, nodeoffset;
  441. int len;
  442. u8 tmp[8 * 16]; /* Up to 64-bit address + 64-bit size */
  443. if (areas > 8) {
  444. printf("%s: num areas %d exceeds hardcoded limit %d\n",
  445. __func__, areas, 8);
  446. return -1;
  447. }
  448. err = fdt_check_header(blob);
  449. if (err < 0) {
  450. printf("%s: %s\n", __func__, fdt_strerror(err));
  451. return err;
  452. }
  453. /* find or create "/memory" node. */
  454. nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
  455. if (nodeoffset < 0)
  456. return nodeoffset;
  457. len = fdt_pack_reg(blob, tmp, start, size, areas);
  458. err = fdt_setprop(blob, nodeoffset, "linux,usable-memory", tmp, len);
  459. if (err < 0) {
  460. printf("WARNING: could not set %s %s.\n",
  461. "reg", fdt_strerror(err));
  462. return err;
  463. }
  464. return 0;
  465. }
  466. #endif
  467. int fdt_fixup_memory(void *blob, u64 start, u64 size)
  468. {
  469. return fdt_fixup_memory_banks(blob, &start, &size, 1);
  470. }
  471. void fdt_fixup_ethernet(void *fdt)
  472. {
  473. int i = 0, j, prop;
  474. char *tmp, *end;
  475. char mac[16];
  476. const char *path;
  477. unsigned char mac_addr[ARP_HLEN];
  478. int offset;
  479. #ifdef FDT_SEQ_MACADDR_FROM_ENV
  480. int nodeoff;
  481. const struct fdt_property *fdt_prop;
  482. #endif
  483. if (fdt_path_offset(fdt, "/aliases") < 0)
  484. return;
  485. /* Cycle through all aliases */
  486. for (prop = 0; ; prop++) {
  487. const char *name;
  488. /* FDT might have been edited, recompute the offset */
  489. offset = fdt_first_property_offset(fdt,
  490. fdt_path_offset(fdt, "/aliases"));
  491. /* Select property number 'prop' */
  492. for (j = 0; j < prop; j++)
  493. offset = fdt_next_property_offset(fdt, offset);
  494. if (offset < 0)
  495. break;
  496. path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
  497. if (!strncmp(name, "ethernet", 8)) {
  498. /* Treat plain "ethernet" same as "ethernet0". */
  499. if (!strcmp(name, "ethernet")
  500. #ifdef FDT_SEQ_MACADDR_FROM_ENV
  501. || !strcmp(name, "ethernet0")
  502. #endif
  503. )
  504. i = 0;
  505. #ifndef FDT_SEQ_MACADDR_FROM_ENV
  506. else
  507. i = trailing_strtol(name);
  508. #endif
  509. if (i != -1) {
  510. if (i == 0)
  511. strcpy(mac, "ethaddr");
  512. else
  513. sprintf(mac, "eth%daddr", i);
  514. } else {
  515. continue;
  516. }
  517. #ifdef FDT_SEQ_MACADDR_FROM_ENV
  518. nodeoff = fdt_path_offset(fdt, path);
  519. fdt_prop = fdt_get_property(fdt, nodeoff, "status",
  520. NULL);
  521. if (fdt_prop && !strcmp(fdt_prop->data, "disabled"))
  522. continue;
  523. i++;
  524. #endif
  525. tmp = env_get(mac);
  526. if (!tmp)
  527. continue;
  528. for (j = 0; j < 6; j++) {
  529. mac_addr[j] = tmp ?
  530. hextoul(tmp, &end) : 0;
  531. if (tmp)
  532. tmp = (*end) ? end + 1 : end;
  533. }
  534. do_fixup_by_path(fdt, path, "mac-address",
  535. &mac_addr, 6, 0);
  536. do_fixup_by_path(fdt, path, "local-mac-address",
  537. &mac_addr, 6, 1);
  538. }
  539. }
  540. }
  541. int fdt_record_loadable(void *blob, u32 index, const char *name,
  542. uintptr_t load_addr, u32 size, uintptr_t entry_point,
  543. const char *type, const char *os, const char *arch)
  544. {
  545. int err, node;
  546. err = fdt_check_header(blob);
  547. if (err < 0) {
  548. printf("%s: %s\n", __func__, fdt_strerror(err));
  549. return err;
  550. }
  551. /* find or create "/fit-images" node */
  552. node = fdt_find_or_add_subnode(blob, 0, "fit-images");
  553. if (node < 0)
  554. return node;
  555. /* find or create "/fit-images/<name>" node */
  556. node = fdt_find_or_add_subnode(blob, node, name);
  557. if (node < 0)
  558. return node;
  559. fdt_setprop_u64(blob, node, "load", load_addr);
  560. if (entry_point != -1)
  561. fdt_setprop_u64(blob, node, "entry", entry_point);
  562. fdt_setprop_u32(blob, node, "size", size);
  563. if (type)
  564. fdt_setprop_string(blob, node, "type", type);
  565. if (os)
  566. fdt_setprop_string(blob, node, "os", os);
  567. if (arch)
  568. fdt_setprop_string(blob, node, "arch", arch);
  569. return node;
  570. }
  571. /* Resize the fdt to its actual size + a bit of padding */
  572. int fdt_shrink_to_minimum(void *blob, uint extrasize)
  573. {
  574. int i;
  575. uint64_t addr, size;
  576. int total, ret;
  577. uint actualsize;
  578. int fdt_memrsv = 0;
  579. if (!blob)
  580. return 0;
  581. total = fdt_num_mem_rsv(blob);
  582. for (i = 0; i < total; i++) {
  583. fdt_get_mem_rsv(blob, i, &addr, &size);
  584. if (addr == (uintptr_t)blob) {
  585. fdt_del_mem_rsv(blob, i);
  586. fdt_memrsv = 1;
  587. break;
  588. }
  589. }
  590. /*
  591. * Calculate the actual size of the fdt
  592. * plus the size needed for 5 fdt_add_mem_rsv, one
  593. * for the fdt itself and 4 for a possible initrd
  594. * ((initrd-start + initrd-end) * 2 (name & value))
  595. */
  596. actualsize = fdt_off_dt_strings(blob) +
  597. fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
  598. actualsize += extrasize;
  599. /* Make it so the fdt ends on a page boundary */
  600. actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
  601. actualsize = actualsize - ((uintptr_t)blob & 0xfff);
  602. /* Change the fdt header to reflect the correct size */
  603. fdt_set_totalsize(blob, actualsize);
  604. if (fdt_memrsv) {
  605. /* Add the new reservation */
  606. ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize);
  607. if (ret < 0)
  608. return ret;
  609. }
  610. return actualsize;
  611. }
  612. /**
  613. * fdt_delete_disabled_nodes: Delete all nodes with status == "disabled"
  614. *
  615. * @blob: ptr to device tree
  616. */
  617. int fdt_delete_disabled_nodes(void *blob)
  618. {
  619. while (1) {
  620. int ret, offset;
  621. offset = fdt_node_offset_by_prop_value(blob, -1, "status",
  622. "disabled", 9);
  623. if (offset < 0)
  624. break;
  625. ret = fdt_del_node(blob, offset);
  626. if (ret < 0)
  627. return ret;
  628. }
  629. return 0;
  630. }
  631. #ifdef CONFIG_PCI
  632. #define CFG_SYS_PCI_NR_INBOUND_WIN 4
  633. #define FDT_PCI_PREFETCH (0x40000000)
  634. #define FDT_PCI_MEM32 (0x02000000)
  635. #define FDT_PCI_IO (0x01000000)
  636. #define FDT_PCI_MEM64 (0x03000000)
  637. int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
  638. int addrcell, sizecell, len, r;
  639. u32 *dma_range;
  640. /* sized based on pci addr cells, size-cells, & address-cells */
  641. u32 dma_ranges[(3 + 2 + 2) * CFG_SYS_PCI_NR_INBOUND_WIN];
  642. addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
  643. sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
  644. dma_range = &dma_ranges[0];
  645. for (r = 0; r < hose->region_count; r++) {
  646. u64 bus_start, phys_start, size;
  647. /* skip if !PCI_REGION_SYS_MEMORY */
  648. if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
  649. continue;
  650. bus_start = (u64)hose->regions[r].bus_start;
  651. phys_start = (u64)hose->regions[r].phys_start;
  652. size = (u64)hose->regions[r].size;
  653. dma_range[0] = 0;
  654. if (size >= 0x100000000ull)
  655. dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64);
  656. else
  657. dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32);
  658. if (hose->regions[r].flags & PCI_REGION_PREFETCH)
  659. dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH);
  660. #ifdef CONFIG_SYS_PCI_64BIT
  661. dma_range[1] = cpu_to_fdt32(bus_start >> 32);
  662. #else
  663. dma_range[1] = 0;
  664. #endif
  665. dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff);
  666. if (addrcell == 2) {
  667. dma_range[3] = cpu_to_fdt32(phys_start >> 32);
  668. dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff);
  669. } else {
  670. dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff);
  671. }
  672. if (sizecell == 2) {
  673. dma_range[3 + addrcell + 0] =
  674. cpu_to_fdt32(size >> 32);
  675. dma_range[3 + addrcell + 1] =
  676. cpu_to_fdt32(size & 0xffffffff);
  677. } else {
  678. dma_range[3 + addrcell + 0] =
  679. cpu_to_fdt32(size & 0xffffffff);
  680. }
  681. dma_range += (3 + addrcell + sizecell);
  682. }
  683. len = dma_range - &dma_ranges[0];
  684. if (len)
  685. fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
  686. return 0;
  687. }
  688. #endif
  689. int fdt_increase_size(void *fdt, int add_len)
  690. {
  691. int newlen;
  692. newlen = fdt_totalsize(fdt) + add_len;
  693. /* Open in place with a new len */
  694. return fdt_open_into(fdt, fdt, newlen);
  695. }
  696. #ifdef CONFIG_FDT_FIXUP_PARTITIONS
  697. #include <jffs2/load_kernel.h>
  698. #include <mtd_node.h>
  699. static int fdt_del_subnodes(const void *blob, int parent_offset)
  700. {
  701. int off, ndepth;
  702. int ret;
  703. for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
  704. (off >= 0) && (ndepth > 0);
  705. off = fdt_next_node(blob, off, &ndepth)) {
  706. if (ndepth == 1) {
  707. debug("delete %s: offset: %x\n",
  708. fdt_get_name(blob, off, 0), off);
  709. ret = fdt_del_node((void *)blob, off);
  710. if (ret < 0) {
  711. printf("Can't delete node: %s\n",
  712. fdt_strerror(ret));
  713. return ret;
  714. } else {
  715. ndepth = 0;
  716. off = parent_offset;
  717. }
  718. }
  719. }
  720. return 0;
  721. }
  722. static int fdt_del_partitions(void *blob, int parent_offset)
  723. {
  724. const void *prop;
  725. int ndepth = 0;
  726. int off;
  727. int ret;
  728. off = fdt_next_node(blob, parent_offset, &ndepth);
  729. if (off > 0 && ndepth == 1) {
  730. prop = fdt_getprop(blob, off, "label", NULL);
  731. if (prop == NULL) {
  732. /*
  733. * Could not find label property, nand {}; node?
  734. * Check subnode, delete partitions there if any.
  735. */
  736. return fdt_del_partitions(blob, off);
  737. } else {
  738. ret = fdt_del_subnodes(blob, parent_offset);
  739. if (ret < 0) {
  740. printf("Can't remove subnodes: %s\n",
  741. fdt_strerror(ret));
  742. return ret;
  743. }
  744. }
  745. }
  746. return 0;
  747. }
  748. static int fdt_node_set_part_info(void *blob, int parent_offset,
  749. struct mtd_device *dev)
  750. {
  751. struct list_head *pentry;
  752. struct part_info *part;
  753. int off, ndepth = 0;
  754. int part_num, ret;
  755. int sizecell;
  756. char buf[64];
  757. ret = fdt_del_partitions(blob, parent_offset);
  758. if (ret < 0)
  759. return ret;
  760. /*
  761. * Check if size/address is 1 or 2 cells.
  762. * We assume #address-cells and #size-cells have same value.
  763. */
  764. sizecell = fdt_getprop_u32_default_node(blob, parent_offset,
  765. 0, "#size-cells", 1);
  766. /*
  767. * Check if it is nand {}; subnode, adjust
  768. * the offset in this case
  769. */
  770. off = fdt_next_node(blob, parent_offset, &ndepth);
  771. if (off > 0 && ndepth == 1)
  772. parent_offset = off;
  773. part_num = 0;
  774. list_for_each_prev(pentry, &dev->parts) {
  775. int newoff;
  776. part = list_entry(pentry, struct part_info, link);
  777. debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
  778. part_num, part->name, part->size,
  779. part->offset, part->mask_flags);
  780. sprintf(buf, "partition@%llx", part->offset);
  781. add_sub:
  782. ret = fdt_add_subnode(blob, parent_offset, buf);
  783. if (ret == -FDT_ERR_NOSPACE) {
  784. ret = fdt_increase_size(blob, 512);
  785. if (!ret)
  786. goto add_sub;
  787. else
  788. goto err_size;
  789. } else if (ret < 0) {
  790. printf("Can't add partition node: %s\n",
  791. fdt_strerror(ret));
  792. return ret;
  793. }
  794. newoff = ret;
  795. /* Check MTD_WRITEABLE_CMD flag */
  796. if (part->mask_flags & 1) {
  797. add_ro:
  798. ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
  799. if (ret == -FDT_ERR_NOSPACE) {
  800. ret = fdt_increase_size(blob, 512);
  801. if (!ret)
  802. goto add_ro;
  803. else
  804. goto err_size;
  805. } else if (ret < 0)
  806. goto err_prop;
  807. }
  808. add_reg:
  809. if (sizecell == 2) {
  810. ret = fdt_setprop_u64(blob, newoff,
  811. "reg", part->offset);
  812. if (!ret)
  813. ret = fdt_appendprop_u64(blob, newoff,
  814. "reg", part->size);
  815. } else {
  816. ret = fdt_setprop_u32(blob, newoff,
  817. "reg", part->offset);
  818. if (!ret)
  819. ret = fdt_appendprop_u32(blob, newoff,
  820. "reg", part->size);
  821. }
  822. if (ret == -FDT_ERR_NOSPACE) {
  823. ret = fdt_increase_size(blob, 512);
  824. if (!ret)
  825. goto add_reg;
  826. else
  827. goto err_size;
  828. } else if (ret < 0)
  829. goto err_prop;
  830. add_label:
  831. ret = fdt_setprop_string(blob, newoff, "label", part->name);
  832. if (ret == -FDT_ERR_NOSPACE) {
  833. ret = fdt_increase_size(blob, 512);
  834. if (!ret)
  835. goto add_label;
  836. else
  837. goto err_size;
  838. } else if (ret < 0)
  839. goto err_prop;
  840. part_num++;
  841. }
  842. return 0;
  843. err_size:
  844. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  845. return ret;
  846. err_prop:
  847. printf("Can't add property: %s\n", fdt_strerror(ret));
  848. return ret;
  849. }
  850. /*
  851. * Update partitions in nor/nand nodes using info from
  852. * mtdparts environment variable. The nodes to update are
  853. * specified by node_info structure which contains mtd device
  854. * type and compatible string: E. g. the board code in
  855. * ft_board_setup() could use:
  856. *
  857. * struct node_info nodes[] = {
  858. * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
  859. * { "cfi-flash", MTD_DEV_TYPE_NOR, },
  860. * };
  861. *
  862. * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
  863. */
  864. void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
  865. int node_info_size)
  866. {
  867. struct mtd_device *dev;
  868. int i, idx;
  869. int noff, parts;
  870. bool inited = false;
  871. for (i = 0; i < node_info_size; i++) {
  872. idx = 0;
  873. fdt_for_each_node_by_compatible(noff, blob, -1,
  874. node_info[i].compat) {
  875. const char *prop;
  876. prop = fdt_getprop(blob, noff, "status", NULL);
  877. if (prop && !strcmp(prop, "disabled"))
  878. continue;
  879. debug("%s: %s, mtd dev type %d\n",
  880. fdt_get_name(blob, noff, 0),
  881. node_info[i].compat, node_info[i].type);
  882. if (!inited) {
  883. if (mtdparts_init() != 0)
  884. return;
  885. inited = true;
  886. }
  887. dev = device_find(node_info[i].type, idx++);
  888. if (dev) {
  889. parts = fdt_subnode_offset(blob, noff,
  890. "partitions");
  891. if (parts < 0)
  892. parts = noff;
  893. if (fdt_node_set_part_info(blob, parts, dev))
  894. return; /* return on error */
  895. }
  896. }
  897. }
  898. }
  899. #endif
  900. int fdt_copy_fixed_partitions(void *blob)
  901. {
  902. ofnode node, subnode;
  903. int off, suboff, res;
  904. char path[256];
  905. int address_cells, size_cells;
  906. u8 i, j, child_count;
  907. node = ofnode_by_compatible(ofnode_null(), "fixed-partitions");
  908. while (ofnode_valid(node)) {
  909. /* copy the U-Boot fixed partition */
  910. address_cells = ofnode_read_simple_addr_cells(node);
  911. size_cells = ofnode_read_simple_size_cells(node);
  912. res = ofnode_get_path(ofnode_get_parent(node), path, sizeof(path));
  913. if (res)
  914. return res;
  915. off = fdt_path_offset(blob, path);
  916. if (off < 0)
  917. return -ENODEV;
  918. off = fdt_find_or_add_subnode(blob, off, "partitions");
  919. res = fdt_setprop_string(blob, off, "compatible", "fixed-partitions");
  920. if (res)
  921. return res;
  922. res = fdt_setprop_u32(blob, off, "#address-cells", address_cells);
  923. if (res)
  924. return res;
  925. res = fdt_setprop_u32(blob, off, "#size-cells", size_cells);
  926. if (res)
  927. return res;
  928. /*
  929. * parse partition in reverse order as fdt_find_or_add_subnode() only
  930. * insert the new node after the parent's properties
  931. */
  932. child_count = ofnode_get_child_count(node);
  933. for (i = child_count; i > 0 ; i--) {
  934. subnode = ofnode_first_subnode(node);
  935. if (!ofnode_valid(subnode))
  936. break;
  937. for (j = 0; (j < i - 1); j++)
  938. subnode = ofnode_next_subnode(subnode);
  939. if (!ofnode_valid(subnode))
  940. break;
  941. const u32 *reg;
  942. int len;
  943. suboff = fdt_find_or_add_subnode(blob, off, ofnode_get_name(subnode));
  944. res = fdt_setprop_string(blob, suboff, "label",
  945. ofnode_read_string(subnode, "label"));
  946. if (res)
  947. return res;
  948. reg = ofnode_get_property(subnode, "reg", &len);
  949. res = fdt_setprop(blob, suboff, "reg", reg, len);
  950. if (res)
  951. return res;
  952. }
  953. /* go to next fixed-partitions node */
  954. node = ofnode_by_compatible(node, "fixed-partitions");
  955. }
  956. return 0;
  957. }
  958. void fdt_del_node_and_alias(void *blob, const char *alias)
  959. {
  960. int off = fdt_path_offset(blob, alias);
  961. if (off < 0)
  962. return;
  963. fdt_del_node(blob, off);
  964. off = fdt_path_offset(blob, "/aliases");
  965. fdt_delprop(blob, off, alias);
  966. }
  967. /* Max address size we deal with */
  968. #define OF_MAX_ADDR_CELLS 4
  969. #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
  970. (ns) > 0)
  971. /* Debug utility */
  972. #ifdef DEBUG
  973. static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
  974. {
  975. printf("%s", s);
  976. while(na--)
  977. printf(" %08x", *(addr++));
  978. printf("\n");
  979. }
  980. #else
  981. static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
  982. #endif
  983. /**
  984. * struct of_bus - Callbacks for bus specific translators
  985. * @name: A string used to identify this bus in debug output.
  986. * @addresses: The name of the DT property from which addresses are
  987. * to be read, typically "reg".
  988. * @match: Return non-zero if the node whose parent is at
  989. * parentoffset in the FDT blob corresponds to a bus
  990. * of this type, otherwise return zero. If NULL a match
  991. * is assumed.
  992. * @count_cells:Count how many cells (be32 values) a node whose parent
  993. * is at parentoffset in the FDT blob will require to
  994. * represent its address (written to *addrc) & size
  995. * (written to *sizec).
  996. * @map: Map the address addr from the address space of this
  997. * bus to that of its parent, making use of the ranges
  998. * read from DT to an array at range. na and ns are the
  999. * number of cells (be32 values) used to hold and address
  1000. * or size, respectively, for this bus. pna is the number
  1001. * of cells used to hold an address for the parent bus.
  1002. * Returns the address in the address space of the parent
  1003. * bus.
  1004. * @translate: Update the value of the address cells at addr within an
  1005. * FDT by adding offset to it. na specifies the number of
  1006. * cells used to hold the address being translated. Returns
  1007. * zero on success, non-zero on error.
  1008. *
  1009. * Each bus type will include a struct of_bus in the of_busses array,
  1010. * providing implementations of some or all of the functions used to
  1011. * match the bus & handle address translation for its children.
  1012. */
  1013. struct of_bus {
  1014. const char *name;
  1015. const char *addresses;
  1016. int (*match)(const void *blob, int parentoffset);
  1017. void (*count_cells)(const void *blob, int parentoffset,
  1018. int *addrc, int *sizec);
  1019. u64 (*map)(fdt32_t *addr, const fdt32_t *range,
  1020. int na, int ns, int pna);
  1021. int (*translate)(fdt32_t *addr, u64 offset, int na);
  1022. };
  1023. /* Default translator (generic bus) */
  1024. void fdt_support_default_count_cells(const void *blob, int parentoffset,
  1025. int *addrc, int *sizec)
  1026. {
  1027. const fdt32_t *prop;
  1028. if (addrc)
  1029. *addrc = fdt_address_cells(blob, parentoffset);
  1030. if (sizec) {
  1031. prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
  1032. if (prop)
  1033. *sizec = be32_to_cpup(prop);
  1034. else
  1035. *sizec = 1;
  1036. }
  1037. }
  1038. static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
  1039. int na, int ns, int pna)
  1040. {
  1041. u64 cp, s, da;
  1042. cp = fdt_read_number(range, na);
  1043. s = fdt_read_number(range + na + pna, ns);
  1044. da = fdt_read_number(addr, na);
  1045. debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
  1046. if (da < cp || da >= (cp + s))
  1047. return OF_BAD_ADDR;
  1048. return da - cp;
  1049. }
  1050. static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
  1051. {
  1052. u64 a = fdt_read_number(addr, na);
  1053. memset(addr, 0, na * 4);
  1054. a += offset;
  1055. if (na > 1)
  1056. addr[na - 2] = cpu_to_fdt32(a >> 32);
  1057. addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
  1058. return 0;
  1059. }
  1060. #ifdef CONFIG_OF_ISA_BUS
  1061. /* ISA bus translator */
  1062. static int of_bus_isa_match(const void *blob, int parentoffset)
  1063. {
  1064. const char *name;
  1065. name = fdt_get_name(blob, parentoffset, NULL);
  1066. if (!name)
  1067. return 0;
  1068. return !strcmp(name, "isa");
  1069. }
  1070. static void of_bus_isa_count_cells(const void *blob, int parentoffset,
  1071. int *addrc, int *sizec)
  1072. {
  1073. if (addrc)
  1074. *addrc = 2;
  1075. if (sizec)
  1076. *sizec = 1;
  1077. }
  1078. static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
  1079. int na, int ns, int pna)
  1080. {
  1081. u64 cp, s, da;
  1082. /* Check address type match */
  1083. if ((addr[0] ^ range[0]) & cpu_to_be32(1))
  1084. return OF_BAD_ADDR;
  1085. cp = fdt_read_number(range + 1, na - 1);
  1086. s = fdt_read_number(range + na + pna, ns);
  1087. da = fdt_read_number(addr + 1, na - 1);
  1088. debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
  1089. if (da < cp || da >= (cp + s))
  1090. return OF_BAD_ADDR;
  1091. return da - cp;
  1092. }
  1093. static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
  1094. {
  1095. return of_bus_default_translate(addr + 1, offset, na - 1);
  1096. }
  1097. #endif /* CONFIG_OF_ISA_BUS */
  1098. /* Array of bus specific translators */
  1099. static struct of_bus of_busses[] = {
  1100. #ifdef CONFIG_OF_ISA_BUS
  1101. /* ISA */
  1102. {
  1103. .name = "isa",
  1104. .addresses = "reg",
  1105. .match = of_bus_isa_match,
  1106. .count_cells = of_bus_isa_count_cells,
  1107. .map = of_bus_isa_map,
  1108. .translate = of_bus_isa_translate,
  1109. },
  1110. #endif /* CONFIG_OF_ISA_BUS */
  1111. /* Default */
  1112. {
  1113. .name = "default",
  1114. .addresses = "reg",
  1115. .count_cells = fdt_support_default_count_cells,
  1116. .map = of_bus_default_map,
  1117. .translate = of_bus_default_translate,
  1118. },
  1119. };
  1120. static struct of_bus *of_match_bus(const void *blob, int parentoffset)
  1121. {
  1122. struct of_bus *bus;
  1123. if (ARRAY_SIZE(of_busses) == 1)
  1124. return of_busses;
  1125. for (bus = of_busses; bus; bus++) {
  1126. if (!bus->match || bus->match(blob, parentoffset))
  1127. return bus;
  1128. }
  1129. /*
  1130. * We should always have matched the default bus at least, since
  1131. * it has a NULL match field. If we didn't then it somehow isn't
  1132. * in the of_busses array or something equally catastrophic has
  1133. * gone wrong.
  1134. */
  1135. assert(0);
  1136. return NULL;
  1137. }
  1138. static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
  1139. struct of_bus *pbus, fdt32_t *addr,
  1140. int na, int ns, int pna, const char *rprop)
  1141. {
  1142. const fdt32_t *ranges;
  1143. int rlen;
  1144. int rone;
  1145. u64 offset = OF_BAD_ADDR;
  1146. /* Normally, an absence of a "ranges" property means we are
  1147. * crossing a non-translatable boundary, and thus the addresses
  1148. * below the current not cannot be converted to CPU physical ones.
  1149. * Unfortunately, while this is very clear in the spec, it's not
  1150. * what Apple understood, and they do have things like /uni-n or
  1151. * /ht nodes with no "ranges" property and a lot of perfectly
  1152. * useable mapped devices below them. Thus we treat the absence of
  1153. * "ranges" as equivalent to an empty "ranges" property which means
  1154. * a 1:1 translation at that level. It's up to the caller not to try
  1155. * to translate addresses that aren't supposed to be translated in
  1156. * the first place. --BenH.
  1157. */
  1158. ranges = fdt_getprop(blob, parent, rprop, &rlen);
  1159. if (ranges == NULL || rlen == 0) {
  1160. offset = fdt_read_number(addr, na);
  1161. memset(addr, 0, pna * 4);
  1162. debug("OF: no ranges, 1:1 translation\n");
  1163. goto finish;
  1164. }
  1165. debug("OF: walking ranges...\n");
  1166. /* Now walk through the ranges */
  1167. rlen /= 4;
  1168. rone = na + pna + ns;
  1169. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  1170. offset = bus->map(addr, ranges, na, ns, pna);
  1171. if (offset != OF_BAD_ADDR)
  1172. break;
  1173. }
  1174. if (offset == OF_BAD_ADDR) {
  1175. debug("OF: not found !\n");
  1176. return 1;
  1177. }
  1178. memcpy(addr, ranges + na, 4 * pna);
  1179. finish:
  1180. of_dump_addr("OF: parent translation for:", addr, pna);
  1181. debug("OF: with offset: %llu\n", offset);
  1182. /* Translate it into parent bus space */
  1183. return pbus->translate(addr, offset, pna);
  1184. }
  1185. /*
  1186. * Translate an address from the device-tree into a CPU physical address,
  1187. * this walks up the tree and applies the various bus mappings on the
  1188. * way.
  1189. *
  1190. * Note: We consider that crossing any level with #size-cells == 0 to mean
  1191. * that translation is impossible (that is we are not dealing with a value
  1192. * that can be mapped to a cpu physical address). This is not really specified
  1193. * that way, but this is traditionally the way IBM at least do things
  1194. */
  1195. static u64 __of_translate_address(const void *blob, int node_offset,
  1196. const fdt32_t *in_addr, const char *rprop)
  1197. {
  1198. int parent;
  1199. struct of_bus *bus, *pbus;
  1200. fdt32_t addr[OF_MAX_ADDR_CELLS];
  1201. int na, ns, pna, pns;
  1202. u64 result = OF_BAD_ADDR;
  1203. debug("OF: ** translation for device %s **\n",
  1204. fdt_get_name(blob, node_offset, NULL));
  1205. /* Get parent & match bus type */
  1206. parent = fdt_parent_offset(blob, node_offset);
  1207. if (parent < 0)
  1208. goto bail;
  1209. bus = of_match_bus(blob, parent);
  1210. /* Cound address cells & copy address locally */
  1211. bus->count_cells(blob, parent, &na, &ns);
  1212. if (!OF_CHECK_COUNTS(na, ns)) {
  1213. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  1214. fdt_get_name(blob, node_offset, NULL));
  1215. goto bail;
  1216. }
  1217. memcpy(addr, in_addr, na * 4);
  1218. debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  1219. bus->name, na, ns, fdt_get_name(blob, parent, NULL));
  1220. of_dump_addr("OF: translating address:", addr, na);
  1221. /* Translate */
  1222. for (;;) {
  1223. /* Switch to parent bus */
  1224. node_offset = parent;
  1225. parent = fdt_parent_offset(blob, node_offset);
  1226. /* If root, we have finished */
  1227. if (parent < 0) {
  1228. debug("OF: reached root node\n");
  1229. result = fdt_read_number(addr, na);
  1230. break;
  1231. }
  1232. /* Get new parent bus and counts */
  1233. pbus = of_match_bus(blob, parent);
  1234. pbus->count_cells(blob, parent, &pna, &pns);
  1235. if (!OF_CHECK_COUNTS(pna, pns)) {
  1236. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  1237. fdt_get_name(blob, node_offset, NULL));
  1238. break;
  1239. }
  1240. debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  1241. pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
  1242. /* Apply bus translation */
  1243. if (of_translate_one(blob, node_offset, bus, pbus,
  1244. addr, na, ns, pna, rprop))
  1245. break;
  1246. /* Complete the move up one level */
  1247. na = pna;
  1248. ns = pns;
  1249. bus = pbus;
  1250. of_dump_addr("OF: one level translation:", addr, na);
  1251. }
  1252. bail:
  1253. return result;
  1254. }
  1255. u64 fdt_translate_address(const void *blob, int node_offset,
  1256. const fdt32_t *in_addr)
  1257. {
  1258. return __of_translate_address(blob, node_offset, in_addr, "ranges");
  1259. }
  1260. u64 fdt_translate_dma_address(const void *blob, int node_offset,
  1261. const fdt32_t *in_addr)
  1262. {
  1263. return __of_translate_address(blob, node_offset, in_addr, "dma-ranges");
  1264. }
  1265. int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu,
  1266. dma_addr_t *bus, u64 *size)
  1267. {
  1268. bool found_dma_ranges = false;
  1269. struct of_bus *bus_node;
  1270. const fdt32_t *ranges;
  1271. int na, ns, pna, pns;
  1272. int parent = node;
  1273. int ret = 0;
  1274. int len;
  1275. /* Find the closest dma-ranges property */
  1276. while (parent >= 0) {
  1277. ranges = fdt_getprop(blob, parent, "dma-ranges", &len);
  1278. /* Ignore empty ranges, they imply no translation required */
  1279. if (ranges && len > 0)
  1280. break;
  1281. /* Once we find 'dma-ranges', then a missing one is an error */
  1282. if (found_dma_ranges && !ranges) {
  1283. ret = -EINVAL;
  1284. goto out;
  1285. }
  1286. if (ranges)
  1287. found_dma_ranges = true;
  1288. parent = fdt_parent_offset(blob, parent);
  1289. }
  1290. if (!ranges || parent < 0) {
  1291. debug("no dma-ranges found for node %s\n",
  1292. fdt_get_name(blob, node, NULL));
  1293. ret = -ENOENT;
  1294. goto out;
  1295. }
  1296. /* switch to that node */
  1297. node = parent;
  1298. parent = fdt_parent_offset(blob, node);
  1299. if (parent < 0) {
  1300. printf("Found dma-ranges in root node, shouldn't happen\n");
  1301. ret = -EINVAL;
  1302. goto out;
  1303. }
  1304. /* Get the address sizes both for the bus and its parent */
  1305. bus_node = of_match_bus(blob, node);
  1306. bus_node->count_cells(blob, node, &na, &ns);
  1307. if (!OF_CHECK_COUNTS(na, ns)) {
  1308. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  1309. fdt_get_name(blob, node, NULL));
  1310. return -EINVAL;
  1311. goto out;
  1312. }
  1313. bus_node = of_match_bus(blob, parent);
  1314. bus_node->count_cells(blob, parent, &pna, &pns);
  1315. if (!OF_CHECK_COUNTS(pna, pns)) {
  1316. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  1317. fdt_get_name(blob, parent, NULL));
  1318. return -EINVAL;
  1319. goto out;
  1320. }
  1321. *bus = fdt_read_number(ranges, na);
  1322. *cpu = fdt_translate_dma_address(blob, node, ranges + na);
  1323. *size = fdt_read_number(ranges + na + pna, ns);
  1324. out:
  1325. return ret;
  1326. }
  1327. /**
  1328. * fdt_node_offset_by_compat_reg: Find a node that matches compatible and
  1329. * who's reg property matches a physical cpu address
  1330. *
  1331. * @blob: ptr to device tree
  1332. * @compat: compatible string to match
  1333. * @compat_off: property name
  1334. *
  1335. */
  1336. int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
  1337. phys_addr_t compat_off)
  1338. {
  1339. int len, off;
  1340. fdt_for_each_node_by_compatible(off, blob, -1, compat) {
  1341. const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
  1342. if (reg && compat_off == fdt_translate_address(blob, off, reg))
  1343. return off;
  1344. }
  1345. return -FDT_ERR_NOTFOUND;
  1346. }
  1347. static int vnode_offset_by_pathf(void *blob, const char *fmt, va_list ap)
  1348. {
  1349. char path[512];
  1350. int len;
  1351. len = vsnprintf(path, sizeof(path), fmt, ap);
  1352. if (len < 0 || len + 1 > sizeof(path))
  1353. return -FDT_ERR_NOSPACE;
  1354. return fdt_path_offset(blob, path);
  1355. }
  1356. /**
  1357. * fdt_node_offset_by_pathf: Find node offset by sprintf formatted path
  1358. *
  1359. * @blob: ptr to device tree
  1360. * @fmt: path format
  1361. * @ap: vsnprintf arguments
  1362. */
  1363. int fdt_node_offset_by_pathf(void *blob, const char *fmt, ...)
  1364. {
  1365. va_list ap;
  1366. int res;
  1367. va_start(ap, fmt);
  1368. res = vnode_offset_by_pathf(blob, fmt, ap);
  1369. va_end(ap);
  1370. return res;
  1371. }
  1372. /*
  1373. * fdt_set_phandle: Create a phandle property for the given node
  1374. *
  1375. * @fdt: ptr to device tree
  1376. * @nodeoffset: node to update
  1377. * @phandle: phandle value to set (must be unique)
  1378. */
  1379. int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
  1380. {
  1381. int ret;
  1382. #ifdef DEBUG
  1383. int off = fdt_node_offset_by_phandle(fdt, phandle);
  1384. if ((off >= 0) && (off != nodeoffset)) {
  1385. char buf[64];
  1386. fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
  1387. printf("Trying to update node %s with phandle %u ",
  1388. buf, phandle);
  1389. fdt_get_path(fdt, off, buf, sizeof(buf));
  1390. printf("that already exists in node %s.\n", buf);
  1391. return -FDT_ERR_BADPHANDLE;
  1392. }
  1393. #endif
  1394. ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
  1395. return ret;
  1396. }
  1397. /*
  1398. * fdt_create_phandle: Get or create a phandle property for the given node
  1399. *
  1400. * @fdt: ptr to device tree
  1401. * @nodeoffset: node to update
  1402. */
  1403. unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
  1404. {
  1405. /* see if there is a phandle already */
  1406. uint32_t phandle = fdt_get_phandle(fdt, nodeoffset);
  1407. /* if we got 0, means no phandle so create one */
  1408. if (phandle == 0) {
  1409. int ret;
  1410. ret = fdt_generate_phandle(fdt, &phandle);
  1411. if (ret < 0) {
  1412. printf("Can't generate phandle: %s\n",
  1413. fdt_strerror(ret));
  1414. return 0;
  1415. }
  1416. ret = fdt_set_phandle(fdt, nodeoffset, phandle);
  1417. if (ret < 0) {
  1418. printf("Can't set phandle %u: %s\n", phandle,
  1419. fdt_strerror(ret));
  1420. return 0;
  1421. }
  1422. }
  1423. return phandle;
  1424. }
  1425. /**
  1426. * fdt_create_phandle_by_compatible: Get or create a phandle for first node with
  1427. * given compatible
  1428. *
  1429. * @fdt: ptr to device tree
  1430. * @compat: node's compatible string
  1431. */
  1432. unsigned int fdt_create_phandle_by_compatible(void *fdt, const char *compat)
  1433. {
  1434. int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
  1435. if (offset < 0) {
  1436. printf("Can't find node with compatible \"%s\": %s\n", compat,
  1437. fdt_strerror(offset));
  1438. return 0;
  1439. }
  1440. return fdt_create_phandle(fdt, offset);
  1441. }
  1442. /**
  1443. * fdt_create_phandle_by_pathf: Get or create a phandle for node given by
  1444. * sprintf-formatted path
  1445. *
  1446. * @fdt: ptr to device tree
  1447. * @fmt, ...: path format string and arguments to pass to sprintf
  1448. */
  1449. unsigned int fdt_create_phandle_by_pathf(void *fdt, const char *fmt, ...)
  1450. {
  1451. va_list ap;
  1452. int offset;
  1453. va_start(ap, fmt);
  1454. offset = vnode_offset_by_pathf(fdt, fmt, ap);
  1455. va_end(ap);
  1456. if (offset < 0) {
  1457. printf("Can't find node by given path: %s\n",
  1458. fdt_strerror(offset));
  1459. return 0;
  1460. }
  1461. return fdt_create_phandle(fdt, offset);
  1462. }
  1463. /*
  1464. * fdt_set_node_status: Set status for the given node
  1465. *
  1466. * @fdt: ptr to device tree
  1467. * @nodeoffset: node to update
  1468. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
  1469. */
  1470. int fdt_set_node_status(void *fdt, int nodeoffset, enum fdt_status status)
  1471. {
  1472. int ret = 0;
  1473. if (nodeoffset < 0)
  1474. return nodeoffset;
  1475. switch (status) {
  1476. case FDT_STATUS_OKAY:
  1477. ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
  1478. break;
  1479. case FDT_STATUS_DISABLED:
  1480. ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
  1481. break;
  1482. case FDT_STATUS_FAIL:
  1483. ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
  1484. break;
  1485. default:
  1486. printf("Invalid fdt status: %x\n", status);
  1487. ret = -1;
  1488. break;
  1489. }
  1490. return ret;
  1491. }
  1492. /*
  1493. * fdt_set_status_by_alias: Set status for the given node given an alias
  1494. *
  1495. * @fdt: ptr to device tree
  1496. * @alias: alias of node to update
  1497. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
  1498. */
  1499. int fdt_set_status_by_alias(void *fdt, const char* alias,
  1500. enum fdt_status status)
  1501. {
  1502. int offset = fdt_path_offset(fdt, alias);
  1503. return fdt_set_node_status(fdt, offset, status);
  1504. }
  1505. /**
  1506. * fdt_set_status_by_compatible: Set node status for first node with given
  1507. * compatible
  1508. *
  1509. * @fdt: ptr to device tree
  1510. * @compat: node's compatible string
  1511. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
  1512. */
  1513. int fdt_set_status_by_compatible(void *fdt, const char *compat,
  1514. enum fdt_status status)
  1515. {
  1516. int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
  1517. if (offset < 0)
  1518. return offset;
  1519. return fdt_set_node_status(fdt, offset, status);
  1520. }
  1521. /**
  1522. * fdt_set_status_by_pathf: Set node status for node given by sprintf-formatted
  1523. * path
  1524. *
  1525. * @fdt: ptr to device tree
  1526. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
  1527. * @fmt, ...: path format string and arguments to pass to sprintf
  1528. */
  1529. int fdt_set_status_by_pathf(void *fdt, enum fdt_status status, const char *fmt,
  1530. ...)
  1531. {
  1532. va_list ap;
  1533. int offset;
  1534. va_start(ap, fmt);
  1535. offset = vnode_offset_by_pathf(fdt, fmt, ap);
  1536. va_end(ap);
  1537. if (offset < 0)
  1538. return offset;
  1539. return fdt_set_node_status(fdt, offset, status);
  1540. }
  1541. /*
  1542. * Verify the physical address of device tree node for a given alias
  1543. *
  1544. * This function locates the device tree node of a given alias, and then
  1545. * verifies that the physical address of that device matches the given
  1546. * parameter. It displays a message if there is a mismatch.
  1547. *
  1548. * Returns 1 on success, 0 on failure
  1549. */
  1550. int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
  1551. {
  1552. const char *path;
  1553. const fdt32_t *reg;
  1554. int node, len;
  1555. u64 dt_addr;
  1556. path = fdt_getprop(fdt, anode, alias, NULL);
  1557. if (!path) {
  1558. /* If there's no such alias, then it's not a failure */
  1559. return 1;
  1560. }
  1561. node = fdt_path_offset(fdt, path);
  1562. if (node < 0) {
  1563. printf("Warning: device tree alias '%s' points to invalid "
  1564. "node %s.\n", alias, path);
  1565. return 0;
  1566. }
  1567. reg = fdt_getprop(fdt, node, "reg", &len);
  1568. if (!reg) {
  1569. printf("Warning: device tree node '%s' has no address.\n",
  1570. path);
  1571. return 0;
  1572. }
  1573. dt_addr = fdt_translate_address(fdt, node, reg);
  1574. if (addr != dt_addr) {
  1575. printf("Warning: U-Boot configured device %s at address %llu,\n"
  1576. "but the device tree has it address %llx.\n",
  1577. alias, addr, dt_addr);
  1578. return 0;
  1579. }
  1580. return 1;
  1581. }
  1582. /*
  1583. * Returns the base address of an SOC or PCI node
  1584. */
  1585. u64 fdt_get_base_address(const void *fdt, int node)
  1586. {
  1587. int size;
  1588. const fdt32_t *prop;
  1589. prop = fdt_getprop(fdt, node, "reg", &size);
  1590. return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR;
  1591. }
  1592. /*
  1593. * Read a property of size <prop_len>. Currently only supports 1 or 2 cells,
  1594. * or 3 cells specially for a PCI address.
  1595. */
  1596. static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
  1597. uint64_t *val, int cells)
  1598. {
  1599. const fdt32_t *prop32;
  1600. const unaligned_fdt64_t *prop64;
  1601. if ((cell_off + cells) > prop_len)
  1602. return -FDT_ERR_NOSPACE;
  1603. prop32 = &prop[cell_off];
  1604. /*
  1605. * Special handling for PCI address in PCI bus <ranges>
  1606. *
  1607. * PCI child address is made up of 3 cells. Advance the cell offset
  1608. * by 1 so that the PCI child address can be correctly read.
  1609. */
  1610. if (cells == 3)
  1611. cell_off += 1;
  1612. prop64 = (const fdt64_t *)&prop[cell_off];
  1613. switch (cells) {
  1614. case 1:
  1615. *val = fdt32_to_cpu(*prop32);
  1616. break;
  1617. case 2:
  1618. case 3:
  1619. *val = fdt64_to_cpu(*prop64);
  1620. break;
  1621. default:
  1622. return -FDT_ERR_NOSPACE;
  1623. }
  1624. return 0;
  1625. }
  1626. /**
  1627. * fdt_read_range - Read a node's n'th range property
  1628. *
  1629. * @fdt: ptr to device tree
  1630. * @node: offset of node
  1631. * @n: range index
  1632. * @child_addr: pointer to storage for the "child address" field
  1633. * @addr: pointer to storage for the CPU view translated physical start
  1634. * @len: pointer to storage for the range length
  1635. *
  1636. * Convenience function that reads and interprets a specific range out of
  1637. * a number of the "ranges" property array.
  1638. */
  1639. int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
  1640. uint64_t *addr, uint64_t *len)
  1641. {
  1642. int pnode = fdt_parent_offset(fdt, node);
  1643. const fdt32_t *ranges;
  1644. int pacells;
  1645. int acells;
  1646. int scells;
  1647. int ranges_len;
  1648. int cell = 0;
  1649. int r = 0;
  1650. /*
  1651. * The "ranges" property is an array of
  1652. * { <child address> <parent address> <size in child address space> }
  1653. *
  1654. * All 3 elements can span a diffent number of cells. Fetch their size.
  1655. */
  1656. pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
  1657. acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
  1658. scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
  1659. /* Now try to get the ranges property */
  1660. ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
  1661. if (!ranges)
  1662. return -FDT_ERR_NOTFOUND;
  1663. ranges_len /= sizeof(uint32_t);
  1664. /* Jump to the n'th entry */
  1665. cell = n * (pacells + acells + scells);
  1666. /* Read <child address> */
  1667. if (child_addr) {
  1668. r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
  1669. acells);
  1670. if (r)
  1671. return r;
  1672. }
  1673. cell += acells;
  1674. /* Read <parent address> */
  1675. if (addr)
  1676. *addr = fdt_translate_address(fdt, node, ranges + cell);
  1677. cell += pacells;
  1678. /* Read <size in child address space> */
  1679. if (len) {
  1680. r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
  1681. if (r)
  1682. return r;
  1683. }
  1684. return 0;
  1685. }
  1686. /**
  1687. * fdt_setup_simplefb_node - Fill and enable a simplefb node
  1688. *
  1689. * @fdt: ptr to device tree
  1690. * @node: offset of the simplefb node
  1691. * @base_address: framebuffer base address
  1692. * @width: width in pixels
  1693. * @height: height in pixels
  1694. * @stride: bytes per line
  1695. * @format: pixel format string
  1696. *
  1697. * Convenience function to fill and enable a simplefb node.
  1698. */
  1699. int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
  1700. u32 height, u32 stride, const char *format)
  1701. {
  1702. char name[32];
  1703. fdt32_t cells[4];
  1704. int i, addrc, sizec, ret;
  1705. fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
  1706. &addrc, &sizec);
  1707. i = 0;
  1708. if (addrc == 2)
  1709. cells[i++] = cpu_to_fdt32(base_address >> 32);
  1710. cells[i++] = cpu_to_fdt32(base_address);
  1711. if (sizec == 2)
  1712. cells[i++] = 0;
  1713. cells[i++] = cpu_to_fdt32(height * stride);
  1714. ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
  1715. if (ret < 0)
  1716. return ret;
  1717. snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
  1718. ret = fdt_set_name(fdt, node, name);
  1719. if (ret < 0)
  1720. return ret;
  1721. ret = fdt_setprop_u32(fdt, node, "width", width);
  1722. if (ret < 0)
  1723. return ret;
  1724. ret = fdt_setprop_u32(fdt, node, "height", height);
  1725. if (ret < 0)
  1726. return ret;
  1727. ret = fdt_setprop_u32(fdt, node, "stride", stride);
  1728. if (ret < 0)
  1729. return ret;
  1730. ret = fdt_setprop_string(fdt, node, "format", format);
  1731. if (ret < 0)
  1732. return ret;
  1733. ret = fdt_setprop_string(fdt, node, "status", "okay");
  1734. if (ret < 0)
  1735. return ret;
  1736. return 0;
  1737. }
  1738. /*
  1739. * Update native-mode in display-timings from display environment variable.
  1740. * The node to update are specified by path.
  1741. */
  1742. int fdt_fixup_display(void *blob, const char *path, const char *display)
  1743. {
  1744. int off, toff;
  1745. if (!display || !path)
  1746. return -FDT_ERR_NOTFOUND;
  1747. toff = fdt_path_offset(blob, path);
  1748. if (toff >= 0)
  1749. toff = fdt_subnode_offset(blob, toff, "display-timings");
  1750. if (toff < 0)
  1751. return toff;
  1752. for (off = fdt_first_subnode(blob, toff);
  1753. off >= 0;
  1754. off = fdt_next_subnode(blob, off)) {
  1755. uint32_t h = fdt_get_phandle(blob, off);
  1756. debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
  1757. fdt32_to_cpu(h));
  1758. if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
  1759. return fdt_setprop_u32(blob, toff, "native-mode", h);
  1760. }
  1761. return toff;
  1762. }
  1763. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  1764. /**
  1765. * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
  1766. *
  1767. * @fdt: ptr to device tree
  1768. * @fdto: ptr to device tree overlay
  1769. *
  1770. * Convenience function to apply an overlay and display helpful messages
  1771. * in the case of an error
  1772. */
  1773. int fdt_overlay_apply_verbose(void *fdt, void *fdto)
  1774. {
  1775. int err;
  1776. bool has_symbols;
  1777. err = fdt_path_offset(fdt, "/__symbols__");
  1778. has_symbols = err >= 0;
  1779. err = fdt_overlay_apply(fdt, fdto);
  1780. if (err < 0) {
  1781. printf("failed on fdt_overlay_apply(): %s\n",
  1782. fdt_strerror(err));
  1783. if (!has_symbols) {
  1784. printf("base fdt does did not have a /__symbols__ node\n");
  1785. printf("make sure you've compiled with -@\n");
  1786. }
  1787. }
  1788. return err;
  1789. }
  1790. #endif
  1791. /**
  1792. * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
  1793. *
  1794. * @blobp: Pointer to FDT pointer
  1795. * Return: 1 if OK, 0 if bad (in which case *blobp is set to NULL)
  1796. */
  1797. int fdt_valid(struct fdt_header **blobp)
  1798. {
  1799. const void *blob = *blobp;
  1800. int err;
  1801. if (!blob) {
  1802. printf("The address of the fdt is invalid (NULL).\n");
  1803. return 0;
  1804. }
  1805. err = fdt_check_header(blob);
  1806. if (err == 0)
  1807. return 1; /* valid */
  1808. if (err < 0) {
  1809. printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
  1810. /*
  1811. * Be more informative on bad version.
  1812. */
  1813. if (err == -FDT_ERR_BADVERSION) {
  1814. if (fdt_version(blob) <
  1815. FDT_FIRST_SUPPORTED_VERSION) {
  1816. printf(" - too old, fdt %d < %d",
  1817. fdt_version(blob),
  1818. FDT_FIRST_SUPPORTED_VERSION);
  1819. }
  1820. if (fdt_last_comp_version(blob) >
  1821. FDT_LAST_SUPPORTED_VERSION) {
  1822. printf(" - too new, fdt %d > %d",
  1823. fdt_version(blob),
  1824. FDT_LAST_SUPPORTED_VERSION);
  1825. }
  1826. }
  1827. printf("\n");
  1828. *blobp = NULL;
  1829. return 0;
  1830. }
  1831. return 1;
  1832. }