spi-rspi.c 31.9 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 28 29 30 31 32
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#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>
33 34
#include <linux/dmaengine.h>
#include <linux/dma-mapping.h>
G
Geert Uytterhoeven 已提交
35
#include <linux/of_device.h>
36
#include <linux/pm_runtime.h>
37
#include <linux/sh_dma.h>
38
#include <linux/spi/spi.h>
39
#include <linux/spi/rspi.h>
40

41 42 43 44 45 46 47 48 49 50 51 52
#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 */
53
#define RSPI_SPCR2		0x0f	/* Control Register 2 (SH only) */
54 55 56 57 58 59 60 61
#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 */
62 63 64 65
#define RSPI_SPCMD(i)		(RSPI_SPCMD0 + (i) * 2)
#define RSPI_NUM_SPCMD		8
#define RSPI_RZ_NUM_SPCMD	4
#define QSPI_NUM_SPCMD		4
66 67

/* RSPI on RZ only */
68 69
#define RSPI_SPBFCR		0x20	/* Buffer Control Register */
#define RSPI_SPBFDR		0x22	/* Buffer Data Count Setting Register */
70

71
/* QSPI only */
72 73 74 75 76 77
#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 */
78
#define QSPI_SPBMUL(i)		(QSPI_SPBMUL0 + (i) * 4)
79

80 81 82 83 84 85 86 87 88 89
/* 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) */
90 91 92
/* QSPI on R-Car M2 only */
#define SPCR_WSWAP		0x02	/* Word Swap of read-data for DMAC */
#define SPCR_BSWAP		0x01	/* Byte Swap of read-data for DMAC */
93 94 95 96 97 98 99 100

/* 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 */
101
#define SPPCR_SPOM		0x04
102 103 104
#define SPPCR_SPLP2		0x02	/* Loopback Mode 2 (non-inverting) */
#define SPPCR_SPLP		0x01	/* Loopback Mode (inverting) */

105 106 107
#define SPPCR_IO3FV		0x04	/* Single-/Dual-SPI Mode IO3 Output Fixed Value */
#define SPPCR_IO2FV		0x04	/* Single-/Dual-SPI Mode IO2 Output Fixed Value */

108 109 110 111 112 113 114
/* 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 */
115
#define SPSR_OVRF		0x01	/* Overrun Error Flag (RSPI only) */
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

/* 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) */
132
#define SPDCR_SPRDTD		0x10	/* Receive Transmit Data Select (SH) */
133 134
#define SPDCR_SLSEL1		0x08
#define SPDCR_SLSEL0		0x04
135
#define SPDCR_SLSEL_MASK	0x0c	/* SSL1 Output Select (SH) */
136 137
#define SPDCR_SPFC1		0x02
#define SPDCR_SPFC0		0x01
138
#define SPDCR_SPFC_MASK		0x03	/* Frame Count Setting (1-4) (SH) */
139

140 141
/* SPCKD - Clock Delay Register */
#define SPCKD_SCKDL_MASK	0x07	/* Clock Delay Setting (1-8) */
142

143 144
/* SSLND - Slave Select Negation Delay Register */
#define SSLND_SLNDL_MASK	0x07	/* SSL Negation Delay Setting (1-8) */
145

146 147
/* SPND - Next-Access Delay Register */
#define SPND_SPNDL_MASK		0x07	/* Next-Access Delay Setting (1-8) */
148

149 150 151 152 153
/* 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 */
154

