Skip to contentSkip to conversation

rtc: microchip: Add driver for PolarFire SoC

PATCH Superseded Conor Dooley 4 participants 7 messages May 16, 2022 – May 30, 2022

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Conor Dooley

author maintainer
#0 lore raw

v3 cover rtc: microchip: Add driver for PolarFire SoC

Hey all,
This is technically a v3 of [0], although a fair bit of time has passed since then. In the meantime I upstreamed the dt-binding, which was in the v1, and this patch depends on the fixes to the dt-binding and device tree etc which landed in v5.18-rc5.

The driver is quite substantially rewritten from the v1, as you wanted it to be switched to "binary" rather than calendar mode - so hopefully I have satisfied your concerns with the original driver. Specifically you had an significant issue with the counter being reset on startup & that is no longer the case.

Thanks,
Conor.

Changes from v2:
- move prescaler out of mpfs_rtc_dev & into probe

Changes from v1:
- remove duplicate and unused defines
- remove oneline mpfs_rtc_set_prescaler function
- dont unconditionally turn off the rtc in the init function
- dont reset the rtc when init is run.
- dont disable the alarm when we boot
- use binary, not calendar mode
- delete mpfs_rtc_init & set prescale in probe
- use dev_pm_set_wake_irq rather than writing suspend/resume functions
- delete calendar mode only register defines
- since using binary mode, set range min to zero
- set range max to max alarm value (is this acceptable?)
- added a MAINTAINERS entry: when v1 was submitted there was nothing to add to, but there is now.

[0] https://lore.kernel.org/linux-rtc/[email protected]/

Conor Dooley (2):
rtc: Add driver for Microchip PolarFire SoC
MAINTAINERS: add PolarFire SoC's RTC

 MAINTAINERS            |   1 +
 drivers/rtc/Kconfig    |  10 ++
 drivers/rtc/Makefile   |   1 +
 drivers/rtc/rtc-mpfs.c | 328 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 340 insertions(+)
 create mode 100644 drivers/rtc/rtc-mpfs.c
Signature · 1 line
2.36.1
author maintainer
#1 lore raw

PATCH v3 1/2 rtc: Add driver for Microchip PolarFire SoC

Add support for the built-in RTC on Microchip PolarFire SoC

---
 drivers/rtc/Kconfig    |  10 ++
 drivers/rtc/Makefile   |   1 +
 drivers/rtc/rtc-mpfs.c | 328 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 339 insertions(+)
 create mode 100644 drivers/rtc/rtc-mpfs.c
drivers/rtc/Kconfig +10 -0
index 41c65b4d2baf..a194422328da 100644
@@ -1966,4 +1966,14 @@ config RTC_DRV_MSC313
 	  This driver can also be built as a module, if so, the module
 	  will be called "rtc-msc313".
 
+config RTC_DRV_POLARFIRE_SOC
+	tristate "Microchip PolarFire SoC built-in RTC"
+	depends on SOC_MICROCHIP_POLARFIRE
+	help
+	  If you say yes here you will get support for the
+	  built-in RTC on Polarfire SoC.
+
+	  This driver can also be built as a module, if so, the module
+	  will be called "rtc-mpfs".
+
 endif # RTC_CLASS
drivers/rtc/Makefile +1 -0
index 2d827d8261d5..25ee5ba870a9 100644
@@ -130,6 +130,7 @@ obj-$(CONFIG_RTC_DRV_PIC32)	+= rtc-pic32.o
 obj-$(CONFIG_RTC_DRV_PL030)	+= rtc-pl030.o
 obj-$(CONFIG_RTC_DRV_PL031)	+= rtc-pl031.o
 obj-$(CONFIG_RTC_DRV_PM8XXX)	+= rtc-pm8xxx.o
+obj-$(CONFIG_RTC_DRV_POLARFIRE_SOC)	+= rtc-mpfs.o
 obj-$(CONFIG_RTC_DRV_PS3)	+= rtc-ps3.o
 obj-$(CONFIG_RTC_DRV_PXA)	+= rtc-pxa.o
 obj-$(CONFIG_RTC_DRV_R7301)	+= rtc-r7301.o
