fdtdec.c 26 KB

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