• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
3 // Copyright (C) 2008 Juergen Beisert
4 
5 #include <linux/clk.h>
6 #include <linux/completion.h>
7 #include <linux/delay.h>
8 #include <linux/dmaengine.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/err.h>
11 #include <linux/interrupt.h>
12 #include <linux/io.h>
13 #include <linux/irq.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 #include <linux/spi/spi.h>
21 #include <linux/types.h>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 #include <linux/property.h>
25 
26 #include <linux/dma/imx-dma.h>
27 
28 #define DRIVER_NAME "spi_imx"
29 
30 static bool use_dma = true;
31 module_param(use_dma, bool, 0644);
32 MODULE_PARM_DESC(use_dma, "Enable usage of DMA when available (default)");
33 
34 /* define polling limits */
35 static unsigned int polling_limit_us = 30;
36 module_param(polling_limit_us, uint, 0664);
37 MODULE_PARM_DESC(polling_limit_us,
38 		 "time in us to run a transfer in polling mode\n");
39 
40 #define MXC_RPM_TIMEOUT		2000 /* 2000ms */
41 
42 #define MXC_CSPIRXDATA		0x00
43 #define MXC_CSPITXDATA		0x04
44 #define MXC_CSPICTRL		0x08
45 #define MXC_CSPIINT		0x0c
46 #define MXC_RESET		0x1c
47 
48 /* generic defines to abstract from the different register layouts */
49 #define MXC_INT_RR	(1 << 0) /* Receive data ready interrupt */
50 #define MXC_INT_TE	(1 << 1) /* Transmit FIFO empty interrupt */
51 #define MXC_INT_RDR	BIT(4) /* Receive date threshold interrupt */
52 
53 /* The maximum bytes that a sdma BD can transfer. */
54 #define MAX_SDMA_BD_BYTES (1 << 15)
55 #define MX51_ECSPI_CTRL_MAX_BURST	512
56 /* The maximum bytes that IMX53_ECSPI can transfer in slave mode.*/
57 #define MX53_MAX_TRANSFER_BYTES		512
58 
59 enum spi_imx_devtype {
60 	IMX1_CSPI,
61 	IMX21_CSPI,
62 	IMX27_CSPI,
63 	IMX31_CSPI,
64 	IMX35_CSPI,	/* CSPI on all i.mx except above */
65 	IMX51_ECSPI,	/* ECSPI on i.mx51 */
66 	IMX53_ECSPI,	/* ECSPI on i.mx53 and later */
67 };
68 
69 struct spi_imx_data;
70 
71 struct spi_imx_devtype_data {
72 	void (*intctrl)(struct spi_imx_data *spi_imx, int enable);
73 	int (*prepare_message)(struct spi_imx_data *spi_imx, struct spi_message *msg);
74 	int (*prepare_transfer)(struct spi_imx_data *spi_imx, struct spi_device *spi);
75 	void (*trigger)(struct spi_imx_data *spi_imx);
76 	int (*rx_available)(struct spi_imx_data *spi_imx);
77 	void (*reset)(struct spi_imx_data *spi_imx);
78 	void (*setup_wml)(struct spi_imx_data *spi_imx);
79 	void (*disable)(struct spi_imx_data *spi_imx);
80 	void (*disable_dma)(struct spi_imx_data *spi_imx);
81 	bool has_dmamode;
82 	bool has_slavemode;
83 	unsigned int fifo_size;
84 	bool dynamic_burst;
85 	/*
86 	 * ERR009165 fixed or not:
87 	 * https://www.nxp.com/docs/en/errata/IMX6DQCE.pdf
88 	 */
89 	bool tx_glitch_fixed;
90 	enum spi_imx_devtype devtype;
91 };
92 
93 struct spi_imx_data {
94 	struct spi_controller *controller;
95 	struct device *dev;
96 
97 	struct completion xfer_done;
98 	void __iomem *base;
99 	unsigned long base_phys;
100 
101 	struct clk *clk_per;
102 	struct clk *clk_ipg;
103 	unsigned long spi_clk;
104 	unsigned int spi_bus_clk;
105 
106 	unsigned int bits_per_word;
107 	unsigned int spi_drctl;
108 
109 	unsigned int count, remainder;
110 	void (*tx)(struct spi_imx_data *spi_imx);
111 	void (*rx)(struct spi_imx_data *spi_imx);
112 	void *rx_buf;
113 	const void *tx_buf;
114 	unsigned int txfifo; /* number of words pushed in tx FIFO */
115 	unsigned int dynamic_burst;
116 	bool rx_only;
117 
118 	/* Slave mode */
119 	bool slave_mode;
120 	bool slave_aborted;
121 	unsigned int slave_burst;
122 
123 	/* DMA */
124 	bool usedma;
125 	u32 wml;
126 	struct completion dma_rx_completion;
127 	struct completion dma_tx_completion;
128 
129 	const struct spi_imx_devtype_data *devtype_data;
130 };
131 
is_imx27_cspi(struct spi_imx_data * d)132 static inline int is_imx27_cspi(struct spi_imx_data *d)
133 {
134 	return d->devtype_data->devtype == IMX27_CSPI;
135 }
136 
is_imx35_cspi(struct spi_imx_data * d)137 static inline int is_imx35_cspi(struct spi_imx_data *d)
138 {
139 	return d->devtype_data->devtype == IMX35_CSPI;
140 }
141 
is_imx51_ecspi(struct spi_imx_data * d)142 static inline int is_imx51_ecspi(struct spi_imx_data *d)
143 {
144 	return d->devtype_data->devtype == IMX51_ECSPI;
145 }
146 
is_imx53_ecspi(struct spi_imx_data * d)147 static inline int is_imx53_ecspi(struct spi_imx_data *d)
148 {
149 	return d->devtype_data->devtype == IMX53_ECSPI;
150 }
151 
152 #define MXC_SPI_BUF_RX(type)						\
153 static void spi_imx_buf_rx_##type(struct spi_imx_data *spi_imx)		\
154 {									\
155 	unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA);	\
156 									\
157 	if (spi_imx->rx_buf) {						\
158 		*(type *)spi_imx->rx_buf = val;				\
159 		spi_imx->rx_buf += sizeof(type);			\
160 	}								\
161 									\
162 	spi_imx->remainder -= sizeof(type);				\
163 }
164 
165 #define MXC_SPI_BUF_TX(type)						\
166 static void spi_imx_buf_tx_##type(struct spi_imx_data *spi_imx)		\
167 {									\
168 	type val = 0;							\
169 									\
170 	if (spi_imx->tx_buf) {						\
171 		val = *(type *)spi_imx->tx_buf;				\
172 		spi_imx->tx_buf += sizeof(type);			\
173 	}								\
174 									\
175 	spi_imx->count -= sizeof(type);					\
176 									\
177 	writel(val, spi_imx->base + MXC_CSPITXDATA);			\
178 }
179 
180 MXC_SPI_BUF_RX(u8)
181 MXC_SPI_BUF_TX(u8)
182 MXC_SPI_BUF_RX(u16)
183 MXC_SPI_BUF_TX(u16)
184 MXC_SPI_BUF_RX(u32)
185 MXC_SPI_BUF_TX(u32)
186 
187 /* First entry is reserved, second entry is valid only if SDHC_SPIEN is set
188  * (which is currently not the case in this driver)
189  */
190 static int mxc_clkdivs[] = {0, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
191 	256, 384, 512, 768, 1024};
192 
193 /* MX21, MX27 */
spi_imx_clkdiv_1(unsigned int fin,unsigned int fspi,unsigned int max,unsigned int * fres)194 static unsigned int spi_imx_clkdiv_1(unsigned int fin,
195 		unsigned int fspi, unsigned int max, unsigned int *fres)
196 {
197 	int i;
198 
199 	for (i = 2; i < max; i++)
200 		if (fspi * mxc_clkdivs[i] >= fin)
201 			break;
202 
203 	*fres = fin / mxc_clkdivs[i];
204 	return i;
205 }
206 
207 /* MX1, MX31, MX35, MX51 CSPI */
spi_imx_clkdiv_2(unsigned int fin,unsigned int fspi,unsigned int * fres)208 static unsigned int spi_imx_clkdiv_2(unsigned int fin,
209 		unsigned int fspi, unsigned int *fres)
210 {
211 	int i, div = 4;
212 
213 	for (i = 0; i < 7; i++) {
214 		if (fspi * div >= fin)
215 			goto out;
216 		div <<= 1;
217 	}
218 
219 out:
220 	*fres = fin / div;
221 	return i;
222 }
223 
spi_imx_bytes_per_word(const int bits_per_word)224 static int spi_imx_bytes_per_word(const int bits_per_word)
225 {
226 	if (bits_per_word <= 8)
227 		return 1;
228 	else if (bits_per_word <= 16)
229 		return 2;
230 	else
231 		return 4;
232 }
233 
spi_imx_can_dma(struct spi_controller * controller,struct spi_device * spi,struct spi_transfer * transfer)234 static bool spi_imx_can_dma(struct spi_controller *controller, struct spi_device *spi,
235 			 struct spi_transfer *transfer)
236 {
237 	struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
238 
239 	if (!use_dma || controller->fallback)
240 		return false;
241 
242 	if (!controller->dma_rx)
243 		return false;
244 
245 	if (spi_imx->slave_mode)
246 		return false;
247 
248 	if (transfer->len < spi_imx->devtype_data->fifo_size)
249 		return false;
250 
251 	spi_imx->dynamic_burst = 0;
252 
253 	return true;
254 }
255 
256 /*
257  * Note the number of natively supported chip selects for MX51 is 4. Some
258  * devices may have less actual SS pins but the register map supports 4. When
259  * using gpio chip selects the cs values passed into the macros below can go
260  * outside the range 0 - 3. We therefore need to limit the cs value to avoid
261  * corrupting bits outside the allocated locations.
262  *
263  * The simplest way to do this is to just mask the cs bits to 2 bits. This
264  * still allows all 4 native chip selects to work as well as gpio chip selects
265  * (which can use any of the 4 chip select configurations).
266  */
267 
268 #define MX51_ECSPI_CTRL		0x08
269 #define MX51_ECSPI_CTRL_ENABLE		(1 <<  0)
270 #define MX51_ECSPI_CTRL_XCH		(1 <<  2)
271 #define MX51_ECSPI_CTRL_SMC		(1 << 3)
272 #define MX51_ECSPI_CTRL_MODE_MASK	(0xf << 4)
273 #define MX51_ECSPI_CTRL_DRCTL(drctl)	((drctl) << 16)
274 #define MX51_ECSPI_CTRL_POSTDIV_OFFSET	8
275 #define MX51_ECSPI_CTRL_PREDIV_OFFSET	12
276 #define MX51_ECSPI_CTRL_CS(cs)		((cs & 3) << 18)
277 #define MX51_ECSPI_CTRL_BL_OFFSET	20
278 #define MX51_ECSPI_CTRL_BL_MASK		(0xfff << 20)
279 
280 #define MX51_ECSPI_CONFIG	0x0c
281 #define MX51_ECSPI_CONFIG_SCLKPHA(cs)	(1 << ((cs & 3) +  0))
282 #define MX51_ECSPI_CONFIG_SCLKPOL(cs)	(1 << ((cs & 3) +  4))
283 #define MX51_ECSPI_CONFIG_SBBCTRL(cs)	(1 << ((cs & 3) +  8))
284 #define MX51_ECSPI_CONFIG_SSBPOL(cs)	(1 << ((cs & 3) + 12))
285 #define MX51_ECSPI_CONFIG_SCLKCTL(cs)	(1 << ((cs & 3) + 20))
286 
287 #define MX51_ECSPI_INT		0x10
288 #define MX51_ECSPI_INT_TEEN		(1 <<  0)
289 #define MX51_ECSPI_INT_RREN		(1 <<  3)
290 #define MX51_ECSPI_INT_RDREN		(1 <<  4)
291 
292 #define MX51_ECSPI_DMA		0x14
293 #define MX51_ECSPI_DMA_TX_WML(wml)	((wml) & 0x3f)
294 #define MX51_ECSPI_DMA_RX_WML(wml)	(((wml) & 0x3f) << 16)
295 #define MX51_ECSPI_DMA_RXT_WML(wml)	(((wml) & 0x3f) << 24)
296 
297 #define MX51_ECSPI_DMA_TEDEN		(1 << 7)
298 #define MX51_ECSPI_DMA_RXDEN		(1 << 23)
299 #define MX51_ECSPI_DMA_RXTDEN		(1 << 31)
300 
301 #define MX51_ECSPI_STAT		0x18
302 #define MX51_ECSPI_STAT_RR		(1 <<  3)
303 
304 #define MX51_ECSPI_TESTREG	0x20
305 #define MX51_ECSPI_TESTREG_LBC	BIT(31)
306 
spi_imx_buf_rx_swap_u32(struct spi_imx_data * spi_imx)307 static void spi_imx_buf_rx_swap_u32(struct spi_imx_data *spi_imx)
308 {
309 	unsigned int val = readl(spi_imx->base + MXC_CSPIRXDATA);
310 
311 	if (spi_imx->rx_buf) {
312 #ifdef __LITTLE_ENDIAN
313 		unsigned int bytes_per_word;
314 
315 		bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
316 		if (bytes_per_word == 1)
317 			swab32s(&val);
318 		else if (bytes_per_word == 2)
319 			swahw32s(&val);
320 #endif
321 		*(u32 *)spi_imx->rx_buf = val;
322 		spi_imx->rx_buf += sizeof(u32);
323 	}
324 
325 	spi_imx->remainder -= sizeof(u32);
326 }
327 
spi_imx_buf_rx_swap(struct spi_imx_data * spi_imx)328 static void spi_imx_buf_rx_swap(struct spi_imx_data *spi_imx)
329 {
330 	int unaligned;
331 	u32 val;
332 
333 	unaligned = spi_imx->remainder % 4;
334 
335 	if (!unaligned) {
336 		spi_imx_buf_rx_swap_u32(spi_imx);
337 		return;
338 	}
339 
340 	if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) {
341 		spi_imx_buf_rx_u16(spi_imx);
342 		return;
343 	}
344 
345 	val = readl(spi_imx->base + MXC_CSPIRXDATA);
346 
347 	while (unaligned--) {
348 		if (spi_imx->rx_buf) {
349 			*(u8 *)spi_imx->rx_buf = (val >> (8 * unaligned)) & 0xff;
350 			spi_imx->rx_buf++;
351 		}
352 		spi_imx->remainder--;
353 	}
354 }
355 
spi_imx_buf_tx_swap_u32(struct spi_imx_data * spi_imx)356 static void spi_imx_buf_tx_swap_u32(struct spi_imx_data *spi_imx)
357 {
358 	u32 val = 0;
359 #ifdef __LITTLE_ENDIAN
360 	unsigned int bytes_per_word;
361 #endif
362 
363 	if (spi_imx->tx_buf) {
364 		val = *(u32 *)spi_imx->tx_buf;
365 		spi_imx->tx_buf += sizeof(u32);
366 	}
367 
368 	spi_imx->count -= sizeof(u32);
369 #ifdef __LITTLE_ENDIAN
370 	bytes_per_word = spi_imx_bytes_per_word(spi_imx->bits_per_word);
371 
372 	if (bytes_per_word == 1)
373 		swab32s(&val);
374 	else if (bytes_per_word == 2)
375 		swahw32s(&val);
376 #endif
377 	writel(val, spi_imx->base + MXC_CSPITXDATA);
378 }
379 
spi_imx_buf_tx_swap(struct spi_imx_data * spi_imx)380 static void spi_imx_buf_tx_swap(struct spi_imx_data *spi_imx)
381 {
382 	int unaligned;
383 	u32 val = 0;
384 
385 	unaligned = spi_imx->count % 4;
386 
387 	if (!unaligned) {
388 		spi_imx_buf_tx_swap_u32(spi_imx);
389 		return;
390 	}
391 
392 	if (spi_imx_bytes_per_word(spi_imx->bits_per_word) == 2) {
393 		spi_imx_buf_tx_u16(spi_imx);
394 		return;
395 	}
396 
397 	while (unaligned--) {
398 		if (spi_imx->tx_buf) {
399 			val |= *(u8 *)spi_imx->tx_buf << (8 * unaligned);
400 			spi_imx->tx_buf++;
401 		}
402 		spi_imx->count--;
403 	}
404 
405 	writel(val, spi_imx->base + MXC_CSPITXDATA);
406 }
407 
mx53_ecspi_rx_slave(struct spi_imx_data * spi_imx)408 static void mx53_ecspi_rx_slave(struct spi_imx_data *spi_imx)
409 {
410 	u32 val = be32_to_cpu(readl(spi_imx->base + MXC_CSPIRXDATA));
411 
412 	if (spi_imx->rx_buf) {
413 		int n_bytes = spi_imx->slave_burst % sizeof(val);
414 
415 		if (!n_bytes)
416 			n_bytes = sizeof(val);
417 
418 		memcpy(spi_imx->rx_buf,
419 		       ((u8 *)&val) + sizeof(val) - n_bytes, n_bytes);
420 
421 		spi_imx->rx_buf += n_bytes;
422 		spi_imx->slave_burst -= n_bytes;
423 	}
424 
425 	spi_imx->remainder -= sizeof(u32);
426 }
427 
mx53_ecspi_tx_slave(struct spi_imx_data * spi_imx)428 static void mx53_ecspi_tx_slave(struct spi_imx_data *spi_imx)
429 {
430 	u32 val = 0;
431 	int n_bytes = spi_imx->count % sizeof(val);
432 
433 	if (!n_bytes)
434 		n_bytes = sizeof(val);
435 
436 	if (spi_imx->tx_buf) {
437 		memcpy(((u8 *)&val) + sizeof(val) - n_bytes,
438 		       spi_imx->tx_buf, n_bytes);
439 		val = cpu_to_be32(val);
440 		spi_imx->tx_buf += n_bytes;
441 	}
442 
443 	spi_imx->count -= n_bytes;
444 
445 	writel(val, spi_imx->base + MXC_CSPITXDATA);
446 }
447 
448 /* MX51 eCSPI */
mx51_ecspi_clkdiv(struct spi_imx_data * spi_imx,unsigned int fspi,unsigned int * fres)449 static unsigned int mx51_ecspi_clkdiv(struct spi_imx_data *spi_imx,
450 				      unsigned int fspi, unsigned int *fres)
451 {
452 	/*
453 	 * there are two 4-bit dividers, the pre-divider divides by
454 	 * $pre, the post-divider by 2^$post
455 	 */
456 	unsigned int pre, post;
457 	unsigned int fin = spi_imx->spi_clk;
458 
459 	fspi = min(fspi, fin);
460 
461 	post = fls(fin) - fls(fspi);
462 	if (fin > fspi << post)
463 		post++;
464 
465 	/* now we have: (fin <= fspi << post) with post being minimal */
466 
467 	post = max(4U, post) - 4;
468 	if (unlikely(post > 0xf)) {
469 		dev_err(spi_imx->dev, "cannot set clock freq: %u (base freq: %u)\n",
470 				fspi, fin);
471 		return 0xff;
472 	}
473 
474 	pre = DIV_ROUND_UP(fin, fspi << post) - 1;
475 
476 	dev_dbg(spi_imx->dev, "%s: fin: %u, fspi: %u, post: %u, pre: %u\n",
477 			__func__, fin, fspi, post, pre);
478 
479 	/* Resulting frequency for the SCLK line. */
480 	*fres = (fin / (pre + 1)) >> post;
481 
482 	return (pre << MX51_ECSPI_CTRL_PREDIV_OFFSET) |
483 		(post << MX51_ECSPI_CTRL_POSTDIV_OFFSET);
484 }
485 
mx51_ecspi_intctrl(struct spi_imx_data * spi_imx,int enable)486 static void mx51_ecspi_intctrl(struct spi_imx_data *spi_imx, int enable)
487 {
488 	unsigned int val = 0;
489 
490 	if (enable & MXC_INT_TE)
491 		val |= MX51_ECSPI_INT_TEEN;
492 
493 	if (enable & MXC_INT_RR)
494 		val |= MX51_ECSPI_INT_RREN;
495 
496 	if (enable & MXC_INT_RDR)
497 		val |= MX51_ECSPI_INT_RDREN;
498 
499 	writel(val, spi_imx->base + MX51_ECSPI_INT);
500 }
501 
mx51_ecspi_trigger(struct spi_imx_data * spi_imx)502 static void mx51_ecspi_trigger(struct spi_imx_data *spi_imx)
503 {
504 	u32 reg;
505 
506 	reg = readl(spi_imx->base + MX51_ECSPI_CTRL);
507 	reg |= MX51_ECSPI_CTRL_XCH;
508 	writel(reg, spi_imx->base + MX51_ECSPI_CTRL);
509 }
510 
mx51_disable_dma(struct spi_imx_data * spi_imx)511 static void mx51_disable_dma(struct spi_imx_data *spi_imx)
512 {
513 	writel(0, spi_imx->base + MX51_ECSPI_DMA);
514 }
515 
mx51_ecspi_disable(struct spi_imx_data * spi_imx)516 static void mx51_ecspi_disable(struct spi_imx_data *spi_imx)
517 {
518 	u32 ctrl;
519 
520 	ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
521 	ctrl &= ~MX51_ECSPI_CTRL_ENABLE;
522 	writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
523 }
524 
mx51_ecspi_prepare_message(struct spi_imx_data * spi_imx,struct spi_message * msg)525 static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx,
526 				      struct spi_message *msg)
527 {
528 	struct spi_device *spi = msg->spi;
529 	struct spi_transfer *xfer;
530 	u32 ctrl = MX51_ECSPI_CTRL_ENABLE;
531 	u32 min_speed_hz = ~0U;
532 	u32 testreg, delay;
533 	u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
534 	u32 current_cfg = cfg;
535 
536 	/* set Master or Slave mode */
537 	if (spi_imx->slave_mode)
538 		ctrl &= ~MX51_ECSPI_CTRL_MODE_MASK;
539 	else
540 		ctrl |= MX51_ECSPI_CTRL_MODE_MASK;
541 
542 	/*
543 	 * Enable SPI_RDY handling (falling edge/level triggered).
544 	 */
545 	if (spi->mode & SPI_READY)
546 		ctrl |= MX51_ECSPI_CTRL_DRCTL(spi_imx->spi_drctl);
547 
548 	/* set chip select to use */
549 	ctrl |= MX51_ECSPI_CTRL_CS(spi->chip_select);
550 
551 	/*
552 	 * The ctrl register must be written first, with the EN bit set other
553 	 * registers must not be written to.
554 	 */
555 	writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
556 
557 	testreg = readl(spi_imx->base + MX51_ECSPI_TESTREG);
558 	if (spi->mode & SPI_LOOP)
559 		testreg |= MX51_ECSPI_TESTREG_LBC;
560 	else
561 		testreg &= ~MX51_ECSPI_TESTREG_LBC;
562 	writel(testreg, spi_imx->base + MX51_ECSPI_TESTREG);
563 
564 	/*
565 	 * eCSPI burst completion by Chip Select signal in Slave mode
566 	 * is not functional for imx53 Soc, config SPI burst completed when
567 	 * BURST_LENGTH + 1 bits are received
568 	 */
569 	if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx))
570 		cfg &= ~MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
571 	else
572 		cfg |= MX51_ECSPI_CONFIG_SBBCTRL(spi->chip_select);
573 
574 	if (spi->mode & SPI_CPOL) {
575 		cfg |= MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
576 		cfg |= MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
577 	} else {
578 		cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL(spi->chip_select);
579 		cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(spi->chip_select);
580 	}
581 
582 	if (spi->mode & SPI_CS_HIGH)
583 		cfg |= MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
584 	else
585 		cfg &= ~MX51_ECSPI_CONFIG_SSBPOL(spi->chip_select);
586 
587 	if (cfg == current_cfg)
588 		return 0;
589 
590 	writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
591 
592 	/*
593 	 * Wait until the changes in the configuration register CONFIGREG
594 	 * propagate into the hardware. It takes exactly one tick of the
595 	 * SCLK clock, but we will wait two SCLK clock just to be sure. The
596 	 * effect of the delay it takes for the hardware to apply changes
597 	 * is noticable if the SCLK clock run very slow. In such a case, if
598 	 * the polarity of SCLK should be inverted, the GPIO ChipSelect might
599 	 * be asserted before the SCLK polarity changes, which would disrupt
600 	 * the SPI communication as the device on the other end would consider
601 	 * the change of SCLK polarity as a clock tick already.
602 	 *
603 	 * Because spi_imx->spi_bus_clk is only set in prepare_message
604 	 * callback, iterate over all the transfers in spi_message, find the
605 	 * one with lowest bus frequency, and use that bus frequency for the
606 	 * delay calculation. In case all transfers have speed_hz == 0, then
607 	 * min_speed_hz is ~0 and the resulting delay is zero.
608 	 */
609 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
610 		if (!xfer->speed_hz)
611 			continue;
612 		min_speed_hz = min(xfer->speed_hz, min_speed_hz);
613 	}
614 
615 	delay = (2 * 1000000) / min_speed_hz;
616 	if (likely(delay < 10))	/* SCLK is faster than 200 kHz */
617 		udelay(delay);
618 	else			/* SCLK is _very_ slow */
619 		usleep_range(delay, delay + 10);
620 
621 	return 0;
622 }
623 
mx51_configure_cpha(struct spi_imx_data * spi_imx,struct spi_device * spi)624 static void mx51_configure_cpha(struct spi_imx_data *spi_imx,
625 				struct spi_device *spi)
626 {
627 	bool cpha = (spi->mode & SPI_CPHA);
628 	bool flip_cpha = (spi->mode & SPI_RX_CPHA_FLIP) && spi_imx->rx_only;
629 	u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG);
630 
631 	/* Flip cpha logical value iff flip_cpha */
632 	cpha ^= flip_cpha;
633 
634 	if (cpha)
635 		cfg |= MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
636 	else
637 		cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA(spi->chip_select);
638 
639 	writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG);
640 }
641 
mx51_ecspi_prepare_transfer(struct spi_imx_data * spi_imx,struct spi_device * spi)642 static int mx51_ecspi_prepare_transfer(struct spi_imx_data *spi_imx,
643 				       struct spi_device *spi)
644 {
645 	u32 ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
646 	u32 clk;
647 
648 	/* Clear BL field and set the right value */
649 	ctrl &= ~MX51_ECSPI_CTRL_BL_MASK;
650 	if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx))
651 		ctrl |= (spi_imx->slave_burst * 8 - 1)
652 			<< MX51_ECSPI_CTRL_BL_OFFSET;
653 	else
654 		ctrl |= (spi_imx->bits_per_word - 1)
655 			<< MX51_ECSPI_CTRL_BL_OFFSET;
656 
657 	/* set clock speed */
658 	ctrl &= ~(0xf << MX51_ECSPI_CTRL_POSTDIV_OFFSET |
659 		  0xf << MX51_ECSPI_CTRL_PREDIV_OFFSET);
660 	ctrl |= mx51_ecspi_clkdiv(spi_imx, spi_imx->spi_bus_clk, &clk);
661 	spi_imx->spi_bus_clk = clk;
662 
663 	mx51_configure_cpha(spi_imx, spi);
664 
665 	/*
666 	 * ERR009165: work in XHC mode instead of SMC as PIO on the chips
667 	 * before i.mx6ul.
668 	 */
669 	if (spi_imx->usedma && spi_imx->devtype_data->tx_glitch_fixed)
670 		ctrl |= MX51_ECSPI_CTRL_SMC;
671 	else
672 		ctrl &= ~MX51_ECSPI_CTRL_SMC;
673 
674 	writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
675 
676 	return 0;
677 }
678 
mx51_setup_wml(struct spi_imx_data * spi_imx)679 static void mx51_setup_wml(struct spi_imx_data *spi_imx)
680 {
681 	u32 tx_wml = 0;
682 
683 	if (spi_imx->devtype_data->tx_glitch_fixed)
684 		tx_wml = spi_imx->wml;
685 	/*
686 	 * Configure the DMA register: setup the watermark
687 	 * and enable DMA request.
688 	 */
689 	writel(MX51_ECSPI_DMA_RX_WML(spi_imx->wml - 1) |
690 		MX51_ECSPI_DMA_TX_WML(tx_wml) |
691 		MX51_ECSPI_DMA_RXT_WML(spi_imx->wml) |
692 		MX51_ECSPI_DMA_TEDEN | MX51_ECSPI_DMA_RXDEN |
693 		MX51_ECSPI_DMA_RXTDEN, spi_imx->base + MX51_ECSPI_DMA);
694 }
695 
mx51_ecspi_rx_available(struct spi_imx_data * spi_imx)696 static int mx51_ecspi_rx_available(struct spi_imx_data *spi_imx)
697 {
698 	return readl(spi_imx->base + MX51_ECSPI_STAT) & MX51_ECSPI_STAT_RR;
699 }
700 
mx51_ecspi_reset(struct spi_imx_data * spi_imx)701 static void mx51_ecspi_reset(struct spi_imx_data *spi_imx)
702 {
703 	/* drain receive buffer */
704 	while (mx51_ecspi_rx_available(spi_imx))
705 		readl(spi_imx->base + MXC_CSPIRXDATA);
706 }
707 
708 #define MX31_INTREG_TEEN	(1 << 0)
709 #define MX31_INTREG_RREN	(1 << 3)
710 
711 #define MX31_CSPICTRL_ENABLE	(1 << 0)
712 #define MX31_CSPICTRL_MASTER	(1 << 1)
713 #define MX31_CSPICTRL_XCH	(1 << 2)
714 #define MX31_CSPICTRL_SMC	(1 << 3)
715 #define MX31_CSPICTRL_POL	(1 << 4)
716 #define MX31_CSPICTRL_PHA	(1 << 5)
717 #define MX31_CSPICTRL_SSCTL	(1 << 6)
718 #define MX31_CSPICTRL_SSPOL	(1 << 7)
719 #define MX31_CSPICTRL_BC_SHIFT	8
720 #define MX35_CSPICTRL_BL_SHIFT	20
721 #define MX31_CSPICTRL_CS_SHIFT	24
722 #define MX35_CSPICTRL_CS_SHIFT	12
723 #define MX31_CSPICTRL_DR_SHIFT	16
724 
725 #define MX31_CSPI_DMAREG	0x10
726 #define MX31_DMAREG_RH_DEN	(1<<4)
727 #define MX31_DMAREG_TH_DEN	(1<<1)
728 
729 #define MX31_CSPISTATUS		0x14
730 #define MX31_STATUS_RR		(1 << 3)
731 
732 #define MX31_CSPI_TESTREG	0x1C
733 #define MX31_TEST_LBC		(1 << 14)
734 
735 /* These functions also work for the i.MX35, but be aware that
736  * the i.MX35 has a slightly different register layout for bits
737  * we do not use here.
738  */
mx31_intctrl(struct spi_imx_data * spi_imx,int enable)739 static void mx31_intctrl(struct spi_imx_data *spi_imx, int enable)
740 {
741 	unsigned int val = 0;
742 
743 	if (enable & MXC_INT_TE)
744 		val |= MX31_INTREG_TEEN;
745 	if (enable & MXC_INT_RR)
746 		val |= MX31_INTREG_RREN;
747 
748 	writel(val, spi_imx->base + MXC_CSPIINT);
749 }
750 
mx31_trigger(struct spi_imx_data * spi_imx)751 static void mx31_trigger(struct spi_imx_data *spi_imx)
752 {
753 	unsigned int reg;
754 
755 	reg = readl(spi_imx->base + MXC_CSPICTRL);
756 	reg |= MX31_CSPICTRL_XCH;
757 	writel(reg, spi_imx->base + MXC_CSPICTRL);
758 }
759 
mx31_prepare_message(struct spi_imx_data * spi_imx,struct spi_message * msg)760 static int mx31_prepare_message(struct spi_imx_data *spi_imx,
761 				struct spi_message *msg)
762 {
763 	return 0;
764 }
765 
mx31_prepare_transfer(struct spi_imx_data * spi_imx,struct spi_device * spi)766 static int mx31_prepare_transfer(struct spi_imx_data *spi_imx,
767 				 struct spi_device *spi)
768 {
769 	unsigned int reg = MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_MASTER;
770 	unsigned int clk;
771 
772 	reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->spi_bus_clk, &clk) <<
773 		MX31_CSPICTRL_DR_SHIFT;
774 	spi_imx->spi_bus_clk = clk;
775 
776 	if (is_imx35_cspi(spi_imx)) {
777 		reg |= (spi_imx->bits_per_word - 1) << MX35_CSPICTRL_BL_SHIFT;
778 		reg |= MX31_CSPICTRL_SSCTL;
779 	} else {
780 		reg |= (spi_imx->bits_per_word - 1) << MX31_CSPICTRL_BC_SHIFT;
781 	}
782 
783 	if (spi->mode & SPI_CPHA)
784 		reg |= MX31_CSPICTRL_PHA;
785 	if (spi->mode & SPI_CPOL)
786 		reg |= MX31_CSPICTRL_POL;
787 	if (spi->mode & SPI_CS_HIGH)
788 		reg |= MX31_CSPICTRL_SSPOL;
789 	if (!spi->cs_gpiod)
790 		reg |= (spi->chip_select) <<
791 			(is_imx35_cspi(spi_imx) ? MX35_CSPICTRL_CS_SHIFT :
792 						  MX31_CSPICTRL_CS_SHIFT);
793 
794 	if (spi_imx->usedma)
795 		reg |= MX31_CSPICTRL_SMC;
796 
797 	writel(reg, spi_imx->base + MXC_CSPICTRL);
798 
799 	reg = readl(spi_imx->base + MX31_CSPI_TESTREG);
800 	if (spi->mode & SPI_LOOP)
801 		reg |= MX31_TEST_LBC;
802 	else
803 		reg &= ~MX31_TEST_LBC;
804 	writel(reg, spi_imx->base + MX31_CSPI_TESTREG);
805 
806 	if (spi_imx->usedma) {
807 		/*
808 		 * configure DMA requests when RXFIFO is half full and
809 		 * when TXFIFO is half empty
810 		 */
811 		writel(MX31_DMAREG_RH_DEN | MX31_DMAREG_TH_DEN,
812 			spi_imx->base + MX31_CSPI_DMAREG);
813 	}
814 
815 	return 0;
816 }
817 
mx31_rx_available(struct spi_imx_data * spi_imx)818 static int mx31_rx_available(struct spi_imx_data *spi_imx)
819 {
820 	return readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR;
821 }
822 
mx31_reset(struct spi_imx_data * spi_imx)823 static void mx31_reset(struct spi_imx_data *spi_imx)
824 {
825 	/* drain receive buffer */
826 	while (readl(spi_imx->base + MX31_CSPISTATUS) & MX31_STATUS_RR)
827 		readl(spi_imx->base + MXC_CSPIRXDATA);
828 }
829 
830 #define MX21_INTREG_RR		(1 << 4)
831 #define MX21_INTREG_TEEN	(1 << 9)
832 #define MX21_INTREG_RREN	(1 << 13)
833 
834 #define MX21_CSPICTRL_POL	(1 << 5)
835 #define MX21_CSPICTRL_PHA	(1 << 6)
836 #define MX21_CSPICTRL_SSPOL	(1 << 8)
837 #define MX21_CSPICTRL_XCH	(1 << 9)
838 #define MX21_CSPICTRL_ENABLE	(1 << 10)
839 #define MX21_CSPICTRL_MASTER	(1 << 11)
840 #define MX21_CSPICTRL_DR_SHIFT	14
841 #define MX21_CSPICTRL_CS_SHIFT	19
842 
mx21_intctrl(struct spi_imx_data * spi_imx,int enable)843 static void mx21_intctrl(struct spi_imx_data *spi_imx, int enable)
844 {
845 	unsigned int val = 0;
846 
847 	if (enable & MXC_INT_TE)
848 		val |= MX21_INTREG_TEEN;
849 	if (enable & MXC_INT_RR)
850 		val |= MX21_INTREG_RREN;
851 
852 	writel(val, spi_imx->base + MXC_CSPIINT);
853 }
854 
mx21_trigger(struct spi_imx_data * spi_imx)855 static void mx21_trigger(struct spi_imx_data *spi_imx)
856 {
857 	unsigned int reg;
858 
859 	reg = readl(spi_imx->base + MXC_CSPICTRL);
860 	reg |= MX21_CSPICTRL_XCH;
861 	writel(reg, spi_imx->base + MXC_CSPICTRL);
862 }
863 
mx21_prepare_message(struct spi_imx_data * spi_imx,struct spi_message * msg)864 static int mx21_prepare_message(struct spi_imx_data *spi_imx,
865 				struct spi_message *msg)
866 {
867 	return 0;
868 }
869 
mx21_prepare_transfer(struct spi_imx_data * spi_imx,struct spi_device * spi)870 static int mx21_prepare_transfer(struct spi_imx_data *spi_imx,
871 				 struct spi_device *spi)
872 {
873 	unsigned int reg = MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_MASTER;
874 	unsigned int max = is_imx27_cspi(spi_imx) ? 16 : 18;
875 	unsigned int clk;
876 
877 	reg |= spi_imx_clkdiv_1(spi_imx->spi_clk, spi_imx->spi_bus_clk, max, &clk)
878 		<< MX21_CSPICTRL_DR_SHIFT;
879 	spi_imx->spi_bus_clk = clk;
880 
881 	reg |= spi_imx->bits_per_word - 1;
882 
883 	if (spi->mode & SPI_CPHA)
884 		reg |= MX21_CSPICTRL_PHA;
885 	if (spi->mode & SPI_CPOL)
886 		reg |= MX21_CSPICTRL_POL;
887 	if (spi->mode & SPI_CS_HIGH)
888 		reg |= MX21_CSPICTRL_SSPOL;
889 	if (!spi->cs_gpiod)
890 		reg |= spi->chip_select << MX21_CSPICTRL_CS_SHIFT;
891 
892 	writel(reg, spi_imx->base + MXC_CSPICTRL);
893 
894 	return 0;
895 }
896 
mx21_rx_available(struct spi_imx_data * spi_imx)897 static int mx21_rx_available(struct spi_imx_data *spi_imx)
898 {
899 	return readl(spi_imx->base + MXC_CSPIINT) & MX21_INTREG_RR;
900 }
901 
mx21_reset(struct spi_imx_data * spi_imx)902 static void mx21_reset(struct spi_imx_data *spi_imx)
903 {
904 	writel(1, spi_imx->base + MXC_RESET);
905 }
906 
907 #define MX1_INTREG_RR		(1 << 3)
908 #define MX1_INTREG_TEEN		(1 << 8)
909 #define MX1_INTREG_RREN		(1 << 11)
910 
911 #define MX1_CSPICTRL_POL	(1 << 4)
912 #define MX1_CSPICTRL_PHA	(1 << 5)
913 #define MX1_CSPICTRL_XCH	(1 << 8)
914 #define MX1_CSPICTRL_ENABLE	(1 << 9)
915 #define MX1_CSPICTRL_MASTER	(1 << 10)
916 #define MX1_CSPICTRL_DR_SHIFT	13
917 
mx1_intctrl(struct spi_imx_data * spi_imx,int enable)918 static void mx1_intctrl(struct spi_imx_data *spi_imx, int enable)
919 {
920 	unsigned int val = 0;
921 
922 	if (enable & MXC_INT_TE)
923 		val |= MX1_INTREG_TEEN;
924 	if (enable & MXC_INT_RR)
925 		val |= MX1_INTREG_RREN;
926 
927 	writel(val, spi_imx->base + MXC_CSPIINT);
928 }
929 
mx1_trigger(struct spi_imx_data * spi_imx)930 static void mx1_trigger(struct spi_imx_data *spi_imx)
931 {
932 	unsigned int reg;
933 
934 	reg = readl(spi_imx->base + MXC_CSPICTRL);
935 	reg |= MX1_CSPICTRL_XCH;
936 	writel(reg, spi_imx->base + MXC_CSPICTRL);
937 }
938 
mx1_prepare_message(struct spi_imx_data * spi_imx,struct spi_message * msg)939 static int mx1_prepare_message(struct spi_imx_data *spi_imx,
940 			       struct spi_message *msg)
941 {
942 	return 0;
943 }
944 
mx1_prepare_transfer(struct spi_imx_data * spi_imx,struct spi_device * spi)945 static int mx1_prepare_transfer(struct spi_imx_data *spi_imx,
946 				struct spi_device *spi)
947 {
948 	unsigned int reg = MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_MASTER;
949 	unsigned int clk;
950 
951 	reg |= spi_imx_clkdiv_2(spi_imx->spi_clk, spi_imx->spi_bus_clk, &clk) <<
952 		MX1_CSPICTRL_DR_SHIFT;
953 	spi_imx->spi_bus_clk = clk;
954 
955 	reg |= spi_imx->bits_per_word - 1;
956 
957 	if (spi->mode & SPI_CPHA)
958 		reg |= MX1_CSPICTRL_PHA;
959 	if (spi->mode & SPI_CPOL)
960 		reg |= MX1_CSPICTRL_POL;
961 
962 	writel(reg, spi_imx->base + MXC_CSPICTRL);
963 
964 	return 0;
965 }
966 
mx1_rx_available(struct spi_imx_data * spi_imx)967 static int mx1_rx_available(struct spi_imx_data *spi_imx)
968 {
969 	return readl(spi_imx->base + MXC_CSPIINT) & MX1_INTREG_RR;
970 }
971 
mx1_reset(struct spi_imx_data * spi_imx)972 static void mx1_reset(struct spi_imx_data *spi_imx)
973 {
974 	writel(1, spi_imx->base + MXC_RESET);
975 }
976 
977 static struct spi_imx_devtype_data imx1_cspi_devtype_data = {
978 	.intctrl = mx1_intctrl,
979 	.prepare_message = mx1_prepare_message,
980 	.prepare_transfer = mx1_prepare_transfer,
981 	.trigger = mx1_trigger,
982 	.rx_available = mx1_rx_available,
983 	.reset = mx1_reset,
984 	.fifo_size = 8,
985 	.has_dmamode = false,
986 	.dynamic_burst = false,
987 	.has_slavemode = false,
988 	.devtype = IMX1_CSPI,
989 };
990 
991 static struct spi_imx_devtype_data imx21_cspi_devtype_data = {
992 	.intctrl = mx21_intctrl,
993 	.prepare_message = mx21_prepare_message,
994 	.prepare_transfer = mx21_prepare_transfer,
995 	.trigger = mx21_trigger,
996 	.rx_available = mx21_rx_available,
997 	.reset = mx21_reset,
998 	.fifo_size = 8,
999 	.has_dmamode = false,
1000 	.dynamic_burst = false,
1001 	.has_slavemode = false,
1002 	.devtype = IMX21_CSPI,
1003 };
1004 
1005 static struct spi_imx_devtype_data imx27_cspi_devtype_data = {
1006 	/* i.mx27 cspi shares the functions with i.mx21 one */
1007 	.intctrl = mx21_intctrl,
1008 	.prepare_message = mx21_prepare_message,
1009 	.prepare_transfer = mx21_prepare_transfer,
1010 	.trigger = mx21_trigger,
1011 	.rx_available = mx21_rx_available,
1012 	.reset = mx21_reset,
1013 	.fifo_size = 8,
1014 	.has_dmamode = false,
1015 	.dynamic_burst = false,
1016 	.has_slavemode = false,
1017 	.devtype = IMX27_CSPI,
1018 };
1019 
1020 static struct spi_imx_devtype_data imx31_cspi_devtype_data = {
1021 	.intctrl = mx31_intctrl,
1022 	.prepare_message = mx31_prepare_message,
1023 	.prepare_transfer = mx31_prepare_transfer,
1024 	.trigger = mx31_trigger,
1025 	.rx_available = mx31_rx_available,
1026 	.reset = mx31_reset,
1027 	.fifo_size = 8,
1028 	.has_dmamode = false,
1029 	.dynamic_burst = false,
1030 	.has_slavemode = false,
1031 	.devtype = IMX31_CSPI,
1032 };
1033 
1034 static struct spi_imx_devtype_data imx35_cspi_devtype_data = {
1035 	/* i.mx35 and later cspi shares the functions with i.mx31 one */
1036 	.intctrl = mx31_intctrl,
1037 	.prepare_message = mx31_prepare_message,
1038 	.prepare_transfer = mx31_prepare_transfer,
1039 	.trigger = mx31_trigger,
1040 	.rx_available = mx31_rx_available,
1041 	.reset = mx31_reset,
1042 	.fifo_size = 8,
1043 	.has_dmamode = true,
1044 	.dynamic_burst = false,
1045 	.has_slavemode = false,
1046 	.devtype = IMX35_CSPI,
1047 };
1048 
1049 static struct spi_imx_devtype_data imx51_ecspi_devtype_data = {
1050 	.intctrl = mx51_ecspi_intctrl,
1051 	.prepare_message = mx51_ecspi_prepare_message,
1052 	.prepare_transfer = mx51_ecspi_prepare_transfer,
1053 	.trigger = mx51_ecspi_trigger,
1054 	.rx_available = mx51_ecspi_rx_available,
1055 	.reset = mx51_ecspi_reset,
1056 	.setup_wml = mx51_setup_wml,
1057 	.disable_dma = mx51_disable_dma,
1058 	.fifo_size = 64,
1059 	.has_dmamode = true,
1060 	.dynamic_burst = true,
1061 	.has_slavemode = true,
1062 	.disable = mx51_ecspi_disable,
1063 	.devtype = IMX51_ECSPI,
1064 };
1065 
1066 static struct spi_imx_devtype_data imx53_ecspi_devtype_data = {
1067 	.intctrl = mx51_ecspi_intctrl,
1068 	.prepare_message = mx51_ecspi_prepare_message,
1069 	.prepare_transfer = mx51_ecspi_prepare_transfer,
1070 	.trigger = mx51_ecspi_trigger,
1071 	.rx_available = mx51_ecspi_rx_available,
1072 	.disable_dma = mx51_disable_dma,
1073 	.reset = mx51_ecspi_reset,
1074 	.fifo_size = 64,
1075 	.has_dmamode = true,
1076 	.has_slavemode = true,
1077 	.disable = mx51_ecspi_disable,
1078 	.devtype = IMX53_ECSPI,
1079 };
1080 
1081 static struct spi_imx_devtype_data imx6ul_ecspi_devtype_data = {
1082 	.intctrl = mx51_ecspi_intctrl,
1083 	.prepare_message = mx51_ecspi_prepare_message,
1084 	.prepare_transfer = mx51_ecspi_prepare_transfer,
1085 	.trigger = mx51_ecspi_trigger,
1086 	.rx_available = mx51_ecspi_rx_available,
1087 	.reset = mx51_ecspi_reset,
1088 	.setup_wml = mx51_setup_wml,
1089 	.fifo_size = 64,
1090 	.has_dmamode = true,
1091 	.dynamic_burst = true,
1092 	.has_slavemode = true,
1093 	.tx_glitch_fixed = true,
1094 	.disable = mx51_ecspi_disable,
1095 	.devtype = IMX51_ECSPI,
1096 };
1097 
1098 static const struct of_device_id spi_imx_dt_ids[] = {
1099 	{ .compatible = "fsl,imx1-cspi", .data = &imx1_cspi_devtype_data, },
1100 	{ .compatible = "fsl,imx21-cspi", .data = &imx21_cspi_devtype_data, },
1101 	{ .compatible = "fsl,imx27-cspi", .data = &imx27_cspi_devtype_data, },
1102 	{ .compatible = "fsl,imx31-cspi", .data = &imx31_cspi_devtype_data, },
1103 	{ .compatible = "fsl,imx35-cspi", .data = &imx35_cspi_devtype_data, },
1104 	{ .compatible = "fsl,imx51-ecspi", .data = &imx51_ecspi_devtype_data, },
1105 	{ .compatible = "fsl,imx53-ecspi", .data = &imx53_ecspi_devtype_data, },
1106 	{ .compatible = "fsl,imx6ul-ecspi", .data = &imx6ul_ecspi_devtype_data, },
1107 	{ /* sentinel */ }
1108 };
1109 MODULE_DEVICE_TABLE(of, spi_imx_dt_ids);
1110 
spi_imx_set_burst_len(struct spi_imx_data * spi_imx,int n_bits)1111 static void spi_imx_set_burst_len(struct spi_imx_data *spi_imx, int n_bits)
1112 {
1113 	u32 ctrl;
1114 
1115 	ctrl = readl(spi_imx->base + MX51_ECSPI_CTRL);
1116 	ctrl &= ~MX51_ECSPI_CTRL_BL_MASK;
1117 	ctrl |= ((n_bits - 1) << MX51_ECSPI_CTRL_BL_OFFSET);
1118 	writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL);
1119 }
1120 
spi_imx_push(struct spi_imx_data * spi_imx)1121 static void spi_imx_push(struct spi_imx_data *spi_imx)
1122 {
1123 	unsigned int burst_len;
1124 
1125 	/*
1126 	 * Reload the FIFO when the remaining bytes to be transferred in the
1127 	 * current burst is 0. This only applies when bits_per_word is a
1128 	 * multiple of 8.
1129 	 */
1130 	if (!spi_imx->remainder) {
1131 		if (spi_imx->dynamic_burst) {
1132 
1133 			/* We need to deal unaligned data first */
1134 			burst_len = spi_imx->count % MX51_ECSPI_CTRL_MAX_BURST;
1135 
1136 			if (!burst_len)
1137 				burst_len = MX51_ECSPI_CTRL_MAX_BURST;
1138 
1139 			spi_imx_set_burst_len(spi_imx, burst_len * 8);
1140 
1141 			spi_imx->remainder = burst_len;
1142 		} else {
1143 			spi_imx->remainder = spi_imx_bytes_per_word(spi_imx->bits_per_word);
1144 		}
1145 	}
1146 
1147 	while (spi_imx->txfifo < spi_imx->devtype_data->fifo_size) {
1148 		if (!spi_imx->count)
1149 			break;
1150 		if (spi_imx->dynamic_burst &&
1151 		    spi_imx->txfifo >= DIV_ROUND_UP(spi_imx->remainder, 4))
1152 			break;
1153 		spi_imx->tx(spi_imx);
1154 		spi_imx->txfifo++;
1155 	}
1156 
1157 	if (!spi_imx->slave_mode)
1158 		spi_imx->devtype_data->trigger(spi_imx);
1159 }
1160 
spi_imx_isr(int irq,void * dev_id)1161 static irqreturn_t spi_imx_isr(int irq, void *dev_id)
1162 {
1163 	struct spi_imx_data *spi_imx = dev_id;
1164 
1165 	while (spi_imx->txfifo &&
1166 	       spi_imx->devtype_data->rx_available(spi_imx)) {
1167 		spi_imx->rx(spi_imx);
1168 		spi_imx->txfifo--;
1169 	}
1170 
1171 	if (spi_imx->count) {
1172 		spi_imx_push(spi_imx);
1173 		return IRQ_HANDLED;
1174 	}
1175 
1176 	if (spi_imx->txfifo) {
1177 		/* No data left to push, but still waiting for rx data,
1178 		 * enable receive data available interrupt.
1179 		 */
1180 		spi_imx->devtype_data->intctrl(
1181 				spi_imx, MXC_INT_RR);
1182 		return IRQ_HANDLED;
1183 	}
1184 
1185 	spi_imx->devtype_data->intctrl(spi_imx, 0);
1186 	complete(&spi_imx->xfer_done);
1187 
1188 	return IRQ_HANDLED;
1189 }
1190 
spi_imx_dma_configure(struct spi_controller * controller)1191 static int spi_imx_dma_configure(struct spi_controller *controller)
1192 {
1193 	int ret;
1194 	enum dma_slave_buswidth buswidth;
1195 	struct dma_slave_config rx = {}, tx = {};
1196 	struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
1197 
1198 	switch (spi_imx_bytes_per_word(spi_imx->bits_per_word)) {
1199 	case 4:
1200 		buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
1201 		break;
1202 	case 2:
1203 		buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
1204 		break;
1205 	case 1:
1206 		buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
1207 		break;
1208 	default:
1209 		return -EINVAL;
1210 	}
1211 
1212 	tx.direction = DMA_MEM_TO_DEV;
1213 	tx.dst_addr = spi_imx->base_phys + MXC_CSPITXDATA;
1214 	tx.dst_addr_width = buswidth;
1215 	tx.dst_maxburst = spi_imx->wml;
1216 	ret = dmaengine_slave_config(controller->dma_tx, &tx);
1217 	if (ret) {
1218 		dev_err(spi_imx->dev, "TX dma configuration failed with %d\n", ret);
1219 		return ret;
1220 	}
1221 
1222 	rx.direction = DMA_DEV_TO_MEM;
1223 	rx.src_addr = spi_imx->base_phys + MXC_CSPIRXDATA;
1224 	rx.src_addr_width = buswidth;
1225 	rx.src_maxburst = spi_imx->wml;
1226 	ret = dmaengine_slave_config(controller->dma_rx, &rx);
1227 	if (ret) {
1228 		dev_err(spi_imx->dev, "RX dma configuration failed with %d\n", ret);
1229 		return ret;
1230 	}
1231 
1232 	return 0;
1233 }
1234 
spi_imx_setupxfer(struct spi_device * spi,struct spi_transfer * t)1235 static int spi_imx_setupxfer(struct spi_device *spi,
1236 				 struct spi_transfer *t)
1237 {
1238 	struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
1239 
1240 	if (!t)
1241 		return 0;
1242 
1243 	if (!t->speed_hz) {
1244 		if (!spi->max_speed_hz) {
1245 			dev_err(&spi->dev, "no speed_hz provided!\n");
1246 			return -EINVAL;
1247 		}
1248 		dev_dbg(&spi->dev, "using spi->max_speed_hz!\n");
1249 		spi_imx->spi_bus_clk = spi->max_speed_hz;
1250 	} else
1251 		spi_imx->spi_bus_clk = t->speed_hz;
1252 
1253 	spi_imx->bits_per_word = t->bits_per_word;
1254 
1255 	/*
1256 	 * Initialize the functions for transfer. To transfer non byte-aligned
1257 	 * words, we have to use multiple word-size bursts, we can't use
1258 	 * dynamic_burst in that case.
1259 	 */
1260 	if (spi_imx->devtype_data->dynamic_burst && !spi_imx->slave_mode &&
1261 	    !(spi->mode & SPI_CS_WORD) &&
1262 	    (spi_imx->bits_per_word == 8 ||
1263 	    spi_imx->bits_per_word == 16 ||
1264 	    spi_imx->bits_per_word == 32)) {
1265 
1266 		spi_imx->rx = spi_imx_buf_rx_swap;
1267 		spi_imx->tx = spi_imx_buf_tx_swap;
1268 		spi_imx->dynamic_burst = 1;
1269 
1270 	} else {
1271 		if (spi_imx->bits_per_word <= 8) {
1272 			spi_imx->rx = spi_imx_buf_rx_u8;
1273 			spi_imx->tx = spi_imx_buf_tx_u8;
1274 		} else if (spi_imx->bits_per_word <= 16) {
1275 			spi_imx->rx = spi_imx_buf_rx_u16;
1276 			spi_imx->tx = spi_imx_buf_tx_u16;
1277 		} else {
1278 			spi_imx->rx = spi_imx_buf_rx_u32;
1279 			spi_imx->tx = spi_imx_buf_tx_u32;
1280 		}
1281 		spi_imx->dynamic_burst = 0;
1282 	}
1283 
1284 	if (spi_imx_can_dma(spi_imx->controller, spi, t))
1285 		spi_imx->usedma = true;
1286 	else
1287 		spi_imx->usedma = false;
1288 
1289 	spi_imx->rx_only = ((t->tx_buf == NULL)
1290 			|| (t->tx_buf == spi->controller->dummy_tx));
1291 
1292 	if (is_imx53_ecspi(spi_imx) && spi_imx->slave_mode) {
1293 		spi_imx->rx = mx53_ecspi_rx_slave;
1294 		spi_imx->tx = mx53_ecspi_tx_slave;
1295 		spi_imx->slave_burst = t->len;
1296 	}
1297 
1298 	spi_imx->devtype_data->prepare_transfer(spi_imx, spi);
1299 
1300 	return 0;
1301 }
1302 
spi_imx_sdma_exit(struct spi_imx_data * spi_imx)1303 static void spi_imx_sdma_exit(struct spi_imx_data *spi_imx)
1304 {
1305 	struct spi_controller *controller = spi_imx->controller;
1306 
1307 	if (controller->dma_rx) {
1308 		dma_release_channel(controller->dma_rx);
1309 		controller->dma_rx = NULL;
1310 	}
1311 
1312 	if (controller->dma_tx) {
1313 		dma_release_channel(controller->dma_tx);
1314 		controller->dma_tx = NULL;
1315 	}
1316 }
1317 
spi_imx_sdma_init(struct device * dev,struct spi_imx_data * spi_imx,struct spi_controller * controller)1318 static int spi_imx_sdma_init(struct device *dev, struct spi_imx_data *spi_imx,
1319 			     struct spi_controller *controller)
1320 {
1321 	int ret;
1322 
1323 	spi_imx->wml = spi_imx->devtype_data->fifo_size / 2;
1324 
1325 	/* Prepare for TX DMA: */
1326 	controller->dma_tx = dma_request_chan(dev, "tx");
1327 	if (IS_ERR(controller->dma_tx)) {
1328 		ret = PTR_ERR(controller->dma_tx);
1329 		dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret);
1330 		controller->dma_tx = NULL;
1331 		goto err;
1332 	}
1333 
1334 	/* Prepare for RX : */
1335 	controller->dma_rx = dma_request_chan(dev, "rx");
1336 	if (IS_ERR(controller->dma_rx)) {
1337 		ret = PTR_ERR(controller->dma_rx);
1338 		dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret);
1339 		controller->dma_rx = NULL;
1340 		goto err;
1341 	}
1342 
1343 	init_completion(&spi_imx->dma_rx_completion);
1344 	init_completion(&spi_imx->dma_tx_completion);
1345 	controller->can_dma = spi_imx_can_dma;
1346 	controller->max_dma_len = MAX_SDMA_BD_BYTES;
1347 	spi_imx->controller->flags = SPI_CONTROLLER_MUST_RX |
1348 					 SPI_CONTROLLER_MUST_TX;
1349 
1350 	return 0;
1351 err:
1352 	spi_imx_sdma_exit(spi_imx);
1353 	return ret;
1354 }
1355 
spi_imx_dma_rx_callback(void * cookie)1356 static void spi_imx_dma_rx_callback(void *cookie)
1357 {
1358 	struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
1359 
1360 	complete(&spi_imx->dma_rx_completion);
1361 }
1362 
spi_imx_dma_tx_callback(void * cookie)1363 static void spi_imx_dma_tx_callback(void *cookie)
1364 {
1365 	struct spi_imx_data *spi_imx = (struct spi_imx_data *)cookie;
1366 
1367 	complete(&spi_imx->dma_tx_completion);
1368 }
1369 
spi_imx_calculate_timeout(struct spi_imx_data * spi_imx,int size)1370 static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size)
1371 {
1372 	unsigned long timeout = 0;
1373 
1374 	/* Time with actual data transfer and CS change delay related to HW */
1375 	timeout = (8 + 4) * size / spi_imx->spi_bus_clk;
1376 
1377 	/* Add extra second for scheduler related activities */
1378 	timeout += 1;
1379 
1380 	/* Double calculated timeout */
1381 	return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);
1382 }
1383 
spi_imx_dma_transfer(struct spi_imx_data * spi_imx,struct spi_transfer * transfer)1384 static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx,
1385 				struct spi_transfer *transfer)
1386 {
1387 	struct dma_async_tx_descriptor *desc_tx, *desc_rx;
1388 	unsigned long transfer_timeout;
1389 	unsigned long timeout;
1390 	struct spi_controller *controller = spi_imx->controller;
1391 	struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
1392 	struct scatterlist *last_sg = sg_last(rx->sgl, rx->nents);
1393 	unsigned int bytes_per_word, i;
1394 	int ret;
1395 
1396 	/* Get the right burst length from the last sg to ensure no tail data */
1397 	bytes_per_word = spi_imx_bytes_per_word(transfer->bits_per_word);
1398 	for (i = spi_imx->devtype_data->fifo_size / 2; i > 0; i--) {
1399 		if (!(sg_dma_len(last_sg) % (i * bytes_per_word)))
1400 			break;
1401 	}
1402 	/* Use 1 as wml in case no available burst length got */
1403 	if (i == 0)
1404 		i = 1;
1405 
1406 	spi_imx->wml =  i;
1407 
1408 	ret = spi_imx_dma_configure(controller);
1409 	if (ret)
1410 		goto dma_failure_no_start;
1411 
1412 	if (!spi_imx->devtype_data->setup_wml) {
1413 		dev_err(spi_imx->dev, "No setup_wml()?\n");
1414 		ret = -EINVAL;
1415 		goto dma_failure_no_start;
1416 	}
1417 	spi_imx->devtype_data->setup_wml(spi_imx);
1418 
1419 	/*
1420 	 * The TX DMA setup starts the transfer, so make sure RX is configured
1421 	 * before TX.
1422 	 */
1423 	desc_rx = dmaengine_prep_slave_sg(controller->dma_rx,
1424 				rx->sgl, rx->nents, DMA_DEV_TO_MEM,
1425 				DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1426 	if (!desc_rx) {
1427 		ret = -EINVAL;
1428 		goto dma_failure_no_start;
1429 	}
1430 
1431 	desc_rx->callback = spi_imx_dma_rx_callback;
1432 	desc_rx->callback_param = (void *)spi_imx;
1433 	dmaengine_submit(desc_rx);
1434 	reinit_completion(&spi_imx->dma_rx_completion);
1435 	dma_async_issue_pending(controller->dma_rx);
1436 
1437 	desc_tx = dmaengine_prep_slave_sg(controller->dma_tx,
1438 				tx->sgl, tx->nents, DMA_MEM_TO_DEV,
1439 				DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1440 	if (!desc_tx) {
1441 		dmaengine_terminate_all(controller->dma_tx);
1442 		dmaengine_terminate_all(controller->dma_rx);
1443 		return -EINVAL;
1444 	}
1445 
1446 	desc_tx->callback = spi_imx_dma_tx_callback;
1447 	desc_tx->callback_param = (void *)spi_imx;
1448 	dmaengine_submit(desc_tx);
1449 	reinit_completion(&spi_imx->dma_tx_completion);
1450 	dma_async_issue_pending(controller->dma_tx);
1451 
1452 	transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1453 
1454 	/* Wait SDMA to finish the data transfer.*/
1455 	timeout = wait_for_completion_timeout(&spi_imx->dma_tx_completion,
1456 						transfer_timeout);
1457 	if (!timeout) {
1458 		dev_err(spi_imx->dev, "I/O Error in DMA TX\n");
1459 		dmaengine_terminate_all(controller->dma_tx);
1460 		dmaengine_terminate_all(controller->dma_rx);
1461 		return -ETIMEDOUT;
1462 	}
1463 
1464 	timeout = wait_for_completion_timeout(&spi_imx->dma_rx_completion,
1465 					      transfer_timeout);
1466 	if (!timeout) {
1467 		dev_err(&controller->dev, "I/O Error in DMA RX\n");
1468 		spi_imx->devtype_data->reset(spi_imx);
1469 		dmaengine_terminate_all(controller->dma_rx);
1470 		return -ETIMEDOUT;
1471 	}
1472 
1473 	return 0;
1474 /* fallback to pio */
1475 dma_failure_no_start:
1476 	transfer->error |= SPI_TRANS_FAIL_NO_START;
1477 	return ret;
1478 }
1479 
spi_imx_pio_transfer(struct spi_device * spi,struct spi_transfer * transfer)1480 static int spi_imx_pio_transfer(struct spi_device *spi,
1481 				struct spi_transfer *transfer)
1482 {
1483 	struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
1484 	unsigned long transfer_timeout;
1485 	unsigned long timeout;
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;
1491 	spi_imx->remainder = 0;
1492 
1493 	reinit_completion(&spi_imx->xfer_done);
1494 
1495 	spi_imx_push(spi_imx);
1496 
1497 	spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE);
1498 
1499 	transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len);
1500 
1501 	timeout = wait_for_completion_timeout(&spi_imx->xfer_done,
1502 					      transfer_timeout);
1503 	if (!timeout) {
1504 		dev_err(&spi->dev, "I/O Error in PIO\n");
1505 		spi_imx->devtype_data->reset(spi_imx);
1506 		return -ETIMEDOUT;
1507 	}
1508 
1509 	return 0;
1510 }
1511 
spi_imx_poll_transfer(struct spi_device * spi,struct spi_transfer * transfer)1512 static int spi_imx_poll_transfer(struct spi_device *spi,
1513 				 struct spi_transfer *transfer)
1514 {
1515 	struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
1516 	unsigned long timeout;
1517 
1518 	spi_imx->tx_buf = transfer->tx_buf;
1519 	spi_imx->rx_buf = transfer->rx_buf;
1520 	spi_imx->count = transfer->len;
1521 	spi_imx->txfifo = 0;
1522 	spi_imx->remainder = 0;
1523 
1524 	/* fill in the fifo before timeout calculations if we are
1525 	 * interrupted here, then the data is getting transferred by
1526 	 * the HW while we are interrupted
1527 	 */
1528 	spi_imx_push(spi_imx);
1529 
1530 	timeout = spi_imx_calculate_timeout(spi_imx, transfer->len) + jiffies;
1531 	while (spi_imx->txfifo) {
1532 		/* RX */
1533 		while (spi_imx->txfifo &&
1534 		       spi_imx->devtype_data->rx_available(spi_imx)) {
1535 			spi_imx->rx(spi_imx);
1536 			spi_imx->txfifo--;
1537 		}
1538 
1539 		/* TX */
1540 		if (spi_imx->count) {
1541 			spi_imx_push(spi_imx);
1542 			continue;
1543 		}
1544 
1545 		if (spi_imx->txfifo &&
1546 		    time_after(jiffies, timeout)) {
1547 
1548 			dev_err_ratelimited(&spi->dev,
1549 					    "timeout period reached: jiffies: %lu- falling back to interrupt mode\n",
1550 					    jiffies - timeout);
1551 
1552 			/* fall back to interrupt mode */
1553 			return spi_imx_pio_transfer(spi, transfer);
1554 		}
1555 	}
1556 
1557 	return 0;
1558 }
1559 
spi_imx_pio_transfer_slave(struct spi_device * spi,struct spi_transfer * transfer)1560 static int spi_imx_pio_transfer_slave(struct spi_device *spi,
1561 				      struct spi_transfer *transfer)
1562 {
1563 	struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
1564 	int ret = 0;
1565 
1566 	if (is_imx53_ecspi(spi_imx) &&
1567 	    transfer->len > MX53_MAX_TRANSFER_BYTES) {
1568 		dev_err(&spi->dev, "Transaction too big, max size is %d bytes\n",
1569 			MX53_MAX_TRANSFER_BYTES);
1570 		return -EMSGSIZE;
1571 	}
1572 
1573 	spi_imx->tx_buf = transfer->tx_buf;
1574 	spi_imx->rx_buf = transfer->rx_buf;
1575 	spi_imx->count = transfer->len;
1576 	spi_imx->txfifo = 0;
1577 	spi_imx->remainder = 0;
1578 
1579 	reinit_completion(&spi_imx->xfer_done);
1580 	spi_imx->slave_aborted = false;
1581 
1582 	spi_imx_push(spi_imx);
1583 
1584 	spi_imx->devtype_data->intctrl(spi_imx, MXC_INT_TE | MXC_INT_RDR);
1585 
1586 	if (wait_for_completion_interruptible(&spi_imx->xfer_done) ||
1587 	    spi_imx->slave_aborted) {
1588 		dev_dbg(&spi->dev, "interrupted\n");
1589 		ret = -EINTR;
1590 	}
1591 
1592 	/* ecspi has a HW issue when works in Slave mode,
1593 	 * after 64 words writtern to TXFIFO, even TXFIFO becomes empty,
1594 	 * ECSPI_TXDATA keeps shift out the last word data,
1595 	 * so we have to disable ECSPI when in slave mode after the
1596 	 * transfer completes
1597 	 */
1598 	if (spi_imx->devtype_data->disable)
1599 		spi_imx->devtype_data->disable(spi_imx);
1600 
1601 	return ret;
1602 }
1603 
spi_imx_transfer_one(struct spi_controller * controller,struct spi_device * spi,struct spi_transfer * transfer)1604 static int spi_imx_transfer_one(struct spi_controller *controller,
1605 				struct spi_device *spi,
1606 				struct spi_transfer *transfer)
1607 {
1608 	struct spi_imx_data *spi_imx = spi_controller_get_devdata(spi->controller);
1609 	unsigned long hz_per_byte, byte_limit;
1610 
1611 	spi_imx_setupxfer(spi, transfer);
1612 	transfer->effective_speed_hz = spi_imx->spi_bus_clk;
1613 
1614 	/* flush rxfifo before transfer */
1615 	while (spi_imx->devtype_data->rx_available(spi_imx))
1616 		readl(spi_imx->base + MXC_CSPIRXDATA);
1617 
1618 	if (spi_imx->slave_mode)
1619 		return spi_imx_pio_transfer_slave(spi, transfer);
1620 
1621 	/*
1622 	 * If we decided in spi_imx_can_dma() that we want to do a DMA
1623 	 * transfer, the SPI transfer has already been mapped, so we
1624 	 * have to do the DMA transfer here.
1625 	 */
1626 	if (spi_imx->usedma)
1627 		return spi_imx_dma_transfer(spi_imx, transfer);
1628 	/*
1629 	 * Calculate the estimated time in us the transfer runs. Find
1630 	 * the number of Hz per byte per polling limit.
1631 	 */
1632 	hz_per_byte = polling_limit_us ? ((8 + 4) * USEC_PER_SEC) / polling_limit_us : 0;
1633 	byte_limit = hz_per_byte ? transfer->effective_speed_hz / hz_per_byte : 1;
1634 
1635 	/* run in polling mode for short transfers */
1636 	if (transfer->len < byte_limit)
1637 		return spi_imx_poll_transfer(spi, transfer);
1638 
1639 	return spi_imx_pio_transfer(spi, transfer);
1640 }
1641 
spi_imx_setup(struct spi_device * spi)1642 static int spi_imx_setup(struct spi_device *spi)
1643 {
1644 	dev_dbg(&spi->dev, "%s: mode %d, %u bpw, %d hz\n", __func__,
1645 		 spi->mode, spi->bits_per_word, spi->max_speed_hz);
1646 
1647 	return 0;
1648 }
1649 
spi_imx_cleanup(struct spi_device * spi)1650 static void spi_imx_cleanup(struct spi_device *spi)
1651 {
1652 }
1653 
1654 static int
spi_imx_prepare_message(struct spi_controller * controller,struct spi_message * msg)1655 spi_imx_prepare_message(struct spi_controller *controller, struct spi_message *msg)
1656 {
1657 	struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
1658 	int ret;
1659 
1660 	ret = pm_runtime_resume_and_get(spi_imx->dev);
1661 	if (ret < 0) {
1662 		dev_err(spi_imx->dev, "failed to enable clock\n");
1663 		return ret;
1664 	}
1665 
1666 	ret = spi_imx->devtype_data->prepare_message(spi_imx, msg);
1667 	if (ret) {
1668 		pm_runtime_mark_last_busy(spi_imx->dev);
1669 		pm_runtime_put_autosuspend(spi_imx->dev);
1670 	}
1671 
1672 	return ret;
1673 }
1674 
1675 static int
spi_imx_unprepare_message(struct spi_controller * controller,struct spi_message * msg)1676 spi_imx_unprepare_message(struct spi_controller *controller, struct spi_message *msg)
1677 {
1678 	struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
1679 
1680 	pm_runtime_mark_last_busy(spi_imx->dev);
1681 	pm_runtime_put_autosuspend(spi_imx->dev);
1682 	return 0;
1683 }
1684 
spi_imx_slave_abort(struct spi_controller * controller)1685 static int spi_imx_slave_abort(struct spi_controller *controller)
1686 {
1687 	struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
1688 
1689 	spi_imx->slave_aborted = true;
1690 	complete(&spi_imx->xfer_done);
1691 
1692 	return 0;
1693 }
1694 
spi_imx_probe(struct platform_device * pdev)1695 static int spi_imx_probe(struct platform_device *pdev)
1696 {
1697 	struct device_node *np = pdev->dev.of_node;
1698 	struct spi_controller *controller;
1699 	struct spi_imx_data *spi_imx;
1700 	struct resource *res;
1701 	int ret, irq, spi_drctl;
1702 	const struct spi_imx_devtype_data *devtype_data =
1703 			of_device_get_match_data(&pdev->dev);
1704 	bool slave_mode;
1705 	u32 val;
1706 
1707 	slave_mode = devtype_data->has_slavemode &&
1708 			of_property_read_bool(np, "spi-slave");
1709 	if (slave_mode)
1710 		controller = spi_alloc_slave(&pdev->dev,
1711 					     sizeof(struct spi_imx_data));
1712 	else
1713 		controller = spi_alloc_master(&pdev->dev,
1714 					      sizeof(struct spi_imx_data));
1715 	if (!controller)
1716 		return -ENOMEM;
1717 
1718 	ret = of_property_read_u32(np, "fsl,spi-rdy-drctl", &spi_drctl);
1719 	if ((ret < 0) || (spi_drctl >= 0x3)) {
1720 		/* '11' is reserved */
1721 		spi_drctl = 0;
1722 	}
1723 
1724 	platform_set_drvdata(pdev, controller);
1725 
1726 	controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
1727 	controller->bus_num = np ? -1 : pdev->id;
1728 	controller->use_gpio_descriptors = true;
1729 
1730 	spi_imx = spi_controller_get_devdata(controller);
1731 	spi_imx->controller = controller;
1732 	spi_imx->dev = &pdev->dev;
1733 	spi_imx->slave_mode = slave_mode;
1734 
1735 	spi_imx->devtype_data = devtype_data;
1736 
1737 	/*
1738 	 * Get number of chip selects from device properties. This can be
1739 	 * coming from device tree or boardfiles, if it is not defined,
1740 	 * a default value of 3 chip selects will be used, as all the legacy
1741 	 * board files have <= 3 chip selects.
1742 	 */
1743 	if (!device_property_read_u32(&pdev->dev, "num-cs", &val))
1744 		controller->num_chipselect = val;
1745 	else
1746 		controller->num_chipselect = 3;
1747 
1748 	spi_imx->controller->transfer_one = spi_imx_transfer_one;
1749 	spi_imx->controller->setup = spi_imx_setup;
1750 	spi_imx->controller->cleanup = spi_imx_cleanup;
1751 	spi_imx->controller->prepare_message = spi_imx_prepare_message;
1752 	spi_imx->controller->unprepare_message = spi_imx_unprepare_message;
1753 	spi_imx->controller->slave_abort = spi_imx_slave_abort;
1754 	spi_imx->controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_NO_CS;
1755 
1756 	if (is_imx35_cspi(spi_imx) || is_imx51_ecspi(spi_imx) ||
1757 	    is_imx53_ecspi(spi_imx))
1758 		spi_imx->controller->mode_bits |= SPI_LOOP | SPI_READY;
1759 
1760 	if (is_imx51_ecspi(spi_imx) || is_imx53_ecspi(spi_imx))
1761 		spi_imx->controller->mode_bits |= SPI_RX_CPHA_FLIP;
1762 
1763 	if (is_imx51_ecspi(spi_imx) &&
1764 	    device_property_read_u32(&pdev->dev, "cs-gpios", NULL))
1765 		/*
1766 		 * When using HW-CS implementing SPI_CS_WORD can be done by just
1767 		 * setting the burst length to the word size. This is
1768 		 * considerably faster than manually controlling the CS.
1769 		 */
1770 		spi_imx->controller->mode_bits |= SPI_CS_WORD;
1771 
1772 	spi_imx->spi_drctl = spi_drctl;
1773 
1774 	init_completion(&spi_imx->xfer_done);
1775 
1776 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1777 	spi_imx->base = devm_ioremap_resource(&pdev->dev, res);
1778 	if (IS_ERR(spi_imx->base)) {
1779 		ret = PTR_ERR(spi_imx->base);
1780 		goto out_controller_put;
1781 	}
1782 	spi_imx->base_phys = res->start;
1783 
1784 	irq = platform_get_irq(pdev, 0);
1785 	if (irq < 0) {
1786 		ret = irq;
1787 		goto out_controller_put;
1788 	}
1789 
1790 	ret = devm_request_irq(&pdev->dev, irq, spi_imx_isr, 0,
1791 			       dev_name(&pdev->dev), spi_imx);
1792 	if (ret) {
1793 		dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
1794 		goto out_controller_put;
1795 	}
1796 
1797 	spi_imx->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
1798 	if (IS_ERR(spi_imx->clk_ipg)) {
1799 		ret = PTR_ERR(spi_imx->clk_ipg);
1800 		goto out_controller_put;
1801 	}
1802 
1803 	spi_imx->clk_per = devm_clk_get(&pdev->dev, "per");
1804 	if (IS_ERR(spi_imx->clk_per)) {
1805 		ret = PTR_ERR(spi_imx->clk_per);
1806 		goto out_controller_put;
1807 	}
1808 
1809 	ret = clk_prepare_enable(spi_imx->clk_per);
1810 	if (ret)
1811 		goto out_controller_put;
1812 
1813 	ret = clk_prepare_enable(spi_imx->clk_ipg);
1814 	if (ret)
1815 		goto out_put_per;
1816 
1817 	pm_runtime_set_autosuspend_delay(spi_imx->dev, MXC_RPM_TIMEOUT);
1818 	pm_runtime_use_autosuspend(spi_imx->dev);
1819 	pm_runtime_get_noresume(spi_imx->dev);
1820 	pm_runtime_set_active(spi_imx->dev);
1821 	pm_runtime_enable(spi_imx->dev);
1822 
1823 	spi_imx->spi_clk = clk_get_rate(spi_imx->clk_per);
1824 	/*
1825 	 * Only validated on i.mx35 and i.mx6 now, can remove the constraint
1826 	 * if validated on other chips.
1827 	 */
1828 	if (spi_imx->devtype_data->has_dmamode) {
1829 		ret = spi_imx_sdma_init(&pdev->dev, spi_imx, controller);
1830 		if (ret == -EPROBE_DEFER)
1831 			goto out_runtime_pm_put;
1832 
1833 		if (ret < 0)
1834 			dev_dbg(&pdev->dev, "dma setup error %d, use pio\n",
1835 				ret);
1836 	}
1837 
1838 	spi_imx->devtype_data->reset(spi_imx);
1839 
1840 	spi_imx->devtype_data->intctrl(spi_imx, 0);
1841 
1842 	controller->dev.of_node = pdev->dev.of_node;
1843 	ret = spi_register_controller(controller);
1844 	if (ret) {
1845 		dev_err_probe(&pdev->dev, ret, "register controller failed\n");
1846 		goto out_register_controller;
1847 	}
1848 
1849 	pm_runtime_mark_last_busy(spi_imx->dev);
1850 	pm_runtime_put_autosuspend(spi_imx->dev);
1851 
1852 	return ret;
1853 
1854 out_register_controller:
1855 	if (spi_imx->devtype_data->has_dmamode)
1856 		spi_imx_sdma_exit(spi_imx);
1857 out_runtime_pm_put:
1858 	pm_runtime_dont_use_autosuspend(spi_imx->dev);
1859 	pm_runtime_set_suspended(&pdev->dev);
1860 	pm_runtime_disable(spi_imx->dev);
1861 
1862 	clk_disable_unprepare(spi_imx->clk_ipg);
1863 out_put_per:
1864 	clk_disable_unprepare(spi_imx->clk_per);
1865 out_controller_put:
1866 	spi_controller_put(controller);
1867 
1868 	return ret;
1869 }
1870 
spi_imx_remove(struct platform_device * pdev)1871 static int spi_imx_remove(struct platform_device *pdev)
1872 {
1873 	struct spi_controller *controller = platform_get_drvdata(pdev);
1874 	struct spi_imx_data *spi_imx = spi_controller_get_devdata(controller);
1875 	int ret;
1876 
1877 	spi_unregister_controller(controller);
1878 
1879 	ret = pm_runtime_get_sync(spi_imx->dev);
1880 	if (ret >= 0)
1881 		writel(0, spi_imx->base + MXC_CSPICTRL);
1882 	else
1883 		dev_warn(spi_imx->dev, "failed to enable clock, skip hw disable\n");
1884 
1885 	pm_runtime_dont_use_autosuspend(spi_imx->dev);
1886 	pm_runtime_put_sync(spi_imx->dev);
1887 	pm_runtime_disable(spi_imx->dev);
1888 
1889 	spi_imx_sdma_exit(spi_imx);
1890 
1891 	return 0;
1892 }
1893 
spi_imx_runtime_resume(struct device * dev)1894 static int __maybe_unused spi_imx_runtime_resume(struct device *dev)
1895 {
1896 	struct spi_controller *controller = dev_get_drvdata(dev);
1897 	struct spi_imx_data *spi_imx;
1898 	int ret;
1899 
1900 	spi_imx = spi_controller_get_devdata(controller);
1901 
1902 	ret = clk_prepare_enable(spi_imx->clk_per);
1903 	if (ret)
1904 		return ret;
1905 
1906 	ret = clk_prepare_enable(spi_imx->clk_ipg);
1907 	if (ret) {
1908 		clk_disable_unprepare(spi_imx->clk_per);
1909 		return ret;
1910 	}
1911 
1912 	return 0;
1913 }
1914 
spi_imx_runtime_suspend(struct device * dev)1915 static int __maybe_unused spi_imx_runtime_suspend(struct device *dev)
1916 {
1917 	struct spi_controller *controller = dev_get_drvdata(dev);
1918 	struct spi_imx_data *spi_imx;
1919 
1920 	spi_imx = spi_controller_get_devdata(controller);
1921 
1922 	clk_disable_unprepare(spi_imx->clk_per);
1923 	clk_disable_unprepare(spi_imx->clk_ipg);
1924 
1925 	return 0;
1926 }
1927 
spi_imx_suspend(struct device * dev)1928 static int __maybe_unused spi_imx_suspend(struct device *dev)
1929 {
1930 	pinctrl_pm_select_sleep_state(dev);
1931 	return 0;
1932 }
1933 
spi_imx_resume(struct device * dev)1934 static int __maybe_unused spi_imx_resume(struct device *dev)
1935 {
1936 	pinctrl_pm_select_default_state(dev);
1937 	return 0;
1938 }
1939 
1940 static const struct dev_pm_ops imx_spi_pm = {
1941 	SET_RUNTIME_PM_OPS(spi_imx_runtime_suspend,
1942 				spi_imx_runtime_resume, NULL)
1943 	SET_SYSTEM_SLEEP_PM_OPS(spi_imx_suspend, spi_imx_resume)
1944 };
1945 
1946 static struct platform_driver spi_imx_driver = {
1947 	.driver = {
1948 		   .name = DRIVER_NAME,
1949 		   .of_match_table = spi_imx_dt_ids,
1950 		   .pm = &imx_spi_pm,
1951 	},
1952 	.probe = spi_imx_probe,
1953 	.remove = spi_imx_remove,
1954 };
1955 module_platform_driver(spi_imx_driver);
1956 
1957 MODULE_DESCRIPTION("i.MX SPI Controller driver");
1958 MODULE_AUTHOR("Sascha Hauer, Pengutronix");
1959 MODULE_LICENSE("GPL");
1960 MODULE_ALIAS("platform:" DRIVER_NAME);
1961