Skip to content

Commit f3c137f

Browse files
committed
drivers: irtc: Updated rtc driver to support NXP RT700.
1. Update nxp irtc driver to fix issue in init and alarm function. 2. Update RTC device tree binding to support "share-counter". 3. Update RT700 dtsi to support rtc0 for cpu0 and rtc1 for cpu1. 4. Update readme. 5. Update unit test project conf for RT700. Signed-off-by: Holt Sun <[email protected]>
1 parent 47b07e5 commit f3c137f

File tree

9 files changed

+166
-40
lines changed

9 files changed

+166
-40
lines changed

boards/nxp/mimxrt700_evk/mimxrt700_evk_mimxrt798s_cm33_cpu0.dts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
i2s-codec-tx = &sai0;
2626
i2s-tx = &sai0;
2727
sdhc0 = &usdhc0;
28+
rtc = &rtc0;
2829
};
2930

3031
chosen {
@@ -264,3 +265,7 @@ zephyr_udc0: &usb0 {
264265
};
265266

266267
p3t1755dp_ard_i2c_interface: &flexcomm8_lpi2c8 {};
268+
269+
&rtc0 {
270+
status = "okay";
271+
};

boards/nxp/mimxrt700_evk/mimxrt700_evk_mimxrt798s_cm33_cpu1.dts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
led0 = &red_led;
2020
sw0 = &user_button_1;
2121
ambient-temp0 = &p3t1755;
22+
rtc = &rtc1;
2223
};
2324

2425
chosen {
@@ -97,3 +98,7 @@
9798
status = "okay";
9899
};
99100
};
101+
102+
&rtc1 {
103+
status = "okay";
104+
};

drivers/rtc/rtc_nxp_irtc.c

Lines changed: 114 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 NXP
2+
* Copyright 2024-2025 NXP
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -15,7 +15,10 @@ struct nxp_irtc_config {
1515
RTC_Type *base;
1616
void (*irq_config_func)(const struct device *dev);
1717
bool is_output_clock_enabled;
18+
#if DT_ANY_INST_HAS_PROP_STATUS_OKAY(clock_src)
1819
uint8_t clock_src;
20+
#endif
21+
bool share_counter;
1922
uint8_t alarm_match_flag;
2023
};
2124

@@ -25,15 +28,15 @@ struct nxp_irtc_data {
2528
rtc_alarm_callback alarm_callback;
2629
void *alarm_user_data;
2730
uint16_t alarm_mask;
31+
bool alarm_pending;
2832
#endif /* CONFIG_RTC_ALARM */
2933
};
3034

3135
/* The IRTC Offset is 2112 instead of 1900 [2112 - 1900] -> [212] */
3236
#define RTC_NXP_IRTC_YEAR_OFFSET 212
3337