155 156 157 158 159 160
/* 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 */
161
#define SPCMD_SPB_8_TO_16(bit)	(((bit - 1) << 8) & SPCMD_SPB_MASK)
162
#define SPCMD_SPB_8BIT		0x0000	/* QSPI only */
163
#define SPCMD_SPB_16BIT		0x0100
164 165 166
#define SPCMD_SPB_20BIT		0x0000
#define SPCMD_SPB_24BIT		0x0100
#define SPCMD_SPB_32BIT		0x0200
167
#define SPCMD_SSLKP		0x0080	/* SSL Signal Level Keeping */
168 169 170 171 172 173 174
#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) */
175 176 177 178 179 180
#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 */
181 182
#define SPBFCR_TXRST		0x80	/* Transmit Buffer Data Reset */
#define SPBFCR_RXRST		0x40	/* Receive Buffer Data Reset */
183 184
#define SPBFCR_TXTRG_MASK	0x30	/* Transmit Buffer Data Triggering Number */
#define SPBFCR_RXTRG_MASK	0x07	/* Receive Buffer Data Triggering Number */
185

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

	unsigned dma_callbacked:1;
199
	unsigned byte_access:1;
200 201
};

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

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

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

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

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

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
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);
}

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

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

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

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

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

272 273 274 275 276 277 278 279 280
	/* 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 */
281 282
	rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size);
	rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
283 284 285 286 287

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

	return 0;
288 289
}

290 291 292 293 294 295 296
/*
 * functions for RSPI on RZ
 */
static int rspi_rz_set_config_register(struct rspi_data *rspi, int access_size)
{
	int spbr;

297 298
	/* Sets output mode, MOSI signal, and (optionally) loopback */
	rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
299 300

	/* Sets transfer bit rate */
301 302
	spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk),
			    2 * rspi->max_speed_hz) - 1;
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
	rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);

	/* 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;
}

324 325 326
/*
 * functions for QSPI
 */
327
static int qspi_set_config_register(struct rspi_data *rspi, int access_size)
328 329 330
{
	int spbr;

331 332
	/* Sets output mode, MOSI signal, and (optionally) loopback */
	rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
333 334

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

338 339 340
	/* Disable dummy transmission, set byte access */
	rspi_write8(rspi, 0, RSPI_SPDCR);
	rspi->byte_access = 1;
341 342 343 344 345 346 347 348

	/* 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)
349
		rspi->spcmd |= SPCMD_SPB_8BIT;
350
	else if (access_size == 16)
351
		rspi->spcmd |= SPCMD_SPB_16BIT;
352
	else
353
		rspi->spcmd |= SPCMD_SPB_32BIT;
354

355
	rspi->spcmd |= SPCMD_SCKDEN | SPCMD_SLNDEN | SPCMD_SPNDEN;
356 357 358 359 360 361 362 363 364 365

	/* 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 */
366
	rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
367

368
	/* Enables SPI function in master mode */
369 370 371 372 373 374 375
	rspi_write8(rspi, SPCR_SPE | SPCR_MSTR, RSPI_SPCR);

	return 0;
}

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

376
static void rspi_enable_irq(const struct rspi_data *rspi, u8 enable)
377 378 379 380
{
	rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | enable, RSPI_SPCR);
}

381
static void rspi_disable_irq(const struct rspi_data *rspi, u8 disable)
382 383 384 385 386 387 388 389 390 391
{
	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);
392 393 394
	if (rspi->spsr & wait_mask)
		return 0;

395 396 397 398 399 400 401 402
	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;
}

403 404 405 406 407 408 409 410 411 412
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);
}

413 414
static int rspi_data_out(struct rspi_data *rspi, u8 data)
{
415 416
	int error = rspi_wait_for_tx_empty(rspi);
	if (error < 0) {
417
		dev_err(&rspi->master->dev, "transmit timeout\n");
418
		return error;
419 420 421 422 423 424 425
	}
	rspi_write_data(rspi, data);
	return 0;
}

static int rspi_data_in(struct rspi_data *rspi)
{
426
	int error;
427 428
	u8 data;

429 430
	error = rspi_wait_for_rx_full(rspi);
	if (error < 0) {
431
		dev_err(&rspi->master->dev, "receive timeout\n");
432
		return error;
433 434 435 436 437
	}
	data = rspi_read_data(rspi);
	return data;
}

