1 From 3b511d972b556712f89ccc68825c0ec8f398dc5c Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 28 Jul 2013 13:46:09 +0200
4 Subject: [PATCH 11/25] MIPS: ralink: add support for systick timer found on
7 Newer Ralink SoC (MT7620x and RT5350) have a 50KHz clock that runs independent
8 of the SoC master clock. If we want to automatic frequency scaling to work we
9 need to use the systick timer as the clock source.
11 Signed-off-by: John Crispin <blogic@openwrt.org>
13 arch/mips/ralink/Kconfig | 7 ++
14 arch/mips/ralink/Makefile | 2 +
15 arch/mips/ralink/cevt-rt3352.c | 145 ++++++++++++++++++++++++++++++++++++++++
16 3 files changed, 154 insertions(+)
17 create mode 100644 arch/mips/ralink/cevt-rt3352.c
19 --- a/arch/mips/ralink/Kconfig
20 +++ b/arch/mips/ralink/Kconfig
25 + bool "Systick Clockevent source"
26 + depends on SOC_RT305X || SOC_MT7620
32 prompt "Ralink SoC selection"
34 --- a/arch/mips/ralink/Makefile
35 +++ b/arch/mips/ralink/Makefile
38 obj-y := prom.o of.o reset.o clk.o irq.o timer.o
40 +obj-$(CONFIG_CLKEVT_RT3352) += cevt-rt3352.o
42 obj-$(CONFIG_SOC_RT288X) += rt288x.o
43 obj-$(CONFIG_SOC_RT305X) += rt305x.o
44 obj-$(CONFIG_SOC_RT3883) += rt3883.o
46 +++ b/arch/mips/ralink/cevt-rt3352.c
49 + * This file is subject to the terms and conditions of the GNU General Public
50 + * License. See the file "COPYING" in the main directory of this archive
53 + * Copyright (C) 2013 by John Crispin <blogic@openwrt.org>
56 +#include <linux/clockchips.h>
57 +#include <linux/clocksource.h>
58 +#include <linux/interrupt.h>
59 +#include <linux/reset.h>
60 +#include <linux/init.h>
61 +#include <linux/time.h>
62 +#include <linux/of.h>
63 +#include <linux/of_irq.h>
64 +#include <linux/of_address.h>
66 +#include <asm/mach-ralink/ralink_regs.h>
68 +#define SYSTICK_FREQ (50 * 1000)
70 +#define SYSTICK_CONFIG 0x00
71 +#define SYSTICK_COMPARE 0x04
72 +#define SYSTICK_COUNT 0x08
74 +/* route systick irq to mips irq 7 instead of the r4k-timer */
75 +#define CFG_EXT_STK_EN 0x2
76 +/* enable the counter */
77 +#define CFG_CNT_EN 0x1
79 +struct systick_device {
80 + void __iomem *membase;
81 + struct clock_event_device dev;
86 +static void systick_set_clock_mode(enum clock_event_mode mode,
87 + struct clock_event_device *evt);
89 +static int systick_next_event(unsigned long delta,
90 + struct clock_event_device *evt)
92 + struct systick_device *sdev;
95 + sdev = container_of(evt, struct systick_device, dev);
96 + count = ioread32(sdev->membase + SYSTICK_COUNT);
97 + count = (count + delta) % SYSTICK_FREQ;
98 + iowrite32(count + delta, sdev->membase + SYSTICK_COMPARE);
103 +static void systick_event_handler(struct clock_event_device *dev)
105 + /* noting to do here */
108 +static irqreturn_t systick_interrupt(int irq, void *dev_id)
110 + struct clock_event_device *dev = (struct clock_event_device *) dev_id;
112 + dev->event_handler(dev);
114 + return IRQ_HANDLED;
117 +static struct systick_device systick = {
120 + * cevt-r4k uses 300, make sure systick
121 + * gets used if available
124 + .features = CLOCK_EVT_FEAT_ONESHOT,
125 + .set_next_event = systick_next_event,
126 + .set_mode = systick_set_clock_mode,
127 + .event_handler = systick_event_handler,
131 +static struct irqaction systick_irqaction = {
132 + .handler = systick_interrupt,
133 + .flags = IRQF_PERCPU | IRQF_TIMER,
134 + .dev_id = &systick.dev,
137 +static void systick_set_clock_mode(enum clock_event_mode mode,
138 + struct clock_event_device *evt)
140 + struct systick_device *sdev;
142 + sdev = container_of(evt, struct systick_device, dev);
145 + case CLOCK_EVT_MODE_ONESHOT:
146 + if (!sdev->irq_requested)
147 + setup_irq(systick.dev.irq, &systick_irqaction);
148 + sdev->irq_requested = 1;
149 + iowrite32(CFG_EXT_STK_EN | CFG_CNT_EN,
150 + systick.membase + SYSTICK_CONFIG);
153 + case CLOCK_EVT_MODE_SHUTDOWN:
154 + if (sdev->irq_requested)
155 + free_irq(systick.dev.irq, &systick_irqaction);
156 + sdev->irq_requested = 0;
157 + iowrite32(0, systick.membase + SYSTICK_CONFIG);
161 + pr_err("%s: Unhandeled mips clock_mode\n", systick.dev.name);
166 +static void __init ralink_systick_init(struct device_node *np)
168 + systick.membase = of_iomap(np, 0);
169 + if (!systick.membase)
172 + systick_irqaction.name = np->name;
173 + systick.dev.name = np->name;
174 + clockevents_calc_mult_shift(&systick.dev, SYSTICK_FREQ, 60);
175 + systick.dev.max_delta_ns = clockevent_delta2ns(0x7fff, &systick.dev);
176 + systick.dev.min_delta_ns = clockevent_delta2ns(0x3, &systick.dev);
177 + systick.dev.irq = irq_of_parse_and_map(np, 0);
178 + if (!systick.dev.irq) {
179 + pr_err("%s: request_irq failed", np->name);
183 + clocksource_mmio_init(systick.membase + SYSTICK_COUNT, np->name,
184 + SYSTICK_FREQ, 301, 16, clocksource_mmio_readl_up);
186 + clockevents_register_device(&systick.dev);
188 + pr_info("%s: runing - mult: %d, shift: %d\n",
189 + np->name, systick.dev.mult, systick.dev.shift);
192 +CLOCKSOURCE_OF_DECLARE(systick, "ralink,cevt-systick", ralink_systick_init);