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