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 Index: linux-3.10.13/arch/mips/ralink/Kconfig
20 ===================================================================
21 --- linux-3.10.13.orig/arch/mips/ralink/Kconfig 2013-09-27 02:18:49.000000000 +0200
22 +++ linux-3.10.13/arch/mips/ralink/Kconfig 2013-10-02 21:40:41.252368892 +0200
27 + bool "Systick Clockevent source"
28 + depends on SOC_RT305X || SOC_MT7620
34 prompt "Ralink SoC selection"
36 Index: linux-3.10.13/arch/mips/ralink/Makefile
37 ===================================================================
38 --- linux-3.10.13.orig/arch/mips/ralink/Makefile 2013-10-02 20:46:43.856230578 +0200
39 +++ linux-3.10.13/arch/mips/ralink/Makefile 2013-10-02 21:13:11.584298451 +0200
42 obj-y := prom.o of.o reset.o clk.o irq.o timer.o
44 +obj-$(CONFIG_CLKEVT_RT3352) += cevt-rt3352.o
46 obj-$(CONFIG_SOC_RT288X) += rt288x.o
47 obj-$(CONFIG_SOC_RT305X) += rt305x.o
48 obj-$(CONFIG_SOC_RT3883) += rt3883.o
49 Index: linux-3.10.13/arch/mips/ralink/cevt-rt3352.c
50 ===================================================================
51 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
52 +++ linux-3.10.13/arch/mips/ralink/cevt-rt3352.c 2013-10-02 21:13:19.396298787 +0200
55 + * This file is subject to the terms and conditions of the GNU General Public
56 + * License. See the file "COPYING" in the main directory of this archive
59 + * Copyright (C) 2013 by John Crispin <blogic@openwrt.org>
62 +#include <linux/clockchips.h>
63 +#include <linux/clocksource.h>
64 +#include <linux/interrupt.h>
65 +#include <linux/reset.h>
66 +#include <linux/init.h>
67 +#include <linux/time.h>
68 +#include <linux/of.h>
69 +#include <linux/of_irq.h>
70 +#include <linux/of_address.h>
72 +#include <asm/mach-ralink/ralink_regs.h>
74 +#define SYSTICK_FREQ (50 * 1000)
76 +#define SYSTICK_CONFIG 0x00
77 +#define SYSTICK_COMPARE 0x04
78 +#define SYSTICK_COUNT 0x08
80 +/* route systick irq to mips irq 7 instead of the r4k-timer */
81 +#define CFG_EXT_STK_EN 0x2
82 +/* enable the counter */
83 +#define CFG_CNT_EN 0x1
85 +struct systick_device {
86 + void __iomem *membase;
87 + struct clock_event_device dev;
92 +static void systick_set_clock_mode(enum clock_event_mode mode,
93 + struct clock_event_device *evt);
95 +static int systick_next_event(unsigned long delta,
96 + struct clock_event_device *evt)
98 + struct systick_device *sdev;
101 + sdev = container_of(evt, struct systick_device, dev);
102 + count = ioread32(sdev->membase + SYSTICK_COUNT);
103 + count = (count + delta) % SYSTICK_FREQ;
104 + iowrite32(count + delta, sdev->membase + SYSTICK_COMPARE);
109 +static void systick_event_handler(struct clock_event_device *dev)
111 + /* noting to do here */
114 +static irqreturn_t systick_interrupt(int irq, void *dev_id)
116 + struct clock_event_device *dev = (struct clock_event_device *) dev_id;
118 + dev->event_handler(dev);
120 + return IRQ_HANDLED;
123 +static struct systick_device systick = {
126 + * cevt-r4k uses 300, make sure systick
127 + * gets used if available
130 + .features = CLOCK_EVT_FEAT_ONESHOT,
131 + .set_next_event = systick_next_event,
132 + .set_mode = systick_set_clock_mode,
133 + .event_handler = systick_event_handler,
137 +static struct irqaction systick_irqaction = {
138 + .handler = systick_interrupt,
139 + .flags = IRQF_PERCPU | IRQF_TIMER,
140 + .dev_id = &systick.dev,
143 +static void systick_set_clock_mode(enum clock_event_mode mode,
144 + struct clock_event_device *evt)
146 + struct systick_device *sdev;
148 + sdev = container_of(evt, struct systick_device, dev);
151 + case CLOCK_EVT_MODE_ONESHOT:
152 + if (!sdev->irq_requested)
153 + setup_irq(systick.dev.irq, &systick_irqaction);
154 + sdev->irq_requested = 1;
155 + iowrite32(CFG_EXT_STK_EN | CFG_CNT_EN,
156 + systick.membase + SYSTICK_CONFIG);
159 + case CLOCK_EVT_MODE_SHUTDOWN:
160 + if (sdev->irq_requested)
161 + free_irq(systick.dev.irq, &systick_irqaction);
162 + sdev->irq_requested = 0;
163 + iowrite32(0, systick.membase + SYSTICK_CONFIG);
167 + pr_err("%s: Unhandeled mips clock_mode\n", systick.dev.name);
172 +static void __init ralink_systick_init(struct device_node *np)
174 + systick.membase = of_iomap(np, 0);
175 + if (!systick.membase)
178 + systick_irqaction.name = np->name;
179 + systick.dev.name = np->name;
180 + clockevents_calc_mult_shift(&systick.dev, SYSTICK_FREQ, 60);
181 + systick.dev.max_delta_ns = clockevent_delta2ns(0x7fff, &systick.dev);
182 + systick.dev.min_delta_ns = clockevent_delta2ns(0x3, &systick.dev);
183 + systick.dev.irq = irq_of_parse_and_map(np, 0);
184 + if (!systick.dev.irq) {
185 + pr_err("%s: request_irq failed", np->name);
189 + clocksource_mmio_init(systick.membase + SYSTICK_COUNT, np->name,
190 + SYSTICK_FREQ, 301, 16, clocksource_mmio_readl_up);
192 + clockevents_register_device(&systick.dev);
194 + pr_info("%s: runing - mult: %d, shift: %d\n",
195 + np->name, systick.dev.mult, systick.dev.shift);
198 +CLOCKSOURCE_OF_DECLARE(systick, "ralink,cevt-systick", ralink_systick_init);