spi-rspi.c 35.6 KB
Newer Older
1 2 3
/*
 * SH RSPI driver
 *
4
 * Copyright (C) 2012, 2013  Renesas Solutions Corp.
5
 * Copyright (C) 2014 Glider bvba
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Based on spi-sh.c:
 * Copyright (C) 2011 Renesas Solutions Corp.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that 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/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/io.h>
#include <linux/clk.h>
28 29
#include <linux/dmaengine.h>
#include <linux/dma-mapping.h>
G
Geert Uytterhoeven 已提交
30
#include <linux/of_device.h>
31
#include <linux/pm_runtime.h>
32
#include <linux/sh_dma.h>
33
#include <linux/spi/spi.h>
34
#include <linux/spi/rspi.h>
35

36 37 38 39 40 41 42 43 44 45 46 47
#define RSPI_SPCR		0x00	/* Control Register */
#define RSPI_SSLP		0x01	/* Slave Select Polarity Register */
#define RSPI_SPPCR		0x02	/* Pin Control Register */
#define RSPI_SPSR		0x03	/* Status Register */
#define RSPI_SPDR		0x04	/* Data Register */
#define RSPI_SPSCR		0x08	/* Sequence Control Register */
#define RSPI_SPSSR		0x09	/* Sequence Status Register */
#define RSPI_SPBR		0x0a	/* Bit Rate Register */
#define RSPI_SPDCR		0x0b	/* Data Control Register */
#define RSPI_SPCKD		0x0c	/* Clock Delay Register */
#define RSPI_SSLND		0x0d	/* Slave Select Negation Delay Register */
#define RSPI_SPND		0x0e	/* Next-Access Delay Register */
48
#define RSPI_SPCR2		0x0f	/* Control Register 2 (SH only) */
49 50 51 52 53 54 55 56
#define RSPI_SPCMD0		0x10	/* Command Register 0 */
#define RSPI_SPCMD1		0x12	/* Command Register 1 */
#define RSPI_SPCMD2		0x14	/* Command Register 2 */
#define RSPI_SPCMD3		0x16	/* Command Register 3 */
#define RSPI_SPCMD4		0x18	/* Command Register 4 */
#define RSPI_SPCMD5		0x1a	/* Command Register 5 */
#define RSPI_SPCMD6		0x1c	/* Command Register 6 */
#define RSPI_SPCMD7		0x1e	/* Command Register 7 */
57 58 59 60
#define RSPI_SPCMD(i)		(RSPI_SPCMD0 + (i) * 2)
#define RSPI_NUM_SPCMD		8
#define RSPI_RZ_NUM_SPCMD	4
#define QSPI_NUM_SPCMD		4
61 62

/* RSPI on RZ only */
63 64
#define RSPI_SPBFCR		0x20	/* Buffer Control Register */
#define RSPI_SPBFDR		0x22	/* Buffer Data Count Setting Register */
65

66
/* QSPI only */
67 68 69 70 71 72
#define QSPI_SPBFCR		0x18	/* Buffer Control Register */
#define QSPI_SPBDCR		0x1a	/* Buffer Data Count Register */
#define QSPI_SPBMUL0		0x1c	/* Transfer Data Length Multiplier Setting Register 0 */
#define QSPI_SPBMUL1		0x20	/* Transfer Data Length Multiplier Setting Register 1 */
#define QSPI_SPBMUL2		0x24	/* Transfer Data Length Multiplier Setting Register 2 */
#define QSPI_SPBMUL3		0x28	/* Transfer Data Length Multiplier Setting Register 3 */
73
#define QSPI_SPBMUL(i)		(QSPI_SPBMUL0 + (i) * 4)
74

75 76 77 78 79 80 81 82 83 84
/* SPCR - Control Register */
#define SPCR_SPRIE		0x80	/* Receive Interrupt Enable */
#define SPCR_SPE		0x40	/* Function Enable */
#define SPCR_SPTIE		0x20	/* Transmit Interrupt Enable */
#define SPCR_SPEIE		0x10	/* Error Interrupt Enable */
#define SPCR_MSTR		0x08	/* Master/Slave Mode Select */
#define SPCR_MODFEN		0x04	/* Mode Fault Error Detection Enable */
/* RSPI on SH only */
#define SPCR_TXMD		0x02	/* TX Only Mode (vs. Full Duplex) */
#define SPCR_SPMS		0x01	/* 3-wire Mode (vs. 4-wire) */
85
/* QSPI on R-Car Gen2 only */
86 87
#define SPCR_WSWAP		0x02	/* Word Swap of read-data for DMAC */
#define SPCR_BSWAP		0x01	/* Byte Swap of read-data for DMAC */
88 89 90 91 92 93 94 95

/* SSLP - Slave Select Polarity Register */
#define SSLP_SSL1P		0x02	/* SSL1 Signal Polarity Setting */
#define SSLP_SSL0P		0x01	/* SSL0 Signal Polarity Setting */

/* SPPCR - Pin Control Register */
#define SPPCR_MOIFE		0x20	/* MOSI Idle Value Fixing Enable */
#define SPPCR_MOIFV		0x10	/* MOSI Idle Fixed Value */
96
#define SPPCR_SPOM		0x04
97 98 99
#define SPPCR_SPLP2		0x02	/* Loopback Mode 2 (non-inverting) */
#define SPPCR_SPLP		0x01	/* Loopback Mode (inverting) */

100 101 102
#define SPPCR_IO3FV		0x04	/* Single-/Dual-SPI Mode IO3 Output Fixed Value */
#define SPPCR_IO2FV		0x04	/* Single-/Dual-SPI Mode IO2 Output Fixed Value */

103 104 105 106 107 108 109
/* SPSR - Status Register */
#define SPSR_SPRF		0x80	/* Receive Buffer Full Flag */
#define SPSR_TEND		0x40	/* Transmit End */
#define SPSR_SPTEF		0x20	/* Transmit Buffer Empty Flag */
#define SPSR_PERF		0x08	/* Parity Error Flag */
#define SPSR_MODF		0x04	/* Mode Fault Error Flag */
#define SPSR_IDLNF		0x02	/* RSPI Idle Flag */
110
#define SPSR_OVRF		0x01	/* Overrun Error Flag (RSPI only) */
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

/* SPSCR - Sequence Control Register */
#define SPSCR_SPSLN_MASK	0x07	/* Sequence Length Specification */

/* SPSSR - Sequence Status Register */
#define SPSSR_SPECM_MASK	0x70	/* Command Error Mask */
#define SPSSR_SPCP_MASK		0x07	/* Command Pointer Mask */

