blob: d3b21faf6b1f812c1f089cf9f8964f732385c69c [file] [log] [blame]
Fabio Estevam79650592018-05-02 16:18:27 -03001// SPDX-License-Identifier: GPL-2.0+
2// Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
3// Copyright (C) 2008 Juergen Beisert
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07004
5#include <linux/clk.h>
6#include <linux/completion.h>
7#include <linux/delay.h>
Robin Gongf62cacc2014-09-11 09:18:44 +08008#include <linux/dmaengine.h>
9#include <linux/dma-mapping.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070010#include <linux/err.h>
11#include <linux/gpio.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070012#include <linux/interrupt.h>
13#include <linux/io.h>
14#include <linux/irq.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070019#include <linux/spi/spi.h>
20#include <linux/spi/spi_bitbang.h>
21#include <linux/types.h>
Shawn Guo22a85e42011-07-10 01:16:41 +080022#include <linux/of.h>
23#include <linux/of_device.h>
24#include <linux/of_gpio.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070025
Robin Gongf62cacc2014-09-11 09:18:44 +080026#include <linux/platform_data/dma-imx.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020027#include <linux/platform_data/spi-imx.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070028
29#define DRIVER_NAME "spi_imx"
30
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
36
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 */
jiada wang71abd292017-09-05 14:12:32 +090040#define MXC_INT_RDR BIT(4) /* Receive date threshold interrupt */
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070041
Robin Gongf62cacc2014-09-11 09:18:44 +080042/* The maximum bytes that a sdma BD can transfer.*/
43#define MAX_SDMA_BD_BYTES (1 << 15)
jiada wang1673c812017-08-10 13:50:08 +090044#define MX51_ECSPI_CTRL_MAX_BURST 512
jiada wang71abd292017-09-05 14:12:32 +090045/* The maximum bytes that IMX53_ECSPI can transfer in slave mode.*/
46#define MX53_MAX_TRANSFER_BYTES 512
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070047
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020048enum spi_imx_devtype {
Shawn Guo04ee5852011-07-10 01:16:39 +080049 IMX1_CSPI,
50 IMX21_CSPI,
51 IMX27_CSPI,
52 IMX31_CSPI,
53 IMX35_CSPI, /* CSPI on all i.mx except above */
jiada wang26e4bb82017-06-08 14:16:01 +090054 IMX51_ECSPI, /* ECSPI on i.mx51 */
55 IMX53_ECSPI, /* ECSPI on i.mx53 and later */
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020056};
57
58struct spi_imx_data;
59
60struct spi_imx_devtype_data {
61 void (*intctrl)(struct spi_imx_data *, int);
Sascha Hauerd52345b2017-06-02 07:38:01 +020062 int (*config)(struct spi_device *);
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020063 void (*trigger)(struct spi_imx_data *);
64 int (*rx_available)(struct spi_imx_data *);
Uwe Kleine-König1723e662010-09-10 09:19:18 +020065 void (*reset)(struct spi_imx_data *);
jiada wang71abd292017-09-05 14:12:32 +090066 void (*disable)(struct spi_imx_data *);
jiada wangfd8d4e22017-06-08 14:16:00 +090067 bool has_dmamode;
jiada wang71abd292017-09-05 14:12:32 +090068 bool has_slavemode;
jiada wangfd8d4e22017-06-08 14:16:00 +090069 unsigned int fifo_size;
jiada wang1673c812017-08-10 13:50:08 +090070 bool dynamic_burst;
Shawn Guo04ee5852011-07-10 01:16:39 +080071 enum spi_imx_devtype devtype;
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020072};
73
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070074struct spi_imx_data {
75 struct spi_bitbang bitbang;
Sascha Hauer6aa800c2016-02-17 14:28:48 +010076 struct device *dev;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070077
78 struct completion xfer_done;
Uwe Kleine-Königcc4d22a2012-03-29 21:54:18 +020079 void __iomem *base;
Anton Bondarenkof12ae172016-02-24 09:20:29 +010080 unsigned long base_phys;
81
Sascha Haueraa29d8402012-03-07 09:30:22 +010082 struct clk *clk_per;
83 struct clk *clk_ipg;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070084 unsigned long spi_clk;
Anton Bondarenko4bfe9272016-02-19 08:43:03 +010085 unsigned int spi_bus_clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070086
Sascha Hauerd52345b2017-06-02 07:38:01 +020087 unsigned int speed_hz;
88 unsigned int bits_per_word;
Leif Middelschultef72efa72017-04-23 21:19:58 +020089 unsigned int spi_drctl;
Anton Bondarenkof12ae172016-02-24 09:20:29 +010090
jiada wang1673c812017-08-10 13:50:08 +090091 unsigned int count, remainder;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070092 void (*tx)(struct spi_imx_data *);
93 void (*rx)(struct spi_imx_data *);
94 void *rx_buf;
95 const void *tx_buf;
96 unsigned int txfifo; /* number of words pushed in tx FIFO */
jiada wang1673c812017-08-10 13:50:08 +090097 unsigned int dynamic_burst, read_u32;
98 unsigned int word_mask;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070099
jiada wang71abd292017-09-05 14:12:32 +0900100 /* Slave mode */
101 bool slave_mode;
102 bool slave_aborted;
103 unsigned int slave_burst;
104
Robin Gongf62cacc2014-09-11 09:18:44 +0800105 /* DMA */
Robin Gongf62cacc2014-09-11 09:18:44 +0800106 bool usedma;
Anton Bondarenko0dfbaa82015-12-05 17:57:01 +0100107 u32 wml;
Robin Gongf62cacc2014-09-11 09:18:44 +0800108 struct completion dma_rx_completion;
109 struct completion dma_tx_completion;
110
Uwe Kleine-König80023cb2012-05-21 21:49:35 +0200111 const struct spi_imx_devtype_data *devtype_data;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700112};
113
Shawn Guo04ee5852011-07-10 01:16:39 +0800114static inline int is_imx27_cspi(struct spi_imx_data *d)
115{
116 return d->devtype_data->devtype == IMX27_CSPI;
117}
118
119static inline int is_imx35_cspi(struct spi_imx_data *d)
120{
121 return d->devtype_data->devtype == IMX35_CSPI;
122}
123
Anton Bondarenkof8a87612015-12-05 17:57:02 +0100124static inline int is_imx51_ecspi(struct spi_imx_data *d)
125{
126 return d->devtype_data->devtype == IMX51_ECSPI;
127}
128
jiada wang26e4bb82017-06-08 14:16:01 +0900129static inline int is_imx53_ecspi(struct spi_imx_data *d)
130{
131 return d->devtype_data->devtype == IMX53_ECSPI;
132}
133
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700134#define MXC_SPI_BUF_RX(type) \
135static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \
136{ \
137 unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \
138 \
139 if (spi_imx->rx_buf) { \
140 *(type *)spi_imx->rx_buf = val; \
141 spi_imx->rx_buf += sizeof(type); \
142 } \
143}
144
145#define MXC_SPI_BUF_TX(type) \
146static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \
147{ \
148 type val = 0; \
149 \
150 if (spi_imx->tx_buf) { \
151 val = *(type *)spi_imx->tx_buf; \
152 spi_imx->tx_buf += sizeof(type); \
153 } \
154 \
155 spi_imx->count -= sizeof(type); \
156 \
157 writel(val, spi_imx->base + MXC_CSPITXDATA); \
158}
159
160MXC_SPI_BUF_RX(u8)
161MXC_SPI_BUF_TX(u8)
162MXC_SPI_BUF_RX(u16)
163MXC_SPI_BUF_TX(u16)
164MXC_SPI_BUF_RX(u32)
165MXC_SPI_BUF_TX(u32)
166
167/* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
168 * (which is currently not the case in this driver)
169 */
170static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
171 256, 384, 512, 768, 1024};
172
173/* MX21, MX27 */
174static unsigned int spi_imx_clkdiv_1(unsigned int fin,
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100175 unsigned int fspi, unsigned int max, unsigned int *fres)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700176{
Shawn Guo04ee5852011-07-10 01:16:39 +0800177 int i;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700178
179 for (i = 2; i < max; i++)
180 if (fspi * mxc_clkdivs[i] >= fin)
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100181 break;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700182
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100183 *fres = fin / mxc_clkdivs[i];
184 return i;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700185}
186
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200187/* MX1, MX31, MX35, MX51 CSPI */
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700188static unsigned int spi_imx_clkdiv_2(unsigned int fin,
Martin Kaiser2636ba82016-09-01 22:38:40 +0200189 unsigned int fspi, unsigned int *fres)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700190{
191 int i, div = 4;
192
193 for (i = 0; i < 7; i++) {
194 if (fspi * div >= fin)
Martin Kaiser2636ba82016-09-01 22:38:40 +0200195 goto out;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700196 div <<= 1;
197 }
198
Martin Kaiser2636ba82016-09-01 22:38:40 +0200199out:
200 *fres = fin / div;
201 return i;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700202}
203
Sascha Hauer2e312f62017-06-02 07:38:04 +0200204static int spi_imx_bytes_per_word(const int bits_per_word)
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100205{
Sascha Hauer2e312f62017-06-02 07:38:04 +0200206 return DIV_ROUND_UP(bits_per_word, BITS_PER_BYTE);
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100207}
208
Robin Gongf62cacc2014-09-11 09:18:44 +0800209static bool spi_imx_can_dma(struct spi_master *master, struct spi_device *spi,
210 struct spi_transfer *transfer)
211{
212 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
Sascha Hauer2e312f62017-06-02 07:38:04 +0200213 unsigned int bytes_per_word, i;
Robin Gongf62cacc2014-09-11 09:18:44 +0800214
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100215 if (!master->dma_rx)
216 return false;
217
jiada wang71abd292017-09-05 14:12:32 +0900218 if (spi_imx->slave_mode)
219 return false;
220
Sascha Hauer2e312f62017-06-02 07:38:04 +0200221 bytes_per_word = spi_imx_bytes_per_word(transfer->bits_per_word);
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100222
Sascha Hauer2e312f62017-06-02 07:38:04 +0200223 if (bytes_per_word != 1 && bytes_per_word != 2 && bytes_per_word != 4)
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100224 return false;
225
jiada wangfd8d4e22017-06-08 14:16:00 +0900226 for (i = spi_imx->devtype_data->fifo_size / 2; i > 0; i--) {
Sascha Hauer2e312f62017-06-02 07:38:04 +0200227 if (!(transfer->len % (i * bytes_per_word)))
Jiada Wang66459c52017-01-06 04:22:18 -0800228 break;
229 }
230
231 if (i == 0)
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100232 return false;
233
Jiada Wang66459c52017-01-06 04:22:18 -0800234 spi_imx->wml = i;
jiada wang1673c812017-08-10 13:50:08 +0900235 spi_imx->dynamic_burst = 0;
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100236
237 return true;
Robin Gongf62cacc2014-09-11 09:18:44 +0800238}
239
Shawn Guo66de7572011-07-10 01:16:37 +0800240#define MX51_ECSPI_CTRL 0x08
241#define MX51_ECSPI_CTRL_ENABLE (1 << 0)
242#define MX51_ECSPI_CTRL_XCH (1 << 2)
Robin Gongf62cacc2014-09-11 09:18:44 +0800243#define MX51_ECSPI_CTRL_SMC (1 << 3)
Shawn Guo66de7572011-07-10 01:16:37 +0800244#define MX51_ECSPI_CTRL_MODE_MASK (0xf << 4)
Leif Middelschultef72efa72017-04-23 21:19:58 +0200245#define MX51_ECSPI_CTRL_DRCTL(drctl) ((drctl) << 16)
Shawn Guo66de7572011-07-10 01:16:37 +0800246#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
jiada wang1673c812017-08-10 13:50:08 +0900250#define MX51_ECSPI_CTRL_BL_MASK (0xfff << 20)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200251
Shawn Guo66de7572011-07-10 01:16:37 +0800252#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))
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200257#define MX51_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs) + 20))
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200258
Shawn Guo66de7572011-07-10 01:16:37 +0800259#define MX51_ECSPI_INT 0x10
260#define MX51_ECSPI_INT_TEEN (1 << 0)
261#define MX51_ECSPI_INT_RREN (1 << 3)
jiada wang71abd292017-09-05 14:12:32 +0900262#define MX51_ECSPI_INT_RDREN (1 << 4)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200263
Robin Gongf62cacc2014-09-11 09:18:44 +0800264#define MX51_ECSPI_DMA 0x14
Sascha Hauerd629c2a2016-02-24 09:20:31 +0100265#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)
Robin Gongf62cacc2014-09-11 09:18:44 +0800268
Sascha Hauer2b0fd062016-02-24 09:20:27 +0100269#define MX51_ECSPI_DMA_TEDEN (1 << 7)
270#define MX51_ECSPI_DMA_RXDEN (1 << 23)
271#define MX51_ECSPI_DMA_RXTDEN (1 << 31)
Robin Gongf62cacc2014-09-11 09:18:44 +0800272
Shawn Guo66de7572011-07-10 01:16:37 +0800273#define MX51_ECSPI_STAT 0x18
274#define MX51_ECSPI_STAT_RR (1 << 3)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200275
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200276#define MX51_ECSPI_TESTREG 0x20
277#define MX51_ECSPI_TESTREG_LBC BIT(31)
278
jiada wang1673c812017-08-10 13:50:08 +0900279static void spi_imx_buf_rx_swap_u32(struct spi_imx_data *spi_imx)
280{
281 unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA);
Arnd Bergmann5904c9d2017-08-23 15:34:43 +0200282#ifdef __LITTLE_ENDIAN
jiada wang1673c812017-08-10 13:50:08 +0900283 unsigned int bytes_per_word;
Arnd Bergmann5904c9d2017-08-23 15:34:43 +0200284#endif
jiada wang1673c812017-08-10 13:50:08 +0900285
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);
293#endif
294 val &= spi_imx->word_mask;
295 *(u32 *)spi_imx->rx_buf = val;
296 spi_imx->rx_buf += sizeof(u32);
297 }
298}
299
300static void spi_imx_buf_rx_swap(struct spi_imx_data *spi_imx)
301{
302 unsigned int bytes_per_word;
303
304 bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
305 if (spi_imx->read_u32) {
306 spi_imx_buf_rx_swap_u32(spi_imx);
307 return;
308 }
309
310 if (bytes_per_word == 1)
311 spi_imx_buf_rx_u8(spi_imx);
312 else if (bytes_per_word == 2)
313 spi_imx_buf_rx_u16(spi_imx);
314}
315
316static void spi_imx_buf_tx_swap_u32(struct spi_imx_data *spi_imx)
317{
318 u32 val = 0;
Arnd Bergmann5904c9d2017-08-23 15:34:43 +0200319#ifdef __LITTLE_ENDIAN
jiada wang1673c812017-08-10 13:50:08 +0900320 unsigned int bytes_per_word;
Arnd Bergmann5904c9d2017-08-23 15:34:43 +0200321#endif
jiada wang1673c812017-08-10 13:50:08 +0900322
323 if (spi_imx->tx_buf) {
324 val = *(u32 *)spi_imx->tx_buf;
325 val &= spi_imx->word_mask;
326 spi_imx->tx_buf += sizeof(u32);
327 }
328
329 spi_imx->count -= sizeof(u32);
330#ifdef __LITTLE_ENDIAN
331 bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
332
333 if (bytes_per_word == 1)
334 val = cpu_to_be32(val);
335 else if (bytes_per_word == 2)
336 val = (val << 16) | (val >> 16);
337#endif
338 writel(val, spi_imx->base + MXC_CSPITXDATA);
339}
340
341static void spi_imx_buf_tx_swap(struct spi_imx_data *spi_imx)
342{
343 u32 ctrl, val;
344 unsigned int bytes_per_word;
345
346 if (spi_imx->count == spi_imx->remainder) {
347 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
348 ctrl &= ~MX51_ECSPI_CTRL_BL_MASK;
349 if (spi_imx->count > MX51_ECSPI_CTRL_MAX_BURST) {
350 spi_imx->remainder = spi_imx->count %
351 MX51_ECSPI_CTRL_MAX_BURST;
352 val = MX51_ECSPI_CTRL_MAX_BURST * 8 - 1;
353 } else if (spi_imx->count >= sizeof(u32)) {
354 spi_imx->remainder = spi_imx->count % sizeof(u32);
355 val = (spi_imx->count - spi_imx->remainder) * 8 - 1;
356 } else {
357 spi_imx->remainder = 0;
358 val = spi_imx->bits_per_word - 1;
359 spi_imx->read_u32 = 0;
360 }
361
362 ctrl |= (val << MX51_ECSPI_CTRL_BL_OFFSET);
363 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
364 }
365
366 if (spi_imx->count >= sizeof(u32)) {
367 spi_imx_buf_tx_swap_u32(spi_imx);
368 return;
369 }
370
371 bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
372
373 if (bytes_per_word == 1)
374 spi_imx_buf_tx_u8(spi_imx);
375 else if (bytes_per_word == 2)
376 spi_imx_buf_tx_u16(spi_imx);
377}
378
jiada wang71abd292017-09-05 14:12:32 +0900379static void mx53_ecspi_rx_slave(struct spi_imx_data *spi_imx)
380{
381 u32 val = be32_to_cpu(readl(spi_imx->base + MXC_CSPIRXDATA));
382
383 if (spi_imx->rx_buf) {
384 int n_bytes = spi_imx->slave_burst % sizeof(val);
385
386 if (!n_bytes)
387 n_bytes = sizeof(val);
388
389 memcpy(spi_imx->rx_buf,
390 ((u8 *)&val) + sizeof(val) - n_bytes, n_bytes);
391
392 spi_imx->rx_buf += n_bytes;
393 spi_imx->slave_burst -= n_bytes;
394 }
395}
396
397static void mx53_ecspi_tx_slave(struct spi_imx_data *spi_imx)
398{
399 u32 val = 0;
400 int n_bytes = spi_imx->count % sizeof(val);
401
402 if (!n_bytes)
403 n_bytes = sizeof(val);
404
405 if (spi_imx->tx_buf) {
406 memcpy(((u8 *)&val) + sizeof(val) - n_bytes,
407 spi_imx->tx_buf, n_bytes);
408 val = cpu_to_be32(val);
409 spi_imx->tx_buf += n_bytes;
410 }
411
412 spi_imx->count -= n_bytes;
413
414 writel(val, spi_imx->base + MXC_CSPITXDATA);
415}
416
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200417/* MX51 eCSPI */
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100418static unsigned int mx51_ecspi_clkdiv(struct spi_imx_data *spi_imx,
419 unsigned int fspi, unsigned int *fres)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200420{
421 /*
422 * there are two 4-bit dividers, the pre-divider divides by
423 * $pre, the post-divider by 2^$post
424 */
425 unsigned int pre, post;
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100426 unsigned int fin = spi_imx->spi_clk;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200427
428 if (unlikely(fspi > fin))
429 return 0;
430
431 post = fls(fin) - fls(fspi);
432 if (fin > fspi << post)
433 post++;
434
435 /* now we have: (fin <= fspi << post) with post being minimal */
436
437 post = max(4U, post) - 4;
438 if (unlikely(post > 0xf)) {
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100439 dev_err(spi_imx->dev, "cannot set clock freq: %u (base freq: %u)\n",
440 fspi, fin);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200441 return 0xff;
442 }
443
444 pre = DIV_ROUND_UP(fin, fspi << post) - 1;
445
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100446 dev_dbg(spi_imx->dev, "%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200447 __func__, fin, fspi, post, pre);
Marek Vasut6fd8b852013-12-18 18:31:47 +0100448
449 /* Resulting frequency for the SCLK line. */
450 *fres = (fin / (pre + 1)) >> post;
451
Shawn Guo66de7572011-07-10 01:16:37 +0800452 return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) |
453 (post << MX51_ECSPI_CTRL_POSTDIV_OFFSET);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200454}
455
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300456static void mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200457{
458 unsigned val = 0;
459
460 if (enable & MXC_INT_TE)
Shawn Guo66de7572011-07-10 01:16:37 +0800461 val |= MX51_ECSPI_INT_TEEN;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200462
463 if (enable & MXC_INT_RR)
Shawn Guo66de7572011-07-10 01:16:37 +0800464 val |= MX51_ECSPI_INT_RREN;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200465
jiada wang71abd292017-09-05 14:12:32 +0900466 if (enable & MXC_INT_RDR)
467 val |= MX51_ECSPI_INT_RDREN;
468
Shawn Guo66de7572011-07-10 01:16:37 +0800469 writel(val, spi_imx->base + MX51_ECSPI_INT);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200470}
471
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300472static void mx51_ecspi_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200473{
Sascha Hauerb03c3882016-02-24 09:20:32 +0100474 u32 reg;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200475
Sascha Hauerb03c3882016-02-24 09:20:32 +0100476 reg = readl(spi_imx->base + MX51_ECSPI_CTRL);
477 reg |= MX51_ECSPI_CTRL_XCH;
Shawn Guo66de7572011-07-10 01:16:37 +0800478 writel(reg, spi_imx->base + MX51_ECSPI_CTRL);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200479}
480
jiada wang71abd292017-09-05 14:12:32 +0900481static void mx51_ecspi_disable(struct spi_imx_data *spi_imx)
482{
483 u32 ctrl;
484
485 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
486 ctrl &= ~MX51_ECSPI_CTRL_ENABLE;
487 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
488}
489
Sascha Hauerd52345b2017-06-02 07:38:01 +0200490static int mx51_ecspi_config(struct spi_device *spi)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200491{
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300492 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100493 u32 ctrl = MX51_ECSPI_CTRL_ENABLE;
Sascha Hauerd52345b2017-06-02 07:38:01 +0200494 u32 clk = spi_imx->speed_hz, delay, reg;
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100495 u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200496
jiada wang71abd292017-09-05 14:12:32 +0900497 /* set Master or Slave mode */
498 if (spi_imx->slave_mode)
499 ctrl &= ~MX51_ECSPI_CTRL_MODE_MASK;
500 else
501 ctrl |= MX51_ECSPI_CTRL_MODE_MASK;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200502
Leif Middelschultef72efa72017-04-23 21:19:58 +0200503 /*
504 * Enable SPI_RDY handling (falling edge/level triggered).
505 */
506 if (spi->mode & SPI_READY)
507 ctrl |= MX51_ECSPI_CTRL_DRCTL(spi_imx->spi_drctl);
508
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200509 /* set clock speed */
Sascha Hauerd52345b2017-06-02 07:38:01 +0200510 ctrl |= mx51_ecspi_clkdiv(spi_imx, spi_imx->speed_hz, &clk);
Anton Bondarenko4bfe9272016-02-19 08:43:03 +0100511 spi_imx->spi_bus_clk = clk;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200512
513 /* set chip select to use */
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300514 ctrl |= MX51_ECSPI_CTRL_CS(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200515
jiada wang71abd292017-09-05 14:12:32 +0900516 if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx))
517 ctrl |= (spi_imx->slave_burst * 8 - 1)
518 << MX51_ECSPI_CTRL_BL_OFFSET;
519 else
520 ctrl |= (spi_imx->bits_per_word - 1)
521 << MX51_ECSPI_CTRL_BL_OFFSET;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200522
jiada wang71abd292017-09-05 14:12:32 +0900523 /*
524 * eCSPI burst completion by Chip Select signal in Slave mode
525 * is not functional for imx53 Soc, config SPI burst completed when
526 * BURST_LENGTH + 1 bits are received
527 */
528 if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx))
529 cfg &= ~MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
530 else
531 cfg |= MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200532
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300533 if (spi->mode & SPI_CPHA)
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300534 cfg |= MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100535 else
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300536 cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200537
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300538 if (spi->mode & SPI_CPOL) {
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300539 cfg |= MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
540 cfg |= MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100541 } else {
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300542 cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
543 cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200544 }
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300545 if (spi->mode & SPI_CS_HIGH)
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300546 cfg |= MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100547 else
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300548 cfg &= ~MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200549
Sascha Hauerb03c3882016-02-24 09:20:32 +0100550 if (spi_imx->usedma)
551 ctrl |= MX51_ECSPI_CTRL_SMC;
552
Anton Bondarenkof677f172015-12-08 07:43:43 +0100553 /* CTRL register always go first to bring out controller from reset */
554 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
555
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200556 reg = readl(spi_imx->base + MX51_ECSPI_TESTREG);
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300557 if (spi->mode & SPI_LOOP)
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200558 reg |= MX51_ECSPI_TESTREG_LBC;
559 else
560 reg &= ~MX51_ECSPI_TESTREG_LBC;
561 writel(reg, spi_imx->base + MX51_ECSPI_TESTREG);
562
Shawn Guo66de7572011-07-10 01:16:37 +0800563 writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200564
Marek Vasut6fd8b852013-12-18 18:31:47 +0100565 /*
566 * Wait until the changes in the configuration register CONFIGREG
567 * propagate into the hardware. It takes exactly one tick of the
568 * SCLK clock, but we will wait two SCLK clock just to be sure. The
569 * effect of the delay it takes for the hardware to apply changes
570 * is noticable if the SCLK clock run very slow. In such a case, if
571 * the polarity of SCLK should be inverted, the GPIO ChipSelect might
572 * be asserted before the SCLK polarity changes, which would disrupt
573 * the SPI communication as the device on the other end would consider
574 * the change of SCLK polarity as a clock tick already.
575 */
576 delay = (2 * 1000000) / clk;
577 if (likely(delay < 10)) /* SCLK is faster than 100 kHz */
578 udelay(delay);
579 else /* SCLK is _very_ slow */
580 usleep_range(delay, delay + 10);
581
Robin Gongf62cacc2014-09-11 09:18:44 +0800582 /*
583 * Configure the DMA register: setup the watermark
584 * and enable DMA request.
585 */
Robin Gongf62cacc2014-09-11 09:18:44 +0800586
Sascha Hauerd629c2a2016-02-24 09:20:31 +0100587 writel(MX51_ECSPI_DMA_RX_WML(spi_imx->wml) |
588 MX51_ECSPI_DMA_TX_WML(spi_imx->wml) |
589 MX51_ECSPI_DMA_RXT_WML(spi_imx->wml) |
Sascha Hauer2b0fd062016-02-24 09:20:27 +0100590 MX51_ECSPI_DMA_TEDEN | MX51_ECSPI_DMA_RXDEN |
591 MX51_ECSPI_DMA_RXTDEN, spi_imx->base + MX51_ECSPI_DMA);
Robin Gongf62cacc2014-09-11 09:18:44 +0800592
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200593 return 0;
594}
595
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300596static int mx51_ecspi_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200597{
Shawn Guo66de7572011-07-10 01:16:37 +0800598 return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200599}
600
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300601static void mx51_ecspi_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200602{
603 /* drain receive buffer */
Shawn Guo66de7572011-07-10 01:16:37 +0800604 while (mx51_ecspi_rx_available(spi_imx))
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200605 readl(spi_imx->base + MXC_CSPIRXDATA);
606}
607
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700608#define MX31_INTREG_TEEN (1 << 0)
609#define MX31_INTREG_RREN (1 << 3)
610
611#define MX31_CSPICTRL_ENABLE (1 << 0)
612#define MX31_CSPICTRL_MASTER (1 << 1)
613#define MX31_CSPICTRL_XCH (1 << 2)
Martin Kaiser2dd33f92016-10-20 00:42:25 +0200614#define MX31_CSPICTRL_SMC (1 << 3)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700615#define MX31_CSPICTRL_POL (1 << 4)
616#define MX31_CSPICTRL_PHA (1 << 5)
617#define MX31_CSPICTRL_SSCTL (1 << 6)
618#define MX31_CSPICTRL_SSPOL (1 << 7)
619#define MX31_CSPICTRL_BC_SHIFT 8
620#define MX35_CSPICTRL_BL_SHIFT 20
621#define MX31_CSPICTRL_CS_SHIFT 24
622#define MX35_CSPICTRL_CS_SHIFT 12
623#define MX31_CSPICTRL_DR_SHIFT 16
624
Martin Kaiser2dd33f92016-10-20 00:42:25 +0200625#define MX31_CSPI_DMAREG 0x10
626#define MX31_DMAREG_RH_DEN (1<<4)
627#define MX31_DMAREG_TH_DEN (1<<1)
628
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700629#define MX31_CSPISTATUS 0x14
630#define MX31_STATUS_RR (1 << 3)
631
Martin Kaiser15ca9212016-09-01 22:39:58 +0200632#define MX31_CSPI_TESTREG 0x1C
633#define MX31_TEST_LBC (1 << 14)
634
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700635/* These functions also work for the i.MX35, but be aware that
636 * the i.MX35 has a slightly different register layout for bits
637 * we do not use here.
638 */
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300639static void mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700640{
641 unsigned int val = 0;
642
643 if (enable & MXC_INT_TE)
644 val |= MX31_INTREG_TEEN;
645 if (enable & MXC_INT_RR)
646 val |= MX31_INTREG_RREN;
647
648 writel(val, spi_imx->base + MXC_CSPIINT);
649}
650
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300651static void mx31_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700652{
653 unsigned int reg;
654
655 reg = readl(spi_imx->base + MXC_CSPICTRL);
656 reg |= MX31_CSPICTRL_XCH;
657 writel(reg, spi_imx->base + MXC_CSPICTRL);
658}
659
Sascha Hauerd52345b2017-06-02 07:38:01 +0200660static int mx31_config(struct spi_device *spi)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700661{
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300662 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700663 unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200664 unsigned int clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700665
Sascha Hauerd52345b2017-06-02 07:38:01 +0200666 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->speed_hz, &clk) <<
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700667 MX31_CSPICTRL_DR_SHIFT;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200668 spi_imx->spi_bus_clk = clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700669
Shawn Guo04ee5852011-07-10 01:16:39 +0800670 if (is_imx35_cspi(spi_imx)) {
Sascha Hauerd52345b2017-06-02 07:38:01 +0200671 reg |= (spi_imx->bits_per_word - 1) << MX35_CSPICTRL_BL_SHIFT;
Shawn Guo2a64a902011-07-10 01:16:38 +0800672 reg |= MX31_CSPICTRL_SSCTL;
673 } else {
Sascha Hauerd52345b2017-06-02 07:38:01 +0200674 reg |= (spi_imx->bits_per_word - 1) << MX31_CSPICTRL_BC_SHIFT;
Shawn Guo2a64a902011-07-10 01:16:38 +0800675 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700676
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300677 if (spi->mode & SPI_CPHA)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700678 reg |= MX31_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300679 if (spi->mode & SPI_CPOL)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700680 reg |= MX31_CSPICTRL_POL;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300681 if (spi->mode & SPI_CS_HIGH)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700682 reg |= MX31_CSPICTRL_SSPOL;
Greg Ungerer602c8f42017-07-11 14:22:11 +1000683 if (!gpio_is_valid(spi->cs_gpio))
684 reg |= (spi->chip_select) <<
Shawn Guo04ee5852011-07-10 01:16:39 +0800685 (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT :
686 MX31_CSPICTRL_CS_SHIFT);
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200687
Martin Kaiser2dd33f92016-10-20 00:42:25 +0200688 if (spi_imx->usedma)
689 reg |= MX31_CSPICTRL_SMC;
690
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200691 writel(reg, spi_imx->base + MXC_CSPICTRL);
692
Martin Kaiser15ca9212016-09-01 22:39:58 +0200693 reg = readl(spi_imx->base + MX31_CSPI_TESTREG);
694 if (spi->mode & SPI_LOOP)
695 reg |= MX31_TEST_LBC;
696 else
697 reg &= ~MX31_TEST_LBC;
698 writel(reg, spi_imx->base + MX31_CSPI_TESTREG);
699
Martin Kaiser2dd33f92016-10-20 00:42:25 +0200700 if (spi_imx->usedma) {
701 /* configure DMA requests when RXFIFO is half full and
702 when TXFIFO is half empty */
703 writel(MX31_DMAREG_RH_DEN | MX31_DMAREG_TH_DEN,
704 spi_imx->base + MX31_CSPI_DMAREG);
705 }
706
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200707 return 0;
708}
709
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300710static int mx31_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700711{
712 return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
713}
714
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300715static void mx31_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200716{
717 /* drain receive buffer */
Shawn Guo2a64a902011-07-10 01:16:38 +0800718 while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200719 readl(spi_imx->base + MXC_CSPIRXDATA);
720}
721
Shawn Guo3451fb12011-07-10 01:16:36 +0800722#define MX21_INTREG_RR (1 << 4)
723#define MX21_INTREG_TEEN (1 << 9)
724#define MX21_INTREG_RREN (1 << 13)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700725
Shawn Guo3451fb12011-07-10 01:16:36 +0800726#define MX21_CSPICTRL_POL (1 << 5)
727#define MX21_CSPICTRL_PHA (1 << 6)
728#define MX21_CSPICTRL_SSPOL (1 << 8)
729#define MX21_CSPICTRL_XCH (1 << 9)
730#define MX21_CSPICTRL_ENABLE (1 << 10)
731#define MX21_CSPICTRL_MASTER (1 << 11)
732#define MX21_CSPICTRL_DR_SHIFT 14
733#define MX21_CSPICTRL_CS_SHIFT 19
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700734
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300735static void mx21_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700736{
737 unsigned int val = 0;
738
739 if (enable & MXC_INT_TE)
Shawn Guo3451fb12011-07-10 01:16:36 +0800740 val |= MX21_INTREG_TEEN;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700741 if (enable & MXC_INT_RR)
Shawn Guo3451fb12011-07-10 01:16:36 +0800742 val |= MX21_INTREG_RREN;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700743
744 writel(val, spi_imx->base + MXC_CSPIINT);
745}
746
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300747static void mx21_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700748{
749 unsigned int reg;
750
751 reg = readl(spi_imx->base + MXC_CSPICTRL);
Shawn Guo3451fb12011-07-10 01:16:36 +0800752 reg |= MX21_CSPICTRL_XCH;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700753 writel(reg, spi_imx->base + MXC_CSPICTRL);
754}
755
Sascha Hauerd52345b2017-06-02 07:38:01 +0200756static int mx21_config(struct spi_device *spi)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700757{
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300758 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Shawn Guo3451fb12011-07-10 01:16:36 +0800759 unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER;
Shawn Guo04ee5852011-07-10 01:16:39 +0800760 unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18;
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100761 unsigned int clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700762
Sascha Hauerd52345b2017-06-02 07:38:01 +0200763 reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, spi_imx->speed_hz, max, &clk)
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100764 << MX21_CSPICTRL_DR_SHIFT;
765 spi_imx->spi_bus_clk = clk;
766
Sascha Hauerd52345b2017-06-02 07:38:01 +0200767 reg |= spi_imx->bits_per_word - 1;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700768
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300769 if (spi->mode & SPI_CPHA)
Shawn Guo3451fb12011-07-10 01:16:36 +0800770 reg |= MX21_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300771 if (spi->mode & SPI_CPOL)
Shawn Guo3451fb12011-07-10 01:16:36 +0800772 reg |= MX21_CSPICTRL_POL;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300773 if (spi->mode & SPI_CS_HIGH)
Shawn Guo3451fb12011-07-10 01:16:36 +0800774 reg |= MX21_CSPICTRL_SSPOL;
Greg Ungerer602c8f42017-07-11 14:22:11 +1000775 if (!gpio_is_valid(spi->cs_gpio))
776 reg |= spi->chip_select << MX21_CSPICTRL_CS_SHIFT;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700777
778 writel(reg, spi_imx->base + MXC_CSPICTRL);
779
780 return 0;
781}
782
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300783static int mx21_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700784{
Shawn Guo3451fb12011-07-10 01:16:36 +0800785 return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700786}
787
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300788static void mx21_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200789{
790 writel(1, spi_imx->base + MXC_RESET);
791}
792
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700793#define MX1_INTREG_RR (1 << 3)
794#define MX1_INTREG_TEEN (1 << 8)
795#define MX1_INTREG_RREN (1 << 11)
796
797#define MX1_CSPICTRL_POL (1 << 4)
798#define MX1_CSPICTRL_PHA (1 << 5)
799#define MX1_CSPICTRL_XCH (1 << 8)
800#define MX1_CSPICTRL_ENABLE (1 << 9)
801#define MX1_CSPICTRL_MASTER (1 << 10)
802#define MX1_CSPICTRL_DR_SHIFT 13
803
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300804static void mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700805{
806 unsigned int val = 0;
807
808 if (enable & MXC_INT_TE)
809 val |= MX1_INTREG_TEEN;
810 if (enable & MXC_INT_RR)
811 val |= MX1_INTREG_RREN;
812
813 writel(val, spi_imx->base + MXC_CSPIINT);
814}
815
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300816static void mx1_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700817{
818 unsigned int reg;
819
820 reg = readl(spi_imx->base + MXC_CSPICTRL);
821 reg |= MX1_CSPICTRL_XCH;
822 writel(reg, spi_imx->base + MXC_CSPICTRL);
823}
824
Sascha Hauerd52345b2017-06-02 07:38:01 +0200825static int mx1_config(struct spi_device *spi)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700826{
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300827 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700828 unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200829 unsigned int clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700830
Sascha Hauerd52345b2017-06-02 07:38:01 +0200831 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->speed_hz, &clk) <<
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700832 MX1_CSPICTRL_DR_SHIFT;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200833 spi_imx->spi_bus_clk = clk;
834
Sascha Hauerd52345b2017-06-02 07:38:01 +0200835 reg |= spi_imx->bits_per_word - 1;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700836
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300837 if (spi->mode & SPI_CPHA)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700838 reg |= MX1_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300839 if (spi->mode & SPI_CPOL)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700840 reg |= MX1_CSPICTRL_POL;
841
842 writel(reg, spi_imx->base + MXC_CSPICTRL);
843
844 return 0;
845}
846
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300847static int mx1_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700848{
849 return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
850}
851
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300852static void mx1_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200853{
854 writel(1, spi_imx->base + MXC_RESET);
855}
856
Shawn Guo04ee5852011-07-10 01:16:39 +0800857static struct spi_imx_devtype_data imx1_cspi_devtype_data = {
858 .intctrl = mx1_intctrl,
859 .config = mx1_config,
860 .trigger = mx1_trigger,
861 .rx_available = mx1_rx_available,
862 .reset = mx1_reset,
jiada wangfd8d4e22017-06-08 14:16:00 +0900863 .fifo_size = 8,
864 .has_dmamode = false,
jiada wang1673c812017-08-10 13:50:08 +0900865 .dynamic_burst = false,
jiada wang71abd292017-09-05 14:12:32 +0900866 .has_slavemode = false,
Shawn Guo04ee5852011-07-10 01:16:39 +0800867 .devtype = IMX1_CSPI,
868};
869
870static struct spi_imx_devtype_data imx21_cspi_devtype_data = {
871 .intctrl = mx21_intctrl,
872 .config = mx21_config,
873 .trigger = mx21_trigger,
874 .rx_available = mx21_rx_available,
875 .reset = mx21_reset,
jiada wangfd8d4e22017-06-08 14:16:00 +0900876 .fifo_size = 8,
877 .has_dmamode = false,
jiada wang1673c812017-08-10 13:50:08 +0900878 .dynamic_burst = false,
jiada wang71abd292017-09-05 14:12:32 +0900879 .has_slavemode = false,
Shawn Guo04ee5852011-07-10 01:16:39 +0800880 .devtype = IMX21_CSPI,
881};
882
883static struct spi_imx_devtype_data imx27_cspi_devtype_data = {
884 /* i.mx27 cspi shares the functions with i.mx21 one */
885 .intctrl = mx21_intctrl,
886 .config = mx21_config,
887 .trigger = mx21_trigger,
888 .rx_available = mx21_rx_available,
889 .reset = mx21_reset,
jiada wangfd8d4e22017-06-08 14:16:00 +0900890 .fifo_size = 8,
891 .has_dmamode = false,
jiada wang1673c812017-08-10 13:50:08 +0900892 .dynamic_burst = false,
jiada wang71abd292017-09-05 14:12:32 +0900893 .has_slavemode = false,
Shawn Guo04ee5852011-07-10 01:16:39 +0800894 .devtype = IMX27_CSPI,
895};
896
897static struct spi_imx_devtype_data imx31_cspi_devtype_data = {
898 .intctrl = mx31_intctrl,
899 .config = mx31_config,
900 .trigger = mx31_trigger,
901 .rx_available = mx31_rx_available,
902 .reset = mx31_reset,
jiada wangfd8d4e22017-06-08 14:16:00 +0900903 .fifo_size = 8,
904 .has_dmamode = false,
jiada wang1673c812017-08-10 13:50:08 +0900905 .dynamic_burst = false,
jiada wang71abd292017-09-05 14:12:32 +0900906 .has_slavemode = false,
Shawn Guo04ee5852011-07-10 01:16:39 +0800907 .devtype = IMX31_CSPI,
908};
909
910static struct spi_imx_devtype_data imx35_cspi_devtype_data = {
911 /* i.mx35 and later cspi shares the functions with i.mx31 one */
912 .intctrl = mx31_intctrl,
913 .config = mx31_config,
914 .trigger = mx31_trigger,
915 .rx_available = mx31_rx_available,
916 .reset = mx31_reset,
jiada wangfd8d4e22017-06-08 14:16:00 +0900917 .fifo_size = 8,
918 .has_dmamode = true,
jiada wang1673c812017-08-10 13:50:08 +0900919 .dynamic_burst = false,
jiada wang71abd292017-09-05 14:12:32 +0900920 .has_slavemode = false,
Shawn Guo04ee5852011-07-10 01:16:39 +0800921 .devtype = IMX35_CSPI,
922};
923
924static struct spi_imx_devtype_data imx51_ecspi_devtype_data = {
925 .intctrl = mx51_ecspi_intctrl,
926 .config = mx51_ecspi_config,
927 .trigger = mx51_ecspi_trigger,
928 .rx_available = mx51_ecspi_rx_available,
929 .reset = mx51_ecspi_reset,
jiada wangfd8d4e22017-06-08 14:16:00 +0900930 .fifo_size = 64,
931 .has_dmamode = true,
jiada wang1673c812017-08-10 13:50:08 +0900932 .dynamic_burst = true,
jiada wang71abd292017-09-05 14:12:32 +0900933 .has_slavemode = true,
934 .disable = mx51_ecspi_disable,
Shawn Guo04ee5852011-07-10 01:16:39 +0800935 .devtype = IMX51_ECSPI,
936};
937
jiada wang26e4bb82017-06-08 14:16:01 +0900938static struct spi_imx_devtype_data imx53_ecspi_devtype_data = {
939 .intctrl = mx51_ecspi_intctrl,
940 .config = mx51_ecspi_config,
941 .trigger = mx51_ecspi_trigger,
942 .rx_available = mx51_ecspi_rx_available,
943 .reset = mx51_ecspi_reset,
944 .fifo_size = 64,
945 .has_dmamode = true,
jiada wang71abd292017-09-05 14:12:32 +0900946 .has_slavemode = true,
947 .disable = mx51_ecspi_disable,
jiada wang26e4bb82017-06-08 14:16:01 +0900948 .devtype = IMX53_ECSPI,
949};
950
Krzysztof Kozlowskidb1b8202015-05-02 00:44:04 +0900951static const struct platform_device_id spi_imx_devtype[] = {
Shawn Guo04ee5852011-07-10 01:16:39 +0800952 {
953 .name = "imx1-cspi",
954 .driver_data = (kernel_ulong_t) &imx1_cspi_devtype_data,
955 }, {
956 .name = "imx21-cspi",
957 .driver_data = (kernel_ulong_t) &imx21_cspi_devtype_data,
958 }, {
959 .name = "imx27-cspi",
960 .driver_data = (kernel_ulong_t) &imx27_cspi_devtype_data,
961 }, {
962 .name = "imx31-cspi",
963 .driver_data = (kernel_ulong_t) &imx31_cspi_devtype_data,
964 }, {
965 .name = "imx35-cspi",
966 .driver_data = (kernel_ulong_t) &imx35_cspi_devtype_data,
967 }, {
968 .name = "imx51-ecspi",
969 .driver_data = (kernel_ulong_t) &imx51_ecspi_devtype_data,
970 }, {
jiada wang26e4bb82017-06-08 14:16:01 +0900971 .name = "imx53-ecspi",
972 .driver_data = (kernel_ulong_t) &imx53_ecspi_devtype_data,
973 }, {
Shawn Guo04ee5852011-07-10 01:16:39 +0800974 /* sentinel */
975 }
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +0200976};
977
Shawn Guo22a85e42011-07-10 01:16:41 +0800978static const struct of_device_id spi_imx_dt_ids[] = {
979 { .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, },
980 { .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, },
981 { .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, },
982 { .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, },
983 { .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, },
984 { .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, },
jiada wang26e4bb82017-06-08 14:16:01 +0900985 { .compatible = "fsl,imx53-ecspi", .data = &imx53_ecspi_devtype_data, },
Shawn Guo22a85e42011-07-10 01:16:41 +0800986 { /* sentinel */ }
987};
Niels de Vos27743e02013-07-29 09:38:05 +0200988MODULE_DEVICE_TABLE(of, spi_imx_dt_ids);
Shawn Guo22a85e42011-07-10 01:16:41 +0800989
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700990static void spi_imx_chipselect(struct spi_device *spi, int is_active)
991{
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -0700992 int active = is_active != BITBANG_CS_INACTIVE;
993 int dev_is_lowactive = !(spi->mode & SPI_CS_HIGH);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700994
Oleksij Rempelab2f3572017-07-25 09:57:09 +0200995 if (spi->mode & SPI_NO_CS)
996 return;
997
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300998 if (!gpio_is_valid(spi->cs_gpio))
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700999 return;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001000
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001001 gpio_set_value(spi->cs_gpio, dev_is_lowactive ^ active);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001002}
1003
1004static void spi_imx_push(struct spi_imx_data *spi_imx)
1005{
jiada wangfd8d4e22017-06-08 14:16:00 +09001006 while (spi_imx->txfifo < spi_imx->devtype_data->fifo_size) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001007 if (!spi_imx->count)
1008 break;
jiada wang1673c812017-08-10 13:50:08 +09001009 if (spi_imx->txfifo && (spi_imx->count == spi_imx->remainder))
1010 break;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001011 spi_imx->tx(spi_imx);
1012 spi_imx->txfifo++;
1013 }
1014
jiada wang71abd292017-09-05 14:12:32 +09001015 if (!spi_imx->slave_mode)
1016 spi_imx->devtype_data->trigger(spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001017}
1018
1019static irqreturn_t spi_imx_isr(int irq, void *dev_id)
1020{
1021 struct spi_imx_data *spi_imx = dev_id;
1022
jiada wang71abd292017-09-05 14:12:32 +09001023 while (spi_imx->txfifo &&
1024 spi_imx->devtype_data->rx_available(spi_imx)) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001025 spi_imx->rx(spi_imx);
1026 spi_imx->txfifo--;
1027 }
1028
1029 if (spi_imx->count) {
1030 spi_imx_push(spi_imx);
1031 return IRQ_HANDLED;
1032 }
1033
1034 if (spi_imx->txfifo) {
1035 /* No data left to push, but still waiting for rx data,
1036 * enable receive data available interrupt.
1037 */
Shawn Guoedd501bb2011-07-10 01:16:35 +08001038 spi_imx->devtype_data->intctrl(
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +02001039 spi_imx, MXC_INT_RR);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001040 return IRQ_HANDLED;
1041 }
1042
Shawn Guoedd501bb2011-07-10 01:16:35 +08001043 spi_imx->devtype_data->intctrl(spi_imx, 0);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001044 complete(&spi_imx->xfer_done);
1045
1046 return IRQ_HANDLED;
1047}
1048
Sascha Hauer65017ee2017-06-02 07:38:03 +02001049static int spi_imx_dma_configure(struct spi_master *master)
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001050{
1051 int ret;
1052 enum dma_slave_buswidth buswidth;
1053 struct dma_slave_config rx = {}, tx = {};
1054 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1055
Sascha Hauer65017ee2017-06-02 07:38:03 +02001056 switch (spi_imx_bytes_per_word(spi_imx->bits_per_word)) {
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001057 case 4:
1058 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
1059 break;
1060 case 2:
1061 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
1062 break;
1063 case 1:
1064 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
1065 break;
1066 default:
1067 return -EINVAL;
1068 }
1069
1070 tx.direction = DMA_MEM_TO_DEV;
1071 tx.dst_addr = spi_imx->base_phys + MXC_CSPITXDATA;
1072 tx.dst_addr_width = buswidth;
1073 tx.dst_maxburst = spi_imx->wml;
1074 ret = dmaengine_slave_config(master->dma_tx, &tx);
1075 if (ret) {
1076 dev_err(spi_imx->dev, "TX dma configuration failed with %d\n", ret);
1077 return ret;
1078 }
1079
1080 rx.direction = DMA_DEV_TO_MEM;
1081 rx.src_addr = spi_imx->base_phys + MXC_CSPIRXDATA;
1082 rx.src_addr_width = buswidth;
1083 rx.src_maxburst = spi_imx->wml;
1084 ret = dmaengine_slave_config(master->dma_rx, &rx);
1085 if (ret) {
1086 dev_err(spi_imx->dev, "RX dma configuration failed with %d\n", ret);
1087 return ret;
1088 }
1089
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001090 return 0;
1091}
1092
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001093static int spi_imx_setupxfer(struct spi_device *spi,
1094 struct spi_transfer *t)
1095{
1096 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001097 int ret;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001098
Sascha Hauerabb1ff12017-06-02 07:37:59 +02001099 if (!t)
1100 return 0;
1101
Sascha Hauerd52345b2017-06-02 07:38:01 +02001102 spi_imx->bits_per_word = t->bits_per_word;
1103 spi_imx->speed_hz = t->speed_hz;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001104
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -07001105 /* Initialize the functions for transfer */
jiada wang71abd292017-09-05 14:12:32 +09001106 if (spi_imx->devtype_data->dynamic_burst && !spi_imx->slave_mode) {
jiada wang1673c812017-08-10 13:50:08 +09001107 u32 mask;
1108
1109 spi_imx->dynamic_burst = 0;
1110 spi_imx->remainder = 0;
1111 spi_imx->read_u32 = 1;
1112
1113 mask = (1 << spi_imx->bits_per_word) - 1;
1114 spi_imx->rx = spi_imx_buf_rx_swap;
1115 spi_imx->tx = spi_imx_buf_tx_swap;
1116 spi_imx->dynamic_burst = 1;
1117 spi_imx->remainder = t->len;
1118
1119 if (spi_imx->bits_per_word <= 8)
1120 spi_imx->word_mask = mask << 24 | mask << 16
1121 | mask << 8 | mask;
1122 else if (spi_imx->bits_per_word <= 16)
1123 spi_imx->word_mask = mask << 16 | mask;
1124 else
1125 spi_imx->word_mask = mask;
Sachin Kamat60514262013-05-30 13:38:09 +05301126 } else {
jiada wang1673c812017-08-10 13:50:08 +09001127 if (spi_imx->bits_per_word <= 8) {
1128 spi_imx->rx = spi_imx_buf_rx_u8;
1129 spi_imx->tx = spi_imx_buf_tx_u8;
1130 } else if (spi_imx->bits_per_word <= 16) {
1131 spi_imx->rx = spi_imx_buf_rx_u16;
1132 spi_imx->tx = spi_imx_buf_tx_u16;
1133 } else {
1134 spi_imx->rx = spi_imx_buf_rx_u32;
1135 spi_imx->tx = spi_imx_buf_tx_u32;
1136 }
Stephen Warren24778be2013-05-21 20:36:35 -06001137 }
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -07001138
Sascha Hauerc008a802016-02-24 09:20:26 +01001139 if (spi_imx_can_dma(spi_imx->bitbang.master, spi, t))
1140 spi_imx->usedma = 1;
1141 else
1142 spi_imx->usedma = 0;
1143
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001144 if (spi_imx->usedma) {
Sascha Hauer65017ee2017-06-02 07:38:03 +02001145 ret = spi_imx_dma_configure(spi->master);
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001146 if (ret)
1147 return ret;
1148 }
1149
jiada wang71abd292017-09-05 14:12:32 +09001150 if (is_imx53_ecspi(spi_imx) && spi_imx->slave_mode) {
1151 spi_imx->rx = mx53_ecspi_rx_slave;
1152 spi_imx->tx = mx53_ecspi_tx_slave;
1153 spi_imx->slave_burst = t->len;
1154 }
1155
Sascha Hauerd52345b2017-06-02 07:38:01 +02001156 spi_imx->devtype_data->config(spi);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001157
1158 return 0;
1159}
1160
Robin Gongf62cacc2014-09-11 09:18:44 +08001161static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx)
1162{
1163 struct spi_master *master = spi_imx->bitbang.master;
1164
1165 if (master->dma_rx) {
1166 dma_release_channel(master->dma_rx);
1167 master->dma_rx = NULL;
1168 }
1169
1170 if (master->dma_tx) {
1171 dma_release_channel(master->dma_tx);
1172 master->dma_tx = NULL;
1173 }
Robin Gongf62cacc2014-09-11 09:18:44 +08001174}
1175
1176static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx,
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001177 struct spi_master *master)
Robin Gongf62cacc2014-09-11 09:18:44 +08001178{
Robin Gongf62cacc2014-09-11 09:18:44 +08001179 int ret;
1180
Robin Gonga02bb402015-02-03 10:25:53 +08001181 /* use pio mode for i.mx6dl chip TKT238285 */
1182 if (of_machine_is_compatible("fsl,imx6dl"))
1183 return 0;
1184
jiada wangfd8d4e22017-06-08 14:16:00 +09001185 spi_imx->wml = spi_imx->devtype_data->fifo_size / 2;
Anton Bondarenko0dfbaa82015-12-05 17:57:01 +01001186
Robin Gongf62cacc2014-09-11 09:18:44 +08001187 /* Prepare for TX DMA: */
Anton Bondarenko37600472015-12-08 07:43:45 +01001188 master->dma_tx = dma_request_slave_channel_reason(dev, "tx");
1189 if (IS_ERR(master->dma_tx)) {
1190 ret = PTR_ERR(master->dma_tx);
1191 dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret);
1192 master->dma_tx = NULL;
Robin Gongf62cacc2014-09-11 09:18:44 +08001193 goto err;
1194 }
1195
Robin Gongf62cacc2014-09-11 09:18:44 +08001196 /* Prepare for RX : */
Anton Bondarenko37600472015-12-08 07:43:45 +01001197 master->dma_rx = dma_request_slave_channel_reason(dev, "rx");
1198 if (IS_ERR(master->dma_rx)) {
1199 ret = PTR_ERR(master->dma_rx);
1200 dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret);
1201 master->dma_rx = NULL;
Robin Gongf62cacc2014-09-11 09:18:44 +08001202 goto err;
1203 }
1204
Robin Gongf62cacc2014-09-11 09:18:44 +08001205 init_completion(&spi_imx->dma_rx_completion);
1206 init_completion(&spi_imx->dma_tx_completion);
1207 master->can_dma = spi_imx_can_dma;
1208 master->max_dma_len = MAX_SDMA_BD_BYTES;
1209 spi_imx->bitbang.master->flags = SPI_MASTER_MUST_RX |
1210 SPI_MASTER_MUST_TX;
Robin Gongf62cacc2014-09-11 09:18:44 +08001211
1212 return 0;
1213err:
1214 spi_imx_sdma_exit(spi_imx);
1215 return ret;
1216}
1217
1218static void spi_imx_dma_rx_callback(void *cookie)
1219{
1220 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
1221
1222 complete(&spi_imx->dma_rx_completion);
1223}
1224
1225static void spi_imx_dma_tx_callback(void *cookie)
1226{
1227 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
1228
1229 complete(&spi_imx->dma_tx_completion);
1230}
1231
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001232static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size)
1233{
1234 unsigned long timeout = 0;
1235
1236 /* Time with actual data transfer and CS change delay related to HW */
1237 timeout = (8 + 4) * size / spi_imx->spi_bus_clk;
1238
1239 /* Add extra second for scheduler related activities */
1240 timeout += 1;
1241
1242 /* Double calculated timeout */
1243 return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);
1244}
1245
Robin Gongf62cacc2014-09-11 09:18:44 +08001246static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,
1247 struct spi_transfer *transfer)
1248{
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001249 struct dma_async_tx_descriptor *desc_tx, *desc_rx;
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001250 unsigned long transfer_timeout;
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001251 unsigned long timeout;
Robin Gongf62cacc2014-09-11 09:18:44 +08001252 struct spi_master *master = spi_imx->bitbang.master;
1253 struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
1254
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001255 /*
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001256 * The TX DMA setup starts the transfer, so make sure RX is configured
1257 * before TX.
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001258 */
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001259 desc_rx = dmaengine_prep_slave_sg(master->dma_rx,
1260 rx->sgl, rx->nents, DMA_DEV_TO_MEM,
1261 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1262 if (!desc_rx)
1263 return -EINVAL;
1264
1265 desc_rx->callback = spi_imx_dma_rx_callback;
1266 desc_rx->callback_param = (void *)spi_imx;
1267 dmaengine_submit(desc_rx);
1268 reinit_completion(&spi_imx->dma_rx_completion);
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001269 dma_async_issue_pending(master->dma_rx);
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001270
1271 desc_tx = dmaengine_prep_slave_sg(master->dma_tx,
1272 tx->sgl, tx->nents, DMA_MEM_TO_DEV,
1273 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1274 if (!desc_tx) {
1275 dmaengine_terminate_all(master->dma_tx);
1276 return -EINVAL;
1277 }
1278
1279 desc_tx->callback = spi_imx_dma_tx_callback;
1280 desc_tx->callback_param = (void *)spi_imx;
1281 dmaengine_submit(desc_tx);
1282 reinit_completion(&spi_imx->dma_tx_completion);
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001283 dma_async_issue_pending(master->dma_tx);
Robin Gongf62cacc2014-09-11 09:18:44 +08001284
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001285 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1286
Robin Gongf62cacc2014-09-11 09:18:44 +08001287 /* Wait SDMA to finish the data transfer.*/
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001288 timeout = wait_for_completion_timeout(&spi_imx->dma_tx_completion,
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001289 transfer_timeout);
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001290 if (!timeout) {
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001291 dev_err(spi_imx->dev, "I/O Error in DMA TX\n");
Robin Gongf62cacc2014-09-11 09:18:44 +08001292 dmaengine_terminate_all(master->dma_tx);
Anton Bondarenkoe47b33c2015-12-05 17:56:59 +01001293 dmaengine_terminate_all(master->dma_rx);
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001294 return -ETIMEDOUT;
Robin Gongf62cacc2014-09-11 09:18:44 +08001295 }
1296
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001297 timeout = wait_for_completion_timeout(&spi_imx->dma_rx_completion,
1298 transfer_timeout);
1299 if (!timeout) {
1300 dev_err(&master->dev, "I/O Error in DMA RX\n");
1301 spi_imx->devtype_data->reset(spi_imx);
1302 dmaengine_terminate_all(master->dma_rx);
1303 return -ETIMEDOUT;
1304 }
Robin Gongf62cacc2014-09-11 09:18:44 +08001305
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001306 return transfer->len;
Robin Gongf62cacc2014-09-11 09:18:44 +08001307}
1308
1309static int spi_imx_pio_transfer(struct spi_device *spi,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001310 struct spi_transfer *transfer)
1311{
1312 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Christian Gmeinerff1ba3d2016-06-21 14:12:54 +02001313 unsigned long transfer_timeout;
1314 unsigned long timeout;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001315
1316 spi_imx->tx_buf = transfer->tx_buf;
1317 spi_imx->rx_buf = transfer->rx_buf;
1318 spi_imx->count = transfer->len;
1319 spi_imx->txfifo = 0;
1320
Axel Linaa0fe822014-02-09 11:06:04 +08001321 reinit_completion(&spi_imx->xfer_done);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001322
1323 spi_imx_push(spi_imx);
1324
Shawn Guoedd501bb2011-07-10 01:16:35 +08001325 spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001326
Christian Gmeinerff1ba3d2016-06-21 14:12:54 +02001327 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1328
1329 timeout = wait_for_completion_timeout(&spi_imx->xfer_done,
1330 transfer_timeout);
1331 if (!timeout) {
1332 dev_err(&spi->dev, "I/O Error in PIO\n");
1333 spi_imx->devtype_data->reset(spi_imx);
1334 return -ETIMEDOUT;
1335 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001336
1337 return transfer->len;
1338}
1339
jiada wang71abd292017-09-05 14:12:32 +09001340static int spi_imx_pio_transfer_slave(struct spi_device *spi,
1341 struct spi_transfer *transfer)
1342{
1343 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1344 int ret = transfer->len;
1345
1346 if (is_imx53_ecspi(spi_imx) &&
1347 transfer->len > MX53_MAX_TRANSFER_BYTES) {
1348 dev_err(&spi->dev, "Transaction too big, max size is %d bytes\n",
1349 MX53_MAX_TRANSFER_BYTES);
1350 return -EMSGSIZE;
1351 }
1352
1353 spi_imx->tx_buf = transfer->tx_buf;
1354 spi_imx->rx_buf = transfer->rx_buf;
1355 spi_imx->count = transfer->len;
1356 spi_imx->txfifo = 0;
1357
1358 reinit_completion(&spi_imx->xfer_done);
1359 spi_imx->slave_aborted = false;
1360
1361 spi_imx_push(spi_imx);
1362
1363 spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE | MXC_INT_RDR);
1364
1365 if (wait_for_completion_interruptible(&spi_imx->xfer_done) ||
1366 spi_imx->slave_aborted) {
1367 dev_dbg(&spi->dev, "interrupted\n");
1368 ret = -EINTR;
1369 }
1370
1371 /* ecspi has a HW issue when works in Slave mode,
1372 * after 64 words writtern to TXFIFO, even TXFIFO becomes empty,
1373 * ECSPI_TXDATA keeps shift out the last word data,
1374 * so we have to disable ECSPI when in slave mode after the
1375 * transfer completes
1376 */
1377 if (spi_imx->devtype_data->disable)
1378 spi_imx->devtype_data->disable(spi_imx);
1379
1380 return ret;
1381}
1382
Robin Gongf62cacc2014-09-11 09:18:44 +08001383static int spi_imx_transfer(struct spi_device *spi,
1384 struct spi_transfer *transfer)
1385{
Robin Gongf62cacc2014-09-11 09:18:44 +08001386 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1387
jiada wang71abd292017-09-05 14:12:32 +09001388 /* flush rxfifo before transfer */
1389 while (spi_imx->devtype_data->rx_available(spi_imx))
1390 spi_imx->rx(spi_imx);
1391
1392 if (spi_imx->slave_mode)
1393 return spi_imx_pio_transfer_slave(spi, transfer);
1394
Sascha Hauerc008a802016-02-24 09:20:26 +01001395 if (spi_imx->usedma)
Sascha Hauer99f1cf12016-02-23 10:23:50 +01001396 return spi_imx_dma_transfer(spi_imx, transfer);
Sascha Hauerc008a802016-02-24 09:20:26 +01001397 else
1398 return spi_imx_pio_transfer(spi, transfer);
Robin Gongf62cacc2014-09-11 09:18:44 +08001399}
1400
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001401static int spi_imx_setup(struct spi_device *spi)
1402{
Alberto Panizzof4d4ecf2010-01-20 13:49:45 -07001403 dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001404 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1405
Oleksij Rempelab2f3572017-07-25 09:57:09 +02001406 if (spi->mode & SPI_NO_CS)
1407 return 0;
1408
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001409 if (gpio_is_valid(spi->cs_gpio))
1410 gpio_direction_output(spi->cs_gpio,
1411 spi->mode & SPI_CS_HIGH ? 0 : 1);
Sascha Hauer6c23e5d2009-10-01 15:44:29 -07001412
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001413 spi_imx_chipselect(spi, BITBANG_CS_INACTIVE);
1414
1415 return 0;
1416}
1417
1418static void spi_imx_cleanup(struct spi_device *spi)
1419{
1420}
1421
Huang Shijie9e556dc2013-10-23 16:31:50 +08001422static int
1423spi_imx_prepare_message(struct spi_master *master, struct spi_message *msg)
1424{
1425 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1426 int ret;
1427
1428 ret = clk_enable(spi_imx->clk_per);
1429 if (ret)
1430 return ret;
1431
1432 ret = clk_enable(spi_imx->clk_ipg);
1433 if (ret) {
1434 clk_disable(spi_imx->clk_per);
1435 return ret;
1436 }
1437
1438 return 0;
1439}
1440
1441static int
1442spi_imx_unprepare_message(struct spi_master *master, struct spi_message *msg)
1443{
1444 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1445
1446 clk_disable(spi_imx->clk_ipg);
1447 clk_disable(spi_imx->clk_per);
1448 return 0;
1449}
1450
jiada wang71abd292017-09-05 14:12:32 +09001451static int spi_imx_slave_abort(struct spi_master *master)
1452{
1453 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1454
1455 spi_imx->slave_aborted = true;
1456 complete(&spi_imx->xfer_done);
1457
1458 return 0;
1459}
1460
Grant Likelyfd4a3192012-12-07 16:57:14 +00001461static int spi_imx_probe(struct platform_device *pdev)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001462{
Shawn Guo22a85e42011-07-10 01:16:41 +08001463 struct device_node *np = pdev->dev.of_node;
1464 const struct of_device_id *of_id =
1465 of_match_device(spi_imx_dt_ids, &pdev->dev);
1466 struct spi_imx_master *mxc_platform_info =
1467 dev_get_platdata(&pdev->dev);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001468 struct spi_master *master;
1469 struct spi_imx_data *spi_imx;
1470 struct resource *res;
Leif Middelschultef72efa72017-04-23 21:19:58 +02001471 int i, ret, irq, spi_drctl;
jiada wang71abd292017-09-05 14:12:32 +09001472 const struct spi_imx_devtype_data *devtype_data = of_id ? of_id->data :
1473 (struct spi_imx_devtype_data *)pdev->id_entry->driver_data;
1474 bool slave_mode;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001475
Shawn Guo22a85e42011-07-10 01:16:41 +08001476 if (!np && !mxc_platform_info) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001477 dev_err(&pdev->dev, "can't get the platform data\n");
1478 return -EINVAL;
1479 }
1480
jiada wang71abd292017-09-05 14:12:32 +09001481 slave_mode = devtype_data->has_slavemode &&
1482 of_property_read_bool(np, "spi-slave");
1483 if (slave_mode)
1484 master = spi_alloc_slave(&pdev->dev,
1485 sizeof(struct spi_imx_data));
1486 else
1487 master = spi_alloc_master(&pdev->dev,
1488 sizeof(struct spi_imx_data));
Fabio Estevam2c147772017-06-20 13:50:55 -03001489 if (!master)
1490 return -ENOMEM;
1491
Leif Middelschultef72efa72017-04-23 21:19:58 +02001492 ret = of_property_read_u32(np, "fsl,spi-rdy-drctl", &spi_drctl);
1493 if ((ret < 0) || (spi_drctl >= 0x3)) {
1494 /* '11' is reserved */
1495 spi_drctl = 0;
1496 }
1497
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001498 platform_set_drvdata(pdev, master);
1499
Stephen Warren24778be2013-05-21 20:36:35 -06001500 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001501 master->bus_num = np ? -1 : pdev->id;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001502
1503 spi_imx = spi_master_get_devdata(master);
Axel Lin94c69f72013-09-10 15:43:41 +08001504 spi_imx->bitbang.master = master;
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001505 spi_imx->dev = &pdev->dev;
jiada wang71abd292017-09-05 14:12:32 +09001506 spi_imx->slave_mode = slave_mode;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001507
jiada wang71abd292017-09-05 14:12:32 +09001508 spi_imx->devtype_data = devtype_data;
Anton Bondarenko4686d1c2015-12-08 07:43:44 +01001509
Trent Piepho881a0b92017-10-31 12:49:04 -07001510 /* Get number of chip selects, either platform data or OF */
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001511 if (mxc_platform_info) {
1512 master->num_chipselect = mxc_platform_info->num_chipselect;
Trent Piephoffd4db92017-10-31 12:49:06 -07001513 if (mxc_platform_info->chipselect) {
Kees Cooka86854d2018-06-12 14:07:58 -07001514 master->cs_gpios = devm_kcalloc(&master->dev,
1515 master->num_chipselect, sizeof(int),
1516 GFP_KERNEL);
Trent Piephoffd4db92017-10-31 12:49:06 -07001517 if (!master->cs_gpios)
1518 return -ENOMEM;
Fabio Estevam4cc122a2011-09-15 17:21:15 -03001519
Trent Piephoffd4db92017-10-31 12:49:06 -07001520 for (i = 0; i < master->num_chipselect; i++)
1521 master->cs_gpios[i] = mxc_platform_info->chipselect[i];
1522 }
Trent Piepho881a0b92017-10-31 12:49:04 -07001523 } else {
1524 u32 num_cs;
1525
1526 if (!of_property_read_u32(np, "num-cs", &num_cs))
1527 master->num_chipselect = num_cs;
1528 /* If not preset, default value of 1 is used */
1529 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001530
1531 spi_imx->bitbang.chipselect = spi_imx_chipselect;
1532 spi_imx->bitbang.setup_transfer = spi_imx_setupxfer;
1533 spi_imx->bitbang.txrx_bufs = spi_imx_transfer;
1534 spi_imx->bitbang.master->setup = spi_imx_setup;
1535 spi_imx->bitbang.master->cleanup = spi_imx_cleanup;
Huang Shijie9e556dc2013-10-23 16:31:50 +08001536 spi_imx->bitbang.master->prepare_message = spi_imx_prepare_message;
1537 spi_imx->bitbang.master->unprepare_message = spi_imx_unprepare_message;
jiada wang71abd292017-09-05 14:12:32 +09001538 spi_imx->bitbang.master->slave_abort = spi_imx_slave_abort;
Oleksij Rempelab2f3572017-07-25 09:57:09 +02001539 spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
1540 | SPI_NO_CS;
jiada wang26e4bb82017-06-08 14:16:01 +09001541 if (is_imx35_cspi(spi_imx) || is_imx51_ecspi(spi_imx) ||
1542 is_imx53_ecspi(spi_imx))
Leif Middelschultef72efa72017-04-23 21:19:58 +02001543 spi_imx->bitbang.master->mode_bits |= SPI_LOOP | SPI_READY;
1544
1545 spi_imx->spi_drctl = spi_drctl;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001546
1547 init_completion(&spi_imx->xfer_done);
1548
1549 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001550 spi_imx->base = devm_ioremap_resource(&pdev->dev, res);
1551 if (IS_ERR(spi_imx->base)) {
1552 ret = PTR_ERR(spi_imx->base);
1553 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001554 }
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001555 spi_imx->base_phys = res->start;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001556
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001557 irq = platform_get_irq(pdev, 0);
1558 if (irq < 0) {
1559 ret = irq;
Fabio Estevam130b82c2013-07-11 01:26:48 -03001560 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001561 }
1562
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001563 ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0,
Alexander Shiyan8fc39b52014-02-22 17:23:46 +04001564 dev_name(&pdev->dev), spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001565 if (ret) {
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001566 dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001567 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001568 }
1569
Sascha Haueraa29d8402012-03-07 09:30:22 +01001570 spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1571 if (IS_ERR(spi_imx->clk_ipg)) {
1572 ret = PTR_ERR(spi_imx->clk_ipg);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001573 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001574 }
1575
Sascha Haueraa29d8402012-03-07 09:30:22 +01001576 spi_imx->clk_per = devm_clk_get(&pdev->dev, "per");
1577 if (IS_ERR(spi_imx->clk_per)) {
1578 ret = PTR_ERR(spi_imx->clk_per);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001579 goto out_master_put;
Sascha Haueraa29d8402012-03-07 09:30:22 +01001580 }
1581
Fabio Estevam83174622013-07-11 01:26:49 -03001582 ret = clk_prepare_enable(spi_imx->clk_per);
1583 if (ret)
1584 goto out_master_put;
1585
1586 ret = clk_prepare_enable(spi_imx->clk_ipg);
1587 if (ret)
1588 goto out_put_per;
Sascha Haueraa29d8402012-03-07 09:30:22 +01001589
1590 spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
Robin Gongf62cacc2014-09-11 09:18:44 +08001591 /*
Martin Kaiser2dd33f92016-10-20 00:42:25 +02001592 * Only validated on i.mx35 and i.mx6 now, can remove the constraint
1593 * if validated on other chips.
Robin Gongf62cacc2014-09-11 09:18:44 +08001594 */
jiada wangfd8d4e22017-06-08 14:16:00 +09001595 if (spi_imx->devtype_data->has_dmamode) {
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001596 ret = spi_imx_sdma_init(&pdev->dev, spi_imx, master);
Anton Bondarenkobf9af082015-12-08 07:43:46 +01001597 if (ret == -EPROBE_DEFER)
1598 goto out_clk_put;
1599
Anton Bondarenko37600472015-12-08 07:43:45 +01001600 if (ret < 0)
1601 dev_err(&pdev->dev, "dma setup error %d, use pio\n",
1602 ret);
1603 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001604
Shawn Guoedd501bb2011-07-10 01:16:35 +08001605 spi_imx->devtype_data->reset(spi_imx);
Daniel Mackce1807b2009-11-19 19:01:42 +00001606
Shawn Guoedd501bb2011-07-10 01:16:35 +08001607 spi_imx->devtype_data->intctrl(spi_imx, 0);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001608
Shawn Guo22a85e42011-07-10 01:16:41 +08001609 master->dev.of_node = pdev->dev.of_node;
Trent Piepho8197f482017-11-06 10:38:23 -08001610 ret = spi_bitbang_start(&spi_imx->bitbang);
1611 if (ret) {
1612 dev_err(&pdev->dev, "bitbang start failed with %d\n", ret);
1613 goto out_clk_put;
1614 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001615
Trent Piepho881a0b92017-10-31 12:49:04 -07001616 /* Request GPIO CS lines, if any */
1617 if (!spi_imx->slave_mode && master->cs_gpios) {
jiada wang71abd292017-09-05 14:12:32 +09001618 for (i = 0; i < master->num_chipselect; i++) {
1619 if (!gpio_is_valid(master->cs_gpios[i]))
1620 continue;
1621
1622 ret = devm_gpio_request(&pdev->dev,
1623 master->cs_gpios[i],
1624 DRIVER_NAME);
1625 if (ret) {
1626 dev_err(&pdev->dev, "Can't get CS GPIO %i\n",
1627 master->cs_gpios[i]);
Trent Piepho4e217912017-10-31 12:49:05 -07001628 goto out_spi_bitbang;
jiada wang71abd292017-09-05 14:12:32 +09001629 }
1630 }
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001631 }
1632
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001633 dev_info(&pdev->dev, "probed\n");
1634
Huang Shijie9e556dc2013-10-23 16:31:50 +08001635 clk_disable(spi_imx->clk_ipg);
1636 clk_disable(spi_imx->clk_per);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001637 return ret;
1638
Trent Piepho4e217912017-10-31 12:49:05 -07001639out_spi_bitbang:
1640 spi_bitbang_stop(&spi_imx->bitbang);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001641out_clk_put:
Sascha Haueraa29d8402012-03-07 09:30:22 +01001642 clk_disable_unprepare(spi_imx->clk_ipg);
Fabio Estevam83174622013-07-11 01:26:49 -03001643out_put_per:
1644 clk_disable_unprepare(spi_imx->clk_per);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001645out_master_put:
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001646 spi_master_put(master);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001647
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001648 return ret;
1649}
1650
Grant Likelyfd4a3192012-12-07 16:57:14 +00001651static int spi_imx_remove(struct platform_device *pdev)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001652{
1653 struct spi_master *master = platform_get_drvdata(pdev);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001654 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
Stefan Agnerd5935742018-01-07 15:05:49 +01001655 int ret;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001656
1657 spi_bitbang_stop(&spi_imx->bitbang);
1658
Stefan Agnerd5935742018-01-07 15:05:49 +01001659 ret = clk_enable(spi_imx->clk_per);
1660 if (ret)
1661 return ret;
1662
1663 ret = clk_enable(spi_imx->clk_ipg);
1664 if (ret) {
1665 clk_disable(spi_imx->clk_per);
1666 return ret;
1667 }
1668
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001669 writel(0, spi_imx->base + MXC_CSPICTRL);
Stefan Agnerd5935742018-01-07 15:05:49 +01001670 clk_disable_unprepare(spi_imx->clk_ipg);
1671 clk_disable_unprepare(spi_imx->clk_per);
Robin Gongf62cacc2014-09-11 09:18:44 +08001672 spi_imx_sdma_exit(spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001673 spi_master_put(master);
1674
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001675 return 0;
1676}
1677
1678static struct platform_driver spi_imx_driver = {
1679 .driver = {
1680 .name = DRIVER_NAME,
Shawn Guo22a85e42011-07-10 01:16:41 +08001681 .of_match_table = spi_imx_dt_ids,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001682 },
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +02001683 .id_table = spi_imx_devtype,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001684 .probe = spi_imx_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001685 .remove = spi_imx_remove,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001686};
Grant Likely940ab882011-10-05 11:29:49 -06001687module_platform_driver(spi_imx_driver);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001688
wangboaf828002018-04-12 16:58:08 +08001689MODULE_DESCRIPTION("SPI Controller driver");
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001690MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1691MODULE_LICENSE("GPL");
Fabio Estevam3133fba32013-01-07 20:42:55 -02001692MODULE_ALIAS("platform:" DRIVER_NAME);