Skip to content

Commit 4ba5ad1

Browse files
committed
drivers: ambiq: Correct peripheral instance calculation
This commit corrected ambiq peripheral instance calculations Signed-off-by: Hao Luo <hluo@ambiq.com>
1 parent ee33539 commit 4ba5ad1

File tree

5 files changed

+32
-22
lines changed

5 files changed

+32
-22
lines changed

drivers/i2c/i2c_ambiq.c

+7-9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct i2c_ambiq_config {
3939
#endif /* CONFIG_I2C_AMBIQ_BUS_RECOVERY */
4040
uint32_t base;
4141
int size;
42+
int inst_idx;
4243
uint32_t bitrate;
4344
const struct pinctrl_dev_config *pcfg;
4445
ambiq_i2c_pwr_func_t pwr_func;
@@ -54,7 +55,6 @@ struct i2c_ambiq_data {
5455
struct k_sem transfer_sem;
5556
i2c_ambiq_callback_t callback;
5657
void *callback_data;
57-
int inst_idx;
5858
uint32_t transfer_status;
5959
bool pm_policy_state_on;
6060
};
@@ -186,6 +186,7 @@ static int i2c_ambiq_write(const struct device *dev, struct i2c_msg *msg, uint16
186186
static int i2c_ambiq_configure(const struct device *dev, uint32_t dev_config)
187187
{
188188
struct i2c_ambiq_data *data = dev->data;
189+
const struct i2c_ambiq_config *cfg = dev->config;
189190

190191
if (!(I2C_MODE_CONTROLLER & dev_config)) {
191192
return -EINVAL;
@@ -206,7 +207,7 @@ static int i2c_ambiq_configure(const struct device *dev, uint32_t dev_config)
206207
}
207208

208209
#ifdef CONFIG_I2C_AMBIQ_DMA
209-
data->iom_cfg.pNBTxnBuf = i2c_dma_tcb_buf[data->inst_idx].buf;
210+
data->iom_cfg.pNBTxnBuf = i2c_dma_tcb_buf[cfg->inst_idx].buf;
210211
data->iom_cfg.ui32NBTxnBufLength = CONFIG_I2C_DMA_TCB_BUFFER_SIZE;
211212
#endif
212213

@@ -343,9 +344,7 @@ static int i2c_ambiq_init(const struct device *dev)
343344

344345
data->iom_cfg.eInterfaceMode = AM_HAL_IOM_I2C_MODE;
345346

346-
if (AM_HAL_STATUS_SUCCESS !=
347-
am_hal_iom_initialize((config->base - IOM0_BASE) / config->size,
348-
&data->iom_handler)) {
347+
if (AM_HAL_STATUS_SUCCESS != am_hal_iom_initialize(config->inst_idx, &data->iom_handler)) {
349348
LOG_ERR("Fail to initialize I2C\n");
350349
return -ENXIO;
351350
}
@@ -438,19 +437,18 @@ static int i2c_ambiq_pm_action(const struct device *dev, enum pm_device_action a
438437
}; \
439438
static struct i2c_ambiq_data i2c_ambiq_data##n = { \
440439
.bus_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.bus_sem, 1, 1), \
441-
.transfer_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.transfer_sem, 0, 1), \
442-
.inst_idx = n, \
443-
}; \
440+
.transfer_sem = Z_SEM_INITIALIZER(i2c_ambiq_data##n.transfer_sem, 0, 1)}; \
444441
static const struct i2c_ambiq_config i2c_ambiq_config##n = { \
445442
.base = DT_INST_REG_ADDR(n), \
446443
.size = DT_INST_REG_SIZE(n), \
444+
.inst_idx = (DT_INST_REG_ADDR(n) - IOM0_BASE) / (IOM1_BASE - IOM0_BASE), \
447445
.bitrate = DT_INST_PROP(n, clock_frequency), \
448446
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
449447
.irq_config_func = i2c_irq_config_func_##n, \
450448
.pwr_func = pwr_on_ambiq_i2c_##n, \
451449
IF_ENABLED(CONFIG_I2C_AMBIQ_BUS_RECOVERY, \
452450
(.scl = GPIO_DT_SPEC_INST_GET_OR(n, scl_gpios, {0}),\
453-
.sda = GPIO_DT_SPEC_INST_GET_OR(n, sda_gpios, {0}),)) }; \
451+
.sda = GPIO_DT_SPEC_INST_GET_OR(n, sda_gpios, {0}),)) }; \
454452
PM_DEVICE_DT_INST_DEFINE(n, i2c_ambiq_pm_action); \
455453
I2C_DEVICE_DT_INST_DEFINE(n, i2c_ambiq_init, PM_DEVICE_DT_INST_GET(n), &i2c_ambiq_data##n, \
456454
&i2c_ambiq_config##n, POST_KERNEL, CONFIG_I2C_INIT_PRIORITY, \

drivers/mspi/mspi_ambiq_ap3.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ LOG_MODULE_REGISTER(mspi_ambiq_ap3);
2626
#define PWRCTRL_MAX_WAIT_US 5
2727
#define MSPI_BUSY BIT(2)
2828

29+
#if defined(CONFIG_SOC_APOLLO3_BLUE)
30+
#define MSPI_BASE_ADDR MSPI_BASE
31+
#define MSPI_ADDR_INTERVAL 1
32+
#else
33+
#define MSPI_BASE_ADDR MSPI0_BASE
34+
#define MSPI_ADDR_INTERVAL (MSPI1_BASE - MSPI0_BASE)
35+
#endif
36+
2937
typedef int (*mspi_ambiq_pwr_func_t)(void);
3038
typedef void (*irq_config_func_t)(void);
3139

@@ -1356,8 +1364,7 @@ static DEVICE_API(mspi, mspi_ambiq_driver_api) = {
13561364

13571365
#define MSPI_CONFIG(n) \
13581366
{ \
1359-
.channel_num = (DT_INST_REG_ADDR(n) - MSPI0_BASE) / \
1360-
(DT_INST_REG_SIZE(n) * 4), \
1367+
.channel_num = (DT_INST_REG_ADDR(n) - MSPI_BASE_ADDR) / MSPI_ADDR_INTERVAL, \
13611368
.op_mode = MSPI_OP_MODE_CONTROLLER, \
13621369
.duplex = MSPI_HALF_DUPLEX, \
13631370
.max_freq = MSPI_MAX_FREQ, \

drivers/spi/spi_ambiq_bleif.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ static int spi_ambiq_init(const struct device *dev)
180180
}
181181
#endif /* CONFIG_SPI_AMBIQ_BLEIF_TIMING_TRACE */
182182

183-
ret = am_hal_ble_initialize((cfg->base - BLEIF_BASE) / cfg->size, &data->BLEhandle);
183+
ret = am_hal_ble_initialize(0, &data->BLEhandle);
184184
if (ret) {
185185
return ret;
186186
}

drivers/spi/spi_ambiq_spic.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ typedef int (*ambiq_spi_pwr_func_t)(void);
3030
struct spi_ambiq_config {
3131
uint32_t base;
3232
int size;
33+
int inst_idx;
3334
uint32_t clock_freq;
3435
const struct pinctrl_dev_config *pcfg;
3536
ambiq_spi_pwr_func_t pwr_func;
@@ -40,7 +41,6 @@ struct spi_ambiq_data {
4041
struct spi_context ctx;
4142
am_hal_iom_config_t iom_cfg;
4243
void *iom_handler;
43-
int inst_idx;
4444
bool cont;
4545
bool pm_policy_state_on;
4646
};
@@ -190,7 +190,7 @@ static int spi_config(const struct device *dev, const struct spi_config *config)
190190
ctx->config = config;
191191

192192
#ifdef CONFIG_SPI_AMBIQ_DMA
193-
data->iom_cfg.pNBTxnBuf = spi_dma_tcb_buf[data->inst_idx].buf;
193+
data->iom_cfg.pNBTxnBuf = spi_dma_tcb_buf[cfg->inst_idx].buf;
194194
data->iom_cfg.ui32NBTxnBufLength = CONFIG_SPI_DMA_TCB_BUFFER_SIZE;
195195
#endif
196196

@@ -431,8 +431,7 @@ static int spi_ambiq_init(const struct device *dev)
431431
const struct spi_ambiq_config *cfg = dev->config;
432432
int ret = 0;
433433

434-
if (AM_HAL_STATUS_SUCCESS !=
435-
am_hal_iom_initialize((cfg->base - IOM0_BASE) / cfg->size, &data->iom_handler)) {
434+
if (AM_HAL_STATUS_SUCCESS != am_hal_iom_initialize(cfg->inst_idx, &data->iom_handler)) {
436435
LOG_ERR("Fail to initialize SPI\n");
437436
return -ENXIO;
438437
}
@@ -508,10 +507,11 @@ static int spi_ambiq_pm_action(const struct device *dev, enum pm_device_action a
508507
static struct spi_ambiq_data spi_ambiq_data##n = { \
509508
SPI_CONTEXT_INIT_LOCK(spi_ambiq_data##n, ctx), \
510509
SPI_CONTEXT_INIT_SYNC(spi_ambiq_data##n, ctx), \
511-
SPI_CONTEXT_CS_GPIOS_INITIALIZE(DT_DRV_INST(n), ctx).inst_idx = n}; \
510+
SPI_CONTEXT_CS_GPIOS_INITIALIZE(DT_DRV_INST(n), ctx)}; \
512511
static const struct spi_ambiq_config spi_ambiq_config##n = { \
513512
.base = DT_INST_REG_ADDR(n), \
514513
.size = DT_INST_REG_SIZE(n), \
514+
.inst_idx = (DT_INST_REG_ADDR(n) - IOM0_BASE) / (IOM1_BASE - IOM0_BASE), \
515515
.clock_freq = DT_INST_PROP(n, clock_frequency), \
516516
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
517517
.irq_config_func = spi_irq_config_func_##n, \

drivers/spi/spi_ambiq_spid.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,19 @@ LOG_MODULE_REGISTER(spi_ambiq_spid);
2424

2525
#define AMBIQ_SPID_PWRCTRL_MAX_WAIT_US 5
2626

27+
#if defined(CONFIG_SOC_SERIES_APOLLO5X)
28+
#define SPID_ADDR_INTERVAL (IOSLAVEFD1_BASE - IOSLAVEFD0_BASE)
29+
#else
30+
#define SPID_ADDR_INTERVAL 1
31+
#endif
32+
2733
typedef int (*ambiq_spi_pwr_func_t)(void);
2834

2935
struct spi_ambiq_config {
3036
const struct gpio_dt_spec int_gpios;
3137
uint32_t base;
3238
int size;
39+
int inst_idx;
3340
const struct pinctrl_dev_config *pcfg;
3441
ambiq_spi_pwr_func_t pwr_func;
3542
void (*irq_config_func)(void);
@@ -39,7 +46,6 @@ struct spi_ambiq_data {
3946
struct spi_context ctx;
4047
am_hal_ios_config_t ios_cfg;
4148
void *ios_handler;
42-
int inst_idx;
4349
struct k_sem spim_wrcmp_sem;
4450
};
4551

@@ -336,8 +342,7 @@ static int spi_ambiq_init(const struct device *dev)
336342
const struct spi_ambiq_config *cfg = dev->config;
337343
int ret = 0;
338344

339-
if (AM_HAL_STATUS_SUCCESS !=
340-
am_hal_ios_initialize((cfg->base - IOSLAVE_BASE) / cfg->size, &data->ios_handler)) {
345+
if (AM_HAL_STATUS_SUCCESS != am_hal_ios_initialize(cfg->inst_idx, &data->ios_handler)) {
341346
LOG_ERR("Fail to initialize SPID\n");
342347
return -ENXIO;
343348
}
@@ -413,12 +418,12 @@ static int spi_ambiq_pm_action(const struct device *dev, enum pm_device_action a
413418
static struct spi_ambiq_data spi_ambiq_data##n = { \
414419
SPI_CONTEXT_INIT_LOCK(spi_ambiq_data##n, ctx), \
415420
SPI_CONTEXT_INIT_SYNC(spi_ambiq_data##n, ctx), \
416-
.spim_wrcmp_sem = Z_SEM_INITIALIZER(spi_ambiq_data##n.spim_wrcmp_sem, 0, 1), \
417-
.inst_idx = n}; \
421+
.spim_wrcmp_sem = Z_SEM_INITIALIZER(spi_ambiq_data##n.spim_wrcmp_sem, 0, 1)}; \
418422
static const struct spi_ambiq_config spi_ambiq_config##n = { \
419423
.int_gpios = GPIO_DT_SPEC_INST_GET(n, int_gpios), \
420424
.base = DT_INST_REG_ADDR(n), \
421425
.size = DT_INST_REG_SIZE(n), \
426+
.inst_idx = (DT_INST_REG_ADDR(n) - IOSLAVE_BASE) / SPID_ADDR_INTERVAL, \
422427
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(n), \
423428
.irq_config_func = spi_irq_config_func_##n, \
424429
.pwr_func = pwr_on_ambiq_spi_##n}; \

0 commit comments

Comments
 (0)