4 * Copyright (C) 2012 Renesas Solutions Corp.
7 * Copyright (C) 2011 Renesas Solutions Corp.
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.
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.
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
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>
33 #include <linux/clk.h>
34 #include <linux/dmaengine.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/sh_dma.h>
37 #include <linux/spi/spi.h>
38 #include <linux/spi/rspi.h>
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
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
71 #define SPCR_SPRIE 0x80
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
81 #define SSLP_SSL1P 0x02
82 #define SSLP_SSL0P 0x01
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
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
100 #define SPSCR_SPSLN_MASK 0x07
103 #define SPSSR_SPECM_MASK 0x70
104 #define SPSSR_SPCP_MASK 0x07
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
116 #define SPCKD_SCKDL_MASK 0x07
119 #define SSLND_SLNDL_MASK 0x07
122 #define SPND_SPNDL_MASK 0x07
125 #define SPCR2_PTE 0x08
126 #define SPCR2_SPIE 0x04
127 #define SPCR2_SPOE 0x02
128 #define SPCR2_SPPE 0x01
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)
137 #define SPCMD_SPB_8BIT 0x0000 /* qspi only */
138 #define SPCMD_SPB_16BIT 0x0100
139 #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
149 #define SPBFCR_TXRST 0x80 /* qspi only */
150 #define SPBFCR_RXRST 0x40 /* qspi only */
152 #define DUMMY_DATA 0x00
157 struct spi_master *master;
158 struct list_head queue;
159 struct work_struct ws;
160 wait_queue_head_t wait;
164 const struct spi_ops *ops;
167 struct dma_chan *chan_tx;
168 struct dma_chan *chan_rx;
171 unsigned dma_width_16bit:1;
172 unsigned dma_callbacked:1;
175 static void rspi_write8(const struct rspi_data *rspi, u8 data, u16 offset)
177 iowrite8(data, rspi->addr + offset);
180 static void rspi_write16(const struct rspi_data *rspi, u16 data, u16 offset)
182 iowrite16(data, rspi->addr + offset);
185 static void rspi_write32(const struct rspi_data *rspi, u32 data, u16 offset)
187 iowrite32(data, rspi->addr + offset);
190 static u8 rspi_read8(const struct rspi_data *rspi, u16 offset)
192 return ioread8(rspi->addr + offset);
195 static u16 rspi_read16(const struct rspi_data *rspi, u16 offset)
197 return ioread16(rspi->addr + offset);
200 /* optional functions */
202 int (*set_config_register)(const struct rspi_data *rspi,
204 int (*send_pio)(struct rspi_data *rspi, struct spi_message *mesg,
205 struct spi_transfer *t);
206 int (*receive_pio)(struct rspi_data *rspi, struct spi_message *mesg,
207 struct spi_transfer *t);
214 static int rspi_set_config_register(const struct rspi_data *rspi,
219 /* Sets output mode(CMOS) and MOSI signal(from previous transfer) */
220 rspi_write8(rspi, 0x00, RSPI_SPPCR);
222 /* Sets transfer bit rate */
223 spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz) - 1;
224 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
226 /* Sets number of frames to be used: 1 frame */
227 rspi_write8(rspi, 0x00, RSPI_SPDCR);
229 /* Sets RSPCK, SSL, next-access delay value */
230 rspi_write8(rspi, 0x00, RSPI_SPCKD);
231 rspi_write8(rspi, 0x00, RSPI_SSLND);
232 rspi_write8(rspi, 0x00, RSPI_SPND);
234 /* Sets parity, interrupt mask */
235 rspi_write8(rspi, 0x00, RSPI_SPCR2);
238 rspi_write16(rspi, SPCMD_SPB_8_TO_16(access_size) | SPCMD_SSLKP,
242 rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
250 static int qspi_set_config_register(const struct rspi_data *rspi,
256 /* Sets output mode(CMOS) and MOSI signal(from previous transfer) */
257 rspi_write8(rspi, 0x00, RSPI_SPPCR);
259 /* Sets transfer bit rate */
260 spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz);
261 rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
263 /* Sets number of frames to be used: 1 frame */
264 rspi_write8(rspi, 0x00, RSPI_SPDCR);
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);
271 /* Data Length Setting */
272 if (access_size == 8)
273 spcmd = SPCMD_SPB_8BIT;
274 else if (access_size == 16)
275 spcmd = SPCMD_SPB_16BIT;
276 else if (access_size == 32)
277 spcmd = SPCMD_SPB_32BIT;
279 spcmd |= SPCMD_SCKDEN | SPCMD_SLNDEN | SPCMD_SSLKP | SPCMD_SPNDEN;
281 /* Resets transfer data length */
282 rspi_write32(rspi, 0, QSPI_SPBMUL0);
284 /* Resets transmit and receive buffer */
285 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
286 /* Sets buffer to allow normal operation */
287 rspi_write8(rspi, 0x00, QSPI_SPBFCR);
290 rspi_write16(rspi, spcmd, RSPI_SPCMD0);
292 /* Enables SPI function in a master mode */
293 rspi_write8(rspi, SPCR_SPE | SPCR_MSTR, RSPI_SPCR);
298 #define set_config_register(spi, n) spi->ops->set_config_register(spi, n)
300 static void rspi_enable_irq(const struct rspi_data *rspi, u8 enable)
302 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | enable, RSPI_SPCR);
305 static void rspi_disable_irq(const struct rspi_data *rspi, u8 disable)
307 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~disable, RSPI_SPCR);
310 static int rspi_wait_for_interrupt(struct rspi_data *rspi, u8 wait_mask,
315 rspi->spsr = rspi_read8(rspi, RSPI_SPSR);
316 rspi_enable_irq(rspi, enable_bit);
317 ret = wait_event_timeout(rspi->wait, rspi->spsr & wait_mask, HZ);
318 if (ret == 0 && !(rspi->spsr & wait_mask))
324 static void rspi_assert_ssl(const struct rspi_data *rspi)
326 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_SPE, RSPI_SPCR);
329 static void rspi_negate_ssl(const struct rspi_data *rspi)
331 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_SPE, RSPI_SPCR);
334 static int rspi_send_pio(struct rspi_data *rspi, struct spi_message *mesg,
335 struct spi_transfer *t)
338 const u8 *data = t->tx_buf;
340 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_TXMD,
343 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
344 dev_err(&rspi->master->dev,
345 "%s: tx empty timeout\n", __func__);
349 rspi_write16(rspi, *data, RSPI_SPDR);
354 /* Waiting for the last transmition */
355 rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
360 static int qspi_send_pio(struct rspi_data *rspi, struct spi_message *mesg,
361 struct spi_transfer *t)
364 const u8 *data = t->tx_buf;
366 rspi_write8(rspi, SPBFCR_TXRST, QSPI_SPBFCR);
367 rspi_write8(rspi, 0x00, QSPI_SPBFCR);
371 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
372 dev_err(&rspi->master->dev,
373 "%s: tx empty timeout\n", __func__);
376 rspi_write8(rspi, *data++, RSPI_SPDR);
378 if (rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE) < 0) {
379 dev_err(&rspi->master->dev,
380 "%s: receive timeout\n", __func__);
383 rspi_read8(rspi, RSPI_SPDR);
388 /* Waiting for the last transmition */
389 rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
394 #define send_pio(spi, mesg, t) spi->ops->send_pio(spi, mesg, t)
396 static void rspi_dma_complete(void *arg)
398 struct rspi_data *rspi = arg;
400 rspi->dma_callbacked = 1;
401 wake_up_interruptible(&rspi->wait);
404 static int rspi_dma_map_sg(struct scatterlist *sg, const void *buf,
405 unsigned len, struct dma_chan *chan,
406 enum dma_transfer_direction dir)
408 sg_init_table(sg, 1);
409 sg_set_buf(sg, buf, len);
410 sg_dma_len(sg) = len;
411 return dma_map_sg(chan->device->dev, sg, 1, dir);
414 static void rspi_dma_unmap_sg(struct scatterlist *sg, struct dma_chan *chan,
415 enum dma_transfer_direction dir)
417 dma_unmap_sg(chan->device->dev, sg, 1, dir);
420 static void rspi_memory_to_8bit(void *buf, const void *data, unsigned len)
423 const u8 *src = data;
426 *dst++ = (u16)(*src++);
431 static void rspi_memory_from_8bit(void *buf, const void *data, unsigned len)
434 const u16 *src = data;
442 static int rspi_send_dma(struct rspi_data *rspi, struct spi_transfer *t)
444 struct scatterlist sg;
445 const void *buf = NULL;
446 struct dma_async_tx_descriptor *desc;
450 if (rspi->dma_width_16bit) {
453 * If DMAC bus width is 16-bit, the driver allocates a dummy
454 * buffer. And, the driver converts original data into the
455 * DMAC data as the following format:
456 * original data: 1st byte, 2nd byte ...
457 * DMAC data: 1st byte, dummy, 2nd byte, dummy ...
460 tmp = kmalloc(len, GFP_KERNEL);
463 rspi_memory_to_8bit(tmp, t->tx_buf, t->len);
470 if (!rspi_dma_map_sg(&sg, buf, len, rspi->chan_tx, DMA_TO_DEVICE)) {
474 desc = dmaengine_prep_slave_sg(rspi->chan_tx, &sg, 1, DMA_TO_DEVICE,
475 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
482 * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be
483 * called. So, this driver disables the IRQ while DMA transfer.
485 disable_irq(rspi->irq);
487 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_TXMD, RSPI_SPCR);
488 rspi_enable_irq(rspi, SPCR_SPTIE);
489 rspi->dma_callbacked = 0;
491 desc->callback = rspi_dma_complete;
492 desc->callback_param = rspi;
493 dmaengine_submit(desc);
494 dma_async_issue_pending(rspi->chan_tx);
496 ret = wait_event_interruptible_timeout(rspi->wait,
497 rspi->dma_callbacked, HZ);
498 if (ret > 0 && rspi->dma_callbacked)
502 rspi_disable_irq(rspi, SPCR_SPTIE);
504 enable_irq(rspi->irq);
507 rspi_dma_unmap_sg(&sg, rspi->chan_tx, DMA_TO_DEVICE);
509 if (rspi->dma_width_16bit)
515 static void rspi_receive_init(const struct rspi_data *rspi)
519 spsr = rspi_read8(rspi, RSPI_SPSR);
520 if (spsr & SPSR_SPRF)
521 rspi_read16(rspi, RSPI_SPDR); /* dummy read */
522 if (spsr & SPSR_OVRF)
523 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPSR) & ~SPSR_OVRF,
527 static int rspi_receive_pio(struct rspi_data *rspi, struct spi_message *mesg,
528 struct spi_transfer *t)
533 rspi_receive_init(rspi);
537 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_TXMD,
540 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
541 dev_err(&rspi->master->dev,
542 "%s: tx empty timeout\n", __func__);
545 /* dummy write for generate clock */
546 rspi_write16(rspi, DUMMY_DATA, RSPI_SPDR);
548 if (rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE) < 0) {
549 dev_err(&rspi->master->dev,
550 "%s: receive timeout\n", __func__);
553 /* SPDR allows 16 or 32-bit access only */
554 *data = (u8)rspi_read16(rspi, RSPI_SPDR);
563 static void qspi_receive_init(const struct rspi_data *rspi)
567 spsr = rspi_read8(rspi, RSPI_SPSR);
568 if (spsr & SPSR_SPRF)
569 rspi_read8(rspi, RSPI_SPDR); /* dummy read */
570 rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
571 rspi_write8(rspi, 0x00, QSPI_SPBFCR);
574 static int qspi_receive_pio(struct rspi_data *rspi, struct spi_message *mesg,
575 struct spi_transfer *t)
580 qspi_receive_init(rspi);
585 if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) {
586 dev_err(&rspi->master->dev,
587 "%s: tx empty timeout\n", __func__);
590 /* dummy write for generate clock */
591 rspi_write8(rspi, DUMMY_DATA, RSPI_SPDR);
593 if (rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE) < 0) {
594 dev_err(&rspi->master->dev,
595 "%s: receive timeout\n", __func__);
598 /* SPDR allows 8, 16 or 32-bit access */
599 *data++ = rspi_read8(rspi, RSPI_SPDR);
606 #define receive_pio(spi, mesg, t) spi->ops->receive_pio(spi, mesg, t)
608 static int rspi_receive_dma(struct rspi_data *rspi, struct spi_transfer *t)
610 struct scatterlist sg, sg_dummy;
611 void *dummy = NULL, *rx_buf = NULL;
612 struct dma_async_tx_descriptor *desc, *desc_dummy;
616 if (rspi->dma_width_16bit) {
618 * If DMAC bus width is 16-bit, the driver allocates a dummy
619 * buffer. And, finally the driver converts the DMAC data into
620 * actual data as the following format:
621 * DMAC data: 1st byte, dummy, 2nd byte, dummy ...
622 * actual data: 1st byte, 2nd byte ...
625 rx_buf = kmalloc(len, GFP_KERNEL);
633 /* prepare dummy transfer to generate SPI clocks */
634 dummy = kzalloc(len, GFP_KERNEL);
639 if (!rspi_dma_map_sg(&sg_dummy, dummy, len, rspi->chan_tx,
644 desc_dummy = dmaengine_prep_slave_sg(rspi->chan_tx, &sg_dummy, 1,
645 DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
648 goto end_dummy_mapped;
651 /* prepare receive transfer */
652 if (!rspi_dma_map_sg(&sg, rx_buf, len, rspi->chan_rx,
655 goto end_dummy_mapped;
658 desc = dmaengine_prep_slave_sg(rspi->chan_rx, &sg, 1, DMA_FROM_DEVICE,
659 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
665 rspi_receive_init(rspi);
668 * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be
669 * called. So, this driver disables the IRQ while DMA transfer.
671 disable_irq(rspi->irq);
673 rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_TXMD, RSPI_SPCR);
674 rspi_enable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE);
675 rspi->dma_callbacked = 0;
677 desc->callback = rspi_dma_complete;
678 desc->callback_param = rspi;
679 dmaengine_submit(desc);
680 dma_async_issue_pending(rspi->chan_rx);
682 desc_dummy->callback = NULL; /* No callback */
683 dmaengine_submit(desc_dummy);
684 dma_async_issue_pending(rspi->chan_tx);
686 ret = wait_event_interruptible_timeout(rspi->wait,
687 rspi->dma_callbacked, HZ);
688 if (ret > 0 && rspi->dma_callbacked)
692 rspi_disable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE);
694 enable_irq(rspi->irq);
697 rspi_dma_unmap_sg(&sg, rspi->chan_rx, DMA_FROM_DEVICE);
699 rspi_dma_unmap_sg(&sg_dummy, rspi->chan_tx, DMA_TO_DEVICE);
701 if (rspi->dma_width_16bit) {
703 rspi_memory_from_8bit(t->rx_buf, rx_buf, t->len);
711 static int rspi_is_dma(const struct rspi_data *rspi, struct spi_transfer *t)
713 if (t->tx_buf && rspi->chan_tx)
715 /* If the module receives data by DMAC, it also needs TX DMAC */
716 if (t->rx_buf && rspi->chan_tx && rspi->chan_rx)
722 static void rspi_work(struct work_struct *work)
724 struct rspi_data *rspi = container_of(work, struct rspi_data, ws);
725 struct spi_message *mesg;
726 struct spi_transfer *t;
731 spin_lock_irqsave(&rspi->lock, flags);
732 if (list_empty(&rspi->queue)) {
733 spin_unlock_irqrestore(&rspi->lock, flags);
736 mesg = list_entry(rspi->queue.next, struct spi_message, queue);
737 list_del_init(&mesg->queue);
738 spin_unlock_irqrestore(&rspi->lock, flags);
740 rspi_assert_ssl(rspi);
742 list_for_each_entry(t, &mesg->transfers, transfer_list) {
744 if (rspi_is_dma(rspi, t))
745 ret = rspi_send_dma(rspi, t);
747 ret = send_pio(rspi, mesg, t);
752 if (rspi_is_dma(rspi, t))
753 ret = rspi_receive_dma(rspi, t);
755 ret = receive_pio(rspi, mesg, t);
759 mesg->actual_length += t->len;
761 rspi_negate_ssl(rspi);
764 mesg->complete(mesg->context);
771 mesg->complete(mesg->context);
774 static int rspi_setup(struct spi_device *spi)
776 struct rspi_data *rspi = spi_master_get_devdata(spi->master);
778 if (!spi->bits_per_word)
779 spi->bits_per_word = 8;
780 rspi->max_speed_hz = spi->max_speed_hz;
782 set_config_register(rspi, 8);
787 static int rspi_transfer(struct spi_device *spi, struct spi_message *mesg)
789 struct rspi_data *rspi = spi_master_get_devdata(spi->master);
792 mesg->actual_length = 0;
793 mesg->status = -EINPROGRESS;
795 spin_lock_irqsave(&rspi->lock, flags);
796 list_add_tail(&mesg->queue, &rspi->queue);
797 schedule_work(&rspi->ws);
798 spin_unlock_irqrestore(&rspi->lock, flags);
803 static void rspi_cleanup(struct spi_device *spi)
807 static irqreturn_t rspi_irq(int irq, void *_sr)
809 struct rspi_data *rspi = _sr;
811 irqreturn_t ret = IRQ_NONE;
814 rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
815 if (spsr & SPSR_SPRF)
816 disable_irq |= SPCR_SPRIE;
817 if (spsr & SPSR_SPTEF)
818 disable_irq |= SPCR_SPTIE;
822 rspi_disable_irq(rspi, disable_irq);
823 wake_up(&rspi->wait);
829 static int rspi_request_dma(struct rspi_data *rspi,
830 struct platform_device *pdev)
832 const struct rspi_plat_data *rspi_pd = dev_get_platdata(&pdev->dev);
833 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
835 struct dma_slave_config cfg;
838 if (!res || !rspi_pd)
839 return 0; /* The driver assumes no error. */
841 rspi->dma_width_16bit = rspi_pd->dma_width_16bit;
843 /* If the module receives data by DMAC, it also needs TX DMAC */
844 if (rspi_pd->dma_rx_id && rspi_pd->dma_tx_id) {
846 dma_cap_set(DMA_SLAVE, mask);
847 rspi->chan_rx = dma_request_channel(mask, shdma_chan_filter,
848 (void *)rspi_pd->dma_rx_id);
850 cfg.slave_id = rspi_pd->dma_rx_id;
851 cfg.direction = DMA_DEV_TO_MEM;
853 cfg.src_addr = res->start + RSPI_SPDR;
854 ret = dmaengine_slave_config(rspi->chan_rx, &cfg);
856 dev_info(&pdev->dev, "Use DMA when rx.\n");
861 if (rspi_pd->dma_tx_id) {
863 dma_cap_set(DMA_SLAVE, mask);
864 rspi->chan_tx = dma_request_channel(mask, shdma_chan_filter,
865 (void *)rspi_pd->dma_tx_id);
867 cfg.slave_id = rspi_pd->dma_tx_id;
868 cfg.direction = DMA_MEM_TO_DEV;
869 cfg.dst_addr = res->start + RSPI_SPDR;
871 ret = dmaengine_slave_config(rspi->chan_tx, &cfg);
873 dev_info(&pdev->dev, "Use DMA when tx\n");
882 static void rspi_release_dma(struct rspi_data *rspi)
885 dma_release_channel(rspi->chan_tx);
887 dma_release_channel(rspi->chan_rx);
890 static int rspi_remove(struct platform_device *pdev)
892 struct rspi_data *rspi = spi_master_get(platform_get_drvdata(pdev));
894 spi_unregister_master(rspi->master);
895 rspi_release_dma(rspi);
896 free_irq(platform_get_irq(pdev, 0), rspi);
899 spi_master_put(rspi->master);
904 static int rspi_probe(struct platform_device *pdev)
906 struct resource *res;
907 struct spi_master *master;
908 struct rspi_data *rspi;
911 const struct rspi_plat_data *rspi_pd = dev_get_platdata(&pdev->dev);
912 const struct spi_ops *ops;
913 const struct platform_device_id *id_entry = pdev->id_entry;
915 ops = (struct spi_ops *)id_entry->driver_data;
916 /* ops parameter check */
917 if (!ops->set_config_register) {
918 dev_err(&pdev->dev, "there is no set_config_register\n");
922 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
923 if (unlikely(res == NULL)) {
924 dev_err(&pdev->dev, "invalid resource\n");
928 irq = platform_get_irq(pdev, 0);
930 dev_err(&pdev->dev, "platform_get_irq error\n");
934 master = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data));
935 if (master == NULL) {
936 dev_err(&pdev->dev, "spi_alloc_master error.\n");
940 rspi = spi_master_get_devdata(master);
941 platform_set_drvdata(pdev, rspi);
943 rspi->master = master;
944 rspi->addr = ioremap(res->start, resource_size(res));
945 if (rspi->addr == NULL) {
946 dev_err(&pdev->dev, "ioremap error.\n");
951 snprintf(clk_name, sizeof(clk_name), "%s%d", id_entry->name, pdev->id);
952 rspi->clk = clk_get(&pdev->dev, clk_name);
953 if (IS_ERR(rspi->clk)) {
954 dev_err(&pdev->dev, "cannot get clock\n");
955 ret = PTR_ERR(rspi->clk);
958 clk_enable(rspi->clk);
960 INIT_LIST_HEAD(&rspi->queue);
961 spin_lock_init(&rspi->lock);
962 INIT_WORK(&rspi->ws, rspi_work);
963 init_waitqueue_head(&rspi->wait);
965 master->num_chipselect = rspi_pd->num_chipselect;
966 if (!master->num_chipselect)
967 master->num_chipselect = 2; /* default */
969 master->bus_num = pdev->id;
970 master->setup = rspi_setup;
971 master->transfer = rspi_transfer;
972 master->cleanup = rspi_cleanup;
974 ret = request_irq(irq, rspi_irq, 0, dev_name(&pdev->dev), rspi);
976 dev_err(&pdev->dev, "request_irq error\n");
981 ret = rspi_request_dma(rspi, pdev);
983 dev_err(&pdev->dev, "rspi_request_dma failed.\n");
987 ret = spi_register_master(master);
989 dev_err(&pdev->dev, "spi_register_master error.\n");
993 dev_info(&pdev->dev, "probed\n");
998 rspi_release_dma(rspi);
1003 iounmap(rspi->addr);
1005 spi_master_put(master);
1010 static struct spi_ops rspi_ops = {
1011 .set_config_register = rspi_set_config_register,
1012 .send_pio = rspi_send_pio,
1013 .receive_pio = rspi_receive_pio,
1016 static struct spi_ops qspi_ops = {
1017 .set_config_register = qspi_set_config_register,
1018 .send_pio = qspi_send_pio,
1019 .receive_pio = qspi_receive_pio,
1022 static struct platform_device_id spi_driver_ids[] = {
1023 { "rspi", (kernel_ulong_t)&rspi_ops },
1024 { "qspi", (kernel_ulong_t)&qspi_ops },
1028 MODULE_DEVICE_TABLE(platform, spi_driver_ids);
1030 static struct platform_driver rspi_driver = {
1031 .probe = rspi_probe,
1032 .remove = rspi_remove,
1033 .id_table = spi_driver_ids,
1035 .name = "renesas_spi",
1036 .owner = THIS_MODULE,
1039 module_platform_driver(rspi_driver);
1041 MODULE_DESCRIPTION("Renesas RSPI bus driver");
1042 MODULE_LICENSE("GPL v2");
1043 MODULE_AUTHOR("Yoshihiro Shimoda");
1044 MODULE_ALIAS("platform:rspi");