1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | #include <linux/string.h> |
3 | #include <linux/kernel.h> |
4 | #include <linux/export.h> |
5 | #include <linux/mod_devicetable.h> |
6 | #include <linux/errno.h> |
7 | #include <linux/irq.h> |
8 | #include <linux/of.h> |
9 | #include <linux/of_platform.h> |
10 | #include <linux/of_address.h> |
11 | #include <linux/of_irq.h> |
12 | #include <linux/platform_device.h> |
13 | |
14 | #include "of_device_common.h" |
15 | |
16 | unsigned int irq_of_parse_and_map(struct device_node *node, int index) |
17 | { |
18 | struct platform_device *op = of_find_device_by_node(np: node); |
19 | |
20 | if (!op || index >= op->archdata.num_irqs) |
21 | return 0; |
22 | |
23 | return op->archdata.irqs[index]; |
24 | } |
25 | EXPORT_SYMBOL(irq_of_parse_and_map); |
26 | |
27 | int of_address_to_resource(struct device_node *node, int index, |
28 | struct resource *r) |
29 | { |
30 | struct platform_device *op = of_find_device_by_node(np: node); |
31 | |
32 | if (!op || index >= op->num_resources) |
33 | return -EINVAL; |
34 | |
35 | memcpy(r, &op->archdata.resource[index], sizeof(*r)); |
36 | return 0; |
37 | } |
38 | EXPORT_SYMBOL_GPL(of_address_to_resource); |
39 | |
40 | void __iomem *of_iomap(struct device_node *node, int index) |
41 | { |
42 | struct platform_device *op = of_find_device_by_node(np: node); |
43 | struct resource *r; |
44 | |
45 | if (!op || index >= op->num_resources) |
46 | return NULL; |
47 | |
48 | r = &op->archdata.resource[index]; |
49 | |
50 | return of_ioremap(r, 0, resource_size(res: r), (char *) r->name); |
51 | } |
52 | EXPORT_SYMBOL(of_iomap); |
53 | |
54 | /* Take the archdata values for IOMMU, STC, and HOSTDATA found in |
55 | * BUS and propagate to all child platform_device objects. |
56 | */ |
57 | void of_propagate_archdata(struct platform_device *bus) |
58 | { |
59 | struct dev_archdata *bus_sd = &bus->dev.archdata; |
60 | struct device_node *bus_dp = bus->dev.of_node; |
61 | struct device_node *dp; |
62 | |
63 | for (dp = bus_dp->child; dp; dp = dp->sibling) { |
64 | struct platform_device *op = of_find_device_by_node(np: dp); |
65 | |
66 | op->dev.archdata.iommu = bus_sd->iommu; |
67 | op->dev.archdata.stc = bus_sd->stc; |
68 | op->dev.archdata.host_controller = bus_sd->host_controller; |
69 | op->dev.archdata.numa_node = bus_sd->numa_node; |
70 | op->dev.dma_ops = bus->dev.dma_ops; |
71 | |
72 | if (dp->child) |
73 | of_propagate_archdata(bus: op); |
74 | } |
75 | } |
76 | |
77 | static void get_cells(struct device_node *dp, int *addrc, int *sizec) |
78 | { |
79 | if (addrc) |
80 | *addrc = of_n_addr_cells(np: dp); |
81 | if (sizec) |
82 | *sizec = of_n_size_cells(np: dp); |
83 | } |
84 | |
85 | /* |
86 | * Default translator (generic bus) |
87 | */ |
88 | |
89 | void of_bus_default_count_cells(struct device_node *dev, int *addrc, int *sizec) |
90 | { |
91 | get_cells(dp: dev, addrc, sizec); |
92 | } |
93 | |
94 | /* Make sure the least significant 64-bits are in-range. Even |
95 | * for 3 or 4 cell values it is a good enough approximation. |
96 | */ |
97 | int of_out_of_range(const u32 *addr, const u32 *base, |
98 | const u32 *size, int na, int ns) |
99 | { |
100 | u64 a = of_read_addr(cell: addr, size: na); |
101 | u64 b = of_read_addr(cell: base, size: na); |
102 | |
103 | if (a < b) |
104 | return 1; |
105 | |
106 | b += of_read_addr(cell: size, size: ns); |
107 | if (a >= b) |
108 | return 1; |
109 | |
110 | return 0; |
111 | } |
112 | |
113 | int of_bus_default_map(u32 *addr, const u32 *range, int na, int ns, int pna) |
114 | { |
115 | u32 result[OF_MAX_ADDR_CELLS]; |
116 | int i; |
117 | |
118 | if (ns > 2) { |
119 | printk("of_device: Cannot handle size cells (%d) > 2." , ns); |
120 | return -EINVAL; |
121 | } |
122 | |
123 | if (of_out_of_range(addr, base: range, size: range + na + pna, na, ns)) |
124 | return -EINVAL; |
125 | |
126 | /* Start with the parent range base. */ |
127 | memcpy(result, range + na, pna * 4); |
128 | |
129 | /* Add in the child address offset. */ |
130 | for (i = 0; i < na; i++) |
131 | result[pna - 1 - i] += |
132 | (addr[na - 1 - i] - |
133 | range[na - 1 - i]); |
134 | |
135 | memcpy(addr, result, pna * 4); |
136 | |
137 | return 0; |
138 | } |
139 | |
140 | unsigned long of_bus_default_get_flags(const u32 *addr, unsigned long flags) |
141 | { |
142 | if (flags) |
143 | return flags; |
144 | return IORESOURCE_MEM; |
145 | } |
146 | |
147 | /* |
148 | * SBUS bus specific translator |
149 | */ |
150 | |
151 | int of_bus_sbus_match(struct device_node *np) |
152 | { |
153 | struct device_node *dp = np; |
154 | |
155 | while (dp) { |
156 | if (of_node_name_eq(np: dp, name: "sbus" ) || |
157 | of_node_name_eq(np: dp, name: "sbi" )) |
158 | return 1; |
159 | |
160 | /* Have a look at use_1to1_mapping(). We're trying |
161 | * to match SBUS if that's the top-level bus and we |
162 | * don't have some intervening real bus that provides |
163 | * ranges based translations. |
164 | */ |
165 | if (of_property_present(np: dp, propname: "ranges" )) |
166 | break; |
167 | |
168 | dp = dp->parent; |
169 | } |
170 | |
171 | return 0; |
172 | } |
173 | |
174 | void of_bus_sbus_count_cells(struct device_node *child, int *addrc, int *sizec) |
175 | { |
176 | if (addrc) |
177 | *addrc = 2; |
178 | if (sizec) |
179 | *sizec = 1; |
180 | } |
181 | |