blob: a5572cb82551a6d88f99d95cffdf6c89c874cee9 [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 Anderson947fc2d2020-10-07 14:37:44 -040065static u64 sifive_clint_get_count(struct udevice *dev)
Bin Mengb6ee5e12018-12-12 06:12:30 -080066{
Sean Anderson947fc2d2020-10-07 14:37:44 -040067 return readq((void __iomem *)MTIME_REG(dev->priv));
Bin Mengb6ee5e12018-12-12 06:12:30 -080068}
69
Sean Anderson272ab202020-09-28 10:52:26 -040070static const struct timer_ops sifive_clint_ops = {
71 .get_count = sifive_clint_get_count,
72};
73
74static int sifive_clint_probe(struct udevice *dev)
Lukas Auerc7460b82019-12-08 23:28:50 +010075{
Sean Anderson272ab202020-09-28 10:52:26 -040076 dev->priv = dev_read_addr_ptr(dev);
77 if (!dev->priv)
78 return -EINVAL;
Lukas Auerc7460b82019-12-08 23:28:50 +010079
Sean Anderson272ab202020-09-28 10:52:26 -040080 return timer_timebase_fallback(dev);
Lukas Auerc7460b82019-12-08 23:28:50 +010081}
82
Bin Mengb6ee5e12018-12-12 06:12:30 -080083static const struct udevice_id sifive_clint_ids[] = {
Sean Anderson272ab202020-09-28 10:52:26 -040084 { .compatible = "riscv,clint0" },
Bin Mengb6ee5e12018-12-12 06:12:30 -080085 { }
86};
87
88U_BOOT_DRIVER(sifive_clint) = {
89 .name = "sifive_clint",
Sean Anderson272ab202020-09-28 10:52:26 -040090 .id = UCLASS_TIMER,
Bin Mengb6ee5e12018-12-12 06:12:30 -080091 .of_match = sifive_clint_ids,
Sean Anderson272ab202020-09-28 10:52:26 -040092 .probe = sifive_clint_probe,
93 .ops = &sifive_clint_ops,
Bin Mengb6ee5e12018-12-12 06:12:30 -080094 .flags = DM_FLAG_PRE_RELOC,
95};