summaryrefslogtreecommitdiffstats
path: root/arch/riscv/cpu/core.c
blob: 1d5902a51f0b7232e064db948c3f90ca58455e62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2012 Regents of the University of California
 * Copyright (C) 2017 SiFive
 * Copyright (C) 2021 Ahmad Fatoum, Pengutronix
 *
 * Common RISC-V core initcalls.
 *
 * All RISC-V systems have a timer attached to every hart.  These timers can
 * either be read from the "time" and "timeh" CSRs, and can use the SBI to
 * setup events, or directly accessed using MMIO registers.
 */
#include <common.h>
#include <init.h>
#include <clock.h>
#include <errno.h>
#include <of.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <memory.h>
#include <asm/barebox-riscv.h>
#include <asm-generic/memory_layout.h>
#include <globalvar.h>
#include <magicvar.h>
#include <asm/system.h>
#include <io.h>

static int riscv_request_stack(void)
{
	extern unsigned long riscv_stack_top;
	return PTR_ERR_OR_ZERO(request_sdram_region("stack", riscv_stack_top - STACK_SIZE, STACK_SIZE));
}
coredevice_initcall(riscv_request_stack);

static struct device_d timer_dev;
static struct device_d serial_sbi_dev;

static s64 hartid;

BAREBOX_MAGICVAR(global.hartid, "RISC-V hartid");

static int riscv_fixup_cpus(struct device_node *root, void *context)
{
	struct device_node *cpus_node, *np, *tmp;

	cpus_node = of_find_node_by_name(root, "cpus");
	if (!cpus_node)
		return 0;

	for_each_child_of_node_safe(cpus_node, tmp, np) {
		u32 cpu_index;

		if (of_property_read_u32(np, "reg", &cpu_index))
			continue;

		if (cpu_index != hartid)
			of_delete_node(np);
	}

	return 0;
}

static int riscv_probe(struct device_d *parent)
{
	int ret;

	/* Each hart has a timer, but we only need one */
	if (IS_ENABLED(CONFIG_RISCV_TIMER) && !timer_dev.parent) {
		timer_dev.id = DEVICE_ID_SINGLE;
		timer_dev.device_node = parent->device_node;
		timer_dev.parent = parent;
		dev_set_name(&timer_dev, "riscv-timer");

		ret = platform_device_register(&timer_dev);
		if (ret)
			return ret;
	}

	if (IS_ENABLED(CONFIG_SERIAL_SBI) && !serial_sbi_dev.parent) {
		serial_sbi_dev.id = DEVICE_ID_SINGLE;
		serial_sbi_dev.device_node = 0;
		serial_sbi_dev.parent = parent;
		dev_set_name(&serial_sbi_dev, "riscv-serial-sbi");

		ret = platform_device_register(&serial_sbi_dev);
		if (ret)
			return ret;
	}

	hartid = riscv_hartid();
	if (hartid >= 0)
		globalvar_add_simple_uint64("hartid", &hartid, "%llu");

	return of_register_fixup(riscv_fixup_cpus, NULL);
}

static struct of_device_id riscv_dt_ids[] = {
	{ .compatible = "riscv", },
	{ /* sentinel */ },
};

static struct driver_d riscv_driver = {
	.name = "riscv",
	.probe = riscv_probe,
	.of_compatible = riscv_dt_ids,
};
postcore_platform_driver(riscv_driver);

static void arch_shutdown(void)
{
	sync_caches_for_execution();
}
archshutdown_exitcall(arch_shutdown);