/* SPDCR - Data Control Register */
#define SPDCR_TXDMY		0x80	/* Dummy Data Transmission Enable */
#define SPDCR_SPLW1		0x40	/* Access Width Specification (RZ) */
#define SPDCR_SPLW0		0x20	/* Access Width Specification (RZ) */
#define SPDCR_SPLLWORD		(SPDCR_SPLW1 | SPDCR_SPLW0)
#define SPDCR_SPLWORD		SPDCR_SPLW1
#define SPDCR_SPLBYTE		SPDCR_SPLW0
#define SPDCR_SPLW		0x20	/* Access Width Specification (SH) */
127
#define SPDCR_SPRDTD		0x10	/* Receive Transmit Data Select (SH) */
128 129
#define SPDCR_SLSEL1		0x08
#define SPDCR_SLSEL0		0x04
130
#define SPDCR_SLSEL_MASK	0x0c	/* SSL1 Output Select (SH) */
131 132
#define SPDCR_SPFC1		0x02
#define SPDCR_SPFC0		0x01
133
#define SPDCR_SPFC_MASK		0x03	/* Frame Count Setting (1-4) (SH) */
134

135 136
/* SPCKD - Clock Delay Register */
#define SPCKD_SCKDL_MASK	0x07	/* Clock Delay Setting (1-8) */
137

138 139
/* SSLND - Slave Select Negation Delay Register */
#define SSLND_SLNDL_MASK	0x07	/* SSL Negation Delay Setting (1-8) */
140

141 142
/* SPND - Next-Access Delay Register */
#define SPND_SPNDL_MASK		0x07	/* Next-Access Delay Setting (1-8) */
143

144 145 146 147 148
/* SPCR2 - Control Register 2 */
#define SPCR2_PTE		0x08	/* Parity Self-Test Enable */
#define SPCR2_SPIE		0x04	/* Idle Interrupt Enable */
#define SPCR2_SPOE		0x02	/* Odd Parity Enable (vs. Even) */
#define SPCR2_SPPE		0x01	/* Parity Enable */
149

150 151 152 153 154 155
/* SPCMDn - Command Registers */
#define SPCMD_SCKDEN		0x8000	/* Clock Delay Setting Enable */
#define SPCMD_SLNDEN		0x4000	/* SSL Negation Delay Setting Enable */
#define SPCMD_SPNDEN		0x2000	/* Next-Access Delay Enable */
#define SPCMD_LSBF		0x1000	/* LSB First */
#define SPCMD_SPB_MASK		0x0f00	/* Data Length Setting */
156
#define SPCMD_SPB_8_TO_16(bit)	(((bit - 1) << 8) & SPCMD_SPB_MASK)
157
#define SPCMD_SPB_8BIT		0x0000	/* QSPI only */
158
#define SPCMD_SPB_16BIT		0x0100
159 160 161
#define SPCMD_SPB_20BIT		0x0000
#define SPCMD_SPB_24BIT		0x0100
#define SPCMD_SPB_32BIT		0x0200
162
#define SPCMD_SSLKP		0x0080	/* SSL Signal Level Keeping */
163 164 165 166 167 168 169
#define SPCMD_SPIMOD_MASK	0x0060	/* SPI Operating Mode (QSPI only) */
#define SPCMD_SPIMOD1		0x0040
#define SPCMD_SPIMOD0		0x0020
#define SPCMD_SPIMOD_SINGLE	0
#define SPCMD_SPIMOD_DUAL	SPCMD_SPIMOD0
#define SPCMD_SPIMOD_QUAD	SPCMD_SPIMOD1
#define SPCMD_SPRW		0x0010	/* SPI Read/Write Access (Dual/Quad) */
170 171 172 173 174 175
#define SPCMD_SSLA_MASK		0x0030	/* SSL Assert Signal Setting (RSPI) */
#define SPCMD_BRDV_MASK		0x000c	/* Bit Rate Division Setting */
#define SPCMD_CPOL		0x0002	/* Clock Polarity Setting */
#define SPCMD_CPHA		0x0001	/* Clock Phase Setting */

/* SPBFCR - Buffer Control Register */
176 177
#define SPBFCR_TXRST		0x80	/* Transmit Buffer Data Reset */
#define SPBFCR_RXRST		0x40	/* Receive Buffer Data Reset */
178 179
#define SPBFCR_TXTRG_MASK	0x30	/* Transmit Buffer Data Triggering Number */
#define SPBFCR_RXTRG_MASK	0x07	/* Receive Buffer Data Triggering Number */
180 181 182 183 184 185 186
/* QSPI on R-Car Gen2 */
#define SPBFCR_TXTRG_1B		0x00	/* 31 bytes (1 byte available) */
#define SPBFCR_TXTRG_32B	0x30	/* 0 byte (32 bytes available) */
#define SPBFCR_RXTRG_1B		0x00	/* 1 byte (31 bytes available) */
#define SPBFCR_RXTRG_32B	0x07	/* 32 bytes (0 byte available) */

#define QSPI_BUFFER_SIZE        32u
187

188 189 190 191 192 193
struct rspi_data {
	void __iomem *addr;
	u32 max_speed_hz;
	struct spi_master *master;
	wait_queue_head_t wait;
	struct clk *clk;
194
	u16 spcmd;
195 196
	u8 spsr;
	u8 sppcr;
197
	int rx_irq, tx_irq;
198
	const struct spi_ops *ops;
199 200

	unsigned dma_callbacked:1;
201
	unsigned byte_access:1;
202 203
};

204
static void rspi_write8(const struct rspi_data *rspi, u8 data, u16 offset)
205 206 207 208
{
	iowrite8(data, rspi->addr + offset);
}

209
static void rspi_write16(const struct rspi_data *rspi, u16 data, u16 offset)
210 211 212 213
{
	iowrite16(data, rspi->addr + offset);
}

214
static void rspi_write32(const struct rspi_data *rspi, u32 data, u16 offset)
215 216 217 218
{
	iowrite32(data, rspi->addr + offset);
}

219
static u8 rspi_read8(const struct rspi_data *rspi, u16 offset)
220 221 222 223
{
	return ioread8(rspi->addr + offset);
}

224
static u16 rspi_read16(const struct rspi_data *rspi, u16 offset)
225 226 227 228
{
	return ioread16(rspi->addr + offset);
}

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
static void rspi_write_data(const struct rspi_data *rspi, u16 data)
{
	if (rspi->byte_access)
		rspi_write8(rspi, data, RSPI_SPDR);
	else /* 16 bit */
		rspi_write16(rspi, data, RSPI_SPDR);
}

static u16 rspi_read_data(const struct rspi_data *rspi)
{
	if (rspi->byte_access)
		return rspi_read8(rspi, RSPI_SPDR);
	else /* 16 bit */
		return rspi_read16(rspi, RSPI_SPDR);
}