34-
#define RTC_NXP_GET_REG_FIELD(_reg, _name, _field) \
35-
((_reg->_name & RTC_##_name##_##_field##_MASK) >> \
36-
RTC_##_name##_##_field##_SHIFT)
38+
#define RTC_NXP_GET_REG_FIELD(_reg, _name, _field) \
39+
((_reg->_name & RTC_##_name##_##_field##_MASK) >> RTC_##_name##_##_field##_SHIFT)
3740

3841
/*
3942
* The runtime where this is accessed is unknown so we force a lock on the registers then force an
@@ -63,6 +66,13 @@ static void nxp_irtc_unlock_registers(RTC_Type *reg)
6366
static int nxp_irtc_set_time(const struct device *dev, const struct rtc_time *timeptr)
6467
{
6568
const struct nxp_irtc_config *config = dev->config;
69+
70+
if (config->share_counter) {
71+
ARG_UNUSED(timeptr);
72+
73+
return -EPERM;
74+
}
75+
6676
struct nxp_irtc_data *data = dev->data;
6777
RTC_Type *irtc_reg = config->base;
6878

@@ -80,11 +90,12 @@ static int nxp_irtc_set_time(const struct device *dev, const struct rtc_time *ti
8090
irtc_reg->SECONDS = RTC_SECONDS_SEC_CNT(timeptr->tm_sec);
8191

8292
irtc_reg->HOURMIN = RTC_HOURMIN_MIN_CNT(timeptr->tm_min) |
83-
RTC_HOURMIN_HOUR_CNT(timeptr->tm_hour);
93+
RTC_HOURMIN_HOUR_CNT(timeptr->tm_hour);
8494

85-
/* 1 is valid for rtc_time.tm_wday property but is out of bounds for IRTC registers */
95+
/* 1 is valid for rtc_time.tm_wday property but is out of bounds for IRTC registers
96+
*/
8697
irtc_reg->DAYS = RTC_DAYS_DAY_CNT(timeptr->tm_mday) |
87-
(timeptr->tm_wday == -1 ? 0 : RTC_DAYS_DOW(timeptr->tm_wday));
98+
(timeptr->tm_wday == -1 ? 0 : RTC_DAYS_DOW(timeptr->tm_wday));
8899

89100
irtc_reg->YEARMON = RTC_YEARMON_MON_CNT(calc_month) | RTC_YEARMON_YROFST(calc_year);
90101

@@ -112,8 +123,8 @@ static int nxp_irtc_get_time(const struct device *dev, struct rtc_time *timeptr)
112123
timeptr->tm_wday = RTC_NXP_GET_REG_FIELD(irtc_reg, DAYS, DOW);
113124
timeptr->tm_mday = RTC_NXP_GET_REG_FIELD(irtc_reg, DAYS, DAY_CNT);
114125
timeptr->tm_mon = RTC_NXP_GET_REG_FIELD(irtc_reg, YEARMON, MON_CNT) - 1;
115-
timeptr->tm_year = (int8_t)RTC_NXP_GET_REG_FIELD(irtc_reg, YEARMON, YROFST) +
116-
RTC_NXP_IRTC_YEAR_OFFSET;
126+
timeptr->tm_year =
127+
(int8_t)RTC_NXP_GET_REG_FIELD(irtc_reg, YEARMON, YROFST) + RTC_NXP_IRTC_YEAR_OFFSET;
117128
if (data->is_dst_enabled) {
118129
timeptr->tm_isdst =
119130
((irtc_reg->CTRL & RTC_CTRL_DST_EN_MASK) >> RTC_CTRL_DST_EN_SHIFT);
@@ -156,6 +167,8 @@ static int nxp_irtc_alarm_set_time(const struct device *dev, uint16_t id, uint16
156167
return -EINVAL;
157168
}
158169

170+
data->alarm_pending = false;
171+
159172
uint32_t key = irq_lock();
160173

161174
nxp_irtc_unlock_registers(irtc_reg);
@@ -186,19 +199,25 @@ static int nxp_irtc_alarm_set_time(const struct device *dev, uint16_t id, uint16
186199
}
187200

188201
/* Clearing out the ALARM Flag Field then setting the correct value */
189-
irtc_reg->CTRL &= ~(0xC);
190-
switch (mask) {
191-
case 0x0F:
192-
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x4);
202+
irtc_reg->CTRL &= ~((uint16_t)RTC_CTRL_ALM_MATCH_MASK);
203+
uint8_t year_month_day_mask = (mask & (BIT(3) | BIT(4) | BIT(5))) >> 3;
204+
205+
switch (year_month_day_mask) {
206+
case 0x00:
207+
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x0);
208+
break;
209+
case 0x01:
210+
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x1);
193211
break;
194-
case 0x1F:
195-
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x8);
212+
case 0x03:
213+
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x2);
196214
break;
197-
case 0x3F:
198-
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0xC);
215+
case 0x07:
216+
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x3);
199217
break;
200218
default:
201-
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x0);
219+
irq_unlock(key);
220+
return -EINVAL;
202221
}
203222

