blob: f7a09d88dacb4363e08f8baedbd821bec3dfc86f [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));
Dmitry Osipenko09b25072019-06-18 17:03:53 +030076 unsigned long period = timer_of_period(to_timer_of(evt));
Viresh Kumar4134d292015-07-03 14:24:35 +053077
Dmitry Osipenko09b25072019-06-18 17:03:53 +030078 writel_relaxed(TIMER_PTV_EN | TIMER_PTV_PER | (period - 1),
Dmitry Osipenko6b349c362019-06-03 21:59:42 +030079 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{
Dmitry Osipenko7c708fd2019-06-18 17:03:54 +030086 struct clock_event_device *evt = dev_id;
Joseph Lob4822dc2019-02-21 15:21:44 +080087 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",
Joseph Lob4822dc2019-02-21 15:21:44 +0800112 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
113 .set_next_event = tegra_timer_set_next_event,
114 .set_state_shutdown = tegra_timer_shutdown,
115 .set_state_periodic = tegra_timer_set_periodic,
116 .set_state_oneshot = tegra_timer_shutdown,
117 .tick_resume = tegra_timer_shutdown,
118 .suspend = tegra_timer_suspend,
119 .resume = tegra_timer_resume,
120 },
121};
122
123static int tegra_timer_setup(unsigned int cpu)
124{
125 struct timer_of *to = per_cpu_ptr(&tegra_to, cpu);
126
Dmitry Osipenko6b349c362019-06-03 21:59:42 +0300127 writel_relaxed(0, timer_of_base(to) + TIMER_PTV);
128 writel_relaxed(TIMER_PCR_INTR_CLR, timer_of_base(to) + TIMER_PCR);
Dmitry Osipenko77d57d12019-06-03 21:59:41 +0300129
Joseph Lob4822dc2019-02-21 15:21:44 +0800130 irq_force_affinity(to->clkevt.irq, cpumask_of(cpu));
131 enable_irq(to->clkevt.irq);
132
133 clockevents_config_and_register(&to->clkevt, timer_of_rate(to),
134 1, /* min */
135 0x1fffffff); /* 29 bits */
136
137 return 0;
138}
139
140static int tegra_timer_stop(unsigned int cpu)
141{
142 struct timer_of *to = per_cpu_ptr(&tegra_to, cpu);
143
144 to->clkevt.set_state_shutdown(&to->clkevt);
145 disable_irq_nosync(to->clkevt.irq);
146
147 return 0;
148}
Joseph Lob4822dc2019-02-21 15:21:44 +0800149
Stephen Boyd35702992013-07-18 16:21:26 -0700150static u64 notrace tegra_read_sched_clock(void)
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800151{
Dmitry Osipenko6b349c362019-06-03 21:59:42 +0300152 return readl_relaxed(timer_reg_base + TIMERUS_CNTR_1US);
Joseph Lob4822dc2019-02-21 15:21:44 +0800153}
154
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300155#ifdef CONFIG_ARM
Joseph Lob4822dc2019-02-21 15:21:44 +0800156static unsigned long tegra_delay_timer_read_counter_long(void)
157{
Dmitry Osipenko6b349c362019-06-03 21:59:42 +0300158 return readl_relaxed(timer_reg_base + TIMERUS_CNTR_1US);
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800159}
160
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300161static struct delay_timer tegra_delay_timer = {
162 .read_current_timer = tegra_delay_timer_read_counter_long,
163 .freq = 1000000,
164};
165#endif
166
Joseph Lo95170f02019-04-02 11:02:34 +0800167static struct timer_of suspend_rtc_to = {
168 .flags = TIMER_OF_BASE | TIMER_OF_CLOCK,
169};
170
Colin Cross09361782010-11-28 16:26:19 -0800171/*
172 * tegra_rtc_read - Reads the Tegra RTC registers
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300173 * Care must be taken that this function is not called while the
Colin Cross09361782010-11-28 16:26:19 -0800174 * tegra_rtc driver could be executing to avoid race conditions
175 * on the RTC shadow register
176 */
Joseph Lo95170f02019-04-02 11:02:34 +0800177static u64 tegra_rtc_read_ms(struct clocksource *cs)
Colin Cross09361782010-11-28 16:26:19 -0800178{
Dmitry Osipenko6b349c362019-06-03 21:59:42 +0300179 void __iomem *reg_base = timer_of_base(&suspend_rtc_to);
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300180
Dmitry Osipenko6b349c362019-06-03 21:59:42 +0300181 u32 ms = readl_relaxed(reg_base + RTC_MILLISECONDS);
182 u32 s = readl_relaxed(reg_base + RTC_SHADOW_SECONDS);
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300183
Colin Cross09361782010-11-28 16:26:19 -0800184 return (u64)s * MSEC_PER_SEC + ms;
185}
186
Joseph Lo95170f02019-04-02 11:02:34 +0800187static struct clocksource suspend_rtc_clocksource = {
188 .name = "tegra_suspend_timer",
189 .rating = 200,
190 .read = tegra_rtc_read_ms,
191 .mask = CLOCKSOURCE_MASK(32),
192 .flags = CLOCK_SOURCE_IS_CONTINUOUS | CLOCK_SOURCE_SUSPEND_NONSTOP,
193};
Xunlei Panga0c29982015-04-01 20:34:25 -0700194
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300195static inline unsigned int tegra_base_for_cpu(int cpu, bool tegra20)
196{
197 if (tegra20) {
198 switch (cpu) {
199 case 0:
200 return TIMER1_BASE;
201 case 1:
202 return TIMER2_BASE;
203 case 2:
204 return TIMER3_BASE;
205 default:
206 return TIMER4_BASE;
207 }
208 }
209
210 return TIMER10_BASE + cpu * 8;
211}
212
213static inline unsigned int tegra_irq_idx_for_cpu(int cpu, bool tegra20)
214{
215 if (tegra20)
216 return TIMER1_IRQ_IDX + cpu;
217
218 return TIMER10_IRQ_IDX + cpu;
219}
220
Dmitry Osipenko99311d02019-06-18 17:03:52 +0300221static inline unsigned long tegra_rate_for_timer(struct timer_of *to,
222 bool tegra20)
223{
224 /*
225 * TIMER1-9 are fixed to 1MHz, TIMER10-13 are running off the
226 * parent clock.
227 */
228 if (tegra20)
229 return 1000000;
230
231 return timer_of_rate(to);
232}
233
Dmitry Osipenko87bd4c22019-06-03 21:59:47 +0300234static int __init tegra_init_timer(struct device_node *np, bool tegra20,
235 int rating)
Peter De Schrijver0ff36b42014-06-12 18:58:29 +0300236{
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300237 struct timer_of *to;
238 int cpu, ret;
Peter De Schrijver0ff36b42014-06-12 18:58:29 +0300239
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300240 to = this_cpu_ptr(&tegra_to);
Joseph Lob4822dc2019-02-21 15:21:44 +0800241 ret = timer_of_init(np, to);
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300242 if (ret)
Joseph Lob4822dc2019-02-21 15:21:44 +0800243 goto out;
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800244
Joseph Lob4822dc2019-02-21 15:21:44 +0800245 timer_reg_base = timer_of_base(to);
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800246
Joseph Lob4822dc2019-02-21 15:21:44 +0800247 /*
248 * Configure microsecond timers to have 1MHz clock
249 * Config register is 0xqqww, where qq is "dividend", ww is "divisor"
250 * Uses n+1 scheme
251 */
252 switch (timer_of_rate(to)) {
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800253 case 12000000:
Joseph Lob4822dc2019-02-21 15:21:44 +0800254 usec_config = 0x000b; /* (11+1)/(0+1) */
255 break;
256 case 12800000:
257 usec_config = 0x043f; /* (63+1)/(4+1) */
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800258 break;
259 case 13000000:
Joseph Lob4822dc2019-02-21 15:21:44 +0800260 usec_config = 0x000c; /* (12+1)/(0+1) */
261 break;
262 case 16800000:
263 usec_config = 0x0453; /* (83+1)/(4+1) */
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800264 break;
265 case 19200000:
Joseph Lob4822dc2019-02-21 15:21:44 +0800266 usec_config = 0x045f; /* (95+1)/(4+1) */
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800267 break;
268 case 26000000:
Joseph Lob4822dc2019-02-21 15:21:44 +0800269 usec_config = 0x0019; /* (25+1)/(0+1) */
270 break;
271 case 38400000:
272 usec_config = 0x04bf; /* (191+1)/(4+1) */
273 break;
274 case 48000000:
275 usec_config = 0x002f; /* (47+1)/(0+1) */
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800276 break;
277 default:
Joseph Lob4822dc2019-02-21 15:21:44 +0800278 ret = -EINVAL;
279 goto out;
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800280 }
281
Dmitry Osipenko6b349c362019-06-03 21:59:42 +0300282 writel_relaxed(usec_config, timer_reg_base + TIMERUS_USEC_CFG);
Joseph Lob4822dc2019-02-21 15:21:44 +0800283
284 for_each_possible_cpu(cpu) {
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300285 struct timer_of *cpu_to = per_cpu_ptr(&tegra_to, cpu);
Dmitry Osipenko99311d02019-06-18 17:03:52 +0300286 unsigned long flags = IRQF_TIMER | IRQF_NOBALANCING;
287 unsigned long rate = tegra_rate_for_timer(to, tegra20);
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300288 unsigned int base = tegra_base_for_cpu(cpu, tegra20);
289 unsigned int idx = tegra_irq_idx_for_cpu(cpu, tegra20);
Dmitry Osipenko99311d02019-06-18 17:03:52 +0300290 unsigned int irq = irq_of_parse_and_map(np, idx);
Joseph Lob4822dc2019-02-21 15:21:44 +0800291
Dmitry Osipenko99311d02019-06-18 17:03:52 +0300292 if (!irq) {
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300293 pr_err("failed to map irq for cpu%d\n", cpu);
Joseph Lob4822dc2019-02-21 15:21:44 +0800294 ret = -EINVAL;
Dmitry Osipenko7a391672019-06-03 21:59:43 +0300295 goto out_irq;
Joseph Lob4822dc2019-02-21 15:21:44 +0800296 }
297
Dmitry Osipenko99311d02019-06-18 17:03:52 +0300298 cpu_to->clkevt.irq = irq;
299 cpu_to->clkevt.rating = rating;
300 cpu_to->clkevt.cpumask = cpumask_of(cpu);
301 cpu_to->of_base.base = timer_reg_base + base;
Dmitry Osipenko09b25072019-06-18 17:03:53 +0300302 cpu_to->of_clk.period = rate / HZ;
Dmitry Osipenko99311d02019-06-18 17:03:52 +0300303 cpu_to->of_clk.rate = rate;
304
Joseph Lob4822dc2019-02-21 15:21:44 +0800305 irq_set_status_flags(cpu_to->clkevt.irq, IRQ_NOAUTOEN);
Dmitry Osipenko99311d02019-06-18 17:03:52 +0300306
307 ret = request_irq(cpu_to->clkevt.irq, tegra_timer_isr, flags,
Joseph Lob4822dc2019-02-21 15:21:44 +0800308 cpu_to->clkevt.name, &cpu_to->clkevt);
309 if (ret) {
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300310 pr_err("failed to set up irq for cpu%d: %d\n",
311 cpu, ret);
Dmitry Osipenko7a391672019-06-03 21:59:43 +0300312 irq_dispose_mapping(cpu_to->clkevt.irq);
313 cpu_to->clkevt.irq = 0;
Joseph Lob4822dc2019-02-21 15:21:44 +0800314 goto out_irq;
315 }
316 }
317
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300318 sched_clock_register(tegra_read_sched_clock, 32, 1000000);
319
320 ret = clocksource_mmio_init(timer_reg_base + TIMERUS_CNTR_1US,
321 "timer_us", 1000000,
322 300, 32, clocksource_mmio_readl_up);
323 if (ret)
324 pr_err("failed to register clocksource: %d\n", ret);
325
326#ifdef CONFIG_ARM
327 register_current_timer_delay(&tegra_delay_timer);
328#endif
329
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300330 ret = cpuhp_setup_state(CPUHP_AP_TEGRA_TIMER_STARTING,
331 "AP_TEGRA_TIMER_STARTING", tegra_timer_setup,
332 tegra_timer_stop);
333 if (ret)
334 pr_err("failed to set up cpu hp state: %d\n", ret);
Joseph Lob4822dc2019-02-21 15:21:44 +0800335
336 return ret;
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300337
Joseph Lob4822dc2019-02-21 15:21:44 +0800338out_irq:
339 for_each_possible_cpu(cpu) {
340 struct timer_of *cpu_to;
341
342 cpu_to = per_cpu_ptr(&tegra_to, cpu);
343 if (cpu_to->clkevt.irq) {
344 free_irq(cpu_to->clkevt.irq, &cpu_to->clkevt);
345 irq_dispose_mapping(cpu_to->clkevt.irq);
346 }
347 }
348out:
349 timer_of_cleanup(to);
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300350
Joseph Lob4822dc2019-02-21 15:21:44 +0800351 return ret;
352}
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300353
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300354static int __init tegra210_init_timer(struct device_node *np)
Joseph Lob4822dc2019-02-21 15:21:44 +0800355{
Dmitry Osipenko87bd4c22019-06-03 21:59:47 +0300356 /*
357 * Arch-timer can't survive across power cycle of CPU core and
358 * after CPUPORESET signal due to a system design shortcoming,
359 * hence tegra-timer is more preferable on Tegra210.
360 */
361 return tegra_init_timer(np, false, 460);
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300362}
363TIMER_OF_DECLARE(tegra210_timer, "nvidia,tegra210-timer", tegra210_init_timer);
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300364
Dmitry Osipenkof6d50ec2019-06-03 21:59:39 +0300365static int __init tegra20_init_timer(struct device_node *np)
366{
Dmitry Osipenko87bd4c22019-06-03 21:59:47 +0300367 int rating;
368
369 /*
370 * Tegra20 and Tegra30 have Cortex A9 CPU that has a TWD timer,
371 * that timer runs off the CPU clock and hence is subjected to
372 * a jitter caused by DVFS clock rate changes. Tegra-timer is
373 * more preferable for older Tegra's, while later SoC generations
374 * have arch-timer as a main per-CPU timer and it is not affected
375 * by DVFS changes.
376 */
377 if (of_machine_is_compatible("nvidia,tegra20") ||
378 of_machine_is_compatible("nvidia,tegra30"))
379 rating = 460;
380 else
381 rating = 330;
382
383 return tegra_init_timer(np, true, rating);
Daniel Lezcano53978bb2016-06-06 17:59:43 +0200384}
Dmitry Osipenkoaf8d9122019-06-03 21:59:40 +0300385TIMER_OF_DECLARE(tegra20_timer, "nvidia,tegra20-timer", tegra20_init_timer);
Daniel Lezcano53978bb2016-06-06 17:59:43 +0200386
387static int __init tegra20_init_rtc(struct device_node *np)
Rob Herring1d16cfb2013-02-07 11:36:23 -0600388{
Joseph Lo95170f02019-04-02 11:02:34 +0800389 int ret;
Rob Herring1d16cfb2013-02-07 11:36:23 -0600390
Joseph Lo95170f02019-04-02 11:02:34 +0800391 ret = timer_of_init(np, &suspend_rtc_to);
392 if (ret)
393 return ret;
Rob Herring1d16cfb2013-02-07 11:36:23 -0600394
Dmitry Osipenko49a678b2019-06-03 21:59:44 +0300395 return clocksource_register_hz(&suspend_rtc_clocksource, 1000);
Colin Cross2d5cd9a2010-01-28 16:41:42 -0800396}
Daniel Lezcano17273392017-05-26 16:56:11 +0200397TIMER_OF_DECLARE(tegra20_rtc, "nvidia,tegra20-rtc", tegra20_init_rtc);