John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 1 | /* |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 2 | * This file contains driver for the Cadence Triple Timer Counter Rev 06 |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 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> |
Soren Brinkmann | 3d77b30 | 2013-07-08 09:51:38 -0700 | [diff] [blame] | 24 | #include <linux/sched_clock.h> |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 25 | |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 26 | /* |
Michal Simek | 4e2bec0 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 27 | * This driver configures the 2 16/32-bit count-up timers as follows: |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 28 | * |
| 29 | * T1: Timer 1, clocksource for generic timekeeping |
| 30 | * T2: Timer 2, clockevent source for hrtimers |
| 31 | * T3: Timer 3, <unused> |
| 32 | * |
| 33 | * The input frequency to the timer module for emulation is 2.5MHz which is |
| 34 | * common to all the timer channels (T1, T2, and T3). With a pre-scaler of 32, |
| 35 | * the timers are clocked at 78.125KHz (12.8 us resolution). |
| 36 | |
| 37 | * The input frequency to the timer module in silicon is configurable and |
| 38 | * obtained from device tree. The pre-scaler of 32 is used. |
| 39 | */ |
| 40 | |
| 41 | /* |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 42 | * Timer Register Offset Definitions of Timer 1, Increment base address by 4 |
| 43 | * and use same offsets for Timer 2 |
| 44 | */ |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 45 | #define TTC_CLK_CNTRL_OFFSET 0x00 /* Clock Control Reg, RW */ |
| 46 | #define TTC_CNT_CNTRL_OFFSET 0x0C /* Counter Control Reg, RW */ |
| 47 | #define TTC_COUNT_VAL_OFFSET 0x18 /* Counter Value Reg, RO */ |
| 48 | #define TTC_INTR_VAL_OFFSET 0x24 /* Interval Count Reg, RW */ |
| 49 | #define TTC_ISR_OFFSET 0x54 /* Interrupt Status Reg, RO */ |
| 50 | #define TTC_IER_OFFSET 0x60 /* Interrupt Enable Reg, RW */ |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 51 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 52 | #define TTC_CNT_CNTRL_DISABLE_MASK 0x1 |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 53 | |
Soren Brinkmann | 30e1e28 | 2013-05-13 10:46:38 -0700 | [diff] [blame] | 54 | #define TTC_CLK_CNTRL_CSRC_MASK (1 << 5) /* clock source */ |
Soren Brinkmann | b3e9072 | 2014-02-19 15:14:42 -0800 | [diff] [blame] | 55 | #define TTC_CLK_CNTRL_PSV_MASK 0x1e |
| 56 | #define TTC_CLK_CNTRL_PSV_SHIFT 1 |
Soren Brinkmann | 30e1e28 | 2013-05-13 10:46:38 -0700 | [diff] [blame] | 57 | |
Soren Brinkmann | 03377e5 | 2012-12-19 10:18:41 -0800 | [diff] [blame] | 58 | /* |
| 59 | * 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] | 60 | * work across most input frequency, but it may need to be more dynamic |
| 61 | */ |
| 62 | #define PRESCALE_EXPONENT 11 /* 2 ^ PRESCALE_EXPONENT = PRESCALE */ |
| 63 | #define PRESCALE 2048 /* The exponent must match this */ |
| 64 | #define CLK_CNTRL_PRESCALE ((PRESCALE_EXPONENT - 1) << 1) |
| 65 | #define CLK_CNTRL_PRESCALE_EN 1 |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 66 | #define CNT_CNTRL_RESET (1 << 4) |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 67 | |
Soren Brinkmann | b3e9072 | 2014-02-19 15:14:42 -0800 | [diff] [blame] | 68 | #define MAX_F_ERR 50 |
| 69 | |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 70 | /** |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 71 | * struct ttc_timer - This definition defines local timer structure |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 72 | * |
| 73 | * @base_addr: Base address of timer |
Soren Brinkmann | c1dcc92 | 2013-11-26 17:04:50 -0800 | [diff] [blame] | 74 | * @freq: Timer input clock frequency |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 75 | * @clk: Associated clock source |
| 76 | * @clk_rate_change_nb Notifier block for clock rate changes |
| 77 | */ |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 78 | struct ttc_timer { |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 79 | void __iomem *base_addr; |
Soren Brinkmann | c1dcc92 | 2013-11-26 17:04:50 -0800 | [diff] [blame] | 80 | unsigned long freq; |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 81 | struct clk *clk; |
| 82 | struct notifier_block clk_rate_change_nb; |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 83 | }; |
| 84 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 85 | #define to_ttc_timer(x) \ |
| 86 | container_of(x, struct ttc_timer, clk_rate_change_nb) |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 87 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 88 | struct ttc_timer_clocksource { |
Soren Brinkmann | b3e9072 | 2014-02-19 15:14:42 -0800 | [diff] [blame] | 89 | u32 scale_clk_ctrl_reg_old; |
| 90 | u32 scale_clk_ctrl_reg_new; |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 91 | struct ttc_timer ttc; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 92 | struct clocksource cs; |
| 93 | }; |
| 94 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 95 | #define to_ttc_timer_clksrc(x) \ |
| 96 | container_of(x, struct ttc_timer_clocksource, cs) |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 97 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 98 | struct ttc_timer_clockevent { |
| 99 | struct ttc_timer ttc; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 100 | struct clock_event_device ce; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 101 | }; |
| 102 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 103 | #define to_ttc_timer_clkevent(x) \ |
| 104 | container_of(x, struct ttc_timer_clockevent, ce) |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 105 | |
Soren Brinkmann | 3d77b30 | 2013-07-08 09:51:38 -0700 | [diff] [blame] | 106 | static void __iomem *ttc_sched_clock_val_reg; |
| 107 | |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 108 | /** |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 109 | * ttc_set_interval - Set the timer interval value |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 110 | * |
| 111 | * @timer: Pointer to the timer instance |
| 112 | * @cycles: Timer interval ticks |
| 113 | **/ |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 114 | static void ttc_set_interval(struct ttc_timer *timer, |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 115 | unsigned long cycles) |
| 116 | { |
| 117 | u32 ctrl_reg; |
| 118 | |
| 119 | /* Disable the counter, set the counter value and re-enable counter */ |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 120 | ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET); |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 121 | ctrl_reg |= TTC_CNT_CNTRL_DISABLE_MASK; |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 122 | writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 123 | |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 124 | writel_relaxed(cycles, timer->base_addr + TTC_INTR_VAL_OFFSET); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 125 | |
Soren Brinkmann | 03377e5 | 2012-12-19 10:18:41 -0800 | [diff] [blame] | 126 | /* |
| 127 | * Reset the counter (0x10) so that it starts from 0, one-shot |
| 128 | * mode makes this needed for timing to be right. |
| 129 | */ |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 130 | ctrl_reg |= CNT_CNTRL_RESET; |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 131 | ctrl_reg &= ~TTC_CNT_CNTRL_DISABLE_MASK; |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 132 | writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 136 | * ttc_clock_event_interrupt - Clock event timer interrupt handler |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 137 | * |
| 138 | * @irq: IRQ number of the Timer |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 139 | * @dev_id: void pointer to the ttc_timer instance |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 140 | * |
| 141 | * returns: Always IRQ_HANDLED - success |
| 142 | **/ |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 143 | static irqreturn_t ttc_clock_event_interrupt(int irq, void *dev_id) |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 144 | { |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 145 | struct ttc_timer_clockevent *ttce = dev_id; |
| 146 | struct ttc_timer *timer = &ttce->ttc; |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 147 | |
| 148 | /* Acknowledge the interrupt and call event handler */ |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 149 | readl_relaxed(timer->base_addr + TTC_ISR_OFFSET); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 150 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 151 | ttce->ce.event_handler(&ttce->ce); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 152 | |
| 153 | return IRQ_HANDLED; |
| 154 | } |
| 155 | |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 156 | /** |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 157 | * __ttc_clocksource_read - Reads the timer counter register |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 158 | * |
| 159 | * returns: Current timer counter register value |
| 160 | **/ |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 161 | static u64 __ttc_clocksource_read(struct clocksource *cs) |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 162 | { |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 163 | struct ttc_timer *timer = &to_ttc_timer_clksrc(cs)->ttc; |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 164 | |
Thomas Gleixner | a5a1d1c | 2016-12-21 20:32:01 +0100 | [diff] [blame] | 165 | return (u64)readl_relaxed(timer->base_addr + |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 166 | TTC_COUNT_VAL_OFFSET); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 167 | } |
| 168 | |
Stephen Boyd | dfded00 | 2013-11-20 00:47:32 +0100 | [diff] [blame] | 169 | static u64 notrace ttc_sched_clock_read(void) |
Soren Brinkmann | 3d77b30 | 2013-07-08 09:51:38 -0700 | [diff] [blame] | 170 | { |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 171 | return readl_relaxed(ttc_sched_clock_val_reg); |
Soren Brinkmann | 3d77b30 | 2013-07-08 09:51:38 -0700 | [diff] [blame] | 172 | } |
| 173 | |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 174 | /** |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 175 | * ttc_set_next_event - Sets the time interval for next event |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 176 | * |
| 177 | * @cycles: Timer interval ticks |
| 178 | * @evt: Address of clock event instance |
| 179 | * |
| 180 | * returns: Always 0 - success |
| 181 | **/ |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 182 | static int ttc_set_next_event(unsigned long cycles, |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 183 | struct clock_event_device *evt) |
| 184 | { |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 185 | struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt); |
| 186 | struct ttc_timer *timer = &ttce->ttc; |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 187 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 188 | ttc_set_interval(timer, cycles); |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | /** |
Viresh Kumar | 5c0a4bb | 2015-06-18 16:24:16 +0530 | [diff] [blame] | 193 | * ttc_set_{shutdown|oneshot|periodic} - Sets the state of timer |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 194 | * |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 195 | * @evt: Address of clock event instance |
| 196 | **/ |
Viresh Kumar | 5c0a4bb | 2015-06-18 16:24:16 +0530 | [diff] [blame] | 197 | static int ttc_shutdown(struct clock_event_device *evt) |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 198 | { |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 199 | struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt); |
| 200 | struct ttc_timer *timer = &ttce->ttc; |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 201 | u32 ctrl_reg; |
| 202 | |
Viresh Kumar | 5c0a4bb | 2015-06-18 16:24:16 +0530 | [diff] [blame] | 203 | ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET); |
| 204 | ctrl_reg |= TTC_CNT_CNTRL_DISABLE_MASK; |
| 205 | writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET); |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static int ttc_set_periodic(struct clock_event_device *evt) |
| 210 | { |
| 211 | struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt); |
| 212 | struct ttc_timer *timer = &ttce->ttc; |
| 213 | |
| 214 | ttc_set_interval(timer, |
| 215 | DIV_ROUND_CLOSEST(ttce->ttc.freq, PRESCALE * HZ)); |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int ttc_resume(struct clock_event_device *evt) |
| 220 | { |
| 221 | struct ttc_timer_clockevent *ttce = to_ttc_timer_clkevent(evt); |
| 222 | struct ttc_timer *timer = &ttce->ttc; |
| 223 | u32 ctrl_reg; |
| 224 | |
| 225 | ctrl_reg = readl_relaxed(timer->base_addr + TTC_CNT_CNTRL_OFFSET); |
| 226 | ctrl_reg &= ~TTC_CNT_CNTRL_DISABLE_MASK; |
| 227 | writel_relaxed(ctrl_reg, timer->base_addr + TTC_CNT_CNTRL_OFFSET); |
| 228 | return 0; |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 229 | } |
| 230 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 231 | static int ttc_rate_change_clocksource_cb(struct notifier_block *nb, |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 232 | unsigned long event, void *data) |
| 233 | { |
| 234 | struct clk_notifier_data *ndata = data; |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 235 | struct ttc_timer *ttc = to_ttc_timer(nb); |
| 236 | struct ttc_timer_clocksource *ttccs = container_of(ttc, |
| 237 | struct ttc_timer_clocksource, ttc); |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 238 | |
| 239 | switch (event) { |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 240 | case PRE_RATE_CHANGE: |
Soren Brinkmann | b3e9072 | 2014-02-19 15:14:42 -0800 | [diff] [blame] | 241 | { |
| 242 | u32 psv; |
| 243 | unsigned long factor, rate_low, rate_high; |
| 244 | |
| 245 | if (ndata->new_rate > ndata->old_rate) { |
| 246 | factor = DIV_ROUND_CLOSEST(ndata->new_rate, |
| 247 | ndata->old_rate); |
| 248 | rate_low = ndata->old_rate; |
| 249 | rate_high = ndata->new_rate; |
| 250 | } else { |
| 251 | factor = DIV_ROUND_CLOSEST(ndata->old_rate, |
| 252 | ndata->new_rate); |
| 253 | rate_low = ndata->new_rate; |
| 254 | rate_high = ndata->old_rate; |
| 255 | } |
| 256 | |
| 257 | if (!is_power_of_2(factor)) |
| 258 | return NOTIFY_BAD; |
| 259 | |
| 260 | if (abs(rate_high - (factor * rate_low)) > MAX_F_ERR) |
| 261 | return NOTIFY_BAD; |
| 262 | |
| 263 | factor = __ilog2_u32(factor); |
| 264 | |
| 265 | /* |
| 266 | * store timer clock ctrl register so we can restore it in case |
| 267 | * of an abort. |
| 268 | */ |
| 269 | ttccs->scale_clk_ctrl_reg_old = |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 270 | readl_relaxed(ttccs->ttc.base_addr + |
| 271 | TTC_CLK_CNTRL_OFFSET); |
Soren Brinkmann | b3e9072 | 2014-02-19 15:14:42 -0800 | [diff] [blame] | 272 | |
| 273 | psv = (ttccs->scale_clk_ctrl_reg_old & |
| 274 | TTC_CLK_CNTRL_PSV_MASK) >> |
| 275 | TTC_CLK_CNTRL_PSV_SHIFT; |
| 276 | if (ndata->new_rate < ndata->old_rate) |
| 277 | psv -= factor; |
| 278 | else |
| 279 | psv += factor; |
| 280 | |
| 281 | /* prescaler within legal range? */ |
| 282 | if (psv & ~(TTC_CLK_CNTRL_PSV_MASK >> TTC_CLK_CNTRL_PSV_SHIFT)) |
| 283 | return NOTIFY_BAD; |
| 284 | |
| 285 | ttccs->scale_clk_ctrl_reg_new = ttccs->scale_clk_ctrl_reg_old & |
| 286 | ~TTC_CLK_CNTRL_PSV_MASK; |
| 287 | ttccs->scale_clk_ctrl_reg_new |= psv << TTC_CLK_CNTRL_PSV_SHIFT; |
| 288 | |
| 289 | |
| 290 | /* scale down: adjust divider in post-change notification */ |
| 291 | if (ndata->new_rate < ndata->old_rate) |
| 292 | return NOTIFY_DONE; |
| 293 | |
| 294 | /* scale up: adjust divider now - before frequency change */ |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 295 | writel_relaxed(ttccs->scale_clk_ctrl_reg_new, |
| 296 | ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET); |
Soren Brinkmann | b3e9072 | 2014-02-19 15:14:42 -0800 | [diff] [blame] | 297 | break; |
| 298 | } |
| 299 | case POST_RATE_CHANGE: |
| 300 | /* scale up: pre-change notification did the adjustment */ |
| 301 | if (ndata->new_rate > ndata->old_rate) |
| 302 | return NOTIFY_OK; |
| 303 | |
| 304 | /* scale down: adjust divider now - after frequency change */ |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 305 | writel_relaxed(ttccs->scale_clk_ctrl_reg_new, |
| 306 | ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET); |
Soren Brinkmann | b3e9072 | 2014-02-19 15:14:42 -0800 | [diff] [blame] | 307 | break; |
| 308 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 309 | case ABORT_RATE_CHANGE: |
Soren Brinkmann | b3e9072 | 2014-02-19 15:14:42 -0800 | [diff] [blame] | 310 | /* we have to undo the adjustment in case we scale up */ |
| 311 | if (ndata->new_rate < ndata->old_rate) |
| 312 | return NOTIFY_OK; |
| 313 | |
| 314 | /* restore original register value */ |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 315 | writel_relaxed(ttccs->scale_clk_ctrl_reg_old, |
| 316 | ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET); |
Soren Brinkmann | b3e9072 | 2014-02-19 15:14:42 -0800 | [diff] [blame] | 317 | /* fall through */ |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 318 | default: |
| 319 | return NOTIFY_DONE; |
| 320 | } |
Soren Brinkmann | b3e9072 | 2014-02-19 15:14:42 -0800 | [diff] [blame] | 321 | |
| 322 | return NOTIFY_DONE; |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 325 | static int __init ttc_setup_clocksource(struct clk *clk, void __iomem *base, |
Michal Simek | 4e2bec0 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 326 | u32 timer_width) |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 327 | { |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 328 | struct ttc_timer_clocksource *ttccs; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 329 | int err; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 330 | |
| 331 | ttccs = kzalloc(sizeof(*ttccs), GFP_KERNEL); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 332 | if (!ttccs) |
| 333 | return -ENOMEM; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 334 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 335 | ttccs->ttc.clk = clk; |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 336 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 337 | err = clk_prepare_enable(ttccs->ttc.clk); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 338 | if (err) { |
Michal Simek | c5263bb | 2013-03-20 10:24:59 +0100 | [diff] [blame] | 339 | kfree(ttccs); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 340 | return err; |
Michal Simek | c5263bb | 2013-03-20 10:24:59 +0100 | [diff] [blame] | 341 | } |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 342 | |
Soren Brinkmann | c1dcc92 | 2013-11-26 17:04:50 -0800 | [diff] [blame] | 343 | ttccs->ttc.freq = clk_get_rate(ttccs->ttc.clk); |
| 344 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 345 | ttccs->ttc.clk_rate_change_nb.notifier_call = |
| 346 | ttc_rate_change_clocksource_cb; |
| 347 | ttccs->ttc.clk_rate_change_nb.next = NULL; |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 348 | |
| 349 | err = clk_notifier_register(ttccs->ttc.clk, |
| 350 | &ttccs->ttc.clk_rate_change_nb); |
| 351 | if (err) |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 352 | pr_warn("Unable to register clock notifier.\n"); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 353 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 354 | ttccs->ttc.base_addr = base; |
| 355 | ttccs->cs.name = "ttc_clocksource"; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 356 | ttccs->cs.rating = 200; |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 357 | ttccs->cs.read = __ttc_clocksource_read; |
Michal Simek | 4e2bec0 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 358 | ttccs->cs.mask = CLOCKSOURCE_MASK(timer_width); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 359 | ttccs->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS; |
| 360 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 361 | /* |
| 362 | * Setup the clock source counter to be an incrementing counter |
| 363 | * with no interrupt and it rolls over at 0xFFFF. Pre-scale |
| 364 | * it by 32 also. Let it start running now. |
| 365 | */ |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 366 | writel_relaxed(0x0, ttccs->ttc.base_addr + TTC_IER_OFFSET); |
| 367 | writel_relaxed(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN, |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 368 | ttccs->ttc.base_addr + TTC_CLK_CNTRL_OFFSET); |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 369 | writel_relaxed(CNT_CNTRL_RESET, |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 370 | ttccs->ttc.base_addr + TTC_CNT_CNTRL_OFFSET); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 371 | |
Soren Brinkmann | c1dcc92 | 2013-11-26 17:04:50 -0800 | [diff] [blame] | 372 | err = clocksource_register_hz(&ttccs->cs, ttccs->ttc.freq / PRESCALE); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 373 | if (err) { |
Michal Simek | c5263bb | 2013-03-20 10:24:59 +0100 | [diff] [blame] | 374 | kfree(ttccs); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 375 | return err; |
Michal Simek | c5263bb | 2013-03-20 10:24:59 +0100 | [diff] [blame] | 376 | } |
Soren Brinkmann | 3d77b30 | 2013-07-08 09:51:38 -0700 | [diff] [blame] | 377 | |
| 378 | ttc_sched_clock_val_reg = base + TTC_COUNT_VAL_OFFSET; |
Michal Simek | 4e2bec0 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 379 | sched_clock_register(ttc_sched_clock_read, timer_width, |
| 380 | ttccs->ttc.freq / PRESCALE); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 381 | |
| 382 | return 0; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 383 | } |
| 384 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 385 | static int ttc_rate_change_clockevent_cb(struct notifier_block *nb, |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 386 | unsigned long event, void *data) |
| 387 | { |
| 388 | struct clk_notifier_data *ndata = data; |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 389 | struct ttc_timer *ttc = to_ttc_timer(nb); |
| 390 | struct ttc_timer_clockevent *ttcce = container_of(ttc, |
| 391 | struct ttc_timer_clockevent, ttc); |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 392 | |
| 393 | switch (event) { |
| 394 | case POST_RATE_CHANGE: |
Soren Brinkmann | c1dcc92 | 2013-11-26 17:04:50 -0800 | [diff] [blame] | 395 | /* update cached frequency */ |
| 396 | ttc->freq = ndata->new_rate; |
| 397 | |
Soren Brinkmann | 5f0ba3b | 2014-02-19 15:14:41 -0800 | [diff] [blame] | 398 | clockevents_update_freq(&ttcce->ce, ndata->new_rate / PRESCALE); |
| 399 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 400 | /* fall through */ |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 401 | case PRE_RATE_CHANGE: |
| 402 | case ABORT_RATE_CHANGE: |
| 403 | default: |
| 404 | return NOTIFY_DONE; |
| 405 | } |
| 406 | } |
| 407 | |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 408 | static int __init ttc_setup_clockevent(struct clk *clk, |
| 409 | void __iomem *base, u32 irq) |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 410 | { |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 411 | struct ttc_timer_clockevent *ttcce; |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 412 | int err; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 413 | |
| 414 | ttcce = kzalloc(sizeof(*ttcce), GFP_KERNEL); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 415 | if (!ttcce) |
| 416 | return -ENOMEM; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 417 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 418 | ttcce->ttc.clk = clk; |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 419 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 420 | err = clk_prepare_enable(ttcce->ttc.clk); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 421 | if (err) { |
Michal Simek | c5263bb | 2013-03-20 10:24:59 +0100 | [diff] [blame] | 422 | kfree(ttcce); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 423 | return err; |
Michal Simek | c5263bb | 2013-03-20 10:24:59 +0100 | [diff] [blame] | 424 | } |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 425 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 426 | ttcce->ttc.clk_rate_change_nb.notifier_call = |
| 427 | ttc_rate_change_clockevent_cb; |
| 428 | ttcce->ttc.clk_rate_change_nb.next = NULL; |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 429 | |
| 430 | err = clk_notifier_register(ttcce->ttc.clk, |
| 431 | &ttcce->ttc.clk_rate_change_nb); |
| 432 | if (err) { |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 433 | pr_warn("Unable to register clock notifier.\n"); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 434 | return err; |
| 435 | } |
| 436 | |
Soren Brinkmann | c1dcc92 | 2013-11-26 17:04:50 -0800 | [diff] [blame] | 437 | ttcce->ttc.freq = clk_get_rate(ttcce->ttc.clk); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 438 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 439 | ttcce->ttc.base_addr = base; |
| 440 | ttcce->ce.name = "ttc_clockevent"; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 441 | ttcce->ce.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT; |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 442 | ttcce->ce.set_next_event = ttc_set_next_event; |
Viresh Kumar | 5c0a4bb | 2015-06-18 16:24:16 +0530 | [diff] [blame] | 443 | ttcce->ce.set_state_shutdown = ttc_shutdown; |
| 444 | ttcce->ce.set_state_periodic = ttc_set_periodic; |
| 445 | ttcce->ce.set_state_oneshot = ttc_shutdown; |
| 446 | ttcce->ce.tick_resume = ttc_resume; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 447 | ttcce->ce.rating = 200; |
| 448 | ttcce->ce.irq = irq; |
Soren Brinkmann | 87e4ee7 | 2012-12-19 10:18:42 -0800 | [diff] [blame] | 449 | ttcce->ce.cpumask = cpu_possible_mask; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 450 | |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 451 | /* |
| 452 | * Setup the clock event timer to be an interval timer which |
| 453 | * is prescaled by 32 using the interval interrupt. Leave it |
| 454 | * disabled for now. |
| 455 | */ |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 456 | writel_relaxed(0x23, ttcce->ttc.base_addr + TTC_CNT_CNTRL_OFFSET); |
| 457 | writel_relaxed(CLK_CNTRL_PRESCALE | CLK_CNTRL_PRESCALE_EN, |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 458 | ttcce->ttc.base_addr + TTC_CLK_CNTRL_OFFSET); |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 459 | writel_relaxed(0x1, ttcce->ttc.base_addr + TTC_IER_OFFSET); |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 460 | |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 461 | err = request_irq(irq, ttc_clock_event_interrupt, |
Michael Opdenacker | 38c30a8 | 2013-12-09 10:12:10 +0100 | [diff] [blame] | 462 | IRQF_TIMER, ttcce->ce.name, ttcce); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 463 | if (err) { |
Michal Simek | c5263bb | 2013-03-20 10:24:59 +0100 | [diff] [blame] | 464 | kfree(ttcce); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 465 | return err; |
Michal Simek | c5263bb | 2013-03-20 10:24:59 +0100 | [diff] [blame] | 466 | } |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 467 | |
| 468 | clockevents_config_and_register(&ttcce->ce, |
Soren Brinkmann | c1dcc92 | 2013-11-26 17:04:50 -0800 | [diff] [blame] | 469 | ttcce->ttc.freq / PRESCALE, 1, 0xfffe); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 470 | |
| 471 | return 0; |
Josh Cartwright | 91dc985 | 2012-10-31 13:56:14 -0600 | [diff] [blame] | 472 | } |
| 473 | |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 474 | /** |
Michal Simek | 9e09dc5 | 2013-03-27 12:05:28 +0100 | [diff] [blame] | 475 | * ttc_timer_init - Initialize the timer |
John Linn | b85a3ef | 2011-06-20 11:47:27 -0600 | [diff] [blame] | 476 | * |
| 477 | * Initializes the timer hardware and register the clock source and clock event |
| 478 | * timers with Linux kernal timer framework |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 479 | */ |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 480 | static int __init ttc_timer_init(struct device_node *timer) |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 481 | { |
| 482 | unsigned int irq; |
| 483 | void __iomem *timer_baseaddr; |
Soren Brinkmann | 30e1e28 | 2013-05-13 10:46:38 -0700 | [diff] [blame] | 484 | struct clk *clk_cs, *clk_ce; |
Michal Simek | c5263bb | 2013-03-20 10:24:59 +0100 | [diff] [blame] | 485 | static int initialized; |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 486 | int clksel, ret; |
Michal Simek | 4e2bec0 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 487 | u32 timer_width = 16; |
Michal Simek | c5263bb | 2013-03-20 10:24:59 +0100 | [diff] [blame] | 488 | |
| 489 | if (initialized) |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 490 | return 0; |
Michal Simek | c5263bb | 2013-03-20 10:24:59 +0100 | [diff] [blame] | 491 | |
| 492 | initialized = 1; |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 493 | |
| 494 | /* |
| 495 | * Get the 1st Triple Timer Counter (TTC) block from the device tree |
| 496 | * and use it. Note that the event timer uses the interrupt and it's the |
| 497 | * 2nd TTC hence the irq_of_parse_and_map(,1) |
| 498 | */ |
| 499 | timer_baseaddr = of_iomap(timer, 0); |
| 500 | if (!timer_baseaddr) { |
| 501 | pr_err("ERROR: invalid timer base address\n"); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 502 | return -ENXIO; |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | irq = irq_of_parse_and_map(timer, 1); |
| 506 | if (irq <= 0) { |
| 507 | pr_err("ERROR: invalid interrupt number\n"); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 508 | return -EINVAL; |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 509 | } |
| 510 | |
Michal Simek | 4e2bec0 | 2014-09-29 01:50:05 +0200 | [diff] [blame] | 511 | of_property_read_u32(timer, "timer-width", &timer_width); |
| 512 | |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 513 | clksel = readl_relaxed(timer_baseaddr + TTC_CLK_CNTRL_OFFSET); |
Soren Brinkmann | 30e1e28 | 2013-05-13 10:46:38 -0700 | [diff] [blame] | 514 | clksel = !!(clksel & TTC_CLK_CNTRL_CSRC_MASK); |
| 515 | clk_cs = of_clk_get(timer, clksel); |
| 516 | if (IS_ERR(clk_cs)) { |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 517 | pr_err("ERROR: timer input clock not found\n"); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 518 | return PTR_ERR(clk_cs); |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Michal Simek | 87ab436 | 2014-04-11 15:39:29 +0200 | [diff] [blame] | 521 | clksel = readl_relaxed(timer_baseaddr + 4 + TTC_CLK_CNTRL_OFFSET); |
Soren Brinkmann | 30e1e28 | 2013-05-13 10:46:38 -0700 | [diff] [blame] | 522 | clksel = !!(clksel & TTC_CLK_CNTRL_CSRC_MASK); |
| 523 | clk_ce = of_clk_get(timer, clksel); |
| 524 | if (IS_ERR(clk_ce)) { |
| 525 | pr_err("ERROR: timer input clock not found\n"); |
Christophe Jaillet | 34c720a | 2016-07-06 07:35:23 +0200 | [diff] [blame] | 526 | return PTR_ERR(clk_ce); |
Soren Brinkmann | 30e1e28 | 2013-05-13 10:46:38 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 529 | ret = ttc_setup_clocksource(clk_cs, timer_baseaddr, timer_width); |
| 530 | if (ret) |
| 531 | return ret; |
| 532 | |
| 533 | ret = ttc_setup_clockevent(clk_ce, timer_baseaddr + 4, irq); |
| 534 | if (ret) |
| 535 | return ret; |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 536 | |
| 537 | pr_info("%s #0 at %p, irq=%d\n", timer->name, timer_baseaddr, irq); |
Daniel Lezcano | 70504f3 | 2016-05-31 19:52:09 +0200 | [diff] [blame] | 538 | |
| 539 | return 0; |
Michal Simek | e932900 | 2013-03-20 10:15:28 +0100 | [diff] [blame] | 540 | } |
| 541 | |
Daniel Lezcano | 177cf6e | 2016-06-07 00:27:44 +0200 | [diff] [blame] | 542 | CLOCKSOURCE_OF_DECLARE(ttc, "cdns,ttc", ttc_timer_init); |