204223
/* Enabling Alarm Interrupts */
@@ -218,39 +237,45 @@ static int nxp_irtc_alarm_get_time(const struct device *dev, uint16_t id, uint16
218237
RTC_Type *irtc_reg = config->base;
219238
uint16_t curr_alarm_mask = data->alarm_mask;
220239
uint16_t return_mask = 0;
240+
uint16_t tmp_hourmin = irtc_reg->ALM_HOURMIN;
241+
uint16_t tmp_yearmon = irtc_reg->ALM_YEARMON;
221242

222243
if (id != 0 || !timeptr) {
223244
return -EINVAL;
224245
}
225246

247+
memset(timeptr, 0, sizeof(struct rtc_time));
248+
226249
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_SECOND) {
227-
timeptr->tm_sec = RTC_NXP_GET_REG_FIELD(irtc_reg, ALM_SECONDS, ALM_SEC);
250+
timeptr->tm_sec = (irtc_reg->ALM_SECONDS) & RTC_ALM_SECONDS_ALM_SEC_MASK;
228251
return_mask |= RTC_ALARM_TIME_MASK_SECOND;
229252
}
230253

231254
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_MINUTE) {
232-
timeptr->tm_min = RTC_NXP_GET_REG_FIELD(irtc_reg, HOURMIN, MIN_CNT);
255+
timeptr->tm_min = tmp_hourmin & RTC_ALM_HOURMIN_ALM_MIN_MASK;
233256
return_mask |= RTC_ALARM_TIME_MASK_MINUTE;
234257
}
235258

236259
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_HOUR) {
237-
timeptr->tm_hour = RTC_NXP_GET_REG_FIELD(irtc_reg, HOURMIN, HOUR_CNT);
260+
timeptr->tm_hour = ((tmp_hourmin & RTC_ALM_HOURMIN_ALM_HOUR_MASK) >>
261+
RTC_ALM_HOURMIN_ALM_HOUR_SHIFT);
238262
return_mask |= RTC_ALARM_TIME_MASK_HOUR;
239263
}
240264

241265
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_MONTHDAY) {
242-
timeptr->tm_mday = RTC_NXP_GET_REG_FIELD(irtc_reg, DAYS, DAY_CNT);
266+
timeptr->tm_mday = (irtc_reg->ALM_DAYS) & RTC_ALM_DAYS_ALM_DAY_MASK;
243267
return_mask |= RTC_ALARM_TIME_MASK_MONTHDAY;
244268
}
245269

246270
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_MONTH) {
247-
timeptr->tm_mon = RTC_NXP_GET_REG_FIELD(irtc_reg, YEARMON, MON_CNT) - 1;
271+
timeptr->tm_mon = (tmp_yearmon & RTC_ALM_YEARMON_ALM_MON_MASK) - 1;
248272
return_mask |= RTC_ALARM_TIME_MASK_MONTH;
249273
}
250274

251275
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_YEAR) {
252-
timeptr->tm_year = (int8_t)RTC_NXP_GET_REG_FIELD(irtc_reg, YEARMON, YROFST) +
253-
RTC_NXP_IRTC_YEAR_OFFSET;
276+
timeptr->tm_year = (int8_t)((tmp_yearmon & RTC_ALM_YEARMON_ALM_YEAR_MASK) >>
277+
RTC_ALM_YEARMON_ALM_YEAR_SHIFT) +
278+
RTC_NXP_IRTC_YEAR_OFFSET;
254279
return_mask |= RTC_ALARM_TIME_MASK_YEAR;
255280
}
256281

