Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 1 | /* |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 2 | * Copyright (C) 2010 Google, Inc. |
| 3 | * |
| 4 | * Author: |
| 5 | * Colin Cross <ccross@google.com> |
| 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/init.h> |
Colin Cross | 62248ae | 2011-02-21 17:04:37 -0800 | [diff] [blame] | 19 | #include <linux/err.h> |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 20 | #include <linux/time.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/irq.h> |
| 23 | #include <linux/clockchips.h> |
| 24 | #include <linux/clocksource.h> |
| 25 | #include <linux/clk.h> |
| 26 | #include <linux/io.h> |
Stephen Warren | 3a04931 | 2012-10-23 11:40:25 -0600 | [diff] [blame] | 27 | #include <linux/of_address.h> |
Stephen Warren | 5641548 | 2012-09-19 13:13:33 -0600 | [diff] [blame] | 28 | #include <linux/of_irq.h> |
Stephen Boyd | 38ff87f | 2013-06-01 23:39:40 -0700 | [diff] [blame] | 29 | #include <linux/sched_clock.h> |
Peter De Schrijver | 0ff36b4 | 2014-06-12 18:58:29 +0300 | [diff] [blame] | 30 | #include <linux/delay.h> |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 31 | |
| 32 | #include <asm/mach/time.h> |
Marc Zyngier | 1fcf3a6 | 2012-01-10 19:44:19 +0000 | [diff] [blame] | 33 | #include <asm/smp_twd.h> |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 34 | |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 35 | #define RTC_SECONDS 0x08 |
| 36 | #define RTC_SHADOW_SECONDS 0x0c |
| 37 | #define RTC_MILLISECONDS 0x10 |
| 38 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 39 | #define TIMERUS_CNTR_1US 0x10 |
| 40 | #define TIMERUS_USEC_CFG 0x14 |
| 41 | #define TIMERUS_CNTR_FREEZE 0x4c |
| 42 | |
| 43 | #define TIMER1_BASE 0x0 |
| 44 | #define TIMER2_BASE 0x8 |
| 45 | #define TIMER3_BASE 0x50 |
| 46 | #define TIMER4_BASE 0x58 |
| 47 | |
| 48 | #define TIMER_PTV 0x0 |
| 49 | #define TIMER_PCR 0x4 |
| 50 | |
Stephen Warren | 3a04931 | 2012-10-23 11:40:25 -0600 | [diff] [blame] | 51 | static void __iomem *timer_reg_base; |
| 52 | static void __iomem *rtc_base; |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 53 | |
Xunlei Pang | a0c2998 | 2015-04-01 20:34:25 -0700 | [diff] [blame] | 54 | static struct timespec64 persistent_ts; |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 55 | static u64 persistent_ms, last_persistent_ms; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 56 | |
Peter De Schrijver | 0ff36b4 | 2014-06-12 18:58:29 +0300 | [diff] [blame] | 57 | static struct delay_timer tegra_delay_timer; |
| 58 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 59 | #define timer_writel(value, reg) \ |
Dmitry Osipenko | 59196bc | 2015-03-30 22:17:11 +0200 | [diff] [blame] | 60 | writel_relaxed(value, timer_reg_base + (reg)) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 61 | #define timer_readl(reg) \ |
Dmitry Osipenko | 59196bc | 2015-03-30 22:17:11 +0200 | [diff] [blame] | 62 | readl_relaxed(timer_reg_base + (reg)) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 63 | |
| 64 | static int tegra_timer_set_next_event(unsigned long cycles, |
| 65 | struct clock_event_device *evt) |
| 66 | { |
| 67 | u32 reg; |
| 68 | |
| 69 | reg = 0x80000000 | ((cycles > 1) ? (cycles-1) : 0); |
| 70 | timer_writel(reg, TIMER3_BASE + TIMER_PTV); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Viresh Kumar | 4134d29 | 2015-07-03 14:24:35 +0530 | [diff] [blame] | 75 | static inline void timer_shutdown(struct clock_event_device *evt) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 76 | { |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 77 | timer_writel(0, TIMER3_BASE + TIMER_PTV); |
Viresh Kumar | 4134d29 | 2015-07-03 14:24:35 +0530 | [diff] [blame] | 78 | } |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 79 | |
Viresh Kumar | 4134d29 | 2015-07-03 14:24:35 +0530 | [diff] [blame] | 80 | static int tegra_timer_shutdown(struct clock_event_device *evt) |
| 81 | { |
| 82 | timer_shutdown(evt); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int tegra_timer_set_periodic(struct clock_event_device *evt) |
| 87 | { |
| 88 | u32 reg = 0xC0000000 | ((1000000 / HZ) - 1); |
| 89 | |
| 90 | timer_shutdown(evt); |
| 91 | timer_writel(reg, TIMER3_BASE + TIMER_PTV); |
| 92 | return 0; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 95 | static struct clock_event_device tegra_clockevent = { |
Viresh Kumar | 4134d29 | 2015-07-03 14:24:35 +0530 | [diff] [blame] | 96 | .name = "timer0", |
| 97 | .rating = 300, |
| 98 | .features = CLOCK_EVT_FEAT_ONESHOT | |
| 99 | CLOCK_EVT_FEAT_PERIODIC, |
| 100 | .set_next_event = tegra_timer_set_next_event, |
| 101 | .set_state_shutdown = tegra_timer_shutdown, |
| 102 | .set_state_periodic = tegra_timer_set_periodic, |
| 103 | .set_state_oneshot = tegra_timer_shutdown, |
| 104 | .tick_resume = tegra_timer_shutdown, |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
Stephen Boyd | 3570299 | 2013-07-18 16:21:26 -0700 | [diff] [blame] | 107 | static u64 notrace tegra_read_sched_clock(void) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 108 | { |
Marc Zyngier | 2f0778af | 2011-12-15 12:19:23 +0100 | [diff] [blame] | 109 | return timer_readl(TIMERUS_CNTR_1US); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 112 | /* |
| 113 | * tegra_rtc_read - Reads the Tegra RTC registers |
| 114 | * Care must be taken that this funciton is not called while the |
| 115 | * tegra_rtc driver could be executing to avoid race conditions |
| 116 | * on the RTC shadow register |
| 117 | */ |
Olof Johansson | b28fba2 | 2011-09-08 17:50:03 -0700 | [diff] [blame] | 118 | static u64 tegra_rtc_read_ms(void) |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 119 | { |
| 120 | u32 ms = readl(rtc_base + RTC_MILLISECONDS); |
| 121 | u32 s = readl(rtc_base + RTC_SHADOW_SECONDS); |
| 122 | return (u64)s * MSEC_PER_SEC + ms; |
| 123 | } |
| 124 | |
| 125 | /* |
Xunlei Pang | a0c2998 | 2015-04-01 20:34:25 -0700 | [diff] [blame] | 126 | * tegra_read_persistent_clock64 - Return time from a persistent clock. |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 127 | * |
| 128 | * Reads the time from a source which isn't disabled during PM, the |
| 129 | * 32k sync timer. Convert the cycles elapsed since last read into |
Xunlei Pang | a0c2998 | 2015-04-01 20:34:25 -0700 | [diff] [blame] | 130 | * nsecs and adds to a monotonically increasing timespec64. |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 131 | * Care must be taken that this funciton is not called while the |
| 132 | * tegra_rtc driver could be executing to avoid race conditions |
| 133 | * on the RTC shadow register |
| 134 | */ |
Xunlei Pang | a0c2998 | 2015-04-01 20:34:25 -0700 | [diff] [blame] | 135 | static void tegra_read_persistent_clock64(struct timespec64 *ts) |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 136 | { |
| 137 | u64 delta; |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 138 | |
| 139 | last_persistent_ms = persistent_ms; |
| 140 | persistent_ms = tegra_rtc_read_ms(); |
| 141 | delta = persistent_ms - last_persistent_ms; |
| 142 | |
Xunlei Pang | a0c2998 | 2015-04-01 20:34:25 -0700 | [diff] [blame] | 143 | timespec64_add_ns(&persistent_ts, delta * NSEC_PER_MSEC); |
| 144 | *ts = persistent_ts; |
| 145 | } |
| 146 | |
Peter De Schrijver | 0ff36b4 | 2014-06-12 18:58:29 +0300 | [diff] [blame] | 147 | static unsigned long tegra_delay_timer_read_counter_long(void) |
| 148 | { |
| 149 | return readl(timer_reg_base + TIMERUS_CNTR_1US); |
| 150 | } |
| 151 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 152 | static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id) |
| 153 | { |
| 154 | struct clock_event_device *evt = (struct clock_event_device *)dev_id; |
| 155 | timer_writel(1<<30, TIMER3_BASE + TIMER_PCR); |
| 156 | evt->event_handler(evt); |
| 157 | return IRQ_HANDLED; |
| 158 | } |
| 159 | |
| 160 | static struct irqaction tegra_timer_irq = { |
| 161 | .name = "timer0", |
Michael Opdenacker | 39304fa | 2013-12-09 10:35:45 +0100 | [diff] [blame] | 162 | .flags = IRQF_TIMER | IRQF_TRIGGER_HIGH, |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 163 | .handler = tegra_timer_interrupt, |
| 164 | .dev_id = &tegra_clockevent, |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 165 | }; |
| 166 | |
Rob Herring | effbfdd | 2013-02-06 14:40:22 -0600 | [diff] [blame] | 167 | static void __init tegra20_init_timer(struct device_node *np) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 168 | { |
Colin Cross | 62248ae | 2011-02-21 17:04:37 -0800 | [diff] [blame] | 169 | struct clk *clk; |
Peter De Schrijver | 8e4fab2 | 2011-12-14 17:03:16 +0200 | [diff] [blame] | 170 | unsigned long rate; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 171 | int ret; |
| 172 | |
Stephen Warren | 3a04931 | 2012-10-23 11:40:25 -0600 | [diff] [blame] | 173 | timer_reg_base = of_iomap(np, 0); |
| 174 | if (!timer_reg_base) { |
Hiroshi Doyu | 3734086 | 2012-12-17 13:35:23 +0200 | [diff] [blame] | 175 | pr_err("Can't map timer registers\n"); |
Stephen Warren | 3a04931 | 2012-10-23 11:40:25 -0600 | [diff] [blame] | 176 | BUG(); |
| 177 | } |
| 178 | |
Stephen Warren | 5641548 | 2012-09-19 13:13:33 -0600 | [diff] [blame] | 179 | tegra_timer_irq.irq = irq_of_parse_and_map(np, 2); |
| 180 | if (tegra_timer_irq.irq <= 0) { |
| 181 | pr_err("Failed to map timer IRQ\n"); |
| 182 | BUG(); |
| 183 | } |
| 184 | |
Peter De Schrijver | 6f88fb8 | 2013-02-04 15:40:30 +0200 | [diff] [blame] | 185 | clk = of_clk_get(np, 0); |
Peter De Schrijver | 8e4fab2 | 2011-12-14 17:03:16 +0200 | [diff] [blame] | 186 | if (IS_ERR(clk)) { |
Stephen Warren | 58664f9 | 2012-10-23 12:21:39 -0600 | [diff] [blame] | 187 | pr_warn("Unable to get timer clock. Assuming 12Mhz input clock.\n"); |
Peter De Schrijver | 8e4fab2 | 2011-12-14 17:03:16 +0200 | [diff] [blame] | 188 | rate = 12000000; |
| 189 | } else { |
Prashant Gaikwad | 6a5278d | 2012-06-05 09:59:35 +0530 | [diff] [blame] | 190 | clk_prepare_enable(clk); |
Peter De Schrijver | 8e4fab2 | 2011-12-14 17:03:16 +0200 | [diff] [blame] | 191 | rate = clk_get_rate(clk); |
| 192 | } |
Colin Cross | 62248ae | 2011-02-21 17:04:37 -0800 | [diff] [blame] | 193 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 194 | switch (rate) { |
| 195 | case 12000000: |
| 196 | timer_writel(0x000b, TIMERUS_USEC_CFG); |
| 197 | break; |
| 198 | case 13000000: |
| 199 | timer_writel(0x000c, TIMERUS_USEC_CFG); |
| 200 | break; |
| 201 | case 19200000: |
| 202 | timer_writel(0x045f, TIMERUS_USEC_CFG); |
| 203 | break; |
| 204 | case 26000000: |
| 205 | timer_writel(0x0019, TIMERUS_USEC_CFG); |
| 206 | break; |
| 207 | default: |
| 208 | WARN(1, "Unknown clock rate"); |
| 209 | } |
| 210 | |
Stephen Boyd | 3570299 | 2013-07-18 16:21:26 -0700 | [diff] [blame] | 211 | sched_clock_register(tegra_read_sched_clock, 32, 1000000); |
Russell King | e3f4c0a | 2010-12-15 21:49:42 +0000 | [diff] [blame] | 212 | |
Russell King | 234b6ced | 2011-05-08 14:09:47 +0100 | [diff] [blame] | 213 | if (clocksource_mmio_init(timer_reg_base + TIMERUS_CNTR_1US, |
| 214 | "timer_us", 1000000, 300, 32, clocksource_mmio_readl_up)) { |
Stephen Warren | 58664f9 | 2012-10-23 12:21:39 -0600 | [diff] [blame] | 215 | pr_err("Failed to register clocksource\n"); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 216 | BUG(); |
| 217 | } |
| 218 | |
Peter De Schrijver | 0ff36b4 | 2014-06-12 18:58:29 +0300 | [diff] [blame] | 219 | tegra_delay_timer.read_current_timer = |
| 220 | tegra_delay_timer_read_counter_long; |
| 221 | tegra_delay_timer.freq = 1000000; |
| 222 | register_current_timer_delay(&tegra_delay_timer); |
| 223 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 224 | ret = setup_irq(tegra_timer_irq.irq, &tegra_timer_irq); |
| 225 | if (ret) { |
Stephen Warren | 58664f9 | 2012-10-23 12:21:39 -0600 | [diff] [blame] | 226 | pr_err("Failed to register timer IRQ: %d\n", ret); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 227 | BUG(); |
| 228 | } |
| 229 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 230 | tegra_clockevent.cpumask = cpu_all_mask; |
| 231 | tegra_clockevent.irq = tegra_timer_irq.irq; |
Shawn Guo | 838a2ae | 2013-01-12 11:50:05 +0000 | [diff] [blame] | 232 | clockevents_config_and_register(&tegra_clockevent, 1000000, |
| 233 | 0x1, 0x1fffffff); |
Rob Herring | 1d16cfb | 2013-02-07 11:36:23 -0600 | [diff] [blame] | 234 | } |
| 235 | CLOCKSOURCE_OF_DECLARE(tegra20_timer, "nvidia,tegra20-timer", tegra20_init_timer); |
| 236 | |
| 237 | static void __init tegra20_init_rtc(struct device_node *np) |
| 238 | { |
| 239 | struct clk *clk; |
| 240 | |
| 241 | rtc_base = of_iomap(np, 0); |
| 242 | if (!rtc_base) { |
| 243 | pr_err("Can't map RTC registers"); |
| 244 | BUG(); |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * rtc registers are used by read_persistent_clock, keep the rtc clock |
| 249 | * enabled |
| 250 | */ |
Arnd Bergmann | 8024206 | 2013-04-09 15:27:52 +0200 | [diff] [blame] | 251 | clk = of_clk_get(np, 0); |
Rob Herring | 1d16cfb | 2013-02-07 11:36:23 -0600 | [diff] [blame] | 252 | if (IS_ERR(clk)) |
| 253 | pr_warn("Unable to get rtc-tegra clock\n"); |
| 254 | else |
| 255 | clk_prepare_enable(clk); |
| 256 | |
Xunlei Pang | cb85071 | 2015-04-01 20:34:26 -0700 | [diff] [blame] | 257 | register_persistent_clock(NULL, tegra_read_persistent_clock64); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 258 | } |
Rob Herring | 1d16cfb | 2013-02-07 11:36:23 -0600 | [diff] [blame] | 259 | CLOCKSOURCE_OF_DECLARE(tegra20_rtc, "nvidia,tegra20-rtc", tegra20_init_rtc); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 260 | |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 261 | #ifdef CONFIG_PM |
| 262 | static u32 usec_config; |
| 263 | |
| 264 | void tegra_timer_suspend(void) |
| 265 | { |
| 266 | usec_config = timer_readl(TIMERUS_USEC_CFG); |
| 267 | } |
| 268 | |
| 269 | void tegra_timer_resume(void) |
| 270 | { |
| 271 | timer_writel(usec_config, TIMERUS_USEC_CFG); |
| 272 | } |
| 273 | #endif |