Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 6 | #ifndef USE_HOSTCC |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 7 | #include <common.h> |
Jean-Jacques Hiblot | 5d96cab | 2017-09-15 12:57:31 +0200 | [diff] [blame] | 8 | #include <boot_fit.h> |
Simon Glass | f1037ad | 2015-11-29 13:17:54 -0700 | [diff] [blame] | 9 | #include <dm.h> |
Jean-Jacques Hiblot | 5d96cab | 2017-09-15 12:57:31 +0200 | [diff] [blame] | 10 | #include <dm/of_extra.h> |
Simon Glass | 9908ad4 | 2014-07-23 06:55:09 -0600 | [diff] [blame] | 11 | #include <errno.h> |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 12 | #include <fdtdec.h> |
Jean-Jacques Hiblot | 5d96cab | 2017-09-15 12:57:31 +0200 | [diff] [blame] | 13 | #include <fdt_support.h> |
Heinrich Schuchardt | 370a176 | 2018-11-18 17:58:50 +0100 | [diff] [blame] | 14 | #include <mapmem.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 15 | #include <linux/libfdt.h> |
Jean-Jacques Hiblot | 5d96cab | 2017-09-15 12:57:31 +0200 | [diff] [blame] | 16 | #include <serial.h> |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 17 | #include <asm/sections.h> |
Simon Glass | 9908ad4 | 2014-07-23 06:55:09 -0600 | [diff] [blame] | 18 | #include <linux/ctype.h> |
Jean-Jacques Hiblot | a2fee16 | 2017-09-15 12:57:32 +0200 | [diff] [blame] | 19 | #include <linux/lzo.h> |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
| 23 | /* |
| 24 | * Here are the type we know about. One day we might allow drivers to |
| 25 | * register. For now we just put them here. The COMPAT macro allows us to |
| 26 | * turn this into a sparse list later, and keeps the ID with the name. |
Simon Glass | 7c38030 | 2016-06-19 17:33:13 -0600 | [diff] [blame] | 27 | * |
| 28 | * NOTE: This list is basically a TODO list for things that need to be |
| 29 | * converted to driver model. So don't add new things here unless there is a |
| 30 | * good reason why driver-model conversion is infeasible. Examples include |
| 31 | * things which are used before driver model is available. |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 32 | */ |
| 33 | #define COMPAT(id, name) name |
| 34 | static const char * const compat_names[COMPAT_COUNT] = { |
Simon Glass | 6544498 | 2012-02-27 10:52:34 +0000 | [diff] [blame] | 35 | COMPAT(UNKNOWN, "<none>"), |
Jimmy Zhang | 5983ef8 | 2012-04-02 13:18:52 +0000 | [diff] [blame] | 36 | COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"), |
| 37 | COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"), |
Jim Lin | 5d309e6 | 2012-07-29 20:53:29 +0000 | [diff] [blame] | 38 | COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"), |
Thierry Reding | f202e02 | 2014-12-09 22:25:09 -0700 | [diff] [blame] | 39 | COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"), |
Tom Warren | ab0cc6b | 2015-03-04 16:36:00 -0700 | [diff] [blame] | 40 | COMPAT(NVIDIA_TEGRA210_XUSB_PADCTL, "nvidia,tegra210-xusb-padctl"), |
Hatim RV | be5ad5a | 2012-12-11 00:52:46 +0000 | [diff] [blame] | 41 | COMPAT(SMSC_LAN9215, "smsc,lan9215"), |
| 42 | COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"), |
Rajeshwari Shinde | 2b71e9d | 2012-12-26 20:03:10 +0000 | [diff] [blame] | 43 | COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"), |
Rajeshwari Shinde | 9d11673 | 2012-12-26 20:03:16 +0000 | [diff] [blame] | 44 | COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"), |
| 45 | COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"), |
Rajeshwari Shinde | d6e9f44 | 2013-01-07 23:35:05 +0000 | [diff] [blame] | 46 | COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"), |
Vivek Gautam | f874523 | 2013-09-14 14:02:48 +0530 | [diff] [blame] | 47 | COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"), |
Akshay Saraswat | aa77318 | 2013-02-25 01:13:01 +0000 | [diff] [blame] | 48 | COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"), |
Piotr Wilczek | 24e368a | 2014-03-07 14:59:39 +0100 | [diff] [blame] | 49 | COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"), |
Jaehoon Chung | e3fb6fa | 2014-05-16 13:59:51 +0900 | [diff] [blame] | 50 | COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"), |
Piotr Wilczek | 12cf19e | 2014-03-07 14:59:41 +0100 | [diff] [blame] | 51 | COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"), |
Simon Glass | 60956094 | 2013-03-11 06:08:08 +0000 | [diff] [blame] | 52 | COMPAT(GENERIC_SPI_FLASH, "spi-flash"), |
Rajeshwari Shinde | 003c28c | 2013-02-14 19:46:15 +0000 | [diff] [blame] | 53 | COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"), |
naveen krishna chatradhi | 0734bae | 2013-04-29 15:58:52 -0700 | [diff] [blame] | 54 | COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"), |
Ajay Kumar | e24c502 | 2014-09-05 16:53:33 +0530 | [diff] [blame] | 55 | COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"), |
Simon Glass | f79d538 | 2014-11-12 22:42:21 -0700 | [diff] [blame] | 56 | COMPAT(INTEL_MICROCODE, "intel,microcode"), |
Thierry Reding | 09c0cf2 | 2014-12-09 22:25:05 -0700 | [diff] [blame] | 57 | COMPAT(AMS_AS3722, "ams,as3722"), |
Bin Meng | 2bfd264 | 2015-02-05 23:42:26 +0800 | [diff] [blame] | 58 | COMPAT(INTEL_QRK_MRC, "intel,quark-mrc"), |
Marek Vasut | f3f8fe2 | 2015-07-25 19:33:56 +0200 | [diff] [blame] | 59 | COMPAT(ALTERA_SOCFPGA_DWMAC, "altr,socfpga-stmmac"), |
Marek Vasut | 1749723 | 2015-07-25 10:48:14 +0200 | [diff] [blame] | 60 | COMPAT(ALTERA_SOCFPGA_DWMMC, "altr,socfpga-dw-mshc"), |
Marek Vasut | f26c1ae | 2015-12-05 19:28:44 +0100 | [diff] [blame] | 61 | COMPAT(ALTERA_SOCFPGA_DWC2USB, "snps,dwc2"), |
Simon Glass | 6f70c11 | 2016-06-19 17:33:14 -0600 | [diff] [blame] | 62 | COMPAT(INTEL_BAYTRAIL_FSP, "intel,baytrail-fsp"), |
| 63 | COMPAT(INTEL_BAYTRAIL_FSP_MDP, "intel,baytrail-fsp-mdp"), |
| 64 | COMPAT(INTEL_IVYBRIDGE_FSP, "intel,ivybridge-fsp"), |
Boris Brezillon | 57f2038 | 2016-06-15 21:09:23 +0200 | [diff] [blame] | 65 | COMPAT(COMPAT_SUNXI_NAND, "allwinner,sun4i-a10-nand"), |
Ley Foon Tan | b25ae72 | 2017-04-05 17:32:47 +0800 | [diff] [blame] | 66 | COMPAT(ALTERA_SOCFPGA_CLK, "altr,clk-mgr"), |
| 67 | COMPAT(ALTERA_SOCFPGA_PINCTRL_SINGLE, "pinctrl-single"), |
| 68 | COMPAT(ALTERA_SOCFPGA_H2F_BRG, "altr,socfpga-hps2fpga-bridge"), |
| 69 | COMPAT(ALTERA_SOCFPGA_LWH2F_BRG, "altr,socfpga-lwhps2fpga-bridge"), |
| 70 | COMPAT(ALTERA_SOCFPGA_F2H_BRG, "altr,socfpga-fpga2hps-bridge"), |
| 71 | COMPAT(ALTERA_SOCFPGA_F2SDR0, "altr,socfpga-fpga2sdram0-bridge"), |
| 72 | COMPAT(ALTERA_SOCFPGA_F2SDR1, "altr,socfpga-fpga2sdram1-bridge"), |
| 73 | COMPAT(ALTERA_SOCFPGA_F2SDR2, "altr,socfpga-fpga2sdram2-bridge"), |
Tien Fong Chee | bbfa5eb | 2017-09-25 16:40:03 +0800 | [diff] [blame] | 74 | COMPAT(ALTERA_SOCFPGA_FPGA0, "altr,socfpga-a10-fpga-mgr"), |
| 75 | COMPAT(ALTERA_SOCFPGA_NOC, "altr,socfpga-a10-noc"), |
Marek Vasut | ee4e36b | 2018-05-12 11:56:10 +0200 | [diff] [blame] | 76 | COMPAT(ALTERA_SOCFPGA_CLK_INIT, "altr,socfpga-a10-clk-init") |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 79 | const char *fdtdec_get_compatible(enum fdt_compat_id id) |
| 80 | { |
| 81 | /* We allow reading of the 'unknown' ID for testing purposes */ |
| 82 | assert(id >= 0 && id < COMPAT_COUNT); |
| 83 | return compat_names[id]; |
| 84 | } |
| 85 | |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 86 | fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node, |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 87 | const char *prop_name, int index, int na, |
| 88 | int ns, fdt_size_t *sizep, |
| 89 | bool translate) |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 90 | { |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 91 | const fdt32_t *prop, *prop_end; |
| 92 | const fdt32_t *prop_addr, *prop_size, *prop_after_size; |
Simon Glass | 04c50d1 | 2015-08-02 18:13:50 -0600 | [diff] [blame] | 93 | int len; |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 94 | fdt_addr_t addr; |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 95 | |
Simon Glass | c8c86dd | 2012-07-12 05:25:01 +0000 | [diff] [blame] | 96 | debug("%s: %s: ", __func__, prop_name); |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 97 | |
| 98 | if (na > (sizeof(fdt_addr_t) / sizeof(fdt32_t))) { |
| 99 | debug("(na too large for fdt_addr_t type)\n"); |
| 100 | return FDT_ADDR_T_NONE; |
| 101 | } |
| 102 | |
| 103 | if (ns > (sizeof(fdt_size_t) / sizeof(fdt32_t))) { |
| 104 | debug("(ns too large for fdt_size_t type)\n"); |
| 105 | return FDT_ADDR_T_NONE; |
| 106 | } |
| 107 | |
| 108 | prop = fdt_getprop(blob, node, prop_name, &len); |
| 109 | if (!prop) { |
| 110 | debug("(not found)\n"); |
| 111 | return FDT_ADDR_T_NONE; |
| 112 | } |
| 113 | prop_end = prop + (len / sizeof(*prop)); |
Simon Glass | c8c86dd | 2012-07-12 05:25:01 +0000 | [diff] [blame] | 114 | |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 115 | prop_addr = prop + (index * (na + ns)); |
| 116 | prop_size = prop_addr + na; |
| 117 | prop_after_size = prop_size + ns; |
| 118 | if (prop_after_size > prop_end) { |
| 119 | debug("(not enough data: expected >= %d cells, got %d cells)\n", |
| 120 | (u32)(prop_after_size - prop), ((u32)(prop_end - prop))); |
| 121 | return FDT_ADDR_T_NONE; |
Simon Glass | c8c86dd | 2012-07-12 05:25:01 +0000 | [diff] [blame] | 122 | } |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 123 | |
Vignesh R | 621cc09 | 2017-03-20 10:04:38 +0530 | [diff] [blame] | 124 | #if CONFIG_IS_ENABLED(OF_TRANSLATE) |
Stephen Warren | 7d30e10 | 2016-08-05 09:47:51 -0600 | [diff] [blame] | 125 | if (translate) |
| 126 | addr = fdt_translate_address(blob, node, prop_addr); |
| 127 | else |
| 128 | #endif |
| 129 | addr = fdtdec_get_number(prop_addr, na); |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 130 | |
| 131 | if (sizep) { |
| 132 | *sizep = fdtdec_get_number(prop_size, ns); |
Simon Glass | 0b14dc7 | 2016-02-29 15:25:37 -0700 | [diff] [blame] | 133 | debug("addr=%08llx, size=%llx\n", (unsigned long long)addr, |
| 134 | (unsigned long long)*sizep); |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 135 | } else { |
Simon Glass | 0b14dc7 | 2016-02-29 15:25:37 -0700 | [diff] [blame] | 136 | debug("addr=%08llx\n", (unsigned long long)addr); |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | return addr; |
| 140 | } |
| 141 | |
| 142 | fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent, |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 143 | int node, const char *prop_name, |
| 144 | int index, fdt_size_t *sizep, |
| 145 | bool translate) |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 146 | { |
| 147 | int na, ns; |
| 148 | |
| 149 | debug("%s: ", __func__); |
| 150 | |
| 151 | na = fdt_address_cells(blob, parent); |
| 152 | if (na < 1) { |
| 153 | debug("(bad #address-cells)\n"); |
| 154 | return FDT_ADDR_T_NONE; |
| 155 | } |
| 156 | |
| 157 | ns = fdt_size_cells(blob, parent); |
Przemyslaw Marczak | 7b42f4d | 2015-09-30 13:14:50 +0200 | [diff] [blame] | 158 | if (ns < 0) { |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 159 | debug("(bad #size-cells)\n"); |
| 160 | return FDT_ADDR_T_NONE; |
| 161 | } |
| 162 | |
| 163 | debug("na=%d, ns=%d, ", na, ns); |
| 164 | |
| 165 | return fdtdec_get_addr_size_fixed(blob, node, prop_name, index, na, |
Stephen Warren | 7d30e10 | 2016-08-05 09:47:51 -0600 | [diff] [blame] | 166 | ns, sizep, translate); |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node, |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 170 | const char *prop_name, int index, |
| 171 | fdt_size_t *sizep, |
| 172 | bool translate) |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 173 | { |
| 174 | int parent; |
| 175 | |
| 176 | debug("%s: ", __func__); |
| 177 | |
| 178 | parent = fdt_parent_offset(blob, node); |
| 179 | if (parent < 0) { |
| 180 | debug("(no parent found)\n"); |
| 181 | return FDT_ADDR_T_NONE; |
| 182 | } |
| 183 | |
| 184 | return fdtdec_get_addr_size_auto_parent(blob, parent, node, prop_name, |
Stephen Warren | 7d30e10 | 2016-08-05 09:47:51 -0600 | [diff] [blame] | 185 | index, sizep, translate); |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | fdt_addr_t fdtdec_get_addr_size(const void *blob, int node, |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 189 | const char *prop_name, fdt_size_t *sizep) |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 190 | { |
Stephen Warren | fda5916 | 2015-09-25 10:11:41 -0600 | [diff] [blame] | 191 | int ns = sizep ? (sizeof(fdt_size_t) / sizeof(fdt32_t)) : 0; |
| 192 | |
Stephen Warren | 6dfe618 | 2015-08-06 15:31:02 -0600 | [diff] [blame] | 193 | return fdtdec_get_addr_size_fixed(blob, node, prop_name, 0, |
| 194 | sizeof(fdt_addr_t) / sizeof(fdt32_t), |
Stephen Warren | 7d30e10 | 2016-08-05 09:47:51 -0600 | [diff] [blame] | 195 | ns, sizep, false); |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 198 | fdt_addr_t fdtdec_get_addr(const void *blob, int node, const char *prop_name) |
Simon Glass | f429955 | 2013-03-19 04:58:51 +0000 | [diff] [blame] | 199 | { |
| 200 | return fdtdec_get_addr_size(blob, node, prop_name, NULL); |
| 201 | } |
| 202 | |
Sekhar Nori | 9108d92 | 2018-12-06 15:40:08 +0530 | [diff] [blame] | 203 | #if CONFIG_IS_ENABLED(PCI) && defined(CONFIG_DM_PCI) |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 204 | int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type, |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 205 | const char *prop_name, struct fdt_pci_addr *addr) |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 206 | { |
| 207 | const u32 *cell; |
| 208 | int len; |
| 209 | int ret = -ENOENT; |
| 210 | |
| 211 | debug("%s: %s: ", __func__, prop_name); |
| 212 | |
| 213 | /* |
| 214 | * If we follow the pci bus bindings strictly, we should check |
| 215 | * the value of the node's parent node's #address-cells and |
| 216 | * #size-cells. They need to be 3 and 2 accordingly. However, |
| 217 | * for simplicity we skip the check here. |
| 218 | */ |
| 219 | cell = fdt_getprop(blob, node, prop_name, &len); |
| 220 | if (!cell) |
| 221 | goto fail; |
| 222 | |
| 223 | if ((len % FDT_PCI_REG_SIZE) == 0) { |
| 224 | int num = len / FDT_PCI_REG_SIZE; |
| 225 | int i; |
| 226 | |
| 227 | for (i = 0; i < num; i++) { |
| 228 | debug("pci address #%d: %08lx %08lx %08lx\n", i, |
Stephen Warren | fed69ad | 2015-10-02 17:44:06 -0600 | [diff] [blame] | 229 | (ulong)fdt32_to_cpu(cell[0]), |
| 230 | (ulong)fdt32_to_cpu(cell[1]), |
| 231 | (ulong)fdt32_to_cpu(cell[2])); |
| 232 | if ((fdt32_to_cpu(*cell) & type) == type) { |
| 233 | addr->phys_hi = fdt32_to_cpu(cell[0]); |
| 234 | addr->phys_mid = fdt32_to_cpu(cell[1]); |
| 235 | addr->phys_lo = fdt32_to_cpu(cell[1]); |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 236 | break; |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 237 | } |
Mario Six | 6abfbc8 | 2018-01-15 11:07:36 +0100 | [diff] [blame] | 238 | |
| 239 | cell += (FDT_PCI_ADDR_CELLS + |
| 240 | FDT_PCI_SIZE_CELLS); |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 241 | } |
| 242 | |
Simon Glass | 2eb77f1 | 2015-03-05 12:25:19 -0700 | [diff] [blame] | 243 | if (i == num) { |
| 244 | ret = -ENXIO; |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 245 | goto fail; |
Simon Glass | 2eb77f1 | 2015-03-05 12:25:19 -0700 | [diff] [blame] | 246 | } |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 247 | |
| 248 | return 0; |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 249 | } |
| 250 | |
Mario Six | 6abfbc8 | 2018-01-15 11:07:36 +0100 | [diff] [blame] | 251 | ret = -EINVAL; |
| 252 | |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 253 | fail: |
| 254 | debug("(not found)\n"); |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device) |
| 259 | { |
| 260 | const char *list, *end; |
| 261 | int len; |
| 262 | |
| 263 | list = fdt_getprop(blob, node, "compatible", &len); |
| 264 | if (!list) |
| 265 | return -ENOENT; |
| 266 | |
| 267 | end = list + len; |
| 268 | while (list < end) { |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 269 | len = strlen(list); |
| 270 | if (len >= strlen("pciVVVV,DDDD")) { |
Mario Six | 6abfbc8 | 2018-01-15 11:07:36 +0100 | [diff] [blame] | 271 | char *s = strstr(list, "pci"); |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 272 | |
| 273 | /* |
| 274 | * check if the string is something like pciVVVV,DDDD.RR |
| 275 | * or just pciVVVV,DDDD |
| 276 | */ |
| 277 | if (s && s[7] == ',' && |
| 278 | (s[12] == '.' || s[12] == 0)) { |
| 279 | s += 3; |
| 280 | *vendor = simple_strtol(s, NULL, 16); |
| 281 | |
| 282 | s += 5; |
| 283 | *device = simple_strtol(s, NULL, 16); |
| 284 | |
| 285 | return 0; |
| 286 | } |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 287 | } |
Bin Meng | a2822b8 | 2015-08-20 06:40:25 -0700 | [diff] [blame] | 288 | list += (len + 1); |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | return -ENOENT; |
| 292 | } |
| 293 | |
Simon Glass | f1037ad | 2015-11-29 13:17:54 -0700 | [diff] [blame] | 294 | int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr, |
| 295 | u32 *bar) |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 296 | { |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 297 | int barnum; |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 298 | |
| 299 | /* extract the bar number from fdt_pci_addr */ |
| 300 | barnum = addr->phys_hi & 0xff; |
Mario Six | 6abfbc8 | 2018-01-15 11:07:36 +0100 | [diff] [blame] | 301 | if (barnum < PCI_BASE_ADDRESS_0 || barnum > PCI_CARDBUS_CIS) |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 302 | return -EINVAL; |
| 303 | |
| 304 | barnum = (barnum - PCI_BASE_ADDRESS_0) / 4; |
Simon Glass | f1037ad | 2015-11-29 13:17:54 -0700 | [diff] [blame] | 305 | *bar = dm_pci_read_bar32(dev, barnum); |
Bin Meng | 779847e | 2014-12-31 16:05:11 +0800 | [diff] [blame] | 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | #endif |
| 310 | |
Che-Liang Chiou | d81d017 | 2012-10-25 16:31:05 +0000 | [diff] [blame] | 311 | uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name, |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 312 | uint64_t default_val) |
Che-Liang Chiou | d81d017 | 2012-10-25 16:31:05 +0000 | [diff] [blame] | 313 | { |
| 314 | const uint64_t *cell64; |
| 315 | int length; |
| 316 | |
| 317 | cell64 = fdt_getprop(blob, node, prop_name, &length); |
| 318 | if (!cell64 || length < sizeof(*cell64)) |
| 319 | return default_val; |
| 320 | |
| 321 | return fdt64_to_cpu(*cell64); |
| 322 | } |
| 323 | |
Simon Glass | 6544498 | 2012-02-27 10:52:34 +0000 | [diff] [blame] | 324 | int fdtdec_get_is_enabled(const void *blob, int node) |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 325 | { |
| 326 | const char *cell; |
| 327 | |
Simon Glass | 6544498 | 2012-02-27 10:52:34 +0000 | [diff] [blame] | 328 | /* |
| 329 | * It should say "okay", so only allow that. Some fdts use "ok" but |
| 330 | * this is a bug. Please fix your device tree source file. See here |
| 331 | * for discussion: |
| 332 | * |
| 333 | * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html |
| 334 | */ |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 335 | cell = fdt_getprop(blob, node, "status", NULL); |
| 336 | if (cell) |
Mario Six | 6abfbc8 | 2018-01-15 11:07:36 +0100 | [diff] [blame] | 337 | return strcmp(cell, "okay") == 0; |
Simon Glass | 6544498 | 2012-02-27 10:52:34 +0000 | [diff] [blame] | 338 | return 1; |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Gerald Van Baren | cdac339 | 2012-11-12 23:13:54 -0500 | [diff] [blame] | 341 | enum fdt_compat_id fdtdec_lookup(const void *blob, int node) |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 342 | { |
| 343 | enum fdt_compat_id id; |
| 344 | |
| 345 | /* Search our drivers */ |
| 346 | for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++) |
Mario Six | 6abfbc8 | 2018-01-15 11:07:36 +0100 | [diff] [blame] | 347 | if (fdt_node_check_compatible(blob, node, |
| 348 | compat_names[id]) == 0) |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 349 | return id; |
| 350 | return COMPAT_UNKNOWN; |
| 351 | } |
| 352 | |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 353 | int fdtdec_next_compatible(const void *blob, int node, enum fdt_compat_id id) |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 354 | { |
| 355 | return fdt_node_offset_by_compatible(blob, node, compat_names[id]); |
| 356 | } |
| 357 | |
Simon Glass | 2e979b1 | 2012-04-02 13:18:42 +0000 | [diff] [blame] | 358 | int fdtdec_next_compatible_subnode(const void *blob, int node, |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 359 | enum fdt_compat_id id, int *depthp) |
Simon Glass | 2e979b1 | 2012-04-02 13:18:42 +0000 | [diff] [blame] | 360 | { |
| 361 | do { |
| 362 | node = fdt_next_node(blob, node, depthp); |
| 363 | } while (*depthp > 1); |
| 364 | |
| 365 | /* If this is a direct subnode, and compatible, return it */ |
| 366 | if (*depthp == 1 && 0 == fdt_node_check_compatible( |
| 367 | blob, node, compat_names[id])) |
| 368 | return node; |
| 369 | |
| 370 | return -FDT_ERR_NOTFOUND; |
| 371 | } |
| 372 | |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 373 | int fdtdec_next_alias(const void *blob, const char *name, enum fdt_compat_id id, |
| 374 | int *upto) |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 375 | { |
| 376 | #define MAX_STR_LEN 20 |
| 377 | char str[MAX_STR_LEN + 20]; |
| 378 | int node, err; |
| 379 | |
| 380 | /* snprintf() is not available */ |
| 381 | assert(strlen(name) < MAX_STR_LEN); |
| 382 | sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto); |
Simon Glass | e1d15ca | 2012-10-31 14:02:42 +0000 | [diff] [blame] | 383 | node = fdt_path_offset(blob, str); |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 384 | if (node < 0) |
| 385 | return node; |
| 386 | err = fdt_node_check_compatible(blob, node, compat_names[id]); |
| 387 | if (err < 0) |
| 388 | return err; |
Simon Glass | 6544498 | 2012-02-27 10:52:34 +0000 | [diff] [blame] | 389 | if (err) |
| 390 | return -FDT_ERR_NOTFOUND; |
| 391 | (*upto)++; |
| 392 | return node; |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 395 | int fdtdec_find_aliases_for_id(const void *blob, const char *name, |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 396 | enum fdt_compat_id id, int *node_list, |
| 397 | int maxcount) |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 398 | { |
Simon Glass | 1f77266 | 2012-02-03 15:13:53 +0000 | [diff] [blame] | 399 | memset(node_list, '\0', sizeof(*node_list) * maxcount); |
| 400 | |
| 401 | return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount); |
| 402 | } |
| 403 | |
| 404 | /* TODO: Can we tighten this code up a little? */ |
| 405 | int fdtdec_add_aliases_for_id(const void *blob, const char *name, |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 406 | enum fdt_compat_id id, int *node_list, |
| 407 | int maxcount) |
Simon Glass | 1f77266 | 2012-02-03 15:13:53 +0000 | [diff] [blame] | 408 | { |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 409 | int name_len = strlen(name); |
| 410 | int nodes[maxcount]; |
| 411 | int num_found = 0; |
| 412 | int offset, node; |
| 413 | int alias_node; |
| 414 | int count; |
| 415 | int i, j; |
| 416 | |
| 417 | /* find the alias node if present */ |
| 418 | alias_node = fdt_path_offset(blob, "/aliases"); |
| 419 | |
| 420 | /* |
| 421 | * start with nothing, and we can assume that the root node can't |
| 422 | * match |
| 423 | */ |
| 424 | memset(nodes, '\0', sizeof(nodes)); |
| 425 | |
| 426 | /* First find all the compatible nodes */ |
| 427 | for (node = count = 0; node >= 0 && count < maxcount;) { |
| 428 | node = fdtdec_next_compatible(blob, node, id); |
| 429 | if (node >= 0) |
| 430 | nodes[count++] = node; |
| 431 | } |
| 432 | if (node >= 0) |
| 433 | debug("%s: warning: maxcount exceeded with alias '%s'\n", |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 434 | __func__, name); |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 435 | |
| 436 | /* Now find all the aliases */ |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 437 | for (offset = fdt_first_property_offset(blob, alias_node); |
| 438 | offset > 0; |
| 439 | offset = fdt_next_property_offset(blob, offset)) { |
| 440 | const struct fdt_property *prop; |
| 441 | const char *path; |
| 442 | int number; |
| 443 | int found; |
| 444 | |
| 445 | node = 0; |
| 446 | prop = fdt_get_property_by_offset(blob, offset, NULL); |
| 447 | path = fdt_string(blob, fdt32_to_cpu(prop->nameoff)); |
| 448 | if (prop->len && 0 == strncmp(path, name, name_len)) |
| 449 | node = fdt_path_offset(blob, prop->data); |
| 450 | if (node <= 0) |
| 451 | continue; |
| 452 | |
| 453 | /* Get the alias number */ |
| 454 | number = simple_strtoul(path + name_len, NULL, 10); |
| 455 | if (number < 0 || number >= maxcount) { |
| 456 | debug("%s: warning: alias '%s' is out of range\n", |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 457 | __func__, path); |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 458 | continue; |
| 459 | } |
| 460 | |
| 461 | /* Make sure the node we found is actually in our list! */ |
| 462 | found = -1; |
| 463 | for (j = 0; j < count; j++) |
| 464 | if (nodes[j] == node) { |
| 465 | found = j; |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | if (found == -1) { |
| 470 | debug("%s: warning: alias '%s' points to a node " |
| 471 | "'%s' that is missing or is not compatible " |
| 472 | " with '%s'\n", __func__, path, |
| 473 | fdt_get_name(blob, node, NULL), |
| 474 | compat_names[id]); |
| 475 | continue; |
| 476 | } |
| 477 | |
| 478 | /* |
| 479 | * Add this node to our list in the right place, and mark |
| 480 | * it as done. |
| 481 | */ |
| 482 | if (fdtdec_get_is_enabled(blob, node)) { |
Simon Glass | 1f77266 | 2012-02-03 15:13:53 +0000 | [diff] [blame] | 483 | if (node_list[number]) { |
| 484 | debug("%s: warning: alias '%s' requires that " |
| 485 | "a node be placed in the list in a " |
| 486 | "position which is already filled by " |
| 487 | "node '%s'\n", __func__, path, |
| 488 | fdt_get_name(blob, node, NULL)); |
| 489 | continue; |
| 490 | } |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 491 | node_list[number] = node; |
| 492 | if (number >= num_found) |
| 493 | num_found = number + 1; |
| 494 | } |
Simon Glass | 1f77266 | 2012-02-03 15:13:53 +0000 | [diff] [blame] | 495 | nodes[found] = 0; |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | /* Add any nodes not mentioned by an alias */ |
| 499 | for (i = j = 0; i < maxcount; i++) { |
| 500 | if (!node_list[i]) { |
| 501 | for (; j < maxcount; j++) |
| 502 | if (nodes[j] && |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 503 | fdtdec_get_is_enabled(blob, nodes[j])) |
Simon Glass | b022ead | 2012-01-17 08:20:50 +0000 | [diff] [blame] | 504 | break; |
| 505 | |
| 506 | /* Have we run out of nodes to add? */ |
| 507 | if (j == maxcount) |
| 508 | break; |
| 509 | |
| 510 | assert(!node_list[i]); |
| 511 | node_list[i] = nodes[j++]; |
| 512 | if (i >= num_found) |
| 513 | num_found = i + 1; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | return num_found; |
| 518 | } |
| 519 | |
Simon Glass | 9908ad4 | 2014-07-23 06:55:09 -0600 | [diff] [blame] | 520 | int fdtdec_get_alias_seq(const void *blob, const char *base, int offset, |
| 521 | int *seqp) |
| 522 | { |
| 523 | int base_len = strlen(base); |
| 524 | const char *find_name; |
| 525 | int find_namelen; |
| 526 | int prop_offset; |
| 527 | int aliases; |
| 528 | |
| 529 | find_name = fdt_get_name(blob, offset, &find_namelen); |
| 530 | debug("Looking for '%s' at %d, name %s\n", base, offset, find_name); |
| 531 | |
| 532 | aliases = fdt_path_offset(blob, "/aliases"); |
| 533 | for (prop_offset = fdt_first_property_offset(blob, aliases); |
| 534 | prop_offset > 0; |
| 535 | prop_offset = fdt_next_property_offset(blob, prop_offset)) { |
| 536 | const char *prop; |
| 537 | const char *name; |
| 538 | const char *slash; |
Simon Glass | 4aaa460 | 2015-06-23 15:39:08 -0600 | [diff] [blame] | 539 | int len, val; |
Simon Glass | 9908ad4 | 2014-07-23 06:55:09 -0600 | [diff] [blame] | 540 | |
| 541 | prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len); |
| 542 | debug(" - %s, %s\n", name, prop); |
| 543 | if (len < find_namelen || *prop != '/' || prop[len - 1] || |
| 544 | strncmp(name, base, base_len)) |
| 545 | continue; |
| 546 | |
| 547 | slash = strrchr(prop, '/'); |
| 548 | if (strcmp(slash + 1, find_name)) |
| 549 | continue; |
Simon Glass | 4aaa460 | 2015-06-23 15:39:08 -0600 | [diff] [blame] | 550 | val = trailing_strtol(name); |
| 551 | if (val != -1) { |
| 552 | *seqp = val; |
| 553 | debug("Found seq %d\n", *seqp); |
| 554 | return 0; |
Simon Glass | 9908ad4 | 2014-07-23 06:55:09 -0600 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
| 558 | debug("Not found\n"); |
| 559 | return -ENOENT; |
| 560 | } |
| 561 | |
Simon Glass | 088dccb | 2015-10-17 19:41:14 -0600 | [diff] [blame] | 562 | const char *fdtdec_get_chosen_prop(const void *blob, const char *name) |
Simon Glass | 1e374fb | 2014-09-04 16:27:24 -0600 | [diff] [blame] | 563 | { |
Simon Glass | 1e374fb | 2014-09-04 16:27:24 -0600 | [diff] [blame] | 564 | int chosen_node; |
Simon Glass | 1e374fb | 2014-09-04 16:27:24 -0600 | [diff] [blame] | 565 | |
| 566 | if (!blob) |
Simon Glass | 088dccb | 2015-10-17 19:41:14 -0600 | [diff] [blame] | 567 | return NULL; |
Simon Glass | 1e374fb | 2014-09-04 16:27:24 -0600 | [diff] [blame] | 568 | chosen_node = fdt_path_offset(blob, "/chosen"); |
Simon Glass | 088dccb | 2015-10-17 19:41:14 -0600 | [diff] [blame] | 569 | return fdt_getprop(blob, chosen_node, name, NULL); |
| 570 | } |
| 571 | |
| 572 | int fdtdec_get_chosen_node(const void *blob, const char *name) |
| 573 | { |
| 574 | const char *prop; |
| 575 | |
| 576 | prop = fdtdec_get_chosen_prop(blob, name); |
Simon Glass | 1e374fb | 2014-09-04 16:27:24 -0600 | [diff] [blame] | 577 | if (!prop) |
| 578 | return -FDT_ERR_NOTFOUND; |
| 579 | return fdt_path_offset(blob, prop); |
| 580 | } |
| 581 | |
Simon Glass | 1691f69 | 2012-03-28 10:08:24 +0000 | [diff] [blame] | 582 | int fdtdec_check_fdt(void) |
| 583 | { |
| 584 | /* |
| 585 | * We must have an FDT, but we cannot panic() yet since the console |
| 586 | * is not ready. So for now, just assert(). Boards which need an early |
| 587 | * FDT (prior to console ready) will need to make their own |
| 588 | * arrangements and do their own checks. |
| 589 | */ |
| 590 | assert(!fdtdec_prepare_fdt()); |
| 591 | return 0; |
| 592 | } |
| 593 | |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 594 | /* |
| 595 | * This function is a little odd in that it accesses global data. At some |
| 596 | * point if the architecture board.c files merge this will make more sense. |
| 597 | * Even now, it is common code. |
| 598 | */ |
Simon Glass | 1691f69 | 2012-03-28 10:08:24 +0000 | [diff] [blame] | 599 | int fdtdec_prepare_fdt(void) |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 600 | { |
Simon Glass | 7113685 | 2013-04-20 08:42:46 +0000 | [diff] [blame] | 601 | if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) || |
| 602 | fdt_check_header(gd->fdt_blob)) { |
Simon Glass | 509dbf9 | 2015-02-27 22:06:32 -0700 | [diff] [blame] | 603 | #ifdef CONFIG_SPL_BUILD |
| 604 | puts("Missing DTB\n"); |
| 605 | #else |
| 606 | puts("No valid device tree binary found - please append one to U-Boot binary, use u-boot-dtb.bin or define CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n"); |
Simon Glass | 63884ec | 2015-06-23 15:39:09 -0600 | [diff] [blame] | 607 | # ifdef DEBUG |
| 608 | if (gd->fdt_blob) { |
| 609 | printf("fdt_blob=%p\n", gd->fdt_blob); |
| 610 | print_buffer((ulong)gd->fdt_blob, gd->fdt_blob, 4, |
| 611 | 32, 0); |
| 612 | } |
| 613 | # endif |
Simon Glass | 509dbf9 | 2015-02-27 22:06:32 -0700 | [diff] [blame] | 614 | #endif |
Simon Glass | 1691f69 | 2012-03-28 10:08:24 +0000 | [diff] [blame] | 615 | return -1; |
| 616 | } |
Simon Glass | 2d2af85 | 2011-10-24 19:15:32 +0000 | [diff] [blame] | 617 | return 0; |
| 618 | } |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 619 | |
| 620 | int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name) |
| 621 | { |
| 622 | const u32 *phandle; |
| 623 | int lookup; |
| 624 | |
Simon Glass | c8c86dd | 2012-07-12 05:25:01 +0000 | [diff] [blame] | 625 | debug("%s: %s\n", __func__, prop_name); |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 626 | phandle = fdt_getprop(blob, node, prop_name, NULL); |
| 627 | if (!phandle) |
| 628 | return -FDT_ERR_NOTFOUND; |
| 629 | |
| 630 | lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle)); |
| 631 | return lookup; |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * Look up a property in a node and check that it has a minimum length. |
| 636 | * |
| 637 | * @param blob FDT blob |
| 638 | * @param node node to examine |
| 639 | * @param prop_name name of property to find |
| 640 | * @param min_len minimum property length in bytes |
| 641 | * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not |
| 642 | found, or -FDT_ERR_BADLAYOUT if not enough data |
| 643 | * @return pointer to cell, which is only valid if err == 0 |
| 644 | */ |
| 645 | static const void *get_prop_check_min_len(const void *blob, int node, |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 646 | const char *prop_name, int min_len, |
| 647 | int *err) |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 648 | { |
| 649 | const void *cell; |
| 650 | int len; |
| 651 | |
| 652 | debug("%s: %s\n", __func__, prop_name); |
| 653 | cell = fdt_getprop(blob, node, prop_name, &len); |
| 654 | if (!cell) |
| 655 | *err = -FDT_ERR_NOTFOUND; |
| 656 | else if (len < min_len) |
| 657 | *err = -FDT_ERR_BADLAYOUT; |
| 658 | else |
| 659 | *err = 0; |
| 660 | return cell; |
| 661 | } |
| 662 | |
| 663 | int fdtdec_get_int_array(const void *blob, int node, const char *prop_name, |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 664 | u32 *array, int count) |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 665 | { |
| 666 | const u32 *cell; |
Mario Six | 6abfbc8 | 2018-01-15 11:07:36 +0100 | [diff] [blame] | 667 | int err = 0; |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 668 | |
| 669 | debug("%s: %s\n", __func__, prop_name); |
| 670 | cell = get_prop_check_min_len(blob, node, prop_name, |
| 671 | sizeof(u32) * count, &err); |
| 672 | if (!err) { |
Mario Six | 6abfbc8 | 2018-01-15 11:07:36 +0100 | [diff] [blame] | 673 | int i; |
| 674 | |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 675 | for (i = 0; i < count; i++) |
| 676 | array[i] = fdt32_to_cpu(cell[i]); |
| 677 | } |
| 678 | return err; |
| 679 | } |
| 680 | |
Simon Glass | eda0c3f | 2014-11-10 18:00:19 -0700 | [diff] [blame] | 681 | int fdtdec_get_int_array_count(const void *blob, int node, |
| 682 | const char *prop_name, u32 *array, int count) |
| 683 | { |
| 684 | const u32 *cell; |
| 685 | int len, elems; |
| 686 | int i; |
| 687 | |
| 688 | debug("%s: %s\n", __func__, prop_name); |
| 689 | cell = fdt_getprop(blob, node, prop_name, &len); |
| 690 | if (!cell) |
| 691 | return -FDT_ERR_NOTFOUND; |
| 692 | elems = len / sizeof(u32); |
| 693 | if (count > elems) |
| 694 | count = elems; |
| 695 | for (i = 0; i < count; i++) |
| 696 | array[i] = fdt32_to_cpu(cell[i]); |
| 697 | |
| 698 | return count; |
| 699 | } |
| 700 | |
Simon Glass | 744ec23 | 2012-04-02 13:18:41 +0000 | [diff] [blame] | 701 | const u32 *fdtdec_locate_array(const void *blob, int node, |
| 702 | const char *prop_name, int count) |
| 703 | { |
| 704 | const u32 *cell; |
| 705 | int err; |
| 706 | |
| 707 | cell = get_prop_check_min_len(blob, node, prop_name, |
| 708 | sizeof(u32) * count, &err); |
| 709 | return err ? NULL : cell; |
| 710 | } |
| 711 | |
Simon Glass | c469736 | 2012-02-27 10:52:35 +0000 | [diff] [blame] | 712 | int fdtdec_get_bool(const void *blob, int node, const char *prop_name) |
| 713 | { |
| 714 | const s32 *cell; |
| 715 | int len; |
| 716 | |
| 717 | debug("%s: %s\n", __func__, prop_name); |
| 718 | cell = fdt_getprop(blob, node, prop_name, &len); |
| 719 | return cell != NULL; |
| 720 | } |
Simon Glass | 0d83c4d | 2012-02-27 10:52:36 +0000 | [diff] [blame] | 721 | |
Simon Glass | b491e9c | 2015-01-05 20:05:26 -0700 | [diff] [blame] | 722 | int fdtdec_parse_phandle_with_args(const void *blob, int src_node, |
| 723 | const char *list_name, |
| 724 | const char *cells_name, |
| 725 | int cell_count, int index, |
| 726 | struct fdtdec_phandle_args *out_args) |
| 727 | { |
| 728 | const __be32 *list, *list_end; |
| 729 | int rc = 0, size, cur_index = 0; |
| 730 | uint32_t count = 0; |
| 731 | int node = -1; |
| 732 | int phandle; |
| 733 | |
| 734 | /* Retrieve the phandle list property */ |
| 735 | list = fdt_getprop(blob, src_node, list_name, &size); |
| 736 | if (!list) |
| 737 | return -ENOENT; |
| 738 | list_end = list + size / sizeof(*list); |
| 739 | |
| 740 | /* Loop over the phandles until all the requested entry is found */ |
| 741 | while (list < list_end) { |
| 742 | rc = -EINVAL; |
| 743 | count = 0; |
| 744 | |
| 745 | /* |
| 746 | * If phandle is 0, then it is an empty entry with no |
| 747 | * arguments. Skip forward to the next entry. |
| 748 | */ |
| 749 | phandle = be32_to_cpup(list++); |
| 750 | if (phandle) { |
| 751 | /* |
| 752 | * Find the provider node and parse the #*-cells |
| 753 | * property to determine the argument length. |
| 754 | * |
| 755 | * This is not needed if the cell count is hard-coded |
| 756 | * (i.e. cells_name not set, but cell_count is set), |
| 757 | * except when we're going to return the found node |
| 758 | * below. |
| 759 | */ |
| 760 | if (cells_name || cur_index == index) { |
| 761 | node = fdt_node_offset_by_phandle(blob, |
| 762 | phandle); |
| 763 | if (!node) { |
| 764 | debug("%s: could not find phandle\n", |
| 765 | fdt_get_name(blob, src_node, |
| 766 | NULL)); |
| 767 | goto err; |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if (cells_name) { |
| 772 | count = fdtdec_get_int(blob, node, cells_name, |
| 773 | -1); |
| 774 | if (count == -1) { |
| 775 | debug("%s: could not get %s for %s\n", |
| 776 | fdt_get_name(blob, src_node, |
| 777 | NULL), |
| 778 | cells_name, |
| 779 | fdt_get_name(blob, node, |
| 780 | NULL)); |
| 781 | goto err; |
| 782 | } |
| 783 | } else { |
| 784 | count = cell_count; |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | * Make sure that the arguments actually fit in the |
| 789 | * remaining property data length |
| 790 | */ |
| 791 | if (list + count > list_end) { |
| 792 | debug("%s: arguments longer than property\n", |
| 793 | fdt_get_name(blob, src_node, NULL)); |
| 794 | goto err; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | /* |
| 799 | * All of the error cases above bail out of the loop, so at |
| 800 | * this point, the parsing is successful. If the requested |
| 801 | * index matches, then fill the out_args structure and return, |
| 802 | * or return -ENOENT for an empty entry. |
| 803 | */ |
| 804 | rc = -ENOENT; |
| 805 | if (cur_index == index) { |
| 806 | if (!phandle) |
| 807 | goto err; |
| 808 | |
| 809 | if (out_args) { |
| 810 | int i; |
| 811 | |
| 812 | if (count > MAX_PHANDLE_ARGS) { |
| 813 | debug("%s: too many arguments %d\n", |
| 814 | fdt_get_name(blob, src_node, |
| 815 | NULL), count); |
| 816 | count = MAX_PHANDLE_ARGS; |
| 817 | } |
| 818 | out_args->node = node; |
| 819 | out_args->args_count = count; |
| 820 | for (i = 0; i < count; i++) { |
| 821 | out_args->args[i] = |
| 822 | be32_to_cpup(list++); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | /* Found it! return success */ |
| 827 | return 0; |
| 828 | } |
| 829 | |
| 830 | node = -1; |
| 831 | list += count; |
| 832 | cur_index++; |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * Result will be one of: |
| 837 | * -ENOENT : index is for empty phandle |
| 838 | * -EINVAL : parsing error on data |
| 839 | * [1..n] : Number of phandle (count mode; when index = -1) |
| 840 | */ |
| 841 | rc = index < 0 ? cur_index : -ENOENT; |
| 842 | err: |
| 843 | return rc; |
| 844 | } |
| 845 | |
Peng Fan | 23dd3a0 | 2016-02-01 13:31:15 +0800 | [diff] [blame] | 846 | int fdtdec_get_child_count(const void *blob, int node) |
| 847 | { |
| 848 | int subnode; |
| 849 | int num = 0; |
| 850 | |
Simon Glass | 499c29e | 2016-10-02 17:59:29 -0600 | [diff] [blame] | 851 | fdt_for_each_subnode(subnode, blob, node) |
Peng Fan | 23dd3a0 | 2016-02-01 13:31:15 +0800 | [diff] [blame] | 852 | num++; |
| 853 | |
| 854 | return num; |
| 855 | } |
| 856 | |
Anton Staff | f2305ec | 2012-04-17 09:01:28 +0000 | [diff] [blame] | 857 | int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name, |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 858 | u8 *array, int count) |
Anton Staff | f2305ec | 2012-04-17 09:01:28 +0000 | [diff] [blame] | 859 | { |
| 860 | const u8 *cell; |
| 861 | int err; |
| 862 | |
| 863 | cell = get_prop_check_min_len(blob, node, prop_name, count, &err); |
| 864 | if (!err) |
| 865 | memcpy(array, cell, count); |
| 866 | return err; |
| 867 | } |
| 868 | |
| 869 | const u8 *fdtdec_locate_byte_array(const void *blob, int node, |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 870 | const char *prop_name, int count) |
Anton Staff | f2305ec | 2012-04-17 09:01:28 +0000 | [diff] [blame] | 871 | { |
| 872 | const u8 *cell; |
| 873 | int err; |
| 874 | |
| 875 | cell = get_prop_check_min_len(blob, node, prop_name, count, &err); |
| 876 | if (err) |
| 877 | return NULL; |
| 878 | return cell; |
| 879 | } |
Abhilash Kesavan | 67217af | 2012-10-25 16:30:58 +0000 | [diff] [blame] | 880 | |
Abhilash Kesavan | 67217af | 2012-10-25 16:30:58 +0000 | [diff] [blame] | 881 | int fdtdec_get_config_int(const void *blob, const char *prop_name, |
Mario Six | bb3e06c | 2018-01-15 11:07:35 +0100 | [diff] [blame] | 882 | int default_val) |
Abhilash Kesavan | 67217af | 2012-10-25 16:30:58 +0000 | [diff] [blame] | 883 | { |
| 884 | int config_node; |
| 885 | |
| 886 | debug("%s: %s\n", __func__, prop_name); |
| 887 | config_node = fdt_path_offset(blob, "/config"); |
| 888 | if (config_node < 0) |
| 889 | return default_val; |
| 890 | return fdtdec_get_int(blob, config_node, prop_name, default_val); |
| 891 | } |
Simon Glass | b049447 | 2012-10-25 16:30:59 +0000 | [diff] [blame] | 892 | |
Gabe Black | 3e1c151 | 2012-10-25 16:31:04 +0000 | [diff] [blame] | 893 | int fdtdec_get_config_bool(const void *blob, const char *prop_name) |
| 894 | { |
| 895 | int config_node; |
| 896 | const void *prop; |
| 897 | |
| 898 | debug("%s: %s\n", __func__, prop_name); |
| 899 | config_node = fdt_path_offset(blob, "/config"); |
| 900 | if (config_node < 0) |
| 901 | return 0; |
| 902 | prop = fdt_get_property(blob, config_node, prop_name, NULL); |
| 903 | |
| 904 | return prop != NULL; |
| 905 | } |
| 906 | |
Simon Glass | b049447 | 2012-10-25 16:30:59 +0000 | [diff] [blame] | 907 | char *fdtdec_get_config_string(const void *blob, const char *prop_name) |
| 908 | { |
| 909 | const char *nodep; |
| 910 | int nodeoffset; |
| 911 | int len; |
| 912 | |
| 913 | debug("%s: %s\n", __func__, prop_name); |
| 914 | nodeoffset = fdt_path_offset(blob, "/config"); |
| 915 | if (nodeoffset < 0) |
| 916 | return NULL; |
| 917 | |
| 918 | nodep = fdt_getprop(blob, nodeoffset, prop_name, &len); |
| 919 | if (!nodep) |
| 920 | return NULL; |
| 921 | |
| 922 | return (char *)nodep; |
| 923 | } |
Simon Glass | f909b9c | 2012-10-25 16:31:00 +0000 | [diff] [blame] | 924 | |
Simon Glass | 9ffbe33 | 2015-03-05 12:25:14 -0700 | [diff] [blame] | 925 | u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells) |
Thierry Reding | a937b80 | 2014-08-26 17:33:53 +0200 | [diff] [blame] | 926 | { |
| 927 | u64 number = 0; |
| 928 | |
| 929 | while (cells--) |
| 930 | number = (number << 32) | fdt32_to_cpu(*ptr++); |
| 931 | |
| 932 | return number; |
| 933 | } |
| 934 | |
| 935 | int fdt_get_resource(const void *fdt, int node, const char *property, |
| 936 | unsigned int index, struct fdt_resource *res) |
| 937 | { |
| 938 | const fdt32_t *ptr, *end; |
| 939 | int na, ns, len, parent; |
| 940 | unsigned int i = 0; |
| 941 | |
| 942 | parent = fdt_parent_offset(fdt, node); |
| 943 | if (parent < 0) |
| 944 | return parent; |
| 945 | |
| 946 | na = fdt_address_cells(fdt, parent); |
| 947 | ns = fdt_size_cells(fdt, parent); |
| 948 | |
| 949 | ptr = fdt_getprop(fdt, node, property, &len); |
| 950 | if (!ptr) |
| 951 | return len; |
| 952 | |
| 953 | end = ptr + len / sizeof(*ptr); |
| 954 | |
| 955 | while (ptr + na + ns <= end) { |
| 956 | if (i == index) { |
Mario Six | 6abfbc8 | 2018-01-15 11:07:36 +0100 | [diff] [blame] | 957 | res->start = fdtdec_get_number(ptr, na); |
| 958 | res->end = res->start; |
Thierry Reding | a937b80 | 2014-08-26 17:33:53 +0200 | [diff] [blame] | 959 | res->end += fdtdec_get_number(&ptr[na], ns) - 1; |
| 960 | return 0; |
| 961 | } |
| 962 | |
| 963 | ptr += na + ns; |
| 964 | i++; |
| 965 | } |
| 966 | |
| 967 | return -FDT_ERR_NOTFOUND; |
| 968 | } |
| 969 | |
| 970 | int fdt_get_named_resource(const void *fdt, int node, const char *property, |
| 971 | const char *prop_names, const char *name, |
| 972 | struct fdt_resource *res) |
| 973 | { |
| 974 | int index; |
| 975 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 976 | index = fdt_stringlist_search(fdt, node, prop_names, name); |
Thierry Reding | a937b80 | 2014-08-26 17:33:53 +0200 | [diff] [blame] | 977 | if (index < 0) |
| 978 | return index; |
| 979 | |
| 980 | return fdt_get_resource(fdt, node, property, index, res); |
| 981 | } |
Thierry Reding | 2485631 | 2014-08-26 17:33:54 +0200 | [diff] [blame] | 982 | |
Simon Glass | b24e864 | 2015-04-14 21:03:21 -0600 | [diff] [blame] | 983 | static int decode_timing_property(const void *blob, int node, const char *name, |
| 984 | struct timing_entry *result) |
| 985 | { |
| 986 | int length, ret = 0; |
| 987 | const u32 *prop; |
| 988 | |
| 989 | prop = fdt_getprop(blob, node, name, &length); |
| 990 | if (!prop) { |
| 991 | debug("%s: could not find property %s\n", |
| 992 | fdt_get_name(blob, node, NULL), name); |
| 993 | return length; |
| 994 | } |
| 995 | |
| 996 | if (length == sizeof(u32)) { |
| 997 | result->typ = fdtdec_get_int(blob, node, name, 0); |
| 998 | result->min = result->typ; |
| 999 | result->max = result->typ; |
| 1000 | } else { |
| 1001 | ret = fdtdec_get_int_array(blob, node, name, &result->min, 3); |
| 1002 | } |
| 1003 | |
| 1004 | return ret; |
| 1005 | } |
| 1006 | |
| 1007 | int fdtdec_decode_display_timing(const void *blob, int parent, int index, |
| 1008 | struct display_timing *dt) |
| 1009 | { |
| 1010 | int i, node, timings_node; |
| 1011 | u32 val = 0; |
| 1012 | int ret = 0; |
| 1013 | |
| 1014 | timings_node = fdt_subnode_offset(blob, parent, "display-timings"); |
| 1015 | if (timings_node < 0) |
| 1016 | return timings_node; |
| 1017 | |
| 1018 | for (i = 0, node = fdt_first_subnode(blob, timings_node); |
| 1019 | node > 0 && i != index; |
| 1020 | node = fdt_next_subnode(blob, node)) |
| 1021 | i++; |
| 1022 | |
| 1023 | if (node < 0) |
| 1024 | return node; |
| 1025 | |
| 1026 | memset(dt, 0, sizeof(*dt)); |
| 1027 | |
| 1028 | ret |= decode_timing_property(blob, node, "hback-porch", |
| 1029 | &dt->hback_porch); |
| 1030 | ret |= decode_timing_property(blob, node, "hfront-porch", |
| 1031 | &dt->hfront_porch); |
| 1032 | ret |= decode_timing_property(blob, node, "hactive", &dt->hactive); |
| 1033 | ret |= decode_timing_property(blob, node, "hsync-len", &dt->hsync_len); |
| 1034 | ret |= decode_timing_property(blob, node, "vback-porch", |
| 1035 | &dt->vback_porch); |
| 1036 | ret |= decode_timing_property(blob, node, "vfront-porch", |
| 1037 | &dt->vfront_porch); |
| 1038 | ret |= decode_timing_property(blob, node, "vactive", &dt->vactive); |
| 1039 | ret |= decode_timing_property(blob, node, "vsync-len", &dt->vsync_len); |
| 1040 | ret |= decode_timing_property(blob, node, "clock-frequency", |
| 1041 | &dt->pixelclock); |
| 1042 | |
| 1043 | dt->flags = 0; |
| 1044 | val = fdtdec_get_int(blob, node, "vsync-active", -1); |
| 1045 | if (val != -1) { |
| 1046 | dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH : |
| 1047 | DISPLAY_FLAGS_VSYNC_LOW; |
| 1048 | } |
| 1049 | val = fdtdec_get_int(blob, node, "hsync-active", -1); |
| 1050 | if (val != -1) { |
| 1051 | dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH : |
| 1052 | DISPLAY_FLAGS_HSYNC_LOW; |
| 1053 | } |
| 1054 | val = fdtdec_get_int(blob, node, "de-active", -1); |
| 1055 | if (val != -1) { |
| 1056 | dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH : |
| 1057 | DISPLAY_FLAGS_DE_LOW; |
| 1058 | } |
| 1059 | val = fdtdec_get_int(blob, node, "pixelclk-active", -1); |
| 1060 | if (val != -1) { |
| 1061 | dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE : |
| 1062 | DISPLAY_FLAGS_PIXDATA_NEGEDGE; |
| 1063 | } |
| 1064 | |
| 1065 | if (fdtdec_get_bool(blob, node, "interlaced")) |
| 1066 | dt->flags |= DISPLAY_FLAGS_INTERLACED; |
| 1067 | if (fdtdec_get_bool(blob, node, "doublescan")) |
| 1068 | dt->flags |= DISPLAY_FLAGS_DOUBLESCAN; |
| 1069 | if (fdtdec_get_bool(blob, node, "doubleclk")) |
| 1070 | dt->flags |= DISPLAY_FLAGS_DOUBLECLK; |
| 1071 | |
Simon Glass | 6eb90e1 | 2016-05-05 07:28:21 -0600 | [diff] [blame] | 1072 | return ret; |
Simon Glass | b24e864 | 2015-04-14 21:03:21 -0600 | [diff] [blame] | 1073 | } |
| 1074 | |
Siva Durga Prasad Paladugu | b3d55ea | 2018-07-16 15:56:11 +0530 | [diff] [blame] | 1075 | int fdtdec_setup_mem_size_base(void) |
Nathan Rossi | ba97b72 | 2016-12-19 00:03:34 +1000 | [diff] [blame] | 1076 | { |
| 1077 | int ret, mem; |
| 1078 | struct fdt_resource res; |
| 1079 | |
| 1080 | mem = fdt_path_offset(gd->fdt_blob, "/memory"); |
| 1081 | if (mem < 0) { |
| 1082 | debug("%s: Missing /memory node\n", __func__); |
| 1083 | return -EINVAL; |
| 1084 | } |
| 1085 | |
| 1086 | ret = fdt_get_resource(gd->fdt_blob, mem, "reg", 0, &res); |
| 1087 | if (ret != 0) { |
| 1088 | debug("%s: Unable to decode first memory bank\n", __func__); |
| 1089 | return -EINVAL; |
| 1090 | } |
| 1091 | |
| 1092 | gd->ram_size = (phys_size_t)(res.end - res.start + 1); |
Siva Durga Prasad Paladugu | 94a1d52 | 2018-07-16 15:56:10 +0530 | [diff] [blame] | 1093 | gd->ram_base = (unsigned long)res.start; |
Simon Glass | 640b78f | 2017-05-27 07:38:13 -0600 | [diff] [blame] | 1094 | debug("%s: Initial DRAM size %llx\n", __func__, |
| 1095 | (unsigned long long)gd->ram_size); |
Nathan Rossi | ba97b72 | 2016-12-19 00:03:34 +1000 | [diff] [blame] | 1096 | |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | #if defined(CONFIG_NR_DRAM_BANKS) |
Jens Wiklander | e2da4c9 | 2018-07-13 12:12:11 +0200 | [diff] [blame] | 1101 | |
Michal Simek | 0bd065c | 2018-10-03 15:53:52 +0200 | [diff] [blame] | 1102 | static int get_next_memory_node(const void *blob, int mem) |
Jens Wiklander | e2da4c9 | 2018-07-13 12:12:11 +0200 | [diff] [blame] | 1103 | { |
Jens Wiklander | e2da4c9 | 2018-07-13 12:12:11 +0200 | [diff] [blame] | 1104 | do { |
Michal Simek | 0bd065c | 2018-10-03 15:53:52 +0200 | [diff] [blame] | 1105 | mem = fdt_node_offset_by_prop_value(gd->fdt_blob, mem, |
| 1106 | "device_type", "memory", 7); |
| 1107 | } while (!fdtdec_get_is_enabled(blob, mem)); |
Jens Wiklander | e2da4c9 | 2018-07-13 12:12:11 +0200 | [diff] [blame] | 1108 | |
| 1109 | return mem; |
| 1110 | } |
| 1111 | |
Nathan Rossi | ba97b72 | 2016-12-19 00:03:34 +1000 | [diff] [blame] | 1112 | int fdtdec_setup_memory_banksize(void) |
| 1113 | { |
Michal Simek | 0bd065c | 2018-10-03 15:53:52 +0200 | [diff] [blame] | 1114 | int bank, ret, mem, reg = 0; |
| 1115 | struct fdt_resource res; |
Nathan Rossi | ba97b72 | 2016-12-19 00:03:34 +1000 | [diff] [blame] | 1116 | |
Michal Simek | 0bd065c | 2018-10-03 15:53:52 +0200 | [diff] [blame] | 1117 | mem = get_next_memory_node(gd->fdt_blob, -1); |
| 1118 | if (mem < 0) { |
| 1119 | debug("%s: Missing /memory node\n", __func__); |
| 1120 | return -EINVAL; |
| 1121 | } |
Nathan Rossi | ba97b72 | 2016-12-19 00:03:34 +1000 | [diff] [blame] | 1122 | |
| 1123 | for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) { |
Michal Simek | 0bd065c | 2018-10-03 15:53:52 +0200 | [diff] [blame] | 1124 | ret = fdt_get_resource(gd->fdt_blob, mem, "reg", reg++, &res); |
| 1125 | if (ret == -FDT_ERR_NOTFOUND) { |
Marek Vasut | 7f0293a | 2017-11-27 05:32:42 +0100 | [diff] [blame] | 1126 | reg = 0; |
Michal Simek | 0bd065c | 2018-10-03 15:53:52 +0200 | [diff] [blame] | 1127 | mem = get_next_memory_node(gd->fdt_blob, mem); |
| 1128 | if (mem == -FDT_ERR_NOTFOUND) |
| 1129 | break; |
| 1130 | |
| 1131 | ret = fdt_get_resource(gd->fdt_blob, mem, "reg", reg++, &res); |
| 1132 | if (ret == -FDT_ERR_NOTFOUND) |
| 1133 | break; |
| 1134 | } |
| 1135 | if (ret != 0) { |
| 1136 | return -EINVAL; |
Marek Vasut | 7f0293a | 2017-11-27 05:32:42 +0100 | [diff] [blame] | 1137 | } |
Nathan Rossi | ba97b72 | 2016-12-19 00:03:34 +1000 | [diff] [blame] | 1138 | |
| 1139 | gd->bd->bi_dram[bank].start = (phys_addr_t)res.start; |
| 1140 | gd->bd->bi_dram[bank].size = |
| 1141 | (phys_size_t)(res.end - res.start + 1); |
| 1142 | |
| 1143 | debug("%s: DRAM Bank #%d: start = 0x%llx, size = 0x%llx\n", |
| 1144 | __func__, bank, |
| 1145 | (unsigned long long)gd->bd->bi_dram[bank].start, |
| 1146 | (unsigned long long)gd->bd->bi_dram[bank].size); |
| 1147 | } |
| 1148 | |
| 1149 | return 0; |
| 1150 | } |
| 1151 | #endif |
| 1152 | |
Jean-Jacques Hiblot | a2fee16 | 2017-09-15 12:57:32 +0200 | [diff] [blame] | 1153 | #if CONFIG_IS_ENABLED(MULTI_DTB_FIT) |
| 1154 | # if CONFIG_IS_ENABLED(MULTI_DTB_FIT_GZIP) ||\ |
| 1155 | CONFIG_IS_ENABLED(MULTI_DTB_FIT_LZO) |
| 1156 | static int uncompress_blob(const void *src, ulong sz_src, void **dstp) |
| 1157 | { |
| 1158 | size_t sz_out = CONFIG_SPL_MULTI_DTB_FIT_UNCOMPRESS_SZ; |
| 1159 | ulong sz_in = sz_src; |
| 1160 | void *dst; |
| 1161 | int rc; |
| 1162 | |
| 1163 | if (CONFIG_IS_ENABLED(GZIP)) |
| 1164 | if (gzip_parse_header(src, sz_in) < 0) |
| 1165 | return -1; |
| 1166 | if (CONFIG_IS_ENABLED(LZO)) |
| 1167 | if (!lzop_is_valid_header(src)) |
| 1168 | return -EBADMSG; |
| 1169 | |
| 1170 | if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC)) { |
| 1171 | dst = malloc(sz_out); |
| 1172 | if (!dst) { |
| 1173 | puts("uncompress_blob: Unable to allocate memory\n"); |
| 1174 | return -ENOMEM; |
| 1175 | } |
| 1176 | } else { |
| 1177 | # if CONFIG_IS_ENABLED(MULTI_DTB_FIT_USER_DEFINED_AREA) |
| 1178 | dst = (void *)CONFIG_VAL(MULTI_DTB_FIT_USER_DEF_ADDR); |
| 1179 | # else |
| 1180 | return -ENOTSUPP; |
| 1181 | # endif |
| 1182 | } |
| 1183 | |
| 1184 | if (CONFIG_IS_ENABLED(GZIP)) |
| 1185 | rc = gunzip(dst, sz_out, (u8 *)src, &sz_in); |
| 1186 | else if (CONFIG_IS_ENABLED(LZO)) |
| 1187 | rc = lzop_decompress(src, sz_in, dst, &sz_out); |
| 1188 | |
| 1189 | if (rc < 0) { |
| 1190 | /* not a valid compressed blob */ |
| 1191 | puts("uncompress_blob: Unable to uncompress\n"); |
| 1192 | if (CONFIG_IS_ENABLED(MULTI_DTB_FIT_DYN_ALLOC)) |
| 1193 | free(dst); |
| 1194 | return -EBADMSG; |
| 1195 | } |
| 1196 | *dstp = dst; |
| 1197 | return 0; |
| 1198 | } |
| 1199 | # else |
| 1200 | static int uncompress_blob(const void *src, ulong sz_src, void **dstp) |
| 1201 | { |
Marek Vasut | 5bd49fb | 2018-10-18 20:37:05 +0200 | [diff] [blame] | 1202 | *dstp = (void *)src; |
| 1203 | return 0; |
Jean-Jacques Hiblot | a2fee16 | 2017-09-15 12:57:32 +0200 | [diff] [blame] | 1204 | } |
| 1205 | # endif |
| 1206 | #endif |
| 1207 | |
Rob Clark | 974dcac | 2018-01-10 11:34:37 +0100 | [diff] [blame] | 1208 | #if defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE) |
| 1209 | /* |
| 1210 | * For CONFIG_OF_SEPARATE, the board may optionally implement this to |
| 1211 | * provide and/or fixup the fdt. |
| 1212 | */ |
| 1213 | __weak void *board_fdt_blob_setup(void) |
| 1214 | { |
| 1215 | void *fdt_blob = NULL; |
| 1216 | #ifdef CONFIG_SPL_BUILD |
| 1217 | /* FDT is at end of BSS unless it is in a different memory region */ |
| 1218 | if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) |
| 1219 | fdt_blob = (ulong *)&_image_binary_end; |
| 1220 | else |
| 1221 | fdt_blob = (ulong *)&__bss_end; |
| 1222 | #else |
| 1223 | /* FDT is at end of image */ |
| 1224 | fdt_blob = (ulong *)&_end; |
| 1225 | #endif |
| 1226 | return fdt_blob; |
| 1227 | } |
| 1228 | #endif |
| 1229 | |
Simon Glass | a087767 | 2015-02-27 22:06:35 -0700 | [diff] [blame] | 1230 | int fdtdec_setup(void) |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 1231 | { |
Masahiro Yamada | 366b24f | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 1232 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Jean-Jacques Hiblot | a2fee16 | 2017-09-15 12:57:32 +0200 | [diff] [blame] | 1233 | # if CONFIG_IS_ENABLED(MULTI_DTB_FIT) |
| 1234 | void *fdt_blob; |
| 1235 | # endif |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 1236 | # ifdef CONFIG_OF_EMBED |
| 1237 | /* Get a pointer to the FDT */ |
Goldschmidt Simon | 5e7a1c2 | 2017-11-21 12:29:56 +0000 | [diff] [blame] | 1238 | # ifdef CONFIG_SPL_BUILD |
| 1239 | gd->fdt_blob = __dtb_dt_spl_begin; |
| 1240 | # else |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 1241 | gd->fdt_blob = __dtb_dt_begin; |
Goldschmidt Simon | 5e7a1c2 | 2017-11-21 12:29:56 +0000 | [diff] [blame] | 1242 | # endif |
Rob Clark | 974dcac | 2018-01-10 11:34:37 +0100 | [diff] [blame] | 1243 | # elif defined(CONFIG_OF_BOARD) || defined(CONFIG_OF_SEPARATE) |
Alex Deymo | 5b661ec | 2017-04-02 01:25:20 -0700 | [diff] [blame] | 1244 | /* Allow the board to override the fdt address. */ |
| 1245 | gd->fdt_blob = board_fdt_blob_setup(); |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 1246 | # elif defined(CONFIG_OF_HOSTFILE) |
| 1247 | if (sandbox_read_fdt_from_file()) { |
| 1248 | puts("Failed to read control FDT\n"); |
| 1249 | return -1; |
| 1250 | } |
| 1251 | # endif |
| 1252 | # ifndef CONFIG_SPL_BUILD |
| 1253 | /* Allow the early environment to override the fdt address */ |
Thomas Fitzsimmons | 919646d | 2018-06-08 17:59:45 -0400 | [diff] [blame] | 1254 | # if CONFIG_IS_ENABLED(OF_PRIOR_STAGE) |
| 1255 | gd->fdt_blob = (void *)prior_stage_fdt_address; |
| 1256 | # else |
Heinrich Schuchardt | 370a176 | 2018-11-18 17:58:50 +0100 | [diff] [blame] | 1257 | gd->fdt_blob = map_sysmem |
| 1258 | (env_get_ulong("fdtcontroladdr", 16, |
| 1259 | (unsigned long)map_to_sysmem(gd->fdt_blob)), 0); |
Thomas Fitzsimmons | 919646d | 2018-06-08 17:59:45 -0400 | [diff] [blame] | 1260 | # endif |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 1261 | # endif |
Jean-Jacques Hiblot | a2fee16 | 2017-09-15 12:57:32 +0200 | [diff] [blame] | 1262 | |
| 1263 | # if CONFIG_IS_ENABLED(MULTI_DTB_FIT) |
| 1264 | /* |
| 1265 | * Try and uncompress the blob. |
| 1266 | * Unfortunately there is no way to know how big the input blob really |
| 1267 | * is. So let us set the maximum input size arbitrarily high. 16MB |
| 1268 | * ought to be more than enough for packed DTBs. |
| 1269 | */ |
| 1270 | if (uncompress_blob(gd->fdt_blob, 0x1000000, &fdt_blob) == 0) |
| 1271 | gd->fdt_blob = fdt_blob; |
| 1272 | |
| 1273 | /* |
| 1274 | * Check if blob is a FIT images containings DTBs. |
| 1275 | * If so, pick the most relevant |
| 1276 | */ |
| 1277 | fdt_blob = locate_dtb_in_fit(gd->fdt_blob); |
Jean-Jacques Hiblot | 7c530e3 | 2018-12-07 14:50:52 +0100 | [diff] [blame] | 1278 | if (fdt_blob) { |
| 1279 | gd->multi_dtb_fit = gd->fdt_blob; |
Jean-Jacques Hiblot | a2fee16 | 2017-09-15 12:57:32 +0200 | [diff] [blame] | 1280 | gd->fdt_blob = fdt_blob; |
Jean-Jacques Hiblot | 7c530e3 | 2018-12-07 14:50:52 +0100 | [diff] [blame] | 1281 | } |
| 1282 | |
Jean-Jacques Hiblot | a2fee16 | 2017-09-15 12:57:32 +0200 | [diff] [blame] | 1283 | # endif |
Heiko Schocher | d7b4232 | 2014-03-03 12:19:30 +0100 | [diff] [blame] | 1284 | #endif |
Jean-Jacques Hiblot | a2fee16 | 2017-09-15 12:57:32 +0200 | [diff] [blame] | 1285 | |
Simon Glass | a087767 | 2015-02-27 22:06:35 -0700 | [diff] [blame] | 1286 | return fdtdec_prepare_fdt(); |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
Jean-Jacques Hiblot | 7c530e3 | 2018-12-07 14:50:52 +0100 | [diff] [blame] | 1289 | #if CONFIG_IS_ENABLED(MULTI_DTB_FIT) |
| 1290 | int fdtdec_resetup(int *rescan) |
| 1291 | { |
| 1292 | void *fdt_blob; |
| 1293 | |
| 1294 | /* |
| 1295 | * If the current DTB is part of a compressed FIT image, |
| 1296 | * try to locate the best match from the uncompressed |
| 1297 | * FIT image stillpresent there. Save the time and space |
| 1298 | * required to uncompress it again. |
| 1299 | */ |
| 1300 | if (gd->multi_dtb_fit) { |
| 1301 | fdt_blob = locate_dtb_in_fit(gd->multi_dtb_fit); |
| 1302 | |
| 1303 | if (fdt_blob == gd->fdt_blob) { |
| 1304 | /* |
| 1305 | * The best match did not change. no need to tear down |
| 1306 | * the DM and rescan the fdt. |
| 1307 | */ |
| 1308 | *rescan = 0; |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | *rescan = 1; |
| 1313 | gd->fdt_blob = fdt_blob; |
| 1314 | return fdtdec_prepare_fdt(); |
| 1315 | } |
| 1316 | |
| 1317 | /* |
| 1318 | * If multi_dtb_fit is NULL, it means that blob appended to u-boot is |
| 1319 | * not a FIT image containings DTB, but a single DTB. There is no need |
| 1320 | * to teard down DM and rescan the DT in this case. |
| 1321 | */ |
| 1322 | *rescan = 0; |
| 1323 | return 0; |
| 1324 | } |
| 1325 | #endif |
| 1326 | |
Michael Pratt | fa28d14 | 2018-06-11 13:07:09 -0600 | [diff] [blame] | 1327 | #ifdef CONFIG_NR_DRAM_BANKS |
| 1328 | int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id, |
| 1329 | phys_addr_t *basep, phys_size_t *sizep, bd_t *bd) |
| 1330 | { |
| 1331 | int addr_cells, size_cells; |
| 1332 | const u32 *cell, *end; |
| 1333 | u64 total_size, size, addr; |
| 1334 | int node, child; |
| 1335 | bool auto_size; |
| 1336 | int bank; |
| 1337 | int len; |
| 1338 | |
| 1339 | debug("%s: board_id=%d\n", __func__, board_id); |
| 1340 | if (!area) |
| 1341 | area = "/memory"; |
| 1342 | node = fdt_path_offset(blob, area); |
| 1343 | if (node < 0) { |
| 1344 | debug("No %s node found\n", area); |
| 1345 | return -ENOENT; |
| 1346 | } |
| 1347 | |
| 1348 | cell = fdt_getprop(blob, node, "reg", &len); |
| 1349 | if (!cell) { |
| 1350 | debug("No reg property found\n"); |
| 1351 | return -ENOENT; |
| 1352 | } |
| 1353 | |
| 1354 | addr_cells = fdt_address_cells(blob, node); |
| 1355 | size_cells = fdt_size_cells(blob, node); |
| 1356 | |
| 1357 | /* Check the board id and mask */ |
| 1358 | for (child = fdt_first_subnode(blob, node); |
| 1359 | child >= 0; |
| 1360 | child = fdt_next_subnode(blob, child)) { |
| 1361 | int match_mask, match_value; |
| 1362 | |
| 1363 | match_mask = fdtdec_get_int(blob, child, "match-mask", -1); |
| 1364 | match_value = fdtdec_get_int(blob, child, "match-value", -1); |
| 1365 | |
| 1366 | if (match_value >= 0 && |
| 1367 | ((board_id & match_mask) == match_value)) { |
| 1368 | /* Found matching mask */ |
| 1369 | debug("Found matching mask %d\n", match_mask); |
| 1370 | node = child; |
| 1371 | cell = fdt_getprop(blob, node, "reg", &len); |
| 1372 | if (!cell) { |
| 1373 | debug("No memory-banks property found\n"); |
| 1374 | return -EINVAL; |
| 1375 | } |
| 1376 | break; |
| 1377 | } |
| 1378 | } |
| 1379 | /* Note: if no matching subnode was found we use the parent node */ |
| 1380 | |
| 1381 | if (bd) { |
| 1382 | memset(bd->bi_dram, '\0', sizeof(bd->bi_dram[0]) * |
| 1383 | CONFIG_NR_DRAM_BANKS); |
| 1384 | } |
| 1385 | |
| 1386 | auto_size = fdtdec_get_bool(blob, node, "auto-size"); |
| 1387 | |
| 1388 | total_size = 0; |
| 1389 | end = cell + len / 4 - addr_cells - size_cells; |
| 1390 | debug("cell at %p, end %p\n", cell, end); |
| 1391 | for (bank = 0; bank < CONFIG_NR_DRAM_BANKS; bank++) { |
| 1392 | if (cell > end) |
| 1393 | break; |
| 1394 | addr = 0; |
| 1395 | if (addr_cells == 2) |
| 1396 | addr += (u64)fdt32_to_cpu(*cell++) << 32UL; |
| 1397 | addr += fdt32_to_cpu(*cell++); |
| 1398 | if (bd) |
| 1399 | bd->bi_dram[bank].start = addr; |
| 1400 | if (basep && !bank) |
| 1401 | *basep = (phys_addr_t)addr; |
| 1402 | |
| 1403 | size = 0; |
| 1404 | if (size_cells == 2) |
| 1405 | size += (u64)fdt32_to_cpu(*cell++) << 32UL; |
| 1406 | size += fdt32_to_cpu(*cell++); |
| 1407 | |
| 1408 | if (auto_size) { |
| 1409 | u64 new_size; |
| 1410 | |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 1411 | debug("Auto-sizing %llx, size %llx: ", addr, size); |
Michael Pratt | fa28d14 | 2018-06-11 13:07:09 -0600 | [diff] [blame] | 1412 | new_size = get_ram_size((long *)(uintptr_t)addr, size); |
| 1413 | if (new_size == size) { |
| 1414 | debug("OK\n"); |
| 1415 | } else { |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 1416 | debug("sized to %llx\n", new_size); |
Michael Pratt | fa28d14 | 2018-06-11 13:07:09 -0600 | [diff] [blame] | 1417 | size = new_size; |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | if (bd) |
| 1422 | bd->bi_dram[bank].size = size; |
| 1423 | total_size += size; |
| 1424 | } |
| 1425 | |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 1426 | debug("Memory size %llu\n", total_size); |
Michael Pratt | fa28d14 | 2018-06-11 13:07:09 -0600 | [diff] [blame] | 1427 | if (sizep) |
| 1428 | *sizep = (phys_size_t)total_size; |
| 1429 | |
| 1430 | return 0; |
| 1431 | } |
| 1432 | #endif /* CONFIG_NR_DRAM_BANKS */ |
| 1433 | |
Simon Glass | 26b78b2 | 2015-02-27 22:06:34 -0700 | [diff] [blame] | 1434 | #endif /* !USE_HOSTCC */ |