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