438 439
static int rspi_pio_transfer(struct rspi_data *rspi, const u8 *tx, u8 *rx,
			     unsigned int n)
440
{
441 442 443 444 445 446 447 448 449 450 451 452 453
	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;
		}
	}
454

455
	return 0;
456 457
}

458 459 460 461 462 463 464 465 466 467 468
static void rspi_dma_complete(void *arg)
{
	struct rspi_data *rspi = arg;

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

static int rspi_send_dma(struct rspi_data *rspi, struct spi_transfer *t)
{
	struct dma_async_tx_descriptor *desc;
469
	int ret;
470

471 472
	desc = dmaengine_prep_slave_sg(rspi->master->dma_tx, t->tx_sg.sgl,
				       t->tx_sg.nents, DMA_TO_DEVICE,
473
				       DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
474 475
	if (!desc)
		return -EIO;
476 477 478 479 480

	/*
	 * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be
	 * called. So, this driver disables the IRQ while DMA transfer.
	 */
481
	disable_irq(rspi->tx_irq);
482 483 484 485 486 487 488 489

	rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_TXMD, RSPI_SPCR);
	rspi_enable_irq(rspi, SPCR_SPTIE);
	rspi->dma_callbacked = 0;

	desc->callback = rspi_dma_complete;
	desc->callback_param = rspi;
	dmaengine_submit(desc);
490
	dma_async_issue_pending(rspi->master->dma_tx);
491 492 493 494 495 496 497 498 499

	ret = wait_event_interruptible_timeout(rspi->wait,
					       rspi->dma_callbacked, HZ);
	if (ret > 0 && rspi->dma_callbacked)
		ret = 0;
	else if (!ret)
		ret = -ETIMEDOUT;
	rspi_disable_irq(rspi, SPCR_SPTIE);

500
	enable_irq(rspi->tx_irq);
501 502 503
	return ret;
}

504
static void rspi_receive_init(const struct rspi_data *rspi)
505
{
506
	u8 spsr;
507 508 509

	spsr = rspi_read8(rspi, RSPI_SPSR);
	if (spsr & SPSR_SPRF)
510
		rspi_read_data(rspi);	/* dummy read */
511 512
	if (spsr & SPSR_OVRF)
		rspi_write8(rspi, rspi_read8(rspi, RSPI_SPSR) & ~SPSR_OVRF,
513
			    RSPI_SPSR);
514 515
}

516 517 518 519 520 521 522
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);
}

523
static void qspi_receive_init(const struct rspi_data *rspi)
524
{
525
	u8 spsr;
526 527 528

	spsr = rspi_read8(rspi, RSPI_SPSR);
	if (spsr & SPSR_SPRF)
529
		rspi_read_data(rspi);   /* dummy read */
530
	rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
531
	rspi_write8(rspi, 0, QSPI_SPBFCR);
532 533
}

534
static int rspi_send_receive_dma(struct rspi_data *rspi, struct spi_transfer *t)
535
{
536
	struct dma_async_tx_descriptor *desc_tx, *desc_rx;
537
	int ret;
538

539
	/* prepare transmit transfer */
540 541 542 543 544
	desc_tx = dmaengine_prep_slave_sg(rspi->master->dma_tx, t->tx_sg.sgl,
					  t->tx_sg.nents, DMA_TO_DEVICE,
					  DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
	if (!desc_tx)
		return -EIO;
545 546

	/* prepare receive transfer */
547 548
	desc_rx = dmaengine_prep_slave_sg(rspi->master->dma_rx, t->rx_sg.sgl,
					  t->rx_sg.nents, DMA_FROM_DEVICE,
549
					  DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
550 551
	if (!desc_rx)
		return -EIO;
552 553 554 555 556 557 558

	rspi_receive_init(rspi);

	/*
	 * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be
	 * called. So, this driver disables the IRQ while DMA transfer.
	 */
559 560 561
	disable_irq(rspi->tx_irq);
	if (rspi->rx_irq != rspi->tx_irq)
		disable_irq(rspi->rx_irq);
562 563 564 565 566

	rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_TXMD, RSPI_SPCR);
	rspi_enable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE);
	rspi->dma_callbacked = 0;

567 568 569
	desc_rx->callback = rspi_dma_complete;
	desc_rx->callback_param = rspi;
	dmaengine_submit(desc_rx);
570
	dma_async_issue_pending(rspi->master->dma_rx);
571

572 573
	desc_tx->callback = NULL;	/* No callback */
	dmaengine_submit(desc_tx);
574
	dma_async_issue_pending(rspi->master->dma_tx);
575 576 577 578 579 580 581 582 583

	ret = wait_event_interruptible_timeout(rspi->wait,
					       rspi->dma_callbacked, HZ);
	if (ret > 0 && rspi->dma_callbacked)
		ret = 0;
	else if (!ret)
		ret = -ETIMEDOUT;
	rspi_disable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE);

