spi-rockchip.c 22.8 KB
Newer Older
1 2
/*
 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
3
 * Author: Addy Ke <addy.ke@rock-chips.com>
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 */

#include <linux/clk.h>
S
Shawn Lin 已提交
17 18 19
#include <linux/dmaengine.h>
#include <linux/module.h>
#include <linux/of.h>
20
#include <linux/pinctrl/consumer.h>
21 22 23
#include <linux/platform_device.h>
#include <linux/spi/spi.h>
#include <linux/pm_runtime.h>
S
Shawn Lin 已提交
24
#include <linux/scatterlist.h>
25 26 27

#define DRIVER_NAME "rockchip-spi"

28 29 30 31 32
#define ROCKCHIP_SPI_CLR_BITS(reg, bits) \
		writel_relaxed(readl_relaxed(reg) & ~(bits), reg)
#define ROCKCHIP_SPI_SET_BITS(reg, bits) \
		writel_relaxed(readl_relaxed(reg) | (bits), reg)

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
/* SPI register offsets */
#define ROCKCHIP_SPI_CTRLR0			0x0000
#define ROCKCHIP_SPI_CTRLR1			0x0004
#define ROCKCHIP_SPI_SSIENR			0x0008
#define ROCKCHIP_SPI_SER			0x000c
#define ROCKCHIP_SPI_BAUDR			0x0010
#define ROCKCHIP_SPI_TXFTLR			0x0014
#define ROCKCHIP_SPI_RXFTLR			0x0018
#define ROCKCHIP_SPI_TXFLR			0x001c
#define ROCKCHIP_SPI_RXFLR			0x0020
#define ROCKCHIP_SPI_SR				0x0024
#define ROCKCHIP_SPI_IPR			0x0028
#define ROCKCHIP_SPI_IMR			0x002c
#define ROCKCHIP_SPI_ISR			0x0030
#define ROCKCHIP_SPI_RISR			0x0034
#define ROCKCHIP_SPI_ICR			0x0038
#define ROCKCHIP_SPI_DMACR			0x003c
#define ROCKCHIP_SPI_DMATDLR		0x0040
#define ROCKCHIP_SPI_DMARDLR		0x0044
#define ROCKCHIP_SPI_TXDR			0x0400
#define ROCKCHIP_SPI_RXDR			0x0800

/* Bit fields in CTRLR0 */
#define CR0_DFS_OFFSET				0

#define CR0_CFS_OFFSET				2

#define CR0_SCPH_OFFSET				6

#define CR0_SCPOL_OFFSET			7

#define CR0_CSM_OFFSET				8
#define CR0_CSM_KEEP				0x0
/* ss_n be high for half sclk_out cycles */
#define CR0_CSM_HALF				0X1
/* ss_n be high for one sclk_out cycle */
#define CR0_CSM_ONE					0x2

/* ss_n to sclk_out delay */
#define CR0_SSD_OFFSET				10
/*
 * The period between ss_n active and
 * sclk_out active is half sclk_out cycles
 */
#define CR0_SSD_HALF				0x0
/*
 * The period between ss_n active and
 * sclk_out active is one sclk_out cycle
 */
#define CR0_SSD_ONE					0x1

#define CR0_EM_OFFSET				11
#define CR0_EM_LITTLE				0x0
#define CR0_EM_BIG					0x1

#define CR0_FBM_OFFSET				12
#define CR0_FBM_MSB					0x0
#define CR0_FBM_LSB					0x1

#define CR0_BHT_OFFSET				13
#define CR0_BHT_16BIT				0x0
#define CR0_BHT_8BIT				0x1

#define CR0_RSD_OFFSET				14

#define CR0_FRF_OFFSET				16
#define CR0_FRF_SPI					0x0
#define CR0_FRF_SSP					0x1
#define CR0_FRF_MICROWIRE			0x2

#define CR0_XFM_OFFSET				18
#define CR0_XFM_MASK				(0x03 << SPI_XFM_OFFSET)
#define CR0_XFM_TR					0x0
#define CR0_XFM_TO					0x1
#define CR0_XFM_RO					0x2

#define CR0_OPM_OFFSET				20
#define CR0_OPM_MASTER				0x0
#define CR0_OPM_SLAVE				0x1

