fdtdec.c 30 KB

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