blob: 80702c9ecc77fe8a6115a1bbe8b0ea08daf97918 [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 King5094b922010-12-15 21:49:06 +000019#include <asm/sched_clock.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010020#include <mach/hardware.h>
Rob Herringf314f332012-02-24 00:06:51 +010021#include <mach/irqs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Walleijef3a0bf2012-01-04 11:42:19 +010023static u32 notrace sa1100_read_sched_clock(void)
Russell King5094b922010-12-15 21:49:06 +000024{
Russell King31696632012-06-06 11:42:36 +010025 return readl_relaxed(OSCR);
Russell King5094b922010-12-15 21:49:06 +000026}
27
Russell King3e238be2008-04-14 23:03:10 +010028#define MIN_OSCR_DELTA 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Russell King3e238be2008-04-14 23:03:10 +010030static irqreturn_t sa1100_ost0_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Russell King3e238be2008-04-14 23:03:10 +010032 struct clock_event_device *c = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Russell King3e238be2008-04-14 23:03:10 +010034 /* Disarm the compare/match, signal the event. */
Russell King31696632012-06-06 11:42:36 +010035 writel_relaxed(readl_relaxed(OIER) & ~OIER_E0, OIER);
36 writel_relaxed(OSSR_M0, OSSR);
Russell King3e238be2008-04-14 23:03:10 +010037 c->event_handler(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 return IRQ_HANDLED;
40}
41
Russell King3e238be2008-04-14 23:03:10 +010042static int
43sa1100_osmr0_set_next_event(unsigned long delta, struct clock_event_device *c)
44{
Uwe Kleine-Königa602f0f2009-12-17 12:43:29 +010045 unsigned long next, oscr;
Russell King3e238be2008-04-14 23:03:10 +010046
Russell King31696632012-06-06 11:42:36 +010047 writel_relaxed(readl_relaxed(OIER) | OIER_E0, OIER);
48 next = readl_relaxed(OSCR) + delta;
49 writel_relaxed(next, OSMR0);
50 oscr = readl_relaxed(OSCR);
Russell King3e238be2008-04-14 23:03:10 +010051
52 return (signed)(next - oscr) <= MIN_OSCR_DELTA ? -ETIME : 0;
53}
54
55static void
56sa1100_osmr0_set_mode(enum clock_event_mode mode, struct clock_event_device *c)
57{
Russell King3e238be2008-04-14 23:03:10 +010058 switch (mode) {
59 case CLOCK_EVT_MODE_ONESHOT:
60 case CLOCK_EVT_MODE_UNUSED:
61 case CLOCK_EVT_MODE_SHUTDOWN:
Russell King31696632012-06-06 11:42:36 +010062 writel_relaxed(readl_relaxed(OIER) & ~OIER_E0, OIER);
63 writel_relaxed(OSSR_M0, OSSR);
Russell King3e238be2008-04-14 23:03:10 +010064 break;
65
66 case CLOCK_EVT_MODE_RESUME:
67 case CLOCK_EVT_MODE_PERIODIC:
68 break;
69 }
70}
71
72static struct clock_event_device ckevt_sa1100_osmr0 = {
73 .name = "osmr0",
74 .features = CLOCK_EVT_FEAT_ONESHOT,
Russell King3e238be2008-04-14 23:03:10 +010075 .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 King3e238be2008-04-14 23:03:10 +010080static struct irqaction sa1100_timer_irq = {
81 .name = "ost0",
82 .flags = IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
83 .handler = sa1100_ost0_interrupt,
84 .dev_id = &ckevt_sa1100_osmr0,
85};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static void __init sa1100_timer_init(void)
88{
Russell King31696632012-06-06 11:42:36 +010089 writel_relaxed(0, OIER);
90 writel_relaxed(OSSR_M0 | OSSR_M1 | OSSR_M2 | OSSR_M3, OSSR);
Russell King3e238be2008-04-14 23:03:10 +010091
Marc Zyngier2f0778af2011-12-15 12:19:23 +010092 setup_sched_clock(sa1100_read_sched_clock, 32, 3686400);
Russell King5094b922010-12-15 21:49:06 +000093
Russell King1ba4c3c2011-05-08 16:14:40 +010094 clockevents_calc_mult_shift(&ckevt_sa1100_osmr0, 3686400, 4);
Russell King3e238be2008-04-14 23:03:10 +010095 ckevt_sa1100_osmr0.max_delta_ns =
96 clockevent_delta2ns(0x7fffffff, &ckevt_sa1100_osmr0);
97 ckevt_sa1100_osmr0.min_delta_ns =
98 clockevent_delta2ns(MIN_OSCR_DELTA * 2, &ckevt_sa1100_osmr0) + 1;
Rusty Russell320ab2b2008-12-13 21:20:26 +103099 ckevt_sa1100_osmr0.cpumask = cpumask_of(0);
Russell Kingd142b6e2007-11-12 21:55:12 +0000100
Russell King3e238be2008-04-14 23:03:10 +0100101 setup_irq(IRQ_OST0, &sa1100_timer_irq);
102
Russell King31696632012-06-06 11:42:36 +0100103 clocksource_mmio_init(OSCR, "oscr", CLOCK_TICK_RATE, 200, 32,
Russell King234b6ced2011-05-08 14:09:47 +0100104 clocksource_mmio_readl_up);
Russell King3e238be2008-04-14 23:03:10 +0100105 clockevents_register_device(&ckevt_sa1100_osmr0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108#ifdef CONFIG_PM
109unsigned long osmr[4], oier;
110
111static void sa1100_timer_suspend(void)
112{
Russell King31696632012-06-06 11:42:36 +0100113 osmr[0] = readl_relaxed(OSMR0);
114 osmr[1] = readl_relaxed(OSMR1);
115 osmr[2] = readl_relaxed(OSMR2);
116 osmr[3] = readl_relaxed(OSMR3);
117 oier = readl_relaxed(OIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120static void sa1100_timer_resume(void)
121{
Russell King31696632012-06-06 11:42:36 +0100122 writel_relaxed(0x0f, OSSR);
123 writel_relaxed(osmr[0], OSMR0);
124 writel_relaxed(osmr[1], OSMR1);
125 writel_relaxed(osmr[2], OSMR2);
126 writel_relaxed(osmr[3], OSMR3);
127 writel_relaxed(oier, OIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 /*
130 * OSMR0 is the system timer: make sure OSCR is sufficiently behind
131 */
Russell King31696632012-06-06 11:42:36 +0100132 writel_relaxed(OSMR0 - LATCH, OSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134#else
135#define sa1100_timer_suspend NULL
136#define sa1100_timer_resume NULL
137#endif
138
139struct sys_timer sa1100_timer = {
140 .init = sa1100_timer_init,
141 .suspend = sa1100_timer_suspend,
142 .resume = sa1100_timer_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};