@@ -262,13 +287,18 @@ static int nxp_irtc_alarm_get_time(const struct device *dev, uint16_t id, uint16
262287
static int nxp_irtc_alarm_is_pending(const struct device *dev, uint16_t id)
263288
{
264289
struct nxp_irtc_data *data = dev->data;
265-
RTC_Type *irtc_reg = config->base;
290+
int ret = 0;
266291

267292
if (id != 0) {
268293
return -EINVAL;
269294
}
270295

271-
return RTC_ISR_ALM_IS(0x4);
296+
__disable_irq();
297+
ret = data->alarm_pending ? 1 : 0;
298+
data->alarm_pending = false;
299+
__enable_irq();
300+
301+
return ret;
272302
}
273303

274304
static int nxp_irtc_alarm_set_callback(const struct device *dev, uint16_t id,
@@ -325,12 +355,32 @@ static int nxp_irtc_init(const struct device *dev)
325355
{
326356
const struct nxp_irtc_config *config = dev->config;
327357
RTC_Type *irtc_reg = config->base;
358+
uint16_t reg = 0;
359+
#ifdef CONFIG_RTC_ALARM
360+
struct nxp_irtc_data *data = dev->data;
361+
#endif
328362

329363
nxp_irtc_unlock_registers(irtc_reg);
330364

331-
/* set the control register bits */
332-
irtc_reg->CTRL = RTC_CTRL_CLK_SEL(config->clock_src) |
333-
RTC_CTRL_CLKO_DIS(!config->is_output_clock_enabled);
365+
reg = irtc_reg->CTRL;
366+
reg &= ~(uint16_t)RTC_CTRL_CLKO_DIS_MASK;
367+
#if DT_ANY_INST_HAS_PROP_STATUS_OKAY(clock_src)
368+
reg &= ~(uint16_t)RTC_CTRL_CLK_SEL_MASK;
369+
#endif
370+
371+
reg |= RTC_CTRL_CLKO_DIS(!config->is_output_clock_enabled);
372+
#if DT_ANY_INST_HAS_PROP_STATUS_OKAY(clock_src)
373+
reg |= RTC_CTRL_CLK_SEL(config->clock_src);
374+
#endif
375+
376+
irtc_reg->CTRL = reg;
377+
378+
#ifdef CONFIG_RTC_ALARM
379+
data->alarm_callback = NULL;
380+
data->alarm_user_data = NULL;
381+
data->alarm_mask = 0;
382+
data->alarm_pending = false;
383+
#endif
334384

335385
config->irq_config_func(dev);
336386

@@ -348,9 +398,11 @@ static void nxp_irtc_isr(const struct device *dev)
348398
nxp_irtc_unlock_registers(irtc_reg);
349399
/* Clearing ISR Register since w1c */
350400
irtc_reg->ISR = irtc_reg->ISR;
351-
352401
if (data->alarm_callback) {
353402
data->alarm_callback(dev, 0, data->alarm_user_data);
403+
data->alarm_pending = false;
404+
} else {
405+
data->alarm_pending = true;
354406
}
355407
irq_unlock(key);
356408
#endif /* CONFIG_RTC_ALARM */
@@ -375,17 +427,41 @@ static DEVICE_API(rtc, rtc_nxp_irtc_driver_api) = {
375427
#endif /* CONFIG_RTC_CALIBRATION */
376428
};
377429

378-
#define RTC_NXP_IRTC_DEVICE_INIT(n) \
430+
#define RTC_NXP_IRTC_IRQ_CONNECT(n, m) \
431+
do { \
432+
IRQ_CONNECT(DT_INST_IRQ_BY_IDX(n, m, irq), DT_INST_IRQ_BY_IDX(n, m, priority), \
433+
nxp_irtc_isr, DEVICE_DT_INST_GET(n), 0); \
434+
irq_enable(DT_INST_IRQ_BY_IDX(n, m, irq)); \
435+
} while (false)
436+
437+
#if DT_INST_IRQ_HAS_IDX(0, 1)
438+
#define NXP_IRTC_CONFIG_FUNC(n) \
379439
static void nxp_irtc_config_func_##n(const struct device *dev) \
380440
{ \
381-
IRQ_CONNECT(DT_INST_IRQN(n), DT_INST_IRQ(n, priority), nxp_irtc_isr, \
382-
DEVICE_DT_INST_GET(n), 0); \
383-
irq_enable(DT_INST_IRQN(n)); \
384-
} \
441+
RTC_NXP_IRTC_IRQ_CONNECT(n, 0); \
442+
RTC_NXP_IRTC_IRQ_CONNECT(n, 1); \
443+
}
444+
#else
445+
#define NXP_IRTC_CONFIG_FUNC(n) \
446+
static void nxp_irtc_config_func_##n(const struct device *dev) \
447+
{ \
448+
RTC_NXP_IRTC_IRQ_CONNECT(n, 0); \
449+
}
450+
#endif
451+
452+
#if DT_ANY_INST_HAS_PROP_STATUS_OKAY(clock_src)
453+
#define NXP_IRTC_CLOCK_SELECTION_INIT(n) .clock_src = DT_INST_PROP(n, clock_src),
454+
#else
455+
#define NXP_IRTC_CLOCK_SELECTION_INIT(n)
456+
#endif
457+
458+
#define RTC_NXP_IRTC_DEVICE_INIT(n) \
459+
NXP_IRTC_CONFIG_FUNC(n) \
385460
static const struct nxp_irtc_config nxp_irtc_config_##n = { \
386461
.base = (RTC_Type *)DT_INST_REG_ADDR(n), \
387-
.clock_src = DT_INST_PROP(n, clock_src), \
388-
.is_output_clock_enabled = DT_INST_PROP(n, output_clk_en), \
462+
NXP_IRTC_CLOCK_SELECTION_INIT(n) \
463+
.share_counter = DT_INST_PROP(n, share_counter), \
464+
.is_output_clock_enabled = DT_INST_PROP(n, output_clk_en), \
389465
.irq_config_func = nxp_irtc_config_func_##n, \
390466
}; \
391467
\

dts/arm/nxp/nxp_rt7xx_cm33_cpu0.dtsi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@
156156
#reset-cells = <1>;
157157
};
158158