245 246
/* optional functions */
struct spi_ops {
247
	int (*set_config_register)(struct rspi_data *rspi, int access_size);
248 249
	int (*transfer_one)(struct spi_master *master, struct spi_device *spi,
			    struct spi_transfer *xfer);
250
	u16 mode_bits;
251
	u16 flags;
252
	u16 fifo_size;
253 254 255
};

/*
256
 * functions for RSPI on legacy SH
257
 */
258
static int rspi_set_config_register(struct rspi_data *rspi, int access_size)
259
{
260 261
	int spbr;

262 263
	/* Sets output mode, MOSI signal, and (optionally) loopback */
	rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
264

265
	/* Sets transfer bit rate */
266 267
	spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk),
			    2 * rspi->max_speed_hz) - 1;
268 269
	rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);

270 271 272
	/* Disable dummy transmission, set 16-bit word access, 1 frame */
	rspi_write8(rspi, 0, RSPI_SPDCR);
	rspi->byte_access = 0;
273

274 275 276 277 278 279 280 281 282
	/* Sets RSPCK, SSL, next-access delay value */
	rspi_write8(rspi, 0x00, RSPI_SPCKD);
	rspi_write8(rspi, 0x00, RSPI_SSLND);
	rspi_write8(rspi, 0x00, RSPI_SPND);

	/* Sets parity, interrupt mask */
	rspi_write8(rspi, 0x00, RSPI_SPCR2);

	/* Sets SPCMD */
283 284
	rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size);
	rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
285 286 287 288 289

	/* Sets RSPI mode */
	rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);

	return 0;
290 291
}

292 293 294 295 296 297
/*
 * functions for RSPI on RZ
 */
static int rspi_rz_set_config_register(struct rspi_data *rspi, int access_size)
{
	int spbr;
298 299
	int div = 0;
	unsigned long clksrc;
300

301 302
	/* Sets output mode, MOSI signal, and (optionally) loopback */
	rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
303

304 305 306 307 308 309 310 311
	clksrc = clk_get_rate(rspi->clk);
	while (div < 3) {
		if (rspi->max_speed_hz >= clksrc/4) /* 4=(CLK/2)/2 */
			break;
		div++;
		clksrc /= 2;
	}

312
	/* Sets transfer bit rate */
313
	spbr = DIV_ROUND_UP(clksrc, 2 * rspi->max_speed_hz) - 1;
314
	rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
315
	rspi->spcmd |= div << 2;
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

	/* Disable dummy transmission, set byte access */
	rspi_write8(rspi, SPDCR_SPLBYTE, RSPI_SPDCR);
	rspi->byte_access = 1;

	/* Sets RSPCK, SSL, next-access delay value */
	rspi_write8(rspi, 0x00, RSPI_SPCKD);
	rspi_write8(rspi, 0x00, RSPI_SSLND);
	rspi_write8(rspi, 0x00, RSPI_SPND);

	/* Sets SPCMD */
	rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size);
	rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);

	/* Sets RSPI mode */
	rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);

	return 0;
}

336 337 338
/*
 * functions for QSPI
 */
339
static int qspi_set_config_register(struct rspi_data *rspi, int access_size)
340 341 342
{
	int spbr;

343 344
	/* Sets output mode, MOSI signal, and (optionally) loopback */
	rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
345 346

	/* Sets transfer bit rate */
347
	spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk), 2 * rspi->max_speed_hz);
348 349
	rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);

350 351 352
	/* Disable dummy transmission, set byte access */
	rspi_write8(rspi, 0, RSPI_SPDCR);
	rspi->byte_access = 1;
353 354 355 356 357 358 359 360

	/* Sets RSPCK, SSL, next-access delay value */
	rspi_write8(rspi, 0x00, RSPI_SPCKD);
	rspi_write8(rspi, 0x00, RSPI_SSLND);
	rspi_write8(rspi, 0x00, RSPI_SPND);

	/* Data Length Setting */
	if (access_size == 8)
361
		rspi->spcmd |= SPCMD_SPB_8BIT;
362
	else if (access_size == 16)
363
		rspi->spcmd |= SPCMD_SPB_16BIT;
364
	else
365
		rspi->spcmd |= SPCMD_SPB_32BIT;
366

367
	rspi->spcmd |= SPCMD_SCKDEN | SPCMD_SLNDEN | SPCMD_SPNDEN;
368 369 370 371 372 373 374 375 376 377

	/* Resets transfer data length */
	rspi_write32(rspi, 0, QSPI_SPBMUL0);

	/* Resets transmit and receive buffer */
	rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
	/* Sets buffer to allow normal operation */
	rspi_write8(rspi, 0x00, QSPI_SPBFCR);

	/* Sets SPCMD */
378
	rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
379

380 381
	/* Sets RSPI mode */
	rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
382 383 384 385

	return 0;
}

386 387 388 389 390 391 392 393 394 395
static void qspi_update(const struct rspi_data *rspi, u8 mask, u8 val, u8 reg)
{
	u8 data;

	data = rspi_read8(rspi, reg);
	data &= ~mask;
	data |= (val & mask);
	rspi_write8(rspi, data, reg);
}

396 397
static unsigned int qspi_set_send_trigger(struct rspi_data *rspi,
					  unsigned int len)
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
{
	unsigned int n;

	n = min(len, QSPI_BUFFER_SIZE);

	if (len >= QSPI_BUFFER_SIZE) {
		/* sets triggering number to 32 bytes */
		qspi_update(rspi, SPBFCR_TXTRG_MASK,
			     SPBFCR_TXTRG_32B, QSPI_SPBFCR);
	} else {
		/* sets triggering number to 1 byte */
		qspi_update(rspi, SPBFCR_TXTRG_MASK,
			     SPBFCR_TXTRG_1B, QSPI_SPBFCR);
	}

	return n;
}

416
static int qspi_set_receive_trigger(struct rspi_data *rspi, unsigned int len)
417 418 419 420 421 422 423 424 425 426 427 428 429 430
{
	unsigned int n;

	n = min(len, QSPI_BUFFER_SIZE);

	if (len >= QSPI_BUFFER_SIZE) {
		/* sets triggering number to 32 bytes */
		qspi_update(rspi, SPBFCR_RXTRG_MASK,
			     SPBFCR_RXTRG_32B, QSPI_SPBFCR);
	} else {
		/* sets triggering number to 1 byte */
		qspi_update(rspi, SPBFCR_RXTRG_MASK,
			     SPBFCR_RXTRG_1B, QSPI_SPBFCR);
	}
431
	return n;
432 433
}

434 435
#define set_config_register(spi, n) spi->ops->set_config_register(spi, n)

436
static void rspi_enable_irq(const struct rspi_data *rspi, u8 enable)
437 438 439 440
{
	rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | enable, RSPI_SPCR);
}

