Repository: zephyrproject-rtos/zephyr
Pull request #87253: drivers: ambiq: Correct peripheral instance calculation
Author: login: AlessandroLuo
Labels: name: area: SPI
description: SPI bus
name: area: I2C
name: platform: Ambiq
description: Ambiq
name: area: MSPI
Created: 2025-03-18T07:08:52Z | Merged: 2025-03-25T04:59:43Z

This commit corrected ambiq peripheral instance calculations


Diff:
diff --git a/drivers/i2c/i2c_ambiq.c b/drivers/i2c/i2c_ambiq.c
index ae4fd1dbcfd67..424dcc97edec3 100644
--- a/drivers/i2c/i2c_ambiq.c
+++ b/drivers/i2c/i2c_ambiq.c
@@ -54,6 +54,7 @@ struct i2c_ambiq_config {
 #endif /* CONFIG_I2C_AMBIQ_BUS_RECOVERY */
 	uint32_t base;
 	int size;
+	int inst_idx;
 	uint32_t bitrate;
 	const struct pinctrl_dev_config *pcfg;
 	void (*irq_config_func)(void);
@@ -68,7 +69,6 @@ struct i2c_ambiq_data {
 	struct k_sem transfer_sem;
 	i2c_ambiq_callback_t callback;
 	void *callback_data;
-	int inst_idx;
 	uint32_t transfer_status;
 	bool pm_policy_state_on;
 };
@@ -247,6 +247,7 @@ static int i2c_ambiq_write(const struct device *dev, struct i2c_msg *msg, uint16
 static int i2c_ambiq_configure(const struct device *dev, uint32_t dev_config)
 {
 	struct i2c_ambiq_data *data = dev->data;
+	const struct i2c_ambiq_config *cfg = dev->config;
 
 	if (!(I2C_MODE_CONTROLLER & dev_config)) {
 		return -EINVAL;
@@ -267,7 +268,7 @@ static int i2c_ambiq_configure(const struct device *dev, uint32_t dev_config)
 	}
 
 #ifdef CONFIG_I2C_AMBIQ_DMA
-	data->iom_cfg.pNBTxnBuf = i2c_dma_tcb_buf[data->inst_idx].buf;
+	data->iom_cfg.pNBTxnBuf = i2c_dma_tcb_buf[cfg->inst_idx].buf;
 	data->iom_cfg.ui32NBTxnBufLength = CONFIG_I2C_DMA_TCB_BUFFER_SIZE;
 #endif
 
@@ -410,8 +411,7 @@ static int i2c_ambiq_init(const struct device *dev)
 
 	data->iom_cfg.eInterfaceMode = AM_HAL_IOM_I2C_MODE;
 
-	if (AM_HAL_STATUS_SUCCESS !=
-	    am_hal_iom_initialize((config->base - IOM0_BASE) / config->size, &data->iom_handler)) {
+	if (AM_HAL_STATUS_SUCCESS != am_hal_iom_initialize(config->inst_idx, &data->iom_handler)) {
 		LOG_ERR("Fail to initialize I2C\n");
 		return -ENXIO;
 	}
@@ -496,18 +496,17 @@ static int i2c_ambiq_pm_action(const struct device *dev, enum pm_device_action a
 	};                                                                                         \
 	static struct i2c_ambiq_data i2c_ambiq_data##n = {                                         \
 		.bus_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.bus_sem, 1, 1),                     \
-		.transfer_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.transfer_sem, 0, 1),           \
-		.inst_idx = n,                                                                     \
-	};                                                                                         \
+		.transfer_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.transfer_sem, 0, 1)};          \
 	static const struct i2c_ambiq_config i2c_ambiq_config##n = {                               \
 		.base = DT_INST_REG_ADDR(n),                                                       \
 		.size = DT_INST_REG_SIZE(n),                                                       \
+		.inst_idx = (DT_INST_REG_ADDR(n) - IOM0_BASE) / (IOM1_BASE - IOM0_BASE),           \
 		.bitrate = DT_INST_PROP(n, clock_frequency),                                       \
 		.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n),                                         \
 		.irq_config_func = i2c_irq_config_func_##n,                                        \
