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>
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
/* Slave spi_dev related */
struct chip_data {
u8 tmode ; /* TR/TO/RO/EEPROM */
u8 type ; /* SPI/SSP/MicroWire */
u16 clk_div ; /* baud rate divider */
u32 speed_hz ; /* baud rate */
} ;
# 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 ) ,
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 ) ) ;
2018-10-11 14:20:07 +03:00
else if ( dws - > cs_override )
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 )
{
u32 tx_left , tx_room , rxtx_gap ;
tx_left = ( dws - > tx_end - dws - > tx ) / dws - > n_bytes ;
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 .
*/
rxtx_gap = ( ( dws - > rx_end - dws - > rx ) - ( dws - > tx_end - dws - > tx ) )
/ dws - > n_bytes ;
return min3 ( tx_left , tx_room , ( u32 ) ( dws - > fifo_len - rxtx_gap ) ) ;
}
/* Return the max entries we should read out of rx fifo */
static inline u32 rx_max ( struct dw_spi * dws )
{
u32 rx_left = ( dws - > rx_end - dws - > rx ) / dws - > n_bytes ;
2015-03-12 22:19:31 +03:00
return min_t ( u32 , rx_left , 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-01-01 06:39:41 +03:00
u32 max ;
2011-03-30 19:09:52 +04:00
u16 txw = 0 ;
2009-12-15 01:20:22 +03:00
2020-01-01 06:39:41 +03:00
spin_lock ( & dws - > buf_lock ) ;
max = tx_max ( dws ) ;
2011-03-30 19:09:54 +04:00
while ( max - - ) {
/* Set the tx word if the transfer's original "tx" is not null */
if ( dws - > tx_end - dws - > len ) {
if ( dws - > n_bytes = = 1 )
txw = * ( u8 * ) ( dws - > tx ) ;
else
txw = * ( u16 * ) ( dws - > tx ) ;
}
2015-08-18 23:21:53 +03:00
dw_write_io_reg ( dws , DW_SPI_DR , txw ) ;
2011-03-30 19:09:54 +04:00
dws - > tx + = dws - > n_bytes ;
2009-12-15 01:20:22 +03:00
}
2020-01-01 06:39:41 +03:00
spin_unlock ( & dws - > buf_lock ) ;
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-01-01 06:39:41 +03:00
u32 max ;
2011-03-30 19:09:52 +04:00
u16 rxw ;
2009-12-15 01:20:22 +03:00
2020-01-01 06:39:41 +03:00
spin_lock ( & dws - > buf_lock ) ;
max = rx_max ( dws ) ;
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 ) ;
2011-03-30 19:09:52 +04:00
/* Care rx only if the transfer's original "rx" is not null */
if ( dws - > rx_end - dws - > len ) {
if ( dws - > n_bytes = = 1 )
* ( u8 * ) ( dws - > rx ) = rxw ;
else
* ( u16 * ) ( dws - > rx ) = rxw ;
}
dws - > rx + = dws - > n_bytes ;
2009-12-15 01:20:22 +03:00
}
2020-01-01 06:39:41 +03:00
spin_unlock ( & dws - > buf_lock ) ;
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
}
static irqreturn_t interrupt_transfer ( struct dw_spi * dws )
{
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 ;
}
2011-03-30 19:09:55 +04:00
dw_reader ( dws ) ;
if ( dws - > rx_end = = dws - > rx ) {
spi_mask_intr ( dws , SPI_INT_TXEI ) ;
2015-03-02 15:58:57 +03:00
spi_finalize_current_transfer ( dws - > master ) ;
2011-03-30 19:09:55 +04:00
return IRQ_HANDLED ;
}
2010-01-20 23:49:45 +03:00
if ( irq_status & SPI_INT_TXEI ) {
spi_mask_intr ( dws , SPI_INT_TXEI ) ;
2011-03-30 19:09:55 +04:00
dw_writer ( dws ) ;
/* Enable TX irq always, it will be disabled when RX finished */
spi_umask_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 ) {
2009-12-15 01:20:22 +03:00
spi_mask_intr ( dws , SPI_INT_TXEI ) ;
return IRQ_HANDLED ;
}
return dws - > transfer_handler ( dws ) ;
}
2020-05-05 16:06:13 +03:00
/* Configure CTRLR0 for DW_apb_ssi */
u32 dw_spi_update_cr0 ( struct spi_controller * master , struct spi_device * spi ,
struct spi_transfer * transfer )
2009-12-15 01:20:22 +03:00
{
2020-05-05 16:06:13 +03:00
struct chip_data * chip = spi_get_ctldata ( spi ) ;
u32 cr0 ;
2009-12-15 01:20:22 +03:00
2020-05-05 16:06:13 +03:00
/* Default SPI mode is SCPOL = 0, SCPH = 0 */
cr0 = ( transfer - > bits_per_word - 1 )
| ( chip - > type < < SPI_FRF_OFFSET )
| ( ( ( ( spi - > mode & SPI_CPOL ) ? 1 : 0 ) < < SPI_SCOL_OFFSET ) |
( ( ( spi - > mode & SPI_CPHA ) ? 1 : 0 ) < < SPI_SCPH_OFFSET ) |
( ( ( spi - > mode & SPI_LOOP ) ? 1 : 0 ) < < SPI_SRL_OFFSET ) )
| ( chip - > tmode < < SPI_TMOD_OFFSET ) ;
return cr0 ;
}
EXPORT_SYMBOL_GPL ( dw_spi_update_cr0 ) ;
2020-05-05 16:06:14 +03:00
/* Configure CTRLR0 for DWC_ssi */
u32 dw_spi_update_cr0_v1_01a ( struct spi_controller * master ,
struct spi_device * spi ,
struct spi_transfer * transfer )
{
struct chip_data * chip = spi_get_ctldata ( spi ) ;
u32 cr0 ;
/* CTRLR0[ 4: 0] Data Frame Size */
cr0 = ( transfer - > bits_per_word - 1 ) ;
/* CTRLR0[ 7: 6] Frame Format */
cr0 | = chip - > type < < DWC_SSI_CTRLR0_FRF_OFFSET ;
/*
* 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 ;
/* CTRLR0[11:10] Transfer Mode */
cr0 | = chip - > tmode < < DWC_SSI_CTRLR0_TMOD_OFFSET ;
/* CTRLR0[13] Shift Register Loop */
cr0 | = ( ( spi - > mode & SPI_LOOP ) ? 1 : 0 ) < < DWC_SSI_CTRLR0_SRL_OFFSET ;
return cr0 ;
2009-12-15 01:20:22 +03:00
}
2020-05-05 16:06:14 +03:00
EXPORT_SYMBOL_GPL ( dw_spi_update_cr0_v1_01a ) ;
2009-12-15 01:20:22 +03:00
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 ) ;
2015-03-02 15:58:57 +03:00
struct chip_data * chip = spi_get_ctldata ( spi ) ;
2020-01-01 06:39:41 +03:00
unsigned long flags ;
2009-12-15 01:20:22 +03:00
u8 imask = 0 ;
2015-02-24 14:32:11 +03:00
u16 txlevel = 0 ;
2015-10-14 23:12:18 +03:00
u32 cr0 ;
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-01-01 06:39:41 +03:00
spin_lock_irqsave ( & dws - > buf_lock , flags ) ;
2009-12-15 01:20:22 +03:00
dws - > tx = ( void * ) transfer - > tx_buf ;
dws - > tx_end = dws - > tx + transfer - > len ;
dws - > rx = transfer - > rx_buf ;
dws - > rx_end = dws - > rx + transfer - > len ;
2015-03-02 15:58:57 +03:00
dws - > len = transfer - > len ;
2020-01-01 06:39:41 +03:00
spin_unlock_irqrestore ( & dws - > buf_lock , flags ) ;
2009-12-15 01:20:22 +03:00
2020-01-03 05:52:10 +03:00
/* Ensure dw->rx and dw->rx_end are visible */
smp_mb ( ) ;
2015-03-02 15:58:56 +03:00
spi_enable_chip ( dws , 0 ) ;
2009-12-15 01:20:22 +03:00
/* Handle per transfer options for bpw and speed */
2016-09-04 03:04:49 +03:00
if ( transfer - > speed_hz ! = dws - > current_freq ) {
if ( transfer - > speed_hz ! = chip - > speed_hz ) {
/* clk_div doesn't support odd number */
2016-09-07 18:45:30 +03:00
chip - > clk_div = ( DIV_ROUND_UP ( dws - > max_freq , transfer - > speed_hz ) + 1 ) & 0xfffe ;
2016-09-04 03:04:49 +03:00
chip - > speed_hz = transfer - > speed_hz ;
}
dws - > current_freq = transfer - > speed_hz ;
2015-09-15 16:26:23 +03:00
spi_set_clk ( dws , chip - > clk_div ) ;
2009-12-15 01:20:22 +03:00
}
2018-09-04 22:49:44 +03:00
2020-05-29 16:11:50 +03:00
transfer - > effective_speed_hz = dws - > max_freq / chip - > clk_div ;
2018-09-04 22:49:44 +03:00
dws - > n_bytes = DIV_ROUND_UP ( transfer - > bits_per_word , BITS_PER_BYTE ) ;
2010-01-21 14:40:52 +03:00
2020-05-05 16:06:13 +03:00
cr0 = dws - > update_cr0 ( master , spi , transfer ) ;
2020-05-05 16:06:12 +03:00
dw_writel ( dws , DW_SPI_CTRLR0 , cr0 ) ;
2015-03-02 15:58:56 +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 ) ;
2010-01-20 23:49:45 +03:00
/*
* Interrupt mode
* we only need set the TXEI IRQ , as TX / RX always happen syncronizely
*/
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 {
2015-02-24 14:32:11 +03:00
txlevel = min_t ( u16 , dws - > fifo_len / 2 , dws - > len / dws - > n_bytes ) ;
2020-05-05 16:06:12 +03:00
dw_writel ( dws , DW_SPI_TXFTLR , txlevel ) ;
2010-01-20 23:49:45 +03:00
2015-03-02 15:58:56 +03:00
/* Set the interrupt mask */
2014-09-02 06:49:24 +04:00
imask | = SPI_INT_TXEI | SPI_INT_TXOI |
SPI_INT_RXUI | SPI_INT_RXOI ;
2015-03-02 15:58:56 +03:00
spi_umask_intr ( dws , imask ) ;
2009-12-15 01:20:22 +03:00
dws - > transfer_handler = interrupt_transfer ;
}
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 )
{
struct chip_data * chip ;
/* Only alloc on first setup */
chip = spi_get_ctldata ( spi ) ;
if ( ! chip ) {
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 ) ;
2009-12-15 01:20:22 +03:00
}
2015-12-23 14:05:39 +03:00
chip - > tmode = SPI_TMOD_TR ;
2014-09-18 21:08:56 +04:00
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 */
if ( dws - > cs_override )
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 ;
dws - > type = SSI_MOTO_SPI ;
2015-10-27 18:48:16 +03:00
dws - > dma_addr = ( dma_addr_t ) ( dws - > paddr + DW_SPI_DR ) ;
2020-01-01 06:39:41 +03:00
spin_lock_init ( & dws - > buf_lock ) ;
2009-12-15 01:20:22 +03:00
2018-07-17 17:23:10 +03:00
spi_controller_set_devdata ( master , 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 ;
2015-03-02 15:58:57 +03:00
master - > set_cs = dw_spi_set_cs ;
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
2018-07-17 17:23:11 +03:00
if ( dws - > set_cs )
master - > set_cs = dws - > set_cs ;
2009-12-15 01:20:22 +03:00
/* Basic HW init */
2015-01-07 17:56:55 +03:00
spi_hw_init ( dev , dws ) ;
2009-12-15 01:20:22 +03:00
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 " ) ;