Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * arch/arch/mach-tegra/timer.c |
| 3 | * |
| 4 | * Copyright (C) 2010 Google, Inc. |
| 5 | * |
| 6 | * Author: |
| 7 | * Colin Cross <ccross@google.com> |
| 8 | * |
| 9 | * This software is licensed under the terms of the GNU General Public |
| 10 | * License version 2, as published by the Free Software Foundation, and |
| 11 | * may be copied, distributed, and modified under those terms. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <linux/init.h> |
Colin Cross | 62248ae | 2011-02-21 17:04:37 -0800 | [diff] [blame] | 21 | #include <linux/err.h> |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 22 | #include <linux/time.h> |
| 23 | #include <linux/interrupt.h> |
| 24 | #include <linux/irq.h> |
| 25 | #include <linux/clockchips.h> |
| 26 | #include <linux/clocksource.h> |
| 27 | #include <linux/clk.h> |
| 28 | #include <linux/io.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 | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 34 | #include <mach/irqs.h> |
| 35 | |
| 36 | #include "board.h" |
| 37 | #include "clock.h" |
Stephen Warren | 2be39c0 | 2012-10-04 14:24:09 -0600 | [diff] [blame] | 38 | #include "iomap.h" |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 39 | |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 40 | #define RTC_SECONDS 0x08 |
| 41 | #define RTC_SHADOW_SECONDS 0x0c |
| 42 | #define RTC_MILLISECONDS 0x10 |
| 43 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 44 | #define TIMERUS_CNTR_1US 0x10 |
| 45 | #define TIMERUS_USEC_CFG 0x14 |
| 46 | #define TIMERUS_CNTR_FREEZE 0x4c |
| 47 | |
| 48 | #define TIMER1_BASE 0x0 |
| 49 | #define TIMER2_BASE 0x8 |
| 50 | #define TIMER3_BASE 0x50 |
| 51 | #define TIMER4_BASE 0x58 |
| 52 | |
| 53 | #define TIMER_PTV 0x0 |
| 54 | #define TIMER_PCR 0x4 |
| 55 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 56 | static void __iomem *timer_reg_base = IO_ADDRESS(TEGRA_TMR1_BASE); |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 57 | static void __iomem *rtc_base = IO_ADDRESS(TEGRA_RTC_BASE); |
| 58 | |
| 59 | static struct timespec persistent_ts; |
| 60 | static u64 persistent_ms, last_persistent_ms; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 61 | |
| 62 | #define timer_writel(value, reg) \ |
Olof Johansson | 75d7116 | 2011-09-08 17:49:13 -0700 | [diff] [blame] | 63 | __raw_writel(value, timer_reg_base + (reg)) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 64 | #define timer_readl(reg) \ |
Olof Johansson | 75d7116 | 2011-09-08 17:49:13 -0700 | [diff] [blame] | 65 | __raw_readl(timer_reg_base + (reg)) |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 66 | |
| 67 | static int tegra_timer_set_next_event(unsigned long cycles, |
| 68 | struct clock_event_device *evt) |
| 69 | { |
| 70 | u32 reg; |
| 71 | |
| 72 | reg = 0x80000000 | ((cycles > 1) ? (cycles-1) : 0); |
| 73 | timer_writel(reg, TIMER3_BASE + TIMER_PTV); |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static void tegra_timer_set_mode(enum clock_event_mode mode, |
| 79 | struct clock_event_device *evt) |
| 80 | { |
| 81 | u32 reg; |
| 82 | |
| 83 | timer_writel(0, TIMER3_BASE + TIMER_PTV); |
| 84 | |
| 85 | switch (mode) { |
| 86 | case CLOCK_EVT_MODE_PERIODIC: |
| 87 | reg = 0xC0000000 | ((1000000/HZ)-1); |
| 88 | timer_writel(reg, TIMER3_BASE + TIMER_PTV); |
| 89 | break; |
| 90 | case CLOCK_EVT_MODE_ONESHOT: |
| 91 | break; |
| 92 | case CLOCK_EVT_MODE_UNUSED: |
| 93 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 94 | case CLOCK_EVT_MODE_RESUME: |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 99 | static struct clock_event_device tegra_clockevent = { |
| 100 | .name = "timer0", |
| 101 | .rating = 300, |
| 102 | .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC, |
| 103 | .set_next_event = tegra_timer_set_next_event, |
| 104 | .set_mode = tegra_timer_set_mode, |
| 105 | }; |
| 106 | |
Marc Zyngier | 2f0778af | 2011-12-15 12:19:23 +0100 | [diff] [blame] | 107 | static u32 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 | /* |
Marc Zyngier | bd0493e | 2012-05-05 19:28:44 +0100 | [diff] [blame] | 126 | * tegra_read_persistent_clock - 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 |
| 130 | * nsecs and adds to a monotonically increasing timespec. |
| 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 | */ |
Marc Zyngier | bd0493e | 2012-05-05 19:28:44 +0100 | [diff] [blame] | 135 | static void tegra_read_persistent_clock(struct timespec *ts) |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 136 | { |
| 137 | u64 delta; |
| 138 | struct timespec *tsp = &persistent_ts; |
| 139 | |
| 140 | last_persistent_ms = persistent_ms; |
| 141 | persistent_ms = tegra_rtc_read_ms(); |
| 142 | delta = persistent_ms - last_persistent_ms; |
| 143 | |
| 144 | timespec_add_ns(tsp, delta * NSEC_PER_MSEC); |
| 145 | *ts = *tsp; |
| 146 | } |
| 147 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 148 | static irqreturn_t tegra_timer_interrupt(int irq, void *dev_id) |
| 149 | { |
| 150 | struct clock_event_device *evt = (struct clock_event_device *)dev_id; |
| 151 | timer_writel(1<<30, TIMER3_BASE + TIMER_PCR); |
| 152 | evt->event_handler(evt); |
| 153 | return IRQ_HANDLED; |
| 154 | } |
| 155 | |
| 156 | static struct irqaction tegra_timer_irq = { |
| 157 | .name = "timer0", |
| 158 | .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_TRIGGER_HIGH, |
| 159 | .handler = tegra_timer_interrupt, |
| 160 | .dev_id = &tegra_clockevent, |
| 161 | .irq = INT_TMR3, |
| 162 | }; |
| 163 | |
Marc Zyngier | 1fcf3a6 | 2012-01-10 19:44:19 +0000 | [diff] [blame] | 164 | #ifdef CONFIG_HAVE_ARM_TWD |
| 165 | static DEFINE_TWD_LOCAL_TIMER(twd_local_timer, |
| 166 | TEGRA_ARM_PERIF_BASE + 0x600, |
| 167 | IRQ_LOCALTIMER); |
| 168 | |
| 169 | static void __init tegra_twd_init(void) |
| 170 | { |
| 171 | int err = twd_local_timer_register(&twd_local_timer); |
| 172 | if (err) |
| 173 | pr_err("twd_local_timer_register failed %d\n", err); |
| 174 | } |
| 175 | #else |
| 176 | #define tegra_twd_init() do {} while(0) |
| 177 | #endif |
| 178 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 179 | static void __init tegra_init_timer(void) |
| 180 | { |
Colin Cross | 62248ae | 2011-02-21 17:04:37 -0800 | [diff] [blame] | 181 | struct clk *clk; |
Peter De Schrijver | 8e4fab2 | 2011-12-14 17:03:16 +0200 | [diff] [blame] | 182 | unsigned long rate; |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 183 | int ret; |
| 184 | |
Colin Cross | 62248ae | 2011-02-21 17:04:37 -0800 | [diff] [blame] | 185 | clk = clk_get_sys("timer", NULL); |
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 | |
| 194 | /* |
| 195 | * rtc registers are used by read_persistent_clock, keep the rtc clock |
| 196 | * enabled |
| 197 | */ |
| 198 | clk = clk_get_sys("rtc-tegra", NULL); |
Peter De Schrijver | 2d85b5d | 2011-10-26 11:41:41 +0300 | [diff] [blame] | 199 | if (IS_ERR(clk)) |
| 200 | pr_warn("Unable to get rtc-tegra clock\n"); |
| 201 | else |
Prashant Gaikwad | 6a5278d | 2012-06-05 09:59:35 +0530 | [diff] [blame] | 202 | clk_prepare_enable(clk); |
Colin Cross | 62248ae | 2011-02-21 17:04:37 -0800 | [diff] [blame] | 203 | |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 204 | switch (rate) { |
| 205 | case 12000000: |
| 206 | timer_writel(0x000b, TIMERUS_USEC_CFG); |
| 207 | break; |
| 208 | case 13000000: |
| 209 | timer_writel(0x000c, TIMERUS_USEC_CFG); |
| 210 | break; |
| 211 | case 19200000: |
| 212 | timer_writel(0x045f, TIMERUS_USEC_CFG); |
| 213 | break; |
| 214 | case 26000000: |
| 215 | timer_writel(0x0019, TIMERUS_USEC_CFG); |
| 216 | break; |
| 217 | default: |
| 218 | WARN(1, "Unknown clock rate"); |
| 219 | } |
| 220 | |
Marc Zyngier | 2f0778af | 2011-12-15 12:19:23 +0100 | [diff] [blame] | 221 | setup_sched_clock(tegra_read_sched_clock, 32, 1000000); |
Russell King | e3f4c0a | 2010-12-15 21:49:42 +0000 | [diff] [blame] | 222 | |
Russell King | 234b6ced | 2011-05-08 14:09:47 +0100 | [diff] [blame] | 223 | if (clocksource_mmio_init(timer_reg_base + TIMERUS_CNTR_1US, |
| 224 | "timer_us", 1000000, 300, 32, clocksource_mmio_readl_up)) { |
Stephen Warren | 58664f9 | 2012-10-23 12:21:39 -0600 | [diff] [blame^] | 225 | pr_err("Failed to register clocksource\n"); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 226 | BUG(); |
| 227 | } |
| 228 | |
| 229 | ret = setup_irq(tegra_timer_irq.irq, &tegra_timer_irq); |
| 230 | if (ret) { |
Stephen Warren | 58664f9 | 2012-10-23 12:21:39 -0600 | [diff] [blame^] | 231 | pr_err("Failed to register timer IRQ: %d\n", ret); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 232 | BUG(); |
| 233 | } |
| 234 | |
| 235 | clockevents_calc_mult_shift(&tegra_clockevent, 1000000, 5); |
| 236 | tegra_clockevent.max_delta_ns = |
| 237 | clockevent_delta2ns(0x1fffffff, &tegra_clockevent); |
| 238 | tegra_clockevent.min_delta_ns = |
| 239 | clockevent_delta2ns(0x1, &tegra_clockevent); |
| 240 | tegra_clockevent.cpumask = cpu_all_mask; |
| 241 | tegra_clockevent.irq = tegra_timer_irq.irq; |
| 242 | clockevents_register_device(&tegra_clockevent); |
Marc Zyngier | 1fcf3a6 | 2012-01-10 19:44:19 +0000 | [diff] [blame] | 243 | tegra_twd_init(); |
Marc Zyngier | bd0493e | 2012-05-05 19:28:44 +0100 | [diff] [blame] | 244 | register_persistent_clock(NULL, tegra_read_persistent_clock); |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Sivaram Nair | f2ef412 | 2012-10-16 13:08:35 +0300 | [diff] [blame] | 247 | struct sys_timer tegra_sys_timer = { |
Colin Cross | 2d5cd9a | 2010-01-28 16:41:42 -0800 | [diff] [blame] | 248 | .init = tegra_init_timer, |
| 249 | }; |
Colin Cross | 0936178 | 2010-11-28 16:26:19 -0800 | [diff] [blame] | 250 | |
| 251 | #ifdef CONFIG_PM |
| 252 | static u32 usec_config; |
| 253 | |
| 254 | void tegra_timer_suspend(void) |
| 255 | { |
| 256 | usec_config = timer_readl(TIMERUS_USEC_CFG); |
| 257 | } |
| 258 | |
| 259 | void tegra_timer_resume(void) |
| 260 | { |
| 261 | timer_writel(usec_config, TIMERUS_USEC_CFG); |
| 262 | } |
| 263 | #endif |