18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci#include <linux/kernel.h> 38c2ecf20Sopenharmony_ci#include <linux/init.h> 48c2ecf20Sopenharmony_ci#include <linux/clocksource.h> 58c2ecf20Sopenharmony_ci#include <linux/clockchips.h> 68c2ecf20Sopenharmony_ci#include <linux/sched_clock.h> 78c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 88c2ecf20Sopenharmony_ci#include <linux/irq.h> 98c2ecf20Sopenharmony_ci#include <linux/io.h> 108c2ecf20Sopenharmony_ci#include <asm/mach/time.h> 118c2ecf20Sopenharmony_ci#include "soc.h" 128c2ecf20Sopenharmony_ci#include "platform.h" 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci/************************************************************************* 158c2ecf20Sopenharmony_ci * Timer handling for EP93xx 168c2ecf20Sopenharmony_ci ************************************************************************* 178c2ecf20Sopenharmony_ci * The ep93xx has four internal timers. Timers 1, 2 (both 16 bit) and 188c2ecf20Sopenharmony_ci * 3 (32 bit) count down at 508 kHz, are self-reloading, and can generate 198c2ecf20Sopenharmony_ci * an interrupt on underflow. Timer 4 (40 bit) counts down at 983.04 kHz, 208c2ecf20Sopenharmony_ci * is free-running, and can't generate interrupts. 218c2ecf20Sopenharmony_ci * 228c2ecf20Sopenharmony_ci * The 508 kHz timers are ideal for use for the timer interrupt, as the 238c2ecf20Sopenharmony_ci * most common values of HZ divide 508 kHz nicely. We pick the 32 bit 248c2ecf20Sopenharmony_ci * timer (timer 3) to get as long sleep intervals as possible when using 258c2ecf20Sopenharmony_ci * CONFIG_NO_HZ. 268c2ecf20Sopenharmony_ci * 278c2ecf20Sopenharmony_ci * The higher clock rate of timer 4 makes it a better choice than the 288c2ecf20Sopenharmony_ci * other timers for use as clock source and for sched_clock(), providing 298c2ecf20Sopenharmony_ci * a stable 40 bit time base. 308c2ecf20Sopenharmony_ci ************************************************************************* 318c2ecf20Sopenharmony_ci */ 328c2ecf20Sopenharmony_ci#define EP93XX_TIMER_REG(x) (EP93XX_TIMER_BASE + (x)) 338c2ecf20Sopenharmony_ci#define EP93XX_TIMER1_LOAD EP93XX_TIMER_REG(0x00) 348c2ecf20Sopenharmony_ci#define EP93XX_TIMER1_VALUE EP93XX_TIMER_REG(0x04) 358c2ecf20Sopenharmony_ci#define EP93XX_TIMER1_CONTROL EP93XX_TIMER_REG(0x08) 368c2ecf20Sopenharmony_ci#define EP93XX_TIMER123_CONTROL_ENABLE (1 << 7) 378c2ecf20Sopenharmony_ci#define EP93XX_TIMER123_CONTROL_MODE (1 << 6) 388c2ecf20Sopenharmony_ci#define EP93XX_TIMER123_CONTROL_CLKSEL (1 << 3) 398c2ecf20Sopenharmony_ci#define EP93XX_TIMER1_CLEAR EP93XX_TIMER_REG(0x0c) 408c2ecf20Sopenharmony_ci#define EP93XX_TIMER2_LOAD EP93XX_TIMER_REG(0x20) 418c2ecf20Sopenharmony_ci#define EP93XX_TIMER2_VALUE EP93XX_TIMER_REG(0x24) 428c2ecf20Sopenharmony_ci#define EP93XX_TIMER2_CONTROL EP93XX_TIMER_REG(0x28) 438c2ecf20Sopenharmony_ci#define EP93XX_TIMER2_CLEAR EP93XX_TIMER_REG(0x2c) 448c2ecf20Sopenharmony_ci#define EP93XX_TIMER4_VALUE_LOW EP93XX_TIMER_REG(0x60) 458c2ecf20Sopenharmony_ci#define EP93XX_TIMER4_VALUE_HIGH EP93XX_TIMER_REG(0x64) 468c2ecf20Sopenharmony_ci#define EP93XX_TIMER4_VALUE_HIGH_ENABLE (1 << 8) 478c2ecf20Sopenharmony_ci#define EP93XX_TIMER3_LOAD EP93XX_TIMER_REG(0x80) 488c2ecf20Sopenharmony_ci#define EP93XX_TIMER3_VALUE EP93XX_TIMER_REG(0x84) 498c2ecf20Sopenharmony_ci#define EP93XX_TIMER3_CONTROL EP93XX_TIMER_REG(0x88) 508c2ecf20Sopenharmony_ci#define EP93XX_TIMER3_CLEAR EP93XX_TIMER_REG(0x8c) 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#define EP93XX_TIMER123_RATE 508469 538c2ecf20Sopenharmony_ci#define EP93XX_TIMER4_RATE 983040 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_cistatic u64 notrace ep93xx_read_sched_clock(void) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci u64 ret; 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci ret = readl(EP93XX_TIMER4_VALUE_LOW); 608c2ecf20Sopenharmony_ci ret |= ((u64) (readl(EP93XX_TIMER4_VALUE_HIGH) & 0xff) << 32); 618c2ecf20Sopenharmony_ci return ret; 628c2ecf20Sopenharmony_ci} 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistatic u64 ep93xx_clocksource_read(struct clocksource *c) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci u64 ret; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci ret = readl(EP93XX_TIMER4_VALUE_LOW); 698c2ecf20Sopenharmony_ci ret |= ((u64) (readl(EP93XX_TIMER4_VALUE_HIGH) & 0xff) << 32); 708c2ecf20Sopenharmony_ci return (u64) ret; 718c2ecf20Sopenharmony_ci} 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistatic int ep93xx_clkevt_set_next_event(unsigned long next, 748c2ecf20Sopenharmony_ci struct clock_event_device *evt) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci /* Default mode: periodic, off, 508 kHz */ 778c2ecf20Sopenharmony_ci u32 tmode = EP93XX_TIMER123_CONTROL_MODE | 788c2ecf20Sopenharmony_ci EP93XX_TIMER123_CONTROL_CLKSEL; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci /* Clear timer */ 818c2ecf20Sopenharmony_ci writel(tmode, EP93XX_TIMER3_CONTROL); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci /* Set next event */ 848c2ecf20Sopenharmony_ci writel(next, EP93XX_TIMER3_LOAD); 858c2ecf20Sopenharmony_ci writel(tmode | EP93XX_TIMER123_CONTROL_ENABLE, 868c2ecf20Sopenharmony_ci EP93XX_TIMER3_CONTROL); 878c2ecf20Sopenharmony_ci return 0; 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cistatic int ep93xx_clkevt_shutdown(struct clock_event_device *evt) 928c2ecf20Sopenharmony_ci{ 938c2ecf20Sopenharmony_ci /* Disable timer */ 948c2ecf20Sopenharmony_ci writel(0, EP93XX_TIMER3_CONTROL); 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci return 0; 978c2ecf20Sopenharmony_ci} 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_cistatic struct clock_event_device ep93xx_clockevent = { 1008c2ecf20Sopenharmony_ci .name = "timer1", 1018c2ecf20Sopenharmony_ci .features = CLOCK_EVT_FEAT_ONESHOT, 1028c2ecf20Sopenharmony_ci .set_state_shutdown = ep93xx_clkevt_shutdown, 1038c2ecf20Sopenharmony_ci .set_state_oneshot = ep93xx_clkevt_shutdown, 1048c2ecf20Sopenharmony_ci .tick_resume = ep93xx_clkevt_shutdown, 1058c2ecf20Sopenharmony_ci .set_next_event = ep93xx_clkevt_set_next_event, 1068c2ecf20Sopenharmony_ci .rating = 300, 1078c2ecf20Sopenharmony_ci}; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic irqreturn_t ep93xx_timer_interrupt(int irq, void *dev_id) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci struct clock_event_device *evt = dev_id; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci /* Writing any value clears the timer interrupt */ 1148c2ecf20Sopenharmony_ci writel(1, EP93XX_TIMER3_CLEAR); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci evt->event_handler(evt); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci return IRQ_HANDLED; 1198c2ecf20Sopenharmony_ci} 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_civoid __init ep93xx_timer_init(void) 1228c2ecf20Sopenharmony_ci{ 1238c2ecf20Sopenharmony_ci int irq = IRQ_EP93XX_TIMER3; 1248c2ecf20Sopenharmony_ci unsigned long flags = IRQF_TIMER | IRQF_IRQPOLL; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci /* Enable and register clocksource and sched_clock on timer 4 */ 1278c2ecf20Sopenharmony_ci writel(EP93XX_TIMER4_VALUE_HIGH_ENABLE, 1288c2ecf20Sopenharmony_ci EP93XX_TIMER4_VALUE_HIGH); 1298c2ecf20Sopenharmony_ci clocksource_mmio_init(NULL, "timer4", 1308c2ecf20Sopenharmony_ci EP93XX_TIMER4_RATE, 200, 40, 1318c2ecf20Sopenharmony_ci ep93xx_clocksource_read); 1328c2ecf20Sopenharmony_ci sched_clock_register(ep93xx_read_sched_clock, 40, 1338c2ecf20Sopenharmony_ci EP93XX_TIMER4_RATE); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci /* Set up clockevent on timer 3 */ 1368c2ecf20Sopenharmony_ci if (request_irq(irq, ep93xx_timer_interrupt, flags, "ep93xx timer", 1378c2ecf20Sopenharmony_ci &ep93xx_clockevent)) 1388c2ecf20Sopenharmony_ci pr_err("Failed to request irq %d (ep93xx timer)\n", irq); 1398c2ecf20Sopenharmony_ci clockevents_config_and_register(&ep93xx_clockevent, 1408c2ecf20Sopenharmony_ci EP93XX_TIMER123_RATE, 1418c2ecf20Sopenharmony_ci 1, 1428c2ecf20Sopenharmony_ci 0xffffffffU); 1438c2ecf20Sopenharmony_ci} 144