Commit | Line | Data |
---|---|---|
fd531431 DM |
1 | #include <linux/string.h> |
2 | #include <linux/kernel.h> | |
f85ff305 | 3 | #include <linux/of.h> |
fd531431 DM |
4 | #include <linux/init.h> |
5 | #include <linux/module.h> | |
6 | #include <linux/mod_devicetable.h> | |
7 | #include <linux/slab.h> | |
3f23de10 | 8 | #include <linux/errno.h> |
c9f5b7e7 | 9 | #include <linux/irq.h> |
3f23de10 SR |
10 | #include <linux/of_device.h> |
11 | #include <linux/of_platform.h> | |
fd531431 | 12 | |
c9f5b7e7 | 13 | #include "of_device_common.h" |
cf44bbc2 | 14 | |
cf44bbc2 DM |
15 | /* |
16 | * PCI bus specific translator | |
17 | */ | |
18 | ||
19 | static int of_bus_pci_match(struct device_node *np) | |
20 | { | |
a83f9823 DM |
21 | if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) { |
22 | /* Do not do PCI specific frobbing if the | |
23 | * PCI bridge lacks a ranges property. We | |
24 | * want to pass it through up to the next | |
25 | * parent as-is, not with the PCI translate | |
26 | * method which chops off the top address cell. | |
27 | */ | |
28 | if (!of_find_property(np, "ranges", NULL)) | |
29 | return 0; | |
30 | ||
31 | return 1; | |
32 | } | |
33 | ||
34 | return 0; | |
cf44bbc2 DM |
35 | } |
36 | ||
37 | static void of_bus_pci_count_cells(struct device_node *np, | |
38 | int *addrc, int *sizec) | |
39 | { | |
40 | if (addrc) | |
41 | *addrc = 3; | |
42 | if (sizec) | |
43 | *sizec = 2; | |
44 | } | |
45 | ||
a83f9823 DM |
46 | static int of_bus_pci_map(u32 *addr, const u32 *range, |
47 | int na, int ns, int pna) | |
cf44bbc2 | 48 | { |
a83f9823 DM |
49 | u32 result[OF_MAX_ADDR_CELLS]; |
50 | int i; | |
cf44bbc2 DM |
51 | |
52 | /* Check address type match */ | |
53 | if ((addr[0] ^ range[0]) & 0x03000000) | |
a83f9823 | 54 | return -EINVAL; |
cf44bbc2 | 55 | |
a83f9823 DM |
56 | if (of_out_of_range(addr + 1, range + 1, range + na + pna, |
57 | na - 1, ns)) | |
58 | return -EINVAL; | |
cf44bbc2 | 59 | |
a83f9823 DM |
60 | /* Start with the parent range base. */ |
61 | memcpy(result, range + na, pna * 4); | |
cf44bbc2 | 62 | |
a83f9823 DM |
63 | /* Add in the child address offset, skipping high cell. */ |
64 | for (i = 0; i < na - 1; i++) | |
65 | result[pna - 1 - i] += | |
66 | (addr[na - 1 - i] - | |
67 | range[na - 1 - i]); | |
68 | ||
69 | memcpy(addr, result, pna * 4); | |
70 | ||
71 | return 0; | |
cf44bbc2 DM |
72 | } |
73 | ||
e3c71a32 | 74 | static unsigned long of_bus_pci_get_flags(const u32 *addr, unsigned long flags) |
cf44bbc2 | 75 | { |
cf44bbc2 DM |
76 | u32 w = addr[0]; |
77 | ||
e3c71a32 DM |
78 | /* For PCI, we override whatever child busses may have used. */ |
79 | flags = 0; | |
cf44bbc2 DM |
80 | switch((w >> 24) & 0x03) { |
81 | case 0x01: | |
82 | flags |= IORESOURCE_IO; | |
e3c71a32 DM |
83 | break; |
84 | ||
cf44bbc2 DM |
85 | case 0x02: /* 32 bits */ |
86 | case 0x03: /* 64 bits */ | |
87 | flags |= IORESOURCE_MEM; | |
e3c71a32 | 88 | break; |
cf44bbc2 DM |
89 | } |
90 | if (w & 0x40000000) | |
91 | flags |= IORESOURCE_PREFETCH; | |
92 | return flags; | |
93 | } | |
94 | ||
bdba4d6b | 95 | static unsigned long of_bus_sbus_get_flags(const u32 *addr, unsigned long flags) |
cf44bbc2 DM |
96 | { |
97 | return IORESOURCE_MEM; | |
98 | } | |
99 | ||
100 | ||
101 | /* | |
102 | * Array of bus specific translators | |
103 | */ | |
104 | ||
105 | static struct of_bus of_busses[] = { | |
106 | /* PCI */ | |
107 | { | |
108 | .name = "pci", | |
109 | .addr_prop_name = "assigned-addresses", | |
110 | .match = of_bus_pci_match, | |
111 | .count_cells = of_bus_pci_count_cells, | |
112 | .map = of_bus_pci_map, | |
cf44bbc2 DM |
113 | .get_flags = of_bus_pci_get_flags, |
114 | }, | |
115 | /* SBUS */ | |
116 | { | |
117 | .name = "sbus", | |
118 | .addr_prop_name = "reg", | |
119 | .match = of_bus_sbus_match, | |
120 | .count_cells = of_bus_sbus_count_cells, | |
c9f5b7e7 | 121 | .map = of_bus_default_map, |
cf44bbc2 DM |
122 | .get_flags = of_bus_sbus_get_flags, |
123 | }, | |
124 | /* Default */ | |
125 | { | |
126 | .name = "default", | |
127 | .addr_prop_name = "reg", | |
128 | .match = NULL, | |
129 | .count_cells = of_bus_default_count_cells, | |
130 | .map = of_bus_default_map, | |
cf44bbc2 DM |
131 | .get_flags = of_bus_default_get_flags, |
132 | }, | |
133 | }; | |
134 | ||
135 | static struct of_bus *of_match_bus(struct device_node *np) | |
136 | { | |
137 | int i; | |
138 | ||
139 | for (i = 0; i < ARRAY_SIZE(of_busses); i ++) | |
140 | if (!of_busses[i].match || of_busses[i].match(np)) | |
141 | return &of_busses[i]; | |
142 | BUG(); | |
143 | return NULL; | |
144 | } | |
145 | ||
146 | static int __init build_one_resource(struct device_node *parent, | |
147 | struct of_bus *bus, | |
148 | struct of_bus *pbus, | |
149 | u32 *addr, | |
150 | int na, int ns, int pna) | |
151 | { | |
8271f042 | 152 | const u32 *ranges; |
cf44bbc2 DM |
153 | unsigned int rlen; |
154 | int rone; | |
cf44bbc2 DM |
155 | |
156 | ranges = of_get_property(parent, "ranges", &rlen); | |
157 | if (ranges == NULL || rlen == 0) { | |
a83f9823 DM |
158 | u32 result[OF_MAX_ADDR_CELLS]; |
159 | int i; | |
160 | ||
161 | memset(result, 0, pna * 4); | |
162 | for (i = 0; i < na; i++) | |
163 | result[pna - 1 - i] = | |
164 | addr[na - 1 - i]; | |
165 | ||
166 | memcpy(addr, result, pna * 4); | |
167 | return 0; | |
cf44bbc2 DM |
168 | } |
169 | ||
170 | /* Now walk through the ranges */ | |
171 | rlen /= 4; | |
172 | rone = na + pna + ns; | |
173 | for (; rlen >= rone; rlen -= rone, ranges += rone) { | |
a83f9823 DM |
174 | if (!bus->map(addr, ranges, na, ns, pna)) |
175 | return 0; | |
cf44bbc2 | 176 | } |
cf44bbc2 | 177 | |
a83f9823 | 178 | return 1; |
cf44bbc2 DM |
179 | } |
180 | ||
5280267c DM |
181 | static int __init use_1to1_mapping(struct device_node *pp) |
182 | { | |
183 | /* If we have a ranges property in the parent, use it. */ | |
184 | if (of_find_property(pp, "ranges", NULL) != NULL) | |
185 | return 0; | |
186 | ||
187 | /* Some SBUS devices use intermediate nodes to express | |
188 | * hierarchy within the device itself. These aren't | |
189 | * real bus nodes, and don't have a 'ranges' property. | |
190 | * But, we should still pass the translation work up | |
191 | * to the SBUS itself. | |
192 | */ | |
193 | if (!strcmp(pp->name, "dma") || | |
194 | !strcmp(pp->name, "espdma") || | |
195 | !strcmp(pp->name, "ledma") || | |
196 | !strcmp(pp->name, "lebuffer")) | |
197 | return 0; | |
198 | ||
199 | return 1; | |
200 | } | |
201 | ||
a83f9823 DM |
202 | static int of_resource_verbose; |
203 | ||
cf44bbc2 DM |
204 | static void __init build_device_resources(struct of_device *op, |
205 | struct device *parent) | |
206 | { | |
207 | struct of_device *p_op; | |
208 | struct of_bus *bus; | |
209 | int na, ns; | |
210 | int index, num_reg; | |
8271f042 | 211 | const void *preg; |
cf44bbc2 DM |
212 | |
213 | if (!parent) | |
214 | return; | |
215 | ||
216 | p_op = to_of_device(parent); | |
217 | bus = of_match_bus(p_op->node); | |
218 | bus->count_cells(op->node, &na, &ns); | |
219 | ||
220 | preg = of_get_property(op->node, bus->addr_prop_name, &num_reg); | |
221 | if (!preg || num_reg == 0) | |
222 | return; | |
223 | ||
224 | /* Convert to num-cells. */ | |
225 | num_reg /= 4; | |
226 | ||
227 | /* Conver to num-entries. */ | |
228 | num_reg /= na + ns; | |
229 | ||
230 | for (index = 0; index < num_reg; index++) { | |
231 | struct resource *r = &op->resource[index]; | |
232 | u32 addr[OF_MAX_ADDR_CELLS]; | |
8271f042 | 233 | const u32 *reg = (preg + (index * ((na + ns) * 4))); |
cf44bbc2 DM |
234 | struct device_node *dp = op->node; |
235 | struct device_node *pp = p_op->node; | |
b85cdd49 | 236 | struct of_bus *pbus, *dbus; |
cf44bbc2 DM |
237 | u64 size, result = OF_BAD_ADDR; |
238 | unsigned long flags; | |
239 | int dna, dns; | |
240 | int pna, pns; | |
241 | ||
242 | size = of_read_addr(reg + na, ns); | |
cf44bbc2 DM |
243 | |
244 | memcpy(addr, reg, na * 4); | |
245 | ||
e3c71a32 DM |
246 | flags = bus->get_flags(reg, 0); |
247 | ||
5280267c | 248 | if (use_1to1_mapping(pp)) { |
cf44bbc2 DM |
249 | result = of_read_addr(addr, na); |
250 | goto build_res; | |
251 | } | |
252 | ||
253 | dna = na; | |
254 | dns = ns; | |
b85cdd49 | 255 | dbus = bus; |
cf44bbc2 DM |
256 | |
257 | while (1) { | |
258 | dp = pp; | |
259 | pp = dp->parent; | |
260 | if (!pp) { | |
261 | result = of_read_addr(addr, dna); | |
262 | break; | |
263 | } | |
264 | ||
265 | pbus = of_match_bus(pp); | |
266 | pbus->count_cells(dp, &pna, &pns); | |
267 | ||
b85cdd49 | 268 | if (build_one_resource(dp, dbus, pbus, addr, |
a83f9823 | 269 | dna, dns, pna)) |
cf44bbc2 DM |
270 | break; |
271 | ||
e3c71a32 DM |
272 | flags = pbus->get_flags(addr, flags); |
273 | ||
cf44bbc2 DM |
274 | dna = pna; |
275 | dns = pns; | |
b85cdd49 | 276 | dbus = pbus; |
cf44bbc2 DM |
277 | } |
278 | ||
279 | build_res: | |
280 | memset(r, 0, sizeof(*r)); | |
a83f9823 DM |
281 | |
282 | if (of_resource_verbose) | |
283 | printk("%s reg[%d] -> %llx\n", | |
284 | op->node->full_name, index, | |
285 | result); | |
286 | ||
cf44bbc2 | 287 | if (result != OF_BAD_ADDR) { |
95714e12 | 288 | r->start = result & 0xffffffff; |
cf44bbc2 | 289 | r->end = result + size - 1; |
95714e12 | 290 | r->flags = flags | ((result >> 32ULL) & 0xffUL); |
cf44bbc2 DM |
291 | } |
292 | r->name = op->node->name; | |
293 | } | |
294 | } | |
295 | ||
296 | static struct of_device * __init scan_one_device(struct device_node *dp, | |
297 | struct device *parent) | |
298 | { | |
299 | struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL); | |
8271f042 | 300 | const struct linux_prom_irqs *intr; |
3d6e4702 | 301 | struct dev_archdata *sd; |
8f96cd1a | 302 | int len, i; |
cf44bbc2 DM |
303 | |
304 | if (!op) | |
305 | return NULL; | |
306 | ||
3d6e4702 DM |
307 | sd = &op->dev.archdata; |
308 | sd->prom_node = dp; | |
309 | sd->op = op; | |
310 | ||
cf44bbc2 DM |
311 | op->node = dp; |
312 | ||
313 | op->clock_freq = of_getintprop_default(dp, "clock-frequency", | |
314 | (25*1000*1000)); | |
315 | op->portid = of_getintprop_default(dp, "upa-portid", -1); | |
316 | if (op->portid == -1) | |
317 | op->portid = of_getintprop_default(dp, "portid", -1); | |
318 | ||
8f96cd1a DM |
319 | intr = of_get_property(dp, "intr", &len); |
320 | if (intr) { | |
321 | op->num_irqs = len / sizeof(struct linux_prom_irqs); | |
322 | for (i = 0; i < op->num_irqs; i++) | |
323 | op->irqs[i] = intr[i].pri; | |
324 | } else { | |
8271f042 SR |
325 | const unsigned int *irq = |
326 | of_get_property(dp, "interrupts", &len); | |
8f96cd1a DM |
327 | |
328 | if (irq) { | |
329 | op->num_irqs = len / sizeof(unsigned int); | |
330 | for (i = 0; i < op->num_irqs; i++) | |
331 | op->irqs[i] = irq[i]; | |
332 | } else { | |
333 | op->num_irqs = 0; | |
334 | } | |
335 | } | |
336 | if (sparc_cpu_model == sun4d) { | |
337 | static int pil_to_sbus[] = { | |
338 | 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0, | |
339 | }; | |
9d7ab1f4 | 340 | struct device_node *io_unit, *sbi = dp->parent; |
8271f042 | 341 | const struct linux_prom_registers *regs; |
9d7ab1f4 DM |
342 | int board, slot; |
343 | ||
344 | while (sbi) { | |
345 | if (!strcmp(sbi->name, "sbi")) | |
346 | break; | |
347 | ||
348 | sbi = sbi->parent; | |
349 | } | |
350 | if (!sbi) | |
351 | goto build_resources; | |
8f96cd1a DM |
352 | |
353 | regs = of_get_property(dp, "reg", NULL); | |
9d7ab1f4 DM |
354 | if (!regs) |
355 | goto build_resources; | |
356 | ||
8f96cd1a DM |
357 | slot = regs->which_io; |
358 | ||
9d7ab1f4 DM |
359 | /* If SBI's parent is not io-unit or the io-unit lacks |
360 | * a "board#" property, something is very wrong. | |
361 | */ | |
362 | if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) { | |
363 | printk("%s: Error, parent is not io-unit.\n", | |
364 | sbi->full_name); | |
365 | goto build_resources; | |
366 | } | |
367 | io_unit = sbi->parent; | |
368 | board = of_getintprop_default(io_unit, "board#", -1); | |
369 | if (board == -1) { | |
370 | printk("%s: Error, lacks board# property.\n", | |
371 | io_unit->full_name); | |
372 | goto build_resources; | |
373 | } | |
374 | ||
8f96cd1a DM |
375 | for (i = 0; i < op->num_irqs; i++) { |
376 | int this_irq = op->irqs[i]; | |
377 | int sbusl = pil_to_sbus[this_irq]; | |
378 | ||
379 | if (sbusl) | |
380 | this_irq = (((board + 1) << 5) + | |
381 | (sbusl << 2) + | |
382 | slot); | |
383 | ||
384 | op->irqs[i] = this_irq; | |
385 | } | |
386 | } | |
cf44bbc2 | 387 | |
9d7ab1f4 | 388 | build_resources: |
cf44bbc2 DM |
389 | build_device_resources(op, parent); |
390 | ||
391 | op->dev.parent = parent; | |
37b7754a | 392 | op->dev.bus = &of_platform_bus_type; |
cf44bbc2 | 393 | if (!parent) |
5acdc1fa | 394 | dev_set_name(&op->dev, "root"); |
cf44bbc2 | 395 | else |
5acdc1fa | 396 | dev_set_name(&op->dev, "%08x", dp->node); |
cf44bbc2 DM |
397 | |
398 | if (of_device_register(op)) { | |
399 | printk("%s: Could not register of device.\n", | |
400 | dp->full_name); | |
401 | kfree(op); | |
402 | op = NULL; | |
403 | } | |
404 | ||
405 | return op; | |
406 | } | |
407 | ||
408 | static void __init scan_tree(struct device_node *dp, struct device *parent) | |
409 | { | |
410 | while (dp) { | |
411 | struct of_device *op = scan_one_device(dp, parent); | |
412 | ||
413 | if (op) | |
414 | scan_tree(dp->child, &op->dev); | |
415 | ||
416 | dp = dp->sibling; | |
417 | } | |
418 | } | |
419 | ||
420 | static void __init scan_of_devices(void) | |
421 | { | |
422 | struct device_node *root = of_find_node_by_path("/"); | |
423 | struct of_device *parent; | |
424 | ||
425 | parent = scan_one_device(root, NULL); | |
426 | if (!parent) | |
427 | return; | |
428 | ||
429 | scan_tree(root->child, &parent->dev); | |
430 | } | |
431 | ||
fd531431 DM |
432 | static int __init of_bus_driver_init(void) |
433 | { | |
cf44bbc2 | 434 | int err; |
fd531431 | 435 | |
3f23de10 | 436 | err = of_bus_type_init(&of_platform_bus_type, "of"); |
cf44bbc2 DM |
437 | if (!err) |
438 | scan_of_devices(); | |
439 | ||
440 | return err; | |
fd531431 DM |
441 | } |
442 | ||
443 | postcore_initcall(of_bus_driver_init); | |
444 | ||
a83f9823 DM |
445 | static int __init of_debug(char *str) |
446 | { | |
447 | int val = 0; | |
448 | ||
449 | get_option(&str, &val); | |
450 | if (val & 1) | |
451 | of_resource_verbose = 1; | |
452 | return 1; | |
453 | } | |
454 | ||
455 | __setup("of_debug=", of_debug); |