linux-rtc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] rtc: Add ASPEED RTC
@ 2018-10-03 13:31 Joel Stanley
  2018-10-03 13:31 ` [PATCH 1/2] rtc: Add ASPEED RTC driver Joel Stanley
  2018-10-03 13:31 ` [PATCH 2/2] dt-bindings: rtc: Add ASPEED description Joel Stanley
  0 siblings, 2 replies; 5+ messages in thread
From: Joel Stanley @ 2018-10-03 13:31 UTC (permalink / raw)
  To: Alessandro Zummo, Alexandre Belloni, linux-rtc
  Cc: Andrew Jeffery, Christian Svensson, linux-arm-kernel, linux-aspeed

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

-- 
2.17.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] rtc: Add ASPEED RTC driver
  2018-10-03 13:31 [PATCH 0/2] rtc: Add ASPEED RTC Joel Stanley
@ 2018-10-03 13:31 ` Joel Stanley
  2018-10-04 18:37   ` Alexandre Belloni
  2018-10-03 13:31 ` [PATCH 2/2] dt-bindings: rtc: Add ASPEED description Joel Stanley
  1 sibling, 1 reply; 5+ messages in thread
From: Joel Stanley @ 2018-10-03 13:31 UTC (permalink / raw)
  To: Alessandro Zummo, Alexandre Belloni, linux-rtc
  Cc: Andrew Jeffery, Christian Svensson, linux-arm-kernel, linux-aspeed

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

Signed-off-by: Joel Stanley <joel@jms.id.au>
---
 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

diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index a819ef07b7ec..372944b59b69 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -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
diff --git a/drivers/rtc/Makefile b/drivers/rtc/Makefile
index 290c1730fb0a..7d924577d133 100644
--- a/drivers/rtc/Makefile
+++ b/drivers/rtc/Makefile
@@ -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
diff --git a/drivers/rtc/rtc-aspeed.c b/drivers/rtc/rtc-aspeed.c
new file mode 100644
index 000000000000..dce00e594910
--- /dev/null
+++ b/drivers/rtc/rtc-aspeed.c
@@ -0,0 +1,142 @@
+// SPDX-License-Identifier: GPL-2.0+
+// Copyright 2015 IBM Corp.
+
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/rtc.h>
+#include <linux/io.h>
+
+struct aspeed_rtc {
+	struct rtc_device	*rtc_dev;
+	void __iomem		*base;
+	spinlock_t		lock;
+};
+
+#define RTC_TIME	0x00
+#define RTC_YEAR	0x04
+#define RTC_CTRL	0x10
+
+#define RTC_UNLOCK	0x02
+#define RTC_ENABLE	0x01
+
+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;
+	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);
+
+	spin_unlock_irqrestore(&rtc->lock, flags);
+
+	return 0;
+}
+
+static int aspeed_rtc_set_time(struct device *dev, struct rtc_time *tm)
+{
+	struct aspeed_rtc *rtc = dev_get_drvdata(dev);
+	unsigned long flags;
+	u32 reg1, reg2, ctrl;
+	int year, cent;
+
+	spin_lock_irqsave(&rtc->lock, flags);
+
+	cent = (tm->tm_year + 1900) / 100;
+	year = tm->tm_year % 100;
+
+	reg1 = (tm->tm_mday << 24) | (tm->tm_hour << 16) | (tm->tm_min << 8) |
+		tm->tm_sec;
+
+	/* Hardware is 1-12, convert to 0-11 */
+	reg2 = ((cent & 0x1f) << 16) | ((year & 0x7f) << 8) |
+		((tm->tm_mon & 0xf) + 1);
+
+	ctrl = readl(rtc->base + RTC_CTRL);
+	writel(ctrl | RTC_UNLOCK, rtc->base + RTC_CTRL);
+
+	writel(reg1, rtc->base + RTC_TIME);
+	writel(reg2, rtc->base + RTC_YEAR);
+
+	writel(ctrl, rtc->base + RTC_CTRL);
+
+	spin_unlock_irqrestore(&rtc->lock, flags);
+
+	return 0;
+}
+
+static const struct rtc_class_ops aspeed_rtc_ops = {
+	.read_time = aspeed_rtc_read_time,
+	.set_time = aspeed_rtc_set_time,
+};
+
+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;
+
+	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);
+
+	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);
+
+	return 0;
+}
+
+static const struct of_device_id aspeed_rtc_match[] = {
+	{ .compatible = "aspeed,ast2400-rtc", },
+	{ .compatible = "aspeed,ast2500-rtc", },
+	{}
+};
+MODULE_DEVICE_TABLE(of, aspeed_rtc_match);
+
+static struct platform_driver aspeed_rtc_driver = {
+	.driver = {
+		.name = "aspeed-rtc",
+		.of_match_table = of_match_ptr(aspeed_rtc_match),
+	},
+};
+
+module_platform_driver_probe(aspeed_rtc_driver, aspeed_rtc_probe);
+
+MODULE_DESCRIPTION("Aspeed RTC driver");
+MODULE_AUTHOR("Joel Stanley <joel@jms.id.au>");
+MODULE_LICENSE("GPL");
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] dt-bindings: rtc: Add ASPEED description
  2018-10-03 13:31 [PATCH 0/2] rtc: Add ASPEED RTC Joel Stanley
  2018-10-03 13:31 ` [PATCH 1/2] rtc: Add ASPEED RTC driver Joel Stanley