#define CR0_MTM_OFFSET				0x21

/* Bit fields in SER, 2bit */
#define SER_MASK					0x3

/* Bit fields in SR, 5bit */
#define SR_MASK						0x1f
#define SR_BUSY						(1 << 0)
#define SR_TF_FULL					(1 << 1)
#define SR_TF_EMPTY					(1 << 2)
#define SR_RF_EMPTY					(1 << 3)
#define SR_RF_FULL					(1 << 4)

/* Bit fields in ISR, IMR, ISR, RISR, 5bit */
#define INT_MASK					0x1f
#define INT_TF_EMPTY				(1 << 0)
#define INT_TF_OVERFLOW				(1 << 1)
#define INT_RF_UNDERFLOW			(1 << 2)
#define INT_RF_OVERFLOW				(1 << 3)
#define INT_RF_FULL					(1 << 4)

/* Bit fields in ICR, 4bit */
#define ICR_MASK					0x0f
#define ICR_ALL						(1 << 0)
#define ICR_RF_UNDERFLOW			(1 << 1)
#define ICR_RF_OVERFLOW				(1 << 2)
#define ICR_TF_OVERFLOW				(1 << 3)

/* Bit fields in DMACR */
#define RF_DMA_EN					(1 << 0)
#define TF_DMA_EN					(1 << 1)

#define RXBUSY						(1 << 0)
#define TXBUSY						(1 << 1)

148 149 150
/* sclk_out: spi master internal logic in rk3x can support 50Mhz */
#define MAX_SCLK_OUT		50000000

151 152 153 154 155 156
/*
 * SPI_CTRLR1 is 16-bits, so we should support lengths of 0xffff + 1. However,
 * the controller seems to hang when given 0x10000, so stick with this for now.
 */
#define ROCKCHIP_SPI_MAX_TRANLEN		0xffff

157 158
#define ROCKCHIP_SPI_MAX_CS_NUM			2

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
enum rockchip_ssi_type {
	SSI_MOTO_SPI = 0,
	SSI_TI_SSP,
	SSI_NS_MICROWIRE,
};

struct rockchip_spi_dma_data {
	struct dma_chan *ch;
	enum dma_transfer_direction direction;
	dma_addr_t addr;
};

struct rockchip_spi {
	struct device *dev;
	struct spi_master *master;

	struct clk *spiclk;
	struct clk *apb_pclk;

	void __iomem *regs;
	/*depth of the FIFO buffer */
	u32 fifo_len;
	/* max bus freq supported */
	u32 max_freq;
	/* supported slave numbers */
	enum rockchip_ssi_type type;

	u16 mode;
	u8 tmode;
	u8 bpw;
	u8 n_bytes;
190
	u32 rsd_nsecs;
191 192 193 194 195 196 197 198 199
	unsigned len;
	u32 speed;

	const void *tx;
	const void *tx_end;
	void *rx;
	void *rx_end;

	u32 state;
200
	/* protect state */
201 202
	spinlock_t lock;

203 204
	bool cs_asserted[ROCKCHIP_SPI_MAX_CS_NUM];

205 206 207 208 209
	u32 use_dma;
	struct sg_table tx_sg;
	struct sg_table rx_sg;
	struct rockchip_spi_dma_data dma_rx;
	struct rockchip_spi_dma_data dma_tx;
210
	struct dma_slave_caps dma_caps;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
};

static inline void spi_enable_chip(struct rockchip_spi *rs, int enable)
{
	writel_relaxed((enable ? 1 : 0), rs->regs + ROCKCHIP_SPI_SSIENR);
}

static inline void spi_set_clk(struct rockchip_spi *rs, u16 div)
{
	writel_relaxed(div, rs->regs + ROCKCHIP_SPI_BAUDR);
}

static inline void flush_fifo(struct rockchip_spi *rs)
{
	while (readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR))
		readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
}

229 230 231 232 233 234 235
static inline void wait_for_idle(struct rockchip_spi *rs)
{
	unsigned long timeout = jiffies + msecs_to_jiffies(5);

	do {
		if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
			return;
236
	} while (!time_after(jiffies, timeout));
237 238 239 240

	dev_warn(rs->dev, "spi controller is in busy state!\n");
}

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
static u32 get_fifo_len(struct rockchip_spi *rs)
{
	u32 fifo;

	for (fifo = 2; fifo < 32; fifo++) {
		writel_relaxed(fifo, rs->regs + ROCKCHIP_SPI_TXFTLR);
		if (fifo != readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFTLR))
			break;
	}

	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_TXFTLR);

	return (fifo == 31) ? 0 : fifo;
}

