Skip to content

Commit cd40623

Browse files
committed
Driver RTC: Updated rtc driver to support NXP RT700.
1. Update nxp irtc driver to fix issue in init and alarm get 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. Signed-off-by: Holt.Sun <holt.sun@nxp.com>
1 parent 4bd1d39 commit cd40623

File tree

9 files changed

+167
-41
lines changed

9 files changed

+167
-41
lines changed

drivers/rtc/rtc_nxp_irtc.c

Lines changed: 93 additions & 39 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,9 @@ 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
1921
uint8_t alarm_match_flag;
2022
};
2123

@@ -31,9 +33,8 @@ struct nxp_irtc_data {
3133
/* The IRTC Offset is 2112 instead of 1900 [2112 - 1900] -> [212] */
3234
#define RTC_NXP_IRTC_YEAR_OFFSET 212
3335

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

3839
/*
3940
* The runtime where this is accessed is unknown so we force a lock on the registers then force an
@@ -79,12 +80,12 @@ static int nxp_irtc_set_time(const struct device *dev, const struct rtc_time *ti
7980
nxp_irtc_unlock_registers(irtc_reg);
8081
irtc_reg->SECONDS = RTC_SECONDS_SEC_CNT(timeptr->tm_sec);
8182

82-
irtc_reg->HOURMIN = RTC_HOURMIN_MIN_CNT(timeptr->tm_min) |
83-
RTC_HOURMIN_HOUR_CNT(timeptr->tm_hour);
83+
irtc_reg->HOURMIN =
84+
RTC_HOURMIN_MIN_CNT(timeptr->tm_min) | RTC_HOURMIN_HOUR_CNT(timeptr->tm_hour);
8485

8586
/* 1 is valid for rtc_time.tm_wday property but is out of bounds for IRTC registers */
8687
irtc_reg->DAYS = RTC_DAYS_DAY_CNT(timeptr->tm_mday) |
87-
(timeptr->tm_wday == -1 ? 0 : RTC_DAYS_DOW(timeptr->tm_wday));
88+
(timeptr->tm_wday == -1 ? 0 : RTC_DAYS_DOW(timeptr->tm_wday));
8889

8990
irtc_reg->YEARMON = RTC_YEARMON_MON_CNT(calc_month) | RTC_YEARMON_YROFST(calc_year);
9091

@@ -112,8 +113,8 @@ static int nxp_irtc_get_time(const struct device *dev, struct rtc_time *timeptr)
112113
timeptr->tm_wday = RTC_NXP_GET_REG_FIELD(irtc_reg, DAYS, DOW);
113114
timeptr->tm_mday = RTC_NXP_GET_REG_FIELD(irtc_reg, DAYS, DAY_CNT);
114115
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;
116+
timeptr->tm_year =
117+
(int8_t)RTC_NXP_GET_REG_FIELD(irtc_reg, YEARMON, YROFST) + RTC_NXP_IRTC_YEAR_OFFSET;
117118
if (data->is_dst_enabled) {
118119
timeptr->tm_isdst =
119120
((irtc_reg->CTRL & RTC_CTRL_DST_EN_MASK) >> RTC_CTRL_DST_EN_SHIFT);
@@ -160,8 +161,9 @@ static int nxp_irtc_alarm_set_time(const struct device *dev, uint16_t id, uint16
160161

161162
nxp_irtc_unlock_registers(irtc_reg);
162163

164+
uint16_t tmp_sec = RTC_ALM_SECONDS_ALM_SEC(timeptr->tm_sec);
163165
if (mask & RTC_ALARM_TIME_MASK_SECOND) {
164-
irtc_reg->ALM_SECONDS = RTC_ALM_SECONDS_ALM_SEC(timeptr->tm_sec);
166+
irtc_reg->ALM_SECONDS = tmp_sec;
165167
}
166168

167169
if (mask & RTC_ALARM_TIME_MASK_MINUTE) {
@@ -186,19 +188,24 @@ static int nxp_irtc_alarm_set_time(const struct device *dev, uint16_t id, uint16
186188
}
187189

188190
/* 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);
191+
irtc_reg->CTRL &= ~((uint16_t)RTC_CTRL_ALM_MATCH_MASK);
192+
uint8_t year_month_day_mask = (mask & (BIT(3) | BIT(4) | BIT(5))) >> 3;
193+
switch (year_month_day_mask) {
194+
case 0x00:
195+
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x0);
196+
break;
197+
case 0x01:
198+
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x1);
193199
break;
194-
case 0x1F:
195-
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x8);
200+
case 0x03:
201+
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x2);
196202
break;
197-
case 0x3F:
198-
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0xC);
203+
case 0x07:
204+
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x3);
199205
break;
200206
default:
201-
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x0);
207+
irq_unlock(key);
208+
return -EINVAL;
202209
}
203210

204211
/* Enabling Alarm Interrupts */
@@ -218,39 +225,45 @@ static int nxp_irtc_alarm_get_time(const struct device *dev, uint16_t id, uint16
218225
RTC_Type *irtc_reg = config->base;
219226
uint16_t curr_alarm_mask = data->alarm_mask;
220227
uint16_t return_mask = 0;
228+
uint16_t tmp_hourmin = irtc_reg->ALM_HOURMIN;
229+
uint16_t tmp_yearmon = irtc_reg->ALM_YEARMON;
221230

222231
if (id != 0 || !timeptr) {
223232
return -EINVAL;
224233
}
225234

235+
memset(timeptr, 0, sizeof(struct rtc_time));
236+
226237
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_SECOND) {
227-
timeptr->tm_sec = RTC_NXP_GET_REG_FIELD(irtc_reg, ALM_SECONDS, ALM_SEC);
238+
timeptr->tm_sec = (irtc_reg->ALM_SECONDS) & RTC_ALM_SECONDS_ALM_SEC_MASK;
228239
return_mask |= RTC_ALARM_TIME_MASK_SECOND;
229240
}
230241

231242
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_MINUTE) {
232-
timeptr->tm_min = RTC_NXP_GET_REG_FIELD(irtc_reg, HOURMIN, MIN_CNT);
243+
timeptr->tm_min = tmp_hourmin & RTC_ALM_HOURMIN_ALM_MIN_MASK;
233244
return_mask |= RTC_ALARM_TIME_MASK_MINUTE;
234245
}
235246

236247
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_HOUR) {
237-
timeptr->tm_hour = RTC_NXP_GET_REG_FIELD(irtc_reg, HOURMIN, HOUR_CNT);
248+
timeptr->tm_hour = ((tmp_hourmin & RTC_ALM_HOURMIN_ALM_HOUR_MASK) >>
249+
RTC_ALM_HOURMIN_ALM_HOUR_SHIFT);
238250
return_mask |= RTC_ALARM_TIME_MASK_HOUR;
239251
}
240252

241253
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_MONTHDAY) {
242-
timeptr->tm_mday = RTC_NXP_GET_REG_FIELD(irtc_reg, DAYS, DAY_CNT);
254+
timeptr->tm_mday = (irtc_reg->ALM_DAYS) & RTC_ALM_DAYS_ALM_DAY_MASK;
243255
return_mask |= RTC_ALARM_TIME_MASK_MONTHDAY;
244256
}
245257

246258
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_MONTH) {
247-
timeptr->tm_mon = RTC_NXP_GET_REG_FIELD(irtc_reg, YEARMON, MON_CNT) - 1;
259+
timeptr->tm_mon = (tmp_yearmon & RTC_ALM_YEARMON_ALM_MON_MASK) - 1;
248260
return_mask |= RTC_ALARM_TIME_MASK_MONTH;
249261
}
250262

