spi-fsl-dspi.c 28.6 KB
Newer Older
1 2 3 4 5 6
// SPDX-License-Identifier: GPL-2.0+
//
// Copyright 2013 Freescale Semiconductor, Inc.
//
// Freescale DSPI driver
// This file contains a driver for the Freescale DSPI
7

8 9
#include <linux/clk.h>
#include <linux/delay.h>
10 11
#include <linux/dmaengine.h>
#include <linux/dma-mapping.h>
12 13 14 15
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/io.h>
16
#include <linux/kernel.h>
17
#include <linux/math64.h>
18
#include <linux/module.h>
19 20
#include <linux/of.h>
#include <linux/of_device.h>
21
#include <linux/pinctrl/consumer.h>
22
#include <linux/platform_device.h>
23
#include <linux/pm_runtime.h>
24
#include <linux/regmap.h>
25 26
#include <linux/sched.h>
#include <linux/spi/spi.h>
27
#include <linux/spi/spi-fsl-dspi.h>
28
#include <linux/spi/spi_bitbang.h>
29
#include <linux/time.h>
30 31 32

#define DRIVER_NAME "fsl-dspi"

33 34 35
#ifdef CONFIG_M5441x
#define DSPI_FIFO_SIZE			16
#else
36
#define DSPI_FIFO_SIZE			4
37
#endif
38
#define DSPI_DMA_BUFSIZE		(DSPI_FIFO_SIZE * 1024)
39 40 41 42 43 44

#define SPI_MCR		0x00
#define SPI_MCR_MASTER		(1 << 31)
#define SPI_MCR_PCSIS		(0x3F << 16)
#define SPI_MCR_CLR_TXF	(1 << 11)
#define SPI_MCR_CLR_RXF	(1 << 10)
45
#define SPI_MCR_XSPI		(1 << 3)
46 47

#define SPI_TCR			0x08
48
#define SPI_TCR_GET_TCNT(x)	(((x) & 0xffff0000) >> 16)
49

50
#define SPI_CTAR(x)		(0x0c + (((x) & 0x3) * 4))
51 52 53 54
#define SPI_CTAR_FMSZ(x)	(((x) & 0x0000000f) << 27)
#define SPI_CTAR_CPOL(x)	((x) << 26)
#define SPI_CTAR_CPHA(x)	((x) << 25)
#define SPI_CTAR_LSBFE(x)	((x) << 24)
55
#define SPI_CTAR_PCSSCK(x)	(((x) & 0x00000003) << 22)
56 57 58 59 60 61 62
#define SPI_CTAR_PASC(x)	(((x) & 0x00000003) << 20)
#define SPI_CTAR_PDT(x)	(((x) & 0x00000003) << 18)
#define SPI_CTAR_PBR(x)	(((x) & 0x00000003) << 16)
#define SPI_CTAR_CSSCK(x)	(((x) & 0x0000000f) << 12)
#define SPI_CTAR_ASC(x)	(((x) & 0x0000000f) << 8)
#define SPI_CTAR_DT(x)		(((x) & 0x0000000f) << 4)
#define SPI_CTAR_BR(x)		((x) & 0x0000000f)
63
#define SPI_CTAR_SCALE_BITS	0xf
64 65 66 67 68

#define SPI_CTAR0_SLAVE	0x0c

#define SPI_SR			0x2c
#define SPI_SR_EOQF		0x10000000
69
#define SPI_SR_TCFQF		0x80000000
70
#define SPI_SR_CLEAR		0xdaad0000
71

72 73 74 75
#define SPI_RSER_TFFFE		BIT(25)
#define SPI_RSER_TFFFD		BIT(24)
#define SPI_RSER_RFDFE		BIT(17)
#define SPI_RSER_RFDFD		BIT(16)
76 77 78

#define SPI_RSER		0x30
#define SPI_RSER_EOQFE		0x10000000
79
#define SPI_RSER_TCFQE		0x80000000
80 81

#define SPI_PUSHR		0x34
82 83 84 85 86 87 88 89 90 91
#define SPI_PUSHR_CMD_CONT	(1 << 15)
#define SPI_PUSHR_CONT		(SPI_PUSHR_CMD_CONT << 16)
#define SPI_PUSHR_CMD_CTAS(x)	(((x) & 0x0003) << 12)
#define SPI_PUSHR_CTAS(x)	(SPI_PUSHR_CMD_CTAS(x) << 16)
#define SPI_PUSHR_CMD_EOQ	(1 << 11)
#define SPI_PUSHR_EOQ		(SPI_PUSHR_CMD_EOQ << 16)
#define SPI_PUSHR_CMD_CTCNT	(1 << 10)
#define SPI_PUSHR_CTCNT		(SPI_PUSHR_CMD_CTCNT << 16)
#define SPI_PUSHR_CMD_PCS(x)	((1 << x) & 0x003f)
#define SPI_PUSHR_PCS(x)	(SPI_PUSHR_CMD_PCS(x) << 16)
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#define SPI_PUSHR_TXDATA(x)	((x) & 0x0000ffff)

#define SPI_PUSHR_SLAVE	0x34

#define SPI_POPR		0x38
#define SPI_POPR_RXDATA(x)	((x) & 0x0000ffff)

#define SPI_TXFR0		0x3c
#define SPI_TXFR1		0x40
#define SPI_TXFR2		0x44
#define SPI_TXFR3		0x48
#define SPI_RXFR0		0x7c
#define SPI_RXFR1		0x80
#define SPI_RXFR2		0x84
#define SPI_RXFR3		0x88

