blob: 340fd1245bc26e5642080ebd28045a4dad05e770 [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;
Shawn Guo04ee5852011-07-10 01:16:39 +080080 enum spi_imx_devtype devtype;
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +020081};
82
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070083struct spi_imx_data {
84 struct spi_bitbang bitbang;
Sascha Hauer6aa800c2016-02-17 14:28:48 +010085 struct device *dev;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070086
87 struct completion xfer_done;
Uwe Kleine-Königcc4d22a2012-03-29 21:54:18 +020088 void __iomem *base;
Anton Bondarenkof12ae172016-02-24 09:20:29 +010089 unsigned long base_phys;
90
Sascha Haueraa29d8402012-03-07 09:30:22 +010091 struct clk *clk_per;
92 struct clk *clk_ipg;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070093 unsigned long spi_clk;
Anton Bondarenko4bfe9272016-02-19 08:43:03 +010094 unsigned int spi_bus_clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -070095
Sascha Hauerd52345b2017-06-02 07:38:01 +020096 unsigned int bits_per_word;
Leif Middelschultef72efa72017-04-23 21:19:58 +020097 unsigned int spi_drctl;
Anton Bondarenkof12ae172016-02-24 09:20:29 +010098
jiada wang1673c812017-08-10 13:50:08 +090099 unsigned int count, remainder;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700100 void (*tx)(struct spi_imx_data *);
101 void (*rx)(struct spi_imx_data *);
102 void *rx_buf;
103 const void *tx_buf;
104 unsigned int txfifo; /* number of words pushed in tx FIFO */
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200105 unsigned int dynamic_burst;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700106
jiada wang71abd292017-09-05 14:12:32 +0900107 /* Slave mode */
108 bool slave_mode;
109 bool slave_aborted;
110 unsigned int slave_burst;
111
Robin Gongf62cacc2014-09-11 09:18:44 +0800112 /* DMA */
Robin Gongf62cacc2014-09-11 09:18:44 +0800113 bool usedma;
Anton Bondarenko0dfbaa82015-12-05 17:57:01 +0100114 u32 wml;
Robin Gongf62cacc2014-09-11 09:18:44 +0800115 struct completion dma_rx_completion;
116 struct completion dma_tx_completion;
117
Uwe Kleine-König80023cb2012-05-21 21:49:35 +0200118 const struct spi_imx_devtype_data *devtype_data;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700119};
120
Shawn Guo04ee5852011-07-10 01:16:39 +0800121static inline int is_imx27_cspi(struct spi_imx_data *d)
122{
123 return d->devtype_data->devtype == IMX27_CSPI;
124}
125
126static inline int is_imx35_cspi(struct spi_imx_data *d)
127{
128 return d->devtype_data->devtype == IMX35_CSPI;
129}
130
Anton Bondarenkof8a876172015-12-05 17:57:02 +0100131static inline int is_imx51_ecspi(struct spi_imx_data *d)
132{
133 return d->devtype_data->devtype == IMX51_ECSPI;
134}
135
jiada wang26e4bb82017-06-08 14:16:01 +0900136static inline int is_imx53_ecspi(struct spi_imx_data *d)
137{
138 return d->devtype_data->devtype == IMX53_ECSPI;
139}
140
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700141#define MXC_SPI_BUF_RX(type) \
142static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx) \
143{ \
144 unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA); \
145 \
146 if (spi_imx->rx_buf) { \
147 *(type *)spi_imx->rx_buf = val; \
148 spi_imx->rx_buf += sizeof(type); \
149 } \
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200150 \
151 spi_imx->remainder -= sizeof(type); \
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700152}
153
154#define MXC_SPI_BUF_TX(type) \
155static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx) \
156{ \
157 type val = 0; \
158 \
159 if (spi_imx->tx_buf) { \
160 val = *(type *)spi_imx->tx_buf; \
161 spi_imx->tx_buf += sizeof(type); \
162 } \
163 \
164 spi_imx->count -= sizeof(type); \
165 \
166 writel(val, spi_imx->base + MXC_CSPITXDATA); \
167}
168
169MXC_SPI_BUF_RX(u8)
170MXC_SPI_BUF_TX(u8)
171MXC_SPI_BUF_RX(u16)
172MXC_SPI_BUF_TX(u16)
173MXC_SPI_BUF_RX(u32)
174MXC_SPI_BUF_TX(u32)
175
176/* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
177 * (which is currently not the case in this driver)
178 */
179static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
180 256, 384, 512, 768, 1024};
181
182/* MX21, MX27 */
183static unsigned int spi_imx_clkdiv_1(unsigned int fin,
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100184 unsigned int fspi, unsigned int max, unsigned int *fres)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700185{
Shawn Guo04ee5852011-07-10 01:16:39 +0800186 int i;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700187
188 for (i = 2; i < max; i++)
189 if (fspi * mxc_clkdivs[i] >= fin)
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100190 break;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700191
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100192 *fres = fin / mxc_clkdivs[i];
193 return i;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700194}
195
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200196/* MX1, MX31, MX35, MX51 CSPI */
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700197static unsigned int spi_imx_clkdiv_2(unsigned int fin,
Martin Kaiser2636ba82016-09-01 22:38:40 +0200198 unsigned int fspi, unsigned int *fres)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700199{
200 int i, div = 4;
201
202 for (i = 0; i < 7; i++) {
203 if (fspi * div >= fin)
Martin Kaiser2636ba82016-09-01 22:38:40 +0200204 goto out;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700205 div <<= 1;
206 }
207
Martin Kaiser2636ba82016-09-01 22:38:40 +0200208out:
209 *fres = fin / div;
210 return i;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700211}
212
Sascha Hauer2e312f62017-06-02 07:38:04 +0200213static int spi_imx_bytes_per_word(const int bits_per_word)
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100214{
Maxime Chevallierafb272082018-07-17 16:31:52 +0200215 if (bits_per_word <= 8)
216 return 1;
217 else if (bits_per_word <= 16)
218 return 2;
219 else
220 return 4;
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100221}
222
Robin Gongf62cacc2014-09-11 09:18:44 +0800223static bool spi_imx_can_dma(struct spi_master *master, struct spi_device *spi,
224 struct spi_transfer *transfer)
225{
226 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
227
Robin Gong7a908832020-06-17 06:42:09 +0800228 if (!use_dma || master->fallback)
Trent Piepho0a9c8992019-03-04 23:02:36 +0000229 return false;
230
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100231 if (!master->dma_rx)
232 return false;
233
jiada wang71abd292017-09-05 14:12:32 +0900234 if (spi_imx->slave_mode)
235 return false;
236
Robin Gong133eb8e2018-10-10 10:32:48 +0000237 if (transfer->len < spi_imx->devtype_data->fifo_size)
238 return false;
239
jiada wang1673c812017-08-10 13:50:08 +0900240 spi_imx->dynamic_burst = 0;
Anton Bondarenkof12ae172016-02-24 09:20:29 +0100241
242 return true;
Robin Gongf62cacc2014-09-11 09:18:44 +0800243}
244
Shawn Guo66de7572011-07-10 01:16:37 +0800245#define MX51_ECSPI_CTRL 0x08
246#define MX51_ECSPI_CTRL_ENABLE (1 << 0)
247#define MX51_ECSPI_CTRL_XCH (1 << 2)
Robin Gongf62cacc2014-09-11 09:18:44 +0800248#define MX51_ECSPI_CTRL_SMC (1 << 3)
Shawn Guo66de7572011-07-10 01:16:37 +0800249#define MX51_ECSPI_CTRL_MODE_MASK (0xf << 4)
Leif Middelschultef72efa72017-04-23 21:19:58 +0200250#define MX51_ECSPI_CTRL_DRCTL(drctl) ((drctl) << 16)
Shawn Guo66de7572011-07-10 01:16:37 +0800251#define MX51_ECSPI_CTRL_POSTDIV_OFFSET 8
252#define MX51_ECSPI_CTRL_PREDIV_OFFSET 12
253#define MX51_ECSPI_CTRL_CS(cs) ((cs) << 18)
254#define MX51_ECSPI_CTRL_BL_OFFSET 20
jiada wang1673c812017-08-10 13:50:08 +0900255#define MX51_ECSPI_CTRL_BL_MASK (0xfff << 20)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200256
Shawn Guo66de7572011-07-10 01:16:37 +0800257#define MX51_ECSPI_CONFIG 0x0c
258#define MX51_ECSPI_CONFIG_SCLKPHA(cs) (1 << ((cs) + 0))
259#define MX51_ECSPI_CONFIG_SCLKPOL(cs) (1 << ((cs) + 4))
260#define MX51_ECSPI_CONFIG_SBBCTRL(cs) (1 << ((cs) + 8))
261#define MX51_ECSPI_CONFIG_SSBPOL(cs) (1 << ((cs) + 12))
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200262#define MX51_ECSPI_CONFIG_SCLKCTL(cs) (1 << ((cs) + 20))
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200263
Shawn Guo66de7572011-07-10 01:16:37 +0800264#define MX51_ECSPI_INT 0x10
265#define MX51_ECSPI_INT_TEEN (1 << 0)
266#define MX51_ECSPI_INT_RREN (1 << 3)
jiada wang71abd292017-09-05 14:12:32 +0900267#define MX51_ECSPI_INT_RDREN (1 << 4)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200268
Uwe Kleine-König30d67142018-11-30 07:47:07 +0100269#define MX51_ECSPI_DMA 0x14
Sascha Hauerd629c2a2016-02-24 09:20:31 +0100270#define MX51_ECSPI_DMA_TX_WML(wml) ((wml) & 0x3f)
271#define MX51_ECSPI_DMA_RX_WML(wml) (((wml) & 0x3f) << 16)
272#define MX51_ECSPI_DMA_RXT_WML(wml) (((wml) & 0x3f) << 24)
Robin Gongf62cacc2014-09-11 09:18:44 +0800273
Sascha Hauer2b0fd062016-02-24 09:20:27 +0100274#define MX51_ECSPI_DMA_TEDEN (1 << 7)
275#define MX51_ECSPI_DMA_RXDEN (1 << 23)
276#define MX51_ECSPI_DMA_RXTDEN (1 << 31)
Robin Gongf62cacc2014-09-11 09:18:44 +0800277
Shawn Guo66de7572011-07-10 01:16:37 +0800278#define MX51_ECSPI_STAT 0x18
279#define MX51_ECSPI_STAT_RR (1 << 3)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200280
Fabio Estevam9f6aa422015-12-03 23:23:24 -0200281#define MX51_ECSPI_TESTREG 0x20
282#define MX51_ECSPI_TESTREG_LBC BIT(31)
283
jiada wang1673c812017-08-10 13:50:08 +0900284static void spi_imx_buf_rx_swap_u32(struct spi_imx_data *spi_imx)
285{
286 unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA);
Arnd Bergmann5904c9d2017-08-23 15:34:43 +0200287#ifdef __LITTLE_ENDIAN
jiada wang1673c812017-08-10 13:50:08 +0900288 unsigned int bytes_per_word;
Arnd Bergmann5904c9d2017-08-23 15:34:43 +0200289#endif
jiada wang1673c812017-08-10 13:50:08 +0900290
291 if (spi_imx->rx_buf) {
292#ifdef __LITTLE_ENDIAN
293 bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
294 if (bytes_per_word == 1)
295 val = cpu_to_be32(val);
296 else if (bytes_per_word == 2)
297 val = (val << 16) | (val >> 16);
298#endif
jiada wang1673c812017-08-10 13:50:08 +0900299 *(u32 *)spi_imx->rx_buf = val;
300 spi_imx->rx_buf += sizeof(u32);
301 }
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200302
303 spi_imx->remainder -= sizeof(u32);
jiada wang1673c812017-08-10 13:50:08 +0900304}
305
306static void spi_imx_buf_rx_swap(struct spi_imx_data *spi_imx)
307{
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200308 int unaligned;
309 u32 val;
jiada wang1673c812017-08-10 13:50:08 +0900310
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200311 unaligned = spi_imx->remainder % 4;
312
313 if (!unaligned) {
jiada wang1673c812017-08-10 13:50:08 +0900314 spi_imx_buf_rx_swap_u32(spi_imx);
315 return;
316 }
317
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200318 if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) {
jiada wang1673c812017-08-10 13:50:08 +0900319 spi_imx_buf_rx_u16(spi_imx);
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200320 return;
321 }
322
323 val = readl(spi_imx->base + MXC_CSPIRXDATA);
324
325 while (unaligned--) {
326 if (spi_imx->rx_buf) {
327 *(u8 *)spi_imx->rx_buf = (val >> (8 * unaligned)) & 0xff;
328 spi_imx->rx_buf++;
329 }
330 spi_imx->remainder--;
331 }
jiada wang1673c812017-08-10 13:50:08 +0900332}
333
334static void spi_imx_buf_tx_swap_u32(struct spi_imx_data *spi_imx)
335{
336 u32 val = 0;
Arnd Bergmann5904c9d2017-08-23 15:34:43 +0200337#ifdef __LITTLE_ENDIAN
jiada wang1673c812017-08-10 13:50:08 +0900338 unsigned int bytes_per_word;
Arnd Bergmann5904c9d2017-08-23 15:34:43 +0200339#endif
jiada wang1673c812017-08-10 13:50:08 +0900340
341 if (spi_imx->tx_buf) {
342 val = *(u32 *)spi_imx->tx_buf;
jiada wang1673c812017-08-10 13:50:08 +0900343 spi_imx->tx_buf += sizeof(u32);
344 }
345
346 spi_imx->count -= sizeof(u32);
347#ifdef __LITTLE_ENDIAN
348 bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
349
350 if (bytes_per_word == 1)
351 val = cpu_to_be32(val);
352 else if (bytes_per_word == 2)
353 val = (val << 16) | (val >> 16);
354#endif
355 writel(val, spi_imx->base + MXC_CSPITXDATA);
356}
357
358static void spi_imx_buf_tx_swap(struct spi_imx_data *spi_imx)
359{
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200360 int unaligned;
361 u32 val = 0;
jiada wang1673c812017-08-10 13:50:08 +0900362
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200363 unaligned = spi_imx->count % 4;
jiada wang1673c812017-08-10 13:50:08 +0900364
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200365 if (!unaligned) {
jiada wang1673c812017-08-10 13:50:08 +0900366 spi_imx_buf_tx_swap_u32(spi_imx);
367 return;
368 }
369
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200370 if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) {
jiada wang1673c812017-08-10 13:50:08 +0900371 spi_imx_buf_tx_u16(spi_imx);
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200372 return;
373 }
374
375 while (unaligned--) {
376 if (spi_imx->tx_buf) {
377 val |= *(u8 *)spi_imx->tx_buf << (8 * unaligned);
378 spi_imx->tx_buf++;
379 }
380 spi_imx->count--;
381 }
382
383 writel(val, spi_imx->base + MXC_CSPITXDATA);
jiada wang1673c812017-08-10 13:50:08 +0900384}
385
jiada wang71abd292017-09-05 14:12:32 +0900386static void mx53_ecspi_rx_slave(struct spi_imx_data *spi_imx)
387{
388 u32 val = be32_to_cpu(readl(spi_imx->base + MXC_CSPIRXDATA));
389
390 if (spi_imx->rx_buf) {
391 int n_bytes = spi_imx->slave_burst % sizeof(val);
392
393 if (!n_bytes)
394 n_bytes = sizeof(val);
395
396 memcpy(spi_imx->rx_buf,
397 ((u8 *)&val) + sizeof(val) - n_bytes, n_bytes);
398
399 spi_imx->rx_buf += n_bytes;
400 spi_imx->slave_burst -= n_bytes;
401 }
Maxime Chevallier2ca300a2018-07-17 16:31:54 +0200402
403 spi_imx->remainder -= sizeof(u32);
jiada wang71abd292017-09-05 14:12:32 +0900404}
405
406static void mx53_ecspi_tx_slave(struct spi_imx_data *spi_imx)
407{
408 u32 val = 0;
409 int n_bytes = spi_imx->count % sizeof(val);
410
411 if (!n_bytes)
412 n_bytes = sizeof(val);
413
414 if (spi_imx->tx_buf) {
415 memcpy(((u8 *)&val) + sizeof(val) - n_bytes,
416 spi_imx->tx_buf, n_bytes);
417 val = cpu_to_be32(val);
418 spi_imx->tx_buf += n_bytes;
419 }
420
421 spi_imx->count -= n_bytes;
422
423 writel(val, spi_imx->base + MXC_CSPITXDATA);
424}
425
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200426/* MX51 eCSPI */
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100427static unsigned int mx51_ecspi_clkdiv(struct spi_imx_data *spi_imx,
428 unsigned int fspi, unsigned int *fres)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200429{
430 /*
431 * there are two 4-bit dividers, the pre-divider divides by
432 * $pre, the post-divider by 2^$post
433 */
434 unsigned int pre, post;
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100435 unsigned int fin = spi_imx->spi_clk;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200436
437 if (unlikely(fspi > fin))
438 return 0;
439
440 post = fls(fin) - fls(fspi);
441 if (fin > fspi << post)
442 post++;
443
444 /* now we have: (fin <= fspi << post) with post being minimal */
445
446 post = max(4U, post) - 4;
447 if (unlikely(post > 0xf)) {
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100448 dev_err(spi_imx->dev, "cannot set clock freq: %u (base freq: %u)\n",
449 fspi, fin);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200450 return 0xff;
451 }
452
453 pre = DIV_ROUND_UP(fin, fspi << post) - 1;
454
Sascha Hauer6aa800c2016-02-17 14:28:48 +0100455 dev_dbg(spi_imx->dev, "%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200456 __func__, fin, fspi, post, pre);
Marek Vasut6fd8b852013-12-18 18:31:47 +0100457
458 /* Resulting frequency for the SCLK line. */
459 *fres = (fin / (pre + 1)) >> post;
460
Shawn Guo66de7572011-07-10 01:16:37 +0800461 return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) |
462 (post << MX51_ECSPI_CTRL_POSTDIV_OFFSET);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200463}
464
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300465static void mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200466{
467 unsigned val = 0;
468
469 if (enable & MXC_INT_TE)
Shawn Guo66de7572011-07-10 01:16:37 +0800470 val |= MX51_ECSPI_INT_TEEN;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200471
472 if (enable & MXC_INT_RR)
Shawn Guo66de7572011-07-10 01:16:37 +0800473 val |= MX51_ECSPI_INT_RREN;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200474
jiada wang71abd292017-09-05 14:12:32 +0900475 if (enable & MXC_INT_RDR)
476 val |= MX51_ECSPI_INT_RDREN;
477
Shawn Guo66de7572011-07-10 01:16:37 +0800478 writel(val, spi_imx->base + MX51_ECSPI_INT);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200479}
480
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300481static void mx51_ecspi_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200482{
Sascha Hauerb03c3882016-02-24 09:20:32 +0100483 u32 reg;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200484
Sascha Hauerb03c3882016-02-24 09:20:32 +0100485 reg = readl(spi_imx->base + MX51_ECSPI_CTRL);
486 reg |= MX51_ECSPI_CTRL_XCH;
Shawn Guo66de7572011-07-10 01:16:37 +0800487 writel(reg, spi_imx->base + MX51_ECSPI_CTRL);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200488}
489
Robin Gongbcd8e772020-05-21 04:34:17 +0800490static void mx51_disable_dma(struct spi_imx_data *spi_imx)
491{
492 writel(0, spi_imx->base + MX51_ECSPI_DMA);
493}
494
jiada wang71abd292017-09-05 14:12:32 +0900495static void mx51_ecspi_disable(struct spi_imx_data *spi_imx)
496{
497 u32 ctrl;
498
499 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
500 ctrl &= ~MX51_ECSPI_CTRL_ENABLE;
501 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
502}
503
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100504static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx,
505 struct spi_message *msg)
506{
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100507 struct spi_device *spi = msg->spi;
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100508 u32 ctrl = MX51_ECSPI_CTRL_ENABLE;
Marek Vasut135cbd32021-07-03 04:23:00 +0200509 u32 testreg, delay;
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100510 u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200511
jiada wang71abd292017-09-05 14:12:32 +0900512 /* set Master or Slave mode */
513 if (spi_imx->slave_mode)
514 ctrl &= ~MX51_ECSPI_CTRL_MODE_MASK;
515 else
516 ctrl |= MX51_ECSPI_CTRL_MODE_MASK;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200517
Leif Middelschultef72efa72017-04-23 21:19:58 +0200518 /*
519 * Enable SPI_RDY handling (falling edge/level triggered).
520 */
521 if (spi->mode & SPI_READY)
522 ctrl |= MX51_ECSPI_CTRL_DRCTL(spi_imx->spi_drctl);
523
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200524 /* set chip select to use */
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300525 ctrl |= MX51_ECSPI_CTRL_CS(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200526
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100527 /*
528 * The ctrl register must be written first, with the EN bit set other
529 * registers must not be written to.
530 */
531 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
532
533 testreg = readl(spi_imx->base + MX51_ECSPI_TESTREG);
534 if (spi->mode & SPI_LOOP)
535 testreg |= MX51_ECSPI_TESTREG_LBC;
jiada wang71abd292017-09-05 14:12:32 +0900536 else
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100537 testreg &= ~MX51_ECSPI_TESTREG_LBC;
538 writel(testreg, spi_imx->base + MX51_ECSPI_TESTREG);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200539
jiada wang71abd292017-09-05 14:12:32 +0900540 /*
541 * eCSPI burst completion by Chip Select signal in Slave mode
542 * is not functional for imx53 Soc, config SPI burst completed when
543 * BURST_LENGTH + 1 bits are received
544 */
545 if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx))
546 cfg &= ~MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
547 else
548 cfg |= MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200549
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300550 if (spi->mode & SPI_CPHA)
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300551 cfg |= MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100552 else
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300553 cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200554
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300555 if (spi->mode & SPI_CPOL) {
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300556 cfg |= MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
557 cfg |= MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100558 } else {
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300559 cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
560 cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
Knut Wohlrabc09b8902012-09-25 13:21:57 +0200561 }
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100562
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300563 if (spi->mode & SPI_CS_HIGH)
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300564 cfg |= MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
Knut Wohlrab793c7f92016-03-15 14:24:36 +0100565 else
Alexander Shiyanb36581d2016-06-08 20:02:06 +0300566 cfg &= ~MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200567
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100568 writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
569
Marek Vasut135cbd32021-07-03 04:23:00 +0200570 /*
571 * Wait until the changes in the configuration register CONFIGREG
572 * propagate into the hardware. It takes exactly one tick of the
573 * SCLK clock, but we will wait two SCLK clock just to be sure. The
574 * effect of the delay it takes for the hardware to apply changes
575 * is noticable if the SCLK clock run very slow. In such a case, if
576 * the polarity of SCLK should be inverted, the GPIO ChipSelect might
577 * be asserted before the SCLK polarity changes, which would disrupt
578 * the SPI communication as the device on the other end would consider
579 * the change of SCLK polarity as a clock tick already.
580 */
581 delay = (2 * 1000000) / spi_imx->spi_bus_clk;
582 if (likely(delay < 10)) /* SCLK is faster than 100 kHz */
583 udelay(delay);
584 else /* SCLK is _very_ slow */
585 usleep_range(delay, delay + 10);
586
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100587 return 0;
588}
589
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100590static int mx51_ecspi_prepare_transfer(struct spi_imx_data *spi_imx,
Clark Wang4df2f5e2021-04-08 18:33:47 +0800591 struct spi_device *spi)
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100592{
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100593 u32 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
Marek Vasut135cbd32021-07-03 04:23:00 +0200594 u32 clk;
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100595
596 /* Clear BL field and set the right value */
597 ctrl &= ~MX51_ECSPI_CTRL_BL_MASK;
598 if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx))
599 ctrl |= (spi_imx->slave_burst * 8 - 1)
600 << MX51_ECSPI_CTRL_BL_OFFSET;
601 else
602 ctrl |= (spi_imx->bits_per_word - 1)
603 << MX51_ECSPI_CTRL_BL_OFFSET;
604
605 /* set clock speed */
606 ctrl &= ~(0xf << MX51_ECSPI_CTRL_POSTDIV_OFFSET |
607 0xf << MX51_ECSPI_CTRL_PREDIV_OFFSET);
Clark Wang4df2f5e2021-04-08 18:33:47 +0800608 ctrl |= mx51_ecspi_clkdiv(spi_imx, spi_imx->spi_bus_clk, &clk);
Uwe Kleine-König00b80ac2018-11-30 07:47:06 +0100609 spi_imx->spi_bus_clk = clk;
610
Sascha Hauerb03c3882016-02-24 09:20:32 +0100611 if (spi_imx->usedma)
612 ctrl |= MX51_ECSPI_CTRL_SMC;
613
Anton Bondarenkof677f172015-12-08 07:43:43 +0100614 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
615
Robin Gong987a2df2018-10-10 10:32:42 +0000616 return 0;
617}
618
619static void mx51_setup_wml(struct spi_imx_data *spi_imx)
620{
Robin Gongf62cacc2014-09-11 09:18:44 +0800621 /*
622 * Configure the DMA register: setup the watermark
623 * and enable DMA request.
624 */
Robin Gong5ba5a372018-10-10 10:32:45 +0000625 writel(MX51_ECSPI_DMA_RX_WML(spi_imx->wml - 1) |
Sascha Hauerd629c2a2016-02-24 09:20:31 +0100626 MX51_ECSPI_DMA_TX_WML(spi_imx->wml) |
627 MX51_ECSPI_DMA_RXT_WML(spi_imx->wml) |
Sascha Hauer2b0fd062016-02-24 09:20:27 +0100628 MX51_ECSPI_DMA_TEDEN | MX51_ECSPI_DMA_RXDEN |
629 MX51_ECSPI_DMA_RXTDEN, spi_imx->base + MX51_ECSPI_DMA);
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200630}
631
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300632static int mx51_ecspi_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200633{
Shawn Guo66de7572011-07-10 01:16:37 +0800634 return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR;
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200635}
636
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300637static void mx51_ecspi_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200638{
639 /* drain receive buffer */
Shawn Guo66de7572011-07-10 01:16:37 +0800640 while (mx51_ecspi_rx_available(spi_imx))
Uwe Kleine-König0b599602010-09-09 21:02:48 +0200641 readl(spi_imx->base + MXC_CSPIRXDATA);
642}
643
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700644#define MX31_INTREG_TEEN (1 << 0)
645#define MX31_INTREG_RREN (1 << 3)
646
647#define MX31_CSPICTRL_ENABLE (1 << 0)
648#define MX31_CSPICTRL_MASTER (1 << 1)
649#define MX31_CSPICTRL_XCH (1 << 2)
Martin Kaiser2dd33f92016-10-20 00:42:25 +0200650#define MX31_CSPICTRL_SMC (1 << 3)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700651#define MX31_CSPICTRL_POL (1 << 4)
652#define MX31_CSPICTRL_PHA (1 << 5)
653#define MX31_CSPICTRL_SSCTL (1 << 6)
654#define MX31_CSPICTRL_SSPOL (1 << 7)
655#define MX31_CSPICTRL_BC_SHIFT 8
656#define MX35_CSPICTRL_BL_SHIFT 20
657#define MX31_CSPICTRL_CS_SHIFT 24
658#define MX35_CSPICTRL_CS_SHIFT 12
659#define MX31_CSPICTRL_DR_SHIFT 16
660
Martin Kaiser2dd33f92016-10-20 00:42:25 +0200661#define MX31_CSPI_DMAREG 0x10
662#define MX31_DMAREG_RH_DEN (1<<4)
663#define MX31_DMAREG_TH_DEN (1<<1)
664
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700665#define MX31_CSPISTATUS 0x14
666#define MX31_STATUS_RR (1 << 3)
667
Martin Kaiser15ca9212016-09-01 22:39:58 +0200668#define MX31_CSPI_TESTREG 0x1C
669#define MX31_TEST_LBC (1 << 14)
670
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700671/* These functions also work for the i.MX35, but be aware that
672 * the i.MX35 has a slightly different register layout for bits
673 * we do not use here.
674 */
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300675static void mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700676{
677 unsigned int val = 0;
678
679 if (enable & MXC_INT_TE)
680 val |= MX31_INTREG_TEEN;
681 if (enable & MXC_INT_RR)
682 val |= MX31_INTREG_RREN;
683
684 writel(val, spi_imx->base + MXC_CSPIINT);
685}
686
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300687static void mx31_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700688{
689 unsigned int reg;
690
691 reg = readl(spi_imx->base + MXC_CSPICTRL);
692 reg |= MX31_CSPICTRL_XCH;
693 writel(reg, spi_imx->base + MXC_CSPICTRL);
694}
695
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100696static int mx31_prepare_message(struct spi_imx_data *spi_imx,
697 struct spi_message *msg)
698{
699 return 0;
700}
701
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100702static int mx31_prepare_transfer(struct spi_imx_data *spi_imx,
Clark Wang4df2f5e2021-04-08 18:33:47 +0800703 struct spi_device *spi)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700704{
705 unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200706 unsigned int clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700707
Clark Wang4df2f5e2021-04-08 18:33:47 +0800708 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->spi_bus_clk, &clk) <<
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700709 MX31_CSPICTRL_DR_SHIFT;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200710 spi_imx->spi_bus_clk = clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700711
Shawn Guo04ee5852011-07-10 01:16:39 +0800712 if (is_imx35_cspi(spi_imx)) {
Sascha Hauerd52345b2017-06-02 07:38:01 +0200713 reg |= (spi_imx->bits_per_word - 1) << MX35_CSPICTRL_BL_SHIFT;
Shawn Guo2a64a902011-07-10 01:16:38 +0800714 reg |= MX31_CSPICTRL_SSCTL;
715 } else {
Sascha Hauerd52345b2017-06-02 07:38:01 +0200716 reg |= (spi_imx->bits_per_word - 1) << MX31_CSPICTRL_BC_SHIFT;
Shawn Guo2a64a902011-07-10 01:16:38 +0800717 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700718
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300719 if (spi->mode & SPI_CPHA)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700720 reg |= MX31_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300721 if (spi->mode & SPI_CPOL)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700722 reg |= MX31_CSPICTRL_POL;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300723 if (spi->mode & SPI_CS_HIGH)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700724 reg |= MX31_CSPICTRL_SSPOL;
Linus Walleij8cdcd8a2020-06-25 22:02:52 +0200725 if (!spi->cs_gpiod)
Greg Ungerer602c8f42017-07-11 14:22:11 +1000726 reg |= (spi->chip_select) <<
Shawn Guo04ee5852011-07-10 01:16:39 +0800727 (is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT :
728 MX31_CSPICTRL_CS_SHIFT);
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200729
Martin Kaiser2dd33f92016-10-20 00:42:25 +0200730 if (spi_imx->usedma)
731 reg |= MX31_CSPICTRL_SMC;
732
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200733 writel(reg, spi_imx->base + MXC_CSPICTRL);
734
Martin Kaiser15ca9212016-09-01 22:39:58 +0200735 reg = readl(spi_imx->base + MX31_CSPI_TESTREG);
736 if (spi->mode & SPI_LOOP)
737 reg |= MX31_TEST_LBC;
738 else
739 reg &= ~MX31_TEST_LBC;
740 writel(reg, spi_imx->base + MX31_CSPI_TESTREG);
741
Martin Kaiser2dd33f92016-10-20 00:42:25 +0200742 if (spi_imx->usedma) {
Uwe Kleine-König30d67142018-11-30 07:47:07 +0100743 /*
744 * configure DMA requests when RXFIFO is half full and
745 * when TXFIFO is half empty
746 */
Martin Kaiser2dd33f92016-10-20 00:42:25 +0200747 writel(MX31_DMAREG_RH_DEN | MX31_DMAREG_TH_DEN,
748 spi_imx->base + MX31_CSPI_DMAREG);
749 }
750
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200751 return 0;
752}
753
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300754static int mx31_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700755{
756 return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
757}
758
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300759static void mx31_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200760{
761 /* drain receive buffer */
Shawn Guo2a64a902011-07-10 01:16:38 +0800762 while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200763 readl(spi_imx->base + MXC_CSPIRXDATA);
764}
765
Shawn Guo3451fb12011-07-10 01:16:36 +0800766#define MX21_INTREG_RR (1 << 4)
767#define MX21_INTREG_TEEN (1 << 9)
768#define MX21_INTREG_RREN (1 << 13)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700769
Shawn Guo3451fb12011-07-10 01:16:36 +0800770#define MX21_CSPICTRL_POL (1 << 5)
771#define MX21_CSPICTRL_PHA (1 << 6)
772#define MX21_CSPICTRL_SSPOL (1 << 8)
773#define MX21_CSPICTRL_XCH (1 << 9)
774#define MX21_CSPICTRL_ENABLE (1 << 10)
775#define MX21_CSPICTRL_MASTER (1 << 11)
776#define MX21_CSPICTRL_DR_SHIFT 14
777#define MX21_CSPICTRL_CS_SHIFT 19
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700778
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300779static void mx21_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700780{
781 unsigned int val = 0;
782
783 if (enable & MXC_INT_TE)
Shawn Guo3451fb12011-07-10 01:16:36 +0800784 val |= MX21_INTREG_TEEN;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700785 if (enable & MXC_INT_RR)
Shawn Guo3451fb12011-07-10 01:16:36 +0800786 val |= MX21_INTREG_RREN;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700787
788 writel(val, spi_imx->base + MXC_CSPIINT);
789}
790
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300791static void mx21_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700792{
793 unsigned int reg;
794
795 reg = readl(spi_imx->base + MXC_CSPICTRL);
Shawn Guo3451fb12011-07-10 01:16:36 +0800796 reg |= MX21_CSPICTRL_XCH;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700797 writel(reg, spi_imx->base + MXC_CSPICTRL);
798}
799
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100800static int mx21_prepare_message(struct spi_imx_data *spi_imx,
801 struct spi_message *msg)
802{
803 return 0;
804}
805
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100806static int mx21_prepare_transfer(struct spi_imx_data *spi_imx,
Clark Wang4df2f5e2021-04-08 18:33:47 +0800807 struct spi_device *spi)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700808{
Shawn Guo3451fb12011-07-10 01:16:36 +0800809 unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER;
Shawn Guo04ee5852011-07-10 01:16:39 +0800810 unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18;
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100811 unsigned int clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700812
Clark Wang4df2f5e2021-04-08 18:33:47 +0800813 reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, spi_imx->spi_bus_clk, max, &clk)
Robert Baldyga32df9ff2016-11-01 22:18:39 +0100814 << MX21_CSPICTRL_DR_SHIFT;
815 spi_imx->spi_bus_clk = clk;
816
Sascha Hauerd52345b2017-06-02 07:38:01 +0200817 reg |= spi_imx->bits_per_word - 1;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700818
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300819 if (spi->mode & SPI_CPHA)
Shawn Guo3451fb12011-07-10 01:16:36 +0800820 reg |= MX21_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300821 if (spi->mode & SPI_CPOL)
Shawn Guo3451fb12011-07-10 01:16:36 +0800822 reg |= MX21_CSPICTRL_POL;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300823 if (spi->mode & SPI_CS_HIGH)
Shawn Guo3451fb12011-07-10 01:16:36 +0800824 reg |= MX21_CSPICTRL_SSPOL;
Linus Walleij8cdcd8a2020-06-25 22:02:52 +0200825 if (!spi->cs_gpiod)
Greg Ungerer602c8f42017-07-11 14:22:11 +1000826 reg |= spi->chip_select << MX21_CSPICTRL_CS_SHIFT;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700827
828 writel(reg, spi_imx->base + MXC_CSPICTRL);
829
830 return 0;
831}
832
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300833static int mx21_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700834{
Shawn Guo3451fb12011-07-10 01:16:36 +0800835 return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700836}
837
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300838static void mx21_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200839{
840 writel(1, spi_imx->base + MXC_RESET);
841}
842
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700843#define MX1_INTREG_RR (1 << 3)
844#define MX1_INTREG_TEEN (1 << 8)
845#define MX1_INTREG_RREN (1 << 11)
846
847#define MX1_CSPICTRL_POL (1 << 4)
848#define MX1_CSPICTRL_PHA (1 << 5)
849#define MX1_CSPICTRL_XCH (1 << 8)
850#define MX1_CSPICTRL_ENABLE (1 << 9)
851#define MX1_CSPICTRL_MASTER (1 << 10)
852#define MX1_CSPICTRL_DR_SHIFT 13
853
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300854static void mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700855{
856 unsigned int val = 0;
857
858 if (enable & MXC_INT_TE)
859 val |= MX1_INTREG_TEEN;
860 if (enable & MXC_INT_RR)
861 val |= MX1_INTREG_RREN;
862
863 writel(val, spi_imx->base + MXC_CSPIINT);
864}
865
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300866static void mx1_trigger(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700867{
868 unsigned int reg;
869
870 reg = readl(spi_imx->base + MXC_CSPICTRL);
871 reg |= MX1_CSPICTRL_XCH;
872 writel(reg, spi_imx->base + MXC_CSPICTRL);
873}
874
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100875static int mx1_prepare_message(struct spi_imx_data *spi_imx,
876 struct spi_message *msg)
877{
878 return 0;
879}
880
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100881static int mx1_prepare_transfer(struct spi_imx_data *spi_imx,
Clark Wang4df2f5e2021-04-08 18:33:47 +0800882 struct spi_device *spi)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700883{
884 unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200885 unsigned int clk;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700886
Clark Wang4df2f5e2021-04-08 18:33:47 +0800887 reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->spi_bus_clk, &clk) <<
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700888 MX1_CSPICTRL_DR_SHIFT;
Martin Kaiser2636ba82016-09-01 22:38:40 +0200889 spi_imx->spi_bus_clk = clk;
890
Sascha Hauerd52345b2017-06-02 07:38:01 +0200891 reg |= spi_imx->bits_per_word - 1;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700892
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300893 if (spi->mode & SPI_CPHA)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700894 reg |= MX1_CSPICTRL_PHA;
Alexander Shiyanc0c7a5d2016-06-08 20:02:07 +0300895 if (spi->mode & SPI_CPOL)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700896 reg |= MX1_CSPICTRL_POL;
897
898 writel(reg, spi_imx->base + MXC_CSPICTRL);
899
900 return 0;
901}
902
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300903static int mx1_rx_available(struct spi_imx_data *spi_imx)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -0700904{
905 return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
906}
907
Alexander Shiyanf989bc62016-06-08 20:02:08 +0300908static void mx1_reset(struct spi_imx_data *spi_imx)
Uwe Kleine-König1723e662010-09-10 09:19:18 +0200909{
910 writel(1, spi_imx->base + MXC_RESET);
911}
912
Shawn Guo04ee5852011-07-10 01:16:39 +0800913static struct spi_imx_devtype_data imx1_cspi_devtype_data = {
914 .intctrl = mx1_intctrl,
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100915 .prepare_message = mx1_prepare_message,
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100916 .prepare_transfer = mx1_prepare_transfer,
Shawn Guo04ee5852011-07-10 01:16:39 +0800917 .trigger = mx1_trigger,
918 .rx_available = mx1_rx_available,
919 .reset = mx1_reset,
jiada wangfd8d4e22017-06-08 14:16:00 +0900920 .fifo_size = 8,
921 .has_dmamode = false,
jiada wang1673c812017-08-10 13:50:08 +0900922 .dynamic_burst = false,
jiada wang71abd292017-09-05 14:12:32 +0900923 .has_slavemode = false,
Shawn Guo04ee5852011-07-10 01:16:39 +0800924 .devtype = IMX1_CSPI,
925};
926
927static struct spi_imx_devtype_data imx21_cspi_devtype_data = {
928 .intctrl = mx21_intctrl,
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100929 .prepare_message = mx21_prepare_message,
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100930 .prepare_transfer = mx21_prepare_transfer,
Shawn Guo04ee5852011-07-10 01:16:39 +0800931 .trigger = mx21_trigger,
932 .rx_available = mx21_rx_available,
933 .reset = mx21_reset,
jiada wangfd8d4e22017-06-08 14:16:00 +0900934 .fifo_size = 8,
935 .has_dmamode = false,
jiada wang1673c812017-08-10 13:50:08 +0900936 .dynamic_burst = false,
jiada wang71abd292017-09-05 14:12:32 +0900937 .has_slavemode = false,
Shawn Guo04ee5852011-07-10 01:16:39 +0800938 .devtype = IMX21_CSPI,
939};
940
941static struct spi_imx_devtype_data imx27_cspi_devtype_data = {
942 /* i.mx27 cspi shares the functions with i.mx21 one */
943 .intctrl = mx21_intctrl,
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100944 .prepare_message = mx21_prepare_message,
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100945 .prepare_transfer = mx21_prepare_transfer,
Shawn Guo04ee5852011-07-10 01:16:39 +0800946 .trigger = mx21_trigger,
947 .rx_available = mx21_rx_available,
948 .reset = mx21_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 = IMX27_CSPI,
954};
955
956static struct spi_imx_devtype_data imx31_cspi_devtype_data = {
957 .intctrl = mx31_intctrl,
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100958 .prepare_message = mx31_prepare_message,
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100959 .prepare_transfer = mx31_prepare_transfer,
Shawn Guo04ee5852011-07-10 01:16:39 +0800960 .trigger = mx31_trigger,
961 .rx_available = mx31_rx_available,
962 .reset = mx31_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 = IMX31_CSPI,
968};
969
970static struct spi_imx_devtype_data imx35_cspi_devtype_data = {
971 /* i.mx35 and later cspi shares the functions with i.mx31 one */
972 .intctrl = mx31_intctrl,
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100973 .prepare_message = mx31_prepare_message,
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100974 .prepare_transfer = mx31_prepare_transfer,
Shawn Guo04ee5852011-07-10 01:16:39 +0800975 .trigger = mx31_trigger,
976 .rx_available = mx31_rx_available,
977 .reset = mx31_reset,
jiada wangfd8d4e22017-06-08 14:16:00 +0900978 .fifo_size = 8,
979 .has_dmamode = true,
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 = IMX35_CSPI,
983};
984
985static struct spi_imx_devtype_data imx51_ecspi_devtype_data = {
986 .intctrl = mx51_ecspi_intctrl,
Uwe Kleine-Könige6972712018-11-30 07:47:05 +0100987 .prepare_message = mx51_ecspi_prepare_message,
Uwe Kleine-König1d374702018-11-30 07:47:08 +0100988 .prepare_transfer = mx51_ecspi_prepare_transfer,
Shawn Guo04ee5852011-07-10 01:16:39 +0800989 .trigger = mx51_ecspi_trigger,
990 .rx_available = mx51_ecspi_rx_available,
991 .reset = mx51_ecspi_reset,
Robin Gong987a2df2018-10-10 10:32:42 +0000992 .setup_wml = mx51_setup_wml,
Robin Gongbcd8e772020-05-21 04:34:17 +0800993 .disable_dma = mx51_disable_dma,
jiada wangfd8d4e22017-06-08 14:16:00 +0900994 .fifo_size = 64,
995 .has_dmamode = true,
jiada wang1673c812017-08-10 13:50:08 +0900996 .dynamic_burst = true,
jiada wang71abd292017-09-05 14:12:32 +0900997 .has_slavemode = true,
998 .disable = mx51_ecspi_disable,
Shawn Guo04ee5852011-07-10 01:16:39 +0800999 .devtype = IMX51_ECSPI,
1000};
1001
jiada wang26e4bb82017-06-08 14:16:01 +09001002static struct spi_imx_devtype_data imx53_ecspi_devtype_data = {
1003 .intctrl = mx51_ecspi_intctrl,
Uwe Kleine-Könige6972712018-11-30 07:47:05 +01001004 .prepare_message = mx51_ecspi_prepare_message,
Uwe Kleine-König1d374702018-11-30 07:47:08 +01001005 .prepare_transfer = mx51_ecspi_prepare_transfer,
jiada wang26e4bb82017-06-08 14:16:01 +09001006 .trigger = mx51_ecspi_trigger,
1007 .rx_available = mx51_ecspi_rx_available,
Robin Gongbcd8e772020-05-21 04:34:17 +08001008 .disable_dma = mx51_disable_dma,
jiada wang26e4bb82017-06-08 14:16:01 +09001009 .reset = mx51_ecspi_reset,
1010 .fifo_size = 64,
1011 .has_dmamode = true,
jiada wang71abd292017-09-05 14:12:32 +09001012 .has_slavemode = true,
1013 .disable = mx51_ecspi_disable,
jiada wang26e4bb82017-06-08 14:16:01 +09001014 .devtype = IMX53_ECSPI,
1015};
1016
Shawn Guo22a85e42011-07-10 01:16:41 +08001017static const struct of_device_id spi_imx_dt_ids[] = {
1018 { .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, },
1019 { .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, },
1020 { .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, },
1021 { .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, },
1022 { .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, },
1023 { .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, },
jiada wang26e4bb82017-06-08 14:16:01 +09001024 { .compatible = "fsl,imx53-ecspi", .data = &imx53_ecspi_devtype_data, },
Shawn Guo22a85e42011-07-10 01:16:41 +08001025 { /* sentinel */ }
1026};
Niels de Vos27743e02013-07-29 09:38:05 +02001027MODULE_DEVICE_TABLE(of, spi_imx_dt_ids);
Shawn Guo22a85e42011-07-10 01:16:41 +08001028
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001029static void spi_imx_set_burst_len(struct spi_imx_data *spi_imx, int n_bits)
1030{
1031 u32 ctrl;
1032
1033 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
1034 ctrl &= ~MX51_ECSPI_CTRL_BL_MASK;
1035 ctrl |= ((n_bits - 1) << MX51_ECSPI_CTRL_BL_OFFSET);
1036 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
1037}
1038
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001039static void spi_imx_push(struct spi_imx_data *spi_imx)
1040{
Uwe Kleine-Königbd961692021-07-16 19:39:27 +02001041 unsigned int burst_len;
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001042
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001043 /*
1044 * Reload the FIFO when the remaining bytes to be transferred in the
1045 * current burst is 0. This only applies when bits_per_word is a
1046 * multiple of 8.
1047 */
1048 if (!spi_imx->remainder) {
1049 if (spi_imx->dynamic_burst) {
1050
1051 /* We need to deal unaligned data first */
1052 burst_len = spi_imx->count % MX51_ECSPI_CTRL_MAX_BURST;
1053
1054 if (!burst_len)
1055 burst_len = MX51_ECSPI_CTRL_MAX_BURST;
1056
1057 spi_imx_set_burst_len(spi_imx, burst_len * 8);
1058
1059 spi_imx->remainder = burst_len;
1060 } else {
Uwe Kleine-Königbd961692021-07-16 19:39:27 +02001061 spi_imx->remainder = spi_imx_bytes_per_word(spi_imx->bits_per_word);
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001062 }
1063 }
1064
jiada wangfd8d4e22017-06-08 14:16:00 +09001065 while (spi_imx->txfifo < spi_imx->devtype_data->fifo_size) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001066 if (!spi_imx->count)
1067 break;
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001068 if (spi_imx->dynamic_burst &&
Uwe Kleine-Königbd961692021-07-16 19:39:27 +02001069 spi_imx->txfifo >= DIV_ROUND_UP(spi_imx->remainder, 4))
jiada wang1673c812017-08-10 13:50:08 +09001070 break;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001071 spi_imx->tx(spi_imx);
1072 spi_imx->txfifo++;
1073 }
1074
jiada wang71abd292017-09-05 14:12:32 +09001075 if (!spi_imx->slave_mode)
1076 spi_imx->devtype_data->trigger(spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001077}
1078
1079static irqreturn_t spi_imx_isr(int irq, void *dev_id)
1080{
1081 struct spi_imx_data *spi_imx = dev_id;
1082
jiada wang71abd292017-09-05 14:12:32 +09001083 while (spi_imx->txfifo &&
1084 spi_imx->devtype_data->rx_available(spi_imx)) {
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001085 spi_imx->rx(spi_imx);
1086 spi_imx->txfifo--;
1087 }
1088
1089 if (spi_imx->count) {
1090 spi_imx_push(spi_imx);
1091 return IRQ_HANDLED;
1092 }
1093
1094 if (spi_imx->txfifo) {
1095 /* No data left to push, but still waiting for rx data,
1096 * enable receive data available interrupt.
1097 */
Shawn Guoedd501bb2011-07-10 01:16:35 +08001098 spi_imx->devtype_data->intctrl(
Uwe Kleine-Königf4ba6312010-09-09 15:29:01 +02001099 spi_imx, MXC_INT_RR);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001100 return IRQ_HANDLED;
1101 }
1102
Shawn Guoedd501bb2011-07-10 01:16:35 +08001103 spi_imx->devtype_data->intctrl(spi_imx, 0);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001104 complete(&spi_imx->xfer_done);
1105
1106 return IRQ_HANDLED;
1107}
1108
Sascha Hauer65017ee2017-06-02 07:38:03 +02001109static int spi_imx_dma_configure(struct spi_master *master)
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001110{
1111 int ret;
1112 enum dma_slave_buswidth buswidth;
1113 struct dma_slave_config rx = {}, tx = {};
1114 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1115
Sascha Hauer65017ee2017-06-02 07:38:03 +02001116 switch (spi_imx_bytes_per_word(spi_imx->bits_per_word)) {
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001117 case 4:
1118 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
1119 break;
1120 case 2:
1121 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
1122 break;
1123 case 1:
1124 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
1125 break;
1126 default:
1127 return -EINVAL;
1128 }
1129
1130 tx.direction = DMA_MEM_TO_DEV;
1131 tx.dst_addr = spi_imx->base_phys + MXC_CSPITXDATA;
1132 tx.dst_addr_width = buswidth;
1133 tx.dst_maxburst = spi_imx->wml;
1134 ret = dmaengine_slave_config(master->dma_tx, &tx);
1135 if (ret) {
1136 dev_err(spi_imx->dev, "TX dma configuration failed with %d\n", ret);
1137 return ret;
1138 }
1139
1140 rx.direction = DMA_DEV_TO_MEM;
1141 rx.src_addr = spi_imx->base_phys + MXC_CSPIRXDATA;
1142 rx.src_addr_width = buswidth;
1143 rx.src_maxburst = spi_imx->wml;
1144 ret = dmaengine_slave_config(master->dma_rx, &rx);
1145 if (ret) {
1146 dev_err(spi_imx->dev, "RX dma configuration failed with %d\n", ret);
1147 return ret;
1148 }
1149
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001150 return 0;
1151}
1152
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001153static int spi_imx_setupxfer(struct spi_device *spi,
1154 struct spi_transfer *t)
1155{
1156 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001157
Sascha Hauerabb1ff12017-06-02 07:37:59 +02001158 if (!t)
1159 return 0;
1160
Clark Wang4df2f5e2021-04-08 18:33:47 +08001161 if (!t->speed_hz) {
1162 if (!spi->max_speed_hz) {
1163 dev_err(&spi->dev, "no speed_hz provided!\n");
1164 return -EINVAL;
1165 }
1166 dev_dbg(&spi->dev, "using spi->max_speed_hz!\n");
1167 spi_imx->spi_bus_clk = spi->max_speed_hz;
1168 } else
1169 spi_imx->spi_bus_clk = t->speed_hz;
1170
Sascha Hauerd52345b2017-06-02 07:38:01 +02001171 spi_imx->bits_per_word = t->bits_per_word;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001172
Maxime Chevallier2801b2f52018-07-17 16:31:51 +02001173 /*
1174 * Initialize the functions for transfer. To transfer non byte-aligned
1175 * words, we have to use multiple word-size bursts, we can't use
1176 * dynamic_burst in that case.
1177 */
1178 if (spi_imx->devtype_data->dynamic_burst && !spi_imx->slave_mode &&
Uwe Kleine-König6e95b232021-07-27 14:42:26 +02001179 !(spi->mode & SPI_CS_WORD) &&
Maxime Chevallier2801b2f52018-07-17 16:31:51 +02001180 (spi_imx->bits_per_word == 8 ||
1181 spi_imx->bits_per_word == 16 ||
1182 spi_imx->bits_per_word == 32)) {
jiada wang1673c812017-08-10 13:50:08 +09001183
jiada wang1673c812017-08-10 13:50:08 +09001184 spi_imx->rx = spi_imx_buf_rx_swap;
1185 spi_imx->tx = spi_imx_buf_tx_swap;
1186 spi_imx->dynamic_burst = 1;
jiada wang1673c812017-08-10 13:50:08 +09001187
Sachin Kamat60514262013-05-30 13:38:09 +05301188 } else {
jiada wang1673c812017-08-10 13:50:08 +09001189 if (spi_imx->bits_per_word <= 8) {
1190 spi_imx->rx = spi_imx_buf_rx_u8;
1191 spi_imx->tx = spi_imx_buf_tx_u8;
1192 } else if (spi_imx->bits_per_word <= 16) {
1193 spi_imx->rx = spi_imx_buf_rx_u16;
1194 spi_imx->tx = spi_imx_buf_tx_u16;
1195 } else {
1196 spi_imx->rx = spi_imx_buf_rx_u32;
1197 spi_imx->tx = spi_imx_buf_tx_u32;
1198 }
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001199 spi_imx->dynamic_burst = 0;
Stephen Warren24778be2013-05-21 20:36:35 -06001200 }
Uwe Kleine-Könige6a0a8b2009-10-01 15:44:33 -07001201
Sascha Hauerc008a802016-02-24 09:20:26 +01001202 if (spi_imx_can_dma(spi_imx->bitbang.master, spi, t))
zhengbine6a8b2c2019-12-24 11:52:05 +08001203 spi_imx->usedma = true;
Sascha Hauerc008a802016-02-24 09:20:26 +01001204 else
zhengbine6a8b2c2019-12-24 11:52:05 +08001205 spi_imx->usedma = false;
Sascha Hauerc008a802016-02-24 09:20:26 +01001206
jiada wang71abd292017-09-05 14:12:32 +09001207 if (is_imx53_ecspi(spi_imx) && spi_imx->slave_mode) {
1208 spi_imx->rx = mx53_ecspi_rx_slave;
1209 spi_imx->tx = mx53_ecspi_tx_slave;
1210 spi_imx->slave_burst = t->len;
1211 }
1212
Clark Wang4df2f5e2021-04-08 18:33:47 +08001213 spi_imx->devtype_data->prepare_transfer(spi_imx, spi);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001214
1215 return 0;
1216}
1217
Robin Gongf62cacc2014-09-11 09:18:44 +08001218static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx)
1219{
1220 struct spi_master *master = spi_imx->bitbang.master;
1221
1222 if (master->dma_rx) {
1223 dma_release_channel(master->dma_rx);
1224 master->dma_rx = NULL;
1225 }
1226
1227 if (master->dma_tx) {
1228 dma_release_channel(master->dma_tx);
1229 master->dma_tx = NULL;
1230 }
Robin Gongf62cacc2014-09-11 09:18:44 +08001231}
1232
1233static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx,
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001234 struct spi_master *master)
Robin Gongf62cacc2014-09-11 09:18:44 +08001235{
Robin Gongf62cacc2014-09-11 09:18:44 +08001236 int ret;
1237
Robin Gonga02bb402015-02-03 10:25:53 +08001238 /* use pio mode for i.mx6dl chip TKT238285 */
1239 if (of_machine_is_compatible("fsl,imx6dl"))
1240 return 0;
1241
jiada wangfd8d4e22017-06-08 14:16:00 +09001242 spi_imx->wml = spi_imx->devtype_data->fifo_size / 2;
Anton Bondarenko0dfbaa82015-12-05 17:57:01 +01001243
Robin Gongf62cacc2014-09-11 09:18:44 +08001244 /* Prepare for TX DMA: */
Peter Ujfalusi5d3aa9c2019-11-13 11:42:51 +02001245 master->dma_tx = dma_request_chan(dev, "tx");
Anton Bondarenko37600472015-12-08 07:43:45 +01001246 if (IS_ERR(master->dma_tx)) {
1247 ret = PTR_ERR(master->dma_tx);
1248 dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret);
1249 master->dma_tx = NULL;
Robin Gongf62cacc2014-09-11 09:18:44 +08001250 goto err;
1251 }
1252
Robin Gongf62cacc2014-09-11 09:18:44 +08001253 /* Prepare for RX : */
Peter Ujfalusi5d3aa9c2019-11-13 11:42:51 +02001254 master->dma_rx = dma_request_chan(dev, "rx");
Anton Bondarenko37600472015-12-08 07:43:45 +01001255 if (IS_ERR(master->dma_rx)) {
1256 ret = PTR_ERR(master->dma_rx);
1257 dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret);
1258 master->dma_rx = NULL;
Robin Gongf62cacc2014-09-11 09:18:44 +08001259 goto err;
1260 }
1261
Robin Gongf62cacc2014-09-11 09:18:44 +08001262 init_completion(&spi_imx->dma_rx_completion);
1263 init_completion(&spi_imx->dma_tx_completion);
1264 master->can_dma = spi_imx_can_dma;
1265 master->max_dma_len = MAX_SDMA_BD_BYTES;
1266 spi_imx->bitbang.master->flags = SPI_MASTER_MUST_RX |
1267 SPI_MASTER_MUST_TX;
Robin Gongf62cacc2014-09-11 09:18:44 +08001268
1269 return 0;
1270err:
1271 spi_imx_sdma_exit(spi_imx);
1272 return ret;
1273}
1274
1275static void spi_imx_dma_rx_callback(void *cookie)
1276{
1277 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
1278
1279 complete(&spi_imx->dma_rx_completion);
1280}
1281
1282static void spi_imx_dma_tx_callback(void *cookie)
1283{
1284 struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
1285
1286 complete(&spi_imx->dma_tx_completion);
1287}
1288
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001289static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size)
1290{
1291 unsigned long timeout = 0;
1292
1293 /* Time with actual data transfer and CS change delay related to HW */
1294 timeout = (8 + 4) * size / spi_imx->spi_bus_clk;
1295
1296 /* Add extra second for scheduler related activities */
1297 timeout += 1;
1298
1299 /* Double calculated timeout */
1300 return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);
1301}
1302
Robin Gongf62cacc2014-09-11 09:18:44 +08001303static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,
1304 struct spi_transfer *transfer)
1305{
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001306 struct dma_async_tx_descriptor *desc_tx, *desc_rx;
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001307 unsigned long transfer_timeout;
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001308 unsigned long timeout;
Robin Gongf62cacc2014-09-11 09:18:44 +08001309 struct spi_master *master = spi_imx->bitbang.master;
1310 struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
Robin Gong5ba5a372018-10-10 10:32:45 +00001311 struct scatterlist *last_sg = sg_last(rx->sgl, rx->nents);
1312 unsigned int bytes_per_word, i;
Robin Gong987a2df2018-10-10 10:32:42 +00001313 int ret;
1314
Robin Gong5ba5a372018-10-10 10:32:45 +00001315 /* Get the right burst length from the last sg to ensure no tail data */
1316 bytes_per_word = spi_imx_bytes_per_word(transfer->bits_per_word);
1317 for (i = spi_imx->devtype_data->fifo_size / 2; i > 0; i--) {
1318 if (!(sg_dma_len(last_sg) % (i * bytes_per_word)))
1319 break;
1320 }
1321 /* Use 1 as wml in case no available burst length got */
1322 if (i == 0)
1323 i = 1;
1324
1325 spi_imx->wml = i;
1326
Robin Gong987a2df2018-10-10 10:32:42 +00001327 ret = spi_imx_dma_configure(master);
1328 if (ret)
Robin Gong7a908832020-06-17 06:42:09 +08001329 goto dma_failure_no_start;
Robin Gong987a2df2018-10-10 10:32:42 +00001330
Robin Gong5ba5a372018-10-10 10:32:45 +00001331 if (!spi_imx->devtype_data->setup_wml) {
1332 dev_err(spi_imx->dev, "No setup_wml()?\n");
Robin Gong7a908832020-06-17 06:42:09 +08001333 ret = -EINVAL;
1334 goto dma_failure_no_start;
Robin Gong5ba5a372018-10-10 10:32:45 +00001335 }
Robin Gong987a2df2018-10-10 10:32:42 +00001336 spi_imx->devtype_data->setup_wml(spi_imx);
Robin Gongf62cacc2014-09-11 09:18:44 +08001337
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001338 /*
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001339 * The TX DMA setup starts the transfer, so make sure RX is configured
1340 * before TX.
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001341 */
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001342 desc_rx = dmaengine_prep_slave_sg(master->dma_rx,
1343 rx->sgl, rx->nents, DMA_DEV_TO_MEM,
1344 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Robin Gong7a908832020-06-17 06:42:09 +08001345 if (!desc_rx) {
1346 ret = -EINVAL;
1347 goto dma_failure_no_start;
1348 }
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001349
1350 desc_rx->callback = spi_imx_dma_rx_callback;
1351 desc_rx->callback_param = (void *)spi_imx;
1352 dmaengine_submit(desc_rx);
1353 reinit_completion(&spi_imx->dma_rx_completion);
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001354 dma_async_issue_pending(master->dma_rx);
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001355
1356 desc_tx = dmaengine_prep_slave_sg(master->dma_tx,
1357 tx->sgl, tx->nents, DMA_MEM_TO_DEV,
1358 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1359 if (!desc_tx) {
1360 dmaengine_terminate_all(master->dma_tx);
Robin Gongbcd8e772020-05-21 04:34:17 +08001361 dmaengine_terminate_all(master->dma_rx);
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001362 return -EINVAL;
1363 }
1364
1365 desc_tx->callback = spi_imx_dma_tx_callback;
1366 desc_tx->callback_param = (void *)spi_imx;
1367 dmaengine_submit(desc_tx);
1368 reinit_completion(&spi_imx->dma_tx_completion);
Anton Bondarenkofab44ef2015-12-05 17:57:00 +01001369 dma_async_issue_pending(master->dma_tx);
Robin Gongf62cacc2014-09-11 09:18:44 +08001370
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001371 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1372
Robin Gongf62cacc2014-09-11 09:18:44 +08001373 /* Wait SDMA to finish the data transfer.*/
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001374 timeout = wait_for_completion_timeout(&spi_imx->dma_tx_completion,
Anton Bondarenko4bfe9272016-02-19 08:43:03 +01001375 transfer_timeout);
Nicholas Mc Guire56536a72015-02-02 03:30:35 -05001376 if (!timeout) {
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001377 dev_err(spi_imx->dev, "I/O Error in DMA TX\n");
Robin Gongf62cacc2014-09-11 09:18:44 +08001378 dmaengine_terminate_all(master->dma_tx);
Anton Bondarenkoe47b33c2015-12-05 17:56:59 +01001379 dmaengine_terminate_all(master->dma_rx);
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001380 return -ETIMEDOUT;
Robin Gongf62cacc2014-09-11 09:18:44 +08001381 }
1382
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001383 timeout = wait_for_completion_timeout(&spi_imx->dma_rx_completion,
1384 transfer_timeout);
1385 if (!timeout) {
1386 dev_err(&master->dev, "I/O Error in DMA RX\n");
1387 spi_imx->devtype_data->reset(spi_imx);
1388 dmaengine_terminate_all(master->dma_rx);
1389 return -ETIMEDOUT;
1390 }
Robin Gongf62cacc2014-09-11 09:18:44 +08001391
Sascha Hauer6b6192c2016-02-24 09:20:33 +01001392 return transfer->len;
Robin Gong7a908832020-06-17 06:42:09 +08001393/* fallback to pio */
1394dma_failure_no_start:
1395 transfer->error |= SPI_TRANS_FAIL_NO_START;
1396 return ret;
Robin Gongf62cacc2014-09-11 09:18:44 +08001397}
1398
1399static int spi_imx_pio_transfer(struct spi_device *spi,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001400 struct spi_transfer *transfer)
1401{
1402 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
Christian Gmeinerff1ba3d2016-06-21 14:12:54 +02001403 unsigned long transfer_timeout;
1404 unsigned long timeout;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001405
1406 spi_imx->tx_buf = transfer->tx_buf;
1407 spi_imx->rx_buf = transfer->rx_buf;
1408 spi_imx->count = transfer->len;
1409 spi_imx->txfifo = 0;
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001410 spi_imx->remainder = 0;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001411
Axel Linaa0fe822014-02-09 11:06:04 +08001412 reinit_completion(&spi_imx->xfer_done);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001413
1414 spi_imx_push(spi_imx);
1415
Shawn Guoedd501bb2011-07-10 01:16:35 +08001416 spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001417
Christian Gmeinerff1ba3d2016-06-21 14:12:54 +02001418 transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1419
1420 timeout = wait_for_completion_timeout(&spi_imx->xfer_done,
1421 transfer_timeout);
1422 if (!timeout) {
1423 dev_err(&spi->dev, "I/O Error in PIO\n");
1424 spi_imx->devtype_data->reset(spi_imx);
1425 return -ETIMEDOUT;
1426 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001427
1428 return transfer->len;
1429}
1430
jiada wang71abd292017-09-05 14:12:32 +09001431static int spi_imx_pio_transfer_slave(struct spi_device *spi,
1432 struct spi_transfer *transfer)
1433{
1434 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1435 int ret = transfer->len;
1436
1437 if (is_imx53_ecspi(spi_imx) &&
1438 transfer->len > MX53_MAX_TRANSFER_BYTES) {
1439 dev_err(&spi->dev, "Transaction too big, max size is %d bytes\n",
1440 MX53_MAX_TRANSFER_BYTES);
1441 return -EMSGSIZE;
1442 }
1443
1444 spi_imx->tx_buf = transfer->tx_buf;
1445 spi_imx->rx_buf = transfer->rx_buf;
1446 spi_imx->count = transfer->len;
1447 spi_imx->txfifo = 0;
Maxime Chevallier2ca300a2018-07-17 16:31:54 +02001448 spi_imx->remainder = 0;
jiada wang71abd292017-09-05 14:12:32 +09001449
1450 reinit_completion(&spi_imx->xfer_done);
1451 spi_imx->slave_aborted = false;
1452
1453 spi_imx_push(spi_imx);
1454
1455 spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE | MXC_INT_RDR);
1456
1457 if (wait_for_completion_interruptible(&spi_imx->xfer_done) ||
1458 spi_imx->slave_aborted) {
1459 dev_dbg(&spi->dev, "interrupted\n");
1460 ret = -EINTR;
1461 }
1462
1463 /* ecspi has a HW issue when works in Slave mode,
1464 * after 64 words writtern to TXFIFO, even TXFIFO becomes empty,
1465 * ECSPI_TXDATA keeps shift out the last word data,
1466 * so we have to disable ECSPI when in slave mode after the
1467 * transfer completes
1468 */
1469 if (spi_imx->devtype_data->disable)
1470 spi_imx->devtype_data->disable(spi_imx);
1471
1472 return ret;
1473}
1474
Robin Gongf62cacc2014-09-11 09:18:44 +08001475static int spi_imx_transfer(struct spi_device *spi,
1476 struct spi_transfer *transfer)
1477{
Robin Gongf62cacc2014-09-11 09:18:44 +08001478 struct spi_imx_data *spi_imx = spi_master_get_devdata(spi->master);
1479
Marc Kleine-Buddebf253e62020-09-17 22:24:20 +02001480 transfer->effective_speed_hz = spi_imx->spi_bus_clk;
1481
jiada wang71abd292017-09-05 14:12:32 +09001482 /* flush rxfifo before transfer */
1483 while (spi_imx->devtype_data->rx_available(spi_imx))
Trent Piephoc8427492019-03-04 20:18:49 +00001484 readl(spi_imx->base + MXC_CSPIRXDATA);
jiada wang71abd292017-09-05 14:12:32 +09001485
1486 if (spi_imx->slave_mode)
1487 return spi_imx_pio_transfer_slave(spi, transfer);
1488
Robin Gong7a908832020-06-17 06:42:09 +08001489 if (spi_imx->usedma)
1490 return spi_imx_dma_transfer(spi_imx, transfer);
Robin Gongbcd8e772020-05-21 04:34:17 +08001491
1492 return spi_imx_pio_transfer(spi, transfer);
Robin Gongf62cacc2014-09-11 09:18:44 +08001493}
1494
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001495static int spi_imx_setup(struct spi_device *spi)
1496{
Alberto Panizzof4d4ecf2010-01-20 13:49:45 -07001497 dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001498 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1499
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001500 return 0;
1501}
1502
1503static void spi_imx_cleanup(struct spi_device *spi)
1504{
1505}
1506
Huang Shijie9e556dc2013-10-23 16:31:50 +08001507static int
1508spi_imx_prepare_message(struct spi_master *master, struct spi_message *msg)
1509{
1510 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1511 int ret;
1512
Clark Wang525c9e52020-07-27 14:33:54 +08001513 ret = pm_runtime_get_sync(spi_imx->dev);
1514 if (ret < 0) {
Zhang Qilong1dcbdd92020-11-02 22:58:35 +08001515 pm_runtime_put_noidle(spi_imx->dev);
Clark Wang525c9e52020-07-27 14:33:54 +08001516 dev_err(spi_imx->dev, "failed to enable clock\n");
Huang Shijie9e556dc2013-10-23 16:31:50 +08001517 return ret;
1518 }
1519
Uwe Kleine-Könige6972712018-11-30 07:47:05 +01001520 ret = spi_imx->devtype_data->prepare_message(spi_imx, msg);
1521 if (ret) {
Clark Wang525c9e52020-07-27 14:33:54 +08001522 pm_runtime_mark_last_busy(spi_imx->dev);
1523 pm_runtime_put_autosuspend(spi_imx->dev);
Uwe Kleine-Könige6972712018-11-30 07:47:05 +01001524 }
1525
1526 return ret;
Huang Shijie9e556dc2013-10-23 16:31:50 +08001527}
1528
1529static int
1530spi_imx_unprepare_message(struct spi_master *master, struct spi_message *msg)
1531{
1532 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1533
Clark Wang525c9e52020-07-27 14:33:54 +08001534 pm_runtime_mark_last_busy(spi_imx->dev);
1535 pm_runtime_put_autosuspend(spi_imx->dev);
Huang Shijie9e556dc2013-10-23 16:31:50 +08001536 return 0;
1537}
1538
jiada wang71abd292017-09-05 14:12:32 +09001539static int spi_imx_slave_abort(struct spi_master *master)
1540{
1541 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
1542
1543 spi_imx->slave_aborted = true;
1544 complete(&spi_imx->xfer_done);
1545
1546 return 0;
1547}
1548
Grant Likelyfd4a3192012-12-07 16:57:14 +00001549static int spi_imx_probe(struct platform_device *pdev)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001550{
Shawn Guo22a85e42011-07-10 01:16:41 +08001551 struct device_node *np = pdev->dev.of_node;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001552 struct spi_master *master;
1553 struct spi_imx_data *spi_imx;
1554 struct resource *res;
Linus Walleij8cdcd8a2020-06-25 22:02:52 +02001555 int ret, irq, spi_drctl;
Tian Tao200d925e2021-03-22 11:57:56 +08001556 const struct spi_imx_devtype_data *devtype_data =
1557 of_device_get_match_data(&pdev->dev);
jiada wang71abd292017-09-05 14:12:32 +09001558 bool slave_mode;
Linus Walleij8cdcd8a2020-06-25 22:02:52 +02001559 u32 val;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001560
jiada wang71abd292017-09-05 14:12:32 +09001561 slave_mode = devtype_data->has_slavemode &&
1562 of_property_read_bool(np, "spi-slave");
1563 if (slave_mode)
1564 master = spi_alloc_slave(&pdev->dev,
1565 sizeof(struct spi_imx_data));
1566 else
1567 master = spi_alloc_master(&pdev->dev,
1568 sizeof(struct spi_imx_data));
Fabio Estevam2c147772017-06-20 13:50:55 -03001569 if (!master)
1570 return -ENOMEM;
1571
Leif Middelschultef72efa72017-04-23 21:19:58 +02001572 ret = of_property_read_u32(np, "fsl,spi-rdy-drctl", &spi_drctl);
1573 if ((ret < 0) || (spi_drctl >= 0x3)) {
1574 /* '11' is reserved */
1575 spi_drctl = 0;
1576 }
1577
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001578 platform_set_drvdata(pdev, master);
1579
Stephen Warren24778be2013-05-21 20:36:35 -06001580 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
Alexander Shiyanb36581d2016-06-08 20:02:06 +03001581 master->bus_num = np ? -1 : pdev->id;
Linus Walleij8cdcd8a2020-06-25 22:02:52 +02001582 master->use_gpio_descriptors = true;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001583
1584 spi_imx = spi_master_get_devdata(master);
Axel Lin94c69f72013-09-10 15:43:41 +08001585 spi_imx->bitbang.master = master;
Sascha Hauer6aa800c2016-02-17 14:28:48 +01001586 spi_imx->dev = &pdev->dev;
jiada wang71abd292017-09-05 14:12:32 +09001587 spi_imx->slave_mode = slave_mode;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001588
jiada wang71abd292017-09-05 14:12:32 +09001589 spi_imx->devtype_data = devtype_data;
Anton Bondarenko4686d1c2015-12-08 07:43:44 +01001590
Linus Walleij8cdcd8a2020-06-25 22:02:52 +02001591 /*
1592 * Get number of chip selects from device properties. This can be
1593 * coming from device tree or boardfiles, if it is not defined,
1594 * a default value of 3 chip selects will be used, as all the legacy
1595 * board files have <= 3 chip selects.
1596 */
1597 if (!device_property_read_u32(&pdev->dev, "num-cs", &val))
1598 master->num_chipselect = val;
1599 else
1600 master->num_chipselect = 3;
Fabio Estevam4cc122a2011-09-15 17:21:15 -03001601
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001602 spi_imx->bitbang.setup_transfer = spi_imx_setupxfer;
1603 spi_imx->bitbang.txrx_bufs = spi_imx_transfer;
1604 spi_imx->bitbang.master->setup = spi_imx_setup;
1605 spi_imx->bitbang.master->cleanup = spi_imx_cleanup;
Huang Shijie9e556dc2013-10-23 16:31:50 +08001606 spi_imx->bitbang.master->prepare_message = spi_imx_prepare_message;
1607 spi_imx->bitbang.master->unprepare_message = spi_imx_unprepare_message;
jiada wang71abd292017-09-05 14:12:32 +09001608 spi_imx->bitbang.master->slave_abort = spi_imx_slave_abort;
Oleksij Rempelab2f3572017-07-25 09:57:09 +02001609 spi_imx->bitbang.master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
1610 | SPI_NO_CS;
jiada wang26e4bb82017-06-08 14:16:01 +09001611 if (is_imx35_cspi(spi_imx) || is_imx51_ecspi(spi_imx) ||
1612 is_imx53_ecspi(spi_imx))
Leif Middelschultef72efa72017-04-23 21:19:58 +02001613 spi_imx->bitbang.master->mode_bits |= SPI_LOOP | SPI_READY;
1614
Uwe Kleine-König6e95b232021-07-27 14:42:26 +02001615 if (is_imx51_ecspi(spi_imx) &&
1616 device_property_read_u32(&pdev->dev, "cs-gpios", NULL))
1617 /*
1618 * When using HW-CS implementing SPI_CS_WORD can be done by just
1619 * setting the burst length to the word size. This is
1620 * considerably faster than manually controlling the CS.
1621 */
1622 spi_imx->bitbang.master->mode_bits |= SPI_CS_WORD;
1623
Leif Middelschultef72efa72017-04-23 21:19:58 +02001624 spi_imx->spi_drctl = spi_drctl;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001625
1626 init_completion(&spi_imx->xfer_done);
1627
1628 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001629 spi_imx->base = devm_ioremap_resource(&pdev->dev, res);
1630 if (IS_ERR(spi_imx->base)) {
1631 ret = PTR_ERR(spi_imx->base);
1632 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001633 }
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001634 spi_imx->base_phys = res->start;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001635
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001636 irq = platform_get_irq(pdev, 0);
1637 if (irq < 0) {
1638 ret = irq;
Fabio Estevam130b82c2013-07-11 01:26:48 -03001639 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001640 }
1641
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001642 ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0,
Alexander Shiyan8fc39b52014-02-22 17:23:46 +04001643 dev_name(&pdev->dev), spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001644 if (ret) {
Fabio Estevam4b5d6aa2014-12-29 19:38:51 -02001645 dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001646 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001647 }
1648
Sascha Haueraa29d8402012-03-07 09:30:22 +01001649 spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1650 if (IS_ERR(spi_imx->clk_ipg)) {
1651 ret = PTR_ERR(spi_imx->clk_ipg);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001652 goto out_master_put;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001653 }
1654
Sascha Haueraa29d8402012-03-07 09:30:22 +01001655 spi_imx->clk_per = devm_clk_get(&pdev->dev, "per");
1656 if (IS_ERR(spi_imx->clk_per)) {
1657 ret = PTR_ERR(spi_imx->clk_per);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001658 goto out_master_put;
Sascha Haueraa29d8402012-03-07 09:30:22 +01001659 }
1660
Sascha Hauer43b6bf42020-10-21 12:45:13 +02001661 ret = clk_prepare_enable(spi_imx->clk_per);
1662 if (ret)
1663 goto out_master_put;
1664
1665 ret = clk_prepare_enable(spi_imx->clk_ipg);
1666 if (ret)
1667 goto out_put_per;
1668
Clark Wang525c9e52020-07-27 14:33:54 +08001669 pm_runtime_set_autosuspend_delay(spi_imx->dev, MXC_RPM_TIMEOUT);
1670 pm_runtime_use_autosuspend(spi_imx->dev);
Clark Wang7cd71202020-11-24 16:52:47 +08001671 pm_runtime_get_noresume(spi_imx->dev);
Sascha Hauer43b6bf42020-10-21 12:45:13 +02001672 pm_runtime_set_active(spi_imx->dev);
1673 pm_runtime_enable(spi_imx->dev);
Sascha Haueraa29d8402012-03-07 09:30:22 +01001674
1675 spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
Robin Gongf62cacc2014-09-11 09:18:44 +08001676 /*
Martin Kaiser2dd33f92016-10-20 00:42:25 +02001677 * Only validated on i.mx35 and i.mx6 now, can remove the constraint
1678 * if validated on other chips.
Robin Gongf62cacc2014-09-11 09:18:44 +08001679 */
jiada wangfd8d4e22017-06-08 14:16:00 +09001680 if (spi_imx->devtype_data->has_dmamode) {
Anton Bondarenkof12ae172016-02-24 09:20:29 +01001681 ret = spi_imx_sdma_init(&pdev->dev, spi_imx, master);
Anton Bondarenkobf9af082015-12-08 07:43:46 +01001682 if (ret == -EPROBE_DEFER)
Clark Wang525c9e52020-07-27 14:33:54 +08001683 goto out_runtime_pm_put;
Anton Bondarenkobf9af082015-12-08 07:43:46 +01001684
Anton Bondarenko37600472015-12-08 07:43:45 +01001685 if (ret < 0)
Fabio Estevam0ec0da72020-08-18 19:35:18 -03001686 dev_dbg(&pdev->dev, "dma setup error %d, use pio\n",
Anton Bondarenko37600472015-12-08 07:43:45 +01001687 ret);
1688 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001689
Shawn Guoedd501bb2011-07-10 01:16:35 +08001690 spi_imx->devtype_data->reset(spi_imx);
Daniel Mackce1807b2009-11-19 19:01:42 +00001691
Shawn Guoedd501bb2011-07-10 01:16:35 +08001692 spi_imx->devtype_data->intctrl(spi_imx, 0);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001693
Shawn Guo22a85e42011-07-10 01:16:41 +08001694 master->dev.of_node = pdev->dev.of_node;
Trent Piepho8197f482017-11-06 10:38:23 -08001695 ret = spi_bitbang_start(&spi_imx->bitbang);
1696 if (ret) {
Guido Günther83466332021-01-18 17:31:10 +01001697 dev_err_probe(&pdev->dev, ret, "bitbang start failed\n");
Marek Vasut45f0bbd2020-10-05 15:22:29 +02001698 goto out_bitbang_start;
Trent Piepho8197f482017-11-06 10:38:23 -08001699 }
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001700
Clark Wang525c9e52020-07-27 14:33:54 +08001701 pm_runtime_mark_last_busy(spi_imx->dev);
1702 pm_runtime_put_autosuspend(spi_imx->dev);
1703
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001704 return ret;
1705
Marek Vasut45f0bbd2020-10-05 15:22:29 +02001706out_bitbang_start:
1707 if (spi_imx->devtype_data->has_dmamode)
1708 spi_imx_sdma_exit(spi_imx);
Clark Wang525c9e52020-07-27 14:33:54 +08001709out_runtime_pm_put:
1710 pm_runtime_dont_use_autosuspend(spi_imx->dev);
Sascha Hauer43b6bf42020-10-21 12:45:13 +02001711 pm_runtime_set_suspended(&pdev->dev);
Clark Wang525c9e52020-07-27 14:33:54 +08001712 pm_runtime_disable(spi_imx->dev);
Sascha Hauer43b6bf42020-10-21 12:45:13 +02001713
1714 clk_disable_unprepare(spi_imx->clk_ipg);
1715out_put_per:
1716 clk_disable_unprepare(spi_imx->clk_per);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001717out_master_put:
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001718 spi_master_put(master);
Fabio Estevam130b82c2013-07-11 01:26:48 -03001719
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001720 return ret;
1721}
1722
Grant Likelyfd4a3192012-12-07 16:57:14 +00001723static int spi_imx_remove(struct platform_device *pdev)
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001724{
1725 struct spi_master *master = platform_get_drvdata(pdev);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001726 struct spi_imx_data *spi_imx = spi_master_get_devdata(master);
Stefan Agnerd5935742018-01-07 15:05:49 +01001727 int ret;
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001728
1729 spi_bitbang_stop(&spi_imx->bitbang);
1730
Clark Wang525c9e52020-07-27 14:33:54 +08001731 ret = pm_runtime_get_sync(spi_imx->dev);
1732 if (ret < 0) {
Zhang Qilong1dcbdd92020-11-02 22:58:35 +08001733 pm_runtime_put_noidle(spi_imx->dev);
Clark Wang525c9e52020-07-27 14:33:54 +08001734 dev_err(spi_imx->dev, "failed to enable clock\n");
Stefan Agnerd5935742018-01-07 15:05:49 +01001735 return ret;
1736 }
1737
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001738 writel(0, spi_imx->base + MXC_CSPICTRL);
Clark Wang525c9e52020-07-27 14:33:54 +08001739
1740 pm_runtime_dont_use_autosuspend(spi_imx->dev);
1741 pm_runtime_put_sync(spi_imx->dev);
1742 pm_runtime_disable(spi_imx->dev);
1743
Robin Gongf62cacc2014-09-11 09:18:44 +08001744 spi_imx_sdma_exit(spi_imx);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001745 spi_master_put(master);
1746
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001747 return 0;
1748}
1749
Clark Wang525c9e52020-07-27 14:33:54 +08001750static int __maybe_unused spi_imx_runtime_resume(struct device *dev)
1751{
1752 struct spi_master *master = dev_get_drvdata(dev);
1753 struct spi_imx_data *spi_imx;
1754 int ret;
1755
1756 spi_imx = spi_master_get_devdata(master);
1757
1758 ret = clk_prepare_enable(spi_imx->clk_per);
1759 if (ret)
1760 return ret;
1761
1762 ret = clk_prepare_enable(spi_imx->clk_ipg);
1763 if (ret) {
1764 clk_disable_unprepare(spi_imx->clk_per);
1765 return ret;
1766 }
1767
1768 return 0;
1769}
1770
1771static int __maybe_unused spi_imx_runtime_suspend(struct device *dev)
1772{
1773 struct spi_master *master = dev_get_drvdata(dev);
1774 struct spi_imx_data *spi_imx;
1775
1776 spi_imx = spi_master_get_devdata(master);
1777
1778 clk_disable_unprepare(spi_imx->clk_per);
1779 clk_disable_unprepare(spi_imx->clk_ipg);
1780
1781 return 0;
1782}
1783
1784static int __maybe_unused spi_imx_suspend(struct device *dev)
1785{
1786 pinctrl_pm_select_sleep_state(dev);
1787 return 0;
1788}
1789
1790static int __maybe_unused spi_imx_resume(struct device *dev)
1791{
1792 pinctrl_pm_select_default_state(dev);
1793 return 0;
1794}
1795
1796static const struct dev_pm_ops imx_spi_pm = {
1797 SET_RUNTIME_PM_OPS(spi_imx_runtime_suspend,
1798 spi_imx_runtime_resume, NULL)
1799 SET_SYSTEM_SLEEP_PM_OPS(spi_imx_suspend, spi_imx_resume)
1800};
1801
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001802static struct platform_driver spi_imx_driver = {
1803 .driver = {
1804 .name = DRIVER_NAME,
Shawn Guo22a85e42011-07-10 01:16:41 +08001805 .of_match_table = spi_imx_dt_ids,
Clark Wang525c9e52020-07-27 14:33:54 +08001806 .pm = &imx_spi_pm,
1807 },
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001808 .probe = spi_imx_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001809 .remove = spi_imx_remove,
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001810};
Grant Likely940ab882011-10-05 11:29:49 -06001811module_platform_driver(spi_imx_driver);
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001812
Fabio Estevam92bad4a42021-03-16 15:09:22 -03001813MODULE_DESCRIPTION("i.MX SPI Controller driver");
Uwe Kleine-König6cdeb002009-10-01 15:44:28 -07001814MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1815MODULE_LICENSE("GPL");
Fabio Estevam3133fba32013-01-07 20:42:55 -02001816MODULE_ALIAS("platform:" DRIVER_NAME);