5626ada5517063ad188211c6e2624258cc14c071
[openwrt/openwrt.git] / target / linux / ramips / patches-3.10 / 0011-MIPS-ralink-add-support-for-systick-timer-found-on-n.patch
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
5 newer ralink SoC
6
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.
10
11 Signed-off-by: John Crispin <blogic@openwrt.org>
12 ---
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
18
19 --- a/arch/mips/ralink/Kconfig
20 +++ b/arch/mips/ralink/Kconfig
21 @@ -1,5 +1,12 @@
22 if RALINK
23
24 +config CLKEVT_RT3352
25 + bool "Systick Clockevent source"
26 + depends on SOC_RT305X || SOC_MT7620
27 + default y
28 + select CLKSRC_OF
29 + select CLKSRC_MMIO
30 +
31 choice
32 prompt "Ralink SoC selection"
33 default SOC_RT305X
34 --- a/arch/mips/ralink/Makefile
35 +++ b/arch/mips/ralink/Makefile
36 @@ -8,6 +8,8 @@
37
38 obj-y := prom.o of.o reset.o clk.o irq.o timer.o
39
40 +obj-$(CONFIG_CLKEVT_RT3352) += cevt-rt3352.o
41 +
42 obj-$(CONFIG_SOC_RT288X) += rt288x.o
43 obj-$(CONFIG_SOC_RT305X) += rt305x.o
44 obj-$(CONFIG_SOC_RT3883) += rt3883.o
45 --- /dev/null
46 +++ b/arch/mips/ralink/cevt-rt3352.c
47 @@ -0,0 +1,145 @@
48 +/*
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
51 + * for more details.
52 + *
53 + * Copyright (C) 2013 by John Crispin <blogic@openwrt.org>
54 + */
55 +
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>
65 +
66 +#include <asm/mach-ralink/ralink_regs.h>
67 +
68 +#define SYSTICK_FREQ (50 * 1000)
69 +
70 +#define SYSTICK_CONFIG 0x00
71 +#define SYSTICK_COMPARE 0x04
72 +#define SYSTICK_COUNT 0x08
73 +
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
78 +
79 +struct systick_device {
80 + void __iomem *membase;
81 + struct clock_event_device dev;
82 + int irq_requested;
83 + int freq_scale;
84 +};
85 +
86 +static void systick_set_clock_mode(enum clock_event_mode mode,
87 + struct clock_event_device *evt);
88 +
89 +static int systick_next_event(unsigned long delta,
90 + struct clock_event_device *evt)
91 +{
92 + struct systick_device *sdev;
93 + u32 count;
94 +
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);
99 +
100 + return 0;
101 +}
102 +
103 +static void systick_event_handler(struct clock_event_device *dev)
104 +{
105 + /* noting to do here */
106 +}
107 +
108 +static irqreturn_t systick_interrupt(int irq, void *dev_id)
109 +{
110 + struct clock_event_device *dev = (struct clock_event_device *) dev_id;
111 +
112 + dev->event_handler(dev);
113 +
114 + return IRQ_HANDLED;
115 +}
116 +
117 +static struct systick_device systick = {
118 + .dev = {
119 + /*
120 + * cevt-r4k uses 300, make sure systick
121 + * gets used if available
122 + */
123 + .rating = 310,
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,
128 + },
129 +};
130 +
131 +static struct irqaction systick_irqaction = {
132 + .handler = systick_interrupt,
133 + .flags = IRQF_PERCPU | IRQF_TIMER,
134 + .dev_id = &systick.dev,
135 +};
136 +
137 +static void systick_set_clock_mode(enum clock_event_mode mode,
138 + struct clock_event_device *evt)
139 +{
140 + struct systick_device *sdev;
141 +
142 + sdev = container_of(evt, struct systick_device, dev);
143 +
144 + switch (mode) {
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);
151 + break;
152 +
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);
158 + break;
159 +
160 + default:
161 + pr_err("%s: Unhandeled mips clock_mode\n", systick.dev.name);
162 + break;
163 + }
164 +}
165 +
166 +static void __init ralink_systick_init(struct device_node *np)
167 +{
168 + systick.membase = of_iomap(np, 0);
169 + if (!systick.membase)
170 + return;
171 +
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);
180 + return;
181 + }
182 +
183 + clocksource_mmio_init(systick.membase + SYSTICK_COUNT, np->name,
184 + SYSTICK_FREQ, 301, 16, clocksource_mmio_readl_up);
185 +
186 + clockevents_register_device(&systick.dev);
187 +
188 + pr_info("%s: runing - mult: %d, shift: %d\n",
189 + np->name, systick.dev.mult, systick.dev.shift);
190 +}
191 +
192 +CLOCKSOURCE_OF_DECLARE(systick, "ralink,cevt-systick", ralink_systick_init);