blob: e90ab5b63a9068ec3901b85cab9b230db67a837c [file] [log] [blame]
SAN People73a59c12006-01-09 17:05:41 +00001/*
Andrew Victor9d041262007-02-05 11:42:07 +01002 * linux/arch/arm/mach-at91/at91rm9200_time.c
SAN People73a59c12006-01-09 17:05:41 +00003 *
4 * Copyright (C) 2003 SAN People
5 * Copyright (C) 2003 ATMEL
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
David Brownell5e802df2007-07-31 01:41:26 +010022#include <linux/kernel.h>
SAN People73a59c12006-01-09 17:05:41 +000023#include <linux/interrupt.h>
Thomas Gleixner07d265d2006-07-01 23:01:50 +010024#include <linux/irq.h>
Alexandre Belloni216ab8f2015-08-16 11:23:44 +020025#include <linux/clk.h>
David Brownell5e802df2007-07-31 01:41:26 +010026#include <linux/clockchips.h>
Joachim Eastwood9fce85c2012-04-04 19:15:15 +020027#include <linux/export.h>
Alexandre Belloniadf2edf2015-03-12 13:07:32 +010028#include <linux/mfd/syscon.h>
29#include <linux/mfd/syscon/atmel-st.h>
Joachim Eastwood454c46d2012-10-28 18:31:07 +000030#include <linux/of_irq.h>
Alexandre Belloniadf2edf2015-03-12 13:07:32 +010031#include <linux/regmap.h>
SAN People73a59c12006-01-09 17:05:41 +000032
Andrew Victor963151f2006-06-19 15:23:41 +010033static unsigned long last_crtr;
David Brownell5e802df2007-07-31 01:41:26 +010034static u32 irqmask;
35static struct clock_event_device clkevt;
Alexandre Belloniadf2edf2015-03-12 13:07:32 +010036static struct regmap *regmap_st;
Alexandre Belloni216ab8f2015-08-16 11:23:44 +020037static int timer_latch;
Jean-Christophe PLAGNIOL-VILLARD2f5893c2011-10-16 18:17:09 +080038
SAN People73a59c12006-01-09 17:05:41 +000039/*
David Brownell5e802df2007-07-31 01:41:26 +010040 * The ST_CRTR is updated asynchronously to the master clock ... but
41 * the updates as seen by the CPU don't seem to be strictly monotonic.
42 * Waiting until we read the same value twice avoids glitching.
SAN People73a59c12006-01-09 17:05:41 +000043 */
David Brownell5e802df2007-07-31 01:41:26 +010044static inline unsigned long read_CRTR(void)
45{
Alexandre Belloniadf2edf2015-03-12 13:07:32 +010046 unsigned int x1, x2;
SAN People73a59c12006-01-09 17:05:41 +000047
Alexandre Belloniadf2edf2015-03-12 13:07:32 +010048 regmap_read(regmap_st, AT91_ST_CRTR, &x1);
SAN People73a59c12006-01-09 17:05:41 +000049 do {
Alexandre Belloniadf2edf2015-03-12 13:07:32 +010050 regmap_read(regmap_st, AT91_ST_CRTR, &x2);
David Brownell5e802df2007-07-31 01:41:26 +010051 if (x1 == x2)
52 break;
53 x1 = x2;
54 } while (1);
SAN People73a59c12006-01-09 17:05:41 +000055 return x1;
56}
57
58/*
SAN People73a59c12006-01-09 17:05:41 +000059 * IRQ handler for the timer.
60 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -070061static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
SAN People73a59c12006-01-09 17:05:41 +000062{
Alexandre Belloniadf2edf2015-03-12 13:07:32 +010063 u32 sr;
64
65 regmap_read(regmap_st, AT91_ST_SR, &sr);
66 sr &= irqmask;
SAN People73a59c12006-01-09 17:05:41 +000067
Uwe Kleine-König501d7032009-09-21 09:30:09 +020068 /*
69 * irqs should be disabled here, but as the irq is shared they are only
70 * guaranteed to be off if the timer irq is registered first.
71 */
72 WARN_ON_ONCE(!irqs_disabled());
73
David Brownell5e802df2007-07-31 01:41:26 +010074 /* simulate "oneshot" timer with alarm */
75 if (sr & AT91_ST_ALMS) {
76 clkevt.event_handler(&clkevt);
SAN People73a59c12006-01-09 17:05:41 +000077 return IRQ_HANDLED;
78 }
David Brownell5e802df2007-07-31 01:41:26 +010079
80 /* periodic mode should handle delayed ticks */
81 if (sr & AT91_ST_PITS) {
82 u32 crtr = read_CRTR();
83
Alexandre Belloni216ab8f2015-08-16 11:23:44 +020084 while (((crtr - last_crtr) & AT91_ST_CRTV) >= timer_latch) {
85 last_crtr += timer_latch;
David Brownell5e802df2007-07-31 01:41:26 +010086 clkevt.event_handler(&clkevt);
87 }
88 return IRQ_HANDLED;
89 }
90
91 /* this irq is shared ... */
92 return IRQ_NONE;
SAN People73a59c12006-01-09 17:05:41 +000093}
94
Magnus Damm8e196082009-04-21 12:24:00 -070095static cycle_t read_clk32k(struct clocksource *cs)
Andrew Victor2a6f9902006-06-19 15:26:50 +010096{
David Brownell5e802df2007-07-31 01:41:26 +010097 return read_CRTR();
Andrew Victor2a6f9902006-06-19 15:26:50 +010098}
99
David Brownell5e802df2007-07-31 01:41:26 +0100100static struct clocksource clk32k = {
101 .name = "32k_counter",
102 .rating = 150,
103 .read = read_clk32k,
104 .mask = CLOCKSOURCE_MASK(20),
David Brownell5e802df2007-07-31 01:41:26 +0100105 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
106};
107
Viresh Kumar8ab28232015-06-18 16:24:45 +0530108static void clkdev32k_disable_and_flush_irq(void)
David Brownell5e802df2007-07-31 01:41:26 +0100109{
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100110 unsigned int val;
111
David Brownell5e802df2007-07-31 01:41:26 +0100112 /* Disable and flush pending timer interrupts */
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100113 regmap_write(regmap_st, AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);
114 regmap_read(regmap_st, AT91_ST_SR, &val);
David Brownell5e802df2007-07-31 01:41:26 +0100115 last_crtr = read_CRTR();
Viresh Kumar8ab28232015-06-18 16:24:45 +0530116}
117
118static int clkevt32k_shutdown(struct clock_event_device *evt)
119{
120 clkdev32k_disable_and_flush_irq();
121 irqmask = 0;
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100122 regmap_write(regmap_st, AT91_ST_IER, irqmask);
Viresh Kumar8ab28232015-06-18 16:24:45 +0530123 return 0;
124}
125
126static int clkevt32k_set_oneshot(struct clock_event_device *dev)
127{
128 clkdev32k_disable_and_flush_irq();
129
130 /*
131 * ALM for oneshot irqs, set by next_event()
132 * before 32 seconds have passed.
133 */
134 irqmask = AT91_ST_ALMS;
135 regmap_write(regmap_st, AT91_ST_RTAR, last_crtr);
136 regmap_write(regmap_st, AT91_ST_IER, irqmask);
137 return 0;
138}
139
140static int clkevt32k_set_periodic(struct clock_event_device *dev)
141{
142 clkdev32k_disable_and_flush_irq();
143
144 /* PIT for periodic irqs; fixed rate of 1/HZ */
145 irqmask = AT91_ST_PITS;
Alexandre Belloni216ab8f2015-08-16 11:23:44 +0200146 regmap_write(regmap_st, AT91_ST_PIMR, timer_latch);
Viresh Kumar8ab28232015-06-18 16:24:45 +0530147 regmap_write(regmap_st, AT91_ST_IER, irqmask);
148 return 0;
David Brownell5e802df2007-07-31 01:41:26 +0100149}
150
151static int
152clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)
153{
David Brownell5e802df2007-07-31 01:41:26 +0100154 u32 alm;
155 int status = 0;
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100156 unsigned int val;
David Brownell5e802df2007-07-31 01:41:26 +0100157
158 BUG_ON(delta < 2);
159
David Brownell5e802df2007-07-31 01:41:26 +0100160 /* The alarm IRQ uses absolute time (now+delta), not the relative
161 * time (delta) in our calling convention. Like all clockevents
162 * using such "match" hardware, we have a race to defend against.
163 *
164 * Our defense here is to have set up the clockevent device so the
165 * delta is at least two. That way we never end up writing RTAR
166 * with the value then held in CRTR ... which would mean the match
167 * wouldn't trigger until 32 seconds later, after CRTR wraps.
168 */
169 alm = read_CRTR();
170
171 /* Cancel any pending alarm; flush any pending IRQ */
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100172 regmap_write(regmap_st, AT91_ST_RTAR, alm);
173 regmap_read(regmap_st, AT91_ST_SR, &val);
David Brownell5e802df2007-07-31 01:41:26 +0100174
175 /* Schedule alarm by writing RTAR. */
176 alm += delta;
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100177 regmap_write(regmap_st, AT91_ST_RTAR, alm);
David Brownell5e802df2007-07-31 01:41:26 +0100178
David Brownell5e802df2007-07-31 01:41:26 +0100179 return status;
180}
181
182static struct clock_event_device clkevt = {
Viresh Kumar8ab28232015-06-18 16:24:45 +0530183 .name = "at91_tick",
184 .features = CLOCK_EVT_FEAT_PERIODIC |
185 CLOCK_EVT_FEAT_ONESHOT,
186 .rating = 150,
187 .set_next_event = clkevt32k_next_event,
188 .set_state_shutdown = clkevt32k_shutdown,
189 .set_state_periodic = clkevt32k_set_periodic,
190 .set_state_oneshot = clkevt32k_set_oneshot,
191 .tick_resume = clkevt32k_shutdown,
David Brownell5e802df2007-07-31 01:41:26 +0100192};
193
SAN People73a59c12006-01-09 17:05:41 +0000194/*
David Brownell5e802df2007-07-31 01:41:26 +0100195 * ST (system timer) module supports both clockevents and clocksource.
SAN People73a59c12006-01-09 17:05:41 +0000196 */
Daniel Lezcanoadbaf522016-06-06 19:11:12 +0200197static int __init atmel_st_timer_init(struct device_node *node)
SAN People73a59c12006-01-09 17:05:41 +0000198{
Alexandre Belloni216ab8f2015-08-16 11:23:44 +0200199 struct clk *sclk;
200 unsigned int sclk_rate, val;
Alexandre Belloni0afb46b2015-03-13 11:54:37 +0100201 int irq, ret;
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100202
203 regmap_st = syscon_node_to_regmap(node);
Daniel Lezcanoadbaf522016-06-06 19:11:12 +0200204 if (IS_ERR(regmap_st)) {
205 pr_err("Unable to get regmap\n");
206 return PTR_ERR(regmap_st);
207 }
Joachim Eastwood454c46d2012-10-28 18:31:07 +0000208
David Brownell5e802df2007-07-31 01:41:26 +0100209 /* Disable all timer interrupts, and clear any pending ones */
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100210 regmap_write(regmap_st, AT91_ST_IDR,
David Brownell5e802df2007-07-31 01:41:26 +0100211 AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100212 regmap_read(regmap_st, AT91_ST_SR, &val);
213
214 /* Get the interrupts property */
Alexandre Belloni0afb46b2015-03-13 11:54:37 +0100215 irq = irq_of_parse_and_map(node, 0);
Daniel Lezcanoadbaf522016-06-06 19:11:12 +0200216 if (!irq) {
217 pr_err("Unable to get IRQ from DT\n");
218 return -EINVAL;
219 }
SAN People73a59c12006-01-09 17:05:41 +0000220
Andrew Victor2a6f9902006-06-19 15:26:50 +0100221 /* Make IRQs happen for the system timer */
Alexandre Belloni0afb46b2015-03-13 11:54:37 +0100222 ret = request_irq(irq, at91rm9200_timer_interrupt,
223 IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
224 "at91_tick", regmap_st);
Daniel Lezcanoadbaf522016-06-06 19:11:12 +0200225 if (ret) {
226 pr_err("Unable to setup IRQ\n");
227 return ret;
228 }
SAN People73a59c12006-01-09 17:05:41 +0000229
Alexandre Belloni216ab8f2015-08-16 11:23:44 +0200230 sclk = of_clk_get(node, 0);
Daniel Lezcanoadbaf522016-06-06 19:11:12 +0200231 if (IS_ERR(sclk)) {
232 pr_err("Unable to get slow clock\n");
233 return PTR_ERR(sclk);
234 }
Alexandre Belloni216ab8f2015-08-16 11:23:44 +0200235
Daniel Lezcanoadbaf522016-06-06 19:11:12 +0200236 ret = clk_prepare_enable(sclk);
237 if (ret) {
238 pr_err("Could not enable slow clock\n");
239 return ret;
240 }
Alexandre Belloni216ab8f2015-08-16 11:23:44 +0200241
242 sclk_rate = clk_get_rate(sclk);
Daniel Lezcanoadbaf522016-06-06 19:11:12 +0200243 if (!sclk_rate) {
244 pr_err("Invalid slow clock rate\n");
245 return -EINVAL;
246 }
Alexandre Belloni216ab8f2015-08-16 11:23:44 +0200247 timer_latch = (sclk_rate + HZ / 2) / HZ;
248
David Brownell5e802df2007-07-31 01:41:26 +0100249 /* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used
250 * directly for the clocksource and all clockevents, after adjusting
251 * its prescaler from the 1 Hz default.
252 */
Alexandre Belloniadf2edf2015-03-12 13:07:32 +0100253 regmap_write(regmap_st, AT91_ST_RTMR, 1);
SAN People73a59c12006-01-09 17:05:41 +0000254
David Brownell5e802df2007-07-31 01:41:26 +0100255 /* Setup timer clockevent, with minimum of two ticks (important!!) */
Rusty Russell320ab2b2008-12-13 21:20:26 +1030256 clkevt.cpumask = cpumask_of(0);
Alexandre Belloni216ab8f2015-08-16 11:23:44 +0200257 clockevents_config_and_register(&clkevt, sclk_rate,
Uwe Kleine-König1c283532013-10-08 16:38:53 +0200258 2, AT91_ST_ALMV);
SAN People73a59c12006-01-09 17:05:41 +0000259
David Brownell5e802df2007-07-31 01:41:26 +0100260 /* register clocksource */
Daniel Lezcanoadbaf522016-06-06 19:11:12 +0200261 return clocksource_register_hz(&clk32k, sclk_rate);
Andrew Victor2a6f9902006-06-19 15:26:50 +0100262}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200263CLOCKSOURCE_OF_DECLARE(atmel_st_timer, "atmel,at91rm9200-st",
Alexandre Bellonibbfc97e2015-03-12 13:07:30 +0100264 atmel_st_timer_init);