4 * Copyright (C) 2012, 2013 Renesas Solutions Corp.
5 * Copyright (C) 2014 Glider bvba
8 * Copyright (C) 2011 Renesas Solutions Corp.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/sched.h>
28 #include <linux/errno.h>
29 #include <linux/interrupt.h>
30 #include <linux/platform_device.h>
32 #include <linux/clk.h>
33 #include <linux/dmaengine.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/of_device.h>
36 #include <linux/sh_dma.h>
37 #include <linux/spi/spi.h>
38 #include <linux/spi/rspi.h>
40 #define RSPI_SPCR 0x00 /* Control Register */
41 #define RSPI_SSLP 0x01 /* Slave Select Polarity Register */
42 #define RSPI_SPPCR 0x02 /* Pin Control Register */
43 #define RSPI_SPSR 0x03 /* Status Register */
44 #define RSPI_SPDR 0x04 /* Data Register */
45 #define RSPI_SPSCR 0x08 /* Sequence Control Register */
46 #define RSPI_SPSSR 0x09 /* Sequence Status Register */
47 #define RSPI_SPBR 0x0a /* Bit Rate Register */
48 #define RSPI_SPDCR 0x0b /* Data Control Register */
49 #define RSPI_SPCKD 0x0c /* Clock Delay Register */
50 #define RSPI_SSLND 0x0d /* Slave Select Negation Delay Register */
51 #define RSPI_SPND 0x0e /* Next-Access Delay Register */
52 #define RSPI_SPCR2 0x0f /* Control Register 2 (SH only) */
53 #define RSPI_SPCMD0 0x10 /* Command Register 0 */
54 #define RSPI_SPCMD1 0x12 /* Command Register 1 */
55 #define RSPI_SPCMD2 0x14 /* Command Register 2 */
56 #define RSPI_SPCMD3 0x16 /* Command Register 3 */
57 #define RSPI_SPCMD4 0x18 /* Command Register 4 */
58 #define RSPI_SPCMD5 0x1a /* Command Register 5 */
59 #define RSPI_SPCMD6 0x1c /* Command Register 6 */
60 #define RSPI_SPCMD7 0x1e /* Command Register 7 */
61 #define RSPI_SPCMD(i) (RSPI_SPCMD0 + (i) * 2)
62 #define RSPI_NUM_SPCMD 8
63 #define RSPI_RZ_NUM_SPCMD 4
64 #define QSPI_NUM_SPCMD 4
67 #define RSPI_SPBFCR 0x20 /* Buffer Control Register */
68 #define RSPI_SPBFDR 0x22 /* Buffer Data Count Setting Register */
71 #define QSPI_SPBFCR 0x18 /* Buffer Control Register */
72 #define QSPI_SPBDCR 0x1a /* Buffer Data Count Register */
73 #define QSPI_SPBMUL0 0x1c /* Transfer Data Length Multiplier Setting Register 0 */
74 #define QSPI_SPBMUL1 0x20 /* Transfer Data Length Multiplier Setting Register 1 */
75 #define QSPI_SPBMUL2 0x24 /* Transfer Data Length Multiplier Setting Register 2 */
76 #define QSPI_SPBMUL3 0x28 /* Transfer Data Length Multiplier Setting Register 3 */
77 #define QSPI_SPBMUL(i) (QSPI_SPBMUL0 + (i) * 4)
79 /* SPCR - Control Register */
80 #define SPCR_SPRIE 0x80 /* Receive Interrupt Enable */
81 #define SPCR_SPE 0x40 /* Function Enable */
82 #define SPCR_SPTIE 0x20 /* Transmit Interrupt Enable */
83 #define SPCR_SPEIE 0x10 /* Error Interrupt Enable */
84 #define SPCR_MSTR 0x08 /* Master/Slave Mode Select */
85 #define SPCR_MODFEN 0x04 /* Mode Fault Error Detection Enable */
87 #define SPCR_TXMD 0x02 /* TX Only Mode (vs. Full Duplex) */
88 #define SPCR_SPMS 0x01 /* 3-wire Mode (vs. 4-wire) */
89 /* QSPI on R-Car M2 only */
90 #define SPCR_WSWAP 0x02 /* Word Swap of read-data for DMAC */
91 #define SPCR_BSWAP 0x01 /* Byte Swap of read-data for DMAC */
93 /* SSLP - Slave Select Polarity Register */
94 #define SSLP_SSL1P 0x02 /* SSL1 Signal Polarity Setting */
95 #define SSLP_SSL0P 0x01 /* SSL0 Signal Polarity Setting */
97 /* SPPCR - Pin Control Register */
98 #define SPPCR_MOIFE 0x20 /* MOSI Idle Value Fixing Enable */
99 #define SPPCR_MOIFV 0x10 /* MOSI Idle Fixed Value */
100 #define SPPCR_SPOM 0x04
101 #define SPPCR_SPLP2 0x02 /* Loopback Mode 2 (non-inverting) */
102 #define SPPCR_SPLP 0x01 /* Loopback Mode (inverting) */
104 #define SPPCR_IO3FV 0x04 /* Single-/Dual-SPI Mode IO3 Output Fixed Value */
105 #define SPPCR_IO2FV 0x04 /* Single-/Dual-SPI Mode IO2 Output Fixed Value */
107 /* SPSR - Status Register */
108 #define SPSR_SPRF 0x80 /* Receive Buffer Full Flag */
109 #define SPSR_TEND 0x40 /* Transmit End */
110 #define SPSR_SPTEF 0x20 /* Transmit Buffer Empty Flag */
111 #define SPSR_PERF 0x08 /* Parity Error Flag */
112 #define SPSR_MODF 0x04 /* Mode Fault Error Flag */
113 #define SPSR_IDLNF 0x02 /* RSPI Idle Flag */
114 #define SPSR_OVRF 0x01 /* Overrun Error Flag (RSPI only) */
116 /* SPSCR - Sequence Control Register */
117 #define SPSCR_SPSLN_MASK 0x07 /* Sequence Length Specification */
119 /* SPSSR - Sequence Status Register */
120 #define SPSSR_SPECM_MASK 0x70 /* Command Error Mask */
121 #define SPSSR_SPCP_MASK 0x07 /* Command Pointer Mask */
123 /* SPDCR - Data Control Register */
124 #define SPDCR_TXDMY 0x80 /* Dummy Data Transmission Enable */
125 #define SPDCR_SPLW1 0x40 /* Access Width Specification (RZ) */
126 #define SPDCR_SPLW0 0x20 /* Access Width Specification (RZ) */
127 #define SPDCR_SPLLWORD (SPDCR_SPLW1 | SPDCR_SPLW0)
128 #define SPDCR_SPLWORD SPDCR_SPLW1
129 #define SPDCR_SPLBYTE SPDCR_SPLW0
130 #define SPDCR_SPLW 0x20 /* Access Width Specification (SH) */
131 #define SPDCR_SPRDTD 0x10 /* Receive Transmit Data Select (SH) */
132 #define SPDCR_SLSEL1 0x08
133 #define SPDCR_SLSEL0 0x04
134 #define SPDCR_SLSEL_MASK 0x0c /* SSL1 Output Select (SH) */
135 #define SPDCR_SPFC1 0x02
136 #define SPDCR_SPFC0 0x01
137 #define SPDCR_SPFC_MASK 0x03 /* Frame Count Setting (1-4) (SH) */
139 /* SPCKD - Clock Delay Register */
140 #define SPCKD_SCKDL_MASK 0x07 /* Clock Delay Setting (1-8) */
142 /* SSLND - Slave Select Negation Delay Register */
143 #define SSLND_SLNDL_MASK 0x07 /* SSL Negation Delay Setting (1-8) */
145 /* SPND - Next-Access Delay Register */
146 #define SPND_SPNDL_MASK 0x07 /* Next-Access Delay Setting (1-8) */
148 /* SPCR2 - Control Register 2 */
149 #define SPCR2_PTE 0x08 /* Parity Self-Test Enable */
150 #define SPCR2_SPIE 0x04 /* Idle Interrupt Enable */
151 #define SPCR2_SPOE 0x02 /* Odd Parity Enable (vs. Even) */
152 #define SPCR2_SPPE 0x01 /* Parity Enable */
154 /* SPCMDn - Command Registers */
155 #define SPCMD_SCKDEN 0x8000 /* Clock Delay Setting Enable */
156 #define SPCMD_SLNDEN 0x4000 /* SSL Negation Delay Setting Enable */
157 #define SPCMD_SPNDEN 0x2000 /* Next-Access Delay Enable */
158 #define SPCMD_LSBF 0x1000 /* LSB First */
159 #define SPCMD_SPB_MASK 0x0f00 /* Data Length Setting */
160 #define SPCMD_SPB_8_TO_16(bit) (((bit - 1) << 8) & SPCMD_SPB_MASK)
161 #define SPCMD_SPB_8BIT 0x0000 /* QSPI only */
162 #define SPCMD_SPB_16BIT 0x0100
163 #define SPCMD_SPB_20BIT 0x0000
164 #define SPCMD_SPB_24BIT 0x0100
165 #define SPCMD_SPB_32BIT 0x0200
166 #define SPCMD_SSLKP 0x0080 /* SSL Signal Level Keeping */
167 #define SPCMD_SPIMOD_MASK 0x0060 /* SPI Operating Mode (QSPI only) */
168 #define SPCMD_SPIMOD1 0x0040
169 #define SPCMD_SPIMOD0 0x0020
170 #define SPCMD_SPIMOD_SINGLE 0
171 #define SPCMD_SPIMOD_DUAL SPCMD_SPIMOD0
172 #define SPCMD_SPIMOD_QUAD SPCMD_SPIMOD1
173 #define SPCMD_SPRW 0x0010 /* SPI Read/Write Access (Dual/Quad) */
174 #define SPCMD_SSLA_MASK 0x0030 /* SSL Assert Signal Setting (RSPI) */
175 #define SPCMD_BRDV_MASK 0x000c /* Bit Rate Division Setting */
176 #define SPCMD_CPOL 0x0002 /* Clock Polarity Setting */
177 #define SPCMD_CPHA 0x0001 /* Clock Phase Setting */
179 /* SPBFCR - Buffer Control Register */
180 #define SPBFCR_TXRST 0x80 /* Transmit Buffer Data Reset */
181 #define SPBFCR_RXRST 0x40 /* Receive Buffer Data Reset */
182 #define SPBFCR_TXTRG_MASK 0x30 /* Transmit Buffer Data Triggering Number */
183 #define SPBFCR_RXTRG_MASK 0x07 /* Receive Buffer Data Triggering Number */
185 #define DUMMY_DATA 0x00
190 struct spi_master *master;
191 wait_queue_head_t wait;
197 const struct spi_ops *ops;
200 struct dma_chan *chan_tx;
201 struct dma_chan *chan_rx;
203 unsigned dma_width_16bit:1;
204 unsigned dma_callbacked:1;
205 unsigned byte_access:1;
208 static void rspi_write8(const struct rspi_data *rspi, u8 data, u16 offset)
210 iowrite8(data, rspi->addr + offset);
213 static void rspi_write16(const struct rspi_data *rspi, u16 data, u16 offset)
215 iowrite16(data, rspi->addr + offset);
218 static void rspi_write32(const struct rspi_data *rspi, u32 data, u16 offset)
220 iowrite32(data, rspi->addr + offset);
223 static u8 rspi_read8(const struct rspi_data *rspi, u16 offset)
225 return ioread8(rspi->addr + offset);
228 static u16 rspi_read16(const struct rspi_data *rspi, u16 offset)
230 return ioread16(rspi->addr + offset);
233 static void rspi_write_data(const struct rspi_data *rspi, u16 data)
235 if (rspi->byte_access)
236 rspi_write8(rspi, data, RSPI_SPDR);
238 rspi_write16(rspi, data, RSPI_SPDR);
241 static u16 rspi_read_data(const struct rspi_data *rspi)
243 if (rspi->byte_access)
244 return rspi_read8(rspi, RSPI_SPDR);
246 return rspi_read16(rspi, RSPI_SPDR);
249 /* optional functions */
251 int (*set_config_register)(struct rspi_data *rspi, int access_size);
252 int (*transfer_one)(struct spi_master *master, struct spi_device *spi,
253 struct spi_transfer *xfer);
258 * functions for RSPI on legacy SH
260 static int rspi_set_config_register(struct rspi_data *rspi, int access_size)
264 /* Sets output mode, MOSI signal, and (optionally) loopback */
265 rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
267 /* Sets transfer bit rate */
268 spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz) - 1;
269 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
271 /* Disable dummy transmission, set 16-bit word access, 1 frame */
272 rspi_write8(rspi, 0, RSPI_SPDCR);
273 rspi->byte_access = 0;
275 /* Sets RSPCK, SSL, next-access delay value */
276 rspi_write8(rspi, 0x00, RSPI_SPCKD);
277 rspi_write8(rspi, 0x00, RSPI_SSLND);
278 rspi_write8(rspi, 0x00, RSPI_SPND);
280 /* Sets parity, interrupt mask */
281 rspi_write8(rspi, 0x00, RSPI_SPCR2);
284 rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size);
285 rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
288 rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
294 * functions for RSPI on RZ
296 static int rspi_rz_set_config_register(struct rspi_data *rspi, int access_size)
300 /* Sets output mode, MOSI signal, and (optionally) loopback */
301 rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
303 /* Sets transfer bit rate */
304 spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz) - 1;
305 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
307 /* Disable dummy transmission, set byte access */
308 rspi_write8(rspi, SPDCR_SPLBYTE, RSPI_SPDCR);
309 rspi->byte_access = 1;
311 /* Sets RSPCK, SSL, next-access delay value */
312 rspi_write8(rspi, 0x00, RSPI_SPCKD);
313 rspi_write8(rspi, 0x00, RSPI_SSLND);
314 rspi_write8(rspi, 0x00, RSPI_SPND);
317 rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size);
318 rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
321 rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
329 static int qspi_set_config_register(struct rspi_data *rspi, int access_size)
333 /* Sets output mode, MOSI signal, and (optionally) loopback */
334 rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
336 /* Sets transfer bit rate */
337 spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz);
338 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
340 /* Disable dummy transmission, set byte access */
341 rspi_write8(rspi, 0, RSPI_SPDCR);
342 rspi->byte_access = 1;
344 /* Sets RSPCK, SSL, next-access delay value */
345 rspi_write8(rspi, 0x00, RSPI_SPCKD);
346 rspi_write8(rspi, 0x00, RSPI_SSLND);
347 rspi_write8(rspi, 0x00, RSPI_SPND);
349 /* Data Length Setting */
350 if (access_size == 8)
351 rspi->spcmd |= SPCMD_SPB_8BIT;
352 else if (access_size == 16)
353 rspi->spcmd |= SPCMD_SPB_16BIT;
355 rspi->spcmd |= SPCMD_SPB_32BIT;
357 rspi->spcmd |= SPCMD_SCKDEN | SPCMD_SLNDEN | SPCMD_SPNDEN;
359 /* Resets transfer data length */
360 rspi_write32(rspi, 0, QSPI_SPBMUL0);
362 /* Resets transmit and receive buffer */
363 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
364 /* Sets buffer to allow normal operation */
365 rspi_write8(rspi, 0x00, QSPI_SPBFCR);
368 rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
370 /* Enables SPI function in master mode */
371 rspi_write8(rspi, SPCR_SPE | SPCR_MSTR, RSPI_SPCR);
376 #define set_config_register(spi, n) spi->ops->set_config_register(spi, n)
378 static void rspi_enable_irq(const struct rspi_data *rspi, u8 enable)
380 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | enable, RSPI_SPCR);
383 static void rspi_disable_irq(const struct rspi_data *rspi, u8 disable)
385 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~disable, RSPI_SPCR);
388 static int rspi_wait_for_interrupt(struct rspi_data *rspi, u8 wait_mask,
393 rspi->spsr = rspi_read8(rspi, RSPI_SPSR);
394 if (rspi->spsr & wait_mask)
397 rspi_enable_irq(rspi, enable_bit);
398 ret = wait_event_timeout(rspi->wait, rspi->spsr & wait_mask, HZ);
399 if (ret == 0 && !(rspi->spsr & wait_mask))
405 static int rspi_data_out(struct rspi_data *rspi, u8 data)
407 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
408 dev_err(&rspi->master->dev, "transmit timeout\n");
411 rspi_write_data(rspi, data);
415 static int rspi_data_in(struct rspi_data *rspi)
419 if (rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE) < 0) {
420 dev_err(&rspi->master->dev, "receive timeout\n");
423 data = rspi_read_data(rspi);
427 static int rspi_data_out_in(struct rspi_data *rspi, u8 data)
431 ret = rspi_data_out(rspi, data);
435 return rspi_data_in(rspi);
438 static void rspi_dma_complete(void *arg)
440 struct rspi_data *rspi = arg;
442 rspi->dma_callbacked = 1;
443 wake_up_interruptible(&rspi->wait);
446 static int rspi_dma_map_sg(struct scatterlist *sg, const void *buf,
447 unsigned len, struct dma_chan *chan,
448 enum dma_transfer_direction dir)
450 sg_init_table(sg, 1);
451 sg_set_buf(sg, buf, len);
452 sg_dma_len(sg) = len;
453 return dma_map_sg(chan->device->dev, sg, 1, dir);
456 static void rspi_dma_unmap_sg(struct scatterlist *sg, struct dma_chan *chan,
457 enum dma_transfer_direction dir)
459 dma_unmap_sg(chan->device->dev, sg, 1, dir);
462 static void rspi_memory_to_8bit(void *buf, const void *data, unsigned len)
465 const u8 *src = data;
468 *dst++ = (u16)(*src++);
473 static void rspi_memory_from_8bit(void *buf, const void *data, unsigned len)
476 const u16 *src = data;
484 static int rspi_send_dma(struct rspi_data *rspi, struct spi_transfer *t)
486 struct scatterlist sg;
487 const void *buf = NULL;
488 struct dma_async_tx_descriptor *desc;
492 if (rspi->dma_width_16bit) {
495 * If DMAC bus width is 16-bit, the driver allocates a dummy
496 * buffer. And, the driver converts original data into the
497 * DMAC data as the following format:
498 * original data: 1st byte, 2nd byte ...
499 * DMAC data: 1st byte, dummy, 2nd byte, dummy ...
502 tmp = kmalloc(len, GFP_KERNEL);
505 rspi_memory_to_8bit(tmp, t->tx_buf, t->len);
512 if (!rspi_dma_map_sg(&sg, buf, len, rspi->chan_tx, DMA_TO_DEVICE)) {
516 desc = dmaengine_prep_slave_sg(rspi->chan_tx, &sg, 1, DMA_TO_DEVICE,
517 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
524 * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be
525 * called. So, this driver disables the IRQ while DMA transfer.
527 disable_irq(rspi->tx_irq);
529 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_TXMD, RSPI_SPCR);
530 rspi_enable_irq(rspi, SPCR_SPTIE);
531 rspi->dma_callbacked = 0;
533 desc->callback = rspi_dma_complete;
534 desc->callback_param = rspi;
535 dmaengine_submit(desc);
536 dma_async_issue_pending(rspi->chan_tx);
538 ret = wait_event_interruptible_timeout(rspi->wait,
539 rspi->dma_callbacked, HZ);
540 if (ret > 0 && rspi->dma_callbacked)
544 rspi_disable_irq(rspi, SPCR_SPTIE);
546 enable_irq(rspi->tx_irq);
549 rspi_dma_unmap_sg(&sg, rspi->chan_tx, DMA_TO_DEVICE);
551 if (rspi->dma_width_16bit)
557 static void rspi_receive_init(const struct rspi_data *rspi)
561 spsr = rspi_read8(rspi, RSPI_SPSR);
562 if (spsr & SPSR_SPRF)
563 rspi_read_data(rspi); /* dummy read */
564 if (spsr & SPSR_OVRF)
565 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPSR) & ~SPSR_OVRF,
569 static void rspi_rz_receive_init(const struct rspi_data *rspi)
571 rspi_receive_init(rspi);
572 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, RSPI_SPBFCR);
573 rspi_write8(rspi, 0, RSPI_SPBFCR);
576 static void qspi_receive_init(const struct rspi_data *rspi)
580 spsr = rspi_read8(rspi, RSPI_SPSR);
581 if (spsr & SPSR_SPRF)
582 rspi_read_data(rspi); /* dummy read */
583 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
584 rspi_write8(rspi, 0, QSPI_SPBFCR);
587 static int rspi_receive_dma(struct rspi_data *rspi, struct spi_transfer *t)
589 struct scatterlist sg, sg_dummy;
590 void *dummy = NULL, *rx_buf = NULL;
591 struct dma_async_tx_descriptor *desc, *desc_dummy;
595 if (rspi->dma_width_16bit) {
597 * If DMAC bus width is 16-bit, the driver allocates a dummy
598 * buffer. And, finally the driver converts the DMAC data into
599 * actual data as the following format:
600 * DMAC data: 1st byte, dummy, 2nd byte, dummy ...
601 * actual data: 1st byte, 2nd byte ...
604 rx_buf = kmalloc(len, GFP_KERNEL);
612 /* prepare dummy transfer to generate SPI clocks */
613 dummy = kzalloc(len, GFP_KERNEL);
618 if (!rspi_dma_map_sg(&sg_dummy, dummy, len, rspi->chan_tx,
623 desc_dummy = dmaengine_prep_slave_sg(rspi->chan_tx, &sg_dummy, 1,
624 DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
627 goto end_dummy_mapped;
630 /* prepare receive transfer */
631 if (!rspi_dma_map_sg(&sg, rx_buf, len, rspi->chan_rx,
634 goto end_dummy_mapped;
637 desc = dmaengine_prep_slave_sg(rspi->chan_rx, &sg, 1, DMA_FROM_DEVICE,
638 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
644 rspi_receive_init(rspi);
647 * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be
648 * called. So, this driver disables the IRQ while DMA transfer.
650 disable_irq(rspi->tx_irq);
651 if (rspi->rx_irq != rspi->tx_irq)
652 disable_irq(rspi->rx_irq);
654 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_TXMD, RSPI_SPCR);
655 rspi_enable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE);
656 rspi->dma_callbacked = 0;
658 desc->callback = rspi_dma_complete;
659 desc->callback_param = rspi;
660 dmaengine_submit(desc);
661 dma_async_issue_pending(rspi->chan_rx);
663 desc_dummy->callback = NULL; /* No callback */
664 dmaengine_submit(desc_dummy);
665 dma_async_issue_pending(rspi->chan_tx);
667 ret = wait_event_interruptible_timeout(rspi->wait,
668 rspi->dma_callbacked, HZ);
669 if (ret > 0 && rspi->dma_callbacked)
673 rspi_disable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE);
675 enable_irq(rspi->tx_irq);
676 if (rspi->rx_irq != rspi->tx_irq)
677 enable_irq(rspi->rx_irq);
680 rspi_dma_unmap_sg(&sg, rspi->chan_rx, DMA_FROM_DEVICE);
682 rspi_dma_unmap_sg(&sg_dummy, rspi->chan_tx, DMA_TO_DEVICE);
684 if (rspi->dma_width_16bit) {
686 rspi_memory_from_8bit(t->rx_buf, rx_buf, t->len);
694 static int rspi_is_dma(const struct rspi_data *rspi, struct spi_transfer *t)
696 if (t->tx_buf && rspi->chan_tx)
698 /* If the module receives data by DMAC, it also needs TX DMAC */
699 if (t->rx_buf && rspi->chan_tx && rspi->chan_rx)
705 static int rspi_transfer_out_in(struct rspi_data *rspi,
706 struct spi_transfer *xfer)
708 int remain = xfer->len, ret;
709 const u8 *tx_buf = xfer->tx_buf;
710 u8 *rx_buf = xfer->rx_buf;
713 rspi_receive_init(rspi);
715 spcr = rspi_read8(rspi, RSPI_SPCR);
720 rspi_write8(rspi, spcr, RSPI_SPCR);
723 data = tx_buf ? *tx_buf++ : DUMMY_DATA;
724 ret = rspi_data_out(rspi, data);
728 ret = rspi_data_in(rspi);
736 /* Wait for the last transmission */
737 rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
742 static int rspi_transfer_one(struct spi_master *master, struct spi_device *spi,
743 struct spi_transfer *xfer)
745 struct rspi_data *rspi = spi_master_get_devdata(master);
748 if (!rspi_is_dma(rspi, xfer))
749 return rspi_transfer_out_in(rspi, xfer);
752 ret = rspi_send_dma(rspi, xfer);
757 return rspi_receive_dma(rspi, xfer);
762 static int rspi_rz_transfer_out_in(struct rspi_data *rspi,
763 struct spi_transfer *xfer)
765 int remain = xfer->len, ret;
766 const u8 *tx_buf = xfer->tx_buf;
767 u8 *rx_buf = xfer->rx_buf;
770 rspi_rz_receive_init(rspi);
773 data = tx_buf ? *tx_buf++ : DUMMY_DATA;
774 ret = rspi_data_out_in(rspi, data);
782 /* Wait for the last transmission */
783 rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
788 static int rspi_rz_transfer_one(struct spi_master *master,
789 struct spi_device *spi,
790 struct spi_transfer *xfer)
792 struct rspi_data *rspi = spi_master_get_devdata(master);
794 return rspi_rz_transfer_out_in(rspi, xfer);
797 static int qspi_transfer_out_in(struct rspi_data *rspi,
798 struct spi_transfer *xfer)
800 int remain = xfer->len, ret;
801 const u8 *tx_buf = xfer->tx_buf;
802 u8 *rx_buf = xfer->rx_buf;
805 qspi_receive_init(rspi);
808 data = tx_buf ? *tx_buf++ : DUMMY_DATA;
809 ret = rspi_data_out_in(rspi, data);
817 /* Wait for the last transmission */
818 rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
823 static int qspi_transfer_out(struct rspi_data *rspi, struct spi_transfer *xfer)
825 const u8 *buf = xfer->tx_buf;
829 for (i = 0; i < xfer->len; i++) {
830 ret = rspi_data_out(rspi, *buf++);
835 /* Wait for the last transmission */
836 rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
841 static int qspi_transfer_in(struct rspi_data *rspi, struct spi_transfer *xfer)
843 u8 *buf = xfer->rx_buf;
847 for (i = 0; i < xfer->len; i++) {
848 ret = rspi_data_in(rspi);
857 static int qspi_transfer_one(struct spi_master *master, struct spi_device *spi,
858 struct spi_transfer *xfer)
860 struct rspi_data *rspi = spi_master_get_devdata(master);
862 if (xfer->tx_buf && xfer->tx_nbits > SPI_NBITS_SINGLE) {
863 /* Quad or Dual SPI Write */
864 return qspi_transfer_out(rspi, xfer);
865 } else if (xfer->rx_buf && xfer->rx_nbits > SPI_NBITS_SINGLE) {
866 /* Quad or Dual SPI Read */
867 return qspi_transfer_in(rspi, xfer);
869 /* Single SPI Transfer */
870 return qspi_transfer_out_in(rspi, xfer);
874 static int rspi_setup(struct spi_device *spi)
876 struct rspi_data *rspi = spi_master_get_devdata(spi->master);
878 rspi->max_speed_hz = spi->max_speed_hz;
880 rspi->spcmd = SPCMD_SSLKP;
881 if (spi->mode & SPI_CPOL)
882 rspi->spcmd |= SPCMD_CPOL;
883 if (spi->mode & SPI_CPHA)
884 rspi->spcmd |= SPCMD_CPHA;
886 /* CMOS output mode and MOSI signal from previous transfer */
888 if (spi->mode & SPI_LOOP)
889 rspi->sppcr |= SPPCR_SPLP;
891 set_config_register(rspi, 8);
896 static u16 qspi_transfer_mode(const struct spi_transfer *xfer)
899 switch (xfer->tx_nbits) {
901 return SPCMD_SPIMOD_QUAD;
903 return SPCMD_SPIMOD_DUAL;
908 switch (xfer->rx_nbits) {
910 return SPCMD_SPIMOD_QUAD | SPCMD_SPRW;
912 return SPCMD_SPIMOD_DUAL | SPCMD_SPRW;
920 static int qspi_setup_sequencer(struct rspi_data *rspi,
921 const struct spi_message *msg)
923 const struct spi_transfer *xfer;
924 unsigned int i = 0, len = 0;
925 u16 current_mode = 0xffff, mode;
927 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
928 mode = qspi_transfer_mode(xfer);
929 if (mode == current_mode) {
934 /* Transfer mode change */
936 /* Set transfer data length of previous transfer */
937 rspi_write32(rspi, len, QSPI_SPBMUL(i - 1));
940 if (i >= QSPI_NUM_SPCMD) {
941 dev_err(&msg->spi->dev,
942 "Too many different transfer modes");
946 /* Program transfer mode for this transfer */
947 rspi_write16(rspi, rspi->spcmd | mode, RSPI_SPCMD(i));
953 /* Set final transfer data length and sequence length */
954 rspi_write32(rspi, len, QSPI_SPBMUL(i - 1));
955 rspi_write8(rspi, i - 1, RSPI_SPSCR);
961 static int rspi_prepare_message(struct spi_master *master,
962 struct spi_message *msg)
964 struct rspi_data *rspi = spi_master_get_devdata(master);
968 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)) {
969 /* Setup sequencer for messages with multiple transfer modes */
970 ret = qspi_setup_sequencer(rspi, msg);
975 /* Enable SPI function in master mode */
976 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_SPE, RSPI_SPCR);
980 static int rspi_unprepare_message(struct spi_master *master,
981 struct spi_message *msg)
983 struct rspi_data *rspi = spi_master_get_devdata(master);
985 /* Disable SPI function */
986 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_SPE, RSPI_SPCR);
988 /* Reset sequencer for Single SPI Transfers */
989 rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
990 rspi_write8(rspi, 0, RSPI_SPSCR);
994 static irqreturn_t rspi_irq_mux(int irq, void *_sr)
996 struct rspi_data *rspi = _sr;
998 irqreturn_t ret = IRQ_NONE;
1001 rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
1002 if (spsr & SPSR_SPRF)
1003 disable_irq |= SPCR_SPRIE;
1004 if (spsr & SPSR_SPTEF)
1005 disable_irq |= SPCR_SPTIE;
1009 rspi_disable_irq(rspi, disable_irq);
1010 wake_up(&rspi->wait);
1016 static irqreturn_t rspi_irq_rx(int irq, void *_sr)
1018 struct rspi_data *rspi = _sr;
1021 rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
1022 if (spsr & SPSR_SPRF) {
1023 rspi_disable_irq(rspi, SPCR_SPRIE);
1024 wake_up(&rspi->wait);
1031 static irqreturn_t rspi_irq_tx(int irq, void *_sr)
1033 struct rspi_data *rspi = _sr;
1036 rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
1037 if (spsr & SPSR_SPTEF) {
1038 rspi_disable_irq(rspi, SPCR_SPTIE);
1039 wake_up(&rspi->wait);
1046 static int rspi_request_dma(struct rspi_data *rspi,
1047 struct platform_device *pdev)
1049 const struct rspi_plat_data *rspi_pd = dev_get_platdata(&pdev->dev);
1050 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1051 dma_cap_mask_t mask;
1052 struct dma_slave_config cfg;
1055 if (!res || !rspi_pd)
1056 return 0; /* The driver assumes no error. */
1058 rspi->dma_width_16bit = rspi_pd->dma_width_16bit;
1060 /* If the module receives data by DMAC, it also needs TX DMAC */
1061 if (rspi_pd->dma_rx_id && rspi_pd->dma_tx_id) {
1063 dma_cap_set(DMA_SLAVE, mask);
1064 rspi->chan_rx = dma_request_channel(mask, shdma_chan_filter,
1065 (void *)rspi_pd->dma_rx_id);
1066 if (rspi->chan_rx) {
1067 cfg.slave_id = rspi_pd->dma_rx_id;
1068 cfg.direction = DMA_DEV_TO_MEM;
1070 cfg.src_addr = res->start + RSPI_SPDR;
1071 ret = dmaengine_slave_config(rspi->chan_rx, &cfg);
1073 dev_info(&pdev->dev, "Use DMA when rx.\n");
1078 if (rspi_pd->dma_tx_id) {
1080 dma_cap_set(DMA_SLAVE, mask);
1081 rspi->chan_tx = dma_request_channel(mask, shdma_chan_filter,
1082 (void *)rspi_pd->dma_tx_id);
1083 if (rspi->chan_tx) {
1084 cfg.slave_id = rspi_pd->dma_tx_id;
1085 cfg.direction = DMA_MEM_TO_DEV;
1086 cfg.dst_addr = res->start + RSPI_SPDR;
1088 ret = dmaengine_slave_config(rspi->chan_tx, &cfg);
1090 dev_info(&pdev->dev, "Use DMA when tx\n");
1099 static void rspi_release_dma(struct rspi_data *rspi)
1102 dma_release_channel(rspi->chan_tx);
1104 dma_release_channel(rspi->chan_rx);
1107 static int rspi_remove(struct platform_device *pdev)
1109 struct rspi_data *rspi = platform_get_drvdata(pdev);
1111 rspi_release_dma(rspi);
1112 clk_disable_unprepare(rspi->clk);
1117 static const struct spi_ops rspi_ops = {
1118 .set_config_register = rspi_set_config_register,
1119 .transfer_one = rspi_transfer_one,
1120 .mode_bits = SPI_CPHA | SPI_CPOL | SPI_LOOP,
1123 static const struct spi_ops rspi_rz_ops = {
1124 .set_config_register = rspi_rz_set_config_register,
1125 .transfer_one = rspi_rz_transfer_one,
1126 .mode_bits = SPI_CPHA | SPI_CPOL | SPI_LOOP,
1129 static const struct spi_ops qspi_ops = {
1130 .set_config_register = qspi_set_config_register,
1131 .transfer_one = qspi_transfer_one,
1132 .mode_bits = SPI_CPHA | SPI_CPOL | SPI_LOOP |
1133 SPI_TX_DUAL | SPI_TX_QUAD |
1134 SPI_RX_DUAL | SPI_RX_QUAD,
1138 static const struct of_device_id rspi_of_match[] = {
1139 /* RSPI on legacy SH */
1140 { .compatible = "renesas,rspi", .data = &rspi_ops },
1141 /* RSPI on RZ/A1H */
1142 { .compatible = "renesas,rspi-rz", .data = &rspi_rz_ops },
1143 /* QSPI on R-Car Gen2 */
1144 { .compatible = "renesas,qspi", .data = &qspi_ops },
1148 MODULE_DEVICE_TABLE(of, rspi_of_match);
1150 static int rspi_parse_dt(struct device *dev, struct spi_master *master)
1155 /* Parse DT properties */
1156 error = of_property_read_u32(dev->of_node, "num-cs", &num_cs);
1158 dev_err(dev, "of_property_read_u32 num-cs failed %d\n", error);
1162 master->num_chipselect = num_cs;
1166 #define rspi_of_match NULL
1167 static inline int rspi_parse_dt(struct device *dev, struct spi_master *master)
1171 #endif /* CONFIG_OF */
1173 static int rspi_request_irq(struct device *dev, unsigned int irq,
1174 irq_handler_t handler, const char *suffix,
1177 const char *base = dev_name(dev);
1178 size_t len = strlen(base) + strlen(suffix) + 2;
1179 char *name = devm_kzalloc(dev, len, GFP_KERNEL);
1182 snprintf(name, len, "%s:%s", base, suffix);
1183 return devm_request_irq(dev, irq, handler, 0, name, dev_id);
1186 static int rspi_probe(struct platform_device *pdev)
1188 struct resource *res;
1189 struct spi_master *master;
1190 struct rspi_data *rspi;
1192 const struct of_device_id *of_id;
1193 const struct rspi_plat_data *rspi_pd;
1194 const struct spi_ops *ops;
1196 master = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data));
1197 if (master == NULL) {
1198 dev_err(&pdev->dev, "spi_alloc_master error.\n");
1202 of_id = of_match_device(rspi_of_match, &pdev->dev);
1205 ret = rspi_parse_dt(&pdev->dev, master);
1209 ops = (struct spi_ops *)pdev->id_entry->driver_data;
1210 rspi_pd = dev_get_platdata(&pdev->dev);
1211 if (rspi_pd && rspi_pd->num_chipselect)
1212 master->num_chipselect = rspi_pd->num_chipselect;
1214 master->num_chipselect = 2; /* default */
1217 /* ops parameter check */
1218 if (!ops->set_config_register) {
1219 dev_err(&pdev->dev, "there is no set_config_register\n");
1224 rspi = spi_master_get_devdata(master);
1225 platform_set_drvdata(pdev, rspi);
1227 rspi->master = master;
1229 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1230 rspi->addr = devm_ioremap_resource(&pdev->dev, res);
1231 if (IS_ERR(rspi->addr)) {
1232 ret = PTR_ERR(rspi->addr);
1236 rspi->clk = devm_clk_get(&pdev->dev, NULL);
1237 if (IS_ERR(rspi->clk)) {
1238 dev_err(&pdev->dev, "cannot get clock\n");
1239 ret = PTR_ERR(rspi->clk);
1243 ret = clk_prepare_enable(rspi->clk);
1245 dev_err(&pdev->dev, "unable to prepare/enable clock\n");
1249 init_waitqueue_head(&rspi->wait);
1251 master->bus_num = pdev->id;
1252 master->setup = rspi_setup;
1253 master->transfer_one = ops->transfer_one;
1254 master->prepare_message = rspi_prepare_message;
1255 master->unprepare_message = rspi_unprepare_message;
1256 master->mode_bits = ops->mode_bits;
1257 master->dev.of_node = pdev->dev.of_node;
1259 ret = platform_get_irq_byname(pdev, "rx");
1261 ret = platform_get_irq_byname(pdev, "mux");
1263 ret = platform_get_irq(pdev, 0);
1265 rspi->rx_irq = rspi->tx_irq = ret;
1268 ret = platform_get_irq_byname(pdev, "tx");
1273 dev_err(&pdev->dev, "platform_get_irq error\n");
1277 if (rspi->rx_irq == rspi->tx_irq) {
1278 /* Single multiplexed interrupt */
1279 ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_mux,
1282 /* Multi-interrupt mode, only SPRI and SPTI are used */
1283 ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_rx,
1286 ret = rspi_request_irq(&pdev->dev, rspi->tx_irq,
1287 rspi_irq_tx, "tx", rspi);
1290 dev_err(&pdev->dev, "request_irq error\n");
1294 ret = rspi_request_dma(rspi, pdev);
1296 dev_err(&pdev->dev, "rspi_request_dma failed.\n");
1300 ret = devm_spi_register_master(&pdev->dev, master);
1302 dev_err(&pdev->dev, "spi_register_master error.\n");
1306 dev_info(&pdev->dev, "probed\n");
1311 rspi_release_dma(rspi);
1313 clk_disable_unprepare(rspi->clk);
1315 spi_master_put(master);
1320 static struct platform_device_id spi_driver_ids[] = {
1321 { "rspi", (kernel_ulong_t)&rspi_ops },
1322 { "rspi-rz", (kernel_ulong_t)&rspi_rz_ops },
1323 { "qspi", (kernel_ulong_t)&qspi_ops },
1327 MODULE_DEVICE_TABLE(platform, spi_driver_ids);
1329 static struct platform_driver rspi_driver = {
1330 .probe = rspi_probe,
1331 .remove = rspi_remove,
1332 .id_table = spi_driver_ids,
1334 .name = "renesas_spi",
1335 .owner = THIS_MODULE,
1336 .of_match_table = of_match_ptr(rspi_of_match),
1339 module_platform_driver(rspi_driver);
1341 MODULE_DESCRIPTION("Renesas RSPI bus driver");
1342 MODULE_LICENSE("GPL v2");
1343 MODULE_AUTHOR("Yoshihiro Shimoda");
1344 MODULE_ALIAS("platform:rspi");