fdt_support.c 44 KB

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