• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Designware SPI core controller driver (refer pxa2xx_spi.c)
4  *
5  * Copyright (c) 2009, Intel Corporation.
6  */
7 
8 #include <linux/dma-mapping.h>
9 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/preempt.h>
12 #include <linux/highmem.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/spi/spi.h>
16 #include <linux/spi/spi-mem.h>
17 #include <linux/string.h>
18 #include <linux/of.h>
19 
20 #include "spi-dw.h"
21 
22 #ifdef CONFIG_DEBUG_FS
23 #include <linux/debugfs.h>
24 #endif
25 
26 /* Slave spi_device related */
27 struct chip_data {
28 	u32 cr0;
29 	u32 rx_sample_dly;	/* RX sample delay */
30 };
31 
32 #ifdef CONFIG_DEBUG_FS
33 
34 #define DW_SPI_DBGFS_REG(_name, _off)	\
35 {					\
36 	.name = _name,			\
37 	.offset = _off,			\
38 }
39 
40 static const struct debugfs_reg32 dw_spi_dbgfs_regs[] = {
41 	DW_SPI_DBGFS_REG("CTRLR0", DW_SPI_CTRLR0),
42 	DW_SPI_DBGFS_REG("CTRLR1", DW_SPI_CTRLR1),
43 	DW_SPI_DBGFS_REG("SSIENR", DW_SPI_SSIENR),
44 	DW_SPI_DBGFS_REG("SER", DW_SPI_SER),
45 	DW_SPI_DBGFS_REG("BAUDR", DW_SPI_BAUDR),
46 	DW_SPI_DBGFS_REG("TXFTLR", DW_SPI_TXFTLR),
47 	DW_SPI_DBGFS_REG("RXFTLR", DW_SPI_RXFTLR),
48 	DW_SPI_DBGFS_REG("TXFLR", DW_SPI_TXFLR),
49 	DW_SPI_DBGFS_REG("RXFLR", DW_SPI_RXFLR),
50 	DW_SPI_DBGFS_REG("SR", DW_SPI_SR),
51 	DW_SPI_DBGFS_REG("IMR", DW_SPI_IMR),
52 	DW_SPI_DBGFS_REG("ISR", DW_SPI_ISR),
53 	DW_SPI_DBGFS_REG("DMACR", DW_SPI_DMACR),
54 	DW_SPI_DBGFS_REG("DMATDLR", DW_SPI_DMATDLR),
55 	DW_SPI_DBGFS_REG("DMARDLR", DW_SPI_DMARDLR),
56 	DW_SPI_DBGFS_REG("RX_SAMPLE_DLY", DW_SPI_RX_SAMPLE_DLY),
57 };
58 
dw_spi_debugfs_init(struct dw_spi * dws)59 static int dw_spi_debugfs_init(struct dw_spi *dws)
60 {
61 	char name[32];
62 
63 	snprintf(name, 32, "dw_spi%d", dws->master->bus_num);
64 	dws->debugfs = debugfs_create_dir(name, NULL);
65 	if (!dws->debugfs)
66 		return -ENOMEM;
67 
68 	dws->regset.regs = dw_spi_dbgfs_regs;
69 	dws->regset.nregs = ARRAY_SIZE(dw_spi_dbgfs_regs);
70 	dws->regset.base = dws->regs;
71 	debugfs_create_regset32("registers", 0400, dws->debugfs, &dws->regset);
72 
73 	return 0;
74 }
75 
dw_spi_debugfs_remove(struct dw_spi * dws)76 static void dw_spi_debugfs_remove(struct dw_spi *dws)
77 {
78 	debugfs_remove_recursive(dws->debugfs);
79 }
80 
81 #else
dw_spi_debugfs_init(struct dw_spi * dws)82 static inline int dw_spi_debugfs_init(struct dw_spi *dws)
83 {
84 	return 0;
85 }
86 
dw_spi_debugfs_remove(struct dw_spi * dws)87 static inline void dw_spi_debugfs_remove(struct dw_spi *dws)
88 {
89 }
90 #endif /* CONFIG_DEBUG_FS */
91 
dw_spi_set_cs(struct spi_device * spi,bool enable)92 void dw_spi_set_cs(struct spi_device *spi, bool enable)
93 {
94 	struct dw_spi *dws = spi_controller_get_devdata(spi->controller);
95 	bool cs_high = !!(spi->mode & SPI_CS_HIGH);
96 
97 	/*
98 	 * DW SPI controller demands any native CS being set in order to
99 	 * proceed with data transfer. So in order to activate the SPI
100 	 * communications we must set a corresponding bit in the Slave
101 	 * Enable register no matter whether the SPI core is configured to
102 	 * support active-high or active-low CS level.
103 	 */
104 	if (cs_high == enable)
105 		dw_writel(dws, DW_SPI_SER, BIT(spi->chip_select));
106 	else
107 		dw_writel(dws, DW_SPI_SER, 0);
108 }
109 EXPORT_SYMBOL_GPL(dw_spi_set_cs);
110 
111 /* Return the max entries we can fill into tx fifo */
tx_max(struct dw_spi * dws)112 static inline u32 tx_max(struct dw_spi *dws)
113 {
114 	u32 tx_room, rxtx_gap;
115 
116 	tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR);
117 
118 	/*
119 	 * Another concern is about the tx/rx mismatch, we
120 	 * though to use (dws->fifo_len - rxflr - txflr) as
121 	 * one maximum value for tx, but it doesn't cover the
122 	 * data which is out of tx/rx fifo and inside the
123 	 * shift registers. So a control from sw point of
124 	 * view is taken.
125 	 */
126 	rxtx_gap = dws->fifo_len - (dws->rx_len - dws->tx_len);
127 
128 	return min3((u32)dws->tx_len, tx_room, rxtx_gap);
129 }
130 
131 /* Return the max entries we should read out of rx fifo */
rx_max(struct dw_spi * dws)132 static inline u32 rx_max(struct dw_spi *dws)
133 {
134 	return min_t(u32, dws->rx_len, dw_readl(dws, DW_SPI_RXFLR));
135 }
136 
dw_writer(struct dw_spi * dws)137 static void dw_writer(struct dw_spi *dws)
138 {
139 	u32 max = tx_max(dws);
140 	u32 txw = 0;
141 
142 	while (max--) {
143 		if (dws->tx) {
144 			if (dws->n_bytes == 1)
145 				txw = *(u8 *)(dws->tx);
146 			else if (dws->n_bytes == 2)
147 				txw = *(u16 *)(dws->tx);
148 			else
149 				txw = *(u32 *)(dws->tx);
150 
151 			dws->tx += dws->n_bytes;
152 		}
153 		dw_write_io_reg(dws, DW_SPI_DR, txw);
154 		--dws->tx_len;
155 	}
156 }
157 
dw_reader(struct dw_spi * dws)158 static void dw_reader(struct dw_spi *dws)
159 {
160 	u32 max = rx_max(dws);
161 	u32 rxw;
162 
163 	while (max--) {
164 		rxw = dw_read_io_reg(dws, DW_SPI_DR);
165 		if (dws->rx) {
166 			if (dws->n_bytes == 1)
167 				*(u8 *)(dws->rx) = rxw;
168 			else if (dws->n_bytes == 2)
169 				*(u16 *)(dws->rx) = rxw;
170 			else
171 				*(u32 *)(dws->rx) = rxw;
172 
173 			dws->rx += dws->n_bytes;
174 		}
175 		--dws->rx_len;
176 	}
177 }
178 
dw_spi_check_status(struct dw_spi * dws,bool raw)179 int dw_spi_check_status(struct dw_spi *dws, bool raw)
180 {
181 	u32 irq_status;
182 	int ret = 0;
183 
184 	if (raw)
185 		irq_status = dw_readl(dws, DW_SPI_RISR);
186 	else
187 		irq_status = dw_readl(dws, DW_SPI_ISR);
188 
189 	if (irq_status & SPI_INT_RXOI) {
190 		dev_err(&dws->master->dev, "RX FIFO overflow detected\n");
191 		ret = -EIO;
192 	}
193 
194 	if (irq_status & SPI_INT_RXUI) {
195 		dev_err(&dws->master->dev, "RX FIFO underflow detected\n");
196 		ret = -EIO;
197 	}
198 
199 	if (irq_status & SPI_INT_TXOI) {
200 		dev_err(&dws->master->dev, "TX FIFO overflow detected\n");
201 		ret = -EIO;
202 	}
203 
204 	/* Generically handle the erroneous situation */
205 	if (ret) {
206 		spi_reset_chip(dws);
207 		if (dws->master->cur_msg)
208 			dws->master->cur_msg->status = ret;
209 	}
210 
211 	return ret;
212 }
213 EXPORT_SYMBOL_GPL(dw_spi_check_status);
214 
dw_spi_transfer_handler(struct dw_spi * dws)215 static irqreturn_t dw_spi_transfer_handler(struct dw_spi *dws)
216 {
217 	u16 irq_status = dw_readl(dws, DW_SPI_ISR);
218 
219 	if (dw_spi_check_status(dws, false)) {
220 		spi_finalize_current_transfer(dws->master);
221 		return IRQ_HANDLED;
222 	}
223 
224 	/*
225 	 * Read data from the Rx FIFO every time we've got a chance executing
226 	 * this method. If there is nothing left to receive, terminate the
227 	 * procedure. Otherwise adjust the Rx FIFO Threshold level if it's a
228 	 * final stage of the transfer. By doing so we'll get the next IRQ
229 	 * right when the leftover incoming data is received.
230 	 */
231 	dw_reader(dws);
232 	if (!dws->rx_len) {
233 		spi_mask_intr(dws, 0xff);
234 		spi_finalize_current_transfer(dws->master);
235 	} else if (dws->rx_len <= dw_readl(dws, DW_SPI_RXFTLR)) {
236 		dw_writel(dws, DW_SPI_RXFTLR, dws->rx_len - 1);
237 	}
238 
239 	/*
240 	 * Send data out if Tx FIFO Empty IRQ is received. The IRQ will be
241 	 * disabled after the data transmission is finished so not to
242 	 * have the TXE IRQ flood at the final stage of the transfer.
243 	 */
244 	if (irq_status & SPI_INT_TXEI) {
245 		dw_writer(dws);
246 		if (!dws->tx_len)
247 			spi_mask_intr(dws, SPI_INT_TXEI);
248 	}
249 
250 	return IRQ_HANDLED;
251 }
252 
dw_spi_irq(int irq,void * dev_id)253 static irqreturn_t dw_spi_irq(int irq, void *dev_id)
254 {
255 	struct spi_controller *master = dev_id;
256 	struct dw_spi *dws = spi_controller_get_devdata(master);
257 	u16 irq_status = dw_readl(dws, DW_SPI_ISR) & 0x3f;
258 
259 	if (!irq_status)
260 		return IRQ_NONE;
261 
262 	if (!master->cur_msg) {
263 		spi_mask_intr(dws, 0xff);
264 		return IRQ_HANDLED;
265 	}
266 
267 	return dws->transfer_handler(dws);
268 }
269 
dw_spi_prepare_cr0(struct dw_spi * dws,struct spi_device * spi)270 static u32 dw_spi_prepare_cr0(struct dw_spi *dws, struct spi_device *spi)
271 {
272 	u32 cr0 = 0;
273 
274 	if (!(dws->caps & DW_SPI_CAP_DWC_SSI)) {
275 		/* CTRLR0[ 5: 4] Frame Format */
276 		cr0 |= SSI_MOTO_SPI << SPI_FRF_OFFSET;
277 
278 		/*
279 		 * SPI mode (SCPOL|SCPH)
280 		 * CTRLR0[ 6] Serial Clock Phase
281 		 * CTRLR0[ 7] Serial Clock Polarity
282 		 */
283 		cr0 |= ((spi->mode & SPI_CPOL) ? 1 : 0) << SPI_SCOL_OFFSET;
284 		cr0 |= ((spi->mode & SPI_CPHA) ? 1 : 0) << SPI_SCPH_OFFSET;
285 
286 		/* CTRLR0[11] Shift Register Loop */
287 		cr0 |= ((spi->mode & SPI_LOOP) ? 1 : 0) << SPI_SRL_OFFSET;
288 	} else {
289 		/* CTRLR0[ 7: 6] Frame Format */
290 		cr0 |= SSI_MOTO_SPI << DWC_SSI_CTRLR0_FRF_OFFSET;
291 
292 		/*
293 		 * SPI mode (SCPOL|SCPH)
294 		 * CTRLR0[ 8] Serial Clock Phase
295 		 * CTRLR0[ 9] Serial Clock Polarity
296 		 */
297 		cr0 |= ((spi->mode & SPI_CPOL) ? 1 : 0) << DWC_SSI_CTRLR0_SCPOL_OFFSET;
298 		cr0 |= ((spi->mode & SPI_CPHA) ? 1 : 0) << DWC_SSI_CTRLR0_SCPH_OFFSET;
299 
300 		/* CTRLR0[13] Shift Register Loop */
301 		cr0 |= ((spi->mode & SPI_LOOP) ? 1 : 0) << DWC_SSI_CTRLR0_SRL_OFFSET;
302 
303 		if (dws->caps & DW_SPI_CAP_KEEMBAY_MST)
304 			cr0 |= DWC_SSI_CTRLR0_KEEMBAY_MST;
305 	}
306 
307 	return cr0;
308 }
309 
dw_spi_update_config(struct dw_spi * dws,struct spi_device * spi,struct dw_spi_cfg * cfg)310 void dw_spi_update_config(struct dw_spi *dws, struct spi_device *spi,
311 			  struct dw_spi_cfg *cfg)
312 {
313 	struct chip_data *chip = spi_get_ctldata(spi);
314 	u32 cr0 = chip->cr0;
315 	u32 speed_hz;
316 	u16 clk_div;
317 
318 	/* CTRLR0[ 4/3: 0] or CTRLR0[ 20: 16] Data Frame Size */
319 	cr0 |= (cfg->dfs - 1) << dws->dfs_offset;
320 
321 	if (!(dws->caps & DW_SPI_CAP_DWC_SSI))
322 		/* CTRLR0[ 9:8] Transfer Mode */
323 		cr0 |= cfg->tmode << SPI_TMOD_OFFSET;
324 	else
325 		/* CTRLR0[11:10] Transfer Mode */
326 		cr0 |= cfg->tmode << DWC_SSI_CTRLR0_TMOD_OFFSET;
327 
328 	dw_writel(dws, DW_SPI_CTRLR0, cr0);
329 
330 	if (cfg->tmode == SPI_TMOD_EPROMREAD || cfg->tmode == SPI_TMOD_RO)
331 		dw_writel(dws, DW_SPI_CTRLR1, cfg->ndf ? cfg->ndf - 1 : 0);
332 
333 	/* Note DW APB SSI clock divider doesn't support odd numbers */
334 	clk_div = (DIV_ROUND_UP(dws->max_freq, cfg->freq) + 1) & 0xfffe;
335 	speed_hz = dws->max_freq / clk_div;
336 
337 	if (dws->current_freq != speed_hz) {
338 		spi_set_clk(dws, clk_div);
339 		dws->current_freq = speed_hz;
340 	}
341 
342 	/* Update RX sample delay if required */
343 	if (dws->cur_rx_sample_dly != chip->rx_sample_dly) {
344 		dw_writel(dws, DW_SPI_RX_SAMPLE_DLY, chip->rx_sample_dly);
345 		dws->cur_rx_sample_dly = chip->rx_sample_dly;
346 	}
347 }
348 EXPORT_SYMBOL_GPL(dw_spi_update_config);
349 
dw_spi_irq_setup(struct dw_spi * dws)350 static void dw_spi_irq_setup(struct dw_spi *dws)
351 {
352 	u16 level;
353 	u8 imask;
354 
355 	/*
356 	 * Originally Tx and Rx data lengths match. Rx FIFO Threshold level
357 	 * will be adjusted at the final stage of the IRQ-based SPI transfer
358 	 * execution so not to lose the leftover of the incoming data.
359 	 */
360 	level = min_t(unsigned int, dws->fifo_len / 2, dws->tx_len);
361 	dw_writel(dws, DW_SPI_TXFTLR, level);
362 	dw_writel(dws, DW_SPI_RXFTLR, level - 1);
363 
364 	dws->transfer_handler = dw_spi_transfer_handler;
365 
366 	imask = SPI_INT_TXEI | SPI_INT_TXOI | SPI_INT_RXUI | SPI_INT_RXOI |
367 		SPI_INT_RXFI;
368 	spi_umask_intr(dws, imask);
369 }
370 
371 /*
372  * The iterative procedure of the poll-based transfer is simple: write as much
373  * as possible to the Tx FIFO, wait until the pending to receive data is ready
374  * to be read, read it from the Rx FIFO and check whether the performed
375  * procedure has been successful.
376  *
377  * Note this method the same way as the IRQ-based transfer won't work well for
378  * the SPI devices connected to the controller with native CS due to the
379  * automatic CS assertion/de-assertion.
380  */
dw_spi_poll_transfer(struct dw_spi * dws,struct spi_transfer * transfer)381 static int dw_spi_poll_transfer(struct dw_spi *dws,
382 				struct spi_transfer *transfer)
383 {
384 	struct spi_delay delay;
385 	u16 nbits;
386 	int ret;
387 
388 	delay.unit = SPI_DELAY_UNIT_SCK;
389 	nbits = dws->n_bytes * BITS_PER_BYTE;
390 
391 	do {
392 		dw_writer(dws);
393 
394 		delay.value = nbits * (dws->rx_len - dws->tx_len);
395 		spi_delay_exec(&delay, transfer);
396 
397 		dw_reader(dws);
398 
399 		ret = dw_spi_check_status(dws, true);
400 		if (ret)
401 			return ret;
402 	} while (dws->rx_len);
403 
404 	return 0;
405 }
406 
dw_spi_transfer_one(struct spi_controller * master,struct spi_device * spi,struct spi_transfer * transfer)407 static int dw_spi_transfer_one(struct spi_controller *master,
408 		struct spi_device *spi, struct spi_transfer *transfer)
409 {
410 	struct dw_spi *dws = spi_controller_get_devdata(master);
411 	struct dw_spi_cfg cfg = {
412 		.tmode = SPI_TMOD_TR,
413 		.dfs = transfer->bits_per_word,
414 		.freq = transfer->speed_hz,
415 	};
416 	int ret;
417 
418 	dws->dma_mapped = 0;
419 	dws->n_bytes =
420 		roundup_pow_of_two(DIV_ROUND_UP(transfer->bits_per_word,
421 						BITS_PER_BYTE));
422 
423 	dws->tx = (void *)transfer->tx_buf;
424 	dws->tx_len = transfer->len / dws->n_bytes;
425 	dws->rx = transfer->rx_buf;
426 	dws->rx_len = dws->tx_len;
427 
428 	/* Ensure the data above is visible for all CPUs */
429 	smp_mb();
430 
431 	spi_enable_chip(dws, 0);
432 
433 	dw_spi_update_config(dws, spi, &cfg);
434 
435 	transfer->effective_speed_hz = dws->current_freq;
436 
437 	/* Check if current transfer is a DMA transaction */
438 	if (master->can_dma && master->can_dma(master, spi, transfer))
439 		dws->dma_mapped = master->cur_msg_mapped;
440 
441 	/* For poll mode just disable all interrupts */
442 	spi_mask_intr(dws, 0xff);
443 
444 	if (dws->dma_mapped) {
445 		ret = dws->dma_ops->dma_setup(dws, transfer);
446 		if (ret)
447 			return ret;
448 	}
449 
450 	spi_enable_chip(dws, 1);
451 
452 	if (dws->dma_mapped)
453 		return dws->dma_ops->dma_transfer(dws, transfer);
454 	else if (dws->irq == IRQ_NOTCONNECTED)
455 		return dw_spi_poll_transfer(dws, transfer);
456 
457 	dw_spi_irq_setup(dws);
458 
459 	return 1;
460 }
461 
dw_spi_handle_err(struct spi_controller * master,struct spi_message * msg)462 static void dw_spi_handle_err(struct spi_controller *master,
463 		struct spi_message *msg)
464 {
465 	struct dw_spi *dws = spi_controller_get_devdata(master);
466 
467 	if (dws->dma_mapped)
468 		dws->dma_ops->dma_stop(dws);
469 
470 	spi_reset_chip(dws);
471 }
472 
dw_spi_adjust_mem_op_size(struct spi_mem * mem,struct spi_mem_op * op)473 static int dw_spi_adjust_mem_op_size(struct spi_mem *mem, struct spi_mem_op *op)
474 {
475 	if (op->data.dir == SPI_MEM_DATA_IN)
476 		op->data.nbytes = clamp_val(op->data.nbytes, 0, SPI_NDF_MASK + 1);
477 
478 	return 0;
479 }
480 
dw_spi_supports_mem_op(struct spi_mem * mem,const struct spi_mem_op * op)481 static bool dw_spi_supports_mem_op(struct spi_mem *mem,
482 				   const struct spi_mem_op *op)
483 {
484 	if (op->data.buswidth > 1 || op->addr.buswidth > 1 ||
485 	    op->dummy.buswidth > 1 || op->cmd.buswidth > 1)
486 		return false;
487 
488 	return spi_mem_default_supports_op(mem, op);
489 }
490 
dw_spi_init_mem_buf(struct dw_spi * dws,const struct spi_mem_op * op)491 static int dw_spi_init_mem_buf(struct dw_spi *dws, const struct spi_mem_op *op)
492 {
493 	unsigned int i, j, len;
494 	u8 *out;
495 
496 	/*
497 	 * Calculate the total length of the EEPROM command transfer and
498 	 * either use the pre-allocated buffer or create a temporary one.
499 	 */
500 	len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
501 	if (op->data.dir == SPI_MEM_DATA_OUT)
502 		len += op->data.nbytes;
503 
504 	if (len <= SPI_BUF_SIZE) {
505 		out = dws->buf;
506 	} else {
507 		out = kzalloc(len, GFP_KERNEL);
508 		if (!out)
509 			return -ENOMEM;
510 	}
511 
512 	/*
513 	 * Collect the operation code, address and dummy bytes into the single
514 	 * buffer. If it's a transfer with data to be sent, also copy it into the
515 	 * single buffer in order to speed the data transmission up.
516 	 */
517 	for (i = 0; i < op->cmd.nbytes; ++i)
518 		out[i] = SPI_GET_BYTE(op->cmd.opcode, op->cmd.nbytes - i - 1);
519 	for (j = 0; j < op->addr.nbytes; ++i, ++j)
520 		out[i] = SPI_GET_BYTE(op->addr.val, op->addr.nbytes - j - 1);
521 	for (j = 0; j < op->dummy.nbytes; ++i, ++j)
522 		out[i] = 0x0;
523 
524 	if (op->data.dir == SPI_MEM_DATA_OUT)
525 		memcpy(&out[i], op->data.buf.out, op->data.nbytes);
526 
527 	dws->n_bytes = 1;
528 	dws->tx = out;
529 	dws->tx_len = len;
530 	if (op->data.dir == SPI_MEM_DATA_IN) {
531 		dws->rx = op->data.buf.in;
532 		dws->rx_len = op->data.nbytes;
533 	} else {
534 		dws->rx = NULL;
535 		dws->rx_len = 0;
536 	}
537 
538 	return 0;
539 }
540 
dw_spi_free_mem_buf(struct dw_spi * dws)541 static void dw_spi_free_mem_buf(struct dw_spi *dws)
542 {
543 	if (dws->tx != dws->buf)
544 		kfree(dws->tx);
545 }
546 
dw_spi_write_then_read(struct dw_spi * dws,struct spi_device * spi)547 static int dw_spi_write_then_read(struct dw_spi *dws, struct spi_device *spi)
548 {
549 	u32 room, entries, sts;
550 	unsigned int len;
551 	u8 *buf;
552 
553 	/*
554 	 * At initial stage we just pre-fill the Tx FIFO in with no rush,
555 	 * since native CS hasn't been enabled yet and the automatic data
556 	 * transmission won't start til we do that.
557 	 */
558 	len = min(dws->fifo_len, dws->tx_len);
559 	buf = dws->tx;
560 	while (len--)
561 		dw_write_io_reg(dws, DW_SPI_DR, *buf++);
562 
563 	/*
564 	 * After setting any bit in the SER register the transmission will
565 	 * start automatically. We have to keep up with that procedure
566 	 * otherwise the CS de-assertion will happen whereupon the memory
567 	 * operation will be pre-terminated.
568 	 */
569 	len = dws->tx_len - ((void *)buf - dws->tx);
570 	dw_spi_set_cs(spi, false);
571 	while (len) {
572 		entries = readl_relaxed(dws->regs + DW_SPI_TXFLR);
573 		if (!entries) {
574 			dev_err(&dws->master->dev, "CS de-assertion on Tx\n");
575 			return -EIO;
576 		}
577 		room = min(dws->fifo_len - entries, len);
578 		for (; room; --room, --len)
579 			dw_write_io_reg(dws, DW_SPI_DR, *buf++);
580 	}
581 
582 	/*
583 	 * Data fetching will start automatically if the EEPROM-read mode is
584 	 * activated. We have to keep up with the incoming data pace to
585 	 * prevent the Rx FIFO overflow causing the inbound data loss.
586 	 */
587 	len = dws->rx_len;
588 	buf = dws->rx;
589 	while (len) {
590 		entries = readl_relaxed(dws->regs + DW_SPI_RXFLR);
591 		if (!entries) {
592 			sts = readl_relaxed(dws->regs + DW_SPI_RISR);
593 			if (sts & SPI_INT_RXOI) {
594 				dev_err(&dws->master->dev, "FIFO overflow on Rx\n");
595 				return -EIO;
596 			}
597 			continue;
598 		}
599 		entries = min(entries, len);
600 		for (; entries; --entries, --len)
601 			*buf++ = dw_read_io_reg(dws, DW_SPI_DR);
602 	}
603 
604 	return 0;
605 }
606 
dw_spi_ctlr_busy(struct dw_spi * dws)607 static inline bool dw_spi_ctlr_busy(struct dw_spi *dws)
608 {
609 	return dw_readl(dws, DW_SPI_SR) & SR_BUSY;
610 }
611 
dw_spi_wait_mem_op_done(struct dw_spi * dws)612 static int dw_spi_wait_mem_op_done(struct dw_spi *dws)
613 {
614 	int retry = SPI_WAIT_RETRIES;
615 	struct spi_delay delay;
616 	unsigned long ns, us;
617 	u32 nents;
618 
619 	nents = dw_readl(dws, DW_SPI_TXFLR);
620 	ns = NSEC_PER_SEC / dws->current_freq * nents;
621 	ns *= dws->n_bytes * BITS_PER_BYTE;
622 	if (ns <= NSEC_PER_USEC) {
623 		delay.unit = SPI_DELAY_UNIT_NSECS;
624 		delay.value = ns;
625 	} else {
626 		us = DIV_ROUND_UP(ns, NSEC_PER_USEC);
627 		delay.unit = SPI_DELAY_UNIT_USECS;
628 		delay.value = clamp_val(us, 0, USHRT_MAX);
629 	}
630 
631 	while (dw_spi_ctlr_busy(dws) && retry--)
632 		spi_delay_exec(&delay, NULL);
633 
634 	if (retry < 0) {
635 		dev_err(&dws->master->dev, "Mem op hanged up\n");
636 		return -EIO;
637 	}
638 
639 	return 0;
640 }
641 
dw_spi_stop_mem_op(struct dw_spi * dws,struct spi_device * spi)642 static void dw_spi_stop_mem_op(struct dw_spi *dws, struct spi_device *spi)
643 {
644 	spi_enable_chip(dws, 0);
645 	dw_spi_set_cs(spi, true);
646 	spi_enable_chip(dws, 1);
647 }
648 
649 /*
650  * The SPI memory operation implementation below is the best choice for the
651  * devices, which are selected by the native chip-select lane. It's
652  * specifically developed to workaround the problem with automatic chip-select
653  * lane toggle when there is no data in the Tx FIFO buffer. Luckily the current
654  * SPI-mem core calls exec_op() callback only if the GPIO-based CS is
655  * unavailable.
656  */
dw_spi_exec_mem_op(struct spi_mem * mem,const struct spi_mem_op * op)657 static int dw_spi_exec_mem_op(struct spi_mem *mem, const struct spi_mem_op *op)
658 {
659 	struct dw_spi *dws = spi_controller_get_devdata(mem->spi->controller);
660 	struct dw_spi_cfg cfg;
661 	unsigned long flags;
662 	int ret;
663 
664 	/*
665 	 * Collect the outbound data into a single buffer to speed the
666 	 * transmission up at least on the initial stage.
667 	 */
668 	ret = dw_spi_init_mem_buf(dws, op);
669 	if (ret)
670 		return ret;
671 
672 	/*
673 	 * DW SPI EEPROM-read mode is required only for the SPI memory Data-IN
674 	 * operation. Transmit-only mode is suitable for the rest of them.
675 	 */
676 	cfg.dfs = 8;
677 	cfg.freq = clamp(mem->spi->max_speed_hz, 0U, dws->max_mem_freq);
678 	if (op->data.dir == SPI_MEM_DATA_IN) {
679 		cfg.tmode = SPI_TMOD_EPROMREAD;
680 		cfg.ndf = op->data.nbytes;
681 	} else {
682 		cfg.tmode = SPI_TMOD_TO;
683 	}
684 
685 	spi_enable_chip(dws, 0);
686 
687 	dw_spi_update_config(dws, mem->spi, &cfg);
688 
689 	spi_mask_intr(dws, 0xff);
690 
691 	spi_enable_chip(dws, 1);
692 
693 	/*
694 	 * DW APB SSI controller has very nasty peculiarities. First originally
695 	 * (without any vendor-specific modifications) it doesn't provide a
696 	 * direct way to set and clear the native chip-select signal. Instead
697 	 * the controller asserts the CS lane if Tx FIFO isn't empty and a
698 	 * transmission is going on, and automatically de-asserts it back to
699 	 * the high level if the Tx FIFO doesn't have anything to be pushed
700 	 * out. Due to that a multi-tasking or heavy IRQs activity might be
701 	 * fatal, since the transfer procedure preemption may cause the Tx FIFO
702 	 * getting empty and sudden CS de-assertion, which in the middle of the
703 	 * transfer will most likely cause the data loss. Secondly the
704 	 * EEPROM-read or Read-only DW SPI transfer modes imply the incoming
705 	 * data being automatically pulled in into the Rx FIFO. So if the
706 	 * driver software is late in fetching the data from the FIFO before
707 	 * it's overflown, new incoming data will be lost. In order to make
708 	 * sure the executed memory operations are CS-atomic and to prevent the
709 	 * Rx FIFO overflow we have to disable the local interrupts so to block
710 	 * any preemption during the subsequent IO operations.
711 	 *
712 	 * Note. At some circumstances disabling IRQs may not help to prevent
713 	 * the problems described above. The CS de-assertion and Rx FIFO
714 	 * overflow may still happen due to the relatively slow system bus or
715 	 * CPU not working fast enough, so the write-then-read algo implemented
716 	 * here just won't keep up with the SPI bus data transfer. Such
717 	 * situation is highly platform specific and is supposed to be fixed by
718 	 * manually restricting the SPI bus frequency using the
719 	 * dws->max_mem_freq parameter.
720 	 */
721 	local_irq_save(flags);
722 	preempt_disable();
723 
724 	ret = dw_spi_write_then_read(dws, mem->spi);
725 
726 	local_irq_restore(flags);
727 	preempt_enable();
728 
729 	/*
730 	 * Wait for the operation being finished and check the controller
731 	 * status only if there hasn't been any run-time error detected. In the
732 	 * former case it's just pointless. In the later one to prevent an
733 	 * additional error message printing since any hw error flag being set
734 	 * would be due to an error detected on the data transfer.
735 	 */
736 	if (!ret) {
737 		ret = dw_spi_wait_mem_op_done(dws);
738 		if (!ret)
739 			ret = dw_spi_check_status(dws, true);
740 	}
741 
742 	dw_spi_stop_mem_op(dws, mem->spi);
743 
744 	dw_spi_free_mem_buf(dws);
745 
746 	return ret;
747 }
748 
749 /*
750  * Initialize the default memory operations if a glue layer hasn't specified
751  * custom ones. Direct mapping operations will be preserved anyway since DW SPI
752  * controller doesn't have an embedded dirmap interface. Note the memory
753  * operations implemented in this driver is the best choice only for the DW APB
754  * SSI controller with standard native CS functionality. If a hardware vendor
755  * has fixed the automatic CS assertion/de-assertion peculiarity, then it will
756  * be safer to use the normal SPI-messages-based transfers implementation.
757  */
dw_spi_init_mem_ops(struct dw_spi * dws)758 static void dw_spi_init_mem_ops(struct dw_spi *dws)
759 {
760 	if (!dws->mem_ops.exec_op && !(dws->caps & DW_SPI_CAP_CS_OVERRIDE) &&
761 	    !dws->set_cs) {
762 		dws->mem_ops.adjust_op_size = dw_spi_adjust_mem_op_size;
763 		dws->mem_ops.supports_op = dw_spi_supports_mem_op;
764 		dws->mem_ops.exec_op = dw_spi_exec_mem_op;
765 		if (!dws->max_mem_freq)
766 			dws->max_mem_freq = dws->max_freq;
767 	}
768 }
769 
770 /* This may be called twice for each spi dev */
dw_spi_setup(struct spi_device * spi)771 static int dw_spi_setup(struct spi_device *spi)
772 {
773 	struct dw_spi *dws = spi_controller_get_devdata(spi->controller);
774 	struct chip_data *chip;
775 
776 	/* Only alloc on first setup */
777 	chip = spi_get_ctldata(spi);
778 	if (!chip) {
779 		struct dw_spi *dws = spi_controller_get_devdata(spi->controller);
780 		u32 rx_sample_dly_ns;
781 
782 		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
783 		if (!chip)
784 			return -ENOMEM;
785 		spi_set_ctldata(spi, chip);
786 		/* Get specific / default rx-sample-delay */
787 		if (device_property_read_u32(&spi->dev,
788 					     "rx-sample-delay-ns",
789 					     &rx_sample_dly_ns) != 0)
790 			/* Use default controller value */
791 			rx_sample_dly_ns = dws->def_rx_sample_dly_ns;
792 		chip->rx_sample_dly = DIV_ROUND_CLOSEST(rx_sample_dly_ns,
793 							NSEC_PER_SEC /
794 							dws->max_freq);
795 	}
796 
797 	/*
798 	 * Update CR0 data each time the setup callback is invoked since
799 	 * the device parameters could have been changed, for instance, by
800 	 * the MMC SPI driver or something else.
801 	 */
802 	chip->cr0 = dw_spi_prepare_cr0(dws, spi);
803 
804 	return 0;
805 }
806 
dw_spi_cleanup(struct spi_device * spi)807 static void dw_spi_cleanup(struct spi_device *spi)
808 {
809 	struct chip_data *chip = spi_get_ctldata(spi);
810 
811 	kfree(chip);
812 	spi_set_ctldata(spi, NULL);
813 }
814 
815 /* Restart the controller, disable all interrupts, clean rx fifo */
spi_hw_init(struct device * dev,struct dw_spi * dws)816 static void spi_hw_init(struct device *dev, struct dw_spi *dws)
817 {
818 	spi_reset_chip(dws);
819 
820 	/*
821 	 * Try to detect the FIFO depth if not set by interface driver,
822 	 * the depth could be from 2 to 256 from HW spec
823 	 */
824 	if (!dws->fifo_len) {
825 		u32 fifo;
826 
827 		for (fifo = 1; fifo < 256; fifo++) {
828 			dw_writel(dws, DW_SPI_TXFTLR, fifo);
829 			if (fifo != dw_readl(dws, DW_SPI_TXFTLR))
830 				break;
831 		}
832 		dw_writel(dws, DW_SPI_TXFTLR, 0);
833 
834 		dws->fifo_len = (fifo == 1) ? 0 : fifo;
835 		dev_dbg(dev, "Detected FIFO size: %u bytes\n", dws->fifo_len);
836 	}
837 
838 	/*
839 	 * Detect CTRLR0.DFS field size and offset by testing the lowest bits
840 	 * writability. Note DWC SSI controller also has the extended DFS, but
841 	 * with zero offset.
842 	 */
843 	if (!(dws->caps & DW_SPI_CAP_DWC_SSI)) {
844 		u32 cr0, tmp = dw_readl(dws, DW_SPI_CTRLR0);
845 
846 		spi_enable_chip(dws, 0);
847 		dw_writel(dws, DW_SPI_CTRLR0, 0xffffffff);
848 		cr0 = dw_readl(dws, DW_SPI_CTRLR0);
849 		dw_writel(dws, DW_SPI_CTRLR0, tmp);
850 		spi_enable_chip(dws, 1);
851 
852 		if (!(cr0 & SPI_DFS_MASK)) {
853 			dws->caps |= DW_SPI_CAP_DFS32;
854 			dws->dfs_offset = SPI_DFS32_OFFSET;
855 			dev_dbg(dev, "Detected 32-bits max data frame size\n");
856 		}
857 	} else {
858 		dws->caps |= DW_SPI_CAP_DFS32;
859 	}
860 
861 	/* enable HW fixup for explicit CS deselect for Amazon's alpine chip */
862 	if (dws->caps & DW_SPI_CAP_CS_OVERRIDE)
863 		dw_writel(dws, DW_SPI_CS_OVERRIDE, 0xF);
864 }
865 
dw_spi_add_host(struct device * dev,struct dw_spi * dws)866 int dw_spi_add_host(struct device *dev, struct dw_spi *dws)
867 {
868 	struct spi_controller *master;
869 	int ret;
870 
871 	if (!dws)
872 		return -EINVAL;
873 
874 	master = spi_alloc_master(dev, 0);
875 	if (!master)
876 		return -ENOMEM;
877 
878 	dws->master = master;
879 	dws->dma_addr = (dma_addr_t)(dws->paddr + DW_SPI_DR);
880 
881 	spi_controller_set_devdata(master, dws);
882 
883 	/* Basic HW init */
884 	spi_hw_init(dev, dws);
885 
886 	ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED, dev_name(dev),
887 			  master);
888 	if (ret < 0 && ret != -ENOTCONN) {
889 		dev_err(dev, "can not get IRQ\n");
890 		goto err_free_master;
891 	}
892 
893 	dw_spi_init_mem_ops(dws);
894 
895 	master->use_gpio_descriptors = true;
896 	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
897 	if (dws->caps & DW_SPI_CAP_DFS32)
898 		master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
899 	else
900 		master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
901 	master->bus_num = dws->bus_num;
902 	master->num_chipselect = dws->num_cs;
903 	master->setup = dw_spi_setup;
904 	master->cleanup = dw_spi_cleanup;
905 	if (dws->set_cs)
906 		master->set_cs = dws->set_cs;
907 	else
908 		master->set_cs = dw_spi_set_cs;
909 	master->transfer_one = dw_spi_transfer_one;
910 	master->handle_err = dw_spi_handle_err;
911 	if (dws->mem_ops.exec_op)
912 		master->mem_ops = &dws->mem_ops;
913 	master->max_speed_hz = dws->max_freq;
914 	master->dev.of_node = dev->of_node;
915 	master->dev.fwnode = dev->fwnode;
916 	master->flags = SPI_MASTER_GPIO_SS;
917 	master->auto_runtime_pm = true;
918 
919 	/* Get default rx sample delay */
920 	device_property_read_u32(dev, "rx-sample-delay-ns",
921 				 &dws->def_rx_sample_dly_ns);
922 
923 	if (dws->dma_ops && dws->dma_ops->dma_init) {
924 		ret = dws->dma_ops->dma_init(dev, dws);
925 		if (ret) {
926 			dev_warn(dev, "DMA init failed\n");
927 		} else {
928 			master->can_dma = dws->dma_ops->can_dma;
929 			master->flags |= SPI_CONTROLLER_MUST_TX;
930 		}
931 	}
932 
933 	ret = spi_register_controller(master);
934 	if (ret) {
935 		dev_err(&master->dev, "problem registering spi master\n");
936 		goto err_dma_exit;
937 	}
938 
939 	dw_spi_debugfs_init(dws);
940 	return 0;
941 
942 err_dma_exit:
943 	if (dws->dma_ops && dws->dma_ops->dma_exit)
944 		dws->dma_ops->dma_exit(dws);
945 	spi_enable_chip(dws, 0);
946 	free_irq(dws->irq, master);
947 err_free_master:
948 	spi_controller_put(master);
949 	return ret;
950 }
951 EXPORT_SYMBOL_GPL(dw_spi_add_host);
952 
dw_spi_remove_host(struct dw_spi * dws)953 void dw_spi_remove_host(struct dw_spi *dws)
954 {
955 	dw_spi_debugfs_remove(dws);
956 
957 	spi_unregister_controller(dws->master);
958 
959 	if (dws->dma_ops && dws->dma_ops->dma_exit)
960 		dws->dma_ops->dma_exit(dws);
961 
962 	spi_shutdown_chip(dws);
963 
964 	free_irq(dws->irq, dws->master);
965 }
966 EXPORT_SYMBOL_GPL(dw_spi_remove_host);
967 
dw_spi_suspend_host(struct dw_spi * dws)968 int dw_spi_suspend_host(struct dw_spi *dws)
969 {
970 	int ret;
971 
972 	ret = spi_controller_suspend(dws->master);
973 	if (ret)
974 		return ret;
975 
976 	spi_shutdown_chip(dws);
977 	return 0;
978 }
979 EXPORT_SYMBOL_GPL(dw_spi_suspend_host);
980 
dw_spi_resume_host(struct dw_spi * dws)981 int dw_spi_resume_host(struct dw_spi *dws)
982 {
983 	spi_hw_init(&dws->master->dev, dws);
984 	return spi_controller_resume(dws->master);
985 }
986 EXPORT_SYMBOL_GPL(dw_spi_resume_host);
987 
988 MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
989 MODULE_DESCRIPTION("Driver for DesignWare SPI controller core");
990 MODULE_LICENSE("GPL v2");
991