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> |
| 11 | #include <dm.h> |
| 12 | #include <regmap.h> |
| 13 | #include <syscon.h> |
| 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 | |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 27 | int riscv_get_time(u64 *time) |
| 28 | { |
Bin Meng | a0018fc | 2020-07-19 23:17:07 -0700 | [diff] [blame] | 29 | /* ensure timer register base has a sane value */ |
| 30 | riscv_init_ipi(); |
| 31 | |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 32 | *time = readq((void __iomem *)MTIME_REG(gd->arch.clint)); |
| 33 | |
| 34 | return 0; |
| 35 | } |
| 36 | |
| 37 | int riscv_set_timecmp(int hart, u64 cmp) |
| 38 | { |
Bin Meng | a0018fc | 2020-07-19 23:17:07 -0700 | [diff] [blame] | 39 | /* ensure timer register base has a sane value */ |
| 40 | riscv_init_ipi(); |
| 41 | |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 42 | writeq(cmp, (void __iomem *)MTIMECMP_REG(gd->arch.clint, hart)); |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
Sean Anderson | 40686c3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 47 | int riscv_init_ipi(void) |
| 48 | { |
Bin Meng | a0018fc | 2020-07-19 23:17:07 -0700 | [diff] [blame] | 49 | if (!gd->arch.clint) { |
| 50 | long *ret = syscon_get_first_range(RISCV_SYSCON_CLINT); |
Sean Anderson | 40686c3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 51 | |
Bin Meng | a0018fc | 2020-07-19 23:17:07 -0700 | [diff] [blame] | 52 | if (IS_ERR(ret)) |
| 53 | return PTR_ERR(ret); |
| 54 | gd->arch.clint = ret; |
| 55 | } |
Sean Anderson | 40686c3 | 2020-06-24 06:41:18 -0400 | [diff] [blame] | 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 60 | int riscv_send_ipi(int hart) |
| 61 | { |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 62 | writel(1, (void __iomem *)MSIP_REG(gd->arch.clint, hart)); |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | int riscv_clear_ipi(int hart) |
| 68 | { |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 69 | writel(0, (void __iomem *)MSIP_REG(gd->arch.clint, hart)); |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
Lukas Auer | 8b3e97b | 2019-12-08 23:28:50 +0100 | [diff] [blame] | 74 | int riscv_get_ipi(int hart, int *pending) |
| 75 | { |
Lukas Auer | 8b3e97b | 2019-12-08 23:28:50 +0100 | [diff] [blame] | 76 | *pending = readl((void __iomem *)MSIP_REG(gd->arch.clint, hart)); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Bin Meng | 644a3cd | 2018-12-12 06:12:30 -0800 | [diff] [blame] | 81 | static const struct udevice_id sifive_clint_ids[] = { |
| 82 | { .compatible = "riscv,clint0", .data = RISCV_SYSCON_CLINT }, |
| 83 | { } |
| 84 | }; |
| 85 | |
| 86 | U_BOOT_DRIVER(sifive_clint) = { |
| 87 | .name = "sifive_clint", |
| 88 | .id = UCLASS_SYSCON, |
| 89 | .of_match = sifive_clint_ids, |
| 90 | .flags = DM_FLAG_PRE_RELOC, |
| 91 | }; |