From 5765523800223bbb60da0e28c179ac59bd344af7 Mon Sep 17 00:00:00 2001 From: Dawid Niedzwiecki Date: Fri, 8 Oct 2021 09:57:51 +0200 Subject: zephyr: optimize I2C mutexes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Change-Id: Ie7b0570eb5629ebd4adabc8a47d0d802d847a773 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3211976 Tested-by: Dawid Niedźwiecki Reviewed-by: Keith Short Commit-Queue: Dawid Niedzwiecki --- common/i2c_controller.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'common/i2c_controller.c') 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; -- cgit v1.2.1