Thomas Gleixner | a636cd6 | 2019-05-19 15:51:34 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 2 | /* |
| 3 | * System timer for CSR SiRFprimaII |
| 4 | * |
| 5 | * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company. |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/clockchips.h> |
| 11 | #include <linux/clocksource.h> |
| 12 | #include <linux/bitops.h> |
| 13 | #include <linux/irq.h> |
| 14 | #include <linux/clk.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/of.h> |
Arnd Bergmann | 67d7134 | 2013-03-19 15:31:08 +0100 | [diff] [blame] | 18 | #include <linux/of_irq.h> |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 19 | #include <linux/of_address.h> |
Stephen Boyd | 38ff87f | 2013-06-01 23:39:40 -0700 | [diff] [blame] | 20 | #include <linux/sched_clock.h> |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 21 | |
Uwe Kleine-König | 980c51a | 2013-11-11 21:06:11 +0100 | [diff] [blame] | 22 | #define PRIMA2_CLOCK_FREQ 1000000 |
| 23 | |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 24 | #define SIRFSOC_TIMER_COUNTER_LO 0x0000 |
| 25 | #define SIRFSOC_TIMER_COUNTER_HI 0x0004 |
| 26 | #define SIRFSOC_TIMER_MATCH_0 0x0008 |
| 27 | #define SIRFSOC_TIMER_MATCH_1 0x000C |
| 28 | #define SIRFSOC_TIMER_MATCH_2 0x0010 |
| 29 | #define SIRFSOC_TIMER_MATCH_3 0x0014 |
| 30 | #define SIRFSOC_TIMER_MATCH_4 0x0018 |
| 31 | #define SIRFSOC_TIMER_MATCH_5 0x001C |
| 32 | #define SIRFSOC_TIMER_STATUS 0x0020 |
| 33 | #define SIRFSOC_TIMER_INT_EN 0x0024 |
| 34 | #define SIRFSOC_TIMER_WATCHDOG_EN 0x0028 |
| 35 | #define SIRFSOC_TIMER_DIV 0x002C |
| 36 | #define SIRFSOC_TIMER_LATCH 0x0030 |
| 37 | #define SIRFSOC_TIMER_LATCHED_LO 0x0034 |
| 38 | #define SIRFSOC_TIMER_LATCHED_HI 0x0038 |
| 39 | |
| 40 | #define SIRFSOC_TIMER_WDT_INDEX 5 |
| 41 | |
| 42 | #define SIRFSOC_TIMER_LATCH_BIT BIT(0) |
| 43 | |
Barry Song | e5598a8 | 2011-09-21 20:56:33 +0800 | [diff] [blame] | 44 | #define SIRFSOC_TIMER_REG_CNT 11 |
| 45 | |
| 46 | static const u32 sirfsoc_timer_reg_list[SIRFSOC_TIMER_REG_CNT] = { |
| 47 | SIRFSOC_TIMER_MATCH_0, SIRFSOC_TIMER_MATCH_1, SIRFSOC_TIMER_MATCH_2, |
| 48 | SIRFSOC_TIMER_MATCH_3, SIRFSOC_TIMER_MATCH_4, SIRFSOC_TIMER_MATCH_5, |
| 49 | SIRFSOC_TIMER_INT_EN, SIRFSOC_TIMER_WATCHDOG_EN, SIRFSOC_TIMER_DIV, |
| 50 | SIRFSOC_TIMER_LATCHED_LO, SIRFSOC_TIMER_LATCHED_HI, |
| 51 | }; |
| 52 | |
| 53 | static u32 sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT]; |
| 54 | |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 55 | static void __iomem *sirfsoc_timer_base; |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 56 | |
| 57 | /* timer0 interrupt handler */ |
| 58 | static irqreturn_t sirfsoc_timer_interrupt(int irq, void *dev_id) |
| 59 | { |
| 60 | struct clock_event_device *ce = dev_id; |
| 61 | |
Bin Shi | 4c1ad70 | 2014-05-06 22:42:29 +0800 | [diff] [blame] | 62 | WARN_ON(!(readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_STATUS) & |
| 63 | BIT(0))); |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 64 | |
| 65 | /* clear timer0 interrupt */ |
| 66 | writel_relaxed(BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_STATUS); |
| 67 | |
| 68 | ce->event_handler(ce); |
| 69 | |
| 70 | return IRQ_HANDLED; |
| 71 | } |
| 72 | |
| 73 | /* read 64-bit timer counter */ |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 74 | static u64 notrace sirfsoc_timer_read(struct clocksource *cs) |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 75 | { |
| 76 | u64 cycles; |
| 77 | |
| 78 | /* latch the 64-bit timer counter */ |
Bin Shi | 4c1ad70 | 2014-05-06 22:42:29 +0800 | [diff] [blame] | 79 | writel_relaxed(SIRFSOC_TIMER_LATCH_BIT, |
| 80 | sirfsoc_timer_base + SIRFSOC_TIMER_LATCH); |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 81 | cycles = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_HI); |
Bin Shi | 4c1ad70 | 2014-05-06 22:42:29 +0800 | [diff] [blame] | 82 | cycles = (cycles << 32) | |
| 83 | readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO); |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 84 | |
| 85 | return cycles; |
| 86 | } |
| 87 | |
| 88 | static int sirfsoc_timer_set_next_event(unsigned long delta, |
| 89 | struct clock_event_device *ce) |
| 90 | { |
| 91 | unsigned long now, next; |
| 92 | |
Bin Shi | 4c1ad70 | 2014-05-06 22:42:29 +0800 | [diff] [blame] | 93 | writel_relaxed(SIRFSOC_TIMER_LATCH_BIT, |
| 94 | sirfsoc_timer_base + SIRFSOC_TIMER_LATCH); |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 95 | now = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO); |
| 96 | next = now + delta; |
| 97 | writel_relaxed(next, sirfsoc_timer_base + SIRFSOC_TIMER_MATCH_0); |
Bin Shi | 4c1ad70 | 2014-05-06 22:42:29 +0800 | [diff] [blame] | 98 | writel_relaxed(SIRFSOC_TIMER_LATCH_BIT, |
| 99 | sirfsoc_timer_base + SIRFSOC_TIMER_LATCH); |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 100 | now = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_LATCHED_LO); |
| 101 | |
| 102 | return next - now > delta ? -ETIME : 0; |
| 103 | } |
| 104 | |
Viresh Kumar | 53cba06 | 2015-06-18 16:24:49 +0530 | [diff] [blame] | 105 | static int sirfsoc_timer_shutdown(struct clock_event_device *evt) |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 106 | { |
| 107 | u32 val = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN); |
Viresh Kumar | 53cba06 | 2015-06-18 16:24:49 +0530 | [diff] [blame] | 108 | |
| 109 | writel_relaxed(val & ~BIT(0), |
| 110 | sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int sirfsoc_timer_set_oneshot(struct clock_event_device *evt) |
| 115 | { |
| 116 | u32 val = readl_relaxed(sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN); |
| 117 | |
| 118 | writel_relaxed(val | BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_INT_EN); |
| 119 | return 0; |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 120 | } |
| 121 | |
Barry Song | e5598a8 | 2011-09-21 20:56:33 +0800 | [diff] [blame] | 122 | static void sirfsoc_clocksource_suspend(struct clocksource *cs) |
| 123 | { |
| 124 | int i; |
| 125 | |
Bin Shi | 4c1ad70 | 2014-05-06 22:42:29 +0800 | [diff] [blame] | 126 | writel_relaxed(SIRFSOC_TIMER_LATCH_BIT, |
| 127 | sirfsoc_timer_base + SIRFSOC_TIMER_LATCH); |
Barry Song | e5598a8 | 2011-09-21 20:56:33 +0800 | [diff] [blame] | 128 | |
| 129 | for (i = 0; i < SIRFSOC_TIMER_REG_CNT; i++) |
Bin Shi | 4c1ad70 | 2014-05-06 22:42:29 +0800 | [diff] [blame] | 130 | sirfsoc_timer_reg_val[i] = |
| 131 | readl_relaxed(sirfsoc_timer_base + |
| 132 | sirfsoc_timer_reg_list[i]); |
Barry Song | e5598a8 | 2011-09-21 20:56:33 +0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void sirfsoc_clocksource_resume(struct clocksource *cs) |
| 136 | { |
| 137 | int i; |
| 138 | |
Barry Song | debeaf6 | 2012-07-30 13:29:30 +0800 | [diff] [blame] | 139 | for (i = 0; i < SIRFSOC_TIMER_REG_CNT - 2; i++) |
Bin Shi | 4c1ad70 | 2014-05-06 22:42:29 +0800 | [diff] [blame] | 140 | writel_relaxed(sirfsoc_timer_reg_val[i], |
| 141 | sirfsoc_timer_base + sirfsoc_timer_reg_list[i]); |
Barry Song | e5598a8 | 2011-09-21 20:56:33 +0800 | [diff] [blame] | 142 | |
Bin Shi | 4c1ad70 | 2014-05-06 22:42:29 +0800 | [diff] [blame] | 143 | writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 2], |
| 144 | sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_LO); |
| 145 | writel_relaxed(sirfsoc_timer_reg_val[SIRFSOC_TIMER_REG_CNT - 1], |
| 146 | sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_HI); |
Barry Song | e5598a8 | 2011-09-21 20:56:33 +0800 | [diff] [blame] | 147 | } |
| 148 | |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 149 | static struct clock_event_device sirfsoc_clockevent = { |
| 150 | .name = "sirfsoc_clockevent", |
| 151 | .rating = 200, |
| 152 | .features = CLOCK_EVT_FEAT_ONESHOT, |
Viresh Kumar | 53cba06 | 2015-06-18 16:24:49 +0530 | [diff] [blame] | 153 | .set_state_shutdown = sirfsoc_timer_shutdown, |
| 154 | .set_state_oneshot = sirfsoc_timer_set_oneshot, |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 155 | .set_next_event = sirfsoc_timer_set_next_event, |
| 156 | }; |
| 157 | |
| 158 | static struct clocksource sirfsoc_clocksource = { |
| 159 | .name = "sirfsoc_clocksource", |
| 160 | .rating = 200, |
| 161 | .mask = CLOCKSOURCE_MASK(64), |
| 162 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 163 | .read = sirfsoc_timer_read, |
Barry Song | e5598a8 | 2011-09-21 20:56:33 +0800 | [diff] [blame] | 164 | .suspend = sirfsoc_clocksource_suspend, |
| 165 | .resume = sirfsoc_clocksource_resume, |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 166 | }; |
| 167 | |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 168 | /* Overwrite weak default sched_clock with more precise one */ |
Stephen Boyd | 130e6b25 | 2013-07-18 16:21:28 -0700 | [diff] [blame] | 169 | static u64 notrace sirfsoc_read_sched_clock(void) |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 170 | { |
Stephen Boyd | 130e6b25 | 2013-07-18 16:21:28 -0700 | [diff] [blame] | 171 | return sirfsoc_timer_read(NULL); |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static void __init sirfsoc_clockevent_init(void) |
| 175 | { |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 176 | sirfsoc_clockevent.cpumask = cpumask_of(0); |
Uwe Kleine-König | 980c51a | 2013-11-11 21:06:11 +0100 | [diff] [blame] | 177 | clockevents_config_and_register(&sirfsoc_clockevent, PRIMA2_CLOCK_FREQ, |
Shawn Guo | 838a2ae | 2013-01-12 11:50:05 +0000 | [diff] [blame] | 178 | 2, -2); |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* initialize the kernel jiffy timer source */ |
Daniel Lezcano | de23484 | 2016-06-06 23:02:59 +0200 | [diff] [blame] | 182 | static int __init sirfsoc_prima2_timer_init(struct device_node *np) |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 183 | { |
| 184 | unsigned long rate; |
afzal mohammed | cc2550b | 2020-02-27 16:29:02 +0530 | [diff] [blame] | 185 | unsigned int irq; |
Binghua Duan | 198678b | 2012-08-20 06:42:36 +0000 | [diff] [blame] | 186 | struct clk *clk; |
Daniel Lezcano | de23484 | 2016-06-06 23:02:59 +0200 | [diff] [blame] | 187 | int ret; |
Binghua Duan | 198678b | 2012-08-20 06:42:36 +0000 | [diff] [blame] | 188 | |
Zhiwu Song | c7cff54 | 2014-05-05 19:30:04 +0800 | [diff] [blame] | 189 | clk = of_clk_get(np, 0); |
Daniel Lezcano | de23484 | 2016-06-06 23:02:59 +0200 | [diff] [blame] | 190 | if (IS_ERR(clk)) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 191 | pr_err("Failed to get clock\n"); |
Daniel Lezcano | de23484 | 2016-06-06 23:02:59 +0200 | [diff] [blame] | 192 | return PTR_ERR(clk); |
| 193 | } |
Zhiwu Song | 3894152 | 2014-07-03 20:52:51 +0800 | [diff] [blame] | 194 | |
Daniel Lezcano | de23484 | 2016-06-06 23:02:59 +0200 | [diff] [blame] | 195 | ret = clk_prepare_enable(clk); |
| 196 | if (ret) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 197 | pr_err("Failed to enable clock\n"); |
Daniel Lezcano | de23484 | 2016-06-06 23:02:59 +0200 | [diff] [blame] | 198 | return ret; |
| 199 | } |
Zhiwu Song | 3894152 | 2014-07-03 20:52:51 +0800 | [diff] [blame] | 200 | |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 201 | rate = clk_get_rate(clk); |
| 202 | |
Daniel Lezcano | de23484 | 2016-06-06 23:02:59 +0200 | [diff] [blame] | 203 | if (rate < PRIMA2_CLOCK_FREQ || rate % PRIMA2_CLOCK_FREQ) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 204 | pr_err("Invalid clock rate\n"); |
Daniel Lezcano | de23484 | 2016-06-06 23:02:59 +0200 | [diff] [blame] | 205 | return -EINVAL; |
| 206 | } |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 207 | |
Arnd Bergmann | 275786b | 2013-03-19 15:27:22 +0100 | [diff] [blame] | 208 | sirfsoc_timer_base = of_iomap(np, 0); |
Daniel Lezcano | de23484 | 2016-06-06 23:02:59 +0200 | [diff] [blame] | 209 | if (!sirfsoc_timer_base) { |
| 210 | pr_err("unable to map timer cpu registers\n"); |
| 211 | return -ENXIO; |
| 212 | } |
Arnd Bergmann | 275786b | 2013-03-19 15:27:22 +0100 | [diff] [blame] | 213 | |
afzal mohammed | cc2550b | 2020-02-27 16:29:02 +0530 | [diff] [blame] | 214 | irq = irq_of_parse_and_map(np, 0); |
Marc Zyngier | bc8d849 | 2012-01-16 11:44:12 +0000 | [diff] [blame] | 215 | |
Uwe Kleine-König | 980c51a | 2013-11-11 21:06:11 +0100 | [diff] [blame] | 216 | writel_relaxed(rate / PRIMA2_CLOCK_FREQ / 2 - 1, |
Bin Shi | 4c1ad70 | 2014-05-06 22:42:29 +0800 | [diff] [blame] | 217 | sirfsoc_timer_base + SIRFSOC_TIMER_DIV); |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 218 | writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_LO); |
| 219 | writel_relaxed(0, sirfsoc_timer_base + SIRFSOC_TIMER_COUNTER_HI); |
| 220 | writel_relaxed(BIT(0), sirfsoc_timer_base + SIRFSOC_TIMER_STATUS); |
| 221 | |
Daniel Lezcano | de23484 | 2016-06-06 23:02:59 +0200 | [diff] [blame] | 222 | ret = clocksource_register_hz(&sirfsoc_clocksource, PRIMA2_CLOCK_FREQ); |
| 223 | if (ret) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 224 | pr_err("Failed to register clocksource\n"); |
Daniel Lezcano | de23484 | 2016-06-06 23:02:59 +0200 | [diff] [blame] | 225 | return ret; |
| 226 | } |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 227 | |
Uwe Kleine-König | 980c51a | 2013-11-11 21:06:11 +0100 | [diff] [blame] | 228 | sched_clock_register(sirfsoc_read_sched_clock, 64, PRIMA2_CLOCK_FREQ); |
Marc Zyngier | bc8d849 | 2012-01-16 11:44:12 +0000 | [diff] [blame] | 229 | |
afzal mohammed | cc2550b | 2020-02-27 16:29:02 +0530 | [diff] [blame] | 230 | ret = request_irq(irq, sirfsoc_timer_interrupt, IRQF_TIMER, |
| 231 | "sirfsoc_timer0", &sirfsoc_clockevent); |
Daniel Lezcano | de23484 | 2016-06-06 23:02:59 +0200 | [diff] [blame] | 232 | if (ret) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 233 | pr_err("Failed to setup irq\n"); |
Daniel Lezcano | de23484 | 2016-06-06 23:02:59 +0200 | [diff] [blame] | 234 | return ret; |
| 235 | } |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 236 | |
| 237 | sirfsoc_clockevent_init(); |
Daniel Lezcano | de23484 | 2016-06-06 23:02:59 +0200 | [diff] [blame] | 238 | |
| 239 | return 0; |
Binghua Duan | 02c981c | 2011-07-08 17:40:12 +0800 | [diff] [blame] | 240 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 241 | TIMER_OF_DECLARE(sirfsoc_prima2_timer, |
Bin Shi | 4c1ad70 | 2014-05-06 22:42:29 +0800 | [diff] [blame] | 242 | "sirf,prima2-tick", sirfsoc_prima2_timer_init); |