Skip to contentSkip to conversation

Add reboot support for SpacemiT K1 platforms

PATCH Applied Aurelien Jarno 3 participants 7 messages Apr 19 – May 11 RISC-V SpacemiT SoC Support

lore
Share & tools
lore
#0 lore raw

cover Add reboot support for SpacemiT K1 platforms

This add reboot support for SpacemiT K1 platforms that uses the SpacemiT P1 PMIC, which is TTBOMK all of them. Note that the Linux kernel also has a driver for that [1], but not yet enabled [2]. However it has been suggested that it's better to implement reboot support at the OpenSBI level [3].

The first patch add I2C support for the SpacemiT P1 SoC, and the second one reboot support for the SpacemiT P1 PMIC. This chip is also used on some SpacemiT K3 platforms, that's why I decided to add it at the PMIC level (lib/utils/reset/fdt_reset_spacemit_p1.c) instead of at the platform level (platform/generic/spacemit/k1.c).

This patchset has been tested on a Banana Pi BPI-F3 board and on a Milk-V Jupiter board.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/power/reset/spacemit-p1-reboot.c
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/

Aurelien Jarno (2):
lib: utils/i2c: add minimal SpacemiT I2C driver
lib: utils/reset: add SpacemiT P1 PMIC support

 lib/utils/i2c/Kconfig                   |   4 +
 lib/utils/i2c/fdt_i2c_spacemit.c        | 220 ++++++++++++++++++++++++
 lib/utils/i2c/objects.mk                |   3 +
 lib/utils/reset/Kconfig                 |   4 +
 lib/utils/reset/fdt_reset_spacemit_p1.c | 112 ++++++++++++
 lib/utils/reset/objects.mk              |   3 +
 platform/generic/configs/defconfig      |   2 +
 7 files changed, 348 insertions(+)
 create mode 100644 lib/utils/i2c/fdt_i2c_spacemit.c
 create mode 100644 lib/utils/reset/fdt_reset_spacemit_p1.c
Signature · 7 lines
2.53.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#1 lore raw

PATCH 1/2 lib: utils/i2c: add minimal SpacemiT I2C driver

Add a simple SpacemiT I2C driver for basic byte transfers over the I2C bus, prioritizing simplicity over performance. The driver operates in PIO mode and does not use interrupts, FIFO, or DMA.

The controller is reset at the start of each transaction to ensure a known initial state, regardless of prior configuration by the kernel. This also avoids the need for additional error recovery code.

This will be used for communication with onboard PMIC to reset and power-off the board.

---
 lib/utils/i2c/Kconfig              |   4 +
 lib/utils/i2c/fdt_i2c_spacemit.c   | 220 +++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk           |   3 +
 platform/generic/configs/defconfig |   1 +
 4 files changed, 228 insertions(+)
 create mode 100644 lib/utils/i2c/fdt_i2c_spacemit.c
lib/utils/i2c/Kconfig +4 -0
index 7fa32fcf..bdaaff62 100644
@@ -14,6 +14,10 @@ config FDT_I2C_SIFIVE
 	bool "SiFive I2C FDT driver"
 	default n
 
+config FDT_I2C_SPACEMIT
+	bool "SpacemiT I2C FDT driver"
+	default n
+
 config FDT_I2C_DW
 	bool "Synopsys Designware I2C FDT driver"
 	select I2C_DW
lib/utils/i2c/fdt_i2c_spacemit.c new file +220 -0

220 lines not on this page · raw message

lib/utils/i2c/objects.mk +3 -0
index d34d6648..91ac17ec 100644
@@ -15,6 +15,9 @@ libsbiutils-objs-$(CONFIG_FDT_I2C) += i2c/fdt_i2c_adapter_drivers.carray.o
 carray-fdt_i2c_adapter_drivers-$(CONFIG_FDT_I2C_SIFIVE) += fdt_i2c_adapter_sifive
 libsbiutils-objs-$(CONFIG_FDT_I2C_SIFIVE) += i2c/fdt_i2c_sifive.o
 
+carray-fdt_i2c_adapter_drivers-$(CONFIG_FDT_I2C_SPACEMIT) += fdt_i2c_adapter_spacemit
+libsbiutils-objs-$(CONFIG_FDT_I2C_SPACEMIT) += i2c/fdt_i2c_spacemit.o
+
 carray-fdt_i2c_adapter_drivers-$(CONFIG_FDT_I2C_DW) += fdt_i2c_adapter_dw
 libsbiutils-objs-$(CONFIG_FDT_I2C_DW) += i2c/fdt_i2c_dw.o
 
platform/generic/configs/defconfig +1 -0
index 1d3431e7..a9cb0f06 100644
@@ -31,6 +31,7 @@ CONFIG_FDT_HSM_RPMI=y
 CONFIG_FDT_HSM_SIFIVE_TMC0=y
 CONFIG_FDT_I2C=y
 CONFIG_FDT_I2C_SIFIVE=y
+CONFIG_FDT_I2C_SPACEMIT=y
 CONFIG_FDT_I2C_DW=y
 CONFIG_FDT_IPI=y
 CONFIG_FDT_IPI_MSWI=y
Signature · 7 lines
2.53.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Tested-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] lib: utils/i2c: add minimal SpacemiT I2C driver

Hi Aurelien,

Quoted 5 lines · Aurelien Jarno

Add a simple SpacemiT I2C driver for basic byte transfers over the I2C bus, prioritizing simplicity over performance. The driver operates in PIO mode and does not use interrupts, FIFO, or DMA.

