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