Skip to contentSkip to conversation

RTC: Add Loongson-2K0300 support

PATCH Superseded Binbin Zhou 5 participants 9 messages Dec 23, 2025 – Dec 30, 2025

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover RTC: Add Loongson-2K0300 support

Hi all:

This patch set introduces the Loongson-2K0300 RTC, which has a similar hardware design to the Loongson-1B, but without the alarm feature.

To Keguang:
Would you have time to test the driver on a Loongson-1?

Thanks.
Binbin

Binbin Zhou (2):
dt-binding: rtc: loongson: Document Loongson-2K0300 compatible
rtc: loongson: Add Loongson-2K0300 support

 .../devicetree/bindings/rtc/loongson,rtc.yaml | 13 ++++
 drivers/rtc/rtc-loongson.c                    | 65 +++++++++++++------
 2 files changed, 57 insertions(+), 21 deletions(-)

base-commit: 16bd954c93360145bc77cc601e350913fc28182d

Signature · 1 line
2.47.3
#1 lore raw

PATCH 1/2 dt-binding: rtc: loongson: Document Loongson-2K0300 compatible

Add "loongson,ls2k0300-rtc" dedicated compatible to represent the RTC interface of the Loongson-2K0300 chip.

Its hardware design is similar to that of the Loongson-1B, but it does not support the alarm feature.

---
 .../devicetree/bindings/rtc/loongson,rtc.yaml       | 13 +++++++++++++
 1 file changed, 13 insertions(+)
Documentation/devicetree/bindings/rtc/loongson,rtc.yaml +13 -0
index f89c1f660aee..aac91c79ffdb 100644
@@ -23,6 +23,7 @@ properties:
           - loongson,ls1b-rtc
           - loongson,ls1c-rtc
           - loongson,ls7a-rtc
+          - loongson,ls2k0300-rtc
           - loongson,ls2k1000-rtc
       - items:
           - enum:
@@ -42,6 +43,18 @@ required:
 
 unevaluatedProperties: false
 
+if:
+  properties:
+    compatible:
+      contains:
+        enum:
+          - loongson,ls1c-rtc
+          - loongson,ls2k0300-rtc
+
+then:
+  properties:
+    interrupts: false
+
 examples:
   - |
     #include <dt-bindings/interrupt-controller/irq.h>
Signature · 1 line
2.47.3
maintainer
#2 lore raw

Re: [PATCH v1 1/2] dt-binding: rtc: loongson: Document Loongson-2K0300 compatible

Quoted 24 lines · Binbin Zhou

Add "loongson,ls2k0300-rtc" dedicated compatible to represent the RTC interface of the Loongson-2K0300 chip.

Its hardware design is similar to that of the Loongson-1B, but it does not support the alarm feature.

---
 .../devicetree/bindings/rtc/loongson,rtc.yaml       | 13 +++++++++++++
 1 file changed, 13 insertions(+)
Documentation/devicetree/bindings/rtc/loongson,rtc.yaml +1 -0
index f89c1f660aee..aac91c79ffdb 100644
@@ -23,6 +23,7 @@ properties:
           - loongson,ls1b-rtc
           - loongson,ls1c-rtc
           - loongson,ls7a-rtc
+          - loongson,ls2k0300-rtc
           - loongson,ls2k1000-rtc
       - items:
           - enum:

Documentation/devicetree/bindings/rtc/loongson,rtc.yaml required: · line 51

Quoted 9 lines
Documentation/devicetree/bindings/rtc/loongson,rtc.yaml +6 -0
@@ -42,6 +43,18 @@ required:
 
 unevaluatedProperties: false
 
+if:
+  properties:
+    compatible:
+      contains:
+        enum:
+          - loongson,ls1c-rtc

This seems unrelated?

Quoted 12 lines
Documentation/devicetree/bindings/rtc/loongson,rtc.yaml +6 -0
+          - loongson,ls2k0300-rtc
+
+then:
+  properties:
+    interrupts: false
+
 examples:
   - |
     #include <dt-bindings/interrupt-controller/irq.h>
Signature · 1 line
2.47.3
#3 lore raw