441
static void rspi_disable_irq(const struct rspi_data *rspi, u8 disable)
442 443 444 445 446 447 448 449 450 451
{
	rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~disable, RSPI_SPCR);
}

static int rspi_wait_for_interrupt(struct rspi_data *rspi, u8 wait_mask,
				   u8 enable_bit)
{
	int ret;

	rspi->spsr = rspi_read8(rspi, RSPI_SPSR);
452 453 454
	if (rspi->spsr & wait_mask)
		return 0;

455 456 457 458 459 460 461 462
	rspi_enable_irq(rspi, enable_bit);
	ret = wait_event_timeout(rspi->wait, rspi->spsr & wait_mask, HZ);
	if (ret == 0 && !(rspi->spsr & wait_mask))
		return -ETIMEDOUT;

	return 0;
}

463 464 465 466 467 468 469 470 471 472
static inline int rspi_wait_for_tx_empty(struct rspi_data *rspi)
{
	return rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
}

static inline int rspi_wait_for_rx_full(struct rspi_data *rspi)
{
	return rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE);
}

473 474
static int rspi_data_out(struct rspi_data *rspi, u8 data)
{
475 476
	int error = rspi_wait_for_tx_empty(rspi);
	if (error < 0) {
477
		dev_err(&rspi->master->dev, "transmit timeout\n");
478
		return error;
479 480 481 482 483 484 485
	}
	rspi_write_data(rspi, data);
	return 0;
}

static int rspi_data_in(struct rspi_data *rspi)
{
486
	int error;
487 488
	u8 data;

489 490
	error = rspi_wait_for_rx_full(rspi);
	if (error < 0) {
491
		dev_err(&rspi->master->dev, "receive timeout\n");
492
		return error;
493 494 495 496 497
	}
	data = rspi_read_data(rspi);
	return data;
}

498 499
static int rspi_pio_transfer(struct rspi_data *rspi, const u8 *tx, u8 *rx,
			     unsigned int n)
500
{
501 502 503 504 505 506 507 508 509 510 511 512 513
	while (n-- > 0) {
		if (tx) {
			int ret = rspi_data_out(rspi, *tx++);
			if (ret < 0)
				return ret;
		}
		if (rx) {
			int ret = rspi_data_in(rspi);
			if (ret < 0)
				return ret;
			*rx++ = ret;
		}
	}
514

515
	return 0;
516 517
}

518 519 520 521 522 523 524 525
static void rspi_dma_complete(void *arg)
{
	struct rspi_data *rspi = arg;

	rspi->dma_callbacked = 1;
	wake_up_interruptible(&rspi->wait);
}

526 527
static int rspi_dma_transfer(struct rspi_data *rspi, struct sg_table *tx,
			     struct sg_table *rx)