584 585 586
	enable_irq(rspi->tx_irq);
	if (rspi->rx_irq != rspi->tx_irq)
		enable_irq(rspi->rx_irq);
587 588 589 590

	return ret;
}

591 592
static bool __rspi_can_dma(const struct rspi_data *rspi,
			   const struct spi_transfer *xfer)
593
{
594 595
	return xfer->len > rspi->ops->fifo_size;
}
596

597 598 599 600 601 602
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);
603 604
}

605 606 607
static int rspi_transfer_out_in(struct rspi_data *rspi,
				struct spi_transfer *xfer)
{
608
	u8 spcr;
609
	int ret;
610 611

	spcr = rspi_read8(rspi, RSPI_SPCR);
612
	if (xfer->rx_buf) {
613
		rspi_receive_init(rspi);
614
		spcr &= ~SPCR_TXMD;
615
	} else {
616
		spcr |= SPCR_TXMD;
617
	}
618 619
	rspi_write8(rspi, spcr, RSPI_SPCR);

620 621 622
	ret = rspi_pio_transfer(rspi, xfer->tx_buf, xfer->rx_buf, xfer->len);
	if (ret < 0)
		return ret;
623 624

	/* Wait for the last transmission */
625
	rspi_wait_for_tx_empty(rspi);
626 627 628 629

	return 0;
}

630 631
static int rspi_transfer_one(struct spi_master *master, struct spi_device *spi,
			     struct spi_transfer *xfer)
632
{
633
	struct rspi_data *rspi = spi_master_get_devdata(master);
634

635
	if (!master->can_dma || !__rspi_can_dma(rspi, xfer))
636
		return rspi_transfer_out_in(rspi, xfer);
637

638
	if (xfer->rx_buf)
639 640 641
		return rspi_send_receive_dma(rspi, xfer);
	else
		return rspi_send_dma(rspi, xfer);
642 643
}

644 645 646
static int rspi_rz_transfer_out_in(struct rspi_data *rspi,
				   struct spi_transfer *xfer)
{
647
	int ret;
648 649 650

	rspi_rz_receive_init(rspi);

651 652 653
	ret = rspi_pio_transfer(rspi, xfer->tx_buf, xfer->rx_buf, xfer->len);
	if (ret < 0)
		return ret;
654 655

	/* Wait for the last transmission */
656
	rspi_wait_for_tx_empty(rspi);
657 658 659 660 661 662 663 664 665 666 667 668 669

	return 0;
}

static int rspi_rz_transfer_one(struct spi_master *master,
				struct spi_device *spi,
				struct spi_transfer *xfer)
{
	struct rspi_data *rspi = spi_master_get_devdata(master);

	return rspi_rz_transfer_out_in(rspi, xfer);
}

670 671
static int qspi_transfer_out_in(struct rspi_data *rspi,
				struct spi_transfer *xfer)
