Skip to contentSkip to conversation

I2C framework, reboot Unmatched via PMIC

PATCH Superseded Nikita Shubin 5 participants 26 messages Nov 10, 2021 – Nov 12, 2021 SiFive drivers

lore
Share & tools
lore
#0 lore raw

v4 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", "gpio-reset" remains with default priority of 128 - default priority, da9063 is 1 by default the least priority for shutdown and highest priority for reset.

da9063 resides in platform/generic code and is used only for sifive_fu740, in case if da9063 is found in device tree.

Alexandre, Heinrich and Anup, the reason of "da9063_system_reset: chip is not da9063 PMIC" wasn't the initialization, the peripheral clocks are initiated by ZSBL, the reason was incorrect timeouts in sifive_i2c_adapter_poll (100ms is really too much, my bad), with polling reworked everything is working smooth regardless of last reset type.

The RTC ALARM reboot seems working, but implementing it in OpenSBI will require a full-scale calendar logic (for day/month correct rollover) which is undesirable, also we sacrifice the ability to wake up on ALARM.

I am currently experimentings with TICK ALARM approach which should behave just like the ALARM one, but i get wierd results with board being powered off and then preventing the onKEY power it back again (turns off shortly after key pressed), until cable plugged/unplugged (the BATTERY is missing).

It looks like OTP settings are messing with logic, a comment from SiFive is really needed.

So we should currently stick to approuch proposed by Alexandre it hasn't failed me so far.

v3->v4:
- fixed typo in include/sbi_utils/i2c/i2c.h
- fixed inaccurate configure remove, leaked into patch 0004
- move all fdt_reset_da9063 code to sifive_fu740 and removed fdt_reset_da9063

Nikita Shubin (5):
lib: utils/reset: add priority to gpio reset
lib: utils/reset: separate driver init func
lib: utils/i2c: Add generic I2C configuration library
lib: utils/i2c: Add simple FDT based I2C framework
platform: sifive_fu740: add platform reset driver

 include/sbi_utils/i2c/fdt_i2c.h     |  26 ++++
 include/sbi_utils/i2c/i2c.h         |  85 +++++++++++++
 include/sbi_utils/reset/fdt_reset.h |   5 +
 lib/utils/i2c/fdt_i2c.c             |  90 ++++++++++++++
 lib/utils/i2c/i2c.c                 |  75 +++++++++++
 lib/utils/i2c/objects.mk            |  12 ++
 lib/utils/reset/fdt_reset.c         |  37 +++---
 lib/utils/reset/fdt_reset_gpio.c    |  10 +-
 platform/generic/fdt_reset_da9063.c | 186 ++++++++++++++++++++++++++++
 platform/generic/objects.mk         |   1 +
 platform/generic/sifive_fu740.c     |  22 ++++
 11 files changed, 531 insertions(+), 18 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/i2c.c
 create mode 100644 lib/utils/i2c/objects.mk
 create mode 100644 platform/generic/fdt_reset_da9063.c
Signature · 1 line
2.31.1
#1 lore raw

PATCH v4 1/6 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.

---
 lib/utils/reset/fdt_reset_gpio.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
lib/utils/reset/fdt_reset_gpio.c +8 -2
index 302035b..d28b6f5 100644
@@ -77,7 +77,10 @@ static void gpio_reset_exec(struct gpio_reset *reset)
 
 static int gpio_system_poweroff_check(u32 type, u32 reason)
 {
-	return !!gpio_reset_get(FALSE, type);
+	if (gpio_reset_get(FALSE, type))
+		return 128;
+
+	return 0;
 }
 
 static void gpio_system_poweroff(u32 type, u32 reason)
