1 From 4f3ae2a7ee1b1c9b9cab287c828f2ed7b1858495 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Thu, 20 Jun 2013 19:21:52 +0200
4 Subject: [PATCH 79/79] 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/pci/Makefile.rej | 10 +++
10 arch/mips/ralink/Kconfig | 2 +
11 arch/mips/ralink/Kconfig.rej | 10 +++
12 arch/mips/ralink/clk.c | 1 +
13 drivers/clocksource/Kconfig | 6 ++
14 drivers/clocksource/Makefile | 1 +
15 drivers/clocksource/cevt-rt3352.c | 162 +++++++++++++++++++++++++++++++++++++
16 7 files changed, 192 insertions(+)
17 create mode 100644 arch/mips/pci/Makefile.rej
18 create mode 100644 arch/mips/ralink/Kconfig.rej
19 create mode 100644 drivers/clocksource/cevt-rt3352.c
21 diff --git a/arch/mips/pci/Makefile.rej b/arch/mips/pci/Makefile.rej
23 index 0000000..c5761e8
25 +++ b/arch/mips/pci/Makefile.rej
27 +--- arch/mips/pci/Makefile
28 ++++ arch/mips/pci/Makefile
30 + obj-$(CONFIG_PCI_LANTIQ) += pci-lantiq.o ops-lantiq.o
31 + obj-$(CONFIG_SOC_RT2880) += pci-rt2880.o
32 + obj-$(CONFIG_SOC_RT3883) += pci-rt3883.o
33 ++obj-$(CONFIG_SOC_MT7620) += pci-mt7620a.o
34 + obj-$(CONFIG_TANBAC_TB0219) += fixup-tb0219.o
35 + obj-$(CONFIG_TANBAC_TB0226) += fixup-tb0226.o
36 + obj-$(CONFIG_TANBAC_TB0287) += fixup-tb0287.o
37 diff --git a/arch/mips/ralink/Kconfig b/arch/mips/ralink/Kconfig
38 index f89fdf8..c8d5b6c 100644
39 --- a/arch/mips/ralink/Kconfig
40 +++ b/arch/mips/ralink/Kconfig
41 @@ -15,6 +15,7 @@ choice
42 select USB_ARCH_HAS_HCD
43 select USB_ARCH_HAS_OHCI
44 select USB_ARCH_HAS_EHCI
45 + select CLKEVT_RT3352
49 @@ -24,6 +25,7 @@ choice
53 + select CLKEVT_RT3352
57 diff --git a/arch/mips/ralink/Kconfig.rej b/arch/mips/ralink/Kconfig.rej
59 index 0000000..d3a804f
61 +++ b/arch/mips/ralink/Kconfig.rej
63 +--- arch/mips/ralink/Kconfig
64 ++++ arch/mips/ralink/Kconfig
69 ++ select CLKEVT_RT3352
73 diff --git a/arch/mips/ralink/clk.c b/arch/mips/ralink/clk.c
74 index 8dfa22f..bba0cdf 100644
75 --- a/arch/mips/ralink/clk.c
76 +++ b/arch/mips/ralink/clk.c
77 @@ -69,4 +69,5 @@ void __init plat_time_init(void)
78 pr_info("CPU Clock: %ldMHz\n", clk_get_rate(clk) / 1000000);
79 mips_hpt_frequency = clk_get_rate(clk) / 2;
81 + clocksource_of_init();
83 diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig
84 index a32b7a9..90bed1a 100644
85 --- a/drivers/clocksource/Kconfig
86 +++ b/drivers/clocksource/Kconfig
87 @@ -7,6 +7,12 @@ config CLKSRC_I8253
93 + depends on MIPS && RALINK
100 diff --git a/drivers/clocksource/Makefile b/drivers/clocksource/Makefile
101 index a33f792..5031f82 100644
102 --- a/drivers/clocksource/Makefile
103 +++ b/drivers/clocksource/Makefile
104 @@ -10,6 +10,7 @@ obj-$(CONFIG_SH_TIMER_TMU) += sh_tmu.o
105 obj-$(CONFIG_EM_TIMER_STI) += em_sti.o
106 obj-$(CONFIG_CLKBLD_I8253) += i8253.o
107 obj-$(CONFIG_CLKSRC_MMIO) += mmio.o
108 +obj-$(CONFIG_CLKEVT_RT3352) += cevt-rt3352.o
109 obj-$(CONFIG_DW_APB_TIMER) += dw_apb_timer.o
110 obj-$(CONFIG_DW_APB_TIMER_OF) += dw_apb_timer_of.o
111 obj-$(CONFIG_CLKSRC_NOMADIK_MTU) += nomadik-mtu.o
112 diff --git a/drivers/clocksource/cevt-rt3352.c b/drivers/clocksource/cevt-rt3352.c
114 index 0000000..bd50edd
116 +++ b/drivers/clocksource/cevt-rt3352.c
119 + * This file is subject to the terms and conditions of the GNU General Public
120 + * License. See the file "COPYING" in the main directory of this archive
121 + * for more details.
123 + * Copyright (C) 2013 by John Crispin <blogic@openwrt.org>
126 +#include <linux/clockchips.h>
127 +#include <linux/clocksource.h>
128 +#include <linux/interrupt.h>
129 +#include <linux/reset.h>
130 +#include <linux/init.h>
131 +#include <linux/of.h>
132 +#include <linux/of_irq.h>
133 +#include <linux/of_address.h>
135 +#include <asm/mach-ralink/ralink_regs.h>
136 +#include <asm/time.h>
138 +#define SYSTICK_FREQ (50 * 1000)
140 +#define SYSTICK_CONFIG 0x00
141 +#define SYSTICK_COMPARE 0x04
142 +#define SYSTICK_COUNT 0x08
144 +/* route systick irq to mips irq 7 instead of the r4k-timer */
145 +#define CFG_EXT_STK_EN 0x2
146 +/* enable the counter */
147 +#define CFG_CNT_EN 0x1
149 +struct systick_device {
150 + void __iomem *membase;
151 + struct clock_event_device dev;
156 +static void systick_set_clock_mode(enum clock_event_mode mode,
157 + struct clock_event_device *evt);
159 +static int systick_next_event(unsigned long delta,
160 + struct clock_event_device *evt)
162 + struct systick_device *sdev = container_of(evt, struct systick_device, dev);
165 + count = ioread32(sdev->membase + SYSTICK_COUNT);
166 + count = (count + delta) % SYSTICK_FREQ;
167 + iowrite32(count + delta, sdev->membase + SYSTICK_COMPARE);
172 +static void systick_event_handler(struct clock_event_device *dev)
174 + /* noting to do here */
177 +static irqreturn_t systick_interrupt(int irq, void *dev_id)
179 + struct clock_event_device *dev = (struct clock_event_device *) dev_id;
181 + dev->event_handler(dev);
183 + return IRQ_HANDLED;
186 +static struct systick_device systick = {
188 + /* cevt-r4k uses 300, make sure systick gets used if available */
190 + .features = CLOCK_EVT_FEAT_ONESHOT,
191 + .set_next_event = systick_next_event,
192 + .set_mode = systick_set_clock_mode,
193 + .event_handler = systick_event_handler,
197 +static struct irqaction systick_irqaction = {
198 + .handler = systick_interrupt,
199 + .flags = IRQF_PERCPU | IRQF_TIMER,
200 + .dev_id = &systick.dev,
204 +#ifdef CONFIG_SOC_MT7620
206 +#define CLK_LUT_CFG 0x40
207 +#define SLEEP_EN BIT(31)
209 +static inline void mt7620_freq_scaling(struct systick_device *sdev, int status)
211 + if (sdev->freq_scale == status)
214 + sdev->freq_scale = status;
216 + pr_info("%s: %s autosleep mode\n", systick.dev.name, (status) ? ("enable") : ("disable"));
218 + rt_sysc_w32(rt_sysc_r32(CLK_LUT_CFG) | SLEEP_EN, CLK_LUT_CFG);
220 + rt_sysc_w32(rt_sysc_r32(CLK_LUT_CFG) & ~SLEEP_EN, CLK_LUT_CFG);
223 +static inline void mt7620_freq_scaling(struct systick_device *sdev, int status) {}
226 +static void systick_set_clock_mode(enum clock_event_mode mode,
227 + struct clock_event_device *evt)
229 + struct systick_device *sdev = container_of(evt, struct systick_device, dev);
232 + case CLOCK_EVT_MODE_ONESHOT:
233 + if (!sdev->irq_requested)
234 + setup_irq(systick.dev.irq, &systick_irqaction);
235 + mt7620_freq_scaling(sdev, 1);
236 + sdev->irq_requested = 1;
237 + iowrite32(CFG_EXT_STK_EN | CFG_CNT_EN, systick.membase + SYSTICK_CONFIG);
240 + case CLOCK_EVT_MODE_SHUTDOWN:
241 + if (sdev->irq_requested)
242 + free_irq(systick.dev.irq, &systick_irqaction);
243 + mt7620_freq_scaling(sdev, 0);
244 + sdev->irq_requested = 0;
245 + iowrite32(0, systick.membase + SYSTICK_CONFIG);
249 + pr_err("%s: Unhandeled mips clock_mode\n", systick.dev.name);
254 +static void __init ralink_systick_init(struct device_node *np)
256 + systick.membase = of_iomap(np, 0);
257 + if (!systick.membase) {
258 + pr_err("%s: of_iomap failed", np->name);
262 + clocksource_mmio_init(systick.membase + SYSTICK_COUNT, np->name,
263 + SYSTICK_FREQ, 301, 16, clocksource_mmio_readl_up);
265 + systick_irqaction.name = np->name;
266 + systick.dev.name = np->name;
267 + clockevent_set_clock(&systick.dev, SYSTICK_FREQ);
268 + systick.dev.max_delta_ns = clockevent_delta2ns(0x7fff, &systick.dev);
269 + systick.dev.min_delta_ns = clockevent_delta2ns(0x3, &systick.dev);
270 + systick.dev.irq = irq_of_parse_and_map(np, 0);
271 + if (!systick.dev.irq)
272 + panic("%s: request_irq failed", np->name);
274 + clockevents_register_device(&systick.dev);
276 + pr_info("%s: runing - mult: %d, shift: %d\n", np->name, systick.dev.mult, systick.dev.shift);
279 +CLOCKSOURCE_OF_DECLARE(systick, "ralink,cevt-systick", ralink_systick_init);