2019-05-29 17:18:02 +03:00
// SPDX-License-Identifier: GPL-2.0-only
2009-12-15 01:20:22 +03:00
/*
2011-06-06 11:16:30 +04:00
* Designware SPI core controller driver ( refer pxa2xx_spi . c )
2009-12-15 01:20:22 +03:00
*
* Copyright ( c ) 2009 , Intel Corporation .
*/
# include <linux/dma-mapping.h>
# include <linux/interrupt.h>
2011-07-03 23:44:29 +04:00
# include <linux/module.h>
2009-12-15 01:20:22 +03:00
# include <linux/highmem.h>
# include <linux/delay.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 11:04:11 +03:00
# include <linux/slab.h>
2009-12-15 01:20:22 +03:00
# include <linux/spi/spi.h>
2020-08-24 23:30:05 +03:00
# include <linux/of.h>
2009-12-15 01:20:22 +03:00
2011-06-06 11:16:30 +04:00
# include "spi-dw.h"
2011-02-28 22:47:12 +03:00
2009-12-15 01:20:22 +03:00
# ifdef CONFIG_DEBUG_FS
# include <linux/debugfs.h>
# endif
2020-10-08 02:54:56 +03:00
/* Slave spi_device related */
2009-12-15 01:20:22 +03:00
struct chip_data {
2020-10-08 02:54:52 +03:00
u32 cr0 ;
2020-08-24 23:30:05 +03:00
u32 rx_sample_dly ; /* RX sample delay */
2009-12-15 01:20:22 +03:00
} ;
# ifdef CONFIG_DEBUG_FS
2020-05-29 16:12:04 +03:00
# define DW_SPI_DBGFS_REG(_name, _off) \
{ \
. name = _name , \
. offset = _off , \
2009-12-15 01:20:22 +03:00
}
2020-05-29 16:12:04 +03:00
static const struct debugfs_reg32 dw_spi_dbgfs_regs [ ] = {
DW_SPI_DBGFS_REG ( " CTRLR0 " , DW_SPI_CTRLR0 ) ,
DW_SPI_DBGFS_REG ( " CTRLR1 " , DW_SPI_CTRLR1 ) ,
DW_SPI_DBGFS_REG ( " SSIENR " , DW_SPI_SSIENR ) ,
DW_SPI_DBGFS_REG ( " SER " , DW_SPI_SER ) ,
DW_SPI_DBGFS_REG ( " BAUDR " , DW_SPI_BAUDR ) ,
DW_SPI_DBGFS_REG ( " TXFTLR " , DW_SPI_TXFTLR ) ,
DW_SPI_DBGFS_REG ( " RXFTLR " , DW_SPI_RXFTLR ) ,
DW_SPI_DBGFS_REG ( " TXFLR " , DW_SPI_TXFLR ) ,
DW_SPI_DBGFS_REG ( " RXFLR " , DW_SPI_RXFLR ) ,
DW_SPI_DBGFS_REG ( " SR " , DW_SPI_SR ) ,
DW_SPI_DBGFS_REG ( " IMR " , DW_SPI_IMR ) ,
DW_SPI_DBGFS_REG ( " ISR " , DW_SPI_ISR ) ,
DW_SPI_DBGFS_REG ( " DMACR " , DW_SPI_DMACR ) ,
DW_SPI_DBGFS_REG ( " DMATDLR " , DW_SPI_DMATDLR ) ,
DW_SPI_DBGFS_REG ( " DMARDLR " , DW_SPI_DMARDLR ) ,
2020-08-24 23:30:05 +03:00
DW_SPI_DBGFS_REG ( " RX_SAMPLE_DLY " , DW_SPI_RX_SAMPLE_DLY ) ,
2009-12-15 01:20:22 +03:00
} ;
2014-09-12 16:11:56 +04:00
static int dw_spi_debugfs_init ( struct dw_spi * dws )
2009-12-15 01:20:22 +03:00
{
2017-01-06 12:35:13 +03:00
char name [ 32 ] ;
2016-12-22 12:18:12 +03:00
2017-01-06 12:35:13 +03:00
snprintf ( name , 32 , " dw_spi%d " , dws - > master - > bus_num ) ;
2016-12-22 12:18:12 +03:00
dws - > debugfs = debugfs_create_dir ( name , NULL ) ;
2009-12-15 01:20:22 +03:00
if ( ! dws - > debugfs )
return - ENOMEM ;
2020-05-29 16:12:04 +03:00
dws - > regset . regs = dw_spi_dbgfs_regs ;
dws - > regset . nregs = ARRAY_SIZE ( dw_spi_dbgfs_regs ) ;
dws - > regset . base = dws - > regs ;
debugfs_create_regset32 ( " registers " , 0400 , dws - > debugfs , & dws - > regset ) ;
2009-12-15 01:20:22 +03:00
return 0 ;
}
2014-09-12 16:11:56 +04:00
static void dw_spi_debugfs_remove ( struct dw_spi * dws )
2009-12-15 01:20:22 +03:00
{
2014-09-02 06:49:24 +04:00
debugfs_remove_recursive ( dws - > debugfs ) ;
2009-12-15 01:20:22 +03:00
}
# else
2014-09-12 16:11:56 +04:00
static inline int dw_spi_debugfs_init ( struct dw_spi * dws )
2009-12-15 01:20:22 +03:00
{
2010-01-21 14:40:49 +03:00
return 0 ;
2009-12-15 01:20:22 +03:00
}
2014-09-12 16:11:56 +04:00
static inline void dw_spi_debugfs_remove ( struct dw_spi * dws )
2009-12-15 01:20:22 +03:00
{
}
# endif /* CONFIG_DEBUG_FS */
2018-07-27 22:53:54 +03:00
void dw_spi_set_cs ( struct spi_device * spi , bool enable )
2015-03-02 15:58:57 +03:00
{
2018-02-01 18:17:29 +03:00
struct dw_spi * dws = spi_controller_get_devdata ( spi - > controller ) ;
2020-05-15 13:47:43 +03:00
bool cs_high = ! ! ( spi - > mode & SPI_CS_HIGH ) ;
2015-03-02 15:58:57 +03:00
2020-05-15 13:47:43 +03:00
/*
* DW SPI controller demands any native CS being set in order to
* proceed with data transfer . So in order to activate the SPI
* communications we must set a corresponding bit in the Slave
* Enable register no matter whether the SPI core is configured to
* support active - high or active - low CS level .
*/
if ( cs_high = = enable )
2015-03-02 15:58:57 +03:00
dw_writel ( dws , DW_SPI_SER , BIT ( spi - > chip_select ) ) ;
2020-09-20 14:28:53 +03:00
else if ( dws - > caps & DW_SPI_CAP_CS_OVERRIDE )
2018-10-11 14:20:07 +03:00
dw_writel ( dws , DW_SPI_SER , 0 ) ;
2015-03-02 15:58:57 +03:00
}
2018-07-27 22:53:54 +03:00
EXPORT_SYMBOL_GPL ( dw_spi_set_cs ) ;
2015-03-02 15:58:57 +03:00
2011-03-30 19:09:54 +04:00
/* Return the max entries we can fill into tx fifo */
static inline u32 tx_max ( struct dw_spi * dws )
{
2020-10-08 02:54:57 +03:00
u32 tx_room , rxtx_gap ;
2011-03-30 19:09:54 +04:00
2015-03-12 22:19:31 +03:00
tx_room = dws - > fifo_len - dw_readl ( dws , DW_SPI_TXFLR ) ;
2011-03-30 19:09:54 +04:00
/*
* Another concern is about the tx / rx mismatch , we
* though to use ( dws - > fifo_len - rxflr - txflr ) as
* one maximum value for tx , but it doesn ' t cover the
* data which is out of tx / rx fifo and inside the
* shift registers . So a control from sw point of
* view is taken .
*/
2020-10-08 02:54:57 +03:00
rxtx_gap = dws - > fifo_len - ( dws - > rx_len - dws - > tx_len ) ;
2011-03-30 19:09:54 +04:00
2020-10-08 02:54:57 +03:00
return min3 ( ( u32 ) dws - > tx_len , tx_room , rxtx_gap ) ;
2011-03-30 19:09:54 +04:00
}
/* Return the max entries we should read out of rx fifo */
static inline u32 rx_max ( struct dw_spi * dws )
{
2020-10-08 02:54:57 +03:00
return min_t ( u32 , dws - > rx_len , dw_readl ( dws , DW_SPI_RXFLR ) ) ;
2011-03-30 19:09:54 +04:00
}
2011-03-30 19:09:55 +04:00
static void dw_writer ( struct dw_spi * dws )
2009-12-15 01:20:22 +03:00
{
2020-09-20 14:28:48 +03:00
u32 max = tx_max ( dws ) ;
2011-03-30 19:09:52 +04:00
u16 txw = 0 ;
2009-12-15 01:20:22 +03:00
2011-03-30 19:09:54 +04:00
while ( max - - ) {
2020-10-08 02:54:57 +03:00
if ( dws - > tx ) {
2011-03-30 19:09:54 +04:00
if ( dws - > n_bytes = = 1 )
txw = * ( u8 * ) ( dws - > tx ) ;
else
txw = * ( u16 * ) ( dws - > tx ) ;
2020-10-08 02:54:57 +03:00
dws - > tx + = dws - > n_bytes ;
2011-03-30 19:09:54 +04:00
}
2015-08-18 23:21:53 +03:00
dw_write_io_reg ( dws , DW_SPI_DR , txw ) ;
2020-10-08 02:54:57 +03:00
- - dws - > tx_len ;
2009-12-15 01:20:22 +03:00
}
}
2011-03-30 19:09:55 +04:00
static void dw_reader ( struct dw_spi * dws )
2009-12-15 01:20:22 +03:00
{
2020-09-20 14:28:48 +03:00
u32 max = rx_max ( dws ) ;
2011-03-30 19:09:52 +04:00
u16 rxw ;
2009-12-15 01:20:22 +03:00
2011-03-30 19:09:54 +04:00
while ( max - - ) {
2015-08-18 23:21:53 +03:00
rxw = dw_read_io_reg ( dws , DW_SPI_DR ) ;
2020-10-08 02:54:57 +03:00
if ( dws - > rx ) {
2011-03-30 19:09:52 +04:00
if ( dws - > n_bytes = = 1 )
* ( u8 * ) ( dws - > rx ) = rxw ;
else
* ( u16 * ) ( dws - > rx ) = rxw ;
2020-10-08 02:54:57 +03:00
dws - > rx + = dws - > n_bytes ;
2011-03-30 19:09:52 +04:00
}
2020-10-08 02:54:57 +03:00
- - dws - > rx_len ;
2009-12-15 01:20:22 +03:00
}
}
static void int_error_stop ( struct dw_spi * dws , const char * msg )
{
2015-03-02 15:58:55 +03:00
spi_reset_chip ( dws ) ;
2009-12-15 01:20:22 +03:00
dev_err ( & dws - > master - > dev , " %s \n " , msg ) ;
2015-03-02 15:58:57 +03:00
dws - > master - > cur_msg - > status = - EIO ;
spi_finalize_current_transfer ( dws - > master ) ;
2009-12-15 01:20:22 +03:00
}
2020-10-08 02:54:59 +03:00
static irqreturn_t dw_spi_transfer_handler ( struct dw_spi * dws )
2009-12-15 01:20:22 +03:00
{
2015-03-12 22:19:31 +03:00
u16 irq_status = dw_readl ( dws , DW_SPI_ISR ) ;
2009-12-15 01:20:22 +03:00
/* Error handling */
if ( irq_status & ( SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI ) ) {
2015-03-12 22:19:31 +03:00
dw_readl ( dws , DW_SPI_ICR ) ;
2011-03-30 19:09:55 +04:00
int_error_stop ( dws , " interrupt_transfer: fifo overrun/underrun " ) ;
2009-12-15 01:20:22 +03:00
return IRQ_HANDLED ;
}
2020-10-08 02:54:58 +03:00
/*
* Read data from the Rx FIFO every time we ' ve got a chance executing
* this method . If there is nothing left to receive , terminate the
* procedure . Otherwise adjust the Rx FIFO Threshold level if it ' s a
* final stage of the transfer . By doing so we ' ll get the next IRQ
* right when the leftover incoming data is received .
*/
2011-03-30 19:09:55 +04:00
dw_reader ( dws ) ;
2020-10-08 02:54:57 +03:00
if ( ! dws - > rx_len ) {
2020-09-20 14:28:50 +03:00
spi_mask_intr ( dws , 0xff ) ;
2015-03-02 15:58:57 +03:00
spi_finalize_current_transfer ( dws - > master ) ;
2020-10-08 02:54:58 +03:00
} else if ( dws - > rx_len < = dw_readl ( dws , DW_SPI_RXFTLR ) ) {
dw_writel ( dws , DW_SPI_RXFTLR , dws - > rx_len - 1 ) ;
2011-03-30 19:09:55 +04:00
}
2020-10-08 02:54:58 +03:00
/*
* Send data out if Tx FIFO Empty IRQ is received . The IRQ will be
* disabled after the data transmission is finished so not to
* have the TXE IRQ flood at the final stage of the transfer .
*/
2010-01-20 23:49:45 +03:00
if ( irq_status & SPI_INT_TXEI ) {
2011-03-30 19:09:55 +04:00
dw_writer ( dws ) ;
2020-10-08 02:54:58 +03:00
if ( ! dws - > tx_len )
spi_mask_intr ( dws , SPI_INT_TXEI ) ;
2009-12-15 01:20:22 +03:00
}
return IRQ_HANDLED ;
}
static irqreturn_t dw_spi_irq ( int irq , void * dev_id )
{
2018-02-01 18:17:29 +03:00
struct spi_controller * master = dev_id ;
struct dw_spi * dws = spi_controller_get_devdata ( master ) ;
2015-03-12 22:19:31 +03:00
u16 irq_status = dw_readl ( dws , DW_SPI_ISR ) & 0x3f ;
2010-09-07 11:27:27 +04:00
if ( ! irq_status )
return IRQ_NONE ;
2009-12-15 01:20:22 +03:00
2015-03-02 15:58:57 +03:00
if ( ! master - > cur_msg ) {
2020-09-20 14:28:50 +03:00
spi_mask_intr ( dws , 0xff ) ;
2009-12-15 01:20:22 +03:00
return IRQ_HANDLED ;
}
return dws - > transfer_handler ( dws ) ;
}
2020-10-08 02:54:52 +03:00
static u32 dw_spi_prepare_cr0 ( struct dw_spi * dws , struct spi_device * spi )
2009-12-15 01:20:22 +03:00
{
2020-10-08 02:54:52 +03:00
u32 cr0 = 0 ;
2020-05-05 16:06:13 +03:00
2020-10-08 02:54:51 +03:00
if ( ! ( dws - > caps & DW_SPI_CAP_DWC_SSI ) ) {
/* CTRLR0[ 5: 4] Frame Format */
cr0 | = SSI_MOTO_SPI < < SPI_FRF_OFFSET ;
2020-05-05 16:06:13 +03:00
2020-10-08 02:54:51 +03:00
/*
* SPI mode ( SCPOL | SCPH )
* CTRLR0 [ 6 ] Serial Clock Phase
* CTRLR0 [ 7 ] Serial Clock Polarity
*/
cr0 | = ( ( spi - > mode & SPI_CPOL ) ? 1 : 0 ) < < SPI_SCOL_OFFSET ;
cr0 | = ( ( spi - > mode & SPI_CPHA ) ? 1 : 0 ) < < SPI_SCPH_OFFSET ;
2020-05-05 16:06:14 +03:00
2020-10-08 02:54:51 +03:00
/* CTRLR0[11] Shift Register Loop */
cr0 | = ( ( spi - > mode & SPI_LOOP ) ? 1 : 0 ) < < SPI_SRL_OFFSET ;
} else {
/* CTRLR0[ 7: 6] Frame Format */
cr0 | = SSI_MOTO_SPI < < DWC_SSI_CTRLR0_FRF_OFFSET ;
2020-05-05 16:06:14 +03:00
2020-10-08 02:54:51 +03:00
/*
* SPI mode ( SCPOL | SCPH )
* CTRLR0 [ 8 ] Serial Clock Phase
* CTRLR0 [ 9 ] Serial Clock Polarity
*/
cr0 | = ( ( spi - > mode & SPI_CPOL ) ? 1 : 0 ) < < DWC_SSI_CTRLR0_SCPOL_OFFSET ;
cr0 | = ( ( spi - > mode & SPI_CPHA ) ? 1 : 0 ) < < DWC_SSI_CTRLR0_SCPH_OFFSET ;
2020-05-05 16:06:14 +03:00
2020-10-08 02:54:51 +03:00
/* CTRLR0[13] Shift Register Loop */
cr0 | = ( ( spi - > mode & SPI_LOOP ) ? 1 : 0 ) < < DWC_SSI_CTRLR0_SRL_OFFSET ;
2020-05-05 16:06:14 +03:00
2020-10-08 02:54:51 +03:00
if ( dws - > caps & DW_SPI_CAP_KEEMBAY_MST )
cr0 | = DWC_SSI_CTRLR0_KEEMBAY_MST ;
}
2020-09-20 14:28:54 +03:00
2020-10-08 02:54:52 +03:00
return cr0 ;
}
2020-10-08 02:54:56 +03:00
void dw_spi_update_config ( struct dw_spi * dws , struct spi_device * spi ,
struct dw_spi_cfg * cfg )
2020-10-08 02:54:52 +03:00
{
struct chip_data * chip = spi_get_ctldata ( spi ) ;
u32 cr0 = chip - > cr0 ;
2020-10-08 02:54:54 +03:00
u32 speed_hz ;
u16 clk_div ;
2020-10-08 02:54:52 +03:00
/* CTRLR0[ 4/3: 0] Data Frame Size */
2020-10-08 02:54:56 +03:00
cr0 | = ( cfg - > dfs - 1 ) ;
2020-10-08 02:54:52 +03:00
if ( ! ( dws - > caps & DW_SPI_CAP_DWC_SSI ) )
/* CTRLR0[ 9:8] Transfer Mode */
2020-10-08 02:54:56 +03:00
cr0 | = cfg - > tmode < < SPI_TMOD_OFFSET ;
2020-10-08 02:54:52 +03:00
else
/* CTRLR0[11:10] Transfer Mode */
2020-10-08 02:54:56 +03:00
cr0 | = cfg - > tmode < < DWC_SSI_CTRLR0_TMOD_OFFSET ;
2020-10-08 02:54:52 +03:00
2020-10-08 02:54:51 +03:00
dw_writel ( dws , DW_SPI_CTRLR0 , cr0 ) ;
2020-10-08 02:54:53 +03:00
2020-10-08 02:54:56 +03:00
if ( cfg - > tmode = = SPI_TMOD_EPROMREAD | | cfg - > tmode = = SPI_TMOD_RO )
dw_writel ( dws , DW_SPI_CTRLR1 , cfg - > ndf ? cfg - > ndf - 1 : 0 ) ;
2020-10-08 02:54:54 +03:00
/* Note DW APB SSI clock divider doesn't support odd numbers */
2020-10-08 02:54:56 +03:00
clk_div = ( DIV_ROUND_UP ( dws - > max_freq , cfg - > freq ) + 1 ) & 0xfffe ;
2020-10-08 02:54:54 +03:00
speed_hz = dws - > max_freq / clk_div ;
if ( dws - > current_freq ! = speed_hz ) {
spi_set_clk ( dws , clk_div ) ;
dws - > current_freq = speed_hz ;
2020-10-08 02:54:53 +03:00
}
2020-10-08 02:54:55 +03:00
/* Update RX sample delay if required */
if ( dws - > cur_rx_sample_dly ! = chip - > rx_sample_dly ) {
dw_writel ( dws , DW_SPI_RX_SAMPLE_DLY , chip - > rx_sample_dly ) ;
dws - > cur_rx_sample_dly = chip - > rx_sample_dly ;
}
2009-12-15 01:20:22 +03:00
}
2020-10-08 02:54:56 +03:00
EXPORT_SYMBOL_GPL ( dw_spi_update_config ) ;
2009-12-15 01:20:22 +03:00
2020-10-08 02:54:59 +03:00
static void dw_spi_irq_setup ( struct dw_spi * dws )
{
u16 level ;
u8 imask ;
/*
* Originally Tx and Rx data lengths match . Rx FIFO Threshold level
* will be adjusted at the final stage of the IRQ - based SPI transfer
* execution so not to lose the leftover of the incoming data .
*/
level = min_t ( u16 , dws - > fifo_len / 2 , dws - > tx_len ) ;
dw_writel ( dws , DW_SPI_TXFTLR , level ) ;
dw_writel ( dws , DW_SPI_RXFTLR , level - 1 ) ;
imask = SPI_INT_TXEI | SPI_INT_TXOI | SPI_INT_RXUI | SPI_INT_RXOI |
SPI_INT_RXFI ;
spi_umask_intr ( dws , imask ) ;
dws - > transfer_handler = dw_spi_transfer_handler ;
}
2018-02-01 18:17:29 +03:00
static int dw_spi_transfer_one ( struct spi_controller * master ,
2015-03-02 15:58:57 +03:00
struct spi_device * spi , struct spi_transfer * transfer )
2009-12-15 01:20:22 +03:00
{
2018-02-01 18:17:29 +03:00
struct dw_spi * dws = spi_controller_get_devdata ( master ) ;
2020-10-08 02:54:56 +03:00
struct dw_spi_cfg cfg = {
. tmode = SPI_TMOD_TR ,
. dfs = transfer - > bits_per_word ,
. freq = transfer - > speed_hz ,
} ;
2015-03-09 17:48:46 +03:00
int ret ;
2009-12-15 01:20:22 +03:00
2015-03-09 17:48:49 +03:00
dws - > dma_mapped = 0 ;
2020-09-20 14:28:47 +03:00
dws - > n_bytes = DIV_ROUND_UP ( transfer - > bits_per_word , BITS_PER_BYTE ) ;
2009-12-15 01:20:22 +03:00
dws - > tx = ( void * ) transfer - > tx_buf ;
2020-10-08 02:54:57 +03:00
dws - > tx_len = transfer - > len / dws - > n_bytes ;
2009-12-15 01:20:22 +03:00
dws - > rx = transfer - > rx_buf ;
2020-10-08 02:54:57 +03:00
dws - > rx_len = dws - > tx_len ;
2009-12-15 01:20:22 +03:00
2020-10-08 02:54:57 +03:00
/* Ensure the data above is visible for all CPUs */
2020-01-03 05:52:10 +03:00
smp_mb ( ) ;
2015-03-02 15:58:56 +03:00
spi_enable_chip ( dws , 0 ) ;
2020-10-08 02:54:56 +03:00
dw_spi_update_config ( dws , spi , & cfg ) ;
2018-09-04 22:49:44 +03:00
2020-10-08 02:54:54 +03:00
transfer - > effective_speed_hz = dws - > current_freq ;
2010-01-21 14:40:52 +03:00
2009-12-15 01:20:22 +03:00
/* Check if current transfer is a DMA transaction */
2015-03-09 17:48:49 +03:00
if ( master - > can_dma & & master - > can_dma ( master , spi , transfer ) )
dws - > dma_mapped = master - > cur_msg_mapped ;
2009-12-15 01:20:22 +03:00
2015-03-02 15:58:56 +03:00
/* For poll mode just disable all interrupts */
spi_mask_intr ( dws , 0xff ) ;
2015-03-09 17:48:46 +03:00
if ( dws - > dma_mapped ) {
2015-03-09 17:48:49 +03:00
ret = dws - > dma_ops - > dma_setup ( dws , transfer ) ;
2015-03-09 17:48:46 +03:00
if ( ret < 0 ) {
spi_enable_chip ( dws , 1 ) ;
return ret ;
}
2020-04-16 14:09:16 +03:00
} else {
2020-10-08 02:54:59 +03:00
dw_spi_irq_setup ( dws ) ;
2009-12-15 01:20:22 +03:00
}
2015-03-02 15:58:56 +03:00
spi_enable_chip ( dws , 1 ) ;
2009-12-15 01:20:22 +03:00
2020-05-29 16:11:51 +03:00
if ( dws - > dma_mapped )
return dws - > dma_ops - > dma_transfer ( dws , transfer ) ;
2009-12-15 01:20:22 +03:00
2015-03-02 15:58:57 +03:00
return 1 ;
2009-12-15 01:20:22 +03:00
}
2018-02-01 18:17:29 +03:00
static void dw_spi_handle_err ( struct spi_controller * master ,
2014-01-31 14:07:44 +04:00
struct spi_message * msg )
2009-12-15 01:20:22 +03:00
{
2018-02-01 18:17:29 +03:00
struct dw_spi * dws = spi_controller_get_devdata ( master ) ;
2009-12-15 01:20:22 +03:00
2015-03-09 17:48:48 +03:00
if ( dws - > dma_mapped )
dws - > dma_ops - > dma_stop ( dws ) ;
2015-03-02 15:58:57 +03:00
spi_reset_chip ( dws ) ;
2009-12-15 01:20:22 +03:00
}
/* This may be called twice for each spi dev */
static int dw_spi_setup ( struct spi_device * spi )
{
2020-10-08 02:54:52 +03:00
struct dw_spi * dws = spi_controller_get_devdata ( spi - > controller ) ;
2009-12-15 01:20:22 +03:00
struct chip_data * chip ;
/* Only alloc on first setup */
chip = spi_get_ctldata ( spi ) ;
if ( ! chip ) {
2020-08-24 23:30:05 +03:00
struct dw_spi * dws = spi_controller_get_devdata ( spi - > controller ) ;
u32 rx_sample_dly_ns ;
2014-08-31 08:47:06 +04:00
chip = kzalloc ( sizeof ( struct chip_data ) , GFP_KERNEL ) ;
2009-12-15 01:20:22 +03:00
if ( ! chip )
return - ENOMEM ;
2013-12-30 22:30:46 +04:00
spi_set_ctldata ( spi , chip ) ;
2020-08-24 23:30:05 +03:00
/* Get specific / default rx-sample-delay */
if ( device_property_read_u32 ( & spi - > dev ,
" rx-sample-delay-ns " ,
& rx_sample_dly_ns ) ! = 0 )
/* Use default controller value */
rx_sample_dly_ns = dws - > def_rx_sample_dly_ns ;
chip - > rx_sample_dly = DIV_ROUND_CLOSEST ( rx_sample_dly_ns ,
NSEC_PER_SEC /
dws - > max_freq ) ;
2009-12-15 01:20:22 +03:00
}
2020-10-08 02:54:52 +03:00
/*
* Update CR0 data each time the setup callback is invoked since
* the device parameters could have been changed , for instance , by
* the MMC SPI driver or something else .
*/
chip - > cr0 = dw_spi_prepare_cr0 ( dws , spi ) ;
2009-12-15 01:20:22 +03:00
return 0 ;
}
2014-08-31 08:47:06 +04:00
static void dw_spi_cleanup ( struct spi_device * spi )
{
struct chip_data * chip = spi_get_ctldata ( spi ) ;
kfree ( chip ) ;
spi_set_ctldata ( spi , NULL ) ;
}
2009-12-15 01:20:22 +03:00
/* Restart the controller, disable all interrupts, clean rx fifo */
2015-01-07 17:56:55 +03:00
static void spi_hw_init ( struct device * dev , struct dw_spi * dws )
2009-12-15 01:20:22 +03:00
{
2015-03-02 15:58:55 +03:00
spi_reset_chip ( dws ) ;
2010-01-21 05:41:10 +03:00
/*
* Try to detect the FIFO depth if not set by interface driver ,
* the depth could be from 2 to 256 from HW spec
*/
if ( ! dws - > fifo_len ) {
u32 fifo ;
2014-09-02 06:49:24 +04:00
2015-02-25 12:39:36 +03:00
for ( fifo = 1 ; fifo < 256 ; fifo + + ) {
2020-05-05 16:06:12 +03:00
dw_writel ( dws , DW_SPI_TXFTLR , fifo ) ;
if ( fifo ! = dw_readl ( dws , DW_SPI_TXFTLR ) )
2010-01-21 05:41:10 +03:00
break ;
}
2020-05-05 16:06:12 +03:00
dw_writel ( dws , DW_SPI_TXFTLR , 0 ) ;
2010-01-21 05:41:10 +03:00
2015-02-25 12:39:36 +03:00
dws - > fifo_len = ( fifo = = 1 ) ? 0 : fifo ;
2015-01-07 17:56:55 +03:00
dev_dbg ( dev , " Detected FIFO size: %u bytes \n " , dws - > fifo_len ) ;
2010-01-21 05:41:10 +03:00
}
2018-10-11 14:20:07 +03:00
/* enable HW fixup for explicit CS deselect for Amazon's alpine chip */
2020-09-20 14:28:53 +03:00
if ( dws - > caps & DW_SPI_CAP_CS_OVERRIDE )
2018-10-11 14:20:07 +03:00
dw_writel ( dws , DW_SPI_CS_OVERRIDE , 0xF ) ;
2009-12-15 01:20:22 +03:00
}
2013-12-30 22:30:44 +04:00
int dw_spi_add_host ( struct device * dev , struct dw_spi * dws )
2009-12-15 01:20:22 +03:00
{
2018-02-01 18:17:29 +03:00
struct spi_controller * master ;
2009-12-15 01:20:22 +03:00
int ret ;
2019-12-06 02:14:21 +03:00
if ( ! dws )
return - EINVAL ;
2009-12-15 01:20:22 +03:00
2013-12-30 22:30:44 +04:00
master = spi_alloc_master ( dev , 0 ) ;
if ( ! master )
return - ENOMEM ;
2009-12-15 01:20:22 +03:00
dws - > master = master ;
2015-10-27 18:48:16 +03:00
dws - > dma_addr = ( dma_addr_t ) ( dws - > paddr + DW_SPI_DR ) ;
2009-12-15 01:20:22 +03:00
2018-07-17 17:23:10 +03:00
spi_controller_set_devdata ( master , dws ) ;
2020-09-20 14:28:50 +03:00
/* Basic HW init */
spi_hw_init ( dev , dws ) ;
2017-01-06 12:35:13 +03:00
ret = request_irq ( dws - > irq , dw_spi_irq , IRQF_SHARED , dev_name ( dev ) ,
master ) ;
2009-12-15 01:20:22 +03:00
if ( ret < 0 ) {
2015-10-14 23:12:17 +03:00
dev_err ( dev , " can not get IRQ \n " ) ;
2009-12-15 01:20:22 +03:00
goto err_free_master ;
}
2019-01-07 18:51:56 +03:00
master - > use_gpio_descriptors = true ;
2014-09-18 21:08:56 +04:00
master - > mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP ;
2018-09-04 22:49:44 +03:00
master - > bits_per_word_mask = SPI_BPW_RANGE_MASK ( 4 , 16 ) ;
2009-12-15 01:20:22 +03:00
master - > bus_num = dws - > bus_num ;
master - > num_chipselect = dws - > num_cs ;
master - > setup = dw_spi_setup ;
2014-08-31 08:47:06 +04:00
master - > cleanup = dw_spi_cleanup ;
2020-10-08 02:54:50 +03:00
if ( dws - > set_cs )
master - > set_cs = dws - > set_cs ;
else
master - > set_cs = dw_spi_set_cs ;
2015-03-02 15:58:57 +03:00
master - > transfer_one = dw_spi_transfer_one ;
master - > handle_err = dw_spi_handle_err ;
2014-02-20 17:37:56 +04:00
master - > max_speed_hz = dws - > max_freq ;
2014-10-08 22:51:34 +04:00
master - > dev . of_node = dev - > of_node ;
2018-12-03 06:15:50 +03:00
master - > dev . fwnode = dev - > fwnode ;
2016-10-10 17:25:25 +03:00
master - > flags = SPI_MASTER_GPIO_SS ;
2019-09-18 11:04:35 +03:00
master - > auto_runtime_pm = true ;
2009-12-15 01:20:22 +03:00
2020-08-24 23:30:05 +03:00
/* Get default rx sample delay */
device_property_read_u32 ( dev , " rx-sample-delay-ns " ,
& dws - > def_rx_sample_dly_ns ) ;
2010-12-24 08:59:11 +03:00
if ( dws - > dma_ops & & dws - > dma_ops - > dma_init ) {
2020-05-06 18:30:24 +03:00
ret = dws - > dma_ops - > dma_init ( dev , dws ) ;
2010-12-24 08:59:11 +03:00
if ( ret ) {
2015-01-07 17:56:54 +03:00
dev_warn ( dev , " DMA init failed \n " ) ;
2015-03-09 17:48:49 +03:00
} else {
master - > can_dma = dws - > dma_ops - > can_dma ;
2020-05-29 16:11:57 +03:00
master - > flags | = SPI_CONTROLLER_MUST_TX ;
2010-12-24 08:59:11 +03:00
}
}
2020-05-25 15:25:01 +03:00
ret = spi_register_controller ( master ) ;
2009-12-15 01:20:22 +03:00
if ( ret ) {
dev_err ( & master - > dev , " problem registering spi master \n " ) ;
2014-01-31 14:07:44 +04:00
goto err_dma_exit ;
2009-12-15 01:20:22 +03:00
}
2014-09-12 16:11:56 +04:00
dw_spi_debugfs_init ( dws ) ;
2009-12-15 01:20:22 +03:00
return 0 ;
2014-01-31 14:07:44 +04:00
err_dma_exit :
2010-12-24 08:59:11 +03:00
if ( dws - > dma_ops & & dws - > dma_ops - > dma_exit )
dws - > dma_ops - > dma_exit ( dws ) ;
2009-12-15 01:20:22 +03:00
spi_enable_chip ( dws , 0 ) ;
2015-10-20 12:11:40 +03:00
free_irq ( dws - > irq , master ) ;
2009-12-15 01:20:22 +03:00
err_free_master :
2018-02-01 18:17:29 +03:00
spi_controller_put ( master ) ;
2009-12-15 01:20:22 +03:00
return ret ;
}
2010-12-24 08:59:10 +03:00
EXPORT_SYMBOL_GPL ( dw_spi_add_host ) ;
2009-12-15 01:20:22 +03:00
2012-12-07 20:57:14 +04:00
void dw_spi_remove_host ( struct dw_spi * dws )
2009-12-15 01:20:22 +03:00
{
2014-09-12 16:11:56 +04:00
dw_spi_debugfs_remove ( dws ) ;
2009-12-15 01:20:22 +03:00
2020-05-25 15:25:01 +03:00
spi_unregister_controller ( dws - > master ) ;
2010-12-24 08:59:11 +03:00
if ( dws - > dma_ops & & dws - > dma_ops - > dma_exit )
dws - > dma_ops - > dma_exit ( dws ) ;
2015-10-14 23:12:23 +03:00
spi_shutdown_chip ( dws ) ;
2015-10-20 12:11:40 +03:00
free_irq ( dws - > irq , dws - > master ) ;
2009-12-15 01:20:22 +03:00
}
2010-12-24 08:59:10 +03:00
EXPORT_SYMBOL_GPL ( dw_spi_remove_host ) ;
2009-12-15 01:20:22 +03:00
int dw_spi_suspend_host ( struct dw_spi * dws )
{
2015-10-14 23:12:23 +03:00
int ret ;
2009-12-15 01:20:22 +03:00
2018-02-01 18:17:29 +03:00
ret = spi_controller_suspend ( dws - > master ) ;
2009-12-15 01:20:22 +03:00
if ( ret )
return ret ;
2015-10-14 23:12:23 +03:00
spi_shutdown_chip ( dws ) ;
return 0 ;
2009-12-15 01:20:22 +03:00
}
2010-12-24 08:59:10 +03:00
EXPORT_SYMBOL_GPL ( dw_spi_suspend_host ) ;
2009-12-15 01:20:22 +03:00
int dw_spi_resume_host ( struct dw_spi * dws )
{
2015-01-07 17:56:55 +03:00
spi_hw_init ( & dws - > master - > dev , dws ) ;
2018-09-05 11:51:57 +03:00
return spi_controller_resume ( dws - > master ) ;
2009-12-15 01:20:22 +03:00
}
2010-12-24 08:59:10 +03:00
EXPORT_SYMBOL_GPL ( dw_spi_resume_host ) ;
2009-12-15 01:20:22 +03:00
MODULE_AUTHOR ( " Feng Tang <feng.tang@intel.com> " ) ;
MODULE_DESCRIPTION ( " Driver for DesignWare SPI controller core " ) ;
MODULE_LICENSE ( " GPL v2 " ) ;