u-boot/drivers/rtc/sandbox_rtc.c
Rasmus Villemoes 9fb6a41cda rtc: sandbox-rtc: fix set method
The current set method is broken; a simple test case is to first set
the date to something in April, then change the date to 31st May:

=> date 040412122020.34
Date: 2020-04-04 (Saturday)    Time: 12:12:34
=> date 053112122020.34
Date: 2020-05-01 (Friday)    Time: 12:12:34

or via the amending of the existing rtc_set_get test case similarly:

$ ./u-boot -T -v
=> ut dm rtc_set_get
Test: dm_test_rtc_set_get: rtc.c
expected: 31/08/2004 18:18:00
actual: 01/08/2004 18:18:00

The problem is that after each register write,
sandbox_i2c_rtc_complete_write() gets called and sets the internal
time from the current set of registers. However, when we get to
writing 31 to mday, the registers are in an inconsistent state (mon is
still 4), so the mktime machinery ends up translating April 31st to
May 1st. Upon the next register write, the registers are populated by
sandbox_i2c_rtc_prepare_read(), so the 31 we just wrote to mday gets
overwritten by a 1.

Fix it by writing all registers at once, and for consistency, update
the get method to retrieve them all with one "i2c transfer".

Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heiko Schocher <hs@denx.de>
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
2020-07-09 06:02:45 +02:00

88 lines
1.9 KiB
C

// SPDX-License-Identifier: GPL-2.0+
/*
* (C) Copyright 2015 Google, Inc
* Written by Simon Glass <sjg@chromium.org>
*/
#include <common.h>
#include <dm.h>
#include <i2c.h>
#include <rtc.h>
#include <asm/rtc.h>
#define REG_COUNT 0x80
static int sandbox_rtc_get(struct udevice *dev, struct rtc_time *time)
{
u8 buf[7];
int ret;
ret = dm_i2c_read(dev, REG_SEC, buf, sizeof(buf));
if (ret < 0)
return ret;
time->tm_sec = buf[REG_SEC - REG_SEC];
time->tm_min = buf[REG_MIN - REG_SEC];
time->tm_hour = buf[REG_HOUR - REG_SEC];
time->tm_mday = buf[REG_MDAY - REG_SEC];
time->tm_mon = buf[REG_MON - REG_SEC];
time->tm_year = buf[REG_YEAR - REG_SEC] + 1900;
time->tm_wday = buf[REG_WDAY - REG_SEC];
return 0;
}
static int sandbox_rtc_set(struct udevice *dev, const struct rtc_time *time)
{
u8 buf[7];
int ret;
buf[REG_SEC - REG_SEC] = time->tm_sec;
buf[REG_MIN - REG_SEC] = time->tm_min;
buf[REG_HOUR - REG_SEC] = time->tm_hour;
buf[REG_MDAY - REG_SEC] = time->tm_mday;
buf[REG_MON - REG_SEC] = time->tm_mon;
buf[REG_YEAR - REG_SEC] = time->tm_year - 1900;
buf[REG_WDAY - REG_SEC] = time->tm_wday;
ret = dm_i2c_write(dev, REG_SEC, buf, sizeof(buf));
if (ret < 0)
return ret;
return 0;
}
static int sandbox_rtc_reset(struct udevice *dev)
{
return dm_i2c_reg_write(dev, REG_RESET, 0);
}
static int sandbox_rtc_read8(struct udevice *dev, unsigned int reg)
{
return dm_i2c_reg_read(dev, reg);
}
static int sandbox_rtc_write8(struct udevice *dev, unsigned int reg, int val)
{
return dm_i2c_reg_write(dev, reg, val);
}
static const struct rtc_ops sandbox_rtc_ops = {
.get = sandbox_rtc_get,
.set = sandbox_rtc_set,
.reset = sandbox_rtc_reset,
.read8 = sandbox_rtc_read8,
.write8 = sandbox_rtc_write8,
};
static const struct udevice_id sandbox_rtc_ids[] = {
{ .compatible = "sandbox-rtc" },
{ }
};
U_BOOT_DRIVER(rtc_sandbox) = {
.name = "rtc-sandbox",
.id = UCLASS_RTC,
.of_match = sandbox_rtc_ids,
.ops = &sandbox_rtc_ops,
};