Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 2 | /* |
Vineet Gupta | c4c9a04 | 2016-10-31 13:46:38 -0700 | [diff] [blame] | 3 | * Copyright (C) 2016-17 Synopsys, Inc. (www.synopsys.com) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 4 | * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 5 | */ |
| 6 | |
Vineet Gupta | c4c9a04 | 2016-10-31 13:46:38 -0700 | [diff] [blame] | 7 | /* ARC700 has two 32bit independent prog Timers: TIMER0 and TIMER1, Each can be |
| 8 | * programmed to go from @count to @limit and optionally interrupt. |
| 9 | * We've designated TIMER0 for clockevents and TIMER1 for clocksource |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 10 | * |
Vineet Gupta | c4c9a04 | 2016-10-31 13:46:38 -0700 | [diff] [blame] | 11 | * ARCv2 based HS38 cores have RTC (in-core) and GFRC (inside ARConnect/MCIP) |
| 12 | * which are suitable for UP and SMP based clocksources respectively |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 13 | */ |
| 14 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 15 | #include <linux/interrupt.h> |
Masahiro Yamada | 93665ab | 2019-05-24 14:40:10 +0900 | [diff] [blame] | 16 | #include <linux/bits.h> |
Noam Camus | 69fbd09 | 2016-01-14 12:20:08 +0530 | [diff] [blame] | 17 | #include <linux/clk.h> |
| 18 | #include <linux/clk-provider.h> |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 19 | #include <linux/clocksource.h> |
| 20 | #include <linux/clockchips.h> |
Noam Camus | eec3c58 | 2016-01-01 15:48:49 +0530 | [diff] [blame] | 21 | #include <linux/cpu.h> |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 22 | #include <linux/of.h> |
| 23 | #include <linux/of_irq.h> |
Alexey Brodkin | bf28760 | 2018-11-19 14:29:17 +0300 | [diff] [blame] | 24 | #include <linux/sched_clock.h> |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 25 | |
Vineet Gupta | b26c2e3 | 2016-10-31 13:06:19 -0700 | [diff] [blame] | 26 | #include <soc/arc/timers.h> |
Vineet Gupta | 2d7f5c4 | 2016-10-31 11:27:08 -0700 | [diff] [blame] | 27 | #include <soc/arc/mcip.h> |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 28 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 29 | |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 30 | static unsigned long arc_timer_freq; |
| 31 | |
| 32 | static int noinline arc_get_timer_clk(struct device_node *node) |
| 33 | { |
| 34 | struct clk *clk; |
| 35 | int ret; |
| 36 | |
| 37 | clk = of_clk_get(node, 0); |
| 38 | if (IS_ERR(clk)) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 39 | pr_err("timer missing clk\n"); |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 40 | return PTR_ERR(clk); |
| 41 | } |
| 42 | |
| 43 | ret = clk_prepare_enable(clk); |
| 44 | if (ret) { |
| 45 | pr_err("Couldn't enable parent clk\n"); |
| 46 | return ret; |
| 47 | } |
| 48 | |
| 49 | arc_timer_freq = clk_get_rate(clk); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 54 | /********** Clock Source Device *********/ |
| 55 | |
Vineet Gupta | 0442142 | 2016-10-31 14:26:41 -0700 | [diff] [blame] | 56 | #ifdef CONFIG_ARC_TIMERS_64BIT |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 57 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 58 | static u64 arc_read_gfrc(struct clocksource *cs) |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 59 | { |
| 60 | unsigned long flags; |
Vineet Gupta | 2cd690e | 2016-11-03 11:38:52 -0700 | [diff] [blame] | 61 | u32 l, h; |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 62 | |
Eugeniy Paltsev | 6bd9549 | 2018-04-19 18:53:05 +0300 | [diff] [blame] | 63 | /* |
| 64 | * From a programming model pov, there seems to be just one instance of |
| 65 | * MCIP_CMD/MCIP_READBACK however micro-architecturally there's |
| 66 | * an instance PER ARC CORE (not per cluster), and there are dedicated |
| 67 | * hardware decode logic (per core) inside ARConnect to handle |
| 68 | * simultaneous read/write accesses from cores via those two registers. |
| 69 | * So several concurrent commands to ARConnect are OK if they are |
| 70 | * trying to access two different sub-components (like GFRC, |
| 71 | * inter-core interrupt, etc...). HW also supports simultaneously |
| 72 | * accessing GFRC by multiple cores. |
| 73 | * That's why it is safe to disable hard interrupts on the local CPU |
| 74 | * before access to GFRC instead of taking global MCIP spinlock |
| 75 | * defined in arch/arc/kernel/mcip.c |
| 76 | */ |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 77 | local_irq_save(flags); |
| 78 | |
Vineet Gupta | d584f0f | 2016-01-22 14:27:50 +0530 | [diff] [blame] | 79 | __mcip_cmd(CMD_GFRC_READ_LO, 0); |
Vineet Gupta | 2cd690e | 2016-11-03 11:38:52 -0700 | [diff] [blame] | 80 | l = read_aux_reg(ARC_REG_MCIP_READBACK); |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 81 | |
Vineet Gupta | d584f0f | 2016-01-22 14:27:50 +0530 | [diff] [blame] | 82 | __mcip_cmd(CMD_GFRC_READ_HI, 0); |
Vineet Gupta | 2cd690e | 2016-11-03 11:38:52 -0700 | [diff] [blame] | 83 | h = read_aux_reg(ARC_REG_MCIP_READBACK); |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 84 | |
| 85 | local_irq_restore(flags); |
| 86 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 87 | return (((u64)h) << 32) | l; |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 88 | } |
| 89 | |
Alexey Brodkin | bf28760 | 2018-11-19 14:29:17 +0300 | [diff] [blame] | 90 | static notrace u64 arc_gfrc_clock_read(void) |
| 91 | { |
| 92 | return arc_read_gfrc(NULL); |
| 93 | } |
| 94 | |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 95 | static struct clocksource arc_counter_gfrc = { |
Vineet Gupta | d584f0f | 2016-01-22 14:27:50 +0530 | [diff] [blame] | 96 | .name = "ARConnect GFRC", |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 97 | .rating = 400, |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 98 | .read = arc_read_gfrc, |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 99 | .mask = CLOCKSOURCE_MASK(64), |
| 100 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 101 | }; |
| 102 | |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 103 | static int __init arc_cs_setup_gfrc(struct device_node *node) |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 104 | { |
Vineet Gupta | ec7cb87 | 2016-10-31 13:02:31 -0700 | [diff] [blame] | 105 | struct mcip_bcr mp; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 106 | int ret; |
| 107 | |
Vineet Gupta | ec7cb87 | 2016-10-31 13:02:31 -0700 | [diff] [blame] | 108 | READ_BCR(ARC_REG_MCIP_BCR, mp); |
| 109 | if (!mp.gfrc) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 110 | pr_warn("Global-64-bit-Ctr clocksource not detected\n"); |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 111 | return -ENXIO; |
Vineet Gupta | ec7cb87 | 2016-10-31 13:02:31 -0700 | [diff] [blame] | 112 | } |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 113 | |
| 114 | ret = arc_get_timer_clk(node); |
| 115 | if (ret) |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 116 | return ret; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 117 | |
Alexey Brodkin | bf28760 | 2018-11-19 14:29:17 +0300 | [diff] [blame] | 118 | sched_clock_register(arc_gfrc_clock_read, 64, arc_timer_freq); |
| 119 | |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 120 | return clocksource_register_hz(&arc_counter_gfrc, arc_timer_freq); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 121 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 122 | TIMER_OF_DECLARE(arc_gfrc, "snps,archs-timer-gfrc", arc_cs_setup_gfrc); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 123 | |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 124 | #define AUX_RTC_CTRL 0x103 |
| 125 | #define AUX_RTC_LOW 0x104 |
| 126 | #define AUX_RTC_HIGH 0x105 |
| 127 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 128 | static u64 arc_read_rtc(struct clocksource *cs) |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 129 | { |
| 130 | unsigned long status; |
Vineet Gupta | 2cd690e | 2016-11-03 11:38:52 -0700 | [diff] [blame] | 131 | u32 l, h; |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 132 | |
Vineet Gupta | 922cc17 | 2016-10-31 14:09:52 -0700 | [diff] [blame] | 133 | /* |
| 134 | * hardware has an internal state machine which tracks readout of |
| 135 | * low/high and updates the CTRL.status if |
| 136 | * - interrupt/exception taken between the two reads |
| 137 | * - high increments after low has been read |
| 138 | */ |
| 139 | do { |
Vineet Gupta | 2cd690e | 2016-11-03 11:38:52 -0700 | [diff] [blame] | 140 | l = read_aux_reg(AUX_RTC_LOW); |
| 141 | h = read_aux_reg(AUX_RTC_HIGH); |
Vineet Gupta | 922cc17 | 2016-10-31 14:09:52 -0700 | [diff] [blame] | 142 | status = read_aux_reg(AUX_RTC_CTRL); |
Masahiro Yamada | 93665ab | 2019-05-24 14:40:10 +0900 | [diff] [blame] | 143 | } while (!(status & BIT(31))); |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 144 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 145 | return (((u64)h) << 32) | l; |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 146 | } |
| 147 | |
Alexey Brodkin | bf28760 | 2018-11-19 14:29:17 +0300 | [diff] [blame] | 148 | static notrace u64 arc_rtc_clock_read(void) |
| 149 | { |
| 150 | return arc_read_rtc(NULL); |
| 151 | } |
| 152 | |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 153 | static struct clocksource arc_counter_rtc = { |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 154 | .name = "ARCv2 RTC", |
| 155 | .rating = 350, |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 156 | .read = arc_read_rtc, |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 157 | .mask = CLOCKSOURCE_MASK(64), |
| 158 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 159 | }; |
| 160 | |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 161 | static int __init arc_cs_setup_rtc(struct device_node *node) |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 162 | { |
Vineet Gupta | ec7cb87 | 2016-10-31 13:02:31 -0700 | [diff] [blame] | 163 | struct bcr_timer timer; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 164 | int ret; |
| 165 | |
Vineet Gupta | ec7cb87 | 2016-10-31 13:02:31 -0700 | [diff] [blame] | 166 | READ_BCR(ARC_REG_TIMERS_BCR, timer); |
| 167 | if (!timer.rtc) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 168 | pr_warn("Local-64-bit-Ctr clocksource not detected\n"); |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 169 | return -ENXIO; |
Vineet Gupta | ec7cb87 | 2016-10-31 13:02:31 -0700 | [diff] [blame] | 170 | } |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 171 | |
| 172 | /* Local to CPU hence not usable in SMP */ |
Vineet Gupta | ec7cb87 | 2016-10-31 13:02:31 -0700 | [diff] [blame] | 173 | if (IS_ENABLED(CONFIG_SMP)) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 174 | pr_warn("Local-64-bit-Ctr not usable in SMP\n"); |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 175 | return -EINVAL; |
Vineet Gupta | ec7cb87 | 2016-10-31 13:02:31 -0700 | [diff] [blame] | 176 | } |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 177 | |
| 178 | ret = arc_get_timer_clk(node); |
| 179 | if (ret) |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 180 | return ret; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 181 | |
| 182 | write_aux_reg(AUX_RTC_CTRL, 1); |
| 183 | |
Alexey Brodkin | bf28760 | 2018-11-19 14:29:17 +0300 | [diff] [blame] | 184 | sched_clock_register(arc_rtc_clock_read, 64, arc_timer_freq); |
| 185 | |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 186 | return clocksource_register_hz(&arc_counter_rtc, arc_timer_freq); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 187 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 188 | TIMER_OF_DECLARE(arc_rtc, "snps,archs-timer-rtc", arc_cs_setup_rtc); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 189 | |
| 190 | #endif |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 191 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 192 | /* |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 193 | * 32bit TIMER1 to keep counting monotonically and wraparound |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 194 | */ |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 195 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 196 | static u64 arc_read_timer1(struct clocksource *cs) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 197 | { |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 198 | return (u64) read_aux_reg(ARC_REG_TIMER1_CNT); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 199 | } |
| 200 | |
Alexey Brodkin | bf28760 | 2018-11-19 14:29:17 +0300 | [diff] [blame] | 201 | static notrace u64 arc_timer1_clock_read(void) |
| 202 | { |
| 203 | return arc_read_timer1(NULL); |
| 204 | } |
| 205 | |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 206 | static struct clocksource arc_counter_timer1 = { |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 207 | .name = "ARC Timer1", |
| 208 | .rating = 300, |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 209 | .read = arc_read_timer1, |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 210 | .mask = CLOCKSOURCE_MASK(32), |
| 211 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 212 | }; |
| 213 | |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 214 | static int __init arc_cs_setup_timer1(struct device_node *node) |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 215 | { |
| 216 | int ret; |
| 217 | |
| 218 | /* Local to CPU hence not usable in SMP */ |
| 219 | if (IS_ENABLED(CONFIG_SMP)) |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 220 | return -EINVAL; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 221 | |
| 222 | ret = arc_get_timer_clk(node); |
| 223 | if (ret) |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 224 | return ret; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 225 | |
Vineet Gupta | b26c2e3 | 2016-10-31 13:06:19 -0700 | [diff] [blame] | 226 | write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMERN_MAX); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 227 | write_aux_reg(ARC_REG_TIMER1_CNT, 0); |
Randy Dunlap | 58100c3 | 2021-09-23 19:08:25 -0700 | [diff] [blame] | 228 | write_aux_reg(ARC_REG_TIMER1_CTRL, ARC_TIMER_CTRL_NH); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 229 | |
Alexey Brodkin | bf28760 | 2018-11-19 14:29:17 +0300 | [diff] [blame] | 230 | sched_clock_register(arc_timer1_clock_read, 32, arc_timer_freq); |
| 231 | |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 232 | return clocksource_register_hz(&arc_counter_timer1, arc_timer_freq); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 233 | } |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 234 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 235 | /********** Clock Event Device *********/ |
| 236 | |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 237 | static int arc_timer_irq; |
Noam Camus | eec3c58 | 2016-01-01 15:48:49 +0530 | [diff] [blame] | 238 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 239 | /* |
Vineet Gupta | c9a98e18 | 2014-06-25 17:14:03 +0530 | [diff] [blame] | 240 | * Arm the timer to interrupt after @cycles |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 241 | * The distinction for oneshot/periodic is done in arc_event_timer_ack() below |
| 242 | */ |
Vineet Gupta | c9a98e18 | 2014-06-25 17:14:03 +0530 | [diff] [blame] | 243 | static void arc_timer_event_setup(unsigned int cycles) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 244 | { |
Vineet Gupta | c9a98e18 | 2014-06-25 17:14:03 +0530 | [diff] [blame] | 245 | write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 246 | write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */ |
| 247 | |
Randy Dunlap | 58100c3 | 2021-09-23 19:08:25 -0700 | [diff] [blame] | 248 | write_aux_reg(ARC_REG_TIMER0_CTRL, ARC_TIMER_CTRL_IE | ARC_TIMER_CTRL_NH); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 249 | } |
| 250 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 251 | |
| 252 | static int arc_clkevent_set_next_event(unsigned long delta, |
| 253 | struct clock_event_device *dev) |
| 254 | { |
| 255 | arc_timer_event_setup(delta); |
| 256 | return 0; |
| 257 | } |
| 258 | |
Viresh Kumar | aeec6cd | 2015-07-16 16:56:14 +0530 | [diff] [blame] | 259 | static int arc_clkevent_set_periodic(struct clock_event_device *dev) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 260 | { |
Viresh Kumar | aeec6cd | 2015-07-16 16:56:14 +0530 | [diff] [blame] | 261 | /* |
| 262 | * At X Hz, 1 sec = 1000ms -> X cycles; |
| 263 | * 10ms -> X / 100 cycles |
| 264 | */ |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 265 | arc_timer_event_setup(arc_timer_freq / HZ); |
Viresh Kumar | aeec6cd | 2015-07-16 16:56:14 +0530 | [diff] [blame] | 266 | return 0; |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = { |
Viresh Kumar | aeec6cd | 2015-07-16 16:56:14 +0530 | [diff] [blame] | 270 | .name = "ARC Timer0", |
| 271 | .features = CLOCK_EVT_FEAT_ONESHOT | |
| 272 | CLOCK_EVT_FEAT_PERIODIC, |
| 273 | .rating = 300, |
Viresh Kumar | aeec6cd | 2015-07-16 16:56:14 +0530 | [diff] [blame] | 274 | .set_next_event = arc_clkevent_set_next_event, |
| 275 | .set_state_periodic = arc_clkevent_set_periodic, |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | static irqreturn_t timer_irq_handler(int irq, void *dev_id) |
| 279 | { |
Vineet Gupta | f8b34c3 | 2014-01-25 00:42:37 +0530 | [diff] [blame] | 280 | /* |
| 281 | * Note that generic IRQ core could have passed @evt for @dev_id if |
| 282 | * irq_set_chip_and_handler() asked for handle_percpu_devid_irq() |
| 283 | */ |
| 284 | struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device); |
Viresh Kumar | aeec6cd | 2015-07-16 16:56:14 +0530 | [diff] [blame] | 285 | int irq_reenable = clockevent_state_periodic(evt); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 286 | |
Vineet Gupta | f8b34c3 | 2014-01-25 00:42:37 +0530 | [diff] [blame] | 287 | /* |
Vineet Gupta | a4f5385 | 2018-02-21 11:31:31 -0800 | [diff] [blame] | 288 | * 1. ACK the interrupt |
| 289 | * - For ARC700, any write to CTRL reg ACKs it, so just rewrite |
| 290 | * Count when [N]ot [H]alted bit. |
| 291 | * - For HS3x, it is a bit subtle. On taken count-down interrupt, |
| 292 | * IP bit [3] is set, which needs to be cleared for ACK'ing. |
| 293 | * The write below can only update the other two bits, hence |
| 294 | * explicitly clears IP bit |
| 295 | * 2. Re-arm interrupt if periodic by writing to IE bit [0] |
Vineet Gupta | f8b34c3 | 2014-01-25 00:42:37 +0530 | [diff] [blame] | 296 | */ |
Randy Dunlap | 58100c3 | 2021-09-23 19:08:25 -0700 | [diff] [blame] | 297 | write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | ARC_TIMER_CTRL_NH); |
Vineet Gupta | f8b34c3 | 2014-01-25 00:42:37 +0530 | [diff] [blame] | 298 | |
| 299 | evt->event_handler(evt); |
| 300 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 301 | return IRQ_HANDLED; |
| 302 | } |
| 303 | |
Anna-Maria Gleixner | ecd8081 | 2016-07-13 17:17:07 +0000 | [diff] [blame] | 304 | |
| 305 | static int arc_timer_starting_cpu(unsigned int cpu) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 306 | { |
Vineet Gupta | 2d4899f | 2014-05-08 14:06:38 +0530 | [diff] [blame] | 307 | struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 308 | |
Noam Camus | eec3c58 | 2016-01-01 15:48:49 +0530 | [diff] [blame] | 309 | evt->cpumask = cpumask_of(smp_processor_id()); |
| 310 | |
Vineet Gupta | b26c2e3 | 2016-10-31 13:06:19 -0700 | [diff] [blame] | 311 | clockevents_config_and_register(evt, arc_timer_freq, 0, ARC_TIMERN_MAX); |
Anna-Maria Gleixner | ecd8081 | 2016-07-13 17:17:07 +0000 | [diff] [blame] | 312 | enable_percpu_irq(arc_timer_irq, 0); |
| 313 | return 0; |
Noam Camus | eec3c58 | 2016-01-01 15:48:49 +0530 | [diff] [blame] | 314 | } |
| 315 | |
Anna-Maria Gleixner | ecd8081 | 2016-07-13 17:17:07 +0000 | [diff] [blame] | 316 | static int arc_timer_dying_cpu(unsigned int cpu) |
| 317 | { |
| 318 | disable_percpu_irq(arc_timer_irq); |
| 319 | return 0; |
| 320 | } |
Noam Camus | eec3c58 | 2016-01-01 15:48:49 +0530 | [diff] [blame] | 321 | |
| 322 | /* |
| 323 | * clockevent setup for boot CPU |
| 324 | */ |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 325 | static int __init arc_clockevent_setup(struct device_node *node) |
Noam Camus | eec3c58 | 2016-01-01 15:48:49 +0530 | [diff] [blame] | 326 | { |
| 327 | struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device); |
| 328 | int ret; |
| 329 | |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 330 | arc_timer_irq = irq_of_parse_and_map(node, 0); |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 331 | if (arc_timer_irq <= 0) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 332 | pr_err("clockevent: missing irq\n"); |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 333 | return -EINVAL; |
| 334 | } |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 335 | |
| 336 | ret = arc_get_timer_clk(node); |
Dejin Zheng | 311fb70 | 2020-04-29 23:12:23 +0800 | [diff] [blame] | 337 | if (ret) |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 338 | return ret; |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 339 | |
Noam Camus | eec3c58 | 2016-01-01 15:48:49 +0530 | [diff] [blame] | 340 | /* Needs apriori irq_set_percpu_devid() done in intc map function */ |
| 341 | ret = request_percpu_irq(arc_timer_irq, timer_irq_handler, |
| 342 | "Timer0 (per-cpu-tick)", evt); |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 343 | if (ret) { |
| 344 | pr_err("clockevent: unable to request irq\n"); |
| 345 | return ret; |
| 346 | } |
Vineet Gupta | 5695794 | 2016-01-28 12:56:03 +0530 | [diff] [blame] | 347 | |
Anna-Maria Gleixner | ecd8081 | 2016-07-13 17:17:07 +0000 | [diff] [blame] | 348 | ret = cpuhp_setup_state(CPUHP_AP_ARC_TIMER_STARTING, |
Thomas Gleixner | 73c1b41 | 2016-12-21 20:19:54 +0100 | [diff] [blame] | 349 | "clockevents/arc/timer:starting", |
Anna-Maria Gleixner | ecd8081 | 2016-07-13 17:17:07 +0000 | [diff] [blame] | 350 | arc_timer_starting_cpu, |
| 351 | arc_timer_dying_cpu); |
| 352 | if (ret) { |
Rafał Miłecki | ac9ce6d | 2017-03-09 10:47:10 +0100 | [diff] [blame] | 353 | pr_err("Failed to setup hotplug state\n"); |
Anna-Maria Gleixner | ecd8081 | 2016-07-13 17:17:07 +0000 | [diff] [blame] | 354 | return ret; |
| 355 | } |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 356 | return 0; |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 357 | } |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 358 | |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 359 | static int __init arc_of_timer_init(struct device_node *np) |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 360 | { |
| 361 | static int init_count = 0; |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 362 | int ret; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 363 | |
| 364 | if (!init_count) { |
| 365 | init_count = 1; |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 366 | ret = arc_clockevent_setup(np); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 367 | } else { |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 368 | ret = arc_cs_setup_timer1(np); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 369 | } |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 370 | |
| 371 | return ret; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 372 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 373 | TIMER_OF_DECLARE(arc_clkevt, "snps,arc-timer", arc_of_timer_init); |