2020-03-17 22:59:40 +08:00
|
|
|
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
/*
|
|
|
|
* fdt_helper.c - Flat Device Tree manipulation helper routines
|
|
|
|
* Implement helper routines on top of libfdt for OpenSBI usage
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 Bin Meng <bmeng.cn@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libfdt.h>
|
|
|
|
#include <sbi/riscv_asm.h>
|
|
|
|
#include <sbi/sbi_console.h>
|
2020-05-13 12:25:28 +08:00
|
|
|
#include <sbi/sbi_hartmask.h>
|
2020-03-17 22:59:40 +08:00
|
|
|
#include <sbi/sbi_platform.h>
|
|
|
|
#include <sbi/sbi_scratch.h>
|
2020-03-24 03:48:57 +08:00
|
|
|
#include <sbi_utils/fdt/fdt_helper.h>
|
2020-05-12 15:27:52 +08:00
|
|
|
#include <sbi_utils/irqchip/plic.h>
|
2020-05-13 12:25:28 +08:00
|
|
|
#include <sbi_utils/sys/clint.h>
|
2020-03-17 22:59:40 +08:00
|
|
|
|
2020-04-24 17:51:34 +08:00
|
|
|
#define DEFAULT_UART_FREQ 0
|
|
|
|
#define DEFAULT_UART_BAUD 115200
|
|
|
|
#define DEFAULT_UART_REG_SHIFT 0
|
|
|
|
#define DEFAULT_UART_REG_IO_WIDTH 1
|
|
|
|
|
2020-04-25 21:20:37 +08:00
|
|
|
#define DEFAULT_SIFIVE_UART_FREQ 0
|
|
|
|
#define DEFAULT_SIFIVE_UART_BAUD 115200
|
|
|
|
#define DEFAULT_SIFIVE_UART_REG_SHIFT 0
|
|
|
|
#define DEFAULT_SIFIVE_UART_REG_IO_WIDTH 4
|
|
|
|
|
2020-04-24 21:02:10 +08:00
|
|
|
const struct fdt_match *fdt_match_node(void *fdt, int nodeoff,
|
|
|
|
const struct fdt_match *match_table)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!fdt || nodeoff < 0 || !match_table)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (match_table->compatible) {
|
|
|
|
ret = fdt_node_check_compatible(fdt, nodeoff,
|
|
|
|
match_table->compatible);
|
|
|
|
if (!ret)
|
|
|
|
return match_table;
|
|
|
|
match_table++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-05-12 16:29:52 +08:00
|
|
|
int fdt_find_match(void *fdt, int startoff,
|
|
|
|
const struct fdt_match *match_table,
|
2020-04-24 21:02:10 +08:00
|
|
|
const struct fdt_match **out_match)
|
|
|
|
{
|
|
|
|
int nodeoff;
|
|
|
|
|
|
|
|
if (!fdt || !match_table)
|
|
|
|
return SBI_ENODEV;
|
|
|
|
|
|
|
|
while (match_table->compatible) {
|
2020-05-12 16:29:52 +08:00
|
|
|
nodeoff = fdt_node_offset_by_compatible(fdt, startoff,
|
2020-04-24 21:02:10 +08:00
|
|
|
match_table->compatible);
|
|
|
|
if (nodeoff >= 0) {
|
|
|
|
if (out_match)
|
|
|
|
*out_match = match_table;
|
|
|
|
return nodeoff;
|
|
|
|
}
|
|
|
|
match_table++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SBI_ENODEV;
|
|
|
|
}
|
|
|
|
|
2020-04-24 21:05:25 +08:00
|
|
|
int fdt_get_node_addr_size(void *fdt, int node, unsigned long *addr,
|
|
|
|
unsigned long *size)
|
2020-03-24 03:48:57 +08:00
|
|
|
{
|
|
|
|
int parent, len, i;
|
|
|
|
int cell_addr, cell_size;
|
|
|
|
const fdt32_t *prop_addr, *prop_size;
|
|
|
|
uint64_t temp = 0;
|
|
|
|
|
|
|
|
parent = fdt_parent_offset(fdt, node);
|
|
|
|
if (parent < 0)
|
|
|
|
return parent;
|
|
|
|
cell_addr = fdt_address_cells(fdt, parent);
|
|
|
|
if (cell_addr < 1)
|
|
|
|
return SBI_ENODEV;
|
|
|
|
|
|
|
|
cell_size = fdt_size_cells(fdt, parent);
|
|
|
|
if (cell_size < 0)
|
|
|
|
return SBI_ENODEV;
|
|
|
|
|
|
|
|
prop_addr = fdt_getprop(fdt, node, "reg", &len);
|
|
|
|
if (!prop_addr)
|
|
|
|
return SBI_ENODEV;
|
|
|
|
prop_size = prop_addr + cell_addr;
|
|
|
|
|
|
|
|
if (addr) {
|
|
|
|
for (i = 0; i < cell_addr; i++)
|
|
|
|
temp = (temp << 32) | fdt32_to_cpu(*prop_addr++);
|
|
|
|
*addr = temp;
|
|
|
|
}
|
|
|
|
temp = 0;
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
for (i = 0; i < cell_size; i++)
|
|
|
|
temp = (temp << 32) | fdt32_to_cpu(*prop_size++);
|
|
|
|
*size = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-26 15:09:13 +08:00
|
|
|
int fdt_parse_hart_id(void *fdt, int cpu_offset, u32 *hartid)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
const void *prop;
|
|
|
|
const fdt32_t *val;
|
|
|
|
|
|
|
|
if (!fdt || cpu_offset < 0)
|
|
|
|
return SBI_EINVAL;
|
|
|
|
|
|
|
|
prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
|
|
|
|
if (!prop || !len)
|
|
|
|
return SBI_EINVAL;
|
|
|
|
if (sbi_strcmp(prop, "cpu"))
|
|
|
|
return SBI_EINVAL;
|
|
|
|
|
|
|
|
val = fdt_getprop(fdt, cpu_offset, "reg", &len);
|
|
|
|
if (!val || len < sizeof(fdt32_t))
|
|
|
|
return SBI_EINVAL;
|
|
|
|
|
|
|
|
if (len > sizeof(fdt32_t))
|
|
|
|
val++;
|
|
|
|
|
|
|
|
if (hartid)
|
|
|
|
*hartid = fdt32_to_cpu(*val);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-24 17:38:19 +08:00
|
|
|
int fdt_parse_max_hart_id(void *fdt, u32 *max_hartid)
|
|
|
|
{
|
|
|
|
u32 hartid;
|
|
|
|
int err, cpu_offset, cpus_offset;
|
|
|
|
|
|
|
|
if (!fdt)
|
|
|
|
return SBI_EINVAL;
|
|
|
|
if (!max_hartid)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*max_hartid = 0;
|
|
|
|
|
|
|
|
cpus_offset = fdt_path_offset(fdt, "/cpus");
|
|
|
|
if (cpus_offset < 0)
|
|
|
|
return cpus_offset;
|
|
|
|
|
|
|
|
fdt_for_each_subnode(cpu_offset, fdt, cpus_offset) {
|
|
|
|
err = fdt_parse_hart_id(fdt, cpu_offset, &hartid);
|
|
|
|
if (err)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (hartid > *max_hartid)
|
|
|
|
*max_hartid = hartid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-25 21:20:37 +08:00
|
|
|
int fdt_parse_sifive_uart_node(void *fdt, int nodeoffset,
|
|
|
|
struct platform_uart_data *uart)
|
|
|
|
{
|
|
|
|
int len, rc;
|
|
|
|
const fdt32_t *val;
|
|
|
|
unsigned long reg_addr, reg_size;
|
|
|
|
|
|
|
|
if (nodeoffset < 0 || !uart || !fdt)
|
|
|
|
return SBI_ENODEV;
|
|
|
|
|
|
|
|
rc = fdt_get_node_addr_size(fdt, nodeoffset, ®_addr, ®_size);
|
|
|
|
if (rc < 0 || !reg_addr || !reg_size)
|
|
|
|
return SBI_ENODEV;
|
|
|
|
uart->addr = reg_addr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UART address is mandaotry. clock-frequency and current-speed
|
|
|
|
* may not be present. Don't return error.
|
|
|
|
*/
|
|
|
|
val = (fdt32_t *)fdt_getprop(fdt, nodeoffset, "clock-frequency", &len);
|
|
|
|
if (len > 0 && val)
|
|
|
|
uart->freq = fdt32_to_cpu(*val);
|
|
|
|
else
|
|
|
|
uart->freq = DEFAULT_SIFIVE_UART_FREQ;
|
|
|
|
|
|
|
|
val = (fdt32_t *)fdt_getprop(fdt, nodeoffset, "current-speed", &len);
|
|
|
|
if (len > 0 && val)
|
|
|
|
uart->baud = fdt32_to_cpu(*val);
|
|
|
|
else
|
|
|
|
uart->baud = DEFAULT_SIFIVE_UART_BAUD;
|
|
|
|
|
|
|
|
/* For SiFive UART, the reg-shift and reg-io-width are fixed .*/
|
|
|
|
uart->reg_shift = DEFAULT_SIFIVE_UART_REG_SHIFT;
|
|
|
|
uart->reg_io_width = DEFAULT_SIFIVE_UART_REG_IO_WIDTH;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-25 21:03:54 +08:00
|
|
|
int fdt_parse_uart8250_node(void *fdt, int nodeoffset,
|
|
|
|
struct platform_uart_data *uart)
|
2020-03-24 03:48:57 +08:00
|
|
|
{
|
2020-04-25 21:03:54 +08:00
|
|
|
int len, rc;
|
|
|
|
const fdt32_t *val;
|
2020-03-24 03:48:57 +08:00
|
|
|
unsigned long reg_addr, reg_size;
|
|
|
|
|
2020-04-25 21:03:54 +08:00
|
|
|
if (nodeoffset < 0 || !uart || !fdt)
|
|
|
|
return SBI_ENODEV;
|
2020-03-24 03:48:57 +08:00
|
|
|
|
|
|
|
rc = fdt_get_node_addr_size(fdt, nodeoffset, ®_addr, ®_size);
|
|
|
|
if (rc < 0 || !reg_addr || !reg_size)
|
|
|
|
return SBI_ENODEV;
|
|
|
|
uart->addr = reg_addr;
|
|
|
|
|
|
|
|
/**
|
2020-04-25 21:03:54 +08:00
|
|
|
* UART address is mandaotry. clock-frequency and current-speed
|
|
|
|
* may not be present. Don't return error.
|
2020-03-24 03:48:57 +08:00
|
|
|
*/
|
|
|
|
val = (fdt32_t *)fdt_getprop(fdt, nodeoffset, "clock-frequency", &len);
|
|
|
|
if (len > 0 && val)
|
|
|
|
uart->freq = fdt32_to_cpu(*val);
|
2020-04-24 17:51:34 +08:00
|
|
|
else
|
|
|
|
uart->freq = DEFAULT_UART_FREQ;
|
2020-03-24 03:48:57 +08:00
|
|
|
|
|
|
|
val = (fdt32_t *)fdt_getprop(fdt, nodeoffset, "current-speed", &len);
|
|
|
|
if (len > 0 && val)
|
|
|
|
uart->baud = fdt32_to_cpu(*val);
|
2020-04-24 17:51:34 +08:00
|
|
|
else
|
|
|
|
uart->baud = DEFAULT_UART_BAUD;
|
|
|
|
|
|
|
|
val = (fdt32_t *)fdt_getprop(fdt, nodeoffset, "reg-shift", &len);
|
|
|
|
if (len > 0 && val)
|
|
|
|
uart->reg_shift = fdt32_to_cpu(*val);
|
|
|
|
else
|
|
|
|
uart->reg_shift = DEFAULT_UART_REG_SHIFT;
|
|
|
|
|
|
|
|
val = (fdt32_t *)fdt_getprop(fdt, nodeoffset, "reg-io-width", &len);
|
|
|
|
if (len > 0 && val)
|
|
|
|
uart->reg_io_width = fdt32_to_cpu(*val);
|
|
|
|
else
|
|
|
|
uart->reg_io_width = DEFAULT_UART_REG_IO_WIDTH;
|
2020-03-24 03:48:57 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-25 21:03:54 +08:00
|
|
|
int fdt_parse_uart8250(void *fdt, struct platform_uart_data *uart,
|
|
|
|
const char *compatible)
|
|
|
|
{
|
|
|
|
int nodeoffset;
|
|
|
|
|
|
|
|
if (!compatible || !uart || !fdt)
|
|
|
|
return SBI_ENODEV;
|
|
|
|
|
|
|
|
nodeoffset = fdt_node_offset_by_compatible(fdt, -1, compatible);
|
|
|
|
if (nodeoffset < 0)
|
|
|
|
return nodeoffset;
|
|
|
|
|
|
|
|
return fdt_parse_uart8250_node(fdt, nodeoffset, uart);
|
|
|
|
}
|
|
|
|
|
2020-05-12 15:27:52 +08:00
|
|
|
int fdt_parse_plic_node(void *fdt, int nodeoffset, struct plic_data *plic)
|
2020-03-24 03:48:57 +08:00
|
|
|
{
|
2020-04-29 16:51:52 +08:00
|
|
|
int len, rc;
|
2020-03-24 03:48:57 +08:00
|
|
|
const fdt32_t *val;
|
|
|
|
unsigned long reg_addr, reg_size;
|
|
|
|
|
2020-04-29 16:51:52 +08:00
|
|
|
if (nodeoffset < 0 || !plic || !fdt)
|
|
|
|
return SBI_ENODEV;
|
2020-03-24 03:48:57 +08:00
|
|
|
|
|
|
|
rc = fdt_get_node_addr_size(fdt, nodeoffset, ®_addr, ®_size);
|
|
|
|
if (rc < 0 || !reg_addr || !reg_size)
|
|
|
|
return SBI_ENODEV;
|
|
|
|
plic->addr = reg_addr;
|
|
|
|
|
|
|
|
val = fdt_getprop(fdt, nodeoffset, "riscv,ndev", &len);
|
|
|
|
if (len > 0)
|
|
|
|
plic->num_src = fdt32_to_cpu(*val);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-12 15:27:52 +08:00
|
|
|
int fdt_parse_plic(void *fdt, struct plic_data *plic, const char *compat)
|
2020-04-29 16:51:52 +08:00
|
|
|
{
|
|
|
|
int nodeoffset;
|
|
|
|
|
2020-05-12 15:27:52 +08:00
|
|
|
if (!compat || !plic || !fdt)
|
2020-04-29 16:51:52 +08:00
|
|
|
return SBI_ENODEV;
|
|
|
|
|
2020-05-12 15:27:52 +08:00
|
|
|
nodeoffset = fdt_node_offset_by_compatible(fdt, -1, compat);
|
2020-04-29 16:51:52 +08:00
|
|
|
if (nodeoffset < 0)
|
|
|
|
return nodeoffset;
|
|
|
|
|
|
|
|
return fdt_parse_plic_node(fdt, nodeoffset, plic);
|
|
|
|
}
|
|
|
|
|
2020-05-13 12:25:28 +08:00
|
|
|
int fdt_parse_clint_node(void *fdt, int nodeoffset, bool for_timer,
|
|
|
|
struct clint_data *clint)
|
|
|
|
{
|
|
|
|
const fdt32_t *val;
|
|
|
|
unsigned long reg_addr, reg_size;
|
|
|
|
int i, rc, count, cpu_offset, cpu_intc_offset;
|
|
|
|
u32 phandle, hwirq, hartid, first_hartid, last_hartid;
|
|
|
|
u32 match_hwirq = (for_timer) ? IRQ_M_TIMER : IRQ_M_SOFT;
|
|
|
|
|
|
|
|
if (nodeoffset < 0 || !clint || !fdt)
|
|
|
|
return SBI_ENODEV;
|
|
|
|
|
|
|
|
rc = fdt_get_node_addr_size(fdt, nodeoffset, ®_addr, ®_size);
|
|
|
|
if (rc < 0 || !reg_addr || !reg_size)
|
|
|
|
return SBI_ENODEV;
|
|
|
|
clint->addr = reg_addr;
|
|
|
|
|
|
|
|
val = fdt_getprop(fdt, nodeoffset, "interrupts-extended", &count);
|
|
|
|
if (!val || count < sizeof(fdt32_t))
|
|
|
|
return SBI_EINVAL;
|
|
|
|
count = count / sizeof(fdt32_t);
|
|
|
|
|
|
|
|
first_hartid = -1U;
|
|
|
|
last_hartid = 0;
|
|
|
|
clint->hart_count = 0;
|
|
|
|
for (i = 0; i < count; i += 2) {
|
|
|
|
phandle = fdt32_to_cpu(val[i]);
|
|
|
|
hwirq = fdt32_to_cpu(val[i + 1]);
|
|
|
|
|
|
|
|
cpu_intc_offset = fdt_node_offset_by_phandle(fdt, phandle);
|
|
|
|
if (cpu_intc_offset < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
cpu_offset = fdt_parent_offset(fdt, cpu_intc_offset);
|
|
|
|
if (cpu_intc_offset < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
rc = fdt_parse_hart_id(fdt, cpu_offset, &hartid);
|
|
|
|
if (rc)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (SBI_HARTMASK_MAX_BITS <= hartid)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (match_hwirq == hwirq) {
|
|
|
|
if (hartid < first_hartid)
|
|
|
|
first_hartid = hartid;
|
|
|
|
if (hartid > last_hartid)
|
|
|
|
last_hartid = hartid;
|
|
|
|
clint->hart_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((last_hartid < first_hartid) || first_hartid == -1U)
|
|
|
|
return SBI_ENODEV;
|
|
|
|
|
|
|
|
clint->first_hartid = first_hartid;
|
|
|
|
count = last_hartid - first_hartid + 1;
|
|
|
|
if (clint->hart_count < count)
|
|
|
|
clint->hart_count = count;
|
|
|
|
|
|
|
|
/* TODO: We should figure-out CLINT has_64bit_mmio from DT node */
|
|
|
|
clint->has_64bit_mmio = TRUE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-24 19:11:06 +08:00
|
|
|
int fdt_parse_compat_addr(void *fdt, unsigned long *addr,
|
|
|
|
const char *compatible)
|
2020-03-24 03:48:57 +08:00
|
|
|
{
|
|
|
|
int nodeoffset, rc;
|
|
|
|
|
|
|
|
nodeoffset = fdt_node_offset_by_compatible(fdt, -1, compatible);
|
|
|
|
if (nodeoffset < 0)
|
|
|
|
return nodeoffset;
|
|
|
|
|
2020-04-24 19:11:06 +08:00
|
|
|
rc = fdt_get_node_addr_size(fdt, nodeoffset, addr, NULL);
|
|
|
|
if (rc < 0 || !addr)
|
2020-03-24 03:48:57 +08:00
|
|
|
return SBI_ENODEV;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|