164dbbd40SGerald Van Baren /*
264dbbd40SGerald Van Baren * (C) Copyright 2007
364dbbd40SGerald Van Baren * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
464dbbd40SGerald Van Baren *
51a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+
664dbbd40SGerald Van Baren */
764dbbd40SGerald Van Baren
864dbbd40SGerald Van Baren #ifndef __FDT_SUPPORT_H
964dbbd40SGerald Van Baren #define __FDT_SUPPORT_H
1064dbbd40SGerald Van Baren
1164dbbd40SGerald Van Baren #ifdef CONFIG_OF_LIBFDT
1264dbbd40SGerald Van Baren
130e00a84cSMasahiro Yamada #include <linux/libfdt.h>
14*5905cceaSRasmus Villemoes #include <abuf.h>
1564dbbd40SGerald Van Baren
1694fb182cSAlexander Graf u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
1794fb182cSAlexander Graf const char *prop, const u32 dflt);
1807e12784SGabe Black u32 fdt_getprop_u32_default(const void *fdt, const char *path,
1907e12784SGabe Black const char *prop, const u32 dflt);
206aa9b52eSJoseph Chen int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
216aa9b52eSJoseph Chen uint64_t val, int is_u64);
223c4c142eSPaul Kocialkowski /**
233c4c142eSPaul Kocialkowski * Add data to the root of the FDT before booting the OS.
243c4c142eSPaul Kocialkowski *
253c4c142eSPaul Kocialkowski * See doc/device-tree-bindings/root.txt
263c4c142eSPaul Kocialkowski *
273c4c142eSPaul Kocialkowski * @param fdt FDT address in memory
283c4c142eSPaul Kocialkowski * @return 0 if ok, or -FDT_ERR_... on error
293c4c142eSPaul Kocialkowski */
3010be5b5dSPaul Kocialkowski int fdt_root(void *fdt);
313c4c142eSPaul Kocialkowski
323c4c142eSPaul Kocialkowski /**
33fb5b9138SJason Zhu * Append info to bootargs
34fb5b9138SJason Zhu *
35fb5b9138SJason Zhu * @param fdt FDT address in memory
36fb5b9138SJason Zhu * @param data string info
37fb5b9138SJason Zhu * @return 0 if ok, else error
38fb5b9138SJason Zhu */
39fb5b9138SJason Zhu int fdt_bootargs_append(void *fdt, char *data);
40fb5b9138SJason Zhu
41fb5b9138SJason Zhu /**
423c4c142eSPaul Kocialkowski * Add chosen data the FDT before booting the OS.
433c4c142eSPaul Kocialkowski *
443c4c142eSPaul Kocialkowski * In particular, this adds the kernel command line (bootargs) to the FDT.
453c4c142eSPaul Kocialkowski *
463c4c142eSPaul Kocialkowski * @param fdt FDT address in memory
473c4c142eSPaul Kocialkowski * @return 0 if ok, or -FDT_ERR_... on error
483c4c142eSPaul Kocialkowski */
49bc6ed0f9SMasahiro Yamada int fdt_chosen(void *fdt);
503c4c142eSPaul Kocialkowski
513c4c142eSPaul Kocialkowski /**
523c4c142eSPaul Kocialkowski * Add initrd information to the FDT before booting the OS.
533c4c142eSPaul Kocialkowski *
543c4c142eSPaul Kocialkowski * @param fdt FDT address in memory
553c4c142eSPaul Kocialkowski * @return 0 if ok, or -FDT_ERR_... on error
563c4c142eSPaul Kocialkowski */
57dbe963aeSMasahiro Yamada int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end);
583c4c142eSPaul Kocialkowski
59e93becf8SKumar Gala void do_fixup_by_path(void *fdt, const char *path, const char *prop,
60e93becf8SKumar Gala const void *val, int len, int create);
61e93becf8SKumar Gala void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
62e93becf8SKumar Gala u32 val, int create);
638ddb10eaSChunhe Lan
do_fixup_by_path_string(void * fdt,const char * path,const char * prop,const char * status)648ddb10eaSChunhe Lan static inline void do_fixup_by_path_string(void *fdt, const char *path,
658ddb10eaSChunhe Lan const char *prop, const char *status)
668ddb10eaSChunhe Lan {
678ddb10eaSChunhe Lan do_fixup_by_path(fdt, path, prop, status, strlen(status) + 1, 1);
688ddb10eaSChunhe Lan }
698ddb10eaSChunhe Lan
709eb77ceaSKumar Gala void do_fixup_by_prop(void *fdt,
719eb77ceaSKumar Gala const char *pname, const void *pval, int plen,
729eb77ceaSKumar Gala const char *prop, const void *val, int len,
739eb77ceaSKumar Gala int create);
749eb77ceaSKumar Gala void do_fixup_by_prop_u32(void *fdt,
759eb77ceaSKumar Gala const char *pname, const void *pval, int plen,
769eb77ceaSKumar Gala const char *prop, u32 val, int create);
779eb77ceaSKumar Gala void do_fixup_by_compat(void *fdt, const char *compat,
789eb77ceaSKumar Gala const char *prop, const void *val, int len, int create);
799eb77ceaSKumar Gala void do_fixup_by_compat_u32(void *fdt, const char *compat,
809eb77ceaSKumar Gala const char *prop, u32 val, int create);
815c1cf89fSAndre Przywara /**
825c1cf89fSAndre Przywara * Setup the memory node in the DT. Creates one if none was existing before.
835c1cf89fSAndre Przywara * Calls fdt_fixup_memory_banks() to populate a single reg pair covering the
845c1cf89fSAndre Przywara * whole memory.
855c1cf89fSAndre Przywara *
865c1cf89fSAndre Przywara * @param blob FDT blob to update
875c1cf89fSAndre Przywara * @param start Begin of DRAM mapping in physical memory
885c1cf89fSAndre Przywara * @param size Size of the single memory bank
895c1cf89fSAndre Przywara * @return 0 if ok, or -1 or -FDT_ERR_... on error
905c1cf89fSAndre Przywara */
913c927281SKumar Gala int fdt_fixup_memory(void *blob, u64 start, u64 size);
925c1cf89fSAndre Przywara
935c1cf89fSAndre Przywara /**
945c1cf89fSAndre Przywara * Fill the DT memory node with multiple memory banks.
955c1cf89fSAndre Przywara * Creates the node if none was existing before.
965c1cf89fSAndre Przywara * If banks is 0, it will not touch the existing reg property. This allows
975c1cf89fSAndre Przywara * boards to not mess with the existing DT setup, which may have been
985c1cf89fSAndre Przywara * filled in properly before.
995c1cf89fSAndre Przywara *
1005c1cf89fSAndre Przywara * @param blob FDT blob to update
1015c1cf89fSAndre Przywara * @param start Array of size <banks> to hold the start addresses.
1025c1cf89fSAndre Przywara * @param size Array of size <banks> to hold the size of each region.
1035c1cf89fSAndre Przywara * @param banks Number of memory banks to create. If 0, the reg
1045c1cf89fSAndre Przywara * property will be left untouched.
1055c1cf89fSAndre Przywara * @return 0 if ok, or -1 or -FDT_ERR_... on error
1065c1cf89fSAndre Przywara */
107a6bd9e83SJohn Rigby int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks);
1085c1cf89fSAndre Przywara
1093a39dbf8SKever Yang int fdt_update_reserved_memory(void *blob, char *name, u64 start, u64 size);
1103a39dbf8SKever Yang
111ba37aa03SKumar Gala void fdt_fixup_ethernet(void *fdt);
11222fb2246SMatthias Fuchs int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
11322fb2246SMatthias Fuchs const void *val, int len, int create);
114b8ec2385STimur Tabi void fdt_fixup_qe_firmware(void *fdt);
11564dbbd40SGerald Van Baren
11608daa258STim Harvey /**
11708daa258STim Harvey * Update native-mode property of display-timings node to the phandle
11808daa258STim Harvey * of the timings matching a display by name (case insensitive).
11908daa258STim Harvey *
12008daa258STim Harvey * see kernel Documentation/devicetree/bindings/video/display-timing.txt
12108daa258STim Harvey *
12208daa258STim Harvey * @param blob FDT blob to update
12308daa258STim Harvey * @param path path within dt
12408daa258STim Harvey * @param display name of display timing to match
12508daa258STim Harvey * @return 0 if ok, or -FDT_ERR_... on error
12608daa258STim Harvey */
12708daa258STim Harvey int fdt_fixup_display(void *blob, const char *path, const char *display);
12808daa258STim Harvey
129b9f6786aSSriram Dash #if defined(CONFIG_USB_EHCI_FSL) || defined(CONFIG_USB_XHCI_FSL)
130a5c289b9SSriram Dash void fsl_fdt_fixup_dr_usb(void *blob, bd_t *bd);
13118e69a35SAnton Vorontsov #else
fsl_fdt_fixup_dr_usb(void * blob,bd_t * bd)132a5c289b9SSriram Dash static inline void fsl_fdt_fixup_dr_usb(void *blob, bd_t *bd) {}
133b9f6786aSSriram Dash #endif /* defined(CONFIG_USB_EHCI_FSL) || defined(CONFIG_USB_XHCI_FSL) */
13418e69a35SAnton Vorontsov
135929a2138SKim Phillips #if defined(CONFIG_SYS_FSL_SEC_COMPAT)
1366b70ffb9SKim Phillips void fdt_fixup_crypto_node(void *blob, int sec_rev);
1376b70ffb9SKim Phillips #else
fdt_fixup_crypto_node(void * blob,int sec_rev)1386b70ffb9SKim Phillips static inline void fdt_fixup_crypto_node(void *blob, int sec_rev) {}
1396b70ffb9SKim Phillips #endif
1406b70ffb9SKim Phillips
141e265ab89SPhilipp Tomsich /**
142e265ab89SPhilipp Tomsich * Record information about a processed loadable in /fit-images (creating
143e265ab89SPhilipp Tomsich * /fit-images if necessary).
144e265ab89SPhilipp Tomsich *
145e265ab89SPhilipp Tomsich * @param blob FDT blob to update
146e265ab89SPhilipp Tomsich * @param index index of this loadable
147e265ab89SPhilipp Tomsich * @param name name of the loadable
148e265ab89SPhilipp Tomsich * @param load_addr address the loadable was loaded to
149e265ab89SPhilipp Tomsich * @param size number of bytes loaded
150e265ab89SPhilipp Tomsich * @param entry_point entry point (if specified, otherwise pass -1)
151e265ab89SPhilipp Tomsich * @param type type (if specified, otherwise pass NULL)
152e265ab89SPhilipp Tomsich * @param os os-type (if specified, otherwise pass NULL)
153e265ab89SPhilipp Tomsich * @return 0 if ok, or -1 or -FDT_ERR_... on error
154e265ab89SPhilipp Tomsich */
155e265ab89SPhilipp Tomsich int fdt_record_loadable(void *blob, u32 index, const char *name,
156e265ab89SPhilipp Tomsich uintptr_t load_addr, u32 size, uintptr_t entry_point,
157e265ab89SPhilipp Tomsich const char *type, const char *os);
158e265ab89SPhilipp Tomsich
1598ab451c4SKumar Gala #ifdef CONFIG_PCI
1608ab451c4SKumar Gala #include <pci.h>
1618ab451c4SKumar Gala int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose);
1628ab451c4SKumar Gala #endif
1638ab451c4SKumar Gala
164a9e8e291SSimon Glass int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name);
165a9e8e291SSimon Glass
166e895a4b0SSimon Glass /**
167e895a4b0SSimon Glass * Add board-specific data to the FDT before booting the OS.
168e895a4b0SSimon Glass *
169e895a4b0SSimon Glass * Use CONFIG_SYS_FDT_PAD to ensure there is sufficient space.
1706f4dbc21SSimon Glass * This function is called if CONFIG_OF_BOARD_SETUP is defined
171e895a4b0SSimon Glass *
172e895a4b0SSimon Glass * @param blob FDT blob to update
173e895a4b0SSimon Glass * @param bd_t Pointer to board data
174e895a4b0SSimon Glass * @return 0 if ok, or -FDT_ERR_... on error
175e895a4b0SSimon Glass */
176e895a4b0SSimon Glass int ft_board_setup(void *blob, bd_t *bd);
177e895a4b0SSimon Glass
178f26d9e52SJoseph Chen /**
179*5905cceaSRasmus Villemoes * board_rng_seed() - Provide a seed to be passed via /chosen/rng-seed
180*5905cceaSRasmus Villemoes *
181*5905cceaSRasmus Villemoes * This function is called if CONFIG_BOARD_RNG_SEED is set, and must
182*5905cceaSRasmus Villemoes * be provided by the board. It should return, via @buf, some suitable
183*5905cceaSRasmus Villemoes * seed value to pass to the kernel.
184*5905cceaSRasmus Villemoes *
185*5905cceaSRasmus Villemoes * @param buf A struct abuf for returning the seed and its size.
186*5905cceaSRasmus Villemoes * @return 0 if ok, negative on error.
187*5905cceaSRasmus Villemoes */
188*5905cceaSRasmus Villemoes int board_rng_seed(struct abuf *buf);
189*5905cceaSRasmus Villemoes
190*5905cceaSRasmus Villemoes /**
191f26d9e52SJoseph Chen * board_fdt_chosen_bootargs() - Arbitrarily amend fdt kernel command line
192f26d9e52SJoseph Chen *
193f26d9e52SJoseph Chen * This is used for late modification of kernel command line arguments just
194f26d9e52SJoseph Chen * before they are added into the /chosen node in flat device tree.
195f26d9e52SJoseph Chen *
196f26d9e52SJoseph Chen * @fdt: fdt blob
197f26d9e52SJoseph Chen * @return: pointer to kernel command line arguments in memory
198f26d9e52SJoseph Chen */
199f26d9e52SJoseph Chen char *board_fdt_chosen_bootargs(void *fdt);
200f26d9e52SJoseph Chen
20100c200f1SVitaly Andrianov /*
20200c200f1SVitaly Andrianov * The keystone2 SOC requires all 32 bit aliased addresses to be converted
20300c200f1SVitaly Andrianov * to their 36 physical format. This has to happen after all fdt nodes
20400c200f1SVitaly Andrianov * are added or modified by the image_setup_libfdt(). The ft_board_setup_ex()
20500c200f1SVitaly Andrianov * called at the end of the image_setup_libfdt() is to do that convertion.
20600c200f1SVitaly Andrianov */
20700c200f1SVitaly Andrianov void ft_board_setup_ex(void *blob, bd_t *bd);
208e125a2ffSGerald Van Baren void ft_cpu_setup(void *blob, bd_t *bd);
209e125a2ffSGerald Van Baren void ft_pci_setup(void *blob, bd_t *bd);
210e125a2ffSGerald Van Baren
211c654b517SSimon Glass /**
212c654b517SSimon Glass * Add system-specific data to the FDT before booting the OS.
213c654b517SSimon Glass *
214c654b517SSimon Glass * Use CONFIG_SYS_FDT_PAD to ensure there is sufficient space.
215c654b517SSimon Glass * This function is called if CONFIG_OF_SYSTEM_SETUP is defined
216c654b517SSimon Glass *
217c654b517SSimon Glass * @param blob FDT blob to update
218c654b517SSimon Glass * @param bd_t Pointer to board data
219c654b517SSimon Glass * @return 0 if ok, or -FDT_ERR_... on error
220c654b517SSimon Glass */
221c654b517SSimon Glass int ft_system_setup(void *blob, bd_t *bd);
222c654b517SSimon Glass
22390fbee3eSJoe Hershberger void set_working_fdt_addr(ulong addr);
224ef476836SHannes Schmelzer
225ef476836SHannes Schmelzer /**
226ef476836SHannes Schmelzer * shrink down the given blob to minimum size + some extrasize if required
227ef476836SHannes Schmelzer *
228ef476836SHannes Schmelzer * @param blob FDT blob to update
229ef476836SHannes Schmelzer * @param extrasize additional bytes needed
230ef476836SHannes Schmelzer * @return 0 if ok, or -FDT_ERR_... on error
231ef476836SHannes Schmelzer */
232ef476836SHannes Schmelzer int fdt_shrink_to_minimum(void *blob, uint extrasize);
233b3606f14STimur Tabi int fdt_increase_size(void *fdt, int add_len);
23454f9c866SKumar Gala
2358a805df1SStefan Roese int fdt_fixup_nor_flash_size(void *blob);
23630d45c0dSStefan Roese
237f4ae23a7SChristopher Spinrath #if defined(CONFIG_FDT_FIXUP_PARTITIONS)
2383c950e2eSAnatolij Gustschin void fdt_fixup_mtdparts(void *fdt, void *node_info, int node_info_size);
239f4ae23a7SChristopher Spinrath #else
fdt_fixup_mtdparts(void * fdt,void * node_info,int node_info_size)240f4ae23a7SChristopher Spinrath static inline void fdt_fixup_mtdparts(void *fdt, void *node_info,
241f4ae23a7SChristopher Spinrath int node_info_size) {}
242f4ae23a7SChristopher Spinrath #endif
243f4ae23a7SChristopher Spinrath
24449b97d9cSKumar Gala void fdt_del_node_and_alias(void *blob, const char *alias);
24511e44fc6SStephen Warren u64 fdt_translate_address(const void *blob, int node_offset,
24611e44fc6SStephen Warren const __be32 *in_addr);
24775e73afdSKumar Gala int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
24875e73afdSKumar Gala phys_addr_t compat_off);
249b4b847e9SKumar Gala int fdt_alloc_phandle(void *blob);
250f117c0f0SKumar Gala int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle);
2513c927cccSTimur Tabi unsigned int fdt_create_phandle(void *fdt, int nodeoffset);
252beca5a5fSAnatolij Gustschin int fdt_add_edid(void *blob, const char *compat, unsigned char *buf);
2533c950e2eSAnatolij Gustschin
254bb682001STimur Tabi int fdt_verify_alias_address(void *fdt, int anode, const char *alias,
255bb682001STimur Tabi u64 addr);
256ec002119SSimon Glass u64 fdt_get_base_address(const void *fdt, int node);
257c48e6868SAlexander Graf int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
258c48e6868SAlexander Graf uint64_t *addr, uint64_t *len);
259bb682001STimur Tabi
2602a523f52SShengzhou Liu enum fdt_status {
2612a523f52SShengzhou Liu FDT_STATUS_OKAY,
2622a523f52SShengzhou Liu FDT_STATUS_DISABLED,
2632a523f52SShengzhou Liu FDT_STATUS_FAIL,
2642a523f52SShengzhou Liu FDT_STATUS_FAIL_ERROR_CODE,
2652a523f52SShengzhou Liu };
2662a523f52SShengzhou Liu int fdt_set_node_status(void *fdt, int nodeoffset,
2672a523f52SShengzhou Liu enum fdt_status status, unsigned int error_code);
fdt_status_okay(void * fdt,int nodeoffset)2682a523f52SShengzhou Liu static inline int fdt_status_okay(void *fdt, int nodeoffset)
2692a523f52SShengzhou Liu {
2702a523f52SShengzhou Liu return fdt_set_node_status(fdt, nodeoffset, FDT_STATUS_OKAY, 0);
2712a523f52SShengzhou Liu }
fdt_status_disabled(void * fdt,int nodeoffset)2722a523f52SShengzhou Liu static inline int fdt_status_disabled(void *fdt, int nodeoffset)
2732a523f52SShengzhou Liu {
2742a523f52SShengzhou Liu return fdt_set_node_status(fdt, nodeoffset, FDT_STATUS_DISABLED, 0);
2752a523f52SShengzhou Liu }
fdt_status_fail(void * fdt,int nodeoffset)276b940ca64SJ. German Rivera static inline int fdt_status_fail(void *fdt, int nodeoffset)
277b940ca64SJ. German Rivera {
278b940ca64SJ. German Rivera return fdt_set_node_status(fdt, nodeoffset, FDT_STATUS_FAIL, 0);
279b940ca64SJ. German Rivera }
2802a523f52SShengzhou Liu
2812a523f52SShengzhou Liu int fdt_set_status_by_alias(void *fdt, const char *alias,
2822a523f52SShengzhou Liu enum fdt_status status, unsigned int error_code);
fdt_status_okay_by_alias(void * fdt,const char * alias)2832a523f52SShengzhou Liu static inline int fdt_status_okay_by_alias(void *fdt, const char *alias)
2842a523f52SShengzhou Liu {
2852a523f52SShengzhou Liu return fdt_set_status_by_alias(fdt, alias, FDT_STATUS_OKAY, 0);
2862a523f52SShengzhou Liu }
fdt_status_disabled_by_alias(void * fdt,const char * alias)2872a523f52SShengzhou Liu static inline int fdt_status_disabled_by_alias(void *fdt, const char *alias)
2882a523f52SShengzhou Liu {
2892a523f52SShengzhou Liu return fdt_set_status_by_alias(fdt, alias, FDT_STATUS_DISABLED, 0);
2902a523f52SShengzhou Liu }
fdt_status_fail_by_alias(void * fdt,const char * alias)291b940ca64SJ. German Rivera static inline int fdt_status_fail_by_alias(void *fdt, const char *alias)
292b940ca64SJ. German Rivera {
293b940ca64SJ. German Rivera return fdt_set_status_by_alias(fdt, alias, FDT_STATUS_FAIL, 0);
294b940ca64SJ. German Rivera }
2952a523f52SShengzhou Liu
29608df4a21SArnab Basu /* Helper to read a big number; size is in cells (not bytes) */
fdt_read_number(const fdt32_t * cell,int size)297eed36609SSimon Glass static inline u64 fdt_read_number(const fdt32_t *cell, int size)
29808df4a21SArnab Basu {
29908df4a21SArnab Basu u64 r = 0;
30008df4a21SArnab Basu while (size--)
30108df4a21SArnab Basu r = (r << 32) | fdt32_to_cpu(*(cell++));
30208df4a21SArnab Basu return r;
30308df4a21SArnab Basu }
30408df4a21SArnab Basu
305eed36609SSimon Glass void fdt_support_default_count_cells(const void *blob, int parentoffset,
306f43b4356SArnab Basu int *addrc, int *sizec);
307d50b07dfSJeroen Hofstee int ft_verify_fdt(void *fdt);
308d50b07dfSJeroen Hofstee int arch_fixup_memory_node(void *blob);
309f43b4356SArnab Basu
310d4f495a8SHans de Goede int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
311d4f495a8SHans de Goede u32 height, u32 stride, const char *format);
312d4f495a8SHans de Goede
3130939009dSPantelis Antoniou int fdt_overlay_apply_verbose(void *fdt, void *fdto);
3140939009dSPantelis Antoniou
31564dbbd40SGerald Van Baren #endif /* ifdef CONFIG_OF_LIBFDT */
31629a23f9dSHeiko Schocher
31729a23f9dSHeiko Schocher #ifdef USE_HOSTCC
31829a23f9dSHeiko Schocher int fdtdec_get_int(const void *blob, int node, const char *prop_name,
31929a23f9dSHeiko Schocher int default_val);
32029a23f9dSHeiko Schocher #endif
321ce176305SPrabhakar Kushwaha #ifdef CONFIG_FMAN_ENET
322ce176305SPrabhakar Kushwaha int fdt_update_ethernet_dt(void *blob);
323ce176305SPrabhakar Kushwaha #endif
32464dbbd40SGerald Van Baren #endif /* ifndef __FDT_SUPPORT_H */
325