108 109 110 111 112 113
#define SPI_CTARE(x)		(0x11c + (((x) & 0x3) * 4))
#define SPI_CTARE_FMSZE(x)	(((x) & 0x1) << 16)
#define SPI_CTARE_DTCP(x)	((x) & 0x7ff)

#define SPI_SREX		0x13c

114 115 116 117 118
#define SPI_FRAME_BITS(bits)	SPI_CTAR_FMSZ((bits) - 1)
#define SPI_FRAME_BITS_MASK	SPI_CTAR_FMSZ(0xf)
#define SPI_FRAME_BITS_16	SPI_CTAR_FMSZ(0xf)
#define SPI_FRAME_BITS_8	SPI_CTAR_FMSZ(0x7)

119 120 121
#define SPI_FRAME_EBITS(bits)	SPI_CTARE_FMSZE(((bits) - 1) >> 4)
#define SPI_FRAME_EBITS_MASK	SPI_CTARE_FMSZE(1)

122 123 124 125
/* Register offsets for regmap_pushr */
#define PUSHR_CMD		0x0
#define PUSHR_TX		0x2

126 127 128 129
#define SPI_CS_INIT		0x01
#define SPI_CS_ASSERT		0x02
#define SPI_CS_DROP		0x04

130 131
#define DMA_COMPLETION_TIMEOUT	msecs_to_jiffies(3000)

132 133 134 135 136
struct chip_data {
	u32 ctar_val;
	u16 void_write_data;
};

137 138 139
enum dspi_trans_mode {
	DSPI_EOQ_MODE = 0,
	DSPI_TCFQ_MODE,
140
	DSPI_DMA_MODE,
141 142 143 144
};

struct fsl_dspi_devtype_data {
	enum dspi_trans_mode trans_mode;
145
	u8 max_clock_factor;
146
	bool xspi_mode;
147 148 149
};

static const struct fsl_dspi_devtype_data vf610_data = {
150
	.trans_mode = DSPI_DMA_MODE,
151
	.max_clock_factor = 2,
152 153 154 155
};

static const struct fsl_dspi_devtype_data ls1021a_v1_data = {
	.trans_mode = DSPI_TCFQ_MODE,
156
	.max_clock_factor = 8,
157
	.xspi_mode = true,
158 159 160 161
};

static const struct fsl_dspi_devtype_data ls2085a_data = {
	.trans_mode = DSPI_TCFQ_MODE,
162
	.max_clock_factor = 8,
163 164
};

165 166 167 168 169
static const struct fsl_dspi_devtype_data coldfire_data = {
	.trans_mode = DSPI_EOQ_MODE,
	.max_clock_factor = 8,
};

170
struct fsl_dspi_dma {
171
	/* Length of transfer in words of DSPI_FIFO_SIZE */
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	u32 curr_xfer_len;

	u32 *tx_dma_buf;
	struct dma_chan *chan_tx;
	dma_addr_t tx_dma_phys;
	struct completion cmd_tx_complete;
	struct dma_async_tx_descriptor *tx_desc;

	u32 *rx_dma_buf;
	struct dma_chan *chan_rx;
	dma_addr_t rx_dma_phys;
	struct completion cmd_rx_complete;
	struct dma_async_tx_descriptor *rx_desc;
};

187
struct fsl_dspi {
C
Chao Fu 已提交
188
	struct spi_master	*master;
189 190
	struct platform_device	*pdev;

191
	struct regmap		*regmap;
192
	struct regmap		*regmap_pushr;
193
	int			irq;
194
	struct clk		*clk;
195

196
	struct spi_transfer	*cur_transfer;
C
Chao Fu 已提交
197
	struct spi_message	*cur_msg;
198 199
	struct chip_data	*cur_chip;
	size_t			len;
200
	const void		*tx;
201 202 203
	void			*rx;
	void			*rx_end;
	u16			void_write_data;
204
	u16			tx_cmd;
205 206
	u8			bits_per_word;
	u8			bytes_per_word;
207
	const struct fsl_dspi_devtype_data *devtype_data;
208

209 210
	wait_queue_head_t	waitq;
	u32			waitflags;
211

212
	struct fsl_dspi_dma	*dma;
213 214
};

215
static u32 dspi_pop_tx(struct fsl_dspi *dspi)
216
{
217
	u32 txdata = 0;
218 219 220 221

	if (dspi->tx) {
		if (dspi->bytes_per_word == 1)
			txdata = *(u8 *)dspi->tx;
222
		else if (dspi->bytes_per_word == 2)
223
			txdata = *(u16 *)dspi->tx;
224 225
		else  /* dspi->bytes_per_word == 4 */
			txdata = *(u32 *)dspi->tx;
226 227 228 229 230
		dspi->tx += dspi->bytes_per_word;
	}
	dspi->len -= dspi->bytes_per_word;
	return txdata;
}
231

232
static u32 dspi_pop_tx_pushr(struct fsl_dspi *dspi)
233
{
234
	u16 cmd = dspi->tx_cmd, data = dspi_pop_tx(dspi);
235

236 237 238 239 240 241 242 243 244 245 246 247
	if (dspi->len > 0)
		cmd |= SPI_PUSHR_CMD_CONT;
	return cmd << 16 | data;
}

static void dspi_push_rx(struct fsl_dspi *dspi, u32 rxdata)
{
	if (!dspi->rx)
		return;

	/* Mask of undefined bits */
	rxdata &= (1 << dspi->bits_per_word) - 1;
248

249 250
	if (dspi->bytes_per_word == 1)
		*(u8 *)dspi->rx = rxdata;
251
	else if (dspi->bytes_per_word == 2)
252
		*(u16 *)dspi->rx = rxdata;
253 254
	else /* dspi->bytes_per_word == 4 */
		*(u32 *)dspi->rx = rxdata;
255
	dspi->rx += dspi->bytes_per_word;
256 257
}

258 259 260 261 262 263 264 265 266 267 268 269
static void dspi_tx_dma_callback(void *arg)
{
	struct fsl_dspi *dspi = arg;
	struct fsl_dspi_dma *dma = dspi->dma;

	complete(&dma->cmd_tx_complete);
}

static void dspi_rx_dma_callback(void *arg)
{
	struct fsl_dspi *dspi = arg;
	struct fsl_dspi_dma *dma = dspi->dma;
270
	int i;
271

272
	if (dspi->rx) {
273 274
		for (i = 0; i < dma->curr_xfer_len; i++)
			dspi_push_rx(dspi, dspi->dma->rx_dma_buf[i]);
275 276 277 278 279 280 281 282 283 284
	}

	complete(&dma->cmd_rx_complete);
}

static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
{
	struct fsl_dspi_dma *dma = dspi->dma;
	struct device *dev = &dspi->pdev->dev;
	int time_left;
285
	int i;
286

287 288
	for (i = 0; i < dma->curr_xfer_len; i++)
		dspi->dma->tx_dma_buf[i] = dspi_pop_tx_pushr(dspi);
289 290 291

	dma->tx_desc = dmaengine_prep_slave_single(dma->chan_tx,
					dma->tx_dma_phys,
292 293 294
					dma->curr_xfer_len *
					DMA_SLAVE_BUSWIDTH_4_BYTES,
					DMA_MEM_TO_DEV,
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
	if (!dma->tx_desc) {
		dev_err(dev, "Not able to get desc for DMA xfer\n");
		return -EIO;
	}

	dma->tx_desc->callback = dspi_tx_dma_callback;
	dma->tx_desc->callback_param = dspi;
	if (dma_submit_error(dmaengine_submit(dma->tx_desc))) {
		dev_err(dev, "DMA submit failed\n");
		return -EINVAL;
	}

	dma->rx_desc = dmaengine_prep_slave_single(dma->chan_rx,
					dma->rx_dma_phys,
310 311 312
					dma->curr_xfer_len *
					DMA_SLAVE_BUSWIDTH_4_BYTES,
					DMA_DEV_TO_MEM,
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
	if (!dma->rx_desc) {
		dev_err(dev, "Not able to get desc for DMA xfer\n");
		return -EIO;
	}

	dma->rx_desc->callback = dspi_rx_dma_callback;
	dma->rx_desc->callback_param = dspi;
	if (dma_submit_error(dmaengine_submit(dma->rx_desc))) {
		dev_err(dev, "DMA submit failed\n");
		return -EINVAL;
	}

	reinit_completion(&dspi->dma->cmd_rx_complete);
	reinit_completion(&dspi->dma->cmd_tx_complete);

	dma_async_issue_pending(dma->chan_rx);
	dma_async_issue_pending(dma->chan_tx);

	time_left = wait_for_completion_timeout(&dspi->dma->cmd_tx_complete,
					DMA_COMPLETION_TIMEOUT);
	if (time_left == 0) {
		dev_err(dev, "DMA tx timeout\n");
		dmaengine_terminate_all(dma->chan_tx);
		dmaengine_terminate_all(dma->chan_rx);
		return -ETIMEDOUT;
	}

	time_left = wait_for_completion_timeout(&dspi->dma->cmd_rx_complete,
					DMA_COMPLETION_TIMEOUT);
	if (time_left == 0) {
		dev_err(dev, "DMA rx timeout\n");
		dmaengine_terminate_all(dma->chan_tx);
		dmaengine_terminate_all(dma->chan_rx);
		return -ETIMEDOUT;
	}

	return 0;
}

static int dspi_dma_xfer(struct fsl_dspi *dspi)
{
	struct fsl_dspi_dma *dma = dspi->dma;
	struct device *dev = &dspi->pdev->dev;
357
	struct spi_message *message = dspi->cur_msg;
358 359 360 361 362
	int curr_remaining_bytes;
	int bytes_per_buffer;
	int ret = 0;

	curr_remaining_bytes = dspi->len;
363
	bytes_per_buffer = DSPI_DMA_BUFSIZE / DSPI_FIFO_SIZE;
364 365
	while (curr_remaining_bytes) {
		/* Check if current transfer fits the DMA buffer */
366 367
		dma->curr_xfer_len = curr_remaining_bytes
			/ dspi->bytes_per_word;
368
		if (dma->curr_xfer_len > bytes_per_buffer)
369 370 371 372 373 374 375 376
			dma->curr_xfer_len = bytes_per_buffer;

		ret = dspi_next_xfer_dma_submit(dspi);
		if (ret) {
			dev_err(dev, "DMA transfer failed\n");
			goto exit;

		} else {
377 378 379 380
			const int len =
				dma->curr_xfer_len * dspi->bytes_per_word;
			curr_remaining_bytes -= len;
			message->actual_length += len;
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
			if (curr_remaining_bytes < 0)
				curr_remaining_bytes = 0;
		}
	}

exit:
	return ret;
}

static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr)
{
	struct fsl_dspi_dma *dma;
	struct dma_slave_config cfg;
	struct device *dev = &dspi->pdev->dev;
	int ret;

	dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
	if (!dma)
		return -ENOMEM;

	dma->chan_rx = dma_request_slave_channel(dev, "rx");
	if (!dma->chan_rx) {
		dev_err(dev, "rx dma channel not available\n");
		ret = -ENODEV;
		return ret;
	}

	dma->chan_tx = dma_request_slave_channel(dev, "tx");
	if (!dma->chan_tx) {
		dev_err(dev, "tx dma channel not available\n");
		ret = -ENODEV;
		goto err_tx_channel;
	}

	dma->tx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE,
					&dma->tx_dma_phys, GFP_KERNEL);
	if (!dma->tx_dma_buf) {
		ret = -ENOMEM;
		goto err_tx_dma_buf;
	}

	dma->rx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE,
					&dma->rx_dma_phys, GFP_KERNEL);
	if (!dma->rx_dma_buf) {
		ret = -ENOMEM;
		goto err_rx_dma_buf;
	}

	cfg.src_addr = phy_addr + SPI_POPR;
	cfg.dst_addr = phy_addr + SPI_PUSHR;
	cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
	cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
	cfg.src_maxburst = 1;
	cfg.dst_maxburst = 1;

	cfg.direction = DMA_DEV_TO_MEM;
	ret = dmaengine_slave_config(dma->chan_rx, &cfg);
	if (ret) {
		dev_err(dev, "can't configure rx dma channel\n");
		ret = -EINVAL;
		goto err_slave_config;
	}

	cfg.direction = DMA_MEM_TO_DEV;
	ret = dmaengine_slave_config(dma->chan_tx, &cfg);
	if (ret) {
		dev_err(dev, "can't configure tx dma channel\n");
		ret = -EINVAL;
		goto err_slave_config;
	}

	dspi->dma = dma;
	init_completion(&dma->cmd_tx_complete);
	init_completion(&dma->cmd_rx_complete);

	return 0;

