1 From bc350a91da176ae8573c0755c66f5ee1911495bc Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Tue, 30 Apr 2013 12:35:50 +0200
4 Subject: [PATCH 142/164] clocksource: MIPS: ralink: add support for systick
5 timer found on newer ralink SoC
7 Signed-off-by: John Crispin <blogic@openwrt.org>
9 arch/mips/ralink/Kconfig | 2 +
10 arch/mips/ralink/clk.c | 1 +
11 drivers/clocksource/Kconfig | 6 ++
12 drivers/clocksource/Makefile | 1 +
13 drivers/clocksource/cevt-rt3352.c | 162 +++++++++++++++++++++++++++++++++++++
14 include/linux/clocksource.h | 1 +
15 6 files changed, 173 insertions(+)
16 create mode 100644 drivers/clocksource/cevt-rt3352.c
18 diff --git a/arch/mips/ralink/Kconfig b/arch/mips/ralink/Kconfig
19 index 38540a4..3fe032c 100644
20 --- a/arch/mips/ralink/Kconfig
21 +++ b/arch/mips/ralink/Kconfig
22 @@ -14,6 +14,7 @@ choice
23 select USB_ARCH_HAS_HCD
24 select USB_ARCH_HAS_OHCI
25 select USB_ARCH_HAS_EHCI
26 + select CLKEVT_RT3352
30 @@ -22,6 +23,7 @@ choice
34 + select CLKEVT_RT3352
38 diff --git a/arch/mips/ralink/clk.c b/arch/mips/ralink/clk.c
39 index 8dfa22f..bba0cdf 100644
40 --- a/arch/mips/ralink/clk.c
41 +++ b/arch/mips/ralink/clk.c
42 @@ -69,4 +69,5 @@ void __init plat_time_init(void)
43 pr_info("CPU Clock: %ldMHz\n", clk_get_rate(clk) / 1000000);
44 mips_hpt_frequency = clk_get_rate(clk) / 2;
46 + clocksource_of_init();
48 diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig
49 index e507ab7..1216c16 100644
50 --- a/drivers/clocksource/Kconfig
51 +++ b/drivers/clocksource/Kconfig
52 @@ -7,6 +7,12 @@ config CLKSRC_I8253
58 + depends on MIPS && RALINK
65 diff --git a/drivers/clocksource/Makefile b/drivers/clocksource/Makefile
66 index 4d8283a..db47a4e 100644
67 --- a/drivers/clocksource/Makefile
68 +++ b/drivers/clocksource/Makefile
69 @@ -10,6 +10,7 @@ obj-$(CONFIG_SH_TIMER_TMU) += sh_tmu.o
70 obj-$(CONFIG_EM_TIMER_STI) += em_sti.o
71 obj-$(CONFIG_CLKBLD_I8253) += i8253.o
72 obj-$(CONFIG_CLKSRC_MMIO) += mmio.o
73 +obj-$(CONFIG_CLKEVT_RT3352) += cevt-rt3352.o
74 obj-$(CONFIG_DW_APB_TIMER) += dw_apb_timer.o
75 obj-$(CONFIG_DW_APB_TIMER_OF) += dw_apb_timer_of.o
76 obj-$(CONFIG_CLKSRC_NOMADIK_MTU) += nomadik-mtu.o
77 diff --git a/drivers/clocksource/cevt-rt3352.c b/drivers/clocksource/cevt-rt3352.c
79 index 0000000..bd50edd
81 +++ b/drivers/clocksource/cevt-rt3352.c
84 + * This file is subject to the terms and conditions of the GNU General Public
85 + * License. See the file "COPYING" in the main directory of this archive
88 + * Copyright (C) 2013 by John Crispin <blogic@openwrt.org>
91 +#include <linux/clockchips.h>
92 +#include <linux/clocksource.h>
93 +#include <linux/interrupt.h>
94 +#include <linux/reset.h>
95 +#include <linux/init.h>
96 +#include <linux/of.h>
97 +#include <linux/of_irq.h>
98 +#include <linux/of_address.h>
100 +#include <asm/mach-ralink/ralink_regs.h>
101 +#include <asm/time.h>
103 +#define SYSTICK_FREQ (50 * 1000)
105 +#define SYSTICK_CONFIG 0x00
106 +#define SYSTICK_COMPARE 0x04
107 +#define SYSTICK_COUNT 0x08
109 +/* route systick irq to mips irq 7 instead of the r4k-timer */
110 +#define CFG_EXT_STK_EN 0x2
111 +/* enable the counter */
112 +#define CFG_CNT_EN 0x1
114 +struct systick_device {
115 + void __iomem *membase;
116 + struct clock_event_device dev;
121 +static void systick_set_clock_mode(enum clock_event_mode mode,
122 + struct clock_event_device *evt);
124 +static int systick_next_event(unsigned long delta,
125 + struct clock_event_device *evt)
127 + struct systick_device *sdev = container_of(evt, struct systick_device, dev);
130 + count = ioread32(sdev->membase + SYSTICK_COUNT);
131 + count = (count + delta) % SYSTICK_FREQ;
132 + iowrite32(count + delta, sdev->membase + SYSTICK_COMPARE);
137 +static void systick_event_handler(struct clock_event_device *dev)
139 + /* noting to do here */
142 +static irqreturn_t systick_interrupt(int irq, void *dev_id)
144 + struct clock_event_device *dev = (struct clock_event_device *) dev_id;
146 + dev->event_handler(dev);
148 + return IRQ_HANDLED;
151 +static struct systick_device systick = {
153 + /* cevt-r4k uses 300, make sure systick gets used if available */
155 + .features = CLOCK_EVT_FEAT_ONESHOT,
156 + .set_next_event = systick_next_event,
157 + .set_mode = systick_set_clock_mode,
158 + .event_handler = systick_event_handler,
162 +static struct irqaction systick_irqaction = {
163 + .handler = systick_interrupt,
164 + .flags = IRQF_PERCPU | IRQF_TIMER,
165 + .dev_id = &systick.dev,
169 +#ifdef CONFIG_SOC_MT7620
171 +#define CLK_LUT_CFG 0x40
172 +#define SLEEP_EN BIT(31)
174 +static inline void mt7620_freq_scaling(struct systick_device *sdev, int status)
176 + if (sdev->freq_scale == status)
179 + sdev->freq_scale = status;
181 + pr_info("%s: %s autosleep mode\n", systick.dev.name, (status) ? ("enable") : ("disable"));
183 + rt_sysc_w32(rt_sysc_r32(CLK_LUT_CFG) | SLEEP_EN, CLK_LUT_CFG);
185 + rt_sysc_w32(rt_sysc_r32(CLK_LUT_CFG) & ~SLEEP_EN, CLK_LUT_CFG);
188 +static inline void mt7620_freq_scaling(struct systick_device *sdev, int status) {}
191 +static void systick_set_clock_mode(enum clock_event_mode mode,
192 + struct clock_event_device *evt)
194 + struct systick_device *sdev = container_of(evt, struct systick_device, dev);
197 + case CLOCK_EVT_MODE_ONESHOT:
198 + if (!sdev->irq_requested)
199 + setup_irq(systick.dev.irq, &systick_irqaction);
200 + mt7620_freq_scaling(sdev, 1);
201 + sdev->irq_requested = 1;
202 + iowrite32(CFG_EXT_STK_EN | CFG_CNT_EN, systick.membase + SYSTICK_CONFIG);
205 + case CLOCK_EVT_MODE_SHUTDOWN:
206 + if (sdev->irq_requested)
207 + free_irq(systick.dev.irq, &systick_irqaction);
208 + mt7620_freq_scaling(sdev, 0);
209 + sdev->irq_requested = 0;
210 + iowrite32(0, systick.membase + SYSTICK_CONFIG);
214 + pr_err("%s: Unhandeled mips clock_mode\n", systick.dev.name);
219 +static void __init ralink_systick_init(struct device_node *np)
221 + systick.membase = of_iomap(np, 0);
222 + if (!systick.membase) {
223 + pr_err("%s: of_iomap failed", np->name);
227 + clocksource_mmio_init(systick.membase + SYSTICK_COUNT, np->name,
228 + SYSTICK_FREQ, 301, 16, clocksource_mmio_readl_up);
230 + systick_irqaction.name = np->name;
231 + systick.dev.name = np->name;
232 + clockevent_set_clock(&systick.dev, SYSTICK_FREQ);
233 + systick.dev.max_delta_ns = clockevent_delta2ns(0x7fff, &systick.dev);
234 + systick.dev.min_delta_ns = clockevent_delta2ns(0x3, &systick.dev);
235 + systick.dev.irq = irq_of_parse_and_map(np, 0);
236 + if (!systick.dev.irq)
237 + panic("%s: request_irq failed", np->name);
239 + clockevents_register_device(&systick.dev);
241 + pr_info("%s: runing - mult: %d, shift: %d\n", np->name, systick.dev.mult, systick.dev.shift);
244 +CLOCKSOURCE_OF_DECLARE(systick, "ralink,cevt-systick", ralink_systick_init);
245 diff --git a/include/linux/clocksource.h b/include/linux/clocksource.h
246 index 27cfda4..08ed5e1 100644
247 --- a/include/linux/clocksource.h
248 +++ b/include/linux/clocksource.h
249 @@ -340,6 +340,7 @@ extern void clocksource_of_init(void);
250 __used __section(__clksrc_of_table) \
251 = { .compatible = compat, .data = fn };
253 +static inline void clocksource_of_init(void) {}
254 #define CLOCKSOURCE_OF_DECLARE(name, compat, fn)