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 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 49 | static u32 usec_config; |
Stephen Warren | 3a04931 | 2012-10-23 11:40:25 -0600 | [diff] [blame] | 50 | static void __iomem *timer_reg_base; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 51 | |
| 52 | static int tegra_timer_set_next_event(unsigned long cycles, |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame^] | 53 | struct clock_event_device *evt) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 54 | { |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 55 | void __iomem *reg_base = timer_of_base(to_timer_of(evt)); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 56 | |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 57 | writel_relaxed(TIMER_PTV_EN | |
| 58 | ((cycles > 1) ? (cycles - 1) : 0), /* n+1 scheme */ |
| 59 | reg_base + TIMER_PTV); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Viresh Kumar | 4134d29 | 2015-07-03 14:24:35 +0530 | [diff] [blame] | 64 | static int tegra_timer_shutdown(struct clock_event_device *evt) |
| 65 | { |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 66 | void __iomem *reg_base = timer_of_base(to_timer_of(evt)); |
| 67 | |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 68 | writel_relaxed(0, reg_base + TIMER_PTV); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 69 | |
Viresh Kumar | 4134d29 | 2015-07-03 14:24:35 +0530 | [diff] [blame] | 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int tegra_timer_set_periodic(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)); |
Viresh Kumar | 4134d29 | 2015-07-03 14:24:35 +0530 | [diff] [blame] | 76 | |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 77 | writel_relaxed(TIMER_PTV_EN | TIMER_PTV_PER | |
| 78 | ((timer_of_rate(to_timer_of(evt)) / HZ) - 1), |
| 79 | reg_base + TIMER_PTV); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 80 | |
Viresh Kumar | 4134d29 | 2015-07-03 14:24:35 +0530 | [diff] [blame] | 81 | return 0; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 84 | static 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 Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 89 | writel_relaxed(TIMER_PCR_INTR_CLR, reg_base + TIMER_PCR); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 90 | evt->event_handler(evt); |
| 91 | |
| 92 | return IRQ_HANDLED; |
| 93 | } |
| 94 | |
| 95 | static void tegra_timer_suspend(struct clock_event_device *evt) |
| 96 | { |
| 97 | void __iomem *reg_base = timer_of_base(to_timer_of(evt)); |
| 98 | |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 99 | writel_relaxed(TIMER_PCR_INTR_CLR, reg_base + TIMER_PCR); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void tegra_timer_resume(struct clock_event_device *evt) |
| 103 | { |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 104 | writel_relaxed(usec_config, timer_reg_base + TIMERUS_USEC_CFG); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 105 | } |
| 106 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 107 | static 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 | |
| 124 | static int tegra_timer_setup(unsigned int cpu) |
| 125 | { |
| 126 | struct timer_of *to = per_cpu_ptr(&tegra_to, cpu); |
| 127 | |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 128 | writel_relaxed(0, timer_of_base(to) + TIMER_PTV); |
| 129 | writel_relaxed(TIMER_PCR_INTR_CLR, timer_of_base(to) + TIMER_PCR); |
Dmitry Osipenko | 77d57d1 | 2019-06-03 21:59:41 +0300 | [diff] [blame] | 130 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 131 | 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 | |
| 141 | static 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 Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 150 | |
Stephen Boyd | 3570299 | 2013-07-18 16:21:26 -0700 | [diff] [blame] | 151 | static u64 notrace tegra_read_sched_clock(void) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 152 | { |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 153 | return readl_relaxed(timer_reg_base + TIMERUS_CNTR_1US); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 154 | } |
| 155 | |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 156 | #ifdef CONFIG_ARM |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 157 | static unsigned long tegra_delay_timer_read_counter_long(void) |
| 158 | { |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 159 | return readl_relaxed(timer_reg_base + TIMERUS_CNTR_1US); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 162 | static struct delay_timer tegra_delay_timer = { |
| 163 | .read_current_timer = tegra_delay_timer_read_counter_long, |
| 164 | .freq = 1000000, |
| 165 | }; |
| 166 | #endif |
| 167 | |
Joseph Lo | 95170f0 | 2019-04-02 11:02:34 +0800 | [diff] [blame] | 168 | static struct timer_of suspend_rtc_to = { |
| 169 | .flags = TIMER_OF_BASE | TIMER_OF_CLOCK, |
| 170 | }; |
| 171 | |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 172 | /* |
| 173 | * tegra_rtc_read - Reads the Tegra RTC registers |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame^] | 174 | * Care must be taken that this function is not called while the |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 175 | * tegra_rtc driver could be executing to avoid race conditions |
| 176 | * on the RTC shadow register |
| 177 | */ |
Joseph Lo | 95170f0 | 2019-04-02 11:02:34 +0800 | [diff] [blame] | 178 | static u64 tegra_rtc_read_ms(struct clocksource *cs) |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 179 | { |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 180 | void __iomem *reg_base = timer_of_base(&suspend_rtc_to); |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame^] | 181 | |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 182 | u32 ms = readl_relaxed(reg_base + RTC_MILLISECONDS); |
| 183 | u32 s = readl_relaxed(reg_base + RTC_SHADOW_SECONDS); |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame^] | 184 | |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 185 | return (u64)s * MSEC_PER_SEC + ms; |
| 186 | } |
| 187 | |
Joseph Lo | 95170f0 | 2019-04-02 11:02:34 +0800 | [diff] [blame] | 188 | static 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 Pang | a0c2998 | 2015-04-01 20:34:25 -0700 | [diff] [blame] | 195 | |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 196 | static 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 | |
| 214 | static 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 | |
| 222 | static int __init tegra_init_timer(struct device_node *np, bool tegra20) |
Peter De Schrijver | 0ff36b4 | 2014-06-12 18:58:29 +0300 | [diff] [blame] | 223 | { |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 224 | struct timer_of *to; |
| 225 | int cpu, ret; |
Peter De Schrijver | 0ff36b4 | 2014-06-12 18:58:29 +0300 | [diff] [blame] | 226 | |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 227 | to = this_cpu_ptr(&tegra_to); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 228 | ret = timer_of_init(np, to); |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame^] | 229 | if (ret) |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 230 | goto out; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 231 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 232 | timer_reg_base = timer_of_base(to); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 233 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 234 | /* |
| 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 Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 240 | case 12000000: |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 241 | usec_config = 0x000b; /* (11+1)/(0+1) */ |
| 242 | break; |
| 243 | case 12800000: |
| 244 | usec_config = 0x043f; /* (63+1)/(4+1) */ |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 245 | break; |
| 246 | case 13000000: |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 247 | usec_config = 0x000c; /* (12+1)/(0+1) */ |
| 248 | break; |
| 249 | case 16800000: |
| 250 | usec_config = 0x0453; /* (83+1)/(4+1) */ |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 251 | break; |
| 252 | case 19200000: |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 253 | usec_config = 0x045f; /* (95+1)/(4+1) */ |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 254 | break; |
| 255 | case 26000000: |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 256 | 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 Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 263 | break; |
| 264 | default: |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 265 | ret = -EINVAL; |
| 266 | goto out; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Dmitry Osipenko | 6b349c36 | 2019-06-03 21:59:42 +0300 | [diff] [blame] | 269 | writel_relaxed(usec_config, timer_reg_base + TIMERUS_USEC_CFG); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 270 | |
| 271 | for_each_possible_cpu(cpu) { |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 272 | struct timer_of *cpu_to = per_cpu_ptr(&tegra_to, cpu); |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 273 | unsigned int base = tegra_base_for_cpu(cpu, tegra20); |
| 274 | unsigned int idx = tegra_irq_idx_for_cpu(cpu, tegra20); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 275 | |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 276 | /* |
| 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 Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 283 | cpu_to = per_cpu_ptr(&tegra_to, cpu); |
| 284 | cpu_to->of_base.base = timer_reg_base + base; |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 285 | cpu_to->clkevt.cpumask = cpumask_of(cpu); |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 286 | cpu_to->clkevt.irq = irq_of_parse_and_map(np, idx); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 287 | if (!cpu_to->clkevt.irq) { |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame^] | 288 | pr_err("failed to map irq for cpu%d\n", cpu); |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 289 | ret = -EINVAL; |
Dmitry Osipenko | 7a39167 | 2019-06-03 21:59:43 +0300 | [diff] [blame] | 290 | goto out_irq; |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 291 | } |
| 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 Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame^] | 298 | pr_err("failed to set up irq for cpu%d: %d\n", |
| 299 | cpu, ret); |
Dmitry Osipenko | 7a39167 | 2019-06-03 21:59:43 +0300 | [diff] [blame] | 300 | irq_dispose_mapping(cpu_to->clkevt.irq); |
| 301 | cpu_to->clkevt.irq = 0; |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 302 | goto out_irq; |
| 303 | } |
| 304 | } |
| 305 | |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 306 | 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 Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame^] | 318 | 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 Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 323 | |
| 324 | return ret; |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame^] | 325 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 326 | out_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 | } |
| 336 | out: |
| 337 | timer_of_cleanup(to); |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame^] | 338 | |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 339 | return ret; |
| 340 | } |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 341 | |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 342 | static int __init tegra210_init_timer(struct device_node *np) |
Joseph Lo | b4822dc | 2019-02-21 15:21:44 +0800 | [diff] [blame] | 343 | { |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 344 | return tegra_init_timer(np, false); |
| 345 | } |
| 346 | TIMER_OF_DECLARE(tegra210_timer, "nvidia,tegra210-timer", tegra210_init_timer); |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 347 | |
Dmitry Osipenko | f6d50ec | 2019-06-03 21:59:39 +0300 | [diff] [blame] | 348 | static int __init tegra20_init_timer(struct device_node *np) |
| 349 | { |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 350 | return tegra_init_timer(np, true); |
Daniel Lezcano | 53978bb | 2016-06-06 17:59:43 +0200 | [diff] [blame] | 351 | } |
Dmitry Osipenko | af8d912 | 2019-06-03 21:59:40 +0300 | [diff] [blame] | 352 | TIMER_OF_DECLARE(tegra20_timer, "nvidia,tegra20-timer", tegra20_init_timer); |
Daniel Lezcano | 53978bb | 2016-06-06 17:59:43 +0200 | [diff] [blame] | 353 | |
| 354 | static int __init tegra20_init_rtc(struct device_node *np) |
Rob Herring | 1d16cfb | 2013-02-07 11:36:23 -0600 | [diff] [blame] | 355 | { |
Joseph Lo | 95170f0 | 2019-04-02 11:02:34 +0800 | [diff] [blame] | 356 | int ret; |
Rob Herring | 1d16cfb | 2013-02-07 11:36:23 -0600 | [diff] [blame] | 357 | |
Joseph Lo | 95170f0 | 2019-04-02 11:02:34 +0800 | [diff] [blame] | 358 | ret = timer_of_init(np, &suspend_rtc_to); |
| 359 | if (ret) |
| 360 | return ret; |
Rob Herring | 1d16cfb | 2013-02-07 11:36:23 -0600 | [diff] [blame] | 361 | |
Dmitry Osipenko | 49a678b | 2019-06-03 21:59:44 +0300 | [diff] [blame^] | 362 | return clocksource_register_hz(&suspend_rtc_clocksource, 1000); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 363 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 364 | TIMER_OF_DECLARE(tegra20_rtc, "nvidia,tegra20-rtc", tegra20_init_rtc); |