Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'spi-fix-v6.11-merge-window' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi

Pull spi fixes from Mark Brown:
"The bulk of this is a series of fixes for the microchip-core driver
mostly originating from one of their customers, I also applied an
additional patch adding support for controlling the word size which
came along with it since it's still the merge window and clearly had a
bunch of fairly thorough testing.

We also have a fix for the compatible used to bind spidev to the
BH2228FV"

* tag 'spi-fix-v6.11-merge-window' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi:
spi: spidev: add correct compatible for Rohm BH2228FV
dt-bindings: trivial-devices: fix Rohm BH2228FV compatible string
spi: microchip-core: add support for word sizes of 1 to 32 bits
spi: microchip-core: ensure TX and RX FIFOs are empty at start of a transfer
spi: microchip-core: fix init function not setting the master and motorola modes
spi: microchip-core: only disable SPI controller when register value change requires it
spi: microchip-core: defer asserting chip select until just before write to TX FIFO
spi: microchip-core: fix the issues in the isr

+114 -81
+3 -1
Documentation/devicetree/bindings/trivial-devices.yaml
··· 328 328 - renesas,hs3001 329 329 # Renesas ISL29501 time-of-flight sensor 330 330 - renesas,isl29501 331 - # Rohm DH2228FV 331 + # Rohm BH2228FV 8 channel DAC 332 + - rohm,bh2228fv 333 + # Rohm DH2228FV - This device does not exist, use rohm,bh2228fv instead. 332 334 - rohm,dh2228fv 333 335 # S524AD0XF1 (128K/256K-bit Serial EEPROM for Low Power) 334 336 - samsung,24ad0xd1
+110 -80
drivers/spi/spi-microchip-core.c
··· 75 75 76 76 #define REG_CONTROL (0x00) 77 77 #define REG_FRAME_SIZE (0x04) 78 + #define FRAME_SIZE_MASK GENMASK(5, 0) 78 79 #define REG_STATUS (0x08) 79 80 #define REG_INT_CLEAR (0x0c) 80 81 #define REG_RX_DATA (0x10) ··· 90 89 #define REG_RIS (0x24) 91 90 #define REG_CONTROL2 (0x28) 92 91 #define REG_COMMAND (0x2c) 92 + #define COMMAND_CLRFRAMECNT BIT(4) 93 + #define COMMAND_TXFIFORST BIT(3) 94 + #define COMMAND_RXFIFORST BIT(2) 93 95 #define REG_PKTSIZE (0x30) 94 96 #define REG_CMD_SIZE (0x34) 95 97 #define REG_HWSTATUS (0x38) ··· 107 103 u8 *rx_buf; 108 104 u32 clk_gen; /* divider for spi output clock generated by the controller */ 109 105 u32 clk_mode; 106 + u32 pending_slave_select; 110 107 int irq; 111 108 int tx_len; 112 109 int rx_len; 113 - int pending; 110 + int n_bytes; 114 111 }; 115 112 116 113 static inline u32 mchp_corespi_read(struct mchp_corespi *spi, unsigned int reg) ··· 135 130 136 131 static inline void mchp_corespi_read_fifo(struct mchp_corespi *spi) 137 132 { 138 - u8 data; 139 - int fifo_max, i = 0; 133 + while (spi->rx_len >= spi->n_bytes && !(mchp_corespi_read(spi, REG_STATUS) & STATUS_RXFIFO_EMPTY)) { 134 + u32 data = mchp_corespi_read(spi, REG_RX_DATA); 140 135 141 - fifo_max = min(spi->rx_len, FIFO_DEPTH); 136 + spi->rx_len -= spi->n_bytes; 142 137 143 - while ((i < fifo_max) && !(mchp_corespi_read(spi, REG_STATUS) & STATUS_RXFIFO_EMPTY)) { 144 - data = mchp_corespi_read(spi, REG_RX_DATA); 138 + if (!spi->rx_buf) 139 + continue; 145 140 146 - if (spi->rx_buf) 147 - *spi->rx_buf++ = data; 148 - i++; 141 + if (spi->n_bytes == 4) 142 + *((u32 *)spi->rx_buf) = data; 143 + else if (spi->n_bytes == 2) 144 + *((u16 *)spi->rx_buf) = data; 145 + else 146 + *spi->rx_buf = data; 147 + 148 + spi->rx_buf += spi->n_bytes; 149 149 } 150 - spi->rx_len -= i; 151 - spi->pending -= i; 152 150 } 153 151 154 152 static void mchp_corespi_enable_ints(struct mchp_corespi *spi) 155 153 { 156 - u32 control, mask = INT_ENABLE_MASK; 154 + u32 control = mchp_corespi_read(spi, REG_CONTROL); 157 155 158 - mchp_corespi_disable(spi); 159 - 160 - control = mchp_corespi_read(spi, REG_CONTROL); 161 - 162 - control |= mask; 163 - mchp_corespi_write(spi, REG_CONTROL, control); 164 - 165 - control |= CONTROL_ENABLE; 156 + control |= INT_ENABLE_MASK; 166 157 mchp_corespi_write(spi, REG_CONTROL, control); 167 158 } 168 159 169 160 static void mchp_corespi_disable_ints(struct mchp_corespi *spi) 170 161 { 171 - u32 control, mask = INT_ENABLE_MASK; 162 + u32 control = mchp_corespi_read(spi, REG_CONTROL); 172 163 173 - mchp_corespi_disable(spi); 174 - 175 - control = mchp_corespi_read(spi, REG_CONTROL); 176 - control &= ~mask; 177 - mchp_corespi_write(spi, REG_CONTROL, control); 178 - 179 - control |= CONTROL_ENABLE; 164 + control &= ~INT_ENABLE_MASK; 180 165 mchp_corespi_write(spi, REG_CONTROL, control); 181 166 } 182 167 183 168 static inline void mchp_corespi_set_xfer_size(struct mchp_corespi *spi, int len) 184 169 { 185 170 u32 control; 186 - u16 lenpart; 171 + u32 lenpart; 172 + u32 frames = mchp_corespi_read(spi, REG_FRAMESUP); 187 173 188 174 /* 189 - * Disable the SPI controller. Writes to transfer length have 190 - * no effect when the controller is enabled. 175 + * Writing to FRAMECNT in REG_CONTROL will reset the frame count, taking 176 + * a shortcut requires an explicit clear. 191 177 */ 192 - mchp_corespi_disable(spi); 178 + if (frames == len) { 179 + mchp_corespi_write(spi, REG_COMMAND, COMMAND_CLRFRAMECNT); 180 + return; 181 + } 193 182 194 183 /* 195 184 * The lower 16 bits of the frame count are stored in the control reg 196 185 * for legacy reasons, but the upper 16 written to a different register: 197 186 * FRAMESUP. While both the upper and lower bits can be *READ* from the 198 - * FRAMESUP register, writing to the lower 16 bits is a NOP 187 + * FRAMESUP register, writing to the lower 16 bits is (supposedly) a NOP. 188 + * 189 + * The driver used to disable the controller while modifying the frame 190 + * count, and mask off the lower 16 bits of len while writing to 191 + * FRAMES_UP. When the driver was changed to disable the controller as 192 + * infrequently as possible, it was discovered that the logic of 193 + * lenpart = len & 0xffff_0000 194 + * write(REG_FRAMESUP, lenpart) 195 + * would actually write zeros into the lower 16 bits on an mpfs250t-es, 196 + * despite documentation stating these bits were read-only. 197 + * Writing len unmasked into FRAMES_UP ensures those bits aren't zeroed 198 + * on an mpfs250t-es and will be a NOP for the lower 16 bits on hardware 199 + * that matches the documentation. 199 200 */ 200 201 lenpart = len & 0xffff; 201 - 202 202 control = mchp_corespi_read(spi, REG_CONTROL); 203 203 control &= ~CONTROL_FRAMECNT_MASK; 204 204 control |= lenpart << CONTROL_FRAMECNT_SHIFT; 205 205 mchp_corespi_write(spi, REG_CONTROL, control); 206 - 207 - lenpart = len & 0xffff0000; 208 - mchp_corespi_write(spi, REG_FRAMESUP, lenpart); 209 - 210 - control |= CONTROL_ENABLE; 211 - mchp_corespi_write(spi, REG_CONTROL, control); 206 + mchp_corespi_write(spi, REG_FRAMESUP, len); 212 207 } 213 208 214 209 static inline void mchp_corespi_write_fifo(struct mchp_corespi *spi) 215 210 { 216 - u8 byte; 217 211 int fifo_max, i = 0; 218 212 219 - fifo_max = min(spi->tx_len, FIFO_DEPTH); 213 + fifo_max = DIV_ROUND_UP(min(spi->tx_len, FIFO_DEPTH), spi->n_bytes); 220 214 mchp_corespi_set_xfer_size(spi, fifo_max); 221 215 222 216 while ((i < fifo_max) && !(mchp_corespi_read(spi, REG_STATUS) & STATUS_TXFIFO_FULL)) { 223 - byte = spi->tx_buf ? *spi->tx_buf++ : 0xaa; 224 - mchp_corespi_write(spi, REG_TX_DATA, byte); 217 + u32 word; 218 + 219 + if (spi->n_bytes == 4) 220 + word = spi->tx_buf ? *((u32 *)spi->tx_buf) : 0xaa; 221 + else if (spi->n_bytes == 2) 222 + word = spi->tx_buf ? *((u16 *)spi->tx_buf) : 0xaa; 223 + else 224 + word = spi->tx_buf ? *spi->tx_buf : 0xaa; 225 + 226 + mchp_corespi_write(spi, REG_TX_DATA, word); 227 + if (spi->tx_buf) 228 + spi->tx_buf += spi->n_bytes; 225 229 i++; 226 230 } 227 231 228 - spi->tx_len -= i; 229 - spi->pending += i; 232 + spi->tx_len -= i * spi->n_bytes; 230 233 } 231 234 232 235 static inline void mchp_corespi_set_framesize(struct mchp_corespi *spi, int bt) 233 236 { 237 + u32 frame_size = mchp_corespi_read(spi, REG_FRAME_SIZE); 234 238 u32 control; 239 + 240 + if ((frame_size & FRAME_SIZE_MASK) == bt) 241 + return; 235 242 236 243 /* 237 244 * Disable the SPI controller. Writes to the frame size have 238 245 * no effect when the controller is enabled. 239 246 */ 240 - mchp_corespi_disable(spi); 247 + control = mchp_corespi_read(spi, REG_CONTROL); 248 + control &= ~CONTROL_ENABLE; 249 + mchp_corespi_write(spi, REG_CONTROL, control); 241 250 242 251 mchp_corespi_write(spi, REG_FRAME_SIZE, bt); 243 252 244 - control = mchp_corespi_read(spi, REG_CONTROL); 245 253 control |= CONTROL_ENABLE; 246 254 mchp_corespi_write(spi, REG_CONTROL, control); 247 255 } ··· 267 249 reg = mchp_corespi_read(corespi, REG_SLAVE_SELECT); 268 250 reg &= ~BIT(spi_get_chipselect(spi, 0)); 269 251 reg |= !disable << spi_get_chipselect(spi, 0); 252 + corespi->pending_slave_select = reg; 270 253 271 - mchp_corespi_write(corespi, REG_SLAVE_SELECT, reg); 254 + /* 255 + * Only deassert chip select immediately. Writing to some registers 256 + * requires the controller to be disabled, which results in the 257 + * output pins being tristated and can cause the SCLK and MOSI lines 258 + * to transition. Therefore asserting the chip select is deferred 259 + * until just before writing to the TX FIFO, to ensure the device 260 + * doesn't see any spurious clock transitions whilst CS is enabled. 261 + */ 262 + if (((spi->mode & SPI_CS_HIGH) == 0) == disable) 263 + mchp_corespi_write(corespi, REG_SLAVE_SELECT, reg); 272 264 } 273 265 274 266 static int mchp_corespi_setup(struct spi_device *spi) ··· 297 269 if (spi->mode & SPI_CS_HIGH) { 298 270 reg = mchp_corespi_read(corespi, REG_SLAVE_SELECT); 299 271 reg |= BIT(spi_get_chipselect(spi, 0)); 272 + corespi->pending_slave_select = reg; 300 273 mchp_corespi_write(corespi, REG_SLAVE_SELECT, reg); 301 274 } 302 275 return 0; ··· 308 279 unsigned long clk_hz; 309 280 u32 control = mchp_corespi_read(spi, REG_CONTROL); 310 281 311 - control |= CONTROL_MASTER; 282 + control &= ~CONTROL_ENABLE; 283 + mchp_corespi_write(spi, REG_CONTROL, control); 312 284 285 + control |= CONTROL_MASTER; 313 286 control &= ~CONTROL_MODE_MASK; 314 287 control |= MOTOROLA_MODE; 315 - 316 - mchp_corespi_set_framesize(spi, DEFAULT_FRAMESIZE); 317 - 318 - /* max. possible spi clock rate is the apb clock rate */ 319 - clk_hz = clk_get_rate(spi->clk); 320 - host->max_speed_hz = clk_hz; 321 288 322 289 /* 323 290 * The controller must be configured so that it doesn't remove Chip ··· 323 298 * BIGFIFO mode is also enabled, which sets the fifo depth to 32 frames 324 299 * for the 8 bit transfers that this driver uses. 325 300 */ 326 - control = mchp_corespi_read(spi, REG_CONTROL); 327 301 control |= CONTROL_SPS | CONTROL_BIGFIFO; 328 302 329 303 mchp_corespi_write(spi, REG_CONTROL, control); 304 + 305 + mchp_corespi_set_framesize(spi, DEFAULT_FRAMESIZE); 306 + 307 + /* max. possible spi clock rate is the apb clock rate */ 308 + clk_hz = clk_get_rate(spi->clk); 309 + host->max_speed_hz = clk_hz; 330 310 331 311 mchp_corespi_enable_ints(spi); 332 312 ··· 340 310 * select is relinquished to the hardware. SSELOUT is enabled too so we 341 311 * can deal with active high targets. 342 312 */ 343 - mchp_corespi_write(spi, REG_SLAVE_SELECT, SSELOUT | SSEL_DIRECT); 313 + spi->pending_slave_select = SSELOUT | SSEL_DIRECT; 314 + mchp_corespi_write(spi, REG_SLAVE_SELECT, spi->pending_slave_select); 344 315 345 316 control = mchp_corespi_read(spi, REG_CONTROL); 346 317 ··· 355 324 { 356 325 u32 control; 357 326 358 - mchp_corespi_disable(spi); 359 - 360 327 control = mchp_corespi_read(spi, REG_CONTROL); 361 328 if (spi->clk_mode) 362 329 control |= CONTROL_CLKMODE; ··· 363 334 364 335 mchp_corespi_write(spi, REG_CLK_GEN, spi->clk_gen); 365 336 mchp_corespi_write(spi, REG_CONTROL, control); 366 - mchp_corespi_write(spi, REG_CONTROL, control | CONTROL_ENABLE); 367 337 } 368 338 369 339 static inline void mchp_corespi_set_mode(struct mchp_corespi *spi, unsigned int mode) 370 340 { 371 - u32 control, mode_val; 341 + u32 mode_val; 342 + u32 control = mchp_corespi_read(spi, REG_CONTROL); 372 343 373 344 switch (mode & SPI_MODE_X_MASK) { 374 345 case SPI_MODE_0: ··· 386 357 } 387 358 388 359 /* 389 - * Disable the SPI controller. Writes to the frame size have 360 + * Disable the SPI controller. Writes to the frame protocol have 390 361 * no effect when the controller is enabled. 391 362 */ 392 - mchp_corespi_disable(spi); 393 363 394 - control = mchp_corespi_read(spi, REG_CONTROL); 364 + control &= ~CONTROL_ENABLE; 365 + mchp_corespi_write(spi, REG_CONTROL, control); 366 + 395 367 control &= ~(SPI_MODE_X_MASK << MODE_X_MASK_SHIFT); 396 368 control |= mode_val; 397 369 ··· 413 383 if (intfield == 0) 414 384 return IRQ_NONE; 415 385 416 - if (intfield & INT_TXDONE) { 386 + if (intfield & INT_TXDONE) 417 387 mchp_corespi_write(spi, REG_INT_CLEAR, INT_TXDONE); 388 + 389 + if (intfield & INT_RXRDY) { 390 + mchp_corespi_write(spi, REG_INT_CLEAR, INT_RXRDY); 418 391 419 392 if (spi->rx_len) 420 393 mchp_corespi_read_fifo(spi); 421 - 422 - if (spi->tx_len) 423 - mchp_corespi_write_fifo(spi); 424 - 425 - if (!spi->rx_len) 426 - finalise = true; 427 394 } 428 395 429 - if (intfield & INT_RXRDY) 430 - mchp_corespi_write(spi, REG_INT_CLEAR, INT_RXRDY); 396 + if (!spi->rx_len && !spi->tx_len) 397 + finalise = true; 431 398 432 399 if (intfield & INT_RX_CHANNEL_OVERFLOW) { 433 400 mchp_corespi_write(spi, REG_INT_CLEAR, INT_RX_CHANNEL_OVERFLOW); ··· 504 477 spi->rx_buf = xfer->rx_buf; 505 478 spi->tx_len = xfer->len; 506 479 spi->rx_len = xfer->len; 507 - spi->pending = 0; 480 + spi->n_bytes = roundup_pow_of_two(DIV_ROUND_UP(xfer->bits_per_word, BITS_PER_BYTE)); 508 481 509 - mchp_corespi_set_xfer_size(spi, (spi->tx_len > FIFO_DEPTH) 510 - ? FIFO_DEPTH : spi->tx_len); 482 + mchp_corespi_set_framesize(spi, xfer->bits_per_word); 511 483 512 - if (spi->tx_len) 484 + mchp_corespi_write(spi, REG_COMMAND, COMMAND_RXFIFORST | COMMAND_TXFIFORST); 485 + 486 + mchp_corespi_write(spi, REG_SLAVE_SELECT, spi->pending_slave_select); 487 + 488 + while (spi->tx_len) 513 489 mchp_corespi_write_fifo(spi); 490 + 514 491 return 1; 515 492 } 516 493 ··· 524 493 struct spi_device *spi_dev = msg->spi; 525 494 struct mchp_corespi *spi = spi_controller_get_devdata(host); 526 495 527 - mchp_corespi_set_framesize(spi, DEFAULT_FRAMESIZE); 528 496 mchp_corespi_set_mode(spi, spi_dev->mode); 529 497 530 498 return 0; ··· 551 521 host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; 552 522 host->use_gpio_descriptors = true; 553 523 host->setup = mchp_corespi_setup; 554 - host->bits_per_word_mask = SPI_BPW_MASK(8); 524 + host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); 555 525 host->transfer_one = mchp_corespi_transfer_one; 556 526 host->prepare_message = mchp_corespi_prepare_message; 557 527 host->set_cs = mchp_corespi_set_cs;
+1
drivers/spi/spidev.c
··· 734 734 { .compatible = "lwn,bk4", .data = &spidev_of_check }, 735 735 { .compatible = "menlo,m53cpld", .data = &spidev_of_check }, 736 736 { .compatible = "micron,spi-authenta", .data = &spidev_of_check }, 737 + { .compatible = "rohm,bh2228fv", .data = &spidev_of_check }, 737 738 { .compatible = "rohm,dh2228fv", .data = &spidev_of_check }, 738 739 { .compatible = "semtech,sx1301", .data = &spidev_of_check }, 739 740 { .compatible = "silabs,em3581", .data = &spidev_of_check },