Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Russell King | 442c817 | 2011-05-08 14:06:52 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Generic MMIO clocksource support |
Russell King | 442c817 | 2011-05-08 14:06:52 +0100 | [diff] [blame] | 4 | */ |
| 5 | #include <linux/clocksource.h> |
| 6 | #include <linux/errno.h> |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/slab.h> |
| 9 | |
| 10 | struct clocksource_mmio { |
| 11 | void __iomem *reg; |
| 12 | struct clocksource clksrc; |
| 13 | }; |
| 14 | |
| 15 | static inline struct clocksource_mmio *to_mmio_clksrc(struct clocksource *c) |
| 16 | { |
| 17 | return container_of(c, struct clocksource_mmio, clksrc); |
| 18 | } |
| 19 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 20 | u64 clocksource_mmio_readl_up(struct clocksource *c) |
Russell King | 442c817 | 2011-05-08 14:06:52 +0100 | [diff] [blame] | 21 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 22 | return (u64)readl_relaxed(to_mmio_clksrc(c)->reg); |
Russell King | 442c817 | 2011-05-08 14:06:52 +0100 | [diff] [blame] | 23 | } |
| 24 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 25 | u64 clocksource_mmio_readl_down(struct clocksource *c) |
Russell King | 442c817 | 2011-05-08 14:06:52 +0100 | [diff] [blame] | 26 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 27 | return ~(u64)readl_relaxed(to_mmio_clksrc(c)->reg) & c->mask; |
Russell King | 442c817 | 2011-05-08 14:06:52 +0100 | [diff] [blame] | 28 | } |
| 29 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 30 | u64 clocksource_mmio_readw_up(struct clocksource *c) |
Russell King | 442c817 | 2011-05-08 14:06:52 +0100 | [diff] [blame] | 31 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 32 | return (u64)readw_relaxed(to_mmio_clksrc(c)->reg); |
Russell King | 442c817 | 2011-05-08 14:06:52 +0100 | [diff] [blame] | 33 | } |
| 34 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 35 | u64 clocksource_mmio_readw_down(struct clocksource *c) |
Russell King | 442c817 | 2011-05-08 14:06:52 +0100 | [diff] [blame] | 36 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 37 | return ~(u64)readw_relaxed(to_mmio_clksrc(c)->reg) & c->mask; |
Russell King | 442c817 | 2011-05-08 14:06:52 +0100 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /** |
| 41 | * clocksource_mmio_init - Initialize a simple mmio based clocksource |
| 42 | * @base: Virtual address of the clock readout register |
| 43 | * @name: Name of the clocksource |
| 44 | * @hz: Frequency of the clocksource in Hz |
| 45 | * @rating: Rating of the clocksource |
| 46 | * @bits: Number of valid bits |
| 47 | * @read: One of clocksource_mmio_read*() above |
| 48 | */ |
| 49 | int __init clocksource_mmio_init(void __iomem *base, const char *name, |
| 50 | unsigned long hz, int rating, unsigned bits, |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 51 | u64 (*read)(struct clocksource *)) |
Russell King | 442c817 | 2011-05-08 14:06:52 +0100 | [diff] [blame] | 52 | { |
| 53 | struct clocksource_mmio *cs; |
| 54 | |
Linus Walleij | e2bf3e6 | 2015-12-10 18:21:41 +0100 | [diff] [blame] | 55 | if (bits > 64 || bits < 16) |
Russell King | 442c817 | 2011-05-08 14:06:52 +0100 | [diff] [blame] | 56 | return -EINVAL; |
| 57 | |
| 58 | cs = kzalloc(sizeof(struct clocksource_mmio), GFP_KERNEL); |
| 59 | if (!cs) |
| 60 | return -ENOMEM; |
| 61 | |
| 62 | cs->reg = base; |
| 63 | cs->clksrc.name = name; |
| 64 | cs->clksrc.rating = rating; |
| 65 | cs->clksrc.read = read; |
| 66 | cs->clksrc.mask = CLOCKSOURCE_MASK(bits); |
| 67 | cs->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS; |
| 68 | |
| 69 | return clocksource_register_hz(&cs->clksrc, hz); |
| 70 | } |