static inline u32 tx_max(struct rockchip_spi *rs)
{
	u32 tx_left, tx_room;

	tx_left = (rs->tx_end - rs->tx) / rs->n_bytes;
	tx_room = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);

	return min(tx_left, tx_room);
}

static inline u32 rx_max(struct rockchip_spi *rs)
{
	u32 rx_left = (rs->rx_end - rs->rx) / rs->n_bytes;
	u32 rx_room = (u32)readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);

	return min(rx_left, rx_room);
}

static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
{
276 277
	struct spi_master *master = spi->master;
	struct rockchip_spi *rs = spi_master_get_devdata(master);
278
	bool cs_asserted = !enable;
279

280 281 282
	/* Return immediately for no-op */
	if (cs_asserted == rs->cs_asserted[spi->chip_select])
		return;
283

284 285 286
	if (cs_asserted) {
		/* Keep things powered as long as CS is asserted */
		pm_runtime_get_sync(rs->dev);
287

288 289 290 291 292
		ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER,
				      BIT(spi->chip_select));
	} else {
		ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER,
				      BIT(spi->chip_select));
293

294 295 296
		/* Drop reference from when we first asserted CS */
		pm_runtime_put(rs->dev);
	}
297

298
	rs->cs_asserted[spi->chip_select] = cs_asserted;
299 300 301
}

static int rockchip_spi_prepare_message(struct spi_master *master,
302
					struct spi_message *msg)
303 304 305 306 307 308 309 310 311
{
	struct rockchip_spi *rs = spi_master_get_devdata(master);
	struct spi_device *spi = msg->spi;

	rs->mode = spi->mode;

	return 0;
}

312 313
static void rockchip_spi_handle_err(struct spi_master *master,
				    struct spi_message *msg)
314 315 316 317 318 319
{
	unsigned long flags;
	struct rockchip_spi *rs = spi_master_get_devdata(master);

	spin_lock_irqsave(&rs->lock, flags);

320 321 322
	/*
	 * For DMA mode, we need terminate DMA channel and flush
	 * fifo for the next transfer if DMA thansfer timeout.
323 324
	 * handle_err() was called by core if transfer failed.
	 * Maybe it is reasonable for error handling here.
325
	 */
326 327
	if (rs->use_dma) {
		if (rs->state & RXBUSY) {
328
			dmaengine_terminate_async(rs->dma_rx.ch);
329 330 331 332
			flush_fifo(rs);
		}

		if (rs->state & TXBUSY)
333
			dmaengine_terminate_async(rs->dma_tx.ch);
334 335 336
	}

	spin_unlock_irqrestore(&rs->lock, flags);
337 338 339 340 341 342
}

static int rockchip_spi_unprepare_message(struct spi_master *master,
					  struct spi_message *msg)
{
	struct rockchip_spi *rs = spi_master_get_devdata(master);
343

344 345
	spi_enable_chip(rs, 0);

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
	return 0;
}

static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
{
	u32 max = tx_max(rs);
	u32 txw = 0;

	while (max--) {
		if (rs->n_bytes == 1)
			txw = *(u8 *)(rs->tx);
		else
			txw = *(u16 *)(rs->tx);

		writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
		rs->tx += rs->n_bytes;
	}
}

static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
{
	u32 max = rx_max(rs);
	u32 rxw;

	while (max--) {
		rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
		if (rs->n_bytes == 1)
			*(u8 *)(rs->rx) = (u8)rxw;
		else
			*(u16 *)(rs->rx) = (u16)rxw;
		rs->rx += rs->n_bytes;
377
	}
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
}

