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> |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 29 | |
| 30 | #include <asm/mach/time.h> |
Marc Zyngier | 1fcf3a6 | 2012-01-10 19:44:19 +0000 | [diff] [blame] | 31 | #include <asm/smp_twd.h> |
Russell King | e3f4c0a | 2010-12-15 21:49:42 +0000 | [diff] [blame] | 32 | #include <asm/sched_clock.h> |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 33 | |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 34 | #define RTC_SECONDS 0x08 |
| 35 | #define RTC_SHADOW_SECONDS 0x0c |
| 36 | #define RTC_MILLISECONDS 0x10 |
| 37 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 38 | #define TIMERUS_CNTR_1US 0x10 |
| 39 | #define TIMERUS_USEC_CFG 0x14 |
| 40 | #define TIMERUS_CNTR_FREEZE 0x4c |
| 41 | |
| 42 | #define TIMER1_BASE 0x0 |
| 43 | #define TIMER2_BASE 0x8 |
| 44 | #define TIMER3_BASE 0x50 |
| 45 | #define TIMER4_BASE 0x58 |
| 46 | |
| 47 | #define TIMER_PTV 0x0 |
| 48 | #define TIMER_PCR 0x4 |
| 49 | |
Stephen Warren | 3a04931 | 2012-10-23 11:40:25 -0600 | [diff] [blame] | 50 | static void __iomem *timer_reg_base; |
| 51 | static void __iomem *rtc_base; |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 52 | |
| 53 | static struct timespec persistent_ts; |
| 54 | static u64 persistent_ms, last_persistent_ms; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 55 | |
| 56 | #define timer_writel(value, reg) \ |
Olof Johansson | 75d7116 | 2011-09-08 17:49:13 -0700 | [diff] [blame] | 57 | __raw_writel(value, timer_reg_base + (reg)) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 58 | #define timer_readl(reg) \ |
Olof Johansson | 75d7116 | 2011-09-08 17:49:13 -0700 | [diff] [blame] | 59 | __raw_readl(timer_reg_base + (reg)) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 60 | |
| 61 | static int tegra_timer_set_next_event(unsigned long cycles, |
| 62 | struct clock_event_device *evt) |
| 63 | { |
| 64 | u32 reg; |
| 65 | |
| 66 | reg = 0x80000000 | ((cycles > 1) ? (cycles-1) : 0); |
| 67 | timer_writel(reg, TIMER3_BASE + TIMER_PTV); |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static void tegra_timer_set_mode(enum clock_event_mode mode, |
| 73 | struct clock_event_device *evt) |
| 74 | { |
| 75 | u32 reg; |
| 76 | |
| 77 | timer_writel(0, TIMER3_BASE + TIMER_PTV); |
| 78 | |
| 79 | switch (mode) { |
| 80 | case CLOCK_EVT_MODE_PERIODIC: |
| 81 | reg = 0xC0000000 | ((1000000/HZ)-1); |
| 82 | timer_writel(reg, TIMER3_BASE + TIMER_PTV); |
| 83 | break; |
| 84 | case CLOCK_EVT_MODE_ONESHOT: |
| 85 | break; |
| 86 | case CLOCK_EVT_MODE_UNUSED: |
| 87 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 88 | case CLOCK_EVT_MODE_RESUME: |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 93 | static struct clock_event_device tegra_clockevent = { |
| 94 | .name = "timer0", |
| 95 | .rating = 300, |
| 96 | .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC, |
| 97 | .set_next_event = tegra_timer_set_next_event, |
| 98 | .set_mode = tegra_timer_set_mode, |
| 99 | }; |
| 100 | |
Marc Zyngier | 2f0778af | 2011-12-15 12:19:23 +0100 | [diff] [blame] | 101 | static u32 notrace tegra_read_sched_clock(void) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 102 | { |
Marc Zyngier | 2f0778af | 2011-12-15 12:19:23 +0100 | [diff] [blame] | 103 | return timer_readl(TIMERUS_CNTR_1US); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 106 | /* |
| 107 | * tegra_rtc_read - Reads the Tegra RTC registers |
| 108 | * Care must be taken that this funciton is not called while the |
| 109 | * tegra_rtc driver could be executing to avoid race conditions |
| 110 | * on the RTC shadow register |
| 111 | */ |
Olof Johansson | b28fba2 | 2011-09-08 17:50:03 -0700 | [diff] [blame] | 112 | static u64 tegra_rtc_read_ms(void) |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 113 | { |
| 114 | u32 ms = readl(rtc_base + RTC_MILLISECONDS); |
| 115 | u32 s = readl(rtc_base + RTC_SHADOW_SECONDS); |
| 116 | return (u64)s * MSEC_PER_SEC + ms; |
| 117 | } |
| 118 | |
| 119 | /* |
Marc Zyngier | bd0493e | 2012-05-05 19:28:44 +0100 | [diff] [blame] | 120 | * tegra_read_persistent_clock - Return time from a persistent clock. |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 121 | * |
| 122 | * Reads the time from a source which isn't disabled during PM, the |
| 123 | * 32k sync timer. Convert the cycles elapsed since last read into |
| 124 | * nsecs and adds to a monotonically increasing timespec. |
| 125 | * Care must be taken that this funciton is not called while the |
| 126 | * tegra_rtc driver could be executing to avoid race conditions |
| 127 | * on the RTC shadow register |
| 128 | */ |
Marc Zyngier | bd0493e | 2012-05-05 19:28:44 +0100 | [diff] [blame] | 129 | static void tegra_read_persistent_clock(struct timespec *ts) |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 130 | { |
| 131 | u64 delta; |
| 132 | struct timespec *tsp = &persistent_ts; |
| 133 | |
| 134 | last_persistent_ms = persistent_ms; |
| 135 | persistent_ms = tegra_rtc_read_ms(); |
| 136 | delta = persistent_ms - last_persistent_ms; |
| 137 | |
| 138 | timespec_add_ns(tsp, delta * NSEC_PER_MSEC); |
| 139 | *ts = *tsp; |
| 140 | } |
| 141 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 142 | static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id) |
| 143 | { |
| 144 | struct clock_event_device *evt = (struct clock_event_device *)dev_id; |
| 145 | timer_writel(1<<30, TIMER3_BASE + TIMER_PCR); |
| 146 | evt->event_handler(evt); |
| 147 | return IRQ_HANDLED; |
| 148 | } |
| 149 | |
| 150 | static struct irqaction tegra_timer_irq = { |
| 151 | .name = "timer0", |
| 152 | .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_HIGH, |
| 153 | .handler = tegra_timer_interrupt, |
| 154 | .dev_id = &tegra_clockevent, |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
Stephen Warren | 5641548 | 2012-09-19 13:13:33 -0600 | [diff] [blame] | 157 | static const struct of_device_id timer_match[] __initconst = { |
| 158 | { .compatible = "nvidia,tegra20-timer" }, |
| 159 | {} |
| 160 | }; |
Marc Zyngier | 1fcf3a6 | 2012-01-10 19:44:19 +0000 | [diff] [blame] | 161 | |
Stephen Warren | 3a04931 | 2012-10-23 11:40:25 -0600 | [diff] [blame] | 162 | static const struct of_device_id rtc_match[] __initconst = { |
| 163 | { .compatible = "nvidia,tegra20-rtc" }, |
| 164 | {} |
| 165 | }; |
| 166 | |
Stephen Warren | 1711b1e | 2012-10-23 11:52:53 -0600 | [diff] [blame] | 167 | static void __init tegra20_init_timer(void) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 168 | { |
Stephen Warren | 5641548 | 2012-09-19 13:13:33 -0600 | [diff] [blame] | 169 | struct device_node *np; |
Colin Cross | 62248ae | 2011-02-21 17:04:37 -0800 | [diff] [blame] | 170 | struct clk *clk; |
Peter De Schrijver | 8e4fab2 | 2011-12-14 17:03:16 +0200 | [diff] [blame] | 171 | unsigned long rate; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 172 | int ret; |
| 173 | |
Stephen Warren | 5641548 | 2012-09-19 13:13:33 -0600 | [diff] [blame] | 174 | np = of_find_matching_node(NULL, timer_match); |
| 175 | if (!np) { |
| 176 | pr_err("Failed to find timer DT node\n"); |
| 177 | BUG(); |
| 178 | } |
| 179 | |
Stephen Warren | 3a04931 | 2012-10-23 11:40:25 -0600 | [diff] [blame] | 180 | timer_reg_base = of_iomap(np, 0); |
| 181 | if (!timer_reg_base) { |
Hiroshi Doyu | 3734086 | 2012-12-17 13:35:23 +0200 | [diff] [blame^] | 182 | pr_err("Can't map timer registers\n"); |
Stephen Warren | 3a04931 | 2012-10-23 11:40:25 -0600 | [diff] [blame] | 183 | BUG(); |
| 184 | } |
| 185 | |
Stephen Warren | 5641548 | 2012-09-19 13:13:33 -0600 | [diff] [blame] | 186 | tegra_timer_irq.irq = irq_of_parse_and_map(np, 2); |
| 187 | if (tegra_timer_irq.irq <= 0) { |
| 188 | pr_err("Failed to map timer IRQ\n"); |
| 189 | BUG(); |
| 190 | } |
| 191 | |
Colin Cross | 62248ae | 2011-02-21 17:04:37 -0800 | [diff] [blame] | 192 | clk = clk_get_sys("timer", NULL); |
Peter De Schrijver | 8e4fab2 | 2011-12-14 17:03:16 +0200 | [diff] [blame] | 193 | if (IS_ERR(clk)) { |
Stephen Warren | 58664f9 | 2012-10-23 12:21:39 -0600 | [diff] [blame] | 194 | 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] | 195 | rate = 12000000; |
| 196 | } else { |
Prashant Gaikwad | 6a5278d | 2012-06-05 09:59:35 +0530 | [diff] [blame] | 197 | clk_prepare_enable(clk); |
Peter De Schrijver | 8e4fab2 | 2011-12-14 17:03:16 +0200 | [diff] [blame] | 198 | rate = clk_get_rate(clk); |
| 199 | } |
Colin Cross | 62248ae | 2011-02-21 17:04:37 -0800 | [diff] [blame] | 200 | |
Stephen Warren | 3a04931 | 2012-10-23 11:40:25 -0600 | [diff] [blame] | 201 | of_node_put(np); |
| 202 | |
| 203 | np = of_find_matching_node(NULL, rtc_match); |
| 204 | if (!np) { |
| 205 | pr_err("Failed to find RTC DT node\n"); |
| 206 | BUG(); |
| 207 | } |
| 208 | |
| 209 | rtc_base = of_iomap(np, 0); |
| 210 | if (!rtc_base) { |
| 211 | pr_err("Can't map RTC registers"); |
| 212 | BUG(); |
| 213 | } |
| 214 | |
Colin Cross | 62248ae | 2011-02-21 17:04:37 -0800 | [diff] [blame] | 215 | /* |
| 216 | * rtc registers are used by read_persistent_clock, keep the rtc clock |
| 217 | * enabled |
| 218 | */ |
| 219 | clk = clk_get_sys("rtc-tegra", NULL); |
Peter De Schrijver | 2d85b5d | 2011-10-26 11:41:41 +0300 | [diff] [blame] | 220 | if (IS_ERR(clk)) |
| 221 | pr_warn("Unable to get rtc-tegra clock\n"); |
| 222 | else |
Prashant Gaikwad | 6a5278d | 2012-06-05 09:59:35 +0530 | [diff] [blame] | 223 | clk_prepare_enable(clk); |
Colin Cross | 62248ae | 2011-02-21 17:04:37 -0800 | [diff] [blame] | 224 | |
Stephen Warren | 5641548 | 2012-09-19 13:13:33 -0600 | [diff] [blame] | 225 | of_node_put(np); |
| 226 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 227 | switch (rate) { |
| 228 | case 12000000: |
| 229 | timer_writel(0x000b, TIMERUS_USEC_CFG); |
| 230 | break; |
| 231 | case 13000000: |
| 232 | timer_writel(0x000c, TIMERUS_USEC_CFG); |
| 233 | break; |
| 234 | case 19200000: |
| 235 | timer_writel(0x045f, TIMERUS_USEC_CFG); |
| 236 | break; |
| 237 | case 26000000: |
| 238 | timer_writel(0x0019, TIMERUS_USEC_CFG); |
| 239 | break; |
| 240 | default: |
| 241 | WARN(1, "Unknown clock rate"); |
| 242 | } |
| 243 | |
Marc Zyngier | 2f0778af | 2011-12-15 12:19:23 +0100 | [diff] [blame] | 244 | setup_sched_clock(tegra_read_sched_clock, 32, 1000000); |
Russell King | e3f4c0a | 2010-12-15 21:49:42 +0000 | [diff] [blame] | 245 | |
Russell King | 234b6ced | 2011-05-08 14:09:47 +0100 | [diff] [blame] | 246 | if (clocksource_mmio_init(timer_reg_base + TIMERUS_CNTR_1US, |
| 247 | "timer_us", 1000000, 300, 32, clocksource_mmio_readl_up)) { |
Stephen Warren | 58664f9 | 2012-10-23 12:21:39 -0600 | [diff] [blame] | 248 | pr_err("Failed to register clocksource\n"); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 249 | BUG(); |
| 250 | } |
| 251 | |
| 252 | ret = setup_irq(tegra_timer_irq.irq, &tegra_timer_irq); |
| 253 | if (ret) { |
Stephen Warren | 58664f9 | 2012-10-23 12:21:39 -0600 | [diff] [blame] | 254 | pr_err("Failed to register timer IRQ: %d\n", ret); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 255 | BUG(); |
| 256 | } |
| 257 | |
| 258 | clockevents_calc_mult_shift(&tegra_clockevent, 1000000, 5); |
| 259 | tegra_clockevent.max_delta_ns = |
| 260 | clockevent_delta2ns(0x1fffffff, &tegra_clockevent); |
| 261 | tegra_clockevent.min_delta_ns = |
| 262 | clockevent_delta2ns(0x1, &tegra_clockevent); |
| 263 | tegra_clockevent.cpumask = cpu_all_mask; |
| 264 | tegra_clockevent.irq = tegra_timer_irq.irq; |
| 265 | clockevents_register_device(&tegra_clockevent); |
Stephen Warren | 5641548 | 2012-09-19 13:13:33 -0600 | [diff] [blame] | 266 | #ifdef CONFIG_HAVE_ARM_TWD |
| 267 | twd_local_timer_of_register(); |
| 268 | #endif |
Marc Zyngier | bd0493e | 2012-05-05 19:28:44 +0100 | [diff] [blame] | 269 | register_persistent_clock(NULL, tegra_read_persistent_clock); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 270 | } |
Stephen Warren | 1711b1e | 2012-10-23 11:52:53 -0600 | [diff] [blame] | 271 | CLOCKSOURCE_OF_DECLARE(tegra20, "nvidia,tegra20-timer", tegra20_init_timer); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 272 | |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 273 | #ifdef CONFIG_PM |
| 274 | static u32 usec_config; |
| 275 | |
| 276 | void tegra_timer_suspend(void) |
| 277 | { |
| 278 | usec_config = timer_readl(TIMERUS_USEC_CFG); |
| 279 | } |
| 280 | |
| 281 | void tegra_timer_resume(void) |
| 282 | { |
| 283 | timer_writel(usec_config, TIMERUS_USEC_CFG); |
| 284 | } |
| 285 | #endif |