fdtdec.c 30 KB

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