blob: b9a2c649cc459bd7b8cca69964640fdc0ef81fda [file] [log] [blame]
Bin Meng644a3cd2018-12-12 06:12:30 -08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
4 *
5 * U-Boot syscon driver for SiFive's Core Local Interruptor (CLINT).
6 * The CLINT block holds memory-mapped control and status registers
7 * associated with software and timer interrupts.
8 */
9
10#include <common.h>
11#include <dm.h>
12#include <regmap.h>
13#include <syscon.h>
14#include <asm/io.h>
15#include <asm/syscon.h>
Simon Glass61b29b82020-02-03 07:36:15 -070016#include <linux/err.h>
Bin Meng644a3cd2018-12-12 06:12:30 -080017
18/* MSIP registers */
19#define MSIP_REG(base, hart) ((ulong)(base) + (hart) * 4)
20/* mtime compare register */
21#define MTIMECMP_REG(base, hart) ((ulong)(base) + 0x4000 + (hart) * 8)
22/* mtime register */
23#define MTIME_REG(base) ((ulong)(base) + 0xbff8)
24
25DECLARE_GLOBAL_DATA_PTR;
26
Bin Meng644a3cd2018-12-12 06:12:30 -080027int riscv_get_time(u64 *time)
28{
Bin Menga0018fc2020-07-19 23:17:07 -070029 /* ensure timer register base has a sane value */
30 riscv_init_ipi();
31
Bin Meng644a3cd2018-12-12 06:12:30 -080032 *time = readq((void __iomem *)MTIME_REG(gd->arch.clint));
33
34 return 0;
35}
36
37int riscv_set_timecmp(int hart, u64 cmp)
38{
Bin Menga0018fc2020-07-19 23:17:07 -070039 /* ensure timer register base has a sane value */
40 riscv_init_ipi();
41
Bin Meng644a3cd2018-12-12 06:12:30 -080042 writeq(cmp, (void __iomem *)MTIMECMP_REG(gd->arch.clint, hart));
43
44 return 0;
45}
46
Sean Anderson40686c32020-06-24 06:41:18 -040047int riscv_init_ipi(void)
48{
Bin Menga0018fc2020-07-19 23:17:07 -070049 if (!gd->arch.clint) {
50 long *ret = syscon_get_first_range(RISCV_SYSCON_CLINT);
Sean Anderson40686c32020-06-24 06:41:18 -040051
Bin Menga0018fc2020-07-19 23:17:07 -070052 if (IS_ERR(ret))
53 return PTR_ERR(ret);
54 gd->arch.clint = ret;
55 }
Sean Anderson40686c32020-06-24 06:41:18 -040056
57 return 0;
58}
59
Bin Meng644a3cd2018-12-12 06:12:30 -080060int riscv_send_ipi(int hart)
61{
Bin Meng644a3cd2018-12-12 06:12:30 -080062 writel(1, (void __iomem *)MSIP_REG(gd->arch.clint, hart));
63
64 return 0;
65}
66
67int riscv_clear_ipi(int hart)
68{
Bin Meng644a3cd2018-12-12 06:12:30 -080069 writel(0, (void __iomem *)MSIP_REG(gd->arch.clint, hart));
70
71 return 0;
72}
73
Lukas Auer8b3e97b2019-12-08 23:28:50 +010074int riscv_get_ipi(int hart, int *pending)
75{
Lukas Auer8b3e97b2019-12-08 23:28:50 +010076 *pending = readl((void __iomem *)MSIP_REG(gd->arch.clint, hart));
77
78 return 0;
79}
80
Bin Meng644a3cd2018-12-12 06:12:30 -080081static const struct udevice_id sifive_clint_ids[] = {
82 { .compatible = "riscv,clint0", .data = RISCV_SYSCON_CLINT },
83 { }
84};
85
86U_BOOT_DRIVER(sifive_clint) = {
87 .name = "sifive_clint",
88 .id = UCLASS_SYSCON,
89 .of_match = sifive_clint_ids,
90 .flags = DM_FLAG_PRE_RELOC,
91};