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> |
Uwe Kleine-König | dc2fc22 | 2013-11-12 20:56:02 +0100 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/errno.h> |
| 14 | #include <linux/interrupt.h> |
Thomas Gleixner | 119c641 | 2006-07-01 22:32:38 +0100 | [diff] [blame] | 15 | #include <linux/irq.h> |
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> |
Stephen Boyd | 38ff87f | 2013-06-01 23:39:40 -0700 | [diff] [blame] | 18 | #include <linux/sched_clock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include <asm/mach/time.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 21 | #include <mach/hardware.h> |
Rob Herring | f314f33 | 2012-02-24 00:06:51 +0100 | [diff] [blame] | 22 | #include <mach/irqs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Uwe Kleine-König | dc2fc22 | 2013-11-12 20:56:02 +0100 | [diff] [blame] | 24 | #define SA1100_CLOCK_FREQ 3686400 |
| 25 | #define SA1100_LATCH DIV_ROUND_CLOSEST(SA1100_CLOCK_FREQ, HZ) |
| 26 | |
Stephen Boyd | 26cad74 | 2013-11-15 15:26:20 -0800 | [diff] [blame] | 27 | static u64 notrace sa1100_read_sched_clock(void) |
Russell King | 5094b92 | 2010-12-15 21:49:06 +0000 | [diff] [blame] | 28 | { |
Russell King | 3169663 | 2012-06-06 11:42:36 +0100 | [diff] [blame] | 29 | return readl_relaxed(OSCR); |
Russell King | 5094b92 | 2010-12-15 21:49:06 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 32 | #define MIN_OSCR_DELTA 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 34 | static irqreturn_t sa1100_ost0_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 36 | struct clock_event_device *c = dev_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 38 | /* Disarm the compare/match, signal the event. */ |
Russell King | 3169663 | 2012-06-06 11:42:36 +0100 | [diff] [blame] | 39 | writel_relaxed(readl_relaxed(OIER) & ~OIER_E0, OIER); |
| 40 | writel_relaxed(OSSR_M0, OSSR); |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 41 | c->event_handler(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | return IRQ_HANDLED; |
| 44 | } |
| 45 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 46 | static int |
| 47 | sa1100_osmr0_set_next_event(unsigned long delta, struct clock_event_device *c) |
| 48 | { |
Uwe Kleine-König | a602f0f | 2009-12-17 12:43:29 +0100 | [diff] [blame] | 49 | unsigned long next, oscr; |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 50 | |
Russell King | 3169663 | 2012-06-06 11:42:36 +0100 | [diff] [blame] | 51 | writel_relaxed(readl_relaxed(OIER) | OIER_E0, OIER); |
| 52 | next = readl_relaxed(OSCR) + delta; |
| 53 | writel_relaxed(next, OSMR0); |
| 54 | oscr = readl_relaxed(OSCR); |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 55 | |
| 56 | return (signed)(next - oscr) <= MIN_OSCR_DELTA ? -ETIME : 0; |
| 57 | } |
| 58 | |
| 59 | static void |
| 60 | sa1100_osmr0_set_mode(enum clock_event_mode mode, struct clock_event_device *c) |
| 61 | { |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 62 | switch (mode) { |
| 63 | case CLOCK_EVT_MODE_ONESHOT: |
| 64 | case CLOCK_EVT_MODE_UNUSED: |
| 65 | case CLOCK_EVT_MODE_SHUTDOWN: |
Russell King | 3169663 | 2012-06-06 11:42:36 +0100 | [diff] [blame] | 66 | writel_relaxed(readl_relaxed(OIER) & ~OIER_E0, OIER); |
| 67 | writel_relaxed(OSSR_M0, OSSR); |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 68 | break; |
| 69 | |
| 70 | case CLOCK_EVT_MODE_RESUME: |
| 71 | case CLOCK_EVT_MODE_PERIODIC: |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | |
Stephen Warren | e3cbfb6 | 2012-11-07 16:35:11 -0700 | [diff] [blame] | 76 | #ifdef CONFIG_PM |
| 77 | unsigned long osmr[4], oier; |
| 78 | |
| 79 | static void sa1100_timer_suspend(struct clock_event_device *cedev) |
| 80 | { |
| 81 | osmr[0] = readl_relaxed(OSMR0); |
| 82 | osmr[1] = readl_relaxed(OSMR1); |
| 83 | osmr[2] = readl_relaxed(OSMR2); |
| 84 | osmr[3] = readl_relaxed(OSMR3); |
| 85 | oier = readl_relaxed(OIER); |
| 86 | } |
| 87 | |
| 88 | static void sa1100_timer_resume(struct clock_event_device *cedev) |
| 89 | { |
| 90 | writel_relaxed(0x0f, OSSR); |
| 91 | writel_relaxed(osmr[0], OSMR0); |
| 92 | writel_relaxed(osmr[1], OSMR1); |
| 93 | writel_relaxed(osmr[2], OSMR2); |
| 94 | writel_relaxed(osmr[3], OSMR3); |
| 95 | writel_relaxed(oier, OIER); |
| 96 | |
| 97 | /* |
| 98 | * OSMR0 is the system timer: make sure OSCR is sufficiently behind |
| 99 | */ |
Uwe Kleine-König | dc2fc22 | 2013-11-12 20:56:02 +0100 | [diff] [blame] | 100 | writel_relaxed(OSMR0 - SA1100_LATCH, OSCR); |
Stephen Warren | e3cbfb6 | 2012-11-07 16:35:11 -0700 | [diff] [blame] | 101 | } |
| 102 | #else |
| 103 | #define sa1100_timer_suspend NULL |
| 104 | #define sa1100_timer_resume NULL |
| 105 | #endif |
| 106 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 107 | static struct clock_event_device ckevt_sa1100_osmr0 = { |
| 108 | .name = "osmr0", |
| 109 | .features = CLOCK_EVT_FEAT_ONESHOT, |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 110 | .rating = 200, |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 111 | .set_next_event = sa1100_osmr0_set_next_event, |
| 112 | .set_mode = sa1100_osmr0_set_mode, |
Stephen Warren | e3cbfb6 | 2012-11-07 16:35:11 -0700 | [diff] [blame] | 113 | .suspend = sa1100_timer_suspend, |
| 114 | .resume = sa1100_timer_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 117 | static struct irqaction sa1100_timer_irq = { |
| 118 | .name = "ost0", |
Michael Opdenacker | 78f6db9 | 2014-03-04 22:04:50 +0100 | [diff] [blame] | 119 | .flags = IRQF_TIMER | IRQF_IRQPOLL, |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 120 | .handler = sa1100_ost0_interrupt, |
| 121 | .dev_id = &ckevt_sa1100_osmr0, |
| 122 | }; |
| 123 | |
Stephen Warren | 6bb27d7 | 2012-11-08 12:40:59 -0700 | [diff] [blame] | 124 | void __init sa1100_timer_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Russell King | 3169663 | 2012-06-06 11:42:36 +0100 | [diff] [blame] | 126 | writel_relaxed(0, OIER); |
| 127 | writel_relaxed(OSSR_M0 | OSSR_M1 | OSSR_M2 | OSSR_M3, OSSR); |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 128 | |
Stephen Boyd | 26cad74 | 2013-11-15 15:26:20 -0800 | [diff] [blame] | 129 | sched_clock_register(sa1100_read_sched_clock, 32, 3686400); |
Russell King | 5094b92 | 2010-12-15 21:49:06 +0000 | [diff] [blame] | 130 | |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 131 | ckevt_sa1100_osmr0.cpumask = cpumask_of(0); |
Russell King | d142b6e | 2007-11-12 21:55:12 +0000 | [diff] [blame] | 132 | |
Russell King | 3e238be | 2008-04-14 23:03:10 +0100 | [diff] [blame] | 133 | setup_irq(IRQ_OST0, &sa1100_timer_irq); |
| 134 | |
Uwe Kleine-König | dc2fc22 | 2013-11-12 20:56:02 +0100 | [diff] [blame] | 135 | clocksource_mmio_init(OSCR, "oscr", SA1100_CLOCK_FREQ, 200, 32, |
Russell King | 234b6ced | 2011-05-08 14:09:47 +0100 | [diff] [blame] | 136 | clocksource_mmio_readl_up); |
Olof Johansson | 8d84981 | 2013-01-14 10:20:02 -0800 | [diff] [blame] | 137 | clockevents_config_and_register(&ckevt_sa1100_osmr0, 3686400, |
| 138 | MIN_OSCR_DELTA * 2, 0x7fffffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |