fdt_support.c 42 KB

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