@@ -93,7 +96,10 @@ static struct sbi_system_reset_device gpio_poweroff = {
 
 static int gpio_system_restart_check(u32 type, u32 reason)
 {
-	return !!gpio_reset_get(TRUE, type);
+	if (gpio_reset_get(TRUE, type))
+		return 128;
+
+	return 0;
 }
 
 static void gpio_system_restart(u32 type, u32 reason)
Signature · 1 line
2.31.1
Applied
#2 lore raw

PATCH v4 1/6 lib: utils/reset: add priority to gpio reset

Quoted 11 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

Make gpio_system_reset_check return priority instead of just true/false.

Make default 128 priority for reset/shutdown.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 40 lines
---
 lib/utils/reset/fdt_reset_gpio.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
lib/utils/reset/fdt_reset_gpio.c +8 -3
index 302035b..d28b6f5 100644
@@ -77,7 +77,10 @@ static void gpio_reset_exec(struct gpio_reset *reset)
 
 static int gpio_system_poweroff_check(u32 type, u32 reason)
 {
-       return !!gpio_reset_get(FALSE, type);
+       if (gpio_reset_get(FALSE, type))
+               return 128;
+
+       return 0;
 }
 
 static void gpio_system_poweroff(u32 type, u32 reason)
@@ -93,7 +96,10 @@ static struct sbi_system_reset_device gpio_poweroff = {
 
 static int gpio_system_restart_check(u32 type, u32 reason)
 {
-       return !!gpio_reset_get(TRUE, type);
+       if (gpio_reset_get(TRUE, type))
+               return 128;
+
+       return 0;
 }
 
 static void gpio_system_restart(u32 type, u32 reason)
--

2.31.1

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

#3 lore raw

PATCH v4 2/6 lib: utils/reset: separate driver init func

In reply to Nikita Shubin

From: Nikita Shubin <[email protected]>

Move driver init code to separate function, so it can be reused elsewhere.

---
 include/sbi_utils/reset/fdt_reset.h |  5 ++++
 lib/utils/reset/fdt_reset.c         | 37 ++++++++++++++++-------------
 2 files changed, 26 insertions(+), 16 deletions(-)
include/sbi_utils/reset/fdt_reset.h +5 -0
index 46167b9..e7f7350 100644
@@ -17,6 +17,11 @@ struct fdt_reset {
 	int (*init)(void *fdt, int nodeoff, const struct fdt_match *match);
 };
 
+/**
+ * fdt_reset_driver_init() - initialize reset driver based on the device-tree
+ */
+int fdt_reset_driver_init(void *fdt, struct fdt_reset *drv);
+
 /**
  * fdt_reset_init() - initialize reset drivers based on the device-tree
  *
lib/utils/reset/fdt_reset.c +21 -16
index f4befa2..66281cb 100644
@@ -29,26 +29,31 @@ static struct fdt_reset *reset_drivers[] = {
 	&fdt_reset_thead,
 };
 
-void fdt_reset_init(void)
+int fdt_reset_driver_init(void *fdt, struct fdt_reset *drv)
 {
-	int pos, noff, rc;
-	struct fdt_reset *drv;
+	int noff, rc = SBI_ENODEV;
 	const struct fdt_match *match;
-	void *fdt = fdt_get_address();
-
-	for (pos = 0; pos < array_size(reset_drivers); pos++) {
-		drv = reset_drivers[pos];
 
-		noff = fdt_find_match(fdt, -1, drv->match_table, &match);
-		if (noff < 0)
-			continue;
+	noff = fdt_find_match(fdt, -1, drv->match_table, &match);
+	if (noff < 0)
+		return SBI_ENODEV;
 
-		if (drv->init) {
-			rc = drv->init(fdt, noff, match);
-			if (rc && rc != SBI_ENODEV) {
-				sbi_printf("%s: %s init failed, %d\n",
-					   __func__, match->compatible, rc);
-			}
+	if (drv->init) {
+		rc = drv->init(fdt, noff, match);
+		if (rc && rc != SBI_ENODEV) {
+			sbi_printf("%s: %s init failed, %d\n",
+				   __func__, match->compatible, rc);
 		}
 	}
+
+	return rc;
+}
+
+void fdt_reset_init(void)
+{
+	int pos;
+	void *fdt = fdt_get_address();
+
+	for (pos = 0; pos < array_size(reset_drivers); pos++)
+		fdt_reset_driver_init(fdt, reset_drivers[pos]);
 }
Signature · 1 line
2.31.1
Applied
#4 lore raw

PATCH v4 2/6 lib: utils/reset: separate driver init func

Quoted 10 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

Move driver init code to separate function, so it can be reused elsewhere.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 81 lines
---
 include/sbi_utils/reset/fdt_reset.h |  5 ++++
 lib/utils/reset/fdt_reset.c         | 37 ++++++++++++++++-------------
 2 files changed, 26 insertions(+), 16 deletions(-)
include/sbi_utils/reset/fdt_reset.h +5 -0
index 46167b9..e7f7350 100644
@@ -17,6 +17,11 @@ struct fdt_reset {
        int (*init)(void *fdt, int nodeoff, const struct fdt_match *match);
 };
 
+/**
+ * fdt_reset_driver_init() - initialize reset driver based on the device-tree
+ */
+int fdt_reset_driver_init(void *fdt, struct fdt_reset *drv);
+
 /**
  * fdt_reset_init() - initialize reset drivers based on the device-tree
  *
lib/utils/reset/fdt_reset.c +21 -17
index f4befa2..66281cb 100644
@@ -29,26 +29,31 @@ static struct fdt_reset *reset_drivers[] = {
        &fdt_reset_thead,
 };
 
-void fdt_reset_init(void)
+int fdt_reset_driver_init(void *fdt, struct fdt_reset *drv)
 {
-       int pos, noff, rc;
-       struct fdt_reset *drv;
+       int noff, rc = SBI_ENODEV;
        const struct fdt_match *match;
-       void *fdt = fdt_get_address();
-
-       for (pos = 0; pos < array_size(reset_drivers); pos++) {
-               drv = reset_drivers[pos];
 
-               noff = fdt_find_match(fdt, -1, drv->match_table, &match);
-               if (noff < 0)
-                       continue;
+       noff = fdt_find_match(fdt, -1, drv->match_table, &match);
+       if (noff < 0)
+               return SBI_ENODEV;
 
-               if (drv->init) {
-                       rc = drv->init(fdt, noff, match);
-                       if (rc && rc != SBI_ENODEV) {
-                               sbi_printf("%s: %s init failed, %d\n",
-                                          __func__, match->compatible, rc);
-                       }
+       if (drv->init) {
+               rc = drv->init(fdt, noff, match);
+               if (rc && rc != SBI_ENODEV) {
+                       sbi_printf("%s: %s init failed, %d\n",
+                                  __func__, match->compatible, rc);
                }
        }
+
+       return rc;
+}
+
+void fdt_reset_init(void)
+{
+       int pos;
+       void *fdt = fdt_get_address();
+
+       for (pos = 0; pos < array_size(reset_drivers); pos++)
+               fdt_reset_driver_init(fdt, reset_drivers[pos]);
 }
--

2.31.1

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

#5 lore raw

PATCH v4 3/6 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...).

---
v3->v4:
- fixed typo in include/sbi_utils/i2c/i2c.h
- fixed inaccurate configure remove, leaked into patch 0004

---
 include/sbi_utils/i2c/i2c.h | 85 +++++++++++++++++++++++++++++++++++++
 lib/utils/i2c/i2c.c         | 75 ++++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk    | 10 +++++
 3 files changed, 170 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 +85 -0
new file mode 100644
index 0000000..79aeeb5
@@ -0,0 +1,85 @@
+/*
+ * 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;
+
+	/**
+	 * Send buffer to given address, register
+	 *
+	 * @return 0 on success and negative error code on failure
+	 */
+	int (*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 (*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);
+
+/** Send to device on I2C adapter bus */
+int i2c_adapter_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_read(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
+		     uint8_t *buffer, int len);
+
+static inline int i2c_adapter_reg_write(struct i2c_adapter *ia, uint8_t addr,
+				 uint8_t reg, uint8_t val)
+{
+	return i2c_adapter_write(ia, addr, reg, &val, 1);
+}
+
+static inline int i2c_adapter_reg_read(struct i2c_adapter *ia, uint8_t addr,
+				       uint8_t reg, uint8_t *val)
+{
+	uint8_t buf;
+	int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
+
+	if (ret)
+		return ret;
+
+	*val = buf;
+	return 0;
+}
+
+#endif
lib/utils/i2c/i2c.c new file +75 -0

75 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
#6 lore raw

PATCH v4 3/6 lib: utils/i2c: Add generic I2C configuration library

? 2021-11-10???? 12:42 +0300?Nikita Shubin???

Quoted 10 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...).

The i2c bus does not define register-related operations, these interfaces should be renamed smbus

Regards,
Xiang W

Quoted 216 lines

---
v3->v4:
- fixed typo in include/sbi_utils/i2c/i2c.h
- fixed inaccurate configure remove, leaked into patch 0004
---
?include/sbi_utils/i2c/i2c.h | 85
+++++++++++++++++++++++++++++++++++++
?lib/utils/i2c/i2c.c???????? | 75 ++++++++++++++++++++++++++++++++ ?lib/utils/i2c/objects.mk??? | 10 +++++
?3 files changed, 170 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..79aeeb5
@@ -0,0 +1,85 @@
+/*
+ * 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;
+
+???????/**
+??????? * Send buffer to given address, register
+??????? *
+??????? * @return 0 on success and negative error code on failure
+??????? */
+???????int (*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 (*read)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
+?????????????????? uint8_t *buffer, int len);

include/sbi_utils/i2c/i2c.h new file +6 -0
+
+???????/** 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 +15 -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);
+
+/** Send to device on I2C adapter bus */
+int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr, uint8_t

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

include/sbi_utils/i2c/i2c.h new file +3 -0
+
+/** Read from device on I2C adapter bus */
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t

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

include/sbi_utils/i2c/i2c.h new file +2 -0
+
+static inline int i2c_adapter_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_write(ia, addr, reg, &val, 1);
+}
+
+static inline int i2c_adapter_reg_read(struct i2c_adapter *ia,

uint8_t addr,
+????????????????????????????????????? uint8_t reg, uint8_t *val)

include/sbi_utils/i2c/i2c.h new file +12 -0
+{
+???????uint8_t buf;
+???????int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
+
+???????if (ret)
+???????????????return ret;
+
+???????*val = buf;
+???????return 0;
+}
+
+#endif
lib/utils/i2c/i2c.c new file +54 -0
new file mode 100644
index 0000000..1f5072c
@@ -0,0 +1,75 @@
+/*
+ * 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_write(struct i2c_adapter *ia, uint8_t addr, uint8_t

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

lib/utils/i2c/i2c.c new file +11 -0
+{
+???????if (!ia)
+???????????????return SBI_EINVAL;
+???????if (!ia->write)
+???????????????return SBI_ENOSYS;
+
+???????return ia->write(ia, addr, reg, buffer, len);
+}
+
+
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t

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

lib/utils/i2c/i2c.c new file +8 -0
+{
+???????if (!ia)
+???????????????return SBI_EINVAL;
+???????if (!ia->read)
+???????????????return SBI_ENOSYS;
+
+???????return ia->read(ia, addr, reg, buffer, len);
+}
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
#7 lore raw

PATCH v4 3/6 lib: utils/i2c: Add generic I2C configuration library

Quoted 7 lines · On Wed, 10 Nov 2021 21:01:39 +0800 Xiang W <[email protected]> wrote:

? 2021-11-10???? 12:42 +0300?Nikita Shubin???

Quoted 6 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...).

Quoted 6 lines
Quoted 4 lines

The i2c bus does not define register-related operations, these interfaces should be renamed smbus

You are right, it was in first series of patches and i dropped it as there is no non-smbus version currently.

So let's resend. I hope this change is trivial enough so i can keep the tags.

Quoted 221 lines

Regards,
Xiang W

Quoted 216 lines

---
v3->v4:
- fixed typo in include/sbi_utils/i2c/i2c.h
- fixed inaccurate configure remove, leaked into patch 0004
---
?include/sbi_utils/i2c/i2c.h | 85
+++++++++++++++++++++++++++++++++++++
?lib/utils/i2c/i2c.c???????? | 75 ++++++++++++++++++++++++++++++++ ?lib/utils/i2c/objects.mk??? | 10 +++++
?3 files changed, 170 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..79aeeb5
@@ -0,0 +1,85 @@
+/*
+ * 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;
+
+???????/**
+??????? * Send buffer to given address, register
+??????? *
+??????? * @return 0 on success and negative error code on failure
+??????? */
+???????int (*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 (*read)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
+?????????????????? uint8_t *buffer, int len);

include/sbi_utils/i2c/i2c.h new file +6 -0
+
+???????/** 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 +15 -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);
+
+/** Send to device on I2C adapter bus */
+int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr, uint8_t

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

include/sbi_utils/i2c/i2c.h new file +3 -0
+
+/** Read from device on I2C adapter bus */
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t

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

include/sbi_utils/i2c/i2c.h new file +2 -0
+
+static inline int i2c_adapter_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_write(ia, addr, reg, &val, 1);
+}
+
+static inline int i2c_adapter_reg_read(struct i2c_adapter *ia,

uint8_t addr,
+????????????????????????????????????? uint8_t reg, uint8_t *val)

include/sbi_utils/i2c/i2c.h new file +12 -0
+{
+???????uint8_t buf;
+???????int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
+
+???????if (ret)
+???????????????return ret;
+
+???????*val = buf;
+???????return 0;
+}
+
+#endif
lib/utils/i2c/i2c.c new file +54 -0
new file mode 100644
index 0000000..1f5072c
@@ -0,0 +1,75 @@
+/*
+ * 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_write(struct i2c_adapter *ia, uint8_t addr, uint8_t

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

lib/utils/i2c/i2c.c new file +11 -0
+{
+???????if (!ia)
+???????????????return SBI_EINVAL;
+???????if (!ia->write)
+???????????????return SBI_ENOSYS;
+
+???????return ia->write(ia, addr, reg, buffer, len);
+}
+
+
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t

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

lib/utils/i2c/i2c.c new file +8 -0
+{
+???????if (!ia)
+???????????????return SBI_EINVAL;
+???????if (!ia->read)
+???????????????return SBI_ENOSYS;
+
+???????return ia->read(ia, addr, reg, buffer, len);
+}
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
#8 lore raw

PATCH v4 3/6 lib: utils/i2c: Add generic I2C configuration library

Quoted 15 lines · Nikita Shubin
Quoted 11 lines · On Wed, 10 Nov 2021 21:01:39 +0800 Xiang W <[email protected]> wrote:

? 2021-11-10???? 12:42 +0300?Nikita Shubin???

Quoted 10 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...).

Quoted 8 lines
Quoted 2 lines

The i2c bus does not define register-related operations, these interfaces should be renamed smbus

You are right, it was in first series of patches and i dropped it as there is no non-smbus version currently.

So let's resend. I hope this change is trivial enough so i can keep the tags.

I think what we have is good to go.

Regards,
Anup

Quoted 228 lines
Quoted 212 lines

Regards,
Xiang W

Quoted 208 lines

---
v3->v4:
- fixed typo in include/sbi_utils/i2c/i2c.h
- fixed inaccurate configure remove, leaked into patch 0004

---
 include/sbi_utils/i2c/i2c.h | 85
+++++++++++++++++++++++++++++++++++++
 lib/utils/i2c/i2c.c         | 75 ++++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk    | 10 +++++
 3 files changed, 170 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..79aeeb5
@@ -0,0 +1,85 @@
+/*
+ * 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;
+
+       /**
+        * Send buffer to given address, register
+        *
+        * @return 0 on success and negative error code on failure
+        */
+       int (*write)(struct i2c_adapter *ia, uint8_t addr, uint8_t

reg,

include/sbi_utils/i2c/i2c.h new file +8 -0
+                   uint8_t *buffer, int len);
+
+       /**
+        * Read buffer from given address, register
+        *
+        * @return 0 on success and negative error code on failure
+        */
+       int (*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 +15 -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);
+
+/** Send to device on I2C adapter bus */
+int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr, uint8_t

reg,

include/sbi_utils/i2c/i2c.h new file +4 -0
+                     uint8_t *buffer, int len);
+
+/** Read from device on I2C adapter bus */
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t

reg,

