18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Amlogic Meson6 SoCs timer handling. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2014 Carlo Caione <carlo@caione.org> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Based on code from Amlogic, Inc 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/bitfield.h> 118c2ecf20Sopenharmony_ci#include <linux/bitops.h> 128c2ecf20Sopenharmony_ci#include <linux/clk.h> 138c2ecf20Sopenharmony_ci#include <linux/clockchips.h> 148c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 158c2ecf20Sopenharmony_ci#include <linux/irq.h> 168c2ecf20Sopenharmony_ci#include <linux/irqreturn.h> 178c2ecf20Sopenharmony_ci#include <linux/sched_clock.h> 188c2ecf20Sopenharmony_ci#include <linux/of.h> 198c2ecf20Sopenharmony_ci#include <linux/of_address.h> 208c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#ifdef CONFIG_ARM 238c2ecf20Sopenharmony_ci#include <linux/delay.h> 248c2ecf20Sopenharmony_ci#endif 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX 0x00 278c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERD_EN BIT(19) 288c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERC_EN BIT(18) 298c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERB_EN BIT(17) 308c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERA_EN BIT(16) 318c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERD_MODE BIT(15) 328c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERC_MODE BIT(14) 338c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERB_MODE BIT(13) 348c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERA_MODE BIT(12) 358c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_MASK GENMASK(10, 8) 368c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_SYSTEM_CLOCK 0x0 378c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_1US 0x1 388c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_10US 0x2 398c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_100US 0x3 408c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_1MS 0x4 418c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERD_INPUT_CLOCK_MASK GENMASK(7, 6) 428c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERC_INPUT_CLOCK_MASK GENMASK(5, 4) 438c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERB_INPUT_CLOCK_MASK GENMASK(3, 2) 448c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERA_INPUT_CLOCK_MASK GENMASK(1, 0) 458c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_1US 0x0 468c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_10US 0x1 478c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_100US 0x0 488c2ecf20Sopenharmony_ci#define MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_1MS 0x3 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci#define MESON_ISA_TIMERA 0x04 518c2ecf20Sopenharmony_ci#define MESON_ISA_TIMERB 0x08 528c2ecf20Sopenharmony_ci#define MESON_ISA_TIMERC 0x0c 538c2ecf20Sopenharmony_ci#define MESON_ISA_TIMERD 0x10 548c2ecf20Sopenharmony_ci#define MESON_ISA_TIMERE 0x14 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic void __iomem *timer_base; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci#ifdef CONFIG_ARM 598c2ecf20Sopenharmony_cistatic unsigned long meson6_read_current_timer(void) 608c2ecf20Sopenharmony_ci{ 618c2ecf20Sopenharmony_ci return readl_relaxed(timer_base + MESON_ISA_TIMERE); 628c2ecf20Sopenharmony_ci} 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistatic struct delay_timer meson6_delay_timer = { 658c2ecf20Sopenharmony_ci .read_current_timer = meson6_read_current_timer, 668c2ecf20Sopenharmony_ci .freq = 1000 * 1000, 678c2ecf20Sopenharmony_ci}; 688c2ecf20Sopenharmony_ci#endif 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cistatic u64 notrace meson6_timer_sched_read(void) 718c2ecf20Sopenharmony_ci{ 728c2ecf20Sopenharmony_ci return (u64)readl(timer_base + MESON_ISA_TIMERE); 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_cistatic void meson6_clkevt_time_stop(void) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci u32 val = readl(timer_base + MESON_ISA_TIMER_MUX); 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci writel(val & ~MESON_ISA_TIMER_MUX_TIMERA_EN, 808c2ecf20Sopenharmony_ci timer_base + MESON_ISA_TIMER_MUX); 818c2ecf20Sopenharmony_ci} 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_cistatic void meson6_clkevt_time_setup(unsigned long delay) 848c2ecf20Sopenharmony_ci{ 858c2ecf20Sopenharmony_ci writel(delay, timer_base + MESON_ISA_TIMERA); 868c2ecf20Sopenharmony_ci} 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_cistatic void meson6_clkevt_time_start(bool periodic) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci u32 val = readl(timer_base + MESON_ISA_TIMER_MUX); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci if (periodic) 938c2ecf20Sopenharmony_ci val |= MESON_ISA_TIMER_MUX_TIMERA_MODE; 948c2ecf20Sopenharmony_ci else 958c2ecf20Sopenharmony_ci val &= ~MESON_ISA_TIMER_MUX_TIMERA_MODE; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci writel(val | MESON_ISA_TIMER_MUX_TIMERA_EN, 988c2ecf20Sopenharmony_ci timer_base + MESON_ISA_TIMER_MUX); 998c2ecf20Sopenharmony_ci} 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_cistatic int meson6_shutdown(struct clock_event_device *evt) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci meson6_clkevt_time_stop(); 1048c2ecf20Sopenharmony_ci return 0; 1058c2ecf20Sopenharmony_ci} 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_cistatic int meson6_set_oneshot(struct clock_event_device *evt) 1088c2ecf20Sopenharmony_ci{ 1098c2ecf20Sopenharmony_ci meson6_clkevt_time_stop(); 1108c2ecf20Sopenharmony_ci meson6_clkevt_time_start(false); 1118c2ecf20Sopenharmony_ci return 0; 1128c2ecf20Sopenharmony_ci} 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistatic int meson6_set_periodic(struct clock_event_device *evt) 1158c2ecf20Sopenharmony_ci{ 1168c2ecf20Sopenharmony_ci meson6_clkevt_time_stop(); 1178c2ecf20Sopenharmony_ci meson6_clkevt_time_setup(USEC_PER_SEC / HZ - 1); 1188c2ecf20Sopenharmony_ci meson6_clkevt_time_start(true); 1198c2ecf20Sopenharmony_ci return 0; 1208c2ecf20Sopenharmony_ci} 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_cistatic int meson6_clkevt_next_event(unsigned long evt, 1238c2ecf20Sopenharmony_ci struct clock_event_device *unused) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci meson6_clkevt_time_stop(); 1268c2ecf20Sopenharmony_ci meson6_clkevt_time_setup(evt); 1278c2ecf20Sopenharmony_ci meson6_clkevt_time_start(false); 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci return 0; 1308c2ecf20Sopenharmony_ci} 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_cistatic struct clock_event_device meson6_clockevent = { 1338c2ecf20Sopenharmony_ci .name = "meson6_tick", 1348c2ecf20Sopenharmony_ci .rating = 400, 1358c2ecf20Sopenharmony_ci .features = CLOCK_EVT_FEAT_PERIODIC | 1368c2ecf20Sopenharmony_ci CLOCK_EVT_FEAT_ONESHOT, 1378c2ecf20Sopenharmony_ci .set_state_shutdown = meson6_shutdown, 1388c2ecf20Sopenharmony_ci .set_state_periodic = meson6_set_periodic, 1398c2ecf20Sopenharmony_ci .set_state_oneshot = meson6_set_oneshot, 1408c2ecf20Sopenharmony_ci .tick_resume = meson6_shutdown, 1418c2ecf20Sopenharmony_ci .set_next_event = meson6_clkevt_next_event, 1428c2ecf20Sopenharmony_ci}; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic irqreturn_t meson6_timer_interrupt(int irq, void *dev_id) 1458c2ecf20Sopenharmony_ci{ 1468c2ecf20Sopenharmony_ci struct clock_event_device *evt = (struct clock_event_device *)dev_id; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci evt->event_handler(evt); 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci return IRQ_HANDLED; 1518c2ecf20Sopenharmony_ci} 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cistatic int __init meson6_timer_init(struct device_node *node) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci u32 val; 1568c2ecf20Sopenharmony_ci int ret, irq; 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci timer_base = of_io_request_and_map(node, 0, "meson6-timer"); 1598c2ecf20Sopenharmony_ci if (IS_ERR(timer_base)) { 1608c2ecf20Sopenharmony_ci pr_err("Can't map registers\n"); 1618c2ecf20Sopenharmony_ci return -ENXIO; 1628c2ecf20Sopenharmony_ci } 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci irq = irq_of_parse_and_map(node, 0); 1658c2ecf20Sopenharmony_ci if (irq <= 0) { 1668c2ecf20Sopenharmony_ci pr_err("Can't parse IRQ\n"); 1678c2ecf20Sopenharmony_ci return -EINVAL; 1688c2ecf20Sopenharmony_ci } 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci /* Set 1us for timer E */ 1718c2ecf20Sopenharmony_ci val = readl(timer_base + MESON_ISA_TIMER_MUX); 1728c2ecf20Sopenharmony_ci val &= ~MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_MASK; 1738c2ecf20Sopenharmony_ci val |= FIELD_PREP(MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_MASK, 1748c2ecf20Sopenharmony_ci MESON_ISA_TIMER_MUX_TIMERE_INPUT_CLOCK_1US); 1758c2ecf20Sopenharmony_ci writel(val, timer_base + MESON_ISA_TIMER_MUX); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci sched_clock_register(meson6_timer_sched_read, 32, USEC_PER_SEC); 1788c2ecf20Sopenharmony_ci clocksource_mmio_init(timer_base + MESON_ISA_TIMERE, node->name, 1798c2ecf20Sopenharmony_ci 1000 * 1000, 300, 32, clocksource_mmio_readl_up); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci /* Timer A base 1us */ 1828c2ecf20Sopenharmony_ci val &= ~MESON_ISA_TIMER_MUX_TIMERA_INPUT_CLOCK_MASK; 1838c2ecf20Sopenharmony_ci val |= FIELD_PREP(MESON_ISA_TIMER_MUX_TIMERA_INPUT_CLOCK_MASK, 1848c2ecf20Sopenharmony_ci MESON_ISA_TIMER_MUX_TIMERABCD_INPUT_CLOCK_1US); 1858c2ecf20Sopenharmony_ci writel(val, timer_base + MESON_ISA_TIMER_MUX); 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci /* Stop the timer A */ 1888c2ecf20Sopenharmony_ci meson6_clkevt_time_stop(); 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci ret = request_irq(irq, meson6_timer_interrupt, 1918c2ecf20Sopenharmony_ci IRQF_TIMER | IRQF_IRQPOLL, "meson6_timer", 1928c2ecf20Sopenharmony_ci &meson6_clockevent); 1938c2ecf20Sopenharmony_ci if (ret) { 1948c2ecf20Sopenharmony_ci pr_warn("failed to setup irq %d\n", irq); 1958c2ecf20Sopenharmony_ci return ret; 1968c2ecf20Sopenharmony_ci } 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci meson6_clockevent.cpumask = cpu_possible_mask; 1998c2ecf20Sopenharmony_ci meson6_clockevent.irq = irq; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci clockevents_config_and_register(&meson6_clockevent, USEC_PER_SEC, 2028c2ecf20Sopenharmony_ci 1, 0xfffe); 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci#ifdef CONFIG_ARM 2058c2ecf20Sopenharmony_ci /* Also use MESON_ISA_TIMERE for delays */ 2068c2ecf20Sopenharmony_ci register_current_timer_delay(&meson6_delay_timer); 2078c2ecf20Sopenharmony_ci#endif 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci return 0; 2108c2ecf20Sopenharmony_ci} 2118c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(meson6, "amlogic,meson6-timer", 2128c2ecf20Sopenharmony_ci meson6_timer_init); 213