18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2014 Oleksij Rempel <linux@rempel-privat.de> 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/kernel.h> 78c2ecf20Sopenharmony_ci#include <linux/init.h> 88c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 98c2ecf20Sopenharmony_ci#include <linux/sched.h> 108c2ecf20Sopenharmony_ci#include <linux/clk.h> 118c2ecf20Sopenharmony_ci#include <linux/clocksource.h> 128c2ecf20Sopenharmony_ci#include <linux/clockchips.h> 138c2ecf20Sopenharmony_ci#include <linux/io.h> 148c2ecf20Sopenharmony_ci#include <linux/of.h> 158c2ecf20Sopenharmony_ci#include <linux/of_address.h> 168c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 178c2ecf20Sopenharmony_ci#include <linux/bitops.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define DRIVER_NAME "asm9260-timer" 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci/* 228c2ecf20Sopenharmony_ci * this device provide 4 offsets for each register: 238c2ecf20Sopenharmony_ci * 0x0 - plain read write mode 248c2ecf20Sopenharmony_ci * 0x4 - set mode, OR logic. 258c2ecf20Sopenharmony_ci * 0x8 - clr mode, XOR logic. 268c2ecf20Sopenharmony_ci * 0xc - togle mode. 278c2ecf20Sopenharmony_ci */ 288c2ecf20Sopenharmony_ci#define SET_REG 4 298c2ecf20Sopenharmony_ci#define CLR_REG 8 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define HW_IR 0x0000 /* RW. Interrupt */ 328c2ecf20Sopenharmony_ci#define BM_IR_CR0 BIT(4) 338c2ecf20Sopenharmony_ci#define BM_IR_MR3 BIT(3) 348c2ecf20Sopenharmony_ci#define BM_IR_MR2 BIT(2) 358c2ecf20Sopenharmony_ci#define BM_IR_MR1 BIT(1) 368c2ecf20Sopenharmony_ci#define BM_IR_MR0 BIT(0) 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#define HW_TCR 0x0010 /* RW. Timer controller */ 398c2ecf20Sopenharmony_ci/* BM_C*_RST 408c2ecf20Sopenharmony_ci * Timer Counter and the Prescale Counter are synchronously reset on the 418c2ecf20Sopenharmony_ci * next positive edge of PCLK. The counters remain reset until TCR[1] is 428c2ecf20Sopenharmony_ci * returned to zero. */ 438c2ecf20Sopenharmony_ci#define BM_C3_RST BIT(7) 448c2ecf20Sopenharmony_ci#define BM_C2_RST BIT(6) 458c2ecf20Sopenharmony_ci#define BM_C1_RST BIT(5) 468c2ecf20Sopenharmony_ci#define BM_C0_RST BIT(4) 478c2ecf20Sopenharmony_ci/* BM_C*_EN 488c2ecf20Sopenharmony_ci * 1 - Timer Counter and Prescale Counter are enabled for counting 498c2ecf20Sopenharmony_ci * 0 - counters are disabled */ 508c2ecf20Sopenharmony_ci#define BM_C3_EN BIT(3) 518c2ecf20Sopenharmony_ci#define BM_C2_EN BIT(2) 528c2ecf20Sopenharmony_ci#define BM_C1_EN BIT(1) 538c2ecf20Sopenharmony_ci#define BM_C0_EN BIT(0) 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#define HW_DIR 0x0020 /* RW. Direction? */ 568c2ecf20Sopenharmony_ci/* 00 - count up 578c2ecf20Sopenharmony_ci * 01 - count down 588c2ecf20Sopenharmony_ci * 10 - ?? 2^n/2 */ 598c2ecf20Sopenharmony_ci#define BM_DIR_COUNT_UP 0 608c2ecf20Sopenharmony_ci#define BM_DIR_COUNT_DOWN 1 618c2ecf20Sopenharmony_ci#define BM_DIR0_SHIFT 0 628c2ecf20Sopenharmony_ci#define BM_DIR1_SHIFT 4 638c2ecf20Sopenharmony_ci#define BM_DIR2_SHIFT 8 648c2ecf20Sopenharmony_ci#define BM_DIR3_SHIFT 12 658c2ecf20Sopenharmony_ci#define BM_DIR_DEFAULT (BM_DIR_COUNT_UP << BM_DIR0_SHIFT | \ 668c2ecf20Sopenharmony_ci BM_DIR_COUNT_UP << BM_DIR1_SHIFT | \ 678c2ecf20Sopenharmony_ci BM_DIR_COUNT_UP << BM_DIR2_SHIFT | \ 688c2ecf20Sopenharmony_ci BM_DIR_COUNT_UP << BM_DIR3_SHIFT) 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci#define HW_TC0 0x0030 /* RO. Timer counter 0 */ 718c2ecf20Sopenharmony_ci/* HW_TC*. Timer counter owerflow (0xffff.ffff to 0x0000.0000) do not generate 728c2ecf20Sopenharmony_ci * interrupt. This registers can be used to detect overflow */ 738c2ecf20Sopenharmony_ci#define HW_TC1 0x0040 748c2ecf20Sopenharmony_ci#define HW_TC2 0x0050 758c2ecf20Sopenharmony_ci#define HW_TC3 0x0060 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci#define HW_PR 0x0070 /* RW. prescaler */ 788c2ecf20Sopenharmony_ci#define BM_PR_DISABLE 0 798c2ecf20Sopenharmony_ci#define HW_PC 0x0080 /* RO. Prescaler counter */ 808c2ecf20Sopenharmony_ci#define HW_MCR 0x0090 /* RW. Match control */ 818c2ecf20Sopenharmony_ci/* enable interrupt on match */ 828c2ecf20Sopenharmony_ci#define BM_MCR_INT_EN(n) (1 << (n * 3 + 0)) 838c2ecf20Sopenharmony_ci/* enable TC reset on match */ 848c2ecf20Sopenharmony_ci#define BM_MCR_RES_EN(n) (1 << (n * 3 + 1)) 858c2ecf20Sopenharmony_ci/* enable stop TC on match */ 868c2ecf20Sopenharmony_ci#define BM_MCR_STOP_EN(n) (1 << (n * 3 + 2)) 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci#define HW_MR0 0x00a0 /* RW. Match reg */ 898c2ecf20Sopenharmony_ci#define HW_MR1 0x00b0 908c2ecf20Sopenharmony_ci#define HW_MR2 0x00C0 918c2ecf20Sopenharmony_ci#define HW_MR3 0x00D0 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci#define HW_CTCR 0x0180 /* Counter control */ 948c2ecf20Sopenharmony_ci#define BM_CTCR0_SHIFT 0 958c2ecf20Sopenharmony_ci#define BM_CTCR1_SHIFT 2 968c2ecf20Sopenharmony_ci#define BM_CTCR2_SHIFT 4 978c2ecf20Sopenharmony_ci#define BM_CTCR3_SHIFT 6 988c2ecf20Sopenharmony_ci#define BM_CTCR_TM 0 /* Timer mode. Every rising PCLK edge. */ 998c2ecf20Sopenharmony_ci#define BM_CTCR_DEFAULT (BM_CTCR_TM << BM_CTCR0_SHIFT | \ 1008c2ecf20Sopenharmony_ci BM_CTCR_TM << BM_CTCR1_SHIFT | \ 1018c2ecf20Sopenharmony_ci BM_CTCR_TM << BM_CTCR2_SHIFT | \ 1028c2ecf20Sopenharmony_ci BM_CTCR_TM << BM_CTCR3_SHIFT) 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistatic struct asm9260_timer_priv { 1058c2ecf20Sopenharmony_ci void __iomem *base; 1068c2ecf20Sopenharmony_ci unsigned long ticks_per_jiffy; 1078c2ecf20Sopenharmony_ci} priv; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic int asm9260_timer_set_next_event(unsigned long delta, 1108c2ecf20Sopenharmony_ci struct clock_event_device *evt) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci /* configure match count for TC0 */ 1138c2ecf20Sopenharmony_ci writel_relaxed(delta, priv.base + HW_MR0); 1148c2ecf20Sopenharmony_ci /* enable TC0 */ 1158c2ecf20Sopenharmony_ci writel_relaxed(BM_C0_EN, priv.base + HW_TCR + SET_REG); 1168c2ecf20Sopenharmony_ci return 0; 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cistatic inline void __asm9260_timer_shutdown(struct clock_event_device *evt) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci /* stop timer0 */ 1228c2ecf20Sopenharmony_ci writel_relaxed(BM_C0_EN, priv.base + HW_TCR + CLR_REG); 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_cistatic int asm9260_timer_shutdown(struct clock_event_device *evt) 1268c2ecf20Sopenharmony_ci{ 1278c2ecf20Sopenharmony_ci __asm9260_timer_shutdown(evt); 1288c2ecf20Sopenharmony_ci return 0; 1298c2ecf20Sopenharmony_ci} 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_cistatic int asm9260_timer_set_oneshot(struct clock_event_device *evt) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci __asm9260_timer_shutdown(evt); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci /* enable reset and stop on match */ 1368c2ecf20Sopenharmony_ci writel_relaxed(BM_MCR_RES_EN(0) | BM_MCR_STOP_EN(0), 1378c2ecf20Sopenharmony_ci priv.base + HW_MCR + SET_REG); 1388c2ecf20Sopenharmony_ci return 0; 1398c2ecf20Sopenharmony_ci} 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_cistatic int asm9260_timer_set_periodic(struct clock_event_device *evt) 1428c2ecf20Sopenharmony_ci{ 1438c2ecf20Sopenharmony_ci __asm9260_timer_shutdown(evt); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci /* disable reset and stop on match */ 1468c2ecf20Sopenharmony_ci writel_relaxed(BM_MCR_RES_EN(0) | BM_MCR_STOP_EN(0), 1478c2ecf20Sopenharmony_ci priv.base + HW_MCR + CLR_REG); 1488c2ecf20Sopenharmony_ci /* configure match count for TC0 */ 1498c2ecf20Sopenharmony_ci writel_relaxed(priv.ticks_per_jiffy, priv.base + HW_MR0); 1508c2ecf20Sopenharmony_ci /* enable TC0 */ 1518c2ecf20Sopenharmony_ci writel_relaxed(BM_C0_EN, priv.base + HW_TCR + SET_REG); 1528c2ecf20Sopenharmony_ci return 0; 1538c2ecf20Sopenharmony_ci} 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_cistatic struct clock_event_device event_dev = { 1568c2ecf20Sopenharmony_ci .name = DRIVER_NAME, 1578c2ecf20Sopenharmony_ci .rating = 200, 1588c2ecf20Sopenharmony_ci .features = CLOCK_EVT_FEAT_PERIODIC | 1598c2ecf20Sopenharmony_ci CLOCK_EVT_FEAT_ONESHOT, 1608c2ecf20Sopenharmony_ci .set_next_event = asm9260_timer_set_next_event, 1618c2ecf20Sopenharmony_ci .set_state_shutdown = asm9260_timer_shutdown, 1628c2ecf20Sopenharmony_ci .set_state_periodic = asm9260_timer_set_periodic, 1638c2ecf20Sopenharmony_ci .set_state_oneshot = asm9260_timer_set_oneshot, 1648c2ecf20Sopenharmony_ci .tick_resume = asm9260_timer_shutdown, 1658c2ecf20Sopenharmony_ci}; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_cistatic irqreturn_t asm9260_timer_interrupt(int irq, void *dev_id) 1688c2ecf20Sopenharmony_ci{ 1698c2ecf20Sopenharmony_ci struct clock_event_device *evt = dev_id; 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci evt->event_handler(evt); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci writel_relaxed(BM_IR_MR0, priv.base + HW_IR); 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci return IRQ_HANDLED; 1768c2ecf20Sopenharmony_ci} 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci/* 1798c2ecf20Sopenharmony_ci * --------------------------------------------------------------------------- 1808c2ecf20Sopenharmony_ci * Timer initialization 1818c2ecf20Sopenharmony_ci * --------------------------------------------------------------------------- 1828c2ecf20Sopenharmony_ci */ 1838c2ecf20Sopenharmony_cistatic int __init asm9260_timer_init(struct device_node *np) 1848c2ecf20Sopenharmony_ci{ 1858c2ecf20Sopenharmony_ci int irq; 1868c2ecf20Sopenharmony_ci struct clk *clk; 1878c2ecf20Sopenharmony_ci int ret; 1888c2ecf20Sopenharmony_ci unsigned long rate; 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci priv.base = of_io_request_and_map(np, 0, np->name); 1918c2ecf20Sopenharmony_ci if (IS_ERR(priv.base)) { 1928c2ecf20Sopenharmony_ci pr_err("%pOFn: unable to map resource\n", np); 1938c2ecf20Sopenharmony_ci return PTR_ERR(priv.base); 1948c2ecf20Sopenharmony_ci } 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci clk = of_clk_get(np, 0); 1978c2ecf20Sopenharmony_ci if (IS_ERR(clk)) { 1988c2ecf20Sopenharmony_ci pr_err("Failed to get clk!\n"); 1998c2ecf20Sopenharmony_ci return PTR_ERR(clk); 2008c2ecf20Sopenharmony_ci } 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci ret = clk_prepare_enable(clk); 2038c2ecf20Sopenharmony_ci if (ret) { 2048c2ecf20Sopenharmony_ci pr_err("Failed to enable clk!\n"); 2058c2ecf20Sopenharmony_ci return ret; 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci irq = irq_of_parse_and_map(np, 0); 2098c2ecf20Sopenharmony_ci ret = request_irq(irq, asm9260_timer_interrupt, IRQF_TIMER, 2108c2ecf20Sopenharmony_ci DRIVER_NAME, &event_dev); 2118c2ecf20Sopenharmony_ci if (ret) { 2128c2ecf20Sopenharmony_ci pr_err("Failed to setup irq!\n"); 2138c2ecf20Sopenharmony_ci return ret; 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci /* set all timers for count-up */ 2178c2ecf20Sopenharmony_ci writel_relaxed(BM_DIR_DEFAULT, priv.base + HW_DIR); 2188c2ecf20Sopenharmony_ci /* disable divider */ 2198c2ecf20Sopenharmony_ci writel_relaxed(BM_PR_DISABLE, priv.base + HW_PR); 2208c2ecf20Sopenharmony_ci /* make sure all timers use every rising PCLK edge. */ 2218c2ecf20Sopenharmony_ci writel_relaxed(BM_CTCR_DEFAULT, priv.base + HW_CTCR); 2228c2ecf20Sopenharmony_ci /* enable interrupt for TC0 and clean setting for all other lines */ 2238c2ecf20Sopenharmony_ci writel_relaxed(BM_MCR_INT_EN(0) , priv.base + HW_MCR); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci rate = clk_get_rate(clk); 2268c2ecf20Sopenharmony_ci clocksource_mmio_init(priv.base + HW_TC1, DRIVER_NAME, rate, 2278c2ecf20Sopenharmony_ci 200, 32, clocksource_mmio_readl_up); 2288c2ecf20Sopenharmony_ci 2298c2ecf20Sopenharmony_ci /* Seems like we can't use counter without match register even if 2308c2ecf20Sopenharmony_ci * actions for MR are disabled. So, set MR to max value. */ 2318c2ecf20Sopenharmony_ci writel_relaxed(0xffffffff, priv.base + HW_MR1); 2328c2ecf20Sopenharmony_ci /* enable TC1 */ 2338c2ecf20Sopenharmony_ci writel_relaxed(BM_C1_EN, priv.base + HW_TCR + SET_REG); 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_ci priv.ticks_per_jiffy = DIV_ROUND_CLOSEST(rate, HZ); 2368c2ecf20Sopenharmony_ci event_dev.cpumask = cpumask_of(0); 2378c2ecf20Sopenharmony_ci clockevents_config_and_register(&event_dev, rate, 0x2c00, 0xfffffffe); 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci return 0; 2408c2ecf20Sopenharmony_ci} 2418c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(asm9260_timer, "alphascale,asm9260-timer", 2428c2ecf20Sopenharmony_ci asm9260_timer_init); 243