blob: 78fc6c868dd0dcfba1db009136b5481622ad9c40 [file] [log] [blame]
Bin Mengb6ee5e12018-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 Glassd66c5f72020-02-03 07:36:15 -070016#include <linux/err.h>
Bin Mengb6ee5e12018-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 Mengb6ee5e12018-12-12 06:12:30 -080027int riscv_get_time(u64 *time)
28{
Bin Mengb6ee5e12018-12-12 06:12:30 -080029 *time = readq((void __iomem *)MTIME_REG(gd->arch.clint));
30
31 return 0;
32}
33
34int riscv_set_timecmp(int hart, u64 cmp)
35{
Bin Mengb6ee5e12018-12-12 06:12:30 -080036 writeq(cmp, (void __iomem *)MTIMECMP_REG(gd->arch.clint, hart));
37
38 return 0;
39}
40
Sean Andersonb1d0cb32020-06-24 06:41:18 -040041int riscv_init_ipi(void)
Bin Mengb6ee5e12018-12-12 06:12:30 -080042{
Sean Andersonb1d0cb32020-06-24 06:41:18 -040043 long *ret = syscon_get_first_range(RISCV_SYSCON_CLINT);
44
45 if (IS_ERR(ret))
46 return PTR_ERR(ret);
47 gd->arch.clint = ret;
48
49 return 0;
50}
Bin Mengb6ee5e12018-12-12 06:12:30 -080051
Sean Andersonb1d0cb32020-06-24 06:41:18 -040052int riscv_send_ipi(int hart)
53{
Bin Mengb6ee5e12018-12-12 06:12:30 -080054 writel(1, (void __iomem *)MSIP_REG(gd->arch.clint, hart));
55
56 return 0;
57}
58
59int riscv_clear_ipi(int hart)
60{
Bin Mengb6ee5e12018-12-12 06:12:30 -080061 writel(0, (void __iomem *)MSIP_REG(gd->arch.clint, hart));
62
63 return 0;
64}
65
Lukas Auerc7460b82019-12-08 23:28:50 +010066int riscv_get_ipi(int hart, int *pending)
67{
Lukas Auerc7460b82019-12-08 23:28:50 +010068 *pending = readl((void __iomem *)MSIP_REG(gd->arch.clint, hart));
69
70 return 0;
71}
72
Bin Mengb6ee5e12018-12-12 06:12:30 -080073static const struct udevice_id sifive_clint_ids[] = {
74 { .compatible = "riscv,clint0", .data = RISCV_SYSCON_CLINT },
75 { }
76};
77
78U_BOOT_DRIVER(sifive_clint) = {
79 .name = "sifive_clint",
80 .id = UCLASS_SYSCON,
81 .of_match = sifive_clint_ids,
82 .flags = DM_FLAG_PRE_RELOC,
83};