fdt_support.c 43 KB

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