blob: e57c4d7414a452d6032cd6dd173525796b796fda [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
Yingjoe Chenf14665f2015-07-13 17:32:46 +080059static void __iomem *gpt_sched_reg __read_mostly;
60
Stanley Chu56d52d32018-07-06 07:11:26 +080061static u64 notrace mtk_gpt_read_sched_clock(void)
Yingjoe Chenf14665f2015-07-13 17:32:46 +080062{
63 return readl_relaxed(gpt_sched_reg);
64}
65
Stanley Chua0858f92018-07-06 07:11:27 +080066static void mtk_gpt_clkevt_time_stop(struct timer_of *to, u8 timer)
Matthias Bruggerecb35302014-07-18 11:36:43 +020067{
68 u32 val;
69
Stanley Chua0858f92018-07-06 07:11:27 +080070 val = readl(timer_of_base(to) + GPT_CTRL_REG(timer));
71 writel(val & ~GPT_CTRL_ENABLE, timer_of_base(to) +
72 GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +020073}
74
Stanley Chua0858f92018-07-06 07:11:27 +080075static void mtk_gpt_clkevt_time_setup(struct timer_of *to,
76 unsigned long delay, u8 timer)
Matthias Bruggerecb35302014-07-18 11:36:43 +020077{
Stanley Chua0858f92018-07-06 07:11:27 +080078 writel(delay, timer_of_base(to) + GPT_CMP_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +020079}
80
Stanley Chua0858f92018-07-06 07:11:27 +080081static void mtk_gpt_clkevt_time_start(struct timer_of *to,
82 bool periodic, u8 timer)
Matthias Bruggerecb35302014-07-18 11:36:43 +020083{
84 u32 val;
85
86 /* Acknowledge interrupt */
Stanley Chua0858f92018-07-06 07:11:27 +080087 writel(GPT_IRQ_ACK(timer), timer_of_base(to) + GPT_IRQ_ACK_REG);
Matthias Bruggerecb35302014-07-18 11:36:43 +020088
Stanley Chua0858f92018-07-06 07:11:27 +080089 val = readl(timer_of_base(to) + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +020090
91 /* Clear 2 bit timer operation mode field */
Stanley Chu56d52d32018-07-06 07:11:26 +080092 val &= ~GPT_CTRL_OP(0x3);
Matthias Bruggerecb35302014-07-18 11:36:43 +020093
94 if (periodic)
Stanley Chu56d52d32018-07-06 07:11:26 +080095 val |= GPT_CTRL_OP(GPT_CTRL_OP_REPEAT);
Matthias Bruggerecb35302014-07-18 11:36:43 +020096 else
Stanley Chu56d52d32018-07-06 07:11:26 +080097 val |= GPT_CTRL_OP(GPT_CTRL_OP_ONESHOT);
Matthias Bruggerecb35302014-07-18 11:36:43 +020098
Stanley Chu56d52d32018-07-06 07:11:26 +080099 writel(val | GPT_CTRL_ENABLE | GPT_CTRL_CLEAR,
Stanley Chua0858f92018-07-06 07:11:27 +0800100 timer_of_base(to) + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200101}
102
Stanley Chu56d52d32018-07-06 07:11:26 +0800103static int mtk_gpt_clkevt_shutdown(struct clock_event_device *clk)
Viresh Kumara2b7e102015-06-18 16:24:27 +0530104{
Stanley Chua0858f92018-07-06 07:11:27 +0800105 mtk_gpt_clkevt_time_stop(to_timer_of(clk), TIMER_CLK_EVT);
106
Viresh Kumara2b7e102015-06-18 16:24:27 +0530107 return 0;
108}
109
Stanley Chu56d52d32018-07-06 07:11:26 +0800110static int mtk_gpt_clkevt_set_periodic(struct clock_event_device *clk)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200111{
Stanley Chua0858f92018-07-06 07:11:27 +0800112 struct timer_of *to = to_timer_of(clk);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200113
Stanley Chua0858f92018-07-06 07:11:27 +0800114 mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT);
115 mtk_gpt_clkevt_time_setup(to, to->of_clk.period, TIMER_CLK_EVT);
116 mtk_gpt_clkevt_time_start(to, true, TIMER_CLK_EVT);
117
Viresh Kumara2b7e102015-06-18 16:24:27 +0530118 return 0;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200119}
120
Stanley Chu56d52d32018-07-06 07:11:26 +0800121static int mtk_gpt_clkevt_next_event(unsigned long event,
Stanley Chua0858f92018-07-06 07:11:27 +0800122 struct clock_event_device *clk)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200123{
Stanley Chua0858f92018-07-06 07:11:27 +0800124 struct timer_of *to = to_timer_of(clk);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200125
Stanley Chua0858f92018-07-06 07:11:27 +0800126 mtk_gpt_clkevt_time_stop(to, TIMER_CLK_EVT);
127 mtk_gpt_clkevt_time_setup(to, event, TIMER_CLK_EVT);
128 mtk_gpt_clkevt_time_start(to, false, TIMER_CLK_EVT);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200129
130 return 0;
131}
132
Stanley Chu56d52d32018-07-06 07:11:26 +0800133static irqreturn_t mtk_gpt_interrupt(int irq, void *dev_id)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200134{
Stanley Chua0858f92018-07-06 07:11:27 +0800135 struct clock_event_device *clkevt = (struct clock_event_device *)dev_id;
136 struct timer_of *to = to_timer_of(clkevt);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200137
138 /* Acknowledge timer0 irq */
Stanley Chua0858f92018-07-06 07:11:27 +0800139 writel(GPT_IRQ_ACK(TIMER_CLK_EVT), timer_of_base(to) + GPT_IRQ_ACK_REG);
140 clkevt->event_handler(clkevt);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200141
142 return IRQ_HANDLED;
143}
144
Matthias Bruggerecb35302014-07-18 11:36:43 +0200145static void
Stanley Chua0858f92018-07-06 07:11:27 +0800146__init mtk_gpt_setup(struct timer_of *to, u8 timer, u8 option)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200147{
Stanley Chu56d52d32018-07-06 07:11:26 +0800148 writel(GPT_CTRL_CLEAR | GPT_CTRL_DISABLE,
Stanley Chua0858f92018-07-06 07:11:27 +0800149 timer_of_base(to) + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200150
Stanley Chu56d52d32018-07-06 07:11:26 +0800151 writel(GPT_CLK_SRC(GPT_CLK_SRC_SYS13M) | GPT_CLK_DIV1,
Stanley Chua0858f92018-07-06 07:11:27 +0800152 timer_of_base(to) + GPT_CLK_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200153
Stanley Chua0858f92018-07-06 07:11:27 +0800154 writel(0x0, timer_of_base(to) + GPT_CMP_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200155
Stanley Chu56d52d32018-07-06 07:11:26 +0800156 writel(GPT_CTRL_OP(option) | GPT_CTRL_ENABLE,
Stanley Chua0858f92018-07-06 07:11:27 +0800157 timer_of_base(to) + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200158}
159
Stanley Chua0858f92018-07-06 07:11:27 +0800160static void mtk_gpt_enable_irq(struct timer_of *to, u8 timer)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200161{
162 u32 val;
163
Daniel Lezcanofc686d02015-08-24 15:14:30 +0200164 /* Disable all interrupts */
Stanley Chua0858f92018-07-06 07:11:27 +0800165 writel(0x0, timer_of_base(to) + GPT_IRQ_EN_REG);
Daniel Lezcanofc686d02015-08-24 15:14:30 +0200166
167 /* Acknowledge all spurious pending interrupts */
Stanley Chua0858f92018-07-06 07:11:27 +0800168 writel(0x3f, timer_of_base(to) + GPT_IRQ_ACK_REG);
Daniel Lezcanofc686d02015-08-24 15:14:30 +0200169
Stanley Chua0858f92018-07-06 07:11:27 +0800170 val = readl(timer_of_base(to) + GPT_IRQ_EN_REG);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200171 writel(val | GPT_IRQ_ENABLE(timer),
Stanley Chua0858f92018-07-06 07:11:27 +0800172 timer_of_base(to) + GPT_IRQ_EN_REG);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200173}
174
Stanley Chua0858f92018-07-06 07:11:27 +0800175static struct timer_of to = {
176 .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
177
178 .clkevt = {
179 .name = "mtk-clkevt",
180 .rating = 300,
181 .cpumask = cpu_possible_mask,
182 },
183
184 .of_irq = {
185 .flags = IRQF_TIMER | IRQF_IRQPOLL,
186 },
187};
188
Stanley Chu56d52d32018-07-06 07:11:26 +0800189static int __init mtk_gpt_init(struct device_node *node)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200190{
Stanley Chua0858f92018-07-06 07:11:27 +0800191 int ret;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200192
Stanley Chua0858f92018-07-06 07:11:27 +0800193 to.clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
194 to.clkevt.set_state_shutdown = mtk_gpt_clkevt_shutdown;
195 to.clkevt.set_state_periodic = mtk_gpt_clkevt_set_periodic;
196 to.clkevt.set_state_oneshot = mtk_gpt_clkevt_shutdown;
197 to.clkevt.tick_resume = mtk_gpt_clkevt_shutdown;
198 to.clkevt.set_next_event = mtk_gpt_clkevt_next_event;
199 to.of_irq.handler = mtk_gpt_interrupt;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200200
Stanley Chua0858f92018-07-06 07:11:27 +0800201 ret = timer_of_init(node, &to);
202 if (ret)
203 goto err;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200204
Matthias Bruggerecb35302014-07-18 11:36:43 +0200205 /* Configure clock source */
Stanley Chua0858f92018-07-06 07:11:27 +0800206 mtk_gpt_setup(&to, TIMER_CLK_SRC, GPT_CTRL_OP_FREERUN);
207 clocksource_mmio_init(timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC),
208 node->name, timer_of_rate(&to), 300, 32,
209 clocksource_mmio_readl_up);
210 gpt_sched_reg = timer_of_base(&to) + GPT_CNT_REG(TIMER_CLK_SRC);
211 sched_clock_register(mtk_gpt_read_sched_clock, 32, timer_of_rate(&to));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200212
213 /* Configure clock event */
Stanley Chua0858f92018-07-06 07:11:27 +0800214 mtk_gpt_setup(&to, TIMER_CLK_EVT, GPT_CTRL_OP_REPEAT);
215 clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
216 TIMER_SYNC_TICKS, 0xffffffff);
Matthias Bruggerd4a19eb32015-02-19 11:41:33 +0100217
Stanley Chua0858f92018-07-06 07:11:27 +0800218 mtk_gpt_enable_irq(&to, TIMER_CLK_EVT);
Matthias Bruggerd4a19eb32015-02-19 11:41:33 +0100219
Daniel Lezcanod64e24c2016-05-31 17:43:47 +0200220 return 0;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200221
Stanley Chua0858f92018-07-06 07:11:27 +0800222err:
223 timer_of_cleanup(&to);
224 return ret;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200225}
Stanley Chu56d52d32018-07-06 07:11:26 +0800226TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init);