18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * Clocksource driver for NXP LPC32xx/18xx/43xx timer 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com> 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Based on: 78c2ecf20Sopenharmony_ci * time-efm32 Copyright (C) 2013 Pengutronix 88c2ecf20Sopenharmony_ci * mach-lpc32xx/timer.c Copyright (C) 2009 - 2010 NXP Semiconductors 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * This file is licensed under the terms of the GNU General Public 118c2ecf20Sopenharmony_ci * License version 2. This program is licensed "as is" without any 128c2ecf20Sopenharmony_ci * warranty of any kind, whether express or implied. 138c2ecf20Sopenharmony_ci * 148c2ecf20Sopenharmony_ci */ 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "%s: " fmt, __func__ 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include <linux/clk.h> 198c2ecf20Sopenharmony_ci#include <linux/clockchips.h> 208c2ecf20Sopenharmony_ci#include <linux/clocksource.h> 218c2ecf20Sopenharmony_ci#include <linux/delay.h> 228c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 238c2ecf20Sopenharmony_ci#include <linux/irq.h> 248c2ecf20Sopenharmony_ci#include <linux/kernel.h> 258c2ecf20Sopenharmony_ci#include <linux/of.h> 268c2ecf20Sopenharmony_ci#include <linux/of_address.h> 278c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 288c2ecf20Sopenharmony_ci#include <linux/sched_clock.h> 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#define LPC32XX_TIMER_IR 0x000 318c2ecf20Sopenharmony_ci#define LPC32XX_TIMER_IR_MR0INT BIT(0) 328c2ecf20Sopenharmony_ci#define LPC32XX_TIMER_TCR 0x004 338c2ecf20Sopenharmony_ci#define LPC32XX_TIMER_TCR_CEN BIT(0) 348c2ecf20Sopenharmony_ci#define LPC32XX_TIMER_TCR_CRST BIT(1) 358c2ecf20Sopenharmony_ci#define LPC32XX_TIMER_TC 0x008 368c2ecf20Sopenharmony_ci#define LPC32XX_TIMER_PR 0x00c 378c2ecf20Sopenharmony_ci#define LPC32XX_TIMER_MCR 0x014 388c2ecf20Sopenharmony_ci#define LPC32XX_TIMER_MCR_MR0I BIT(0) 398c2ecf20Sopenharmony_ci#define LPC32XX_TIMER_MCR_MR0R BIT(1) 408c2ecf20Sopenharmony_ci#define LPC32XX_TIMER_MCR_MR0S BIT(2) 418c2ecf20Sopenharmony_ci#define LPC32XX_TIMER_MR0 0x018 428c2ecf20Sopenharmony_ci#define LPC32XX_TIMER_CTCR 0x070 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistruct lpc32xx_clock_event_ddata { 458c2ecf20Sopenharmony_ci struct clock_event_device evtdev; 468c2ecf20Sopenharmony_ci void __iomem *base; 478c2ecf20Sopenharmony_ci u32 ticks_per_jiffy; 488c2ecf20Sopenharmony_ci}; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/* Needed for the sched clock */ 518c2ecf20Sopenharmony_cistatic void __iomem *clocksource_timer_counter; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic u64 notrace lpc32xx_read_sched_clock(void) 548c2ecf20Sopenharmony_ci{ 558c2ecf20Sopenharmony_ci return readl(clocksource_timer_counter); 568c2ecf20Sopenharmony_ci} 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_cistatic unsigned long lpc32xx_delay_timer_read(void) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci return readl(clocksource_timer_counter); 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_cistatic struct delay_timer lpc32xx_delay_timer = { 648c2ecf20Sopenharmony_ci .read_current_timer = lpc32xx_delay_timer_read, 658c2ecf20Sopenharmony_ci}; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_cistatic int lpc32xx_clkevt_next_event(unsigned long delta, 688c2ecf20Sopenharmony_ci struct clock_event_device *evtdev) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci struct lpc32xx_clock_event_ddata *ddata = 718c2ecf20Sopenharmony_ci container_of(evtdev, struct lpc32xx_clock_event_ddata, evtdev); 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci /* 748c2ecf20Sopenharmony_ci * Place timer in reset and program the delta in the match 758c2ecf20Sopenharmony_ci * channel 0 (MR0). When the timer counter matches the value 768c2ecf20Sopenharmony_ci * in MR0 register the match will trigger an interrupt. 778c2ecf20Sopenharmony_ci * After setup the timer is released from reset and enabled. 788c2ecf20Sopenharmony_ci */ 798c2ecf20Sopenharmony_ci writel_relaxed(LPC32XX_TIMER_TCR_CRST, ddata->base + LPC32XX_TIMER_TCR); 808c2ecf20Sopenharmony_ci writel_relaxed(delta, ddata->base + LPC32XX_TIMER_MR0); 818c2ecf20Sopenharmony_ci writel_relaxed(LPC32XX_TIMER_TCR_CEN, ddata->base + LPC32XX_TIMER_TCR); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci return 0; 848c2ecf20Sopenharmony_ci} 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_cistatic int lpc32xx_clkevt_shutdown(struct clock_event_device *evtdev) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci struct lpc32xx_clock_event_ddata *ddata = 898c2ecf20Sopenharmony_ci container_of(evtdev, struct lpc32xx_clock_event_ddata, evtdev); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci /* Disable the timer */ 928c2ecf20Sopenharmony_ci writel_relaxed(0, ddata->base + LPC32XX_TIMER_TCR); 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci return 0; 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_cistatic int lpc32xx_clkevt_oneshot(struct clock_event_device *evtdev) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci struct lpc32xx_clock_event_ddata *ddata = 1008c2ecf20Sopenharmony_ci container_of(evtdev, struct lpc32xx_clock_event_ddata, evtdev); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci /* 1038c2ecf20Sopenharmony_ci * When using oneshot, we must also disable the timer 1048c2ecf20Sopenharmony_ci * to wait for the first call to set_next_event(). 1058c2ecf20Sopenharmony_ci */ 1068c2ecf20Sopenharmony_ci writel_relaxed(0, ddata->base + LPC32XX_TIMER_TCR); 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci /* Enable interrupt, reset on match and stop on match (MCR). */ 1098c2ecf20Sopenharmony_ci writel_relaxed(LPC32XX_TIMER_MCR_MR0I | LPC32XX_TIMER_MCR_MR0R | 1108c2ecf20Sopenharmony_ci LPC32XX_TIMER_MCR_MR0S, ddata->base + LPC32XX_TIMER_MCR); 1118c2ecf20Sopenharmony_ci return 0; 1128c2ecf20Sopenharmony_ci} 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_cistatic int lpc32xx_clkevt_periodic(struct clock_event_device *evtdev) 1158c2ecf20Sopenharmony_ci{ 1168c2ecf20Sopenharmony_ci struct lpc32xx_clock_event_ddata *ddata = 1178c2ecf20Sopenharmony_ci container_of(evtdev, struct lpc32xx_clock_event_ddata, evtdev); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci /* Enable interrupt and reset on match. */ 1208c2ecf20Sopenharmony_ci writel_relaxed(LPC32XX_TIMER_MCR_MR0I | LPC32XX_TIMER_MCR_MR0R, 1218c2ecf20Sopenharmony_ci ddata->base + LPC32XX_TIMER_MCR); 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci /* 1248c2ecf20Sopenharmony_ci * Place timer in reset and program the delta in the match 1258c2ecf20Sopenharmony_ci * channel 0 (MR0). 1268c2ecf20Sopenharmony_ci */ 1278c2ecf20Sopenharmony_ci writel_relaxed(LPC32XX_TIMER_TCR_CRST, ddata->base + LPC32XX_TIMER_TCR); 1288c2ecf20Sopenharmony_ci writel_relaxed(ddata->ticks_per_jiffy, ddata->base + LPC32XX_TIMER_MR0); 1298c2ecf20Sopenharmony_ci writel_relaxed(LPC32XX_TIMER_TCR_CEN, ddata->base + LPC32XX_TIMER_TCR); 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci return 0; 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic irqreturn_t lpc32xx_clock_event_handler(int irq, void *dev_id) 1358c2ecf20Sopenharmony_ci{ 1368c2ecf20Sopenharmony_ci struct lpc32xx_clock_event_ddata *ddata = dev_id; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci /* Clear match on channel 0 */ 1398c2ecf20Sopenharmony_ci writel_relaxed(LPC32XX_TIMER_IR_MR0INT, ddata->base + LPC32XX_TIMER_IR); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci ddata->evtdev.event_handler(&ddata->evtdev); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci return IRQ_HANDLED; 1448c2ecf20Sopenharmony_ci} 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_cistatic struct lpc32xx_clock_event_ddata lpc32xx_clk_event_ddata = { 1478c2ecf20Sopenharmony_ci .evtdev = { 1488c2ecf20Sopenharmony_ci .name = "lpc3220 clockevent", 1498c2ecf20Sopenharmony_ci .features = CLOCK_EVT_FEAT_ONESHOT | 1508c2ecf20Sopenharmony_ci CLOCK_EVT_FEAT_PERIODIC, 1518c2ecf20Sopenharmony_ci .rating = 300, 1528c2ecf20Sopenharmony_ci .set_next_event = lpc32xx_clkevt_next_event, 1538c2ecf20Sopenharmony_ci .set_state_shutdown = lpc32xx_clkevt_shutdown, 1548c2ecf20Sopenharmony_ci .set_state_oneshot = lpc32xx_clkevt_oneshot, 1558c2ecf20Sopenharmony_ci .set_state_periodic = lpc32xx_clkevt_periodic, 1568c2ecf20Sopenharmony_ci }, 1578c2ecf20Sopenharmony_ci}; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_cistatic int __init lpc32xx_clocksource_init(struct device_node *np) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci void __iomem *base; 1628c2ecf20Sopenharmony_ci unsigned long rate; 1638c2ecf20Sopenharmony_ci struct clk *clk; 1648c2ecf20Sopenharmony_ci int ret; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci clk = of_clk_get_by_name(np, "timerclk"); 1678c2ecf20Sopenharmony_ci if (IS_ERR(clk)) { 1688c2ecf20Sopenharmony_ci pr_err("clock get failed (%ld)\n", PTR_ERR(clk)); 1698c2ecf20Sopenharmony_ci return PTR_ERR(clk); 1708c2ecf20Sopenharmony_ci } 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci ret = clk_prepare_enable(clk); 1738c2ecf20Sopenharmony_ci if (ret) { 1748c2ecf20Sopenharmony_ci pr_err("clock enable failed (%d)\n", ret); 1758c2ecf20Sopenharmony_ci goto err_clk_enable; 1768c2ecf20Sopenharmony_ci } 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci base = of_iomap(np, 0); 1798c2ecf20Sopenharmony_ci if (!base) { 1808c2ecf20Sopenharmony_ci pr_err("unable to map registers\n"); 1818c2ecf20Sopenharmony_ci ret = -EADDRNOTAVAIL; 1828c2ecf20Sopenharmony_ci goto err_iomap; 1838c2ecf20Sopenharmony_ci } 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci /* 1868c2ecf20Sopenharmony_ci * Disable and reset timer then set it to free running timer 1878c2ecf20Sopenharmony_ci * mode (CTCR) with no prescaler (PR) or match operations (MCR). 1888c2ecf20Sopenharmony_ci * After setup the timer is released from reset and enabled. 1898c2ecf20Sopenharmony_ci */ 1908c2ecf20Sopenharmony_ci writel_relaxed(LPC32XX_TIMER_TCR_CRST, base + LPC32XX_TIMER_TCR); 1918c2ecf20Sopenharmony_ci writel_relaxed(0, base + LPC32XX_TIMER_PR); 1928c2ecf20Sopenharmony_ci writel_relaxed(0, base + LPC32XX_TIMER_MCR); 1938c2ecf20Sopenharmony_ci writel_relaxed(0, base + LPC32XX_TIMER_CTCR); 1948c2ecf20Sopenharmony_ci writel_relaxed(LPC32XX_TIMER_TCR_CEN, base + LPC32XX_TIMER_TCR); 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci rate = clk_get_rate(clk); 1978c2ecf20Sopenharmony_ci ret = clocksource_mmio_init(base + LPC32XX_TIMER_TC, "lpc3220 timer", 1988c2ecf20Sopenharmony_ci rate, 300, 32, clocksource_mmio_readl_up); 1998c2ecf20Sopenharmony_ci if (ret) { 2008c2ecf20Sopenharmony_ci pr_err("failed to init clocksource (%d)\n", ret); 2018c2ecf20Sopenharmony_ci goto err_clocksource_init; 2028c2ecf20Sopenharmony_ci } 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci clocksource_timer_counter = base + LPC32XX_TIMER_TC; 2058c2ecf20Sopenharmony_ci lpc32xx_delay_timer.freq = rate; 2068c2ecf20Sopenharmony_ci register_current_timer_delay(&lpc32xx_delay_timer); 2078c2ecf20Sopenharmony_ci sched_clock_register(lpc32xx_read_sched_clock, 32, rate); 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci return 0; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_cierr_clocksource_init: 2128c2ecf20Sopenharmony_ci iounmap(base); 2138c2ecf20Sopenharmony_cierr_iomap: 2148c2ecf20Sopenharmony_ci clk_disable_unprepare(clk); 2158c2ecf20Sopenharmony_cierr_clk_enable: 2168c2ecf20Sopenharmony_ci clk_put(clk); 2178c2ecf20Sopenharmony_ci return ret; 2188c2ecf20Sopenharmony_ci} 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_cistatic int __init lpc32xx_clockevent_init(struct device_node *np) 2218c2ecf20Sopenharmony_ci{ 2228c2ecf20Sopenharmony_ci void __iomem *base; 2238c2ecf20Sopenharmony_ci unsigned long rate; 2248c2ecf20Sopenharmony_ci struct clk *clk; 2258c2ecf20Sopenharmony_ci int ret, irq; 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci clk = of_clk_get_by_name(np, "timerclk"); 2288c2ecf20Sopenharmony_ci if (IS_ERR(clk)) { 2298c2ecf20Sopenharmony_ci pr_err("clock get failed (%ld)\n", PTR_ERR(clk)); 2308c2ecf20Sopenharmony_ci return PTR_ERR(clk); 2318c2ecf20Sopenharmony_ci } 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci ret = clk_prepare_enable(clk); 2348c2ecf20Sopenharmony_ci if (ret) { 2358c2ecf20Sopenharmony_ci pr_err("clock enable failed (%d)\n", ret); 2368c2ecf20Sopenharmony_ci goto err_clk_enable; 2378c2ecf20Sopenharmony_ci } 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci base = of_iomap(np, 0); 2408c2ecf20Sopenharmony_ci if (!base) { 2418c2ecf20Sopenharmony_ci pr_err("unable to map registers\n"); 2428c2ecf20Sopenharmony_ci ret = -EADDRNOTAVAIL; 2438c2ecf20Sopenharmony_ci goto err_iomap; 2448c2ecf20Sopenharmony_ci } 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci irq = irq_of_parse_and_map(np, 0); 2478c2ecf20Sopenharmony_ci if (!irq) { 2488c2ecf20Sopenharmony_ci pr_err("get irq failed\n"); 2498c2ecf20Sopenharmony_ci ret = -ENOENT; 2508c2ecf20Sopenharmony_ci goto err_irq; 2518c2ecf20Sopenharmony_ci } 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci /* 2548c2ecf20Sopenharmony_ci * Disable timer and clear any pending interrupt (IR) on match 2558c2ecf20Sopenharmony_ci * channel 0 (MR0). Clear the prescaler as it's not used. 2568c2ecf20Sopenharmony_ci */ 2578c2ecf20Sopenharmony_ci writel_relaxed(0, base + LPC32XX_TIMER_TCR); 2588c2ecf20Sopenharmony_ci writel_relaxed(0, base + LPC32XX_TIMER_PR); 2598c2ecf20Sopenharmony_ci writel_relaxed(0, base + LPC32XX_TIMER_CTCR); 2608c2ecf20Sopenharmony_ci writel_relaxed(LPC32XX_TIMER_IR_MR0INT, base + LPC32XX_TIMER_IR); 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci rate = clk_get_rate(clk); 2638c2ecf20Sopenharmony_ci lpc32xx_clk_event_ddata.base = base; 2648c2ecf20Sopenharmony_ci lpc32xx_clk_event_ddata.ticks_per_jiffy = DIV_ROUND_CLOSEST(rate, HZ); 2658c2ecf20Sopenharmony_ci clockevents_config_and_register(&lpc32xx_clk_event_ddata.evtdev, 2668c2ecf20Sopenharmony_ci rate, 1, -1); 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci ret = request_irq(irq, lpc32xx_clock_event_handler, 2698c2ecf20Sopenharmony_ci IRQF_TIMER | IRQF_IRQPOLL, "lpc3220 clockevent", 2708c2ecf20Sopenharmony_ci &lpc32xx_clk_event_ddata); 2718c2ecf20Sopenharmony_ci if (ret) { 2728c2ecf20Sopenharmony_ci pr_err("request irq failed\n"); 2738c2ecf20Sopenharmony_ci goto err_irq; 2748c2ecf20Sopenharmony_ci } 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci return 0; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_cierr_irq: 2798c2ecf20Sopenharmony_ci iounmap(base); 2808c2ecf20Sopenharmony_cierr_iomap: 2818c2ecf20Sopenharmony_ci clk_disable_unprepare(clk); 2828c2ecf20Sopenharmony_cierr_clk_enable: 2838c2ecf20Sopenharmony_ci clk_put(clk); 2848c2ecf20Sopenharmony_ci return ret; 2858c2ecf20Sopenharmony_ci} 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_ci/* 2888c2ecf20Sopenharmony_ci * This function asserts that we have exactly one clocksource and one 2898c2ecf20Sopenharmony_ci * clock_event_device in the end. 2908c2ecf20Sopenharmony_ci */ 2918c2ecf20Sopenharmony_cistatic int __init lpc32xx_timer_init(struct device_node *np) 2928c2ecf20Sopenharmony_ci{ 2938c2ecf20Sopenharmony_ci static int has_clocksource, has_clockevent; 2948c2ecf20Sopenharmony_ci int ret = 0; 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ci if (!has_clocksource) { 2978c2ecf20Sopenharmony_ci ret = lpc32xx_clocksource_init(np); 2988c2ecf20Sopenharmony_ci if (!ret) { 2998c2ecf20Sopenharmony_ci has_clocksource = 1; 3008c2ecf20Sopenharmony_ci return 0; 3018c2ecf20Sopenharmony_ci } 3028c2ecf20Sopenharmony_ci } 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ci if (!has_clockevent) { 3058c2ecf20Sopenharmony_ci ret = lpc32xx_clockevent_init(np); 3068c2ecf20Sopenharmony_ci if (!ret) { 3078c2ecf20Sopenharmony_ci has_clockevent = 1; 3088c2ecf20Sopenharmony_ci return 0; 3098c2ecf20Sopenharmony_ci } 3108c2ecf20Sopenharmony_ci } 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci return ret; 3138c2ecf20Sopenharmony_ci} 3148c2ecf20Sopenharmony_ciTIMER_OF_DECLARE(lpc32xx_timer, "nxp,lpc3220-timer", lpc32xx_timer_init); 315