err_slave_config:
459 460
	dma_free_coherent(dev, DSPI_DMA_BUFSIZE,
			dma->rx_dma_buf, dma->rx_dma_phys);
461
err_rx_dma_buf:
462 463
	dma_free_coherent(dev, DSPI_DMA_BUFSIZE,
			dma->tx_dma_buf, dma->tx_dma_phys);
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
err_tx_dma_buf:
	dma_release_channel(dma->chan_tx);
err_tx_channel:
	dma_release_channel(dma->chan_rx);

	devm_kfree(dev, dma);
	dspi->dma = NULL;

	return ret;
}

static void dspi_release_dma(struct fsl_dspi *dspi)
{
	struct fsl_dspi_dma *dma = dspi->dma;
	struct device *dev = &dspi->pdev->dev;

	if (dma) {
		if (dma->chan_tx) {
			dma_unmap_single(dev, dma->tx_dma_phys,
					DSPI_DMA_BUFSIZE, DMA_TO_DEVICE);
			dma_release_channel(dma->chan_tx);
		}

		if (dma->chan_rx) {
			dma_unmap_single(dev, dma->rx_dma_phys,
					DSPI_DMA_BUFSIZE, DMA_FROM_DEVICE);
			dma_release_channel(dma->chan_rx);
		}
	}
}

495 496 497 498 499 500 501 502 503
static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
		unsigned long clkrate)
{
	/* Valid baud rate pre-scaler values */
	int pbr_tbl[4] = {2, 3, 5, 7};
	int brs[16] = {	2,	4,	6,	8,
		16,	32,	64,	128,
		256,	512,	1024,	2048,
		4096,	8192,	16384,	32768 };
504 505 506 507
	int scale_needed, scale, minscale = INT_MAX;
	int i, j;

	scale_needed = clkrate / speed_hz;
508 509
	if (clkrate % speed_hz)
		scale_needed++;
510 511 512 513 514 515 516 517 518 519 520

	for (i = 0; i < ARRAY_SIZE(brs); i++)
		for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
			scale = brs[i] * pbr_tbl[j];
			if (scale >= scale_needed) {
				if (scale < minscale) {
					minscale = scale;
					*br = i;
					*pbr = j;
				}
				break;
521 522 523
			}
		}

524 525 526 527 528 529
	if (minscale == INT_MAX) {
		pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
			speed_hz, clkrate);
		*pbr = ARRAY_SIZE(pbr_tbl) - 1;
		*br =  ARRAY_SIZE(brs) - 1;
	}
530 531
}

532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
static void ns_delay_scale(char *psc, char *sc, int delay_ns,
		unsigned long clkrate)
{
	int pscale_tbl[4] = {1, 3, 5, 7};
	int scale_needed, scale, minscale = INT_MAX;
	int i, j;
	u32 remainder;

	scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC,
			&remainder);
	if (remainder)
		scale_needed++;

	for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++)
		for (j = 0; j <= SPI_CTAR_SCALE_BITS; j++) {
			scale = pscale_tbl[i] * (2 << j);
			if (scale >= scale_needed) {
				if (scale < minscale) {
					minscale = scale;
					*psc = i;
					*sc = j;
				}
				break;
555 556 557
			}
		}

558 559 560 561 562 563
	if (minscale == INT_MAX) {
		pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
			delay_ns, clkrate);
		*psc = ARRAY_SIZE(pscale_tbl) - 1;
		*sc = SPI_CTAR_SCALE_BITS;
	}
564 565
}

