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 --- a/arch/mips/ralink/Kconfig
19 +++ b/arch/mips/ralink/Kconfig
20 @@ -14,6 +14,7 @@ choice
21 select USB_ARCH_HAS_HCD
22 select USB_ARCH_HAS_OHCI
23 select USB_ARCH_HAS_EHCI
24 + select CLKEVT_RT3352
28 @@ -22,6 +23,7 @@ choice
32 + select CLKEVT_RT3352
36 --- a/arch/mips/ralink/clk.c
37 +++ b/arch/mips/ralink/clk.c
38 @@ -69,4 +69,5 @@ void __init plat_time_init(void)
39 pr_info("CPU Clock: %ldMHz\n", clk_get_rate(clk) / 1000000);
40 mips_hpt_frequency = clk_get_rate(clk) / 2;
42 + clocksource_of_init();
44 --- a/drivers/clocksource/Kconfig
45 +++ b/drivers/clocksource/Kconfig
46 @@ -7,6 +7,12 @@ config CLKSRC_I8253
52 + depends on MIPS && RALINK
59 --- a/drivers/clocksource/Makefile
60 +++ b/drivers/clocksource/Makefile
61 @@ -10,6 +10,7 @@ obj-$(CONFIG_SH_TIMER_TMU) += sh_tmu.o
62 obj-$(CONFIG_EM_TIMER_STI) += em_sti.o
63 obj-$(CONFIG_CLKBLD_I8253) += i8253.o
64 obj-$(CONFIG_CLKSRC_MMIO) += mmio.o
65 +obj-$(CONFIG_CLKEVT_RT3352) += cevt-rt3352.o
66 obj-$(CONFIG_DW_APB_TIMER) += dw_apb_timer.o
67 obj-$(CONFIG_DW_APB_TIMER_OF) += dw_apb_timer_of.o
68 obj-$(CONFIG_CLKSRC_NOMADIK_MTU) += nomadik-mtu.o
70 +++ b/drivers/clocksource/cevt-rt3352.c
73 + * This file is subject to the terms and conditions of the GNU General Public
74 + * License. See the file "COPYING" in the main directory of this archive
77 + * Copyright (C) 2013 by John Crispin <blogic@openwrt.org>
80 +#include <linux/clockchips.h>
81 +#include <linux/clocksource.h>
82 +#include <linux/interrupt.h>
83 +#include <linux/reset.h>
84 +#include <linux/init.h>
85 +#include <linux/of.h>
86 +#include <linux/of_irq.h>
87 +#include <linux/of_address.h>
89 +#include <asm/mach-ralink/ralink_regs.h>
90 +#include <asm/time.h>
92 +#define SYSTICK_FREQ (50 * 1000)
94 +#define SYSTICK_CONFIG 0x00
95 +#define SYSTICK_COMPARE 0x04
96 +#define SYSTICK_COUNT 0x08
98 +/* route systick irq to mips irq 7 instead of the r4k-timer */
99 +#define CFG_EXT_STK_EN 0x2
100 +/* enable the counter */
101 +#define CFG_CNT_EN 0x1
103 +struct systick_device {
104 + void __iomem *membase;
105 + struct clock_event_device dev;
110 +static void systick_set_clock_mode(enum clock_event_mode mode,
111 + struct clock_event_device *evt);
113 +static int systick_next_event(unsigned long delta,
114 + struct clock_event_device *evt)
116 + struct systick_device *sdev = container_of(evt, struct systick_device, dev);
119 + count = ioread32(sdev->membase + SYSTICK_COUNT);
120 + count = (count + delta) % SYSTICK_FREQ;
121 + iowrite32(count + delta, sdev->membase + SYSTICK_COMPARE);
126 +static void systick_event_handler(struct clock_event_device *dev)
128 + /* noting to do here */
131 +static irqreturn_t systick_interrupt(int irq, void *dev_id)
133 + struct clock_event_device *dev = (struct clock_event_device *) dev_id;
135 + dev->event_handler(dev);
137 + return IRQ_HANDLED;
140 +static struct systick_device systick = {
142 + /* cevt-r4k uses 300, make sure systick gets used if available */
144 + .features = CLOCK_EVT_FEAT_ONESHOT,
145 + .set_next_event = systick_next_event,
146 + .set_mode = systick_set_clock_mode,
147 + .event_handler = systick_event_handler,
151 +static struct irqaction systick_irqaction = {
152 + .handler = systick_interrupt,
153 + .flags = IRQF_PERCPU | IRQF_TIMER,
154 + .dev_id = &systick.dev,
158 +#ifdef CONFIG_SOC_MT7620
160 +#define CLK_LUT_CFG 0x40
161 +#define SLEEP_EN BIT(31)
163 +static inline void mt7620_freq_scaling(struct systick_device *sdev, int status)
165 + if (sdev->freq_scale == status)
168 + sdev->freq_scale = status;
170 + pr_info("%s: %s autosleep mode\n", systick.dev.name, (status) ? ("enable") : ("disable"));
172 + rt_sysc_w32(rt_sysc_r32(CLK_LUT_CFG) | SLEEP_EN, CLK_LUT_CFG);
174 + rt_sysc_w32(rt_sysc_r32(CLK_LUT_CFG) & ~SLEEP_EN, CLK_LUT_CFG);
177 +static inline void mt7620_freq_scaling(struct systick_device *sdev, int status) {}
180 +static void systick_set_clock_mode(enum clock_event_mode mode,
181 + struct clock_event_device *evt)
183 + struct systick_device *sdev = container_of(evt, struct systick_device, dev);
186 + case CLOCK_EVT_MODE_ONESHOT:
187 + if (!sdev->irq_requested)
188 + setup_irq(systick.dev.irq, &systick_irqaction);
189 + mt7620_freq_scaling(sdev, 1);
190 + sdev->irq_requested = 1;
191 + iowrite32(CFG_EXT_STK_EN | CFG_CNT_EN, systick.membase + SYSTICK_CONFIG);
194 + case CLOCK_EVT_MODE_SHUTDOWN:
195 + if (sdev->irq_requested)
196 + free_irq(systick.dev.irq, &systick_irqaction);
197 + mt7620_freq_scaling(sdev, 0);
198 + sdev->irq_requested = 0;
199 + iowrite32(0, systick.membase + SYSTICK_CONFIG);
203 + pr_err("%s: Unhandeled mips clock_mode\n", systick.dev.name);
208 +static void __init ralink_systick_init(struct device_node *np)
210 + systick.membase = of_iomap(np, 0);
211 + if (!systick.membase) {
212 + pr_err("%s: of_iomap failed", np->name);
216 + clocksource_mmio_init(systick.membase + SYSTICK_COUNT, np->name,
217 + SYSTICK_FREQ, 301, 16, clocksource_mmio_readl_up);
219 + systick_irqaction.name = np->name;
220 + systick.dev.name = np->name;
221 + clockevent_set_clock(&systick.dev, SYSTICK_FREQ);
222 + systick.dev.max_delta_ns = clockevent_delta2ns(0x7fff, &systick.dev);
223 + systick.dev.min_delta_ns = clockevent_delta2ns(0x3, &systick.dev);
224 + systick.dev.irq = irq_of_parse_and_map(np, 0);
225 + if (!systick.dev.irq)
226 + panic("%s: request_irq failed", np->name);
228 + clockevents_register_device(&systick.dev);
230 + pr_info("%s: runing - mult: %d, shift: %d\n", np->name, systick.dev.mult, systick.dev.shift);
233 +CLOCKSOURCE_OF_DECLARE(systick, "ralink,cevt-systick", ralink_systick_init);
234 --- a/include/linux/clocksource.h
235 +++ b/include/linux/clocksource.h
236 @@ -340,6 +340,7 @@ extern void clocksource_of_init(void);
237 __used __section(__clksrc_of_table) \
238 = { .compatible = compat, .data = fn };
240 +static inline void clocksource_of_init(void) {}
241 #define CLOCKSOURCE_OF_DECLARE(name, compat, fn)