Import the common Flattened Device Tree infrastructure.

o fdtbus(4) - the main abstract bus driver for all FDT-compliant systems. This
  is a direct replacement for the many incompatible bus drivers grouping
  integrated peripherals on embedded platforms (like obio(4), ocpbus(4) etc.)

o simplebus(4) - bus driver representing ePAPR style 'simple-bus' node, which
  is an umbrella device for most of the integrated peripherals on a typical
  system-on-chip device.

o Other components (common routines library, PCI node processing helper
  functions)

Reviewed by:	imp
Sponsored by:	The FreeBSD Foundation
This commit is contained in:
raj 2010-06-02 17:17:45 +00:00
parent f05cc30a98
commit 51b403ec50
6 changed files with 2140 additions and 0 deletions

629
sys/dev/fdt/fdt_common.c Normal file
View File

@ -0,0 +1,629 @@
/*-
* Copyright (c) 2009-2010 The FreeBSD Foundation
* All rights reserved.
*
* This software was developed by Semihalf under sponsorship from
* the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <machine/fdt.h>
#include <machine/resource.h>
#include <dev/fdt/fdt_common.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/ofw/openfirm.h>
#include "ofw_bus_if.h"
#ifdef DEBUG
#define debugf(fmt, args...) do { printf("%s(): ", __func__); \
printf(fmt,##args); } while (0)
#else
#define debugf(fmt, args...)
#endif
#define FDT_COMPAT_LEN 255
#define FDT_TYPE_LEN 64
#define FDT_REG_CELLS 4
vm_paddr_t fdt_immr_pa;
vm_offset_t fdt_immr_va;
vm_offset_t fdt_immr_size;
int
fdt_immr_addr(void)
{
pcell_t ranges[6], *rangesptr;
phandle_t node;
u_long base, size;
pcell_t addr_cells, size_cells, par_addr_cells;
int len, tuple_size, tuples;
/*
* Try to access the SOC node directly i.e. through /aliases/.
*/
if ((node = OF_finddevice("soc")) != 0)
if (fdt_is_compatible_strict(node, "simple-bus"))
goto moveon;
/*
* Find the node the long way.
*/
if ((node = OF_finddevice("/")) == 0)
return (ENXIO);
if ((node = fdt_find_compatible(node, "simple-bus", 1)) == 0)
return (ENXIO);
moveon:
if ((fdt_addrsize_cells(node, &addr_cells, &size_cells)) != 0)
return (ENXIO);
/*
* Process 'ranges' property.
*/
par_addr_cells = fdt_parent_addr_cells(node);
if (par_addr_cells > 2)
return (ERANGE);
len = OF_getproplen(node, "ranges");
if (len > sizeof(ranges))
return (ENOMEM);
if (OF_getprop(node, "ranges", ranges, sizeof(ranges)) <= 0)
return (EINVAL);
tuple_size = sizeof(pcell_t) * (addr_cells + par_addr_cells +
size_cells);
tuples = len / tuple_size;
if (fdt_ranges_verify(ranges, tuples, par_addr_cells,
addr_cells, size_cells)) {
return (ERANGE);
}
base = 0;
size = 0;
rangesptr = &ranges[0];
base = fdt_data_get((void *)rangesptr, addr_cells);
rangesptr += addr_cells;
base += fdt_data_get((void *)rangesptr, par_addr_cells);
rangesptr += par_addr_cells;
size = fdt_data_get((void *)rangesptr, size_cells);
fdt_immr_pa = base;
fdt_immr_va = FDT_IMMR_VA;
fdt_immr_size = size;
return (0);
}
/*
* This routine is an early-usage version of the ofw_bus_is_compatible() when
* the ofw_bus I/F is not available (like early console routines and similar).
* Note the buffer has to be on the stack since malloc() is usually not
* available in such cases either.
*/
int
fdt_is_compatible(phandle_t node, const char *compatstr)
{
char buf[FDT_COMPAT_LEN];
char *compat;
int len, onelen, l, rv;
if ((len = OF_getproplen(node, "compatible")) <= 0)
return (0);
compat = (char *)&buf;
bzero(compat, FDT_COMPAT_LEN);
if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0)
return (0);
onelen = strlen(compatstr);
rv = 0;
while (len > 0) {
if (strncasecmp(compat, compatstr, onelen) == 0) {
/* Found it. */
rv = 1;
break;
}
/* Slide to the next sub-string. */
l = strlen(compat) + 1;
compat += l;
len -= l;
}
return (rv);
}
int
fdt_is_compatible_strict(phandle_t node, const char *compatible)
{
char compat[FDT_COMPAT_LEN];
if (OF_getproplen(node, "compatible") <= 0)
return (0);
if (OF_getprop(node, "compatible", compat, FDT_COMPAT_LEN) < 0)
return (0);
if (strncasecmp(compat, compatible, FDT_COMPAT_LEN) == 0)
/* This fits. */
return (1);
return (0);
}
phandle_t
fdt_find_compatible(phandle_t start, const char *compat, int strict)
{
phandle_t child;
/*
* Traverse all children of 'start' node, and find first with
* matching 'compatible' property.
*/
for (child = OF_child(start); child != 0; child = OF_peer(child))
if (fdt_is_compatible(child, compat)) {
if (strict)
if (!fdt_is_compatible_strict(child, compat))
continue;
return (child);
}
return (0);
}
int
fdt_is_enabled(phandle_t node)
{
char *stat;
int ena, len;
len = OF_getprop_alloc(node, "status", sizeof(char),
(void **)&stat);
if (len <= 0)
/* It is OK if no 'status' property. */
return (1);
/* Anything other than 'okay' means disabled. */
ena = 0;
if (strncmp((char *)stat, "okay", len) == 0)
ena = 1;
free(stat, M_OFWPROP);
return (ena);
}
int
fdt_is_type(phandle_t node, const char *typestr)
{
char type[FDT_TYPE_LEN];
if (OF_getproplen(node, "device_type") <= 0)
return (0);
if (OF_getprop(node, "device_type", type, FDT_TYPE_LEN) < 0)
return (0);
if (strncasecmp(type, typestr, FDT_TYPE_LEN) == 0)
/* This fits. */
return (1);
return (0);
}
int
fdt_parent_addr_cells(phandle_t node)
{
pcell_t addr_cells;
/* Find out #address-cells of the superior bus. */
if (OF_searchprop(OF_parent(node), "#address-cells", &addr_cells,
sizeof(addr_cells)) <= 0)
addr_cells = 2;
return ((int)fdt32_to_cpu(addr_cells));
}
int
fdt_data_verify(void *data, int cells)
{
uint64_t d64;
if (cells > 1) {
d64 = fdt64_to_cpu(*((uint64_t *)data));
if (((d64 >> 32) & 0xffffffffull) != 0 || cells > 2)
return (ERANGE);
}
return (0);
}
int
fdt_pm_is_enabled(phandle_t node)
{
int ret;
ret = 1;
#if defined(SOC_MV_KIRKWOOD) || defined(SOC_MV_DISCOVERY)
ret = fdt_pm(node);
#endif
return (ret);
}
u_long
fdt_data_get(void *data, int cells)
{
if (cells == 1)
return (fdt32_to_cpu(*((uint32_t *)data)));
return (fdt64_to_cpu(*((uint64_t *)data)));
}
int
fdt_addrsize_cells(phandle_t node, int *addr_cells, int *size_cells)
{
pcell_t cell;
int cell_size;
/*
* Retrieve #{address,size}-cells.
*/
cell_size = sizeof(cell);
if (OF_getprop(node, "#address-cells", &cell, cell_size) < cell_size)
cell = 2;
*addr_cells = fdt32_to_cpu((int)cell);
if (OF_getprop(node, "#size-cells", &cell, cell_size) < cell_size)
cell = 1;
*size_cells = fdt32_to_cpu((int)cell);
if (*addr_cells > 3 || *size_cells > 2)
return (ERANGE);
return (0);
}
int
fdt_ranges_verify(pcell_t *ranges, int tuples, int par_addr_cells,
int this_addr_cells, int this_size_cells)
{
int i, rv, ulsz;
if (par_addr_cells > 2 || this_addr_cells > 2 || this_size_cells > 2)
return (ERANGE);
/*
* This is the max size the resource manager can handle for addresses
* and sizes.
*/
ulsz = sizeof(u_long);
if (par_addr_cells <= ulsz && this_addr_cells <= ulsz &&
this_size_cells <= ulsz)
/* We can handle everything */
return (0);
rv = 0;
for (i = 0; i < tuples; i++) {
if (fdt_data_verify((void *)ranges, par_addr_cells))
goto err;
ranges += par_addr_cells;
if (fdt_data_verify((void *)ranges, this_addr_cells))
goto err;
ranges += this_addr_cells;
if (fdt_data_verify((void *)ranges, this_size_cells))
goto err;
ranges += this_size_cells;
}
return (0);
err:
debugf("using address range >%d-bit not supported\n", ulsz * 8);
return (ERANGE);
}
int
fdt_data_to_res(pcell_t *data, int addr_cells, int size_cells, u_long *start,
u_long *count)
{
/* Address portion. */
if (fdt_data_verify((void *)data, addr_cells))
return (ERANGE);
*start = fdt_data_get((void *)data, addr_cells);
data += addr_cells;
/* Size portion. */
if (fdt_data_verify((void *)data, size_cells))
return (ERANGE);
*count = fdt_data_get((void *)data, size_cells);
return (0);
}
int
fdt_regsize(phandle_t node, u_long *base, u_long *size)
{
pcell_t reg[4];
int addr_cells, len, size_cells;
if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells))
return (ENXIO);
if ((sizeof(pcell_t) * (addr_cells + size_cells)) > sizeof(reg))
return (ENOMEM);
len = OF_getprop(node, "reg", &reg, sizeof(reg));
if (len <= 0)
return (EINVAL);
*base = fdt_data_get(&reg[0], addr_cells);
*size = fdt_data_get(&reg[addr_cells], size_cells);
return (0);
}
int
fdt_reg_to_rl(phandle_t node, struct resource_list *rl, u_long base)
{
u_long start, end, count;
pcell_t *reg, *regptr;
pcell_t addr_cells, size_cells;
int tuple_size, tuples;
int i, rv;
if (fdt_addrsize_cells(OF_parent(node), &addr_cells, &size_cells) != 0)
return (ENXIO);
tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
tuples = OF_getprop_alloc(node, "reg", tuple_size, (void **)&reg);
debugf("addr_cells = %d, size_cells = %d\n", addr_cells, size_cells);
debugf("tuples = %d, tuple size = %d\n", tuples, tuple_size);
if (tuples <= 0)
/* No 'reg' property in this node. */
return (0);
regptr = reg;
for (i = 0; i < tuples; i++) {
rv = fdt_data_to_res(reg, addr_cells, size_cells, &start,
&count);
if (rv != 0) {
resource_list_free(rl);
goto out;
}
reg += addr_cells + size_cells;
/* Calculate address range relative to base. */
start &= 0x000ffffful;
start = base + start;
end = start + count - 1;
debugf("reg addr start = %lx, end = %lx, count = %lx\n", start,
end, count);
resource_list_add(rl, SYS_RES_MEMORY, i, start, end,
count);
}
rv = 0;
out:
free(regptr, M_OFWPROP);
return (rv);
}
int
fdt_intr_decode(phandle_t intr_parent, pcell_t *intr, int *interrupt,
int *trig, int *pol)
{
fdt_pic_decode_t intr_decode;
int i, rv;
for (i = 0; fdt_pic_table[i] != NULL; i++) {
/* XXX check if pic_handle has interrupt-controller prop? */
intr_decode = fdt_pic_table[i];
rv = intr_decode(intr_parent, intr, interrupt, trig, pol);
if (rv == 0)
/* This was recognized as our PIC and decoded. */
return (0);
}
return (ENXIO);
}
int
fdt_intr_to_rl(phandle_t node, struct resource_list *rl,
struct fdt_sense_level *intr_sl)
{
phandle_t intr_par;
ihandle_t iph;
pcell_t *intr;
pcell_t intr_cells;
int interrupt, trig, pol;
int i, intr_num, rv;
if (OF_getproplen(node, "interrupts") <= 0)
/* Node does not have 'interrupts' property. */
return (0);
/*
* Find #interrupt-cells of the interrupt domain.
*/
if (OF_getprop(node, "interrupt-parent", &iph, sizeof(iph)) <= 0) {
debugf("no intr-parent phandle\n");
intr_par = OF_parent(node);
} else {
iph = fdt32_to_cpu(iph);
intr_par = OF_instance_to_package(iph);
}
if (OF_getprop(intr_par, "#interrupt-cells", &intr_cells,
sizeof(intr_cells)) <= 0) {
debugf("no intr-cells defined, defaulting to 1\n");
intr_cells = 1;
}
intr_cells = fdt32_to_cpu(intr_cells);
intr_num = OF_getprop_alloc(node, "interrupts",
intr_cells * sizeof(pcell_t), (void **)&intr);
if (intr_num <= 0 || intr_num > DI_MAX_INTR_NUM)
return (ERANGE);
rv = 0;
for (i = 0; i < intr_num; i++) {
interrupt = -1;
trig = pol = 0;
if (fdt_intr_decode(intr_par, &intr[i * intr_cells],
&interrupt, &trig, &pol) != 0) {
rv = ENXIO;
goto out;
}
if (interrupt < 0) {
rv = ERANGE;
goto out;
}
debugf("decoded intr = %d, trig = %d, pol = %d\n", interrupt,
trig, pol);
intr_sl[intr_num].trig = trig;
intr_sl[intr_num].pol = pol;
resource_list_add(rl, SYS_RES_IRQ, i, interrupt, interrupt, 1);
}
out:
free(intr, M_OFWPROP);
return (rv);
}
int
fdt_get_phyaddr(phandle_t node, int *phy_addr)
{
phandle_t phy_node;
ihandle_t phy_ihandle;
pcell_t phy_handle, phy_reg;
if (OF_getprop(node, "phy-handle", (void *)&phy_handle,
sizeof(phy_handle)) <= 0)
return (ENXIO);
phy_ihandle = (ihandle_t)phy_handle;
phy_ihandle = fdt32_to_cpu(phy_ihandle);
phy_node = OF_instance_to_package(phy_ihandle);
if (OF_getprop(phy_node, "reg", (void *)&phy_reg,
sizeof(phy_reg)) <= 0)
return (ENXIO);
*phy_addr = fdt32_to_cpu(phy_reg);
return (0);
}
int
fdt_get_mem_regions(struct mem_region *mr, int *mrcnt, uint32_t *memsize)
{
pcell_t reg[FDT_REG_CELLS * FDT_MEM_REGIONS];
pcell_t *regp;
phandle_t memory;
uint32_t memory_size;
int addr_cells, size_cells;
int i, max_size, reg_len, rv, tuple_size, tuples;
max_size = sizeof(reg);
memory = OF_finddevice("/memory");
if (memory <= 0) {
rv = ENXIO;
goto out;
}
if ((rv = fdt_addrsize_cells(OF_parent(memory), &addr_cells,
&size_cells)) != 0)
goto out;
if (addr_cells > 2) {
rv = ERANGE;
goto out;
}
tuple_size = sizeof(pcell_t) * (addr_cells + size_cells);
reg_len = OF_getproplen(memory, "reg");
if (reg_len <= 0 || reg_len > sizeof(reg)) {
rv = ERANGE;
goto out;
}
if (OF_getprop(memory, "reg", reg, reg_len) <= 0) {
rv = ENXIO;
goto out;
}
memory_size = 0;
tuples = reg_len / tuple_size;
regp = (pcell_t *)&reg;
for (i = 0; i < tuples; i++) {
rv = fdt_data_to_res(regp, addr_cells, size_cells,
(u_long *)&mr[i].mr_start, (u_long *)&mr[i].mr_size);
if (rv != 0)
goto out;
regp += addr_cells + size_cells;
memory_size += mr[i].mr_size;
}
if (memory_size == 0) {
rv = ERANGE;
goto out;
}
*mrcnt = i;
*memsize = memory_size;
rv = 0;
out:
return (rv);
}