566
static void fifo_write(struct fsl_dspi *dspi)
567
{
568
	regmap_write(dspi->regmap, SPI_PUSHR, dspi_pop_tx_pushr(dspi));
569
}
570

571 572 573 574 575 576 577 578 579 580 581 582 583 584
static void cmd_fifo_write(struct fsl_dspi *dspi)
{
	u16 cmd = dspi->tx_cmd;

	if (dspi->len > 0)
		cmd |= SPI_PUSHR_CMD_CONT;
	regmap_write(dspi->regmap_pushr, PUSHR_CMD, cmd);
}

static void tx_fifo_write(struct fsl_dspi *dspi, u16 txdata)
{
	regmap_write(dspi->regmap_pushr, PUSHR_TX, txdata);
}

585
static void dspi_tcfq_write(struct fsl_dspi *dspi)
586
{
587 588
	/* Clear transfer count */
	dspi->tx_cmd |= SPI_PUSHR_CMD_CTCNT;
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611

	if (dspi->devtype_data->xspi_mode && dspi->bits_per_word > 16) {
		/* Write two TX FIFO entries first, and then the corresponding
		 * CMD FIFO entry.
		 */
		u32 data = dspi_pop_tx(dspi);

		if (dspi->cur_chip->ctar_val & SPI_CTAR_LSBFE(1)) {
			/* LSB */
			tx_fifo_write(dspi, data & 0xFFFF);
			tx_fifo_write(dspi, data >> 16);
		} else {
			/* MSB */
			tx_fifo_write(dspi, data >> 16);
			tx_fifo_write(dspi, data & 0xFFFF);
		}
		cmd_fifo_write(dspi);
	} else {
		/* Write one entry to both TX FIFO and CMD FIFO
		 * simultaneously.
		 */
		fifo_write(dspi);
	}
612
}
613

614
static u32 fifo_read(struct fsl_dspi *dspi)
615
{
616
	u32 rxdata = 0;
617

618 619
	regmap_read(dspi->regmap, SPI_POPR, &rxdata);
	return rxdata;
620 621
}

622
static void dspi_tcfq_read(struct fsl_dspi *dspi)
623
{
624
	dspi_push_rx(dspi, fifo_read(dspi));
625
}
626

627
static void dspi_eoq_write(struct fsl_dspi *dspi)
628
{
629
	int fifo_size = DSPI_FIFO_SIZE;
630
	u16 xfer_cmd = dspi->tx_cmd;
631 632 633

	/* Fill TX FIFO with as many transfers as possible */
	while (dspi->len && fifo_size--) {
634
		dspi->tx_cmd = xfer_cmd;
635 636 637 638 639 640 641 642
		/* Request EOQF for last transfer in FIFO */
		if (dspi->len == dspi->bytes_per_word || fifo_size == 0)
			dspi->tx_cmd |= SPI_PUSHR_CMD_EOQ;
		/* Clear transfer count for first transfer in FIFO */
		if (fifo_size == (DSPI_FIFO_SIZE - 1))
			dspi->tx_cmd |= SPI_PUSHR_CMD_CTCNT;
		/* Write combined TX FIFO and CMD FIFO entry */
		fifo_write(dspi);
643
	}
644 645
}

646
static void dspi_eoq_read(struct fsl_dspi *dspi)
647
{
648
	int fifo_size = DSPI_FIFO_SIZE;
649

650 651 652
	/* Read one FIFO entry at and push to rx buffer */
	while ((dspi->rx < dspi->rx_end) && fifo_size--)
		dspi_push_rx(dspi, fifo_read(dspi));
653 654
}

C
Chao Fu 已提交
655 656
static int dspi_transfer_one_message(struct spi_master *master,
		struct spi_message *message)
