1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef __OF_ADDRESS_H
3#define __OF_ADDRESS_H
4#include <linux/ioport.h>
5#include <linux/errno.h>
6#include <linux/of.h>
7#include <linux/io.h>
8
9struct of_pci_range_parser {
10 struct device_node *node;
11 const __be32 *range;
12 const __be32 *end;
13 int np;
14 int pna;
15};
16
17struct of_pci_range {
18 u32 pci_space;
19 u64 pci_addr;
20 u64 cpu_addr;
21 u64 size;
22 u32 flags;
23};
24
25#define for_each_of_pci_range(parser, range) \
26 for (; of_pci_range_parser_one(parser, range);)
27
28/* Translate a DMA address from device space to CPU space */
29extern u64 of_translate_dma_address(struct device_node *dev,
30 const __be32 *in_addr);
31
32#ifdef CONFIG_OF_ADDRESS
33extern u64 of_translate_address(struct device_node *np, const __be32 *addr);
34extern int of_address_to_resource(struct device_node *dev, int index,
35 struct resource *r);
36extern struct device_node *of_find_matching_node_by_address(
37 struct device_node *from,
38 const struct of_device_id *matches,
39 u64 base_address);
40extern void __iomem *of_iomap(struct device_node *device, int index);
41void __iomem *of_io_request_and_map(struct device_node *device,
42 int index, const char *name);
43
44/* Extract an address from a device, returns the region size and
45 * the address space flags too. The PCI version uses a BAR number
46 * instead of an absolute index
47 */
48extern const __be32 *of_get_address(struct device_node *dev, int index,
49 u64 *size, unsigned int *flags);
50
51extern int of_pci_range_parser_init(struct of_pci_range_parser *parser,
52 struct device_node *node);
53extern int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
54 struct device_node *node);
55extern struct of_pci_range *of_pci_range_parser_one(
56 struct of_pci_range_parser *parser,
57 struct of_pci_range *range);
58extern int of_dma_get_range(struct device_node *np, u64 *dma_addr,
59 u64 *paddr, u64 *size);
60extern bool of_dma_is_coherent(struct device_node *np);
61#else /* CONFIG_OF_ADDRESS */
62static inline void __iomem *of_io_request_and_map(struct device_node *device,
63 int index, const char *name)
64{
65 return IOMEM_ERR_PTR(-EINVAL);
66}
67
68static inline u64 of_translate_address(struct device_node *np,
69 const __be32 *addr)
70{
71 return OF_BAD_ADDR;
72}
73
74static inline struct device_node *of_find_matching_node_by_address(
75 struct device_node *from,
76 const struct of_device_id *matches,
77 u64 base_address)
78{
79 return NULL;
80}
81
82static inline const __be32 *of_get_address(struct device_node *dev, int index,
83 u64 *size, unsigned int *flags)
84{
85 return NULL;
86}
87
88static inline int of_pci_range_parser_init(struct of_pci_range_parser *parser,
89 struct device_node *node)
90{
91 return -ENOSYS;
92}
93
94static inline int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
95 struct device_node *node)
96{
97 return -ENOSYS;
98}
99
100static inline struct of_pci_range *of_pci_range_parser_one(
101 struct of_pci_range_parser *parser,
102 struct of_pci_range *range)
103{
104 return NULL;
105}
106
107static inline int of_dma_get_range(struct device_node *np, u64 *dma_addr,
108 u64 *paddr, u64 *size)
109{
110 return -ENODEV;
111}
112
113static inline bool of_dma_is_coherent(struct device_node *np)
114{
115 return false;
116}
117#endif /* CONFIG_OF_ADDRESS */
118
119#ifdef CONFIG_OF
120extern int of_address_to_resource(struct device_node *dev, int index,
121 struct resource *r);
122void __iomem *of_iomap(struct device_node *node, int index);
123#else
124static inline int of_address_to_resource(struct device_node *dev, int index,
125 struct resource *r)
126{
127 return -EINVAL;
128}
129
130static inline void __iomem *of_iomap(struct device_node *device, int index)
131{
132 return NULL;
133}
134#endif
135
136#if defined(CONFIG_OF_ADDRESS) && defined(CONFIG_PCI)
137extern const __be32 *of_get_pci_address(struct device_node *dev, int bar_no,
138 u64 *size, unsigned int *flags);
139extern int of_pci_address_to_resource(struct device_node *dev, int bar,
140 struct resource *r);
141extern int of_pci_range_to_resource(struct of_pci_range *range,
142 struct device_node *np,
143 struct resource *res);
144#else /* CONFIG_OF_ADDRESS && CONFIG_PCI */
145static inline int of_pci_address_to_resource(struct device_node *dev, int bar,
146 struct resource *r)
147{
148 return -ENOSYS;
149}
150
151static inline const __be32 *of_get_pci_address(struct device_node *dev,
152 int bar_no, u64 *size, unsigned int *flags)
153{
154 return NULL;
155}
156static inline int of_pci_range_to_resource(struct of_pci_range *range,
157 struct device_node *np,
158 struct resource *res)
159{
160 return -ENOSYS;
161}
162#endif /* CONFIG_OF_ADDRESS && CONFIG_PCI */
163
164#endif /* __OF_ADDRESS_H */
165
166