summaryrefslogtreecommitdiff
path: root/common/i2c_controller.c
diff options
context:
space:
mode:
authorDawid Niedzwiecki <dn@semihalf.com>2021-10-08 09:57:51 +0200
committerCommit Bot <commit-bot@chromium.org>2021-10-11 10:18:31 +0000
commit5765523800223bbb60da0e28c179ac59bd344af7 (patch)
treebd44925a58ba906feea06534515ea33151e33ba3 /common/i2c_controller.c
parent6292ad3dc1a0fbd04e67e2376bebd84010fd57e0 (diff)
downloadchrome-ec-5765523800223bbb60da0e28c179ac59bd344af7.tar.gz
zephyr: optimize I2C mutexes
I2C_PORT_COUNT is bigger than the actual physical I2C number in Zephyr. Since the access to I2C has to be controlled per physical port, limit the size of the port_mutex array. Introduce the I2C_DEVICE_COUNT define to represent the number of the physical I2C ports. It is not possible to automate it fully with the current devicetree macros. A new I2C_COPMAT defines will have to be introduced for new chips. The general assumption is that there is only one type of I2C port per chip pointed by the compatible property e.g. "nuvoton,npcx-i2c-port". BUG=b:199918263 BRANCH=none TEST=zmake testall; Make sure there is no "Access I2C without lock!" message in console. Signed-off-by: Dawid Niedzwiecki <dn@semihalf.com> Change-Id: Ie7b0570eb5629ebd4adabc8a47d0d802d847a773 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3211976 Tested-by: Dawid Niedźwiecki <dn@semihalf.com> Reviewed-by: Keith Short <keithshort@chromium.org> Commit-Queue: Dawid Niedzwiecki <dawidn@google.com>
Diffstat (limited to 'common/i2c_controller.c')
-rw-r--r--common/i2c_controller.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/common/i2c_controller.c b/common/i2c_controller.c
index 8ce4e2bad1..62beb38e5a 100644
--- a/common/i2c_controller.c
+++ b/common/i2c_controller.c
@@ -48,7 +48,15 @@
#define I2C_BITBANG_PORT_COUNT 0
#endif
+#ifdef CONFIG_ZEPHYR
+/* I2C_PORT_COUNT is bigger than the real count of used I2C devices, so
+ * use a special define for that to save RAM.
+ */
+static mutex_t port_mutex[I2C_DEVICE_COUNT + I2C_BITBANG_PORT_COUNT];
+#else
static mutex_t port_mutex[I2C_CONTROLLER_COUNT + I2C_BITBANG_PORT_COUNT];
+#endif /* CONFIG_ZEPHYR */
+
/* A bitmap of the controllers which are currently servicing a request. */
static volatile uint32_t i2c_port_active_list;
BUILD_ASSERT(ARRAY_SIZE(port_mutex) < 32);
@@ -322,9 +330,6 @@ void i2c_lock(int port, int lock)
/* Lock the controller, not the port */
port = i2c_port_to_controller(port);
#endif
- if (port < 0 || port >= ARRAY_SIZE(port_mutex))
- return;
-
if (IS_ENABLED(CONFIG_ZEPHYR)) {
/*
* For Zephyr: to convert an i2c port enum value to a port
@@ -335,6 +340,9 @@ void i2c_lock(int port, int lock)
port = i2c_get_physical_port(port);
}
+ if (port < 0 || port >= ARRAY_SIZE(port_mutex))
+ return;
+
if (lock) {
uint32_t irq_lock_key;