Rick Chen | a1f2487 | 2019-04-02 15:56:40 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2019, Rick Chen <rick@andestech.com> |
| 4 | * |
| 5 | * U-Boot syscon driver for Andes's Platform Level Machine Timer (PLMT). |
| 6 | * The PLMT block holds memory-mapped mtime register |
| 7 | * associated with timer tick. |
| 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> |
Rick Chen | a1f2487 | 2019-04-02 15:56:40 +0800 | [diff] [blame] | 17 | |
| 18 | /* mtime register */ |
| 19 | #define MTIME_REG(base) ((ulong)(base)) |
| 20 | |
| 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
| 23 | #define PLMT_BASE_GET(void) \ |
| 24 | do { \ |
| 25 | long *ret; \ |
| 26 | \ |
| 27 | if (!gd->arch.plmt) { \ |
| 28 | ret = syscon_get_first_range(RISCV_SYSCON_PLMT); \ |
| 29 | if (IS_ERR(ret)) \ |
| 30 | return PTR_ERR(ret); \ |
| 31 | gd->arch.plmt = ret; \ |
| 32 | } \ |
| 33 | } while (0) |
| 34 | |
| 35 | int riscv_get_time(u64 *time) |
| 36 | { |
| 37 | PLMT_BASE_GET(); |
| 38 | |
| 39 | *time = readq((void __iomem *)MTIME_REG(gd->arch.plmt)); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static const struct udevice_id andes_plmt_ids[] = { |
| 45 | { .compatible = "riscv,plmt0", .data = RISCV_SYSCON_PLMT }, |
| 46 | { } |
| 47 | }; |
| 48 | |
| 49 | U_BOOT_DRIVER(andes_plmt) = { |
| 50 | .name = "andes_plmt", |
| 51 | .id = UCLASS_SYSCON, |
| 52 | .of_match = andes_plmt_ids, |
| 53 | .flags = DM_FLAG_PRE_RELOC, |
| 54 | }; |