Path: blob/master/drivers/clocksource/timer-armada-370-xp.c
49885 views
// SPDX-License-Identifier: GPL-2.01/*2* Marvell Armada 370/XP SoC timer handling.3*4* Copyright (C) 2012 Marvell5*6* Lior Amsalem <[email protected]>7* Gregory CLEMENT <[email protected]>8* Thomas Petazzoni <[email protected]>9*10* Timer 0 is used as free-running clocksource, while timer 1 is11* used as clock_event_device.12*13* ---14* Clocksource driver for Armada 370 and Armada XP SoC.15* This driver implements one compatible string for each SoC, given16* each has its own characteristics:17*18* * Armada 370 has no 25 MHz fixed timer.19*20* * Armada XP cannot work properly without such 25 MHz fixed timer as21* doing otherwise leads to using a clocksource whose frequency varies22* when doing cpufreq frequency changes.23*24* See Documentation/devicetree/bindings/timer/marvell,armada-370-xp-timer.txt25*/2627#include <linux/init.h>28#include <linux/platform_device.h>29#include <linux/kernel.h>30#include <linux/clk.h>31#include <linux/cpu.h>32#include <linux/timer.h>33#include <linux/clockchips.h>34#include <linux/interrupt.h>35#include <linux/of.h>36#include <linux/of_irq.h>37#include <linux/of_address.h>38#include <linux/irq.h>39#include <linux/module.h>40#include <linux/sched_clock.h>41#include <linux/percpu.h>42#include <linux/syscore_ops.h>4344#include <asm/delay.h>4546/*47* Timer block registers.48*/49#define TIMER_CTRL_OFF 0x000050#define TIMER0_EN BIT(0)51#define TIMER0_RELOAD_EN BIT(1)52#define TIMER0_25MHZ BIT(11)53#define TIMER0_DIV(div) ((div) << 19)54#define TIMER1_EN BIT(2)55#define TIMER1_RELOAD_EN BIT(3)56#define TIMER1_25MHZ BIT(12)57#define TIMER1_DIV(div) ((div) << 22)58#define TIMER_EVENTS_STATUS 0x000459#define TIMER0_CLR_MASK (~0x1)60#define TIMER1_CLR_MASK (~0x100)61#define TIMER0_RELOAD_OFF 0x001062#define TIMER0_VAL_OFF 0x001463#define TIMER1_RELOAD_OFF 0x001864#define TIMER1_VAL_OFF 0x001c6566#define LCL_TIMER_EVENTS_STATUS 0x002867/* Global timers are connected to the coherency fabric clock, and the68below divider reduces their incrementing frequency. */69#define TIMER_DIVIDER_SHIFT 570#define TIMER_DIVIDER (1 << TIMER_DIVIDER_SHIFT)7172/*73* SoC-specific data.74*/75static void __iomem *timer_base, *local_base;76static unsigned int timer_clk;77static bool timer25Mhz = true;78static u32 enable_mask;7980/*81* Number of timer ticks per jiffy.82*/83static u32 ticks_per_jiffy;8485static struct clock_event_device __percpu *armada_370_xp_evt;8687static void local_timer_ctrl_clrset(u32 clr, u32 set)88{89writel((readl(local_base + TIMER_CTRL_OFF) & ~clr) | set,90local_base + TIMER_CTRL_OFF);91}9293static u64 notrace armada_370_xp_read_sched_clock(void)94{95return ~readl(timer_base + TIMER0_VAL_OFF);96}9798/*99* Clockevent handling.100*/101static int102armada_370_xp_clkevt_next_event(unsigned long delta,103struct clock_event_device *dev)104{105/*106* Clear clockevent timer interrupt.107*/108writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);109110/*111* Setup new clockevent timer value.112*/113writel(delta, local_base + TIMER0_VAL_OFF);114115/*116* Enable the timer.117*/118local_timer_ctrl_clrset(TIMER0_RELOAD_EN, enable_mask);119return 0;120}121122static int armada_370_xp_clkevt_shutdown(struct clock_event_device *evt)123{124/*125* Disable timer.126*/127local_timer_ctrl_clrset(TIMER0_EN, 0);128129/*130* ACK pending timer interrupt.131*/132writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);133return 0;134}135136static int armada_370_xp_clkevt_set_periodic(struct clock_event_device *evt)137{138/*139* Setup timer to fire at 1/HZ intervals.140*/141writel(ticks_per_jiffy - 1, local_base + TIMER0_RELOAD_OFF);142writel(ticks_per_jiffy - 1, local_base + TIMER0_VAL_OFF);143144/*145* Enable timer.146*/147local_timer_ctrl_clrset(0, TIMER0_RELOAD_EN | enable_mask);148return 0;149}150151static int armada_370_xp_clkevt_irq;152153static irqreturn_t armada_370_xp_timer_interrupt(int irq, void *dev_id)154{155/*156* ACK timer interrupt and call event handler.157*/158struct clock_event_device *evt = dev_id;159160writel(TIMER0_CLR_MASK, local_base + LCL_TIMER_EVENTS_STATUS);161evt->event_handler(evt);162163return IRQ_HANDLED;164}165166/*167* Setup the local clock events for a CPU.168*/169static int armada_370_xp_timer_starting_cpu(unsigned int cpu)170{171struct clock_event_device *evt = per_cpu_ptr(armada_370_xp_evt, cpu);172u32 clr = 0, set = 0;173174if (timer25Mhz)175set = TIMER0_25MHZ;176else177clr = TIMER0_25MHZ;178local_timer_ctrl_clrset(clr, set);179180evt->name = "armada_370_xp_per_cpu_tick";181evt->features = CLOCK_EVT_FEAT_ONESHOT |182CLOCK_EVT_FEAT_PERIODIC;183evt->shift = 32;184evt->rating = 300;185evt->set_next_event = armada_370_xp_clkevt_next_event;186evt->set_state_shutdown = armada_370_xp_clkevt_shutdown;187evt->set_state_periodic = armada_370_xp_clkevt_set_periodic;188evt->set_state_oneshot = armada_370_xp_clkevt_shutdown;189evt->tick_resume = armada_370_xp_clkevt_shutdown;190evt->irq = armada_370_xp_clkevt_irq;191evt->cpumask = cpumask_of(cpu);192193clockevents_config_and_register(evt, timer_clk, 1, 0xfffffffe);194enable_percpu_irq(evt->irq, 0);195196return 0;197}198199static int armada_370_xp_timer_dying_cpu(unsigned int cpu)200{201struct clock_event_device *evt = per_cpu_ptr(armada_370_xp_evt, cpu);202203disable_percpu_irq(evt->irq);204return 0;205}206207static u32 timer0_ctrl_reg, timer0_local_ctrl_reg;208209static int armada_370_xp_timer_suspend(void *data)210{211timer0_ctrl_reg = readl(timer_base + TIMER_CTRL_OFF);212timer0_local_ctrl_reg = readl(local_base + TIMER_CTRL_OFF);213return 0;214}215216static void armada_370_xp_timer_resume(void *data)217{218writel(0xffffffff, timer_base + TIMER0_VAL_OFF);219writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);220writel(timer0_ctrl_reg, timer_base + TIMER_CTRL_OFF);221writel(timer0_local_ctrl_reg, local_base + TIMER_CTRL_OFF);222}223224static const struct syscore_ops armada_370_xp_timer_syscore_ops = {225.suspend = armada_370_xp_timer_suspend,226.resume = armada_370_xp_timer_resume,227};228229static struct syscore armada_370_xp_timer_syscore = {230.ops = &armada_370_xp_timer_syscore_ops,231};232233static unsigned long armada_370_delay_timer_read(void)234{235return ~readl(timer_base + TIMER0_VAL_OFF);236}237238static struct delay_timer armada_370_delay_timer = {239.read_current_timer = armada_370_delay_timer_read,240};241242static int __init armada_370_xp_timer_common_init(struct device_node *np)243{244u32 clr = 0, set = 0;245int res;246247timer_base = of_iomap(np, 0);248if (!timer_base) {249pr_err("Failed to iomap\n");250return -ENXIO;251}252253local_base = of_iomap(np, 1);254if (!local_base) {255pr_err("Failed to iomap\n");256return -ENXIO;257}258259if (timer25Mhz) {260set = TIMER0_25MHZ;261enable_mask = TIMER0_EN;262} else {263clr = TIMER0_25MHZ;264enable_mask = TIMER0_EN | TIMER0_DIV(TIMER_DIVIDER_SHIFT);265}266atomic_io_modify(timer_base + TIMER_CTRL_OFF, clr | set, set);267local_timer_ctrl_clrset(clr, set);268269/*270* We use timer 0 as clocksource, and private(local) timer 0271* for clockevents272*/273armada_370_xp_clkevt_irq = irq_of_parse_and_map(np, 4);274275ticks_per_jiffy = (timer_clk + HZ / 2) / HZ;276277/*278* Setup free-running clocksource timer (interrupts279* disabled).280*/281writel(0xffffffff, timer_base + TIMER0_VAL_OFF);282writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);283284atomic_io_modify(timer_base + TIMER_CTRL_OFF,285TIMER0_RELOAD_EN | enable_mask,286TIMER0_RELOAD_EN | enable_mask);287288armada_370_delay_timer.freq = timer_clk;289register_current_timer_delay(&armada_370_delay_timer);290291/*292* Set scale and timer for sched_clock.293*/294sched_clock_register(armada_370_xp_read_sched_clock, 32, timer_clk);295296res = clocksource_mmio_init(timer_base + TIMER0_VAL_OFF,297"armada_370_xp_clocksource",298timer_clk, 300, 32, clocksource_mmio_readl_down);299if (res) {300pr_err("Failed to initialize clocksource mmio\n");301return res;302}303304armada_370_xp_evt = alloc_percpu(struct clock_event_device);305if (!armada_370_xp_evt)306return -ENOMEM;307308/*309* Setup clockevent timer (interrupt-driven).310*/311res = request_percpu_irq(armada_370_xp_clkevt_irq,312armada_370_xp_timer_interrupt,313"armada_370_xp_per_cpu_tick",314armada_370_xp_evt);315/* Immediately configure the timer on the boot CPU */316if (res) {317pr_err("Failed to request percpu irq\n");318return res;319}320321res = cpuhp_setup_state(CPUHP_AP_ARMADA_TIMER_STARTING,322"clockevents/armada:starting",323armada_370_xp_timer_starting_cpu,324armada_370_xp_timer_dying_cpu);325if (res) {326pr_err("Failed to setup hotplug state and timer\n");327return res;328}329330register_syscore(&armada_370_xp_timer_syscore);331332return 0;333}334335static int __init armada_xp_timer_init(struct device_node *np)336{337struct clk *clk = of_clk_get_by_name(np, "fixed");338int ret;339340if (IS_ERR(clk)) {341pr_err("Failed to get clock\n");342return PTR_ERR(clk);343}344345ret = clk_prepare_enable(clk);346if (ret)347return ret;348349timer_clk = clk_get_rate(clk);350351return armada_370_xp_timer_common_init(np);352}353TIMER_OF_DECLARE(armada_xp, "marvell,armada-xp-timer",354armada_xp_timer_init);355356static int __init armada_375_timer_init(struct device_node *np)357{358struct clk *clk;359int ret;360361clk = of_clk_get_by_name(np, "fixed");362if (!IS_ERR(clk)) {363ret = clk_prepare_enable(clk);364if (ret)365return ret;366timer_clk = clk_get_rate(clk);367} else {368369/*370* This fallback is required in order to retain proper371* devicetree backwards compatibility.372*/373clk = of_clk_get(np, 0);374375/* Must have at least a clock */376if (IS_ERR(clk)) {377pr_err("Failed to get clock\n");378return PTR_ERR(clk);379}380381ret = clk_prepare_enable(clk);382if (ret)383return ret;384385timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;386timer25Mhz = false;387}388389return armada_370_xp_timer_common_init(np);390}391TIMER_OF_DECLARE(armada_375, "marvell,armada-375-timer",392armada_375_timer_init);393394static int __init armada_370_timer_init(struct device_node *np)395{396struct clk *clk;397int ret;398399clk = of_clk_get(np, 0);400if (IS_ERR(clk)) {401pr_err("Failed to get clock\n");402return PTR_ERR(clk);403}404405ret = clk_prepare_enable(clk);406if (ret)407return ret;408409timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;410timer25Mhz = false;411412return armada_370_xp_timer_common_init(np);413}414TIMER_OF_DECLARE(armada_370, "marvell,armada-370-timer",415armada_370_timer_init);416417418