Skip to content

Commit 70e45b7

Browse files
decsnyjhedberg
authored andcommitted
drivers: spi_mcux_flexcomm: Fix spi slave count check
The current implementation checking for the validity of the spi slave number is wrong. First of all, there is an off by one error where it allows SPI_CHIP_SELECT_COUNT value as a valid slave, when really it should be that value minus one. Secondly, it doesn't take into account the fact that having software controlled GPIO CS can technically have any number of slaves on the bus. So fix by finding the max of these two values and fixing the off by one mistake. Also, for RW612, only one HW native CS is available due to SOC pinmux limitations. Signed-off-by: Declan Snyder <[email protected]>
1 parent d18c4db commit 70e45b7

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

drivers/spi/spi_mcux_flexcomm.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Copyright (c) 2016, Freescale Semiconductor, Inc.
3-
* Copyright 2017, 2019, 2025, NXP
3+
* Copyright 2017, 2019, 2025 NXP
44
*
55
* SPDX-License-Identifier: Apache-2.0
66
*/
@@ -29,7 +29,13 @@ LOG_MODULE_REGISTER(spi_mcux_flexcomm, CONFIG_SPI_LOG_LEVEL);
2929

3030
#include "spi_context.h"
3131

32-
#define SPI_CHIP_SELECT_COUNT 4
32+
#ifdef CONFIG_SOC_SERIES_RW6XX
33+
/* The RW61x SOC only allows use of HW chip select 0 */
34+
#define SPI_CHIP_SELECT_COUNT 1
35+
#else
36+
#define SPI_CHIP_SELECT_COUNT FSL_FEATURE_SPI_SSEL_COUNT
37+
#endif
38+
3339
#define SPI_MAX_DATA_WIDTH 16
3440
#define SPI_MIN_DATA_WIDTH 4
3541

@@ -225,9 +231,19 @@ static int spi_mcux_configure(const struct device *dev,
225231
return -EINVAL;
226232
}
227233

228-
if (spi_cfg->slave > SPI_CHIP_SELECT_COUNT) {
229-
LOG_ERR("Slave %d is greater than %d",
230-
spi_cfg->slave, SPI_CHIP_SELECT_COUNT);
234+
uint8_t max_slave = SPI_CHIP_SELECT_COUNT;
235+
236+
#ifndef DT_SPI_CTX_HAS_NO_CS_GPIOS
237+
struct spi_context *ctx = &data->ctx;
238+
239+
max_slave = MAX(max_slave, ctx->num_cs_gpios);
240+
#endif
241+
242+
max_slave -= 1;
243+
244+
if (spi_cfg->slave > max_slave) {
245+
LOG_ERR("Slave %d is greater than max %d",
246+
spi_cfg->slave, max_slave);
231247
return -EINVAL;
232248
}
233249

0 commit comments

Comments
 (0)