1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/string.h>
3 #include <linux/kernel.h>
5 #include <linux/export.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/errno.h>
9 #include <linux/of_platform.h>
10 #include <linux/of_address.h>
11 #include <linux/of_device.h>
12 #include <linux/of_irq.h>
14 #include "of_device_common.h"
16 unsigned int irq_of_parse_and_map(struct device_node
*node
, int index
)
18 struct platform_device
*op
= of_find_device_by_node(node
);
20 if (!op
|| index
>= op
->archdata
.num_irqs
)
23 return op
->archdata
.irqs
[index
];
25 EXPORT_SYMBOL(irq_of_parse_and_map
);
27 int of_address_to_resource(struct device_node
*node
, int index
,
30 struct platform_device
*op
= of_find_device_by_node(node
);
32 if (!op
|| index
>= op
->num_resources
)
35 memcpy(r
, &op
->archdata
.resource
[index
], sizeof(*r
));
38 EXPORT_SYMBOL_GPL(of_address_to_resource
);
40 void __iomem
*of_iomap(struct device_node
*node
, int index
)
42 struct platform_device
*op
= of_find_device_by_node(node
);
45 if (!op
|| index
>= op
->num_resources
)
48 r
= &op
->archdata
.resource
[index
];
50 return of_ioremap(r
, 0, resource_size(r
), (char *) r
->name
);
52 EXPORT_SYMBOL(of_iomap
);
54 /* Take the archdata values for IOMMU, STC, and HOSTDATA found in
55 * BUS and propagate to all child platform_device objects.
57 void of_propagate_archdata(struct platform_device
*bus
)
59 struct dev_archdata
*bus_sd
= &bus
->dev
.archdata
;
60 struct device_node
*bus_dp
= bus
->dev
.of_node
;
61 struct device_node
*dp
;
63 for (dp
= bus_dp
->child
; dp
; dp
= dp
->sibling
) {
64 struct platform_device
*op
= of_find_device_by_node(dp
);
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
;
72 of_propagate_archdata(op
);
76 static void get_cells(struct device_node
*dp
, int *addrc
, int *sizec
)
79 *addrc
= of_n_addr_cells(dp
);
81 *sizec
= of_n_size_cells(dp
);
85 * Default translator (generic bus)
88 void of_bus_default_count_cells(struct device_node
*dev
, int *addrc
, int *sizec
)
90 get_cells(dev
, addrc
, sizec
);
93 /* Make sure the least significant 64-bits are in-range. Even
94 * for 3 or 4 cell values it is a good enough approximation.
96 int of_out_of_range(const u32
*addr
, const u32
*base
,
97 const u32
*size
, int na
, int ns
)
99 u64 a
= of_read_addr(addr
, na
);
100 u64 b
= of_read_addr(base
, na
);
105 b
+= of_read_addr(size
, ns
);
112 int of_bus_default_map(u32
*addr
, const u32
*range
, int na
, int ns
, int pna
)
114 u32 result
[OF_MAX_ADDR_CELLS
];
118 printk("of_device: Cannot handle size cells (%d) > 2.", ns
);
122 if (of_out_of_range(addr
, range
, range
+ na
+ pna
, na
, ns
))
125 /* Start with the parent range base. */
126 memcpy(result
, range
+ na
, pna
* 4);
128 /* Add in the child address offset. */
129 for (i
= 0; i
< na
; i
++)
130 result
[pna
- 1 - i
] +=
134 memcpy(addr
, result
, pna
* 4);
139 unsigned long of_bus_default_get_flags(const u32
*addr
, unsigned long flags
)
143 return IORESOURCE_MEM
;
147 * SBUS bus specific translator
150 int of_bus_sbus_match(struct device_node
*np
)
152 struct device_node
*dp
= np
;
155 if (of_node_name_eq(dp
, "sbus") ||
156 of_node_name_eq(dp
, "sbi"))
159 /* Have a look at use_1to1_mapping(). We're trying
160 * to match SBUS if that's the top-level bus and we
161 * don't have some intervening real bus that provides
162 * ranges based translations.
164 if (of_find_property(dp
, "ranges", NULL
) != NULL
)
173 void of_bus_sbus_count_cells(struct device_node
*child
, int *addrc
, int *sizec
)