Skip to content

Commit c60f477

Browse files
VCASTMfourmone
authored andcommitted
rtc: stm32: fix issues of stm32_rtc_valid_alrm function
stm32_rtc_valid_alrm function have some issues : - arithmetical operations are impossible on BCD values - "cur_mon + 1" can overflow - the use case with the next month, the same day/hour/minutes went wrong To solve that, we prefer to use timestamp comparison. e.g. : On 5 Dec. 2021, the alarm limit is 5 Jan. 2022 (+31 days) On 31 Jan 2021, the alarm limit is 28 Feb. 2022 (+28 days) Signed-off-by: Valentin Caron <valentin.caron@foss.st.com> Change-Id: I8b694f8e54c0ea7eda6c5080be1b00cad4f20cb5 Reviewed-on: https://gerrit.st.com/c/mpu/oe/st/linux-stm32/+/240381 Reviewed-by: CITOOLS <MDG-smet-aci-reviews@list.st.com> Reviewed-by: CIBUILD <MDG-smet-aci-builds@list.st.com> Reviewed-by: Fabien DESSENNE <fabien.dessenne@foss.st.com>
1 parent c80b7ca commit c60f477

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

drivers/rtc/rtc-stm32.c

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@
104104
/* Max STM32 RTC register offset is 0x3FC */
105105
#define UNDEF_REG 0xFFFF
106106

107+
/* STM32 RTC driver time helpers */
108+
#define SEC_PER_DAY (24 * 60 * 60)
109+
107110
struct stm32_rtc;
108111

109112
struct stm32_rtc_registers {
@@ -522,40 +525,42 @@ static int stm32_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
522525
return 0;
523526
}
524527

525-
static int stm32_rtc_valid_alrm(struct stm32_rtc *rtc, struct rtc_time *tm)
528+
static int stm32_rtc_valid_alrm(struct device *dev, struct rtc_time *tm)
526529
{
527-
const struct stm32_rtc_registers *regs = &rtc->data->regs;
528-
int cur_day, cur_mon, cur_year, cur_hour, cur_min, cur_sec;
529-
unsigned int dr = readl_relaxed(rtc->base + regs->dr);
530-
unsigned int tr = readl_relaxed(rtc->base + regs->tr);
531-
532-
cur_day = (dr & STM32_RTC_DR_DATE) >> STM32_RTC_DR_DATE_SHIFT;
533-
cur_mon = (dr & STM32_RTC_DR_MONTH) >> STM32_RTC_DR_MONTH_SHIFT;
534-
cur_year = (dr & STM32_RTC_DR_YEAR) >> STM32_RTC_DR_YEAR_SHIFT;
535-
cur_sec = (tr & STM32_RTC_TR_SEC) >> STM32_RTC_TR_SEC_SHIFT;
536-
cur_min = (tr & STM32_RTC_TR_MIN) >> STM32_RTC_TR_MIN_SHIFT;
537-
cur_hour = (tr & STM32_RTC_TR_HOUR) >> STM32_RTC_TR_HOUR_SHIFT;
530+
static struct rtc_time now;
531+
time64_t max_alarm_time64;
532+
int max_day_forward;
533+
int next_month;
534+
int next_year;
538535

539536
/*
540537
* Assuming current date is M-D-Y H:M:S.
541538
* RTC alarm can't be set on a specific month and year.
542539
* So the valid alarm range is:
543540
* M-D-Y H:M:S < alarm <= (M+1)-D-Y H:M:S
544-
* with a specific case for December...
545541
*/
546-
if ((((tm->tm_year > cur_year) &&
547-
(tm->tm_mon == 0x1) && (cur_mon == 0x12)) ||
548-
((tm->tm_year == cur_year) &&
549-
(tm->tm_mon <= cur_mon + 1))) &&
550-
((tm->tm_mday > cur_day) ||
551-
((tm->tm_mday == cur_day) &&
552-
((tm->tm_hour > cur_hour) ||
553-
((tm->tm_hour == cur_hour) && (tm->tm_min > cur_min)) ||
554-
((tm->tm_hour == cur_hour) && (tm->tm_min == cur_min) &&
555-
(tm->tm_sec >= cur_sec))))))
556-
return 0;
542+
stm32_rtc_read_time(dev, &now);
543+
544+
/*
545+
* Find the next month and the year of the next month.
546+
* Note: tm_mon and next_month are from 0 to 11
547+
*/
548+
next_month = now.tm_mon + 1;
549+
if (next_month == 12) {
550+
next_month = 0;
551+
next_year = now.tm_year + 1;
552+
} else {
553+
next_year = now.tm_year;
554+
}
557555

558-
return -EINVAL;
556+
/* Find the maximum limit of alarm in days. */
557+
max_day_forward = rtc_month_days(now.tm_mon, now.tm_year)
558+
- now.tm_mday
559+
+ min(rtc_month_days(next_month, next_year), now.tm_mday);
560+
561+
/* Convert to timestamp and compare the alarm time and its upper limit */
562+
max_alarm_time64 = rtc_tm_to_time64(&now) + max_day_forward * SEC_PER_DAY;
563+
return rtc_tm_to_time64(tm) <= max_alarm_time64 ? 0 : -EINVAL;
559564
}
560565

561566
static int stm32_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
@@ -566,17 +571,17 @@ static int stm32_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
566571
unsigned int cr, isr, alrmar;
567572
int ret = 0;
568573

569-
tm2bcd(tm);
570-
571574
/*
572575
* RTC alarm can't be set on a specific date, unless this date is
573576
* up to the same day of month next month.
574577
*/
575-
if (stm32_rtc_valid_alrm(rtc, tm) < 0) {
578+
if (stm32_rtc_valid_alrm(dev, tm) < 0) {
576579
dev_err(dev, "Alarm can be set only on upcoming month.\n");
577580
return -EINVAL;
578581
}
579582

583+
tm2bcd(tm);
584+
580585
alrmar = 0;
581586
/* tm_year and tm_mon are not used because not supported by RTC */
582587
alrmar |= (tm->tm_mday << STM32_RTC_ALRMXR_DATE_SHIFT) &

0 commit comments

Comments
 (0)