Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 1 | // 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 Anderson | e5ca9a7 | 2020-09-28 10:52:26 -0400 | [diff] [blame] | 11 | #include <clk.h> |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 12 | #include <dm.h> |
Sean Anderson | e5ca9a7 | 2020-09-28 10:52:26 -0400 | [diff] [blame] | 13 | #include <timer.h> |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 14 | #include <asm/io.h> |
| 15 | #include <asm/syscon.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 16 | #include <linux/err.h> |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 17 | |
| 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 | |
| 25 | DECLARE_GLOBAL_DATA_PTR; |
| 26 | |
Sean Anderson | 40686c3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 27 | int riscv_init_ipi(void) |
| 28 | { |
Sean Anderson | e5ca9a7 | 2020-09-28 10:52:26 -0400 | [diff] [blame] | 29 | int ret; |
| 30 | struct udevice *dev; |
Sean Anderson | 40686c3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 31 | |
Sean Anderson | e5ca9a7 | 2020-09-28 10:52:26 -0400 | [diff] [blame] | 32 | 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 Anderson | 40686c3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 44 | int riscv_send_ipi(int hart) |
| 45 | { |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 46 | writel(1, (void __iomem *)MSIP_REG(gd->arch.clint, hart)); |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | int riscv_clear_ipi(int hart) |
| 52 | { |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 53 | writel(0, (void __iomem *)MSIP_REG(gd->arch.clint, hart)); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
Lukas Auer | 8b3e97b | 2019-12-08 23:28:50 +0100 | [diff] [blame] | 58 | int riscv_get_ipi(int hart, int *pending) |
| 59 | { |
Lukas Auer | 8b3e97b | 2019-12-08 23:28:50 +0100 | [diff] [blame] | 60 | *pending = readl((void __iomem *)MSIP_REG(gd->arch.clint, hart)); |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Sean Anderson | e5ca9a7 | 2020-09-28 10:52:26 -0400 | [diff] [blame] | 65 | static 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 | |
| 72 | static const struct timer_ops sifive_clint_ops = { |
| 73 | .get_count = sifive_clint_get_count, |
| 74 | }; |
| 75 | |
| 76 | static 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 Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 85 | static const struct udevice_id sifive_clint_ids[] = { |
Sean Anderson | e5ca9a7 | 2020-09-28 10:52:26 -0400 | [diff] [blame] | 86 | { .compatible = "riscv,clint0" }, |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 87 | { } |
| 88 | }; |
| 89 | |
| 90 | U_BOOT_DRIVER(sifive_clint) = { |
| 91 | .name = "sifive_clint", |
Sean Anderson | e5ca9a7 | 2020-09-28 10:52:26 -0400 | [diff] [blame] | 92 | .id = UCLASS_TIMER, |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 93 | .of_match = sifive_clint_ids, |
Sean Anderson | e5ca9a7 | 2020-09-28 10:52:26 -0400 | [diff] [blame] | 94 | .probe = sifive_clint_probe, |
| 95 | .ops = &sifive_clint_ops, |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 96 | .flags = DM_FLAG_PRE_RELOC, |
| 97 | }; |