fdtdec: Implement fdtdec_add_reserved_memory()
[project/bcm63xx/u-boot.git] / include / fdtdec.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * Copyright (c) 2011 The Chromium OS Authors.
4 */
5
6 #ifndef __fdtdec_h
7 #define __fdtdec_h
8
9 /*
10 * This file contains convenience functions for decoding useful and
11 * enlightening information from FDTs. It is intended to be used by device
12 * drivers and board-specific code within U-Boot. It aims to reduce the
13 * amount of FDT munging required within U-Boot itself, so that driver code
14 * changes to support FDT are minimized.
15 */
16
17 #include <linux/libfdt.h>
18 #include <pci.h>
19
20 /*
21 * A typedef for a physical address. Note that fdt data is always big
22 * endian even on a litle endian machine.
23 */
24 typedef phys_addr_t fdt_addr_t;
25 typedef phys_size_t fdt_size_t;
26
27 static inline fdt32_t fdt_addr_unpack(fdt_addr_t addr, fdt32_t *upper)
28 {
29 if (upper)
30 #ifdef CONFIG_PHYS_64BIT
31 *upper = addr >> 32;
32 #else
33 *upper = 0;
34 #endif
35
36 return addr;
37 }
38
39 static inline fdt32_t fdt_size_unpack(fdt_size_t size, fdt32_t *upper)
40 {
41 if (upper)
42 #ifdef CONFIG_PHYS_64BIT
43 *upper = size >> 32;
44 #else
45 *upper = 0;
46 #endif
47
48 return size;
49 }
50
51 #ifdef CONFIG_PHYS_64BIT
52 #define FDT_ADDR_T_NONE (-1U)
53 #define fdt_addr_to_cpu(reg) be64_to_cpu(reg)
54 #define fdt_size_to_cpu(reg) be64_to_cpu(reg)
55 #define cpu_to_fdt_addr(reg) cpu_to_be64(reg)
56 #define cpu_to_fdt_size(reg) cpu_to_be64(reg)
57 typedef fdt64_t fdt_val_t;
58 #else
59 #define FDT_ADDR_T_NONE (-1U)
60 #define fdt_addr_to_cpu(reg) be32_to_cpu(reg)
61 #define fdt_size_to_cpu(reg) be32_to_cpu(reg)
62 #define cpu_to_fdt_addr(reg) cpu_to_be32(reg)
63 #define cpu_to_fdt_size(reg) cpu_to_be32(reg)
64 typedef fdt32_t fdt_val_t;
65 #endif
66
67 /* Information obtained about memory from the FDT */
68 struct fdt_memory {
69 fdt_addr_t start;
70 fdt_addr_t end;
71 };
72
73 struct bd_info;
74
75 #ifdef CONFIG_SPL_BUILD
76 #define SPL_BUILD 1
77 #else
78 #define SPL_BUILD 0
79 #endif
80
81 #if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
82 extern phys_addr_t prior_stage_fdt_address;
83 #endif
84
85 /*
86 * Information about a resource. start is the first address of the resource
87 * and end is the last address (inclusive). The length of the resource will
88 * be equal to: end - start + 1.
89 */
90 struct fdt_resource {
91 fdt_addr_t start;
92 fdt_addr_t end;
93 };
94
95 enum fdt_pci_space {
96 FDT_PCI_SPACE_CONFIG = 0,
97 FDT_PCI_SPACE_IO = 0x01000000,
98 FDT_PCI_SPACE_MEM32 = 0x02000000,
99 FDT_PCI_SPACE_MEM64 = 0x03000000,
100 FDT_PCI_SPACE_MEM32_PREF = 0x42000000,
101 FDT_PCI_SPACE_MEM64_PREF = 0x43000000,
102 };
103
104 #define FDT_PCI_ADDR_CELLS 3
105 #define FDT_PCI_SIZE_CELLS 2
106 #define FDT_PCI_REG_SIZE \
107 ((FDT_PCI_ADDR_CELLS + FDT_PCI_SIZE_CELLS) * sizeof(u32))
108
109 /*
110 * The Open Firmware spec defines PCI physical address as follows:
111 *
112 * bits# 31 .... 24 23 .... 16 15 .... 08 07 .... 00
113 *
114 * phys.hi cell: npt000ss bbbbbbbb dddddfff rrrrrrrr
115 * phys.mid cell: hhhhhhhh hhhhhhhh hhhhhhhh hhhhhhhh
116 * phys.lo cell: llllllll llllllll llllllll llllllll
117 *
118 * where:
119 *
120 * n: is 0 if the address is relocatable, 1 otherwise
121 * p: is 1 if addressable region is prefetchable, 0 otherwise
122 * t: is 1 if the address is aliased (for non-relocatable I/O) below 1MB
123 * (for Memory), or below 64KB (for relocatable I/O)
124 * ss: is the space code, denoting the address space
125 * bbbbbbbb: is the 8-bit Bus Number
126 * ddddd: is the 5-bit Device Number
127 * fff: is the 3-bit Function Number
128 * rrrrrrrr: is the 8-bit Register Number
129 * hhhhhhhh: is a 32-bit unsigned number
130 * llllllll: is a 32-bit unsigned number
131 */
132 struct fdt_pci_addr {
133 u32 phys_hi;
134 u32 phys_mid;
135 u32 phys_lo;
136 };
137
138 /**
139 * Compute the size of a resource.
140 *
141 * @param res the resource to operate on
142 * @return the size of the resource
143 */
144 static inline fdt_size_t fdt_resource_size(const struct fdt_resource *res)
145 {
146 return res->end - res->start + 1;
147 }
148
149 /**
150 * Compat types that we know about and for which we might have drivers.
151 * Each is named COMPAT_<dir>_<filename> where <dir> is the directory
152 * within drivers.
153 */
154 enum fdt_compat_id {
155 COMPAT_UNKNOWN,
156 COMPAT_NVIDIA_TEGRA20_EMC, /* Tegra20 memory controller */
157 COMPAT_NVIDIA_TEGRA20_EMC_TABLE, /* Tegra20 memory timing table */
158 COMPAT_NVIDIA_TEGRA20_NAND, /* Tegra2 NAND controller */
159 COMPAT_NVIDIA_TEGRA124_XUSB_PADCTL,
160 /* Tegra124 XUSB pad controller */
161 COMPAT_NVIDIA_TEGRA210_XUSB_PADCTL,
162 /* Tegra210 XUSB pad controller */
163 COMPAT_SMSC_LAN9215, /* SMSC 10/100 Ethernet LAN9215 */
164 COMPAT_SAMSUNG_EXYNOS5_SROMC, /* Exynos5 SROMC */
165 COMPAT_SAMSUNG_EXYNOS_USB_PHY, /* Exynos phy controller for usb2.0 */
166 COMPAT_SAMSUNG_EXYNOS5_USB3_PHY,/* Exynos phy controller for usb3.0 */
167 COMPAT_SAMSUNG_EXYNOS_TMU, /* Exynos TMU */
168 COMPAT_SAMSUNG_EXYNOS_MIPI_DSI, /* Exynos mipi dsi */
169 COMPAT_SAMSUNG_EXYNOS_DWMMC, /* Exynos DWMMC controller */
170 COMPAT_GENERIC_SPI_FLASH, /* Generic SPI Flash chip */
171 COMPAT_SAMSUNG_EXYNOS_SYSMMU, /* Exynos sysmmu */
172 COMPAT_INTEL_MICROCODE, /* Intel microcode update */
173 COMPAT_INTEL_QRK_MRC, /* Intel Quark MRC */
174 COMPAT_ALTERA_SOCFPGA_DWMAC, /* SoCFPGA Ethernet controller */
175 COMPAT_ALTERA_SOCFPGA_DWMMC, /* SoCFPGA DWMMC controller */
176 COMPAT_ALTERA_SOCFPGA_DWC2USB, /* SoCFPGA DWC2 USB controller */
177 COMPAT_INTEL_BAYTRAIL_FSP, /* Intel Bay Trail FSP */
178 COMPAT_INTEL_BAYTRAIL_FSP_MDP, /* Intel FSP memory-down params */
179 COMPAT_INTEL_IVYBRIDGE_FSP, /* Intel Ivy Bridge FSP */
180 COMPAT_SUNXI_NAND, /* SUNXI NAND controller */
181 COMPAT_ALTERA_SOCFPGA_CLK, /* SoCFPGA Clock initialization */
182 COMPAT_ALTERA_SOCFPGA_PINCTRL_SINGLE, /* SoCFPGA pinctrl-single */
183 COMPAT_ALTERA_SOCFPGA_H2F_BRG, /* SoCFPGA hps2fpga bridge */
184 COMPAT_ALTERA_SOCFPGA_LWH2F_BRG, /* SoCFPGA lwhps2fpga bridge */
185 COMPAT_ALTERA_SOCFPGA_F2H_BRG, /* SoCFPGA fpga2hps bridge */
186 COMPAT_ALTERA_SOCFPGA_F2SDR0, /* SoCFPGA fpga2SDRAM0 bridge */
187 COMPAT_ALTERA_SOCFPGA_F2SDR1, /* SoCFPGA fpga2SDRAM1 bridge */
188 COMPAT_ALTERA_SOCFPGA_F2SDR2, /* SoCFPGA fpga2SDRAM2 bridge */
189 COMPAT_ALTERA_SOCFPGA_FPGA0, /* SOCFPGA FPGA manager */
190 COMPAT_ALTERA_SOCFPGA_NOC, /* SOCFPGA Arria 10 NOC */
191 COMPAT_ALTERA_SOCFPGA_CLK_INIT, /* SOCFPGA Arria 10 clk init */
192
193 COMPAT_COUNT,
194 };
195
196 #define MAX_PHANDLE_ARGS 16
197 struct fdtdec_phandle_args {
198 int node;
199 int args_count;
200 uint32_t args[MAX_PHANDLE_ARGS];
201 };
202
203 /**
204 * fdtdec_parse_phandle_with_args() - Find a node pointed by phandle in a list
205 *
206 * This function is useful to parse lists of phandles and their arguments.
207 *
208 * Example:
209 *
210 * phandle1: node1 {
211 * #list-cells = <2>;
212 * }
213 *
214 * phandle2: node2 {
215 * #list-cells = <1>;
216 * }
217 *
218 * node3 {
219 * list = <&phandle1 1 2 &phandle2 3>;
220 * }
221 *
222 * To get a device_node of the `node2' node you may call this:
223 * fdtdec_parse_phandle_with_args(blob, node3, "list", "#list-cells", 0, 1,
224 * &args);
225 *
226 * (This function is a modified version of __of_parse_phandle_with_args() from
227 * Linux 3.18)
228 *
229 * @blob: Pointer to device tree
230 * @src_node: Offset of device tree node containing a list
231 * @list_name: property name that contains a list
232 * @cells_name: property name that specifies the phandles' arguments count,
233 * or NULL to use @cells_count
234 * @cells_count: Cell count to use if @cells_name is NULL
235 * @index: index of a phandle to parse out
236 * @out_args: optional pointer to output arguments structure (will be filled)
237 * @return 0 on success (with @out_args filled out if not NULL), -ENOENT if
238 * @list_name does not exist, a phandle was not found, @cells_name
239 * could not be found, the arguments were truncated or there were too
240 * many arguments.
241 *
242 */
243 int fdtdec_parse_phandle_with_args(const void *blob, int src_node,
244 const char *list_name,
245 const char *cells_name,
246 int cell_count, int index,
247 struct fdtdec_phandle_args *out_args);
248
249 /**
250 * Find the next numbered alias for a peripheral. This is used to enumerate
251 * all the peripherals of a certain type.
252 *
253 * Do the first call with *upto = 0. Assuming /aliases/<name>0 exists then
254 * this function will return a pointer to the node the alias points to, and
255 * then update *upto to 1. Next time you call this function, the next node
256 * will be returned.
257 *
258 * All nodes returned will match the compatible ID, as it is assumed that
259 * all peripherals use the same driver.
260 *
261 * @param blob FDT blob to use
262 * @param name Root name of alias to search for
263 * @param id Compatible ID to look for
264 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
265 */
266 int fdtdec_next_alias(const void *blob, const char *name,
267 enum fdt_compat_id id, int *upto);
268
269 /**
270 * Find the compatible ID for a given node.
271 *
272 * Generally each node has at least one compatible string attached to it.
273 * This function looks through our list of known compatible strings and
274 * returns the corresponding ID which matches the compatible string.
275 *
276 * @param blob FDT blob to use
277 * @param node Node containing compatible string to find
278 * @return compatible ID, or COMPAT_UNKNOWN if we cannot find a match
279 */
280 enum fdt_compat_id fdtdec_lookup(const void *blob, int node);
281
282 /**
283 * Find the next compatible node for a peripheral.
284 *
285 * Do the first call with node = 0. This function will return a pointer to
286 * the next compatible node. Next time you call this function, pass the
287 * value returned, and the next node will be provided.
288 *
289 * @param blob FDT blob to use
290 * @param node Start node for search
291 * @param id Compatible ID to look for (enum fdt_compat_id)
292 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
293 */
294 int fdtdec_next_compatible(const void *blob, int node,
295 enum fdt_compat_id id);
296
297 /**
298 * Find the next compatible subnode for a peripheral.
299 *
300 * Do the first call with node set to the parent and depth = 0. This
301 * function will return the offset of the next compatible node. Next time
302 * you call this function, pass the node value returned last time, with
303 * depth unchanged, and the next node will be provided.
304 *
305 * @param blob FDT blob to use
306 * @param node Start node for search
307 * @param id Compatible ID to look for (enum fdt_compat_id)
308 * @param depthp Current depth (set to 0 before first call)
309 * @return offset of next compatible node, or -FDT_ERR_NOTFOUND if no more
310 */
311 int fdtdec_next_compatible_subnode(const void *blob, int node,
312 enum fdt_compat_id id, int *depthp);
313
314 /*
315 * Look up an address property in a node and return the parsed address, and
316 * optionally the parsed size.
317 *
318 * This variant assumes a known and fixed number of cells are used to
319 * represent the address and size.
320 *
321 * You probably don't want to use this function directly except to parse
322 * non-standard properties, and never to parse the "reg" property. Instead,
323 * use one of the "auto" variants below, which automatically honor the
324 * #address-cells and #size-cells properties in the parent node.
325 *
326 * @param blob FDT blob
327 * @param node node to examine
328 * @param prop_name name of property to find
329 * @param index which address to retrieve from a list of addresses. Often 0.
330 * @param na the number of cells used to represent an address
331 * @param ns the number of cells used to represent a size
332 * @param sizep a pointer to store the size into. Use NULL if not required
333 * @param translate Indicates whether to translate the returned value
334 * using the parent node's ranges property.
335 * @return address, if found, or FDT_ADDR_T_NONE if not
336 */
337 fdt_addr_t fdtdec_get_addr_size_fixed(const void *blob, int node,
338 const char *prop_name, int index, int na, int ns,
339 fdt_size_t *sizep, bool translate);
340
341 /*
342 * Look up an address property in a node and return the parsed address, and
343 * optionally the parsed size.
344 *
345 * This variant automatically determines the number of cells used to represent
346 * the address and size by parsing the provided parent node's #address-cells
347 * and #size-cells properties.
348 *
349 * @param blob FDT blob
350 * @param parent parent node of @node
351 * @param node node to examine
352 * @param prop_name name of property to find
353 * @param index which address to retrieve from a list of addresses. Often 0.
354 * @param sizep a pointer to store the size into. Use NULL if not required
355 * @param translate Indicates whether to translate the returned value
356 * using the parent node's ranges property.
357 * @return address, if found, or FDT_ADDR_T_NONE if not
358 */
359 fdt_addr_t fdtdec_get_addr_size_auto_parent(const void *blob, int parent,
360 int node, const char *prop_name, int index, fdt_size_t *sizep,
361 bool translate);
362
363 /*
364 * Look up an address property in a node and return the parsed address, and
365 * optionally the parsed size.
366 *
367 * This variant automatically determines the number of cells used to represent
368 * the address and size by parsing the parent node's #address-cells
369 * and #size-cells properties. The parent node is automatically found.
370 *
371 * The automatic parent lookup implemented by this function is slow.
372 * Consequently, fdtdec_get_addr_size_auto_parent() should be used where
373 * possible.
374 *
375 * @param blob FDT blob
376 * @param parent parent node of @node
377 * @param node node to examine
378 * @param prop_name name of property to find
379 * @param index which address to retrieve from a list of addresses. Often 0.
380 * @param sizep a pointer to store the size into. Use NULL if not required
381 * @param translate Indicates whether to translate the returned value
382 * using the parent node's ranges property.
383 * @return address, if found, or FDT_ADDR_T_NONE if not
384 */
385 fdt_addr_t fdtdec_get_addr_size_auto_noparent(const void *blob, int node,
386 const char *prop_name, int index, fdt_size_t *sizep,
387 bool translate);
388
389 /*
390 * Look up an address property in a node and return the parsed address.
391 *
392 * This variant hard-codes the number of cells used to represent the address
393 * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
394 * always returns the first address value in the property (index 0).
395 *
396 * Use of this function is not recommended due to the hard-coding of cell
397 * counts. There is no programmatic validation that these hard-coded values
398 * actually match the device tree content in any way at all. This assumption
399 * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
400 * set in the U-Boot build and exercising strict control over DT content to
401 * ensure use of matching #address-cells/#size-cells properties. However, this
402 * approach is error-prone; those familiar with DT will not expect the
403 * assumption to exist, and could easily invalidate it. If the assumption is
404 * invalidated, this function will not report the issue, and debugging will
405 * be required. Instead, use fdtdec_get_addr_size_auto_parent().
406 *
407 * @param blob FDT blob
408 * @param node node to examine
409 * @param prop_name name of property to find
410 * @return address, if found, or FDT_ADDR_T_NONE if not
411 */
412 fdt_addr_t fdtdec_get_addr(const void *blob, int node,
413 const char *prop_name);
414
415 /*
416 * Look up an address property in a node and return the parsed address, and
417 * optionally the parsed size.
418 *
419 * This variant hard-codes the number of cells used to represent the address
420 * and size based on sizeof(fdt_addr_t) and sizeof(fdt_size_t). It also
421 * always returns the first address value in the property (index 0).
422 *
423 * Use of this function is not recommended due to the hard-coding of cell
424 * counts. There is no programmatic validation that these hard-coded values
425 * actually match the device tree content in any way at all. This assumption
426 * can be satisfied by manually ensuring CONFIG_PHYS_64BIT is appropriately
427 * set in the U-Boot build and exercising strict control over DT content to
428 * ensure use of matching #address-cells/#size-cells properties. However, this
429 * approach is error-prone; those familiar with DT will not expect the
430 * assumption to exist, and could easily invalidate it. If the assumption is
431 * invalidated, this function will not report the issue, and debugging will
432 * be required. Instead, use fdtdec_get_addr_size_auto_parent().
433 *
434 * @param blob FDT blob
435 * @param node node to examine
436 * @param prop_name name of property to find
437 * @param sizep a pointer to store the size into. Use NULL if not required
438 * @return address, if found, or FDT_ADDR_T_NONE if not
439 */
440 fdt_addr_t fdtdec_get_addr_size(const void *blob, int node,
441 const char *prop_name, fdt_size_t *sizep);
442
443 /**
444 * Look at an address property in a node and return the pci address which
445 * corresponds to the given type in the form of fdt_pci_addr.
446 * The property must hold one fdt_pci_addr with a lengh.
447 *
448 * @param blob FDT blob
449 * @param node node to examine
450 * @param type pci address type (FDT_PCI_SPACE_xxx)
451 * @param prop_name name of property to find
452 * @param addr returns pci address in the form of fdt_pci_addr
453 * @return 0 if ok, -ENOENT if the property did not exist, -EINVAL if the
454 * format of the property was invalid, -ENXIO if the requested
455 * address type was not found
456 */
457 int fdtdec_get_pci_addr(const void *blob, int node, enum fdt_pci_space type,
458 const char *prop_name, struct fdt_pci_addr *addr);
459
460 /**
461 * Look at the compatible property of a device node that represents a PCI
462 * device and extract pci vendor id and device id from it.
463 *
464 * @param blob FDT blob
465 * @param node node to examine
466 * @param vendor vendor id of the pci device
467 * @param device device id of the pci device
468 * @return 0 if ok, negative on error
469 */
470 int fdtdec_get_pci_vendev(const void *blob, int node,
471 u16 *vendor, u16 *device);
472
473 /**
474 * Look at the pci address of a device node that represents a PCI device
475 * and return base address of the pci device's registers.
476 *
477 * @param dev device to examine
478 * @param addr pci address in the form of fdt_pci_addr
479 * @param bar returns base address of the pci device's registers
480 * @return 0 if ok, negative on error
481 */
482 int fdtdec_get_pci_bar32(struct udevice *dev, struct fdt_pci_addr *addr,
483 u32 *bar);
484
485 /**
486 * Look up a 32-bit integer property in a node and return it. The property
487 * must have at least 4 bytes of data. The value of the first cell is
488 * returned.
489 *
490 * @param blob FDT blob
491 * @param node node to examine
492 * @param prop_name name of property to find
493 * @param default_val default value to return if the property is not found
494 * @return integer value, if found, or default_val if not
495 */
496 s32 fdtdec_get_int(const void *blob, int node, const char *prop_name,
497 s32 default_val);
498
499 /**
500 * Unsigned version of fdtdec_get_int. The property must have at least
501 * 4 bytes of data. The value of the first cell is returned.
502 *
503 * @param blob FDT blob
504 * @param node node to examine
505 * @param prop_name name of property to find
506 * @param default_val default value to return if the property is not found
507 * @return unsigned integer value, if found, or default_val if not
508 */
509 unsigned int fdtdec_get_uint(const void *blob, int node, const char *prop_name,
510 unsigned int default_val);
511
512 /**
513 * Get a variable-sized number from a property
514 *
515 * This reads a number from one or more cells.
516 *
517 * @param ptr Pointer to property
518 * @param cells Number of cells containing the number
519 * @return the value in the cells
520 */
521 u64 fdtdec_get_number(const fdt32_t *ptr, unsigned int cells);
522
523 /**
524 * Look up a 64-bit integer property in a node and return it. The property
525 * must have at least 8 bytes of data (2 cells). The first two cells are
526 * concatenated to form a 8 bytes value, where the first cell is top half and
527 * the second cell is bottom half.
528 *
529 * @param blob FDT blob
530 * @param node node to examine
531 * @param prop_name name of property to find
532 * @param default_val default value to return if the property is not found
533 * @return integer value, if found, or default_val if not
534 */
535 uint64_t fdtdec_get_uint64(const void *blob, int node, const char *prop_name,
536 uint64_t default_val);
537
538 /**
539 * Checks whether a node is enabled.
540 * This looks for a 'status' property. If this exists, then returns 1 if
541 * the status is 'ok' and 0 otherwise. If there is no status property,
542 * it returns 1 on the assumption that anything mentioned should be enabled
543 * by default.
544 *
545 * @param blob FDT blob
546 * @param node node to examine
547 * @return integer value 0 (not enabled) or 1 (enabled)
548 */
549 int fdtdec_get_is_enabled(const void *blob, int node);
550
551 /**
552 * Make sure we have a valid fdt available to control U-Boot.
553 *
554 * If not, a message is printed to the console if the console is ready.
555 *
556 * @return 0 if all ok, -1 if not
557 */
558 int fdtdec_prepare_fdt(void);
559
560 /**
561 * Checks that we have a valid fdt available to control U-Boot.
562
563 * However, if not then for the moment nothing is done, since this function
564 * is called too early to panic().
565 *
566 * @returns 0
567 */
568 int fdtdec_check_fdt(void);
569
570 /**
571 * Find the nodes for a peripheral and return a list of them in the correct
572 * order. This is used to enumerate all the peripherals of a certain type.
573 *
574 * To use this, optionally set up a /aliases node with alias properties for
575 * a peripheral. For example, for usb you could have:
576 *
577 * aliases {
578 * usb0 = "/ehci@c5008000";
579 * usb1 = "/ehci@c5000000";
580 * };
581 *
582 * Pass "usb" as the name to this function and will return a list of two
583 * nodes offsets: /ehci@c5008000 and ehci@c5000000.
584 *
585 * All nodes returned will match the compatible ID, as it is assumed that
586 * all peripherals use the same driver.
587 *
588 * If no alias node is found, then the node list will be returned in the
589 * order found in the fdt. If the aliases mention a node which doesn't
590 * exist, then this will be ignored. If nodes are found with no aliases,
591 * they will be added in any order.
592 *
593 * If there is a gap in the aliases, then this function return a 0 node at
594 * that position. The return value will also count these gaps.
595 *
596 * This function checks node properties and will not return nodes which are
597 * marked disabled (status = "disabled").
598 *
599 * @param blob FDT blob to use
600 * @param name Root name of alias to search for
601 * @param id Compatible ID to look for
602 * @param node_list Place to put list of found nodes
603 * @param maxcount Maximum number of nodes to find
604 * @return number of nodes found on success, FDT_ERR_... on error
605 */
606 int fdtdec_find_aliases_for_id(const void *blob, const char *name,
607 enum fdt_compat_id id, int *node_list, int maxcount);
608
609 /*
610 * This function is similar to fdtdec_find_aliases_for_id() except that it
611 * adds to the node_list that is passed in. Any 0 elements are considered
612 * available for allocation - others are considered already used and are
613 * skipped.
614 *
615 * You can use this by calling fdtdec_find_aliases_for_id() with an
616 * uninitialised array, then setting the elements that are returned to -1,
617 * say, then calling this function, perhaps with a different compat id.
618 * Any elements you get back that are >0 are new nodes added by the call
619 * to this function.
620 *
621 * Note that if you have some nodes with aliases and some without, you are
622 * sailing close to the wind. The call to fdtdec_find_aliases_for_id() with
623 * one compat_id may fill in positions for which you have aliases defined
624 * for another compat_id. When you later call *this* function with the second
625 * compat_id, the alias positions may already be used. A debug warning may
626 * be generated in this case, but it is safest to define aliases for all
627 * nodes when you care about the ordering.
628 */
629 int fdtdec_add_aliases_for_id(const void *blob, const char *name,
630 enum fdt_compat_id id, int *node_list, int maxcount);
631
632 /**
633 * Get the alias sequence number of a node
634 *
635 * This works out whether a node is pointed to by an alias, and if so, the
636 * sequence number of that alias. Aliases are of the form <base><num> where
637 * <num> is the sequence number. For example spi2 would be sequence number
638 * 2.
639 *
640 * @param blob Device tree blob (if NULL, then error is returned)
641 * @param base Base name for alias (before the underscore)
642 * @param node Node to look up
643 * @param seqp This is set to the sequence number if one is found,
644 * but otherwise the value is left alone
645 * @return 0 if a sequence was found, -ve if not
646 */
647 int fdtdec_get_alias_seq(const void *blob, const char *base, int node,
648 int *seqp);
649
650 /**
651 * Get the highest alias number for susbystem.
652 *
653 * It parses all aliases and find out highest recorded alias for subsystem.
654 * Aliases are of the form <base><num> where <num> is the sequence number.
655 *
656 * @param blob Device tree blob (if NULL, then error is returned)
657 * @param base Base name for alias susbystem (before the number)
658 *
659 * @return 0 highest alias ID, -1 if not found
660 */
661 int fdtdec_get_alias_highest_id(const void *blob, const char *base);
662
663 /**
664 * Get a property from the /chosen node
665 *
666 * @param blob Device tree blob (if NULL, then NULL is returned)
667 * @param name Property name to look up
668 * @return Value of property, or NULL if it does not exist
669 */
670 const char *fdtdec_get_chosen_prop(const void *blob, const char *name);
671
672 /**
673 * Get the offset of the given /chosen node
674 *
675 * This looks up a property in /chosen containing the path to another node,
676 * then finds the offset of that node.
677 *
678 * @param blob Device tree blob (if NULL, then error is returned)
679 * @param name Property name, e.g. "stdout-path"
680 * @return Node offset referred to by that chosen node, or -ve FDT_ERR_...
681 */
682 int fdtdec_get_chosen_node(const void *blob, const char *name);
683
684 /*
685 * Get the name for a compatible ID
686 *
687 * @param id Compatible ID to look for
688 * @return compatible string for that id
689 */
690 const char *fdtdec_get_compatible(enum fdt_compat_id id);
691
692 /* Look up a phandle and follow it to its node. Then return the offset
693 * of that node.
694 *
695 * @param blob FDT blob
696 * @param node node to examine
697 * @param prop_name name of property to find
698 * @return node offset if found, -ve error code on error
699 */
700 int fdtdec_lookup_phandle(const void *blob, int node, const char *prop_name);
701
702 /**
703 * Look up a property in a node and return its contents in an integer
704 * array of given length. The property must have at least enough data for
705 * the array (4*count bytes). It may have more, but this will be ignored.
706 *
707 * @param blob FDT blob
708 * @param node node to examine
709 * @param prop_name name of property to find
710 * @param array array to fill with data
711 * @param count number of array elements
712 * @return 0 if ok, or -FDT_ERR_NOTFOUND if the property is not found,
713 * or -FDT_ERR_BADLAYOUT if not enough data
714 */
715 int fdtdec_get_int_array(const void *blob, int node, const char *prop_name,
716 u32 *array, int count);
717
718 /**
719 * Look up a property in a node and return its contents in an integer
720 * array of given length. The property must exist but may have less data that
721 * expected (4*count bytes). It may have more, but this will be ignored.
722 *
723 * @param blob FDT blob
724 * @param node node to examine
725 * @param prop_name name of property to find
726 * @param array array to fill with data
727 * @param count number of array elements
728 * @return number of array elements if ok, or -FDT_ERR_NOTFOUND if the
729 * property is not found
730 */
731 int fdtdec_get_int_array_count(const void *blob, int node,
732 const char *prop_name, u32 *array, int count);
733
734 /**
735 * Look up a property in a node and return a pointer to its contents as a
736 * unsigned int array of given length. The property must have at least enough
737 * data for the array ('count' cells). It may have more, but this will be
738 * ignored. The data is not copied.
739 *
740 * Note that you must access elements of the array with fdt32_to_cpu(),
741 * since the elements will be big endian even on a little endian machine.
742 *
743 * @param blob FDT blob
744 * @param node node to examine
745 * @param prop_name name of property to find
746 * @param count number of array elements
747 * @return pointer to array if found, or NULL if the property is not
748 * found or there is not enough data
749 */
750 const u32 *fdtdec_locate_array(const void *blob, int node,
751 const char *prop_name, int count);
752
753 /**
754 * Look up a boolean property in a node and return it.
755 *
756 * A boolean properly is true if present in the device tree and false if not
757 * present, regardless of its value.
758 *
759 * @param blob FDT blob
760 * @param node node to examine
761 * @param prop_name name of property to find
762 * @return 1 if the properly is present; 0 if it isn't present
763 */
764 int fdtdec_get_bool(const void *blob, int node, const char *prop_name);
765
766 /*
767 * Count child nodes of one parent node.
768 *
769 * @param blob FDT blob
770 * @param node parent node
771 * @return number of child node; 0 if there is not child node
772 */
773 int fdtdec_get_child_count(const void *blob, int node);
774
775 /**
776 * Look in the FDT for a config item with the given name and return its value
777 * as a 32-bit integer. The property must have at least 4 bytes of data. The
778 * value of the first cell is returned.
779 *
780 * @param blob FDT blob to use
781 * @param prop_name Node property name
782 * @param default_val default value to return if the property is not found
783 * @return integer value, if found, or default_val if not
784 */
785 int fdtdec_get_config_int(const void *blob, const char *prop_name,
786 int default_val);
787
788 /**
789 * Look in the FDT for a config item with the given name
790 * and return whether it exists.
791 *
792 * @param blob FDT blob
793 * @param prop_name property name to look up
794 * @return 1, if it exists, or 0 if not
795 */
796 int fdtdec_get_config_bool(const void *blob, const char *prop_name);
797
798 /**
799 * Look in the FDT for a config item with the given name and return its value
800 * as a string.
801 *
802 * @param blob FDT blob
803 * @param prop_name property name to look up
804 * @returns property string, NULL on error.
805 */
806 char *fdtdec_get_config_string(const void *blob, const char *prop_name);
807
808 /*
809 * Look up a property in a node and return its contents in a byte
810 * array of given length. The property must have at least enough data for
811 * the array (count bytes). It may have more, but this will be ignored.
812 *
813 * @param blob FDT blob
814 * @param node node to examine
815 * @param prop_name name of property to find
816 * @param array array to fill with data
817 * @param count number of array elements
818 * @return 0 if ok, or -FDT_ERR_MISSING if the property is not found,
819 * or -FDT_ERR_BADLAYOUT if not enough data
820 */
821 int fdtdec_get_byte_array(const void *blob, int node, const char *prop_name,
822 u8 *array, int count);
823
824 /**
825 * Look up a property in a node and return a pointer to its contents as a
826 * byte array of given length. The property must have at least enough data
827 * for the array (count bytes). It may have more, but this will be ignored.
828 * The data is not copied.
829 *
830 * @param blob FDT blob
831 * @param node node to examine
832 * @param prop_name name of property to find
833 * @param count number of array elements
834 * @return pointer to byte array if found, or NULL if the property is not
835 * found or there is not enough data
836 */
837 const u8 *fdtdec_locate_byte_array(const void *blob, int node,
838 const char *prop_name, int count);
839
840 /**
841 * Obtain an indexed resource from a device property.
842 *
843 * @param fdt FDT blob
844 * @param node node to examine
845 * @param property name of the property to parse
846 * @param index index of the resource to retrieve
847 * @param res returns the resource
848 * @return 0 if ok, negative on error
849 */
850 int fdt_get_resource(const void *fdt, int node, const char *property,
851 unsigned int index, struct fdt_resource *res);
852
853 /**
854 * Obtain a named resource from a device property.
855 *
856 * Look up the index of the name in a list of strings and return the resource
857 * at that index.
858 *
859 * @param fdt FDT blob
860 * @param node node to examine
861 * @param property name of the property to parse
862 * @param prop_names name of the property containing the list of names
863 * @param name the name of the entry to look up
864 * @param res returns the resource
865 */
866 int fdt_get_named_resource(const void *fdt, int node, const char *property,
867 const char *prop_names, const char *name,
868 struct fdt_resource *res);
869
870 /* Display timings from linux include/video/display_timing.h */
871 enum display_flags {
872 DISPLAY_FLAGS_HSYNC_LOW = 1 << 0,
873 DISPLAY_FLAGS_HSYNC_HIGH = 1 << 1,
874 DISPLAY_FLAGS_VSYNC_LOW = 1 << 2,
875 DISPLAY_FLAGS_VSYNC_HIGH = 1 << 3,
876
877 /* data enable flag */
878 DISPLAY_FLAGS_DE_LOW = 1 << 4,
879 DISPLAY_FLAGS_DE_HIGH = 1 << 5,
880 /* drive data on pos. edge */
881 DISPLAY_FLAGS_PIXDATA_POSEDGE = 1 << 6,
882 /* drive data on neg. edge */
883 DISPLAY_FLAGS_PIXDATA_NEGEDGE = 1 << 7,
884 DISPLAY_FLAGS_INTERLACED = 1 << 8,
885 DISPLAY_FLAGS_DOUBLESCAN = 1 << 9,
886 DISPLAY_FLAGS_DOUBLECLK = 1 << 10,
887 };
888
889 /*
890 * A single signal can be specified via a range of minimal and maximal values
891 * with a typical value, that lies somewhere inbetween.
892 */
893 struct timing_entry {
894 u32 min;
895 u32 typ;
896 u32 max;
897 };
898
899 /*
900 * Single "mode" entry. This describes one set of signal timings a display can
901 * have in one setting. This struct can later be converted to struct videomode
902 * (see include/video/videomode.h). As each timing_entry can be defined as a
903 * range, one struct display_timing may become multiple struct videomodes.
904 *
905 * Example: hsync active high, vsync active low
906 *
907 * Active Video
908 * Video ______________________XXXXXXXXXXXXXXXXXXXXXX_____________________
909 * |<- sync ->|<- back ->|<----- active ----->|<- front ->|<- sync..
910 * | | porch | | porch |
911 *
912 * HSync _|¯¯¯¯¯¯¯¯¯¯|___________________________________________|¯¯¯¯¯¯¯¯¯
913 *
914 * VSync ¯|__________|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|_________
915 */
916 struct display_timing {
917 struct timing_entry pixelclock;
918
919 struct timing_entry hactive; /* hor. active video */
920 struct timing_entry hfront_porch; /* hor. front porch */
921 struct timing_entry hback_porch; /* hor. back porch */
922 struct timing_entry hsync_len; /* hor. sync len */
923
924 struct timing_entry vactive; /* ver. active video */
925 struct timing_entry vfront_porch; /* ver. front porch */
926 struct timing_entry vback_porch; /* ver. back porch */
927 struct timing_entry vsync_len; /* ver. sync len */
928
929 enum display_flags flags; /* display flags */
930 bool hdmi_monitor; /* is hdmi monitor? */
931 };
932
933 /**
934 * fdtdec_decode_display_timing() - decode display timings
935 *
936 * Decode display timings from the supplied 'display-timings' node.
937 * See doc/device-tree-bindings/video/display-timing.txt for binding
938 * information.
939 *
940 * @param blob FDT blob
941 * @param node 'display-timing' node containing the timing subnodes
942 * @param index Index number to read (0=first timing subnode)
943 * @param config Place to put timings
944 * @return 0 if OK, -FDT_ERR_NOTFOUND if not found
945 */
946 int fdtdec_decode_display_timing(const void *blob, int node, int index,
947 struct display_timing *config);
948
949 /**
950 * fdtdec_setup_mem_size_base_fdt() - decode and setup gd->ram_size and
951 * gd->ram_start
952 *
953 * Decode the /memory 'reg' property to determine the size and start of the
954 * first memory bank, populate the global data with the size and start of the
955 * first bank of memory.
956 *
957 * This function should be called from a boards dram_init(). This helper
958 * function allows for boards to query the device tree for DRAM size and start
959 * address instead of hard coding the value in the case where the memory size
960 * and start address cannot be detected automatically.
961 *
962 * @param blob FDT blob
963 *
964 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
965 * invalid
966 */
967 int fdtdec_setup_mem_size_base_fdt(const void *blob);
968
969 /**
970 * fdtdec_setup_mem_size_base() - decode and setup gd->ram_size and
971 * gd->ram_start
972 *
973 * Decode the /memory 'reg' property to determine the size and start of the
974 * first memory bank, populate the global data with the size and start of the
975 * first bank of memory.
976 *
977 * This function should be called from a boards dram_init(). This helper
978 * function allows for boards to query the device tree for DRAM size and start
979 * address instead of hard coding the value in the case where the memory size
980 * and start address cannot be detected automatically.
981 *
982 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
983 * invalid
984 */
985 int fdtdec_setup_mem_size_base(void);
986
987 /**
988 * fdtdec_setup_memory_banksize_fdt() - decode and populate gd->bd->bi_dram
989 *
990 * Decode the /memory 'reg' property to determine the address and size of the
991 * memory banks. Use this data to populate the global data board info with the
992 * phys address and size of memory banks.
993 *
994 * This function should be called from a boards dram_init_banksize(). This
995 * helper function allows for boards to query the device tree for memory bank
996 * information instead of hard coding the information in cases where it cannot
997 * be detected automatically.
998 *
999 * @param blob FDT blob
1000 *
1001 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
1002 * invalid
1003 */
1004 int fdtdec_setup_memory_banksize_fdt(const void *blob);
1005
1006 /**
1007 * fdtdec_setup_memory_banksize() - decode and populate gd->bd->bi_dram
1008 *
1009 * Decode the /memory 'reg' property to determine the address and size of the
1010 * memory banks. Use this data to populate the global data board info with the
1011 * phys address and size of memory banks.
1012 *
1013 * This function should be called from a boards dram_init_banksize(). This
1014 * helper function allows for boards to query the device tree for memory bank
1015 * information instead of hard coding the information in cases where it cannot
1016 * be detected automatically.
1017 *
1018 * @return 0 if OK, -EINVAL if the /memory node or reg property is missing or
1019 * invalid
1020 */
1021 int fdtdec_setup_memory_banksize(void);
1022
1023 /**
1024 * fdtdec_set_phandle() - sets the phandle of a given node
1025 *
1026 * @param blob FDT blob
1027 * @param node offset in the FDT blob of the node whose phandle is to
1028 * be set
1029 * @param phandle phandle to set for the given node
1030 * @return 0 on success or a negative error code on failure
1031 */
1032 int fdtdec_set_phandle(void *blob, int node, uint32_t phandle);
1033
1034 /**
1035 * fdtdec_add_reserved_memory() - add or find a reserved-memory node
1036 *
1037 * If a reserved-memory node already exists for the given carveout, a phandle
1038 * for that node will be returned. Otherwise a new node will be created and a
1039 * phandle corresponding to it will be returned.
1040 *
1041 * See Documentation/devicetree/bindings/reserved-memory/reserved-memory.txt
1042 * for details on how to use reserved memory regions.
1043 *
1044 * As an example, consider the following code snippet:
1045 *
1046 * struct fdt_memory fb = {
1047 * .start = 0x92cb3000,
1048 * .end = 0x934b2fff,
1049 * };
1050 * uint32_t phandle;
1051 *
1052 * fdtdec_add_reserved_memory(fdt, "framebuffer", &fb, &phandle);
1053 *
1054 * This results in the following subnode being added to the top-level
1055 * /reserved-memory node:
1056 *
1057 * reserved-memory {
1058 * #address-cells = <0x00000002>;
1059 * #size-cells = <0x00000002>;
1060 * ranges;
1061 *
1062 * framebuffer@92cb3000 {
1063 * reg = <0x00000000 0x92cb3000 0x00000000 0x00800000>;
1064 * phandle = <0x0000004d>;
1065 * };
1066 * };
1067 *
1068 * If the top-level /reserved-memory node does not exist, it will be created.
1069 * The phandle returned from the function call can be used to reference this
1070 * reserved memory region from other nodes.
1071 *
1072 * @param blob FDT blob
1073 * @param basename base name of the node to create
1074 * @param carveout information about the carveout region
1075 * @param phandlep return location for the phandle of the carveout region
1076 * @return 0 on success or a negative error code on failure
1077 */
1078 int fdtdec_add_reserved_memory(void *blob, const char *basename,
1079 const struct fdt_memory *carveout,
1080 uint32_t *phandlep);
1081
1082 /**
1083 * Set up the device tree ready for use
1084 */
1085 int fdtdec_setup(void);
1086
1087 #if CONFIG_IS_ENABLED(MULTI_DTB_FIT)
1088 /**
1089 * fdtdec_resetup() - Set up the device tree again
1090 *
1091 * The main difference with fdtdec_setup() is that it returns if the fdt has
1092 * changed because a better match has been found.
1093 * This is typically used for boards that rely on a DM driver to detect the
1094 * board type. This function sould be called by the board code after the stuff
1095 * needed by board_fit_config_name_match() to operate porperly is available.
1096 * If this functions signals that a rescan is necessary, the board code must
1097 * unbind all the drivers using dm_uninit() and then rescan the DT with
1098 * dm_init_and_scan().
1099 *
1100 * @param rescan Returns a flag indicating that fdt has changed and rescanning
1101 * the fdt is required
1102 *
1103 * @return 0 if OK, -ve on error
1104 */
1105 int fdtdec_resetup(int *rescan);
1106 #endif
1107
1108 /**
1109 * Board-specific FDT initialization. Returns the address to a device tree blob.
1110 * Called when CONFIG_OF_BOARD is defined, or if CONFIG_OF_SEPARATE is defined
1111 * and the board implements it.
1112 */
1113 void *board_fdt_blob_setup(void);
1114
1115 /*
1116 * Decode the size of memory
1117 *
1118 * RAM size is normally set in a /memory node and consists of a list of
1119 * (base, size) cells in the 'reg' property. This information is used to
1120 * determine the total available memory as well as the address and size
1121 * of each bank.
1122 *
1123 * Optionally the memory configuration can vary depending on a board id,
1124 * typically read from strapping resistors or an EEPROM on the board.
1125 *
1126 * Finally, memory size can be detected (within certain limits) by probing
1127 * the available memory. It is safe to do so within the limits provides by
1128 * the board's device tree information. This makes it possible to produce
1129 * boards with different memory sizes, where the device tree specifies the
1130 * maximum memory configuration, and the smaller memory configuration is
1131 * probed.
1132 *
1133 * This function decodes that information, returning the memory base address,
1134 * size and bank information. See the memory.txt binding for full
1135 * documentation.
1136 *
1137 * @param blob Device tree blob
1138 * @param area Name of node to check (NULL means "/memory")
1139 * @param board_id Board ID to look up
1140 * @param basep Returns base address of first memory bank (NULL to
1141 * ignore)
1142 * @param sizep Returns total memory size (NULL to ignore)
1143 * @param bd Updated with the memory bank information (NULL to skip)
1144 * @return 0 if OK, -ve on error
1145 */
1146 int fdtdec_decode_ram_size(const void *blob, const char *area, int board_id,
1147 phys_addr_t *basep, phys_size_t *sizep,
1148 struct bd_info *bd);
1149
1150 #endif