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