blob: 9de75153183124cc8997c6ab61d0c01d9b2637bc [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Russell King442c8172011-05-08 14:06:52 +01002/*
3 * Generic MMIO clocksource support
Russell King442c8172011-05-08 14:06:52 +01004 */
5#include <linux/clocksource.h>
6#include <linux/errno.h>
7#include <linux/init.h>
8#include <linux/slab.h>
9
10struct clocksource_mmio {
11 void __iomem *reg;
12 struct clocksource clksrc;
13};
14
15static inline struct clocksource_mmio *to_mmio_clksrc(struct clocksource *c)
16{
17 return container_of(c, struct clocksource_mmio, clksrc);
18}
19
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010020u64 clocksource_mmio_readl_up(struct clocksource *c)
Russell King442c8172011-05-08 14:06:52 +010021{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010022 return (u64)readl_relaxed(to_mmio_clksrc(c)->reg);
Russell King442c8172011-05-08 14:06:52 +010023}
24
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010025u64 clocksource_mmio_readl_down(struct clocksource *c)
Russell King442c8172011-05-08 14:06:52 +010026{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010027 return ~(u64)readl_relaxed(to_mmio_clksrc(c)->reg) & c->mask;
Russell King442c8172011-05-08 14:06:52 +010028}
29
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010030u64 clocksource_mmio_readw_up(struct clocksource *c)
Russell King442c8172011-05-08 14:06:52 +010031{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010032 return (u64)readw_relaxed(to_mmio_clksrc(c)->reg);
Russell King442c8172011-05-08 14:06:52 +010033}
34
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010035u64 clocksource_mmio_readw_down(struct clocksource *c)
Russell King442c8172011-05-08 14:06:52 +010036{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010037 return ~(u64)readw_relaxed(to_mmio_clksrc(c)->reg) & c->mask;
Russell King442c8172011-05-08 14:06:52 +010038}
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 */
49int __init clocksource_mmio_init(void __iomem *base, const char *name,
50 unsigned long hz, int rating, unsigned bits,
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010051 u64 (*read)(struct clocksource *))
Russell King442c8172011-05-08 14:06:52 +010052{
53 struct clocksource_mmio *cs;
54
Linus Walleije2bf3e62015-12-10 18:21:41 +010055 if (bits > 64 || bits < 16)
Russell King442c8172011-05-08 14:06:52 +010056 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}