Skip to content

Commit 7bd5a0a

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/+/239160 Reviewed-by: CITOOLS <MDG-smet-aci-reviews@list.st.com> Reviewed-by: Eric FOURMONT <eric.fourmont-ext@st.com> Tested-by: Eric FOURMONT <eric.fourmont-ext@st.com>
1 parent 7d34eca commit 7bd5a0a

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

drivers/rtc/rtc-stm32.c

+33-28
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 {
@@ -521,40 +524,42 @@ static int stm32_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
521524
return 0;
522525
}
523526

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

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

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

560565
static int stm32_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
@@ -565,17 +570,17 @@ static int stm32_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
565570
unsigned int cr, isr, alrmar;
566571
int ret = 0;
567572

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

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

0 commit comments

Comments
 (0)