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