Skip to contentSkip to conversation

I2C framework, reboot Unmatched via PMIC

PATCH Superseded Nikita Shubin 5 participants 38 messages Oct 15, 2021 – Nov 4, 2021 2 messages not in the archive SiFive drivers

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover I2C framework, reboot Unmatched via PMIC

From: Nikita Shubin <[email protected]>

This series introduce rebooting via i2c da9063 PMIC, currently on SiFive Unmatched board.

"gpio-poweroff" remains with default priority of 128 - default priority, da9063 is 1 by default the least priority.

da9063-reset {
	compatible = "da9063-reset";
	priority = <1>;
};

Is required to be added as a child node of PMIC.

tested via Linux and U-Boot with reset extension:

OpenSBI:
Platform Reboot Device : da9063-reset
Platform Shutdown Device : gpio-reset

v1 -> v2:
Added:
lib: sbi: add priority for reset handler

Renamed read/write to smbus_write/smbus_read, as actually this is not a "raw" read/write but a one with a register address provided, later a "raw" version will be need but currently i don't have anything to test it.

To Xiang W:
I have analized your proposal of switching to sbi_list, and switched i2c adapters array to list. Unfortunately to switch drivers we require "init" functions.

to Alexander Ghiti:

Quoted 1 line

No need for the struct i2c_adapter argument as it is globally accessible.

It looks like a more clean and reusable way to me, let's here more opinions

Quoted 3 lines

Why should the da9063 device care about the adapter configuration here? I think It should just rely on an already configured/initialized i2c controller.

With clocks init added to sifive_i2c configure it can become usable even if nobody cared about controller initialization.

Quoted 3 lines

Establishing the link between the i2c device and its adapter should somehow be implicitly done by the i2c library, IMO the device should not care about its controller.

And how do you think it will look like ? In out case it's not a separate driver/client entity but all togather for simplicity.

Nikita Shubin (5):
lib: utils/reset: add priority to gpio reset
lib: utils/i2c: Add generic I2C configuration library
lib: utils/i2c: Add simple FDT based I2C framework
lib: utils/i2c: Add minimal SiFive I2C driver
lib: utils/reset: Add generic da9063 reset driver

 include/sbi_utils/i2c/fdt_i2c.h    |  26 +++
 include/sbi_utils/i2c/i2c.h        |  99 +++++++++++
 lib/utils/i2c/fdt_i2c.c            | 111 ++++++++++++
 lib/utils/i2c/fdt_i2c_sifive.c     | 271 +++++++++++++++++++++++++++++
 lib/utils/i2c/i2c.c                |  85 +++++++++
 lib/utils/i2c/objects.mk           |  12 ++
 lib/utils/reset/fdt_reset.c        |   2 +
 lib/utils/reset/fdt_reset_da9063.c | 214 +++++++++++++++++++++++
 lib/utils/reset/fdt_reset_gpio.c   |  18 +-
 lib/utils/reset/objects.mk         |   1 +
 10 files changed, 836 insertions(+), 3 deletions(-)
 create mode 100644 include/sbi_utils/i2c/fdt_i2c.h
 create mode 100644 include/sbi_utils/i2c/i2c.h
 create mode 100644 lib/utils/i2c/fdt_i2c.c
 create mode 100644 lib/utils/i2c/fdt_i2c_sifive.c
 create mode 100644 lib/utils/i2c/i2c.c
 create mode 100644 lib/utils/i2c/objects.mk
 create mode 100644 lib/utils/reset/fdt_reset_da9063.c
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 1/5 lib: utils/reset: add priority to gpio reset

From: Nikita Shubin <[email protected]>

Make gpio_system_reset_check return priority instead of just true/false.

Make default 128 priority for reset/shutdown and ability to specify priority in device tree.

---
 lib/utils/reset/fdt_reset_gpio.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
lib/utils/reset/fdt_reset_gpio.c +15 -3
index 4da1450..44b67d8 100644
@@ -23,16 +23,19 @@ struct gpio_reset {
 	struct gpio_pin pin;
 	u32 active_delay;
 	u32 inactive_delay;
+	u8 priority;
 };
 
 static struct gpio_reset poweroff = {
 	.active_delay = 100,
-	.inactive_delay = 100
+	.inactive_delay = 100,
+	.priority = 128
 };
 
 static struct gpio_reset restart = {
 	.active_delay = 100,
-	.inactive_delay = 100
+	.inactive_delay = 100,
+	.priority = 128
 };
 
 static struct gpio_reset *gpio_get_reset_settings(u32 type)
@@ -59,7 +62,12 @@ static struct gpio_reset *gpio_get_reset_settings(u32 type)
 
 static int gpio_system_reset_check(u32 type, u32 reason)
 {
-	return !!gpio_get_reset_settings(type);
+	struct gpio_reset *reset = gpio_get_reset_settings(type);
+
+	if (reset)
+		return reset->priority;
+
+	return 0;
 }
 
 static void gpio_system_reset(u32 type, u32 reason)
@@ -115,6 +123,10 @@ static int gpio_reset_init(void *fdt, int nodeoff,
 	if (len > 0)
 		reset->inactive_delay = fdt32_to_cpu(*val);
 
+	val = fdt_getprop(fdt, nodeoff, "priority", &len);
+	if (len > 0)
+		reset->priority = fdt32_to_cpu(*val);
+
 	sbi_system_reset_add_device(&gpio_reset);
 
 	return 0;
Signature · 1 line
2.31.1
#2 lore raw

PATCH v2 2/5 lib: utils/i2c: Add generic I2C configuration library

In reply to Nikita Shubin

From: Nikita Shubin <[email protected]>

Helper library to keep track of registered I2C adapters, identified by dts offset, basic send/read functions and adapter configuration (enable, set dividers, etc...).

---
v1 -> v2:
- switch to lists instead of array
- rename send to write
- use buffers instead of single byte
- add single byte read/write

---
 include/sbi_utils/i2c/i2c.h | 99 +++++++++++++++++++++++++++++++++++++
 lib/utils/i2c/i2c.c         | 85 +++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk    | 10 ++++
 3 files changed, 194 insertions(+)
 create mode 100644 include/sbi_utils/i2c/i2c.h
 create mode 100644 lib/utils/i2c/i2c.c
 create mode 100644 lib/utils/i2c/objects.mk
include/sbi_utils/i2c/i2c.h new file +99 -0
new file mode 100644
index 0000000..76cdb66
@@ -0,0 +1,99 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ */
+
+#ifndef __I2C_H__
+#define __I2C_H__
+
+#include <sbi/sbi_types.h>
+#include <sbi/sbi_list.h>
+
+/** Representation of a I2C adapter */
+struct i2c_adapter {
+	/** Pointer to I2C driver owning this I2C adapter */
+	void *driver;
+
+	/** Unique ID of the I2C adapter assigned by the driver */
+	int id;
+
+	/**
+	 * Configure I2C adapter
+	 *
+	 * Enable, set dividers, etc...
+	 *
+	 * @return 0 on success and negative error code on failure
+	 */
+	int (*configure)(struct i2c_adapter *ia);
+
+	/**
+	 * Send buffer to given address, register
+	 *
+	 * @return 0 on success and negative error code on failure
+	 */
+	int (*smbus_write)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
+		    uint8_t *buffer, int len);
+
+	/**
+	 * Read buffer from given address, register
+	 *
+	 * @return 0 on success and negative error code on failure
+	 */
+	int (*smbus_read)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
+		    uint8_t *buffer, int len);
+
+	/** List */
+	struct sbi_dlist node;
+};
+
+static inline struct i2c_adapter *to_i2c_adapter(struct sbi_dlist *node)
+{
+	return container_of(node, struct i2c_adapter, node);
+}
+
+/** Find a registered I2C adapter */
+struct i2c_adapter *i2c_adapter_find(int id);
+
+/** Register I2C adapter */
+int i2c_adapter_add(struct i2c_adapter *ia);
+
+/** Un-register I2C adapter */
+void i2c_adapter_remove(struct i2c_adapter *ia);
+
+/** Configure I2C adapter prior to send/read */
+int i2c_adapter_configure(struct i2c_adapter *ia);
+
+/** Write to device on I2C adapter bus */
+int i2c_adapter_smbus_write(struct i2c_adapter *ia, uint8_t addr,
+			uint8_t reg, uint8_t *buffer, int len);
+
+/** Read from device on I2C adapter bus */
+int i2c_adapter_smbus_read(struct i2c_adapter *ia, uint8_t addr,
+			uint8_t reg, uint8_t *buffer, int len);
+
+/** Write single byte from device on I2C adapter bus */
+static inline int i2c_adapter_smbus_reg_write(struct i2c_adapter *ia,
+				uint8_t addr, uint8_t reg, uint8_t val)
+{
+	return i2c_adapter_smbus_write(ia, addr, reg, &val, 1);
+}
+
+/** Read single byte from device on I2C adapter bus */
+static inline int i2c_adapter_smbus_reg_read(struct i2c_adapter *ia,
+				uint8_t addr, uint8_t reg, uint8_t *val)
+{
+	uint8_t buf;
+	int ret = i2c_adapter_smbus_read(ia, addr, reg, &buf, 1);
+
+	if (ret)
+		return ret;
+
+	*val = buf;
+	return 0;
+}
+
+#endif
lib/utils/i2c/i2c.c new file +85 -0

85 lines not on this page · raw message

lib/utils/i2c/objects.mk new file +10 -0
new file mode 100644
index 0000000..16a70da
@@ -0,0 +1,10 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2021 YADRO
+#
+# Authors:
+#   Nikita Shubin <[email protected]>
+#
+
+libsbiutils-objs-y += i2c/i2c.o
Signature · 1 line
2.31.1
#3 lore raw

PATCH v2 2/5 lib: utils/i2c: Add generic I2C configuration library

On Fri, Oct 15, 2021 at 3:20 PM Nikita Shubin
<[email protected]> wrote:>

Quoted 57 lines

From: Nikita Shubin <[email protected]>

Helper library to keep track of registered I2C adapters, identified by dts offset, basic send/read functions and adapter configuration (enable, set dividers, etc...).

---
v1 -> v2:
- switch to lists instead of array
- rename send to write
- use buffers instead of single byte
- add single byte read/write

---
 include/sbi_utils/i2c/i2c.h | 99 +++++++++++++++++++++++++++++++++++++
 lib/utils/i2c/i2c.c         | 85 +++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk    | 10 ++++
 3 files changed, 194 insertions(+)
 create mode 100644 include/sbi_utils/i2c/i2c.h
 create mode 100644 lib/utils/i2c/i2c.c
 create mode 100644 lib/utils/i2c/objects.mk
include/sbi_utils/i2c/i2c.h new file +29 -0
new file mode 100644
index 0000000..76cdb66
@@ -0,0 +1,99 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ */
+
+#ifndef __I2C_H__
+#define __I2C_H__
+
+#include <sbi/sbi_types.h>
+#include <sbi/sbi_list.h>
+
+/** Representation of a I2C adapter */
+struct i2c_adapter {
+       /** Pointer to I2C driver owning this I2C adapter */
+       void *driver;
+
+       /** Unique ID of the I2C adapter assigned by the driver */
+       int id;
+
+       /**
+        * Configure I2C adapter
+        *
+        * Enable, set dividers, etc...
+        *
+        * @return 0 on success and negative error code on failure

include/sbi_utils/i2c/i2c.h line 39

Quoted 10 lines
include/sbi_utils/i2c/i2c.h new file +10 -0
+        */
+       int (*configure)(struct i2c_adapter *ia);
+
+       /**
+        * Send buffer to given address, register
+        *
+        * @return 0 on success and negative error code on failure
+        */
+       int (*smbus_write)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
+                   uint8_t *buffer, int len);

Why did you rename those functions with smbus prefix? And I would use size_t for the len argument.

Quoted 48 lines
include/sbi_utils/i2c/i2c.h new file +48 -0
+
+       /**
+        * Read buffer from given address, register
+        *
+        * @return 0 on success and negative error code on failure
+        */
+       int (*smbus_read)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
+                   uint8_t *buffer, int len);
+
+       /** List */
+       struct sbi_dlist node;
+};
+
+static inline struct i2c_adapter *to_i2c_adapter(struct sbi_dlist *node)
+{
+       return container_of(node, struct i2c_adapter, node);
+}
+
+/** Find a registered I2C adapter */
+struct i2c_adapter *i2c_adapter_find(int id);
+
+/** Register I2C adapter */
+int i2c_adapter_add(struct i2c_adapter *ia);
+
+/** Un-register I2C adapter */
+void i2c_adapter_remove(struct i2c_adapter *ia);
+
+/** Configure I2C adapter prior to send/read */
+int i2c_adapter_configure(struct i2c_adapter *ia);
+
+/** Write to device on I2C adapter bus */
+int i2c_adapter_smbus_write(struct i2c_adapter *ia, uint8_t addr,
+                       uint8_t reg, uint8_t *buffer, int len);
+
+/** Read from device on I2C adapter bus */
+int i2c_adapter_smbus_read(struct i2c_adapter *ia, uint8_t addr,
+                       uint8_t reg, uint8_t *buffer, int len);
+
+/** Write single byte from device on I2C adapter bus */
+static inline int i2c_adapter_smbus_reg_write(struct i2c_adapter *ia,
+                               uint8_t addr, uint8_t reg, uint8_t val)
+{
+       return i2c_adapter_smbus_write(ia, addr, reg, &val, 1);
+}
+
+/** Read single byte from device on I2C adapter bus */
+static inline int i2c_adapter_smbus_reg_read(struct i2c_adapter *ia,
+                               uint8_t addr, uint8_t reg, uint8_t *val)

include/sbi_utils/i2c/i2c.h line 97

Quoted 10 lines
include/sbi_utils/i2c/i2c.h new file +10 -0
+{
+       uint8_t buf;
+       int ret = i2c_adapter_smbus_read(ia, addr, reg, &buf, 1);
+
+       if (ret)
+               return ret;
+
+       *val = buf;
+       return 0;
+}

Nice to add the helpers, though I'm not sure "reg_read" is the right suffix as it may not be quite explicit: what about simply read_byte?

Quoted 83 lines
include/sbi_utils/i2c/i2c.h new file +2 -0
+
+#endif
lib/utils/i2c/i2c.c new file +75 -0
new file mode 100644
index 0000000..d23ac91
@@ -0,0 +1,85 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ *
+ * derivate: lib/utils/gpio/gpio.c
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#include <sbi/sbi_error.h>
+#include <sbi_utils/i2c/i2c.h>
+
+static SBI_LIST_HEAD(i2c_adapters_list);
+
+struct i2c_adapter *i2c_adapter_find(int id)
+{
+       struct sbi_dlist *pos;
+
+       sbi_list_for_each(pos, &(i2c_adapters_list)) {
+               struct i2c_adapter *adap = to_i2c_adapter(pos);
+
+               if (adap->id == id)
+                       return adap;
+       }
+
+       return NULL;
+}
+
+int i2c_adapter_add(struct i2c_adapter *ia)
+{
+       if (!ia)
+               return SBI_EINVAL;
+
+       if (i2c_adapter_find(ia->id))
+               return SBI_EALREADY;
+
+       sbi_list_add(&(ia->node), &(i2c_adapters_list));
+
+       return 0;
+}
+
+void i2c_adapter_remove(struct i2c_adapter *ia)
+{
+       if (!ia)
+               return;
+
+       sbi_list_del(&(ia->node));
+}
+
+int i2c_adapter_configure(struct i2c_adapter *ia)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->configure)
+               return 0;
+
+       return ia->configure(ia);
+}
+
+int i2c_adapter_smbus_write(struct i2c_adapter *ia, uint8_t addr,
+                       uint8_t reg, uint8_t *buffer, int len)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->smbus_write)
+               return SBI_ENOSYS;
+
+       return ia->smbus_write(ia, addr, reg, buffer, len);
+}
+
+

lib/utils/i2c/i2c.c line 85

Quoted 10 lines
lib/utils/i2c/i2c.c new file +10 -0
+int i2c_adapter_smbus_read(struct i2c_adapter *ia, uint8_t addr,
+                    uint8_t reg, uint8_t *buffer, int len)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->smbus_read)
+               return SBI_ENOSYS;
+
+       return ia->smbus_read(ia, addr, reg, buffer, len);
+}

Again, I don't find those helpers really useful, IMO the adapter should be hidden from the device, see my previous review. But you can convince me we don't need it, I'm open to hearing what you think :)

Alex

Quoted 24 lines
lib/utils/i2c/objects.mk new file +10 -1
new file mode 100644
index 0000000..16a70da
@@ -0,0 +1,10 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2021 YADRO
+#
+# Authors:
+#   Nikita Shubin <[email protected]>
+#
+
+libsbiutils-objs-y += i2c/i2c.o
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#4 lore raw

PATCH v2 2/5 lib: utils/i2c: Add generic I2C configuration library

Hello Alexandre!

Quoted 65 lines · On Tue, 19 Oct 2021 14:04:28 +0200 Alexandre Ghiti…

On Fri, Oct 15, 2021 at 3:20 PM Nikita Shubin
<[email protected]> wrote:>

Quoted 63 lines

From: Nikita Shubin <[email protected]>

Helper library to keep track of registered I2C adapters, identified by dts offset, basic send/read functions and adapter configuration (enable, set dividers, etc...).

---
v1 -> v2:
- switch to lists instead of array
- rename send to write
- use buffers instead of single byte
- add single byte read/write

---
 include/sbi_utils/i2c/i2c.h | 99
+++++++++++++++++++++++++++++++++++++ lib/utils/i2c/i2c.c         |
85 +++++++++++++++++++++++++++++++ lib/utils/i2c/objects.mk    | 10
++++ 3 files changed, 194 insertions(+)
 create mode 100644 include/sbi_utils/i2c/i2c.h
 create mode 100644 lib/utils/i2c/i2c.c
 create mode 100644 lib/utils/i2c/objects.mk
(quoted diff) +0 -0

b/include/sbi_utils/i2c/i2c.h new file mode 100644 index 0000000..76cdb66

include/sbi_utils/i2c/i2c.h new file +35 -0
@@ -0,0 +1,99 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ */
+
+#ifndef __I2C_H__
+#define __I2C_H__
+
+#include <sbi/sbi_types.h>
+#include <sbi/sbi_list.h>
+
+/** Representation of a I2C adapter */
+struct i2c_adapter {
+       /** Pointer to I2C driver owning this I2C adapter */
+       void *driver;
+
+       /** Unique ID of the I2C adapter assigned by the driver */
+       int id;
+
+       /**
+        * Configure I2C adapter
+        *
+        * Enable, set dividers, etc...
+        *
+        * @return 0 on success and negative error code on failure
+        */
+       int (*configure)(struct i2c_adapter *ia);
+
+       /**
+        * Send buffer to given address, register
+        *
Quoted 8 lines
Quoted 5 lines
include/sbi_utils/i2c/i2c.h new file +3 -0
+        * @return 0 on success and negative error code on failure
+        */
+       int (*smbus_write)(struct i2c_adapter *ia, uint8_t addr,

uint8_t reg,
+ uint8_t *buffer, int len);

Why did you rename those functions with smbus prefix? And I would use size_t for the len argument.

Cause, i think, actually specifying a register to read/write as a part of transaction is an smbus thing, as i stated in cover letter, raw read/write i2c don't use repeated start.

And may be size_t indeed.

Quoted 57 lines
Quoted 57 lines
include/sbi_utils/i2c/i2c.h new file +7 -0
+
+       /**
+        * Read buffer from given address, register
+        *
+        * @return 0 on success and negative error code on failure
+        */
+       int (*smbus_read)(struct i2c_adapter *ia, uint8_t addr,

uint8_t reg,

include/sbi_utils/i2c/i2c.h new file +7 -0
+                   uint8_t *buffer, int len);
+
+       /** List */
+       struct sbi_dlist node;
+};
+
+static inline struct i2c_adapter *to_i2c_adapter(struct sbi_dlist

*node) +{

include/sbi_utils/i2c/i2c.h new file +25 -0
+       return container_of(node, struct i2c_adapter, node);
+}
+
+/** Find a registered I2C adapter */
+struct i2c_adapter *i2c_adapter_find(int id);
+
+/** Register I2C adapter */
+int i2c_adapter_add(struct i2c_adapter *ia);
+
+/** Un-register I2C adapter */
+void i2c_adapter_remove(struct i2c_adapter *ia);
+
+/** Configure I2C adapter prior to send/read */
+int i2c_adapter_configure(struct i2c_adapter *ia);
+
+/** Write to device on I2C adapter bus */
+int i2c_adapter_smbus_write(struct i2c_adapter *ia, uint8_t addr,
+                       uint8_t reg, uint8_t *buffer, int len);
+
+/** Read from device on I2C adapter bus */
+int i2c_adapter_smbus_read(struct i2c_adapter *ia, uint8_t addr,
+                       uint8_t reg, uint8_t *buffer, int len);
+
+/** Write single byte from device on I2C adapter bus */
+static inline int i2c_adapter_smbus_reg_write(struct i2c_adapter

*ia,
+ uint8_t addr, uint8_t reg, uint8_t
val) +{

include/sbi_utils/i2c/i2c.h new file +5 -0
+       return i2c_adapter_smbus_write(ia, addr, reg, &val, 1);
+}
+
+/** Read single byte from device on I2C adapter bus */
+static inline int i2c_adapter_smbus_reg_read(struct i2c_adapter

*ia,
+ uint8_t addr, uint8_t reg, uint8_t
*val) +{

include/sbi_utils/i2c/i2c.h new file +4 -0
+       uint8_t buf;
+       int ret = i2c_adapter_smbus_read(ia, addr, reg, &buf, 1);
+
+       if (ret)
Quoted 8 lines

include/sbi_utils/i2c/i2c.h line 97

Quoted 5 lines
include/sbi_utils/i2c/i2c.h new file +5 -0
+               return ret;
+
+       *val = buf;
+       return 0;
+}  

