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