fdtdec.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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 <linux/ctype.h>
  12. #include <asm/gpio.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_TEGRA114_I2C, "nvidia,tegra114-i2c"),
  26. COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
  27. COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
  28. COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
  29. COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
  30. COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
  31. COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
  32. COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
  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_TEGRA20_SFLASH, "nvidia,tegra20-sflash"),
  38. COMPAT(NVIDIA_TEGRA20_SLINK, "nvidia,tegra20-slink"),
  39. COMPAT(NVIDIA_TEGRA114_SPI, "nvidia,tegra114-spi"),
  40. COMPAT(NVIDIA_TEGRA124_PCIE, "nvidia,tegra124-pcie"),
  41. COMPAT(NVIDIA_TEGRA30_PCIE, "nvidia,tegra30-pcie"),
  42. COMPAT(NVIDIA_TEGRA20_PCIE, "nvidia,tegra20-pcie"),
  43. COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
  44. COMPAT(SMSC_LAN9215, "smsc,lan9215"),
  45. COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
  46. COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
  47. COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
  48. COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
  49. COMPAT(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"),
  50. COMPAT(GOOGLE_CROS_EC, "google,cros-ec"),
  51. COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
  52. COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
  53. COMPAT(SAMSUNG_EXYNOS5_XHCI, "samsung,exynos5250-xhci"),
  54. COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
  55. COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
  56. COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
  57. COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
  58. COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
  59. COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
  60. COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
  61. COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
  62. COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
  63. COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"),
  64. COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
  65. COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
  66. COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
  67. COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"),
  68. COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
  69. COMPAT(SANDBOX_HOST_EMULATION, "sandbox,host-emulation"),
  70. COMPAT(SANDBOX_LCD_SDL, "sandbox,lcd-sdl"),
  71. COMPAT(TI_TPS65090, "ti,tps65090"),
  72. COMPAT(COMPAT_NXP_PTN3460, "nxp,ptn3460"),
  73. COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
  74. COMPAT(PARADE_PS8625, "parade,ps8625"),
  75. COMPAT(COMPAT_INTEL_LPC, "intel,lpc"),
  76. COMPAT(INTEL_MICROCODE, "intel,microcode"),
  77. COMPAT(MEMORY_SPD, "memory-spd"),
  78. COMPAT(INTEL_PANTHERPOINT_AHCI, "intel,pantherpoint-ahci"),
  79. COMPAT(INTEL_MODEL_206AX, "intel,model-206ax"),
  80. COMPAT(INTEL_GMA, "intel,gma"),
  81. COMPAT(AMS_AS3722, "ams,as3722"),
  82. COMPAT(INTEL_ICH_SPI, "intel,ich-spi"),
  83. };
  84. const char *fdtdec_get_compatible(enum fdt_compat_id id)
  85. {
  86. /* We allow reading of the 'unknown' ID for testing purposes */
  87. assert(id >= 0 && id < COMPAT_COUNT);
  88. return compat_names[id];
  89. }
  90. fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
  91. const char *prop_name, fdt_size_t *sizep)
  92. {
  93. const fdt_addr_t *cell;
  94. int len;
  95. debug("%s: %s: ", __func__, prop_name);
  96. cell = fdt_getprop(blob, node, prop_name, &len);
  97. if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
  98. len == sizeof(fdt_addr_t) * 2)) {
  99. fdt_addr_t addr = fdt_addr_to_cpu(*cell);
  100. if (sizep) {
  101. const fdt_size_t *size;
  102. size = (fdt_size_t *)((char *)cell +
  103. sizeof(fdt_addr_t));
  104. *sizep = fdt_size_to_cpu(*size);
  105. debug("addr=%08lx, size=%08x\n",
  106. (ulong)addr, *sizep);
  107. } else {
  108. debug("%08lx\n", (ulong)addr);
  109. }
  110. return addr;
  111. }
  112. debug("(not found)\n");
  113. return FDT_ADDR_T_NONE;
  114. }
  115. fdt_addr_t fdtdec_get_addr(const void *blob, int node,
  116. const char *prop_name)
  117. {
  118. return fdtdec_get_addr_size(blob, node, prop_name, NULL);
  119. }
  120. #ifdef CONFIG_PCI
  121. int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
  122. const char *prop_name, struct fdt_pci_addr *addr)
  123. {
  124. const u32 *cell;
  125. int len;
  126. int ret = -ENOENT;
  127. debug("%s: %s: ", __func__, prop_name);
  128. /*
  129. * If we follow the pci bus bindings strictly, we should check
  130. * the value of the node's parent node's #address-cells and
  131. * #size-cells. They need to be 3 and 2 accordingly. However,
  132. * for simplicity we skip the check here.
  133. */
  134. cell = fdt_getprop(blob, node, prop_name, &len);
  135. if (!cell)
  136. goto fail;
  137. if ((len % FDT_PCI_REG_SIZE) == 0) {
  138. int num = len / FDT_PCI_REG_SIZE;
  139. int i;
  140. for (i = 0; i < num; i++) {
  141. debug("pci address #%d: %08lx %08lx %08lx\n", i,
  142. (ulong)fdt_addr_to_cpu(cell[0]),
  143. (ulong)fdt_addr_to_cpu(cell[1]),
  144. (ulong)fdt_addr_to_cpu(cell[2]));
  145. if ((fdt_addr_to_cpu(*cell) & type) == type) {
  146. addr->phys_hi = fdt_addr_to_cpu(cell[0]);
  147. addr->phys_mid = fdt_addr_to_cpu(cell[1]);
  148. addr->phys_lo = fdt_addr_to_cpu(cell[2]);
  149. break;
  150. } else {
  151. cell += (FDT_PCI_ADDR_CELLS +
  152. FDT_PCI_SIZE_CELLS);
  153. }
  154. }
  155. if (i == num)
  156. goto fail;
  157. return 0;
  158. } else {
  159. ret = -EINVAL;
  160. }
  161. fail:
  162. debug("(not found)\n");
  163. return ret;
  164. }
  165. int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
  166. {
  167. const char *list, *end;
  168. int len;
  169. list = fdt_getprop(blob, node, "compatible", &len);
  170. if (!list)
  171. return -ENOENT;
  172. end = list + len;
  173. while (list < end) {
  174. char *s;
  175. len = strlen(list);
  176. if (len >= strlen("pciVVVV,DDDD")) {
  177. s = strstr(list, "pci");
  178. /*
  179. * check if the string is something like pciVVVV,DDDD.RR
  180. * or just pciVVVV,DDDD
  181. */
  182. if (s && s[7] == ',' &&
  183. (s[12] == '.' || s[12] == 0)) {
  184. s += 3;
  185. *vendor = simple_strtol(s, NULL, 16);
  186. s += 5;
  187. *device = simple_strtol(s, NULL, 16);
  188. return 0;
  189. }
  190. } else {
  191. list += (len + 1);
  192. }
  193. }
  194. return -ENOENT;
  195. }
  196. int fdtdec_get_pci_bdf(const void *blob, int node,
  197. struct fdt_pci_addr *addr, pci_dev_t *bdf)
  198. {
  199. u16 dt_vendor, dt_device, vendor, device;
  200. int ret;
  201. /* get vendor id & device id from the compatible string */
  202. ret = fdtdec_get_pci_vendev(blob, node, &dt_vendor, &dt_device);
  203. if (ret)
  204. return ret;
  205. /* extract the bdf from fdt_pci_addr */
  206. *bdf = addr->phys_hi & 0xffff00;
  207. /* read vendor id & device id based on bdf */
  208. pci_read_config_word(*bdf, PCI_VENDOR_ID, &vendor);
  209. pci_read_config_word(*bdf, PCI_DEVICE_ID, &device);
  210. /*
  211. * Note there are two places in the device tree to fully describe
  212. * a pci device: one is via compatible string with a format of
  213. * "pciVVVV,DDDD" and the other one is the bdf numbers encoded in
  214. * the device node's reg address property. We read the vendor id
  215. * and device id based on bdf and compare the values with the
  216. * "VVVV,DDDD". If they are the same, then we are good to use bdf
  217. * to read device's bar. But if they are different, we have to rely
  218. * on the vendor id and device id extracted from the compatible
  219. * string and locate the real bdf by pci_find_device(). This is
  220. * because normally we may only know device's device number and
  221. * function number when writing device tree. The bus number is
  222. * dynamically assigned during the pci enumeration process.
  223. */
  224. if ((dt_vendor != vendor) || (dt_device != device)) {
  225. *bdf = pci_find_device(dt_vendor, dt_device, 0);
  226. if (*bdf == -1)
  227. return -ENODEV;
  228. }
  229. return 0;
  230. }
  231. int fdtdec_get_pci_bar32(const void *blob, int node,
  232. struct fdt_pci_addr *addr, u32 *bar)
  233. {
  234. pci_dev_t bdf;
  235. int barnum;
  236. int ret;
  237. /* get pci devices's bdf */
  238. ret = fdtdec_get_pci_bdf(blob, node, addr, &bdf);
  239. if (ret)
  240. return ret;
  241. /* extract the bar number from fdt_pci_addr */
  242. barnum = addr->phys_hi & 0xff;
  243. if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
  244. return -EINVAL;
  245. barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
  246. *bar = pci_read_bar32(pci_bus_to_hose(PCI_BUS(bdf)), bdf, barnum);
  247. return 0;
  248. }
  249. #endif
  250. uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
  251. uint64_t default_val)
  252. {
  253. const uint64_t *cell64;
  254. int length;
  255. cell64 = fdt_getprop(blob, node, prop_name, &length);
  256. if (!cell64 || length < sizeof(*cell64))
  257. return default_val;
  258. return fdt64_to_cpu(*cell64);
  259. }
  260. int fdtdec_get_is_enabled(const void *blob, int node)
  261. {
  262. const char *cell;
  263. /*
  264. * It should say "okay", so only allow that. Some fdts use "ok" but
  265. * this is a bug. Please fix your device tree source file. See here
  266. * for discussion:
  267. *
  268. * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
  269. */
  270. cell = fdt_getprop(blob, node, "status", NULL);
  271. if (cell)
  272. return 0 == strcmp(cell, "okay");
  273. return 1;
  274. }
  275. enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
  276. {
  277. enum fdt_compat_id id;
  278. /* Search our drivers */
  279. for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
  280. if (0 == fdt_node_check_compatible(blob, node,
  281. compat_names[id]))
  282. return id;
  283. return COMPAT_UNKNOWN;
  284. }
  285. int fdtdec_next_compatible(const void *blob, int node,
  286. enum fdt_compat_id id)
  287. {
  288. return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
  289. }
  290. int fdtdec_next_compatible_subnode(const void *blob, int node,
  291. enum fdt_compat_id id, int *depthp)
  292. {
  293. do {
  294. node = fdt_next_node(blob, node, depthp);
  295. } while (*depthp > 1);
  296. /* If this is a direct subnode, and compatible, return it */
  297. if (*depthp == 1 && 0 == fdt_node_check_compatible(
  298. blob, node, compat_names[id]))
  299. return node;
  300. return -FDT_ERR_NOTFOUND;
  301. }
  302. int fdtdec_next_alias(const void *blob, const char *name,
  303. enum fdt_compat_id id, int *upto)
  304. {
  305. #define MAX_STR_LEN 20
  306. char str[MAX_STR_LEN + 20];
  307. int node, err;
  308. /* snprintf() is not available */
  309. assert(strlen(name) < MAX_STR_LEN);
  310. sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
  311. node = fdt_path_offset(blob, str);
  312. if (node < 0)
  313. return node;
  314. err = fdt_node_check_compatible(blob, node, compat_names[id]);
  315. if (err < 0)
  316. return err;
  317. if (err)
  318. return -FDT_ERR_NOTFOUND;
  319. (*upto)++;
  320. return node;
  321. }
  322. int fdtdec_find_aliases_for_id(const void *blob, const char *name,
  323. enum fdt_compat_id id, int *node_list, int maxcount)
  324. {
  325. memset(node_list, '\0', sizeof(*node_list) * maxcount);
  326. return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
  327. }
  328. /* TODO: Can we tighten this code up a little? */
  329. int fdtdec_add_aliases_for_id(const void *blob, const char *name,
  330. enum fdt_compat_id id, int *node_list, int maxcount)
  331. {
  332. int name_len = strlen(name);
  333. int nodes[maxcount];
  334. int num_found = 0;
  335. int offset, node;
  336. int alias_node;
  337. int count;
  338. int i, j;
  339. /* find the alias node if present */
  340. alias_node = fdt_path_offset(blob, "/aliases");
  341. /*
  342. * start with nothing, and we can assume that the root node can't
  343. * match
  344. */
  345. memset(nodes, '\0', sizeof(nodes));
  346. /* First find all the compatible nodes */
  347. for (node = count = 0; node >= 0 && count < maxcount;) {
  348. node = fdtdec_next_compatible(blob, node, id);
  349. if (node >= 0)
  350. nodes[count++] = node;
  351. }
  352. if (node >= 0)
  353. debug("%s: warning: maxcount exceeded with alias '%s'\n",
  354. __func__, name);
  355. /* Now find all the aliases */
  356. for (offset = fdt_first_property_offset(blob, alias_node);
  357. offset > 0;
  358. offset = fdt_next_property_offset(blob, offset)) {
  359. const struct fdt_property *prop;
  360. const char *path;
  361. int number;
  362. int found;
  363. node = 0;
  364. prop = fdt_get_property_by_offset(blob, offset, NULL);
  365. path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  366. if (prop->len && 0 == strncmp(path, name, name_len))
  367. node = fdt_path_offset(blob, prop->data);
  368. if (node <= 0)
  369. continue;
  370. /* Get the alias number */
  371. number = simple_strtoul(path + name_len, NULL, 10);
  372. if (number < 0 || number >= maxcount) {
  373. debug("%s: warning: alias '%s' is out of range\n",
  374. __func__, path);
  375. continue;
  376. }
  377. /* Make sure the node we found is actually in our list! */
  378. found = -1;
  379. for (j = 0; j < count; j++)
  380. if (nodes[j] == node) {
  381. found = j;
  382. break;
  383. }
  384. if (found == -1) {
  385. debug("%s: warning: alias '%s' points to a node "
  386. "'%s' that is missing or is not compatible "
  387. " with '%s'\n", __func__, path,
  388. fdt_get_name(blob, node, NULL),
  389. compat_names[id]);
  390. continue;
  391. }
  392. /*
  393. * Add this node to our list in the right place, and mark
  394. * it as done.
  395. */
  396. if (fdtdec_get_is_enabled(blob, node)) {
  397. if (node_list[number]) {
  398. debug("%s: warning: alias '%s' requires that "
  399. "a node be placed in the list in a "
  400. "position which is already filled by "
  401. "node '%s'\n", __func__, path,
  402. fdt_get_name(blob, node, NULL));
  403. continue;
  404. }
  405. node_list[number] = node;
  406. if (number >= num_found)
  407. num_found = number + 1;
  408. }
  409. nodes[found] = 0;
  410. }
  411. /* Add any nodes not mentioned by an alias */
  412. for (i = j = 0; i < maxcount; i++) {
  413. if (!node_list[i]) {
  414. for (; j < maxcount; j++)
  415. if (nodes[j] &&
  416. fdtdec_get_is_enabled(blob, nodes[j]))
  417. break;
  418. /* Have we run out of nodes to add? */
  419. if (j == maxcount)
  420. break;
  421. assert(!node_list[i]);
  422. node_list[i] = nodes[j++];
  423. if (i >= num_found)
  424. num_found = i + 1;
  425. }
  426. }
  427. return num_found;
  428. }
  429. int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
  430. int *seqp)
  431. {
  432. int base_len = strlen(base);
  433. const char *find_name;
  434. int find_namelen;
  435. int prop_offset;
  436. int aliases;
  437. find_name = fdt_get_name(blob, offset, &find_namelen);
  438. debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
  439. aliases = fdt_path_offset(blob, "/aliases");
  440. for (prop_offset = fdt_first_property_offset(blob, aliases);
  441. prop_offset > 0;
  442. prop_offset = fdt_next_property_offset(blob, prop_offset)) {
  443. const char *prop;
  444. const char *name;
  445. const char *slash;
  446. const char *p;
  447. int len;
  448. prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
  449. debug(" - %s, %s\n", name, prop);
  450. if (len < find_namelen || *prop != '/' || prop[len - 1] ||
  451. strncmp(name, base, base_len))
  452. continue;
  453. slash = strrchr(prop, '/');
  454. if (strcmp(slash + 1, find_name))
  455. continue;
  456. for (p = name + strlen(name) - 1; p > name; p--) {
  457. if (!isdigit(*p)) {
  458. *seqp = simple_strtoul(p + 1, NULL, 10);
  459. debug("Found seq %d\n", *seqp);
  460. return 0;
  461. }
  462. }
  463. }
  464. debug("Not found\n");
  465. return -ENOENT;
  466. }
  467. int fdtdec_get_chosen_node(const void *blob, const char *name)
  468. {
  469. const char *prop;
  470. int chosen_node;
  471. int len;
  472. if (!blob)
  473. return -FDT_ERR_NOTFOUND;
  474. chosen_node = fdt_path_offset(blob, "/chosen");
  475. prop = fdt_getprop(blob, chosen_node, name, &len);
  476. if (!prop)
  477. return -FDT_ERR_NOTFOUND;
  478. return fdt_path_offset(blob, prop);
  479. }
  480. int fdtdec_check_fdt(void)
  481. {
  482. /*
  483. * We must have an FDT, but we cannot panic() yet since the console
  484. * is not ready. So for now, just assert(). Boards which need an early
  485. * FDT (prior to console ready) will need to make their own
  486. * arrangements and do their own checks.
  487. */
  488. assert(!fdtdec_prepare_fdt());
  489. return 0;
  490. }
  491. /*
  492. * This function is a little odd in that it accesses global data. At some
  493. * point if the architecture board.c files merge this will make more sense.
  494. * Even now, it is common code.
  495. */
  496. int fdtdec_prepare_fdt(void)
  497. {
  498. if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
  499. fdt_check_header(gd->fdt_blob)) {
  500. printf("No valid FDT found - please append one to U-Boot "
  501. "binary, use u-boot-dtb.bin or define "
  502. "CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
  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. /**
  593. * Decode a list of GPIOs from an FDT. This creates a list of GPIOs with no
  594. * terminating item.
  595. *
  596. * @param blob FDT blob to use
  597. * @param node Node to look at
  598. * @param prop_name Node property name
  599. * @param gpio Array of gpio elements to fill from FDT. This will be
  600. * untouched if either 0 or an error is returned
  601. * @param max_count Maximum number of elements allowed
  602. * @return number of GPIOs read if ok, -FDT_ERR_BADLAYOUT if max_count would
  603. * be exceeded, or -FDT_ERR_NOTFOUND if the property is missing.
  604. */
  605. int fdtdec_decode_gpios(const void *blob, int node, const char *prop_name,
  606. struct fdt_gpio_state *gpio, int max_count)
  607. {
  608. const struct fdt_property *prop;
  609. const u32 *cell;
  610. const char *name;
  611. int len, i;
  612. debug("%s: %s\n", __func__, prop_name);
  613. assert(max_count > 0);
  614. prop = fdt_get_property(blob, node, prop_name, &len);
  615. if (!prop) {
  616. debug("%s: property '%s' missing\n", __func__, prop_name);
  617. return -FDT_ERR_NOTFOUND;
  618. }
  619. /* We will use the name to tag the GPIO */
  620. name = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
  621. cell = (u32 *)prop->data;
  622. len /= sizeof(u32) * 3; /* 3 cells per GPIO record */
  623. if (len > max_count) {
  624. debug(" %s: too many GPIOs / cells for "
  625. "property '%s'\n", __func__, prop_name);
  626. return -FDT_ERR_BADLAYOUT;
  627. }
  628. /* Read out the GPIO data from the cells */
  629. for (i = 0; i < len; i++, cell += 3) {
  630. gpio[i].gpio = fdt32_to_cpu(cell[1]);
  631. gpio[i].flags = fdt32_to_cpu(cell[2]);
  632. gpio[i].name = name;
  633. }
  634. return len;
  635. }
  636. int fdtdec_decode_gpio(const void *blob, int node, const char *prop_name,
  637. struct fdt_gpio_state *gpio)
  638. {
  639. int err;
  640. debug("%s: %s\n", __func__, prop_name);
  641. gpio->gpio = FDT_GPIO_NONE;
  642. gpio->name = NULL;
  643. err = fdtdec_decode_gpios(blob, node, prop_name, gpio, 1);
  644. return err == 1 ? 0 : err;
  645. }
  646. int fdtdec_get_gpio(struct fdt_gpio_state *gpio)
  647. {
  648. int val;
  649. if (!fdt_gpio_isvalid(gpio))
  650. return -1;
  651. val = gpio_get_value(gpio->gpio);
  652. return gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
  653. }
  654. int fdtdec_set_gpio(struct fdt_gpio_state *gpio, int val)
  655. {
  656. if (!fdt_gpio_isvalid(gpio))
  657. return -1;
  658. val = gpio->flags & FDT_GPIO_ACTIVE_LOW ? val ^ 1 : val;
  659. return gpio_set_value(gpio->gpio, val);
  660. }
  661. int fdtdec_setup_gpio(struct fdt_gpio_state *gpio)
  662. {
  663. /*
  664. * Return success if there is no GPIO defined. This is used for
  665. * optional GPIOs)
  666. */
  667. if (!fdt_gpio_isvalid(gpio))
  668. return 0;
  669. if (gpio_request(gpio->gpio, gpio->name))
  670. return -1;
  671. return 0;
  672. }
  673. int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
  674. u8 *array, int count)
  675. {
  676. const u8 *cell;
  677. int err;
  678. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  679. if (!err)
  680. memcpy(array, cell, count);
  681. return err;
  682. }
  683. const u8 *fdtdec_locate_byte_array(const void *blob, int node,
  684. const char *prop_name, int count)
  685. {
  686. const u8 *cell;
  687. int err;
  688. cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
  689. if (err)
  690. return NULL;
  691. return cell;
  692. }
  693. int fdtdec_get_config_int(const void *blob, const char *prop_name,
  694. int default_val)
  695. {
  696. int config_node;
  697. debug("%s: %s\n", __func__, prop_name);
  698. config_node = fdt_path_offset(blob, "/config");
  699. if (config_node < 0)
  700. return default_val;
  701. return fdtdec_get_int(blob, config_node, prop_name, default_val);
  702. }
  703. int fdtdec_get_config_bool(const void *blob, const char *prop_name)
  704. {
  705. int config_node;
  706. const void *prop;
  707. debug("%s: %s\n", __func__, prop_name);
  708. config_node = fdt_path_offset(blob, "/config");
  709. if (config_node < 0)
  710. return 0;
  711. prop = fdt_get_property(blob, config_node, prop_name, NULL);
  712. return prop != NULL;
  713. }
  714. char *fdtdec_get_config_string(const void *blob, const char *prop_name)
  715. {
  716. const char *nodep;
  717. int nodeoffset;
  718. int len;
  719. debug("%s: %s\n", __func__, prop_name);
  720. nodeoffset = fdt_path_offset(blob, "/config");
  721. if (nodeoffset < 0)
  722. return NULL;
  723. nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
  724. if (!nodep)
  725. return NULL;
  726. return (char *)nodep;
  727. }
  728. int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
  729. fdt_addr_t *basep, fdt_size_t *sizep)
  730. {
  731. const fdt_addr_t *cell;
  732. int len;
  733. debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
  734. prop_name);
  735. cell = fdt_getprop(blob, node, prop_name, &len);
  736. if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
  737. debug("cell=%p, len=%d\n", cell, len);
  738. return -1;
  739. }
  740. *basep = fdt_addr_to_cpu(*cell);
  741. *sizep = fdt_size_to_cpu(cell[1]);
  742. debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
  743. (ulong)*sizep);
  744. return 0;
  745. }
  746. /**
  747. * Read a flash entry from the fdt
  748. *
  749. * @param blob FDT blob
  750. * @param node Offset of node to read
  751. * @param name Name of node being read
  752. * @param entry Place to put offset and size of this node
  753. * @return 0 if ok, -ve on error
  754. */
  755. int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
  756. struct fmap_entry *entry)
  757. {
  758. const char *prop;
  759. u32 reg[2];
  760. if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
  761. debug("Node '%s' has bad/missing 'reg' property\n", name);
  762. return -FDT_ERR_NOTFOUND;
  763. }
  764. entry->offset = reg[0];
  765. entry->length = reg[1];
  766. entry->used = fdtdec_get_int(blob, node, "used", entry->length);
  767. prop = fdt_getprop(blob, node, "compress", NULL);
  768. entry->compress_algo = prop && !strcmp(prop, "lzo") ?
  769. FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
  770. prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
  771. entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
  772. entry->hash = (uint8_t *)prop;
  773. return 0;
  774. }
  775. static u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
  776. {
  777. u64 number = 0;
  778. while (cells--)
  779. number = (number << 32) | fdt32_to_cpu(*ptr++);
  780. return number;
  781. }
  782. int fdt_get_resource(const void *fdt, int node, const char *property,
  783. unsigned int index, struct fdt_resource *res)
  784. {
  785. const fdt32_t *ptr, *end;
  786. int na, ns, len, parent;
  787. unsigned int i = 0;
  788. parent = fdt_parent_offset(fdt, node);
  789. if (parent < 0)
  790. return parent;
  791. na = fdt_address_cells(fdt, parent);
  792. ns = fdt_size_cells(fdt, parent);
  793. ptr = fdt_getprop(fdt, node, property, &len);
  794. if (!ptr)
  795. return len;
  796. end = ptr + len / sizeof(*ptr);
  797. while (ptr + na + ns <= end) {
  798. if (i == index) {
  799. res->start = res->end = fdtdec_get_number(ptr, na);
  800. res->end += fdtdec_get_number(&ptr[na], ns) - 1;
  801. return 0;
  802. }
  803. ptr += na + ns;
  804. i++;
  805. }
  806. return -FDT_ERR_NOTFOUND;
  807. }
  808. int fdt_get_named_resource(const void *fdt, int node, const char *property,
  809. const char *prop_names, const char *name,
  810. struct fdt_resource *res)
  811. {
  812. int index;
  813. index = fdt_find_string(fdt, node, prop_names, name);
  814. if (index < 0)
  815. return index;
  816. return fdt_get_resource(fdt, node, property, index, res);
  817. }
  818. int fdtdec_decode_memory_region(const void *blob, int config_node,
  819. const char *mem_type, const char *suffix,
  820. fdt_addr_t *basep, fdt_size_t *sizep)
  821. {
  822. char prop_name[50];
  823. const char *mem;
  824. fdt_size_t size, offset_size;
  825. fdt_addr_t base, offset;
  826. int node;
  827. if (config_node == -1) {
  828. config_node = fdt_path_offset(blob, "/config");
  829. if (config_node < 0) {
  830. debug("%s: Cannot find /config node\n", __func__);
  831. return -ENOENT;
  832. }
  833. }
  834. if (!suffix)
  835. suffix = "";
  836. snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
  837. suffix);
  838. mem = fdt_getprop(blob, config_node, prop_name, NULL);
  839. if (!mem) {
  840. debug("%s: No memory type for '%s', using /memory\n", __func__,
  841. prop_name);
  842. mem = "/memory";
  843. }
  844. node = fdt_path_offset(blob, mem);
  845. if (node < 0) {
  846. debug("%s: Failed to find node '%s': %s\n", __func__, mem,
  847. fdt_strerror(node));
  848. return -ENOENT;
  849. }
  850. /*
  851. * Not strictly correct - the memory may have multiple banks. We just
  852. * use the first
  853. */
  854. if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
  855. debug("%s: Failed to decode memory region %s\n", __func__,
  856. mem);
  857. return -EINVAL;
  858. }
  859. snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
  860. suffix);
  861. if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
  862. &offset_size)) {
  863. debug("%s: Failed to decode memory region '%s'\n", __func__,
  864. prop_name);
  865. return -EINVAL;
  866. }
  867. *basep = base + offset;
  868. *sizep = offset_size;
  869. return 0;
  870. }
  871. #endif