blob: 450a42ec2141a89585c8c5124f2aa36f9c457d9f [file] [log] [blame]
Wolfram Sang9135bac2018-08-22 00:02:23 +02001// SPDX-License-Identifier: GPL-2.0
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09002/*
3 * SH RSPI driver
4 *
Geert Uytterhoeven93722202014-01-24 09:43:58 +01005 * Copyright (C) 2012, 2013 Renesas Solutions Corp.
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +01006 * Copyright (C) 2014 Glider bvba
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09007 *
8 * Based on spi-sh.c:
9 * Copyright (C) 2011 Renesas Solutions Corp.
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090010 */
11
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/sched.h>
15#include <linux/errno.h>
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090016#include <linux/interrupt.h>
17#include <linux/platform_device.h>
18#include <linux/io.h>
19#include <linux/clk.h>
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +090020#include <linux/dmaengine.h>
21#include <linux/dma-mapping.h>
Geert Uytterhoeven426ef762014-01-28 10:21:38 +010022#include <linux/of_device.h>
Geert Uytterhoeven490c9772014-03-11 10:59:12 +010023#include <linux/pm_runtime.h>
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +090024#include <linux/sh_dma.h>
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090025#include <linux/spi/spi.h>
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +090026#include <linux/spi/rspi.h>
Geert Uytterhoevenf3a14a32020-03-09 18:15:37 +010027#include <linux/spinlock.h>
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090028
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010029#define RSPI_SPCR 0x00 /* Control Register */
30#define RSPI_SSLP 0x01 /* Slave Select Polarity Register */
31#define RSPI_SPPCR 0x02 /* Pin Control Register */
32#define RSPI_SPSR 0x03 /* Status Register */
33#define RSPI_SPDR 0x04 /* Data Register */
34#define RSPI_SPSCR 0x08 /* Sequence Control Register */
35#define RSPI_SPSSR 0x09 /* Sequence Status Register */
36#define RSPI_SPBR 0x0a /* Bit Rate Register */
37#define RSPI_SPDCR 0x0b /* Data Control Register */
38#define RSPI_SPCKD 0x0c /* Clock Delay Register */
39#define RSPI_SSLND 0x0d /* Slave Select Negation Delay Register */
40#define RSPI_SPND 0x0e /* Next-Access Delay Register */
Geert Uytterhoeven862d3572014-01-24 09:43:59 +010041#define RSPI_SPCR2 0x0f /* Control Register 2 (SH only) */
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010042#define RSPI_SPCMD0 0x10 /* Command Register 0 */
43#define RSPI_SPCMD1 0x12 /* Command Register 1 */
44#define RSPI_SPCMD2 0x14 /* Command Register 2 */
45#define RSPI_SPCMD3 0x16 /* Command Register 3 */
46#define RSPI_SPCMD4 0x18 /* Command Register 4 */
47#define RSPI_SPCMD5 0x1a /* Command Register 5 */
48#define RSPI_SPCMD6 0x1c /* Command Register 6 */
49#define RSPI_SPCMD7 0x1e /* Command Register 7 */
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +010050#define RSPI_SPCMD(i) (RSPI_SPCMD0 + (i) * 2)
51#define RSPI_NUM_SPCMD 8
52#define RSPI_RZ_NUM_SPCMD 4
53#define QSPI_NUM_SPCMD 4
Geert Uytterhoeven862d3572014-01-24 09:43:59 +010054
55/* RSPI on RZ only */
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010056#define RSPI_SPBFCR 0x20 /* Buffer Control Register */
57#define RSPI_SPBFDR 0x22 /* Buffer Data Count Setting Register */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090058
Geert Uytterhoeven862d3572014-01-24 09:43:59 +010059/* QSPI only */
Geert Uytterhoevenfbe50722014-01-12 11:27:38 +010060#define QSPI_SPBFCR 0x18 /* Buffer Control Register */
61#define QSPI_SPBDCR 0x1a /* Buffer Data Count Register */
62#define QSPI_SPBMUL0 0x1c /* Transfer Data Length Multiplier Setting Register 0 */
63#define QSPI_SPBMUL1 0x20 /* Transfer Data Length Multiplier Setting Register 1 */
64#define QSPI_SPBMUL2 0x24 /* Transfer Data Length Multiplier Setting Register 2 */
65#define QSPI_SPBMUL3 0x28 /* Transfer Data Length Multiplier Setting Register 3 */
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +010066#define QSPI_SPBMUL(i) (QSPI_SPBMUL0 + (i) * 4)
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +090067
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010068/* SPCR - Control Register */
69#define SPCR_SPRIE 0x80 /* Receive Interrupt Enable */
70#define SPCR_SPE 0x40 /* Function Enable */
71#define SPCR_SPTIE 0x20 /* Transmit Interrupt Enable */
72#define SPCR_SPEIE 0x10 /* Error Interrupt Enable */
73#define SPCR_MSTR 0x08 /* Master/Slave Mode Select */
74#define SPCR_MODFEN 0x04 /* Mode Fault Error Detection Enable */
75/* RSPI on SH only */
76#define SPCR_TXMD 0x02 /* TX Only Mode (vs. Full Duplex) */
77#define SPCR_SPMS 0x01 /* 3-wire Mode (vs. 4-wire) */
Geert Uytterhoeven6089af72014-08-28 10:10:19 +020078/* QSPI on R-Car Gen2 only */
Geert Uytterhoevenfbe50722014-01-12 11:27:38 +010079#define SPCR_WSWAP 0x02 /* Word Swap of read-data for DMAC */
80#define SPCR_BSWAP 0x01 /* Byte Swap of read-data for DMAC */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090081
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010082/* SSLP - Slave Select Polarity Register */
Geert Uytterhoevenf3a14a32020-03-09 18:15:37 +010083#define SSLP_SSLP(i) BIT(i) /* SSLi Signal Polarity Setting */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090084
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010085/* SPPCR - Pin Control Register */
86#define SPPCR_MOIFE 0x20 /* MOSI Idle Value Fixing Enable */
87#define SPPCR_MOIFV 0x10 /* MOSI Idle Fixed Value */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090088#define SPPCR_SPOM 0x04
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010089#define SPPCR_SPLP2 0x02 /* Loopback Mode 2 (non-inverting) */
90#define SPPCR_SPLP 0x01 /* Loopback Mode (inverting) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090091
Geert Uytterhoevenfbe50722014-01-12 11:27:38 +010092#define SPPCR_IO3FV 0x04 /* Single-/Dual-SPI Mode IO3 Output Fixed Value */
93#define SPPCR_IO2FV 0x04 /* Single-/Dual-SPI Mode IO2 Output Fixed Value */
94
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010095/* SPSR - Status Register */
96#define SPSR_SPRF 0x80 /* Receive Buffer Full Flag */
97#define SPSR_TEND 0x40 /* Transmit End */
98#define SPSR_SPTEF 0x20 /* Transmit Buffer Empty Flag */
99#define SPSR_PERF 0x08 /* Parity Error Flag */
100#define SPSR_MODF 0x04 /* Mode Fault Error Flag */
101#define SPSR_IDLNF 0x02 /* RSPI Idle Flag */
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100102#define SPSR_OVRF 0x01 /* Overrun Error Flag (RSPI only) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900103
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100104/* SPSCR - Sequence Control Register */
105#define SPSCR_SPSLN_MASK 0x07 /* Sequence Length Specification */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900106
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100107/* SPSSR - Sequence Status Register */
108#define SPSSR_SPECM_MASK 0x70 /* Command Error Mask */
109#define SPSSR_SPCP_MASK 0x07 /* Command Pointer Mask */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900110
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100111/* SPDCR - Data Control Register */
112#define SPDCR_TXDMY 0x80 /* Dummy Data Transmission Enable */
113#define SPDCR_SPLW1 0x40 /* Access Width Specification (RZ) */
114#define SPDCR_SPLW0 0x20 /* Access Width Specification (RZ) */
115#define SPDCR_SPLLWORD (SPDCR_SPLW1 | SPDCR_SPLW0)
116#define SPDCR_SPLWORD SPDCR_SPLW1
117#define SPDCR_SPLBYTE SPDCR_SPLW0
118#define SPDCR_SPLW 0x20 /* Access Width Specification (SH) */
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100119#define SPDCR_SPRDTD 0x10 /* Receive Transmit Data Select (SH) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900120#define SPDCR_SLSEL1 0x08
121#define SPDCR_SLSEL0 0x04
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100122#define SPDCR_SLSEL_MASK 0x0c /* SSL1 Output Select (SH) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900123#define SPDCR_SPFC1 0x02
124#define SPDCR_SPFC0 0x01
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100125#define SPDCR_SPFC_MASK 0x03 /* Frame Count Setting (1-4) (SH) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900126
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100127/* SPCKD - Clock Delay Register */
128#define SPCKD_SCKDL_MASK 0x07 /* Clock Delay Setting (1-8) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900129
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100130/* SSLND - Slave Select Negation Delay Register */
131#define SSLND_SLNDL_MASK 0x07 /* SSL Negation Delay Setting (1-8) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900132
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100133/* SPND - Next-Access Delay Register */
134#define SPND_SPNDL_MASK 0x07 /* Next-Access Delay Setting (1-8) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900135
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100136/* SPCR2 - Control Register 2 */
137#define SPCR2_PTE 0x08 /* Parity Self-Test Enable */
138#define SPCR2_SPIE 0x04 /* Idle Interrupt Enable */
139#define SPCR2_SPOE 0x02 /* Odd Parity Enable (vs. Even) */
140#define SPCR2_SPPE 0x01 /* Parity Enable */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900141
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100142/* SPCMDn - Command Registers */
143#define SPCMD_SCKDEN 0x8000 /* Clock Delay Setting Enable */
144#define SPCMD_SLNDEN 0x4000 /* SSL Negation Delay Setting Enable */
145#define SPCMD_SPNDEN 0x2000 /* Next-Access Delay Enable */
146#define SPCMD_LSBF 0x1000 /* LSB First */
147#define SPCMD_SPB_MASK 0x0f00 /* Data Length Setting */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900148#define SPCMD_SPB_8_TO_16(bit) (((bit - 1) << 8) & SPCMD_SPB_MASK)
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100149#define SPCMD_SPB_8BIT 0x0000 /* QSPI only */
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900150#define SPCMD_SPB_16BIT 0x0100
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900151#define SPCMD_SPB_20BIT 0x0000
152#define SPCMD_SPB_24BIT 0x0100
153#define SPCMD_SPB_32BIT 0x0200
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100154#define SPCMD_SSLKP 0x0080 /* SSL Signal Level Keeping */
Geert Uytterhoevenfbe50722014-01-12 11:27:38 +0100155#define SPCMD_SPIMOD_MASK 0x0060 /* SPI Operating Mode (QSPI only) */
156#define SPCMD_SPIMOD1 0x0040
157#define SPCMD_SPIMOD0 0x0020
158#define SPCMD_SPIMOD_SINGLE 0
159#define SPCMD_SPIMOD_DUAL SPCMD_SPIMOD0
160#define SPCMD_SPIMOD_QUAD SPCMD_SPIMOD1
161#define SPCMD_SPRW 0x0010 /* SPI Read/Write Access (Dual/Quad) */
Geert Uytterhoeven9815ed82020-01-02 14:38:21 +0100162#define SPCMD_SSLA(i) ((i) << 4) /* SSL Assert Signal Setting */
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100163#define SPCMD_BRDV_MASK 0x000c /* Bit Rate Division Setting */
Geert Uytterhoeven8dd71692020-08-19 14:58:59 +0200164#define SPCMD_BRDV(brdv) ((brdv) << 2)
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100165#define SPCMD_CPOL 0x0002 /* Clock Polarity Setting */
166#define SPCMD_CPHA 0x0001 /* Clock Phase Setting */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900167
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100168/* SPBFCR - Buffer Control Register */
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100169#define SPBFCR_TXRST 0x80 /* Transmit Buffer Data Reset */
170#define SPBFCR_RXRST 0x40 /* Receive Buffer Data Reset */
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100171#define SPBFCR_TXTRG_MASK 0x30 /* Transmit Buffer Data Triggering Number */
172#define SPBFCR_RXTRG_MASK 0x07 /* Receive Buffer Data Triggering Number */
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900173/* QSPI on R-Car Gen2 */
174#define SPBFCR_TXTRG_1B 0x00 /* 31 bytes (1 byte available) */
175#define SPBFCR_TXTRG_32B 0x30 /* 0 byte (32 bytes available) */
176#define SPBFCR_RXTRG_1B 0x00 /* 1 byte (31 bytes available) */
177#define SPBFCR_RXTRG_32B 0x07 /* 32 bytes (0 byte available) */
178
179#define QSPI_BUFFER_SIZE 32u
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900180
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900181struct rspi_data {
182 void __iomem *addr;
Geert Uytterhoevene0fe7002020-06-08 11:59:34 +0200183 u32 speed_hz;
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100184 struct spi_controller *ctlr;
Geert Uytterhoevenf3a14a32020-03-09 18:15:37 +0100185 struct platform_device *pdev;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900186 wait_queue_head_t wait;
Geert Uytterhoevenf3a14a32020-03-09 18:15:37 +0100187 spinlock_t lock; /* Protects RMW-access to RSPI_SSLP */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900188 struct clk *clk;
Geert Uytterhoeven348e5152014-01-12 11:27:43 +0100189 u16 spcmd;
Geert Uytterhoeven06a7a3c2014-01-24 09:44:00 +0100190 u8 spsr;
191 u8 sppcr;
Geert Uytterhoeven93722202014-01-24 09:43:58 +0100192 int rx_irq, tx_irq;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900193 const struct spi_ops *ops;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900194
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900195 unsigned dma_callbacked:1;
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100196 unsigned byte_access:1;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900197};
198
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100199static void rspi_write8(const struct rspi_data *rspi, u8 data, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900200{
201 iowrite8(data, rspi->addr + offset);
202}
203
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100204static void rspi_write16(const struct rspi_data *rspi, u16 data, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900205{
206 iowrite16(data, rspi->addr + offset);
207}
208
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100209static void rspi_write32(const struct rspi_data *rspi, u32 data, u16 offset)
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900210{
211 iowrite32(data, rspi->addr + offset);
212}
213
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100214static u8 rspi_read8(const struct rspi_data *rspi, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900215{
216 return ioread8(rspi->addr + offset);
217}
218
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100219static u16 rspi_read16(const struct rspi_data *rspi, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900220{
221 return ioread16(rspi->addr + offset);
222}
223
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100224static void rspi_write_data(const struct rspi_data *rspi, u16 data)
225{
226 if (rspi->byte_access)
227 rspi_write8(rspi, data, RSPI_SPDR);
228 else /* 16 bit */
229 rspi_write16(rspi, data, RSPI_SPDR);
230}
231
232static u16 rspi_read_data(const struct rspi_data *rspi)
233{
234 if (rspi->byte_access)
235 return rspi_read8(rspi, RSPI_SPDR);
236 else /* 16 bit */
237 return rspi_read16(rspi, RSPI_SPDR);
238}
239
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900240/* optional functions */
241struct spi_ops {
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100242 int (*set_config_register)(struct rspi_data *rspi, int access_size);
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100243 int (*transfer_one)(struct spi_controller *ctlr,
244 struct spi_device *spi, struct spi_transfer *xfer);
Geert Uytterhoevencd982e62020-02-18 11:58:08 +0100245 u16 extra_mode_bits;
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +0200246 u16 flags;
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +0200247 u16 fifo_size;
Geert Uytterhoeven144d8f92020-01-02 14:38:22 +0100248 u8 num_hw_ss;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900249};
250
251/*
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100252 * functions for RSPI on legacy SH
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900253 */
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100254static int rspi_set_config_register(struct rspi_data *rspi, int access_size)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900255{
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900256 int spbr;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900257
Geert Uytterhoeven06a7a3c2014-01-24 09:44:00 +0100258 /* Sets output mode, MOSI signal, and (optionally) loopback */
259 rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900260
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900261 /* Sets transfer bit rate */
Geert Uytterhoevene0fe7002020-06-08 11:59:34 +0200262 spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk), 2 * rspi->speed_hz) - 1;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900263 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
264
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100265 /* Disable dummy transmission, set 16-bit word access, 1 frame */
266 rspi_write8(rspi, 0, RSPI_SPDCR);
267 rspi->byte_access = 0;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900268
269 /* Sets RSPCK, SSL, next-access delay value */
270 rspi_write8(rspi, 0x00, RSPI_SPCKD);
271 rspi_write8(rspi, 0x00, RSPI_SSLND);
272 rspi_write8(rspi, 0x00, RSPI_SPND);
273
274 /* Sets parity, interrupt mask */
275 rspi_write8(rspi, 0x00, RSPI_SPCR2);
276
Geert Uytterhoeven26843bb2019-03-12 19:45:13 +0100277 /* Resets sequencer */
278 rspi_write8(rspi, 0, RSPI_SPSCR);
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100279 rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size);
280 rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900281
282 /* Sets RSPI mode */
283 rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
284
285 return 0;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900286}
287
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900288/*
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100289 * functions for RSPI on RZ
290 */
291static int rspi_rz_set_config_register(struct rspi_data *rspi, int access_size)
292{
293 int spbr;
Geert Uytterhoeven8dd71692020-08-19 14:58:59 +0200294 int brdv = 0;
Chris Brandtaeb8f8c2016-08-05 09:36:03 -0400295 unsigned long clksrc;
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100296
Geert Uytterhoeven06a7a3c2014-01-24 09:44:00 +0100297 /* Sets output mode, MOSI signal, and (optionally) loopback */
298 rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100299
Chris Brandtaeb8f8c2016-08-05 09:36:03 -0400300 clksrc = clk_get_rate(rspi->clk);
Geert Uytterhoeven8dd71692020-08-19 14:58:59 +0200301 while (brdv < 3) {
Geert Uytterhoevene0fe7002020-06-08 11:59:34 +0200302 if (rspi->speed_hz >= clksrc/4) /* 4=(CLK/2)/2 */
Chris Brandtaeb8f8c2016-08-05 09:36:03 -0400303 break;
Geert Uytterhoeven8dd71692020-08-19 14:58:59 +0200304 brdv++;
Chris Brandtaeb8f8c2016-08-05 09:36:03 -0400305 clksrc /= 2;
306 }
307
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100308 /* Sets transfer bit rate */
Geert Uytterhoevene0fe7002020-06-08 11:59:34 +0200309 spbr = DIV_ROUND_UP(clksrc, 2 * rspi->speed_hz) - 1;
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100310 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
Geert Uytterhoeven8dd71692020-08-19 14:58:59 +0200311 rspi->spcmd |= SPCMD_BRDV(brdv);
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100312
313 /* Disable dummy transmission, set byte access */
314 rspi_write8(rspi, SPDCR_SPLBYTE, RSPI_SPDCR);
315 rspi->byte_access = 1;
316
317 /* Sets RSPCK, SSL, next-access delay value */
318 rspi_write8(rspi, 0x00, RSPI_SPCKD);
319 rspi_write8(rspi, 0x00, RSPI_SSLND);
320 rspi_write8(rspi, 0x00, RSPI_SPND);
321
Geert Uytterhoeven26843bb2019-03-12 19:45:13 +0100322 /* Resets sequencer */
323 rspi_write8(rspi, 0, RSPI_SPSCR);
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100324 rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size);
325 rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
326
327 /* Sets RSPI mode */
328 rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
329
330 return 0;
331}
332
333/*
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900334 * functions for QSPI
335 */
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100336static int qspi_set_config_register(struct rspi_data *rspi, int access_size)
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900337{
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900338 int spbr;
339
Geert Uytterhoeven06a7a3c2014-01-24 09:44:00 +0100340 /* Sets output mode, MOSI signal, and (optionally) loopback */
341 rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900342
343 /* Sets transfer bit rate */
Geert Uytterhoevene0fe7002020-06-08 11:59:34 +0200344 spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk), 2 * rspi->speed_hz);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900345 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
346
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100347 /* Disable dummy transmission, set byte access */
348 rspi_write8(rspi, 0, RSPI_SPDCR);
349 rspi->byte_access = 1;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900350
351 /* Sets RSPCK, SSL, next-access delay value */
352 rspi_write8(rspi, 0x00, RSPI_SPCKD);
353 rspi_write8(rspi, 0x00, RSPI_SSLND);
354 rspi_write8(rspi, 0x00, RSPI_SPND);
355
356 /* Data Length Setting */
357 if (access_size == 8)
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100358 rspi->spcmd |= SPCMD_SPB_8BIT;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900359 else if (access_size == 16)
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100360 rspi->spcmd |= SPCMD_SPB_16BIT;
Laurent Pinchart8e1c8092013-11-27 01:41:44 +0100361 else
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100362 rspi->spcmd |= SPCMD_SPB_32BIT;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900363
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100364 rspi->spcmd |= SPCMD_SCKDEN | SPCMD_SLNDEN | SPCMD_SPNDEN;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900365
366 /* Resets transfer data length */
367 rspi_write32(rspi, 0, QSPI_SPBMUL0);
368
369 /* Resets transmit and receive buffer */
370 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
371 /* Sets buffer to allow normal operation */
372 rspi_write8(rspi, 0x00, QSPI_SPBFCR);
373
Geert Uytterhoeven26843bb2019-03-12 19:45:13 +0100374 /* Resets sequencer */
375 rspi_write8(rspi, 0, RSPI_SPSCR);
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100376 rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900377
Geert Uytterhoevenb458a342017-12-07 11:09:21 +0100378 /* Sets RSPI mode */
379 rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900380
381 return 0;
382}
383
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900384static void qspi_update(const struct rspi_data *rspi, u8 mask, u8 val, u8 reg)
385{
386 u8 data;
387
388 data = rspi_read8(rspi, reg);
389 data &= ~mask;
390 data |= (val & mask);
391 rspi_write8(rspi, data, reg);
392}
393
Geert Uytterhoevencb76b1c2015-06-23 15:04:29 +0200394static unsigned int qspi_set_send_trigger(struct rspi_data *rspi,
395 unsigned int len)
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900396{
397 unsigned int n;
398
399 n = min(len, QSPI_BUFFER_SIZE);
400
401 if (len >= QSPI_BUFFER_SIZE) {
402 /* sets triggering number to 32 bytes */
403 qspi_update(rspi, SPBFCR_TXTRG_MASK,
404 SPBFCR_TXTRG_32B, QSPI_SPBFCR);
405 } else {
406 /* sets triggering number to 1 byte */
407 qspi_update(rspi, SPBFCR_TXTRG_MASK,
408 SPBFCR_TXTRG_1B, QSPI_SPBFCR);
409 }
410
411 return n;
412}
413
Hiep Cao Minh3be09be2016-11-04 17:38:54 +0900414static int qspi_set_receive_trigger(struct rspi_data *rspi, unsigned int len)
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900415{
416 unsigned int n;
417
418 n = min(len, QSPI_BUFFER_SIZE);
419
420 if (len >= QSPI_BUFFER_SIZE) {
421 /* sets triggering number to 32 bytes */
422 qspi_update(rspi, SPBFCR_RXTRG_MASK,
423 SPBFCR_RXTRG_32B, QSPI_SPBFCR);
424 } else {
425 /* sets triggering number to 1 byte */
426 qspi_update(rspi, SPBFCR_RXTRG_MASK,
427 SPBFCR_RXTRG_1B, QSPI_SPBFCR);
428 }
Hiep Cao Minh3be09be2016-11-04 17:38:54 +0900429 return n;
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900430}
431
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100432static void rspi_enable_irq(const struct rspi_data *rspi, u8 enable)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900433{
434 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | enable, RSPI_SPCR);
435}
436
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100437static void rspi_disable_irq(const struct rspi_data *rspi, u8 disable)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900438{
439 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~disable, RSPI_SPCR);
440}
441
442static int rspi_wait_for_interrupt(struct rspi_data *rspi, u8 wait_mask,
443 u8 enable_bit)
444{
445 int ret;
446
447 rspi->spsr = rspi_read8(rspi, RSPI_SPSR);
Geert Uytterhoeven5dd1ad22014-02-04 11:06:24 +0100448 if (rspi->spsr & wait_mask)
449 return 0;
450
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900451 rspi_enable_irq(rspi, enable_bit);
452 ret = wait_event_timeout(rspi->wait, rspi->spsr & wait_mask, HZ);
453 if (ret == 0 && !(rspi->spsr & wait_mask))
454 return -ETIMEDOUT;
455
456 return 0;
457}
458
Geert Uytterhoeven5f684c32014-06-02 15:38:03 +0200459static inline int rspi_wait_for_tx_empty(struct rspi_data *rspi)
460{
461 return rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
462}
463
464static inline int rspi_wait_for_rx_full(struct rspi_data *rspi)
465{
466 return rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE);
467}
468
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100469static int rspi_data_out(struct rspi_data *rspi, u8 data)
470{
Geert Uytterhoeven5f684c32014-06-02 15:38:03 +0200471 int error = rspi_wait_for_tx_empty(rspi);
472 if (error < 0) {
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100473 dev_err(&rspi->ctlr->dev, "transmit timeout\n");
Geert Uytterhoeven5f684c32014-06-02 15:38:03 +0200474 return error;
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100475 }
476 rspi_write_data(rspi, data);
477 return 0;
478}
479
480static int rspi_data_in(struct rspi_data *rspi)
481{
Geert Uytterhoeven5f684c32014-06-02 15:38:03 +0200482 int error;
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100483 u8 data;
484
Geert Uytterhoeven5f684c32014-06-02 15:38:03 +0200485 error = rspi_wait_for_rx_full(rspi);
486 if (error < 0) {
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100487 dev_err(&rspi->ctlr->dev, "receive timeout\n");
Geert Uytterhoeven5f684c32014-06-02 15:38:03 +0200488 return error;
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100489 }
490 data = rspi_read_data(rspi);
491 return data;
492}
493
Geert Uytterhoeven6837b8e92014-06-02 15:38:07 +0200494static int rspi_pio_transfer(struct rspi_data *rspi, const u8 *tx, u8 *rx,
495 unsigned int n)
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100496{
Geert Uytterhoeven6837b8e92014-06-02 15:38:07 +0200497 while (n-- > 0) {
498 if (tx) {
499 int ret = rspi_data_out(rspi, *tx++);
500 if (ret < 0)
501 return ret;
502 }
503 if (rx) {
504 int ret = rspi_data_in(rspi);
505 if (ret < 0)
506 return ret;
507 *rx++ = ret;
508 }
509 }
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100510
Geert Uytterhoeven6837b8e92014-06-02 15:38:07 +0200511 return 0;
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100512}
513
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900514static void rspi_dma_complete(void *arg)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900515{
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900516 struct rspi_data *rspi = arg;
517
518 rspi->dma_callbacked = 1;
519 wake_up_interruptible(&rspi->wait);
520}
521
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200522static int rspi_dma_transfer(struct rspi_data *rspi, struct sg_table *tx,
523 struct sg_table *rx)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900524{
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200525 struct dma_async_tx_descriptor *desc_tx = NULL, *desc_rx = NULL;
526 u8 irq_mask = 0;
527 unsigned int other_irq = 0;
528 dma_cookie_t cookie;
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +0200529 int ret;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900530
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200531 /* First prepare and submit the DMA request(s), as this may fail */
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200532 if (rx) {
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100533 desc_rx = dmaengine_prep_slave_sg(rspi->ctlr->dma_rx, rx->sgl,
534 rx->nents, DMA_DEV_TO_MEM,
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200535 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200536 if (!desc_rx) {
537 ret = -EAGAIN;
538 goto no_dma_rx;
539 }
540
541 desc_rx->callback = rspi_dma_complete;
542 desc_rx->callback_param = rspi;
543 cookie = dmaengine_submit(desc_rx);
544 if (dma_submit_error(cookie)) {
545 ret = cookie;
546 goto no_dma_rx;
547 }
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200548
549 irq_mask |= SPCR_SPRIE;
550 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900551
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200552 if (tx) {
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100553 desc_tx = dmaengine_prep_slave_sg(rspi->ctlr->dma_tx, tx->sgl,
554 tx->nents, DMA_MEM_TO_DEV,
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200555 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
556 if (!desc_tx) {
557 ret = -EAGAIN;
558 goto no_dma_tx;
559 }
560
561 if (rx) {
562 /* No callback */
563 desc_tx->callback = NULL;
564 } else {
565 desc_tx->callback = rspi_dma_complete;
566 desc_tx->callback_param = rspi;
567 }
568 cookie = dmaengine_submit(desc_tx);
569 if (dma_submit_error(cookie)) {
570 ret = cookie;
571 goto no_dma_tx;
572 }
573
574 irq_mask |= SPCR_SPTIE;
575 }
576
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900577 /*
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200578 * DMAC needs SPxIE, but if SPxIE is set, the IRQ routine will be
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900579 * called. So, this driver disables the IRQ while DMA transfer.
580 */
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200581 if (tx)
582 disable_irq(other_irq = rspi->tx_irq);
583 if (rx && rspi->rx_irq != other_irq)
584 disable_irq(rspi->rx_irq);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900585
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200586 rspi_enable_irq(rspi, irq_mask);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900587 rspi->dma_callbacked = 0;
588
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200589 /* Now start DMA */
590 if (rx)
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100591 dma_async_issue_pending(rspi->ctlr->dma_rx);
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200592 if (tx)
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100593 dma_async_issue_pending(rspi->ctlr->dma_tx);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900594
595 ret = wait_event_interruptible_timeout(rspi->wait,
596 rspi->dma_callbacked, HZ);
Geert Uytterhoeven8dbbaa42018-09-05 10:49:39 +0200597 if (ret > 0 && rspi->dma_callbacked) {
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900598 ret = 0;
Geert Uytterhoeven8dbbaa42018-09-05 10:49:39 +0200599 } else {
600 if (!ret) {
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100601 dev_err(&rspi->ctlr->dev, "DMA timeout\n");
Geert Uytterhoeven8dbbaa42018-09-05 10:49:39 +0200602 ret = -ETIMEDOUT;
603 }
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200604 if (tx)
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100605 dmaengine_terminate_all(rspi->ctlr->dma_tx);
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200606 if (rx)
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100607 dmaengine_terminate_all(rspi->ctlr->dma_rx);
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200608 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900609
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200610 rspi_disable_irq(rspi, irq_mask);
611
612 if (tx)
613 enable_irq(rspi->tx_irq);
614 if (rx && rspi->rx_irq != other_irq)
615 enable_irq(rspi->rx_irq);
616
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900617 return ret;
Geert Uytterhoeven85912a82014-07-09 12:26:22 +0200618
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200619no_dma_tx:
620 if (rx)
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100621 dmaengine_terminate_all(rspi->ctlr->dma_rx);
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200622no_dma_rx:
623 if (ret == -EAGAIN) {
Geert Uytterhoeven1bec84d2020-01-02 14:38:19 +0100624 dev_warn_once(&rspi->ctlr->dev,
625 "DMA not available, falling back to PIO\n");
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200626 }
627 return ret;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900628}
629
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100630static void rspi_receive_init(const struct rspi_data *rspi)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900631{
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100632 u8 spsr;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900633
634 spsr = rspi_read8(rspi, RSPI_SPSR);
635 if (spsr & SPSR_SPRF)
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100636 rspi_read_data(rspi); /* dummy read */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900637 if (spsr & SPSR_OVRF)
638 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPSR) & ~SPSR_OVRF,
Geert Uytterhoevendf900e62013-12-23 19:34:24 +0100639 RSPI_SPSR);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900640}
641
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100642static void rspi_rz_receive_init(const struct rspi_data *rspi)
643{
644 rspi_receive_init(rspi);
645 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, RSPI_SPBFCR);
646 rspi_write8(rspi, 0, RSPI_SPBFCR);
647}
648
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100649static void qspi_receive_init(const struct rspi_data *rspi)
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900650{
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100651 u8 spsr;
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900652
653 spsr = rspi_read8(rspi, RSPI_SPSR);
654 if (spsr & SPSR_SPRF)
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100655 rspi_read_data(rspi); /* dummy read */
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900656 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
Geert Uytterhoeven340a15e2014-01-24 09:43:57 +0100657 rspi_write8(rspi, 0, QSPI_SPBFCR);
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900658}
659
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +0200660static bool __rspi_can_dma(const struct rspi_data *rspi,
661 const struct spi_transfer *xfer)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900662{
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +0200663 return xfer->len > rspi->ops->fifo_size;
664}
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900665
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100666static bool rspi_can_dma(struct spi_controller *ctlr, struct spi_device *spi,
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +0200667 struct spi_transfer *xfer)
668{
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100669 struct rspi_data *rspi = spi_controller_get_devdata(ctlr);
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +0200670
671 return __rspi_can_dma(rspi, xfer);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900672}
673
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900674static int rspi_dma_check_then_transfer(struct rspi_data *rspi,
675 struct spi_transfer *xfer)
676{
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100677 if (!rspi->ctlr->can_dma || !__rspi_can_dma(rspi, xfer))
Hiep Cao Minh63103722015-04-30 11:12:12 +0900678 return -EAGAIN;
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900679
Hiep Cao Minh63103722015-04-30 11:12:12 +0900680 /* rx_buf can be NULL on RSPI on SH in TX-only Mode */
681 return rspi_dma_transfer(rspi, &xfer->tx_sg,
682 xfer->rx_buf ? &xfer->rx_sg : NULL);
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900683}
684
Geert Uytterhoeven8b983e92014-06-02 15:38:19 +0200685static int rspi_common_transfer(struct rspi_data *rspi,
686 struct spi_transfer *xfer)
687{
688 int ret;
689
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900690 ret = rspi_dma_check_then_transfer(rspi, xfer);
691 if (ret != -EAGAIN)
692 return ret;
Geert Uytterhoeven8b983e92014-06-02 15:38:19 +0200693
694 ret = rspi_pio_transfer(rspi, xfer->tx_buf, xfer->rx_buf, xfer->len);
695 if (ret < 0)
696 return ret;
697
698 /* Wait for the last transmission */
699 rspi_wait_for_tx_empty(rspi);
700
701 return 0;
702}
703
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100704static int rspi_transfer_one(struct spi_controller *ctlr,
705 struct spi_device *spi, struct spi_transfer *xfer)
Geert Uytterhoeven8449fd72014-01-24 09:43:56 +0100706{
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100707 struct rspi_data *rspi = spi_controller_get_devdata(ctlr);
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +0200708 u8 spcr;
Geert Uytterhoeven8449fd72014-01-24 09:43:56 +0100709
Geert Uytterhoeven8449fd72014-01-24 09:43:56 +0100710 spcr = rspi_read8(rspi, RSPI_SPCR);
Geert Uytterhoeven6837b8e92014-06-02 15:38:07 +0200711 if (xfer->rx_buf) {
Geert Uytterhoeven32c64262014-06-02 15:38:04 +0200712 rspi_receive_init(rspi);
Geert Uytterhoeven8449fd72014-01-24 09:43:56 +0100713 spcr &= ~SPCR_TXMD;
Geert Uytterhoeven32c64262014-06-02 15:38:04 +0200714 } else {
Geert Uytterhoeven8449fd72014-01-24 09:43:56 +0100715 spcr |= SPCR_TXMD;
Geert Uytterhoeven32c64262014-06-02 15:38:04 +0200716 }
Geert Uytterhoeven8449fd72014-01-24 09:43:56 +0100717 rspi_write8(rspi, spcr, RSPI_SPCR);
718
Geert Uytterhoeven8b983e92014-06-02 15:38:19 +0200719 return rspi_common_transfer(rspi, xfer);
Geert Uytterhoeven8449fd72014-01-24 09:43:56 +0100720}
721
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100722static int rspi_rz_transfer_one(struct spi_controller *ctlr,
Geert Uytterhoeven03e627c2014-06-02 15:38:16 +0200723 struct spi_device *spi,
724 struct spi_transfer *xfer)
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100725{
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100726 struct rspi_data *rspi = spi_controller_get_devdata(ctlr);
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100727
728 rspi_rz_receive_init(rspi);
729
Geert Uytterhoeven8b983e92014-06-02 15:38:19 +0200730 return rspi_common_transfer(rspi, xfer);
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100731}
732
Hiep Cao Minha91bbe72015-05-22 18:59:36 +0900733static int qspi_trigger_transfer_out_in(struct rspi_data *rspi, const u8 *tx,
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900734 u8 *rx, unsigned int len)
735{
Geert Uytterhoevencb76b1c2015-06-23 15:04:29 +0200736 unsigned int i, n;
737 int ret;
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900738
739 while (len > 0) {
740 n = qspi_set_send_trigger(rspi, len);
741 qspi_set_receive_trigger(rspi, len);
Hoan Nguyen An7e95b162019-04-23 18:19:21 +0900742 ret = rspi_wait_for_tx_empty(rspi);
743 if (ret < 0) {
744 dev_err(&rspi->ctlr->dev, "transmit timeout\n");
745 return ret;
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900746 }
Hoan Nguyen An7e95b162019-04-23 18:19:21 +0900747 for (i = 0; i < n; i++)
748 rspi_write_data(rspi, *tx++);
749
750 ret = rspi_wait_for_rx_full(rspi);
751 if (ret < 0) {
752 dev_err(&rspi->ctlr->dev, "receive timeout\n");
753 return ret;
754 }
755 for (i = 0; i < n; i++)
756 *rx++ = rspi_read_data(rspi);
757
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900758 len -= n;
759 }
760
761 return 0;
762}
763
Geert Uytterhoeven340a15e2014-01-24 09:43:57 +0100764static int qspi_transfer_out_in(struct rspi_data *rspi,
765 struct spi_transfer *xfer)
766{
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900767 int ret;
768
Geert Uytterhoeven340a15e2014-01-24 09:43:57 +0100769 qspi_receive_init(rspi);
770
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900771 ret = rspi_dma_check_then_transfer(rspi, xfer);
772 if (ret != -EAGAIN)
773 return ret;
774
Hiep Cao Minhcc2e9322015-05-22 18:59:37 +0900775 return qspi_trigger_transfer_out_in(rspi, xfer->tx_buf,
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900776 xfer->rx_buf, xfer->len);
Geert Uytterhoeven340a15e2014-01-24 09:43:57 +0100777}
778
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100779static int qspi_transfer_out(struct rspi_data *rspi, struct spi_transfer *xfer)
780{
Arnd Bergmanndb300832016-11-08 14:46:12 +0100781 const u8 *tx = xfer->tx_buf;
782 unsigned int n = xfer->len;
783 unsigned int i, len;
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100784 int ret;
785
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100786 if (rspi->ctlr->can_dma && __rspi_can_dma(rspi, xfer)) {
Geert Uytterhoeven85912a82014-07-09 12:26:22 +0200787 ret = rspi_dma_transfer(rspi, &xfer->tx_sg, NULL);
788 if (ret != -EAGAIN)
789 return ret;
790 }
Geert Uytterhoeven4f12b5e2014-06-02 15:38:17 +0200791
Arnd Bergmanndb300832016-11-08 14:46:12 +0100792 while (n > 0) {
793 len = qspi_set_send_trigger(rspi, n);
Hoan Nguyen An7e95b162019-04-23 18:19:21 +0900794 ret = rspi_wait_for_tx_empty(rspi);
795 if (ret < 0) {
796 dev_err(&rspi->ctlr->dev, "transmit timeout\n");
797 return ret;
Arnd Bergmanndb300832016-11-08 14:46:12 +0100798 }
Hoan Nguyen An7e95b162019-04-23 18:19:21 +0900799 for (i = 0; i < len; i++)
800 rspi_write_data(rspi, *tx++);
801
Arnd Bergmanndb300832016-11-08 14:46:12 +0100802 n -= len;
803 }
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100804
805 /* Wait for the last transmission */
Geert Uytterhoeven5f684c32014-06-02 15:38:03 +0200806 rspi_wait_for_tx_empty(rspi);
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100807
808 return 0;
809}
810
811static int qspi_transfer_in(struct rspi_data *rspi, struct spi_transfer *xfer)
812{
Arnd Bergmanndb300832016-11-08 14:46:12 +0100813 u8 *rx = xfer->rx_buf;
814 unsigned int n = xfer->len;
815 unsigned int i, len;
816 int ret;
817
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100818 if (rspi->ctlr->can_dma && __rspi_can_dma(rspi, xfer)) {
Geert Uytterhoeven85912a82014-07-09 12:26:22 +0200819 int ret = rspi_dma_transfer(rspi, NULL, &xfer->rx_sg);
820 if (ret != -EAGAIN)
821 return ret;
822 }
Geert Uytterhoeven4f12b5e2014-06-02 15:38:17 +0200823
Arnd Bergmanndb300832016-11-08 14:46:12 +0100824 while (n > 0) {
825 len = qspi_set_receive_trigger(rspi, n);
Hoan Nguyen An7e95b162019-04-23 18:19:21 +0900826 ret = rspi_wait_for_rx_full(rspi);
827 if (ret < 0) {
828 dev_err(&rspi->ctlr->dev, "receive timeout\n");
829 return ret;
Arnd Bergmanndb300832016-11-08 14:46:12 +0100830 }
Hoan Nguyen An7e95b162019-04-23 18:19:21 +0900831 for (i = 0; i < len; i++)
832 *rx++ = rspi_read_data(rspi);
833
Arnd Bergmanndb300832016-11-08 14:46:12 +0100834 n -= len;
835 }
836
837 return 0;
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100838}
839
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100840static int qspi_transfer_one(struct spi_controller *ctlr,
841 struct spi_device *spi, struct spi_transfer *xfer)
Geert Uytterhoeveneb557f72014-01-24 09:43:55 +0100842{
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100843 struct rspi_data *rspi = spi_controller_get_devdata(ctlr);
Geert Uytterhoeveneb557f72014-01-24 09:43:55 +0100844
Geert Uytterhoevenba824d42014-02-21 17:29:18 +0100845 if (spi->mode & SPI_LOOP) {
846 return qspi_transfer_out_in(rspi, xfer);
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +0200847 } else if (xfer->tx_nbits > SPI_NBITS_SINGLE) {
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100848 /* Quad or Dual SPI Write */
849 return qspi_transfer_out(rspi, xfer);
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +0200850 } else if (xfer->rx_nbits > SPI_NBITS_SINGLE) {
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100851 /* Quad or Dual SPI Read */
852 return qspi_transfer_in(rspi, xfer);
853 } else {
854 /* Single SPI Transfer */
855 return qspi_transfer_out_in(rspi, xfer);
856 }
Geert Uytterhoeveneb557f72014-01-24 09:43:55 +0100857}
858
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100859static u16 qspi_transfer_mode(const struct spi_transfer *xfer)
860{
861 if (xfer->tx_buf)
862 switch (xfer->tx_nbits) {
863 case SPI_NBITS_QUAD:
864 return SPCMD_SPIMOD_QUAD;
865 case SPI_NBITS_DUAL:
866 return SPCMD_SPIMOD_DUAL;
867 default:
868 return 0;
869 }
870 if (xfer->rx_buf)
871 switch (xfer->rx_nbits) {
872 case SPI_NBITS_QUAD:
873 return SPCMD_SPIMOD_QUAD | SPCMD_SPRW;
874 case SPI_NBITS_DUAL:
875 return SPCMD_SPIMOD_DUAL | SPCMD_SPRW;
876 default:
877 return 0;
878 }
879
880 return 0;
881}
882
883static int qspi_setup_sequencer(struct rspi_data *rspi,
884 const struct spi_message *msg)
885{
886 const struct spi_transfer *xfer;
887 unsigned int i = 0, len = 0;
888 u16 current_mode = 0xffff, mode;
889
890 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
891 mode = qspi_transfer_mode(xfer);
892 if (mode == current_mode) {
893 len += xfer->len;
894 continue;
895 }
896
897 /* Transfer mode change */
898 if (i) {
899 /* Set transfer data length of previous transfer */
900 rspi_write32(rspi, len, QSPI_SPBMUL(i - 1));
901 }
902
903 if (i >= QSPI_NUM_SPCMD) {
904 dev_err(&msg->spi->dev,
905 "Too many different transfer modes");
906 return -EINVAL;
907 }
908
909 /* Program transfer mode for this transfer */
910 rspi_write16(rspi, rspi->spcmd | mode, RSPI_SPCMD(i));
911 current_mode = mode;
912 len = xfer->len;
913 i++;
914 }
915 if (i) {
916 /* Set final transfer data length and sequence length */
917 rspi_write32(rspi, len, QSPI_SPBMUL(i - 1));
918 rspi_write8(rspi, i - 1, RSPI_SPSCR);
919 }
920
921 return 0;
922}
923
Geert Uytterhoevenf3a14a32020-03-09 18:15:37 +0100924static int rspi_setup(struct spi_device *spi)
925{
926 struct rspi_data *rspi = spi_controller_get_devdata(spi->controller);
927 u8 sslp;
928
929 if (spi->cs_gpiod)
930 return 0;
931
932 pm_runtime_get_sync(&rspi->pdev->dev);
933 spin_lock_irq(&rspi->lock);
934
935 sslp = rspi_read8(rspi, RSPI_SSLP);
936 if (spi->mode & SPI_CS_HIGH)
937 sslp |= SSLP_SSLP(spi->chip_select);
938 else
939 sslp &= ~SSLP_SSLP(spi->chip_select);
940 rspi_write8(rspi, sslp, RSPI_SSLP);
941
942 spin_unlock_irq(&rspi->lock);
943 pm_runtime_put(&rspi->pdev->dev);
944 return 0;
945}
946
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100947static int rspi_prepare_message(struct spi_controller *ctlr,
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100948 struct spi_message *msg)
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100949{
Geert Uytterhoeven9428a072019-02-08 10:09:07 +0100950 struct rspi_data *rspi = spi_controller_get_devdata(ctlr);
Geert Uytterhoeven42bdaae2019-03-12 19:43:31 +0100951 struct spi_device *spi = msg->spi;
Geert Uytterhoevene0fe7002020-06-08 11:59:34 +0200952 const struct spi_transfer *xfer;
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100953 int ret;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900954
Geert Uytterhoevene0fe7002020-06-08 11:59:34 +0200955 /*
956 * As the Bit Rate Register must not be changed while the device is
957 * active, all transfers in a message must use the same bit rate.
958 * In theory, the sequencer could be enabled, and each Command Register
959 * could divide the base bit rate by a different value.
960 * However, most RSPI variants do not have Transfer Data Length
961 * Multiplier Setting Registers, so each sequence step would be limited
962 * to a single word, making this feature unsuitable for large
963 * transfers, which would gain most from it.
964 */
965 rspi->speed_hz = spi->max_speed_hz;
966 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
967 if (xfer->speed_hz < rspi->speed_hz)
968 rspi->speed_hz = xfer->speed_hz;
969 }
Geert Uytterhoeven42bdaae2019-03-12 19:43:31 +0100970
971 rspi->spcmd = SPCMD_SSLKP;
972 if (spi->mode & SPI_CPOL)
973 rspi->spcmd |= SPCMD_CPOL;
974 if (spi->mode & SPI_CPHA)
975 rspi->spcmd |= SPCMD_CPHA;
Geert Uytterhoevenc046f8f2020-02-18 11:58:09 +0100976 if (spi->mode & SPI_LSB_FIRST)
977 rspi->spcmd |= SPCMD_LSBF;
Geert Uytterhoeven42bdaae2019-03-12 19:43:31 +0100978
Geert Uytterhoeven9815ed82020-01-02 14:38:21 +0100979 /* Configure slave signal to assert */
Geert Uytterhoeven144d8f92020-01-02 14:38:22 +0100980 rspi->spcmd |= SPCMD_SSLA(spi->cs_gpiod ? rspi->ctlr->unused_native_cs
981 : spi->chip_select);
Geert Uytterhoeven9815ed82020-01-02 14:38:21 +0100982
Geert Uytterhoeven42bdaae2019-03-12 19:43:31 +0100983 /* CMOS output mode and MOSI signal from previous transfer */
984 rspi->sppcr = 0;
985 if (spi->mode & SPI_LOOP)
986 rspi->sppcr |= SPPCR_SPLP;
987
Geert Uytterhoeven8f2344f2020-01-02 14:38:20 +0100988 rspi->ops->set_config_register(rspi, 8);
Geert Uytterhoeven42bdaae2019-03-12 19:43:31 +0100989
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100990 if (msg->spi->mode &
991 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)) {
992 /* Setup sequencer for messages with multiple transfer modes */
993 ret = qspi_setup_sequencer(rspi, msg);
994 if (ret < 0)
995 return ret;
996 }
997
998 /* Enable SPI function in master mode */
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100999 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_SPE, RSPI_SPCR);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001000 return 0;
1001}
1002
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001003static int rspi_unprepare_message(struct spi_controller *ctlr,
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +01001004 struct spi_message *msg)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001005{
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001006 struct rspi_data *rspi = spi_controller_get_devdata(ctlr);
Geert Uytterhoeven79d23492014-01-24 09:43:52 +01001007
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +01001008 /* Disable SPI function */
Geert Uytterhoeven79d23492014-01-24 09:43:52 +01001009 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_SPE, RSPI_SPCR);
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +01001010
1011 /* Reset sequencer for Single SPI Transfers */
1012 rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
1013 rspi_write8(rspi, 0, RSPI_SPSCR);
Geert Uytterhoeven79d23492014-01-24 09:43:52 +01001014 return 0;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001015}
1016
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001017static irqreturn_t rspi_irq_mux(int irq, void *_sr)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001018{
Geert Uytterhoevenc132f092013-12-24 10:49:31 +01001019 struct rspi_data *rspi = _sr;
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +01001020 u8 spsr;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001021 irqreturn_t ret = IRQ_NONE;
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +01001022 u8 disable_irq = 0;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001023
1024 rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
1025 if (spsr & SPSR_SPRF)
1026 disable_irq |= SPCR_SPRIE;
1027 if (spsr & SPSR_SPTEF)
1028 disable_irq |= SPCR_SPTIE;
1029
1030 if (disable_irq) {
1031 ret = IRQ_HANDLED;
1032 rspi_disable_irq(rspi, disable_irq);
1033 wake_up(&rspi->wait);
1034 }
1035
1036 return ret;
1037}
1038
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001039static irqreturn_t rspi_irq_rx(int irq, void *_sr)
1040{
1041 struct rspi_data *rspi = _sr;
1042 u8 spsr;
1043
1044 rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
1045 if (spsr & SPSR_SPRF) {
1046 rspi_disable_irq(rspi, SPCR_SPRIE);
1047 wake_up(&rspi->wait);
1048 return IRQ_HANDLED;
1049 }
1050
1051 return 0;
1052}
1053
1054static irqreturn_t rspi_irq_tx(int irq, void *_sr)
1055{
1056 struct rspi_data *rspi = _sr;
1057 u8 spsr;
1058
1059 rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
1060 if (spsr & SPSR_SPTEF) {
1061 rspi_disable_irq(rspi, SPCR_SPTIE);
1062 wake_up(&rspi->wait);
1063 return IRQ_HANDLED;
1064 }
1065
1066 return 0;
1067}
1068
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001069static struct dma_chan *rspi_request_dma_chan(struct device *dev,
1070 enum dma_transfer_direction dir,
1071 unsigned int id,
1072 dma_addr_t port_addr)
1073{
1074 dma_cap_mask_t mask;
1075 struct dma_chan *chan;
1076 struct dma_slave_config cfg;
1077 int ret;
1078
1079 dma_cap_zero(mask);
1080 dma_cap_set(DMA_SLAVE, mask);
1081
Geert Uytterhoevene825b8d2014-08-06 14:59:02 +02001082 chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
1083 (void *)(unsigned long)id, dev,
1084 dir == DMA_MEM_TO_DEV ? "tx" : "rx");
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001085 if (!chan) {
Geert Uytterhoevene825b8d2014-08-06 14:59:02 +02001086 dev_warn(dev, "dma_request_slave_channel_compat failed\n");
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001087 return NULL;
1088 }
1089
1090 memset(&cfg, 0, sizeof(cfg));
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001091 cfg.direction = dir;
Geert Uytterhoevena30b95a72014-08-06 14:59:01 +02001092 if (dir == DMA_MEM_TO_DEV) {
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001093 cfg.dst_addr = port_addr;
Geert Uytterhoevena30b95a72014-08-06 14:59:01 +02001094 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1095 } else {
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001096 cfg.src_addr = port_addr;
Geert Uytterhoevena30b95a72014-08-06 14:59:01 +02001097 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1098 }
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001099
1100 ret = dmaengine_slave_config(chan, &cfg);
1101 if (ret) {
1102 dev_warn(dev, "dmaengine_slave_config failed %d\n", ret);
1103 dma_release_channel(chan);
1104 return NULL;
1105 }
1106
1107 return chan;
1108}
1109
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001110static int rspi_request_dma(struct device *dev, struct spi_controller *ctlr,
Geert Uytterhoevenfcdc49a2014-06-02 15:38:10 +02001111 const struct resource *res)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001112{
Geert Uytterhoevenfcdc49a2014-06-02 15:38:10 +02001113 const struct rspi_plat_data *rspi_pd = dev_get_platdata(dev);
Geert Uytterhoevene825b8d2014-08-06 14:59:02 +02001114 unsigned int dma_tx_id, dma_rx_id;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001115
Geert Uytterhoevene825b8d2014-08-06 14:59:02 +02001116 if (dev->of_node) {
1117 /* In the OF case we will get the slave IDs from the DT */
1118 dma_tx_id = 0;
1119 dma_rx_id = 0;
1120 } else if (rspi_pd && rspi_pd->dma_tx_id && rspi_pd->dma_rx_id) {
1121 dma_tx_id = rspi_pd->dma_tx_id;
1122 dma_rx_id = rspi_pd->dma_rx_id;
1123 } else {
1124 /* The driver assumes no error. */
1125 return 0;
1126 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001127
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001128 ctlr->dma_tx = rspi_request_dma_chan(dev, DMA_MEM_TO_DEV, dma_tx_id,
1129 res->start + RSPI_SPDR);
1130 if (!ctlr->dma_tx)
Geert Uytterhoeven5f338d02014-06-02 15:38:11 +02001131 return -ENODEV;
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001132
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001133 ctlr->dma_rx = rspi_request_dma_chan(dev, DMA_DEV_TO_MEM, dma_rx_id,
1134 res->start + RSPI_SPDR);
1135 if (!ctlr->dma_rx) {
1136 dma_release_channel(ctlr->dma_tx);
1137 ctlr->dma_tx = NULL;
Geert Uytterhoeven5f338d02014-06-02 15:38:11 +02001138 return -ENODEV;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001139 }
Shimoda, Yoshihiro0243c5362012-08-02 17:17:33 +09001140
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001141 ctlr->can_dma = rspi_can_dma;
Geert Uytterhoeven5f338d02014-06-02 15:38:11 +02001142 dev_info(dev, "DMA available");
Shimoda, Yoshihiro0243c5362012-08-02 17:17:33 +09001143 return 0;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001144}
1145
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001146static void rspi_release_dma(struct spi_controller *ctlr)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001147{
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001148 if (ctlr->dma_tx)
1149 dma_release_channel(ctlr->dma_tx);
1150 if (ctlr->dma_rx)
1151 dma_release_channel(ctlr->dma_rx);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001152}
1153
Grant Likelyfd4a3192012-12-07 16:57:14 +00001154static int rspi_remove(struct platform_device *pdev)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001155{
Laurent Pinchart5ffbe2d2013-11-27 01:41:45 +01001156 struct rspi_data *rspi = platform_get_drvdata(pdev);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001157
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001158 rspi_release_dma(rspi->ctlr);
Geert Uytterhoeven490c9772014-03-11 10:59:12 +01001159 pm_runtime_disable(&pdev->dev);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001160
1161 return 0;
1162}
1163
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001164static const struct spi_ops rspi_ops = {
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +02001165 .set_config_register = rspi_set_config_register,
1166 .transfer_one = rspi_transfer_one,
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001167 .flags = SPI_CONTROLLER_MUST_TX,
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +02001168 .fifo_size = 8,
Geert Uytterhoeven144d8f92020-01-02 14:38:22 +01001169 .num_hw_ss = 2,
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001170};
1171
1172static const struct spi_ops rspi_rz_ops = {
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +02001173 .set_config_register = rspi_rz_set_config_register,
1174 .transfer_one = rspi_rz_transfer_one,
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001175 .flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX,
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +02001176 .fifo_size = 8, /* 8 for TX, 32 for RX */
Geert Uytterhoeven144d8f92020-01-02 14:38:22 +01001177 .num_hw_ss = 1,
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001178};
1179
1180static const struct spi_ops qspi_ops = {
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +02001181 .set_config_register = qspi_set_config_register,
1182 .transfer_one = qspi_transfer_one,
Geert Uytterhoevencd982e62020-02-18 11:58:08 +01001183 .extra_mode_bits = SPI_TX_DUAL | SPI_TX_QUAD |
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +02001184 SPI_RX_DUAL | SPI_RX_QUAD,
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001185 .flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX,
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +02001186 .fifo_size = 32,
Geert Uytterhoeven144d8f92020-01-02 14:38:22 +01001187 .num_hw_ss = 1,
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001188};
1189
1190#ifdef CONFIG_OF
1191static const struct of_device_id rspi_of_match[] = {
1192 /* RSPI on legacy SH */
1193 { .compatible = "renesas,rspi", .data = &rspi_ops },
1194 /* RSPI on RZ/A1H */
1195 { .compatible = "renesas,rspi-rz", .data = &rspi_rz_ops },
1196 /* QSPI on R-Car Gen2 */
1197 { .compatible = "renesas,qspi", .data = &qspi_ops },
1198 { /* sentinel */ }
1199};
1200
1201MODULE_DEVICE_TABLE(of, rspi_of_match);
1202
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001203static int rspi_parse_dt(struct device *dev, struct spi_controller *ctlr)
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001204{
1205 u32 num_cs;
1206 int error;
1207
1208 /* Parse DT properties */
1209 error = of_property_read_u32(dev->of_node, "num-cs", &num_cs);
1210 if (error) {
1211 dev_err(dev, "of_property_read_u32 num-cs failed %d\n", error);
1212 return error;
1213 }
1214
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001215 ctlr->num_chipselect = num_cs;
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001216 return 0;
1217}
1218#else
Shimoda, Yoshihiro64b67de2014-02-03 10:43:46 +09001219#define rspi_of_match NULL
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001220static inline int rspi_parse_dt(struct device *dev, struct spi_controller *ctlr)
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001221{
1222 return -EINVAL;
1223}
1224#endif /* CONFIG_OF */
1225
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001226static int rspi_request_irq(struct device *dev, unsigned int irq,
1227 irq_handler_t handler, const char *suffix,
1228 void *dev_id)
1229{
Geert Uytterhoeven43937452014-08-06 14:59:00 +02001230 const char *name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s",
1231 dev_name(dev), suffix);
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001232 if (!name)
1233 return -ENOMEM;
Geert Uytterhoeven43937452014-08-06 14:59:00 +02001234
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001235 return devm_request_irq(dev, irq, handler, 0, name, dev_id);
1236}
1237
Grant Likelyfd4a3192012-12-07 16:57:14 +00001238static int rspi_probe(struct platform_device *pdev)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001239{
1240 struct resource *res;
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001241 struct spi_controller *ctlr;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001242 struct rspi_data *rspi;
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001243 int ret;
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001244 const struct rspi_plat_data *rspi_pd;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001245 const struct spi_ops *ops;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001246
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001247 ctlr = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data));
1248 if (ctlr == NULL)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001249 return -ENOMEM;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001250
Geert Uytterhoeven219a7bc2017-10-04 14:19:53 +02001251 ops = of_device_get_match_data(&pdev->dev);
1252 if (ops) {
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001253 ret = rspi_parse_dt(&pdev->dev, ctlr);
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001254 if (ret)
1255 goto error1;
1256 } else {
1257 ops = (struct spi_ops *)pdev->id_entry->driver_data;
1258 rspi_pd = dev_get_platdata(&pdev->dev);
1259 if (rspi_pd && rspi_pd->num_chipselect)
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001260 ctlr->num_chipselect = rspi_pd->num_chipselect;
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001261 else
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001262 ctlr->num_chipselect = 2; /* default */
Geert Uytterhoevend64b4722014-08-06 14:58:59 +02001263 }
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001264
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001265 rspi = spi_controller_get_devdata(ctlr);
Jingoo Han24b5a822013-05-23 19:20:40 +09001266 platform_set_drvdata(pdev, rspi);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001267 rspi->ops = ops;
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001268 rspi->ctlr = ctlr;
Laurent Pinchart5d79e9a2013-11-27 01:41:46 +01001269
1270 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1271 rspi->addr = devm_ioremap_resource(&pdev->dev, res);
1272 if (IS_ERR(rspi->addr)) {
1273 ret = PTR_ERR(rspi->addr);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001274 goto error1;
1275 }
1276
Geert Uytterhoeven29f397b2014-01-24 09:44:02 +01001277 rspi->clk = devm_clk_get(&pdev->dev, NULL);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001278 if (IS_ERR(rspi->clk)) {
1279 dev_err(&pdev->dev, "cannot get clock\n");
1280 ret = PTR_ERR(rspi->clk);
Laurent Pinchart5d79e9a2013-11-27 01:41:46 +01001281 goto error1;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001282 }
Geert Uytterhoeven17fe0d92014-01-24 09:44:01 +01001283
Geert Uytterhoevenf3a14a32020-03-09 18:15:37 +01001284 rspi->pdev = pdev;
Geert Uytterhoeven490c9772014-03-11 10:59:12 +01001285 pm_runtime_enable(&pdev->dev);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001286
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001287 init_waitqueue_head(&rspi->wait);
Geert Uytterhoevenf3a14a32020-03-09 18:15:37 +01001288 spin_lock_init(&rspi->lock);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001289
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001290 ctlr->bus_num = pdev->id;
Geert Uytterhoevenf3a14a32020-03-09 18:15:37 +01001291 ctlr->setup = rspi_setup;
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001292 ctlr->auto_runtime_pm = true;
1293 ctlr->transfer_one = ops->transfer_one;
1294 ctlr->prepare_message = rspi_prepare_message;
1295 ctlr->unprepare_message = rspi_unprepare_message;
Geert Uytterhoevenf3a14a32020-03-09 18:15:37 +01001296 ctlr->mode_bits = SPI_CPHA | SPI_CPOL | SPI_CS_HIGH | SPI_LSB_FIRST |
1297 SPI_LOOP | ops->extra_mode_bits;
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001298 ctlr->flags = ops->flags;
1299 ctlr->dev.of_node = pdev->dev.of_node;
Geert Uytterhoeven144d8f92020-01-02 14:38:22 +01001300 ctlr->use_gpio_descriptors = true;
1301 ctlr->max_native_cs = rspi->ops->num_hw_ss;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001302
Geert Uytterhoeven2de860b2019-10-16 16:31:01 +02001303 ret = platform_get_irq_byname_optional(pdev, "rx");
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001304 if (ret < 0) {
Geert Uytterhoeven2de860b2019-10-16 16:31:01 +02001305 ret = platform_get_irq_byname_optional(pdev, "mux");
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001306 if (ret < 0)
1307 ret = platform_get_irq(pdev, 0);
1308 if (ret >= 0)
1309 rspi->rx_irq = rspi->tx_irq = ret;
1310 } else {
1311 rspi->rx_irq = ret;
1312 ret = platform_get_irq_byname(pdev, "tx");
1313 if (ret >= 0)
1314 rspi->tx_irq = ret;
1315 }
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001316
1317 if (rspi->rx_irq == rspi->tx_irq) {
1318 /* Single multiplexed interrupt */
1319 ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_mux,
1320 "mux", rspi);
1321 } else {
1322 /* Multi-interrupt mode, only SPRI and SPTI are used */
1323 ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_rx,
1324 "rx", rspi);
1325 if (!ret)
1326 ret = rspi_request_irq(&pdev->dev, rspi->tx_irq,
1327 rspi_irq_tx, "tx", rspi);
1328 }
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001329 if (ret < 0) {
1330 dev_err(&pdev->dev, "request_irq error\n");
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +01001331 goto error2;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001332 }
1333
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001334 ret = rspi_request_dma(&pdev->dev, ctlr, res);
Geert Uytterhoeven27e105a2014-06-02 15:38:08 +02001335 if (ret < 0)
1336 dev_warn(&pdev->dev, "DMA not available, using PIO\n");
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001337
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001338 ret = devm_spi_register_controller(&pdev->dev, ctlr);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001339 if (ret < 0) {
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001340 dev_err(&pdev->dev, "devm_spi_register_controller error.\n");
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +01001341 goto error3;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001342 }
1343
1344 dev_info(&pdev->dev, "probed\n");
1345
1346 return 0;
1347
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +01001348error3:
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001349 rspi_release_dma(ctlr);
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +01001350error2:
Geert Uytterhoeven490c9772014-03-11 10:59:12 +01001351 pm_runtime_disable(&pdev->dev);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001352error1:
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001353 spi_controller_put(ctlr);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001354
1355 return ret;
1356}
1357
Krzysztof Kozlowski8634daf2015-05-02 00:44:05 +09001358static const struct platform_device_id spi_driver_ids[] = {
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001359 { "rspi", (kernel_ulong_t)&rspi_ops },
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001360 {},
1361};
1362
1363MODULE_DEVICE_TABLE(platform, spi_driver_ids);
1364
Geert Uytterhoevenc1ca59c2018-09-05 10:49:38 +02001365#ifdef CONFIG_PM_SLEEP
1366static int rspi_suspend(struct device *dev)
1367{
Wolfram Sangbe0bf622018-10-21 22:00:45 +02001368 struct rspi_data *rspi = dev_get_drvdata(dev);
Geert Uytterhoevenc1ca59c2018-09-05 10:49:38 +02001369
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001370 return spi_controller_suspend(rspi->ctlr);
Geert Uytterhoevenc1ca59c2018-09-05 10:49:38 +02001371}
1372
1373static int rspi_resume(struct device *dev)
1374{
Wolfram Sangbe0bf622018-10-21 22:00:45 +02001375 struct rspi_data *rspi = dev_get_drvdata(dev);
Geert Uytterhoevenc1ca59c2018-09-05 10:49:38 +02001376
Geert Uytterhoeven9428a072019-02-08 10:09:07 +01001377 return spi_controller_resume(rspi->ctlr);
Geert Uytterhoevenc1ca59c2018-09-05 10:49:38 +02001378}
1379
1380static SIMPLE_DEV_PM_OPS(rspi_pm_ops, rspi_suspend, rspi_resume);
1381#define DEV_PM_OPS &rspi_pm_ops
1382#else
1383#define DEV_PM_OPS NULL
1384#endif /* CONFIG_PM_SLEEP */
1385
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001386static struct platform_driver rspi_driver = {
1387 .probe = rspi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001388 .remove = rspi_remove,
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001389 .id_table = spi_driver_ids,
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001390 .driver = {
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001391 .name = "renesas_spi",
Geert Uytterhoevenc1ca59c2018-09-05 10:49:38 +02001392 .pm = DEV_PM_OPS,
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001393 .of_match_table = of_match_ptr(rspi_of_match),
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001394 },
1395};
1396module_platform_driver(rspi_driver);
1397
1398MODULE_DESCRIPTION("Renesas RSPI bus driver");
1399MODULE_LICENSE("GPL v2");
1400MODULE_AUTHOR("Yoshihiro Shimoda");
1401MODULE_ALIAS("platform:rspi");