+// SPDX-License-Identifier: GPL-2.0+
/*
* (C) Copyright 2008
* Gururaja Hebbar gururajakr@sanyo.co.in
*
* reference linux-2.6.20.6/drivers/rtc/rtc-pl031.c
- *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
*/
#include <common.h>
#include <command.h>
+#include <dm.h>
+#include <errno.h>
#include <rtc.h>
-
-#if defined(CONFIG_CMD_DATE)
-
-#ifndef CONFIG_SYS_RTC_PL031_BASE
-#error CONFIG_SYS_RTC_PL031_BASE is not defined!
-#endif
+#include <asm/io.h>
+#include <asm/types.h>
/*
* Register definitions
#define RTC_CR_START (1 << 0)
-#define RTC_WRITE_REG(addr, val) \
- (*(volatile unsigned int *)(CONFIG_SYS_RTC_PL031_BASE + (addr)) = (val))
-#define RTC_READ_REG(addr) \
- (*(volatile unsigned int *)(CONFIG_SYS_RTC_PL031_BASE + (addr)))
+struct pl031_platdata {
+ phys_addr_t base;
+};
+
+static inline u32 pl031_read_reg(struct udevice *dev, int reg)
+{
+ struct pl031_platdata *pdata = dev_get_platdata(dev);
-static int pl031_initted = 0;
+ return readl(pdata->base + reg);
+}
-/* Enable RTC Start in Control register*/
-void rtc_init(void)
+static inline u32 pl031_write_reg(struct udevice *dev, int reg, u32 value)
{
- RTC_WRITE_REG(RTC_CR, RTC_CR_START);
+ struct pl031_platdata *pdata = dev_get_platdata(dev);
- pl031_initted = 1;
+ return writel(value, pdata->base + reg);
}
/*
- * Reset the RTC. We set the date back to 1970-01-01.
+ * Probe RTC device
+ */
+static int pl031_probe(struct udevice *dev)
+{
+ /* Enable RTC Start in Control register*/
+ pl031_write_reg(dev, RTC_CR, RTC_CR_START);
+
+ return 0;
+}
+
+/*
+ * Get the current time from the RTC
*/
-void rtc_reset(void)
+static int pl031_get(struct udevice *dev, struct rtc_time *tm)
{
- RTC_WRITE_REG(RTC_LR, 0x00);
- if(!pl031_initted)
- rtc_init();
+ unsigned long tim;
+
+ if (!tm)
+ return -EINVAL;
+
+ tim = pl031_read_reg(dev, RTC_DR);
+
+ rtc_to_tm(tim, tm);
+
+ debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
+ tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
+ tm->tm_hour, tm->tm_min, tm->tm_sec);
+
+ return 0;
}
/*
* Set the RTC
-*/
-int rtc_set(struct rtc_time *tmp)
+ */
+static int pl031_set(struct udevice *dev, const struct rtc_time *tm)
{
unsigned long tim;
- if(!pl031_initted)
- rtc_init();
+ if (!tm)
+ return -EINVAL;
- if (tmp == NULL) {
- puts("Error setting the date/time\n");
- return -1;
- }
+ debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
+ tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_wday,
+ tm->tm_hour, tm->tm_min, tm->tm_sec);
/* Calculate number of seconds this incoming time represents */
- tim = mktime(tmp->tm_year, tmp->tm_mon, tmp->tm_mday,
- tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
+ tim = rtc_mktime(tm);
- RTC_WRITE_REG(RTC_LR, tim);
+ pl031_write_reg(dev, RTC_LR, tim);
- return -1;
+ return 0;
}
/*
- * Get the current time from the RTC
+ * Reset the RTC. We set the date back to 1970-01-01.
*/
-int rtc_get(struct rtc_time *tmp)
+static int pl031_reset(struct udevice *dev)
{
- ulong tim;
+ pl031_write_reg(dev, RTC_LR, 0);
- if(!pl031_initted)
- rtc_init();
+ return 0;
+}
- if (tmp == NULL) {
- puts("Error getting the date/time\n");
- return -1;
- }
+static const struct rtc_ops pl031_ops = {
+ .get = pl031_get,
+ .set = pl031_set,
+ .reset = pl031_reset,
+};
- tim = RTC_READ_REG(RTC_DR);
+static const struct udevice_id pl031_ids[] = {
+ { .compatible = "arm,pl031" },
+ { }
+};
- to_tm (tim, tmp);
+static int pl031_ofdata_to_platdata(struct udevice *dev)
+{
+ struct pl031_platdata *pdata = dev_get_platdata(dev);
- debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
- tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
- tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
+ pdata->base = dev_read_addr(dev);
return 0;
}
-#endif
+U_BOOT_DRIVER(rtc_pl031) = {
+ .name = "rtc-pl031",
+ .id = UCLASS_RTC,
+ .of_match = pl031_ids,
+ .probe = pl031_probe,
+ .ofdata_to_platdata = pl031_ofdata_to_platdata,
+ .platdata_auto_alloc_size = sizeof(struct pl031_platdata),
+ .ops = &pl031_ops,
+};