blob: b9cbb56d6e9d9dbd9c7605196488b0872fd8bc4d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/timex.h>
Russell King3e238be2008-04-14 23:03:10 +010016#include <linux/clockchips.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/mach/time.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010019#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Russell King3e238be2008-04-14 23:03:10 +010021#define MIN_OSCR_DELTA 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Russell King3e238be2008-04-14 23:03:10 +010023static irqreturn_t sa1100_ost0_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Russell King3e238be2008-04-14 23:03:10 +010025 struct clock_event_device *c = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Russell King3e238be2008-04-14 23:03:10 +010027 /* Disarm the compare/match, signal the event. */
28 OIER &= ~OIER_E0;
29 OSSR = OSSR_M0;
30 c->event_handler(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 return IRQ_HANDLED;
33}
34
Russell King3e238be2008-04-14 23:03:10 +010035static int
36sa1100_osmr0_set_next_event(unsigned long delta, struct clock_event_device *c)
37{
38 unsigned long flags, next, oscr;
39
40 raw_local_irq_save(flags);
41 OIER |= OIER_E0;
42 next = OSCR + delta;
43 OSMR0 = next;
44 oscr = OSCR;
45 raw_local_irq_restore(flags);
46
47 return (signed)(next - oscr) <= MIN_OSCR_DELTA ? -ETIME : 0;
48}
49
50static void
51sa1100_osmr0_set_mode(enum clock_event_mode mode, struct clock_event_device *c)
52{
53 unsigned long flags;
54
55 switch (mode) {
56 case CLOCK_EVT_MODE_ONESHOT:
57 case CLOCK_EVT_MODE_UNUSED:
58 case CLOCK_EVT_MODE_SHUTDOWN:
59 raw_local_irq_save(flags);
60 OIER &= ~OIER_E0;
61 OSSR = OSSR_M0;
62 raw_local_irq_restore(flags);
63 break;
64
65 case CLOCK_EVT_MODE_RESUME:
66 case CLOCK_EVT_MODE_PERIODIC:
67 break;
68 }
69}
70
71static struct clock_event_device ckevt_sa1100_osmr0 = {
72 .name = "osmr0",
73 .features = CLOCK_EVT_FEAT_ONESHOT,
74 .shift = 32,
75 .rating = 200,
Russell King3e238be2008-04-14 23:03:10 +010076 .set_next_event = sa1100_osmr0_set_next_event,
77 .set_mode = sa1100_osmr0_set_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
Russell Kingfac28e62009-09-27 17:32:47 +010080static cycle_t sa1100_read_oscr(struct clocksource *s)
Russell Kingd142b6e2007-11-12 21:55:12 +000081{
82 return OSCR;
83}
84
85static struct clocksource cksrc_sa1100_oscr = {
86 .name = "oscr",
87 .rating = 200,
88 .read = sa1100_read_oscr,
89 .mask = CLOCKSOURCE_MASK(32),
90 .shift = 20,
91 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
92};
93
Russell King3e238be2008-04-14 23:03:10 +010094static struct irqaction sa1100_timer_irq = {
95 .name = "ost0",
96 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
97 .handler = sa1100_ost0_interrupt,
98 .dev_id = &ckevt_sa1100_osmr0,
99};
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static void __init sa1100_timer_init(void)
102{
Nicolas Pitre5285eb572005-11-08 22:43:06 +0000103 OIER = 0; /* disable any timer interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 OSSR = 0xf; /* clear status on all timers */
Russell King3e238be2008-04-14 23:03:10 +0100105
106 ckevt_sa1100_osmr0.mult =
107 div_sc(3686400, NSEC_PER_SEC, ckevt_sa1100_osmr0.shift);
108 ckevt_sa1100_osmr0.max_delta_ns =
109 clockevent_delta2ns(0x7fffffff, &ckevt_sa1100_osmr0);
110 ckevt_sa1100_osmr0.min_delta_ns =
111 clockevent_delta2ns(MIN_OSCR_DELTA * 2, &ckevt_sa1100_osmr0) + 1;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030112 ckevt_sa1100_osmr0.cpumask = cpumask_of(0);
Russell Kingd142b6e2007-11-12 21:55:12 +0000113
114 cksrc_sa1100_oscr.mult =
115 clocksource_hz2mult(CLOCK_TICK_RATE, cksrc_sa1100_oscr.shift);
116
Russell King3e238be2008-04-14 23:03:10 +0100117 setup_irq(IRQ_OST0, &sa1100_timer_irq);
118
Russell Kingd142b6e2007-11-12 21:55:12 +0000119 clocksource_register(&cksrc_sa1100_oscr);
Russell King3e238be2008-04-14 23:03:10 +0100120 clockevents_register_device(&ckevt_sa1100_osmr0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123#ifdef CONFIG_PM
124unsigned long osmr[4], oier;
125
126static void sa1100_timer_suspend(void)
127{
128 osmr[0] = OSMR0;
129 osmr[1] = OSMR1;
130 osmr[2] = OSMR2;
131 osmr[3] = OSMR3;
132 oier = OIER;
133}
134
135static void sa1100_timer_resume(void)
136{
137 OSSR = 0x0f;
138 OSMR0 = osmr[0];
139 OSMR1 = osmr[1];
140 OSMR2 = osmr[2];
141 OSMR3 = osmr[3];
142 OIER = oier;
143
144 /*
145 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
146 */
147 OSCR = OSMR0 - LATCH;
148}
149#else
150#define sa1100_timer_suspend NULL
151#define sa1100_timer_resume NULL
152#endif
153
154struct sys_timer sa1100_timer = {
155 .init = sa1100_timer_init,
156 .suspend = sa1100_timer_suspend,
157 .resume = sa1100_timer_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};