blob: ccd5cf201d04b173252f2456661f982abd5af95f [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>
28#include <linux/list.h>
29#include <linux/workqueue.h>
30#include <linux/interrupt.h>
31#include <linux/platform_device.h>
32#include <linux/io.h>
33#include <linux/clk.h>
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +090034#include <linux/dmaengine.h>
35#include <linux/dma-mapping.h>
36#include <linux/sh_dma.h>
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090037#include <linux/spi/spi.h>
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +090038#include <linux/spi/rspi.h>
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090039
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010040#define RSPI_SPCR 0x00 /* Control Register */
41#define RSPI_SSLP 0x01 /* Slave Select Polarity Register */
42#define RSPI_SPPCR 0x02 /* Pin Control Register */
43#define RSPI_SPSR 0x03 /* Status Register */
44#define RSPI_SPDR 0x04 /* Data Register */
45#define RSPI_SPSCR 0x08 /* Sequence Control Register */
46#define RSPI_SPSSR 0x09 /* Sequence Status Register */
47#define RSPI_SPBR 0x0a /* Bit Rate Register */
48#define RSPI_SPDCR 0x0b /* Data Control Register */
49#define RSPI_SPCKD 0x0c /* Clock Delay Register */
50#define RSPI_SSLND 0x0d /* Slave Select Negation Delay Register */
51#define RSPI_SPND 0x0e /* Next-Access Delay Register */
52#define RSPI_SPCR2 0x0f /* Control Register 2 */
53#define RSPI_SPCMD0 0x10 /* Command Register 0 */
54#define RSPI_SPCMD1 0x12 /* Command Register 1 */
55#define RSPI_SPCMD2 0x14 /* Command Register 2 */
56#define RSPI_SPCMD3 0x16 /* Command Register 3 */
57#define RSPI_SPCMD4 0x18 /* Command Register 4 */
58#define RSPI_SPCMD5 0x1a /* Command Register 5 */
59#define RSPI_SPCMD6 0x1c /* Command Register 6 */
60#define RSPI_SPCMD7 0x1e /* Command Register 7 */
61#define RSPI_SPBFCR 0x20 /* Buffer Control Register */
62#define RSPI_SPBFDR 0x22 /* Buffer Data Count Setting Register */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090063
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +090064/*qspi only */
Geert Uytterhoevenfbe50722014-01-12 11:27:38 +010065#define QSPI_SPBFCR 0x18 /* Buffer Control Register */
66#define QSPI_SPBDCR 0x1a /* Buffer Data Count Register */
67#define QSPI_SPBMUL0 0x1c /* Transfer Data Length Multiplier Setting Register 0 */
68#define QSPI_SPBMUL1 0x20 /* Transfer Data Length Multiplier Setting Register 1 */
69#define QSPI_SPBMUL2 0x24 /* Transfer Data Length Multiplier Setting Register 2 */
70#define QSPI_SPBMUL3 0x28 /* Transfer Data Length Multiplier Setting Register 3 */
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +090071
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010072/* SPCR - Control Register */
73#define SPCR_SPRIE 0x80 /* Receive Interrupt Enable */
74#define SPCR_SPE 0x40 /* Function Enable */
75#define SPCR_SPTIE 0x20 /* Transmit Interrupt Enable */
76#define SPCR_SPEIE 0x10 /* Error Interrupt Enable */
77#define SPCR_MSTR 0x08 /* Master/Slave Mode Select */
78#define SPCR_MODFEN 0x04 /* Mode Fault Error Detection Enable */
79/* RSPI on SH only */
80#define SPCR_TXMD 0x02 /* TX Only Mode (vs. Full Duplex) */
81#define SPCR_SPMS 0x01 /* 3-wire Mode (vs. 4-wire) */
Geert Uytterhoevenfbe50722014-01-12 11:27:38 +010082/* QSPI on R-Car M2 only */
83#define SPCR_WSWAP 0x02 /* Word Swap of read-data for DMAC */
84#define SPCR_BSWAP 0x01 /* Byte Swap of read-data for DMAC */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090085
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010086/* SSLP - Slave Select Polarity Register */
87#define SSLP_SSL1P 0x02 /* SSL1 Signal Polarity Setting */
88#define SSLP_SSL0P 0x01 /* SSL0 Signal Polarity Setting */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090089
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010090/* SPPCR - Pin Control Register */
91#define SPPCR_MOIFE 0x20 /* MOSI Idle Value Fixing Enable */
92#define SPPCR_MOIFV 0x10 /* MOSI Idle Fixed Value */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090093#define SPPCR_SPOM 0x04
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010094#define SPPCR_SPLP2 0x02 /* Loopback Mode 2 (non-inverting) */
95#define SPPCR_SPLP 0x01 /* Loopback Mode (inverting) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090096
Geert Uytterhoevenfbe50722014-01-12 11:27:38 +010097#define SPPCR_IO3FV 0x04 /* Single-/Dual-SPI Mode IO3 Output Fixed Value */
98#define SPPCR_IO2FV 0x04 /* Single-/Dual-SPI Mode IO2 Output Fixed Value */
99
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100100/* SPSR - Status Register */
101#define SPSR_SPRF 0x80 /* Receive Buffer Full Flag */
102#define SPSR_TEND 0x40 /* Transmit End */
103#define SPSR_SPTEF 0x20 /* Transmit Buffer Empty Flag */
104#define SPSR_PERF 0x08 /* Parity Error Flag */
105#define SPSR_MODF 0x04 /* Mode Fault Error Flag */
106#define SPSR_IDLNF 0x02 /* RSPI Idle Flag */
107#define SPSR_OVRF 0x01 /* Overrun Error Flag */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900108
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100109/* SPSCR - Sequence Control Register */
110#define SPSCR_SPSLN_MASK 0x07 /* Sequence Length Specification */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900111
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100112/* SPSSR - Sequence Status Register */
113#define SPSSR_SPECM_MASK 0x70 /* Command Error Mask */
114#define SPSSR_SPCP_MASK 0x07 /* Command Pointer Mask */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900115
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100116/* SPDCR - Data Control Register */
117#define SPDCR_TXDMY 0x80 /* Dummy Data Transmission Enable */
118#define SPDCR_SPLW1 0x40 /* Access Width Specification (RZ) */
119#define SPDCR_SPLW0 0x20 /* Access Width Specification (RZ) */
120#define SPDCR_SPLLWORD (SPDCR_SPLW1 | SPDCR_SPLW0)
121#define SPDCR_SPLWORD SPDCR_SPLW1
122#define SPDCR_SPLBYTE SPDCR_SPLW0
123#define SPDCR_SPLW 0x20 /* Access Width Specification (SH) */
124#define SPDCR_SPRDTD 0x10 /* Receive Transmit Data Select */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900125#define SPDCR_SLSEL1 0x08
126#define SPDCR_SLSEL0 0x04
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100127#define SPDCR_SLSEL_MASK 0x0c /* SSL1 Output Select */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900128#define SPDCR_SPFC1 0x02
129#define SPDCR_SPFC0 0x01
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100130#define SPDCR_SPFC_MASK 0x03 /* Frame Count Setting (1-4) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900131
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100132/* SPCKD - Clock Delay Register */
133#define SPCKD_SCKDL_MASK 0x07 /* Clock Delay Setting (1-8) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900134
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100135/* SSLND - Slave Select Negation Delay Register */
136#define SSLND_SLNDL_MASK 0x07 /* SSL Negation Delay Setting (1-8) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900137
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100138/* SPND - Next-Access Delay Register */
139#define SPND_SPNDL_MASK 0x07 /* Next-Access Delay Setting (1-8) */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900140
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100141/* SPCR2 - Control Register 2 */
142#define SPCR2_PTE 0x08 /* Parity Self-Test Enable */
143#define SPCR2_SPIE 0x04 /* Idle Interrupt Enable */
144#define SPCR2_SPOE 0x02 /* Odd Parity Enable (vs. Even) */
145#define SPCR2_SPPE 0x01 /* Parity Enable */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900146
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100147/* SPCMDn - Command Registers */
148#define SPCMD_SCKDEN 0x8000 /* Clock Delay Setting Enable */
149#define SPCMD_SLNDEN 0x4000 /* SSL Negation Delay Setting Enable */
150#define SPCMD_SPNDEN 0x2000 /* Next-Access Delay Enable */
151#define SPCMD_LSBF 0x1000 /* LSB First */
152#define SPCMD_SPB_MASK 0x0f00 /* Data Length Setting */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900153#define SPCMD_SPB_8_TO_16(bit) (((bit - 1) << 8) & SPCMD_SPB_MASK)
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900154#define SPCMD_SPB_8BIT 0x0000 /* qspi only */
155#define SPCMD_SPB_16BIT 0x0100
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900156#define SPCMD_SPB_20BIT 0x0000
157#define SPCMD_SPB_24BIT 0x0100
158#define SPCMD_SPB_32BIT 0x0200
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100159#define SPCMD_SSLKP 0x0080 /* SSL Signal Level Keeping */
Geert Uytterhoevenfbe50722014-01-12 11:27:38 +0100160#define SPCMD_SPIMOD_MASK 0x0060 /* SPI Operating Mode (QSPI only) */
161#define SPCMD_SPIMOD1 0x0040
162#define SPCMD_SPIMOD0 0x0020
163#define SPCMD_SPIMOD_SINGLE 0
164#define SPCMD_SPIMOD_DUAL SPCMD_SPIMOD0
165#define SPCMD_SPIMOD_QUAD SPCMD_SPIMOD1
166#define SPCMD_SPRW 0x0010 /* SPI Read/Write Access (Dual/Quad) */
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100167#define SPCMD_SSLA_MASK 0x0030 /* SSL Assert Signal Setting (RSPI) */
168#define SPCMD_BRDV_MASK 0x000c /* Bit Rate Division Setting */
169#define SPCMD_CPOL 0x0002 /* Clock Polarity Setting */
170#define SPCMD_CPHA 0x0001 /* Clock Phase Setting */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900171
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100172/* SPBFCR - Buffer Control Register */
173#define SPBFCR_TXRST 0x80 /* Transmit Buffer Data Reset (qspi only) */
174#define SPBFCR_RXRST 0x40 /* Receive Buffer Data Reset (qspi only) */
175#define SPBFCR_TXTRG_MASK 0x30 /* Transmit Buffer Data Triggering Number */
176#define SPBFCR_RXTRG_MASK 0x07 /* Receive Buffer Data Triggering Number */
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900177
Geert Uytterhoeven2aae80b2013-12-24 10:49:33 +0100178#define DUMMY_DATA 0x00
179
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900180struct rspi_data {
181 void __iomem *addr;
182 u32 max_speed_hz;
183 struct spi_master *master;
184 struct list_head queue;
185 struct work_struct ws;
186 wait_queue_head_t wait;
187 spinlock_t lock;
188 struct clk *clk;
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100189 u8 spsr;
Geert Uytterhoeven348e5152014-01-12 11:27:43 +0100190 u16 spcmd;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900191 const struct spi_ops *ops;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900192
193 /* for dmaengine */
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900194 struct dma_chan *chan_tx;
195 struct dma_chan *chan_rx;
196 int irq;
197
198 unsigned dma_width_16bit:1;
199 unsigned dma_callbacked:1;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900200};
201
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100202static void rspi_write8(const struct rspi_data *rspi, u8 data, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900203{
204 iowrite8(data, rspi->addr + offset);
205}
206
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100207static void rspi_write16(const struct rspi_data *rspi, u16 data, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900208{
209 iowrite16(data, rspi->addr + offset);
210}
211
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100212static void rspi_write32(const struct rspi_data *rspi, u32 data, u16 offset)
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900213{
214 iowrite32(data, rspi->addr + offset);
215}
216
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100217static u8 rspi_read8(const struct rspi_data *rspi, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900218{
219 return ioread8(rspi->addr + offset);
220}
221
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100222static u16 rspi_read16(const struct rspi_data *rspi, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900223{
224 return ioread16(rspi->addr + offset);
225}
226
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900227/* optional functions */
228struct spi_ops {
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100229 int (*set_config_register)(const struct rspi_data *rspi,
230 int access_size);
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100231 int (*send_pio)(struct rspi_data *rspi, struct spi_transfer *t);
232 int (*receive_pio)(struct rspi_data *rspi, struct spi_transfer *t);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900233};
234
235/*
236 * functions for RSPI
237 */
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100238static int rspi_set_config_register(const struct rspi_data *rspi,
239 int access_size)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900240{
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900241 int spbr;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900242
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900243 /* Sets output mode(CMOS) and MOSI signal(from previous transfer) */
244 rspi_write8(rspi, 0x00, RSPI_SPPCR);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900245
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900246 /* Sets transfer bit rate */
247 spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz) - 1;
248 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
249
250 /* Sets number of frames to be used: 1 frame */
251 rspi_write8(rspi, 0x00, RSPI_SPDCR);
252
253 /* Sets RSPCK, SSL, next-access delay value */
254 rspi_write8(rspi, 0x00, RSPI_SPCKD);
255 rspi_write8(rspi, 0x00, RSPI_SSLND);
256 rspi_write8(rspi, 0x00, RSPI_SPND);
257
258 /* Sets parity, interrupt mask */
259 rspi_write8(rspi, 0x00, RSPI_SPCR2);
260
261 /* Sets SPCMD */
Geert Uytterhoeven348e5152014-01-12 11:27:43 +0100262 rspi_write16(rspi, SPCMD_SPB_8_TO_16(access_size) | rspi->spcmd,
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900263 RSPI_SPCMD0);
264
265 /* Sets RSPI mode */
266 rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
267
268 return 0;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900269}
270
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900271/*
272 * functions for QSPI
273 */
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100274static int qspi_set_config_register(const struct rspi_data *rspi,
275 int access_size)
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900276{
277 u16 spcmd;
278 int spbr;
279
280 /* Sets output mode(CMOS) and MOSI signal(from previous transfer) */
281 rspi_write8(rspi, 0x00, RSPI_SPPCR);
282
283 /* Sets transfer bit rate */
284 spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz);
285 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
286
287 /* Sets number of frames to be used: 1 frame */
288 rspi_write8(rspi, 0x00, RSPI_SPDCR);
289
290 /* Sets RSPCK, SSL, next-access delay value */
291 rspi_write8(rspi, 0x00, RSPI_SPCKD);
292 rspi_write8(rspi, 0x00, RSPI_SSLND);
293 rspi_write8(rspi, 0x00, RSPI_SPND);
294
295 /* Data Length Setting */
296 if (access_size == 8)
297 spcmd = SPCMD_SPB_8BIT;
298 else if (access_size == 16)
299 spcmd = SPCMD_SPB_16BIT;
Laurent Pinchart8e1c8092013-11-27 01:41:44 +0100300 else
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900301 spcmd = SPCMD_SPB_32BIT;
302
Geert Uytterhoeven348e5152014-01-12 11:27:43 +0100303 spcmd |= SPCMD_SCKDEN | SPCMD_SLNDEN | rspi->spcmd | SPCMD_SPNDEN;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900304
305 /* Resets transfer data length */
306 rspi_write32(rspi, 0, QSPI_SPBMUL0);
307
308 /* Resets transmit and receive buffer */
309 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
310 /* Sets buffer to allow normal operation */
311 rspi_write8(rspi, 0x00, QSPI_SPBFCR);
312
313 /* Sets SPCMD */
314 rspi_write16(rspi, spcmd, RSPI_SPCMD0);
315
316 /* Enables SPI function in a master mode */
317 rspi_write8(rspi, SPCR_SPE | SPCR_MSTR, RSPI_SPCR);
318
319 return 0;
320}
321
322#define set_config_register(spi, n) spi->ops->set_config_register(spi, n)
323
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100324static void rspi_enable_irq(const struct rspi_data *rspi, u8 enable)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900325{
326 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | enable, RSPI_SPCR);
327}
328
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100329static void rspi_disable_irq(const struct rspi_data *rspi, u8 disable)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900330{
331 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~disable, RSPI_SPCR);
332}
333
334static int rspi_wait_for_interrupt(struct rspi_data *rspi, u8 wait_mask,
335 u8 enable_bit)
336{
337 int ret;
338
339 rspi->spsr = rspi_read8(rspi, RSPI_SPSR);
340 rspi_enable_irq(rspi, enable_bit);
341 ret = wait_event_timeout(rspi->wait, rspi->spsr & wait_mask, HZ);
342 if (ret == 0 && !(rspi->spsr & wait_mask))
343 return -ETIMEDOUT;
344
345 return 0;
346}
347
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100348static void rspi_assert_ssl(const struct rspi_data *rspi)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900349{
350 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_SPE, RSPI_SPCR);
351}
352
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100353static void rspi_negate_ssl(const struct rspi_data *rspi)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900354{
355 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_SPE, RSPI_SPCR);
356}
357
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100358static int rspi_send_pio(struct rspi_data *rspi, struct spi_transfer *t)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900359{
360 int remain = t->len;
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100361 const u8 *data = t->tx_buf;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900362 while (remain > 0) {
363 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_TXMD,
364 RSPI_SPCR);
365
366 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
367 dev_err(&rspi->master->dev,
368 "%s: tx empty timeout\n", __func__);
369 return -ETIMEDOUT;
370 }
371
372 rspi_write16(rspi, *data, RSPI_SPDR);
373 data++;
374 remain--;
375 }
376
Geert Uytterhoevenb7ed6b82014-01-14 10:20:32 +0100377 /* Waiting for the last transmission */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900378 rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
379
380 return 0;
381}
382
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100383static int qspi_send_pio(struct rspi_data *rspi, struct spi_transfer *t)
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900384{
385 int remain = t->len;
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100386 const u8 *data = t->tx_buf;
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900387
388 rspi_write8(rspi, SPBFCR_TXRST, QSPI_SPBFCR);
389 rspi_write8(rspi, 0x00, QSPI_SPBFCR);
390
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900391 while (remain > 0) {
392
393 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
394 dev_err(&rspi->master->dev,
395 "%s: tx empty timeout\n", __func__);
396 return -ETIMEDOUT;
397 }
398 rspi_write8(rspi, *data++, RSPI_SPDR);
399
400 if (rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE) < 0) {
401 dev_err(&rspi->master->dev,
402 "%s: receive timeout\n", __func__);
403 return -ETIMEDOUT;
404 }
405 rspi_read8(rspi, RSPI_SPDR);
406
407 remain--;
408 }
409
Geert Uytterhoevenb7ed6b82014-01-14 10:20:32 +0100410 /* Waiting for the last transmission */
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900411 rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
412
413 return 0;
414}
415
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100416#define send_pio(spi, t) spi->ops->send_pio(spi, t)
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900417
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900418static void rspi_dma_complete(void *arg)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900419{
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900420 struct rspi_data *rspi = arg;
421
422 rspi->dma_callbacked = 1;
423 wake_up_interruptible(&rspi->wait);
424}
425
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100426static int rspi_dma_map_sg(struct scatterlist *sg, const void *buf,
427 unsigned len, struct dma_chan *chan,
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900428 enum dma_transfer_direction dir)
429{
430 sg_init_table(sg, 1);
431 sg_set_buf(sg, buf, len);
432 sg_dma_len(sg) = len;
433 return dma_map_sg(chan->device->dev, sg, 1, dir);
434}
435
436static void rspi_dma_unmap_sg(struct scatterlist *sg, struct dma_chan *chan,
437 enum dma_transfer_direction dir)
438{
439 dma_unmap_sg(chan->device->dev, sg, 1, dir);
440}
441
442static void rspi_memory_to_8bit(void *buf, const void *data, unsigned len)
443{
444 u16 *dst = buf;
445 const u8 *src = data;
446
447 while (len) {
448 *dst++ = (u16)(*src++);
449 len--;
450 }
451}
452
453static void rspi_memory_from_8bit(void *buf, const void *data, unsigned len)
454{
455 u8 *dst = buf;
456 const u16 *src = data;
457
458 while (len) {
459 *dst++ = (u8)*src++;
460 len--;
461 }
462}
463
464static int rspi_send_dma(struct rspi_data *rspi, struct spi_transfer *t)
465{
466 struct scatterlist sg;
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100467 const void *buf = NULL;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900468 struct dma_async_tx_descriptor *desc;
469 unsigned len;
470 int ret = 0;
471
472 if (rspi->dma_width_16bit) {
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100473 void *tmp;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900474 /*
475 * If DMAC bus width is 16-bit, the driver allocates a dummy
476 * buffer. And, the driver converts original data into the
477 * DMAC data as the following format:
478 * original data: 1st byte, 2nd byte ...
479 * DMAC data: 1st byte, dummy, 2nd byte, dummy ...
480 */
481 len = t->len * 2;
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100482 tmp = kmalloc(len, GFP_KERNEL);
483 if (!tmp)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900484 return -ENOMEM;
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100485 rspi_memory_to_8bit(tmp, t->tx_buf, t->len);
486 buf = tmp;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900487 } else {
488 len = t->len;
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100489 buf = t->tx_buf;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900490 }
491
492 if (!rspi_dma_map_sg(&sg, buf, len, rspi->chan_tx, DMA_TO_DEVICE)) {
493 ret = -EFAULT;
494 goto end_nomap;
495 }
496 desc = dmaengine_prep_slave_sg(rspi->chan_tx, &sg, 1, DMA_TO_DEVICE,
497 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
498 if (!desc) {
499 ret = -EIO;
500 goto end;
501 }
502
503 /*
504 * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be
505 * called. So, this driver disables the IRQ while DMA transfer.
506 */
507 disable_irq(rspi->irq);
508
509 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_TXMD, RSPI_SPCR);
510 rspi_enable_irq(rspi, SPCR_SPTIE);
511 rspi->dma_callbacked = 0;
512
513 desc->callback = rspi_dma_complete;
514 desc->callback_param = rspi;
515 dmaengine_submit(desc);
516 dma_async_issue_pending(rspi->chan_tx);
517
518 ret = wait_event_interruptible_timeout(rspi->wait,
519 rspi->dma_callbacked, HZ);
520 if (ret > 0 && rspi->dma_callbacked)
521 ret = 0;
522 else if (!ret)
523 ret = -ETIMEDOUT;
524 rspi_disable_irq(rspi, SPCR_SPTIE);
525
526 enable_irq(rspi->irq);
527
528end:
529 rspi_dma_unmap_sg(&sg, rspi->chan_tx, DMA_TO_DEVICE);
530end_nomap:
531 if (rspi->dma_width_16bit)
532 kfree(buf);
533
534 return ret;
535}
536
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100537static void rspi_receive_init(const struct rspi_data *rspi)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900538{
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100539 u8 spsr;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900540
541 spsr = rspi_read8(rspi, RSPI_SPSR);
542 if (spsr & SPSR_SPRF)
543 rspi_read16(rspi, RSPI_SPDR); /* dummy read */
544 if (spsr & SPSR_OVRF)
545 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPSR) & ~SPSR_OVRF,
Geert Uytterhoevendf900e62013-12-23 19:34:24 +0100546 RSPI_SPSR);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900547}
548
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100549static int rspi_receive_pio(struct rspi_data *rspi, struct spi_transfer *t)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900550{
551 int remain = t->len;
552 u8 *data;
553
554 rspi_receive_init(rspi);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900555
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100556 data = t->rx_buf;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900557 while (remain > 0) {
558 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_TXMD,
559 RSPI_SPCR);
560
561 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
562 dev_err(&rspi->master->dev,
563 "%s: tx empty timeout\n", __func__);
564 return -ETIMEDOUT;
565 }
566 /* dummy write for generate clock */
Geert Uytterhoeven2aae80b2013-12-24 10:49:33 +0100567 rspi_write16(rspi, DUMMY_DATA, RSPI_SPDR);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900568
569 if (rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE) < 0) {
570 dev_err(&rspi->master->dev,
571 "%s: receive timeout\n", __func__);
572 return -ETIMEDOUT;
573 }
574 /* SPDR allows 16 or 32-bit access only */
575 *data = (u8)rspi_read16(rspi, RSPI_SPDR);
576
577 data++;
578 remain--;
579 }
580
581 return 0;
582}
583
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100584static void qspi_receive_init(const struct rspi_data *rspi)
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900585{
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100586 u8 spsr;
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900587
588 spsr = rspi_read8(rspi, RSPI_SPSR);
589 if (spsr & SPSR_SPRF)
590 rspi_read8(rspi, RSPI_SPDR); /* dummy read */
591 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
592 rspi_write8(rspi, 0x00, QSPI_SPBFCR);
593}
594
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100595static int qspi_receive_pio(struct rspi_data *rspi, struct spi_transfer *t)
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900596{
597 int remain = t->len;
598 u8 *data;
599
600 qspi_receive_init(rspi);
601
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100602 data = t->rx_buf;
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900603 while (remain > 0) {
604
605 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
606 dev_err(&rspi->master->dev,
607 "%s: tx empty timeout\n", __func__);
608 return -ETIMEDOUT;
609 }
610 /* dummy write for generate clock */
Geert Uytterhoeven2aae80b2013-12-24 10:49:33 +0100611 rspi_write8(rspi, DUMMY_DATA, RSPI_SPDR);
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900612
613 if (rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE) < 0) {
614 dev_err(&rspi->master->dev,
615 "%s: receive timeout\n", __func__);
616 return -ETIMEDOUT;
617 }
618 /* SPDR allows 8, 16 or 32-bit access */
619 *data++ = rspi_read8(rspi, RSPI_SPDR);
620 remain--;
621 }
622
623 return 0;
624}
625
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100626#define receive_pio(spi, t) spi->ops->receive_pio(spi, t)
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900627
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900628static int rspi_receive_dma(struct rspi_data *rspi, struct spi_transfer *t)
629{
630 struct scatterlist sg, sg_dummy;
631 void *dummy = NULL, *rx_buf = NULL;
632 struct dma_async_tx_descriptor *desc, *desc_dummy;
633 unsigned len;
634 int ret = 0;
635
636 if (rspi->dma_width_16bit) {
637 /*
638 * If DMAC bus width is 16-bit, the driver allocates a dummy
639 * buffer. And, finally the driver converts the DMAC data into
640 * actual data as the following format:
641 * DMAC data: 1st byte, dummy, 2nd byte, dummy ...
642 * actual data: 1st byte, 2nd byte ...
643 */
644 len = t->len * 2;
645 rx_buf = kmalloc(len, GFP_KERNEL);
646 if (!rx_buf)
647 return -ENOMEM;
648 } else {
649 len = t->len;
650 rx_buf = t->rx_buf;
651 }
652
653 /* prepare dummy transfer to generate SPI clocks */
654 dummy = kzalloc(len, GFP_KERNEL);
655 if (!dummy) {
656 ret = -ENOMEM;
657 goto end_nomap;
658 }
659 if (!rspi_dma_map_sg(&sg_dummy, dummy, len, rspi->chan_tx,
660 DMA_TO_DEVICE)) {
661 ret = -EFAULT;
662 goto end_nomap;
663 }
664 desc_dummy = dmaengine_prep_slave_sg(rspi->chan_tx, &sg_dummy, 1,
665 DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
666 if (!desc_dummy) {
667 ret = -EIO;
668 goto end_dummy_mapped;
669 }
670
671 /* prepare receive transfer */
672 if (!rspi_dma_map_sg(&sg, rx_buf, len, rspi->chan_rx,
673 DMA_FROM_DEVICE)) {
674 ret = -EFAULT;
675 goto end_dummy_mapped;
676
677 }
678 desc = dmaengine_prep_slave_sg(rspi->chan_rx, &sg, 1, DMA_FROM_DEVICE,
679 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
680 if (!desc) {
681 ret = -EIO;
682 goto end;
683 }
684
685 rspi_receive_init(rspi);
686
687 /*
688 * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be
689 * called. So, this driver disables the IRQ while DMA transfer.
690 */
691 disable_irq(rspi->irq);
692
693 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_TXMD, RSPI_SPCR);
694 rspi_enable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE);
695 rspi->dma_callbacked = 0;
696
697 desc->callback = rspi_dma_complete;
698 desc->callback_param = rspi;
699 dmaengine_submit(desc);
700 dma_async_issue_pending(rspi->chan_rx);
701
702 desc_dummy->callback = NULL; /* No callback */
703 dmaengine_submit(desc_dummy);
704 dma_async_issue_pending(rspi->chan_tx);
705
706 ret = wait_event_interruptible_timeout(rspi->wait,
707 rspi->dma_callbacked, HZ);
708 if (ret > 0 && rspi->dma_callbacked)
709 ret = 0;
710 else if (!ret)
711 ret = -ETIMEDOUT;
712 rspi_disable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE);
713
714 enable_irq(rspi->irq);
715
716end:
717 rspi_dma_unmap_sg(&sg, rspi->chan_rx, DMA_FROM_DEVICE);
718end_dummy_mapped:
719 rspi_dma_unmap_sg(&sg_dummy, rspi->chan_tx, DMA_TO_DEVICE);
720end_nomap:
721 if (rspi->dma_width_16bit) {
722 if (!ret)
723 rspi_memory_from_8bit(t->rx_buf, rx_buf, t->len);
724 kfree(rx_buf);
725 }
726 kfree(dummy);
727
728 return ret;
729}
730
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100731static int rspi_is_dma(const struct rspi_data *rspi, struct spi_transfer *t)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900732{
733 if (t->tx_buf && rspi->chan_tx)
734 return 1;
735 /* If the module receives data by DMAC, it also needs TX DMAC */
736 if (t->rx_buf && rspi->chan_tx && rspi->chan_rx)
737 return 1;
738
739 return 0;
740}
741
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900742static void rspi_work(struct work_struct *work)
743{
744 struct rspi_data *rspi = container_of(work, struct rspi_data, ws);
745 struct spi_message *mesg;
746 struct spi_transfer *t;
747 unsigned long flags;
748 int ret;
749
Shimoda, Yoshihiro8d4d08c2013-08-27 11:15:09 +0900750 while (1) {
751 spin_lock_irqsave(&rspi->lock, flags);
752 if (list_empty(&rspi->queue)) {
753 spin_unlock_irqrestore(&rspi->lock, flags);
754 break;
755 }
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900756 mesg = list_entry(rspi->queue.next, struct spi_message, queue);
757 list_del_init(&mesg->queue);
758 spin_unlock_irqrestore(&rspi->lock, flags);
759
760 rspi_assert_ssl(rspi);
761
762 list_for_each_entry(t, &mesg->transfers, transfer_list) {
763 if (t->tx_buf) {
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900764 if (rspi_is_dma(rspi, t))
765 ret = rspi_send_dma(rspi, t);
766 else
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100767 ret = send_pio(rspi, t);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900768 if (ret < 0)
769 goto error;
770 }
771 if (t->rx_buf) {
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900772 if (rspi_is_dma(rspi, t))
773 ret = rspi_receive_dma(rspi, t);
774 else
Geert Uytterhoeven91949a22014-01-24 09:43:51 +0100775 ret = receive_pio(rspi, t);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900776 if (ret < 0)
777 goto error;
778 }
779 mesg->actual_length += t->len;
780 }
781 rspi_negate_ssl(rspi);
782
783 mesg->status = 0;
784 mesg->complete(mesg->context);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900785 }
786
787 return;
788
789error:
790 mesg->status = ret;
791 mesg->complete(mesg->context);
792}
793
794static int rspi_setup(struct spi_device *spi)
795{
796 struct rspi_data *rspi = spi_master_get_devdata(spi->master);
797
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900798 rspi->max_speed_hz = spi->max_speed_hz;
799
Geert Uytterhoeven348e5152014-01-12 11:27:43 +0100800 rspi->spcmd = SPCMD_SSLKP;
801 if (spi->mode & SPI_CPOL)
802 rspi->spcmd |= SPCMD_CPOL;
803 if (spi->mode & SPI_CPHA)
804 rspi->spcmd |= SPCMD_CPHA;
805
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900806 set_config_register(rspi, 8);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900807
808 return 0;
809}
810
811static int rspi_transfer(struct spi_device *spi, struct spi_message *mesg)
812{
813 struct rspi_data *rspi = spi_master_get_devdata(spi->master);
814 unsigned long flags;
815
816 mesg->actual_length = 0;
817 mesg->status = -EINPROGRESS;
818
819 spin_lock_irqsave(&rspi->lock, flags);
820 list_add_tail(&mesg->queue, &rspi->queue);
821 schedule_work(&rspi->ws);
822 spin_unlock_irqrestore(&rspi->lock, flags);
823
824 return 0;
825}
826
827static void rspi_cleanup(struct spi_device *spi)
828{
829}
830
831static irqreturn_t rspi_irq(int irq, void *_sr)
832{
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100833 struct rspi_data *rspi = _sr;
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100834 u8 spsr;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900835 irqreturn_t ret = IRQ_NONE;
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100836 u8 disable_irq = 0;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900837
838 rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
839 if (spsr & SPSR_SPRF)
840 disable_irq |= SPCR_SPRIE;
841 if (spsr & SPSR_SPTEF)
842 disable_irq |= SPCR_SPTIE;
843
844 if (disable_irq) {
845 ret = IRQ_HANDLED;
846 rspi_disable_irq(rspi, disable_irq);
847 wake_up(&rspi->wait);
848 }
849
850 return ret;
851}
852
Grant Likelyfd4a3192012-12-07 16:57:14 +0000853static int rspi_request_dma(struct rspi_data *rspi,
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900854 struct platform_device *pdev)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900855{
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100856 const struct rspi_plat_data *rspi_pd = dev_get_platdata(&pdev->dev);
Guennadi Liakhovetskie2b05092013-08-02 15:03:42 +0200857 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900858 dma_cap_mask_t mask;
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900859 struct dma_slave_config cfg;
860 int ret;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900861
Guennadi Liakhovetskie2b05092013-08-02 15:03:42 +0200862 if (!res || !rspi_pd)
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900863 return 0; /* The driver assumes no error. */
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900864
865 rspi->dma_width_16bit = rspi_pd->dma_width_16bit;
866
867 /* If the module receives data by DMAC, it also needs TX DMAC */
868 if (rspi_pd->dma_rx_id && rspi_pd->dma_tx_id) {
869 dma_cap_zero(mask);
870 dma_cap_set(DMA_SLAVE, mask);
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900871 rspi->chan_rx = dma_request_channel(mask, shdma_chan_filter,
872 (void *)rspi_pd->dma_rx_id);
873 if (rspi->chan_rx) {
874 cfg.slave_id = rspi_pd->dma_rx_id;
875 cfg.direction = DMA_DEV_TO_MEM;
Guennadi Liakhovetskie2b05092013-08-02 15:03:42 +0200876 cfg.dst_addr = 0;
877 cfg.src_addr = res->start + RSPI_SPDR;
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900878 ret = dmaengine_slave_config(rspi->chan_rx, &cfg);
879 if (!ret)
880 dev_info(&pdev->dev, "Use DMA when rx.\n");
881 else
882 return ret;
883 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900884 }
885 if (rspi_pd->dma_tx_id) {
886 dma_cap_zero(mask);
887 dma_cap_set(DMA_SLAVE, mask);
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900888 rspi->chan_tx = dma_request_channel(mask, shdma_chan_filter,
889 (void *)rspi_pd->dma_tx_id);
890 if (rspi->chan_tx) {
891 cfg.slave_id = rspi_pd->dma_tx_id;
892 cfg.direction = DMA_MEM_TO_DEV;
Guennadi Liakhovetskie2b05092013-08-02 15:03:42 +0200893 cfg.dst_addr = res->start + RSPI_SPDR;
894 cfg.src_addr = 0;
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900895 ret = dmaengine_slave_config(rspi->chan_tx, &cfg);
896 if (!ret)
897 dev_info(&pdev->dev, "Use DMA when tx\n");
898 else
899 return ret;
900 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900901 }
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900902
903 return 0;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900904}
905
Grant Likelyfd4a3192012-12-07 16:57:14 +0000906static void rspi_release_dma(struct rspi_data *rspi)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900907{
908 if (rspi->chan_tx)
909 dma_release_channel(rspi->chan_tx);
910 if (rspi->chan_rx)
911 dma_release_channel(rspi->chan_rx);
912}
913
Grant Likelyfd4a3192012-12-07 16:57:14 +0000914static int rspi_remove(struct platform_device *pdev)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900915{
Laurent Pinchart5ffbe2d2013-11-27 01:41:45 +0100916 struct rspi_data *rspi = platform_get_drvdata(pdev);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900917
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900918 rspi_release_dma(rspi);
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +0100919 clk_disable(rspi->clk);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900920
921 return 0;
922}
923
Grant Likelyfd4a3192012-12-07 16:57:14 +0000924static int rspi_probe(struct platform_device *pdev)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900925{
926 struct resource *res;
927 struct spi_master *master;
928 struct rspi_data *rspi;
929 int ret, irq;
930 char clk_name[16];
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100931 const struct rspi_plat_data *rspi_pd = dev_get_platdata(&pdev->dev);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900932 const struct spi_ops *ops;
933 const struct platform_device_id *id_entry = pdev->id_entry;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900934
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900935 ops = (struct spi_ops *)id_entry->driver_data;
936 /* ops parameter check */
937 if (!ops->set_config_register) {
938 dev_err(&pdev->dev, "there is no set_config_register\n");
939 return -ENODEV;
940 }
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900941
942 irq = platform_get_irq(pdev, 0);
943 if (irq < 0) {
944 dev_err(&pdev->dev, "platform_get_irq error\n");
945 return -ENODEV;
946 }
947
948 master = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data));
949 if (master == NULL) {
950 dev_err(&pdev->dev, "spi_alloc_master error.\n");
951 return -ENOMEM;
952 }
953
954 rspi = spi_master_get_devdata(master);
Jingoo Han24b5a822013-05-23 19:20:40 +0900955 platform_set_drvdata(pdev, rspi);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900956 rspi->ops = ops;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900957 rspi->master = master;
Laurent Pinchart5d79e9a2013-11-27 01:41:46 +0100958
959 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
960 rspi->addr = devm_ioremap_resource(&pdev->dev, res);
961 if (IS_ERR(rspi->addr)) {
962 ret = PTR_ERR(rspi->addr);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900963 goto error1;
964 }
965
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900966 snprintf(clk_name, sizeof(clk_name), "%s%d", id_entry->name, pdev->id);
Laurent Pinchart5d79e9a2013-11-27 01:41:46 +0100967 rspi->clk = devm_clk_get(&pdev->dev, clk_name);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900968 if (IS_ERR(rspi->clk)) {
969 dev_err(&pdev->dev, "cannot get clock\n");
970 ret = PTR_ERR(rspi->clk);
Laurent Pinchart5d79e9a2013-11-27 01:41:46 +0100971 goto error1;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900972 }
973 clk_enable(rspi->clk);
974
975 INIT_LIST_HEAD(&rspi->queue);
976 spin_lock_init(&rspi->lock);
977 INIT_WORK(&rspi->ws, rspi_work);
978 init_waitqueue_head(&rspi->wait);
979
Geert Uytterhoevenefd85ac2013-12-23 19:34:23 +0100980 if (rspi_pd && rspi_pd->num_chipselect)
981 master->num_chipselect = rspi_pd->num_chipselect;
982 else
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900983 master->num_chipselect = 2; /* default */
984
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900985 master->bus_num = pdev->id;
986 master->setup = rspi_setup;
987 master->transfer = rspi_transfer;
988 master->cleanup = rspi_cleanup;
Geert Uytterhoeven348e5152014-01-12 11:27:43 +0100989 master->mode_bits = SPI_CPHA | SPI_CPOL;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900990
Laurent Pinchart5d79e9a2013-11-27 01:41:46 +0100991 ret = devm_request_irq(&pdev->dev, irq, rspi_irq, 0,
992 dev_name(&pdev->dev), rspi);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900993 if (ret < 0) {
994 dev_err(&pdev->dev, "request_irq error\n");
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +0100995 goto error2;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900996 }
997
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900998 rspi->irq = irq;
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +0900999 ret = rspi_request_dma(rspi, pdev);
1000 if (ret < 0) {
1001 dev_err(&pdev->dev, "rspi_request_dma failed.\n");
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +01001002 goto error3;
Shimoda, Yoshihiro0243c532012-08-02 17:17:33 +09001003 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001004
Jingoo Han9e03d052013-12-04 14:13:50 +09001005 ret = devm_spi_register_master(&pdev->dev, master);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001006 if (ret < 0) {
1007 dev_err(&pdev->dev, "spi_register_master error.\n");
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +01001008 goto error3;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001009 }
1010
1011 dev_info(&pdev->dev, "probed\n");
1012
1013 return 0;
1014
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +01001015error3:
Laurent Pinchart5d79e9a2013-11-27 01:41:46 +01001016 rspi_release_dma(rspi);
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +01001017error2:
1018 clk_disable(rspi->clk);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001019error1:
1020 spi_master_put(master);
1021
1022 return ret;
1023}
1024
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001025static struct spi_ops rspi_ops = {
1026 .set_config_register = rspi_set_config_register,
Hiep Cao Minhcb52c672013-10-10 17:14:03 +09001027 .send_pio = rspi_send_pio,
1028 .receive_pio = rspi_receive_pio,
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001029};
1030
1031static struct spi_ops qspi_ops = {
1032 .set_config_register = qspi_set_config_register,
Hiep Cao Minhcb52c672013-10-10 17:14:03 +09001033 .send_pio = qspi_send_pio,
1034 .receive_pio = qspi_receive_pio,
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001035};
1036
1037static struct platform_device_id spi_driver_ids[] = {
1038 { "rspi", (kernel_ulong_t)&rspi_ops },
1039 { "qspi", (kernel_ulong_t)&qspi_ops },
1040 {},
1041};
1042
1043MODULE_DEVICE_TABLE(platform, spi_driver_ids);
1044
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001045static struct platform_driver rspi_driver = {
1046 .probe = rspi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001047 .remove = rspi_remove,
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001048 .id_table = spi_driver_ids,
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001049 .driver = {
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001050 .name = "renesas_spi",
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001051 .owner = THIS_MODULE,
1052 },
1053};
1054module_platform_driver(rspi_driver);
1055
1056MODULE_DESCRIPTION("Renesas RSPI bus driver");
1057MODULE_LICENSE("GPL v2");
1058MODULE_AUTHOR("Yoshihiro Shimoda");
1059MODULE_ALIAS("platform:rspi");