Skip to contentSkip to conversation

riscv: rtc: sophgo: add rtc support for CV1800

PATCH Superseded Alexander Sverdlin 4 participants 14 messages Feb 13, 2025 – Feb 17, 2025

Subsystems (4)

v11 of 15 Superseded Latest is v15 →

lore
Share & tools
lore
#0 lore raw

v11 cover riscv: rtc: sophgo: add rtc support for CV1800

Real Time Clock (RTC) is an independently powered module within the chip, which includes a 32KHz oscillator and a Power On Reset/POR submodule. It can be used for time display and timed alarm generation.

Changes since v10:
- only start RTC on set_time;
- add machine restart handler (as separate patch 3/3);

Changes since v9:
- picked up orphaned series;
- further simplified bitmask macros;
- unconditional RTC start (rtc_enable_sec_counter());
- dropped ANA_CALIB modification;
- successfully tested on SG2000;

v9: https://lore.kernel.org/linux-riscv/[email protected]/

Changes since v8:
- delete unused macros
- using 0/1 instead of the DISABLE/ENABLE macro
- pass in the correct pointer when applying for IRQ
- convert the incoming pointer into an appropriate structure pointer in the irq handler

v8: https://lore.kernel.org/all/[email protected]/

Changes since v7:
- pass checkpatch.pl --strict
- using u32 replace uint32
- using devm_kzalloc(*) replace devm_kzalloc(sizeof())
- sort header files alphabetically
- delete unnecessary header files
- fix wrap error
- drop dependent description
- using hardware automatic calibration replace software calibration. see documentation 197 page

v7: https://lore.kernel.org/all/[email protected]/
documentation: https://github.com/milkv-duo/duo-files/blob/main/duo/datasheet/CV1800B-CV1801B-Preliminary-Datasheet-full-en.pdf

Changes since v6:
- completely delete POR dt node
- remove syscon tag
- use devm_regmap_init_mmio() replace
syscon_node_to_regmap

v6: https://lore.kernel.org/all/[email protected]/

Changes since v5:
- remove unnecessary lock
- fix cv1800_rtc_alarm_irq_enable()
- remove duplicate checks
- using alrm->enabled instead of unconditionally enabling
- remove disable alarms on probe
- using rtc_update_irq() replace mess of alarm
- remove leak clk
- useing devm_rtc_allocate_device() and devm_rtc_register_device() instead old way
- add judgment for rtc_enable_sec_counter()
- add POR nodes in DTS. This POR device shares the register region with the RTC device

v5: https://lore.kernel.org/all/[email protected]/

Changes since v4:
- remove POR dt-bindings because it empty
- remove MFD dt-bindings because SoC does
not have MFDs
- add syscon attribute to share registers
with POR

v4: https://lore.kernel.org/all/[email protected]/

Changes since v3:
- temporarily not submitting RTC driver code waiting for communication with IC designer
- add MFD dt-bindings
- add POR dt-bindings

v3: https://lore.kernel.org/all/[email protected]/

Changes since v2:
- add mfd support for CV1800
- add rtc to mfd
- using regmap replace iomap
- merge register address in dts

v2: https://lore.kernel.org/lkml/[email protected]/

Changes since v1
- fix duplicate names in subject
- using RTC replace RTC controller
- improve the properties of dt-bindings
- using unevaluatedProperties replace additionalProperties
- dt-bindings passed the test
- using devm_platform_ioremap_resource() replace platform_get_resource() and devm_ioremap_resource()
- fix random order of the code
- fix wrong wrapping of the devm_request_irq() and map the flag with dts
- using devm_clk_get_enabled replace devm_clk_get() and clk_prepare_enable()
- fix return style
- add rtc clock calibration function
- use spinlock when write register on read/set time

v1: https://lore.kernel.org/lkml/[email protected]/

Alexander Sverdlin (1):
rtc: sophgo: cv18xx: Add restart handler