528
{
529 530 531 532
	struct dma_async_tx_descriptor *desc_tx = NULL, *desc_rx = NULL;
	u8 irq_mask = 0;
	unsigned int other_irq = 0;
	dma_cookie_t cookie;
533
	int ret;
534

535
	/* First prepare and submit the DMA request(s), as this may fail */
536 537
	if (rx) {
		desc_rx = dmaengine_prep_slave_sg(rspi->master->dma_rx,
538
					rx->sgl, rx->nents, DMA_DEV_TO_MEM,
539
					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
540 541 542 543 544 545 546 547 548 549 550 551
		if (!desc_rx) {
			ret = -EAGAIN;
			goto no_dma_rx;
		}

		desc_rx->callback = rspi_dma_complete;
		desc_rx->callback_param = rspi;
		cookie = dmaengine_submit(desc_rx);
		if (dma_submit_error(cookie)) {
			ret = cookie;
			goto no_dma_rx;
		}
552 553 554

		irq_mask |= SPCR_SPRIE;
	}
555

556 557
	if (tx) {
		desc_tx = dmaengine_prep_slave_sg(rspi->master->dma_tx,
558
					tx->sgl, tx->nents, DMA_MEM_TO_DEV,
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
		if (!desc_tx) {
			ret = -EAGAIN;
			goto no_dma_tx;
		}

		if (rx) {
			/* No callback */
			desc_tx->callback = NULL;
		} else {
			desc_tx->callback = rspi_dma_complete;
			desc_tx->callback_param = rspi;
		}
		cookie = dmaengine_submit(desc_tx);
		if (dma_submit_error(cookie)) {
			ret = cookie;
			goto no_dma_tx;
		}

		irq_mask |= SPCR_SPTIE;
	}

581
	/*
582
	 * DMAC needs SPxIE, but if SPxIE is set, the IRQ routine will be
583 584
	 * called. So, this driver disables the IRQ while DMA transfer.
	 */
585 586 587 588
	if (tx)
		disable_irq(other_irq = rspi->tx_irq);
	if (rx && rspi->rx_irq != other_irq)
		disable_irq(rspi->rx_irq);
589

590
	rspi_enable_irq(rspi, irq_mask);
591 592
	rspi->dma_callbacked = 0;

593 594
	/* Now start DMA */
	if (rx)
595
		dma_async_issue_pending(rspi->master->dma_rx);
596
	if (tx)
597
		dma_async_issue_pending(rspi->master->dma_tx);
598 599 600

	ret = wait_event_interruptible_timeout(rspi->wait,
					       rspi->dma_callbacked, HZ);
601
	if (ret > 0 && rspi->dma_callbacked) {
602
		ret = 0;
603 604 605 606 607
	} else {
		if (!ret) {
			dev_err(&rspi->master->dev, "DMA timeout\n");
			ret = -ETIMEDOUT;
		}
608 609 610 611 612
		if (tx)
			dmaengine_terminate_all(rspi->master->dma_tx);
		if (rx)
			dmaengine_terminate_all(rspi->master->dma_rx);
	}
613

614 615 616 617 618 619 620
	rspi_disable_irq(rspi, irq_mask);

	if (tx)
		enable_irq(rspi->tx_irq);
	if (rx && rspi->rx_irq != other_irq)
		enable_irq(rspi->rx_irq);

621
	return ret;
622

623 624 625 626 627 628 629 630 631 632
no_dma_tx:
	if (rx)
		dmaengine_terminate_all(rspi->master->dma_rx);
no_dma_rx:
	if (ret == -EAGAIN) {
		pr_warn_once("%s %s: DMA not available, falling back to PIO\n",
			     dev_driver_string(&rspi->master->dev),
			     dev_name(&rspi->master->dev));
	}
	return ret;
633 634
}

635
static void rspi_receive_init(const struct rspi_data *rspi)
636
{
637
	u8 spsr;
638 639 640

	spsr = rspi_read8(rspi, RSPI_SPSR);
	if (spsr & SPSR_SPRF)
641
		rspi_read_data(rspi);	/* dummy read */
642 643
	if (spsr & SPSR_OVRF)
		rspi_write8(rspi, rspi_read8(rspi, RSPI_SPSR) & ~SPSR_OVRF,
644
			    RSPI_SPSR);
645 646
}

647 648 649 650 651 652 653
static void rspi_rz_receive_init(const struct rspi_data *rspi)
{
	rspi_receive_init(rspi);
	rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, RSPI_SPBFCR);
	rspi_write8(rspi, 0, RSPI_SPBFCR);
}

654
static void qspi_receive_init(const struct rspi_data *rspi)
655
{
656
	u8 spsr;
657 658 659

	spsr = rspi_read8(rspi, RSPI_SPSR);
	if (spsr & SPSR_SPRF)
660
		rspi_read_data(rspi);   /* dummy read */
661
	rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
662
	rspi_write8(rspi, 0, QSPI_SPBFCR);
663 664
}

665 666
static bool __rspi_can_dma(const struct rspi_data *rspi,
			   const struct spi_transfer *xfer)
667
{
668 669
	return xfer->len > rspi->ops->fifo_size;
}
670

671 672 673 674 675 676
static bool rspi_can_dma(struct spi_master *master, struct spi_device *spi,
			 struct spi_transfer *xfer)
{
	struct rspi_data *rspi = spi_master_get_devdata(master);

	return __rspi_can_dma(rspi, xfer);
677 678
}

679 680
static int rspi_dma_check_then_transfer(struct rspi_data *rspi,
					 struct spi_transfer *xfer)
681
{
682 683
	if (!rspi->master->can_dma || !__rspi_can_dma(rspi, xfer))
		return -EAGAIN;
684

685 686 687
	/* rx_buf can be NULL on RSPI on SH in TX-only Mode */
	return rspi_dma_transfer(rspi, &xfer->tx_sg,
				xfer->rx_buf ? &xfer->rx_sg : NULL);
688 689 690 691 692 693 694 695 696 697 698
}

static int rspi_common_transfer(struct rspi_data *rspi,
				struct spi_transfer *xfer)
{
	int ret;

	ret = rspi_dma_check_then_transfer(rspi, xfer);
	if (ret != -EAGAIN)
		return ret;

699 700 701 702 703 704 705 706 707 708
	ret = rspi_pio_transfer(rspi, xfer->tx_buf, xfer->rx_buf, xfer->len);
	if (ret < 0)
		return ret;

	/* Wait for the last transmission */
	rspi_wait_for_tx_empty(rspi);

	return 0;
}

709 710
static int rspi_transfer_one(struct spi_master *master, struct spi_device *spi,
			     struct spi_transfer *xfer)
711
{
712
	struct rspi_data *rspi = spi_master_get_devdata(master);
713
	u8 spcr;
714 715

	spcr = rspi_read8(rspi, RSPI_SPCR);
716
	if (xfer->rx_buf) {
717
		rspi_receive_init(rspi);
718
		spcr &= ~SPCR_TXMD;
719
	} else {
720
		spcr |= SPCR_TXMD;
721
	}
722 723
	rspi_write8(rspi, spcr, RSPI_SPCR);

724
	return rspi_common_transfer(rspi, xfer);
725 726
}

727 728 729
static int rspi_rz_transfer_one(struct spi_master *master,
				struct spi_device *spi,
				struct spi_transfer *xfer)
730
{
731
	struct rspi_data *rspi = spi_master_get_devdata(master);
732 733 734

	rspi_rz_receive_init(rspi);

735
	return rspi_common_transfer(rspi, xfer);
736 737
}

738
static int qspi_trigger_transfer_out_in(struct rspi_data *rspi, const u8 *tx,
739 740
					u8 *rx, unsigned int len)
{
741 742
	unsigned int i, n;
	int ret;
743 744 745 746 747

	while (len > 0) {
		n = qspi_set_send_trigger(rspi, len);
		qspi_set_receive_trigger(rspi, len);
		if (n == QSPI_BUFFER_SIZE) {
748 749
			ret = rspi_wait_for_tx_empty(rspi);
			if (ret < 0) {
750
				dev_err(&rspi->master->dev, "transmit timeout\n");
751
				return ret;
752 753 754 755
			}
			for (i = 0; i < n; i++)
				rspi_write_data(rspi, *tx++);

756 757
			ret = rspi_wait_for_rx_full(rspi);
			if (ret < 0) {
758
				dev_err(&rspi->master->dev, "receive timeout\n");
759
				return ret;
760 761 762 763 764 765 766 767 768 769 770 771 772 773
			}
			for (i = 0; i < n; i++)
				*rx++ = rspi_read_data(rspi);
		} else {
			ret = rspi_pio_transfer(rspi, tx, rx, n);
			if (ret < 0)
				return ret;
		}
		len -= n;
	}

	return 0;
}

774 775
static int qspi_transfer_out_in(struct rspi_data *rspi,
				struct spi_transfer *xfer)
776
{
777 778
	int ret;

779 780
	qspi_receive_init(rspi);

781 782 783 784
	ret = rspi_dma_check_then_transfer(rspi, xfer);
	if (ret != -EAGAIN)
		return ret;

785
	return qspi_trigger_transfer_out_in(rspi, xfer->tx_buf,
786
					    xfer->rx_buf, xfer->len);
787 788
}

789 790
static int qspi_transfer_out(struct rspi_data *rspi, struct spi_transfer *xfer)
{
791 792 793
	const u8 *tx = xfer->tx_buf;
	unsigned int n = xfer->len;
	unsigned int i, len;
794 795
	int ret;

796 797 798 799 800
	if (rspi->master->can_dma && __rspi_can_dma(rspi, xfer)) {
		ret = rspi_dma_transfer(rspi, &xfer->tx_sg, NULL);
		if (ret != -EAGAIN)
			return ret;
	}
801

802 803 804 805 806 807 808 809 810 811 812
	while (n > 0) {
		len = qspi_set_send_trigger(rspi, n);
		if (len == QSPI_BUFFER_SIZE) {
			ret = rspi_wait_for_tx_empty(rspi);
			if (ret < 0) {
				dev_err(&rspi->master->dev, "transmit timeout\n");
				return ret;
			}
			for (i = 0; i < len; i++)
				rspi_write_data(rspi, *tx++);
		} else {
813
			ret = rspi_pio_transfer(rspi, tx, NULL, len);
814 815 816 817 818
			if (ret < 0)
				return ret;
		}
		n -= len;
	}
819 820

	/* Wait for the last transmission */
821
	rspi_wait_for_tx_empty(rspi);
822 823 824 825 826 827

	return 0;
}

static int qspi_transfer_in(struct rspi_data *rspi, struct spi_transfer *xfer)
{
828 829 830 831 832
	u8 *rx = xfer->rx_buf;
	unsigned int n = xfer->len;
	unsigned int i, len;
	int ret;

833 834 835 836 837
	if (rspi->master->can_dma && __rspi_can_dma(rspi, xfer)) {
		int ret = rspi_dma_transfer(rspi, NULL, &xfer->rx_sg);
		if (ret != -EAGAIN)
			return ret;
	}
838

839 840 841 842 843 844 845 846 847 848 849
	while (n > 0) {
		len = qspi_set_receive_trigger(rspi, n);
		if (len == QSPI_BUFFER_SIZE) {
			ret = rspi_wait_for_rx_full(rspi);
			if (ret < 0) {
				dev_err(&rspi->master->dev, "receive timeout\n");
				return ret;
			}
			for (i = 0; i < len; i++)
				*rx++ = rspi_read_data(rspi);
		} else {
850
			ret = rspi_pio_transfer(rspi, NULL, rx, len);
851 852 853 854 855 856 857
			if (ret < 0)
				return ret;
		}
		n -= len;
	}

	return 0;
858 859
}

860 861 862 863 864
static int qspi_transfer_one(struct spi_master *master, struct spi_device *spi,
			     struct spi_transfer *xfer)
{
	struct rspi_data *rspi = spi_master_get_devdata(master);

865 866
	if (spi->mode & SPI_LOOP) {
		return qspi_transfer_out_in(rspi, xfer);
867
	} else if (xfer->tx_nbits > SPI_NBITS_SINGLE) {
868 869
		/* Quad or Dual SPI Write */
		return qspi_transfer_out(rspi, xfer);
870
	} else if (xfer->rx_nbits > SPI_NBITS_SINGLE) {
871 872 873 874 875 876
		/* Quad or Dual SPI Read */
		return qspi_transfer_in(rspi, xfer);
	} else {
		/* Single SPI Transfer */
		return qspi_transfer_out_in(rspi, xfer);
	}
877 878 879 880 881 882 883 884
}

static int rspi_setup(struct spi_device *spi)
{
	struct rspi_data *rspi = spi_master_get_devdata(spi->master);

	rspi->max_speed_hz = spi->max_speed_hz;

885 886 887 888 889 890
	rspi->spcmd = SPCMD_SSLKP;
	if (spi->mode & SPI_CPOL)
		rspi->spcmd |= SPCMD_CPOL;
	if (spi->mode & SPI_CPHA)
		rspi->spcmd |= SPCMD_CPHA;

891 892 893 894 895
	/* CMOS output mode and MOSI signal from previous transfer */
	rspi->sppcr = 0;
	if (spi->mode & SPI_LOOP)
		rspi->sppcr |= SPPCR_SPLP;

896
	set_config_register(rspi, 8);
897 898 899 900

	return 0;
}

901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 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
static u16 qspi_transfer_mode(const struct spi_transfer *xfer)
{
	if (xfer->tx_buf)
		switch (xfer->tx_nbits) {
		case SPI_NBITS_QUAD:
			return SPCMD_SPIMOD_QUAD;
		case SPI_NBITS_DUAL:
			return SPCMD_SPIMOD_DUAL;
		default:
			return 0;
		}
	if (xfer->rx_buf)
		switch (xfer->rx_nbits) {
		case SPI_NBITS_QUAD:
			return SPCMD_SPIMOD_QUAD | SPCMD_SPRW;
		case SPI_NBITS_DUAL:
			return SPCMD_SPIMOD_DUAL | SPCMD_SPRW;
		default:
			return 0;
		}

	return 0;
}

static int qspi_setup_sequencer(struct rspi_data *rspi,
				const struct spi_message *msg)
{
	const struct spi_transfer *xfer;
	unsigned int i = 0, len = 0;
	u16 current_mode = 0xffff, mode;

	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
		mode = qspi_transfer_mode(xfer);
		if (mode == current_mode) {
			len += xfer->len;
			continue;
		}

		/* Transfer mode change */
		if (i) {
			/* Set transfer data length of previous transfer */
			rspi_write32(rspi, len, QSPI_SPBMUL(i - 1));
		}

		if (i >= QSPI_NUM_SPCMD) {
			dev_err(&msg->spi->dev,
				"Too many different transfer modes");
			return -EINVAL;
		}

		/* Program transfer mode for this transfer */
		rspi_write16(rspi, rspi->spcmd | mode, RSPI_SPCMD(i));
		current_mode = mode;
		len = xfer->len;
		i++;
	}
	if (i) {
		/* Set final transfer data length and sequence length */
		rspi_write32(rspi, len, QSPI_SPBMUL(i - 1));
		rspi_write8(rspi, i - 1, RSPI_SPSCR);
	}

	return 0;
}

