1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Mediatek SoCs General-Purpose Timer handling.
4 *
5 * Copyright (C) 2014 Matthias Brugger
6 *
7 * Matthias Brugger <matthias.bgg@gmail.com>
8 */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <linux/clockchips.h>
13 #include <linux/clocksource.h>
14 #include <linux/interrupt.h>
15 #include <linux/irqreturn.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/sched_clock.h>
20 #include <linux/slab.h>
21 #include "timer-of.h"
22
23 #define TIMER_CLK_EVT (1)
24 #define TIMER_CLK_SRC (2)
25
26 #define TIMER_SYNC_TICKS (3)
27
28 /* gpt */
29 #define GPT_IRQ_EN_REG 0x00
30 #define GPT_IRQ_ENABLE(val) BIT((val) - 1)
31 #define GPT_IRQ_ACK_REG 0x08
32 #define GPT_IRQ_ACK(val) BIT((val) - 1)
33
34 #define GPT_CTRL_REG(val) (0x10 * (val))
35 #define GPT_CTRL_OP(val) (((val) & 0x3) << 4)
36 #define GPT_CTRL_OP_ONESHOT (0)
37 #define GPT_CTRL_OP_REPEAT (1)
38 #define GPT_CTRL_OP_FREERUN (3)
39 #define GPT_CTRL_CLEAR (2)
40 #define GPT_CTRL_ENABLE (1)
41 #define GPT_CTRL_DISABLE (0)
42
43 #define GPT_CLK_REG(val) (0x04 + (0x10 * (val)))
44 #define GPT_CLK_SRC(val) (((val) & 0x1) << 4)
45 #define GPT_CLK_SRC_SYS13M (0)
46 #define GPT_CLK_SRC_RTC32K (1)
47 #define GPT_CLK_DIV1 (0x0)
48 #define GPT_CLK_DIV2 (0x1)
49
50 #define GPT_CNT_REG(val) (0x08 + (0x10 * (val)))
51 #define GPT_CMP_REG(val) (0x0C + (0x10 * (val)))
52
53 /* system timer */
54 #define SYST_BASE (0x40)
55
56 #define SYST_CON (SYST_BASE + 0x0)
57 #define SYST_VAL (SYST_BASE + 0x4)
58
59 #define SYST_CON_REG(to) (timer_of_base(to) + SYST_CON)
60 #define SYST_VAL_REG(to) (timer_of_base(to) + SYST_VAL)
61
62 /*
63 * SYST_CON_EN: Clock enable. Shall be set to
64 * - Start timer countdown.
65 * - Allow timeout ticks being updated.
66 * - Allow changing interrupt status,like clear irq pending.
67 *
68 * SYST_CON_IRQ_EN: Set to enable interrupt.
69 *
70 * SYST_CON_IRQ_CLR: Set to clear interrupt.
71 */
72 #define SYST_CON_EN BIT(0)
73 #define SYST_CON_IRQ_EN BIT(1)
74 #define SYST_CON_IRQ_CLR BIT(4)
75
76 static void __iomem *gpt_sched_reg __read_mostly;
77
mtk_syst_ack_irq(struct timer_of * to)78 static void mtk_syst_ack_irq(struct timer_of *to)
79 {
80 /* Clear and disable interrupt */
81 writel(SYST_CON_EN, SYST_CON_REG(to));
82 writel(SYST_CON_IRQ_CLR | SYST_CON_EN, SYST_CON_REG(to));
83 }
84
mtk_syst_handler(int irq,void * dev_id)85 static irqreturn_t mtk_syst_handler(int irq, void *dev_id)
86 {
87 struct clock_event_device *clkevt = dev_id;
88 struct timer_of *to = to_timer_of(clkevt);
89
90 mtk_syst_ack_irq(to);
91 clkevt->event_handler(clkevt);
92
93 return IRQ_HANDLED;
94 }
95
mtk_syst_clkevt_next_event(unsigned long ticks,struct clock_event_device * clkevt)96 static int mtk_syst_clkevt_next_event(unsigned long ticks,
97 struct clock_event_device *clkevt)
98 {
99 struct timer_of *to = to_timer_of(clkevt);
100
101 /* Enable clock to allow timeout tick update later */
102 writel(SYST_CON_EN, SYST_CON_REG(to));
103
104 /*
105 * Write new timeout ticks. Timer shall start countdown
106 * after timeout ticks are updated.
107 */
108 writel(ticks, SYST_VAL_REG(to));
109
110 /* Enable interrupt */
111 writel(SYST_CON_EN | SYST_CON_IRQ_EN, SYST_CON_REG(to));
112
113 return 0;
114 }
115
mtk_syst_clkevt_shutdown(struct clock_event_device * clkevt)116 static int mtk_syst_clkevt_shutdown(struct clock_event_device *clkevt)
117 {
118 /* Clear any irq */
119 mtk_syst_ack_irq(to_timer_of(clkevt));
120
121 /* Disable timer */
122 writel(0, SYST_CON_REG(to_timer_of(clkevt)));
123
124 return 0;
125 }
126
mtk_syst_clkevt_resume(struct clock_event_device * clkevt)127 static int mtk_syst_clkevt_resume(struct clock_event_device *clkevt)
128 {
129 return mtk_syst_clkevt_shutdown(clkevt);
130 }
131
mtk_syst_clkevt_oneshot(struct clock_event_device * clkevt)132 static int mtk_syst_clkevt_oneshot(struct clock_event_device *clkevt)
133 {
134 return 0;
135 }
136
mtk_gpt_read_sched_clock(void)137 static u64 notrace mtk_gpt_read_sched_clock(void)
138 {
139 return readl_relaxed(gpt_sched_reg);
140 }
141
mtk_gpt_clkevt_time_stop(struct timer_of * to,u8 timer)142 static void mtk_gpt_clkevt_time_stop(struct timer_of *to, u8 timer)
143 {
144 u32 val;
145
146 val = readl(timer_of_base(to) + GPT_CTRL_REG(timer));
147 writel(val & ~GPT_CTRL_ENABLE, timer_of_base(to) +
148 GPT_CTRL_REG(timer));
149 }
150
mtk_gpt_clkevt_time_setup(struct timer_of * to,unsigned long delay,u8 timer)151 static void mtk_gpt_clkevt_time_setup(struct timer_of *to,
152 unsigned long delay, u8 timer)
153 {
154 writel(delay, timer_of_base(to) + GPT_CMP_REG(timer));
155 }
156
mtk_gpt_clkevt_time_start(struct timer_of * to,bool periodic,u8 timer)157 static void mtk_gpt_clkevt_time_start(struct timer_of *to,
158 bool periodic, u8 timer)
159 {
160 u32 val;
161
162 /* Acknowledge interrupt */
163 writel(GPT_IRQ_ACK(timer), timer_of_base(to) + GPT_IRQ_ACK_REG);
164
165 val = readl(timer_of_base(to) + GPT_CTRL_REG(timer));
166
167 /* Clear 2 bit timer operation mode field */
168 val &= ~GPT_CTRL_OP(0x3);
169
170 if (periodic)
171 val |= GPT_CTRL_OP(GPT_CTRL_OP_REPEAT);
172 else
173 val |= GPT_CTRL_OP(GPT_CTRL_OP_ONESHOT);
174
175 writel(val | GPT_CTRL_ENABLE | GPT_CTRL_CLEAR,
176 timer_of_base(to) + GPT_CTRL_REG(timer));
177 }
178
mtk_gpt_clkevt_shutdown(struct clock_event_device * clk)179 static int mtk_gpt_clkevt_shutdown(struct clock_event_device *clk)
180 {
181 mtk_gpt_clkevt_time_stop(to_timer_of(clk), TIMER_CLK_EVT);
182
183 return 0;
184 }
185
mtk_gpt_clkevt_set_periodic(struct clock_event_device * clk)186 static int mtk_gpt_clkevt_set_periodic(struct clock_event_device *clk)
187 {
188 struct timer_of *to = to_timer_of(clk);
189
190 mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT);
191 mtk_gpt_clkevt_time_setup(to, to->of_clk.period, TIMER_CLK_EVT);
192 mtk_gpt_clkevt_time_start(to, true, TIMER_CLK_EVT);
193
194 return 0;
195 }
196
mtk_gpt_clkevt_next_event(unsigned long event,struct clock_event_device * clk)197 static int mtk_gpt_clkevt_next_event(unsigned long event,
198 struct clock_event_device *clk)
199 {
200 struct timer_of *to = to_timer_of(clk);
201
202 mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT);
203 mtk_gpt_clkevt_time_setup(to, event, TIMER_CLK_EVT);
204 mtk_gpt_clkevt_time_start(to, false, TIMER_CLK_EVT);
205
206 return 0;
207 }
208
mtk_gpt_interrupt(int irq,void * dev_id)209 static irqreturn_t mtk_gpt_interrupt(int irq, void *dev_id)
210 {
211 struct clock_event_device *clkevt = (struct clock_event_device *)dev_id;
212 struct timer_of *to = to_timer_of(clkevt);
213
214 /* Acknowledge timer0 irq */
215 writel(GPT_IRQ_ACK(TIMER_CLK_EVT), timer_of_base(to) + GPT_IRQ_ACK_REG);
216 clkevt->event_handler(clkevt);
217
218 return IRQ_HANDLED;
219 }
220
221 static void
mtk_gpt_setup(struct timer_of * to,u8 timer,u8 option)222 __init mtk_gpt_setup(struct timer_of *to, u8 timer, u8 option)
223 {
224 writel(GPT_CTRL_CLEAR | GPT_CTRL_DISABLE,
225 timer_of_base(to) + GPT_CTRL_REG(timer));
226
227 writel(GPT_CLK_SRC(GPT_CLK_SRC_SYS13M) | GPT_CLK_DIV1,
228 timer_of_base(to) + GPT_CLK_REG(timer));
229
230 writel(0x0, timer_of_base(to) + GPT_CMP_REG(timer));
231
232 writel(GPT_CTRL_OP(option) | GPT_CTRL_ENABLE,
233 timer_of_base(to) + GPT_CTRL_REG(timer));
234 }
235
mtk_gpt_enable_irq(struct timer_of * to,u8 timer)236 static void mtk_gpt_enable_irq(struct timer_of *to, u8 timer)
237 {
238 u32 val;
239
240 /* Disable all interrupts */
241 writel(0x0, timer_of_base(to) + GPT_IRQ_EN_REG);
242
243 /* Acknowledge all spurious pending interrupts */
244 writel(0x3f, timer_of_base(to) + GPT_IRQ_ACK_REG);
245
246 val = readl(timer_of_base(to) + GPT_IRQ_EN_REG);
247 writel(val | GPT_IRQ_ENABLE(timer),
248 timer_of_base(to) + GPT_IRQ_EN_REG);
249 }
250
mtk_gpt_resume(struct clock_event_device * clk)251 static void mtk_gpt_resume(struct clock_event_device *clk)
252 {
253 struct timer_of *to = to_timer_of(clk);
254
255 mtk_gpt_enable_irq(to, TIMER_CLK_EVT);
256 }
257
mtk_gpt_suspend(struct clock_event_device * clk)258 static void mtk_gpt_suspend(struct clock_event_device *clk)
259 {
260 struct timer_of *to = to_timer_of(clk);
261
262 /* Disable all interrupts */
263 writel(0x0, timer_of_base(to) + GPT_IRQ_EN_REG);
264
265 /*
266 * This is called with interrupts disabled,
267 * so we need to ack any interrupt that is pending
268 * or for example ATF will prevent a suspend from completing.
269 */
270 writel(0x3f, timer_of_base(to) + GPT_IRQ_ACK_REG);
271 }
272
273 static struct timer_of to = {
274 .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
275
276 .clkevt = {
277 .name = "mtk-clkevt",
278 .rating = 300,
279 .cpumask = cpu_possible_mask,
280 },
281
282 .of_irq = {
283 .flags = IRQF_TIMER | IRQF_IRQPOLL,
284 },
285 };
286
mtk_syst_init(struct device_node * node)287 static int __init mtk_syst_init(struct device_node *node)
288 {
289 int ret;
290
291 to.clkevt.features = CLOCK_EVT_FEAT_DYNIRQ | CLOCK_EVT_FEAT_ONESHOT;
292 to.clkevt.set_state_shutdown = mtk_syst_clkevt_shutdown;
293 to.clkevt.set_state_oneshot = mtk_syst_clkevt_oneshot;
294 to.clkevt.tick_resume = mtk_syst_clkevt_resume;
295 to.clkevt.set_next_event = mtk_syst_clkevt_next_event;
296 to.of_irq.handler = mtk_syst_handler;
297
298 ret = timer_of_init(node, &to);
299 if (ret)
300 return ret;
301
302 clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
303 TIMER_SYNC_TICKS, 0xffffffff);
304
305 return 0;
306 }
307
mtk_gpt_init(struct device_node * node)308 static int __init mtk_gpt_init(struct device_node *node)
309 {
310 int ret;
311
312 to.clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
313 to.clkevt.set_state_shutdown = mtk_gpt_clkevt_shutdown;
314 to.clkevt.set_state_periodic = mtk_gpt_clkevt_set_periodic;
315 to.clkevt.set_state_oneshot = mtk_gpt_clkevt_shutdown;
316 to.clkevt.tick_resume = mtk_gpt_clkevt_shutdown;
317 to.clkevt.set_next_event = mtk_gpt_clkevt_next_event;
318 to.clkevt.suspend = mtk_gpt_suspend;
319 to.clkevt.resume = mtk_gpt_resume;
320 to.of_irq.handler = mtk_gpt_interrupt;
321
322 ret = timer_of_init(node, &to);
323 if (ret)
324 return ret;
325
326 /* Configure clock source */
327 mtk_gpt_setup(&to, TIMER_CLK_SRC, GPT_CTRL_OP_FREERUN);
328 clocksource_mmio_init(timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC),
329 node->name, timer_of_rate(&to), 300, 32,
330 clocksource_mmio_readl_up);
331 gpt_sched_reg = timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC);
332 sched_clock_register(mtk_gpt_read_sched_clock, 32, timer_of_rate(&to));
333
334 /* Configure clock event */
335 mtk_gpt_setup(&to, TIMER_CLK_EVT, GPT_CTRL_OP_REPEAT);
336 clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
337 TIMER_SYNC_TICKS, 0xffffffff);
338
339 mtk_gpt_enable_irq(&to, TIMER_CLK_EVT);
340
341 return 0;
342 }
343
344 #ifndef MODULE
345 TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init);
346 TIMER_OF_DECLARE(mtk_mt6765, "mediatek,mt6765-timer", mtk_syst_init);
347 #else
mtk_timer_probe(struct platform_device * pdev)348 static int mtk_timer_probe(struct platform_device *pdev)
349 {
350 int (*timer_init)(struct device_node *node);
351 struct device_node *np = pdev->dev.of_node;
352
353 timer_init = of_device_get_match_data(&pdev->dev);
354 return timer_init(np);
355 }
356
357 static const struct of_device_id mtk_timer_match_table[] = {
358 { .compatible = "mediatek,mt6577-timer", .data = mtk_gpt_init },
359 { .compatible = "mediatek,mt6765-timer", .data = mtk_syst_init },
360 { /* sentinel */ }
361 };
362
363 static struct platform_driver mtk_timer_driver = {
364 .probe = mtk_timer_probe,
365 .driver = {
366 .name = "mediatek-timer",
367 .of_match_table = mtk_timer_match_table,
368 },
369 };
370 module_platform_driver(mtk_timer_driver);
371
372 MODULE_DESCRIPTION("MediaTek Timer driver");
373 MODULE_LICENSE("GPL v2");
374 #endif
375