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