966
static int rspi_prepare_message(struct spi_master *master,
967
				struct spi_message *msg)
968 969
{
	struct rspi_data *rspi = spi_master_get_devdata(master);
970
	int ret;
971

972 973 974 975 976 977 978 979 980
	if (msg->spi->mode &
	    (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)) {
		/* Setup sequencer for messages with multiple transfer modes */
		ret = qspi_setup_sequencer(rspi, msg);
		if (ret < 0)
			return ret;
	}

	/* Enable SPI function in master mode */
981
	rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_SPE, RSPI_SPCR);
982 983 984
	return 0;
}

985
static int rspi_unprepare_message(struct spi_master *master,
986
				  struct spi_message *msg)
987
{
988 989
	struct rspi_data *rspi = spi_master_get_devdata(master);

990
	/* Disable SPI function */
991
	rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_SPE, RSPI_SPCR);
992 993 994 995

	/* Reset sequencer for Single SPI Transfers */
	rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
	rspi_write8(rspi, 0, RSPI_SPSCR);
996
	return 0;
997 998
}

999
static irqreturn_t rspi_irq_mux(int irq, void *_sr)
1000
{
G
Geert Uytterhoeven 已提交
1001
	struct rspi_data *rspi = _sr;
1002
	u8 spsr;
1003
	irqreturn_t ret = IRQ_NONE;
1004
	u8 disable_irq = 0;
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

	rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
	if (spsr & SPSR_SPRF)
		disable_irq |= SPCR_SPRIE;
	if (spsr & SPSR_SPTEF)
		disable_irq |= SPCR_SPTIE;

	if (disable_irq) {
		ret = IRQ_HANDLED;
		rspi_disable_irq(rspi, disable_irq);
		wake_up(&rspi->wait);
	}

	return ret;
}

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
static irqreturn_t rspi_irq_rx(int irq, void *_sr)
{
	struct rspi_data *rspi = _sr;
	u8 spsr;

	rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
	if (spsr & SPSR_SPRF) {
		rspi_disable_irq(rspi, SPCR_SPRIE);
		wake_up(&rspi->wait);
		return IRQ_HANDLED;
	}

	return 0;
}

