Skip to contentSkip to conversation

rtc: Add ASPEED RTC

PATCH Superseded Joel Stanley 2 participants 5 messages Oct 3, 2018 – Oct 4, 2018

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Joel Stanley

author maintainer
#0 lore raw

cover rtc: Add ASPEED RTC

This adds support for the ASPEED RTC hardware.

Joel Stanley (2):
rtc: Add ASPEED RTC driver
dt-bindings: rtc: Add ASPEED description

 .../devicetree/bindings/rtc/rtc-aspeed.txt    |  18 +++
 drivers/rtc/Kconfig                           |  10 ++
 drivers/rtc/Makefile                          |   1 +
 drivers/rtc/rtc-aspeed.c                      | 142 ++++++++++++++++++
 4 files changed, 171 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/rtc-aspeed.txt
 create mode 100644 drivers/rtc/rtc-aspeed.c
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH 1/2 rtc: Add ASPEED RTC driver

Read and writes the time to the non-battery backed RTC in the ASPEED AST2400 and AST2500 system on chip.

---
 drivers/rtc/Kconfig      |  10 +++
 drivers/rtc/Makefile     |   1 +
 drivers/rtc/rtc-aspeed.c | 142 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 153 insertions(+)
 create mode 100644 drivers/rtc/rtc-aspeed.c
drivers/rtc/Kconfig +10 -0
index a819ef07b7ec..372944b59b69 100644
@@ -1783,6 +1783,16 @@ config RTC_DRV_RTD119X
 	  If you say yes here, you get support for the RTD1295 SoC
 	  Real Time Clock.
 
+config RTC_DRV_ASPEED
+	tristate "Aspeed RTC"
+	depends on ARCH_ASPEED || COMPILE_TEST
+	help
+	  If you say yes here you get support for the ASPEED AST2400 and
+	  AST2500 SoC real time clocks.
+
+	  This driver can also be built as a module, if so, the module
+	  will be called "rtc-aspeed".
+
 comment "HID Sensor RTC drivers"
 
 config RTC_DRV_HID_SENSOR_TIME
drivers/rtc/Makefile +1 -0
index 290c1730fb0a..7d924577d133 100644
@@ -33,6 +33,7 @@ obj-$(CONFIG_RTC_DRV_AC100)	+= rtc-ac100.o
 obj-$(CONFIG_RTC_DRV_ARMADA38X)	+= rtc-armada38x.o
 obj-$(CONFIG_RTC_DRV_AS3722)	+= rtc-as3722.o
 obj-$(CONFIG_RTC_DRV_ASM9260)	+= rtc-asm9260.o
+obj-$(CONFIG_RTC_DRV_ASPEED)	+= rtc-aspeed.o
 obj-$(CONFIG_RTC_DRV_AT91RM9200)+= rtc-at91rm9200.o
 obj-$(CONFIG_RTC_DRV_AT91SAM9)	+= rtc-at91sam9.o
 obj-$(CONFIG_RTC_DRV_AU1XXX)	+= rtc-au1xxx.o
drivers/rtc/rtc-aspeed.c new file +142 -0

142 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw

PATCH 1/2 rtc: Add ASPEED RTC driver

Hello,