Re: [PATCH v1 1/2] dt-binding: rtc: loongson: Document Loongson-2K0300 compatible

Hi Rob:

Thanks for your review.

Quoted 29 lines · Rob Herring
Quoted 27 lines · Binbin Zhou

Add "loongson,ls2k0300-rtc" dedicated compatible to represent the RTC interface of the Loongson-2K0300 chip.

Its hardware design is similar to that of the Loongson-1B, but it does not support the alarm feature.

---
 .../devicetree/bindings/rtc/loongson,rtc.yaml       | 13 +++++++++++++
 1 file changed, 13 insertions(+)
Documentation/devicetree/bindings/rtc/loongson,rtc.yaml +1 -0
index f89c1f660aee..aac91c79ffdb 100644
@@ -23,6 +23,7 @@ properties:
           - loongson,ls1b-rtc
           - loongson,ls1c-rtc
           - loongson,ls7a-rtc
+          - loongson,ls2k0300-rtc
           - loongson,ls2k1000-rtc
       - items:
           - enum:
@@ -42,6 +43,18 @@ required:
 
 unevaluatedProperties: false
 
Quoted 8 lines

Documentation/devicetree/bindings/rtc/loongson,rtc.yaml required: · line 51

Quoted 6 lines
Documentation/devicetree/bindings/rtc/loongson,rtc.yaml +6 -0
+if:
+  properties:
+    compatible:
+      contains:
+        enum:
+          - loongson,ls1c-rtc

This seems unrelated?

Loongson-1C does not support the alarm feature, so the interrupts property is not needed.

Technically, this is my fault and should have been described in the binding before, just correcting it now.
Perhaps I should have mentioned this in the commit message.

Quoted 13 lines
Quoted 13 lines
Documentation/devicetree/bindings/rtc/loongson,rtc.yaml +6 -1
+          - loongson,ls2k0300-rtc
+
+then:
+  properties:
+    interrupts: false
+
 examples:
   - |
     #include <dt-bindings/interrupt-controller/irq.h>
--

2.47.3

Signature · 2 lines
Thanks.
Binbin
#4 lore raw

PATCH 2/2 rtc: loongson: Add Loongson-2K0300 support

In reply to Binbin Zhou

The Loongson-2K0300's rtc hardware design is similar to that of the Loongson-1B, but it does not support the alarm feature.

Introduce LOONGSON_RTC_ALARM_WORKAROUND, which indicates a chip that does not support the alarm function, and rewritethe related logic in loongson_rtc_alarm_setting()

---
 drivers/rtc/rtc-loongson.c | 65 ++++++++++++++++++++++++++------------
 1 file changed, 44 insertions(+), 21 deletions(-)
drivers/rtc/rtc-loongson.c +44 -21
index 2ca7ffd5d7a9..6b076bd4e24e 100644
@@ -67,6 +67,7 @@
  * Accessing the relevant registers will cause the system to hang.
  */
 #define LS1C_RTC_CTRL_WORKAROUND	BIT(0)
