Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 1 | /* |
| 2 | * System timer for CSR SiRFprimaII |
| 3 | * |
| 4 | * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company. |
| 5 | * |
| 6 | * Licensed under GPLv2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/clockchips.h> |
| 12 | #include <linux/clocksource.h> |
Stephen Boyd | 05a6548 | 2013-02-15 17:02:16 -0800 | [diff] [blame] | 13 | #include <linux/cpu.h> |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 14 | #include <linux/bitops.h> |
| 15 | #include <linux/irq.h> |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/of_irq.h> |
| 20 | #include <linux/of_address.h> |
Stephen Boyd | 38ff87f | 2013-06-01 23:39:40 -0700 | [diff] [blame] | 21 | #include <linux/sched_clock.h> |
Uwe Kleine-König | 980c51a | 2013-11-11 21:06:11 +0100 | [diff] [blame] | 22 | |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 23 | #define SIRFSOC_TIMER_32COUNTER_0_CTRL 0x0000 |
| 24 | #define SIRFSOC_TIMER_32COUNTER_1_CTRL 0x0004 |
| 25 | #define SIRFSOC_TIMER_MATCH_0 0x0018 |
| 26 | #define SIRFSOC_TIMER_MATCH_1 0x001c |
| 27 | #define SIRFSOC_TIMER_COUNTER_0 0x0048 |
| 28 | #define SIRFSOC_TIMER_COUNTER_1 0x004c |
| 29 | #define SIRFSOC_TIMER_INTR_STATUS 0x0060 |
| 30 | #define SIRFSOC_TIMER_WATCHDOG_EN 0x0064 |
| 31 | #define SIRFSOC_TIMER_64COUNTER_CTRL 0x0068 |
| 32 | #define SIRFSOC_TIMER_64COUNTER_LO 0x006c |
| 33 | #define SIRFSOC_TIMER_64COUNTER_HI 0x0070 |
| 34 | #define SIRFSOC_TIMER_64COUNTER_LOAD_LO 0x0074 |
| 35 | #define SIRFSOC_TIMER_64COUNTER_LOAD_HI 0x0078 |
| 36 | #define SIRFSOC_TIMER_64COUNTER_RLATCHED_LO 0x007c |
| 37 | #define SIRFSOC_TIMER_64COUNTER_RLATCHED_HI 0x0080 |
| 38 | |
| 39 | #define SIRFSOC_TIMER_REG_CNT 6 |
| 40 | |
Barry Song | 5833ac9 | 2015-01-12 00:04:43 +0800 | [diff] [blame] | 41 | static unsigned long atlas7_timer_rate; |
Yanchang Li | ef89af1 | 2014-11-11 20:42:52 +0800 | [diff] [blame] | 42 | |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 43 | static const u32 sirfsoc_timer_reg_list[SIRFSOC_TIMER_REG_CNT] = { |
| 44 | SIRFSOC_TIMER_WATCHDOG_EN, |
| 45 | SIRFSOC_TIMER_32COUNTER_0_CTRL, |
| 46 | SIRFSOC_TIMER_32COUNTER_1_CTRL, |
| 47 | SIRFSOC_TIMER_64COUNTER_CTRL, |
| 48 | SIRFSOC_TIMER_64COUNTER_RLATCHED_LO, |
| 49 | SIRFSOC_TIMER_64COUNTER_RLATCHED_HI, |
| 50 | }; |
| 51 | |
| 52 | static u32 sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT]; |
| 53 | |
| 54 | static void __iomem *sirfsoc_timer_base; |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 55 | |
| 56 | /* disable count and interrupt */ |
| 57 | static inline void sirfsoc_timer_count_disable(int idx) |
| 58 | { |
| 59 | writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx) & ~0x7, |
| 60 | sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx); |
| 61 | } |
| 62 | |
| 63 | /* enable count and interrupt */ |
| 64 | static inline void sirfsoc_timer_count_enable(int idx) |
| 65 | { |
Hao Liu | 28cf356 | 2014-09-29 01:50:06 +0200 | [diff] [blame] | 66 | writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx) | 0x3, |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 67 | sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL + 4 * idx); |
| 68 | } |
| 69 | |
| 70 | /* timer interrupt handler */ |
| 71 | static irqreturn_t sirfsoc_timer_interrupt(int irq, void *dev_id) |
| 72 | { |
| 73 | struct clock_event_device *ce = dev_id; |
| 74 | int cpu = smp_processor_id(); |
| 75 | |
| 76 | /* clear timer interrupt */ |
| 77 | writel_relaxed(BIT(cpu), sirfsoc_timer_base + SIRFSOC_TIMER_INTR_STATUS); |
| 78 | |
| 79 | if (ce->mode == CLOCK_EVT_MODE_ONESHOT) |
| 80 | sirfsoc_timer_count_disable(cpu); |
| 81 | |
| 82 | ce->event_handler(ce); |
| 83 | |
| 84 | return IRQ_HANDLED; |
| 85 | } |
| 86 | |
| 87 | /* read 64-bit timer counter */ |
| 88 | static cycle_t sirfsoc_timer_read(struct clocksource *cs) |
| 89 | { |
| 90 | u64 cycles; |
| 91 | |
| 92 | writel_relaxed((readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) | |
| 93 | BIT(0)) & ~BIT(1), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL); |
| 94 | |
| 95 | cycles = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_RLATCHED_HI); |
| 96 | cycles = (cycles << 32) | readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_RLATCHED_LO); |
| 97 | |
| 98 | return cycles; |
| 99 | } |
| 100 | |
| 101 | static int sirfsoc_timer_set_next_event(unsigned long delta, |
| 102 | struct clock_event_device *ce) |
| 103 | { |
| 104 | int cpu = smp_processor_id(); |
| 105 | |
Hao Liu | 28cf356 | 2014-09-29 01:50:06 +0200 | [diff] [blame] | 106 | /* disable timer first, then modify the related registers */ |
| 107 | sirfsoc_timer_count_disable(cpu); |
| 108 | |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 109 | writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0 + |
| 110 | 4 * cpu); |
| 111 | writel_relaxed(delta, sirfsoc_timer_base + SIRFSOC_TIMER_MATCH_0 + |
| 112 | 4 * cpu); |
| 113 | |
| 114 | /* enable the tick */ |
| 115 | sirfsoc_timer_count_enable(cpu); |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static void sirfsoc_timer_set_mode(enum clock_event_mode mode, |
| 121 | struct clock_event_device *ce) |
| 122 | { |
| 123 | switch (mode) { |
| 124 | case CLOCK_EVT_MODE_ONESHOT: |
| 125 | /* enable in set_next_event */ |
| 126 | break; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | sirfsoc_timer_count_disable(smp_processor_id()); |
| 132 | } |
| 133 | |
| 134 | static void sirfsoc_clocksource_suspend(struct clocksource *cs) |
| 135 | { |
| 136 | int i; |
| 137 | |
| 138 | for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++) |
| 139 | sirfsoc_timer_reg_val[i] = readl_relaxed(sirfsoc_timer_base + sirfsoc_timer_reg_list[i]); |
| 140 | } |
| 141 | |
| 142 | static void sirfsoc_clocksource_resume(struct clocksource *cs) |
| 143 | { |
| 144 | int i; |
| 145 | |
| 146 | for (i = 0; i < SIRFSOC_TIMER_REG_CNT - 2; i++) |
| 147 | writel_relaxed(sirfsoc_timer_reg_val[i], sirfsoc_timer_base + sirfsoc_timer_reg_list[i]); |
| 148 | |
| 149 | writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 2], |
| 150 | sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO); |
| 151 | writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 1], |
| 152 | sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI); |
| 153 | |
| 154 | writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) | |
| 155 | BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL); |
| 156 | } |
| 157 | |
Stephen Boyd | 05a6548 | 2013-02-15 17:02:16 -0800 | [diff] [blame] | 158 | static struct clock_event_device __percpu *sirfsoc_clockevent; |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 159 | |
| 160 | static struct clocksource sirfsoc_clocksource = { |
| 161 | .name = "sirfsoc_clocksource", |
| 162 | .rating = 200, |
| 163 | .mask = CLOCKSOURCE_MASK(64), |
| 164 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 165 | .read = sirfsoc_timer_read, |
| 166 | .suspend = sirfsoc_clocksource_suspend, |
| 167 | .resume = sirfsoc_clocksource_resume, |
| 168 | }; |
| 169 | |
| 170 | static struct irqaction sirfsoc_timer_irq = { |
| 171 | .name = "sirfsoc_timer0", |
| 172 | .flags = IRQF_TIMER | IRQF_NOBALANCING, |
| 173 | .handler = sirfsoc_timer_interrupt, |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 174 | }; |
| 175 | |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 176 | static struct irqaction sirfsoc_timer1_irq = { |
| 177 | .name = "sirfsoc_timer1", |
| 178 | .flags = IRQF_TIMER | IRQF_NOBALANCING, |
| 179 | .handler = sirfsoc_timer_interrupt, |
| 180 | }; |
| 181 | |
Paul Gortmaker | 8c37bb3 | 2013-06-19 11:32:08 -0400 | [diff] [blame] | 182 | static int sirfsoc_local_timer_setup(struct clock_event_device *ce) |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 183 | { |
Stephen Boyd | 05a6548 | 2013-02-15 17:02:16 -0800 | [diff] [blame] | 184 | int cpu = smp_processor_id(); |
| 185 | struct irqaction *action; |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 186 | |
Stephen Boyd | 05a6548 | 2013-02-15 17:02:16 -0800 | [diff] [blame] | 187 | if (cpu == 0) |
| 188 | action = &sirfsoc_timer_irq; |
| 189 | else |
| 190 | action = &sirfsoc_timer1_irq; |
| 191 | |
| 192 | ce->irq = action->irq; |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 193 | ce->name = "local_timer"; |
Stephen Boyd | 05a6548 | 2013-02-15 17:02:16 -0800 | [diff] [blame] | 194 | ce->features = CLOCK_EVT_FEAT_ONESHOT; |
| 195 | ce->rating = 200; |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 196 | ce->set_mode = sirfsoc_timer_set_mode; |
| 197 | ce->set_next_event = sirfsoc_timer_set_next_event; |
Barry Song | 5833ac9 | 2015-01-12 00:04:43 +0800 | [diff] [blame] | 198 | clockevents_calc_mult_shift(ce, atlas7_timer_rate, 60); |
Stephen Boyd | 05a6548 | 2013-02-15 17:02:16 -0800 | [diff] [blame] | 199 | ce->max_delta_ns = clockevent_delta2ns(-2, ce); |
| 200 | ce->min_delta_ns = clockevent_delta2ns(2, ce); |
| 201 | ce->cpumask = cpumask_of(cpu); |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 202 | |
Stephen Boyd | 05a6548 | 2013-02-15 17:02:16 -0800 | [diff] [blame] | 203 | action->dev_id = ce; |
| 204 | BUG_ON(setup_irq(ce->irq, action)); |
Zhiwu Song | f214be5 | 2014-05-07 14:46:44 +0800 | [diff] [blame] | 205 | irq_force_affinity(action->irq, cpumask_of(cpu)); |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 206 | |
| 207 | clockevents_register_device(ce); |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static void sirfsoc_local_timer_stop(struct clock_event_device *ce) |
| 212 | { |
Stephen Boyd | 05a6548 | 2013-02-15 17:02:16 -0800 | [diff] [blame] | 213 | int cpu = smp_processor_id(); |
| 214 | |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 215 | sirfsoc_timer_count_disable(1); |
| 216 | |
Stephen Boyd | 05a6548 | 2013-02-15 17:02:16 -0800 | [diff] [blame] | 217 | if (cpu == 0) |
| 218 | remove_irq(sirfsoc_timer_irq.irq, &sirfsoc_timer_irq); |
| 219 | else |
| 220 | remove_irq(sirfsoc_timer1_irq.irq, &sirfsoc_timer1_irq); |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 221 | } |
| 222 | |
Olof Johansson | 47dcd35 | 2013-07-23 14:51:34 -0700 | [diff] [blame] | 223 | static int sirfsoc_cpu_notify(struct notifier_block *self, |
| 224 | unsigned long action, void *hcpu) |
Stephen Boyd | 05a6548 | 2013-02-15 17:02:16 -0800 | [diff] [blame] | 225 | { |
| 226 | /* |
| 227 | * Grab cpu pointer in each case to avoid spurious |
| 228 | * preemptible warnings |
| 229 | */ |
| 230 | switch (action & ~CPU_TASKS_FROZEN) { |
| 231 | case CPU_STARTING: |
| 232 | sirfsoc_local_timer_setup(this_cpu_ptr(sirfsoc_clockevent)); |
| 233 | break; |
| 234 | case CPU_DYING: |
| 235 | sirfsoc_local_timer_stop(this_cpu_ptr(sirfsoc_clockevent)); |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | return NOTIFY_OK; |
| 240 | } |
| 241 | |
Olof Johansson | 47dcd35 | 2013-07-23 14:51:34 -0700 | [diff] [blame] | 242 | static struct notifier_block sirfsoc_cpu_nb = { |
Stephen Boyd | 05a6548 | 2013-02-15 17:02:16 -0800 | [diff] [blame] | 243 | .notifier_call = sirfsoc_cpu_notify, |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 244 | }; |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 245 | |
| 246 | static void __init sirfsoc_clockevent_init(void) |
| 247 | { |
Stephen Boyd | 05a6548 | 2013-02-15 17:02:16 -0800 | [diff] [blame] | 248 | sirfsoc_clockevent = alloc_percpu(struct clock_event_device); |
| 249 | BUG_ON(!sirfsoc_clockevent); |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 250 | |
Stephen Boyd | 05a6548 | 2013-02-15 17:02:16 -0800 | [diff] [blame] | 251 | BUG_ON(register_cpu_notifier(&sirfsoc_cpu_nb)); |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 252 | |
Stephen Boyd | 05a6548 | 2013-02-15 17:02:16 -0800 | [diff] [blame] | 253 | /* Immediately configure the timer on the boot CPU */ |
| 254 | sirfsoc_local_timer_setup(this_cpu_ptr(sirfsoc_clockevent)); |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* initialize the kernel jiffy timer source */ |
Barry Song | 5833ac9 | 2015-01-12 00:04:43 +0800 | [diff] [blame] | 258 | static void __init sirfsoc_atlas7_timer_init(struct device_node *np) |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 259 | { |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 260 | struct clk *clk; |
| 261 | |
Zhiwu Song | c7cff54 | 2014-05-05 19:30:04 +0800 | [diff] [blame] | 262 | clk = of_clk_get(np, 0); |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 263 | BUG_ON(IS_ERR(clk)); |
Zhiwu Song | 3894152 | 2014-07-03 20:52:51 +0800 | [diff] [blame] | 264 | |
| 265 | BUG_ON(clk_prepare_enable(clk)); |
| 266 | |
Barry Song | 5833ac9 | 2015-01-12 00:04:43 +0800 | [diff] [blame] | 267 | atlas7_timer_rate = clk_get_rate(clk); |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 268 | |
Yanchang Li | ef89af1 | 2014-11-11 20:42:52 +0800 | [diff] [blame] | 269 | /* timer dividers: 0, not divided */ |
| 270 | writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL); |
| 271 | writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_0_CTRL); |
| 272 | writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_32COUNTER_1_CTRL); |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 273 | |
| 274 | /* Initialize timer counters to 0 */ |
| 275 | writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_LO); |
| 276 | writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_LOAD_HI); |
| 277 | writel_relaxed(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL) | |
| 278 | BIT(1) | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_64COUNTER_CTRL); |
| 279 | writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_0); |
| 280 | writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_1); |
| 281 | |
| 282 | /* Clear all interrupts */ |
| 283 | writel_relaxed(0xFFFF, sirfsoc_timer_base + SIRFSOC_TIMER_INTR_STATUS); |
| 284 | |
Barry Song | 5833ac9 | 2015-01-12 00:04:43 +0800 | [diff] [blame] | 285 | BUG_ON(clocksource_register_hz(&sirfsoc_clocksource, atlas7_timer_rate)); |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 286 | |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 287 | sirfsoc_clockevent_init(); |
| 288 | } |
| 289 | |
Arnd Bergmann | 275786b | 2013-03-19 15:27:22 +0100 | [diff] [blame] | 290 | static void __init sirfsoc_of_timer_init(struct device_node *np) |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 291 | { |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 292 | sirfsoc_timer_base = of_iomap(np, 0); |
| 293 | if (!sirfsoc_timer_base) |
| 294 | panic("unable to map timer cpu registers\n"); |
| 295 | |
| 296 | sirfsoc_timer_irq.irq = irq_of_parse_and_map(np, 0); |
| 297 | if (!sirfsoc_timer_irq.irq) |
| 298 | panic("No irq passed for timer0 via DT\n"); |
| 299 | |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 300 | sirfsoc_timer1_irq.irq = irq_of_parse_and_map(np, 1); |
| 301 | if (!sirfsoc_timer1_irq.irq) |
| 302 | panic("No irq passed for timer1 via DT\n"); |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 303 | |
Barry Song | 5833ac9 | 2015-01-12 00:04:43 +0800 | [diff] [blame] | 304 | sirfsoc_atlas7_timer_init(np); |
Barry Song | 4898de3 | 2012-12-20 19:37:32 +0800 | [diff] [blame] | 305 | } |
Barry Song | 5833ac9 | 2015-01-12 00:04:43 +0800 | [diff] [blame] | 306 | CLOCKSOURCE_OF_DECLARE(sirfsoc_atlas7_timer, "sirf,atlas7-tick", sirfsoc_of_timer_init); |