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