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> |
Sean Anderson | e86463f | 2020-09-28 10:52:24 -0400 | [diff] [blame] | 4 | * Copyright (C) 2020, Sean Anderson <seanga2@gmail.com> |
Rick Chen | a1f2487 | 2019-04-02 15:56:40 +0800 | [diff] [blame] | 5 | * |
| 6 | * U-Boot syscon driver for Andes's Platform Level Machine Timer (PLMT). |
| 7 | * The PLMT block holds memory-mapped mtime register |
| 8 | * associated with timer tick. |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <dm.h> |
Sean Anderson | e86463f | 2020-09-28 10:52:24 -0400 | [diff] [blame] | 13 | #include <timer.h> |
Rick Chen | a1f2487 | 2019-04-02 15:56:40 +0800 | [diff] [blame] | 14 | #include <asm/io.h> |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 15 | #include <dm/device-internal.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 | |
Sean Anderson | 8af7bb9 | 2020-10-07 14:37:44 -0400 | [diff] [blame] | 21 | static u64 andes_plmt_get_count(struct udevice *dev) |
Rick Chen | a1f2487 | 2019-04-02 15:56:40 +0800 | [diff] [blame] | 22 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 23 | return readq((void __iomem *)MTIME_REG(dev_get_priv(dev))); |
Rick Chen | a1f2487 | 2019-04-02 15:56:40 +0800 | [diff] [blame] | 24 | } |
| 25 | |
Sean Anderson | e86463f | 2020-09-28 10:52:24 -0400 | [diff] [blame] | 26 | static const struct timer_ops andes_plmt_ops = { |
| 27 | .get_count = andes_plmt_get_count, |
| 28 | }; |
| 29 | |
| 30 | static int andes_plmt_probe(struct udevice *dev) |
| 31 | { |
Simon Glass | 0fd3d91 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 32 | dev_set_priv(dev, dev_read_addr_ptr(dev)); |
| 33 | if (!dev_get_priv(dev)) |
Sean Anderson | e86463f | 2020-09-28 10:52:24 -0400 | [diff] [blame] | 34 | return -EINVAL; |
| 35 | |
| 36 | return timer_timebase_fallback(dev); |
| 37 | } |
| 38 | |
Rick Chen | a1f2487 | 2019-04-02 15:56:40 +0800 | [diff] [blame] | 39 | static const struct udevice_id andes_plmt_ids[] = { |
Sean Anderson | e86463f | 2020-09-28 10:52:24 -0400 | [diff] [blame] | 40 | { .compatible = "riscv,plmt0" }, |
Rick Chen | a1f2487 | 2019-04-02 15:56:40 +0800 | [diff] [blame] | 41 | { } |
| 42 | }; |
| 43 | |
| 44 | U_BOOT_DRIVER(andes_plmt) = { |
| 45 | .name = "andes_plmt", |
Sean Anderson | e86463f | 2020-09-28 10:52:24 -0400 | [diff] [blame] | 46 | .id = UCLASS_TIMER, |
Rick Chen | a1f2487 | 2019-04-02 15:56:40 +0800 | [diff] [blame] | 47 | .of_match = andes_plmt_ids, |
Sean Anderson | e86463f | 2020-09-28 10:52:24 -0400 | [diff] [blame] | 48 | .ops = &andes_plmt_ops, |
| 49 | .probe = andes_plmt_probe, |
Rick Chen | a1f2487 | 2019-04-02 15:56:40 +0800 | [diff] [blame] | 50 | .flags = DM_FLAG_PRE_RELOC, |
| 51 | }; |