251263
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;
264+
timeptr->tm_year = (int8_t)((tmp_yearmon & RTC_ALM_YEARMON_ALM_YEAR_MASK) >>
265+
RTC_ALM_YEARMON_ALM_YEAR_SHIFT) +
266+
RTC_NXP_IRTC_YEAR_OFFSET;
254267
return_mask |= RTC_ALARM_TIME_MASK_YEAR;
255268
}
256269

@@ -261,14 +274,23 @@ static int nxp_irtc_alarm_get_time(const struct device *dev, uint16_t id, uint16
261274

262275
static int nxp_irtc_alarm_is_pending(const struct device *dev, uint16_t id)
263276
{
264-
struct nxp_irtc_data *data = dev->data;
277+
const struct nxp_irtc_config *config = dev->config;
265278
RTC_Type *irtc_reg = config->base;
266279

267280
if (id != 0) {
268281
return -EINVAL;
269282
}
270283

271-
return RTC_ISR_ALM_IS(0x4);
284+
if ((irtc_reg->ISR & RTC_ISR_ALM_IS_MASK) != 0) {
285+
/* Clear the Alarm Interrupt */
286+
nxp_irtc_unlock_registers(irtc_reg);
287+
/* Alarm is pending */
288+
irtc_reg->ISR = RTC_ISR_ALM_IS_MASK;
289+
return 1;
290+
} else {
291+
/* Alarm is not pending */
292+
return 0;
293+
}
272294
}
273295

274296
static int nxp_irtc_alarm_set_callback(const struct device *dev, uint16_t id,
@@ -325,12 +347,22 @@ static int nxp_irtc_init(const struct device *dev)
325347
{
326348
const struct nxp_irtc_config *config = dev->config;
327349
RTC_Type *irtc_reg = config->base;
350+
uint16_t reg = 0;
328351

329352
nxp_irtc_unlock_registers(irtc_reg);
330353

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);
354+
reg = irtc_reg->CTRL;
355+
reg &= ~(uint16_t)RTC_CTRL_CLKO_DIS_MASK;
356+
#if DT_ANY_INST_HAS_PROP_STATUS_OKAY(clock_src)
357+
reg &= ~(uint16_t)RTC_CTRL_CLK_SEL_MASK;
358+
#endif
359+
360+
reg |= RTC_CTRL_CLKO_DIS(!config->is_output_clock_enabled);
361+
#if DT_ANY_INST_HAS_PROP_STATUS_OKAY(clock_src)
362+
reg |= RTC_CTRL_CLK_SEL(config->clock_src);
363+
#endif
364+
365+
irtc_reg->CTRL = reg;
334366

335367
config->irq_config_func(dev);
336368

@@ -348,7 +380,6 @@ static void nxp_irtc_isr(const struct device *dev)
348380
nxp_irtc_unlock_registers(irtc_reg);
349381
/* Clearing ISR Register since w1c */
350382
irtc_reg->ISR = irtc_reg->ISR;
351-
352383
if (data->alarm_callback) {
353384
data->alarm_callback(dev, 0, data->alarm_user_data);
354385
}
@@ -375,17 +406,40 @@ static DEVICE_API(rtc, rtc_nxp_irtc_driver_api) = {
375406
#endif /* CONFIG_RTC_CALIBRATION */
376407
};
377408

378-
#define RTC_NXP_IRTC_DEVICE_INIT(n) \
409+
#define RTC_NXP_IRTC_IRQ_CONNECT(n, m) \
410+
do { \
411+
IRQ_CONNECT(DT_INST_IRQ_BY_IDX(n, m, irq), DT_INST_IRQ_BY_IDX(n, m, priority), \
412+
nxp_irtc_isr, DEVICE_DT_INST_GET(n), 0); \
413+
irq_enable(DT_INST_IRQ_BY_IDX(n, m, irq)); \
414+
} while (false)
415+
416+
#if DT_INST_IRQ_HAS_IDX(0, 1)
417+
#define NXP_IRTC_CONFIG_FUNC(n) \
418+
static void nxp_irtc_config_func_##n(const struct device *dev) \
419+
{ \
420+
RTC_NXP_IRTC_IRQ_CONNECT(n, 0); \
421+
RTC_NXP_IRTC_IRQ_CONNECT(n, 1); \
422+
}
423+
#else
424+
#define NXP_IRTC_CONFIG_FUNC(n) \
379425
static void nxp_irtc_config_func_##n(const struct device *dev) \
380426
{ \
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-
} \
427+
RTC_NXP_IRTC_IRQ_CONNECT(n, 0); \
428+
}
429+
#endif
430+
431+
#if DT_ANY_INST_HAS_PROP_STATUS_OKAY(clock_src)
432+
#define NXP_IRTC_CLOCK_SELECTION_INIT(n) .clock_src = DT_INST_PROP(n, clock_src),
433+
#else
434+
#define NXP_IRTC_CLOCK_SELECTION_INIT(n)
435+
#endif
436+
437+
#define RTC_NXP_IRTC_DEVICE_INIT(n) \
438+
NXP_IRTC_CONFIG_FUNC(n) \
385439
static const struct nxp_irtc_config nxp_irtc_config_##n = { \
386440
.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), \
441+
NXP_IRTC_CLOCK_SELECTION_INIT(n).is_output_clock_enabled = \
442+
DT_INST_PROP(n, output_clk_en), \
389443
.irq_config_func = nxp_irtc_config_func_##n, \
390444
}; \
391445
\

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 slave irtc instance shares the data and time counters of the master irtc instance.
47+
This means the code cannot set the data and time counters, but can only read them.

