fdtdec.c 30 KB

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