John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file contains driver for the Xilinx PS Timer Counter IP. |
| 3 | * |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 4 | * Copyright (C) 2011-2013 Xilinx |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 5 | * |
| 6 | * based on arch/mips/kernel/time.c timer driver |
| 7 | * |
| 8 | * This software is licensed under the terms of the GNU General Public |
| 9 | * License version 2, as published by the Free Software Foundation, and |
| 10 | * may be copied, distributed, and modified under those terms. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 18 | #include <linux/clk.h> |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 19 | #include <linux/interrupt.h> |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 20 | #include <linux/clockchips.h> |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 21 | #include <linux/of_address.h> |
| 22 | #include <linux/of_irq.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/clk-provider.h> |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 25 | #include "common.h" |
| 26 | |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 27 | /* |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 28 | * This driver configures the 2 16-bit count-up timers as follows: |
| 29 | * |
| 30 | * T1: Timer 1, clocksource for generic timekeeping |
| 31 | * T2: Timer 2, clockevent source for hrtimers |
| 32 | * T3: Timer 3, <unused> |
| 33 | * |
| 34 | * The input frequency to the timer module for emulation is 2.5MHz which is |
| 35 | * common to all the timer channels (T1, T2, and T3). With a pre-scaler of 32, |
| 36 | * the timers are clocked at 78.125KHz (12.8 us resolution). |
| 37 | |
| 38 | * The input frequency to the timer module in silicon is configurable and |
| 39 | * obtained from device tree. The pre-scaler of 32 is used. |
| 40 | */ |
| 41 | |
| 42 | /* |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 43 | * Timer Register Offset Definitions of Timer 1, Increment base address by 4 |
| 44 | * and use same offsets for Timer 2 |
| 45 | */ |
Soren Brinkmann | d16aaf4 | 2012-12-19 10:18:39 -0800 | [diff] [blame] | 46 | #define XTTCPS_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */ |
| 47 | #define XTTCPS_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */ |
| 48 | #define XTTCPS_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */ |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 49 | #define XTTCPS_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */ |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 50 | #define XTTCPS_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */ |
| 51 | #define XTTCPS_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */ |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 52 | |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 53 | #define XTTCPS_CNT_CNTRL_DISABLE_MASK 0x1 |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 54 | |
Soren Brinkmann | 03377e5 | 2012-12-19 10:18:41 -0800 | [diff] [blame] | 55 | /* |
| 56 | * Setup the timers to use pre-scaling, using a fixed value for now that will |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 57 | * work across most input frequency, but it may need to be more dynamic |
| 58 | */ |
| 59 | #define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */ |
| 60 | #define PRESCALE 2048 /* The exponent must match this */ |
| 61 | #define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1) |
| 62 | #define CLK_CNTRL_PRESCALE_EN 1 |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 63 | #define CNT_CNTRL_RESET (1 << 4) |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 64 | |
| 65 | /** |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 66 | * struct xttcps_timer - This definition defines local timer structure |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 67 | * |
| 68 | * @base_addr: Base address of timer |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 69 | * @clk: Associated clock source |
| 70 | * @clk_rate_change_nb Notifier block for clock rate changes |
| 71 | */ |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 72 | struct xttcps_timer { |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 73 | void __iomem *base_addr; |
| 74 | struct clk *clk; |
| 75 | struct notifier_block clk_rate_change_nb; |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 76 | }; |
| 77 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 78 | #define to_xttcps_timer(x) \ |
| 79 | container_of(x, struct xttcps_timer, clk_rate_change_nb) |
| 80 | |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 81 | struct xttcps_timer_clocksource { |
| 82 | struct xttcps_timer xttc; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 83 | struct clocksource cs; |
| 84 | }; |
| 85 | |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 86 | #define to_xttcps_timer_clksrc(x) \ |
| 87 | container_of(x, struct xttcps_timer_clocksource, cs) |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 88 | |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 89 | struct xttcps_timer_clockevent { |
| 90 | struct xttcps_timer xttc; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 91 | struct clock_event_device ce; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 92 | }; |
| 93 | |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 94 | #define to_xttcps_timer_clkevent(x) \ |
| 95 | container_of(x, struct xttcps_timer_clockevent, ce) |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 96 | |
| 97 | /** |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 98 | * xttcps_set_interval - Set the timer interval value |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 99 | * |
| 100 | * @timer: Pointer to the timer instance |
| 101 | * @cycles: Timer interval ticks |
| 102 | **/ |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 103 | static void xttcps_set_interval(struct xttcps_timer *timer, |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 104 | unsigned long cycles) |
| 105 | { |
| 106 | u32 ctrl_reg; |
| 107 | |
| 108 | /* Disable the counter, set the counter value and re-enable counter */ |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 109 | ctrl_reg = __raw_readl(timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET); |
| 110 | ctrl_reg |= XTTCPS_CNT_CNTRL_DISABLE_MASK; |
| 111 | __raw_writel(ctrl_reg, timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 112 | |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 113 | __raw_writel(cycles, timer->base_addr + XTTCPS_INTR_VAL_OFFSET); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 114 | |
Soren Brinkmann | 03377e5 | 2012-12-19 10:18:41 -0800 | [diff] [blame] | 115 | /* |
| 116 | * Reset the counter (0x10) so that it starts from 0, one-shot |
| 117 | * mode makes this needed for timing to be right. |
| 118 | */ |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 119 | ctrl_reg |= CNT_CNTRL_RESET; |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 120 | ctrl_reg &= ~XTTCPS_CNT_CNTRL_DISABLE_MASK; |
| 121 | __raw_writel(ctrl_reg, timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 125 | * xttcps_clock_event_interrupt - Clock event timer interrupt handler |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 126 | * |
| 127 | * @irq: IRQ number of the Timer |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 128 | * @dev_id: void pointer to the xttcps_timer instance |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 129 | * |
| 130 | * returns: Always IRQ_HANDLED - success |
| 131 | **/ |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 132 | static irqreturn_t xttcps_clock_event_interrupt(int irq, void *dev_id) |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 133 | { |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 134 | struct xttcps_timer_clockevent *xttce = dev_id; |
| 135 | struct xttcps_timer *timer = &xttce->xttc; |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 136 | |
| 137 | /* Acknowledge the interrupt and call event handler */ |
Soren Brinkmann | af7f032 | 2012-12-19 10:18:37 -0800 | [diff] [blame] | 138 | __raw_readl(timer->base_addr + XTTCPS_ISR_OFFSET); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 139 | |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 140 | xttce->ce.event_handler(&xttce->ce); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 141 | |
| 142 | return IRQ_HANDLED; |
| 143 | } |
| 144 | |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 145 | /** |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 146 | * __xttc_clocksource_read - Reads the timer counter register |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 147 | * |
| 148 | * returns: Current timer counter register value |
| 149 | **/ |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 150 | static cycle_t __xttc_clocksource_read(struct clocksource *cs) |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 151 | { |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 152 | struct xttcps_timer *timer = &to_xttcps_timer_clksrc(cs)->xttc; |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 153 | |
| 154 | return (cycle_t)__raw_readl(timer->base_addr + |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 155 | XTTCPS_COUNT_VAL_OFFSET); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 156 | } |
| 157 | |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 158 | /** |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 159 | * xttcps_set_next_event - Sets the time interval for next event |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 160 | * |
| 161 | * @cycles: Timer interval ticks |
| 162 | * @evt: Address of clock event instance |
| 163 | * |
| 164 | * returns: Always 0 - success |
| 165 | **/ |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 166 | static int xttcps_set_next_event(unsigned long cycles, |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 167 | struct clock_event_device *evt) |
| 168 | { |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 169 | struct xttcps_timer_clockevent *xttce = to_xttcps_timer_clkevent(evt); |
| 170 | struct xttcps_timer *timer = &xttce->xttc; |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 171 | |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 172 | xttcps_set_interval(timer, cycles); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /** |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 177 | * xttcps_set_mode - Sets the mode of timer |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 178 | * |
| 179 | * @mode: Mode to be set |
| 180 | * @evt: Address of clock event instance |
| 181 | **/ |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 182 | static void xttcps_set_mode(enum clock_event_mode mode, |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 183 | struct clock_event_device *evt) |
| 184 | { |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 185 | struct xttcps_timer_clockevent *xttce = to_xttcps_timer_clkevent(evt); |
| 186 | struct xttcps_timer *timer = &xttce->xttc; |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 187 | u32 ctrl_reg; |
| 188 | |
| 189 | switch (mode) { |
| 190 | case CLOCK_EVT_MODE_PERIODIC: |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 191 | xttcps_set_interval(timer, |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 192 | DIV_ROUND_CLOSEST(clk_get_rate(xttce->xttc.clk), |
| 193 | PRESCALE * HZ)); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 194 | break; |
| 195 | case CLOCK_EVT_MODE_ONESHOT: |
| 196 | case CLOCK_EVT_MODE_UNUSED: |
| 197 | case CLOCK_EVT_MODE_SHUTDOWN: |
| 198 | ctrl_reg = __raw_readl(timer->base_addr + |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 199 | XTTCPS_CNT_CNTRL_OFFSET); |
| 200 | ctrl_reg |= XTTCPS_CNT_CNTRL_DISABLE_MASK; |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 201 | __raw_writel(ctrl_reg, |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 202 | timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 203 | break; |
| 204 | case CLOCK_EVT_MODE_RESUME: |
| 205 | ctrl_reg = __raw_readl(timer->base_addr + |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 206 | XTTCPS_CNT_CNTRL_OFFSET); |
| 207 | ctrl_reg &= ~XTTCPS_CNT_CNTRL_DISABLE_MASK; |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 208 | __raw_writel(ctrl_reg, |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 209 | timer->base_addr + XTTCPS_CNT_CNTRL_OFFSET); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 210 | break; |
| 211 | } |
| 212 | } |
| 213 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 214 | static int xttcps_rate_change_clocksource_cb(struct notifier_block *nb, |
| 215 | unsigned long event, void *data) |
| 216 | { |
| 217 | struct clk_notifier_data *ndata = data; |
| 218 | struct xttcps_timer *xttcps = to_xttcps_timer(nb); |
| 219 | struct xttcps_timer_clocksource *xttccs = container_of(xttcps, |
| 220 | struct xttcps_timer_clocksource, xttc); |
| 221 | |
| 222 | switch (event) { |
| 223 | case POST_RATE_CHANGE: |
| 224 | /* |
| 225 | * Do whatever is necessary to maintain a proper time base |
| 226 | * |
| 227 | * I cannot find a way to adjust the currently used clocksource |
| 228 | * to the new frequency. __clocksource_updatefreq_hz() sounds |
| 229 | * good, but does not work. Not sure what's that missing. |
| 230 | * |
| 231 | * This approach works, but triggers two clocksource switches. |
| 232 | * The first after unregister to clocksource jiffies. And |
| 233 | * another one after the register to the newly registered timer. |
| 234 | * |
| 235 | * Alternatively we could 'waste' another HW timer to ping pong |
| 236 | * between clock sources. That would also use one register and |
| 237 | * one unregister call, but only trigger one clocksource switch |
| 238 | * for the cost of another HW timer used by the OS. |
| 239 | */ |
| 240 | clocksource_unregister(&xttccs->cs); |
| 241 | clocksource_register_hz(&xttccs->cs, |
| 242 | ndata->new_rate / PRESCALE); |
| 243 | /* fall through */ |
| 244 | case PRE_RATE_CHANGE: |
| 245 | case ABORT_RATE_CHANGE: |
| 246 | default: |
| 247 | return NOTIFY_DONE; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | static void __init xttc_setup_clocksource(struct clk *clk, void __iomem *base) |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 252 | { |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 253 | struct xttcps_timer_clocksource *ttccs; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 254 | int err; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 255 | |
| 256 | ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL); |
| 257 | if (WARN_ON(!ttccs)) |
| 258 | return; |
| 259 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 260 | ttccs->xttc.clk = clk; |
| 261 | |
| 262 | err = clk_prepare_enable(ttccs->xttc.clk); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 263 | if (WARN_ON(err)) |
| 264 | return; |
| 265 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 266 | ttccs->xttc.clk_rate_change_nb.notifier_call = |
| 267 | xttcps_rate_change_clocksource_cb; |
| 268 | ttccs->xttc.clk_rate_change_nb.next = NULL; |
| 269 | if (clk_notifier_register(ttccs->xttc.clk, |
| 270 | &ttccs->xttc.clk_rate_change_nb)) |
| 271 | pr_warn("Unable to register clock notifier.\n"); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 272 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 273 | ttccs->xttc.base_addr = base; |
| 274 | ttccs->cs.name = "xttcps_clocksource"; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 275 | ttccs->cs.rating = 200; |
| 276 | ttccs->cs.read = __xttc_clocksource_read; |
| 277 | ttccs->cs.mask = CLOCKSOURCE_MASK(16); |
| 278 | ttccs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS; |
| 279 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 280 | /* |
| 281 | * Setup the clock source counter to be an incrementing counter |
| 282 | * with no interrupt and it rolls over at 0xFFFF. Pre-scale |
| 283 | * it by 32 also. Let it start running now. |
| 284 | */ |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 285 | __raw_writel(0x0, ttccs->xttc.base_addr + XTTCPS_IER_OFFSET); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 286 | __raw_writel(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN, |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 287 | ttccs->xttc.base_addr + XTTCPS_CLK_CNTRL_OFFSET); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 288 | __raw_writel(CNT_CNTRL_RESET, |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 289 | ttccs->xttc.base_addr + XTTCPS_CNT_CNTRL_OFFSET); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 290 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 291 | err = clocksource_register_hz(&ttccs->cs, |
| 292 | clk_get_rate(ttccs->xttc.clk) / PRESCALE); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 293 | if (WARN_ON(err)) |
| 294 | return; |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 295 | |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 296 | } |
| 297 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 298 | static int xttcps_rate_change_clockevent_cb(struct notifier_block *nb, |
| 299 | unsigned long event, void *data) |
| 300 | { |
| 301 | struct clk_notifier_data *ndata = data; |
| 302 | struct xttcps_timer *xttcps = to_xttcps_timer(nb); |
| 303 | struct xttcps_timer_clockevent *xttcce = container_of(xttcps, |
| 304 | struct xttcps_timer_clockevent, xttc); |
| 305 | |
| 306 | switch (event) { |
| 307 | case POST_RATE_CHANGE: |
| 308 | { |
| 309 | unsigned long flags; |
| 310 | |
| 311 | /* |
| 312 | * clockevents_update_freq should be called with IRQ disabled on |
| 313 | * the CPU the timer provides events for. The timer we use is |
| 314 | * common to both CPUs, not sure if we need to run on both |
| 315 | * cores. |
| 316 | */ |
| 317 | local_irq_save(flags); |
| 318 | clockevents_update_freq(&xttcce->ce, |
| 319 | ndata->new_rate / PRESCALE); |
| 320 | local_irq_restore(flags); |
| 321 | |
| 322 | /* fall through */ |
| 323 | } |
| 324 | case PRE_RATE_CHANGE: |
| 325 | case ABORT_RATE_CHANGE: |
| 326 | default: |
| 327 | return NOTIFY_DONE; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | static void __init xttc_setup_clockevent(struct clk *clk, |
| 332 | void __iomem *base, u32 irq) |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 333 | { |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 334 | struct xttcps_timer_clockevent *ttcce; |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 335 | int err; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 336 | |
| 337 | ttcce = kzalloc(sizeof(*ttcce), GFP_KERNEL); |
| 338 | if (WARN_ON(!ttcce)) |
| 339 | return; |
| 340 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 341 | ttcce->xttc.clk = clk; |
| 342 | |
| 343 | err = clk_prepare_enable(ttcce->xttc.clk); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 344 | if (WARN_ON(err)) |
| 345 | return; |
| 346 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 347 | ttcce->xttc.clk_rate_change_nb.notifier_call = |
| 348 | xttcps_rate_change_clockevent_cb; |
| 349 | ttcce->xttc.clk_rate_change_nb.next = NULL; |
| 350 | if (clk_notifier_register(ttcce->xttc.clk, |
| 351 | &ttcce->xttc.clk_rate_change_nb)) |
| 352 | pr_warn("Unable to register clock notifier.\n"); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 353 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 354 | ttcce->xttc.base_addr = base; |
| 355 | ttcce->ce.name = "xttcps_clockevent"; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 356 | ttcce->ce.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT; |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 357 | ttcce->ce.set_next_event = xttcps_set_next_event; |
| 358 | ttcce->ce.set_mode = xttcps_set_mode; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 359 | ttcce->ce.rating = 200; |
| 360 | ttcce->ce.irq = irq; |
Soren Brinkmann | 87e4ee7 | 2012-12-19 10:18:42 -0800 | [diff] [blame] | 361 | ttcce->ce.cpumask = cpu_possible_mask; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 362 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 363 | /* |
| 364 | * Setup the clock event timer to be an interval timer which |
| 365 | * is prescaled by 32 using the interval interrupt. Leave it |
| 366 | * disabled for now. |
| 367 | */ |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 368 | __raw_writel(0x23, ttcce->xttc.base_addr + XTTCPS_CNT_CNTRL_OFFSET); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 369 | __raw_writel(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN, |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 370 | ttcce->xttc.base_addr + XTTCPS_CLK_CNTRL_OFFSET); |
| 371 | __raw_writel(0x1, ttcce->xttc.base_addr + XTTCPS_IER_OFFSET); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 372 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 373 | err = request_irq(irq, xttcps_clock_event_interrupt, |
| 374 | IRQF_DISABLED | IRQF_TIMER, |
| 375 | ttcce->ce.name, ttcce); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 376 | if (WARN_ON(err)) |
| 377 | return; |
| 378 | |
| 379 | clockevents_config_and_register(&ttcce->ce, |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 380 | clk_get_rate(ttcce->xttc.clk) / PRESCALE, 1, 0xfffe); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 381 | } |
| 382 | |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 383 | /** |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 384 | * xttcps_timer_init - Initialize the timer |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 385 | * |
| 386 | * Initializes the timer hardware and register the clock source and clock event |
| 387 | * timers with Linux kernal timer framework |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 388 | */ |
| 389 | static void __init xttcps_timer_init_of(struct device_node *timer) |
| 390 | { |
| 391 | unsigned int irq; |
| 392 | void __iomem *timer_baseaddr; |
| 393 | struct clk *clk; |
| 394 | |
| 395 | /* |
| 396 | * Get the 1st Triple Timer Counter (TTC) block from the device tree |
| 397 | * and use it. Note that the event timer uses the interrupt and it's the |
| 398 | * 2nd TTC hence the irq_of_parse_and_map(,1) |
| 399 | */ |
| 400 | timer_baseaddr = of_iomap(timer, 0); |
| 401 | if (!timer_baseaddr) { |
| 402 | pr_err("ERROR: invalid timer base address\n"); |
| 403 | BUG(); |
| 404 | } |
| 405 | |
| 406 | irq = irq_of_parse_and_map(timer, 1); |
| 407 | if (irq <= 0) { |
| 408 | pr_err("ERROR: invalid interrupt number\n"); |
| 409 | BUG(); |
| 410 | } |
| 411 | |
| 412 | clk = of_clk_get_by_name(timer, "cpu_1x"); |
| 413 | if (IS_ERR(clk)) { |
| 414 | pr_err("ERROR: timer input clock not found\n"); |
| 415 | BUG(); |
| 416 | } |
| 417 | |
| 418 | xttc_setup_clocksource(clk, timer_baseaddr); |
| 419 | xttc_setup_clockevent(clk, timer_baseaddr + 4, irq); |
| 420 | |
| 421 | pr_info("%s #0 at %p, irq=%d\n", timer->name, timer_baseaddr, irq); |
| 422 | } |
| 423 | |
Soren Brinkmann | f184c5c | 2012-12-19 10:18:36 -0800 | [diff] [blame] | 424 | void __init xttcps_timer_init(void) |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 425 | { |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 426 | const char * const timer_list[] = { |
| 427 | "cdns,ttc", |
| 428 | NULL |
| 429 | }; |
| 430 | struct device_node *timer; |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 431 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 432 | timer = of_find_compatible_node(NULL, NULL, timer_list[0]); |
| 433 | if (!timer) { |
| 434 | pr_err("ERROR: no compatible timer found\n"); |
| 435 | BUG(); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 436 | } |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame^] | 437 | |
| 438 | xttcps_timer_init_of(timer); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 439 | } |