2 * Copyright (C) Maxime Coquelin 2015
3 * Author: Maxime Coquelin <mcoquelin.stm32@gmail.com>
4 * License terms: GNU General Public License (GPL), version 2
6 * Inspired by time-efm32.c from Uwe Kleine-Koenig
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>
15 #include <linux/of_address.h>
16 #include <linux/of_irq.h>
17 #include <linux/clk.h>
18 #include <linux/reset.h>
27 #define TIM_CR1_CEN BIT(0)
28 #define TIM_CR1_OPM BIT(3)
29 #define TIM_CR1_ARPE BIT(7)
31 #define TIM_DIER_UIE BIT(0)
33 #define TIM_SR_UIF BIT(0)
35 #define TIM_EGR_UG BIT(0)
37 struct stm32_clock_event_ddata {
38 struct clock_event_device evtdev;
39 unsigned periodic_top;
43 static int stm32_clock_event_shutdown(struct clock_event_device *evtdev)
45 struct stm32_clock_event_ddata *data =
46 container_of(evtdev, struct stm32_clock_event_ddata, evtdev);
47 void *base = data->base;
49 writel_relaxed(0, base + TIM_CR1);
53 static int stm32_clock_event_set_periodic(struct clock_event_device *evtdev)
55 struct stm32_clock_event_ddata *data =
56 container_of(evtdev, struct stm32_clock_event_ddata, evtdev);
57 void *base = data->base;
59 writel_relaxed(data->periodic_top, base + TIM_ARR);
60 writel_relaxed(TIM_CR1_ARPE | TIM_CR1_CEN, base + TIM_CR1);
64 static int stm32_clock_event_set_next_event(unsigned long evt,
65 struct clock_event_device *evtdev)
67 struct stm32_clock_event_ddata *data =
68 container_of(evtdev, struct stm32_clock_event_ddata, evtdev);
70 writel_relaxed(evt, data->base + TIM_ARR);
71 writel_relaxed(TIM_CR1_ARPE | TIM_CR1_OPM | TIM_CR1_CEN,
72 data->base + TIM_CR1);
77 static irqreturn_t stm32_clock_event_handler(int irq, void *dev_id)
79 struct stm32_clock_event_ddata *data = dev_id;
81 writel_relaxed(0, data->base + TIM_SR);
83 data->evtdev.event_handler(&data->evtdev);
88 static struct stm32_clock_event_ddata clock_event_ddata = {
90 .name = "stm32 clockevent",
91 .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC,
92 .set_state_shutdown = stm32_clock_event_shutdown,
93 .set_state_periodic = stm32_clock_event_set_periodic,
94 .set_state_oneshot = stm32_clock_event_shutdown,
95 .tick_resume = stm32_clock_event_shutdown,
96 .set_next_event = stm32_clock_event_set_next_event,
101 static int __init stm32_clockevent_init(struct device_node *np)
103 struct stm32_clock_event_ddata *data = &clock_event_ddata;
105 struct reset_control *rstc;
106 unsigned long rate, max_delta;
107 int irq, ret, bits, prescaler = 1;
109 clk = of_clk_get(np, 0);
112 pr_err("failed to get clock for clockevent (%d)\n", ret);
116 ret = clk_prepare_enable(clk);
118 pr_err("failed to enable timer clock for clockevent (%d)\n",
123 rate = clk_get_rate(clk);
125 rstc = of_reset_control_get(np, NULL);
127 reset_control_assert(rstc);
128 reset_control_deassert(rstc);
131 data->base = of_iomap(np, 0);
134 pr_err("failed to map registers for clockevent\n");
138 irq = irq_of_parse_and_map(np, 0);
141 pr_err("%pOF: failed to get irq.\n", np);
145 /* Detect whether the timer is 16 or 32 bits */
146 writel_relaxed(~0U, data->base + TIM_ARR);
147 max_delta = readl_relaxed(data->base + TIM_ARR);
148 if (max_delta == ~0U) {
155 writel_relaxed(0, data->base + TIM_ARR);
157 writel_relaxed(prescaler - 1, data->base + TIM_PSC);
158 writel_relaxed(TIM_EGR_UG, data->base + TIM_EGR);
159 writel_relaxed(TIM_DIER_UIE, data->base + TIM_DIER);
160 writel_relaxed(0, data->base + TIM_SR);
162 data->periodic_top = DIV_ROUND_CLOSEST(rate, prescaler * HZ);
164 clockevents_config_and_register(&data->evtdev,
165 DIV_ROUND_CLOSEST(rate, prescaler),
168 ret = request_irq(irq, stm32_clock_event_handler, IRQF_TIMER,
169 "stm32 clockevent", data);
171 pr_err("%pOF: failed to request irq.\n", np);
175 pr_info("%pOF: STM32 clockevent driver initialized (%d bits)\n",
183 clk_disable_unprepare(clk);
190 TIMER_OF_DECLARE(stm32, "st,stm32-timer", stm32_clockevent_init);