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