657
{
C
Chao Fu 已提交
658 659 660 661
	struct fsl_dspi *dspi = spi_master_get_devdata(master);
	struct spi_device *spi = message->spi;
	struct spi_transfer *transfer;
	int status = 0;
662 663
	enum dspi_trans_mode trans_mode;

C
Chao Fu 已提交
664 665 666 667 668 669
	message->actual_length = 0;

	list_for_each_entry(transfer, &message->transfers, transfer_list) {
		dspi->cur_transfer = transfer;
		dspi->cur_msg = message;
		dspi->cur_chip = spi_get_ctldata(spi);
670 671 672
		/* Prepare command word for CMD FIFO */
		dspi->tx_cmd = SPI_PUSHR_CMD_CTAS(0) |
			SPI_PUSHR_CMD_PCS(spi->chip_select);
673
		if (list_is_last(&dspi->cur_transfer->transfer_list,
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
				 &dspi->cur_msg->transfers)) {
			/* Leave PCS activated after last transfer when
			 * cs_change is set.
			 */
			if (transfer->cs_change)
				dspi->tx_cmd |= SPI_PUSHR_CMD_CONT;
		} else {
			/* Keep PCS active between transfers in same message
			 * when cs_change is not set, and de-activate PCS
			 * between transfers in the same message when
			 * cs_change is set.
			 */
			if (!transfer->cs_change)
				dspi->tx_cmd |= SPI_PUSHR_CMD_CONT;
		}

C
Chao Fu 已提交
690 691
		dspi->void_write_data = dspi->cur_chip->void_write_data;

692
		dspi->tx = transfer->tx_buf;
C
Chao Fu 已提交
693 694 695
		dspi->rx = transfer->rx_buf;
		dspi->rx_end = dspi->rx + transfer->len;
		dspi->len = transfer->len;
696 697 698 699
		/* Validated transfer specific frame size (defaults applied) */
		dspi->bits_per_word = transfer->bits_per_word;
		if (transfer->bits_per_word <= 8)
			dspi->bytes_per_word = 1;
700
		else if (transfer->bits_per_word <= 16)
701
			dspi->bytes_per_word = 2;
702 703
		else
			dspi->bytes_per_word = 4;
C
Chao Fu 已提交
704 705

		regmap_update_bits(dspi->regmap, SPI_MCR,
706 707
				   SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
				   SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
708
		regmap_write(dspi->regmap, SPI_CTAR(0),
709 710
			     dspi->cur_chip->ctar_val |
			     SPI_FRAME_BITS(transfer->bits_per_word));
711 712 713 714
		if (dspi->devtype_data->xspi_mode)
			regmap_write(dspi->regmap, SPI_CTARE(0),
				     SPI_FRAME_EBITS(transfer->bits_per_word)
				     | SPI_CTARE_DTCP(1));
715

716 717 718 719
		trans_mode = dspi->devtype_data->trans_mode;
		switch (trans_mode) {
		case DSPI_EOQ_MODE:
			regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
720
			dspi_eoq_write(dspi);
721 722 723
			break;
		case DSPI_TCFQ_MODE:
			regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_TCFQE);
724
			dspi_tcfq_write(dspi);
725
			break;
726 727 728 729 730
		case DSPI_DMA_MODE:
			regmap_write(dspi->regmap, SPI_RSER,
				SPI_RSER_TFFFE | SPI_RSER_TFFFD |
				SPI_RSER_RFDFE | SPI_RSER_RFDFD);
			status = dspi_dma_xfer(dspi);
731
			break;
732 733 734 735 736 737
		default:
			dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
				trans_mode);
			status = -EINVAL;
			goto out;
		}
738

739 740 741 742 743 744 745
		if (trans_mode != DSPI_DMA_MODE) {
			if (wait_event_interruptible(dspi->waitq,
						dspi->waitflags))
				dev_err(&dspi->pdev->dev,
					"wait transfer complete fail!\n");
			dspi->waitflags = 0;
		}
746

C
Chao Fu 已提交
747 748
		if (transfer->delay_usecs)
			udelay(transfer->delay_usecs);
749 750
	}

751
out:
C
Chao Fu 已提交
752 753 754 755
	message->status = status;
	spi_finalize_current_message(master);

	return status;
756 757
}

C
Chao Fu 已提交
758
static int dspi_setup(struct spi_device *spi)
759 760 761
{
	struct chip_data *chip;
	struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
762
	struct fsl_dspi_platform_data *pdata;
763 764
	u32 cs_sck_delay = 0, sck_cs_delay = 0;
	unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
765
	unsigned char pasc = 0, asc = 0;
766
	unsigned long clkrate;
767 768 769 770

	/* Only alloc on first setup */
	chip = spi_get_ctldata(spi);
	if (chip == NULL) {
771
		chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
772 773 774 775
		if (!chip)
			return -ENOMEM;
	}

776
	pdata = dev_get_platdata(&dspi->pdev->dev);
777

778 779 780 781 782 783 784 785 786 787
	if (!pdata) {
		of_property_read_u32(spi->dev.of_node, "fsl,spi-cs-sck-delay",
				&cs_sck_delay);

		of_property_read_u32(spi->dev.of_node, "fsl,spi-sck-cs-delay",
				&sck_cs_delay);
	} else {
		cs_sck_delay = pdata->cs_sck_delay;
		sck_cs_delay = pdata->sck_cs_delay;
	}
788

789 790
	chip->void_write_data = 0;

791 792 793 794 795 796 797 798
	clkrate = clk_get_rate(dspi->clk);
	hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);

	/* Set PCS to SCK delay scale values */
	ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate);

	/* Set After SCK delay scale values */
	ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate);
799

800
	chip->ctar_val = SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
801 802
		| SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
		| SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
803 804 805 806
		| SPI_CTAR_PCSSCK(pcssck)
		| SPI_CTAR_CSSCK(cssck)
		| SPI_CTAR_PASC(pasc)
		| SPI_CTAR_ASC(asc)
807 808 809 810 811 812 813 814
		| SPI_CTAR_PBR(pbr)
		| SPI_CTAR_BR(br);

	spi_set_ctldata(spi, chip);

	return 0;
}

815 816 817 818 819 820 821 822 823 824
static void dspi_cleanup(struct spi_device *spi)
{
	struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);

	dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
			spi->master->bus_num, spi->chip_select);

	kfree(chip);
}

825 826 827
static irqreturn_t dspi_interrupt(int irq, void *dev_id)
{
	struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
C
Chao Fu 已提交
828
	struct spi_message *msg = dspi->cur_msg;
829
	enum dspi_trans_mode trans_mode;
830
	u32 spi_sr, spi_tcr;
831
	u16 spi_tcnt;
832 833 834 835

	regmap_read(dspi->regmap, SPI_SR, &spi_sr);
	regmap_write(dspi->regmap, SPI_SR, spi_sr);

836

837
	if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF)) {
838 839 840
		/* Get transfer counter (in number of SPI transfers). It was
		 * reset to 0 when transfer(s) were started.
		 */
841 842
		regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
		spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
843
		/* Update total number of bytes that were transferred */
844
		msg->actual_length += spi_tcnt * dspi->bytes_per_word;
845 846

		trans_mode = dspi->devtype_data->trans_mode;
847 848
		switch (trans_mode) {
		case DSPI_EOQ_MODE:
849
			dspi_eoq_read(dspi);
850 851
			break;
		case DSPI_TCFQ_MODE:
852
			dspi_tcfq_read(dspi);
853 854 855 856
			break;
		default:
			dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
				trans_mode);
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
				return IRQ_HANDLED;
		}

		if (!dspi->len) {
			dspi->waitflags = 1;
			wake_up_interruptible(&dspi->waitq);
		} else {
			switch (trans_mode) {
			case DSPI_EOQ_MODE:
				dspi_eoq_write(dspi);
				break;
			case DSPI_TCFQ_MODE:
				dspi_tcfq_write(dspi);
				break;
			default:
				dev_err(&dspi->pdev->dev,
					"unsupported trans_mode %u\n",
					trans_mode);
			}
876 877
		}
	}