static int rockchip_spi_pio_transfer(struct rockchip_spi *rs)
{
	int remain = 0;

	do {
		if (rs->tx) {
			remain = rs->tx_end - rs->tx;
			rockchip_spi_pio_writer(rs);
		}

		if (rs->rx) {
			remain = rs->rx_end - rs->rx;
			rockchip_spi_pio_reader(rs);
		}

		cpu_relax();
	} while (remain);

398 399 400 401
	/* If tx, wait until the FIFO data completely. */
	if (rs->tx)
		wait_for_idle(rs);

402 403
	spi_enable_chip(rs, 0);

404 405 406 407 408 409 410 411 412 413 414
	return 0;
}

static void rockchip_spi_dma_rxcb(void *data)
{
	unsigned long flags;
	struct rockchip_spi *rs = data;

	spin_lock_irqsave(&rs->lock, flags);

	rs->state &= ~RXBUSY;
415 416
	if (!(rs->state & TXBUSY)) {
		spi_enable_chip(rs, 0);
417
		spi_finalize_current_transfer(rs->master);
418
	}
419 420 421 422 423 424 425 426 427

	spin_unlock_irqrestore(&rs->lock, flags);
}

static void rockchip_spi_dma_txcb(void *data)
{
	unsigned long flags;
	struct rockchip_spi *rs = data;

428 429 430
	/* Wait until the FIFO data completely. */
	wait_for_idle(rs);

431 432 433
	spin_lock_irqsave(&rs->lock, flags);

	rs->state &= ~TXBUSY;
434 435
	if (!(rs->state & RXBUSY)) {
		spi_enable_chip(rs, 0);
436
		spi_finalize_current_transfer(rs->master);
437
	}
438 439 440 441

	spin_unlock_irqrestore(&rs->lock, flags);
}

442
static int rockchip_spi_prepare_dma(struct rockchip_spi *rs)
443 444 445 446 447 448 449 450 451 452
{
	unsigned long flags;
	struct dma_slave_config rxconf, txconf;
	struct dma_async_tx_descriptor *rxdesc, *txdesc;

	spin_lock_irqsave(&rs->lock, flags);
	rs->state &= ~RXBUSY;
	rs->state &= ~TXBUSY;
	spin_unlock_irqrestore(&rs->lock, flags);

453
	rxdesc = NULL;
454 455 456 457
	if (rs->rx) {
		rxconf.direction = rs->dma_rx.direction;
		rxconf.src_addr = rs->dma_rx.addr;
		rxconf.src_addr_width = rs->n_bytes;
458 459 460 461
		if (rs->dma_caps.max_burst > 4)
			rxconf.src_maxburst = 4;
		else
			rxconf.src_maxburst = 1;
462 463
		dmaengine_slave_config(rs->dma_rx.ch, &rxconf);

464 465
		rxdesc = dmaengine_prep_slave_sg(
				rs->dma_rx.ch,
466 467
				rs->rx_sg.sgl, rs->rx_sg.nents,
				rs->dma_rx.direction, DMA_PREP_INTERRUPT);
468 469
		if (!rxdesc)
			return -EINVAL;
470 471 472 473 474

		rxdesc->callback = rockchip_spi_dma_rxcb;
		rxdesc->callback_param = rs;
	}

475
	txdesc = NULL;
476 477 478 479
	if (rs->tx) {
		txconf.direction = rs->dma_tx.direction;
		txconf.dst_addr = rs->dma_tx.addr;
		txconf.dst_addr_width = rs->n_bytes;
480 481 482 483
		if (rs->dma_caps.max_burst > 4)
			txconf.dst_maxburst = 4;
		else
			txconf.dst_maxburst = 1;
484 485
		dmaengine_slave_config(rs->dma_tx.ch, &txconf);

486 487
		txdesc = dmaengine_prep_slave_sg(
				rs->dma_tx.ch,
488 489
				rs->tx_sg.sgl, rs->tx_sg.nents,
				rs->dma_tx.direction, DMA_PREP_INTERRUPT);
490 491 492 493 494
		if (!txdesc) {
			if (rxdesc)
				dmaengine_terminate_sync(rs->dma_rx.ch);
			return -EINVAL;
		}
495 496 497 498 499 500

		txdesc->callback = rockchip_spi_dma_txcb;
		txdesc->callback_param = rs;
	}

	/* rx must be started before tx due to spi instinct */
501
	if (rxdesc) {
502 503 504 505 506 507 508
		spin_lock_irqsave(&rs->lock, flags);
		rs->state |= RXBUSY;
		spin_unlock_irqrestore(&rs->lock, flags);
		dmaengine_submit(rxdesc);
		dma_async_issue_pending(rs->dma_rx.ch);
	}

509
	if (txdesc) {
510 511 512 513 514 515
		spin_lock_irqsave(&rs->lock, flags);
		rs->state |= TXBUSY;
		spin_unlock_irqrestore(&rs->lock, flags);
		dmaengine_submit(txdesc);
		dma_async_issue_pending(rs->dma_tx.ch);
	}
516 517

	return 0;
518 519 520 521 522 523
}

