fdtdec.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. /*
  2. * Copyright (c) 2011 The Chromium OS Authors.
  3. * SPDX-License-Identifier: GPL-2.0+
  4. */
  5. #ifndef USE_HOSTCC
  6. #include <common.h>
  7. #include <errno.h>
  8. #include <serial.h>
  9. #include <libfdt.h>
  10. #include <fdtdec.h>
  11. #include <asm/sections.h>
  12. #include <linux/ctype.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /*
  15. * Here are the type we know about. One day we might allow drivers to
  16. * register. For now we just put them here. The COMPAT macro allows us to
  17. * turn this into a sparse list later, and keeps the ID with the name.
  18. */
  19. #define COMPAT(id, name) name
  20. static const char * const compat_names[COMPAT_COUNT] = {
  21. COMPAT(UNKNOWN, "<none>"),
  22. COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
  23. COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
  24. COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
  25. COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
  26. COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
  27. COMPAT(NVIDIA_TEGRA124_DC, "nvidia,tegra124-dc"),
  28. COMPAT(NVIDIA_TEGRA124_SOR, "nvidia,tegra124-sor"),
  29. COMPAT(NVIDIA_TEGRA124_PMC, "nvidia,tegra124-pmc"),
  30. COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
  31. COMPAT(NVIDIA_TEGRA210_SDMMC, "nvidia,tegra210-sdhci"),
  32. COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
  33. COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
  34. COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
  35. COMPAT(NVIDIA_TEGRA124_PCIE, "nvidia,tegra124-pcie"),
  36. COMPAT(NVIDIA_TEGRA30_PCIE, "nvidia,tegra30-pcie"),
  37. COMPAT(NVIDIA_TEGRA20_PCIE, "nvidia,tegra20-pcie"),
  38. COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
  39. COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"),
  40. COMPAT(SMSC_LAN9215, "smsc,lan9215"),
  41. COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
  42. COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
  43. COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
  44. COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
  45. COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
  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"),
  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,slb9645tt"),
  60. COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
  61. COMPAT(SANDBOX_LCD_SDL, "sandbox,lcd-sdl"),
  62. COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
  63. COMPAT(INTEL_MICROCODE, "intel,microcode"),
  64. COMPAT(MEMORY_SPD, "memory-spd"),
  65. COMPAT(INTEL_PANTHERPOINT_AHCI, "intel,pantherpoint-ahci"),
  66. COMPAT(INTEL_MODEL_206AX, "intel,model-206ax"),
  67. COMPAT(INTEL_GMA, "intel,gma"),
  68. COMPAT(AMS_AS3722, "ams,as3722"),
  69. COMPAT(INTEL_ICH_SPI, "intel,ich-spi"),
  70. COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"),
  71. COMPAT(INTEL_X86_PINCTRL, "intel,x86-pinctrl"),
  72. COMPAT(SOCIONEXT_XHCI, "socionext,uniphier-xhci"),
  73. COMPAT(COMPAT_INTEL_PCH, "intel,bd82x6x"),
  74. COMPAT(COMPAT_INTEL_IRQ_ROUTER, "intel,irq-router"),
  75. COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"),
  76. COMPAT(COMPAT_INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"),
  77. COMPAT(COMPAT_INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"),
  78. };
  79. const char *fdtdec_get_compatible(enum fdt_compat_id id)
  80. {
  81. /* We allow reading of the 'unknown' ID for testing purposes */
  82. assert(id >= 0 && id < COMPAT_COUNT);
  83. return compat_names[id];
  84. }
  85. fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
  86. const char *prop_name, fdt_size_t *sizep)
  87. {
  88. const fdt_addr_t *cell;
  89. int len;
  90. debug("%s: %s: ", __func__, prop_name);
  91. cell = fdt_getprop(blob, node, prop_name, &len);
  92. if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
  93. len == sizeof(fdt_addr_t) * 2)) {
  94. fdt_addr_t addr = fdt_addr_to_cpu(*cell);
  95. if (sizep) {
  96. const fdt_size_t *size;
  97. size = (fdt_size_t *)((char *)cell +
  98. sizeof(fdt_addr_t));
  99. *sizep = fdt_size_to_cpu(*size);
  100. debug("addr=%08lx, size=%llx\n",
  101. (ulong)addr, (u64)*sizep);
  102. } else {
  103. debug("%08lx\n", (ulong)addr);
  104. }
  105. return addr;
  106. }
  107. debug("(not found)\n");
  108. return FDT_ADDR_T_NONE;
  109. }
  110. fdt_addr_t fdtdec_get_addr(const void *blob, int node,
  111. const char *prop_name)
  112. {
  113. return fdtdec_get_addr_size(blob, node, prop_name, NULL);
  114. }
  115. #ifdef CONFIG_PCI
  116. int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
  117. const char *prop_name, struct fdt_pci_addr *addr)
  118. {
  119. const u32 *cell;
  120. int len;
  121. int ret = -ENOENT;
  122. debug("%s: %s: ", __func__, prop_name);
  123. /*
  124. * If we follow the pci bus bindings strictly, we should check
  125. * the value of the node's parent node's #address-cells and
  126. * #size-cells. They need to be 3 and 2 accordingly. However,
  127. * for simplicity we skip the check here.
  128. */
  129. cell = fdt_getprop(blob, node, prop_name, &len);
  130. if (!cell)
  131. goto fail;
  132. if ((len % FDT_PCI_REG_SIZE) == 0) {
  133. int num = len / FDT_PCI_REG_SIZE;
  134. int i;
  135. for (i = 0; i < num; i++) {
  136. debug("pci address #%d: %08lx %08lx %08lx\n", i,
  137. (ulong)fdt_addr_to_cpu(cell[0]),
  138. (ulong)fdt_addr_to_cpu(cell[1]),
  139. (ulong)fdt_addr_to_cpu(cell[2]));
  140. if ((fdt_addr_to_cpu(*cell) & type) == type) {
  141. addr->phys_hi = fdt_addr_to_cpu(cell[0]);
  142. addr->phys_mid = fdt_addr_to_cpu(cell[1]);
  143. addr->phys_lo = fdt_addr_to_cpu(cell[2]);
  144. break;
  145. } else {
  146. cell += (FDT_PCI_ADDR_CELLS +
  147. FDT_PCI_SIZE_CELLS);
  148. }
  149. }
  150. if (i == num) {
  151. ret = -ENXIO;
  152. goto fail;
  153. }
  154. return 0;
  155. } else {
  156. ret = -EINVAL;
  157. }
  158. fail:
  159. debug("(not found)\n");
  160. return ret;
  161. }
  162. int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
  163. {
  164. const char *list, *end;
  165. int len;
  166. list = fdt_getprop(blob, node, "compatible", &len);
  167. if (!list)
  168. return -ENOENT;
  169. end = list + len;
  170. while (list < end) {
  171. char *s;
  172. len = strlen(list);
  173. if (len >= strlen("pciVVVV,DDDD")) {
  174. s = strstr(list, "pci");
  175. /*
  176. * check if the string is something like pciVVVV,DDDD.RR
  177. * or just pciVVVV,DDDD
  178. */
  179. if (s && s[7] == ',' &&
  180. (s[12] == '.' || s[12] == 0)) {
  181. s += 3;
  182. *vendor = simple_strtol(s, NULL, 16);
  183. s += 5;
  184. *device = simple_strtol(s, NULL, 16);
  185. return 0;
  186. }
  187. }
  188. list += (len + 1);
  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. int len, val;
  443. prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
  444. debug(" - %s, %s\n", name, prop);
  445. if (len < find_namelen || *prop != '/' || prop[len - 1] ||
  446. strncmp(name, base, base_len))
  447. continue;
  448. slash = strrchr(prop, '/');
  449. if (strcmp(slash + 1, find_name))
  450. continue;
  451. val = trailing_strtol(name);
  452. if (val != -1) {
  453. *seqp = val;
  454. debug("Found seq %d\n", *seqp);
  455. return 0;
  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. #ifdef CONFIG_SPL_BUILD
  495. puts("Missing DTB\n");
  496. #else
  497. 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");
  498. # ifdef DEBUG
  499. if (gd->fdt_blob) {
  500. printf("fdt_blob=%p\n", gd->fdt_blob);
  501. print_buffer((ulong)gd->fdt_blob, gd->fdt_blob, 4,
  502. 32, 0);
  503. }
  504. # endif
  505. #endif
  506. return -1;
  507. }
  508. return 0;
  509. }
  510. int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
  511. {
  512. const u32 *phandle;
  513. int lookup;
  514. debug("%s: %s\n", __func__, prop_name);
  515. phandle = fdt_getprop(blob, node, prop_name, NULL);
  516. if (!phandle)
  517. return -FDT_ERR_NOTFOUND;
  518. lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
  519. return lookup;
  520. }
  521. /**
  522. * Look up a property in a node and check that it has a minimum length.
  523. *
  524. * @param blob FDT blob
  525. * @param node node to examine
  526. * @param prop_name name of property to find
  527. * @param min_len minimum property length in bytes
  528. * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
  529. found, or -FDT_ERR_BADLAYOUT if not enough data
  530. * @return pointer to cell, which is only valid if err == 0
  531. */
  532. static const void *get_prop_check_min_len(const void *blob, int node,
  533. const char *prop_name, int min_len, int *err)
  534. {
  535. const void *cell;
  536. int len;
  537. debug("%s: %s\n", __func__, prop_name);
  538. cell = fdt_getprop(blob, node, prop_name, &len);
  539. if (!cell)
  540. *err = -FDT_ERR_NOTFOUND;
  541. else if (len < min_len)
  542. *err = -FDT_ERR_BADLAYOUT;
  543. else
  544. *err = 0;
  545. return cell;
  546. }
  547. int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
  548. u32 *array, int count)
  549. {
  550. const u32 *cell;
  551. int i, err = 0;
  552. debug("%s: %s\n", __func__, prop_name);
  553. cell = get_prop_check_min_len(blob, node, prop_name,
  554. sizeof(u32) * count, &err);
  555. if (!err) {
  556. for (i = 0; i < count; i++)
  557. array[i] = fdt32_to_cpu(cell[i]);
  558. }
  559. return err;
  560. }
  561. int fdtdec_get_int_array_count(const void *blob, int node,
  562. const char *prop_name, u32 *array, int count)
  563. {
  564. const u32 *cell;
  565. int len, elems;
  566. int i;
  567. debug("%s: %s\n", __func__, prop_name);
  568. cell = fdt_getprop(blob, node, prop_name, &len);
  569. if (!cell)
  570. return -FDT_ERR_NOTFOUND;
  571. elems = len / sizeof(u32);
  572. if (count > elems)
  573. count = elems;
  574. for (i = 0; i < count; i++)
  575. array[i] = fdt32_to_cpu(cell[i]);
  576. return count;
  577. }
  578. const u32 *fdtdec_locate_array(const void *blob, int node,
  579. const char *prop_name, int count)
  580. {
  581. const u32 *cell;
  582. int err;
  583. cell = get_prop_check_min_len(blob, node, prop_name,
  584. sizeof(u32) * count, &err);
  585. return err ? NULL : cell;
  586. }
  587. int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
  588. {
  589. const s32 *cell;
  590. int len;
  591. debug("%s: %s\n", __func__, prop_name);
  592. cell = fdt_getprop(blob, node, prop_name, &len);
  593. return cell != NULL;
  594. }
  595. int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
  596. const char *list_name,
  597. const char *cells_name,
  598. int cell_count, int index,
  599. struct fdtdec_phandle_args *out_args)
  600. {
  601. const __be32 *list, *list_end;
  602. int rc = 0, size, cur_index = 0;
  603. uint32_t count = 0;
  604. int node = -1;
  605. int phandle;
  606. /* Retrieve the phandle list property */
  607. list = fdt_getprop(blob, src_node, list_name, &size);
  608. if (!list)
  609. return -ENOENT;
  610. list_end = list + size / sizeof(*list);
  611. /* Loop over the phandles until all the requested entry is found */
  612. while (list < list_end) {
  613. rc = -EINVAL;
  614. count = 0;
  615. /*
  616. * If phandle is 0, then it is an empty entry with no
  617. * arguments. Skip forward to the next entry.
  618. */
  619. phandle = be32_to_cpup(list++);
  620. if (phandle) {
  621. /*
  622. * Find the provider node and parse the #*-cells
  623. * property to determine the argument length.
  624. *
  625. * This is not needed if the cell count is hard-coded
  626. * (i.e. cells_name not set, but cell_count is set),
  627. * except when we're going to return the found node
  628. * below.
  629. */
  630. if (cells_name || cur_index == index) {
  631. node = fdt_node_offset_by_phandle(blob,
  632. phandle);
  633. if (!node) {
  634. debug("%s: could not find phandle\n",
  635. fdt_get_name(blob, src_node,
  636. NULL));
  637. goto err;
  638. }
  639. }
  640. if (cells_name) {
  641. count = fdtdec_get_int(blob, node, cells_name,
  642. -1);
  643. if (count == -1) {
  644. debug("%s: could not get %s for %s\n",
  645. fdt_get_name(blob, src_node,
  646. NULL),
  647. cells_name,
  648. fdt_get_name(blob, node,
  649. NULL));
  650. goto err;
  651. }
  652. } else {
  653. count = cell_count;
  654. }
  655. /*
  656. * Make sure that the arguments actually fit in the
  657. * remaining property data length
  658. */
  659. if (list + count > list_end) {
  660. debug("%s: arguments longer than property\n",
  661. fdt_get_name(blob, src_node, NULL));
  662. goto err;
  663. }
  664. }
  665. /*
  666. * All of the error cases above bail out of the loop, so at
  667. * this point, the parsing is successful. If the requested
  668. * index matches, then fill the out_args structure and return,
  669. * or return -ENOENT for an empty entry.
  670. */
  671. rc = -ENOENT;
  672. if (cur_index == index) {
  673. if (!phandle)
  674. goto err;
  675. if (out_args) {
  676. int i;
  677. if (count > MAX_PHANDLE_ARGS) {
  678. debug("%s: too many arguments %d\n",
  679. fdt_get_name(blob, src_node,
  680. NULL), count);
  681. count = MAX_PHANDLE_ARGS;
  682. }
  683. out_args->node = node;
  684. out_args->args_count = count;
  685. for (i = 0; i < count; i++) {
  686. out_args->args[i] =
  687. be32_to_cpup(list++);
  688. }
  689. }
  690. /* Found it! return success */
  691. return 0;
  692. }
  693. node = -1;
  694. list += count;
  695. cur_index++;
  696. }
  697. /*
  698. * Result will be one of:
  699. * -ENOENT : index is for empty phandle
  700. * -EINVAL : parsing error on data
  701. * [1..n] : Number of phandle (count mode; when index = -1)
  702. */
  703. rc = index < 0 ? cur_index : -ENOENT;
  704. err:
  705. return rc;
  706. }
  707. int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
  708. u8 *array, int count)
  709. {
  710. const u8 *cell;
  711. int err;
  712. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  713. if (!err)
  714. memcpy(array, cell, count);
  715. return err;
  716. }
  717. const u8 *fdtdec_locate_byte_array(const void *blob, int node,
  718. const char *prop_name, int count)
  719. {
  720. const u8 *cell;
  721. int err;
  722. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  723. if (err)
  724. return NULL;
  725. return cell;
  726. }
  727. int fdtdec_get_config_int(const void *blob, const char *prop_name,
  728. int default_val)
  729. {
  730. int config_node;
  731. debug("%s: %s\n", __func__, prop_name);
  732. config_node = fdt_path_offset(blob, "/config");
  733. if (config_node < 0)
  734. return default_val;
  735. return fdtdec_get_int(blob, config_node, prop_name, default_val);
  736. }
  737. int fdtdec_get_config_bool(const void *blob, const char *prop_name)
  738. {
  739. int config_node;
  740. const void *prop;
  741. debug("%s: %s\n", __func__, prop_name);
  742. config_node = fdt_path_offset(blob, "/config");
  743. if (config_node < 0)
  744. return 0;
  745. prop = fdt_get_property(blob, config_node, prop_name, NULL);
  746. return prop != NULL;
  747. }
  748. char *fdtdec_get_config_string(const void *blob, const char *prop_name)
  749. {
  750. const char *nodep;
  751. int nodeoffset;
  752. int len;
  753. debug("%s: %s\n", __func__, prop_name);
  754. nodeoffset = fdt_path_offset(blob, "/config");
  755. if (nodeoffset < 0)
  756. return NULL;
  757. nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
  758. if (!nodep)
  759. return NULL;
  760. return (char *)nodep;
  761. }
  762. int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
  763. fdt_addr_t *basep, fdt_size_t *sizep)
  764. {
  765. const fdt_addr_t *cell;
  766. int len;
  767. debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
  768. prop_name);
  769. cell = fdt_getprop(blob, node, prop_name, &len);
  770. if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
  771. debug("cell=%p, len=%d\n", cell, len);
  772. return -1;
  773. }
  774. *basep = fdt_addr_to_cpu(*cell);
  775. *sizep = fdt_size_to_cpu(cell[1]);
  776. debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
  777. (ulong)*sizep);
  778. return 0;
  779. }
  780. /**
  781. * Read a flash entry from the fdt
  782. *
  783. * @param blob FDT blob
  784. * @param node Offset of node to read
  785. * @param name Name of node being read
  786. * @param entry Place to put offset and size of this node
  787. * @return 0 if ok, -ve on error
  788. */
  789. int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
  790. struct fmap_entry *entry)
  791. {
  792. const char *prop;
  793. u32 reg[2];
  794. if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
  795. debug("Node '%s' has bad/missing 'reg' property\n", name);
  796. return -FDT_ERR_NOTFOUND;
  797. }
  798. entry->offset = reg[0];
  799. entry->length = reg[1];
  800. entry->used = fdtdec_get_int(blob, node, "used", entry->length);
  801. prop = fdt_getprop(blob, node, "compress", NULL);
  802. entry->compress_algo = prop && !strcmp(prop, "lzo") ?
  803. FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
  804. prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
  805. entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
  806. entry->hash = (uint8_t *)prop;
  807. return 0;
  808. }
  809. u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
  810. {
  811. u64 number = 0;
  812. while (cells--)
  813. number = (number << 32) | fdt32_to_cpu(*ptr++);
  814. return number;
  815. }
  816. int fdt_get_resource(const void *fdt, int node, const char *property,
  817. unsigned int index, struct fdt_resource *res)
  818. {
  819. const fdt32_t *ptr, *end;
  820. int na, ns, len, parent;
  821. unsigned int i = 0;
  822. parent = fdt_parent_offset(fdt, node);
  823. if (parent < 0)
  824. return parent;
  825. na = fdt_address_cells(fdt, parent);
  826. ns = fdt_size_cells(fdt, parent);
  827. ptr = fdt_getprop(fdt, node, property, &len);
  828. if (!ptr)
  829. return len;
  830. end = ptr + len / sizeof(*ptr);
  831. while (ptr + na + ns <= end) {
  832. if (i == index) {
  833. res->start = res->end = fdtdec_get_number(ptr, na);
  834. res->end += fdtdec_get_number(&ptr[na], ns) - 1;
  835. return 0;
  836. }
  837. ptr += na + ns;
  838. i++;
  839. }
  840. return -FDT_ERR_NOTFOUND;
  841. }
  842. int fdt_get_named_resource(const void *fdt, int node, const char *property,
  843. const char *prop_names, const char *name,
  844. struct fdt_resource *res)
  845. {
  846. int index;
  847. index = fdt_find_string(fdt, node, prop_names, name);
  848. if (index < 0)
  849. return index;
  850. return fdt_get_resource(fdt, node, property, index, res);
  851. }
  852. int fdtdec_decode_memory_region(const void *blob, int config_node,
  853. const char *mem_type, const char *suffix,
  854. fdt_addr_t *basep, fdt_size_t *sizep)
  855. {
  856. char prop_name[50];
  857. const char *mem;
  858. fdt_size_t size, offset_size;
  859. fdt_addr_t base, offset;
  860. int node;
  861. if (config_node == -1) {
  862. config_node = fdt_path_offset(blob, "/config");
  863. if (config_node < 0) {
  864. debug("%s: Cannot find /config node\n", __func__);
  865. return -ENOENT;
  866. }
  867. }
  868. if (!suffix)
  869. suffix = "";
  870. snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
  871. suffix);
  872. mem = fdt_getprop(blob, config_node, prop_name, NULL);
  873. if (!mem) {
  874. debug("%s: No memory type for '%s', using /memory\n", __func__,
  875. prop_name);
  876. mem = "/memory";
  877. }
  878. node = fdt_path_offset(blob, mem);
  879. if (node < 0) {
  880. debug("%s: Failed to find node '%s': %s\n", __func__, mem,
  881. fdt_strerror(node));
  882. return -ENOENT;
  883. }
  884. /*
  885. * Not strictly correct - the memory may have multiple banks. We just
  886. * use the first
  887. */
  888. if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
  889. debug("%s: Failed to decode memory region %s\n", __func__,
  890. mem);
  891. return -EINVAL;
  892. }
  893. snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
  894. suffix);
  895. if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
  896. &offset_size)) {
  897. debug("%s: Failed to decode memory region '%s'\n", __func__,
  898. prop_name);
  899. return -EINVAL;
  900. }
  901. *basep = base + offset;
  902. *sizep = offset_size;
  903. return 0;
  904. }
  905. static int decode_timing_property(const void *blob, int node, const char *name,
  906. struct timing_entry *result)
  907. {
  908. int length, ret = 0;
  909. const u32 *prop;
  910. prop = fdt_getprop(blob, node, name, &length);
  911. if (!prop) {
  912. debug("%s: could not find property %s\n",
  913. fdt_get_name(blob, node, NULL), name);
  914. return length;
  915. }
  916. if (length == sizeof(u32)) {
  917. result->typ = fdtdec_get_int(blob, node, name, 0);
  918. result->min = result->typ;
  919. result->max = result->typ;
  920. } else {
  921. ret = fdtdec_get_int_array(blob, node, name, &result->min, 3);
  922. }
  923. return ret;
  924. }
  925. int fdtdec_decode_display_timing(const void *blob, int parent, int index,
  926. struct display_timing *dt)
  927. {
  928. int i, node, timings_node;
  929. u32 val = 0;
  930. int ret = 0;
  931. timings_node = fdt_subnode_offset(blob, parent, "display-timings");
  932. if (timings_node < 0)
  933. return timings_node;
  934. for (i = 0, node = fdt_first_subnode(blob, timings_node);
  935. node > 0 && i != index;
  936. node = fdt_next_subnode(blob, node))
  937. i++;
  938. if (node < 0)
  939. return node;
  940. memset(dt, 0, sizeof(*dt));
  941. ret |= decode_timing_property(blob, node, "hback-porch",
  942. &dt->hback_porch);
  943. ret |= decode_timing_property(blob, node, "hfront-porch",
  944. &dt->hfront_porch);
  945. ret |= decode_timing_property(blob, node, "hactive", &dt->hactive);
  946. ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len);
  947. ret |= decode_timing_property(blob, node, "vback-porch",
  948. &dt->vback_porch);
  949. ret |= decode_timing_property(blob, node, "vfront-porch",
  950. &dt->vfront_porch);
  951. ret |= decode_timing_property(blob, node, "vactive", &dt->vactive);
  952. ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len);
  953. ret |= decode_timing_property(blob, node, "clock-frequency",
  954. &dt->pixelclock);
  955. dt->flags = 0;
  956. val = fdtdec_get_int(blob, node, "vsync-active", -1);
  957. if (val != -1) {
  958. dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
  959. DISPLAY_FLAGS_VSYNC_LOW;
  960. }
  961. val = fdtdec_get_int(blob, node, "hsync-active", -1);
  962. if (val != -1) {
  963. dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
  964. DISPLAY_FLAGS_HSYNC_LOW;
  965. }
  966. val = fdtdec_get_int(blob, node, "de-active", -1);
  967. if (val != -1) {
  968. dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
  969. DISPLAY_FLAGS_DE_LOW;
  970. }
  971. val = fdtdec_get_int(blob, node, "pixelclk-active", -1);
  972. if (val != -1) {
  973. dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
  974. DISPLAY_FLAGS_PIXDATA_NEGEDGE;
  975. }
  976. if (fdtdec_get_bool(blob, node, "interlaced"))
  977. dt->flags |= DISPLAY_FLAGS_INTERLACED;
  978. if (fdtdec_get_bool(blob, node, "doublescan"))
  979. dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
  980. if (fdtdec_get_bool(blob, node, "doubleclk"))
  981. dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
  982. return 0;
  983. }
  984. int fdtdec_setup(void)
  985. {
  986. #if CONFIG_IS_ENABLED(OF_CONTROL)
  987. # ifdef CONFIG_OF_EMBED
  988. /* Get a pointer to the FDT */
  989. gd->fdt_blob = __dtb_dt_begin;
  990. # elif defined CONFIG_OF_SEPARATE
  991. # ifdef CONFIG_SPL_BUILD
  992. /* FDT is at end of BSS */
  993. gd->fdt_blob = (ulong *)&__bss_end;
  994. # else
  995. /* FDT is at end of image */
  996. gd->fdt_blob = (ulong *)&_end;
  997. # endif
  998. # elif defined(CONFIG_OF_HOSTFILE)
  999. if (sandbox_read_fdt_from_file()) {
  1000. puts("Failed to read control FDT\n");
  1001. return -1;
  1002. }
  1003. # endif
  1004. # ifndef CONFIG_SPL_BUILD
  1005. /* Allow the early environment to override the fdt address */
  1006. gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
  1007. (uintptr_t)gd->fdt_blob);
  1008. # endif
  1009. #endif
  1010. return fdtdec_prepare_fdt();
  1011. }
  1012. #endif /* !USE_HOSTCC */