Nice to add the helpers, though I'm not sure "reg_read" is the right suffix as it may not be quite explicit: what about simply read_byte?

I think byte_read/byte_write should be "raw" and omit the uint8_t reg and have naming like:

i2c_adapter_byte_read
i2c_adapter_byte_write

Without smbus prefix, but that's a bit different story.

Quoted 90 lines
Quoted 90 lines
include/sbi_utils/i2c/i2c.h new file +2 -0
+
+#endif
lib/utils/i2c/i2c.c new file +81 -0
new file mode 100644
index 0000000..d23ac91
@@ -0,0 +1,85 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ *
+ * derivate: lib/utils/gpio/gpio.c
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#include <sbi/sbi_error.h>
+#include <sbi_utils/i2c/i2c.h>
+
+static SBI_LIST_HEAD(i2c_adapters_list);
+
+struct i2c_adapter *i2c_adapter_find(int id)
+{
+       struct sbi_dlist *pos;
+
+       sbi_list_for_each(pos, &(i2c_adapters_list)) {
+               struct i2c_adapter *adap = to_i2c_adapter(pos);
+
+               if (adap->id == id)
+                       return adap;
+       }
+
+       return NULL;
+}
+
+int i2c_adapter_add(struct i2c_adapter *ia)
+{
+       if (!ia)
+               return SBI_EINVAL;
+
+       if (i2c_adapter_find(ia->id))
+               return SBI_EALREADY;
+
+       sbi_list_add(&(ia->node), &(i2c_adapters_list));
+
+       return 0;
+}
+
+void i2c_adapter_remove(struct i2c_adapter *ia)
+{
+       if (!ia)
+               return;
+
+       sbi_list_del(&(ia->node));
+}
+
+int i2c_adapter_configure(struct i2c_adapter *ia)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->configure)
+               return 0;
+
+       return ia->configure(ia);
+}
+
+int i2c_adapter_smbus_write(struct i2c_adapter *ia, uint8_t addr,
+                       uint8_t reg, uint8_t *buffer, int len)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->smbus_write)
+               return SBI_ENOSYS;
+
+       return ia->smbus_write(ia, addr, reg, buffer, len);
+}
+
+
+int i2c_adapter_smbus_read(struct i2c_adapter *ia, uint8_t addr,
+                    uint8_t reg, uint8_t *buffer, int len)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->smbus_read)
Quoted 8 lines

lib/utils/i2c/i2c.c line 85

Quoted 4 lines
lib/utils/i2c/i2c.c new file +4 -0
+               return SBI_ENOSYS;
+
+       return ia->smbus_read(ia, addr, reg, buffer, len);
+}  

Again, I don't find those helpers really useful, IMO the adapter should be hidden from the device, see my previous review. But you can convince me we don't need it, I'm open to hearing what you think :)

let's move this to 0 patch disucssion.

Quoted 28 lines

Alex

Quoted 24 lines
lib/utils/i2c/objects.mk new file +10 -1
new file mode 100644
index 0000000..16a70da
@@ -0,0 +1,10 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2021 YADRO
+#
+# Authors:
+#   Nikita Shubin <[email protected]>
+#
+
+libsbiutils-objs-y += i2c/i2c.o
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#5 lore raw

PATCH v2 2/5 lib: utils/i2c: Add generic I2C configuration library

Quoted 80 lines · Nikita Shubin

Hello Alexandre!

Quoted 65 lines · On Tue, 19 Oct 2021 14:04:28 +0200 Alexandre Ghiti…

On Fri, Oct 15, 2021 at 3:20 PM Nikita Shubin
<[email protected]> wrote:>

Quoted 63 lines

From: Nikita Shubin <[email protected]>

Helper library to keep track of registered I2C adapters, identified by dts offset, basic send/read functions and adapter configuration (enable, set dividers, etc...).

---
v1 -> v2:
- switch to lists instead of array
- rename send to write
- use buffers instead of single byte
- add single byte read/write

---
 include/sbi_utils/i2c/i2c.h | 99
+++++++++++++++++++++++++++++++++++++ lib/utils/i2c/i2c.c         |
85 +++++++++++++++++++++++++++++++ lib/utils/i2c/objects.mk    | 10
++++ 3 files changed, 194 insertions(+)
 create mode 100644 include/sbi_utils/i2c/i2c.h
 create mode 100644 lib/utils/i2c/i2c.c
 create mode 100644 lib/utils/i2c/objects.mk
(quoted diff) +0 -0

b/include/sbi_utils/i2c/i2c.h new file mode 100644 index 0000000..76cdb66

include/sbi_utils/i2c/i2c.h new file +35 -0
@@ -0,0 +1,99 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ */
+
+#ifndef __I2C_H__
+#define __I2C_H__
+
+#include <sbi/sbi_types.h>
+#include <sbi/sbi_list.h>
+
+/** Representation of a I2C adapter */
+struct i2c_adapter {
+       /** Pointer to I2C driver owning this I2C adapter */
+       void *driver;
+
+       /** Unique ID of the I2C adapter assigned by the driver */
+       int id;
+
+       /**
+        * Configure I2C adapter
+        *
+        * Enable, set dividers, etc...
+        *
+        * @return 0 on success and negative error code on failure
+        */
+       int (*configure)(struct i2c_adapter *ia);
+
+       /**
+        * Send buffer to given address, register
+        *
Quoted 8 lines
Quoted 5 lines
include/sbi_utils/i2c/i2c.h new file +3 -0
+        * @return 0 on success and negative error code on failure
+        */
+       int (*smbus_write)(struct i2c_adapter *ia, uint8_t addr,

uint8_t reg,
+ uint8_t *buffer, int len);

Why did you rename those functions with smbus prefix? And I would use size_t for the len argument.

Quoted 3 lines

Cause, i think, actually specifying a register to read/write as a part of transaction is an smbus thing, as i stated in cover letter, raw read/write i2c don't use repeated start.

Ok!

Quoted 69 lines

And may be size_t indeed.

Quoted 57 lines
Quoted 57 lines
include/sbi_utils/i2c/i2c.h new file +7 -0
+
+       /**
+        * Read buffer from given address, register
+        *
+        * @return 0 on success and negative error code on failure
+        */
+       int (*smbus_read)(struct i2c_adapter *ia, uint8_t addr,

uint8_t reg,

include/sbi_utils/i2c/i2c.h new file +7 -0
+                   uint8_t *buffer, int len);
+
+       /** List */
+       struct sbi_dlist node;
+};
+
+static inline struct i2c_adapter *to_i2c_adapter(struct sbi_dlist

*node) +{

include/sbi_utils/i2c/i2c.h new file +25 -0
+       return container_of(node, struct i2c_adapter, node);
+}
+
+/** Find a registered I2C adapter */
+struct i2c_adapter *i2c_adapter_find(int id);
+
+/** Register I2C adapter */
+int i2c_adapter_add(struct i2c_adapter *ia);
+
+/** Un-register I2C adapter */
+void i2c_adapter_remove(struct i2c_adapter *ia);
+
+/** Configure I2C adapter prior to send/read */
+int i2c_adapter_configure(struct i2c_adapter *ia);
+
+/** Write to device on I2C adapter bus */
+int i2c_adapter_smbus_write(struct i2c_adapter *ia, uint8_t addr,
+                       uint8_t reg, uint8_t *buffer, int len);
+
+/** Read from device on I2C adapter bus */
+int i2c_adapter_smbus_read(struct i2c_adapter *ia, uint8_t addr,
+                       uint8_t reg, uint8_t *buffer, int len);
+
+/** Write single byte from device on I2C adapter bus */
+static inline int i2c_adapter_smbus_reg_write(struct i2c_adapter

*ia,
+ uint8_t addr, uint8_t reg, uint8_t
val) +{

include/sbi_utils/i2c/i2c.h new file +5 -0
+       return i2c_adapter_smbus_write(ia, addr, reg, &val, 1);
+}
+
+/** Read single byte from device on I2C adapter bus */
+static inline int i2c_adapter_smbus_reg_read(struct i2c_adapter

*ia,
+ uint8_t addr, uint8_t reg, uint8_t
*val) +{

include/sbi_utils/i2c/i2c.h new file +4 -0
+       uint8_t buf;
+       int ret = i2c_adapter_smbus_read(ia, addr, reg, &buf, 1);
+
+       if (ret)
Quoted 8 lines

include/sbi_utils/i2c/i2c.h line 97

Quoted 5 lines
include/sbi_utils/i2c/i2c.h new file +5 -0
+               return ret;
+
+       *val = buf;
+       return 0;
+}

Nice to add the helpers, though I'm not sure "reg_read" is the right suffix as it may not be quite explicit: what about simply read_byte?

Quoted 7 lines

I think byte_read/byte_write should be "raw" and omit the uint8_t reg and have naming like:

i2c_adapter_byte_read
i2c_adapter_byte_write

Without smbus prefix, but that's a bit different story.

As I'm far from being an i2c/smbus expert, are we sure the value of a register always holds in an uint8_t?

Quoted 132 lines
Quoted 92 lines
Quoted 92 lines
include/sbi_utils/i2c/i2c.h new file +2 -0
+
+#endif
lib/utils/i2c/i2c.c new file +81 -0
new file mode 100644
index 0000000..d23ac91
@@ -0,0 +1,85 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ *
+ * derivate: lib/utils/gpio/gpio.c
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#include <sbi/sbi_error.h>
+#include <sbi_utils/i2c/i2c.h>
+
+static SBI_LIST_HEAD(i2c_adapters_list);
+
+struct i2c_adapter *i2c_adapter_find(int id)
+{
+       struct sbi_dlist *pos;
+
+       sbi_list_for_each(pos, &(i2c_adapters_list)) {
+               struct i2c_adapter *adap = to_i2c_adapter(pos);
+
+               if (adap->id == id)
+                       return adap;
+       }
+
+       return NULL;
+}
+
+int i2c_adapter_add(struct i2c_adapter *ia)
+{
+       if (!ia)
+               return SBI_EINVAL;
+
+       if (i2c_adapter_find(ia->id))
+               return SBI_EALREADY;
+
+       sbi_list_add(&(ia->node), &(i2c_adapters_list));
+
+       return 0;
+}
+
+void i2c_adapter_remove(struct i2c_adapter *ia)
+{
+       if (!ia)
+               return;
+
+       sbi_list_del(&(ia->node));
+}
+
+int i2c_adapter_configure(struct i2c_adapter *ia)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->configure)
+               return 0;
+
+       return ia->configure(ia);
+}
+
+int i2c_adapter_smbus_write(struct i2c_adapter *ia, uint8_t addr,
+                       uint8_t reg, uint8_t *buffer, int len)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->smbus_write)
+               return SBI_ENOSYS;
+
+       return ia->smbus_write(ia, addr, reg, buffer, len);
+}
+
+
+int i2c_adapter_smbus_read(struct i2c_adapter *ia, uint8_t addr,
+                    uint8_t reg, uint8_t *buffer, int len)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->smbus_read)
Quoted 8 lines

lib/utils/i2c/i2c.c line 85

Quoted 4 lines
lib/utils/i2c/i2c.c new file +4 -0
+               return SBI_ENOSYS;
+
+       return ia->smbus_read(ia, addr, reg, buffer, len);
+}

Again, I don't find those helpers really useful, IMO the adapter should be hidden from the device, see my previous review. But you can convince me we don't need it, I'm open to hearing what you think :)

let's move this to 0 patch disucssion.

Quoted 28 lines

Alex

Quoted 24 lines
lib/utils/i2c/objects.mk new file +10 -1
new file mode 100644
index 0000000..16a70da
@@ -0,0 +1,10 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2021 YADRO
+#
+# Authors:
+#   Nikita Shubin <[email protected]>
+#
+
+libsbiutils-objs-y += i2c/i2c.o
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#6 lore raw

PATCH v2 3/5 lib: utils/i2c: Add simple FDT based I2C framework

In reply to Nikita Shubin

From: Nikita Shubin <[email protected]>

FDT based I2C framework on the top of I2C library.

The drivers are probed on demand by fdt_i2c_adapter_get function.

---
 include/sbi_utils/i2c/fdt_i2c.h |  26 ++++++++
 lib/utils/i2c/fdt_i2c.c         | 108 ++++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk        |   1 +
 3 files changed, 135 insertions(+)
 create mode 100644 include/sbi_utils/i2c/fdt_i2c.h
 create mode 100644 lib/utils/i2c/fdt_i2c.c
include/sbi_utils/i2c/fdt_i2c.h new file +26 -0
new file mode 100644
index 0000000..1f41a0f
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ */
+
+#ifndef __FDT_I2C_H__
+#define __FDT_I2C_H__
+
+#include <sbi_utils/i2c/i2c.h>
+
+/** FDT based I2C adapter driver */
+struct fdt_i2c_adapter {
+	const struct fdt_match *match_table;
+	int (*init)(void *fdt, int nodeoff,
+		    const struct fdt_match *match);
+};
+
+/** Get I2C adapter identified by nodeoff */
+int fdt_i2c_adapter_get(void *fdt, int nodeoff,
+			struct i2c_adapter **out_adapter);
+
+#endif
lib/utils/i2c/fdt_i2c.c new file +108 -0

108 lines not on this page · raw message

lib/utils/i2c/objects.mk +1 -0
index 16a70da..06baa65 100644
@@ -8,3 +8,4 @@
 #
 
 libsbiutils-objs-y += i2c/i2c.o
+libsbiutils-objs-y += i2c/fdt_i2c.o
Signature · 1 line
2.31.1
#7 lore raw

PATCH v2 3/5 lib: utils/i2c: Add simple FDT based I2C framework

Quoted 83 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

FDT based I2C framework on the top of I2C library.

The drivers are probed on demand by fdt_i2c_adapter_get function.

---
 include/sbi_utils/i2c/fdt_i2c.h |  26 ++++++++
 lib/utils/i2c/fdt_i2c.c         | 108 ++++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk        |   1 +
 3 files changed, 135 insertions(+)
 create mode 100644 include/sbi_utils/i2c/fdt_i2c.h
 create mode 100644 lib/utils/i2c/fdt_i2c.c
include/sbi_utils/i2c/fdt_i2c.h new file +26 -0
new file mode 100644
index 0000000..1f41a0f
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ */
+
+#ifndef __FDT_I2C_H__
+#define __FDT_I2C_H__
+
+#include <sbi_utils/i2c/i2c.h>
+
+/** FDT based I2C adapter driver */
+struct fdt_i2c_adapter {
+       const struct fdt_match *match_table;
+       int (*init)(void *fdt, int nodeoff,
+                   const struct fdt_match *match);
+};
+
+/** Get I2C adapter identified by nodeoff */
+int fdt_i2c_adapter_get(void *fdt, int nodeoff,
+                       struct i2c_adapter **out_adapter);
+
+#endif
lib/utils/i2c/fdt_i2c.c new file +28 -0
new file mode 100644
index 0000000..fce6d73
@@ -0,0 +1,108 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ *
+ * derivate: lib/utils/gpio/fdt_gpio.c
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#include <libfdt.h>
+#include <sbi/sbi_error.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/i2c/fdt_i2c.h>
+
+#include <sbi/sbi_console.h>
+
+static struct fdt_i2c_adapter *i2c_adapter_drivers[] = {
+};
+
+static struct fdt_i2c_adapter
+       *fdt_i2c_adapter_driver(struct i2c_adapter *adapter)
+{
+       int pos;
+

lib/utils/i2c/fdt_i2c.c line 38

Quoted 10 lines
lib/utils/i2c/fdt_i2c.c new file +10 -0
+       if (!adapter)
+               return NULL;
+
+       for (pos = 0; pos < array_size(i2c_adapter_drivers); pos++) {
+               if (adapter->driver == i2c_adapter_drivers[pos])
+                       return i2c_adapter_drivers[pos];
+       }
+
+       return NULL;
+}

I don't see the point of this function since it simply returns adapter->driver right?

Quoted 87 lines
lib/utils/i2c/fdt_i2c.c new file +70 -0
+
+static int fdt_i2c_adapter_init(void *fdt, int nodeoff)
+{
+       int pos, rc;
+       struct fdt_i2c_adapter *drv;
+       const struct fdt_match *match;
+
+       /* Try all I2C drivers one-by-one */
+       for (pos = 0; pos < array_size(i2c_adapter_drivers); pos++) {
+               drv = i2c_adapter_drivers[pos];
+               match = fdt_match_node(fdt, nodeoff, drv->match_table);
+               if (match && drv->init) {
+                       rc = drv->init(fdt, nodeoff, match);
+                       if (rc == SBI_ENODEV)
+                               continue;
+                       if (rc)
+                               return rc;
+                       return 0;
+               }
+       }
+
+       return SBI_ENOSYS;
+}
+
+static int fdt_i2c_adapter_find(void *fdt, int nodeoff,
+                               struct i2c_adapter **out_adapter)
+{
+       int rc;
+       struct i2c_adapter *adapter = i2c_adapter_find(nodeoff);
+
+       if (!adapter) {
+               /* I2C adapter not found so initialize matching driver */
+               rc = fdt_i2c_adapter_init(fdt, nodeoff);
+               if (rc)
+                       return rc;
+
+               /* Try to find I2C adapter again */
+               adapter = i2c_adapter_find(nodeoff);
+               if (!adapter)
+                       return SBI_ENOSYS;
+       }
+
+       if (out_adapter)
+               *out_adapter = adapter;
+
+       return 0;
+}
+
+int fdt_i2c_adapter_get(void *fdt, int nodeoff,
+                       struct i2c_adapter **out_adapter)
+{
+       int rc;
+       struct i2c_adapter *adapter;
+       struct fdt_i2c_adapter *drv;
+
+       if (!fdt || (nodeoff < 0) || !out_adapter)
+               return SBI_EINVAL;
+
+       rc = fdt_i2c_adapter_find(fdt, nodeoff, &adapter);
+       if (rc)
+               return rc;
+
+       drv = fdt_i2c_adapter_driver(adapter);
+       if (!drv)
+               return SBI_ENOSYS;
+
+       *out_adapter = adapter;
+
+       return 0;
+}
lib/utils/i2c/objects.mk +1 -1
index 16a70da..06baa65 100644
@@ -8,3 +8,4 @@
 #
 
 libsbiutils-objs-y += i2c/i2c.o
+libsbiutils-objs-y += i2c/fdt_i2c.o
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#8 lore raw

PATCH v2 3/5 lib: utils/i2c: Add simple FDT based I2C framework

Hello Alexandre!

Quoted 91 lines · On Tue, 19 Oct 2021 14:05:58 +0200 Alexandre Ghiti…
Quoted 89 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

FDT based I2C framework on the top of I2C library.

The drivers are probed on demand by fdt_i2c_adapter_get function.

