Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved. |
| 7 | */ |
Matt Redfearn | 3ca5768d | 2018-03-29 10:49:03 +0100 | [diff] [blame] | 8 | |
| 9 | #define pr_fmt(fmt) "mips-gic-timer: " fmt |
| 10 | |
Andrew Bresticker | 5b4e845 | 2015-02-23 18:28:34 -0800 | [diff] [blame] | 11 | #include <linux/clk.h> |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 12 | #include <linux/clockchips.h> |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 13 | #include <linux/cpu.h> |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 14 | #include <linux/init.h> |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 15 | #include <linux/interrupt.h> |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 16 | #include <linux/notifier.h> |
Andrew Bresticker | e12aa82 | 2014-11-12 11:43:39 -0800 | [diff] [blame] | 17 | #include <linux/of_irq.h> |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 18 | #include <linux/percpu.h> |
| 19 | #include <linux/smp.h> |
Steven J. Hill | dfa762e | 2013-04-10 16:28:36 -0500 | [diff] [blame] | 20 | #include <linux/time.h> |
Paul Burton | e07127a | 2017-08-12 21:36:11 -0700 | [diff] [blame] | 21 | #include <asm/mips-cps.h> |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 22 | |
Andrew Bresticker | 5fee56e | 2014-10-20 12:04:00 -0700 | [diff] [blame] | 23 | static DEFINE_PER_CPU(struct clock_event_device, gic_clockevent_device); |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 24 | static int gic_timer_irq; |
Andrew Bresticker | b0854514 | 2014-10-20 12:04:01 -0700 | [diff] [blame] | 25 | static unsigned int gic_frequency; |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 26 | |
Paul Burton | e07127a | 2017-08-12 21:36:11 -0700 | [diff] [blame] | 27 | static u64 notrace gic_read_count(void) |
| 28 | { |
| 29 | unsigned int hi, hi2, lo; |
| 30 | |
| 31 | if (mips_cm_is64) |
| 32 | return read_gic_counter(); |
| 33 | |
| 34 | do { |
| 35 | hi = read_gic_counter_32h(); |
| 36 | lo = read_gic_counter_32l(); |
| 37 | hi2 = read_gic_counter_32h(); |
| 38 | } while (hi2 != hi); |
| 39 | |
| 40 | return (((u64) hi) << 32) + lo; |
| 41 | } |
| 42 | |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 43 | static int gic_next_event(unsigned long delta, struct clock_event_device *evt) |
| 44 | { |
Matt Redfearn | f16ff2b | 2017-10-19 12:55:35 +0100 | [diff] [blame] | 45 | int cpu = cpumask_first(evt->cpumask); |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 46 | u64 cnt; |
| 47 | int res; |
| 48 | |
| 49 | cnt = gic_read_count(); |
| 50 | cnt += (u64)delta; |
Matt Redfearn | f16ff2b | 2017-10-19 12:55:35 +0100 | [diff] [blame] | 51 | if (cpu == raw_smp_processor_id()) { |
| 52 | write_gic_vl_compare(cnt); |
| 53 | } else { |
| 54 | write_gic_vl_other(mips_cm_vp_id(cpu)); |
| 55 | write_gic_vo_compare(cnt); |
| 56 | } |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 57 | res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0; |
| 58 | return res; |
| 59 | } |
| 60 | |
Andrew Bresticker | 5fee56e | 2014-10-20 12:04:00 -0700 | [diff] [blame] | 61 | static irqreturn_t gic_compare_interrupt(int irq, void *dev_id) |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 62 | { |
Andrew Bresticker | f7ea306 | 2014-10-20 12:04:03 -0700 | [diff] [blame] | 63 | struct clock_event_device *cd = dev_id; |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 64 | |
Paul Burton | e07127a | 2017-08-12 21:36:11 -0700 | [diff] [blame] | 65 | write_gic_vl_compare(read_gic_vl_compare()); |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 66 | cd->event_handler(cd); |
| 67 | return IRQ_HANDLED; |
| 68 | } |
| 69 | |
YueHaibing | 9039de4 | 2019-03-22 22:43:59 +0800 | [diff] [blame] | 70 | static struct irqaction gic_compare_irqaction = { |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 71 | .handler = gic_compare_interrupt, |
Andrew Bresticker | f7ea306 | 2014-10-20 12:04:03 -0700 | [diff] [blame] | 72 | .percpu_dev_id = &gic_clockevent_device, |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 73 | .flags = IRQF_PERCPU | IRQF_TIMER, |
| 74 | .name = "timer", |
| 75 | }; |
| 76 | |
Richard Cochran | 2dab909 | 2016-07-13 17:16:44 +0000 | [diff] [blame] | 77 | static void gic_clockevent_cpu_init(unsigned int cpu, |
| 78 | struct clock_event_device *cd) |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 79 | { |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 80 | cd->name = "MIPS GIC"; |
| 81 | cd->features = CLOCK_EVT_FEAT_ONESHOT | |
| 82 | CLOCK_EVT_FEAT_C3STOP; |
| 83 | |
Andrew Bresticker | a45da56 | 2014-10-20 12:04:06 -0700 | [diff] [blame] | 84 | cd->rating = 350; |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 85 | cd->irq = gic_timer_irq; |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 86 | cd->cpumask = cpumask_of(cpu); |
| 87 | cd->set_next_event = gic_next_event; |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 88 | |
Andrew Bresticker | b695d8e | 2014-10-20 12:04:05 -0700 | [diff] [blame] | 89 | clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff); |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 90 | |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 91 | enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE); |
| 92 | } |
| 93 | |
| 94 | static void gic_clockevent_cpu_exit(struct clock_event_device *cd) |
| 95 | { |
| 96 | disable_percpu_irq(gic_timer_irq); |
| 97 | } |
| 98 | |
Ezequiel Garcia | fc6a677 | 2015-07-27 15:00:15 +0100 | [diff] [blame] | 99 | static void gic_update_frequency(void *data) |
| 100 | { |
| 101 | unsigned long rate = (unsigned long)data; |
| 102 | |
| 103 | clockevents_update_freq(this_cpu_ptr(&gic_clockevent_device), rate); |
| 104 | } |
| 105 | |
Richard Cochran | 2dab909 | 2016-07-13 17:16:44 +0000 | [diff] [blame] | 106 | static int gic_starting_cpu(unsigned int cpu) |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 107 | { |
Richard Cochran | 2dab909 | 2016-07-13 17:16:44 +0000 | [diff] [blame] | 108 | gic_clockevent_cpu_init(cpu, this_cpu_ptr(&gic_clockevent_device)); |
| 109 | return 0; |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Ezequiel Garcia | fc6a677 | 2015-07-27 15:00:15 +0100 | [diff] [blame] | 112 | static int gic_clk_notifier(struct notifier_block *nb, unsigned long action, |
| 113 | void *data) |
| 114 | { |
| 115 | struct clk_notifier_data *cnd = data; |
| 116 | |
| 117 | if (action == POST_RATE_CHANGE) |
| 118 | on_each_cpu(gic_update_frequency, (void *)cnd->new_rate, 1); |
| 119 | |
| 120 | return NOTIFY_OK; |
| 121 | } |
| 122 | |
Richard Cochran | 2dab909 | 2016-07-13 17:16:44 +0000 | [diff] [blame] | 123 | static int gic_dying_cpu(unsigned int cpu) |
| 124 | { |
| 125 | gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device)); |
| 126 | return 0; |
| 127 | } |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 128 | |
Ezequiel Garcia | fc6a677 | 2015-07-27 15:00:15 +0100 | [diff] [blame] | 129 | static struct notifier_block gic_clk_nb = { |
| 130 | .notifier_call = gic_clk_notifier, |
| 131 | }; |
| 132 | |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 133 | static int gic_clockevent_init(void) |
| 134 | { |
Ezequiel Garcia | f95ac85 | 2015-07-27 15:00:13 +0100 | [diff] [blame] | 135 | int ret; |
| 136 | |
Paul Burton | 6982530 | 2016-09-13 17:56:44 +0100 | [diff] [blame] | 137 | if (!gic_frequency) |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 138 | return -ENXIO; |
| 139 | |
Ezequiel Garcia | f95ac85 | 2015-07-27 15:00:13 +0100 | [diff] [blame] | 140 | ret = setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction); |
Paul Burton | 2fd0c93 | 2016-09-13 17:56:43 +0100 | [diff] [blame] | 141 | if (ret < 0) { |
Matt Redfearn | 3ca5768d | 2018-03-29 10:49:03 +0100 | [diff] [blame] | 142 | pr_err("IRQ %d setup failed (%d)\n", gic_timer_irq, ret); |
Ezequiel Garcia | f95ac85 | 2015-07-27 15:00:13 +0100 | [diff] [blame] | 143 | return ret; |
Paul Burton | 2fd0c93 | 2016-09-13 17:56:43 +0100 | [diff] [blame] | 144 | } |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 145 | |
Richard Cochran | 2dab909 | 2016-07-13 17:16:44 +0000 | [diff] [blame] | 146 | cpuhp_setup_state(CPUHP_AP_MIPS_GIC_TIMER_STARTING, |
Thomas Gleixner | 73c1b41 | 2016-12-21 20:19:54 +0100 | [diff] [blame] | 147 | "clockevents/mips/gic/timer:starting", |
| 148 | gic_starting_cpu, gic_dying_cpu); |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 152 | static u64 gic_hpt_read(struct clocksource *cs) |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 153 | { |
Steven J. Hill | dfa762e | 2013-04-10 16:28:36 -0500 | [diff] [blame] | 154 | return gic_read_count(); |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static struct clocksource gic_clocksource = { |
Alex Smith | a7f4df4 | 2015-10-21 09:57:44 +0100 | [diff] [blame] | 158 | .name = "GIC", |
| 159 | .read = gic_hpt_read, |
| 160 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 161 | .archdata = { .vdso_clock_mode = VDSO_CLOCK_GIC }, |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
Daniel Lezcano | d8152bf | 2016-06-06 17:57:25 +0200 | [diff] [blame] | 164 | static int __init __gic_clocksource_init(void) |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 165 | { |
Paul Burton | e07127a | 2017-08-12 21:36:11 -0700 | [diff] [blame] | 166 | unsigned int count_width; |
Ezequiel Garcia | f95ac85 | 2015-07-27 15:00:13 +0100 | [diff] [blame] | 167 | int ret; |
| 168 | |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 169 | /* Set clocksource mask. */ |
Paul Burton | e07127a | 2017-08-12 21:36:11 -0700 | [diff] [blame] | 170 | count_width = read_gic_config() & GIC_CONFIG_COUNTBITS; |
Felix Fietkau | 5753405 | 2018-02-28 10:56:10 +0100 | [diff] [blame] | 171 | count_width >>= __ffs(GIC_CONFIG_COUNTBITS); |
Paul Burton | e07127a | 2017-08-12 21:36:11 -0700 | [diff] [blame] | 172 | count_width *= 4; |
| 173 | count_width += 32; |
| 174 | gic_clocksource.mask = CLOCKSOURCE_MASK(count_width); |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 175 | |
| 176 | /* Calculate a somewhat reasonable rating value. */ |
Andrew Bresticker | e12aa82 | 2014-11-12 11:43:39 -0800 | [diff] [blame] | 177 | gic_clocksource.rating = 200 + gic_frequency / 10000000; |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 178 | |
Ezequiel Garcia | f95ac85 | 2015-07-27 15:00:13 +0100 | [diff] [blame] | 179 | ret = clocksource_register_hz(&gic_clocksource, gic_frequency); |
| 180 | if (ret < 0) |
Matt Redfearn | 3ca5768d | 2018-03-29 10:49:03 +0100 | [diff] [blame] | 181 | pr_warn("Unable to register clocksource\n"); |
Daniel Lezcano | d8152bf | 2016-06-06 17:57:25 +0200 | [diff] [blame] | 182 | |
| 183 | return ret; |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 184 | } |
Andrew Bresticker | e12aa82 | 2014-11-12 11:43:39 -0800 | [diff] [blame] | 185 | |
Paul Gortmaker | be5769e | 2016-08-17 12:21:35 +0200 | [diff] [blame] | 186 | static int __init gic_clocksource_of_init(struct device_node *node) |
Andrew Bresticker | e12aa82 | 2014-11-12 11:43:39 -0800 | [diff] [blame] | 187 | { |
Andrew Bresticker | 5b4e845 | 2015-02-23 18:28:34 -0800 | [diff] [blame] | 188 | struct clk *clk; |
Ezequiel Garcia | fc6a677 | 2015-07-27 15:00:15 +0100 | [diff] [blame] | 189 | int ret; |
Andrew Bresticker | 5b4e845 | 2015-02-23 18:28:34 -0800 | [diff] [blame] | 190 | |
Paul Burton | e07127a | 2017-08-12 21:36:11 -0700 | [diff] [blame] | 191 | if (!mips_gic_present() || !node->parent || |
Daniel Lezcano | d8152bf | 2016-06-06 17:57:25 +0200 | [diff] [blame] | 192 | !of_device_is_compatible(node->parent, "mti,gic")) { |
Matt Redfearn | 3ca5768d | 2018-03-29 10:49:03 +0100 | [diff] [blame] | 193 | pr_warn("No DT definition\n"); |
Daniel Lezcano | d8152bf | 2016-06-06 17:57:25 +0200 | [diff] [blame] | 194 | return -ENXIO; |
| 195 | } |
Andrew Bresticker | e12aa82 | 2014-11-12 11:43:39 -0800 | [diff] [blame] | 196 | |
Andrew Bresticker | 5b4e845 | 2015-02-23 18:28:34 -0800 | [diff] [blame] | 197 | clk = of_clk_get(node, 0); |
| 198 | if (!IS_ERR(clk)) { |
Christophe Jaillet | 8c3ecd6 | 2017-06-23 21:55:10 +0200 | [diff] [blame] | 199 | ret = clk_prepare_enable(clk); |
| 200 | if (ret < 0) { |
Matt Redfearn | 3ca5768d | 2018-03-29 10:49:03 +0100 | [diff] [blame] | 201 | pr_err("Failed to enable clock\n"); |
Ezequiel Garcia | eb811c7 | 2015-07-27 15:00:12 +0100 | [diff] [blame] | 202 | clk_put(clk); |
Christophe Jaillet | 8c3ecd6 | 2017-06-23 21:55:10 +0200 | [diff] [blame] | 203 | return ret; |
Ezequiel Garcia | eb811c7 | 2015-07-27 15:00:12 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Andrew Bresticker | 5b4e845 | 2015-02-23 18:28:34 -0800 | [diff] [blame] | 206 | gic_frequency = clk_get_rate(clk); |
Andrew Bresticker | 5b4e845 | 2015-02-23 18:28:34 -0800 | [diff] [blame] | 207 | } else if (of_property_read_u32(node, "clock-frequency", |
| 208 | &gic_frequency)) { |
Matt Redfearn | 3ca5768d | 2018-03-29 10:49:03 +0100 | [diff] [blame] | 209 | pr_err("Frequency not specified\n"); |
Ingo Molnar | ed7158b | 2018-02-22 10:54:55 +0100 | [diff] [blame] | 210 | return -EINVAL; |
Andrew Bresticker | e12aa82 | 2014-11-12 11:43:39 -0800 | [diff] [blame] | 211 | } |
| 212 | gic_timer_irq = irq_of_parse_and_map(node, 0); |
| 213 | if (!gic_timer_irq) { |
Matt Redfearn | 3ca5768d | 2018-03-29 10:49:03 +0100 | [diff] [blame] | 214 | pr_err("IRQ not specified\n"); |
Ingo Molnar | ed7158b | 2018-02-22 10:54:55 +0100 | [diff] [blame] | 215 | return -EINVAL; |
Andrew Bresticker | e12aa82 | 2014-11-12 11:43:39 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Daniel Lezcano | d8152bf | 2016-06-06 17:57:25 +0200 | [diff] [blame] | 218 | ret = __gic_clocksource_init(); |
| 219 | if (ret) |
| 220 | return ret; |
Ezequiel Garcia | fc6a677 | 2015-07-27 15:00:15 +0100 | [diff] [blame] | 221 | |
| 222 | ret = gic_clockevent_init(); |
| 223 | if (!ret && !IS_ERR(clk)) { |
| 224 | if (clk_notifier_register(clk, &gic_clk_nb) < 0) |
Matt Redfearn | 3ca5768d | 2018-03-29 10:49:03 +0100 | [diff] [blame] | 225 | pr_warn("Unable to register clock notifier\n"); |
Ezequiel Garcia | fc6a677 | 2015-07-27 15:00:15 +0100 | [diff] [blame] | 226 | } |
Ezequiel Garcia | 67d4e66 | 2015-07-27 15:00:14 +0100 | [diff] [blame] | 227 | |
| 228 | /* And finally start the counter */ |
Paul Burton | e07127a | 2017-08-12 21:36:11 -0700 | [diff] [blame] | 229 | clear_gic_config(GIC_CONFIG_COUNTSTOP); |
Daniel Lezcano | d8152bf | 2016-06-06 17:57:25 +0200 | [diff] [blame] | 230 | |
| 231 | return 0; |
Andrew Bresticker | e12aa82 | 2014-11-12 11:43:39 -0800 | [diff] [blame] | 232 | } |
Daniel Lezcano | 1727339 | 2017-05-26 16:56:11 +0200 | [diff] [blame] | 233 | TIMER_OF_DECLARE(mips_gic_timer, "mti,gic-timer", |
Andrew Bresticker | e12aa82 | 2014-11-12 11:43:39 -0800 | [diff] [blame] | 234 | gic_clocksource_of_init); |