112
sys/dev/fdt/fdt_common.h Normal file
View File

@ -0,0 +1,112 @@
/*-
* Copyright (c) 2009-2010 The FreeBSD Foundation
* All rights reserved.
*
* This software was developed by Semihalf under sponsorship from
* the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _FDT_COMMON_H_
#define _FDT_COMMON_H_
#include <contrib/libfdt/libfdt_env.h>
#include <dev/ofw/ofw_bus.h>
#include <machine/fdt.h>
#define FDT_MEM_REGIONS 8
#define DI_MAX_INTR_NUM 8
struct fdt_pci_range {
u_long base_pci;
u_long base_parent;
u_long len;
};
struct fdt_pci_intr {
int addr_cells;
int intr_cells;
int map_len;
pcell_t *map;
pcell_t *mask;
};
struct fdt_sense_level {
enum intr_trigger trig;
enum intr_polarity pol;
};
typedef int (*fdt_pic_decode_t)(phandle_t, pcell_t *, int *, int *, int *);
extern fdt_pic_decode_t fdt_pic_table[];
typedef void (*fdt_fixup_t)(phandle_t);
struct fdt_fixup_entry {
char *model;
fdt_fixup_t handler;
};
extern struct fdt_fixup_entry fdt_fixup_table[];
extern vm_paddr_t fdt_immr_pa;
extern vm_offset_t fdt_immr_va;
extern vm_offset_t fdt_immr_size;
struct fdt_pm_mask_entry {
char *compat;
uint32_t mask;
};
extern struct fdt_pm_mask_entry fdt_pm_mask_table[];
#if defined(FDT_DTB_STATIC)
extern u_char fdt_static_dtb;
#endif
int fdt_addrsize_cells(phandle_t, int *, int *);
u_long fdt_data_get(void *, int);
int fdt_data_to_res(pcell_t *, int, int, u_long *, u_long *);
int fdt_data_verify(void *, int);
phandle_t fdt_find_compatible(phandle_t, const char *, int);
int fdt_get_mem_regions(struct mem_region *, int *, uint32_t *);
int fdt_get_phyaddr(phandle_t node, int *);
int fdt_immr_addr(void);
int fdt_regsize(phandle_t, u_long *, u_long *);
int fdt_intr_decode(phandle_t, pcell_t *, int *, int *, int *);
int fdt_intr_to_rl(phandle_t, struct resource_list *, struct fdt_sense_level *);
int fdt_is_compatible(phandle_t, const char *);
int fdt_is_compatible_strict(phandle_t, const char *);
int fdt_is_enabled(phandle_t);
int fdt_pm_is_enabled(phandle_t);
int fdt_is_type(phandle_t, const char *);
int fdt_parent_addr_cells(phandle_t);
int fdt_pci_intr_info(phandle_t, struct fdt_pci_intr *);
int fdt_pci_ranges(phandle_t, struct fdt_pci_range *, struct fdt_pci_range *);
int fdt_pci_ranges_decode(phandle_t, struct fdt_pci_range *,
struct fdt_pci_range *);
int fdt_pci_route_intr(int, int, int, int, struct fdt_pci_intr *, int *);
int fdt_ranges_verify(pcell_t *, int, int, int, int);
int fdt_reg_to_rl(phandle_t, struct resource_list *, u_long);
int fdt_pm(phandle_t);
#endif /* _FDT_COMMON_H_ */

