xref: /openbmc/linux/include/linux/of_address.h (revision d9fd5a71)
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 
42 #ifdef CONFIG_OF_ADDRESS
43 extern u64 of_translate_address(struct device_node *np, const __be32 *addr);
44 extern int of_address_to_resource(struct device_node *dev, int index,
45 				  struct resource *r);
46 extern void __iomem *of_iomap(struct device_node *device, int index);
47 void __iomem *of_io_request_and_map(struct device_node *device,
48 				    int index, const char *name);
49 
50 /* Extract an address from a device, returns the region size and
51  * the address space flags too. The PCI version uses a BAR number
52  * instead of an absolute index
53  */
54 extern const __be32 *of_get_address(struct device_node *dev, int index,
55 			   u64 *size, unsigned int *flags);
56 
57 extern int of_pci_range_parser_init(struct of_pci_range_parser *parser,
58 			struct device_node *node);
59 extern int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
60 			struct device_node *node);
61 extern struct of_pci_range *of_pci_range_parser_one(
62 					struct of_pci_range_parser *parser,
63 					struct of_pci_range *range);
64 extern bool of_dma_is_coherent(struct device_node *np);
65 #else /* CONFIG_OF_ADDRESS */
66 static inline void __iomem *of_io_request_and_map(struct device_node *device,
67 						  int index, const char *name)
68 {
69 	return IOMEM_ERR_PTR(-EINVAL);
70 }
71 
72 static inline u64 of_translate_address(struct device_node *np,
73 				       const __be32 *addr)
74 {
75 	return OF_BAD_ADDR;
76 }
77 
78 static inline const __be32 *of_get_address(struct device_node *dev, int index,
79 					u64 *size, unsigned int *flags)
80 {
81 	return NULL;
82 }
83 
84 static inline int of_pci_range_parser_init(struct of_pci_range_parser *parser,
85 			struct device_node *node)
86 {
87 	return -ENOSYS;
88 }
89 
90 static inline int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,
91 			struct device_node *node)
92 {
93 	return -ENOSYS;
94 }
95 
96 static inline struct of_pci_range *of_pci_range_parser_one(
97 					struct of_pci_range_parser *parser,
98 					struct of_pci_range *range)
99 {
100 	return NULL;
101 }
102 
103 static inline bool of_dma_is_coherent(struct device_node *np)
104 {
105 	return false;
106 }
107 #endif /* CONFIG_OF_ADDRESS */
108 
109 #ifdef CONFIG_OF
110 extern int of_address_to_resource(struct device_node *dev, int index,
111 				  struct resource *r);
112 void __iomem *of_iomap(struct device_node *node, int index);
113 #else
114 static inline int of_address_to_resource(struct device_node *dev, int index,
115 					 struct resource *r)
116 {
117 	return -EINVAL;
118 }
119 
120 static inline void __iomem *of_iomap(struct device_node *device, int index)
121 {
122 	return NULL;
123 }
124 #endif
125 #define of_range_parser_init of_pci_range_parser_init
126 
127 #if defined(CONFIG_OF_ADDRESS) && defined(CONFIG_PCI)
128 extern const __be32 *of_get_pci_address(struct device_node *dev, int bar_no,
129 			       u64 *size, unsigned int *flags);
130 extern int of_pci_address_to_resource(struct device_node *dev, int bar,
131 				      struct resource *r);
132 extern int of_pci_range_to_resource(struct of_pci_range *range,
133 				    struct device_node *np,
134 				    struct resource *res);
135 #else /* CONFIG_OF_ADDRESS && CONFIG_PCI */
136 static inline int of_pci_address_to_resource(struct device_node *dev, int bar,
137 				             struct resource *r)
138 {
139 	return -ENOSYS;
140 }
141 
142 static inline const __be32 *of_get_pci_address(struct device_node *dev,
143 		int bar_no, u64 *size, unsigned int *flags)
144 {
145 	return NULL;
146 }
147 static inline int of_pci_range_to_resource(struct of_pci_range *range,
148 					   struct device_node *np,
149 					   struct resource *res)
150 {
151 	return -ENOSYS;
152 }
153 #endif /* CONFIG_OF_ADDRESS && CONFIG_PCI */
154 
155 #endif /* __OF_ADDRESS_H */
156