---
 include/sbi_utils/i2c/fdt_i2c.h |  26 ++++++++
 lib/utils/i2c/fdt_i2c.c         | 108
++++++++++++++++++++++++++++++++ lib/utils/i2c/objects.mk        |
 1 + 3 files changed, 135 insertions(+)
 create mode 100644 include/sbi_utils/i2c/fdt_i2c.h
 create mode 100644 lib/utils/i2c/fdt_i2c.c
(quoted diff) +0 -0

b/include/sbi_utils/i2c/fdt_i2c.h new file mode 100644 index 0000000..1f41a0f

include/sbi_utils/i2c/fdt_i2c.h new file +26 -0
@@ -0,0 +1,26 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ */
+
+#ifndef __FDT_I2C_H__
+#define __FDT_I2C_H__
+
+#include <sbi_utils/i2c/i2c.h>
+
+/** FDT based I2C adapter driver */
+struct fdt_i2c_adapter {
+       const struct fdt_match *match_table;
+       int (*init)(void *fdt, int nodeoff,
+                   const struct fdt_match *match);
+};
+
+/** Get I2C adapter identified by nodeoff */
+int fdt_i2c_adapter_get(void *fdt, int nodeoff,
+                       struct i2c_adapter **out_adapter);
+
+#endif
lib/utils/i2c/fdt_i2c.c new file +32 -0
new file mode 100644
index 0000000..fce6d73
@@ -0,0 +1,108 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ *
+ * derivate: lib/utils/gpio/fdt_gpio.c
+ * Authors:
+ *   Anup Patel <[email protected]>
+ */
+
+#include <libfdt.h>
+#include <sbi/sbi_error.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/i2c/fdt_i2c.h>
+
+#include <sbi/sbi_console.h>
+
+static struct fdt_i2c_adapter *i2c_adapter_drivers[] = {
+};
+
+static struct fdt_i2c_adapter
+       *fdt_i2c_adapter_driver(struct i2c_adapter *adapter)
+{
+       int pos;
+
+       if (!adapter)
+               return NULL;
+
+       for (pos = 0; pos < array_size(i2c_adapter_drivers); pos++)

{
+ if (adapter->driver == i2c_adapter_drivers[pos])

Quoted 8 lines

lib/utils/i2c/fdt_i2c.c line 38

Quoted 5 lines
lib/utils/i2c/fdt_i2c.c new file +5 -0
+                       return i2c_adapter_drivers[pos];
+       }
+
+       return NULL;
+}  

I don't see the point of this function since it simply returns adapter->driver right?

It looks like you are right. This can be ommited as well as fdt_i2c_adapter_driver check in fdt_i2c_adapter_get.

Quoted 91 lines
Quoted 91 lines
lib/utils/i2c/fdt_i2c.c new file +9 -0
+
+static int fdt_i2c_adapter_init(void *fdt, int nodeoff)
+{
+       int pos, rc;
+       struct fdt_i2c_adapter *drv;
+       const struct fdt_match *match;
+
+       /* Try all I2C drivers one-by-one */
+       for (pos = 0; pos < array_size(i2c_adapter_drivers); pos++)

{

lib/utils/i2c/fdt_i2c.c new file +2 -0
+               drv = i2c_adapter_drivers[pos];
+               match = fdt_match_node(fdt, nodeoff,

drv->match_table);

lib/utils/i2c/fdt_i2c.c new file +21 -0
+               if (match && drv->init) {
+                       rc = drv->init(fdt, nodeoff, match);
+                       if (rc == SBI_ENODEV)
+                               continue;
+                       if (rc)
+                               return rc;
+                       return 0;
+               }
+       }
+
+       return SBI_ENOSYS;
+}
+
+static int fdt_i2c_adapter_find(void *fdt, int nodeoff,
+                               struct i2c_adapter **out_adapter)
+{
+       int rc;
+       struct i2c_adapter *adapter = i2c_adapter_find(nodeoff);
+
+       if (!adapter) {
+               /* I2C adapter not found so initialize matching

driver */

lib/utils/i2c/fdt_i2c.c new file +38 -0
+               rc = fdt_i2c_adapter_init(fdt, nodeoff);
+               if (rc)
+                       return rc;
+
+               /* Try to find I2C adapter again */
+               adapter = i2c_adapter_find(nodeoff);
+               if (!adapter)
+                       return SBI_ENOSYS;
+       }
+
+       if (out_adapter)
+               *out_adapter = adapter;
+
+       return 0;
+}
+
+int fdt_i2c_adapter_get(void *fdt, int nodeoff,
+                       struct i2c_adapter **out_adapter)
+{
+       int rc;
+       struct i2c_adapter *adapter;
+       struct fdt_i2c_adapter *drv;
+
+       if (!fdt || (nodeoff < 0) || !out_adapter)
+               return SBI_EINVAL;
+
+       rc = fdt_i2c_adapter_find(fdt, nodeoff, &adapter);
+       if (rc)
+               return rc;
+
+       drv = fdt_i2c_adapter_driver(adapter);
+       if (!drv)
+               return SBI_ENOSYS;
+
+       *out_adapter = adapter;
+
+       return 0;
+}
lib/utils/i2c/objects.mk +1 -1
index 16a70da..06baa65 100644
@@ -8,3 +8,4 @@
 #
 
 libsbiutils-objs-y += i2c/i2c.o
+libsbiutils-objs-y += i2c/fdt_i2c.o
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#9 lore raw

PATCH v2 4/5 lib: utils/i2c: Add minimal SiFive I2C driver

In reply to Nikita Shubin

From: Nikita Shubin <[email protected]>

Minimum SiFive I2C driver to read/send bytes over I2C bus.

This allows querying information and perform operation of onboard PMIC, as well as power-off and reset.

---
v1 -> v2:
- setreg/getreg add sifive_i2c_ prefix
- use sbi_timer_mdelay for delay
- renamed send to write
- reworked read/write to support buffers
- renamed read/write to smbus_read/smbus_write

---
 lib/utils/i2c/fdt_i2c.c        |   3 +
 lib/utils/i2c/fdt_i2c_sifive.c | 271 +++++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk       |   1 +
 3 files changed, 275 insertions(+)
 create mode 100644 lib/utils/i2c/fdt_i2c_sifive.c
lib/utils/i2c/fdt_i2c.c +3 -0
index fce6d73..bbf9df6 100644
@@ -18,7 +18,10 @@
 
 #include <sbi/sbi_console.h>
 
+extern struct fdt_i2c_adapter fdt_i2c_adapter_sifive;
+
 static struct fdt_i2c_adapter *i2c_adapter_drivers[] = {
+	&fdt_i2c_adapter_sifive
 };
 
 static struct fdt_i2c_adapter
lib/utils/i2c/fdt_i2c_sifive.c new file +271 -0

271 lines not on this page · raw message

lib/utils/i2c/objects.mk +1 -0
index 06baa65..d52ab18 100644
@@ -9,3 +9,4 @@
 
 libsbiutils-objs-y += i2c/i2c.o
 libsbiutils-objs-y += i2c/fdt_i2c.o
+libsbiutils-objs-y += i2c/fdt_i2c_sifive.o
Signature · 1 line
2.31.1
#10 lore raw

PATCH v2 4/5 lib: utils/i2c: Add minimal SiFive I2C driver

Quoted 99 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

Minimum SiFive I2C driver to read/send bytes over I2C bus.

This allows querying information and perform operation of onboard PMIC, as well as power-off and reset.

---
v1 -> v2:
- setreg/getreg add sifive_i2c_ prefix
- use sbi_timer_mdelay for delay
- renamed send to write
- reworked read/write to support buffers
- renamed read/write to smbus_read/smbus_write

---
 lib/utils/i2c/fdt_i2c.c        |   3 +
 lib/utils/i2c/fdt_i2c_sifive.c | 271 +++++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk       |   1 +
 3 files changed, 275 insertions(+)
 create mode 100644 lib/utils/i2c/fdt_i2c_sifive.c
lib/utils/i2c/fdt_i2c.c +3 -0
index fce6d73..bbf9df6 100644
@@ -18,7 +18,10 @@
 
 #include <sbi/sbi_console.h>
 
+extern struct fdt_i2c_adapter fdt_i2c_adapter_sifive;
+
 static struct fdt_i2c_adapter *i2c_adapter_drivers[] = {
+       &fdt_i2c_adapter_sifive
 };
 
 static struct fdt_i2c_adapter
lib/utils/i2c/fdt_i2c_sifive.c new file +55 -0
new file mode 100644
index 0000000..aeb67ee
@@ -0,0 +1,271 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ */
+
+#include <sbi/riscv_io.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_timer.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/i2c/fdt_i2c.h>
+
+#define SIFIVE_I2C_ADAPTER_MAX 2
+
+#define SIFIVE_I2C_PRELO       0x00
+#define SIFIVE_I2C_PREHI       0x04
+#define SIFIVE_I2C_CTR         0x08
+#define SIFIVE_I2C_TXR         0x00c
+#define SIFIVE_I2C_RXR         SIFIVE_I2C_TXR
+#define SIFIVE_I2C_CR          0x010
+#define SIFIVE_I2C_SR          SIFIVE_I2C_CR
+
+#define SIFIVE_I2C_CTR_IEN     (1 << 6)
+#define SIFIVE_I2C_CTR_EN      (1 << 7)
+
+#define SIFIVE_I2C_CMD_IACK    (1 << 0)
+#define SIFIVE_I2C_CMD_ACK     (1 << 3)
+#define SIFIVE_I2C_CMD_WR      (1 << 4)
+#define SIFIVE_I2C_CMD_RD      (1 << 5)
+#define SIFIVE_I2C_CMD_STO     (1 << 6)
+#define SIFIVE_I2C_CMD_STA     (1 << 7)
+
+#define SIFIVE_I2C_STATUS_IF   (1 << 0)
+#define SIFIVE_I2C_STATUS_TIP  (1 << 1)
+#define SIFIVE_I2C_STATUS_AL   (1 << 5)
+#define SIFIVE_I2C_STATUS_BUSY (1 << 6)
+#define SIFIVE_I2C_STATUS_RXACK        (1 << 7)
+
+#define SIFIVE_I2C_WRITE_BIT   (0 << 0)
+#define SIFIVE_I2C_READ_BIT    (1 << 0)
+
+struct sifive_i2c_adapter {
+       unsigned long addr;
+       struct i2c_adapter adapter;
+};
+
+static unsigned int sifive_i2c_adapter_count;
+static struct sifive_i2c_adapter sifive_i2c_adapter_array[SIFIVE_I2C_ADAPTER_MAX];
+
+extern struct fdt_i2c_adapter fdt_i2c_adapter_sifive;
+
+static inline void sifive_i2c_setreg(struct sifive_i2c_adapter *adap,

lib/utils/i2c/fdt_i2c_sifive.c line 65

Quoted 10 lines
lib/utils/i2c/fdt_i2c_sifive.c new file +10 -0
+                                    int reg, u8 value)
+{
+       writel(value, (volatile void *)adap->addr + reg);
+}
+
+static inline u8 sifive_i2c_getreg(struct sifive_i2c_adapter *adap, int reg)
+{
+       return readl((volatile void *)adap->addr + reg);
+}
+

reg is defined as a uint8_t in calling functions and here as an int.

Quoted 223 lines
lib/utils/i2c/fdt_i2c_sifive.c new file +206 -0
+static int sifive_i2c_adapter_rxack(struct sifive_i2c_adapter *adap)
+{
+       uint8_t val = sifive_i2c_getreg(adap, SIFIVE_I2C_SR);
+
+       if (val & SIFIVE_I2C_STATUS_RXACK)
+               return SBI_EIO;
+
+       return 0;
+}
+
+static int sifive_i2c_adapter_poll(struct sifive_i2c_adapter *adap, uint32_t mask)
+{
+       int max_retry = 5;
+       uint8_t val;
+
+       do {
+               val = sifive_i2c_getreg(adap, SIFIVE_I2C_SR);
+               sbi_timer_mdelay(100);
+       } while ((val & mask) && (max_retry--) > 0);
+
+       if (max_retry <= 0)
+               return SBI_ETIMEDOUT;
+
+       return 0;
+}
+
+#define sifive_i2c_adapter_poll_tip(adap) sifive_i2c_adapter_poll(adap, SIFIVE_I2C_STATUS_TIP)
+#define sifive_i2c_adapter_poll_busy(adap) sifive_i2c_adapter_poll(adap, SIFIVE_I2C_STATUS_BUSY)
+
+static int sifive_i2c_adapter_start(struct sifive_i2c_adapter *adap, uint8_t addr, uint8_t bit)
+{
+       uint8_t val = (addr << 1) | bit;
+
+       sifive_i2c_setreg(adap, SIFIVE_I2C_TXR, val);
+       val = SIFIVE_I2C_CMD_STA | SIFIVE_I2C_CMD_WR | SIFIVE_I2C_CMD_IACK;
+       sifive_i2c_setreg(adap, SIFIVE_I2C_CR, val);
+
+       return sifive_i2c_adapter_poll_tip(adap);
+}
+
+static int sifive_i2c_adapter_smbus_write(struct i2c_adapter *ia,
+                                       uint8_t addr, uint8_t reg,
+                                       uint8_t *buffer, int len)
+{
+       struct sifive_i2c_adapter *adap =
+               container_of(ia, struct sifive_i2c_adapter, adapter);
+       int rc = sifive_i2c_adapter_start(adap, addr, SIFIVE_I2C_WRITE_BIT);
+
+       if (rc)
+               return rc;
+
+       rc = sifive_i2c_adapter_rxack(adap);
+       if (rc)
+               return rc;
+
+       /* set register address */
+       sifive_i2c_setreg(adap, SIFIVE_I2C_TXR, reg);
+       sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_WR | SIFIVE_I2C_CMD_IACK);
+       rc = sifive_i2c_adapter_poll_tip(adap);
+       if (rc)
+               return rc;
+
+       rc = sifive_i2c_adapter_rxack(adap);
+       if (rc)
+               return rc;
+
+       /* set value */
+       while (len) {
+               sifive_i2c_setreg(adap, SIFIVE_I2C_TXR, *buffer);
+               sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_WR | SIFIVE_I2C_CMD_IACK);
+
+               rc = sifive_i2c_adapter_poll_tip(adap);
+               if (rc)
+                       return rc;
+
+               rc = sifive_i2c_adapter_rxack(adap);
+               if (rc)
+                       return rc;
+
+               buffer++;
+               len--;
+       }
+
+       sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_STO | SIFIVE_I2C_CMD_IACK);
+
+       /* poll BUSY instead of ACK*/
+       rc = sifive_i2c_adapter_poll_busy(adap);
+       if (rc)
+               return rc;
+
+       sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_IACK);
+
+       return 0;
+}
+
+static int sifive_i2c_adapter_smbus_read(struct i2c_adapter *ia,
+                                       uint8_t addr, uint8_t reg,
+                                       uint8_t *buffer, int len)
+{
+       struct sifive_i2c_adapter *adap =
+               container_of(ia, struct sifive_i2c_adapter, adapter);
+       int rc;
+
+       rc = sifive_i2c_adapter_start(adap, addr, SIFIVE_I2C_WRITE_BIT);
+       if (rc)
+               return rc;
+
+       rc = sifive_i2c_adapter_rxack(adap);
+       if (rc)
+               return rc;
+
+       sifive_i2c_setreg(adap, SIFIVE_I2C_TXR, reg);
+       sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_WR | SIFIVE_I2C_CMD_IACK);
+
+       rc = sifive_i2c_adapter_poll_tip(adap);
+       if (rc)
+               return rc;
+
+       rc = sifive_i2c_adapter_rxack(adap);
+       if (rc)
+               return rc;
+
+       /* setting addr with high 0 bit */
+       rc = sifive_i2c_adapter_start(adap, addr, SIFIVE_I2C_READ_BIT);
+       if (rc)
+               return rc;
+
+       rc = sifive_i2c_adapter_rxack(adap);
+       if (rc)
+               return rc;
+
+       while (len) {
+               if (len == 1)
+                       sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_ACK | SIFIVE_I2C_CMD_RD | SIFIVE_I2C_CMD_IACK);
+               else
+                       sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_RD | SIFIVE_I2C_CMD_IACK);
+
+               rc = sifive_i2c_adapter_poll_tip(adap);
+               if (rc)
+                       return rc;
+
+               *buffer = sifive_i2c_getreg(adap, SIFIVE_I2C_RXR);
+               buffer++;
+               len--;
+       }
+
+       sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_STO | SIFIVE_I2C_CMD_IACK);
+       rc = sifive_i2c_adapter_poll_busy(adap);
+       if (rc)
+               return rc;
+
+       sifive_i2c_setreg(adap, SIFIVE_I2C_CR, SIFIVE_I2C_CMD_IACK);
+
+       return 0;
+}
+
+static int sifive_i2c_adapter_configure(struct i2c_adapter *ia)
+{
+       struct sifive_i2c_adapter *adap =
+               container_of(ia, struct sifive_i2c_adapter, adapter);
+
+       /* enable controller/disable interrupts */
+       sifive_i2c_setreg(adap, SIFIVE_I2C_CTR, SIFIVE_I2C_CTR_EN);
+
+       return 0;
+}
+
+static int sifive_i2c_init(void *fdt, int nodeoff,
+                           const struct fdt_match *match)
+{
+       int rc;
+       struct sifive_i2c_adapter *adapter;
+       uint64_t addr;
+
+       if (sifive_i2c_adapter_count >= SIFIVE_I2C_ADAPTER_MAX)
+               return SBI_ENOSPC;
+
+       adapter = &sifive_i2c_adapter_array[sifive_i2c_adapter_count];
+
+       rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
+       if (rc)
+               return rc;
+
+       adapter->addr = addr;
+       adapter->adapter.driver = &fdt_i2c_adapter_sifive;
+       adapter->adapter.id = nodeoff;
+       adapter->adapter.smbus_write = sifive_i2c_adapter_smbus_write;
+       adapter->adapter.smbus_read = sifive_i2c_adapter_smbus_read;
+       adapter->adapter.configure = sifive_i2c_adapter_configure;
+       rc = i2c_adapter_add(&adapter->adapter);
+       if (rc)
+               return rc;
+
+       sifive_i2c_adapter_count++;
+       return 0;
+}
+
+static const struct fdt_match sifive_i2c_match[] = {
+       { .compatible = "sifive,i2c0" },
+       { },
+};
+
+struct fdt_i2c_adapter fdt_i2c_adapter_sifive = {
+       .match_table = sifive_i2c_match,
+       .init = sifive_i2c_init,
+};
lib/utils/i2c/objects.mk +1 -1
index 06baa65..d52ab18 100644
@@ -9,3 +9,4 @@
 
 libsbiutils-objs-y += i2c/i2c.o
 libsbiutils-objs-y += i2c/fdt_i2c.o
+libsbiutils-objs-y += i2c/fdt_i2c_sifive.o
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#11 lore raw

PATCH v2 4/5 lib: utils/i2c: Add minimal SiFive I2C driver

Hello Alexandre!

Quoted 6 lines · On Tue, 19 Oct 2021 14:34:30 +0200 Alexandre Ghiti…
Quoted 6 lines

+static inline void sifive_i2c_setreg(struct sifive_i2c_adapter *adap,

lib/utils/i2c/fdt_i2c_sifive.c new file +4 -0
+                                    int reg, u8 value)
+{
+       writel(value, (volatile void *)adap->addr + reg);
+}
Quoted 8 lines

lib/utils/i2c/fdt_i2c_sifive.c line 65

Quoted 6 lines
(quoted diff) +2 -0
+
+static inline u8 sifive_i2c_getreg(struct sifive_i2c_adapter

*adap, int reg) +{

lib/utils/i2c/fdt_i2c_sifive.c new file +3 -0
+       return readl((volatile void *)adap->addr + reg);
+}
+  

reg is defined as a uint8_t in calling functions and here as an int.

Agreed.

#12 lore raw

PATCH v2 5/5 lib: utils/reset: Add generic da9063 reset driver

In reply to Nikita Shubin

From: Nikita Shubin <[email protected]>

da9063 PMIC can be used to reset/shutdown the
Sifive Unmatched board.

shutdown is done simply by writing SHUTDOWN bit to DA9063_REG_CONTROL_F register.

reset via setting WAKEUP bit in DA9063_REG_CONTROL_F register followed by masking POWER and POWER1 domains and setting STANDBY bit in DA9063_REG_CONTROL_A, originally discovered by Alexandre Ghiti on linux-riscv maillists.

