blob: 51c05292d145cb3f7de2b2f96116d83cbc23475f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/mach-sa1100/time.c
3 *
4 * Copyright (C) 1998 Deborah Wallach.
Kristoffer Ericson93982532008-11-26 20:58:43 +01005 * Twiddles (C) 1999 Hugo Fiennes <hugo@empeg.com>
6 *
Nicolas Pitre2f82af02009-09-14 03:25:28 -04007 * 2000/03/29 (C) Nicolas Pitre <nico@fluxnic.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Rewritten: big cleanup, much simpler, better HZ accuracy.
9 *
10 */
11#include <linux/init.h>
12#include <linux/errno.h>
13#include <linux/interrupt.h>
Thomas Gleixner119c6412006-07-01 22:32:38 +010014#include <linux/irq.h>
Russell King5094b922010-12-15 21:49:06 +000015#include <linux/sched.h> /* just for sched_clock() - funny that */
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/timex.h>
Russell King3e238be2008-04-14 23:03:10 +010017#include <linux/clockchips.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/mach/time.h>
Russell King5094b922010-12-15 21:49:06 +000020#include <asm/sched_clock.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010021#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Russell King5094b922010-12-15 21:49:06 +000023/*
24 * This is the SA11x0 sched_clock implementation.
25 */
26static DEFINE_CLOCK_DATA(cd);
27
28/*
29 * Constants generated by clocks_calc_mult_shift(m, s, 3.6864MHz,
30 * NSEC_PER_SEC, 60).
31 * This gives a resolution of about 271ns and a wrap period of about 19min.
32 */
33#define SC_MULT 2275555556u
34#define SC_SHIFT 23
35
36unsigned long long notrace sched_clock(void)
37{
38 u32 cyc = OSCR;
39 return cyc_to_fixed_sched_clock(&cd, cyc, (u32)~0, SC_MULT, SC_SHIFT);
40}
41
42static void notrace sa1100_update_sched_clock(void)
43{
44 u32 cyc = OSCR;
45 update_sched_clock(&cd, cyc, (u32)~0);
46}
47
Russell King3e238be2008-04-14 23:03:10 +010048#define MIN_OSCR_DELTA 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Russell King3e238be2008-04-14 23:03:10 +010050static irqreturn_t sa1100_ost0_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Russell King3e238be2008-04-14 23:03:10 +010052 struct clock_event_device *c = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Russell King3e238be2008-04-14 23:03:10 +010054 /* Disarm the compare/match, signal the event. */
55 OIER &= ~OIER_E0;
56 OSSR = OSSR_M0;
57 c->event_handler(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return IRQ_HANDLED;
60}
61
Russell King3e238be2008-04-14 23:03:10 +010062static int
63sa1100_osmr0_set_next_event(unsigned long delta, struct clock_event_device *c)
64{
Uwe Kleine-Königa602f0f2009-12-17 12:43:29 +010065 unsigned long next, oscr;
Russell King3e238be2008-04-14 23:03:10 +010066
Russell King3e238be2008-04-14 23:03:10 +010067 OIER |= OIER_E0;
68 next = OSCR + delta;
69 OSMR0 = next;
70 oscr = OSCR;
Russell King3e238be2008-04-14 23:03:10 +010071
72 return (signed)(next - oscr) <= MIN_OSCR_DELTA ? -ETIME : 0;
73}
74
75static void
76sa1100_osmr0_set_mode(enum clock_event_mode mode, struct clock_event_device *c)
77{
Russell King3e238be2008-04-14 23:03:10 +010078 switch (mode) {
79 case CLOCK_EVT_MODE_ONESHOT:
80 case CLOCK_EVT_MODE_UNUSED:
81 case CLOCK_EVT_MODE_SHUTDOWN:
Russell King3e238be2008-04-14 23:03:10 +010082 OIER &= ~OIER_E0;
83 OSSR = OSSR_M0;
Russell King3e238be2008-04-14 23:03:10 +010084 break;
85
86 case CLOCK_EVT_MODE_RESUME:
87 case CLOCK_EVT_MODE_PERIODIC:
88 break;
89 }
90}
91
92static struct clock_event_device ckevt_sa1100_osmr0 = {
93 .name = "osmr0",
94 .features = CLOCK_EVT_FEAT_ONESHOT,
Russell King3e238be2008-04-14 23:03:10 +010095 .rating = 200,
Russell King3e238be2008-04-14 23:03:10 +010096 .set_next_event = sa1100_osmr0_set_next_event,
97 .set_mode = sa1100_osmr0_set_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
Russell Kingfac28e62009-09-27 17:32:47 +0100100static cycle_t sa1100_read_oscr(struct clocksource *s)
Russell Kingd142b6e2007-11-12 21:55:12 +0000101{
102 return OSCR;
103}
104
105static struct clocksource cksrc_sa1100_oscr = {
106 .name = "oscr",
107 .rating = 200,
108 .read = sa1100_read_oscr,
109 .mask = CLOCKSOURCE_MASK(32),
Russell Kingd142b6e2007-11-12 21:55:12 +0000110 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
111};
112
Russell King3e238be2008-04-14 23:03:10 +0100113static struct irqaction sa1100_timer_irq = {
114 .name = "ost0",
115 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
116 .handler = sa1100_ost0_interrupt,
117 .dev_id = &ckevt_sa1100_osmr0,
118};
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static void __init sa1100_timer_init(void)
121{
Russell King1ba4c3c2011-05-08 16:14:40 +0100122 OIER = 0;
123 OSSR = OSSR_M0 | OSSR_M1 | OSSR_M2 | OSSR_M3;
Russell King3e238be2008-04-14 23:03:10 +0100124
Russell King5094b922010-12-15 21:49:06 +0000125 init_fixed_sched_clock(&cd, sa1100_update_sched_clock, 32,
126 3686400, SC_MULT, SC_SHIFT);
127
Russell King1ba4c3c2011-05-08 16:14:40 +0100128 clockevents_calc_mult_shift(&ckevt_sa1100_osmr0, 3686400, 4);
Russell King3e238be2008-04-14 23:03:10 +0100129 ckevt_sa1100_osmr0.max_delta_ns =
130 clockevent_delta2ns(0x7fffffff, &ckevt_sa1100_osmr0);
131 ckevt_sa1100_osmr0.min_delta_ns =
132 clockevent_delta2ns(MIN_OSCR_DELTA * 2, &ckevt_sa1100_osmr0) + 1;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030133 ckevt_sa1100_osmr0.cpumask = cpumask_of(0);
Russell Kingd142b6e2007-11-12 21:55:12 +0000134
Russell King3e238be2008-04-14 23:03:10 +0100135 setup_irq(IRQ_OST0, &sa1100_timer_irq);
136
Russell King2c760b52010-12-13 13:19:35 +0000137 clocksource_register_hz(&cksrc_sa1100_oscr, CLOCK_TICK_RATE);
Russell King3e238be2008-04-14 23:03:10 +0100138 clockevents_register_device(&ckevt_sa1100_osmr0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
141#ifdef CONFIG_PM
142unsigned long osmr[4], oier;
143
144static void sa1100_timer_suspend(void)
145{
146 osmr[0] = OSMR0;
147 osmr[1] = OSMR1;
148 osmr[2] = OSMR2;
149 osmr[3] = OSMR3;
150 oier = OIER;
151}
152
153static void sa1100_timer_resume(void)
154{
155 OSSR = 0x0f;
156 OSMR0 = osmr[0];
157 OSMR1 = osmr[1];
158 OSMR2 = osmr[2];
159 OSMR3 = osmr[3];
160 OIER = oier;
161
162 /*
163 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
164 */
165 OSCR = OSMR0 - LATCH;
166}
167#else
168#define sa1100_timer_suspend NULL
169#define sa1100_timer_resume NULL
170#endif
171
172struct sys_timer sa1100_timer = {
173 .init = sa1100_timer_init,
174 .suspend = sa1100_timer_suspend,
175 .resume = sa1100_timer_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};