fdtdec.c 30 KB

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