Path: blob/master/drivers/clocksource/timer-armada-370-xp.c
26278 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)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)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 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 unsigned long armada_370_delay_timer_read(void)230{231return ~readl(timer_base + TIMER0_VAL_OFF);232}233234static struct delay_timer armada_370_delay_timer = {235.read_current_timer = armada_370_delay_timer_read,236};237238static int __init armada_370_xp_timer_common_init(struct device_node *np)239{240u32 clr = 0, set = 0;241int res;242243timer_base = of_iomap(np, 0);244if (!timer_base) {245pr_err("Failed to iomap\n");246return -ENXIO;247}248249local_base = of_iomap(np, 1);250if (!local_base) {251pr_err("Failed to iomap\n");252return -ENXIO;253}254255if (timer25Mhz) {256set = TIMER0_25MHZ;257enable_mask = TIMER0_EN;258} else {259clr = TIMER0_25MHZ;260enable_mask = TIMER0_EN | TIMER0_DIV(TIMER_DIVIDER_SHIFT);261}262atomic_io_modify(timer_base + TIMER_CTRL_OFF, clr | set, set);263local_timer_ctrl_clrset(clr, set);264265/*266* We use timer 0 as clocksource, and private(local) timer 0267* for clockevents268*/269armada_370_xp_clkevt_irq = irq_of_parse_and_map(np, 4);270271ticks_per_jiffy = (timer_clk + HZ / 2) / HZ;272273/*274* Setup free-running clocksource timer (interrupts275* disabled).276*/277writel(0xffffffff, timer_base + TIMER0_VAL_OFF);278writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF);279280atomic_io_modify(timer_base + TIMER_CTRL_OFF,281TIMER0_RELOAD_EN | enable_mask,282TIMER0_RELOAD_EN | enable_mask);283284armada_370_delay_timer.freq = timer_clk;285register_current_timer_delay(&armada_370_delay_timer);286287/*288* Set scale and timer for sched_clock.289*/290sched_clock_register(armada_370_xp_read_sched_clock, 32, timer_clk);291292res = clocksource_mmio_init(timer_base + TIMER0_VAL_OFF,293"armada_370_xp_clocksource",294timer_clk, 300, 32, clocksource_mmio_readl_down);295if (res) {296pr_err("Failed to initialize clocksource mmio\n");297return res;298}299300armada_370_xp_evt = alloc_percpu(struct clock_event_device);301if (!armada_370_xp_evt)302return -ENOMEM;303304/*305* Setup clockevent timer (interrupt-driven).306*/307res = request_percpu_irq(armada_370_xp_clkevt_irq,308armada_370_xp_timer_interrupt,309"armada_370_xp_per_cpu_tick",310armada_370_xp_evt);311/* Immediately configure the timer on the boot CPU */312if (res) {313pr_err("Failed to request percpu irq\n");314return res;315}316317res = cpuhp_setup_state(CPUHP_AP_ARMADA_TIMER_STARTING,318"clockevents/armada:starting",319armada_370_xp_timer_starting_cpu,320armada_370_xp_timer_dying_cpu);321if (res) {322pr_err("Failed to setup hotplug state and timer\n");323return res;324}325326register_syscore_ops(&armada_370_xp_timer_syscore_ops);327328return 0;329}330331static int __init armada_xp_timer_init(struct device_node *np)332{333struct clk *clk = of_clk_get_by_name(np, "fixed");334int ret;335336if (IS_ERR(clk)) {337pr_err("Failed to get clock\n");338return PTR_ERR(clk);339}340341ret = clk_prepare_enable(clk);342if (ret)343return ret;344345timer_clk = clk_get_rate(clk);346347return armada_370_xp_timer_common_init(np);348}349TIMER_OF_DECLARE(armada_xp, "marvell,armada-xp-timer",350armada_xp_timer_init);351352static int __init armada_375_timer_init(struct device_node *np)353{354struct clk *clk;355int ret;356357clk = of_clk_get_by_name(np, "fixed");358if (!IS_ERR(clk)) {359ret = clk_prepare_enable(clk);360if (ret)361return ret;362timer_clk = clk_get_rate(clk);363} else {364365/*366* This fallback is required in order to retain proper367* devicetree backwards compatibility.368*/369clk = of_clk_get(np, 0);370371/* Must have at least a clock */372if (IS_ERR(clk)) {373pr_err("Failed to get clock\n");374return PTR_ERR(clk);375}376377ret = clk_prepare_enable(clk);378if (ret)379return ret;380381timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;382timer25Mhz = false;383}384385return armada_370_xp_timer_common_init(np);386}387TIMER_OF_DECLARE(armada_375, "marvell,armada-375-timer",388armada_375_timer_init);389390static int __init armada_370_timer_init(struct device_node *np)391{392struct clk *clk;393int ret;394395clk = of_clk_get(np, 0);396if (IS_ERR(clk)) {397pr_err("Failed to get clock\n");398return PTR_ERR(clk);399}400401ret = clk_prepare_enable(clk);402if (ret)403return ret;404405timer_clk = clk_get_rate(clk) / TIMER_DIVIDER;406timer25Mhz = false;407408return armada_370_xp_timer_common_init(np);409}410TIMER_OF_DECLARE(armada_370, "marvell,armada-370-timer",411armada_370_timer_init);412413414