diff options
Diffstat (limited to 'arch/sparc/kernel/of_device.c')
-rw-r--r-- | arch/sparc/kernel/of_device.c | 477 |
1 files changed, 475 insertions, 2 deletions
diff --git a/arch/sparc/kernel/of_device.c b/arch/sparc/kernel/of_device.c index 80a809478781..bc956c530376 100644 --- a/arch/sparc/kernel/of_device.c +++ b/arch/sparc/kernel/of_device.c | |||
@@ -129,6 +129,26 @@ static int of_device_resume(struct device * dev) | |||
129 | return error; | 129 | return error; |
130 | } | 130 | } |
131 | 131 | ||
132 | static int node_match(struct device *dev, void *data) | ||
133 | { | ||
134 | struct of_device *op = to_of_device(dev); | ||
135 | struct device_node *dp = data; | ||
136 | |||
137 | return (op->node == dp); | ||
138 | } | ||
139 | |||
140 | struct of_device *of_find_device_by_node(struct device_node *dp) | ||
141 | { | ||
142 | struct device *dev = bus_find_device(&of_bus_type, NULL, | ||
143 | dp, node_match); | ||
144 | |||
145 | if (dev) | ||
146 | return to_of_device(dev); | ||
147 | |||
148 | return NULL; | ||
149 | } | ||
150 | EXPORT_SYMBOL(of_find_device_by_node); | ||
151 | |||
132 | #ifdef CONFIG_PCI | 152 | #ifdef CONFIG_PCI |
133 | struct bus_type ebus_bus_type = { | 153 | struct bus_type ebus_bus_type = { |
134 | .name = "ebus", | 154 | .name = "ebus", |
@@ -153,10 +173,459 @@ struct bus_type sbus_bus_type = { | |||
153 | EXPORT_SYMBOL(sbus_bus_type); | 173 | EXPORT_SYMBOL(sbus_bus_type); |
154 | #endif | 174 | #endif |
155 | 175 | ||
176 | struct bus_type of_bus_type = { | ||
177 | .name = "of", | ||
178 | .match = of_platform_bus_match, | ||
179 | .probe = of_device_probe, | ||
180 | .remove = of_device_remove, | ||
181 | .suspend = of_device_suspend, | ||
182 | .resume = of_device_resume, | ||
183 | }; | ||
184 | EXPORT_SYMBOL(of_bus_type); | ||
185 | |||
186 | static inline u64 of_read_addr(u32 *cell, int size) | ||
187 | { | ||
188 | u64 r = 0; | ||
189 | while (size--) | ||
190 | r = (r << 32) | *(cell++); | ||
191 | return r; | ||
192 | } | ||
193 | |||
194 | static void __init get_cells(struct device_node *dp, | ||
195 | int *addrc, int *sizec) | ||
196 | { | ||
197 | if (addrc) | ||
198 | *addrc = of_n_addr_cells(dp); | ||
199 | if (sizec) | ||
200 | *sizec = of_n_size_cells(dp); | ||
201 | } | ||
202 | |||
203 | /* Max address size we deal with */ | ||
204 | #define OF_MAX_ADDR_CELLS 4 | ||
205 | |||
206 | struct of_bus { | ||
207 | const char *name; | ||
208 | const char *addr_prop_name; | ||
209 | int (*match)(struct device_node *parent); | ||
210 | void (*count_cells)(struct device_node *child, | ||
211 | int *addrc, int *sizec); | ||
212 | u64 (*map)(u32 *addr, u32 *range, int na, int ns, int pna); | ||
213 | int (*translate)(u32 *addr, u64 offset, int na); | ||
214 | unsigned int (*get_flags)(u32 *addr); | ||
215 | }; | ||
216 | |||
217 | /* | ||
218 | * Default translator (generic bus) | ||
219 | */ | ||
220 | |||
221 | static void of_bus_default_count_cells(struct device_node *dev, | ||
222 | int *addrc, int *sizec) | ||
223 | { | ||
224 | get_cells(dev, addrc, sizec); | ||
225 | } | ||
226 | |||
227 | static u64 of_bus_default_map(u32 *addr, u32 *range, int na, int ns, int pna) | ||
228 | { | ||
229 | u64 cp, s, da; | ||
230 | |||
231 | cp = of_read_addr(range, na); | ||
232 | s = of_read_addr(range + na + pna, ns); | ||
233 | da = of_read_addr(addr, na); | ||
234 | |||
235 | if (da < cp || da >= (cp + s)) | ||
236 | return OF_BAD_ADDR; | ||
237 | return da - cp; | ||
238 | } | ||
239 | |||
240 | static int of_bus_default_translate(u32 *addr, u64 offset, int na) | ||
241 | { | ||
242 | u64 a = of_read_addr(addr, na); | ||
243 | memset(addr, 0, na * 4); | ||
244 | a += offset; | ||
245 | if (na > 1) | ||
246 | addr[na - 2] = a >> 32; | ||
247 | addr[na - 1] = a & 0xffffffffu; | ||
248 | |||
249 | return 0; | ||
250 | } | ||
251 | |||
252 | static unsigned int of_bus_default_get_flags(u32 *addr) | ||
253 | { | ||
254 | return IORESOURCE_MEM; | ||
255 | } | ||
256 | |||
257 | |||
258 | /* | ||
259 | * PCI bus specific translator | ||
260 | */ | ||
261 | |||
262 | static int of_bus_pci_match(struct device_node *np) | ||
263 | { | ||
264 | return !strcmp(np->type, "pci") || !strcmp(np->type, "pciex"); | ||
265 | } | ||
266 | |||
267 | static void of_bus_pci_count_cells(struct device_node *np, | ||
268 | int *addrc, int *sizec) | ||
269 | { | ||
270 | if (addrc) | ||
271 | *addrc = 3; | ||
272 | if (sizec) | ||
273 | *sizec = 2; | ||
274 | } | ||
275 | |||
276 | static u64 of_bus_pci_map(u32 *addr, u32 *range, int na, int ns, int pna) | ||
277 | { | ||
278 | u64 cp, s, da; | ||
279 | |||
280 | /* Check address type match */ | ||
281 | if ((addr[0] ^ range[0]) & 0x03000000) | ||
282 | return OF_BAD_ADDR; | ||
283 | |||
284 | /* Read address values, skipping high cell */ | ||
285 | cp = of_read_addr(range + 1, na - 1); | ||
286 | s = of_read_addr(range + na + pna, ns); | ||
287 | da = of_read_addr(addr + 1, na - 1); | ||
288 | |||
289 | if (da < cp || da >= (cp + s)) | ||
290 | return OF_BAD_ADDR; | ||
291 | return da - cp; | ||
292 | } | ||
293 | |||
294 | static int of_bus_pci_translate(u32 *addr, u64 offset, int na) | ||
295 | { | ||
296 | return of_bus_default_translate(addr + 1, offset, na - 1); | ||
297 | } | ||
298 | |||
299 | static unsigned int of_bus_pci_get_flags(u32 *addr) | ||
300 | { | ||
301 | unsigned int flags = 0; | ||
302 | u32 w = addr[0]; | ||
303 | |||
304 | switch((w >> 24) & 0x03) { | ||
305 | case 0x01: | ||
306 | flags |= IORESOURCE_IO; | ||
307 | case 0x02: /* 32 bits */ | ||
308 | case 0x03: /* 64 bits */ | ||
309 | flags |= IORESOURCE_MEM; | ||
310 | } | ||
311 | if (w & 0x40000000) | ||
312 | flags |= IORESOURCE_PREFETCH; | ||
313 | return flags; | ||
314 | } | ||
315 | |||
316 | /* | ||
317 | * SBUS bus specific translator | ||
318 | */ | ||
319 | |||
320 | static int of_bus_sbus_match(struct device_node *np) | ||
321 | { | ||
322 | return !strcmp(np->name, "sbus") || | ||
323 | !strcmp(np->name, "sbi"); | ||
324 | } | ||
325 | |||
326 | static void of_bus_sbus_count_cells(struct device_node *child, | ||
327 | int *addrc, int *sizec) | ||
328 | { | ||
329 | if (addrc) | ||
330 | *addrc = 2; | ||
331 | if (sizec) | ||
332 | *sizec = 1; | ||
333 | } | ||
334 | |||
335 | static u64 of_bus_sbus_map(u32 *addr, u32 *range, int na, int ns, int pna) | ||
336 | { | ||
337 | return of_bus_default_map(addr, range, na, ns, pna); | ||
338 | } | ||
339 | |||
340 | static int of_bus_sbus_translate(u32 *addr, u64 offset, int na) | ||
341 | { | ||
342 | return of_bus_default_translate(addr, offset, na); | ||
343 | } | ||
344 | |||
345 | static unsigned int of_bus_sbus_get_flags(u32 *addr) | ||
346 | { | ||
347 | return IORESOURCE_MEM; | ||
348 | } | ||
349 | |||
350 | |||
351 | /* | ||
352 | * Array of bus specific translators | ||
353 | */ | ||
354 | |||
355 | static struct of_bus of_busses[] = { | ||
356 | /* PCI */ | ||
357 | { | ||
358 | .name = "pci", | ||
359 | .addr_prop_name = "assigned-addresses", | ||
360 | .match = of_bus_pci_match, | ||
361 | .count_cells = of_bus_pci_count_cells, | ||
362 | .map = of_bus_pci_map, | ||
363 | .translate = of_bus_pci_translate, | ||
364 | .get_flags = of_bus_pci_get_flags, | ||
365 | }, | ||
366 | /* SBUS */ | ||
367 | { | ||
368 | .name = "sbus", | ||
369 | .addr_prop_name = "reg", | ||
370 | .match = of_bus_sbus_match, | ||
371 | .count_cells = of_bus_sbus_count_cells, | ||
372 | .map = of_bus_sbus_map, | ||
373 | .translate = of_bus_sbus_translate, | ||
374 | .get_flags = of_bus_sbus_get_flags, | ||
375 | }, | ||
376 | /* Default */ | ||
377 | { | ||
378 | .name = "default", | ||
379 | .addr_prop_name = "reg", | ||
380 | .match = NULL, | ||
381 | .count_cells = of_bus_default_count_cells, | ||
382 | .map = of_bus_default_map, | ||
383 | .translate = of_bus_default_translate, | ||
384 | .get_flags = of_bus_default_get_flags, | ||
385 | }, | ||
386 | }; | ||
387 | |||
388 | static struct of_bus *of_match_bus(struct device_node *np) | ||
389 | { | ||
390 | int i; | ||
391 | |||
392 | for (i = 0; i < ARRAY_SIZE(of_busses); i ++) | ||
393 | if (!of_busses[i].match || of_busses[i].match(np)) | ||
394 | return &of_busses[i]; | ||
395 | BUG(); | ||
396 | return NULL; | ||
397 | } | ||
398 | |||
399 | static int __init build_one_resource(struct device_node *parent, | ||
400 | struct of_bus *bus, | ||
401 | struct of_bus *pbus, | ||
402 | u32 *addr, | ||
403 | int na, int ns, int pna) | ||
404 | { | ||
405 | u32 *ranges; | ||
406 | unsigned int rlen; | ||
407 | int rone; | ||
408 | u64 offset = OF_BAD_ADDR; | ||
409 | |||
410 | ranges = of_get_property(parent, "ranges", &rlen); | ||
411 | if (ranges == NULL || rlen == 0) { | ||
412 | offset = of_read_addr(addr, na); | ||
413 | memset(addr, 0, pna * 4); | ||
414 | goto finish; | ||
415 | } | ||
416 | |||
417 | /* Now walk through the ranges */ | ||
418 | rlen /= 4; | ||
419 | rone = na + pna + ns; | ||
420 | for (; rlen >= rone; rlen -= rone, ranges += rone) { | ||
421 | offset = bus->map(addr, ranges, na, ns, pna); | ||
422 | if (offset != OF_BAD_ADDR) | ||
423 | break; | ||
424 | } | ||
425 | if (offset == OF_BAD_ADDR) | ||
426 | return 1; | ||
427 | |||
428 | memcpy(addr, ranges + na, 4 * pna); | ||
429 | |||
430 | finish: | ||
431 | /* Translate it into parent bus space */ | ||
432 | return pbus->translate(addr, offset, pna); | ||
433 | } | ||
434 | |||
435 | static void __init build_device_resources(struct of_device *op, | ||
436 | struct device *parent) | ||
437 | { | ||
438 | struct of_device *p_op; | ||
439 | struct of_bus *bus; | ||
440 | int na, ns; | ||
441 | int index, num_reg; | ||
442 | void *preg; | ||
443 | |||
444 | if (!parent) | ||
445 | return; | ||
446 | |||
447 | p_op = to_of_device(parent); | ||
448 | bus = of_match_bus(p_op->node); | ||
449 | bus->count_cells(op->node, &na, &ns); | ||
450 | |||
451 | preg = of_get_property(op->node, bus->addr_prop_name, &num_reg); | ||
452 | if (!preg || num_reg == 0) | ||
453 | return; | ||
454 | |||
455 | /* Convert to num-cells. */ | ||
456 | num_reg /= 4; | ||
457 | |||
458 | /* Conver to num-entries. */ | ||
459 | num_reg /= na + ns; | ||
460 | |||
461 | for (index = 0; index < num_reg; index++) { | ||
462 | struct resource *r = &op->resource[index]; | ||
463 | u32 addr[OF_MAX_ADDR_CELLS]; | ||
464 | u32 *reg = (preg + (index * ((na + ns) * 4))); | ||
465 | struct device_node *dp = op->node; | ||
466 | struct device_node *pp = p_op->node; | ||
467 | struct of_bus *pbus; | ||
468 | u64 size, result = OF_BAD_ADDR; | ||
469 | unsigned long flags; | ||
470 | int dna, dns; | ||
471 | int pna, pns; | ||
472 | |||
473 | size = of_read_addr(reg + na, ns); | ||
474 | flags = bus->get_flags(reg); | ||
475 | |||
476 | memcpy(addr, reg, na * 4); | ||
477 | |||
478 | /* If the immediate parent has no ranges property to apply, | ||
479 | * just use a 1<->1 mapping. | ||
480 | */ | ||
481 | if (of_find_property(pp, "ranges", NULL) == NULL) { | ||
482 | result = of_read_addr(addr, na); | ||
483 | goto build_res; | ||
484 | } | ||
485 | |||
486 | dna = na; | ||
487 | dns = ns; | ||
488 | |||
489 | while (1) { | ||
490 | dp = pp; | ||
491 | pp = dp->parent; | ||
492 | if (!pp) { | ||
493 | result = of_read_addr(addr, dna); | ||
494 | break; | ||
495 | } | ||
496 | |||
497 | pbus = of_match_bus(pp); | ||
498 | pbus->count_cells(dp, &pna, &pns); | ||
499 | |||
500 | if (build_one_resource(dp, bus, pbus, addr, dna, dns, pna)) | ||
501 | break; | ||
502 | |||
503 | dna = pna; | ||
504 | dns = pns; | ||
505 | bus = pbus; | ||
506 | } | ||
507 | |||
508 | build_res: | ||
509 | memset(r, 0, sizeof(*r)); | ||
510 | if (result != OF_BAD_ADDR) { | ||
511 | r->start = result & 0xffffffff; | ||
512 | r->end = result + size - 1; | ||
513 | r->flags = flags | ((result >> 32ULL) & 0xffUL); | ||
514 | } else { | ||
515 | r->start = ~0UL; | ||
516 | r->end = ~0UL; | ||
517 | } | ||
518 | r->name = op->node->name; | ||
519 | } | ||
520 | } | ||
521 | |||
522 | static struct of_device * __init scan_one_device(struct device_node *dp, | ||
523 | struct device *parent) | ||
524 | { | ||
525 | struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL); | ||
526 | struct linux_prom_irqs *intr; | ||
527 | int len, i; | ||
528 | |||
529 | if (!op) | ||
530 | return NULL; | ||
531 | |||
532 | op->node = dp; | ||
533 | |||
534 | op->clock_freq = of_getintprop_default(dp, "clock-frequency", | ||
535 | (25*1000*1000)); | ||
536 | op->portid = of_getintprop_default(dp, "upa-portid", -1); | ||
537 | if (op->portid == -1) | ||
538 | op->portid = of_getintprop_default(dp, "portid", -1); | ||
539 | |||
540 | intr = of_get_property(dp, "intr", &len); | ||
541 | if (intr) { | ||
542 | op->num_irqs = len / sizeof(struct linux_prom_irqs); | ||
543 | for (i = 0; i < op->num_irqs; i++) | ||
544 | op->irqs[i] = intr[i].pri; | ||
545 | } else { | ||
546 | unsigned int *irq = of_get_property(dp, "interrupts", &len); | ||
547 | |||
548 | if (irq) { | ||
549 | op->num_irqs = len / sizeof(unsigned int); | ||
550 | for (i = 0; i < op->num_irqs; i++) | ||
551 | op->irqs[i] = irq[i]; | ||
552 | } else { | ||
553 | op->num_irqs = 0; | ||
554 | } | ||
555 | } | ||
556 | if (sparc_cpu_model == sun4d) { | ||
557 | static int pil_to_sbus[] = { | ||
558 | 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0, | ||
559 | }; | ||
560 | struct device_node *busp = dp->parent; | ||
561 | struct linux_prom_registers *regs; | ||
562 | int board = of_getintprop_default(busp, "board#", 0); | ||
563 | int slot; | ||
564 | |||
565 | regs = of_get_property(dp, "reg", NULL); | ||
566 | slot = regs->which_io; | ||
567 | |||
568 | for (i = 0; i < op->num_irqs; i++) { | ||
569 | int this_irq = op->irqs[i]; | ||
570 | int sbusl = pil_to_sbus[this_irq]; | ||
571 | |||
572 | if (sbusl) | ||
573 | this_irq = (((board + 1) << 5) + | ||
574 | (sbusl << 2) + | ||
575 | slot); | ||
576 | |||
577 | op->irqs[i] = this_irq; | ||
578 | } | ||
579 | } | ||
580 | |||
581 | build_device_resources(op, parent); | ||
582 | |||
583 | op->dev.parent = parent; | ||
584 | op->dev.bus = &of_bus_type; | ||
585 | if (!parent) | ||
586 | strcpy(op->dev.bus_id, "root"); | ||
587 | else | ||
588 | strcpy(op->dev.bus_id, dp->path_component_name); | ||
589 | |||
590 | if (of_device_register(op)) { | ||
591 | printk("%s: Could not register of device.\n", | ||
592 | dp->full_name); | ||
593 | kfree(op); | ||
594 | op = NULL; | ||
595 | } | ||
596 | |||
597 | return op; | ||
598 | } | ||
599 | |||
600 | static void __init scan_tree(struct device_node *dp, struct device *parent) | ||
601 | { | ||
602 | while (dp) { | ||
603 | struct of_device *op = scan_one_device(dp, parent); | ||
604 | |||
605 | if (op) | ||
606 | scan_tree(dp->child, &op->dev); | ||
607 | |||
608 | dp = dp->sibling; | ||
609 | } | ||
610 | } | ||
611 | |||
612 | static void __init scan_of_devices(void) | ||
613 | { | ||
614 | struct device_node *root = of_find_node_by_path("/"); | ||
615 | struct of_device *parent; | ||
616 | |||
617 | parent = scan_one_device(root, NULL); | ||
618 | if (!parent) | ||
619 | return; | ||
620 | |||
621 | scan_tree(root->child, &parent->dev); | ||
622 | } | ||
623 | |||
156 | static int __init of_bus_driver_init(void) | 624 | static int __init of_bus_driver_init(void) |
157 | { | 625 | { |
158 | int err = 0; | 626 | int err; |
159 | 627 | ||
628 | err = bus_register(&of_bus_type); | ||
160 | #ifdef CONFIG_PCI | 629 | #ifdef CONFIG_PCI |
161 | if (!err) | 630 | if (!err) |
162 | err = bus_register(&ebus_bus_type); | 631 | err = bus_register(&ebus_bus_type); |
@@ -165,7 +634,11 @@ static int __init of_bus_driver_init(void) | |||
165 | if (!err) | 634 | if (!err) |
166 | err = bus_register(&sbus_bus_type); | 635 | err = bus_register(&sbus_bus_type); |
167 | #endif | 636 | #endif |
168 | return 0; | 637 | |
638 | if (!err) | ||
639 | scan_of_devices(); | ||
640 | |||
641 | return err; | ||
169 | } | 642 | } |
170 | 643 | ||
171 | postcore_initcall(of_bus_driver_init); | 644 | postcore_initcall(of_bus_driver_init); |