fdt_support.c 43 KB

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