blob: 9318edcd89635ea195e1389ef8acc1853358c216 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Matthias Bruggerecb35302014-07-18 11:36:43 +02002/*
3 * Mediatek SoCs General-Purpose Timer handling.
4 *
5 * Copyright (C) 2014 Matthias Brugger
6 *
7 * Matthias Brugger <matthias.bgg@gmail.com>
Matthias Bruggerecb35302014-07-18 11:36:43 +02008 */
9
Alexey Klimov9a78ec42015-10-25 23:21:22 +000010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
Matthias Bruggerecb35302014-07-18 11:36:43 +020012#include <linux/clockchips.h>
Stanley Chua0858f92018-07-06 07:11:27 +080013#include <linux/clocksource.h>
Matthias Bruggerecb35302014-07-18 11:36:43 +020014#include <linux/interrupt.h>
Matthias Bruggerecb35302014-07-18 11:36:43 +020015#include <linux/irqreturn.h>
Yingjoe Chenf14665f2015-07-13 17:32:46 +080016#include <linux/sched_clock.h>
Matthias Bruggerecb35302014-07-18 11:36:43 +020017#include <linux/slab.h>
Stanley Chua0858f92018-07-06 07:11:27 +080018#include "timer-of.h"
Matthias Bruggerecb35302014-07-18 11:36:43 +020019
Stanley Chu56d52d32018-07-06 07:11:26 +080020#define TIMER_CLK_EVT (1)
21#define TIMER_CLK_SRC (2)
Matthias Bruggerecb35302014-07-18 11:36:43 +020022
Stanley Chu56d52d32018-07-06 07:11:26 +080023#define TIMER_SYNC_TICKS (3)
Matthias Bruggerecb35302014-07-18 11:36:43 +020024
Stanley Chu56d52d32018-07-06 07:11:26 +080025/* gpt */
26#define GPT_IRQ_EN_REG 0x00
27#define GPT_IRQ_ENABLE(val) BIT((val) - 1)
28#define GPT_IRQ_ACK_REG 0x08
29#define GPT_IRQ_ACK(val) BIT((val) - 1)
Matthias Bruggerecb35302014-07-18 11:36:43 +020030
Stanley Chu56d52d32018-07-06 07:11:26 +080031#define GPT_CTRL_REG(val) (0x10 * (val))
32#define GPT_CTRL_OP(val) (((val) & 0x3) << 4)
33#define GPT_CTRL_OP_ONESHOT (0)
34#define GPT_CTRL_OP_REPEAT (1)
35#define GPT_CTRL_OP_FREERUN (3)
36#define GPT_CTRL_CLEAR (2)
37#define GPT_CTRL_ENABLE (1)
38#define GPT_CTRL_DISABLE (0)
Matthias Bruggerecb35302014-07-18 11:36:43 +020039
Stanley Chu56d52d32018-07-06 07:11:26 +080040#define GPT_CLK_REG(val) (0x04 + (0x10 * (val)))
41#define GPT_CLK_SRC(val) (((val) & 0x1) << 4)
42#define GPT_CLK_SRC_SYS13M (0)
43#define GPT_CLK_SRC_RTC32K (1)
44#define GPT_CLK_DIV1 (0x0)
45#define GPT_CLK_DIV2 (0x1)
46
47#define GPT_CNT_REG(val) (0x08 + (0x10 * (val)))
48#define GPT_CMP_REG(val) (0x0C + (0x10 * (val)))
Matthias Bruggerecb35302014-07-18 11:36:43 +020049
Stanley Chue3af6772018-07-06 07:11:28 +080050/* system timer */
51#define SYST_BASE (0x40)
52
53#define SYST_CON (SYST_BASE + 0x0)
54#define SYST_VAL (SYST_BASE + 0x4)
55
56#define SYST_CON_REG(to) (timer_of_base(to) + SYST_CON)
57#define SYST_VAL_REG(to) (timer_of_base(to) + SYST_VAL)
58
59/*
60 * SYST_CON_EN: Clock enable. Shall be set to
61 * - Start timer countdown.
62 * - Allow timeout ticks being updated.
63 * - Allow changing interrupt functions.
64 *
65 * SYST_CON_IRQ_EN: Set to allow interrupt.
66 *
67 * SYST_CON_IRQ_CLR: Set to clear interrupt.
68 */
69#define SYST_CON_EN BIT(0)
70#define SYST_CON_IRQ_EN BIT(1)
71#define SYST_CON_IRQ_CLR BIT(4)
72
Yingjoe Chenf14665f2015-07-13 17:32:46 +080073static void __iomem *gpt_sched_reg __read_mostly;
74
Stanley Chue3af6772018-07-06 07:11:28 +080075static void mtk_syst_ack_irq(struct timer_of *to)
76{
77 /* Clear and disable interrupt */
78 writel(SYST_CON_IRQ_CLR | SYST_CON_EN, SYST_CON_REG(to));
79}
80
81static irqreturn_t mtk_syst_handler(int irq, void *dev_id)
82{
83 struct clock_event_device *clkevt = dev_id;
84 struct timer_of *to = to_timer_of(clkevt);
85
86 mtk_syst_ack_irq(to);
87 clkevt->event_handler(clkevt);
88
89 return IRQ_HANDLED;
90}
91
92static int mtk_syst_clkevt_next_event(unsigned long ticks,
93 struct clock_event_device *clkevt)
94{
95 struct timer_of *to = to_timer_of(clkevt);
96
97 /* Enable clock to allow timeout tick update later */
98 writel(SYST_CON_EN, SYST_CON_REG(to));
99
100 /*
101 * Write new timeout ticks. Timer shall start countdown
102 * after timeout ticks are updated.
103 */
104 writel(ticks, SYST_VAL_REG(to));
105
106 /* Enable interrupt */
107 writel(SYST_CON_EN | SYST_CON_IRQ_EN, SYST_CON_REG(to));
108
109 return 0;
110}
111
112static int mtk_syst_clkevt_shutdown(struct clock_event_device *clkevt)
113{
114 /* Disable timer */
115 writel(0, SYST_CON_REG(to_timer_of(clkevt)));
116
117 return 0;
118}
119
120static int mtk_syst_clkevt_resume(struct clock_event_device *clkevt)
121{
122 return mtk_syst_clkevt_shutdown(clkevt);
123}
124
125static int mtk_syst_clkevt_oneshot(struct clock_event_device *clkevt)
126{
127 return 0;
128}
129
Stanley Chu56d52d32018-07-06 07:11:26 +0800130static u64 notrace mtk_gpt_read_sched_clock(void)
Yingjoe Chenf14665f2015-07-13 17:32:46 +0800131{
132 return readl_relaxed(gpt_sched_reg);
133}
134
Stanley Chua0858f92018-07-06 07:11:27 +0800135static void mtk_gpt_clkevt_time_stop(struct timer_of *to, u8 timer)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200136{
137 u32 val;
138
Stanley Chua0858f92018-07-06 07:11:27 +0800139 val = readl(timer_of_base(to) + GPT_CTRL_REG(timer));
140 writel(val & ~GPT_CTRL_ENABLE, timer_of_base(to) +
141 GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200142}
143
Stanley Chua0858f92018-07-06 07:11:27 +0800144static void mtk_gpt_clkevt_time_setup(struct timer_of *to,
145 unsigned long delay, u8 timer)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200146{
Stanley Chua0858f92018-07-06 07:11:27 +0800147 writel(delay, timer_of_base(to) + GPT_CMP_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200148}
149
Stanley Chua0858f92018-07-06 07:11:27 +0800150static void mtk_gpt_clkevt_time_start(struct timer_of *to,
151 bool periodic, u8 timer)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200152{
153 u32 val;
154
155 /* Acknowledge interrupt */
Stanley Chua0858f92018-07-06 07:11:27 +0800156 writel(GPT_IRQ_ACK(timer), timer_of_base(to) + GPT_IRQ_ACK_REG);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200157
Stanley Chua0858f92018-07-06 07:11:27 +0800158 val = readl(timer_of_base(to) + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200159
160 /* Clear 2 bit timer operation mode field */
Stanley Chu56d52d32018-07-06 07:11:26 +0800161 val &= ~GPT_CTRL_OP(0x3);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200162
163 if (periodic)
Stanley Chu56d52d32018-07-06 07:11:26 +0800164 val |= GPT_CTRL_OP(GPT_CTRL_OP_REPEAT);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200165 else
Stanley Chu56d52d32018-07-06 07:11:26 +0800166 val |= GPT_CTRL_OP(GPT_CTRL_OP_ONESHOT);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200167
Stanley Chu56d52d32018-07-06 07:11:26 +0800168 writel(val | GPT_CTRL_ENABLE | GPT_CTRL_CLEAR,
Stanley Chua0858f92018-07-06 07:11:27 +0800169 timer_of_base(to) + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200170}
171
Stanley Chu56d52d32018-07-06 07:11:26 +0800172static int mtk_gpt_clkevt_shutdown(struct clock_event_device *clk)
Viresh Kumara2b7e102015-06-18 16:24:27 +0530173{
Stanley Chua0858f92018-07-06 07:11:27 +0800174 mtk_gpt_clkevt_time_stop(to_timer_of(clk), TIMER_CLK_EVT);
175
Viresh Kumara2b7e102015-06-18 16:24:27 +0530176 return 0;
177}
178
Stanley Chu56d52d32018-07-06 07:11:26 +0800179static int mtk_gpt_clkevt_set_periodic(struct clock_event_device *clk)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200180{
Stanley Chua0858f92018-07-06 07:11:27 +0800181 struct timer_of *to = to_timer_of(clk);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200182
Stanley Chua0858f92018-07-06 07:11:27 +0800183 mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT);
184 mtk_gpt_clkevt_time_setup(to, to->of_clk.period, TIMER_CLK_EVT);
185 mtk_gpt_clkevt_time_start(to, true, TIMER_CLK_EVT);
186
Viresh Kumara2b7e102015-06-18 16:24:27 +0530187 return 0;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200188}
189
Stanley Chu56d52d32018-07-06 07:11:26 +0800190static int mtk_gpt_clkevt_next_event(unsigned long event,
Stanley Chua0858f92018-07-06 07:11:27 +0800191 struct clock_event_device *clk)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200192{
Stanley Chua0858f92018-07-06 07:11:27 +0800193 struct timer_of *to = to_timer_of(clk);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200194
Stanley Chua0858f92018-07-06 07:11:27 +0800195 mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT);
196 mtk_gpt_clkevt_time_setup(to, event, TIMER_CLK_EVT);
197 mtk_gpt_clkevt_time_start(to, false, TIMER_CLK_EVT);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200198
199 return 0;
200}
201
Stanley Chu56d52d32018-07-06 07:11:26 +0800202static irqreturn_t mtk_gpt_interrupt(int irq, void *dev_id)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200203{
Stanley Chua0858f92018-07-06 07:11:27 +0800204 struct clock_event_device *clkevt = (struct clock_event_device *)dev_id;
205 struct timer_of *to = to_timer_of(clkevt);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200206
207 /* Acknowledge timer0 irq */
Stanley Chua0858f92018-07-06 07:11:27 +0800208 writel(GPT_IRQ_ACK(TIMER_CLK_EVT), timer_of_base(to) + GPT_IRQ_ACK_REG);
209 clkevt->event_handler(clkevt);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200210
211 return IRQ_HANDLED;
212}
213
Matthias Bruggerecb35302014-07-18 11:36:43 +0200214static void
Stanley Chua0858f92018-07-06 07:11:27 +0800215__init mtk_gpt_setup(struct timer_of *to, u8 timer, u8 option)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200216{
Stanley Chu56d52d32018-07-06 07:11:26 +0800217 writel(GPT_CTRL_CLEAR | GPT_CTRL_DISABLE,
Stanley Chua0858f92018-07-06 07:11:27 +0800218 timer_of_base(to) + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200219
Stanley Chu56d52d32018-07-06 07:11:26 +0800220 writel(GPT_CLK_SRC(GPT_CLK_SRC_SYS13M) | GPT_CLK_DIV1,
Stanley Chua0858f92018-07-06 07:11:27 +0800221 timer_of_base(to) + GPT_CLK_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200222
Stanley Chua0858f92018-07-06 07:11:27 +0800223 writel(0x0, timer_of_base(to) + GPT_CMP_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200224
Stanley Chu56d52d32018-07-06 07:11:26 +0800225 writel(GPT_CTRL_OP(option) | GPT_CTRL_ENABLE,
Stanley Chua0858f92018-07-06 07:11:27 +0800226 timer_of_base(to) + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200227}
228
Stanley Chua0858f92018-07-06 07:11:27 +0800229static void mtk_gpt_enable_irq(struct timer_of *to, u8 timer)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200230{
231 u32 val;
232
Daniel Lezcanofc686d02015-08-24 15:14:30 +0200233 /* Disable all interrupts */
Stanley Chua0858f92018-07-06 07:11:27 +0800234 writel(0x0, timer_of_base(to) + GPT_IRQ_EN_REG);
Daniel Lezcanofc686d02015-08-24 15:14:30 +0200235
236 /* Acknowledge all spurious pending interrupts */
Stanley Chua0858f92018-07-06 07:11:27 +0800237 writel(0x3f, timer_of_base(to) + GPT_IRQ_ACK_REG);
Daniel Lezcanofc686d02015-08-24 15:14:30 +0200238
Stanley Chua0858f92018-07-06 07:11:27 +0800239 val = readl(timer_of_base(to) + GPT_IRQ_EN_REG);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200240 writel(val | GPT_IRQ_ENABLE(timer),
Stanley Chua0858f92018-07-06 07:11:27 +0800241 timer_of_base(to) + GPT_IRQ_EN_REG);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200242}
243
Stanley Chua0858f92018-07-06 07:11:27 +0800244static struct timer_of to = {
245 .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
246
247 .clkevt = {
248 .name = "mtk-clkevt",
249 .rating = 300,
250 .cpumask = cpu_possible_mask,
251 },
252
253 .of_irq = {
254 .flags = IRQF_TIMER | IRQF_IRQPOLL,
255 },
256};
257
Stanley Chue3af6772018-07-06 07:11:28 +0800258static int __init mtk_syst_init(struct device_node *node)
259{
260 int ret;
261
262 to.clkevt.features = CLOCK_EVT_FEAT_DYNIRQ | CLOCK_EVT_FEAT_ONESHOT;
263 to.clkevt.set_state_shutdown = mtk_syst_clkevt_shutdown;
264 to.clkevt.set_state_oneshot = mtk_syst_clkevt_oneshot;
265 to.clkevt.tick_resume = mtk_syst_clkevt_resume;
266 to.clkevt.set_next_event = mtk_syst_clkevt_next_event;
267 to.of_irq.handler = mtk_syst_handler;
268
269 ret = timer_of_init(node, &to);
270 if (ret)
Fabien Parent41d49e72019-09-19 21:13:15 +0200271 return ret;
Stanley Chue3af6772018-07-06 07:11:28 +0800272
273 clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
274 TIMER_SYNC_TICKS, 0xffffffff);
275
276 return 0;
Stanley Chue3af6772018-07-06 07:11:28 +0800277}
278
Stanley Chu56d52d32018-07-06 07:11:26 +0800279static int __init mtk_gpt_init(struct device_node *node)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200280{
Stanley Chua0858f92018-07-06 07:11:27 +0800281 int ret;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200282
Stanley Chua0858f92018-07-06 07:11:27 +0800283 to.clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
284 to.clkevt.set_state_shutdown = mtk_gpt_clkevt_shutdown;
285 to.clkevt.set_state_periodic = mtk_gpt_clkevt_set_periodic;
286 to.clkevt.set_state_oneshot = mtk_gpt_clkevt_shutdown;
287 to.clkevt.tick_resume = mtk_gpt_clkevt_shutdown;
288 to.clkevt.set_next_event = mtk_gpt_clkevt_next_event;
289 to.of_irq.handler = mtk_gpt_interrupt;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200290
Stanley Chua0858f92018-07-06 07:11:27 +0800291 ret = timer_of_init(node, &to);
292 if (ret)
Fabien Parent41d49e72019-09-19 21:13:15 +0200293 return ret;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200294
Matthias Bruggerecb35302014-07-18 11:36:43 +0200295 /* Configure clock source */
Stanley Chua0858f92018-07-06 07:11:27 +0800296 mtk_gpt_setup(&to, TIMER_CLK_SRC, GPT_CTRL_OP_FREERUN);
297 clocksource_mmio_init(timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC),
298 node->name, timer_of_rate(&to), 300, 32,
299 clocksource_mmio_readl_up);
300 gpt_sched_reg = timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC);
301 sched_clock_register(mtk_gpt_read_sched_clock, 32, timer_of_rate(&to));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200302
303 /* Configure clock event */
Stanley Chua0858f92018-07-06 07:11:27 +0800304 mtk_gpt_setup(&to, TIMER_CLK_EVT, GPT_CTRL_OP_REPEAT);
305 clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
306 TIMER_SYNC_TICKS, 0xffffffff);
Matthias Bruggerd4a19eb32015-02-19 11:41:33 +0100307
Stanley Chua0858f92018-07-06 07:11:27 +0800308 mtk_gpt_enable_irq(&to, TIMER_CLK_EVT);
Matthias Bruggerd4a19eb32015-02-19 11:41:33 +0100309
Daniel Lezcanod64e24c2016-05-31 17:43:47 +0200310 return 0;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200311}
Stanley Chu56d52d32018-07-06 07:11:26 +0800312TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init);
Stanley Chue3af6772018-07-06 07:11:28 +0800313TIMER_OF_DECLARE(mtk_mt6765, "mediatek,mt6765-timer", mtk_syst_init);