static void rockchip_spi_config(struct rockchip_spi *rs)
{
	u32 div = 0;
	u32 dmacr = 0;
524
	int rsd = 0;
525 526

	u32 cr0 = (CR0_BHT_8BIT << CR0_BHT_OFFSET)
527 528
		| (CR0_SSD_ONE << CR0_SSD_OFFSET)
		| (CR0_EM_BIG << CR0_EM_OFFSET);
529 530 531 532 533 534 535 536 537 538 539 540 541

	cr0 |= (rs->n_bytes << CR0_DFS_OFFSET);
	cr0 |= ((rs->mode & 0x3) << CR0_SCPH_OFFSET);
	cr0 |= (rs->tmode << CR0_XFM_OFFSET);
	cr0 |= (rs->type << CR0_FRF_OFFSET);

	if (rs->use_dma) {
		if (rs->tx)
			dmacr |= TF_DMA_EN;
		if (rs->rx)
			dmacr |= RF_DMA_EN;
	}

542 543 544
	if (WARN_ON(rs->speed > MAX_SCLK_OUT))
		rs->speed = MAX_SCLK_OUT;

545
	/* the minimum divisor is 2 */
546 547 548 549 550
	if (rs->max_freq < 2 * rs->speed) {
		clk_set_rate(rs->spiclk, 2 * rs->speed);
		rs->max_freq = clk_get_rate(rs->spiclk);
	}

551
	/* div doesn't support odd number */
552
	div = DIV_ROUND_UP(rs->max_freq, rs->speed);
553 554
	div = (div + 1) & 0xfffe;

555 556 557 558 559 560 561 562 563 564 565 566 567 568
	/* Rx sample delay is expressed in parent clock cycles (max 3) */
	rsd = DIV_ROUND_CLOSEST(rs->rsd_nsecs * (rs->max_freq >> 8),
				1000000000 >> 8);
	if (!rsd && rs->rsd_nsecs) {
		pr_warn_once("rockchip-spi: %u Hz are too slow to express %u ns delay\n",
			     rs->max_freq, rs->rsd_nsecs);
	} else if (rsd > 3) {
		rsd = 3;
		pr_warn_once("rockchip-spi: %u Hz are too fast to express %u ns delay, clamping at %u ns\n",
			     rs->max_freq, rs->rsd_nsecs,
			     rsd * 1000000000U / rs->max_freq);
	}
	cr0 |= rsd << CR0_RSD_OFFSET;

569 570
	writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);

571 572 573 574 575 576 577
	if (rs->n_bytes == 1)
		writel_relaxed(rs->len - 1, rs->regs + ROCKCHIP_SPI_CTRLR1);
	else if (rs->n_bytes == 2)
		writel_relaxed((rs->len / 2) - 1, rs->regs + ROCKCHIP_SPI_CTRLR1);
	else
		writel_relaxed((rs->len * 2) - 1, rs->regs + ROCKCHIP_SPI_CTRLR1);

578 579 580 581 582 583 584 585 586
	writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_TXFTLR);
	writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);

	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMATDLR);
	writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMARDLR);
	writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);

	spi_set_clk(rs, div);

587
	dev_dbg(rs->dev, "cr0 0x%x, div %d\n", cr0, div);
588 589
}

590 591 592 593 594
static size_t rockchip_spi_max_transfer_size(struct spi_device *spi)
{
	return ROCKCHIP_SPI_MAX_TRANLEN;
}

