[U-Boot] [PATCH 1/1] ARM: qemu-arm: enable RTC

Takahiro AKASHI takahiro.akashi at linaro.org
Mon Jul 2 23:51:32 UTC 2018


On Mon, Jul 02, 2018 at 07:07:55PM +0300, Tuomas Tynkkynen wrote:
> Hi Heinrich,
> 
> On 06/29/2018 01:34 AM, Heinrich Schuchardt wrote:
> >QEMU provides an emulated ARM AMBA PrimeCell PL031 RTC.
> >
> >The patch sets the base address in the board include file according to the
> >definition in hw/arm/virt.c of the QEMU source. It defines the Kconfig
> >option for the existing driver, and enables the RTC driver in
> >qemu_arm64_defconfig and qemu_arm_defconfig as well as the date command.
> >
> >We need an RTC to provide the GetTime() runtime service in the UEFI
> >subsystem.
> >
> >Signed-off-by: Heinrich Schuchardt <xypron.glpk at gmx.de>
> >---
> >  configs/qemu_arm64_defconfig | 2 ++
> >  configs/qemu_arm_defconfig   | 2 ++
> >  drivers/rtc/Kconfig          | 7 +++++++
> >  include/configs/qemu-arm.h   | 3 +++
> >  4 files changed, 14 insertions(+)
> >
> 
> Reviewed-by: Tuomas Tynkkynen <tuomas.tynkkynen at iki.fi>
> Tested-by: Tuomas Tynkkynen <tuomas.tynkkynen at iki.fi>
> 
> - Tuomas

Well, it is a good time. Why not change the driver to driver model
like below:
 * I intentionally leave CONFIG_DM_RTC not mandatory here
 * The patch may be split into two parts; one for rtc, the other for qemu-arm

---8<---
>From c2e701dfb8ca48025e8266035cd455287178f85b Mon Sep 17 00:00:00 2001
From: AKASHI Takahiro <takahiro.akashi at linaro.org>
Date: Tue, 3 Jul 2018 08:32:16 +0900
Subject: [PATCH] rtc: pl031: convert the driver to driver model

Signed-off-by: AKASHI Takahiro <takahiro.akashi at linaro.org>
---
 board/emulation/qemu-arm/qemu-arm.c  | 13 ++++
 drivers/rtc/Kconfig                  |  7 +++
 drivers/rtc/pl031.c                  | 91 +++++++++++++++++++++++++---
 include/configs/qemu-arm.h           |  4 ++
 include/dm/platform_data/rtc_pl031.h | 10 +++
 5 files changed, 118 insertions(+), 7 deletions(-)
 create mode 100644 include/dm/platform_data/rtc_pl031.h

diff --git a/board/emulation/qemu-arm/qemu-arm.c b/board/emulation/qemu-arm/qemu-arm.c
index 085cbbef99..3084f2aa71 100644
--- a/board/emulation/qemu-arm/qemu-arm.c
+++ b/board/emulation/qemu-arm/qemu-arm.c
@@ -3,8 +3,21 @@
  * Copyright (c) 2017 Tuomas Tynkkynen
  */
 #include <common.h>
+#include <dm.h>
+#include <dm/platform_data/rtc_pl031.h>
 #include <fdtdec.h>
 
+#ifdef CONFIG_DM_RTC
+static const struct pl031_rtc_platdata pl031_pdata = {
+	.base = SYS_RTC_BASE,
+};
+
+U_BOOT_DEVICE(qemu_arm_rtc) = {
+	.name = "rtc-pl031",
+	.platdata = &pl031_pdata,
+};
+#endif
+
 #ifdef CONFIG_ARM64
 #include <asm/armv8/mmu.h>
 
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index a3f8c8aecc..50d9236601 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -55,6 +55,13 @@ config RTC_MV
 	  Enable Marvell RTC driver. This driver supports the rtc that is present
 	  on some Marvell SoCs.
 
+config RTC_PL031
+	bool "Enable ARM PL031 RTC driver"
+	imply DM_RTC
+	imply CMD_DATE
+	help
+	  Enable ARM PL031 RTC driver.
+
 config RTC_S35392A
 	bool "Enable S35392A driver"
 	select BITREVERSE
diff --git a/drivers/rtc/pl031.c b/drivers/rtc/pl031.c
index 8955805e3b..884e3a13fe 100644
--- a/drivers/rtc/pl031.c
+++ b/drivers/rtc/pl031.c
@@ -8,12 +8,25 @@
 
 #include <common.h>
 #include <command.h>
+#include <dm.h>
+#include <dm/platform_data/rtc_pl031.h>
 #include <rtc.h>
 
 #if defined(CONFIG_CMD_DATE)
 
