blob: 4c8dbac11b4252465b7d5d118529a25e1a1b2693 [file] [log] [blame]
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +09001/*
2 * SH RSPI driver
3 *
4 * Copyright (C) 2012 Renesas Solutions Corp.
5 *
6 * Based on spi-sh.c:
7 * Copyright (C) 2011 Renesas Solutions Corp.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/sched.h>
27#include <linux/errno.h>
28#include <linux/list.h>
29#include <linux/workqueue.h>
30#include <linux/interrupt.h>
31#include <linux/platform_device.h>
32#include <linux/io.h>
33#include <linux/clk.h>
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +090034#include <linux/dmaengine.h>
35#include <linux/dma-mapping.h>
36#include <linux/sh_dma.h>
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090037#include <linux/spi/spi.h>
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +090038#include <linux/spi/rspi.h>
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090039
40#define RSPI_SPCR 0x00
41#define RSPI_SSLP 0x01
42#define RSPI_SPPCR 0x02
43#define RSPI_SPSR 0x03
44#define RSPI_SPDR 0x04
45#define RSPI_SPSCR 0x08
46#define RSPI_SPSSR 0x09
47#define RSPI_SPBR 0x0a
48#define RSPI_SPDCR 0x0b
49#define RSPI_SPCKD 0x0c
50#define RSPI_SSLND 0x0d
51#define RSPI_SPND 0x0e
52#define RSPI_SPCR2 0x0f
53#define RSPI_SPCMD0 0x10
54#define RSPI_SPCMD1 0x12
55#define RSPI_SPCMD2 0x14
56#define RSPI_SPCMD3 0x16
57#define RSPI_SPCMD4 0x18
58#define RSPI_SPCMD5 0x1a
59#define RSPI_SPCMD6 0x1c
60#define RSPI_SPCMD7 0x1e
61
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +090062/*qspi only */
63#define QSPI_SPBFCR 0x18
64#define QSPI_SPBDCR 0x1a
65#define QSPI_SPBMUL0 0x1c
66#define QSPI_SPBMUL1 0x20
67#define QSPI_SPBMUL2 0x24
68#define QSPI_SPBMUL3 0x28
69
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +090070/* SPCR */
71#define SPCR_SPRIE 0x80
72#define SPCR_SPE 0x40
73#define SPCR_SPTIE 0x20
74#define SPCR_SPEIE 0x10
75#define SPCR_MSTR 0x08
76#define SPCR_MODFEN 0x04
77#define SPCR_TXMD 0x02
78#define SPCR_SPMS 0x01
79
80/* SSLP */
81#define SSLP_SSL1P 0x02
82#define SSLP_SSL0P 0x01
83
84/* SPPCR */
85#define SPPCR_MOIFE 0x20
86#define SPPCR_MOIFV 0x10
87#define SPPCR_SPOM 0x04
88#define SPPCR_SPLP2 0x02
89#define SPPCR_SPLP 0x01
90
91/* SPSR */
92#define SPSR_SPRF 0x80
93#define SPSR_SPTEF 0x20
94#define SPSR_PERF 0x08
95#define SPSR_MODF 0x04
96#define SPSR_IDLNF 0x02
97#define SPSR_OVRF 0x01
98
99/* SPSCR */
100#define SPSCR_SPSLN_MASK 0x07
101
102/* SPSSR */
103#define SPSSR_SPECM_MASK 0x70
104#define SPSSR_SPCP_MASK 0x07
105
106/* SPDCR */
107#define SPDCR_SPLW 0x20
108#define SPDCR_SPRDTD 0x10
109#define SPDCR_SLSEL1 0x08
110#define SPDCR_SLSEL0 0x04
111#define SPDCR_SLSEL_MASK 0x0c
112#define SPDCR_SPFC1 0x02
113#define SPDCR_SPFC0 0x01
114
115/* SPCKD */
116#define SPCKD_SCKDL_MASK 0x07
117
118/* SSLND */
119#define SSLND_SLNDL_MASK 0x07
120
121/* SPND */
122#define SPND_SPNDL_MASK 0x07
123
124/* SPCR2 */
125#define SPCR2_PTE 0x08
126#define SPCR2_SPIE 0x04
127#define SPCR2_SPOE 0x02
128#define SPCR2_SPPE 0x01
129
130/* SPCMDn */
131#define SPCMD_SCKDEN 0x8000
132#define SPCMD_SLNDEN 0x4000
133#define SPCMD_SPNDEN 0x2000
134#define SPCMD_LSBF 0x1000
135#define SPCMD_SPB_MASK 0x0f00
136#define SPCMD_SPB_8_TO_16(bit) (((bit - 1) << 8) & SPCMD_SPB_MASK)
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900137#define SPCMD_SPB_8BIT 0x0000 /* qspi only */
138#define SPCMD_SPB_16BIT 0x0100
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900139#define SPCMD_SPB_20BIT 0x0000
140#define SPCMD_SPB_24BIT 0x0100
141#define SPCMD_SPB_32BIT 0x0200
142#define SPCMD_SSLKP 0x0080
143#define SPCMD_SSLA_MASK 0x0030
144#define SPCMD_BRDV_MASK 0x000c
145#define SPCMD_CPOL 0x0002
146#define SPCMD_CPHA 0x0001
147
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900148/* SPBFCR */
149#define SPBFCR_TXRST 0x80 /* qspi only */
150#define SPBFCR_RXRST 0x40 /* qspi only */
151
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900152struct rspi_data {
153 void __iomem *addr;
154 u32 max_speed_hz;
155 struct spi_master *master;
156 struct list_head queue;
157 struct work_struct ws;
158 wait_queue_head_t wait;
159 spinlock_t lock;
160 struct clk *clk;
161 unsigned char spsr;
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900162 const struct spi_ops *ops;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900163
164 /* for dmaengine */
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900165 struct dma_chan *chan_tx;
166 struct dma_chan *chan_rx;
167 int irq;
168
169 unsigned dma_width_16bit:1;
170 unsigned dma_callbacked:1;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900171};
172
173static void rspi_write8(struct rspi_data *rspi, u8 data, u16 offset)
174{
175 iowrite8(data, rspi->addr + offset);
176}
177
178static void rspi_write16(struct rspi_data *rspi, u16 data, u16 offset)
179{
180 iowrite16(data, rspi->addr + offset);
181}
182
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900183static void rspi_write32(struct rspi_data *rspi, u32 data, u16 offset)
184{
185 iowrite32(data, rspi->addr + offset);
186}
187
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900188static u8 rspi_read8(struct rspi_data *rspi, u16 offset)
189{
190 return ioread8(rspi->addr + offset);
191}
192
193static u16 rspi_read16(struct rspi_data *rspi, u16 offset)
194{
195 return ioread16(rspi->addr + offset);
196}
197
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900198/* optional functions */
199struct spi_ops {
200 int (*set_config_register)(struct rspi_data *rspi, int access_size);
201};
202
203/*
204 * functions for RSPI
205 */
206static int rspi_set_config_register(struct rspi_data *rspi, int access_size)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900207{
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900208 int spbr;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900209
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900210 /* Sets output mode(CMOS) and MOSI signal(from previous transfer) */
211 rspi_write8(rspi, 0x00, RSPI_SPPCR);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900212
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900213 /* Sets transfer bit rate */
214 spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz) - 1;
215 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
216
217 /* Sets number of frames to be used: 1 frame */
218 rspi_write8(rspi, 0x00, RSPI_SPDCR);
219
220 /* Sets RSPCK, SSL, next-access delay value */
221 rspi_write8(rspi, 0x00, RSPI_SPCKD);
222 rspi_write8(rspi, 0x00, RSPI_SSLND);
223 rspi_write8(rspi, 0x00, RSPI_SPND);
224
225 /* Sets parity, interrupt mask */
226 rspi_write8(rspi, 0x00, RSPI_SPCR2);
227
228 /* Sets SPCMD */
229 rspi_write16(rspi, SPCMD_SPB_8_TO_16(access_size) | SPCMD_SSLKP,
230 RSPI_SPCMD0);
231
232 /* Sets RSPI mode */
233 rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
234
235 return 0;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900236}
237
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900238/*
239 * functions for QSPI
240 */
241static int qspi_set_config_register(struct rspi_data *rspi, int access_size)
242{
243 u16 spcmd;
244 int spbr;
245
246 /* Sets output mode(CMOS) and MOSI signal(from previous transfer) */
247 rspi_write8(rspi, 0x00, RSPI_SPPCR);
248
249 /* Sets transfer bit rate */
250 spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz);
251 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
252
253 /* Sets number of frames to be used: 1 frame */
254 rspi_write8(rspi, 0x00, RSPI_SPDCR);
255
256 /* Sets RSPCK, SSL, next-access delay value */
257 rspi_write8(rspi, 0x00, RSPI_SPCKD);
258 rspi_write8(rspi, 0x00, RSPI_SSLND);
259 rspi_write8(rspi, 0x00, RSPI_SPND);
260
261 /* Data Length Setting */
262 if (access_size == 8)
263 spcmd = SPCMD_SPB_8BIT;
264 else if (access_size == 16)
265 spcmd = SPCMD_SPB_16BIT;
266 else if (access_size == 32)
267 spcmd = SPCMD_SPB_32BIT;
268
269 spcmd |= SPCMD_SCKDEN | SPCMD_SLNDEN | SPCMD_SSLKP | SPCMD_SPNDEN;
270
271 /* Resets transfer data length */
272 rspi_write32(rspi, 0, QSPI_SPBMUL0);
273
274 /* Resets transmit and receive buffer */
275 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
276 /* Sets buffer to allow normal operation */
277 rspi_write8(rspi, 0x00, QSPI_SPBFCR);
278
279 /* Sets SPCMD */
280 rspi_write16(rspi, spcmd, RSPI_SPCMD0);
281
282 /* Enables SPI function in a master mode */
283 rspi_write8(rspi, SPCR_SPE | SPCR_MSTR, RSPI_SPCR);
284
285 return 0;
286}
287
288#define set_config_register(spi, n) spi->ops->set_config_register(spi, n)
289
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900290static void rspi_enable_irq(struct rspi_data *rspi, u8 enable)
291{
292 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | enable, RSPI_SPCR);
293}
294
295static void rspi_disable_irq(struct rspi_data *rspi, u8 disable)
296{
297 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~disable, RSPI_SPCR);
298}
299
300static int rspi_wait_for_interrupt(struct rspi_data *rspi, u8 wait_mask,
301 u8 enable_bit)
302{
303 int ret;
304
305 rspi->spsr = rspi_read8(rspi, RSPI_SPSR);
306 rspi_enable_irq(rspi, enable_bit);
307 ret = wait_event_timeout(rspi->wait, rspi->spsr & wait_mask, HZ);
308 if (ret == 0 && !(rspi->spsr & wait_mask))
309 return -ETIMEDOUT;
310
311 return 0;
312}
313
314static void rspi_assert_ssl(struct rspi_data *rspi)
315{
316 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_SPE, RSPI_SPCR);
317}
318
319static void rspi_negate_ssl(struct rspi_data *rspi)
320{
321 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_SPE, RSPI_SPCR);
322}
323
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900324static int rspi_send_pio(struct rspi_data *rspi, struct spi_message *mesg,
325 struct spi_transfer *t)
326{
327 int remain = t->len;
328 u8 *data;
329
330 data = (u8 *)t->tx_buf;
331 while (remain > 0) {
332 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_TXMD,
333 RSPI_SPCR);
334
335 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
336 dev_err(&rspi->master->dev,
337 "%s: tx empty timeout\n", __func__);
338 return -ETIMEDOUT;
339 }
340
341 rspi_write16(rspi, *data, RSPI_SPDR);
342 data++;
343 remain--;
344 }
345
346 /* Waiting for the last transmition */
347 rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
348
349 return 0;
350}
351
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900352static void rspi_dma_complete(void *arg)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900353{
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900354 struct rspi_data *rspi = arg;
355
356 rspi->dma_callbacked = 1;
357 wake_up_interruptible(&rspi->wait);
358}
359
360static int rspi_dma_map_sg(struct scatterlist *sg, void *buf, unsigned len,
361 struct dma_chan *chan,
362 enum dma_transfer_direction dir)
363{
364 sg_init_table(sg, 1);
365 sg_set_buf(sg, buf, len);
366 sg_dma_len(sg) = len;
367 return dma_map_sg(chan->device->dev, sg, 1, dir);
368}
369
370static void rspi_dma_unmap_sg(struct scatterlist *sg, struct dma_chan *chan,
371 enum dma_transfer_direction dir)
372{
373 dma_unmap_sg(chan->device->dev, sg, 1, dir);
374}
375
376static void rspi_memory_to_8bit(void *buf, const void *data, unsigned len)
377{
378 u16 *dst = buf;
379 const u8 *src = data;
380
381 while (len) {
382 *dst++ = (u16)(*src++);
383 len--;
384 }
385}
386
387static void rspi_memory_from_8bit(void *buf, const void *data, unsigned len)
388{
389 u8 *dst = buf;
390 const u16 *src = data;
391
392 while (len) {
393 *dst++ = (u8)*src++;
394 len--;
395 }
396}
397
398static int rspi_send_dma(struct rspi_data *rspi, struct spi_transfer *t)
399{
400 struct scatterlist sg;
401 void *buf = NULL;
402 struct dma_async_tx_descriptor *desc;
403 unsigned len;
404 int ret = 0;
405
406 if (rspi->dma_width_16bit) {
407 /*
408 * If DMAC bus width is 16-bit, the driver allocates a dummy
409 * buffer. And, the driver converts original data into the
410 * DMAC data as the following format:
411 * original data: 1st byte, 2nd byte ...
412 * DMAC data: 1st byte, dummy, 2nd byte, dummy ...
413 */
414 len = t->len * 2;
415 buf = kmalloc(len, GFP_KERNEL);
416 if (!buf)
417 return -ENOMEM;
418 rspi_memory_to_8bit(buf, t->tx_buf, t->len);
419 } else {
420 len = t->len;
421 buf = (void *)t->tx_buf;
422 }
423
424 if (!rspi_dma_map_sg(&sg, buf, len, rspi->chan_tx, DMA_TO_DEVICE)) {
425 ret = -EFAULT;
426 goto end_nomap;
427 }
428 desc = dmaengine_prep_slave_sg(rspi->chan_tx, &sg, 1, DMA_TO_DEVICE,
429 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
430 if (!desc) {
431 ret = -EIO;
432 goto end;
433 }
434
435 /*
436 * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be
437 * called. So, this driver disables the IRQ while DMA transfer.
438 */
439 disable_irq(rspi->irq);
440
441 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_TXMD, RSPI_SPCR);
442 rspi_enable_irq(rspi, SPCR_SPTIE);
443 rspi->dma_callbacked = 0;
444
445 desc->callback = rspi_dma_complete;
446 desc->callback_param = rspi;
447 dmaengine_submit(desc);
448 dma_async_issue_pending(rspi->chan_tx);
449
450 ret = wait_event_interruptible_timeout(rspi->wait,
451 rspi->dma_callbacked, HZ);
452 if (ret > 0 && rspi->dma_callbacked)
453 ret = 0;
454 else if (!ret)
455 ret = -ETIMEDOUT;
456 rspi_disable_irq(rspi, SPCR_SPTIE);
457
458 enable_irq(rspi->irq);
459
460end:
461 rspi_dma_unmap_sg(&sg, rspi->chan_tx, DMA_TO_DEVICE);
462end_nomap:
463 if (rspi->dma_width_16bit)
464 kfree(buf);
465
466 return ret;
467}
468
469static void rspi_receive_init(struct rspi_data *rspi)
470{
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900471 unsigned char spsr;
472
473 spsr = rspi_read8(rspi, RSPI_SPSR);
474 if (spsr & SPSR_SPRF)
475 rspi_read16(rspi, RSPI_SPDR); /* dummy read */
476 if (spsr & SPSR_OVRF)
477 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPSR) & ~SPSR_OVRF,
478 RSPI_SPCR);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900479}
480
481static int rspi_receive_pio(struct rspi_data *rspi, struct spi_message *mesg,
482 struct spi_transfer *t)
483{
484 int remain = t->len;
485 u8 *data;
486
487 rspi_receive_init(rspi);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900488
489 data = (u8 *)t->rx_buf;
490 while (remain > 0) {
491 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_TXMD,
492 RSPI_SPCR);
493
494 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
495 dev_err(&rspi->master->dev,
496 "%s: tx empty timeout\n", __func__);
497 return -ETIMEDOUT;
498 }
499 /* dummy write for generate clock */
500 rspi_write16(rspi, 0x00, RSPI_SPDR);
501
502 if (rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE) < 0) {
503 dev_err(&rspi->master->dev,
504 "%s: receive timeout\n", __func__);
505 return -ETIMEDOUT;
506 }
507 /* SPDR allows 16 or 32-bit access only */
508 *data = (u8)rspi_read16(rspi, RSPI_SPDR);
509
510 data++;
511 remain--;
512 }
513
514 return 0;
515}
516
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900517static int rspi_receive_dma(struct rspi_data *rspi, struct spi_transfer *t)
518{
519 struct scatterlist sg, sg_dummy;
520 void *dummy = NULL, *rx_buf = NULL;
521 struct dma_async_tx_descriptor *desc, *desc_dummy;
522 unsigned len;
523 int ret = 0;
524
525 if (rspi->dma_width_16bit) {
526 /*
527 * If DMAC bus width is 16-bit, the driver allocates a dummy
528 * buffer. And, finally the driver converts the DMAC data into
529 * actual data as the following format:
530 * DMAC data: 1st byte, dummy, 2nd byte, dummy ...
531 * actual data: 1st byte, 2nd byte ...
532 */
533 len = t->len * 2;
534 rx_buf = kmalloc(len, GFP_KERNEL);
535 if (!rx_buf)
536 return -ENOMEM;
537 } else {
538 len = t->len;
539 rx_buf = t->rx_buf;
540 }
541
542 /* prepare dummy transfer to generate SPI clocks */
543 dummy = kzalloc(len, GFP_KERNEL);
544 if (!dummy) {
545 ret = -ENOMEM;
546 goto end_nomap;
547 }
548 if (!rspi_dma_map_sg(&sg_dummy, dummy, len, rspi->chan_tx,
549 DMA_TO_DEVICE)) {
550 ret = -EFAULT;
551 goto end_nomap;
552 }
553 desc_dummy = dmaengine_prep_slave_sg(rspi->chan_tx, &sg_dummy, 1,
554 DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
555 if (!desc_dummy) {
556 ret = -EIO;
557 goto end_dummy_mapped;
558 }
559
560 /* prepare receive transfer */
561 if (!rspi_dma_map_sg(&sg, rx_buf, len, rspi->chan_rx,
562 DMA_FROM_DEVICE)) {
563 ret = -EFAULT;
564 goto end_dummy_mapped;
565
566 }
567 desc = dmaengine_prep_slave_sg(rspi->chan_rx, &sg, 1, DMA_FROM_DEVICE,
568 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
569 if (!desc) {
570 ret = -EIO;
571 goto end;
572 }
573
574 rspi_receive_init(rspi);
575
576 /*
577 * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be
578 * called. So, this driver disables the IRQ while DMA transfer.
579 */
580 disable_irq(rspi->irq);
581
582 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_TXMD, RSPI_SPCR);
583 rspi_enable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE);
584 rspi->dma_callbacked = 0;
585
586 desc->callback = rspi_dma_complete;
587 desc->callback_param = rspi;
588 dmaengine_submit(desc);
589 dma_async_issue_pending(rspi->chan_rx);
590
591 desc_dummy->callback = NULL; /* No callback */
592 dmaengine_submit(desc_dummy);
593 dma_async_issue_pending(rspi->chan_tx);
594
595 ret = wait_event_interruptible_timeout(rspi->wait,
596 rspi->dma_callbacked, HZ);
597 if (ret > 0 && rspi->dma_callbacked)
598 ret = 0;
599 else if (!ret)
600 ret = -ETIMEDOUT;
601 rspi_disable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE);
602
603 enable_irq(rspi->irq);
604
605end:
606 rspi_dma_unmap_sg(&sg, rspi->chan_rx, DMA_FROM_DEVICE);
607end_dummy_mapped:
608 rspi_dma_unmap_sg(&sg_dummy, rspi->chan_tx, DMA_TO_DEVICE);
609end_nomap:
610 if (rspi->dma_width_16bit) {
611 if (!ret)
612 rspi_memory_from_8bit(t->rx_buf, rx_buf, t->len);
613 kfree(rx_buf);
614 }
615 kfree(dummy);
616
617 return ret;
618}
619
620static int rspi_is_dma(struct rspi_data *rspi, struct spi_transfer *t)
621{
622 if (t->tx_buf && rspi->chan_tx)
623 return 1;
624 /* If the module receives data by DMAC, it also needs TX DMAC */
625 if (t->rx_buf && rspi->chan_tx && rspi->chan_rx)
626 return 1;
627
628 return 0;
629}
630
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900631static void rspi_work(struct work_struct *work)
632{
633 struct rspi_data *rspi = container_of(work, struct rspi_data, ws);
634 struct spi_message *mesg;
635 struct spi_transfer *t;
636 unsigned long flags;
637 int ret;
638
Shimoda, Yoshihiro8d4d08c2013-08-27 11:15:09 +0900639 while (1) {
640 spin_lock_irqsave(&rspi->lock, flags);
641 if (list_empty(&rspi->queue)) {
642 spin_unlock_irqrestore(&rspi->lock, flags);
643 break;
644 }
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900645 mesg = list_entry(rspi->queue.next, struct spi_message, queue);
646 list_del_init(&mesg->queue);
647 spin_unlock_irqrestore(&rspi->lock, flags);
648
649 rspi_assert_ssl(rspi);
650
651 list_for_each_entry(t, &mesg->transfers, transfer_list) {
652 if (t->tx_buf) {
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900653 if (rspi_is_dma(rspi, t))
654 ret = rspi_send_dma(rspi, t);
655 else
656 ret = rspi_send_pio(rspi, mesg, t);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900657 if (ret < 0)
658 goto error;
659 }
660 if (t->rx_buf) {
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900661 if (rspi_is_dma(rspi, t))
662 ret = rspi_receive_dma(rspi, t);
663 else
664 ret = rspi_receive_pio(rspi, mesg, t);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900665 if (ret < 0)
666 goto error;
667 }
668 mesg->actual_length += t->len;
669 }
670 rspi_negate_ssl(rspi);
671
672 mesg->status = 0;
673 mesg->complete(mesg->context);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900674 }
675
676 return;
677
678error:
679 mesg->status = ret;
680 mesg->complete(mesg->context);
681}
682
683static int rspi_setup(struct spi_device *spi)
684{
685 struct rspi_data *rspi = spi_master_get_devdata(spi->master);
686
687 if (!spi->bits_per_word)
688 spi->bits_per_word = 8;
689 rspi->max_speed_hz = spi->max_speed_hz;
690
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900691 set_config_register(rspi, 8);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900692
693 return 0;
694}
695
696static int rspi_transfer(struct spi_device *spi, struct spi_message *mesg)
697{
698 struct rspi_data *rspi = spi_master_get_devdata(spi->master);
699 unsigned long flags;
700
701 mesg->actual_length = 0;
702 mesg->status = -EINPROGRESS;
703
704 spin_lock_irqsave(&rspi->lock, flags);
705 list_add_tail(&mesg->queue, &rspi->queue);
706 schedule_work(&rspi->ws);
707 spin_unlock_irqrestore(&rspi->lock, flags);
708
709 return 0;
710}
711
712static void rspi_cleanup(struct spi_device *spi)
713{
714}
715
716static irqreturn_t rspi_irq(int irq, void *_sr)
717{
718 struct rspi_data *rspi = (struct rspi_data *)_sr;
719 unsigned long spsr;
720 irqreturn_t ret = IRQ_NONE;
721 unsigned char disable_irq = 0;
722
723 rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
724 if (spsr & SPSR_SPRF)
725 disable_irq |= SPCR_SPRIE;
726 if (spsr & SPSR_SPTEF)
727 disable_irq |= SPCR_SPTIE;
728
729 if (disable_irq) {
730 ret = IRQ_HANDLED;
731 rspi_disable_irq(rspi, disable_irq);
732 wake_up(&rspi->wait);
733 }
734
735 return ret;
736}
737
Grant Likelyfd4a3192012-12-07 16:57:14 +0000738static int rspi_request_dma(struct rspi_data *rspi,
Shimoda, Yoshihiro0243c5362012-08-02 17:17:33 +0900739 struct platform_device *pdev)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900740{
Jingoo Han8074cf02013-07-30 16:58:59 +0900741 struct rspi_plat_data *rspi_pd = dev_get_platdata(&pdev->dev);
Guennadi Liakhovetskie2b05092013-08-02 15:03:42 +0200742 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900743 dma_cap_mask_t mask;
Shimoda, Yoshihiro0243c5362012-08-02 17:17:33 +0900744 struct dma_slave_config cfg;
745 int ret;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900746
Guennadi Liakhovetskie2b05092013-08-02 15:03:42 +0200747 if (!res || !rspi_pd)
Shimoda, Yoshihiro0243c5362012-08-02 17:17:33 +0900748 return 0; /* The driver assumes no error. */
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900749
750 rspi->dma_width_16bit = rspi_pd->dma_width_16bit;
751
752 /* If the module receives data by DMAC, it also needs TX DMAC */
753 if (rspi_pd->dma_rx_id && rspi_pd->dma_tx_id) {
754 dma_cap_zero(mask);
755 dma_cap_set(DMA_SLAVE, mask);
Shimoda, Yoshihiro0243c5362012-08-02 17:17:33 +0900756 rspi->chan_rx = dma_request_channel(mask, shdma_chan_filter,
757 (void *)rspi_pd->dma_rx_id);
758 if (rspi->chan_rx) {
759 cfg.slave_id = rspi_pd->dma_rx_id;
760 cfg.direction = DMA_DEV_TO_MEM;
Guennadi Liakhovetskie2b05092013-08-02 15:03:42 +0200761 cfg.dst_addr = 0;
762 cfg.src_addr = res->start + RSPI_SPDR;
Shimoda, Yoshihiro0243c5362012-08-02 17:17:33 +0900763 ret = dmaengine_slave_config(rspi->chan_rx, &cfg);
764 if (!ret)
765 dev_info(&pdev->dev, "Use DMA when rx.\n");
766 else
767 return ret;
768 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900769 }
770 if (rspi_pd->dma_tx_id) {
771 dma_cap_zero(mask);
772 dma_cap_set(DMA_SLAVE, mask);
Shimoda, Yoshihiro0243c5362012-08-02 17:17:33 +0900773 rspi->chan_tx = dma_request_channel(mask, shdma_chan_filter,
774 (void *)rspi_pd->dma_tx_id);
775 if (rspi->chan_tx) {
776 cfg.slave_id = rspi_pd->dma_tx_id;
777 cfg.direction = DMA_MEM_TO_DEV;
Guennadi Liakhovetskie2b05092013-08-02 15:03:42 +0200778 cfg.dst_addr = res->start + RSPI_SPDR;
779 cfg.src_addr = 0;
Shimoda, Yoshihiro0243c5362012-08-02 17:17:33 +0900780 ret = dmaengine_slave_config(rspi->chan_tx, &cfg);
781 if (!ret)
782 dev_info(&pdev->dev, "Use DMA when tx\n");
783 else
784 return ret;
785 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900786 }
Shimoda, Yoshihiro0243c5362012-08-02 17:17:33 +0900787
788 return 0;
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900789}
790
Grant Likelyfd4a3192012-12-07 16:57:14 +0000791static void rspi_release_dma(struct rspi_data *rspi)
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900792{
793 if (rspi->chan_tx)
794 dma_release_channel(rspi->chan_tx);
795 if (rspi->chan_rx)
796 dma_release_channel(rspi->chan_rx);
797}
798
Grant Likelyfd4a3192012-12-07 16:57:14 +0000799static int rspi_remove(struct platform_device *pdev)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900800{
Axel Lin9d3405d2013-08-31 19:42:56 +0800801 struct rspi_data *rspi = spi_master_get(platform_get_drvdata(pdev));
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900802
803 spi_unregister_master(rspi->master);
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900804 rspi_release_dma(rspi);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900805 free_irq(platform_get_irq(pdev, 0), rspi);
806 clk_put(rspi->clk);
807 iounmap(rspi->addr);
808 spi_master_put(rspi->master);
809
810 return 0;
811}
812
Grant Likelyfd4a3192012-12-07 16:57:14 +0000813static int rspi_probe(struct platform_device *pdev)
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900814{
815 struct resource *res;
816 struct spi_master *master;
817 struct rspi_data *rspi;
818 int ret, irq;
819 char clk_name[16];
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900820 struct rspi_plat_data *rspi_pd = pdev->dev.platform_data;
821 const struct spi_ops *ops;
822 const struct platform_device_id *id_entry = pdev->id_entry;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900823
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900824 ops = (struct spi_ops *)id_entry->driver_data;
825 /* ops parameter check */
826 if (!ops->set_config_register) {
827 dev_err(&pdev->dev, "there is no set_config_register\n");
828 return -ENODEV;
829 }
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900830 /* get base addr */
831 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
832 if (unlikely(res == NULL)) {
833 dev_err(&pdev->dev, "invalid resource\n");
834 return -EINVAL;
835 }
836
837 irq = platform_get_irq(pdev, 0);
838 if (irq < 0) {
839 dev_err(&pdev->dev, "platform_get_irq error\n");
840 return -ENODEV;
841 }
842
843 master = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data));
844 if (master == NULL) {
845 dev_err(&pdev->dev, "spi_alloc_master error.\n");
846 return -ENOMEM;
847 }
848
849 rspi = spi_master_get_devdata(master);
Jingoo Han24b5a822013-05-23 19:20:40 +0900850 platform_set_drvdata(pdev, rspi);
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900851 rspi->ops = ops;
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900852 rspi->master = master;
853 rspi->addr = ioremap(res->start, resource_size(res));
854 if (rspi->addr == NULL) {
855 dev_err(&pdev->dev, "ioremap error.\n");
856 ret = -ENOMEM;
857 goto error1;
858 }
859
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900860 snprintf(clk_name, sizeof(clk_name), "%s%d", id_entry->name, pdev->id);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900861 rspi->clk = clk_get(&pdev->dev, clk_name);
862 if (IS_ERR(rspi->clk)) {
863 dev_err(&pdev->dev, "cannot get clock\n");
864 ret = PTR_ERR(rspi->clk);
865 goto error2;
866 }
867 clk_enable(rspi->clk);
868
869 INIT_LIST_HEAD(&rspi->queue);
870 spin_lock_init(&rspi->lock);
871 INIT_WORK(&rspi->ws, rspi_work);
872 init_waitqueue_head(&rspi->wait);
873
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900874 master->num_chipselect = rspi_pd->num_chipselect;
875 if (!master->num_chipselect)
876 master->num_chipselect = 2; /* default */
877
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900878 master->bus_num = pdev->id;
879 master->setup = rspi_setup;
880 master->transfer = rspi_transfer;
881 master->cleanup = rspi_cleanup;
882
883 ret = request_irq(irq, rspi_irq, 0, dev_name(&pdev->dev), rspi);
884 if (ret < 0) {
885 dev_err(&pdev->dev, "request_irq error\n");
886 goto error3;
887 }
888
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900889 rspi->irq = irq;
Shimoda, Yoshihiro0243c5362012-08-02 17:17:33 +0900890 ret = rspi_request_dma(rspi, pdev);
891 if (ret < 0) {
892 dev_err(&pdev->dev, "rspi_request_dma failed.\n");
893 goto error4;
894 }
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900895
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900896 ret = spi_register_master(master);
897 if (ret < 0) {
898 dev_err(&pdev->dev, "spi_register_master error.\n");
899 goto error4;
900 }
901
902 dev_info(&pdev->dev, "probed\n");
903
904 return 0;
905
906error4:
Shimoda, Yoshihiroa3633fe2012-04-20 14:50:36 +0900907 rspi_release_dma(rspi);
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900908 free_irq(irq, rspi);
909error3:
910 clk_put(rspi->clk);
911error2:
912 iounmap(rspi->addr);
913error1:
914 spi_master_put(master);
915
916 return ret;
917}
918
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900919static struct spi_ops rspi_ops = {
920 .set_config_register = rspi_set_config_register,
921};
922
923static struct spi_ops qspi_ops = {
924 .set_config_register = qspi_set_config_register,
925};
926
927static struct platform_device_id spi_driver_ids[] = {
928 { "rspi", (kernel_ulong_t)&rspi_ops },
929 { "qspi", (kernel_ulong_t)&qspi_ops },
930 {},
931};
932
933MODULE_DEVICE_TABLE(platform, spi_driver_ids);
934
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900935static struct platform_driver rspi_driver = {
936 .probe = rspi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000937 .remove = rspi_remove,
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900938 .id_table = spi_driver_ids,
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900939 .driver = {
Hiep Cao Minh5ce0ba82013-09-03 13:10:26 +0900940 .name = "renesas_spi",
Shimoda, Yoshihiro0b2182d2012-03-07 14:46:25 +0900941 .owner = THIS_MODULE,
942 },
943};
944module_platform_driver(rspi_driver);
945
946MODULE_DESCRIPTION("Renesas RSPI bus driver");
947MODULE_LICENSE("GPL v2");
948MODULE_AUTHOR("Yoshihiro Shimoda");
949MODULE_ALIAS("platform:rspi");