blob: 0ba8155b8287d540352c5c777c14dc98c6ec8089 [file] [log] [blame]
Maxime Ripardb2ac5d72012-11-12 15:07:50 +01001/*
2 * Allwinner A1X SoCs timer handling.
3 *
4 * Copyright (C) 2012 Maxime Ripard
5 *
6 * Maxime Ripard <maxime.ripard@free-electrons.com>
7 *
8 * Based on code from
9 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
10 * Benn Huang <benn@allwinnertech.com>
11 *
12 * This file is licensed under the terms of the GNU General Public
13 * License version 2. This program is licensed "as is" without any
14 * warranty of any kind, whether express or implied.
15 */
16
17#include <linux/clk.h>
18#include <linux/clockchips.h>
19#include <linux/interrupt.h>
20#include <linux/irq.h>
21#include <linux/irqreturn.h>
Maxime Ripard137c6b32013-07-16 16:45:37 +020022#include <linux/sched_clock.h>
Maxime Ripardb2ac5d72012-11-12 15:07:50 +010023#include <linux/of.h>
24#include <linux/of_address.h>
25#include <linux/of_irq.h>
Maxime Ripardb2ac5d72012-11-12 15:07:50 +010026
Daniel Lezcano239751e2017-06-06 23:07:51 +020027#include "timer-of.h"
28
Maxime Ripard04981732013-03-10 17:03:46 +010029#define TIMER_IRQ_EN_REG 0x00
Maxime Ripard40777642013-07-16 16:45:37 +020030#define TIMER_IRQ_EN(val) BIT(val)
Maxime Ripardb2ac5d72012-11-12 15:07:50 +010031#define TIMER_IRQ_ST_REG 0x04
Maxime Ripard04981732013-03-10 17:03:46 +010032#define TIMER_CTL_REG(val) (0x10 * val + 0x10)
Maxime Ripard40777642013-07-16 16:45:37 +020033#define TIMER_CTL_ENABLE BIT(0)
Maxime Ripard9eded232013-07-16 16:45:37 +020034#define TIMER_CTL_RELOAD BIT(1)
Maxime Riparda2c49e72013-07-16 16:45:38 +020035#define TIMER_CTL_CLK_SRC(val) (((val) & 0x3) << 2)
36#define TIMER_CTL_CLK_SRC_OSC24M (1)
37#define TIMER_CTL_CLK_PRES(val) (((val) & 0x7) << 4)
Maxime Ripard40777642013-07-16 16:45:37 +020038#define TIMER_CTL_ONESHOT BIT(7)
Maxime Ripardbb008b92013-07-16 16:45:37 +020039#define TIMER_INTVAL_REG(val) (0x10 * (val) + 0x14)
40#define TIMER_CNTVAL_REG(val) (0x10 * (val) + 0x18)
Maxime Ripardb2ac5d72012-11-12 15:07:50 +010041
Maxime Ripard12e14802013-10-14 21:07:47 +020042#define TIMER_SYNC_TICKS 3
43
Maxime Ripard63d88f12013-07-16 16:45:38 +020044/*
45 * When we disable a timer, we need to wait at least for 2 cycles of
46 * the timer source clock. We will use for that the clocksource timer
47 * that is already setup and runs at the same frequency than the other
48 * timers, and we never will be disabled.
49 */
Daniel Lezcano239751e2017-06-06 23:07:51 +020050static void sun4i_clkevt_sync(void __iomem *base)
Maxime Ripard63d88f12013-07-16 16:45:38 +020051{
Daniel Lezcano239751e2017-06-06 23:07:51 +020052 u32 old = readl(base + TIMER_CNTVAL_REG(1));
Maxime Ripard63d88f12013-07-16 16:45:38 +020053
Daniel Lezcano239751e2017-06-06 23:07:51 +020054 while ((old - readl(base + TIMER_CNTVAL_REG(1))) < TIMER_SYNC_TICKS)
Maxime Ripard63d88f12013-07-16 16:45:38 +020055 cpu_relax();
56}
57
Daniel Lezcano239751e2017-06-06 23:07:51 +020058static void sun4i_clkevt_time_stop(void __iomem *base, u8 timer)
Maxime Ripard96651a02013-07-16 16:45:38 +020059{
Daniel Lezcano239751e2017-06-06 23:07:51 +020060 u32 val = readl(base + TIMER_CTL_REG(timer));
61 writel(val & ~TIMER_CTL_ENABLE, base + TIMER_CTL_REG(timer));
62 sun4i_clkevt_sync(base);
Maxime Ripard96651a02013-07-16 16:45:38 +020063}
64
Daniel Lezcano239751e2017-06-06 23:07:51 +020065static void sun4i_clkevt_time_setup(void __iomem *base, u8 timer,
66 unsigned long delay)
Maxime Ripard96651a02013-07-16 16:45:38 +020067{
Daniel Lezcano239751e2017-06-06 23:07:51 +020068 writel(delay, base + TIMER_INTVAL_REG(timer));
Maxime Ripard96651a02013-07-16 16:45:38 +020069}
70
Daniel Lezcano239751e2017-06-06 23:07:51 +020071static void sun4i_clkevt_time_start(void __iomem *base, u8 timer,
72 bool periodic)
Maxime Ripard96651a02013-07-16 16:45:38 +020073{
Daniel Lezcano239751e2017-06-06 23:07:51 +020074 u32 val = readl(base + TIMER_CTL_REG(timer));
Maxime Ripard96651a02013-07-16 16:45:38 +020075
76 if (periodic)
77 val &= ~TIMER_CTL_ONESHOT;
78 else
79 val |= TIMER_CTL_ONESHOT;
80
Maxime Ripard7e141832013-07-16 16:45:38 +020081 writel(val | TIMER_CTL_ENABLE | TIMER_CTL_RELOAD,
Daniel Lezcano239751e2017-06-06 23:07:51 +020082 base + TIMER_CTL_REG(timer));
Maxime Ripard96651a02013-07-16 16:45:38 +020083}
84
Viresh Kumar6de6c972015-06-18 16:24:37 +053085static int sun4i_clkevt_shutdown(struct clock_event_device *evt)
Maxime Ripardb2ac5d72012-11-12 15:07:50 +010086{
Daniel Lezcano239751e2017-06-06 23:07:51 +020087 struct timer_of *to = to_timer_of(evt);
88
89 sun4i_clkevt_time_stop(timer_of_base(to), 0);
90
Viresh Kumar6de6c972015-06-18 16:24:37 +053091 return 0;
92}
93
94static int sun4i_clkevt_set_oneshot(struct clock_event_device *evt)
95{
Daniel Lezcano239751e2017-06-06 23:07:51 +020096 struct timer_of *to = to_timer_of(evt);
97
98 sun4i_clkevt_time_stop(timer_of_base(to), 0);
99 sun4i_clkevt_time_start(timer_of_base(to), 0, false);
100
Viresh Kumar6de6c972015-06-18 16:24:37 +0530101 return 0;
102}
103
104static int sun4i_clkevt_set_periodic(struct clock_event_device *evt)
105{
Daniel Lezcano239751e2017-06-06 23:07:51 +0200106 struct timer_of *to = to_timer_of(evt);
107
108 sun4i_clkevt_time_stop(timer_of_base(to), 0);
109 sun4i_clkevt_time_setup(timer_of_base(to), 0, timer_of_period(to));
110 sun4i_clkevt_time_start(timer_of_base(to), 0, true);
111
Viresh Kumar6de6c972015-06-18 16:24:37 +0530112 return 0;
Maxime Ripardb2ac5d72012-11-12 15:07:50 +0100113}
114
Maxime Ripard119fd632013-03-24 11:49:25 +0100115static int sun4i_clkevt_next_event(unsigned long evt,
Daniel Lezcano239751e2017-06-06 23:07:51 +0200116 struct clock_event_device *clkevt)
Maxime Ripardb2ac5d72012-11-12 15:07:50 +0100117{
Daniel Lezcano239751e2017-06-06 23:07:51 +0200118 struct timer_of *to = to_timer_of(clkevt);
119
120 sun4i_clkevt_time_stop(timer_of_base(to), 0);
121 sun4i_clkevt_time_setup(timer_of_base(to), 0, evt - TIMER_SYNC_TICKS);
122 sun4i_clkevt_time_start(timer_of_base(to), 0, false);
Maxime Ripardb2ac5d72012-11-12 15:07:50 +0100123
124 return 0;
125}
126
Daniel Lezcano239751e2017-06-06 23:07:51 +0200127static void sun4i_timer_clear_interrupt(void __iomem *base)
Chen-Yu Tsaib53e7d02016-08-25 14:26:59 +0800128{
Daniel Lezcano239751e2017-06-06 23:07:51 +0200129 writel(TIMER_IRQ_EN(0), base + TIMER_IRQ_ST_REG);
Chen-Yu Tsaib53e7d02016-08-25 14:26:59 +0800130}
Maxime Ripardb2ac5d72012-11-12 15:07:50 +0100131
Maxime Ripard119fd632013-03-24 11:49:25 +0100132static irqreturn_t sun4i_timer_interrupt(int irq, void *dev_id)
Maxime Ripardb2ac5d72012-11-12 15:07:50 +0100133{
134 struct clock_event_device *evt = (struct clock_event_device *)dev_id;
Daniel Lezcano239751e2017-06-06 23:07:51 +0200135 struct timer_of *to = to_timer_of(evt);
Maxime Ripardb2ac5d72012-11-12 15:07:50 +0100136
Daniel Lezcano239751e2017-06-06 23:07:51 +0200137 sun4i_timer_clear_interrupt(timer_of_base(to));
Maxime Ripardb2ac5d72012-11-12 15:07:50 +0100138 evt->event_handler(evt);
139
140 return IRQ_HANDLED;
141}
142
Daniel Lezcano239751e2017-06-06 23:07:51 +0200143static struct timer_of to = {
144 .flags = TIMER_OF_IRQ | TIMER_OF_CLOCK | TIMER_OF_BASE,
145
146 .clkevt = {
147 .name = "sun4i_tick",
148 .rating = 350,
149 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
150 .set_state_shutdown = sun4i_clkevt_shutdown,
151 .set_state_periodic = sun4i_clkevt_set_periodic,
152 .set_state_oneshot = sun4i_clkevt_set_oneshot,
153 .tick_resume = sun4i_clkevt_shutdown,
154 .set_next_event = sun4i_clkevt_next_event,
155 .cpumask = cpu_possible_mask,
156 },
157
158 .of_irq = {
159 .handler = sun4i_timer_interrupt,
160 .flags = IRQF_TIMER | IRQF_IRQPOLL,
161 },
Maxime Ripardb2ac5d72012-11-12 15:07:50 +0100162};
163
Stephen Boyd662e7232013-11-20 00:47:32 +0100164static u64 notrace sun4i_timer_sched_read(void)
Maxime Ripard137c6b32013-07-16 16:45:37 +0200165{
Daniel Lezcano239751e2017-06-06 23:07:51 +0200166 return ~readl(timer_of_base(&to) + TIMER_CNTVAL_REG(1));
Maxime Ripard137c6b32013-07-16 16:45:37 +0200167}
168
Daniel Lezcanoce5dc742016-06-06 17:59:09 +0200169static int __init sun4i_timer_init(struct device_node *node)
Maxime Ripardb2ac5d72012-11-12 15:07:50 +0100170{
Daniel Lezcano239751e2017-06-06 23:07:51 +0200171 int ret;
Maxime Ripardb2ac5d72012-11-12 15:07:50 +0100172 u32 val;
173
Daniel Lezcano239751e2017-06-06 23:07:51 +0200174 ret = timer_of_init(node, &to);
175 if (ret)
Daniel Lezcanoce5dc742016-06-06 17:59:09 +0200176 return ret;
Maxime Ripardb2ac5d72012-11-12 15:07:50 +0100177
Daniel Lezcano239751e2017-06-06 23:07:51 +0200178 writel(~0, timer_of_base(&to) + TIMER_INTVAL_REG(1));
Maxime Ripard137c6b32013-07-16 16:45:37 +0200179 writel(TIMER_CTL_ENABLE | TIMER_CTL_RELOAD |
180 TIMER_CTL_CLK_SRC(TIMER_CTL_CLK_SRC_OSC24M),
Daniel Lezcano239751e2017-06-06 23:07:51 +0200181 timer_of_base(&to) + TIMER_CTL_REG(1));
Maxime Ripard137c6b32013-07-16 16:45:37 +0200182
Hans de Goede37b8b002015-03-30 22:17:10 +0200183 /*
184 * sched_clock_register does not have priorities, and on sun6i and
185 * later there is a better sched_clock registered by arm_arch_timer.c
186 */
187 if (of_machine_is_compatible("allwinner,sun4i-a10") ||
188 of_machine_is_compatible("allwinner,sun5i-a13") ||
Mesih Kilinc0113ab82019-02-11 12:21:08 +0300189 of_machine_is_compatible("allwinner,sun5i-a10s") ||
190 of_machine_is_compatible("allwinner,suniv-f1c100s"))
Daniel Lezcano239751e2017-06-06 23:07:51 +0200191 sched_clock_register(sun4i_timer_sched_read, 32,
192 timer_of_rate(&to));
Hans de Goede37b8b002015-03-30 22:17:10 +0200193
Daniel Lezcano239751e2017-06-06 23:07:51 +0200194 ret = clocksource_mmio_init(timer_of_base(&to) + TIMER_CNTVAL_REG(1),
195 node->name, timer_of_rate(&to), 350, 32,
196 clocksource_mmio_readl_down);
Daniel Lezcanoce5dc742016-06-06 17:59:09 +0200197 if (ret) {
Rafał Miłeckiac9ce6d2017-03-09 10:47:10 +0100198 pr_err("Failed to register clocksource\n");
Daniel Lezcanoce5dc742016-06-06 17:59:09 +0200199 return ret;
200 }
Maxime Ripard137c6b32013-07-16 16:45:37 +0200201
Maxime Ripard7e141832013-07-16 16:45:38 +0200202 writel(TIMER_CTL_CLK_SRC(TIMER_CTL_CLK_SRC_OSC24M),
Daniel Lezcano239751e2017-06-06 23:07:51 +0200203 timer_of_base(&to) + TIMER_CTL_REG(0));
Maxime Ripardb2ac5d72012-11-12 15:07:50 +0100204
Marc Zyngier6db50bb2013-12-02 09:29:35 +0000205 /* Make sure timer is stopped before playing with interrupts */
Daniel Lezcano239751e2017-06-06 23:07:51 +0200206 sun4i_clkevt_time_stop(timer_of_base(&to), 0);
Marc Zyngier6db50bb2013-12-02 09:29:35 +0000207
Chen-Yu Tsaib53e7d02016-08-25 14:26:59 +0800208 /* clear timer0 interrupt */
Daniel Lezcano239751e2017-06-06 23:07:51 +0200209 sun4i_timer_clear_interrupt(timer_of_base(&to));
Chen-Yu Tsaib53e7d02016-08-25 14:26:59 +0800210
Daniel Lezcano239751e2017-06-06 23:07:51 +0200211 clockevents_config_and_register(&to.clkevt, timer_of_rate(&to),
Maxime Ripard6bab4a82014-11-18 23:59:33 +0100212 TIMER_SYNC_TICKS, 0xffffffff);
213
Maxime Ripardb2ac5d72012-11-12 15:07:50 +0100214 /* Enable timer0 interrupt */
Daniel Lezcano239751e2017-06-06 23:07:51 +0200215 val = readl(timer_of_base(&to) + TIMER_IRQ_EN_REG);
216 writel(val | TIMER_IRQ_EN(0), timer_of_base(&to) + TIMER_IRQ_EN_REG);
Daniel Lezcanoce5dc742016-06-06 17:59:09 +0200217
218 return ret;
Maxime Ripardb2ac5d72012-11-12 15:07:50 +0100219}
Daniel Lezcano17273392017-05-26 16:56:11 +0200220TIMER_OF_DECLARE(sun4i, "allwinner,sun4i-a10-timer",
Maxime Ripard119fd632013-03-24 11:49:25 +0100221 sun4i_timer_init);
Maxime Ripardbca4e082019-07-22 10:12:21 +0200222TIMER_OF_DECLARE(sun8i_a23, "allwinner,sun8i-a23-timer",
223 sun4i_timer_init);
224TIMER_OF_DECLARE(sun8i_v3s, "allwinner,sun8i-v3s-timer",
225 sun4i_timer_init);
Mesih Kilinc0113ab82019-02-11 12:21:08 +0300226TIMER_OF_DECLARE(suniv, "allwinner,suniv-f1c100s-timer",
227 sun4i_timer_init);