Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * vineetg: Jan 1011 |
| 9 | * -sched_clock( ) no longer jiffies based. Uses the same clocksource |
| 10 | * as gtod |
| 11 | * |
| 12 | * Rajeshwarr/Vineetg: Mar 2008 |
| 13 | * -Implemented CONFIG_GENERIC_TIME (rather deleted arch specific code) |
| 14 | * for arch independent gettimeofday() |
| 15 | * -Implemented CONFIG_GENERIC_CLOCKEVENTS as base for hrtimers |
| 16 | * |
| 17 | * Vineetg: Mar 2008: Forked off from time.c which now is time-jiff.c |
| 18 | */ |
| 19 | |
| 20 | /* ARC700 has two 32bit independent prog Timers: TIMER0 and TIMER1 |
| 21 | * Each can programmed to go from @count to @limit and optionally |
| 22 | * interrupt when that happens. |
| 23 | * A write to Control Register clears the Interrupt |
| 24 | * |
| 25 | * We've designated TIMER0 for events (clockevents) |
| 26 | * while TIMER1 for free running (clocksource) |
| 27 | * |
| 28 | * Newer ARC700 cores have 64bit clk fetching RTSC insn, preferred over TIMER1 |
Vineet Gupta | 565a9b4 | 2015-03-07 17:06:09 +0530 | [diff] [blame] | 29 | * which however is currently broken |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 30 | */ |
| 31 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 32 | #include <linux/interrupt.h> |
Noam Camus | 69fbd09 | 2016-01-14 12:20:08 +0530 | [diff] [blame] | 33 | #include <linux/clk.h> |
| 34 | #include <linux/clk-provider.h> |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 35 | #include <linux/clocksource.h> |
| 36 | #include <linux/clockchips.h> |
Noam Camus | eec3c58 | 2016-01-01 15:48:49 +0530 | [diff] [blame] | 37 | #include <linux/cpu.h> |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 38 | #include <linux/of.h> |
| 39 | #include <linux/of_irq.h> |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 40 | #include <asm/irq.h> |
| 41 | #include <asm/arcregs.h> |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 42 | |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 43 | #include <asm/mcip.h> |
| 44 | |
Vineet Gupta | da1677b | 2013-05-14 13:28:17 +0530 | [diff] [blame] | 45 | /* Timer related Aux registers */ |
| 46 | #define ARC_REG_TIMER0_LIMIT 0x23 /* timer 0 limit */ |
| 47 | #define ARC_REG_TIMER0_CTRL 0x22 /* timer 0 control */ |
| 48 | #define ARC_REG_TIMER0_CNT 0x21 /* timer 0 count */ |
| 49 | #define ARC_REG_TIMER1_LIMIT 0x102 /* timer 1 limit */ |
| 50 | #define ARC_REG_TIMER1_CTRL 0x101 /* timer 1 control */ |
| 51 | #define ARC_REG_TIMER1_CNT 0x100 /* timer 1 count */ |
| 52 | |
Adam Buchbinder | 7423cc0 | 2016-02-23 15:24:55 -0800 | [diff] [blame] | 53 | #define TIMER_CTRL_IE (1 << 0) /* Interrupt when Count reaches limit */ |
| 54 | #define TIMER_CTRL_NH (1 << 1) /* Count only when CPU NOT halted */ |
Vineet Gupta | da1677b | 2013-05-14 13:28:17 +0530 | [diff] [blame] | 55 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 56 | #define ARC_TIMER_MAX 0xFFFFFFFF |
| 57 | |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 58 | static unsigned long arc_timer_freq; |
| 59 | |
| 60 | static int noinline arc_get_timer_clk(struct device_node *node) |
| 61 | { |
| 62 | struct clk *clk; |
| 63 | int ret; |
| 64 | |
| 65 | clk = of_clk_get(node, 0); |
| 66 | if (IS_ERR(clk)) { |
| 67 | pr_err("timer missing clk"); |
| 68 | return PTR_ERR(clk); |
| 69 | } |
| 70 | |
| 71 | ret = clk_prepare_enable(clk); |
| 72 | if (ret) { |
| 73 | pr_err("Couldn't enable parent clk\n"); |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | arc_timer_freq = clk_get_rate(clk); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 82 | /********** Clock Source Device *********/ |
| 83 | |
Vineet Gupta | d584f0f | 2016-01-22 14:27:50 +0530 | [diff] [blame] | 84 | #ifdef CONFIG_ARC_HAS_GFRC |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 85 | |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 86 | static cycle_t arc_read_gfrc(struct clocksource *cs) |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 87 | { |
| 88 | unsigned long flags; |
Vineet Gupta | 2cd690e | 2016-11-03 11:38:52 -0700 | [diff] [blame^] | 89 | u32 l, h; |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 90 | |
| 91 | local_irq_save(flags); |
| 92 | |
Vineet Gupta | d584f0f | 2016-01-22 14:27:50 +0530 | [diff] [blame] | 93 | __mcip_cmd(CMD_GFRC_READ_LO, 0); |
Vineet Gupta | 2cd690e | 2016-11-03 11:38:52 -0700 | [diff] [blame^] | 94 | l = read_aux_reg(ARC_REG_MCIP_READBACK); |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 95 | |
Vineet Gupta | d584f0f | 2016-01-22 14:27:50 +0530 | [diff] [blame] | 96 | __mcip_cmd(CMD_GFRC_READ_HI, 0); |
Vineet Gupta | 2cd690e | 2016-11-03 11:38:52 -0700 | [diff] [blame^] | 97 | h = read_aux_reg(ARC_REG_MCIP_READBACK); |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 98 | |
| 99 | local_irq_restore(flags); |
| 100 | |
Vineet Gupta | 2cd690e | 2016-11-03 11:38:52 -0700 | [diff] [blame^] | 101 | return (((cycle_t)h) << 32) | l; |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 102 | } |
| 103 | |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 104 | static struct clocksource arc_counter_gfrc = { |
Vineet Gupta | d584f0f | 2016-01-22 14:27:50 +0530 | [diff] [blame] | 105 | .name = "ARConnect GFRC", |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 106 | .rating = 400, |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 107 | .read = arc_read_gfrc, |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 108 | .mask = CLOCKSOURCE_MASK(64), |
| 109 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 110 | }; |
| 111 | |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 112 | static int __init arc_cs_setup_gfrc(struct device_node *node) |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 113 | { |
| 114 | int exists = cpuinfo_arc700[0].extn.gfrc; |
| 115 | int ret; |
| 116 | |
| 117 | if (WARN(!exists, "Global-64-bit-Ctr clocksource not detected")) |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 118 | return -ENXIO; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 119 | |
| 120 | ret = arc_get_timer_clk(node); |
| 121 | if (ret) |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 122 | return ret; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 123 | |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 124 | return clocksource_register_hz(&arc_counter_gfrc, arc_timer_freq); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 125 | } |
Daniel Lezcano | 177cf6e | 2016-06-07 00:27:44 +0200 | [diff] [blame] | 126 | CLOCKSOURCE_OF_DECLARE(arc_gfrc, "snps,archs-timer-gfrc", arc_cs_setup_gfrc); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 127 | |
| 128 | #endif |
Vineet Gupta | 72d7288 | 2014-12-24 18:41:55 +0530 | [diff] [blame] | 129 | |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 130 | #ifdef CONFIG_ARC_HAS_RTC |
| 131 | |
| 132 | #define AUX_RTC_CTRL 0x103 |
| 133 | #define AUX_RTC_LOW 0x104 |
| 134 | #define AUX_RTC_HIGH 0x105 |
| 135 | |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 136 | static cycle_t arc_read_rtc(struct clocksource *cs) |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 137 | { |
| 138 | unsigned long status; |
Vineet Gupta | 2cd690e | 2016-11-03 11:38:52 -0700 | [diff] [blame^] | 139 | u32 l, h; |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 140 | |
Vineet Gupta | 922cc17 | 2016-10-31 14:09:52 -0700 | [diff] [blame] | 141 | /* |
| 142 | * hardware has an internal state machine which tracks readout of |
| 143 | * low/high and updates the CTRL.status if |
| 144 | * - interrupt/exception taken between the two reads |
| 145 | * - high increments after low has been read |
| 146 | */ |
| 147 | do { |
Vineet Gupta | 2cd690e | 2016-11-03 11:38:52 -0700 | [diff] [blame^] | 148 | l = read_aux_reg(AUX_RTC_LOW); |
| 149 | h = read_aux_reg(AUX_RTC_HIGH); |
Vineet Gupta | 922cc17 | 2016-10-31 14:09:52 -0700 | [diff] [blame] | 150 | status = read_aux_reg(AUX_RTC_CTRL); |
| 151 | } while (!(status & _BITUL(31))); |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 152 | |
Vineet Gupta | 2cd690e | 2016-11-03 11:38:52 -0700 | [diff] [blame^] | 153 | return (((cycle_t)h) << 32) | l; |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 154 | } |
| 155 | |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 156 | static struct clocksource arc_counter_rtc = { |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 157 | .name = "ARCv2 RTC", |
| 158 | .rating = 350, |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 159 | .read = arc_read_rtc, |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 160 | .mask = CLOCKSOURCE_MASK(64), |
| 161 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 162 | }; |
| 163 | |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 164 | static int __init arc_cs_setup_rtc(struct device_node *node) |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 165 | { |
| 166 | int exists = cpuinfo_arc700[smp_processor_id()].extn.rtc; |
| 167 | int ret; |
| 168 | |
| 169 | if (WARN(!exists, "Local-64-bit-Ctr clocksource not detected")) |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 170 | return -ENXIO; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 171 | |
| 172 | /* Local to CPU hence not usable in SMP */ |
| 173 | if (WARN(IS_ENABLED(CONFIG_SMP), "Local-64-bit-Ctr not usable in SMP")) |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 174 | return -EINVAL; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 175 | |
| 176 | ret = arc_get_timer_clk(node); |
| 177 | if (ret) |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 178 | return ret; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 179 | |
| 180 | write_aux_reg(AUX_RTC_CTRL, 1); |
| 181 | |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 182 | return clocksource_register_hz(&arc_counter_rtc, arc_timer_freq); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 183 | } |
Daniel Lezcano | 177cf6e | 2016-06-07 00:27:44 +0200 | [diff] [blame] | 184 | CLOCKSOURCE_OF_DECLARE(arc_rtc, "snps,archs-timer-rtc", arc_cs_setup_rtc); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 185 | |
| 186 | #endif |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 187 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 188 | /* |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 189 | * 32bit TIMER1 to keep counting monotonically and wraparound |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 190 | */ |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 191 | |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 192 | static cycle_t arc_read_timer1(struct clocksource *cs) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 193 | { |
| 194 | return (cycle_t) read_aux_reg(ARC_REG_TIMER1_CNT); |
| 195 | } |
| 196 | |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 197 | static struct clocksource arc_counter_timer1 = { |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 198 | .name = "ARC Timer1", |
| 199 | .rating = 300, |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 200 | .read = arc_read_timer1, |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 201 | .mask = CLOCKSOURCE_MASK(32), |
| 202 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 203 | }; |
| 204 | |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 205 | static int __init arc_cs_setup_timer1(struct device_node *node) |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 206 | { |
| 207 | int ret; |
| 208 | |
| 209 | /* Local to CPU hence not usable in SMP */ |
| 210 | if (IS_ENABLED(CONFIG_SMP)) |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 211 | return -EINVAL; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 212 | |
| 213 | ret = arc_get_timer_clk(node); |
| 214 | if (ret) |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 215 | return ret; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 216 | |
| 217 | write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMER_MAX); |
| 218 | write_aux_reg(ARC_REG_TIMER1_CNT, 0); |
| 219 | write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH); |
| 220 | |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 221 | return clocksource_register_hz(&arc_counter_timer1, arc_timer_freq); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 222 | } |
Vineet Gupta | aa93e8e | 2013-11-07 14:57:16 +0530 | [diff] [blame] | 223 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 224 | /********** Clock Event Device *********/ |
| 225 | |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 226 | static int arc_timer_irq; |
Noam Camus | eec3c58 | 2016-01-01 15:48:49 +0530 | [diff] [blame] | 227 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 228 | /* |
Vineet Gupta | c9a98e18 | 2014-06-25 17:14:03 +0530 | [diff] [blame] | 229 | * Arm the timer to interrupt after @cycles |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 230 | * The distinction for oneshot/periodic is done in arc_event_timer_ack() below |
| 231 | */ |
Vineet Gupta | c9a98e18 | 2014-06-25 17:14:03 +0530 | [diff] [blame] | 232 | static void arc_timer_event_setup(unsigned int cycles) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 233 | { |
Vineet Gupta | c9a98e18 | 2014-06-25 17:14:03 +0530 | [diff] [blame] | 234 | write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 235 | write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */ |
| 236 | |
| 237 | write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH); |
| 238 | } |
| 239 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 240 | |
| 241 | static int arc_clkevent_set_next_event(unsigned long delta, |
| 242 | struct clock_event_device *dev) |
| 243 | { |
| 244 | arc_timer_event_setup(delta); |
| 245 | return 0; |
| 246 | } |
| 247 | |
Viresh Kumar | aeec6cd | 2015-07-16 16:56:14 +0530 | [diff] [blame] | 248 | static int arc_clkevent_set_periodic(struct clock_event_device *dev) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 249 | { |
Viresh Kumar | aeec6cd | 2015-07-16 16:56:14 +0530 | [diff] [blame] | 250 | /* |
| 251 | * At X Hz, 1 sec = 1000ms -> X cycles; |
| 252 | * 10ms -> X / 100 cycles |
| 253 | */ |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 254 | arc_timer_event_setup(arc_timer_freq / HZ); |
Viresh Kumar | aeec6cd | 2015-07-16 16:56:14 +0530 | [diff] [blame] | 255 | return 0; |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = { |
Viresh Kumar | aeec6cd | 2015-07-16 16:56:14 +0530 | [diff] [blame] | 259 | .name = "ARC Timer0", |
| 260 | .features = CLOCK_EVT_FEAT_ONESHOT | |
| 261 | CLOCK_EVT_FEAT_PERIODIC, |
| 262 | .rating = 300, |
Viresh Kumar | aeec6cd | 2015-07-16 16:56:14 +0530 | [diff] [blame] | 263 | .set_next_event = arc_clkevent_set_next_event, |
| 264 | .set_state_periodic = arc_clkevent_set_periodic, |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | static irqreturn_t timer_irq_handler(int irq, void *dev_id) |
| 268 | { |
Vineet Gupta | f8b34c3 | 2014-01-25 00:42:37 +0530 | [diff] [blame] | 269 | /* |
| 270 | * Note that generic IRQ core could have passed @evt for @dev_id if |
| 271 | * irq_set_chip_and_handler() asked for handle_percpu_devid_irq() |
| 272 | */ |
| 273 | struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device); |
Viresh Kumar | aeec6cd | 2015-07-16 16:56:14 +0530 | [diff] [blame] | 274 | int irq_reenable = clockevent_state_periodic(evt); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 275 | |
Vineet Gupta | f8b34c3 | 2014-01-25 00:42:37 +0530 | [diff] [blame] | 276 | /* |
| 277 | * Any write to CTRL reg ACks the interrupt, we rewrite the |
| 278 | * Count when [N]ot [H]alted bit. |
| 279 | * And re-arm it if perioid by [I]nterrupt [E]nable bit |
| 280 | */ |
| 281 | write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH); |
| 282 | |
| 283 | evt->event_handler(evt); |
| 284 | |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 285 | return IRQ_HANDLED; |
| 286 | } |
| 287 | |
Anna-Maria Gleixner | ecd8081 | 2016-07-13 17:17:07 +0000 | [diff] [blame] | 288 | |
| 289 | static int arc_timer_starting_cpu(unsigned int cpu) |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 290 | { |
Vineet Gupta | 2d4899f | 2014-05-08 14:06:38 +0530 | [diff] [blame] | 291 | struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 292 | |
Noam Camus | eec3c58 | 2016-01-01 15:48:49 +0530 | [diff] [blame] | 293 | evt->cpumask = cpumask_of(smp_processor_id()); |
| 294 | |
Anna-Maria Gleixner | ecd8081 | 2016-07-13 17:17:07 +0000 | [diff] [blame] | 295 | clockevents_config_and_register(evt, arc_timer_freq, 0, ARC_TIMER_MAX); |
| 296 | enable_percpu_irq(arc_timer_irq, 0); |
| 297 | return 0; |
Noam Camus | eec3c58 | 2016-01-01 15:48:49 +0530 | [diff] [blame] | 298 | } |
| 299 | |
Anna-Maria Gleixner | ecd8081 | 2016-07-13 17:17:07 +0000 | [diff] [blame] | 300 | static int arc_timer_dying_cpu(unsigned int cpu) |
| 301 | { |
| 302 | disable_percpu_irq(arc_timer_irq); |
| 303 | return 0; |
| 304 | } |
Noam Camus | eec3c58 | 2016-01-01 15:48:49 +0530 | [diff] [blame] | 305 | |
| 306 | /* |
| 307 | * clockevent setup for boot CPU |
| 308 | */ |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 309 | static int __init arc_clockevent_setup(struct device_node *node) |
Noam Camus | eec3c58 | 2016-01-01 15:48:49 +0530 | [diff] [blame] | 310 | { |
| 311 | struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device); |
| 312 | int ret; |
| 313 | |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 314 | arc_timer_irq = irq_of_parse_and_map(node, 0); |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 315 | if (arc_timer_irq <= 0) { |
| 316 | pr_err("clockevent: missing irq"); |
| 317 | return -EINVAL; |
| 318 | } |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 319 | |
| 320 | ret = arc_get_timer_clk(node); |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 321 | if (ret) { |
| 322 | pr_err("clockevent: missing clk"); |
| 323 | return ret; |
| 324 | } |
Vineet Gupta | 77c8d0d | 2016-01-01 17:58:45 +0530 | [diff] [blame] | 325 | |
Noam Camus | eec3c58 | 2016-01-01 15:48:49 +0530 | [diff] [blame] | 326 | /* Needs apriori irq_set_percpu_devid() done in intc map function */ |
| 327 | ret = request_percpu_irq(arc_timer_irq, timer_irq_handler, |
| 328 | "Timer0 (per-cpu-tick)", evt); |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 329 | if (ret) { |
| 330 | pr_err("clockevent: unable to request irq\n"); |
| 331 | return ret; |
| 332 | } |
Vineet Gupta | 5695794 | 2016-01-28 12:56:03 +0530 | [diff] [blame] | 333 | |
Anna-Maria Gleixner | ecd8081 | 2016-07-13 17:17:07 +0000 | [diff] [blame] | 334 | ret = cpuhp_setup_state(CPUHP_AP_ARC_TIMER_STARTING, |
| 335 | "AP_ARC_TIMER_STARTING", |
| 336 | arc_timer_starting_cpu, |
| 337 | arc_timer_dying_cpu); |
| 338 | if (ret) { |
| 339 | pr_err("Failed to setup hotplug state"); |
| 340 | return ret; |
| 341 | } |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 342 | return 0; |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 343 | } |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 344 | |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 345 | static int __init arc_of_timer_init(struct device_node *np) |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 346 | { |
| 347 | static int init_count = 0; |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 348 | int ret; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 349 | |
| 350 | if (!init_count) { |
| 351 | init_count = 1; |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 352 | ret = arc_clockevent_setup(np); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 353 | } else { |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 354 | ret = arc_cs_setup_timer1(np); |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 355 | } |
Daniel Lezcano | 43d7560 | 2016-06-15 14:50:12 +0200 | [diff] [blame] | 356 | |
| 357 | return ret; |
Vineet Gupta | e608b53 | 2016-01-01 18:05:48 +0530 | [diff] [blame] | 358 | } |
Daniel Lezcano | 177cf6e | 2016-06-07 00:27:44 +0200 | [diff] [blame] | 359 | CLOCKSOURCE_OF_DECLARE(arc_clkevt, "snps,arc-timer", arc_of_timer_init); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 360 | |
| 361 | /* |
| 362 | * Called from start_kernel() - boot CPU only |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 363 | */ |
| 364 | void __init time_init(void) |
| 365 | { |
Noam Camus | 69fbd09 | 2016-01-14 12:20:08 +0530 | [diff] [blame] | 366 | of_clk_init(NULL); |
| 367 | clocksource_probe(); |
Vineet Gupta | d8005e6 | 2013-01-18 15:12:18 +0530 | [diff] [blame] | 368 | } |