Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/mach-sa1100/time.c |
| 3 | * |
| 4 | * Copyright (C) 1998 Deborah Wallach. |
Kristoffer Ericson | 9398253 | 2008-11-26 20:58:43 +0100 | [diff] [blame] | 5 | * Twiddles (C) 1999 Hugo Fiennes <hugo@empeg.com> |
| 6 | * |
Nicolas Pitre | 2f82af0 | 2009-09-14 03:25:28 -0400 | [diff] [blame] | 7 | * 2000/03/29 (C) Nicolas Pitre <nico@fluxnic.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * 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 Gleixner | 119c641 | 2006-07-01 22:32:38 +0100 | [diff] [blame] | 14 | #include <linux/irq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/timex.h> |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 16 | #include <linux/clockchips.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #include <asm/mach/time.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 19 | #include <mach/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 21 | #define MIN_OSCR_DELTA 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 23 | static irqreturn_t sa1100_ost0_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | { |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 25 | struct clock_event_device *c = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 27 | /* Disarm the compare/match, signal the event. */ |
| 28 | OIER &= ~OIER_E0; |
| 29 | OSSR = OSSR_M0; |
| 30 | c->event_handler(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | return IRQ_HANDLED; |
| 33 | } |
| 34 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 35 | static int |
| 36 | sa1100_osmr0_set_next_event(unsigned long delta, struct clock_event_device *c) |
| 37 | { |
Uwe Kleine-König | a602f0f | 2009-12-17 12:43:29 +0100 | [diff] [blame] | 38 | unsigned long next, oscr; |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 39 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 40 | OIER |= OIER_E0; |
| 41 | next = OSCR + delta; |
| 42 | OSMR0 = next; |
| 43 | oscr = OSCR; |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 44 | |
| 45 | return (signed)(next - oscr) <= MIN_OSCR_DELTA ? -ETIME : 0; |
| 46 | } |
| 47 | |
| 48 | static void |
| 49 | sa1100_osmr0_set_mode(enum clock_event_mode mode, struct clock_event_device *c) |
| 50 | { |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 51 | switch (mode) { |
| 52 | case CLOCK_EVT_MODE_ONESHOT: |
| 53 | case CLOCK_EVT_MODE_UNUSED: |
| 54 | case CLOCK_EVT_MODE_SHUTDOWN: |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 55 | OIER &= ~OIER_E0; |
| 56 | OSSR = OSSR_M0; |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 57 | break; |
| 58 | |
| 59 | case CLOCK_EVT_MODE_RESUME: |
| 60 | case CLOCK_EVT_MODE_PERIODIC: |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static struct clock_event_device ckevt_sa1100_osmr0 = { |
| 66 | .name = "osmr0", |
| 67 | .features = CLOCK_EVT_FEAT_ONESHOT, |
| 68 | .shift = 32, |
| 69 | .rating = 200, |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 70 | .set_next_event = sa1100_osmr0_set_next_event, |
| 71 | .set_mode = sa1100_osmr0_set_mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Russell King | fac28e6 | 2009-09-27 17:32:47 +0100 | [diff] [blame] | 74 | static cycle_t sa1100_read_oscr(struct clocksource *s) |
Russell King | d142b6e | 2007-11-12 21:55:12 +0000 | [diff] [blame] | 75 | { |
| 76 | return OSCR; |
| 77 | } |
| 78 | |
| 79 | static 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 King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 88 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | static void __init sa1100_timer_init(void) |
| 96 | { |
Nicolas Pitre | 5285eb5 | 2005-11-08 22:43:06 +0000 | [diff] [blame] | 97 | OIER = 0; /* disable any timer interrupts */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | OSSR = 0xf; /* clear status on all timers */ |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 99 | |
| 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 Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 106 | ckevt_sa1100_osmr0.cpumask = cpumask_of(0); |
Russell King | d142b6e | 2007-11-12 21:55:12 +0000 | [diff] [blame] | 107 | |
| 108 | cksrc_sa1100_oscr.mult = |
| 109 | clocksource_hz2mult(CLOCK_TICK_RATE, cksrc_sa1100_oscr.shift); |
| 110 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 111 | setup_irq(IRQ_OST0, &sa1100_timer_irq); |
| 112 | |
Russell King | d142b6e | 2007-11-12 21:55:12 +0000 | [diff] [blame] | 113 | clocksource_register(&cksrc_sa1100_oscr); |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 114 | clockevents_register_device(&ckevt_sa1100_osmr0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | #ifdef CONFIG_PM |
| 118 | unsigned long osmr[4], oier; |
| 119 | |
| 120 | static 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 | |
| 129 | static 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 | |
| 148 | struct sys_timer sa1100_timer = { |
| 149 | .init = sa1100_timer_init, |
| 150 | .suspend = sa1100_timer_suspend, |
| 151 | .resume = sa1100_timer_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | }; |