fdtdec.c 26 KB

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