2 * Copyright (c) 2009-2010 The FreeBSD Foundation
5 * This software was developed by Semihalf under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
39 #include <machine/fdt.h>
40 #include <machine/resource.h>
42 #include <dev/fdt/fdt_common.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 #include <dev/ofw/openfirm.h>
47 #include "ofw_bus_if.h"
50 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
51 printf(fmt,##args); } while (0)
53 #define debugf(fmt, args...)
56 #define FDT_COMPAT_LEN 255
57 #define FDT_TYPE_LEN 64
59 #define FDT_REG_CELLS 4
61 vm_paddr_t fdt_immr_pa;
62 vm_offset_t fdt_immr_va;
63 vm_offset_t fdt_immr_size;
66 fdt_immr_addr(vm_offset_t immr_va)
68 pcell_t ranges[6], *rangesptr;
71 pcell_t addr_cells, size_cells, par_addr_cells;
72 int len, tuple_size, tuples;
75 * Try to access the SOC node directly i.e. through /aliases/.
77 if ((node = OF_finddevice("soc")) != 0)
78 if (fdt_is_compatible_strict(node, "simple-bus"))
81 * Find the node the long way.
83 if ((node = OF_finddevice("/")) == 0)
86 if ((node = fdt_find_compatible(node, "simple-bus", 1)) == 0)
90 if ((fdt_addrsize_cells(node, &addr_cells, &size_cells)) != 0)
93 * Process 'ranges' property.
95 par_addr_cells = fdt_parent_addr_cells(node);
96 if (par_addr_cells > 2)
99 len = OF_getproplen(node, "ranges");
100 if (len > sizeof(ranges))
103 if (OF_getprop(node, "ranges", ranges, sizeof(ranges)) <= 0)
106 tuple_size = sizeof(pcell_t) * (addr_cells + par_addr_cells +
108 tuples = len / tuple_size;
110 if (fdt_ranges_verify(ranges, tuples, par_addr_cells,
111 addr_cells, size_cells)) {
116 rangesptr = &ranges[0];
118 base = fdt_data_get((void *)rangesptr, addr_cells);
119 rangesptr += addr_cells;
120 base += fdt_data_get((void *)rangesptr, par_addr_cells);
121 rangesptr += par_addr_cells;
122 size = fdt_data_get((void *)rangesptr, size_cells);
125 fdt_immr_va = immr_va;
126 fdt_immr_size = size;
132 * This routine is an early-usage version of the ofw_bus_is_compatible() when
133 * the ofw_bus I/F is not available (like early console routines and similar).
134 * Note the buffer has to be on the stack since malloc() is usually not
135 * available in such cases either.
138 fdt_is_compatible(phandle_t node, const char *compatstr)
140 char buf[FDT_COMPAT_LEN];
142 int len, onelen, l, rv;
144 if ((len = OF_getproplen(node, "compatible")) <= 0)
147 compat = (char *)&buf;
148 bzero(compat, FDT_COMPAT_LEN);
150 if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0)
153 onelen = strlen(compatstr);
156 if (strncasecmp(compat, compatstr, onelen) == 0) {
161 /* Slide to the next sub-string. */
162 l = strlen(compat) + 1;
171 fdt_is_compatible_strict(phandle_t node, const char *compatible)
173 char compat[FDT_COMPAT_LEN];
175 if (OF_getproplen(node, "compatible") <= 0)
178 if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0)
181 if (strncasecmp(compat, compatible, FDT_COMPAT_LEN) == 0)
189 fdt_find_compatible(phandle_t start, const char *compat, int strict)
194 * Traverse all children of 'start' node, and find first with
195 * matching 'compatible' property.
197 for (child = OF_child(start); child != 0; child = OF_peer(child))
198 if (fdt_is_compatible(child, compat)) {
200 if (!fdt_is_compatible_strict(child, compat))
208 fdt_is_enabled(phandle_t node)
213 len = OF_getprop_alloc(node, "status", sizeof(char),
217 /* It is OK if no 'status' property. */
220 /* Anything other than 'okay' means disabled. */
222 if (strncmp((char *)stat, "okay", len) == 0)
225 free(stat, M_OFWPROP);
230 fdt_is_type(phandle_t node, const char *typestr)
232 char type[FDT_TYPE_LEN];
234 if (OF_getproplen(node, "device_type") <= 0)
237 if (OF_getprop(node, "device_type", type, FDT_TYPE_LEN) < 0)
240 if (strncasecmp(type, typestr, FDT_TYPE_LEN) == 0)
248 fdt_parent_addr_cells(phandle_t node)
252 /* Find out #address-cells of the superior bus. */
253 if (OF_searchprop(OF_parent(node), "#address-cells", &addr_cells,
254 sizeof(addr_cells)) <= 0)
257 return ((int)fdt32_to_cpu(addr_cells));
261 fdt_data_verify(void *data, int cells)
266 d64 = fdt64_to_cpu(*((uint64_t *)data));
267 if (((d64 >> 32) & 0xffffffffull) != 0 || cells > 2)
275 fdt_pm_is_enabled(phandle_t node)
281 #if defined(SOC_MV_KIRKWOOD) || defined(SOC_MV_DISCOVERY)
288 fdt_data_get(void *data, int cells)
292 return (fdt32_to_cpu(*((uint32_t *)data)));
294 return (fdt64_to_cpu(*((uint64_t *)data)));
298 fdt_addrsize_cells(phandle_t node, int *addr_cells, int *size_cells)
304 * Retrieve #{address,size}-cells.
306 cell_size = sizeof(cell);
307 if (OF_getprop(node, "#address-cells", &cell, cell_size) < cell_size)
309 *addr_cells = fdt32_to_cpu((int)cell);
311 if (OF_getprop(node, "#size-cells", &cell, cell_size) < cell_size)
313 *size_cells = fdt32_to_cpu((int)cell);
315 if (*addr_cells > 3 || *size_cells > 2)
321 fdt_ranges_verify(pcell_t *ranges, int tuples, int par_addr_cells,
322 int this_addr_cells, int this_size_cells)
326 if (par_addr_cells > 2 || this_addr_cells > 2 || this_size_cells > 2)
330 * This is the max size the resource manager can handle for addresses
333 ulsz = sizeof(u_long);
334 if (par_addr_cells <= ulsz && this_addr_cells <= ulsz &&
335 this_size_cells <= ulsz)
336 /* We can handle everything */
340 for (i = 0; i < tuples; i++) {
342 if (fdt_data_verify((void *)ranges, par_addr_cells))
344 ranges += par_addr_cells;
346 if (fdt_data_verify((void *)ranges, this_addr_cells))
348 ranges += this_addr_cells;
350 if (fdt_data_verify((void *)ranges, this_size_cells))
352 ranges += this_size_cells;
358 debugf("using address range >%d-bit not supported\n", ulsz * 8);
363 fdt_data_to_res(pcell_t *data, int addr_cells, int size_cells, u_long *start,
367 /* Address portion. */
368 if (fdt_data_verify((void *)data, addr_cells))
371 *start = fdt_data_get((void *)data, addr_cells);
375 if (fdt_data_verify((void *)data, size_cells))
378 *count = fdt_data_get((void *)data, size_cells);
383 fdt_regsize(phandle_t node, u_long *base, u_long *size)
386 int addr_cells, len, size_cells;
388 if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells))
391 if ((sizeof(pcell_t) * (addr_cells + size_cells)) > sizeof(reg))
394 len = OF_getprop(node, "reg", ®, sizeof(reg));
398 *base = fdt_data_get(®[0], addr_cells);
399 *size = fdt_data_get(®[addr_cells], size_cells);
404 fdt_reg_to_rl(phandle_t node, struct resource_list *rl, u_long base)
406 u_long start, end, count;
407 pcell_t *reg, *regptr;
408 pcell_t addr_cells, size_cells;
409 int tuple_size, tuples;
412 if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells) != 0)
415 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
416 tuples = OF_getprop_alloc(node, "reg", tuple_size, (void **)®);
417 debugf("addr_cells = %d, size_cells = %d\n", addr_cells, size_cells);
418 debugf("tuples = %d, tuple size = %d\n", tuples, tuple_size);
420 /* No 'reg' property in this node. */
424 for (i = 0; i < tuples; i++) {
426 rv = fdt_data_to_res(reg, addr_cells, size_cells, &start,
429 resource_list_free(rl);
432 reg += addr_cells + size_cells;
434 /* Calculate address range relative to base. */
435 start &= 0x000ffffful;
436 start = base + start;
437 end = start + count - 1;
439 debugf("reg addr start = %lx, end = %lx, count = %lx\n", start,
442 resource_list_add(rl, SYS_RES_MEMORY, i, start, end,
448 free(regptr, M_OFWPROP);
453 fdt_intr_decode(phandle_t intr_parent, pcell_t *intr, int *interrupt,
456 fdt_pic_decode_t intr_decode;
459 for (i = 0; fdt_pic_table[i] != NULL; i++) {
461 /* XXX check if pic_handle has interrupt-controller prop? */
463 intr_decode = fdt_pic_table[i];
464 rv = intr_decode(intr_parent, intr, interrupt, trig, pol);
467 /* This was recognized as our PIC and decoded. */
475 fdt_intr_to_rl(phandle_t node, struct resource_list *rl,
476 struct fdt_sense_level *intr_sl)
482 int interrupt, trig, pol;
483 int i, intr_num, irq, rv;
485 if (OF_getproplen(node, "interrupts") <= 0)
486 /* Node does not have 'interrupts' property. */
490 * Find #interrupt-cells of the interrupt domain.
492 if (OF_getprop(node, "interrupt-parent", &iph, sizeof(iph)) <= 0) {
493 debugf("no intr-parent phandle\n");
494 intr_par = OF_parent(node);
496 iph = fdt32_to_cpu(iph);
497 intr_par = OF_instance_to_package(iph);
500 if (OF_getprop(intr_par, "#interrupt-cells", &intr_cells,
501 sizeof(intr_cells)) <= 0) {
502 debugf("no intr-cells defined, defaulting to 1\n");
505 intr_cells = fdt32_to_cpu(intr_cells);
507 intr_num = OF_getprop_alloc(node, "interrupts",
508 intr_cells * sizeof(pcell_t), (void **)&intr);
509 if (intr_num <= 0 || intr_num > DI_MAX_INTR_NUM)
513 for (i = 0; i < intr_num; i++) {
518 if (fdt_intr_decode(intr_par, &intr[i * intr_cells],
519 &interrupt, &trig, &pol) != 0) {
529 debugf("decoded intr = %d, trig = %d, pol = %d\n", interrupt,
532 intr_sl[i].trig = trig;
533 intr_sl[i].pol = pol;
535 irq = FDT_MAP_IRQ(intr_par, interrupt);
536 resource_list_add(rl, SYS_RES_IRQ, i, irq, irq, 1);
540 free(intr, M_OFWPROP);
545 fdt_get_phyaddr(phandle_t node, device_t dev, int *phy_addr, void **phy_sc)
548 ihandle_t phy_ihandle;
549 pcell_t phy_handle, phy_reg;
551 device_t parent, child;
553 if (OF_getprop(node, "phy-handle", (void *)&phy_handle,
554 sizeof(phy_handle)) <= 0)
557 phy_ihandle = (ihandle_t)phy_handle;
558 phy_ihandle = fdt32_to_cpu(phy_ihandle);
559 phy_node = OF_instance_to_package(phy_ihandle);
561 if (OF_getprop(phy_node, "reg", (void *)&phy_reg,
562 sizeof(phy_reg)) <= 0)
565 *phy_addr = fdt32_to_cpu(phy_reg);
568 * Search for softc used to communicate with phy.
572 * Step 1: Search for ancestor of the phy-node with a "phy-handle"
575 phy_node = OF_parent(phy_node);
576 while (phy_node != 0) {
577 if (OF_getprop(phy_node, "phy-handle", (void *)&phy_handle,
578 sizeof(phy_handle)) > 0)
580 phy_node = OF_parent(phy_node);
586 * Step 2: For each device with the same parent and name as ours
587 * compare its node with the one found in step 1, ancestor of phy
588 * node (stored in phy_node).
590 parent = device_get_parent(dev);
592 child = device_find_child(parent, device_get_name(dev), i);
593 while (child != NULL) {
594 if (ofw_bus_get_node(child) == phy_node)
597 child = device_find_child(parent, device_get_name(dev), i);
603 * Use softc of the device found.
605 *phy_sc = (void *)device_get_softc(child);
611 fdt_get_mem_regions(struct mem_region *mr, int *mrcnt, uint32_t *memsize)
613 pcell_t reg[FDT_REG_CELLS * FDT_MEM_REGIONS];
616 uint32_t memory_size;
617 int addr_cells, size_cells;
618 int i, max_size, reg_len, rv, tuple_size, tuples;
620 max_size = sizeof(reg);
621 memory = OF_finddevice("/memory");
627 if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells,
631 if (addr_cells > 2) {
636 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
637 reg_len = OF_getproplen(memory, "reg");
638 if (reg_len <= 0 || reg_len > sizeof(reg)) {
643 if (OF_getprop(memory, "reg", reg, reg_len) <= 0) {
649 tuples = reg_len / tuple_size;
650 regp = (pcell_t *)®
651 for (i = 0; i < tuples; i++) {
653 rv = fdt_data_to_res(regp, addr_cells, size_cells,
654 (u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size);
659 regp += addr_cells + size_cells;
660 memory_size += mr[i].mr_size;
663 if (memory_size == 0) {
669 *memsize = memory_size;