forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter_mcux_snvs.c
342 lines (269 loc) · 8.11 KB
/
counter_mcux_snvs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright (c) 2021 Basalte bv
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT nxp_imx_snvs_rtc
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(mcux_snvs, CONFIG_COUNTER_LOG_LEVEL);
#if CONFIG_COUNTER_MCUX_SNVS_SRTC
#define MCUX_SNVS_SRTC
#define MCUX_SNVS_NUM_CHANNELS 2
#else
#define MCUX_SNVS_NUM_CHANNELS 1
#endif
#include <zephyr/drivers/counter.h>
#include <zephyr/irq.h>
#include <fsl_snvs_hp.h>
#ifdef MCUX_SNVS_SRTC
#include <fsl_snvs_lp.h>
#endif
struct mcux_snvs_config {
/* info must be first element */
struct counter_config_info info;
SNVS_Type *base;
void (*irq_config_func)(const struct device *dev);
};
struct mcux_snvs_data {
counter_alarm_callback_t alarm_hp_rtc_callback;
void *alarm_hp_rtc_user_data;
#ifdef MCUX_SNVS_SRTC
counter_alarm_callback_t alarm_lp_srtc_callback;
void *alarm_lp_srtc_user_data;
#endif
};
static int mcux_snvs_start(const struct device *dev)
{
ARG_UNUSED(dev);
return -EALREADY;
}
static int mcux_snvs_stop(const struct device *dev)
{
ARG_UNUSED(dev);
return -ENOTSUP;
}
static int mcux_snvs_get_value(const struct device *dev, uint32_t *ticks)
{
const struct mcux_snvs_config *config = dev->config;
uint32_t tmp = 0;
do {
*ticks = tmp;
tmp = (config->base->HPRTCMR << 17U);
tmp |= (config->base->HPRTCLR >> 15U);
} while (*ticks != tmp);
return 0;
}
static int mcux_snvs_set_alarm(const struct device *dev,
uint8_t chan_id,
const struct counter_alarm_cfg *alarm_cfg)
{
const struct mcux_snvs_config *config = dev->config;
struct mcux_snvs_data *data = dev->data;
uint32_t current, ticks;
mcux_snvs_get_value(dev, ¤t);
ticks = alarm_cfg->ticks;
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) == 0) {
ticks += current;
}
if (ticks < current) {
LOG_ERR("Invalid alarm ticks");
return -EINVAL;
}
if (chan_id == 0) {
if (data->alarm_hp_rtc_callback) {
return -EBUSY;
}
data->alarm_hp_rtc_callback = alarm_cfg->callback;
data->alarm_hp_rtc_user_data = alarm_cfg->user_data;
/* disable RTC alarm interrupt */
config->base->HPCR &= ~SNVS_HPCR_HPTA_EN_MASK;
while ((config->base->HPCR & SNVS_HPCR_HPTA_EN_MASK) != 0U) {
}
/* Set alarm in seconds*/
config->base->HPTAMR = (uint32_t)(ticks >> 17U);
config->base->HPTALR = (uint32_t)(ticks << 15U);
/* enable RTC alarm interrupt */
config->base->HPCR |= SNVS_HPCR_HPTA_EN_MASK;
#ifdef MCUX_SNVS_SRTC
} else if (chan_id == 1) {
if (data->alarm_lp_srtc_callback) {
return -EBUSY;
}
data->alarm_lp_srtc_callback = alarm_cfg->callback;
data->alarm_lp_srtc_user_data = alarm_cfg->user_data;
/* disable SRTC alarm interrupt */
config->base->LPCR &= ~SNVS_LPCR_LPTA_EN_MASK;
while ((config->base->LPCR & SNVS_LPCR_LPTA_EN_MASK) != 0U) {
}
/* Set alarm in seconds*/
config->base->LPTAR = ticks;
/* enable SRTC alarm interrupt */
config->base->LPCR |= SNVS_LPCR_LPTA_EN_MASK;
#endif
} else {
LOG_ERR("Invalid channel id");
return -EINVAL;
}
return 0;
}
static int mcux_snvs_cancel_alarm(const struct device *dev,
uint8_t chan_id)
{
const struct mcux_snvs_config *config = dev->config;
struct mcux_snvs_data *data = dev->data;
if (chan_id == 0) {
/* disable RTC alarm interrupt */
config->base->HPCR &= ~SNVS_HPCR_HPTA_EN_MASK;
while ((config->base->HPCR & SNVS_HPCR_HPTA_EN_MASK) != 0U) {
}
/* clear callback */
data->alarm_hp_rtc_callback = NULL;
#ifdef MCUX_SNVS_SRTC
} else if (chan_id == 1) {
/* disable SRTC alarm interrupt */
config->base->LPCR &= ~SNVS_LPCR_LPTA_EN_MASK;
while ((config->base->LPCR & SNVS_LPCR_LPTA_EN_MASK) != 0U) {
}
/* clear callback */
data->alarm_lp_srtc_callback = NULL;
#endif
} else {
LOG_ERR("Invalid channel id");
return -EINVAL;
}
return 0;
}
static int mcux_snvs_set_top_value(const struct device *dev,
const struct counter_top_cfg *cfg)
{
ARG_UNUSED(dev);
ARG_UNUSED(cfg);
return -ENOTSUP;
}
static uint32_t mcux_snvs_get_pending_int(const struct device *dev)
{
const struct mcux_snvs_config *config = dev->config;
uint32_t flags;
flags = SNVS_HP_RTC_GetStatusFlags(config->base) & kSNVS_RTC_AlarmInterruptFlag;
#ifdef MCUX_SNVS_SRTC
flags |= SNVS_LP_SRTC_GetStatusFlags(config->base) & kSNVS_SRTC_AlarmInterruptFlag;
#endif
return flags;
}
static uint32_t mcux_snvs_get_top_value(const struct device *dev)
{
ARG_UNUSED(dev);
return UINT32_MAX;
}
void mcux_snvs_isr(const struct device *dev)
{
const struct mcux_snvs_config *config = dev->config;
struct mcux_snvs_data *data = dev->data;
uint32_t current;
mcux_snvs_get_value(dev, ¤t);
if (SNVS_HP_RTC_GetStatusFlags(config->base) & kSNVS_RTC_AlarmInterruptFlag) {
/* Clear alarm flag */
SNVS_HP_RTC_ClearStatusFlags(config->base, kSNVS_RTC_AlarmInterruptFlag);
if (data->alarm_hp_rtc_callback) {
data->alarm_hp_rtc_callback(dev, 0, current, data->alarm_hp_rtc_user_data);
mcux_snvs_cancel_alarm(dev, 0);
}
}
#ifdef MCUX_SNVS_SRTC
if (SNVS_LP_SRTC_GetStatusFlags(config->base) & kSNVS_SRTC_AlarmInterruptFlag) {
/* Clear alarm flag */
SNVS_LP_SRTC_ClearStatusFlags(config->base, kSNVS_SRTC_AlarmInterruptFlag);
if (data->alarm_lp_srtc_callback) {
data->alarm_lp_srtc_callback(dev, 1, current,
data->alarm_lp_srtc_user_data);
mcux_snvs_cancel_alarm(dev, 1);
}
}
#endif
}
int mcux_snvs_rtc_set(const struct device *dev, uint32_t ticks)
{
const struct mcux_snvs_config *config = dev->config;
#ifdef MCUX_SNVS_SRTC
SNVS_LP_SRTC_StopTimer(config->base);
config->base->LPSRTCMR = (uint32_t)(ticks >> 17U);
config->base->LPSRTCLR = (uint32_t)(ticks << 15U);
SNVS_LP_SRTC_StartTimer(config->base);
/* Sync to our high power RTC */
SNVS_HP_RTC_TimeSynchronize(config->base);
#else
SNVS_HP_RTC_StopTimer(config->base);
config->base->HPRTCMR = (uint32_t)(ticks >> 17U);
config->base->HPRTCLR = (uint32_t)(ticks << 15U);
SNVS_HP_RTC_StartTimer(config->base);
#endif
return 0;
}
static int mcux_snvs_init(const struct device *dev)
{
const struct mcux_snvs_config *config = dev->config;
snvs_hp_rtc_config_t hp_rtc_config;
#ifdef MCUX_SNVS_SRTC
snvs_lp_srtc_config_t lp_srtc_config;
#endif
SNVS_HP_RTC_GetDefaultConfig(&hp_rtc_config);
SNVS_HP_RTC_Init(config->base, &hp_rtc_config);
#ifdef MCUX_SNVS_SRTC
/* Reset power glitch detector */
SNVS_LP_Init(config->base);
/* Init SRTC to default config */
SNVS_LP_SRTC_GetDefaultConfig(&lp_srtc_config);
SNVS_LP_SRTC_Init(config->base, &lp_srtc_config);
#if CONFIG_COUNTER_MCUX_SNVS_SRTC_WAKE
config->base->LPCR |= SNVS_LPCR_LPWUI_EN_MASK;
#endif
/* RTC should always run */
SNVS_LP_SRTC_StartTimer(config->base);
SNVS_HP_RTC_TimeSynchronize(config->base);
#endif
/* RTC should always run */
SNVS_HP_RTC_StartTimer(config->base);
config->irq_config_func(dev);
return 0;
}
static DEVICE_API(counter, mcux_snvs_driver_api) = {
.start = mcux_snvs_start,
.stop = mcux_snvs_stop,
.get_value = mcux_snvs_get_value,
.set_alarm = mcux_snvs_set_alarm,
.cancel_alarm = mcux_snvs_cancel_alarm,
.set_top_value = mcux_snvs_set_top_value,
.get_pending_int = mcux_snvs_get_pending_int,
.get_top_value = mcux_snvs_get_top_value,
};
/*
* This driver is single-instance. If the devicetree contains multiple
* instances, this will fail and the driver needs to be revisited.
*/
BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) <= 1,
"unsupported snvs instance");
#if DT_NODE_HAS_STATUS_OKAY(DT_DRV_INST(0))
static struct mcux_snvs_data mcux_snvs_data_0;
static void mcux_snvs_irq_config_0(const struct device *dev);
static struct mcux_snvs_config mcux_snvs_config_0 = {
.info = {
.max_top_value = 0,
.freq = 1,
.channels = MCUX_SNVS_NUM_CHANNELS,
.flags = COUNTER_CONFIG_INFO_COUNT_UP,
},
.base = (SNVS_Type *)DT_REG_ADDR(DT_INST_PARENT(0)),
.irq_config_func = mcux_snvs_irq_config_0,
};
DEVICE_DT_INST_DEFINE(0, &mcux_snvs_init, NULL,
&mcux_snvs_data_0,
&mcux_snvs_config_0,
POST_KERNEL, CONFIG_COUNTER_INIT_PRIORITY,
&mcux_snvs_driver_api);
static void mcux_snvs_irq_config_0(const struct device *dev)
{
IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority),
mcux_snvs_isr, DEVICE_DT_INST_GET(0), 0);
irq_enable(DT_INST_IRQN(0));
}
#endif /* DT_NODE_HAS_STATUS_OKAY(DT_DRV_INST(0)) */