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>
38 #include <sys/limits.h>
40 #include <machine/fdt.h>
41 #include <machine/resource.h>
43 #include <dev/fdt/fdt_common.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/ofw/openfirm.h>
48 #include "ofw_bus_if.h"
51 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
52 printf(fmt,##args); } while (0)
54 #define debugf(fmt, args...)
57 #define FDT_COMPAT_LEN 255
58 #define FDT_TYPE_LEN 64
60 #define FDT_REG_CELLS 4
62 vm_paddr_t fdt_immr_pa;
63 vm_offset_t fdt_immr_va;
64 vm_offset_t fdt_immr_size;
66 struct fdt_ic_list fdt_ic_list_head = SLIST_HEAD_INITIALIZER(fdt_ic_list_head);
69 fdt_get_range(phandle_t node, int range_id, u_long *base, u_long *size)
71 pcell_t ranges[6], *rangesptr;
72 pcell_t addr_cells, size_cells, par_addr_cells;
73 int len, tuple_size, tuples;
75 if ((fdt_addrsize_cells(node, &addr_cells, &size_cells)) != 0)
78 * Process 'ranges' property.
80 par_addr_cells = fdt_parent_addr_cells(node);
81 if (par_addr_cells > 2)
84 len = OF_getproplen(node, "ranges");
85 if (len > sizeof(ranges))
93 if (!(range_id < len))
96 if (OF_getprop(node, "ranges", ranges, sizeof(ranges)) <= 0)
99 tuple_size = sizeof(pcell_t) * (addr_cells + par_addr_cells +
101 tuples = len / tuple_size;
103 if (fdt_ranges_verify(ranges, tuples, par_addr_cells,
104 addr_cells, size_cells)) {
109 rangesptr = &ranges[range_id];
111 *base = fdt_data_get((void *)rangesptr, addr_cells);
112 rangesptr += addr_cells;
113 *base += fdt_data_get((void *)rangesptr, par_addr_cells);
114 rangesptr += par_addr_cells;
115 *size = fdt_data_get((void *)rangesptr, size_cells);
120 fdt_immr_addr(vm_offset_t immr_va)
127 * Try to access the SOC node directly i.e. through /aliases/.
129 if ((node = OF_finddevice("soc")) != 0)
130 if (fdt_is_compatible_strict(node, "simple-bus"))
133 * Find the node the long way.
135 if ((node = OF_finddevice("/")) == 0)
138 if ((node = fdt_find_compatible(node, "simple-bus", 1)) == 0)
142 if ((r = fdt_get_range(node, 0, &base, &size)) == 0) {
144 fdt_immr_va = immr_va;
145 fdt_immr_size = size;
152 * This routine is an early-usage version of the ofw_bus_is_compatible() when
153 * the ofw_bus I/F is not available (like early console routines and similar).
154 * Note the buffer has to be on the stack since malloc() is usually not
155 * available in such cases either.
158 fdt_is_compatible(phandle_t node, const char *compatstr)
160 char buf[FDT_COMPAT_LEN];
162 int len, onelen, l, rv;
164 if ((len = OF_getproplen(node, "compatible")) <= 0)
167 compat = (char *)&buf;
168 bzero(compat, FDT_COMPAT_LEN);
170 if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0)
173 onelen = strlen(compatstr);
176 if (strncasecmp(compat, compatstr, onelen) == 0) {
181 /* Slide to the next sub-string. */
182 l = strlen(compat) + 1;
191 fdt_is_compatible_strict(phandle_t node, const char *compatible)
193 char compat[FDT_COMPAT_LEN];
195 if (OF_getproplen(node, "compatible") <= 0)
198 if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0)
201 if (strncasecmp(compat, compatible, FDT_COMPAT_LEN) == 0)
209 fdt_find_compatible(phandle_t start, const char *compat, int strict)
214 * Traverse all children of 'start' node, and find first with
215 * matching 'compatible' property.
217 for (child = OF_child(start); child != 0; child = OF_peer(child))
218 if (fdt_is_compatible(child, compat)) {
220 if (!fdt_is_compatible_strict(child, compat))
228 fdt_is_enabled(phandle_t node)
233 len = OF_getprop_alloc(node, "status", sizeof(char),
237 /* It is OK if no 'status' property. */
240 /* Anything other than 'okay' means disabled. */
242 if (strncmp((char *)stat, "okay", len) == 0)
245 free(stat, M_OFWPROP);
250 fdt_is_type(phandle_t node, const char *typestr)
252 char type[FDT_TYPE_LEN];
254 if (OF_getproplen(node, "device_type") <= 0)
257 if (OF_getprop(node, "device_type", type, FDT_TYPE_LEN) < 0)
260 if (strncasecmp(type, typestr, FDT_TYPE_LEN) == 0)
268 fdt_parent_addr_cells(phandle_t node)
272 /* Find out #address-cells of the superior bus. */
273 if (OF_searchprop(OF_parent(node), "#address-cells", &addr_cells,
274 sizeof(addr_cells)) <= 0)
277 return ((int)fdt32_to_cpu(addr_cells));
281 fdt_data_verify(void *data, int cells)
286 d64 = fdt64_to_cpu(*((uint64_t *)data));
287 if (((d64 >> 32) & 0xffffffffull) != 0 || cells > 2)
295 fdt_pm_is_enabled(phandle_t node)
301 #if defined(SOC_MV_KIRKWOOD) || defined(SOC_MV_DISCOVERY)
308 fdt_data_get(void *data, int cells)
312 return (fdt32_to_cpu(*((uint32_t *)data)));
314 return (fdt64_to_cpu(*((uint64_t *)data)));
318 fdt_addrsize_cells(phandle_t node, int *addr_cells, int *size_cells)
324 * Retrieve #{address,size}-cells.
326 cell_size = sizeof(cell);
327 if (OF_getprop(node, "#address-cells", &cell, cell_size) < cell_size)
329 *addr_cells = fdt32_to_cpu((int)cell);
331 if (OF_getprop(node, "#size-cells", &cell, cell_size) < cell_size)
333 *size_cells = fdt32_to_cpu((int)cell);
335 if (*addr_cells > 3 || *size_cells > 2)
341 fdt_ranges_verify(pcell_t *ranges, int tuples, int par_addr_cells,
342 int this_addr_cells, int this_size_cells)
346 if (par_addr_cells > 2 || this_addr_cells > 2 || this_size_cells > 2)
350 * This is the max size the resource manager can handle for addresses
353 ulsz = sizeof(u_long);
354 if (par_addr_cells <= ulsz && this_addr_cells <= ulsz &&
355 this_size_cells <= ulsz)
356 /* We can handle everything */
360 for (i = 0; i < tuples; i++) {
362 if (fdt_data_verify((void *)ranges, par_addr_cells))
364 ranges += par_addr_cells;
366 if (fdt_data_verify((void *)ranges, this_addr_cells))
368 ranges += this_addr_cells;
370 if (fdt_data_verify((void *)ranges, this_size_cells))
372 ranges += this_size_cells;
378 debugf("using address range >%d-bit not supported\n", ulsz * 8);
383 fdt_data_to_res(pcell_t *data, int addr_cells, int size_cells, u_long *start,
387 /* Address portion. */
388 if (fdt_data_verify((void *)data, addr_cells))
391 *start = fdt_data_get((void *)data, addr_cells);
395 if (fdt_data_verify((void *)data, size_cells))
398 *count = fdt_data_get((void *)data, size_cells);
403 fdt_regsize(phandle_t node, u_long *base, u_long *size)
406 int addr_cells, len, size_cells;
408 if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells))
411 if ((sizeof(pcell_t) * (addr_cells + size_cells)) > sizeof(reg))
414 len = OF_getprop(node, "reg", ®, sizeof(reg));
418 *base = fdt_data_get(®[0], addr_cells);
419 *size = fdt_data_get(®[addr_cells], size_cells);
424 fdt_reg_to_rl(phandle_t node, struct resource_list *rl)
426 u_long end, count, start;
427 pcell_t *reg, *regptr;
428 pcell_t addr_cells, size_cells;
429 int tuple_size, tuples;
431 long busaddr, bussize;
433 if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells) != 0)
435 if (fdt_get_range(OF_parent(node), 0, &busaddr, &bussize)) {
440 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
441 tuples = OF_getprop_alloc(node, "reg", tuple_size, (void **)®);
442 debugf("addr_cells = %d, size_cells = %d\n", addr_cells, size_cells);
443 debugf("tuples = %d, tuple size = %d\n", tuples, tuple_size);
445 /* No 'reg' property in this node. */
449 for (i = 0; i < tuples; i++) {
451 rv = fdt_data_to_res(reg, addr_cells, size_cells, &start,
454 resource_list_free(rl);
457 reg += addr_cells + size_cells;
459 /* Calculate address range relative to base. */
461 end = start + count - 1;
463 debugf("reg addr start = %lx, end = %lx, count = %lx\n", start,
466 resource_list_add(rl, SYS_RES_MEMORY, i, start, end,
472 free(regptr, M_OFWPROP);
477 fdt_intr_decode(phandle_t intr_parent, pcell_t *intr, int *interrupt,
480 fdt_pic_decode_t intr_decode;
483 for (i = 0; fdt_pic_table[i] != NULL; i++) {
485 /* XXX check if pic_handle has interrupt-controller prop? */
487 intr_decode = fdt_pic_table[i];
488 rv = intr_decode(intr_parent, intr, interrupt, trig, pol);
491 /* This was recognized as our PIC and decoded. */
499 fdt_intr_to_rl(phandle_t node, struct resource_list *rl,
500 struct fdt_sense_level *intr_sl)
506 int interrupt, trig, pol;
507 int i, intr_num, irq, rv;
509 if (OF_getproplen(node, "interrupts") <= 0)
510 /* Node does not have 'interrupts' property. */
514 * Find #interrupt-cells of the interrupt domain.
516 if (OF_getprop(node, "interrupt-parent", &iph, sizeof(iph)) <= 0) {
517 debugf("no intr-parent phandle\n");
518 intr_par = OF_parent(node);
520 iph = fdt32_to_cpu(iph);
521 intr_par = OF_instance_to_package(iph);
524 if (OF_getprop(intr_par, "#interrupt-cells", &intr_cells,
525 sizeof(intr_cells)) <= 0) {
526 debugf("no intr-cells defined, defaulting to 1\n");
530 intr_cells = fdt32_to_cpu(intr_cells);
532 intr_num = OF_getprop_alloc(node, "interrupts",
533 intr_cells * sizeof(pcell_t), (void **)&intr);
534 if (intr_num <= 0 || intr_num > DI_MAX_INTR_NUM)
538 for (i = 0; i < intr_num; i++) {
543 if (fdt_intr_decode(intr_par, &intr[i * intr_cells],
544 &interrupt, &trig, &pol) != 0) {
554 debugf("decoded intr = %d, trig = %d, pol = %d\n", interrupt,
557 intr_sl[i].trig = trig;
558 intr_sl[i].pol = pol;
560 irq = FDT_MAP_IRQ(intr_par, interrupt);
561 resource_list_add(rl, SYS_RES_IRQ, i, irq, irq, 1);
565 free(intr, M_OFWPROP);
570 fdt_get_phyaddr(phandle_t node, device_t dev, int *phy_addr, void **phy_sc)
573 ihandle_t phy_ihandle;
574 pcell_t phy_handle, phy_reg;
576 device_t parent, child;
578 if (OF_getprop(node, "phy-handle", (void *)&phy_handle,
579 sizeof(phy_handle)) <= 0)
582 phy_ihandle = (ihandle_t)phy_handle;
583 phy_ihandle = fdt32_to_cpu(phy_ihandle);
584 phy_node = OF_instance_to_package(phy_ihandle);
586 if (OF_getprop(phy_node, "reg", (void *)&phy_reg,
587 sizeof(phy_reg)) <= 0)
590 *phy_addr = fdt32_to_cpu(phy_reg);
593 * Search for softc used to communicate with phy.
597 * Step 1: Search for ancestor of the phy-node with a "phy-handle"
600 phy_node = OF_parent(phy_node);
601 while (phy_node != 0) {
602 if (OF_getprop(phy_node, "phy-handle", (void *)&phy_handle,
603 sizeof(phy_handle)) > 0)
605 phy_node = OF_parent(phy_node);
611 * Step 2: For each device with the same parent and name as ours
612 * compare its node with the one found in step 1, ancestor of phy
613 * node (stored in phy_node).
615 parent = device_get_parent(dev);
617 child = device_find_child(parent, device_get_name(dev), i);
618 while (child != NULL) {
619 if (ofw_bus_get_node(child) == phy_node)
622 child = device_find_child(parent, device_get_name(dev), i);
628 * Use softc of the device found.
630 *phy_sc = (void *)device_get_softc(child);
636 fdt_get_reserved_regions(struct mem_region *mr, int *mrcnt)
638 pcell_t reserve[FDT_REG_CELLS * FDT_MEM_REGIONS];
640 phandle_t memory, root;
641 uint32_t memory_size;
642 int addr_cells, size_cells;
643 int i, max_size, res_len, rv, tuple_size, tuples;
645 max_size = sizeof(reserve);
646 root = OF_finddevice("/");
647 memory = OF_finddevice("/memory");
653 if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells,
657 if (addr_cells > 2) {
662 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
664 res_len = OF_getproplen(root, "memreserve");
665 if (res_len <= 0 || res_len > sizeof(reserve)) {
670 if (OF_getprop(root, "memreserve", reserve, res_len) <= 0) {
676 tuples = res_len / tuple_size;
677 reservep = (pcell_t *)&reserve;
678 for (i = 0; i < tuples; i++) {
680 rv = fdt_data_to_res(reservep, addr_cells, size_cells,
681 (u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size);
686 reservep += addr_cells + size_cells;
696 fdt_get_mem_regions(struct mem_region *mr, int *mrcnt, uint32_t *memsize)
698 pcell_t reg[FDT_REG_CELLS * FDT_MEM_REGIONS];
701 uint32_t memory_size;
702 int addr_cells, size_cells;
703 int i, max_size, reg_len, rv, tuple_size, tuples;
705 max_size = sizeof(reg);
706 memory = OF_finddevice("/memory");
712 if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells,
716 if (addr_cells > 2) {
721 tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
722 reg_len = OF_getproplen(memory, "reg");
723 if (reg_len <= 0 || reg_len > sizeof(reg)) {
728 if (OF_getprop(memory, "reg", reg, reg_len) <= 0) {
734 tuples = reg_len / tuple_size;
735 regp = (pcell_t *)®
736 for (i = 0; i < tuples; i++) {
738 rv = fdt_data_to_res(regp, addr_cells, size_cells,
739 (u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size);
744 regp += addr_cells + size_cells;
745 memory_size += mr[i].mr_size;
748 if (memory_size == 0) {
754 *memsize = memory_size;
761 fdt_get_unit(device_t dev)
765 name = ofw_bus_get_name(dev);
766 name = strchr(name, '@') + 1;
768 return (strtol(name,NULL,0));