drivers/rtc/rtc-mpfs.c new file +328 -0

328 lines not on this page · raw message

Signature · 1 line
2.36.1
#2 lore raw

Re: [PATCH v3 1/2] rtc: Add driver for Microchip PolarFire SoC

Hello,

drivers/rtc/rtc-mpfs.c line 56

Quoted 4 lines · Conor Dooley
drivers/rtc/rtc-mpfs.c new file +4 -0
+struct mpfs_rtc_dev {
+	struct rtc_device *rtc;
+	void __iomem *base;
+	int wakeup_irq;

I believe this is only used in .probe so you make it local to this function.

drivers/rtc/rtc-mpfs.c line 90

Quoted 11 lines
drivers/rtc/rtc-mpfs.c new file +9 -0
+};
+
 
 
+static int mpfs_rtc_readtime(struct device *dev, struct rtc_time *tm)
+{
+	struct mpfs_rtc_dev *rtcdev = dev_get_drvdata(dev);
+	u64 time;
+
+	time = ((u64)readl(rtcdev->base + DATETIME_UPPER_REG) & DATETIME_UPPER_MASK) << 32;
+	time |= readl(rtcdev->base + DATETIME_LOWER_REG);

Are the registers properly latched on a DATETIME_UPPER_REG read?

drivers/rtc/rtc-mpfs.c line 91

Quoted 1 line
drivers/rtc/rtc-mpfs.c new file +1 -0
+	rtc_time64_to_tm(time + rtcdev->rtc->range_min, tm);

range_min is never set so it will end up being 0. I guess you can avoid a bunch of arithmetic in all the driver. Offsetting will happen in the core which will probably never happen anyway because the max year is 141338. I guess we will all be gone by then ;)

Quoted 61 lines
drivers/rtc/rtc-mpfs.c new file +60 -0
+
+	return 0;
+}
+
 
+static int mpfs_rtc_probe(struct platform_device *pdev)
+{
+	struct mpfs_rtc_dev *rtcdev;
+	struct clk *clk;
+	u32 prescaler;
+	int ret;
+
+	rtcdev = devm_kzalloc(&pdev->dev, sizeof(struct mpfs_rtc_dev), GFP_KERNEL);
+	if (!rtcdev)
+		return -ENOMEM;
+
+	platform_set_drvdata(pdev, rtcdev);
+
+	rtcdev->rtc = devm_rtc_allocate_device(&pdev->dev);
+	if (IS_ERR(rtcdev->rtc))
+		return PTR_ERR(rtcdev->rtc);
+
+	rtcdev->rtc->ops = &mpfs_rtc_ops;
+
+	/* range is capped by alarm max, lower reg is 31:0 & upper is 10:0 */
+	rtcdev->rtc->range_max = GENMASK_ULL(42, 0);
+
+	clk = mpfs_rtc_init_clk(&pdev->dev);
+	if (IS_ERR(clk))
+		return PTR_ERR(clk);
+
+	rtcdev->base = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(rtcdev->base)) {
+		dev_dbg(&pdev->dev, "invalid ioremap resources\n");
+		return PTR_ERR(rtcdev->base);
+	}
+
+	rtcdev->wakeup_irq = platform_get_irq(pdev, 0);
+	if (rtcdev->wakeup_irq <= 0) {
+		dev_dbg(&pdev->dev, "could not get wakeup irq\n");
+		return rtcdev->wakeup_irq;
+	}
+	ret = devm_request_irq(&pdev->dev, rtcdev->wakeup_irq, mpfs_rtc_wakeup_irq_handler, 0,
+			       dev_name(&pdev->dev), rtcdev);
+	if (ret) {
+		dev_dbg(&pdev->dev, "could not request wakeup irq\n");
+		return ret;
+	}
+
+	/* prescaler hardware adds 1 to reg value */
+	prescaler = clk_get_rate(devm_clk_get(&pdev->dev, "rtcref")) - 1;
+
+	if (prescaler > MAX_PRESCALER_COUNT) {
+		dev_dbg(&pdev->dev, "invalid prescaler %d\n", prescaler);
+		return -EINVAL;
+	}
+
+	writel(prescaler, rtcdev->base + PRESCALER_REG);
+	dev_info(&pdev->dev, "prescaler set to: 0x%X \r\n", prescaler);
+
+	device_init_wakeup(&pdev->dev, true);

