blob: 3cfa50a6d5354db5e3b1cc5ad5288b1bf11c9a72 [file] [log] [blame]
Simon Glass2d2af852011-10-24 19:15:32 +00001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02003 * SPDX-License-Identifier: GPL-2.0+
Simon Glass2d2af852011-10-24 19:15:32 +00004 */
5
Heiko Schocherd7b42322014-03-03 12:19:30 +01006#ifndef USE_HOSTCC
Simon Glass2d2af852011-10-24 19:15:32 +00007#include <common.h>
Simon Glass9908ad42014-07-23 06:55:09 -06008#include <errno.h>
Simon Glass2d2af852011-10-24 19:15:32 +00009#include <serial.h>
10#include <libfdt.h>
11#include <fdtdec.h>
Simon Glass9908ad42014-07-23 06:55:09 -060012#include <linux/ctype.h>
Simon Glass2d2af852011-10-24 19:15:32 +000013
14DECLARE_GLOBAL_DATA_PTR;
15
16/*
17 * Here are the type we know about. One day we might allow drivers to
18 * register. For now we just put them here. The COMPAT macro allows us to
19 * turn this into a sparse list later, and keeps the ID with the name.
20 */
21#define COMPAT(id, name) name
22static const char * const compat_names[COMPAT_COUNT] = {
Simon Glass65444982012-02-27 10:52:34 +000023 COMPAT(UNKNOWN, "<none>"),
Simon Glass08d6ec22012-02-27 10:52:49 +000024 COMPAT(NVIDIA_TEGRA20_USB, "nvidia,tegra20-ehci"),
Jim Lin2fefb8b2013-06-21 19:05:47 +080025 COMPAT(NVIDIA_TEGRA30_USB, "nvidia,tegra30-ehci"),
26 COMPAT(NVIDIA_TEGRA114_USB, "nvidia,tegra114-ehci"),
Tom Warrenf8cf4b22013-02-08 07:25:30 +000027 COMPAT(NVIDIA_TEGRA114_I2C, "nvidia,tegra114-i2c"),
Yen Linccf7e902012-03-06 19:00:23 +000028 COMPAT(NVIDIA_TEGRA20_I2C, "nvidia,tegra20-i2c"),
29 COMPAT(NVIDIA_TEGRA20_DVC, "nvidia,tegra20-i2c-dvc"),
Jimmy Zhang5983ef82012-04-02 13:18:52 +000030 COMPAT(NVIDIA_TEGRA20_EMC, "nvidia,tegra20-emc"),
31 COMPAT(NVIDIA_TEGRA20_EMC_TABLE, "nvidia,tegra20-emc-table"),
Rakesh Iyer382abbd2012-04-17 09:01:35 +000032 COMPAT(NVIDIA_TEGRA20_KBC, "nvidia,tegra20-kbc"),
Jim Lin5d309e62012-07-29 20:53:29 +000033 COMPAT(NVIDIA_TEGRA20_NAND, "nvidia,tegra20-nand"),
Simon Glass1564f342012-10-17 13:24:49 +000034 COMPAT(NVIDIA_TEGRA20_PWM, "nvidia,tegra20-pwm"),
Wei Ni6555e692012-10-17 13:24:50 +000035 COMPAT(NVIDIA_TEGRA20_DC, "nvidia,tegra20-dc"),
Stephen Warren65e0eb12014-01-24 12:46:06 -070036 COMPAT(NVIDIA_TEGRA124_SDMMC, "nvidia,tegra124-sdhci"),
Tom Warren7db0e2d2013-03-04 14:07:18 -070037 COMPAT(NVIDIA_TEGRA30_SDMMC, "nvidia,tegra30-sdhci"),
Tom Warren9745cf82013-02-21 12:31:30 +000038 COMPAT(NVIDIA_TEGRA20_SDMMC, "nvidia,tegra20-sdhci"),
Allen Martine7659522013-01-29 13:51:24 +000039 COMPAT(NVIDIA_TEGRA20_SFLASH, "nvidia,tegra20-sflash"),
Allen Martinba4fb9b2013-01-29 13:51:28 +000040 COMPAT(NVIDIA_TEGRA20_SLINK, "nvidia,tegra20-slink"),
Allen Martinb54f0d92013-03-16 18:58:09 +000041 COMPAT(NVIDIA_TEGRA114_SPI, "nvidia,tegra114-spi"),
Thierry Reding412c7582014-12-09 22:25:12 -070042 COMPAT(NVIDIA_TEGRA124_PCIE, "nvidia,tegra124-pcie"),
43 COMPAT(NVIDIA_TEGRA30_PCIE, "nvidia,tegra30-pcie"),
44 COMPAT(NVIDIA_TEGRA20_PCIE, "nvidia,tegra20-pcie"),
Thierry Redingf202e022014-12-09 22:25:09 -070045 COMPAT(NVIDIA_TEGRA124_XUSB_PADCTL, "nvidia,tegra124-xusb-padctl"),
Hatim RVbe5ad5a2012-12-11 00:52:46 +000046 COMPAT(SMSC_LAN9215, "smsc,lan9215"),
47 COMPAT(SAMSUNG_EXYNOS5_SROMC, "samsung,exynos-sromc"),
Rajeshwari Shinde2b71e9d2012-12-26 20:03:10 +000048 COMPAT(SAMSUNG_S3C2440_I2C, "samsung,s3c2440-i2c"),
Rajeshwari Shinde9d116732012-12-26 20:03:16 +000049 COMPAT(SAMSUNG_EXYNOS5_SOUND, "samsung,exynos-sound"),
50 COMPAT(WOLFSON_WM8994_CODEC, "wolfson,wm8994-codec"),
Rajeshwari Shinded95beaf2012-12-26 20:03:20 +000051 COMPAT(SAMSUNG_EXYNOS_SPI, "samsung,exynos-spi"),
Hung-ying Tyanc48ca88f2013-05-15 18:27:28 +080052 COMPAT(GOOGLE_CROS_EC, "google,cros-ec"),
Hung-ying Tyan4a48bcf2013-05-15 18:27:32 +080053 COMPAT(GOOGLE_CROS_EC_KEYB, "google,cros-ec-keyb"),
Rajeshwari Shinded6e9f442013-01-07 23:35:05 +000054 COMPAT(SAMSUNG_EXYNOS_EHCI, "samsung,exynos-ehci"),
Vivek Gautamf8745232013-09-14 14:02:48 +053055 COMPAT(SAMSUNG_EXYNOS5_XHCI, "samsung,exynos5250-xhci"),
Rajeshwari Shinded6e9f442013-01-07 23:35:05 +000056 COMPAT(SAMSUNG_EXYNOS_USB_PHY, "samsung,exynos-usb-phy"),
Vivek Gautamf8745232013-09-14 14:02:48 +053057 COMPAT(SAMSUNG_EXYNOS5_USB3_PHY, "samsung,exynos5250-usb3-phy"),
Akshay Saraswataa773182013-02-25 01:13:01 +000058 COMPAT(SAMSUNG_EXYNOS_TMU, "samsung,exynos-tmu"),
Ajay Kumar035a1cd2013-02-21 23:53:00 +000059 COMPAT(SAMSUNG_EXYNOS_FIMD, "samsung,exynos-fimd"),
Piotr Wilczek24e368a2014-03-07 14:59:39 +010060 COMPAT(SAMSUNG_EXYNOS_MIPI_DSI, "samsung,exynos-mipi-dsi"),
Ajay Kumar39bac152013-02-21 23:53:05 +000061 COMPAT(SAMSUNG_EXYNOS5_DP, "samsung,exynos5-dp"),
Jaehoon Chunge3fb6fa2014-05-16 13:59:51 +090062 COMPAT(SAMSUNG_EXYNOS_DWMMC, "samsung,exynos-dwmmc"),
Piotr Wilczek12cf19e2014-03-07 14:59:41 +010063 COMPAT(SAMSUNG_EXYNOS_MMC, "samsung,exynos-mmc"),
Rajeshwari Shindeffff1612013-06-24 16:47:20 +053064 COMPAT(SAMSUNG_EXYNOS_SERIAL, "samsung,exynos4210-uart"),
Rajeshwari Shinde1e9eeb02013-01-08 21:03:38 +000065 COMPAT(MAXIM_MAX77686_PMIC, "maxim,max77686_pmic"),
Simon Glass609560942013-03-11 06:08:08 +000066 COMPAT(GENERIC_SPI_FLASH, "spi-flash"),
Rajeshwari Shinde003c28c2013-02-14 19:46:15 +000067 COMPAT(MAXIM_98095_CODEC, "maxim,max98095-codec"),
Rong Chang8faa9452013-04-12 10:44:57 +000068 COMPAT(INFINEON_SLB9635_TPM, "infineon,slb9635-tpm"),
Vincent Palatin06883eb2013-04-12 11:04:36 +000069 COMPAT(INFINEON_SLB9645_TPM, "infineon,slb9645-tpm"),
naveen krishna chatradhi0734bae2013-04-29 15:58:52 -070070 COMPAT(SAMSUNG_EXYNOS5_I2C, "samsung,exynos5-hsi2c"),
Simon Glass36170f02014-02-27 13:26:12 -070071 COMPAT(SANDBOX_HOST_EMULATION, "sandbox,host-emulation"),
Simon Glassb9ddbf42014-02-27 13:26:19 -070072 COMPAT(SANDBOX_LCD_SDL, "sandbox,lcd-sdl"),
Tom Wai-Hong Tam2ab56952014-05-20 06:01:36 -060073 COMPAT(TI_TPS65090, "ti,tps65090"),
Simon Glassb81b5a82014-05-20 06:01:42 -060074 COMPAT(COMPAT_NXP_PTN3460, "nxp,ptn3460"),
Ajay Kumare24c5022014-09-05 16:53:33 +053075 COMPAT(SAMSUNG_EXYNOS_SYSMMU, "samsung,sysmmu-v3.3"),
Vadim Bendebury8c5e2642014-09-05 16:53:34 +053076 COMPAT(PARADE_PS8625, "parade,ps8625"),
Simon Glasseff23f72014-10-10 07:30:14 -060077 COMPAT(COMPAT_INTEL_LPC, "intel,lpc"),
Simon Glassf79d5382014-11-12 22:42:21 -070078 COMPAT(INTEL_MICROCODE, "intel,microcode"),
Simon Glass268eefd2014-11-12 22:42:28 -070079 COMPAT(MEMORY_SPD, "memory-spd"),
Simon Glasscd0adb32014-11-14 18:18:38 -070080 COMPAT(INTEL_PANTHERPOINT_AHCI, "intel,pantherpoint-ahci"),
Simon Glasscf46d372014-11-24 21:18:16 -070081 COMPAT(INTEL_MODEL_206AX, "intel,model-206ax"),
Simon Glassd90f8e12014-11-14 20:56:36 -070082 COMPAT(INTEL_GMA, "intel,gma"),
Thierry Reding09c0cf22014-12-09 22:25:05 -070083 COMPAT(AMS_AS3722, "ams,as3722"),
Simon Glassb34f4052015-01-19 22:16:06 -070084 COMPAT(INTEL_ICH_SPI, "intel,ich-spi"),
Simon Glass2d2af852011-10-24 19:15:32 +000085};
86
Simon Glassb022ead2012-01-17 08:20:50 +000087const char *fdtdec_get_compatible(enum fdt_compat_id id)
88{
89 /* We allow reading of the 'unknown' ID for testing purposes */
90 assert(id >= 0 && id < COMPAT_COUNT);
91 return compat_names[id];
92}
93
Simon Glassf4299552013-03-19 04:58:51 +000094fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
95 const char *prop_name, fdt_size_t *sizep)
Simon Glass2d2af852011-10-24 19:15:32 +000096{
97 const fdt_addr_t *cell;
98 int len;
99
Simon Glassc8c86dd2012-07-12 05:25:01 +0000100 debug("%s: %s: ", __func__, prop_name);
Simon Glass2d2af852011-10-24 19:15:32 +0000101 cell = fdt_getprop(blob, node, prop_name, &len);
Simon Glassf4299552013-03-19 04:58:51 +0000102 if (cell && ((!sizep && len == sizeof(fdt_addr_t)) ||
103 len == sizeof(fdt_addr_t) * 2)) {
Simon Glassc8c86dd2012-07-12 05:25:01 +0000104 fdt_addr_t addr = fdt_addr_to_cpu(*cell);
Simon Glassf4299552013-03-19 04:58:51 +0000105 if (sizep) {
106 const fdt_size_t *size;
Simon Glassc8c86dd2012-07-12 05:25:01 +0000107
Simon Glassf4299552013-03-19 04:58:51 +0000108 size = (fdt_size_t *)((char *)cell +
109 sizeof(fdt_addr_t));
110 *sizep = fdt_size_to_cpu(*size);
Simon Glasse18d1022013-11-10 10:26:54 -0700111 debug("addr=%08lx, size=%08x\n",
112 (ulong)addr, *sizep);
Simon Glassf4299552013-03-19 04:58:51 +0000113 } else {
Simon Glasse18d1022013-11-10 10:26:54 -0700114 debug("%08lx\n", (ulong)addr);
Simon Glassf4299552013-03-19 04:58:51 +0000115 }
Simon Glassc8c86dd2012-07-12 05:25:01 +0000116 return addr;
117 }
118 debug("(not found)\n");
Simon Glass2d2af852011-10-24 19:15:32 +0000119 return FDT_ADDR_T_NONE;
120}
121
Simon Glassf4299552013-03-19 04:58:51 +0000122fdt_addr_t fdtdec_get_addr(const void *blob, int node,
123 const char *prop_name)
124{
125 return fdtdec_get_addr_size(blob, node, prop_name, NULL);
126}
127
Bin Meng779847e2014-12-31 16:05:11 +0800128#ifdef CONFIG_PCI
129int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
130 const char *prop_name, struct fdt_pci_addr *addr)
131{
132 const u32 *cell;
133 int len;
134 int ret = -ENOENT;
135
136 debug("%s: %s: ", __func__, prop_name);
137
138 /*
139 * If we follow the pci bus bindings strictly, we should check
140 * the value of the node's parent node's #address-cells and
141 * #size-cells. They need to be 3 and 2 accordingly. However,
142 * for simplicity we skip the check here.
143 */
144 cell = fdt_getprop(blob, node, prop_name, &len);
145 if (!cell)
146 goto fail;
147
148 if ((len % FDT_PCI_REG_SIZE) == 0) {
149 int num = len / FDT_PCI_REG_SIZE;
150 int i;
151
152 for (i = 0; i < num; i++) {
153 debug("pci address #%d: %08lx %08lx %08lx\n", i,
154 (ulong)fdt_addr_to_cpu(cell[0]),
155 (ulong)fdt_addr_to_cpu(cell[1]),
156 (ulong)fdt_addr_to_cpu(cell[2]));
157 if ((fdt_addr_to_cpu(*cell) & type) == type) {
158 addr->phys_hi = fdt_addr_to_cpu(cell[0]);
159 addr->phys_mid = fdt_addr_to_cpu(cell[1]);
160 addr->phys_lo = fdt_addr_to_cpu(cell[2]);
161 break;
162 } else {
163 cell += (FDT_PCI_ADDR_CELLS +
164 FDT_PCI_SIZE_CELLS);
165 }
166 }
167
168 if (i == num)
169 goto fail;
170
171 return 0;
172 } else {
173 ret = -EINVAL;
174 }
175
176fail:
177 debug("(not found)\n");
178 return ret;
179}
180
181int fdtdec_get_pci_vendev(const void *blob, int node, u16 *vendor, u16 *device)
182{
183 const char *list, *end;
184 int len;
185
186 list = fdt_getprop(blob, node, "compatible", &len);
187 if (!list)
188 return -ENOENT;
189
190 end = list + len;
191 while (list < end) {
192 char *s;
193
194 len = strlen(list);
195 if (len >= strlen("pciVVVV,DDDD")) {
196 s = strstr(list, "pci");
197
198 /*
199 * check if the string is something like pciVVVV,DDDD.RR
200 * or just pciVVVV,DDDD
201 */
202 if (s && s[7] == ',' &&
203 (s[12] == '.' || s[12] == 0)) {
204 s += 3;
205 *vendor = simple_strtol(s, NULL, 16);
206
207 s += 5;
208 *device = simple_strtol(s, NULL, 16);
209
210 return 0;
211 }
212 } else {
213 list += (len + 1);
214 }
215 }
216
217 return -ENOENT;
218}
219
220int fdtdec_get_pci_bdf(const void *blob, int node,
221 struct fdt_pci_addr *addr, pci_dev_t *bdf)
222{
223 u16 dt_vendor, dt_device, vendor, device;
224 int ret;
225
226 /* get vendor id & device id from the compatible string */
227 ret = fdtdec_get_pci_vendev(blob, node, &dt_vendor, &dt_device);
228 if (ret)
229 return ret;
230
231 /* extract the bdf from fdt_pci_addr */
232 *bdf = addr->phys_hi & 0xffff00;
233
234 /* read vendor id & device id based on bdf */
235 pci_read_config_word(*bdf, PCI_VENDOR_ID, &vendor);
236 pci_read_config_word(*bdf, PCI_DEVICE_ID, &device);
237
238 /*
239 * Note there are two places in the device tree to fully describe
240 * a pci device: one is via compatible string with a format of
241 * "pciVVVV,DDDD" and the other one is the bdf numbers encoded in
242 * the device node's reg address property. We read the vendor id
243 * and device id based on bdf and compare the values with the
244 * "VVVV,DDDD". If they are the same, then we are good to use bdf
245 * to read device's bar. But if they are different, we have to rely
246 * on the vendor id and device id extracted from the compatible
247 * string and locate the real bdf by pci_find_device(). This is
248 * because normally we may only know device's device number and
249 * function number when writing device tree. The bus number is
250 * dynamically assigned during the pci enumeration process.
251 */
252 if ((dt_vendor != vendor) || (dt_device != device)) {
253 *bdf = pci_find_device(dt_vendor, dt_device, 0);
254 if (*bdf == -1)
255 return -ENODEV;
256 }
257
258 return 0;
259}
260
261int fdtdec_get_pci_bar32(const void *blob, int node,
262 struct fdt_pci_addr *addr, u32 *bar)
263{
264 pci_dev_t bdf;
265 int barnum;
266 int ret;
267
268 /* get pci devices's bdf */
269 ret = fdtdec_get_pci_bdf(blob, node, addr, &bdf);
270 if (ret)
271 return ret;
272
273 /* extract the bar number from fdt_pci_addr */
274 barnum = addr->phys_hi & 0xff;
275 if ((barnum < PCI_BASE_ADDRESS_0) || (barnum > PCI_CARDBUS_CIS))
276 return -EINVAL;
277
278 barnum = (barnum - PCI_BASE_ADDRESS_0) / 4;
279 *bar = pci_read_bar32(pci_bus_to_hose(PCI_BUS(bdf)), bdf, barnum);
280
281 return 0;
282}
283#endif
284
Che-Liang Chioud81d0172012-10-25 16:31:05 +0000285uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
286 uint64_t default_val)
287{
288 const uint64_t *cell64;
289 int length;
290
291 cell64 = fdt_getprop(blob, node, prop_name, &length);
292 if (!cell64 || length < sizeof(*cell64))
293 return default_val;
294
295 return fdt64_to_cpu(*cell64);
296}
297
Simon Glass65444982012-02-27 10:52:34 +0000298int fdtdec_get_is_enabled(const void *blob, int node)
Simon Glass2d2af852011-10-24 19:15:32 +0000299{
300 const char *cell;
301
Simon Glass65444982012-02-27 10:52:34 +0000302 /*
303 * It should say "okay", so only allow that. Some fdts use "ok" but
304 * this is a bug. Please fix your device tree source file. See here
305 * for discussion:
306 *
307 * http://www.mail-archive.com/u-boot@lists.denx.de/msg71598.html
308 */
Simon Glass2d2af852011-10-24 19:15:32 +0000309 cell = fdt_getprop(blob, node, "status", NULL);
310 if (cell)
Simon Glass65444982012-02-27 10:52:34 +0000311 return 0 == strcmp(cell, "okay");
312 return 1;
Simon Glass2d2af852011-10-24 19:15:32 +0000313}
314
Gerald Van Barencdac3392012-11-12 23:13:54 -0500315enum fdt_compat_id fdtdec_lookup(const void *blob, int node)
Simon Glass2d2af852011-10-24 19:15:32 +0000316{
317 enum fdt_compat_id id;
318
319 /* Search our drivers */
320 for (id = COMPAT_UNKNOWN; id < COMPAT_COUNT; id++)
321 if (0 == fdt_node_check_compatible(blob, node,
322 compat_names[id]))
323 return id;
324 return COMPAT_UNKNOWN;
325}
326
327int fdtdec_next_compatible(const void *blob, int node,
328 enum fdt_compat_id id)
329{
330 return fdt_node_offset_by_compatible(blob, node, compat_names[id]);
331}
332
Simon Glass2e979b12012-04-02 13:18:42 +0000333int fdtdec_next_compatible_subnode(const void *blob, int node,
334 enum fdt_compat_id id, int *depthp)
335{
336 do {
337 node = fdt_next_node(blob, node, depthp);
338 } while (*depthp > 1);
339
340 /* If this is a direct subnode, and compatible, return it */
341 if (*depthp == 1 && 0 == fdt_node_check_compatible(
342 blob, node, compat_names[id]))
343 return node;
344
345 return -FDT_ERR_NOTFOUND;
346}
347
Simon Glass2d2af852011-10-24 19:15:32 +0000348int fdtdec_next_alias(const void *blob, const char *name,
349 enum fdt_compat_id id, int *upto)
350{
351#define MAX_STR_LEN 20
352 char str[MAX_STR_LEN + 20];
353 int node, err;
354
355 /* snprintf() is not available */
356 assert(strlen(name) < MAX_STR_LEN);
357 sprintf(str, "%.*s%d", MAX_STR_LEN, name, *upto);
Simon Glasse1d15ca2012-10-31 14:02:42 +0000358 node = fdt_path_offset(blob, str);
Simon Glass2d2af852011-10-24 19:15:32 +0000359 if (node < 0)
360 return node;
361 err = fdt_node_check_compatible(blob, node, compat_names[id]);
362 if (err < 0)
363 return err;
Simon Glass65444982012-02-27 10:52:34 +0000364 if (err)
365 return -FDT_ERR_NOTFOUND;
366 (*upto)++;
367 return node;
Simon Glass2d2af852011-10-24 19:15:32 +0000368}
369
Simon Glassb022ead2012-01-17 08:20:50 +0000370int fdtdec_find_aliases_for_id(const void *blob, const char *name,
371 enum fdt_compat_id id, int *node_list, int maxcount)
372{
Simon Glass1f772662012-02-03 15:13:53 +0000373 memset(node_list, '\0', sizeof(*node_list) * maxcount);
374
375 return fdtdec_add_aliases_for_id(blob, name, id, node_list, maxcount);
376}
377
378/* TODO: Can we tighten this code up a little? */
379int fdtdec_add_aliases_for_id(const void *blob, const char *name,
380 enum fdt_compat_id id, int *node_list, int maxcount)
381{
Simon Glassb022ead2012-01-17 08:20:50 +0000382 int name_len = strlen(name);
383 int nodes[maxcount];
384 int num_found = 0;
385 int offset, node;
386 int alias_node;
387 int count;
388 int i, j;
389
390 /* find the alias node if present */
391 alias_node = fdt_path_offset(blob, "/aliases");
392
393 /*
394 * start with nothing, and we can assume that the root node can't
395 * match
396 */
397 memset(nodes, '\0', sizeof(nodes));
398
399 /* First find all the compatible nodes */
400 for (node = count = 0; node >= 0 && count < maxcount;) {
401 node = fdtdec_next_compatible(blob, node, id);
402 if (node >= 0)
403 nodes[count++] = node;
404 }
405 if (node >= 0)
406 debug("%s: warning: maxcount exceeded with alias '%s'\n",
407 __func__, name);
408
409 /* Now find all the aliases */
Simon Glassb022ead2012-01-17 08:20:50 +0000410 for (offset = fdt_first_property_offset(blob, alias_node);
411 offset > 0;
412 offset = fdt_next_property_offset(blob, offset)) {
413 const struct fdt_property *prop;
414 const char *path;
415 int number;
416 int found;
417
418 node = 0;
419 prop = fdt_get_property_by_offset(blob, offset, NULL);
420 path = fdt_string(blob, fdt32_to_cpu(prop->nameoff));
421 if (prop->len && 0 == strncmp(path, name, name_len))
422 node = fdt_path_offset(blob, prop->data);
423 if (node <= 0)
424 continue;
425
426 /* Get the alias number */
427 number = simple_strtoul(path + name_len, NULL, 10);
428 if (number < 0 || number >= maxcount) {
429 debug("%s: warning: alias '%s' is out of range\n",
430 __func__, path);
431 continue;
432 }
433
434 /* Make sure the node we found is actually in our list! */
435 found = -1;
436 for (j = 0; j < count; j++)
437 if (nodes[j] == node) {
438 found = j;
439 break;
440 }
441
442 if (found == -1) {
443 debug("%s: warning: alias '%s' points to a node "
444 "'%s' that is missing or is not compatible "
445 " with '%s'\n", __func__, path,
446 fdt_get_name(blob, node, NULL),
447 compat_names[id]);
448 continue;
449 }
450
451 /*
452 * Add this node to our list in the right place, and mark
453 * it as done.
454 */
455 if (fdtdec_get_is_enabled(blob, node)) {
Simon Glass1f772662012-02-03 15:13:53 +0000456 if (node_list[number]) {
457 debug("%s: warning: alias '%s' requires that "
458 "a node be placed in the list in a "
459 "position which is already filled by "
460 "node '%s'\n", __func__, path,
461 fdt_get_name(blob, node, NULL));
462 continue;
463 }
Simon Glassb022ead2012-01-17 08:20:50 +0000464 node_list[number] = node;
465 if (number >= num_found)
466 num_found = number + 1;
467 }
Simon Glass1f772662012-02-03 15:13:53 +0000468 nodes[found] = 0;
Simon Glassb022ead2012-01-17 08:20:50 +0000469 }
470
471 /* Add any nodes not mentioned by an alias */
472 for (i = j = 0; i < maxcount; i++) {
473 if (!node_list[i]) {
474 for (; j < maxcount; j++)
475 if (nodes[j] &&
476 fdtdec_get_is_enabled(blob, nodes[j]))
477 break;
478
479 /* Have we run out of nodes to add? */
480 if (j == maxcount)
481 break;
482
483 assert(!node_list[i]);
484 node_list[i] = nodes[j++];
485 if (i >= num_found)
486 num_found = i + 1;
487 }
488 }
489
490 return num_found;
491}
492
Simon Glass9908ad42014-07-23 06:55:09 -0600493int fdtdec_get_alias_seq(const void *blob, const char *base, int offset,
494 int *seqp)
495{
496 int base_len = strlen(base);
497 const char *find_name;
498 int find_namelen;
499 int prop_offset;
500 int aliases;
501
502 find_name = fdt_get_name(blob, offset, &find_namelen);
503 debug("Looking for '%s' at %d, name %s\n", base, offset, find_name);
504
505 aliases = fdt_path_offset(blob, "/aliases");
506 for (prop_offset = fdt_first_property_offset(blob, aliases);
507 prop_offset > 0;
508 prop_offset = fdt_next_property_offset(blob, prop_offset)) {
509 const char *prop;
510 const char *name;
511 const char *slash;
512 const char *p;
513 int len;
514
515 prop = fdt_getprop_by_offset(blob, prop_offset, &name, &len);
516 debug(" - %s, %s\n", name, prop);
517 if (len < find_namelen || *prop != '/' || prop[len - 1] ||
518 strncmp(name, base, base_len))
519 continue;
520
521 slash = strrchr(prop, '/');
522 if (strcmp(slash + 1, find_name))
523 continue;
Simon Glass37ef3252014-11-11 10:46:20 -0700524 for (p = name + strlen(name) - 1; p > name; p--) {
525 if (!isdigit(*p)) {
526 *seqp = simple_strtoul(p + 1, NULL, 10);
Simon Glass9908ad42014-07-23 06:55:09 -0600527 debug("Found seq %d\n", *seqp);
528 return 0;
529 }
530 }
531 }
532
533 debug("Not found\n");
534 return -ENOENT;
535}
536
Simon Glass1e374fb2014-09-04 16:27:24 -0600537int fdtdec_get_chosen_node(const void *blob, const char *name)
538{
539 const char *prop;
540 int chosen_node;
541 int len;
542
543 if (!blob)
544 return -FDT_ERR_NOTFOUND;
545 chosen_node = fdt_path_offset(blob, "/chosen");
546 prop = fdt_getprop(blob, chosen_node, name, &len);
547 if (!prop)
548 return -FDT_ERR_NOTFOUND;
549 return fdt_path_offset(blob, prop);
550}
551
Simon Glass1691f692012-03-28 10:08:24 +0000552int fdtdec_check_fdt(void)
553{
554 /*
555 * We must have an FDT, but we cannot panic() yet since the console
556 * is not ready. So for now, just assert(). Boards which need an early
557 * FDT (prior to console ready) will need to make their own
558 * arrangements and do their own checks.
559 */
560 assert(!fdtdec_prepare_fdt());
561 return 0;
562}
563
Simon Glass2d2af852011-10-24 19:15:32 +0000564/*
565 * This function is a little odd in that it accesses global data. At some
566 * point if the architecture board.c files merge this will make more sense.
567 * Even now, it is common code.
568 */
Simon Glass1691f692012-03-28 10:08:24 +0000569int fdtdec_prepare_fdt(void)
Simon Glass2d2af852011-10-24 19:15:32 +0000570{
Simon Glass71136852013-04-20 08:42:46 +0000571 if (!gd->fdt_blob || ((uintptr_t)gd->fdt_blob & 3) ||
572 fdt_check_header(gd->fdt_blob)) {
Simon Glass1691f692012-03-28 10:08:24 +0000573 printf("No valid FDT found - please append one to U-Boot "
574 "binary, use u-boot-dtb.bin or define "
Simon Glass62cf9122013-04-26 02:53:43 +0000575 "CONFIG_OF_EMBED. For sandbox, use -d <file.dtb>\n");
Simon Glass1691f692012-03-28 10:08:24 +0000576 return -1;
577 }
Simon Glass2d2af852011-10-24 19:15:32 +0000578 return 0;
579}
Simon Glassc4697362012-02-27 10:52:35 +0000580
581int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name)
582{
583 const u32 *phandle;
584 int lookup;
585
Simon Glassc8c86dd2012-07-12 05:25:01 +0000586 debug("%s: %s\n", __func__, prop_name);
Simon Glassc4697362012-02-27 10:52:35 +0000587 phandle = fdt_getprop(blob, node, prop_name, NULL);
588 if (!phandle)
589 return -FDT_ERR_NOTFOUND;
590
591 lookup = fdt_node_offset_by_phandle(blob, fdt32_to_cpu(*phandle));
592 return lookup;
593}
594
595/**
596 * Look up a property in a node and check that it has a minimum length.
597 *
598 * @param blob FDT blob
599 * @param node node to examine
600 * @param prop_name name of property to find
601 * @param min_len minimum property length in bytes
602 * @param err 0 if ok, or -FDT_ERR_NOTFOUND if the property is not
603 found, or -FDT_ERR_BADLAYOUT if not enough data
604 * @return pointer to cell, which is only valid if err == 0
605 */
606static const void *get_prop_check_min_len(const void *blob, int node,
607 const char *prop_name, int min_len, int *err)
608{
609 const void *cell;
610 int len;
611
612 debug("%s: %s\n", __func__, prop_name);
613 cell = fdt_getprop(blob, node, prop_name, &len);
614 if (!cell)
615 *err = -FDT_ERR_NOTFOUND;
616 else if (len < min_len)
617 *err = -FDT_ERR_BADLAYOUT;
618 else
619 *err = 0;
620 return cell;
621}
622
623int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
624 u32 *array, int count)
625{
626 const u32 *cell;
627 int i, err = 0;
628
629 debug("%s: %s\n", __func__, prop_name);
630 cell = get_prop_check_min_len(blob, node, prop_name,
631 sizeof(u32) * count, &err);
632 if (!err) {
633 for (i = 0; i < count; i++)
634 array[i] = fdt32_to_cpu(cell[i]);
635 }
636 return err;
637}
638
Simon Glasseda0c3f2014-11-10 18:00:19 -0700639int fdtdec_get_int_array_count(const void *blob, int node,
640 const char *prop_name, u32 *array, int count)
641{
642 const u32 *cell;
643 int len, elems;
644 int i;
645
646 debug("%s: %s\n", __func__, prop_name);
647 cell = fdt_getprop(blob, node, prop_name, &len);
648 if (!cell)
649 return -FDT_ERR_NOTFOUND;
650 elems = len / sizeof(u32);
651 if (count > elems)
652 count = elems;
653 for (i = 0; i < count; i++)
654 array[i] = fdt32_to_cpu(cell[i]);
655
656 return count;
657}
658
Simon Glass744ec232012-04-02 13:18:41 +0000659const u32 *fdtdec_locate_array(const void *blob, int node,
660 const char *prop_name, int count)
661{
662 const u32 *cell;
663 int err;
664
665 cell = get_prop_check_min_len(blob, node, prop_name,
666 sizeof(u32) * count, &err);
667 return err ? NULL : cell;
668}
669
Simon Glassc4697362012-02-27 10:52:35 +0000670int fdtdec_get_bool(const void *blob, int node, const char *prop_name)
671{
672 const s32 *cell;
673 int len;
674
675 debug("%s: %s\n", __func__, prop_name);
676 cell = fdt_getprop(blob, node, prop_name, &len);
677 return cell != NULL;
678}
Simon Glass0d83c4d2012-02-27 10:52:36 +0000679
Simon Glassb491e9c2015-01-05 20:05:26 -0700680int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
681 const char *list_name,
682 const char *cells_name,
683 int cell_count, int index,
684 struct fdtdec_phandle_args *out_args)
685{
686 const __be32 *list, *list_end;
687 int rc = 0, size, cur_index = 0;
688 uint32_t count = 0;
689 int node = -1;
690 int phandle;
691
692 /* Retrieve the phandle list property */
693 list = fdt_getprop(blob, src_node, list_name, &size);
694 if (!list)
695 return -ENOENT;
696 list_end = list + size / sizeof(*list);
697
698 /* Loop over the phandles until all the requested entry is found */
699 while (list < list_end) {
700 rc = -EINVAL;
701 count = 0;
702
703 /*
704 * If phandle is 0, then it is an empty entry with no
705 * arguments. Skip forward to the next entry.
706 */
707 phandle = be32_to_cpup(list++);
708 if (phandle) {
709 /*
710 * Find the provider node and parse the #*-cells
711 * property to determine the argument length.
712 *
713 * This is not needed if the cell count is hard-coded
714 * (i.e. cells_name not set, but cell_count is set),
715 * except when we're going to return the found node
716 * below.
717 */
718 if (cells_name || cur_index == index) {
719 node = fdt_node_offset_by_phandle(blob,
720 phandle);
721 if (!node) {
722 debug("%s: could not find phandle\n",
723 fdt_get_name(blob, src_node,
724 NULL));
725 goto err;
726 }
727 }
728
729 if (cells_name) {
730 count = fdtdec_get_int(blob, node, cells_name,
731 -1);
732 if (count == -1) {
733 debug("%s: could not get %s for %s\n",
734 fdt_get_name(blob, src_node,
735 NULL),
736 cells_name,
737 fdt_get_name(blob, node,
738 NULL));
739 goto err;
740 }
741 } else {
742 count = cell_count;
743 }
744
745 /*
746 * Make sure that the arguments actually fit in the
747 * remaining property data length
748 */
749 if (list + count > list_end) {
750 debug("%s: arguments longer than property\n",
751 fdt_get_name(blob, src_node, NULL));
752 goto err;
753 }
754 }
755
756 /*
757 * All of the error cases above bail out of the loop, so at
758 * this point, the parsing is successful. If the requested
759 * index matches, then fill the out_args structure and return,
760 * or return -ENOENT for an empty entry.
761 */
762 rc = -ENOENT;
763 if (cur_index == index) {
764 if (!phandle)
765 goto err;
766
767 if (out_args) {
768 int i;
769
770 if (count > MAX_PHANDLE_ARGS) {
771 debug("%s: too many arguments %d\n",
772 fdt_get_name(blob, src_node,
773 NULL), count);
774 count = MAX_PHANDLE_ARGS;
775 }
776 out_args->node = node;
777 out_args->args_count = count;
778 for (i = 0; i < count; i++) {
779 out_args->args[i] =
780 be32_to_cpup(list++);
781 }
782 }
783
784 /* Found it! return success */
785 return 0;
786 }
787
788 node = -1;
789 list += count;
790 cur_index++;
791 }
792
793 /*
794 * Result will be one of:
795 * -ENOENT : index is for empty phandle
796 * -EINVAL : parsing error on data
797 * [1..n] : Number of phandle (count mode; when index = -1)
798 */
799 rc = index < 0 ? cur_index : -ENOENT;
800 err:
801 return rc;
802}
803
Anton Stafff2305ec2012-04-17 09:01:28 +0000804int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
805 u8 *array, int count)
806{
807 const u8 *cell;
808 int err;
809
810 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
811 if (!err)
812 memcpy(array, cell, count);
813 return err;
814}
815
816const u8 *fdtdec_locate_byte_array(const void *blob, int node,
817 const char *prop_name, int count)
818{
819 const u8 *cell;
820 int err;
821
822 cell = get_prop_check_min_len(blob, node, prop_name, count, &err);
823 if (err)
824 return NULL;
825 return cell;
826}
Abhilash Kesavan67217af2012-10-25 16:30:58 +0000827
Abhilash Kesavan67217af2012-10-25 16:30:58 +0000828int fdtdec_get_config_int(const void *blob, const char *prop_name,
829 int default_val)
830{
831 int config_node;
832
833 debug("%s: %s\n", __func__, prop_name);
834 config_node = fdt_path_offset(blob, "/config");
835 if (config_node < 0)
836 return default_val;
837 return fdtdec_get_int(blob, config_node, prop_name, default_val);
838}
Simon Glassb0494472012-10-25 16:30:59 +0000839
Gabe Black3e1c1512012-10-25 16:31:04 +0000840int fdtdec_get_config_bool(const void *blob, const char *prop_name)
841{
842 int config_node;
843 const void *prop;
844
845 debug("%s: %s\n", __func__, prop_name);
846 config_node = fdt_path_offset(blob, "/config");
847 if (config_node < 0)
848 return 0;
849 prop = fdt_get_property(blob, config_node, prop_name, NULL);
850
851 return prop != NULL;
852}
853
Simon Glassb0494472012-10-25 16:30:59 +0000854char *fdtdec_get_config_string(const void *blob, const char *prop_name)
855{
856 const char *nodep;
857 int nodeoffset;
858 int len;
859
860 debug("%s: %s\n", __func__, prop_name);
861 nodeoffset = fdt_path_offset(blob, "/config");
862 if (nodeoffset < 0)
863 return NULL;
864
865 nodep = fdt_getprop(blob, nodeoffset, prop_name, &len);
866 if (!nodep)
867 return NULL;
868
869 return (char *)nodep;
870}
Simon Glassf909b9c2012-10-25 16:31:00 +0000871
Simon Glassa28f8ba2014-10-23 18:58:51 -0600872int fdtdec_decode_region(const void *blob, int node, const char *prop_name,
873 fdt_addr_t *basep, fdt_size_t *sizep)
Simon Glassf909b9c2012-10-25 16:31:00 +0000874{
875 const fdt_addr_t *cell;
876 int len;
877
Simon Glassa28f8ba2014-10-23 18:58:51 -0600878 debug("%s: %s: %s\n", __func__, fdt_get_name(blob, node, NULL),
879 prop_name);
Simon Glassf909b9c2012-10-25 16:31:00 +0000880 cell = fdt_getprop(blob, node, prop_name, &len);
Simon Glassa28f8ba2014-10-23 18:58:51 -0600881 if (!cell || (len < sizeof(fdt_addr_t) * 2)) {
882 debug("cell=%p, len=%d\n", cell, len);
Simon Glassf909b9c2012-10-25 16:31:00 +0000883 return -1;
Simon Glassa28f8ba2014-10-23 18:58:51 -0600884 }
885
886 *basep = fdt_addr_to_cpu(*cell);
887 *sizep = fdt_size_to_cpu(cell[1]);
888 debug("%s: base=%08lx, size=%lx\n", __func__, (ulong)*basep,
889 (ulong)*sizep);
Simon Glassf909b9c2012-10-25 16:31:00 +0000890
Simon Glassf909b9c2012-10-25 16:31:00 +0000891 return 0;
892}
Simon Glass9f783722014-02-27 13:26:01 -0700893
894/**
895 * Read a flash entry from the fdt
896 *
897 * @param blob FDT blob
898 * @param node Offset of node to read
899 * @param name Name of node being read
900 * @param entry Place to put offset and size of this node
901 * @return 0 if ok, -ve on error
902 */
903int fdtdec_read_fmap_entry(const void *blob, int node, const char *name,
904 struct fmap_entry *entry)
905{
Simon Glassf5e141f2014-10-23 18:58:52 -0600906 const char *prop;
Simon Glass9f783722014-02-27 13:26:01 -0700907 u32 reg[2];
908
909 if (fdtdec_get_int_array(blob, node, "reg", reg, 2)) {
910 debug("Node '%s' has bad/missing 'reg' property\n", name);
911 return -FDT_ERR_NOTFOUND;
912 }
913 entry->offset = reg[0];
914 entry->length = reg[1];
Simon Glassf5e141f2014-10-23 18:58:52 -0600915 entry->used = fdtdec_get_int(blob, node, "used", entry->length);
916 prop = fdt_getprop(blob, node, "compress", NULL);
917 entry->compress_algo = prop && !strcmp(prop, "lzo") ?
918 FMAP_COMPRESS_LZO : FMAP_COMPRESS_NONE;
919 prop = fdt_getprop(blob, node, "hash", &entry->hash_size);
920 entry->hash_algo = prop ? FMAP_HASH_SHA256 : FMAP_HASH_NONE;
921 entry->hash = (uint8_t *)prop;
Simon Glass9f783722014-02-27 13:26:01 -0700922
923 return 0;
924}
Thierry Redinga937b802014-08-26 17:33:53 +0200925
926static u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells)
927{
928 u64 number = 0;
929
930 while (cells--)
931 number = (number << 32) | fdt32_to_cpu(*ptr++);
932
933 return number;
934}
935
936int fdt_get_resource(const void *fdt, int node, const char *property,
937 unsigned int index, struct fdt_resource *res)
938{
939 const fdt32_t *ptr, *end;
940 int na, ns, len, parent;
941 unsigned int i = 0;
942
943 parent = fdt_parent_offset(fdt, node);
944 if (parent < 0)
945 return parent;
946
947 na = fdt_address_cells(fdt, parent);
948 ns = fdt_size_cells(fdt, parent);
949
950 ptr = fdt_getprop(fdt, node, property, &len);
951 if (!ptr)
952 return len;
953
954 end = ptr + len / sizeof(*ptr);
955
956 while (ptr + na + ns <= end) {
957 if (i == index) {
958 res->start = res->end = fdtdec_get_number(ptr, na);
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
970int 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
976 index = fdt_find_string(fdt, node, prop_names, name);
977 if (index < 0)
978 return index;
979
980 return fdt_get_resource(fdt, node, property, index, res);
981}
Thierry Reding24856312014-08-26 17:33:54 +0200982
Simon Glass68941862014-10-23 18:58:56 -0600983int fdtdec_decode_memory_region(const void *blob, int config_node,
984 const char *mem_type, const char *suffix,
985 fdt_addr_t *basep, fdt_size_t *sizep)
986{
987 char prop_name[50];
988 const char *mem;
989 fdt_size_t size, offset_size;
990 fdt_addr_t base, offset;
991 int node;
992
993 if (config_node == -1) {
994 config_node = fdt_path_offset(blob, "/config");
995 if (config_node < 0) {
996 debug("%s: Cannot find /config node\n", __func__);
997 return -ENOENT;
998 }
999 }
1000 if (!suffix)
1001 suffix = "";
1002
1003 snprintf(prop_name, sizeof(prop_name), "%s-memory%s", mem_type,
1004 suffix);
1005 mem = fdt_getprop(blob, config_node, prop_name, NULL);
1006 if (!mem) {
1007 debug("%s: No memory type for '%s', using /memory\n", __func__,
1008 prop_name);
1009 mem = "/memory";
1010 }
1011
1012 node = fdt_path_offset(blob, mem);
1013 if (node < 0) {
1014 debug("%s: Failed to find node '%s': %s\n", __func__, mem,
1015 fdt_strerror(node));
1016 return -ENOENT;
1017 }
1018
1019 /*
1020 * Not strictly correct - the memory may have multiple banks. We just
1021 * use the first
1022 */
1023 if (fdtdec_decode_region(blob, node, "reg", &base, &size)) {
1024 debug("%s: Failed to decode memory region %s\n", __func__,
1025 mem);
1026 return -EINVAL;
1027 }
1028
1029 snprintf(prop_name, sizeof(prop_name), "%s-offset%s", mem_type,
1030 suffix);
1031 if (fdtdec_decode_region(blob, config_node, prop_name, &offset,
1032 &offset_size)) {
1033 debug("%s: Failed to decode memory region '%s'\n", __func__,
1034 prop_name);
1035 return -EINVAL;
1036 }
1037
1038 *basep = base + offset;
1039 *sizep = offset_size;
1040
1041 return 0;
1042}
Heiko Schocherd7b42322014-03-03 12:19:30 +01001043#endif