include/sbi_utils/i2c/i2c.h new file +3 -0
+                    uint8_t *buffer, int len);
+
+static inline int i2c_adapter_reg_write(struct i2c_adapter *ia,

uint8_t addr,

include/sbi_utils/i2c/i2c.h new file +6 -0
+                                uint8_t reg, uint8_t val)
+{
+       return i2c_adapter_write(ia, addr, reg, &val, 1);
+}
+
+static inline int i2c_adapter_reg_read(struct i2c_adapter *ia,

uint8_t addr,

include/sbi_utils/i2c/i2c.h new file +13 -0
+                                      uint8_t reg, uint8_t *val)
+{
+       uint8_t buf;
+       int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
+
+       if (ret)
+               return ret;
+
+       *val = buf;
+       return 0;
+}
+
+#endif
lib/utils/i2c/i2c.c new file +54 -0
new file mode 100644
index 0000000..1f5072c
@@ -0,0 +1,75 @@
+/*
+ * 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_write(struct i2c_adapter *ia, uint8_t addr, uint8_t

reg,

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

reg,

lib/utils/i2c/i2c.c new file +9 -0
+                    uint8_t *buffer, int len)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->read)
+               return SBI_ENOSYS;
+
+       return ia->read(ia, addr, reg, buffer, len);
+}
lib/utils/i2c/objects.mk new file +6 -0
new file mode 100644
index 0000000..16a70da
@@ -0,0 +1,10 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2021 YADRO
+#
+# Authors:
Quoted 10 lines
Quoted 8 lines
lib/utils/i2c/objects.mk new file +4 -1
+#   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

#9 lore raw

PATCH v4 3/6 lib: utils/i2c: Add generic I2C configuration library

Quoted 21 lines · On Wed, 10 Nov 2021 22:31:08 +0530 Anup Patel <[email protected]> wrote:
Quoted 19 lines · Nikita Shubin
Quoted 14 lines · On Wed, 10 Nov 2021 21:01:39 +0800 Xiang W <[email protected]> wrote:

? 2021-11-10???? 12:42 +0300?Nikita Shubin???

Quoted 11 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...).

Tested-by: Heinrich Schuchardt <[email protected]> Reviewed-by: Alexandre Ghiti <[email protected]> Tested-by: Alexandre Ghiti <[email protected]> Signed-off-by: Nikita Shubin <[email protected]>

The i2c bus does not define register-related operations, these interfaces should be renamed smbus

Quoted 7 lines
Quoted 5 lines

You are right, it was in first series of patches and i dropped it as there is no non-smbus version currently.

So let's resend. I hope this change is trivial enough so i can keep the tags.

I think what we have is good to go.

Okay, no problem - we can always rename later if raw i2c read/write will come up.

Quoted 235 lines

Regards,
Anup

Quoted 231 lines
Quoted 215 lines

Regards,
Xiang W

Quoted 211 lines

---
v3->v4:
- fixed typo in include/sbi_utils/i2c/i2c.h
- fixed inaccurate configure remove, leaked into patch 0004

---
 include/sbi_utils/i2c/i2c.h | 85
+++++++++++++++++++++++++++++++++++++
 lib/utils/i2c/i2c.c         | 75
++++++++++++++++++++++++++++++++ lib/utils/i2c/objects.mk    |
10 +++++ 3 files changed, 170 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 +21 -0
new file mode 100644
index 0000000..79aeeb5
@@ -0,0 +1,85 @@
+/*
+ * 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

*/

include/sbi_utils/i2c/i2c.h new file +6 -0
+       int id;
+
+       /**
+        * Send buffer to given address, register
+        *
+        * @return 0 on success and negative error code on

failure

include/sbi_utils/i2c/i2c.h new file +2 -0
+        */
+       int (*write)(struct i2c_adapter *ia, uint8_t addr,

uint8_t reg,

include/sbi_utils/i2c/i2c.h new file +6 -0
+                   uint8_t *buffer, int len);
+
+       /**
+        * Read buffer from given address, register
+        *
+        * @return 0 on success and negative error code on

failure

include/sbi_utils/i2c/i2c.h new file +2 -0
+        */
+       int (*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 +15 -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);
+
+/** Send to device on I2C adapter bus */
+int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr,

uint8_t reg,

include/sbi_utils/i2c/i2c.h new file +4 -0
+                     uint8_t *buffer, int len);
+
+/** Read from device on I2C adapter bus */
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr,

uint8_t reg,

include/sbi_utils/i2c/i2c.h new file +3 -0
+                    uint8_t *buffer, int len);
+
+static inline int i2c_adapter_reg_write(struct i2c_adapter *ia,

uint8_t addr,

include/sbi_utils/i2c/i2c.h new file +6 -0
+                                uint8_t reg, uint8_t val)
+{
+       return i2c_adapter_write(ia, addr, reg, &val, 1);
+}
+
+static inline int i2c_adapter_reg_read(struct i2c_adapter *ia,

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

include/sbi_utils/i2c/i2c.h new file +11 -0
+       uint8_t buf;
+       int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
+
+       if (ret)
+               return ret;
+
+       *val = buf;
+       return 0;
+}
+
+#endif
lib/utils/i2c/i2c.c new file +54 -0
new file mode 100644
index 0000000..1f5072c
@@ -0,0 +1,75 @@
+/*
+ * 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_write(struct i2c_adapter *ia, uint8_t addr,

uint8_t reg,

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

uint8_t reg,

lib/utils/i2c/i2c.c new file +9 -0
+                    uint8_t *buffer, int len)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->read)
+               return SBI_ENOSYS;
+
+       return ia->read(ia, addr, reg, buffer, len);
+}
lib/utils/i2c/objects.mk new file +6 -0
new file mode 100644
index 0000000..16a70da
@@ -0,0 +1,10 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2021 YADRO
+#
+# Authors:
Quoted 10 lines
Quoted 8 lines
lib/utils/i2c/objects.mk new file +4 -1
+#   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

#10 lore raw

PATCH v4 3/6 lib: utils/i2c: Add generic I2C configuration library

In reply to Xiang W
Quoted 8 lines · Xiang W

? 2021-11-10???? 12:42 +0300?Nikita Shubin???

Quoted 6 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...).

Quoted 6 lines
Quoted 4 lines

The i2c bus does not define register-related operations, these interfaces should be renamed smbus

I disagree with this suggestion, the existing read/write interface names are generic enough to send/receive an array of bytes. These are also suitable for reading EEPROMs or DDR SPD data, etc. I don't see why we should explicitly name it SMBUS and restrict it's use.

Regards,
Anup

Quoted 226 lines

Regards,
Xiang W

Quoted 213 lines

---
v3->v4:
- fixed typo in include/sbi_utils/i2c/i2c.h
- fixed inaccurate configure remove, leaked into patch 0004

---
 include/sbi_utils/i2c/i2c.h | 85
+++++++++++++++++++++++++++++++++++++
 lib/utils/i2c/i2c.c         | 75 ++++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk    | 10 +++++
 3 files changed, 170 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..79aeeb5
@@ -0,0 +1,85 @@
+/*
+ * 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;
+
+       /**
+        * Send buffer to given address, register
+        *
+        * @return 0 on success and negative error code on failure
+        */
+       int (*write)(struct i2c_adapter *ia, uint8_t addr, uint8_t

reg,

include/sbi_utils/i2c/i2c.h new file +8 -0
+                   uint8_t *buffer, int len);
+
+       /**
+        * Read buffer from given address, register
+        *
+        * @return 0 on success and negative error code on failure
+        */
+       int (*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 +15 -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);
+
+/** Send to device on I2C adapter bus */
+int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr, uint8_t

reg,

include/sbi_utils/i2c/i2c.h new file +4 -0
+                     uint8_t *buffer, int len);
+
+/** Read from device on I2C adapter bus */
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t

reg,

include/sbi_utils/i2c/i2c.h new file +3 -0
+                    uint8_t *buffer, int len);
+
+static inline int i2c_adapter_reg_write(struct i2c_adapter *ia,

uint8_t addr,

include/sbi_utils/i2c/i2c.h new file +6 -0
+                                uint8_t reg, uint8_t val)
+{
+       return i2c_adapter_write(ia, addr, reg, &val, 1);
+}
+
+static inline int i2c_adapter_reg_read(struct i2c_adapter *ia,

uint8_t addr,

include/sbi_utils/i2c/i2c.h new file +13 -0
+                                      uint8_t reg, uint8_t *val)
+{
+       uint8_t buf;
+       int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
+
+       if (ret)
+               return ret;
+
+       *val = buf;
+       return 0;
+}
+
+#endif
lib/utils/i2c/i2c.c new file +54 -0
new file mode 100644
index 0000000..1f5072c
@@ -0,0 +1,75 @@
+/*
+ * 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_write(struct i2c_adapter *ia, uint8_t addr, uint8_t

reg,

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

reg,

lib/utils/i2c/i2c.c new file +9 -0
+                    uint8_t *buffer, int len)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->read)
+               return SBI_ENOSYS;
+
+       return ia->read(ia, addr, reg, buffer, len);
+}
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
--
Quoted 3 lines

2.31.1

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

#11 lore raw

PATCH v4 3/6 lib: utils/i2c: Add generic I2C configuration library

? 2021-11-10???? 22:30 +0530?Anup Patel???

Quoted 14 lines · Xiang W
Quoted 13 lines

? 2021-11-10???? 12:42 +0300?Nikita Shubin???

Quoted 11 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...).

Tested-by: Heinrich Schuchardt
<[email protected]>

Quoted 8 lines
Quoted 2 lines

The i2c bus does not define register-related operations, these interfaces should be renamed smbus

I disagree with this suggestion, the existing read/write interface names are generic enough to send/receive an array of bytes. These are also suitable for reading EEPROMs or DDR SPD data, etc. I don't see why we should explicitly name it SMBUS and restrict it's use.

IIC is a particularly flexible bus. The standard only defines how to address the device, but does not define how to access the registers in the device. How to operate a device is defined by the device's data sheet.

For example: 24LC64 EEPROM random read process
S: Start signal
Addr(7bit): address
R/W (1bit): read and write control signal
A/NA(1bit): Acknowledge signal
Data(8bit): data
P: STOP signal
[]: Indicates that data is transferred from the device to the host

S Addr R [A] Data [A] Data [A] S Addr R [Data] NA P
The first two Data are used to pass the address to be accessed, and the latter Data is used to accept the read data

The current interface cannot operate 24LC64

Regards,
Xiang W

Quoted 240 lines

Regards,
Anup

Quoted 236 lines

Regards,
Xiang W

Quoted 223 lines

---
v3->v4:
- fixed typo in include/sbi_utils/i2c/i2c.h
- fixed inaccurate configure remove, leaked into patch 0004
---
?include/sbi_utils/i2c/i2c.h | 85
+++++++++++++++++++++++++++++++++++++
?lib/utils/i2c/i2c.c???????? | 75
++++++++++++++++++++++++++++++++
?lib/utils/i2c/objects.mk??? | 10 +++++
?3 files changed, 170 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 +21 -0
new file mode 100644
index 0000000..79aeeb5
@@ -0,0 +1,85 @@
+/*
+ * 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;
+
+?????? /**
+??????? * Send buffer to given address, register +??????? *
+??????? * @return 0 on success and negative error code on failure
+??????? */
+?????? int (*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 (*read)(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
+?????????????????? uint8_t *buffer, int len);

include/sbi_utils/i2c/i2c.h new file +6 -0
+
+?????? /** 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 +15 -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);
+
+/** Send to device on I2C adapter bus */
+int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr,

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

include/sbi_utils/i2c/i2c.h new file +3 -0
+
+/** Read from device on I2C adapter bus */
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr,

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

include/sbi_utils/i2c/i2c.h new file +2 -0
+
+static inline int i2c_adapter_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_write(ia, addr, reg, &val, 1);
+}
+
+static inline int i2c_adapter_reg_read(struct i2c_adapter *ia,

uint8_t addr,
+????????????????????????????????????? uint8_t reg, uint8_t *val)

