fdt_support.c 43 KB

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