@ 2018-10-03 13:31 ` Joel Stanley
  2018-10-04 18:37   ` Alexandre Belloni
  1 sibling, 1 reply; 5+ messages in thread
From: Joel Stanley @ 2018-10-03 13:31 UTC (permalink / raw)
  To: Alessandro Zummo, Alexandre Belloni, linux-rtc
  Cc: Andrew Jeffery, Christian Svensson, linux-arm-kernel, linux-aspeed

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

Signed-off-by: Joel Stanley <joel@jms.id.au>
---
 .../devicetree/bindings/rtc/rtc-aspeed.txt     | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/rtc-aspeed.txt

diff --git a/Documentation/devicetree/bindings/rtc/rtc-aspeed.txt b/Documentation/devicetree/bindings/rtc/rtc-aspeed.txt
new file mode 100644
index 000000000000..d31a4d24c75f
--- /dev/null
+++ b/Documentation/devicetree/bindings/rtc/rtc-aspeed.txt
@@ -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@1e781000 {
+           compatible = "aspeed,ast2400-rtc";
+           reg = <0x1e781000 0x18>;
+           status = "disabled";
+   };
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] rtc: Add ASPEED RTC driver
  2018-10-03 13:31 ` [PATCH 1/2] rtc: Add ASPEED RTC driver Joel Stanley
@ 2018-10-04 18:37   ` Alexandre Belloni
  0 siblings, 0 replies; 5+ messages in thread
From: Alexandre Belloni @ 2018-10-04 18:37 UTC (permalink / raw)
  To: Joel Stanley
  Cc: Alessandro Zummo, linux-rtc, Andrew Jeffery, Christian Svensson,
	linux-arm-kernel, linux-aspeed

Hello,

On 03/10/2018 15:31:54+0200, Joel Stanley wrote:
> +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;
> +	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?

> +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;
> +
> +	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.

> +	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.

-- 
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] dt-bindings: rtc: Add ASPEED description
  2018-10-03 13:31 ` [PATCH 2/2] dt-bindings: rtc: Add ASPEED description Joel Stanley
@ 2018-10-04 18:37   ` Alexandre Belloni
  0 siblings, 0 replies; 5+ messages in thread
From: Alexandre Belloni @ 2018-10-04 18:37 UTC (permalink / raw)
  To: Joel Stanley
  Cc: Alessandro Zummo, linux-rtc, Andrew Jeffery, Christian Svensson,
	linux-arm-kernel, linux-aspeed

I think this patch should come first.

On 03/10/2018 15:31:55+0200, Joel Stanley wrote:
> Describe the RTC as used in the ASPEED ast2400 and ast2500 SoCs.
> 
> Signed-off-by: Joel Stanley <joel@jms.id.au>
> ---
>  .../devicetree/bindings/rtc/rtc-aspeed.txt     | 18 ++++++++++++++++++
>  1 file changed, 18 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/rtc/rtc-aspeed.txt
> 
> diff --git a/Documentation/devicetree/bindings/rtc/rtc-aspeed.txt b/Documentation/devicetree/bindings/rtc/rtc-aspeed.txt
> new file mode 100644
> index 000000000000..d31a4d24c75f
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/rtc/rtc-aspeed.txt
> @@ -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@1e781000 {
> +           compatible = "aspeed,ast2400-rtc";
> +           reg = <0x1e781000 0x18>;
> +           status = "disabled";
> +   };
> -- 
> 2.17.1
> 

-- 
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-10-04 18:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-10-03 13:31 [PATCH 0/2] rtc: Add ASPEED RTC Joel Stanley
2018-10-03 13:31 ` [PATCH 1/2] rtc: Add ASPEED RTC driver Joel Stanley
2018-10-04 18:37   ` Alexandre Belloni
2018-10-03 13:31 ` [PATCH 2/2] dt-bindings: rtc: Add ASPEED description Joel Stanley
2018-10-04 18:37   ` Alexandre Belloni

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).