blob: 58b1c67a19147b9ef5b539181e23e6d62830e954 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutec33de32011-11-08 23:18:14 +00002/*
3 * Freescale i.MX28 SPI driver
4 *
Lukasz Majewskid99b0182019-06-19 17:31:07 +02005 * Copyright (C) 2019 DENX Software Engineering
6 * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
7 *
Marek Vasutec33de32011-11-08 23:18:14 +00008 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
9 * on behalf of DENX Software Engineering GmbH
10 *
Marek Vasutec33de32011-11-08 23:18:14 +000011 * NOTE: This driver only supports the SPI-controller chipselects,
12 * GPIO driven chipselects are not supported.
13 */
14
15#include <common.h>
16#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060017#include <memalign.h>
Marek Vasutec33de32011-11-08 23:18:14 +000018#include <spi.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090019#include <linux/errno.h>
Marek Vasutec33de32011-11-08 23:18:14 +000020#include <asm/io.h>
21#include <asm/arch/clock.h>
22#include <asm/arch/imx-regs.h>
23#include <asm/arch/sys_proto.h>
Stefano Babic552a8482017-06-29 10:16:06 +020024#include <asm/mach-imx/dma.h>
Marek Vasutec33de32011-11-08 23:18:14 +000025
26#define MXS_SPI_MAX_TIMEOUT 1000000
27#define MXS_SPI_PORT_OFFSET 0x2000
Fabio Estevam148ca642012-04-23 08:30:50 +000028#define MXS_SSP_CHIPSELECT_MASK 0x00300000
29#define MXS_SSP_CHIPSELECT_SHIFT 20
Marek Vasutec33de32011-11-08 23:18:14 +000030
Marek Vasut7c5e6f72012-07-09 00:48:33 +000031#define MXSSSP_SMALL_TRANSFER 512
32
Otavio Salvador9c471142012-08-05 09:05:31 +000033static void mxs_spi_start_xfer(struct mxs_ssp_regs *ssp_regs)
Marek Vasutec33de32011-11-08 23:18:14 +000034{
35 writel(SSP_CTRL0_LOCK_CS, &ssp_regs->hw_ssp_ctrl0_set);
36 writel(SSP_CTRL0_IGNORE_CRC, &ssp_regs->hw_ssp_ctrl0_clr);
37}
38
Otavio Salvador9c471142012-08-05 09:05:31 +000039static void mxs_spi_end_xfer(struct mxs_ssp_regs *ssp_regs)
Marek Vasutec33de32011-11-08 23:18:14 +000040{
41 writel(SSP_CTRL0_LOCK_CS, &ssp_regs->hw_ssp_ctrl0_clr);
42 writel(SSP_CTRL0_IGNORE_CRC, &ssp_regs->hw_ssp_ctrl0_set);
43}
44
Lukasz Majewskid99b0182019-06-19 17:31:07 +020045#if !CONFIG_IS_ENABLED(DM_SPI)
46struct mxs_spi_slave {
47 struct spi_slave slave;
48 uint32_t max_khz;
49 uint32_t mode;
50 struct mxs_ssp_regs *regs;
51};
52
53static inline struct mxs_spi_slave *to_mxs_slave(struct spi_slave *slave)
54{
55 return container_of(slave, struct mxs_spi_slave, slave);
56}
57#else
58#include <dm.h>
59#include <errno.h>
Lukasz Majewskiec0c81f2019-09-05 09:54:58 +020060#include <dt-structs.h>
61
62#ifdef CONFIG_MX28
63#define dtd_fsl_imx_spi dtd_fsl_imx28_spi
64#else /* CONFIG_MX23 */
65#define dtd_fsl_imx_spi dtd_fsl_imx23_spi
66#endif
67
Lukasz Majewskid99b0182019-06-19 17:31:07 +020068struct mxs_spi_platdata {
Lukasz Majewskiec0c81f2019-09-05 09:54:58 +020069#if CONFIG_IS_ENABLED(OF_PLATDATA)
70 struct dtd_fsl_imx_spi dtplat;
71#endif
Lukasz Majewskid99b0182019-06-19 17:31:07 +020072 s32 frequency; /* Default clock frequency, -1 for none */
73 fdt_addr_t base; /* SPI IP block base address */
74 int num_cs; /* Number of CSes supported */
75 int dma_id; /* ID of the DMA channel */
76 int clk_id; /* ID of the SSP clock */
77};
78
79struct mxs_spi_priv {
80 struct mxs_ssp_regs *regs;
81 unsigned int dma_channel;
82 unsigned int max_freq;
83 unsigned int clk_id;
84 unsigned int mode;
85};
86#endif
87
88#if !CONFIG_IS_ENABLED(DM_SPI)
Marek Vasutccd4d5a2012-07-09 00:48:32 +000089static int mxs_spi_xfer_pio(struct mxs_spi_slave *slave,
90 char *data, int length, int write, unsigned long flags)
Marek Vasutec33de32011-11-08 23:18:14 +000091{
Otavio Salvador9c471142012-08-05 09:05:31 +000092 struct mxs_ssp_regs *ssp_regs = slave->regs;
Lukasz Majewskid99b0182019-06-19 17:31:07 +020093#else
94static int mxs_spi_xfer_pio(struct mxs_spi_priv *priv,
95 char *data, int length, int write,
96 unsigned long flags)
97{
98 struct mxs_ssp_regs *ssp_regs = priv->regs;
99#endif
Marek Vasutc7065fa2012-07-09 00:48:31 +0000100
Marek Vasutec33de32011-11-08 23:18:14 +0000101 if (flags & SPI_XFER_BEGIN)
102 mxs_spi_start_xfer(ssp_regs);
103
Marek Vasutccd4d5a2012-07-09 00:48:32 +0000104 while (length--) {
Marek Vasutec33de32011-11-08 23:18:14 +0000105 /* We transfer 1 byte */
Marek Vasutc96e78c2013-02-23 02:42:59 +0000106#if defined(CONFIG_MX23)
107 writel(SSP_CTRL0_XFER_COUNT_MASK, &ssp_regs->hw_ssp_ctrl0_clr);
108 writel(1, &ssp_regs->hw_ssp_ctrl0_set);
109#elif defined(CONFIG_MX28)
Marek Vasutec33de32011-11-08 23:18:14 +0000110 writel(1, &ssp_regs->hw_ssp_xfer_size);
Marek Vasutc96e78c2013-02-23 02:42:59 +0000111#endif
Marek Vasutec33de32011-11-08 23:18:14 +0000112
Marek Vasutccd4d5a2012-07-09 00:48:32 +0000113 if ((flags & SPI_XFER_END) && !length)
Marek Vasutec33de32011-11-08 23:18:14 +0000114 mxs_spi_end_xfer(ssp_regs);
115
Marek Vasutc7065fa2012-07-09 00:48:31 +0000116 if (write)
Marek Vasutec33de32011-11-08 23:18:14 +0000117 writel(SSP_CTRL0_READ, &ssp_regs->hw_ssp_ctrl0_clr);
118 else
119 writel(SSP_CTRL0_READ, &ssp_regs->hw_ssp_ctrl0_set);
120
121 writel(SSP_CTRL0_RUN, &ssp_regs->hw_ssp_ctrl0_set);
122
Otavio Salvadorfa7a51c2012-08-13 09:53:12 +0000123 if (mxs_wait_mask_set(&ssp_regs->hw_ssp_ctrl0_reg,
Marek Vasutec33de32011-11-08 23:18:14 +0000124 SSP_CTRL0_RUN, MXS_SPI_MAX_TIMEOUT)) {
125 printf("MXS SPI: Timeout waiting for start\n");
Fabio Estevamd9fb6a42012-03-18 17:23:35 +0000126 return -ETIMEDOUT;
Marek Vasutec33de32011-11-08 23:18:14 +0000127 }
128
Marek Vasutc7065fa2012-07-09 00:48:31 +0000129 if (write)
130 writel(*data++, &ssp_regs->hw_ssp_data);
Marek Vasutec33de32011-11-08 23:18:14 +0000131
132 writel(SSP_CTRL0_DATA_XFER, &ssp_regs->hw_ssp_ctrl0_set);
133
Marek Vasutc7065fa2012-07-09 00:48:31 +0000134 if (!write) {
Otavio Salvadorfa7a51c2012-08-13 09:53:12 +0000135 if (mxs_wait_mask_clr(&ssp_regs->hw_ssp_status_reg,
Marek Vasutec33de32011-11-08 23:18:14 +0000136 SSP_STATUS_FIFO_EMPTY, MXS_SPI_MAX_TIMEOUT)) {
137 printf("MXS SPI: Timeout waiting for data\n");
Fabio Estevamd9fb6a42012-03-18 17:23:35 +0000138 return -ETIMEDOUT;
Marek Vasutec33de32011-11-08 23:18:14 +0000139 }
140
Marek Vasutc7065fa2012-07-09 00:48:31 +0000141 *data = readl(&ssp_regs->hw_ssp_data);
142 data++;
Marek Vasutec33de32011-11-08 23:18:14 +0000143 }
144
Otavio Salvadorfa7a51c2012-08-13 09:53:12 +0000145 if (mxs_wait_mask_clr(&ssp_regs->hw_ssp_ctrl0_reg,
Marek Vasutec33de32011-11-08 23:18:14 +0000146 SSP_CTRL0_RUN, MXS_SPI_MAX_TIMEOUT)) {
147 printf("MXS SPI: Timeout waiting for finish\n");
Fabio Estevamd9fb6a42012-03-18 17:23:35 +0000148 return -ETIMEDOUT;
Marek Vasutec33de32011-11-08 23:18:14 +0000149 }
150 }
151
152 return 0;
Marek Vasutccd4d5a2012-07-09 00:48:32 +0000153}
154
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200155#if !CONFIG_IS_ENABLED(DM_SPI)
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000156static int mxs_spi_xfer_dma(struct mxs_spi_slave *slave,
157 char *data, int length, int write, unsigned long flags)
158{
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200159 struct mxs_ssp_regs *ssp_regs = slave->regs;
160#else
161static int mxs_spi_xfer_dma(struct mxs_spi_priv *priv,
162 char *data, int length, int write,
163 unsigned long flags)
164{ struct mxs_ssp_regs *ssp_regs = priv->regs;
165#endif
Marek Vasut2c432142012-08-21 16:17:27 +0000166 const int xfer_max_sz = 0xff00;
167 const int desc_count = DIV_ROUND_UP(length, xfer_max_sz) + 1;
Marek Vasut2c432142012-08-21 16:17:27 +0000168 struct mxs_dma_desc *dp;
169 uint32_t ctrl0;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000170 uint32_t cache_data_count;
Marek Vasut88d15552012-08-31 16:07:59 +0000171 const uint32_t dstart = (uint32_t)data;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000172 int dmach;
Marek Vasut2c432142012-08-21 16:17:27 +0000173 int tl;
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000174 int ret = 0;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000175
Marek Vasutc96e78c2013-02-23 02:42:59 +0000176#if defined(CONFIG_MX23)
177 const int mxs_spi_pio_words = 1;
178#elif defined(CONFIG_MX28)
179 const int mxs_spi_pio_words = 4;
180#endif
181
Marek Vasut2c432142012-08-21 16:17:27 +0000182 ALLOC_CACHE_ALIGN_BUFFER(struct mxs_dma_desc, desc, desc_count);
183
184 memset(desc, 0, sizeof(struct mxs_dma_desc) * desc_count);
185
186 ctrl0 = readl(&ssp_regs->hw_ssp_ctrl0);
187 ctrl0 |= SSP_CTRL0_DATA_XFER;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000188
189 if (flags & SPI_XFER_BEGIN)
190 ctrl0 |= SSP_CTRL0_LOCK_CS;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000191 if (!write)
192 ctrl0 |= SSP_CTRL0_READ;
193
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000194 if (length % ARCH_DMA_MINALIGN)
195 cache_data_count = roundup(length, ARCH_DMA_MINALIGN);
196 else
197 cache_data_count = length;
198
Marek Vasut88d15552012-08-31 16:07:59 +0000199 /* Flush data to DRAM so DMA can pick them up */
Marek Vasut2c432142012-08-21 16:17:27 +0000200 if (write)
Marek Vasut88d15552012-08-31 16:07:59 +0000201 flush_dcache_range(dstart, dstart + cache_data_count);
202
203 /* Invalidate the area, so no writeback into the RAM races with DMA */
204 invalidate_dcache_range(dstart, dstart + cache_data_count);
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000205
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200206#if !CONFIG_IS_ENABLED(DM_SPI)
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000207 dmach = MXS_DMA_CHANNEL_AHB_APBH_SSP0 + slave->slave.bus;
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200208#else
209 dmach = priv->dma_channel;
210#endif
Marek Vasut2c432142012-08-21 16:17:27 +0000211
212 dp = desc;
213 while (length) {
214 dp->address = (dma_addr_t)dp;
215 dp->cmd.address = (dma_addr_t)data;
216
217 /*
218 * This is correct, even though it does indeed look insane.
219 * I hereby have to, wholeheartedly, thank Freescale Inc.,
220 * for always inventing insane hardware and keeping me busy
221 * and employed ;-)
222 */
223 if (write)
224 dp->cmd.data = MXS_DMA_DESC_COMMAND_DMA_READ;
225 else
226 dp->cmd.data = MXS_DMA_DESC_COMMAND_DMA_WRITE;
227
228 /*
229 * The DMA controller can transfer large chunks (64kB) at
230 * time by setting the transfer length to 0. Setting tl to
231 * 0x10000 will overflow below and make .data contain 0.
232 * Otherwise, 0xff00 is the transfer maximum.
233 */
234 if (length >= 0x10000)
235 tl = 0x10000;
236 else
237 tl = min(length, xfer_max_sz);
238
239 dp->cmd.data |=
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000240 ((tl & 0xffff) << MXS_DMA_DESC_BYTES_OFFSET) |
Marek Vasutc96e78c2013-02-23 02:42:59 +0000241 (mxs_spi_pio_words << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
Marek Vasut2c432142012-08-21 16:17:27 +0000242 MXS_DMA_DESC_HALT_ON_TERMINATE |
243 MXS_DMA_DESC_TERMINATE_FLUSH;
Marek Vasut2c432142012-08-21 16:17:27 +0000244
245 data += tl;
246 length -= tl;
247
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000248 if (!length) {
249 dp->cmd.data |= MXS_DMA_DESC_IRQ | MXS_DMA_DESC_DEC_SEM;
250
251 if (flags & SPI_XFER_END) {
252 ctrl0 &= ~SSP_CTRL0_LOCK_CS;
253 ctrl0 |= SSP_CTRL0_IGNORE_CRC;
254 }
255 }
256
257 /*
Marek Vasutc96e78c2013-02-23 02:42:59 +0000258 * Write CTRL0, CMD0, CMD1 and XFER_SIZE registers in
259 * case of MX28, write only CTRL0 in case of MX23 due
260 * to the difference in register layout. It is utterly
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000261 * essential that the XFER_SIZE register is written on
262 * a per-descriptor basis with the same size as is the
263 * descriptor!
264 */
265 dp->cmd.pio_words[0] = ctrl0;
Marek Vasutc96e78c2013-02-23 02:42:59 +0000266#ifdef CONFIG_MX28
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000267 dp->cmd.pio_words[1] = 0;
268 dp->cmd.pio_words[2] = 0;
269 dp->cmd.pio_words[3] = tl;
Marek Vasutc96e78c2013-02-23 02:42:59 +0000270#endif
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000271
Marek Vasut2c432142012-08-21 16:17:27 +0000272 mxs_dma_desc_append(dmach, dp);
273
274 dp++;
275 }
276
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000277 if (mxs_dma_go(dmach))
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000278 ret = -EINVAL;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000279
280 /* The data arrived into DRAM, invalidate cache over them */
Marek Vasut88d15552012-08-31 16:07:59 +0000281 if (!write)
282 invalidate_dcache_range(dstart, dstart + cache_data_count);
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000283
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000284 return ret;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000285}
286
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200287#if !CONFIG_IS_ENABLED(DM_SPI)
Marek Vasutccd4d5a2012-07-09 00:48:32 +0000288int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
289 const void *dout, void *din, unsigned long flags)
290{
291 struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
Otavio Salvador9c471142012-08-05 09:05:31 +0000292 struct mxs_ssp_regs *ssp_regs = mxs_slave->regs;
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200293#else
294int mxs_spi_xfer(struct udevice *dev, unsigned int bitlen,
295 const void *dout, void *din, unsigned long flags)
296{
297 struct udevice *bus = dev_get_parent(dev);
298 struct mxs_spi_priv *priv = dev_get_priv(bus);
299 struct mxs_ssp_regs *ssp_regs = priv->regs;
300#endif
Marek Vasutccd4d5a2012-07-09 00:48:32 +0000301 int len = bitlen / 8;
302 char dummy;
303 int write = 0;
304 char *data = NULL;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000305 int dma = 1;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000306
Marek Vasutccd4d5a2012-07-09 00:48:32 +0000307 if (bitlen == 0) {
308 if (flags & SPI_XFER_END) {
309 din = (void *)&dummy;
310 len = 1;
311 } else
312 return 0;
313 }
314
315 /* Half-duplex only */
316 if (din && dout)
317 return -EINVAL;
318 /* No data */
319 if (!din && !dout)
320 return 0;
321
322 if (dout) {
323 data = (char *)dout;
324 write = 1;
325 } else if (din) {
326 data = (char *)din;
327 write = 0;
328 }
329
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000330 /*
331 * Check for alignment, if the buffer is aligned, do DMA transfer,
332 * PIO otherwise. This is a temporary workaround until proper bounce
333 * buffer is in place.
334 */
335 if (dma) {
336 if (((uint32_t)data) & (ARCH_DMA_MINALIGN - 1))
337 dma = 0;
338 if (((uint32_t)len) & (ARCH_DMA_MINALIGN - 1))
339 dma = 0;
340 }
341
342 if (!dma || (len < MXSSSP_SMALL_TRANSFER)) {
343 writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_clr);
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200344#if !CONFIG_IS_ENABLED(DM_SPI)
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000345 return mxs_spi_xfer_pio(mxs_slave, data, len, write, flags);
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200346#else
347 return mxs_spi_xfer_pio(priv, data, len, write, flags);
348#endif
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000349 } else {
350 writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_set);
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200351#if !CONFIG_IS_ENABLED(DM_SPI)
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000352 return mxs_spi_xfer_dma(mxs_slave, data, len, write, flags);
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200353#else
354 return mxs_spi_xfer_dma(priv, data, len, write, flags);
355#endif
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000356 }
Marek Vasutec33de32011-11-08 23:18:14 +0000357}
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200358
359#if !CONFIG_IS_ENABLED(DM_SPI)
360int spi_cs_is_valid(unsigned int bus, unsigned int cs)
361{
362 /* MXS SPI: 4 ports and 3 chip selects maximum */
363 if (!mxs_ssp_bus_id_valid(bus) || cs > 2)
364 return 0;
365 else
366 return 1;
367}
368
369struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
370 unsigned int max_hz, unsigned int mode)
371{
372 struct mxs_spi_slave *mxs_slave;
373
374 if (!spi_cs_is_valid(bus, cs)) {
375 printf("mxs_spi: invalid bus %d / chip select %d\n", bus, cs);
376 return NULL;
377 }
378
379 mxs_slave = spi_alloc_slave(struct mxs_spi_slave, bus, cs);
380 if (!mxs_slave)
381 return NULL;
382
383 if (mxs_dma_init_channel(MXS_DMA_CHANNEL_AHB_APBH_SSP0 + bus))
384 goto err_init;
385
386 mxs_slave->max_khz = max_hz / 1000;
387 mxs_slave->mode = mode;
388 mxs_slave->regs = mxs_ssp_regs_by_bus(bus);
389
390 return &mxs_slave->slave;
391
392err_init:
393 free(mxs_slave);
394 return NULL;
395}
396
397void spi_free_slave(struct spi_slave *slave)
398{
399 struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
400
401 free(mxs_slave);
402}
403
404int spi_claim_bus(struct spi_slave *slave)
405{
406 struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
407 struct mxs_ssp_regs *ssp_regs = mxs_slave->regs;
408 u32 reg = 0;
409
410 mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
411
412 writel((slave->cs << MXS_SSP_CHIPSELECT_SHIFT) |
413 SSP_CTRL0_BUS_WIDTH_ONE_BIT,
414 &ssp_regs->hw_ssp_ctrl0);
415
416 reg = SSP_CTRL1_SSP_MODE_SPI | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS;
417 reg |= (mxs_slave->mode & SPI_CPOL) ? SSP_CTRL1_POLARITY : 0;
418 reg |= (mxs_slave->mode & SPI_CPHA) ? SSP_CTRL1_PHASE : 0;
419 writel(reg, &ssp_regs->hw_ssp_ctrl1);
420
421 writel(0, &ssp_regs->hw_ssp_cmd0);
422
423 mxs_set_ssp_busclock(slave->bus, mxs_slave->max_khz);
424
425 return 0;
426}
427
428void spi_release_bus(struct spi_slave *slave)
429{
430}
431
432#else /* CONFIG_DM_SPI */
433/* Base numbers of i.MX2[38] clk for ssp0 IP block */
434#define MXS_SSP_IMX23_CLKID_SSP0 33
435#define MXS_SSP_IMX28_CLKID_SSP0 46
436
437static int mxs_spi_probe(struct udevice *bus)
438{
439 struct mxs_spi_platdata *plat = dev_get_platdata(bus);
440 struct mxs_spi_priv *priv = dev_get_priv(bus);
441 int ret;
442
443 debug("%s: probe\n", __func__);
Lukasz Majewskiec0c81f2019-09-05 09:54:58 +0200444
445#if CONFIG_IS_ENABLED(OF_PLATDATA)
446 struct dtd_fsl_imx_spi *dtplat = &plat->dtplat;
447 struct phandle_1_arg *p1a = &dtplat->clocks[0];
448
449 priv->regs = (struct mxs_ssp_regs *)dtplat->reg[0];
450 priv->dma_channel = dtplat->dmas[1];
451 priv->clk_id = p1a->arg[0];
452 priv->max_freq = dtplat->spi_max_frequency;
453 plat->num_cs = dtplat->num_cs;
454
455 debug("OF_PLATDATA: regs: 0x%x max freq: %d clkid: %d\n",
456 (unsigned int)priv->regs, priv->max_freq, priv->clk_id);
457#else
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200458 priv->regs = (struct mxs_ssp_regs *)plat->base;
459 priv->max_freq = plat->frequency;
460
461 priv->dma_channel = plat->dma_id;
462 priv->clk_id = plat->clk_id;
Lukasz Majewskiec0c81f2019-09-05 09:54:58 +0200463#endif
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200464
Lukasz Majewskic2050e12019-09-05 09:54:57 +0200465 mxs_reset_block(&priv->regs->hw_ssp_ctrl0_reg);
466
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200467 ret = mxs_dma_init_channel(priv->dma_channel);
468 if (ret) {
469 printf("%s: DMA init channel error %d\n", __func__, ret);
470 return ret;
471 }
472
473 return 0;
474}
475
476static int mxs_spi_claim_bus(struct udevice *dev)
477{
478 struct udevice *bus = dev_get_parent(dev);
479 struct mxs_spi_priv *priv = dev_get_priv(bus);
480 struct mxs_ssp_regs *ssp_regs = priv->regs;
481 int cs = spi_chip_select(dev);
482
483 /*
484 * i.MX28 supports up to 3 CS (SSn0, SSn1, SSn2)
485 * To set them it uses following tuple (WAIT_FOR_IRQ,WAIT_FOR_CMD),
486 * where:
487 *
488 * WAIT_FOR_IRQ is bit 21 of HW_SSP_CTRL0
489 * WAIT_FOR_CMD is bit 20 (#defined as MXS_SSP_CHIPSELECT_SHIFT here) of
490 * HW_SSP_CTRL0
491 * SSn0 b00
492 * SSn1 b01
493 * SSn2 b10 (which require setting WAIT_FOR_IRQ)
494 *
495 * However, for now i.MX28 SPI driver will support up till 2 CSes
496 * (SSn0, and SSn1).
497 */
498
499 /* Ungate SSP clock and set active CS */
500 clrsetbits_le32(&ssp_regs->hw_ssp_ctrl0,
501 BIT(MXS_SSP_CHIPSELECT_SHIFT) |
502 SSP_CTRL0_CLKGATE, (cs << MXS_SSP_CHIPSELECT_SHIFT));
503
504 return 0;
505}
506
507static int mxs_spi_release_bus(struct udevice *dev)
508{
509 struct udevice *bus = dev_get_parent(dev);
510 struct mxs_spi_priv *priv = dev_get_priv(bus);
511 struct mxs_ssp_regs *ssp_regs = priv->regs;
512
513 /* Gate SSP clock */
514 setbits_le32(&ssp_regs->hw_ssp_ctrl0, SSP_CTRL0_CLKGATE);
515
516 return 0;
517}
518
519static int mxs_spi_set_speed(struct udevice *bus, uint speed)
520{
521 struct mxs_spi_priv *priv = dev_get_priv(bus);
522#ifdef CONFIG_MX28
523 int clkid = priv->clk_id - MXS_SSP_IMX28_CLKID_SSP0;
524#else /* CONFIG_MX23 */
525 int clkid = priv->clk_id - MXS_SSP_IMX23_CLKID_SSP0;
526#endif
527 if (speed > priv->max_freq)
528 speed = priv->max_freq;
529
530 debug("%s speed: %u [Hz] clkid: %d\n", __func__, speed, clkid);
531 mxs_set_ssp_busclock(clkid, speed / 1000);
532
533 return 0;
534}
535
536static int mxs_spi_set_mode(struct udevice *bus, uint mode)
537{
538 struct mxs_spi_priv *priv = dev_get_priv(bus);
539 struct mxs_ssp_regs *ssp_regs = priv->regs;
540 u32 reg;
541
542 priv->mode = mode;
543 debug("%s: mode 0x%x\n", __func__, mode);
544
545 reg = SSP_CTRL1_SSP_MODE_SPI | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS;
546 reg |= (priv->mode & SPI_CPOL) ? SSP_CTRL1_POLARITY : 0;
547 reg |= (priv->mode & SPI_CPHA) ? SSP_CTRL1_PHASE : 0;
548 writel(reg, &ssp_regs->hw_ssp_ctrl1);
549
550 /* Single bit SPI support */
551 writel(SSP_CTRL0_BUS_WIDTH_ONE_BIT, &ssp_regs->hw_ssp_ctrl0);
552
553 return 0;
554}
555
556static const struct dm_spi_ops mxs_spi_ops = {
557 .claim_bus = mxs_spi_claim_bus,
558 .release_bus = mxs_spi_release_bus,
559 .xfer = mxs_spi_xfer,
560 .set_speed = mxs_spi_set_speed,
561 .set_mode = mxs_spi_set_mode,
562 /*
563 * cs_info is not needed, since we require all chip selects to be
564 * in the device tree explicitly
565 */
566};
567
568#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
569static int mxs_ofdata_to_platdata(struct udevice *bus)
570{
571 struct mxs_spi_platdata *plat = bus->platdata;
572 u32 prop[2];
573 int ret;
574
575 plat->base = dev_read_addr(bus);
576 plat->frequency =
577 dev_read_u32_default(bus, "spi-max-frequency", 40000000);
578 plat->num_cs = dev_read_u32_default(bus, "num-cs", 2);
579
580 ret = dev_read_u32_array(bus, "dmas", prop, ARRAY_SIZE(prop));
581 if (ret) {
582 printf("%s: Reading 'dmas' property failed!\n", __func__);
583 return ret;
584 }
585 plat->dma_id = prop[1];
586
587 ret = dev_read_u32_array(bus, "clocks", prop, ARRAY_SIZE(prop));
588 if (ret) {
589 printf("%s: Reading 'clocks' property failed!\n", __func__);
590 return ret;
591 }
592 plat->clk_id = prop[1];
593
594 debug("%s: base=0x%x, max-frequency=%d num-cs=%d dma_id=%d clk_id=%d\n",
595 __func__, (uint)plat->base, plat->frequency, plat->num_cs,
596 plat->dma_id, plat->clk_id);
597
598 return 0;
599}
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200600
601static const struct udevice_id mxs_spi_ids[] = {
602 { .compatible = "fsl,imx23-spi" },
603 { .compatible = "fsl,imx28-spi" },
604 { }
605};
Lukasz Majewskiec0c81f2019-09-05 09:54:58 +0200606#endif
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200607
608U_BOOT_DRIVER(mxs_spi) = {
Lukasz Majewskiec0c81f2019-09-05 09:54:58 +0200609#ifdef CONFIG_MX28
610 .name = "fsl_imx28_spi",
611#else /* CONFIG_MX23 */
612 .name = "fsl_imx23_spi",
613#endif
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200614 .id = UCLASS_SPI,
615#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
616 .of_match = mxs_spi_ids,
617 .ofdata_to_platdata = mxs_ofdata_to_platdata,
618#endif
Lukasz Majewski3c50e012019-09-05 09:54:56 +0200619 .platdata_auto_alloc_size = sizeof(struct mxs_spi_platdata),
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200620 .ops = &mxs_spi_ops,
621 .priv_auto_alloc_size = sizeof(struct mxs_spi_priv),
622 .probe = mxs_spi_probe,
623};
624#endif