-		IF_ENABLED(CONFIG_I2C_AMBIQ_BUS_RECOVERY,			\
-		(.scl = GPIO_DT_SPEC_INST_GET_OR(n, scl_gpios, {0}),\
-		 .sda = GPIO_DT_SPEC_INST_GET_OR(n, sda_gpios, {0}),)) };       \
+		IF_ENABLED(CONFIG_I2C_AMBIQ_BUS_RECOVERY,                                          \
+		(.scl = GPIO_DT_SPEC_INST_GET_OR(n, scl_gpios, {0}),                               \
+		 .sda = GPIO_DT_SPEC_INST_GET_OR(n, sda_gpios, {0}),)) };                          \
 	PM_DEVICE_DT_INST_DEFINE(n, i2c_ambiq_pm_action);                                          \
 	I2C_DEVICE_DT_INST_DEFINE(n, i2c_ambiq_init, PM_DEVICE_DT_INST_GET(n), &i2c_ambiq_data##n, \
 				  &i2c_ambiq_config##n, POST_KERNEL, CONFIG_I2C_INIT_PRIORITY,     \
diff --git a/drivers/mspi/mspi_ambiq_ap3.c b/drivers/mspi/mspi_ambiq_ap3.c
index 12bdae882ae65..0e2148c1e594b 100644
--- a/drivers/mspi/mspi_ambiq_ap3.c
+++ b/drivers/mspi/mspi_ambiq_ap3.c
@@ -26,6 +26,14 @@ LOG_MODULE_REGISTER(mspi_ambiq_ap3);
 #define PWRCTRL_MAX_WAIT_US  5
 #define MSPI_BUSY            BIT(2)
 
+#if defined(CONFIG_SOC_APOLLO3_BLUE)
+#define MSPI_BASE_ADDR       MSPI_BASE
+#define MSPI_ADDR_INTERVAL   1
+#else
+#define MSPI_BASE_ADDR       MSPI0_BASE
+#define MSPI_ADDR_INTERVAL   (MSPI1_BASE - MSPI0_BASE)
+#endif
+
 typedef int (*mspi_ambiq_pwr_func_t)(void);
 typedef void (*irq_config_func_t)(void);
 
@@ -1356,8 +1364,8 @@ static DEVICE_API(mspi, mspi_ambiq_driver_api) = {
 
 #define MSPI_CONFIG(n)                                                                           \
 	{                                                                                        \
-		.channel_num           = (DT_INST_REG_ADDR(n) - MSPI0_BASE) /                    \
-					 (DT_INST_REG_SIZE(n) * 4),                              \
+		.channel_num           = (DT_INST_REG_ADDR(n) - MSPI_BASE_ADDR) /                \
+									 MSPI_ADDR_INTERVAL,     \
 		.op_mode               = MSPI_OP_MODE_CONTROLLER,                                \
 		.duplex                = MSPI_HALF_DUPLEX,                                       \
 		.max_freq              = MSPI_MAX_FREQ,                                          \
diff --git a/drivers/spi/spi_ambiq_bleif.c b/drivers/spi/spi_ambiq_bleif.c
index 92b94a1a5b7fc..89bdb4d8299a5 100644
--- a/drivers/spi/spi_ambiq_bleif.c
+++ b/drivers/spi/spi_ambiq_bleif.c
@@ -175,7 +175,7 @@ static int spi_ambiq_init(const struct device *dev)
 	}
 #endif /* CONFIG_SPI_AMBIQ_BLEIF_TIMING_TRACE */
 
-	ret = am_hal_ble_initialize((cfg->base - BLEIF_BASE) / cfg->size, &data->BLEhandle);
+	ret = am_hal_ble_initialize(0, &data->BLEhandle);
 	if (ret) {
 		return ret;
 	}
diff --git a/drivers/spi/spi_ambiq_spic.c b/drivers/spi/spi_ambiq_spic.c
index 2a1dae106afdf..bf1d7738c0bdc 100644
--- a/drivers/spi/spi_ambiq_spic.c
+++ b/drivers/spi/spi_ambiq_spic.c
@@ -44,6 +44,7 @@ LOG_MODULE_REGISTER(spi_ambiq);
 struct spi_ambiq_config {
 	uint32_t base;
 	int size;
+	int inst_idx;
 	uint32_t clock_freq;
 	const struct pinctrl_dev_config *pcfg;
 	void (*irq_config_func)(void);
@@ -53,7 +54,6 @@ struct spi_ambiq_data {
 	struct spi_context ctx;
 	am_hal_iom_config_t iom_cfg;
 	void *iom_handler;
-	int inst_idx;
 	bool cont;
 	bool pm_policy_state_on;
 };
@@ -244,7 +244,7 @@ static int spi_config(const struct device *dev, const struct spi_config *config)
 	ctx->config = config;
 
 #ifdef CONFIG_SPI_AMBIQ_DMA
-	data->iom_cfg.pNBTxnBuf = spi_dma_tcb_buf[data->inst_idx].buf;
+	data->iom_cfg.pNBTxnBuf = spi_dma_tcb_buf[cfg->inst_idx].buf;
 	data->iom_cfg.ui32NBTxnBufLength = CONFIG_SPI_DMA_TCB_BUFFER_SIZE;
 #endif
 
@@ -506,8 +506,7 @@ static int spi_ambiq_init(const struct device *dev)
 	const struct spi_ambiq_config *cfg = dev->config;
 	int ret = 0;
 
-	if (AM_HAL_STATUS_SUCCESS !=
-	    am_hal_iom_initialize((cfg->base - IOM0_BASE) / cfg->size, &data->iom_handler)) {
+	if (AM_HAL_STATUS_SUCCESS != am_hal_iom_initialize(cfg->inst_idx, &data->iom_handler)) {
 		LOG_ERR("Fail to initialize SPI\n");
 		return -ENXIO;
 	}
@@ -575,10 +574,11 @@ static int spi_ambiq_pm_action(const struct device *dev, enum pm_device_action a
 	static struct spi_ambiq_data spi_ambiq_data##n = {                                         \
 		SPI_CONTEXT_INIT_LOCK(spi_ambiq_data##n, ctx),                                     \
 		SPI_CONTEXT_INIT_SYNC(spi_ambiq_data##n, ctx),                                     \
-		SPI_CONTEXT_CS_GPIOS_INITIALIZE(DT_DRV_INST(n), ctx).inst_idx = n};                \
+		SPI_CONTEXT_CS_GPIOS_INITIALIZE(DT_DRV_INST(n), ctx)};                             \
 	static const struct spi_ambiq_config spi_ambiq_config##n = {                               \
 		.base = DT_INST_REG_ADDR(n),                                                       \
 		.size = DT_INST_REG_SIZE(n),                                                       \
+		.inst_idx = (DT_INST_REG_ADDR(n) - IOM0_BASE) / (IOM1_BASE - IOM0_BASE),           \
 		.clock_freq = DT_INST_PROP(n, clock_frequency),                                    \
 		.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n),                                         \
 		.irq_config_func = spi_irq_config_func_##n};                                       \
diff --git a/drivers/spi/spi_ambiq_spid.c b/drivers/spi/spi_ambiq_spid.c
index be19fcf9cc3c6..1a0d3115fc37c 100644
--- a/drivers/spi/spi_ambiq_spid.c
+++ b/drivers/spi/spi_ambiq_spid.c
@@ -22,10 +22,12 @@ LOG_MODULE_REGISTER(spi_ambiq_spid);
 #include "spi_context.h"
 #include <am_mcu_apollo.h>
 
+#define SPID_ADDR_INTERVAL 1
 struct spi_ambiq_config {
 	const struct gpio_dt_spec int_gpios;
 	uint32_t base;
 	int size;
+	int inst_idx;
 	const struct pinctrl_dev_config *pcfg;
 	void (*irq_config_func)(void);
 };
@@ -34,7 +36,6 @@ struct spi_ambiq_data {
 	struct spi_context ctx;
 	am_hal_ios_config_t ios_cfg;
 	void *ios_handler;
-	int inst_idx;
 	struct k_sem spim_wrcmp_sem;
 };
 
@@ -331,8 +332,7 @@ static int spi_ambiq_init(const struct device *dev)
 	const struct spi_ambiq_config *cfg = dev->config;
 	int ret = 0;
 
-	if (AM_HAL_STATUS_SUCCESS !=
-	    am_hal_ios_initialize((cfg->base - IOSLAVE_BASE) / cfg->size, &data->ios_handler)) {
+	if (AM_HAL_STATUS_SUCCESS != am_hal_ios_initialize(cfg->inst_idx, &data->ios_handler)) {
 		LOG_ERR("Fail to initialize SPID\n");
 		return -ENXIO;
 	}
@@ -400,12 +400,12 @@ static int spi_ambiq_pm_action(const struct device *dev, enum pm_device_action a
 	static struct spi_ambiq_data spi_ambiq_data##n = {                                         \
 		SPI_CONTEXT_INIT_LOCK(spi_ambiq_data##n, ctx),                                     \
 		SPI_CONTEXT_INIT_SYNC(spi_ambiq_data##n, ctx),                                     \
-		.spim_wrcmp_sem = Z_SEM_INITIALIZER(spi_ambiq_data##n.spim_wrcmp_sem, 0, 1),       \
-		.inst_idx = n};                                                                    \
+		.spim_wrcmp_sem = Z_SEM_INITIALIZER(spi_ambiq_data##n.spim_wrcmp_sem, 0, 1)};      \
 	static const struct spi_ambiq_config spi_ambiq_config##n = {                               \
 		.int_gpios = GPIO_DT_SPEC_INST_GET(n, int_gpios),                                  \
 		.base = DT_INST_REG_ADDR(n),                                                       \
 		.size = DT_INST_REG_SIZE(n),                                                       \
+		.inst_idx = (DT_INST_REG_ADDR(n) - IOSLAVE_BASE) / SPID_ADDR_INTERVAL,             \
 		.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n),                                         \
 		.irq_config_func = spi_irq_config_func_##n};                                       \
 	PM_DEVICE_DT_INST_DEFINE(n, spi_ambiq_pm_action);                                          \


Reviews:
user: login: aaronyegx
state: DISMISSED
author_association: CONTRIBUTOR
submitted_at: 2025-03-18T10:22:29Z
user: login: swift-tk
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-18T10:23:14Z
user: login: swift-tk
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-18T10:24:04Z
user: login: swift-tk
state: DISMISSED
author_association: CONTRIBUTOR
submitted_at: 2025-03-19T02:56:12Z
user: login: aaronyegx
state: DISMISSED
author_association: CONTRIBUTOR
submitted_at: 2025-03-19T03:58:26Z
user: login: RichardSWheatley
state: DISMISSED
author_association: CONTRIBUTOR
submitted_at: 2025-03-19T14:47:40Z
user: login: aaronyegx
state: DISMISSED
author_association: CONTRIBUTOR
submitted_at: 2025-03-21T01:13:11Z
user: login: swift-tk
state: DISMISSED
author_association: CONTRIBUTOR
submitted_at: 2025-03-24T09:07:06Z
user: login: swift-tk
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-03-24T09:25:21Z
user: login: aaronyegx
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-03-25T01:37:10Z

Review comments:
diff_hunk: @@ -438,19 +437,18 @@ static int i2c_ambiq_pm_action(const struct device *dev, enum pm_device_action a
 	};                                                                                         \
 	static struct i2c_ambiq_data i2c_ambiq_data##n = {                                         \
 		.bus_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.bus_sem, 1, 1),                     \
-		.transfer_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.transfer_sem, 0, 1),           \
-		.inst_idx = n,                                                                     \
-	};                                                                                         \
+		.transfer_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.transfer_sem, 0, 1)};          \
 	static const struct i2c_ambiq_config i2c_ambiq_config##n = {                               \
 		.base = DT_INST_REG_ADDR(n),                                                       \
 		.size = DT_INST_REG_SIZE(n),                                                       \
+		.inst_idx = (DT_INST_REG_ADDR(n) - IOM0_BASE) / (IOM1_BASE - IOM0_BASE),           \
 		.bitrate = DT_INST_PROP(n, clock_frequency),                                       \
 		.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n),                                         \
 		.irq_config_func = i2c_irq_config_func_##n,                                        \
 		.pwr_func = pwr_on_ambiq_i2c_##n,                                                  \
 		IF_ENABLED(CONFIG_I2C_AMBIQ_BUS_RECOVERY,			\
 		(.scl = GPIO_DT_SPEC_INST_GET_OR(n, scl_gpios, {0}),\
-		 .sda = GPIO_DT_SPEC_INST_GET_OR(n, sda_gpios, {0}),)) };      \
+		 .sda = GPIO_DT_SPEC_INST_GET_OR(n, sda_gpios, {0}),)) };       \
path: drivers/i2c/i2c_ambiq.c
user: login: swift-tk
body: ought to align the slashes way back at ln100?
created_at: 2025-03-18T10:23:14Z
author_association: CONTRIBUTOR
diff_hunk: @@ -1356,8 +1364,7 @@ static DEVICE_API(mspi, mspi_ambiq_driver_api) = {
 
 #define MSPI_CONFIG(n)                                                                           \
 	{                                                                                        \
-		.channel_num           = (DT_INST_REG_ADDR(n) - MSPI0_BASE) /                    \
-					 (DT_INST_REG_SIZE(n) * 4),                              \
+		.channel_num      = (DT_INST_REG_ADDR(n) - MSPI_BASE_ADDR) / MSPI_ADDR_INTERVAL, \
path: drivers/mspi/mspi_ambiq_ap3.c
user: login: swift-tk
body: please align my equal signs, thanks!
created_at: 2025-03-18T10:24:04Z
author_association: CONTRIBUTOR

PR comments:
user: login: kartben
created_at: 2025-03-20T08:33:33Z
body: @AlessandroLuo this needs to be rebased - thx!

author_association: MEMBER
user: login: RichardSWheatley
created_at: 2025-03-20T12:39:54Z
body: > @AlessandroLuo this needs to be rebased - thx!
> 
> 

I'm sure he will be happy to rebase but for future reference can you specify exactly why?
author_association: CONTRIBUTOR
user: login: AlessandroLuo
created_at: 2025-03-20T15:39:47Z
body: > @AlessandroLuo this needs to be rebased - thx!

Done
author_association: CONTRIBUTOR