fdt_support.c 43 KB

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