blob: e662140427a563caaef8c1ffe5c2e710db4811ca [file] [log] [blame]
Bin Meng055700e2018-09-26 06:55:14 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
4 */
5
6#include <common.h>
Bin Meng7a3bbfb2018-12-12 06:12:34 -08007#include <cpu.h>
Bin Mengedfe9a92018-12-12 06:12:38 -08008#include <dm.h>
Bin Meng7a3bbfb2018-12-12 06:12:34 -08009#include <log.h>
Bin Meng055700e2018-09-26 06:55:14 -070010#include <asm/csr.h>
Bin Menga7544ed2018-12-12 06:12:40 -080011#include <asm/encoding.h>
Bin Mengedfe9a92018-12-12 06:12:38 -080012#include <dm/uclass-internal.h>
Bin Meng055700e2018-09-26 06:55:14 -070013
Lukas Auer39a652b2018-11-22 11:26:29 +010014/*
15 * prior_stage_fdt_address must be stored in the data section since it is used
16 * before the bss section is available.
17 */
18phys_addr_t prior_stage_fdt_address __attribute__((section(".data")));
19
Bin Meng055700e2018-09-26 06:55:14 -070020static inline bool supports_extension(char ext)
21{
Bin Mengedfe9a92018-12-12 06:12:38 -080022#ifdef CONFIG_CPU
23 struct udevice *dev;
24 char desc[32];
25
26 uclass_find_first_device(UCLASS_CPU, &dev);
27 if (!dev) {
28 debug("unable to find the RISC-V cpu device\n");
29 return false;
30 }
31 if (!cpu_get_desc(dev, desc, sizeof(desc))) {
32 /* skip the first 4 characters (rv32|rv64) */
33 if (strchr(desc + 4, ext))
34 return true;
35 }
36
37 return false;
38#else /* !CONFIG_CPU */
39#ifdef CONFIG_RISCV_MMODE
Bin Meng055700e2018-09-26 06:55:14 -070040 return csr_read(misa) & (1 << (ext - 'a'));
Bin Mengedfe9a92018-12-12 06:12:38 -080041#else /* !CONFIG_RISCV_MMODE */
42#warning "There is no way to determine the available extensions in S-mode."
43#warning "Please convert your board to use the RISC-V CPU driver."
44 return false;
45#endif /* CONFIG_RISCV_MMODE */
46#endif /* CONFIG_CPU */
Bin Meng055700e2018-09-26 06:55:14 -070047}
48
Bin Meng7a3bbfb2018-12-12 06:12:34 -080049static int riscv_cpu_probe(void)
50{
51#ifdef CONFIG_CPU
52 int ret;
53
54 /* probe cpus so that RISC-V timer can be bound */
55 ret = cpu_probe_all();
56 if (ret)
57 return log_msg_ret("RISC-V cpus probe failed\n", ret);
58#endif
59
60 return 0;
61}
62
63int arch_cpu_init_dm(void)
64{
Bin Menga7544ed2018-12-12 06:12:40 -080065 int ret;
66
67 ret = riscv_cpu_probe();
68 if (ret)
69 return ret;
70
71 /* Enable FPU */
72 if (supports_extension('d') || supports_extension('f')) {
73 csr_set(MODE_PREFIX(status), MSTATUS_FS);
74 csr_write(fcsr, 0);
75 }
76
77 if (CONFIG_IS_ENABLED(RISCV_MMODE)) {
78 /*
79 * Enable perf counters for cycle, time,
80 * and instret counters only
81 */
82 csr_write(mcounteren, GENMASK(2, 0));
83
84 /* Disable paging */
85 if (supports_extension('s'))
86 csr_write(satp, 0);
87 }
88
89 return 0;
Bin Meng7a3bbfb2018-12-12 06:12:34 -080090}
91
92int arch_early_init_r(void)
93{
94 return riscv_cpu_probe();
95}