2011-06-06 11:16:30 +04:00
/*
2009-12-01 17:29:20 +03:00
* Copyright ( c ) 2009 Nuvoton technology .
* Wan ZongShun < mcuos . com @ gmail . com >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation .
*
2011-06-06 11:16:30 +04:00
*/
2009-12-01 17:29:20 +03:00
2011-11-24 07:10:51 +04:00
# include <linux/module.h>
2009-12-01 17:29:20 +03:00
# include <linux/spinlock.h>
# include <linux/interrupt.h>
# include <linux/delay.h>
# include <linux/errno.h>
# include <linux/err.h>
# include <linux/clk.h>
# include <linux/device.h>
# include <linux/platform_device.h>
# include <linux/gpio.h>
# include <linux/io.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-01 17:29:20 +03:00
# include <linux/spi/spi.h>
# include <linux/spi/spi_bitbang.h>
2012-08-24 17:18:36 +04:00
# include <linux/platform_data/spi-nuc900.h>
2009-12-01 17:29:20 +03:00
/* usi registers offset */
# define USI_CNT 0x00
# define USI_DIV 0x04
# define USI_SSR 0x08
# define USI_RX0 0x10
# define USI_TX0 0x10
/* usi register bit */
# define ENINT (0x01 << 17)
# define ENFLG (0x01 << 16)
2014-03-21 09:24:14 +04:00
# define SLEEP (0x0f << 12)
2009-12-01 17:29:20 +03:00
# define TXNUM (0x03 << 8)
2014-03-21 09:24:14 +04:00
# define TXBITLEN (0x1f << 3)
2009-12-01 17:29:20 +03:00
# define TXNEG (0x01 << 2)
# define RXNEG (0x01 << 1)
# define LSB (0x01 << 10)
# define SELECTLEV (0x01 << 2)
# define SELECTPOL (0x01 << 31)
# define SELECTSLAVE 0x01
# define GOBUSY 0x01
struct nuc900_spi {
struct spi_bitbang bitbang ;
struct completion done ;
void __iomem * regs ;
int irq ;
int len ;
int count ;
const unsigned char * tx ;
unsigned char * rx ;
struct clk * clk ;
struct spi_master * master ;
struct nuc900_spi_info * pdata ;
spinlock_t lock ;
} ;
static inline struct nuc900_spi * to_hw ( struct spi_device * sdev )
{
return spi_master_get_devdata ( sdev - > master ) ;
}
static void nuc900_slave_select ( struct spi_device * spi , unsigned int ssr )
{
struct nuc900_spi * hw = to_hw ( spi ) ;
unsigned int val ;
unsigned int cs = spi - > mode & SPI_CS_HIGH ? 1 : 0 ;
unsigned int cpol = spi - > mode & SPI_CPOL ? 1 : 0 ;
unsigned long flags ;
spin_lock_irqsave ( & hw - > lock , flags ) ;
val = __raw_readl ( hw - > regs + USI_SSR ) ;
if ( ! cs )
val & = ~ SELECTLEV ;
else
val | = SELECTLEV ;
if ( ! ssr )
val & = ~ SELECTSLAVE ;
else
val | = SELECTSLAVE ;
__raw_writel ( val , hw - > regs + USI_SSR ) ;
val = __raw_readl ( hw - > regs + USI_CNT ) ;
if ( ! cpol )
val & = ~ SELECTPOL ;
else
val | = SELECTPOL ;
__raw_writel ( val , hw - > regs + USI_CNT ) ;
spin_unlock_irqrestore ( & hw - > lock , flags ) ;
}
static void nuc900_spi_chipsel ( struct spi_device * spi , int value )
{
switch ( value ) {
case BITBANG_CS_INACTIVE :
nuc900_slave_select ( spi , 0 ) ;
break ;
case BITBANG_CS_ACTIVE :
nuc900_slave_select ( spi , 1 ) ;
break ;
}
}
2014-03-21 09:24:14 +04:00
static void nuc900_spi_setup_txnum ( struct nuc900_spi * hw , unsigned int txnum )
2009-12-01 17:29:20 +03:00
{
unsigned int val ;
unsigned long flags ;
spin_lock_irqsave ( & hw - > lock , flags ) ;
2014-03-21 09:24:14 +04:00
val = __raw_readl ( hw - > regs + USI_CNT ) & ~ TXNUM ;
2009-12-01 17:29:20 +03:00
2014-03-21 09:24:14 +04:00
if ( txnum )
2009-12-01 17:29:20 +03:00
val | = txnum < < 0x08 ;
__raw_writel ( val , hw - > regs + USI_CNT ) ;
spin_unlock_irqrestore ( & hw - > lock , flags ) ;
}
static void nuc900_spi_setup_txbitlen ( struct nuc900_spi * hw ,
unsigned int txbitlen )
{
unsigned int val ;
unsigned long flags ;
spin_lock_irqsave ( & hw - > lock , flags ) ;
2014-03-21 09:24:14 +04:00
val = __raw_readl ( hw - > regs + USI_CNT ) & ~ TXBITLEN ;
2009-12-01 17:29:20 +03:00
val | = ( txbitlen < < 0x03 ) ;
__raw_writel ( val , hw - > regs + USI_CNT ) ;
spin_unlock_irqrestore ( & hw - > lock , flags ) ;
}
static void nuc900_spi_gobusy ( struct nuc900_spi * hw )
{
unsigned int val ;
unsigned long flags ;
spin_lock_irqsave ( & hw - > lock , flags ) ;
val = __raw_readl ( hw - > regs + USI_CNT ) ;
val | = GOBUSY ;
__raw_writel ( val , hw - > regs + USI_CNT ) ;
spin_unlock_irqrestore ( & hw - > lock , flags ) ;
}
static inline unsigned int hw_txbyte ( struct nuc900_spi * hw , int count )
{
return hw - > tx ? hw - > tx [ count ] : 0 ;
}
static int nuc900_spi_txrx ( struct spi_device * spi , struct spi_transfer * t )
{
struct nuc900_spi * hw = to_hw ( spi ) ;
hw - > tx = t - > tx_buf ;
hw - > rx = t - > rx_buf ;
hw - > len = t - > len ;
hw - > count = 0 ;
__raw_writel ( hw_txbyte ( hw , 0x0 ) , hw - > regs + USI_TX0 ) ;
nuc900_spi_gobusy ( hw ) ;
wait_for_completion ( & hw - > done ) ;
return hw - > count ;
}
static irqreturn_t nuc900_spi_irq ( int irq , void * dev )
{
struct nuc900_spi * hw = dev ;
unsigned int status ;
unsigned int count = hw - > count ;
status = __raw_readl ( hw - > regs + USI_CNT ) ;
__raw_writel ( status , hw - > regs + USI_CNT ) ;
if ( status & ENFLG ) {
hw - > count + + ;
if ( hw - > rx )
hw - > rx [ count ] = __raw_readl ( hw - > regs + USI_RX0 ) ;
count + + ;
if ( count < hw - > len ) {
__raw_writel ( hw_txbyte ( hw , count ) , hw - > regs + USI_TX0 ) ;
nuc900_spi_gobusy ( hw ) ;
} else {
complete ( & hw - > done ) ;
}
return IRQ_HANDLED ;
}
complete ( & hw - > done ) ;
return IRQ_HANDLED ;
}
static void nuc900_tx_edge ( struct nuc900_spi * hw , unsigned int edge )
{
unsigned int val ;
unsigned long flags ;
spin_lock_irqsave ( & hw - > lock , flags ) ;
val = __raw_readl ( hw - > regs + USI_CNT ) ;
if ( edge )
val | = TXNEG ;
else
val & = ~ TXNEG ;
__raw_writel ( val , hw - > regs + USI_CNT ) ;
spin_unlock_irqrestore ( & hw - > lock , flags ) ;
}
static void nuc900_rx_edge ( struct nuc900_spi * hw , unsigned int edge )
{
unsigned int val ;
unsigned long flags ;
spin_lock_irqsave ( & hw - > lock , flags ) ;
val = __raw_readl ( hw - > regs + USI_CNT ) ;
if ( edge )
val | = RXNEG ;
else
val & = ~ RXNEG ;
__raw_writel ( val , hw - > regs + USI_CNT ) ;
spin_unlock_irqrestore ( & hw - > lock , flags ) ;
}
static void nuc900_send_first ( struct nuc900_spi * hw , unsigned int lsb )
{
unsigned int val ;
unsigned long flags ;
spin_lock_irqsave ( & hw - > lock , flags ) ;
val = __raw_readl ( hw - > regs + USI_CNT ) ;
if ( lsb )
val | = LSB ;
else
val & = ~ LSB ;
__raw_writel ( val , hw - > regs + USI_CNT ) ;
spin_unlock_irqrestore ( & hw - > lock , flags ) ;
}
static void nuc900_set_sleep ( struct nuc900_spi * hw , unsigned int sleep )
{
unsigned int val ;
unsigned long flags ;
spin_lock_irqsave ( & hw - > lock , flags ) ;
2014-03-21 09:24:14 +04:00
val = __raw_readl ( hw - > regs + USI_CNT ) & ~ SLEEP ;
2009-12-01 17:29:20 +03:00
if ( sleep )
val | = ( sleep < < 12 ) ;
2014-03-21 09:24:14 +04:00
2009-12-01 17:29:20 +03:00
__raw_writel ( val , hw - > regs + USI_CNT ) ;
spin_unlock_irqrestore ( & hw - > lock , flags ) ;
}
static void nuc900_enable_int ( struct nuc900_spi * hw )
{
unsigned int val ;
unsigned long flags ;
spin_lock_irqsave ( & hw - > lock , flags ) ;
val = __raw_readl ( hw - > regs + USI_CNT ) ;
val | = ENINT ;
__raw_writel ( val , hw - > regs + USI_CNT ) ;
spin_unlock_irqrestore ( & hw - > lock , flags ) ;
}
static void nuc900_set_divider ( struct nuc900_spi * hw )
{
__raw_writel ( hw - > pdata - > divider , hw - > regs + USI_DIV ) ;
}
static void nuc900_init_spi ( struct nuc900_spi * hw )
{
clk_enable ( hw - > clk ) ;
spin_lock_init ( & hw - > lock ) ;
nuc900_tx_edge ( hw , hw - > pdata - > txneg ) ;
nuc900_rx_edge ( hw , hw - > pdata - > rxneg ) ;
nuc900_send_first ( hw , hw - > pdata - > lsb ) ;
nuc900_set_sleep ( hw , hw - > pdata - > sleep ) ;
nuc900_spi_setup_txbitlen ( hw , hw - > pdata - > txbitlen ) ;
nuc900_spi_setup_txnum ( hw , hw - > pdata - > txnum ) ;
nuc900_set_divider ( hw ) ;
nuc900_enable_int ( hw ) ;
}
2012-12-07 20:57:14 +04:00
static int nuc900_spi_probe ( struct platform_device * pdev )
2009-12-01 17:29:20 +03:00
{
struct nuc900_spi * hw ;
struct spi_master * master ;
2014-02-08 07:15:57 +04:00
struct resource * res ;
2009-12-01 17:29:20 +03:00
int err = 0 ;
master = spi_alloc_master ( & pdev - > dev , sizeof ( struct nuc900_spi ) ) ;
if ( master = = NULL ) {
dev_err ( & pdev - > dev , " No memory for spi_master \n " ) ;
2013-12-09 14:18:18 +04:00
return - ENOMEM ;
2009-12-01 17:29:20 +03:00
}
hw = spi_master_get_devdata ( master ) ;
2013-09-10 11:43:41 +04:00
hw - > master = master ;
2013-07-30 11:58:59 +04:00
hw - > pdata = dev_get_platdata ( & pdev - > dev ) ;
2009-12-01 17:29:20 +03:00
if ( hw - > pdata = = NULL ) {
dev_err ( & pdev - > dev , " No platform data supplied \n " ) ;
err = - ENOENT ;
goto err_pdata ;
}
platform_set_drvdata ( pdev , hw ) ;
init_completion ( & hw - > done ) ;
2013-08-15 10:11:21 +04:00
master - > mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH ;
2014-01-20 19:22:07 +04:00
if ( hw - > pdata - > lsb )
master - > mode_bits | = SPI_LSB_FIRST ;
2009-12-01 17:29:20 +03:00
master - > num_chipselect = hw - > pdata - > num_cs ;
master - > bus_num = hw - > pdata - > bus_num ;
hw - > bitbang . master = hw - > master ;
hw - > bitbang . chipselect = nuc900_spi_chipsel ;
hw - > bitbang . txrx_bufs = nuc900_spi_txrx ;
2014-02-08 07:15:57 +04:00
res = platform_get_resource ( pdev , IORESOURCE_MEM , 0 ) ;
hw - > regs = devm_ioremap_resource ( & pdev - > dev , res ) ;
2013-12-09 14:18:18 +04:00
if ( IS_ERR ( hw - > regs ) ) {
err = PTR_ERR ( hw - > regs ) ;
2009-12-01 17:29:20 +03:00
goto err_pdata ;
}
hw - > irq = platform_get_irq ( pdev , 0 ) ;
if ( hw - > irq < 0 ) {
dev_err ( & pdev - > dev , " No IRQ specified \n " ) ;
err = - ENOENT ;
2013-12-09 14:18:18 +04:00
goto err_pdata ;
2009-12-01 17:29:20 +03:00
}
2013-12-09 14:18:18 +04:00
err = devm_request_irq ( & pdev - > dev , hw - > irq , nuc900_spi_irq , 0 ,
pdev - > name , hw ) ;
2009-12-01 17:29:20 +03:00
if ( err ) {
dev_err ( & pdev - > dev , " Cannot claim IRQ \n " ) ;
2013-12-09 14:18:18 +04:00
goto err_pdata ;
2009-12-01 17:29:20 +03:00
}
2013-12-09 14:18:18 +04:00
hw - > clk = devm_clk_get ( & pdev - > dev , " spi " ) ;
2009-12-01 17:29:20 +03:00
if ( IS_ERR ( hw - > clk ) ) {
dev_err ( & pdev - > dev , " No clock for device \n " ) ;
err = PTR_ERR ( hw - > clk ) ;
2013-12-09 14:18:18 +04:00
goto err_pdata ;
2009-12-01 17:29:20 +03:00
}
2011-11-25 03:23:28 +04:00
mfp_set_groupg ( & pdev - > dev , NULL ) ;
2009-12-01 17:29:20 +03:00
nuc900_init_spi ( hw ) ;
err = spi_bitbang_start ( & hw - > bitbang ) ;
if ( err ) {
dev_err ( & pdev - > dev , " Failed to register SPI master \n " ) ;
goto err_register ;
}
return 0 ;
err_register :
clk_disable ( hw - > clk ) ;
err_pdata :
2010-11-15 06:04:47 +03:00
spi_master_put ( hw - > master ) ;
2009-12-01 17:29:20 +03:00
return err ;
}
2012-12-07 20:57:14 +04:00
static int nuc900_spi_remove ( struct platform_device * dev )
2009-12-01 17:29:20 +03:00
{
struct nuc900_spi * hw = platform_get_drvdata ( dev ) ;
2011-05-15 03:33:28 +04:00
spi_bitbang_stop ( & hw - > bitbang ) ;
2009-12-01 17:29:20 +03:00
clk_disable ( hw - > clk ) ;
spi_master_put ( hw - > master ) ;
return 0 ;
}
static struct platform_driver nuc900_spi_driver = {
. probe = nuc900_spi_probe ,
2012-12-07 20:57:14 +04:00
. remove = nuc900_spi_remove ,
2009-12-01 17:29:20 +03:00
. driver = {
. name = " nuc900-spi " ,
} ,
} ;
2011-10-05 21:29:49 +04:00
module_platform_driver ( nuc900_spi_driver ) ;
2009-12-01 17:29:20 +03:00
MODULE_AUTHOR ( " Wan ZongShun <mcuos.com@gmail.com> " ) ;
MODULE_DESCRIPTION ( " nuc900 spi driver! " ) ;
MODULE_LICENSE ( " GPL " ) ;
MODULE_ALIAS ( " platform:nuc900-spi " ) ;