blob: 8e7894a026acebb61ba401dc8410951484dcc0de [file] [log] [blame]
Matthias Bruggerecb35302014-07-18 11:36:43 +02001/*
2 * Mediatek SoCs General-Purpose Timer handling.
3 *
4 * Copyright (C) 2014 Matthias Brugger
5 *
6 * Matthias Brugger <matthias.bgg@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
Alexey Klimov9a78ec42015-10-25 23:21:22 +000019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Matthias Bruggerecb35302014-07-18 11:36:43 +020021#include <linux/clockchips.h>
Stanley Chua0858f92018-07-06 07:11:27 +080022#include <linux/clocksource.h>
Matthias Bruggerecb35302014-07-18 11:36:43 +020023#include <linux/interrupt.h>
Matthias Bruggerecb35302014-07-18 11:36:43 +020024#include <linux/irqreturn.h>
Yingjoe Chenf14665f2015-07-13 17:32:46 +080025#include <linux/sched_clock.h>
Matthias Bruggerecb35302014-07-18 11:36:43 +020026#include <linux/slab.h>
Stanley Chua0858f92018-07-06 07:11:27 +080027#include "timer-of.h"
Matthias Bruggerecb35302014-07-18 11:36:43 +020028
Stanley Chu56d52d32018-07-06 07:11:26 +080029#define TIMER_CLK_EVT (1)
30#define TIMER_CLK_SRC (2)
Matthias Bruggerecb35302014-07-18 11:36:43 +020031
Stanley Chu56d52d32018-07-06 07:11:26 +080032#define TIMER_SYNC_TICKS (3)
Matthias Bruggerecb35302014-07-18 11:36:43 +020033
Stanley Chu56d52d32018-07-06 07:11:26 +080034/* gpt */
35#define GPT_IRQ_EN_REG 0x00
36#define GPT_IRQ_ENABLE(val) BIT((val) - 1)
37#define GPT_IRQ_ACK_REG 0x08
38#define GPT_IRQ_ACK(val) BIT((val) - 1)
Matthias Bruggerecb35302014-07-18 11:36:43 +020039
Stanley Chu56d52d32018-07-06 07:11:26 +080040#define GPT_CTRL_REG(val) (0x10 * (val))
41#define GPT_CTRL_OP(val) (((val) & 0x3) << 4)
42#define GPT_CTRL_OP_ONESHOT (0)
43#define GPT_CTRL_OP_REPEAT (1)
44#define GPT_CTRL_OP_FREERUN (3)
45#define GPT_CTRL_CLEAR (2)
46#define GPT_CTRL_ENABLE (1)
47#define GPT_CTRL_DISABLE (0)
Matthias Bruggerecb35302014-07-18 11:36:43 +020048
Stanley Chu56d52d32018-07-06 07:11:26 +080049#define GPT_CLK_REG(val) (0x04 + (0x10 * (val)))
50#define GPT_CLK_SRC(val) (((val) & 0x1) << 4)
51#define GPT_CLK_SRC_SYS13M (0)
52#define GPT_CLK_SRC_RTC32K (1)
53#define GPT_CLK_DIV1 (0x0)
54#define GPT_CLK_DIV2 (0x1)
55
56#define GPT_CNT_REG(val) (0x08 + (0x10 * (val)))
57#define GPT_CMP_REG(val) (0x0C + (0x10 * (val)))
Matthias Bruggerecb35302014-07-18 11:36:43 +020058
Stanley Chue3af6772018-07-06 07:11:28 +080059/* system timer */
60#define SYST_BASE (0x40)
61
62#define SYST_CON (SYST_BASE + 0x0)
63#define SYST_VAL (SYST_BASE + 0x4)
64
65#define SYST_CON_REG(to) (timer_of_base(to) + SYST_CON)
66#define SYST_VAL_REG(to) (timer_of_base(to) + SYST_VAL)
67
68/*
69 * SYST_CON_EN: Clock enable. Shall be set to
70 * - Start timer countdown.
71 * - Allow timeout ticks being updated.
72 * - Allow changing interrupt functions.
73 *
74 * SYST_CON_IRQ_EN: Set to allow interrupt.
75 *
76 * SYST_CON_IRQ_CLR: Set to clear interrupt.
77 */
78#define SYST_CON_EN BIT(0)
79#define SYST_CON_IRQ_EN BIT(1)
80#define SYST_CON_IRQ_CLR BIT(4)
81
Yingjoe Chenf14665f2015-07-13 17:32:46 +080082static void __iomem *gpt_sched_reg __read_mostly;
83
Stanley Chue3af6772018-07-06 07:11:28 +080084static void mtk_syst_ack_irq(struct timer_of *to)
85{
86 /* Clear and disable interrupt */
87 writel(SYST_CON_IRQ_CLR | SYST_CON_EN, SYST_CON_REG(to));
88}
89
90static irqreturn_t mtk_syst_handler(int irq, void *dev_id)
91{
92 struct clock_event_device *clkevt = dev_id;
93 struct timer_of *to = to_timer_of(clkevt);
94
95 mtk_syst_ack_irq(to);
96 clkevt->event_handler(clkevt);
97
98 return IRQ_HANDLED;
99}
100
101static int mtk_syst_clkevt_next_event(unsigned long ticks,
102 struct clock_event_device *clkevt)
103{
104 struct timer_of *to = to_timer_of(clkevt);
105
106 /* Enable clock to allow timeout tick update later */
107 writel(SYST_CON_EN, SYST_CON_REG(to));
108
109 /*
110 * Write new timeout ticks. Timer shall start countdown
111 * after timeout ticks are updated.
112 */
113 writel(ticks, SYST_VAL_REG(to));
114
115 /* Enable interrupt */
116 writel(SYST_CON_EN | SYST_CON_IRQ_EN, SYST_CON_REG(to));
117
118 return 0;
119}
120
121static int mtk_syst_clkevt_shutdown(struct clock_event_device *clkevt)
122{
123 /* Disable timer */
124 writel(0, SYST_CON_REG(to_timer_of(clkevt)));
125
126 return 0;
127}
128
129static int mtk_syst_clkevt_resume(struct clock_event_device *clkevt)
130{
131 return mtk_syst_clkevt_shutdown(clkevt);
132}
133
134static int mtk_syst_clkevt_oneshot(struct clock_event_device *clkevt)
135{
136 return 0;
137}
138
Stanley Chu56d52d32018-07-06 07:11:26 +0800139static u64 notrace mtk_gpt_read_sched_clock(void)
Yingjoe Chenf14665f2015-07-13 17:32:46 +0800140{
141 return readl_relaxed(gpt_sched_reg);
142}
143
Stanley Chua0858f92018-07-06 07:11:27 +0800144static void mtk_gpt_clkevt_time_stop(struct timer_of *to, u8 timer)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200145{
146 u32 val;
147
Stanley Chua0858f92018-07-06 07:11:27 +0800148 val = readl(timer_of_base(to) + GPT_CTRL_REG(timer));
149 writel(val & ~GPT_CTRL_ENABLE, timer_of_base(to) +
150 GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200151}
152
Stanley Chua0858f92018-07-06 07:11:27 +0800153static void mtk_gpt_clkevt_time_setup(struct timer_of *to,
154 unsigned long delay, u8 timer)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200155{
Stanley Chua0858f92018-07-06 07:11:27 +0800156 writel(delay, timer_of_base(to) + GPT_CMP_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200157}
158
Stanley Chua0858f92018-07-06 07:11:27 +0800159static void mtk_gpt_clkevt_time_start(struct timer_of *to,
160 bool periodic, u8 timer)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200161{
162 u32 val;
163
164 /* Acknowledge interrupt */
Stanley Chua0858f92018-07-06 07:11:27 +0800165 writel(GPT_IRQ_ACK(timer), timer_of_base(to) + GPT_IRQ_ACK_REG);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200166
Stanley Chua0858f92018-07-06 07:11:27 +0800167 val = readl(timer_of_base(to) + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200168
169 /* Clear 2 bit timer operation mode field */
Stanley Chu56d52d32018-07-06 07:11:26 +0800170 val &= ~GPT_CTRL_OP(0x3);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200171
172 if (periodic)
Stanley Chu56d52d32018-07-06 07:11:26 +0800173 val |= GPT_CTRL_OP(GPT_CTRL_OP_REPEAT);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200174 else
Stanley Chu56d52d32018-07-06 07:11:26 +0800175 val |= GPT_CTRL_OP(GPT_CTRL_OP_ONESHOT);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200176
Stanley Chu56d52d32018-07-06 07:11:26 +0800177 writel(val | GPT_CTRL_ENABLE | GPT_CTRL_CLEAR,
Stanley Chua0858f92018-07-06 07:11:27 +0800178 timer_of_base(to) + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200179}
180
Stanley Chu56d52d32018-07-06 07:11:26 +0800181static int mtk_gpt_clkevt_shutdown(struct clock_event_device *clk)
Viresh Kumara2b7e102015-06-18 16:24:27 +0530182{
Stanley Chua0858f92018-07-06 07:11:27 +0800183 mtk_gpt_clkevt_time_stop(to_timer_of(clk), TIMER_CLK_EVT);
184
Viresh Kumara2b7e102015-06-18 16:24:27 +0530185 return 0;
186}
187
Stanley Chu56d52d32018-07-06 07:11:26 +0800188static int mtk_gpt_clkevt_set_periodic(struct clock_event_device *clk)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200189{
Stanley Chua0858f92018-07-06 07:11:27 +0800190 struct timer_of *to = to_timer_of(clk);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200191
Stanley Chua0858f92018-07-06 07:11:27 +0800192 mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT);
193 mtk_gpt_clkevt_time_setup(to, to->of_clk.period, TIMER_CLK_EVT);
194 mtk_gpt_clkevt_time_start(to, true, TIMER_CLK_EVT);
195
Viresh Kumara2b7e102015-06-18 16:24:27 +0530196 return 0;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200197}
198
Stanley Chu56d52d32018-07-06 07:11:26 +0800199static int mtk_gpt_clkevt_next_event(unsigned long event,
Stanley Chua0858f92018-07-06 07:11:27 +0800200 struct clock_event_device *clk)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200201{
Stanley Chua0858f92018-07-06 07:11:27 +0800202 struct timer_of *to = to_timer_of(clk);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200203
Stanley Chua0858f92018-07-06 07:11:27 +0800204 mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT);
205 mtk_gpt_clkevt_time_setup(to, event, TIMER_CLK_EVT);
206 mtk_gpt_clkevt_time_start(to, false, TIMER_CLK_EVT);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200207
208 return 0;
209}
210
Stanley Chu56d52d32018-07-06 07:11:26 +0800211static irqreturn_t mtk_gpt_interrupt(int irq, void *dev_id)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200212{
Stanley Chua0858f92018-07-06 07:11:27 +0800213 struct clock_event_device *clkevt = (struct clock_event_device *)dev_id;
214 struct timer_of *to = to_timer_of(clkevt);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200215
216 /* Acknowledge timer0 irq */
Stanley Chua0858f92018-07-06 07:11:27 +0800217 writel(GPT_IRQ_ACK(TIMER_CLK_EVT), timer_of_base(to) + GPT_IRQ_ACK_REG);
218 clkevt->event_handler(clkevt);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200219
220 return IRQ_HANDLED;
221}
222
Matthias Bruggerecb35302014-07-18 11:36:43 +0200223static void
Stanley Chua0858f92018-07-06 07:11:27 +0800224__init mtk_gpt_setup(struct timer_of *to, u8 timer, u8 option)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200225{
Stanley Chu56d52d32018-07-06 07:11:26 +0800226 writel(GPT_CTRL_CLEAR | GPT_CTRL_DISABLE,
Stanley Chua0858f92018-07-06 07:11:27 +0800227 timer_of_base(to) + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200228
Stanley Chu56d52d32018-07-06 07:11:26 +0800229 writel(GPT_CLK_SRC(GPT_CLK_SRC_SYS13M) | GPT_CLK_DIV1,
Stanley Chua0858f92018-07-06 07:11:27 +0800230 timer_of_base(to) + GPT_CLK_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200231
Stanley Chua0858f92018-07-06 07:11:27 +0800232 writel(0x0, timer_of_base(to) + GPT_CMP_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200233
Stanley Chu56d52d32018-07-06 07:11:26 +0800234 writel(GPT_CTRL_OP(option) | GPT_CTRL_ENABLE,
Stanley Chua0858f92018-07-06 07:11:27 +0800235 timer_of_base(to) + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200236}
237
Stanley Chua0858f92018-07-06 07:11:27 +0800238static void mtk_gpt_enable_irq(struct timer_of *to, u8 timer)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200239{
240 u32 val;
241
Daniel Lezcanofc686d02015-08-24 15:14:30 +0200242 /* Disable all interrupts */
Stanley Chua0858f92018-07-06 07:11:27 +0800243 writel(0x0, timer_of_base(to) + GPT_IRQ_EN_REG);
Daniel Lezcanofc686d02015-08-24 15:14:30 +0200244
245 /* Acknowledge all spurious pending interrupts */
Stanley Chua0858f92018-07-06 07:11:27 +0800246 writel(0x3f, timer_of_base(to) + GPT_IRQ_ACK_REG);
Daniel Lezcanofc686d02015-08-24 15:14:30 +0200247
Stanley Chua0858f92018-07-06 07:11:27 +0800248 val = readl(timer_of_base(to) + GPT_IRQ_EN_REG);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200249 writel(val | GPT_IRQ_ENABLE(timer),
Stanley Chua0858f92018-07-06 07:11:27 +0800250 timer_of_base(to) + GPT_IRQ_EN_REG);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200251}
252
Stanley Chua0858f92018-07-06 07:11:27 +0800253static struct timer_of to = {
254 .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
255
256 .clkevt = {
257 .name = "mtk-clkevt",
258 .rating = 300,
259 .cpumask = cpu_possible_mask,
260 },
261
262 .of_irq = {
263 .flags = IRQF_TIMER | IRQF_IRQPOLL,
264 },
265};
266
Stanley Chue3af6772018-07-06 07:11:28 +0800267static int __init mtk_syst_init(struct device_node *node)
268{
269 int ret;
270
271 to.clkevt.features = CLOCK_EVT_FEAT_DYNIRQ | CLOCK_EVT_FEAT_ONESHOT;
272 to.clkevt.set_state_shutdown = mtk_syst_clkevt_shutdown;
273 to.clkevt.set_state_oneshot = mtk_syst_clkevt_oneshot;
274 to.clkevt.tick_resume = mtk_syst_clkevt_resume;
275 to.clkevt.set_next_event = mtk_syst_clkevt_next_event;
276 to.of_irq.handler = mtk_syst_handler;
277
278 ret = timer_of_init(node, &to);
279 if (ret)
Fabien Parentdaa2c402019-09-19 21:13:15 +0200280 return ret;
Stanley Chue3af6772018-07-06 07:11:28 +0800281
282 clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
283 TIMER_SYNC_TICKS, 0xffffffff);
284
285 return 0;
Stanley Chue3af6772018-07-06 07:11:28 +0800286}
287
Stanley Chu56d52d32018-07-06 07:11:26 +0800288static int __init mtk_gpt_init(struct device_node *node)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200289{
Stanley Chua0858f92018-07-06 07:11:27 +0800290 int ret;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200291
Stanley Chua0858f92018-07-06 07:11:27 +0800292 to.clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
293 to.clkevt.set_state_shutdown = mtk_gpt_clkevt_shutdown;
294 to.clkevt.set_state_periodic = mtk_gpt_clkevt_set_periodic;
295 to.clkevt.set_state_oneshot = mtk_gpt_clkevt_shutdown;
296 to.clkevt.tick_resume = mtk_gpt_clkevt_shutdown;
297 to.clkevt.set_next_event = mtk_gpt_clkevt_next_event;
298 to.of_irq.handler = mtk_gpt_interrupt;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200299
Stanley Chua0858f92018-07-06 07:11:27 +0800300 ret = timer_of_init(node, &to);
301 if (ret)
Fabien Parentdaa2c402019-09-19 21:13:15 +0200302 return ret;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200303
Matthias Bruggerecb35302014-07-18 11:36:43 +0200304 /* Configure clock source */
Stanley Chua0858f92018-07-06 07:11:27 +0800305 mtk_gpt_setup(&to, TIMER_CLK_SRC, GPT_CTRL_OP_FREERUN);
306 clocksource_mmio_init(timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC),
307 node->name, timer_of_rate(&to), 300, 32,
308 clocksource_mmio_readl_up);
309 gpt_sched_reg = timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC);
310 sched_clock_register(mtk_gpt_read_sched_clock, 32, timer_of_rate(&to));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200311
312 /* Configure clock event */
Stanley Chua0858f92018-07-06 07:11:27 +0800313 mtk_gpt_setup(&to, TIMER_CLK_EVT, GPT_CTRL_OP_REPEAT);
314 clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
315 TIMER_SYNC_TICKS, 0xffffffff);
Matthias Bruggerd4a19eb32015-02-19 11:41:33 +0100316
Stanley Chua0858f92018-07-06 07:11:27 +0800317 mtk_gpt_enable_irq(&to, TIMER_CLK_EVT);
Matthias Bruggerd4a19eb32015-02-19 11:41:33 +0100318
Daniel Lezcanod64e24c2016-05-31 17:43:47 +0200319 return 0;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200320}
Stanley Chu56d52d32018-07-06 07:11:26 +0800321TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init);
Stanley Chue3af6772018-07-06 07:11:28 +0800322TIMER_OF_DECLARE(mtk_mt6765, "mediatek,mt6765-timer", mtk_syst_init);