---
v1 -> v2:
- changed to sub dts node da9063-reset instead of relying on dlg,da9063
- added priority dts node
- return priority system_reset_check either readed from dts or default 1

---
 lib/utils/reset/fdt_reset.c        |   2 +
 lib/utils/reset/fdt_reset_da9063.c | 214 +++++++++++++++++++++++++++++
 lib/utils/reset/objects.mk         |   1 +
 3 files changed, 217 insertions(+)
 create mode 100644 lib/utils/reset/fdt_reset_da9063.c
lib/utils/reset/fdt_reset.c +2 -0
index 168bb0c..92f37b0 100644
@@ -18,6 +18,7 @@ extern struct fdt_reset fdt_reset_htif;
 extern struct fdt_reset fdt_reset_sifive_test;
 extern struct fdt_reset fdt_reset_sunxi_wdt;
 extern struct fdt_reset fdt_reset_thead;
+extern struct fdt_reset fdt_reset_da9063;
 
 static struct fdt_reset *reset_drivers[] = {
 	&fdt_poweroff_gpio,
@@ -26,6 +27,7 @@ static struct fdt_reset *reset_drivers[] = {
 	&fdt_reset_sifive_test,
 	&fdt_reset_sunxi_wdt,
 	&fdt_reset_thead,
+	&fdt_reset_da9063,
 };
 
 int fdt_reset_init(void)
lib/utils/reset/fdt_reset_da9063.c new file +214 -0

214 lines not on this page · raw message

lib/utils/reset/objects.mk +1 -0
index 6c95db3..cfe4c09 100644
@@ -14,3 +14,4 @@ libsbiutils-objs-y += reset/fdt_reset_sifive_test.o
 libsbiutils-objs-y += reset/fdt_reset_sunxi_wdt.o
 libsbiutils-objs-y += reset/fdt_reset_thead.o
 libsbiutils-objs-y += reset/fdt_reset_thead_asm.o
+libsbiutils-objs-y += reset/fdt_reset_da9063.o
Signature · 1 line
2.31.1
#13 lore raw

PATCH v2 5/5 lib: utils/reset: Add generic da9063 reset driver

Quoted 253 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

da9063 PMIC can be used to reset/shutdown the
Sifive Unmatched board.

shutdown is done simply by writing SHUTDOWN bit to DA9063_REG_CONTROL_F register.

reset via setting WAKEUP bit in DA9063_REG_CONTROL_F register followed by masking POWER and POWER1 domains and setting STANDBY bit in DA9063_REG_CONTROL_A, originally discovered by Alexandre Ghiti on linux-riscv maillists.

---
v1 -> v2:
- changed to sub dts node da9063-reset instead of relying on dlg,da9063
- added priority dts node
- return priority system_reset_check either readed from dts or default 1

---
 lib/utils/reset/fdt_reset.c        |   2 +
 lib/utils/reset/fdt_reset_da9063.c | 214 +++++++++++++++++++++++++++++
 lib/utils/reset/objects.mk         |   1 +
 3 files changed, 217 insertions(+)
 create mode 100644 lib/utils/reset/fdt_reset_da9063.c
lib/utils/reset/fdt_reset.c +2 -0
index 168bb0c..92f37b0 100644
@@ -18,6 +18,7 @@ extern struct fdt_reset fdt_reset_htif;
 extern struct fdt_reset fdt_reset_sifive_test;
 extern struct fdt_reset fdt_reset_sunxi_wdt;
 extern struct fdt_reset fdt_reset_thead;
+extern struct fdt_reset fdt_reset_da9063;
 
 static struct fdt_reset *reset_drivers[] = {
        &fdt_poweroff_gpio,
@@ -26,6 +27,7 @@ static struct fdt_reset *reset_drivers[] = {
        &fdt_reset_sifive_test,
        &fdt_reset_sunxi_wdt,
        &fdt_reset_thead,
+       &fdt_reset_da9063,
 };
 
 int fdt_reset_init(void)
lib/utils/reset/fdt_reset_da9063.c new file +199 -0
new file mode 100644
index 0000000..e3c9ced
@@ -0,0 +1,214 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ */
+
+#include <libfdt.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_ecall_interface.h>
+#include <sbi/sbi_hart.h>
+#include <sbi/sbi_system.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/i2c/fdt_i2c.h>
+#include <sbi_utils/reset/fdt_reset.h>
+
+#define DA9063_REG_PAGE_CON            0x00
+#define DA9063_REG_CONTROL_A           0x0e
+#define DA9063_REG_CONTROL_C           0x10
+#define DA9063_REG_CONTROL_F           0x13
+#define DA9063_REG_DEVICE_ID           0x81
+
+#define DA9063_CONTROL_A_CP_EN         (1 << 7)
+#define DA9063_CONTROL_A_M_POWER1_EN   (1 << 6)
+#define DA9063_CONTROL_A_M_POWER_EN    (1 << 5)
+#define DA9063_CONTROL_A_M_SYSTEM_EN   (1 << 4)
+#define DA9063_CONTROL_A_STANDBY       (1 << 3)
+#define DA9063_CONTROL_A_POWER1_EN     (1 << 2)
+#define DA9063_CONTROL_A_POWER_EN      (1 << 1)
+#define DA9063_CONTROL_A_SYSTEM_EN     (1 << 0)
+
+#define DA9063_CONTROL_F_WAKEUP                (1 << 2)
+#define DA9063_CONTROL_F_SHUTDOWN      (1 << 1)
+#define DA9063_CONTROL_F_WATCHDOG      (1 << 0)
+
+#define DA9063_CONTROL_C_DEF_SUPPLY    (1 << 7)
+#define DA9063_CONTROL_C_SLEW_RATE     (1 << 4)
+#define DA9063_CONTROL_C_OTPREAD_EN    (1 << 3)
+#define DA9063_CONTROL_C_AUTO_BOOT     (1 << 2)
+#define DA9063_CONTROL_C_DEBOUNCING    (1 << 0)
+
+#define PMIC_CHIP_ID_DA9063            0x61
+
+static struct {
+       struct i2c_adapter *adapter;
+       uint32_t reg;
+       u8 priority;
+} da9063 = {
+       .priority = 1
+};
+
+static int da9063_system_reset_check(u32 type, u32 reason)
+{
+       switch (type) {
+       case SBI_SRST_RESET_TYPE_SHUTDOWN:
+       case SBI_SRST_RESET_TYPE_COLD_REBOOT:
+       case SBI_SRST_RESET_TYPE_WARM_REBOOT:
+               return da9063.priority;
+       }
+
+       return 0;
+}
+
+static inline int da9063_sanity_check(struct i2c_adapter *adap, uint32_t reg)
+{
+       uint8_t val;
+       int rc = i2c_adapter_smbus_reg_write(adap, reg, DA9063_REG_PAGE_CON, 0x02);
+
+       if (rc)
+               return rc;
+
+       /* check set page*/
+       rc = i2c_adapter_smbus_reg_read(adap, reg, 0x0, &val);
+       if (rc)
+               return rc;
+
+       if (val != 0x02)
+               return SBI_ENODEV;
+
+       /* read and check device id */
+       rc = i2c_adapter_smbus_reg_read(adap, reg, DA9063_REG_DEVICE_ID, &val);
+       if (rc)
+               return rc;
+
+       if (val != PMIC_CHIP_ID_DA9063)
+               return SBI_ENODEV;
+
+       return 0;
+}
+
+static inline int da9063_shutdown(struct i2c_adapter *adap, uint32_t reg)
+{
+       int rc = i2c_adapter_smbus_reg_write(adap, da9063.reg,
+                                       DA9063_REG_PAGE_CON, 0x00);
+
+       if (rc)
+               return rc;
+
+       return i2c_adapter_smbus_reg_write(adap, da9063.reg,
+                               DA9063_REG_CONTROL_F, DA9063_CONTROL_F_SHUTDOWN);
+}
+
+static inline int da9063_reset(struct i2c_adapter *adap, uint32_t reg)
+{
+       int rc = i2c_adapter_smbus_reg_write(adap, da9063.reg,
+                                       DA9063_REG_PAGE_CON, 0x00);
+
+       if (rc)
+               return rc;
+
+       rc = i2c_adapter_smbus_reg_write(adap, da9063.reg,
+                             DA9063_REG_CONTROL_F, DA9063_CONTROL_F_WAKEUP);
+       if (rc)
+               return rc;
+
+       return i2c_adapter_smbus_reg_write(adap, da9063.reg,
+                               DA9063_REG_CONTROL_A,
+                               DA9063_CONTROL_A_M_POWER1_EN |
+                               DA9063_CONTROL_A_M_POWER_EN |
+                               DA9063_CONTROL_A_STANDBY);
+}
+
+static void da9063_system_reset(u32 type, u32 reason)
+{
+       struct i2c_adapter *adap = da9063.adapter;
+       uint32_t reg = da9063.reg;
+       int rc;
+
+       if (adap) {
+               /* may include clock init */
+               i2c_adapter_configure(adap);
+
+               /* sanity check */
+               rc = da9063_sanity_check(adap, reg);
+               if (rc) {
+                       sbi_printf("%s: chip is not da9063 PMIC\n", __func__);
+                       goto skip_reset;
+               }
+
+               switch (type) {
+               case SBI_SRST_RESET_TYPE_SHUTDOWN:
+                       da9063_shutdown(adap, reg);
+                       break;
+               case SBI_SRST_RESET_TYPE_COLD_REBOOT:
+               case SBI_SRST_RESET_TYPE_WARM_REBOOT:
+                       da9063_reset(adap, reg);
+                       break;
+               }
+
+               while
+                       (1);
+skip_reset:
+       }
+
+       sbi_hart_hang();
+}
+
+static struct sbi_system_reset_device da9063_reset_i2c = {
+       .name = "da9063-reset",
+       .system_reset_check = da9063_system_reset_check,
+       .system_reset = da9063_system_reset
+};
+
+static int da9063_reset_init(void *fdt, int nodeoff,
+                          const struct fdt_match *match)
+{
+       int rc, i2c_dev, i2c_bus, len;
+       const fdt32_t *val;
+       struct i2c_adapter *adapter;
+       uint64_t addr;
+
+       /* find dlg,da9063 parent node */
+       i2c_dev = fdt_parent_offset(fdt, nodeoff);
+       if (i2c_dev < 0)
+               return i2c_dev;
+
+       rc = fdt_get_node_addr_size(fdt, i2c_dev, 0, &addr, NULL);
+       if (rc)
+               return rc;
+
+       da9063.reg = addr;
+       val = fdt_getprop(fdt, nodeoff, "priority", &len);
+       if (len > 0)
+               da9063.priority = fdt32_to_cpu(*val);
+
+       /* find i2c bus parent node */
+       i2c_bus = fdt_parent_offset(fdt, i2c_dev);
+       if (i2c_bus < 0)
+               return i2c_bus;
+
+       /* i2c adapter get */
+       rc = fdt_i2c_adapter_get(fdt, i2c_bus, &adapter);
+       if (rc)
+               return rc;
+
+       da9063.adapter = adapter;

lib/utils/reset/fdt_reset_da9063.c line 209

Quoted 10 lines
lib/utils/reset/fdt_reset_da9063.c new file +10 -0
+
+       sbi_system_reset_add_device(&da9063_reset_i2c);
+
+       return 0;
+}
+
+static const struct fdt_match da9063_reset_match[] = {
+       { .compatible = "da9063-reset", .data = (void *)TRUE },
+       { },
+};

Agree with Jessica here, as per Adam, the reset as we implement it here should remain a board specific thing, so we should not rely on this device tree node. I was hoping to make something that would work in general for da9063 users, but no luck.

And finally I think we should listen to Adam and go for a full reset using the RTC, instead of the partial reset we implemented here. We sacrifice the RTC but anyway, it was not even exposed in the device tree, we should add a comment there so that people don't get surprised if they enable it. I will post the right sequence adapted to your sources soon.

Thanks,

Alex

Quoted 22 lines
lib/utils/reset/fdt_reset_da9063.c new file +5 -0
+
+struct fdt_reset fdt_reset_da9063 = {
+       .match_table = da9063_reset_match,
+       .init = da9063_reset_init,
+};
lib/utils/reset/objects.mk +1 -1
index 6c95db3..cfe4c09 100644
@@ -14,3 +14,4 @@ libsbiutils-objs-y += reset/fdt_reset_sifive_test.o
 libsbiutils-objs-y += reset/fdt_reset_sunxi_wdt.o
 libsbiutils-objs-y += reset/fdt_reset_thead.o
 libsbiutils-objs-y += reset/fdt_reset_thead_asm.o
+libsbiutils-objs-y += reset/fdt_reset_da9063.o
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#14 lore raw

PATCH v2 5/5 lib: utils/reset: Add generic da9063 reset driver

Hello Alexandre!

Quoted 11 lines · On Tue, 19 Oct 2021 14:39:16 +0200 Alexandre Ghiti…

lib/utils/reset/fdt_reset_da9063.c line 209

Quoted 5 lines
lib/utils/reset/fdt_reset_da9063.c new file +5 -0
+
+static const struct fdt_match da9063_reset_match[] = {
+       { .compatible = "da9063-reset", .data = (void *)TRUE },
+       { },
+};  

Agree with Jessica here, as per Adam, the reset as we implement it here should remain a board specific thing, so we should not rely on this device tree node. I was hoping to make something that would work in general for da9063 users, but no luck.

Quoted 6 lines

And finally I think we should listen to Adam and go for a full reset using the RTC, instead of the partial reset we implemented here. We sacrifice the RTC but anyway, it was not even exposed in the device tree, we should add a comment there so that people don't get surprised if they enable it. I will post the right sequence adapted to your sources soon.

Well the RTC is much more valuable than reset actually and used for many tests.

Indeed da9063 reset can be moved totally to "platform" code, but do you think that leaving users with hard-coded reset cutting them off from RTC and no other possibility, other than fixing OpenSBI code in-place is really a good option ?

With DTS approuch it can be as simple as removing the da9063-reset from DTS file.

Quoted 28 lines

Thanks,

Alex

Quoted 22 lines
lib/utils/reset/fdt_reset_da9063.c new file +5 -0
+
+struct fdt_reset fdt_reset_da9063 = {
+       .match_table = da9063_reset_match,
+       .init = da9063_reset_init,
+};
lib/utils/reset/objects.mk +0 -0
index 6c95db3..cfe4c09 100644
@@ -14,3 +14,4 @@ libsbiutils-objs-y +=

reset/fdt_reset_sifive_test.o libsbiutils-objs-y += reset/fdt_reset_sunxi_wdt.o libsbiutils-objs-y += reset/fdt_reset_thead.o libsbiutils-objs-y += reset/fdt_reset_thead_asm.o +libsbiutils-objs-y += reset/fdt_reset_da9063.o --
2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#15 lore raw

PATCH v2 5/5 lib: utils/reset: Add generic da9063 reset driver

Quoted 27 lines · Nikita Shubin

Hello Alexandre!

Quoted 11 lines · On Tue, 19 Oct 2021 14:39:16 +0200 Alexandre Ghiti…

lib/utils/reset/fdt_reset_da9063.c line 209

Quoted 5 lines
lib/utils/reset/fdt_reset_da9063.c new file +5 -0
+
+static const struct fdt_match da9063_reset_match[] = {
+       { .compatible = "da9063-reset", .data = (void *)TRUE },
+       { },
+};

Agree with Jessica here, as per Adam, the reset as we implement it here should remain a board specific thing, so we should not rely on this device tree node. I was hoping to make something that would work in general for da9063 users, but no luck.

Quoted 6 lines

And finally I think we should listen to Adam and go for a full reset using the RTC, instead of the partial reset we implemented here. We sacrifice the RTC but anyway, it was not even exposed in the device tree, we should add a comment there so that people don't get surprised if they enable it. I will post the right sequence adapted to your sources soon.

Well the RTC is much more valuable than reset actually and used for many tests.

Quoted 8 lines

Indeed da9063 reset can be moved totally to "platform" code, but do you think that leaving users with hard-coded reset cutting them off from RTC and no other possibility, other than fixing OpenSBI code in-place is really a good option ?

With DTS approuch it can be as simple as removing the da9063-reset from DTS file.

Arf, I change my mind every two days regarding this...I'm just trying to have a reliable reset, and to quote Adam below, the reset as implemented here is only partial:

"With the sequence you provided this is only a partial reset whereby all of the output rails are sequenced down then up again and restored to OTP voltages. However the remainder of the chip settings aren't reset as this isn't a true reset of the device going through full reload from OTP, so for example settings of regulator mode GPIO states, or IRQ mask bits would persist on the restart, which could have implications on system operation."

I imagine the partial reset could have side-effects on the RTC configuration too. So we might end up with a partial reset and a broken RTC...

and again from Adam:

"Personally, if it was possible I think the RTC approach would be best as it's a full reset and to me is far safer with regards to potential side effects"

Quoted 29 lines
Quoted 28 lines

Thanks,

Alex

Quoted 22 lines
lib/utils/reset/fdt_reset_da9063.c new file +5 -0
+
+struct fdt_reset fdt_reset_da9063 = {
+       .match_table = da9063_reset_match,
+       .init = da9063_reset_init,
+};
lib/utils/reset/objects.mk +0 -0
index 6c95db3..cfe4c09 100644
@@ -14,3 +14,4 @@ libsbiutils-objs-y +=

reset/fdt_reset_sifive_test.o libsbiutils-objs-y += reset/fdt_reset_sunxi_wdt.o libsbiutils-objs-y += reset/fdt_reset_thead.o libsbiutils-objs-y += reset/fdt_reset_thead_asm.o +libsbiutils-objs-y += reset/fdt_reset_da9063.o --
2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#16 lore raw

PATCH v2 5/5 lib: utils/reset: Add generic da9063 reset driver

Quoted 42 lines · On Wed, 20 Oct 2021 10:33:26 +0200 Alexandre Ghiti…
Quoted 27 lines · Nikita Shubin

Hello Alexandre!

Quoted 11 lines · On Tue, 19 Oct 2021 14:39:16 +0200 Alexandre Ghiti…

lib/utils/reset/fdt_reset_da9063.c line 209

Quoted 5 lines
lib/utils/reset/fdt_reset_da9063.c new file +5 -0
+
+static const struct fdt_match da9063_reset_match[] = {
+       { .compatible = "da9063-reset", .data = (void *)TRUE },
+       { },
+};  

Agree with Jessica here, as per Adam, the reset as we implement it here should remain a board specific thing, so we should not rely on this device tree node. I was hoping to make something that would work in general for da9063 users, but no luck.

Quoted 6 lines

And finally I think we should listen to Adam and go for a full reset using the RTC, instead of the partial reset we implemented here. We sacrifice the RTC but anyway, it was not even exposed in the device tree, we should add a comment there so that people don't get surprised if they enable it. I will post the right sequence adapted to your sources soon.

Well the RTC is much more valuable than reset actually and used for many tests.

Quoted 8 lines

Indeed da9063 reset can be moved totally to "platform" code, but do you think that leaving users with hard-coded reset cutting them off from RTC and no other possibility, other than fixing OpenSBI code in-place is really a good option ?

With DTS approuch it can be as simple as removing the da9063-reset from DTS file.

Arf, I change my mind every two days regarding this...I'm just trying to have a reliable reset, and to quote Adam below, the reset as implemented here is only partial:

Quoted 7 lines

"With the sequence you provided this is only a partial reset whereby all of the output rails are sequenced down then up again and restored to OTP voltages. However the remainder of the chip settings aren't reset as this isn't a true reset of the device going through full reload from OTP, so for example settings of regulator mode GPIO states, or IRQ mask bits would persist on the restart, which could have implications on system operation."

Actually i don't think battling over a board which isn't supposed to be more than a development board and won't be massed used makes any sense.

It's more like QoL for those who are currently porting software or experimenting with RISC-V.

Before this we used openocd reset, which can be greatly automated with expect.

Quoted 5 lines

I imagine the partial reset could have side-effects on the RTC configuration too. So we might end up with a partial reset and a broken RTC...

Well we should really investigate this.

As well as i don't quite understand this "sacrifice RTC" thing.

Quoted 36 lines

and again from Adam:

"Personally, if it was possible I think the RTC approach would be best as it's a full reset and to me is far safer with regards to potential side effects"

Quoted 29 lines
Quoted 28 lines

Thanks,

Alex

Quoted 22 lines
lib/utils/reset/fdt_reset_da9063.c new file +5 -0
+
+struct fdt_reset fdt_reset_da9063 = {
+       .match_table = da9063_reset_match,
+       .init = da9063_reset_init,
+};
(quoted diff) +0 -0

b/lib/utils/reset/objects.mk index 6c95db3..cfe4c09 100644

lib/utils/reset/objects.mk +0 -0
@@ -14,3 +14,4 @@ libsbiutils-objs-y +=

reset/fdt_reset_sifive_test.o libsbiutils-objs-y += reset/fdt_reset_sunxi_wdt.o libsbiutils-objs-y += reset/fdt_reset_thead.o libsbiutils-objs-y += reset/fdt_reset_thead_asm.o +libsbiutils-objs-y += reset/fdt_reset_da9063.o --
2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#17 lore raw

PATCH v2 5/5 lib: utils/reset: Add generic da9063 reset driver

Quoted 64 lines · Nikita Shubin
Quoted 42 lines · On Wed, 20 Oct 2021 10:33:26 +0200 Alexandre Ghiti…
Quoted 27 lines · Nikita Shubin

Hello Alexandre!

Quoted 11 lines · On Tue, 19 Oct 2021 14:39:16 +0200 Alexandre Ghiti…

lib/utils/reset/fdt_reset_da9063.c line 209

Quoted 5 lines
lib/utils/reset/fdt_reset_da9063.c new file +5 -0
+
+static const struct fdt_match da9063_reset_match[] = {
+       { .compatible = "da9063-reset", .data = (void *)TRUE },
+       { },
+};

Agree with Jessica here, as per Adam, the reset as we implement it here should remain a board specific thing, so we should not rely on this device tree node. I was hoping to make something that would work in general for da9063 users, but no luck.

Quoted 6 lines

And finally I think we should listen to Adam and go for a full reset using the RTC, instead of the partial reset we implemented here. We sacrifice the RTC but anyway, it was not even exposed in the device tree, we should add a comment there so that people don't get surprised if they enable it. I will post the right sequence adapted to your sources soon.

Well the RTC is much more valuable than reset actually and used for many tests.

Quoted 8 lines

Indeed da9063 reset can be moved totally to "platform" code, but do you think that leaving users with hard-coded reset cutting them off from RTC and no other possibility, other than fixing OpenSBI code in-place is really a good option ?

With DTS approuch it can be as simple as removing the da9063-reset from DTS file.

Arf, I change my mind every two days regarding this...I'm just trying to have a reliable reset, and to quote Adam below, the reset as implemented here is only partial:

Quoted 7 lines

"With the sequence you provided this is only a partial reset whereby all of the output rails are sequenced down then up again and restored to OTP voltages. However the remainder of the chip settings aren't reset as this isn't a true reset of the device going through full reload from OTP, so for example settings of regulator mode GPIO states, or IRQ mask bits would persist on the restart, which could have implications on system operation."

Actually i don't think battling over a board which isn't supposed to be more than a development board and won't be massed used makes any sense.

It's more like QoL for those who are currently porting software or experimenting with RISC-V.

Before this we used openocd reset, which can be greatly automated with expect.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

I imagine the partial reset could have side-effects on the RTC configuration too. So we might end up with a partial reset and a broken RTC...

Well we should really investigate this.

As well as i don't quite understand this "sacrifice RTC" thing.

To use the RTC for the reset, the cleanest solution is to set a one-shot alarm which could have been already programmed by the user. There is also the periodic tick which may or not clobber user-specified registers and is way simpler to implement.

I gave you all the input I had from Dialog, the choice is up to you now, we spent enough time on this already.

Thanks,

Alex

Quoted 38 lines
Quoted 37 lines

and again from Adam:

"Personally, if it was possible I think the RTC approach would be best as it's a full reset and to me is far safer with regards to potential side effects"

Quoted 29 lines
Quoted 28 lines

Thanks,

Alex

Quoted 22 lines
lib/utils/reset/fdt_reset_da9063.c new file +5 -0
+
+struct fdt_reset fdt_reset_da9063 = {
+       .match_table = da9063_reset_match,
+       .init = da9063_reset_init,
+};
(quoted diff) +0 -0

b/lib/utils/reset/objects.mk index 6c95db3..cfe4c09 100644

lib/utils/reset/objects.mk +0 -0
@@ -14,3 +14,4 @@ libsbiutils-objs-y +=

reset/fdt_reset_sifive_test.o libsbiutils-objs-y += reset/fdt_reset_sunxi_wdt.o libsbiutils-objs-y += reset/fdt_reset_thead.o libsbiutils-objs-y += reset/fdt_reset_thead_asm.o +libsbiutils-objs-y += reset/fdt_reset_da9063.o --
2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#18 lore raw

PATCH v2 5/5 lib: utils/reset: Add generic da9063 reset driver

In reply to Nikita Shubin
Quoted 95 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

da9063 PMIC can be used to reset/shutdown the
Sifive Unmatched board.

shutdown is done simply by writing SHUTDOWN bit to DA9063_REG_CONTROL_F register.

reset via setting WAKEUP bit in DA9063_REG_CONTROL_F register followed by masking POWER and POWER1 domains and setting STANDBY bit in DA9063_REG_CONTROL_A, originally discovered by Alexandre Ghiti on linux-riscv maillists.

---
v1 -> v2:
- changed to sub dts node da9063-reset instead of relying on dlg,da9063
- added priority dts node
- return priority system_reset_check either readed from dts or default 1

---
  lib/utils/reset/fdt_reset.c        |   2 +
  lib/utils/reset/fdt_reset_da9063.c | 214 +++++++++++++++++++++++++++++
  lib/utils/reset/objects.mk         |   1 +
  3 files changed, 217 insertions(+)

create mode 100644 lib/utils/reset/fdt_reset_da9063.c

lib/utils/reset/fdt_reset.c +2 -0
index 168bb0c..92f37b0 100644
@@ -18,6 +18,7 @@ extern struct fdt_reset fdt_reset_htif;
  extern struct fdt_reset fdt_reset_sifive_test;
  extern struct fdt_reset fdt_reset_sunxi_wdt;
  extern struct fdt_reset fdt_reset_thead;
+extern struct fdt_reset fdt_reset_da9063;
 
  static struct fdt_reset *reset_drivers[] = {
  	&fdt_poweroff_gpio,
@@ -26,6 +27,7 @@ static struct fdt_reset *reset_drivers[] = {
  	&fdt_reset_sifive_test,
  	&fdt_reset_sunxi_wdt,
  	&fdt_reset_thead,
+	&fdt_reset_da9063,
  };
 
  int fdt_reset_init(void)
lib/utils/reset/fdt_reset_da9063.c new file +42 -0
new file mode 100644
index 0000000..e3c9ced
@@ -0,0 +1,214 @@
+/*
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2021 YADRO
+ *
+ * Authors:
+ *   Nikita Shubin <[email protected]>
+ */
+
+#include <libfdt.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_ecall_interface.h>
+#include <sbi/sbi_hart.h>
+#include <sbi/sbi_system.h>
+#include <sbi_utils/fdt/fdt_helper.h>
+#include <sbi_utils/i2c/fdt_i2c.h>
+#include <sbi_utils/reset/fdt_reset.h>
+
+#define DA9063_REG_PAGE_CON		0x00
+#define DA9063_REG_CONTROL_A		0x0e
+#define DA9063_REG_CONTROL_C		0x10
+#define DA9063_REG_CONTROL_F		0x13
+#define DA9063_REG_DEVICE_ID		0x81
+
+#define DA9063_CONTROL_A_CP_EN		(1 << 7)
+#define DA9063_CONTROL_A_M_POWER1_EN	(1 << 6)
+#define DA9063_CONTROL_A_M_POWER_EN	(1 << 5)
+#define DA9063_CONTROL_A_M_SYSTEM_EN	(1 << 4)
+#define DA9063_CONTROL_A_STANDBY	(1 << 3)
+#define DA9063_CONTROL_A_POWER1_EN	(1 << 2)
+#define DA9063_CONTROL_A_POWER_EN	(1 << 1)
+#define DA9063_CONTROL_A_SYSTEM_EN	(1 << 0)
+
+#define DA9063_CONTROL_F_WAKEUP		(1 << 2)
+#define DA9063_CONTROL_F_SHUTDOWN	(1 << 1)
+#define DA9063_CONTROL_F_WATCHDOG	(1 << 0)
+
+#define DA9063_CONTROL_C_DEF_SUPPLY	(1 << 7)
+#define DA9063_CONTROL_C_SLEW_RATE	(1 << 4)
+#define DA9063_CONTROL_C_OTPREAD_EN	(1 << 3)
+#define DA9063_CONTROL_C_AUTO_BOOT	(1 << 2)

lib/utils/reset/fdt_reset_da9063.c line 52

Quoted 10 lines
lib/utils/reset/fdt_reset_da9063.c new file +10 -0
+#define DA9063_CONTROL_C_DEBOUNCING	(1 << 0)
+
+#define PMIC_CHIP_ID_DA9063		0x61
+
+static struct {
+	struct i2c_adapter *adapter;
+	uint32_t reg;
+	u8 priority;
+} da9063 = {
+	.priority = 1

This value should be 0 until da9063_reset_init() succees. See comments below.

Quoted 120 lines
lib/utils/reset/fdt_reset_da9063.c new file +120 -0
+};
+
+static int da9063_system_reset_check(u32 type, u32 reason)
+{
+	switch (type) {
+	case SBI_SRST_RESET_TYPE_SHUTDOWN:
+	case SBI_SRST_RESET_TYPE_COLD_REBOOT:
+	case SBI_SRST_RESET_TYPE_WARM_REBOOT:
+		return da9063.priority;
+	}
+
+	return 0;
+}
+
+static inline int da9063_sanity_check(struct i2c_adapter *adap, uint32_t reg)
+{
+	uint8_t val;
+	int rc = i2c_adapter_smbus_reg_write(adap, reg, DA9063_REG_PAGE_CON, 0x02);
+
+	if (rc)
+		return rc;
+
+	/* check set page*/
+	rc = i2c_adapter_smbus_reg_read(adap, reg, 0x0, &val);
+	if (rc)
+		return rc;
+
+	if (val != 0x02)
+		return SBI_ENODEV;
+
+	/* read and check device id */
+	rc = i2c_adapter_smbus_reg_read(adap, reg, DA9063_REG_DEVICE_ID, &val);
+	if (rc)
+		return rc;
+
+	if (val != PMIC_CHIP_ID_DA9063)
+		return SBI_ENODEV;
+
+	return 0;
+}
+
+static inline int da9063_shutdown(struct i2c_adapter *adap, uint32_t reg)
+{
+	int rc = i2c_adapter_smbus_reg_write(adap, da9063.reg,
+					DA9063_REG_PAGE_CON, 0x00);
+
+	if (rc)
+		return rc;
+
+	return i2c_adapter_smbus_reg_write(adap, da9063.reg,
+				DA9063_REG_CONTROL_F, DA9063_CONTROL_F_SHUTDOWN);
+}
+
+static inline int da9063_reset(struct i2c_adapter *adap, uint32_t reg)
+{
+	int rc = i2c_adapter_smbus_reg_write(adap, da9063.reg,
+					DA9063_REG_PAGE_CON, 0x00);
+
+	if (rc)
+		return rc;
+
+	rc = i2c_adapter_smbus_reg_write(adap, da9063.reg,
+			      DA9063_REG_CONTROL_F, DA9063_CONTROL_F_WAKEUP);
+	if (rc)
+		return rc;
+
+	return i2c_adapter_smbus_reg_write(adap, da9063.reg,
+				DA9063_REG_CONTROL_A,
+				DA9063_CONTROL_A_M_POWER1_EN |
+				DA9063_CONTROL_A_M_POWER_EN |
+				DA9063_CONTROL_A_STANDBY);
+}
+
+static void da9063_system_reset(u32 type, u32 reason)
+{
+	struct i2c_adapter *adap = da9063.adapter;
+	uint32_t reg = da9063.reg;
+	int rc;
+
+	if (adap) {
+		/* may include clock init */
+		i2c_adapter_configure(adap);
+
+		/* sanity check */
+		rc = da9063_sanity_check(adap, reg);
+		if (rc) {
+			sbi_printf("%s: chip is not da9063 PMIC\n", __func__);
+			goto skip_reset;
+		}
+
+		switch (type) {
+		case SBI_SRST_RESET_TYPE_SHUTDOWN:
+			da9063_shutdown(adap, reg);
+			break;
+		case SBI_SRST_RESET_TYPE_COLD_REBOOT:
+		case SBI_SRST_RESET_TYPE_WARM_REBOOT:
+			da9063_reset(adap, reg);
+			break;
+		}
+
+		while
+			(1);
+skip_reset:
+	}
+
+	sbi_hart_hang();
+}
+
+static struct sbi_system_reset_device da9063_reset_i2c = {
+	.name = "da9063-reset",
+	.system_reset_check = da9063_system_reset_check,
+	.system_reset = da9063_system_reset
+};
+
+static int da9063_reset_init(void *fdt, int nodeoff,
+			   const struct fdt_match *match)
+{
+	int rc, i2c_dev, i2c_bus, len;
+	const fdt32_t *val;
+	struct i2c_adapter *adapter;

lib/utils/reset/fdt_reset_da9063.c line 182

Quoted 10 lines
lib/utils/reset/fdt_reset_da9063.c new file +10 -0
+	uint64_t addr;
+
+	/* find dlg,da9063 parent node */
+	i2c_dev = fdt_parent_offset(fdt, nodeoff);
+	if (i2c_dev < 0)
+		return i2c_dev;
+
+	rc = fdt_get_node_addr_size(fdt, i2c_dev, 0, &addr, NULL);
+	if (rc)
+		return rc;

If you return anything but -ENODEV or 0 to fdt_reset_init(), fdt_reset_init will return a non-zero return value and the board will hang without any console message.

I see the following alternative solutions:

1) Move the initialization of the console before all other devices and provide error messages.

2) Return -ENODEV if the problem does not require hanging.

3) Let fdt_reset_init() just continue in case of any initialization error.

1) makes sense anyway. But hanging is a bad idea.
2) and 3) require that system_reset_check return 0 if the initialization failed.

