162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci#include <linux/kernel.h> 362306a36Sopenharmony_ci#include <linux/init.h> 462306a36Sopenharmony_ci#include <linux/clocksource.h> 562306a36Sopenharmony_ci#include <linux/clockchips.h> 662306a36Sopenharmony_ci#include <linux/sched_clock.h> 762306a36Sopenharmony_ci#include <linux/interrupt.h> 862306a36Sopenharmony_ci#include <linux/irq.h> 962306a36Sopenharmony_ci#include <linux/io.h> 1062306a36Sopenharmony_ci#include <asm/mach/time.h> 1162306a36Sopenharmony_ci#include "soc.h" 1262306a36Sopenharmony_ci#include "platform.h" 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci/************************************************************************* 1562306a36Sopenharmony_ci * Timer handling for EP93xx 1662306a36Sopenharmony_ci ************************************************************************* 1762306a36Sopenharmony_ci * The ep93xx has four internal timers. Timers 1, 2 (both 16 bit) and 1862306a36Sopenharmony_ci * 3 (32 bit) count down at 508 kHz, are self-reloading, and can generate 1962306a36Sopenharmony_ci * an interrupt on underflow. Timer 4 (40 bit) counts down at 983.04 kHz, 2062306a36Sopenharmony_ci * is free-running, and can't generate interrupts. 2162306a36Sopenharmony_ci * 2262306a36Sopenharmony_ci * The 508 kHz timers are ideal for use for the timer interrupt, as the 2362306a36Sopenharmony_ci * most common values of HZ divide 508 kHz nicely. We pick the 32 bit 2462306a36Sopenharmony_ci * timer (timer 3) to get as long sleep intervals as possible when using 2562306a36Sopenharmony_ci * CONFIG_NO_HZ. 2662306a36Sopenharmony_ci * 2762306a36Sopenharmony_ci * The higher clock rate of timer 4 makes it a better choice than the 2862306a36Sopenharmony_ci * other timers for use as clock source and for sched_clock(), providing 2962306a36Sopenharmony_ci * a stable 40 bit time base. 3062306a36Sopenharmony_ci ************************************************************************* 3162306a36Sopenharmony_ci */ 3262306a36Sopenharmony_ci#define EP93XX_TIMER_REG(x) (EP93XX_TIMER_BASE + (x)) 3362306a36Sopenharmony_ci#define EP93XX_TIMER1_LOAD EP93XX_TIMER_REG(0x00) 3462306a36Sopenharmony_ci#define EP93XX_TIMER1_VALUE EP93XX_TIMER_REG(0x04) 3562306a36Sopenharmony_ci#define EP93XX_TIMER1_CONTROL EP93XX_TIMER_REG(0x08) 3662306a36Sopenharmony_ci#define EP93XX_TIMER123_CONTROL_ENABLE (1 << 7) 3762306a36Sopenharmony_ci#define EP93XX_TIMER123_CONTROL_MODE (1 << 6) 3862306a36Sopenharmony_ci#define EP93XX_TIMER123_CONTROL_CLKSEL (1 << 3) 3962306a36Sopenharmony_ci#define EP93XX_TIMER1_CLEAR EP93XX_TIMER_REG(0x0c) 4062306a36Sopenharmony_ci#define EP93XX_TIMER2_LOAD EP93XX_TIMER_REG(0x20) 4162306a36Sopenharmony_ci#define EP93XX_TIMER2_VALUE EP93XX_TIMER_REG(0x24) 4262306a36Sopenharmony_ci#define EP93XX_TIMER2_CONTROL EP93XX_TIMER_REG(0x28) 4362306a36Sopenharmony_ci#define EP93XX_TIMER2_CLEAR EP93XX_TIMER_REG(0x2c) 4462306a36Sopenharmony_ci#define EP93XX_TIMER4_VALUE_LOW EP93XX_TIMER_REG(0x60) 4562306a36Sopenharmony_ci#define EP93XX_TIMER4_VALUE_HIGH EP93XX_TIMER_REG(0x64) 4662306a36Sopenharmony_ci#define EP93XX_TIMER4_VALUE_HIGH_ENABLE (1 << 8) 4762306a36Sopenharmony_ci#define EP93XX_TIMER3_LOAD EP93XX_TIMER_REG(0x80) 4862306a36Sopenharmony_ci#define EP93XX_TIMER3_VALUE EP93XX_TIMER_REG(0x84) 4962306a36Sopenharmony_ci#define EP93XX_TIMER3_CONTROL EP93XX_TIMER_REG(0x88) 5062306a36Sopenharmony_ci#define EP93XX_TIMER3_CLEAR EP93XX_TIMER_REG(0x8c) 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci#define EP93XX_TIMER123_RATE 508469 5362306a36Sopenharmony_ci#define EP93XX_TIMER4_RATE 983040 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_cistatic u64 notrace ep93xx_read_sched_clock(void) 5662306a36Sopenharmony_ci{ 5762306a36Sopenharmony_ci u64 ret; 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_ci ret = readl(EP93XX_TIMER4_VALUE_LOW); 6062306a36Sopenharmony_ci ret |= ((u64) (readl(EP93XX_TIMER4_VALUE_HIGH) & 0xff) << 32); 6162306a36Sopenharmony_ci return ret; 6262306a36Sopenharmony_ci} 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_cistatic u64 ep93xx_clocksource_read(struct clocksource *c) 6562306a36Sopenharmony_ci{ 6662306a36Sopenharmony_ci u64 ret; 6762306a36Sopenharmony_ci 6862306a36Sopenharmony_ci ret = readl(EP93XX_TIMER4_VALUE_LOW); 6962306a36Sopenharmony_ci ret |= ((u64) (readl(EP93XX_TIMER4_VALUE_HIGH) & 0xff) << 32); 7062306a36Sopenharmony_ci return (u64) ret; 7162306a36Sopenharmony_ci} 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_cistatic int ep93xx_clkevt_set_next_event(unsigned long next, 7462306a36Sopenharmony_ci struct clock_event_device *evt) 7562306a36Sopenharmony_ci{ 7662306a36Sopenharmony_ci /* Default mode: periodic, off, 508 kHz */ 7762306a36Sopenharmony_ci u32 tmode = EP93XX_TIMER123_CONTROL_MODE | 7862306a36Sopenharmony_ci EP93XX_TIMER123_CONTROL_CLKSEL; 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci /* Clear timer */ 8162306a36Sopenharmony_ci writel(tmode, EP93XX_TIMER3_CONTROL); 8262306a36Sopenharmony_ci 8362306a36Sopenharmony_ci /* Set next event */ 8462306a36Sopenharmony_ci writel(next, EP93XX_TIMER3_LOAD); 8562306a36Sopenharmony_ci writel(tmode | EP93XX_TIMER123_CONTROL_ENABLE, 8662306a36Sopenharmony_ci EP93XX_TIMER3_CONTROL); 8762306a36Sopenharmony_ci return 0; 8862306a36Sopenharmony_ci} 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci 9162306a36Sopenharmony_cistatic int ep93xx_clkevt_shutdown(struct clock_event_device *evt) 9262306a36Sopenharmony_ci{ 9362306a36Sopenharmony_ci /* Disable timer */ 9462306a36Sopenharmony_ci writel(0, EP93XX_TIMER3_CONTROL); 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci return 0; 9762306a36Sopenharmony_ci} 9862306a36Sopenharmony_ci 9962306a36Sopenharmony_cistatic struct clock_event_device ep93xx_clockevent = { 10062306a36Sopenharmony_ci .name = "timer1", 10162306a36Sopenharmony_ci .features = CLOCK_EVT_FEAT_ONESHOT, 10262306a36Sopenharmony_ci .set_state_shutdown = ep93xx_clkevt_shutdown, 10362306a36Sopenharmony_ci .set_state_oneshot = ep93xx_clkevt_shutdown, 10462306a36Sopenharmony_ci .tick_resume = ep93xx_clkevt_shutdown, 10562306a36Sopenharmony_ci .set_next_event = ep93xx_clkevt_set_next_event, 10662306a36Sopenharmony_ci .rating = 300, 10762306a36Sopenharmony_ci}; 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_cistatic irqreturn_t ep93xx_timer_interrupt(int irq, void *dev_id) 11062306a36Sopenharmony_ci{ 11162306a36Sopenharmony_ci struct clock_event_device *evt = dev_id; 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci /* Writing any value clears the timer interrupt */ 11462306a36Sopenharmony_ci writel(1, EP93XX_TIMER3_CLEAR); 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci evt->event_handler(evt); 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci return IRQ_HANDLED; 11962306a36Sopenharmony_ci} 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_civoid __init ep93xx_timer_init(void) 12262306a36Sopenharmony_ci{ 12362306a36Sopenharmony_ci int irq = IRQ_EP93XX_TIMER3; 12462306a36Sopenharmony_ci unsigned long flags = IRQF_TIMER | IRQF_IRQPOLL; 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci /* Enable and register clocksource and sched_clock on timer 4 */ 12762306a36Sopenharmony_ci writel(EP93XX_TIMER4_VALUE_HIGH_ENABLE, 12862306a36Sopenharmony_ci EP93XX_TIMER4_VALUE_HIGH); 12962306a36Sopenharmony_ci clocksource_mmio_init(NULL, "timer4", 13062306a36Sopenharmony_ci EP93XX_TIMER4_RATE, 200, 40, 13162306a36Sopenharmony_ci ep93xx_clocksource_read); 13262306a36Sopenharmony_ci sched_clock_register(ep93xx_read_sched_clock, 40, 13362306a36Sopenharmony_ci EP93XX_TIMER4_RATE); 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci /* Set up clockevent on timer 3 */ 13662306a36Sopenharmony_ci if (request_irq(irq, ep93xx_timer_interrupt, flags, "ep93xx timer", 13762306a36Sopenharmony_ci &ep93xx_clockevent)) 13862306a36Sopenharmony_ci pr_err("Failed to request irq %d (ep93xx timer)\n", irq); 13962306a36Sopenharmony_ci clockevents_config_and_register(&ep93xx_clockevent, 14062306a36Sopenharmony_ci EP93XX_TIMER123_RATE, 14162306a36Sopenharmony_ci 1, 14262306a36Sopenharmony_ci 0xffffffffU); 14362306a36Sopenharmony_ci} 144