blob: c9704c596fa6707ceef1186b8e6a55d8f50ce806 [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>
Sean Andersone5ca9a72020-09-28 10:52:26 -040011#include <clk.h>
Bin Meng644a3cd2018-12-12 06:12:30 -080012#include <dm.h>
Sean Andersone5ca9a72020-09-28 10:52:26 -040013#include <timer.h>
Bin Meng644a3cd2018-12-12 06:12:30 -080014#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
Sean Anderson40686c32020-06-24 06:41:18 -040027int riscv_init_ipi(void)
28{
Sean Andersone5ca9a72020-09-28 10:52:26 -040029 int ret;
30 struct udevice *dev;
Sean Anderson40686c32020-06-24 06:41:18 -040031
Sean Andersone5ca9a72020-09-28 10:52:26 -040032 ret = uclass_get_device_by_driver(UCLASS_TIMER,
33 DM_GET_DRIVER(sifive_clint), &dev);
34 if (ret)
35 return ret;
36
37 gd->arch.clint = dev_read_addr_ptr(dev);
38 if (!gd->arch.clint)
39 return -EINVAL;
Sean Anderson40686c32020-06-24 06:41:18 -040040
41 return 0;
42}
43
Bin Meng644a3cd2018-12-12 06:12:30 -080044int riscv_send_ipi(int hart)
45{
Bin Meng644a3cd2018-12-12 06:12:30 -080046 writel(1, (void __iomem *)MSIP_REG(gd->arch.clint, hart));
47
48 return 0;
49}
50
51int riscv_clear_ipi(int hart)
52{
Bin Meng644a3cd2018-12-12 06:12:30 -080053 writel(0, (void __iomem *)MSIP_REG(gd->arch.clint, hart));
54
55 return 0;
56}
57
Lukas Auer8b3e97b2019-12-08 23:28:50 +010058int riscv_get_ipi(int hart, int *pending)
59{
Lukas Auer8b3e97b2019-12-08 23:28:50 +010060 *pending = readl((void __iomem *)MSIP_REG(gd->arch.clint, hart));
61
62 return 0;
63}
64
Sean Andersone5ca9a72020-09-28 10:52:26 -040065static int sifive_clint_get_count(struct udevice *dev, u64 *count)
66{
67 *count = readq((void __iomem *)MTIME_REG(dev->priv));
68
69 return 0;
70}
71
72static const struct timer_ops sifive_clint_ops = {
73 .get_count = sifive_clint_get_count,
74};
75
76static int sifive_clint_probe(struct udevice *dev)
77{
78 dev->priv = dev_read_addr_ptr(dev);
79 if (!dev->priv)
80 return -EINVAL;
81
82 return timer_timebase_fallback(dev);
83}
84
Bin Meng644a3cd2018-12-12 06:12:30 -080085static const struct udevice_id sifive_clint_ids[] = {
Sean Andersone5ca9a72020-09-28 10:52:26 -040086 { .compatible = "riscv,clint0" },
Bin Meng644a3cd2018-12-12 06:12:30 -080087 { }
88};
89
90U_BOOT_DRIVER(sifive_clint) = {
91 .name = "sifive_clint",
Sean Andersone5ca9a72020-09-28 10:52:26 -040092 .id = UCLASS_TIMER,
Bin Meng644a3cd2018-12-12 06:12:30 -080093 .of_match = sifive_clint_ids,
Sean Andersone5ca9a72020-09-28 10:52:26 -040094 .probe = sifive_clint_probe,
95 .ops = &sifive_clint_ops,
Bin Meng644a3cd2018-12-12 06:12:30 -080096 .flags = DM_FLAG_PRE_RELOC,
97};