Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 1 | /* |
| 2 | * arch/arm/plat-orion/time.c |
| 3 | * |
| 4 | * Marvell Orion SoC timer handling. |
| 5 | * |
| 6 | * This file is licensed under the terms of the GNU General Public |
| 7 | * License version 2. This program is licensed "as is" without any |
| 8 | * warranty of any kind, whether express or implied. |
| 9 | * |
| 10 | * Timer 0 is used as free-running clocksource, while timer 1 is |
| 11 | * used as clock_event_device. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
Nicolas Pitre | a399e3f | 2009-05-15 00:42:36 -0400 | [diff] [blame] | 15 | #include <linux/timer.h> |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 16 | #include <linux/clockchips.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/irq.h> |
Stephen Boyd | 38ff87f | 2013-06-01 23:39:40 -0700 | [diff] [blame] | 19 | #include <linux/sched_clock.h> |
Andrew Lunn | 48fce88 | 2013-10-23 16:12:50 +0200 | [diff] [blame] | 20 | #include <plat/time.h> |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 21 | |
| 22 | /* |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 23 | * MBus bridge block registers. |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 24 | */ |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 25 | #define BRIDGE_CAUSE_OFF 0x0110 |
| 26 | #define BRIDGE_MASK_OFF 0x0114 |
| 27 | #define BRIDGE_INT_TIMER0 0x0002 |
| 28 | #define BRIDGE_INT_TIMER1 0x0004 |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 29 | |
| 30 | |
| 31 | /* |
| 32 | * Timer block registers. |
| 33 | */ |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 34 | #define TIMER_CTRL_OFF 0x0000 |
| 35 | #define TIMER0_EN 0x0001 |
| 36 | #define TIMER0_RELOAD_EN 0x0002 |
| 37 | #define TIMER1_EN 0x0004 |
| 38 | #define TIMER1_RELOAD_EN 0x0008 |
| 39 | #define TIMER0_RELOAD_OFF 0x0010 |
| 40 | #define TIMER0_VAL_OFF 0x0014 |
| 41 | #define TIMER1_RELOAD_OFF 0x0018 |
| 42 | #define TIMER1_VAL_OFF 0x001c |
| 43 | |
| 44 | |
| 45 | /* |
| 46 | * SoC-specific data. |
| 47 | */ |
| 48 | static void __iomem *bridge_base; |
| 49 | static u32 bridge_timer1_clr_mask; |
| 50 | static void __iomem *timer_base; |
| 51 | |
| 52 | |
| 53 | /* |
| 54 | * Number of timer ticks per jiffy. |
| 55 | */ |
| 56 | static u32 ticks_per_jiffy; |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 57 | |
| 58 | |
| 59 | /* |
Stefan Agner | 8a3269f | 2009-05-12 10:30:41 -0700 | [diff] [blame] | 60 | * Orion's sched_clock implementation. It has a resolution of |
Russell King | f06a162 | 2010-12-15 21:55:06 +0000 | [diff] [blame] | 61 | * at least 7.5ns (133MHz TCLK). |
Stefan Agner | 8a3269f | 2009-05-12 10:30:41 -0700 | [diff] [blame] | 62 | */ |
Stefan Agner | 8a3269f | 2009-05-12 10:30:41 -0700 | [diff] [blame] | 63 | |
Stephen Boyd | b44653b | 2013-11-15 15:26:24 -0800 | [diff] [blame] | 64 | static u64 notrace orion_read_sched_clock(void) |
Stefan Agner | 8a3269f | 2009-05-12 10:30:41 -0700 | [diff] [blame] | 65 | { |
Marc Zyngier | 2f0778af | 2011-12-15 12:19:23 +0100 | [diff] [blame] | 66 | return ~readl(timer_base + TIMER0_VAL_OFF); |
Stefan Agner | 8a3269f | 2009-05-12 10:30:41 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 70 | * Clockevent handling. |
| 71 | */ |
| 72 | static int |
| 73 | orion_clkevt_next_event(unsigned long delta, struct clock_event_device *dev) |
| 74 | { |
| 75 | unsigned long flags; |
| 76 | u32 u; |
| 77 | |
| 78 | if (delta == 0) |
| 79 | return -ETIME; |
| 80 | |
| 81 | local_irq_save(flags); |
| 82 | |
| 83 | /* |
| 84 | * Clear and enable clockevent timer interrupt. |
| 85 | */ |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 86 | writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 87 | |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 88 | u = readl(bridge_base + BRIDGE_MASK_OFF); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 89 | u |= BRIDGE_INT_TIMER1; |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 90 | writel(u, bridge_base + BRIDGE_MASK_OFF); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * Setup new clockevent timer value. |
| 94 | */ |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 95 | writel(delta, timer_base + TIMER1_VAL_OFF); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * Enable the timer. |
| 99 | */ |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 100 | u = readl(timer_base + TIMER_CTRL_OFF); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 101 | u = (u & ~TIMER1_RELOAD_EN) | TIMER1_EN; |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 102 | writel(u, timer_base + TIMER_CTRL_OFF); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 103 | |
| 104 | local_irq_restore(flags); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | orion_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev) |
| 111 | { |
| 112 | unsigned long flags; |
| 113 | u32 u; |
| 114 | |
| 115 | local_irq_save(flags); |
| 116 | if (mode == CLOCK_EVT_MODE_PERIODIC) { |
| 117 | /* |
| 118 | * Setup timer to fire at 1/HZ intervals. |
| 119 | */ |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 120 | writel(ticks_per_jiffy - 1, timer_base + TIMER1_RELOAD_OFF); |
| 121 | writel(ticks_per_jiffy - 1, timer_base + TIMER1_VAL_OFF); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * Enable timer interrupt. |
| 125 | */ |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 126 | u = readl(bridge_base + BRIDGE_MASK_OFF); |
| 127 | writel(u | BRIDGE_INT_TIMER1, bridge_base + BRIDGE_MASK_OFF); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 128 | |
| 129 | /* |
| 130 | * Enable timer. |
| 131 | */ |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 132 | u = readl(timer_base + TIMER_CTRL_OFF); |
| 133 | writel(u | TIMER1_EN | TIMER1_RELOAD_EN, |
| 134 | timer_base + TIMER_CTRL_OFF); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 135 | } else { |
| 136 | /* |
| 137 | * Disable timer. |
| 138 | */ |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 139 | u = readl(timer_base + TIMER_CTRL_OFF); |
| 140 | writel(u & ~TIMER1_EN, timer_base + TIMER_CTRL_OFF); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 141 | |
| 142 | /* |
| 143 | * Disable timer interrupt. |
| 144 | */ |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 145 | u = readl(bridge_base + BRIDGE_MASK_OFF); |
| 146 | writel(u & ~BRIDGE_INT_TIMER1, bridge_base + BRIDGE_MASK_OFF); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * ACK pending timer interrupt. |
| 150 | */ |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 151 | writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 152 | |
| 153 | } |
| 154 | local_irq_restore(flags); |
| 155 | } |
| 156 | |
| 157 | static struct clock_event_device orion_clkevt = { |
| 158 | .name = "orion_tick", |
| 159 | .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC, |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 160 | .rating = 300, |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 161 | .set_next_event = orion_clkevt_next_event, |
| 162 | .set_mode = orion_clkevt_mode, |
| 163 | }; |
| 164 | |
| 165 | static irqreturn_t orion_timer_interrupt(int irq, void *dev_id) |
| 166 | { |
| 167 | /* |
| 168 | * ACK timer interrupt and call event handler. |
| 169 | */ |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 170 | writel(bridge_timer1_clr_mask, bridge_base + BRIDGE_CAUSE_OFF); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 171 | orion_clkevt.event_handler(&orion_clkevt); |
| 172 | |
| 173 | return IRQ_HANDLED; |
| 174 | } |
| 175 | |
| 176 | static struct irqaction orion_timer_irq = { |
| 177 | .name = "orion_tick", |
Michael Opdenacker | eb4d552 | 2013-10-12 05:49:20 +0200 | [diff] [blame] | 178 | .flags = IRQF_TIMER, |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 179 | .handler = orion_timer_interrupt |
| 180 | }; |
| 181 | |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 182 | void __init |
Thomas Petazzoni | e96a030 | 2012-09-11 14:27:25 +0200 | [diff] [blame] | 183 | orion_time_set_base(void __iomem *_timer_base) |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 184 | { |
Thomas Petazzoni | e96a030 | 2012-09-11 14:27:25 +0200 | [diff] [blame] | 185 | timer_base = _timer_base; |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void __init |
Thomas Petazzoni | e96a030 | 2012-09-11 14:27:25 +0200 | [diff] [blame] | 189 | orion_time_init(void __iomem *_bridge_base, u32 _bridge_timer1_clr_mask, |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 190 | unsigned int irq, unsigned int tclk) |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 191 | { |
| 192 | u32 u; |
| 193 | |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 194 | /* |
| 195 | * Set SoC-specific data. |
| 196 | */ |
Thomas Petazzoni | e96a030 | 2012-09-11 14:27:25 +0200 | [diff] [blame] | 197 | bridge_base = _bridge_base; |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 198 | bridge_timer1_clr_mask = _bridge_timer1_clr_mask; |
| 199 | |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 200 | ticks_per_jiffy = (tclk + HZ/2) / HZ; |
| 201 | |
Stefan Agner | 8a3269f | 2009-05-12 10:30:41 -0700 | [diff] [blame] | 202 | /* |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 203 | * Set scale and timer for sched_clock. |
Stefan Agner | 8a3269f | 2009-05-12 10:30:41 -0700 | [diff] [blame] | 204 | */ |
Stephen Boyd | b44653b | 2013-11-15 15:26:24 -0800 | [diff] [blame] | 205 | sched_clock_register(orion_read_sched_clock, 32, tclk); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 206 | |
| 207 | /* |
| 208 | * Setup free-running clocksource timer (interrupts |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 209 | * disabled). |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 210 | */ |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 211 | writel(0xffffffff, timer_base + TIMER0_VAL_OFF); |
| 212 | writel(0xffffffff, timer_base + TIMER0_RELOAD_OFF); |
| 213 | u = readl(bridge_base + BRIDGE_MASK_OFF); |
| 214 | writel(u & ~BRIDGE_INT_TIMER0, bridge_base + BRIDGE_MASK_OFF); |
| 215 | u = readl(timer_base + TIMER_CTRL_OFF); |
| 216 | writel(u | TIMER0_EN | TIMER0_RELOAD_EN, timer_base + TIMER_CTRL_OFF); |
Russell King | bfe45e0 | 2011-05-08 15:33:30 +0100 | [diff] [blame] | 217 | clocksource_mmio_init(timer_base + TIMER0_VAL_OFF, "orion_clocksource", |
| 218 | tclk, 300, 32, clocksource_mmio_readl_down); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 219 | |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 220 | /* |
Lennert Buytenhek | 4ee1f6b | 2010-10-15 16:50:26 +0200 | [diff] [blame] | 221 | * Setup clockevent timer (interrupt-driven). |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 222 | */ |
| 223 | setup_irq(irq, &orion_timer_irq); |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 224 | orion_clkevt.cpumask = cpumask_of(0); |
Shawn Guo | 838a2ae | 2013-01-12 11:50:05 +0000 | [diff] [blame] | 225 | clockevents_config_and_register(&orion_clkevt, tclk, 1, 0xfffffffe); |
Lennert Buytenhek | 2bac1de | 2008-03-27 14:51:40 -0400 | [diff] [blame] | 226 | } |