1 /*******************************************************************************
3 * CNS3XXX SPI controller driver (master mode only)
5 * Copyright (c) 2008 Cavium Networks
6 * Copyright 2011 Gateworks Corporation
7 * Chris Lang <clang@gateworks.com>
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
13 * This file is distributed in the hope that it will be useful,
14 * but AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this file; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA or
21 * visit http://www.gnu.org/licenses/.
23 * This file may also be available under a different license from Cavium.
24 * Contact Cavium Networks for more information
26 ******************************************************************************/
28 #include <linux/init.h>
29 #include <linux/spinlock.h>
30 #include <linux/workqueue.h>
31 #include <linux/interrupt.h>
32 #include <linux/delay.h>
33 #include <linux/errno.h>
34 #include <linux/err.h>
35 #include <linux/clk.h>
36 #include <linux/platform_device.h>
38 #include <linux/spi/spi.h>
39 #include <linux/spi/spi_bitbang.h>
40 #include <linux/mtd/partitions.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/slab.h>
45 #include <asm/memory.h>
47 #include <asm/delay.h>
48 #include <linux/module.h>
51 * define access macros
53 #define SPI_MEM_MAP_VALUE(reg_offset) (*((u32 volatile *)(hw->base + reg_offset)))
55 #define SPI_CONFIGURATION_REG SPI_MEM_MAP_VALUE(0x00)
56 #define SPI_SERVICE_STATUS_REG SPI_MEM_MAP_VALUE(0x04)
57 #define SPI_BIT_RATE_CONTROL_REG SPI_MEM_MAP_VALUE(0x08)
58 #define SPI_TRANSMIT_CONTROL_REG SPI_MEM_MAP_VALUE(0x0C)
59 #define SPI_TRANSMIT_BUFFER_REG SPI_MEM_MAP_VALUE(0x10)
60 #define SPI_RECEIVE_CONTROL_REG SPI_MEM_MAP_VALUE(0x14)
61 #define SPI_RECEIVE_BUFFER_REG SPI_MEM_MAP_VALUE(0x18)
62 #define SPI_FIFO_TRANSMIT_CONFIG_REG SPI_MEM_MAP_VALUE(0x1C)
63 #define SPI_FIFO_TRANSMIT_CONTROL_REG SPI_MEM_MAP_VALUE(0x20)
64 #define SPI_FIFO_RECEIVE_CONFIG_REG SPI_MEM_MAP_VALUE(0x24)
65 #define SPI_INTERRUPT_STATUS_REG SPI_MEM_MAP_VALUE(0x28)
66 #define SPI_INTERRUPT_ENABLE_REG SPI_MEM_MAP_VALUE(0x2C)
68 #define SPI_TRANSMIT_BUFFER_REG_ADDR (CNS3XXX_SSP_BASE +0x10)
69 #define SPI_RECEIVE_BUFFER_REG_ADDR (CNS3XXX_SSP_BASE +0x18)
71 /* Structure for SPI controller of CNS3XXX SOCs */
73 /* bitbang has to be first */
74 struct spi_bitbang bitbang
;
75 struct completion done
;
76 wait_queue_head_t wait
;
80 int last_in_message_list
;
83 const unsigned char *tx
;
87 struct spi_master
*master
;
88 struct platform_device
*pdev
;
92 static inline u8
cns3xxx_spi_bus_idle(struct cns3xxx_spi
*hw
)
94 return ((SPI_SERVICE_STATUS_REG
& 0x1) ? 0 : 1);
97 static inline u8
cns3xxx_spi_tx_buffer_empty(struct cns3xxx_spi
*hw
)
99 return ((SPI_INTERRUPT_STATUS_REG
& (0x1 << 3)) ? 1 : 0);
102 static inline u8
cns3xxx_spi_rx_buffer_full(struct cns3xxx_spi
*hw
)
104 return ((SPI_INTERRUPT_STATUS_REG
& (0x1 << 2)) ? 1 : 0);
107 u8
cns3xxx_spi_tx_rx(struct cns3xxx_spi
*hw
, u8 tx_channel
, u8 tx_eof
,
108 u32 tx_data
, u32
* rx_data
)
113 while (!cns3xxx_spi_bus_idle(hw
)) ; // do nothing
115 while (!cns3xxx_spi_tx_buffer_empty(hw
)) ; // do nothing
117 SPI_TRANSMIT_CONTROL_REG
&= ~(0x7);
118 SPI_TRANSMIT_CONTROL_REG
|= (tx_channel
& 0x3) | ((tx_eof
& 0x1) << 2);
120 SPI_TRANSMIT_BUFFER_REG
= tx_data
;
122 while (!cns3xxx_spi_rx_buffer_full(hw
)) ; // do nothing
124 rx_channel
= SPI_RECEIVE_CONTROL_REG
& 0x3;
125 rx_eof
= (SPI_RECEIVE_CONTROL_REG
& (0x1 << 2)) ? 1 : 0;
127 *rx_data
= SPI_RECEIVE_BUFFER_REG
;
129 if ((tx_channel
!= rx_channel
) || (tx_eof
!= rx_eof
)) {
136 u8
cns3xxx_spi_tx(struct cns3xxx_spi
*hw
, u8 tx_channel
, u8 tx_eof
, u32 tx_data
)
139 while (!cns3xxx_spi_bus_idle(hw
)) ; // do nothing
141 while (!cns3xxx_spi_tx_buffer_empty(hw
)) ; // do nothing
143 SPI_TRANSMIT_CONTROL_REG
&= ~(0x7);
144 SPI_TRANSMIT_CONTROL_REG
|= (tx_channel
& 0x3) | ((tx_eof
& 0x1) << 2);
146 SPI_TRANSMIT_BUFFER_REG
= tx_data
;
151 static inline struct cns3xxx_spi
*to_hw(struct spi_device
*sdev
)
153 return spi_master_get_devdata(sdev
->master
);
156 static int cns3xxx_spi_setup_transfer(struct spi_device
*spi
,
157 struct spi_transfer
*t
)
162 static void cns3xxx_spi_chipselect(struct spi_device
*spi
, int value
)
164 struct cns3xxx_spi
*hw
= to_hw(spi
);
165 unsigned int spi_config
;
168 case BITBANG_CS_INACTIVE
:
171 case BITBANG_CS_ACTIVE
:
172 spi_config
= SPI_CONFIGURATION_REG
;
174 if (spi
->mode
& SPI_CPHA
)
175 spi_config
|= (0x1 << 13);
177 spi_config
&= ~(0x1 << 13);
179 if (spi
->mode
& SPI_CPOL
)
180 spi_config
|= (0x1 << 14);
182 spi_config
&= ~(0x1 << 14);
184 /* write new configration */
185 SPI_CONFIGURATION_REG
= spi_config
;
187 SPI_TRANSMIT_CONTROL_REG
&= ~(0x7);
188 SPI_TRANSMIT_CONTROL_REG
|= (spi
->chip_select
& 0x3);
194 static int cns3xxx_spi_setup(struct spi_device
*spi
)
196 if (!spi
->bits_per_word
)
197 spi
->bits_per_word
= 8;
202 static int cns3xxx_spi_txrx(struct spi_device
*spi
, struct spi_transfer
*t
)
204 struct cns3xxx_spi
*hw
= to_hw(spi
);
206 dev_dbg(&spi
->dev
, "txrx: tx %p, rx %p, len %d\n", t
->tx_buf
, t
->rx_buf
,
213 hw
->last_in_message_list
= t
->last_in_message_list
;
215 init_completion(&hw
->done
);
220 for (i
= 0; i
< (hw
->len
- 1); i
++) {
222 "[SPI_CNS3XXX_DEBUG] hw->tx[%02d]: 0x%02x\n", i
,
224 cns3xxx_spi_tx_rx(hw
, spi
->chip_select
, 0, hw
->tx
[i
],
229 "[SPI_CNS3XXX_DEBUG] hw->rx[%02d]: 0x%02x\n",
234 if (t
->last_in_message_list
) {
235 cns3xxx_spi_tx_rx(hw
, spi
->chip_select
, 1, hw
->tx
[i
],
240 "[SPI_CNS3XXX_DEBUG] hw->rx[%02d]: 0x%02x\n",
244 cns3xxx_spi_tx_rx(hw
, spi
->chip_select
, 0, hw
->tx
[i
],
253 for (i
= 0; i
< (hw
->len
- 1); i
++) {
254 cns3xxx_spi_tx_rx(hw
, spi
->chip_select
, 0, 0xff, &rx_data
);
257 "[SPI_CNS3XXX_DEBUG] hw->rx[%02d]: 0x%02x\n", i
,
261 if (t
->last_in_message_list
) {
262 cns3xxx_spi_tx_rx(hw
, spi
->chip_select
, 1, 0xff, &rx_data
);
264 cns3xxx_spi_tx_rx(hw
, spi
->chip_select
, 0, 0xff, &rx_data
);
267 dev_dbg(&spi
->dev
, "[SPI_CNS3XXX_DEBUG] hw->rx[%02d]: 0x%02x\n",
274 static void __init
cns3xxx_spi_initial(struct cns3xxx_spi
*hw
)
276 SPI_CONFIGURATION_REG
= (((0x0 & 0x3) << 0) | /* 8bits shift length */
277 (0x0 << 9) | /* SPI mode */
278 (0x0 << 10) | /* disable FIFO */
279 (0x1 << 11) | /* SPI master mode */
280 (0x0 << 12) | /* disable SPI loopback mode */
281 (0x1 << 13) | /* clock phase */
282 (0x1 << 14) | /* clock polarity */
283 (0x0 << 24) | /* disable - SPI data swap */
284 (0x1 << 29) | /* enable - 2IO Read mode */
285 (0x0 << 30) | /* disable - SPI high speed read for system boot up */
286 (0x0 << 31)); /* disable - SPI */
288 /* Set SPI bit rate PCLK/2 */
289 SPI_BIT_RATE_CONTROL_REG
= 0x1;
291 /* Set SPI Tx channel 0 */
292 SPI_TRANSMIT_CONTROL_REG
= 0x0;
294 /* Set Tx FIFO Threshold, Tx FIFO has 2 words */
295 SPI_FIFO_TRANSMIT_CONFIG_REG
&= ~(0x03 << 4);
296 SPI_FIFO_TRANSMIT_CONFIG_REG
|= ((0x0 & 0x03) << 4);
298 /* Set Rx FIFO Threshold, Rx FIFO has 2 words */
299 SPI_FIFO_RECEIVE_CONFIG_REG
&= ~(0x03 << 4);
300 SPI_FIFO_RECEIVE_CONFIG_REG
|= ((0x0 & 0x03) << 4);
302 /* Disable all interrupt */
303 SPI_INTERRUPT_ENABLE_REG
= 0x0;
305 /* Clear spurious interrupt sources */
306 SPI_INTERRUPT_STATUS_REG
= (0x0F << 4);
309 SPI_CONFIGURATION_REG
|= (0x1 << 31);
314 static int cns3xxx_spi_probe(struct platform_device
*pdev
)
316 struct spi_master
*master
;
317 struct cns3xxx_spi
*hw
;
318 struct resource
*res
;
321 printk("%s: setup CNS3XXX SPI Controller\n", __FUNCTION__
);
323 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
327 /* Allocate master with space for cns3xxx_spi */
328 master
= spi_alloc_master(&pdev
->dev
, sizeof(struct cns3xxx_spi
));
329 if (master
== NULL
) {
330 dev_err(&pdev
->dev
, "No memory for spi_master\n");
335 hw
= spi_master_get_devdata(master
);
336 memset(hw
, 0, sizeof(struct cns3xxx_spi
));
338 hw
->master
= spi_master_get(master
);
339 hw
->dev
= &pdev
->dev
;
341 hw
->base
= devm_ioremap_resource(hw
->dev
, res
);
342 if (IS_ERR(hw
->base
)) {
343 dev_err(hw
->dev
, "Unable to map registers\n");
344 err
= PTR_ERR(hw
->base
);
348 platform_set_drvdata(pdev
, hw
);
349 init_completion(&hw
->done
);
351 /* setup the master state. */
353 master
->num_chipselect
= 4;
356 /* setup the state for the bitbang driver */
358 hw
->bitbang
.master
= hw
->master
;
359 hw
->bitbang
.setup_transfer
= cns3xxx_spi_setup_transfer
;
360 hw
->bitbang
.chipselect
= cns3xxx_spi_chipselect
;
361 hw
->bitbang
.txrx_bufs
= cns3xxx_spi_txrx
;
362 hw
->bitbang
.master
->setup
= cns3xxx_spi_setup
;
364 dev_dbg(hw
->dev
, "bitbang at %p\n", &hw
->bitbang
);
366 /* SPI controller initializations */
367 cns3xxx_spi_initial(hw
);
369 /* register SPI controller */
371 err
= spi_bitbang_start(&hw
->bitbang
);
373 dev_err(&pdev
->dev
, "Failed to register SPI master\n");
380 spi_master_put(hw
->master
);;
386 static int cns3xxx_spi_remove(struct platform_device
*dev
)
388 struct cns3xxx_spi
*hw
= platform_get_drvdata(dev
);
390 platform_set_drvdata(dev
, NULL
);
392 spi_unregister_master(hw
->master
);
394 spi_master_put(hw
->master
);
400 static int cns3xxx_spi_suspend(struct platform_device
*pdev
, pm_message_t msg
)
402 struct cns3xxx_spi
*hw
= platform_get_drvdata(pdev
);
407 static int cns3xxx_spi_resume(struct platform_device
*pdev
)
409 struct cns3xxx_spi
*hw
= platform_get_drvdata(pdev
);
415 #define cns3xxx_spi_suspend NULL
416 #define cns3xxx_spi_resume NULL
419 static struct platform_driver cns3xxx_spi_driver
= {
420 .probe
= cns3xxx_spi_probe
,
421 .remove
= cns3xxx_spi_remove
,
422 .suspend
= cns3xxx_spi_suspend
,
423 .resume
= cns3xxx_spi_resume
,
425 .name
= "cns3xxx_spi",
426 .owner
= THIS_MODULE
,
430 static int __init
cns3xxx_spi_init(void)
432 return platform_driver_register(&cns3xxx_spi_driver
);
435 static void __exit
cns3xxx_spi_exit(void)
437 platform_driver_unregister(&cns3xxx_spi_driver
);
440 module_init(cns3xxx_spi_init
);
441 module_exit(cns3xxx_spi_exit
);
443 MODULE_AUTHOR("Cavium Networks");
444 MODULE_DESCRIPTION("CNS3XXX SPI Controller Driver");
445 MODULE_LICENSE("GPL");
446 MODULE_ALIAS("platform:cns3xxx_spi");
448 EXPORT_SYMBOL_GPL(cns3xxx_spi_tx_rx
);