fdt_support.c 39 KB

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