fdt_support.c 44 KB

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