18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * arch/arm/mach-pxa/time.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * PXA clocksource, clockevents, and OST interrupt handlers. 68c2ecf20Sopenharmony_ci * Copyright (c) 2007 by Bill Gatliff <bgat@billgatliff.com>. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Derived from Nicolas Pitre's PXA timer handler Copyright (c) 2001 98c2ecf20Sopenharmony_ci * by MontaVista Software, Inc. (Nico, your code rocks!) 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/kernel.h> 138c2ecf20Sopenharmony_ci#include <linux/init.h> 148c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 158c2ecf20Sopenharmony_ci#include <linux/clk.h> 168c2ecf20Sopenharmony_ci#include <linux/clockchips.h> 178c2ecf20Sopenharmony_ci#include <linux/of_address.h> 188c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 198c2ecf20Sopenharmony_ci#include <linux/sched/clock.h> 208c2ecf20Sopenharmony_ci#include <linux/sched_clock.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include <clocksource/pxa.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#include <asm/div64.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define OSMR0 0x00 /* OS Timer 0 Match Register */ 278c2ecf20Sopenharmony_ci#define OSMR1 0x04 /* OS Timer 1 Match Register */ 288c2ecf20Sopenharmony_ci#define OSMR2 0x08 /* OS Timer 2 Match Register */ 298c2ecf20Sopenharmony_ci#define OSMR3 0x0C /* OS Timer 3 Match Register */ 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define OSCR 0x10 /* OS Timer Counter Register */ 328c2ecf20Sopenharmony_ci#define OSSR 0x14 /* OS Timer Status Register */ 338c2ecf20Sopenharmony_ci#define OWER 0x18 /* OS Timer Watchdog Enable Register */ 348c2ecf20Sopenharmony_ci#define OIER 0x1C /* OS Timer Interrupt Enable Register */ 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci#define OSSR_M3 (1 << 3) /* Match status channel 3 */ 378c2ecf20Sopenharmony_ci#define OSSR_M2 (1 << 2) /* Match status channel 2 */ 388c2ecf20Sopenharmony_ci#define OSSR_M1 (1 << 1) /* Match status channel 1 */ 398c2ecf20Sopenharmony_ci#define OSSR_M0 (1 << 0) /* Match status channel 0 */ 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#define OIER_E0 (1 << 0) /* Interrupt enable channel 0 */ 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci/* 448c2ecf20Sopenharmony_ci * This is PXA's sched_clock implementation. This has a resolution 458c2ecf20Sopenharmony_ci * of at least 308 ns and a maximum value of 208 days. 468c2ecf20Sopenharmony_ci * 478c2ecf20Sopenharmony_ci * The return value is guaranteed to be monotonic in that range as 488c2ecf20Sopenharmony_ci * long as there is always less than 582 seconds between successive 498c2ecf20Sopenharmony_ci * calls to sched_clock() which should always be the case in practice. 508c2ecf20Sopenharmony_ci */ 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#define timer_readl(reg) readl_relaxed(timer_base + (reg)) 538c2ecf20Sopenharmony_ci#define timer_writel(val, reg) writel_relaxed((val), timer_base + (reg)) 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_cistatic void __iomem *timer_base; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_cistatic u64 notrace pxa_read_sched_clock(void) 588c2ecf20Sopenharmony_ci{ 598c2ecf20Sopenharmony_ci return timer_readl(OSCR); 608c2ecf20Sopenharmony_ci} 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci#define MIN_OSCR_DELTA 16 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_cistatic irqreturn_t 668c2ecf20Sopenharmony_cipxa_ost0_interrupt(int irq, void *dev_id) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci struct clock_event_device *c = dev_id; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci /* Disarm the compare/match, signal the event. */ 718c2ecf20Sopenharmony_ci timer_writel(timer_readl(OIER) & ~OIER_E0, OIER); 728c2ecf20Sopenharmony_ci timer_writel(OSSR_M0, OSSR); 738c2ecf20Sopenharmony_ci c->event_handler(c); 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci return IRQ_HANDLED; 768c2ecf20Sopenharmony_ci} 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistatic int 798c2ecf20Sopenharmony_cipxa_osmr0_set_next_event(unsigned long delta, struct clock_event_device *dev) 808c2ecf20Sopenharmony_ci{ 818c2ecf20Sopenharmony_ci unsigned long next, oscr; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci timer_writel(timer_readl(OIER) | OIER_E0, OIER); 848c2ecf20Sopenharmony_ci next = timer_readl(OSCR) + delta; 858c2ecf20Sopenharmony_ci timer_writel(next, OSMR0); 868c2ecf20Sopenharmony_ci oscr = timer_readl(OSCR); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci return (signed)(next - oscr) <= MIN_OSCR_DELTA ? -ETIME : 0; 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cistatic int pxa_osmr0_shutdown(struct clock_event_device *evt) 928c2ecf20Sopenharmony_ci{ 938c2ecf20Sopenharmony_ci /* initializing, released, or preparing for suspend */ 948c2ecf20Sopenharmony_ci timer_writel(timer_readl(OIER) & ~OIER_E0, OIER); 958c2ecf20Sopenharmony_ci timer_writel(OSSR_M0, OSSR); 968c2ecf20Sopenharmony_ci return 0; 978c2ecf20Sopenharmony_ci} 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci#ifdef CONFIG_PM 1008c2ecf20Sopenharmony_cistatic unsigned long osmr[4], oier, oscr; 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_cistatic void pxa_timer_suspend(struct clock_event_device *cedev) 1038c2ecf20Sopenharmony_ci{ 1048c2ecf20Sopenharmony_ci osmr[0] = timer_readl(OSMR0); 1058c2ecf20Sopenharmony_ci osmr[1] = timer_readl(OSMR1); 1068c2ecf20Sopenharmony_ci osmr[2] = timer_readl(OSMR2); 1078c2ecf20Sopenharmony_ci osmr[3] = timer_readl(OSMR3); 1088c2ecf20Sopenharmony_ci oier = timer_readl(OIER); 1098c2ecf20Sopenharmony_ci oscr = timer_readl(OSCR); 1108c2ecf20Sopenharmony_ci} 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_cistatic void pxa_timer_resume(struct clock_event_device *cedev) 1138c2ecf20Sopenharmony_ci{ 1148c2ecf20Sopenharmony_ci /* 1158c2ecf20Sopenharmony_ci * Ensure that we have at least MIN_OSCR_DELTA between match 1168c2ecf20Sopenharmony_ci * register 0 and the OSCR, to guarantee that we will receive 1178c2ecf20Sopenharmony_ci * the one-shot timer interrupt. We adjust OSMR0 in preference 1188c2ecf20Sopenharmony_ci * to OSCR to guarantee that OSCR is monotonically incrementing. 1198c2ecf20Sopenharmony_ci */ 1208c2ecf20Sopenharmony_ci if (osmr[0] - oscr < MIN_OSCR_DELTA) 1218c2ecf20Sopenharmony_ci osmr[0] += MIN_OSCR_DELTA; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci timer_writel(osmr[0], OSMR0); 1248c2ecf20Sopenharmony_ci timer_writel(osmr[1], OSMR1); 1258c2ecf20Sopenharmony_ci timer_writel(osmr[2], OSMR2); 1268c2ecf20Sopenharmony_ci timer_writel(osmr[3], OSMR3); 1278c2ecf20Sopenharmony_ci timer_writel(oier, OIER); 1288c2ecf20Sopenharmony_ci timer_writel(oscr, OSCR); 1298c2ecf20Sopenharmony_ci} 1308c2ecf20Sopenharmony_ci#else 1318c2ecf20Sopenharmony_ci#define pxa_timer_suspend NULL 1328c2ecf20Sopenharmony_ci#define pxa_timer_resume NULL 1338c2ecf20Sopenharmony_ci#endif 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_cistatic struct clock_event_device ckevt_pxa_osmr0 = { 1368c2ecf20Sopenharmony_ci .name = "osmr0", 1378c2ecf20Sopenharmony_ci .features = CLOCK_EVT_FEAT_ONESHOT, 1388c2ecf20Sopenharmony_ci .rating = 200, 1398c2ecf20Sopenharmony_ci .set_next_event = pxa_osmr0_set_next_event, 1408c2ecf20Sopenharmony_ci .set_state_shutdown = pxa_osmr0_shutdown, 1418c2ecf20Sopenharmony_ci .set_state_oneshot = pxa_osmr0_shutdown, 1428c2ecf20Sopenharmony_ci .suspend = pxa_timer_suspend, 1438c2ecf20Sopenharmony_ci .resume = pxa_timer_resume, 1448c2ecf20Sopenharmony_ci}; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_cistatic int __init pxa_timer_common_init(int irq, unsigned long clock_tick_rate) 1478c2ecf20Sopenharmony_ci{ 1488c2ecf20Sopenharmony_ci int ret; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci timer_writel(0, OIER); 1518c2ecf20Sopenharmony_ci timer_writel(OSSR_M0 | OSSR_M1 | OSSR_M2 | OSSR_M3, OSSR); 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci sched_clock_register(pxa_read_sched_clock, 32, clock_tick_rate); 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci ckevt_pxa_osmr0.cpumask = cpumask_of(0); 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci ret = request_irq(irq, pxa_ost0_interrupt, IRQF_TIMER | IRQF_IRQPOLL, 1588c2ecf20Sopenharmony_ci "ost0", &ckevt_pxa_osmr0); 1598c2ecf20Sopenharmony_ci if (ret) { 1608c2ecf20Sopenharmony_ci pr_err("Failed to setup irq\n"); 1618c2ecf20Sopenharmony_ci return ret; 1628c2ecf20Sopenharmony_ci } 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci ret = clocksource_mmio_init(timer_base + OSCR, "oscr0", clock_tick_rate, 200, 1658c2ecf20Sopenharmony_ci 32, clocksource_mmio_readl_up); 1668c2ecf20Sopenharmony_ci if (ret) { 1678c2ecf20Sopenharmony_ci pr_err("Failed to init clocksource\n"); 1688c2ecf20Sopenharmony_ci return ret; 1698c2ecf20Sopenharmony_ci } 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci clockevents_config_and_register(&ckevt_pxa_osmr0, clock_tick_rate, 1728c2ecf20Sopenharmony_ci MIN_OSCR_DELTA * 2, 0x7fffffff); 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci return 0; 1758c2ecf20Sopenharmony_ci} 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_cistatic int __init pxa_timer_dt_init(struct device_node *np) 1788c2ecf20Sopenharmony_ci{ 1798c2ecf20Sopenharmony_ci struct clk *clk; 1808c2ecf20Sopenharmony_ci int irq, ret; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci /* timer registers are shared with watchdog timer */ 1838c2ecf20Sopenharmony_ci timer_base = of_iomap(np, 0); 1848c2ecf20Sopenharmony_ci if (!timer_base) { 1858c2ecf20Sopenharmony_ci pr_err("%pOFn: unable to map resource\n", np); 1868c2ecf20Sopenharmony_ci return -ENXIO; 1878c2ecf20Sopenharmony_ci } 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci clk = of_clk_get(np, 0); 1908c2ecf20Sopenharmony_ci if (IS_ERR(clk)) { 1918c2ecf20Sopenharmony_ci pr_crit("%pOFn: unable to get clk\n", np); 1928c2ecf20Sopenharmony_ci return PTR_ERR(clk); 1938c2ecf20Sopenharmony_ci } 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci ret = clk_prepare_enable(clk); 1968c2ecf20Sopenharmony_ci if (ret) { 1978c2ecf20Sopenharmony_ci pr_crit("Failed to prepare clock\n"); 1988c2ecf20Sopenharmony_ci return ret; 1998c2ecf20Sopenharmony_ci } 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci /* we are only interested in OS-timer0 irq */ 2028c2ecf20Sopenharmony_ci irq = irq_of_parse_and_map(np, 0); 2038c2ecf20Sopenharmony_ci if (irq <= 0) { 2048c2ecf20Sopenharmony_ci pr_crit("%pOFn: unable to parse OS-timer0 irq\n", np); 2058c2ecf20Sopenharmony_ci return -EINVAL; 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci return pxa_timer_common_init(irq, clk_get_rate(clk)); 2098c2ecf20Sopenharmony_ci} 2108c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(pxa_timer, "marvell,pxa-timer", pxa_timer_dt_init); 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci/* 2138c2ecf20Sopenharmony_ci * Legacy timer init for non device-tree boards. 2148c2ecf20Sopenharmony_ci */ 2158c2ecf20Sopenharmony_civoid __init pxa_timer_nodt_init(int irq, void __iomem *base) 2168c2ecf20Sopenharmony_ci{ 2178c2ecf20Sopenharmony_ci struct clk *clk; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci timer_base = base; 2208c2ecf20Sopenharmony_ci clk = clk_get(NULL, "OSTIMER0"); 2218c2ecf20Sopenharmony_ci if (clk && !IS_ERR(clk)) { 2228c2ecf20Sopenharmony_ci clk_prepare_enable(clk); 2238c2ecf20Sopenharmony_ci pxa_timer_common_init(irq, clk_get_rate(clk)); 2248c2ecf20Sopenharmony_ci } else { 2258c2ecf20Sopenharmony_ci pr_crit("%s: unable to get clk\n", __func__); 2268c2ecf20Sopenharmony_ci } 2278c2ecf20Sopenharmony_ci} 228