drivers/rtc/rtc-mpfs.c line 301

Quoted 10 lines
drivers/rtc/rtc-mpfs.c new file +10 -0
+	ret = dev_pm_set_wake_irq(&pdev->dev, rtcdev->wakeup_irq);
+	if (ret)
+		dev_err(&pdev->dev, "failed to enable irq wake\n");
+
+	return devm_rtc_register_device(rtcdev->rtc);
+}
+
+static int mpfs_rtc_remove(struct platform_device *pdev)
+{
+	mpfs_rtc_alarm_irq_enable(&pdev->dev, 0);

This is not something you want to do if you want to wake up from hibernate or any similar sleep state.

Quoted 30 lines
drivers/rtc/rtc-mpfs.c new file +27 -0
+	device_init_wakeup(&pdev->dev, 0);
+
+	return 0;
+}
+
+static const struct of_device_id mpfs_rtc_of_match[] = {
+	{ .compatible = "microchip,mpfs-rtc" },
+	{ }
+};
+
+MODULE_DEVICE_TABLE(of, mpfs_rtc_of_match);
+
+static struct platform_driver mpfs_rtc_driver = {
+	.probe = mpfs_rtc_probe,
+	.remove = mpfs_rtc_remove,
+	.driver	= {
+		.name = "mpfs_rtc",
+		.of_match_table = mpfs_rtc_of_match,
+	},
+};
+
+module_platform_driver(mpfs_rtc_driver);
+
+MODULE_DESCRIPTION("Real time clock for Microchip Polarfire SoC");
+MODULE_AUTHOR("Daire McNamara <[email protected]>");
+MODULE_AUTHOR("Conor Dooley <[email protected]>");
+MODULE_LICENSE("GPL");
Signature · 1 line
2.36.1
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author maintainer
#3 lore raw

Re: [PATCH v3 1/2] rtc: Add driver for Microchip PolarFire SoC

Quoted 16 lines · Alexandre Belloni

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Hello,

drivers/rtc/rtc-mpfs.c line 56

Quoted 4 lines · Conor Dooley
drivers/rtc/rtc-mpfs.c new file +4 -0
+struct mpfs_rtc_dev {
+     struct rtc_device *rtc;
+     void __iomem *base;
+     int wakeup_irq;

I believe this is only used in .probe so you make it local to this function.>

Quoted 2 lines
(quoted diff) +2 -0
+};
+
Quoted 9 lines

drivers/rtc/rtc-mpfs.c line 90

Quoted 7 lines
drivers/rtc/rtc-mpfs.c new file +7 -0
+static int mpfs_rtc_readtime(struct device *dev, struct rtc_time *tm)
+{
+     struct mpfs_rtc_dev *rtcdev = dev_get_drvdata(dev);
+     u64 time;
+
+     time = ((u64)readl(rtcdev->base + DATETIME_UPPER_REG) & DATETIME_UPPER_MASK) << 32;
+     time |= readl(rtcdev->base + DATETIME_LOWER_REG);

Are the registers properly latched on a DATETIME_UPPER_REG read?

It's a good thing you asked - I went to double check and these reads are backwards. /facepalm
A read from the upper register will always be aligned with the last read of the lower register. Stupid oversight, sorry.

Quoted 7 lines
Quoted 2 lines

+ rtc_time64_to_tm(time + rtcdev->rtc->range_min, tm);

range_min is never set so it will end up being 0. I guess you can avoid a bunch of arithmetic in all the driver. Offsetting will happen in the core which will probably never happen anyway because the max year is 141338. I guess we will all be gone by then ;)

SGTM, including not being around in year 141338...

