blob: 4b30ba6228c19dd3f9ce6288fd55f4031a1d9bb7 [file] [log] [blame]
Thomas Gleixner9c92ab62019-05-29 07:17:56 -07001// SPDX-License-Identifier: GPL-2.0-only
Colin Cross2d5cd9a2010-01-28 16:41:42 -08002/*
Colin Cross2d5cd9a2010-01-28 16:41:42 -08003 * Copyright (C) 2010 Google, Inc.
4 *
5 * Author:
6 * Colin Cross <ccross@google.com>
Colin Cross2d5cd9a2010-01-28 16:41:42 -08007 */
8
Colin Cross2d5cd9a2010-01-28 16:41:42 -08009#include <linux/clk.h>
Joseph Lob4822dc2019-02-21 15:21:44 +080010#include <linux/clockchips.h>
11#include <linux/cpu.h>
12#include <linux/cpumask.h>
13#include <linux/delay.h>
14#include <linux/err.h>
15#include <linux/interrupt.h>
Stephen Warren3a049312012-10-23 11:40:25 -060016#include <linux/of_address.h>
Stephen Warren56415482012-09-19 13:13:33 -060017#include <linux/of_irq.h>
Joseph Lob4822dc2019-02-21 15:21:44 +080018#include <linux/percpu.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070019#include <linux/sched_clock.h>
Joseph Lob4822dc2019-02-21 15:21:44 +080020#include <linux/time.h>
Colin Cross2d5cd9a2010-01-28 16:41:42 -080021
Joseph Lob4822dc2019-02-21 15:21:44 +080022#include "timer-of.h"
23
24#ifdef CONFIG_ARM
Colin Cross2d5cd9a2010-01-28 16:41:42 -080025#include <asm/mach/time.h>
Joseph Lob4822dc2019-02-21 15:21:44 +080026#endif
Colin Cross2d5cd9a2010-01-28 16:41:42 -080027
Colin Cross09361782010-11-28 16:26:19 -080028#define RTC_SECONDS 0x08
29#define RTC_SHADOW_SECONDS 0x0c
30#define RTC_MILLISECONDS 0x10
31
Colin Cross2d5cd9a2010-01-28 16:41:42 -080032#define TIMERUS_CNTR_1US 0x10
33#define TIMERUS_USEC_CFG 0x14
34#define TIMERUS_CNTR_FREEZE 0x4c
35
Joseph Lob4822dc2019-02-21 15:21:44 +080036#define TIMER_PTV 0x0
37#define TIMER_PTV_EN BIT(31)
38#define TIMER_PTV_PER BIT(30)
39#define TIMER_PCR 0x4
40#define TIMER_PCR_INTR_CLR BIT(30)
Colin Cross2d5cd9a2010-01-28 16:41:42 -080041
Joseph Lob4822dc2019-02-21 15:21:44 +080042#ifdef CONFIG_ARM
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +030043#define TIMER_CPU0 0x00 /* TIMER1 */
44#define TIMER_CPU2 0x50 /* TIMER3 */
45#define TIMER1_IRQ_IDX 0
46#define IRQ_IDX_FOR_CPU(cpu) (TIMER1_IRQ_IDX + cpu)
47#define TIMER_BASE_FOR_CPU(cpu) \
48 (((cpu) & 1) * 8 + ((cpu) < 2 ? TIMER_CPU0 : TIMER_CPU2))
Joseph Lob4822dc2019-02-21 15:21:44 +080049#else
50#define TIMER_CPU0 0x90 /* TIMER10 */
51#define TIMER10_IRQ_IDX 10
52#define IRQ_IDX_FOR_CPU(cpu) (TIMER10_IRQ_IDX + cpu)
Joseph Lob4822dc2019-02-21 15:21:44 +080053#define TIMER_BASE_FOR_CPU(cpu) (TIMER_CPU0 + (cpu) * 8)
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +030054#endif
Colin Cross2d5cd9a2010-01-28 16:41:42 -080055
Joseph Lob4822dc2019-02-21 15:21:44 +080056static u32 usec_config;
Stephen Warren3a049312012-10-23 11:40:25 -060057static void __iomem *timer_reg_base;
Joseph Lob4822dc2019-02-21 15:21:44 +080058#ifdef CONFIG_ARM
Peter De Schrijver0ff36b42014-06-12 18:58:29 +030059static struct delay_timer tegra_delay_timer;
Joseph Lob4822dc2019-02-21 15:21:44 +080060#endif
Colin Cross2d5cd9a2010-01-28 16:41:42 -080061
62static int tegra_timer_set_next_event(unsigned long cycles,
63 struct clock_event_device *evt)
64{
Joseph Lob4822dc2019-02-21 15:21:44 +080065 void __iomem *reg_base = timer_of_base(to_timer_of(evt));
Colin Cross2d5cd9a2010-01-28 16:41:42 -080066
Joseph Lob4822dc2019-02-21 15:21:44 +080067 writel(TIMER_PTV_EN |
68 ((cycles > 1) ? (cycles - 1) : 0), /* n+1 scheme */
69 reg_base + TIMER_PTV);
Colin Cross2d5cd9a2010-01-28 16:41:42 -080070
71 return 0;
72}
73
Viresh Kumar4134d292015-07-03 14:24:35 +053074static int tegra_timer_shutdown(struct clock_event_device *evt)
75{
Joseph Lob4822dc2019-02-21 15:21:44 +080076 void __iomem *reg_base = timer_of_base(to_timer_of(evt));
77
78 writel(0, reg_base + TIMER_PTV);
79
Viresh Kumar4134d292015-07-03 14:24:35 +053080 return 0;
81}
82
83static int tegra_timer_set_periodic(struct clock_event_device *evt)
84{
Joseph Lob4822dc2019-02-21 15:21:44 +080085 void __iomem *reg_base = timer_of_base(to_timer_of(evt));
Viresh Kumar4134d292015-07-03 14:24:35 +053086
Joseph Lob4822dc2019-02-21 15:21:44 +080087 writel(TIMER_PTV_EN | TIMER_PTV_PER |
88 ((timer_of_rate(to_timer_of(evt)) / HZ) - 1),
89 reg_base + TIMER_PTV);
90
Viresh Kumar4134d292015-07-03 14:24:35 +053091 return 0;
Colin Cross2d5cd9a2010-01-28 16:41:42 -080092}
93
Joseph Lob4822dc2019-02-21 15:21:44 +080094static irqreturn_t tegra_timer_isr(int irq, void *dev_id)
95{
96 struct clock_event_device *evt = (struct clock_event_device *)dev_id;
97 void __iomem *reg_base = timer_of_base(to_timer_of(evt));
98
99 writel(TIMER_PCR_INTR_CLR, reg_base + TIMER_PCR);
100 evt->event_handler(evt);
101
102 return IRQ_HANDLED;
103}
104
105static void tegra_timer_suspend(struct clock_event_device *evt)
106{
107 void __iomem *reg_base = timer_of_base(to_timer_of(evt));
108
109 writel(TIMER_PCR_INTR_CLR, reg_base + TIMER_PCR);
110}
111
112static void tegra_timer_resume(struct clock_event_device *evt)
113{
114 writel(usec_config, timer_reg_base + TIMERUS_USEC_CFG);
115}
116
Joseph Lob4822dc2019-02-21 15:21:44 +0800117static DEFINE_PER_CPU(struct timer_of, tegra_to) = {
118 .flags = TIMER_OF_CLOCK | TIMER_OF_BASE,
119
120 .clkevt = {
121 .name = "tegra_timer",
122 .rating = 460,
123 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
124 .set_next_event = tegra_timer_set_next_event,
125 .set_state_shutdown = tegra_timer_shutdown,
126 .set_state_periodic = tegra_timer_set_periodic,
127 .set_state_oneshot = tegra_timer_shutdown,
128 .tick_resume = tegra_timer_shutdown,
129 .suspend = tegra_timer_suspend,
130 .resume = tegra_timer_resume,
131 },
132};
133
134static int tegra_timer_setup(unsigned int cpu)
135{
136 struct timer_of *to = per_cpu_ptr(&tegra_to, cpu);
137
138 irq_force_affinity(to->clkevt.irq, cpumask_of(cpu));
139 enable_irq(to->clkevt.irq);
140
141 clockevents_config_and_register(&to->clkevt, timer_of_rate(to),
142 1, /* min */
143 0x1fffffff); /* 29 bits */
144
145 return 0;
146}
147
148static int tegra_timer_stop(unsigned int cpu)
149{
150 struct timer_of *to = per_cpu_ptr(&tegra_to, cpu);
151
152 to->clkevt.set_state_shutdown(&to->clkevt);
153 disable_irq_nosync(to->clkevt.irq);
154
155 return 0;
156}
Joseph Lob4822dc2019-02-21 15:21:44 +0800157
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300158#ifdef CONFIG_ARM
Stephen Boyd35702992013-07-18 16:21:26 -0700159static u64 notrace tegra_read_sched_clock(void)
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800160{
Joseph Lob4822dc2019-02-21 15:21:44 +0800161 return readl(timer_reg_base + TIMERUS_CNTR_1US);
162}
163
164static unsigned long tegra_delay_timer_read_counter_long(void)
165{
166 return readl(timer_reg_base + TIMERUS_CNTR_1US);
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800167}
168
Joseph Lo95170f02019-04-02 11:02:34 +0800169static struct timer_of suspend_rtc_to = {
170 .flags = TIMER_OF_BASE | TIMER_OF_CLOCK,
171};
172
Colin Cross09361782010-11-28 16:26:19 -0800173/*
174 * tegra_rtc_read - Reads the Tegra RTC registers
175 * Care must be taken that this funciton is not called while the
176 * tegra_rtc driver could be executing to avoid race conditions
177 * on the RTC shadow register
178 */
Joseph Lo95170f02019-04-02 11:02:34 +0800179static u64 tegra_rtc_read_ms(struct clocksource *cs)
Colin Cross09361782010-11-28 16:26:19 -0800180{
Joseph Lo95170f02019-04-02 11:02:34 +0800181 u32 ms = readl(timer_of_base(&suspend_rtc_to) + RTC_MILLISECONDS);
182 u32 s = readl(timer_of_base(&suspend_rtc_to) + RTC_SHADOW_SECONDS);
Colin Cross09361782010-11-28 16:26:19 -0800183 return (u64)s * MSEC_PER_SEC + ms;
184}
185
Joseph Lo95170f02019-04-02 11:02:34 +0800186static struct clocksource suspend_rtc_clocksource = {
187 .name = "tegra_suspend_timer",
188 .rating = 200,
189 .read = tegra_rtc_read_ms,
190 .mask = CLOCKSOURCE_MASK(32),
191 .flags = CLOCK_SOURCE_IS_CONTINUOUS | CLOCK_SOURCE_SUSPEND_NONSTOP,
192};
Joseph Lob4822dc2019-02-21 15:21:44 +0800193#endif
Xunlei Panga0c29982015-04-01 20:34:25 -0700194
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300195static int tegra_init_timer(struct device_node *np, bool tegra20)
Peter De Schrijver0ff36b42014-06-12 18:58:29 +0300196{
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300197 struct timer_of *to;
198 int cpu, ret;
Peter De Schrijver0ff36b42014-06-12 18:58:29 +0300199
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300200 to = this_cpu_ptr(&tegra_to);
Joseph Lob4822dc2019-02-21 15:21:44 +0800201 ret = timer_of_init(np, to);
202 if (ret < 0)
203 goto out;
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800204
Joseph Lob4822dc2019-02-21 15:21:44 +0800205 timer_reg_base = timer_of_base(to);
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800206
Joseph Lob4822dc2019-02-21 15:21:44 +0800207 /*
208 * Configure microsecond timers to have 1MHz clock
209 * Config register is 0xqqww, where qq is "dividend", ww is "divisor"
210 * Uses n+1 scheme
211 */
212 switch (timer_of_rate(to)) {
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800213 case 12000000:
Joseph Lob4822dc2019-02-21 15:21:44 +0800214 usec_config = 0x000b; /* (11+1)/(0+1) */
215 break;
216 case 12800000:
217 usec_config = 0x043f; /* (63+1)/(4+1) */
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800218 break;
219 case 13000000:
Joseph Lob4822dc2019-02-21 15:21:44 +0800220 usec_config = 0x000c; /* (12+1)/(0+1) */
221 break;
222 case 16800000:
223 usec_config = 0x0453; /* (83+1)/(4+1) */
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800224 break;
225 case 19200000:
Joseph Lob4822dc2019-02-21 15:21:44 +0800226 usec_config = 0x045f; /* (95+1)/(4+1) */
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800227 break;
228 case 26000000:
Joseph Lob4822dc2019-02-21 15:21:44 +0800229 usec_config = 0x0019; /* (25+1)/(0+1) */
230 break;
231 case 38400000:
232 usec_config = 0x04bf; /* (191+1)/(4+1) */
233 break;
234 case 48000000:
235 usec_config = 0x002f; /* (47+1)/(0+1) */
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800236 break;
237 default:
Joseph Lob4822dc2019-02-21 15:21:44 +0800238 ret = -EINVAL;
239 goto out;
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800240 }
241
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300242 writel(usec_config, timer_reg_base + TIMERUS_USEC_CFG);
Joseph Lob4822dc2019-02-21 15:21:44 +0800243
244 for_each_possible_cpu(cpu) {
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300245 struct timer_of *cpu_to = per_cpu_ptr(&tegra_to, cpu);
Joseph Lob4822dc2019-02-21 15:21:44 +0800246
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300247 /*
248 * TIMER1-9 are fixed to 1MHz, TIMER10-13 are running off the
249 * parent clock.
250 */
251 if (tegra20)
252 cpu_to->of_clk.rate = 1000000;
253
Joseph Lob4822dc2019-02-21 15:21:44 +0800254 cpu_to->of_base.base = timer_reg_base + TIMER_BASE_FOR_CPU(cpu);
Joseph Lob4822dc2019-02-21 15:21:44 +0800255 cpu_to->clkevt.cpumask = cpumask_of(cpu);
256 cpu_to->clkevt.irq =
257 irq_of_parse_and_map(np, IRQ_IDX_FOR_CPU(cpu));
258 if (!cpu_to->clkevt.irq) {
259 pr_err("%s: can't map IRQ for CPU%d\n",
260 __func__, cpu);
261 ret = -EINVAL;
262 goto out;
263 }
264
265 irq_set_status_flags(cpu_to->clkevt.irq, IRQ_NOAUTOEN);
266 ret = request_irq(cpu_to->clkevt.irq, tegra_timer_isr,
267 IRQF_TIMER | IRQF_NOBALANCING,
268 cpu_to->clkevt.name, &cpu_to->clkevt);
269 if (ret) {
270 pr_err("%s: cannot setup irq %d for CPU%d\n",
271 __func__, cpu_to->clkevt.irq, cpu);
272 ret = -EINVAL;
273 goto out_irq;
274 }
275 }
276
277 cpuhp_setup_state(CPUHP_AP_TEGRA_TIMER_STARTING,
278 "AP_TEGRA_TIMER_STARTING", tegra_timer_setup,
279 tegra_timer_stop);
280
281 return ret;
282out_irq:
283 for_each_possible_cpu(cpu) {
284 struct timer_of *cpu_to;
285
286 cpu_to = per_cpu_ptr(&tegra_to, cpu);
287 if (cpu_to->clkevt.irq) {
288 free_irq(cpu_to->clkevt.irq, &cpu_to->clkevt);
289 irq_dispose_mapping(cpu_to->clkevt.irq);
290 }
291 }
292out:
293 timer_of_cleanup(to);
294 return ret;
295}
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300296
297#ifdef CONFIG_ARM64
298static int __init tegra210_init_timer(struct device_node *np)
Joseph Lob4822dc2019-02-21 15:21:44 +0800299{
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300300 return tegra_init_timer(np, false);
301}
302TIMER_OF_DECLARE(tegra210_timer, "nvidia,tegra210-timer", tegra210_init_timer);
303#else /* CONFIG_ARM */
304static int __init tegra20_init_timer(struct device_node *np)
305{
306 struct timer_of *to;
307 int err;
Joseph Lob4822dc2019-02-21 15:21:44 +0800308
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300309 err = tegra_init_timer(np, true);
310 if (err)
311 return err;
Joseph Lob4822dc2019-02-21 15:21:44 +0800312
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300313 to = this_cpu_ptr(&tegra_to);
Joseph Lob4822dc2019-02-21 15:21:44 +0800314
315 sched_clock_register(tegra_read_sched_clock, 32,
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300316 timer_of_rate(to));
317 err = clocksource_mmio_init(timer_reg_base + TIMERUS_CNTR_1US,
318 "timer_us", timer_of_rate(to),
Joseph Lob4822dc2019-02-21 15:21:44 +0800319 300, 32, clocksource_mmio_readl_up);
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300320 if (err)
321 pr_err("Failed to register clocksource: %d\n", err);
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800322
Peter De Schrijver0ff36b42014-06-12 18:58:29 +0300323 tegra_delay_timer.read_current_timer =
324 tegra_delay_timer_read_counter_long;
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300325 tegra_delay_timer.freq = timer_of_rate(to);
Peter De Schrijver0ff36b42014-06-12 18:58:29 +0300326 register_current_timer_delay(&tegra_delay_timer);
327
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300328 return 0;
Daniel Lezcano53978bb2016-06-06 17:59:43 +0200329}
Daniel Lezcano53978bb2016-06-06 17:59:43 +0200330
331static int __init tegra20_init_rtc(struct device_node *np)
Rob Herring1d16cfb2013-02-07 11:36:23 -0600332{
Joseph Lo95170f02019-04-02 11:02:34 +0800333 int ret;
Rob Herring1d16cfb2013-02-07 11:36:23 -0600334
Joseph Lo95170f02019-04-02 11:02:34 +0800335 ret = timer_of_init(np, &suspend_rtc_to);
336 if (ret)
337 return ret;
Rob Herring1d16cfb2013-02-07 11:36:23 -0600338
Joseph Lo95170f02019-04-02 11:02:34 +0800339 clocksource_register_hz(&suspend_rtc_clocksource, 1000);
Rob Herring1d16cfb2013-02-07 11:36:23 -0600340
Joseph Lo95170f02019-04-02 11:02:34 +0800341 return 0;
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800342}
Daniel Lezcano17273392017-05-26 16:56:11 +0200343TIMER_OF_DECLARE(tegra20_rtc, "nvidia,tegra20-rtc", tegra20_init_rtc);
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300344TIMER_OF_DECLARE(tegra20_timer, "nvidia,tegra20-timer", tegra20_init_timer);
Joseph Lob4822dc2019-02-21 15:21:44 +0800345#endif