blob: bced17d2d2c10ccc70b0d0974bb9f3786fe1c40d [file] [log] [blame]
Steven J. Hill778eeb12012-12-07 03:51:04 +00001/*
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 Brestickera331ce62014-10-20 12:03:59 -07008#include <linux/clockchips.h>
Steven J. Hill778eeb12012-12-07 03:51:04 +00009#include <linux/init.h>
Andrew Brestickera331ce62014-10-20 12:03:59 -070010#include <linux/interrupt.h>
Andrew Bresticker4060bbe2014-10-20 12:03:53 -070011#include <linux/irqchip/mips-gic.h>
Andrew Brestickera331ce62014-10-20 12:03:59 -070012#include <linux/percpu.h>
13#include <linux/smp.h>
Steven J. Hilldfa762e2013-04-10 16:28:36 -050014#include <linux/time.h>
Steven J. Hill778eeb12012-12-07 03:51:04 +000015
Andrew Brestickera331ce62014-10-20 12:03:59 -070016#include <asm/time.h>
17
Andrew Bresticker5fee56e2014-10-20 12:04:00 -070018static DEFINE_PER_CPU(struct clock_event_device, gic_clockevent_device);
19static int gic_timer_irq_installed;
Andrew Brestickerb0854512014-10-20 12:04:01 -070020static unsigned int gic_frequency;
Andrew Brestickera331ce62014-10-20 12:03:59 -070021
22static int gic_next_event(unsigned long delta, struct clock_event_device *evt)
23{
24 u64 cnt;
25 int res;
26
27 cnt = gic_read_count();
28 cnt += (u64)delta;
29 gic_write_cpu_compare(cnt, cpumask_first(evt->cpumask));
30 res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0;
31 return res;
32}
33
Andrew Bresticker5fee56e2014-10-20 12:04:00 -070034static void gic_set_clock_mode(enum clock_event_mode mode,
Andrew Brestickera331ce62014-10-20 12:03:59 -070035 struct clock_event_device *evt)
36{
37 /* Nothing to do ... */
38}
39
Andrew Bresticker5fee56e2014-10-20 12:04:00 -070040static irqreturn_t gic_compare_interrupt(int irq, void *dev_id)
Andrew Brestickera331ce62014-10-20 12:03:59 -070041{
42 struct clock_event_device *cd;
43 int cpu = smp_processor_id();
44
45 gic_write_compare(gic_read_compare());
46 cd = &per_cpu(gic_clockevent_device, cpu);
47 cd->event_handler(cd);
48 return IRQ_HANDLED;
49}
50
51struct irqaction gic_compare_irqaction = {
52 .handler = gic_compare_interrupt,
53 .flags = IRQF_PERCPU | IRQF_TIMER,
54 .name = "timer",
55};
56
Andrew Bresticker5fee56e2014-10-20 12:04:00 -070057static void gic_event_handler(struct clock_event_device *dev)
Andrew Brestickera331ce62014-10-20 12:03:59 -070058{
59}
60
61int gic_clockevent_init(void)
62{
63 unsigned int cpu = smp_processor_id();
64 struct clock_event_device *cd;
65 unsigned int irq;
66
67 if (!cpu_has_counter || !gic_frequency)
68 return -ENXIO;
69
70 irq = MIPS_GIC_IRQ_BASE + GIC_LOCAL_TO_HWIRQ(GIC_LOCAL_INT_COMPARE);
71
72 cd = &per_cpu(gic_clockevent_device, cpu);
73
74 cd->name = "MIPS GIC";
75 cd->features = CLOCK_EVT_FEAT_ONESHOT |
76 CLOCK_EVT_FEAT_C3STOP;
77
78 clockevent_set_clock(cd, gic_frequency);
79
80 /* Calculate the min / max delta */
81 cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);
82 cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
83
84 cd->rating = 300;
85 cd->irq = irq;
86 cd->cpumask = cpumask_of(cpu);
87 cd->set_next_event = gic_next_event;
88 cd->set_mode = gic_set_clock_mode;
89 cd->event_handler = gic_event_handler;
90
91 clockevents_register_device(cd);
92
93 if (!gic_timer_irq_installed) {
94 setup_percpu_irq(irq, &gic_compare_irqaction);
95 gic_timer_irq_installed = 1;
96 }
97
98 enable_percpu_irq(irq, IRQ_TYPE_NONE);
99
100 return 0;
101}
102
Steven J. Hill778eeb12012-12-07 03:51:04 +0000103static cycle_t gic_hpt_read(struct clocksource *cs)
104{
Steven J. Hilldfa762e2013-04-10 16:28:36 -0500105 return gic_read_count();
Steven J. Hill778eeb12012-12-07 03:51:04 +0000106}
107
108static struct clocksource gic_clocksource = {
109 .name = "GIC",
110 .read = gic_hpt_read,
111 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
112};
113
114void __init gic_clocksource_init(unsigned int frequency)
115{
Andrew Brestickerb0854512014-10-20 12:04:01 -0700116 gic_frequency = frequency;
117
Steven J. Hill778eeb12012-12-07 03:51:04 +0000118 /* Set clocksource mask. */
Andrew Bresticker387904f2014-10-20 12:03:49 -0700119 gic_clocksource.mask = CLOCKSOURCE_MASK(gic_get_count_width());
Steven J. Hill778eeb12012-12-07 03:51:04 +0000120
121 /* Calculate a somewhat reasonable rating value. */
122 gic_clocksource.rating = 200 + frequency / 10000000;
123
124 clocksource_register_hz(&gic_clocksource, frequency);
125}