fdtdec.c 31 KB

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