Thomas Gleixner | 9c92ab6 | 2019-05-29 07:17:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 2 | /* |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 3 | * Copyright (C) 2010 Google, Inc. |
| 4 | * |
| 5 | * Author: |
| 6 | * Colin Cross <ccross@google.com> |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 7 | */ |
| 8 | |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame] | 9 | #define pr_fmt(fmt) "tegra-timer: " fmt |
| 10 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 11 | #include <linux/clk.h> |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 12 | #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 Warren | 3a04931 | 2012-10-23 11:40:25 -0600 | [diff] [blame] | 18 | #include <linux/of_address.h> |
Stephen Warren | 5641548 | 2012-09-19 13:13:33 -0600 | [diff] [blame] | 19 | #include <linux/of_irq.h> |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 20 | #include <linux/percpu.h> |
Stephen Boyd | 38ff87f | 2013-06-01 23:39:40 -0700 | [diff] [blame] | 21 | #include <linux/sched_clock.h> |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 22 | #include <linux/time.h> |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 23 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 24 | #include "timer-of.h" |
| 25 | |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame] | 26 | #define RTC_SECONDS 0x08 |
| 27 | #define RTC_SHADOW_SECONDS 0x0c |
| 28 | #define RTC_MILLISECONDS 0x10 |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 29 | |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame] | 30 | #define TIMERUS_CNTR_1US 0x10 |
| 31 | #define TIMERUS_USEC_CFG 0x14 |
| 32 | #define TIMERUS_CNTR_FREEZE 0x4c |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 33 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 34 | #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 Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 39 | |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 40 | #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 Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 46 | #define TIMER1_IRQ_IDX 0 |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 47 | #define TIMER10_IRQ_IDX 10 |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 48 | |
Dmitry Osipenko | 2e08a4b | 2019-06-18 17:03:55 +0300 | [diff] [blame] | 49 | #define TIMER_1MHz 1000000 |
| 50 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 51 | static u32 usec_config; |
Stephen Warren | 3a04931 | 2012-10-23 11:40:25 -0600 | [diff] [blame] | 52 | static void __iomem *timer_reg_base; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 53 | |
| 54 | static int tegra_timer_set_next_event(unsigned long cycles, |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame] | 55 | struct clock_event_device *evt) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 56 | { |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 57 | void __iomem *reg_base = timer_of_base(to_timer_of(evt)); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 58 | |
Dmitry Osipenko | 0ef6b01 | 2019-06-18 17:03:57 +0300 | [diff] [blame] | 59 | /* |
| 60 | * Tegra's timer uses n+1 scheme for the counter, i.e. timer will |
| 61 | * fire after one tick if 0 is loaded. |
| 62 | * |
| 63 | * The minimum and maximum numbers of oneshot ticks are defined |
| 64 | * by clockevents_config_and_register(1, 0x1fffffff + 1) invocation |
| 65 | * below in the code. Hence the cycles (ticks) can't be outside of |
| 66 | * a range supportable by hardware. |
| 67 | */ |
| 68 | writel_relaxed(TIMER_PTV_EN | (cycles - 1), reg_base + TIMER_PTV); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
Viresh Kumar | 4134d29 | 2015-07-03 14:24:35 +0530 | [diff] [blame] | 73 | static int tegra_timer_shutdown(struct clock_event_device *evt) |
| 74 | { |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 75 | void __iomem *reg_base = timer_of_base(to_timer_of(evt)); |
| 76 | |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 77 | writel_relaxed(0, reg_base + TIMER_PTV); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 78 | |
Viresh Kumar | 4134d29 | 2015-07-03 14:24:35 +0530 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static int tegra_timer_set_periodic(struct clock_event_device *evt) |
| 83 | { |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 84 | void __iomem *reg_base = timer_of_base(to_timer_of(evt)); |
Dmitry Osipenko | 09b2507 | 2019-06-18 17:03:53 +0300 | [diff] [blame] | 85 | unsigned long period = timer_of_period(to_timer_of(evt)); |
Viresh Kumar | 4134d29 | 2015-07-03 14:24:35 +0530 | [diff] [blame] | 86 | |
Dmitry Osipenko | 09b2507 | 2019-06-18 17:03:53 +0300 | [diff] [blame] | 87 | writel_relaxed(TIMER_PTV_EN | TIMER_PTV_PER | (period - 1), |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 88 | reg_base + TIMER_PTV); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 89 | |
Viresh Kumar | 4134d29 | 2015-07-03 14:24:35 +0530 | [diff] [blame] | 90 | return 0; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 93 | static irqreturn_t tegra_timer_isr(int irq, void *dev_id) |
| 94 | { |
Dmitry Osipenko | 7c708fd | 2019-06-18 17:03:54 +0300 | [diff] [blame] | 95 | struct clock_event_device *evt = dev_id; |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 96 | void __iomem *reg_base = timer_of_base(to_timer_of(evt)); |
| 97 | |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 98 | writel_relaxed(TIMER_PCR_INTR_CLR, reg_base + TIMER_PCR); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 99 | evt->event_handler(evt); |
| 100 | |
| 101 | return IRQ_HANDLED; |
| 102 | } |
| 103 | |
| 104 | static void tegra_timer_suspend(struct clock_event_device *evt) |
| 105 | { |
| 106 | void __iomem *reg_base = timer_of_base(to_timer_of(evt)); |
| 107 | |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 108 | writel_relaxed(TIMER_PCR_INTR_CLR, reg_base + TIMER_PCR); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static void tegra_timer_resume(struct clock_event_device *evt) |
| 112 | { |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 113 | writel_relaxed(usec_config, timer_reg_base + TIMERUS_USEC_CFG); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 114 | } |
| 115 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 116 | static DEFINE_PER_CPU(struct timer_of, tegra_to) = { |
| 117 | .flags = TIMER_OF_CLOCK | TIMER_OF_BASE, |
| 118 | |
| 119 | .clkevt = { |
| 120 | .name = "tegra_timer", |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 121 | .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC, |
| 122 | .set_next_event = tegra_timer_set_next_event, |
| 123 | .set_state_shutdown = tegra_timer_shutdown, |
| 124 | .set_state_periodic = tegra_timer_set_periodic, |
| 125 | .set_state_oneshot = tegra_timer_shutdown, |
| 126 | .tick_resume = tegra_timer_shutdown, |
| 127 | .suspend = tegra_timer_suspend, |
| 128 | .resume = tegra_timer_resume, |
| 129 | }, |
| 130 | }; |
| 131 | |
| 132 | static int tegra_timer_setup(unsigned int cpu) |
| 133 | { |
| 134 | struct timer_of *to = per_cpu_ptr(&tegra_to, cpu); |
| 135 | |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 136 | writel_relaxed(0, timer_of_base(to) + TIMER_PTV); |
| 137 | writel_relaxed(TIMER_PCR_INTR_CLR, timer_of_base(to) + TIMER_PCR); |
Dmitry Osipenko | 77d57d1 | 2019-06-03 21:59:41 +0300 | [diff] [blame] | 138 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 139 | irq_force_affinity(to->clkevt.irq, cpumask_of(cpu)); |
| 140 | enable_irq(to->clkevt.irq); |
| 141 | |
Dmitry Osipenko | 6fde389 | 2019-06-18 17:03:58 +0300 | [diff] [blame] | 142 | /* |
| 143 | * Tegra's timer uses n+1 scheme for the counter, i.e. timer will |
| 144 | * fire after one tick if 0 is loaded and thus minimum number of |
| 145 | * ticks is 1. In result both of the clocksource's tick limits are |
| 146 | * higher than a minimum and maximum that hardware register can |
| 147 | * take by 1, this is then taken into account by set_next_event |
| 148 | * callback. |
| 149 | */ |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 150 | clockevents_config_and_register(&to->clkevt, timer_of_rate(to), |
| 151 | 1, /* min */ |
Dmitry Osipenko | 6fde389 | 2019-06-18 17:03:58 +0300 | [diff] [blame] | 152 | 0x1fffffff + 1); /* max 29 bits + 1 */ |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static int tegra_timer_stop(unsigned int cpu) |
| 158 | { |
| 159 | struct timer_of *to = per_cpu_ptr(&tegra_to, cpu); |
| 160 | |
| 161 | to->clkevt.set_state_shutdown(&to->clkevt); |
| 162 | disable_irq_nosync(to->clkevt.irq); |
| 163 | |
| 164 | return 0; |
| 165 | } |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 166 | |
Stephen Boyd | 3570299 | 2013-07-18 16:21:26 -0700 | [diff] [blame] | 167 | static u64 notrace tegra_read_sched_clock(void) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 168 | { |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 169 | return readl_relaxed(timer_reg_base + TIMERUS_CNTR_1US); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 170 | } |
| 171 | |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 172 | #ifdef CONFIG_ARM |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 173 | static unsigned long tegra_delay_timer_read_counter_long(void) |
| 174 | { |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 175 | return readl_relaxed(timer_reg_base + TIMERUS_CNTR_1US); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 178 | static struct delay_timer tegra_delay_timer = { |
| 179 | .read_current_timer = tegra_delay_timer_read_counter_long, |
Dmitry Osipenko | 2e08a4b | 2019-06-18 17:03:55 +0300 | [diff] [blame] | 180 | .freq = TIMER_1MHz, |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 181 | }; |
| 182 | #endif |
| 183 | |
Joseph Lo | 95170f0 | 2019-04-02 11:02:34 +0800 | [diff] [blame] | 184 | static struct timer_of suspend_rtc_to = { |
| 185 | .flags = TIMER_OF_BASE | TIMER_OF_CLOCK, |
| 186 | }; |
| 187 | |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 188 | /* |
| 189 | * tegra_rtc_read - Reads the Tegra RTC registers |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame] | 190 | * Care must be taken that this function is not called while the |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 191 | * tegra_rtc driver could be executing to avoid race conditions |
| 192 | * on the RTC shadow register |
| 193 | */ |
Joseph Lo | 95170f0 | 2019-04-02 11:02:34 +0800 | [diff] [blame] | 194 | static u64 tegra_rtc_read_ms(struct clocksource *cs) |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 195 | { |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 196 | void __iomem *reg_base = timer_of_base(&suspend_rtc_to); |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame] | 197 | |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 198 | u32 ms = readl_relaxed(reg_base + RTC_MILLISECONDS); |
| 199 | u32 s = readl_relaxed(reg_base + RTC_SHADOW_SECONDS); |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame] | 200 | |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 201 | return (u64)s * MSEC_PER_SEC + ms; |
| 202 | } |
| 203 | |
Joseph Lo | 95170f0 | 2019-04-02 11:02:34 +0800 | [diff] [blame] | 204 | static struct clocksource suspend_rtc_clocksource = { |
| 205 | .name = "tegra_suspend_timer", |
| 206 | .rating = 200, |
| 207 | .read = tegra_rtc_read_ms, |
| 208 | .mask = CLOCKSOURCE_MASK(32), |
| 209 | .flags = CLOCK_SOURCE_IS_CONTINUOUS | CLOCK_SOURCE_SUSPEND_NONSTOP, |
| 210 | }; |
Xunlei Pang | a0c2998 | 2015-04-01 20:34:25 -0700 | [diff] [blame] | 211 | |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 212 | static inline unsigned int tegra_base_for_cpu(int cpu, bool tegra20) |
| 213 | { |
| 214 | if (tegra20) { |
| 215 | switch (cpu) { |
| 216 | case 0: |
| 217 | return TIMER1_BASE; |
| 218 | case 1: |
| 219 | return TIMER2_BASE; |
| 220 | case 2: |
| 221 | return TIMER3_BASE; |
| 222 | default: |
| 223 | return TIMER4_BASE; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return TIMER10_BASE + cpu * 8; |
| 228 | } |
| 229 | |
| 230 | static inline unsigned int tegra_irq_idx_for_cpu(int cpu, bool tegra20) |
| 231 | { |
| 232 | if (tegra20) |
| 233 | return TIMER1_IRQ_IDX + cpu; |
| 234 | |
| 235 | return TIMER10_IRQ_IDX + cpu; |
| 236 | } |
| 237 | |
Dmitry Osipenko | 99311d0 | 2019-06-18 17:03:52 +0300 | [diff] [blame] | 238 | static inline unsigned long tegra_rate_for_timer(struct timer_of *to, |
| 239 | bool tegra20) |
| 240 | { |
| 241 | /* |
| 242 | * TIMER1-9 are fixed to 1MHz, TIMER10-13 are running off the |
| 243 | * parent clock. |
| 244 | */ |
| 245 | if (tegra20) |
Dmitry Osipenko | 2e08a4b | 2019-06-18 17:03:55 +0300 | [diff] [blame] | 246 | return TIMER_1MHz; |
Dmitry Osipenko | 99311d0 | 2019-06-18 17:03:52 +0300 | [diff] [blame] | 247 | |
| 248 | return timer_of_rate(to); |
| 249 | } |
| 250 | |
Dmitry Osipenko | 87bd4c2 | 2019-06-03 21:59:47 +0300 | [diff] [blame] | 251 | static int __init tegra_init_timer(struct device_node *np, bool tegra20, |
| 252 | int rating) |
Peter De Schrijver | 0ff36b4 | 2014-06-12 18:58:29 +0300 | [diff] [blame] | 253 | { |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 254 | struct timer_of *to; |
| 255 | int cpu, ret; |
Peter De Schrijver | 0ff36b4 | 2014-06-12 18:58:29 +0300 | [diff] [blame] | 256 | |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 257 | to = this_cpu_ptr(&tegra_to); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 258 | ret = timer_of_init(np, to); |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame] | 259 | if (ret) |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 260 | goto out; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 261 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 262 | timer_reg_base = timer_of_base(to); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 263 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 264 | /* |
| 265 | * Configure microsecond timers to have 1MHz clock |
| 266 | * Config register is 0xqqww, where qq is "dividend", ww is "divisor" |
| 267 | * Uses n+1 scheme |
| 268 | */ |
| 269 | switch (timer_of_rate(to)) { |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 270 | case 12000000: |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 271 | usec_config = 0x000b; /* (11+1)/(0+1) */ |
| 272 | break; |
| 273 | case 12800000: |
| 274 | usec_config = 0x043f; /* (63+1)/(4+1) */ |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 275 | break; |
| 276 | case 13000000: |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 277 | usec_config = 0x000c; /* (12+1)/(0+1) */ |
| 278 | break; |
| 279 | case 16800000: |
| 280 | usec_config = 0x0453; /* (83+1)/(4+1) */ |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 281 | break; |
| 282 | case 19200000: |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 283 | usec_config = 0x045f; /* (95+1)/(4+1) */ |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 284 | break; |
| 285 | case 26000000: |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 286 | usec_config = 0x0019; /* (25+1)/(0+1) */ |
| 287 | break; |
| 288 | case 38400000: |
| 289 | usec_config = 0x04bf; /* (191+1)/(4+1) */ |
| 290 | break; |
| 291 | case 48000000: |
| 292 | usec_config = 0x002f; /* (47+1)/(0+1) */ |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 293 | break; |
| 294 | default: |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 295 | ret = -EINVAL; |
| 296 | goto out; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 299 | writel_relaxed(usec_config, timer_reg_base + TIMERUS_USEC_CFG); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 300 | |
| 301 | for_each_possible_cpu(cpu) { |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 302 | struct timer_of *cpu_to = per_cpu_ptr(&tegra_to, cpu); |
Dmitry Osipenko | 99311d0 | 2019-06-18 17:03:52 +0300 | [diff] [blame] | 303 | unsigned long flags = IRQF_TIMER | IRQF_NOBALANCING; |
| 304 | unsigned long rate = tegra_rate_for_timer(to, tegra20); |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 305 | unsigned int base = tegra_base_for_cpu(cpu, tegra20); |
| 306 | unsigned int idx = tegra_irq_idx_for_cpu(cpu, tegra20); |
Dmitry Osipenko | 99311d0 | 2019-06-18 17:03:52 +0300 | [diff] [blame] | 307 | unsigned int irq = irq_of_parse_and_map(np, idx); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 308 | |
Dmitry Osipenko | 99311d0 | 2019-06-18 17:03:52 +0300 | [diff] [blame] | 309 | if (!irq) { |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame] | 310 | pr_err("failed to map irq for cpu%d\n", cpu); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 311 | ret = -EINVAL; |
Dmitry Osipenko | 7a39167 | 2019-06-03 21:59:43 +0300 | [diff] [blame] | 312 | goto out_irq; |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 313 | } |
| 314 | |
Dmitry Osipenko | 99311d0 | 2019-06-18 17:03:52 +0300 | [diff] [blame] | 315 | cpu_to->clkevt.irq = irq; |
| 316 | cpu_to->clkevt.rating = rating; |
| 317 | cpu_to->clkevt.cpumask = cpumask_of(cpu); |
| 318 | cpu_to->of_base.base = timer_reg_base + base; |
Dmitry Osipenko | 09b2507 | 2019-06-18 17:03:53 +0300 | [diff] [blame] | 319 | cpu_to->of_clk.period = rate / HZ; |
Dmitry Osipenko | 99311d0 | 2019-06-18 17:03:52 +0300 | [diff] [blame] | 320 | cpu_to->of_clk.rate = rate; |
| 321 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 322 | irq_set_status_flags(cpu_to->clkevt.irq, IRQ_NOAUTOEN); |
Dmitry Osipenko | 99311d0 | 2019-06-18 17:03:52 +0300 | [diff] [blame] | 323 | |
| 324 | ret = request_irq(cpu_to->clkevt.irq, tegra_timer_isr, flags, |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 325 | cpu_to->clkevt.name, &cpu_to->clkevt); |
| 326 | if (ret) { |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame] | 327 | pr_err("failed to set up irq for cpu%d: %d\n", |
| 328 | cpu, ret); |
Dmitry Osipenko | 7a39167 | 2019-06-03 21:59:43 +0300 | [diff] [blame] | 329 | irq_dispose_mapping(cpu_to->clkevt.irq); |
| 330 | cpu_to->clkevt.irq = 0; |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 331 | goto out_irq; |
| 332 | } |
| 333 | } |
| 334 | |
Dmitry Osipenko | 2e08a4b | 2019-06-18 17:03:55 +0300 | [diff] [blame] | 335 | sched_clock_register(tegra_read_sched_clock, 32, TIMER_1MHz); |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 336 | |
| 337 | ret = clocksource_mmio_init(timer_reg_base + TIMERUS_CNTR_1US, |
Dmitry Osipenko | 2e08a4b | 2019-06-18 17:03:55 +0300 | [diff] [blame] | 338 | "timer_us", TIMER_1MHz, 300, 32, |
| 339 | clocksource_mmio_readl_up); |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 340 | if (ret) |
| 341 | pr_err("failed to register clocksource: %d\n", ret); |
| 342 | |
| 343 | #ifdef CONFIG_ARM |
| 344 | register_current_timer_delay(&tegra_delay_timer); |
| 345 | #endif |
| 346 | |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame] | 347 | ret = cpuhp_setup_state(CPUHP_AP_TEGRA_TIMER_STARTING, |
| 348 | "AP_TEGRA_TIMER_STARTING", tegra_timer_setup, |
| 349 | tegra_timer_stop); |
| 350 | if (ret) |
| 351 | pr_err("failed to set up cpu hp state: %d\n", ret); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 352 | |
| 353 | return ret; |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame] | 354 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 355 | out_irq: |
| 356 | for_each_possible_cpu(cpu) { |
| 357 | struct timer_of *cpu_to; |
| 358 | |
| 359 | cpu_to = per_cpu_ptr(&tegra_to, cpu); |
| 360 | if (cpu_to->clkevt.irq) { |
| 361 | free_irq(cpu_to->clkevt.irq, &cpu_to->clkevt); |
| 362 | irq_dispose_mapping(cpu_to->clkevt.irq); |
| 363 | } |
| 364 | } |
Dmitry Osipenko | fc9babc | 2019-06-18 17:03:56 +0300 | [diff] [blame] | 365 | |
| 366 | to->of_base.base = timer_reg_base; |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 367 | out: |
| 368 | timer_of_cleanup(to); |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame] | 369 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 370 | return ret; |
| 371 | } |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 372 | |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 373 | static int __init tegra210_init_timer(struct device_node *np) |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 374 | { |
Dmitry Osipenko | 87bd4c2 | 2019-06-03 21:59:47 +0300 | [diff] [blame] | 375 | /* |
| 376 | * Arch-timer can't survive across power cycle of CPU core and |
| 377 | * after CPUPORESET signal due to a system design shortcoming, |
| 378 | * hence tegra-timer is more preferable on Tegra210. |
| 379 | */ |
| 380 | return tegra_init_timer(np, false, 460); |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 381 | } |
| 382 | TIMER_OF_DECLARE(tegra210_timer, "nvidia,tegra210-timer", tegra210_init_timer); |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 383 | |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 384 | static int __init tegra20_init_timer(struct device_node *np) |
| 385 | { |
Dmitry Osipenko | 87bd4c2 | 2019-06-03 21:59:47 +0300 | [diff] [blame] | 386 | int rating; |
| 387 | |
| 388 | /* |
| 389 | * Tegra20 and Tegra30 have Cortex A9 CPU that has a TWD timer, |
| 390 | * that timer runs off the CPU clock and hence is subjected to |
| 391 | * a jitter caused by DVFS clock rate changes. Tegra-timer is |
| 392 | * more preferable for older Tegra's, while later SoC generations |
| 393 | * have arch-timer as a main per-CPU timer and it is not affected |
| 394 | * by DVFS changes. |
| 395 | */ |
| 396 | if (of_machine_is_compatible("nvidia,tegra20") || |
| 397 | of_machine_is_compatible("nvidia,tegra30")) |
| 398 | rating = 460; |
| 399 | else |
| 400 | rating = 330; |
| 401 | |
| 402 | return tegra_init_timer(np, true, rating); |
Daniel Lezcano | 53978bb | 2016-06-06 17:59:43 +0200 | [diff] [blame] | 403 | } |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 404 | TIMER_OF_DECLARE(tegra20_timer, "nvidia,tegra20-timer", tegra20_init_timer); |
Daniel Lezcano | 53978bb | 2016-06-06 17:59:43 +0200 | [diff] [blame] | 405 | |
| 406 | static int __init tegra20_init_rtc(struct device_node *np) |
Rob Herring | 1d16cfb | 2013-02-07 11:36:23 -0600 | [diff] [blame] | 407 | { |
Joseph Lo | 95170f0 | 2019-04-02 11:02:34 +0800 | [diff] [blame] | 408 | int ret; |
Rob Herring | 1d16cfb | 2013-02-07 11:36:23 -0600 | [diff] [blame] | 409 | |
Joseph Lo | 95170f0 | 2019-04-02 11:02:34 +0800 | [diff] [blame] | 410 | ret = timer_of_init(np, &suspend_rtc_to); |
| 411 | if (ret) |
| 412 | return ret; |
Rob Herring | 1d16cfb | 2013-02-07 11:36:23 -0600 | [diff] [blame] | 413 | |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame] | 414 | return clocksource_register_hz(&suspend_rtc_clocksource, 1000); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 415 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 416 | TIMER_OF_DECLARE(tegra20_rtc, "nvidia,tegra20-rtc", tegra20_init_rtc); |