fdt_support.c 50 KB

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