Commit | Line | Data |
---|---|---|
0b2182dd SY |
1 | /* |
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> | |
0b2182dd SY |
28 | #include <linux/interrupt.h> |
29 | #include <linux/platform_device.h> | |
30 | #include <linux/io.h> | |
31 | #include <linux/clk.h> | |
a3633fe7 SY |
32 | #include <linux/dmaengine.h> |
33 | #include <linux/dma-mapping.h> | |
34 | #include <linux/sh_dma.h> | |
0b2182dd | 35 | #include <linux/spi/spi.h> |
a3633fe7 | 36 | #include <linux/spi/rspi.h> |
0b2182dd | 37 | |
6ab4865b GU |
38 | #define RSPI_SPCR 0x00 /* Control Register */ |
39 | #define RSPI_SSLP 0x01 /* Slave Select Polarity Register */ | |
40 | #define RSPI_SPPCR 0x02 /* Pin Control Register */ | |
41 | #define RSPI_SPSR 0x03 /* Status Register */ | |
42 | #define RSPI_SPDR 0x04 /* Data Register */ | |
43 | #define RSPI_SPSCR 0x08 /* Sequence Control Register */ | |
44 | #define RSPI_SPSSR 0x09 /* Sequence Status Register */ | |
45 | #define RSPI_SPBR 0x0a /* Bit Rate Register */ | |
46 | #define RSPI_SPDCR 0x0b /* Data Control Register */ | |
47 | #define RSPI_SPCKD 0x0c /* Clock Delay Register */ | |
48 | #define RSPI_SSLND 0x0d /* Slave Select Negation Delay Register */ | |
49 | #define RSPI_SPND 0x0e /* Next-Access Delay Register */ | |
50 | #define RSPI_SPCR2 0x0f /* Control Register 2 */ | |
51 | #define RSPI_SPCMD0 0x10 /* Command Register 0 */ | |
52 | #define RSPI_SPCMD1 0x12 /* Command Register 1 */ | |
53 | #define RSPI_SPCMD2 0x14 /* Command Register 2 */ | |
54 | #define RSPI_SPCMD3 0x16 /* Command Register 3 */ | |
55 | #define RSPI_SPCMD4 0x18 /* Command Register 4 */ | |
56 | #define RSPI_SPCMD5 0x1a /* Command Register 5 */ | |
57 | #define RSPI_SPCMD6 0x1c /* Command Register 6 */ | |
58 | #define RSPI_SPCMD7 0x1e /* Command Register 7 */ | |
59 | #define RSPI_SPBFCR 0x20 /* Buffer Control Register */ | |
60 | #define RSPI_SPBFDR 0x22 /* Buffer Data Count Setting Register */ | |
0b2182dd | 61 | |
5ce0ba88 | 62 | /*qspi only */ |
fbe5072b GU |
63 | #define QSPI_SPBFCR 0x18 /* Buffer Control Register */ |
64 | #define QSPI_SPBDCR 0x1a /* Buffer Data Count Register */ | |
65 | #define QSPI_SPBMUL0 0x1c /* Transfer Data Length Multiplier Setting Register 0 */ | |
66 | #define QSPI_SPBMUL1 0x20 /* Transfer Data Length Multiplier Setting Register 1 */ | |
67 | #define QSPI_SPBMUL2 0x24 /* Transfer Data Length Multiplier Setting Register 2 */ | |
68 | #define QSPI_SPBMUL3 0x28 /* Transfer Data Length Multiplier Setting Register 3 */ | |
5ce0ba88 | 69 | |
6ab4865b GU |
70 | /* SPCR - Control Register */ |
71 | #define SPCR_SPRIE 0x80 /* Receive Interrupt Enable */ | |
72 | #define SPCR_SPE 0x40 /* Function Enable */ | |
73 | #define SPCR_SPTIE 0x20 /* Transmit Interrupt Enable */ | |
74 | #define SPCR_SPEIE 0x10 /* Error Interrupt Enable */ | |
75 | #define SPCR_MSTR 0x08 /* Master/Slave Mode Select */ | |
76 | #define SPCR_MODFEN 0x04 /* Mode Fault Error Detection Enable */ | |
77 | /* RSPI on SH only */ | |
78 | #define SPCR_TXMD 0x02 /* TX Only Mode (vs. Full Duplex) */ | |
79 | #define SPCR_SPMS 0x01 /* 3-wire Mode (vs. 4-wire) */ | |
fbe5072b GU |
80 | /* QSPI on R-Car M2 only */ |
81 | #define SPCR_WSWAP 0x02 /* Word Swap of read-data for DMAC */ | |
82 | #define SPCR_BSWAP 0x01 /* Byte Swap of read-data for DMAC */ | |
6ab4865b GU |
83 | |
84 | /* SSLP - Slave Select Polarity Register */ | |
85 | #define SSLP_SSL1P 0x02 /* SSL1 Signal Polarity Setting */ | |
86 | #define SSLP_SSL0P 0x01 /* SSL0 Signal Polarity Setting */ | |
87 | ||
88 | /* SPPCR - Pin Control Register */ | |
89 | #define SPPCR_MOIFE 0x20 /* MOSI Idle Value Fixing Enable */ | |
90 | #define SPPCR_MOIFV 0x10 /* MOSI Idle Fixed Value */ | |
0b2182dd | 91 | #define SPPCR_SPOM 0x04 |
6ab4865b GU |
92 | #define SPPCR_SPLP2 0x02 /* Loopback Mode 2 (non-inverting) */ |
93 | #define SPPCR_SPLP 0x01 /* Loopback Mode (inverting) */ | |
94 | ||
fbe5072b GU |
95 | #define SPPCR_IO3FV 0x04 /* Single-/Dual-SPI Mode IO3 Output Fixed Value */ |
96 | #define SPPCR_IO2FV 0x04 /* Single-/Dual-SPI Mode IO2 Output Fixed Value */ | |
97 | ||
6ab4865b GU |
98 | /* SPSR - Status Register */ |
99 | #define SPSR_SPRF 0x80 /* Receive Buffer Full Flag */ | |
100 | #define SPSR_TEND 0x40 /* Transmit End */ | |
101 | #define SPSR_SPTEF 0x20 /* Transmit Buffer Empty Flag */ | |
102 | #define SPSR_PERF 0x08 /* Parity Error Flag */ | |
103 | #define SPSR_MODF 0x04 /* Mode Fault Error Flag */ | |
104 | #define SPSR_IDLNF 0x02 /* RSPI Idle Flag */ | |
105 | #define SPSR_OVRF 0x01 /* Overrun Error Flag */ | |
106 | ||
107 | /* SPSCR - Sequence Control Register */ | |
108 | #define SPSCR_SPSLN_MASK 0x07 /* Sequence Length Specification */ | |
109 | ||
110 | /* SPSSR - Sequence Status Register */ | |
111 | #define SPSSR_SPECM_MASK 0x70 /* Command Error Mask */ | |
112 | #define SPSSR_SPCP_MASK 0x07 /* Command Pointer Mask */ | |
113 | ||
114 | /* SPDCR - Data Control Register */ | |
115 | #define SPDCR_TXDMY 0x80 /* Dummy Data Transmission Enable */ | |
116 | #define SPDCR_SPLW1 0x40 /* Access Width Specification (RZ) */ | |
117 | #define SPDCR_SPLW0 0x20 /* Access Width Specification (RZ) */ | |
118 | #define SPDCR_SPLLWORD (SPDCR_SPLW1 | SPDCR_SPLW0) | |
119 | #define SPDCR_SPLWORD SPDCR_SPLW1 | |
120 | #define SPDCR_SPLBYTE SPDCR_SPLW0 | |
121 | #define SPDCR_SPLW 0x20 /* Access Width Specification (SH) */ | |
122 | #define SPDCR_SPRDTD 0x10 /* Receive Transmit Data Select */ | |
0b2182dd SY |
123 | #define SPDCR_SLSEL1 0x08 |
124 | #define SPDCR_SLSEL0 0x04 | |
6ab4865b | 125 | #define SPDCR_SLSEL_MASK 0x0c /* SSL1 Output Select */ |
0b2182dd SY |
126 | #define SPDCR_SPFC1 0x02 |
127 | #define SPDCR_SPFC0 0x01 | |
6ab4865b | 128 | #define SPDCR_SPFC_MASK 0x03 /* Frame Count Setting (1-4) */ |
0b2182dd | 129 | |
6ab4865b GU |
130 | /* SPCKD - Clock Delay Register */ |
131 | #define SPCKD_SCKDL_MASK 0x07 /* Clock Delay Setting (1-8) */ | |
0b2182dd | 132 | |
6ab4865b GU |
133 | /* SSLND - Slave Select Negation Delay Register */ |
134 | #define SSLND_SLNDL_MASK 0x07 /* SSL Negation Delay Setting (1-8) */ | |
0b2182dd | 135 | |
6ab4865b GU |
136 | /* SPND - Next-Access Delay Register */ |
137 | #define SPND_SPNDL_MASK 0x07 /* Next-Access Delay Setting (1-8) */ | |
0b2182dd | 138 | |
6ab4865b GU |
139 | /* SPCR2 - Control Register 2 */ |
140 | #define SPCR2_PTE 0x08 /* Parity Self-Test Enable */ | |
141 | #define SPCR2_SPIE 0x04 /* Idle Interrupt Enable */ | |
142 | #define SPCR2_SPOE 0x02 /* Odd Parity Enable (vs. Even) */ | |
143 | #define SPCR2_SPPE 0x01 /* Parity Enable */ | |
0b2182dd | 144 | |
6ab4865b GU |
145 | /* SPCMDn - Command Registers */ |
146 | #define SPCMD_SCKDEN 0x8000 /* Clock Delay Setting Enable */ | |
147 | #define SPCMD_SLNDEN 0x4000 /* SSL Negation Delay Setting Enable */ | |
148 | #define SPCMD_SPNDEN 0x2000 /* Next-Access Delay Enable */ | |
149 | #define SPCMD_LSBF 0x1000 /* LSB First */ | |
150 | #define SPCMD_SPB_MASK 0x0f00 /* Data Length Setting */ | |
0b2182dd | 151 | #define SPCMD_SPB_8_TO_16(bit) (((bit - 1) << 8) & SPCMD_SPB_MASK) |
5ce0ba88 HCM |
152 | #define SPCMD_SPB_8BIT 0x0000 /* qspi only */ |
153 | #define SPCMD_SPB_16BIT 0x0100 | |
0b2182dd SY |
154 | #define SPCMD_SPB_20BIT 0x0000 |
155 | #define SPCMD_SPB_24BIT 0x0100 | |
156 | #define SPCMD_SPB_32BIT 0x0200 | |
6ab4865b | 157 | #define SPCMD_SSLKP 0x0080 /* SSL Signal Level Keeping */ |
fbe5072b GU |
158 | #define SPCMD_SPIMOD_MASK 0x0060 /* SPI Operating Mode (QSPI only) */ |
159 | #define SPCMD_SPIMOD1 0x0040 | |
160 | #define SPCMD_SPIMOD0 0x0020 | |
161 | #define SPCMD_SPIMOD_SINGLE 0 | |
162 | #define SPCMD_SPIMOD_DUAL SPCMD_SPIMOD0 | |
163 | #define SPCMD_SPIMOD_QUAD SPCMD_SPIMOD1 | |
164 | #define SPCMD_SPRW 0x0010 /* SPI Read/Write Access (Dual/Quad) */ | |
6ab4865b GU |
165 | #define SPCMD_SSLA_MASK 0x0030 /* SSL Assert Signal Setting (RSPI) */ |
166 | #define SPCMD_BRDV_MASK 0x000c /* Bit Rate Division Setting */ | |
167 | #define SPCMD_CPOL 0x0002 /* Clock Polarity Setting */ | |
168 | #define SPCMD_CPHA 0x0001 /* Clock Phase Setting */ | |
169 | ||
170 | /* SPBFCR - Buffer Control Register */ | |
171 | #define SPBFCR_TXRST 0x80 /* Transmit Buffer Data Reset (qspi only) */ | |
172 | #define SPBFCR_RXRST 0x40 /* Receive Buffer Data Reset (qspi only) */ | |
173 | #define SPBFCR_TXTRG_MASK 0x30 /* Transmit Buffer Data Triggering Number */ | |
174 | #define SPBFCR_RXTRG_MASK 0x07 /* Receive Buffer Data Triggering Number */ | |
5ce0ba88 | 175 | |
2aae80b2 GU |
176 | #define DUMMY_DATA 0x00 |
177 | ||
0b2182dd SY |
178 | struct rspi_data { |
179 | void __iomem *addr; | |
180 | u32 max_speed_hz; | |
181 | struct spi_master *master; | |
0b2182dd | 182 | wait_queue_head_t wait; |
0b2182dd | 183 | struct clk *clk; |
97b95c11 | 184 | u8 spsr; |
348e5153 | 185 | u16 spcmd; |
5ce0ba88 | 186 | const struct spi_ops *ops; |
a3633fe7 SY |
187 | |
188 | /* for dmaengine */ | |
a3633fe7 SY |
189 | struct dma_chan *chan_tx; |
190 | struct dma_chan *chan_rx; | |
191 | int irq; | |
192 | ||
193 | unsigned dma_width_16bit:1; | |
194 | unsigned dma_callbacked:1; | |
74da7686 | 195 | unsigned byte_access:1; |
0b2182dd SY |
196 | }; |
197 | ||
baf588f4 | 198 | static void rspi_write8(const struct rspi_data *rspi, u8 data, u16 offset) |
0b2182dd SY |
199 | { |
200 | iowrite8(data, rspi->addr + offset); | |
201 | } | |
202 | ||
baf588f4 | 203 | static void rspi_write16(const struct rspi_data *rspi, u16 data, u16 offset) |
0b2182dd SY |
204 | { |
205 | iowrite16(data, rspi->addr + offset); | |
206 | } | |
207 | ||
baf588f4 | 208 | static void rspi_write32(const struct rspi_data *rspi, u32 data, u16 offset) |
5ce0ba88 HCM |
209 | { |
210 | iowrite32(data, rspi->addr + offset); | |
211 | } | |
212 | ||
baf588f4 | 213 | static u8 rspi_read8(const struct rspi_data *rspi, u16 offset) |
0b2182dd SY |
214 | { |
215 | return ioread8(rspi->addr + offset); | |
216 | } | |
217 | ||
baf588f4 | 218 | static u16 rspi_read16(const struct rspi_data *rspi, u16 offset) |
0b2182dd SY |
219 | { |
220 | return ioread16(rspi->addr + offset); | |
221 | } | |
222 | ||
74da7686 GU |
223 | static void rspi_write_data(const struct rspi_data *rspi, u16 data) |
224 | { | |
225 | if (rspi->byte_access) | |
226 | rspi_write8(rspi, data, RSPI_SPDR); | |
227 | else /* 16 bit */ | |
228 | rspi_write16(rspi, data, RSPI_SPDR); | |
229 | } | |
230 | ||
231 | static u16 rspi_read_data(const struct rspi_data *rspi) | |
232 | { | |
233 | if (rspi->byte_access) | |
234 | return rspi_read8(rspi, RSPI_SPDR); | |
235 | else /* 16 bit */ | |
236 | return rspi_read16(rspi, RSPI_SPDR); | |
237 | } | |
238 | ||
5ce0ba88 HCM |
239 | /* optional functions */ |
240 | struct spi_ops { | |
74da7686 | 241 | int (*set_config_register)(struct rspi_data *rspi, int access_size); |
91949a2d GU |
242 | int (*send_pio)(struct rspi_data *rspi, struct spi_transfer *t); |
243 | int (*receive_pio)(struct rspi_data *rspi, struct spi_transfer *t); | |
5ce0ba88 HCM |
244 | }; |
245 | ||
246 | /* | |
247 | * functions for RSPI | |
248 | */ | |
74da7686 | 249 | static int rspi_set_config_register(struct rspi_data *rspi, int access_size) |
0b2182dd | 250 | { |
5ce0ba88 HCM |
251 | int spbr; |
252 | ||
253 | /* Sets output mode(CMOS) and MOSI signal(from previous transfer) */ | |
254 | rspi_write8(rspi, 0x00, RSPI_SPPCR); | |
0b2182dd | 255 | |
5ce0ba88 HCM |
256 | /* Sets transfer bit rate */ |
257 | spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz) - 1; | |
258 | rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR); | |
259 | ||
74da7686 GU |
260 | /* Disable dummy transmission, set 16-bit word access, 1 frame */ |
261 | rspi_write8(rspi, 0, RSPI_SPDCR); | |
262 | rspi->byte_access = 0; | |
0b2182dd | 263 | |
5ce0ba88 HCM |
264 | /* Sets RSPCK, SSL, next-access delay value */ |
265 | rspi_write8(rspi, 0x00, RSPI_SPCKD); | |
266 | rspi_write8(rspi, 0x00, RSPI_SSLND); | |
267 | rspi_write8(rspi, 0x00, RSPI_SPND); | |
268 | ||
269 | /* Sets parity, interrupt mask */ | |
270 | rspi_write8(rspi, 0x00, RSPI_SPCR2); | |
271 | ||
272 | /* Sets SPCMD */ | |
348e5153 | 273 | rspi_write16(rspi, SPCMD_SPB_8_TO_16(access_size) | rspi->spcmd, |
5ce0ba88 HCM |
274 | RSPI_SPCMD0); |
275 | ||
276 | /* Sets RSPI mode */ | |
277 | rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR); | |
278 | ||
279 | return 0; | |
0b2182dd SY |
280 | } |
281 | ||
5ce0ba88 HCM |
282 | /* |
283 | * functions for QSPI | |
284 | */ | |
74da7686 | 285 | static int qspi_set_config_register(struct rspi_data *rspi, int access_size) |
5ce0ba88 HCM |
286 | { |
287 | u16 spcmd; | |
288 | int spbr; | |
289 | ||
290 | /* Sets output mode(CMOS) and MOSI signal(from previous transfer) */ | |
291 | rspi_write8(rspi, 0x00, RSPI_SPPCR); | |
292 | ||
293 | /* Sets transfer bit rate */ | |
294 | spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz); | |
295 | rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR); | |
296 | ||
74da7686 GU |
297 | /* Disable dummy transmission, set byte access */ |
298 | rspi_write8(rspi, 0, RSPI_SPDCR); | |
299 | rspi->byte_access = 1; | |
5ce0ba88 HCM |
300 | |
301 | /* Sets RSPCK, SSL, next-access delay value */ | |
302 | rspi_write8(rspi, 0x00, RSPI_SPCKD); | |
303 | rspi_write8(rspi, 0x00, RSPI_SSLND); | |
304 | rspi_write8(rspi, 0x00, RSPI_SPND); | |
305 | ||
306 | /* Data Length Setting */ | |
307 | if (access_size == 8) | |
308 | spcmd = SPCMD_SPB_8BIT; | |
309 | else if (access_size == 16) | |
310 | spcmd = SPCMD_SPB_16BIT; | |
8e1c8096 | 311 | else |
5ce0ba88 HCM |
312 | spcmd = SPCMD_SPB_32BIT; |
313 | ||
348e5153 | 314 | spcmd |= SPCMD_SCKDEN | SPCMD_SLNDEN | rspi->spcmd | SPCMD_SPNDEN; |
5ce0ba88 HCM |
315 | |
316 | /* Resets transfer data length */ | |
317 | rspi_write32(rspi, 0, QSPI_SPBMUL0); | |
318 | ||
319 | /* Resets transmit and receive buffer */ | |
320 | rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR); | |
321 | /* Sets buffer to allow normal operation */ | |
322 | rspi_write8(rspi, 0x00, QSPI_SPBFCR); | |
323 | ||
324 | /* Sets SPCMD */ | |
325 | rspi_write16(rspi, spcmd, RSPI_SPCMD0); | |
326 | ||
327 | /* Enables SPI function in a master mode */ | |
328 | rspi_write8(rspi, SPCR_SPE | SPCR_MSTR, RSPI_SPCR); | |
329 | ||
330 | return 0; | |
331 | } | |
332 | ||
333 | #define set_config_register(spi, n) spi->ops->set_config_register(spi, n) | |
334 | ||
baf588f4 | 335 | static void rspi_enable_irq(const struct rspi_data *rspi, u8 enable) |
0b2182dd SY |
336 | { |
337 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | enable, RSPI_SPCR); | |
338 | } | |
339 | ||
baf588f4 | 340 | static void rspi_disable_irq(const struct rspi_data *rspi, u8 disable) |
0b2182dd SY |
341 | { |
342 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~disable, RSPI_SPCR); | |
343 | } | |
344 | ||
345 | static int rspi_wait_for_interrupt(struct rspi_data *rspi, u8 wait_mask, | |
346 | u8 enable_bit) | |
347 | { | |
348 | int ret; | |
349 | ||
350 | rspi->spsr = rspi_read8(rspi, RSPI_SPSR); | |
351 | rspi_enable_irq(rspi, enable_bit); | |
352 | ret = wait_event_timeout(rspi->wait, rspi->spsr & wait_mask, HZ); | |
353 | if (ret == 0 && !(rspi->spsr & wait_mask)) | |
354 | return -ETIMEDOUT; | |
355 | ||
356 | return 0; | |
357 | } | |
358 | ||
35301c99 GU |
359 | static int rspi_data_out(struct rspi_data *rspi, u8 data) |
360 | { | |
361 | if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) { | |
362 | dev_err(&rspi->master->dev, "transmit timeout\n"); | |
363 | return -ETIMEDOUT; | |
364 | } | |
365 | rspi_write_data(rspi, data); | |
366 | return 0; | |
367 | } | |
368 | ||
369 | static int rspi_data_in(struct rspi_data *rspi) | |
370 | { | |
371 | u8 data; | |
372 | ||
373 | if (rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE) < 0) { | |
374 | dev_err(&rspi->master->dev, "receive timeout\n"); | |
375 | return -ETIMEDOUT; | |
376 | } | |
377 | data = rspi_read_data(rspi); | |
378 | return data; | |
379 | } | |
380 | ||
381 | static int rspi_data_out_in(struct rspi_data *rspi, u8 data) | |
382 | { | |
383 | int ret; | |
384 | ||
385 | ret = rspi_data_out(rspi, data); | |
386 | if (ret < 0) | |
387 | return ret; | |
388 | ||
389 | return rspi_data_in(rspi); | |
390 | } | |
391 | ||
91949a2d | 392 | static int rspi_send_pio(struct rspi_data *rspi, struct spi_transfer *t) |
0b2182dd | 393 | { |
35301c99 | 394 | int remain = t->len, ret; |
c132f094 | 395 | const u8 *data = t->tx_buf; |
35301c99 | 396 | |
0b2182dd SY |
397 | while (remain > 0) { |
398 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_TXMD, | |
399 | RSPI_SPCR); | |
400 | ||
35301c99 GU |
401 | ret = rspi_data_out(rspi, *data++); |
402 | if (ret < 0) | |
403 | return ret; | |
0b2182dd SY |
404 | remain--; |
405 | } | |
406 | ||
b7ed6b88 | 407 | /* Waiting for the last transmission */ |
0b2182dd SY |
408 | rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE); |
409 | ||
410 | return 0; | |
411 | } | |
412 | ||
91949a2d | 413 | static int qspi_send_pio(struct rspi_data *rspi, struct spi_transfer *t) |
cb52c673 | 414 | { |
35301c99 | 415 | int remain = t->len, ret; |
c132f094 | 416 | const u8 *data = t->tx_buf; |
cb52c673 HCM |
417 | |
418 | rspi_write8(rspi, SPBFCR_TXRST, QSPI_SPBFCR); | |
419 | rspi_write8(rspi, 0x00, QSPI_SPBFCR); | |
420 | ||
cb52c673 | 421 | while (remain > 0) { |
35301c99 GU |
422 | /* dummy read */ |
423 | ret = rspi_data_out_in(rspi, *data++); | |
424 | if (ret < 0) | |
425 | return ret; | |
cb52c673 HCM |
426 | remain--; |
427 | } | |
428 | ||
b7ed6b88 | 429 | /* Waiting for the last transmission */ |
cb52c673 HCM |
430 | rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE); |
431 | ||
432 | return 0; | |
433 | } | |
434 | ||
91949a2d | 435 | #define send_pio(spi, t) spi->ops->send_pio(spi, t) |
cb52c673 | 436 | |
a3633fe7 SY |
437 | static void rspi_dma_complete(void *arg) |
438 | { | |
439 | struct rspi_data *rspi = arg; | |
440 | ||
441 | rspi->dma_callbacked = 1; | |
442 | wake_up_interruptible(&rspi->wait); | |
443 | } | |
444 | ||
c132f094 GU |
445 | static int rspi_dma_map_sg(struct scatterlist *sg, const void *buf, |
446 | unsigned len, struct dma_chan *chan, | |
a3633fe7 SY |
447 | enum dma_transfer_direction dir) |
448 | { | |
449 | sg_init_table(sg, 1); | |
450 | sg_set_buf(sg, buf, len); | |
451 | sg_dma_len(sg) = len; | |
452 | return dma_map_sg(chan->device->dev, sg, 1, dir); | |
453 | } | |
454 | ||
455 | static void rspi_dma_unmap_sg(struct scatterlist *sg, struct dma_chan *chan, | |
456 | enum dma_transfer_direction dir) | |
457 | { | |
458 | dma_unmap_sg(chan->device->dev, sg, 1, dir); | |
459 | } | |
460 | ||
461 | static void rspi_memory_to_8bit(void *buf, const void *data, unsigned len) | |
462 | { | |
463 | u16 *dst = buf; | |
464 | const u8 *src = data; | |
465 | ||
466 | while (len) { | |
467 | *dst++ = (u16)(*src++); | |
468 | len--; | |
469 | } | |
470 | } | |
471 | ||
472 | static void rspi_memory_from_8bit(void *buf, const void *data, unsigned len) | |
473 | { | |
474 | u8 *dst = buf; | |
475 | const u16 *src = data; | |
476 | ||
477 | while (len) { | |
478 | *dst++ = (u8)*src++; | |
479 | len--; | |
480 | } | |
481 | } | |
482 | ||
483 | static int rspi_send_dma(struct rspi_data *rspi, struct spi_transfer *t) | |
484 | { | |
485 | struct scatterlist sg; | |
c132f094 | 486 | const void *buf = NULL; |
a3633fe7 SY |
487 | struct dma_async_tx_descriptor *desc; |
488 | unsigned len; | |
489 | int ret = 0; | |
490 | ||
491 | if (rspi->dma_width_16bit) { | |
c132f094 | 492 | void *tmp; |
a3633fe7 SY |
493 | /* |
494 | * If DMAC bus width is 16-bit, the driver allocates a dummy | |
495 | * buffer. And, the driver converts original data into the | |
496 | * DMAC data as the following format: | |
497 | * original data: 1st byte, 2nd byte ... | |
498 | * DMAC data: 1st byte, dummy, 2nd byte, dummy ... | |
499 | */ | |
500 | len = t->len * 2; | |
c132f094 GU |
501 | tmp = kmalloc(len, GFP_KERNEL); |
502 | if (!tmp) | |
a3633fe7 | 503 | return -ENOMEM; |
c132f094 GU |
504 | rspi_memory_to_8bit(tmp, t->tx_buf, t->len); |
505 | buf = tmp; | |
a3633fe7 SY |
506 | } else { |
507 | len = t->len; | |
c132f094 | 508 | buf = t->tx_buf; |
a3633fe7 SY |
509 | } |
510 | ||
511 | if (!rspi_dma_map_sg(&sg, buf, len, rspi->chan_tx, DMA_TO_DEVICE)) { | |
512 | ret = -EFAULT; | |
513 | goto end_nomap; | |
514 | } | |
515 | desc = dmaengine_prep_slave_sg(rspi->chan_tx, &sg, 1, DMA_TO_DEVICE, | |
516 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); | |
517 | if (!desc) { | |
518 | ret = -EIO; | |
519 | goto end; | |
520 | } | |
521 | ||
522 | /* | |
523 | * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be | |
524 | * called. So, this driver disables the IRQ while DMA transfer. | |
525 | */ | |
526 | disable_irq(rspi->irq); | |
527 | ||
528 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_TXMD, RSPI_SPCR); | |
529 | rspi_enable_irq(rspi, SPCR_SPTIE); | |
530 | rspi->dma_callbacked = 0; | |
531 | ||
532 | desc->callback = rspi_dma_complete; | |
533 | desc->callback_param = rspi; | |
534 | dmaengine_submit(desc); | |
535 | dma_async_issue_pending(rspi->chan_tx); | |
536 | ||
537 | ret = wait_event_interruptible_timeout(rspi->wait, | |
538 | rspi->dma_callbacked, HZ); | |
539 | if (ret > 0 && rspi->dma_callbacked) | |
540 | ret = 0; | |
541 | else if (!ret) | |
542 | ret = -ETIMEDOUT; | |
543 | rspi_disable_irq(rspi, SPCR_SPTIE); | |
544 | ||
545 | enable_irq(rspi->irq); | |
546 | ||
547 | end: | |
548 | rspi_dma_unmap_sg(&sg, rspi->chan_tx, DMA_TO_DEVICE); | |
549 | end_nomap: | |
550 | if (rspi->dma_width_16bit) | |
551 | kfree(buf); | |
552 | ||
553 | return ret; | |
554 | } | |
555 | ||
baf588f4 | 556 | static void rspi_receive_init(const struct rspi_data *rspi) |
0b2182dd | 557 | { |
97b95c11 | 558 | u8 spsr; |
0b2182dd SY |
559 | |
560 | spsr = rspi_read8(rspi, RSPI_SPSR); | |
561 | if (spsr & SPSR_SPRF) | |
74da7686 | 562 | rspi_read_data(rspi); /* dummy read */ |
0b2182dd SY |
563 | if (spsr & SPSR_OVRF) |
564 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPSR) & ~SPSR_OVRF, | |
df900e67 | 565 | RSPI_SPSR); |
a3633fe7 SY |
566 | } |
567 | ||
91949a2d | 568 | static int rspi_receive_pio(struct rspi_data *rspi, struct spi_transfer *t) |
a3633fe7 | 569 | { |
35301c99 GU |
570 | int remain = t->len, ret; |
571 | u8 *data = t->rx_buf; | |
a3633fe7 SY |
572 | |
573 | rspi_receive_init(rspi); | |
0b2182dd | 574 | |
0b2182dd SY |
575 | while (remain > 0) { |
576 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_TXMD, | |
577 | RSPI_SPCR); | |
578 | ||
35301c99 GU |
579 | /* dummy write data for generate clock */ |
580 | ret = rspi_data_out_in(rspi, DUMMY_DATA); | |
581 | if (ret < 0) | |
582 | return ret; | |
583 | *data++ = ret; | |
0b2182dd SY |
584 | remain--; |
585 | } | |
586 | ||
587 | return 0; | |
588 | } | |
589 | ||
baf588f4 | 590 | static void qspi_receive_init(const struct rspi_data *rspi) |
cb52c673 | 591 | { |
97b95c11 | 592 | u8 spsr; |
cb52c673 HCM |
593 | |
594 | spsr = rspi_read8(rspi, RSPI_SPSR); | |
595 | if (spsr & SPSR_SPRF) | |
74da7686 | 596 | rspi_read_data(rspi); /* dummy read */ |
cb52c673 HCM |
597 | rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR); |
598 | rspi_write8(rspi, 0x00, QSPI_SPBFCR); | |
599 | } | |
600 | ||
91949a2d | 601 | static int qspi_receive_pio(struct rspi_data *rspi, struct spi_transfer *t) |
cb52c673 | 602 | { |
35301c99 GU |
603 | int remain = t->len, ret; |
604 | u8 *data = t->rx_buf; | |
cb52c673 HCM |
605 | |
606 | qspi_receive_init(rspi); | |
607 | ||
cb52c673 | 608 | while (remain > 0) { |
cb52c673 | 609 | /* dummy write for generate clock */ |
35301c99 GU |
610 | ret = rspi_data_out_in(rspi, DUMMY_DATA); |
611 | if (ret < 0) | |
612 | return ret; | |
613 | *data++ = ret; | |
cb52c673 HCM |
614 | remain--; |
615 | } | |
616 | ||
617 | return 0; | |
618 | } | |
619 | ||
91949a2d | 620 | #define receive_pio(spi, t) spi->ops->receive_pio(spi, t) |
cb52c673 | 621 | |
a3633fe7 SY |
622 | static int rspi_receive_dma(struct rspi_data *rspi, struct spi_transfer *t) |
623 | { | |
624 | struct scatterlist sg, sg_dummy; | |
625 | void *dummy = NULL, *rx_buf = NULL; | |
626 | struct dma_async_tx_descriptor *desc, *desc_dummy; | |
627 | unsigned len; | |
628 | int ret = 0; | |
629 | ||
630 | if (rspi->dma_width_16bit) { | |
631 | /* | |
632 | * If DMAC bus width is 16-bit, the driver allocates a dummy | |
633 | * buffer. And, finally the driver converts the DMAC data into | |
634 | * actual data as the following format: | |
635 | * DMAC data: 1st byte, dummy, 2nd byte, dummy ... | |
636 | * actual data: 1st byte, 2nd byte ... | |
637 | */ | |
638 | len = t->len * 2; | |
639 | rx_buf = kmalloc(len, GFP_KERNEL); | |
640 | if (!rx_buf) | |
641 | return -ENOMEM; | |
642 | } else { | |
643 | len = t->len; | |
644 | rx_buf = t->rx_buf; | |
645 | } | |
646 | ||
647 | /* prepare dummy transfer to generate SPI clocks */ | |
648 | dummy = kzalloc(len, GFP_KERNEL); | |
649 | if (!dummy) { | |
650 | ret = -ENOMEM; | |
651 | goto end_nomap; | |
652 | } | |
653 | if (!rspi_dma_map_sg(&sg_dummy, dummy, len, rspi->chan_tx, | |
654 | DMA_TO_DEVICE)) { | |
655 | ret = -EFAULT; | |
656 | goto end_nomap; | |
657 | } | |
658 | desc_dummy = dmaengine_prep_slave_sg(rspi->chan_tx, &sg_dummy, 1, | |
659 | DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); | |
660 | if (!desc_dummy) { | |
661 | ret = -EIO; | |
662 | goto end_dummy_mapped; | |
663 | } | |
664 | ||
665 | /* prepare receive transfer */ | |
666 | if (!rspi_dma_map_sg(&sg, rx_buf, len, rspi->chan_rx, | |
667 | DMA_FROM_DEVICE)) { | |
668 | ret = -EFAULT; | |
669 | goto end_dummy_mapped; | |
670 | ||
671 | } | |
672 | desc = dmaengine_prep_slave_sg(rspi->chan_rx, &sg, 1, DMA_FROM_DEVICE, | |
673 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); | |
674 | if (!desc) { | |
675 | ret = -EIO; | |
676 | goto end; | |
677 | } | |
678 | ||
679 | rspi_receive_init(rspi); | |
680 | ||
681 | /* | |
682 | * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be | |
683 | * called. So, this driver disables the IRQ while DMA transfer. | |
684 | */ | |
685 | disable_irq(rspi->irq); | |
686 | ||
687 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_TXMD, RSPI_SPCR); | |
688 | rspi_enable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE); | |
689 | rspi->dma_callbacked = 0; | |
690 | ||
691 | desc->callback = rspi_dma_complete; | |
692 | desc->callback_param = rspi; | |
693 | dmaengine_submit(desc); | |
694 | dma_async_issue_pending(rspi->chan_rx); | |
695 | ||
696 | desc_dummy->callback = NULL; /* No callback */ | |
697 | dmaengine_submit(desc_dummy); | |
698 | dma_async_issue_pending(rspi->chan_tx); | |
699 | ||
700 | ret = wait_event_interruptible_timeout(rspi->wait, | |
701 | rspi->dma_callbacked, HZ); | |
702 | if (ret > 0 && rspi->dma_callbacked) | |
703 | ret = 0; | |
704 | else if (!ret) | |
705 | ret = -ETIMEDOUT; | |
706 | rspi_disable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE); | |
707 | ||
708 | enable_irq(rspi->irq); | |
709 | ||
710 | end: | |
711 | rspi_dma_unmap_sg(&sg, rspi->chan_rx, DMA_FROM_DEVICE); | |
712 | end_dummy_mapped: | |
713 | rspi_dma_unmap_sg(&sg_dummy, rspi->chan_tx, DMA_TO_DEVICE); | |
714 | end_nomap: | |
715 | if (rspi->dma_width_16bit) { | |
716 | if (!ret) | |
717 | rspi_memory_from_8bit(t->rx_buf, rx_buf, t->len); | |
718 | kfree(rx_buf); | |
719 | } | |
720 | kfree(dummy); | |
721 | ||
722 | return ret; | |
723 | } | |
724 | ||
baf588f4 | 725 | static int rspi_is_dma(const struct rspi_data *rspi, struct spi_transfer *t) |
a3633fe7 SY |
726 | { |
727 | if (t->tx_buf && rspi->chan_tx) | |
728 | return 1; | |
729 | /* If the module receives data by DMAC, it also needs TX DMAC */ | |
730 | if (t->rx_buf && rspi->chan_tx && rspi->chan_rx) | |
731 | return 1; | |
732 | ||
733 | return 0; | |
734 | } | |
735 | ||
79d23495 GU |
736 | static int rspi_transfer_one(struct spi_master *master, struct spi_device *spi, |
737 | struct spi_transfer *xfer) | |
0b2182dd | 738 | { |
79d23495 GU |
739 | struct rspi_data *rspi = spi_master_get_devdata(master); |
740 | int ret = 0; | |
0b2182dd | 741 | |
79d23495 GU |
742 | if (xfer->tx_buf) { |
743 | if (rspi_is_dma(rspi, xfer)) | |
744 | ret = rspi_send_dma(rspi, xfer); | |
745 | else | |
746 | ret = send_pio(rspi, xfer); | |
747 | if (ret < 0) | |
748 | return ret; | |
0b2182dd | 749 | } |
79d23495 GU |
750 | if (xfer->rx_buf) { |
751 | if (rspi_is_dma(rspi, xfer)) | |
752 | ret = rspi_receive_dma(rspi, xfer); | |
753 | else | |
754 | ret = receive_pio(rspi, xfer); | |
755 | } | |
756 | return ret; | |
0b2182dd SY |
757 | } |
758 | ||
759 | static int rspi_setup(struct spi_device *spi) | |
760 | { | |
761 | struct rspi_data *rspi = spi_master_get_devdata(spi->master); | |
762 | ||
0b2182dd SY |
763 | rspi->max_speed_hz = spi->max_speed_hz; |
764 | ||
348e5153 GU |
765 | rspi->spcmd = SPCMD_SSLKP; |
766 | if (spi->mode & SPI_CPOL) | |
767 | rspi->spcmd |= SPCMD_CPOL; | |
768 | if (spi->mode & SPI_CPHA) | |
769 | rspi->spcmd |= SPCMD_CPHA; | |
770 | ||
5ce0ba88 | 771 | set_config_register(rspi, 8); |
0b2182dd SY |
772 | |
773 | return 0; | |
774 | } | |
775 | ||
79d23495 | 776 | static void rspi_cleanup(struct spi_device *spi) |
0b2182dd | 777 | { |
79d23495 | 778 | } |
0b2182dd | 779 | |
79d23495 GU |
780 | static int rspi_prepare_message(struct spi_master *master, |
781 | struct spi_message *message) | |
782 | { | |
783 | struct rspi_data *rspi = spi_master_get_devdata(master); | |
0b2182dd | 784 | |
79d23495 | 785 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_SPE, RSPI_SPCR); |
0b2182dd SY |
786 | return 0; |
787 | } | |
788 | ||
79d23495 GU |
789 | static int rspi_unprepare_message(struct spi_master *master, |
790 | struct spi_message *message) | |
0b2182dd | 791 | { |
79d23495 GU |
792 | struct rspi_data *rspi = spi_master_get_devdata(master); |
793 | ||
794 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_SPE, RSPI_SPCR); | |
795 | return 0; | |
0b2182dd SY |
796 | } |
797 | ||
798 | static irqreturn_t rspi_irq(int irq, void *_sr) | |
799 | { | |
c132f094 | 800 | struct rspi_data *rspi = _sr; |
97b95c11 | 801 | u8 spsr; |
0b2182dd | 802 | irqreturn_t ret = IRQ_NONE; |
97b95c11 | 803 | u8 disable_irq = 0; |
0b2182dd SY |
804 | |
805 | rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR); | |
806 | if (spsr & SPSR_SPRF) | |
807 | disable_irq |= SPCR_SPRIE; | |
808 | if (spsr & SPSR_SPTEF) | |
809 | disable_irq |= SPCR_SPTIE; | |
810 | ||
811 | if (disable_irq) { | |
812 | ret = IRQ_HANDLED; | |
813 | rspi_disable_irq(rspi, disable_irq); | |
814 | wake_up(&rspi->wait); | |
815 | } | |
816 | ||
817 | return ret; | |
818 | } | |
819 | ||
fd4a319b | 820 | static int rspi_request_dma(struct rspi_data *rspi, |
0243c536 | 821 | struct platform_device *pdev) |
a3633fe7 | 822 | { |
baf588f4 | 823 | const struct rspi_plat_data *rspi_pd = dev_get_platdata(&pdev->dev); |
e2b05099 | 824 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
a3633fe7 | 825 | dma_cap_mask_t mask; |
0243c536 SY |
826 | struct dma_slave_config cfg; |
827 | int ret; | |
a3633fe7 | 828 | |
e2b05099 | 829 | if (!res || !rspi_pd) |
0243c536 | 830 | return 0; /* The driver assumes no error. */ |
a3633fe7 SY |
831 | |
832 | rspi->dma_width_16bit = rspi_pd->dma_width_16bit; | |
833 | ||
834 | /* If the module receives data by DMAC, it also needs TX DMAC */ | |
835 | if (rspi_pd->dma_rx_id && rspi_pd->dma_tx_id) { | |
836 | dma_cap_zero(mask); | |
837 | dma_cap_set(DMA_SLAVE, mask); | |
0243c536 SY |
838 | rspi->chan_rx = dma_request_channel(mask, shdma_chan_filter, |
839 | (void *)rspi_pd->dma_rx_id); | |
840 | if (rspi->chan_rx) { | |
841 | cfg.slave_id = rspi_pd->dma_rx_id; | |
842 | cfg.direction = DMA_DEV_TO_MEM; | |
e2b05099 GL |
843 | cfg.dst_addr = 0; |
844 | cfg.src_addr = res->start + RSPI_SPDR; | |
0243c536 SY |
845 | ret = dmaengine_slave_config(rspi->chan_rx, &cfg); |
846 | if (!ret) | |
847 | dev_info(&pdev->dev, "Use DMA when rx.\n"); | |
848 | else | |
849 | return ret; | |
850 | } | |
a3633fe7 SY |
851 | } |
852 | if (rspi_pd->dma_tx_id) { | |
853 | dma_cap_zero(mask); | |
854 | dma_cap_set(DMA_SLAVE, mask); | |
0243c536 SY |
855 | rspi->chan_tx = dma_request_channel(mask, shdma_chan_filter, |
856 | (void *)rspi_pd->dma_tx_id); | |
857 | if (rspi->chan_tx) { | |
858 | cfg.slave_id = rspi_pd->dma_tx_id; | |
859 | cfg.direction = DMA_MEM_TO_DEV; | |
e2b05099 GL |
860 | cfg.dst_addr = res->start + RSPI_SPDR; |
861 | cfg.src_addr = 0; | |
0243c536 SY |
862 | ret = dmaengine_slave_config(rspi->chan_tx, &cfg); |
863 | if (!ret) | |
864 | dev_info(&pdev->dev, "Use DMA when tx\n"); | |
865 | else | |
866 | return ret; | |
867 | } | |
a3633fe7 | 868 | } |
0243c536 SY |
869 | |
870 | return 0; | |
a3633fe7 SY |
871 | } |
872 | ||
fd4a319b | 873 | static void rspi_release_dma(struct rspi_data *rspi) |
a3633fe7 SY |
874 | { |
875 | if (rspi->chan_tx) | |
876 | dma_release_channel(rspi->chan_tx); | |
877 | if (rspi->chan_rx) | |
878 | dma_release_channel(rspi->chan_rx); | |
879 | } | |
880 | ||
fd4a319b | 881 | static int rspi_remove(struct platform_device *pdev) |
0b2182dd | 882 | { |
5ffbe2d9 | 883 | struct rspi_data *rspi = platform_get_drvdata(pdev); |
0b2182dd | 884 | |
a3633fe7 | 885 | rspi_release_dma(rspi); |
fcb4ed74 | 886 | clk_disable(rspi->clk); |
0b2182dd SY |
887 | |
888 | return 0; | |
889 | } | |
890 | ||
fd4a319b | 891 | static int rspi_probe(struct platform_device *pdev) |
0b2182dd SY |
892 | { |
893 | struct resource *res; | |
894 | struct spi_master *master; | |
895 | struct rspi_data *rspi; | |
896 | int ret, irq; | |
897 | char clk_name[16]; | |
baf588f4 | 898 | const struct rspi_plat_data *rspi_pd = dev_get_platdata(&pdev->dev); |
5ce0ba88 HCM |
899 | const struct spi_ops *ops; |
900 | const struct platform_device_id *id_entry = pdev->id_entry; | |
901 | ||
902 | ops = (struct spi_ops *)id_entry->driver_data; | |
903 | /* ops parameter check */ | |
904 | if (!ops->set_config_register) { | |
905 | dev_err(&pdev->dev, "there is no set_config_register\n"); | |
906 | return -ENODEV; | |
907 | } | |
0b2182dd SY |
908 | |
909 | irq = platform_get_irq(pdev, 0); | |
910 | if (irq < 0) { | |
911 | dev_err(&pdev->dev, "platform_get_irq error\n"); | |
912 | return -ENODEV; | |
913 | } | |
914 | ||
915 | master = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data)); | |
916 | if (master == NULL) { | |
917 | dev_err(&pdev->dev, "spi_alloc_master error.\n"); | |
918 | return -ENOMEM; | |
919 | } | |
920 | ||
921 | rspi = spi_master_get_devdata(master); | |
24b5a82c | 922 | platform_set_drvdata(pdev, rspi); |
5ce0ba88 | 923 | rspi->ops = ops; |
0b2182dd | 924 | rspi->master = master; |
5d79e9ac LP |
925 | |
926 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
927 | rspi->addr = devm_ioremap_resource(&pdev->dev, res); | |
928 | if (IS_ERR(rspi->addr)) { | |
929 | ret = PTR_ERR(rspi->addr); | |
0b2182dd SY |
930 | goto error1; |
931 | } | |
932 | ||
5ce0ba88 | 933 | snprintf(clk_name, sizeof(clk_name), "%s%d", id_entry->name, pdev->id); |
5d79e9ac | 934 | rspi->clk = devm_clk_get(&pdev->dev, clk_name); |
0b2182dd SY |
935 | if (IS_ERR(rspi->clk)) { |
936 | dev_err(&pdev->dev, "cannot get clock\n"); | |
937 | ret = PTR_ERR(rspi->clk); | |
5d79e9ac | 938 | goto error1; |
0b2182dd SY |
939 | } |
940 | clk_enable(rspi->clk); | |
941 | ||
0b2182dd SY |
942 | init_waitqueue_head(&rspi->wait); |
943 | ||
efd85acb GU |
944 | if (rspi_pd && rspi_pd->num_chipselect) |
945 | master->num_chipselect = rspi_pd->num_chipselect; | |
946 | else | |
5ce0ba88 HCM |
947 | master->num_chipselect = 2; /* default */ |
948 | ||
0b2182dd SY |
949 | master->bus_num = pdev->id; |
950 | master->setup = rspi_setup; | |
79d23495 | 951 | master->transfer_one = rspi_transfer_one; |
0b2182dd | 952 | master->cleanup = rspi_cleanup; |
79d23495 GU |
953 | master->prepare_message = rspi_prepare_message; |
954 | master->unprepare_message = rspi_unprepare_message; | |
348e5153 | 955 | master->mode_bits = SPI_CPHA | SPI_CPOL; |
0b2182dd | 956 | |
5d79e9ac LP |
957 | ret = devm_request_irq(&pdev->dev, irq, rspi_irq, 0, |
958 | dev_name(&pdev->dev), rspi); | |
0b2182dd SY |
959 | if (ret < 0) { |
960 | dev_err(&pdev->dev, "request_irq error\n"); | |
fcb4ed74 | 961 | goto error2; |
0b2182dd SY |
962 | } |
963 | ||
a3633fe7 | 964 | rspi->irq = irq; |
0243c536 SY |
965 | ret = rspi_request_dma(rspi, pdev); |
966 | if (ret < 0) { | |
967 | dev_err(&pdev->dev, "rspi_request_dma failed.\n"); | |
fcb4ed74 | 968 | goto error3; |
0243c536 | 969 | } |
a3633fe7 | 970 | |
9e03d05e | 971 | ret = devm_spi_register_master(&pdev->dev, master); |
0b2182dd SY |
972 | if (ret < 0) { |
973 | dev_err(&pdev->dev, "spi_register_master error.\n"); | |
fcb4ed74 | 974 | goto error3; |
0b2182dd SY |
975 | } |
976 | ||
977 | dev_info(&pdev->dev, "probed\n"); | |
978 | ||
979 | return 0; | |
980 | ||
fcb4ed74 | 981 | error3: |
5d79e9ac | 982 | rspi_release_dma(rspi); |
fcb4ed74 GU |
983 | error2: |
984 | clk_disable(rspi->clk); | |
0b2182dd SY |
985 | error1: |
986 | spi_master_put(master); | |
987 | ||
988 | return ret; | |
989 | } | |
990 | ||
5ce0ba88 HCM |
991 | static struct spi_ops rspi_ops = { |
992 | .set_config_register = rspi_set_config_register, | |
cb52c673 HCM |
993 | .send_pio = rspi_send_pio, |
994 | .receive_pio = rspi_receive_pio, | |
5ce0ba88 HCM |
995 | }; |
996 | ||
997 | static struct spi_ops qspi_ops = { | |
998 | .set_config_register = qspi_set_config_register, | |
cb52c673 HCM |
999 | .send_pio = qspi_send_pio, |
1000 | .receive_pio = qspi_receive_pio, | |
5ce0ba88 HCM |
1001 | }; |
1002 | ||
1003 | static struct platform_device_id spi_driver_ids[] = { | |
1004 | { "rspi", (kernel_ulong_t)&rspi_ops }, | |
1005 | { "qspi", (kernel_ulong_t)&qspi_ops }, | |
1006 | {}, | |
1007 | }; | |
1008 | ||
1009 | MODULE_DEVICE_TABLE(platform, spi_driver_ids); | |
1010 | ||
0b2182dd SY |
1011 | static struct platform_driver rspi_driver = { |
1012 | .probe = rspi_probe, | |
fd4a319b | 1013 | .remove = rspi_remove, |
5ce0ba88 | 1014 | .id_table = spi_driver_ids, |
0b2182dd | 1015 | .driver = { |
5ce0ba88 | 1016 | .name = "renesas_spi", |
0b2182dd SY |
1017 | .owner = THIS_MODULE, |
1018 | }, | |
1019 | }; | |
1020 | module_platform_driver(rspi_driver); | |
1021 | ||
1022 | MODULE_DESCRIPTION("Renesas RSPI bus driver"); | |
1023 | MODULE_LICENSE("GPL v2"); | |
1024 | MODULE_AUTHOR("Yoshihiro Shimoda"); | |
1025 | MODULE_ALIAS("platform:rspi"); |