Quoted 8 lines

The controller is reset at the start of each transaction to ensure a known initial state, regardless of prior configuration by the kernel. This also avoids the need for additional error recovery code.

This will be used for communication with onboard PMIC to reset and power-off the board.

Thanks
-Anand

Quoted 283 lines
---
 lib/utils/i2c/Kconfig              |   4 +
 lib/utils/i2c/fdt_i2c_spacemit.c   | 220 +++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk           |   3 +
 platform/generic/configs/defconfig |   1 +
 4 files changed, 228 insertions(+)
 create mode 100644 lib/utils/i2c/fdt_i2c_spacemit.c
lib/utils/i2c/Kconfig +4 -0
index 7fa32fcf..bdaaff62 100644
@@ -14,6 +14,10 @@ config FDT_I2C_SIFIVE
        bool "SiFive I2C FDT driver"
        default n
 
+config FDT_I2C_SPACEMIT
+       bool "SpacemiT I2C FDT driver"
+       default n
+
 config FDT_I2C_DW
        bool "Synopsys Designware I2C FDT driver"
        select I2C_DW
lib/utils/i2c/fdt_i2c_spacemit.c new file +220 -0
new file mode 100644
index 00000000..9009b6b6
@@ -0,0 +1,220 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Aurelien Jarno
+ *
+ * Authors:
+ *   Aurelien Jarno <[email protected]>
+ */
+
+#include <sbi/riscv_io.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_heap.h>
+#include <sbi/sbi_timer.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/i2c/fdt_i2c.h>
+
+/* Controller registers */
+#define ICR_OFFSET     0x00            /* I2C control register */
+#define IDBR_OFFSET    0x0c            /* I2C data buffer register */
+
+/* Control register bits */
+#define ICR_START      BIT(0)          /* start */
+#define ICR_STOP       BIT(1)          /* stop */
+#define ICR_ACKNAK     BIT(2)          /* ACK(0) or NAK(1) */
+#define ICR_TB         BIT(3)          /* transfer byte */
+#define ICR_UR         BIT(10)         /* unit reset */
+#define ICR_SCLE       BIT(13)         /* SCL enable  */
+#define ICR_IUE                BIT(14)         /* unit enable */
+
+/* Timing */
+#define I2C_RESET_US   10
+#define I2C_TIMEOUT_US 1000
+
+struct spacemit_i2c_adapter {
+       unsigned long base;
+       struct i2c_adapter adapter;
+};
+
+static inline void spacemit_i2c_set_reg(struct spacemit_i2c_adapter *adap,
+                                       uint8_t reg, uint32_t val)
+{
+       writel(val, (void *)adap->base + reg);
+}
+
+static inline uint32_t spacemit_i2c_get_reg(struct spacemit_i2c_adapter *adap,
+                                           uint32_t reg)
+{
+       return readl((void *)adap->base + reg);
+}
+
+static void spacemit_i2c_reset(struct spacemit_i2c_adapter *adap)
+{
+       /* disable unit */
+       spacemit_i2c_set_reg(adap, ICR_OFFSET, 0);
+       sbi_timer_udelay(I2C_RESET_US);
+
+       /* reset unit */
+       spacemit_i2c_set_reg(adap, ICR_OFFSET, ICR_UR);
+       sbi_timer_udelay(I2C_RESET_US);
+
+       /* clear reset and enable unit and SCL */
+       spacemit_i2c_set_reg(adap, ICR_OFFSET, ICR_IUE | ICR_SCLE);
+}
+
+static int spacemit_i2c_wait_xfer_done(struct spacemit_i2c_adapter *adap)
+{
+       for (int i = 0; i < I2C_TIMEOUT_US; i++) {
+               uint32_t val = spacemit_i2c_get_reg(adap, ICR_OFFSET);
+
+               if (!(val & ICR_TB))
+                       return 0;
+
+               sbi_timer_udelay(1);
+       };
+
+       return SBI_ETIMEDOUT;
+}
+
+static void spacemit_i2c_start_xfer(struct spacemit_i2c_adapter *adap,
+                                   uint32_t ctrl)
+{
+       const uint32_t ctrl_mask = ICR_START | ICR_STOP | ICR_ACKNAK;
+       uint32_t val;
+
+       val = spacemit_i2c_get_reg(adap, ICR_OFFSET);
+       val &= ~ctrl_mask;
+       val |= (ctrl & ctrl_mask);
+       val |= ICR_TB;
+
+       spacemit_i2c_set_reg(adap, ICR_OFFSET, val);
+}
+
+static int spacemit_i2c_xfer_write(struct spacemit_i2c_adapter *adap,
+                                  uint8_t byte, uint32_t ctrl)
+{
+       spacemit_i2c_set_reg(adap, IDBR_OFFSET, byte);
+       spacemit_i2c_start_xfer(adap, ctrl);
+
+       return spacemit_i2c_wait_xfer_done(adap);
+}
+
+static int spacemit_i2c_xfer_read(struct spacemit_i2c_adapter *adap,
+                                 uint8_t *byte, uint32_t ctrl)
+{
+       int rc;
+
+       spacemit_i2c_start_xfer(adap, ctrl);
+
+       rc = spacemit_i2c_wait_xfer_done(adap);
+       if (rc)
+               return rc;
+
+       *byte = spacemit_i2c_get_reg(adap, IDBR_OFFSET);
+       return 0;
+}
+
+static int spacemit_i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr,
+                                     uint8_t reg, uint8_t *buffer, int len)
+{
+       struct spacemit_i2c_adapter *adap =
+               container_of(ia, struct spacemit_i2c_adapter, adapter);
+       int rc;
+
+       /* reset controller to a known state */
+       spacemit_i2c_reset(adap);
+
+       /* send device address (in write mode) */
+       rc = spacemit_i2c_xfer_write(adap, addr << 1, ICR_START);
+       if (rc)
+               return rc;
+
+       /* send register + data bytes */
+       for (int i = 0; i < len + 1; i++) {
+               uint32_t ctrl = (i == len) ? ICR_STOP : 0;
+               uint8_t byte = (i == 0) ? reg : buffer[i - 1];
+
+               rc = spacemit_i2c_xfer_write(adap, byte, ctrl);
+               if (rc)
+                       return rc;
+       }
+
+       return 0;
+}
+
+static int spacemit_i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr,
+                                    uint8_t reg, uint8_t *buffer, int len)
+{
+       struct spacemit_i2c_adapter *adap =
+               container_of(ia, struct spacemit_i2c_adapter, adapter);
+       int rc;
+
+       /* reset controller to a known state */
+       spacemit_i2c_reset(adap);
+
+       /* send device address (in write mode) */
+       rc = spacemit_i2c_xfer_write(adap, addr << 1, ICR_START);
+       if (rc)
+               return rc;
+
+       /* send register */
+       rc = spacemit_i2c_xfer_write(adap, reg, 0);
+       if (rc)
+               return rc;
+
+       /* repeated start and send device address (in read mode) */
+       rc = spacemit_i2c_xfer_write(adap, (addr << 1) | 1, ICR_START);
+       if (rc)
+               return rc;
+
+       /* read data bytes */
+       for (int i = 0; i < len; i++) {
+               uint32_t ctrl = (i == len - 1) ? (ICR_ACKNAK | ICR_STOP) : 0;
+
+               rc = spacemit_i2c_xfer_read(adap, &buffer[i], ctrl);
+               if (rc)
+                       return rc;
+       }
+
+       return 0;
+}
+
+static int spacemit_i2c_init(const void *fdt, int nodeoff,
+                            const struct fdt_match *match)
+{
+       struct spacemit_i2c_adapter *adapter;
+       uint64_t base;
+       int rc;
+
+       adapter = sbi_zalloc(sizeof(*adapter));
+       if (!adapter)
+               return SBI_ENOMEM;
+
+       rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &base, NULL);
+       if (rc) {
+               sbi_free(adapter);
+               return rc;
+       }
+
+       adapter->base = base;
+       adapter->adapter.id = nodeoff;
+       adapter->adapter.write = spacemit_i2c_adapter_write;
+       adapter->adapter.read = spacemit_i2c_adapter_read;
+       rc = i2c_adapter_add(&adapter->adapter);
+       if (rc) {
+               sbi_free(adapter);
+               return rc;
+       }
+
+       return 0;
+}
+
+static const struct fdt_match spacemit_i2c_match[] = {
+       { .compatible = "spacemit,k1-i2c" },
+       { },
+};
+
+const struct fdt_driver fdt_i2c_adapter_spacemit = {
+       .match_table = spacemit_i2c_match,
+       .init = spacemit_i2c_init,
+};
lib/utils/i2c/objects.mk +3 -0
index d34d6648..91ac17ec 100644
@@ -15,6 +15,9 @@ libsbiutils-objs-$(CONFIG_FDT_I2C) += i2c/fdt_i2c_adapter_drivers.carray.o
 carray-fdt_i2c_adapter_drivers-$(CONFIG_FDT_I2C_SIFIVE) += fdt_i2c_adapter_sifive
 libsbiutils-objs-$(CONFIG_FDT_I2C_SIFIVE) += i2c/fdt_i2c_sifive.o
 