Best regards

Heinrich

Quoted 42 lines
lib/utils/reset/fdt_reset_da9063.c new file +32 -0
+
+	da9063.reg = addr;
+	val = fdt_getprop(fdt, nodeoff, "priority", &len);
+	if (len > 0)
+		da9063.priority = fdt32_to_cpu(*val);
+
+	/* find i2c bus parent node */
+	i2c_bus = fdt_parent_offset(fdt, i2c_dev);
+	if (i2c_bus < 0)
+		return i2c_bus;
+
+	/* i2c adapter get */
+	rc = fdt_i2c_adapter_get(fdt, i2c_bus, &adapter);
+	if (rc)
+		return rc;
+
+	da9063.adapter = adapter;
+
+	sbi_system_reset_add_device(&da9063_reset_i2c);
+
+	return 0;
+}
+
+static const struct fdt_match da9063_reset_match[] = {
+	{ .compatible = "da9063-reset", .data = (void *)TRUE },
+	{ },
+};
+
+struct fdt_reset fdt_reset_da9063 = {
+	.match_table = da9063_reset_match,
+	.init = da9063_reset_init,
+};
lib/utils/reset/objects.mk +1 -0
index 6c95db3..cfe4c09 100644
@@ -14,3 +14,4 @@ libsbiutils-objs-y += reset/fdt_reset_sifive_test.o
  libsbiutils-objs-y += reset/fdt_reset_sunxi_wdt.o
  libsbiutils-objs-y += reset/fdt_reset_thead.o
  libsbiutils-objs-y += reset/fdt_reset_thead_asm.o
+libsbiutils-objs-y += reset/fdt_reset_da9063.o
#19 lore raw

PATCH v2 5/5 lib: utils/reset: Add generic da9063 reset driver

Quoted 9 lines · Heinrich Schuchardt
Quoted 8 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

da9063 PMIC can be used to reset/shutdown the Sifive Unmatched board.

shutdown is done simply by writing SHUTDOWN bit to DA9063_REG_CONTROL_F register.

Quoted 7 lines
Quoted 7 lines

reset via setting WAKEUP bit in DA9063_REG_CONTROL_F register followed by masking POWER and POWER1 domains and setting STANDBY bit in DA9063_REG_CONTROL_A, originally discovered by Alexandre Ghiti on linux-riscv maillists.

Alexandre and I had the same finding for the driver:

Reset works fine after a hard reset (via the reset button) when coming from Linux.

After a poweroff reset in U-Boot does not work.

Our expectation is that OpenSBI ensures that the SBI system reset extension works upon entry into the next firmware stage without requiring any further initialization by that software.

When a reset is requested you should make not presumption about the state of the system. Simply fully initialize the da9063 state to guarantee a successful reset.

Best regards

Heinrich

#20 lore raw

PATCH v2 5/5 lib: utils/reset: Add generic da9063 reset driver

Hello Heinrich.

Quoted 16 lines · On Tue, 26 Oct 2021 11:40:13 +0200 Heinrich Schuchardt…
Quoted 15 lines · Heinrich Schuchardt
Quoted 14 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

da9063 PMIC can be used to reset/shutdown the Sifive Unmatched board.

shutdown is done simply by writing SHUTDOWN bit to DA9063_REG_CONTROL_F register.

reset via setting WAKEUP bit in DA9063_REG_CONTROL_F register followed by masking POWER and POWER1 domains and setting STANDBY bit in DA9063_REG_CONTROL_A, originally discovered by Alexandre Ghiti on linux-riscv maillists.

Quoted 8 lines
Quoted 1 line
Quoted 1 line

Alexandre and I had the same finding for the driver:

Reset works fine after a hard reset (via the reset button) when coming from Linux.

After a poweroff reset in U-Boot does not work.

Thank you for testing!

Quoted 8 lines

Our expectation is that OpenSBI ensures that the SBI system reset extension works upon entry into the next firmware stage without requiring any further initialization by that software.

When a reset is requested you should make not presumption about the state of the system. Simply fully initialize the da9063 state to guarantee a successful reset.

I think you are right and we shouldn't rely on someone already initialized it for us.

Indeed i should investigate and expiriment some more.

#21 lore raw

PATCH v2 5/5 lib: utils/reset: Add generic da9063 reset driver

In reply to Heinrich Schuchardt

Hello All!

Quoted 25 lines · On Tue, 26 Oct 2021 11:40:13 +0200 Heinrich Schuchardt…
Quoted 9 lines · Heinrich Schuchardt
Quoted 8 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

da9063 PMIC can be used to reset/shutdown the Sifive Unmatched board.

shutdown is done simply by writing SHUTDOWN bit to DA9063_REG_CONTROL_F register.

Quoted 7 lines
Quoted 7 lines

reset via setting WAKEUP bit in DA9063_REG_CONTROL_F register followed by masking POWER and POWER1 domains and setting STANDBY bit in DA9063_REG_CONTROL_A, originally discovered by Alexandre Ghiti on linux-riscv maillists.

Alexandre and I had the same finding for the driver:

Reset works fine after a hard reset (via the reset button) when coming from Linux.

After a poweroff reset in U-Boot does not work.

Quoted 7 lines

Our expectation is that OpenSBI ensures that the SBI system reset extension works upon entry into the next firmware stage without requiring any further initialization by that software.

When a reset is requested you should make not presumption about the state of the system. Simply fully initialize the da9063 state to guarantee a successful reset.

Heinrich and Alexandre, i have done a small investigation:

The RTC works fine on Unmatched, just had a small issue with PLIC driver firing IRQ only once, here is a separate patch from Guo Ren:

https://patchwork.kernel.org/project/linux-riscv/patch/20211101131736.3800114-1-guoren at kernel.org/

And a small patch to enable da9063 as a wakeup source:

https://github.com/YADRO-KNS/linux/commit/37c2a5b2031a42af99dd7125f8c93ab71236d3fa

Indeed after power-cycle with RESET button or POWER button, I2C is unavailable (not initialised by U-Boot):

First boot reset after powercycle with powerdown/reset button:
=> reset
resetting ...
da9063_system_reset: chip is not da9063 PMIC

After rebooting with Linux reboot:
=> reset
resetting ...

