fdt_support.c 42 KB

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