1 /* SPDX-License-Identifier: GPL-2.0 */
4 #include <linux/ioport.h>
5 #include <linux/errno.h>
11 struct of_pci_range_parser
{
12 struct device_node
*node
;
13 const struct of_bus
*bus
;
21 #define of_range_parser of_pci_range_parser
32 #define of_range of_pci_range
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
39 * of_range_count - Get the number of "ranges" or "dma-ranges" entries
40 * @parser: Parser state initialized by of_range_parser_init()
42 * Returns the number of entries or 0 if none.
44 * Note that calling this within or after the for_each_of_range() iterator will
45 * be inaccurate giving the number of entries remaining.
47 static inline int of_range_count(const struct of_range_parser
*parser
)
49 if (!parser
|| !parser
->node
|| !parser
->range
|| parser
->range
== parser
->end
)
51 return (parser
->end
- parser
->range
) / (parser
->na
+ parser
->pna
+ parser
->ns
);
54 /* Translate a DMA address from device space to CPU space */
55 extern u64
of_translate_dma_address(struct device_node
*dev
,
56 const __be32
*in_addr
);
57 extern const __be32
*of_translate_dma_region(struct device_node
*dev
, const __be32
*addr
,
58 phys_addr_t
*start
, size_t *length
);
60 #ifdef CONFIG_OF_ADDRESS
61 extern u64
of_translate_address(struct device_node
*np
, const __be32
*addr
);
62 extern int of_address_to_resource(struct device_node
*dev
, int index
,
64 extern void __iomem
*of_iomap(struct device_node
*device
, int index
);
65 void __iomem
*of_io_request_and_map(struct device_node
*device
,
66 int index
, const char *name
);
68 /* Extract an address from a device, returns the region size and
69 * the address space flags too. The PCI version uses a BAR number
70 * instead of an absolute index
72 extern const __be32
*__of_get_address(struct device_node
*dev
, int index
, int bar_no
,
73 u64
*size
, unsigned int *flags
);
75 int of_property_read_reg(struct device_node
*np
, int idx
, u64
*addr
, u64
*size
);
77 extern int of_pci_range_parser_init(struct of_pci_range_parser
*parser
,
78 struct device_node
*node
);
79 extern int of_pci_dma_range_parser_init(struct of_pci_range_parser
*parser
,
80 struct device_node
*node
);
81 extern struct of_pci_range
*of_pci_range_parser_one(
82 struct of_pci_range_parser
*parser
,
83 struct of_pci_range
*range
);
84 extern int of_pci_address_to_resource(struct device_node
*dev
, int bar
,
86 extern int of_pci_range_to_resource(const struct of_pci_range
*range
,
87 const struct device_node
*np
,
88 struct resource
*res
);
89 extern int of_range_to_resource(struct device_node
*np
, int index
,
90 struct resource
*res
);
91 extern bool of_dma_is_coherent(struct device_node
*np
);
92 #else /* CONFIG_OF_ADDRESS */
93 static inline void __iomem
*of_io_request_and_map(struct device_node
*device
,
94 int index
, const char *name
)
96 return IOMEM_ERR_PTR(-EINVAL
);
99 static inline u64
of_translate_address(struct device_node
*np
,
105 static inline const __be32
*__of_get_address(struct device_node
*dev
, int index
, int bar_no
,
106 u64
*size
, unsigned int *flags
)
111 static inline int of_property_read_reg(struct device_node
*np
, int idx
, u64
*addr
, u64
*size
)
116 static inline int of_pci_range_parser_init(struct of_pci_range_parser
*parser
,
117 struct device_node
*node
)
122 static inline int of_pci_dma_range_parser_init(struct of_pci_range_parser
*parser
,
123 struct device_node
*node
)
128 static inline struct of_pci_range
*of_pci_range_parser_one(
129 struct of_pci_range_parser
*parser
,
130 struct of_pci_range
*range
)
135 static inline int of_pci_address_to_resource(struct device_node
*dev
, int bar
,
141 static inline int of_pci_range_to_resource(struct of_pci_range
*range
,
142 struct device_node
*np
,
143 struct resource
*res
)
148 static inline int of_range_to_resource(struct device_node
*np
, int index
,
149 struct resource
*res
)
154 static inline bool of_dma_is_coherent(struct device_node
*np
)
158 #endif /* CONFIG_OF_ADDRESS */
161 extern int of_address_to_resource(struct device_node
*dev
, int index
,
163 void __iomem
*of_iomap(struct device_node
*node
, int index
);
165 static inline int of_address_to_resource(struct device_node
*dev
, int index
,
171 static inline void __iomem
*of_iomap(struct device_node
*device
, int index
)
176 #define of_range_parser_init of_pci_range_parser_init
178 static inline const __be32
*of_get_address(struct device_node
*dev
, int index
,
179 u64
*size
, unsigned int *flags
)
181 return __of_get_address(dev
, index
, -1, size
, flags
);
184 static inline const __be32
*of_get_pci_address(struct device_node
*dev
, int bar_no
,
185 u64
*size
, unsigned int *flags
)
187 return __of_get_address(dev
, -1, bar_no
, size
, flags
);
190 static inline int of_address_count(struct device_node
*np
)
195 while (of_address_to_resource(np
, count
, &res
) == 0)
201 #endif /* __OF_ADDRESS_H */