0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/kernel.h>
0010 #include <linux/clocksource.h>
0011 #include <linux/clockchips.h>
0012 #include <linux/delay.h>
0013 #include <linux/irq.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/of.h>
0016 #include <linux/of_address.h>
0017 #include <linux/of_irq.h>
0018 #include <linux/clk.h>
0019 #include <linux/reset.h>
0020 #include <linux/sched_clock.h>
0021 #include <linux/slab.h>
0022
0023 #include "timer-of.h"
0024
0025 #define TIM_CR1 0x00
0026 #define TIM_DIER 0x0c
0027 #define TIM_SR 0x10
0028 #define TIM_EGR 0x14
0029 #define TIM_CNT 0x24
0030 #define TIM_PSC 0x28
0031 #define TIM_ARR 0x2c
0032 #define TIM_CCR1 0x34
0033
0034 #define TIM_CR1_CEN BIT(0)
0035 #define TIM_CR1_UDIS BIT(1)
0036 #define TIM_CR1_OPM BIT(3)
0037 #define TIM_CR1_ARPE BIT(7)
0038
0039 #define TIM_DIER_UIE BIT(0)
0040 #define TIM_DIER_CC1IE BIT(1)
0041
0042 #define TIM_SR_UIF BIT(0)
0043
0044 #define TIM_EGR_UG BIT(0)
0045
0046 #define TIM_PSC_MAX USHRT_MAX
0047 #define TIM_PSC_CLKRATE 10000
0048
0049 struct stm32_timer_private {
0050 int bits;
0051 };
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062 static void stm32_timer_of_bits_set(struct timer_of *to, int bits)
0063 {
0064 struct stm32_timer_private *pd = to->private_data;
0065
0066 pd->bits = bits;
0067 }
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078 static int stm32_timer_of_bits_get(struct timer_of *to)
0079 {
0080 struct stm32_timer_private *pd = to->private_data;
0081
0082 return pd->bits;
0083 }
0084
0085 static void __iomem *stm32_timer_cnt __read_mostly;
0086
0087 static u64 notrace stm32_read_sched_clock(void)
0088 {
0089 return readl_relaxed(stm32_timer_cnt);
0090 }
0091
0092 static struct delay_timer stm32_timer_delay;
0093
0094 static unsigned long stm32_read_delay(void)
0095 {
0096 return readl_relaxed(stm32_timer_cnt);
0097 }
0098
0099 static void stm32_clock_event_disable(struct timer_of *to)
0100 {
0101 writel_relaxed(0, timer_of_base(to) + TIM_DIER);
0102 }
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112 static void stm32_timer_start(struct timer_of *to)
0113 {
0114 writel_relaxed(TIM_CR1_UDIS | TIM_CR1_CEN, timer_of_base(to) + TIM_CR1);
0115 }
0116
0117 static int stm32_clock_event_shutdown(struct clock_event_device *clkevt)
0118 {
0119 struct timer_of *to = to_timer_of(clkevt);
0120
0121 stm32_clock_event_disable(to);
0122
0123 return 0;
0124 }
0125
0126 static int stm32_clock_event_set_next_event(unsigned long evt,
0127 struct clock_event_device *clkevt)
0128 {
0129 struct timer_of *to = to_timer_of(clkevt);
0130 unsigned long now, next;
0131
0132 next = readl_relaxed(timer_of_base(to) + TIM_CNT) + evt;
0133 writel_relaxed(next, timer_of_base(to) + TIM_CCR1);
0134 now = readl_relaxed(timer_of_base(to) + TIM_CNT);
0135
0136 if ((next - now) > evt)
0137 return -ETIME;
0138
0139 writel_relaxed(TIM_DIER_CC1IE, timer_of_base(to) + TIM_DIER);
0140
0141 return 0;
0142 }
0143
0144 static int stm32_clock_event_set_periodic(struct clock_event_device *clkevt)
0145 {
0146 struct timer_of *to = to_timer_of(clkevt);
0147
0148 stm32_timer_start(to);
0149
0150 return stm32_clock_event_set_next_event(timer_of_period(to), clkevt);
0151 }
0152
0153 static int stm32_clock_event_set_oneshot(struct clock_event_device *clkevt)
0154 {
0155 struct timer_of *to = to_timer_of(clkevt);
0156
0157 stm32_timer_start(to);
0158
0159 return 0;
0160 }
0161
0162 static irqreturn_t stm32_clock_event_handler(int irq, void *dev_id)
0163 {
0164 struct clock_event_device *clkevt = (struct clock_event_device *)dev_id;
0165 struct timer_of *to = to_timer_of(clkevt);
0166
0167 writel_relaxed(0, timer_of_base(to) + TIM_SR);
0168
0169 if (clockevent_state_periodic(clkevt))
0170 stm32_clock_event_set_periodic(clkevt);
0171 else
0172 stm32_clock_event_shutdown(clkevt);
0173
0174 clkevt->event_handler(clkevt);
0175
0176 return IRQ_HANDLED;
0177 }
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188 static void __init stm32_timer_set_width(struct timer_of *to)
0189 {
0190 u32 width;
0191
0192 writel_relaxed(UINT_MAX, timer_of_base(to) + TIM_ARR);
0193
0194 width = readl_relaxed(timer_of_base(to) + TIM_ARR);
0195
0196 stm32_timer_of_bits_set(to, width == UINT_MAX ? 32 : 16);
0197 }
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207 static void __init stm32_timer_set_prescaler(struct timer_of *to)
0208 {
0209 int prescaler = 1;
0210
0211 if (stm32_timer_of_bits_get(to) != 32) {
0212 prescaler = DIV_ROUND_CLOSEST(timer_of_rate(to),
0213 TIM_PSC_CLKRATE);
0214
0215
0216
0217
0218
0219 prescaler = prescaler < TIM_PSC_MAX ? prescaler : TIM_PSC_MAX;
0220 }
0221
0222 writel_relaxed(prescaler - 1, timer_of_base(to) + TIM_PSC);
0223 writel_relaxed(TIM_EGR_UG, timer_of_base(to) + TIM_EGR);
0224 writel_relaxed(0, timer_of_base(to) + TIM_SR);
0225
0226
0227 to->of_clk.rate = DIV_ROUND_CLOSEST(to->of_clk.rate, prescaler);
0228 to->of_clk.period = DIV_ROUND_UP(to->of_clk.rate, HZ);
0229 }
0230
0231 static int __init stm32_clocksource_init(struct timer_of *to)
0232 {
0233 u32 bits = stm32_timer_of_bits_get(to);
0234 const char *name = to->np->full_name;
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245 if (bits == 32 && !stm32_timer_cnt) {
0246
0247
0248
0249
0250
0251 stm32_timer_start(to);
0252
0253 stm32_timer_cnt = timer_of_base(to) + TIM_CNT;
0254 sched_clock_register(stm32_read_sched_clock, bits, timer_of_rate(to));
0255 pr_info("%s: STM32 sched_clock registered\n", name);
0256
0257 stm32_timer_delay.read_current_timer = stm32_read_delay;
0258 stm32_timer_delay.freq = timer_of_rate(to);
0259 register_current_timer_delay(&stm32_timer_delay);
0260 pr_info("%s: STM32 delay timer registered\n", name);
0261 }
0262
0263 return clocksource_mmio_init(timer_of_base(to) + TIM_CNT, name,
0264 timer_of_rate(to), bits == 32 ? 250 : 100,
0265 bits, clocksource_mmio_readl_up);
0266 }
0267
0268 static void __init stm32_clockevent_init(struct timer_of *to)
0269 {
0270 u32 bits = stm32_timer_of_bits_get(to);
0271
0272 to->clkevt.name = to->np->full_name;
0273 to->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
0274 to->clkevt.set_state_shutdown = stm32_clock_event_shutdown;
0275 to->clkevt.set_state_periodic = stm32_clock_event_set_periodic;
0276 to->clkevt.set_state_oneshot = stm32_clock_event_set_oneshot;
0277 to->clkevt.tick_resume = stm32_clock_event_shutdown;
0278 to->clkevt.set_next_event = stm32_clock_event_set_next_event;
0279 to->clkevt.rating = bits == 32 ? 250 : 100;
0280
0281 clockevents_config_and_register(&to->clkevt, timer_of_rate(to), 0x1,
0282 (1 << bits) - 1);
0283
0284 pr_info("%pOF: STM32 clockevent driver initialized (%d bits)\n",
0285 to->np, bits);
0286 }
0287
0288 static int __init stm32_timer_init(struct device_node *node)
0289 {
0290 struct reset_control *rstc;
0291 struct timer_of *to;
0292 int ret;
0293
0294 to = kzalloc(sizeof(*to), GFP_KERNEL);
0295 if (!to)
0296 return -ENOMEM;
0297
0298 to->flags = TIMER_OF_IRQ | TIMER_OF_CLOCK | TIMER_OF_BASE;
0299 to->of_irq.handler = stm32_clock_event_handler;
0300
0301 ret = timer_of_init(node, to);
0302 if (ret)
0303 goto err;
0304
0305 to->private_data = kzalloc(sizeof(struct stm32_timer_private),
0306 GFP_KERNEL);
0307 if (!to->private_data) {
0308 ret = -ENOMEM;
0309 goto deinit;
0310 }
0311
0312 rstc = of_reset_control_get(node, NULL);
0313 if (!IS_ERR(rstc)) {
0314 reset_control_assert(rstc);
0315 reset_control_deassert(rstc);
0316 }
0317
0318 stm32_timer_set_width(to);
0319
0320 stm32_timer_set_prescaler(to);
0321
0322 ret = stm32_clocksource_init(to);
0323 if (ret)
0324 goto deinit;
0325
0326 stm32_clockevent_init(to);
0327 return 0;
0328
0329 deinit:
0330 timer_of_cleanup(to);
0331 err:
0332 kfree(to);
0333 return ret;
0334 }
0335
0336 TIMER_OF_DECLARE(stm32, "st,stm32-timer", stm32_timer_init);