blob: 9abb289dce72996afd9e3402278589971868dd74 [file] [log] [blame]
Andrew Victor1a0ed732006-12-01 09:04:47 +01001/*
Andrew Victorad48ce72008-04-16 20:43:49 +01002 * at91sam926x_time.c - Periodic Interval Timer (PIT) for at91sam926x
Andrew Victor1a0ed732006-12-01 09:04:47 +01003 *
4 * Copyright (C) 2005-2006 M. Amine SAYA, ATMEL Rousset, France
5 * Revision 2005 M. Nicolas Diremdjian, ATMEL Rousset, France
Andrew Victorad48ce72008-04-16 20:43:49 +01006 * Converted to ClockSource/ClockEvents by David Brownell.
Andrew Victor1a0ed732006-12-01 09:04:47 +01007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
Maxime Ripard52c3ffb2014-07-01 11:33:14 +020012
Maxime Ripardcffbfe62014-07-01 11:33:21 +020013#define pr_fmt(fmt) "AT91: PIT: " fmt
14
Maxime Ripard52c3ffb2014-07-01 11:33:14 +020015#include <linux/clk.h>
16#include <linux/clockchips.h>
Andrew Victor1a0ed732006-12-01 09:04:47 +010017#include <linux/interrupt.h>
18#include <linux/irq.h>
19#include <linux/kernel.h>
Jean-Christophe PLAGNIOL-VILLARD23fa6482012-02-27 11:19:34 +010020#include <linux/of.h>
21#include <linux/of_address.h>
22#include <linux/of_irq.h>
Maxime Ripard64568d12014-07-01 11:33:23 +020023#include <linux/slab.h>
Andrew Victor1a0ed732006-12-01 09:04:47 +010024
Uwe Kleine-Königac11a1d2013-11-14 10:49:19 +010025#include <mach/hardware.h>
Andrew Victor1a0ed732006-12-01 09:04:47 +010026
Jean-Christophe PLAGNIOL-VILLARDffe5cd82012-10-30 08:09:09 +080027#define AT91_PIT_MR 0x00 /* Mode Register */
Maxime Ripard52c3ffb2014-07-01 11:33:14 +020028#define AT91_PIT_PITIEN BIT(25) /* Timer Interrupt Enable */
29#define AT91_PIT_PITEN BIT(24) /* Timer Enabled */
30#define AT91_PIT_PIV GENMASK(19, 0) /* Periodic Interval Value */
Andrew Victor1a0ed732006-12-01 09:04:47 +010031
Jean-Christophe PLAGNIOL-VILLARDffe5cd82012-10-30 08:09:09 +080032#define AT91_PIT_SR 0x04 /* Status Register */
Maxime Ripard52c3ffb2014-07-01 11:33:14 +020033#define AT91_PIT_PITS BIT(0) /* Timer Status */
Jean-Christophe PLAGNIOL-VILLARDffe5cd82012-10-30 08:09:09 +080034
35#define AT91_PIT_PIVR 0x08 /* Periodic Interval Value Register */
36#define AT91_PIT_PIIR 0x0c /* Periodic Interval Image Register */
Maxime Ripard52c3ffb2014-07-01 11:33:14 +020037#define AT91_PIT_PICNT GENMASK(31, 20) /* Interval Counter */
38#define AT91_PIT_CPIV GENMASK(19, 0) /* Inverval Value */
Andrew Victor1a0ed732006-12-01 09:04:47 +010039
40#define PIT_CPIV(x) ((x) & AT91_PIT_CPIV)
41#define PIT_PICNT(x) (((x) & AT91_PIT_PICNT) >> 20)
42
Maxime Ripard64568d12014-07-01 11:33:23 +020043struct pit_data {
44 struct clock_event_device clkevt;
45 struct clocksource clksrc;
Andrew Victorad48ce72008-04-16 20:43:49 +010046
Maxime Ripard64568d12014-07-01 11:33:23 +020047 void __iomem *base;
48 u32 cycle;
49 u32 cnt;
50 unsigned int irq;
51 struct clk *mck;
52};
53
54static inline struct pit_data *clksrc_to_pit_data(struct clocksource *clksrc)
Jean-Christophe PLAGNIOL-VILLARD4ab0c5992011-09-18 22:29:50 +080055{
Maxime Ripard64568d12014-07-01 11:33:23 +020056 return container_of(clksrc, struct pit_data, clksrc);
Jean-Christophe PLAGNIOL-VILLARD4ab0c5992011-09-18 22:29:50 +080057}
58
Maxime Ripard64568d12014-07-01 11:33:23 +020059static inline struct pit_data *clkevt_to_pit_data(struct clock_event_device *clkevt)
Jean-Christophe PLAGNIOL-VILLARD4ab0c5992011-09-18 22:29:50 +080060{
Maxime Ripard64568d12014-07-01 11:33:23 +020061 return container_of(clkevt, struct pit_data, clkevt);
62}
63
64static inline unsigned int pit_read(void __iomem *base, unsigned int reg_offset)
65{
66 return __raw_readl(base + reg_offset);
67}
68
69static inline void pit_write(void __iomem *base, unsigned int reg_offset, unsigned long value)
70{
71 __raw_writel(value, base + reg_offset);
Jean-Christophe PLAGNIOL-VILLARD4ab0c5992011-09-18 22:29:50 +080072}
Andrew Victorad48ce72008-04-16 20:43:49 +010073
Andrew Victor1a0ed732006-12-01 09:04:47 +010074/*
Andrew Victorad48ce72008-04-16 20:43:49 +010075 * Clocksource: just a monotonic counter of MCK/16 cycles.
76 * We don't care whether or not PIT irqs are enabled.
Andrew Victor1a0ed732006-12-01 09:04:47 +010077 */
Magnus Damm8e196082009-04-21 12:24:00 -070078static cycle_t read_pit_clk(struct clocksource *cs)
Andrew Victor1a0ed732006-12-01 09:04:47 +010079{
Maxime Ripard64568d12014-07-01 11:33:23 +020080 struct pit_data *data = clksrc_to_pit_data(cs);
Andrew Victorad48ce72008-04-16 20:43:49 +010081 unsigned long flags;
82 u32 elapsed;
83 u32 t;
Andrew Victor1a0ed732006-12-01 09:04:47 +010084
Andrew Victorad48ce72008-04-16 20:43:49 +010085 raw_local_irq_save(flags);
Maxime Ripard64568d12014-07-01 11:33:23 +020086 elapsed = data->cnt;
87 t = pit_read(data->base, AT91_PIT_PIIR);
Andrew Victorad48ce72008-04-16 20:43:49 +010088 raw_local_irq_restore(flags);
Andrew Victor1a0ed732006-12-01 09:04:47 +010089
Maxime Ripard64568d12014-07-01 11:33:23 +020090 elapsed += PIT_PICNT(t) * data->cycle;
Andrew Victorad48ce72008-04-16 20:43:49 +010091 elapsed += PIT_CPIV(t);
92 return elapsed;
Andrew Victor1a0ed732006-12-01 09:04:47 +010093}
94
Andrew Victorad48ce72008-04-16 20:43:49 +010095/*
96 * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
97 */
98static void
99pit_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev)
100{
Maxime Ripard64568d12014-07-01 11:33:23 +0200101 struct pit_data *data = clkevt_to_pit_data(dev);
102
Andrew Victorad48ce72008-04-16 20:43:49 +0100103 switch (mode) {
104 case CLOCK_EVT_MODE_PERIODIC:
Uwe Kleine-König501d7032009-09-21 09:30:09 +0200105 /* update clocksource counter */
Maxime Ripard64568d12014-07-01 11:33:23 +0200106 data->cnt += data->cycle * PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
107 pit_write(data->base, AT91_PIT_MR,
108 (data->cycle - 1) | AT91_PIT_PITEN | AT91_PIT_PITIEN);
Andrew Victorad48ce72008-04-16 20:43:49 +0100109 break;
110 case CLOCK_EVT_MODE_ONESHOT:
111 BUG();
112 /* FALLTHROUGH */
113 case CLOCK_EVT_MODE_SHUTDOWN:
114 case CLOCK_EVT_MODE_UNUSED:
115 /* disable irq, leaving the clocksource active */
Maxime Ripard64568d12014-07-01 11:33:23 +0200116 pit_write(data->base, AT91_PIT_MR,
117 (data->cycle - 1) | AT91_PIT_PITEN);
Andrew Victorad48ce72008-04-16 20:43:49 +0100118 break;
119 case CLOCK_EVT_MODE_RESUME:
120 break;
121 }
122}
123
Stephen Warren49356ae2012-11-07 16:32:41 -0700124static void at91sam926x_pit_suspend(struct clock_event_device *cedev)
125{
Maxime Ripard64568d12014-07-01 11:33:23 +0200126 struct pit_data *data = clkevt_to_pit_data(cedev);
127
Stephen Warren49356ae2012-11-07 16:32:41 -0700128 /* Disable timer */
Maxime Ripard64568d12014-07-01 11:33:23 +0200129 pit_write(data->base, AT91_PIT_MR, 0);
Stephen Warren49356ae2012-11-07 16:32:41 -0700130}
131
Maxime Ripard64568d12014-07-01 11:33:23 +0200132static void at91sam926x_pit_reset(struct pit_data *data)
Stephen Warren49356ae2012-11-07 16:32:41 -0700133{
134 /* Disable timer and irqs */
Maxime Ripard64568d12014-07-01 11:33:23 +0200135 pit_write(data->base, AT91_PIT_MR, 0);
Stephen Warren49356ae2012-11-07 16:32:41 -0700136
137 /* Clear any pending interrupts, wait for PIT to stop counting */
Maxime Ripard64568d12014-07-01 11:33:23 +0200138 while (PIT_CPIV(pit_read(data->base, AT91_PIT_PIVR)) != 0)
Stephen Warren49356ae2012-11-07 16:32:41 -0700139 cpu_relax();
140
141 /* Start PIT but don't enable IRQ */
Maxime Ripard64568d12014-07-01 11:33:23 +0200142 pit_write(data->base, AT91_PIT_MR,
143 (data->cycle - 1) | AT91_PIT_PITEN);
Stephen Warren49356ae2012-11-07 16:32:41 -0700144}
145
146static void at91sam926x_pit_resume(struct clock_event_device *cedev)
147{
Maxime Ripard64568d12014-07-01 11:33:23 +0200148 struct pit_data *data = clkevt_to_pit_data(cedev);
149
150 at91sam926x_pit_reset(data);
Stephen Warren49356ae2012-11-07 16:32:41 -0700151}
152
Andrew Victor1a0ed732006-12-01 09:04:47 +0100153/*
154 * IRQ handler for the timer.
155 */
Andrew Victorad48ce72008-04-16 20:43:49 +0100156static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id)
Andrew Victor1a0ed732006-12-01 09:04:47 +0100157{
Maxime Ripard64568d12014-07-01 11:33:23 +0200158 struct pit_data *data = dev_id;
159
Uwe Kleine-König501d7032009-09-21 09:30:09 +0200160 /*
161 * irqs should be disabled here, but as the irq is shared they are only
162 * guaranteed to be off if the timer irq is registered first.
163 */
164 WARN_ON_ONCE(!irqs_disabled());
Andrew Victor1a0ed732006-12-01 09:04:47 +0100165
Andrew Victorad48ce72008-04-16 20:43:49 +0100166 /* The PIT interrupt may be disabled, and is shared */
Maxime Ripard64568d12014-07-01 11:33:23 +0200167 if ((data->clkevt.mode == CLOCK_EVT_MODE_PERIODIC) &&
168 (pit_read(data->base, AT91_PIT_SR) & AT91_PIT_PITS)) {
Andrew Victorad48ce72008-04-16 20:43:49 +0100169 unsigned nr_ticks;
170
171 /* Get number of ticks performed before irq, and ack it */
Maxime Ripard64568d12014-07-01 11:33:23 +0200172 nr_ticks = PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
Andrew Victor1a0ed732006-12-01 09:04:47 +0100173 do {
Maxime Ripard64568d12014-07-01 11:33:23 +0200174 data->cnt += data->cycle;
175 data->clkevt.event_handler(&data->clkevt);
Andrew Victor1a0ed732006-12-01 09:04:47 +0100176 nr_ticks--;
177 } while (nr_ticks);
178
Andrew Victor1a0ed732006-12-01 09:04:47 +0100179 return IRQ_HANDLED;
Andrew Victorad48ce72008-04-16 20:43:49 +0100180 }
181
182 return IRQ_NONE;
Andrew Victor1a0ed732006-12-01 09:04:47 +0100183}
184
Andrew Victor1a0ed732006-12-01 09:04:47 +0100185/*
Andrew Victorad48ce72008-04-16 20:43:49 +0100186 * Set up both clocksource and clockevent support.
Andrew Victor1a0ed732006-12-01 09:04:47 +0100187 */
Maxime Ripard64568d12014-07-01 11:33:23 +0200188static void __init at91sam926x_pit_common_init(struct pit_data *data)
Andrew Victor1a0ed732006-12-01 09:04:47 +0100189{
Andrew Victorad48ce72008-04-16 20:43:49 +0100190 unsigned long pit_rate;
191 unsigned bits;
Nicolas Ferre986c2652012-02-17 11:54:29 +0100192 int ret;
Andrew Victor1a0ed732006-12-01 09:04:47 +0100193
Andrew Victorad48ce72008-04-16 20:43:49 +0100194 /*
195 * Use our actual MCK to figure out how many MCK/16 ticks per
196 * 1/HZ period (instead of a compile-time constant LATCH).
197 */
Maxime Ripard64568d12014-07-01 11:33:23 +0200198 pit_rate = clk_get_rate(data->mck) / 16;
199 data->cycle = DIV_ROUND_CLOSEST(pit_rate, HZ);
200 WARN_ON(((data->cycle - 1) & ~AT91_PIT_PIV) != 0);
Andrew Victorad48ce72008-04-16 20:43:49 +0100201
202 /* Initialize and enable the timer */
Maxime Ripard64568d12014-07-01 11:33:23 +0200203 at91sam926x_pit_reset(data);
Andrew Victorad48ce72008-04-16 20:43:49 +0100204
205 /*
206 * Register clocksource. The high order bits of PIV are unused,
207 * so this isn't a 32-bit counter unless we get clockevent irqs.
208 */
Maxime Ripard64568d12014-07-01 11:33:23 +0200209 bits = 12 /* PICNT */ + ilog2(data->cycle) /* PIV */;
210 data->clksrc.mask = CLOCKSOURCE_MASK(bits);
211 data->clksrc.name = "pit";
212 data->clksrc.rating = 175;
213 data->clksrc.read = read_pit_clk,
214 data->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS,
215 clocksource_register_hz(&data->clksrc, pit_rate);
Andrew Victorad48ce72008-04-16 20:43:49 +0100216
217 /* Set up irq handler */
Maxime Ripard64568d12014-07-01 11:33:23 +0200218 ret = request_irq(data->irq, at91sam926x_pit_interrupt,
Maxime Ripard7f282e02014-07-01 11:33:22 +0200219 IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
Maxime Ripard64568d12014-07-01 11:33:23 +0200220 "at91_tick", data);
Nicolas Ferre986c2652012-02-17 11:54:29 +0100221 if (ret)
Maxime Ripardcffbfe62014-07-01 11:33:21 +0200222 panic(pr_fmt("Unable to setup IRQ\n"));
Andrew Victorad48ce72008-04-16 20:43:49 +0100223
224 /* Set up and register clockevents */
Maxime Ripard64568d12014-07-01 11:33:23 +0200225 data->clkevt.name = "pit";
226 data->clkevt.features = CLOCK_EVT_FEAT_PERIODIC;
227 data->clkevt.shift = 32;
228 data->clkevt.mult = div_sc(pit_rate, NSEC_PER_SEC, data->clkevt.shift);
229 data->clkevt.rating = 100;
230 data->clkevt.cpumask = cpumask_of(0);
231
232 data->clkevt.set_mode = pit_clkevt_mode;
233 data->clkevt.resume = at91sam926x_pit_resume;
234 data->clkevt.suspend = at91sam926x_pit_suspend;
235 clockevents_register_device(&data->clkevt);
Andrew Victor1a0ed732006-12-01 09:04:47 +0100236}
237
Maxime Ripardf807a892014-07-01 11:33:18 +0200238static void __init at91sam926x_pit_dt_init(struct device_node *node)
239{
Maxime Ripard64568d12014-07-01 11:33:23 +0200240 struct pit_data *data;
Maxime Ripardf807a892014-07-01 11:33:18 +0200241
Maxime Ripard64568d12014-07-01 11:33:23 +0200242 data = kzalloc(sizeof(*data), GFP_KERNEL);
243 if (!data)
244 panic(pr_fmt("Unable to allocate memory\n"));
245
246 data->base = of_iomap(node, 0);
247 if (!data->base)
Maxime Ripardcffbfe62014-07-01 11:33:21 +0200248 panic(pr_fmt("Could not map PIT address\n"));
Maxime Ripardf807a892014-07-01 11:33:18 +0200249
Maxime Ripard64568d12014-07-01 11:33:23 +0200250 data->mck = of_clk_get(node, 0);
251 if (IS_ERR(data->mck))
Maxime Ripardf807a892014-07-01 11:33:18 +0200252 /* Fallback on clkdev for !CCF-based boards */
Maxime Ripard64568d12014-07-01 11:33:23 +0200253 data->mck = clk_get(NULL, "mck");
Maxime Ripardf807a892014-07-01 11:33:18 +0200254
Maxime Ripard64568d12014-07-01 11:33:23 +0200255 if (IS_ERR(data->mck))
Maxime Ripardcffbfe62014-07-01 11:33:21 +0200256 panic(pr_fmt("Unable to get mck clk\n"));
Maxime Ripardf807a892014-07-01 11:33:18 +0200257
258 /* Get the interrupts property */
Maxime Ripard64568d12014-07-01 11:33:23 +0200259 data->irq = irq_of_parse_and_map(node, 0);
260 if (!data->irq)
Maxime Ripardcffbfe62014-07-01 11:33:21 +0200261 panic(pr_fmt("Unable to get IRQ from DT\n"));
Maxime Ripardf807a892014-07-01 11:33:18 +0200262
Maxime Ripard64568d12014-07-01 11:33:23 +0200263 at91sam926x_pit_common_init(data);
Maxime Ripardf807a892014-07-01 11:33:18 +0200264}
265CLOCKSOURCE_OF_DECLARE(at91sam926x_pit, "atmel,at91sam9260-pit",
266 at91sam926x_pit_dt_init);
267
Maxime Ripard64568d12014-07-01 11:33:23 +0200268static void __iomem *pit_base_addr;
269
Maxime Ripardf807a892014-07-01 11:33:18 +0200270void __init at91sam926x_pit_init(void)
271{
Maxime Ripard64568d12014-07-01 11:33:23 +0200272 struct pit_data *data;
273
274 data = kzalloc(sizeof(*data), GFP_KERNEL);
275 if (!data)
276 panic(pr_fmt("Unable to allocate memory\n"));
277
278 data->base = pit_base_addr;
279
280 data->mck = clk_get(NULL, "mck");
281 if (IS_ERR(data->mck))
Maxime Ripardcffbfe62014-07-01 11:33:21 +0200282 panic(pr_fmt("Unable to get mck clk\n"));
Maxime Ripardf807a892014-07-01 11:33:18 +0200283
Maxime Ripard64568d12014-07-01 11:33:23 +0200284 data->irq = NR_IRQS_LEGACY + AT91_ID_SYS;
285
286 at91sam926x_pit_common_init(data);
Maxime Ripardf807a892014-07-01 11:33:18 +0200287}
288
Jean-Christophe PLAGNIOL-VILLARD4ab0c5992011-09-18 22:29:50 +0800289void __init at91sam926x_ioremap_pit(u32 addr)
290{
Maxime Riparda7d84d72014-07-01 11:33:17 +0200291 if (of_have_populated_dt())
Jean-Christophe PLAGNIOL-VILLARD23fa6482012-02-27 11:19:34 +0100292 return;
Maxime Riparda7d84d72014-07-01 11:33:17 +0200293
Jean-Christophe PLAGNIOL-VILLARD4ab0c5992011-09-18 22:29:50 +0800294 pit_base_addr = ioremap(addr, 16);
295
296 if (!pit_base_addr)
Maxime Ripardcffbfe62014-07-01 11:33:21 +0200297 panic(pr_fmt("Impossible to ioremap PIT\n"));
Andrew Victor1a0ed732006-12-01 09:04:47 +0100298}