+carray-fdt_i2c_adapter_drivers-$(CONFIG_FDT_I2C_SPACEMIT) += fdt_i2c_adapter_spacemit
+libsbiutils-objs-$(CONFIG_FDT_I2C_SPACEMIT) += i2c/fdt_i2c_spacemit.o
+
 carray-fdt_i2c_adapter_drivers-$(CONFIG_FDT_I2C_DW) += fdt_i2c_adapter_dw
 libsbiutils-objs-$(CONFIG_FDT_I2C_DW) += i2c/fdt_i2c_dw.o
 
platform/generic/configs/defconfig +1 -1
index 1d3431e7..a9cb0f06 100644
@@ -31,6 +31,7 @@ CONFIG_FDT_HSM_RPMI=y
 CONFIG_FDT_HSM_SIFIVE_TMC0=y
 CONFIG_FDT_I2C=y
 CONFIG_FDT_I2C_SIFIVE=y
+CONFIG_FDT_I2C_SPACEMIT=y
 CONFIG_FDT_I2C_DW=y
 CONFIG_FDT_IPI=y
 CONFIG_FDT_IPI_MSWI=y
--

2.53.0

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

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Applied
#3 lore raw

Re: [PATCH 1/2] lib: utils/i2c: add minimal SpacemiT I2C driver

In reply to Aurelien Jarno
Quoted 5 lines · Aurelien Jarno

Add a simple SpacemiT I2C driver for basic byte transfers over the I2C bus, prioritizing simplicity over performance. The driver operates in PIO mode and does not use interrupts, FIFO, or DMA.

