1c1b40e44SStuart Menefy /* 2c1b40e44SStuart Menefy * drivers/clocksource/arm_global_timer.c 3c1b40e44SStuart Menefy * 4c1b40e44SStuart Menefy * Copyright (C) 2013 STMicroelectronics (R&D) Limited. 5c1b40e44SStuart Menefy * Author: Stuart Menefy <[email protected]> 6c1b40e44SStuart Menefy * Author: Srinivas Kandagatla <[email protected]> 7c1b40e44SStuart Menefy * 8c1b40e44SStuart Menefy * This program is free software; you can redistribute it and/or modify 9c1b40e44SStuart Menefy * it under the terms of the GNU General Public License version 2 as 10c1b40e44SStuart Menefy * published by the Free Software Foundation. 11c1b40e44SStuart Menefy */ 12c1b40e44SStuart Menefy 13c1b40e44SStuart Menefy #include <linux/init.h> 14c1b40e44SStuart Menefy #include <linux/interrupt.h> 15c1b40e44SStuart Menefy #include <linux/clocksource.h> 16c1b40e44SStuart Menefy #include <linux/clockchips.h> 17c1b40e44SStuart Menefy #include <linux/cpu.h> 18c1b40e44SStuart Menefy #include <linux/clk.h> 19c1b40e44SStuart Menefy #include <linux/err.h> 20c1b40e44SStuart Menefy #include <linux/io.h> 21c1b40e44SStuart Menefy #include <linux/of.h> 22c1b40e44SStuart Menefy #include <linux/of_irq.h> 23c1b40e44SStuart Menefy #include <linux/of_address.h> 24c1b40e44SStuart Menefy #include <linux/sched_clock.h> 25c1b40e44SStuart Menefy 26c1b40e44SStuart Menefy #include <asm/cputype.h> 27c1b40e44SStuart Menefy 28c1b40e44SStuart Menefy #define GT_COUNTER0 0x00 29c1b40e44SStuart Menefy #define GT_COUNTER1 0x04 30c1b40e44SStuart Menefy 31c1b40e44SStuart Menefy #define GT_CONTROL 0x08 32c1b40e44SStuart Menefy #define GT_CONTROL_TIMER_ENABLE BIT(0) /* this bit is NOT banked */ 33c1b40e44SStuart Menefy #define GT_CONTROL_COMP_ENABLE BIT(1) /* banked */ 34c1b40e44SStuart Menefy #define GT_CONTROL_IRQ_ENABLE BIT(2) /* banked */ 35c1b40e44SStuart Menefy #define GT_CONTROL_AUTO_INC BIT(3) /* banked */ 36c1b40e44SStuart Menefy 37c1b40e44SStuart Menefy #define GT_INT_STATUS 0x0c 38c1b40e44SStuart Menefy #define GT_INT_STATUS_EVENT_FLAG BIT(0) 39c1b40e44SStuart Menefy 40c1b40e44SStuart Menefy #define GT_COMP0 0x10 41c1b40e44SStuart Menefy #define GT_COMP1 0x14 42c1b40e44SStuart Menefy #define GT_AUTO_INC 0x18 43c1b40e44SStuart Menefy 44c1b40e44SStuart Menefy /* 45c1b40e44SStuart Menefy * We are expecting to be clocked by the ARM peripheral clock. 46c1b40e44SStuart Menefy * 47c1b40e44SStuart Menefy * Note: it is assumed we are using a prescaler value of zero, so this is 48c1b40e44SStuart Menefy * the units for all operations. 49c1b40e44SStuart Menefy */ 50c1b40e44SStuart Menefy static void __iomem *gt_base; 51c1b40e44SStuart Menefy static unsigned long gt_clk_rate; 52c1b40e44SStuart Menefy static int gt_ppi; 53c1b40e44SStuart Menefy static struct clock_event_device __percpu *gt_evt; 54c1b40e44SStuart Menefy 55c1b40e44SStuart Menefy /* 56c1b40e44SStuart Menefy * To get the value from the Global Timer Counter register proceed as follows: 57c1b40e44SStuart Menefy * 1. Read the upper 32-bit timer counter register 58c1b40e44SStuart Menefy * 2. Read the lower 32-bit timer counter register 59c1b40e44SStuart Menefy * 3. Read the upper 32-bit timer counter register again. If the value is 60c1b40e44SStuart Menefy * different to the 32-bit upper value read previously, go back to step 2. 61c1b40e44SStuart Menefy * Otherwise the 64-bit timer counter value is correct. 62c1b40e44SStuart Menefy */ 63c1b40e44SStuart Menefy static u64 gt_counter_read(void) 64c1b40e44SStuart Menefy { 65c1b40e44SStuart Menefy u64 counter; 66c1b40e44SStuart Menefy u32 lower; 67c1b40e44SStuart Menefy u32 upper, old_upper; 68c1b40e44SStuart Menefy 69c1b40e44SStuart Menefy upper = readl_relaxed(gt_base + GT_COUNTER1); 70c1b40e44SStuart Menefy do { 71c1b40e44SStuart Menefy old_upper = upper; 72c1b40e44SStuart Menefy lower = readl_relaxed(gt_base + GT_COUNTER0); 73c1b40e44SStuart Menefy upper = readl_relaxed(gt_base + GT_COUNTER1); 74c1b40e44SStuart Menefy } while (upper != old_upper); 75c1b40e44SStuart Menefy 76c1b40e44SStuart Menefy counter = upper; 77c1b40e44SStuart Menefy counter <<= 32; 78c1b40e44SStuart Menefy counter |= lower; 79c1b40e44SStuart Menefy return counter; 80c1b40e44SStuart Menefy } 81c1b40e44SStuart Menefy 82c1b40e44SStuart Menefy /** 83c1b40e44SStuart Menefy * To ensure that updates to comparator value register do not set the 84c1b40e44SStuart Menefy * Interrupt Status Register proceed as follows: 85c1b40e44SStuart Menefy * 1. Clear the Comp Enable bit in the Timer Control Register. 86c1b40e44SStuart Menefy * 2. Write the lower 32-bit Comparator Value Register. 87c1b40e44SStuart Menefy * 3. Write the upper 32-bit Comparator Value Register. 88c1b40e44SStuart Menefy * 4. Set the Comp Enable bit and, if necessary, the IRQ enable bit. 89c1b40e44SStuart Menefy */ 90c1b40e44SStuart Menefy static void gt_compare_set(unsigned long delta, int periodic) 91c1b40e44SStuart Menefy { 92c1b40e44SStuart Menefy u64 counter = gt_counter_read(); 93c1b40e44SStuart Menefy unsigned long ctrl; 94c1b40e44SStuart Menefy 95c1b40e44SStuart Menefy counter += delta; 96c1b40e44SStuart Menefy ctrl = GT_CONTROL_TIMER_ENABLE; 97c1b40e44SStuart Menefy writel(ctrl, gt_base + GT_CONTROL); 98c1b40e44SStuart Menefy writel(lower_32_bits(counter), gt_base + GT_COMP0); 99c1b40e44SStuart Menefy writel(upper_32_bits(counter), gt_base + GT_COMP1); 100c1b40e44SStuart Menefy 101c1b40e44SStuart Menefy if (periodic) { 102c1b40e44SStuart Menefy writel(delta, gt_base + GT_AUTO_INC); 103c1b40e44SStuart Menefy ctrl |= GT_CONTROL_AUTO_INC; 104c1b40e44SStuart Menefy } 105c1b40e44SStuart Menefy 106c1b40e44SStuart Menefy ctrl |= GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE; 107c1b40e44SStuart Menefy writel(ctrl, gt_base + GT_CONTROL); 108c1b40e44SStuart Menefy } 109c1b40e44SStuart Menefy 110*e511e6c3SViresh Kumar static int gt_clockevent_shutdown(struct clock_event_device *evt) 111c1b40e44SStuart Menefy { 112c1b40e44SStuart Menefy unsigned long ctrl; 113c1b40e44SStuart Menefy 114c1b40e44SStuart Menefy ctrl = readl(gt_base + GT_CONTROL); 115*e511e6c3SViresh Kumar ctrl &= ~(GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE | 116*e511e6c3SViresh Kumar GT_CONTROL_AUTO_INC); 117c1b40e44SStuart Menefy writel(ctrl, gt_base + GT_CONTROL); 118*e511e6c3SViresh Kumar return 0; 119c1b40e44SStuart Menefy } 120*e511e6c3SViresh Kumar 121*e511e6c3SViresh Kumar static int gt_clockevent_set_periodic(struct clock_event_device *evt) 122*e511e6c3SViresh Kumar { 123*e511e6c3SViresh Kumar gt_compare_set(DIV_ROUND_CLOSEST(gt_clk_rate, HZ), 1); 124*e511e6c3SViresh Kumar return 0; 125c1b40e44SStuart Menefy } 126c1b40e44SStuart Menefy 127c1b40e44SStuart Menefy static int gt_clockevent_set_next_event(unsigned long evt, 128c1b40e44SStuart Menefy struct clock_event_device *unused) 129c1b40e44SStuart Menefy { 130c1b40e44SStuart Menefy gt_compare_set(evt, 0); 131c1b40e44SStuart Menefy return 0; 132c1b40e44SStuart Menefy } 133c1b40e44SStuart Menefy 134c1b40e44SStuart Menefy static irqreturn_t gt_clockevent_interrupt(int irq, void *dev_id) 135c1b40e44SStuart Menefy { 136c1b40e44SStuart Menefy struct clock_event_device *evt = dev_id; 137c1b40e44SStuart Menefy 138c1b40e44SStuart Menefy if (!(readl_relaxed(gt_base + GT_INT_STATUS) & 139c1b40e44SStuart Menefy GT_INT_STATUS_EVENT_FLAG)) 140c1b40e44SStuart Menefy return IRQ_NONE; 141c1b40e44SStuart Menefy 142c1b40e44SStuart Menefy /** 143c1b40e44SStuart Menefy * ERRATA 740657( Global Timer can send 2 interrupts for 144c1b40e44SStuart Menefy * the same event in single-shot mode) 145c1b40e44SStuart Menefy * Workaround: 146c1b40e44SStuart Menefy * Either disable single-shot mode. 147c1b40e44SStuart Menefy * Or 148c1b40e44SStuart Menefy * Modify the Interrupt Handler to avoid the 149c1b40e44SStuart Menefy * offending sequence. This is achieved by clearing 150c1b40e44SStuart Menefy * the Global Timer flag _after_ having incremented 151c1b40e44SStuart Menefy * the Comparator register value to a higher value. 152c1b40e44SStuart Menefy */ 153*e511e6c3SViresh Kumar if (clockevent_state_oneshot(evt)) 154c1b40e44SStuart Menefy gt_compare_set(ULONG_MAX, 0); 155c1b40e44SStuart Menefy 156c1b40e44SStuart Menefy writel_relaxed(GT_INT_STATUS_EVENT_FLAG, gt_base + GT_INT_STATUS); 157c1b40e44SStuart Menefy evt->event_handler(evt); 158c1b40e44SStuart Menefy 159c1b40e44SStuart Menefy return IRQ_HANDLED; 160c1b40e44SStuart Menefy } 161c1b40e44SStuart Menefy 1628c37bb3aSPaul Gortmaker static int gt_clockevents_init(struct clock_event_device *clk) 163c1b40e44SStuart Menefy { 164c1b40e44SStuart Menefy int cpu = smp_processor_id(); 165c1b40e44SStuart Menefy 166c1b40e44SStuart Menefy clk->name = "arm_global_timer"; 1676661039dSSoren Brinkmann clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT | 1686661039dSSoren Brinkmann CLOCK_EVT_FEAT_PERCPU; 169*e511e6c3SViresh Kumar clk->set_state_shutdown = gt_clockevent_shutdown; 170*e511e6c3SViresh Kumar clk->set_state_periodic = gt_clockevent_set_periodic; 171*e511e6c3SViresh Kumar clk->set_state_oneshot = gt_clockevent_shutdown; 172c1b40e44SStuart Menefy clk->set_next_event = gt_clockevent_set_next_event; 173c1b40e44SStuart Menefy clk->cpumask = cpumask_of(cpu); 174c1b40e44SStuart Menefy clk->rating = 300; 175c1b40e44SStuart Menefy clk->irq = gt_ppi; 176c1b40e44SStuart Menefy clockevents_config_and_register(clk, gt_clk_rate, 177c1b40e44SStuart Menefy 1, 0xffffffff); 178c1b40e44SStuart Menefy enable_percpu_irq(clk->irq, IRQ_TYPE_NONE); 179c1b40e44SStuart Menefy return 0; 180c1b40e44SStuart Menefy } 181c1b40e44SStuart Menefy 182c1b40e44SStuart Menefy static void gt_clockevents_stop(struct clock_event_device *clk) 183c1b40e44SStuart Menefy { 184*e511e6c3SViresh Kumar gt_clockevent_shutdown(clk); 185c1b40e44SStuart Menefy disable_percpu_irq(clk->irq); 186c1b40e44SStuart Menefy } 187c1b40e44SStuart Menefy 188c1b40e44SStuart Menefy static cycle_t gt_clocksource_read(struct clocksource *cs) 189c1b40e44SStuart Menefy { 190c1b40e44SStuart Menefy return gt_counter_read(); 191c1b40e44SStuart Menefy } 192c1b40e44SStuart Menefy 193c1b40e44SStuart Menefy static struct clocksource gt_clocksource = { 194c1b40e44SStuart Menefy .name = "arm_global_timer", 195c1b40e44SStuart Menefy .rating = 300, 196c1b40e44SStuart Menefy .read = gt_clocksource_read, 197c1b40e44SStuart Menefy .mask = CLOCKSOURCE_MASK(64), 198c1b40e44SStuart Menefy .flags = CLOCK_SOURCE_IS_CONTINUOUS, 199c1b40e44SStuart Menefy }; 200c1b40e44SStuart Menefy 201c1b40e44SStuart Menefy #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK 202af066fceSStephen Boyd static u64 notrace gt_sched_clock_read(void) 203c1b40e44SStuart Menefy { 204c1b40e44SStuart Menefy return gt_counter_read(); 205c1b40e44SStuart Menefy } 206c1b40e44SStuart Menefy #endif 207c1b40e44SStuart Menefy 208c1b40e44SStuart Menefy static void __init gt_clocksource_init(void) 209c1b40e44SStuart Menefy { 210c1b40e44SStuart Menefy writel(0, gt_base + GT_CONTROL); 211c1b40e44SStuart Menefy writel(0, gt_base + GT_COUNTER0); 212c1b40e44SStuart Menefy writel(0, gt_base + GT_COUNTER1); 213c1b40e44SStuart Menefy /* enables timer on all the cores */ 214c1b40e44SStuart Menefy writel(GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL); 215c1b40e44SStuart Menefy 216c1b40e44SStuart Menefy #ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK 217af066fceSStephen Boyd sched_clock_register(gt_sched_clock_read, 64, gt_clk_rate); 218c1b40e44SStuart Menefy #endif 219c1b40e44SStuart Menefy clocksource_register_hz(>_clocksource, gt_clk_rate); 220c1b40e44SStuart Menefy } 221c1b40e44SStuart Menefy 2228c37bb3aSPaul Gortmaker static int gt_cpu_notify(struct notifier_block *self, unsigned long action, 2238c37bb3aSPaul Gortmaker void *hcpu) 224c1b40e44SStuart Menefy { 225c1b40e44SStuart Menefy switch (action & ~CPU_TASKS_FROZEN) { 226c1b40e44SStuart Menefy case CPU_STARTING: 227c1b40e44SStuart Menefy gt_clockevents_init(this_cpu_ptr(gt_evt)); 228c1b40e44SStuart Menefy break; 229c1b40e44SStuart Menefy case CPU_DYING: 230c1b40e44SStuart Menefy gt_clockevents_stop(this_cpu_ptr(gt_evt)); 231c1b40e44SStuart Menefy break; 232c1b40e44SStuart Menefy } 233c1b40e44SStuart Menefy 234c1b40e44SStuart Menefy return NOTIFY_OK; 235c1b40e44SStuart Menefy } 2368c37bb3aSPaul Gortmaker static struct notifier_block gt_cpu_nb = { 237c1b40e44SStuart Menefy .notifier_call = gt_cpu_notify, 238c1b40e44SStuart Menefy }; 239c1b40e44SStuart Menefy 240c1b40e44SStuart Menefy static void __init global_timer_of_register(struct device_node *np) 241c1b40e44SStuart Menefy { 242c1b40e44SStuart Menefy struct clk *gt_clk; 243c1b40e44SStuart Menefy int err = 0; 244c1b40e44SStuart Menefy 245c1b40e44SStuart Menefy /* 2462cf2ff9fSMatthew Leach * In A9 r2p0 the comparators for each processor with the global timer 247c1b40e44SStuart Menefy * fire when the timer value is greater than or equal to. In previous 248c1b40e44SStuart Menefy * revisions the comparators fired when the timer value was equal to. 249c1b40e44SStuart Menefy */ 250af040ffcSRussell King if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9 2512cf2ff9fSMatthew Leach && (read_cpuid_id() & 0xf0000f) < 0x200000) { 252c1b40e44SStuart Menefy pr_warn("global-timer: non support for this cpu version.\n"); 253c1b40e44SStuart Menefy return; 254c1b40e44SStuart Menefy } 255c1b40e44SStuart Menefy 256c1b40e44SStuart Menefy gt_ppi = irq_of_parse_and_map(np, 0); 257c1b40e44SStuart Menefy if (!gt_ppi) { 258c1b40e44SStuart Menefy pr_warn("global-timer: unable to parse irq\n"); 259c1b40e44SStuart Menefy return; 260c1b40e44SStuart Menefy } 261c1b40e44SStuart Menefy 262c1b40e44SStuart Menefy gt_base = of_iomap(np, 0); 263c1b40e44SStuart Menefy if (!gt_base) { 264c1b40e44SStuart Menefy pr_warn("global-timer: invalid base address\n"); 265c1b40e44SStuart Menefy return; 266c1b40e44SStuart Menefy } 267c1b40e44SStuart Menefy 268c1b40e44SStuart Menefy gt_clk = of_clk_get(np, 0); 269c1b40e44SStuart Menefy if (!IS_ERR(gt_clk)) { 270c1b40e44SStuart Menefy err = clk_prepare_enable(gt_clk); 271c1b40e44SStuart Menefy if (err) 272c1b40e44SStuart Menefy goto out_unmap; 273c1b40e44SStuart Menefy } else { 274c1b40e44SStuart Menefy pr_warn("global-timer: clk not found\n"); 275c1b40e44SStuart Menefy err = -EINVAL; 276c1b40e44SStuart Menefy goto out_unmap; 277c1b40e44SStuart Menefy } 278c1b40e44SStuart Menefy 279c1b40e44SStuart Menefy gt_clk_rate = clk_get_rate(gt_clk); 280c1b40e44SStuart Menefy gt_evt = alloc_percpu(struct clock_event_device); 281c1b40e44SStuart Menefy if (!gt_evt) { 282c1b40e44SStuart Menefy pr_warn("global-timer: can't allocate memory\n"); 283c1b40e44SStuart Menefy err = -ENOMEM; 284c1b40e44SStuart Menefy goto out_clk; 285c1b40e44SStuart Menefy } 286c1b40e44SStuart Menefy 287c1b40e44SStuart Menefy err = request_percpu_irq(gt_ppi, gt_clockevent_interrupt, 288c1b40e44SStuart Menefy "gt", gt_evt); 289c1b40e44SStuart Menefy if (err) { 290c1b40e44SStuart Menefy pr_warn("global-timer: can't register interrupt %d (%d)\n", 291c1b40e44SStuart Menefy gt_ppi, err); 292c1b40e44SStuart Menefy goto out_free; 293c1b40e44SStuart Menefy } 294c1b40e44SStuart Menefy 295c1b40e44SStuart Menefy err = register_cpu_notifier(>_cpu_nb); 296c1b40e44SStuart Menefy if (err) { 297c1b40e44SStuart Menefy pr_warn("global-timer: unable to register cpu notifier.\n"); 298c1b40e44SStuart Menefy goto out_irq; 299c1b40e44SStuart Menefy } 300c1b40e44SStuart Menefy 301c1b40e44SStuart Menefy /* Immediately configure the timer on the boot CPU */ 302c1b40e44SStuart Menefy gt_clocksource_init(); 303c1b40e44SStuart Menefy gt_clockevents_init(this_cpu_ptr(gt_evt)); 304c1b40e44SStuart Menefy 305c1b40e44SStuart Menefy return; 306c1b40e44SStuart Menefy 307c1b40e44SStuart Menefy out_irq: 308c1b40e44SStuart Menefy free_percpu_irq(gt_ppi, gt_evt); 309c1b40e44SStuart Menefy out_free: 310c1b40e44SStuart Menefy free_percpu(gt_evt); 311c1b40e44SStuart Menefy out_clk: 312c1b40e44SStuart Menefy clk_disable_unprepare(gt_clk); 313c1b40e44SStuart Menefy out_unmap: 314c1b40e44SStuart Menefy iounmap(gt_base); 315c1b40e44SStuart Menefy WARN(err, "ARM Global timer register failed (%d)\n", err); 316c1b40e44SStuart Menefy } 317c1b40e44SStuart Menefy 318c1b40e44SStuart Menefy /* Only tested on r2p2 and r3p0 */ 319c1b40e44SStuart Menefy CLOCKSOURCE_OF_DECLARE(arm_gt, "arm,cortex-a9-global-timer", 320c1b40e44SStuart Menefy global_timer_of_register); 321