All okay and that's means that it's not a full reset, I2C was initialised by linux, and not resetted.

However if we arm the clock and go shutdown:

sh -c "echo date '+%s' -d '+ 1 minutes' >
/sys/class/rtc/rtc0/wakealarm" shutdown
shutdown

We get:
=> reset
resetting ...
da9063_system_reset: chip is not da9063 PMIC

So "reset" with RTC is indeed a full reset...

All above also means that "sacrificing" RTC means sacrificing user setted wakeup ALARM's.

May be it's worth splitting reset into WARM_REBOOT, COLD_REBOOT with WARM reboot is going through method proposed by Alexandre and COLD reboot via RTC, and explicitly specify that COLD reboot WILL overwrite ALARM setting ?

What do you think about this ?

Quoted 4 lines

Best regards

Heinrich

#22 lore raw

PATCH v2 5/5 lib: utils/reset: Add generic da9063 reset driver

Quoted 57 lines · Nikita Shubin

Hello All!

Quoted 25 lines · On Tue, 26 Oct 2021 11:40:13 +0200 Heinrich Schuchardt…
Quoted 9 lines · Heinrich Schuchardt
Quoted 8 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

da9063 PMIC can be used to reset/shutdown the Sifive Unmatched board.

shutdown is done simply by writing SHUTDOWN bit to DA9063_REG_CONTROL_F register.

Quoted 7 lines
Quoted 7 lines

reset via setting WAKEUP bit in DA9063_REG_CONTROL_F register followed by masking POWER and POWER1 domains and setting STANDBY bit in DA9063_REG_CONTROL_A, originally discovered by Alexandre Ghiti on linux-riscv maillists.

Alexandre and I had the same finding for the driver:

Reset works fine after a hard reset (via the reset button) when coming from Linux.

After a poweroff reset in U-Boot does not work.

Quoted 7 lines

Our expectation is that OpenSBI ensures that the SBI system reset extension works upon entry into the next firmware stage without requiring any further initialization by that software.

When a reset is requested you should make not presumption about the state of the system. Simply fully initialize the da9063 state to guarantee a successful reset.

Heinrich and Alexandre, i have done a small investigation:

The RTC works fine on Unmatched, just had a small issue with PLIC driver firing IRQ only once, here is a separate patch from Guo Ren:

https://patchwork.kernel.org/project/linux-riscv/patch/20211101131736.3800114-1-guoren at kernel.org/

And a small patch to enable da9063 as a wakeup source:

https://github.com/YADRO-KNS/linux/commit/37c2a5b2031a42af99dd7125f8c93ab71236d3fa

Indeed after power-cycle with RESET button or POWER button, I2C is unavailable (not initialised by U-Boot):

First boot reset after powercycle with powerdown/reset button:
=> reset
resetting ...
da9063_system_reset: chip is not da9063 PMIC

Quoted 6 lines

After rebooting with Linux reboot:
=> reset
resetting ...

All okay and that's means that it's not a full reset, I2C was initialised by linux, and not resetted.

Doesn't this imply that the I2C initialization code should be added to OpenSBI?

Best regards

Heinrich

Quoted 29 lines

However if we arm the clock and go shutdown:

sh -c "echo date '+%s' -d '+ 1 minutes' >
/sys/class/rtc/rtc0/wakealarm" shutdown
shutdown

We get:
=> reset
resetting ...
da9063_system_reset: chip is not da9063 PMIC

So "reset" with RTC is indeed a full reset...

All above also means that "sacrificing" RTC means sacrificing user setted wakeup ALARM's.

May be it's worth splitting reset into WARM_REBOOT, COLD_REBOOT with WARM reboot is going through method proposed by Alexandre and COLD reboot via RTC, and explicitly specify that COLD reboot WILL overwrite ALARM setting ?

What do you think about this ?

Quoted 4 lines

Best regards

Heinrich

#23 lore raw

PATCH v2 5/5 lib: utils/reset: Add generic da9063 reset driver

Quoted 10 lines · On Wed, 3 Nov 2021 13:07:29 +0100 Heinrich Schuchardt…
Quoted 6 lines

After rebooting with Linux reboot:
=> reset
resetting ...

All okay and that's means that it's not a full reset, I2C was initialised by linux, and not resetted.

Doesn't this imply that the I2C initialization code should be added to OpenSBI?

Without any doubt.

Quoted 34 lines

Best regards

Heinrich

Quoted 29 lines

However if we arm the clock and go shutdown:

sh -c "echo date '+%s' -d '+ 1 minutes' >
/sys/class/rtc/rtc0/wakealarm" shutdown
shutdown

We get:
=> reset
resetting ...
da9063_system_reset: chip is not da9063 PMIC

So "reset" with RTC is indeed a full reset...

All above also means that "sacrificing" RTC means sacrificing user setted wakeup ALARM's.

May be it's worth splitting reset into WARM_REBOOT, COLD_REBOOT with WARM reboot is going through method proposed by Alexandre and COLD reboot via RTC, and explicitly specify that COLD reboot WILL overwrite ALARM setting ?

What do you think about this ?

Quoted 4 lines

Best regards

Heinrich

#24 lore raw

PATCH v2 5/5 lib: utils/reset: Add generic da9063 reset driver

Quoted 8 lines · Nikita Shubin
Quoted 4 lines · On Wed, 3 Nov 2021 13:07:29 +0100 Heinrich Schuchardt…
Quoted 4 lines

After rebooting with Linux reboot:
=> reset
resetting ...

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

All okay and that's means that it's not a full reset, I2C was initialised by linux, and not resetted.

Doesn't this imply that the I2C initialization code should be added to OpenSBI?

Without any doubt.

I am okay having a simple I2C initialization.

This series will be the last thing for OpenSBI v1.0 release so please update this series soon.

Regards,
Anup

Quoted 41 lines
Quoted 35 lines

Best regards

Heinrich

Quoted 29 lines

However if we arm the clock and go shutdown:

sh -c "echo date '+%s' -d '+ 1 minutes' >
/sys/class/rtc/rtc0/wakealarm" shutdown
shutdown

We get:
=> reset
resetting ...
da9063_system_reset: chip is not da9063 PMIC

So "reset" with RTC is indeed a full reset...

All above also means that "sacrificing" RTC means sacrificing user setted wakeup ALARM's.

May be it's worth splitting reset into WARM_REBOOT, COLD_REBOOT with WARM reboot is going through method proposed by Alexandre and COLD reboot via RTC, and explicitly specify that COLD reboot WILL overwrite ALARM setting ?

What do you think about this ?

Quoted 4 lines

Best regards

Heinrich

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#25 lore raw

v2 cover I2C framework, reboot Unmatched via PMIC

In reply to Nikita Shubin
Quoted 6 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

This series introduce rebooting via i2c da9063 PMIC, currently on SiFive Unmatched board.

Quoted 7 lines

"gpio-poweroff" remains with default priority of 128 - default priority, da9063 is 1 by default the least priority.

da9063-reset {
	compatible = "da9063-reset";
	priority = <1>;
};

I don?t see why priority is needed; the DA9063 is known to always be a last-resort way to reboot. Just hard-code it as the lowest priority. I also don?t see why we need a device tree node, just make it a platform quirk, because this isn?t (and shouldn?t be) a standardised thing? Definitely don?t want it being exposed to anything other than OpenSBI, which is what ends up happening if it lives in the device tree...

Jess

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#28 lore raw

v2 cover I2C framework, reboot Unmatched via PMIC

On Fri, 15 Oct 2021 14:56:40 +0100
Jessica Clarke <[email protected]> wrote:

Hello Jessica,

Quoted 11 lines

The chip is common for RTC, power management and watchdog functionality. Using it to reboot in this manner is not, as evidenced by the fact that Linux does not have a driver for doing so and the hardware vendor is unsure of how it?s even working the way it is. I hope this driver is never needed for future RISC-V hardware (and I don?t quite understand how it ended up being needed for the Unmatched, when the Unleashed had a gpio-restart device, though that lacked a gpio-poweroff I believe; seems like a very strange oversight).

Jess

Implementing SBC reset via Watchdog or PMIC is quite a common practice.

#29 lore raw

v2 cover I2C framework, reboot Unmatched via PMIC

Quoted 15 lines · Nikita Shubin

On Fri, 15 Oct 2021 14:56:40 +0100
Jessica Clarke <[email protected]> wrote:

Hello Jessica,

Quoted 9 lines

The chip is common for RTC, power management and watchdog functionality. Using it to reboot in this manner is not, as evidenced by the fact that Linux does not have a driver for doing so and the hardware vendor is unsure of how it?s even working the way it is. I hope this driver is never needed for future RISC-V hardware (and I don?t quite understand how it ended up being needed for the Unmatched, when the Unleashed had a gpio-restart device, though that lacked a gpio-poweroff I believe; seems like a very strange oversight).

Quoted 4 lines
Quoted 2 lines

Jess

Implementing SBC reset via Watchdog or PMIC is quite a common practice.

This sequence differs from the existing watchdog-based implementation though, and to quote one of Dialog?s own engineers on LKML:

Quoted 3 lines

Personally, if it was possible I think the RTC approach would be best as it's a full reset and to me is far safer with regards to potential side effects, but as that's not on the table then this seems the only other approach in your case.

This is why I am suggesting it not be a generic device tree thing, as the vendor is basically saying it?s not a good option but the only choice you have, so should be kept a quirk.

Jess

#30 lore raw

v2 cover I2C framework, reboot Unmatched via PMIC

Quoted 17 lines · Jessica Clarke
Quoted 15 lines · Nikita Shubin

On Fri, 15 Oct 2021 14:56:40 +0100
Jessica Clarke <[email protected]> wrote:

Hello Jessica,

Quoted 9 lines

The chip is common for RTC, power management and watchdog functionality. Using it to reboot in this manner is not, as evidenced by the fact that Linux does not have a driver for doing so and the hardware vendor is unsure of how it?s even working the way it is. I hope this driver is never needed for future RISC-V hardware (and I don?t quite understand how it ended up being needed for the Unmatched, when the Unleashed had a gpio-restart device, though that lacked a gpio-poweroff I believe; seems like a very strange oversight).

Quoted 13 lines
Quoted 4 lines
Quoted 2 lines

Jess

Implementing SBC reset via Watchdog or PMIC is quite a common practice.

This sequence differs from the existing watchdog-based implementation though, and to quote one of Dialog?s own engineers on LKML:

Quoted 3 lines

Personally, if it was possible I think the RTC approach would be best as it's a full reset and to me is far safer with regards to potential side effects, but as that's not on the table then this seems the only other approach in your case.

I don't know if you followed the discussion Nikita here:
https://patchwork.kernel.org/project/linux-riscv/patch/20210921053356.1705833-1-alexandre.ghiti at canonical.com/

Quoted 3 lines

This is why I am suggesting it not be a generic device tree thing, as the vendor is basically saying it?s not a good option but the only choice you have, so should be kept a quirk.

This is not the only choice and Adam admits he does not know the "side-effects" of a partial reset like the one implemented here. But to implement the solution he proposes, we need to sacrifice the RTC as it may override user configuration. Maybe it's better to make sure the reset works as expected at the expense of the RTC, that's an open question and today I would tend to listen to him :)

Alex

Quoted 8 lines

Jess

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#31 lore raw

v2 cover I2C framework, reboot Unmatched via PMIC

In reply to Nikita Shubin
Quoted 31 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

This series introduce rebooting via i2c da9063 PMIC, currently on SiFive Unmatched board.

"gpio-poweroff" remains with default priority of 128 - default priority, da9063 is 1 by default the least priority.

da9063-reset {
        compatible = "da9063-reset";
        priority = <1>;
};

Is required to be added as a child node of PMIC.

tested via Linux and U-Boot with reset extension:

OpenSBI:
Platform Reboot Device : da9063-reset
Platform Shutdown Device : gpio-reset

v1 -> v2:
Added:
lib: sbi: add priority for reset handler

Renamed read/write to smbus_write/smbus_read, as actually this is not a "raw" read/write but a one with a register address provided, later a "raw" version will be need but currently i don't have anything to test it.

Quoted 5 lines

To Xiang W:
I have analized your proposal of switching to sbi_list, and switched i2c adapters array to list. Unfortunately to switch drivers we require "init" functions.

This also has the merit of getting rid of this limit of 16 i2c adapters, that's nice. Maybe you could use that in the gpio library too?

Quoted 4 lines

to Alexander Ghiti:

Quoted 1 line

No need for the struct i2c_adapter argument as it is globally accessible.

Quoted 8 lines

It looks like a more clean and reusable way to me, let's here more opinions

Quoted 3 lines

Why should the da9063 device care about the adapter configuration here? I think It should just rely on an already configured/initialized i2c controller.

With clocks init added to sifive_i2c configure it can become usable even if nobody cared about controller initialization.

I don't understand what you mean here. IMO, the device driver should only take care of the communication with the device, not the controller. That can be done in the init function of the controller driver, which in addition would simplify the callbacks and the device driver code.

Quoted 7 lines
Quoted 4 lines

Establishing the link between the i2c device and its adapter should somehow be implicitly done by the i2c library, IMO the device should not care about its controller.

And how do you think it will look like ? In out case it's not a separate driver/client entity but all togather for simplicity.

In fdt_i2c_adapter_get, instead of passing a pointer to a struct i2c_adapter, we could pass a i2c_device structure which would get filled automatically with its adapter chip. But that implies introducing this i2c_device like I mentioned in the previous review :) In addition, I took a look at the gpio library and they did it this way too.

Again, I don't see why the driver developer should care about its adapter since everything could be done behind the scenes for him.

Quoted 35 lines

Nikita Shubin (5):
lib: utils/reset: add priority to gpio reset
lib: utils/i2c: Add generic I2C configuration library
lib: utils/i2c: Add simple FDT based I2C framework
lib: utils/i2c: Add minimal SiFive I2C driver
lib: utils/reset: Add generic da9063 reset driver

 include/sbi_utils/i2c/fdt_i2c.h    |  26 +++
 include/sbi_utils/i2c/i2c.h        |  99 +++++++++++
 lib/utils/i2c/fdt_i2c.c            | 111 ++++++++++++
 lib/utils/i2c/fdt_i2c_sifive.c     | 271 +++++++++++++++++++++++++++++
 lib/utils/i2c/i2c.c                |  85 +++++++++
 lib/utils/i2c/objects.mk           |  12 ++
 lib/utils/reset/fdt_reset.c        |   2 +
 lib/utils/reset/fdt_reset_da9063.c | 214 +++++++++++++++++++++++
 lib/utils/reset/fdt_reset_gpio.c   |  18 +-
 lib/utils/reset/objects.mk         |   1 +
 10 files changed, 836 insertions(+), 3 deletions(-)
 create mode 100644 include/sbi_utils/i2c/fdt_i2c.h
 create mode 100644 include/sbi_utils/i2c/i2c.h
 create mode 100644 lib/utils/i2c/fdt_i2c.c
 create mode 100644 lib/utils/i2c/fdt_i2c_sifive.c
 create mode 100644 lib/utils/i2c/i2c.c
 create mode 100644 lib/utils/i2c/objects.mk
 create mode 100644 lib/utils/reset/fdt_reset_da9063.c

--
2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#32 lore raw

v2 cover I2C framework, reboot Unmatched via PMIC

Quoted 39 lines · On Tue, 19 Oct 2021 13:57:35 +0200 Alexandre Ghiti…
Quoted 31 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

This series introduce rebooting via i2c da9063 PMIC, currently on SiFive Unmatched board.

"gpio-poweroff" remains with default priority of 128 - default priority, da9063 is 1 by default the least priority.

da9063-reset {
        compatible = "da9063-reset";
        priority = <1>;
};

Is required to be added as a child node of PMIC.

tested via Linux and U-Boot with reset extension:

OpenSBI:
Platform Reboot Device : da9063-reset
Platform Shutdown Device : gpio-reset

v1 -> v2:
Added:
lib: sbi: add priority for reset handler

Renamed read/write to smbus_write/smbus_read, as actually this is not a "raw" read/write but a one with a register address provided, later a "raw" version will be need but currently i don't have anything to test it.

Quoted 5 lines

To Xiang W:
I have analized your proposal of switching to sbi_list, and switched i2c adapters array to list. Unfortunately to switch drivers we require "init" functions.

Quoted 3 lines

This also has the merit of getting rid of this limit of 16 i2c adapters, that's nice. Maybe you could use that in the gpio library too?

Indeed, but it's not a part of this patch series.

Quoted 12 lines
Quoted 12 lines

to Alexander Ghiti:

Quoted 2 lines

No need for the struct i2c_adapter argument as it is globally accessible.

It looks like a more clean and reusable way to me, let's here more opinions

Quoted 3 lines

Why should the da9063 device care about the adapter configuration here? I think It should just rely on an already configured/initialized i2c controller.

Quoted 8 lines
Quoted 2 lines

With clocks init added to sifive_i2c configure it can become usable even if nobody cared about controller initialization.

I don't understand what you mean here. IMO, the device driver should only take care of the communication with the device, not the controller. That can be done in the init function of the controller driver, which in addition would simplify the callbacks and the device driver code.

Well to make use of I2C you should enable it togather with clocks, see u-boot/arch/riscv/dts/fu740-c000.dtsi for example:

i2c0: i2c at 10030000 {
...
clocks = <&prci PRCI_CLK_PCLK>;
...

It part of driver responsibility to enable clocks it requires, cause if not is required nobody will enable them.

Quoted 16 lines
Quoted 8 lines
Quoted 5 lines

Establishing the link between the i2c device and its adapter should somehow be implicitly done by the i2c library, IMO the device should not care about its controller.

And how do you think it will look like ? In out case it's not a separate driver/client entity but all togather for simplicity.

In fdt_i2c_adapter_get, instead of passing a pointer to a struct i2c_adapter, we could pass a i2c_device structure which would get filled automatically with its adapter chip. But that implies introducing this i2c_device like I mentioned in the previous review :) In addition, I took a look at the gpio library and they did it this way too.

Quoted 2 lines

Again, I don't see why the driver developer should care about its adapter since everything could be done behind the scenes for him.

Are you suggesting to separate the da9063_reset into client/driver parts ? Don't you think that it will add unnecessary complicity if our case ?

Linux i2c_client has a reference to the adapter it's sitting on:
https://elixir.bootlin.com/linux/v5.15-rc6/source/include/linux/i2c.h#L336

u-boot is relying on some "default" i2c adapter/bus ideed, but if we look on:

https://elixir.bootlin.com/u-boot/latest/source/include/i2c.h#L718

We see it's rather complex and hard to understand.

Quoted 36 lines
Quoted 36 lines

Nikita Shubin (5):
lib: utils/reset: add priority to gpio reset
lib: utils/i2c: Add generic I2C configuration library
lib: utils/i2c: Add simple FDT based I2C framework
lib: utils/i2c: Add minimal SiFive I2C driver
lib: utils/reset: Add generic da9063 reset driver

 include/sbi_utils/i2c/fdt_i2c.h    |  26 +++
 include/sbi_utils/i2c/i2c.h        |  99 +++++++++++
 lib/utils/i2c/fdt_i2c.c            | 111 ++++++++++++
 lib/utils/i2c/fdt_i2c_sifive.c     | 271
+++++++++++++++++++++++++++++ lib/utils/i2c/i2c.c                |
85 +++++++++ lib/utils/i2c/objects.mk           |  12 ++
 lib/utils/reset/fdt_reset.c        |   2 +
 lib/utils/reset/fdt_reset_da9063.c | 214 +++++++++++++++++++++++
 lib/utils/reset/fdt_reset_gpio.c   |  18 +-
 lib/utils/reset/objects.mk         |   1 +
 10 files changed, 836 insertions(+), 3 deletions(-)
 create mode 100644 include/sbi_utils/i2c/fdt_i2c.h
 create mode 100644 include/sbi_utils/i2c/i2c.h
 create mode 100644 lib/utils/i2c/fdt_i2c.c
 create mode 100644 lib/utils/i2c/fdt_i2c_sifive.c
 create mode 100644 lib/utils/i2c/i2c.c
 create mode 100644 lib/utils/i2c/objects.mk
 create mode 100644 lib/utils/reset/fdt_reset_da9063.c

--
2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#33 lore raw

v2 cover I2C framework, reboot Unmatched via PMIC

