fdtdec.c 27 KB

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