blob: 9ad31489bbeffc449d6c2bc5d51d486863db4a93 [file] [log] [blame]
Linus Walleij47505352017-01-22 13:17:17 +01001/*
Linus Walleijf5bf0ee2017-03-24 22:32:34 +01002 * Faraday Technology FTTMR010 timer driver
Linus Walleij47505352017-01-22 13:17:17 +01003 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
4 *
5 * Based on a rewrite of arch/arm/mach-gemini/timer.c:
6 * Copyright (C) 2001-2006 Storlink, Corp.
7 * Copyright (C) 2008-2009 Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
8 */
9#include <linux/interrupt.h>
10#include <linux/io.h>
11#include <linux/of.h>
12#include <linux/of_address.h>
13#include <linux/of_irq.h>
Linus Walleij47505352017-01-22 13:17:17 +010014#include <linux/clockchips.h>
15#include <linux/clocksource.h>
16#include <linux/sched_clock.h>
Linus Walleij28e71e22017-03-24 22:32:35 +010017#include <linux/clk.h>
Linus Walleije7bad212017-05-18 22:17:01 +020018#include <linux/slab.h>
Linus Walleij47505352017-01-22 13:17:17 +010019
20/*
21 * Register definitions for the timers
22 */
23#define TIMER1_COUNT (0x00)
24#define TIMER1_LOAD (0x04)
25#define TIMER1_MATCH1 (0x08)
26#define TIMER1_MATCH2 (0x0c)
27#define TIMER2_COUNT (0x10)
28#define TIMER2_LOAD (0x14)
29#define TIMER2_MATCH1 (0x18)
30#define TIMER2_MATCH2 (0x1c)
31#define TIMER3_COUNT (0x20)
32#define TIMER3_LOAD (0x24)
33#define TIMER3_MATCH1 (0x28)
34#define TIMER3_MATCH2 (0x2c)
35#define TIMER_CR (0x30)
36#define TIMER_INTR_STATE (0x34)
37#define TIMER_INTR_MASK (0x38)
38
39#define TIMER_1_CR_ENABLE (1 << 0)
40#define TIMER_1_CR_CLOCK (1 << 1)
41#define TIMER_1_CR_INT (1 << 2)
42#define TIMER_2_CR_ENABLE (1 << 3)
43#define TIMER_2_CR_CLOCK (1 << 4)
44#define TIMER_2_CR_INT (1 << 5)
45#define TIMER_3_CR_ENABLE (1 << 6)
46#define TIMER_3_CR_CLOCK (1 << 7)
47#define TIMER_3_CR_INT (1 << 8)
48#define TIMER_1_CR_UPDOWN (1 << 9)
49#define TIMER_2_CR_UPDOWN (1 << 10)
50#define TIMER_3_CR_UPDOWN (1 << 11)
51#define TIMER_DEFAULT_FLAGS (TIMER_1_CR_UPDOWN | \
52 TIMER_3_CR_ENABLE | \
53 TIMER_3_CR_UPDOWN)
54
55#define TIMER_1_INT_MATCH1 (1 << 0)
56#define TIMER_1_INT_MATCH2 (1 << 1)
57#define TIMER_1_INT_OVERFLOW (1 << 2)
58#define TIMER_2_INT_MATCH1 (1 << 3)
59#define TIMER_2_INT_MATCH2 (1 << 4)
60#define TIMER_2_INT_OVERFLOW (1 << 5)
61#define TIMER_3_INT_MATCH1 (1 << 6)
62#define TIMER_3_INT_MATCH2 (1 << 7)
63#define TIMER_3_INT_OVERFLOW (1 << 8)
64#define TIMER_INT_ALL_MASK 0x1ff
65
Linus Walleije7bad212017-05-18 22:17:01 +020066struct fttmr010 {
67 void __iomem *base;
68 unsigned int tick_rate;
69 struct clock_event_device clkevt;
70};
71
72/* A local singleton used by sched_clock, which is stateless */
73static struct fttmr010 *local_fttmr;
74
75static inline struct fttmr010 *to_fttmr010(struct clock_event_device *evt)
76{
77 return container_of(evt, struct fttmr010, clkevt);
78}
Linus Walleij47505352017-01-22 13:17:17 +010079
Linus Walleijf5bf0ee2017-03-24 22:32:34 +010080static u64 notrace fttmr010_read_sched_clock(void)
Linus Walleij47505352017-01-22 13:17:17 +010081{
Linus Walleije7bad212017-05-18 22:17:01 +020082 return readl(local_fttmr->base + TIMER3_COUNT);
Linus Walleij47505352017-01-22 13:17:17 +010083}
84
Linus Walleijf5bf0ee2017-03-24 22:32:34 +010085static int fttmr010_timer_set_next_event(unsigned long cycles,
Linus Walleij47505352017-01-22 13:17:17 +010086 struct clock_event_device *evt)
87{
Linus Walleije7bad212017-05-18 22:17:01 +020088 struct fttmr010 *fttmr010 = to_fttmr010(evt);
Linus Walleij47505352017-01-22 13:17:17 +010089 u32 cr;
90
91 /* Setup the match register */
Linus Walleije7bad212017-05-18 22:17:01 +020092 cr = readl(fttmr010->base + TIMER1_COUNT);
93 writel(cr + cycles, fttmr010->base + TIMER1_MATCH1);
94 if (readl(fttmr010->base + TIMER1_COUNT) - cr > cycles)
Linus Walleij47505352017-01-22 13:17:17 +010095 return -ETIME;
96
97 return 0;
98}
99
Linus Walleijf5bf0ee2017-03-24 22:32:34 +0100100static int fttmr010_timer_shutdown(struct clock_event_device *evt)
Linus Walleij47505352017-01-22 13:17:17 +0100101{
Linus Walleije7bad212017-05-18 22:17:01 +0200102 struct fttmr010 *fttmr010 = to_fttmr010(evt);
Linus Walleij47505352017-01-22 13:17:17 +0100103 u32 cr;
104
Linus Walleij47505352017-01-22 13:17:17 +0100105 /* Stop timer and interrupt. */
Linus Walleije7bad212017-05-18 22:17:01 +0200106 cr = readl(fttmr010->base + TIMER_CR);
Linus Walleij47505352017-01-22 13:17:17 +0100107 cr &= ~(TIMER_1_CR_ENABLE | TIMER_1_CR_INT);
Linus Walleije7bad212017-05-18 22:17:01 +0200108 writel(cr, fttmr010->base + TIMER_CR);
109
110 return 0;
111}
112
113static int fttmr010_timer_set_oneshot(struct clock_event_device *evt)
114{
115 struct fttmr010 *fttmr010 = to_fttmr010(evt);
116 u32 cr;
117
118 /* Stop timer and interrupt. */
119 cr = readl(fttmr010->base + TIMER_CR);
120 cr &= ~(TIMER_1_CR_ENABLE | TIMER_1_CR_INT);
121 writel(cr, fttmr010->base + TIMER_CR);
Linus Walleij47505352017-01-22 13:17:17 +0100122
123 /* Setup counter start from 0 */
Linus Walleije7bad212017-05-18 22:17:01 +0200124 writel(0, fttmr010->base + TIMER1_COUNT);
125 writel(0, fttmr010->base + TIMER1_LOAD);
Linus Walleij47505352017-01-22 13:17:17 +0100126
Linus Walleije7bad212017-05-18 22:17:01 +0200127 /* Enable interrupt */
128 cr = readl(fttmr010->base + TIMER_INTR_MASK);
Linus Walleij47505352017-01-22 13:17:17 +0100129 cr &= ~(TIMER_1_INT_OVERFLOW | TIMER_1_INT_MATCH2);
130 cr |= TIMER_1_INT_MATCH1;
Linus Walleije7bad212017-05-18 22:17:01 +0200131 writel(cr, fttmr010->base + TIMER_INTR_MASK);
Linus Walleij47505352017-01-22 13:17:17 +0100132
Linus Walleije7bad212017-05-18 22:17:01 +0200133 /* Start the timer */
134 cr = readl(fttmr010->base + TIMER_CR);
Linus Walleij47505352017-01-22 13:17:17 +0100135 cr |= TIMER_1_CR_ENABLE;
Linus Walleije7bad212017-05-18 22:17:01 +0200136 writel(cr, fttmr010->base + TIMER_CR);
Linus Walleij47505352017-01-22 13:17:17 +0100137
138 return 0;
139}
140
Linus Walleijf5bf0ee2017-03-24 22:32:34 +0100141static int fttmr010_timer_set_periodic(struct clock_event_device *evt)
Linus Walleij47505352017-01-22 13:17:17 +0100142{
Linus Walleije7bad212017-05-18 22:17:01 +0200143 struct fttmr010 *fttmr010 = to_fttmr010(evt);
144 u32 period = DIV_ROUND_CLOSEST(fttmr010->tick_rate, HZ);
Linus Walleij47505352017-01-22 13:17:17 +0100145 u32 cr;
146
147 /* Stop timer and interrupt */
Linus Walleije7bad212017-05-18 22:17:01 +0200148 cr = readl(fttmr010->base + TIMER_CR);
Linus Walleij47505352017-01-22 13:17:17 +0100149 cr &= ~(TIMER_1_CR_ENABLE | TIMER_1_CR_INT);
Linus Walleije7bad212017-05-18 22:17:01 +0200150 writel(cr, fttmr010->base + TIMER_CR);
Linus Walleij47505352017-01-22 13:17:17 +0100151
152 /* Setup timer to fire at 1/HT intervals. */
153 cr = 0xffffffff - (period - 1);
Linus Walleije7bad212017-05-18 22:17:01 +0200154 writel(cr, fttmr010->base + TIMER1_COUNT);
155 writel(cr, fttmr010->base + TIMER1_LOAD);
Linus Walleij47505352017-01-22 13:17:17 +0100156
157 /* enable interrupt on overflow */
Linus Walleije7bad212017-05-18 22:17:01 +0200158 cr = readl(fttmr010->base + TIMER_INTR_MASK);
Linus Walleij47505352017-01-22 13:17:17 +0100159 cr &= ~(TIMER_1_INT_MATCH1 | TIMER_1_INT_MATCH2);
160 cr |= TIMER_1_INT_OVERFLOW;
Linus Walleije7bad212017-05-18 22:17:01 +0200161 writel(cr, fttmr010->base + TIMER_INTR_MASK);
Linus Walleij47505352017-01-22 13:17:17 +0100162
163 /* Start the timer */
Linus Walleije7bad212017-05-18 22:17:01 +0200164 cr = readl(fttmr010->base + TIMER_CR);
Linus Walleij47505352017-01-22 13:17:17 +0100165 cr |= TIMER_1_CR_ENABLE;
166 cr |= TIMER_1_CR_INT;
Linus Walleije7bad212017-05-18 22:17:01 +0200167 writel(cr, fttmr010->base + TIMER_CR);
Linus Walleij47505352017-01-22 13:17:17 +0100168
169 return 0;
170}
171
Linus Walleij47505352017-01-22 13:17:17 +0100172/*
173 * IRQ handler for the timer
174 */
Linus Walleijf5bf0ee2017-03-24 22:32:34 +0100175static irqreturn_t fttmr010_timer_interrupt(int irq, void *dev_id)
Linus Walleij47505352017-01-22 13:17:17 +0100176{
Linus Walleije7bad212017-05-18 22:17:01 +0200177 struct clock_event_device *evt = dev_id;
Linus Walleij47505352017-01-22 13:17:17 +0100178
179 evt->event_handler(evt);
180 return IRQ_HANDLED;
181}
182
Linus Walleijdd984422017-05-18 22:17:00 +0200183static int __init fttmr010_timer_init(struct device_node *np)
Linus Walleij47505352017-01-22 13:17:17 +0100184{
Linus Walleije7bad212017-05-18 22:17:01 +0200185 struct fttmr010 *fttmr010;
Linus Walleij47505352017-01-22 13:17:17 +0100186 int irq;
Linus Walleijdd984422017-05-18 22:17:00 +0200187 struct clk *clk;
188 int ret;
189
190 /*
191 * These implementations require a clock reference.
192 * FIXME: we currently only support clocking using PCLK
193 * and using EXTCLK is not supported in the driver.
194 */
195 clk = of_clk_get_by_name(np, "PCLK");
196 if (IS_ERR(clk)) {
197 pr_err("could not get PCLK\n");
198 return PTR_ERR(clk);
199 }
200 ret = clk_prepare_enable(clk);
201 if (ret) {
202 pr_err("failed to enable PCLK\n");
203 return ret;
204 }
Linus Walleij47505352017-01-22 13:17:17 +0100205
Linus Walleije7bad212017-05-18 22:17:01 +0200206 fttmr010 = kzalloc(sizeof(*fttmr010), GFP_KERNEL);
207 if (!fttmr010) {
208 ret = -ENOMEM;
209 goto out_disable_clock;
210 }
211 fttmr010->tick_rate = clk_get_rate(clk);
212
213 fttmr010->base = of_iomap(np, 0);
214 if (!fttmr010->base) {
Linus Walleij47505352017-01-22 13:17:17 +0100215 pr_err("Can't remap registers");
Linus Walleije7bad212017-05-18 22:17:01 +0200216 ret = -ENXIO;
217 goto out_free;
Linus Walleij47505352017-01-22 13:17:17 +0100218 }
219 /* IRQ for timer 1 */
220 irq = irq_of_parse_and_map(np, 0);
221 if (irq <= 0) {
222 pr_err("Can't parse IRQ");
Linus Walleije7bad212017-05-18 22:17:01 +0200223 ret = -EINVAL;
224 goto out_unmap;
Linus Walleij47505352017-01-22 13:17:17 +0100225 }
226
Linus Walleij47505352017-01-22 13:17:17 +0100227 /*
228 * Reset the interrupt mask and status
229 */
Linus Walleije7bad212017-05-18 22:17:01 +0200230 writel(TIMER_INT_ALL_MASK, fttmr010->base + TIMER_INTR_MASK);
231 writel(0, fttmr010->base + TIMER_INTR_STATE);
232 writel(TIMER_DEFAULT_FLAGS, fttmr010->base + TIMER_CR);
Linus Walleij47505352017-01-22 13:17:17 +0100233
234 /*
235 * Setup free-running clocksource timer (interrupts
236 * disabled.)
237 */
Linus Walleije7bad212017-05-18 22:17:01 +0200238 local_fttmr = fttmr010;
239 writel(0, fttmr010->base + TIMER3_COUNT);
240 writel(0, fttmr010->base + TIMER3_LOAD);
241 writel(0, fttmr010->base + TIMER3_MATCH1);
242 writel(0, fttmr010->base + TIMER3_MATCH2);
243 clocksource_mmio_init(fttmr010->base + TIMER3_COUNT,
244 "FTTMR010-TIMER3",
245 fttmr010->tick_rate,
Linus Walleij47505352017-01-22 13:17:17 +0100246 300, 32, clocksource_mmio_readl_up);
Linus Walleije7bad212017-05-18 22:17:01 +0200247 sched_clock_register(fttmr010_read_sched_clock, 32,
248 fttmr010->tick_rate);
Linus Walleij47505352017-01-22 13:17:17 +0100249
250 /*
Linus Walleije7bad212017-05-18 22:17:01 +0200251 * Setup clockevent timer (interrupt-driven) on timer 1.
Linus Walleij47505352017-01-22 13:17:17 +0100252 */
Linus Walleije7bad212017-05-18 22:17:01 +0200253 writel(0, fttmr010->base + TIMER1_COUNT);
254 writel(0, fttmr010->base + TIMER1_LOAD);
255 writel(0, fttmr010->base + TIMER1_MATCH1);
256 writel(0, fttmr010->base + TIMER1_MATCH2);
257 ret = request_irq(irq, fttmr010_timer_interrupt, IRQF_TIMER,
258 "FTTMR010-TIMER1", &fttmr010->clkevt);
259 if (ret) {
260 pr_err("FTTMR010-TIMER1 no IRQ\n");
261 goto out_unmap;
262 }
263
264 fttmr010->clkevt.name = "FTTMR010-TIMER1";
265 /* Reasonably fast and accurate clock event */
266 fttmr010->clkevt.rating = 300;
267 fttmr010->clkevt.features = CLOCK_EVT_FEAT_PERIODIC |
268 CLOCK_EVT_FEAT_ONESHOT;
269 fttmr010->clkevt.set_next_event = fttmr010_timer_set_next_event;
270 fttmr010->clkevt.set_state_shutdown = fttmr010_timer_shutdown;
271 fttmr010->clkevt.set_state_periodic = fttmr010_timer_set_periodic;
272 fttmr010->clkevt.set_state_oneshot = fttmr010_timer_set_oneshot;
273 fttmr010->clkevt.tick_resume = fttmr010_timer_shutdown;
274 fttmr010->clkevt.cpumask = cpumask_of(0);
275 fttmr010->clkevt.irq = irq;
276 clockevents_config_and_register(&fttmr010->clkevt,
277 fttmr010->tick_rate,
Linus Walleij47505352017-01-22 13:17:17 +0100278 1, 0xffffffff);
279
280 return 0;
Linus Walleije7bad212017-05-18 22:17:01 +0200281
282out_unmap:
283 iounmap(fttmr010->base);
284out_free:
285 kfree(fttmr010);
286out_disable_clock:
287 clk_disable_unprepare(clk);
288
289 return ret;
Linus Walleij47505352017-01-22 13:17:17 +0100290}
Linus Walleijdd984422017-05-18 22:17:00 +0200291CLOCKSOURCE_OF_DECLARE(fttmr010, "faraday,fttmr010", fttmr010_timer_init);
292CLOCKSOURCE_OF_DECLARE(gemini, "cortina,gemini-timer", fttmr010_timer_init);