+#define LOONGSON_RTC_ALARM_WORKAROUND	BIT(1)
 
 struct loongson_rtc_config {
 	u32 pm_offset;	/* Offset of PM domain, for RTC alarm wakeup */
@@ -89,7 +90,7 @@ static const struct loongson_rtc_config ls1b_rtc_config = {
 
 static const struct loongson_rtc_config ls1c_rtc_config = {
 	.pm_offset = 0,
-	.flags = LS1C_RTC_CTRL_WORKAROUND,
+	.flags = LS1C_RTC_CTRL_WORKAROUND | LOONGSON_RTC_ALARM_WORKAROUND,
 };
 
 static const struct loongson_rtc_config generic_rtc_config = {
@@ -97,6 +98,11 @@ static const struct loongson_rtc_config generic_rtc_config = {
 	.flags = 0,
 };
 
+static const struct loongson_rtc_config ls2k0300_rtc_config = {
+	.pm_offset = 0x0,
+	.flags = LOONGSON_RTC_ALARM_WORKAROUND,
+};
+
 static const struct loongson_rtc_config ls2k1000_rtc_config = {
 	.pm_offset = 0x800,
 	.flags = 0,
@@ -299,9 +305,41 @@ static const struct rtc_class_ops loongson_rtc_ops = {
 	.alarm_irq_enable = loongson_rtc_alarm_irq_enable,
 };
 
+static int loongson_rtc_alarm_setting(struct platform_device *pdev, void __iomem *regs)
+{
+	int ret = 0, alarm_irq;
+	struct device *dev = &pdev->dev;
+	struct loongson_rtc_priv *priv = dev_get_drvdata(dev);
+
+	if (priv->config->flags & LOONGSON_RTC_ALARM_WORKAROUND) {
+		/* Loongson-1C/Loongson-2K0300 RTC does not support alarm */
+		clear_bit(RTC_FEATURE_ALARM, priv->rtcdev->features);
+		return 0;
+	}
+
+	/* Get RTC alarm irq */
+	alarm_irq = platform_get_irq(pdev, 0);
+	if (alarm_irq < 0)
+		return alarm_irq;
+
+	ret = devm_request_irq(dev, alarm_irq, loongson_rtc_isr, 0, "loongson-alarm",
+			       priv);
+	if (ret < 0)
+		return ret;
+
+	priv->pm_base = regs - priv->config->pm_offset;
+	device_init_wakeup(dev, true);
+
+	if (has_acpi_companion(dev))
+		acpi_install_fixed_event_handler(ACPI_EVENT_RTC,
+						 loongson_rtc_handler, priv);
+
+	return ret;
+}
+
 static int loongson_rtc_probe(struct platform_device *pdev)
 {
-	int ret, alarm_irq;
+	int ret;
 	void __iomem *regs;
 	struct loongson_rtc_priv *priv;
 	struct device *dev = &pdev->dev;
@@ -330,25 +368,9 @@ static int loongson_rtc_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, PTR_ERR(priv->rtcdev),
 				     "devm_rtc_allocate_device failed\n");
 
-	/* Get RTC alarm irq */
-	alarm_irq = platform_get_irq(pdev, 0);
-	if (alarm_irq > 0) {
-		ret = devm_request_irq(dev, alarm_irq, loongson_rtc_isr,
-				       0, "loongson-alarm", priv);
-		if (ret < 0)
-			return dev_err_probe(dev, ret, "Unable to request irq %d\n",
-					     alarm_irq);
-
-		priv->pm_base = regs - priv->config->pm_offset;
-		device_init_wakeup(dev, true);
-
-		if (has_acpi_companion(dev))
-			acpi_install_fixed_event_handler(ACPI_EVENT_RTC,
-							 loongson_rtc_handler, priv);
-	} else {
-		/* Loongson-1C RTC does not support alarm */
-		clear_bit(RTC_FEATURE_ALARM, priv->rtcdev->features);
-	}
+	ret = loongson_rtc_alarm_setting(pdev, regs);
+	if (ret)
+		return ret;
 
 	/* Loongson RTC does not support UIE */
 	clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, priv->rtcdev->features);
@@ -379,6 +401,7 @@ static const struct of_device_id loongson_rtc_of_match[] = {
 	{ .compatible = "loongson,ls1b-rtc", .data = &ls1b_rtc_config },
 	{ .compatible = "loongson,ls1c-rtc", .data = &ls1c_rtc_config },
 	{ .compatible = "loongson,ls7a-rtc", .data = &generic_rtc_config },
+	{ .compatible = "loongson,ls2k0300-rtc", .data = &ls2k0300_rtc_config },
 	{ .compatible = "loongson,ls2k1000-rtc", .data = &ls2k1000_rtc_config },
 	{ /* sentinel */ }
 };
Signature · 1 line
2.47.3
maintainer
#5 lore raw

Re: [PATCH v1 2/2] rtc: loongson: Add Loongson-2K0300 support

Hi, Binbin,

Quoted 18 lines · Binbin Zhou

The Loongson-2K0300's rtc hardware design is similar to that of the Loongson-1B, but it does not support the alarm feature.

Introduce LOONGSON_RTC_ALARM_WORKAROUND, which indicates a chip that does not support the alarm function, and rewritethe related logic in loongson_rtc_alarm_setting()

---
 drivers/rtc/rtc-loongson.c | 65 ++++++++++++++++++++++++++------------
 1 file changed, 44 insertions(+), 21 deletions(-)
drivers/rtc/rtc-loongson.c +0 -0
index 2ca7ffd5d7a9..6b076bd4e24e 100644

drivers/rtc/rtc-loongson.c line 70

Quoted 4 lines
drivers/rtc/rtc-loongson.c +1 -0
@@ -67,6 +67,7 @@
  * Accessing the relevant registers will cause the system to hang.
  */
 #define LS1C_RTC_CTRL_WORKAROUND       BIT(0)
+#define LOONGSON_RTC_ALARM_WORKAROUND  BIT(1)

Can we reuse existing logic, which means "alarm_irq = platform_get_irq(pdev, 0)" return 0 for LS2K0300?

Huacai

Quoted 108 lines
drivers/rtc/rtc-loongson.c +43 -22
 struct loongson_rtc_config {
        u32 pm_offset;  /* Offset of PM domain, for RTC alarm wakeup */
@@ -89,7 +90,7 @@ static const struct loongson_rtc_config ls1b_rtc_config = {
 
 static const struct loongson_rtc_config ls1c_rtc_config = {
        .pm_offset = 0,
-       .flags = LS1C_RTC_CTRL_WORKAROUND,
+       .flags = LS1C_RTC_CTRL_WORKAROUND | LOONGSON_RTC_ALARM_WORKAROUND,
 };
 
 static const struct loongson_rtc_config generic_rtc_config = {
@@ -97,6 +98,11 @@ static const struct loongson_rtc_config generic_rtc_config = {
        .flags = 0,
 };
 
+static const struct loongson_rtc_config ls2k0300_rtc_config = {
+       .pm_offset = 0x0,
+       .flags = LOONGSON_RTC_ALARM_WORKAROUND,
+};
+
 static const struct loongson_rtc_config ls2k1000_rtc_config = {
        .pm_offset = 0x800,
        .flags = 0,
@@ -299,9 +305,41 @@ static const struct rtc_class_ops loongson_rtc_ops = {
        .alarm_irq_enable = loongson_rtc_alarm_irq_enable,
 };
 
+static int loongson_rtc_alarm_setting(struct platform_device *pdev, void __iomem *regs)
+{
+       int ret = 0, alarm_irq;
+       struct device *dev = &pdev->dev;
+       struct loongson_rtc_priv *priv = dev_get_drvdata(dev);
+
+       if (priv->config->flags & LOONGSON_RTC_ALARM_WORKAROUND) {
+               /* Loongson-1C/Loongson-2K0300 RTC does not support alarm */
+               clear_bit(RTC_FEATURE_ALARM, priv->rtcdev->features);
+               return 0;
+       }
+
+       /* Get RTC alarm irq */
+       alarm_irq = platform_get_irq(pdev, 0);
+       if (alarm_irq < 0)
+               return alarm_irq;
+
+       ret = devm_request_irq(dev, alarm_irq, loongson_rtc_isr, 0, "loongson-alarm",
+                              priv);
+       if (ret < 0)
+               return ret;
+
+       priv->pm_base = regs - priv->config->pm_offset;
+       device_init_wakeup(dev, true);
+
+       if (has_acpi_companion(dev))
+               acpi_install_fixed_event_handler(ACPI_EVENT_RTC,
+                                                loongson_rtc_handler, priv);
+
+       return ret;
+}
+
 static int loongson_rtc_probe(struct platform_device *pdev)
 {
-       int ret, alarm_irq;
+       int ret;
        void __iomem *regs;
        struct loongson_rtc_priv *priv;
        struct device *dev = &pdev->dev;
@@ -330,25 +368,9 @@ static int loongson_rtc_probe(struct platform_device *pdev)
                return dev_err_probe(dev, PTR_ERR(priv->rtcdev),
                                     "devm_rtc_allocate_device failed\n");
 
-       /* Get RTC alarm irq */
-       alarm_irq = platform_get_irq(pdev, 0);
-       if (alarm_irq > 0) {
-               ret = devm_request_irq(dev, alarm_irq, loongson_rtc_isr,
-                                      0, "loongson-alarm", priv);
-               if (ret < 0)
-                       return dev_err_probe(dev, ret, "Unable to request irq %d\n",
-                                            alarm_irq);
-
-               priv->pm_base = regs - priv->config->pm_offset;
-               device_init_wakeup(dev, true);
-
-               if (has_acpi_companion(dev))
-                       acpi_install_fixed_event_handler(ACPI_EVENT_RTC,
-                                                        loongson_rtc_handler, priv);
-       } else {
-               /* Loongson-1C RTC does not support alarm */
-               clear_bit(RTC_FEATURE_ALARM, priv->rtcdev->features);
-       }
+       ret = loongson_rtc_alarm_setting(pdev, regs);
+       if (ret)
+               return ret;
 
        /* Loongson RTC does not support UIE */
        clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, priv->rtcdev->features);
@@ -379,6 +401,7 @@ static const struct of_device_id loongson_rtc_of_match[] = {
        { .compatible = "loongson,ls1b-rtc", .data = &ls1b_rtc_config },
        { .compatible = "loongson,ls1c-rtc", .data = &ls1c_rtc_config },
        { .compatible = "loongson,ls7a-rtc", .data = &generic_rtc_config },
+       { .compatible = "loongson,ls2k0300-rtc", .data = &ls2k0300_rtc_config },
        { .compatible = "loongson,ls2k1000-rtc", .data = &ls2k1000_rtc_config },
        { /* sentinel */ }
 };
--

2.47.3

#6 lore raw

Re: [PATCH v1 2/2] rtc: loongson: Add Loongson-2K0300 support

Hi:

Quoted 17 lines · Huacai Chen

Hi, Binbin,

Quoted 13 lines · Binbin Zhou

The Loongson-2K0300's rtc hardware design is similar to that of the Loongson-1B, but it does not support the alarm feature.

Introduce LOONGSON_RTC_ALARM_WORKAROUND, which indicates a chip that does not support the alarm function, and rewritethe related logic in loongson_rtc_alarm_setting()

---
 drivers/rtc/rtc-loongson.c | 65 ++++++++++++++++++++++++++------------
 1 file changed, 44 insertions(+), 21 deletions(-)
Quoted 11 lines

drivers/rtc/rtc-loongson.c line 70

Quoted 9 lines
drivers/rtc/rtc-loongson.c +1 -0
index 2ca7ffd5d7a9..6b076bd4e24e 100644
@@ -67,6 +67,7 @@
  * Accessing the relevant registers will cause the system to hang.
  */
 #define LS1C_RTC_CTRL_WORKAROUND       BIT(0)
+#define LOONGSON_RTC_ALARM_WORKAROUND  BIT(1)

Can we reuse existing logic, which means "alarm_irq = platform_get_irq(pdev, 0)" return 0 for LS2K0300?

First of all, as I understand it, platform_get_irq() doesn't normally return 0 unless interrupt 0 is configured in the DTS, which doesn't seem appropriate.

Also, the return value of platform_get_irq() was previously used to determine if the alarm feature was supported, which is not particularly accurate. It should be an inherent feature of the chip, and I think it would be more appropriate to define it in loongson_rtc_config.

Quoted 97 lines

Huacai

Quoted 94 lines
drivers/rtc/rtc-loongson.c +42 -21
 struct loongson_rtc_config {
        u32 pm_offset;  /* Offset of PM domain, for RTC alarm wakeup */
@@ -89,7 +90,7 @@ static const struct loongson_rtc_config ls1b_rtc_config = {
 
 static const struct loongson_rtc_config ls1c_rtc_config = {
        .pm_offset = 0,
-       .flags = LS1C_RTC_CTRL_WORKAROUND,
+       .flags = LS1C_RTC_CTRL_WORKAROUND | LOONGSON_RTC_ALARM_WORKAROUND,
 };
 
 static const struct loongson_rtc_config generic_rtc_config = {
@@ -97,6 +98,11 @@ static const struct loongson_rtc_config generic_rtc_config = {
        .flags = 0,
 };
 
+static const struct loongson_rtc_config ls2k0300_rtc_config = {
+       .pm_offset = 0x0,
+       .flags = LOONGSON_RTC_ALARM_WORKAROUND,
+};
+
 static const struct loongson_rtc_config ls2k1000_rtc_config = {
        .pm_offset = 0x800,
        .flags = 0,
@@ -299,9 +305,41 @@ static const struct rtc_class_ops loongson_rtc_ops = {
        .alarm_irq_enable = loongson_rtc_alarm_irq_enable,
 };
 
+static int loongson_rtc_alarm_setting(struct platform_device *pdev, void __iomem *regs)
+{
+       int ret = 0, alarm_irq;
+       struct device *dev = &pdev->dev;
+       struct loongson_rtc_priv *priv = dev_get_drvdata(dev);
+
+       if (priv->config->flags & LOONGSON_RTC_ALARM_WORKAROUND) {
+               /* Loongson-1C/Loongson-2K0300 RTC does not support alarm */
+               clear_bit(RTC_FEATURE_ALARM, priv->rtcdev->features);
+               return 0;
+       }
+
+       /* Get RTC alarm irq */
+       alarm_irq = platform_get_irq(pdev, 0);
+       if (alarm_irq < 0)
+               return alarm_irq;
+
+       ret = devm_request_irq(dev, alarm_irq, loongson_rtc_isr, 0, "loongson-alarm",
+                              priv);
+       if (ret < 0)
+               return ret;
+
+       priv->pm_base = regs - priv->config->pm_offset;
+       device_init_wakeup(dev, true);
+
+       if (has_acpi_companion(dev))
+               acpi_install_fixed_event_handler(ACPI_EVENT_RTC,
+                                                loongson_rtc_handler, priv);
+
+       return ret;
+}
+
 static int loongson_rtc_probe(struct platform_device *pdev)
 {
-       int ret, alarm_irq;
+       int ret;
        void __iomem *regs;
        struct loongson_rtc_priv *priv;
        struct device *dev = &pdev->dev;
@@ -330,25 +368,9 @@ static int loongson_rtc_probe(struct platform_device *pdev)
                return dev_err_probe(dev, PTR_ERR(priv->rtcdev),
                                     "devm_rtc_allocate_device failed\n");
 
-       /* Get RTC alarm irq */
-       alarm_irq = platform_get_irq(pdev, 0);
-       if (alarm_irq > 0) {
-               ret = devm_request_irq(dev, alarm_irq, loongson_rtc_isr,
-                                      0, "loongson-alarm", priv);
-               if (ret < 0)
-                       return dev_err_probe(dev, ret, "Unable to request irq %d\n",
-                                            alarm_irq);
-
-               priv->pm_base = regs - priv->config->pm_offset;
-               device_init_wakeup(dev, true);
-
-               if (has_acpi_companion(dev))
-                       acpi_install_fixed_event_handler(ACPI_EVENT_RTC,
-                                                        loongson_rtc_handler, priv);
-       } else {
-               /* Loongson-1C RTC does not support alarm */
-               clear_bit(RTC_FEATURE_ALARM, priv->rtcdev->features);
-       }
+       ret = loongson_rtc_alarm_setting(pdev, regs);
+       if (ret)
+               return ret;
 
Quoted 14 lines
Quoted 11 lines
drivers/rtc/rtc-loongson.c +1 -1
        /* Loongson RTC does not support UIE */
        clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, priv->rtcdev->features);
@@ -379,6 +401,7 @@ static const struct of_device_id loongson_rtc_of_match[] = {
        { .compatible = "loongson,ls1b-rtc", .data = &ls1b_rtc_config },
        { .compatible = "loongson,ls1c-rtc", .data = &ls1c_rtc_config },
        { .compatible = "loongson,ls7a-rtc", .data = &generic_rtc_config },
+       { .compatible = "loongson,ls2k0300-rtc", .data = &ls2k0300_rtc_config },
        { .compatible = "loongson,ls2k1000-rtc", .data = &ls2k1000_rtc_config },
        { /* sentinel */ }
 };
--
Quoted 3 lines

2.47.3

--
Thanks.
Binbin

maintainer
#7 lore raw

Re: [PATCH v1 2/2] rtc: loongson: Add Loongson-2K0300 support

Quoted 38 lines · Binbin Zhou

Hi:

Quoted 17 lines · Huacai Chen

Hi, Binbin,

Quoted 13 lines · Binbin Zhou

The Loongson-2K0300's rtc hardware design is similar to that of the Loongson-1B, but it does not support the alarm feature.

Introduce LOONGSON_RTC_ALARM_WORKAROUND, which indicates a chip that does not support the alarm function, and rewritethe related logic in loongson_rtc_alarm_setting()

---
 drivers/rtc/rtc-loongson.c | 65 ++++++++++++++++++++++++++------------
 1 file changed, 44 insertions(+), 21 deletions(-)
Quoted 11 lines

drivers/rtc/rtc-loongson.c line 70

Quoted 9 lines
drivers/rtc/rtc-loongson.c +1 -0
index 2ca7ffd5d7a9..6b076bd4e24e 100644
@@ -67,6 +67,7 @@
  * Accessing the relevant registers will cause the system to hang.
  */
 #define LS1C_RTC_CTRL_WORKAROUND       BIT(0)
+#define LOONGSON_RTC_ALARM_WORKAROUND  BIT(1)

Can we reuse existing logic, which means "alarm_irq = platform_get_irq(pdev, 0)" return 0 for LS2K0300?

First of all, as I understand it, platform_get_irq() doesn't normally return 0 unless interrupt 0 is configured in the DTS, which doesn't seem appropriate.

Quoted 5 lines

Also, the return value of platform_get_irq() was previously used to determine if the alarm feature was supported, which is not particularly accurate. It should be an inherent feature of the chip, and I think it would be more appropriate to define it in loongson_rtc_config.

OK, then I think it is better to rename LS1C_RTC_CTRL_WORKAROUND to LOONGSON_RTC_CTRL_WORKAROUND for consistency.

Huacai

Quoted 115 lines
Quoted 97 lines

Huacai

Quoted 94 lines
drivers/rtc/rtc-loongson.c +42 -21
 struct loongson_rtc_config {
        u32 pm_offset;  /* Offset of PM domain, for RTC alarm wakeup */
@@ -89,7 +90,7 @@ static const struct loongson_rtc_config ls1b_rtc_config = {
 
 static const struct loongson_rtc_config ls1c_rtc_config = {
        .pm_offset = 0,
-       .flags = LS1C_RTC_CTRL_WORKAROUND,
+       .flags = LS1C_RTC_CTRL_WORKAROUND | LOONGSON_RTC_ALARM_WORKAROUND,
 };
 
 static const struct loongson_rtc_config generic_rtc_config = {
@@ -97,6 +98,11 @@ static const struct loongson_rtc_config generic_rtc_config = {
        .flags = 0,
 };
 
+static const struct loongson_rtc_config ls2k0300_rtc_config = {
+       .pm_offset = 0x0,
+       .flags = LOONGSON_RTC_ALARM_WORKAROUND,
+};
+
 static const struct loongson_rtc_config ls2k1000_rtc_config = {
        .pm_offset = 0x800,
        .flags = 0,
@@ -299,9 +305,41 @@ static const struct rtc_class_ops loongson_rtc_ops = {
        .alarm_irq_enable = loongson_rtc_alarm_irq_enable,
 };
 
+static int loongson_rtc_alarm_setting(struct platform_device *pdev, void __iomem *regs)
+{
+       int ret = 0, alarm_irq;
+       struct device *dev = &pdev->dev;
+       struct loongson_rtc_priv *priv = dev_get_drvdata(dev);
+
+       if (priv->config->flags & LOONGSON_RTC_ALARM_WORKAROUND) {
+               /* Loongson-1C/Loongson-2K0300 RTC does not support alarm */
+               clear_bit(RTC_FEATURE_ALARM, priv->rtcdev->features);
+               return 0;
+       }
+
+       /* Get RTC alarm irq */
+       alarm_irq = platform_get_irq(pdev, 0);
+       if (alarm_irq < 0)
+               return alarm_irq;
+
+       ret = devm_request_irq(dev, alarm_irq, loongson_rtc_isr, 0, "loongson-alarm",
+                              priv);
+       if (ret < 0)
+               return ret;
+
+       priv->pm_base = regs - priv->config->pm_offset;
+       device_init_wakeup(dev, true);
+
+       if (has_acpi_companion(dev))
+               acpi_install_fixed_event_handler(ACPI_EVENT_RTC,
+                                                loongson_rtc_handler, priv);
+
+       return ret;
+}
+
 static int loongson_rtc_probe(struct platform_device *pdev)
 {
-       int ret, alarm_irq;
+       int ret;
        void __iomem *regs;
        struct loongson_rtc_priv *priv;
        struct device *dev = &pdev->dev;
@@ -330,25 +368,9 @@ static int loongson_rtc_probe(struct platform_device *pdev)
                return dev_err_probe(dev, PTR_ERR(priv->rtcdev),
                                     "devm_rtc_allocate_device failed\n");
 
-       /* Get RTC alarm irq */
-       alarm_irq = platform_get_irq(pdev, 0);
-       if (alarm_irq > 0) {
-               ret = devm_request_irq(dev, alarm_irq, loongson_rtc_isr,
-                                      0, "loongson-alarm", priv);
-               if (ret < 0)
-                       return dev_err_probe(dev, ret, "Unable to request irq %d\n",
-                                            alarm_irq);
-
-               priv->pm_base = regs - priv->config->pm_offset;
-               device_init_wakeup(dev, true);
-
-               if (has_acpi_companion(dev))
-                       acpi_install_fixed_event_handler(ACPI_EVENT_RTC,
-                                                        loongson_rtc_handler, priv);
-       } else {
-               /* Loongson-1C RTC does not support alarm */
-               clear_bit(RTC_FEATURE_ALARM, priv->rtcdev->features);
-       }
+       ret = loongson_rtc_alarm_setting(pdev, regs);
+       if (ret)
+               return ret;
 
Quoted 14 lines
Quoted 11 lines
drivers/rtc/rtc-loongson.c +1 -1
        /* Loongson RTC does not support UIE */
        clear_bit(RTC_FEATURE_UPDATE_INTERRUPT, priv->rtcdev->features);
@@ -379,6 +401,7 @@ static const struct of_device_id loongson_rtc_of_match[] = {
        { .compatible = "loongson,ls1b-rtc", .data = &ls1b_rtc_config },
        { .compatible = "loongson,ls1c-rtc", .data = &ls1c_rtc_config },
        { .compatible = "loongson,ls7a-rtc", .data = &generic_rtc_config },
+       { .compatible = "loongson,ls2k0300-rtc", .data = &ls2k0300_rtc_config },
        { .compatible = "loongson,ls2k1000-rtc", .data = &ls2k1000_rtc_config },
        { /* sentinel */ }
 };
--
Quoted 3 lines

2.47.3

--
Thanks.
Binbin

Reviewed-by Tested-by on the v1 series
#8 lore raw
In reply to Binbin Zhou

For the whole series:

Quoted 25 lines · Binbin Zhou

Hi all:

This patch set introduces the Loongson-2K0300 RTC, which has a similar hardware design to the Loongson-1B, but without the alarm feature.

To Keguang:
Would you have time to test the driver on a Loongson-1?

Thanks.
Binbin

Binbin Zhou (2):
dt-binding: rtc: loongson: Document Loongson-2K0300 compatible
rtc: loongson: Add Loongson-2K0300 support

 .../devicetree/bindings/rtc/loongson,rtc.yaml | 13 ++++
 drivers/rtc/rtc-loongson.c                    | 65 +++++++++++++------
 2 files changed, 57 insertions(+), 21 deletions(-)

base-commit: 16bd954c93360145bc77cc601e350913fc28182d --
2.47.3

Signature · 3 lines
Best regards,

Keguang Zhang