fdtdec.c 30 KB

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