595 596
static int rockchip_spi_transfer_one(
		struct spi_master *master,
597 598 599
		struct spi_device *spi,
		struct spi_transfer *xfer)
{
600
	int ret = 0;
601 602
	struct rockchip_spi *rs = spi_master_get_devdata(master);

603 604
	WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
		(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
605 606 607 608 609 610

	if (!xfer->tx_buf && !xfer->rx_buf) {
		dev_err(rs->dev, "No buffer for transfer\n");
		return -EINVAL;
	}

611 612 613 614 615
	if (xfer->len > ROCKCHIP_SPI_MAX_TRANLEN) {
		dev_err(rs->dev, "Transfer is too long (%d)\n", xfer->len);
		return -EINVAL;
	}

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
	rs->speed = xfer->speed_hz;
	rs->bpw = xfer->bits_per_word;
	rs->n_bytes = rs->bpw >> 3;

	rs->tx = xfer->tx_buf;
	rs->tx_end = rs->tx + xfer->len;
	rs->rx = xfer->rx_buf;
	rs->rx_end = rs->rx + xfer->len;
	rs->len = xfer->len;

	rs->tx_sg = xfer->tx_sg;
	rs->rx_sg = xfer->rx_sg;

	if (rs->tx && rs->rx)
		rs->tmode = CR0_XFM_TR;
	else if (rs->tx)
		rs->tmode = CR0_XFM_TO;
	else if (rs->rx)
		rs->tmode = CR0_XFM_RO;

636
	/* we need prepare dma before spi was enabled */
637
	if (master->can_dma && master->can_dma(master, spi, xfer))
638
		rs->use_dma = 1;
639
	else
640 641 642 643
		rs->use_dma = 0;

	rockchip_spi_config(rs);

644 645 646
	if (rs->use_dma) {
		if (rs->tmode == CR0_XFM_RO) {
			/* rx: dma must be prepared first */
647
			ret = rockchip_spi_prepare_dma(rs);
648 649 650 651
			spi_enable_chip(rs, 1);
		} else {
			/* tx or tr: spi must be enabled first */
			spi_enable_chip(rs, 1);
652
			ret = rockchip_spi_prepare_dma(rs);
653
		}
654 655
		/* successful DMA prepare means the transfer is in progress */
		ret = ret ? ret : 1;
656 657
	} else {
		spi_enable_chip(rs, 1);
658
		ret = rockchip_spi_pio_transfer(rs);
659
	}
660 661 662 663 664

	return ret;
}

static bool rockchip_spi_can_dma(struct spi_master *master,
665 666
				 struct spi_device *spi,
				 struct spi_transfer *xfer)
667 668 669 670 671 672 673 674
{
	struct rockchip_spi *rs = spi_master_get_devdata(master);

	return (xfer->len > rs->fifo_len);
}

static int rockchip_spi_probe(struct platform_device *pdev)
{
675
	int ret;
676 677 678
	struct rockchip_spi *rs;
	struct spi_master *master;
	struct resource *mem;
679
	u32 rsd_nsecs;
680 681

	master = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi));
682
	if (!master)
683
		return -ENOMEM;
684

685 686 687 688 689 690 691 692 693
	platform_set_drvdata(pdev, master);

	rs = spi_master_get_devdata(master);

	/* Get basic io resource and map it */
	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	rs->regs = devm_ioremap_resource(&pdev->dev, mem);
	if (IS_ERR(rs->regs)) {
		ret =  PTR_ERR(rs->regs);
694
		goto err_put_master;
695 696 697 698 699 700
	}

	rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
	if (IS_ERR(rs->apb_pclk)) {
		dev_err(&pdev->dev, "Failed to get apb_pclk\n");
		ret = PTR_ERR(rs->apb_pclk);
701
		goto err_put_master;
702 703 704 705 706 707
	}

	rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
	if (IS_ERR(rs->spiclk)) {
		dev_err(&pdev->dev, "Failed to get spi_pclk\n");
		ret = PTR_ERR(rs->spiclk);
708
		goto err_put_master;
709 710 711
	}

	ret = clk_prepare_enable(rs->apb_pclk);
712
	if (ret < 0) {
713
		dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
714
		goto err_put_master;
715 716 717
	}

	ret = clk_prepare_enable(rs->spiclk);
718
	if (ret < 0) {
719
		dev_err(&pdev->dev, "Failed to enable spi_clk\n");
720
		goto err_disable_apbclk;
721 722 723 724 725 726 727 728 729
	}

	spi_enable_chip(rs, 0);

	rs->type = SSI_MOTO_SPI;
	rs->master = master;
	rs->dev = &pdev->dev;
	rs->max_freq = clk_get_rate(rs->spiclk);

