1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale i.MX28 SPI driver
5 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
6 * on behalf of DENX Software Engineering GmbH
8 * NOTE: This driver only supports the SPI-controller chipselects,
9 * GPIO driven chipselects are not supported.
16 #include <linux/errno.h>
18 #include <asm/arch/clock.h>
19 #include <asm/arch/imx-regs.h>
20 #include <asm/arch/sys_proto.h>
21 #include <asm/mach-imx/dma.h>
23 #define MXS_SPI_MAX_TIMEOUT 1000000
24 #define MXS_SPI_PORT_OFFSET 0x2000
25 #define MXS_SSP_CHIPSELECT_MASK 0x00300000
26 #define MXS_SSP_CHIPSELECT_SHIFT 20
28 #define MXSSSP_SMALL_TRANSFER 512
30 struct mxs_spi_slave {
31 struct spi_slave slave;
34 struct mxs_ssp_regs *regs;
37 static inline struct mxs_spi_slave *to_mxs_slave(struct spi_slave *slave)
39 return container_of(slave, struct mxs_spi_slave, slave);
42 int spi_cs_is_valid(unsigned int bus, unsigned int cs)
44 /* MXS SPI: 4 ports and 3 chip selects maximum */
45 if (!mxs_ssp_bus_id_valid(bus) || cs > 2)
51 struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
52 unsigned int max_hz, unsigned int mode)
54 struct mxs_spi_slave *mxs_slave;
56 if (!spi_cs_is_valid(bus, cs)) {
57 printf("mxs_spi: invalid bus %d / chip select %d\n", bus, cs);
61 mxs_slave = spi_alloc_slave(struct mxs_spi_slave, bus, cs);
65 if (mxs_dma_init_channel(MXS_DMA_CHANNEL_AHB_APBH_SSP0 + bus))
68 mxs_slave->max_khz = max_hz / 1000;
69 mxs_slave->mode = mode;
70 mxs_slave->regs = mxs_ssp_regs_by_bus(bus);
72 return &mxs_slave->slave;
79 void spi_free_slave(struct spi_slave *slave)
81 struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
85 int spi_claim_bus(struct spi_slave *slave)
87 struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
88 struct mxs_ssp_regs *ssp_regs = mxs_slave->regs;
91 mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
93 writel((slave->cs << MXS_SSP_CHIPSELECT_SHIFT) |
94 SSP_CTRL0_BUS_WIDTH_ONE_BIT,
95 &ssp_regs->hw_ssp_ctrl0);
97 reg = SSP_CTRL1_SSP_MODE_SPI | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS;
98 reg |= (mxs_slave->mode & SPI_CPOL) ? SSP_CTRL1_POLARITY : 0;
99 reg |= (mxs_slave->mode & SPI_CPHA) ? SSP_CTRL1_PHASE : 0;
100 writel(reg, &ssp_regs->hw_ssp_ctrl1);
102 writel(0, &ssp_regs->hw_ssp_cmd0);
104 mxs_set_ssp_busclock(slave->bus, mxs_slave->max_khz);
109 void spi_release_bus(struct spi_slave *slave)
113 static void mxs_spi_start_xfer(struct mxs_ssp_regs *ssp_regs)
115 writel(SSP_CTRL0_LOCK_CS, &ssp_regs->hw_ssp_ctrl0_set);
116 writel(SSP_CTRL0_IGNORE_CRC, &ssp_regs->hw_ssp_ctrl0_clr);
119 static void mxs_spi_end_xfer(struct mxs_ssp_regs *ssp_regs)
121 writel(SSP_CTRL0_LOCK_CS, &ssp_regs->hw_ssp_ctrl0_clr);
122 writel(SSP_CTRL0_IGNORE_CRC, &ssp_regs->hw_ssp_ctrl0_set);
125 static int mxs_spi_xfer_pio(struct mxs_spi_slave *slave,
126 char *data, int length, int write, unsigned long flags)
128 struct mxs_ssp_regs *ssp_regs = slave->regs;
130 if (flags & SPI_XFER_BEGIN)
131 mxs_spi_start_xfer(ssp_regs);
134 /* We transfer 1 byte */
135 #if defined(CONFIG_MX23)
136 writel(SSP_CTRL0_XFER_COUNT_MASK, &ssp_regs->hw_ssp_ctrl0_clr);
137 writel(1, &ssp_regs->hw_ssp_ctrl0_set);
138 #elif defined(CONFIG_MX28)
139 writel(1, &ssp_regs->hw_ssp_xfer_size);
142 if ((flags & SPI_XFER_END) && !length)
143 mxs_spi_end_xfer(ssp_regs);
146 writel(SSP_CTRL0_READ, &ssp_regs->hw_ssp_ctrl0_clr);
148 writel(SSP_CTRL0_READ, &ssp_regs->hw_ssp_ctrl0_set);
150 writel(SSP_CTRL0_RUN, &ssp_regs->hw_ssp_ctrl0_set);
152 if (mxs_wait_mask_set(&ssp_regs->hw_ssp_ctrl0_reg,
153 SSP_CTRL0_RUN, MXS_SPI_MAX_TIMEOUT)) {
154 printf("MXS SPI: Timeout waiting for start\n");
159 writel(*data++, &ssp_regs->hw_ssp_data);
161 writel(SSP_CTRL0_DATA_XFER, &ssp_regs->hw_ssp_ctrl0_set);
164 if (mxs_wait_mask_clr(&ssp_regs->hw_ssp_status_reg,
165 SSP_STATUS_FIFO_EMPTY, MXS_SPI_MAX_TIMEOUT)) {
166 printf("MXS SPI: Timeout waiting for data\n");
170 *data = readl(&ssp_regs->hw_ssp_data);
174 if (mxs_wait_mask_clr(&ssp_regs->hw_ssp_ctrl0_reg,
175 SSP_CTRL0_RUN, MXS_SPI_MAX_TIMEOUT)) {
176 printf("MXS SPI: Timeout waiting for finish\n");
184 static int mxs_spi_xfer_dma(struct mxs_spi_slave *slave,
185 char *data, int length, int write, unsigned long flags)
187 const int xfer_max_sz = 0xff00;
188 const int desc_count = DIV_ROUND_UP(length, xfer_max_sz) + 1;
189 struct mxs_ssp_regs *ssp_regs = slave->regs;
190 struct mxs_dma_desc *dp;
192 uint32_t cache_data_count;
193 const uint32_t dstart = (uint32_t)data;
198 #if defined(CONFIG_MX23)
199 const int mxs_spi_pio_words = 1;
200 #elif defined(CONFIG_MX28)
201 const int mxs_spi_pio_words = 4;
204 ALLOC_CACHE_ALIGN_BUFFER(struct mxs_dma_desc, desc, desc_count);
206 memset(desc, 0, sizeof(struct mxs_dma_desc) * desc_count);
208 ctrl0 = readl(&ssp_regs->hw_ssp_ctrl0);
209 ctrl0 |= SSP_CTRL0_DATA_XFER;
211 if (flags & SPI_XFER_BEGIN)
212 ctrl0 |= SSP_CTRL0_LOCK_CS;
214 ctrl0 |= SSP_CTRL0_READ;
216 if (length % ARCH_DMA_MINALIGN)
217 cache_data_count = roundup(length, ARCH_DMA_MINALIGN);
219 cache_data_count = length;
221 /* Flush data to DRAM so DMA can pick them up */
223 flush_dcache_range(dstart, dstart + cache_data_count);
225 /* Invalidate the area, so no writeback into the RAM races with DMA */
226 invalidate_dcache_range(dstart, dstart + cache_data_count);
228 dmach = MXS_DMA_CHANNEL_AHB_APBH_SSP0 + slave->slave.bus;
232 dp->address = (dma_addr_t)dp;
233 dp->cmd.address = (dma_addr_t)data;
236 * This is correct, even though it does indeed look insane.
237 * I hereby have to, wholeheartedly, thank Freescale Inc.,
238 * for always inventing insane hardware and keeping me busy
242 dp->cmd.data = MXS_DMA_DESC_COMMAND_DMA_READ;
244 dp->cmd.data = MXS_DMA_DESC_COMMAND_DMA_WRITE;
247 * The DMA controller can transfer large chunks (64kB) at
248 * time by setting the transfer length to 0. Setting tl to
249 * 0x10000 will overflow below and make .data contain 0.
250 * Otherwise, 0xff00 is the transfer maximum.
252 if (length >= 0x10000)
255 tl = min(length, xfer_max_sz);
258 ((tl & 0xffff) << MXS_DMA_DESC_BYTES_OFFSET) |
259 (mxs_spi_pio_words << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
260 MXS_DMA_DESC_HALT_ON_TERMINATE |
261 MXS_DMA_DESC_TERMINATE_FLUSH;
267 dp->cmd.data |= MXS_DMA_DESC_IRQ | MXS_DMA_DESC_DEC_SEM;
269 if (flags & SPI_XFER_END) {
270 ctrl0 &= ~SSP_CTRL0_LOCK_CS;
271 ctrl0 |= SSP_CTRL0_IGNORE_CRC;
276 * Write CTRL0, CMD0, CMD1 and XFER_SIZE registers in
277 * case of MX28, write only CTRL0 in case of MX23 due
278 * to the difference in register layout. It is utterly
279 * essential that the XFER_SIZE register is written on
280 * a per-descriptor basis with the same size as is the
283 dp->cmd.pio_words[0] = ctrl0;
285 dp->cmd.pio_words[1] = 0;
286 dp->cmd.pio_words[2] = 0;
287 dp->cmd.pio_words[3] = tl;
290 mxs_dma_desc_append(dmach, dp);
295 if (mxs_dma_go(dmach))
298 /* The data arrived into DRAM, invalidate cache over them */
300 invalidate_dcache_range(dstart, dstart + cache_data_count);
305 int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
306 const void *dout, void *din, unsigned long flags)
308 struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
309 struct mxs_ssp_regs *ssp_regs = mxs_slave->regs;
310 int len = bitlen / 8;
317 if (flags & SPI_XFER_END) {
318 din = (void *)&dummy;
324 /* Half-duplex only */
340 * Check for alignment, if the buffer is aligned, do DMA transfer,
341 * PIO otherwise. This is a temporary workaround until proper bounce
342 * buffer is in place.
345 if (((uint32_t)data) & (ARCH_DMA_MINALIGN - 1))
347 if (((uint32_t)len) & (ARCH_DMA_MINALIGN - 1))
351 if (!dma || (len < MXSSSP_SMALL_TRANSFER)) {
352 writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_clr);
353 return mxs_spi_xfer_pio(mxs_slave, data, len, write, flags);
355 writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_set);
356 return mxs_spi_xfer_dma(mxs_slave, data, len, write, flags);