202 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2#ifndef __OF_ADDRESS_H3#define __OF_ADDRESS_H4#include <linux/ioport.h>5#include <linux/errno.h>6#include <linux/of.h>7#include <linux/io.h>8 9struct of_bus;10 11struct 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_parser22 23struct 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_range33 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_range37 38/*39 * of_range_count - Get the number of "ranges" or "dma-ranges" entries40 * @parser: Parser state initialized by of_range_parser_init()41 *42 * Returns the number of entries or 0 if none.43 *44 * Note that calling this within or after the for_each_of_range() iterator will45 * be inaccurate giving the number of entries remaining.46 */47static inline int of_range_count(const struct of_range_parser *parser)48{49 if (!parser || !parser->node || !parser->range || parser->range == parser->end)50 return 0;51 return (parser->end - parser->range) / (parser->na + parser->pna + parser->ns);52}53 54/* Translate a DMA address from device space to CPU space */55extern u64 of_translate_dma_address(struct device_node *dev,56 const __be32 *in_addr);57extern const __be32 *of_translate_dma_region(struct device_node *dev, const __be32 *addr,58 phys_addr_t *start, size_t *length);59 60#ifdef CONFIG_OF_ADDRESS61extern u64 of_translate_address(struct device_node *np, const __be32 *addr);62extern int of_address_to_resource(struct device_node *dev, int index,63 struct resource *r);64extern void __iomem *of_iomap(struct device_node *device, int index);65void __iomem *of_io_request_and_map(struct device_node *device,66 int index, const char *name);67 68/* Extract an address from a device, returns the region size and69 * the address space flags too. The PCI version uses a BAR number70 * instead of an absolute index71 */72extern const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,73 u64 *size, unsigned int *flags);74 75int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size);76 77extern int of_pci_range_parser_init(struct of_pci_range_parser *parser,78 struct device_node *node);79extern int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,80 struct device_node *node);81extern struct of_pci_range *of_pci_range_parser_one(82 struct of_pci_range_parser *parser,83 struct of_pci_range *range);84extern int of_pci_address_to_resource(struct device_node *dev, int bar,85 struct resource *r);86extern int of_pci_range_to_resource(struct of_pci_range *range,87 struct device_node *np,88 struct resource *res);89extern int of_range_to_resource(struct device_node *np, int index,90 struct resource *res);91extern bool of_dma_is_coherent(struct device_node *np);92#else /* CONFIG_OF_ADDRESS */93static inline void __iomem *of_io_request_and_map(struct device_node *device,94 int index, const char *name)95{96 return IOMEM_ERR_PTR(-EINVAL);97}98 99static inline u64 of_translate_address(struct device_node *np,100 const __be32 *addr)101{102 return OF_BAD_ADDR;103}104 105static inline const __be32 *__of_get_address(struct device_node *dev, int index, int bar_no,106 u64 *size, unsigned int *flags)107{108 return NULL;109}110 111static inline int of_property_read_reg(struct device_node *np, int idx, u64 *addr, u64 *size)112{113 return -ENOSYS;114}115 116static inline int of_pci_range_parser_init(struct of_pci_range_parser *parser,117 struct device_node *node)118{119 return -ENOSYS;120}121 122static inline int of_pci_dma_range_parser_init(struct of_pci_range_parser *parser,123 struct device_node *node)124{125 return -ENOSYS;126}127 128static inline struct of_pci_range *of_pci_range_parser_one(129 struct of_pci_range_parser *parser,130 struct of_pci_range *range)131{132 return NULL;133}134 135static inline int of_pci_address_to_resource(struct device_node *dev, int bar,136 struct resource *r)137{138 return -ENOSYS;139}140 141static inline int of_pci_range_to_resource(struct of_pci_range *range,142 struct device_node *np,143 struct resource *res)144{145 return -ENOSYS;146}147 148static inline int of_range_to_resource(struct device_node *np, int index,149 struct resource *res)150{151 return -ENOSYS;152}153 154static inline bool of_dma_is_coherent(struct device_node *np)155{156 return false;157}158#endif /* CONFIG_OF_ADDRESS */159 160#ifdef CONFIG_OF161extern int of_address_to_resource(struct device_node *dev, int index,162 struct resource *r);163void __iomem *of_iomap(struct device_node *node, int index);164#else165static inline int of_address_to_resource(struct device_node *dev, int index,166 struct resource *r)167{168 return -EINVAL;169}170 171static inline void __iomem *of_iomap(struct device_node *device, int index)172{173 return NULL;174}175#endif176#define of_range_parser_init of_pci_range_parser_init177 178static inline const __be32 *of_get_address(struct device_node *dev, int index,179 u64 *size, unsigned int *flags)180{181 return __of_get_address(dev, index, -1, size, flags);182}183 184static inline const __be32 *of_get_pci_address(struct device_node *dev, int bar_no,185 u64 *size, unsigned int *flags)186{187 return __of_get_address(dev, -1, bar_no, size, flags);188}189 190static inline int of_address_count(struct device_node *np)191{192 struct resource res;193 int count = 0;194 195 while (of_address_to_resource(np, count, &res) == 0)196 count++;197 198 return count;199}200 201#endif /* __OF_ADDRESS_H */202