2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-04-20 18:37:25 +00:00
|
|
|
/*
|
|
|
|
* (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>
|
2020-07-07 19:11:48 +00:00
|
|
|
#include <dm/acpi.h>
|
2015-04-20 18:37:25 +00:00
|
|
|
|
|
|
|
#define REG_COUNT 0x80
|
|
|
|
|
|
|
|
static int sandbox_rtc_get(struct udevice *dev, struct rtc_time *time)
|
|
|
|
{
|
2020-07-06 20:01:16 +00:00
|
|
|
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];
|
2015-04-20 18:37:25 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int sandbox_rtc_set(struct udevice *dev, const struct rtc_time *time)
|
|
|
|
{
|
2020-07-06 20:01:16 +00:00
|
|
|
u8 buf[7];
|
2015-04-20 18:37:25 +00:00
|
|
|
int ret;
|
|
|
|
|
2020-07-06 20:01:16 +00:00
|
|
|
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));
|
2015-04-20 18:37:25 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-07-07 19:11:48 +00:00
|
|
|
#if CONFIG_IS_ENABLED(ACPIGEN)
|
|
|
|
static int sandbox_rtc_get_name(const struct udevice *dev, char *out_name)
|
|
|
|
{
|
|
|
|
return acpi_copy_name(out_name, "RTCC");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct acpi_ops sandbox_rtc_acpi_ops = {
|
|
|
|
.get_name = sandbox_rtc_get_name,
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2015-04-20 18:37:25 +00:00
|
|
|
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" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2020-10-03 17:31:36 +00:00
|
|
|
U_BOOT_DRIVER(sandbox_rtc) = {
|
|
|
|
.name = "sandbox_rtc",
|
2015-04-20 18:37:25 +00:00
|
|
|
.id = UCLASS_RTC,
|
|
|
|
.of_match = sandbox_rtc_ids,
|
|
|
|
.ops = &sandbox_rtc_ops,
|
2020-07-07 19:11:48 +00:00
|
|
|
ACPI_OPS_PTR(&sandbox_rtc_acpi_ops)
|
2015-04-20 18:37:25 +00:00
|
|
|
};
|