blob: a10fa667325f0b7e14e35487711456e6c228dea9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/arm/mach-pxa/time.c
3 *
Bill Gatliff7bbb18c2007-07-21 03:39:36 +01004 * PXA clocksource, clockevents, and OST interrupt handlers.
5 * Copyright (c) 2007 by Bill Gatliff <bgat@billgatliff.com>.
6 *
7 * Derived from Nicolas Pitre's PXA timer handler Copyright (c) 2001
8 * by MontaVista Software, Inc. (Nico, your code rocks!)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/interrupt.h>
Robert Jarzmikab5354c2014-07-14 18:52:02 +020018#include <linux/clk.h>
Bill Gatliff7bbb18c2007-07-21 03:39:36 +010019#include <linux/clockchips.h>
Robert Jarzmikab5354c2014-07-14 18:52:02 +020020#include <linux/of_address.h>
21#include <linux/of_irq.h>
Ingo Molnare6017572017-02-01 16:36:40 +010022#include <linux/sched/clock.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070023#include <linux/sched_clock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Baoyou Xieaa8c0f1a2016-08-23 23:19:29 +080025#include <clocksource/pxa.h>
26
Nicolas Pitre6c3a1582007-08-17 16:55:22 +010027#include <asm/div64.h>
Robert Jarzmikab5354c2014-07-14 18:52:02 +020028
29#define OSMR0 0x00 /* OS Timer 0 Match Register */
30#define OSMR1 0x04 /* OS Timer 1 Match Register */
31#define OSMR2 0x08 /* OS Timer 2 Match Register */
32#define OSMR3 0x0C /* OS Timer 3 Match Register */
33
34#define OSCR 0x10 /* OS Timer Counter Register */
35#define OSSR 0x14 /* OS Timer Status Register */
36#define OWER 0x18 /* OS Timer Watchdog Enable Register */
37#define OIER 0x1C /* OS Timer Interrupt Enable Register */
38
39#define OSSR_M3 (1 << 3) /* Match status channel 3 */
40#define OSSR_M2 (1 << 2) /* Match status channel 2 */
41#define OSSR_M1 (1 << 1) /* Match status channel 1 */
42#define OSSR_M0 (1 << 0) /* Match status channel 0 */
43
44#define OIER_E0 (1 << 0) /* Interrupt enable channel 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Nicolas Pitre6c3a1582007-08-17 16:55:22 +010046/*
47 * This is PXA's sched_clock implementation. This has a resolution
48 * of at least 308 ns and a maximum value of 208 days.
49 *
50 * The return value is guaranteed to be monotonic in that range as
51 * long as there is always less than 582 seconds between successive
52 * calls to sched_clock() which should always be the case in practice.
53 */
Nicolas Pitre6c3a1582007-08-17 16:55:22 +010054
Robert Jarzmikab5354c2014-07-14 18:52:02 +020055#define timer_readl(reg) readl_relaxed(timer_base + (reg))
56#define timer_writel(val, reg) writel_relaxed((val), timer_base + (reg))
57
58static void __iomem *timer_base;
59
Stephen Boyd364ed1e2013-11-15 15:26:19 -080060static u64 notrace pxa_read_sched_clock(void)
Nicolas Pitre6c3a1582007-08-17 16:55:22 +010061{
Robert Jarzmikab5354c2014-07-14 18:52:02 +020062 return timer_readl(OSCR);
Nicolas Pitre6c3a1582007-08-17 16:55:22 +010063}
64
65
Russell Kinga88264c2007-11-12 22:45:16 +000066#define MIN_OSCR_DELTA 16
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static irqreturn_t
Bill Gatliff7bbb18c2007-07-21 03:39:36 +010069pxa_ost0_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Bill Gatliff7bbb18c2007-07-21 03:39:36 +010071 struct clock_event_device *c = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Russell Kinga88264c2007-11-12 22:45:16 +000073 /* Disarm the compare/match, signal the event. */
Robert Jarzmikab5354c2014-07-14 18:52:02 +020074 timer_writel(timer_readl(OIER) & ~OIER_E0, OIER);
75 timer_writel(OSSR_M0, OSSR);
Russell Kinga88264c2007-11-12 22:45:16 +000076 c->event_handler(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 return IRQ_HANDLED;
79}
80
Bill Gatliff7bbb18c2007-07-21 03:39:36 +010081static int
82pxa_osmr0_set_next_event(unsigned long delta, struct clock_event_device *dev)
83{
Uwe Kleine-Königa602f0f2009-12-17 12:43:29 +010084 unsigned long next, oscr;
Bill Gatliff7bbb18c2007-07-21 03:39:36 +010085
Robert Jarzmikab5354c2014-07-14 18:52:02 +020086 timer_writel(timer_readl(OIER) | OIER_E0, OIER);
87 next = timer_readl(OSCR) + delta;
88 timer_writel(next, OSMR0);
89 oscr = timer_readl(OSCR);
Russell King91bc51d2007-11-08 23:35:46 +000090
91 return (signed)(next - oscr) <= MIN_OSCR_DELTA ? -ETIME : 0;
Bill Gatliff7bbb18c2007-07-21 03:39:36 +010092}
93
Viresh Kumar47d490e2015-06-18 16:24:30 +053094static int pxa_osmr0_shutdown(struct clock_event_device *evt)
Bill Gatliff7bbb18c2007-07-21 03:39:36 +010095{
Viresh Kumar47d490e2015-06-18 16:24:30 +053096 /* initializing, released, or preparing for suspend */
97 timer_writel(timer_readl(OIER) & ~OIER_E0, OIER);
98 timer_writel(OSSR_M0, OSSR);
99 return 0;
Bill Gatliff7bbb18c2007-07-21 03:39:36 +0100100}
101
Stephen Warren5b30d5b2012-11-07 16:34:13 -0700102#ifdef CONFIG_PM
103static unsigned long osmr[4], oier, oscr;
104
105static void pxa_timer_suspend(struct clock_event_device *cedev)
106{
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200107 osmr[0] = timer_readl(OSMR0);
108 osmr[1] = timer_readl(OSMR1);
109 osmr[2] = timer_readl(OSMR2);
110 osmr[3] = timer_readl(OSMR3);
111 oier = timer_readl(OIER);
112 oscr = timer_readl(OSCR);
Stephen Warren5b30d5b2012-11-07 16:34:13 -0700113}
114
115static void pxa_timer_resume(struct clock_event_device *cedev)
116{
117 /*
118 * Ensure that we have at least MIN_OSCR_DELTA between match
119 * register 0 and the OSCR, to guarantee that we will receive
120 * the one-shot timer interrupt. We adjust OSMR0 in preference
121 * to OSCR to guarantee that OSCR is monotonically incrementing.
122 */
123 if (osmr[0] - oscr < MIN_OSCR_DELTA)
124 osmr[0] += MIN_OSCR_DELTA;
125
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200126 timer_writel(osmr[0], OSMR0);
127 timer_writel(osmr[1], OSMR1);
128 timer_writel(osmr[2], OSMR2);
129 timer_writel(osmr[3], OSMR3);
130 timer_writel(oier, OIER);
131 timer_writel(oscr, OSCR);
Stephen Warren5b30d5b2012-11-07 16:34:13 -0700132}
133#else
134#define pxa_timer_suspend NULL
135#define pxa_timer_resume NULL
136#endif
137
Bill Gatliff7bbb18c2007-07-21 03:39:36 +0100138static struct clock_event_device ckevt_pxa_osmr0 = {
Viresh Kumar47d490e2015-06-18 16:24:30 +0530139 .name = "osmr0",
140 .features = CLOCK_EVT_FEAT_ONESHOT,
141 .rating = 200,
142 .set_next_event = pxa_osmr0_set_next_event,
143 .set_state_shutdown = pxa_osmr0_shutdown,
144 .set_state_oneshot = pxa_osmr0_shutdown,
145 .suspend = pxa_timer_suspend,
146 .resume = pxa_timer_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
Bill Gatliff7bbb18c2007-07-21 03:39:36 +0100149static struct irqaction pxa_ost0_irq = {
150 .name = "ost0",
Michael Opdenackered7936f2013-12-09 11:22:22 +0100151 .flags = IRQF_TIMER | IRQF_IRQPOLL,
Bill Gatliff7bbb18c2007-07-21 03:39:36 +0100152 .handler = pxa_ost0_interrupt,
153 .dev_id = &ckevt_pxa_osmr0,
154};
155
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200156static int __init pxa_timer_common_init(int irq, unsigned long clock_tick_rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200158 int ret;
159
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200160 timer_writel(0, OIER);
161 timer_writel(OSSR_M0 | OSSR_M1 | OSSR_M2 | OSSR_M3, OSSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Stephen Boyd364ed1e2013-11-15 15:26:19 -0800163 sched_clock_register(pxa_read_sched_clock, 32, clock_tick_rate);
Nicolas Pitre6c3a1582007-08-17 16:55:22 +0100164
Rusty Russell320ab2b2008-12-13 21:20:26 +1030165 ckevt_pxa_osmr0.cpumask = cpumask_of(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200167 ret = setup_irq(irq, &pxa_ost0_irq);
168 if (ret) {
Rafał Miłeckiac9ce6d2017-03-09 10:47:10 +0100169 pr_err("Failed to setup irq\n");
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200170 return ret;
171 }
Bill Gatliff7bbb18c2007-07-21 03:39:36 +0100172
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200173 ret = clocksource_mmio_init(timer_base + OSCR, "oscr0", clock_tick_rate, 200,
174 32, clocksource_mmio_readl_up);
175 if (ret) {
Rafał Miłeckiac9ce6d2017-03-09 10:47:10 +0100176 pr_err("Failed to init clocksource\n");
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200177 return ret;
178 }
179
Olof Johansson8d849812013-01-14 10:20:02 -0800180 clockevents_config_and_register(&ckevt_pxa_osmr0, clock_tick_rate,
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200181 MIN_OSCR_DELTA * 2, 0x7fffffff);
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200182
183 return 0;
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200184}
185
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200186static int __init pxa_timer_dt_init(struct device_node *np)
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200187{
188 struct clk *clk;
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200189 int irq, ret;
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200190
191 /* timer registers are shared with watchdog timer */
192 timer_base = of_iomap(np, 0);
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200193 if (!timer_base) {
194 pr_err("%s: unable to map resource\n", np->name);
195 return -ENXIO;
196 }
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200197
198 clk = of_clk_get(np, 0);
199 if (IS_ERR(clk)) {
200 pr_crit("%s: unable to get clk\n", np->name);
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200201 return PTR_ERR(clk);
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200202 }
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200203
204 ret = clk_prepare_enable(clk);
205 if (ret) {
Rafał Miłeckiac9ce6d2017-03-09 10:47:10 +0100206 pr_crit("Failed to prepare clock\n");
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200207 return ret;
208 }
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200209
210 /* we are only interested in OS-timer0 irq */
211 irq = irq_of_parse_and_map(np, 0);
212 if (irq <= 0) {
213 pr_crit("%s: unable to parse OS-timer0 irq\n", np->name);
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200214 return -EINVAL;
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200215 }
216
Daniel Lezcanobe3aff82016-06-06 17:58:27 +0200217 return pxa_timer_common_init(irq, clk_get_rate(clk));
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200218}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200219CLOCKSOURCE_OF_DECLARE(pxa_timer, "marvell,pxa-timer", pxa_timer_dt_init);
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200220
221/*
222 * Legacy timer init for non device-tree boards.
223 */
Robert Jarzmikf4e14ed2016-09-19 21:12:13 +0200224void __init pxa_timer_nodt_init(int irq, void __iomem *base)
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200225{
226 struct clk *clk;
227
228 timer_base = base;
229 clk = clk_get(NULL, "OSTIMER0");
Robert Jarzmikf4e14ed2016-09-19 21:12:13 +0200230 if (clk && !IS_ERR(clk)) {
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200231 clk_prepare_enable(clk);
Robert Jarzmikf4e14ed2016-09-19 21:12:13 +0200232 pxa_timer_common_init(irq, clk_get_rate(clk));
233 } else {
Robert Jarzmikab5354c2014-07-14 18:52:02 +0200234 pr_crit("%s: unable to get clk\n", __func__);
Robert Jarzmikf4e14ed2016-09-19 21:12:13 +0200235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}