878

879 880 881
	return IRQ_HANDLED;
}

882
static const struct of_device_id fsl_dspi_dt_ids[] = {
883 884 885
	{ .compatible = "fsl,vf610-dspi", .data = &vf610_data, },
	{ .compatible = "fsl,ls1021a-v1.0-dspi", .data = &ls1021a_v1_data, },
	{ .compatible = "fsl,ls2085a-dspi", .data = &ls2085a_data, },
886 887 888 889 890 891 892 893 894 895 896 897 898
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);

#ifdef CONFIG_PM_SLEEP
static int dspi_suspend(struct device *dev)
{
	struct spi_master *master = dev_get_drvdata(dev);
	struct fsl_dspi *dspi = spi_master_get_devdata(master);

	spi_master_suspend(master);
	clk_disable_unprepare(dspi->clk);

899 900
	pinctrl_pm_select_sleep_state(dev);

901 902 903 904 905 906 907
	return 0;
}

static int dspi_resume(struct device *dev)
{
	struct spi_master *master = dev_get_drvdata(dev);
	struct fsl_dspi *dspi = spi_master_get_devdata(master);
908
	int ret;
909

910 911
	pinctrl_pm_select_default_state(dev);

912 913 914
	ret = clk_prepare_enable(dspi->clk);
	if (ret)
		return ret;
915 916 917 918 919 920
	spi_master_resume(master);

	return 0;
}
#endif /* CONFIG_PM_SLEEP */

921
static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
922

923 924 925 926 927 928 929 930 931 932 933
static const struct regmap_range dspi_volatile_ranges[] = {
	regmap_reg_range(SPI_MCR, SPI_TCR),
	regmap_reg_range(SPI_SR, SPI_SR),
	regmap_reg_range(SPI_PUSHR, SPI_RXFR3),
};

static const struct regmap_access_table dspi_volatile_table = {
	.yes_ranges     = dspi_volatile_ranges,
	.n_yes_ranges   = ARRAY_SIZE(dspi_volatile_ranges),
};

934
static const struct regmap_config dspi_regmap_config = {
935 936 937 938
	.reg_bits = 32,
	.val_bits = 32,
	.reg_stride = 4,
	.max_register = 0x88,
939
	.volatile_table = &dspi_volatile_table,
940 941
};

942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
static const struct regmap_range dspi_xspi_volatile_ranges[] = {
	regmap_reg_range(SPI_MCR, SPI_TCR),
	regmap_reg_range(SPI_SR, SPI_SR),
	regmap_reg_range(SPI_PUSHR, SPI_RXFR3),
	regmap_reg_range(SPI_SREX, SPI_SREX),
};

static const struct regmap_access_table dspi_xspi_volatile_table = {
	.yes_ranges     = dspi_xspi_volatile_ranges,
	.n_yes_ranges   = ARRAY_SIZE(dspi_xspi_volatile_ranges),
};

static const struct regmap_config dspi_xspi_regmap_config[] = {
	{
		.reg_bits = 32,
		.val_bits = 32,
		.reg_stride = 4,
		.max_register = 0x13c,
		.volatile_table = &dspi_xspi_volatile_table,
	},
	{
		.name = "pushr",
		.reg_bits = 16,
		.val_bits = 16,
		.reg_stride = 2,
		.max_register = 0x2,
	},
};

971 972
static void dspi_init(struct fsl_dspi *dspi)
{
973 974
	regmap_write(dspi->regmap, SPI_MCR, SPI_MCR_MASTER | SPI_MCR_PCSIS |
		     (dspi->devtype_data->xspi_mode ? SPI_MCR_XSPI : 0));
975
	regmap_write(dspi->regmap, SPI_SR, SPI_SR_CLEAR);
976 977 978
	if (dspi->devtype_data->xspi_mode)
		regmap_write(dspi->regmap, SPI_CTARE(0),
			     SPI_CTARE_FMSZE(0) | SPI_CTARE_DTCP(1));
979 980
}

