fdtdec.c 32 KB

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