Jingbao Qiu (2):
dt-bindings: rtc: sophgo: add RTC support for Sophgo CV1800 series SoC
rtc: sophgo: add rtc support for Sophgo CV1800 SoC

 .../bindings/rtc/sophgo,cv1800-rtc.yaml       |  53 ++++
 drivers/rtc/Kconfig                           |  10 +
 drivers/rtc/Makefile                          |   1 +
 drivers/rtc/rtc-cv1800.c                      | 277 ++++++++++++++++++
 4 files changed, 341 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml
 create mode 100644 drivers/rtc/rtc-cv1800.c
Signature · 1 line
2.48.1
#1 lore raw

PATCH v11 1/3 dt-bindings: rtc: sophgo: add RTC support for Sophgo CV1800 series SoC

From: Jingbao Qiu <[email protected]>

Add RTC devicetree binding for Sophgo CV1800 SoC.

---
 .../bindings/rtc/sophgo,cv1800-rtc.yaml       | 53 +++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml
Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml new file +53 -0
new file mode 100644
index 000000000000..b36b51a69166
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/rtc/sophgo,cv1800-rtc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Real Time Clock of the Sophgo CV1800 SoC
+
+description:
+  Real Time Clock (RTC) is an independently powered module
+  within the chip, which includes a 32KHz oscillator and a
+  Power On Reset/POR submodule. It can be used for time display
+  and timed alarm generation. In addition, the hardware state
+  machine provides triggering and timing control for chip
+  power on, off, and reset.
+
+maintainers:
+  - Jingbao Qiu <[email protected]>
+
+allOf:
+  - $ref: rtc.yaml#
+
+properties:
+  compatible:
+    const: sophgo,cv1800-rtc
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    rtc@5025000 {
+        compatible = "sophgo,cv1800-rtc";
+        reg = <0x5025000 0x2000>;
+        interrupts = <17 IRQ_TYPE_LEVEL_HIGH>;
+        clocks = <&osc>;
+    };
Signature · 1 line
2.48.1
maintainer Reviewed-by on v11 1/3
#2 lore raw

Re: [PATCH v11 1/3] dt-bindings: rtc: sophgo: add RTC support for Sophgo CV1800 series SoC

Quoted 34 lines · Alexander Sverdlin

From: Jingbao Qiu <[email protected]>

Add RTC devicetree binding for Sophgo CV1800 SoC.

---
 .../bindings/rtc/sophgo,cv1800-rtc.yaml       | 53 +++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml
Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml new file +16 -0
new file mode 100644
index 000000000000..b36b51a69166
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/rtc/sophgo,cv1800-rtc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Real Time Clock of the Sophgo CV1800 SoC
+
+description:
+  Real Time Clock (RTC) is an independently powered module
+  within the chip, which includes a 32KHz oscillator and a
+  Power On Reset/POR submodule. It can be used for time display
+  and timed alarm generation. In addition, the hardware state
+  machine provides triggering and timing control for chip
+  power on, off, and reset.
+

Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml line 25

Quoted 10 lines
Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml new file +9 -0
+maintainers:
+  - Jingbao Qiu <[email protected]>
+
+allOf:
+  - $ref: rtc.yaml#
+
 
+properties:
+  compatible:
+    const: sophgo,cv1800-rtc

Now I prefer cv1800b to cv1800, using wildcard to provide base compatible is not a good idea. This old design should be changed.

Otherwise, it looks good to me. With the compatible change:

Regards,
Inochi

maintainer
#3 lore raw

Re: [PATCH v11 1/3] dt-bindings: rtc: sophgo: add RTC support for Sophgo CV1800 series SoC

In reply to Alexander Sverdlin
Quoted 63 lines · Alexander Sverdlin

From: Jingbao Qiu <[email protected]>

Add RTC devicetree binding for Sophgo CV1800 SoC.