672
{
673
	int ret;
674

675 676
	qspi_receive_init(rspi);

677 678 679
	ret = rspi_pio_transfer(rspi, xfer->tx_buf, xfer->rx_buf, xfer->len);
	if (ret < 0)
		return ret;
680 681

	/* Wait for the last transmission */
682
	rspi_wait_for_tx_empty(rspi);
683 684 685 686

	return 0;
}

687 688 689 690
static int qspi_transfer_out(struct rspi_data *rspi, struct spi_transfer *xfer)
{
	int ret;

691 692 693
	ret = rspi_pio_transfer(rspi, xfer->tx_buf, NULL, xfer->len);
	if (ret < 0)
		return ret;
694 695

	/* Wait for the last transmission */
696
	rspi_wait_for_tx_empty(rspi);
697 698 699 700 701 702

	return 0;
}

static int qspi_transfer_in(struct rspi_data *rspi, struct spi_transfer *xfer)
{
703
	return rspi_pio_transfer(rspi, NULL, xfer->rx_buf, xfer->len);
704 705
}

706 707 708 709 710
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);

711 712
	if (spi->mode & SPI_LOOP) {
		return qspi_transfer_out_in(rspi, xfer);
713
	} else if (xfer->tx_nbits > SPI_NBITS_SINGLE) {
714 715
		/* Quad or Dual SPI Write */
		return qspi_transfer_out(rspi, xfer);
716
	} else if (xfer->rx_nbits > SPI_NBITS_SINGLE) {
717 718 719 720 721 722
		/* Quad or Dual SPI Read */
		return qspi_transfer_in(rspi, xfer);
	} else {
		/* Single SPI Transfer */
		return qspi_transfer_out_in(rspi, xfer);
	}
723 724 725 726 727 728 729 730
}

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;

731 732 733 734 735 736
	rspi->spcmd = SPCMD_SSLKP;
	if (spi->mode & SPI_CPOL)
		rspi->spcmd |= SPCMD_CPOL;
	if (spi->mode & SPI_CPHA)
		rspi->spcmd |= SPCMD_CPHA;

737 738 739 740 741
	/* CMOS output mode and MOSI signal from previous transfer */
	rspi->sppcr = 0;
	if (spi->mode & SPI_LOOP)
		rspi->sppcr |= SPPCR_SPLP;

742
	set_config_register(rspi, 8);
743 744 745 746

	return 0;
}

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
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;
}

812
static int rspi_prepare_message(struct spi_master *master,
813
				struct spi_message *msg)
814 815
{
	struct rspi_data *rspi = spi_master_get_devdata(master);
816
	int ret;
817

818 819 820 821 822 823 824 825 826
	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 */
827
	rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_SPE, RSPI_SPCR);
828 829 830
	return 0;
}

831
static int rspi_unprepare_message(struct spi_master *master,
832
				  struct spi_message *msg)
833
{
834 835
	struct rspi_data *rspi = spi_master_get_devdata(master);

836
	/* Disable SPI function */
837
	rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_SPE, RSPI_SPCR);
838 839 840 841

	/* Reset sequencer for Single SPI Transfers */
	rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
	rspi_write8(rspi, 0, RSPI_SPSCR);
842
	return 0;
843 844
}

845
static irqreturn_t rspi_irq_mux(int irq, void *_sr)
846
{
G
Geert Uytterhoeven 已提交
847
	struct rspi_data *rspi = _sr;
848
	u8 spsr;
849
	irqreturn_t ret = IRQ_NONE;
850
	u8 disable_irq = 0;
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866

	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;
}

867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
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;
}

897 898 899 900
static struct dma_chan *rspi_request_dma_chan(struct device *dev,
					      enum dma_transfer_direction dir,
					      unsigned int id,
					      dma_addr_t port_addr)
