2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2015-04-20 18:37:23 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2015 Google, Inc
|
|
|
|
* Written by Simon Glass <sjg@chromium.org>
|
|
|
|
*/
|
|
|
|
|
2021-04-27 09:02:19 +00:00
|
|
|
#define LOG_CATEGORY UCLASS_RTC
|
|
|
|
|
2015-04-20 18:37:23 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <errno.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2015-04-20 18:37:23 +00:00
|
|
|
#include <rtc.h>
|
|
|
|
|
|
|
|
int dm_rtc_get(struct udevice *dev, struct rtc_time *time)
|
|
|
|
{
|
|
|
|
struct rtc_ops *ops = rtc_get_ops(dev);
|
|
|
|
|
|
|
|
assert(ops);
|
|
|
|
if (!ops->get)
|
|
|
|
return -ENOSYS;
|
|
|
|
return ops->get(dev, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
int dm_rtc_set(struct udevice *dev, struct rtc_time *time)
|
|
|
|
{
|
|
|
|
struct rtc_ops *ops = rtc_get_ops(dev);
|
|
|
|
|
|
|
|
assert(ops);
|
|
|
|
if (!ops->set)
|
|
|
|
return -ENOSYS;
|
|
|
|
return ops->set(dev, time);
|
|
|
|
}
|
|
|
|
|
|
|
|
int dm_rtc_reset(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct rtc_ops *ops = rtc_get_ops(dev);
|
|
|
|
|
|
|
|
assert(ops);
|
|
|
|
if (!ops->reset)
|
|
|
|
return -ENOSYS;
|
|
|
|
return ops->reset(dev);
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:01:10 +00:00
|
|
|
int dm_rtc_read(struct udevice *dev, unsigned int reg, u8 *buf, unsigned int len)
|
|
|
|
{
|
|
|
|
struct rtc_ops *ops = rtc_get_ops(dev);
|
|
|
|
|
|
|
|
assert(ops);
|
|
|
|
if (ops->read)
|
|
|
|
return ops->read(dev, reg, buf, len);
|
|
|
|
if (!ops->read8)
|
|
|
|
return -ENOSYS;
|
|
|
|
while (len--) {
|
|
|
|
int ret = ops->read8(dev, reg++);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
*buf++ = ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:01:11 +00:00
|
|
|
int dm_rtc_write(struct udevice *dev, unsigned int reg,
|
|
|
|
const u8 *buf, unsigned int len)
|
|
|
|
{
|
|
|
|
struct rtc_ops *ops = rtc_get_ops(dev);
|
|
|
|
|
|
|
|
assert(ops);
|
|
|
|
if (ops->write)
|
|
|
|
return ops->write(dev, reg, buf, len);
|
|
|
|
if (!ops->write8)
|
|
|
|
return -ENOSYS;
|
|
|
|
while (len--) {
|
|
|
|
int ret = ops->write8(dev, reg++, *buf++);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-20 18:37:23 +00:00
|
|
|
int rtc_read8(struct udevice *dev, unsigned int reg)
|
|
|
|
{
|
|
|
|
struct rtc_ops *ops = rtc_get_ops(dev);
|
|
|
|
|
|
|
|
assert(ops);
|
2020-07-06 20:01:12 +00:00
|
|
|
if (ops->read8)
|
|
|
|
return ops->read8(dev, reg);
|
|
|
|
if (ops->read) {
|
|
|
|
u8 buf[1];
|
|
|
|
int ret = ops->read(dev, reg, buf, 1);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
return buf[0];
|
|
|
|
}
|
|
|
|
return -ENOSYS;
|
2015-04-20 18:37:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int rtc_write8(struct udevice *dev, unsigned int reg, int val)
|
|
|
|
{
|
|
|
|
struct rtc_ops *ops = rtc_get_ops(dev);
|
|
|
|
|
|
|
|
assert(ops);
|
2020-07-06 20:01:12 +00:00
|
|
|
if (ops->write8)
|
|
|
|
return ops->write8(dev, reg, val);
|
|
|
|
if (ops->write) {
|
|
|
|
u8 buf[1] = { val };
|
|
|
|
|
|
|
|
return ops->write(dev, reg, buf, 1);
|
|
|
|
}
|
|
|
|
return -ENOSYS;
|
2015-04-20 18:37:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-16 14:26:27 +00:00
|
|
|
int rtc_read16(struct udevice *dev, unsigned int reg, u16 *valuep)
|
|
|
|
{
|
|
|
|
u16 value = 0;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(value); i++) {
|
|
|
|
ret = rtc_read8(dev, reg + i);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
value |= ret << (i << 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
*valuep = value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rtc_write16(struct udevice *dev, unsigned int reg, u16 value)
|
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(value); i++) {
|
|
|
|
ret = rtc_write8(dev, reg + i, (value >> (i << 3)) & 0xff);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-20 18:37:23 +00:00
|
|
|
int rtc_read32(struct udevice *dev, unsigned int reg, u32 *valuep)
|
|
|
|
{
|
|
|
|
u32 value = 0;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(value); i++) {
|
|
|
|
ret = rtc_read8(dev, reg + i);
|
2015-10-18 21:55:31 +00:00
|
|
|
if (ret < 0)
|
2015-04-20 18:37:23 +00:00
|
|
|
return ret;
|
|
|
|
value |= ret << (i << 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
*valuep = value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rtc_write32(struct udevice *dev, unsigned int reg, u32 value)
|
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof(value); i++) {
|
|
|
|
ret = rtc_write8(dev, reg + i, (value >> (i << 3)) & 0xff);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
UCLASS_DRIVER(rtc) = {
|
|
|
|
.name = "rtc",
|
|
|
|
.id = UCLASS_RTC,
|
2021-07-29 10:57:12 +00:00
|
|
|
.flags = DM_UC_FLAG_SEQ_ALIAS,
|
2021-08-07 13:24:04 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_REAL)
|
2018-11-18 15:14:35 +00:00
|
|
|
.post_bind = dm_scan_fdt_dev,
|
2020-10-03 17:31:34 +00:00
|
|
|
#endif
|
2015-04-20 18:37:23 +00:00
|
|
|
};
|