1 /*
2  * Copyright (c) 2023 TOKITA Hiroshi
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <hardware/timer.h>
8 
9 #include <zephyr/drivers/counter.h>
10 #include <zephyr/drivers/clock_control.h>
11 #include <zephyr/drivers/reset.h>
12 #include <zephyr/sys/atomic.h>
13 #include <zephyr/irq.h>
14 #include <cmsis_core.h>
15 
16 #define LOG_LEVEL CONFIG_COUNTER_LOG_LEVEL
17 #include <zephyr/logging/log.h>
18 
19 LOG_MODULE_REGISTER(counter_rpi_pico_timer, LOG_LEVEL);
20 
21 #define DT_DRV_COMPAT raspberrypi_pico_timer
22 
23 struct counter_rpi_pico_timer_ch_data {
24 	counter_alarm_callback_t callback;
25 	void *user_data;
26 };
27 
28 struct counter_rpi_pico_timer_data {
29 	struct counter_rpi_pico_timer_ch_data *ch_data;
30 	uint32_t guard_period;
31 };
32 
33 struct counter_rpi_pico_timer_config {
34 	struct counter_config_info info;
35 	timer_hw_t *timer;
36 	void (*irq_config)();
37 	const struct device *clk_dev;
38 	clock_control_subsys_t clk_id;
39 	const struct reset_dt_spec reset;
40 };
41 
counter_rpi_pico_timer_start(const struct device * dev)42 static int counter_rpi_pico_timer_start(const struct device *dev)
43 {
44 	const struct counter_rpi_pico_timer_config *config = dev->config;
45 
46 	config->timer->pause = 0;
47 
48 	return 0;
49 }
50 
counter_rpi_pico_timer_stop(const struct device * dev)51 static int counter_rpi_pico_timer_stop(const struct device *dev)
52 {
53 	const struct counter_rpi_pico_timer_config *config = dev->config;
54 
55 	config->timer->pause = 1u;
56 	config->timer->timelw = 0;
57 	config->timer->timehw = 0;
58 
59 	return 0;
60 }
61 
counter_rpi_pico_timer_get_top_value(const struct device * dev)62 static uint32_t counter_rpi_pico_timer_get_top_value(const struct device *dev)
63 {
64 	const struct counter_rpi_pico_timer_config *config = dev->config;
65 
66 	return config->info.max_top_value;
67 }
68 
counter_rpi_pico_timer_get_value(const struct device * dev,uint32_t * ticks)69 static int counter_rpi_pico_timer_get_value(const struct device *dev, uint32_t *ticks)
70 {
71 	*ticks = time_us_32();
72 	return 0;
73 }
74 
counter_rpi_pico_timer_set_alarm(const struct device * dev,uint8_t id,const struct counter_alarm_cfg * alarm_cfg)75 static int counter_rpi_pico_timer_set_alarm(const struct device *dev, uint8_t id,
76 					    const struct counter_alarm_cfg *alarm_cfg)
77 {
78 	const struct counter_rpi_pico_timer_config *config = dev->config;
79 	struct counter_rpi_pico_timer_data *data = dev->data;
80 	struct counter_rpi_pico_timer_ch_data *chdata = &data->ch_data[id];
81 	uint64_t target = (alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) ? 0 : alarm_cfg->ticks;
82 	absolute_time_t alarm_at;
83 	bool missed;
84 
85 	update_us_since_boot(&alarm_at, config->timer->timerawl + target);
86 
87 	if (alarm_cfg->ticks > counter_rpi_pico_timer_get_top_value(dev)) {
88 		return -EINVAL;
89 	}
90 
91 	if (chdata->callback) {
92 		return -EBUSY;
93 	}
94 
95 	chdata->callback = alarm_cfg->callback;
96 	chdata->user_data = alarm_cfg->user_data;
97 
98 	missed = timer_hardware_alarm_set_target(config->timer, id, alarm_at);
99 
100 	if (missed) {
101 		if (alarm_cfg->flags & COUNTER_ALARM_CFG_EXPIRE_WHEN_LATE) {
102 			timer_hardware_alarm_force_irq(config->timer, id);
103 		}
104 		chdata->callback = NULL;
105 		chdata->user_data = NULL;
106 		return -ETIME;
107 	}
108 
109 	return 0;
110 }
111 
counter_rpi_pico_timer_cancel_alarm(const struct device * dev,uint8_t id)112 static int counter_rpi_pico_timer_cancel_alarm(const struct device *dev, uint8_t id)
113 {
114 	struct counter_rpi_pico_timer_data *data = dev->data;
115 	struct counter_rpi_pico_timer_ch_data *chdata = &data->ch_data[id];
116 	const struct counter_rpi_pico_timer_config *config = dev->config;
117 
118 	chdata->callback = NULL;
119 	chdata->user_data = NULL;
120 	timer_hardware_alarm_cancel(config->timer, id);
121 
122 	return 0;
123 }
124 
counter_rpi_pico_timer_set_top_value(const struct device * dev,const struct counter_top_cfg * cfg)125 static int counter_rpi_pico_timer_set_top_value(const struct device *dev,
126 						const struct counter_top_cfg *cfg)
127 {
128 	ARG_UNUSED(dev);
129 	ARG_UNUSED(cfg);
130 
131 	return -ENOTSUP;
132 }
133 
counter_rpi_pico_timer_get_pending_int(const struct device * dev)134 static uint32_t counter_rpi_pico_timer_get_pending_int(const struct device *dev)
135 {
136 	return 0;
137 }
138 
counter_rpi_pico_timer_get_guard_period(const struct device * dev,uint32_t flags)139 static uint32_t counter_rpi_pico_timer_get_guard_period(const struct device *dev, uint32_t flags)
140 {
141 	struct counter_rpi_pico_timer_data *data = dev->data;
142 
143 	return data->guard_period;
144 }
145 
counter_rpi_pico_timer_set_guard_period(const struct device * dev,uint32_t guard,uint32_t flags)146 static int counter_rpi_pico_timer_set_guard_period(const struct device *dev, uint32_t guard,
147 						   uint32_t flags)
148 {
149 	struct counter_rpi_pico_timer_data *data = dev->data;
150 
151 	__ASSERT_NO_MSG(guard < counter_rpi_pico_timer_get_top_value(dev));
152 
153 	data->guard_period = guard;
154 
155 	return 0;
156 }
157 
counter_rpi_pico_irq_handle(uint32_t ch,void * arg)158 static void counter_rpi_pico_irq_handle(uint32_t ch, void *arg)
159 {
160 	struct device *dev = arg;
161 	struct counter_rpi_pico_timer_data *data = dev->data;
162 	counter_alarm_callback_t cb = data->ch_data[ch].callback;
163 	void *user_data = data->ch_data[ch].user_data;
164 
165 	if (cb) {
166 		data->ch_data[ch].callback = NULL;
167 		data->ch_data[ch].user_data = NULL;
168 		cb(dev, ch, time_us_32(), user_data);
169 	}
170 }
171 
counter_rpi_pico_timer_init(const struct device * dev)172 static int counter_rpi_pico_timer_init(const struct device *dev)
173 {
174 	const struct counter_rpi_pico_timer_config *config = dev->config;
175 	int ret;
176 
177 	ret = clock_control_on(config->clk_dev, config->clk_id);
178 	if (ret < 0) {
179 		return ret;
180 	}
181 
182 	ret = reset_line_toggle_dt(&config->reset);
183 	if (ret < 0) {
184 		return ret;
185 	}
186 
187 	config->irq_config();
188 
189 	return 0;
190 }
191 
192 static DEVICE_API(counter, counter_rpi_pico_driver_api) = {
193 	.start = counter_rpi_pico_timer_start,
194 	.stop = counter_rpi_pico_timer_stop,
195 	.get_value = counter_rpi_pico_timer_get_value,
196 	.set_alarm = counter_rpi_pico_timer_set_alarm,
197 	.cancel_alarm = counter_rpi_pico_timer_cancel_alarm,
198 	.set_top_value = counter_rpi_pico_timer_set_top_value,
199 	.get_pending_int = counter_rpi_pico_timer_get_pending_int,
200 	.get_top_value = counter_rpi_pico_timer_get_top_value,
201 	.get_guard_period = counter_rpi_pico_timer_get_guard_period,
202 	.set_guard_period = counter_rpi_pico_timer_set_guard_period,
203 };
204 
205 #define RPI_PICO_TIMER_IRQ_ENABLE(node_id, name, idx)                                              \
206 	do {                                                                                       \
207 		timer_hardware_alarm_set_callback((timer_hw_t *)DT_REG_ADDR(node_id), idx,         \
208 						  counter_rpi_pico_irq_handle);                    \
209 		IRQ_CONNECT((DT_IRQ_BY_IDX(node_id, idx, irq)),                                    \
210 			    (DT_IRQ_BY_IDX(node_id, idx, priority)), hardware_alarm_irq_handler,   \
211 			    (DEVICE_DT_GET(node_id)), 0);                                          \
212 		irq_enable((DT_IRQ_BY_IDX(node_id, idx, irq)));                                    \
213 	} while (false);
214 
215 #define COUNTER_RPI_PICO_TIMER(inst)                                                               \
216 	static void counter_irq_config##inst(void)                                                 \
217 	{                                                                                          \
218 		DT_INST_FOREACH_PROP_ELEM(inst, interrupt_names, RPI_PICO_TIMER_IRQ_ENABLE);       \
219 	}                                                                                          \
220 	static struct counter_rpi_pico_timer_ch_data                                               \
221 		ch_data##inst[DT_NUM_IRQS(DT_DRV_INST(inst))];                                     \
222 	static struct counter_rpi_pico_timer_data counter_##inst##_data = {                        \
223 		.ch_data = ch_data##inst,                                                          \
224 	};                                                                                         \
225 	static const struct counter_rpi_pico_timer_config counter_##inst##_config = {              \
226 		.timer = (timer_hw_t *)DT_INST_REG_ADDR(inst),                                     \
227 		.irq_config = counter_irq_config##inst,                                            \
228 		.info =                                                                            \
229 			{                                                                          \
230 				.max_top_value = UINT32_MAX,                                       \
231 				.freq = 1000000,                                                   \
232 				.flags = COUNTER_CONFIG_INFO_COUNT_UP,                             \
233 				.channels = ARRAY_SIZE(ch_data##inst),                             \
234 			},                                                                         \
235 		.clk_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(inst)),                               \
236 		.clk_id = (clock_control_subsys_t)DT_INST_PHA_BY_IDX(inst, clocks, 0, clk_id),     \
237 		.reset = RESET_DT_SPEC_INST_GET(inst),                                             \
238 	};                                                                                         \
239 	DEVICE_DT_INST_DEFINE(inst, counter_rpi_pico_timer_init, NULL, &counter_##inst##_data,     \
240 			      &counter_##inst##_config, PRE_KERNEL_1,                              \
241 			      CONFIG_COUNTER_INIT_PRIORITY, &counter_rpi_pico_driver_api);
242 
243 DT_INST_FOREACH_STATUS_OKAY(COUNTER_RPI_PICO_TIMER)
244