1 // SPDX-License-Identifier: GPL-2.0-only
3 * Designware SPI core controller driver (refer pxa2xx_spi.c)
5 * Copyright (c) 2009, Intel Corporation.
8 #include <linux/dma-mapping.h>
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/highmem.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <linux/spi/spi.h>
18 #ifdef CONFIG_DEBUG_FS
19 #include <linux/debugfs.h>
22 /* Slave spi_dev related */
24 u8 tmode; /* TR/TO/RO/EEPROM */
25 u8 type; /* SPI/SSP/MicroWire */
27 u16 clk_div; /* baud rate divider */
28 u32 speed_hz; /* baud rate */
31 #ifdef CONFIG_DEBUG_FS
33 #define DW_SPI_DBGFS_REG(_name, _off) \
39 static const struct debugfs_reg32 dw_spi_dbgfs_regs[] = {
40 DW_SPI_DBGFS_REG("CTRLR0", DW_SPI_CTRLR0),
41 DW_SPI_DBGFS_REG("CTRLR1", DW_SPI_CTRLR1),
42 DW_SPI_DBGFS_REG("SSIENR", DW_SPI_SSIENR),
43 DW_SPI_DBGFS_REG("SER", DW_SPI_SER),
44 DW_SPI_DBGFS_REG("BAUDR", DW_SPI_BAUDR),
45 DW_SPI_DBGFS_REG("TXFTLR", DW_SPI_TXFTLR),
46 DW_SPI_DBGFS_REG("RXFTLR", DW_SPI_RXFTLR),
47 DW_SPI_DBGFS_REG("TXFLR", DW_SPI_TXFLR),
48 DW_SPI_DBGFS_REG("RXFLR", DW_SPI_RXFLR),
49 DW_SPI_DBGFS_REG("SR", DW_SPI_SR),
50 DW_SPI_DBGFS_REG("IMR", DW_SPI_IMR),
51 DW_SPI_DBGFS_REG("ISR", DW_SPI_ISR),
52 DW_SPI_DBGFS_REG("DMACR", DW_SPI_DMACR),
53 DW_SPI_DBGFS_REG("DMATDLR", DW_SPI_DMATDLR),
54 DW_SPI_DBGFS_REG("DMARDLR", DW_SPI_DMARDLR),
57 static int dw_spi_debugfs_init(struct dw_spi *dws)
61 snprintf(name, 32, "dw_spi%d", dws->master->bus_num);
62 dws->debugfs = debugfs_create_dir(name, NULL);
66 dws->regset.regs = dw_spi_dbgfs_regs;
67 dws->regset.nregs = ARRAY_SIZE(dw_spi_dbgfs_regs);
68 dws->regset.base = dws->regs;
69 debugfs_create_regset32("registers", 0400, dws->debugfs, &dws->regset);
74 static void dw_spi_debugfs_remove(struct dw_spi *dws)
76 debugfs_remove_recursive(dws->debugfs);
80 static inline int dw_spi_debugfs_init(struct dw_spi *dws)
85 static inline void dw_spi_debugfs_remove(struct dw_spi *dws)
88 #endif /* CONFIG_DEBUG_FS */
90 void dw_spi_set_cs(struct spi_device *spi, bool enable)
92 struct dw_spi *dws = spi_controller_get_devdata(spi->controller);
93 bool cs_high = !!(spi->mode & SPI_CS_HIGH);
96 * DW SPI controller demands any native CS being set in order to
97 * proceed with data transfer. So in order to activate the SPI
98 * communications we must set a corresponding bit in the Slave
99 * Enable register no matter whether the SPI core is configured to
100 * support active-high or active-low CS level.
102 if (cs_high == enable)
103 dw_writel(dws, DW_SPI_SER, BIT(spi->chip_select));
104 else if (dws->cs_override)
105 dw_writel(dws, DW_SPI_SER, 0);
107 EXPORT_SYMBOL_GPL(dw_spi_set_cs);
109 /* Return the max entries we can fill into tx fifo */
110 static inline u32 tx_max(struct dw_spi *dws)
112 u32 tx_left, tx_room, rxtx_gap;
114 tx_left = (dws->tx_end - dws->tx) / dws->n_bytes;
115 tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR);
118 * Another concern is about the tx/rx mismatch, we
119 * though to use (dws->fifo_len - rxflr - txflr) as
120 * one maximum value for tx, but it doesn't cover the
121 * data which is out of tx/rx fifo and inside the
122 * shift registers. So a control from sw point of
125 rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx))
128 return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap));
131 /* Return the max entries we should read out of rx fifo */
132 static inline u32 rx_max(struct dw_spi *dws)
134 u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes;
136 return min_t(u32, rx_left, dw_readl(dws, DW_SPI_RXFLR));
139 static void dw_writer(struct dw_spi *dws)
144 spin_lock(&dws->buf_lock);
147 /* Set the tx word if the transfer's original "tx" is not null */
148 if (dws->tx_end - dws->len) {
149 if (dws->n_bytes == 1)
150 txw = *(u8 *)(dws->tx);
152 txw = *(u16 *)(dws->tx);
154 dw_write_io_reg(dws, DW_SPI_DR, txw);
155 dws->tx += dws->n_bytes;
157 spin_unlock(&dws->buf_lock);
160 static void dw_reader(struct dw_spi *dws)
165 spin_lock(&dws->buf_lock);
168 rxw = dw_read_io_reg(dws, DW_SPI_DR);
169 /* Care rx only if the transfer's original "rx" is not null */
170 if (dws->rx_end - dws->len) {
171 if (dws->n_bytes == 1)
172 *(u8 *)(dws->rx) = rxw;
174 *(u16 *)(dws->rx) = rxw;
176 dws->rx += dws->n_bytes;
178 spin_unlock(&dws->buf_lock);
181 static void int_error_stop(struct dw_spi *dws, const char *msg)
185 dev_err(&dws->master->dev, "%s\n", msg);
186 dws->master->cur_msg->status = -EIO;
187 spi_finalize_current_transfer(dws->master);
190 static irqreturn_t interrupt_transfer(struct dw_spi *dws)
192 u16 irq_status = dw_readl(dws, DW_SPI_ISR);
195 if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) {
196 dw_readl(dws, DW_SPI_ICR);
197 int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun");
202 if (dws->rx_end == dws->rx) {
203 spi_mask_intr(dws, SPI_INT_TXEI);
204 spi_finalize_current_transfer(dws->master);
207 if (irq_status & SPI_INT_TXEI) {
208 spi_mask_intr(dws, SPI_INT_TXEI);
210 /* Enable TX irq always, it will be disabled when RX finished */
211 spi_umask_intr(dws, SPI_INT_TXEI);
217 static irqreturn_t dw_spi_irq(int irq, void *dev_id)
219 struct spi_controller *master = dev_id;
220 struct dw_spi *dws = spi_controller_get_devdata(master);
221 u16 irq_status = dw_readl(dws, DW_SPI_ISR) & 0x3f;
226 if (!master->cur_msg) {
227 spi_mask_intr(dws, SPI_INT_TXEI);
231 return dws->transfer_handler(dws);
234 /* Configure CTRLR0 for DW_apb_ssi */
235 u32 dw_spi_update_cr0(struct spi_controller *master, struct spi_device *spi,
236 struct spi_transfer *transfer)
238 struct chip_data *chip = spi_get_ctldata(spi);
241 /* Default SPI mode is SCPOL = 0, SCPH = 0 */
242 cr0 = (transfer->bits_per_word - 1)
243 | (chip->type << SPI_FRF_OFFSET)
244 | ((((spi->mode & SPI_CPOL) ? 1 : 0) << SPI_SCOL_OFFSET) |
245 (((spi->mode & SPI_CPHA) ? 1 : 0) << SPI_SCPH_OFFSET) |
246 (((spi->mode & SPI_LOOP) ? 1 : 0) << SPI_SRL_OFFSET))
247 | (chip->tmode << SPI_TMOD_OFFSET);
251 EXPORT_SYMBOL_GPL(dw_spi_update_cr0);
253 /* Configure CTRLR0 for DWC_ssi */
254 u32 dw_spi_update_cr0_v1_01a(struct spi_controller *master,
255 struct spi_device *spi,
256 struct spi_transfer *transfer)
258 struct chip_data *chip = spi_get_ctldata(spi);
261 /* CTRLR0[ 4: 0] Data Frame Size */
262 cr0 = (transfer->bits_per_word - 1);
264 /* CTRLR0[ 7: 6] Frame Format */
265 cr0 |= chip->type << DWC_SSI_CTRLR0_FRF_OFFSET;
268 * SPI mode (SCPOL|SCPH)
269 * CTRLR0[ 8] Serial Clock Phase
270 * CTRLR0[ 9] Serial Clock Polarity
272 cr0 |= ((spi->mode & SPI_CPOL) ? 1 : 0) << DWC_SSI_CTRLR0_SCPOL_OFFSET;
273 cr0 |= ((spi->mode & SPI_CPHA) ? 1 : 0) << DWC_SSI_CTRLR0_SCPH_OFFSET;
275 /* CTRLR0[11:10] Transfer Mode */
276 cr0 |= chip->tmode << DWC_SSI_CTRLR0_TMOD_OFFSET;
278 /* CTRLR0[13] Shift Register Loop */
279 cr0 |= ((spi->mode & SPI_LOOP) ? 1 : 0) << DWC_SSI_CTRLR0_SRL_OFFSET;
283 EXPORT_SYMBOL_GPL(dw_spi_update_cr0_v1_01a);
285 static int dw_spi_transfer_one(struct spi_controller *master,
286 struct spi_device *spi, struct spi_transfer *transfer)
288 struct dw_spi *dws = spi_controller_get_devdata(master);
289 struct chip_data *chip = spi_get_ctldata(spi);
297 spin_lock_irqsave(&dws->buf_lock, flags);
298 dws->tx = (void *)transfer->tx_buf;
299 dws->tx_end = dws->tx + transfer->len;
300 dws->rx = transfer->rx_buf;
301 dws->rx_end = dws->rx + transfer->len;
302 dws->len = transfer->len;
303 spin_unlock_irqrestore(&dws->buf_lock, flags);
305 /* Ensure dw->rx and dw->rx_end are visible */
308 spi_enable_chip(dws, 0);
310 /* Handle per transfer options for bpw and speed */
311 if (transfer->speed_hz != dws->current_freq) {
312 if (transfer->speed_hz != chip->speed_hz) {
313 /* clk_div doesn't support odd number */
314 chip->clk_div = (DIV_ROUND_UP(dws->max_freq, transfer->speed_hz) + 1) & 0xfffe;
315 chip->speed_hz = transfer->speed_hz;
317 dws->current_freq = transfer->speed_hz;
318 spi_set_clk(dws, chip->clk_div);
321 transfer->effective_speed_hz = dws->max_freq / chip->clk_div;
322 dws->n_bytes = DIV_ROUND_UP(transfer->bits_per_word, BITS_PER_BYTE);
324 cr0 = dws->update_cr0(master, spi, transfer);
325 dw_writel(dws, DW_SPI_CTRLR0, cr0);
327 /* Check if current transfer is a DMA transaction */
328 if (master->can_dma && master->can_dma(master, spi, transfer))
329 dws->dma_mapped = master->cur_msg_mapped;
331 /* For poll mode just disable all interrupts */
332 spi_mask_intr(dws, 0xff);
336 * we only need set the TXEI IRQ, as TX/RX always happen syncronizely
338 if (dws->dma_mapped) {
339 ret = dws->dma_ops->dma_setup(dws, transfer);
341 spi_enable_chip(dws, 1);
345 txlevel = min_t(u16, dws->fifo_len / 2, dws->len / dws->n_bytes);
346 dw_writel(dws, DW_SPI_TXFTLR, txlevel);
348 /* Set the interrupt mask */
349 imask |= SPI_INT_TXEI | SPI_INT_TXOI |
350 SPI_INT_RXUI | SPI_INT_RXOI;
351 spi_umask_intr(dws, imask);
353 dws->transfer_handler = interrupt_transfer;
356 spi_enable_chip(dws, 1);
359 return dws->dma_ops->dma_transfer(dws, transfer);
364 static void dw_spi_handle_err(struct spi_controller *master,
365 struct spi_message *msg)
367 struct dw_spi *dws = spi_controller_get_devdata(master);
370 dws->dma_ops->dma_stop(dws);
375 /* This may be called twice for each spi dev */
376 static int dw_spi_setup(struct spi_device *spi)
378 struct chip_data *chip;
380 /* Only alloc on first setup */
381 chip = spi_get_ctldata(spi);
383 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
386 spi_set_ctldata(spi, chip);
389 chip->tmode = SPI_TMOD_TR;
394 static void dw_spi_cleanup(struct spi_device *spi)
396 struct chip_data *chip = spi_get_ctldata(spi);
399 spi_set_ctldata(spi, NULL);
402 /* Restart the controller, disable all interrupts, clean rx fifo */
403 static void spi_hw_init(struct device *dev, struct dw_spi *dws)
408 * Try to detect the FIFO depth if not set by interface driver,
409 * the depth could be from 2 to 256 from HW spec
411 if (!dws->fifo_len) {
414 for (fifo = 1; fifo < 256; fifo++) {
415 dw_writel(dws, DW_SPI_TXFTLR, fifo);
416 if (fifo != dw_readl(dws, DW_SPI_TXFTLR))
419 dw_writel(dws, DW_SPI_TXFTLR, 0);
421 dws->fifo_len = (fifo == 1) ? 0 : fifo;
422 dev_dbg(dev, "Detected FIFO size: %u bytes\n", dws->fifo_len);
425 /* enable HW fixup for explicit CS deselect for Amazon's alpine chip */
426 if (dws->cs_override)
427 dw_writel(dws, DW_SPI_CS_OVERRIDE, 0xF);
430 int dw_spi_add_host(struct device *dev, struct dw_spi *dws)
432 struct spi_controller *master;
438 master = spi_alloc_master(dev, 0);
442 dws->master = master;
443 dws->type = SSI_MOTO_SPI;
444 dws->dma_addr = (dma_addr_t)(dws->paddr + DW_SPI_DR);
445 spin_lock_init(&dws->buf_lock);
447 spi_controller_set_devdata(master, dws);
449 ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED, dev_name(dev),
452 dev_err(dev, "can not get IRQ\n");
453 goto err_free_master;
456 master->use_gpio_descriptors = true;
457 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
458 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
459 master->bus_num = dws->bus_num;
460 master->num_chipselect = dws->num_cs;
461 master->setup = dw_spi_setup;
462 master->cleanup = dw_spi_cleanup;
463 master->set_cs = dw_spi_set_cs;
464 master->transfer_one = dw_spi_transfer_one;
465 master->handle_err = dw_spi_handle_err;
466 master->max_speed_hz = dws->max_freq;
467 master->dev.of_node = dev->of_node;
468 master->dev.fwnode = dev->fwnode;
469 master->flags = SPI_MASTER_GPIO_SS;
470 master->auto_runtime_pm = true;
473 master->set_cs = dws->set_cs;
476 spi_hw_init(dev, dws);
478 if (dws->dma_ops && dws->dma_ops->dma_init) {
479 ret = dws->dma_ops->dma_init(dev, dws);
481 dev_warn(dev, "DMA init failed\n");
483 master->can_dma = dws->dma_ops->can_dma;
484 master->flags |= SPI_CONTROLLER_MUST_TX;
488 ret = spi_register_controller(master);
490 dev_err(&master->dev, "problem registering spi master\n");
494 dw_spi_debugfs_init(dws);
498 if (dws->dma_ops && dws->dma_ops->dma_exit)
499 dws->dma_ops->dma_exit(dws);
500 spi_enable_chip(dws, 0);
501 free_irq(dws->irq, master);
503 spi_controller_put(master);
506 EXPORT_SYMBOL_GPL(dw_spi_add_host);
508 void dw_spi_remove_host(struct dw_spi *dws)
510 dw_spi_debugfs_remove(dws);
512 spi_unregister_controller(dws->master);
514 if (dws->dma_ops && dws->dma_ops->dma_exit)
515 dws->dma_ops->dma_exit(dws);
517 spi_shutdown_chip(dws);
519 free_irq(dws->irq, dws->master);
521 EXPORT_SYMBOL_GPL(dw_spi_remove_host);
523 int dw_spi_suspend_host(struct dw_spi *dws)
527 ret = spi_controller_suspend(dws->master);
531 spi_shutdown_chip(dws);
534 EXPORT_SYMBOL_GPL(dw_spi_suspend_host);
536 int dw_spi_resume_host(struct dw_spi *dws)
538 spi_hw_init(&dws->master->dev, dws);
539 return spi_controller_resume(dws->master);
541 EXPORT_SYMBOL_GPL(dw_spi_resume_host);
544 MODULE_DESCRIPTION("Driver for DesignWare SPI controller core");
545 MODULE_LICENSE("GPL v2");