Path: blob/master/drivers/clocksource/arm_global_timer.c
29266 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(unsigned int psv)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, psv - 1) |272GT_CONTROL_TIMER_ENABLE, gt_base + GT_CONTROL);273274#ifdef CONFIG_CLKSRC_ARM_GLOBAL_TIMER_SCHED_CLOCK275sched_clock_register(gt_sched_clock_read, 64, gt_target_rate);276#endif277return clocksource_register_hz(>_clocksource, gt_target_rate);278}279280static int gt_clk_rate_change_cb(struct notifier_block *nb,281unsigned long event, void *data)282{283struct clk_notifier_data *ndata = data;284285switch (event) {286case PRE_RATE_CHANGE:287{288unsigned long psv;289290psv = DIV_ROUND_CLOSEST(ndata->new_rate, gt_target_rate);291if (!psv ||292abs(gt_target_rate - (ndata->new_rate / psv)) > MAX_F_ERR)293return NOTIFY_BAD;294295psv--;296297/* prescaler within legal range? */298if (!FIELD_FIT(GT_CONTROL_PRESCALER_MASK, psv))299return NOTIFY_BAD;300301/*302* store timer clock ctrl register so we can restore it in case303* of an abort.304*/305gt_psv_bck = gt_read_presc();306gt_psv_new = psv;307/* scale down: adjust divider in post-change notification */308if (ndata->new_rate < ndata->old_rate)309return NOTIFY_DONE;310311/* scale up: adjust divider now - before frequency change */312gt_write_presc(psv);313break;314}315case POST_RATE_CHANGE:316/* scale up: pre-change notification did the adjustment */317if (ndata->new_rate > ndata->old_rate)318return NOTIFY_OK;319320/* scale down: adjust divider now - after frequency change */321gt_write_presc(gt_psv_new);322break;323324case ABORT_RATE_CHANGE:325/* we have to undo the adjustment in case we scale up */326if (ndata->new_rate < ndata->old_rate)327return NOTIFY_OK;328329/* restore original register value */330gt_write_presc(gt_psv_bck);331break;332default:333return NOTIFY_DONE;334}335336return NOTIFY_DONE;337}338339struct gt_prescaler_config {340const char *compatible;341unsigned long prescaler;342};343344static const struct gt_prescaler_config gt_prescaler_configs[] = {345/*346* On am43 the global timer clock is a child of the clock used for CPU347* OPPs, so the initial prescaler has to be compatible with all OPPs348* which are 300, 600, 720, 800 and 1000 with a fixed divider of 2, this349* gives us a GCD of 10. Initial frequency is 1000, so the prescaler is350* 50.351*/352{ .compatible = "ti,am43", .prescaler = 50 },353{ .compatible = "xlnx,zynq-7000", .prescaler = 2 },354{ .compatible = NULL }355};356357static unsigned long gt_get_initial_prescaler_value(struct device_node *np)358{359const struct gt_prescaler_config *config;360361if (CONFIG_ARM_GT_INITIAL_PRESCALER_VAL != 0)362return CONFIG_ARM_GT_INITIAL_PRESCALER_VAL;363364for (config = gt_prescaler_configs; config->compatible; config++) {365if (of_machine_is_compatible(config->compatible))366return config->prescaler;367}368369return 1;370}371372static int __init global_timer_of_register(struct device_node *np)373{374struct clk *gt_clk;375static unsigned long gt_clk_rate;376int err;377unsigned long psv;378379/*380* In A9 r2p0 the comparators for each processor with the global timer381* fire when the timer value is greater than or equal to. In previous382* revisions the comparators fired when the timer value was equal to.383*/384if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9385&& (read_cpuid_id() & 0xf0000f) < 0x200000) {386pr_warn("global-timer: non support for this cpu version.\n");387return -ENOSYS;388}389390gt_ppi = irq_of_parse_and_map(np, 0);391if (!gt_ppi) {392pr_warn("global-timer: unable to parse irq\n");393return -EINVAL;394}395396gt_base = of_iomap(np, 0);397if (!gt_base) {398pr_warn("global-timer: invalid base address\n");399return -ENXIO;400}401402gt_clk = of_clk_get(np, 0);403if (!IS_ERR(gt_clk)) {404err = clk_prepare_enable(gt_clk);405if (err)406goto out_unmap;407} else {408pr_warn("global-timer: clk not found\n");409err = -EINVAL;410goto out_unmap;411}412413psv = gt_get_initial_prescaler_value(np);414gt_clk_rate = clk_get_rate(gt_clk);415gt_target_rate = gt_clk_rate / psv;416gt_clk_rate_change_nb.notifier_call =417gt_clk_rate_change_cb;418err = clk_notifier_register(gt_clk, >_clk_rate_change_nb);419if (err) {420pr_warn("Unable to register clock notifier\n");421goto out_clk;422}423424gt_evt = alloc_percpu(struct clock_event_device);425if (!gt_evt) {426pr_warn("global-timer: can't allocate memory\n");427err = -ENOMEM;428goto out_clk_nb;429}430431err = request_percpu_irq(gt_ppi, gt_clockevent_interrupt,432"gt", gt_evt);433if (err) {434pr_warn("global-timer: can't register interrupt %d (%d)\n",435gt_ppi, err);436goto out_free;437}438439/* Register and immediately configure the timer on the boot CPU */440err = gt_clocksource_init(psv);441if (err)442goto out_irq;443444err = cpuhp_setup_state(CPUHP_AP_ARM_GLOBAL_TIMER_STARTING,445"clockevents/arm/global_timer:starting",446gt_starting_cpu, gt_dying_cpu);447if (err)448goto out_irq;449450gt_delay_timer_init();451452return 0;453454out_irq:455free_percpu_irq(gt_ppi, gt_evt);456out_free:457free_percpu(gt_evt);458out_clk_nb:459clk_notifier_unregister(gt_clk, >_clk_rate_change_nb);460out_clk:461clk_disable_unprepare(gt_clk);462out_unmap:463iounmap(gt_base);464WARN(err, "ARM Global timer register failed (%d)\n", err);465466return err;467}468469/* Only tested on r2p2 and r3p0 */470TIMER_OF_DECLARE(arm_gt, "arm,cortex-a9-global-timer",471global_timer_of_register);472473474