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> |
Russell King | 5094b92 | 2010-12-15 21:49:06 +0000 | [diff] [blame] | 15 | #include <linux/sched.h> /* just for sched_clock() - funny that */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/timex.h> |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 17 | #include <linux/clockchips.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include <asm/mach/time.h> |
Russell King | 5094b92 | 2010-12-15 21:49:06 +0000 | [diff] [blame] | 20 | #include <asm/sched_clock.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 21 | #include <mach/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Russell King | 5094b92 | 2010-12-15 21:49:06 +0000 | [diff] [blame] | 23 | /* |
| 24 | * This is the SA11x0 sched_clock implementation. |
| 25 | */ |
| 26 | static 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 | |
| 36 | unsigned 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 | |
| 42 | static void notrace sa1100_update_sched_clock(void) |
| 43 | { |
| 44 | u32 cyc = OSCR; |
| 45 | update_sched_clock(&cd, cyc, (u32)~0); |
| 46 | } |
| 47 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 48 | #define MIN_OSCR_DELTA 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 50 | static irqreturn_t sa1100_ost0_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 52 | struct clock_event_device *c = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 54 | /* Disarm the compare/match, signal the event. */ |
| 55 | OIER &= ~OIER_E0; |
| 56 | OSSR = OSSR_M0; |
| 57 | c->event_handler(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | return IRQ_HANDLED; |
| 60 | } |
| 61 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 62 | static int |
| 63 | sa1100_osmr0_set_next_event(unsigned long delta, struct clock_event_device *c) |
| 64 | { |
Uwe Kleine-König | a602f0f | 2009-12-17 12:43:29 +0100 | [diff] [blame] | 65 | unsigned long next, oscr; |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 66 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 67 | OIER |= OIER_E0; |
| 68 | next = OSCR + delta; |
| 69 | OSMR0 = next; |
| 70 | oscr = OSCR; |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 71 | |
| 72 | return (signed)(next - oscr) <= MIN_OSCR_DELTA ? -ETIME : 0; |
| 73 | } |
| 74 | |
| 75 | static void |
| 76 | sa1100_osmr0_set_mode(enum clock_event_mode mode, struct clock_event_device *c) |
| 77 | { |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 78 | switch (mode) { |
| 79 | case CLOCK_EVT_MODE_ONESHOT: |
| 80 | case CLOCK_EVT_MODE_UNUSED: |
| 81 | case CLOCK_EVT_MODE_SHUTDOWN: |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 82 | OIER &= ~OIER_E0; |
| 83 | OSSR = OSSR_M0; |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 84 | break; |
| 85 | |
| 86 | case CLOCK_EVT_MODE_RESUME: |
| 87 | case CLOCK_EVT_MODE_PERIODIC: |
| 88 | break; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | static struct clock_event_device ckevt_sa1100_osmr0 = { |
| 93 | .name = "osmr0", |
| 94 | .features = CLOCK_EVT_FEAT_ONESHOT, |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 95 | .rating = 200, |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 96 | .set_next_event = sa1100_osmr0_set_next_event, |
| 97 | .set_mode = sa1100_osmr0_set_mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Russell King | fac28e6 | 2009-09-27 17:32:47 +0100 | [diff] [blame] | 100 | static cycle_t sa1100_read_oscr(struct clocksource *s) |
Russell King | d142b6e | 2007-11-12 21:55:12 +0000 | [diff] [blame] | 101 | { |
| 102 | return OSCR; |
| 103 | } |
| 104 | |
| 105 | static struct clocksource cksrc_sa1100_oscr = { |
| 106 | .name = "oscr", |
| 107 | .rating = 200, |
| 108 | .read = sa1100_read_oscr, |
| 109 | .mask = CLOCKSOURCE_MASK(32), |
Russell King | d142b6e | 2007-11-12 21:55:12 +0000 | [diff] [blame] | 110 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 111 | }; |
| 112 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 113 | static 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | static void __init sa1100_timer_init(void) |
| 121 | { |
Russell King | 1ba4c3c | 2011-05-08 16:14:40 +0100 | [diff] [blame^] | 122 | OIER = 0; |
| 123 | OSSR = OSSR_M0 | OSSR_M1 | OSSR_M2 | OSSR_M3; |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 124 | |
Russell King | 5094b92 | 2010-12-15 21:49:06 +0000 | [diff] [blame] | 125 | init_fixed_sched_clock(&cd, sa1100_update_sched_clock, 32, |
| 126 | 3686400, SC_MULT, SC_SHIFT); |
| 127 | |
Russell King | 1ba4c3c | 2011-05-08 16:14:40 +0100 | [diff] [blame^] | 128 | clockevents_calc_mult_shift(&ckevt_sa1100_osmr0, 3686400, 4); |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 129 | 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 Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 133 | ckevt_sa1100_osmr0.cpumask = cpumask_of(0); |
Russell King | d142b6e | 2007-11-12 21:55:12 +0000 | [diff] [blame] | 134 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 135 | setup_irq(IRQ_OST0, &sa1100_timer_irq); |
| 136 | |
Russell King | 2c760b5 | 2010-12-13 13:19:35 +0000 | [diff] [blame] | 137 | clocksource_register_hz(&cksrc_sa1100_oscr, CLOCK_TICK_RATE); |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 138 | clockevents_register_device(&ckevt_sa1100_osmr0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | #ifdef CONFIG_PM |
| 142 | unsigned long osmr[4], oier; |
| 143 | |
| 144 | static 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 | |
| 153 | static 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 | |
| 172 | struct sys_timer sa1100_timer = { |
| 173 | .init = sa1100_timer_init, |
| 174 | .suspend = sa1100_timer_suspend, |
| 175 | .resume = sa1100_timer_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | }; |