blob: 74b6e0e570b64e247681d052da44f6dcb645e16b [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{
Uwe Kleine-Königa602f0f2009-12-17 12:43:29 +010038 unsigned long next, oscr;
Russell King3e238be2008-04-14 23:03:10 +010039
Russell King3e238be2008-04-14 23:03:10 +010040 OIER |= OIER_E0;
41 next = OSCR + delta;
42 OSMR0 = next;
43 oscr = OSCR;
Russell King3e238be2008-04-14 23:03:10 +010044
45 return (signed)(next - oscr) <= MIN_OSCR_DELTA ? -ETIME : 0;
46}
47
48static void
49sa1100_osmr0_set_mode(enum clock_event_mode mode, struct clock_event_device *c)
50{
Russell King3e238be2008-04-14 23:03:10 +010051 switch (mode) {
52 case CLOCK_EVT_MODE_ONESHOT:
53 case CLOCK_EVT_MODE_UNUSED:
54 case CLOCK_EVT_MODE_SHUTDOWN:
Russell King3e238be2008-04-14 23:03:10 +010055 OIER &= ~OIER_E0;
56 OSSR = OSSR_M0;
Russell King3e238be2008-04-14 23:03:10 +010057 break;
58
59 case CLOCK_EVT_MODE_RESUME:
60 case CLOCK_EVT_MODE_PERIODIC:
61 break;
62 }
63}
64
65static struct clock_event_device ckevt_sa1100_osmr0 = {
66 .name = "osmr0",
67 .features = CLOCK_EVT_FEAT_ONESHOT,
68 .shift = 32,
69 .rating = 200,
Russell King3e238be2008-04-14 23:03:10 +010070 .set_next_event = sa1100_osmr0_set_next_event,
71 .set_mode = sa1100_osmr0_set_mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
Russell Kingfac28e62009-09-27 17:32:47 +010074static cycle_t sa1100_read_oscr(struct clocksource *s)
Russell Kingd142b6e2007-11-12 21:55:12 +000075{
76 return OSCR;
77}
78
79static struct clocksource cksrc_sa1100_oscr = {
80 .name = "oscr",
81 .rating = 200,
82 .read = sa1100_read_oscr,
83 .mask = CLOCKSOURCE_MASK(32),
84 .shift = 20,
85 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
86};
87
Russell King3e238be2008-04-14 23:03:10 +010088static struct irqaction sa1100_timer_irq = {
89 .name = "ost0",
90 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
91 .handler = sa1100_ost0_interrupt,
92 .dev_id = &ckevt_sa1100_osmr0,
93};
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static void __init sa1100_timer_init(void)
96{
Nicolas Pitre5285eb52005-11-08 22:43:06 +000097 OIER = 0; /* disable any timer interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 OSSR = 0xf; /* clear status on all timers */
Russell King3e238be2008-04-14 23:03:10 +010099
100 ckevt_sa1100_osmr0.mult =
101 div_sc(3686400, NSEC_PER_SEC, ckevt_sa1100_osmr0.shift);
102 ckevt_sa1100_osmr0.max_delta_ns =
103 clockevent_delta2ns(0x7fffffff, &ckevt_sa1100_osmr0);
104 ckevt_sa1100_osmr0.min_delta_ns =
105 clockevent_delta2ns(MIN_OSCR_DELTA * 2, &ckevt_sa1100_osmr0) + 1;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030106 ckevt_sa1100_osmr0.cpumask = cpumask_of(0);
Russell Kingd142b6e2007-11-12 21:55:12 +0000107
108 cksrc_sa1100_oscr.mult =
109 clocksource_hz2mult(CLOCK_TICK_RATE, cksrc_sa1100_oscr.shift);
110
Russell King3e238be2008-04-14 23:03:10 +0100111 setup_irq(IRQ_OST0, &sa1100_timer_irq);
112
Russell Kingd142b6e2007-11-12 21:55:12 +0000113 clocksource_register(&cksrc_sa1100_oscr);
Russell King3e238be2008-04-14 23:03:10 +0100114 clockevents_register_device(&ckevt_sa1100_osmr0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117#ifdef CONFIG_PM
118unsigned long osmr[4], oier;
119
120static void sa1100_timer_suspend(void)
121{
122 osmr[0] = OSMR0;
123 osmr[1] = OSMR1;
124 osmr[2] = OSMR2;
125 osmr[3] = OSMR3;
126 oier = OIER;
127}
128
129static void sa1100_timer_resume(void)
130{
131 OSSR = 0x0f;
132 OSMR0 = osmr[0];
133 OSMR1 = osmr[1];
134 OSMR2 = osmr[2];
135 OSMR3 = osmr[3];
136 OIER = oier;
137
138 /*
139 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
140 */
141 OSCR = OSMR0 - LATCH;
142}
143#else
144#define sa1100_timer_suspend NULL
145#define sa1100_timer_resume NULL
146#endif
147
148struct sys_timer sa1100_timer = {
149 .init = sa1100_timer_init,
150 .suspend = sa1100_timer_suspend,
151 .resume = sa1100_timer_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152};