365
sys/dev/fdt/fdt_pci.c Normal file
View File

@ -0,0 +1,365 @@
/*-
* Copyright (c) 2010 The FreeBSD Foundation
* All rights reserved.
*
* This software was developed by Semihalf under sponsorship from
* the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ktr.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <sys/malloc.h>
#include <dev/fdt/fdt_common.h>
#include <machine/fdt.h>
#include "ofw_bus_if.h"
#define DEBUG
#undef DEBUG
#ifdef DEBUG
#define debugf(fmt, args...) do { printf("%s(): ", __func__); \
printf(fmt,##args); } while (0)
#else
#define debugf(fmt, args...)
#endif
#define FDT_RANGES_CELLS ((3 + 3 + 2) * 2)
static void
fdt_pci_range_dump(struct fdt_pci_range *range)
{
#ifdef DEBUG
printf("\n");
printf(" base_pci = 0x%08lx\n", range->base_pci);
printf(" base_par = 0x%08lx\n", range->base_parent);
printf(" len = 0x%08lx\n", range->len);
#endif
}
int
fdt_pci_ranges_decode(phandle_t node, struct fdt_pci_range *io_space,
struct fdt_pci_range *mem_space)
{
pcell_t ranges[FDT_RANGES_CELLS];
struct fdt_pci_range *pci_space;
pcell_t addr_cells, size_cells, par_addr_cells;
pcell_t *rangesptr;
pcell_t cell0, cell1, cell2;
int tuple_size, tuples, i, rv, offset_cells, len;
/*
* Retrieve 'ranges' property.
*/
if ((fdt_addrsize_cells(node, &addr_cells, &size_cells)) != 0)
return (EINVAL);
if (addr_cells != 3 || size_cells != 2)
return (ERANGE);
par_addr_cells = fdt_parent_addr_cells(node);
if (par_addr_cells > 3)
return (ERANGE);
len = OF_getproplen(node, "ranges");
if (len > sizeof(ranges))
return (ENOMEM);
if (OF_getprop(node, "ranges", ranges, sizeof(ranges)) <= 0)
return (EINVAL);
tuple_size = sizeof(pcell_t) * (addr_cells + par_addr_cells +
size_cells);
tuples = len / tuple_size;
rangesptr = &ranges[0];
offset_cells = 0;
for (i = 0; i < tuples; i++) {
cell0 = fdt_data_get((void *)rangesptr, 1);
rangesptr++;
cell1 = fdt_data_get((void *)rangesptr, 1);
rangesptr++;
cell2 = fdt_data_get((void *)rangesptr, 1);
rangesptr++;
if (cell0 & 0x02000000) {
pci_space = mem_space;
} else if (cell0 & 0x01000000) {
pci_space = io_space;
} else {
rv = ERANGE;
goto out;
}
if (par_addr_cells == 3) {
/*
* This is a PCI subnode 'ranges'. Skip cell0 and
* cell1 of this entry and only use cell2.
*/
offset_cells = 2;
rangesptr += offset_cells;
}
if (fdt_data_verify((void *)rangesptr, par_addr_cells -
offset_cells)) {
rv = ERANGE;
goto out;
}
pci_space->base_parent = fdt_data_get((void *)rangesptr,
par_addr_cells - offset_cells);
rangesptr += par_addr_cells - offset_cells;
if (fdt_data_verify((void *)rangesptr, size_cells)) {
rv = ERANGE;
goto out;
}
pci_space->len = fdt_data_get((void *)rangesptr, size_cells);
rangesptr += size_cells;
pci_space->base_pci = cell2;
}
rv = 0;
out:
return (rv);
}
int
fdt_pci_ranges(phandle_t node, struct fdt_pci_range *io_space,
struct fdt_pci_range *mem_space)
{
struct fdt_pci_range par_io_space, par_mem_space;
u_long base;
int err;
debugf("Processing parent PCI node: %x\n", node);
if ((err = fdt_pci_ranges_decode(OF_parent(node), &par_io_space,
&par_mem_space)) != 0) {
debugf("could not decode parent PCI node 'ranges'\n");
return (err);
}
debugf("Processing PCI sub node: %x\n", node);
if ((err = fdt_pci_ranges_decode(node, io_space, mem_space)) != 0) {
debugf("could not decode PCI subnode 'ranges'\n");
return (err);
}
base = io_space->base_parent & 0x000fffff;
base += par_io_space.base_parent;
io_space->base_parent = base;
base = mem_space->base_parent & 0x000fffff;
base += par_mem_space.base_parent;
mem_space->base_parent = base;
debugf("Post fixup dump:\n");
fdt_pci_range_dump(io_space);
fdt_pci_range_dump(mem_space);
return (0);
}
static int
fdt_addr_cells(phandle_t node, int *addr_cells)
{
pcell_t cell;
int cell_size;
cell_size = sizeof(cell);
if (OF_getprop(node, "#address-cells", &cell, cell_size) < cell_size)
return (EINVAL);
*addr_cells = fdt32_to_cpu((int)cell);
if (*addr_cells > 3)
return (ERANGE);
return (0);
}
static int
fdt_interrupt_cells(phandle_t node)
{
pcell_t intr_cells;
if (OF_getprop(node, "#interrupt-cells", &intr_cells,
sizeof(intr_cells)) <= 0) {
debugf("no intr-cells defined, defaulting to 1\n");
intr_cells = 1;
}
intr_cells = fdt32_to_cpu(intr_cells);
return ((int)intr_cells);
}
int
fdt_pci_intr_info(phandle_t node, struct fdt_pci_intr *intr_info)
{
void *map, *mask;
phandle_t pci_par;
int intr_cells, addr_cells;
int len;
addr_cells = fdt_parent_addr_cells(node);
pci_par = OF_parent(node);
intr_cells = fdt_interrupt_cells(pci_par);
/*
* Retrieve the interrupt map and mask properties.
*/
len = OF_getprop_alloc(pci_par, "interrupt-map-mask", 1, &mask);
if (len / sizeof(pcell_t) != (addr_cells + intr_cells)) {
debugf("bad mask len = %d\n", len);
goto err;
}
len = OF_getprop_alloc(pci_par, "interrupt-map", 1, &map);
if (len <= 0) {
debugf("bad map len = %d\n", len);
goto err;
}
intr_info->map_len = len;
intr_info->map = map;
intr_info->mask = mask;
intr_info->addr_cells = addr_cells;
intr_info->intr_cells = intr_cells;
return (0);
err:
free(mask, M_OFWPROP);
return (ENXIO);
}
int
fdt_pci_route_intr(int bus, int slot, int func, int pin,
struct fdt_pci_intr *intr_info, int *interrupt)
{
pcell_t child_spec[4], masked[4];
ihandle_t iph;
pcell_t intr_par;
pcell_t *map_ptr;
uint32_t addr;
int i, j, map_len;
int par_intr_cells, par_addr_cells, child_spec_cells, row_cells;
int par_idx, spec_idx, err, trig, pol;
child_spec_cells = intr_info->addr_cells + intr_info->intr_cells;
if (child_spec_cells > sizeof(child_spec) / sizeof(pcell_t))
return (ENOMEM);
addr = (bus << 16) | (slot << 11) | (func << 8);
child_spec[0] = addr;
child_spec[1] = 0;
child_spec[2] = 0;
child_spec[3] = pin;
map_len = intr_info->map_len;
map_ptr = intr_info->map;
par_idx = child_spec_cells;
i = 0;
while (i < map_len) {
iph = fdt32_to_cpu(map_ptr[par_idx]);
intr_par = OF_instance_to_package(iph);
err = fdt_addr_cells(intr_par, &par_addr_cells);
if (err != 0) {
debugf("could not retrieve intr parent #addr-cells\n");
return (err);
}
par_intr_cells = fdt_interrupt_cells(intr_par);
row_cells = child_spec_cells + 1 + par_addr_cells +
par_intr_cells;
/*
* Apply mask and look up the entry in interrupt map.
*/
for (j = 0; j < child_spec_cells; j++) {
masked[j] = child_spec[j] &
fdt32_to_cpu(intr_info->mask[j]);
if (masked[j] != fdt32_to_cpu(map_ptr[j]))
goto next;
}
/*
* Decode interrupt of the parent intr controller.
*/
spec_idx = child_spec_cells + 1 + par_addr_cells;
err = fdt_intr_decode(intr_par, &map_ptr[spec_idx],
interrupt, &trig, &pol);
if (err != 0) {
debugf("could not decode interrupt\n");
return (err);
}
debugf("decoded intr = %d, trig = %d, pol = %d\n", *interrupt,
trig, pol);
/* XXX we should probably call powerpc_config() here... */
return (0);
next:
map_ptr += row_cells;
i += (row_cells * sizeof(pcell_t));
}
return (ENXIO);
}
#if defined(__arm__)
int
fdt_pci_devmap(phandle_t node, struct pmap_devmap *devmap, vm_offset_t io_va,
vm_offset_t mem_va)
{
struct fdt_pci_range io_space, mem_space;
int error;
if ((error = fdt_pci_ranges_decode(node, &io_space, &mem_space)) != 0)
return (error);
devmap->pd_va = io_va;
devmap->pd_pa = io_space.base_parent;
devmap->pd_size = io_space.len;
devmap->pd_prot = VM_PROT_READ | VM_PROT_WRITE;
devmap->pd_cache = PTE_NOCACHE;
devmap++;
devmap->pd_va = mem_va;
devmap->pd_pa = mem_space.base_parent;
devmap->pd_size = mem_space.len;
devmap->pd_prot = VM_PROT_READ | VM_PROT_WRITE;
devmap->pd_cache = PTE_NOCACHE;
return (0);
}
#endif

