blob: c9704c596fa6707ceef1186b8e6a55d8f50ce806 [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>
Sean Anderson272ab202020-09-28 10:52:26 -040011#include <clk.h>
Bin Mengb6ee5e12018-12-12 06:12:30 -080012#include <dm.h>
Sean Anderson272ab202020-09-28 10:52:26 -040013#include <timer.h>
Bin Mengb6ee5e12018-12-12 06:12:30 -080014#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
Sean Anderson272ab202020-09-28 10:52:26 -040027int riscv_init_ipi(void)
Bin Mengb6ee5e12018-12-12 06:12:30 -080028{
Sean Anderson272ab202020-09-28 10:52:26 -040029 int ret;
30 struct udevice *dev;
Bin Meng257875d2020-07-19 23:17:07 -070031
Sean Anderson272ab202020-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;
Bin Mengb6ee5e12018-12-12 06:12:30 -080040
41 return 0;
42}
43
Sean Anderson272ab202020-09-28 10:52:26 -040044int riscv_send_ipi(int hart)
Bin Mengb6ee5e12018-12-12 06:12:30 -080045{
Sean Anderson272ab202020-09-28 10:52:26 -040046 writel(1, (void __iomem *)MSIP_REG(gd->arch.clint, hart));
Bin Mengb6ee5e12018-12-12 06:12:30 -080047
48 return 0;
49}
50
Sean Anderson272ab202020-09-28 10:52:26 -040051int riscv_clear_ipi(int hart)
Bin Mengb6ee5e12018-12-12 06:12:30 -080052{
Sean Anderson272ab202020-09-28 10:52:26 -040053 writel(0, (void __iomem *)MSIP_REG(gd->arch.clint, hart));
Sean Andersonb1d0cb32020-06-24 06:41:18 -040054
55 return 0;
56}
Bin Mengb6ee5e12018-12-12 06:12:30 -080057
Sean Anderson272ab202020-09-28 10:52:26 -040058int riscv_get_ipi(int hart, int *pending)
Sean Andersonb1d0cb32020-06-24 06:41:18 -040059{
Sean Anderson272ab202020-09-28 10:52:26 -040060 *pending = readl((void __iomem *)MSIP_REG(gd->arch.clint, hart));
Bin Mengb6ee5e12018-12-12 06:12:30 -080061
62 return 0;
63}
64
Sean Anderson272ab202020-09-28 10:52:26 -040065static int sifive_clint_get_count(struct udevice *dev, u64 *count)
Bin Mengb6ee5e12018-12-12 06:12:30 -080066{
Sean Anderson272ab202020-09-28 10:52:26 -040067 *count = readq((void __iomem *)MTIME_REG(dev->priv));
Bin Mengb6ee5e12018-12-12 06:12:30 -080068
69 return 0;
70}
71
Sean Anderson272ab202020-09-28 10:52:26 -040072static const struct timer_ops sifive_clint_ops = {
73 .get_count = sifive_clint_get_count,
74};
75
76static int sifive_clint_probe(struct udevice *dev)
Lukas Auerc7460b82019-12-08 23:28:50 +010077{
Sean Anderson272ab202020-09-28 10:52:26 -040078 dev->priv = dev_read_addr_ptr(dev);
79 if (!dev->priv)
80 return -EINVAL;
Lukas Auerc7460b82019-12-08 23:28:50 +010081
Sean Anderson272ab202020-09-28 10:52:26 -040082 return timer_timebase_fallback(dev);
Lukas Auerc7460b82019-12-08 23:28:50 +010083}
84
Bin Mengb6ee5e12018-12-12 06:12:30 -080085static const struct udevice_id sifive_clint_ids[] = {
Sean Anderson272ab202020-09-28 10:52:26 -040086 { .compatible = "riscv,clint0" },
Bin Mengb6ee5e12018-12-12 06:12:30 -080087 { }
88};
89
90U_BOOT_DRIVER(sifive_clint) = {
91 .name = "sifive_clint",
Sean Anderson272ab202020-09-28 10:52:26 -040092 .id = UCLASS_TIMER,
Bin Mengb6ee5e12018-12-12 06:12:30 -080093 .of_match = sifive_clint_ids,
Sean Anderson272ab202020-09-28 10:52:26 -040094 .probe = sifive_clint_probe,
95 .ops = &sifive_clint_ops,
Bin Mengb6ee5e12018-12-12 06:12:30 -080096 .flags = DM_FLAG_PRE_RELOC,
97};