Thanks,
Conor.

Quoted 111 lines
Quoted 62 lines
drivers/rtc/rtc-mpfs.c new file +60 -0
+
+     return 0;
+}
+
 
+static int mpfs_rtc_probe(struct platform_device *pdev)
+{
+     struct mpfs_rtc_dev *rtcdev;
+     struct clk *clk;
+     u32 prescaler;
+     int ret;
+
+     rtcdev = devm_kzalloc(&pdev->dev, sizeof(struct mpfs_rtc_dev), GFP_KERNEL);
+     if (!rtcdev)
+             return -ENOMEM;
+
+     platform_set_drvdata(pdev, rtcdev);
+
+     rtcdev->rtc = devm_rtc_allocate_device(&pdev->dev);
+     if (IS_ERR(rtcdev->rtc))
+             return PTR_ERR(rtcdev->rtc);
+
+     rtcdev->rtc->ops = &mpfs_rtc_ops;
+
+     /* range is capped by alarm max, lower reg is 31:0 & upper is 10:0 */
+     rtcdev->rtc->range_max = GENMASK_ULL(42, 0);
+
+     clk = mpfs_rtc_init_clk(&pdev->dev);
+     if (IS_ERR(clk))
+             return PTR_ERR(clk);
+
+     rtcdev->base = devm_platform_ioremap_resource(pdev, 0);
+     if (IS_ERR(rtcdev->base)) {
+             dev_dbg(&pdev->dev, "invalid ioremap resources\n");
+             return PTR_ERR(rtcdev->base);
+     }
+
+     rtcdev->wakeup_irq = platform_get_irq(pdev, 0);
+     if (rtcdev->wakeup_irq <= 0) {
+             dev_dbg(&pdev->dev, "could not get wakeup irq\n");
+             return rtcdev->wakeup_irq;
+     }
+     ret = devm_request_irq(&pdev->dev, rtcdev->wakeup_irq, mpfs_rtc_wakeup_irq_handler, 0,
+                            dev_name(&pdev->dev), rtcdev);
+     if (ret) {
+             dev_dbg(&pdev->dev, "could not request wakeup irq\n");
+             return ret;
+     }
+
+     /* prescaler hardware adds 1 to reg value */
+     prescaler = clk_get_rate(devm_clk_get(&pdev->dev, "rtcref")) - 1;
+
+     if (prescaler > MAX_PRESCALER_COUNT) {
+             dev_dbg(&pdev->dev, "invalid prescaler %d\n", prescaler);
+             return -EINVAL;
+     }
+
+     writel(prescaler, rtcdev->base + PRESCALER_REG);
+     dev_info(&pdev->dev, "prescaler set to: 0x%X \r\n", prescaler);
+
+     device_init_wakeup(&pdev->dev, true);

drivers/rtc/rtc-mpfs.c line 301

Quoted 10 lines
drivers/rtc/rtc-mpfs.c new file +10 -0
+     ret = dev_pm_set_wake_irq(&pdev->dev, rtcdev->wakeup_irq);
+     if (ret)
+             dev_err(&pdev->dev, "failed to enable irq wake\n");
+
+     return devm_rtc_register_device(rtcdev->rtc);
+}
+
+static int mpfs_rtc_remove(struct platform_device *pdev)
+{
+     mpfs_rtc_alarm_irq_enable(&pdev->dev, 0);

This is not something you want to do if you want to wake up from hibernate or any similar sleep state.

Quoted 28 lines
drivers/rtc/rtc-mpfs.c new file +27 -1
+     device_init_wakeup(&pdev->dev, 0);
+
+     return 0;
+}
+
+static const struct of_device_id mpfs_rtc_of_match[] = {
+     { .compatible = "microchip,mpfs-rtc" },
+     { }
+};
+
+MODULE_DEVICE_TABLE(of, mpfs_rtc_of_match);
+
+static struct platform_driver mpfs_rtc_driver = {
+     .probe = mpfs_rtc_probe,
+     .remove = mpfs_rtc_remove,
+     .driver = {
+             .name = "mpfs_rtc",
+             .of_match_table = mpfs_rtc_of_match,
+     },
+};
+
+module_platform_driver(mpfs_rtc_driver);
+
+MODULE_DESCRIPTION("Real time clock for Microchip Polarfire SoC");
+MODULE_AUTHOR("Daire McNamara <[email protected]>");
+MODULE_AUTHOR("Conor Dooley <[email protected]>");
+MODULE_LICENSE("GPL");
--
Quoted 2 lines