View File

@ -0,0 +1,47 @@
/*-
* Copyright (c) 2010 The FreeBSD Foundation
* All rights reserved.
*
* This software was developed by Semihalf under sponsorship from
* the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include "fdt_static_dtb.h"
.data
.ascii "Device Tree Blob STARTS here"
.global fdt_static_dtb
/*
* The device tree blob must be aligned at 8-bytes boundary. Use
* gas-specific 'balign' extension to ensure the same alignment behaviour on
* all archs (the .align directive meaning can vary accross gas arch
* variations).
*/
.balign 8
fdt_static_dtb:
.incbin FDT_DTB_FILE
.ascii "Device Tree Blob STOPS here"

654
sys/dev/fdt/fdtbus.c Normal file
View File

@ -0,0 +1,654 @@
/*-
* Copyright (c) 2009-2010 The FreeBSD Foundation
* All rights reserved.
*
* This software was developed by Semihalf under sponsorship from
* the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ktr.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <sys/malloc.h>
#include <dev/ofw/openfirm.h>
#include <machine/fdt.h>
#include "fdt_common.h"
#include "ofw_bus_if.h"
#define DEBUG
#undef DEBUG
#ifdef DEBUG
#define debugf(fmt, args...) do { printf("%s(): ", __func__); \
printf(fmt,##args); } while (0)
#else
#define debugf(fmt, args...)
#endif
static MALLOC_DEFINE(M_FDTBUS, "fdtbus", "FDTbus devices information");
struct fdtbus_devinfo {
phandle_t di_node;
char *di_name;
char *di_type;
char *di_compat;
struct resource_list di_res;
/* Interrupts sense-level info for this device */
struct fdt_sense_level di_intr_sl[DI_MAX_INTR_NUM];
};
struct fdtbus_softc {
struct rman sc_irq;
struct rman sc_mem;
};
/*
* Prototypes.
*/
static int fdtbus_probe(device_t);
static int fdtbus_attach(device_t);
static int fdtbus_print_child(device_t, device_t);
static struct resource *fdtbus_alloc_resource(device_t, device_t, int,
int *, u_long, u_long, u_long, u_int);
static int fdtbus_release_resource(device_t, device_t, int, int,
struct resource *);
static int fdtbus_activate_resource(device_t, device_t, int, int,
struct resource *);
static int fdtbus_deactivate_resource(device_t, device_t, int, int,
struct resource *);
static int fdtbus_setup_intr(device_t, device_t, struct resource *, int,
driver_filter_t *, driver_intr_t *, void *, void **);
static int fdtbus_teardown_intr(device_t, device_t, struct resource *,
void *);
static const char *fdtbus_ofw_get_name(device_t, device_t);
static phandle_t fdtbus_ofw_get_node(device_t, device_t);
static const char *fdtbus_ofw_get_type(device_t, device_t);
static const char *fdtbus_ofw_get_compat(device_t, device_t);
/*
* Local routines.
*/
static void newbus_device_from_fdt_node(device_t, phandle_t);
/*
* Bus interface definition.
*/
static device_method_t fdtbus_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, fdtbus_probe),
DEVMETHOD(device_attach, fdtbus_attach),
DEVMETHOD(device_detach, bus_generic_detach),
DEVMETHOD(device_shutdown, bus_generic_shutdown),
DEVMETHOD(device_suspend, bus_generic_suspend),
DEVMETHOD(device_resume, bus_generic_resume),
/* Bus interface */
DEVMETHOD(bus_print_child, fdtbus_print_child),
DEVMETHOD(bus_alloc_resource, fdtbus_alloc_resource),
DEVMETHOD(bus_release_resource, fdtbus_release_resource),
DEVMETHOD(bus_activate_resource, fdtbus_activate_resource),
DEVMETHOD(bus_deactivate_resource, fdtbus_deactivate_resource),
DEVMETHOD(bus_setup_intr, fdtbus_setup_intr),
DEVMETHOD(bus_teardown_intr, fdtbus_teardown_intr),
/* OFW bus interface */
DEVMETHOD(ofw_bus_get_node, fdtbus_ofw_get_node),
DEVMETHOD(ofw_bus_get_name, fdtbus_ofw_get_name),
DEVMETHOD(ofw_bus_get_type, fdtbus_ofw_get_type),
DEVMETHOD(ofw_bus_get_compat, fdtbus_ofw_get_compat),
{ 0, 0 }
};
static driver_t fdtbus_driver = {
"fdtbus",
fdtbus_methods,
sizeof(struct fdtbus_softc)
};
devclass_t fdtbus_devclass;
DRIVER_MODULE(fdtbus, nexus, fdtbus_driver, fdtbus_devclass, 0, 0);
static int
fdtbus_probe(device_t dev)
{
device_set_desc(dev, "FDT main bus");
if (!bootverbose)
device_quiet(dev);
return (BUS_PROBE_DEFAULT);
}
static int
fdtbus_attach(device_t dev)
{
phandle_t root;
phandle_t child;
struct fdtbus_softc *sc;
u_long start, end;
int error;
if ((root = OF_peer(0)) == 0)
panic("fdtbus_attach: no root node.");
sc = device_get_softc(dev);
/*
* IRQ rman.
*/
start = 0;
end = FDT_INTR_MAX - 1;
sc->sc_irq.rm_start = start;
sc->sc_irq.rm_end = end;
sc->sc_irq.rm_type = RMAN_ARRAY;
sc->sc_irq.rm_descr = "Interrupt request lines";
if ((error = rman_init(&sc->sc_irq)) != 0) {
device_printf(dev, "could not init IRQ rman, error = %d\n",
error);
return (error);
}
if ((error = rman_manage_region(&sc->sc_irq, start, end)) != 0) {
device_printf(dev, "could not manage IRQ region, error = %d\n",
error);
return (error);
}
/*
* Mem-mapped I/O space rman.
*/
start = 0;
end = ~0u;
sc->sc_mem.rm_start = start;
sc->sc_mem.rm_end = end;
sc->sc_mem.rm_type = RMAN_ARRAY;
sc->sc_mem.rm_descr = "I/O memory";
if ((error = rman_init(&sc->sc_mem)) != 0) {
device_printf(dev, "could not init I/O mem rman, error = %d\n",
error);
return (error);
}
if ((error = rman_manage_region(&sc->sc_mem, start, end)) != 0) {
device_printf(dev, "could not manage I/O mem region, "
"error = %d\n", error);
return (error);
}
/*
* Walk the FDT root node and add top-level devices as our children.
*/
for (child = OF_child(root); child != 0; child = OF_peer(child)) {
/* Check and process 'status' property. */
if (!(fdt_is_enabled(child)))
continue;
newbus_device_from_fdt_node(dev, child);
}
return (bus_generic_attach(dev));
}
static int
fdtbus_print_child(device_t dev, device_t child)
{
struct fdtbus_devinfo *di;
struct resource_list *rl;
int rv;
di = device_get_ivars(child);
rl = &di->di_res;
rv = 0;
rv += bus_print_child_header(dev, child);
rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
rv += bus_print_child_footer(dev, child);
return (rv);
}
static void
newbus_device_destroy(device_t dev)
{
struct fdtbus_devinfo *di;
di = device_get_ivars(dev);
free(di->di_name, M_OFWPROP);
free(di->di_type, M_OFWPROP);
free(di->di_compat, M_OFWPROP);
resource_list_free(&di->di_res);
free(di, M_FDTBUS);
}
static device_t
newbus_device_create(device_t dev_par, phandle_t node, char *name, char *type,
char *compat)
{
device_t child;
struct fdtbus_devinfo *di;
child = device_add_child(dev_par, NULL, -1);
if (child == NULL) {
free(name, M_OFWPROP);
free(type, M_OFWPROP);
free(compat, M_OFWPROP);
return (NULL);
}
di = malloc(sizeof(*di), M_FDTBUS, M_WAITOK);
di->di_node = node;
di->di_name = name;
di->di_type = type;
di->di_compat = compat;
resource_list_init(&di->di_res);
if (fdt_reg_to_rl(node, &di->di_res, fdt_immr_va)) {
device_printf(child, "could not process 'reg' property\n");
newbus_device_destroy(child);
child = NULL;
goto out;
}
if (fdt_intr_to_rl(node, &di->di_res, di->di_intr_sl)) {
device_printf(child, "could not process 'interrupts' "
"property\n");
newbus_device_destroy(child);
child = NULL;
goto out;
}
device_set_ivars(child, di);
debugf("added child name='%s', node=%p\n", name, (void *)node);
out:
return (child);
}
static device_t
newbus_pci_create(device_t dev_par, phandle_t dt_node, u_long par_base,
u_long par_size)
{
pcell_t reg[3 + 2];
device_t dev_child;
u_long start, end, count;
struct fdtbus_devinfo *di;
char *name, *type, *compat;
int len;
OF_getprop_alloc(dt_node, "device_type", 1, (void **)&type);
if (!(type != NULL && strcmp(type, "pci") == 0)) {
/* Only process 'pci' subnodes. */
free(type, M_OFWPROP);
return (NULL);
}
OF_getprop_alloc(dt_node, "name", 1, (void **)&name);
OF_getprop_alloc(OF_parent(dt_node), "compatible", 1,
(void **)&compat);
dev_child = device_add_child(dev_par, NULL, -1);
if (dev_child == NULL) {
free(name, M_OFWPROP);
free(type, M_OFWPROP);
free(compat, M_OFWPROP);
return (NULL);
}
di = malloc(sizeof(*di), M_FDTBUS, M_WAITOK);
di->di_node = dt_node;
di->di_name = name;
di->di_type = type;
di->di_compat = compat;
resource_list_init(&di->di_res);
/*
* Produce and set SYS_RES_MEMORY resources.
*/
start = 0;
count = 0;
len = OF_getprop(dt_node, "reg", &reg, sizeof(reg));
if (len > 0) {
if (fdt_data_verify((void *)&reg[1], 2) != 0) {
device_printf(dev_child, "'reg' address value out of "
"range\n");
newbus_device_destroy(dev_child);
dev_child = NULL;
goto out;
}
start = fdt_data_get((void *)&reg[1], 2);
if (fdt_data_verify((void *)&reg[3], 2) != 0) {
device_printf(dev_child, "'reg' size value out of "
"range\n");
newbus_device_destroy(dev_child);
dev_child = NULL;
goto out;
}
count = fdt_data_get((void *)&reg[3], 2);
}
/* Calculate address range relative to base. */
par_base &= 0x000ffffful;
start &= 0x000ffffful;
start += par_base + fdt_immr_va;
if (count == 0)
count = par_size;
end = start + count - 1;
debugf("start = 0x%08lx, end = 0x%08lx, count = 0x%08lx\n",
start, end, count);
if (count > par_size) {
device_printf(dev_child, "'reg' size value out of range\n");
newbus_device_destroy(dev_child);
dev_child = NULL;
goto out;
}
resource_list_add(&di->di_res, SYS_RES_MEMORY, 0, start, end, count);
/*
* Set SYS_RES_IRQ resources.
*/
if (fdt_intr_to_rl(OF_parent(dt_node), &di->di_res, di->di_intr_sl)) {
device_printf(dev_child, "could not process 'interrupts' "
"property\n");
newbus_device_destroy(dev_child);
dev_child = NULL;
goto out;
}
device_set_ivars(dev_child, di);
debugf("added child name='%s', node=%p\n", name,
(void *)dt_node);
out:
return (dev_child);
}
static void
pci_from_fdt_node(device_t dev_par, phandle_t dt_node, char *name,
char *type, char *compat)
{
u_long reg_base, reg_size;
phandle_t dt_child;
/*
* Retrieve 'reg' property.
*/
if (fdt_regsize(dt_node, &reg_base, &reg_size) != 0) {
device_printf(dev_par, "could not retrieve 'reg' prop\n");
return;
}
/*
* Walk the PCI node and instantiate newbus devices representing
* logical resources (bridges / ports).
*/
for (dt_child = OF_child(dt_node); dt_child != 0;
dt_child = OF_peer(dt_child)) {
if (!(fdt_is_enabled(dt_child)))
continue;
newbus_pci_create(dev_par, dt_child, reg_base, reg_size);
}
}
/*
* These FDT nodes do not need a corresponding newbus device object.
*/
static char *fdt_devices_skip[] = {
"aliases",
"chosen",
"memory",
NULL
};
static void
newbus_device_from_fdt_node(device_t dev_par, phandle_t node)
{
char *name, *type, *compat;
device_t child;
int i;
OF_getprop_alloc(node, "name", 1, (void **)&name);
OF_getprop_alloc(node, "device_type", 1, (void **)&type);
OF_getprop_alloc(node, "compatible", 1, (void **)&compat);
for (i = 0; fdt_devices_skip[i] != NULL; i++)
if (name != NULL && strcmp(name, fdt_devices_skip[i]) == 0) {
debugf("skipping instantiating FDT device='%s'\n",
name);
return;
}
if (type != NULL && strcmp(type, "pci") == 0) {
pci_from_fdt_node(dev_par, node, name, type, compat);
return;
}
child = newbus_device_create(dev_par, node, name, type, compat);
}
static struct resource *
fdtbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
u_long start, u_long end, u_long count, u_int flags)
{
struct fdtbus_softc *sc;
struct resource *res;
struct rman *rm;
struct fdtbus_devinfo *di;
struct resource_list_entry *rle;
int needactivate;
/*
* Request for the default allocation with a given rid: use resource
* list stored in the local device info.
*/
if ((start == 0UL) && (end == ~0UL)) {
if ((di = device_get_ivars(child)) == NULL)
return (NULL);
if (type == SYS_RES_IOPORT)
type = SYS_RES_MEMORY;
rle = resource_list_find(&di->di_res, type, *rid);
if (rle == NULL) {
device_printf(bus, "no default resources for "
"rid = %d, type = %d\n", *rid, type);
return (NULL);
}
start = rle->start;
end = rle->end;
count = rle->count;
}
sc = device_get_softc(bus);
needactivate = flags & RF_ACTIVE;
flags &= ~RF_ACTIVE;
switch (type) {
case SYS_RES_IRQ:
rm = &sc->sc_irq;
break;
case SYS_RES_IOPORT:
case SYS_RES_MEMORY:
rm = &sc->sc_mem;
break;
default:
return (NULL);
}
res = rman_reserve_resource(rm, start, end, count, flags, child);
if (res == NULL) {
device_printf(bus, "failed to reserve resource %#lx - %#lx "
"(%#lx)\n", start, end, count);
return (NULL);
}
rman_set_rid(res, *rid);
if (type == SYS_RES_IOPORT || type == SYS_RES_MEMORY) {
/* XXX endianess should be set based on SOC node */
rman_set_bustag(res, fdtbus_bs_tag);
rman_set_bushandle(res, rman_get_start(res));
}
if (needactivate)
if (bus_activate_resource(child, type, *rid, res)) {
device_printf(child, "resource activation failed\n");
rman_release_resource(res);
return (NULL);
}
return (res);
}
static int
fdtbus_release_resource(device_t bus, device_t child, int type, int rid,
struct resource *res)
{
int err;
if (rman_get_flags(res) & RF_ACTIVE) {
err = bus_deactivate_resource(child, type, rid, res);
if (err)
return (err);
}
return (rman_release_resource(res));
}
static int
fdtbus_setup_intr(device_t bus, device_t child, struct resource *res,
int flags, driver_filter_t *filter, driver_intr_t *ihand, void *arg,
void **cookiep)
{
int err;
*cookiep = 0;
if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
flags |= INTR_EXCL;
err = rman_activate_resource(res);
if (err)
return (err);
#if defined(__powerpc__)
err = powerpc_setup_intr(device_get_nameunit(child),
rman_get_start(res), filter, ihand, arg, flags, cookiep);
#elif defined(__arm__)
arm_setup_irqhandler(device_get_nameunit(child),
filter, ihand, arg, rman_get_start(res), flags, cookiep);
arm_unmask_irq(rman_get_start(res));
err = 0;
#endif
return (err);
}
static int
fdtbus_activate_resource(device_t bus, device_t child, int type, int rid,
struct resource *res)
{
return (rman_activate_resource(res));
}
static int
fdtbus_deactivate_resource(device_t bus, device_t child, int type, int rid,
struct resource *res)
{
return (rman_deactivate_resource(res));
}
static int
fdtbus_teardown_intr(device_t bus, device_t child, struct resource *res,
void *cookie)
{
#if defined(__powerpc__)
return (powerpc_teardown_intr(cookie));
#elif defined(__arm__)
return (arm_remove_irqhandler(rman_get_start(res), cookie));
#endif
}
static const char *
fdtbus_ofw_get_name(device_t bus, device_t dev)
{
struct fdtbus_devinfo *di;
return ((di = device_get_ivars(dev)) == NULL ? NULL : di->di_name);
}
static phandle_t
fdtbus_ofw_get_node(device_t bus, device_t dev)
{
struct fdtbus_devinfo *di;
return ((di = device_get_ivars(dev)) == NULL ? 0 : di->di_node);
}
static const char *
fdtbus_ofw_get_type(device_t bus, device_t dev)
{
struct fdtbus_devinfo *di;
return ((di = device_get_ivars(dev)) == NULL ? NULL : di->di_type);
}
static const char *
fdtbus_ofw_get_compat(device_t bus, device_t dev)
{
struct fdtbus_devinfo *di;
return ((di = device_get_ivars(dev)) == NULL ? NULL : di->di_compat);
}