730 731 732 733
	if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
				  &rsd_nsecs))
		rs->rsd_nsecs = rsd_nsecs;

734 735 736
	rs->fifo_len = get_fifo_len(rs);
	if (!rs->fifo_len) {
		dev_err(&pdev->dev, "Failed to get fifo length\n");
737
		ret = -EINVAL;
738
		goto err_disable_spiclk;
739 740 741 742 743 744 745 746 747
	}

	spin_lock_init(&rs->lock);

	pm_runtime_set_active(&pdev->dev);
	pm_runtime_enable(&pdev->dev);

	master->auto_runtime_pm = true;
	master->bus_num = pdev->id;
748
	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
749
	master->num_chipselect = ROCKCHIP_SPI_MAX_CS_NUM;
750 751 752 753 754 755 756
	master->dev.of_node = pdev->dev.of_node;
	master->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8);

	master->set_cs = rockchip_spi_set_cs;
	master->prepare_message = rockchip_spi_prepare_message;
	master->unprepare_message = rockchip_spi_unprepare_message;
	master->transfer_one = rockchip_spi_transfer_one;
757
	master->max_transfer_size = rockchip_spi_max_transfer_size;
758
	master->handle_err = rockchip_spi_handle_err;
759
	master->flags = SPI_MASTER_GPIO_SS;
760

761 762
	rs->dma_tx.ch = dma_request_chan(rs->dev, "tx");
	if (IS_ERR(rs->dma_tx.ch)) {
763 764 765
		/* Check tx to see if we need defer probing driver */
		if (PTR_ERR(rs->dma_tx.ch) == -EPROBE_DEFER) {
			ret = -EPROBE_DEFER;
766
			goto err_disable_pm_runtime;
767
		}
768
		dev_warn(rs->dev, "Failed to request TX DMA channel\n");
769
		rs->dma_tx.ch = NULL;
770
	}
771

772 773 774 775
	rs->dma_rx.ch = dma_request_chan(rs->dev, "rx");
	if (IS_ERR(rs->dma_rx.ch)) {
		if (PTR_ERR(rs->dma_rx.ch) == -EPROBE_DEFER) {
			ret = -EPROBE_DEFER;
776
			goto err_free_dma_tx;
777 778
		}
		dev_warn(rs->dev, "Failed to request RX DMA channel\n");
779
		rs->dma_rx.ch = NULL;
780 781 782
	}

	if (rs->dma_tx.ch && rs->dma_rx.ch) {
783
		dma_get_slave_caps(rs->dma_rx.ch, &(rs->dma_caps));
784 785 786
		rs->dma_tx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_TXDR);
		rs->dma_rx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_RXDR);
		rs->dma_tx.direction = DMA_MEM_TO_DEV;
787
		rs->dma_rx.direction = DMA_DEV_TO_MEM;
788 789 790 791 792 793 794

		master->can_dma = rockchip_spi_can_dma;
		master->dma_tx = rs->dma_tx.ch;
		master->dma_rx = rs->dma_rx.ch;
	}

	ret = devm_spi_register_master(&pdev->dev, master);
795
	if (ret < 0) {
796
		dev_err(&pdev->dev, "Failed to register master\n");
797
		goto err_free_dma_rx;
798 799 800 801
	}

	return 0;

802
err_free_dma_rx:
803 804
	if (rs->dma_rx.ch)
		dma_release_channel(rs->dma_rx.ch);
805 806 807
err_free_dma_tx:
	if (rs->dma_tx.ch)
		dma_release_channel(rs->dma_tx.ch);
808 809 810
err_disable_pm_runtime:
	pm_runtime_disable(&pdev->dev);
err_disable_spiclk:
811
	clk_disable_unprepare(rs->spiclk);
812
err_disable_apbclk:
813
	clk_disable_unprepare(rs->apb_pclk);
814
err_put_master:
815 816 817 818 819 820 821 822 823 824
	spi_master_put(master);

	return ret;
}

