fdtdec.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * SPDX-License-Identifier: GPL-2.0+
  4. */
  5. #ifndef USE_HOSTCC
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <serial.h>
  10. #include <libfdt.h>
  11. #include <fdt_support.h>
  12. #include <fdtdec.h>
  13. #include <asm/sections.h>
  14. #include <dm/of_extra.h>
  15. #include <linux/ctype.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. /*
  18. * Here are the type we know about. One day we might allow drivers to
  19. * register. For now we just put them here. The COMPAT macro allows us to
  20. * turn this into a sparse list later, and keeps the ID with the name.
  21. *
  22. * NOTE: This list is basically a TODO list for things that need to be
  23. * converted to driver model. So don't add new things here unless there is a
  24. * good reason why driver-model conversion is infeasible. Examples include
  25. * things which are used before driver model is available.
  26. */
  27. #define COMPAT(id, name) name
  28. static const char * const compat_names[COMPAT_COUNT] = {
  29. COMPAT(UNKNOWN, "<none>"),
  30. COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
  31. COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
  32. COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
  33. COMPAT(NVIDIA_TEGRA124_PMC, "nvidia,tegra124-pmc"),
  34. COMPAT(NVIDIA_TEGRA186_SDMMC, "nvidia,tegra186-sdhci"),
  35. COMPAT(NVIDIA_TEGRA210_SDMMC, "nvidia,tegra210-sdhci"),
  36. COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
  37. COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
  38. COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
  39. COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
  40. COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"),
  41. COMPAT(SMSC_LAN9215, "smsc,lan9215"),
  42. COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
  43. COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
  44. COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
  45. COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
  46. COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
  47. COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
  48. COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
  49. COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
  50. COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
  51. COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
  52. COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686"),
  53. COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
  54. COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
  55. COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
  56. COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
  57. COMPAT(INTEL_MICROCODE, "intel,microcode"),
  58. COMPAT(AMS_AS3722, "ams,as3722"),
  59. COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
  60. COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"),
  61. COMPAT(ALTERA_SOCFPGA_DWMMC, "altr,socfpga-dw-mshc"),
  62. COMPAT(ALTERA_SOCFPGA_DWC2USB, "snps,dwc2"),
  63. COMPAT(INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"),
  64. COMPAT(INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"),
  65. COMPAT(INTEL_IVYBRIDGE_FSP, "intel,ivybridge-fsp"),
  66. COMPAT(COMPAT_SUNXI_NAND, "allwinner,sun4i-a10-nand"),
  67. COMPAT(ALTERA_SOCFPGA_CLK, "altr,clk-mgr"),
  68. COMPAT(ALTERA_SOCFPGA_PINCTRL_SINGLE, "pinctrl-single"),
  69. COMPAT(ALTERA_SOCFPGA_H2F_BRG, "altr,socfpga-hps2fpga-bridge"),
  70. COMPAT(ALTERA_SOCFPGA_LWH2F_BRG, "altr,socfpga-lwhps2fpga-bridge"),
  71. COMPAT(ALTERA_SOCFPGA_F2H_BRG, "altr,socfpga-fpga2hps-bridge"),
  72. COMPAT(ALTERA_SOCFPGA_F2SDR0, "altr,socfpga-fpga2sdram0-bridge"),
  73. COMPAT(ALTERA_SOCFPGA_F2SDR1, "altr,socfpga-fpga2sdram1-bridge"),
  74. COMPAT(ALTERA_SOCFPGA_F2SDR2, "altr,socfpga-fpga2sdram2-bridge"),
  75. };
  76. const char *fdtdec_get_compatible(enum fdt_compat_id id)
  77. {
  78. /* We allow reading of the 'unknown' ID for testing purposes */
  79. assert(id >= 0 && id < COMPAT_COUNT);
  80. return compat_names[id];
  81. }
  82. fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
  83. const char *prop_name, int index, int na, int ns,
  84. fdt_size_t *sizep, bool translate)
  85. {
  86. const fdt32_t *prop, *prop_end;
  87. const fdt32_t *prop_addr, *prop_size, *prop_after_size;
  88. int len;
  89. fdt_addr_t addr;
  90. debug("%s: %s: ", __func__, prop_name);
  91. if (na > (sizeof(fdt_addr_t) / sizeof(fdt32_t))) {
  92. debug("(na too large for fdt_addr_t type)\n");
  93. return FDT_ADDR_T_NONE;
  94. }
  95. if (ns > (sizeof(fdt_size_t) / sizeof(fdt32_t))) {
  96. debug("(ns too large for fdt_size_t type)\n");
  97. return FDT_ADDR_T_NONE;
  98. }
  99. prop = fdt_getprop(blob, node, prop_name, &len);
  100. if (!prop) {
  101. debug("(not found)\n");
  102. return FDT_ADDR_T_NONE;
  103. }
  104. prop_end = prop + (len / sizeof(*prop));
  105. prop_addr = prop + (index * (na + ns));
  106. prop_size = prop_addr + na;
  107. prop_after_size = prop_size + ns;
  108. if (prop_after_size > prop_end) {
  109. debug("(not enough data: expected >= %d cells, got %d cells)\n",
  110. (u32)(prop_after_size - prop), ((u32)(prop_end - prop)));
  111. return FDT_ADDR_T_NONE;
  112. }
  113. #if CONFIG_IS_ENABLED(OF_TRANSLATE)
  114. if (translate)
  115. addr = fdt_translate_address(blob, node, prop_addr);
  116. else
  117. #endif
  118. addr = fdtdec_get_number(prop_addr, na);
  119. if (sizep) {
  120. *sizep = fdtdec_get_number(prop_size, ns);
  121. debug("addr=%08llx, size=%llx\n", (unsigned long long)addr,
  122. (unsigned long long)*sizep);
  123. } else {
  124. debug("addr=%08llx\n", (unsigned long long)addr);
  125. }
  126. return addr;
  127. }
  128. fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
  129. int node, const char *prop_name, int index, fdt_size_t *sizep,
  130. bool translate)
  131. {
  132. int na, ns;
  133. debug("%s: ", __func__);
  134. na = fdt_address_cells(blob, parent);
  135. if (na < 1) {
  136. debug("(bad #address-cells)\n");
  137. return FDT_ADDR_T_NONE;
  138. }
  139. ns = fdt_size_cells(blob, parent);
  140. if (ns < 0) {
  141. debug("(bad #size-cells)\n");
  142. return FDT_ADDR_T_NONE;
  143. }
  144. debug("na=%d, ns=%d, ", na, ns);
  145. return fdtdec_get_addr_size_fixed(blob, node, prop_name, index, na,
  146. ns, sizep, translate);
  147. }
  148. fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
  149. const char *prop_name, int index, fdt_size_t *sizep,
  150. bool translate)
  151. {
  152. int parent;
  153. debug("%s: ", __func__);
  154. parent = fdt_parent_offset(blob, node);
  155. if (parent < 0) {
  156. debug("(no parent found)\n");
  157. return FDT_ADDR_T_NONE;
  158. }
  159. return fdtdec_get_addr_size_auto_parent(blob, parent, node, prop_name,
  160. index, sizep, translate);
  161. }
  162. fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
  163. const char *prop_name, fdt_size_t *sizep)
  164. {
  165. int ns = sizep ? (sizeof(fdt_size_t) / sizeof(fdt32_t)) : 0;
  166. return fdtdec_get_addr_size_fixed(blob, node, prop_name, 0,
  167. sizeof(fdt_addr_t) / sizeof(fdt32_t),
  168. ns, sizep, false);
  169. }
  170. fdt_addr_t fdtdec_get_addr(const void *blob, int node,
  171. const char *prop_name)
  172. {
  173. return fdtdec_get_addr_size(blob, node, prop_name, NULL);
  174. }
  175. #if defined(CONFIG_PCI) && defined(CONFIG_DM_PCI)
  176. int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
  177. const char *prop_name, struct fdt_pci_addr *addr)
  178. {
  179. const u32 *cell;
  180. int len;
  181. int ret = -ENOENT;
  182. debug("%s: %s: ", __func__, prop_name);
  183. /*
  184. * If we follow the pci bus bindings strictly, we should check
  185. * the value of the node's parent node's #address-cells and
  186. * #size-cells. They need to be 3 and 2 accordingly. However,
  187. * for simplicity we skip the check here.
  188. */
  189. cell = fdt_getprop(blob, node, prop_name, &len);
  190. if (!cell)
  191. goto fail;
  192. if ((len % FDT_PCI_REG_SIZE) == 0) {
  193. int num = len / FDT_PCI_REG_SIZE;
  194. int i;
  195. for (i = 0; i < num; i++) {
  196. debug("pci address #%d: %08lx %08lx %08lx\n", i,
  197. (ulong)fdt32_to_cpu(cell[0]),
  198. (ulong)fdt32_to_cpu(cell[1]),
  199. (ulong)fdt32_to_cpu(cell[2]));
  200. if ((fdt32_to_cpu(*cell) & type) == type) {
  201. addr->phys_hi = fdt32_to_cpu(cell[0]);
  202. addr->phys_mid = fdt32_to_cpu(cell[1]);
  203. addr->phys_lo = fdt32_to_cpu(cell[1]);
  204. break;
  205. } else {
  206. cell += (FDT_PCI_ADDR_CELLS +
  207. FDT_PCI_SIZE_CELLS);
  208. }
  209. }
  210. if (i == num) {
  211. ret = -ENXIO;
  212. goto fail;
  213. }
  214. return 0;
  215. } else {
  216. ret = -EINVAL;
  217. }
  218. fail:
  219. debug("(not found)\n");
  220. return ret;
  221. }
  222. int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
  223. {
  224. const char *list, *end;
  225. int len;
  226. list = fdt_getprop(blob, node, "compatible", &len);
  227. if (!list)
  228. return -ENOENT;
  229. end = list + len;
  230. while (list < end) {
  231. char *s;
  232. len = strlen(list);
  233. if (len >= strlen("pciVVVV,DDDD")) {
  234. s = strstr(list, "pci");
  235. /*
  236. * check if the string is something like pciVVVV,DDDD.RR
  237. * or just pciVVVV,DDDD
  238. */
  239. if (s && s[7] == ',' &&
  240. (s[12] == '.' || s[12] == 0)) {
  241. s += 3;
  242. *vendor = simple_strtol(s, NULL, 16);
  243. s += 5;
  244. *device = simple_strtol(s, NULL, 16);
  245. return 0;
  246. }
  247. }
  248. list += (len + 1);
  249. }
  250. return -ENOENT;
  251. }
  252. int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr,
  253. u32 *bar)
  254. {
  255. int barnum;
  256. /* extract the bar number from fdt_pci_addr */
  257. barnum = addr->phys_hi & 0xff;
  258. if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
  259. return -EINVAL;
  260. barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
  261. *bar = dm_pci_read_bar32(dev, barnum);
  262. return 0;
  263. }
  264. #endif
  265. uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
  266. uint64_t default_val)
  267. {
  268. const uint64_t *cell64;
  269. int length;
  270. cell64 = fdt_getprop(blob, node, prop_name, &length);
  271. if (!cell64 || length < sizeof(*cell64))
  272. return default_val;
  273. return fdt64_to_cpu(*cell64);
  274. }
  275. int fdtdec_get_is_enabled(const void *blob, int node)
  276. {
  277. const char *cell;
  278. /*
  279. * It should say "okay", so only allow that. Some fdts use "ok" but
  280. * this is a bug. Please fix your device tree source file. See here
  281. * for discussion:
  282. *
  283. * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
  284. */
  285. cell = fdt_getprop(blob, node, "status", NULL);
  286. if (cell)
  287. return 0 == strcmp(cell, "okay");
  288. return 1;
  289. }
  290. enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
  291. {
  292. enum fdt_compat_id id;
  293. /* Search our drivers */
  294. for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
  295. if (0 == fdt_node_check_compatible(blob, node,
  296. compat_names[id]))
  297. return id;
  298. return COMPAT_UNKNOWN;
  299. }
  300. int fdtdec_next_compatible(const void *blob, int node,
  301. enum fdt_compat_id id)
  302. {
  303. return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
  304. }
  305. int fdtdec_next_compatible_subnode(const void *blob, int node,
  306. enum fdt_compat_id id, int *depthp)
  307. {
  308. do {
  309. node = fdt_next_node(blob, node, depthp);
  310. } while (*depthp > 1);
  311. /* If this is a direct subnode, and compatible, return it */
  312. if (*depthp == 1 && 0 == fdt_node_check_compatible(
  313. blob, node, compat_names[id]))
  314. return node;
  315. return -FDT_ERR_NOTFOUND;
  316. }
  317. int fdtdec_next_alias(const void *blob, const char *name,
  318. enum fdt_compat_id id, int *upto)
  319. {
  320. #define MAX_STR_LEN 20
  321. char str[MAX_STR_LEN + 20];
  322. int node, err;
  323. /* snprintf() is not available */
  324. assert(strlen(name) < MAX_STR_LEN);
  325. sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
  326. node = fdt_path_offset(blob, str);
  327. if (node < 0)
  328. return node;
  329. err = fdt_node_check_compatible(blob, node, compat_names[id]);
  330. if (err < 0)
  331. return err;
  332. if (err)
  333. return -FDT_ERR_NOTFOUND;
  334. (*upto)++;
  335. return node;
  336. }
  337. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  338. enum fdt_compat_id id, int *node_list, int maxcount)
  339. {
  340. memset(node_list, '\0', sizeof(*node_list) * maxcount);
  341. return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
  342. }
  343. /* TODO: Can we tighten this code up a little? */
  344. int fdtdec_add_aliases_for_id(const void *blob, const char *name,
  345. enum fdt_compat_id id, int *node_list, int maxcount)
  346. {
  347. int name_len = strlen(name);
  348. int nodes[maxcount];
  349. int num_found = 0;
  350. int offset, node;
  351. int alias_node;
  352. int count;
  353. int i, j;
  354. /* find the alias node if present */
  355. alias_node = fdt_path_offset(blob, "/aliases");
  356. /*
  357. * start with nothing, and we can assume that the root node can't
  358. * match
  359. */
  360. memset(nodes, '\0', sizeof(nodes));
  361. /* First find all the compatible nodes */
  362. for (node = count = 0; node >= 0 && count < maxcount;) {
  363. node = fdtdec_next_compatible(blob, node, id);
  364. if (node >= 0)
  365. nodes[count++] = node;
  366. }
  367. if (node >= 0)
  368. debug("%s: warning: maxcount exceeded with alias '%s'\n",
  369. __func__, name);
  370. /* Now find all the aliases */
  371. for (offset = fdt_first_property_offset(blob, alias_node);
  372. offset > 0;
  373. offset = fdt_next_property_offset(blob, offset)) {
  374. const struct fdt_property *prop;
  375. const char *path;
  376. int number;
  377. int found;
  378. node = 0;
  379. prop = fdt_get_property_by_offset(blob, offset, NULL);
  380. path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  381. if (prop->len && 0 == strncmp(path, name, name_len))
  382. node = fdt_path_offset(blob, prop->data);
  383. if (node <= 0)
  384. continue;
  385. /* Get the alias number */
  386. number = simple_strtoul(path + name_len, NULL, 10);
  387. if (number < 0 || number >= maxcount) {
  388. debug("%s: warning: alias '%s' is out of range\n",
  389. __func__, path);
  390. continue;
  391. }
  392. /* Make sure the node we found is actually in our list! */
  393. found = -1;
  394. for (j = 0; j < count; j++)
  395. if (nodes[j] == node) {
  396. found = j;
  397. break;
  398. }
  399. if (found == -1) {
  400. debug("%s: warning: alias '%s' points to a node "
  401. "'%s' that is missing or is not compatible "
  402. " with '%s'\n", __func__, path,
  403. fdt_get_name(blob, node, NULL),
  404. compat_names[id]);
  405. continue;
  406. }
  407. /*
  408. * Add this node to our list in the right place, and mark
  409. * it as done.
  410. */
  411. if (fdtdec_get_is_enabled(blob, node)) {
  412. if (node_list[number]) {
  413. debug("%s: warning: alias '%s' requires that "
  414. "a node be placed in the list in a "
  415. "position which is already filled by "
  416. "node '%s'\n", __func__, path,
  417. fdt_get_name(blob, node, NULL));
  418. continue;
  419. }
  420. node_list[number] = node;
  421. if (number >= num_found)
  422. num_found = number + 1;
  423. }
  424. nodes[found] = 0;
  425. }
  426. /* Add any nodes not mentioned by an alias */
  427. for (i = j = 0; i < maxcount; i++) {
  428. if (!node_list[i]) {
  429. for (; j < maxcount; j++)
  430. if (nodes[j] &&
  431. fdtdec_get_is_enabled(blob, nodes[j]))
  432. break;
  433. /* Have we run out of nodes to add? */
  434. if (j == maxcount)
  435. break;
  436. assert(!node_list[i]);
  437. node_list[i] = nodes[j++];
  438. if (i >= num_found)
  439. num_found = i + 1;
  440. }
  441. }
  442. return num_found;
  443. }
  444. int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
  445. int *seqp)
  446. {
  447. int base_len = strlen(base);
  448. const char *find_name;
  449. int find_namelen;
  450. int prop_offset;
  451. int aliases;
  452. find_name = fdt_get_name(blob, offset, &find_namelen);
  453. debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
  454. aliases = fdt_path_offset(blob, "/aliases");
  455. for (prop_offset = fdt_first_property_offset(blob, aliases);
  456. prop_offset > 0;
  457. prop_offset = fdt_next_property_offset(blob, prop_offset)) {
  458. const char *prop;
  459. const char *name;
  460. const char *slash;
  461. int len, val;
  462. prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
  463. debug(" - %s, %s\n", name, prop);
  464. if (len < find_namelen || *prop != '/' || prop[len - 1] ||
  465. strncmp(name, base, base_len))
  466. continue;
  467. slash = strrchr(prop, '/');
  468. if (strcmp(slash + 1, find_name))
  469. continue;
  470. val = trailing_strtol(name);
  471. if (val != -1) {
  472. *seqp = val;
  473. debug("Found seq %d\n", *seqp);
  474. return 0;
  475. }
  476. }
  477. debug("Not found\n");
  478. return -ENOENT;
  479. }
  480. const char *fdtdec_get_chosen_prop(const void *blob, const char *name)
  481. {
  482. int chosen_node;
  483. if (!blob)
  484. return NULL;
  485. chosen_node = fdt_path_offset(blob, "/chosen");
  486. return fdt_getprop(blob, chosen_node, name, NULL);
  487. }
  488. int fdtdec_get_chosen_node(const void *blob, const char *name)
  489. {
  490. const char *prop;
  491. prop = fdtdec_get_chosen_prop(blob, name);
  492. if (!prop)
  493. return -FDT_ERR_NOTFOUND;
  494. return fdt_path_offset(blob, prop);
  495. }
  496. int fdtdec_check_fdt(void)
  497. {
  498. /*
  499. * We must have an FDT, but we cannot panic() yet since the console
  500. * is not ready. So for now, just assert(). Boards which need an early
  501. * FDT (prior to console ready) will need to make their own
  502. * arrangements and do their own checks.
  503. */
  504. assert(!fdtdec_prepare_fdt());
  505. return 0;
  506. }
  507. /*
  508. * This function is a little odd in that it accesses global data. At some
  509. * point if the architecture board.c files merge this will make more sense.
  510. * Even now, it is common code.
  511. */
  512. int fdtdec_prepare_fdt(void)
  513. {
  514. if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
  515. fdt_check_header(gd->fdt_blob)) {
  516. #ifdef CONFIG_SPL_BUILD
  517. puts("Missing DTB\n");
  518. #else
  519. puts("No valid device tree binary found - please append one to U-Boot binary, use u-boot-dtb.bin or define CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
  520. # ifdef DEBUG
  521. if (gd->fdt_blob) {
  522. printf("fdt_blob=%p\n", gd->fdt_blob);
  523. print_buffer((ulong)gd->fdt_blob, gd->fdt_blob, 4,
  524. 32, 0);
  525. }
  526. # endif
  527. #endif
  528. return -1;
  529. }
  530. return 0;
  531. }
  532. int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
  533. {
  534. const u32 *phandle;
  535. int lookup;
  536. debug("%s: %s\n", __func__, prop_name);
  537. phandle = fdt_getprop(blob, node, prop_name, NULL);
  538. if (!phandle)
  539. return -FDT_ERR_NOTFOUND;
  540. lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
  541. return lookup;
  542. }
  543. /**
  544. * Look up a property in a node and check that it has a minimum length.
  545. *
  546. * @param blob FDT blob
  547. * @param node node to examine
  548. * @param prop_name name of property to find
  549. * @param min_len minimum property length in bytes
  550. * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
  551. found, or -FDT_ERR_BADLAYOUT if not enough data
  552. * @return pointer to cell, which is only valid if err == 0
  553. */
  554. static const void *get_prop_check_min_len(const void *blob, int node,
  555. const char *prop_name, int min_len, int *err)
  556. {
  557. const void *cell;
  558. int len;
  559. debug("%s: %s\n", __func__, prop_name);
  560. cell = fdt_getprop(blob, node, prop_name, &len);
  561. if (!cell)
  562. *err = -FDT_ERR_NOTFOUND;
  563. else if (len < min_len)
  564. *err = -FDT_ERR_BADLAYOUT;
  565. else
  566. *err = 0;
  567. return cell;
  568. }
  569. int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
  570. u32 *array, int count)
  571. {
  572. const u32 *cell;
  573. int i, err = 0;
  574. debug("%s: %s\n", __func__, prop_name);
  575. cell = get_prop_check_min_len(blob, node, prop_name,
  576. sizeof(u32) * count, &err);
  577. if (!err) {
  578. for (i = 0; i < count; i++)
  579. array[i] = fdt32_to_cpu(cell[i]);
  580. }
  581. return err;
  582. }
  583. int fdtdec_get_int_array_count(const void *blob, int node,
  584. const char *prop_name, u32 *array, int count)
  585. {
  586. const u32 *cell;
  587. int len, elems;
  588. int i;
  589. debug("%s: %s\n", __func__, prop_name);
  590. cell = fdt_getprop(blob, node, prop_name, &len);
  591. if (!cell)
  592. return -FDT_ERR_NOTFOUND;
  593. elems = len / sizeof(u32);
  594. if (count > elems)
  595. count = elems;
  596. for (i = 0; i < count; i++)
  597. array[i] = fdt32_to_cpu(cell[i]);
  598. return count;
  599. }
  600. const u32 *fdtdec_locate_array(const void *blob, int node,
  601. const char *prop_name, int count)
  602. {
  603. const u32 *cell;
  604. int err;
  605. cell = get_prop_check_min_len(blob, node, prop_name,
  606. sizeof(u32) * count, &err);
  607. return err ? NULL : cell;
  608. }
  609. int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
  610. {
  611. const s32 *cell;
  612. int len;
  613. debug("%s: %s\n", __func__, prop_name);
  614. cell = fdt_getprop(blob, node, prop_name, &len);
  615. return cell != NULL;
  616. }
  617. int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
  618. const char *list_name,
  619. const char *cells_name,
  620. int cell_count, int index,
  621. struct fdtdec_phandle_args *out_args)
  622. {
  623. const __be32 *list, *list_end;
  624. int rc = 0, size, cur_index = 0;
  625. uint32_t count = 0;
  626. int node = -1;
  627. int phandle;
  628. /* Retrieve the phandle list property */
  629. list = fdt_getprop(blob, src_node, list_name, &size);
  630. if (!list)
  631. return -ENOENT;
  632. list_end = list + size / sizeof(*list);
  633. /* Loop over the phandles until all the requested entry is found */
  634. while (list < list_end) {
  635. rc = -EINVAL;
  636. count = 0;
  637. /*
  638. * If phandle is 0, then it is an empty entry with no
  639. * arguments. Skip forward to the next entry.
  640. */
  641. phandle = be32_to_cpup(list++);
  642. if (phandle) {
  643. /*
  644. * Find the provider node and parse the #*-cells
  645. * property to determine the argument length.
  646. *
  647. * This is not needed if the cell count is hard-coded
  648. * (i.e. cells_name not set, but cell_count is set),
  649. * except when we're going to return the found node
  650. * below.
  651. */
  652. if (cells_name || cur_index == index) {
  653. node = fdt_node_offset_by_phandle(blob,
  654. phandle);
  655. if (!node) {
  656. debug("%s: could not find phandle\n",
  657. fdt_get_name(blob, src_node,
  658. NULL));
  659. goto err;
  660. }
  661. }
  662. if (cells_name) {
  663. count = fdtdec_get_int(blob, node, cells_name,
  664. -1);
  665. if (count == -1) {
  666. debug("%s: could not get %s for %s\n",
  667. fdt_get_name(blob, src_node,
  668. NULL),
  669. cells_name,
  670. fdt_get_name(blob, node,
  671. NULL));
  672. goto err;
  673. }
  674. } else {
  675. count = cell_count;
  676. }
  677. /*
  678. * Make sure that the arguments actually fit in the
  679. * remaining property data length
  680. */
  681. if (list + count > list_end) {
  682. debug("%s: arguments longer than property\n",
  683. fdt_get_name(blob, src_node, NULL));
  684. goto err;
  685. }
  686. }
  687. /*
  688. * All of the error cases above bail out of the loop, so at
  689. * this point, the parsing is successful. If the requested
  690. * index matches, then fill the out_args structure and return,
  691. * or return -ENOENT for an empty entry.
  692. */
  693. rc = -ENOENT;
  694. if (cur_index == index) {
  695. if (!phandle)
  696. goto err;
  697. if (out_args) {
  698. int i;
  699. if (count > MAX_PHANDLE_ARGS) {
  700. debug("%s: too many arguments %d\n",
  701. fdt_get_name(blob, src_node,
  702. NULL), count);
  703. count = MAX_PHANDLE_ARGS;
  704. }
  705. out_args->node = node;
  706. out_args->args_count = count;
  707. for (i = 0; i < count; i++) {
  708. out_args->args[i] =
  709. be32_to_cpup(list++);
  710. }
  711. }
  712. /* Found it! return success */
  713. return 0;
  714. }
  715. node = -1;
  716. list += count;
  717. cur_index++;
  718. }
  719. /*
  720. * Result will be one of:
  721. * -ENOENT : index is for empty phandle
  722. * -EINVAL : parsing error on data
  723. * [1..n] : Number of phandle (count mode; when index = -1)
  724. */
  725. rc = index < 0 ? cur_index : -ENOENT;
  726. err:
  727. return rc;
  728. }
  729. int fdtdec_get_child_count(const void *blob, int node)
  730. {
  731. int subnode;
  732. int num = 0;
  733. fdt_for_each_subnode(subnode, blob, node)
  734. num++;
  735. return num;
  736. }
  737. int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
  738. u8 *array, int count)
  739. {
  740. const u8 *cell;
  741. int err;
  742. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  743. if (!err)
  744. memcpy(array, cell, count);
  745. return err;
  746. }
  747. const u8 *fdtdec_locate_byte_array(const void *blob, int node,
  748. const char *prop_name, int count)
  749. {
  750. const u8 *cell;
  751. int err;
  752. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  753. if (err)
  754. return NULL;
  755. return cell;
  756. }
  757. int fdtdec_get_config_int(const void *blob, const char *prop_name,
  758. int default_val)
  759. {
  760. int config_node;
  761. debug("%s: %s\n", __func__, prop_name);
  762. config_node = fdt_path_offset(blob, "/config");
  763. if (config_node < 0)
  764. return default_val;
  765. return fdtdec_get_int(blob, config_node, prop_name, default_val);
  766. }
  767. int fdtdec_get_config_bool(const void *blob, const char *prop_name)
  768. {
  769. int config_node;
  770. const void *prop;
  771. debug("%s: %s\n", __func__, prop_name);
  772. config_node = fdt_path_offset(blob, "/config");
  773. if (config_node < 0)
  774. return 0;
  775. prop = fdt_get_property(blob, config_node, prop_name, NULL);
  776. return prop != NULL;
  777. }
  778. char *fdtdec_get_config_string(const void *blob, const char *prop_name)
  779. {
  780. const char *nodep;
  781. int nodeoffset;
  782. int len;
  783. debug("%s: %s\n", __func__, prop_name);
  784. nodeoffset = fdt_path_offset(blob, "/config");
  785. if (nodeoffset < 0)
  786. return NULL;
  787. nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
  788. if (!nodep)
  789. return NULL;
  790. return (char *)nodep;
  791. }
  792. int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
  793. fdt_addr_t *basep, fdt_size_t *sizep)
  794. {
  795. const fdt_addr_t *cell;
  796. int len;
  797. debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
  798. prop_name);
  799. cell = fdt_getprop(blob, node, prop_name, &len);
  800. if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
  801. debug("cell=%p, len=%d\n", cell, len);
  802. return -1;
  803. }
  804. *basep = fdt_addr_to_cpu(*cell);
  805. *sizep = fdt_size_to_cpu(cell[1]);
  806. debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
  807. (ulong)*sizep);
  808. return 0;
  809. }
  810. u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
  811. {
  812. u64 number = 0;
  813. while (cells--)
  814. number = (number << 32) | fdt32_to_cpu(*ptr++);
  815. return number;
  816. }
  817. int fdt_get_resource(const void *fdt, int node, const char *property,
  818. unsigned int index, struct fdt_resource *res)
  819. {
  820. const fdt32_t *ptr, *end;
  821. int na, ns, len, parent;
  822. unsigned int i = 0;
  823. parent = fdt_parent_offset(fdt, node);
  824. if (parent < 0)
  825. return parent;
  826. na = fdt_address_cells(fdt, parent);
  827. ns = fdt_size_cells(fdt, parent);
  828. ptr = fdt_getprop(fdt, node, property, &len);
  829. if (!ptr)
  830. return len;
  831. end = ptr + len / sizeof(*ptr);
  832. while (ptr + na + ns <= end) {
  833. if (i == index) {
  834. res->start = res->end = fdtdec_get_number(ptr, na);
  835. res->end += fdtdec_get_number(&ptr[na], ns) - 1;
  836. return 0;
  837. }
  838. ptr += na + ns;
  839. i++;
  840. }
  841. return -FDT_ERR_NOTFOUND;
  842. }
  843. int fdt_get_named_resource(const void *fdt, int node, const char *property,
  844. const char *prop_names, const char *name,
  845. struct fdt_resource *res)
  846. {
  847. int index;
  848. index = fdt_stringlist_search(fdt, node, prop_names, name);
  849. if (index < 0)
  850. return index;
  851. return fdt_get_resource(fdt, node, property, index, res);
  852. }
  853. int fdtdec_decode_memory_region(const void *blob, int config_node,
  854. const char *mem_type, const char *suffix,
  855. fdt_addr_t *basep, fdt_size_t *sizep)
  856. {
  857. char prop_name[50];
  858. const char *mem;
  859. fdt_size_t size, offset_size;
  860. fdt_addr_t base, offset;
  861. int node;
  862. if (config_node == -1) {
  863. config_node = fdt_path_offset(blob, "/config");
  864. if (config_node < 0) {
  865. debug("%s: Cannot find /config node\n", __func__);
  866. return -ENOENT;
  867. }
  868. }
  869. if (!suffix)
  870. suffix = "";
  871. snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
  872. suffix);
  873. mem = fdt_getprop(blob, config_node, prop_name, NULL);
  874. if (!mem) {
  875. debug("%s: No memory type for '%s', using /memory\n", __func__,
  876. prop_name);
  877. mem = "/memory";
  878. }
  879. node = fdt_path_offset(blob, mem);
  880. if (node < 0) {
  881. debug("%s: Failed to find node '%s': %s\n", __func__, mem,
  882. fdt_strerror(node));
  883. return -ENOENT;
  884. }
  885. /*
  886. * Not strictly correct - the memory may have multiple banks. We just
  887. * use the first
  888. */
  889. if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
  890. debug("%s: Failed to decode memory region %s\n", __func__,
  891. mem);
  892. return -EINVAL;
  893. }
  894. snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
  895. suffix);
  896. if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
  897. &offset_size)) {
  898. debug("%s: Failed to decode memory region '%s'\n", __func__,
  899. prop_name);
  900. return -EINVAL;
  901. }
  902. *basep = base + offset;
  903. *sizep = offset_size;
  904. return 0;
  905. }
  906. static int decode_timing_property(const void *blob, int node, const char *name,
  907. struct timing_entry *result)
  908. {
  909. int length, ret = 0;
  910. const u32 *prop;
  911. prop = fdt_getprop(blob, node, name, &length);
  912. if (!prop) {
  913. debug("%s: could not find property %s\n",
  914. fdt_get_name(blob, node, NULL), name);
  915. return length;
  916. }
  917. if (length == sizeof(u32)) {
  918. result->typ = fdtdec_get_int(blob, node, name, 0);
  919. result->min = result->typ;
  920. result->max = result->typ;
  921. } else {
  922. ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
  923. }
  924. return ret;
  925. }
  926. int fdtdec_decode_display_timing(const void *blob, int parent, int index,
  927. struct display_timing *dt)
  928. {
  929. int i, node, timings_node;
  930. u32 val = 0;
  931. int ret = 0;
  932. timings_node = fdt_subnode_offset(blob, parent, "display-timings");
  933. if (timings_node < 0)
  934. return timings_node;
  935. for (i = 0, node = fdt_first_subnode(blob, timings_node);
  936. node > 0 && i != index;
  937. node = fdt_next_subnode(blob, node))
  938. i++;
  939. if (node < 0)
  940. return node;
  941. memset(dt, 0, sizeof(*dt));
  942. ret |= decode_timing_property(blob, node, "hback-porch",
  943. &dt->hback_porch);
  944. ret |= decode_timing_property(blob, node, "hfront-porch",
  945. &dt->hfront_porch);
  946. ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
  947. ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
  948. ret |= decode_timing_property(blob, node, "vback-porch",
  949. &dt->vback_porch);
  950. ret |= decode_timing_property(blob, node, "vfront-porch",
  951. &dt->vfront_porch);
  952. ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
  953. ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
  954. ret |= decode_timing_property(blob, node, "clock-frequency",
  955. &dt->pixelclock);
  956. dt->flags = 0;
  957. val = fdtdec_get_int(blob, node, "vsync-active", -1);
  958. if (val != -1) {
  959. dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
  960. DISPLAY_FLAGS_VSYNC_LOW;
  961. }
  962. val = fdtdec_get_int(blob, node, "hsync-active", -1);
  963. if (val != -1) {
  964. dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
  965. DISPLAY_FLAGS_HSYNC_LOW;
  966. }
  967. val = fdtdec_get_int(blob, node, "de-active", -1);
  968. if (val != -1) {
  969. dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
  970. DISPLAY_FLAGS_DE_LOW;
  971. }
  972. val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
  973. if (val != -1) {
  974. dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
  975. DISPLAY_FLAGS_PIXDATA_NEGEDGE;
  976. }
  977. if (fdtdec_get_bool(blob, node, "interlaced"))
  978. dt->flags |= DISPLAY_FLAGS_INTERLACED;
  979. if (fdtdec_get_bool(blob, node, "doublescan"))
  980. dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
  981. if (fdtdec_get_bool(blob, node, "doubleclk"))
  982. dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
  983. return ret;
  984. }
  985. int fdtdec_setup_memory_size(void)
  986. {
  987. int ret, mem;
  988. struct fdt_resource res;
  989. mem = fdt_path_offset(gd->fdt_blob, "/memory");
  990. if (mem < 0) {
  991. debug("%s: Missing /memory node\n", __func__);
  992. return -EINVAL;
  993. }
  994. ret = fdt_get_resource(gd->fdt_blob, mem, "reg", 0, &res);
  995. if (ret != 0) {
  996. debug("%s: Unable to decode first memory bank\n", __func__);
  997. return -EINVAL;
  998. }
  999. gd->ram_size = (phys_size_t)(res.end - res.start + 1);
  1000. debug("%s: Initial DRAM size %llx\n", __func__, (u64)gd->ram_size);
  1001. return 0;
  1002. }
  1003. #if defined(CONFIG_NR_DRAM_BANKS)
  1004. int fdtdec_setup_memory_banksize(void)
  1005. {
  1006. int bank, ret, mem;
  1007. struct fdt_resource res;
  1008. mem = fdt_path_offset(gd->fdt_blob, "/memory");
  1009. if (mem < 0) {
  1010. debug("%s: Missing /memory node\n", __func__);
  1011. return -EINVAL;
  1012. }
  1013. for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) {
  1014. ret = fdt_get_resource(gd->fdt_blob, mem, "reg", bank, &res);
  1015. if (ret == -FDT_ERR_NOTFOUND)
  1016. break;
  1017. if (ret != 0)
  1018. return -EINVAL;
  1019. gd->bd->bi_dram[bank].start = (phys_addr_t)res.start;
  1020. gd->bd->bi_dram[bank].size =
  1021. (phys_size_t)(res.end - res.start + 1);
  1022. debug("%s: DRAM Bank #%d: start = 0x%llx, size = 0x%llx\n",
  1023. __func__, bank,
  1024. (unsigned long long)gd->bd->bi_dram[bank].start,
  1025. (unsigned long long)gd->bd->bi_dram[bank].size);
  1026. }
  1027. return 0;
  1028. }
  1029. #endif
  1030. int fdtdec_setup(void)
  1031. {
  1032. #if CONFIG_IS_ENABLED(OF_CONTROL)
  1033. # ifdef CONFIG_OF_EMBED
  1034. /* Get a pointer to the FDT */
  1035. gd->fdt_blob = __dtb_dt_begin;
  1036. # elif defined CONFIG_OF_SEPARATE
  1037. # ifdef CONFIG_SPL_BUILD
  1038. /* FDT is at end of BSS unless it is in a different memory region */
  1039. if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS))
  1040. gd->fdt_blob = (ulong *)&_image_binary_end;
  1041. else
  1042. gd->fdt_blob = (ulong *)&__bss_end;
  1043. # else
  1044. /* FDT is at end of image */
  1045. gd->fdt_blob = (ulong *)&_end;
  1046. # endif
  1047. # elif defined(CONFIG_OF_BOARD)
  1048. /* Allow the board to override the fdt address. */
  1049. gd->fdt_blob = board_fdt_blob_setup();
  1050. # elif defined(CONFIG_OF_HOSTFILE)
  1051. if (sandbox_read_fdt_from_file()) {
  1052. puts("Failed to read control FDT\n");
  1053. return -1;
  1054. }
  1055. # endif
  1056. # ifndef CONFIG_SPL_BUILD
  1057. /* Allow the early environment to override the fdt address */
  1058. gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
  1059. (uintptr_t)gd->fdt_blob);
  1060. # endif
  1061. #endif
  1062. return fdtdec_prepare_fdt();
  1063. }
  1064. #endif /* !USE_HOSTCC */