-#ifndef CONFIG_SYS_RTC_PL031_BASE
-#error CONFIG_SYS_RTC_PL031_BASE is not defined!
+#define	__RTC_WRITE_REG(base, addr, val) \
+			(*(volatile unsigned int *)((base) + (addr)) = (val))
+#define	__RTC_READ_REG(base, addr)	\
+			(*(volatile unsigned int *)((base) + (addr)))
+
+#ifdef CONFIG_DM_RTC
+phys_addr_t pl031_base;
+#else
+# ifndef CONFIG_SYS_RTC_PL031_BASE
+# error CONFIG_SYS_RTC_PL031_BASE is not defined!
+# endif
+
+phys_addr_t pl031_base = CONFIG_SYS_RTC_PL031_BASE;
 #endif
 
 /*
@@ -30,10 +43,8 @@
 
 #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)))
+#define	RTC_WRITE_REG(addr, val)	__RTC_WRITE_REG(pl031_base, addr, val)
+#define	RTC_READ_REG(addr)		__RTC_READ_REG(pl031_base, addr)
 
 static int pl031_initted = 0;
 
@@ -104,4 +115,70 @@ int rtc_get(struct rtc_time *tmp)
 	return 0;
 }
 
-#endif
+#ifdef CONFIG_DM_RTC
+void pl031_rtc_init(struct pl031_rtc_platdata *pdata)
+{
+	pl031_base = pdata->base;
+}
+
+static int pl031_rtc_get(struct udevice *dev, struct rtc_time *tm)
+{
+	struct pl031_rtc_platdata *pdata = dev_get_platdata(dev);
+
+	if (!pl031_initted)
+		pl031_rtc_init(pdata);
+
+	return rtc_get(tm);
+}
+
+static int pl031_rtc_set(struct udevice *dev, const struct rtc_time *tm)
+{
+	struct pl031_rtc_platdata *pdata = dev_get_platdata(dev);
+
+	if (!pl031_initted)
+		pl031_rtc_init(pdata);
+
+	return rtc_set(tm);
+}
+
+static int pl031_rtc_reset(struct udevice *dev)
+{
+	struct pl031_rtc_platdata *pdata = dev_get_platdata(dev);
+
+	if (!pl031_initted)
+		pl031_rtc_init(pdata);
+
+	rtc_reset();
+
+	return 0;
+}
+
+static const struct rtc_ops pl031_rtc_ops = {
+	.get = pl031_rtc_get,
+	.set = pl031_rtc_set,
+	.reset = pl031_rtc_reset,
+};
+
+static const struct udevice_id pl031_rtc_ids[] = {
+	{ .compatible = "arm,pl031" },
+	{ }
+};
+
+static int pl031_rtc_ofdata_to_platdata(struct udevice *dev)
+{
+	struct pl031_rtc_platdata *pdata = dev_get_platdata(dev);
+
+	pdata->base = devfdt_get_addr(dev);
+	return 0;
+}
+
+U_BOOT_DRIVER(rtc_pl031) = {
+	.name	= "rtc-pl031",
+	.id	= UCLASS_RTC,
+	.ofdata_to_platdata = pl031_rtc_ofdata_to_platdata,
+	.of_match = pl031_rtc_ids,
+	.ops	= &pl031_rtc_ops,
+};
+#endif /* CONFIG_DM_RTC */
+
+#endif /* CONFIG_CMD_DATE */
diff --git a/include/configs/qemu-arm.h b/include/configs/qemu-arm.h
index b7debb9cc7..569fa729a9 100644
--- a/include/configs/qemu-arm.h
+++ b/include/configs/qemu-arm.h
@@ -36,6 +36,10 @@
 #define CONFIG_SYS_MONITOR_BASE		CONFIG_SYS_FLASH_BASE0
 #define CONFIG_SYS_FLASH_BASE		CONFIG_SYS_FLASH_BASE0
 
+#define SYS_PERI_BASE			0x9000000
+#define SYS_UART_BASE			SYS_PERI_BASE
+#define SYS_RTC_BASE			(SYS_PERI_BASE + 0x10000)
+
 /* PCI */
 /*
  * #define CONFIG_SYS_PCI_64BIT		1
diff --git a/include/dm/platform_data/rtc_pl031.h b/include/dm/platform_data/rtc_pl031.h
new file mode 100644
index 0000000000..b35642b15d
--- /dev/null
+++ b/include/dm/platform_data/rtc_pl031.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+#ifndef __rtc_pl031_h
+#define __rtc_pl031_h
+
+struct pl031_rtc_platdata {
+	phys_addr_t base;
+};
+
+#endif
-- 
2.17.0



More information about the U-Boot mailing list