Quoted 8 lines

The controller is reset at the start of each transaction to ensure a known initial state, regardless of prior configuration by the kernel. This also avoids the need for additional error recovery code.

This will be used for communication with onboard PMIC to reset and power-off the board.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 283 lines
---
 lib/utils/i2c/Kconfig              |   4 +
 lib/utils/i2c/fdt_i2c_spacemit.c   | 220 +++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk           |   3 +
 platform/generic/configs/defconfig |   1 +
 4 files changed, 228 insertions(+)
 create mode 100644 lib/utils/i2c/fdt_i2c_spacemit.c
lib/utils/i2c/Kconfig +4 -0
index 7fa32fcf..bdaaff62 100644
@@ -14,6 +14,10 @@ config FDT_I2C_SIFIVE
        bool "SiFive I2C FDT driver"
        default n
 
+config FDT_I2C_SPACEMIT
+       bool "SpacemiT I2C FDT driver"
+       default n
+
 config FDT_I2C_DW
        bool "Synopsys Designware I2C FDT driver"
        select I2C_DW
lib/utils/i2c/fdt_i2c_spacemit.c new file +220 -0
new file mode 100644
index 00000000..9009b6b6
@@ -0,0 +1,220 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Aurelien Jarno
+ *
+ * Authors:
+ *   Aurelien Jarno <[email protected]>
+ */
+
+#include <sbi/riscv_io.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_heap.h>
+#include <sbi/sbi_timer.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/i2c/fdt_i2c.h>
+
+/* Controller registers */
+#define ICR_OFFSET     0x00            /* I2C control register */
+#define IDBR_OFFSET    0x0c            /* I2C data buffer register */
+
+/* Control register bits */
+#define ICR_START      BIT(0)          /* start */
+#define ICR_STOP       BIT(1)          /* stop */
+#define ICR_ACKNAK     BIT(2)          /* ACK(0) or NAK(1) */
+#define ICR_TB         BIT(3)          /* transfer byte */
+#define ICR_UR         BIT(10)         /* unit reset */
+#define ICR_SCLE       BIT(13)         /* SCL enable  */
+#define ICR_IUE                BIT(14)         /* unit enable */
+
+/* Timing */
+#define I2C_RESET_US   10
+#define I2C_TIMEOUT_US 1000
+
+struct spacemit_i2c_adapter {
+       unsigned long base;
+       struct i2c_adapter adapter;
+};
+
+static inline void spacemit_i2c_set_reg(struct spacemit_i2c_adapter *adap,
+                                       uint8_t reg, uint32_t val)
+{
+       writel(val, (void *)adap->base + reg);
+}
+
+static inline uint32_t spacemit_i2c_get_reg(struct spacemit_i2c_adapter *adap,
+                                           uint32_t reg)
+{
+       return readl((void *)adap->base + reg);
+}
+
+static void spacemit_i2c_reset(struct spacemit_i2c_adapter *adap)
+{
+       /* disable unit */
+       spacemit_i2c_set_reg(adap, ICR_OFFSET, 0);
+       sbi_timer_udelay(I2C_RESET_US);
+
+       /* reset unit */
+       spacemit_i2c_set_reg(adap, ICR_OFFSET, ICR_UR);
+       sbi_timer_udelay(I2C_RESET_US);
+
+       /* clear reset and enable unit and SCL */
+       spacemit_i2c_set_reg(adap, ICR_OFFSET, ICR_IUE | ICR_SCLE);
+}
+
+static int spacemit_i2c_wait_xfer_done(struct spacemit_i2c_adapter *adap)
+{
+       for (int i = 0; i < I2C_TIMEOUT_US; i++) {
+               uint32_t val = spacemit_i2c_get_reg(adap, ICR_OFFSET);
+
+               if (!(val & ICR_TB))
+                       return 0;
+
+               sbi_timer_udelay(1);
+       };
+
+       return SBI_ETIMEDOUT;
+}
+
+static void spacemit_i2c_start_xfer(struct spacemit_i2c_adapter *adap,
+                                   uint32_t ctrl)
+{
+       const uint32_t ctrl_mask = ICR_START | ICR_STOP | ICR_ACKNAK;
+       uint32_t val;
+
+       val = spacemit_i2c_get_reg(adap, ICR_OFFSET);
+       val &= ~ctrl_mask;
+       val |= (ctrl & ctrl_mask);
+       val |= ICR_TB;
+
+       spacemit_i2c_set_reg(adap, ICR_OFFSET, val);
+}
+
+static int spacemit_i2c_xfer_write(struct spacemit_i2c_adapter *adap,
+                                  uint8_t byte, uint32_t ctrl)
+{
+       spacemit_i2c_set_reg(adap, IDBR_OFFSET, byte);
+       spacemit_i2c_start_xfer(adap, ctrl);
+
+       return spacemit_i2c_wait_xfer_done(adap);
+}
+
+static int spacemit_i2c_xfer_read(struct spacemit_i2c_adapter *adap,
+                                 uint8_t *byte, uint32_t ctrl)
+{
+       int rc;
+
+       spacemit_i2c_start_xfer(adap, ctrl);
+
+       rc = spacemit_i2c_wait_xfer_done(adap);
+       if (rc)
+               return rc;
+
+       *byte = spacemit_i2c_get_reg(adap, IDBR_OFFSET);
+       return 0;
+}
+
+static int spacemit_i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr,
+                                     uint8_t reg, uint8_t *buffer, int len)
+{
+       struct spacemit_i2c_adapter *adap =
+               container_of(ia, struct spacemit_i2c_adapter, adapter);
+       int rc;
+
+       /* reset controller to a known state */
+       spacemit_i2c_reset(adap);
+
+       /* send device address (in write mode) */
+       rc = spacemit_i2c_xfer_write(adap, addr << 1, ICR_START);
+       if (rc)
+               return rc;
+
+       /* send register + data bytes */
+       for (int i = 0; i < len + 1; i++) {
+               uint32_t ctrl = (i == len) ? ICR_STOP : 0;
+               uint8_t byte = (i == 0) ? reg : buffer[i - 1];
+
+               rc = spacemit_i2c_xfer_write(adap, byte, ctrl);
+               if (rc)
+                       return rc;
+       }
+
+       return 0;
+}
+
+static int spacemit_i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr,
+                                    uint8_t reg, uint8_t *buffer, int len)
+{
+       struct spacemit_i2c_adapter *adap =
+               container_of(ia, struct spacemit_i2c_adapter, adapter);
+       int rc;
+
+       /* reset controller to a known state */
+       spacemit_i2c_reset(adap);
+
+       /* send device address (in write mode) */
+       rc = spacemit_i2c_xfer_write(adap, addr << 1, ICR_START);
+       if (rc)
+               return rc;
+
+       /* send register */
+       rc = spacemit_i2c_xfer_write(adap, reg, 0);
+       if (rc)
+               return rc;
+
+       /* repeated start and send device address (in read mode) */
+       rc = spacemit_i2c_xfer_write(adap, (addr << 1) | 1, ICR_START);
+       if (rc)
+               return rc;
+
+       /* read data bytes */
+       for (int i = 0; i < len; i++) {
+               uint32_t ctrl = (i == len - 1) ? (ICR_ACKNAK | ICR_STOP) : 0;
+
+               rc = spacemit_i2c_xfer_read(adap, &buffer[i], ctrl);
+               if (rc)
+                       return rc;
+       }
+
+       return 0;
+}
+
+static int spacemit_i2c_init(const void *fdt, int nodeoff,
+                            const struct fdt_match *match)
+{
+       struct spacemit_i2c_adapter *adapter;
+       uint64_t base;
+       int rc;
+
+       adapter = sbi_zalloc(sizeof(*adapter));
+       if (!adapter)
+               return SBI_ENOMEM;
+
+       rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &base, NULL);
+       if (rc) {
+               sbi_free(adapter);
+               return rc;
+       }
+
+       adapter->base = base;
+       adapter->adapter.id = nodeoff;
+       adapter->adapter.write = spacemit_i2c_adapter_write;
+       adapter->adapter.read = spacemit_i2c_adapter_read;
+       rc = i2c_adapter_add(&adapter->adapter);
+       if (rc) {
+               sbi_free(adapter);
+               return rc;
+       }
+
+       return 0;
+}
+
+static const struct fdt_match spacemit_i2c_match[] = {
+       { .compatible = "spacemit,k1-i2c" },
+       { },
+};
+
+const struct fdt_driver fdt_i2c_adapter_spacemit = {
+       .match_table = spacemit_i2c_match,
+       .init = spacemit_i2c_init,
+};
lib/utils/i2c/objects.mk +3 -0
index d34d6648..91ac17ec 100644
@@ -15,6 +15,9 @@ libsbiutils-objs-$(CONFIG_FDT_I2C) += i2c/fdt_i2c_adapter_drivers.carray.o
 carray-fdt_i2c_adapter_drivers-$(CONFIG_FDT_I2C_SIFIVE) += fdt_i2c_adapter_sifive
 libsbiutils-objs-$(CONFIG_FDT_I2C_SIFIVE) += i2c/fdt_i2c_sifive.o
 
