1 --- a/drivers/spi/Kconfig
2 +++ b/drivers/spi/Kconfig
3 @@ -236,6 +236,39 @@ config SPI_XILINX
4 See the "OPB Serial Peripheral Interface (SPI) (v1.00e)"
5 Product Specification document (DS464) for hardware details.
8 + tristate "CNS3XXX SPI controller"
9 + depends on ARCH_CNS3XXX && SPI_MASTER && EXPERIMENTAL
12 + This enables using the CNS3XXX SPI controller in master
15 +config SPI_CNS3XXX_DEBUG
16 + boolean "Debug support for CNS3XXX SPI drivers"
17 + depends on SPI_CNS3XXX
19 + Say "yes" to enable debug messaging
21 +config SPI_CNS3XXX_2IOREAD
22 + bool "CNS3XXX SPI 2 IO Read Mode"
23 + depends on SPI_CNS3XXX
25 + This enables 2 IO Read Mode
27 +config SPI_CNS3XXX_USEDMA
28 + bool "CNS3XXX SPI DMA Mode"
29 + depends on SPI_CNS3XXX
32 + This enables DMA Mode
34 +config SPI_CNS3XXX_USEDMA_DEBUG
35 + boolean "Debug support for CNS3XXX SPI DMA drivers"
36 + depends on SPI_CNS3XXX_USEDMA
38 + Say "yes" to enable debug messaging
41 # Add new SPI master controllers in alphabetical order above this line
43 --- a/drivers/spi/Makefile
44 +++ b/drivers/spi/Makefile
45 @@ -32,6 +32,7 @@ obj-$(CONFIG_SPI_S3C24XX) += spi_s3c24x
46 obj-$(CONFIG_SPI_TXX9) += spi_txx9.o
47 obj-$(CONFIG_SPI_XILINX) += xilinx_spi.o
48 obj-$(CONFIG_SPI_SH_SCI) += spi_sh_sci.o
49 +obj-$(CONFIG_SPI_CNS3XXX) += spi_cns3xxx.o
50 # ... add above this line ...
52 # SPI protocol drivers (device/link on bus)
53 --- a/drivers/spi/spi_bitbang.c
54 +++ b/drivers/spi/spi_bitbang.c
55 @@ -334,6 +334,14 @@ static void bitbang_work(struct work_str
57 if (!m->is_dma_mapped)
58 t->rx_dma = t->tx_dma = 0;
60 +#ifdef CONFIG_ARCH_CNS3XXX
61 + if (t->transfer_list.next == &m->transfers) {
62 + t->last_in_message_list = 1;
64 + t->last_in_message_list = 0;
67 status = bitbang->txrx_bufs(spi, t);
70 --- a/drivers/spi/spi.c
71 +++ b/drivers/spi/spi.c
72 @@ -769,6 +769,89 @@ int spi_write_then_read(struct spi_devic
74 EXPORT_SYMBOL_GPL(spi_write_then_read);
76 +#ifdef CONFIG_ARCH_CNS3XXX
78 + * spi_write_read_sync - SPI synchronous write & read
79 + * @spi: device with which data will be exchanged
80 + * @txbuf: data to be written (need not be dma-safe)
81 + * @n_tx: size of txbuf, in bytes
82 + * @rxbuf: buffer into which data will be read
83 + * @n_rx: size of rxbuf, in bytes (need not be dma-safe)
85 + * This performs a half duplex MicroWire style transaction with the
86 + * device, sending txbuf and then reading rxbuf. The return value
87 + * is zero for success, else a negative errno status code.
88 + * This call may only be used from a context that may sleep.
90 + * Parameters to this routine are always copied using a small buffer;
91 + * performance-sensitive or bulk transfer code should instead use
92 + * spi_{async,sync}() calls with dma-safe buffers.
94 +int spi_write_read_sync(struct spi_device *spi,
95 + const u8 *txbuf, unsigned n_tx,
96 + u8 *rxbuf, unsigned n_rx)
98 + static DECLARE_MUTEX(lock);
101 + struct spi_message message;
102 + struct spi_transfer x;
105 + /* Use preallocated DMA-safe buffer. We can't avoid copying here,
106 + * (as a pure convenience thing), but we can keep heap costs
107 + * out of the hot path ...
110 + while (!str8131_spi_bus_idle()){
111 + printk("spi bus is not idle \n"); // do nothing
113 + while (!str8131_spi_tx_buffer_empty()){
114 + printk("spi tx buffer is not empty \n"); // do nothing
117 + if ((n_tx + n_rx) > SPI_BUFSIZ)
119 + spi_message_init(&message);
120 + memset(&x, 0, sizeof x);
122 + spi_message_add_tail(&x, &message);
124 + /* ... unless someone else is using the pre-allocated buffer */
125 + if (down_trylock(&lock)) {
126 + local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
132 + memcpy(local_buf, txbuf, n_tx);
133 + x.tx_buf = local_buf;
134 + x.rx_buf = local_buf + n_tx;
137 + status = spi_sync(spi, &message);
139 + memcpy(rxbuf, x.rx_buf, n_rx);
140 + status = message.status;
143 + if (x.tx_buf == buf)
151 +EXPORT_SYMBOL_GPL(spi_write_read_sync);
152 +#endif /* CONFIG_ARCH_CNS3XXX */
159 /*-------------------------------------------------------------------------*/
161 static int __init spi_init(void)
163 +++ b/drivers/spi/spi_cns3xxx.c
165 +/*******************************************************************************
167 + * CNS3XXX SPI controller driver (master mode only)
169 + * Copyright (c) 2008 Cavium Networks
171 + * This file is free software; you can redistribute it and/or modify
172 + * it under the terms of the GNU General Public License, Version 2, as
173 + * published by the Free Software Foundation.
175 + * This file is distributed in the hope that it will be useful,
176 + * but AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty of
177 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
178 + * NONINFRINGEMENT. See the GNU General Public License for more details.
180 + * You should have received a copy of the GNU General Public License
181 + * along with this file; if not, write to the Free Software
182 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA or
183 + * visit http://www.gnu.org/licenses/.
185 + * This file may also be available under a different license from Cavium.
186 + * Contact Cavium Networks for more information
188 + ******************************************************************************/
190 +#include <linux/init.h>
191 +#include <linux/spinlock.h>
192 +#include <linux/workqueue.h>
193 +#include <linux/interrupt.h>
194 +#include <linux/delay.h>
195 +#include <linux/errno.h>
196 +#include <linux/err.h>
197 +#include <linux/clk.h>
198 +#include <linux/platform_device.h>
200 +#include <linux/spi/spi.h>
201 +#include <linux/spi/spi_bitbang.h>
202 +#include <linux/mtd/partitions.h>
203 +#include <linux/dma-mapping.h>
206 +#include <asm/memory.h>
207 +#include <asm/dma.h>
208 +#include <asm/delay.h>
209 +#include <mach/board.h>
210 +#include <mach/dmac.h>
211 +#include <linux/module.h>
212 +#include <mach/misc.h>
213 +#include <mach/gpio.h>
214 +#include <mach/pm.h>
216 +#define LE8221_SPI_CS 1
217 +#define SI3226_SPI_CS 1
219 +#define CNS3XXX_SPI_INTERRUPT
220 +#undef CNS3XXX_SPI_INTERRUPT /* Interrupt is not supported for D2 and SEN */
223 + * define access macros
225 +#define SPI_MEM_MAP_VALUE(reg_offset) (*((u32 volatile *)(CNS3XXX_SSP_BASE_VIRT + reg_offset)))
227 +#define SPI_CONFIGURATION_REG SPI_MEM_MAP_VALUE(0x40)
228 +#define SPI_SERVICE_STATUS_REG SPI_MEM_MAP_VALUE(0x44)
229 +#define SPI_BIT_RATE_CONTROL_REG SPI_MEM_MAP_VALUE(0x48)
230 +#define SPI_TRANSMIT_CONTROL_REG SPI_MEM_MAP_VALUE(0x4C)
231 +#define SPI_TRANSMIT_BUFFER_REG SPI_MEM_MAP_VALUE(0x50)
232 +#define SPI_RECEIVE_CONTROL_REG SPI_MEM_MAP_VALUE(0x54)
233 +#define SPI_RECEIVE_BUFFER_REG SPI_MEM_MAP_VALUE(0x58)
234 +#define SPI_FIFO_TRANSMIT_CONFIG_REG SPI_MEM_MAP_VALUE(0x5C)
235 +#define SPI_FIFO_TRANSMIT_CONTROL_REG SPI_MEM_MAP_VALUE(0x60)
236 +#define SPI_FIFO_RECEIVE_CONFIG_REG SPI_MEM_MAP_VALUE(0x64)
237 +#define SPI_INTERRUPT_STATUS_REG SPI_MEM_MAP_VALUE(0x68)
238 +#define SPI_INTERRUPT_ENABLE_REG SPI_MEM_MAP_VALUE(0x6C)
240 +#define SPI_TRANSMIT_BUFFER_REG_ADDR (CNS3XXX_SSP_BASE +0x50)
241 +#define SPI_RECEIVE_BUFFER_REG_ADDR (CNS3XXX_SSP_BASE +0x58)
243 +/* Structure for SPI controller of CNS3XXX SOCs */
244 +struct cns3xxx_spi {
245 + /* bitbang has to be first */
246 + struct spi_bitbang bitbang;
247 + struct completion done;
248 + wait_queue_head_t wait;
252 + int last_in_message_list;
255 + const unsigned char *tx;
258 + struct spi_master *master;
259 + struct platform_device *pdev;
260 + struct device *dev;
263 +static inline u8 cns3xxx_spi_bus_idle(void)
265 + return ((SPI_SERVICE_STATUS_REG & 0x1) ? 0 : 1);
268 +static inline u8 cns3xxx_spi_tx_buffer_empty(void)
270 + return ((SPI_INTERRUPT_STATUS_REG & (0x1 << 3)) ? 1 : 0);
273 +static inline u8 cns3xxx_spi_rx_buffer_full(void)
275 + return ((SPI_INTERRUPT_STATUS_REG & (0x1 << 2)) ? 1 : 0);
278 +u8 cns3xxx_spi_tx_rx(u8 tx_channel, u8 tx_eof, u32 tx_data,
284 + while (!cns3xxx_spi_bus_idle()) ; // do nothing
286 + while (!cns3xxx_spi_tx_buffer_empty()) ; // do nothing
288 + SPI_TRANSMIT_CONTROL_REG &= ~(0x7);
289 + SPI_TRANSMIT_CONTROL_REG |= (tx_channel & 0x3) | ((tx_eof & 0x1) << 2);
291 + SPI_TRANSMIT_BUFFER_REG = tx_data;
293 + while (!cns3xxx_spi_rx_buffer_full()) ; // do nothing
295 + rx_channel = SPI_RECEIVE_CONTROL_REG & 0x3;
296 + rx_eof = (SPI_RECEIVE_CONTROL_REG & (0x1 << 2)) ? 1 : 0;
298 + *rx_data = SPI_RECEIVE_BUFFER_REG;
300 + if ((tx_channel != rx_channel) || (tx_eof != rx_eof)) {
307 +u8 cns3xxx_spi_tx(u8 tx_channel, u8 tx_eof, u32 tx_data)
310 + while (!cns3xxx_spi_bus_idle()) ; // do nothing
312 + while (!cns3xxx_spi_tx_buffer_empty()) ; // do nothing
314 + SPI_TRANSMIT_CONTROL_REG &= ~(0x7);
315 + SPI_TRANSMIT_CONTROL_REG |= (tx_channel & 0x3) | ((tx_eof & 0x1) << 2);
317 + SPI_TRANSMIT_BUFFER_REG = tx_data;
324 +#ifdef CONFIG_SPI_CNS3XXX_DEBUG
325 +static void spi_slave_probe(void)
328 + u32 rx_data1, rx_data2, rx_data3;
330 + cns3xxx_spi_tx_rx(0, 0, 0x9f, &rx_data1);
331 + cns3xxx_spi_tx_rx(0, 0, 0xff, &rx_data1);
332 + cns3xxx_spi_tx_rx(0, 0, 0xff, &rx_data2);
333 + cns3xxx_spi_tx_rx(0, 1, 0xff, &rx_data3);
334 + printk("[SPI_CNS3XXX_DEBUG] manufacturer: %x\n", rx_data1);
335 + printk("[SPI_CNS3XXX_DEBUG] device: %x\n",
336 + ((rx_data2 & 0xff) << 8) | (u16) (rx_data3 & 0xff));
338 + cns3xxx_spi_tx_rx(0, 0, 0x03, &rx_data1);
339 + cns3xxx_spi_tx_rx(0, 0, 0x00, &rx_data1);
340 + cns3xxx_spi_tx_rx(0, 0, 0x00, &rx_data1);
341 + cns3xxx_spi_tx_rx(0, 0, 0x00, &rx_data1);
342 + for (i = 0; i < 15; i++) {
343 + cns3xxx_spi_tx_rx(0, 0, 0xff, &rx_data1);
344 + printk("[SPI_CNS3XXX_DEBUG] flash[%02d]:0x%02x\n", i,
347 + cns3xxx_spi_tx_rx(0, 1, 0xff, &rx_data1);
348 + printk("[SPI_CNS3XXX_DEBUG] flash[%02d]:0x%02x\n", i, rx_data1 & 0xff);
351 +#define spi_slave_probe() do{}while(0)
354 +static inline struct cns3xxx_spi *to_hw(struct spi_device *sdev)
356 + return spi_master_get_devdata(sdev->master);
359 +static int cns3xxx_spi_setup_transfer(struct spi_device *spi,
360 + struct spi_transfer *t)
365 +static void cns3xxx_spi_chipselect(struct spi_device *spi, int value)
367 + unsigned int spi_config;
370 + case BITBANG_CS_INACTIVE:
373 + case BITBANG_CS_ACTIVE:
374 + spi_config = SPI_CONFIGURATION_REG;
376 + if (spi->mode & SPI_CPHA)
377 + spi_config |= (0x1 << 13);
379 + spi_config &= ~(0x1 << 13);
381 + if (spi->mode & SPI_CPOL)
382 + spi_config |= (0x1 << 14);
384 + spi_config &= ~(0x1 << 14);
386 + /* write new configration */
387 + SPI_CONFIGURATION_REG = spi_config;
389 + SPI_TRANSMIT_CONTROL_REG &= ~(0x7);
390 + SPI_TRANSMIT_CONTROL_REG |= (spi->chip_select & 0x3);
392 +#if defined(CONFIG_LE8221_CONTROL)
393 + if (spi->chip_select == LE8221_SPI_CS) {
394 + SPI_CONFIGURATION_REG |= (0x1 << 9);
396 +#elif defined (CONFIG_SI3226_CONTROL_API)
397 + if (spi->chip_select == SI3226_SPI_CS) {
398 + SPI_CONFIGURATION_REG &= ~(0x1 << 9);
405 +static int cns3xxx_spi_setup(struct spi_device *spi)
407 + if (!spi->bits_per_word)
408 + spi->bits_per_word = 8;
413 +#ifdef CONFIG_SPI_CNS3XXX_USEDMA
415 +int cns3xxx_spi_dma_irq_handler(void *pdata)
418 + struct cns3xxx_spi *hw = pdata;
419 + complete(&hw->done);
424 +static int cns3xxx_spi_dma_initialize(int *rxchan, int *txchan, int *rxevtno,
425 + int *txevtno, void *handlerargs)
427 + *rxchan = dmac_get_channel(cns3xxx_spi_dma_irq_handler, handlerargs);
428 + if ((*rxchan) == -1)
430 + *txchan = dmac_get_channel(NULL, NULL);
431 + if ((*txchan) == -1)
434 + if (dmac_get_event(*rxchan, *rxevtno) == -1)
437 + if (dmac_get_event(*txchan, *txevtno) == -1)
442 + dmac_release_event(*rxchan, *rxevtno);
444 + dmac_release_channel(*txchan);
446 + dmac_release_channel(*rxchan);
451 +static int cns3xxx_spi_start_dma(int rch, int tch, int rev, int tev,
452 + struct spi_transfer *t, struct cns3xxx_spi *hw)
454 + static void *dummy;
455 + static dma_addr_t dummy_dma;
456 + dma_addr_t rdma, tdma;
457 + int rx_inc, tx_inc;
458 + int lc0, totlps, lc1, rump;
462 + dummy = dma_alloc_coherent(NULL, 16, &dummy_dma, GFP_KERNEL);
463 +#ifdef CONFIG_SPI_CNS3XXX_DEBUG_DMA
464 + printk("Allocated Memory for dummy buffer va:%p,pa:%x\n", dummy,
471 + *((uint32_t *) dummy) = 0xffffffff;
473 + (t->tx_buf) ? (tdma = t->tx_dma, tx_inc = 1) :
474 + (tdma = dummy_dma, tx_inc = 0);
475 + (t->rx_buf) ? (rdma = t->rx_dma, rx_inc = 1) :
476 + (rdma = dummy_dma, rx_inc = 0);
478 +#ifdef CONFIG_SPI_CNS3XXX_DEBUG_DMA
479 + printk("Here with tdma %x, rdma %x\n", tdma, rdma);
484 + cns3xxx_spi_tx_rx(0,0,(t->tx_buf) ? hw->tx[0] : 0xff ,&rx_data);
486 + hw->rx[0] = rx_data & 0xff;
488 + cns3xxx_spi_dma_irq_handler(hw);
493 + totlps = t->len - 1 -1;
494 + if (totlps > 0x100) {
496 + lc1 = totlps / lc0;
497 + rump = totlps % lc0;
505 + cns3xxx_spi_tx(0,0,*((uint32_t *) t->tx_buf));
509 + cns3xxx_spi_tx(0,0,0xff);
512 + //SPI_RECEIVE_BUFFER_REG;
514 + DMAC_DMAMOV(tch, SAR, tdma);
515 + DMAC_DMAMOV(tch, DAR, SPI_TRANSMIT_BUFFER_REG_ADDR);
516 + DMAC_DMAMOV(tch, CCR,
517 + dmac_create_ctrlval(tx_inc, 1, 1, 0, 1, 1, 0));
518 + //DMAC_WFE(tch, rev);
520 + DMAC_DMALP(tch, 1, lc1);
521 + DMAC_DMALP(tch, 0, lc0);
522 + DMAC_WFE(tch, rev);
526 + DMAC_DMASEV(tch, tev);
527 + DMAC_DMALPEND(tch, 0,
528 + DMAWFE_INSTR_SIZE + DMASEV_INSTR_SIZE +
529 + DMAWMB_INSTR_SIZE + DMAST_INSTR_SIZE +
530 + DMALD_INSTR_SIZE, 1);
532 + DMAC_DMALPEND(tch, 1,
533 + DMALP_INSTR_SIZE + DMALPEND_INSTR_SIZE +
534 + DMAWFE_INSTR_SIZE + DMASEV_INSTR_SIZE +
535 + DMAWMB_INSTR_SIZE + DMAST_INSTR_SIZE +
536 + DMALD_INSTR_SIZE, 1);
539 + DMAC_DMALP(tch, 0, rump);
540 + DMAC_WFE(tch, rev);
544 + DMAC_DMASEV(tch, tev);
545 + DMAC_DMALPEND(tch, 0,
546 + DMAWFE_INSTR_SIZE + DMASEV_INSTR_SIZE +
547 + DMAWMB_INSTR_SIZE + DMAST_INSTR_SIZE +
548 + DMALD_INSTR_SIZE, 1);
556 + DMAC_DMAMOV(rch, SAR, SPI_RECEIVE_BUFFER_REG_ADDR);
557 + DMAC_DMAMOV(rch, DAR, rdma);
558 + DMAC_DMAMOV(rch, CCR,
559 + dmac_create_ctrlval(0, 1, 1, rx_inc, 1, 1, 0));
562 + DMAC_DMALP(rch, 1, lc1);
563 + DMAC_DMALP(rch, 0, lc0);
564 + DMAC_DMAWFP(rch, DMAC_SPI_PERIPH_ID, PERIPHERAL);
565 + DMAC_DMALDP(rch, DMAC_SPI_PERIPH_ID, 0);
568 + DMAC_DMASEV(rch, rev);
569 + DMAC_WFE(rch, tev);
570 + DMAC_DMALPEND(rch, 0,
571 + DMAWFE_INSTR_SIZE + DMASEV_INSTR_SIZE +
572 + DMAWMB_INSTR_SIZE + DMAST_INSTR_SIZE +
573 + DMALDP_INSTR_SIZE + DMAWFP_INSTR_SIZE, 1);
575 + DMAC_DMALPEND(rch, 1,
576 + DMAWFE_INSTR_SIZE +
577 + DMASEV_INSTR_SIZE + DMAWMB_INSTR_SIZE +
578 + DMAST_INSTR_SIZE + DMALDP_INSTR_SIZE +
579 + DMAWFP_INSTR_SIZE + DMALP_INSTR_SIZE +
580 + DMALPEND_INSTR_SIZE, 1);
584 + DMAC_DMALP(rch, 0, rump);
585 + DMAC_DMAWFP(rch, DMAC_SPI_PERIPH_ID, PERIPHERAL);
586 + DMAC_DMALDP(rch, DMAC_SPI_PERIPH_ID, 0);
589 + DMAC_DMASEV(rch, rev);
590 + DMAC_WFE(rch, tev);
591 + DMAC_DMALPEND(rch, 0,
592 + DMAWFE_INSTR_SIZE +
593 + DMASEV_INSTR_SIZE + DMAWMB_INSTR_SIZE +
594 + DMAST_INSTR_SIZE + DMALDP_INSTR_SIZE +
595 + DMAWFP_INSTR_SIZE, 1);
598 + DMAC_DMAWFP(rch, DMAC_SPI_PERIPH_ID, PERIPHERAL);
599 + DMAC_DMALDP(rch, DMAC_SPI_PERIPH_ID, 0);
603 + DMAC_DMAFLUSHP(rch, DMAC_SPI_PERIPH_ID);
604 + DMAC_DMASEV(rch, rch); // This will generate an interrupt
611 +static void cns3xxx_spi_dma_uninitialize(int rch, int tch, int revt, int tevt)
613 + dmac_release_event(rch, revt);
614 + dmac_release_event(tch, tevt);
615 + dmac_release_channel(rch);
616 + dmac_release_channel(tch);
620 +#endif /* CONFIG_SPI_CNS3XXX_USEDMA */
622 +static int cns3xxx_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
624 + struct cns3xxx_spi *hw = to_hw(spi);
625 +#ifdef CONFIG_SPI_CNS3XXX_USEDMA
626 + int spi_rxchan, spi_txchan, spi_rxevt, spi_txevt;
629 + dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n", t->tx_buf, t->rx_buf,
632 + hw->tx = t->tx_buf;
633 + hw->rx = t->rx_buf;
636 + hw->last_in_message_list = t->last_in_message_list;
638 +#ifdef CONFIG_SPI_CNS3XXX_USEDMA
639 + init_completion(&hw->done);
641 + if (cns3xxx_spi_dma_initialize
642 + (&spi_rxchan, &spi_txchan, &spi_rxevt, &spi_txevt, hw)) {
643 + dev_dbg(&spi->dev, "%s:%d Could not initialize DMA. \n",
644 + __FUNCTION__, __LINE__);
650 + dma_map_single(NULL, t->tx_buf, t->len, DMA_TO_DEVICE);
653 + dma_map_single(NULL, t->rx_buf, t->len, DMA_FROM_DEVICE);
655 + if (cns3xxx_spi_start_dma
656 + (spi_rxchan, spi_txchan, spi_rxevt, spi_txevt, t, hw)) {
657 + dev_dbg(&spi->dev, "Could not start DMA. \n");
659 + dma_unmap_single(NULL, t->tx_dma, t->len,
663 + dma_unmap_single(NULL, t->rx_dma, t->len,
666 + cns3xxx_spi_dma_uninitialize(spi_rxchan, spi_txchan, spi_rxevt,
671 + wait_for_completion(&hw->done);
673 + dev_dbg(&spi->dev, "DMA reported completion of transfer of %d bytes\n",
677 + dma_unmap_single(NULL, t->tx_dma, t->len, DMA_TO_DEVICE);
680 + dma_unmap_single(NULL, t->rx_dma, t->len, DMA_FROM_DEVICE);
682 + cns3xxx_spi_dma_uninitialize(spi_rxchan, spi_txchan, spi_rxevt,
685 + if (t->last_in_message_list)
686 + cns3xxx_spi_tx_rx(spi->chip_select, 1,
687 + (hw->tx) ? hw->tx[hw->len - 1] : 0xff,
690 + cns3xxx_spi_tx_rx(spi->chip_select, 0,
691 + (hw->tx) ? hw->tx[hw->len - 1] : 0xff,
695 + hw->rx[hw->len - 1] = rx_data & 0xff;
699 +#else /* !CONFIG_SPI_CNS3XXX_USEDMA */
701 +#ifdef CNS3XXX_SPI_INTERRUPT
703 + init_completion(&hw->done);
705 + /* Effectively, we are enabling only the Receive Buffer Interrupt Enable */
706 + /* TX Buf Underrun and RX Buf Overrun are not to happen */
707 + SPI_INTERRUPT_ENABLE_REG = (0x1 << 2);
708 +// (0x0) | (0x1 << 2) | (0x0 << 3) | (0x1 << 6) | (0x1 << 7);
710 + /* Write data and wait for completion */
711 + SPI_TRANSMIT_CONTROL_REG &= ~(0x7);
712 + SPI_TRANSMIT_CONTROL_REG |= (spi->chip_select & 0x3) |
713 + ((((hw->last_in_message_list) && (hw->len == 1)) ? 0x1 : 0x0) << 2);
715 + SPI_TRANSMIT_BUFFER_REG = (hw->tx) ? hw->tx[hw->count] : 0xff;
717 + wait_for_completion(&hw->done);
719 + SPI_INTERRUPT_ENABLE_REG = 0;
723 +#else /* !CNS3XXX_SPI_INTERRUPT */
725 + init_completion(&hw->done);
730 + for (i = 0; i < (hw->len - 1); i++) {
732 + "[SPI_CNS3XXX_DEBUG] hw->tx[%02d]: 0x%02x\n", i,
734 + cns3xxx_spi_tx_rx(spi->chip_select, 0, hw->tx[i],
737 + hw->rx[i] = rx_data;
739 + "[SPI_CNS3XXX_DEBUG] hw->rx[%02d]: 0x%02x\n",
744 + if (t->last_in_message_list) {
745 + cns3xxx_spi_tx_rx(spi->chip_select, 1, hw->tx[i],
748 + hw->rx[i] = rx_data;
750 + "[SPI_CNS3XXX_DEBUG] hw->rx[%02d]: 0x%02x\n",
754 + cns3xxx_spi_tx_rx(spi->chip_select, 0, hw->tx[i],
763 + for (i = 0; i < (hw->len - 1); i++) {
764 + cns3xxx_spi_tx_rx(spi->chip_select, 0, 0xff, &rx_data);
765 + hw->rx[i] = rx_data;
767 + "[SPI_CNS3XXX_DEBUG] hw->rx[%02d]: 0x%02x\n", i,
771 + if (t->last_in_message_list) {
772 + cns3xxx_spi_tx_rx(spi->chip_select, 1, 0xff, &rx_data);
774 + cns3xxx_spi_tx_rx(spi->chip_select, 0, 0xff, &rx_data);
776 + hw->rx[i] = rx_data;
777 + dev_dbg(&spi->dev, "[SPI_CNS3XXX_DEBUG] hw->rx[%02d]: 0x%02x\n",
783 +#endif /* CNS3XXX_SPI_INTERRUPT */
785 +#endif /* CONFIG_SPI_CNS3XXX_USEDMA */
788 +#ifdef CNS3XXX_SPI_INTERRUPT
789 +/* Driver supports single master only.
790 + * We have disabled fifo, so we wait for the receive buff full interrupt.
791 + * Receive Buff overrun, transmit buff underrun are not to happen
793 +static irqreturn_t cns3xxx_spi_irq(int irq, void *dev)
795 + struct cns3xxx_spi *hw = dev;
796 + uint32_t int_status;
798 + unsigned int count = hw->count;
800 + /* Read the interrupt status and clear interrupt */
801 + int_status = SPI_INTERRUPT_STATUS_REG;
803 + if (!(int_status & (0x1 << 2))) {
804 + printk("DEBUG THIS ! Unexpected interrupt (status = 0x%x)", int_status);
805 + /* Clearing spurious interrupts */
806 + SPI_INTERRUPT_STATUS_REG = (0xF << 4);
810 + /* Read to clear */
811 + data = SPI_RECEIVE_BUFFER_REG & 0xff;
814 + hw->rx[hw->count] = data;
820 + SPI_TRANSMIT_CONTROL_REG |=
821 + ((((hw->last_in_message_list) && (hw->len == 1)) ? 0x1 : 0x0) << 2);
822 + SPI_TRANSMIT_BUFFER_REG = (hw->tx) ? hw->tx[hw->count] : 0xff;
824 + complete(&hw->done);
828 + return IRQ_HANDLED;
832 +static void __init cns3xxx_spi_initial(void)
835 + /* share pin config. */
838 + /* GPIOB18 is set to PCM by default */
839 + MISC_GPIOB_PIN_ENABLE_REG &= ~(MISC_GSW_P0_CRS_PIN);
840 + gpio_direction_output(50, 1);
842 + PM_PLL_HM_PD_CTRL_REG &= ~(0x1 << 5);
843 + HAL_MISC_ENABLE_SPI_PINS();
844 + HAL_MISC_ENABLE_PCM_PINS(); /* this just for PCM test */
845 + cns3xxx_pwr_clk_en(CNS3XXX_PWR_CLK_EN(SPI_PCM_I2C));
846 + cns3xxx_pwr_soft_rst(CNS3XXX_PWR_SOFTWARE_RST(SPI_PCM_I2C));
849 + SPI_CONFIGURATION_REG = (((0x0 & 0x3) << 0) | /* 8bits shift length */
850 + (0x0 << 9) | /* SPI mode */
851 + (0x0 << 10) | /* disable FIFO */
852 + (0x1 << 11) | /* SPI master mode */
853 + (0x0 << 12) | /* disable SPI loopback mode */
854 + (0x1 << 13) | /* clock phase */
855 + (0x1 << 14) | /* clock polarity */
856 + (0x0 << 24) | /* disable - SPI data swap */
857 +#ifdef CONFIG_SPI_CNS3XXX_2IOREAD
858 + (0x1 << 29) | /* enable - 2IO Read mode */
860 + (0x0 << 29) | /* disablea - 2IO Read mode */
862 + (0x0 << 30) | /* disable - SPI high speed read for system boot up */
863 + (0x0 << 31)); /* disable - SPI */
865 + /* Set SPI bit rate PCLK/2 */
866 + SPI_BIT_RATE_CONTROL_REG = 0x1;
868 + /* Set SPI Tx channel 0 */
869 + SPI_TRANSMIT_CONTROL_REG = 0x0;
871 + /* Set Tx FIFO Threshold, Tx FIFO has 2 words */
872 + SPI_FIFO_TRANSMIT_CONFIG_REG &= ~(0x03 << 4);
873 + SPI_FIFO_TRANSMIT_CONFIG_REG |= ((0x0 & 0x03) << 4);
875 + /* Set Rx FIFO Threshold, Rx FIFO has 2 words */
876 + SPI_FIFO_RECEIVE_CONFIG_REG &= ~(0x03 << 4);
877 + SPI_FIFO_RECEIVE_CONFIG_REG |= ((0x0 & 0x03) << 4);
879 + /* Disable all interrupt */
880 + SPI_INTERRUPT_ENABLE_REG = 0x0;
882 + /* Clear spurious interrupt sources */
883 + SPI_INTERRUPT_STATUS_REG = (0x0F << 4);
886 + SPI_CONFIGURATION_REG |= (0x1 << 31);
891 +static int __init cns3xxx_spi_probe(struct platform_device *pdev)
893 + struct spi_master *master;
894 + struct cns3xxx_spi *hw;
897 + printk("%s: setup CNS3XXX SPI Controller", __FUNCTION__);
898 +#ifdef CONFIG_SPI_CNS3XXX_USEDMA
899 + printk(" w/ DMA \n");
901 +#ifdef CNS3XXX_SPI_INTERRUPT
902 + printk(" in Interrupt mode, w/o DMA \n");
904 + printk(" in polling mode, w/o DMA \n");
908 + /* share pin config. */
909 +// HAL_MISC_ENABLE_SPI_PINS();
911 + /* Allocate master with space for cns3xxx_spi */
912 + master = spi_alloc_master(&pdev->dev, sizeof(struct cns3xxx_spi));
913 + if (master == NULL) {
914 + dev_err(&pdev->dev, "No memory for spi_master\n");
919 + hw = spi_master_get_devdata(master);
920 + memset(hw, 0, sizeof(struct cns3xxx_spi));
922 + hw->master = spi_master_get(master);
923 + hw->dev = &pdev->dev;
925 + platform_set_drvdata(pdev, hw);
926 + init_completion(&hw->done);
928 + /* setup the master state. */
930 + master->num_chipselect = 4;
931 + master->bus_num = 1;
933 + /* setup the state for the bitbang driver */
935 + hw->bitbang.master = hw->master;
936 + hw->bitbang.setup_transfer = cns3xxx_spi_setup_transfer;
937 + hw->bitbang.chipselect = cns3xxx_spi_chipselect;
938 + hw->bitbang.txrx_bufs = cns3xxx_spi_txrx;
939 + hw->bitbang.master->setup = cns3xxx_spi_setup;
941 + dev_dbg(hw->dev, "bitbang at %p\n", &hw->bitbang);
943 +#ifdef CNS3XXX_SPI_INTERRUPT
944 + err = request_irq(IRQ_CNS3XXX_SPI, cns3xxx_spi_irq, IRQF_SHARED, "cns3xxx_spi", hw);
946 + dev_err(&pdev->dev, "Cannot claim IRQ\n");
951 + /* SPI controller initializations */
952 + cns3xxx_spi_initial();
954 + /* register SPI controller */
956 + err = spi_bitbang_start(&hw->bitbang);
958 + dev_err(&pdev->dev, "Failed to register SPI master\n");
967 +#ifdef CNS3XXX_SPI_INTERRUPT
970 + spi_master_put(hw->master);;
976 +static int __devexit cns3xxx_spi_remove(struct platform_device *dev)
978 + struct cns3xxx_spi *hw = platform_get_drvdata(dev);
980 + platform_set_drvdata(dev, NULL);
982 + spi_unregister_master(hw->master);
984 + //cns3xxx_spi_clk_disable();
986 + spi_master_put(hw->master);
992 +static int cns3xxx_spi_suspend(struct platform_device *pdev, pm_message_t msg)
994 + struct cns3xxx_spi *hw = platform_get_drvdata(pdev);
996 + //cns3xxx_spi_clk_disable();
1000 +static int cns3xxx_spi_resume(struct platform_device *pdev)
1002 + struct cns3xxx_spi *hw = platform_get_drvdata(pdev);
1004 + //cns3xxx_spi_clk_enable()
1009 +#define cns3xxx_spi_suspend NULL
1010 +#define cns3xxx_spi_resume NULL
1013 +static struct platform_driver cns3xxx_spi_driver = {
1014 + .probe = cns3xxx_spi_probe,
1015 + .remove = __devexit_p(cns3xxx_spi_remove),
1016 + .suspend = cns3xxx_spi_suspend,
1017 + .resume = cns3xxx_spi_resume,
1019 + .name = "cns3xxx_spi",
1020 + .owner = THIS_MODULE,
1024 +static int __init cns3xxx_spi_init(void)
1026 + return platform_driver_register(&cns3xxx_spi_driver);
1029 +static void __exit cns3xxx_spi_exit(void)
1031 + platform_driver_unregister(&cns3xxx_spi_driver);
1034 +module_init(cns3xxx_spi_init);
1035 +module_exit(cns3xxx_spi_exit);
1037 +MODULE_AUTHOR("Cavium Networks");
1038 +MODULE_DESCRIPTION("CNS3XXX SPI Controller Driver");
1039 +MODULE_LICENSE("GPL");
1040 +MODULE_ALIAS("platform:cns3xxx_spi");
1042 +EXPORT_SYMBOL_GPL(cns3xxx_spi_tx_rx);
1043 --- a/include/linux/spi/spi.h
1044 +++ b/include/linux/spi/spi.h
1045 @@ -424,6 +424,12 @@ struct spi_transfer {
1049 +#ifdef CONFIG_ARCH_CNS3XXX
1050 + unsigned last_in_message_list;
1051 +#ifdef CONFIG_SPI_CNS3XXX_2IOREAD
1055 struct list_head transfer_list;
1058 @@ -627,6 +633,13 @@ spi_read(struct spi_device *spi, u8 *buf
1059 return spi_sync(spi, &m);
1062 +#ifdef CONFIG_ARCH_CNS3XXX
1063 +extern int spi_write_read_sync(struct spi_device *spi,
1064 + const u8 *txbuf, unsigned n_tx,
1065 + u8 *rxbuf, unsigned n_rx);
1069 /* this copies txbuf and rxbuf data; for small transfers only! */
1070 extern int spi_write_then_read(struct spi_device *spi,
1071 const u8 *txbuf, unsigned n_tx,