1fa5635a2SAndrew Bresticker /* 2fa5635a2SAndrew Bresticker * This file is subject to the terms and conditions of the GNU General Public 3fa5635a2SAndrew Bresticker * License. See the file "COPYING" in the main directory of this archive 4fa5635a2SAndrew Bresticker * for more details. 5fa5635a2SAndrew Bresticker * 6fa5635a2SAndrew Bresticker * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved. 7fa5635a2SAndrew Bresticker */ 85b4e8453SAndrew Bresticker #include <linux/clk.h> 9a331ce63SAndrew Bresticker #include <linux/clockchips.h> 10e4752dbbSAndrew Bresticker #include <linux/cpu.h> 11fa5635a2SAndrew Bresticker #include <linux/init.h> 12a331ce63SAndrew Bresticker #include <linux/interrupt.h> 13e4752dbbSAndrew Bresticker #include <linux/notifier.h> 14e12aa828SAndrew Bresticker #include <linux/of_irq.h> 15a331ce63SAndrew Bresticker #include <linux/percpu.h> 16a331ce63SAndrew Bresticker #include <linux/smp.h> 17fa5635a2SAndrew Bresticker #include <linux/time.h> 18*e07127a0SPaul Burton #include <asm/mips-cps.h> 19fa5635a2SAndrew Bresticker 205fee56e0SAndrew Bresticker static DEFINE_PER_CPU(struct clock_event_device, gic_clockevent_device); 21e4752dbbSAndrew Bresticker static int gic_timer_irq; 22b0854514SAndrew Bresticker static unsigned int gic_frequency; 23a331ce63SAndrew Bresticker 24*e07127a0SPaul Burton static u64 notrace gic_read_count(void) 25*e07127a0SPaul Burton { 26*e07127a0SPaul Burton unsigned int hi, hi2, lo; 27*e07127a0SPaul Burton 28*e07127a0SPaul Burton if (mips_cm_is64) 29*e07127a0SPaul Burton return read_gic_counter(); 30*e07127a0SPaul Burton 31*e07127a0SPaul Burton do { 32*e07127a0SPaul Burton hi = read_gic_counter_32h(); 33*e07127a0SPaul Burton lo = read_gic_counter_32l(); 34*e07127a0SPaul Burton hi2 = read_gic_counter_32h(); 35*e07127a0SPaul Burton } while (hi2 != hi); 36*e07127a0SPaul Burton 37*e07127a0SPaul Burton return (((u64) hi) << 32) + lo; 38*e07127a0SPaul Burton } 39*e07127a0SPaul Burton 40a331ce63SAndrew Bresticker static int gic_next_event(unsigned long delta, struct clock_event_device *evt) 41a331ce63SAndrew Bresticker { 42*e07127a0SPaul Burton unsigned long flags; 43a331ce63SAndrew Bresticker u64 cnt; 44a331ce63SAndrew Bresticker int res; 45a331ce63SAndrew Bresticker 46a331ce63SAndrew Bresticker cnt = gic_read_count(); 47a331ce63SAndrew Bresticker cnt += (u64)delta; 48*e07127a0SPaul Burton local_irq_save(flags); 49*e07127a0SPaul Burton write_gic_vl_other(mips_cm_vp_id(cpumask_first(evt->cpumask))); 50*e07127a0SPaul Burton write_gic_vo_compare(cnt); 51*e07127a0SPaul Burton local_irq_restore(flags); 52a331ce63SAndrew Bresticker res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0; 53a331ce63SAndrew Bresticker return res; 54a331ce63SAndrew Bresticker } 55a331ce63SAndrew Bresticker 565fee56e0SAndrew Bresticker static irqreturn_t gic_compare_interrupt(int irq, void *dev_id) 57a331ce63SAndrew Bresticker { 58f7ea3060SAndrew Bresticker struct clock_event_device *cd = dev_id; 59a331ce63SAndrew Bresticker 60*e07127a0SPaul Burton write_gic_vl_compare(read_gic_vl_compare()); 61a331ce63SAndrew Bresticker cd->event_handler(cd); 62a331ce63SAndrew Bresticker return IRQ_HANDLED; 63a331ce63SAndrew Bresticker } 64a331ce63SAndrew Bresticker 65a331ce63SAndrew Bresticker struct irqaction gic_compare_irqaction = { 66a331ce63SAndrew Bresticker .handler = gic_compare_interrupt, 67f7ea3060SAndrew Bresticker .percpu_dev_id = &gic_clockevent_device, 68a331ce63SAndrew Bresticker .flags = IRQF_PERCPU | IRQF_TIMER, 69a331ce63SAndrew Bresticker .name = "timer", 70a331ce63SAndrew Bresticker }; 71a331ce63SAndrew Bresticker 722dab9093SRichard Cochran static void gic_clockevent_cpu_init(unsigned int cpu, 732dab9093SRichard Cochran struct clock_event_device *cd) 74a331ce63SAndrew Bresticker { 75a331ce63SAndrew Bresticker cd->name = "MIPS GIC"; 76a331ce63SAndrew Bresticker cd->features = CLOCK_EVT_FEAT_ONESHOT | 77a331ce63SAndrew Bresticker CLOCK_EVT_FEAT_C3STOP; 78a331ce63SAndrew Bresticker 79a45da565SAndrew Bresticker cd->rating = 350; 80e4752dbbSAndrew Bresticker cd->irq = gic_timer_irq; 81a331ce63SAndrew Bresticker cd->cpumask = cpumask_of(cpu); 82a331ce63SAndrew Bresticker cd->set_next_event = gic_next_event; 83a331ce63SAndrew Bresticker 84b695d8e6SAndrew Bresticker clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff); 85a331ce63SAndrew Bresticker 86e4752dbbSAndrew Bresticker enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE); 87a331ce63SAndrew Bresticker } 88a331ce63SAndrew Bresticker 89e4752dbbSAndrew Bresticker static void gic_clockevent_cpu_exit(struct clock_event_device *cd) 90e4752dbbSAndrew Bresticker { 91e4752dbbSAndrew Bresticker disable_percpu_irq(gic_timer_irq); 92e4752dbbSAndrew Bresticker } 93e4752dbbSAndrew Bresticker 94fc6a6772SEzequiel Garcia static void gic_update_frequency(void *data) 95fc6a6772SEzequiel Garcia { 96fc6a6772SEzequiel Garcia unsigned long rate = (unsigned long)data; 97fc6a6772SEzequiel Garcia 98fc6a6772SEzequiel Garcia clockevents_update_freq(this_cpu_ptr(&gic_clockevent_device), rate); 99fc6a6772SEzequiel Garcia } 100fc6a6772SEzequiel Garcia 1012dab9093SRichard Cochran static int gic_starting_cpu(unsigned int cpu) 102e4752dbbSAndrew Bresticker { 1032dab9093SRichard Cochran gic_clockevent_cpu_init(cpu, this_cpu_ptr(&gic_clockevent_device)); 1042dab9093SRichard Cochran return 0; 105e4752dbbSAndrew Bresticker } 106e4752dbbSAndrew Bresticker 107fc6a6772SEzequiel Garcia static int gic_clk_notifier(struct notifier_block *nb, unsigned long action, 108fc6a6772SEzequiel Garcia void *data) 109fc6a6772SEzequiel Garcia { 110fc6a6772SEzequiel Garcia struct clk_notifier_data *cnd = data; 111fc6a6772SEzequiel Garcia 112fc6a6772SEzequiel Garcia if (action == POST_RATE_CHANGE) 113fc6a6772SEzequiel Garcia on_each_cpu(gic_update_frequency, (void *)cnd->new_rate, 1); 114fc6a6772SEzequiel Garcia 115fc6a6772SEzequiel Garcia return NOTIFY_OK; 116fc6a6772SEzequiel Garcia } 117fc6a6772SEzequiel Garcia 1182dab9093SRichard Cochran static int gic_dying_cpu(unsigned int cpu) 1192dab9093SRichard Cochran { 1202dab9093SRichard Cochran gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device)); 1212dab9093SRichard Cochran return 0; 1222dab9093SRichard Cochran } 123e4752dbbSAndrew Bresticker 124fc6a6772SEzequiel Garcia static struct notifier_block gic_clk_nb = { 125fc6a6772SEzequiel Garcia .notifier_call = gic_clk_notifier, 126fc6a6772SEzequiel Garcia }; 127fc6a6772SEzequiel Garcia 128e4752dbbSAndrew Bresticker static int gic_clockevent_init(void) 129e4752dbbSAndrew Bresticker { 130f95ac855SEzequiel Garcia int ret; 131f95ac855SEzequiel Garcia 1326982530eSPaul Burton if (!gic_frequency) 133e4752dbbSAndrew Bresticker return -ENXIO; 134e4752dbbSAndrew Bresticker 135f95ac855SEzequiel Garcia ret = setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction); 1362fd0c93cSPaul Burton if (ret < 0) { 1372fd0c93cSPaul Burton pr_err("GIC timer IRQ %d setup failed: %d\n", 1382fd0c93cSPaul Burton gic_timer_irq, ret); 139f95ac855SEzequiel Garcia return ret; 1402fd0c93cSPaul Burton } 141e4752dbbSAndrew Bresticker 1422dab9093SRichard Cochran cpuhp_setup_state(CPUHP_AP_MIPS_GIC_TIMER_STARTING, 14373c1b41eSThomas Gleixner "clockevents/mips/gic/timer:starting", 14473c1b41eSThomas Gleixner gic_starting_cpu, gic_dying_cpu); 145a331ce63SAndrew Bresticker return 0; 146a331ce63SAndrew Bresticker } 147a331ce63SAndrew Bresticker 148a5a1d1c2SThomas Gleixner static u64 gic_hpt_read(struct clocksource *cs) 149fa5635a2SAndrew Bresticker { 150fa5635a2SAndrew Bresticker return gic_read_count(); 151fa5635a2SAndrew Bresticker } 152fa5635a2SAndrew Bresticker 153fa5635a2SAndrew Bresticker static struct clocksource gic_clocksource = { 154fa5635a2SAndrew Bresticker .name = "GIC", 155fa5635a2SAndrew Bresticker .read = gic_hpt_read, 156fa5635a2SAndrew Bresticker .flags = CLOCK_SOURCE_IS_CONTINUOUS, 157a7f4df4eSAlex Smith .archdata = { .vdso_clock_mode = VDSO_CLOCK_GIC }, 158fa5635a2SAndrew Bresticker }; 159fa5635a2SAndrew Bresticker 160d8152bf8SDaniel Lezcano static int __init __gic_clocksource_init(void) 161fa5635a2SAndrew Bresticker { 162*e07127a0SPaul Burton unsigned int count_width; 163f95ac855SEzequiel Garcia int ret; 164f95ac855SEzequiel Garcia 165fa5635a2SAndrew Bresticker /* Set clocksource mask. */ 166*e07127a0SPaul Burton count_width = read_gic_config() & GIC_CONFIG_COUNTBITS; 167*e07127a0SPaul Burton count_width >>= __fls(GIC_CONFIG_COUNTBITS); 168*e07127a0SPaul Burton count_width *= 4; 169*e07127a0SPaul Burton count_width += 32; 170*e07127a0SPaul Burton gic_clocksource.mask = CLOCKSOURCE_MASK(count_width); 171fa5635a2SAndrew Bresticker 172fa5635a2SAndrew Bresticker /* Calculate a somewhat reasonable rating value. */ 173e12aa828SAndrew Bresticker gic_clocksource.rating = 200 + gic_frequency / 10000000; 174fa5635a2SAndrew Bresticker 175f95ac855SEzequiel Garcia ret = clocksource_register_hz(&gic_clocksource, gic_frequency); 176f95ac855SEzequiel Garcia if (ret < 0) 177f95ac855SEzequiel Garcia pr_warn("GIC: Unable to register clocksource\n"); 178d8152bf8SDaniel Lezcano 179d8152bf8SDaniel Lezcano return ret; 180fa5635a2SAndrew Bresticker } 181e12aa828SAndrew Bresticker 182be5769e2SPaul Gortmaker static int __init gic_clocksource_of_init(struct device_node *node) 183e12aa828SAndrew Bresticker { 1845b4e8453SAndrew Bresticker struct clk *clk; 185fc6a6772SEzequiel Garcia int ret; 1865b4e8453SAndrew Bresticker 187*e07127a0SPaul Burton if (!mips_gic_present() || !node->parent || 188d8152bf8SDaniel Lezcano !of_device_is_compatible(node->parent, "mti,gic")) { 189ac9ce6d1SRafał Miłecki pr_warn("No DT definition for the mips gic driver\n"); 190d8152bf8SDaniel Lezcano return -ENXIO; 191d8152bf8SDaniel Lezcano } 192e12aa828SAndrew Bresticker 1935b4e8453SAndrew Bresticker clk = of_clk_get(node, 0); 1945b4e8453SAndrew Bresticker if (!IS_ERR(clk)) { 1958c3ecd60SChristophe Jaillet ret = clk_prepare_enable(clk); 1968c3ecd60SChristophe Jaillet if (ret < 0) { 197eb811c73SEzequiel Garcia pr_err("GIC failed to enable clock\n"); 1985b4e8453SAndrew Bresticker clk_put(clk); 1998c3ecd60SChristophe Jaillet return ret; 200eb811c73SEzequiel Garcia } 201eb811c73SEzequiel Garcia 202eb811c73SEzequiel Garcia gic_frequency = clk_get_rate(clk); 2035b4e8453SAndrew Bresticker } else if (of_property_read_u32(node, "clock-frequency", 2045b4e8453SAndrew Bresticker &gic_frequency)) { 205e12aa828SAndrew Bresticker pr_err("GIC frequency not specified.\n"); 206d8152bf8SDaniel Lezcano return -EINVAL;; 207e12aa828SAndrew Bresticker } 208e12aa828SAndrew Bresticker gic_timer_irq = irq_of_parse_and_map(node, 0); 209e12aa828SAndrew Bresticker if (!gic_timer_irq) { 210e12aa828SAndrew Bresticker pr_err("GIC timer IRQ not specified.\n"); 211d8152bf8SDaniel Lezcano return -EINVAL;; 212e12aa828SAndrew Bresticker } 213e12aa828SAndrew Bresticker 214d8152bf8SDaniel Lezcano ret = __gic_clocksource_init(); 215d8152bf8SDaniel Lezcano if (ret) 216d8152bf8SDaniel Lezcano return ret; 217fc6a6772SEzequiel Garcia 218fc6a6772SEzequiel Garcia ret = gic_clockevent_init(); 219fc6a6772SEzequiel Garcia if (!ret && !IS_ERR(clk)) { 220fc6a6772SEzequiel Garcia if (clk_notifier_register(clk, &gic_clk_nb) < 0) 221fc6a6772SEzequiel Garcia pr_warn("GIC: Unable to register clock notifier\n"); 222fc6a6772SEzequiel Garcia } 22367d4e669SEzequiel Garcia 22467d4e669SEzequiel Garcia /* And finally start the counter */ 225*e07127a0SPaul Burton clear_gic_config(GIC_CONFIG_COUNTSTOP); 226d8152bf8SDaniel Lezcano 227d8152bf8SDaniel Lezcano return 0; 228e12aa828SAndrew Bresticker } 22917273395SDaniel Lezcano TIMER_OF_DECLARE(mips_gic_timer, "mti,gic-timer", 230e12aa828SAndrew Bresticker gic_clocksource_of_init); 231