Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Walleij | 361c81f | 2015-06-15 13:15:26 +0200 | [diff] [blame] | 2 | #include <linux/kernel.h> |
| 3 | #include <linux/init.h> |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 4 | #include <linux/clocksource.h> |
| 5 | #include <linux/clockchips.h> |
| 6 | #include <linux/sched_clock.h> |
Linus Walleij | 361c81f | 2015-06-15 13:15:26 +0200 | [diff] [blame] | 7 | #include <linux/interrupt.h> |
| 8 | #include <linux/irq.h> |
| 9 | #include <linux/io.h> |
| 10 | #include <asm/mach/time.h> |
| 11 | #include "soc.h" |
| 12 | |
| 13 | /************************************************************************* |
| 14 | * Timer handling for EP93xx |
| 15 | ************************************************************************* |
| 16 | * The ep93xx has four internal timers. Timers 1, 2 (both 16 bit) and |
| 17 | * 3 (32 bit) count down at 508 kHz, are self-reloading, and can generate |
| 18 | * an interrupt on underflow. Timer 4 (40 bit) counts down at 983.04 kHz, |
| 19 | * is free-running, and can't generate interrupts. |
| 20 | * |
| 21 | * The 508 kHz timers are ideal for use for the timer interrupt, as the |
Linus Walleij | 8ed3912 | 2015-06-16 09:00:44 +0200 | [diff] [blame] | 22 | * most common values of HZ divide 508 kHz nicely. We pick the 32 bit |
| 23 | * timer (timer 3) to get as long sleep intervals as possible when using |
| 24 | * CONFIG_NO_HZ. |
Linus Walleij | 361c81f | 2015-06-15 13:15:26 +0200 | [diff] [blame] | 25 | * |
| 26 | * The higher clock rate of timer 4 makes it a better choice than the |
Linus Walleij | 8ed3912 | 2015-06-16 09:00:44 +0200 | [diff] [blame] | 27 | * other timers for use as clock source and for sched_clock(), providing |
| 28 | * a stable 40 bit time base. |
| 29 | ************************************************************************* |
Linus Walleij | 361c81f | 2015-06-15 13:15:26 +0200 | [diff] [blame] | 30 | */ |
| 31 | #define EP93XX_TIMER_REG(x) (EP93XX_TIMER_BASE + (x)) |
| 32 | #define EP93XX_TIMER1_LOAD EP93XX_TIMER_REG(0x00) |
| 33 | #define EP93XX_TIMER1_VALUE EP93XX_TIMER_REG(0x04) |
| 34 | #define EP93XX_TIMER1_CONTROL EP93XX_TIMER_REG(0x08) |
| 35 | #define EP93XX_TIMER123_CONTROL_ENABLE (1 << 7) |
| 36 | #define EP93XX_TIMER123_CONTROL_MODE (1 << 6) |
| 37 | #define EP93XX_TIMER123_CONTROL_CLKSEL (1 << 3) |
| 38 | #define EP93XX_TIMER1_CLEAR EP93XX_TIMER_REG(0x0c) |
| 39 | #define EP93XX_TIMER2_LOAD EP93XX_TIMER_REG(0x20) |
| 40 | #define EP93XX_TIMER2_VALUE EP93XX_TIMER_REG(0x24) |
| 41 | #define EP93XX_TIMER2_CONTROL EP93XX_TIMER_REG(0x28) |
| 42 | #define EP93XX_TIMER2_CLEAR EP93XX_TIMER_REG(0x2c) |
| 43 | #define EP93XX_TIMER4_VALUE_LOW EP93XX_TIMER_REG(0x60) |
| 44 | #define EP93XX_TIMER4_VALUE_HIGH EP93XX_TIMER_REG(0x64) |
| 45 | #define EP93XX_TIMER4_VALUE_HIGH_ENABLE (1 << 8) |
| 46 | #define EP93XX_TIMER3_LOAD EP93XX_TIMER_REG(0x80) |
| 47 | #define EP93XX_TIMER3_VALUE EP93XX_TIMER_REG(0x84) |
| 48 | #define EP93XX_TIMER3_CONTROL EP93XX_TIMER_REG(0x88) |
| 49 | #define EP93XX_TIMER3_CLEAR EP93XX_TIMER_REG(0x8c) |
| 50 | |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 51 | #define EP93XX_TIMER123_RATE 508469 |
| 52 | #define EP93XX_TIMER4_RATE 983040 |
Linus Walleij | 361c81f | 2015-06-15 13:15:26 +0200 | [diff] [blame] | 53 | |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 54 | static u64 notrace ep93xx_read_sched_clock(void) |
| 55 | { |
| 56 | u64 ret; |
Linus Walleij | 361c81f | 2015-06-15 13:15:26 +0200 | [diff] [blame] | 57 | |
Linus Walleij | d118d97 | 2015-06-15 14:38:16 +0200 | [diff] [blame] | 58 | ret = readl(EP93XX_TIMER4_VALUE_LOW); |
| 59 | ret |= ((u64) (readl(EP93XX_TIMER4_VALUE_HIGH) & 0xff) << 32); |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 60 | return ret; |
| 61 | } |
| 62 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 63 | u64 ep93xx_clocksource_read(struct clocksource *c) |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 64 | { |
| 65 | u64 ret; |
| 66 | |
Linus Walleij | d118d97 | 2015-06-15 14:38:16 +0200 | [diff] [blame] | 67 | ret = readl(EP93XX_TIMER4_VALUE_LOW); |
| 68 | ret |= ((u64) (readl(EP93XX_TIMER4_VALUE_HIGH) & 0xff) << 32); |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 69 | return (u64) ret; |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static int ep93xx_clkevt_set_next_event(unsigned long next, |
| 73 | struct clock_event_device *evt) |
| 74 | { |
| 75 | /* Default mode: periodic, off, 508 kHz */ |
| 76 | u32 tmode = EP93XX_TIMER123_CONTROL_MODE | |
| 77 | EP93XX_TIMER123_CONTROL_CLKSEL; |
| 78 | |
| 79 | /* Clear timer */ |
Linus Walleij | d5878e6 | 2015-06-15 14:42:25 +0200 | [diff] [blame] | 80 | writel(tmode, EP93XX_TIMER3_CONTROL); |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 81 | |
| 82 | /* Set next event */ |
Linus Walleij | d5878e6 | 2015-06-15 14:42:25 +0200 | [diff] [blame] | 83 | writel(next, EP93XX_TIMER3_LOAD); |
Linus Walleij | d118d97 | 2015-06-15 14:38:16 +0200 | [diff] [blame] | 84 | writel(tmode | EP93XX_TIMER123_CONTROL_ENABLE, |
Linus Walleij | d5878e6 | 2015-06-15 14:42:25 +0200 | [diff] [blame] | 85 | EP93XX_TIMER3_CONTROL); |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | |
Viresh Kumar | a54868b | 2015-08-06 14:41:13 +0530 | [diff] [blame] | 90 | static int ep93xx_clkevt_shutdown(struct clock_event_device *evt) |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 91 | { |
| 92 | /* Disable timer */ |
Linus Walleij | d5878e6 | 2015-06-15 14:42:25 +0200 | [diff] [blame] | 93 | writel(0, EP93XX_TIMER3_CONTROL); |
Viresh Kumar | a54868b | 2015-08-06 14:41:13 +0530 | [diff] [blame] | 94 | |
| 95 | return 0; |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static struct clock_event_device ep93xx_clockevent = { |
Viresh Kumar | a54868b | 2015-08-06 14:41:13 +0530 | [diff] [blame] | 99 | .name = "timer1", |
| 100 | .features = CLOCK_EVT_FEAT_ONESHOT, |
| 101 | .set_state_shutdown = ep93xx_clkevt_shutdown, |
| 102 | .set_state_oneshot = ep93xx_clkevt_shutdown, |
| 103 | .tick_resume = ep93xx_clkevt_shutdown, |
| 104 | .set_next_event = ep93xx_clkevt_set_next_event, |
| 105 | .rating = 300, |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 106 | }; |
Linus Walleij | 361c81f | 2015-06-15 13:15:26 +0200 | [diff] [blame] | 107 | |
| 108 | static irqreturn_t ep93xx_timer_interrupt(int irq, void *dev_id) |
| 109 | { |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 110 | struct clock_event_device *evt = dev_id; |
| 111 | |
Linus Walleij | 361c81f | 2015-06-15 13:15:26 +0200 | [diff] [blame] | 112 | /* Writing any value clears the timer interrupt */ |
Linus Walleij | d5878e6 | 2015-06-15 14:42:25 +0200 | [diff] [blame] | 113 | writel(1, EP93XX_TIMER3_CLEAR); |
Linus Walleij | 361c81f | 2015-06-15 13:15:26 +0200 | [diff] [blame] | 114 | |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 115 | evt->event_handler(evt); |
Linus Walleij | 361c81f | 2015-06-15 13:15:26 +0200 | [diff] [blame] | 116 | |
| 117 | return IRQ_HANDLED; |
| 118 | } |
| 119 | |
Linus Walleij | 361c81f | 2015-06-15 13:15:26 +0200 | [diff] [blame] | 120 | void __init ep93xx_timer_init(void) |
| 121 | { |
afzal mohammed | 2164f34 | 2020-03-27 18:11:43 +0530 | [diff] [blame] | 122 | int irq = IRQ_EP93XX_TIMER3; |
| 123 | unsigned long flags = IRQF_TIMER | IRQF_IRQPOLL; |
| 124 | |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 125 | /* Enable and register clocksource and sched_clock on timer 4 */ |
Linus Walleij | d118d97 | 2015-06-15 14:38:16 +0200 | [diff] [blame] | 126 | writel(EP93XX_TIMER4_VALUE_HIGH_ENABLE, |
| 127 | EP93XX_TIMER4_VALUE_HIGH); |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 128 | clocksource_mmio_init(NULL, "timer4", |
| 129 | EP93XX_TIMER4_RATE, 200, 40, |
| 130 | ep93xx_clocksource_read); |
| 131 | sched_clock_register(ep93xx_read_sched_clock, 40, |
| 132 | EP93XX_TIMER4_RATE); |
Linus Walleij | 361c81f | 2015-06-15 13:15:26 +0200 | [diff] [blame] | 133 | |
Linus Walleij | d5878e6 | 2015-06-15 14:42:25 +0200 | [diff] [blame] | 134 | /* Set up clockevent on timer 3 */ |
afzal mohammed | 2164f34 | 2020-03-27 18:11:43 +0530 | [diff] [blame] | 135 | if (request_irq(irq, ep93xx_timer_interrupt, flags, "ep93xx timer", |
| 136 | &ep93xx_clockevent)) |
| 137 | pr_err("Failed to request irq %d (ep93xx timer)\n", irq); |
Linus Walleij | 000bc17 | 2015-06-15 14:34:03 +0200 | [diff] [blame] | 138 | clockevents_config_and_register(&ep93xx_clockevent, |
| 139 | EP93XX_TIMER123_RATE, |
| 140 | 1, |
Linus Walleij | d5878e6 | 2015-06-15 14:42:25 +0200 | [diff] [blame] | 141 | 0xffffffffU); |
Linus Walleij | 361c81f | 2015-06-15 13:15:26 +0200 | [diff] [blame] | 142 | } |