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