Commit 46828a5f authored by Valentin Caron's avatar Valentin Caron Committed by Alexandre Belloni

rtc: stm32: fix issues of stm32_rtc_valid_alrm function

stm32_rtc_valid_alrm function has 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: default avatarValentin Caron <valentin.caron@foss.st.com>
Link: https://lore.kernel.org/r/20230705174357.353616-7-valentin.caron@foss.st.comSigned-off-by: default avatarAlexandre Belloni <alexandre.belloni@bootlin.com>
parent fb9a7e53
...@@ -90,6 +90,9 @@ ...@@ -90,6 +90,9 @@
/* Max STM32 RTC register offset is 0x3FC */ /* Max STM32 RTC register offset is 0x3FC */
#define UNDEF_REG 0xFFFF #define UNDEF_REG 0xFFFF
/* STM32 RTC driver time helpers */
#define SEC_PER_DAY (24 * 60 * 60)
struct stm32_rtc; struct stm32_rtc;
struct stm32_rtc_registers { struct stm32_rtc_registers {
...@@ -427,40 +430,42 @@ static int stm32_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) ...@@ -427,40 +430,42 @@ static int stm32_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
return 0; return 0;
} }
static int stm32_rtc_valid_alrm(struct stm32_rtc *rtc, struct rtc_time *tm) static int stm32_rtc_valid_alrm(struct device *dev, struct rtc_time *tm)
{ {
const struct stm32_rtc_registers *regs = &rtc->data->regs; static struct rtc_time now;
int cur_day, cur_mon, cur_year, cur_hour, cur_min, cur_sec; time64_t max_alarm_time64;
unsigned int tr = readl_relaxed(rtc->base + regs->tr); int max_day_forward;
unsigned int dr = readl_relaxed(rtc->base + regs->dr); int next_month;
int next_year;
cur_day = (dr & STM32_RTC_DR_DATE) >> STM32_RTC_DR_DATE_SHIFT;
cur_mon = (dr & STM32_RTC_DR_MONTH) >> STM32_RTC_DR_MONTH_SHIFT;
cur_year = (dr & STM32_RTC_DR_YEAR) >> STM32_RTC_DR_YEAR_SHIFT;
cur_sec = (tr & STM32_RTC_TR_SEC) >> STM32_RTC_TR_SEC_SHIFT;
cur_min = (tr & STM32_RTC_TR_MIN) >> STM32_RTC_TR_MIN_SHIFT;
cur_hour = (tr & STM32_RTC_TR_HOUR) >> STM32_RTC_TR_HOUR_SHIFT;
/* /*
* Assuming current date is M-D-Y H:M:S. * Assuming current date is M-D-Y H:M:S.
* RTC alarm can't be set on a specific month and year. * RTC alarm can't be set on a specific month and year.
* So the valid alarm range is: * So the valid alarm range is:
* M-D-Y H:M:S < alarm <= (M+1)-D-Y H:M:S * M-D-Y H:M:S < alarm <= (M+1)-D-Y H:M:S
* with a specific case for December...
*/ */
if ((((tm->tm_year > cur_year) && stm32_rtc_read_time(dev, &now);
(tm->tm_mon == 0x1) && (cur_mon == 0x12)) ||
((tm->tm_year == cur_year) && /*
(tm->tm_mon <= cur_mon + 1))) && * Find the next month and the year of the next month.
((tm->tm_mday > cur_day) || * Note: tm_mon and next_month are from 0 to 11
((tm->tm_mday == cur_day) && */
((tm->tm_hour > cur_hour) || next_month = now.tm_mon + 1;
((tm->tm_hour == cur_hour) && (tm->tm_min > cur_min)) || if (next_month == 12) {
((tm->tm_hour == cur_hour) && (tm->tm_min == cur_min) && next_month = 0;
(tm->tm_sec >= cur_sec)))))) next_year = now.tm_year + 1;
return 0; } else {
next_year = now.tm_year;
}
return -EINVAL; /* Find the maximum limit of alarm in days. */
max_day_forward = rtc_month_days(now.tm_mon, now.tm_year)
- now.tm_mday
+ min(rtc_month_days(next_month, next_year), now.tm_mday);
/* Convert to timestamp and compare the alarm time and its upper limit */
max_alarm_time64 = rtc_tm_to_time64(&now) + max_day_forward * SEC_PER_DAY;
return rtc_tm_to_time64(tm) <= max_alarm_time64 ? 0 : -EINVAL;
} }
static int stm32_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) static int stm32_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
...@@ -471,17 +476,17 @@ static int stm32_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) ...@@ -471,17 +476,17 @@ static int stm32_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm)
unsigned int cr, isr, alrmar; unsigned int cr, isr, alrmar;
int ret = 0; int ret = 0;
tm2bcd(tm);
/* /*
* RTC alarm can't be set on a specific date, unless this date is * RTC alarm can't be set on a specific date, unless this date is
* up to the same day of month next month. * up to the same day of month next month.
*/ */
if (stm32_rtc_valid_alrm(rtc, tm) < 0) { if (stm32_rtc_valid_alrm(dev, tm) < 0) {
dev_err(dev, "Alarm can be set only on upcoming month.\n"); dev_err(dev, "Alarm can be set only on upcoming month.\n");
return -EINVAL; return -EINVAL;
} }
tm2bcd(tm);
alrmar = 0; alrmar = 0;
/* tm_year and tm_mon are not used because not supported by RTC */ /* tm_year and tm_mon are not used because not supported by RTC */
alrmar |= (tm->tm_mday << STM32_RTC_ALRMXR_DATE_SHIFT) & alrmar |= (tm->tm_mday << STM32_RTC_ALRMXR_DATE_SHIFT) &
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment