blob: f6ddae30933f7cc71455eb9285392c23fd45703e [file] [log] [blame]
Fabio Estevame45e7782018-05-22 20:05:06 -03001// SPDX-License-Identifier: GPL-2.0+
2//
3// Copyright (C) 2000-2001 Deep Blue Solutions
4// Copyright (C) 2002 Shane Nay (shane@minirl.com)
5// Copyright (C) 2006-2007 Pavel Pisa (ppisa@pikron.com)
6// Copyright (C) 2008 Juergen Beisert (kernel@pengutronix.de)
7// Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved.
Shawn Guo4e472092010-12-18 21:39:30 +08008
Shawn Guo39d13672012-04-29 00:02:37 +08009#include <linux/err.h>
Shawn Guo4e472092010-12-18 21:39:30 +080010#include <linux/interrupt.h>
11#include <linux/irq.h>
12#include <linux/clockchips.h>
13#include <linux/clk.h>
Shawn Guoeeca6e62012-08-20 08:51:45 +080014#include <linux/of.h>
Shawn Guobe35bd22013-03-25 14:57:41 +080015#include <linux/of_address.h>
Shawn Guoeeca6e62012-08-20 08:51:45 +080016#include <linux/of_irq.h>
Shawn Guo7a9c39f2013-03-25 20:04:34 +080017#include <linux/stmp_device.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070018#include <linux/sched_clock.h>
Shawn Guo4e472092010-12-18 21:39:30 +080019
Shawn Guo4e472092010-12-18 21:39:30 +080020/*
21 * There are 2 versions of the timrot on Freescale MXS-based SoCs.
22 * The v1 on MX23 only gets 16 bits counter, while v2 on MX28
23 * extends the counter to 32 bits.
24 *
25 * The implementation uses two timers, one for clock_event and
26 * another for clocksource. MX28 uses timrot 0 and 1, while MX23
27 * uses 0 and 2.
28 */
29
30#define MX23_TIMROT_VERSION_OFFSET 0x0a0
31#define MX28_TIMROT_VERSION_OFFSET 0x120
32#define BP_TIMROT_MAJOR_VERSION 24
33#define BV_TIMROT_VERSION_1 0x01
34#define BV_TIMROT_VERSION_2 0x02
35#define timrot_is_v1() (timrot_major_version == BV_TIMROT_VERSION_1)
36
37/*
38 * There are 4 registers for each timrotv2 instance, and 2 registers
39 * for each timrotv1. So address step 0x40 in macros below strides
40 * one instance of timrotv2 while two instances of timrotv1.
41 *
42 * As the result, HW_TIMROT_XXXn(1) defines the address of timrot1
43 * on MX28 while timrot2 on MX23.
44 */
45/* common between v1 and v2 */
46#define HW_TIMROT_ROTCTRL 0x00
47#define HW_TIMROT_TIMCTRLn(n) (0x20 + (n) * 0x40)
48/* v1 only */
49#define HW_TIMROT_TIMCOUNTn(n) (0x30 + (n) * 0x40)
50/* v2 only */
51#define HW_TIMROT_RUNNING_COUNTn(n) (0x30 + (n) * 0x40)
52#define HW_TIMROT_FIXED_COUNTn(n) (0x40 + (n) * 0x40)
53
54#define BM_TIMROT_TIMCTRLn_RELOAD (1 << 6)
55#define BM_TIMROT_TIMCTRLn_UPDATE (1 << 7)
56#define BM_TIMROT_TIMCTRLn_IRQ_EN (1 << 14)
57#define BM_TIMROT_TIMCTRLn_IRQ (1 << 15)
58#define BP_TIMROT_TIMCTRLn_SELECT 0
Torben Hohn2fb318f2012-12-21 15:06:15 +010059#define BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL 0x8
60#define BV_TIMROTv2_TIMCTRLn_SELECT__32KHZ_XTAL 0xb
61#define BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS 0xf
Shawn Guo4e472092010-12-18 21:39:30 +080062
63static struct clock_event_device mxs_clockevent_device;
Shawn Guo4e472092010-12-18 21:39:30 +080064
Shawn Guobe35bd22013-03-25 14:57:41 +080065static void __iomem *mxs_timrot_base;
Shawn Guo4e472092010-12-18 21:39:30 +080066static u32 timrot_major_version;
67
68static inline void timrot_irq_disable(void)
69{
Shawn Guo7a9c39f2013-03-25 20:04:34 +080070 __raw_writel(BM_TIMROT_TIMCTRLn_IRQ_EN, mxs_timrot_base +
71 HW_TIMROT_TIMCTRLn(0) + STMP_OFFSET_REG_CLR);
Shawn Guo4e472092010-12-18 21:39:30 +080072}
73
74static inline void timrot_irq_enable(void)
75{
Shawn Guo7a9c39f2013-03-25 20:04:34 +080076 __raw_writel(BM_TIMROT_TIMCTRLn_IRQ_EN, mxs_timrot_base +
77 HW_TIMROT_TIMCTRLn(0) + STMP_OFFSET_REG_SET);
Shawn Guo4e472092010-12-18 21:39:30 +080078}
79
80static void timrot_irq_acknowledge(void)
81{
Shawn Guo7a9c39f2013-03-25 20:04:34 +080082 __raw_writel(BM_TIMROT_TIMCTRLn_IRQ, mxs_timrot_base +
83 HW_TIMROT_TIMCTRLn(0) + STMP_OFFSET_REG_CLR);
Shawn Guo4e472092010-12-18 21:39:30 +080084}
85
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010086static u64 timrotv1_get_cycles(struct clocksource *cs)
Shawn Guo4e472092010-12-18 21:39:30 +080087{
88 return ~((__raw_readl(mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1))
89 & 0xffff0000) >> 16);
90}
91
Shawn Guo4e472092010-12-18 21:39:30 +080092static int timrotv1_set_next_event(unsigned long evt,
93 struct clock_event_device *dev)
94{
95 /* timrot decrements the count */
96 __raw_writel(evt, mxs_timrot_base + HW_TIMROT_TIMCOUNTn(0));
97
98 return 0;
99}
100
101static int timrotv2_set_next_event(unsigned long evt,
102 struct clock_event_device *dev)
103{
104 /* timrot decrements the count */
105 __raw_writel(evt, mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(0));
106
107 return 0;
108}
109
110static irqreturn_t mxs_timer_interrupt(int irq, void *dev_id)
111{
112 struct clock_event_device *evt = dev_id;
113
114 timrot_irq_acknowledge();
115 evt->event_handler(evt);
116
117 return IRQ_HANDLED;
118}
119
120static struct irqaction mxs_timer_irq = {
121 .name = "MXS Timer Tick",
122 .dev_id = &mxs_clockevent_device,
123 .flags = IRQF_TIMER | IRQF_IRQPOLL,
124 .handler = mxs_timer_interrupt,
125};
126
Viresh Kumareb8703e2015-06-18 16:24:28 +0530127static void mxs_irq_clear(char *state)
Shawn Guo4e472092010-12-18 21:39:30 +0800128{
129 /* Disable interrupt in timer module */
130 timrot_irq_disable();
131
Viresh Kumareb8703e2015-06-18 16:24:28 +0530132 /* Set event time into the furthest future */
133 if (timrot_is_v1())
134 __raw_writel(0xffff, mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
135 else
136 __raw_writel(0xffffffff,
137 mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
Shawn Guo4e472092010-12-18 21:39:30 +0800138
Viresh Kumareb8703e2015-06-18 16:24:28 +0530139 /* Clear pending interrupt */
140 timrot_irq_acknowledge();
Shawn Guo4e472092010-12-18 21:39:30 +0800141
142#ifdef DEBUG
Viresh Kumareb8703e2015-06-18 16:24:28 +0530143 pr_info("%s: changing mode to %s\n", __func__, state)
Shawn Guo4e472092010-12-18 21:39:30 +0800144#endif /* DEBUG */
Viresh Kumareb8703e2015-06-18 16:24:28 +0530145}
Shawn Guo4e472092010-12-18 21:39:30 +0800146
Viresh Kumareb8703e2015-06-18 16:24:28 +0530147static int mxs_shutdown(struct clock_event_device *evt)
148{
149 mxs_irq_clear("shutdown");
Shawn Guo4e472092010-12-18 21:39:30 +0800150
Viresh Kumareb8703e2015-06-18 16:24:28 +0530151 return 0;
152}
153
154static int mxs_set_oneshot(struct clock_event_device *evt)
155{
156 if (clockevent_state_oneshot(evt))
157 mxs_irq_clear("oneshot");
158 timrot_irq_enable();
159 return 0;
Shawn Guo4e472092010-12-18 21:39:30 +0800160}
161
162static struct clock_event_device mxs_clockevent_device = {
Viresh Kumareb8703e2015-06-18 16:24:28 +0530163 .name = "mxs_timrot",
164 .features = CLOCK_EVT_FEAT_ONESHOT,
165 .set_state_shutdown = mxs_shutdown,
166 .set_state_oneshot = mxs_set_oneshot,
167 .tick_resume = mxs_shutdown,
168 .set_next_event = timrotv2_set_next_event,
169 .rating = 200,
Shawn Guo4e472092010-12-18 21:39:30 +0800170};
171
172static int __init mxs_clockevent_init(struct clk *timer_clk)
173{
Shawn Guo838a2ae2013-01-12 11:50:05 +0000174 if (timrot_is_v1())
Shawn Guo4e472092010-12-18 21:39:30 +0800175 mxs_clockevent_device.set_next_event = timrotv1_set_next_event;
Shawn Guo838a2ae2013-01-12 11:50:05 +0000176 mxs_clockevent_device.cpumask = cpumask_of(0);
177 clockevents_config_and_register(&mxs_clockevent_device,
Torben Hohn93044fa2012-12-21 15:06:16 +0100178 clk_get_rate(timer_clk),
179 timrot_is_v1() ? 0xf : 0x2,
Shawn Guo838a2ae2013-01-12 11:50:05 +0000180 timrot_is_v1() ? 0xfffe : 0xfffffffe);
Shawn Guo4e472092010-12-18 21:39:30 +0800181
182 return 0;
183}
184
185static struct clocksource clocksource_mxs = {
186 .name = "mxs_timer",
187 .rating = 200,
Russell King5c61ddc2011-05-08 17:21:49 +0100188 .read = timrotv1_get_cycles,
189 .mask = CLOCKSOURCE_MASK(16),
Shawn Guo4e472092010-12-18 21:39:30 +0800190 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
191};
192
Stephen Boydfcfca6e2013-07-18 16:21:23 -0700193static u64 notrace mxs_read_sched_clock_v2(void)
Stanislav Meduna67948ad2012-11-08 23:39:14 +0100194{
195 return ~readl_relaxed(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1));
196}
197
Shawn Guo4e472092010-12-18 21:39:30 +0800198static int __init mxs_clocksource_init(struct clk *timer_clk)
199{
200 unsigned int c = clk_get_rate(timer_clk);
201
Russell King5c61ddc2011-05-08 17:21:49 +0100202 if (timrot_is_v1())
203 clocksource_register_hz(&clocksource_mxs, c);
Stanislav Meduna67948ad2012-11-08 23:39:14 +0100204 else {
Russell King5c61ddc2011-05-08 17:21:49 +0100205 clocksource_mmio_init(mxs_timrot_base + HW_TIMROT_RUNNING_COUNTn(1),
206 "mxs_timer", c, 200, 32, clocksource_mmio_readl_down);
Stephen Boydfcfca6e2013-07-18 16:21:23 -0700207 sched_clock_register(mxs_read_sched_clock_v2, 32, c);
Stanislav Meduna67948ad2012-11-08 23:39:14 +0100208 }
Shawn Guo4e472092010-12-18 21:39:30 +0800209
210 return 0;
211}
212
Daniel Lezcanoe1d2b9f2016-06-06 17:58:03 +0200213static int __init mxs_timer_init(struct device_node *np)
Shawn Guo4e472092010-12-18 21:39:30 +0800214{
Shawn Guo50260922012-04-29 00:02:41 +0800215 struct clk *timer_clk;
Daniel Lezcanoe1d2b9f2016-06-06 17:58:03 +0200216 int irq, ret;
Shawn Guoeeca6e62012-08-20 08:51:45 +0800217
Shawn Guobe35bd22013-03-25 14:57:41 +0800218 mxs_timrot_base = of_iomap(np, 0);
219 WARN_ON(!mxs_timrot_base);
220
Shawn Guo2efb9502013-03-25 22:57:14 +0800221 timer_clk = of_clk_get(np, 0);
Shawn Guo50260922012-04-29 00:02:41 +0800222 if (IS_ERR(timer_clk)) {
223 pr_err("%s: failed to get clk\n", __func__);
Daniel Lezcanoe1d2b9f2016-06-06 17:58:03 +0200224 return PTR_ERR(timer_clk);
Shawn Guo39d13672012-04-29 00:02:37 +0800225 }
226
Daniel Lezcanoe1d2b9f2016-06-06 17:58:03 +0200227 ret = clk_prepare_enable(timer_clk);
228 if (ret)
229 return ret;
Shawn Guo4e472092010-12-18 21:39:30 +0800230
231 /*
232 * Initialize timers to a known state
233 */
Shawn Guo7a9c39f2013-03-25 20:04:34 +0800234 stmp_reset_block(mxs_timrot_base + HW_TIMROT_ROTCTRL);
Shawn Guo4e472092010-12-18 21:39:30 +0800235
236 /* get timrot version */
237 timrot_major_version = __raw_readl(mxs_timrot_base +
Shawn Guo220d2f22013-03-25 19:41:39 +0800238 (of_device_is_compatible(np, "fsl,imx23-timrot") ?
239 MX23_TIMROT_VERSION_OFFSET :
Shawn Guo4e472092010-12-18 21:39:30 +0800240 MX28_TIMROT_VERSION_OFFSET));
241 timrot_major_version >>= BP_TIMROT_MAJOR_VERSION;
242
243 /* one for clock_event */
244 __raw_writel((timrot_is_v1() ?
245 BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
Torben Hohn2fb318f2012-12-21 15:06:15 +0100246 BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) |
Shawn Guo4e472092010-12-18 21:39:30 +0800247 BM_TIMROT_TIMCTRLn_UPDATE |
248 BM_TIMROT_TIMCTRLn_IRQ_EN,
249 mxs_timrot_base + HW_TIMROT_TIMCTRLn(0));
250
251 /* another for clocksource */
252 __raw_writel((timrot_is_v1() ?
253 BV_TIMROTv1_TIMCTRLn_SELECT__32KHZ_XTAL :
Torben Hohn2fb318f2012-12-21 15:06:15 +0100254 BV_TIMROTv2_TIMCTRLn_SELECT__TICK_ALWAYS) |
Shawn Guo4e472092010-12-18 21:39:30 +0800255 BM_TIMROT_TIMCTRLn_RELOAD,
256 mxs_timrot_base + HW_TIMROT_TIMCTRLn(1));
257
258 /* set clocksource timer fixed count to the maximum */
259 if (timrot_is_v1())
260 __raw_writel(0xffff,
261 mxs_timrot_base + HW_TIMROT_TIMCOUNTn(1));
262 else
263 __raw_writel(0xffffffff,
264 mxs_timrot_base + HW_TIMROT_FIXED_COUNTn(1));
265
266 /* init and register the timer to the framework */
Daniel Lezcanoe1d2b9f2016-06-06 17:58:03 +0200267 ret = mxs_clocksource_init(timer_clk);
268 if (ret)
269 return ret;
270
271 ret = mxs_clockevent_init(timer_clk);
272 if (ret)
273 return ret;
Shawn Guo4e472092010-12-18 21:39:30 +0800274
275 /* Make irqs happen */
Shawn Guoeeca6e62012-08-20 08:51:45 +0800276 irq = irq_of_parse_and_map(np, 0);
Daniel Lezcanoe1d2b9f2016-06-06 17:58:03 +0200277 if (irq <= 0)
278 return -EINVAL;
279
280 return setup_irq(irq, &mxs_timer_irq);
Shawn Guo4e472092010-12-18 21:39:30 +0800281}
Daniel Lezcano17273392017-05-26 16:56:11 +0200282TIMER_OF_DECLARE(mxs, "fsl,timrot", mxs_timer_init);