159+
rtc0: rtc@68000 {
160+
compatible = "nxp,irtc";
161+
reg = <0x68000 0x1000>;
162+
interrupts = <26 0>, <27 0>;
163+
clock-frequency = <32768>;
164+
prescaler = <1>;
165+
alarms-count = <1>;
166+
status = "disabled";
167+
};
168+
159169
clkctl0: clkctl@1000 {
160170
compatible = "nxp,lpc-syscon";
161171
reg = <0x1000 0x1000>;

dts/arm/nxp/nxp_rt7xx_cm33_cpu1.dtsi

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@
131131
#reset-cells = <1>;
132132
};
133133

134+
rtc1: rtc@69000 {
135+
compatible = "nxp,irtc";
136+
reg = <0x69000 0x1000>;
137+
interrupts = <23 0>, <24 0>;
138+
clock-frequency = <32768>;
139+
prescaler = <1>;
140+
alarms-count = <1>;
141+
share-counter;
142+
status = "disabled";
143+
};
144+
134145
clkctl1: clkctl@41000 {
135146
compatible = "nxp,lpc-syscon";
136147
reg = <0x41000 0x1000>;

dts/bindings/rtc/nxp,irtc.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2024 NXP
1+
# Copyright 2024-2025 NXP
22
# SPDX-License-Identifier: Apache-2.0
33

44
description: IRTC
@@ -15,7 +15,6 @@ properties:
1515

1616
clock-src:
1717
type: int
18-
required: true
1918
enum:
2019
- 0
2120
- 1
@@ -40,3 +39,9 @@ properties:
4039
3 <- Coarse 1Hz
4140
Default value ensures the output clock is turned off.
4241
This is the reset value.
42+
43+
share-counter:
44+
type: boolean
45+
description: |
46+
This secondary IRTC instance shares the data and time counters of the primary IRTC instance.
47+
This means the code cannot set the data and time counters, but can only read them.

0 commit comments

Comments
 (0)