Quoted 73 lines · Nikita Shubin
Quoted 39 lines · On Tue, 19 Oct 2021 13:57:35 +0200 Alexandre Ghiti…
Quoted 31 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

This series introduce rebooting via i2c da9063 PMIC, currently on SiFive Unmatched board.

"gpio-poweroff" remains with default priority of 128 - default priority, da9063 is 1 by default the least priority.

da9063-reset {
        compatible = "da9063-reset";
        priority = <1>;
};

Is required to be added as a child node of PMIC.

tested via Linux and U-Boot with reset extension:

OpenSBI:
Platform Reboot Device : da9063-reset
Platform Shutdown Device : gpio-reset

v1 -> v2:
Added:
lib: sbi: add priority for reset handler

Renamed read/write to smbus_write/smbus_read, as actually this is not a "raw" read/write but a one with a register address provided, later a "raw" version will be need but currently i don't have anything to test it.

Quoted 5 lines

To Xiang W:
I have analized your proposal of switching to sbi_list, and switched i2c adapters array to list. Unfortunately to switch drivers we require "init" functions.

Quoted 3 lines

This also has the merit of getting rid of this limit of 16 i2c adapters, that's nice. Maybe you could use that in the gpio library too?

Indeed, but it's not a part of this patch series.

Quoted 12 lines
Quoted 12 lines

to Alexander Ghiti:

Quoted 2 lines

No need for the struct i2c_adapter argument as it is globally accessible.

It looks like a more clean and reusable way to me, let's here more opinions

Quoted 3 lines

Why should the da9063 device care about the adapter configuration here? I think It should just rely on an already configured/initialized i2c controller.

Quoted 8 lines
Quoted 2 lines

With clocks init added to sifive_i2c configure it can become usable even if nobody cared about controller initialization.

I don't understand what you mean here. IMO, the device driver should only take care of the communication with the device, not the controller. That can be done in the init function of the controller driver, which in addition would simplify the callbacks and the device driver code.

Well to make use of I2C you should enable it togather with clocks, see u-boot/arch/riscv/dts/fu740-c000.dtsi for example:

Quoted 7 lines

i2c0: i2c at 10030000 {
...
clocks = <&prci PRCI_CLK_PCLK>;
...

It part of driver responsibility to enable clocks it requires, cause if not is required nobody will enable them.

I understand that but why couldn't this configuration be done in the adapter driver instead of the da9063 driver? At the initialization of the adapter driver, or at each beginning of a write sequence...etc.

Quoted 17 lines
Quoted 17 lines
Quoted 9 lines
Quoted 6 lines

Establishing the link between the i2c device and its adapter should somehow be implicitly done by the i2c library, IMO the device should not care about its controller.

And how do you think it will look like ? In out case it's not a separate driver/client entity but all togather for simplicity.

In fdt_i2c_adapter_get, instead of passing a pointer to a struct i2c_adapter, we could pass a i2c_device structure which would get filled automatically with its adapter chip. But that implies introducing this i2c_device like I mentioned in the previous review :) In addition, I took a look at the gpio library and they did it this way too.

Quoted 6 lines
Quoted 2 lines

Again, I don't see why the driver developer should care about its adapter since everything could be done behind the scenes for him.

Are you suggesting to separate the da9063_reset into client/driver parts ? Don't you think that it will add unnecessary complicity if our case ?

To make sure we understand each other, find below a diff of what it would look like the way I imagine it. It just makes the notion of adapter disappear from the i2c device driver, that's all. This is what is done in openSBI with gpio_pin structure, in Linux here
https://www.kernel.org/doc/html/latest/i2c/writing-clients.html#smbus-communication.

include/sbi_utils/i2c/i2c.h +4 -0
index 76cdb66..0f1671e 100644
@@ -50,6 +50,10 @@ struct i2c_adapter {
        struct sbi_dlist node;
 };
 
+struct i2c_device {
+       struct i2c_adapter *adap;
+};
+
 static inline struct i2c_adapter *to_i2c_adapter(struct sbi_dlist *node)
 {
        return container_of(node, struct i2c_adapter, node);
lib/utils/i2c/i2c.c +8 -8
index d23ac91..9c64d09 100644
@@ -61,25 +61,25 @@ int i2c_adapter_configure(struct i2c_adapter *ia)
        return ia->configure(ia);
 }
 
