fdtdec.c 31 KB

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