blob: 6a3704142f31f7f262cdb8afc9437ae9df9d8d30 [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
Dmitry Osipenko49a678b2019-06-03 21:59:44 +03009#define pr_fmt(fmt) "tegra-timer: " fmt
10
Colin Cross2d5cd9a2010-01-28 16:41:42 -080011#include <linux/clk.h>
Joseph Lob4822dc2019-02-21 15:21:44 +080012#include <linux/clockchips.h>
13#include <linux/cpu.h>
14#include <linux/cpumask.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/interrupt.h>
Stephen Warren3a049312012-10-23 11:40:25 -060018#include <linux/of_address.h>
Stephen Warren56415482012-09-19 13:13:33 -060019#include <linux/of_irq.h>
Joseph Lob4822dc2019-02-21 15:21:44 +080020#include <linux/percpu.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070021#include <linux/sched_clock.h>
Joseph Lob4822dc2019-02-21 15:21:44 +080022#include <linux/time.h>
Colin Cross2d5cd9a2010-01-28 16:41:42 -080023
Joseph Lob4822dc2019-02-21 15:21:44 +080024#include "timer-of.h"
25
Dmitry Osipenko49a678b2019-06-03 21:59:44 +030026#define RTC_SECONDS 0x08
27#define RTC_SHADOW_SECONDS 0x0c
28#define RTC_MILLISECONDS 0x10
Colin Cross09361782010-11-28 16:26:19 -080029
Dmitry Osipenko49a678b2019-06-03 21:59:44 +030030#define TIMERUS_CNTR_1US 0x10
31#define TIMERUS_USEC_CFG 0x14
32#define TIMERUS_CNTR_FREEZE 0x4c
Colin Cross2d5cd9a2010-01-28 16:41:42 -080033
Joseph Lob4822dc2019-02-21 15:21:44 +080034#define TIMER_PTV 0x0
35#define TIMER_PTV_EN BIT(31)
36#define TIMER_PTV_PER BIT(30)
37#define TIMER_PCR 0x4
38#define TIMER_PCR_INTR_CLR BIT(30)
Colin Cross2d5cd9a2010-01-28 16:41:42 -080039
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +030040#define TIMER1_BASE 0x00
41#define TIMER2_BASE 0x08
42#define TIMER3_BASE 0x50
43#define TIMER4_BASE 0x58
44#define TIMER10_BASE 0x90
45
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +030046#define TIMER1_IRQ_IDX 0
Joseph Lob4822dc2019-02-21 15:21:44 +080047#define TIMER10_IRQ_IDX 10
Colin Cross2d5cd9a2010-01-28 16:41:42 -080048
Joseph Lob4822dc2019-02-21 15:21:44 +080049static u32 usec_config;
Stephen Warren3a049312012-10-23 11:40:25 -060050static void __iomem *timer_reg_base;
Colin Cross2d5cd9a2010-01-28 16:41:42 -080051
52static int tegra_timer_set_next_event(unsigned long cycles,
Dmitry Osipenko49a678b2019-06-03 21:59:44 +030053 struct clock_event_device *evt)
Colin Cross2d5cd9a2010-01-28 16:41:42 -080054{
Joseph Lob4822dc2019-02-21 15:21:44 +080055 void __iomem *reg_base = timer_of_base(to_timer_of(evt));
Colin Cross2d5cd9a2010-01-28 16:41:42 -080056
Dmitry Osipenko6b349c362019-06-03 21:59:42 +030057 writel_relaxed(TIMER_PTV_EN |
58 ((cycles > 1) ? (cycles - 1) : 0), /* n+1 scheme */
59 reg_base + TIMER_PTV);
Colin Cross2d5cd9a2010-01-28 16:41:42 -080060
61 return 0;
62}
63
Viresh Kumar4134d292015-07-03 14:24:35 +053064static int tegra_timer_shutdown(struct clock_event_device *evt)
65{
Joseph Lob4822dc2019-02-21 15:21:44 +080066 void __iomem *reg_base = timer_of_base(to_timer_of(evt));
67
Dmitry Osipenko6b349c362019-06-03 21:59:42 +030068 writel_relaxed(0, reg_base + TIMER_PTV);
Joseph Lob4822dc2019-02-21 15:21:44 +080069
Viresh Kumar4134d292015-07-03 14:24:35 +053070 return 0;
71}
72
73static int tegra_timer_set_periodic(struct clock_event_device *evt)
74{
Joseph Lob4822dc2019-02-21 15:21:44 +080075 void __iomem *reg_base = timer_of_base(to_timer_of(evt));
Viresh Kumar4134d292015-07-03 14:24:35 +053076
Dmitry Osipenko6b349c362019-06-03 21:59:42 +030077 writel_relaxed(TIMER_PTV_EN | TIMER_PTV_PER |
78 ((timer_of_rate(to_timer_of(evt)) / HZ) - 1),
79 reg_base + TIMER_PTV);
Joseph Lob4822dc2019-02-21 15:21:44 +080080
Viresh Kumar4134d292015-07-03 14:24:35 +053081 return 0;
Colin Cross2d5cd9a2010-01-28 16:41:42 -080082}
83
Joseph Lob4822dc2019-02-21 15:21:44 +080084static irqreturn_t tegra_timer_isr(int irq, void *dev_id)
85{
86 struct clock_event_device *evt = (struct clock_event_device *)dev_id;
87 void __iomem *reg_base = timer_of_base(to_timer_of(evt));
88
Dmitry Osipenko6b349c362019-06-03 21:59:42 +030089 writel_relaxed(TIMER_PCR_INTR_CLR, reg_base + TIMER_PCR);
Joseph Lob4822dc2019-02-21 15:21:44 +080090 evt->event_handler(evt);
91
92 return IRQ_HANDLED;
93}
94
95static void tegra_timer_suspend(struct clock_event_device *evt)
96{
97 void __iomem *reg_base = timer_of_base(to_timer_of(evt));
98
Dmitry Osipenko6b349c362019-06-03 21:59:42 +030099 writel_relaxed(TIMER_PCR_INTR_CLR, reg_base + TIMER_PCR);
Joseph Lob4822dc2019-02-21 15:21:44 +0800100}
101
102static void tegra_timer_resume(struct clock_event_device *evt)
103{
Dmitry Osipenko6b349c362019-06-03 21:59:42 +0300104 writel_relaxed(usec_config, timer_reg_base + TIMERUS_USEC_CFG);
Joseph Lob4822dc2019-02-21 15:21:44 +0800105}
106
Joseph Lob4822dc2019-02-21 15:21:44 +0800107static DEFINE_PER_CPU(struct timer_of, tegra_to) = {
108 .flags = TIMER_OF_CLOCK | TIMER_OF_BASE,
109
110 .clkevt = {
111 .name = "tegra_timer",
112 .rating = 460,
113 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
114 .set_next_event = tegra_timer_set_next_event,
115 .set_state_shutdown = tegra_timer_shutdown,
116 .set_state_periodic = tegra_timer_set_periodic,
117 .set_state_oneshot = tegra_timer_shutdown,
118 .tick_resume = tegra_timer_shutdown,
119 .suspend = tegra_timer_suspend,
120 .resume = tegra_timer_resume,
121 },
122};
123
124static int tegra_timer_setup(unsigned int cpu)
125{
126 struct timer_of *to = per_cpu_ptr(&tegra_to, cpu);
127
Dmitry Osipenko6b349c362019-06-03 21:59:42 +0300128 writel_relaxed(0, timer_of_base(to) + TIMER_PTV);
129 writel_relaxed(TIMER_PCR_INTR_CLR, timer_of_base(to) + TIMER_PCR);
Dmitry Osipenko77d57d12019-06-03 21:59:41 +0300130
Joseph Lob4822dc2019-02-21 15:21:44 +0800131 irq_force_affinity(to->clkevt.irq, cpumask_of(cpu));
132 enable_irq(to->clkevt.irq);
133
134 clockevents_config_and_register(&to->clkevt, timer_of_rate(to),
135 1, /* min */
136 0x1fffffff); /* 29 bits */
137
138 return 0;
139}
140
141static int tegra_timer_stop(unsigned int cpu)
142{
143 struct timer_of *to = per_cpu_ptr(&tegra_to, cpu);
144
145 to->clkevt.set_state_shutdown(&to->clkevt);
146 disable_irq_nosync(to->clkevt.irq);
147
148 return 0;
149}
Joseph Lob4822dc2019-02-21 15:21:44 +0800150
Stephen Boyd35702992013-07-18 16:21:26 -0700151static u64 notrace tegra_read_sched_clock(void)
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800152{
Dmitry Osipenko6b349c362019-06-03 21:59:42 +0300153 return readl_relaxed(timer_reg_base + TIMERUS_CNTR_1US);
Joseph Lob4822dc2019-02-21 15:21:44 +0800154}
155
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300156#ifdef CONFIG_ARM
Joseph Lob4822dc2019-02-21 15:21:44 +0800157static unsigned long tegra_delay_timer_read_counter_long(void)
158{
Dmitry Osipenko6b349c362019-06-03 21:59:42 +0300159 return readl_relaxed(timer_reg_base + TIMERUS_CNTR_1US);
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800160}
161
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300162static struct delay_timer tegra_delay_timer = {
163 .read_current_timer = tegra_delay_timer_read_counter_long,
164 .freq = 1000000,
165};
166#endif
167
Joseph Lo95170f02019-04-02 11:02:34 +0800168static struct timer_of suspend_rtc_to = {
169 .flags = TIMER_OF_BASE | TIMER_OF_CLOCK,
170};
171
Colin Cross09361782010-11-28 16:26:19 -0800172/*
173 * tegra_rtc_read - Reads the Tegra RTC registers
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300174 * Care must be taken that this function is not called while the
Colin Cross09361782010-11-28 16:26:19 -0800175 * tegra_rtc driver could be executing to avoid race conditions
176 * on the RTC shadow register
177 */
Joseph Lo95170f02019-04-02 11:02:34 +0800178static u64 tegra_rtc_read_ms(struct clocksource *cs)
Colin Cross09361782010-11-28 16:26:19 -0800179{
Dmitry Osipenko6b349c362019-06-03 21:59:42 +0300180 void __iomem *reg_base = timer_of_base(&suspend_rtc_to);
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300181
Dmitry Osipenko6b349c362019-06-03 21:59:42 +0300182 u32 ms = readl_relaxed(reg_base + RTC_MILLISECONDS);
183 u32 s = readl_relaxed(reg_base + RTC_SHADOW_SECONDS);
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300184
Colin Cross09361782010-11-28 16:26:19 -0800185 return (u64)s * MSEC_PER_SEC + ms;
186}
187
Joseph Lo95170f02019-04-02 11:02:34 +0800188static struct clocksource suspend_rtc_clocksource = {
189 .name = "tegra_suspend_timer",
190 .rating = 200,
191 .read = tegra_rtc_read_ms,
192 .mask = CLOCKSOURCE_MASK(32),
193 .flags = CLOCK_SOURCE_IS_CONTINUOUS | CLOCK_SOURCE_SUSPEND_NONSTOP,
194};
Xunlei Panga0c29982015-04-01 20:34:25 -0700195
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300196static inline unsigned int tegra_base_for_cpu(int cpu, bool tegra20)
197{
198 if (tegra20) {
199 switch (cpu) {
200 case 0:
201 return TIMER1_BASE;
202 case 1:
203 return TIMER2_BASE;
204 case 2:
205 return TIMER3_BASE;
206 default:
207 return TIMER4_BASE;
208 }
209 }
210
211 return TIMER10_BASE + cpu * 8;
212}
213
214static inline unsigned int tegra_irq_idx_for_cpu(int cpu, bool tegra20)
215{
216 if (tegra20)
217 return TIMER1_IRQ_IDX + cpu;
218
219 return TIMER10_IRQ_IDX + cpu;
220}
221
222static int __init tegra_init_timer(struct device_node *np, bool tegra20)
Peter De Schrijver0ff36b42014-06-12 18:58:29 +0300223{
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300224 struct timer_of *to;
225 int cpu, ret;
Peter De Schrijver0ff36b42014-06-12 18:58:29 +0300226
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300227 to = this_cpu_ptr(&tegra_to);
Joseph Lob4822dc2019-02-21 15:21:44 +0800228 ret = timer_of_init(np, to);
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300229 if (ret)
Joseph Lob4822dc2019-02-21 15:21:44 +0800230 goto out;
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800231
Joseph Lob4822dc2019-02-21 15:21:44 +0800232 timer_reg_base = timer_of_base(to);
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800233
Joseph Lob4822dc2019-02-21 15:21:44 +0800234 /*
235 * Configure microsecond timers to have 1MHz clock
236 * Config register is 0xqqww, where qq is "dividend", ww is "divisor"
237 * Uses n+1 scheme
238 */
239 switch (timer_of_rate(to)) {
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800240 case 12000000:
Joseph Lob4822dc2019-02-21 15:21:44 +0800241 usec_config = 0x000b; /* (11+1)/(0+1) */
242 break;
243 case 12800000:
244 usec_config = 0x043f; /* (63+1)/(4+1) */
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800245 break;
246 case 13000000:
Joseph Lob4822dc2019-02-21 15:21:44 +0800247 usec_config = 0x000c; /* (12+1)/(0+1) */
248 break;
249 case 16800000:
250 usec_config = 0x0453; /* (83+1)/(4+1) */
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800251 break;
252 case 19200000:
Joseph Lob4822dc2019-02-21 15:21:44 +0800253 usec_config = 0x045f; /* (95+1)/(4+1) */
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800254 break;
255 case 26000000:
Joseph Lob4822dc2019-02-21 15:21:44 +0800256 usec_config = 0x0019; /* (25+1)/(0+1) */
257 break;
258 case 38400000:
259 usec_config = 0x04bf; /* (191+1)/(4+1) */
260 break;
261 case 48000000:
262 usec_config = 0x002f; /* (47+1)/(0+1) */
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800263 break;
264 default:
Joseph Lob4822dc2019-02-21 15:21:44 +0800265 ret = -EINVAL;
266 goto out;
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800267 }
268
Dmitry Osipenko6b349c362019-06-03 21:59:42 +0300269 writel_relaxed(usec_config, timer_reg_base + TIMERUS_USEC_CFG);
Joseph Lob4822dc2019-02-21 15:21:44 +0800270
271 for_each_possible_cpu(cpu) {
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300272 struct timer_of *cpu_to = per_cpu_ptr(&tegra_to, cpu);
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300273 unsigned int base = tegra_base_for_cpu(cpu, tegra20);
274 unsigned int idx = tegra_irq_idx_for_cpu(cpu, tegra20);
Joseph Lob4822dc2019-02-21 15:21:44 +0800275
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300276 /*
277 * TIMER1-9 are fixed to 1MHz, TIMER10-13 are running off the
278 * parent clock.
279 */
280 if (tegra20)
281 cpu_to->of_clk.rate = 1000000;
282
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300283 cpu_to = per_cpu_ptr(&tegra_to, cpu);
284 cpu_to->of_base.base = timer_reg_base + base;
Joseph Lob4822dc2019-02-21 15:21:44 +0800285 cpu_to->clkevt.cpumask = cpumask_of(cpu);
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300286 cpu_to->clkevt.irq = irq_of_parse_and_map(np, idx);
Joseph Lob4822dc2019-02-21 15:21:44 +0800287 if (!cpu_to->clkevt.irq) {
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300288 pr_err("failed to map irq for cpu%d\n", cpu);
Joseph Lob4822dc2019-02-21 15:21:44 +0800289 ret = -EINVAL;
Dmitry Osipenko7a391672019-06-03 21:59:43 +0300290 goto out_irq;
Joseph Lob4822dc2019-02-21 15:21:44 +0800291 }
292
293 irq_set_status_flags(cpu_to->clkevt.irq, IRQ_NOAUTOEN);
294 ret = request_irq(cpu_to->clkevt.irq, tegra_timer_isr,
295 IRQF_TIMER | IRQF_NOBALANCING,
296 cpu_to->clkevt.name, &cpu_to->clkevt);
297 if (ret) {
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300298 pr_err("failed to set up irq for cpu%d: %d\n",
299 cpu, ret);
Dmitry Osipenko7a391672019-06-03 21:59:43 +0300300 irq_dispose_mapping(cpu_to->clkevt.irq);
301 cpu_to->clkevt.irq = 0;
Joseph Lob4822dc2019-02-21 15:21:44 +0800302 goto out_irq;
303 }
304 }
305
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300306 sched_clock_register(tegra_read_sched_clock, 32, 1000000);
307
308 ret = clocksource_mmio_init(timer_reg_base + TIMERUS_CNTR_1US,
309 "timer_us", 1000000,
310 300, 32, clocksource_mmio_readl_up);
311 if (ret)
312 pr_err("failed to register clocksource: %d\n", ret);
313
314#ifdef CONFIG_ARM
315 register_current_timer_delay(&tegra_delay_timer);
316#endif
317
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300318 ret = cpuhp_setup_state(CPUHP_AP_TEGRA_TIMER_STARTING,
319 "AP_TEGRA_TIMER_STARTING", tegra_timer_setup,
320 tegra_timer_stop);
321 if (ret)
322 pr_err("failed to set up cpu hp state: %d\n", ret);
Joseph Lob4822dc2019-02-21 15:21:44 +0800323
324 return ret;
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300325
Joseph Lob4822dc2019-02-21 15:21:44 +0800326out_irq:
327 for_each_possible_cpu(cpu) {
328 struct timer_of *cpu_to;
329
330 cpu_to = per_cpu_ptr(&tegra_to, cpu);
331 if (cpu_to->clkevt.irq) {
332 free_irq(cpu_to->clkevt.irq, &cpu_to->clkevt);
333 irq_dispose_mapping(cpu_to->clkevt.irq);
334 }
335 }
336out:
337 timer_of_cleanup(to);
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300338
Joseph Lob4822dc2019-02-21 15:21:44 +0800339 return ret;
340}
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300341
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300342static int __init tegra210_init_timer(struct device_node *np)
Joseph Lob4822dc2019-02-21 15:21:44 +0800343{
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300344 return tegra_init_timer(np, false);
345}
346TIMER_OF_DECLARE(tegra210_timer, "nvidia,tegra210-timer", tegra210_init_timer);
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300347
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300348static int __init tegra20_init_timer(struct device_node *np)
349{
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300350 return tegra_init_timer(np, true);
Daniel Lezcano53978bb2016-06-06 17:59:43 +0200351}
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300352TIMER_OF_DECLARE(tegra20_timer, "nvidia,tegra20-timer", tegra20_init_timer);
Daniel Lezcano53978bb2016-06-06 17:59:43 +0200353
354static int __init tegra20_init_rtc(struct device_node *np)
Rob Herring1d16cfb2013-02-07 11:36:23 -0600355{
Joseph Lo95170f02019-04-02 11:02:34 +0800356 int ret;
Rob Herring1d16cfb2013-02-07 11:36:23 -0600357
Joseph Lo95170f02019-04-02 11:02:34 +0800358 ret = timer_of_init(np, &suspend_rtc_to);
359 if (ret)
360 return ret;
Rob Herring1d16cfb2013-02-07 11:36:23 -0600361
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300362 return clocksource_register_hz(&suspend_rtc_clocksource, 1000);
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800363}
Daniel Lezcano17273392017-05-26 16:56:11 +0200364TIMER_OF_DECLARE(tegra20_rtc, "nvidia,tegra20-rtc", tegra20_init_rtc);