Skip to content

Commit 2a1ab77

Browse files
committed
Driver RTC: 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. Signed-off-by: Holt.Sun <holt.sun@nxp.com>
1 parent 4bd1d39 commit 2a1ab77

File tree

9 files changed

+165
-40
lines changed

9 files changed

+165
-40
lines changed

drivers/rtc/rtc_nxp_irtc.c

Lines changed: 91 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,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);
@@ -186,19 +187,24 @@ static int nxp_irtc_alarm_set_time(const struct device *dev, uint16_t id, uint16
186187
}
187188

188189
/* 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);
190+
irtc_reg->CTRL &= ~((uint16_t)RTC_CTRL_ALM_MATCH_MASK);
191+
uint8_t year_month_day_mask = (mask & (BIT(3) | BIT(4) | BIT(5))) >> 3;
192+
switch (year_month_day_mask) {
193+
case 0x00:
194+
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x0);
195+
break;
196+
case 0x01:
197+
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x1);
193198
break;
194-
case 0x1F:
195-
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x8);
199+
case 0x03:
200+
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x2);
196201
break;
197-
case 0x3F:
198-
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0xC);
202+
case 0x07:
203+
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x3);
199204
break;
200205
default:
201-
irtc_reg->CTRL |= RTC_CTRL_ALM_MATCH(0x0);
206+
irq_unlock(key);
207+
return -EINVAL;
202208
}
203209

204210
/* Enabling Alarm Interrupts */
@@ -218,39 +224,45 @@ static int nxp_irtc_alarm_get_time(const struct device *dev, uint16_t id, uint16
218224
RTC_Type *irtc_reg = config->base;
219225
uint16_t curr_alarm_mask = data->alarm_mask;
220226
uint16_t return_mask = 0;
227+
uint16_t tmp_hourmin = irtc_reg->ALM_HOURMIN;
228+
uint16_t tmp_yearmon = irtc_reg->ALM_YEARMON;
221229

222230
if (id != 0 || !timeptr) {
223231
return -EINVAL;
224232
}
225233

234+
memset(timeptr, 0, sizeof(struct rtc_time));
235+
226236
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_SECOND) {
227-
timeptr->tm_sec = RTC_NXP_GET_REG_FIELD(irtc_reg, ALM_SECONDS, ALM_SEC);
237+
timeptr->tm_sec = (irtc_reg->ALM_SECONDS) & RTC_ALM_SECONDS_ALM_SEC_MASK;
228238
return_mask |= RTC_ALARM_TIME_MASK_SECOND;
229239
}
230240

231241
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_MINUTE) {
232-
timeptr->tm_min = RTC_NXP_GET_REG_FIELD(irtc_reg, HOURMIN, MIN_CNT);
242+
timeptr->tm_min = tmp_hourmin & RTC_ALM_HOURMIN_ALM_MIN_MASK;
233243
return_mask |= RTC_ALARM_TIME_MASK_MINUTE;
234244
}
235245

236246
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_HOUR) {
237-
timeptr->tm_hour = RTC_NXP_GET_REG_FIELD(irtc_reg, HOURMIN, HOUR_CNT);
247+
timeptr->tm_hour = ((tmp_hourmin & RTC_ALM_HOURMIN_ALM_HOUR_MASK) >>
248+
RTC_ALM_HOURMIN_ALM_HOUR_SHIFT);
238249
return_mask |= RTC_ALARM_TIME_MASK_HOUR;
239250
}
240251

241252
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_MONTHDAY) {
242-
timeptr->tm_mday = RTC_NXP_GET_REG_FIELD(irtc_reg, DAYS, DAY_CNT);
253+
timeptr->tm_mday = (irtc_reg->ALM_DAYS) & RTC_ALM_DAYS_ALM_DAY_MASK;
243254
return_mask |= RTC_ALARM_TIME_MASK_MONTHDAY;
244255
}
245256

246257
if (curr_alarm_mask & RTC_ALARM_TIME_MASK_MONTH) {
247-
timeptr->tm_mon = RTC_NXP_GET_REG_FIELD(irtc_reg, YEARMON, MON_CNT) - 1;
258+
timeptr->tm_mon = (tmp_yearmon & RTC_ALM_YEARMON_ALM_MON_MASK) - 1;
248259
return_mask |= RTC_ALARM_TIME_MASK_MONTH;
249260
}
250261

251262
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;
263+
timeptr->tm_year = (int8_t)((tmp_yearmon & RTC_ALM_YEARMON_ALM_YEAR_MASK) >>
264+
RTC_ALM_YEARMON_ALM_YEAR_SHIFT) +
265+
RTC_NXP_IRTC_YEAR_OFFSET;
254266
return_mask |= RTC_ALARM_TIME_MASK_YEAR;
255267
}
256268

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

262274
static int nxp_irtc_alarm_is_pending(const struct device *dev, uint16_t id)
263275
{
264-
struct nxp_irtc_data *data = dev->data;
276+
const struct nxp_irtc_config *config = dev->config;
265277
RTC_Type *irtc_reg = config->base;
266278

267279
if (id != 0) {
268280
return -EINVAL;
269281
}
270282

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

274295
static int nxp_irtc_alarm_set_callback(const struct device *dev, uint16_t id,
@@ -325,12 +346,22 @@ static int nxp_irtc_init(const struct device *dev)
325346
{
326347
const struct nxp_irtc_config *config = dev->config;
327348
RTC_Type *irtc_reg = config->base;
349+
uint16_t reg = 0;
328350

329351
nxp_irtc_unlock_registers(irtc_reg);
330352

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

335366
config->irq_config_func(dev);
336367

@@ -348,7 +379,6 @@ static void nxp_irtc_isr(const struct device *dev)
348379
nxp_irtc_unlock_registers(irtc_reg);
349380
/* Clearing ISR Register since w1c */
350381
irtc_reg->ISR = irtc_reg->ISR;
351-
352382
if (data->alarm_callback) {
353383
data->alarm_callback(dev, 0, data->alarm_user_data);
354384
}
@@ -375,17 +405,40 @@ static DEVICE_API(rtc, rtc_nxp_irtc_driver_api) = {
375405
#endif /* CONFIG_RTC_CALIBRATION */
376406
};
377407

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

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:

samples/drivers/rtc/src/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
const struct device *const rtc = DEVICE_DT_GET(DT_ALIAS(rtc));
1313

14+
#if !(DT_PROP(DT_ALIAS(rtc), share_counter))
1415
static int set_date_time(const struct device *rtc)
1516
{
1617
int ret = 0;
@@ -30,6 +31,7 @@ static int set_date_time(const struct device *rtc)
3031
}
3132
return ret;
3233
}
34+
#endif
3335

3436
static int get_date_time(const struct device *rtc)
3537
{
@@ -56,7 +58,9 @@ int main(void)
5658
return 0;
5759
}
5860

61+
#if !(DT_PROP(DT_ALIAS(rtc), share_counter))
5962
set_date_time(rtc);
63+
#endif
6064

6165
/* Continuously read the current date and time from the RTC */
6266
while (get_date_time(rtc) == 0) {

0 commit comments

Comments
 (0)