Quoted 19 lines · Joel Stanley
drivers/rtc/rtc-aspeed.c new file +19 -0
+static int aspeed_rtc_read_time(struct device *dev, struct rtc_time *tm)
+{
+	struct aspeed_rtc *rtc = dev_get_drvdata(dev);
+	unsigned int cent, year, mon, day, hour, min, sec;
+	unsigned long flags;
+	u32 reg1, reg2;
+
+	spin_lock_irqsave(&rtc->lock, flags);
+
+	do {
+		reg2 = readl(rtc->base + RTC_YEAR);
+		reg1 = readl(rtc->base + RTC_TIME);
+	} while (reg2 != readl(rtc->base + RTC_YEAR));
+
+	day  = (reg1 >> 24) & 0x1f;
+	hour = (reg1 >> 16) & 0x1f;
+	min  = (reg1 >>  8) & 0x3f;
+	sec  = (reg1 >>  0) & 0x3f;
+	cent = (reg2 >> 16) & 0x1f;

drivers/rtc/rtc-aspeed.c line 51

Quoted 10 lines
drivers/rtc/rtc-aspeed.c new file +10 -0
+	year = (reg2 >>  8) & 0x7f;
+	/*
+	 * Month is 1-12 in hardware, and 0-11 in struct rtc_time, however we
+	 * are using mktime64 which is 1-12, so no adjustment is necessary
+	 */
+	mon  = (reg2 >>  0) & 0x0f;
+
+	rtc_time64_to_tm(mktime64(cent * 100 + year, mon, day, hour, min, sec),
+			tm);
+

This is quite wasteful. You already have the broken out time. Why don't you directly fill the tm struct?

Quoted 9 lines
drivers/rtc/rtc-aspeed.c new file +9 -0
+static int aspeed_rtc_probe(struct platform_device *pdev)
+{
+	struct resource *res;
+	struct aspeed_rtc *rtc;
+
+	rtc = devm_kzalloc(&pdev->dev, sizeof(*rtc), GFP_KERNEL);
+	if (!rtc)
+		return -ENOMEM;
+

drivers/rtc/rtc-aspeed.c line 112

Quoted 10 lines
drivers/rtc/rtc-aspeed.c new file +10 -0
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	rtc->base = devm_ioremap_resource(&pdev->dev, res);
+	if (IS_ERR(rtc->base))
+		return PTR_ERR(rtc->base);
+
+	platform_set_drvdata(pdev, rtc);
+
+	rtc->rtc_dev = devm_rtc_device_register(&pdev->dev, pdev->name,
+						&aspeed_rtc_ops, THIS_MODULE);
+

Please use devm_rtc_allocate_device to allocate the rtc and then register it with rtc_register_device. Please also fill rtc->range_{min,max} before the registration.

drivers/rtc/rtc-aspeed.c line 120

Quoted 8 lines
drivers/rtc/rtc-aspeed.c new file +8 -0
+	if (IS_ERR(rtc->rtc_dev))
+		return PTR_ERR(rtc->rtc_dev);
+
+	spin_lock_init(&rtc->lock);
+
+	/* Enable RTC and clear the unlock bit */
+	writel(RTC_ENABLE, rtc->base + RTC_CTRL);
+

Maybe this should only be done in set_time so you can know whether the time that is read in read_time has a chance to be valid.

For example you could return -EINVAL when RTC_ENABLE is not set if this bit is readable.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author maintainer
#3 lore raw

PATCH 2/2 dt-bindings: rtc: Add ASPEED description

In reply to Joel Stanley

Describe the RTC as used in the ASPEED ast2400 and ast2500 SoCs.

---
 .../devicetree/bindings/rtc/rtc-aspeed.txt     | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/rtc-aspeed.txt
Documentation/devicetree/bindings/rtc/rtc-aspeed.txt new file +18 -0
new file mode 100644
index 000000000000..d31a4d24c75f
@@ -0,0 +1,18 @@
+ASPEED BMC RTC
+==============
+
+Required properties:
+ - compatible: should be one of the following
+   * aspeed,ast2400-rtc for the ast2400
+   * aspeed,ast2500-rtc for the ast2500
+
+ - reg: physical base address of the controller and length of memory mapped
+   region
+
+Example:
+
+   rtc at 1e781000 {
+           compatible = "aspeed,ast2400-rtc";
+           reg = <0x1e781000 0x18>;
+           status = "disabled";
+   };
Signature · 1 line
2.17.1
#4 lore raw

PATCH 2/2 dt-bindings: rtc: Add ASPEED description

I think this patch should come first.

Quoted 35 lines · Joel Stanley

Describe the RTC as used in the ASPEED ast2400 and ast2500 SoCs.

---
 .../devicetree/bindings/rtc/rtc-aspeed.txt     | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/rtc-aspeed.txt
Documentation/devicetree/bindings/rtc/rtc-aspeed.txt new file +18 -0
new file mode 100644
index 000000000000..d31a4d24c75f
@@ -0,0 +1,18 @@
+ASPEED BMC RTC
+==============
+
+Required properties:
+ - compatible: should be one of the following
+   * aspeed,ast2400-rtc for the ast2400
+   * aspeed,ast2500-rtc for the ast2500
+
+ - reg: physical base address of the controller and length of memory mapped
+   region
+
+Example:
+
+   rtc at 1e781000 {
+           compatible = "aspeed,ast2400-rtc";
+           reg = <0x1e781000 0x18>;
+           status = "disabled";
+   };
Signature · 1 line
2.17.1
Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com