fdt_support.c 44 KB

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