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

spi: meson-spicc: add DMA support

Add DMA support for spicc driver.

DMA works if the transfer meets the following conditions:
1. 64 bits per word;
2. The transfer length must be multiples of the dma_burst_len,
and the dma_burst_len should be one of 8,7...2,
otherwise, it will be split into several SPI bursts.

Signed-off-by: Sunny Luo <sunny.luo@amlogic.com>
Signed-off-by: Xianwei Zhao <xianwei.zhao@amlogic.com>
Link: https://patch.msgid.link/20250414-spi-dma-v2-1-84bbd92fa469@amlogic.com
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Xianwei Zhao and committed by
Mark Brown
18197e98 b50a1e1f

+220 -21
+220 -21
drivers/spi/spi-meson-spicc.c
··· 21 21 #include <linux/interrupt.h> 22 22 #include <linux/reset.h> 23 23 #include <linux/pinctrl/consumer.h> 24 + #include <linux/dma-mapping.h> 24 25 25 26 /* 26 - * The Meson SPICC controller could support DMA based transfers, but is not 27 - * implemented by the vendor code, and while having the registers documentation 28 - * it has never worked on the GXL Hardware. 29 - * The PIO mode is the only mode implemented, and due to badly designed HW : 30 - * - all transfers are cutted in 16 words burst because the FIFO hangs on 31 - * TX underflow, and there is no TX "Half-Empty" interrupt, so we go by 32 - * FIFO max size chunk only 33 - * - CS management is dumb, and goes UP between every burst, so is really a 34 - * "Data Valid" signal than a Chip Select, GPIO link should be used instead 35 - * to have a CS go down over the full transfer 27 + * There are two modes for data transmission: PIO and DMA. 28 + * When bits_per_word is 8, 16, 24, or 32, data is transferred using PIO mode. 29 + * When bits_per_word is 64, DMA mode is used by default. 30 + * 31 + * DMA achieves a transfer with one or more SPI bursts, each SPI burst is made 32 + * up of one or more DMA bursts. The DMA burst implementation mechanism is, 33 + * For TX, when the number of words in TXFIFO is less than the preset 34 + * reading threshold, SPICC starts a reading DMA burst, which reads the preset 35 + * number of words from TX buffer, then writes them into TXFIFO. 36 + * For RX, when the number of words in RXFIFO is greater than the preset 37 + * writing threshold, SPICC starts a writing request burst, which reads the 38 + * preset number of words from RXFIFO, then write them into RX buffer. 39 + * DMA works if the transfer meets the following conditions, 40 + * - 64 bits per word 41 + * - The transfer length in word must be multiples of the dma_burst_len, and 42 + * the dma_burst_len should be one of 8,7...2, otherwise, it will be split 43 + * into several SPI bursts by this driver 36 44 */ 37 45 38 46 #define SPICC_MAX_BURST 128 ··· 136 128 137 129 #define SPICC_DWADDR 0x24 /* Write Address of DMA */ 138 130 131 + #define SPICC_LD_CNTL0 0x28 132 + #define VSYNC_IRQ_SRC_SELECT BIT(0) 133 + #define DMA_EN_SET_BY_VSYNC BIT(2) 134 + #define XCH_EN_SET_BY_VSYNC BIT(3) 135 + #define DMA_READ_COUNTER_EN BIT(4) 136 + #define DMA_WRITE_COUNTER_EN BIT(5) 137 + #define DMA_RADDR_LOAD_BY_VSYNC BIT(6) 138 + #define DMA_WADDR_LOAD_BY_VSYNC BIT(7) 139 + #define DMA_ADDR_LOAD_FROM_LD_ADDR BIT(8) 140 + 141 + #define SPICC_LD_CNTL1 0x2c 142 + #define DMA_READ_COUNTER GENMASK(15, 0) 143 + #define DMA_WRITE_COUNTER GENMASK(31, 16) 144 + #define DMA_BURST_LEN_DEFAULT 8 145 + #define DMA_BURST_COUNT_MAX 0xffff 146 + #define SPI_BURST_LEN_MAX (DMA_BURST_LEN_DEFAULT * DMA_BURST_COUNT_MAX) 147 + 139 148 #define SPICC_ENH_CTL0 0x38 /* Enhanced Feature */ 140 149 #define SPICC_ENH_CLK_CS_DELAY_MASK GENMASK(15, 0) 141 150 #define SPICC_ENH_DATARATE_MASK GENMASK(23, 16) ··· 196 171 struct pinctrl *pinctrl; 197 172 struct pinctrl_state *pins_idle_high; 198 173 struct pinctrl_state *pins_idle_low; 174 + dma_addr_t tx_dma; 175 + dma_addr_t rx_dma; 176 + bool using_dma; 199 177 }; 200 178 201 179 #define pow2_clk_to_spicc(_div) container_of(_div, struct meson_spicc_device, pow2_div) ··· 228 200 SPICC_ENH_MOSI_OEN | SPICC_ENH_CLK_OEN | SPICC_ENH_CS_OEN; 229 201 230 202 writel_relaxed(conf, spicc->base + SPICC_ENH_CTL0); 203 + } 204 + 205 + static int meson_spicc_dma_map(struct meson_spicc_device *spicc, 206 + struct spi_transfer *t) 207 + { 208 + struct device *dev = spicc->host->dev.parent; 209 + 210 + if (!(t->tx_buf && t->rx_buf)) 211 + return -EINVAL; 212 + 213 + t->tx_dma = dma_map_single(dev, (void *)t->tx_buf, t->len, DMA_TO_DEVICE); 214 + if (dma_mapping_error(dev, t->tx_dma)) 215 + return -ENOMEM; 216 + 217 + t->rx_dma = dma_map_single(dev, t->rx_buf, t->len, DMA_FROM_DEVICE); 218 + if (dma_mapping_error(dev, t->rx_dma)) 219 + return -ENOMEM; 220 + 221 + spicc->tx_dma = t->tx_dma; 222 + spicc->rx_dma = t->rx_dma; 223 + 224 + return 0; 225 + } 226 + 227 + static void meson_spicc_dma_unmap(struct meson_spicc_device *spicc, 228 + struct spi_transfer *t) 229 + { 230 + struct device *dev = spicc->host->dev.parent; 231 + 232 + if (t->tx_dma) 233 + dma_unmap_single(dev, t->tx_dma, t->len, DMA_TO_DEVICE); 234 + if (t->rx_dma) 235 + dma_unmap_single(dev, t->rx_dma, t->len, DMA_FROM_DEVICE); 236 + } 237 + 238 + /* 239 + * According to the remain words length, calculate a suitable spi burst length 240 + * and a dma burst length for current spi burst 241 + */ 242 + static u32 meson_spicc_calc_dma_len(struct meson_spicc_device *spicc, 243 + u32 len, u32 *dma_burst_len) 244 + { 245 + u32 i; 246 + 247 + if (len <= spicc->data->fifo_size) { 248 + *dma_burst_len = len; 249 + return len; 250 + } 251 + 252 + *dma_burst_len = DMA_BURST_LEN_DEFAULT; 253 + 254 + if (len == (SPI_BURST_LEN_MAX + 1)) 255 + return SPI_BURST_LEN_MAX - DMA_BURST_LEN_DEFAULT; 256 + 257 + if (len >= SPI_BURST_LEN_MAX) 258 + return SPI_BURST_LEN_MAX; 259 + 260 + for (i = DMA_BURST_LEN_DEFAULT; i > 1; i--) 261 + if ((len % i) == 0) { 262 + *dma_burst_len = i; 263 + return len; 264 + } 265 + 266 + i = len % DMA_BURST_LEN_DEFAULT; 267 + len -= i; 268 + 269 + if (i == 1) 270 + len -= DMA_BURST_LEN_DEFAULT; 271 + 272 + return len; 273 + } 274 + 275 + static void meson_spicc_setup_dma(struct meson_spicc_device *spicc) 276 + { 277 + unsigned int len; 278 + unsigned int dma_burst_len, dma_burst_count; 279 + unsigned int count_en = 0; 280 + unsigned int txfifo_thres = 0; 281 + unsigned int read_req = 0; 282 + unsigned int rxfifo_thres = 31; 283 + unsigned int write_req = 0; 284 + unsigned int ld_ctr1 = 0; 285 + 286 + writel_relaxed(spicc->tx_dma, spicc->base + SPICC_DRADDR); 287 + writel_relaxed(spicc->rx_dma, spicc->base + SPICC_DWADDR); 288 + 289 + /* Set the max burst length to support a transmission with length of 290 + * no more than 1024 bytes(128 words), which must use the CS management 291 + * because of some strict timing requirements 292 + */ 293 + writel_bits_relaxed(SPICC_BURSTLENGTH_MASK, SPICC_BURSTLENGTH_MASK, 294 + spicc->base + SPICC_CONREG); 295 + 296 + len = meson_spicc_calc_dma_len(spicc, spicc->xfer_remain, 297 + &dma_burst_len); 298 + spicc->xfer_remain -= len; 299 + dma_burst_count = DIV_ROUND_UP(len, dma_burst_len); 300 + dma_burst_len--; 301 + 302 + if (spicc->tx_dma) { 303 + spicc->tx_dma += len; 304 + count_en |= DMA_READ_COUNTER_EN; 305 + txfifo_thres = spicc->data->fifo_size - dma_burst_len; 306 + read_req = dma_burst_len; 307 + ld_ctr1 |= FIELD_PREP(DMA_READ_COUNTER, dma_burst_count); 308 + } 309 + 310 + if (spicc->rx_dma) { 311 + spicc->rx_dma += len; 312 + count_en |= DMA_WRITE_COUNTER_EN; 313 + rxfifo_thres = dma_burst_len; 314 + write_req = dma_burst_len; 315 + ld_ctr1 |= FIELD_PREP(DMA_WRITE_COUNTER, dma_burst_count); 316 + } 317 + 318 + writel_relaxed(count_en, spicc->base + SPICC_LD_CNTL0); 319 + writel_relaxed(ld_ctr1, spicc->base + SPICC_LD_CNTL1); 320 + writel_relaxed(SPICC_DMA_ENABLE 321 + | SPICC_DMA_URGENT 322 + | FIELD_PREP(SPICC_TXFIFO_THRESHOLD_MASK, txfifo_thres) 323 + | FIELD_PREP(SPICC_READ_BURST_MASK, read_req) 324 + | FIELD_PREP(SPICC_RXFIFO_THRESHOLD_MASK, rxfifo_thres) 325 + | FIELD_PREP(SPICC_WRITE_BURST_MASK, write_req), 326 + spicc->base + SPICC_DMAREG); 327 + } 328 + 329 + static irqreturn_t meson_spicc_dma_irq(struct meson_spicc_device *spicc) 330 + { 331 + if (readl_relaxed(spicc->base + SPICC_DMAREG) & SPICC_DMA_ENABLE) 332 + return IRQ_HANDLED; 333 + 334 + if (spicc->xfer_remain) { 335 + meson_spicc_setup_dma(spicc); 336 + } else { 337 + writel_bits_relaxed(SPICC_SMC, 0, spicc->base + SPICC_CONREG); 338 + writel_relaxed(0, spicc->base + SPICC_INTREG); 339 + writel_relaxed(0, spicc->base + SPICC_DMAREG); 340 + meson_spicc_dma_unmap(spicc, spicc->xfer); 341 + complete(&spicc->done); 342 + } 343 + 344 + return IRQ_HANDLED; 231 345 } 232 346 233 347 static inline bool meson_spicc_txfull(struct meson_spicc_device *spicc) ··· 462 292 struct meson_spicc_device *spicc = (void *) data; 463 293 464 294 writel_bits_relaxed(SPICC_TC, SPICC_TC, spicc->base + SPICC_STATREG); 295 + 296 + if (spicc->using_dma) 297 + return meson_spicc_dma_irq(spicc); 465 298 466 299 /* Empty RX FIFO */ 467 300 meson_spicc_rx(spicc); ··· 599 426 600 427 meson_spicc_reset_fifo(spicc); 601 428 602 - /* Setup burst */ 603 - meson_spicc_setup_burst(spicc); 604 - 605 429 /* Setup wait for completion */ 606 430 reinit_completion(&spicc->done); 607 431 ··· 612 442 /* Increase it twice and add 200 ms tolerance */ 613 443 timeout += timeout + 200; 614 444 615 - /* Start burst */ 616 - writel_bits_relaxed(SPICC_XCH, SPICC_XCH, spicc->base + SPICC_CONREG); 445 + if (xfer->bits_per_word == 64) { 446 + int ret; 617 447 618 - /* Enable interrupts */ 619 - writel_relaxed(SPICC_TC_EN, spicc->base + SPICC_INTREG); 448 + /* dma_burst_len 1 can't trigger a dma burst */ 449 + if (xfer->len < 16) 450 + return -EINVAL; 451 + 452 + ret = meson_spicc_dma_map(spicc, xfer); 453 + if (ret) { 454 + meson_spicc_dma_unmap(spicc, xfer); 455 + dev_err(host->dev.parent, "dma map failed\n"); 456 + return ret; 457 + } 458 + 459 + spicc->using_dma = true; 460 + spicc->xfer_remain = DIV_ROUND_UP(xfer->len, spicc->bytes_per_word); 461 + meson_spicc_setup_dma(spicc); 462 + writel_relaxed(SPICC_TE_EN, spicc->base + SPICC_INTREG); 463 + writel_bits_relaxed(SPICC_SMC, SPICC_SMC, spicc->base + SPICC_CONREG); 464 + } else { 465 + spicc->using_dma = false; 466 + /* Setup burst */ 467 + meson_spicc_setup_burst(spicc); 468 + 469 + /* Start burst */ 470 + writel_bits_relaxed(SPICC_XCH, SPICC_XCH, spicc->base + SPICC_CONREG); 471 + 472 + /* Enable interrupts */ 473 + writel_relaxed(SPICC_TC_EN, spicc->base + SPICC_INTREG); 474 + } 620 475 621 476 if (!wait_for_completion_timeout(&spicc->done, msecs_to_jiffies(timeout))) 622 477 return -ETIMEDOUT; ··· 739 544 { 740 545 if (!spi->controller_state) 741 546 spi->controller_state = spi_controller_get_devdata(spi->controller); 547 + 548 + /* DMA works at 64 bits, the rest works on PIO */ 549 + if (spi->bits_per_word != 8 && 550 + spi->bits_per_word != 16 && 551 + spi->bits_per_word != 24 && 552 + spi->bits_per_word != 32 && 553 + spi->bits_per_word != 64) 554 + return -EINVAL; 742 555 743 556 return 0; 744 557 } ··· 1056 853 host->num_chipselect = 4; 1057 854 host->dev.of_node = pdev->dev.of_node; 1058 855 host->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LOOP; 1059 - host->bits_per_word_mask = SPI_BPW_MASK(32) | 1060 - SPI_BPW_MASK(24) | 1061 - SPI_BPW_MASK(16) | 1062 - SPI_BPW_MASK(8); 1063 856 host->flags = (SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX); 1064 857 host->min_speed_hz = spicc->data->min_speed_hz; 1065 858 host->max_speed_hz = spicc->data->max_speed_hz;