fdtdec.c 30 KB

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