blob: b2dd0a4d2446298ee15bcee1cd26a56fa26c6939 [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>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070011#include <linux/interrupt.h>
12#include <linux/io.h>
13#include <linux/irq.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
Clark Wang525c9e52020-07-27 14:33:54 +080016#include <linux/pinctrl/consumer.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070017#include <linux/platform_device.h>
Clark Wang525c9e52020-07-27 14:33:54 +080018#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070020#include <linux/spi/spi.h>
21#include <linux/spi/spi_bitbang.h>
22#include <linux/types.h>
Shawn Guo22a85e42011-07-10 01:16:41 +080023#include <linux/of.h>
24#include <linux/of_device.h>
Linus Walleij8cdcd8a2020-06-25 22:02:52 +020025#include <linux/property.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070026
Robin Gongf62cacc2014-09-11 09:18:44 +080027#include <linux/platform_data/dma-imx.h>
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070028
29#define DRIVER_NAME "spi_imx"
30
Trent Piepho0a9c8992019-03-04 23:02:36 +000031static bool use_dma = true;
32module_param(use_dma, bool, 0644);
33MODULE_PARM_DESC(use_dma, "Enable usage of DMA when available (default)");
34
Clark Wang525c9e52020-07-27 14:33:54 +080035#define MXC_RPM_TIMEOUT 2000 /* 2000ms */
36
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070037#define MXC_CSPIRXDATA 0x00
38#define MXC_CSPITXDATA 0x04
39#define MXC_CSPICTRL 0x08
40#define MXC_CSPIINT 0x0c
41#define MXC_RESET 0x1c
42
43/* generic defines to abstract from the different register layouts */
44#define MXC_INT_RR (1 << 0) /* Receive data ready interrupt */
45#define MXC_INT_TE (1 << 1) /* Transmit FIFO empty interrupt */
jiada wang71abd292017-09-05 14:12:32 +090046#define MXC_INT_RDR BIT(4) /* Receive date threshold interrupt */
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070047
Uwe Kleine-König30d67142018-11-30 07:47:07 +010048/* The maximum bytes that a sdma BD can transfer. */
49#define MAX_SDMA_BD_BYTES (1 << 15)
jiada wang1673c812017-08-10 13:50:08 +090050#define MX51_ECSPI_CTRL_MAX_BURST 512
jiada wang71abd292017-09-05 14:12:32 +090051/* The maximum bytes that IMX53_ECSPI can transfer in slave mode.*/
52#define MX53_MAX_TRANSFER_BYTES 512
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070053
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020054enum spi_imx_devtype {
Shawn Guo04ee5852011-07-10 01:16:39 +080055 IMX1_CSPI,
56 IMX21_CSPI,
57 IMX27_CSPI,
58 IMX31_CSPI,
59 IMX35_CSPI, /* CSPI on all i.mx except above */
jiada wang26e4bb82017-06-08 14:16:01 +090060 IMX51_ECSPI, /* ECSPI on i.mx51 */
61 IMX53_ECSPI, /* ECSPI on i.mx53 and later */
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020062};
63
64struct spi_imx_data;
65
66struct spi_imx_devtype_data {
67 void (*intctrl)(struct spi_imx_data *, int);
Uwe Kleine-Könige6972712018-11-30 07:47:05 +010068 int (*prepare_message)(struct spi_imx_data *, struct spi_message *);
Clark Wang4df2f5e2021-04-08 18:33:47 +080069 int (*prepare_transfer)(struct spi_imx_data *, struct spi_device *);
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020070 void (*trigger)(struct spi_imx_data *);
71 int (*rx_available)(struct spi_imx_data *);
Uwe Kleine-König1723e662010-09-10 09:19:18 +020072 void (*reset)(struct spi_imx_data *);
Robin Gong987a2df2018-10-10 10:32:42 +000073 void (*setup_wml)(struct spi_imx_data *);
jiada wang71abd292017-09-05 14:12:32 +090074 void (*disable)(struct spi_imx_data *);
Robin Gongbcd8e772020-05-21 04:34:17 +080075 void (*disable_dma)(struct spi_imx_data *);
jiada wangfd8d4e22017-06-08 14:16:00 +090076 bool has_dmamode;
jiada wang71abd292017-09-05 14:12:32 +090077 bool has_slavemode;
jiada wangfd8d4e22017-06-08 14:16:00 +090078 unsigned int fifo_size;
jiada wang1673c812017-08-10 13:50:08 +090079 bool dynamic_burst;
Robin Gong8eb12522021-07-14 18:20:48 +080080 /*
81 * ERR009165 fixed or not:
82 * https://www.nxp.com/docs/en/errata/IMX6DQCE.pdf
83 */
84 bool tx_glitch_fixed;
Shawn Guo04ee5852011-07-10 01:16:39 +080085 enum spi_imx_devtype devtype;
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020086};
87
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070088struct spi_imx_data {
89 struct spi_bitbang bitbang;
Sascha Hauer6aa800c2016-02-17 14:28:48 +010090 struct device *dev;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070091
92 struct completion xfer_done;
Uwe Kleine-Königcc4d22a2012-03-29 21:54:18 +020093 void __iomem *base;
Anton Bondarenkof12ae172016-02-24 09:20:29 +010094 unsigned long base_phys;
95
Sascha Haueraa29d8402012-03-07 09:30:22 +010096 struct clk *clk_per;
97 struct clk *clk_ipg;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070098 unsigned long spi_clk;
Anton Bondarenko4bfe9272016-02-19 08:43:03 +010099 unsigned int spi_bus_clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700100
Sascha Hauerd52345b2017-06-02 07:38:01 +0200101 unsigned int bits_per_word;
Leif Middelschultef72efa72017-04-23 21:19:58 +0200102 unsigned int spi_drctl;
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100103
jiada wang1673c812017-08-10 13:50:08 +0900104 unsigned int count, remainder;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700105 void (*tx)(struct spi_imx_data *);
106 void (*rx)(struct spi_imx_data *);
107 void *rx_buf;
108 const void *tx_buf;
109 unsigned int txfifo; /* number of words pushed in tx FIFO */
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200110 unsigned int dynamic_burst;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700111
jiada wang71abd292017-09-05 14:12:32 +0900112 /* Slave mode */
113 bool slave_mode;
114 bool slave_aborted;
115 unsigned int slave_burst;
116
Robin Gongf62cacc2014-09-11 09:18:44 +0800117 /* DMA */
Robin Gongf62cacc2014-09-11 09:18:44 +0800118 bool usedma;
Anton Bondarenko0dfbaa82015-12-05 17:57:01 +0100119 u32 wml;
Robin Gongf62cacc2014-09-11 09:18:44 +0800120 struct completion dma_rx_completion;
121 struct completion dma_tx_completion;
122
Uwe Kleine-König80023cb2012-05-21 21:49:35 +0200123 const struct spi_imx_devtype_data *devtype_data;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700124};
125
Shawn Guo04ee5852011-07-10 01:16:39 +0800126static inline int is_imx27_cspi(struct spi_imx_data *d)
127{
128 return d->devtype_data->devtype == IMX27_CSPI;
129}
130
131static inline int is_imx35_cspi(struct spi_imx_data *d)
132{
133 return d->devtype_data->devtype == IMX35_CSPI;
134}
135
Anton Bondarenkof8a876172015-12-05 17:57:02 +0100136static inline int is_imx51_ecspi(struct spi_imx_data *d)
137{
138 return d->devtype_data->devtype == IMX51_ECSPI;
139}
140
jiada wang26e4bb82017-06-08 14:16:01 +0900141static inline int is_imx53_ecspi(struct spi_imx_data *d)
142{
143 return d->devtype_data->devtype == IMX53_ECSPI;
144}
145
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700146#define MXC_SPI_BUF_RX(type) \
147static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \
148{ \
149 unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \
150 \
151 if (spi_imx->rx_buf) { \
152 *(type *)spi_imx->rx_buf = val; \
153 spi_imx->rx_buf += sizeof(type); \
154 } \
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200155 \
156 spi_imx->remainder -= sizeof(type); \
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700157}
158
159#define MXC_SPI_BUF_TX(type) \
160static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \
161{ \
162 type val = 0; \
163 \
164 if (spi_imx->tx_buf) { \
165 val = *(type *)spi_imx->tx_buf; \
166 spi_imx->tx_buf += sizeof(type); \
167 } \
168 \
169 spi_imx->count -= sizeof(type); \
170 \
171 writel(val, spi_imx->base + MXC_CSPITXDATA); \
172}
173
174MXC_SPI_BUF_RX(u8)
175MXC_SPI_BUF_TX(u8)
176MXC_SPI_BUF_RX(u16)
177MXC_SPI_BUF_TX(u16)
178MXC_SPI_BUF_RX(u32)
179MXC_SPI_BUF_TX(u32)
180
181/* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
182 * (which is currently not the case in this driver)
183 */
184static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
185 256, 384, 512, 768, 1024};
186
187/* MX21, MX27 */
188static unsigned int spi_imx_clkdiv_1(unsigned int fin,
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100189 unsigned int fspi, unsigned int max, unsigned int *fres)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700190{
Shawn Guo04ee5852011-07-10 01:16:39 +0800191 int i;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700192
193 for (i = 2; i < max; i++)
194 if (fspi * mxc_clkdivs[i] >= fin)
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100195 break;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700196
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100197 *fres = fin / mxc_clkdivs[i];
198 return i;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700199}
200
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200201/* MX1, MX31, MX35, MX51 CSPI */
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700202static unsigned int spi_imx_clkdiv_2(unsigned int fin,
Martin Kaiser2636ba82016-09-01 22:38:40 +0200203 unsigned int fspi, unsigned int *fres)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700204{
205 int i, div = 4;
206
207 for (i = 0; i < 7; i++) {
208 if (fspi * div >= fin)
Martin Kaiser2636ba82016-09-01 22:38:40 +0200209 goto out;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700210 div <<= 1;
211 }
212
Martin Kaiser2636ba82016-09-01 22:38:40 +0200213out:
214 *fres = fin / div;
215 return i;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700216}
217
Sascha Hauer2e312f62017-06-02 07:38:04 +0200218static int spi_imx_bytes_per_word(const int bits_per_word)
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100219{
Maxime Chevallierafb272082018-07-17 16:31:52 +0200220 if (bits_per_word <= 8)
221 return 1;
222 else if (bits_per_word <= 16)
223 return 2;
224 else
225 return 4;
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100226}
227
Robin Gongf62cacc2014-09-11 09:18:44 +0800228static bool spi_imx_can_dma(struct spi_master *master, struct spi_device *spi,
229 struct spi_transfer *transfer)
230{
231 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
232
Robin Gong7a908832020-06-17 06:42:09 +0800233 if (!use_dma || master->fallback)
Trent Piepho0a9c8992019-03-04 23:02:36 +0000234 return false;
235
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100236 if (!master->dma_rx)
237 return false;
238
jiada wang71abd292017-09-05 14:12:32 +0900239 if (spi_imx->slave_mode)
240 return false;
241
Robin Gong133eb8e2018-10-10 10:32:48 +0000242 if (transfer->len < spi_imx->devtype_data->fifo_size)
243 return false;
244
jiada wang1673c812017-08-10 13:50:08 +0900245 spi_imx->dynamic_burst = 0;
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100246
247 return true;
Robin Gongf62cacc2014-09-11 09:18:44 +0800248}
249
Shawn Guo66de7572011-07-10 01:16:37 +0800250#define MX51_ECSPI_CTRL 0x08
251#define MX51_ECSPI_CTRL_ENABLE (1 << 0)
252#define MX51_ECSPI_CTRL_XCH (1 << 2)
Robin Gongf62cacc2014-09-11 09:18:44 +0800253#define MX51_ECSPI_CTRL_SMC (1 << 3)
Shawn Guo66de7572011-07-10 01:16:37 +0800254#define MX51_ECSPI_CTRL_MODE_MASK (0xf << 4)
Leif Middelschultef72efa72017-04-23 21:19:58 +0200255#define MX51_ECSPI_CTRL_DRCTL(drctl) ((drctl) << 16)
Shawn Guo66de7572011-07-10 01:16:37 +0800256#define MX51_ECSPI_CTRL_POSTDIV_OFFSET 8
257#define MX51_ECSPI_CTRL_PREDIV_OFFSET 12
258#define MX51_ECSPI_CTRL_CS(cs) ((cs) << 18)
259#define MX51_ECSPI_CTRL_BL_OFFSET 20
jiada wang1673c812017-08-10 13:50:08 +0900260#define MX51_ECSPI_CTRL_BL_MASK (0xfff << 20)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200261
Shawn Guo66de7572011-07-10 01:16:37 +0800262#define MX51_ECSPI_CONFIG 0x0c
263#define MX51_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs) + 0))
264#define MX51_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs) + 4))
265#define MX51_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs) + 8))
266#define MX51_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs) + 12))
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200267#define MX51_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs) + 20))
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200268
Shawn Guo66de7572011-07-10 01:16:37 +0800269#define MX51_ECSPI_INT 0x10
270#define MX51_ECSPI_INT_TEEN (1 << 0)
271#define MX51_ECSPI_INT_RREN (1 << 3)
jiada wang71abd292017-09-05 14:12:32 +0900272#define MX51_ECSPI_INT_RDREN (1 << 4)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200273
Uwe Kleine-König30d67142018-11-30 07:47:07 +0100274#define MX51_ECSPI_DMA 0x14
Sascha Hauerd629c2a2016-02-24 09:20:31 +0100275#define MX51_ECSPI_DMA_TX_WML(wml) ((wml) & 0x3f)
276#define MX51_ECSPI_DMA_RX_WML(wml) (((wml) & 0x3f) << 16)
277#define MX51_ECSPI_DMA_RXT_WML(wml) (((wml) & 0x3f) << 24)
Robin Gongf62cacc2014-09-11 09:18:44 +0800278
Sascha Hauer2b0fd062016-02-24 09:20:27 +0100279#define MX51_ECSPI_DMA_TEDEN (1 << 7)
280#define MX51_ECSPI_DMA_RXDEN (1 << 23)
281#define MX51_ECSPI_DMA_RXTDEN (1 << 31)
Robin Gongf62cacc2014-09-11 09:18:44 +0800282
Shawn Guo66de7572011-07-10 01:16:37 +0800283#define MX51_ECSPI_STAT 0x18
284#define MX51_ECSPI_STAT_RR (1 << 3)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200285
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200286#define MX51_ECSPI_TESTREG 0x20
287#define MX51_ECSPI_TESTREG_LBC BIT(31)
288
jiada wang1673c812017-08-10 13:50:08 +0900289static void spi_imx_buf_rx_swap_u32(struct spi_imx_data *spi_imx)
290{
291 unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA);
Arnd Bergmann5904c9d2017-08-23 15:34:43 +0200292#ifdef __LITTLE_ENDIAN
jiada wang1673c812017-08-10 13:50:08 +0900293 unsigned int bytes_per_word;
Arnd Bergmann5904c9d2017-08-23 15:34:43 +0200294#endif
jiada wang1673c812017-08-10 13:50:08 +0900295
296 if (spi_imx->rx_buf) {
297#ifdef __LITTLE_ENDIAN
298 bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
299 if (bytes_per_word == 1)
300 val = cpu_to_be32(val);
301 else if (bytes_per_word == 2)
302 val = (val << 16) | (val >> 16);
303#endif
jiada wang1673c812017-08-10 13:50:08 +0900304 *(u32 *)spi_imx->rx_buf = val;
305 spi_imx->rx_buf += sizeof(u32);
306 }
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200307
308 spi_imx->remainder -= sizeof(u32);
jiada wang1673c812017-08-10 13:50:08 +0900309}
310
311static void spi_imx_buf_rx_swap(struct spi_imx_data *spi_imx)
312{
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200313 int unaligned;
314 u32 val;
jiada wang1673c812017-08-10 13:50:08 +0900315
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200316 unaligned = spi_imx->remainder % 4;
317
318 if (!unaligned) {
jiada wang1673c812017-08-10 13:50:08 +0900319 spi_imx_buf_rx_swap_u32(spi_imx);
320 return;
321 }
322
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200323 if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) {
jiada wang1673c812017-08-10 13:50:08 +0900324 spi_imx_buf_rx_u16(spi_imx);
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200325 return;
326 }
327
328 val = readl(spi_imx->base + MXC_CSPIRXDATA);
329
330 while (unaligned--) {
331 if (spi_imx->rx_buf) {
332 *(u8 *)spi_imx->rx_buf = (val >> (8 * unaligned)) & 0xff;
333 spi_imx->rx_buf++;
334 }
335 spi_imx->remainder--;
336 }
jiada wang1673c812017-08-10 13:50:08 +0900337}
338
339static void spi_imx_buf_tx_swap_u32(struct spi_imx_data *spi_imx)
340{
341 u32 val = 0;
Arnd Bergmann5904c9d2017-08-23 15:34:43 +0200342#ifdef __LITTLE_ENDIAN
jiada wang1673c812017-08-10 13:50:08 +0900343 unsigned int bytes_per_word;
Arnd Bergmann5904c9d2017-08-23 15:34:43 +0200344#endif
jiada wang1673c812017-08-10 13:50:08 +0900345
346 if (spi_imx->tx_buf) {
347 val = *(u32 *)spi_imx->tx_buf;
jiada wang1673c812017-08-10 13:50:08 +0900348 spi_imx->tx_buf += sizeof(u32);
349 }
350
351 spi_imx->count -= sizeof(u32);
352#ifdef __LITTLE_ENDIAN
353 bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
354
355 if (bytes_per_word == 1)
356 val = cpu_to_be32(val);
357 else if (bytes_per_word == 2)
358 val = (val << 16) | (val >> 16);
359#endif
360 writel(val, spi_imx->base + MXC_CSPITXDATA);
361}
362
363static void spi_imx_buf_tx_swap(struct spi_imx_data *spi_imx)
364{
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200365 int unaligned;
366 u32 val = 0;
jiada wang1673c812017-08-10 13:50:08 +0900367
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200368 unaligned = spi_imx->count % 4;
jiada wang1673c812017-08-10 13:50:08 +0900369
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200370 if (!unaligned) {
jiada wang1673c812017-08-10 13:50:08 +0900371 spi_imx_buf_tx_swap_u32(spi_imx);
372 return;
373 }
374
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200375 if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) {
jiada wang1673c812017-08-10 13:50:08 +0900376 spi_imx_buf_tx_u16(spi_imx);
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200377 return;
378 }
379
380 while (unaligned--) {
381 if (spi_imx->tx_buf) {
382 val |= *(u8 *)spi_imx->tx_buf << (8 * unaligned);
383 spi_imx->tx_buf++;
384 }
385 spi_imx->count--;
386 }
387
388 writel(val, spi_imx->base + MXC_CSPITXDATA);
jiada wang1673c812017-08-10 13:50:08 +0900389}
390
jiada wang71abd292017-09-05 14:12:32 +0900391static void mx53_ecspi_rx_slave(struct spi_imx_data *spi_imx)
392{
393 u32 val = be32_to_cpu(readl(spi_imx->base + MXC_CSPIRXDATA));
394
395 if (spi_imx->rx_buf) {
396 int n_bytes = spi_imx->slave_burst % sizeof(val);
397
398 if (!n_bytes)
399 n_bytes = sizeof(val);
400
401 memcpy(spi_imx->rx_buf,
402 ((u8 *)&val) + sizeof(val) - n_bytes, n_bytes);
403
404 spi_imx->rx_buf += n_bytes;
405 spi_imx->slave_burst -= n_bytes;
406 }
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200407
408 spi_imx->remainder -= sizeof(u32);
jiada wang71abd292017-09-05 14:12:32 +0900409}
410
411static void mx53_ecspi_tx_slave(struct spi_imx_data *spi_imx)
412{
413 u32 val = 0;
414 int n_bytes = spi_imx->count % sizeof(val);
415
416 if (!n_bytes)
417 n_bytes = sizeof(val);
418
419 if (spi_imx->tx_buf) {
420 memcpy(((u8 *)&val) + sizeof(val) - n_bytes,
421 spi_imx->tx_buf, n_bytes);
422 val = cpu_to_be32(val);
423 spi_imx->tx_buf += n_bytes;
424 }
425
426 spi_imx->count -= n_bytes;
427
428 writel(val, spi_imx->base + MXC_CSPITXDATA);
429}
430
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200431/* MX51 eCSPI */
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100432static unsigned int mx51_ecspi_clkdiv(struct spi_imx_data *spi_imx,
433 unsigned int fspi, unsigned int *fres)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200434{
435 /*
436 * there are two 4-bit dividers, the pre-divider divides by
437 * $pre, the post-divider by 2^$post
438 */
439 unsigned int pre, post;
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100440 unsigned int fin = spi_imx->spi_clk;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200441
442 if (unlikely(fspi > fin))
443 return 0;
444
445 post = fls(fin) - fls(fspi);
446 if (fin > fspi << post)
447 post++;
448
449 /* now we have: (fin <= fspi << post) with post being minimal */
450
451 post = max(4U, post) - 4;
452 if (unlikely(post > 0xf)) {
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100453 dev_err(spi_imx->dev, "cannot set clock freq: %u (base freq: %u)\n",
454 fspi, fin);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200455 return 0xff;
456 }
457
458 pre = DIV_ROUND_UP(fin, fspi << post) - 1;
459
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100460 dev_dbg(spi_imx->dev, "%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200461 __func__, fin, fspi, post, pre);
Marek Vasut6fd8b852013-12-18 18:31:47 +0100462
463 /* Resulting frequency for the SCLK line. */
464 *fres = (fin / (pre + 1)) >> post;
465
Shawn Guo66de7572011-07-10 01:16:37 +0800466 return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) |
467 (post << MX51_ECSPI_CTRL_POSTDIV_OFFSET);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200468}
469
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300470static void mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200471{
472 unsigned val = 0;
473
474 if (enable & MXC_INT_TE)
Shawn Guo66de7572011-07-10 01:16:37 +0800475 val |= MX51_ECSPI_INT_TEEN;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200476
477 if (enable & MXC_INT_RR)
Shawn Guo66de7572011-07-10 01:16:37 +0800478 val |= MX51_ECSPI_INT_RREN;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200479
jiada wang71abd292017-09-05 14:12:32 +0900480 if (enable & MXC_INT_RDR)
481 val |= MX51_ECSPI_INT_RDREN;
482
Shawn Guo66de7572011-07-10 01:16:37 +0800483 writel(val, spi_imx->base + MX51_ECSPI_INT);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200484}
485
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300486static void mx51_ecspi_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200487{
Sascha Hauerb03c3882016-02-24 09:20:32 +0100488 u32 reg;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200489
Sascha Hauerb03c3882016-02-24 09:20:32 +0100490 reg = readl(spi_imx->base + MX51_ECSPI_CTRL);
491 reg |= MX51_ECSPI_CTRL_XCH;
Shawn Guo66de7572011-07-10 01:16:37 +0800492 writel(reg, spi_imx->base + MX51_ECSPI_CTRL);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200493}
494
Robin Gongbcd8e772020-05-21 04:34:17 +0800495static void mx51_disable_dma(struct spi_imx_data *spi_imx)
496{
497 writel(0, spi_imx->base + MX51_ECSPI_DMA);
498}
499
jiada wang71abd292017-09-05 14:12:32 +0900500static void mx51_ecspi_disable(struct spi_imx_data *spi_imx)
501{
502 u32 ctrl;
503
504 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
505 ctrl &= ~MX51_ECSPI_CTRL_ENABLE;
506 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
507}
508
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100509static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx,
510 struct spi_message *msg)
511{
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100512 struct spi_device *spi = msg->spi;
Marek Vasut53ca18a2021-07-26 12:01:02 +0200513 struct spi_transfer *xfer;
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100514 u32 ctrl = MX51_ECSPI_CTRL_ENABLE;
Marek Vasut53ca18a2021-07-26 12:01:02 +0200515 u32 min_speed_hz = ~0U;
Marek Vasut135cbd32021-07-03 04:23:00 +0200516 u32 testreg, delay;
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100517 u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200518
jiada wang71abd292017-09-05 14:12:32 +0900519 /* set Master or Slave mode */
520 if (spi_imx->slave_mode)
521 ctrl &= ~MX51_ECSPI_CTRL_MODE_MASK;
522 else
523 ctrl |= MX51_ECSPI_CTRL_MODE_MASK;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200524
Leif Middelschultef72efa72017-04-23 21:19:58 +0200525 /*
526 * Enable SPI_RDY handling (falling edge/level triggered).
527 */
528 if (spi->mode & SPI_READY)
529 ctrl |= MX51_ECSPI_CTRL_DRCTL(spi_imx->spi_drctl);
530
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200531 /* set chip select to use */
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300532 ctrl |= MX51_ECSPI_CTRL_CS(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200533
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100534 /*
535 * The ctrl register must be written first, with the EN bit set other
536 * registers must not be written to.
537 */
538 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
539
540 testreg = readl(spi_imx->base + MX51_ECSPI_TESTREG);
541 if (spi->mode & SPI_LOOP)
542 testreg |= MX51_ECSPI_TESTREG_LBC;
jiada wang71abd292017-09-05 14:12:32 +0900543 else
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100544 testreg &= ~MX51_ECSPI_TESTREG_LBC;
545 writel(testreg, spi_imx->base + MX51_ECSPI_TESTREG);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200546
jiada wang71abd292017-09-05 14:12:32 +0900547 /*
548 * eCSPI burst completion by Chip Select signal in Slave mode
549 * is not functional for imx53 Soc, config SPI burst completed when
550 * BURST_LENGTH + 1 bits are received
551 */
552 if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx))
553 cfg &= ~MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
554 else
555 cfg |= MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200556
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300557 if (spi->mode & SPI_CPHA)
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300558 cfg |= MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100559 else
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300560 cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200561
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300562 if (spi->mode & SPI_CPOL) {
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300563 cfg |= MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
564 cfg |= MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100565 } else {
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300566 cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
567 cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200568 }
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100569
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300570 if (spi->mode & SPI_CS_HIGH)
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300571 cfg |= MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100572 else
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300573 cfg &= ~MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200574
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100575 writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
576
Marek Vasut135cbd32021-07-03 04:23:00 +0200577 /*
578 * Wait until the changes in the configuration register CONFIGREG
579 * propagate into the hardware. It takes exactly one tick of the
580 * SCLK clock, but we will wait two SCLK clock just to be sure. The
581 * effect of the delay it takes for the hardware to apply changes
582 * is noticable if the SCLK clock run very slow. In such a case, if
583 * the polarity of SCLK should be inverted, the GPIO ChipSelect might
584 * be asserted before the SCLK polarity changes, which would disrupt
585 * the SPI communication as the device on the other end would consider
586 * the change of SCLK polarity as a clock tick already.
Marek Vasut53ca18a2021-07-26 12:01:02 +0200587 *
588 * Because spi_imx->spi_bus_clk is only set in bitbang prepare_message
589 * callback, iterate over all the transfers in spi_message, find the
590 * one with lowest bus frequency, and use that bus frequency for the
591 * delay calculation. In case all transfers have speed_hz == 0, then
592 * min_speed_hz is ~0 and the resulting delay is zero.
Marek Vasut135cbd32021-07-03 04:23:00 +0200593 */
Marek Vasut53ca18a2021-07-26 12:01:02 +0200594 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
595 if (!xfer->speed_hz)
596 continue;
597 min_speed_hz = min(xfer->speed_hz, min_speed_hz);
598 }
599
600 delay = (2 * 1000000) / min_speed_hz;
Marek Vasut36c25302021-07-27 18:04:28 +0200601 if (likely(delay < 10)) /* SCLK is faster than 200 kHz */
Marek Vasut135cbd32021-07-03 04:23:00 +0200602 udelay(delay);
603 else /* SCLK is _very_ slow */
604 usleep_range(delay, delay + 10);
605
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100606 return 0;
607}
608
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100609static int mx51_ecspi_prepare_transfer(struct spi_imx_data *spi_imx,
Clark Wang4df2f5e2021-04-08 18:33:47 +0800610 struct spi_device *spi)
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100611{
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100612 u32 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
Marek Vasut135cbd32021-07-03 04:23:00 +0200613 u32 clk;
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100614
615 /* Clear BL field and set the right value */
616 ctrl &= ~MX51_ECSPI_CTRL_BL_MASK;
617 if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx))
618 ctrl |= (spi_imx->slave_burst * 8 - 1)
619 << MX51_ECSPI_CTRL_BL_OFFSET;
620 else
621 ctrl |= (spi_imx->bits_per_word - 1)
622 << MX51_ECSPI_CTRL_BL_OFFSET;
623
624 /* set clock speed */
625 ctrl &= ~(0xf << MX51_ECSPI_CTRL_POSTDIV_OFFSET |
626 0xf << MX51_ECSPI_CTRL_PREDIV_OFFSET);
Clark Wang4df2f5e2021-04-08 18:33:47 +0800627 ctrl |= mx51_ecspi_clkdiv(spi_imx, spi_imx->spi_bus_clk, &clk);
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100628 spi_imx->spi_bus_clk = clk;
629
Robin Gong8eb12522021-07-14 18:20:48 +0800630 /*
631 * ERR009165: work in XHC mode instead of SMC as PIO on the chips
632 * before i.mx6ul.
633 */
634 if (spi_imx->usedma && spi_imx->devtype_data->tx_glitch_fixed)
Sascha Hauerb03c3882016-02-24 09:20:32 +0100635 ctrl |= MX51_ECSPI_CTRL_SMC;
Robin Gong8eb12522021-07-14 18:20:48 +0800636 else
637 ctrl &= ~MX51_ECSPI_CTRL_SMC;
Sascha Hauerb03c3882016-02-24 09:20:32 +0100638
Anton Bondarenkof677f172015-12-08 07:43:43 +0100639 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
640
Robin Gong987a2df2018-10-10 10:32:42 +0000641 return 0;
642}
643
644static void mx51_setup_wml(struct spi_imx_data *spi_imx)
645{
Robin Gong8eb12522021-07-14 18:20:48 +0800646 u32 tx_wml = 0;
647
648 if (spi_imx->devtype_data->tx_glitch_fixed)
649 tx_wml = spi_imx->wml;
Robin Gongf62cacc2014-09-11 09:18:44 +0800650 /*
651 * Configure the DMA register: setup the watermark
652 * and enable DMA request.
653 */
Robin Gong5ba5a372018-10-10 10:32:45 +0000654 writel(MX51_ECSPI_DMA_RX_WML(spi_imx->wml - 1) |
Robin Gong8eb12522021-07-14 18:20:48 +0800655 MX51_ECSPI_DMA_TX_WML(tx_wml) |
Sascha Hauerd629c2a2016-02-24 09:20:31 +0100656 MX51_ECSPI_DMA_RXT_WML(spi_imx->wml) |
Sascha Hauer2b0fd062016-02-24 09:20:27 +0100657 MX51_ECSPI_DMA_TEDEN | MX51_ECSPI_DMA_RXDEN |
658 MX51_ECSPI_DMA_RXTDEN, spi_imx->base + MX51_ECSPI_DMA);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200659}
660
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300661static int mx51_ecspi_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200662{
Shawn Guo66de7572011-07-10 01:16:37 +0800663 return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200664}
665
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300666static void mx51_ecspi_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200667{
668 /* drain receive buffer */
Shawn Guo66de7572011-07-10 01:16:37 +0800669 while (mx51_ecspi_rx_available(spi_imx))
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200670 readl(spi_imx->base + MXC_CSPIRXDATA);
671}
672
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700673#define MX31_INTREG_TEEN (1 << 0)
674#define MX31_INTREG_RREN (1 << 3)
675
676#define MX31_CSPICTRL_ENABLE (1 << 0)
677#define MX31_CSPICTRL_MASTER (1 << 1)
678#define MX31_CSPICTRL_XCH (1 << 2)
Martin Kaiser2dd33f92016-10-20 00:42:25 +0200679#define MX31_CSPICTRL_SMC (1 << 3)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700680#define MX31_CSPICTRL_POL (1 << 4)
681#define MX31_CSPICTRL_PHA (1 << 5)
682#define MX31_CSPICTRL_SSCTL (1 << 6)
683#define MX31_CSPICTRL_SSPOL (1 << 7)
684#define MX31_CSPICTRL_BC_SHIFT 8
685#define MX35_CSPICTRL_BL_SHIFT 20
686#define MX31_CSPICTRL_CS_SHIFT 24
687#define MX35_CSPICTRL_CS_SHIFT 12
688#define MX31_CSPICTRL_DR_SHIFT 16
689
Martin Kaiser2dd33f92016-10-20 00:42:25 +0200690#define MX31_CSPI_DMAREG 0x10
691#define MX31_DMAREG_RH_DEN (1<<4)
692#define MX31_DMAREG_TH_DEN (1<<1)
693
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700694#define MX31_CSPISTATUS 0x14
695#define MX31_STATUS_RR (1 << 3)
696
Martin Kaiser15ca9212016-09-01 22:39:58 +0200697#define MX31_CSPI_TESTREG 0x1C
698#define MX31_TEST_LBC (1 << 14)
699
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700700/* These functions also work for the i.MX35, but be aware that
701 * the i.MX35 has a slightly different register layout for bits
702 * we do not use here.
703 */
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300704static void mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700705{
706 unsigned int val = 0;
707
708 if (enable & MXC_INT_TE)
709 val |= MX31_INTREG_TEEN;
710 if (enable & MXC_INT_RR)
711 val |= MX31_INTREG_RREN;
712
713 writel(val, spi_imx->base + MXC_CSPIINT);
714}
715
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300716static void mx31_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700717{
718 unsigned int reg;
719
720 reg = readl(spi_imx->base + MXC_CSPICTRL);
721 reg |= MX31_CSPICTRL_XCH;
722 writel(reg, spi_imx->base + MXC_CSPICTRL);
723}
724
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100725static int mx31_prepare_message(struct spi_imx_data *spi_imx,
726 struct spi_message *msg)
727{
728 return 0;
729}
730
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100731static int mx31_prepare_transfer(struct spi_imx_data *spi_imx,
Clark Wang4df2f5e2021-04-08 18:33:47 +0800732 struct spi_device *spi)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700733{
734 unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200735 unsigned int clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700736
Clark Wang4df2f5e2021-04-08 18:33:47 +0800737 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->spi_bus_clk, &clk) <<
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700738 MX31_CSPICTRL_DR_SHIFT;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200739 spi_imx->spi_bus_clk = clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700740
Shawn Guo04ee5852011-07-10 01:16:39 +0800741 if (is_imx35_cspi(spi_imx)) {
Sascha Hauerd52345b2017-06-02 07:38:01 +0200742 reg |= (spi_imx->bits_per_word - 1) << MX35_CSPICTRL_BL_SHIFT;
Shawn Guo2a64a902011-07-10 01:16:38 +0800743 reg |= MX31_CSPICTRL_SSCTL;
744 } else {
Sascha Hauerd52345b2017-06-02 07:38:01 +0200745 reg |= (spi_imx->bits_per_word - 1) << MX31_CSPICTRL_BC_SHIFT;
Shawn Guo2a64a902011-07-10 01:16:38 +0800746 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700747
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300748 if (spi->mode & SPI_CPHA)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700749 reg |= MX31_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300750 if (spi->mode & SPI_CPOL)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700751 reg |= MX31_CSPICTRL_POL;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300752 if (spi->mode & SPI_CS_HIGH)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700753 reg |= MX31_CSPICTRL_SSPOL;
Linus Walleij8cdcd8a2020-06-25 22:02:52 +0200754 if (!spi->cs_gpiod)
Greg Ungerer602c8f42017-07-11 14:22:11 +1000755 reg |= (spi->chip_select) <<
Shawn Guo04ee5852011-07-10 01:16:39 +0800756 (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT :
757 MX31_CSPICTRL_CS_SHIFT);
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200758
Martin Kaiser2dd33f92016-10-20 00:42:25 +0200759 if (spi_imx->usedma)
760 reg |= MX31_CSPICTRL_SMC;
761
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200762 writel(reg, spi_imx->base + MXC_CSPICTRL);
763
Martin Kaiser15ca9212016-09-01 22:39:58 +0200764 reg = readl(spi_imx->base + MX31_CSPI_TESTREG);
765 if (spi->mode & SPI_LOOP)
766 reg |= MX31_TEST_LBC;
767 else
768 reg &= ~MX31_TEST_LBC;
769 writel(reg, spi_imx->base + MX31_CSPI_TESTREG);
770
Martin Kaiser2dd33f92016-10-20 00:42:25 +0200771 if (spi_imx->usedma) {
Uwe Kleine-König30d67142018-11-30 07:47:07 +0100772 /*
773 * configure DMA requests when RXFIFO is half full and
774 * when TXFIFO is half empty
775 */
Martin Kaiser2dd33f92016-10-20 00:42:25 +0200776 writel(MX31_DMAREG_RH_DEN | MX31_DMAREG_TH_DEN,
777 spi_imx->base + MX31_CSPI_DMAREG);
778 }
779
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200780 return 0;
781}
782
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300783static int mx31_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700784{
785 return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
786}
787
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300788static void mx31_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200789{
790 /* drain receive buffer */
Shawn Guo2a64a902011-07-10 01:16:38 +0800791 while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200792 readl(spi_imx->base + MXC_CSPIRXDATA);
793}
794
Shawn Guo3451fb12011-07-10 01:16:36 +0800795#define MX21_INTREG_RR (1 << 4)
796#define MX21_INTREG_TEEN (1 << 9)
797#define MX21_INTREG_RREN (1 << 13)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700798
Shawn Guo3451fb12011-07-10 01:16:36 +0800799#define MX21_CSPICTRL_POL (1 << 5)
800#define MX21_CSPICTRL_PHA (1 << 6)
801#define MX21_CSPICTRL_SSPOL (1 << 8)
802#define MX21_CSPICTRL_XCH (1 << 9)
803#define MX21_CSPICTRL_ENABLE (1 << 10)
804#define MX21_CSPICTRL_MASTER (1 << 11)
805#define MX21_CSPICTRL_DR_SHIFT 14
806#define MX21_CSPICTRL_CS_SHIFT 19
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700807
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300808static void mx21_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700809{
810 unsigned int val = 0;
811
812 if (enable & MXC_INT_TE)
Shawn Guo3451fb12011-07-10 01:16:36 +0800813 val |= MX21_INTREG_TEEN;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700814 if (enable & MXC_INT_RR)
Shawn Guo3451fb12011-07-10 01:16:36 +0800815 val |= MX21_INTREG_RREN;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700816
817 writel(val, spi_imx->base + MXC_CSPIINT);
818}
819
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300820static void mx21_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700821{
822 unsigned int reg;
823
824 reg = readl(spi_imx->base + MXC_CSPICTRL);
Shawn Guo3451fb12011-07-10 01:16:36 +0800825 reg |= MX21_CSPICTRL_XCH;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700826 writel(reg, spi_imx->base + MXC_CSPICTRL);
827}
828
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100829static int mx21_prepare_message(struct spi_imx_data *spi_imx,
830 struct spi_message *msg)
831{
832 return 0;
833}
834
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100835static int mx21_prepare_transfer(struct spi_imx_data *spi_imx,
Clark Wang4df2f5e2021-04-08 18:33:47 +0800836 struct spi_device *spi)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700837{
Shawn Guo3451fb12011-07-10 01:16:36 +0800838 unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER;
Shawn Guo04ee5852011-07-10 01:16:39 +0800839 unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18;
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100840 unsigned int clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700841
Clark Wang4df2f5e2021-04-08 18:33:47 +0800842 reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, spi_imx->spi_bus_clk, max, &clk)
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100843 << MX21_CSPICTRL_DR_SHIFT;
844 spi_imx->spi_bus_clk = clk;
845
Sascha Hauerd52345b2017-06-02 07:38:01 +0200846 reg |= spi_imx->bits_per_word - 1;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700847
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300848 if (spi->mode & SPI_CPHA)
Shawn Guo3451fb12011-07-10 01:16:36 +0800849 reg |= MX21_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300850 if (spi->mode & SPI_CPOL)
Shawn Guo3451fb12011-07-10 01:16:36 +0800851 reg |= MX21_CSPICTRL_POL;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300852 if (spi->mode & SPI_CS_HIGH)
Shawn Guo3451fb12011-07-10 01:16:36 +0800853 reg |= MX21_CSPICTRL_SSPOL;
Linus Walleij8cdcd8a2020-06-25 22:02:52 +0200854 if (!spi->cs_gpiod)
Greg Ungerer602c8f42017-07-11 14:22:11 +1000855 reg |= spi->chip_select << MX21_CSPICTRL_CS_SHIFT;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700856
857 writel(reg, spi_imx->base + MXC_CSPICTRL);
858
859 return 0;
860}
861
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300862static int mx21_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700863{
Shawn Guo3451fb12011-07-10 01:16:36 +0800864 return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700865}
866
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300867static void mx21_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200868{
869 writel(1, spi_imx->base + MXC_RESET);
870}
871
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700872#define MX1_INTREG_RR (1 << 3)
873#define MX1_INTREG_TEEN (1 << 8)
874#define MX1_INTREG_RREN (1 << 11)
875
876#define MX1_CSPICTRL_POL (1 << 4)
877#define MX1_CSPICTRL_PHA (1 << 5)
878#define MX1_CSPICTRL_XCH (1 << 8)
879#define MX1_CSPICTRL_ENABLE (1 << 9)
880#define MX1_CSPICTRL_MASTER (1 << 10)
881#define MX1_CSPICTRL_DR_SHIFT 13
882
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300883static void mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700884{
885 unsigned int val = 0;
886
887 if (enable & MXC_INT_TE)
888 val |= MX1_INTREG_TEEN;
889 if (enable & MXC_INT_RR)
890 val |= MX1_INTREG_RREN;
891
892 writel(val, spi_imx->base + MXC_CSPIINT);
893}
894
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300895static void mx1_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700896{
897 unsigned int reg;
898
899 reg = readl(spi_imx->base + MXC_CSPICTRL);
900 reg |= MX1_CSPICTRL_XCH;
901 writel(reg, spi_imx->base + MXC_CSPICTRL);
902}
903
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100904static int mx1_prepare_message(struct spi_imx_data *spi_imx,
905 struct spi_message *msg)
906{
907 return 0;
908}
909
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100910static int mx1_prepare_transfer(struct spi_imx_data *spi_imx,
Clark Wang4df2f5e2021-04-08 18:33:47 +0800911 struct spi_device *spi)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700912{
913 unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200914 unsigned int clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700915
Clark Wang4df2f5e2021-04-08 18:33:47 +0800916 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->spi_bus_clk, &clk) <<
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700917 MX1_CSPICTRL_DR_SHIFT;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200918 spi_imx->spi_bus_clk = clk;
919
Sascha Hauerd52345b2017-06-02 07:38:01 +0200920 reg |= spi_imx->bits_per_word - 1;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700921
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300922 if (spi->mode & SPI_CPHA)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700923 reg |= MX1_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300924 if (spi->mode & SPI_CPOL)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700925 reg |= MX1_CSPICTRL_POL;
926
927 writel(reg, spi_imx->base + MXC_CSPICTRL);
928
929 return 0;
930}
931
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300932static int mx1_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700933{
934 return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
935}
936
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300937static void mx1_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200938{
939 writel(1, spi_imx->base + MXC_RESET);
940}
941
Shawn Guo04ee5852011-07-10 01:16:39 +0800942static struct spi_imx_devtype_data imx1_cspi_devtype_data = {
943 .intctrl = mx1_intctrl,
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100944 .prepare_message = mx1_prepare_message,
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100945 .prepare_transfer = mx1_prepare_transfer,
Shawn Guo04ee5852011-07-10 01:16:39 +0800946 .trigger = mx1_trigger,
947 .rx_available = mx1_rx_available,
948 .reset = mx1_reset,
jiada wangfd8d4e22017-06-08 14:16:00 +0900949 .fifo_size = 8,
950 .has_dmamode = false,
jiada wang1673c812017-08-10 13:50:08 +0900951 .dynamic_burst = false,
jiada wang71abd292017-09-05 14:12:32 +0900952 .has_slavemode = false,
Shawn Guo04ee5852011-07-10 01:16:39 +0800953 .devtype = IMX1_CSPI,
954};
955
956static struct spi_imx_devtype_data imx21_cspi_devtype_data = {
957 .intctrl = mx21_intctrl,
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100958 .prepare_message = mx21_prepare_message,
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100959 .prepare_transfer = mx21_prepare_transfer,
Shawn Guo04ee5852011-07-10 01:16:39 +0800960 .trigger = mx21_trigger,
961 .rx_available = mx21_rx_available,
962 .reset = mx21_reset,
jiada wangfd8d4e22017-06-08 14:16:00 +0900963 .fifo_size = 8,
964 .has_dmamode = false,
jiada wang1673c812017-08-10 13:50:08 +0900965 .dynamic_burst = false,
jiada wang71abd292017-09-05 14:12:32 +0900966 .has_slavemode = false,
Shawn Guo04ee5852011-07-10 01:16:39 +0800967 .devtype = IMX21_CSPI,
968};
969
970static struct spi_imx_devtype_data imx27_cspi_devtype_data = {
971 /* i.mx27 cspi shares the functions with i.mx21 one */
972 .intctrl = mx21_intctrl,
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100973 .prepare_message = mx21_prepare_message,
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100974 .prepare_transfer = mx21_prepare_transfer,
Shawn Guo04ee5852011-07-10 01:16:39 +0800975 .trigger = mx21_trigger,
976 .rx_available = mx21_rx_available,
977 .reset = mx21_reset,
jiada wangfd8d4e22017-06-08 14:16:00 +0900978 .fifo_size = 8,
979 .has_dmamode = false,
jiada wang1673c812017-08-10 13:50:08 +0900980 .dynamic_burst = false,
jiada wang71abd292017-09-05 14:12:32 +0900981 .has_slavemode = false,
Shawn Guo04ee5852011-07-10 01:16:39 +0800982 .devtype = IMX27_CSPI,
983};
984
985static struct spi_imx_devtype_data imx31_cspi_devtype_data = {
986 .intctrl = mx31_intctrl,
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100987 .prepare_message = mx31_prepare_message,
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100988 .prepare_transfer = mx31_prepare_transfer,
Shawn Guo04ee5852011-07-10 01:16:39 +0800989 .trigger = mx31_trigger,
990 .rx_available = mx31_rx_available,
991 .reset = mx31_reset,
jiada wangfd8d4e22017-06-08 14:16:00 +0900992 .fifo_size = 8,
993 .has_dmamode = false,
jiada wang1673c812017-08-10 13:50:08 +0900994 .dynamic_burst = false,
jiada wang71abd292017-09-05 14:12:32 +0900995 .has_slavemode = false,
Shawn Guo04ee5852011-07-10 01:16:39 +0800996 .devtype = IMX31_CSPI,
997};
998
999static struct spi_imx_devtype_data imx35_cspi_devtype_data = {
1000 /* i.mx35 and later cspi shares the functions with i.mx31 one */
1001 .intctrl = mx31_intctrl,
Uwe Kleine-Könige6972712018-11-30 07:47:05 +01001002 .prepare_message = mx31_prepare_message,
Uwe Kleine-König1d374702018-11-30 07:47:08 +01001003 .prepare_transfer = mx31_prepare_transfer,
Shawn Guo04ee5852011-07-10 01:16:39 +08001004 .trigger = mx31_trigger,
1005 .rx_available = mx31_rx_available,
1006 .reset = mx31_reset,
jiada wangfd8d4e22017-06-08 14:16:00 +09001007 .fifo_size = 8,
1008 .has_dmamode = true,
jiada wang1673c812017-08-10 13:50:08 +09001009 .dynamic_burst = false,
jiada wang71abd292017-09-05 14:12:32 +09001010 .has_slavemode = false,
Shawn Guo04ee5852011-07-10 01:16:39 +08001011 .devtype = IMX35_CSPI,
1012};
1013
1014static struct spi_imx_devtype_data imx51_ecspi_devtype_data = {
1015 .intctrl = mx51_ecspi_intctrl,
Uwe Kleine-Könige6972712018-11-30 07:47:05 +01001016 .prepare_message = mx51_ecspi_prepare_message,
Uwe Kleine-König1d374702018-11-30 07:47:08 +01001017 .prepare_transfer = mx51_ecspi_prepare_transfer,
Shawn Guo04ee5852011-07-10 01:16:39 +08001018 .trigger = mx51_ecspi_trigger,
1019 .rx_available = mx51_ecspi_rx_available,
1020 .reset = mx51_ecspi_reset,
Robin Gong987a2df2018-10-10 10:32:42 +00001021 .setup_wml = mx51_setup_wml,
Robin Gongbcd8e772020-05-21 04:34:17 +08001022 .disable_dma = mx51_disable_dma,
jiada wangfd8d4e22017-06-08 14:16:00 +09001023 .fifo_size = 64,
1024 .has_dmamode = true,
jiada wang1673c812017-08-10 13:50:08 +09001025 .dynamic_burst = true,
jiada wang71abd292017-09-05 14:12:32 +09001026 .has_slavemode = true,
1027 .disable = mx51_ecspi_disable,
Shawn Guo04ee5852011-07-10 01:16:39 +08001028 .devtype = IMX51_ECSPI,
1029};
1030
jiada wang26e4bb82017-06-08 14:16:01 +09001031static struct spi_imx_devtype_data imx53_ecspi_devtype_data = {
1032 .intctrl = mx51_ecspi_intctrl,
Uwe Kleine-Könige6972712018-11-30 07:47:05 +01001033 .prepare_message = mx51_ecspi_prepare_message,
Uwe Kleine-König1d374702018-11-30 07:47:08 +01001034 .prepare_transfer = mx51_ecspi_prepare_transfer,
jiada wang26e4bb82017-06-08 14:16:01 +09001035 .trigger = mx51_ecspi_trigger,
1036 .rx_available = mx51_ecspi_rx_available,
Robin Gongbcd8e772020-05-21 04:34:17 +08001037 .disable_dma = mx51_disable_dma,
jiada wang26e4bb82017-06-08 14:16:01 +09001038 .reset = mx51_ecspi_reset,
1039 .fifo_size = 64,
1040 .has_dmamode = true,
jiada wang71abd292017-09-05 14:12:32 +09001041 .has_slavemode = true,
1042 .disable = mx51_ecspi_disable,
jiada wang26e4bb82017-06-08 14:16:01 +09001043 .devtype = IMX53_ECSPI,
1044};
1045
Robin Gong8eb12522021-07-14 18:20:48 +08001046static struct spi_imx_devtype_data imx6ul_ecspi_devtype_data = {
1047 .intctrl = mx51_ecspi_intctrl,
1048 .prepare_message = mx51_ecspi_prepare_message,
1049 .prepare_transfer = mx51_ecspi_prepare_transfer,
1050 .trigger = mx51_ecspi_trigger,
1051 .rx_available = mx51_ecspi_rx_available,
1052 .reset = mx51_ecspi_reset,
1053 .setup_wml = mx51_setup_wml,
1054 .fifo_size = 64,
1055 .has_dmamode = true,
1056 .dynamic_burst = true,
1057 .has_slavemode = true,
1058 .tx_glitch_fixed = true,
1059 .disable = mx51_ecspi_disable,
1060 .devtype = IMX51_ECSPI,
1061};
1062
Shawn Guo22a85e42011-07-10 01:16:41 +08001063static const struct of_device_id spi_imx_dt_ids[] = {
1064 { .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, },
1065 { .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, },
1066 { .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, },
1067 { .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, },
1068 { .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, },
1069 { .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, },
jiada wang26e4bb82017-06-08 14:16:01 +09001070 { .compatible = "fsl,imx53-ecspi", .data = &imx53_ecspi_devtype_data, },
Robin Gong8eb12522021-07-14 18:20:48 +08001071 { .compatible = "fsl,imx6ul-ecspi", .data = &imx6ul_ecspi_devtype_data, },
Shawn Guo22a85e42011-07-10 01:16:41 +08001072 { /* sentinel */ }
1073};
Niels de Vos27743e02013-07-29 09:38:05 +02001074MODULE_DEVICE_TABLE(of, spi_imx_dt_ids);
Shawn Guo22a85e42011-07-10 01:16:41 +08001075
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001076static void spi_imx_set_burst_len(struct spi_imx_data *spi_imx, int n_bits)
1077{
1078 u32 ctrl;
1079
1080 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
1081 ctrl &= ~MX51_ECSPI_CTRL_BL_MASK;
1082 ctrl |= ((n_bits - 1) << MX51_ECSPI_CTRL_BL_OFFSET);
1083 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
1084}
1085
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001086static void spi_imx_push(struct spi_imx_data *spi_imx)
1087{
Uwe Kleine-Königbd961692021-07-16 19:39:27 +02001088 unsigned int burst_len;
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001089
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001090 /*
1091 * Reload the FIFO when the remaining bytes to be transferred in the
1092 * current burst is 0. This only applies when bits_per_word is a
1093 * multiple of 8.
1094 */
1095 if (!spi_imx->remainder) {
1096 if (spi_imx->dynamic_burst) {
1097
1098 /* We need to deal unaligned data first */
1099 burst_len = spi_imx->count % MX51_ECSPI_CTRL_MAX_BURST;
1100
1101 if (!burst_len)
1102 burst_len = MX51_ECSPI_CTRL_MAX_BURST;
1103
1104 spi_imx_set_burst_len(spi_imx, burst_len * 8);
1105
1106 spi_imx->remainder = burst_len;
1107 } else {
Uwe Kleine-Königbd961692021-07-16 19:39:27 +02001108 spi_imx->remainder = spi_imx_bytes_per_word(spi_imx->bits_per_word);
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001109 }
1110 }
1111
jiada wangfd8d4e22017-06-08 14:16:00 +09001112 while (spi_imx->txfifo < spi_imx->devtype_data->fifo_size) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001113 if (!spi_imx->count)
1114 break;
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001115 if (spi_imx->dynamic_burst &&
Uwe Kleine-Königbd961692021-07-16 19:39:27 +02001116 spi_imx->txfifo >= DIV_ROUND_UP(spi_imx->remainder, 4))
jiada wang1673c812017-08-10 13:50:08 +09001117 break;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001118 spi_imx->tx(spi_imx);
1119 spi_imx->txfifo++;
1120 }
1121
jiada wang71abd292017-09-05 14:12:32 +09001122 if (!spi_imx->slave_mode)
1123 spi_imx->devtype_data->trigger(spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001124}
1125
1126static irqreturn_t spi_imx_isr(int irq, void *dev_id)
1127{
1128 struct spi_imx_data *spi_imx = dev_id;
1129
jiada wang71abd292017-09-05 14:12:32 +09001130 while (spi_imx->txfifo &&
1131 spi_imx->devtype_data->rx_available(spi_imx)) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001132 spi_imx->rx(spi_imx);
1133 spi_imx->txfifo--;
1134 }
1135
1136 if (spi_imx->count) {
1137 spi_imx_push(spi_imx);
1138 return IRQ_HANDLED;
1139 }
1140
1141 if (spi_imx->txfifo) {
1142 /* No data left to push, but still waiting for rx data,
1143 * enable receive data available interrupt.
1144 */
Shawn Guoedd501bb2011-07-10 01:16:35 +08001145 spi_imx->devtype_data->intctrl(
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +02001146 spi_imx, MXC_INT_RR);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001147 return IRQ_HANDLED;
1148 }
1149
Shawn Guoedd501bb2011-07-10 01:16:35 +08001150 spi_imx->devtype_data->intctrl(spi_imx, 0);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001151 complete(&spi_imx->xfer_done);
1152
1153 return IRQ_HANDLED;
1154}
1155
Sascha Hauer65017ee2017-06-02 07:38:03 +02001156static int spi_imx_dma_configure(struct spi_master *master)
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001157{
1158 int ret;
1159 enum dma_slave_buswidth buswidth;
1160 struct dma_slave_config rx = {}, tx = {};
1161 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1162
Sascha Hauer65017ee2017-06-02 07:38:03 +02001163 switch (spi_imx_bytes_per_word(spi_imx->bits_per_word)) {
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001164 case 4:
1165 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
1166 break;
1167 case 2:
1168 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
1169 break;
1170 case 1:
1171 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
1172 break;
1173 default:
1174 return -EINVAL;
1175 }
1176
1177 tx.direction = DMA_MEM_TO_DEV;
1178 tx.dst_addr = spi_imx->base_phys + MXC_CSPITXDATA;
1179 tx.dst_addr_width = buswidth;
1180 tx.dst_maxburst = spi_imx->wml;
1181 ret = dmaengine_slave_config(master->dma_tx, &tx);
1182 if (ret) {
1183 dev_err(spi_imx->dev, "TX dma configuration failed with %d\n", ret);
1184 return ret;
1185 }
1186
1187 rx.direction = DMA_DEV_TO_MEM;
1188 rx.src_addr = spi_imx->base_phys + MXC_CSPIRXDATA;
1189 rx.src_addr_width = buswidth;
1190 rx.src_maxburst = spi_imx->wml;
1191 ret = dmaengine_slave_config(master->dma_rx, &rx);
1192 if (ret) {
1193 dev_err(spi_imx->dev, "RX dma configuration failed with %d\n", ret);
1194 return ret;
1195 }
1196
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001197 return 0;
1198}
1199
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001200static int spi_imx_setupxfer(struct spi_device *spi,
1201 struct spi_transfer *t)
1202{
1203 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001204
Sascha Hauerabb1ff12017-06-02 07:37:59 +02001205 if (!t)
1206 return 0;
1207
Clark Wang4df2f5e2021-04-08 18:33:47 +08001208 if (!t->speed_hz) {
1209 if (!spi->max_speed_hz) {
1210 dev_err(&spi->dev, "no speed_hz provided!\n");
1211 return -EINVAL;
1212 }
1213 dev_dbg(&spi->dev, "using spi->max_speed_hz!\n");
1214 spi_imx->spi_bus_clk = spi->max_speed_hz;
1215 } else
1216 spi_imx->spi_bus_clk = t->speed_hz;
1217
Sascha Hauerd52345b2017-06-02 07:38:01 +02001218 spi_imx->bits_per_word = t->bits_per_word;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001219
Maxime Chevallier2801b2f52018-07-17 16:31:51 +02001220 /*
1221 * Initialize the functions for transfer. To transfer non byte-aligned
1222 * words, we have to use multiple word-size bursts, we can't use
1223 * dynamic_burst in that case.
1224 */
1225 if (spi_imx->devtype_data->dynamic_burst && !spi_imx->slave_mode &&
Uwe Kleine-König6e95b232021-07-27 14:42:26 +02001226 !(spi->mode & SPI_CS_WORD) &&
Maxime Chevallier2801b2f52018-07-17 16:31:51 +02001227 (spi_imx->bits_per_word == 8 ||
1228 spi_imx->bits_per_word == 16 ||
1229 spi_imx->bits_per_word == 32)) {
jiada wang1673c812017-08-10 13:50:08 +09001230
jiada wang1673c812017-08-10 13:50:08 +09001231 spi_imx->rx = spi_imx_buf_rx_swap;
1232 spi_imx->tx = spi_imx_buf_tx_swap;
1233 spi_imx->dynamic_burst = 1;
jiada wang1673c812017-08-10 13:50:08 +09001234
Sachin Kamat60514262013-05-30 13:38:09 +05301235 } else {
jiada wang1673c812017-08-10 13:50:08 +09001236 if (spi_imx->bits_per_word <= 8) {
1237 spi_imx->rx = spi_imx_buf_rx_u8;
1238 spi_imx->tx = spi_imx_buf_tx_u8;
1239 } else if (spi_imx->bits_per_word <= 16) {
1240 spi_imx->rx = spi_imx_buf_rx_u16;
1241 spi_imx->tx = spi_imx_buf_tx_u16;
1242 } else {
1243 spi_imx->rx = spi_imx_buf_rx_u32;
1244 spi_imx->tx = spi_imx_buf_tx_u32;
1245 }
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001246 spi_imx->dynamic_burst = 0;
Stephen Warren24778be2013-05-21 20:36:35 -06001247 }
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -07001248
Sascha Hauerc008a802016-02-24 09:20:26 +01001249 if (spi_imx_can_dma(spi_imx->bitbang.master, spi, t))
zhengbine6a8b2c2019-12-24 11:52:05 +08001250 spi_imx->usedma = true;
Sascha Hauerc008a802016-02-24 09:20:26 +01001251 else
zhengbine6a8b2c2019-12-24 11:52:05 +08001252 spi_imx->usedma = false;
Sascha Hauerc008a802016-02-24 09:20:26 +01001253
jiada wang71abd292017-09-05 14:12:32 +09001254 if (is_imx53_ecspi(spi_imx) && spi_imx->slave_mode) {
1255 spi_imx->rx = mx53_ecspi_rx_slave;
1256 spi_imx->tx = mx53_ecspi_tx_slave;
1257 spi_imx->slave_burst = t->len;
1258 }
1259
Clark Wang4df2f5e2021-04-08 18:33:47 +08001260 spi_imx->devtype_data->prepare_transfer(spi_imx, spi);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001261
1262 return 0;
1263}
1264
Robin Gongf62cacc2014-09-11 09:18:44 +08001265static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx)
1266{
1267 struct spi_master *master = spi_imx->bitbang.master;
1268
1269 if (master->dma_rx) {
1270 dma_release_channel(master->dma_rx);
1271 master->dma_rx = NULL;
1272 }
1273
1274 if (master->dma_tx) {
1275 dma_release_channel(master->dma_tx);
1276 master->dma_tx = NULL;
1277 }
Robin Gongf62cacc2014-09-11 09:18:44 +08001278}
1279
1280static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx,
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001281 struct spi_master *master)
Robin Gongf62cacc2014-09-11 09:18:44 +08001282{
Robin Gongf62cacc2014-09-11 09:18:44 +08001283 int ret;
1284
jiada wangfd8d4e22017-06-08 14:16:00 +09001285 spi_imx->wml = spi_imx->devtype_data->fifo_size / 2;
Anton Bondarenko0dfbaa82015-12-05 17:57:01 +01001286
Robin Gongf62cacc2014-09-11 09:18:44 +08001287 /* Prepare for TX DMA: */
Peter Ujfalusi5d3aa9c2019-11-13 11:42:51 +02001288 master->dma_tx = dma_request_chan(dev, "tx");
Anton Bondarenko37600472015-12-08 07:43:45 +01001289 if (IS_ERR(master->dma_tx)) {
1290 ret = PTR_ERR(master->dma_tx);
1291 dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret);
1292 master->dma_tx = NULL;
Robin Gongf62cacc2014-09-11 09:18:44 +08001293 goto err;
1294 }
1295
Robin Gongf62cacc2014-09-11 09:18:44 +08001296 /* Prepare for RX : */
Peter Ujfalusi5d3aa9c2019-11-13 11:42:51 +02001297 master->dma_rx = dma_request_chan(dev, "rx");
Anton Bondarenko37600472015-12-08 07:43:45 +01001298 if (IS_ERR(master->dma_rx)) {
1299 ret = PTR_ERR(master->dma_rx);
1300 dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret);
1301 master->dma_rx = NULL;
Robin Gongf62cacc2014-09-11 09:18:44 +08001302 goto err;
1303 }
1304
Robin Gongf62cacc2014-09-11 09:18:44 +08001305 init_completion(&spi_imx->dma_rx_completion);
1306 init_completion(&spi_imx->dma_tx_completion);
1307 master->can_dma = spi_imx_can_dma;
1308 master->max_dma_len = MAX_SDMA_BD_BYTES;
1309 spi_imx->bitbang.master->flags = SPI_MASTER_MUST_RX |
1310 SPI_MASTER_MUST_TX;
Robin Gongf62cacc2014-09-11 09:18:44 +08001311
1312 return 0;
1313err:
1314 spi_imx_sdma_exit(spi_imx);
1315 return ret;
1316}
1317
1318static void spi_imx_dma_rx_callback(void *cookie)
1319{
1320 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
1321
1322 complete(&spi_imx->dma_rx_completion);
1323}
1324
1325static void spi_imx_dma_tx_callback(void *cookie)
1326{
1327 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
1328
1329 complete(&spi_imx->dma_tx_completion);
1330}
1331
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001332static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size)
1333{
1334 unsigned long timeout = 0;
1335
1336 /* Time with actual data transfer and CS change delay related to HW */
1337 timeout = (8 + 4) * size / spi_imx->spi_bus_clk;
1338
1339 /* Add extra second for scheduler related activities */
1340 timeout += 1;
1341
1342 /* Double calculated timeout */
1343 return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);
1344}
1345
Robin Gongf62cacc2014-09-11 09:18:44 +08001346static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,
1347 struct spi_transfer *transfer)
1348{
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001349 struct dma_async_tx_descriptor *desc_tx, *desc_rx;
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001350 unsigned long transfer_timeout;
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001351 unsigned long timeout;
Robin Gongf62cacc2014-09-11 09:18:44 +08001352 struct spi_master *master = spi_imx->bitbang.master;
1353 struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
Robin Gong5ba5a372018-10-10 10:32:45 +00001354 struct scatterlist *last_sg = sg_last(rx->sgl, rx->nents);
1355 unsigned int bytes_per_word, i;
Robin Gong987a2df2018-10-10 10:32:42 +00001356 int ret;
1357
Robin Gong5ba5a372018-10-10 10:32:45 +00001358 /* Get the right burst length from the last sg to ensure no tail data */
1359 bytes_per_word = spi_imx_bytes_per_word(transfer->bits_per_word);
1360 for (i = spi_imx->devtype_data->fifo_size / 2; i > 0; i--) {
1361 if (!(sg_dma_len(last_sg) % (i * bytes_per_word)))
1362 break;
1363 }
1364 /* Use 1 as wml in case no available burst length got */
1365 if (i == 0)
1366 i = 1;
1367
1368 spi_imx->wml = i;
1369
Robin Gong987a2df2018-10-10 10:32:42 +00001370 ret = spi_imx_dma_configure(master);
1371 if (ret)
Robin Gong7a908832020-06-17 06:42:09 +08001372 goto dma_failure_no_start;
Robin Gong987a2df2018-10-10 10:32:42 +00001373
Robin Gong5ba5a372018-10-10 10:32:45 +00001374 if (!spi_imx->devtype_data->setup_wml) {
1375 dev_err(spi_imx->dev, "No setup_wml()?\n");
Robin Gong7a908832020-06-17 06:42:09 +08001376 ret = -EINVAL;
1377 goto dma_failure_no_start;
Robin Gong5ba5a372018-10-10 10:32:45 +00001378 }
Robin Gong987a2df2018-10-10 10:32:42 +00001379 spi_imx->devtype_data->setup_wml(spi_imx);
Robin Gongf62cacc2014-09-11 09:18:44 +08001380
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001381 /*
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001382 * The TX DMA setup starts the transfer, so make sure RX is configured
1383 * before TX.
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001384 */
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001385 desc_rx = dmaengine_prep_slave_sg(master->dma_rx,
1386 rx->sgl, rx->nents, DMA_DEV_TO_MEM,
1387 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Robin Gong7a908832020-06-17 06:42:09 +08001388 if (!desc_rx) {
1389 ret = -EINVAL;
1390 goto dma_failure_no_start;
1391 }
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001392
1393 desc_rx->callback = spi_imx_dma_rx_callback;
1394 desc_rx->callback_param = (void *)spi_imx;
1395 dmaengine_submit(desc_rx);
1396 reinit_completion(&spi_imx->dma_rx_completion);
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001397 dma_async_issue_pending(master->dma_rx);
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001398
1399 desc_tx = dmaengine_prep_slave_sg(master->dma_tx,
1400 tx->sgl, tx->nents, DMA_MEM_TO_DEV,
1401 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1402 if (!desc_tx) {
1403 dmaengine_terminate_all(master->dma_tx);
Robin Gongbcd8e772020-05-21 04:34:17 +08001404 dmaengine_terminate_all(master->dma_rx);
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001405 return -EINVAL;
1406 }
1407
1408 desc_tx->callback = spi_imx_dma_tx_callback;
1409 desc_tx->callback_param = (void *)spi_imx;
1410 dmaengine_submit(desc_tx);
1411 reinit_completion(&spi_imx->dma_tx_completion);
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001412 dma_async_issue_pending(master->dma_tx);
Robin Gongf62cacc2014-09-11 09:18:44 +08001413
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001414 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1415
Robin Gongf62cacc2014-09-11 09:18:44 +08001416 /* Wait SDMA to finish the data transfer.*/
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001417 timeout = wait_for_completion_timeout(&spi_imx->dma_tx_completion,
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001418 transfer_timeout);
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001419 if (!timeout) {
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001420 dev_err(spi_imx->dev, "I/O Error in DMA TX\n");
Robin Gongf62cacc2014-09-11 09:18:44 +08001421 dmaengine_terminate_all(master->dma_tx);
Anton Bondarenkoe47b33c2015-12-05 17:56:59 +01001422 dmaengine_terminate_all(master->dma_rx);
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001423 return -ETIMEDOUT;
Robin Gongf62cacc2014-09-11 09:18:44 +08001424 }
1425
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001426 timeout = wait_for_completion_timeout(&spi_imx->dma_rx_completion,
1427 transfer_timeout);
1428 if (!timeout) {
1429 dev_err(&master->dev, "I/O Error in DMA RX\n");
1430 spi_imx->devtype_data->reset(spi_imx);
1431 dmaengine_terminate_all(master->dma_rx);
1432 return -ETIMEDOUT;
1433 }
Robin Gongf62cacc2014-09-11 09:18:44 +08001434
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001435 return transfer->len;
Robin Gong7a908832020-06-17 06:42:09 +08001436/* fallback to pio */
1437dma_failure_no_start:
1438 transfer->error |= SPI_TRANS_FAIL_NO_START;
1439 return ret;
Robin Gongf62cacc2014-09-11 09:18:44 +08001440}
1441
1442static int spi_imx_pio_transfer(struct spi_device *spi,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001443 struct spi_transfer *transfer)
1444{
1445 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Christian Gmeinerff1ba3d2016-06-21 14:12:54 +02001446 unsigned long transfer_timeout;
1447 unsigned long timeout;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001448
1449 spi_imx->tx_buf = transfer->tx_buf;
1450 spi_imx->rx_buf = transfer->rx_buf;
1451 spi_imx->count = transfer->len;
1452 spi_imx->txfifo = 0;
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001453 spi_imx->remainder = 0;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001454
Axel Linaa0fe822014-02-09 11:06:04 +08001455 reinit_completion(&spi_imx->xfer_done);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001456
1457 spi_imx_push(spi_imx);
1458
Shawn Guoedd501bb2011-07-10 01:16:35 +08001459 spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001460
Christian Gmeinerff1ba3d2016-06-21 14:12:54 +02001461 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1462
1463 timeout = wait_for_completion_timeout(&spi_imx->xfer_done,
1464 transfer_timeout);
1465 if (!timeout) {
1466 dev_err(&spi->dev, "I/O Error in PIO\n");
1467 spi_imx->devtype_data->reset(spi_imx);
1468 return -ETIMEDOUT;
1469 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001470
1471 return transfer->len;
1472}
1473
jiada wang71abd292017-09-05 14:12:32 +09001474static int spi_imx_pio_transfer_slave(struct spi_device *spi,
1475 struct spi_transfer *transfer)
1476{
1477 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1478 int ret = transfer->len;
1479
1480 if (is_imx53_ecspi(spi_imx) &&
1481 transfer->len > MX53_MAX_TRANSFER_BYTES) {
1482 dev_err(&spi->dev, "Transaction too big, max size is %d bytes\n",
1483 MX53_MAX_TRANSFER_BYTES);
1484 return -EMSGSIZE;
1485 }
1486
1487 spi_imx->tx_buf = transfer->tx_buf;
1488 spi_imx->rx_buf = transfer->rx_buf;
1489 spi_imx->count = transfer->len;
1490 spi_imx->txfifo = 0;
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001491 spi_imx->remainder = 0;
jiada wang71abd292017-09-05 14:12:32 +09001492
1493 reinit_completion(&spi_imx->xfer_done);
1494 spi_imx->slave_aborted = false;
1495
1496 spi_imx_push(spi_imx);
1497
1498 spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE | MXC_INT_RDR);
1499
1500 if (wait_for_completion_interruptible(&spi_imx->xfer_done) ||
1501 spi_imx->slave_aborted) {
1502 dev_dbg(&spi->dev, "interrupted\n");
1503 ret = -EINTR;
1504 }
1505
1506 /* ecspi has a HW issue when works in Slave mode,
1507 * after 64 words writtern to TXFIFO, even TXFIFO becomes empty,
1508 * ECSPI_TXDATA keeps shift out the last word data,
1509 * so we have to disable ECSPI when in slave mode after the
1510 * transfer completes
1511 */
1512 if (spi_imx->devtype_data->disable)
1513 spi_imx->devtype_data->disable(spi_imx);
1514
1515 return ret;
1516}
1517
Robin Gongf62cacc2014-09-11 09:18:44 +08001518static int spi_imx_transfer(struct spi_device *spi,
1519 struct spi_transfer *transfer)
1520{
Robin Gongf62cacc2014-09-11 09:18:44 +08001521 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1522
Marc Kleine-Buddebf253e62020-09-17 22:24:20 +02001523 transfer->effective_speed_hz = spi_imx->spi_bus_clk;
1524
jiada wang71abd292017-09-05 14:12:32 +09001525 /* flush rxfifo before transfer */
1526 while (spi_imx->devtype_data->rx_available(spi_imx))
Trent Piephoc8427492019-03-04 20:18:49 +00001527 readl(spi_imx->base + MXC_CSPIRXDATA);
jiada wang71abd292017-09-05 14:12:32 +09001528
1529 if (spi_imx->slave_mode)
1530 return spi_imx_pio_transfer_slave(spi, transfer);
1531
Robin Gong7a908832020-06-17 06:42:09 +08001532 if (spi_imx->usedma)
1533 return spi_imx_dma_transfer(spi_imx, transfer);
Robin Gongbcd8e772020-05-21 04:34:17 +08001534
1535 return spi_imx_pio_transfer(spi, transfer);
Robin Gongf62cacc2014-09-11 09:18:44 +08001536}
1537
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001538static int spi_imx_setup(struct spi_device *spi)
1539{
Alberto Panizzof4d4ecf2010-01-20 13:49:45 -07001540 dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001541 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1542
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001543 return 0;
1544}
1545
1546static void spi_imx_cleanup(struct spi_device *spi)
1547{
1548}
1549
Huang Shijie9e556dc2013-10-23 16:31:50 +08001550static int
1551spi_imx_prepare_message(struct spi_master *master, struct spi_message *msg)
1552{
1553 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1554 int ret;
1555
Clark Wang525c9e52020-07-27 14:33:54 +08001556 ret = pm_runtime_get_sync(spi_imx->dev);
1557 if (ret < 0) {
Zhang Qilong1dcbdd92020-11-02 22:58:35 +08001558 pm_runtime_put_noidle(spi_imx->dev);
Clark Wang525c9e52020-07-27 14:33:54 +08001559 dev_err(spi_imx->dev, "failed to enable clock\n");
Huang Shijie9e556dc2013-10-23 16:31:50 +08001560 return ret;
1561 }
1562
Uwe Kleine-Könige6972712018-11-30 07:47:05 +01001563 ret = spi_imx->devtype_data->prepare_message(spi_imx, msg);
1564 if (ret) {
Clark Wang525c9e52020-07-27 14:33:54 +08001565 pm_runtime_mark_last_busy(spi_imx->dev);
1566 pm_runtime_put_autosuspend(spi_imx->dev);
Uwe Kleine-Könige6972712018-11-30 07:47:05 +01001567 }
1568
1569 return ret;
Huang Shijie9e556dc2013-10-23 16:31:50 +08001570}
1571
1572static int
1573spi_imx_unprepare_message(struct spi_master *master, struct spi_message *msg)
1574{
1575 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1576
Clark Wang525c9e52020-07-27 14:33:54 +08001577 pm_runtime_mark_last_busy(spi_imx->dev);
1578 pm_runtime_put_autosuspend(spi_imx->dev);
Huang Shijie9e556dc2013-10-23 16:31:50 +08001579 return 0;
1580}
1581
jiada wang71abd292017-09-05 14:12:32 +09001582static int spi_imx_slave_abort(struct spi_master *master)
1583{
1584 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1585
1586 spi_imx->slave_aborted = true;
1587 complete(&spi_imx->xfer_done);
1588
1589 return 0;
1590}
1591
Grant Likelyfd4a3192012-12-07 16:57:14 +00001592static int spi_imx_probe(struct platform_device *pdev)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001593{
Shawn Guo22a85e42011-07-10 01:16:41 +08001594 struct device_node *np = pdev->dev.of_node;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001595 struct spi_master *master;
1596 struct spi_imx_data *spi_imx;
1597 struct resource *res;
Linus Walleij8cdcd8a2020-06-25 22:02:52 +02001598 int ret, irq, spi_drctl;
Tian Tao200d925e2021-03-22 11:57:56 +08001599 const struct spi_imx_devtype_data *devtype_data =
1600 of_device_get_match_data(&pdev->dev);
jiada wang71abd292017-09-05 14:12:32 +09001601 bool slave_mode;
Linus Walleij8cdcd8a2020-06-25 22:02:52 +02001602 u32 val;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001603
jiada wang71abd292017-09-05 14:12:32 +09001604 slave_mode = devtype_data->has_slavemode &&
1605 of_property_read_bool(np, "spi-slave");
1606 if (slave_mode)
1607 master = spi_alloc_slave(&pdev->dev,
1608 sizeof(struct spi_imx_data));
1609 else
1610 master = spi_alloc_master(&pdev->dev,
1611 sizeof(struct spi_imx_data));
Fabio Estevam2c147772017-06-20 13:50:55 -03001612 if (!master)
1613 return -ENOMEM;
1614
Leif Middelschultef72efa72017-04-23 21:19:58 +02001615 ret = of_property_read_u32(np, "fsl,spi-rdy-drctl", &spi_drctl);
1616 if ((ret < 0) || (spi_drctl >= 0x3)) {
1617 /* '11' is reserved */
1618 spi_drctl = 0;
1619 }
1620
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001621 platform_set_drvdata(pdev, master);
1622
Stephen Warren24778be2013-05-21 20:36:35 -06001623 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001624 master->bus_num = np ? -1 : pdev->id;
Linus Walleij8cdcd8a2020-06-25 22:02:52 +02001625 master->use_gpio_descriptors = true;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001626
1627 spi_imx = spi_master_get_devdata(master);
Axel Lin94c69f72013-09-10 15:43:41 +08001628 spi_imx->bitbang.master = master;
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001629 spi_imx->dev = &pdev->dev;
jiada wang71abd292017-09-05 14:12:32 +09001630 spi_imx->slave_mode = slave_mode;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001631
jiada wang71abd292017-09-05 14:12:32 +09001632 spi_imx->devtype_data = devtype_data;
Anton Bondarenko4686d1c2015-12-08 07:43:44 +01001633
Linus Walleij8cdcd8a2020-06-25 22:02:52 +02001634 /*
1635 * Get number of chip selects from device properties. This can be
1636 * coming from device tree or boardfiles, if it is not defined,
1637 * a default value of 3 chip selects will be used, as all the legacy
1638 * board files have <= 3 chip selects.
1639 */
1640 if (!device_property_read_u32(&pdev->dev, "num-cs", &val))
1641 master->num_chipselect = val;
1642 else
1643 master->num_chipselect = 3;
Fabio Estevam4cc122a2011-09-15 17:21:15 -03001644
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001645 spi_imx->bitbang.setup_transfer = spi_imx_setupxfer;
1646 spi_imx->bitbang.txrx_bufs = spi_imx_transfer;
1647 spi_imx->bitbang.master->setup = spi_imx_setup;
1648 spi_imx->bitbang.master->cleanup = spi_imx_cleanup;
Huang Shijie9e556dc2013-10-23 16:31:50 +08001649 spi_imx->bitbang.master->prepare_message = spi_imx_prepare_message;
1650 spi_imx->bitbang.master->unprepare_message = spi_imx_unprepare_message;
jiada wang71abd292017-09-05 14:12:32 +09001651 spi_imx->bitbang.master->slave_abort = spi_imx_slave_abort;
Oleksij Rempelab2f3572017-07-25 09:57:09 +02001652 spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
1653 | SPI_NO_CS;
jiada wang26e4bb82017-06-08 14:16:01 +09001654 if (is_imx35_cspi(spi_imx) || is_imx51_ecspi(spi_imx) ||
1655 is_imx53_ecspi(spi_imx))
Leif Middelschultef72efa72017-04-23 21:19:58 +02001656 spi_imx->bitbang.master->mode_bits |= SPI_LOOP | SPI_READY;
1657
Uwe Kleine-König6e95b232021-07-27 14:42:26 +02001658 if (is_imx51_ecspi(spi_imx) &&
1659 device_property_read_u32(&pdev->dev, "cs-gpios", NULL))
1660 /*
1661 * When using HW-CS implementing SPI_CS_WORD can be done by just
1662 * setting the burst length to the word size. This is
1663 * considerably faster than manually controlling the CS.
1664 */
1665 spi_imx->bitbang.master->mode_bits |= SPI_CS_WORD;
1666
Leif Middelschultef72efa72017-04-23 21:19:58 +02001667 spi_imx->spi_drctl = spi_drctl;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001668
1669 init_completion(&spi_imx->xfer_done);
1670
1671 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001672 spi_imx->base = devm_ioremap_resource(&pdev->dev, res);
1673 if (IS_ERR(spi_imx->base)) {
1674 ret = PTR_ERR(spi_imx->base);
1675 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001676 }
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001677 spi_imx->base_phys = res->start;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001678
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001679 irq = platform_get_irq(pdev, 0);
1680 if (irq < 0) {
1681 ret = irq;
Fabio Estevam130b82c2013-07-11 01:26:48 -03001682 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001683 }
1684
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001685 ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0,
Alexander Shiyan8fc39b52014-02-22 17:23:46 +04001686 dev_name(&pdev->dev), spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001687 if (ret) {
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001688 dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001689 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001690 }
1691
Sascha Haueraa29d8402012-03-07 09:30:22 +01001692 spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1693 if (IS_ERR(spi_imx->clk_ipg)) {
1694 ret = PTR_ERR(spi_imx->clk_ipg);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001695 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001696 }
1697
Sascha Haueraa29d8402012-03-07 09:30:22 +01001698 spi_imx->clk_per = devm_clk_get(&pdev->dev, "per");
1699 if (IS_ERR(spi_imx->clk_per)) {
1700 ret = PTR_ERR(spi_imx->clk_per);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001701 goto out_master_put;
Sascha Haueraa29d8402012-03-07 09:30:22 +01001702 }
1703
Sascha Hauer43b6bf42020-10-21 12:45:13 +02001704 ret = clk_prepare_enable(spi_imx->clk_per);
1705 if (ret)
1706 goto out_master_put;
1707
1708 ret = clk_prepare_enable(spi_imx->clk_ipg);
1709 if (ret)
1710 goto out_put_per;
1711
Clark Wang525c9e52020-07-27 14:33:54 +08001712 pm_runtime_set_autosuspend_delay(spi_imx->dev, MXC_RPM_TIMEOUT);
1713 pm_runtime_use_autosuspend(spi_imx->dev);
Clark Wang7cd71202020-11-24 16:52:47 +08001714 pm_runtime_get_noresume(spi_imx->dev);
Sascha Hauer43b6bf42020-10-21 12:45:13 +02001715 pm_runtime_set_active(spi_imx->dev);
1716 pm_runtime_enable(spi_imx->dev);
Sascha Haueraa29d8402012-03-07 09:30:22 +01001717
1718 spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
Robin Gongf62cacc2014-09-11 09:18:44 +08001719 /*
Martin Kaiser2dd33f92016-10-20 00:42:25 +02001720 * Only validated on i.mx35 and i.mx6 now, can remove the constraint
1721 * if validated on other chips.
Robin Gongf62cacc2014-09-11 09:18:44 +08001722 */
jiada wangfd8d4e22017-06-08 14:16:00 +09001723 if (spi_imx->devtype_data->has_dmamode) {
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001724 ret = spi_imx_sdma_init(&pdev->dev, spi_imx, master);
Anton Bondarenkobf9af082015-12-08 07:43:46 +01001725 if (ret == -EPROBE_DEFER)
Clark Wang525c9e52020-07-27 14:33:54 +08001726 goto out_runtime_pm_put;
Anton Bondarenkobf9af082015-12-08 07:43:46 +01001727
Anton Bondarenko37600472015-12-08 07:43:45 +01001728 if (ret < 0)
Fabio Estevam0ec0da72020-08-18 19:35:18 -03001729 dev_dbg(&pdev->dev, "dma setup error %d, use pio\n",
Anton Bondarenko37600472015-12-08 07:43:45 +01001730 ret);
1731 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001732
Shawn Guoedd501bb2011-07-10 01:16:35 +08001733 spi_imx->devtype_data->reset(spi_imx);
Daniel Mackce1807b2009-11-19 19:01:42 +00001734
Shawn Guoedd501bb2011-07-10 01:16:35 +08001735 spi_imx->devtype_data->intctrl(spi_imx, 0);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001736
Shawn Guo22a85e42011-07-10 01:16:41 +08001737 master->dev.of_node = pdev->dev.of_node;
Trent Piepho8197f482017-11-06 10:38:23 -08001738 ret = spi_bitbang_start(&spi_imx->bitbang);
1739 if (ret) {
Guido Günther83466332021-01-18 17:31:10 +01001740 dev_err_probe(&pdev->dev, ret, "bitbang start failed\n");
Marek Vasut45f0bbd2020-10-05 15:22:29 +02001741 goto out_bitbang_start;
Trent Piepho8197f482017-11-06 10:38:23 -08001742 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001743
Clark Wang525c9e52020-07-27 14:33:54 +08001744 pm_runtime_mark_last_busy(spi_imx->dev);
1745 pm_runtime_put_autosuspend(spi_imx->dev);
1746
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001747 return ret;
1748
Marek Vasut45f0bbd2020-10-05 15:22:29 +02001749out_bitbang_start:
1750 if (spi_imx->devtype_data->has_dmamode)
1751 spi_imx_sdma_exit(spi_imx);
Clark Wang525c9e52020-07-27 14:33:54 +08001752out_runtime_pm_put:
1753 pm_runtime_dont_use_autosuspend(spi_imx->dev);
Sascha Hauer43b6bf42020-10-21 12:45:13 +02001754 pm_runtime_set_suspended(&pdev->dev);
Clark Wang525c9e52020-07-27 14:33:54 +08001755 pm_runtime_disable(spi_imx->dev);
Sascha Hauer43b6bf42020-10-21 12:45:13 +02001756
1757 clk_disable_unprepare(spi_imx->clk_ipg);
1758out_put_per:
1759 clk_disable_unprepare(spi_imx->clk_per);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001760out_master_put:
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001761 spi_master_put(master);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001762
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001763 return ret;
1764}
1765
Grant Likelyfd4a3192012-12-07 16:57:14 +00001766static int spi_imx_remove(struct platform_device *pdev)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001767{
1768 struct spi_master *master = platform_get_drvdata(pdev);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001769 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
Stefan Agnerd5935742018-01-07 15:05:49 +01001770 int ret;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001771
1772 spi_bitbang_stop(&spi_imx->bitbang);
1773
Clark Wang525c9e52020-07-27 14:33:54 +08001774 ret = pm_runtime_get_sync(spi_imx->dev);
1775 if (ret < 0) {
Zhang Qilong1dcbdd92020-11-02 22:58:35 +08001776 pm_runtime_put_noidle(spi_imx->dev);
Clark Wang525c9e52020-07-27 14:33:54 +08001777 dev_err(spi_imx->dev, "failed to enable clock\n");
Stefan Agnerd5935742018-01-07 15:05:49 +01001778 return ret;
1779 }
1780
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001781 writel(0, spi_imx->base + MXC_CSPICTRL);
Clark Wang525c9e52020-07-27 14:33:54 +08001782
1783 pm_runtime_dont_use_autosuspend(spi_imx->dev);
1784 pm_runtime_put_sync(spi_imx->dev);
1785 pm_runtime_disable(spi_imx->dev);
1786
Robin Gongf62cacc2014-09-11 09:18:44 +08001787 spi_imx_sdma_exit(spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001788 spi_master_put(master);
1789
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001790 return 0;
1791}
1792
Clark Wang525c9e52020-07-27 14:33:54 +08001793static int __maybe_unused spi_imx_runtime_resume(struct device *dev)
1794{
1795 struct spi_master *master = dev_get_drvdata(dev);
1796 struct spi_imx_data *spi_imx;
1797 int ret;
1798
1799 spi_imx = spi_master_get_devdata(master);
1800
1801 ret = clk_prepare_enable(spi_imx->clk_per);
1802 if (ret)
1803 return ret;
1804
1805 ret = clk_prepare_enable(spi_imx->clk_ipg);
1806 if (ret) {
1807 clk_disable_unprepare(spi_imx->clk_per);
1808 return ret;
1809 }
1810
1811 return 0;
1812}
1813
1814static int __maybe_unused spi_imx_runtime_suspend(struct device *dev)
1815{
1816 struct spi_master *master = dev_get_drvdata(dev);
1817 struct spi_imx_data *spi_imx;
1818
1819 spi_imx = spi_master_get_devdata(master);
1820
1821 clk_disable_unprepare(spi_imx->clk_per);
1822 clk_disable_unprepare(spi_imx->clk_ipg);
1823
1824 return 0;
1825}
1826
1827static int __maybe_unused spi_imx_suspend(struct device *dev)
1828{
1829 pinctrl_pm_select_sleep_state(dev);
1830 return 0;
1831}
1832
1833static int __maybe_unused spi_imx_resume(struct device *dev)
1834{
1835 pinctrl_pm_select_default_state(dev);
1836 return 0;
1837}
1838
1839static const struct dev_pm_ops imx_spi_pm = {
1840 SET_RUNTIME_PM_OPS(spi_imx_runtime_suspend,
1841 spi_imx_runtime_resume, NULL)
1842 SET_SYSTEM_SLEEP_PM_OPS(spi_imx_suspend, spi_imx_resume)
1843};
1844
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001845static struct platform_driver spi_imx_driver = {
1846 .driver = {
1847 .name = DRIVER_NAME,
Shawn Guo22a85e42011-07-10 01:16:41 +08001848 .of_match_table = spi_imx_dt_ids,
Clark Wang525c9e52020-07-27 14:33:54 +08001849 .pm = &imx_spi_pm,
1850 },
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001851 .probe = spi_imx_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001852 .remove = spi_imx_remove,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001853};
Grant Likely940ab882011-10-05 11:29:49 -06001854module_platform_driver(spi_imx_driver);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001855
Fabio Estevam92bad4a42021-03-16 15:09:22 -03001856MODULE_DESCRIPTION("i.MX SPI Controller driver");
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001857MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1858MODULE_LICENSE("GPL");
Fabio Estevam3133fba32013-01-07 20:42:55 -02001859MODULE_ALIAS("platform:" DRIVER_NAME);