static int rockchip_spi_remove(struct platform_device *pdev)
{
	struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
	struct rockchip_spi *rs = spi_master_get_devdata(master);

825
	pm_runtime_get_sync(&pdev->dev);
826 827 828 829

	clk_disable_unprepare(rs->spiclk);
	clk_disable_unprepare(rs->apb_pclk);

830 831 832 833
	pm_runtime_put_noidle(&pdev->dev);
	pm_runtime_disable(&pdev->dev);
	pm_runtime_set_suspended(&pdev->dev);

834 835 836 837 838
	if (rs->dma_tx.ch)
		dma_release_channel(rs->dma_tx.ch);
	if (rs->dma_rx.ch)
		dma_release_channel(rs->dma_rx.ch);

839 840
	spi_master_put(master);

841 842 843 844 845 846
	return 0;
}

#ifdef CONFIG_PM_SLEEP
static int rockchip_spi_suspend(struct device *dev)
{
847
	int ret;
848 849 850 851
	struct spi_master *master = dev_get_drvdata(dev);
	struct rockchip_spi *rs = spi_master_get_devdata(master);

	ret = spi_master_suspend(rs->master);
852
	if (ret < 0)
853 854
		return ret;

855 856 857
	ret = pm_runtime_force_suspend(dev);
	if (ret < 0)
		return ret;
858

859 860
	pinctrl_pm_select_sleep_state(dev);

861
	return 0;
862 863 864 865
}

static int rockchip_spi_resume(struct device *dev)
{
866
	int ret;
867 868 869
	struct spi_master *master = dev_get_drvdata(dev);
	struct rockchip_spi *rs = spi_master_get_devdata(master);

870 871
	pinctrl_pm_select_default_state(dev);

872 873 874
	ret = pm_runtime_force_resume(dev);
	if (ret < 0)
		return ret;
875 876 877 878 879 880 881

	ret = spi_master_resume(rs->master);
	if (ret < 0) {
		clk_disable_unprepare(rs->spiclk);
		clk_disable_unprepare(rs->apb_pclk);
	}

882
	return 0;
883 884 885
}
#endif /* CONFIG_PM_SLEEP */

886
#ifdef CONFIG_PM
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
static int rockchip_spi_runtime_suspend(struct device *dev)
{
	struct spi_master *master = dev_get_drvdata(dev);
	struct rockchip_spi *rs = spi_master_get_devdata(master);

	clk_disable_unprepare(rs->spiclk);
	clk_disable_unprepare(rs->apb_pclk);

	return 0;
}

static int rockchip_spi_runtime_resume(struct device *dev)
{
	int ret;
	struct spi_master *master = dev_get_drvdata(dev);
	struct rockchip_spi *rs = spi_master_get_devdata(master);

	ret = clk_prepare_enable(rs->apb_pclk);
905
	if (ret < 0)
906 907 908
		return ret;

	ret = clk_prepare_enable(rs->spiclk);
909
	if (ret < 0)
910 911
		clk_disable_unprepare(rs->apb_pclk);

912
	return 0;
913
}
914
#endif /* CONFIG_PM */
915 916 917 918 919 920 921 922

static const struct dev_pm_ops rockchip_spi_pm = {
	SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
	SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
			   rockchip_spi_runtime_resume, NULL)
};

static const struct of_device_id rockchip_spi_dt_match[] = {
923
	{ .compatible = "rockchip,rv1108-spi", },
924
	{ .compatible = "rockchip,rk3036-spi", },
925
	{ .compatible = "rockchip,rk3066-spi", },
926
	{ .compatible = "rockchip,rk3188-spi", },
927
	{ .compatible = "rockchip,rk3228-spi", },
928
	{ .compatible = "rockchip,rk3288-spi", },
929
	{ .compatible = "rockchip,rk3368-spi", },
930
	{ .compatible = "rockchip,rk3399-spi", },
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
	{ },
};
MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);

static struct platform_driver rockchip_spi_driver = {
	.driver = {
		.name	= DRIVER_NAME,
		.pm = &rockchip_spi_pm,
		.of_match_table = of_match_ptr(rockchip_spi_dt_match),
	},
	.probe = rockchip_spi_probe,
	.remove = rockchip_spi_remove,
};

module_platform_driver(rockchip_spi_driver);

947
MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
948 949
MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
MODULE_LICENSE("GPL v2");