blob: e3657d29e7654265ca6a590708aecdb03d96535f [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/clk.h>
22#include <linux/clockchips.h>
23#include <linux/interrupt.h>
24#include <linux/irq.h>
25#include <linux/irqreturn.h>
26#include <linux/of.h>
27#include <linux/of_address.h>
28#include <linux/of_irq.h>
Yingjoe Chenf14665f2015-07-13 17:32:46 +080029#include <linux/sched_clock.h>
Matthias Bruggerecb35302014-07-18 11:36:43 +020030#include <linux/slab.h>
31
Stanley Chu56d52d32018-07-06 07:11:26 +080032#define TIMER_CLK_EVT (1)
33#define TIMER_CLK_SRC (2)
Matthias Bruggerecb35302014-07-18 11:36:43 +020034
Stanley Chu56d52d32018-07-06 07:11:26 +080035#define TIMER_SYNC_TICKS (3)
Matthias Bruggerecb35302014-07-18 11:36:43 +020036
Stanley Chu56d52d32018-07-06 07:11:26 +080037/* gpt */
38#define GPT_IRQ_EN_REG 0x00
39#define GPT_IRQ_ENABLE(val) BIT((val) - 1)
40#define GPT_IRQ_ACK_REG 0x08
41#define GPT_IRQ_ACK(val) BIT((val) - 1)
Matthias Bruggerecb35302014-07-18 11:36:43 +020042
Stanley Chu56d52d32018-07-06 07:11:26 +080043#define GPT_CTRL_REG(val) (0x10 * (val))
44#define GPT_CTRL_OP(val) (((val) & 0x3) << 4)
45#define GPT_CTRL_OP_ONESHOT (0)
46#define GPT_CTRL_OP_REPEAT (1)
47#define GPT_CTRL_OP_FREERUN (3)
48#define GPT_CTRL_CLEAR (2)
49#define GPT_CTRL_ENABLE (1)
50#define GPT_CTRL_DISABLE (0)
Matthias Bruggerecb35302014-07-18 11:36:43 +020051
Stanley Chu56d52d32018-07-06 07:11:26 +080052#define GPT_CLK_REG(val) (0x04 + (0x10 * (val)))
53#define GPT_CLK_SRC(val) (((val) & 0x1) << 4)
54#define GPT_CLK_SRC_SYS13M (0)
55#define GPT_CLK_SRC_RTC32K (1)
56#define GPT_CLK_DIV1 (0x0)
57#define GPT_CLK_DIV2 (0x1)
58
59#define GPT_CNT_REG(val) (0x08 + (0x10 * (val)))
60#define GPT_CMP_REG(val) (0x0C + (0x10 * (val)))
Matthias Bruggerecb35302014-07-18 11:36:43 +020061
62struct mtk_clock_event_device {
63 void __iomem *gpt_base;
64 u32 ticks_per_jiffy;
65 struct clock_event_device dev;
66};
67
Yingjoe Chenf14665f2015-07-13 17:32:46 +080068static void __iomem *gpt_sched_reg __read_mostly;
69
Stanley Chu56d52d32018-07-06 07:11:26 +080070static u64 notrace mtk_gpt_read_sched_clock(void)
Yingjoe Chenf14665f2015-07-13 17:32:46 +080071{
72 return readl_relaxed(gpt_sched_reg);
73}
74
Matthias Bruggerecb35302014-07-18 11:36:43 +020075static inline struct mtk_clock_event_device *to_mtk_clk(
76 struct clock_event_device *c)
77{
78 return container_of(c, struct mtk_clock_event_device, dev);
79}
80
Stanley Chu56d52d32018-07-06 07:11:26 +080081static void mtk_gpt_clkevt_time_stop(struct mtk_clock_event_device *evt, u8 timer)
Matthias Bruggerecb35302014-07-18 11:36:43 +020082{
83 u32 val;
84
Stanley Chu56d52d32018-07-06 07:11:26 +080085 val = readl(evt->gpt_base + GPT_CTRL_REG(timer));
86 writel(val & ~GPT_CTRL_ENABLE, evt->gpt_base +
87 GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +020088}
89
Stanley Chu56d52d32018-07-06 07:11:26 +080090static void mtk_gpt_clkevt_time_setup(struct mtk_clock_event_device *evt,
Matthias Bruggerecb35302014-07-18 11:36:43 +020091 unsigned long delay, u8 timer)
92{
Stanley Chu56d52d32018-07-06 07:11:26 +080093 writel(delay, evt->gpt_base + GPT_CMP_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +020094}
95
Stanley Chu56d52d32018-07-06 07:11:26 +080096static void mtk_gpt_clkevt_time_start(struct mtk_clock_event_device *evt,
Matthias Bruggerecb35302014-07-18 11:36:43 +020097 bool periodic, u8 timer)
98{
99 u32 val;
100
101 /* Acknowledge interrupt */
102 writel(GPT_IRQ_ACK(timer), evt->gpt_base + GPT_IRQ_ACK_REG);
103
Stanley Chu56d52d32018-07-06 07:11:26 +0800104 val = readl(evt->gpt_base + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200105
106 /* Clear 2 bit timer operation mode field */
Stanley Chu56d52d32018-07-06 07:11:26 +0800107 val &= ~GPT_CTRL_OP(0x3);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200108
109 if (periodic)
Stanley Chu56d52d32018-07-06 07:11:26 +0800110 val |= GPT_CTRL_OP(GPT_CTRL_OP_REPEAT);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200111 else
Stanley Chu56d52d32018-07-06 07:11:26 +0800112 val |= GPT_CTRL_OP(GPT_CTRL_OP_ONESHOT);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200113
Stanley Chu56d52d32018-07-06 07:11:26 +0800114 writel(val | GPT_CTRL_ENABLE | GPT_CTRL_CLEAR,
115 evt->gpt_base + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200116}
117
Stanley Chu56d52d32018-07-06 07:11:26 +0800118static int mtk_gpt_clkevt_shutdown(struct clock_event_device *clk)
Viresh Kumara2b7e102015-06-18 16:24:27 +0530119{
Stanley Chu56d52d32018-07-06 07:11:26 +0800120 mtk_gpt_clkevt_time_stop(to_mtk_clk(clk), TIMER_CLK_EVT);
Viresh Kumara2b7e102015-06-18 16:24:27 +0530121 return 0;
122}
123
Stanley Chu56d52d32018-07-06 07:11:26 +0800124static int mtk_gpt_clkevt_set_periodic(struct clock_event_device *clk)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200125{
126 struct mtk_clock_event_device *evt = to_mtk_clk(clk);
127
Stanley Chu56d52d32018-07-06 07:11:26 +0800128 mtk_gpt_clkevt_time_stop(evt, TIMER_CLK_EVT);
129 mtk_gpt_clkevt_time_setup(evt, evt->ticks_per_jiffy, TIMER_CLK_EVT);
130 mtk_gpt_clkevt_time_start(evt, true, TIMER_CLK_EVT);
Viresh Kumara2b7e102015-06-18 16:24:27 +0530131 return 0;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200132}
133
Stanley Chu56d52d32018-07-06 07:11:26 +0800134static int mtk_gpt_clkevt_next_event(unsigned long event,
Matthias Bruggerecb35302014-07-18 11:36:43 +0200135 struct clock_event_device *clk)
136{
137 struct mtk_clock_event_device *evt = to_mtk_clk(clk);
138
Stanley Chu56d52d32018-07-06 07:11:26 +0800139 mtk_gpt_clkevt_time_stop(evt, TIMER_CLK_EVT);
140 mtk_gpt_clkevt_time_setup(evt, event, TIMER_CLK_EVT);
141 mtk_gpt_clkevt_time_start(evt, false, TIMER_CLK_EVT);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200142
143 return 0;
144}
145
Stanley Chu56d52d32018-07-06 07:11:26 +0800146static irqreturn_t mtk_gpt_interrupt(int irq, void *dev_id)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200147{
148 struct mtk_clock_event_device *evt = dev_id;
149
150 /* Acknowledge timer0 irq */
Stanley Chu56d52d32018-07-06 07:11:26 +0800151 writel(GPT_IRQ_ACK(TIMER_CLK_EVT), evt->gpt_base + GPT_IRQ_ACK_REG);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200152 evt->dev.event_handler(&evt->dev);
153
154 return IRQ_HANDLED;
155}
156
Matthias Bruggerecb35302014-07-18 11:36:43 +0200157static void
Stanley Chu56d52d32018-07-06 07:11:26 +0800158__init mtk_gpt_setup(struct mtk_clock_event_device *evt, u8 timer, u8 option)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200159{
Stanley Chu56d52d32018-07-06 07:11:26 +0800160 writel(GPT_CTRL_CLEAR | GPT_CTRL_DISABLE,
161 evt->gpt_base + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200162
Stanley Chu56d52d32018-07-06 07:11:26 +0800163 writel(GPT_CLK_SRC(GPT_CLK_SRC_SYS13M) | GPT_CLK_DIV1,
164 evt->gpt_base + GPT_CLK_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200165
Stanley Chu56d52d32018-07-06 07:11:26 +0800166 writel(0x0, evt->gpt_base + GPT_CMP_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200167
Stanley Chu56d52d32018-07-06 07:11:26 +0800168 writel(GPT_CTRL_OP(option) | GPT_CTRL_ENABLE,
169 evt->gpt_base + GPT_CTRL_REG(timer));
Matthias Bruggerecb35302014-07-18 11:36:43 +0200170}
171
Stanley Chu56d52d32018-07-06 07:11:26 +0800172static void mtk_gpt_enable_irq(struct mtk_clock_event_device *evt, u8 timer)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200173{
174 u32 val;
175
Daniel Lezcanofc686d02015-08-24 15:14:30 +0200176 /* Disable all interrupts */
177 writel(0x0, evt->gpt_base + GPT_IRQ_EN_REG);
178
179 /* Acknowledge all spurious pending interrupts */
180 writel(0x3f, evt->gpt_base + GPT_IRQ_ACK_REG);
181
Matthias Bruggerecb35302014-07-18 11:36:43 +0200182 val = readl(evt->gpt_base + GPT_IRQ_EN_REG);
183 writel(val | GPT_IRQ_ENABLE(timer),
184 evt->gpt_base + GPT_IRQ_EN_REG);
185}
186
Stanley Chu56d52d32018-07-06 07:11:26 +0800187static int __init mtk_gpt_init(struct device_node *node)
Matthias Bruggerecb35302014-07-18 11:36:43 +0200188{
189 struct mtk_clock_event_device *evt;
190 struct resource res;
191 unsigned long rate = 0;
192 struct clk *clk;
193
194 evt = kzalloc(sizeof(*evt), GFP_KERNEL);
Alexey Klimov6cd7cca2015-10-25 23:21:23 +0000195 if (!evt)
Daniel Lezcanod64e24c2016-05-31 17:43:47 +0200196 return -ENOMEM;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200197
198 evt->dev.name = "mtk_tick";
199 evt->dev.rating = 300;
200 evt->dev.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
Stanley Chu56d52d32018-07-06 07:11:26 +0800201 evt->dev.set_state_shutdown = mtk_gpt_clkevt_shutdown;
202 evt->dev.set_state_periodic = mtk_gpt_clkevt_set_periodic;
203 evt->dev.set_state_oneshot = mtk_gpt_clkevt_shutdown;
204 evt->dev.tick_resume = mtk_gpt_clkevt_shutdown;
205 evt->dev.set_next_event = mtk_gpt_clkevt_next_event;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200206 evt->dev.cpumask = cpu_possible_mask;
207
Stanley Chu56d52d32018-07-06 07:11:26 +0800208 evt->gpt_base = of_io_request_and_map(node, 0, "mtk-timer-gpt");
Matthias Bruggerecb35302014-07-18 11:36:43 +0200209 if (IS_ERR(evt->gpt_base)) {
Alexey Klimov6cd7cca2015-10-25 23:21:23 +0000210 pr_err("Can't get resource\n");
Alexey Klimov11faa202015-10-25 23:21:24 +0000211 goto err_kzalloc;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200212 }
213
214 evt->dev.irq = irq_of_parse_and_map(node, 0);
215 if (evt->dev.irq <= 0) {
Alexey Klimov6cd7cca2015-10-25 23:21:23 +0000216 pr_err("Can't parse IRQ\n");
Matthias Bruggerecb35302014-07-18 11:36:43 +0200217 goto err_mem;
218 }
219
220 clk = of_clk_get(node, 0);
221 if (IS_ERR(clk)) {
Alexey Klimov6cd7cca2015-10-25 23:21:23 +0000222 pr_err("Can't get timer clock\n");
Matthias Bruggerecb35302014-07-18 11:36:43 +0200223 goto err_irq;
224 }
225
226 if (clk_prepare_enable(clk)) {
Alexey Klimov6cd7cca2015-10-25 23:21:23 +0000227 pr_err("Can't prepare clock\n");
Matthias Bruggerecb35302014-07-18 11:36:43 +0200228 goto err_clk_put;
229 }
230 rate = clk_get_rate(clk);
231
Stanley Chu56d52d32018-07-06 07:11:26 +0800232 if (request_irq(evt->dev.irq, mtk_gpt_interrupt,
Matthias Bruggerecb35302014-07-18 11:36:43 +0200233 IRQF_TIMER | IRQF_IRQPOLL, "mtk_timer", evt)) {
Alexey Klimov6cd7cca2015-10-25 23:21:23 +0000234 pr_err("failed to setup irq %d\n", evt->dev.irq);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200235 goto err_clk_disable;
236 }
237
238 evt->ticks_per_jiffy = DIV_ROUND_UP(rate, HZ);
239
Matthias Bruggerecb35302014-07-18 11:36:43 +0200240 /* Configure clock source */
Stanley Chu56d52d32018-07-06 07:11:26 +0800241 mtk_gpt_setup(evt, TIMER_CLK_SRC, GPT_CTRL_OP_FREERUN);
242 clocksource_mmio_init(evt->gpt_base + GPT_CNT_REG(TIMER_CLK_SRC),
Matthias Bruggerecb35302014-07-18 11:36:43 +0200243 node->name, rate, 300, 32, clocksource_mmio_readl_up);
Stanley Chu56d52d32018-07-06 07:11:26 +0800244 gpt_sched_reg = evt->gpt_base + GPT_CNT_REG(TIMER_CLK_SRC);
245 sched_clock_register(mtk_gpt_read_sched_clock, 32, rate);
Matthias Bruggerecb35302014-07-18 11:36:43 +0200246
247 /* Configure clock event */
Stanley Chu56d52d32018-07-06 07:11:26 +0800248 mtk_gpt_setup(evt, TIMER_CLK_EVT, GPT_CTRL_OP_REPEAT);
249 clockevents_config_and_register(&evt->dev, rate, TIMER_SYNC_TICKS,
Matthias Bruggerecb35302014-07-18 11:36:43 +0200250 0xffffffff);
Matthias Bruggerd4a19eb32015-02-19 11:41:33 +0100251
Stanley Chu56d52d32018-07-06 07:11:26 +0800252 mtk_gpt_enable_irq(evt, TIMER_CLK_EVT);
Matthias Bruggerd4a19eb32015-02-19 11:41:33 +0100253
Daniel Lezcanod64e24c2016-05-31 17:43:47 +0200254 return 0;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200255
256err_clk_disable:
257 clk_disable_unprepare(clk);
258err_clk_put:
259 clk_put(clk);
260err_irq:
261 irq_dispose_mapping(evt->dev.irq);
262err_mem:
263 iounmap(evt->gpt_base);
264 of_address_to_resource(node, 0, &res);
265 release_mem_region(res.start, resource_size(&res));
Alexey Klimov11faa202015-10-25 23:21:24 +0000266err_kzalloc:
267 kfree(evt);
Daniel Lezcanod64e24c2016-05-31 17:43:47 +0200268
269 return -EINVAL;
Matthias Bruggerecb35302014-07-18 11:36:43 +0200270}
Stanley Chu56d52d32018-07-06 07:11:26 +0800271TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init);