1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2eb36f293SDaniel Lezcano /* 3eb36f293SDaniel Lezcano * arch/arm/mach-pxa/time.c 4eb36f293SDaniel Lezcano * 5eb36f293SDaniel Lezcano * PXA clocksource, clockevents, and OST interrupt handlers. 6eb36f293SDaniel Lezcano * Copyright (c) 2007 by Bill Gatliff <bgat@billgatliff.com>. 7eb36f293SDaniel Lezcano * 8eb36f293SDaniel Lezcano * Derived from Nicolas Pitre's PXA timer handler Copyright (c) 2001 9eb36f293SDaniel Lezcano * by MontaVista Software, Inc. (Nico, your code rocks!) 10eb36f293SDaniel Lezcano */ 11eb36f293SDaniel Lezcano 12eb36f293SDaniel Lezcano #include <linux/kernel.h> 13eb36f293SDaniel Lezcano #include <linux/init.h> 14eb36f293SDaniel Lezcano #include <linux/interrupt.h> 15eb36f293SDaniel Lezcano #include <linux/clk.h> 16eb36f293SDaniel Lezcano #include <linux/clockchips.h> 17eb36f293SDaniel Lezcano #include <linux/of_address.h> 18eb36f293SDaniel Lezcano #include <linux/of_irq.h> 19eb36f293SDaniel Lezcano #include <linux/sched/clock.h> 20eb36f293SDaniel Lezcano #include <linux/sched_clock.h> 21eb36f293SDaniel Lezcano 22eb36f293SDaniel Lezcano #include <clocksource/pxa.h> 23eb36f293SDaniel Lezcano 24eb36f293SDaniel Lezcano #include <asm/div64.h> 25eb36f293SDaniel Lezcano 26eb36f293SDaniel Lezcano #define OSMR0 0x00 /* OS Timer 0 Match Register */ 27eb36f293SDaniel Lezcano #define OSMR1 0x04 /* OS Timer 1 Match Register */ 28eb36f293SDaniel Lezcano #define OSMR2 0x08 /* OS Timer 2 Match Register */ 29eb36f293SDaniel Lezcano #define OSMR3 0x0C /* OS Timer 3 Match Register */ 30eb36f293SDaniel Lezcano 31eb36f293SDaniel Lezcano #define OSCR 0x10 /* OS Timer Counter Register */ 32eb36f293SDaniel Lezcano #define OSSR 0x14 /* OS Timer Status Register */ 33eb36f293SDaniel Lezcano #define OWER 0x18 /* OS Timer Watchdog Enable Register */ 34eb36f293SDaniel Lezcano #define OIER 0x1C /* OS Timer Interrupt Enable Register */ 35eb36f293SDaniel Lezcano 36eb36f293SDaniel Lezcano #define OSSR_M3 (1 << 3) /* Match status channel 3 */ 37eb36f293SDaniel Lezcano #define OSSR_M2 (1 << 2) /* Match status channel 2 */ 38eb36f293SDaniel Lezcano #define OSSR_M1 (1 << 1) /* Match status channel 1 */ 39eb36f293SDaniel Lezcano #define OSSR_M0 (1 << 0) /* Match status channel 0 */ 40eb36f293SDaniel Lezcano 41eb36f293SDaniel Lezcano #define OIER_E0 (1 << 0) /* Interrupt enable channel 0 */ 42eb36f293SDaniel Lezcano 43eb36f293SDaniel Lezcano /* 44eb36f293SDaniel Lezcano * This is PXA's sched_clock implementation. This has a resolution 45eb36f293SDaniel Lezcano * of at least 308 ns and a maximum value of 208 days. 46eb36f293SDaniel Lezcano * 47eb36f293SDaniel Lezcano * The return value is guaranteed to be monotonic in that range as 48eb36f293SDaniel Lezcano * long as there is always less than 582 seconds between successive 49eb36f293SDaniel Lezcano * calls to sched_clock() which should always be the case in practice. 50eb36f293SDaniel Lezcano */ 51eb36f293SDaniel Lezcano 52eb36f293SDaniel Lezcano #define timer_readl(reg) readl_relaxed(timer_base + (reg)) 53eb36f293SDaniel Lezcano #define timer_writel(val, reg) writel_relaxed((val), timer_base + (reg)) 54eb36f293SDaniel Lezcano 55eb36f293SDaniel Lezcano static void __iomem *timer_base; 56eb36f293SDaniel Lezcano 57eb36f293SDaniel Lezcano static u64 notrace pxa_read_sched_clock(void) 58eb36f293SDaniel Lezcano { 59eb36f293SDaniel Lezcano return timer_readl(OSCR); 60eb36f293SDaniel Lezcano } 61eb36f293SDaniel Lezcano 62eb36f293SDaniel Lezcano 63eb36f293SDaniel Lezcano #define MIN_OSCR_DELTA 16 64eb36f293SDaniel Lezcano 65eb36f293SDaniel Lezcano static irqreturn_t 66eb36f293SDaniel Lezcano pxa_ost0_interrupt(int irq, void *dev_id) 67eb36f293SDaniel Lezcano { 68eb36f293SDaniel Lezcano struct clock_event_device *c = dev_id; 69eb36f293SDaniel Lezcano 70eb36f293SDaniel Lezcano /* Disarm the compare/match, signal the event. */ 71eb36f293SDaniel Lezcano timer_writel(timer_readl(OIER) & ~OIER_E0, OIER); 72eb36f293SDaniel Lezcano timer_writel(OSSR_M0, OSSR); 73eb36f293SDaniel Lezcano c->event_handler(c); 74eb36f293SDaniel Lezcano 75eb36f293SDaniel Lezcano return IRQ_HANDLED; 76eb36f293SDaniel Lezcano } 77eb36f293SDaniel Lezcano 78eb36f293SDaniel Lezcano static int 79eb36f293SDaniel Lezcano pxa_osmr0_set_next_event(unsigned long delta, struct clock_event_device *dev) 80eb36f293SDaniel Lezcano { 81eb36f293SDaniel Lezcano unsigned long next, oscr; 82eb36f293SDaniel Lezcano 83eb36f293SDaniel Lezcano timer_writel(timer_readl(OIER) | OIER_E0, OIER); 84eb36f293SDaniel Lezcano next = timer_readl(OSCR) + delta; 85eb36f293SDaniel Lezcano timer_writel(next, OSMR0); 86eb36f293SDaniel Lezcano oscr = timer_readl(OSCR); 87eb36f293SDaniel Lezcano 88eb36f293SDaniel Lezcano return (signed)(next - oscr) <= MIN_OSCR_DELTA ? -ETIME : 0; 89eb36f293SDaniel Lezcano } 90eb36f293SDaniel Lezcano 91eb36f293SDaniel Lezcano static int pxa_osmr0_shutdown(struct clock_event_device *evt) 92eb36f293SDaniel Lezcano { 93eb36f293SDaniel Lezcano /* initializing, released, or preparing for suspend */ 94eb36f293SDaniel Lezcano timer_writel(timer_readl(OIER) & ~OIER_E0, OIER); 95eb36f293SDaniel Lezcano timer_writel(OSSR_M0, OSSR); 96eb36f293SDaniel Lezcano return 0; 97eb36f293SDaniel Lezcano } 98eb36f293SDaniel Lezcano 99eb36f293SDaniel Lezcano #ifdef CONFIG_PM 100eb36f293SDaniel Lezcano static unsigned long osmr[4], oier, oscr; 101eb36f293SDaniel Lezcano 102eb36f293SDaniel Lezcano static void pxa_timer_suspend(struct clock_event_device *cedev) 103eb36f293SDaniel Lezcano { 104eb36f293SDaniel Lezcano osmr[0] = timer_readl(OSMR0); 105eb36f293SDaniel Lezcano osmr[1] = timer_readl(OSMR1); 106eb36f293SDaniel Lezcano osmr[2] = timer_readl(OSMR2); 107eb36f293SDaniel Lezcano osmr[3] = timer_readl(OSMR3); 108eb36f293SDaniel Lezcano oier = timer_readl(OIER); 109eb36f293SDaniel Lezcano oscr = timer_readl(OSCR); 110eb36f293SDaniel Lezcano } 111eb36f293SDaniel Lezcano 112eb36f293SDaniel Lezcano static void pxa_timer_resume(struct clock_event_device *cedev) 113eb36f293SDaniel Lezcano { 114eb36f293SDaniel Lezcano /* 115eb36f293SDaniel Lezcano * Ensure that we have at least MIN_OSCR_DELTA between match 116eb36f293SDaniel Lezcano * register 0 and the OSCR, to guarantee that we will receive 117eb36f293SDaniel Lezcano * the one-shot timer interrupt. We adjust OSMR0 in preference 118eb36f293SDaniel Lezcano * to OSCR to guarantee that OSCR is monotonically incrementing. 119eb36f293SDaniel Lezcano */ 120eb36f293SDaniel Lezcano if (osmr[0] - oscr < MIN_OSCR_DELTA) 121eb36f293SDaniel Lezcano osmr[0] += MIN_OSCR_DELTA; 122eb36f293SDaniel Lezcano 123eb36f293SDaniel Lezcano timer_writel(osmr[0], OSMR0); 124eb36f293SDaniel Lezcano timer_writel(osmr[1], OSMR1); 125eb36f293SDaniel Lezcano timer_writel(osmr[2], OSMR2); 126eb36f293SDaniel Lezcano timer_writel(osmr[3], OSMR3); 127eb36f293SDaniel Lezcano timer_writel(oier, OIER); 128eb36f293SDaniel Lezcano timer_writel(oscr, OSCR); 129eb36f293SDaniel Lezcano } 130eb36f293SDaniel Lezcano #else 131eb36f293SDaniel Lezcano #define pxa_timer_suspend NULL 132eb36f293SDaniel Lezcano #define pxa_timer_resume NULL 133eb36f293SDaniel Lezcano #endif 134eb36f293SDaniel Lezcano 135eb36f293SDaniel Lezcano static struct clock_event_device ckevt_pxa_osmr0 = { 136eb36f293SDaniel Lezcano .name = "osmr0", 137eb36f293SDaniel Lezcano .features = CLOCK_EVT_FEAT_ONESHOT, 138eb36f293SDaniel Lezcano .rating = 200, 139eb36f293SDaniel Lezcano .set_next_event = pxa_osmr0_set_next_event, 140eb36f293SDaniel Lezcano .set_state_shutdown = pxa_osmr0_shutdown, 141eb36f293SDaniel Lezcano .set_state_oneshot = pxa_osmr0_shutdown, 142eb36f293SDaniel Lezcano .suspend = pxa_timer_suspend, 143eb36f293SDaniel Lezcano .resume = pxa_timer_resume, 144eb36f293SDaniel Lezcano }; 145eb36f293SDaniel Lezcano 146eb36f293SDaniel Lezcano static int __init pxa_timer_common_init(int irq, unsigned long clock_tick_rate) 147eb36f293SDaniel Lezcano { 148eb36f293SDaniel Lezcano int ret; 149eb36f293SDaniel Lezcano 150eb36f293SDaniel Lezcano timer_writel(0, OIER); 151eb36f293SDaniel Lezcano timer_writel(OSSR_M0 | OSSR_M1 | OSSR_M2 | OSSR_M3, OSSR); 152eb36f293SDaniel Lezcano 153eb36f293SDaniel Lezcano sched_clock_register(pxa_read_sched_clock, 32, clock_tick_rate); 154eb36f293SDaniel Lezcano 155eb36f293SDaniel Lezcano ckevt_pxa_osmr0.cpumask = cpumask_of(0); 156eb36f293SDaniel Lezcano 157*cc2550b4Safzal mohammed ret = request_irq(irq, pxa_ost0_interrupt, IRQF_TIMER | IRQF_IRQPOLL, 158*cc2550b4Safzal mohammed "ost0", &ckevt_pxa_osmr0); 159eb36f293SDaniel Lezcano if (ret) { 160eb36f293SDaniel Lezcano pr_err("Failed to setup irq\n"); 161eb36f293SDaniel Lezcano return ret; 162eb36f293SDaniel Lezcano } 163eb36f293SDaniel Lezcano 164eb36f293SDaniel Lezcano ret = clocksource_mmio_init(timer_base + OSCR, "oscr0", clock_tick_rate, 200, 165eb36f293SDaniel Lezcano 32, clocksource_mmio_readl_up); 166eb36f293SDaniel Lezcano if (ret) { 167eb36f293SDaniel Lezcano pr_err("Failed to init clocksource\n"); 168eb36f293SDaniel Lezcano return ret; 169eb36f293SDaniel Lezcano } 170eb36f293SDaniel Lezcano 171eb36f293SDaniel Lezcano clockevents_config_and_register(&ckevt_pxa_osmr0, clock_tick_rate, 172eb36f293SDaniel Lezcano MIN_OSCR_DELTA * 2, 0x7fffffff); 173eb36f293SDaniel Lezcano 174eb36f293SDaniel Lezcano return 0; 175eb36f293SDaniel Lezcano } 176eb36f293SDaniel Lezcano 177eb36f293SDaniel Lezcano static int __init pxa_timer_dt_init(struct device_node *np) 178eb36f293SDaniel Lezcano { 179eb36f293SDaniel Lezcano struct clk *clk; 180eb36f293SDaniel Lezcano int irq, ret; 181eb36f293SDaniel Lezcano 182eb36f293SDaniel Lezcano /* timer registers are shared with watchdog timer */ 183eb36f293SDaniel Lezcano timer_base = of_iomap(np, 0); 184eb36f293SDaniel Lezcano if (!timer_base) { 185eb36f293SDaniel Lezcano pr_err("%pOFn: unable to map resource\n", np); 186eb36f293SDaniel Lezcano return -ENXIO; 187eb36f293SDaniel Lezcano } 188eb36f293SDaniel Lezcano 189eb36f293SDaniel Lezcano clk = of_clk_get(np, 0); 190eb36f293SDaniel Lezcano if (IS_ERR(clk)) { 191eb36f293SDaniel Lezcano pr_crit("%pOFn: unable to get clk\n", np); 192eb36f293SDaniel Lezcano return PTR_ERR(clk); 193eb36f293SDaniel Lezcano } 194eb36f293SDaniel Lezcano 195eb36f293SDaniel Lezcano ret = clk_prepare_enable(clk); 196eb36f293SDaniel Lezcano if (ret) { 197eb36f293SDaniel Lezcano pr_crit("Failed to prepare clock\n"); 198eb36f293SDaniel Lezcano return ret; 199eb36f293SDaniel Lezcano } 200eb36f293SDaniel Lezcano 201eb36f293SDaniel Lezcano /* we are only interested in OS-timer0 irq */ 202eb36f293SDaniel Lezcano irq = irq_of_parse_and_map(np, 0); 203eb36f293SDaniel Lezcano if (irq <= 0) { 204eb36f293SDaniel Lezcano pr_crit("%pOFn: unable to parse OS-timer0 irq\n", np); 205eb36f293SDaniel Lezcano return -EINVAL; 206eb36f293SDaniel Lezcano } 207eb36f293SDaniel Lezcano 208eb36f293SDaniel Lezcano return pxa_timer_common_init(irq, clk_get_rate(clk)); 209eb36f293SDaniel Lezcano } 210eb36f293SDaniel Lezcano TIMER_OF_DECLARE(pxa_timer, "marvell,pxa-timer", pxa_timer_dt_init); 211eb36f293SDaniel Lezcano 212eb36f293SDaniel Lezcano /* 213eb36f293SDaniel Lezcano * Legacy timer init for non device-tree boards. 214eb36f293SDaniel Lezcano */ 215eb36f293SDaniel Lezcano void __init pxa_timer_nodt_init(int irq, void __iomem *base) 216eb36f293SDaniel Lezcano { 217eb36f293SDaniel Lezcano struct clk *clk; 218eb36f293SDaniel Lezcano 219eb36f293SDaniel Lezcano timer_base = base; 220eb36f293SDaniel Lezcano clk = clk_get(NULL, "OSTIMER0"); 221eb36f293SDaniel Lezcano if (clk && !IS_ERR(clk)) { 222eb36f293SDaniel Lezcano clk_prepare_enable(clk); 223eb36f293SDaniel Lezcano pxa_timer_common_init(irq, clk_get_rate(clk)); 224eb36f293SDaniel Lezcano } else { 225eb36f293SDaniel Lezcano pr_crit("%s: unable to get clk\n", __func__); 226eb36f293SDaniel Lezcano } 227eb36f293SDaniel Lezcano } 228