+carray-fdt_i2c_adapter_drivers-$(CONFIG_FDT_I2C_SPACEMIT) += fdt_i2c_adapter_spacemit
+libsbiutils-objs-$(CONFIG_FDT_I2C_SPACEMIT) += i2c/fdt_i2c_spacemit.o
+
 carray-fdt_i2c_adapter_drivers-$(CONFIG_FDT_I2C_DW) += fdt_i2c_adapter_dw
 libsbiutils-objs-$(CONFIG_FDT_I2C_DW) += i2c/fdt_i2c_dw.o
 
platform/generic/configs/defconfig +1 -1
index 1d3431e7..a9cb0f06 100644
@@ -31,6 +31,7 @@ CONFIG_FDT_HSM_RPMI=y
 CONFIG_FDT_HSM_SIFIVE_TMC0=y
 CONFIG_FDT_I2C=y
 CONFIG_FDT_I2C_SIFIVE=y
+CONFIG_FDT_I2C_SPACEMIT=y
 CONFIG_FDT_I2C_DW=y
 CONFIG_FDT_IPI=y
 CONFIG_FDT_IPI_MSWI=y
--

2.53.0

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

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#4 lore raw

PATCH 2/2 lib: utils/reset: add SpacemiT P1 PMIC support

In reply to Aurelien Jarno

The SpacemiT P1 is a PMIC commonly found with SpacemiT CPU like K1. Add a reset driver for it.

---
 lib/utils/reset/Kconfig                 |   4 +
 lib/utils/reset/fdt_reset_spacemit_p1.c | 112 ++++++++++++++++++++++++
 lib/utils/reset/objects.mk              |   3 +
 platform/generic/configs/defconfig      |   1 +
 4 files changed, 120 insertions(+)
 create mode 100644 lib/utils/reset/fdt_reset_spacemit_p1.c