333
sys/dev/fdt/simplebus.c Normal file
View File

@ -0,0 +1,333 @@
/*-
* Copyright (c) 2009-2010 The FreeBSD Foundation
* All rights reserved.
*
* This software was developed by Semihalf under sponsorship from
* the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_platform.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ktr.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <sys/malloc.h>
#include <machine/fdt.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/ofw/openfirm.h>
#include "fdt_common.h"
#include "ofw_bus_if.h"
#define DEBUG
#undef DEBUG
#ifdef DEBUG
#define debugf(fmt, args...) do { printf("%s(): ", __func__); \
printf(fmt,##args); } while (0)
#else
#define debugf(fmt, args...)
#endif
static MALLOC_DEFINE(M_SIMPLEBUS, "simplebus", "simplebus devices information");
struct simplebus_softc {
int sc_addr_cells;
int sc_size_cells;
u_long sc_start_pa;
u_long sc_start_va;
u_long sc_size;
};
struct simplebus_devinfo {
struct ofw_bus_devinfo di_ofw;
struct resource_list di_res;
/* Interrupts sense-level info for this device */
struct fdt_sense_level di_intr_sl[DI_MAX_INTR_NUM];
};
/*
* Prototypes.
*/
static int simplebus_probe(device_t);
static int simplebus_attach(device_t);
static int simplebus_print_child(device_t, device_t);
static int simplebus_setup_intr(device_t, device_t, struct resource *, int,
driver_filter_t *, driver_intr_t *, void *, void **);
static struct resource *simplebus_alloc_resource(device_t, device_t, int,
int *, u_long, u_long, u_long, u_int);
static struct resource_list *simplebus_get_resource_list(device_t, device_t);
static ofw_bus_get_devinfo_t simplebus_get_devinfo;
/*
* Bus interface definition.
*/
static device_method_t simplebus_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, simplebus_probe),
DEVMETHOD(device_attach, simplebus_attach),
DEVMETHOD(device_detach, bus_generic_detach),
DEVMETHOD(device_shutdown, bus_generic_shutdown),
DEVMETHOD(device_suspend, bus_generic_suspend),
DEVMETHOD(device_resume, bus_generic_resume),
/* Bus interface */
DEVMETHOD(bus_print_child, simplebus_print_child),
DEVMETHOD(bus_alloc_resource, simplebus_alloc_resource),
DEVMETHOD(bus_release_resource, bus_generic_release_resource),
DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
DEVMETHOD(bus_setup_intr, simplebus_setup_intr),
DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
DEVMETHOD(bus_get_resource_list, simplebus_get_resource_list),
/* OFW bus interface */
DEVMETHOD(ofw_bus_get_devinfo, simplebus_get_devinfo),
DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
{ 0, 0 }
};
static driver_t simplebus_driver = {
"simplebus",
simplebus_methods,
sizeof(struct simplebus_softc)
};
devclass_t simplebus_devclass;
DRIVER_MODULE(simplebus, fdtbus, simplebus_driver, simplebus_devclass, 0, 0);
static int
simplebus_probe(device_t dev)
{
if (!ofw_bus_is_compatible_strict(dev, "simple-bus"))
return (ENXIO);
device_set_desc(dev, "Flattened device tree simple bus");
return (BUS_PROBE_DEFAULT);
}
static int
simplebus_attach(device_t dev)
{
device_t dev_child;
struct simplebus_devinfo *di;
struct simplebus_softc *sc;
phandle_t dt_node, dt_child;
sc = device_get_softc(dev);
sc->sc_start_pa = fdt_immr_pa;
sc->sc_start_va = fdt_immr_va;
sc->sc_size = fdt_immr_size;
/*
* Walk simple-bus and add direct subordinates as our children.
*/
dt_node = ofw_bus_get_node(dev);
for (dt_child = OF_child(dt_node); dt_child != 0;
dt_child = OF_peer(dt_child)) {
/* Check and process 'status' property. */
if (!(fdt_is_enabled(dt_child)))
continue;
if (!(fdt_pm_is_enabled(dt_child)))
continue;
di = malloc(sizeof(*di), M_SIMPLEBUS, M_WAITOK | M_ZERO);
if (ofw_bus_gen_setup_devinfo(&di->di_ofw, dt_child) != 0) {
free(di, M_SIMPLEBUS);
device_printf(dev, "could not set up devinfo\n");
continue;
}
resource_list_init(&di->di_res);
if (fdt_reg_to_rl(dt_child, &di->di_res, sc->sc_start_va)) {
device_printf(dev, "could not process 'reg' "
"property\n");
ofw_bus_gen_destroy_devinfo(&di->di_ofw);
free(di, M_SIMPLEBUS);
continue;
}
if (fdt_intr_to_rl(dt_child, &di->di_res, di->di_intr_sl)) {
device_printf(dev, "could not process 'interrupts' "
"property\n");
resource_list_free(&di->di_res);
ofw_bus_gen_destroy_devinfo(&di->di_ofw);
free(di, M_SIMPLEBUS);
continue;
}
/* Add newbus device for this FDT node */
dev_child = device_add_child(dev, NULL, -1);
if (dev_child == NULL) {
device_printf(dev, "could not add child: %s\n",
di->di_ofw.obd_name);
resource_list_free(&di->di_res);
ofw_bus_gen_destroy_devinfo(&di->di_ofw);
free(di, M_SIMPLEBUS);
continue;
}
device_set_ivars(dev_child, di);
}
return (bus_generic_attach(dev));
}
static int
simplebus_print_child(device_t dev, device_t child)
{
struct simplebus_devinfo *di;
struct resource_list *rl;
int rv;
di = device_get_ivars(child);
rl = &di->di_res;
rv = 0;
rv += bus_print_child_header(dev, child);
rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#lx");
rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
rv += bus_print_child_footer(dev, child);
return (rv);
}
static struct resource *
simplebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
u_long start, u_long end, u_long count, u_int flags)
{
struct simplebus_devinfo *di;
struct resource_list_entry *rle;
/*
* Request for the default allocation with a given rid: use resource
* list stored in the local device info.
*/
if ((start == 0UL) && (end == ~0UL)) {
if ((di = device_get_ivars(child)) == NULL)
return (NULL);
if (type == SYS_RES_IOPORT)
type = SYS_RES_MEMORY;
rle = resource_list_find(&di->di_res, type, *rid);
if (rle == NULL) {
device_printf(bus, "no default resources for "
"rid = %d, type = %d\n", *rid, type);
return (NULL);
}
start = rle->start;
end = rle->end;
count = rle->count;
}
return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
count, flags));
}
static struct resource_list *
simplebus_get_resource_list(device_t bus, device_t child)
{
struct simplebus_devinfo *di;
di = device_get_ivars(child);
return (&di->di_res);
}
static int
simplebus_setup_intr(device_t bus, device_t child, struct resource *res,
int flags, driver_filter_t *filter, driver_intr_t *ihand, void *arg,
void **cookiep)
{
struct simplebus_devinfo *di;
enum intr_trigger trig;
enum intr_polarity pol;
int irq, rid;
if (res == NULL)
panic("simplebus_setup_intr: NULL irq resource!");
rid = rman_get_rid(res);
if (rid > DI_MAX_INTR_NUM) {
device_printf(child, "rid out of range rid = %d\n", rid);
return (ERANGE);
}
irq = rman_get_start(res);
if ((di = device_get_ivars(child)) == NULL) {
device_printf(child, "could not retrieve devinfo\n");
return (ENXIO);
}
trig = di->di_intr_sl[rid].trig;
pol = di->di_intr_sl[rid].pol;
debugf("intr config: irq = %d, trig = %d, pol = %d\n", irq, trig, pol);
#if defined(__powerpc__)
int err;
err = powerpc_config_intr(irq, trig, pol);
if (err)
return (err);
#endif
return (bus_generic_setup_intr(bus, child, res, flags, filter, ihand,
arg, cookiep));
}
static const struct ofw_bus_devinfo *
simplebus_get_devinfo(device_t bus, device_t child)
{
struct simplebus_devinfo *di;
di = device_get_ivars(child);
return (&di->di_ofw);
}