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 | */ |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 8 | #include <linux/clockchips.h> |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 9 | #include <linux/cpu.h> |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 10 | #include <linux/init.h> |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 11 | #include <linux/interrupt.h> |
Andrew Bresticker | 4060bbe | 2014-10-20 12:03:53 -0700 | [diff] [blame] | 12 | #include <linux/irqchip/mips-gic.h> |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 13 | #include <linux/notifier.h> |
Andrew Bresticker | e12aa82 | 2014-11-12 11:43:39 -0800 | [diff] [blame] | 14 | #include <linux/of_irq.h> |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 15 | #include <linux/percpu.h> |
| 16 | #include <linux/smp.h> |
Steven J. Hill | dfa762e | 2013-04-10 16:28:36 -0500 | [diff] [blame] | 17 | #include <linux/time.h> |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 18 | |
Andrew Bresticker | 5fee56e | 2014-10-20 12:04:00 -0700 | [diff] [blame] | 19 | static DEFINE_PER_CPU(struct clock_event_device, gic_clockevent_device); |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 20 | static int gic_timer_irq; |
Andrew Bresticker | b085451 | 2014-10-20 12:04:01 -0700 | [diff] [blame] | 21 | static unsigned int gic_frequency; |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 22 | |
| 23 | static int gic_next_event(unsigned long delta, struct clock_event_device *evt) |
| 24 | { |
| 25 | u64 cnt; |
| 26 | int res; |
| 27 | |
| 28 | cnt = gic_read_count(); |
| 29 | cnt += (u64)delta; |
| 30 | gic_write_cpu_compare(cnt, cpumask_first(evt->cpumask)); |
| 31 | res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0; |
| 32 | return res; |
| 33 | } |
| 34 | |
Andrew Bresticker | 5fee56e | 2014-10-20 12:04:00 -0700 | [diff] [blame] | 35 | static void gic_set_clock_mode(enum clock_event_mode mode, |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 36 | struct clock_event_device *evt) |
| 37 | { |
| 38 | /* Nothing to do ... */ |
| 39 | } |
| 40 | |
Andrew Bresticker | 5fee56e | 2014-10-20 12:04:00 -0700 | [diff] [blame] | 41 | static irqreturn_t gic_compare_interrupt(int irq, void *dev_id) |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 42 | { |
Andrew Bresticker | f7ea306 | 2014-10-20 12:04:03 -0700 | [diff] [blame] | 43 | struct clock_event_device *cd = dev_id; |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 44 | |
| 45 | gic_write_compare(gic_read_compare()); |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 46 | cd->event_handler(cd); |
| 47 | return IRQ_HANDLED; |
| 48 | } |
| 49 | |
| 50 | struct irqaction gic_compare_irqaction = { |
| 51 | .handler = gic_compare_interrupt, |
Andrew Bresticker | f7ea306 | 2014-10-20 12:04:03 -0700 | [diff] [blame] | 52 | .percpu_dev_id = &gic_clockevent_device, |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 53 | .flags = IRQF_PERCPU | IRQF_TIMER, |
| 54 | .name = "timer", |
| 55 | }; |
| 56 | |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 57 | static void gic_clockevent_cpu_init(struct clock_event_device *cd) |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 58 | { |
| 59 | unsigned int cpu = smp_processor_id(); |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 60 | |
| 61 | cd->name = "MIPS GIC"; |
| 62 | cd->features = CLOCK_EVT_FEAT_ONESHOT | |
| 63 | CLOCK_EVT_FEAT_C3STOP; |
| 64 | |
Andrew Bresticker | a45da56 | 2014-10-20 12:04:06 -0700 | [diff] [blame] | 65 | cd->rating = 350; |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 66 | cd->irq = gic_timer_irq; |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 67 | cd->cpumask = cpumask_of(cpu); |
| 68 | cd->set_next_event = gic_next_event; |
| 69 | cd->set_mode = gic_set_clock_mode; |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 70 | |
Andrew Bresticker | b695d8e | 2014-10-20 12:04:05 -0700 | [diff] [blame] | 71 | clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff); |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 72 | |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 73 | enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE); |
| 74 | } |
| 75 | |
| 76 | static void gic_clockevent_cpu_exit(struct clock_event_device *cd) |
| 77 | { |
| 78 | disable_percpu_irq(gic_timer_irq); |
| 79 | } |
| 80 | |
| 81 | static int gic_cpu_notifier(struct notifier_block *nb, unsigned long action, |
| 82 | void *data) |
| 83 | { |
| 84 | switch (action & ~CPU_TASKS_FROZEN) { |
| 85 | case CPU_STARTING: |
| 86 | gic_clockevent_cpu_init(this_cpu_ptr(&gic_clockevent_device)); |
| 87 | break; |
| 88 | case CPU_DYING: |
| 89 | gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device)); |
| 90 | break; |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 93 | return NOTIFY_OK; |
| 94 | } |
| 95 | |
| 96 | static struct notifier_block gic_cpu_nb = { |
| 97 | .notifier_call = gic_cpu_notifier, |
| 98 | }; |
| 99 | |
| 100 | static int gic_clockevent_init(void) |
| 101 | { |
| 102 | if (!cpu_has_counter || !gic_frequency) |
| 103 | return -ENXIO; |
| 104 | |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 105 | setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction); |
| 106 | |
| 107 | register_cpu_notifier(&gic_cpu_nb); |
| 108 | |
| 109 | gic_clockevent_cpu_init(this_cpu_ptr(&gic_clockevent_device)); |
Andrew Bresticker | a331ce6 | 2014-10-20 12:03:59 -0700 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 114 | static cycle_t gic_hpt_read(struct clocksource *cs) |
| 115 | { |
Steven J. Hill | dfa762e | 2013-04-10 16:28:36 -0500 | [diff] [blame] | 116 | return gic_read_count(); |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static struct clocksource gic_clocksource = { |
| 120 | .name = "GIC", |
| 121 | .read = gic_hpt_read, |
| 122 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 123 | }; |
| 124 | |
Andrew Bresticker | e12aa82 | 2014-11-12 11:43:39 -0800 | [diff] [blame] | 125 | static void __init __gic_clocksource_init(void) |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 126 | { |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 127 | /* Set clocksource mask. */ |
Andrew Bresticker | 387904f | 2014-10-20 12:03:49 -0700 | [diff] [blame] | 128 | gic_clocksource.mask = CLOCKSOURCE_MASK(gic_get_count_width()); |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 129 | |
| 130 | /* Calculate a somewhat reasonable rating value. */ |
Andrew Bresticker | e12aa82 | 2014-11-12 11:43:39 -0800 | [diff] [blame] | 131 | gic_clocksource.rating = 200 + gic_frequency / 10000000; |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 132 | |
Andrew Bresticker | e12aa82 | 2014-11-12 11:43:39 -0800 | [diff] [blame] | 133 | clocksource_register_hz(&gic_clocksource, gic_frequency); |
Andrew Bresticker | e4752db | 2014-10-20 12:04:04 -0700 | [diff] [blame] | 134 | |
| 135 | gic_clockevent_init(); |
Steven J. Hill | 778eeb1 | 2012-12-07 03:51:04 +0000 | [diff] [blame] | 136 | } |
Andrew Bresticker | e12aa82 | 2014-11-12 11:43:39 -0800 | [diff] [blame] | 137 | |
| 138 | void __init gic_clocksource_init(unsigned int frequency) |
| 139 | { |
| 140 | gic_frequency = frequency; |
| 141 | gic_timer_irq = MIPS_GIC_IRQ_BASE + |
| 142 | GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_COMPARE); |
| 143 | |
| 144 | __gic_clocksource_init(); |
| 145 | } |
| 146 | |
| 147 | static void __init gic_clocksource_of_init(struct device_node *node) |
| 148 | { |
| 149 | if (WARN_ON(!gic_present || !node->parent || |
| 150 | !of_device_is_compatible(node->parent, "mti,gic"))) |
| 151 | return; |
| 152 | |
| 153 | if (of_property_read_u32(node, "clock-frequency", &gic_frequency)) { |
| 154 | pr_err("GIC frequency not specified.\n"); |
| 155 | return; |
| 156 | } |
| 157 | gic_timer_irq = irq_of_parse_and_map(node, 0); |
| 158 | if (!gic_timer_irq) { |
| 159 | pr_err("GIC timer IRQ not specified.\n"); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | __gic_clocksource_init(); |
| 164 | } |
| 165 | CLOCKSOURCE_OF_DECLARE(mips_gic_timer, "mti,gic-timer", |
| 166 | gic_clocksource_of_init); |