lib/utils/reset/Kconfig +4 -0
index 4835921f..f98926e5 100644
@@ -33,6 +33,10 @@ config FDT_RESET_SG2042_HWMON_MCU
 	bool "Sophgo SG2042 hwmon MCU FDT reset driver"
 	default n
 
+config FDT_RESET_SPACEMIT_P1
+	bool "SpacemiT P1 reset driver"
+	default n
+
 config FDT_RESET_SUNXI_WDT
 	bool "Sunxi WDT FDT reset driver"
 	default n
lib/utils/reset/fdt_reset_spacemit_p1.c new file +112 -0

112 lines not on this page · raw message

lib/utils/reset/objects.mk +3 -0
index ac38b49d..3c681c27 100644
@@ -20,6 +20,9 @@ libsbiutils-objs-$(CONFIG_FDT_RESET_HTIF) += reset/fdt_reset_htif.o
 carray-fdt_early_drivers-$(CONFIG_FDT_RESET_SG2042_HWMON_MCU) += fdt_reset_sg2042_mcu
 libsbiutils-objs-$(CONFIG_FDT_RESET_SG2042_HWMON_MCU) += reset/fdt_reset_sg2042_hwmon_mcu.o
 
+carray-fdt_early_drivers-$(CONFIG_FDT_RESET_SPACEMIT_P1) += fdt_reset_spacemit_p1
+libsbiutils-objs-$(CONFIG_FDT_RESET_SPACEMIT_P1) += reset/fdt_reset_spacemit_p1.o
+
 carray-fdt_early_drivers-$(CONFIG_FDT_RESET_SUNXI_WDT) += fdt_reset_sunxi_wdt
 libsbiutils-objs-$(CONFIG_FDT_RESET_SUNXI_WDT) += reset/fdt_reset_sunxi_wdt.o
 
platform/generic/configs/defconfig +1 -0
index a9cb0f06..ae278856 100644
@@ -51,6 +51,7 @@ CONFIG_FDT_RESET_GPIO=y
 CONFIG_FDT_RESET_HTIF=y
 CONFIG_FDT_RESET_RPMI=y
 CONFIG_FDT_RESET_SG2042_HWMON_MCU=y
+CONFIG_FDT_RESET_SPACEMIT_P1=y
 CONFIG_FDT_RESET_SUNXI_WDT=y
 CONFIG_FDT_RESET_SYSCON=y
 CONFIG_FDT_SERIAL=y
Signature · 7 lines
2.53.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Tested-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] lib: utils/reset: add SpacemiT P1 PMIC support

Hi Aurelien,

Quoted 5 lines · Aurelien Jarno

The SpacemiT P1 is a PMIC commonly found with SpacemiT CPU like K1. Add a reset driver for it.

Thanks
-Anand

Quoted 175 lines
---
 lib/utils/reset/Kconfig                 |   4 +
 lib/utils/reset/fdt_reset_spacemit_p1.c | 112 ++++++++++++++++++++++++
 lib/utils/reset/objects.mk              |   3 +
 platform/generic/configs/defconfig      |   1 +
 4 files changed, 120 insertions(+)
 create mode 100644 lib/utils/reset/fdt_reset_spacemit_p1.c
lib/utils/reset/Kconfig +4 -0
index 4835921f..f98926e5 100644
@@ -33,6 +33,10 @@ config FDT_RESET_SG2042_HWMON_MCU
        bool "Sophgo SG2042 hwmon MCU FDT reset driver"
        default n
 
+config FDT_RESET_SPACEMIT_P1
+       bool "SpacemiT P1 reset driver"
+       default n
+
 config FDT_RESET_SUNXI_WDT
        bool "Sunxi WDT FDT reset driver"
        default n