---
 .../bindings/rtc/sophgo,cv1800-rtc.yaml       | 53 +++++++++++++++++++
 1 file changed, 53 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml
Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml new file +43 -0
new file mode 100644
index 000000000000..b36b51a69166
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/rtc/sophgo,cv1800-rtc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Real Time Clock of the Sophgo CV1800 SoC
+
+description:
+  Real Time Clock (RTC) is an independently powered module
+  within the chip, which includes a 32KHz oscillator and a
+  Power On Reset/POR submodule. It can be used for time display
+  and timed alarm generation. In addition, the hardware state
+  machine provides triggering and timing control for chip
+  power on, off, and reset.
+
+maintainers:
+  - Jingbao Qiu <[email protected]>
+
+allOf:
+  - $ref: rtc.yaml#
+
+properties:
+  compatible:
+    const: sophgo,cv1800-rtc
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+
+unevaluatedProperties: false
+
 

Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml line 53

Quoted 10 lines
Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml new file +10 -0
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    rtc@5025000 {
+        compatible = "sophgo,cv1800-rtc";
+        reg = <0x5025000 0x2000>;
+        interrupts = <17 IRQ_TYPE_LEVEL_HIGH>;
+        clocks = <&osc>;
+    };

Just for curiosity, Do you leave a way to implement the 8051 subsys, since its registers are in rtc. (You can check the chapter "8051 subsystem" of the SG2002, which can be found at https://github.com/sophgo/sophgo-doc).

Regards,
Inochi

#4 lore raw

Re: [PATCH v11 1/3] dt-bindings: rtc: sophgo: add RTC support for Sophgo CV1800 series SoC

Hi Inochi,

Quoted 8 lines · Inochi Amaoto
Quoted 8 lines
Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml new file +8 -0
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    rtc@5025000 {
+        compatible = "sophgo,cv1800-rtc";
+        reg = <0x5025000 0x2000>;
+        interrupts = <17 IRQ_TYPE_LEVEL_HIGH>;
Quoted 8 lines
Quoted 3 lines

+        clocks = <&osc>;
+    };

Just for curiosity, Do you leave a way to implement the 8051 subsys, since its registers are in rtc. (You can check the chapter "8051 subsystem" of the SG2002, which can be found at https://github.com/sophgo/sophgo-doc).

well, I suppose, you know the answer, according to how this has been modelled within this series, all the functionality could only be added on top of RTC driver. Do you think it's time to re-design it as compatible = "syscon", "simple-mfd" with children nodes for RTC and reboot?

Signature · 1 line
Alexander Sverdlin.
maintainer
#5 lore raw

Re: [PATCH v11 1/3] dt-bindings: rtc: sophgo: add RTC support for Sophgo CV1800 series SoC

Quoted 20 lines · Alexander Sverdlin

Hi Inochi,

Quoted 8 lines · Inochi Amaoto
Quoted 8 lines
Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml new file +8 -0
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    rtc@5025000 {
+        compatible = "sophgo,cv1800-rtc";
+        reg = <0x5025000 0x2000>;
+        interrupts = <17 IRQ_TYPE_LEVEL_HIGH>;
Quoted 8 lines
Quoted 3 lines

+        clocks = <&osc>;
+    };

Just for curiosity, Do you leave a way to implement the 8051 subsys, since its registers are in rtc. (You can check the chapter "8051 subsystem" of the SG2002, which can be found at https://github.com/sophgo/sophgo-doc).

Quoted 3 lines

well, I suppose, you know the answer, according to how this has been modelled within this series, all the functionality could only be added on top of RTC driver.

I don't think it is a good idea to add everything in the RTC driver. I prefer to separate them to sub devices if possible, so we can make full use of all the framework provided.

Quoted 3 lines

Do you think it's time to re-design it as compatible = "syscon", "simple-mfd" with children nodes for RTC and reboot?

Yes, but you should submit the 8051 and reboot device early, and change your binding with the right compatible. It is not allowd to change compatible after the binding is determined.

Regards,
Inochi

#6 lore raw

Re: [PATCH v11 1/3] dt-bindings: rtc: sophgo: add RTC support for Sophgo CV1800 series SoC

In reply to Alexander Sverdlin
Quoted 26 lines · Alexander Sverdlin

From: Jingbao Qiu <[email protected]>

Add RTC devicetree binding for Sophgo CV1800 SoC.

---
  .../bindings/rtc/sophgo,cv1800-rtc.yaml       | 53 +++++++++++++++++++
  1 file changed, 53 insertions(+)

create mode 100644 Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml

Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml new file +8 -0
new file mode 100644
index 000000000000..b36b51a69166
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/rtc/sophgo,cv1800-rtc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Real Time Clock of the Sophgo CV1800 SoC
+

Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml line 18

Quoted 10 lines
Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml new file +10 -0
+description:
+  Real Time Clock (RTC) is an independently powered module
+  within the chip, which includes a 32KHz oscillator and a
+  Power On Reset/POR submodule. It can be used for time display
+  and timed alarm generation. In addition, the hardware state
+  machine provides triggering and timing control for chip
+  power on, off, and reset.
+
+maintainers:
+  - Jingbao Qiu <[email protected]>

I guess Jingbao will not take this role. If he doesn't raise any objections, please just change this.

Jingbao, do you have any comment on this?

[......]

#7 lore raw

Re: [PATCH v11 1/3] dt-bindings: rtc: sophgo: add RTC support for Sophgo CV1800 series SoC

Hi Chen!

Quoted 38 lines · Chen Wang
Quoted 24 lines

Add RTC devicetree binding for Sophgo CV1800 SoC.

---
   .../bindings/rtc/sophgo,cv1800-rtc.yaml       | 53 +++++++++++++++++++

   1 file changed, 53 insertions(+)

   create mode 100644 Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml

Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml new file +8 -0
new file mode 100644
index 000000000000..b36b51a69166
@@ -0,0 +1,53 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/rtc/sophgo,cv1800-rtc.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Real Time Clock of the Sophgo CV1800 SoC
+

Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml line 18

Quoted 10 lines
Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml new file +10 -0
+description:
+  Real Time Clock (RTC) is an independently powered module
+  within the chip, which includes a 32KHz oscillator and a
+  Power On Reset/POR submodule. It can be used for time display
+  and timed alarm generation. In addition, the hardware state
+  machine provides triggering and timing control for chip
+  power on, off, and reset.
+
+maintainers:
+  - Jingbao Qiu <[email protected]>

I guess Jingbao will not take this role. If he doesn't raise any objections, please just change this.

Quoted 1 line

Jingbao, do you have any comment on this?

New version will look rather like this:
https://lore.kernel.org/linux-devicetree/[email protected]/

But I'll be happy to take you suggestion and replace my name in the new version with someone more afiliated with Sophgo ;-)

Signature · 1 line
Alexander Sverdlin.
#8 lore raw

PATCH v11 2/3 rtc: sophgo: add rtc support for Sophgo CV1800 SoC

In reply to Alexander Sverdlin

From: Jingbao Qiu <[email protected]>

Implement the RTC driver for CV1800, which able to provide time alarm.

---
Changes since v10:
- only start RTC on set_time;
Changes since v9:
- further simplified bitmask macros;
- unconditional RTC start (rtc_enable_sec_counter()), otherwise didn't start on SG2000;
- dropped ANA_CALIB modification (has been forgotten in v8 with the drop of SW calibration to switch to HW calibration);
- successfully tested on SG2000;

 drivers/rtc/Kconfig      |  10 ++
 drivers/rtc/Makefile     |   1 +
 drivers/rtc/rtc-cv1800.c | 228 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 239 insertions(+)
 create mode 100644 drivers/rtc/rtc-cv1800.c
drivers/rtc/Kconfig +10 -0
index 0bbbf778ecfa..019622db3e93 100644
@@ -1159,6 +1159,16 @@ config RTC_DRV_DS2404
 	  This driver can also be built as a module. If so, the module
 	  will be called rtc-ds2404.
 
+config RTC_DRV_CV1800
+	tristate "Sophgo CV1800 RTC"
+	depends on ARCH_SOPHGO || COMPILE_TEST
+	help
+	  If you say yes here you get support the RTC driver
+	  for Sophgo CV1800 chip.
+
+	  This driver can also be built as a module. If so, the
+	  module will be called rtc-cv1800.
+
 config RTC_DRV_DA9052
 	tristate "Dialog DA9052/DA9053 RTC"
 	depends on PMIC_DA9052
drivers/rtc/Makefile +1 -0
index 489b4ab07068..621b30a33dda 100644
@@ -44,6 +44,7 @@ obj-$(CONFIG_RTC_DRV_CADENCE)	+= rtc-cadence.o
 obj-$(CONFIG_RTC_DRV_CMOS)	+= rtc-cmos.o
 obj-$(CONFIG_RTC_DRV_CPCAP)	+= rtc-cpcap.o
 obj-$(CONFIG_RTC_DRV_CROS_EC)	+= rtc-cros-ec.o
+obj-$(CONFIG_RTC_DRV_CV1800)	+= rtc-cv1800.o
 obj-$(CONFIG_RTC_DRV_DA9052)	+= rtc-da9052.o
 obj-$(CONFIG_RTC_DRV_DA9055)	+= rtc-da9055.o
 obj-$(CONFIG_RTC_DRV_DA9063)	+= rtc-da9063.o
drivers/rtc/rtc-cv1800.c new file +228 -0

228 lines not on this page · raw message

Signature · 1 line
2.48.1
#9 lore raw

PATCH v11 3/3 rtc: sophgo: cv18xx: Add restart handler

In reply to Alexander Sverdlin

Currently provided ATF firmware for ARM64 Sophgo SoCs is binary-only. To make Linux independent from this binary firmware, provide restart handler triggering COLD or WARM reboot directly via RTC HW block as alternative to PSCI interface.

Handler priority SYS_OFF_PRIO_DEFAULT of 0 is desired, because if PSCI is indeed present, its priority of 129 will override the new one. On RiskV side the behaviour is unchanged as well because SBI SRST handler has higher priority as well (192).

Tested on SG2000 in ARM64 mode.

---
v11:
- patch introduced;

 drivers/rtc/rtc-cv1800.c | 51 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
drivers/rtc/rtc-cv1800.c +50 -1
index 5c30f7388983..586d7195f1f4 100644
@@ -9,17 +9,23 @@
 #include <linux/irq.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/notifier.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
+#include <linux/reboot.h>
 #include <linux/regmap.h>
 #include <linux/rtc.h>
 
+#define CTRL_UNLOCKKEY         0x0004
+#define CTRL0                  0x0008
 #define SEC_PULSE_GEN          0x1004
 #define ALARM_TIME             0x1008
 #define ALARM_ENABLE           0x100C
 #define SET_SEC_CNTR_VAL       0x1010
 #define SET_SEC_CNTR_TRIG      0x1014
 #define SEC_CNTR_VAL           0x1018
+#define EN_PWR_CYC_REQ         0x10C8
+#define EN_WARM_RST_REQ        0x10CC
 
 /*
  * When in VDDBKUP domain, this MACRO register
@@ -28,6 +34,10 @@
 #define MACRO_RO_T             0x14A8
 #define MACRO_RG_SET_T         0x1498
 
+/* CTRL0 bits */
+#define REQ_PWR_CYC            BIT(3)
+#define REQ_WARM_RST           BIT(4)
+
 #define ALARM_ENABLE_MASK      BIT(0)
 #define SEL_SEC_PULSE          BIT(31)
 
@@ -160,6 +170,29 @@ static const struct rtc_class_ops cv1800_rtc_ops = {
 	.alarm_irq_enable = cv1800_rtc_alarm_irq_enable,
 };
 
+static int cv1800_restart_handler(struct sys_off_data *data)
+{
+	struct cv1800_rtc_priv *info = data->cb_data;
+	u32 reg_en = EN_WARM_RST_REQ;
+	u32 request = 0xFFFF0800;
+
+	if (data->mode == REBOOT_COLD) {
+		reg_en = EN_PWR_CYC_REQ;
+		request |= REQ_PWR_CYC;
+	} else {
+		request |= REQ_WARM_RST;
+	}
+
+	/* Enable reset request */
+	regmap_write(info->rtc_map, reg_en, 1);
+	/* Enable CTRL0 register access */
+	regmap_write(info->rtc_map, CTRL_UNLOCKKEY, 0xAB18);
+	/* Request reset */
+	regmap_write(info->rtc_map, CTRL0, request);
+
+	return NOTIFY_DONE;
+}
+
 static int cv1800_rtc_probe(struct platform_device *pdev)
 {
 	struct cv1800_rtc_priv *rtc;
@@ -205,7 +238,23 @@ static int cv1800_rtc_probe(struct platform_device *pdev)
 		return dev_err_probe(&pdev->dev, ret,
 				     "cannot register interrupt handler\n");
 
-	return devm_rtc_register_device(rtc->rtc_dev);
+	ret = devm_rtc_register_device(rtc->rtc_dev);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret, "cannot register RTC\n");
+
+	/*
+	 * SYS_OFF_PRIO_DEFAULT of 0 is desired here because both RiscV SBI SRST
+	 * (prio 192) and ARM PSCI (prio 129) will override this one; this
+	 * handler shall serve as a fallback in cases where firmware is not
+	 * present.
+	 */
+	ret = devm_register_restart_handler(&pdev->dev, cv1800_restart_handler,
+					    rtc);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret,
+				     "cannot register restart handler\n");
+
+	return 0;
 }
 
 static const struct of_device_id cv1800_dt_ids[] = {
Signature · 1 line
2.48.1
#10 lore raw

Re: [PATCH v11 3/3] rtc: sophgo: cv18xx: Add restart handler

Quoted 90 lines · Alexander Sverdlin

Currently provided ATF firmware for ARM64 Sophgo SoCs is binary-only. To make Linux independent from this binary firmware, provide restart handler triggering COLD or WARM reboot directly via RTC HW block as alternative to PSCI interface.

Handler priority SYS_OFF_PRIO_DEFAULT of 0 is desired, because if PSCI is indeed present, its priority of 129 will override the new one. On RiskV side the behaviour is unchanged as well because SBI SRST handler has higher priority as well (192).

Tested on SG2000 in ARM64 mode.

---
v11:
- patch introduced;

 drivers/rtc/rtc-cv1800.c | 51 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)
drivers/rtc/rtc-cv1800.c +33 -0
index 5c30f7388983..586d7195f1f4 100644
@@ -9,17 +9,23 @@
 #include <linux/irq.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/notifier.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
+#include <linux/reboot.h>
 #include <linux/regmap.h>
 #include <linux/rtc.h>
 
+#define CTRL_UNLOCKKEY         0x0004
+#define CTRL0                  0x0008
 #define SEC_PULSE_GEN          0x1004
 #define ALARM_TIME             0x1008
 #define ALARM_ENABLE           0x100C
 #define SET_SEC_CNTR_VAL       0x1010
 #define SET_SEC_CNTR_TRIG      0x1014
 #define SEC_CNTR_VAL           0x1018
+#define EN_PWR_CYC_REQ         0x10C8
+#define EN_WARM_RST_REQ        0x10CC
 
 /*
  * When in VDDBKUP domain, this MACRO register
@@ -28,6 +34,10 @@
 #define MACRO_RO_T             0x14A8
 #define MACRO_RG_SET_T         0x1498
 
+/* CTRL0 bits */
+#define REQ_PWR_CYC            BIT(3)
+#define REQ_WARM_RST           BIT(4)
+
 #define ALARM_ENABLE_MASK      BIT(0)
 #define SEL_SEC_PULSE          BIT(31)
 
@@ -160,6 +170,29 @@ static const struct rtc_class_ops cv1800_rtc_ops = {
 	.alarm_irq_enable = cv1800_rtc_alarm_irq_enable,
 };
 
+static int cv1800_restart_handler(struct sys_off_data *data)
+{
+	struct cv1800_rtc_priv *info = data->cb_data;
+	u32 reg_en = EN_WARM_RST_REQ;
+	u32 request = 0xFFFF0800;
+
+	if (data->mode == REBOOT_COLD) {
+		reg_en = EN_PWR_CYC_REQ;
+		request |= REQ_PWR_CYC;
+	} else {
+		request |= REQ_WARM_RST;
+	}
+
+	/* Enable reset request */
+	regmap_write(info->rtc_map, reg_en, 1);
+	/* Enable CTRL0 register access */
+	regmap_write(info->rtc_map, CTRL_UNLOCKKEY, 0xAB18);
+	/* Request reset */
+	regmap_write(info->rtc_map, CTRL0, request);
+
+	return NOTIFY_DONE;
+}
+
 static int cv1800_rtc_probe(struct platform_device *pdev)
 {
 	struct cv1800_rtc_priv *rtc;

drivers/rtc/rtc-cv1800.c static int cv1800_rtc_probe(struct platform_device *pdev) · line 243

Quoted 7 lines
drivers/rtc/rtc-cv1800.c +3 -1
@@ -205,7 +238,23 @@ static int cv1800_rtc_probe(struct platform_device *pdev)
 		return dev_err_probe(&pdev->dev, ret,
 				     "cannot register interrupt handler\n");
 
-	return devm_rtc_register_device(rtc->rtc_dev);
+	ret = devm_rtc_register_device(rtc->rtc_dev);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret, "cannot register RTC\n");

This message is useless, there is no failure path without a message in devm_rtc_register_device.

drivers/rtc/rtc-cv1800.c static int cv1800_rtc_probe(struct platform_device *pdev) · line 255

Quoted 12 lines
drivers/rtc/rtc-cv1800.c +12 -0
+
+	/*
+	 * SYS_OFF_PRIO_DEFAULT of 0 is desired here because both RiscV SBI SRST
+	 * (prio 192) and ARM PSCI (prio 129) will override this one; this
+	 * handler shall serve as a fallback in cases where firmware is not
+	 * present.
+	 */
+	ret = devm_register_restart_handler(&pdev->dev, cv1800_restart_handler,
+					    rtc);
+	if (ret)
+		return dev_err_probe(&pdev->dev, ret,
+				     "cannot register restart handler\n");

Probe must never fail after devm_rtc_register_device.

Quoted 8 lines
drivers/rtc/rtc-cv1800.c +2 -0
+
+	return 0;
 }
 
 static const struct of_device_id cv1800_dt_ids[] = {
Signature · 1 line
2.48.1
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#11 lore raw
In reply to Alexander Sverdlin

Hello, Alexander,

First of all, thank you for re-picking up this orphan patchset.

We recently created a mailing list for sophgo ([email protected]), and we would appreciate it if you send a copy of any sophgo-related patches to the mailist in the future.

You can see https://github.com/sophgo/linux/wiki for more details about this mailist, and we are keeping track of the status of upstreaming sophgo products through this wiki. We have updated the status of some of the patches you are working on, please help double check.

Thanks,

Chen

Quoted 125 lines · Alexander Sverdlin

Real Time Clock (RTC) is an independently powered module within the chip, which includes a 32KHz oscillator and a Power On Reset/POR submodule. It can be used for time display and timed alarm generation.

Changes since v10:
- only start RTC on set_time;
- add machine restart handler (as separate patch 3/3);

Changes since v9:
- picked up orphaned series;
- further simplified bitmask macros;
- unconditional RTC start (rtc_enable_sec_counter());
- dropped ANA_CALIB modification;
- successfully tested on SG2000;

v9: https://lore.kernel.org/linux-riscv/[email protected]/

Changes since v8:
- delete unused macros
- using 0/1 instead of the DISABLE/ENABLE macro
- pass in the correct pointer when applying for IRQ
- convert the incoming pointer into an appropriate structure pointer in the irq handler

v8: https://lore.kernel.org/all/[email protected]/

Changes since v7:
- pass checkpatch.pl --strict
- using u32 replace uint32
- using devm_kzalloc(*) replace devm_kzalloc(sizeof())
- sort header files alphabetically
- delete unnecessary header files
- fix wrap error
- drop dependent description
- using hardware automatic calibration replace software calibration. see documentation 197 page

v7: https://lore.kernel.org/all/[email protected]/
documentation: https://github.com/milkv-duo/duo-files/blob/main/duo/datasheet/CV1800B-CV1801B-Preliminary-Datasheet-full-en.pdf

Changes since v6:
- completely delete POR dt node
- remove syscon tag
- use devm_regmap_init_mmio() replace
syscon_node_to_regmap

v6: https://lore.kernel.org/all/[email protected]/

Changes since v5:
- remove unnecessary lock
- fix cv1800_rtc_alarm_irq_enable()
- remove duplicate checks
- using alrm->enabled instead of unconditionally enabling
- remove disable alarms on probe
- using rtc_update_irq() replace mess of alarm
- remove leak clk
- useing devm_rtc_allocate_device() and devm_rtc_register_device() instead old way
- add judgment for rtc_enable_sec_counter()
- add POR nodes in DTS. This POR device shares the register region with the RTC device

v5: https://lore.kernel.org/all/[email protected]/

Changes since v4:
- remove POR dt-bindings because it empty
- remove MFD dt-bindings because SoC does
not have MFDs
- add syscon attribute to share registers
with POR

v4: https://lore.kernel.org/all/[email protected]/

Changes since v3:
- temporarily not submitting RTC driver code waiting for communication with IC designer
- add MFD dt-bindings
- add POR dt-bindings

v3: https://lore.kernel.org/all/[email protected]/

Changes since v2:
- add mfd support for CV1800
- add rtc to mfd
- using regmap replace iomap
- merge register address in dts

v2: https://lore.kernel.org/lkml/[email protected]/

Changes since v1
- fix duplicate names in subject
- using RTC replace RTC controller
- improve the properties of dt-bindings
- using unevaluatedProperties replace additionalProperties
- dt-bindings passed the test
- using devm_platform_ioremap_resource() replace platform_get_resource() and devm_ioremap_resource()
- fix random order of the code
- fix wrong wrapping of the devm_request_irq() and map the flag with dts
- using devm_clk_get_enabled replace devm_clk_get() and clk_prepare_enable()
- fix return style
- add rtc clock calibration function
- use spinlock when write register on read/set time

v1: https://lore.kernel.org/lkml/[email protected]/

Alexander Sverdlin (1):
rtc: sophgo: cv18xx: Add restart handler

Jingbao Qiu (2):
dt-bindings: rtc: sophgo: add RTC support for Sophgo CV1800 series SoC
rtc: sophgo: add rtc support for Sophgo CV1800 SoC

  .../bindings/rtc/sophgo,cv1800-rtc.yaml       |  53 ++++
  drivers/rtc/Kconfig                           |  10 +
  drivers/rtc/Makefile                          |   1 +
  drivers/rtc/rtc-cv1800.c                      | 277 ++++++++++++++++++
  4 files changed, 341 insertions(+)

create mode 100644 Documentation/devicetree/bindings/rtc/sophgo,cv1800-rtc.yaml create mode 100644 drivers/rtc/rtc-cv1800.c

#12 lore raw

Hi Chen!

Quoted 5 lines · Chen Wang

First of all, thank you for re-picking up this orphan patchset.

We recently created a mailing list for sophgo ([email protected]), and we would appreciate it if you send a copy of any sophgo-related patches to the mailist in the future.

Does it make sense to add the address to the MAINTAINERS file? Or has a corresponding patch been already posted?

Quoted 4 lines

You can see https://github.com/sophgo/linux/wiki for more details about this mailist, and we are keeping track of the status of upstreaming sophgo products through this wiki. We have updated the status of some of the patches you are working on, please help double check.

Looks good to me, thanks!

Signature · 1 line
Alexander Sverdlin.
#13 lore raw
Quoted 10 lines · Alexander Sverdlin

Hi Chen!

Quoted 5 lines · Chen Wang

First of all, thank you for re-picking up this orphan patchset.

We recently created a mailing list for sophgo ([email protected]), and we would appreciate it if you send a copy of any sophgo-related patches to the mailist in the future.

Does it make sense to add the address to the MAINTAINERS file? Or has a corresponding patch been already posted?

Yes, see https://lore.kernel.org/linux-kernel/[email protected]/

Quoted 6 lines
Quoted 4 lines

You can see https://github.com/sophgo/linux/wiki for more details about this mailist, and we are keeping track of the status of upstreaming sophgo products through this wiki. We have updated the status of some of the patches you are working on, please help double check.

Looks good to me, thanks!