2.36.1

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

#4 lore raw

Re: [PATCH v3 1/2] rtc: Add driver for Microchip PolarFire SoC

In reply to Conor Dooley

Hi!

drivers/rtc/rtc-mpfs.c line 40

Quoted 6 lines

Add support for the built-in RTC on Microchip PolarFire SoC

drivers/rtc/rtc-mpfs.c new file +4 -0
+#define CONTROL_UPLOAD_BIT	BIT(5)
+#define CONTROL_DOWNLOAD_BIT	BIT(6)
+#define CONTROL_DOWNLOAD_BIT	BIT(6)
+#define CONTROL_WAKEUP_CLR_BIT	BIT(8)

Dup?

drivers/rtc/rtc-mpfs.c line 67

Quoted 9 lines
drivers/rtc/rtc-mpfs.c new file +9 -0
+static void mpfs_rtc_start(struct mpfs_rtc_dev *rtcdev)
+{
+	u32 ctrl;
+
+	ctrl = readl(rtcdev->base + CONTROL_REG);
+	ctrl &= ~(CONTROL_STOP_BIT | CONTROL_START_BIT);
+	ctrl |= CONTROL_START_BIT;
+	writel(ctrl, rtcdev->base + CONTROL_REG);
+}

You don't need to clear bit just to set it.

drivers/rtc/rtc-mpfs.c line 115

Quoted 4 lines
drivers/rtc/rtc-mpfs.c new file +4 -0
+	do {
+		prog = readl(rtcdev->base + CONTROL_REG);
+		prog &= CONTROL_UPLOAD_BIT;
+	} while (prog);

Limit to XY iterations?

drivers/rtc/rtc-mpfs.c line 128

Quoted 7 lines
drivers/rtc/rtc-mpfs.c new file +7 -0
+
+static int mpfs_rtc_readalarm(struct device *dev, struct rtc_wkalrm *alrm)
+{
+	u32 mode = readl(rtcdev->base + MODE_REG);
+	u64 time;
+
+	mode = readl(rtcdev->base + MODE_REG);

Dup?

Quoted 1 line

+ if (mode & MODE_WAKE_EN) + alrm->enabled = true; + else + alrm->enabled = false; +

enabled = ()?

Best regards,
Pavel

Signature · 7 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#5 lore raw

Re: [PATCH v3 1/2] rtc: Add driver for Microchip PolarFire SoC

Quoted 8 lines · Pavel Machek

Add support for the built-in RTC on Microchip PolarFire SoC

drivers/rtc/rtc-mpfs.c line 115

Quoted 4 lines
drivers/rtc/rtc-mpfs.c new file +4 -0
+     do {
+             prog = readl(rtcdev->base + CONTROL_REG);
+             prog &= CONTROL_UPLOAD_BIT;
+     } while (prog);

Limit to XY iterations?

Perhaps read_poll_timeout()?

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

author maintainer
#6 lore raw

PATCH v3 2/2 MAINTAINERS: add PolarFire SoC's RTC

In reply to Conor Dooley

Add an entry for PolarFire SoC's RTC drver to the existing support for PolarFire SoC.

---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index e8c52d0192a6..625d735f6a24 100644
@@ -16952,6 +16952,7 @@ L:	[email protected]
 S:	Supported
 F:	arch/riscv/boot/dts/microchip/
 F:	drivers/mailbox/mailbox-mpfs.c
+F:	drivers/rtc/rtc-mpfs.c
 F:	drivers/soc/microchip/
 F:	include/soc/microchip/mpfs.h
 
Signature · 1 line
2.36.1