Re: [PATCH 1/2] rtc: m48t59: Accommodate chips that lack a century bit
From: Alexandre Belloni
Date: Thu Oct 03 2024 - 04:10:27 EST
Hello,
On 03/10/2024 13:23:22+1000, Finn Thain wrote:
> The m48t59 driver is needed by both SPARC and MVME systems. Linux on
> MVME uses 1970 as "year zero" rather than 1968 that's used on SPARC.
> Add support for the MVME convention. Otherwise, the RTC on non-SPARC
> systems can only read and write dates between 1900 and 1999.
>
> Tested-by: Daniel Palmer <daniel@xxxxxxxx>
> Signed-off-by: Finn Thain <fthain@xxxxxxxxxxxxxx>
> ---
> drivers/rtc/rtc-m48t59.c | 31 +++++++++++++++----------------
> 1 file changed, 15 insertions(+), 16 deletions(-)
>
> diff --git a/drivers/rtc/rtc-m48t59.c b/drivers/rtc/rtc-m48t59.c
> index f0f6b9b6daec..e2d882ea5c2f 100644
> --- a/drivers/rtc/rtc-m48t59.c
> +++ b/drivers/rtc/rtc-m48t59.c
> @@ -57,6 +57,17 @@ m48t59_mem_readb(struct device *dev, u32 ofs)
> return readb(m48t59->ioaddr+ofs);
> }
>
> +/*
> + * Sun SPARC machines count years since 1968. MVME machines running Linux
> + * count years since 1970.
> + */
> +
> +#ifdef CONFIG_SPARC
> +#define YEAR0 68
> +#else
> +#define YEAR0 70
> +#endif
> +
> /*
> * NOTE: M48T59 only uses BCD mode
> */
> @@ -82,10 +93,7 @@ static int m48t59_rtc_read_time(struct device *dev, struct rtc_time *tm)
> dev_dbg(dev, "Century bit is enabled\n");
> tm->tm_year += 100; /* one century */
> }
> -#ifdef CONFIG_SPARC
> - /* Sun SPARC machines count years since 1968 */
> - tm->tm_year += 68;
> -#endif
> + tm->tm_year += YEAR0;
>
I'm super happy to see someone working on this, while you are it, can
you use m48t59->rtc->start_secs and m48t59->rtc->set_start_time in probe
instead of offsetting tm_year in read_time/set_time so we can later use
device tree or any other mechanism to extend the range?
It is super funny because I was telling Geert that I wanted to get rid
of this #ifdef CONFIG_SPARC two weeks ago at LPC. That could indeed then
come from platform data.
> tm->tm_wday = bcd2bin(val & 0x07);
> tm->tm_hour = bcd2bin(M48T59_READ(M48T59_HOUR) & 0x3F);
> @@ -108,10 +116,7 @@ static int m48t59_rtc_set_time(struct device *dev, struct rtc_time *tm)
> u8 val = 0;
> int year = tm->tm_year;
>
> -#ifdef CONFIG_SPARC
> - /* Sun SPARC machines count years since 1968 */
> - year -= 68;
> -#endif
> + year -= YEAR0;
>
> dev_dbg(dev, "RTC set time %04d-%02d-%02d %02d/%02d/%02d\n",
> year + 1900, tm->tm_mon, tm->tm_mday,
> @@ -163,10 +168,7 @@ static int m48t59_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
> M48T59_SET_BITS(M48T59_CNTL_READ, M48T59_CNTL);
>
> tm->tm_year = bcd2bin(M48T59_READ(M48T59_YEAR));
> -#ifdef CONFIG_SPARC
> - /* Sun SPARC machines count years since 1968 */
> - tm->tm_year += 68;
> -#endif
> + tm->tm_year += YEAR0;
> /* tm_mon is 0-11 */
> tm->tm_mon = bcd2bin(M48T59_READ(M48T59_MONTH)) - 1;
>
> @@ -199,10 +201,7 @@ static int m48t59_rtc_setalarm(struct device *dev, struct rtc_wkalrm *alrm)
> unsigned long flags;
> int year = tm->tm_year;
>
> -#ifdef CONFIG_SPARC
> - /* Sun SPARC machines count years since 1968 */
> - year -= 68;
> -#endif
> + year -= YEAR0;
>
> /* If no irq, we don't support ALARM */
> if (m48t59->irq == NO_IRQ)
> --
> 2.39.5
>
--
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com