fdt_support.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  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. #define MEMORY_BANKS_MAX 4
  351. int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
  352. {
  353. int err, nodeoffset;
  354. int len, i;
  355. u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
  356. if (banks > MEMORY_BANKS_MAX) {
  357. printf("%s: num banks %d exceeds hardcoded limit %d."
  358. " Recompile with higher MEMORY_BANKS_MAX?\n",
  359. __FUNCTION__, banks, MEMORY_BANKS_MAX);
  360. return -1;
  361. }
  362. err = fdt_check_header(blob);
  363. if (err < 0) {
  364. printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
  365. return err;
  366. }
  367. /* find or create "/memory" node. */
  368. nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
  369. if (nodeoffset < 0)
  370. return nodeoffset;
  371. err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
  372. sizeof("memory"));
  373. if (err < 0) {
  374. printf("WARNING: could not set %s %s.\n", "device_type",
  375. fdt_strerror(err));
  376. return err;
  377. }
  378. for (i = 0; i < banks; i++) {
  379. if (start[i] == 0 && size[i] == 0)
  380. break;
  381. }
  382. banks = i;
  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. static 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. static 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, const struct node_info *node_info,
  762. int node_info_size)
  763. {
  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,
  772. node_info[i].compat);
  773. while (noff != -FDT_ERR_NOTFOUND) {
  774. debug("%s: %s, mtd dev type %d\n",
  775. fdt_get_name(blob, noff, 0),
  776. node_info[i].compat, node_info[i].type);
  777. dev = device_find(node_info[i].type, idx++);
  778. if (dev) {
  779. if (fdt_node_set_part_info(blob, noff, dev))
  780. return; /* return on error */
  781. }
  782. /* Jump to next flash node */
  783. noff = fdt_node_offset_by_compatible(blob, noff,
  784. node_info[i].compat);
  785. }
  786. }
  787. }
  788. #endif
  789. void fdt_del_node_and_alias(void *blob, const char *alias)
  790. {
  791. int off = fdt_path_offset(blob, alias);
  792. if (off < 0)
  793. return;
  794. fdt_del_node(blob, off);
  795. off = fdt_path_offset(blob, "/aliases");
  796. fdt_delprop(blob, off, alias);
  797. }
  798. /* Max address size we deal with */
  799. #define OF_MAX_ADDR_CELLS 4
  800. #define OF_BAD_ADDR FDT_ADDR_T_NONE
  801. #define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
  802. (ns) > 0)
  803. /* Debug utility */
  804. #ifdef DEBUG
  805. static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
  806. {
  807. printf("%s", s);
  808. while(na--)
  809. printf(" %08x", *(addr++));
  810. printf("\n");
  811. }
  812. #else
  813. static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
  814. #endif
  815. /**
  816. * struct of_bus - Callbacks for bus specific translators
  817. * @name: A string used to identify this bus in debug output.
  818. * @addresses: The name of the DT property from which addresses are
  819. * to be read, typically "reg".
  820. * @match: Return non-zero if the node whose parent is at
  821. * parentoffset in the FDT blob corresponds to a bus
  822. * of this type, otherwise return zero. If NULL a match
  823. * is assumed.
  824. * @count_cells:Count how many cells (be32 values) a node whose parent
  825. * is at parentoffset in the FDT blob will require to
  826. * represent its address (written to *addrc) & size
  827. * (written to *sizec).
  828. * @map: Map the address addr from the address space of this
  829. * bus to that of its parent, making use of the ranges
  830. * read from DT to an array at range. na and ns are the
  831. * number of cells (be32 values) used to hold and address
  832. * or size, respectively, for this bus. pna is the number
  833. * of cells used to hold an address for the parent bus.
  834. * Returns the address in the address space of the parent
  835. * bus.
  836. * @translate: Update the value of the address cells at addr within an
  837. * FDT by adding offset to it. na specifies the number of
  838. * cells used to hold the address being translated. Returns
  839. * zero on success, non-zero on error.
  840. *
  841. * Each bus type will include a struct of_bus in the of_busses array,
  842. * providing implementations of some or all of the functions used to
  843. * match the bus & handle address translation for its children.
  844. */
  845. struct of_bus {
  846. const char *name;
  847. const char *addresses;
  848. int (*match)(const void *blob, int parentoffset);
  849. void (*count_cells)(const void *blob, int parentoffset,
  850. int *addrc, int *sizec);
  851. u64 (*map)(fdt32_t *addr, const fdt32_t *range,
  852. int na, int ns, int pna);
  853. int (*translate)(fdt32_t *addr, u64 offset, int na);
  854. };
  855. /* Default translator (generic bus) */
  856. void fdt_support_default_count_cells(const void *blob, int parentoffset,
  857. int *addrc, int *sizec)
  858. {
  859. const fdt32_t *prop;
  860. if (addrc)
  861. *addrc = fdt_address_cells(blob, parentoffset);
  862. if (sizec) {
  863. prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
  864. if (prop)
  865. *sizec = be32_to_cpup(prop);
  866. else
  867. *sizec = 1;
  868. }
  869. }
  870. static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
  871. int na, int ns, int pna)
  872. {
  873. u64 cp, s, da;
  874. cp = fdt_read_number(range, na);
  875. s = fdt_read_number(range + na + pna, ns);
  876. da = fdt_read_number(addr, na);
  877. debug("OF: default map, cp=%" PRIu64 ", s=%" PRIu64
  878. ", da=%" PRIu64 "\n", cp, s, da);
  879. if (da < cp || da >= (cp + s))
  880. return OF_BAD_ADDR;
  881. return da - cp;
  882. }
  883. static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
  884. {
  885. u64 a = fdt_read_number(addr, na);
  886. memset(addr, 0, na * 4);
  887. a += offset;
  888. if (na > 1)
  889. addr[na - 2] = cpu_to_fdt32(a >> 32);
  890. addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
  891. return 0;
  892. }
  893. #ifdef CONFIG_OF_ISA_BUS
  894. /* ISA bus translator */
  895. static int of_bus_isa_match(const void *blob, int parentoffset)
  896. {
  897. const char *name;
  898. name = fdt_get_name(blob, parentoffset, NULL);
  899. if (!name)
  900. return 0;
  901. return !strcmp(name, "isa");
  902. }
  903. static void of_bus_isa_count_cells(const void *blob, int parentoffset,
  904. int *addrc, int *sizec)
  905. {
  906. if (addrc)
  907. *addrc = 2;
  908. if (sizec)
  909. *sizec = 1;
  910. }
  911. static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
  912. int na, int ns, int pna)
  913. {
  914. u64 cp, s, da;
  915. /* Check address type match */
  916. if ((addr[0] ^ range[0]) & cpu_to_be32(1))
  917. return OF_BAD_ADDR;
  918. cp = fdt_read_number(range + 1, na - 1);
  919. s = fdt_read_number(range + na + pna, ns);
  920. da = fdt_read_number(addr + 1, na - 1);
  921. debug("OF: ISA map, cp=%" PRIu64 ", s=%" PRIu64
  922. ", da=%" PRIu64 "\n", cp, s, da);
  923. if (da < cp || da >= (cp + s))
  924. return OF_BAD_ADDR;
  925. return da - cp;
  926. }
  927. static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
  928. {
  929. return of_bus_default_translate(addr + 1, offset, na - 1);
  930. }
  931. #endif /* CONFIG_OF_ISA_BUS */
  932. /* Array of bus specific translators */
  933. static struct of_bus of_busses[] = {
  934. #ifdef CONFIG_OF_ISA_BUS
  935. /* ISA */
  936. {
  937. .name = "isa",
  938. .addresses = "reg",
  939. .match = of_bus_isa_match,
  940. .count_cells = of_bus_isa_count_cells,
  941. .map = of_bus_isa_map,
  942. .translate = of_bus_isa_translate,
  943. },
  944. #endif /* CONFIG_OF_ISA_BUS */
  945. /* Default */
  946. {
  947. .name = "default",
  948. .addresses = "reg",
  949. .count_cells = fdt_support_default_count_cells,
  950. .map = of_bus_default_map,
  951. .translate = of_bus_default_translate,
  952. },
  953. };
  954. static struct of_bus *of_match_bus(const void *blob, int parentoffset)
  955. {
  956. struct of_bus *bus;
  957. if (ARRAY_SIZE(of_busses) == 1)
  958. return of_busses;
  959. for (bus = of_busses; bus; bus++) {
  960. if (!bus->match || bus->match(blob, parentoffset))
  961. return bus;
  962. }
  963. /*
  964. * We should always have matched the default bus at least, since
  965. * it has a NULL match field. If we didn't then it somehow isn't
  966. * in the of_busses array or something equally catastrophic has
  967. * gone wrong.
  968. */
  969. assert(0);
  970. return NULL;
  971. }
  972. static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
  973. struct of_bus *pbus, fdt32_t *addr,
  974. int na, int ns, int pna, const char *rprop)
  975. {
  976. const fdt32_t *ranges;
  977. int rlen;
  978. int rone;
  979. u64 offset = OF_BAD_ADDR;
  980. /* Normally, an absence of a "ranges" property means we are
  981. * crossing a non-translatable boundary, and thus the addresses
  982. * below the current not cannot be converted to CPU physical ones.
  983. * Unfortunately, while this is very clear in the spec, it's not
  984. * what Apple understood, and they do have things like /uni-n or
  985. * /ht nodes with no "ranges" property and a lot of perfectly
  986. * useable mapped devices below them. Thus we treat the absence of
  987. * "ranges" as equivalent to an empty "ranges" property which means
  988. * a 1:1 translation at that level. It's up to the caller not to try
  989. * to translate addresses that aren't supposed to be translated in
  990. * the first place. --BenH.
  991. */
  992. ranges = fdt_getprop(blob, parent, rprop, &rlen);
  993. if (ranges == NULL || rlen == 0) {
  994. offset = fdt_read_number(addr, na);
  995. memset(addr, 0, pna * 4);
  996. debug("OF: no ranges, 1:1 translation\n");
  997. goto finish;
  998. }
  999. debug("OF: walking ranges...\n");
  1000. /* Now walk through the ranges */
  1001. rlen /= 4;
  1002. rone = na + pna + ns;
  1003. for (; rlen >= rone; rlen -= rone, ranges += rone) {
  1004. offset = bus->map(addr, ranges, na, ns, pna);
  1005. if (offset != OF_BAD_ADDR)
  1006. break;
  1007. }
  1008. if (offset == OF_BAD_ADDR) {
  1009. debug("OF: not found !\n");
  1010. return 1;
  1011. }
  1012. memcpy(addr, ranges + na, 4 * pna);
  1013. finish:
  1014. of_dump_addr("OF: parent translation for:", addr, pna);
  1015. debug("OF: with offset: %" PRIu64 "\n", offset);
  1016. /* Translate it into parent bus space */
  1017. return pbus->translate(addr, offset, pna);
  1018. }
  1019. /*
  1020. * Translate an address from the device-tree into a CPU physical address,
  1021. * this walks up the tree and applies the various bus mappings on the
  1022. * way.
  1023. *
  1024. * Note: We consider that crossing any level with #size-cells == 0 to mean
  1025. * that translation is impossible (that is we are not dealing with a value
  1026. * that can be mapped to a cpu physical address). This is not really specified
  1027. * that way, but this is traditionally the way IBM at least do things
  1028. */
  1029. static u64 __of_translate_address(const void *blob, int node_offset,
  1030. const fdt32_t *in_addr, const char *rprop)
  1031. {
  1032. int parent;
  1033. struct of_bus *bus, *pbus;
  1034. fdt32_t addr[OF_MAX_ADDR_CELLS];
  1035. int na, ns, pna, pns;
  1036. u64 result = OF_BAD_ADDR;
  1037. debug("OF: ** translation for device %s **\n",
  1038. fdt_get_name(blob, node_offset, NULL));
  1039. /* Get parent & match bus type */
  1040. parent = fdt_parent_offset(blob, node_offset);
  1041. if (parent < 0)
  1042. goto bail;
  1043. bus = of_match_bus(blob, parent);
  1044. /* Cound address cells & copy address locally */
  1045. bus->count_cells(blob, parent, &na, &ns);
  1046. if (!OF_CHECK_COUNTS(na, ns)) {
  1047. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  1048. fdt_get_name(blob, node_offset, NULL));
  1049. goto bail;
  1050. }
  1051. memcpy(addr, in_addr, na * 4);
  1052. debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
  1053. bus->name, na, ns, fdt_get_name(blob, parent, NULL));
  1054. of_dump_addr("OF: translating address:", addr, na);
  1055. /* Translate */
  1056. for (;;) {
  1057. /* Switch to parent bus */
  1058. node_offset = parent;
  1059. parent = fdt_parent_offset(blob, node_offset);
  1060. /* If root, we have finished */
  1061. if (parent < 0) {
  1062. debug("OF: reached root node\n");
  1063. result = fdt_read_number(addr, na);
  1064. break;
  1065. }
  1066. /* Get new parent bus and counts */
  1067. pbus = of_match_bus(blob, parent);
  1068. pbus->count_cells(blob, parent, &pna, &pns);
  1069. if (!OF_CHECK_COUNTS(pna, pns)) {
  1070. printf("%s: Bad cell count for %s\n", __FUNCTION__,
  1071. fdt_get_name(blob, node_offset, NULL));
  1072. break;
  1073. }
  1074. debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
  1075. pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
  1076. /* Apply bus translation */
  1077. if (of_translate_one(blob, node_offset, bus, pbus,
  1078. addr, na, ns, pna, rprop))
  1079. break;
  1080. /* Complete the move up one level */
  1081. na = pna;
  1082. ns = pns;
  1083. bus = pbus;
  1084. of_dump_addr("OF: one level translation:", addr, na);
  1085. }
  1086. bail:
  1087. return result;
  1088. }
  1089. u64 fdt_translate_address(const void *blob, int node_offset,
  1090. const fdt32_t *in_addr)
  1091. {
  1092. return __of_translate_address(blob, node_offset, in_addr, "ranges");
  1093. }
  1094. /**
  1095. * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
  1096. * who's reg property matches a physical cpu address
  1097. *
  1098. * @blob: ptr to device tree
  1099. * @compat: compatiable string to match
  1100. * @compat_off: property name
  1101. *
  1102. */
  1103. int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
  1104. phys_addr_t compat_off)
  1105. {
  1106. int len, off = fdt_node_offset_by_compatible(blob, -1, compat);
  1107. while (off != -FDT_ERR_NOTFOUND) {
  1108. const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
  1109. if (reg) {
  1110. if (compat_off == fdt_translate_address(blob, off, reg))
  1111. return off;
  1112. }
  1113. off = fdt_node_offset_by_compatible(blob, off, compat);
  1114. }
  1115. return -FDT_ERR_NOTFOUND;
  1116. }
  1117. /**
  1118. * fdt_alloc_phandle: Return next free phandle value
  1119. *
  1120. * @blob: ptr to device tree
  1121. */
  1122. int fdt_alloc_phandle(void *blob)
  1123. {
  1124. int offset;
  1125. uint32_t phandle = 0;
  1126. for (offset = fdt_next_node(blob, -1, NULL); offset >= 0;
  1127. offset = fdt_next_node(blob, offset, NULL)) {
  1128. phandle = max(phandle, fdt_get_phandle(blob, offset));
  1129. }
  1130. return phandle + 1;
  1131. }
  1132. /*
  1133. * fdt_set_phandle: Create a phandle property for the given node
  1134. *
  1135. * @fdt: ptr to device tree
  1136. * @nodeoffset: node to update
  1137. * @phandle: phandle value to set (must be unique)
  1138. */
  1139. int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
  1140. {
  1141. int ret;
  1142. #ifdef DEBUG
  1143. int off = fdt_node_offset_by_phandle(fdt, phandle);
  1144. if ((off >= 0) && (off != nodeoffset)) {
  1145. char buf[64];
  1146. fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
  1147. printf("Trying to update node %s with phandle %u ",
  1148. buf, phandle);
  1149. fdt_get_path(fdt, off, buf, sizeof(buf));
  1150. printf("that already exists in node %s.\n", buf);
  1151. return -FDT_ERR_BADPHANDLE;
  1152. }
  1153. #endif
  1154. ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
  1155. if (ret < 0)
  1156. return ret;
  1157. /*
  1158. * For now, also set the deprecated "linux,phandle" property, so that we
  1159. * don't break older kernels.
  1160. */
  1161. ret = fdt_setprop_cell(fdt, nodeoffset, "linux,phandle", phandle);
  1162. return ret;
  1163. }
  1164. /*
  1165. * fdt_create_phandle: Create a phandle property for the given node
  1166. *
  1167. * @fdt: ptr to device tree
  1168. * @nodeoffset: node to update
  1169. */
  1170. unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
  1171. {
  1172. /* see if there is a phandle already */
  1173. int phandle = fdt_get_phandle(fdt, nodeoffset);
  1174. /* if we got 0, means no phandle so create one */
  1175. if (phandle == 0) {
  1176. int ret;
  1177. phandle = fdt_alloc_phandle(fdt);
  1178. ret = fdt_set_phandle(fdt, nodeoffset, phandle);
  1179. if (ret < 0) {
  1180. printf("Can't set phandle %u: %s\n", phandle,
  1181. fdt_strerror(ret));
  1182. return 0;
  1183. }
  1184. }
  1185. return phandle;
  1186. }
  1187. /*
  1188. * fdt_set_node_status: Set status for the given node
  1189. *
  1190. * @fdt: ptr to device tree
  1191. * @nodeoffset: node to update
  1192. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
  1193. * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
  1194. * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
  1195. */
  1196. int fdt_set_node_status(void *fdt, int nodeoffset,
  1197. enum fdt_status status, unsigned int error_code)
  1198. {
  1199. char buf[16];
  1200. int ret = 0;
  1201. if (nodeoffset < 0)
  1202. return nodeoffset;
  1203. switch (status) {
  1204. case FDT_STATUS_OKAY:
  1205. ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
  1206. break;
  1207. case FDT_STATUS_DISABLED:
  1208. ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
  1209. break;
  1210. case FDT_STATUS_FAIL:
  1211. ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
  1212. break;
  1213. case FDT_STATUS_FAIL_ERROR_CODE:
  1214. sprintf(buf, "fail-%d", error_code);
  1215. ret = fdt_setprop_string(fdt, nodeoffset, "status", buf);
  1216. break;
  1217. default:
  1218. printf("Invalid fdt status: %x\n", status);
  1219. ret = -1;
  1220. break;
  1221. }
  1222. return ret;
  1223. }
  1224. /*
  1225. * fdt_set_status_by_alias: Set status for the given node given an alias
  1226. *
  1227. * @fdt: ptr to device tree
  1228. * @alias: alias of node to update
  1229. * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED,
  1230. * FDT_STATUS_FAIL, FDT_STATUS_FAIL_ERROR_CODE
  1231. * @error_code: optional, only used if status is FDT_STATUS_FAIL_ERROR_CODE
  1232. */
  1233. int fdt_set_status_by_alias(void *fdt, const char* alias,
  1234. enum fdt_status status, unsigned int error_code)
  1235. {
  1236. int offset = fdt_path_offset(fdt, alias);
  1237. return fdt_set_node_status(fdt, offset, status, error_code);
  1238. }
  1239. #if defined(CONFIG_VIDEO) || defined(CONFIG_LCD)
  1240. int fdt_add_edid(void *blob, const char *compat, unsigned char *edid_buf)
  1241. {
  1242. int noff;
  1243. int ret;
  1244. noff = fdt_node_offset_by_compatible(blob, -1, compat);
  1245. if (noff != -FDT_ERR_NOTFOUND) {
  1246. debug("%s: %s\n", fdt_get_name(blob, noff, 0), compat);
  1247. add_edid:
  1248. ret = fdt_setprop(blob, noff, "edid", edid_buf, 128);
  1249. if (ret == -FDT_ERR_NOSPACE) {
  1250. ret = fdt_increase_size(blob, 512);
  1251. if (!ret)
  1252. goto add_edid;
  1253. else
  1254. goto err_size;
  1255. } else if (ret < 0) {
  1256. printf("Can't add property: %s\n", fdt_strerror(ret));
  1257. return ret;
  1258. }
  1259. }
  1260. return 0;
  1261. err_size:
  1262. printf("Can't increase blob size: %s\n", fdt_strerror(ret));
  1263. return ret;
  1264. }
  1265. #endif
  1266. /*
  1267. * Verify the physical address of device tree node for a given alias
  1268. *
  1269. * This function locates the device tree node of a given alias, and then
  1270. * verifies that the physical address of that device matches the given
  1271. * parameter. It displays a message if there is a mismatch.
  1272. *
  1273. * Returns 1 on success, 0 on failure
  1274. */
  1275. int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
  1276. {
  1277. const char *path;
  1278. const fdt32_t *reg;
  1279. int node, len;
  1280. u64 dt_addr;
  1281. path = fdt_getprop(fdt, anode, alias, NULL);
  1282. if (!path) {
  1283. /* If there's no such alias, then it's not a failure */
  1284. return 1;
  1285. }
  1286. node = fdt_path_offset(fdt, path);
  1287. if (node < 0) {
  1288. printf("Warning: device tree alias '%s' points to invalid "
  1289. "node %s.\n", alias, path);
  1290. return 0;
  1291. }
  1292. reg = fdt_getprop(fdt, node, "reg", &len);
  1293. if (!reg) {
  1294. printf("Warning: device tree node '%s' has no address.\n",
  1295. path);
  1296. return 0;
  1297. }
  1298. dt_addr = fdt_translate_address(fdt, node, reg);
  1299. if (addr != dt_addr) {
  1300. printf("Warning: U-Boot configured device %s at address %"
  1301. PRIx64 ",\n but the device tree has it address %"
  1302. PRIx64 ".\n", alias, addr, dt_addr);
  1303. return 0;
  1304. }
  1305. return 1;
  1306. }
  1307. /*
  1308. * Returns the base address of an SOC or PCI node
  1309. */
  1310. u64 fdt_get_base_address(const void *fdt, int node)
  1311. {
  1312. int size;
  1313. const fdt32_t *prop;
  1314. prop = fdt_getprop(fdt, node, "reg", &size);
  1315. return prop ? fdt_translate_address(fdt, node, prop) : 0;
  1316. }
  1317. /*
  1318. * Read a property of size <prop_len>. Currently only supports 1 or 2 cells.
  1319. */
  1320. static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
  1321. uint64_t *val, int cells)
  1322. {
  1323. const fdt32_t *prop32 = &prop[cell_off];
  1324. const fdt64_t *prop64 = (const fdt64_t *)&prop[cell_off];
  1325. if ((cell_off + cells) > prop_len)
  1326. return -FDT_ERR_NOSPACE;
  1327. switch (cells) {
  1328. case 1:
  1329. *val = fdt32_to_cpu(*prop32);
  1330. break;
  1331. case 2:
  1332. *val = fdt64_to_cpu(*prop64);
  1333. break;
  1334. default:
  1335. return -FDT_ERR_NOSPACE;
  1336. }
  1337. return 0;
  1338. }
  1339. /**
  1340. * fdt_read_range - Read a node's n'th range property
  1341. *
  1342. * @fdt: ptr to device tree
  1343. * @node: offset of node
  1344. * @n: range index
  1345. * @child_addr: pointer to storage for the "child address" field
  1346. * @addr: pointer to storage for the CPU view translated physical start
  1347. * @len: pointer to storage for the range length
  1348. *
  1349. * Convenience function that reads and interprets a specific range out of
  1350. * a number of the "ranges" property array.
  1351. */
  1352. int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
  1353. uint64_t *addr, uint64_t *len)
  1354. {
  1355. int pnode = fdt_parent_offset(fdt, node);
  1356. const fdt32_t *ranges;
  1357. int pacells;
  1358. int acells;
  1359. int scells;
  1360. int ranges_len;
  1361. int cell = 0;
  1362. int r = 0;
  1363. /*
  1364. * The "ranges" property is an array of
  1365. * { <child address> <parent address> <size in child address space> }
  1366. *
  1367. * All 3 elements can span a diffent number of cells. Fetch their size.
  1368. */
  1369. pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
  1370. acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
  1371. scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
  1372. /* Now try to get the ranges property */
  1373. ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
  1374. if (!ranges)
  1375. return -FDT_ERR_NOTFOUND;
  1376. ranges_len /= sizeof(uint32_t);
  1377. /* Jump to the n'th entry */
  1378. cell = n * (pacells + acells + scells);
  1379. /* Read <child address> */
  1380. if (child_addr) {
  1381. r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
  1382. acells);
  1383. if (r)
  1384. return r;
  1385. }
  1386. cell += acells;
  1387. /* Read <parent address> */
  1388. if (addr)
  1389. *addr = fdt_translate_address(fdt, node, ranges + cell);
  1390. cell += pacells;
  1391. /* Read <size in child address space> */
  1392. if (len) {
  1393. r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
  1394. if (r)
  1395. return r;
  1396. }
  1397. return 0;
  1398. }
  1399. /**
  1400. * fdt_setup_simplefb_node - Fill and enable a simplefb node
  1401. *
  1402. * @fdt: ptr to device tree
  1403. * @node: offset of the simplefb node
  1404. * @base_address: framebuffer base address
  1405. * @width: width in pixels
  1406. * @height: height in pixels
  1407. * @stride: bytes per line
  1408. * @format: pixel format string
  1409. *
  1410. * Convenience function to fill and enable a simplefb node.
  1411. */
  1412. int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
  1413. u32 height, u32 stride, const char *format)
  1414. {
  1415. char name[32];
  1416. fdt32_t cells[4];
  1417. int i, addrc, sizec, ret;
  1418. fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
  1419. &addrc, &sizec);
  1420. i = 0;
  1421. if (addrc == 2)
  1422. cells[i++] = cpu_to_fdt32(base_address >> 32);
  1423. cells[i++] = cpu_to_fdt32(base_address);
  1424. if (sizec == 2)
  1425. cells[i++] = 0;
  1426. cells[i++] = cpu_to_fdt32(height * stride);
  1427. ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
  1428. if (ret < 0)
  1429. return ret;
  1430. snprintf(name, sizeof(name), "framebuffer@%" PRIx64, base_address);
  1431. ret = fdt_set_name(fdt, node, name);
  1432. if (ret < 0)
  1433. return ret;
  1434. ret = fdt_setprop_u32(fdt, node, "width", width);
  1435. if (ret < 0)
  1436. return ret;
  1437. ret = fdt_setprop_u32(fdt, node, "height", height);
  1438. if (ret < 0)
  1439. return ret;
  1440. ret = fdt_setprop_u32(fdt, node, "stride", stride);
  1441. if (ret < 0)
  1442. return ret;
  1443. ret = fdt_setprop_string(fdt, node, "format", format);
  1444. if (ret < 0)
  1445. return ret;
  1446. ret = fdt_setprop_string(fdt, node, "status", "okay");
  1447. if (ret < 0)
  1448. return ret;
  1449. return 0;
  1450. }
  1451. /*
  1452. * Update native-mode in display-timings from display environment variable.
  1453. * The node to update are specified by path.
  1454. */
  1455. int fdt_fixup_display(void *blob, const char *path, const char *display)
  1456. {
  1457. int off, toff;
  1458. if (!display || !path)
  1459. return -FDT_ERR_NOTFOUND;
  1460. toff = fdt_path_offset(blob, path);
  1461. if (toff >= 0)
  1462. toff = fdt_subnode_offset(blob, toff, "display-timings");
  1463. if (toff < 0)
  1464. return toff;
  1465. for (off = fdt_first_subnode(blob, toff);
  1466. off >= 0;
  1467. off = fdt_next_subnode(blob, off)) {
  1468. uint32_t h = fdt_get_phandle(blob, off);
  1469. debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
  1470. fdt32_to_cpu(h));
  1471. if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
  1472. return fdt_setprop_u32(blob, toff, "native-mode", h);
  1473. }
  1474. return toff;
  1475. }
  1476. #ifdef CONFIG_OF_LIBFDT_OVERLAY
  1477. /**
  1478. * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
  1479. *
  1480. * @fdt: ptr to device tree
  1481. * @fdto: ptr to device tree overlay
  1482. *
  1483. * Convenience function to apply an overlay and display helpful messages
  1484. * in the case of an error
  1485. */
  1486. int fdt_overlay_apply_verbose(void *fdt, void *fdto)
  1487. {
  1488. int err;
  1489. bool has_symbols;
  1490. err = fdt_path_offset(fdt, "/__symbols__");
  1491. has_symbols = err >= 0;
  1492. err = fdt_overlay_apply(fdt, fdto);
  1493. if (err < 0) {
  1494. printf("failed on fdt_overlay_apply(): %s\n",
  1495. fdt_strerror(err));
  1496. if (!has_symbols) {
  1497. printf("base fdt does did not have a /__symbols__ node\n");
  1498. printf("make sure you've compiled with -@\n");
  1499. }
  1500. }
  1501. return err;
  1502. }
  1503. #endif