Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Allwinner A1X SoCs timer handling. |
| 3 | * |
| 4 | * Copyright (C) 2012 Maxime Ripard |
| 5 | * |
| 6 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
| 7 | * |
| 8 | * Based on code from |
| 9 | * Allwinner Technology Co., Ltd. <www.allwinnertech.com> |
| 10 | * Benn Huang <benn@allwinnertech.com> |
| 11 | * |
| 12 | * This file is licensed under the terms of the GNU General Public |
| 13 | * License version 2. This program is licensed "as is" without any |
| 14 | * warranty of any kind, whether express or implied. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/clk.h> |
| 18 | #include <linux/clockchips.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/irq.h> |
| 21 | #include <linux/irqreturn.h> |
Maxime Ripard | 137c6b3 | 2013-07-16 16:45:37 +0200 | [diff] [blame] | 22 | #include <linux/sched_clock.h> |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 23 | #include <linux/of.h> |
| 24 | #include <linux/of_address.h> |
| 25 | #include <linux/of_irq.h> |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 26 | |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 27 | #include "timer-of.h" |
| 28 | |
Maxime Ripard | 0498173 | 2013-03-10 17:03:46 +0100 | [diff] [blame] | 29 | #define TIMER_IRQ_EN_REG 0x00 |
Maxime Ripard | 4077764 | 2013-07-16 16:45:37 +0200 | [diff] [blame] | 30 | #define TIMER_IRQ_EN(val) BIT(val) |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 31 | #define TIMER_IRQ_ST_REG 0x04 |
Maxime Ripard | 0498173 | 2013-03-10 17:03:46 +0100 | [diff] [blame] | 32 | #define TIMER_CTL_REG(val) (0x10 * val + 0x10) |
Maxime Ripard | 4077764 | 2013-07-16 16:45:37 +0200 | [diff] [blame] | 33 | #define TIMER_CTL_ENABLE BIT(0) |
Maxime Ripard | 9eded23 | 2013-07-16 16:45:37 +0200 | [diff] [blame] | 34 | #define TIMER_CTL_RELOAD BIT(1) |
Maxime Ripard | a2c49e7 | 2013-07-16 16:45:38 +0200 | [diff] [blame] | 35 | #define TIMER_CTL_CLK_SRC(val) (((val) & 0x3) << 2) |
| 36 | #define TIMER_CTL_CLK_SRC_OSC24M (1) |
| 37 | #define TIMER_CTL_CLK_PRES(val) (((val) & 0x7) << 4) |
Maxime Ripard | 4077764 | 2013-07-16 16:45:37 +0200 | [diff] [blame] | 38 | #define TIMER_CTL_ONESHOT BIT(7) |
Maxime Ripard | bb008b9 | 2013-07-16 16:45:37 +0200 | [diff] [blame] | 39 | #define TIMER_INTVAL_REG(val) (0x10 * (val) + 0x14) |
| 40 | #define TIMER_CNTVAL_REG(val) (0x10 * (val) + 0x18) |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 41 | |
Maxime Ripard | 12e1480 | 2013-10-14 21:07:47 +0200 | [diff] [blame] | 42 | #define TIMER_SYNC_TICKS 3 |
| 43 | |
Maxime Ripard | 63d88f1 | 2013-07-16 16:45:38 +0200 | [diff] [blame] | 44 | /* |
| 45 | * When we disable a timer, we need to wait at least for 2 cycles of |
| 46 | * the timer source clock. We will use for that the clocksource timer |
| 47 | * that is already setup and runs at the same frequency than the other |
| 48 | * timers, and we never will be disabled. |
| 49 | */ |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 50 | static void sun4i_clkevt_sync(void __iomem *base) |
Maxime Ripard | 63d88f1 | 2013-07-16 16:45:38 +0200 | [diff] [blame] | 51 | { |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 52 | u32 old = readl(base + TIMER_CNTVAL_REG(1)); |
Maxime Ripard | 63d88f1 | 2013-07-16 16:45:38 +0200 | [diff] [blame] | 53 | |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 54 | while ((old - readl(base + TIMER_CNTVAL_REG(1))) < TIMER_SYNC_TICKS) |
Maxime Ripard | 63d88f1 | 2013-07-16 16:45:38 +0200 | [diff] [blame] | 55 | cpu_relax(); |
| 56 | } |
| 57 | |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 58 | static void sun4i_clkevt_time_stop(void __iomem *base, u8 timer) |
Maxime Ripard | 96651a0 | 2013-07-16 16:45:38 +0200 | [diff] [blame] | 59 | { |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 60 | u32 val = readl(base + TIMER_CTL_REG(timer)); |
| 61 | writel(val & ~TIMER_CTL_ENABLE, base + TIMER_CTL_REG(timer)); |
| 62 | sun4i_clkevt_sync(base); |
Maxime Ripard | 96651a0 | 2013-07-16 16:45:38 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 65 | static void sun4i_clkevt_time_setup(void __iomem *base, u8 timer, |
| 66 | unsigned long delay) |
Maxime Ripard | 96651a0 | 2013-07-16 16:45:38 +0200 | [diff] [blame] | 67 | { |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 68 | writel(delay, base + TIMER_INTVAL_REG(timer)); |
Maxime Ripard | 96651a0 | 2013-07-16 16:45:38 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 71 | static void sun4i_clkevt_time_start(void __iomem *base, u8 timer, |
| 72 | bool periodic) |
Maxime Ripard | 96651a0 | 2013-07-16 16:45:38 +0200 | [diff] [blame] | 73 | { |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 74 | u32 val = readl(base + TIMER_CTL_REG(timer)); |
Maxime Ripard | 96651a0 | 2013-07-16 16:45:38 +0200 | [diff] [blame] | 75 | |
| 76 | if (periodic) |
| 77 | val &= ~TIMER_CTL_ONESHOT; |
| 78 | else |
| 79 | val |= TIMER_CTL_ONESHOT; |
| 80 | |
Maxime Ripard | 7e14183 | 2013-07-16 16:45:38 +0200 | [diff] [blame] | 81 | writel(val | TIMER_CTL_ENABLE | TIMER_CTL_RELOAD, |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 82 | base + TIMER_CTL_REG(timer)); |
Maxime Ripard | 96651a0 | 2013-07-16 16:45:38 +0200 | [diff] [blame] | 83 | } |
| 84 | |
Viresh Kumar | 6de6c97 | 2015-06-18 16:24:37 +0530 | [diff] [blame] | 85 | static int sun4i_clkevt_shutdown(struct clock_event_device *evt) |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 86 | { |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 87 | struct timer_of *to = to_timer_of(evt); |
| 88 | |
| 89 | sun4i_clkevt_time_stop(timer_of_base(to), 0); |
| 90 | |
Viresh Kumar | 6de6c97 | 2015-06-18 16:24:37 +0530 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int sun4i_clkevt_set_oneshot(struct clock_event_device *evt) |
| 95 | { |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 96 | struct timer_of *to = to_timer_of(evt); |
| 97 | |
| 98 | sun4i_clkevt_time_stop(timer_of_base(to), 0); |
| 99 | sun4i_clkevt_time_start(timer_of_base(to), 0, false); |
| 100 | |
Viresh Kumar | 6de6c97 | 2015-06-18 16:24:37 +0530 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int sun4i_clkevt_set_periodic(struct clock_event_device *evt) |
| 105 | { |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 106 | struct timer_of *to = to_timer_of(evt); |
| 107 | |
| 108 | sun4i_clkevt_time_stop(timer_of_base(to), 0); |
| 109 | sun4i_clkevt_time_setup(timer_of_base(to), 0, timer_of_period(to)); |
| 110 | sun4i_clkevt_time_start(timer_of_base(to), 0, true); |
| 111 | |
Viresh Kumar | 6de6c97 | 2015-06-18 16:24:37 +0530 | [diff] [blame] | 112 | return 0; |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Maxime Ripard | 119fd63 | 2013-03-24 11:49:25 +0100 | [diff] [blame] | 115 | static int sun4i_clkevt_next_event(unsigned long evt, |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 116 | struct clock_event_device *clkevt) |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 117 | { |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 118 | struct timer_of *to = to_timer_of(clkevt); |
| 119 | |
| 120 | sun4i_clkevt_time_stop(timer_of_base(to), 0); |
| 121 | sun4i_clkevt_time_setup(timer_of_base(to), 0, evt - TIMER_SYNC_TICKS); |
| 122 | sun4i_clkevt_time_start(timer_of_base(to), 0, false); |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 127 | static void sun4i_timer_clear_interrupt(void __iomem *base) |
Chen-Yu Tsai | b53e7d0 | 2016-08-25 14:26:59 +0800 | [diff] [blame] | 128 | { |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 129 | writel(TIMER_IRQ_EN(0), base + TIMER_IRQ_ST_REG); |
Chen-Yu Tsai | b53e7d0 | 2016-08-25 14:26:59 +0800 | [diff] [blame] | 130 | } |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 131 | |
Maxime Ripard | 119fd63 | 2013-03-24 11:49:25 +0100 | [diff] [blame] | 132 | static irqreturn_t sun4i_timer_interrupt(int irq, void *dev_id) |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 133 | { |
| 134 | struct clock_event_device *evt = (struct clock_event_device *)dev_id; |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 135 | struct timer_of *to = to_timer_of(evt); |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 136 | |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 137 | sun4i_timer_clear_interrupt(timer_of_base(to)); |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 138 | evt->event_handler(evt); |
| 139 | |
| 140 | return IRQ_HANDLED; |
| 141 | } |
| 142 | |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 143 | static struct timer_of to = { |
| 144 | .flags = TIMER_OF_IRQ | TIMER_OF_CLOCK | TIMER_OF_BASE, |
| 145 | |
| 146 | .clkevt = { |
| 147 | .name = "sun4i_tick", |
| 148 | .rating = 350, |
| 149 | .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT, |
| 150 | .set_state_shutdown = sun4i_clkevt_shutdown, |
| 151 | .set_state_periodic = sun4i_clkevt_set_periodic, |
| 152 | .set_state_oneshot = sun4i_clkevt_set_oneshot, |
| 153 | .tick_resume = sun4i_clkevt_shutdown, |
| 154 | .set_next_event = sun4i_clkevt_next_event, |
| 155 | .cpumask = cpu_possible_mask, |
| 156 | }, |
| 157 | |
| 158 | .of_irq = { |
| 159 | .handler = sun4i_timer_interrupt, |
| 160 | .flags = IRQF_TIMER | IRQF_IRQPOLL, |
| 161 | }, |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 162 | }; |
| 163 | |
Stephen Boyd | 662e723 | 2013-11-20 00:47:32 +0100 | [diff] [blame] | 164 | static u64 notrace sun4i_timer_sched_read(void) |
Maxime Ripard | 137c6b3 | 2013-07-16 16:45:37 +0200 | [diff] [blame] | 165 | { |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 166 | return ~readl(timer_of_base(&to) + TIMER_CNTVAL_REG(1)); |
Maxime Ripard | 137c6b3 | 2013-07-16 16:45:37 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Daniel Lezcano | ce5dc74 | 2016-06-06 17:59:09 +0200 | [diff] [blame] | 169 | static int __init sun4i_timer_init(struct device_node *node) |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 170 | { |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 171 | int ret; |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 172 | u32 val; |
| 173 | |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 174 | ret = timer_of_init(node, &to); |
| 175 | if (ret) |
Daniel Lezcano | ce5dc74 | 2016-06-06 17:59:09 +0200 | [diff] [blame] | 176 | return ret; |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 177 | |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 178 | writel(~0, timer_of_base(&to) + TIMER_INTVAL_REG(1)); |
Maxime Ripard | 137c6b3 | 2013-07-16 16:45:37 +0200 | [diff] [blame] | 179 | writel(TIMER_CTL_ENABLE | TIMER_CTL_RELOAD | |
| 180 | TIMER_CTL_CLK_SRC(TIMER_CTL_CLK_SRC_OSC24M), |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 181 | timer_of_base(&to) + TIMER_CTL_REG(1)); |
Maxime Ripard | 137c6b3 | 2013-07-16 16:45:37 +0200 | [diff] [blame] | 182 | |
Hans de Goede | 37b8b00 | 2015-03-30 22:17:10 +0200 | [diff] [blame] | 183 | /* |
| 184 | * sched_clock_register does not have priorities, and on sun6i and |
| 185 | * later there is a better sched_clock registered by arm_arch_timer.c |
| 186 | */ |
| 187 | if (of_machine_is_compatible("allwinner,sun4i-a10") || |
| 188 | of_machine_is_compatible("allwinner,sun5i-a13") || |
Mesih Kilinc | 0113ab8 | 2019-02-11 12:21:08 +0300 | [diff] [blame] | 189 | of_machine_is_compatible("allwinner,sun5i-a10s") || |
| 190 | of_machine_is_compatible("allwinner,suniv-f1c100s")) |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 191 | sched_clock_register(sun4i_timer_sched_read, 32, |
| 192 | timer_of_rate(&to)); |
Hans de Goede | 37b8b00 | 2015-03-30 22:17:10 +0200 | [diff] [blame] | 193 | |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 194 | ret = clocksource_mmio_init(timer_of_base(&to) + TIMER_CNTVAL_REG(1), |
| 195 | node->name, timer_of_rate(&to), 350, 32, |
| 196 | clocksource_mmio_readl_down); |
Daniel Lezcano | ce5dc74 | 2016-06-06 17:59:09 +0200 | [diff] [blame] | 197 | if (ret) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 198 | pr_err("Failed to register clocksource\n"); |
Daniel Lezcano | ce5dc74 | 2016-06-06 17:59:09 +0200 | [diff] [blame] | 199 | return ret; |
| 200 | } |
Maxime Ripard | 137c6b3 | 2013-07-16 16:45:37 +0200 | [diff] [blame] | 201 | |
Maxime Ripard | 7e14183 | 2013-07-16 16:45:38 +0200 | [diff] [blame] | 202 | writel(TIMER_CTL_CLK_SRC(TIMER_CTL_CLK_SRC_OSC24M), |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 203 | timer_of_base(&to) + TIMER_CTL_REG(0)); |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 204 | |
Marc Zyngier | 6db50bb | 2013-12-02 09:29:35 +0000 | [diff] [blame] | 205 | /* Make sure timer is stopped before playing with interrupts */ |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 206 | sun4i_clkevt_time_stop(timer_of_base(&to), 0); |
Marc Zyngier | 6db50bb | 2013-12-02 09:29:35 +0000 | [diff] [blame] | 207 | |
Chen-Yu Tsai | b53e7d0 | 2016-08-25 14:26:59 +0800 | [diff] [blame] | 208 | /* clear timer0 interrupt */ |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 209 | sun4i_timer_clear_interrupt(timer_of_base(&to)); |
Chen-Yu Tsai | b53e7d0 | 2016-08-25 14:26:59 +0800 | [diff] [blame] | 210 | |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 211 | clockevents_config_and_register(&to.clkevt, timer_of_rate(&to), |
Maxime Ripard | 6bab4a8 | 2014-11-18 23:59:33 +0100 | [diff] [blame] | 212 | TIMER_SYNC_TICKS, 0xffffffff); |
| 213 | |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 214 | /* Enable timer0 interrupt */ |
Daniel Lezcano | 239751e | 2017-06-06 23:07:51 +0200 | [diff] [blame] | 215 | val = readl(timer_of_base(&to) + TIMER_IRQ_EN_REG); |
| 216 | writel(val | TIMER_IRQ_EN(0), timer_of_base(&to) + TIMER_IRQ_EN_REG); |
Daniel Lezcano | ce5dc74 | 2016-06-06 17:59:09 +0200 | [diff] [blame] | 217 | |
| 218 | return ret; |
Maxime Ripard | b2ac5d7 | 2012-11-12 15:07:50 +0100 | [diff] [blame] | 219 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 220 | TIMER_OF_DECLARE(sun4i, "allwinner,sun4i-a10-timer", |
Maxime Ripard | 119fd63 | 2013-03-24 11:49:25 +0100 | [diff] [blame] | 221 | sun4i_timer_init); |
Maxime Ripard | bca4e08 | 2019-07-22 10:12:21 +0200 | [diff] [blame] | 222 | TIMER_OF_DECLARE(sun8i_a23, "allwinner,sun8i-a23-timer", |
| 223 | sun4i_timer_init); |
| 224 | TIMER_OF_DECLARE(sun8i_v3s, "allwinner,sun8i-v3s-timer", |
| 225 | sun4i_timer_init); |
Mesih Kilinc | 0113ab8 | 2019-02-11 12:21:08 +0300 | [diff] [blame] | 226 | TIMER_OF_DECLARE(suniv, "allwinner,suniv-f1c100s-timer", |
| 227 | sun4i_timer_init); |