lib/utils/reset/fdt_reset_spacemit_p1.c new file +112 -0
new file mode 100644
index 00000000..5312e741
@@ -0,0 +1,112 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Aurelien Jarno
+ * Authors:
+ *   Aurelien Jarno <[email protected]>
+ */
+
+#include <libfdt.h>
+#include <platform_override.h>
+#include <sbi/sbi_bitops.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_system.h>
+#include <sbi_utils/fdt/fdt_driver.h>
+#include <sbi_utils/i2c/fdt_i2c.h>
+
+/* SpacemiT P1 Power Control Register 2 */
+#define PWR_CTRL2              0x7e
+#define PWR_CTRL2_SHUTDOWN     BIT(2)  /* Shutdown request */
+#define PWR_CTRL2_RST          BIT(1)  /* Reset request */
+
+static struct i2c_adapter *p1_adapter = NULL;
+static uint32_t p1_reg = 0;
+
+static int p1_system_reset_check(uint32_t type, uint32_t reason)
+{
+       switch (type) {
+       case SBI_SRST_RESET_TYPE_SHUTDOWN:
+               return 1;
+       case SBI_SRST_RESET_TYPE_COLD_REBOOT:
+       case SBI_SRST_RESET_TYPE_WARM_REBOOT:
+               return 255;
+       }
+
+       return 0;
+}
+
+static void p1_ops(uint32_t type)
+{
+       uint8_t byte;
+       int rc;
+
+       rc = i2c_adapter_reg_read(p1_adapter, p1_reg, PWR_CTRL2, &byte);
+       if (rc) {
+               sbi_printf("%s: cannot read P1 Power Control Register 2\n", __func__);
+               return;
+       }
+
+       if (type == SBI_SRST_RESET_TYPE_SHUTDOWN)
+               byte |= PWR_CTRL2_SHUTDOWN;
+       else
+               byte |= PWR_CTRL2_RST;
+
+       rc = i2c_adapter_reg_write(p1_adapter, p1_reg, PWR_CTRL2, byte);
+       if (rc)
+               sbi_printf("%s: cannot write P1 Power Control Register 2\n", __func__);
+}
+
+static void p1_system_reset(uint32_t type, uint32_t reason)
+{
+       switch (type) {
+       case SBI_SRST_RESET_TYPE_SHUTDOWN:
+       case SBI_SRST_RESET_TYPE_COLD_REBOOT:
+       case SBI_SRST_RESET_TYPE_WARM_REBOOT:
+               p1_ops(type);
+               break;
+       }
+}
+
+static struct sbi_system_reset_device p1_reset = {
+       .name = "spacemit-p1-reset",
+       .system_reset_check = p1_system_reset_check,
+       .system_reset = p1_system_reset
+};
+
+static int p1_reset_init(const void *fdt, int nodeoff,
+                        const struct fdt_match *match)
+{
+       int rc, i2c_bus;
+       uint64_t addr;
+
+       /* we are spacemit,p1 node */
+       rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
+       if (rc)
+               return rc;
+
+       p1_reg = addr;
+
+       /* find i2c bus parent node */
+       i2c_bus = fdt_parent_offset(fdt, nodeoff);
+       if (i2c_bus < 0)
+               return i2c_bus;
+
+       /* i2c adapter get */
+       rc = fdt_i2c_adapter_get(fdt, i2c_bus, &p1_adapter);
+       if (rc)
+               return rc;
+
+       sbi_system_reset_add_device(&p1_reset);
+
+       return 0;
+}
+
+static const struct fdt_match p1_reset_match[] = {
+       { .compatible = "spacemit,p1", .data = (void *)true },
+       { },
+};
+
+const struct fdt_driver fdt_reset_spacemit_p1 = {
+       .match_table = p1_reset_match,
+       .init = p1_reset_init,
+};
lib/utils/reset/objects.mk +3 -0
index ac38b49d..3c681c27 100644
@@ -20,6 +20,9 @@ libsbiutils-objs-$(CONFIG_FDT_RESET_HTIF) += reset/fdt_reset_htif.o
 carray-fdt_early_drivers-$(CONFIG_FDT_RESET_SG2042_HWMON_MCU) += fdt_reset_sg2042_mcu
 libsbiutils-objs-$(CONFIG_FDT_RESET_SG2042_HWMON_MCU) += reset/fdt_reset_sg2042_hwmon_mcu.o
 
+carray-fdt_early_drivers-$(CONFIG_FDT_RESET_SPACEMIT_P1) += fdt_reset_spacemit_p1
+libsbiutils-objs-$(CONFIG_FDT_RESET_SPACEMIT_P1) += reset/fdt_reset_spacemit_p1.o
+
 carray-fdt_early_drivers-$(CONFIG_FDT_RESET_SUNXI_WDT) += fdt_reset_sunxi_wdt
 libsbiutils-objs-$(CONFIG_FDT_RESET_SUNXI_WDT) += reset/fdt_reset_sunxi_wdt.o
 
platform/generic/configs/defconfig +1 -1
index a9cb0f06..ae278856 100644
@@ -51,6 +51,7 @@ CONFIG_FDT_RESET_GPIO=y
 CONFIG_FDT_RESET_HTIF=y
 CONFIG_FDT_RESET_RPMI=y
 CONFIG_FDT_RESET_SG2042_HWMON_MCU=y
+CONFIG_FDT_RESET_SPACEMIT_P1=y
 CONFIG_FDT_RESET_SUNXI_WDT=y
 CONFIG_FDT_RESET_SYSCON=y
 CONFIG_FDT_SERIAL=y
--

2.53.0

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

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Applied
#6 lore raw

Re: [PATCH 2/2] lib: utils/reset: add SpacemiT P1 PMIC support

In reply to Aurelien Jarno
Quoted 5 lines · Aurelien Jarno

The SpacemiT P1 is a PMIC commonly found with SpacemiT CPU like K1. Add a reset driver for it.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 175 lines
---
 lib/utils/reset/Kconfig                 |   4 +
 lib/utils/reset/fdt_reset_spacemit_p1.c | 112 ++++++++++++++++++++++++
 lib/utils/reset/objects.mk              |   3 +
 platform/generic/configs/defconfig      |   1 +
 4 files changed, 120 insertions(+)
 create mode 100644 lib/utils/reset/fdt_reset_spacemit_p1.c
lib/utils/reset/Kconfig +4 -0
index 4835921f..f98926e5 100644
@@ -33,6 +33,10 @@ config FDT_RESET_SG2042_HWMON_MCU
        bool "Sophgo SG2042 hwmon MCU FDT reset driver"
        default n
 
+config FDT_RESET_SPACEMIT_P1
+       bool "SpacemiT P1 reset driver"
+       default n
+
 config FDT_RESET_SUNXI_WDT
        bool "Sunxi WDT FDT reset driver"
        default n