static irqreturn_t rspi_irq_tx(int irq, void *_sr)
{
	struct rspi_data *rspi = _sr;
	u8 spsr;

	rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
	if (spsr & SPSR_SPTEF) {
		rspi_disable_irq(rspi, SPCR_SPTIE);
		wake_up(&rspi->wait);
		return IRQ_HANDLED;
	}

	return 0;
}

1051 1052 1053 1054
static struct dma_chan *rspi_request_dma_chan(struct device *dev,
					      enum dma_transfer_direction dir,
					      unsigned int id,
					      dma_addr_t port_addr)
1055 1056
{
	dma_cap_mask_t mask;
1057
	struct dma_chan *chan;
1058 1059
	struct dma_slave_config cfg;
	int ret;
1060

1061 1062 1063
	dma_cap_zero(mask);
	dma_cap_set(DMA_SLAVE, mask);

1064 1065 1066
	chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
				(void *)(unsigned long)id, dev,
				dir == DMA_MEM_TO_DEV ? "tx" : "rx");
1067
	if (!chan) {
1068
		dev_warn(dev, "dma_request_slave_channel_compat failed\n");
1069 1070 1071 1072 1073
		return NULL;
	}

	memset(&cfg, 0, sizeof(cfg));
	cfg.direction = dir;
1074
	if (dir == DMA_MEM_TO_DEV) {
1075
		cfg.dst_addr = port_addr;
1076 1077
		cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
	} else {
1078
		cfg.src_addr = port_addr;
1079 1080
		cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
	}
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091

	ret = dmaengine_slave_config(chan, &cfg);
	if (ret) {
		dev_warn(dev, "dmaengine_slave_config failed %d\n", ret);
		dma_release_channel(chan);
		return NULL;
	}

	return chan;
}

1092
static int rspi_request_dma(struct device *dev, struct spi_master *master,
1093
			    const struct resource *res)
1094
{
1095
	const struct rspi_plat_data *rspi_pd = dev_get_platdata(dev);
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
	unsigned int dma_tx_id, dma_rx_id;

	if (dev->of_node) {
		/* In the OF case we will get the slave IDs from the DT */
		dma_tx_id = 0;
		dma_rx_id = 0;
	} else if (rspi_pd && rspi_pd->dma_tx_id && rspi_pd->dma_rx_id) {
		dma_tx_id = rspi_pd->dma_tx_id;
		dma_rx_id = rspi_pd->dma_rx_id;
	} else {
		/* The driver assumes no error. */
		return 0;
	}
1109

1110
	master->dma_tx = rspi_request_dma_chan(dev, DMA_MEM_TO_DEV, dma_tx_id,
1111
					       res->start + RSPI_SPDR);
1112
	if (!master->dma_tx)
1113 1114
		return -ENODEV;

1115
	master->dma_rx = rspi_request_dma_chan(dev, DMA_DEV_TO_MEM, dma_rx_id,
1116
					       res->start + RSPI_SPDR);
1117 1118 1119
	if (!master->dma_rx) {
		dma_release_channel(master->dma_tx);
		master->dma_tx = NULL;
1120
		return -ENODEV;
1121
	}
1122

1123
	master->can_dma = rspi_can_dma;
1124
	dev_info(dev, "DMA available");
1125
	return 0;
1126 1127
}

1128
static void rspi_release_dma(struct spi_master *master)
1129
{
1130 1131 1132 1133
	if (master->dma_tx)
		dma_release_channel(master->dma_tx);
	if (master->dma_rx)
		dma_release_channel(master->dma_rx);
1134 1135
}

1136
static int rspi_remove(struct platform_device *pdev)
1137
{
1138
	struct rspi_data *rspi = platform_get_drvdata(pdev);
1139

1140
	rspi_release_dma(rspi->master);
1141
	pm_runtime_disable(&pdev->dev);
1142 1143 1144 1145

	return 0;
}

G
Geert Uytterhoeven 已提交
1146
static const struct spi_ops rspi_ops = {
1147 1148 1149 1150
	.set_config_register =	rspi_set_config_register,
	.transfer_one =		rspi_transfer_one,
	.mode_bits =		SPI_CPHA | SPI_CPOL | SPI_LOOP,
	.flags =		SPI_MASTER_MUST_TX,
1151
	.fifo_size =		8,
G
Geert Uytterhoeven 已提交
1152 1153 1154
};

static const struct spi_ops rspi_rz_ops = {
1155 1156 1157 1158
	.set_config_register =	rspi_rz_set_config_register,
	.transfer_one =		rspi_rz_transfer_one,
	.mode_bits =		SPI_CPHA | SPI_CPOL | SPI_LOOP,
	.flags =		SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX,
1159
	.fifo_size =		8,	/* 8 for TX, 32 for RX */
G
Geert Uytterhoeven 已提交
1160 1161 1162
};

static const struct spi_ops qspi_ops = {
1163 1164 1165 1166 1167 1168
	.set_config_register =	qspi_set_config_register,
	.transfer_one =		qspi_transfer_one,
	.mode_bits =		SPI_CPHA | SPI_CPOL | SPI_LOOP |
				SPI_TX_DUAL | SPI_TX_QUAD |
				SPI_RX_DUAL | SPI_RX_QUAD,
	.flags =		SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX,
1169
	.fifo_size =		32,
G
Geert Uytterhoeven 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
};

#ifdef CONFIG_OF
static const struct of_device_id rspi_of_match[] = {
	/* RSPI on legacy SH */
	{ .compatible = "renesas,rspi", .data = &rspi_ops },
	/* RSPI on RZ/A1H */
	{ .compatible = "renesas,rspi-rz", .data = &rspi_rz_ops },
	/* QSPI on R-Car Gen2 */
	{ .compatible = "renesas,qspi", .data = &qspi_ops },
	{ /* sentinel */ }
};

MODULE_DEVICE_TABLE(of, rspi_of_match);

static int rspi_parse_dt(struct device *dev, struct spi_master *master)
{
	u32 num_cs;
	int error;

	/* Parse DT properties */
	error = of_property_read_u32(dev->of_node, "num-cs", &num_cs);
	if (error) {
		dev_err(dev, "of_property_read_u32 num-cs failed %d\n", error);
		return error;
	}

	master->num_chipselect = num_cs;
	return 0;
}
#else
1201
#define rspi_of_match	NULL
G
Geert Uytterhoeven 已提交
1202 1203 1204 1205 1206 1207
static inline int rspi_parse_dt(struct device *dev, struct spi_master *master)
{
	return -EINVAL;
}
#endif /* CONFIG_OF */