901 902
{
	dma_cap_mask_t mask;
903
	struct dma_chan *chan;
904 905
	struct dma_slave_config cfg;
	int ret;
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
	dma_cap_zero(mask);
	dma_cap_set(DMA_SLAVE, mask);

	chan = dma_request_channel(mask, shdma_chan_filter,
				   (void *)(unsigned long)id);
	if (!chan) {
		dev_warn(dev, "dma_request_channel failed\n");
		return NULL;
	}

	memset(&cfg, 0, sizeof(cfg));
	cfg.slave_id = id;
	cfg.direction = dir;
	if (dir == DMA_MEM_TO_DEV)
		cfg.dst_addr = port_addr;
	else
		cfg.src_addr = port_addr;

	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;
}

935
static int rspi_request_dma(struct device *dev, struct spi_master *master,
936
			    const struct resource *res)
937
{
938
	const struct rspi_plat_data *rspi_pd = dev_get_platdata(dev);
939

940
	if (!rspi_pd || !rspi_pd->dma_rx_id || !rspi_pd->dma_tx_id)
941
		return 0;	/* The driver assumes no error. */
942

943 944 945 946
	master->dma_rx = rspi_request_dma_chan(dev, DMA_DEV_TO_MEM,
					       rspi_pd->dma_rx_id,
					       res->start + RSPI_SPDR);
	if (!master->dma_rx)
947 948
		return -ENODEV;

949 950 951 952 953 954
	master->dma_tx = rspi_request_dma_chan(dev, DMA_MEM_TO_DEV,
					       rspi_pd->dma_tx_id,
					       res->start + RSPI_SPDR);
	if (!master->dma_tx) {
		dma_release_channel(master->dma_rx);
		master->dma_rx = NULL;
955
		return -ENODEV;
956
	}
957

958
	master->can_dma = rspi_can_dma;
959
	dev_info(dev, "DMA available");
960
	return 0;
961 962
}

963
static void rspi_release_dma(struct rspi_data *rspi)
964
{
965 966 967 968
	if (rspi->master->dma_tx)
		dma_release_channel(rspi->master->dma_tx);
	if (rspi->master->dma_rx)
		dma_release_channel(rspi->master->dma_rx);
969 970
}

971
static int rspi_remove(struct platform_device *pdev)
972
{
973
	struct rspi_data *rspi = platform_get_drvdata(pdev);
974

975
	rspi_release_dma(rspi);
976
	pm_runtime_disable(&pdev->dev);
977 978 979 980

	return 0;
}

G
Geert Uytterhoeven 已提交
981
static const struct spi_ops rspi_ops = {
982 983 984 985
	.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,
986
	.fifo_size =		8,
G
Geert Uytterhoeven 已提交
987 988 989
};

static const struct spi_ops rspi_rz_ops = {
990 991 992 993
	.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,
994
	.fifo_size =		8,	/* 8 for TX, 32 for RX */
G
Geert Uytterhoeven 已提交
995 996 997
};

static const struct spi_ops qspi_ops = {
998 999 1000 1001 1002 1003
	.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,
1004
	.fifo_size =		32,
G
Geert Uytterhoeven 已提交
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
};

#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
1036
#define rspi_of_match	NULL
G
Geert Uytterhoeven 已提交
1037 1038 1039 1040 1041 1042
static inline int rspi_parse_dt(struct device *dev, struct spi_master *master)
{
	return -EINVAL;
}
#endif /* CONFIG_OF */

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
static int rspi_request_irq(struct device *dev, unsigned int irq,
			    irq_handler_t handler, const char *suffix,
			    void *dev_id)
{
	const char *base = dev_name(dev);
	size_t len = strlen(base) + strlen(suffix) + 2;
	char *name = devm_kzalloc(dev, len, GFP_KERNEL);
	if (!name)
		return -ENOMEM;
	snprintf(name, len, "%s:%s", base, suffix);
	return devm_request_irq(dev, irq, handler, 0, name, dev_id);
}