981 982 983 984 985 986
static int dspi_probe(struct platform_device *pdev)
{
	struct device_node *np = pdev->dev.of_node;
	struct spi_master *master;
	struct fsl_dspi *dspi;
	struct resource *res;
987
	const struct regmap_config *regmap_config;
988
	void __iomem *base;
989
	struct fsl_dspi_platform_data *pdata;
990 991 992 993 994 995 996 997
	int ret = 0, cs_num, bus_num;

	master = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
	if (!master)
		return -ENOMEM;

	dspi = spi_master_get_devdata(master);
	dspi->pdev = pdev;
C
Chao Fu 已提交
998 999 1000 1001 1002 1003
	dspi->master = master;

	master->transfer = NULL;
	master->setup = dspi_setup;
	master->transfer_one_message = dspi_transfer_one_message;
	master->dev.of_node = pdev->dev.of_node;
1004

1005
	master->cleanup = dspi_cleanup;
1006
	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
1007

1008 1009 1010 1011
	pdata = dev_get_platdata(&pdev->dev);
	if (pdata) {
		master->num_chipselect = pdata->cs_num;
		master->bus_num = pdata->bus_num;
1012

1013 1014
		dspi->devtype_data = &coldfire_data;
	} else {
1015

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
		ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
		if (ret < 0) {
			dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
			goto out_master_put;
		}
		master->num_chipselect = cs_num;

		ret = of_property_read_u32(np, "bus-num", &bus_num);
		if (ret < 0) {
			dev_err(&pdev->dev, "can't get bus-num\n");
			goto out_master_put;
		}
		master->bus_num = bus_num;

		dspi->devtype_data = of_device_get_match_data(&pdev->dev);
		if (!dspi->devtype_data) {
			dev_err(&pdev->dev, "can't get devtype_data\n");
			ret = -EFAULT;
			goto out_master_put;
		}
1036 1037
	}

1038 1039 1040 1041 1042
	if (dspi->devtype_data->xspi_mode)
		master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
	else
		master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);

1043
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1044 1045 1046
	base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(base)) {
		ret = PTR_ERR(base);
1047 1048 1049
		goto out_master_put;
	}

1050 1051 1052 1053 1054
	if (dspi->devtype_data->xspi_mode)
		regmap_config = &dspi_xspi_regmap_config[0];
	else
		regmap_config = &dspi_regmap_config;
	dspi->regmap = devm_regmap_init_mmio(&pdev->dev, base, regmap_config);
1055 1056 1057
	if (IS_ERR(dspi->regmap)) {
		dev_err(&pdev->dev, "failed to init regmap: %ld\n",
				PTR_ERR(dspi->regmap));
1058 1059
		ret = PTR_ERR(dspi->regmap);
		goto out_master_put;
1060 1061
	}

1062 1063 1064 1065 1066 1067 1068 1069
	if (dspi->devtype_data->xspi_mode) {
		dspi->regmap_pushr = devm_regmap_init_mmio(
			&pdev->dev, base + SPI_PUSHR,
			&dspi_xspi_regmap_config[1]);
		if (IS_ERR(dspi->regmap_pushr)) {
			dev_err(&pdev->dev,
				"failed to init pushr regmap: %ld\n",
				PTR_ERR(dspi->regmap_pushr));
1070
			ret = PTR_ERR(dspi->regmap_pushr);
1071 1072 1073 1074
			goto out_master_put;
		}
	}

1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
	dspi->clk = devm_clk_get(&pdev->dev, "dspi");
	if (IS_ERR(dspi->clk)) {
		ret = PTR_ERR(dspi->clk);
		dev_err(&pdev->dev, "unable to get clock\n");
		goto out_master_put;
	}
	ret = clk_prepare_enable(dspi->clk);
	if (ret)
		goto out_master_put;

1085
	dspi_init(dspi);
1086 1087 1088 1089
	dspi->irq = platform_get_irq(pdev, 0);
	if (dspi->irq < 0) {
		dev_err(&pdev->dev, "can't get platform irq\n");
		ret = dspi->irq;
1090
		goto out_clk_put;
1091 1092
	}

1093 1094
	ret = devm_request_irq(&pdev->dev, dspi->irq, dspi_interrupt,
			       IRQF_SHARED, pdev->name, dspi);
1095 1096
	if (ret < 0) {
		dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
1097
		goto out_clk_put;
1098 1099
	}

1100
	if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) {
1101 1102
		ret = dspi_request_dma(dspi, res->start);
		if (ret < 0) {
1103 1104 1105 1106 1107
			dev_err(&pdev->dev, "can't get dma channels\n");
			goto out_clk_put;
		}
	}

1108 1109 1110
	master->max_speed_hz =
		clk_get_rate(dspi->clk) / dspi->devtype_data->max_clock_factor;

1111
	init_waitqueue_head(&dspi->waitq);
1112
	platform_set_drvdata(pdev, master);
1113

C
Chao Fu 已提交
1114
	ret = spi_register_master(master);
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
	if (ret != 0) {
		dev_err(&pdev->dev, "Problem registering DSPI master\n");
		goto out_clk_put;
	}

	return ret;

out_clk_put:
	clk_disable_unprepare(dspi->clk);
out_master_put:
	spi_master_put(master);

	return ret;
}

static int dspi_remove(struct platform_device *pdev)
{
1132 1133
	struct spi_master *master = platform_get_drvdata(pdev);
	struct fsl_dspi *dspi = spi_master_get_devdata(master);
1134 1135

	/* Disconnect from the SPI framework */
1136
	dspi_release_dma(dspi);
1137
	clk_disable_unprepare(dspi->clk);
C
Chao Fu 已提交
1138
	spi_unregister_master(dspi->master);
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153

	return 0;
}

static struct platform_driver fsl_dspi_driver = {
	.driver.name    = DRIVER_NAME,
	.driver.of_match_table = fsl_dspi_dt_ids,
	.driver.owner   = THIS_MODULE,
	.driver.pm = &dspi_pm,
	.probe          = dspi_probe,
	.remove		= dspi_remove,
};
module_platform_driver(fsl_dspi_driver);

MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
1154
MODULE_LICENSE("GPL");
1155
MODULE_ALIAS("platform:" DRIVER_NAME);