include/sbi_utils/i2c/i2c.h new file +12 -0
+{
+?????? uint8_t buf;
+?????? int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
+
+?????? if (ret)
+?????????????? return ret;
+
+?????? *val = buf;
+?????? return 0;
+}
+
+#endif
lib/utils/i2c/i2c.c new file +54 -0
new file mode 100644
index 0000000..1f5072c
@@ -0,0 +1,75 @@
+/*
+ * 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_write(struct i2c_adapter *ia, uint8_t addr,

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

lib/utils/i2c/i2c.c new file +11 -0
+{
+?????? if (!ia)
+?????????????? return SBI_EINVAL;
+?????? if (!ia->write)
+?????????????? return SBI_ENOSYS;
+
+?????? return ia->write(ia, addr, reg, buffer, len);
+}
+
+
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr,

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

lib/utils/i2c/i2c.c new file +8 -0
+{
+?????? if (!ia)
+?????????????? return SBI_EINVAL;
+?????? if (!ia->read)
+?????????????? return SBI_ENOSYS;
+
+?????? return ia->read(ia, addr, reg, buffer, len);
+}
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
--
Quoted 3 lines

2.31.1

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

#12 lore raw

PATCH v4 3/6 lib: utils/i2c: Add generic I2C configuration library

? 2021-11-11???? 13:12 +0800?Xiang W???

Quoted 38 lines

? 2021-11-10???? 22:30 +0530?Anup Patel???

Quoted 14 lines · Xiang W
Quoted 13 lines

? 2021-11-10???? 12:42 +0300?Nikita Shubin???

Quoted 11 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...).

Tested-by: Heinrich Schuchardt
<[email protected]>

Quoted 8 lines
Quoted 2 lines

The i2c bus does not define register-related operations, these interfaces should be renamed smbus

I disagree with this suggestion, the existing read/write interface names are generic enough to send/receive an array of bytes. These are also suitable for reading EEPROMs or DDR SPD data, etc. I don't see why we should explicitly name it SMBUS and restrict it's use.

IIC is a particularly flexible bus. The standard only defines how to address the device, but does not define how to access the registers in
the device. How to operate a device is defined by the device's data sheet.

For example: 24LC64 EEPROM random read process
S: Start signal
Addr(7bit): address
R/W (1bit): read and write control signal
A/NA(1bit): Acknowledge signal
Data(8bit): data
P: STOP signal
[]: Indicates that data is transferred from the device to the host

Quoted 1 line

S Addr R [A] Data [A] Data [A] S Addr R [Data] NA P

The previous line is wrong, it should look like this

S Addr W [A] Data [A] Data [A] S Addr R [Data] NA P

Quoted 256 lines

The first two Data are used to pass the address to be accessed, and the
latter Data is used to accept the read data

The current interface cannot operate 24LC64

Regards,
Xiang W

Quoted 244 lines

Regards,
Anup

Quoted 240 lines

Regards,
Xiang W

Quoted 227 lines

---
v3->v4:
- fixed typo in include/sbi_utils/i2c/i2c.h
- fixed inaccurate configure remove, leaked into patch 0004
---
?include/sbi_utils/i2c/i2c.h | 85
+++++++++++++++++++++++++++++++++++++
?lib/utils/i2c/i2c.c???????? | 75
++++++++++++++++++++++++++++++++
?lib/utils/i2c/objects.mk??? | 10 +++++
?3 files changed, 170 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 +21 -0
new file mode 100644
index 0000000..79aeeb5
@@ -0,0 +1,85 @@
+/*
+ * 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;
+
+?????? /**
+??????? * Send buffer to given address, register +??????? *
+??????? * @return 0 on success and negative error code on failure
+??????? */
+?????? int (*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 (*read)(struct i2c_adapter *ia, uint8_t addr, uint8_t
reg,
+?????????????????? uint8_t *buffer, int len);

include/sbi_utils/i2c/i2c.h new file +6 -0
+
+?????? /** 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 +15 -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);
+
+/** Send to device on I2C adapter bus */
+int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr,

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

include/sbi_utils/i2c/i2c.h new file +3 -0
+
+/** Read from device on I2C adapter bus */
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr,

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

include/sbi_utils/i2c/i2c.h new file +2 -0
+
+static inline int i2c_adapter_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_write(ia, addr, reg, &val, 1);
+}
+
+static inline int i2c_adapter_reg_read(struct i2c_adapter *ia,

uint8_t addr, +????????????????????????????????????? uint8_t reg, uint8_t *val)

include/sbi_utils/i2c/i2c.h new file +12 -0
+{
+?????? uint8_t buf;
+?????? int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
+
+?????? if (ret)
+?????????????? return ret;
+
+?????? *val = buf;
+?????? return 0;
+}
+
+#endif
lib/utils/i2c/i2c.c new file +54 -0
new file mode 100644
index 0000000..1f5072c
@@ -0,0 +1,75 @@
+/*
+ * 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_write(struct i2c_adapter *ia, uint8_t addr,

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

lib/utils/i2c/i2c.c new file +11 -0
+{
+?????? if (!ia)
+?????????????? return SBI_EINVAL;
+?????? if (!ia->write)
+?????????????? return SBI_ENOSYS;
+
+?????? return ia->write(ia, addr, reg, buffer, len);
+}
+
+
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr,

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

lib/utils/i2c/i2c.c new file +8 -0
+{
+?????? if (!ia)
+?????????????? return SBI_EINVAL;
+?????? if (!ia->read)
+?????????????? return SBI_ENOSYS;
+
+?????? return ia->read(ia, addr, reg, buffer, len);
+}
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
--
Quoted 3 lines

2.31.1

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

#13 lore raw

PATCH v4 3/6 lib: utils/i2c: Add generic I2C configuration library

Quoted 40 lines · Xiang W

? 2021-11-11???? 13:12 +0800?Xiang W???

Quoted 38 lines

? 2021-11-10???? 22:30 +0530?Anup Patel???

Quoted 14 lines · Xiang W
Quoted 13 lines

? 2021-11-10???? 12:42 +0300?Nikita Shubin???

Quoted 11 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...).

Tested-by: Heinrich Schuchardt
<[email protected]>

Quoted 8 lines
Quoted 2 lines

The i2c bus does not define register-related operations, these interfaces should be renamed smbus

I disagree with this suggestion, the existing read/write interface names are generic enough to send/receive an array of bytes. These are also suitable for reading EEPROMs or DDR SPD data, etc. I don't see why we should explicitly name it SMBUS and restrict it's use.

IIC is a particularly flexible bus. The standard only defines how to address the device, but does not define how to access the registers in
the device. How to operate a device is defined by the device's data sheet.

For example: 24LC64 EEPROM random read process
S: Start signal
Addr(7bit): address
R/W (1bit): read and write control signal
A/NA(1bit): Acknowledge signal
Data(8bit): data
P: STOP signal
[]: Indicates that data is transferred from the device to the host

Quoted 4 lines
Quoted 1 line

S Addr R [A] Data [A] Data [A] S Addr R [Data] NA P

The previous line is wrong, it should look like this

S Addr W [A] Data [A] Data [A] S Addr R [Data] NA P

I see your concern about "uint_8 reg" parameter.

I think this can be broken down into two parameters "uint8_t *cmd" and "int cmd_len" but this can be done as separate patch.

Regards,
Anup

Quoted 259 lines
Quoted 257 lines

The first two Data are used to pass the address to be accessed, and the
latter Data is used to accept the read data

The current interface cannot operate 24LC64

Regards,
Xiang W

Quoted 244 lines

Regards,
Anup

Quoted 240 lines

Regards,
Xiang W

Quoted 227 lines

---
v3->v4:
- fixed typo in include/sbi_utils/i2c/i2c.h
- fixed inaccurate configure remove, leaked into patch 0004

---
 include/sbi_utils/i2c/i2c.h | 85
+++++++++++++++++++++++++++++++++++++
 lib/utils/i2c/i2c.c         | 75
++++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk    | 10 +++++
 3 files changed, 170 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 +21 -0
new file mode 100644
index 0000000..79aeeb5
@@ -0,0 +1,85 @@
+/*
+ * 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

*/

include/sbi_utils/i2c/i2c.h new file +6 -0
+       int id;
+
+       /**
+        * Send buffer to given address, register
+        *
+        * @return 0 on success and negative error code on

failure

include/sbi_utils/i2c/i2c.h new file +2 -0
+        */
+       int (*write)(struct i2c_adapter *ia, uint8_t addr,

uint8_t
reg,

include/sbi_utils/i2c/i2c.h new file +6 -0
+                   uint8_t *buffer, int len);
+
+       /**
+        * Read buffer from given address, register
+        *
+        * @return 0 on success and negative error code on

failure

include/sbi_utils/i2c/i2c.h new file +2 -0
+        */
+       int (*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 +15 -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);
+
+/** Send to device on I2C adapter bus */
+int i2c_adapter_write(struct i2c_adapter *ia, uint8_t addr,

uint8_t
reg,

include/sbi_utils/i2c/i2c.h new file +4 -0
+                     uint8_t *buffer, int len);
+
+/** Read from device on I2C adapter bus */
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr,