1056
static int rspi_probe(struct platform_device *pdev)
1057 1058 1059 1060
{
	struct resource *res;
	struct spi_master *master;
	struct rspi_data *rspi;
1061
	int ret;
G
Geert Uytterhoeven 已提交
1062 1063
	const struct of_device_id *of_id;
	const struct rspi_plat_data *rspi_pd;
1064
	const struct spi_ops *ops;
1065 1066 1067 1068 1069 1070 1071

	master = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data));
	if (master == NULL) {
		dev_err(&pdev->dev, "spi_alloc_master error.\n");
		return -ENOMEM;
	}

G
Geert Uytterhoeven 已提交
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	of_id = of_match_device(rspi_of_match, &pdev->dev);
	if (of_id) {
		ops = of_id->data;
		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 */
	};

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

1094
	rspi = spi_master_get_devdata(master);
1095
	platform_set_drvdata(pdev, rspi);
1096
	rspi->ops = ops;
1097
	rspi->master = master;
1098 1099 1100 1101 1102

	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);
1103 1104 1105
		goto error1;
	}

1106
	rspi->clk = devm_clk_get(&pdev->dev, NULL);
1107 1108 1109
	if (IS_ERR(rspi->clk)) {
		dev_err(&pdev->dev, "cannot get clock\n");
		ret = PTR_ERR(rspi->clk);
1110
		goto error1;
1111
	}
1112

1113
	pm_runtime_enable(&pdev->dev);
1114 1115 1116 1117 1118

	init_waitqueue_head(&rspi->wait);

	master->bus_num = pdev->id;
	master->setup = rspi_setup;
1119
	master->auto_runtime_pm = true;
1120
	master->transfer_one = ops->transfer_one;
1121 1122
	master->prepare_message = rspi_prepare_message;
	master->unprepare_message = rspi_unprepare_message;
1123
	master->mode_bits = ops->mode_bits;
1124
	master->flags = ops->flags;
G
Geert Uytterhoeven 已提交
1125
	master->dev.of_node = pdev->dev.of_node;
1126

1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
	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);
	}
1157 1158
	if (ret < 0) {
		dev_err(&pdev->dev, "request_irq error\n");
1159
		goto error2;
1160 1161
	}

1162
	ret = rspi_request_dma(&pdev->dev, master, res);
1163 1164
	if (ret < 0)
		dev_warn(&pdev->dev, "DMA not available, using PIO\n");
1165

1166
	ret = devm_spi_register_master(&pdev->dev, master);
1167 1168
	if (ret < 0) {
		dev_err(&pdev->dev, "spi_register_master error.\n");
1169
		goto error3;
1170 1171 1172 1173 1174 1175
	}

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

	return 0;

1176
error3:
1177
	rspi_release_dma(rspi);
1178
error2:
1179
	pm_runtime_disable(&pdev->dev);
1180 1181 1182 1183 1184 1185
error1:
	spi_master_put(master);

	return ret;
}

1186 1187
static struct platform_device_id spi_driver_ids[] = {
	{ "rspi",	(kernel_ulong_t)&rspi_ops },
1188
	{ "rspi-rz",	(kernel_ulong_t)&rspi_rz_ops },
1189 1190 1191 1192 1193 1194
	{ "qspi",	(kernel_ulong_t)&qspi_ops },
	{},
};

MODULE_DEVICE_TABLE(platform, spi_driver_ids);

1195 1196
static struct platform_driver rspi_driver = {
	.probe =	rspi_probe,
1197
	.remove =	rspi_remove,
1198
	.id_table =	spi_driver_ids,
1199
	.driver		= {
1200
		.name = "renesas_spi",
1201
		.owner	= THIS_MODULE,
G
Geert Uytterhoeven 已提交
1202
		.of_match_table = of_match_ptr(rspi_of_match),
1203 1204 1205 1206 1207 1208 1209 1210
	},
};
module_platform_driver(rspi_driver);

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