Unverified Commit f879365c authored by CL Wang's avatar CL Wang Committed by Mark Brown
Browse files

spi: atcspi200: Handle invalid buswidth and fix compiler warning



The kernel test robot reported a compile-time error regarding the
FIELD_PREP() value being too large for the TRANS_DUAL_QUAD field:

  error: FIELD_PREP: value too large for the field
  note: in expansion of macro 'TRANS_DUAL_QUAD'
  tc |= TRANS_DUAL_QUAD(ffs(op->data.buswidth) - 1);

This occurs because TRANS_DUAL_QUAD is defined as a 2-bit field, and
GCC's static analysis cannot deduce that `ffs(op->data.buswidth) - 1`
will strictly fall within the 0~3 range. Although the SPI framework
guarantees that `op->data.buswidth` is valid at runtime (e.g., 1, 2,
4, 8), an explicit bounds check is necessary to satisfy the compiler.

To resolve the build warning, introduce a safe fallback mechanism.
If an unexpected buswidth is encountered, the driver will trigger
a WARN_ON_ONCE to leave a trace and fall back to width_code = 0
(standard 1-bit SPI mode). This approach guarantees predictable
hardware behavior.

Reported-by: default avatarkernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202602140738.P7ZozxzI-lkp@intel.com/


Suggested-by: default avatarPei Xiao <xiaopei01@kylinos.cn>
Signed-off-by: default avatarCL Wang <cl634@andestech.com>
Link: https://patch.msgid.link/20260303024737.1791196-1-cl634@andestech.com


Signed-off-by: default avatarMark Brown <broonie@kernel.org>
parent 743956bb
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -195,7 +195,15 @@ static void atcspi_set_trans_ctl(struct atcspi_dev *spi,
	if (op->addr.buswidth > 1)
		tc |= TRANS_ADDR_FMT;
	if (op->data.nbytes) {
		tc |= TRANS_DUAL_QUAD(ffs(op->data.buswidth) - 1);
		unsigned int width_code;

		width_code = ffs(op->data.buswidth) - 1;
		if (unlikely(width_code > 3)) {
			WARN_ON_ONCE(1);
			width_code = 0;
		}
		tc |= TRANS_DUAL_QUAD(width_code);

		if (op->data.dir == SPI_MEM_DATA_IN) {
			if (op->dummy.nbytes)
				tc |= TRANS_MODE_DMY_READ |