blob: 0e4d169c90d7045268dbf3abe56cf87f90d34b6d [file] [log] [blame]
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001/*
2 * SH RSPI driver
3 *
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 *
6 * Based on spi-sh.c:
7 * Copyright (C) 2011 Renesas Solutions Corp.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/sched.h>
27#include <linux/errno.h>
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090028#include <linux/interrupt.h>
29#include <linux/platform_device.h>
30#include <linux/io.h>
31#include <linux/clk.h>
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +090032#include <linux/dmaengine.h>
33#include <linux/dma-mapping.h>
34#include <linux/sh_dma.h>
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090035#include <linux/spi/spi.h>
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +090036#include <linux/spi/rspi.h>
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090037
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010038#define RSPI_SPCR 0x00 /* Control Register */
39#define RSPI_SSLP 0x01 /* Slave Select Polarity Register */
40#define RSPI_SPPCR 0x02 /* Pin Control Register */
41#define RSPI_SPSR 0x03 /* Status Register */
42#define RSPI_SPDR 0x04 /* Data Register */
43#define RSPI_SPSCR 0x08 /* Sequence Control Register */
44#define RSPI_SPSSR 0x09 /* Sequence Status Register */
45#define RSPI_SPBR 0x0a /* Bit Rate Register */
46#define RSPI_SPDCR 0x0b /* Data Control Register */
47#define RSPI_SPCKD 0x0c /* Clock Delay Register */
48#define RSPI_SSLND 0x0d /* Slave Select Negation Delay Register */
49#define RSPI_SPND 0x0e /* Next-Access Delay Register */
50#define RSPI_SPCR2 0x0f /* Control Register 2 */
51#define RSPI_SPCMD0 0x10 /* Command Register 0 */
52#define RSPI_SPCMD1 0x12 /* Command Register 1 */
53#define RSPI_SPCMD2 0x14 /* Command Register 2 */
54#define RSPI_SPCMD3 0x16 /* Command Register 3 */
55#define RSPI_SPCMD4 0x18 /* Command Register 4 */
56#define RSPI_SPCMD5 0x1a /* Command Register 5 */
57#define RSPI_SPCMD6 0x1c /* Command Register 6 */
58#define RSPI_SPCMD7 0x1e /* Command Register 7 */
59#define RSPI_SPBFCR 0x20 /* Buffer Control Register */
60#define RSPI_SPBFDR 0x22 /* Buffer Data Count Setting Register */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090061
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +090062/*qspi only */
Geert Uytterhoevenfbe50722014-01-12 11:27:38 +010063#define QSPI_SPBFCR 0x18 /* Buffer Control Register */
64#define QSPI_SPBDCR 0x1a /* Buffer Data Count Register */
65#define QSPI_SPBMUL0 0x1c /* Transfer Data Length Multiplier Setting Register 0 */
66#define QSPI_SPBMUL1 0x20 /* Transfer Data Length Multiplier Setting Register 1 */
67#define QSPI_SPBMUL2 0x24 /* Transfer Data Length Multiplier Setting Register 2 */
68#define QSPI_SPBMUL3 0x28 /* Transfer Data Length Multiplier Setting Register 3 */
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +090069
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010070/* SPCR - Control Register */
71#define SPCR_SPRIE 0x80 /* Receive Interrupt Enable */
72#define SPCR_SPE 0x40 /* Function Enable */
73#define SPCR_SPTIE 0x20 /* Transmit Interrupt Enable */
74#define SPCR_SPEIE 0x10 /* Error Interrupt Enable */
75#define SPCR_MSTR 0x08 /* Master/Slave Mode Select */
76#define SPCR_MODFEN 0x04 /* Mode Fault Error Detection Enable */
77/* RSPI on SH only */
78#define SPCR_TXMD 0x02 /* TX Only Mode (vs. Full Duplex) */
79#define SPCR_SPMS 0x01 /* 3-wire Mode (vs. 4-wire) */
Geert Uytterhoevenfbe50722014-01-12 11:27:38 +010080/* QSPI on R-Car M2 only */
81#define SPCR_WSWAP 0x02 /* Word Swap of read-data for DMAC */
82#define SPCR_BSWAP 0x01 /* Byte Swap of read-data for DMAC */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090083
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010084/* SSLP - Slave Select Polarity Register */
85#define SSLP_SSL1P 0x02 /* SSL1 Signal Polarity Setting */
86#define SSLP_SSL0P 0x01 /* SSL0 Signal Polarity Setting */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090087
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010088/* SPPCR - Pin Control Register */
89#define SPPCR_MOIFE 0x20 /* MOSI Idle Value Fixing Enable */
90#define SPPCR_MOIFV 0x10 /* MOSI Idle Fixed Value */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090091#define SPPCR_SPOM 0x04
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010092#define SPPCR_SPLP2 0x02 /* Loopback Mode 2 (non-inverting) */
93#define SPPCR_SPLP 0x01 /* Loopback Mode (inverting) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090094
Geert Uytterhoevenfbe50722014-01-12 11:27:38 +010095#define SPPCR_IO3FV 0x04 /* Single-/Dual-SPI Mode IO3 Output Fixed Value */
96#define SPPCR_IO2FV 0x04 /* Single-/Dual-SPI Mode IO2 Output Fixed Value */
97
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010098/* SPSR - Status Register */
99#define SPSR_SPRF 0x80 /* Receive Buffer Full Flag */
100#define SPSR_TEND 0x40 /* Transmit End */
101#define SPSR_SPTEF 0x20 /* Transmit Buffer Empty Flag */
102#define SPSR_PERF 0x08 /* Parity Error Flag */
103#define SPSR_MODF 0x04 /* Mode Fault Error Flag */
104#define SPSR_IDLNF 0x02 /* RSPI Idle Flag */
105#define SPSR_OVRF 0x01 /* Overrun Error Flag */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900106
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100107/* SPSCR - Sequence Control Register */
108#define SPSCR_SPSLN_MASK 0x07 /* Sequence Length Specification */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900109
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100110/* SPSSR - Sequence Status Register */
111#define SPSSR_SPECM_MASK 0x70 /* Command Error Mask */
112#define SPSSR_SPCP_MASK 0x07 /* Command Pointer Mask */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900113
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100114/* SPDCR - Data Control Register */
115#define SPDCR_TXDMY 0x80 /* Dummy Data Transmission Enable */
116#define SPDCR_SPLW1 0x40 /* Access Width Specification (RZ) */
117#define SPDCR_SPLW0 0x20 /* Access Width Specification (RZ) */
118#define SPDCR_SPLLWORD (SPDCR_SPLW1 | SPDCR_SPLW0)
119#define SPDCR_SPLWORD SPDCR_SPLW1
120#define SPDCR_SPLBYTE SPDCR_SPLW0
121#define SPDCR_SPLW 0x20 /* Access Width Specification (SH) */
122#define SPDCR_SPRDTD 0x10 /* Receive Transmit Data Select */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900123#define SPDCR_SLSEL1 0x08
124#define SPDCR_SLSEL0 0x04
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100125#define SPDCR_SLSEL_MASK 0x0c /* SSL1 Output Select */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900126#define SPDCR_SPFC1 0x02
127#define SPDCR_SPFC0 0x01
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100128#define SPDCR_SPFC_MASK 0x03 /* Frame Count Setting (1-4) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900129
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100130/* SPCKD - Clock Delay Register */
131#define SPCKD_SCKDL_MASK 0x07 /* Clock Delay Setting (1-8) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900132
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100133/* SSLND - Slave Select Negation Delay Register */
134#define SSLND_SLNDL_MASK 0x07 /* SSL Negation Delay Setting (1-8) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900135
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100136/* SPND - Next-Access Delay Register */
137#define SPND_SPNDL_MASK 0x07 /* Next-Access Delay Setting (1-8) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900138
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100139/* SPCR2 - Control Register 2 */
140#define SPCR2_PTE 0x08 /* Parity Self-Test Enable */
141#define SPCR2_SPIE 0x04 /* Idle Interrupt Enable */
142#define SPCR2_SPOE 0x02 /* Odd Parity Enable (vs. Even) */
143#define SPCR2_SPPE 0x01 /* Parity Enable */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900144
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100145/* SPCMDn - Command Registers */
146#define SPCMD_SCKDEN 0x8000 /* Clock Delay Setting Enable */
147#define SPCMD_SLNDEN 0x4000 /* SSL Negation Delay Setting Enable */
148#define SPCMD_SPNDEN 0x2000 /* Next-Access Delay Enable */
149#define SPCMD_LSBF 0x1000 /* LSB First */
150#define SPCMD_SPB_MASK 0x0f00 /* Data Length Setting */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900151#define SPCMD_SPB_8_TO_16(bit) (((bit - 1) << 8) & SPCMD_SPB_MASK)
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900152#define SPCMD_SPB_8BIT 0x0000 /* qspi only */
153#define SPCMD_SPB_16BIT 0x0100
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900154#define SPCMD_SPB_20BIT 0x0000
155#define SPCMD_SPB_24BIT 0x0100
156#define SPCMD_SPB_32BIT 0x0200
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100157#define SPCMD_SSLKP 0x0080 /* SSL Signal Level Keeping */
Geert Uytterhoevenfbe50722014-01-12 11:27:38 +0100158#define SPCMD_SPIMOD_MASK 0x0060 /* SPI Operating Mode (QSPI only) */
159#define SPCMD_SPIMOD1 0x0040
160#define SPCMD_SPIMOD0 0x0020
161#define SPCMD_SPIMOD_SINGLE 0
162#define SPCMD_SPIMOD_DUAL SPCMD_SPIMOD0
163#define SPCMD_SPIMOD_QUAD SPCMD_SPIMOD1
164#define SPCMD_SPRW 0x0010 /* SPI Read/Write Access (Dual/Quad) */
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100165#define SPCMD_SSLA_MASK 0x0030 /* SSL Assert Signal Setting (RSPI) */
166#define SPCMD_BRDV_MASK 0x000c /* Bit Rate Division Setting */
167#define SPCMD_CPOL 0x0002 /* Clock Polarity Setting */
168#define SPCMD_CPHA 0x0001 /* Clock Phase Setting */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900169
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100170/* SPBFCR - Buffer Control Register */
171#define SPBFCR_TXRST 0x80 /* Transmit Buffer Data Reset (qspi only) */
172#define SPBFCR_RXRST 0x40 /* Receive Buffer Data Reset (qspi only) */
173#define SPBFCR_TXTRG_MASK 0x30 /* Transmit Buffer Data Triggering Number */
174#define SPBFCR_RXTRG_MASK 0x07 /* Receive Buffer Data Triggering Number */
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900175
Geert Uytterhoeven2aae80b2013-12-24 10:49:33 +0100176#define DUMMY_DATA 0x00
177
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900178struct rspi_data {
179 void __iomem *addr;
180 u32 max_speed_hz;
181 struct spi_master *master;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900182 wait_queue_head_t wait;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900183 struct clk *clk;
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100184 u8 spsr;
Geert Uytterhoeven348e5152014-01-12 11:27:43 +0100185 u16 spcmd;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900186 const struct spi_ops *ops;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900187
188 /* for dmaengine */
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900189 struct dma_chan *chan_tx;
190 struct dma_chan *chan_rx;
191 int irq;
192
193 unsigned dma_width_16bit:1;
194 unsigned dma_callbacked:1;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900195};
196
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100197static void rspi_write8(const struct rspi_data *rspi, u8 data, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900198{
199 iowrite8(data, rspi->addr + offset);
200}
201
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100202static void rspi_write16(const struct rspi_data *rspi, u16 data, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900203{
204 iowrite16(data, rspi->addr + offset);
205}
206
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100207static void rspi_write32(const struct rspi_data *rspi, u32 data, u16 offset)
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900208{
209 iowrite32(data, rspi->addr + offset);
210}
211
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100212static u8 rspi_read8(const struct rspi_data *rspi, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900213{
214 return ioread8(rspi->addr + offset);
215}
216
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100217static u16 rspi_read16(const struct rspi_data *rspi, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900218{
219 return ioread16(rspi->addr + offset);
220}
221
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900222/* optional functions */
223struct spi_ops {
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100224 int (*set_config_register)(const struct rspi_data *rspi,
225 int access_size);
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100226 int (*send_pio)(struct rspi_data *rspi, struct spi_transfer *t);
227 int (*receive_pio)(struct rspi_data *rspi, struct spi_transfer *t);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900228};
229
230/*
231 * functions for RSPI
232 */
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100233static int rspi_set_config_register(const struct rspi_data *rspi,
234 int access_size)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900235{
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900236 int spbr;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900237
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900238 /* Sets output mode(CMOS) and MOSI signal(from previous transfer) */
239 rspi_write8(rspi, 0x00, RSPI_SPPCR);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900240
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900241 /* Sets transfer bit rate */
242 spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz) - 1;
243 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
244
245 /* Sets number of frames to be used: 1 frame */
246 rspi_write8(rspi, 0x00, RSPI_SPDCR);
247
248 /* Sets RSPCK, SSL, next-access delay value */
249 rspi_write8(rspi, 0x00, RSPI_SPCKD);
250 rspi_write8(rspi, 0x00, RSPI_SSLND);
251 rspi_write8(rspi, 0x00, RSPI_SPND);
252
253 /* Sets parity, interrupt mask */
254 rspi_write8(rspi, 0x00, RSPI_SPCR2);
255
256 /* Sets SPCMD */
Geert Uytterhoeven348e5152014-01-12 11:27:43 +0100257 rspi_write16(rspi, SPCMD_SPB_8_TO_16(access_size) | rspi->spcmd,
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900258 RSPI_SPCMD0);
259
260 /* Sets RSPI mode */
261 rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
262
263 return 0;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900264}
265
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900266/*
267 * functions for QSPI
268 */
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100269static int qspi_set_config_register(const struct rspi_data *rspi,
270 int access_size)
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900271{
272 u16 spcmd;
273 int spbr;
274
275 /* Sets output mode(CMOS) and MOSI signal(from previous transfer) */
276 rspi_write8(rspi, 0x00, RSPI_SPPCR);
277
278 /* Sets transfer bit rate */
279 spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz);
280 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
281
282 /* Sets number of frames to be used: 1 frame */
283 rspi_write8(rspi, 0x00, RSPI_SPDCR);
284
285 /* Sets RSPCK, SSL, next-access delay value */
286 rspi_write8(rspi, 0x00, RSPI_SPCKD);
287 rspi_write8(rspi, 0x00, RSPI_SSLND);
288 rspi_write8(rspi, 0x00, RSPI_SPND);
289
290 /* Data Length Setting */
291 if (access_size == 8)
292 spcmd = SPCMD_SPB_8BIT;
293 else if (access_size == 16)
294 spcmd = SPCMD_SPB_16BIT;
Laurent Pinchart8e1c8092013-11-27 01:41:44 +0100295 else
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900296 spcmd = SPCMD_SPB_32BIT;
297
Geert Uytterhoeven348e5152014-01-12 11:27:43 +0100298 spcmd |= SPCMD_SCKDEN | SPCMD_SLNDEN | rspi->spcmd | SPCMD_SPNDEN;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900299
300 /* Resets transfer data length */
301 rspi_write32(rspi, 0, QSPI_SPBMUL0);
302
303 /* Resets transmit and receive buffer */
304 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
305 /* Sets buffer to allow normal operation */
306 rspi_write8(rspi, 0x00, QSPI_SPBFCR);
307
308 /* Sets SPCMD */
309 rspi_write16(rspi, spcmd, RSPI_SPCMD0);
310
311 /* Enables SPI function in a master mode */
312 rspi_write8(rspi, SPCR_SPE | SPCR_MSTR, RSPI_SPCR);
313
314 return 0;
315}
316
317#define set_config_register(spi, n) spi->ops->set_config_register(spi, n)
318
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100319static void rspi_enable_irq(const struct rspi_data *rspi, u8 enable)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900320{
321 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | enable, RSPI_SPCR);
322}
323
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100324static void rspi_disable_irq(const struct rspi_data *rspi, u8 disable)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900325{
326 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~disable, RSPI_SPCR);
327}
328
329static int rspi_wait_for_interrupt(struct rspi_data *rspi, u8 wait_mask,
330 u8 enable_bit)
331{
332 int ret;
333
334 rspi->spsr = rspi_read8(rspi, RSPI_SPSR);
335 rspi_enable_irq(rspi, enable_bit);
336 ret = wait_event_timeout(rspi->wait, rspi->spsr & wait_mask, HZ);
337 if (ret == 0 && !(rspi->spsr & wait_mask))
338 return -ETIMEDOUT;
339
340 return 0;
341}
342
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100343static int rspi_send_pio(struct rspi_data *rspi, struct spi_transfer *t)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900344{
345 int remain = t->len;
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100346 const u8 *data = t->tx_buf;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900347 while (remain > 0) {
348 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_TXMD,
349 RSPI_SPCR);
350
351 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
352 dev_err(&rspi->master->dev,
353 "%s: tx empty timeout\n", __func__);
354 return -ETIMEDOUT;
355 }
356
357 rspi_write16(rspi, *data, RSPI_SPDR);
358 data++;
359 remain--;
360 }
361
Geert Uytterhoevenb7ed6b82014-01-14 10:20:32 +0100362 /* Waiting for the last transmission */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900363 rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
364
365 return 0;
366}
367
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100368static int qspi_send_pio(struct rspi_data *rspi, struct spi_transfer *t)
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900369{
370 int remain = t->len;
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100371 const u8 *data = t->tx_buf;
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900372
373 rspi_write8(rspi, SPBFCR_TXRST, QSPI_SPBFCR);
374 rspi_write8(rspi, 0x00, QSPI_SPBFCR);
375
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900376 while (remain > 0) {
377
378 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
379 dev_err(&rspi->master->dev,
380 "%s: tx empty timeout\n", __func__);
381 return -ETIMEDOUT;
382 }
383 rspi_write8(rspi, *data++, RSPI_SPDR);
384
385 if (rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE) < 0) {
386 dev_err(&rspi->master->dev,
387 "%s: receive timeout\n", __func__);
388 return -ETIMEDOUT;
389 }
390 rspi_read8(rspi, RSPI_SPDR);
391
392 remain--;
393 }
394
Geert Uytterhoevenb7ed6b82014-01-14 10:20:32 +0100395 /* Waiting for the last transmission */
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900396 rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
397
398 return 0;
399}
400
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100401#define send_pio(spi, t) spi->ops->send_pio(spi, t)
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900402
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900403static void rspi_dma_complete(void *arg)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900404{
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900405 struct rspi_data *rspi = arg;
406
407 rspi->dma_callbacked = 1;
408 wake_up_interruptible(&rspi->wait);
409}
410
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100411static int rspi_dma_map_sg(struct scatterlist *sg, const void *buf,
412 unsigned len, struct dma_chan *chan,
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900413 enum dma_transfer_direction dir)
414{
415 sg_init_table(sg, 1);
416 sg_set_buf(sg, buf, len);
417 sg_dma_len(sg) = len;
418 return dma_map_sg(chan->device->dev, sg, 1, dir);
419}
420
421static void rspi_dma_unmap_sg(struct scatterlist *sg, struct dma_chan *chan,
422 enum dma_transfer_direction dir)
423{
424 dma_unmap_sg(chan->device->dev, sg, 1, dir);
425}
426
427static void rspi_memory_to_8bit(void *buf, const void *data, unsigned len)
428{
429 u16 *dst = buf;
430 const u8 *src = data;
431
432 while (len) {
433 *dst++ = (u16)(*src++);
434 len--;
435 }
436}
437
438static void rspi_memory_from_8bit(void *buf, const void *data, unsigned len)
439{
440 u8 *dst = buf;
441 const u16 *src = data;
442
443 while (len) {
444 *dst++ = (u8)*src++;
445 len--;
446 }
447}
448
449static int rspi_send_dma(struct rspi_data *rspi, struct spi_transfer *t)
450{
451 struct scatterlist sg;
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100452 const void *buf = NULL;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900453 struct dma_async_tx_descriptor *desc;
454 unsigned len;
455 int ret = 0;
456
457 if (rspi->dma_width_16bit) {
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100458 void *tmp;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900459 /*
460 * If DMAC bus width is 16-bit, the driver allocates a dummy
461 * buffer. And, the driver converts original data into the
462 * DMAC data as the following format:
463 * original data: 1st byte, 2nd byte ...
464 * DMAC data: 1st byte, dummy, 2nd byte, dummy ...
465 */
466 len = t->len * 2;
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100467 tmp = kmalloc(len, GFP_KERNEL);
468 if (!tmp)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900469 return -ENOMEM;
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100470 rspi_memory_to_8bit(tmp, t->tx_buf, t->len);
471 buf = tmp;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900472 } else {
473 len = t->len;
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100474 buf = t->tx_buf;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900475 }
476
477 if (!rspi_dma_map_sg(&sg, buf, len, rspi->chan_tx, DMA_TO_DEVICE)) {
478 ret = -EFAULT;
479 goto end_nomap;
480 }
481 desc = dmaengine_prep_slave_sg(rspi->chan_tx, &sg, 1, DMA_TO_DEVICE,
482 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
483 if (!desc) {
484 ret = -EIO;
485 goto end;
486 }
487
488 /*
489 * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be
490 * called. So, this driver disables the IRQ while DMA transfer.
491 */
492 disable_irq(rspi->irq);
493
494 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_TXMD, RSPI_SPCR);
495 rspi_enable_irq(rspi, SPCR_SPTIE);
496 rspi->dma_callbacked = 0;
497
498 desc->callback = rspi_dma_complete;
499 desc->callback_param = rspi;
500 dmaengine_submit(desc);
501 dma_async_issue_pending(rspi->chan_tx);
502
503 ret = wait_event_interruptible_timeout(rspi->wait,
504 rspi->dma_callbacked, HZ);
505 if (ret > 0 && rspi->dma_callbacked)
506 ret = 0;
507 else if (!ret)
508 ret = -ETIMEDOUT;
509 rspi_disable_irq(rspi, SPCR_SPTIE);
510
511 enable_irq(rspi->irq);
512
513end:
514 rspi_dma_unmap_sg(&sg, rspi->chan_tx, DMA_TO_DEVICE);
515end_nomap:
516 if (rspi->dma_width_16bit)
517 kfree(buf);
518
519 return ret;
520}
521
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100522static void rspi_receive_init(const struct rspi_data *rspi)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900523{
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100524 u8 spsr;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900525
526 spsr = rspi_read8(rspi, RSPI_SPSR);
527 if (spsr & SPSR_SPRF)
528 rspi_read16(rspi, RSPI_SPDR); /* dummy read */
529 if (spsr & SPSR_OVRF)
530 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPSR) & ~SPSR_OVRF,
Geert Uytterhoevendf900e62013-12-23 19:34:24 +0100531 RSPI_SPSR);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900532}
533
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100534static int rspi_receive_pio(struct rspi_data *rspi, struct spi_transfer *t)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900535{
536 int remain = t->len;
537 u8 *data;
538
539 rspi_receive_init(rspi);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900540
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100541 data = t->rx_buf;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900542 while (remain > 0) {
543 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_TXMD,
544 RSPI_SPCR);
545
546 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
547 dev_err(&rspi->master->dev,
548 "%s: tx empty timeout\n", __func__);
549 return -ETIMEDOUT;
550 }
551 /* dummy write for generate clock */
Geert Uytterhoeven2aae80b2013-12-24 10:49:33 +0100552 rspi_write16(rspi, DUMMY_DATA, RSPI_SPDR);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900553
554 if (rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE) < 0) {
555 dev_err(&rspi->master->dev,
556 "%s: receive timeout\n", __func__);
557 return -ETIMEDOUT;
558 }
559 /* SPDR allows 16 or 32-bit access only */
560 *data = (u8)rspi_read16(rspi, RSPI_SPDR);
561
562 data++;
563 remain--;
564 }
565
566 return 0;
567}
568
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100569static void qspi_receive_init(const struct rspi_data *rspi)
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900570{
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100571 u8 spsr;
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900572
573 spsr = rspi_read8(rspi, RSPI_SPSR);
574 if (spsr & SPSR_SPRF)
575 rspi_read8(rspi, RSPI_SPDR); /* dummy read */
576 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
577 rspi_write8(rspi, 0x00, QSPI_SPBFCR);
578}
579
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100580static int qspi_receive_pio(struct rspi_data *rspi, struct spi_transfer *t)
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900581{
582 int remain = t->len;
583 u8 *data;
584
585 qspi_receive_init(rspi);
586
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100587 data = t->rx_buf;
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900588 while (remain > 0) {
589
590 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
591 dev_err(&rspi->master->dev,
592 "%s: tx empty timeout\n", __func__);
593 return -ETIMEDOUT;
594 }
595 /* dummy write for generate clock */
Geert Uytterhoeven2aae80b2013-12-24 10:49:33 +0100596 rspi_write8(rspi, DUMMY_DATA, RSPI_SPDR);
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900597
598 if (rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE) < 0) {
599 dev_err(&rspi->master->dev,
600 "%s: receive timeout\n", __func__);
601 return -ETIMEDOUT;
602 }
603 /* SPDR allows 8, 16 or 32-bit access */
604 *data++ = rspi_read8(rspi, RSPI_SPDR);
605 remain--;
606 }
607
608 return 0;
609}
610
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100611#define receive_pio(spi, t) spi->ops->receive_pio(spi, t)
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900612
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900613static int rspi_receive_dma(struct rspi_data *rspi, struct spi_transfer *t)
614{
615 struct scatterlist sg, sg_dummy;
616 void *dummy = NULL, *rx_buf = NULL;
617 struct dma_async_tx_descriptor *desc, *desc_dummy;
618 unsigned len;
619 int ret = 0;
620
621 if (rspi->dma_width_16bit) {
622 /*
623 * If DMAC bus width is 16-bit, the driver allocates a dummy
624 * buffer. And, finally the driver converts the DMAC data into
625 * actual data as the following format:
626 * DMAC data: 1st byte, dummy, 2nd byte, dummy ...
627 * actual data: 1st byte, 2nd byte ...
628 */
629 len = t->len * 2;
630 rx_buf = kmalloc(len, GFP_KERNEL);
631 if (!rx_buf)
632 return -ENOMEM;
633 } else {
634 len = t->len;
635 rx_buf = t->rx_buf;
636 }
637
638 /* prepare dummy transfer to generate SPI clocks */
639 dummy = kzalloc(len, GFP_KERNEL);
640 if (!dummy) {
641 ret = -ENOMEM;
642 goto end_nomap;
643 }
644 if (!rspi_dma_map_sg(&sg_dummy, dummy, len, rspi->chan_tx,
645 DMA_TO_DEVICE)) {
646 ret = -EFAULT;
647 goto end_nomap;
648 }
649 desc_dummy = dmaengine_prep_slave_sg(rspi->chan_tx, &sg_dummy, 1,
650 DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
651 if (!desc_dummy) {
652 ret = -EIO;
653 goto end_dummy_mapped;
654 }
655
656 /* prepare receive transfer */
657 if (!rspi_dma_map_sg(&sg, rx_buf, len, rspi->chan_rx,
658 DMA_FROM_DEVICE)) {
659 ret = -EFAULT;
660 goto end_dummy_mapped;
661
662 }
663 desc = dmaengine_prep_slave_sg(rspi->chan_rx, &sg, 1, DMA_FROM_DEVICE,
664 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
665 if (!desc) {
666 ret = -EIO;
667 goto end;
668 }
669
670 rspi_receive_init(rspi);
671
672 /*
673 * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be
674 * called. So, this driver disables the IRQ while DMA transfer.
675 */
676 disable_irq(rspi->irq);
677
678 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_TXMD, RSPI_SPCR);
679 rspi_enable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE);
680 rspi->dma_callbacked = 0;
681
682 desc->callback = rspi_dma_complete;
683 desc->callback_param = rspi;
684 dmaengine_submit(desc);
685 dma_async_issue_pending(rspi->chan_rx);
686
687 desc_dummy->callback = NULL; /* No callback */
688 dmaengine_submit(desc_dummy);
689 dma_async_issue_pending(rspi->chan_tx);
690
691 ret = wait_event_interruptible_timeout(rspi->wait,
692 rspi->dma_callbacked, HZ);
693 if (ret > 0 && rspi->dma_callbacked)
694 ret = 0;
695 else if (!ret)
696 ret = -ETIMEDOUT;
697 rspi_disable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE);
698
699 enable_irq(rspi->irq);
700
701end:
702 rspi_dma_unmap_sg(&sg, rspi->chan_rx, DMA_FROM_DEVICE);
703end_dummy_mapped:
704 rspi_dma_unmap_sg(&sg_dummy, rspi->chan_tx, DMA_TO_DEVICE);
705end_nomap:
706 if (rspi->dma_width_16bit) {
707 if (!ret)
708 rspi_memory_from_8bit(t->rx_buf, rx_buf, t->len);
709 kfree(rx_buf);
710 }
711 kfree(dummy);
712
713 return ret;
714}
715
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100716static int rspi_is_dma(const struct rspi_data *rspi, struct spi_transfer *t)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900717{
718 if (t->tx_buf && rspi->chan_tx)
719 return 1;
720 /* If the module receives data by DMAC, it also needs TX DMAC */
721 if (t->rx_buf && rspi->chan_tx && rspi->chan_rx)
722 return 1;
723
724 return 0;
725}
726
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100727static int rspi_transfer_one(struct spi_master *master, struct spi_device *spi,
728 struct spi_transfer *xfer)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900729{
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100730 struct rspi_data *rspi = spi_master_get_devdata(master);
731 int ret = 0;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900732
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100733 if (xfer->tx_buf) {
734 if (rspi_is_dma(rspi, xfer))
735 ret = rspi_send_dma(rspi, xfer);
736 else
737 ret = send_pio(rspi, xfer);
738 if (ret < 0)
739 return ret;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900740 }
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100741 if (xfer->rx_buf) {
742 if (rspi_is_dma(rspi, xfer))
743 ret = rspi_receive_dma(rspi, xfer);
744 else
745 ret = receive_pio(rspi, xfer);
746 }
747 return ret;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900748}
749
750static int rspi_setup(struct spi_device *spi)
751{
752 struct rspi_data *rspi = spi_master_get_devdata(spi->master);
753
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900754 rspi->max_speed_hz = spi->max_speed_hz;
755
Geert Uytterhoeven348e5152014-01-12 11:27:43 +0100756 rspi->spcmd = SPCMD_SSLKP;
757 if (spi->mode & SPI_CPOL)
758 rspi->spcmd |= SPCMD_CPOL;
759 if (spi->mode & SPI_CPHA)
760 rspi->spcmd |= SPCMD_CPHA;
761
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900762 set_config_register(rspi, 8);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900763
764 return 0;
765}
766
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100767static void rspi_cleanup(struct spi_device *spi)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900768{
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100769}
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900770
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100771static int rspi_prepare_message(struct spi_master *master,
772 struct spi_message *message)
773{
774 struct rspi_data *rspi = spi_master_get_devdata(master);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900775
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100776 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_SPE, RSPI_SPCR);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900777 return 0;
778}
779
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100780static int rspi_unprepare_message(struct spi_master *master,
781 struct spi_message *message)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900782{
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100783 struct rspi_data *rspi = spi_master_get_devdata(master);
784
785 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_SPE, RSPI_SPCR);
786 return 0;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900787}
788
789static irqreturn_t rspi_irq(int irq, void *_sr)
790{
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100791 struct rspi_data *rspi = _sr;
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100792 u8 spsr;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900793 irqreturn_t ret = IRQ_NONE;
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100794 u8 disable_irq = 0;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900795
796 rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
797 if (spsr & SPSR_SPRF)
798 disable_irq |= SPCR_SPRIE;
799 if (spsr & SPSR_SPTEF)
800 disable_irq |= SPCR_SPTIE;
801
802 if (disable_irq) {
803 ret = IRQ_HANDLED;
804 rspi_disable_irq(rspi, disable_irq);
805 wake_up(&rspi->wait);
806 }
807
808 return ret;
809}
810
Grant Likelyfd4a3192012-12-07 16:57:14 +0000811static int rspi_request_dma(struct rspi_data *rspi,
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900812 struct platform_device *pdev)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900813{
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100814 const struct rspi_plat_data *rspi_pd = dev_get_platdata(&pdev->dev);
Guennadi Liakhovetskie2b05092013-08-02 15:03:42 +0200815 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900816 dma_cap_mask_t mask;
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900817 struct dma_slave_config cfg;
818 int ret;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900819
Guennadi Liakhovetskie2b05092013-08-02 15:03:42 +0200820 if (!res || !rspi_pd)
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900821 return 0; /* The driver assumes no error. */
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900822
823 rspi->dma_width_16bit = rspi_pd->dma_width_16bit;
824
825 /* If the module receives data by DMAC, it also needs TX DMAC */
826 if (rspi_pd->dma_rx_id && rspi_pd->dma_tx_id) {
827 dma_cap_zero(mask);
828 dma_cap_set(DMA_SLAVE, mask);
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900829 rspi->chan_rx = dma_request_channel(mask, shdma_chan_filter,
830 (void *)rspi_pd->dma_rx_id);
831 if (rspi->chan_rx) {
832 cfg.slave_id = rspi_pd->dma_rx_id;
833 cfg.direction = DMA_DEV_TO_MEM;
Guennadi Liakhovetskie2b05092013-08-02 15:03:42 +0200834 cfg.dst_addr = 0;
835 cfg.src_addr = res->start + RSPI_SPDR;
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900836 ret = dmaengine_slave_config(rspi->chan_rx, &cfg);
837 if (!ret)
838 dev_info(&pdev->dev, "Use DMA when rx.\n");
839 else
840 return ret;
841 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900842 }
843 if (rspi_pd->dma_tx_id) {
844 dma_cap_zero(mask);
845 dma_cap_set(DMA_SLAVE, mask);
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900846 rspi->chan_tx = dma_request_channel(mask, shdma_chan_filter,
847 (void *)rspi_pd->dma_tx_id);
848 if (rspi->chan_tx) {
849 cfg.slave_id = rspi_pd->dma_tx_id;
850 cfg.direction = DMA_MEM_TO_DEV;
Guennadi Liakhovetskie2b05092013-08-02 15:03:42 +0200851 cfg.dst_addr = res->start + RSPI_SPDR;
852 cfg.src_addr = 0;
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900853 ret = dmaengine_slave_config(rspi->chan_tx, &cfg);
854 if (!ret)
855 dev_info(&pdev->dev, "Use DMA when tx\n");
856 else
857 return ret;
858 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900859 }
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900860
861 return 0;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900862}
863
Grant Likelyfd4a3192012-12-07 16:57:14 +0000864static void rspi_release_dma(struct rspi_data *rspi)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900865{
866 if (rspi->chan_tx)
867 dma_release_channel(rspi->chan_tx);
868 if (rspi->chan_rx)
869 dma_release_channel(rspi->chan_rx);
870}
871
Grant Likelyfd4a3192012-12-07 16:57:14 +0000872static int rspi_remove(struct platform_device *pdev)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900873{
Laurent Pinchart5ffbe2d2013-11-27 01:41:45 +0100874 struct rspi_data *rspi = platform_get_drvdata(pdev);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900875
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900876 rspi_release_dma(rspi);
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +0100877 clk_disable(rspi->clk);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900878
879 return 0;
880}
881
Grant Likelyfd4a3192012-12-07 16:57:14 +0000882static int rspi_probe(struct platform_device *pdev)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900883{
884 struct resource *res;
885 struct spi_master *master;
886 struct rspi_data *rspi;
887 int ret, irq;
888 char clk_name[16];
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100889 const struct rspi_plat_data *rspi_pd = dev_get_platdata(&pdev->dev);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900890 const struct spi_ops *ops;
891 const struct platform_device_id *id_entry = pdev->id_entry;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900892
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900893 ops = (struct spi_ops *)id_entry->driver_data;
894 /* ops parameter check */
895 if (!ops->set_config_register) {
896 dev_err(&pdev->dev, "there is no set_config_register\n");
897 return -ENODEV;
898 }
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900899
900 irq = platform_get_irq(pdev, 0);
901 if (irq < 0) {
902 dev_err(&pdev->dev, "platform_get_irq error\n");
903 return -ENODEV;
904 }
905
906 master = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data));
907 if (master == NULL) {
908 dev_err(&pdev->dev, "spi_alloc_master error.\n");
909 return -ENOMEM;
910 }
911
912 rspi = spi_master_get_devdata(master);
Jingoo Han24b5a822013-05-23 19:20:40 +0900913 platform_set_drvdata(pdev, rspi);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900914 rspi->ops = ops;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900915 rspi->master = master;
Laurent Pinchart5d79e9a2013-11-27 01:41:46 +0100916
917 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
918 rspi->addr = devm_ioremap_resource(&pdev->dev, res);
919 if (IS_ERR(rspi->addr)) {
920 ret = PTR_ERR(rspi->addr);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900921 goto error1;
922 }
923
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900924 snprintf(clk_name, sizeof(clk_name), "%s%d", id_entry->name, pdev->id);
Laurent Pinchart5d79e9a2013-11-27 01:41:46 +0100925 rspi->clk = devm_clk_get(&pdev->dev, clk_name);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900926 if (IS_ERR(rspi->clk)) {
927 dev_err(&pdev->dev, "cannot get clock\n");
928 ret = PTR_ERR(rspi->clk);
Laurent Pinchart5d79e9a2013-11-27 01:41:46 +0100929 goto error1;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900930 }
931 clk_enable(rspi->clk);
932
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900933 init_waitqueue_head(&rspi->wait);
934
Geert Uytterhoevenefd85ac2013-12-23 19:34:23 +0100935 if (rspi_pd && rspi_pd->num_chipselect)
936 master->num_chipselect = rspi_pd->num_chipselect;
937 else
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900938 master->num_chipselect = 2; /* default */
939
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900940 master->bus_num = pdev->id;
941 master->setup = rspi_setup;
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100942 master->transfer_one = rspi_transfer_one;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900943 master->cleanup = rspi_cleanup;
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100944 master->prepare_message = rspi_prepare_message;
945 master->unprepare_message = rspi_unprepare_message;
Geert Uytterhoeven348e5152014-01-12 11:27:43 +0100946 master->mode_bits = SPI_CPHA | SPI_CPOL;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900947
Laurent Pinchart5d79e9a2013-11-27 01:41:46 +0100948 ret = devm_request_irq(&pdev->dev, irq, rspi_irq, 0,
949 dev_name(&pdev->dev), rspi);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900950 if (ret < 0) {
951 dev_err(&pdev->dev, "request_irq error\n");
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +0100952 goto error2;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900953 }
954
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900955 rspi->irq = irq;
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900956 ret = rspi_request_dma(rspi, pdev);
957 if (ret < 0) {
958 dev_err(&pdev->dev, "rspi_request_dma failed.\n");
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +0100959 goto error3;
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900960 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900961
Jingoo Han9e03d052013-12-04 14:13:50 +0900962 ret = devm_spi_register_master(&pdev->dev, master);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900963 if (ret < 0) {
964 dev_err(&pdev->dev, "spi_register_master error.\n");
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +0100965 goto error3;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900966 }
967
968 dev_info(&pdev->dev, "probed\n");
969
970 return 0;
971
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +0100972error3:
Laurent Pinchart5d79e9a2013-11-27 01:41:46 +0100973 rspi_release_dma(rspi);
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +0100974error2:
975 clk_disable(rspi->clk);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900976error1:
977 spi_master_put(master);
978
979 return ret;
980}
981
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900982static struct spi_ops rspi_ops = {
983 .set_config_register = rspi_set_config_register,
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900984 .send_pio = rspi_send_pio,
985 .receive_pio = rspi_receive_pio,
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900986};
987
988static struct spi_ops qspi_ops = {
989 .set_config_register = qspi_set_config_register,
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900990 .send_pio = qspi_send_pio,
991 .receive_pio = qspi_receive_pio,
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900992};
993
994static struct platform_device_id spi_driver_ids[] = {
995 { "rspi", (kernel_ulong_t)&rspi_ops },
996 { "qspi", (kernel_ulong_t)&qspi_ops },
997 {},
998};
999
1000MODULE_DEVICE_TABLE(platform, spi_driver_ids);
1001
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001002static struct platform_driver rspi_driver = {
1003 .probe = rspi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001004 .remove = rspi_remove,
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001005 .id_table = spi_driver_ids,
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001006 .driver = {
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001007 .name = "renesas_spi",
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001008 .owner = THIS_MODULE,
1009 },
1010};
1011module_platform_driver(rspi_driver);
1012
1013MODULE_DESCRIPTION("Renesas RSPI bus driver");
1014MODULE_LICENSE("GPL v2");
1015MODULE_AUTHOR("Yoshihiro Shimoda");
1016MODULE_ALIAS("platform:rspi");