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/gpio.h>
12 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/spi/spi.h>
20 #include <linux/spi/spi_bitbang.h>
21 #include <linux/types.h>
23 #include <linux/of_device.h>
24 #include <linux/of_gpio.h>
26 #include <linux/platform_data/dma-imx.h>
27 #include <linux/platform_data/spi-imx.h>
29 #define DRIVER_NAME "spi_imx"
31 #define MXC_CSPIRXDATA 0x00
32 #define MXC_CSPITXDATA 0x04
33 #define MXC_CSPICTRL 0x08
34 #define MXC_CSPIINT 0x0c
35 #define MXC_RESET 0x1c
37 /* generic defines to abstract from the different register layouts */
38 #define MXC_INT_RR (1 << 0) /* Receive data ready interrupt */
39 #define MXC_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */
40 #define MXC_INT_RDR BIT(4) /* Receive date threshold interrupt */
42 /* The maximum bytes that a sdma BD can transfer.*/
43 #define MAX_SDMA_BD_BYTES (1 << 15)
44 #define MX51_ECSPI_CTRL_MAX_BURST 512
45 /* The maximum bytes that IMX53_ECSPI can transfer in slave mode.*/
46 #define MX53_MAX_TRANSFER_BYTES 512
48 enum spi_imx_devtype {
53 IMX35_CSPI, /* CSPI on all i.mx except above */
54 IMX51_ECSPI, /* ECSPI on i.mx51 */
55 IMX53_ECSPI, /* ECSPI on i.mx53 and later */
60 struct spi_imx_devtype_data {
61 void (*intctrl)(struct spi_imx_data *, int);
62 int (*config)(struct spi_device *);
63 void (*trigger)(struct spi_imx_data *);
64 int (*rx_available)(struct spi_imx_data *);
65 void (*reset)(struct spi_imx_data *);
66 void (*disable)(struct spi_imx_data *);
69 unsigned int fifo_size;
71 enum spi_imx_devtype devtype;
75 struct spi_bitbang bitbang;
78 struct completion xfer_done;
80 unsigned long base_phys;
84 unsigned long spi_clk;
85 unsigned int spi_bus_clk;
87 unsigned int speed_hz;
88 unsigned int bits_per_word;
89 unsigned int spi_drctl;
91 unsigned int count, remainder;
92 void (*tx)(struct spi_imx_data *);
93 void (*rx)(struct spi_imx_data *);
96 unsigned int txfifo; /* number of words pushed in tx FIFO */
97 unsigned int dynamic_burst;
102 unsigned int slave_burst;
107 struct completion dma_rx_completion;
108 struct completion dma_tx_completion;
110 const struct spi_imx_devtype_data *devtype_data;
113 static inline int is_imx27_cspi(struct spi_imx_data *d)
115 return d->devtype_data->devtype == IMX27_CSPI;
118 static inline int is_imx35_cspi(struct spi_imx_data *d)
120 return d->devtype_data->devtype == IMX35_CSPI;
123 static inline int is_imx51_ecspi(struct spi_imx_data *d)
125 return d->devtype_data->devtype == IMX51_ECSPI;
128 static inline int is_imx53_ecspi(struct spi_imx_data *d)
130 return d->devtype_data->devtype == IMX53_ECSPI;
133 #define MXC_SPI_BUF_RX(type) \
134 static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \
136 unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \
138 if (spi_imx->rx_buf) { \
139 *(type *)spi_imx->rx_buf = val; \
140 spi_imx->rx_buf += sizeof(type); \
143 spi_imx->remainder -= sizeof(type); \
146 #define MXC_SPI_BUF_TX(type) \
147 static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \
151 if (spi_imx->tx_buf) { \
152 val = *(type *)spi_imx->tx_buf; \
153 spi_imx->tx_buf += sizeof(type); \
156 spi_imx->count -= sizeof(type); \
158 writel(val, spi_imx->base + MXC_CSPITXDATA); \
168 /* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
169 * (which is currently not the case in this driver)
171 static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
172 256, 384, 512, 768, 1024};
175 static unsigned int spi_imx_clkdiv_1(unsigned int fin,
176 unsigned int fspi, unsigned int max, unsigned int *fres)
180 for (i = 2; i < max; i++)
181 if (fspi * mxc_clkdivs[i] >= fin)
184 *fres = fin / mxc_clkdivs[i];
188 /* MX1, MX31, MX35, MX51 CSPI */
189 static unsigned int spi_imx_clkdiv_2(unsigned int fin,
190 unsigned int fspi, unsigned int *fres)
194 for (i = 0; i < 7; i++) {
195 if (fspi * div >= fin)
205 static int spi_imx_bytes_per_word(const int bits_per_word)
207 if (bits_per_word <= 8)
209 else if (bits_per_word <= 16)
215 static bool spi_imx_can_dma(struct spi_master *master, struct spi_device *spi,
216 struct spi_transfer *transfer)
218 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
219 unsigned int bytes_per_word, i;
224 if (spi_imx->slave_mode)
227 bytes_per_word = spi_imx_bytes_per_word(transfer->bits_per_word);
229 for (i = spi_imx->devtype_data->fifo_size / 2; i > 0; i--) {
230 if (!(transfer->len % (i * bytes_per_word)))
235 spi_imx->dynamic_burst = 0;
240 #define MX51_ECSPI_CTRL 0x08
241 #define MX51_ECSPI_CTRL_ENABLE (1 << 0)
242 #define MX51_ECSPI_CTRL_XCH (1 << 2)
243 #define MX51_ECSPI_CTRL_SMC (1 << 3)
244 #define MX51_ECSPI_CTRL_MODE_MASK (0xf << 4)
245 #define MX51_ECSPI_CTRL_DRCTL(drctl) ((drctl) << 16)
246 #define MX51_ECSPI_CTRL_POSTDIV_OFFSET 8
247 #define MX51_ECSPI_CTRL_PREDIV_OFFSET 12
248 #define MX51_ECSPI_CTRL_CS(cs) ((cs) << 18)
249 #define MX51_ECSPI_CTRL_BL_OFFSET 20
250 #define MX51_ECSPI_CTRL_BL_MASK (0xfff << 20)
252 #define MX51_ECSPI_CONFIG 0x0c
253 #define MX51_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs) + 0))
254 #define MX51_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs) + 4))
255 #define MX51_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs) + 8))
256 #define MX51_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs) + 12))
257 #define MX51_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs) + 20))
259 #define MX51_ECSPI_INT 0x10
260 #define MX51_ECSPI_INT_TEEN (1 << 0)
261 #define MX51_ECSPI_INT_RREN (1 << 3)
262 #define MX51_ECSPI_INT_RDREN (1 << 4)
264 #define MX51_ECSPI_DMA 0x14
265 #define MX51_ECSPI_DMA_TX_WML(wml) ((wml) & 0x3f)
266 #define MX51_ECSPI_DMA_RX_WML(wml) (((wml) & 0x3f) << 16)
267 #define MX51_ECSPI_DMA_RXT_WML(wml) (((wml) & 0x3f) << 24)
269 #define MX51_ECSPI_DMA_TEDEN (1 << 7)
270 #define MX51_ECSPI_DMA_RXDEN (1 << 23)
271 #define MX51_ECSPI_DMA_RXTDEN (1 << 31)
273 #define MX51_ECSPI_STAT 0x18
274 #define MX51_ECSPI_STAT_RR (1 << 3)
276 #define MX51_ECSPI_TESTREG 0x20
277 #define MX51_ECSPI_TESTREG_LBC BIT(31)
279 static void spi_imx_buf_rx_swap_u32(struct spi_imx_data *spi_imx)
281 unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA);
282 #ifdef __LITTLE_ENDIAN
283 unsigned int bytes_per_word;
286 if (spi_imx->rx_buf) {
287 #ifdef __LITTLE_ENDIAN
288 bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
289 if (bytes_per_word == 1)
290 val = cpu_to_be32(val);
291 else if (bytes_per_word == 2)
292 val = (val << 16) | (val >> 16);
294 *(u32 *)spi_imx->rx_buf = val;
295 spi_imx->rx_buf += sizeof(u32);
298 spi_imx->remainder -= sizeof(u32);
301 static void spi_imx_buf_rx_swap(struct spi_imx_data *spi_imx)
306 unaligned = spi_imx->remainder % 4;
309 spi_imx_buf_rx_swap_u32(spi_imx);
313 if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) {
314 spi_imx_buf_rx_u16(spi_imx);
318 val = readl(spi_imx->base + MXC_CSPIRXDATA);
320 while (unaligned--) {
321 if (spi_imx->rx_buf) {
322 *(u8 *)spi_imx->rx_buf = (val >> (8 * unaligned)) & 0xff;
325 spi_imx->remainder--;
329 static void spi_imx_buf_tx_swap_u32(struct spi_imx_data *spi_imx)
332 #ifdef __LITTLE_ENDIAN
333 unsigned int bytes_per_word;
336 if (spi_imx->tx_buf) {
337 val = *(u32 *)spi_imx->tx_buf;
338 spi_imx->tx_buf += sizeof(u32);
341 spi_imx->count -= sizeof(u32);
342 #ifdef __LITTLE_ENDIAN
343 bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
345 if (bytes_per_word == 1)
346 val = cpu_to_be32(val);
347 else if (bytes_per_word == 2)
348 val = (val << 16) | (val >> 16);
350 writel(val, spi_imx->base + MXC_CSPITXDATA);
353 static void spi_imx_buf_tx_swap(struct spi_imx_data *spi_imx)
358 unaligned = spi_imx->count % 4;
361 spi_imx_buf_tx_swap_u32(spi_imx);
365 if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) {
366 spi_imx_buf_tx_u16(spi_imx);
370 while (unaligned--) {
371 if (spi_imx->tx_buf) {
372 val |= *(u8 *)spi_imx->tx_buf << (8 * unaligned);
378 writel(val, spi_imx->base + MXC_CSPITXDATA);
381 static void mx53_ecspi_rx_slave(struct spi_imx_data *spi_imx)
383 u32 val = be32_to_cpu(readl(spi_imx->base + MXC_CSPIRXDATA));
385 if (spi_imx->rx_buf) {
386 int n_bytes = spi_imx->slave_burst % sizeof(val);
389 n_bytes = sizeof(val);
391 memcpy(spi_imx->rx_buf,
392 ((u8 *)&val) + sizeof(val) - n_bytes, n_bytes);
394 spi_imx->rx_buf += n_bytes;
395 spi_imx->slave_burst -= n_bytes;
398 spi_imx->remainder -= sizeof(u32);
401 static void mx53_ecspi_tx_slave(struct spi_imx_data *spi_imx)
404 int n_bytes = spi_imx->count % sizeof(val);
407 n_bytes = sizeof(val);
409 if (spi_imx->tx_buf) {
410 memcpy(((u8 *)&val) + sizeof(val) - n_bytes,
411 spi_imx->tx_buf, n_bytes);
412 val = cpu_to_be32(val);
413 spi_imx->tx_buf += n_bytes;
416 spi_imx->count -= n_bytes;
418 writel(val, spi_imx->base + MXC_CSPITXDATA);
422 static unsigned int mx51_ecspi_clkdiv(struct spi_imx_data *spi_imx,
423 unsigned int fspi, unsigned int *fres)
426 * there are two 4-bit dividers, the pre-divider divides by
427 * $pre, the post-divider by 2^$post
429 unsigned int pre, post;
430 unsigned int fin = spi_imx->spi_clk;
432 if (unlikely(fspi > fin))
435 post = fls(fin) - fls(fspi);
436 if (fin > fspi << post)
439 /* now we have: (fin <= fspi << post) with post being minimal */
441 post = max(4U, post) - 4;
442 if (unlikely(post > 0xf)) {
443 dev_err(spi_imx->dev, "cannot set clock freq: %u (base freq: %u)\n",
448 pre = DIV_ROUND_UP(fin, fspi << post) - 1;
450 dev_dbg(spi_imx->dev, "%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
451 __func__, fin, fspi, post, pre);
453 /* Resulting frequency for the SCLK line. */
454 *fres = (fin / (pre + 1)) >> post;
456 return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) |
457 (post << MX51_ECSPI_CTRL_POSTDIV_OFFSET);
460 static void mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable)
464 if (enable & MXC_INT_TE)
465 val |= MX51_ECSPI_INT_TEEN;
467 if (enable & MXC_INT_RR)
468 val |= MX51_ECSPI_INT_RREN;
470 if (enable & MXC_INT_RDR)
471 val |= MX51_ECSPI_INT_RDREN;
473 writel(val, spi_imx->base + MX51_ECSPI_INT);
476 static void mx51_ecspi_trigger(struct spi_imx_data *spi_imx)
480 reg = readl(spi_imx->base + MX51_ECSPI_CTRL);
481 reg |= MX51_ECSPI_CTRL_XCH;
482 writel(reg, spi_imx->base + MX51_ECSPI_CTRL);
485 static void mx51_ecspi_disable(struct spi_imx_data *spi_imx)
489 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
490 ctrl &= ~MX51_ECSPI_CTRL_ENABLE;
491 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
494 static int mx51_ecspi_config(struct spi_device *spi)
496 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
497 u32 ctrl = MX51_ECSPI_CTRL_ENABLE;
498 u32 clk = spi_imx->speed_hz, delay, reg;
499 u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
501 /* set Master or Slave mode */
502 if (spi_imx->slave_mode)
503 ctrl &= ~MX51_ECSPI_CTRL_MODE_MASK;
505 ctrl |= MX51_ECSPI_CTRL_MODE_MASK;
508 * Enable SPI_RDY handling (falling edge/level triggered).
510 if (spi->mode & SPI_READY)
511 ctrl |= MX51_ECSPI_CTRL_DRCTL(spi_imx->spi_drctl);
513 /* set clock speed */
514 ctrl |= mx51_ecspi_clkdiv(spi_imx, spi_imx->speed_hz, &clk);
515 spi_imx->spi_bus_clk = clk;
517 /* set chip select to use */
518 ctrl |= MX51_ECSPI_CTRL_CS(spi->chip_select);
520 if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx))
521 ctrl |= (spi_imx->slave_burst * 8 - 1)
522 << MX51_ECSPI_CTRL_BL_OFFSET;
524 ctrl |= (spi_imx->bits_per_word - 1)
525 << MX51_ECSPI_CTRL_BL_OFFSET;
528 * eCSPI burst completion by Chip Select signal in Slave mode
529 * is not functional for imx53 Soc, config SPI burst completed when
530 * BURST_LENGTH + 1 bits are received
532 if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx))
533 cfg &= ~MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
535 cfg |= MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
537 if (spi->mode & SPI_CPHA)
538 cfg |= MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
540 cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
542 if (spi->mode & SPI_CPOL) {
543 cfg |= MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
544 cfg |= MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
546 cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
547 cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
549 if (spi->mode & SPI_CS_HIGH)
550 cfg |= MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
552 cfg &= ~MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
555 ctrl |= MX51_ECSPI_CTRL_SMC;
557 /* CTRL register always go first to bring out controller from reset */
558 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
560 reg = readl(spi_imx->base + MX51_ECSPI_TESTREG);
561 if (spi->mode & SPI_LOOP)
562 reg |= MX51_ECSPI_TESTREG_LBC;
564 reg &= ~MX51_ECSPI_TESTREG_LBC;
565 writel(reg, spi_imx->base + MX51_ECSPI_TESTREG);
567 writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
570 * Wait until the changes in the configuration register CONFIGREG
571 * propagate into the hardware. It takes exactly one tick of the
572 * SCLK clock, but we will wait two SCLK clock just to be sure. The
573 * effect of the delay it takes for the hardware to apply changes
574 * is noticable if the SCLK clock run very slow. In such a case, if
575 * the polarity of SCLK should be inverted, the GPIO ChipSelect might
576 * be asserted before the SCLK polarity changes, which would disrupt
577 * the SPI communication as the device on the other end would consider
578 * the change of SCLK polarity as a clock tick already.
580 delay = (2 * 1000000) / clk;
581 if (likely(delay < 10)) /* SCLK is faster than 100 kHz */
583 else /* SCLK is _very_ slow */
584 usleep_range(delay, delay + 10);
587 * Configure the DMA register: setup the watermark
588 * and enable DMA request.
591 writel(MX51_ECSPI_DMA_RX_WML(spi_imx->wml) |
592 MX51_ECSPI_DMA_TX_WML(spi_imx->wml) |
593 MX51_ECSPI_DMA_RXT_WML(spi_imx->wml) |
594 MX51_ECSPI_DMA_TEDEN | MX51_ECSPI_DMA_RXDEN |
595 MX51_ECSPI_DMA_RXTDEN, spi_imx->base + MX51_ECSPI_DMA);
600 static int mx51_ecspi_rx_available(struct spi_imx_data *spi_imx)
602 return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR;
605 static void mx51_ecspi_reset(struct spi_imx_data *spi_imx)
607 /* drain receive buffer */
608 while (mx51_ecspi_rx_available(spi_imx))
609 readl(spi_imx->base + MXC_CSPIRXDATA);
612 #define MX31_INTREG_TEEN (1 << 0)
613 #define MX31_INTREG_RREN (1 << 3)
615 #define MX31_CSPICTRL_ENABLE (1 << 0)
616 #define MX31_CSPICTRL_MASTER (1 << 1)
617 #define MX31_CSPICTRL_XCH (1 << 2)
618 #define MX31_CSPICTRL_SMC (1 << 3)
619 #define MX31_CSPICTRL_POL (1 << 4)
620 #define MX31_CSPICTRL_PHA (1 << 5)
621 #define MX31_CSPICTRL_SSCTL (1 << 6)
622 #define MX31_CSPICTRL_SSPOL (1 << 7)
623 #define MX31_CSPICTRL_BC_SHIFT 8
624 #define MX35_CSPICTRL_BL_SHIFT 20
625 #define MX31_CSPICTRL_CS_SHIFT 24
626 #define MX35_CSPICTRL_CS_SHIFT 12
627 #define MX31_CSPICTRL_DR_SHIFT 16
629 #define MX31_CSPI_DMAREG 0x10
630 #define MX31_DMAREG_RH_DEN (1<<4)
631 #define MX31_DMAREG_TH_DEN (1<<1)
633 #define MX31_CSPISTATUS 0x14
634 #define MX31_STATUS_RR (1 << 3)
636 #define MX31_CSPI_TESTREG 0x1C
637 #define MX31_TEST_LBC (1 << 14)
639 /* These functions also work for the i.MX35, but be aware that
640 * the i.MX35 has a slightly different register layout for bits
641 * we do not use here.
643 static void mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
645 unsigned int val = 0;
647 if (enable & MXC_INT_TE)
648 val |= MX31_INTREG_TEEN;
649 if (enable & MXC_INT_RR)
650 val |= MX31_INTREG_RREN;
652 writel(val, spi_imx->base + MXC_CSPIINT);
655 static void mx31_trigger(struct spi_imx_data *spi_imx)
659 reg = readl(spi_imx->base + MXC_CSPICTRL);
660 reg |= MX31_CSPICTRL_XCH;
661 writel(reg, spi_imx->base + MXC_CSPICTRL);
664 static int mx31_config(struct spi_device *spi)
666 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
667 unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
670 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->speed_hz, &clk) <<
671 MX31_CSPICTRL_DR_SHIFT;
672 spi_imx->spi_bus_clk = clk;
674 if (is_imx35_cspi(spi_imx)) {
675 reg |= (spi_imx->bits_per_word - 1) << MX35_CSPICTRL_BL_SHIFT;
676 reg |= MX31_CSPICTRL_SSCTL;
678 reg |= (spi_imx->bits_per_word - 1) << MX31_CSPICTRL_BC_SHIFT;
681 if (spi->mode & SPI_CPHA)
682 reg |= MX31_CSPICTRL_PHA;
683 if (spi->mode & SPI_CPOL)
684 reg |= MX31_CSPICTRL_POL;
685 if (spi->mode & SPI_CS_HIGH)
686 reg |= MX31_CSPICTRL_SSPOL;
687 if (!gpio_is_valid(spi->cs_gpio))
688 reg |= (spi->chip_select) <<
689 (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT :
690 MX31_CSPICTRL_CS_SHIFT);
693 reg |= MX31_CSPICTRL_SMC;
695 writel(reg, spi_imx->base + MXC_CSPICTRL);
697 reg = readl(spi_imx->base + MX31_CSPI_TESTREG);
698 if (spi->mode & SPI_LOOP)
699 reg |= MX31_TEST_LBC;
701 reg &= ~MX31_TEST_LBC;
702 writel(reg, spi_imx->base + MX31_CSPI_TESTREG);
704 if (spi_imx->usedma) {
705 /* configure DMA requests when RXFIFO is half full and
706 when TXFIFO is half empty */
707 writel(MX31_DMAREG_RH_DEN | MX31_DMAREG_TH_DEN,
708 spi_imx->base + MX31_CSPI_DMAREG);
714 static int mx31_rx_available(struct spi_imx_data *spi_imx)
716 return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
719 static void mx31_reset(struct spi_imx_data *spi_imx)
721 /* drain receive buffer */
722 while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR)
723 readl(spi_imx->base + MXC_CSPIRXDATA);
726 #define MX21_INTREG_RR (1 << 4)
727 #define MX21_INTREG_TEEN (1 << 9)
728 #define MX21_INTREG_RREN (1 << 13)
730 #define MX21_CSPICTRL_POL (1 << 5)
731 #define MX21_CSPICTRL_PHA (1 << 6)
732 #define MX21_CSPICTRL_SSPOL (1 << 8)
733 #define MX21_CSPICTRL_XCH (1 << 9)
734 #define MX21_CSPICTRL_ENABLE (1 << 10)
735 #define MX21_CSPICTRL_MASTER (1 << 11)
736 #define MX21_CSPICTRL_DR_SHIFT 14
737 #define MX21_CSPICTRL_CS_SHIFT 19
739 static void mx21_intctrl(struct spi_imx_data *spi_imx, int enable)
741 unsigned int val = 0;
743 if (enable & MXC_INT_TE)
744 val |= MX21_INTREG_TEEN;
745 if (enable & MXC_INT_RR)
746 val |= MX21_INTREG_RREN;
748 writel(val, spi_imx->base + MXC_CSPIINT);
751 static void mx21_trigger(struct spi_imx_data *spi_imx)
755 reg = readl(spi_imx->base + MXC_CSPICTRL);
756 reg |= MX21_CSPICTRL_XCH;
757 writel(reg, spi_imx->base + MXC_CSPICTRL);
760 static int mx21_config(struct spi_device *spi)
762 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
763 unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER;
764 unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18;
767 reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, spi_imx->speed_hz, max, &clk)
768 << MX21_CSPICTRL_DR_SHIFT;
769 spi_imx->spi_bus_clk = clk;
771 reg |= spi_imx->bits_per_word - 1;
773 if (spi->mode & SPI_CPHA)
774 reg |= MX21_CSPICTRL_PHA;
775 if (spi->mode & SPI_CPOL)
776 reg |= MX21_CSPICTRL_POL;
777 if (spi->mode & SPI_CS_HIGH)
778 reg |= MX21_CSPICTRL_SSPOL;
779 if (!gpio_is_valid(spi->cs_gpio))
780 reg |= spi->chip_select << MX21_CSPICTRL_CS_SHIFT;
782 writel(reg, spi_imx->base + MXC_CSPICTRL);
787 static int mx21_rx_available(struct spi_imx_data *spi_imx)
789 return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR;
792 static void mx21_reset(struct spi_imx_data *spi_imx)
794 writel(1, spi_imx->base + MXC_RESET);
797 #define MX1_INTREG_RR (1 << 3)
798 #define MX1_INTREG_TEEN (1 << 8)
799 #define MX1_INTREG_RREN (1 << 11)
801 #define MX1_CSPICTRL_POL (1 << 4)
802 #define MX1_CSPICTRL_PHA (1 << 5)
803 #define MX1_CSPICTRL_XCH (1 << 8)
804 #define MX1_CSPICTRL_ENABLE (1 << 9)
805 #define MX1_CSPICTRL_MASTER (1 << 10)
806 #define MX1_CSPICTRL_DR_SHIFT 13
808 static void mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
810 unsigned int val = 0;
812 if (enable & MXC_INT_TE)
813 val |= MX1_INTREG_TEEN;
814 if (enable & MXC_INT_RR)
815 val |= MX1_INTREG_RREN;
817 writel(val, spi_imx->base + MXC_CSPIINT);
820 static void mx1_trigger(struct spi_imx_data *spi_imx)
824 reg = readl(spi_imx->base + MXC_CSPICTRL);
825 reg |= MX1_CSPICTRL_XCH;
826 writel(reg, spi_imx->base + MXC_CSPICTRL);
829 static int mx1_config(struct spi_device *spi)
831 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
832 unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
835 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->speed_hz, &clk) <<
836 MX1_CSPICTRL_DR_SHIFT;
837 spi_imx->spi_bus_clk = clk;
839 reg |= spi_imx->bits_per_word - 1;
841 if (spi->mode & SPI_CPHA)
842 reg |= MX1_CSPICTRL_PHA;
843 if (spi->mode & SPI_CPOL)
844 reg |= MX1_CSPICTRL_POL;
846 writel(reg, spi_imx->base + MXC_CSPICTRL);
851 static int mx1_rx_available(struct spi_imx_data *spi_imx)
853 return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
856 static void mx1_reset(struct spi_imx_data *spi_imx)
858 writel(1, spi_imx->base + MXC_RESET);
861 static struct spi_imx_devtype_data imx1_cspi_devtype_data = {
862 .intctrl = mx1_intctrl,
863 .config = mx1_config,
864 .trigger = mx1_trigger,
865 .rx_available = mx1_rx_available,
868 .has_dmamode = false,
869 .dynamic_burst = false,
870 .has_slavemode = false,
871 .devtype = IMX1_CSPI,
874 static struct spi_imx_devtype_data imx21_cspi_devtype_data = {
875 .intctrl = mx21_intctrl,
876 .config = mx21_config,
877 .trigger = mx21_trigger,
878 .rx_available = mx21_rx_available,
881 .has_dmamode = false,
882 .dynamic_burst = false,
883 .has_slavemode = false,
884 .devtype = IMX21_CSPI,
887 static struct spi_imx_devtype_data imx27_cspi_devtype_data = {
888 /* i.mx27 cspi shares the functions with i.mx21 one */
889 .intctrl = mx21_intctrl,
890 .config = mx21_config,
891 .trigger = mx21_trigger,
892 .rx_available = mx21_rx_available,
895 .has_dmamode = false,
896 .dynamic_burst = false,
897 .has_slavemode = false,
898 .devtype = IMX27_CSPI,
901 static struct spi_imx_devtype_data imx31_cspi_devtype_data = {
902 .intctrl = mx31_intctrl,
903 .config = mx31_config,
904 .trigger = mx31_trigger,
905 .rx_available = mx31_rx_available,
908 .has_dmamode = false,
909 .dynamic_burst = false,
910 .has_slavemode = false,
911 .devtype = IMX31_CSPI,
914 static struct spi_imx_devtype_data imx35_cspi_devtype_data = {
915 /* i.mx35 and later cspi shares the functions with i.mx31 one */
916 .intctrl = mx31_intctrl,
917 .config = mx31_config,
918 .trigger = mx31_trigger,
919 .rx_available = mx31_rx_available,
923 .dynamic_burst = false,
924 .has_slavemode = false,
925 .devtype = IMX35_CSPI,
928 static struct spi_imx_devtype_data imx51_ecspi_devtype_data = {
929 .intctrl = mx51_ecspi_intctrl,
930 .config = mx51_ecspi_config,
931 .trigger = mx51_ecspi_trigger,
932 .rx_available = mx51_ecspi_rx_available,
933 .reset = mx51_ecspi_reset,
936 .dynamic_burst = true,
937 .has_slavemode = true,
938 .disable = mx51_ecspi_disable,
939 .devtype = IMX51_ECSPI,
942 static struct spi_imx_devtype_data imx53_ecspi_devtype_data = {
943 .intctrl = mx51_ecspi_intctrl,
944 .config = mx51_ecspi_config,
945 .trigger = mx51_ecspi_trigger,
946 .rx_available = mx51_ecspi_rx_available,
947 .reset = mx51_ecspi_reset,
950 .has_slavemode = true,
951 .disable = mx51_ecspi_disable,
952 .devtype = IMX53_ECSPI,
955 static const struct platform_device_id spi_imx_devtype[] = {
958 .driver_data = (kernel_ulong_t) &imx1_cspi_devtype_data,
960 .name = "imx21-cspi",
961 .driver_data = (kernel_ulong_t) &imx21_cspi_devtype_data,
963 .name = "imx27-cspi",
964 .driver_data = (kernel_ulong_t) &imx27_cspi_devtype_data,
966 .name = "imx31-cspi",
967 .driver_data = (kernel_ulong_t) &imx31_cspi_devtype_data,
969 .name = "imx35-cspi",
970 .driver_data = (kernel_ulong_t) &imx35_cspi_devtype_data,
972 .name = "imx51-ecspi",
973 .driver_data = (kernel_ulong_t) &imx51_ecspi_devtype_data,
975 .name = "imx53-ecspi",
976 .driver_data = (kernel_ulong_t) &imx53_ecspi_devtype_data,
982 static const struct of_device_id spi_imx_dt_ids[] = {
983 { .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, },
984 { .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, },
985 { .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, },
986 { .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, },
987 { .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, },
988 { .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, },
989 { .compatible = "fsl,imx53-ecspi", .data = &imx53_ecspi_devtype_data, },
992 MODULE_DEVICE_TABLE(of, spi_imx_dt_ids);
994 static void spi_imx_chipselect(struct spi_device *spi, int is_active)
996 int active = is_active != BITBANG_CS_INACTIVE;
997 int dev_is_lowactive = !(spi->mode & SPI_CS_HIGH);
999 if (spi->mode & SPI_NO_CS)
1002 if (!gpio_is_valid(spi->cs_gpio))
1005 gpio_set_value(spi->cs_gpio, dev_is_lowactive ^ active);
1008 static void spi_imx_set_burst_len(struct spi_imx_data *spi_imx, int n_bits)
1012 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
1013 ctrl &= ~MX51_ECSPI_CTRL_BL_MASK;
1014 ctrl |= ((n_bits - 1) << MX51_ECSPI_CTRL_BL_OFFSET);
1015 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
1018 static void spi_imx_push(struct spi_imx_data *spi_imx)
1020 unsigned int burst_len, fifo_words;
1022 if (spi_imx->dynamic_burst)
1025 fifo_words = spi_imx_bytes_per_word(spi_imx->bits_per_word);
1027 * Reload the FIFO when the remaining bytes to be transferred in the
1028 * current burst is 0. This only applies when bits_per_word is a
1031 if (!spi_imx->remainder) {
1032 if (spi_imx->dynamic_burst) {
1034 /* We need to deal unaligned data first */
1035 burst_len = spi_imx->count % MX51_ECSPI_CTRL_MAX_BURST;
1038 burst_len = MX51_ECSPI_CTRL_MAX_BURST;
1040 spi_imx_set_burst_len(spi_imx, burst_len * 8);
1042 spi_imx->remainder = burst_len;
1044 spi_imx->remainder = fifo_words;
1048 while (spi_imx->txfifo < spi_imx->devtype_data->fifo_size) {
1049 if (!spi_imx->count)
1051 if (spi_imx->dynamic_burst &&
1052 spi_imx->txfifo >= DIV_ROUND_UP(spi_imx->remainder,
1055 spi_imx->tx(spi_imx);
1059 if (!spi_imx->slave_mode)
1060 spi_imx->devtype_data->trigger(spi_imx);
1063 static irqreturn_t spi_imx_isr(int irq, void *dev_id)
1065 struct spi_imx_data *spi_imx = dev_id;
1067 while (spi_imx->txfifo &&
1068 spi_imx->devtype_data->rx_available(spi_imx)) {
1069 spi_imx->rx(spi_imx);
1073 if (spi_imx->count) {
1074 spi_imx_push(spi_imx);
1078 if (spi_imx->txfifo) {
1079 /* No data left to push, but still waiting for rx data,
1080 * enable receive data available interrupt.
1082 spi_imx->devtype_data->intctrl(
1083 spi_imx, MXC_INT_RR);
1087 spi_imx->devtype_data->intctrl(spi_imx, 0);
1088 complete(&spi_imx->xfer_done);
1093 static int spi_imx_dma_configure(struct spi_master *master)
1096 enum dma_slave_buswidth buswidth;
1097 struct dma_slave_config rx = {}, tx = {};
1098 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1100 switch (spi_imx_bytes_per_word(spi_imx->bits_per_word)) {
1102 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
1105 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
1108 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
1114 tx.direction = DMA_MEM_TO_DEV;
1115 tx.dst_addr = spi_imx->base_phys + MXC_CSPITXDATA;
1116 tx.dst_addr_width = buswidth;
1117 tx.dst_maxburst = spi_imx->wml;
1118 ret = dmaengine_slave_config(master->dma_tx, &tx);
1120 dev_err(spi_imx->dev, "TX dma configuration failed with %d\n", ret);
1124 rx.direction = DMA_DEV_TO_MEM;
1125 rx.src_addr = spi_imx->base_phys + MXC_CSPIRXDATA;
1126 rx.src_addr_width = buswidth;
1127 rx.src_maxburst = spi_imx->wml;
1128 ret = dmaengine_slave_config(master->dma_rx, &rx);
1130 dev_err(spi_imx->dev, "RX dma configuration failed with %d\n", ret);
1137 static int spi_imx_setupxfer(struct spi_device *spi,
1138 struct spi_transfer *t)
1140 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1146 spi_imx->bits_per_word = t->bits_per_word;
1147 spi_imx->speed_hz = t->speed_hz;
1150 * Initialize the functions for transfer. To transfer non byte-aligned
1151 * words, we have to use multiple word-size bursts, we can't use
1152 * dynamic_burst in that case.
1154 if (spi_imx->devtype_data->dynamic_burst && !spi_imx->slave_mode &&
1155 (spi_imx->bits_per_word == 8 ||
1156 spi_imx->bits_per_word == 16 ||
1157 spi_imx->bits_per_word == 32)) {
1159 spi_imx->rx = spi_imx_buf_rx_swap;
1160 spi_imx->tx = spi_imx_buf_tx_swap;
1161 spi_imx->dynamic_burst = 1;
1164 if (spi_imx->bits_per_word <= 8) {
1165 spi_imx->rx = spi_imx_buf_rx_u8;
1166 spi_imx->tx = spi_imx_buf_tx_u8;
1167 } else if (spi_imx->bits_per_word <= 16) {
1168 spi_imx->rx = spi_imx_buf_rx_u16;
1169 spi_imx->tx = spi_imx_buf_tx_u16;
1171 spi_imx->rx = spi_imx_buf_rx_u32;
1172 spi_imx->tx = spi_imx_buf_tx_u32;
1174 spi_imx->dynamic_burst = 0;
1177 if (spi_imx_can_dma(spi_imx->bitbang.master, spi, t))
1178 spi_imx->usedma = 1;
1180 spi_imx->usedma = 0;
1182 if (spi_imx->usedma) {
1183 ret = spi_imx_dma_configure(spi->master);
1188 if (is_imx53_ecspi(spi_imx) && spi_imx->slave_mode) {
1189 spi_imx->rx = mx53_ecspi_rx_slave;
1190 spi_imx->tx = mx53_ecspi_tx_slave;
1191 spi_imx->slave_burst = t->len;
1194 spi_imx->devtype_data->config(spi);
1199 static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx)
1201 struct spi_master *master = spi_imx->bitbang.master;
1203 if (master->dma_rx) {
1204 dma_release_channel(master->dma_rx);
1205 master->dma_rx = NULL;
1208 if (master->dma_tx) {
1209 dma_release_channel(master->dma_tx);
1210 master->dma_tx = NULL;
1214 static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx,
1215 struct spi_master *master)
1219 /* use pio mode for i.mx6dl chip TKT238285 */
1220 if (of_machine_is_compatible("fsl,imx6dl"))
1223 spi_imx->wml = spi_imx->devtype_data->fifo_size / 2;
1225 /* Prepare for TX DMA: */
1226 master->dma_tx = dma_request_slave_channel_reason(dev, "tx");
1227 if (IS_ERR(master->dma_tx)) {
1228 ret = PTR_ERR(master->dma_tx);
1229 dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret);
1230 master->dma_tx = NULL;
1234 /* Prepare for RX : */
1235 master->dma_rx = dma_request_slave_channel_reason(dev, "rx");
1236 if (IS_ERR(master->dma_rx)) {
1237 ret = PTR_ERR(master->dma_rx);
1238 dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret);
1239 master->dma_rx = NULL;
1243 init_completion(&spi_imx->dma_rx_completion);
1244 init_completion(&spi_imx->dma_tx_completion);
1245 master->can_dma = spi_imx_can_dma;
1246 master->max_dma_len = MAX_SDMA_BD_BYTES;
1247 spi_imx->bitbang.master->flags = SPI_MASTER_MUST_RX |
1252 spi_imx_sdma_exit(spi_imx);
1256 static void spi_imx_dma_rx_callback(void *cookie)
1258 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
1260 complete(&spi_imx->dma_rx_completion);
1263 static void spi_imx_dma_tx_callback(void *cookie)
1265 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
1267 complete(&spi_imx->dma_tx_completion);
1270 static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size)
1272 unsigned long timeout = 0;
1274 /* Time with actual data transfer and CS change delay related to HW */
1275 timeout = (8 + 4) * size / spi_imx->spi_bus_clk;
1277 /* Add extra second for scheduler related activities */
1280 /* Double calculated timeout */
1281 return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);
1284 static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,
1285 struct spi_transfer *transfer)
1287 struct dma_async_tx_descriptor *desc_tx, *desc_rx;
1288 unsigned long transfer_timeout;
1289 unsigned long timeout;
1290 struct spi_master *master = spi_imx->bitbang.master;
1291 struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
1294 * The TX DMA setup starts the transfer, so make sure RX is configured
1297 desc_rx = dmaengine_prep_slave_sg(master->dma_rx,
1298 rx->sgl, rx->nents, DMA_DEV_TO_MEM,
1299 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1303 desc_rx->callback = spi_imx_dma_rx_callback;
1304 desc_rx->callback_param = (void *)spi_imx;
1305 dmaengine_submit(desc_rx);
1306 reinit_completion(&spi_imx->dma_rx_completion);
1307 dma_async_issue_pending(master->dma_rx);
1309 desc_tx = dmaengine_prep_slave_sg(master->dma_tx,
1310 tx->sgl, tx->nents, DMA_MEM_TO_DEV,
1311 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1313 dmaengine_terminate_all(master->dma_tx);
1317 desc_tx->callback = spi_imx_dma_tx_callback;
1318 desc_tx->callback_param = (void *)spi_imx;
1319 dmaengine_submit(desc_tx);
1320 reinit_completion(&spi_imx->dma_tx_completion);
1321 dma_async_issue_pending(master->dma_tx);
1323 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1325 /* Wait SDMA to finish the data transfer.*/
1326 timeout = wait_for_completion_timeout(&spi_imx->dma_tx_completion,
1329 dev_err(spi_imx->dev, "I/O Error in DMA TX\n");
1330 dmaengine_terminate_all(master->dma_tx);
1331 dmaengine_terminate_all(master->dma_rx);
1335 timeout = wait_for_completion_timeout(&spi_imx->dma_rx_completion,
1338 dev_err(&master->dev, "I/O Error in DMA RX\n");
1339 spi_imx->devtype_data->reset(spi_imx);
1340 dmaengine_terminate_all(master->dma_rx);
1344 return transfer->len;
1347 static int spi_imx_pio_transfer(struct spi_device *spi,
1348 struct spi_transfer *transfer)
1350 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1351 unsigned long transfer_timeout;
1352 unsigned long timeout;
1354 spi_imx->tx_buf = transfer->tx_buf;
1355 spi_imx->rx_buf = transfer->rx_buf;
1356 spi_imx->count = transfer->len;
1357 spi_imx->txfifo = 0;
1358 spi_imx->remainder = 0;
1360 reinit_completion(&spi_imx->xfer_done);
1362 spi_imx_push(spi_imx);
1364 spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
1366 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1368 timeout = wait_for_completion_timeout(&spi_imx->xfer_done,
1371 dev_err(&spi->dev, "I/O Error in PIO\n");
1372 spi_imx->devtype_data->reset(spi_imx);
1376 return transfer->len;
1379 static int spi_imx_pio_transfer_slave(struct spi_device *spi,
1380 struct spi_transfer *transfer)
1382 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1383 int ret = transfer->len;
1385 if (is_imx53_ecspi(spi_imx) &&
1386 transfer->len > MX53_MAX_TRANSFER_BYTES) {
1387 dev_err(&spi->dev, "Transaction too big, max size is %d bytes\n",
1388 MX53_MAX_TRANSFER_BYTES);
1392 spi_imx->tx_buf = transfer->tx_buf;
1393 spi_imx->rx_buf = transfer->rx_buf;
1394 spi_imx->count = transfer->len;
1395 spi_imx->txfifo = 0;
1396 spi_imx->remainder = 0;
1398 reinit_completion(&spi_imx->xfer_done);
1399 spi_imx->slave_aborted = false;
1401 spi_imx_push(spi_imx);
1403 spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE | MXC_INT_RDR);
1405 if (wait_for_completion_interruptible(&spi_imx->xfer_done) ||
1406 spi_imx->slave_aborted) {
1407 dev_dbg(&spi->dev, "interrupted\n");
1411 /* ecspi has a HW issue when works in Slave mode,
1412 * after 64 words writtern to TXFIFO, even TXFIFO becomes empty,
1413 * ECSPI_TXDATA keeps shift out the last word data,
1414 * so we have to disable ECSPI when in slave mode after the
1415 * transfer completes
1417 if (spi_imx->devtype_data->disable)
1418 spi_imx->devtype_data->disable(spi_imx);
1423 static int spi_imx_transfer(struct spi_device *spi,
1424 struct spi_transfer *transfer)
1426 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1428 /* flush rxfifo before transfer */
1429 while (spi_imx->devtype_data->rx_available(spi_imx))
1430 spi_imx->rx(spi_imx);
1432 if (spi_imx->slave_mode)
1433 return spi_imx_pio_transfer_slave(spi, transfer);
1435 if (spi_imx->usedma)
1436 return spi_imx_dma_transfer(spi_imx, transfer);
1438 return spi_imx_pio_transfer(spi, transfer);
1441 static int spi_imx_setup(struct spi_device *spi)
1443 dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
1444 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1446 if (spi->mode & SPI_NO_CS)
1449 if (gpio_is_valid(spi->cs_gpio))
1450 gpio_direction_output(spi->cs_gpio,
1451 spi->mode & SPI_CS_HIGH ? 0 : 1);
1453 spi_imx_chipselect(spi, BITBANG_CS_INACTIVE);
1458 static void spi_imx_cleanup(struct spi_device *spi)
1463 spi_imx_prepare_message(struct spi_master *master, struct spi_message *msg)
1465 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1468 ret = clk_enable(spi_imx->clk_per);
1472 ret = clk_enable(spi_imx->clk_ipg);
1474 clk_disable(spi_imx->clk_per);
1482 spi_imx_unprepare_message(struct spi_master *master, struct spi_message *msg)
1484 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1486 clk_disable(spi_imx->clk_ipg);
1487 clk_disable(spi_imx->clk_per);
1491 static int spi_imx_slave_abort(struct spi_master *master)
1493 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1495 spi_imx->slave_aborted = true;
1496 complete(&spi_imx->xfer_done);
1501 static int spi_imx_probe(struct platform_device *pdev)
1503 struct device_node *np = pdev->dev.of_node;
1504 const struct of_device_id *of_id =
1505 of_match_device(spi_imx_dt_ids, &pdev->dev);
1506 struct spi_imx_master *mxc_platform_info =
1507 dev_get_platdata(&pdev->dev);
1508 struct spi_master *master;
1509 struct spi_imx_data *spi_imx;
1510 struct resource *res;
1511 int i, ret, irq, spi_drctl;
1512 const struct spi_imx_devtype_data *devtype_data = of_id ? of_id->data :
1513 (struct spi_imx_devtype_data *)pdev->id_entry->driver_data;
1516 if (!np && !mxc_platform_info) {
1517 dev_err(&pdev->dev, "can't get the platform data\n");
1521 slave_mode = devtype_data->has_slavemode &&
1522 of_property_read_bool(np, "spi-slave");
1524 master = spi_alloc_slave(&pdev->dev,
1525 sizeof(struct spi_imx_data));
1527 master = spi_alloc_master(&pdev->dev,
1528 sizeof(struct spi_imx_data));
1532 ret = of_property_read_u32(np, "fsl,spi-rdy-drctl", &spi_drctl);
1533 if ((ret < 0) || (spi_drctl >= 0x3)) {
1534 /* '11' is reserved */
1538 platform_set_drvdata(pdev, master);
1540 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
1541 master->bus_num = np ? -1 : pdev->id;
1543 spi_imx = spi_master_get_devdata(master);
1544 spi_imx->bitbang.master = master;
1545 spi_imx->dev = &pdev->dev;
1546 spi_imx->slave_mode = slave_mode;
1548 spi_imx->devtype_data = devtype_data;
1550 /* Get number of chip selects, either platform data or OF */
1551 if (mxc_platform_info) {
1552 master->num_chipselect = mxc_platform_info->num_chipselect;
1553 if (mxc_platform_info->chipselect) {
1554 master->cs_gpios = devm_kcalloc(&master->dev,
1555 master->num_chipselect, sizeof(int),
1557 if (!master->cs_gpios)
1560 for (i = 0; i < master->num_chipselect; i++)
1561 master->cs_gpios[i] = mxc_platform_info->chipselect[i];
1566 if (!of_property_read_u32(np, "num-cs", &num_cs))
1567 master->num_chipselect = num_cs;
1568 /* If not preset, default value of 1 is used */
1571 spi_imx->bitbang.chipselect = spi_imx_chipselect;
1572 spi_imx->bitbang.setup_transfer = spi_imx_setupxfer;
1573 spi_imx->bitbang.txrx_bufs = spi_imx_transfer;
1574 spi_imx->bitbang.master->setup = spi_imx_setup;
1575 spi_imx->bitbang.master->cleanup = spi_imx_cleanup;
1576 spi_imx->bitbang.master->prepare_message = spi_imx_prepare_message;
1577 spi_imx->bitbang.master->unprepare_message = spi_imx_unprepare_message;
1578 spi_imx->bitbang.master->slave_abort = spi_imx_slave_abort;
1579 spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
1581 if (is_imx35_cspi(spi_imx) || is_imx51_ecspi(spi_imx) ||
1582 is_imx53_ecspi(spi_imx))
1583 spi_imx->bitbang.master->mode_bits |= SPI_LOOP | SPI_READY;
1585 spi_imx->spi_drctl = spi_drctl;
1587 init_completion(&spi_imx->xfer_done);
1589 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1590 spi_imx->base = devm_ioremap_resource(&pdev->dev, res);
1591 if (IS_ERR(spi_imx->base)) {
1592 ret = PTR_ERR(spi_imx->base);
1593 goto out_master_put;
1595 spi_imx->base_phys = res->start;
1597 irq = platform_get_irq(pdev, 0);
1600 goto out_master_put;
1603 ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0,
1604 dev_name(&pdev->dev), spi_imx);
1606 dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
1607 goto out_master_put;
1610 spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1611 if (IS_ERR(spi_imx->clk_ipg)) {
1612 ret = PTR_ERR(spi_imx->clk_ipg);
1613 goto out_master_put;
1616 spi_imx->clk_per = devm_clk_get(&pdev->dev, "per");
1617 if (IS_ERR(spi_imx->clk_per)) {
1618 ret = PTR_ERR(spi_imx->clk_per);
1619 goto out_master_put;
1622 ret = clk_prepare_enable(spi_imx->clk_per);
1624 goto out_master_put;
1626 ret = clk_prepare_enable(spi_imx->clk_ipg);
1630 spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
1632 * Only validated on i.mx35 and i.mx6 now, can remove the constraint
1633 * if validated on other chips.
1635 if (spi_imx->devtype_data->has_dmamode) {
1636 ret = spi_imx_sdma_init(&pdev->dev, spi_imx, master);
1637 if (ret == -EPROBE_DEFER)
1641 dev_err(&pdev->dev, "dma setup error %d, use pio\n",
1645 spi_imx->devtype_data->reset(spi_imx);
1647 spi_imx->devtype_data->intctrl(spi_imx, 0);
1649 master->dev.of_node = pdev->dev.of_node;
1650 ret = spi_bitbang_start(&spi_imx->bitbang);
1652 dev_err(&pdev->dev, "bitbang start failed with %d\n", ret);
1656 /* Request GPIO CS lines, if any */
1657 if (!spi_imx->slave_mode && master->cs_gpios) {
1658 for (i = 0; i < master->num_chipselect; i++) {
1659 if (!gpio_is_valid(master->cs_gpios[i]))
1662 ret = devm_gpio_request(&pdev->dev,
1663 master->cs_gpios[i],
1666 dev_err(&pdev->dev, "Can't get CS GPIO %i\n",
1667 master->cs_gpios[i]);
1668 goto out_spi_bitbang;
1673 dev_info(&pdev->dev, "probed\n");
1675 clk_disable(spi_imx->clk_ipg);
1676 clk_disable(spi_imx->clk_per);
1680 spi_bitbang_stop(&spi_imx->bitbang);
1682 clk_disable_unprepare(spi_imx->clk_ipg);
1684 clk_disable_unprepare(spi_imx->clk_per);
1686 spi_master_put(master);
1691 static int spi_imx_remove(struct platform_device *pdev)
1693 struct spi_master *master = platform_get_drvdata(pdev);
1694 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1697 spi_bitbang_stop(&spi_imx->bitbang);
1699 ret = clk_enable(spi_imx->clk_per);
1703 ret = clk_enable(spi_imx->clk_ipg);
1705 clk_disable(spi_imx->clk_per);
1709 writel(0, spi_imx->base + MXC_CSPICTRL);
1710 clk_disable_unprepare(spi_imx->clk_ipg);
1711 clk_disable_unprepare(spi_imx->clk_per);
1712 spi_imx_sdma_exit(spi_imx);
1713 spi_master_put(master);
1718 static struct platform_driver spi_imx_driver = {
1720 .name = DRIVER_NAME,
1721 .of_match_table = spi_imx_dt_ids,
1723 .id_table = spi_imx_devtype,
1724 .probe = spi_imx_probe,
1725 .remove = spi_imx_remove,
1727 module_platform_driver(spi_imx_driver);
1729 MODULE_DESCRIPTION("SPI Controller driver");
1730 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1731 MODULE_LICENSE("GPL");
1732 MODULE_ALIAS("platform:" DRIVER_NAME);