lib/utils/reset/fdt_reset_spacemit_p1.c new file +112 -0
new file mode 100644
index 00000000..5312e741
@@ -0,0 +1,112 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Aurelien Jarno
+ * Authors:
+ *   Aurelien Jarno <[email protected]>
+ */
+
+#include <libfdt.h>
+#include <platform_override.h>
+#include <sbi/sbi_bitops.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_system.h>
+#include <sbi_utils/fdt/fdt_driver.h>
+#include <sbi_utils/i2c/fdt_i2c.h>
+
+/* SpacemiT P1 Power Control Register 2 */
+#define PWR_CTRL2              0x7e
+#define PWR_CTRL2_SHUTDOWN     BIT(2)  /* Shutdown request */
+#define PWR_CTRL2_RST          BIT(1)  /* Reset request */
+
+static struct i2c_adapter *p1_adapter = NULL;
+static uint32_t p1_reg = 0;
+
+static int p1_system_reset_check(uint32_t type, uint32_t reason)
+{
+       switch (type) {
+       case SBI_SRST_RESET_TYPE_SHUTDOWN:
+               return 1;
+       case SBI_SRST_RESET_TYPE_COLD_REBOOT:
+       case SBI_SRST_RESET_TYPE_WARM_REBOOT:
+               return 255;
+       }
+
+       return 0;
+}
+
+static void p1_ops(uint32_t type)
+{
+       uint8_t byte;
+       int rc;
+
+       rc = i2c_adapter_reg_read(p1_adapter, p1_reg, PWR_CTRL2, &byte);
+       if (rc) {
+               sbi_printf("%s: cannot read P1 Power Control Register 2\n", __func__);
+               return;
+       }
+
+       if (type == SBI_SRST_RESET_TYPE_SHUTDOWN)
+               byte |= PWR_CTRL2_SHUTDOWN;
+       else
+               byte |= PWR_CTRL2_RST;
+
+       rc = i2c_adapter_reg_write(p1_adapter, p1_reg, PWR_CTRL2, byte);
+       if (rc)
+               sbi_printf("%s: cannot write P1 Power Control Register 2\n", __func__);
+}
+
+static void p1_system_reset(uint32_t type, uint32_t reason)
+{
+       switch (type) {
+       case SBI_SRST_RESET_TYPE_SHUTDOWN:
+       case SBI_SRST_RESET_TYPE_COLD_REBOOT:
+       case SBI_SRST_RESET_TYPE_WARM_REBOOT:
+               p1_ops(type);
+               break;
+       }
+}
+
+static struct sbi_system_reset_device p1_reset = {
+       .name = "spacemit-p1-reset",
+       .system_reset_check = p1_system_reset_check,
+       .system_reset = p1_system_reset
+};
+
+static int p1_reset_init(const void *fdt, int nodeoff,
+                        const struct fdt_match *match)
+{
+       int rc, i2c_bus;
+       uint64_t addr;
+
+       /* we are spacemit,p1 node */
+       rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
+       if (rc)
+               return rc;
+
+       p1_reg = addr;
+
+       /* find i2c bus parent node */
+       i2c_bus = fdt_parent_offset(fdt, nodeoff);
+       if (i2c_bus < 0)
+               return i2c_bus;
+
+       /* i2c adapter get */
+       rc = fdt_i2c_adapter_get(fdt, i2c_bus, &p1_adapter);
+       if (rc)
+               return rc;
+
+       sbi_system_reset_add_device(&p1_reset);
+
+       return 0;
+}
+
+static const struct fdt_match p1_reset_match[] = {
+       { .compatible = "spacemit,p1", .data = (void *)true },
+       { },
+};
+
+const struct fdt_driver fdt_reset_spacemit_p1 = {
+       .match_table = p1_reset_match,
+       .init = p1_reset_init,
+};
lib/utils/reset/objects.mk +3 -0
index ac38b49d..3c681c27 100644
@@ -20,6 +20,9 @@ libsbiutils-objs-$(CONFIG_FDT_RESET_HTIF) += reset/fdt_reset_htif.o
 carray-fdt_early_drivers-$(CONFIG_FDT_RESET_SG2042_HWMON_MCU) += fdt_reset_sg2042_mcu
 libsbiutils-objs-$(CONFIG_FDT_RESET_SG2042_HWMON_MCU) += reset/fdt_reset_sg2042_hwmon_mcu.o
 
+carray-fdt_early_drivers-$(CONFIG_FDT_RESET_SPACEMIT_P1) += fdt_reset_spacemit_p1
+libsbiutils-objs-$(CONFIG_FDT_RESET_SPACEMIT_P1) += reset/fdt_reset_spacemit_p1.o
+
 carray-fdt_early_drivers-$(CONFIG_FDT_RESET_SUNXI_WDT) += fdt_reset_sunxi_wdt
 libsbiutils-objs-$(CONFIG_FDT_RESET_SUNXI_WDT) += reset/fdt_reset_sunxi_wdt.o
 
platform/generic/configs/defconfig +1 -1
index a9cb0f06..ae278856 100644
@@ -51,6 +51,7 @@ CONFIG_FDT_RESET_GPIO=y
 CONFIG_FDT_RESET_HTIF=y
 CONFIG_FDT_RESET_RPMI=y
 CONFIG_FDT_RESET_SG2042_HWMON_MCU=y
+CONFIG_FDT_RESET_SPACEMIT_P1=y
 CONFIG_FDT_RESET_SUNXI_WDT=y
 CONFIG_FDT_RESET_SYSCON=y
 CONFIG_FDT_SERIAL=y
--

2.53.0

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

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi