linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rtc: ds1307: fix century bit support
@ 2016-07-13  0:34 Alexandre Belloni
  2016-07-14 19:54 ` Arnaud Ebalard
  0 siblings, 1 reply; 3+ messages in thread
From: Alexandre Belloni @ 2016-07-13  0:34 UTC (permalink / raw)
  To: Arnaud Ebalard
  Cc: Alessandro Zummo, rtc-linux, linux-kernel, Alexandre Belloni

Add an option to properly support the century bit of ds1337 and compatibles
and ds1340.
Because the driver had a bug until now, it is not possible to switch users
to the fixed code directly as RTCs in the field will wrongly have the
century bit set.

Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
---

Arnaud, do you mind testing that patch, I still don't have the necessary
hardware but I think this is the proper course of action.

 drivers/rtc/Kconfig      | 14 ++++++++++++++
 drivers/rtc/rtc-ds1307.c | 48 +++++++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 57 insertions(+), 5 deletions(-)

diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index 8526f1cded08..f47c2f5ff70d 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -224,6 +224,20 @@ config RTC_DRV_DS1307_HWMON
 	  Say Y here if you want to expose temperature sensor data on
 	  rtc-ds1307 (only DS3231)
 
+config RTC_DRV_DS1307_CENTURY
+	bool "Century bit support for rtc-ds1307"
+	depends on RTC_DRV_DS1307
+	default n
+	help
+	  The DS1307 driver suffered from a bug where it was enabling the
+	  century bit inconditionnally but never used it when reading the time.
+	  It made the driver unable to support dates beyond 2099.
+	  Setting this option will add proper support for the century bit but if
+	  the time was previously set using a kernel predating this option,
+	  reading the date will return a date in the next century.
+	  To solve that, you could boot a kernel without this option set, set
+	  the RTC date and then boot a kernel with this option set.
+
 config RTC_DRV_DS1374
 	tristate "Dallas/Maxim DS1374"
 	help
diff --git a/drivers/rtc/rtc-ds1307.c b/drivers/rtc/rtc-ds1307.c
index 8e1c5cb6ece6..4c5890864d9c 100644
--- a/drivers/rtc/rtc-ds1307.c
+++ b/drivers/rtc/rtc-ds1307.c
@@ -382,10 +382,25 @@ static int ds1307_get_time(struct device *dev, struct rtc_time *t)
 	t->tm_mday = bcd2bin(ds1307->regs[DS1307_REG_MDAY] & 0x3f);
 	tmp = ds1307->regs[DS1307_REG_MONTH] & 0x1f;
 	t->tm_mon = bcd2bin(tmp) - 1;
-
-	/* assume 20YY not 19YY, and ignore DS1337_BIT_CENTURY */
 	t->tm_year = bcd2bin(ds1307->regs[DS1307_REG_YEAR]) + 100;
 
+#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
+	switch (ds1307->type) {
+	case ds_1337:
+	case ds_1339:
+	case ds_3231:
+		if (ds1307->regs[DS1307_REG_MONTH] && DS1337_BIT_CENTURY)
+			t->tm_year += 100;
+		break;
+	case ds_1340:
+		if (ds1307->regs[DS1307_REG_HOUR] && DS1340_BIT_CENTURY)
+			t->tm_year += 100;
+		break;
+	default:
+		break;
+	}
+#endif
+
 	dev_dbg(dev, "%s secs=%d, mins=%d, "
 		"hours=%d, mday=%d, mon=%d, year=%d, wday=%d\n",
 		"read", t->tm_sec, t->tm_min,
@@ -409,6 +424,27 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
 		t->tm_hour, t->tm_mday,
 		t->tm_mon, t->tm_year, t->tm_wday);
 
+#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
+	if (t->tm_year < 100)
+		return -EINVAL;
+
+	switch (ds1307->type) {
+	case ds_1337:
+	case ds_1339:
+	case ds_3231:
+	case ds_1340:
+		if (t->tm_year > 299)
+			return -EINVAL;
+	default:
+		if (t->tm_year > 199)
+			return -EINVAL;
+		break;
+	}
+#else
+	if (t->tm_year < 100 || t->tm_year > 199)
+		return -EINVAL;
+#endif
+
 	buf[DS1307_REG_SECS] = bin2bcd(t->tm_sec);
 	buf[DS1307_REG_MIN] = bin2bcd(t->tm_min);
 	buf[DS1307_REG_HOUR] = bin2bcd(t->tm_hour);
@@ -424,11 +460,13 @@ static int ds1307_set_time(struct device *dev, struct rtc_time *t)
 	case ds_1337:
 	case ds_1339:
 	case ds_3231:
-		buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
+		if (t->tm_year > 199)
+			buf[DS1307_REG_MONTH] |= DS1337_BIT_CENTURY;
 		break;
 	case ds_1340:
-		buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN
-				| DS1340_BIT_CENTURY;
+		buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY_EN;
+		if (t->tm_year > 199)
+			buf[DS1307_REG_HOUR] |= DS1340_BIT_CENTURY;
 		break;
 	case mcp794xx:
 		/*
-- 
2.8.1

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] rtc: ds1307: fix century bit support
  2016-07-13  0:34 [PATCH] rtc: ds1307: fix century bit support Alexandre Belloni
@ 2016-07-14 19:54 ` Arnaud Ebalard
  2016-07-20 21:16   ` Alexandre Belloni
  0 siblings, 1 reply; 3+ messages in thread
From: Arnaud Ebalard @ 2016-07-14 19:54 UTC (permalink / raw)
  To: Alexandre Belloni; +Cc: Alessandro Zummo, rtc-linux, linux-kernel

Hi Alexandre,

Alexandre Belloni <alexandre.belloni@free-electrons.com> writes:

> Add an option to properly support the century bit of ds1337 and compatibles
> and ds1340.
> Because the driver had a bug until now, it is not possible to switch users
> to the fixed code directly as RTCs in the field will wrongly have the
> century bit set.
>
> Signed-off-by: Alexandre Belloni <alexandre.belloni@free-electrons.com>
> ---
>
> Arnaud, do you mind testing that patch, I still don't have the necessary
> hardware but I think this is the proper course of action.

I did some tests w/ all patches applied (the one below and the ones in
the other series). I had to fix two errors: one pointed in the patch
below and the other one in a patch in the other series (I'll reply to
the patch).

With both RTC_DRV_DS1307 and RTC_DRV_DS1307_CENTURY enabled, this works
as expected on my side: booting on a system which had only seen an
ISL12057 driver, the time on the system at boot (or via hwclock -r) is
correct. Playing w/ -s and -w options works as expected. Booting back on
a kernel w/ ISL12057 driver, things are also ok. So, AFAICT, your
changes do the work.

One small thing though: for user which have always used RTC_DRV_ISL12057
config knob, the change will not be transparent. Would it be possible to
somehow make that old kernel config knob some kind of alias for
RTC_DRV_DS1307 and RTC_DRV_DS1307_CENTURY in Kconfig?

Cheers,

a+


> +#ifdef CONFIG_RTC_DRV_DS1307_CENTURY
> +	switch (ds1307->type) {
> +	case ds_1337:
> +	case ds_1339:
> +	case ds_3231:
> +		if (ds1307->regs[DS1307_REG_MONTH] && DS1337_BIT_CENTURY)

                                                   ^^


> +			t->tm_year += 100;
> +		break;
> +	case ds_1340:
> +		if (ds1307->regs[DS1307_REG_HOUR] && DS1340_BIT_CENTURY)

                                                  ^^

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] rtc: ds1307: fix century bit support
  2016-07-14 19:54 ` Arnaud Ebalard
@ 2016-07-20 21:16   ` Alexandre Belloni
  0 siblings, 0 replies; 3+ messages in thread
From: Alexandre Belloni @ 2016-07-20 21:16 UTC (permalink / raw)
  To: Arnaud Ebalard; +Cc: Alessandro Zummo, rtc-linux, linux-kernel

On 14/07/2016 at 21:54:45 +0200, Arnaud Ebalard wrote :
> One small thing though: for user which have always used RTC_DRV_ISL12057
> config knob, the change will not be transparent. Would it be possible to
> somehow make that old kernel config knob some kind of alias for
> RTC_DRV_DS1307 and RTC_DRV_DS1307_CENTURY in Kconfig?
> 

Well, I tried multiple things but making RTC_DRV_ISL12057 hidden makes it
unable to select both RTC_DRV_DS1307 and RTC_DRV_DS1307_CENTURY. I'll
try to find a solution but I pretty much prefer not having
RTC_DRV_ISL12057 selectable anymore

-- 
Alexandre Belloni, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-07-20 21:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-13  0:34 [PATCH] rtc: ds1307: fix century bit support Alexandre Belloni
2016-07-14 19:54 ` Arnaud Ebalard
2016-07-20 21:16   ` Alexandre Belloni

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).