1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
3 // Copyright (C) 2008 Juergen Beisert
6 #include <linux/completion.h>
7 #include <linux/delay.h>
8 #include <linux/dmaengine.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/err.h>
11 #include <linux/interrupt.h>
13 #include <linux/irq.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 #include <linux/spi/spi.h>
21 #include <linux/types.h>
23 #include <linux/of_device.h>
24 #include <linux/property.h>
26 #include <linux/dma/imx-dma.h>
28 #define DRIVER_NAME "spi_imx"
30 static bool use_dma = true;
31 module_param(use_dma, bool, 0644);
32 MODULE_PARM_DESC(use_dma, "Enable usage of DMA when available (default)");
34 /* define polling limits */
35 static unsigned int polling_limit_us = 30;
36 module_param(polling_limit_us, uint, 0664);
37 MODULE_PARM_DESC(polling_limit_us,
38 "time in us to run a transfer in polling mode\n");
40 #define MXC_RPM_TIMEOUT 2000 /* 2000ms */
42 #define MXC_CSPIRXDATA 0x00
43 #define MXC_CSPITXDATA 0x04
44 #define MXC_CSPICTRL 0x08
45 #define MXC_CSPIINT 0x0c
46 #define MXC_RESET 0x1c
48 /* generic defines to abstract from the different register layouts */
49 #define MXC_INT_RR (1 << 0) /* Receive data ready interrupt */
50 #define MXC_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */
51 #define MXC_INT_RDR BIT(4) /* Receive date threshold interrupt */
53 /* The maximum bytes that a sdma BD can transfer. */
54 #define MAX_SDMA_BD_BYTES (1 << 15)
55 #define MX51_ECSPI_CTRL_MAX_BURST 512
56 /* The maximum bytes that IMX53_ECSPI can transfer in slave mode.*/
57 #define MX53_MAX_TRANSFER_BYTES 512
59 enum spi_imx_devtype {
64 IMX35_CSPI, /* CSPI on all i.mx except above */
65 IMX51_ECSPI, /* ECSPI on i.mx51 */
66 IMX53_ECSPI, /* ECSPI on i.mx53 and later */
71 struct spi_imx_devtype_data {
72 void (*intctrl)(struct spi_imx_data *spi_imx, int enable);
73 int (*prepare_message)(struct spi_imx_data *spi_imx, struct spi_message *msg);
74 int (*prepare_transfer)(struct spi_imx_data *spi_imx, struct spi_device *spi);
75 void (*trigger)(struct spi_imx_data *spi_imx);
76 int (*rx_available)(struct spi_imx_data *spi_imx);
77 void (*reset)(struct spi_imx_data *spi_imx);
78 void (*setup_wml)(struct spi_imx_data *spi_imx);
79 void (*disable)(struct spi_imx_data *spi_imx);
80 void (*disable_dma)(struct spi_imx_data *spi_imx);
83 unsigned int fifo_size;
86 * ERR009165 fixed or not:
87 * https://www.nxp.com/docs/en/errata/IMX6DQCE.pdf
90 enum spi_imx_devtype devtype;
94 struct spi_controller *controller;
97 struct completion xfer_done;
99 unsigned long base_phys;
103 unsigned long spi_clk;
104 unsigned int spi_bus_clk;
106 unsigned int bits_per_word;
107 unsigned int spi_drctl;
109 unsigned int count, remainder;
110 void (*tx)(struct spi_imx_data *spi_imx);
111 void (*rx)(struct spi_imx_data *spi_imx);
114 unsigned int txfifo; /* number of words pushed in tx FIFO */
115 unsigned int dynamic_burst;
121 unsigned int slave_burst;
126 struct completion dma_rx_completion;
127 struct completion dma_tx_completion;
129 const struct spi_imx_devtype_data *devtype_data;
132 static inline int is_imx27_cspi(struct spi_imx_data *d)
134 return d->devtype_data->devtype == IMX27_CSPI;
137 static inline int is_imx35_cspi(struct spi_imx_data *d)
139 return d->devtype_data->devtype == IMX35_CSPI;
142 static inline int is_imx51_ecspi(struct spi_imx_data *d)
144 return d->devtype_data->devtype == IMX51_ECSPI;
147 static inline int is_imx53_ecspi(struct spi_imx_data *d)
149 return d->devtype_data->devtype == IMX53_ECSPI;
152 #define MXC_SPI_BUF_RX(type) \
153 static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \
155 unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \
157 if (spi_imx->rx_buf) { \
158 *(type *)spi_imx->rx_buf = val; \
159 spi_imx->rx_buf += sizeof(type); \
162 spi_imx->remainder -= sizeof(type); \
165 #define MXC_SPI_BUF_TX(type) \
166 static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \
170 if (spi_imx->tx_buf) { \
171 val = *(type *)spi_imx->tx_buf; \
172 spi_imx->tx_buf += sizeof(type); \
175 spi_imx->count -= sizeof(type); \
177 writel(val, spi_imx->base + MXC_CSPITXDATA); \
187 /* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
188 * (which is currently not the case in this driver)
190 static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
191 256, 384, 512, 768, 1024};
194 static unsigned int spi_imx_clkdiv_1(unsigned int fin,
195 unsigned int fspi, unsigned int max, unsigned int *fres)
199 for (i = 2; i < max; i++)
200 if (fspi * mxc_clkdivs[i] >= fin)
203 *fres = fin / mxc_clkdivs[i];
207 /* MX1, MX31, MX35, MX51 CSPI */
208 static unsigned int spi_imx_clkdiv_2(unsigned int fin,
209 unsigned int fspi, unsigned int *fres)
213 for (i = 0; i < 7; i++) {
214 if (fspi * div >= fin)
224 static int spi_imx_bytes_per_word(const int bits_per_word)
226 if (bits_per_word <= 8)
228 else if (bits_per_word <= 16)
234 static bool spi_imx_can_dma(struct spi_controller *controller, struct spi_device *spi,
235 struct spi_transfer *transfer)
237 struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
239 if (!use_dma || controller->fallback)
242 if (!controller->dma_rx)
245 if (spi_imx->slave_mode)
248 if (transfer->len < spi_imx->devtype_data->fifo_size)
251 spi_imx->dynamic_burst = 0;
256 #define MX51_ECSPI_CTRL 0x08
257 #define MX51_ECSPI_CTRL_ENABLE (1 << 0)
258 #define MX51_ECSPI_CTRL_XCH (1 << 2)
259 #define MX51_ECSPI_CTRL_SMC (1 << 3)
260 #define MX51_ECSPI_CTRL_MODE_MASK (0xf << 4)
261 #define MX51_ECSPI_CTRL_DRCTL(drctl) ((drctl) << 16)
262 #define MX51_ECSPI_CTRL_POSTDIV_OFFSET 8
263 #define MX51_ECSPI_CTRL_PREDIV_OFFSET 12
264 #define MX51_ECSPI_CTRL_CS(cs) ((cs) << 18)
265 #define MX51_ECSPI_CTRL_BL_OFFSET 20
266 #define MX51_ECSPI_CTRL_BL_MASK (0xfff << 20)
268 #define MX51_ECSPI_CONFIG 0x0c
269 #define MX51_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs) + 0))
270 #define MX51_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs) + 4))
271 #define MX51_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs) + 8))
272 #define MX51_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs) + 12))
273 #define MX51_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs) + 20))
275 #define MX51_ECSPI_INT 0x10
276 #define MX51_ECSPI_INT_TEEN (1 << 0)
277 #define MX51_ECSPI_INT_RREN (1 << 3)
278 #define MX51_ECSPI_INT_RDREN (1 << 4)
280 #define MX51_ECSPI_DMA 0x14
281 #define MX51_ECSPI_DMA_TX_WML(wml) ((wml) & 0x3f)
282 #define MX51_ECSPI_DMA_RX_WML(wml) (((wml) & 0x3f) << 16)
283 #define MX51_ECSPI_DMA_RXT_WML(wml) (((wml) & 0x3f) << 24)
285 #define MX51_ECSPI_DMA_TEDEN (1 << 7)
286 #define MX51_ECSPI_DMA_RXDEN (1 << 23)
287 #define MX51_ECSPI_DMA_RXTDEN (1 << 31)
289 #define MX51_ECSPI_STAT 0x18
290 #define MX51_ECSPI_STAT_RR (1 << 3)
292 #define MX51_ECSPI_TESTREG 0x20
293 #define MX51_ECSPI_TESTREG_LBC BIT(31)
295 static void spi_imx_buf_rx_swap_u32(struct spi_imx_data *spi_imx)
297 unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA);
299 if (spi_imx->rx_buf) {
300 #ifdef __LITTLE_ENDIAN
301 unsigned int bytes_per_word;
303 bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
304 if (bytes_per_word == 1)
306 else if (bytes_per_word == 2)
309 *(u32 *)spi_imx->rx_buf = val;
310 spi_imx->rx_buf += sizeof(u32);
313 spi_imx->remainder -= sizeof(u32);
316 static void spi_imx_buf_rx_swap(struct spi_imx_data *spi_imx)
321 unaligned = spi_imx->remainder % 4;
324 spi_imx_buf_rx_swap_u32(spi_imx);
328 if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) {
329 spi_imx_buf_rx_u16(spi_imx);
333 val = readl(spi_imx->base + MXC_CSPIRXDATA);
335 while (unaligned--) {
336 if (spi_imx->rx_buf) {
337 *(u8 *)spi_imx->rx_buf = (val >> (8 * unaligned)) & 0xff;
340 spi_imx->remainder--;
344 static void spi_imx_buf_tx_swap_u32(struct spi_imx_data *spi_imx)
347 #ifdef __LITTLE_ENDIAN
348 unsigned int bytes_per_word;
351 if (spi_imx->tx_buf) {
352 val = *(u32 *)spi_imx->tx_buf;
353 spi_imx->tx_buf += sizeof(u32);
356 spi_imx->count -= sizeof(u32);
357 #ifdef __LITTLE_ENDIAN
358 bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
360 if (bytes_per_word == 1)
362 else if (bytes_per_word == 2)
365 writel(val, spi_imx->base + MXC_CSPITXDATA);
368 static void spi_imx_buf_tx_swap(struct spi_imx_data *spi_imx)
373 unaligned = spi_imx->count % 4;
376 spi_imx_buf_tx_swap_u32(spi_imx);
380 if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) {
381 spi_imx_buf_tx_u16(spi_imx);
385 while (unaligned--) {
386 if (spi_imx->tx_buf) {
387 val |= *(u8 *)spi_imx->tx_buf << (8 * unaligned);
393 writel(val, spi_imx->base + MXC_CSPITXDATA);
396 static void mx53_ecspi_rx_slave(struct spi_imx_data *spi_imx)
398 u32 val = be32_to_cpu(readl(spi_imx->base + MXC_CSPIRXDATA));
400 if (spi_imx->rx_buf) {
401 int n_bytes = spi_imx->slave_burst % sizeof(val);
404 n_bytes = sizeof(val);
406 memcpy(spi_imx->rx_buf,
407 ((u8 *)&val) + sizeof(val) - n_bytes, n_bytes);
409 spi_imx->rx_buf += n_bytes;
410 spi_imx->slave_burst -= n_bytes;
413 spi_imx->remainder -= sizeof(u32);
416 static void mx53_ecspi_tx_slave(struct spi_imx_data *spi_imx)
419 int n_bytes = spi_imx->count % sizeof(val);
422 n_bytes = sizeof(val);
424 if (spi_imx->tx_buf) {
425 memcpy(((u8 *)&val) + sizeof(val) - n_bytes,
426 spi_imx->tx_buf, n_bytes);
427 val = cpu_to_be32(val);
428 spi_imx->tx_buf += n_bytes;
431 spi_imx->count -= n_bytes;
433 writel(val, spi_imx->base + MXC_CSPITXDATA);
437 static unsigned int mx51_ecspi_clkdiv(struct spi_imx_data *spi_imx,
438 unsigned int fspi, unsigned int *fres)
441 * there are two 4-bit dividers, the pre-divider divides by
442 * $pre, the post-divider by 2^$post
444 unsigned int pre, post;
445 unsigned int fin = spi_imx->spi_clk;
447 if (unlikely(fspi > fin))
450 post = fls(fin) - fls(fspi);
451 if (fin > fspi << post)
454 /* now we have: (fin <= fspi << post) with post being minimal */
456 post = max(4U, post) - 4;
457 if (unlikely(post > 0xf)) {
458 dev_err(spi_imx->dev, "cannot set clock freq: %u (base freq: %u)\n",
463 pre = DIV_ROUND_UP(fin, fspi << post) - 1;
465 dev_dbg(spi_imx->dev, "%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
466 __func__, fin, fspi, post, pre);
468 /* Resulting frequency for the SCLK line. */
469 *fres = (fin / (pre + 1)) >> post;
471 return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) |
472 (post << MX51_ECSPI_CTRL_POSTDIV_OFFSET);
475 static void mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable)
477 unsigned int val = 0;
479 if (enable & MXC_INT_TE)
480 val |= MX51_ECSPI_INT_TEEN;
482 if (enable & MXC_INT_RR)
483 val |= MX51_ECSPI_INT_RREN;
485 if (enable & MXC_INT_RDR)
486 val |= MX51_ECSPI_INT_RDREN;
488 writel(val, spi_imx->base + MX51_ECSPI_INT);
491 static void mx51_ecspi_trigger(struct spi_imx_data *spi_imx)
495 reg = readl(spi_imx->base + MX51_ECSPI_CTRL);
496 reg |= MX51_ECSPI_CTRL_XCH;
497 writel(reg, spi_imx->base + MX51_ECSPI_CTRL);
500 static void mx51_disable_dma(struct spi_imx_data *spi_imx)
502 writel(0, spi_imx->base + MX51_ECSPI_DMA);
505 static void mx51_ecspi_disable(struct spi_imx_data *spi_imx)
509 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
510 ctrl &= ~MX51_ECSPI_CTRL_ENABLE;
511 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
514 static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx,
515 struct spi_message *msg)
517 struct spi_device *spi = msg->spi;
518 struct spi_transfer *xfer;
519 u32 ctrl = MX51_ECSPI_CTRL_ENABLE;
520 u32 min_speed_hz = ~0U;
522 u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
523 u32 current_cfg = cfg;
525 /* set Master or Slave mode */
526 if (spi_imx->slave_mode)
527 ctrl &= ~MX51_ECSPI_CTRL_MODE_MASK;
529 ctrl |= MX51_ECSPI_CTRL_MODE_MASK;
532 * Enable SPI_RDY handling (falling edge/level triggered).
534 if (spi->mode & SPI_READY)
535 ctrl |= MX51_ECSPI_CTRL_DRCTL(spi_imx->spi_drctl);
537 /* set chip select to use */
538 ctrl |= MX51_ECSPI_CTRL_CS(spi->chip_select);
541 * The ctrl register must be written first, with the EN bit set other
542 * registers must not be written to.
544 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
546 testreg = readl(spi_imx->base + MX51_ECSPI_TESTREG);
547 if (spi->mode & SPI_LOOP)
548 testreg |= MX51_ECSPI_TESTREG_LBC;
550 testreg &= ~MX51_ECSPI_TESTREG_LBC;
551 writel(testreg, spi_imx->base + MX51_ECSPI_TESTREG);
554 * eCSPI burst completion by Chip Select signal in Slave mode
555 * is not functional for imx53 Soc, config SPI burst completed when
556 * BURST_LENGTH + 1 bits are received
558 if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx))
559 cfg &= ~MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
561 cfg |= MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
563 if (spi->mode & SPI_CPOL) {
564 cfg |= MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
565 cfg |= MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
567 cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
568 cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
571 if (spi->mode & SPI_CS_HIGH)
572 cfg |= MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
574 cfg &= ~MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
576 if (cfg == current_cfg)
579 writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
582 * Wait until the changes in the configuration register CONFIGREG
583 * propagate into the hardware. It takes exactly one tick of the
584 * SCLK clock, but we will wait two SCLK clock just to be sure. The
585 * effect of the delay it takes for the hardware to apply changes
586 * is noticable if the SCLK clock run very slow. In such a case, if
587 * the polarity of SCLK should be inverted, the GPIO ChipSelect might
588 * be asserted before the SCLK polarity changes, which would disrupt
589 * the SPI communication as the device on the other end would consider
590 * the change of SCLK polarity as a clock tick already.
592 * Because spi_imx->spi_bus_clk is only set in prepare_message
593 * callback, iterate over all the transfers in spi_message, find the
594 * one with lowest bus frequency, and use that bus frequency for the
595 * delay calculation. In case all transfers have speed_hz == 0, then
596 * min_speed_hz is ~0 and the resulting delay is zero.
598 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
601 min_speed_hz = min(xfer->speed_hz, min_speed_hz);
604 delay = (2 * 1000000) / min_speed_hz;
605 if (likely(delay < 10)) /* SCLK is faster than 200 kHz */
607 else /* SCLK is _very_ slow */
608 usleep_range(delay, delay + 10);
613 static void mx51_configure_cpha(struct spi_imx_data *spi_imx,
614 struct spi_device *spi)
616 bool cpha = (spi->mode & SPI_CPHA);
617 bool flip_cpha = (spi->mode & SPI_RX_CPHA_FLIP) && spi_imx->rx_only;
618 u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
620 /* Flip cpha logical value iff flip_cpha */
624 cfg |= MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
626 cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
628 writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
631 static int mx51_ecspi_prepare_transfer(struct spi_imx_data *spi_imx,
632 struct spi_device *spi)
634 u32 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
637 /* Clear BL field and set the right value */
638 ctrl &= ~MX51_ECSPI_CTRL_BL_MASK;
639 if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx))
640 ctrl |= (spi_imx->slave_burst * 8 - 1)
641 << MX51_ECSPI_CTRL_BL_OFFSET;
643 ctrl |= (spi_imx->bits_per_word - 1)
644 << MX51_ECSPI_CTRL_BL_OFFSET;
646 /* set clock speed */
647 ctrl &= ~(0xf << MX51_ECSPI_CTRL_POSTDIV_OFFSET |
648 0xf << MX51_ECSPI_CTRL_PREDIV_OFFSET);
649 ctrl |= mx51_ecspi_clkdiv(spi_imx, spi_imx->spi_bus_clk, &clk);
650 spi_imx->spi_bus_clk = clk;
652 mx51_configure_cpha(spi_imx, spi);
655 * ERR009165: work in XHC mode instead of SMC as PIO on the chips
658 if (spi_imx->usedma && spi_imx->devtype_data->tx_glitch_fixed)
659 ctrl |= MX51_ECSPI_CTRL_SMC;
661 ctrl &= ~MX51_ECSPI_CTRL_SMC;
663 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
668 static void mx51_setup_wml(struct spi_imx_data *spi_imx)
672 if (spi_imx->devtype_data->tx_glitch_fixed)
673 tx_wml = spi_imx->wml;
675 * Configure the DMA register: setup the watermark
676 * and enable DMA request.
678 writel(MX51_ECSPI_DMA_RX_WML(spi_imx->wml - 1) |
679 MX51_ECSPI_DMA_TX_WML(tx_wml) |
680 MX51_ECSPI_DMA_RXT_WML(spi_imx->wml) |
681 MX51_ECSPI_DMA_TEDEN | MX51_ECSPI_DMA_RXDEN |
682 MX51_ECSPI_DMA_RXTDEN, spi_imx->base + MX51_ECSPI_DMA);
685 static int mx51_ecspi_rx_available(struct spi_imx_data *spi_imx)
687 return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR;
690 static void mx51_ecspi_reset(struct spi_imx_data *spi_imx)
692 /* drain receive buffer */
693 while (mx51_ecspi_rx_available(spi_imx))
694 readl(spi_imx->base + MXC_CSPIRXDATA);
697 #define MX31_INTREG_TEEN (1 << 0)
698 #define MX31_INTREG_RREN (1 << 3)
700 #define MX31_CSPICTRL_ENABLE (1 << 0)
701 #define MX31_CSPICTRL_MASTER (1 << 1)
702 #define MX31_CSPICTRL_XCH (1 << 2)
703 #define MX31_CSPICTRL_SMC (1 << 3)
704 #define MX31_CSPICTRL_POL (1 << 4)
705 #define MX31_CSPICTRL_PHA (1 << 5)
706 #define MX31_CSPICTRL_SSCTL (1 << 6)
707 #define MX31_CSPICTRL_SSPOL (1 << 7)
708 #define MX31_CSPICTRL_BC_SHIFT 8
709 #define MX35_CSPICTRL_BL_SHIFT 20
710 #define MX31_CSPICTRL_CS_SHIFT 24
711 #define MX35_CSPICTRL_CS_SHIFT 12
712 #define MX31_CSPICTRL_DR_SHIFT 16
714 #define MX31_CSPI_DMAREG 0x10
715 #define MX31_DMAREG_RH_DEN (1<<4)
716 #define MX31_DMAREG_TH_DEN (1<<1)
718 #define MX31_CSPISTATUS 0x14
719 #define MX31_STATUS_RR (1 << 3)
721 #define MX31_CSPI_TESTREG 0x1C
722 #define MX31_TEST_LBC (1 << 14)
724 /* These functions also work for the i.MX35, but be aware that
725 * the i.MX35 has a slightly different register layout for bits
726 * we do not use here.
728 static void mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
730 unsigned int val = 0;
732 if (enable & MXC_INT_TE)
733 val |= MX31_INTREG_TEEN;
734 if (enable & MXC_INT_RR)
735 val |= MX31_INTREG_RREN;
737 writel(val, spi_imx->base + MXC_CSPIINT);
740 static void mx31_trigger(struct spi_imx_data *spi_imx)
744 reg = readl(spi_imx->base + MXC_CSPICTRL);
745 reg |= MX31_CSPICTRL_XCH;
746 writel(reg, spi_imx->base + MXC_CSPICTRL);
749 static int mx31_prepare_message(struct spi_imx_data *spi_imx,
750 struct spi_message *msg)
755 static int mx31_prepare_transfer(struct spi_imx_data *spi_imx,
756 struct spi_device *spi)
758 unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
761 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->spi_bus_clk, &clk) <<
762 MX31_CSPICTRL_DR_SHIFT;
763 spi_imx->spi_bus_clk = clk;
765 if (is_imx35_cspi(spi_imx)) {
766 reg |= (spi_imx->bits_per_word - 1) << MX35_CSPICTRL_BL_SHIFT;
767 reg |= MX31_CSPICTRL_SSCTL;
769 reg |= (spi_imx->bits_per_word - 1) << MX31_CSPICTRL_BC_SHIFT;
772 if (spi->mode & SPI_CPHA)
773 reg |= MX31_CSPICTRL_PHA;
774 if (spi->mode & SPI_CPOL)
775 reg |= MX31_CSPICTRL_POL;
776 if (spi->mode & SPI_CS_HIGH)
777 reg |= MX31_CSPICTRL_SSPOL;
779 reg |= (spi->chip_select) <<
780 (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT :
781 MX31_CSPICTRL_CS_SHIFT);
784 reg |= MX31_CSPICTRL_SMC;
786 writel(reg, spi_imx->base + MXC_CSPICTRL);
788 reg = readl(spi_imx->base + MX31_CSPI_TESTREG);
789 if (spi->mode & SPI_LOOP)
790 reg |= MX31_TEST_LBC;
792 reg &= ~MX31_TEST_LBC;
793 writel(reg, spi_imx->base + MX31_CSPI_TESTREG);
795 if (spi_imx->usedma) {
797 * configure DMA requests when RXFIFO is half full and
798 * when TXFIFO is half empty
800 writel(MX31_DMAREG_RH_DEN | MX31_DMAREG_TH_DEN,
801 spi_imx->base + MX31_CSPI_DMAREG);
807 static int mx31_rx_available(struct spi_imx_data *spi_imx)
809 return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
812 static void mx31_reset(struct spi_imx_data *spi_imx)
814 /* drain receive buffer */
815 while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR)
816 readl(spi_imx->base + MXC_CSPIRXDATA);
819 #define MX21_INTREG_RR (1 << 4)
820 #define MX21_INTREG_TEEN (1 << 9)
821 #define MX21_INTREG_RREN (1 << 13)
823 #define MX21_CSPICTRL_POL (1 << 5)
824 #define MX21_CSPICTRL_PHA (1 << 6)
825 #define MX21_CSPICTRL_SSPOL (1 << 8)
826 #define MX21_CSPICTRL_XCH (1 << 9)
827 #define MX21_CSPICTRL_ENABLE (1 << 10)
828 #define MX21_CSPICTRL_MASTER (1 << 11)
829 #define MX21_CSPICTRL_DR_SHIFT 14
830 #define MX21_CSPICTRL_CS_SHIFT 19
832 static void mx21_intctrl(struct spi_imx_data *spi_imx, int enable)
834 unsigned int val = 0;
836 if (enable & MXC_INT_TE)
837 val |= MX21_INTREG_TEEN;
838 if (enable & MXC_INT_RR)
839 val |= MX21_INTREG_RREN;
841 writel(val, spi_imx->base + MXC_CSPIINT);
844 static void mx21_trigger(struct spi_imx_data *spi_imx)
848 reg = readl(spi_imx->base + MXC_CSPICTRL);
849 reg |= MX21_CSPICTRL_XCH;
850 writel(reg, spi_imx->base + MXC_CSPICTRL);
853 static int mx21_prepare_message(struct spi_imx_data *spi_imx,
854 struct spi_message *msg)
859 static int mx21_prepare_transfer(struct spi_imx_data *spi_imx,
860 struct spi_device *spi)
862 unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER;
863 unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18;
866 reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, spi_imx->spi_bus_clk, max, &clk)
867 << MX21_CSPICTRL_DR_SHIFT;
868 spi_imx->spi_bus_clk = clk;
870 reg |= spi_imx->bits_per_word - 1;
872 if (spi->mode & SPI_CPHA)
873 reg |= MX21_CSPICTRL_PHA;
874 if (spi->mode & SPI_CPOL)
875 reg |= MX21_CSPICTRL_POL;
876 if (spi->mode & SPI_CS_HIGH)
877 reg |= MX21_CSPICTRL_SSPOL;
879 reg |= spi->chip_select << MX21_CSPICTRL_CS_SHIFT;
881 writel(reg, spi_imx->base + MXC_CSPICTRL);
886 static int mx21_rx_available(struct spi_imx_data *spi_imx)
888 return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR;
891 static void mx21_reset(struct spi_imx_data *spi_imx)
893 writel(1, spi_imx->base + MXC_RESET);
896 #define MX1_INTREG_RR (1 << 3)
897 #define MX1_INTREG_TEEN (1 << 8)
898 #define MX1_INTREG_RREN (1 << 11)
900 #define MX1_CSPICTRL_POL (1 << 4)
901 #define MX1_CSPICTRL_PHA (1 << 5)
902 #define MX1_CSPICTRL_XCH (1 << 8)
903 #define MX1_CSPICTRL_ENABLE (1 << 9)
904 #define MX1_CSPICTRL_MASTER (1 << 10)
905 #define MX1_CSPICTRL_DR_SHIFT 13
907 static void mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
909 unsigned int val = 0;
911 if (enable & MXC_INT_TE)
912 val |= MX1_INTREG_TEEN;
913 if (enable & MXC_INT_RR)
914 val |= MX1_INTREG_RREN;
916 writel(val, spi_imx->base + MXC_CSPIINT);
919 static void mx1_trigger(struct spi_imx_data *spi_imx)
923 reg = readl(spi_imx->base + MXC_CSPICTRL);
924 reg |= MX1_CSPICTRL_XCH;
925 writel(reg, spi_imx->base + MXC_CSPICTRL);
928 static int mx1_prepare_message(struct spi_imx_data *spi_imx,
929 struct spi_message *msg)
934 static int mx1_prepare_transfer(struct spi_imx_data *spi_imx,
935 struct spi_device *spi)
937 unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
940 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->spi_bus_clk, &clk) <<
941 MX1_CSPICTRL_DR_SHIFT;
942 spi_imx->spi_bus_clk = clk;
944 reg |= spi_imx->bits_per_word - 1;
946 if (spi->mode & SPI_CPHA)
947 reg |= MX1_CSPICTRL_PHA;
948 if (spi->mode & SPI_CPOL)
949 reg |= MX1_CSPICTRL_POL;
951 writel(reg, spi_imx->base + MXC_CSPICTRL);
956 static int mx1_rx_available(struct spi_imx_data *spi_imx)
958 return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
961 static void mx1_reset(struct spi_imx_data *spi_imx)
963 writel(1, spi_imx->base + MXC_RESET);
966 static struct spi_imx_devtype_data imx1_cspi_devtype_data = {
967 .intctrl = mx1_intctrl,
968 .prepare_message = mx1_prepare_message,
969 .prepare_transfer = mx1_prepare_transfer,
970 .trigger = mx1_trigger,
971 .rx_available = mx1_rx_available,
974 .has_dmamode = false,
975 .dynamic_burst = false,
976 .has_slavemode = false,
977 .devtype = IMX1_CSPI,
980 static struct spi_imx_devtype_data imx21_cspi_devtype_data = {
981 .intctrl = mx21_intctrl,
982 .prepare_message = mx21_prepare_message,
983 .prepare_transfer = mx21_prepare_transfer,
984 .trigger = mx21_trigger,
985 .rx_available = mx21_rx_available,
988 .has_dmamode = false,
989 .dynamic_burst = false,
990 .has_slavemode = false,
991 .devtype = IMX21_CSPI,
994 static struct spi_imx_devtype_data imx27_cspi_devtype_data = {
995 /* i.mx27 cspi shares the functions with i.mx21 one */
996 .intctrl = mx21_intctrl,
997 .prepare_message = mx21_prepare_message,
998 .prepare_transfer = mx21_prepare_transfer,
999 .trigger = mx21_trigger,
1000 .rx_available = mx21_rx_available,
1001 .reset = mx21_reset,
1003 .has_dmamode = false,
1004 .dynamic_burst = false,
1005 .has_slavemode = false,
1006 .devtype = IMX27_CSPI,
1009 static struct spi_imx_devtype_data imx31_cspi_devtype_data = {
1010 .intctrl = mx31_intctrl,
1011 .prepare_message = mx31_prepare_message,
1012 .prepare_transfer = mx31_prepare_transfer,
1013 .trigger = mx31_trigger,
1014 .rx_available = mx31_rx_available,
1015 .reset = mx31_reset,
1017 .has_dmamode = false,
1018 .dynamic_burst = false,
1019 .has_slavemode = false,
1020 .devtype = IMX31_CSPI,
1023 static struct spi_imx_devtype_data imx35_cspi_devtype_data = {
1024 /* i.mx35 and later cspi shares the functions with i.mx31 one */
1025 .intctrl = mx31_intctrl,
1026 .prepare_message = mx31_prepare_message,
1027 .prepare_transfer = mx31_prepare_transfer,
1028 .trigger = mx31_trigger,
1029 .rx_available = mx31_rx_available,
1030 .reset = mx31_reset,
1032 .has_dmamode = true,
1033 .dynamic_burst = false,
1034 .has_slavemode = false,
1035 .devtype = IMX35_CSPI,
1038 static struct spi_imx_devtype_data imx51_ecspi_devtype_data = {
1039 .intctrl = mx51_ecspi_intctrl,
1040 .prepare_message = mx51_ecspi_prepare_message,
1041 .prepare_transfer = mx51_ecspi_prepare_transfer,
1042 .trigger = mx51_ecspi_trigger,
1043 .rx_available = mx51_ecspi_rx_available,
1044 .reset = mx51_ecspi_reset,
1045 .setup_wml = mx51_setup_wml,
1046 .disable_dma = mx51_disable_dma,
1048 .has_dmamode = true,
1049 .dynamic_burst = true,
1050 .has_slavemode = true,
1051 .disable = mx51_ecspi_disable,
1052 .devtype = IMX51_ECSPI,
1055 static struct spi_imx_devtype_data imx53_ecspi_devtype_data = {
1056 .intctrl = mx51_ecspi_intctrl,
1057 .prepare_message = mx51_ecspi_prepare_message,
1058 .prepare_transfer = mx51_ecspi_prepare_transfer,
1059 .trigger = mx51_ecspi_trigger,
1060 .rx_available = mx51_ecspi_rx_available,
1061 .disable_dma = mx51_disable_dma,
1062 .reset = mx51_ecspi_reset,
1064 .has_dmamode = true,
1065 .has_slavemode = true,
1066 .disable = mx51_ecspi_disable,
1067 .devtype = IMX53_ECSPI,
1070 static struct spi_imx_devtype_data imx6ul_ecspi_devtype_data = {
1071 .intctrl = mx51_ecspi_intctrl,
1072 .prepare_message = mx51_ecspi_prepare_message,
1073 .prepare_transfer = mx51_ecspi_prepare_transfer,
1074 .trigger = mx51_ecspi_trigger,
1075 .rx_available = mx51_ecspi_rx_available,
1076 .reset = mx51_ecspi_reset,
1077 .setup_wml = mx51_setup_wml,
1079 .has_dmamode = true,
1080 .dynamic_burst = true,
1081 .has_slavemode = true,
1082 .tx_glitch_fixed = true,
1083 .disable = mx51_ecspi_disable,
1084 .devtype = IMX51_ECSPI,
1087 static const struct of_device_id spi_imx_dt_ids[] = {
1088 { .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, },
1089 { .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, },
1090 { .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, },
1091 { .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, },
1092 { .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, },
1093 { .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, },
1094 { .compatible = "fsl,imx53-ecspi", .data = &imx53_ecspi_devtype_data, },
1095 { .compatible = "fsl,imx6ul-ecspi", .data = &imx6ul_ecspi_devtype_data, },
1098 MODULE_DEVICE_TABLE(of, spi_imx_dt_ids);
1100 static void spi_imx_set_burst_len(struct spi_imx_data *spi_imx, int n_bits)
1104 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
1105 ctrl &= ~MX51_ECSPI_CTRL_BL_MASK;
1106 ctrl |= ((n_bits - 1) << MX51_ECSPI_CTRL_BL_OFFSET);
1107 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
1110 static void spi_imx_push(struct spi_imx_data *spi_imx)
1112 unsigned int burst_len;
1115 * Reload the FIFO when the remaining bytes to be transferred in the
1116 * current burst is 0. This only applies when bits_per_word is a
1119 if (!spi_imx->remainder) {
1120 if (spi_imx->dynamic_burst) {
1122 /* We need to deal unaligned data first */
1123 burst_len = spi_imx->count % MX51_ECSPI_CTRL_MAX_BURST;
1126 burst_len = MX51_ECSPI_CTRL_MAX_BURST;
1128 spi_imx_set_burst_len(spi_imx, burst_len * 8);
1130 spi_imx->remainder = burst_len;
1132 spi_imx->remainder = spi_imx_bytes_per_word(spi_imx->bits_per_word);
1136 while (spi_imx->txfifo < spi_imx->devtype_data->fifo_size) {
1137 if (!spi_imx->count)
1139 if (spi_imx->dynamic_burst &&
1140 spi_imx->txfifo >= DIV_ROUND_UP(spi_imx->remainder, 4))
1142 spi_imx->tx(spi_imx);
1146 if (!spi_imx->slave_mode)
1147 spi_imx->devtype_data->trigger(spi_imx);
1150 static irqreturn_t spi_imx_isr(int irq, void *dev_id)
1152 struct spi_imx_data *spi_imx = dev_id;
1154 while (spi_imx->txfifo &&
1155 spi_imx->devtype_data->rx_available(spi_imx)) {
1156 spi_imx->rx(spi_imx);
1160 if (spi_imx->count) {
1161 spi_imx_push(spi_imx);
1165 if (spi_imx->txfifo) {
1166 /* No data left to push, but still waiting for rx data,
1167 * enable receive data available interrupt.
1169 spi_imx->devtype_data->intctrl(
1170 spi_imx, MXC_INT_RR);
1174 spi_imx->devtype_data->intctrl(spi_imx, 0);
1175 complete(&spi_imx->xfer_done);
1180 static int spi_imx_dma_configure(struct spi_controller *controller)
1183 enum dma_slave_buswidth buswidth;
1184 struct dma_slave_config rx = {}, tx = {};
1185 struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
1187 switch (spi_imx_bytes_per_word(spi_imx->bits_per_word)) {
1189 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
1192 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
1195 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
1201 tx.direction = DMA_MEM_TO_DEV;
1202 tx.dst_addr = spi_imx->base_phys + MXC_CSPITXDATA;
1203 tx.dst_addr_width = buswidth;
1204 tx.dst_maxburst = spi_imx->wml;
1205 ret = dmaengine_slave_config(controller->dma_tx, &tx);
1207 dev_err(spi_imx->dev, "TX dma configuration failed with %d\n", ret);
1211 rx.direction = DMA_DEV_TO_MEM;
1212 rx.src_addr = spi_imx->base_phys + MXC_CSPIRXDATA;
1213 rx.src_addr_width = buswidth;
1214 rx.src_maxburst = spi_imx->wml;
1215 ret = dmaengine_slave_config(controller->dma_rx, &rx);
1217 dev_err(spi_imx->dev, "RX dma configuration failed with %d\n", ret);
1224 static int spi_imx_setupxfer(struct spi_device *spi,
1225 struct spi_transfer *t)
1227 struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
1233 if (!spi->max_speed_hz) {
1234 dev_err(&spi->dev, "no speed_hz provided!\n");
1237 dev_dbg(&spi->dev, "using spi->max_speed_hz!\n");
1238 spi_imx->spi_bus_clk = spi->max_speed_hz;
1240 spi_imx->spi_bus_clk = t->speed_hz;
1242 spi_imx->bits_per_word = t->bits_per_word;
1245 * Initialize the functions for transfer. To transfer non byte-aligned
1246 * words, we have to use multiple word-size bursts, we can't use
1247 * dynamic_burst in that case.
1249 if (spi_imx->devtype_data->dynamic_burst && !spi_imx->slave_mode &&
1250 !(spi->mode & SPI_CS_WORD) &&
1251 (spi_imx->bits_per_word == 8 ||
1252 spi_imx->bits_per_word == 16 ||
1253 spi_imx->bits_per_word == 32)) {
1255 spi_imx->rx = spi_imx_buf_rx_swap;
1256 spi_imx->tx = spi_imx_buf_tx_swap;
1257 spi_imx->dynamic_burst = 1;
1260 if (spi_imx->bits_per_word <= 8) {
1261 spi_imx->rx = spi_imx_buf_rx_u8;
1262 spi_imx->tx = spi_imx_buf_tx_u8;
1263 } else if (spi_imx->bits_per_word <= 16) {
1264 spi_imx->rx = spi_imx_buf_rx_u16;
1265 spi_imx->tx = spi_imx_buf_tx_u16;
1267 spi_imx->rx = spi_imx_buf_rx_u32;
1268 spi_imx->tx = spi_imx_buf_tx_u32;
1270 spi_imx->dynamic_burst = 0;
1273 if (spi_imx_can_dma(spi_imx->controller, spi, t))
1274 spi_imx->usedma = true;
1276 spi_imx->usedma = false;
1278 spi_imx->rx_only = ((t->tx_buf == NULL)
1279 || (t->tx_buf == spi->controller->dummy_tx));
1281 if (is_imx53_ecspi(spi_imx) && spi_imx->slave_mode) {
1282 spi_imx->rx = mx53_ecspi_rx_slave;
1283 spi_imx->tx = mx53_ecspi_tx_slave;
1284 spi_imx->slave_burst = t->len;
1287 spi_imx->devtype_data->prepare_transfer(spi_imx, spi);
1292 static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx)
1294 struct spi_controller *controller = spi_imx->controller;
1296 if (controller->dma_rx) {
1297 dma_release_channel(controller->dma_rx);
1298 controller->dma_rx = NULL;
1301 if (controller->dma_tx) {
1302 dma_release_channel(controller->dma_tx);
1303 controller->dma_tx = NULL;
1307 static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx,
1308 struct spi_controller *controller)
1312 spi_imx->wml = spi_imx->devtype_data->fifo_size / 2;
1314 /* Prepare for TX DMA: */
1315 controller->dma_tx = dma_request_chan(dev, "tx");
1316 if (IS_ERR(controller->dma_tx)) {
1317 ret = PTR_ERR(controller->dma_tx);
1318 dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret);
1319 controller->dma_tx = NULL;
1323 /* Prepare for RX : */
1324 controller->dma_rx = dma_request_chan(dev, "rx");
1325 if (IS_ERR(controller->dma_rx)) {
1326 ret = PTR_ERR(controller->dma_rx);
1327 dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret);
1328 controller->dma_rx = NULL;
1332 init_completion(&spi_imx->dma_rx_completion);
1333 init_completion(&spi_imx->dma_tx_completion);
1334 controller->can_dma = spi_imx_can_dma;
1335 controller->max_dma_len = MAX_SDMA_BD_BYTES;
1336 spi_imx->controller->flags = SPI_CONTROLLER_MUST_RX |
1337 SPI_CONTROLLER_MUST_TX;
1341 spi_imx_sdma_exit(spi_imx);
1345 static void spi_imx_dma_rx_callback(void *cookie)
1347 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
1349 complete(&spi_imx->dma_rx_completion);
1352 static void spi_imx_dma_tx_callback(void *cookie)
1354 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
1356 complete(&spi_imx->dma_tx_completion);
1359 static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size)
1361 unsigned long timeout = 0;
1363 /* Time with actual data transfer and CS change delay related to HW */
1364 timeout = (8 + 4) * size / spi_imx->spi_bus_clk;
1366 /* Add extra second for scheduler related activities */
1369 /* Double calculated timeout */
1370 return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);
1373 static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,
1374 struct spi_transfer *transfer)
1376 struct dma_async_tx_descriptor *desc_tx, *desc_rx;
1377 unsigned long transfer_timeout;
1378 unsigned long timeout;
1379 struct spi_controller *controller = spi_imx->controller;
1380 struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
1381 struct scatterlist *last_sg = sg_last(rx->sgl, rx->nents);
1382 unsigned int bytes_per_word, i;
1385 /* Get the right burst length from the last sg to ensure no tail data */
1386 bytes_per_word = spi_imx_bytes_per_word(transfer->bits_per_word);
1387 for (i = spi_imx->devtype_data->fifo_size / 2; i > 0; i--) {
1388 if (!(sg_dma_len(last_sg) % (i * bytes_per_word)))
1391 /* Use 1 as wml in case no available burst length got */
1397 ret = spi_imx_dma_configure(controller);
1399 goto dma_failure_no_start;
1401 if (!spi_imx->devtype_data->setup_wml) {
1402 dev_err(spi_imx->dev, "No setup_wml()?\n");
1404 goto dma_failure_no_start;
1406 spi_imx->devtype_data->setup_wml(spi_imx);
1409 * The TX DMA setup starts the transfer, so make sure RX is configured
1412 desc_rx = dmaengine_prep_slave_sg(controller->dma_rx,
1413 rx->sgl, rx->nents, DMA_DEV_TO_MEM,
1414 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1417 goto dma_failure_no_start;
1420 desc_rx->callback = spi_imx_dma_rx_callback;
1421 desc_rx->callback_param = (void *)spi_imx;
1422 dmaengine_submit(desc_rx);
1423 reinit_completion(&spi_imx->dma_rx_completion);
1424 dma_async_issue_pending(controller->dma_rx);
1426 desc_tx = dmaengine_prep_slave_sg(controller->dma_tx,
1427 tx->sgl, tx->nents, DMA_MEM_TO_DEV,
1428 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1430 dmaengine_terminate_all(controller->dma_tx);
1431 dmaengine_terminate_all(controller->dma_rx);
1435 desc_tx->callback = spi_imx_dma_tx_callback;
1436 desc_tx->callback_param = (void *)spi_imx;
1437 dmaengine_submit(desc_tx);
1438 reinit_completion(&spi_imx->dma_tx_completion);
1439 dma_async_issue_pending(controller->dma_tx);
1441 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1443 /* Wait SDMA to finish the data transfer.*/
1444 timeout = wait_for_completion_timeout(&spi_imx->dma_tx_completion,
1447 dev_err(spi_imx->dev, "I/O Error in DMA TX\n");
1448 dmaengine_terminate_all(controller->dma_tx);
1449 dmaengine_terminate_all(controller->dma_rx);
1453 timeout = wait_for_completion_timeout(&spi_imx->dma_rx_completion,
1456 dev_err(&controller->dev, "I/O Error in DMA RX\n");
1457 spi_imx->devtype_data->reset(spi_imx);
1458 dmaengine_terminate_all(controller->dma_rx);
1463 /* fallback to pio */
1464 dma_failure_no_start:
1465 transfer->error |= SPI_TRANS_FAIL_NO_START;
1469 static int spi_imx_pio_transfer(struct spi_device *spi,
1470 struct spi_transfer *transfer)
1472 struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
1473 unsigned long transfer_timeout;
1474 unsigned long timeout;
1476 spi_imx->tx_buf = transfer->tx_buf;
1477 spi_imx->rx_buf = transfer->rx_buf;
1478 spi_imx->count = transfer->len;
1479 spi_imx->txfifo = 0;
1480 spi_imx->remainder = 0;
1482 reinit_completion(&spi_imx->xfer_done);
1484 spi_imx_push(spi_imx);
1486 spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
1488 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1490 timeout = wait_for_completion_timeout(&spi_imx->xfer_done,
1493 dev_err(&spi->dev, "I/O Error in PIO\n");
1494 spi_imx->devtype_data->reset(spi_imx);
1501 static int spi_imx_poll_transfer(struct spi_device *spi,
1502 struct spi_transfer *transfer)
1504 struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
1505 unsigned long timeout;
1507 spi_imx->tx_buf = transfer->tx_buf;
1508 spi_imx->rx_buf = transfer->rx_buf;
1509 spi_imx->count = transfer->len;
1510 spi_imx->txfifo = 0;
1511 spi_imx->remainder = 0;
1513 /* fill in the fifo before timeout calculations if we are
1514 * interrupted here, then the data is getting transferred by
1515 * the HW while we are interrupted
1517 spi_imx_push(spi_imx);
1519 timeout = spi_imx_calculate_timeout(spi_imx, transfer->len) + jiffies;
1520 while (spi_imx->txfifo) {
1522 while (spi_imx->txfifo &&
1523 spi_imx->devtype_data->rx_available(spi_imx)) {
1524 spi_imx->rx(spi_imx);
1529 if (spi_imx->count) {
1530 spi_imx_push(spi_imx);
1534 if (spi_imx->txfifo &&
1535 time_after(jiffies, timeout)) {
1537 dev_err_ratelimited(&spi->dev,
1538 "timeout period reached: jiffies: %lu- falling back to interrupt mode\n",
1541 /* fall back to interrupt mode */
1542 return spi_imx_pio_transfer(spi, transfer);
1549 static int spi_imx_pio_transfer_slave(struct spi_device *spi,
1550 struct spi_transfer *transfer)
1552 struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
1555 if (is_imx53_ecspi(spi_imx) &&
1556 transfer->len > MX53_MAX_TRANSFER_BYTES) {
1557 dev_err(&spi->dev, "Transaction too big, max size is %d bytes\n",
1558 MX53_MAX_TRANSFER_BYTES);
1562 spi_imx->tx_buf = transfer->tx_buf;
1563 spi_imx->rx_buf = transfer->rx_buf;
1564 spi_imx->count = transfer->len;
1565 spi_imx->txfifo = 0;
1566 spi_imx->remainder = 0;
1568 reinit_completion(&spi_imx->xfer_done);
1569 spi_imx->slave_aborted = false;
1571 spi_imx_push(spi_imx);
1573 spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE | MXC_INT_RDR);
1575 if (wait_for_completion_interruptible(&spi_imx->xfer_done) ||
1576 spi_imx->slave_aborted) {
1577 dev_dbg(&spi->dev, "interrupted\n");
1581 /* ecspi has a HW issue when works in Slave mode,
1582 * after 64 words writtern to TXFIFO, even TXFIFO becomes empty,
1583 * ECSPI_TXDATA keeps shift out the last word data,
1584 * so we have to disable ECSPI when in slave mode after the
1585 * transfer completes
1587 if (spi_imx->devtype_data->disable)
1588 spi_imx->devtype_data->disable(spi_imx);
1593 static int spi_imx_transfer_one(struct spi_controller *controller,
1594 struct spi_device *spi,
1595 struct spi_transfer *transfer)
1597 struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
1598 unsigned long hz_per_byte, byte_limit;
1600 spi_imx_setupxfer(spi, transfer);
1601 transfer->effective_speed_hz = spi_imx->spi_bus_clk;
1603 /* flush rxfifo before transfer */
1604 while (spi_imx->devtype_data->rx_available(spi_imx))
1605 readl(spi_imx->base + MXC_CSPIRXDATA);
1607 if (spi_imx->slave_mode)
1608 return spi_imx_pio_transfer_slave(spi, transfer);
1611 * Calculate the estimated time in us the transfer runs. Find
1612 * the number of Hz per byte per polling limit.
1614 hz_per_byte = polling_limit_us ? ((8 + 4) * USEC_PER_SEC) / polling_limit_us : 0;
1615 byte_limit = hz_per_byte ? transfer->effective_speed_hz / hz_per_byte : 1;
1617 /* run in polling mode for short transfers */
1618 if (transfer->len < byte_limit)
1619 return spi_imx_poll_transfer(spi, transfer);
1621 if (spi_imx->usedma)
1622 return spi_imx_dma_transfer(spi_imx, transfer);
1624 return spi_imx_pio_transfer(spi, transfer);
1627 static int spi_imx_setup(struct spi_device *spi)
1629 dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
1630 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1635 static void spi_imx_cleanup(struct spi_device *spi)
1640 spi_imx_prepare_message(struct spi_controller *controller, struct spi_message *msg)
1642 struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
1645 ret = pm_runtime_resume_and_get(spi_imx->dev);
1647 dev_err(spi_imx->dev, "failed to enable clock\n");
1651 ret = spi_imx->devtype_data->prepare_message(spi_imx, msg);
1653 pm_runtime_mark_last_busy(spi_imx->dev);
1654 pm_runtime_put_autosuspend(spi_imx->dev);
1661 spi_imx_unprepare_message(struct spi_controller *controller, struct spi_message *msg)
1663 struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
1665 pm_runtime_mark_last_busy(spi_imx->dev);
1666 pm_runtime_put_autosuspend(spi_imx->dev);
1670 static int spi_imx_slave_abort(struct spi_controller *controller)
1672 struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
1674 spi_imx->slave_aborted = true;
1675 complete(&spi_imx->xfer_done);
1680 static int spi_imx_probe(struct platform_device *pdev)
1682 struct device_node *np = pdev->dev.of_node;
1683 struct spi_controller *controller;
1684 struct spi_imx_data *spi_imx;
1685 struct resource *res;
1686 int ret, irq, spi_drctl;
1687 const struct spi_imx_devtype_data *devtype_data =
1688 of_device_get_match_data(&pdev->dev);
1692 slave_mode = devtype_data->has_slavemode &&
1693 of_property_read_bool(np, "spi-slave");
1695 controller = spi_alloc_slave(&pdev->dev,
1696 sizeof(struct spi_imx_data));
1698 controller = spi_alloc_master(&pdev->dev,
1699 sizeof(struct spi_imx_data));
1703 ret = of_property_read_u32(np, "fsl,spi-rdy-drctl", &spi_drctl);
1704 if ((ret < 0) || (spi_drctl >= 0x3)) {
1705 /* '11' is reserved */
1709 platform_set_drvdata(pdev, controller);
1711 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
1712 controller->bus_num = np ? -1 : pdev->id;
1713 controller->use_gpio_descriptors = true;
1715 spi_imx = spi_controller_get_devdata(controller);
1716 spi_imx->controller = controller;
1717 spi_imx->dev = &pdev->dev;
1718 spi_imx->slave_mode = slave_mode;
1720 spi_imx->devtype_data = devtype_data;
1723 * Get number of chip selects from device properties. This can be
1724 * coming from device tree or boardfiles, if it is not defined,
1725 * a default value of 3 chip selects will be used, as all the legacy
1726 * board files have <= 3 chip selects.
1728 if (!device_property_read_u32(&pdev->dev, "num-cs", &val))
1729 controller->num_chipselect = val;
1731 controller->num_chipselect = 3;
1733 spi_imx->controller->transfer_one = spi_imx_transfer_one;
1734 spi_imx->controller->setup = spi_imx_setup;
1735 spi_imx->controller->cleanup = spi_imx_cleanup;
1736 spi_imx->controller->prepare_message = spi_imx_prepare_message;
1737 spi_imx->controller->unprepare_message = spi_imx_unprepare_message;
1738 spi_imx->controller->slave_abort = spi_imx_slave_abort;
1739 spi_imx->controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS;
1741 if (is_imx35_cspi(spi_imx) || is_imx51_ecspi(spi_imx) ||
1742 is_imx53_ecspi(spi_imx))
1743 spi_imx->controller->mode_bits |= SPI_LOOP | SPI_READY;
1745 if (is_imx51_ecspi(spi_imx) || is_imx53_ecspi(spi_imx))
1746 spi_imx->controller->mode_bits |= SPI_RX_CPHA_FLIP;
1748 if (is_imx51_ecspi(spi_imx) &&
1749 device_property_read_u32(&pdev->dev, "cs-gpios", NULL))
1751 * When using HW-CS implementing SPI_CS_WORD can be done by just
1752 * setting the burst length to the word size. This is
1753 * considerably faster than manually controlling the CS.
1755 spi_imx->controller->mode_bits |= SPI_CS_WORD;
1757 spi_imx->spi_drctl = spi_drctl;
1759 init_completion(&spi_imx->xfer_done);
1761 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1762 spi_imx->base = devm_ioremap_resource(&pdev->dev, res);
1763 if (IS_ERR(spi_imx->base)) {
1764 ret = PTR_ERR(spi_imx->base);
1765 goto out_controller_put;
1767 spi_imx->base_phys = res->start;
1769 irq = platform_get_irq(pdev, 0);
1772 goto out_controller_put;
1775 ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0,
1776 dev_name(&pdev->dev), spi_imx);
1778 dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
1779 goto out_controller_put;
1782 spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1783 if (IS_ERR(spi_imx->clk_ipg)) {
1784 ret = PTR_ERR(spi_imx->clk_ipg);
1785 goto out_controller_put;
1788 spi_imx->clk_per = devm_clk_get(&pdev->dev, "per");
1789 if (IS_ERR(spi_imx->clk_per)) {
1790 ret = PTR_ERR(spi_imx->clk_per);
1791 goto out_controller_put;
1794 ret = clk_prepare_enable(spi_imx->clk_per);
1796 goto out_controller_put;
1798 ret = clk_prepare_enable(spi_imx->clk_ipg);
1802 pm_runtime_set_autosuspend_delay(spi_imx->dev, MXC_RPM_TIMEOUT);
1803 pm_runtime_use_autosuspend(spi_imx->dev);
1804 pm_runtime_get_noresume(spi_imx->dev);
1805 pm_runtime_set_active(spi_imx->dev);
1806 pm_runtime_enable(spi_imx->dev);
1808 spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
1810 * Only validated on i.mx35 and i.mx6 now, can remove the constraint
1811 * if validated on other chips.
1813 if (spi_imx->devtype_data->has_dmamode) {
1814 ret = spi_imx_sdma_init(&pdev->dev, spi_imx, controller);
1815 if (ret == -EPROBE_DEFER)
1816 goto out_runtime_pm_put;
1819 dev_dbg(&pdev->dev, "dma setup error %d, use pio\n",
1823 spi_imx->devtype_data->reset(spi_imx);
1825 spi_imx->devtype_data->intctrl(spi_imx, 0);
1827 controller->dev.of_node = pdev->dev.of_node;
1828 ret = spi_register_controller(controller);
1830 dev_err_probe(&pdev->dev, ret, "register controller failed\n");
1831 goto out_register_controller;
1834 pm_runtime_mark_last_busy(spi_imx->dev);
1835 pm_runtime_put_autosuspend(spi_imx->dev);
1839 out_register_controller:
1840 if (spi_imx->devtype_data->has_dmamode)
1841 spi_imx_sdma_exit(spi_imx);
1843 pm_runtime_dont_use_autosuspend(spi_imx->dev);
1844 pm_runtime_set_suspended(&pdev->dev);
1845 pm_runtime_disable(spi_imx->dev);
1847 clk_disable_unprepare(spi_imx->clk_ipg);
1849 clk_disable_unprepare(spi_imx->clk_per);
1851 spi_controller_put(controller);
1856 static int spi_imx_remove(struct platform_device *pdev)
1858 struct spi_controller *controller = platform_get_drvdata(pdev);
1859 struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
1862 spi_unregister_controller(controller);
1864 ret = pm_runtime_resume_and_get(spi_imx->dev);
1866 dev_err(spi_imx->dev, "failed to enable clock\n");
1870 writel(0, spi_imx->base + MXC_CSPICTRL);
1872 pm_runtime_dont_use_autosuspend(spi_imx->dev);
1873 pm_runtime_put_sync(spi_imx->dev);
1874 pm_runtime_disable(spi_imx->dev);
1876 spi_imx_sdma_exit(spi_imx);
1881 static int __maybe_unused spi_imx_runtime_resume(struct device *dev)
1883 struct spi_controller *controller = dev_get_drvdata(dev);
1884 struct spi_imx_data *spi_imx;
1887 spi_imx = spi_controller_get_devdata(controller);
1889 ret = clk_prepare_enable(spi_imx->clk_per);
1893 ret = clk_prepare_enable(spi_imx->clk_ipg);
1895 clk_disable_unprepare(spi_imx->clk_per);
1902 static int __maybe_unused spi_imx_runtime_suspend(struct device *dev)
1904 struct spi_controller *controller = dev_get_drvdata(dev);
1905 struct spi_imx_data *spi_imx;
1907 spi_imx = spi_controller_get_devdata(controller);
1909 clk_disable_unprepare(spi_imx->clk_per);
1910 clk_disable_unprepare(spi_imx->clk_ipg);
1915 static int __maybe_unused spi_imx_suspend(struct device *dev)
1917 pinctrl_pm_select_sleep_state(dev);
1921 static int __maybe_unused spi_imx_resume(struct device *dev)
1923 pinctrl_pm_select_default_state(dev);
1927 static const struct dev_pm_ops imx_spi_pm = {
1928 SET_RUNTIME_PM_OPS(spi_imx_runtime_suspend,
1929 spi_imx_runtime_resume, NULL)
1930 SET_SYSTEM_SLEEP_PM_OPS(spi_imx_suspend, spi_imx_resume)
1933 static struct platform_driver spi_imx_driver = {
1935 .name = DRIVER_NAME,
1936 .of_match_table = spi_imx_dt_ids,
1939 .probe = spi_imx_probe,
1940 .remove = spi_imx_remove,
1942 module_platform_driver(spi_imx_driver);
1944 MODULE_DESCRIPTION("i.MX SPI Controller driver");
1945 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1946 MODULE_LICENSE("GPL");
1947 MODULE_ALIAS("platform:" DRIVER_NAME);