18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Allwinner A1X SoCs timer handling. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 2012 Maxime Ripard 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Maxime Ripard <maxime.ripard@free-electrons.com> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Based on code from 98c2ecf20Sopenharmony_ci * Allwinner Technology Co., Ltd. <www.allwinnertech.com> 108c2ecf20Sopenharmony_ci * Benn Huang <benn@allwinnertech.com> 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * This file is licensed under the terms of the GNU General Public 138c2ecf20Sopenharmony_ci * License version 2. This program is licensed "as is" without any 148c2ecf20Sopenharmony_ci * warranty of any kind, whether express or implied. 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include <linux/clk.h> 188c2ecf20Sopenharmony_ci#include <linux/clockchips.h> 198c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 208c2ecf20Sopenharmony_ci#include <linux/irq.h> 218c2ecf20Sopenharmony_ci#include <linux/irqreturn.h> 228c2ecf20Sopenharmony_ci#include <linux/sched_clock.h> 238c2ecf20Sopenharmony_ci#include <linux/of.h> 248c2ecf20Sopenharmony_ci#include <linux/of_address.h> 258c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#include "timer-of.h" 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#define TIMER_IRQ_EN_REG 0x00 308c2ecf20Sopenharmony_ci#define TIMER_IRQ_EN(val) BIT(val) 318c2ecf20Sopenharmony_ci#define TIMER_IRQ_ST_REG 0x04 328c2ecf20Sopenharmony_ci#define TIMER_CTL_REG(val) (0x10 * val + 0x10) 338c2ecf20Sopenharmony_ci#define TIMER_CTL_ENABLE BIT(0) 348c2ecf20Sopenharmony_ci#define TIMER_CTL_RELOAD BIT(1) 358c2ecf20Sopenharmony_ci#define TIMER_CTL_CLK_SRC(val) (((val) & 0x3) << 2) 368c2ecf20Sopenharmony_ci#define TIMER_CTL_CLK_SRC_OSC24M (1) 378c2ecf20Sopenharmony_ci#define TIMER_CTL_CLK_PRES(val) (((val) & 0x7) << 4) 388c2ecf20Sopenharmony_ci#define TIMER_CTL_ONESHOT BIT(7) 398c2ecf20Sopenharmony_ci#define TIMER_INTVAL_REG(val) (0x10 * (val) + 0x14) 408c2ecf20Sopenharmony_ci#define TIMER_CNTVAL_REG(val) (0x10 * (val) + 0x18) 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci#define TIMER_SYNC_TICKS 3 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci/* 458c2ecf20Sopenharmony_ci * When we disable a timer, we need to wait at least for 2 cycles of 468c2ecf20Sopenharmony_ci * the timer source clock. We will use for that the clocksource timer 478c2ecf20Sopenharmony_ci * that is already setup and runs at the same frequency than the other 488c2ecf20Sopenharmony_ci * timers, and we never will be disabled. 498c2ecf20Sopenharmony_ci */ 508c2ecf20Sopenharmony_cistatic void sun4i_clkevt_sync(void __iomem *base) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci u32 old = readl(base + TIMER_CNTVAL_REG(1)); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci while ((old - readl(base + TIMER_CNTVAL_REG(1))) < TIMER_SYNC_TICKS) 558c2ecf20Sopenharmony_ci cpu_relax(); 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistatic void sun4i_clkevt_time_stop(void __iomem *base, u8 timer) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci u32 val = readl(base + TIMER_CTL_REG(timer)); 618c2ecf20Sopenharmony_ci writel(val & ~TIMER_CTL_ENABLE, base + TIMER_CTL_REG(timer)); 628c2ecf20Sopenharmony_ci sun4i_clkevt_sync(base); 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_cistatic void sun4i_clkevt_time_setup(void __iomem *base, u8 timer, 668c2ecf20Sopenharmony_ci unsigned long delay) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci writel(delay, base + TIMER_INTVAL_REG(timer)); 698c2ecf20Sopenharmony_ci} 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_cistatic void sun4i_clkevt_time_start(void __iomem *base, u8 timer, 728c2ecf20Sopenharmony_ci bool periodic) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci u32 val = readl(base + TIMER_CTL_REG(timer)); 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci if (periodic) 778c2ecf20Sopenharmony_ci val &= ~TIMER_CTL_ONESHOT; 788c2ecf20Sopenharmony_ci else 798c2ecf20Sopenharmony_ci val |= TIMER_CTL_ONESHOT; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci writel(val | TIMER_CTL_ENABLE | TIMER_CTL_RELOAD, 828c2ecf20Sopenharmony_ci base + TIMER_CTL_REG(timer)); 838c2ecf20Sopenharmony_ci} 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_cistatic int sun4i_clkevt_shutdown(struct clock_event_device *evt) 868c2ecf20Sopenharmony_ci{ 878c2ecf20Sopenharmony_ci struct timer_of *to = to_timer_of(evt); 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci sun4i_clkevt_time_stop(timer_of_base(to), 0); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci return 0; 928c2ecf20Sopenharmony_ci} 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_cistatic int sun4i_clkevt_set_oneshot(struct clock_event_device *evt) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci struct timer_of *to = to_timer_of(evt); 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci sun4i_clkevt_time_stop(timer_of_base(to), 0); 998c2ecf20Sopenharmony_ci sun4i_clkevt_time_start(timer_of_base(to), 0, false); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci return 0; 1028c2ecf20Sopenharmony_ci} 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistatic int sun4i_clkevt_set_periodic(struct clock_event_device *evt) 1058c2ecf20Sopenharmony_ci{ 1068c2ecf20Sopenharmony_ci struct timer_of *to = to_timer_of(evt); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci sun4i_clkevt_time_stop(timer_of_base(to), 0); 1098c2ecf20Sopenharmony_ci sun4i_clkevt_time_setup(timer_of_base(to), 0, timer_of_period(to)); 1108c2ecf20Sopenharmony_ci sun4i_clkevt_time_start(timer_of_base(to), 0, true); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci return 0; 1138c2ecf20Sopenharmony_ci} 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_cistatic int sun4i_clkevt_next_event(unsigned long evt, 1168c2ecf20Sopenharmony_ci struct clock_event_device *clkevt) 1178c2ecf20Sopenharmony_ci{ 1188c2ecf20Sopenharmony_ci struct timer_of *to = to_timer_of(clkevt); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci sun4i_clkevt_time_stop(timer_of_base(to), 0); 1218c2ecf20Sopenharmony_ci sun4i_clkevt_time_setup(timer_of_base(to), 0, evt - TIMER_SYNC_TICKS); 1228c2ecf20Sopenharmony_ci sun4i_clkevt_time_start(timer_of_base(to), 0, false); 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci return 0; 1258c2ecf20Sopenharmony_ci} 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_cistatic void sun4i_timer_clear_interrupt(void __iomem *base) 1288c2ecf20Sopenharmony_ci{ 1298c2ecf20Sopenharmony_ci writel(TIMER_IRQ_EN(0), base + TIMER_IRQ_ST_REG); 1308c2ecf20Sopenharmony_ci} 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_cistatic irqreturn_t sun4i_timer_interrupt(int irq, void *dev_id) 1338c2ecf20Sopenharmony_ci{ 1348c2ecf20Sopenharmony_ci struct clock_event_device *evt = (struct clock_event_device *)dev_id; 1358c2ecf20Sopenharmony_ci struct timer_of *to = to_timer_of(evt); 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci sun4i_timer_clear_interrupt(timer_of_base(to)); 1388c2ecf20Sopenharmony_ci evt->event_handler(evt); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci return IRQ_HANDLED; 1418c2ecf20Sopenharmony_ci} 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_cistatic struct timer_of to = { 1448c2ecf20Sopenharmony_ci .flags = TIMER_OF_IRQ | TIMER_OF_CLOCK | TIMER_OF_BASE, 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci .clkevt = { 1478c2ecf20Sopenharmony_ci .name = "sun4i_tick", 1488c2ecf20Sopenharmony_ci .rating = 350, 1498c2ecf20Sopenharmony_ci .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, 1508c2ecf20Sopenharmony_ci .set_state_shutdown = sun4i_clkevt_shutdown, 1518c2ecf20Sopenharmony_ci .set_state_periodic = sun4i_clkevt_set_periodic, 1528c2ecf20Sopenharmony_ci .set_state_oneshot = sun4i_clkevt_set_oneshot, 1538c2ecf20Sopenharmony_ci .tick_resume = sun4i_clkevt_shutdown, 1548c2ecf20Sopenharmony_ci .set_next_event = sun4i_clkevt_next_event, 1558c2ecf20Sopenharmony_ci .cpumask = cpu_possible_mask, 1568c2ecf20Sopenharmony_ci }, 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci .of_irq = { 1598c2ecf20Sopenharmony_ci .handler = sun4i_timer_interrupt, 1608c2ecf20Sopenharmony_ci .flags = IRQF_TIMER | IRQF_IRQPOLL, 1618c2ecf20Sopenharmony_ci }, 1628c2ecf20Sopenharmony_ci}; 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_cistatic u64 notrace sun4i_timer_sched_read(void) 1658c2ecf20Sopenharmony_ci{ 1668c2ecf20Sopenharmony_ci return ~readl(timer_of_base(&to) + TIMER_CNTVAL_REG(1)); 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_cistatic int __init sun4i_timer_init(struct device_node *node) 1708c2ecf20Sopenharmony_ci{ 1718c2ecf20Sopenharmony_ci int ret; 1728c2ecf20Sopenharmony_ci u32 val; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci ret = timer_of_init(node, &to); 1758c2ecf20Sopenharmony_ci if (ret) 1768c2ecf20Sopenharmony_ci return ret; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci writel(~0, timer_of_base(&to) + TIMER_INTVAL_REG(1)); 1798c2ecf20Sopenharmony_ci writel(TIMER_CTL_ENABLE | TIMER_CTL_RELOAD | 1808c2ecf20Sopenharmony_ci TIMER_CTL_CLK_SRC(TIMER_CTL_CLK_SRC_OSC24M), 1818c2ecf20Sopenharmony_ci timer_of_base(&to) + TIMER_CTL_REG(1)); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci /* 1848c2ecf20Sopenharmony_ci * sched_clock_register does not have priorities, and on sun6i and 1858c2ecf20Sopenharmony_ci * later there is a better sched_clock registered by arm_arch_timer.c 1868c2ecf20Sopenharmony_ci */ 1878c2ecf20Sopenharmony_ci if (of_machine_is_compatible("allwinner,sun4i-a10") || 1888c2ecf20Sopenharmony_ci of_machine_is_compatible("allwinner,sun5i-a13") || 1898c2ecf20Sopenharmony_ci of_machine_is_compatible("allwinner,sun5i-a10s") || 1908c2ecf20Sopenharmony_ci of_machine_is_compatible("allwinner,suniv-f1c100s")) 1918c2ecf20Sopenharmony_ci sched_clock_register(sun4i_timer_sched_read, 32, 1928c2ecf20Sopenharmony_ci timer_of_rate(&to)); 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci ret = clocksource_mmio_init(timer_of_base(&to) + TIMER_CNTVAL_REG(1), 1958c2ecf20Sopenharmony_ci node->name, timer_of_rate(&to), 350, 32, 1968c2ecf20Sopenharmony_ci clocksource_mmio_readl_down); 1978c2ecf20Sopenharmony_ci if (ret) { 1988c2ecf20Sopenharmony_ci pr_err("Failed to register clocksource\n"); 1998c2ecf20Sopenharmony_ci return ret; 2008c2ecf20Sopenharmony_ci } 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci writel(TIMER_CTL_CLK_SRC(TIMER_CTL_CLK_SRC_OSC24M), 2038c2ecf20Sopenharmony_ci timer_of_base(&to) + TIMER_CTL_REG(0)); 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci /* Make sure timer is stopped before playing with interrupts */ 2068c2ecf20Sopenharmony_ci sun4i_clkevt_time_stop(timer_of_base(&to), 0); 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci /* clear timer0 interrupt */ 2098c2ecf20Sopenharmony_ci sun4i_timer_clear_interrupt(timer_of_base(&to)); 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci clockevents_config_and_register(&to.clkevt, timer_of_rate(&to), 2128c2ecf20Sopenharmony_ci TIMER_SYNC_TICKS, 0xffffffff); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_ci /* Enable timer0 interrupt */ 2158c2ecf20Sopenharmony_ci val = readl(timer_of_base(&to) + TIMER_IRQ_EN_REG); 2168c2ecf20Sopenharmony_ci writel(val | TIMER_IRQ_EN(0), timer_of_base(&to) + TIMER_IRQ_EN_REG); 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci return ret; 2198c2ecf20Sopenharmony_ci} 2208c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(sun4i, "allwinner,sun4i-a10-timer", 2218c2ecf20Sopenharmony_ci sun4i_timer_init); 2228c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(sun8i_a23, "allwinner,sun8i-a23-timer", 2238c2ecf20Sopenharmony_ci sun4i_timer_init); 2248c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(sun8i_v3s, "allwinner,sun8i-v3s-timer", 2258c2ecf20Sopenharmony_ci sun4i_timer_init); 2268c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(suniv, "allwinner,suniv-f1c100s-timer", 2278c2ecf20Sopenharmony_ci sun4i_timer_init); 228