samples/drivers/rtc/README.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ Build and flash as follows, replacing ``stm32f3_disco`` with your board:
2222
:goals: build flash
2323
:compact:
2424

25+
.. zephyr-app-commands::
26+
:zephyr-app: samples/drivers/rtc
27+
:board: mimxrt700_evk/mimxrt798s/cm33_cpu0
28+
:goals: build flash
29+
30+
.. zephyr-app-commands::
31+
:zephyr-app: samples/drivers/rtc
32+
:board: mimxrt700_evk/mimxrt798s/cm33_cpu1
33+
:goals: build ram
34+
2535
Sample Output
2636
=============
2737

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
/ {
8+
aliases {
9+
rtc = &rtc0;
10+
};
11+
};
12+
13+
&rtc0 {
14+
status = "okay";
15+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright 2025 NXP
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
/ {
8+
aliases {
9+
rtc = &rtc1;
10+
};
11+
};
12+
13+
&rtc1 {
14+
status = "okay";
15+
};

samples/drivers/rtc/sample.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ tests:
44
sample.drivers.rtc:
55
platform_allow:
66
- stm32f3_disco
7+
- mimxrt700_evk/mimxrt798s/cm33_cpu0
8+
- mimxrt700_evk/mimxrt798s/cm33_cpu1
79
integration_platforms:
810
- stm32f3_disco
911
tags:

0 commit comments

Comments
 (0)