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