Bin Meng | b6ee5e1 | 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 | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 11 | #include <clk.h> |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 12 | #include <dm.h> |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 13 | #include <timer.h> |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 14 | #include <asm/io.h> |
| 15 | #include <asm/syscon.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 16 | #include <linux/err.h> |
Bin Meng | b6ee5e1 | 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 | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 27 | int riscv_init_ipi(void) |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 28 | { |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 29 | int ret; |
| 30 | struct udevice *dev; |
Bin Meng | 257875d | 2020-07-19 23:17:07 -0700 | [diff] [blame] | 31 | |
Sean Anderson | 272ab20 | 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; |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 44 | int riscv_send_ipi(int hart) |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 45 | { |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 46 | writel(1, (void __iomem *)MSIP_REG(gd->arch.clint, hart)); |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 51 | int riscv_clear_ipi(int hart) |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 52 | { |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 53 | writel(0, (void __iomem *)MSIP_REG(gd->arch.clint, hart)); |
Sean Anderson | b1d0cb3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 54 | |
| 55 | return 0; |
| 56 | } |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 57 | |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 58 | int riscv_get_ipi(int hart, int *pending) |
Sean Anderson | b1d0cb3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 59 | { |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 60 | *pending = readl((void __iomem *)MSIP_REG(gd->arch.clint, hart)); |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 65 | static int sifive_clint_get_count(struct udevice *dev, u64 *count) |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 66 | { |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 67 | *count = readq((void __iomem *)MTIME_REG(dev->priv)); |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 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) |
Lukas Auer | c7460b8 | 2019-12-08 23:28:50 +0100 | [diff] [blame] | 77 | { |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 78 | dev->priv = dev_read_addr_ptr(dev); |
| 79 | if (!dev->priv) |
| 80 | return -EINVAL; |
Lukas Auer | c7460b8 | 2019-12-08 23:28:50 +0100 | [diff] [blame] | 81 | |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 82 | return timer_timebase_fallback(dev); |
Lukas Auer | c7460b8 | 2019-12-08 23:28:50 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 85 | static const struct udevice_id sifive_clint_ids[] = { |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 86 | { .compatible = "riscv,clint0" }, |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 87 | { } |
| 88 | }; |
| 89 | |
| 90 | U_BOOT_DRIVER(sifive_clint) = { |
| 91 | .name = "sifive_clint", |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 92 | .id = UCLASS_TIMER, |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 93 | .of_match = sifive_clint_ids, |
Sean Anderson | 272ab20 | 2020-09-28 10:52:26 -0400 | [diff] [blame^] | 94 | .probe = sifive_clint_probe, |
| 95 | .ops = &sifive_clint_ops, |
Bin Meng | b6ee5e1 | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 96 | .flags = DM_FLAG_PRE_RELOC, |
| 97 | }; |