uint8_t
reg,

include/sbi_utils/i2c/i2c.h new file +3 -0
+                    uint8_t *buffer, int len);
+
+static inline int i2c_adapter_reg_write(struct i2c_adapter

*ia,
uint8_t addr,

include/sbi_utils/i2c/i2c.h new file +6 -0
+                                uint8_t reg, uint8_t val)
+{
+       return i2c_adapter_write(ia, addr, reg, &val, 1);
+}
+
+static inline int i2c_adapter_reg_read(struct i2c_adapter *ia,

uint8_t addr,
+ uint8_t reg, uint8_t
*val)

include/sbi_utils/i2c/i2c.h new file +12 -0
+{
+       uint8_t buf;
+       int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
+
+       if (ret)
+               return ret;
+
+       *val = buf;
+       return 0;
+}
+
+#endif
lib/utils/i2c/i2c.c new file +54 -0
new file mode 100644
index 0000000..1f5072c
@@ -0,0 +1,75 @@
+/*
+ * 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_write(struct i2c_adapter *ia, uint8_t addr,

uint8_t
reg,

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

uint8_t
reg,

lib/utils/i2c/i2c.c new file +9 -0
+                    uint8_t *buffer, int len)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->read)
+               return SBI_ENOSYS;
+
+       return ia->read(ia, addr, reg, buffer, len);
+}
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
--
Quoted 3 lines

2.31.1

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

#14 lore raw

PATCH v4 3/6 lib: utils/i2c: Add generic I2C configuration library

? 2021-11-11???? 11:46 +0530?Anup Patel???

Quoted 53 lines · Xiang W
Quoted 47 lines

? 2021-11-11???? 13:12 +0800?Xiang W???

Quoted 45 lines

? 2021-11-10???? 22:30 +0530?Anup Patel???

Quoted 19 lines · Xiang W
Quoted 14 lines

? 2021-11-10???? 12:42 +0300?Nikita Shubin???

Quoted 6 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...).

Quoted 6 lines

Tested-by: Heinrich Schuchardt
<[email protected]>
Reviewed-by: Alexandre Ghiti
<[email protected]>

Quoted 3 lines

The i2c bus does not define register-related operations, these
interfaces should be renamed smbus

Quoted 6 lines

I disagree with this suggestion, the existing read/write interface
names are generic enough to send/receive an array of bytes. These are also suitable for reading EEPROMs or DDR SPD data, etc. I don't see why we should explicitly name it SMBUS and restrict it's use.

IIC is a particularly flexible bus. The standard only defines how to
address the device, but does not define how to access the registers
in
the device. How to operate a device is defined by the device's data
sheet.

For example: 24LC64 EEPROM random read process
S: Start signal
Addr(7bit): address
R/W (1bit): read and write control signal A/NA(1bit): Acknowledge signal
Data(8bit): data
P: STOP signal
[]: Indicates that data is transferred from the device to the host

Quoted 4 lines
Quoted 1 line

S Addr R [A] Data [A] Data [A] S Addr R [Data] NA P

The previous line is wrong, it should look like this

S Addr W [A] Data [A] Data [A] S Addr R [Data] NA P

Quoted 7 lines

I see your concern about "uint_8 reg" parameter.

I think this can be broken down into two parameters "uint8_t *cmd" and "int cmd_len" but this can be done as separate patch.

Regards,
Anup

This is not enough

i2c defines three transmission modes
1. Read: Read data from the device
2. Write: Write data to the device
3. Combined operation. This is the lack of a STOP signal between two consecutive read and write operations. Both operations can be read or write, and the data length is not fixed.

You can refer to: https://www.pololu.com/file/0J435/UM10204.pdf

I suggest changing the interface to this:

int i2c_adapter_read(struct i2c_adapter *ia,?

	unsigned addr, unsigned addrlen, 
	unsigned count, uint8_t *buff);
int i2c_adapter_write(struct i2c_adapter *ia,
	unsigned addr, unsigned addrlen, 
	unsigned count, uint8_t *buff);

int i2c_adapter_combined_trans(struct i2c_adapter *ia,
unsigned addr, unsigned addrlen, unsigned rw0, unsigned c0, uint8_t *b0, unsigned rw1, unsigned c1, uint8_t *b1);

i2c_adapter_smbus_xxx
...

Regards,
Xiang W

Quoted 268 lines
Quoted 268 lines
Quoted 266 lines

The first two Data are used to pass the address to be accessed, and
the
latter Data is used to accept the read data

The current interface cannot operate 24LC64

Regards,
Xiang W

Quoted 251 lines

Regards,
Anup

Quoted 247 lines

Regards,
Xiang W

Quoted 234 lines

---
v3->v4:
- fixed typo in include/sbi_utils/i2c/i2c.h
- fixed inaccurate configure remove, leaked into patch 0004
---
?include/sbi_utils/i2c/i2c.h | 85
+++++++++++++++++++++++++++++++++++++
?lib/utils/i2c/i2c.c???????? | 75
++++++++++++++++++++++++++++++++
?lib/utils/i2c/objects.mk??? | 10 +++++
?3 files changed, 170 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 +18 -0
new file mode 100644
index 0000000..79aeeb5
@@ -0,0 +1,85 @@
+/*
+ * 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;
+
+?????? /**
+??????? * Send buffer to given address, register +??????? *
+??????? * @return 0 on success and negative error code on failure
+??????? */
+?????? int (*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 (*read)(struct i2c_adapter *ia, uint8_t addr, uint8_t
reg,
+?????????????????? uint8_t *buffer, int len);

include/sbi_utils/i2c/i2c.h new file +6 -0
+
+?????? /** 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);

include/sbi_utils/i2c/i2c.h new file +13 -0
+}
+
+/** 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);
+
+/** Send to device on I2C adapter bus */
+int i2c_adapter_write(struct i2c_adapter *ia, uint8_t

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

include/sbi_utils/i2c/i2c.h new file +3 -0
+
+/** Read from device on I2C adapter bus */
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr,

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

include/sbi_utils/i2c/i2c.h new file +2 -0
+
+static inline int i2c_adapter_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_write(ia, addr, reg, &val, 1);
+}
+
+static inline int i2c_adapter_reg_read(struct i2c_adapter

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

include/sbi_utils/i2c/i2c.h new file +12 -0
+{
+?????? uint8_t buf;
+?????? int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
+
+?????? if (ret)
+?????????????? return ret;
+
+?????? *val = buf;
+?????? return 0;
+}
+
+#endif
lib/utils/i2c/i2c.c new file +24 -0
new file mode 100644
index 0000000..1f5072c
@@ -0,0 +1,75 @@
+/*
+ * 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);

lib/utils/i2c/i2c.c new file +30 -0
+
+?????????????? 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_write(struct i2c_adapter *ia, uint8_t

addr,
uint8_t
reg,
+??????????????????? uint8_t *buffer, int len)

lib/utils/i2c/i2c.c new file +11 -0
+{
+?????? if (!ia)
+?????????????? return SBI_EINVAL;
+?????? if (!ia->write)
+?????????????? return SBI_ENOSYS;
+
+?????? return ia->write(ia, addr, reg, buffer, len);
+}
+
+
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr,

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

lib/utils/i2c/i2c.c new file +8 -0
+{
+?????? if (!ia)
+?????????????? return SBI_EINVAL;
+?????? if (!ia->read)
+?????????????? return SBI_ENOSYS;
+
+?????? return ia->read(ia, addr, reg, buffer, len);
+}
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
--
Quoted 3 lines

2.31.1

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

#15 lore raw

PATCH v4 3/6 lib: utils/i2c: Add generic I2C configuration library

Quoted 87 lines · Xiang W

? 2021-11-11???? 11:46 +0530?Anup Patel???

Quoted 53 lines · Xiang W
Quoted 47 lines

? 2021-11-11???? 13:12 +0800?Xiang W???

Quoted 45 lines

? 2021-11-10???? 22:30 +0530?Anup Patel???

Quoted 19 lines · Xiang W
Quoted 14 lines

? 2021-11-10???? 12:42 +0300?Nikita Shubin???

Quoted 6 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...).

Quoted 6 lines

Tested-by: Heinrich Schuchardt
<[email protected]>
Reviewed-by: Alexandre Ghiti
<[email protected]>

Quoted 3 lines

The i2c bus does not define register-related operations, these
interfaces should be renamed smbus

Quoted 6 lines

I disagree with this suggestion, the existing read/write interface
names are generic enough to send/receive an array of bytes. These are also suitable for reading EEPROMs or DDR SPD data, etc. I don't see why we should explicitly name it SMBUS and restrict it's use.

IIC is a particularly flexible bus. The standard only defines how to
address the device, but does not define how to access the registers
in
the device. How to operate a device is defined by the device's data
sheet.

For example: 24LC64 EEPROM random read process
S: Start signal
Addr(7bit): address
R/W (1bit): read and write control signal A/NA(1bit): Acknowledge signal
Data(8bit): data
P: STOP signal
[]: Indicates that data is transferred from the device to the host

Quoted 4 lines
Quoted 1 line

S Addr R [A] Data [A] Data [A] S Addr R [Data] NA P

The previous line is wrong, it should look like this

S Addr W [A] Data [A] Data [A] S Addr R [Data] NA P

Quoted 7 lines

I see your concern about "uint_8 reg" parameter.

I think this can be broken down into two parameters "uint8_t *cmd" and "int cmd_len" but this can be done as separate patch.

Regards,
Anup

This is not enough

i2c defines three transmission modes
1. Read: Read data from the device
2. Write: Write data to the device
3. Combined operation. This is the lack of a STOP signal between two consecutive read and write operations. Both operations can be read or write, and the data length is not fixed.

You can refer to: https://www.pololu.com/file/0J435/UM10204.pdf

I suggest changing the interface to this:

int i2c_adapter_read(struct i2c_adapter *ia,
unsigned addr, unsigned addrlen, unsigned count, uint8_t *buff);
int i2c_adapter_write(struct i2c_adapter *ia,
unsigned addr, unsigned addrlen, unsigned count, uint8_t *buff);
int i2c_adapter_combined_trans(struct i2c_adapter *ia,
unsigned addr, unsigned addrlen, unsigned rw0, unsigned c0, uint8_t *b0, unsigned rw1, unsigned c1, uint8_t *b1);

Quoted 2 lines

i2c_adapter_smbus_xxx
...

There is always room for improvement in future. I would defer improving the I2C adapter interface after OpenSBI v1.0 release.

Regards,
Anup

Quoted 273 lines

Regards,
Xiang W

Quoted 268 lines
Quoted 268 lines
Quoted 266 lines

The first two Data are used to pass the address to be accessed, and
the
latter Data is used to accept the read data

The current interface cannot operate 24LC64

Regards,
Xiang W

Quoted 251 lines

Regards,
Anup

Quoted 247 lines

Regards,
Xiang W

Quoted 234 lines

---
v3->v4:
- fixed typo in include/sbi_utils/i2c/i2c.h
- fixed inaccurate configure remove, leaked into patch 0004

---
 include/sbi_utils/i2c/i2c.h | 85
+++++++++++++++++++++++++++++++++++++
 lib/utils/i2c/i2c.c         | 75
++++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk    | 10 +++++
 3 files changed, 170 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 +18 -0
new file mode 100644
index 0000000..79aeeb5
@@ -0,0 +1,85 @@
+/*
+ * 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

*/

include/sbi_utils/i2c/i2c.h new file +3 -0
+       void *driver;
+
+       /** Unique ID of the I2C adapter assigned by the

driver
*/

include/sbi_utils/i2c/i2c.h new file +6 -0
+       int id;
+
+       /**
+        * Send buffer to given address, register
+        *
+        * @return 0 on success and negative error code on

failure

include/sbi_utils/i2c/i2c.h new file +2 -0
+        */
+       int (*write)(struct i2c_adapter *ia, uint8_t addr,

uint8_t
reg,

include/sbi_utils/i2c/i2c.h new file +6 -0
+                   uint8_t *buffer, int len);
+
+       /**
+        * Read buffer from given address, register
+        *
+        * @return 0 on success and negative error code on

failure

include/sbi_utils/i2c/i2c.h new file +2 -0
+        */
+       int (*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 +2 -0
+{
+       return container_of(node, struct i2c_adapter,

node);

include/sbi_utils/i2c/i2c.h new file +13 -0
+}
+
+/** 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);
+
+/** Send to device on I2C adapter bus */
+int i2c_adapter_write(struct i2c_adapter *ia, uint8_t

addr,
uint8_t
reg,

include/sbi_utils/i2c/i2c.h new file +4 -0
+                     uint8_t *buffer, int len);
+
+/** Read from device on I2C adapter bus */
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr,

uint8_t
reg,

include/sbi_utils/i2c/i2c.h new file +3 -0
+                    uint8_t *buffer, int len);
+
+static inline int i2c_adapter_reg_write(struct i2c_adapter

*ia,
uint8_t addr,

include/sbi_utils/i2c/i2c.h new file +6 -0
+                                uint8_t reg, uint8_t val)
+{
+       return i2c_adapter_write(ia, addr, reg, &val, 1);
+}
+
+static inline int i2c_adapter_reg_read(struct i2c_adapter

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

include/sbi_utils/i2c/i2c.h new file +12 -0
+{
+       uint8_t buf;
+       int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
+
+       if (ret)
+               return ret;
+
+       *val = buf;
+       return 0;
+}
+
+#endif
lib/utils/i2c/i2c.c new file +24 -0
new file mode 100644
index 0000000..1f5072c
@@ -0,0 +1,75 @@
+/*
+ * 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);

lib/utils/i2c/i2c.c new file +30 -0
+
+               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_write(struct i2c_adapter *ia, uint8_t

addr,
uint8_t
reg,

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

uint8_t
reg,

lib/utils/i2c/i2c.c new file +9 -0
+                    uint8_t *buffer, int len)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->read)
+               return SBI_ENOSYS;
+
+       return ia->read(ia, addr, reg, buffer, len);
+}
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
--
Quoted 3 lines

2.31.1

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

#16 lore raw

PATCH v4 3/6 lib: utils/i2c: Add generic I2C configuration library

In reply to Xiang W
Quoted 82 lines · Xiang W

? 2021-11-11???? 11:46 +0530?Anup Patel???

Quoted 53 lines · Xiang W
Quoted 47 lines

? 2021-11-11???? 13:12 +0800?Xiang W???

Quoted 45 lines

? 2021-11-10???? 22:30 +0530?Anup Patel???

Quoted 19 lines · Xiang W
Quoted 14 lines

? 2021-11-10???? 12:42 +0300?Nikita Shubin???

Quoted 6 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...).

Quoted 6 lines

Tested-by: Heinrich Schuchardt
<[email protected]>
Reviewed-by: Alexandre Ghiti
<[email protected]>

Quoted 3 lines

The i2c bus does not define register-related operations, these
interfaces should be renamed smbus

Quoted 6 lines

I disagree with this suggestion, the existing read/write interface
names are generic enough to send/receive an array of bytes. These are also suitable for reading EEPROMs or DDR SPD data, etc. I don't see why we should explicitly name it SMBUS and restrict it's use.

IIC is a particularly flexible bus. The standard only defines how to
address the device, but does not define how to access the registers
in
the device. How to operate a device is defined by the device's data
sheet.

For example: 24LC64 EEPROM random read process
S: Start signal
Addr(7bit): address
R/W (1bit): read and write control signal A/NA(1bit): Acknowledge signal
Data(8bit): data
P: STOP signal
[]: Indicates that data is transferred from the device to the host

Quoted 4 lines
Quoted 1 line

S Addr R [A] Data [A] Data [A] S Addr R [Data] NA P

The previous line is wrong, it should look like this

S Addr W [A] Data [A] Data [A] S Addr R [Data] NA P

Quoted 7 lines

I see your concern about "uint_8 reg" parameter.

I think this can be broken down into two parameters "uint8_t *cmd" and "int cmd_len" but this can be done as separate patch.

Regards,
Anup

This is not enough

i2c defines three transmission modes
1. Read: Read data from the device
2. Write: Write data to the device
3. Combined operation. This is the lack of a STOP signal between two consecutive read and write operations. Both operations can be read or write, and the data length is not fixed.

You can refer to: https://www.pololu.com/file/0J435/UM10204.pdf

I suggest changing the interface to this:

int i2c_adapter_read(struct i2c_adapter *ia,

	unsigned addr, unsigned addrlen, 
	unsigned count, uint8_t *buff);
int i2c_adapter_write(struct i2c_adapter *ia,
	unsigned addr, unsigned addrlen, 
	unsigned count, uint8_t *buff);
Quoted 6 lines

int i2c_adapter_combined_trans(struct i2c_adapter *ia,
unsigned addr, unsigned addrlen, unsigned rw0, unsigned c0, uint8_t *b0, unsigned rw1, unsigned c1, uint8_t *b1);

i2c_adapter_smbus_xxx

SMBus should be a generic wrapper around an I2C controller interface; once you have the I2C operations there is nothing controller-dependent about it. Or, really I2CSMBus; you can get SMBus controllers that don?t expose raw I2C (well, most kinda do if you configure them right).

Jess

Applied
#17 lore raw

PATCH v4 3/6 lib: utils/i2c: Add generic I2C configuration library

In reply to Nikita Shubin
Quoted 11 lines · 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...).

I am assuming the i2c adapter interface is going to improve over time so with that let's go ahead with this patch as starting point.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 209 lines

---
v3->v4:
- fixed typo in include/sbi_utils/i2c/i2c.h
- fixed inaccurate configure remove, leaked into patch 0004

---
 include/sbi_utils/i2c/i2c.h | 85 +++++++++++++++++++++++++++++++++++++
 lib/utils/i2c/i2c.c         | 75 ++++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk    | 10 +++++
 3 files changed, 170 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 +85 -0
new file mode 100644
index 0000000..79aeeb5
@@ -0,0 +1,85 @@
+/*
+ * 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;
+
+       /**
+        * Send buffer to given address, register
+        *
+        * @return 0 on success and negative error code on failure
+        */
+       int (*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 (*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);
+
+/** Send to device on I2C adapter bus */
+int i2c_adapter_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_read(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
+                    uint8_t *buffer, int len);
+
+static inline int i2c_adapter_reg_write(struct i2c_adapter *ia, uint8_t addr,
+                                uint8_t reg, uint8_t val)
+{
+       return i2c_adapter_write(ia, addr, reg, &val, 1);
+}
+
+static inline int i2c_adapter_reg_read(struct i2c_adapter *ia, uint8_t addr,
+                                      uint8_t reg, uint8_t *val)
+{
+       uint8_t buf;
+       int ret = i2c_adapter_read(ia, addr, reg, &buf, 1);
+
+       if (ret)
+               return ret;
+
+       *val = buf;
+       return 0;
+}
+
+#endif
lib/utils/i2c/i2c.c new file +75 -0
new file mode 100644
index 0000000..1f5072c
@@ -0,0 +1,75 @@
+/*
+ * 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_write(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
+                    uint8_t *buffer, int len)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->write)
+               return SBI_ENOSYS;
+
+       return ia->write(ia, addr, reg, buffer, len);
+}
+
+
+int i2c_adapter_read(struct i2c_adapter *ia, uint8_t addr, uint8_t reg,
+                    uint8_t *buffer, int len)
+{
+       if (!ia)
+               return SBI_EINVAL;
+       if (!ia->read)
+               return SBI_ENOSYS;
+
+       return ia->read(ia, addr, reg, buffer, len);
+}
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

#18 lore raw

PATCH v4 4/6 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.

---
v3->v4:
- fixed inaccurate configure remove, leaked into patch 0004

---
 include/sbi_utils/i2c/fdt_i2c.h | 26 ++++++++++
 lib/utils/i2c/fdt_i2c.c         | 87 +++++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk        |  1 +
 3 files changed, 114 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 +87 -0

87 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
Applied
#19 lore raw

PATCH v4 4/6 lib: utils/i2c: Add simple FDT based I2C framework

Quoted 5 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

FDT based I2C framework on the top of I2C library.

Quoted 7 lines

The drivers are probed on demand by fdt_i2c_adapter_get function.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 153 lines

---
v3->v4:
- fixed inaccurate configure remove, leaked into patch 0004

---
 include/sbi_utils/i2c/fdt_i2c.h | 26 ++++++++++
 lib/utils/i2c/fdt_i2c.c         | 87 +++++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk        |  1 +
 3 files changed, 114 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 +87 -0
new file mode 100644
index 0000000..3182f56
@@ -0,0 +1,87 @@
+/*
+ * 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 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;
+
+       if (!fdt || (nodeoff < 0) || !out_adapter)
+               return SBI_EINVAL;
+
+       rc = fdt_i2c_adapter_find(fdt, nodeoff, &adapter);
+       if (rc)
+               return rc;
+
+       *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

#20 lore raw

PATCH v4 5/6 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.

---
 lib/utils/i2c/fdt_i2c.c        |   3 +
 lib/utils/i2c/fdt_i2c_sifive.c | 262 +++++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk       |   1 +
 3 files changed, 266 insertions(+)
 create mode 100644 lib/utils/i2c/fdt_i2c_sifive.c
lib/utils/i2c/fdt_i2c.c +3 -0
index 3182f56..d4ec6a3 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 int fdt_i2c_adapter_init(void *fdt, int nodeoff)
lib/utils/i2c/fdt_i2c_sifive.c new file +262 -0

262 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
Applied
#21 lore raw

PATCH v4 5/6 lib: utils/i2c: Add minimal SiFive I2C driver

Quoted 5 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

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

Quoted 7 lines

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

Quite a few lines in this patch were crossing 80 characters so I have updated these lines at time of merging this patch.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 307 lines
---
 lib/utils/i2c/fdt_i2c.c        |   3 +
 lib/utils/i2c/fdt_i2c_sifive.c | 262 +++++++++++++++++++++++++++++++++
 lib/utils/i2c/objects.mk       |   1 +
 3 files changed, 266 insertions(+)
 create mode 100644 lib/utils/i2c/fdt_i2c_sifive.c
lib/utils/i2c/fdt_i2c.c +3 -0
index 3182f56..d4ec6a3 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 int fdt_i2c_adapter_init(void *fdt, int nodeoff)
lib/utils/i2c/fdt_i2c_sifive.c new file +262 -0
new file mode 100644
index 0000000..404eda4
@@ -0,0 +1,262 @@
+/*
+ * 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,
+                                    uint8_t reg, uint8_t value)
+{
+       writel(value, (volatile void *)adap->addr + reg);
+}
+
+static inline uint8_t sifive_i2c_getreg(struct sifive_i2c_adapter *adap,
+                                       uint8_t reg)
+{
+       return readl((volatile void *)adap->addr + reg);
+}
+
+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)
+{
+       unsigned int timeout = 1; // [msec]
+       int count = 0;
+       uint8_t val;
+
+       sbi_timer_udelay(80); // worst case if bus speed is 100 kHz
+
+       do {
+               val = sifive_i2c_getreg(adap, SIFIVE_I2C_SR);
+               if (!(val & mask))
+                       return 0;
+
+               sbi_timer_udelay(1);
+               count += 1;
+               if (count == (timeout * 1000))
+                       return SBI_ETIMEDOUT;
+       } while (1);
+}
+
+#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_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_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_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.write = sifive_i2c_adapter_write;
+       adapter->adapter.read = sifive_i2c_adapter_read;
+       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

#22 lore raw

PATCH v4 1/1 platform: sifive_fu740: add platform 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.

---
v3->v4:
- moved all code to generic/sifive_fu740.c

---
 platform/generic/sifive_fu740.c | 192 ++++++++++++++++++++++++++++++++
 1 file changed, 192 insertions(+)
platform/generic/sifive_fu740.c +192 -0
index c8325d3..dde5926 100644
@@ -2,14 +2,190 @@
  * SPDX-License-Identifier: BSD-2-Clause
  *
  * Copyright (c) 2021 SiFive
+ * Copyright (c) 2021 YADRO
  *
  * Authors:
  *   David Abdurachmanov <[email protected]>
+ *   Nikita Shubin <[email protected]>
  */
 
 #include <platform_override.h>
+#include <libfdt.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_system.h>
+#include <sbi/sbi_console.h>
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/fdt/fdt_fixup.h>
+#include <sbi_utils/reset/fdt_reset.h>
+#include <sbi_utils/i2c/fdt_i2c.h>
+
+#define DA9063_REG_PAGE_CON		0x00
+#define DA9063_REG_CONTROL_A		0x0e
+#define DA9063_REG_CONTROL_F		0x13
+#define DA9063_REG_DEVICE_ID		0x81
+
+#define DA9063_CONTROL_A_M_POWER1_EN	(1 << 6)
+#define DA9063_CONTROL_A_M_POWER_EN	(1 << 5)
+#define DA9063_CONTROL_A_STANDBY	(1 << 3)
+
+#define DA9063_CONTROL_F_WAKEUP	(1 << 2)
+#define DA9063_CONTROL_F_SHUTDOWN	(1 << 1)
+
+#define PMIC_CHIP_ID_DA9063		0x61
+
+static struct {
+	struct i2c_adapter *adapter;
+	uint32_t reg;
+} da9063;
+
+static int da9063_system_reset_check(u32 type, u32 reason)
+{
+	switch (type) {
+	case SBI_SRST_RESET_TYPE_SHUTDOWN:
+		return 1;
+	case SBI_SRST_RESET_TYPE_COLD_REBOOT:
+	case SBI_SRST_RESET_TYPE_WARM_REBOOT:
+		return 255;
+	}
+
+	return 0;
+}
+
+static inline int da9063_sanity_check(struct i2c_adapter *adap, uint32_t reg)
+{
+	uint8_t val;
+	int rc = i2c_adapter_reg_write(adap, reg, DA9063_REG_PAGE_CON, 0x02);
+
+	if (rc)
+		return rc;
+
+	/* check set page*/
+	rc = i2c_adapter_reg_read(adap, reg, 0x0, &val);
+	if (rc)
+		return rc;
+
+	if (val != 0x02)
+		return SBI_ENODEV;
+
+	/* read and check device id */
+	rc = i2c_adapter_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_reg_write(adap, da9063.reg, DA9063_REG_PAGE_CON, 0x00);
+
+	if (rc)
+		return rc;
+
+	return i2c_adapter_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_reg_write(adap, da9063.reg, DA9063_REG_PAGE_CON, 0x00);
+
+	if (rc)
+		return rc;
+
+	rc = i2c_adapter_reg_write(adap, da9063.reg,
+				   DA9063_REG_CONTROL_F, DA9063_CONTROL_F_WAKEUP);
+	if (rc)
+		return rc;
+
+	return i2c_adapter_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) {
+		/* 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_bus;
+	struct i2c_adapter *adapter;
+	uint64_t addr;
+
+	/* we are dlg,da9063 node */
+	rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
+	if (rc)
+		return rc;
+
+	da9063.reg = addr;
+
+	/* find i2c bus parent node */
+	i2c_bus = fdt_parent_offset(fdt, nodeoff);
+	if (i2c_bus < 0)
+		return i2c_bus;
+
+	/* i2c adapter get */
+	rc = fdt_i2c_adapter_get(fdt, i2c_bus, &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 = "dlg,da9063", .data = (void *)TRUE },
+	{ },
+};
+
+struct fdt_reset fdt_reset_da9063 = {
+	.match_table = da9063_reset_match,
+	.init = da9063_reset_init,
+};
 
 static u64 sifive_fu740_tlbr_flush_limit(const struct fdt_match *match)
 {
@@ -23,6 +199,21 @@ static u64 sifive_fu740_tlbr_flush_limit(const struct fdt_match *match)
 	return 0;
 }
 
+static int sifive_fu740_final_init(bool cold_boot, const struct fdt_match *)
+{
+	int rc;
+	void *fdt = fdt_get_address();
+
+	if (cold_boot) {
+		rc = fdt_reset_driver_init(fdt, &fdt_reset_da9063);
+		if (rc)
+			sbi_printf("%s: failed to find da9063 for reset\n",
+				   __func__);
+	}
+
+	return 0;
+}
+
 static const struct fdt_match sifive_fu740_match[] = {
 	{ .compatible = "sifive,fu740" },
 	{ .compatible = "sifive,fu740-c000" },
@@ -33,4 +224,5 @@ static const struct fdt_match sifive_fu740_match[] = {
 const struct platform_override sifive_fu740 = {
 	.match_table = sifive_fu740_match,
 	.tlbr_flush_limit = sifive_fu740_tlbr_flush_limit,
+	.final_init = sifive_fu740_final_init,
 };
Signature · 1 line
2.31.1
#23 lore raw

PATCH v4 1/1 platform: sifive_fu740: add platform reset driver

Quoted 1 line · Nikita Shubin

From: Nikita Shubin <[email protected]>

Obviously this is patch 6/6 of the v4 series.

No need to resend.

Best regards

Heinrich

#24 lore raw

PATCH v4 1/1 platform: sifive_fu740: add platform reset driver

In reply to Nikita Shubin
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 3 lines

I would not copy Tested-by: and Reviewed-by: if a patch has changed significantly (e.g. putting the code into other files).

For patches 1-5 the only differences are fixed comment typos. There it is ok for me to copy the Reviewed-by:.

I have tested the new series and found no issues.

Quoted 232 lines

---
v3->v4:
- moved all code to generic/sifive_fu740.c

---
  platform/generic/sifive_fu740.c | 192 ++++++++++++++++++++++++++++++++
  1 file changed, 192 insertions(+)
platform/generic/sifive_fu740.c +192 -0
index c8325d3..dde5926 100644
@@ -2,14 +2,190 @@
   * SPDX-License-Identifier: BSD-2-Clause
   *
   * Copyright (c) 2021 SiFive
+ * Copyright (c) 2021 YADRO
   *
   * Authors:
   *   David Abdurachmanov <[email protected]>
+ *   Nikita Shubin <[email protected]>
   */
 
  #include <platform_override.h>
+#include <libfdt.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_system.h>
+#include <sbi/sbi_console.h>
  #include <sbi_utils/fdt/fdt_helper.h>
  #include <sbi_utils/fdt/fdt_fixup.h>
+#include <sbi_utils/reset/fdt_reset.h>
+#include <sbi_utils/i2c/fdt_i2c.h>
+
+#define DA9063_REG_PAGE_CON		0x00
+#define DA9063_REG_CONTROL_A		0x0e
+#define DA9063_REG_CONTROL_F		0x13
+#define DA9063_REG_DEVICE_ID		0x81
+
+#define DA9063_CONTROL_A_M_POWER1_EN	(1 << 6)
+#define DA9063_CONTROL_A_M_POWER_EN	(1 << 5)
+#define DA9063_CONTROL_A_STANDBY	(1 << 3)
+
+#define DA9063_CONTROL_F_WAKEUP	(1 << 2)
+#define DA9063_CONTROL_F_SHUTDOWN	(1 << 1)
+
+#define PMIC_CHIP_ID_DA9063		0x61
+
+static struct {
+	struct i2c_adapter *adapter;
+	uint32_t reg;
+} da9063;
+
+static int da9063_system_reset_check(u32 type, u32 reason)
+{
+	switch (type) {
+	case SBI_SRST_RESET_TYPE_SHUTDOWN:
+		return 1;
+	case SBI_SRST_RESET_TYPE_COLD_REBOOT:
+	case SBI_SRST_RESET_TYPE_WARM_REBOOT:
+		return 255;
+	}
+
+	return 0;
+}
+
+static inline int da9063_sanity_check(struct i2c_adapter *adap, uint32_t reg)
+{
+	uint8_t val;
+	int rc = i2c_adapter_reg_write(adap, reg, DA9063_REG_PAGE_CON, 0x02);
+
+	if (rc)
+		return rc;
+
+	/* check set page*/
+	rc = i2c_adapter_reg_read(adap, reg, 0x0, &val);
+	if (rc)
+		return rc;
+
+	if (val != 0x02)
+		return SBI_ENODEV;
+
+	/* read and check device id */
+	rc = i2c_adapter_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_reg_write(adap, da9063.reg, DA9063_REG_PAGE_CON, 0x00);
+
+	if (rc)
+		return rc;
+
+	return i2c_adapter_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_reg_write(adap, da9063.reg, DA9063_REG_PAGE_CON, 0x00);
+
+	if (rc)
+		return rc;
+
+	rc = i2c_adapter_reg_write(adap, da9063.reg,
+				   DA9063_REG_CONTROL_F, DA9063_CONTROL_F_WAKEUP);
+	if (rc)
+		return rc;
+
+	return i2c_adapter_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) {
+		/* 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_bus;
+	struct i2c_adapter *adapter;
+	uint64_t addr;
+
+	/* we are dlg,da9063 node */
+	rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
+	if (rc)
+		return rc;
+
+	da9063.reg = addr;
+
+	/* find i2c bus parent node */
+	i2c_bus = fdt_parent_offset(fdt, nodeoff);
+	if (i2c_bus < 0)
+		return i2c_bus;
+
+	/* i2c adapter get */
+	rc = fdt_i2c_adapter_get(fdt, i2c_bus, &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 = "dlg,da9063", .data = (void *)TRUE },
+	{ },
+};
+
+struct fdt_reset fdt_reset_da9063 = {
+	.match_table = da9063_reset_match,
+	.init = da9063_reset_init,
+};
 
  static u64 sifive_fu740_tlbr_flush_limit(const struct fdt_match *match)
  {
@@ -23,6 +199,21 @@ static u64 sifive_fu740_tlbr_flush_limit(const struct fdt_match *match)
  	return 0;
  }
 
+static int sifive_fu740_final_init(bool cold_boot, const struct fdt_match *)
+{
+	int rc;
+	void *fdt = fdt_get_address();
+
+	if (cold_boot) {
+		rc = fdt_reset_driver_init(fdt, &fdt_reset_da9063);
+		if (rc)
+			sbi_printf("%s: failed to find da9063 for reset\n",
+				   __func__);
+	}
+
+	return 0;
+}
+
  static const struct fdt_match sifive_fu740_match[] = {
  	{ .compatible = "sifive,fu740" },
  	{ .compatible = "sifive,fu740-c000" },
@@ -33,4 +224,5 @@ static const struct fdt_match sifive_fu740_match[] = {
  const struct platform_override sifive_fu740 = {
  	.match_table = sifive_fu740_match,
  	.tlbr_flush_limit = sifive_fu740_tlbr_flush_limit,
+	.final_init = sifive_fu740_final_init,
  };
Applied
#25 lore raw

PATCH v4 1/1 platform: sifive_fu740: add platform reset driver

In reply to Nikita Shubin
Quoted 156 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.

---
v3->v4:
- moved all code to generic/sifive_fu740.c

---
 platform/generic/sifive_fu740.c | 192 ++++++++++++++++++++++++++++++++
 1 file changed, 192 insertions(+)
platform/generic/sifive_fu740.c +114 -0
index c8325d3..dde5926 100644
@@ -2,14 +2,190 @@
  * SPDX-License-Identifier: BSD-2-Clause
  *
  * Copyright (c) 2021 SiFive
+ * Copyright (c) 2021 YADRO
  *
  * Authors:
  *   David Abdurachmanov <[email protected]>
+ *   Nikita Shubin <[email protected]>
  */
 
 #include <platform_override.h>
+#include <libfdt.h>
+#include <sbi/sbi_error.h>
+#include <sbi/sbi_system.h>
+#include <sbi/sbi_console.h>
 #include <sbi_utils/fdt/fdt_helper.h>
 #include <sbi_utils/fdt/fdt_fixup.h>
+#include <sbi_utils/reset/fdt_reset.h>
+#include <sbi_utils/i2c/fdt_i2c.h>
+
+#define DA9063_REG_PAGE_CON            0x00
+#define DA9063_REG_CONTROL_A           0x0e
+#define DA9063_REG_CONTROL_F           0x13
+#define DA9063_REG_DEVICE_ID           0x81
+
+#define DA9063_CONTROL_A_M_POWER1_EN   (1 << 6)
+#define DA9063_CONTROL_A_M_POWER_EN    (1 << 5)
+#define DA9063_CONTROL_A_STANDBY       (1 << 3)
+
+#define DA9063_CONTROL_F_WAKEUP        (1 << 2)
+#define DA9063_CONTROL_F_SHUTDOWN      (1 << 1)
+
+#define PMIC_CHIP_ID_DA9063            0x61
+
+static struct {
+       struct i2c_adapter *adapter;
+       uint32_t reg;
+} da9063;
+
+static int da9063_system_reset_check(u32 type, u32 reason)
+{
+       switch (type) {
+       case SBI_SRST_RESET_TYPE_SHUTDOWN:
+               return 1;
+       case SBI_SRST_RESET_TYPE_COLD_REBOOT:
+       case SBI_SRST_RESET_TYPE_WARM_REBOOT:
+               return 255;
+       }
+
+       return 0;
+}
+
+static inline int da9063_sanity_check(struct i2c_adapter *adap, uint32_t reg)
+{
+       uint8_t val;
+       int rc = i2c_adapter_reg_write(adap, reg, DA9063_REG_PAGE_CON, 0x02);
+
+       if (rc)
+               return rc;
+
+       /* check set page*/
+       rc = i2c_adapter_reg_read(adap, reg, 0x0, &val);
+       if (rc)
+               return rc;
+
+       if (val != 0x02)
+               return SBI_ENODEV;
+
+       /* read and check device id */
+       rc = i2c_adapter_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_reg_write(adap, da9063.reg, DA9063_REG_PAGE_CON, 0x00);
+
+       if (rc)
+               return rc;
+
+       return i2c_adapter_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_reg_write(adap, da9063.reg, DA9063_REG_PAGE_CON, 0x00);
+
+       if (rc)
+               return rc;
+
+       rc = i2c_adapter_reg_write(adap, da9063.reg,
+                                  DA9063_REG_CONTROL_F, DA9063_CONTROL_F_WAKEUP);
+       if (rc)
+               return rc;
+
+       return i2c_adapter_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) {
+               /* 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:

platform/generic/sifive_fu740.c line 136

Quoted 10 lines
platform/generic/sifive_fu740.c +10 -0
+                       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);

This "while (1)" is not need due to sbi_hart_hang() below.

Quoted 56 lines
platform/generic/sifive_fu740.c +52 -0
+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_bus;
+       struct i2c_adapter *adapter;
+       uint64_t addr;
+
+       /* we are dlg,da9063 node */
+       rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
+       if (rc)
+               return rc;
+
+       da9063.reg = addr;
+
+       /* find i2c bus parent node */
+       i2c_bus = fdt_parent_offset(fdt, nodeoff);
+       if (i2c_bus < 0)
+               return i2c_bus;
+
+       /* i2c adapter get */
+       rc = fdt_i2c_adapter_get(fdt, i2c_bus, &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 = "dlg,da9063", .data = (void *)TRUE },
+       { },
+};
+
+struct fdt_reset fdt_reset_da9063 = {
+       .match_table = da9063_reset_match,
+       .init = da9063_reset_init,
+};
 
 static u64 sifive_fu740_tlbr_flush_limit(const struct fdt_match *match)
 {

platform/generic/sifive_fu740.c static u64 sifive_fu740_tlbr_flush_limit(const struct fdt_match *match) · line 202

Quoted 4 lines
platform/generic/sifive_fu740.c +1 -0
@@ -23,6 +199,21 @@ static u64 sifive_fu740_tlbr_flush_limit(const struct fdt_match *match)
        return 0;
 }
 
+static int sifive_fu740_final_init(bool cold_boot, const struct fdt_match *)

Missing parameter name here.

Quoted 27 lines
platform/generic/sifive_fu740.c +15 -1
+{
+       int rc;
+       void *fdt = fdt_get_address();
+
+       if (cold_boot) {
+               rc = fdt_reset_driver_init(fdt, &fdt_reset_da9063);
+               if (rc)
+                       sbi_printf("%s: failed to find da9063 for reset\n",
+                                  __func__);
+       }
+
+       return 0;
+}
+
 static const struct fdt_match sifive_fu740_match[] = {
        { .compatible = "sifive,fu740" },
        { .compatible = "sifive,fu740-c000" },
@@ -33,4 +224,5 @@ static const struct fdt_match sifive_fu740_match[] = {
 const struct platform_override sifive_fu740 = {
        .match_table = sifive_fu740_match,
        .tlbr_flush_limit = sifive_fu740_tlbr_flush_limit,
+       .final_init = sifive_fu740_final_init,
 };
--

2.31.1

Quoted 4 lines

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

I have taken care of above minor comments at time of merging this patch.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup