2018-08-22 01:02:23 +03:00
// SPDX-License-Identifier: GPL-2.0
2011-02-15 04:30:32 +03:00
/*
* SH SPI bus driver
*
* Copyright ( C ) 2011 Renesas Solutions Corp .
*
* Based on pxa2xx_spi . c :
* Copyright ( C ) 2005 Stephen Street / StreetFire Sound Labs
*/
# include <linux/module.h>
# include <linux/kernel.h>
# include <linux/sched.h>
# include <linux/errno.h>
# include <linux/timer.h>
# include <linux/delay.h>
# include <linux/list.h>
# include <linux/workqueue.h>
# include <linux/interrupt.h>
# include <linux/platform_device.h>
# include <linux/io.h>
# include <linux/spi/spi.h>
# define SPI_SH_TBR 0x00
# define SPI_SH_RBR 0x00
# define SPI_SH_CR1 0x08
# define SPI_SH_CR2 0x10
# define SPI_SH_CR3 0x18
# define SPI_SH_CR4 0x20
# define SPI_SH_CR5 0x28
/* CR1 */
# define SPI_SH_TBE 0x80
# define SPI_SH_TBF 0x40
# define SPI_SH_RBE 0x20
# define SPI_SH_RBF 0x10
# define SPI_SH_PFONRD 0x08
# define SPI_SH_SSDB 0x04
# define SPI_SH_SSD 0x02
# define SPI_SH_SSA 0x01
/* CR2 */
# define SPI_SH_RSTF 0x80
# define SPI_SH_LOOPBK 0x40
# define SPI_SH_CPOL 0x20
# define SPI_SH_CPHA 0x10
# define SPI_SH_L1M0 0x08
/* CR3 */
# define SPI_SH_MAX_BYTE 0xFF
/* CR4 */
# define SPI_SH_TBEI 0x80
# define SPI_SH_TBFI 0x40
# define SPI_SH_RBEI 0x20
# define SPI_SH_RBFI 0x10
# define SPI_SH_WPABRT 0x04
# define SPI_SH_SSS 0x01
/* CR8 */
# define SPI_SH_P1L0 0x80
# define SPI_SH_PP1L0 0x40
# define SPI_SH_MUXI 0x20
# define SPI_SH_MUXIRQ 0x10
# define SPI_SH_FIFO_SIZE 32
# define SPI_SH_SEND_TIMEOUT (3 * HZ)
# define SPI_SH_RECEIVE_TIMEOUT (HZ >> 3)
# undef DEBUG
struct spi_sh_data {
void __iomem * addr ;
int irq ;
2023-08-18 12:31:52 +03:00
struct spi_controller * host ;
2011-02-15 04:30:32 +03:00
unsigned long cr1 ;
wait_queue_head_t wait ;
2012-03-07 09:45:37 +04:00
int width ;
2011-02-15 04:30:32 +03:00
} ;
static void spi_sh_write ( struct spi_sh_data * ss , unsigned long data ,
unsigned long offset )
{
2012-03-07 09:45:37 +04:00
if ( ss - > width = = 8 )
iowrite8 ( data , ss - > addr + ( offset > > 2 ) ) ;
else if ( ss - > width = = 32 )
iowrite32 ( data , ss - > addr + offset ) ;
2011-02-15 04:30:32 +03:00
}
static unsigned long spi_sh_read ( struct spi_sh_data * ss , unsigned long offset )
{
2012-03-07 09:45:37 +04:00
if ( ss - > width = = 8 )
return ioread8 ( ss - > addr + ( offset > > 2 ) ) ;
else if ( ss - > width = = 32 )
return ioread32 ( ss - > addr + offset ) ;
else
return 0 ;
2011-02-15 04:30:32 +03:00
}
static void spi_sh_set_bit ( struct spi_sh_data * ss , unsigned long val ,
unsigned long offset )
{
unsigned long tmp ;
tmp = spi_sh_read ( ss , offset ) ;
tmp | = val ;
spi_sh_write ( ss , tmp , offset ) ;
}
static void spi_sh_clear_bit ( struct spi_sh_data * ss , unsigned long val ,
unsigned long offset )
{
unsigned long tmp ;
tmp = spi_sh_read ( ss , offset ) ;
tmp & = ~ val ;
spi_sh_write ( ss , tmp , offset ) ;
}
static void clear_fifo ( struct spi_sh_data * ss )
{
spi_sh_set_bit ( ss , SPI_SH_RSTF , SPI_SH_CR2 ) ;
spi_sh_clear_bit ( ss , SPI_SH_RSTF , SPI_SH_CR2 ) ;
}
static int spi_sh_wait_receive_buffer ( struct spi_sh_data * ss )
{
int timeout = 100000 ;
while ( spi_sh_read ( ss , SPI_SH_CR1 ) & SPI_SH_RBE ) {
udelay ( 10 ) ;
if ( timeout - - < 0 )
return - ETIMEDOUT ;
}
return 0 ;
}
static int spi_sh_wait_write_buffer_empty ( struct spi_sh_data * ss )
{
int timeout = 100000 ;
while ( ! ( spi_sh_read ( ss , SPI_SH_CR1 ) & SPI_SH_TBE ) ) {
udelay ( 10 ) ;
if ( timeout - - < 0 )
return - ETIMEDOUT ;
}
return 0 ;
}
static int spi_sh_send ( struct spi_sh_data * ss , struct spi_message * mesg ,
struct spi_transfer * t )
{
int i , retval = 0 ;
int remain = t - > len ;
int cur_len ;
unsigned char * data ;
long ret ;
if ( t - > len )
spi_sh_set_bit ( ss , SPI_SH_SSA , SPI_SH_CR1 ) ;
data = ( unsigned char * ) t - > tx_buf ;
while ( remain > 0 ) {
cur_len = min ( SPI_SH_FIFO_SIZE , remain ) ;
for ( i = 0 ; i < cur_len & &
! ( spi_sh_read ( ss , SPI_SH_CR4 ) &
SPI_SH_WPABRT ) & &
! ( spi_sh_read ( ss , SPI_SH_CR1 ) & SPI_SH_TBF ) ;
i + + )
spi_sh_write ( ss , ( unsigned long ) data [ i ] , SPI_SH_TBR ) ;
if ( spi_sh_read ( ss , SPI_SH_CR4 ) & SPI_SH_WPABRT ) {
/* Abort SPI operation */
spi_sh_set_bit ( ss , SPI_SH_WPABRT , SPI_SH_CR4 ) ;
retval = - EIO ;
break ;
}
cur_len = i ;
remain - = cur_len ;
data + = cur_len ;
if ( remain > 0 ) {
ss - > cr1 & = ~ SPI_SH_TBE ;
spi_sh_set_bit ( ss , SPI_SH_TBE , SPI_SH_CR4 ) ;
ret = wait_event_interruptible_timeout ( ss - > wait ,
ss - > cr1 & SPI_SH_TBE ,
SPI_SH_SEND_TIMEOUT ) ;
if ( ret = = 0 & & ! ( ss - > cr1 & SPI_SH_TBE ) ) {
printk ( KERN_ERR " %s: timeout \n " , __func__ ) ;
return - ETIMEDOUT ;
}
}
}
if ( list_is_last ( & t - > transfer_list , & mesg - > transfers ) ) {
2014-01-13 06:00:44 +04:00
spi_sh_clear_bit ( ss , SPI_SH_SSD | SPI_SH_SSDB , SPI_SH_CR1 ) ;
2011-02-15 04:30:32 +03:00
spi_sh_set_bit ( ss , SPI_SH_SSA , SPI_SH_CR1 ) ;
ss - > cr1 & = ~ SPI_SH_TBE ;
spi_sh_set_bit ( ss , SPI_SH_TBE , SPI_SH_CR4 ) ;
ret = wait_event_interruptible_timeout ( ss - > wait ,
ss - > cr1 & SPI_SH_TBE ,
SPI_SH_SEND_TIMEOUT ) ;
if ( ret = = 0 & & ( ss - > cr1 & SPI_SH_TBE ) ) {
printk ( KERN_ERR " %s: timeout \n " , __func__ ) ;
return - ETIMEDOUT ;
}
}
return retval ;
}
static int spi_sh_receive ( struct spi_sh_data * ss , struct spi_message * mesg ,
struct spi_transfer * t )
{
int i ;
int remain = t - > len ;
int cur_len ;
unsigned char * data ;
long ret ;
if ( t - > len > SPI_SH_MAX_BYTE )
spi_sh_write ( ss , SPI_SH_MAX_BYTE , SPI_SH_CR3 ) ;
else
spi_sh_write ( ss , t - > len , SPI_SH_CR3 ) ;
2014-01-13 06:00:44 +04:00
spi_sh_clear_bit ( ss , SPI_SH_SSD | SPI_SH_SSDB , SPI_SH_CR1 ) ;
2011-02-15 04:30:32 +03:00
spi_sh_set_bit ( ss , SPI_SH_SSA , SPI_SH_CR1 ) ;
spi_sh_wait_write_buffer_empty ( ss ) ;
data = ( unsigned char * ) t - > rx_buf ;
while ( remain > 0 ) {
if ( remain > = SPI_SH_FIFO_SIZE ) {
ss - > cr1 & = ~ SPI_SH_RBF ;
spi_sh_set_bit ( ss , SPI_SH_RBF , SPI_SH_CR4 ) ;
ret = wait_event_interruptible_timeout ( ss - > wait ,
ss - > cr1 & SPI_SH_RBF ,
SPI_SH_RECEIVE_TIMEOUT ) ;
if ( ret = = 0 & &
spi_sh_read ( ss , SPI_SH_CR1 ) & SPI_SH_RBE ) {
printk ( KERN_ERR " %s: timeout \n " , __func__ ) ;
return - ETIMEDOUT ;
}
}
cur_len = min ( SPI_SH_FIFO_SIZE , remain ) ;
for ( i = 0 ; i < cur_len ; i + + ) {
if ( spi_sh_wait_receive_buffer ( ss ) )
break ;
data [ i ] = ( unsigned char ) spi_sh_read ( ss , SPI_SH_RBR ) ;
}
remain - = cur_len ;
data + = cur_len ;
}
/* deassert CS when SPI is receiving. */
if ( t - > len > SPI_SH_MAX_BYTE ) {
clear_fifo ( ss ) ;
spi_sh_write ( ss , 1 , SPI_SH_CR3 ) ;
} else {
spi_sh_write ( ss , 0 , SPI_SH_CR3 ) ;
}
return 0 ;
}
2022-06-10 18:46:49 +03:00
static int spi_sh_transfer_one_message ( struct spi_controller * ctlr ,
struct spi_message * mesg )
2011-02-15 04:30:32 +03:00
{
2022-06-10 18:46:49 +03:00
struct spi_sh_data * ss = spi_controller_get_devdata ( ctlr ) ;
2011-02-15 04:30:32 +03:00
struct spi_transfer * t ;
int ret ;
pr_debug ( " %s: enter \n " , __func__ ) ;
2022-06-10 18:46:49 +03:00
spi_sh_clear_bit ( ss , SPI_SH_SSA , SPI_SH_CR1 ) ;
2011-02-15 04:30:32 +03:00
2022-06-10 18:46:49 +03:00
list_for_each_entry ( t , & mesg - > transfers , transfer_list ) {
pr_debug ( " tx_buf = %p, rx_buf = %p \n " ,
t - > tx_buf , t - > rx_buf ) ;
pr_debug ( " len = %d, delay.value = %d \n " ,
t - > len , t - > delay . value ) ;
if ( t - > tx_buf ) {
ret = spi_sh_send ( ss , mesg , t ) ;
if ( ret < 0 )
goto error ;
}
if ( t - > rx_buf ) {
ret = spi_sh_receive ( ss , mesg , t ) ;
if ( ret < 0 )
goto error ;
}
mesg - > actual_length + = t - > len ;
2011-02-15 04:30:32 +03:00
}
2022-06-10 18:46:49 +03:00
mesg - > status = 0 ;
spi_finalize_current_message ( ctlr ) ;
2011-02-15 04:30:32 +03:00
clear_fifo ( ss ) ;
spi_sh_set_bit ( ss , SPI_SH_SSD , SPI_SH_CR1 ) ;
udelay ( 100 ) ;
spi_sh_clear_bit ( ss , SPI_SH_SSA | SPI_SH_SSDB | SPI_SH_SSD ,
SPI_SH_CR1 ) ;
clear_fifo ( ss ) ;
2022-06-10 18:46:49 +03:00
return 0 ;
2011-02-15 04:30:32 +03:00
error :
mesg - > status = ret ;
2022-06-10 18:46:49 +03:00
spi_finalize_current_message ( ctlr ) ;
2014-04-02 18:21:04 +04:00
if ( mesg - > complete )
mesg - > complete ( mesg - > context ) ;
2011-02-15 04:30:32 +03:00
spi_sh_clear_bit ( ss , SPI_SH_SSA | SPI_SH_SSDB | SPI_SH_SSD ,
SPI_SH_CR1 ) ;
clear_fifo ( ss ) ;
2022-06-10 18:46:49 +03:00
return ret ;
2011-02-15 04:30:32 +03:00
}
static int spi_sh_setup ( struct spi_device * spi )
{
2023-08-18 12:31:52 +03:00
struct spi_sh_data * ss = spi_controller_get_devdata ( spi - > controller ) ;
2011-02-15 04:30:32 +03:00
pr_debug ( " %s: enter \n " , __func__ ) ;
spi_sh_write ( ss , 0xfe , SPI_SH_CR1 ) ; /* SPI sycle stop */
spi_sh_write ( ss , 0x00 , SPI_SH_CR1 ) ; /* CR1 init */
spi_sh_write ( ss , 0x00 , SPI_SH_CR3 ) ; /* CR3 init */
clear_fifo ( ss ) ;
/* 1/8 clock */
spi_sh_write ( ss , spi_sh_read ( ss , SPI_SH_CR2 ) | 0x07 , SPI_SH_CR2 ) ;
udelay ( 10 ) ;
return 0 ;
}
static void spi_sh_cleanup ( struct spi_device * spi )
{
2023-08-18 12:31:52 +03:00
struct spi_sh_data * ss = spi_controller_get_devdata ( spi - > controller ) ;
2011-02-15 04:30:32 +03:00
pr_debug ( " %s: enter \n " , __func__ ) ;
spi_sh_clear_bit ( ss , SPI_SH_SSA | SPI_SH_SSDB | SPI_SH_SSD ,
SPI_SH_CR1 ) ;
}
static irqreturn_t spi_sh_irq ( int irq , void * _ss )
{
struct spi_sh_data * ss = ( struct spi_sh_data * ) _ss ;
unsigned long cr1 ;
cr1 = spi_sh_read ( ss , SPI_SH_CR1 ) ;
if ( cr1 & SPI_SH_TBE )
ss - > cr1 | = SPI_SH_TBE ;
if ( cr1 & SPI_SH_TBF )
ss - > cr1 | = SPI_SH_TBF ;
if ( cr1 & SPI_SH_RBE )
ss - > cr1 | = SPI_SH_RBE ;
if ( cr1 & SPI_SH_RBF )
ss - > cr1 | = SPI_SH_RBF ;
if ( ss - > cr1 ) {
spi_sh_clear_bit ( ss , ss - > cr1 , SPI_SH_CR4 ) ;
wake_up ( & ss - > wait ) ;
}
return IRQ_HANDLED ;
}
2023-03-03 20:20:20 +03:00
static void spi_sh_remove ( struct platform_device * pdev )
2011-02-15 04:30:32 +03:00
{
2013-05-23 14:20:40 +04:00
struct spi_sh_data * ss = platform_get_drvdata ( pdev ) ;
2011-02-15 04:30:32 +03:00
2023-08-18 12:31:52 +03:00
spi_unregister_controller ( ss - > host ) ;
2011-02-15 04:30:32 +03:00
free_irq ( ss - > irq , ss ) ;
}
2012-12-07 20:57:14 +04:00
static int spi_sh_probe ( struct platform_device * pdev )
2011-02-15 04:30:32 +03:00
{
struct resource * res ;
2023-08-18 12:31:52 +03:00
struct spi_controller * host ;
2011-02-15 04:30:32 +03:00
struct spi_sh_data * ss ;
int ret , irq ;
/* get base addr */
res = platform_get_resource ( pdev , IORESOURCE_MEM , 0 ) ;
if ( unlikely ( res = = NULL ) ) {
dev_err ( & pdev - > dev , " invalid resource \n " ) ;
return - EINVAL ;
}
irq = platform_get_irq ( pdev , 0 ) ;
2019-07-30 21:15:41 +03:00
if ( irq < 0 )
2017-08-09 18:46:48 +03:00
return irq ;
2011-02-15 04:30:32 +03:00
2023-08-18 12:31:52 +03:00
host = devm_spi_alloc_host ( & pdev - > dev , sizeof ( struct spi_sh_data ) ) ;
if ( host = = NULL ) {
dev_err ( & pdev - > dev , " devm_spi_alloc_host error. \n " ) ;
2011-02-15 04:30:32 +03:00
return - ENOMEM ;
}
2023-08-18 12:31:52 +03:00
ss = spi_controller_get_devdata ( host ) ;
2013-05-23 14:20:40 +04:00
platform_set_drvdata ( pdev , ss ) ;
2011-02-15 04:30:32 +03:00
2012-03-07 09:45:37 +04:00
switch ( res - > flags & IORESOURCE_MEM_TYPE_MASK ) {
case IORESOURCE_MEM_8BIT :
ss - > width = 8 ;
break ;
case IORESOURCE_MEM_32BIT :
ss - > width = 32 ;
break ;
default :
dev_err ( & pdev - > dev , " No support width \n " ) ;
2020-12-07 11:17:04 +03:00
return - ENODEV ;
2012-03-07 09:45:37 +04:00
}
2011-02-15 04:30:32 +03:00
ss - > irq = irq ;
2023-08-18 12:31:52 +03:00
ss - > host = host ;
2014-07-06 20:14:50 +04:00
ss - > addr = devm_ioremap ( & pdev - > dev , res - > start , resource_size ( res ) ) ;
2011-02-15 04:30:32 +03:00
if ( ss - > addr = = NULL ) {
dev_err ( & pdev - > dev , " ioremap error. \n " ) ;
2020-12-07 11:17:04 +03:00
return - ENOMEM ;
2011-02-15 04:30:32 +03:00
}
init_waitqueue_head ( & ss - > wait ) ;
2011-10-22 13:56:55 +04:00
ret = request_irq ( irq , spi_sh_irq , 0 , " spi_sh " , ss ) ;
2011-02-15 04:30:32 +03:00
if ( ret < 0 ) {
dev_err ( & pdev - > dev , " request_irq error \n " ) ;
2020-12-07 11:17:04 +03:00
return ret ;
2011-02-15 04:30:32 +03:00
}
2023-08-18 12:31:52 +03:00
host - > num_chipselect = 2 ;
host - > bus_num = pdev - > id ;
host - > setup = spi_sh_setup ;
host - > transfer_one_message = spi_sh_transfer_one_message ;
host - > cleanup = spi_sh_cleanup ;
2011-02-15 04:30:32 +03:00
2023-08-18 12:31:52 +03:00
ret = spi_register_controller ( host ) ;
2011-02-15 04:30:32 +03:00
if ( ret < 0 ) {
2023-08-18 12:31:52 +03:00
printk ( KERN_ERR " spi_register_controller error. \n " ) ;
2014-07-06 20:14:50 +04:00
goto error3 ;
2011-02-15 04:30:32 +03:00
}
return 0 ;
error3 :
2014-07-06 20:14:50 +04:00
free_irq ( irq , ss ) ;
2011-02-15 04:30:32 +03:00
return ret ;
}
static struct platform_driver spi_sh_driver = {
. probe = spi_sh_probe ,
2023-03-03 20:20:20 +03:00
. remove_new = spi_sh_remove ,
2011-02-15 04:30:32 +03:00
. driver = {
. name = " sh_spi " ,
} ,
} ;
2011-10-05 21:29:49 +04:00
module_platform_driver ( spi_sh_driver ) ;
2011-02-15 04:30:32 +03:00
MODULE_DESCRIPTION ( " SH SPI bus driver " ) ;
2018-08-22 01:02:23 +03:00
MODULE_LICENSE ( " GPL v2 " ) ;
2011-02-15 04:30:32 +03:00
MODULE_AUTHOR ( " Yoshihiro Shimoda " ) ;
MODULE_ALIAS ( " platform:sh_spi " ) ;