-int i2c_adapter_smbus_write(struct i2c_adapter *ia, uint8_t addr,
+int i2c_smbus_write(struct i2c_device *dev, uint8_t addr,
                        uint8_t reg, uint8_t *buffer, int len)
 {
-       if (!ia)
+       if (!dev)
                return SBI_EINVAL;
-       if (!ia->smbus_write)
+       if (!dev->adap || !dev->adap->smbus_write)
                return SBI_ENOSYS;
 
-       return ia->smbus_write(ia, addr, reg, buffer, len);
+       return dev->adap->smbus_write(dev, addr, reg, buffer, len);
 }
 
 
-int i2c_adapter_smbus_read(struct i2c_adapter *ia, uint8_t addr,
+int i2c_smbus_read(struct i2c_device *dev, uint8_t addr,
                     uint8_t reg, uint8_t *buffer, int len)
 {
-       if (!ia)
+       if (!dev)
                return SBI_EINVAL;
-       if (!ia->smbus_read)
+       if (!dev->adap || !dev->adap->smbus_read)
                return SBI_ENOSYS;
 
-       return ia->smbus_read(ia, addr, reg, buffer, len);
+       return dev->adap->smbus_read(dev, addr, reg, buffer, len);
 }
(quoted diff) +0 -0

b/lib/utils/reset/fdt_reset_da9063.c
index e3c9ced..a14d941 100644

lib/utils/reset/fdt_reset_da9063.c +1 -1
@@ -45,7 +45,7 @@
 #define PMIC_CHIP_ID_DA9063            0x61
 
 static struct {
-       struct i2c_adapter *adapter;
+       struct i2c_device *dev;
        uint32_t reg;
        u8 priority;
 } da9063 = {
@@ -103,20 +103,20 @@ static inline int da9063_shutdown(struct

i2c_adapter *adap, uint32_t reg)

                                DA9063_REG_CONTROL_F,

DA9063_CONTROL_F_SHUTDOWN);
}

-static inline int da9063_reset(struct i2c_adapter *adap, uint32_t reg) +static inline int da9063_reset(struct i2c_device *dev, uint32_t reg)
{
- int rc = i2c_adapter_smbus_reg_write(adap, da9063.reg,
+ int rc = i2c_smbus_reg_write(dev, da9063.reg,

                                        DA9063_REG_PAGE_CON, 0x00);

        if (rc)
                return rc;

- rc = i2c_adapter_smbus_reg_write(adap, da9063.reg,
+ rc = i2c_smbus_reg_write(dev, da9063.reg,

                              DA9063_REG_CONTROL_F, DA9063_CONTROL_F_WAKEUP);
        if (rc)
                return rc;

- return i2c_adapter_smbus_reg_write(adap, da9063.reg,
+ return i2c_smbus_reg_write(dev, da9063.reg,

                                DA9063_REG_CONTROL_A,
                                DA9063_CONTROL_A_M_POWER1_EN |
                                DA9063_CONTROL_A_M_POWER_EN |
@@ -169,7 +169,6 @@ static int da9063_reset_init(void *fdt, int nodeoff,
 {
        int rc, i2c_dev, i2c_bus, len;
        const fdt32_t *val;
-       struct i2c_adapter *adapter;
        uint64_t addr;

        /* find dlg,da9063 parent node */
@@ -192,12 +191,10 @@ static int da9063_reset_init(void *fdt, int nodeoff,
                return i2c_bus;

        /* i2c adapter get */
-       rc = fdt_i2c_adapter_get(fdt, i2c_bus, &adapter);
+       rc = fdt_i2c_adapter_get(fdt, i2c_bus, &da9063.dev);
        if (rc)
                return rc;

- da9063.adapter = adapter;
-

        sbi_system_reset_add_device(&da9063_reset_i2c);

        return 0;
Quoted 50 lines

Linux i2c_client has a reference to the adapter it's sitting on:
https://elixir.bootlin.com/linux/v5.15-rc6/source/include/linux/i2c.h#L336

u-boot is relying on some "default" i2c adapter/bus ideed, but if we look on:

https://elixir.bootlin.com/u-boot/latest/source/include/i2c.h#L718

We see it's rather complex and hard to understand.

Quoted 36 lines
Quoted 36 lines

Nikita Shubin (5):
lib: utils/reset: add priority to gpio reset
lib: utils/i2c: Add generic I2C configuration library
lib: utils/i2c: Add simple FDT based I2C framework
lib: utils/i2c: Add minimal SiFive I2C driver
lib: utils/reset: Add generic da9063 reset driver

 include/sbi_utils/i2c/fdt_i2c.h    |  26 +++
 include/sbi_utils/i2c/i2c.h        |  99 +++++++++++
 lib/utils/i2c/fdt_i2c.c            | 111 ++++++++++++
 lib/utils/i2c/fdt_i2c_sifive.c     | 271
+++++++++++++++++++++++++++++ lib/utils/i2c/i2c.c                |
85 +++++++++ lib/utils/i2c/objects.mk           |  12 ++
 lib/utils/reset/fdt_reset.c        |   2 +
 lib/utils/reset/fdt_reset_da9063.c | 214 +++++++++++++++++++++++
 lib/utils/reset/fdt_reset_gpio.c   |  18 +-
 lib/utils/reset/objects.mk         |   1 +
 10 files changed, 836 insertions(+), 3 deletions(-)
 create mode 100644 include/sbi_utils/i2c/fdt_i2c.h
 create mode 100644 include/sbi_utils/i2c/i2c.h
 create mode 100644 lib/utils/i2c/fdt_i2c.c
 create mode 100644 lib/utils/i2c/fdt_i2c_sifive.c
 create mode 100644 lib/utils/i2c/i2c.c
 create mode 100644 lib/utils/i2c/objects.mk
 create mode 100644 lib/utils/reset/fdt_reset_da9063.c

--
2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#34 lore raw

v2 cover I2C framework, reboot Unmatched via PMIC

Quoted 8 lines · On Wed, 20 Oct 2021 10:11:56 +0200 Alexandre Ghiti…
Quoted 3 lines

It part of driver responsibility to enable clocks it requires, cause if not is required nobody will enable them.

I understand that but why couldn't this configuration be done in the adapter driver instead of the da9063 driver? At the initialization of the adapter driver, or at each beginning of a write sequence...etc.

I think we should stick to "don't touch until you really need", that's why we shouldn't do it in driver init, we can enable clocks on unused bus which will lead to more power consumption, and other's won't be aware that it needs to be gated off (and even so it's need to gated on once again).

Also it's really good to disable interrupts when you don't need them.

Doing it every read/write seems quite wrong to me, that's why i decided to add a separate method for such tasks.

Quoted 138 lines
Quoted 17 lines
Quoted 17 lines
Quoted 9 lines
Quoted 6 lines

Establishing the link between the i2c device and its adapter should somehow be implicitly done by the i2c library, IMO the device should not care about its controller.

And how do you think it will look like ? In out case it's not a separate driver/client entity but all togather for simplicity.

In fdt_i2c_adapter_get, instead of passing a pointer to a struct i2c_adapter, we could pass a i2c_device structure which would get filled automatically with its adapter chip. But that implies introducing this i2c_device like I mentioned in the previous review :) In addition, I took a look at the gpio library and they did it this way too.

Quoted 7 lines
Quoted 3 lines

Again, I don't see why the driver developer should care about its adapter since everything could be done behind the scenes for him.

Are you suggesting to separate the da9063_reset into client/driver parts ? Don't you think that it will add unnecessary complicity if our case ?

To make sure we understand each other, find below a diff of what it would look like the way I imagine it. It just makes the notion of adapter disappear from the i2c device driver, that's all. This is what is done in openSBI with gpio_pin structure, in Linux here
https://www.kernel.org/doc/html/latest/i2c/writing-clients.html#smbus-communication.

include/sbi_utils/i2c/i2c.h +4 -0
index 76cdb66..0f1671e 100644
@@ -50,6 +50,10 @@ struct i2c_adapter {
        struct sbi_dlist node;
 };
 
+struct i2c_device {
+       struct i2c_adapter *adap;
+};
+
 static inline struct i2c_adapter *to_i2c_adapter(struct sbi_dlist
 *node) {
        return container_of(node, struct i2c_adapter, node);
lib/utils/i2c/i2c.c +8 -8
index d23ac91..9c64d09 100644
@@ -61,25 +61,25 @@ int i2c_adapter_configure(struct i2c_adapter *ia)
        return ia->configure(ia);
 }
 
-int i2c_adapter_smbus_write(struct i2c_adapter *ia, uint8_t addr,
+int i2c_smbus_write(struct i2c_device *dev, uint8_t addr,
                        uint8_t reg, uint8_t *buffer, int len)
 {
-       if (!ia)
+       if (!dev)
                return SBI_EINVAL;
-       if (!ia->smbus_write)
+       if (!dev->adap || !dev->adap->smbus_write)
                return SBI_ENOSYS;
 
-       return ia->smbus_write(ia, addr, reg, buffer, len);
+       return dev->adap->smbus_write(dev, addr, reg, buffer, len);
 }
 
 
-int i2c_adapter_smbus_read(struct i2c_adapter *ia, uint8_t addr,
+int i2c_smbus_read(struct i2c_device *dev, uint8_t addr,
                     uint8_t reg, uint8_t *buffer, int len)
 {
-       if (!ia)
+       if (!dev)
                return SBI_EINVAL;
-       if (!ia->smbus_read)
+       if (!dev->adap || !dev->adap->smbus_read)
                return SBI_ENOSYS;
 
-       return ia->smbus_read(ia, addr, reg, buffer, len);
+       return dev->adap->smbus_read(dev, addr, reg, buffer, len);
 }
lib/utils/reset/fdt_reset_da9063.c +1 -2
index e3c9ced..a14d941 100644
@@ -45,7 +45,7 @@
 #define PMIC_CHIP_ID_DA9063            0x61
 
 static struct {
-       struct i2c_adapter *adapter;
+       struct i2c_device *dev;
        uint32_t reg;
        u8 priority;
 } da9063 = {
@@ -103,20 +103,20 @@ static inline int da9063_shutdown(struct
 i2c_adapter *adap, uint32_t reg)
                                DA9063_REG_CONTROL_F,
 DA9063_CONTROL_F_SHUTDOWN);
 }
 
-static inline int da9063_reset(struct i2c_adapter *adap, uint32_t

reg) +static inline int da9063_reset(struct i2c_device *dev, uint32_t reg) {

lib/utils/reset/fdt_reset_da9063.c +2 -2
-       int rc = i2c_adapter_smbus_reg_write(adap, da9063.reg,
+       int rc = i2c_smbus_reg_write(dev, da9063.reg,
                                        DA9063_REG_PAGE_CON, 0x00);
 
        if (rc)
                return rc;
 
-       rc = i2c_adapter_smbus_reg_write(adap, da9063.reg,
+       rc = i2c_smbus_reg_write(dev, da9063.reg,
                              DA9063_REG_CONTROL_F,

DA9063_CONTROL_F_WAKEUP); if (rc)

                return rc;
lib/utils/reset/fdt_reset_da9063.c +1 -2
-       return i2c_adapter_smbus_reg_write(adap, da9063.reg,
+       return i2c_smbus_reg_write(dev, da9063.reg,
                                DA9063_REG_CONTROL_A,
                                DA9063_CONTROL_A_M_POWER1_EN |
                                DA9063_CONTROL_A_M_POWER_EN |
@@ -169,7 +169,6 @@ static int da9063_reset_init(void *fdt, int
 nodeoff, {
        int rc, i2c_dev, i2c_bus, len;
        const fdt32_t *val;
-       struct i2c_adapter *adapter;
        uint64_t addr;
 
        /* find dlg,da9063 parent node */
@@ -192,12 +191,10 @@ static int da9063_reset_init(void *fdt, int
 nodeoff, return i2c_bus;
 
        /* i2c adapter get */

lib/utils/reset/fdt_reset_da9063.c static int da9063_reset_init(void *fdt, int · line 200

Quoted 10 lines
lib/utils/reset/fdt_reset_da9063.c +1 -3
-       rc = fdt_i2c_adapter_get(fdt, i2c_bus, &adapter);
+       rc = fdt_i2c_adapter_get(fdt, i2c_bus, &da9063.dev);
        if (rc)
                return rc;
 
-       da9063.adapter = adapter;
-
        sbi_system_reset_add_device(&da9063_reset_i2c);
 
        return 0;

So you indeed suggesting to add one level of abstraction.

I don't see any strong benefit from this.

Who will take care of device creation/initialisation in this case - the adapter ?

Quoted 51 lines
Quoted 51 lines

Linux i2c_client has a reference to the adapter it's sitting on:
https://elixir.bootlin.com/linux/v5.15-rc6/source/include/linux/i2c.h#L336

u-boot is relying on some "default" i2c adapter/bus ideed, but if we look on:

https://elixir.bootlin.com/u-boot/latest/source/include/i2c.h#L718

We see it's rather complex and hard to understand.

Quoted 36 lines
Quoted 36 lines

Nikita Shubin (5):
lib: utils/reset: add priority to gpio reset
lib: utils/i2c: Add generic I2C configuration library
lib: utils/i2c: Add simple FDT based I2C framework
lib: utils/i2c: Add minimal SiFive I2C driver
lib: utils/reset: Add generic da9063 reset driver

 include/sbi_utils/i2c/fdt_i2c.h    |  26 +++
 include/sbi_utils/i2c/i2c.h        |  99 +++++++++++
 lib/utils/i2c/fdt_i2c.c            | 111 ++++++++++++
 lib/utils/i2c/fdt_i2c_sifive.c     | 271
+++++++++++++++++++++++++++++ lib/utils/i2c/i2c.c
 | 85 +++++++++ lib/utils/i2c/objects.mk           |  12 ++
 lib/utils/reset/fdt_reset.c        |   2 +
 lib/utils/reset/fdt_reset_da9063.c | 214
+++++++++++++++++++++++ lib/utils/reset/fdt_reset_gpio.c   |
18 +- lib/utils/reset/objects.mk         |   1 +
 10 files changed, 836 insertions(+), 3 deletions(-)
 create mode 100644 include/sbi_utils/i2c/fdt_i2c.h
 create mode 100644 include/sbi_utils/i2c/i2c.h
 create mode 100644 lib/utils/i2c/fdt_i2c.c
 create mode 100644 lib/utils/i2c/fdt_i2c_sifive.c
 create mode 100644 lib/utils/i2c/i2c.c
 create mode 100644 lib/utils/i2c/objects.mk
 create mode 100644 lib/utils/reset/fdt_reset_da9063.c

--
2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#35 lore raw

v2 cover I2C framework, reboot Unmatched via PMIC

Quoted 170 lines · Nikita Shubin
Quoted 8 lines · On Wed, 20 Oct 2021 10:11:56 +0200 Alexandre Ghiti…
Quoted 3 lines

It part of driver responsibility to enable clocks it requires, cause if not is required nobody will enable them.

I understand that but why couldn't this configuration be done in the adapter driver instead of the da9063 driver? At the initialization of the adapter driver, or at each beginning of a write sequence...etc.

I think we should stick to "don't touch until you really need", that's why we shouldn't do it in driver init, we can enable clocks on unused bus which will lead to more power consumption, and other's won't be aware that it needs to be gated off (and even so it's need to gated on once again).

Also it's really good to disable interrupts when you don't need them.

Doing it every read/write seems quite wrong to me, that's why i decided to add a separate method for such tasks.

Quoted 146 lines
Quoted 17 lines
Quoted 17 lines
Quoted 9 lines
Quoted 6 lines

Establishing the link between the i2c device and its adapter should somehow be implicitly done by the i2c library, IMO the device should not care about its controller.

And how do you think it will look like ? In out case it's not a separate driver/client entity but all togather for simplicity.

In fdt_i2c_adapter_get, instead of passing a pointer to a struct i2c_adapter, we could pass a i2c_device structure which would get filled automatically with its adapter chip. But that implies introducing this i2c_device like I mentioned in the previous review :) In addition, I took a look at the gpio library and they did it this way too.

Quoted 7 lines
Quoted 3 lines

Again, I don't see why the driver developer should care about its adapter since everything could be done behind the scenes for him.

Are you suggesting to separate the da9063_reset into client/driver parts ? Don't you think that it will add unnecessary complicity if our case ?

To make sure we understand each other, find below a diff of what it would look like the way I imagine it. It just makes the notion of adapter disappear from the i2c device driver, that's all. This is what is done in openSBI with gpio_pin structure, in Linux here
https://www.kernel.org/doc/html/latest/i2c/writing-clients.html#smbus-communication.

include/sbi_utils/i2c/i2c.h +4 -0
index 76cdb66..0f1671e 100644
@@ -50,6 +50,10 @@ struct i2c_adapter {
        struct sbi_dlist node;
 };
 
+struct i2c_device {
+       struct i2c_adapter *adap;
+};
+
 static inline struct i2c_adapter *to_i2c_adapter(struct sbi_dlist
 *node) {
        return container_of(node, struct i2c_adapter, node);
lib/utils/i2c/i2c.c +8 -8
index d23ac91..9c64d09 100644
@@ -61,25 +61,25 @@ int i2c_adapter_configure(struct i2c_adapter *ia)
        return ia->configure(ia);
 }
 
-int i2c_adapter_smbus_write(struct i2c_adapter *ia, uint8_t addr,
+int i2c_smbus_write(struct i2c_device *dev, uint8_t addr,
                        uint8_t reg, uint8_t *buffer, int len)
 {
-       if (!ia)
+       if (!dev)
                return SBI_EINVAL;
-       if (!ia->smbus_write)
+       if (!dev->adap || !dev->adap->smbus_write)
                return SBI_ENOSYS;
 
-       return ia->smbus_write(ia, addr, reg, buffer, len);
+       return dev->adap->smbus_write(dev, addr, reg, buffer, len);
 }
 
 
-int i2c_adapter_smbus_read(struct i2c_adapter *ia, uint8_t addr,
+int i2c_smbus_read(struct i2c_device *dev, uint8_t addr,
                     uint8_t reg, uint8_t *buffer, int len)
 {
-       if (!ia)
+       if (!dev)
                return SBI_EINVAL;
-       if (!ia->smbus_read)
+       if (!dev->adap || !dev->adap->smbus_read)
                return SBI_ENOSYS;
 
-       return ia->smbus_read(ia, addr, reg, buffer, len);
+       return dev->adap->smbus_read(dev, addr, reg, buffer, len);
 }
lib/utils/reset/fdt_reset_da9063.c +1 -2
index e3c9ced..a14d941 100644
@@ -45,7 +45,7 @@
 #define PMIC_CHIP_ID_DA9063            0x61
 
 static struct {
-       struct i2c_adapter *adapter;
+       struct i2c_device *dev;
        uint32_t reg;
        u8 priority;
 } da9063 = {
@@ -103,20 +103,20 @@ static inline int da9063_shutdown(struct
 i2c_adapter *adap, uint32_t reg)
                                DA9063_REG_CONTROL_F,
 DA9063_CONTROL_F_SHUTDOWN);
 }
 
-static inline int da9063_reset(struct i2c_adapter *adap, uint32_t

reg) +static inline int da9063_reset(struct i2c_device *dev, uint32_t reg) {

lib/utils/reset/fdt_reset_da9063.c +2 -2
-       int rc = i2c_adapter_smbus_reg_write(adap, da9063.reg,
+       int rc = i2c_smbus_reg_write(dev, da9063.reg,
                                        DA9063_REG_PAGE_CON, 0x00);
 
        if (rc)
                return rc;
 
-       rc = i2c_adapter_smbus_reg_write(adap, da9063.reg,
+       rc = i2c_smbus_reg_write(dev, da9063.reg,
                              DA9063_REG_CONTROL_F,

DA9063_CONTROL_F_WAKEUP); if (rc)

                return rc;
lib/utils/reset/fdt_reset_da9063.c +2 -5
-       return i2c_adapter_smbus_reg_write(adap, da9063.reg,
+       return i2c_smbus_reg_write(dev, da9063.reg,
                                DA9063_REG_CONTROL_A,
                                DA9063_CONTROL_A_M_POWER1_EN |
                                DA9063_CONTROL_A_M_POWER_EN |
@@ -169,7 +169,6 @@ static int da9063_reset_init(void *fdt, int
 nodeoff, {
        int rc, i2c_dev, i2c_bus, len;
        const fdt32_t *val;
-       struct i2c_adapter *adapter;
        uint64_t addr;
 
        /* find dlg,da9063 parent node */
@@ -192,12 +191,10 @@ static int da9063_reset_init(void *fdt, int
 nodeoff, return i2c_bus;
 
        /* i2c adapter get */
-       rc = fdt_i2c_adapter_get(fdt, i2c_bus, &adapter);
+       rc = fdt_i2c_adapter_get(fdt, i2c_bus, &da9063.dev);
        if (rc)
                return rc;
 
-       da9063.adapter = adapter;
-
        sbi_system_reset_add_device(&da9063_reset_i2c);
 
Quoted 6 lines

lib/utils/reset/fdt_reset_da9063.c static int da9063_reset_init(void *fdt, int · line 200

Quoted 2 lines
lib/utils/reset/fdt_reset_da9063.c +0 -0
        return 0;

So you indeed suggesting to add one level of abstraction.

I don't see any strong benefit from this.

Ok.

Quoted 56 lines

Who will take care of device creation/initialisation in this case - the adapter ?

Quoted 51 lines
Quoted 51 lines

Linux i2c_client has a reference to the adapter it's sitting on:
https://elixir.bootlin.com/linux/v5.15-rc6/source/include/linux/i2c.h#L336

u-boot is relying on some "default" i2c adapter/bus ideed, but if we look on:

https://elixir.bootlin.com/u-boot/latest/source/include/i2c.h#L718

We see it's rather complex and hard to understand.

Quoted 36 lines
Quoted 36 lines

Nikita Shubin (5):
lib: utils/reset: add priority to gpio reset
lib: utils/i2c: Add generic I2C configuration library
lib: utils/i2c: Add simple FDT based I2C framework
lib: utils/i2c: Add minimal SiFive I2C driver
lib: utils/reset: Add generic da9063 reset driver

 include/sbi_utils/i2c/fdt_i2c.h    |  26 +++
 include/sbi_utils/i2c/i2c.h        |  99 +++++++++++
 lib/utils/i2c/fdt_i2c.c            | 111 ++++++++++++
 lib/utils/i2c/fdt_i2c_sifive.c     | 271
+++++++++++++++++++++++++++++ lib/utils/i2c/i2c.c
 | 85 +++++++++ lib/utils/i2c/objects.mk           |  12 ++
 lib/utils/reset/fdt_reset.c        |   2 +
 lib/utils/reset/fdt_reset_da9063.c | 214
+++++++++++++++++++++++ lib/utils/reset/fdt_reset_gpio.c   |
18 +- lib/utils/reset/objects.mk         |   1 +
 10 files changed, 836 insertions(+), 3 deletions(-)
 create mode 100644 include/sbi_utils/i2c/fdt_i2c.h
 create mode 100644 include/sbi_utils/i2c/i2c.h
 create mode 100644 lib/utils/i2c/fdt_i2c.c
 create mode 100644 lib/utils/i2c/fdt_i2c_sifive.c
 create mode 100644 lib/utils/i2c/i2c.c
 create mode 100644 lib/utils/i2c/objects.mk
 create mode 100644 lib/utils/reset/fdt_reset_da9063.c

--
2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#36 lore raw

v2 cover I2C framework, reboot Unmatched via PMIC

In reply to Nikita Shubin
Quoted 82 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

This series introduce rebooting via i2c da9063 PMIC, currently on SiFive Unmatched board.

"gpio-poweroff" remains with default priority of 128 - default priority, da9063 is 1 by default the least priority.

da9063-reset {
        compatible = "da9063-reset";
        priority = <1>;
};

Is required to be added as a child node of PMIC.

tested via Linux and U-Boot with reset extension:

OpenSBI:
Platform Reboot Device : da9063-reset
Platform Shutdown Device : gpio-reset

v1 -> v2:
Added:
lib: sbi: add priority for reset handler

Renamed read/write to smbus_write/smbus_read, as actually this is not a "raw" read/write but a one with a register address provided, later a "raw" version will be need but currently i don't have anything to test it.

To Xiang W:
I have analized your proposal of switching to sbi_list, and switched i2c adapters array to list. Unfortunately to switch drivers we require "init" functions.

to Alexander Ghiti:

Quoted 1 line

No need for the struct i2c_adapter argument as it is globally accessible.

It looks like a more clean and reusable way to me, let's here more opinions

Quoted 3 lines

Why should the da9063 device care about the adapter configuration here? I think It should just rely on an already configured/initialized i2c controller.

With clocks init added to sifive_i2c configure it can become usable even if nobody cared about controller initialization.

Quoted 3 lines

Establishing the link between the i2c device and its adapter should somehow be implicitly done by the i2c library, IMO the device should not care about its controller.

And how do you think it will look like ? In out case it's not a separate driver/client entity but all togather for simplicity.

Nikita Shubin (5):
lib: utils/reset: add priority to gpio reset
lib: utils/i2c: Add generic I2C configuration library
lib: utils/i2c: Add simple FDT based I2C framework
lib: utils/i2c: Add minimal SiFive I2C driver
lib: utils/reset: Add generic da9063 reset driver

 include/sbi_utils/i2c/fdt_i2c.h    |  26 +++
 include/sbi_utils/i2c/i2c.h        |  99 +++++++++++
 lib/utils/i2c/fdt_i2c.c            | 111 ++++++++++++
 lib/utils/i2c/fdt_i2c_sifive.c     | 271 +++++++++++++++++++++++++++++
 lib/utils/i2c/i2c.c                |  85 +++++++++
 lib/utils/i2c/objects.mk           |  12 ++
 lib/utils/reset/fdt_reset.c        |   2 +
 lib/utils/reset/fdt_reset_da9063.c | 214 +++++++++++++++++++++++
 lib/utils/reset/fdt_reset_gpio.c   |  18 +-
 lib/utils/reset/objects.mk         |   1 +
 10 files changed, 836 insertions(+), 3 deletions(-)
 create mode 100644 include/sbi_utils/i2c/fdt_i2c.h
 create mode 100644 include/sbi_utils/i2c/i2c.h
 create mode 100644 lib/utils/i2c/fdt_i2c.c
 create mode 100644 lib/utils/i2c/fdt_i2c_sifive.c
 create mode 100644 lib/utils/i2c/i2c.c
 create mode 100644 lib/utils/i2c/objects.mk
 create mode 100644 lib/utils/reset/fdt_reset_da9063.c
Quoted 3 lines

--
2.31.1

I'm testing your patchset and although it seems to work from Linux, it fails when used from u-boot at the first smbus_write in da9063_sanity_check. Maybe it relies on some initialization done by the Linux driver?

Thanks,

Alex

Quoted 5 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#37 lore raw

v2 cover I2C framework, reboot Unmatched via PMIC

Hello Alexandre!

Quoted 85 lines · On Wed, 20 Oct 2021 06:59:25 +0200 Alexandre Ghiti…
Quoted 83 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

This series introduce rebooting via i2c da9063 PMIC, currently on SiFive Unmatched board.

"gpio-poweroff" remains with default priority of 128 - default priority, da9063 is 1 by default the least priority.

da9063-reset {
        compatible = "da9063-reset";
        priority = <1>;
};

Is required to be added as a child node of PMIC.

tested via Linux and U-Boot with reset extension:

OpenSBI:
Platform Reboot Device : da9063-reset
Platform Shutdown Device : gpio-reset

v1 -> v2:
Added:
lib: sbi: add priority for reset handler

Renamed read/write to smbus_write/smbus_read, as actually this is not a "raw" read/write but a one with a register address provided, later a "raw" version will be need but currently i don't have anything to test it.

To Xiang W:
I have analized your proposal of switching to sbi_list, and switched i2c adapters array to list. Unfortunately to switch drivers we require "init" functions.

to Alexander Ghiti:

Quoted 2 lines

No need for the struct i2c_adapter argument as it is globally accessible.

It looks like a more clean and reusable way to me, let's here more opinions

Quoted 3 lines

Why should the da9063 device care about the adapter configuration here? I think It should just rely on an already configured/initialized i2c controller.

With clocks init added to sifive_i2c configure it can become usable even if nobody cared about controller initialization.

Quoted 3 lines

Establishing the link between the i2c device and its adapter should somehow be implicitly done by the i2c library, IMO the device should not care about its controller.

And how do you think it will look like ? In out case it's not a separate driver/client entity but all togather for simplicity.

Nikita Shubin (5):
lib: utils/reset: add priority to gpio reset
lib: utils/i2c: Add generic I2C configuration library
lib: utils/i2c: Add simple FDT based I2C framework
lib: utils/i2c: Add minimal SiFive I2C driver
lib: utils/reset: Add generic da9063 reset driver

 include/sbi_utils/i2c/fdt_i2c.h    |  26 +++
 include/sbi_utils/i2c/i2c.h        |  99 +++++++++++
 lib/utils/i2c/fdt_i2c.c            | 111 ++++++++++++
 lib/utils/i2c/fdt_i2c_sifive.c     | 271
+++++++++++++++++++++++++++++ lib/utils/i2c/i2c.c                |
85 +++++++++ lib/utils/i2c/objects.mk           |  12 ++
 lib/utils/reset/fdt_reset.c        |   2 +
 lib/utils/reset/fdt_reset_da9063.c | 214 +++++++++++++++++++++++
 lib/utils/reset/fdt_reset_gpio.c   |  18 +-
 lib/utils/reset/objects.mk         |   1 +
 10 files changed, 836 insertions(+), 3 deletions(-)
 create mode 100644 include/sbi_utils/i2c/fdt_i2c.h
 create mode 100644 include/sbi_utils/i2c/i2c.h
 create mode 100644 lib/utils/i2c/fdt_i2c.c
 create mode 100644 lib/utils/i2c/fdt_i2c_sifive.c
 create mode 100644 lib/utils/i2c/i2c.c
 create mode 100644 lib/utils/i2c/objects.mk
 create mode 100644 lib/utils/reset/fdt_reset_da9063.c
Quoted 9 lines
Quoted 3 lines

--
2.31.1

I'm testing your patchset and although it seems to work from Linux, it fails when used from u-boot at the first smbus_write in da9063_sanity_check. Maybe it relies on some initialization done by the Linux driver?

That's sound strange becouse i explicitly tested with u-boot SBI extension, well in fact i tested mostly via u-boot because it's quicker than booting Linux.

Do you have CONFIG_SYS_I2C_OCORES enabled ?

I tested on u-boot v2021.07 with Sifive Unmatched patches and SBI reset extension applied. May it is time for configure ;) ?

Sharing my u-boot branch:
https://github.com/maquefel/u-boot/tree/yadro/riscv/unmatched-v2021.07

Quoted 10 lines

Thanks,

Alex

Quoted 5 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#38 lore raw

v2 cover I2C framework, reboot Unmatched via PMIC

Quoted 105 lines · Nikita Shubin

Hello Alexandre!

Quoted 85 lines · On Wed, 20 Oct 2021 06:59:25 +0200 Alexandre Ghiti…
Quoted 83 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

This series introduce rebooting via i2c da9063 PMIC, currently on SiFive Unmatched board.

"gpio-poweroff" remains with default priority of 128 - default priority, da9063 is 1 by default the least priority.

da9063-reset {
        compatible = "da9063-reset";
        priority = <1>;
};

Is required to be added as a child node of PMIC.

tested via Linux and U-Boot with reset extension:

OpenSBI:
Platform Reboot Device : da9063-reset
Platform Shutdown Device : gpio-reset

v1 -> v2:
Added:
lib: sbi: add priority for reset handler

Renamed read/write to smbus_write/smbus_read, as actually this is not a "raw" read/write but a one with a register address provided, later a "raw" version will be need but currently i don't have anything to test it.

To Xiang W:
I have analized your proposal of switching to sbi_list, and switched i2c adapters array to list. Unfortunately to switch drivers we require "init" functions.

to Alexander Ghiti:

Quoted 2 lines

No need for the struct i2c_adapter argument as it is globally accessible.

It looks like a more clean and reusable way to me, let's here more opinions

Quoted 3 lines

Why should the da9063 device care about the adapter configuration here? I think It should just rely on an already configured/initialized i2c controller.

With clocks init added to sifive_i2c configure it can become usable even if nobody cared about controller initialization.

Quoted 3 lines

Establishing the link between the i2c device and its adapter should somehow be implicitly done by the i2c library, IMO the device should not care about its controller.

And how do you think it will look like ? In out case it's not a separate driver/client entity but all togather for simplicity.

Nikita Shubin (5):
lib: utils/reset: add priority to gpio reset
lib: utils/i2c: Add generic I2C configuration library
lib: utils/i2c: Add simple FDT based I2C framework
lib: utils/i2c: Add minimal SiFive I2C driver
lib: utils/reset: Add generic da9063 reset driver

 include/sbi_utils/i2c/fdt_i2c.h    |  26 +++
 include/sbi_utils/i2c/i2c.h        |  99 +++++++++++
 lib/utils/i2c/fdt_i2c.c            | 111 ++++++++++++
 lib/utils/i2c/fdt_i2c_sifive.c     | 271
+++++++++++++++++++++++++++++ lib/utils/i2c/i2c.c                |
85 +++++++++ lib/utils/i2c/objects.mk           |  12 ++
 lib/utils/reset/fdt_reset.c        |   2 +
 lib/utils/reset/fdt_reset_da9063.c | 214 +++++++++++++++++++++++
 lib/utils/reset/fdt_reset_gpio.c   |  18 +-
 lib/utils/reset/objects.mk         |   1 +
 10 files changed, 836 insertions(+), 3 deletions(-)
 create mode 100644 include/sbi_utils/i2c/fdt_i2c.h
 create mode 100644 include/sbi_utils/i2c/i2c.h
 create mode 100644 lib/utils/i2c/fdt_i2c.c
 create mode 100644 lib/utils/i2c/fdt_i2c_sifive.c
 create mode 100644 lib/utils/i2c/i2c.c
 create mode 100644 lib/utils/i2c/objects.mk
 create mode 100644 lib/utils/reset/fdt_reset_da9063.c
Quoted 9 lines
Quoted 3 lines

--
2.31.1

I'm testing your patchset and although it seems to work from Linux, it fails when used from u-boot at the first smbus_write in da9063_sanity_check. Maybe it relies on some initialization done by the Linux driver?

That's sound strange becouse i explicitly tested with u-boot SBI extension, well in fact i tested mostly via u-boot because it's quicker than booting Linux.

Quoted 7 lines

Do you have CONFIG_SYS_I2C_OCORES enabled ?

I tested on u-boot v2021.07 with Sifive Unmatched patches and SBI reset extension applied. May it is time for configure ;) ?

Sharing my u-boot branch:
https://github.com/maquefel/u-boot/tree/yadro/riscv/unmatched-v2021.07

That's weird, it still does not work for me from u-boot with the following:

- openSBI: https://github.com/AlexGhiti/opensbi/tree/int/alex/nikita_pmic_reset_v2
- u-boot: https://github.com/maquefel/u-boot/tree/yadro/riscv/unmatched-v2021.07

And this fails this way:

=> reset
resetting ...
da9063_system_reset: chip is not da9063 PMIC

Alex

Quoted 13 lines
Quoted 12 lines

Thanks,

Alex

Quoted 5 lines

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#39 lore raw

v2 cover I2C framework, reboot Unmatched via PMIC

Hello Alexandre!

Quoted 9 lines · On Tue, 26 Oct 2021 08:34:17 +0200 Alexandre Ghiti…

That's weird, it still does not work for me from u-boot with the following:

- openSBI:
https://github.com/AlexGhiti/opensbi/tree/int/alex/nikita_pmic_reset_v2
- u-boot:
https://github.com/maquefel/u-boot/tree/yadro/riscv/unmatched-v2021.07

Quoted 6 lines

And this fails this way:

=> reset
resetting ...
da9063_system_reset: chip is not da9063 PMIC

Thank you for testing once again!

I'll try to hug the board and don't release it until i conduct some experiments.

Currently i am fighting the RTC, to find out if it is usable:

In short i get one and only one ALARM interrupt, and no further interrupts:

https://forums.sifive.com/t/has-anyone-got-da9063-rtc-working-from-linux/5302

And currently trying to find out if reset is related to this somehow...