fdtdec.c 30 KB

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