blob: 3a9756fbf1b15a8b4b1e0c2bc006dc0fbacd1cb5 [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>
60struct mxs_spi_platdata {
61 s32 frequency; /* Default clock frequency, -1 for none */
62 fdt_addr_t base; /* SPI IP block base address */
63 int num_cs; /* Number of CSes supported */
64 int dma_id; /* ID of the DMA channel */
65 int clk_id; /* ID of the SSP clock */
66};
67
68struct mxs_spi_priv {
69 struct mxs_ssp_regs *regs;
70 unsigned int dma_channel;
71 unsigned int max_freq;
72 unsigned int clk_id;
73 unsigned int mode;
74};
75#endif
76
77#if !CONFIG_IS_ENABLED(DM_SPI)
Marek Vasutccd4d5a2012-07-09 00:48:32 +000078static int mxs_spi_xfer_pio(struct mxs_spi_slave *slave,
79 char *data, int length, int write, unsigned long flags)
Marek Vasutec33de32011-11-08 23:18:14 +000080{
Otavio Salvador9c471142012-08-05 09:05:31 +000081 struct mxs_ssp_regs *ssp_regs = slave->regs;
Lukasz Majewskid99b0182019-06-19 17:31:07 +020082#else
83static int mxs_spi_xfer_pio(struct mxs_spi_priv *priv,
84 char *data, int length, int write,
85 unsigned long flags)
86{
87 struct mxs_ssp_regs *ssp_regs = priv->regs;
88#endif
Marek Vasutc7065fa2012-07-09 00:48:31 +000089
Marek Vasutec33de32011-11-08 23:18:14 +000090 if (flags & SPI_XFER_BEGIN)
91 mxs_spi_start_xfer(ssp_regs);
92
Marek Vasutccd4d5a2012-07-09 00:48:32 +000093 while (length--) {
Marek Vasutec33de32011-11-08 23:18:14 +000094 /* We transfer 1 byte */
Marek Vasutc96e78c2013-02-23 02:42:59 +000095#if defined(CONFIG_MX23)
96 writel(SSP_CTRL0_XFER_COUNT_MASK, &ssp_regs->hw_ssp_ctrl0_clr);
97 writel(1, &ssp_regs->hw_ssp_ctrl0_set);
98#elif defined(CONFIG_MX28)
Marek Vasutec33de32011-11-08 23:18:14 +000099 writel(1, &ssp_regs->hw_ssp_xfer_size);
Marek Vasutc96e78c2013-02-23 02:42:59 +0000100#endif
Marek Vasutec33de32011-11-08 23:18:14 +0000101
Marek Vasutccd4d5a2012-07-09 00:48:32 +0000102 if ((flags & SPI_XFER_END) && !length)
Marek Vasutec33de32011-11-08 23:18:14 +0000103 mxs_spi_end_xfer(ssp_regs);
104
Marek Vasutc7065fa2012-07-09 00:48:31 +0000105 if (write)
Marek Vasutec33de32011-11-08 23:18:14 +0000106 writel(SSP_CTRL0_READ, &ssp_regs->hw_ssp_ctrl0_clr);
107 else
108 writel(SSP_CTRL0_READ, &ssp_regs->hw_ssp_ctrl0_set);
109
110 writel(SSP_CTRL0_RUN, &ssp_regs->hw_ssp_ctrl0_set);
111
Otavio Salvadorfa7a51c2012-08-13 09:53:12 +0000112 if (mxs_wait_mask_set(&ssp_regs->hw_ssp_ctrl0_reg,
Marek Vasutec33de32011-11-08 23:18:14 +0000113 SSP_CTRL0_RUN, MXS_SPI_MAX_TIMEOUT)) {
114 printf("MXS SPI: Timeout waiting for start\n");
Fabio Estevamd9fb6a42012-03-18 17:23:35 +0000115 return -ETIMEDOUT;
Marek Vasutec33de32011-11-08 23:18:14 +0000116 }
117
Marek Vasutc7065fa2012-07-09 00:48:31 +0000118 if (write)
119 writel(*data++, &ssp_regs->hw_ssp_data);
Marek Vasutec33de32011-11-08 23:18:14 +0000120
121 writel(SSP_CTRL0_DATA_XFER, &ssp_regs->hw_ssp_ctrl0_set);
122
Marek Vasutc7065fa2012-07-09 00:48:31 +0000123 if (!write) {
Otavio Salvadorfa7a51c2012-08-13 09:53:12 +0000124 if (mxs_wait_mask_clr(&ssp_regs->hw_ssp_status_reg,
Marek Vasutec33de32011-11-08 23:18:14 +0000125 SSP_STATUS_FIFO_EMPTY, MXS_SPI_MAX_TIMEOUT)) {
126 printf("MXS SPI: Timeout waiting for data\n");
Fabio Estevamd9fb6a42012-03-18 17:23:35 +0000127 return -ETIMEDOUT;
Marek Vasutec33de32011-11-08 23:18:14 +0000128 }
129
Marek Vasutc7065fa2012-07-09 00:48:31 +0000130 *data = readl(&ssp_regs->hw_ssp_data);
131 data++;
Marek Vasutec33de32011-11-08 23:18:14 +0000132 }
133
Otavio Salvadorfa7a51c2012-08-13 09:53:12 +0000134 if (mxs_wait_mask_clr(&ssp_regs->hw_ssp_ctrl0_reg,
Marek Vasutec33de32011-11-08 23:18:14 +0000135 SSP_CTRL0_RUN, MXS_SPI_MAX_TIMEOUT)) {
136 printf("MXS SPI: Timeout waiting for finish\n");
Fabio Estevamd9fb6a42012-03-18 17:23:35 +0000137 return -ETIMEDOUT;
Marek Vasutec33de32011-11-08 23:18:14 +0000138 }
139 }
140
141 return 0;
Marek Vasutccd4d5a2012-07-09 00:48:32 +0000142}
143
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200144#if !CONFIG_IS_ENABLED(DM_SPI)
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000145static int mxs_spi_xfer_dma(struct mxs_spi_slave *slave,
146 char *data, int length, int write, unsigned long flags)
147{
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200148 struct mxs_ssp_regs *ssp_regs = slave->regs;
149#else
150static int mxs_spi_xfer_dma(struct mxs_spi_priv *priv,
151 char *data, int length, int write,
152 unsigned long flags)
153{ struct mxs_ssp_regs *ssp_regs = priv->regs;
154#endif
Marek Vasut2c432142012-08-21 16:17:27 +0000155 const int xfer_max_sz = 0xff00;
156 const int desc_count = DIV_ROUND_UP(length, xfer_max_sz) + 1;
Marek Vasut2c432142012-08-21 16:17:27 +0000157 struct mxs_dma_desc *dp;
158 uint32_t ctrl0;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000159 uint32_t cache_data_count;
Marek Vasut88d15552012-08-31 16:07:59 +0000160 const uint32_t dstart = (uint32_t)data;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000161 int dmach;
Marek Vasut2c432142012-08-21 16:17:27 +0000162 int tl;
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000163 int ret = 0;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000164
Marek Vasutc96e78c2013-02-23 02:42:59 +0000165#if defined(CONFIG_MX23)
166 const int mxs_spi_pio_words = 1;
167#elif defined(CONFIG_MX28)
168 const int mxs_spi_pio_words = 4;
169#endif
170
Marek Vasut2c432142012-08-21 16:17:27 +0000171 ALLOC_CACHE_ALIGN_BUFFER(struct mxs_dma_desc, desc, desc_count);
172
173 memset(desc, 0, sizeof(struct mxs_dma_desc) * desc_count);
174
175 ctrl0 = readl(&ssp_regs->hw_ssp_ctrl0);
176 ctrl0 |= SSP_CTRL0_DATA_XFER;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000177
178 if (flags & SPI_XFER_BEGIN)
179 ctrl0 |= SSP_CTRL0_LOCK_CS;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000180 if (!write)
181 ctrl0 |= SSP_CTRL0_READ;
182
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000183 if (length % ARCH_DMA_MINALIGN)
184 cache_data_count = roundup(length, ARCH_DMA_MINALIGN);
185 else
186 cache_data_count = length;
187
Marek Vasut88d15552012-08-31 16:07:59 +0000188 /* Flush data to DRAM so DMA can pick them up */
Marek Vasut2c432142012-08-21 16:17:27 +0000189 if (write)
Marek Vasut88d15552012-08-31 16:07:59 +0000190 flush_dcache_range(dstart, dstart + cache_data_count);
191
192 /* Invalidate the area, so no writeback into the RAM races with DMA */
193 invalidate_dcache_range(dstart, dstart + cache_data_count);
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000194
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200195#if !CONFIG_IS_ENABLED(DM_SPI)
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000196 dmach = MXS_DMA_CHANNEL_AHB_APBH_SSP0 + slave->slave.bus;
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200197#else
198 dmach = priv->dma_channel;
199#endif
Marek Vasut2c432142012-08-21 16:17:27 +0000200
201 dp = desc;
202 while (length) {
203 dp->address = (dma_addr_t)dp;
204 dp->cmd.address = (dma_addr_t)data;
205
206 /*
207 * This is correct, even though it does indeed look insane.
208 * I hereby have to, wholeheartedly, thank Freescale Inc.,
209 * for always inventing insane hardware and keeping me busy
210 * and employed ;-)
211 */
212 if (write)
213 dp->cmd.data = MXS_DMA_DESC_COMMAND_DMA_READ;
214 else
215 dp->cmd.data = MXS_DMA_DESC_COMMAND_DMA_WRITE;
216
217 /*
218 * The DMA controller can transfer large chunks (64kB) at
219 * time by setting the transfer length to 0. Setting tl to
220 * 0x10000 will overflow below and make .data contain 0.
221 * Otherwise, 0xff00 is the transfer maximum.
222 */
223 if (length >= 0x10000)
224 tl = 0x10000;
225 else
226 tl = min(length, xfer_max_sz);
227
228 dp->cmd.data |=
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000229 ((tl & 0xffff) << MXS_DMA_DESC_BYTES_OFFSET) |
Marek Vasutc96e78c2013-02-23 02:42:59 +0000230 (mxs_spi_pio_words << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
Marek Vasut2c432142012-08-21 16:17:27 +0000231 MXS_DMA_DESC_HALT_ON_TERMINATE |
232 MXS_DMA_DESC_TERMINATE_FLUSH;
Marek Vasut2c432142012-08-21 16:17:27 +0000233
234 data += tl;
235 length -= tl;
236
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000237 if (!length) {
238 dp->cmd.data |= MXS_DMA_DESC_IRQ | MXS_DMA_DESC_DEC_SEM;
239
240 if (flags & SPI_XFER_END) {
241 ctrl0 &= ~SSP_CTRL0_LOCK_CS;
242 ctrl0 |= SSP_CTRL0_IGNORE_CRC;
243 }
244 }
245
246 /*
Marek Vasutc96e78c2013-02-23 02:42:59 +0000247 * Write CTRL0, CMD0, CMD1 and XFER_SIZE registers in
248 * case of MX28, write only CTRL0 in case of MX23 due
249 * to the difference in register layout. It is utterly
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000250 * essential that the XFER_SIZE register is written on
251 * a per-descriptor basis with the same size as is the
252 * descriptor!
253 */
254 dp->cmd.pio_words[0] = ctrl0;
Marek Vasutc96e78c2013-02-23 02:42:59 +0000255#ifdef CONFIG_MX28
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000256 dp->cmd.pio_words[1] = 0;
257 dp->cmd.pio_words[2] = 0;
258 dp->cmd.pio_words[3] = tl;
Marek Vasutc96e78c2013-02-23 02:42:59 +0000259#endif
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000260
Marek Vasut2c432142012-08-21 16:17:27 +0000261 mxs_dma_desc_append(dmach, dp);
262
263 dp++;
264 }
265
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000266 if (mxs_dma_go(dmach))
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000267 ret = -EINVAL;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000268
269 /* The data arrived into DRAM, invalidate cache over them */
Marek Vasut88d15552012-08-31 16:07:59 +0000270 if (!write)
271 invalidate_dcache_range(dstart, dstart + cache_data_count);
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000272
Marek Vasute9f7eaf2012-08-31 16:08:00 +0000273 return ret;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000274}
275
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200276#if !CONFIG_IS_ENABLED(DM_SPI)
Marek Vasutccd4d5a2012-07-09 00:48:32 +0000277int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
278 const void *dout, void *din, unsigned long flags)
279{
280 struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
Otavio Salvador9c471142012-08-05 09:05:31 +0000281 struct mxs_ssp_regs *ssp_regs = mxs_slave->regs;
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200282#else
283int mxs_spi_xfer(struct udevice *dev, unsigned int bitlen,
284 const void *dout, void *din, unsigned long flags)
285{
286 struct udevice *bus = dev_get_parent(dev);
287 struct mxs_spi_priv *priv = dev_get_priv(bus);
288 struct mxs_ssp_regs *ssp_regs = priv->regs;
289#endif
Marek Vasutccd4d5a2012-07-09 00:48:32 +0000290 int len = bitlen / 8;
291 char dummy;
292 int write = 0;
293 char *data = NULL;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000294 int dma = 1;
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000295
Marek Vasutccd4d5a2012-07-09 00:48:32 +0000296 if (bitlen == 0) {
297 if (flags & SPI_XFER_END) {
298 din = (void *)&dummy;
299 len = 1;
300 } else
301 return 0;
302 }
303
304 /* Half-duplex only */
305 if (din && dout)
306 return -EINVAL;
307 /* No data */
308 if (!din && !dout)
309 return 0;
310
311 if (dout) {
312 data = (char *)dout;
313 write = 1;
314 } else if (din) {
315 data = (char *)din;
316 write = 0;
317 }
318
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000319 /*
320 * Check for alignment, if the buffer is aligned, do DMA transfer,
321 * PIO otherwise. This is a temporary workaround until proper bounce
322 * buffer is in place.
323 */
324 if (dma) {
325 if (((uint32_t)data) & (ARCH_DMA_MINALIGN - 1))
326 dma = 0;
327 if (((uint32_t)len) & (ARCH_DMA_MINALIGN - 1))
328 dma = 0;
329 }
330
331 if (!dma || (len < MXSSSP_SMALL_TRANSFER)) {
332 writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_clr);
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200333#if !CONFIG_IS_ENABLED(DM_SPI)
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000334 return mxs_spi_xfer_pio(mxs_slave, data, len, write, flags);
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200335#else
336 return mxs_spi_xfer_pio(priv, data, len, write, flags);
337#endif
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000338 } else {
339 writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_set);
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200340#if !CONFIG_IS_ENABLED(DM_SPI)
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000341 return mxs_spi_xfer_dma(mxs_slave, data, len, write, flags);
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200342#else
343 return mxs_spi_xfer_dma(priv, data, len, write, flags);
344#endif
Marek Vasut7c5e6f72012-07-09 00:48:33 +0000345 }
Marek Vasutec33de32011-11-08 23:18:14 +0000346}
Lukasz Majewskid99b0182019-06-19 17:31:07 +0200347
348#if !CONFIG_IS_ENABLED(DM_SPI)
349int spi_cs_is_valid(unsigned int bus, unsigned int cs)
350{
351 /* MXS SPI: 4 ports and 3 chip selects maximum */
352 if (!mxs_ssp_bus_id_valid(bus) || cs > 2)
353 return 0;
354 else
355 return 1;
356}
357
358struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
359 unsigned int max_hz, unsigned int mode)
360{
361 struct mxs_spi_slave *mxs_slave;
362
363 if (!spi_cs_is_valid(bus, cs)) {
364 printf("mxs_spi: invalid bus %d / chip select %d\n", bus, cs);
365 return NULL;
366 }
367
368 mxs_slave = spi_alloc_slave(struct mxs_spi_slave, bus, cs);
369 if (!mxs_slave)
370 return NULL;
371
372 if (mxs_dma_init_channel(MXS_DMA_CHANNEL_AHB_APBH_SSP0 + bus))
373 goto err_init;
374
375 mxs_slave->max_khz = max_hz / 1000;
376 mxs_slave->mode = mode;
377 mxs_slave->regs = mxs_ssp_regs_by_bus(bus);
378
379 return &mxs_slave->slave;
380
381err_init:
382 free(mxs_slave);
383 return NULL;
384}
385
386void spi_free_slave(struct spi_slave *slave)
387{
388 struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
389
390 free(mxs_slave);
391}
392
393int spi_claim_bus(struct spi_slave *slave)
394{
395 struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
396 struct mxs_ssp_regs *ssp_regs = mxs_slave->regs;
397 u32 reg = 0;
398
399 mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
400
401 writel((slave->cs << MXS_SSP_CHIPSELECT_SHIFT) |
402 SSP_CTRL0_BUS_WIDTH_ONE_BIT,
403 &ssp_regs->hw_ssp_ctrl0);
404
405 reg = SSP_CTRL1_SSP_MODE_SPI | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS;
406 reg |= (mxs_slave->mode & SPI_CPOL) ? SSP_CTRL1_POLARITY : 0;
407 reg |= (mxs_slave->mode & SPI_CPHA) ? SSP_CTRL1_PHASE : 0;
408 writel(reg, &ssp_regs->hw_ssp_ctrl1);
409
410 writel(0, &ssp_regs->hw_ssp_cmd0);
411
412 mxs_set_ssp_busclock(slave->bus, mxs_slave->max_khz);
413
414 return 0;
415}
416
417void spi_release_bus(struct spi_slave *slave)
418{
419}
420
421#else /* CONFIG_DM_SPI */
422/* Base numbers of i.MX2[38] clk for ssp0 IP block */
423#define MXS_SSP_IMX23_CLKID_SSP0 33
424#define MXS_SSP_IMX28_CLKID_SSP0 46
425
426static int mxs_spi_probe(struct udevice *bus)
427{
428 struct mxs_spi_platdata *plat = dev_get_platdata(bus);
429 struct mxs_spi_priv *priv = dev_get_priv(bus);
430 int ret;
431
432 debug("%s: probe\n", __func__);
433 priv->regs = (struct mxs_ssp_regs *)plat->base;
434 priv->max_freq = plat->frequency;
435
436 priv->dma_channel = plat->dma_id;
437 priv->clk_id = plat->clk_id;
438
439 ret = mxs_dma_init_channel(priv->dma_channel);
440 if (ret) {
441 printf("%s: DMA init channel error %d\n", __func__, ret);
442 return ret;
443 }
444
445 return 0;
446}
447
448static int mxs_spi_claim_bus(struct udevice *dev)
449{
450 struct udevice *bus = dev_get_parent(dev);
451 struct mxs_spi_priv *priv = dev_get_priv(bus);
452 struct mxs_ssp_regs *ssp_regs = priv->regs;
453 int cs = spi_chip_select(dev);
454
455 /*
456 * i.MX28 supports up to 3 CS (SSn0, SSn1, SSn2)
457 * To set them it uses following tuple (WAIT_FOR_IRQ,WAIT_FOR_CMD),
458 * where:
459 *
460 * WAIT_FOR_IRQ is bit 21 of HW_SSP_CTRL0
461 * WAIT_FOR_CMD is bit 20 (#defined as MXS_SSP_CHIPSELECT_SHIFT here) of
462 * HW_SSP_CTRL0
463 * SSn0 b00
464 * SSn1 b01
465 * SSn2 b10 (which require setting WAIT_FOR_IRQ)
466 *
467 * However, for now i.MX28 SPI driver will support up till 2 CSes
468 * (SSn0, and SSn1).
469 */
470
471 /* Ungate SSP clock and set active CS */
472 clrsetbits_le32(&ssp_regs->hw_ssp_ctrl0,
473 BIT(MXS_SSP_CHIPSELECT_SHIFT) |
474 SSP_CTRL0_CLKGATE, (cs << MXS_SSP_CHIPSELECT_SHIFT));
475
476 return 0;
477}
478
479static int mxs_spi_release_bus(struct udevice *dev)
480{
481 struct udevice *bus = dev_get_parent(dev);
482 struct mxs_spi_priv *priv = dev_get_priv(bus);
483 struct mxs_ssp_regs *ssp_regs = priv->regs;
484
485 /* Gate SSP clock */
486 setbits_le32(&ssp_regs->hw_ssp_ctrl0, SSP_CTRL0_CLKGATE);
487
488 return 0;
489}
490
491static int mxs_spi_set_speed(struct udevice *bus, uint speed)
492{
493 struct mxs_spi_priv *priv = dev_get_priv(bus);
494#ifdef CONFIG_MX28
495 int clkid = priv->clk_id - MXS_SSP_IMX28_CLKID_SSP0;
496#else /* CONFIG_MX23 */
497 int clkid = priv->clk_id - MXS_SSP_IMX23_CLKID_SSP0;
498#endif
499 if (speed > priv->max_freq)
500 speed = priv->max_freq;
501
502 debug("%s speed: %u [Hz] clkid: %d\n", __func__, speed, clkid);
503 mxs_set_ssp_busclock(clkid, speed / 1000);
504
505 return 0;
506}
507
508static int mxs_spi_set_mode(struct udevice *bus, uint mode)
509{
510 struct mxs_spi_priv *priv = dev_get_priv(bus);
511 struct mxs_ssp_regs *ssp_regs = priv->regs;
512 u32 reg;
513
514 priv->mode = mode;
515 debug("%s: mode 0x%x\n", __func__, mode);
516
517 reg = SSP_CTRL1_SSP_MODE_SPI | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS;
518 reg |= (priv->mode & SPI_CPOL) ? SSP_CTRL1_POLARITY : 0;
519 reg |= (priv->mode & SPI_CPHA) ? SSP_CTRL1_PHASE : 0;
520 writel(reg, &ssp_regs->hw_ssp_ctrl1);
521
522 /* Single bit SPI support */
523 writel(SSP_CTRL0_BUS_WIDTH_ONE_BIT, &ssp_regs->hw_ssp_ctrl0);
524
525 return 0;
526}
527
528static const struct dm_spi_ops mxs_spi_ops = {
529 .claim_bus = mxs_spi_claim_bus,
530 .release_bus = mxs_spi_release_bus,
531 .xfer = mxs_spi_xfer,
532 .set_speed = mxs_spi_set_speed,
533 .set_mode = mxs_spi_set_mode,
534 /*
535 * cs_info is not needed, since we require all chip selects to be
536 * in the device tree explicitly
537 */
538};
539
540#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
541static int mxs_ofdata_to_platdata(struct udevice *bus)
542{
543 struct mxs_spi_platdata *plat = bus->platdata;
544 u32 prop[2];
545 int ret;
546
547 plat->base = dev_read_addr(bus);
548 plat->frequency =
549 dev_read_u32_default(bus, "spi-max-frequency", 40000000);
550 plat->num_cs = dev_read_u32_default(bus, "num-cs", 2);
551
552 ret = dev_read_u32_array(bus, "dmas", prop, ARRAY_SIZE(prop));
553 if (ret) {
554 printf("%s: Reading 'dmas' property failed!\n", __func__);
555 return ret;
556 }
557 plat->dma_id = prop[1];
558
559 ret = dev_read_u32_array(bus, "clocks", prop, ARRAY_SIZE(prop));
560 if (ret) {
561 printf("%s: Reading 'clocks' property failed!\n", __func__);
562 return ret;
563 }
564 plat->clk_id = prop[1];
565
566 debug("%s: base=0x%x, max-frequency=%d num-cs=%d dma_id=%d clk_id=%d\n",
567 __func__, (uint)plat->base, plat->frequency, plat->num_cs,
568 plat->dma_id, plat->clk_id);
569
570 return 0;
571}
572#endif
573
574static const struct udevice_id mxs_spi_ids[] = {
575 { .compatible = "fsl,imx23-spi" },
576 { .compatible = "fsl,imx28-spi" },
577 { }
578};
579
580U_BOOT_DRIVER(mxs_spi) = {
581 .name = "mxs_spi",
582 .id = UCLASS_SPI,
583#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
584 .of_match = mxs_spi_ids,
585 .ofdata_to_platdata = mxs_ofdata_to_platdata,
586#endif
587 .priv_auto_alloc_size = sizeof(struct mxs_spi_platdata),
588 .ops = &mxs_spi_ops,
589 .priv_auto_alloc_size = sizeof(struct mxs_spi_priv),
590 .probe = mxs_spi_probe,
591};
592#endif