Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Maxime Coquelin 2015 |
| 3 | * Author: Maxime Coquelin <mcoquelin.stm32@gmail.com> |
| 4 | * License terms: GNU General Public License (GPL), version 2 |
| 5 | * |
| 6 | * Inspired by time-efm32.c from Uwe Kleine-Koenig |
| 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/clocksource.h> |
| 11 | #include <linux/clockchips.h> |
| 12 | #include <linux/irq.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/of.h> |
| 15 | #include <linux/of_address.h> |
| 16 | #include <linux/of_irq.h> |
| 17 | #include <linux/clk.h> |
| 18 | #include <linux/reset.h> |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 19 | #include <linux/slab.h> |
| 20 | |
| 21 | #include "timer-of.h" |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 22 | |
| 23 | #define TIM_CR1 0x00 |
| 24 | #define TIM_DIER 0x0c |
| 25 | #define TIM_SR 0x10 |
| 26 | #define TIM_EGR 0x14 |
| 27 | #define TIM_PSC 0x28 |
| 28 | #define TIM_ARR 0x2c |
| 29 | |
| 30 | #define TIM_CR1_CEN BIT(0) |
| 31 | #define TIM_CR1_OPM BIT(3) |
| 32 | #define TIM_CR1_ARPE BIT(7) |
| 33 | |
| 34 | #define TIM_DIER_UIE BIT(0) |
| 35 | |
| 36 | #define TIM_SR_UIF BIT(0) |
| 37 | |
| 38 | #define TIM_EGR_UG BIT(0) |
| 39 | |
Benjamin Gaignard | 4744daa | 2018-01-08 14:28:54 +0100 | [diff] [blame^] | 40 | #define TIM_PSC_MAX USHRT_MAX |
| 41 | #define TIM_PSC_CLKRATE 10000 |
| 42 | |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 43 | static int stm32_clock_event_shutdown(struct clock_event_device *clkevt) |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 44 | { |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 45 | struct timer_of *to = to_timer_of(clkevt); |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 46 | |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 47 | writel_relaxed(0, timer_of_base(to) + TIM_CR1); |
| 48 | |
Viresh Kumar | 8e8af4c | 2015-06-18 16:24:50 +0530 | [diff] [blame] | 49 | return 0; |
| 50 | } |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 51 | |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 52 | static int stm32_clock_event_set_periodic(struct clock_event_device *clkevt) |
Viresh Kumar | 8e8af4c | 2015-06-18 16:24:50 +0530 | [diff] [blame] | 53 | { |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 54 | struct timer_of *to = to_timer_of(clkevt); |
Viresh Kumar | 8e8af4c | 2015-06-18 16:24:50 +0530 | [diff] [blame] | 55 | |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 56 | writel_relaxed(timer_of_period(to), timer_of_base(to) + TIM_ARR); |
| 57 | writel_relaxed(TIM_CR1_ARPE | TIM_CR1_CEN, timer_of_base(to) + TIM_CR1); |
| 58 | |
Viresh Kumar | 8e8af4c | 2015-06-18 16:24:50 +0530 | [diff] [blame] | 59 | return 0; |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static int stm32_clock_event_set_next_event(unsigned long evt, |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 63 | struct clock_event_device *clkevt) |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 64 | { |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 65 | struct timer_of *to = to_timer_of(clkevt); |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 66 | |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 67 | writel_relaxed(evt, timer_of_base(to) + TIM_ARR); |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 68 | writel_relaxed(TIM_CR1_ARPE | TIM_CR1_OPM | TIM_CR1_CEN, |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 69 | timer_of_base(to) + TIM_CR1); |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | static irqreturn_t stm32_clock_event_handler(int irq, void *dev_id) |
| 75 | { |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 76 | struct clock_event_device *clkevt = (struct clock_event_device *)dev_id; |
| 77 | struct timer_of *to = to_timer_of(clkevt); |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 78 | |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 79 | writel_relaxed(0, timer_of_base(to) + TIM_SR); |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 80 | |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 81 | clkevt->event_handler(clkevt); |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 82 | |
| 83 | return IRQ_HANDLED; |
| 84 | } |
| 85 | |
Daniel Lezcano | 70c62cf | 2018-01-08 14:28:53 +0100 | [diff] [blame] | 86 | /** |
| 87 | * stm32_timer_width - Sort out the timer width (32/16) |
| 88 | * @to: a pointer to a timer-of structure |
| 89 | * |
| 90 | * Write the 32-bit max value and read/return the result. If the timer |
| 91 | * is 32 bits wide, the result will be UINT_MAX, otherwise it will |
| 92 | * be truncated by the 16-bit register to USHRT_MAX. |
| 93 | * |
| 94 | * Returns UINT_MAX if the timer is 32 bits wide, USHRT_MAX if it is a |
| 95 | * 16 bits wide. |
| 96 | */ |
| 97 | static u32 __init stm32_timer_width(struct timer_of *to) |
| 98 | { |
| 99 | writel_relaxed(UINT_MAX, timer_of_base(to) + TIM_ARR); |
| 100 | |
| 101 | return readl_relaxed(timer_of_base(to) + TIM_ARR); |
| 102 | } |
| 103 | |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 104 | static void __init stm32_clockevent_init(struct timer_of *to) |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 105 | { |
Daniel Lezcano | 70c62cf | 2018-01-08 14:28:53 +0100 | [diff] [blame] | 106 | u32 width = 0; |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 107 | int prescaler; |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 108 | |
Daniel Lezcano | f2ed8ef | 2018-01-08 14:28:52 +0100 | [diff] [blame] | 109 | to->clkevt.name = to->np->full_name; |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 110 | to->clkevt.features = CLOCK_EVT_FEAT_PERIODIC; |
| 111 | to->clkevt.set_state_shutdown = stm32_clock_event_shutdown; |
| 112 | to->clkevt.set_state_periodic = stm32_clock_event_set_periodic; |
| 113 | to->clkevt.set_state_oneshot = stm32_clock_event_shutdown; |
| 114 | to->clkevt.tick_resume = stm32_clock_event_shutdown; |
| 115 | to->clkevt.set_next_event = stm32_clock_event_set_next_event; |
| 116 | |
Daniel Lezcano | 70c62cf | 2018-01-08 14:28:53 +0100 | [diff] [blame] | 117 | width = stm32_timer_width(to); |
| 118 | if (width == UINT_MAX) { |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 119 | prescaler = 1; |
| 120 | to->clkevt.rating = 250; |
| 121 | } else { |
Benjamin Gaignard | 4744daa | 2018-01-08 14:28:54 +0100 | [diff] [blame^] | 122 | prescaler = DIV_ROUND_CLOSEST(timer_of_rate(to), |
| 123 | TIM_PSC_CLKRATE); |
| 124 | /* |
| 125 | * The prescaler register is an u16, the variable |
| 126 | * can't be greater than TIM_PSC_MAX, let's cap it in |
| 127 | * this case. |
| 128 | */ |
| 129 | prescaler = prescaler < TIM_PSC_MAX ? prescaler : TIM_PSC_MAX; |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 130 | to->clkevt.rating = 100; |
| 131 | } |
| 132 | writel_relaxed(0, timer_of_base(to) + TIM_ARR); |
| 133 | |
| 134 | writel_relaxed(prescaler - 1, timer_of_base(to) + TIM_PSC); |
| 135 | writel_relaxed(TIM_EGR_UG, timer_of_base(to) + TIM_EGR); |
| 136 | writel_relaxed(0, timer_of_base(to) + TIM_SR); |
| 137 | writel_relaxed(TIM_DIER_UIE, timer_of_base(to) + TIM_DIER); |
| 138 | |
| 139 | /* Adjust rate and period given the prescaler value */ |
| 140 | to->of_clk.rate = DIV_ROUND_CLOSEST(to->of_clk.rate, prescaler); |
| 141 | to->of_clk.period = DIV_ROUND_UP(to->of_clk.rate, HZ); |
| 142 | |
| 143 | clockevents_config_and_register(&to->clkevt, |
Daniel Lezcano | 70c62cf | 2018-01-08 14:28:53 +0100 | [diff] [blame] | 144 | timer_of_rate(to), 0x1, width); |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 145 | |
| 146 | pr_info("%pOF: STM32 clockevent driver initialized (%d bits)\n", |
Daniel Lezcano | 70c62cf | 2018-01-08 14:28:53 +0100 | [diff] [blame] | 147 | to->np, width == UINT_MAX ? 32 : 16); |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static int __init stm32_timer_init(struct device_node *node) |
| 151 | { |
| 152 | struct reset_control *rstc; |
| 153 | struct timer_of *to; |
| 154 | int ret; |
| 155 | |
| 156 | to = kzalloc(sizeof(*to), GFP_KERNEL); |
| 157 | if (!to) |
Daniel Lezcano | e0aeca3 | 2018-01-08 14:28:50 +0100 | [diff] [blame] | 158 | return -ENOMEM; |
| 159 | |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 160 | to->flags = TIMER_OF_IRQ | TIMER_OF_CLOCK | TIMER_OF_BASE; |
| 161 | to->of_irq.handler = stm32_clock_event_handler; |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 162 | |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 163 | ret = timer_of_init(node, to); |
| 164 | if (ret) |
| 165 | goto err; |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 166 | |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 167 | rstc = of_reset_control_get(node, NULL); |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 168 | if (!IS_ERR(rstc)) { |
| 169 | reset_control_assert(rstc); |
| 170 | reset_control_deassert(rstc); |
| 171 | } |
| 172 | |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 173 | stm32_clockevent_init(to); |
| 174 | return 0; |
| 175 | err: |
| 176 | kfree(to); |
Daniel Lezcano | 38d94c5 | 2016-06-06 23:28:17 +0200 | [diff] [blame] | 177 | return ret; |
Maxime Coquelin | e37e459 | 2015-05-22 23:03:33 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Benjamin Gaignard | d04af49 | 2018-01-08 14:28:51 +0100 | [diff] [blame] | 180 | TIMER_OF_DECLARE(stm32, "st,stm32-timer", stm32_timer_init); |