fdt_support.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  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. fdt_setprop_u64(blob, node, "load", load_addr);
  523. if (entry_point != -1)
  524. fdt_setprop_u64(blob, node, "entry", entry_point);
  525. fdt_setprop_u32(blob, node, "size", size);
  526. if (type)
  527. fdt_setprop_string(blob, node, "type", type);
  528. if (os)
  529. fdt_setprop_string(blob, node, "os", os);
  530. return node;
  531. }
  532. /* Resize the fdt to its actual size + a bit of padding */
  533. int fdt_shrink_to_minimum(void *blob, uint extrasize)
  534. {
  535. int i;
  536. uint64_t addr, size;
  537. int total, ret;
  538. uint actualsize;
  539. int fdt_memrsv = 0;
  540. if (!blob)
  541. return 0;
  542. total = fdt_num_mem_rsv(blob);
  543. for (i = 0; i < total; i++) {
  544. fdt_get_mem_rsv(blob, i, &addr, &size);
  545. if (addr == (uintptr_t)blob) {
  546. fdt_del_mem_rsv(blob, i);
  547. fdt_memrsv = 1;
  548. break;
  549. }
  550. }
  551. /*
  552. * Calculate the actual size of the fdt
  553. * plus the size needed for 5 fdt_add_mem_rsv, one
  554. * for the fdt itself and 4 for a possible initrd
  555. * ((initrd-start + initrd-end) * 2 (name & value))
  556. */
  557. actualsize = fdt_off_dt_strings(blob) +
  558. fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
  559. actualsize += extrasize;
  560. /* Make it so the fdt ends on a page boundary */
  561. actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
  562. actualsize = actualsize - ((uintptr_t)blob & 0xfff);
  563. /* Change the fdt header to reflect the correct size */
  564. fdt_set_totalsize(blob, actualsize);
  565. if (fdt_memrsv) {
  566. /* Add the new reservation */
  567. ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize);
  568. if (ret < 0)
  569. return ret;
  570. }
  571. return actualsize;
  572. }
  573. #ifdef CONFIG_PCI
  574. #define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
  575. #define FDT_PCI_PREFETCH (0x40000000)
  576. #define FDT_PCI_MEM32 (0x02000000)
  577. #define FDT_PCI_IO (0x01000000)
  578. #define FDT_PCI_MEM64 (0x03000000)
  579. int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
  580. int addrcell, sizecell, len, r;
  581. u32 *dma_range;
  582. /* sized based on pci addr cells, size-cells, & address-cells */
  583. u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
  584. addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
  585. sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
  586. dma_range = &dma_ranges[0];
  587. for (r = 0; r < hose->region_count; r++) {
  588. u64 bus_start, phys_start, size;
  589. /* skip if !PCI_REGION_SYS_MEMORY */
  590. if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
  591. continue;
  592. bus_start = (u64)hose->regions[r].bus_start;
  593. phys_start = (u64)hose->regions[r].phys_start;
  594. size = (u64)hose->regions[r].size;
  595. dma_range[0] = 0;
  596. if (size >= 0x100000000ull)
  597. dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64);
  598. else
  599. dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32);
  600. if (hose->regions[r].flags & PCI_REGION_PREFETCH)
  601. dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH);
  602. #ifdef CONFIG_SYS_PCI_64BIT
  603. dma_range[1] = cpu_to_fdt32(bus_start >> 32);
  604. #else
  605. dma_range[1] = 0;
  606. #endif
  607. dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff);
  608. if (addrcell == 2) {
  609. dma_range[3] = cpu_to_fdt32(phys_start >> 32);
  610. dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff);
  611. } else {
  612. dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff);
  613. }
  614. if (sizecell == 2) {
  615. dma_range[3 + addrcell + 0] =
  616. cpu_to_fdt32(size >> 32);
  617. dma_range[3 + addrcell + 1] =
  618. cpu_to_fdt32(size & 0xffffffff);
  619. } else {
  620. dma_range[3 + addrcell + 0] =
  621. cpu_to_fdt32(size & 0xffffffff);
  622. }
  623. dma_range += (3 + addrcell + sizecell);
  624. }
  625. len = dma_range - &dma_ranges[0];
  626. if (len)
  627. fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
  628. return 0;
  629. }
  630. #endif
  631. int fdt_increase_size(void *fdt, int add_len)
  632. {
  633. int newlen;
  634. newlen = fdt_totalsize(fdt) + add_len;
  635. /* Open in place with a new len */
  636. return fdt_open_into(fdt, fdt, newlen);
  637. }
  638. #ifdef CONFIG_FDT_FIXUP_PARTITIONS
  639. #include <jffs2/load_kernel.h>
  640. #include <mtd_node.h>
  641. static int fdt_del_subnodes(const void *blob, int parent_offset)
  642. {
  643. int off, ndepth;
  644. int ret;
  645. for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
  646. (off >= 0) && (ndepth > 0);
  647. off = fdt_next_node(blob, off, &ndepth)) {
  648. if (ndepth == 1) {
  649. debug("delete %s: offset: %x\n",
  650. fdt_get_name(blob, off, 0), off);
  651. ret = fdt_del_node((void *)blob, off);
  652. if (ret < 0) {
  653. printf("Can't delete node: %s\n",
  654. fdt_strerror(ret));
  655. return ret;
  656. } else {
  657. ndepth = 0;
  658. off = parent_offset;
  659. }
  660. }
  661. }
  662. return 0;
  663. }
  664. static int fdt_del_partitions(void *blob, int parent_offset)
  665. {
  666. const void *prop;
  667. int ndepth = 0;
  668. int off;
  669. int ret;
  670. off = fdt_next_node(blob, parent_offset, &ndepth);
  671. if (off > 0 && ndepth == 1) {
  672. prop = fdt_getprop(blob, off, "label", NULL);
  673. if (prop == NULL) {
  674. /*
  675. * Could not find label property, nand {}; node?
  676. * Check subnode, delete partitions there if any.
  677. */
  678. return fdt_del_partitions(blob, off);
  679. } else {
  680. ret = fdt_del_subnodes(blob, parent_offset);
  681. if (ret < 0) {
  682. printf("Can't remove subnodes: %s\n",
  683. fdt_strerror(ret));
  684. return ret;
  685. }
  686. }
  687. }
  688. return 0;
  689. }
  690. static int fdt_node_set_part_info(void *blob, int parent_offset,
  691. struct mtd_device *dev)
  692. {
  693. struct list_head *pentry;
  694. struct part_info *part;
  695. int off, ndepth = 0;
  696. int part_num, ret;
  697. int sizecell;
  698. char buf[64];
  699. ret = fdt_del_partitions(blob, parent_offset);
  700. if (ret < 0)
  701. return ret;
  702. /*
  703. * Check if size/address is 1 or 2 cells.
  704. * We assume #address-cells and #size-cells have same value.
  705. */
  706. sizecell = fdt_getprop_u32_default_node(blob, parent_offset,
  707. 0, "#size-cells", 1);
  708. /*
  709. * Check if it is nand {}; subnode, adjust
  710. * the offset in this case
  711. */
  712. off = fdt_next_node(blob, parent_offset, &ndepth);
  713. if (off > 0 && ndepth == 1)
  714. parent_offset = off;
  715. part_num = 0;
  716. list_for_each_prev(pentry, &dev->parts) {
  717. int newoff;
  718. part = list_entry(pentry, struct part_info, link);
  719. debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
  720. part_num, part->name, part->size,
  721. part->offset, part->mask_flags);
  722. sprintf(buf, "partition@%llx", part->offset);
  723. add_sub:
  724. ret = fdt_add_subnode(blob, parent_offset, buf);
  725. if (ret == -FDT_ERR_NOSPACE) {
  726. ret = fdt_increase_size(blob, 512);
  727. if (!ret)
  728. goto add_sub;
  729. else
  730. goto err_size;
  731. } else if (ret < 0) {
  732. printf("Can't add partition node: %s\n",
  733. fdt_strerror(ret));
  734. return ret;
  735. }
  736. newoff = ret;
  737. /* Check MTD_WRITEABLE_CMD flag */
  738. if (part->mask_flags & 1) {
  739. add_ro:
  740. ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
  741. if (ret == -FDT_ERR_NOSPACE) {
  742. ret = fdt_increase_size(blob, 512);
  743. if (!ret)
  744. goto add_ro;
  745. else
  746. goto err_size;
  747. } else if (ret < 0)
  748. goto err_prop;
  749. }
  750. add_reg:
  751. if (sizecell == 2) {
  752. ret = fdt_setprop_u64(blob, newoff,
  753. "reg", part->offset);
  754. if (!ret)
  755. ret = fdt_appendprop_u64(blob, newoff,
  756. "reg", part->size);
  757. } else {
  758. ret = fdt_setprop_u32(blob, newoff,
  759. "reg", part->offset);
  760. if (!ret)
  761. ret = fdt_appendprop_u32(blob, newoff,
  762. "reg", part->size);
  763. }
  764. if (ret == -FDT_ERR_NOSPACE) {
  765. ret = fdt_increase_size(blob, 512);
  766. if (!ret)
  767. goto add_reg;
  768. else
  769. goto err_size;
  770. } else if (ret < 0)
  771. goto err_prop;
  772. add_label:
  773. ret = fdt_setprop_string(blob, newoff, "label", part->name);
  774. if (ret == -FDT_ERR_NOSPACE) {
  775. ret = fdt_increase_size(blob, 512);
  776. if (!ret)
  777. goto add_label;
  778. else
  779. goto err_size;
  780. } else if (ret < 0)
  781. goto err_prop;
  782. part_num++;
  783. }
  784. return 0;
  785. err_size:
  786. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  787. return ret;
  788. err_prop:
  789. printf("Can't add property: %s\n", fdt_strerror(ret));
  790. return ret;
  791. }
  792. /*
  793. * Update partitions in nor/nand nodes using info from
  794. * mtdparts environment variable. The nodes to update are
  795. * specified by node_info structure which contains mtd device
  796. * type and compatible string: E. g. the board code in
  797. * ft_board_setup() could use:
  798. *
  799. * struct node_info nodes[] = {
  800. * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
  801. * { "cfi-flash", MTD_DEV_TYPE_NOR, },
  802. * };
  803. *
  804. * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
  805. */
  806. void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
  807. int node_info_size)
  808. {
  809. struct mtd_device *dev;
  810. int i, idx;
  811. int noff;
  812. bool inited = false;
  813. for (i = 0; i < node_info_size; i++) {
  814. idx = 0;
  815. noff = -1;
  816. while ((noff = fdt_node_offset_by_compatible(blob, noff,
  817. node_info[i].compat)) >= 0) {
  818. const char *prop;
  819. prop = fdt_getprop(blob, noff, "status", NULL);
  820. if (prop && !strcmp(prop, "disabled"))
  821. continue;
  822. debug("%s: %s, mtd dev type %d\n",
  823. fdt_get_name(blob, noff, 0),
  824. node_info[i].compat, node_info[i].type);
  825. if (!inited) {
  826. if (mtdparts_init() != 0)
  827. return;
  828. inited = true;
  829. }
  830. dev = device_find(node_info[i].type, idx++);
  831. if (dev) {
  832. if (fdt_node_set_part_info(blob, noff, dev))
  833. return; /* return on error */
  834. }
  835. }
  836. }
  837. }
  838. #endif
  839. void fdt_del_node_and_alias(void *blob, const char *alias)
  840. {
  841. int off = fdt_path_offset(blob, alias);
  842. if (off < 0)
  843. return;
  844. fdt_del_node(blob, off);
  845. off = fdt_path_offset(blob, "/aliases");
  846. fdt_delprop(blob, off, alias);
  847. }
  848. /* Max address size we deal with */
  849. #define OF_MAX_ADDR_CELLS 4
  850. #define OF_BAD_ADDR FDT_ADDR_T_NONE
  851. #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
  852. (ns) > 0)
  853. /* Debug utility */
  854. #ifdef DEBUG
  855. static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
  856. {
  857. printf("%s", s);
  858. while(na--)
  859. printf(" %08x", *(addr++));
  860. printf("\n");
  861. }
  862. #else
  863. static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
  864. #endif
  865. /**
  866. * struct of_bus - Callbacks for bus specific translators
  867. * @name: A string used to identify this bus in debug output.
  868. * @addresses: The name of the DT property from which addresses are
  869. * to be read, typically "reg".
  870. * @match: Return non-zero if the node whose parent is at
  871. * parentoffset in the FDT blob corresponds to a bus
  872. * of this type, otherwise return zero. If NULL a match
  873. * is assumed.
  874. * @count_cells:Count how many cells (be32 values) a node whose parent
  875. * is at parentoffset in the FDT blob will require to
  876. * represent its address (written to *addrc) & size
  877. * (written to *sizec).
  878. * @map: Map the address addr from the address space of this
  879. * bus to that of its parent, making use of the ranges
  880. * read from DT to an array at range. na and ns are the
  881. * number of cells (be32 values) used to hold and address
  882. * or size, respectively, for this bus. pna is the number
  883. * of cells used to hold an address for the parent bus.
  884. * Returns the address in the address space of the parent
  885. * bus.
  886. * @translate: Update the value of the address cells at addr within an
  887. * FDT by adding offset to it. na specifies the number of
  888. * cells used to hold the address being translated. Returns
  889. * zero on success, non-zero on error.
  890. *
  891. * Each bus type will include a struct of_bus in the of_busses array,
  892. * providing implementations of some or all of the functions used to
  893. * match the bus & handle address translation for its children.
  894. */
  895. struct of_bus {
  896. const char *name;
  897. const char *addresses;
  898. int (*match)(const void *blob, int parentoffset);
  899. void (*count_cells)(const void *blob, int parentoffset,
  900. int *addrc, int *sizec);
  901. u64 (*map)(fdt32_t *addr, const fdt32_t *range,
  902. int na, int ns, int pna);
  903. int (*translate)(fdt32_t *addr, u64 offset, int na);
  904. };
  905. /* Default translator (generic bus) */
  906. void fdt_support_default_count_cells(const void *blob, int parentoffset,
  907. int *addrc, int *sizec)
  908. {
  909. const fdt32_t *prop;
  910. if (addrc)
  911. *addrc = fdt_address_cells(blob, parentoffset);
  912. if (sizec) {
  913. prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
  914. if (prop)
  915. *sizec = be32_to_cpup(prop);
  916. else
  917. *sizec = 1;
  918. }
  919. }
  920. static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
  921. int na, int ns, int pna)
  922. {
  923. u64 cp, s, da;
  924. cp = fdt_read_number(range, na);
  925. s = fdt_read_number(range + na + pna, ns);
  926. da = fdt_read_number(addr, na);
  927. debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
  928. if (da < cp || da >= (cp + s))
  929. return OF_BAD_ADDR;
  930. return da - cp;
  931. }
  932. static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
  933. {
  934. u64 a = fdt_read_number(addr, na);
  935. memset(addr, 0, na * 4);
  936. a += offset;
  937. if (na > 1)
  938. addr[na - 2] = cpu_to_fdt32(a >> 32);
  939. addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
  940. return 0;
  941. }
  942. #ifdef CONFIG_OF_ISA_BUS
  943. /* ISA bus translator */
  944. static int of_bus_isa_match(const void *blob, int parentoffset)
  945. {
  946. const char *name;
  947. name = fdt_get_name(blob, parentoffset, NULL);
  948. if (!name)
  949. return 0;
  950. return !strcmp(name, "isa");
  951. }
  952. static void of_bus_isa_count_cells(const void *blob, int parentoffset,
  953. int *addrc, int *sizec)
  954. {
  955. if (addrc)
  956. *addrc = 2;
  957. if (sizec)
  958. *sizec = 1;
  959. }
  960. static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
  961. int na, int ns, int pna)
  962. {
  963. u64 cp, s, da;
  964. /* Check address type match */
  965. if ((addr[0] ^ range[0]) & cpu_to_be32(1))
  966. return OF_BAD_ADDR;
  967. cp = fdt_read_number(range + 1, na - 1);
  968. s = fdt_read_number(range + na + pna, ns);
  969. da = fdt_read_number(addr + 1, na - 1);
  970. debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
  971. if (da < cp || da >= (cp + s))
  972. return OF_BAD_ADDR;
  973. return da - cp;
  974. }
  975. static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
  976. {
  977. return of_bus_default_translate(addr + 1, offset, na - 1);
  978. }
  979. #endif /* CONFIG_OF_ISA_BUS */
  980. /* Array of bus specific translators */
  981. static struct of_bus of_busses[] = {
  982. #ifdef CONFIG_OF_ISA_BUS
  983. /* ISA */
  984. {
  985. .name = "isa",
  986. .addresses = "reg",
  987. .match = of_bus_isa_match,
  988. .count_cells = of_bus_isa_count_cells,
  989. .map = of_bus_isa_map,
  990. .translate = of_bus_isa_translate,
  991. },
  992. #endif /* CONFIG_OF_ISA_BUS */
  993. /* Default */
  994. {
  995. .name = "default",
  996. .addresses = "reg",
  997. .count_cells = fdt_support_default_count_cells,
  998. .map = of_bus_default_map,
  999. .translate = of_bus_default_translate,
  1000. },
  1001. };
  1002. static struct of_bus *of_match_bus(const void *blob, int parentoffset)
  1003. {
  1004. struct of_bus *bus;
  1005. if (ARRAY_SIZE(of_busses) == 1)
  1006. return of_busses;
  1007. for (bus = of_busses; bus; bus++) {
  1008. if (!bus->match || bus->match(blob, parentoffset))
  1009. return bus;
  1010. }
  1011. /*
  1012. * We should always have matched the default bus at least, since
  1013. * it has a NULL match field. If we didn't then it somehow isn't
  1014. * in the of_busses array or something equally catastrophic has
  1015. * gone wrong.
  1016. */
  1017. assert(0);
  1018. return NULL;
  1019. }
  1020. static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
  1021. struct of_bus *pbus, fdt32_t *addr,
  1022. int na, int ns, int pna, const char *rprop)
  1023. {
  1024. const fdt32_t *ranges;
  1025. int rlen;
  1026. int rone;
  1027. u64 offset = OF_BAD_ADDR;
  1028. /* Normally, an absence of a "ranges" property means we are
  1029. * crossing a non-translatable boundary, and thus the addresses
  1030. * below the current not cannot be converted to CPU physical ones.
  1031. * Unfortunately, while this is very clear in the spec, it's not
  1032. * what Apple understood, and they do have things like /uni-n or
  1033. * /ht nodes with no "ranges" property and a lot of perfectly
  1034. * useable mapped devices below them. Thus we treat the absence of
  1035. * "ranges" as equivalent to an empty "ranges" property which means
  1036. * a 1:1 translation at that level. It's up to the caller not to try
  1037. * to translate addresses that aren't supposed to be translated in
  1038. * the first place. --BenH.
  1039. */
  1040. ranges = fdt_getprop(blob, parent, rprop, &rlen);
  1041. if (ranges == NULL || rlen == 0) {
  1042. offset = fdt_read_number(addr, na);
  1043. memset(addr, 0, pna * 4);
  1044. debug("OF: no ranges, 1:1 translation\n");
  1045. goto finish;
  1046. }
  1047. debug("OF: walking ranges...\n");
  1048. /* Now walk through the ranges */
  1049. rlen /= 4;
  1050. rone = na + pna + ns;
  1051. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  1052. offset = bus->map(addr, ranges, na, ns, pna);
  1053. if (offset != OF_BAD_ADDR)
  1054. break;
  1055. }
  1056. if (offset == OF_BAD_ADDR) {
  1057. debug("OF: not found !\n");
  1058. return 1;
  1059. }
  1060. memcpy(addr, ranges + na, 4 * pna);
  1061. finish:
  1062. of_dump_addr("OF: parent translation for:", addr, pna);
  1063. debug("OF: with offset: %llu\n", offset);
  1064. /* Translate it into parent bus space */
  1065. return pbus->translate(addr, offset, pna);
  1066. }
  1067. /*
  1068. * Translate an address from the device-tree into a CPU physical address,
  1069. * this walks up the tree and applies the various bus mappings on the
  1070. * way.
  1071. *
  1072. * Note: We consider that crossing any level with #size-cells == 0 to mean
  1073. * that translation is impossible (that is we are not dealing with a value
  1074. * that can be mapped to a cpu physical address). This is not really specified
  1075. * that way, but this is traditionally the way IBM at least do things
  1076. */
  1077. static u64 __of_translate_address(const void *blob, int node_offset,
  1078. const fdt32_t *in_addr, const char *rprop)
  1079. {
  1080. int parent;
  1081. struct of_bus *bus, *pbus;
  1082. fdt32_t addr[OF_MAX_ADDR_CELLS];
  1083. int na, ns, pna, pns;
  1084. u64 result = OF_BAD_ADDR;
  1085. debug("OF: ** translation for device %s **\n",
  1086. fdt_get_name(blob, node_offset, NULL));
  1087. /* Get parent & match bus type */
  1088. parent = fdt_parent_offset(blob, node_offset);
  1089. if (parent < 0)
  1090. goto bail;
  1091. bus = of_match_bus(blob, parent);
  1092. /* Cound address cells & copy address locally */
  1093. bus->count_cells(blob, parent, &na, &ns);
  1094. if (!OF_CHECK_COUNTS(na, ns)) {
  1095. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  1096. fdt_get_name(blob, node_offset, NULL));
  1097. goto bail;
  1098. }
  1099. memcpy(addr, in_addr, na * 4);
  1100. debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  1101. bus->name, na, ns, fdt_get_name(blob, parent, NULL));
  1102. of_dump_addr("OF: translating address:", addr, na);
  1103. /* Translate */
  1104. for (;;) {
  1105. /* Switch to parent bus */
  1106. node_offset = parent;
  1107. parent = fdt_parent_offset(blob, node_offset);
  1108. /* If root, we have finished */
  1109. if (parent < 0) {
  1110. debug("OF: reached root node\n");
  1111. result = fdt_read_number(addr, na);
  1112. break;
  1113. }
  1114. /* Get new parent bus and counts */
  1115. pbus = of_match_bus(blob, parent);
  1116. pbus->count_cells(blob, parent, &pna, &pns);
  1117. if (!OF_CHECK_COUNTS(pna, pns)) {
  1118. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  1119. fdt_get_name(blob, node_offset, NULL));
  1120. break;
  1121. }
  1122. debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  1123. pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
  1124. /* Apply bus translation */
  1125. if (of_translate_one(blob, node_offset, bus, pbus,
  1126. addr, na, ns, pna, rprop))
  1127. break;
  1128. /* Complete the move up one level */
  1129. na = pna;
  1130. ns = pns;
  1131. bus = pbus;
  1132. of_dump_addr("OF: one level translation:", addr, na);
  1133. }
  1134. bail:
  1135. return result;
  1136. }
  1137. u64 fdt_translate_address(const void *blob, int node_offset,
  1138. const fdt32_t *in_addr)
  1139. {
  1140. return __of_translate_address(blob, node_offset, in_addr, "ranges");
  1141. }
  1142. u64 fdt_translate_dma_address(const void *blob, int node_offset,
  1143. const fdt32_t *in_addr)
  1144. {
  1145. return __of_translate_address(blob, node_offset, in_addr, "dma-ranges");
  1146. }
  1147. /**
  1148. * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
  1149. * who's reg property matches a physical cpu address
  1150. *
  1151. * @blob: ptr to device tree
  1152. * @compat: compatiable string to match
  1153. * @compat_off: property name
  1154. *
  1155. */
  1156. int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
  1157. phys_addr_t compat_off)
  1158. {
  1159. int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
  1160. while (off != -FDT_ERR_NOTFOUND) {
  1161. const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
  1162. if (reg) {
  1163. if (compat_off == fdt_translate_address(blob, off, reg))
  1164. return off;
  1165. }
  1166. off = fdt_node_offset_by_compatible(blob, off, compat);
  1167. }
  1168. return -FDT_ERR_NOTFOUND;
  1169. }
  1170. /**
  1171. * fdt_alloc_phandle: Return next free phandle value
  1172. *
  1173. * @blob: ptr to device tree
  1174. */
  1175. int fdt_alloc_phandle(void *blob)
  1176. {
  1177. int offset;
  1178. uint32_t phandle = 0;
  1179. for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
  1180. offset = fdt_next_node(blob, offset, NULL)) {
  1181. phandle = max(phandle, fdt_get_phandle(blob, offset));
  1182. }
  1183. return phandle + 1;
  1184. }
  1185. /*
  1186. * fdt_set_phandle: Create a phandle property for the given node
  1187. *
  1188. * @fdt: ptr to device tree
  1189. * @nodeoffset: node to update
  1190. * @phandle: phandle value to set (must be unique)
  1191. */
  1192. int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
  1193. {
  1194. int ret;
  1195. #ifdef DEBUG
  1196. int off = fdt_node_offset_by_phandle(fdt, phandle);
  1197. if ((off >= 0) && (off != nodeoffset)) {
  1198. char buf[64];
  1199. fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
  1200. printf("Trying to update node %s with phandle %u ",
  1201. buf, phandle);
  1202. fdt_get_path(fdt, off, buf, sizeof(buf));
  1203. printf("that already exists in node %s.\n", buf);
  1204. return -FDT_ERR_BADPHANDLE;
  1205. }
  1206. #endif
  1207. ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
  1208. if (ret < 0)
  1209. return ret;
  1210. /*
  1211. * For now, also set the deprecated "linux,phandle" property, so that we
  1212. * don't break older kernels.
  1213. */
  1214. ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
  1215. return ret;
  1216. }
  1217. /*
  1218. * fdt_create_phandle: Create a phandle property for the given node
  1219. *
  1220. * @fdt: ptr to device tree
  1221. * @nodeoffset: node to update
  1222. */
  1223. unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
  1224. {
  1225. /* see if there is a phandle already */
  1226. int phandle = fdt_get_phandle(fdt, nodeoffset);
  1227. /* if we got 0, means no phandle so create one */
  1228. if (phandle == 0) {
  1229. int ret;
  1230. phandle = fdt_alloc_phandle(fdt);
  1231. ret = fdt_set_phandle(fdt, nodeoffset, phandle);
  1232. if (ret < 0) {
  1233. printf("Can't set phandle %u: %s\n", phandle,
  1234. fdt_strerror(ret));
  1235. return 0;
  1236. }
  1237. }
  1238. return phandle;
  1239. }
  1240. /*
  1241. * fdt_set_node_status: Set status for the given node
  1242. *
  1243. * @fdt: ptr to device tree
  1244. * @nodeoffset: node to update
  1245. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
  1246. * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
  1247. * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
  1248. */
  1249. int fdt_set_node_status(void *fdt, int nodeoffset,
  1250. enum fdt_status status, unsigned int error_code)
  1251. {
  1252. char buf[16];
  1253. int ret = 0;
  1254. if (nodeoffset < 0)
  1255. return nodeoffset;
  1256. switch (status) {
  1257. case FDT_STATUS_OKAY:
  1258. ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
  1259. break;
  1260. case FDT_STATUS_DISABLED:
  1261. ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
  1262. break;
  1263. case FDT_STATUS_FAIL:
  1264. ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
  1265. break;
  1266. case FDT_STATUS_FAIL_ERROR_CODE:
  1267. sprintf(buf, "fail-%d", error_code);
  1268. ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
  1269. break;
  1270. default:
  1271. printf("Invalid fdt status: %x\n", status);
  1272. ret = -1;
  1273. break;
  1274. }
  1275. return ret;
  1276. }
  1277. /*
  1278. * fdt_set_status_by_alias: Set status for the given node given an alias
  1279. *
  1280. * @fdt: ptr to device tree
  1281. * @alias: alias of node to update
  1282. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
  1283. * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
  1284. * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
  1285. */
  1286. int fdt_set_status_by_alias(void *fdt, const char* alias,
  1287. enum fdt_status status, unsigned int error_code)
  1288. {
  1289. int offset = fdt_path_offset(fdt, alias);
  1290. return fdt_set_node_status(fdt, offset, status, error_code);
  1291. }
  1292. #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
  1293. int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
  1294. {
  1295. int noff;
  1296. int ret;
  1297. noff = fdt_node_offset_by_compatible(blob, -1, compat);
  1298. if (noff != -FDT_ERR_NOTFOUND) {
  1299. debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
  1300. add_edid:
  1301. ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
  1302. if (ret == -FDT_ERR_NOSPACE) {
  1303. ret = fdt_increase_size(blob, 512);
  1304. if (!ret)
  1305. goto add_edid;
  1306. else
  1307. goto err_size;
  1308. } else if (ret < 0) {
  1309. printf("Can't add property: %s\n", fdt_strerror(ret));
  1310. return ret;
  1311. }
  1312. }
  1313. return 0;
  1314. err_size:
  1315. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  1316. return ret;
  1317. }
  1318. #endif
  1319. /*
  1320. * Verify the physical address of device tree node for a given alias
  1321. *
  1322. * This function locates the device tree node of a given alias, and then
  1323. * verifies that the physical address of that device matches the given
  1324. * parameter. It displays a message if there is a mismatch.
  1325. *
  1326. * Returns 1 on success, 0 on failure
  1327. */
  1328. int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
  1329. {
  1330. const char *path;
  1331. const fdt32_t *reg;
  1332. int node, len;
  1333. u64 dt_addr;
  1334. path = fdt_getprop(fdt, anode, alias, NULL);
  1335. if (!path) {
  1336. /* If there's no such alias, then it's not a failure */
  1337. return 1;
  1338. }
  1339. node = fdt_path_offset(fdt, path);
  1340. if (node < 0) {
  1341. printf("Warning: device tree alias '%s' points to invalid "
  1342. "node %s.\n", alias, path);
  1343. return 0;
  1344. }
  1345. reg = fdt_getprop(fdt, node, "reg", &len);
  1346. if (!reg) {
  1347. printf("Warning: device tree node '%s' has no address.\n",
  1348. path);
  1349. return 0;
  1350. }
  1351. dt_addr = fdt_translate_address(fdt, node, reg);
  1352. if (addr != dt_addr) {
  1353. printf("Warning: U-Boot configured device %s at address %llu,\n"
  1354. "but the device tree has it address %llx.\n",
  1355. alias, addr, dt_addr);
  1356. return 0;
  1357. }
  1358. return 1;
  1359. }
  1360. /*
  1361. * Returns the base address of an SOC or PCI node
  1362. */
  1363. u64 fdt_get_base_address(const void *fdt, int node)
  1364. {
  1365. int size;
  1366. const fdt32_t *prop;
  1367. prop = fdt_getprop(fdt, node, "reg", &size);
  1368. return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR;
  1369. }
  1370. /*
  1371. * Read a property of size <prop_len>. Currently only supports 1 or 2 cells.
  1372. */
  1373. static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
  1374. uint64_t *val, int cells)
  1375. {
  1376. const fdt32_t *prop32 = &prop[cell_off];
  1377. const unaligned_fdt64_t *prop64 = (const fdt64_t *)&prop[cell_off];
  1378. if ((cell_off + cells) > prop_len)
  1379. return -FDT_ERR_NOSPACE;
  1380. switch (cells) {
  1381. case 1:
  1382. *val = fdt32_to_cpu(*prop32);
  1383. break;
  1384. case 2:
  1385. *val = fdt64_to_cpu(*prop64);
  1386. break;
  1387. default:
  1388. return -FDT_ERR_NOSPACE;
  1389. }
  1390. return 0;
  1391. }
  1392. /**
  1393. * fdt_read_range - Read a node's n'th range property
  1394. *
  1395. * @fdt: ptr to device tree
  1396. * @node: offset of node
  1397. * @n: range index
  1398. * @child_addr: pointer to storage for the "child address" field
  1399. * @addr: pointer to storage for the CPU view translated physical start
  1400. * @len: pointer to storage for the range length
  1401. *
  1402. * Convenience function that reads and interprets a specific range out of
  1403. * a number of the "ranges" property array.
  1404. */
  1405. int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
  1406. uint64_t *addr, uint64_t *len)
  1407. {
  1408. int pnode = fdt_parent_offset(fdt, node);
  1409. const fdt32_t *ranges;
  1410. int pacells;
  1411. int acells;
  1412. int scells;
  1413. int ranges_len;
  1414. int cell = 0;
  1415. int r = 0;
  1416. /*
  1417. * The "ranges" property is an array of
  1418. * { <child address> <parent address> <size in child address space> }
  1419. *
  1420. * All 3 elements can span a diffent number of cells. Fetch their size.
  1421. */
  1422. pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
  1423. acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
  1424. scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
  1425. /* Now try to get the ranges property */
  1426. ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
  1427. if (!ranges)
  1428. return -FDT_ERR_NOTFOUND;
  1429. ranges_len /= sizeof(uint32_t);
  1430. /* Jump to the n'th entry */
  1431. cell = n * (pacells + acells + scells);
  1432. /* Read <child address> */
  1433. if (child_addr) {
  1434. r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
  1435. acells);
  1436. if (r)
  1437. return r;
  1438. }
  1439. cell += acells;
  1440. /* Read <parent address> */
  1441. if (addr)
  1442. *addr = fdt_translate_address(fdt, node, ranges + cell);
  1443. cell += pacells;
  1444. /* Read <size in child address space> */
  1445. if (len) {
  1446. r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
  1447. if (r)
  1448. return r;
  1449. }
  1450. return 0;
  1451. }
  1452. /**
  1453. * fdt_setup_simplefb_node - Fill and enable a simplefb node
  1454. *
  1455. * @fdt: ptr to device tree
  1456. * @node: offset of the simplefb node
  1457. * @base_address: framebuffer base address
  1458. * @width: width in pixels
  1459. * @height: height in pixels
  1460. * @stride: bytes per line
  1461. * @format: pixel format string
  1462. *
  1463. * Convenience function to fill and enable a simplefb node.
  1464. */
  1465. int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
  1466. u32 height, u32 stride, const char *format)
  1467. {
  1468. char name[32];
  1469. fdt32_t cells[4];
  1470. int i, addrc, sizec, ret;
  1471. fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
  1472. &addrc, &sizec);
  1473. i = 0;
  1474. if (addrc == 2)
  1475. cells[i++] = cpu_to_fdt32(base_address >> 32);
  1476. cells[i++] = cpu_to_fdt32(base_address);
  1477. if (sizec == 2)
  1478. cells[i++] = 0;
  1479. cells[i++] = cpu_to_fdt32(height * stride);
  1480. ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
  1481. if (ret < 0)
  1482. return ret;
  1483. snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
  1484. ret = fdt_set_name(fdt, node, name);
  1485. if (ret < 0)
  1486. return ret;
  1487. ret = fdt_setprop_u32(fdt, node, "width", width);
  1488. if (ret < 0)
  1489. return ret;
  1490. ret = fdt_setprop_u32(fdt, node, "height", height);
  1491. if (ret < 0)
  1492. return ret;
  1493. ret = fdt_setprop_u32(fdt, node, "stride", stride);
  1494. if (ret < 0)
  1495. return ret;
  1496. ret = fdt_setprop_string(fdt, node, "format", format);
  1497. if (ret < 0)
  1498. return ret;
  1499. ret = fdt_setprop_string(fdt, node, "status", "okay");
  1500. if (ret < 0)
  1501. return ret;
  1502. return 0;
  1503. }
  1504. /*
  1505. * Update native-mode in display-timings from display environment variable.
  1506. * The node to update are specified by path.
  1507. */
  1508. int fdt_fixup_display(void *blob, const char *path, const char *display)
  1509. {
  1510. int off, toff;
  1511. if (!display || !path)
  1512. return -FDT_ERR_NOTFOUND;
  1513. toff = fdt_path_offset(blob, path);
  1514. if (toff >= 0)
  1515. toff = fdt_subnode_offset(blob, toff, "display-timings");
  1516. if (toff < 0)
  1517. return toff;
  1518. for (off = fdt_first_subnode(blob, toff);
  1519. off >= 0;
  1520. off = fdt_next_subnode(blob, off)) {
  1521. uint32_t h = fdt_get_phandle(blob, off);
  1522. debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
  1523. fdt32_to_cpu(h));
  1524. if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
  1525. return fdt_setprop_u32(blob, toff, "native-mode", h);
  1526. }
  1527. return toff;
  1528. }
  1529. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  1530. /**
  1531. * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
  1532. *
  1533. * @fdt: ptr to device tree
  1534. * @fdto: ptr to device tree overlay
  1535. *
  1536. * Convenience function to apply an overlay and display helpful messages
  1537. * in the case of an error
  1538. */
  1539. int fdt_overlay_apply_verbose(void *fdt, void *fdto)
  1540. {
  1541. int err;
  1542. bool has_symbols;
  1543. err = fdt_path_offset(fdt, "/__symbols__");
  1544. has_symbols = err >= 0;
  1545. err = fdt_overlay_apply(fdt, fdto);
  1546. if (err < 0) {
  1547. printf("failed on fdt_overlay_apply(): %s\n",
  1548. fdt_strerror(err));
  1549. if (!has_symbols) {
  1550. printf("base fdt does did not have a /__symbols__ node\n");
  1551. printf("make sure you've compiled with -@\n");
  1552. }
  1553. }
  1554. return err;
  1555. }
  1556. #endif