Path: blob/master/drivers/clocksource/arm_global_timer.c
26278 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* drivers/clocksource/arm_global_timer.c3*4* Copyright (C) 2013 STMicroelectronics (R&D) Limited.5* Author: Stuart Menefy <[email protected]>6* Author: Srinivas Kandagatla <[email protected]>7*/89#include <linux/init.h>10#include <linux/interrupt.h>11#include <linux/bitfield.h>12#include <linux/clocksource.h>13#include <linux/clockchips.h>14#include <linux/cpu.h>15#include <linux/clk.h>16#include <linux/delay.h>17#include <linux/err.h>18#include <linux/io.h>19#include <linux/of.h>20#include <linux/of_irq.h>21#include <linux/of_address.h>22#include <linux/sched_clock.h>2324#include <asm/cputype.h>2526#define GT_COUNTER0 0x0027#define GT_COUNTER1 0x042829#define GT_CONTROL 0x0830#define GT_CONTROL_TIMER_ENABLE BIT(0) /* this bit is NOT banked */31#define GT_CONTROL_COMP_ENABLE BIT(1) /* banked */32#define GT_CONTROL_IRQ_ENABLE BIT(2) /* banked */33#define GT_CONTROL_AUTO_INC BIT(3) /* banked */34#define GT_CONTROL_PRESCALER_MASK GENMASK(15, 8)3536#define GT_INT_STATUS 0x0c37#define GT_INT_STATUS_EVENT_FLAG BIT(0)3839#define GT_COMP0 0x1040#define GT_COMP1 0x1441#define GT_AUTO_INC 0x184243#define MAX_F_ERR 5044/*45* We are expecting to be clocked by the ARM peripheral clock.46*47* Note: it is assumed we are using a prescaler value of zero, so this is48* the units for all operations.49*/50static void __iomem *gt_base;51static struct notifier_block gt_clk_rate_change_nb;52static u32 gt_psv_new, gt_psv_bck;53static unsigned long gt_target_rate;54static int gt_ppi;55static struct clock_event_device __percpu *gt_evt;5657/*58* To get the value from the Global Timer Counter register proceed as follows:59* 1. Read the upper 32-bit timer counter register60* 2. Read the lower 32-bit timer counter register61* 3. Read the upper 32-bit timer counter register again. If the value is62* different to the 32-bit upper value read previously, go back to step 2.63* Otherwise the 64-bit timer counter value is correct.64*/65static u64 notrace _gt_counter_read(void)66{67u64 counter;68u32 lower;69u32 upper, old_upper;7071upper = readl_relaxed(gt_base + GT_COUNTER1);72do {73old_upper = upper;74lower = readl_relaxed(gt_base + GT_COUNTER0);75upper = readl_relaxed(gt_base + GT_COUNTER1);76} while (upper != old_upper);7778counter = upper;79counter <<= 32;80counter |= lower;81return counter;82}8384static u64 gt_counter_read(void)85{86return _gt_counter_read();87}8889/*90* To ensure that updates to comparator value register do not set the91* Interrupt Status Register proceed as follows:92* 1. Clear the Comp Enable bit in the Timer Control Register.93* 2. Write the lower 32-bit Comparator Value Register.94* 3. Write the upper 32-bit Comparator Value Register.95* 4. Set the Comp Enable bit and, if necessary, the IRQ enable bit.96*/97static void gt_compare_set(unsigned long delta, int periodic)98{99u64 counter = gt_counter_read();100unsigned long ctrl;101102counter += delta;103ctrl = readl(gt_base + GT_CONTROL);104ctrl &= ~(GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE |105GT_CONTROL_AUTO_INC);106ctrl |= GT_CONTROL_TIMER_ENABLE;107writel_relaxed(ctrl, gt_base + GT_CONTROL);108writel_relaxed(lower_32_bits(counter), gt_base + GT_COMP0);109writel_relaxed(upper_32_bits(counter), gt_base + GT_COMP1);110111if (periodic) {112writel_relaxed(delta, gt_base + GT_AUTO_INC);113ctrl |= GT_CONTROL_AUTO_INC;114}115116ctrl |= GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE;117writel_relaxed(ctrl, gt_base + GT_CONTROL);118}119120static int gt_clockevent_shutdown(struct clock_event_device *evt)121{122unsigned long ctrl;123124ctrl = readl(gt_base + GT_CONTROL);125ctrl &= ~(GT_CONTROL_COMP_ENABLE | GT_CONTROL_IRQ_ENABLE |126GT_CONTROL_AUTO_INC);127writel(ctrl, gt_base + GT_CONTROL);128return 0;129}130131static int gt_clockevent_set_periodic(struct clock_event_device *evt)132{133gt_compare_set(DIV_ROUND_CLOSEST(gt_target_rate, HZ), 1);134return 0;135}136137static int gt_clockevent_set_next_event(unsigned long evt,138struct clock_event_device *unused)139{140gt_compare_set(evt, 0);141return 0;142}143144static irqreturn_t gt_clockevent_interrupt(int irq, void *dev_id)145{146struct clock_event_device *evt = dev_id;147148if (!(readl_relaxed(gt_base + GT_INT_STATUS) &149GT_INT_STATUS_EVENT_FLAG))150return IRQ_NONE;151152/**153* ERRATA 740657( Global Timer can send 2 interrupts for154* the same event in single-shot mode)155* Workaround:156* Either disable single-shot mode.157* Or158* Modify the Interrupt Handler to avoid the159* offending sequence. This is achieved by clearing160* the Global Timer flag _after_ having incremented161* the Comparator register value to a higher value.162*/163if (clockevent_state_oneshot(evt))164gt_compare_set(ULONG_MAX, 0);165166writel_relaxed(GT_INT_STATUS_EVENT_FLAG, gt_base + GT_INT_STATUS);167evt->event_handler(evt);168169return IRQ_HANDLED;170}171172static int gt_starting_cpu(unsigned int cpu)173{174struct clock_event_device *clk = this_cpu_ptr(gt_evt);175176clk->name = "arm_global_timer";177clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |178CLOCK_EVT_FEAT_PERCPU;179clk->set_state_shutdown = gt_clockevent_shutdown;180clk->set_state_periodic = gt_clockevent_set_periodic;181clk->set_state_oneshot = gt_clockevent_shutdown;182clk->set_state_oneshot_stopped = gt_clockevent_shutdown;183clk->set_next_event = gt_clockevent_set_next_event;184clk->cpumask = cpumask_of(cpu);185clk->rating = 300;186clk->irq = gt_ppi;187clockevents_config_and_register(clk, gt_target_rate,1881, 0xffffffff);189enable_percpu_irq(clk->irq, IRQ_TYPE_NONE);190return 0;191}192193static int gt_dying_cpu(unsigned int cpu)194{195struct clock_event_device *clk = this_cpu_ptr(gt_evt);196197disable_percpu_irq(clk->irq);198return 0;199}200201static u64 gt_clocksource_read(struct clocksource *cs)202{203return gt_counter_read();204}205206static void gt_resume(struct clocksource *cs)207{208unsigned long ctrl;209210ctrl = readl(gt_base + GT_CONTROL);211if (!(ctrl & GT_CONTROL_TIMER_ENABLE))212/* re-enable timer on resume */213writel(GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);214}215216static struct clocksource gt_clocksource = {217.name = "arm_global_timer",218.rating = 300,219.read = gt_clocksource_read,220.mask = CLOCKSOURCE_MASK(64),221.flags = CLOCK_SOURCE_IS_CONTINUOUS,222.resume = gt_resume,223};224225#ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK226static u64 notrace gt_sched_clock_read(void)227{228return _gt_counter_read();229}230#endif231232static unsigned long gt_read_long(void)233{234return readl_relaxed(gt_base + GT_COUNTER0);235}236237static struct delay_timer gt_delay_timer = {238.read_current_timer = gt_read_long,239};240241static void gt_write_presc(u32 psv)242{243u32 reg;244245reg = readl(gt_base + GT_CONTROL);246reg &= ~GT_CONTROL_PRESCALER_MASK;247reg |= FIELD_PREP(GT_CONTROL_PRESCALER_MASK, psv);248writel(reg, gt_base + GT_CONTROL);249}250251static u32 gt_read_presc(void)252{253u32 reg;254255reg = readl(gt_base + GT_CONTROL);256return FIELD_GET(GT_CONTROL_PRESCALER_MASK, reg);257}258259static void __init gt_delay_timer_init(void)260{261gt_delay_timer.freq = gt_target_rate;262register_current_timer_delay(>_delay_timer);263}264265static int __init gt_clocksource_init(void)266{267writel(0, gt_base + GT_CONTROL);268writel(0, gt_base + GT_COUNTER0);269writel(0, gt_base + GT_COUNTER1);270/* set prescaler and enable timer on all the cores */271writel(FIELD_PREP(GT_CONTROL_PRESCALER_MASK,272CONFIG_ARM_GT_INITIAL_PRESCALER_VAL - 1) |273GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);274275#ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK276sched_clock_register(gt_sched_clock_read, 64, gt_target_rate);277#endif278return clocksource_register_hz(>_clocksource, gt_target_rate);279}280281static int gt_clk_rate_change_cb(struct notifier_block *nb,282unsigned long event, void *data)283{284struct clk_notifier_data *ndata = data;285286switch (event) {287case PRE_RATE_CHANGE:288{289unsigned long psv;290291psv = DIV_ROUND_CLOSEST(ndata->new_rate, gt_target_rate);292if (!psv ||293abs(gt_target_rate - (ndata->new_rate / psv)) > MAX_F_ERR)294return NOTIFY_BAD;295296psv--;297298/* prescaler within legal range? */299if (!FIELD_FIT(GT_CONTROL_PRESCALER_MASK, psv))300return NOTIFY_BAD;301302/*303* store timer clock ctrl register so we can restore it in case304* of an abort.305*/306gt_psv_bck = gt_read_presc();307gt_psv_new = psv;308/* scale down: adjust divider in post-change notification */309if (ndata->new_rate < ndata->old_rate)310return NOTIFY_DONE;311312/* scale up: adjust divider now - before frequency change */313gt_write_presc(psv);314break;315}316case POST_RATE_CHANGE:317/* scale up: pre-change notification did the adjustment */318if (ndata->new_rate > ndata->old_rate)319return NOTIFY_OK;320321/* scale down: adjust divider now - after frequency change */322gt_write_presc(gt_psv_new);323break;324325case ABORT_RATE_CHANGE:326/* we have to undo the adjustment in case we scale up */327if (ndata->new_rate < ndata->old_rate)328return NOTIFY_OK;329330/* restore original register value */331gt_write_presc(gt_psv_bck);332break;333default:334return NOTIFY_DONE;335}336337return NOTIFY_DONE;338}339340static int __init global_timer_of_register(struct device_node *np)341{342struct clk *gt_clk;343static unsigned long gt_clk_rate;344int err;345346/*347* In A9 r2p0 the comparators for each processor with the global timer348* fire when the timer value is greater than or equal to. In previous349* revisions the comparators fired when the timer value was equal to.350*/351if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9352&& (read_cpuid_id() & 0xf0000f) < 0x200000) {353pr_warn("global-timer: non support for this cpu version.\n");354return -ENOSYS;355}356357gt_ppi = irq_of_parse_and_map(np, 0);358if (!gt_ppi) {359pr_warn("global-timer: unable to parse irq\n");360return -EINVAL;361}362363gt_base = of_iomap(np, 0);364if (!gt_base) {365pr_warn("global-timer: invalid base address\n");366return -ENXIO;367}368369gt_clk = of_clk_get(np, 0);370if (!IS_ERR(gt_clk)) {371err = clk_prepare_enable(gt_clk);372if (err)373goto out_unmap;374} else {375pr_warn("global-timer: clk not found\n");376err = -EINVAL;377goto out_unmap;378}379380gt_clk_rate = clk_get_rate(gt_clk);381gt_target_rate = gt_clk_rate / CONFIG_ARM_GT_INITIAL_PRESCALER_VAL;382gt_clk_rate_change_nb.notifier_call =383gt_clk_rate_change_cb;384err = clk_notifier_register(gt_clk, >_clk_rate_change_nb);385if (err) {386pr_warn("Unable to register clock notifier\n");387goto out_clk;388}389390gt_evt = alloc_percpu(struct clock_event_device);391if (!gt_evt) {392pr_warn("global-timer: can't allocate memory\n");393err = -ENOMEM;394goto out_clk_nb;395}396397err = request_percpu_irq(gt_ppi, gt_clockevent_interrupt,398"gt", gt_evt);399if (err) {400pr_warn("global-timer: can't register interrupt %d (%d)\n",401gt_ppi, err);402goto out_free;403}404405/* Register and immediately configure the timer on the boot CPU */406err = gt_clocksource_init();407if (err)408goto out_irq;409410err = cpuhp_setup_state(CPUHP_AP_ARM_GLOBAL_TIMER_STARTING,411"clockevents/arm/global_timer:starting",412gt_starting_cpu, gt_dying_cpu);413if (err)414goto out_irq;415416gt_delay_timer_init();417418return 0;419420out_irq:421free_percpu_irq(gt_ppi, gt_evt);422out_free:423free_percpu(gt_evt);424out_clk_nb:425clk_notifier_unregister(gt_clk, >_clk_rate_change_nb);426out_clk:427clk_disable_unprepare(gt_clk);428out_unmap:429iounmap(gt_base);430WARN(err, "ARM Global timer register failed (%d)\n", err);431432return err;433}434435/* Only tested on r2p2 and r3p0 */436TIMER_OF_DECLARE(arm_gt, "arm,cortex-a9-global-timer",437global_timer_of_register);438439440