blob: a4ef641b522727e008daccbbc980af62566ee297 [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>
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090027
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010028#define RSPI_SPCR 0x00 /* Control Register */
29#define RSPI_SSLP 0x01 /* Slave Select Polarity Register */
30#define RSPI_SPPCR 0x02 /* Pin Control Register */
31#define RSPI_SPSR 0x03 /* Status Register */
32#define RSPI_SPDR 0x04 /* Data Register */
33#define RSPI_SPSCR 0x08 /* Sequence Control Register */
34#define RSPI_SPSSR 0x09 /* Sequence Status Register */
35#define RSPI_SPBR 0x0a /* Bit Rate Register */
36#define RSPI_SPDCR 0x0b /* Data Control Register */
37#define RSPI_SPCKD 0x0c /* Clock Delay Register */
38#define RSPI_SSLND 0x0d /* Slave Select Negation Delay Register */
39#define RSPI_SPND 0x0e /* Next-Access Delay Register */
Geert Uytterhoeven862d3572014-01-24 09:43:59 +010040#define RSPI_SPCR2 0x0f /* Control Register 2 (SH only) */
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010041#define RSPI_SPCMD0 0x10 /* Command Register 0 */
42#define RSPI_SPCMD1 0x12 /* Command Register 1 */
43#define RSPI_SPCMD2 0x14 /* Command Register 2 */
44#define RSPI_SPCMD3 0x16 /* Command Register 3 */
45#define RSPI_SPCMD4 0x18 /* Command Register 4 */
46#define RSPI_SPCMD5 0x1a /* Command Register 5 */
47#define RSPI_SPCMD6 0x1c /* Command Register 6 */
48#define RSPI_SPCMD7 0x1e /* Command Register 7 */
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +010049#define RSPI_SPCMD(i) (RSPI_SPCMD0 + (i) * 2)
50#define RSPI_NUM_SPCMD 8
51#define RSPI_RZ_NUM_SPCMD 4
52#define QSPI_NUM_SPCMD 4
Geert Uytterhoeven862d3572014-01-24 09:43:59 +010053
54/* RSPI on RZ only */
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010055#define RSPI_SPBFCR 0x20 /* Buffer Control Register */
56#define RSPI_SPBFDR 0x22 /* Buffer Data Count Setting Register */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090057
Geert Uytterhoeven862d3572014-01-24 09:43:59 +010058/* QSPI only */
Geert Uytterhoevenfbe50722014-01-12 11:27:38 +010059#define QSPI_SPBFCR 0x18 /* Buffer Control Register */
60#define QSPI_SPBDCR 0x1a /* Buffer Data Count Register */
61#define QSPI_SPBMUL0 0x1c /* Transfer Data Length Multiplier Setting Register 0 */
62#define QSPI_SPBMUL1 0x20 /* Transfer Data Length Multiplier Setting Register 1 */
63#define QSPI_SPBMUL2 0x24 /* Transfer Data Length Multiplier Setting Register 2 */
64#define QSPI_SPBMUL3 0x28 /* Transfer Data Length Multiplier Setting Register 3 */
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +010065#define QSPI_SPBMUL(i) (QSPI_SPBMUL0 + (i) * 4)
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +090066
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010067/* SPCR - Control Register */
68#define SPCR_SPRIE 0x80 /* Receive Interrupt Enable */
69#define SPCR_SPE 0x40 /* Function Enable */
70#define SPCR_SPTIE 0x20 /* Transmit Interrupt Enable */
71#define SPCR_SPEIE 0x10 /* Error Interrupt Enable */
72#define SPCR_MSTR 0x08 /* Master/Slave Mode Select */
73#define SPCR_MODFEN 0x04 /* Mode Fault Error Detection Enable */
74/* RSPI on SH only */
75#define SPCR_TXMD 0x02 /* TX Only Mode (vs. Full Duplex) */
76#define SPCR_SPMS 0x01 /* 3-wire Mode (vs. 4-wire) */
Geert Uytterhoeven6089af72014-08-28 10:10:19 +020077/* QSPI on R-Car Gen2 only */
Geert Uytterhoevenfbe50722014-01-12 11:27:38 +010078#define SPCR_WSWAP 0x02 /* Word Swap of read-data for DMAC */
79#define SPCR_BSWAP 0x01 /* Byte Swap of read-data for DMAC */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090080
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +010081/* SSLP - Slave Select Polarity Register */
82#define SSLP_SSL1P 0x02 /* SSL1 Signal Polarity Setting */
83#define SSLP_SSL0P 0x01 /* SSL0 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 Uytterhoeven6ab48652014-01-12 11:27:37 +0100162#define SPCMD_SSLA_MASK 0x0030 /* SSL Assert Signal Setting (RSPI) */
163#define SPCMD_BRDV_MASK 0x000c /* Bit Rate Division Setting */
164#define SPCMD_CPOL 0x0002 /* Clock Polarity Setting */
165#define SPCMD_CPHA 0x0001 /* Clock Phase Setting */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900166
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100167/* SPBFCR - Buffer Control Register */
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100168#define SPBFCR_TXRST 0x80 /* Transmit Buffer Data Reset */
169#define SPBFCR_RXRST 0x40 /* Receive Buffer Data Reset */
Geert Uytterhoeven6ab48652014-01-12 11:27:37 +0100170#define SPBFCR_TXTRG_MASK 0x30 /* Transmit Buffer Data Triggering Number */
171#define SPBFCR_RXTRG_MASK 0x07 /* Receive Buffer Data Triggering Number */
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900172/* QSPI on R-Car Gen2 */
173#define SPBFCR_TXTRG_1B 0x00 /* 31 bytes (1 byte available) */
174#define SPBFCR_TXTRG_32B 0x30 /* 0 byte (32 bytes available) */
175#define SPBFCR_RXTRG_1B 0x00 /* 1 byte (31 bytes available) */
176#define SPBFCR_RXTRG_32B 0x07 /* 32 bytes (0 byte available) */
177
178#define QSPI_BUFFER_SIZE 32u
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900179
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900180struct rspi_data {
181 void __iomem *addr;
182 u32 max_speed_hz;
183 struct spi_master *master;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900184 wait_queue_head_t wait;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900185 struct clk *clk;
Geert Uytterhoeven348e5152014-01-12 11:27:43 +0100186 u16 spcmd;
Geert Uytterhoeven06a7a3c2014-01-24 09:44:00 +0100187 u8 spsr;
188 u8 sppcr;
Geert Uytterhoeven93722202014-01-24 09:43:58 +0100189 int rx_irq, tx_irq;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900190 const struct spi_ops *ops;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900191
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900192 unsigned dma_callbacked:1;
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100193 unsigned byte_access:1;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900194};
195
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100196static void rspi_write8(const struct rspi_data *rspi, u8 data, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900197{
198 iowrite8(data, rspi->addr + offset);
199}
200
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100201static void rspi_write16(const struct rspi_data *rspi, u16 data, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900202{
203 iowrite16(data, rspi->addr + offset);
204}
205
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100206static void rspi_write32(const struct rspi_data *rspi, u32 data, u16 offset)
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900207{
208 iowrite32(data, rspi->addr + offset);
209}
210
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100211static u8 rspi_read8(const struct rspi_data *rspi, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900212{
213 return ioread8(rspi->addr + offset);
214}
215
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100216static u16 rspi_read16(const struct rspi_data *rspi, u16 offset)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900217{
218 return ioread16(rspi->addr + offset);
219}
220
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100221static void rspi_write_data(const struct rspi_data *rspi, u16 data)
222{
223 if (rspi->byte_access)
224 rspi_write8(rspi, data, RSPI_SPDR);
225 else /* 16 bit */
226 rspi_write16(rspi, data, RSPI_SPDR);
227}
228
229static u16 rspi_read_data(const struct rspi_data *rspi)
230{
231 if (rspi->byte_access)
232 return rspi_read8(rspi, RSPI_SPDR);
233 else /* 16 bit */
234 return rspi_read16(rspi, RSPI_SPDR);
235}
236
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900237/* optional functions */
238struct spi_ops {
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100239 int (*set_config_register)(struct rspi_data *rspi, int access_size);
Geert Uytterhoeveneb557f72014-01-24 09:43:55 +0100240 int (*transfer_one)(struct spi_master *master, struct spi_device *spi,
241 struct spi_transfer *xfer);
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100242 u16 mode_bits;
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +0200243 u16 flags;
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +0200244 u16 fifo_size;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900245};
246
247/*
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100248 * functions for RSPI on legacy SH
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900249 */
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100250static int rspi_set_config_register(struct rspi_data *rspi, int access_size)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900251{
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900252 int spbr;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900253
Geert Uytterhoeven06a7a3c2014-01-24 09:44:00 +0100254 /* Sets output mode, MOSI signal, and (optionally) loopback */
255 rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900256
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900257 /* Sets transfer bit rate */
Geert Uytterhoeven3beb61d2014-05-22 20:07:35 +0200258 spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk),
259 2 * rspi->max_speed_hz) - 1;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900260 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
261
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100262 /* Disable dummy transmission, set 16-bit word access, 1 frame */
263 rspi_write8(rspi, 0, RSPI_SPDCR);
264 rspi->byte_access = 0;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900265
266 /* Sets RSPCK, SSL, next-access delay value */
267 rspi_write8(rspi, 0x00, RSPI_SPCKD);
268 rspi_write8(rspi, 0x00, RSPI_SSLND);
269 rspi_write8(rspi, 0x00, RSPI_SPND);
270
271 /* Sets parity, interrupt mask */
272 rspi_write8(rspi, 0x00, RSPI_SPCR2);
273
274 /* Sets SPCMD */
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100275 rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size);
276 rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900277
278 /* Sets RSPI mode */
279 rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
280
281 return 0;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900282}
283
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900284/*
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100285 * functions for RSPI on RZ
286 */
287static int rspi_rz_set_config_register(struct rspi_data *rspi, int access_size)
288{
289 int spbr;
Chris Brandtaeb8f8c2016-08-05 09:36:03 -0400290 int div = 0;
291 unsigned long clksrc;
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100292
Geert Uytterhoeven06a7a3c2014-01-24 09:44:00 +0100293 /* Sets output mode, MOSI signal, and (optionally) loopback */
294 rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100295
Chris Brandtaeb8f8c2016-08-05 09:36:03 -0400296 clksrc = clk_get_rate(rspi->clk);
297 while (div < 3) {
298 if (rspi->max_speed_hz >= clksrc/4) /* 4=(CLK/2)/2 */
299 break;
300 div++;
301 clksrc /= 2;
302 }
303
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100304 /* Sets transfer bit rate */
Chris Brandtaeb8f8c2016-08-05 09:36:03 -0400305 spbr = DIV_ROUND_UP(clksrc, 2 * rspi->max_speed_hz) - 1;
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100306 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
Chris Brandtaeb8f8c2016-08-05 09:36:03 -0400307 rspi->spcmd |= div << 2;
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100308
309 /* Disable dummy transmission, set byte access */
310 rspi_write8(rspi, SPDCR_SPLBYTE, RSPI_SPDCR);
311 rspi->byte_access = 1;
312
313 /* Sets RSPCK, SSL, next-access delay value */
314 rspi_write8(rspi, 0x00, RSPI_SPCKD);
315 rspi_write8(rspi, 0x00, RSPI_SSLND);
316 rspi_write8(rspi, 0x00, RSPI_SPND);
317
318 /* Sets SPCMD */
319 rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size);
320 rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
321
322 /* Sets RSPI mode */
323 rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
324
325 return 0;
326}
327
328/*
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900329 * functions for QSPI
330 */
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100331static int qspi_set_config_register(struct rspi_data *rspi, int access_size)
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900332{
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900333 int spbr;
334
Geert Uytterhoeven06a7a3c2014-01-24 09:44:00 +0100335 /* Sets output mode, MOSI signal, and (optionally) loopback */
336 rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900337
338 /* Sets transfer bit rate */
Geert Uytterhoeven3beb61d2014-05-22 20:07:35 +0200339 spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk), 2 * rspi->max_speed_hz);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900340 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
341
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100342 /* Disable dummy transmission, set byte access */
343 rspi_write8(rspi, 0, RSPI_SPDCR);
344 rspi->byte_access = 1;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900345
346 /* Sets RSPCK, SSL, next-access delay value */
347 rspi_write8(rspi, 0x00, RSPI_SPCKD);
348 rspi_write8(rspi, 0x00, RSPI_SSLND);
349 rspi_write8(rspi, 0x00, RSPI_SPND);
350
351 /* Data Length Setting */
352 if (access_size == 8)
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100353 rspi->spcmd |= SPCMD_SPB_8BIT;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900354 else if (access_size == 16)
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100355 rspi->spcmd |= SPCMD_SPB_16BIT;
Laurent Pinchart8e1c8092013-11-27 01:41:44 +0100356 else
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100357 rspi->spcmd |= SPCMD_SPB_32BIT;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900358
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100359 rspi->spcmd |= SPCMD_SCKDEN | SPCMD_SLNDEN | SPCMD_SPNDEN;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900360
361 /* Resets transfer data length */
362 rspi_write32(rspi, 0, QSPI_SPBMUL0);
363
364 /* Resets transmit and receive buffer */
365 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
366 /* Sets buffer to allow normal operation */
367 rspi_write8(rspi, 0x00, QSPI_SPBFCR);
368
369 /* Sets SPCMD */
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100370 rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900371
Geert Uytterhoevenb458a342017-12-07 11:09:21 +0100372 /* Sets RSPI mode */
373 rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900374
375 return 0;
376}
377
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900378static void qspi_update(const struct rspi_data *rspi, u8 mask, u8 val, u8 reg)
379{
380 u8 data;
381
382 data = rspi_read8(rspi, reg);
383 data &= ~mask;
384 data |= (val & mask);
385 rspi_write8(rspi, data, reg);
386}
387
Geert Uytterhoevencb76b1c2015-06-23 15:04:29 +0200388static unsigned int qspi_set_send_trigger(struct rspi_data *rspi,
389 unsigned int len)
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900390{
391 unsigned int n;
392
393 n = min(len, QSPI_BUFFER_SIZE);
394
395 if (len >= QSPI_BUFFER_SIZE) {
396 /* sets triggering number to 32 bytes */
397 qspi_update(rspi, SPBFCR_TXTRG_MASK,
398 SPBFCR_TXTRG_32B, QSPI_SPBFCR);
399 } else {
400 /* sets triggering number to 1 byte */
401 qspi_update(rspi, SPBFCR_TXTRG_MASK,
402 SPBFCR_TXTRG_1B, QSPI_SPBFCR);
403 }
404
405 return n;
406}
407
Hiep Cao Minh3be09be2016-11-04 17:38:54 +0900408static int qspi_set_receive_trigger(struct rspi_data *rspi, unsigned int len)
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900409{
410 unsigned int n;
411
412 n = min(len, QSPI_BUFFER_SIZE);
413
414 if (len >= QSPI_BUFFER_SIZE) {
415 /* sets triggering number to 32 bytes */
416 qspi_update(rspi, SPBFCR_RXTRG_MASK,
417 SPBFCR_RXTRG_32B, QSPI_SPBFCR);
418 } else {
419 /* sets triggering number to 1 byte */
420 qspi_update(rspi, SPBFCR_RXTRG_MASK,
421 SPBFCR_RXTRG_1B, QSPI_SPBFCR);
422 }
Hiep Cao Minh3be09be2016-11-04 17:38:54 +0900423 return n;
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900424}
425
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900426#define set_config_register(spi, n) spi->ops->set_config_register(spi, n)
427
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100428static void rspi_enable_irq(const struct rspi_data *rspi, u8 enable)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900429{
430 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | enable, RSPI_SPCR);
431}
432
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100433static void rspi_disable_irq(const struct rspi_data *rspi, u8 disable)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900434{
435 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~disable, RSPI_SPCR);
436}
437
438static int rspi_wait_for_interrupt(struct rspi_data *rspi, u8 wait_mask,
439 u8 enable_bit)
440{
441 int ret;
442
443 rspi->spsr = rspi_read8(rspi, RSPI_SPSR);
Geert Uytterhoeven5dd1ad22014-02-04 11:06:24 +0100444 if (rspi->spsr & wait_mask)
445 return 0;
446
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900447 rspi_enable_irq(rspi, enable_bit);
448 ret = wait_event_timeout(rspi->wait, rspi->spsr & wait_mask, HZ);
449 if (ret == 0 && !(rspi->spsr & wait_mask))
450 return -ETIMEDOUT;
451
452 return 0;
453}
454
Geert Uytterhoeven5f684c32014-06-02 15:38:03 +0200455static inline int rspi_wait_for_tx_empty(struct rspi_data *rspi)
456{
457 return rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
458}
459
460static inline int rspi_wait_for_rx_full(struct rspi_data *rspi)
461{
462 return rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE);
463}
464
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100465static int rspi_data_out(struct rspi_data *rspi, u8 data)
466{
Geert Uytterhoeven5f684c32014-06-02 15:38:03 +0200467 int error = rspi_wait_for_tx_empty(rspi);
468 if (error < 0) {
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100469 dev_err(&rspi->master->dev, "transmit timeout\n");
Geert Uytterhoeven5f684c32014-06-02 15:38:03 +0200470 return error;
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100471 }
472 rspi_write_data(rspi, data);
473 return 0;
474}
475
476static int rspi_data_in(struct rspi_data *rspi)
477{
Geert Uytterhoeven5f684c32014-06-02 15:38:03 +0200478 int error;
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100479 u8 data;
480
Geert Uytterhoeven5f684c32014-06-02 15:38:03 +0200481 error = rspi_wait_for_rx_full(rspi);
482 if (error < 0) {
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100483 dev_err(&rspi->master->dev, "receive timeout\n");
Geert Uytterhoeven5f684c32014-06-02 15:38:03 +0200484 return error;
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100485 }
486 data = rspi_read_data(rspi);
487 return data;
488}
489
Geert Uytterhoeven6837b8e92014-06-02 15:38:07 +0200490static int rspi_pio_transfer(struct rspi_data *rspi, const u8 *tx, u8 *rx,
491 unsigned int n)
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100492{
Geert Uytterhoeven6837b8e92014-06-02 15:38:07 +0200493 while (n-- > 0) {
494 if (tx) {
495 int ret = rspi_data_out(rspi, *tx++);
496 if (ret < 0)
497 return ret;
498 }
499 if (rx) {
500 int ret = rspi_data_in(rspi);
501 if (ret < 0)
502 return ret;
503 *rx++ = ret;
504 }
505 }
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100506
Geert Uytterhoeven6837b8e92014-06-02 15:38:07 +0200507 return 0;
Geert Uytterhoeven35301c92014-01-24 09:43:54 +0100508}
509
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900510static void rspi_dma_complete(void *arg)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900511{
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900512 struct rspi_data *rspi = arg;
513
514 rspi->dma_callbacked = 1;
515 wake_up_interruptible(&rspi->wait);
516}
517
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200518static int rspi_dma_transfer(struct rspi_data *rspi, struct sg_table *tx,
519 struct sg_table *rx)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900520{
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200521 struct dma_async_tx_descriptor *desc_tx = NULL, *desc_rx = NULL;
522 u8 irq_mask = 0;
523 unsigned int other_irq = 0;
524 dma_cookie_t cookie;
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +0200525 int ret;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900526
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200527 /* First prepare and submit the DMA request(s), as this may fail */
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200528 if (rx) {
529 desc_rx = dmaengine_prep_slave_sg(rspi->master->dma_rx,
Stefan Agner768d59f2018-03-19 23:16:22 +0100530 rx->sgl, rx->nents, DMA_DEV_TO_MEM,
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200531 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200532 if (!desc_rx) {
533 ret = -EAGAIN;
534 goto no_dma_rx;
535 }
536
537 desc_rx->callback = rspi_dma_complete;
538 desc_rx->callback_param = rspi;
539 cookie = dmaengine_submit(desc_rx);
540 if (dma_submit_error(cookie)) {
541 ret = cookie;
542 goto no_dma_rx;
543 }
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200544
545 irq_mask |= SPCR_SPRIE;
546 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900547
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200548 if (tx) {
549 desc_tx = dmaengine_prep_slave_sg(rspi->master->dma_tx,
Stefan Agner768d59f2018-03-19 23:16:22 +0100550 tx->sgl, tx->nents, DMA_MEM_TO_DEV,
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200551 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
552 if (!desc_tx) {
553 ret = -EAGAIN;
554 goto no_dma_tx;
555 }
556
557 if (rx) {
558 /* No callback */
559 desc_tx->callback = NULL;
560 } else {
561 desc_tx->callback = rspi_dma_complete;
562 desc_tx->callback_param = rspi;
563 }
564 cookie = dmaengine_submit(desc_tx);
565 if (dma_submit_error(cookie)) {
566 ret = cookie;
567 goto no_dma_tx;
568 }
569
570 irq_mask |= SPCR_SPTIE;
571 }
572
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900573 /*
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200574 * DMAC needs SPxIE, but if SPxIE is set, the IRQ routine will be
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900575 * called. So, this driver disables the IRQ while DMA transfer.
576 */
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200577 if (tx)
578 disable_irq(other_irq = rspi->tx_irq);
579 if (rx && rspi->rx_irq != other_irq)
580 disable_irq(rspi->rx_irq);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900581
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200582 rspi_enable_irq(rspi, irq_mask);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900583 rspi->dma_callbacked = 0;
584
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200585 /* Now start DMA */
586 if (rx)
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200587 dma_async_issue_pending(rspi->master->dma_rx);
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200588 if (tx)
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200589 dma_async_issue_pending(rspi->master->dma_tx);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900590
591 ret = wait_event_interruptible_timeout(rspi->wait,
592 rspi->dma_callbacked, HZ);
Geert Uytterhoeven8dbbaa42018-09-05 10:49:39 +0200593 if (ret > 0 && rspi->dma_callbacked) {
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900594 ret = 0;
Geert Uytterhoeven8dbbaa42018-09-05 10:49:39 +0200595 } else {
596 if (!ret) {
597 dev_err(&rspi->master->dev, "DMA timeout\n");
598 ret = -ETIMEDOUT;
599 }
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200600 if (tx)
601 dmaengine_terminate_all(rspi->master->dma_tx);
602 if (rx)
603 dmaengine_terminate_all(rspi->master->dma_rx);
604 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900605
Geert Uytterhoevenc52fb6d2014-06-02 15:38:15 +0200606 rspi_disable_irq(rspi, irq_mask);
607
608 if (tx)
609 enable_irq(rspi->tx_irq);
610 if (rx && rspi->rx_irq != other_irq)
611 enable_irq(rspi->rx_irq);
612
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900613 return ret;
Geert Uytterhoeven85912a82014-07-09 12:26:22 +0200614
Geert Uytterhoeven3819bc82014-08-06 14:58:58 +0200615no_dma_tx:
616 if (rx)
617 dmaengine_terminate_all(rspi->master->dma_rx);
618no_dma_rx:
619 if (ret == -EAGAIN) {
620 pr_warn_once("%s %s: DMA not available, falling back to PIO\n",
621 dev_driver_string(&rspi->master->dev),
622 dev_name(&rspi->master->dev));
623 }
624 return ret;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900625}
626
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100627static void rspi_receive_init(const struct rspi_data *rspi)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900628{
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100629 u8 spsr;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900630
631 spsr = rspi_read8(rspi, RSPI_SPSR);
632 if (spsr & SPSR_SPRF)
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100633 rspi_read_data(rspi); /* dummy read */
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900634 if (spsr & SPSR_OVRF)
635 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPSR) & ~SPSR_OVRF,
Geert Uytterhoevendf900e62013-12-23 19:34:24 +0100636 RSPI_SPSR);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900637}
638
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100639static void rspi_rz_receive_init(const struct rspi_data *rspi)
640{
641 rspi_receive_init(rspi);
642 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, RSPI_SPBFCR);
643 rspi_write8(rspi, 0, RSPI_SPBFCR);
644}
645
Geert Uytterhoevenbaf588f2013-12-24 10:49:32 +0100646static void qspi_receive_init(const struct rspi_data *rspi)
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900647{
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100648 u8 spsr;
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900649
650 spsr = rspi_read8(rspi, RSPI_SPSR);
651 if (spsr & SPSR_SPRF)
Geert Uytterhoeven74da7682014-01-24 09:43:53 +0100652 rspi_read_data(rspi); /* dummy read */
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900653 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
Geert Uytterhoeven340a15e2014-01-24 09:43:57 +0100654 rspi_write8(rspi, 0, QSPI_SPBFCR);
Hiep Cao Minhcb52c672013-10-10 17:14:03 +0900655}
656
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +0200657static bool __rspi_can_dma(const struct rspi_data *rspi,
658 const struct spi_transfer *xfer)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900659{
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +0200660 return xfer->len > rspi->ops->fifo_size;
661}
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900662
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +0200663static bool rspi_can_dma(struct spi_master *master, struct spi_device *spi,
664 struct spi_transfer *xfer)
665{
666 struct rspi_data *rspi = spi_master_get_devdata(master);
667
668 return __rspi_can_dma(rspi, xfer);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900669}
670
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900671static int rspi_dma_check_then_transfer(struct rspi_data *rspi,
672 struct spi_transfer *xfer)
673{
Hiep Cao Minh63103722015-04-30 11:12:12 +0900674 if (!rspi->master->can_dma || !__rspi_can_dma(rspi, xfer))
675 return -EAGAIN;
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900676
Hiep Cao Minh63103722015-04-30 11:12:12 +0900677 /* rx_buf can be NULL on RSPI on SH in TX-only Mode */
678 return rspi_dma_transfer(rspi, &xfer->tx_sg,
679 xfer->rx_buf ? &xfer->rx_sg : NULL);
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900680}
681
Geert Uytterhoeven8b983e92014-06-02 15:38:19 +0200682static int rspi_common_transfer(struct rspi_data *rspi,
683 struct spi_transfer *xfer)
684{
685 int ret;
686
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900687 ret = rspi_dma_check_then_transfer(rspi, xfer);
688 if (ret != -EAGAIN)
689 return ret;
Geert Uytterhoeven8b983e92014-06-02 15:38:19 +0200690
691 ret = rspi_pio_transfer(rspi, xfer->tx_buf, xfer->rx_buf, xfer->len);
692 if (ret < 0)
693 return ret;
694
695 /* Wait for the last transmission */
696 rspi_wait_for_tx_empty(rspi);
697
698 return 0;
699}
700
Geert Uytterhoeven8393fa72014-06-02 15:38:13 +0200701static int rspi_transfer_one(struct spi_master *master, struct spi_device *spi,
702 struct spi_transfer *xfer)
Geert Uytterhoeven8449fd72014-01-24 09:43:56 +0100703{
Geert Uytterhoeven8393fa72014-06-02 15:38:13 +0200704 struct rspi_data *rspi = spi_master_get_devdata(master);
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +0200705 u8 spcr;
Geert Uytterhoeven8449fd72014-01-24 09:43:56 +0100706
Geert Uytterhoeven8449fd72014-01-24 09:43:56 +0100707 spcr = rspi_read8(rspi, RSPI_SPCR);
Geert Uytterhoeven6837b8e92014-06-02 15:38:07 +0200708 if (xfer->rx_buf) {
Geert Uytterhoeven32c64262014-06-02 15:38:04 +0200709 rspi_receive_init(rspi);
Geert Uytterhoeven8449fd72014-01-24 09:43:56 +0100710 spcr &= ~SPCR_TXMD;
Geert Uytterhoeven32c64262014-06-02 15:38:04 +0200711 } else {
Geert Uytterhoeven8449fd72014-01-24 09:43:56 +0100712 spcr |= SPCR_TXMD;
Geert Uytterhoeven32c64262014-06-02 15:38:04 +0200713 }
Geert Uytterhoeven8449fd72014-01-24 09:43:56 +0100714 rspi_write8(rspi, spcr, RSPI_SPCR);
715
Geert Uytterhoeven8b983e92014-06-02 15:38:19 +0200716 return rspi_common_transfer(rspi, xfer);
Geert Uytterhoeven8449fd72014-01-24 09:43:56 +0100717}
718
Geert Uytterhoeven03e627c2014-06-02 15:38:16 +0200719static int rspi_rz_transfer_one(struct spi_master *master,
720 struct spi_device *spi,
721 struct spi_transfer *xfer)
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100722{
Geert Uytterhoeven03e627c2014-06-02 15:38:16 +0200723 struct rspi_data *rspi = spi_master_get_devdata(master);
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100724
725 rspi_rz_receive_init(rspi);
726
Geert Uytterhoeven8b983e92014-06-02 15:38:19 +0200727 return rspi_common_transfer(rspi, xfer);
Geert Uytterhoeven862d3572014-01-24 09:43:59 +0100728}
729
Hiep Cao Minha91bbe72015-05-22 18:59:36 +0900730static int qspi_trigger_transfer_out_in(struct rspi_data *rspi, const u8 *tx,
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900731 u8 *rx, unsigned int len)
732{
Geert Uytterhoevencb76b1c2015-06-23 15:04:29 +0200733 unsigned int i, n;
734 int ret;
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900735
736 while (len > 0) {
737 n = qspi_set_send_trigger(rspi, len);
738 qspi_set_receive_trigger(rspi, len);
739 if (n == QSPI_BUFFER_SIZE) {
Geert Uytterhoeven5d4db692015-06-23 15:04:28 +0200740 ret = rspi_wait_for_tx_empty(rspi);
741 if (ret < 0) {
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900742 dev_err(&rspi->master->dev, "transmit timeout\n");
Geert Uytterhoeven5d4db692015-06-23 15:04:28 +0200743 return ret;
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900744 }
745 for (i = 0; i < n; i++)
746 rspi_write_data(rspi, *tx++);
747
Geert Uytterhoeven5d4db692015-06-23 15:04:28 +0200748 ret = rspi_wait_for_rx_full(rspi);
749 if (ret < 0) {
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900750 dev_err(&rspi->master->dev, "receive timeout\n");
Geert Uytterhoeven5d4db692015-06-23 15:04:28 +0200751 return ret;
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900752 }
753 for (i = 0; i < n; i++)
754 *rx++ = rspi_read_data(rspi);
755 } else {
756 ret = rspi_pio_transfer(rspi, tx, rx, n);
757 if (ret < 0)
758 return ret;
759 }
760 len -= n;
761 }
762
763 return 0;
764}
765
Geert Uytterhoeven340a15e2014-01-24 09:43:57 +0100766static int qspi_transfer_out_in(struct rspi_data *rspi,
767 struct spi_transfer *xfer)
768{
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900769 int ret;
770
Geert Uytterhoeven340a15e2014-01-24 09:43:57 +0100771 qspi_receive_init(rspi);
772
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900773 ret = rspi_dma_check_then_transfer(rspi, xfer);
774 if (ret != -EAGAIN)
775 return ret;
776
Hiep Cao Minhcc2e9322015-05-22 18:59:37 +0900777 return qspi_trigger_transfer_out_in(rspi, xfer->tx_buf,
Hiep Cao Minh4b6fe3e2014-10-23 12:14:13 +0900778 xfer->rx_buf, xfer->len);
Geert Uytterhoeven340a15e2014-01-24 09:43:57 +0100779}
780
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100781static int qspi_transfer_out(struct rspi_data *rspi, struct spi_transfer *xfer)
782{
Arnd Bergmanndb300832016-11-08 14:46:12 +0100783 const u8 *tx = xfer->tx_buf;
784 unsigned int n = xfer->len;
785 unsigned int i, len;
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100786 int ret;
787
Geert Uytterhoeven85912a82014-07-09 12:26:22 +0200788 if (rspi->master->can_dma && __rspi_can_dma(rspi, xfer)) {
789 ret = rspi_dma_transfer(rspi, &xfer->tx_sg, NULL);
790 if (ret != -EAGAIN)
791 return ret;
792 }
Geert Uytterhoeven4f12b5e2014-06-02 15:38:17 +0200793
Arnd Bergmanndb300832016-11-08 14:46:12 +0100794 while (n > 0) {
795 len = qspi_set_send_trigger(rspi, n);
796 if (len == QSPI_BUFFER_SIZE) {
797 ret = rspi_wait_for_tx_empty(rspi);
798 if (ret < 0) {
799 dev_err(&rspi->master->dev, "transmit timeout\n");
800 return ret;
801 }
802 for (i = 0; i < len; i++)
803 rspi_write_data(rspi, *tx++);
804 } else {
DongCVad16d4a2017-02-15 19:50:52 +0900805 ret = rspi_pio_transfer(rspi, tx, NULL, len);
Arnd Bergmanndb300832016-11-08 14:46:12 +0100806 if (ret < 0)
807 return ret;
808 }
809 n -= len;
810 }
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100811
812 /* Wait for the last transmission */
Geert Uytterhoeven5f684c32014-06-02 15:38:03 +0200813 rspi_wait_for_tx_empty(rspi);
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100814
815 return 0;
816}
817
818static int qspi_transfer_in(struct rspi_data *rspi, struct spi_transfer *xfer)
819{
Arnd Bergmanndb300832016-11-08 14:46:12 +0100820 u8 *rx = xfer->rx_buf;
821 unsigned int n = xfer->len;
822 unsigned int i, len;
823 int ret;
824
Geert Uytterhoeven85912a82014-07-09 12:26:22 +0200825 if (rspi->master->can_dma && __rspi_can_dma(rspi, xfer)) {
826 int ret = rspi_dma_transfer(rspi, NULL, &xfer->rx_sg);
827 if (ret != -EAGAIN)
828 return ret;
829 }
Geert Uytterhoeven4f12b5e2014-06-02 15:38:17 +0200830
Arnd Bergmanndb300832016-11-08 14:46:12 +0100831 while (n > 0) {
832 len = qspi_set_receive_trigger(rspi, n);
833 if (len == QSPI_BUFFER_SIZE) {
834 ret = rspi_wait_for_rx_full(rspi);
835 if (ret < 0) {
836 dev_err(&rspi->master->dev, "receive timeout\n");
837 return ret;
838 }
839 for (i = 0; i < len; i++)
840 *rx++ = rspi_read_data(rspi);
841 } else {
DongCVad16d4a2017-02-15 19:50:52 +0900842 ret = rspi_pio_transfer(rspi, NULL, rx, len);
Arnd Bergmanndb300832016-11-08 14:46:12 +0100843 if (ret < 0)
844 return ret;
Arnd Bergmanndb300832016-11-08 14:46:12 +0100845 }
846 n -= len;
847 }
848
849 return 0;
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100850}
851
Geert Uytterhoeveneb557f72014-01-24 09:43:55 +0100852static int qspi_transfer_one(struct spi_master *master, struct spi_device *spi,
853 struct spi_transfer *xfer)
854{
855 struct rspi_data *rspi = spi_master_get_devdata(master);
Geert Uytterhoeveneb557f72014-01-24 09:43:55 +0100856
Geert Uytterhoevenba824d42014-02-21 17:29:18 +0100857 if (spi->mode & SPI_LOOP) {
858 return qspi_transfer_out_in(rspi, xfer);
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +0200859 } else if (xfer->tx_nbits > SPI_NBITS_SINGLE) {
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100860 /* Quad or Dual SPI Write */
861 return qspi_transfer_out(rspi, xfer);
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +0200862 } else if (xfer->rx_nbits > SPI_NBITS_SINGLE) {
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100863 /* Quad or Dual SPI Read */
864 return qspi_transfer_in(rspi, xfer);
865 } else {
866 /* Single SPI Transfer */
867 return qspi_transfer_out_in(rspi, xfer);
868 }
Geert Uytterhoeveneb557f72014-01-24 09:43:55 +0100869}
870
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900871static int rspi_setup(struct spi_device *spi)
872{
873 struct rspi_data *rspi = spi_master_get_devdata(spi->master);
874
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900875 rspi->max_speed_hz = spi->max_speed_hz;
876
Geert Uytterhoeven348e5152014-01-12 11:27:43 +0100877 rspi->spcmd = SPCMD_SSLKP;
878 if (spi->mode & SPI_CPOL)
879 rspi->spcmd |= SPCMD_CPOL;
880 if (spi->mode & SPI_CPHA)
881 rspi->spcmd |= SPCMD_CPHA;
882
Geert Uytterhoeven06a7a3c2014-01-24 09:44:00 +0100883 /* CMOS output mode and MOSI signal from previous transfer */
884 rspi->sppcr = 0;
885 if (spi->mode & SPI_LOOP)
886 rspi->sppcr |= SPPCR_SPLP;
887
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900888 set_config_register(rspi, 8);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900889
890 return 0;
891}
892
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100893static u16 qspi_transfer_mode(const struct spi_transfer *xfer)
894{
895 if (xfer->tx_buf)
896 switch (xfer->tx_nbits) {
897 case SPI_NBITS_QUAD:
898 return SPCMD_SPIMOD_QUAD;
899 case SPI_NBITS_DUAL:
900 return SPCMD_SPIMOD_DUAL;
901 default:
902 return 0;
903 }
904 if (xfer->rx_buf)
905 switch (xfer->rx_nbits) {
906 case SPI_NBITS_QUAD:
907 return SPCMD_SPIMOD_QUAD | SPCMD_SPRW;
908 case SPI_NBITS_DUAL:
909 return SPCMD_SPIMOD_DUAL | SPCMD_SPRW;
910 default:
911 return 0;
912 }
913
914 return 0;
915}
916
917static int qspi_setup_sequencer(struct rspi_data *rspi,
918 const struct spi_message *msg)
919{
920 const struct spi_transfer *xfer;
921 unsigned int i = 0, len = 0;
922 u16 current_mode = 0xffff, mode;
923
924 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
925 mode = qspi_transfer_mode(xfer);
926 if (mode == current_mode) {
927 len += xfer->len;
928 continue;
929 }
930
931 /* Transfer mode change */
932 if (i) {
933 /* Set transfer data length of previous transfer */
934 rspi_write32(rspi, len, QSPI_SPBMUL(i - 1));
935 }
936
937 if (i >= QSPI_NUM_SPCMD) {
938 dev_err(&msg->spi->dev,
939 "Too many different transfer modes");
940 return -EINVAL;
941 }
942
943 /* Program transfer mode for this transfer */
944 rspi_write16(rspi, rspi->spcmd | mode, RSPI_SPCMD(i));
945 current_mode = mode;
946 len = xfer->len;
947 i++;
948 }
949 if (i) {
950 /* Set final transfer data length and sequence length */
951 rspi_write32(rspi, len, QSPI_SPBMUL(i - 1));
952 rspi_write8(rspi, i - 1, RSPI_SPSCR);
953 }
954
955 return 0;
956}
957
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100958static int rspi_prepare_message(struct spi_master *master,
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100959 struct spi_message *msg)
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100960{
961 struct rspi_data *rspi = spi_master_get_devdata(master);
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100962 int ret;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900963
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100964 if (msg->spi->mode &
965 (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)) {
966 /* Setup sequencer for messages with multiple transfer modes */
967 ret = qspi_setup_sequencer(rspi, msg);
968 if (ret < 0)
969 return ret;
970 }
971
972 /* Enable SPI function in master mode */
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100973 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_SPE, RSPI_SPCR);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900974 return 0;
975}
976
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100977static int rspi_unprepare_message(struct spi_master *master,
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100978 struct spi_message *msg)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900979{
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100980 struct rspi_data *rspi = spi_master_get_devdata(master);
981
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100982 /* Disable SPI function */
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100983 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_SPE, RSPI_SPCR);
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +0100984
985 /* Reset sequencer for Single SPI Transfers */
986 rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
987 rspi_write8(rspi, 0, RSPI_SPSCR);
Geert Uytterhoeven79d23492014-01-24 09:43:52 +0100988 return 0;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900989}
990
Geert Uytterhoeven93722202014-01-24 09:43:58 +0100991static irqreturn_t rspi_irq_mux(int irq, void *_sr)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900992{
Geert Uytterhoevenc132f092013-12-24 10:49:31 +0100993 struct rspi_data *rspi = _sr;
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100994 u8 spsr;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900995 irqreturn_t ret = IRQ_NONE;
Geert Uytterhoeven97b95c12013-12-24 10:49:34 +0100996 u8 disable_irq = 0;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900997
998 rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
999 if (spsr & SPSR_SPRF)
1000 disable_irq |= SPCR_SPRIE;
1001 if (spsr & SPSR_SPTEF)
1002 disable_irq |= SPCR_SPTIE;
1003
1004 if (disable_irq) {
1005 ret = IRQ_HANDLED;
1006 rspi_disable_irq(rspi, disable_irq);
1007 wake_up(&rspi->wait);
1008 }
1009
1010 return ret;
1011}
1012
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001013static irqreturn_t rspi_irq_rx(int irq, void *_sr)
1014{
1015 struct rspi_data *rspi = _sr;
1016 u8 spsr;
1017
1018 rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
1019 if (spsr & SPSR_SPRF) {
1020 rspi_disable_irq(rspi, SPCR_SPRIE);
1021 wake_up(&rspi->wait);
1022 return IRQ_HANDLED;
1023 }
1024
1025 return 0;
1026}
1027
1028static irqreturn_t rspi_irq_tx(int irq, void *_sr)
1029{
1030 struct rspi_data *rspi = _sr;
1031 u8 spsr;
1032
1033 rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
1034 if (spsr & SPSR_SPTEF) {
1035 rspi_disable_irq(rspi, SPCR_SPTIE);
1036 wake_up(&rspi->wait);
1037 return IRQ_HANDLED;
1038 }
1039
1040 return 0;
1041}
1042
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001043static struct dma_chan *rspi_request_dma_chan(struct device *dev,
1044 enum dma_transfer_direction dir,
1045 unsigned int id,
1046 dma_addr_t port_addr)
1047{
1048 dma_cap_mask_t mask;
1049 struct dma_chan *chan;
1050 struct dma_slave_config cfg;
1051 int ret;
1052
1053 dma_cap_zero(mask);
1054 dma_cap_set(DMA_SLAVE, mask);
1055
Geert Uytterhoevene825b8d2014-08-06 14:59:02 +02001056 chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
1057 (void *)(unsigned long)id, dev,
1058 dir == DMA_MEM_TO_DEV ? "tx" : "rx");
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001059 if (!chan) {
Geert Uytterhoevene825b8d2014-08-06 14:59:02 +02001060 dev_warn(dev, "dma_request_slave_channel_compat failed\n");
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001061 return NULL;
1062 }
1063
1064 memset(&cfg, 0, sizeof(cfg));
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001065 cfg.direction = dir;
Geert Uytterhoevena30b95a72014-08-06 14:59:01 +02001066 if (dir == DMA_MEM_TO_DEV) {
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001067 cfg.dst_addr = port_addr;
Geert Uytterhoevena30b95a72014-08-06 14:59:01 +02001068 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1069 } else {
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001070 cfg.src_addr = port_addr;
Geert Uytterhoevena30b95a72014-08-06 14:59:01 +02001071 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1072 }
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001073
1074 ret = dmaengine_slave_config(chan, &cfg);
1075 if (ret) {
1076 dev_warn(dev, "dmaengine_slave_config failed %d\n", ret);
1077 dma_release_channel(chan);
1078 return NULL;
1079 }
1080
1081 return chan;
1082}
1083
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +02001084static int rspi_request_dma(struct device *dev, struct spi_master *master,
Geert Uytterhoevenfcdc49a2014-06-02 15:38:10 +02001085 const struct resource *res)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001086{
Geert Uytterhoevenfcdc49a2014-06-02 15:38:10 +02001087 const struct rspi_plat_data *rspi_pd = dev_get_platdata(dev);
Geert Uytterhoevene825b8d2014-08-06 14:59:02 +02001088 unsigned int dma_tx_id, dma_rx_id;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001089
Geert Uytterhoevene825b8d2014-08-06 14:59:02 +02001090 if (dev->of_node) {
1091 /* In the OF case we will get the slave IDs from the DT */
1092 dma_tx_id = 0;
1093 dma_rx_id = 0;
1094 } else if (rspi_pd && rspi_pd->dma_tx_id && rspi_pd->dma_rx_id) {
1095 dma_tx_id = rspi_pd->dma_tx_id;
1096 dma_rx_id = rspi_pd->dma_rx_id;
1097 } else {
1098 /* The driver assumes no error. */
1099 return 0;
1100 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001101
Geert Uytterhoevene825b8d2014-08-06 14:59:02 +02001102 master->dma_tx = rspi_request_dma_chan(dev, DMA_MEM_TO_DEV, dma_tx_id,
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +02001103 res->start + RSPI_SPDR);
Geert Uytterhoevene825b8d2014-08-06 14:59:02 +02001104 if (!master->dma_tx)
Geert Uytterhoeven5f338d02014-06-02 15:38:11 +02001105 return -ENODEV;
Geert Uytterhoeven65bf2202014-06-02 15:38:09 +02001106
Geert Uytterhoevene825b8d2014-08-06 14:59:02 +02001107 master->dma_rx = rspi_request_dma_chan(dev, DMA_DEV_TO_MEM, dma_rx_id,
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +02001108 res->start + RSPI_SPDR);
Geert Uytterhoevene825b8d2014-08-06 14:59:02 +02001109 if (!master->dma_rx) {
1110 dma_release_channel(master->dma_tx);
1111 master->dma_tx = NULL;
Geert Uytterhoeven5f338d02014-06-02 15:38:11 +02001112 return -ENODEV;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001113 }
Shimoda, Yoshihiro0243c5362012-08-02 17:17:33 +09001114
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +02001115 master->can_dma = rspi_can_dma;
Geert Uytterhoeven5f338d02014-06-02 15:38:11 +02001116 dev_info(dev, "DMA available");
Shimoda, Yoshihiro0243c5362012-08-02 17:17:33 +09001117 return 0;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001118}
1119
Geert Uytterhoevenafcc98d2014-06-06 13:38:43 +02001120static void rspi_release_dma(struct spi_master *master)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001121{
Geert Uytterhoevenafcc98d2014-06-06 13:38:43 +02001122 if (master->dma_tx)
1123 dma_release_channel(master->dma_tx);
1124 if (master->dma_rx)
1125 dma_release_channel(master->dma_rx);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001126}
1127
Grant Likelyfd4a3192012-12-07 16:57:14 +00001128static int rspi_remove(struct platform_device *pdev)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001129{
Laurent Pinchart5ffbe2d2013-11-27 01:41:45 +01001130 struct rspi_data *rspi = platform_get_drvdata(pdev);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001131
Geert Uytterhoevenafcc98d2014-06-06 13:38:43 +02001132 rspi_release_dma(rspi->master);
Geert Uytterhoeven490c9772014-03-11 10:59:12 +01001133 pm_runtime_disable(&pdev->dev);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001134
1135 return 0;
1136}
1137
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001138static const struct spi_ops rspi_ops = {
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +02001139 .set_config_register = rspi_set_config_register,
1140 .transfer_one = rspi_transfer_one,
1141 .mode_bits = SPI_CPHA | SPI_CPOL | SPI_LOOP,
1142 .flags = SPI_MASTER_MUST_TX,
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +02001143 .fifo_size = 8,
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001144};
1145
1146static const struct spi_ops rspi_rz_ops = {
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +02001147 .set_config_register = rspi_rz_set_config_register,
1148 .transfer_one = rspi_rz_transfer_one,
1149 .mode_bits = SPI_CPHA | SPI_CPOL | SPI_LOOP,
1150 .flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX,
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +02001151 .fifo_size = 8, /* 8 for TX, 32 for RX */
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001152};
1153
1154static const struct spi_ops qspi_ops = {
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +02001155 .set_config_register = qspi_set_config_register,
1156 .transfer_one = qspi_transfer_one,
1157 .mode_bits = SPI_CPHA | SPI_CPOL | SPI_LOOP |
1158 SPI_TX_DUAL | SPI_TX_QUAD |
1159 SPI_RX_DUAL | SPI_RX_QUAD,
1160 .flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX,
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +02001161 .fifo_size = 32,
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001162};
1163
1164#ifdef CONFIG_OF
1165static const struct of_device_id rspi_of_match[] = {
1166 /* RSPI on legacy SH */
1167 { .compatible = "renesas,rspi", .data = &rspi_ops },
1168 /* RSPI on RZ/A1H */
1169 { .compatible = "renesas,rspi-rz", .data = &rspi_rz_ops },
1170 /* QSPI on R-Car Gen2 */
1171 { .compatible = "renesas,qspi", .data = &qspi_ops },
1172 { /* sentinel */ }
1173};
1174
1175MODULE_DEVICE_TABLE(of, rspi_of_match);
1176
1177static int rspi_parse_dt(struct device *dev, struct spi_master *master)
1178{
1179 u32 num_cs;
1180 int error;
1181
1182 /* Parse DT properties */
1183 error = of_property_read_u32(dev->of_node, "num-cs", &num_cs);
1184 if (error) {
1185 dev_err(dev, "of_property_read_u32 num-cs failed %d\n", error);
1186 return error;
1187 }
1188
1189 master->num_chipselect = num_cs;
1190 return 0;
1191}
1192#else
Shimoda, Yoshihiro64b67de2014-02-03 10:43:46 +09001193#define rspi_of_match NULL
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001194static inline int rspi_parse_dt(struct device *dev, struct spi_master *master)
1195{
1196 return -EINVAL;
1197}
1198#endif /* CONFIG_OF */
1199
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001200static int rspi_request_irq(struct device *dev, unsigned int irq,
1201 irq_handler_t handler, const char *suffix,
1202 void *dev_id)
1203{
Geert Uytterhoeven43937452014-08-06 14:59:00 +02001204 const char *name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s",
1205 dev_name(dev), suffix);
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001206 if (!name)
1207 return -ENOMEM;
Geert Uytterhoeven43937452014-08-06 14:59:00 +02001208
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001209 return devm_request_irq(dev, irq, handler, 0, name, dev_id);
1210}
1211
Grant Likelyfd4a3192012-12-07 16:57:14 +00001212static int rspi_probe(struct platform_device *pdev)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001213{
1214 struct resource *res;
1215 struct spi_master *master;
1216 struct rspi_data *rspi;
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001217 int ret;
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001218 const struct rspi_plat_data *rspi_pd;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001219 const struct spi_ops *ops;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001220
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001221 master = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data));
Geert Uytterhoevenffcfae32017-01-04 11:15:07 +01001222 if (master == NULL)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001223 return -ENOMEM;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001224
Geert Uytterhoeven219a7bc2017-10-04 14:19:53 +02001225 ops = of_device_get_match_data(&pdev->dev);
1226 if (ops) {
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001227 ret = rspi_parse_dt(&pdev->dev, master);
1228 if (ret)
1229 goto error1;
1230 } else {
1231 ops = (struct spi_ops *)pdev->id_entry->driver_data;
1232 rspi_pd = dev_get_platdata(&pdev->dev);
1233 if (rspi_pd && rspi_pd->num_chipselect)
1234 master->num_chipselect = rspi_pd->num_chipselect;
1235 else
1236 master->num_chipselect = 2; /* default */
Geert Uytterhoevend64b4722014-08-06 14:58:59 +02001237 }
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001238
1239 /* ops parameter check */
1240 if (!ops->set_config_register) {
1241 dev_err(&pdev->dev, "there is no set_config_register\n");
1242 ret = -ENODEV;
1243 goto error1;
1244 }
1245
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001246 rspi = spi_master_get_devdata(master);
Jingoo Han24b5a822013-05-23 19:20:40 +09001247 platform_set_drvdata(pdev, rspi);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001248 rspi->ops = ops;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001249 rspi->master = master;
Laurent Pinchart5d79e9a2013-11-27 01:41:46 +01001250
1251 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1252 rspi->addr = devm_ioremap_resource(&pdev->dev, res);
1253 if (IS_ERR(rspi->addr)) {
1254 ret = PTR_ERR(rspi->addr);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001255 goto error1;
1256 }
1257
Geert Uytterhoeven29f397b2014-01-24 09:44:02 +01001258 rspi->clk = devm_clk_get(&pdev->dev, NULL);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001259 if (IS_ERR(rspi->clk)) {
1260 dev_err(&pdev->dev, "cannot get clock\n");
1261 ret = PTR_ERR(rspi->clk);
Laurent Pinchart5d79e9a2013-11-27 01:41:46 +01001262 goto error1;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001263 }
Geert Uytterhoeven17fe0d92014-01-24 09:44:01 +01001264
Geert Uytterhoeven490c9772014-03-11 10:59:12 +01001265 pm_runtime_enable(&pdev->dev);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001266
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001267 init_waitqueue_head(&rspi->wait);
1268
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001269 master->bus_num = pdev->id;
1270 master->setup = rspi_setup;
Geert Uytterhoeven490c9772014-03-11 10:59:12 +01001271 master->auto_runtime_pm = true;
Geert Uytterhoeveneb557f72014-01-24 09:43:55 +01001272 master->transfer_one = ops->transfer_one;
Geert Uytterhoeven79d23492014-01-24 09:43:52 +01001273 master->prepare_message = rspi_prepare_message;
1274 master->unprepare_message = rspi_unprepare_message;
Geert Uytterhoeven880c6d12014-01-30 09:43:50 +01001275 master->mode_bits = ops->mode_bits;
Geert Uytterhoevenb42e0352014-06-02 15:38:06 +02001276 master->flags = ops->flags;
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001277 master->dev.of_node = pdev->dev.of_node;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001278
Geert Uytterhoeven93722202014-01-24 09:43:58 +01001279 ret = platform_get_irq_byname(pdev, "rx");
1280 if (ret < 0) {
1281 ret = platform_get_irq_byname(pdev, "mux");
1282 if (ret < 0)
1283 ret = platform_get_irq(pdev, 0);
1284 if (ret >= 0)
1285 rspi->rx_irq = rspi->tx_irq = ret;
1286 } else {
1287 rspi->rx_irq = ret;
1288 ret = platform_get_irq_byname(pdev, "tx");
1289 if (ret >= 0)
1290 rspi->tx_irq = ret;
1291 }
1292 if (ret < 0) {
1293 dev_err(&pdev->dev, "platform_get_irq error\n");
1294 goto error2;
1295 }
1296
1297 if (rspi->rx_irq == rspi->tx_irq) {
1298 /* Single multiplexed interrupt */
1299 ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_mux,
1300 "mux", rspi);
1301 } else {
1302 /* Multi-interrupt mode, only SPRI and SPTI are used */
1303 ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_rx,
1304 "rx", rspi);
1305 if (!ret)
1306 ret = rspi_request_irq(&pdev->dev, rspi->tx_irq,
1307 rspi_irq_tx, "tx", rspi);
1308 }
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001309 if (ret < 0) {
1310 dev_err(&pdev->dev, "request_irq error\n");
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +01001311 goto error2;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001312 }
1313
Geert Uytterhoeven2f777ec2014-06-02 15:38:12 +02001314 ret = rspi_request_dma(&pdev->dev, master, res);
Geert Uytterhoeven27e105a2014-06-02 15:38:08 +02001315 if (ret < 0)
1316 dev_warn(&pdev->dev, "DMA not available, using PIO\n");
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +09001317
Jingoo Han9e03d052013-12-04 14:13:50 +09001318 ret = devm_spi_register_master(&pdev->dev, master);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001319 if (ret < 0) {
1320 dev_err(&pdev->dev, "spi_register_master error.\n");
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +01001321 goto error3;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001322 }
1323
1324 dev_info(&pdev->dev, "probed\n");
1325
1326 return 0;
1327
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +01001328error3:
Geert Uytterhoevenafcc98d2014-06-06 13:38:43 +02001329 rspi_release_dma(master);
Geert Uytterhoevenfcb4ed72014-01-14 10:20:33 +01001330error2:
Geert Uytterhoeven490c9772014-03-11 10:59:12 +01001331 pm_runtime_disable(&pdev->dev);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001332error1:
1333 spi_master_put(master);
1334
1335 return ret;
1336}
1337
Krzysztof Kozlowski8634daf2015-05-02 00:44:05 +09001338static const struct platform_device_id spi_driver_ids[] = {
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001339 { "rspi", (kernel_ulong_t)&rspi_ops },
Geert Uytterhoeven862d3572014-01-24 09:43:59 +01001340 { "rspi-rz", (kernel_ulong_t)&rspi_rz_ops },
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001341 { "qspi", (kernel_ulong_t)&qspi_ops },
1342 {},
1343};
1344
1345MODULE_DEVICE_TABLE(platform, spi_driver_ids);
1346
Geert Uytterhoevenc1ca59c2018-09-05 10:49:38 +02001347#ifdef CONFIG_PM_SLEEP
1348static int rspi_suspend(struct device *dev)
1349{
Wolfram Sangbe0bf622018-10-21 22:00:45 +02001350 struct rspi_data *rspi = dev_get_drvdata(dev);
Geert Uytterhoevenc1ca59c2018-09-05 10:49:38 +02001351
1352 return spi_master_suspend(rspi->master);
1353}
1354
1355static int rspi_resume(struct device *dev)
1356{
Wolfram Sangbe0bf622018-10-21 22:00:45 +02001357 struct rspi_data *rspi = dev_get_drvdata(dev);
Geert Uytterhoevenc1ca59c2018-09-05 10:49:38 +02001358
1359 return spi_master_resume(rspi->master);
1360}
1361
1362static SIMPLE_DEV_PM_OPS(rspi_pm_ops, rspi_suspend, rspi_resume);
1363#define DEV_PM_OPS &rspi_pm_ops
1364#else
1365#define DEV_PM_OPS NULL
1366#endif /* CONFIG_PM_SLEEP */
1367
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001368static struct platform_driver rspi_driver = {
1369 .probe = rspi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001370 .remove = rspi_remove,
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001371 .id_table = spi_driver_ids,
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001372 .driver = {
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +09001373 .name = "renesas_spi",
Geert Uytterhoevenc1ca59c2018-09-05 10:49:38 +02001374 .pm = DEV_PM_OPS,
Geert Uytterhoeven426ef762014-01-28 10:21:38 +01001375 .of_match_table = of_match_ptr(rspi_of_match),
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001376 },
1377};
1378module_platform_driver(rspi_driver);
1379
1380MODULE_DESCRIPTION("Renesas RSPI bus driver");
1381MODULE_LICENSE("GPL v2");
1382MODULE_AUTHOR("Yoshihiro Shimoda");
1383MODULE_ALIAS("platform:rspi");