1208 1209 1210 1211
static int rspi_request_irq(struct device *dev, unsigned int irq,
			    irq_handler_t handler, const char *suffix,
			    void *dev_id)
{
1212 1213
	const char *name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s",
					  dev_name(dev), suffix);
1214 1215
	if (!name)
		return -ENOMEM;
1216

1217 1218 1219
	return devm_request_irq(dev, irq, handler, 0, name, dev_id);
}

1220
static int rspi_probe(struct platform_device *pdev)
1221 1222 1223 1224
{
	struct resource *res;
	struct spi_master *master;
	struct rspi_data *rspi;
1225
	int ret;
G
Geert Uytterhoeven 已提交
1226
	const struct rspi_plat_data *rspi_pd;
1227
	const struct spi_ops *ops;
1228 1229

	master = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data));
1230
	if (master == NULL)
1231 1232
		return -ENOMEM;

1233 1234
	ops = of_device_get_match_data(&pdev->dev);
	if (ops) {
G
Geert Uytterhoeven 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
		ret = rspi_parse_dt(&pdev->dev, master);
		if (ret)
			goto error1;
	} else {
		ops = (struct spi_ops *)pdev->id_entry->driver_data;
		rspi_pd = dev_get_platdata(&pdev->dev);
		if (rspi_pd && rspi_pd->num_chipselect)
			master->num_chipselect = rspi_pd->num_chipselect;
		else
			master->num_chipselect = 2; /* default */
1245
	}
G
Geert Uytterhoeven 已提交
1246 1247 1248 1249 1250 1251 1252 1253

	/* ops parameter check */
	if (!ops->set_config_register) {
		dev_err(&pdev->dev, "there is no set_config_register\n");
		ret = -ENODEV;
		goto error1;
	}

1254
	rspi = spi_master_get_devdata(master);
1255
	platform_set_drvdata(pdev, rspi);
1256
	rspi->ops = ops;
1257
	rspi->master = master;
1258 1259 1260 1261 1262

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	rspi->addr = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(rspi->addr)) {
		ret = PTR_ERR(rspi->addr);
1263 1264 1265
		goto error1;
	}

1266
	rspi->clk = devm_clk_get(&pdev->dev, NULL);
1267 1268 1269
	if (IS_ERR(rspi->clk)) {
		dev_err(&pdev->dev, "cannot get clock\n");
		ret = PTR_ERR(rspi->clk);
1270
		goto error1;
1271
	}
1272

1273
	pm_runtime_enable(&pdev->dev);
1274 1275 1276 1277 1278

	init_waitqueue_head(&rspi->wait);

	master->bus_num = pdev->id;
	master->setup = rspi_setup;
1279
	master->auto_runtime_pm = true;
1280
	master->transfer_one = ops->transfer_one;
1281 1282
	master->prepare_message = rspi_prepare_message;
	master->unprepare_message = rspi_unprepare_message;
1283
	master->mode_bits = ops->mode_bits;
1284
	master->flags = ops->flags;
G
Geert Uytterhoeven 已提交
1285
	master->dev.of_node = pdev->dev.of_node;
1286

1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
	ret = platform_get_irq_byname(pdev, "rx");
	if (ret < 0) {
		ret = platform_get_irq_byname(pdev, "mux");
		if (ret < 0)
			ret = platform_get_irq(pdev, 0);
		if (ret >= 0)
			rspi->rx_irq = rspi->tx_irq = ret;
	} else {
		rspi->rx_irq = ret;
		ret = platform_get_irq_byname(pdev, "tx");
		if (ret >= 0)
			rspi->tx_irq = ret;
	}
	if (ret < 0) {
		dev_err(&pdev->dev, "platform_get_irq error\n");
		goto error2;
	}

	if (rspi->rx_irq == rspi->tx_irq) {
		/* Single multiplexed interrupt */
		ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_mux,
				       "mux", rspi);
	} else {
		/* Multi-interrupt mode, only SPRI and SPTI are used */
		ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_rx,
				       "rx", rspi);
		if (!ret)
			ret = rspi_request_irq(&pdev->dev, rspi->tx_irq,
					       rspi_irq_tx, "tx", rspi);
	}
1317 1318
	if (ret < 0) {
		dev_err(&pdev->dev, "request_irq error\n");
1319
		goto error2;
1320 1321
	}

1322
	ret = rspi_request_dma(&pdev->dev, master, res);
1323 1324
	if (ret < 0)
		dev_warn(&pdev->dev, "DMA not available, using PIO\n");
1325

1326
	ret = devm_spi_register_master(&pdev->dev, master);
1327 1328
	if (ret < 0) {
		dev_err(&pdev->dev, "spi_register_master error.\n");
1329
		goto error3;
1330 1331 1332 1333 1334 1335
	}

	dev_info(&pdev->dev, "probed\n");

	return 0;

1336
error3:
1337
	rspi_release_dma(master);
1338
error2:
1339
	pm_runtime_disable(&pdev->dev);
1340 1341 1342 1343 1344 1345
error1:
	spi_master_put(master);

	return ret;
}

1346
static const struct platform_device_id spi_driver_ids[] = {
1347
	{ "rspi",	(kernel_ulong_t)&rspi_ops },
1348
	{ "rspi-rz",	(kernel_ulong_t)&rspi_rz_ops },
1349 1350 1351 1352 1353 1354
	{ "qspi",	(kernel_ulong_t)&qspi_ops },
	{},
};

MODULE_DEVICE_TABLE(platform, spi_driver_ids);

1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
#ifdef CONFIG_PM_SLEEP
static int rspi_suspend(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct rspi_data *rspi = platform_get_drvdata(pdev);

	return spi_master_suspend(rspi->master);
}

static int rspi_resume(struct device *dev)
{
	struct platform_device *pdev = to_platform_device(dev);
	struct rspi_data *rspi = platform_get_drvdata(pdev);

	return spi_master_resume(rspi->master);
}

static SIMPLE_DEV_PM_OPS(rspi_pm_ops, rspi_suspend, rspi_resume);
#define DEV_PM_OPS	&rspi_pm_ops
#else
#define DEV_PM_OPS	NULL
#endif /* CONFIG_PM_SLEEP */

1378 1379
static struct platform_driver rspi_driver = {
	.probe =	rspi_probe,
1380
	.remove =	rspi_remove,
1381
	.id_table =	spi_driver_ids,
1382
	.driver		= {
1383
		.name = "renesas_spi",
1384
		.pm = DEV_PM_OPS,
G
Geert Uytterhoeven 已提交
1385
		.of_match_table = of_match_ptr(rspi_of_match),
1386 1387 1388 1389 1390 1391 1392 1393
	},
};
module_platform_driver(rspi_driver);

MODULE_DESCRIPTION("Renesas RSPI bus driver");
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Yoshihiro Shimoda");
MODULE_ALIAS("platform:rspi");