1 --- a/drivers/spi/Kconfig
2 +++ b/drivers/spi/Kconfig
3 @@ -123,6 +123,13 @@ config SPI_BUTTERFLY
4 inexpensive battery powered microcontroller evaluation board.
5 This same cable can be used to flash new firmware.
8 + tristate "CNS3XXX SPI controller"
9 + depends on ARCH_CNS3XXX && SPI_MASTER
12 + This enables using the CNS3XXX SPI controller in master mode.
14 config SPI_COLDFIRE_QSPI
15 tristate "Freescale Coldfire QSPI controller"
16 depends on (M520x || M523x || M5249 || M527x || M528x || M532x)
17 --- a/drivers/spi/Makefile
18 +++ b/drivers/spi/Makefile
19 @@ -56,6 +56,7 @@ obj-$(CONFIG_SPI_SH_SCI) += spi_sh_sci.
20 obj-$(CONFIG_SPI_SH_MSIOF) += spi_sh_msiof.o
21 obj-$(CONFIG_SPI_STMP3XXX) += spi_stmp.o
22 obj-$(CONFIG_SPI_NUC900) += spi_nuc900.o
23 +obj-$(CONFIG_SPI_CNS3XXX) += spi_cns3xxx.o
25 # special build for s3c24xx spi driver with fiq support
26 spi_s3c24xx_hw-y := spi_s3c24xx.o
28 +++ b/drivers/spi/spi_cns3xxx.c
30 +/*******************************************************************************
32 + * CNS3XXX SPI controller driver (master mode only)
34 + * Copyright (c) 2008 Cavium Networks
35 + * Copyright 2011 Gateworks Corporation
36 + * Chris Lang <clang@gateworks.com>
38 + * This file is free software; you can redistribute it and/or modify
39 + * it under the terms of the GNU General Public License, Version 2, as
40 + * published by the Free Software Foundation.
42 + * This file is distributed in the hope that it will be useful,
43 + * but AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty of
44 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
45 + * NONINFRINGEMENT. See the GNU General Public License for more details.
47 + * You should have received a copy of the GNU General Public License
48 + * along with this file; if not, write to the Free Software
49 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA or
50 + * visit http://www.gnu.org/licenses/.
52 + * This file may also be available under a different license from Cavium.
53 + * Contact Cavium Networks for more information
55 + ******************************************************************************/
57 +#include <linux/init.h>
58 +#include <linux/spinlock.h>
59 +#include <linux/workqueue.h>
60 +#include <linux/interrupt.h>
61 +#include <linux/delay.h>
62 +#include <linux/errno.h>
63 +#include <linux/err.h>
64 +#include <linux/clk.h>
65 +#include <linux/platform_device.h>
67 +#include <linux/spi/spi.h>
68 +#include <linux/spi/spi_bitbang.h>
69 +#include <linux/mtd/partitions.h>
70 +#include <linux/dma-mapping.h>
71 +#include <linux/slab.h>
74 +#include <asm/memory.h>
76 +#include <asm/delay.h>
77 +#include <mach/cns3xxx.h>
78 +#include <linux/module.h>
82 + * define access macros
84 +#define SPI_MEM_MAP_VALUE(reg_offset) (*((u32 volatile *)(CNS3XXX_SSP_BASE_VIRT + reg_offset)))
86 +#define SPI_CONFIGURATION_REG SPI_MEM_MAP_VALUE(0x40)
87 +#define SPI_SERVICE_STATUS_REG SPI_MEM_MAP_VALUE(0x44)
88 +#define SPI_BIT_RATE_CONTROL_REG SPI_MEM_MAP_VALUE(0x48)
89 +#define SPI_TRANSMIT_CONTROL_REG SPI_MEM_MAP_VALUE(0x4C)
90 +#define SPI_TRANSMIT_BUFFER_REG SPI_MEM_MAP_VALUE(0x50)
91 +#define SPI_RECEIVE_CONTROL_REG SPI_MEM_MAP_VALUE(0x54)
92 +#define SPI_RECEIVE_BUFFER_REG SPI_MEM_MAP_VALUE(0x58)
93 +#define SPI_FIFO_TRANSMIT_CONFIG_REG SPI_MEM_MAP_VALUE(0x5C)
94 +#define SPI_FIFO_TRANSMIT_CONTROL_REG SPI_MEM_MAP_VALUE(0x60)
95 +#define SPI_FIFO_RECEIVE_CONFIG_REG SPI_MEM_MAP_VALUE(0x64)
96 +#define SPI_INTERRUPT_STATUS_REG SPI_MEM_MAP_VALUE(0x68)
97 +#define SPI_INTERRUPT_ENABLE_REG SPI_MEM_MAP_VALUE(0x6C)
99 +#define SPI_TRANSMIT_BUFFER_REG_ADDR (CNS3XXX_SSP_BASE +0x50)
100 +#define SPI_RECEIVE_BUFFER_REG_ADDR (CNS3XXX_SSP_BASE +0x58)
102 +/* Structure for SPI controller of CNS3XXX SOCs */
103 +struct cns3xxx_spi {
104 + /* bitbang has to be first */
105 + struct spi_bitbang bitbang;
106 + struct completion done;
107 + wait_queue_head_t wait;
111 + int last_in_message_list;
114 + const unsigned char *tx;
117 + struct spi_master *master;
118 + struct platform_device *pdev;
119 + struct device *dev;
122 +static inline u8 cns3xxx_spi_bus_idle(void)
124 + return ((SPI_SERVICE_STATUS_REG & 0x1) ? 0 : 1);
127 +static inline u8 cns3xxx_spi_tx_buffer_empty(void)
129 + return ((SPI_INTERRUPT_STATUS_REG & (0x1 << 3)) ? 1 : 0);
132 +static inline u8 cns3xxx_spi_rx_buffer_full(void)
134 + return ((SPI_INTERRUPT_STATUS_REG & (0x1 << 2)) ? 1 : 0);
137 +u8 cns3xxx_spi_tx_rx(u8 tx_channel, u8 tx_eof, u32 tx_data,
143 + while (!cns3xxx_spi_bus_idle()) ; // do nothing
145 + while (!cns3xxx_spi_tx_buffer_empty()) ; // do nothing
147 + SPI_TRANSMIT_CONTROL_REG &= ~(0x7);
148 + SPI_TRANSMIT_CONTROL_REG |= (tx_channel & 0x3) | ((tx_eof & 0x1) << 2);
150 + SPI_TRANSMIT_BUFFER_REG = tx_data;
152 + while (!cns3xxx_spi_rx_buffer_full()) ; // do nothing
154 + rx_channel = SPI_RECEIVE_CONTROL_REG & 0x3;
155 + rx_eof = (SPI_RECEIVE_CONTROL_REG & (0x1 << 2)) ? 1 : 0;
157 + *rx_data = SPI_RECEIVE_BUFFER_REG;
159 + if ((tx_channel != rx_channel) || (tx_eof != rx_eof)) {
166 +u8 cns3xxx_spi_tx(u8 tx_channel, u8 tx_eof, u32 tx_data)
169 + while (!cns3xxx_spi_bus_idle()) ; // do nothing
171 + while (!cns3xxx_spi_tx_buffer_empty()) ; // do nothing
173 + SPI_TRANSMIT_CONTROL_REG &= ~(0x7);
174 + SPI_TRANSMIT_CONTROL_REG |= (tx_channel & 0x3) | ((tx_eof & 0x1) << 2);
176 + SPI_TRANSMIT_BUFFER_REG = tx_data;
181 +static inline struct cns3xxx_spi *to_hw(struct spi_device *sdev)
183 + return spi_master_get_devdata(sdev->master);
186 +static int cns3xxx_spi_setup_transfer(struct spi_device *spi,
187 + struct spi_transfer *t)
192 +static void cns3xxx_spi_chipselect(struct spi_device *spi, int value)
194 + unsigned int spi_config;
197 + case BITBANG_CS_INACTIVE:
200 + case BITBANG_CS_ACTIVE:
201 + spi_config = SPI_CONFIGURATION_REG;
203 + if (spi->mode & SPI_CPHA)
204 + spi_config |= (0x1 << 13);
206 + spi_config &= ~(0x1 << 13);
208 + if (spi->mode & SPI_CPOL)
209 + spi_config |= (0x1 << 14);
211 + spi_config &= ~(0x1 << 14);
213 + /* write new configration */
214 + SPI_CONFIGURATION_REG = spi_config;
216 + SPI_TRANSMIT_CONTROL_REG &= ~(0x7);
217 + SPI_TRANSMIT_CONTROL_REG |= (spi->chip_select & 0x3);
223 +static int cns3xxx_spi_setup(struct spi_device *spi)
225 + if (!spi->bits_per_word)
226 + spi->bits_per_word = 8;
231 +static int cns3xxx_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
233 + struct cns3xxx_spi *hw = to_hw(spi);
235 + dev_dbg(&spi->dev, "txrx: tx %p, rx %p, len %d\n", t->tx_buf, t->rx_buf,
238 + hw->tx = t->tx_buf;
239 + hw->rx = t->rx_buf;
242 + hw->last_in_message_list = t->last_in_message_list;
244 + init_completion(&hw->done);
249 + for (i = 0; i < (hw->len - 1); i++) {
251 + "[SPI_CNS3XXX_DEBUG] hw->tx[%02d]: 0x%02x\n", i,
253 + cns3xxx_spi_tx_rx(spi->chip_select, 0, hw->tx[i],
256 + hw->rx[i] = rx_data;
258 + "[SPI_CNS3XXX_DEBUG] hw->rx[%02d]: 0x%02x\n",
263 + if (t->last_in_message_list) {
264 + cns3xxx_spi_tx_rx(spi->chip_select, 1, hw->tx[i],
267 + hw->rx[i] = rx_data;
269 + "[SPI_CNS3XXX_DEBUG] hw->rx[%02d]: 0x%02x\n",
273 + cns3xxx_spi_tx_rx(spi->chip_select, 0, hw->tx[i],
282 + for (i = 0; i < (hw->len - 1); i++) {
283 + cns3xxx_spi_tx_rx(spi->chip_select, 0, 0xff, &rx_data);
284 + hw->rx[i] = rx_data;
286 + "[SPI_CNS3XXX_DEBUG] hw->rx[%02d]: 0x%02x\n", i,
290 + if (t->last_in_message_list) {
291 + cns3xxx_spi_tx_rx(spi->chip_select, 1, 0xff, &rx_data);
293 + cns3xxx_spi_tx_rx(spi->chip_select, 0, 0xff, &rx_data);
295 + hw->rx[i] = rx_data;
296 + dev_dbg(&spi->dev, "[SPI_CNS3XXX_DEBUG] hw->rx[%02d]: 0x%02x\n",
303 +static void __init cns3xxx_spi_initial(void)
305 + u32 __iomem *gpiob = __io(CNS3XXX_MISC_BASE_VIRT + 0x0018);
306 + u32 gpiob_pins = __raw_readl(gpiob);
308 + /* MMC/SD pins share with GPIOA */
309 + gpiob_pins |= 0xf80;
310 + __raw_writel(gpiob_pins, gpiob);
312 + /* share pin config. */
313 + //PM_PLL_HM_PD_CTRL_REG &= ~(0x1 << 5);
314 + //HAL_MISC_ENABLE_SPI_PINS();
315 + cns3xxx_pwr_clk_en(CNS3XXX_PWR_CLK_EN(SPI_PCM_I2C));
316 + cns3xxx_pwr_soft_rst(CNS3XXX_PWR_SOFTWARE_RST(SPI_PCM_I2C));
318 + SPI_CONFIGURATION_REG = (((0x0 & 0x3) << 0) | /* 8bits shift length */
319 + (0x0 << 9) | /* SPI mode */
320 + (0x0 << 10) | /* disable FIFO */
321 + (0x1 << 11) | /* SPI master mode */
322 + (0x0 << 12) | /* disable SPI loopback mode */
323 + (0x1 << 13) | /* clock phase */
324 + (0x1 << 14) | /* clock polarity */
325 + (0x0 << 24) | /* disable - SPI data swap */
326 + (0x1 << 29) | /* enable - 2IO Read mode */
327 + (0x0 << 30) | /* disable - SPI high speed read for system boot up */
328 + (0x0 << 31)); /* disable - SPI */
330 + /* Set SPI bit rate PCLK/2 */
331 + SPI_BIT_RATE_CONTROL_REG = 0x1;
333 + /* Set SPI Tx channel 0 */
334 + SPI_TRANSMIT_CONTROL_REG = 0x0;
336 + /* Set Tx FIFO Threshold, Tx FIFO has 2 words */
337 + SPI_FIFO_TRANSMIT_CONFIG_REG &= ~(0x03 << 4);
338 + SPI_FIFO_TRANSMIT_CONFIG_REG |= ((0x0 & 0x03) << 4);
340 + /* Set Rx FIFO Threshold, Rx FIFO has 2 words */
341 + SPI_FIFO_RECEIVE_CONFIG_REG &= ~(0x03 << 4);
342 + SPI_FIFO_RECEIVE_CONFIG_REG |= ((0x0 & 0x03) << 4);
344 + /* Disable all interrupt */
345 + SPI_INTERRUPT_ENABLE_REG = 0x0;
347 + /* Clear spurious interrupt sources */
348 + SPI_INTERRUPT_STATUS_REG = (0x0F << 4);
351 + SPI_CONFIGURATION_REG |= (0x1 << 31);
356 +static int __init cns3xxx_spi_probe(struct platform_device *pdev)
358 + struct spi_master *master;
359 + struct cns3xxx_spi *hw;
362 + printk("%s: setup CNS3XXX SPI Controller\n", __FUNCTION__);
364 + /* Allocate master with space for cns3xxx_spi */
365 + master = spi_alloc_master(&pdev->dev, sizeof(struct cns3xxx_spi));
366 + if (master == NULL) {
367 + dev_err(&pdev->dev, "No memory for spi_master\n");
372 + hw = spi_master_get_devdata(master);
373 + memset(hw, 0, sizeof(struct cns3xxx_spi));
375 + hw->master = spi_master_get(master);
376 + hw->dev = &pdev->dev;
378 + platform_set_drvdata(pdev, hw);
379 + init_completion(&hw->done);
381 + /* setup the master state. */
383 + master->num_chipselect = 4;
384 + master->bus_num = 1;
386 + /* setup the state for the bitbang driver */
388 + hw->bitbang.master = hw->master;
389 + hw->bitbang.setup_transfer = cns3xxx_spi_setup_transfer;
390 + hw->bitbang.chipselect = cns3xxx_spi_chipselect;
391 + hw->bitbang.txrx_bufs = cns3xxx_spi_txrx;
392 + hw->bitbang.master->setup = cns3xxx_spi_setup;
394 + dev_dbg(hw->dev, "bitbang at %p\n", &hw->bitbang);
396 + /* SPI controller initializations */
397 + cns3xxx_spi_initial();
399 + /* register SPI controller */
401 + err = spi_bitbang_start(&hw->bitbang);
403 + dev_err(&pdev->dev, "Failed to register SPI master\n");
410 + spi_master_put(hw->master);;
416 +static int __devexit cns3xxx_spi_remove(struct platform_device *dev)
418 + struct cns3xxx_spi *hw = platform_get_drvdata(dev);
420 + platform_set_drvdata(dev, NULL);
422 + spi_unregister_master(hw->master);
424 + spi_master_put(hw->master);
430 +static int cns3xxx_spi_suspend(struct platform_device *pdev, pm_message_t msg)
432 + struct cns3xxx_spi *hw = platform_get_drvdata(pdev);
437 +static int cns3xxx_spi_resume(struct platform_device *pdev)
439 + struct cns3xxx_spi *hw = platform_get_drvdata(pdev);
445 +#define cns3xxx_spi_suspend NULL
446 +#define cns3xxx_spi_resume NULL
449 +static struct platform_driver cns3xxx_spi_driver = {
450 + .probe = cns3xxx_spi_probe,
451 + .remove = __devexit_p(cns3xxx_spi_remove),
452 + .suspend = cns3xxx_spi_suspend,
453 + .resume = cns3xxx_spi_resume,
455 + .name = "cns3xxx_spi",
456 + .owner = THIS_MODULE,
460 +static int __init cns3xxx_spi_init(void)
462 + return platform_driver_register(&cns3xxx_spi_driver);
465 +static void __exit cns3xxx_spi_exit(void)
467 + platform_driver_unregister(&cns3xxx_spi_driver);
470 +module_init(cns3xxx_spi_init);
471 +module_exit(cns3xxx_spi_exit);
473 +MODULE_AUTHOR("Cavium Networks");
474 +MODULE_DESCRIPTION("CNS3XXX SPI Controller Driver");
475 +MODULE_LICENSE("GPL");
476 +MODULE_ALIAS("platform:cns3xxx_spi");
478 +EXPORT_SYMBOL_GPL(cns3xxx_spi_tx_rx);
479 --- a/include/linux/spi/spi.h
480 +++ b/include/linux/spi/spi.h
481 @@ -446,6 +446,13 @@ struct spi_transfer {
484 struct list_head transfer_list;
486 +#ifdef CONFIG_ARCH_CNS3XXX
487 + unsigned last_in_message_list;
488 +#ifdef CONFIG_SPI_CNS3XXX_2IOREAD
495 --- a/drivers/spi/spi_bitbang.c
496 +++ b/drivers/spi/spi_bitbang.c
497 @@ -329,6 +329,12 @@ static void bitbang_work(struct work_str
499 if (!m->is_dma_mapped)
500 t->rx_dma = t->tx_dma = 0;
502 + if (t->transfer_list.next == &m->transfers)
503 + t->last_in_message_list = 1;
505 + t->last_in_message_list = 0;
507 status = bitbang->txrx_bufs(spi, t);