fdt_support.c 46 KB

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