fdtdec.c 27 KB

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