2008-04-11 21:03:40 +04:00
/*
* Freescale LBC and UPM routines .
*
2010-10-18 15:22:31 +08:00
* Copyright © 2007 - 2008 MontaVista Software , Inc .
* Copyright © 2010 Freescale Semiconductor
2008-04-11 21:03:40 +04:00
*
* Author : Anton Vorontsov < avorontsov @ ru . mvista . com >
2010-10-18 15:22:31 +08:00
* Author : Jack Lan < Jack . Lan @ freescale . com >
* Author : Roy Zang < tie - fei . zang @ freescale . com >
2008-04-11 21:03:40 +04:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*/
2008-10-09 04:32:59 +04:00
# include <linux/init.h>
2011-07-22 18:24:23 -04:00
# include <linux/export.h>
2008-04-11 21:03:40 +04:00
# include <linux/kernel.h>
2008-10-09 04:32:59 +04:00
# include <linux/compiler.h>
# include <linux/spinlock.h>
# include <linux/types.h>
# include <linux/io.h>
2008-04-11 21:03:40 +04:00
# include <linux/of.h>
2010-10-18 15:22:31 +08:00
# include <linux/slab.h>
2011-05-27 14:25:11 -04:00
# include <linux/sched.h>
2010-10-18 15:22:31 +08:00
# include <linux/platform_device.h>
# include <linux/interrupt.h>
# include <linux/mod_devicetable.h>
2016-02-09 15:09:08 +05:30
# include <linux/syscore_ops.h>
2008-10-09 04:32:59 +04:00
# include <asm/prom.h>
2008-04-11 21:03:40 +04:00
# include <asm/fsl_lbc.h>
2016-12-04 13:47:28 +01:00
static DEFINE_SPINLOCK ( fsl_lbc_lock ) ;
2010-10-18 15:22:31 +08:00
struct fsl_lbc_ctrl * fsl_lbc_ctrl_dev ;
EXPORT_SYMBOL ( fsl_lbc_ctrl_dev ) ;
2008-04-11 21:03:40 +04:00
2010-10-18 15:22:32 +08:00
/**
* fsl_lbc_addr - convert the base address
* @ addr_base : base address of the memory bank
*
* This function converts a base address of lbc into the right format for the
* BR register . If the SOC has eLBC then it returns 32 bit physical address
* else it convers a 34 bit local bus physical address to correct format of
* 32 bit address for BR register ( Example : MPC8641 ) .
*/
u32 fsl_lbc_addr ( phys_addr_t addr_base )
{
struct device_node * np = fsl_lbc_ctrl_dev - > dev - > of_node ;
u32 addr = addr_base & 0xffff8000 ;
if ( of_device_is_compatible ( np , " fsl,elbc " ) )
return addr ;
return addr | ( ( addr_base & 0x300000000ull ) > > 19 ) ;
}
EXPORT_SYMBOL ( fsl_lbc_addr ) ;
2008-04-11 21:03:40 +04:00
/**
* fsl_lbc_find - find Localbus bank
* @ addr_base : base address of the memory bank
*
* This function walks LBC banks comparing " Base address " field of the BR
* registers with the supplied addr_base argument . When bases match this
* function returns bank number ( starting with 0 ) , otherwise it returns
* appropriate errno value .
*/
int fsl_lbc_find ( phys_addr_t addr_base )
{
int i ;
2010-10-18 15:22:31 +08:00
struct fsl_lbc_regs __iomem * lbc ;
2008-04-11 21:03:40 +04:00
2010-10-18 15:22:31 +08:00
if ( ! fsl_lbc_ctrl_dev | | ! fsl_lbc_ctrl_dev - > regs )
2008-04-11 21:03:40 +04:00
return - ENODEV ;
2010-10-18 15:22:31 +08:00
lbc = fsl_lbc_ctrl_dev - > regs ;
for ( i = 0 ; i < ARRAY_SIZE ( lbc - > bank ) ; i + + ) {
2012-11-30 17:35:00 -06:00
u32 br = in_be32 ( & lbc - > bank [ i ] . br ) ;
u32 or = in_be32 ( & lbc - > bank [ i ] . or ) ;
2008-04-11 21:03:40 +04:00
2010-10-18 15:22:32 +08:00
if ( br & BR_V & & ( br & or & BR_BA ) = = fsl_lbc_addr ( addr_base ) )
2008-04-11 21:03:40 +04:00
return i ;
}
return - ENOENT ;
}
EXPORT_SYMBOL ( fsl_lbc_find ) ;
/**
* fsl_upm_find - find pre - programmed UPM via base address
* @ addr_base : base address of the memory bank controlled by the UPM
* @ upm : pointer to the allocated fsl_upm structure
*
* This function fills fsl_upm structure so you can use it with the rest of
* UPM API . On success this function returns 0 , otherwise it returns
* appropriate errno value .
*/
int fsl_upm_find ( phys_addr_t addr_base , struct fsl_upm * upm )
{
int bank ;
2012-11-30 17:35:00 -06:00
u32 br ;
2010-10-18 15:22:31 +08:00
struct fsl_lbc_regs __iomem * lbc ;
2008-04-11 21:03:40 +04:00
bank = fsl_lbc_find ( addr_base ) ;
if ( bank < 0 )
return bank ;
2010-10-18 15:22:31 +08:00
if ( ! fsl_lbc_ctrl_dev | | ! fsl_lbc_ctrl_dev - > regs )
return - ENODEV ;
lbc = fsl_lbc_ctrl_dev - > regs ;
br = in_be32 ( & lbc - > bank [ bank ] . br ) ;
2008-04-11 21:03:40 +04:00
switch ( br & BR_MSEL ) {
case BR_MS_UPMA :
2010-10-18 15:22:31 +08:00
upm - > mxmr = & lbc - > mamr ;
2008-04-11 21:03:40 +04:00
break ;
case BR_MS_UPMB :
2010-10-18 15:22:31 +08:00
upm - > mxmr = & lbc - > mbmr ;
2008-04-11 21:03:40 +04:00
break ;
case BR_MS_UPMC :
2010-10-18 15:22:31 +08:00
upm - > mxmr = & lbc - > mcmr ;
2008-04-11 21:03:40 +04:00
break ;
default :
return - EINVAL ;
}
switch ( br & BR_PS ) {
case BR_PS_8 :
upm - > width = 8 ;
break ;
case BR_PS_16 :
upm - > width = 16 ;
break ;
case BR_PS_32 :
upm - > width = 32 ;
break ;
default :
return - EINVAL ;
}
return 0 ;
}
EXPORT_SYMBOL ( fsl_upm_find ) ;
2008-10-09 04:32:59 +04:00
/**
* fsl_upm_run_pattern - actually run an UPM pattern
* @ upm : pointer to the fsl_upm structure obtained via fsl_upm_find
* @ io_base : remapped pointer to where memory access should happen
* @ mar : MAR register content during pattern execution
*
* This function triggers dummy write to the memory specified by the io_base ,
* thus UPM pattern actually executed . Note that mar usage depends on the
* pre - programmed AMX bits in the UPM RAM .
*/
int fsl_upm_run_pattern ( struct fsl_upm * upm , void __iomem * io_base , u32 mar )
{
int ret = 0 ;
unsigned long flags ;
2010-10-18 15:22:31 +08:00
if ( ! fsl_lbc_ctrl_dev | | ! fsl_lbc_ctrl_dev - > regs )
return - ENODEV ;
2008-10-09 04:32:59 +04:00
spin_lock_irqsave ( & fsl_lbc_lock , flags ) ;
2010-10-18 15:22:31 +08:00
out_be32 ( & fsl_lbc_ctrl_dev - > regs - > mar , mar ) ;
2008-10-09 04:32:59 +04:00
switch ( upm - > width ) {
case 8 :
out_8 ( io_base , 0x0 ) ;
break ;
case 16 :
out_be16 ( io_base , 0x0 ) ;
break ;
case 32 :
out_be32 ( io_base , 0x0 ) ;
break ;
default :
ret = - EINVAL ;
break ;
}
spin_unlock_irqrestore ( & fsl_lbc_lock , flags ) ;
return ret ;
}
EXPORT_SYMBOL ( fsl_upm_run_pattern ) ;
2010-10-18 15:22:31 +08:00
2012-12-21 14:04:10 -08:00
static int fsl_lbc_ctrl_init ( struct fsl_lbc_ctrl * ctrl ,
struct device_node * node )
2010-10-18 15:22:31 +08:00
{
struct fsl_lbc_regs __iomem * lbc = ctrl - > regs ;
/* clear event registers */
setbits32 ( & lbc - > ltesr , LTESR_CLEAR ) ;
out_be32 ( & lbc - > lteatr , 0 ) ;
out_be32 ( & lbc - > ltear , 0 ) ;
out_be32 ( & lbc - > lteccr , LTECCR_CLEAR ) ;
out_be32 ( & lbc - > ltedr , LTEDR_ENABLE ) ;
2011-05-19 18:48:01 +08:00
/* Set the monitor timeout value to the maximum for erratum A001 */
if ( of_device_is_compatible ( node , " fsl,elbc " ) )
clrsetbits_be32 ( & lbc - > lbcr , LBCR_BMT , LBCR_BMTPS ) ;
2010-10-18 15:22:31 +08:00
return 0 ;
}
/*
* NOTE : This interrupt is used to report localbus events of various kinds ,
* such as transaction errors on the chipselects .
*/
static irqreturn_t fsl_lbc_ctrl_irq ( int irqno , void * data )
{
struct fsl_lbc_ctrl * ctrl = data ;
struct fsl_lbc_regs __iomem * lbc = ctrl - > regs ;
u32 status ;
2014-01-07 14:27:41 +08:00
unsigned long flags ;
2010-10-18 15:22:31 +08:00
2014-01-07 14:27:41 +08:00
spin_lock_irqsave ( & fsl_lbc_lock , flags ) ;
2010-10-18 15:22:31 +08:00
status = in_be32 ( & lbc - > ltesr ) ;
2014-01-07 14:27:41 +08:00
if ( ! status ) {
spin_unlock_irqrestore ( & fsl_lbc_lock , flags ) ;
2010-10-18 15:22:31 +08:00
return IRQ_NONE ;
2014-01-07 14:27:41 +08:00
}
2010-10-18 15:22:31 +08:00
out_be32 ( & lbc - > ltesr , LTESR_CLEAR ) ;
out_be32 ( & lbc - > lteatr , 0 ) ;
out_be32 ( & lbc - > ltear , 0 ) ;
ctrl - > irq_status = status ;
if ( status & LTESR_BM )
dev_err ( ctrl - > dev , " Local bus monitor time-out: "
" LTESR 0x%08X \n " , status ) ;
if ( status & LTESR_WP )
dev_err ( ctrl - > dev , " Write protect error: "
" LTESR 0x%08X \n " , status ) ;
if ( status & LTESR_ATMW )
dev_err ( ctrl - > dev , " Atomic write error: "
" LTESR 0x%08X \n " , status ) ;
if ( status & LTESR_ATMR )
dev_err ( ctrl - > dev , " Atomic read error: "
" LTESR 0x%08X \n " , status ) ;
if ( status & LTESR_CS )
dev_err ( ctrl - > dev , " Chip select error: "
" LTESR 0x%08X \n " , status ) ;
if ( status & LTESR_FCT ) {
dev_err ( ctrl - > dev , " FCM command time-out: "
" LTESR 0x%08X \n " , status ) ;
smp_wmb ( ) ;
wake_up ( & ctrl - > irq_wait ) ;
}
if ( status & LTESR_PAR ) {
dev_err ( ctrl - > dev , " Parity or Uncorrectable ECC error: "
" LTESR 0x%08X \n " , status ) ;
smp_wmb ( ) ;
wake_up ( & ctrl - > irq_wait ) ;
}
if ( status & LTESR_CC ) {
smp_wmb ( ) ;
wake_up ( & ctrl - > irq_wait ) ;
}
if ( status & ~ LTESR_MASK )
dev_err ( ctrl - > dev , " Unknown error: "
" LTESR 0x%08X \n " , status ) ;
2014-01-07 14:27:41 +08:00
spin_unlock_irqrestore ( & fsl_lbc_lock , flags ) ;
2010-10-18 15:22:31 +08:00
return IRQ_HANDLED ;
}
/*
* fsl_lbc_ctrl_probe
*
* called by device layer when it finds a device matching
* one our driver can handled . This code allocates all of
* the resources needed for the controller only . The
* resources for the NAND banks themselves are allocated
* in the chip probe function .
*/
2012-12-21 14:04:10 -08:00
static int fsl_lbc_ctrl_probe ( struct platform_device * dev )
2010-10-18 15:22:31 +08:00
{
int ret ;
if ( ! dev - > dev . of_node ) {
dev_err ( & dev - > dev , " Device OF-Node is NULL " ) ;
return - EFAULT ;
}
fsl_lbc_ctrl_dev = kzalloc ( sizeof ( * fsl_lbc_ctrl_dev ) , GFP_KERNEL ) ;
if ( ! fsl_lbc_ctrl_dev )
return - ENOMEM ;
dev_set_drvdata ( & dev - > dev , fsl_lbc_ctrl_dev ) ;
spin_lock_init ( & fsl_lbc_ctrl_dev - > lock ) ;
init_waitqueue_head ( & fsl_lbc_ctrl_dev - > irq_wait ) ;
fsl_lbc_ctrl_dev - > regs = of_iomap ( dev - > dev . of_node , 0 ) ;
if ( ! fsl_lbc_ctrl_dev - > regs ) {
dev_err ( & dev - > dev , " failed to get memory region \n " ) ;
ret = - ENODEV ;
goto err ;
}
2014-01-07 14:27:41 +08:00
fsl_lbc_ctrl_dev - > irq [ 0 ] = irq_of_parse_and_map ( dev - > dev . of_node , 0 ) ;
if ( ! fsl_lbc_ctrl_dev - > irq [ 0 ] ) {
2010-10-18 15:22:31 +08:00
dev_err ( & dev - > dev , " failed to get irq resource \n " ) ;
ret = - ENODEV ;
goto err ;
}
fsl_lbc_ctrl_dev - > dev = & dev - > dev ;
2011-05-19 18:48:01 +08:00
ret = fsl_lbc_ctrl_init ( fsl_lbc_ctrl_dev , dev - > dev . of_node ) ;
2010-10-18 15:22:31 +08:00
if ( ret < 0 )
goto err ;
2014-01-07 14:27:41 +08:00
ret = request_irq ( fsl_lbc_ctrl_dev - > irq [ 0 ] , fsl_lbc_ctrl_irq , 0 ,
2010-10-18 15:22:31 +08:00
" fsl-lbc " , fsl_lbc_ctrl_dev ) ;
if ( ret ! = 0 ) {
dev_err ( & dev - > dev , " failed to install irq (%d) \n " ,
2014-01-07 14:27:41 +08:00
fsl_lbc_ctrl_dev - > irq [ 0 ] ) ;
ret = fsl_lbc_ctrl_dev - > irq [ 0 ] ;
2010-10-18 15:22:31 +08:00
goto err ;
}
2014-01-07 14:27:41 +08:00
fsl_lbc_ctrl_dev - > irq [ 1 ] = irq_of_parse_and_map ( dev - > dev . of_node , 1 ) ;
if ( fsl_lbc_ctrl_dev - > irq [ 1 ] ) {
ret = request_irq ( fsl_lbc_ctrl_dev - > irq [ 1 ] , fsl_lbc_ctrl_irq ,
IRQF_SHARED , " fsl-lbc-err " , fsl_lbc_ctrl_dev ) ;
if ( ret ) {
dev_err ( & dev - > dev , " failed to install irq (%d) \n " ,
fsl_lbc_ctrl_dev - > irq [ 1 ] ) ;
ret = fsl_lbc_ctrl_dev - > irq [ 1 ] ;
goto err1 ;
}
}
2011-06-03 10:45:11 +08:00
/* Enable interrupts for any detected events */
out_be32 ( & fsl_lbc_ctrl_dev - > regs - > lteir , LTEIR_ENABLE ) ;
2010-10-18 15:22:31 +08:00
return 0 ;
2014-01-07 14:27:41 +08:00
err1 :
free_irq ( fsl_lbc_ctrl_dev - > irq [ 0 ] , fsl_lbc_ctrl_dev ) ;
2010-10-18 15:22:31 +08:00
err :
iounmap ( fsl_lbc_ctrl_dev - > regs ) ;
kfree ( fsl_lbc_ctrl_dev ) ;
2011-10-27 18:18:37 +04:00
fsl_lbc_ctrl_dev = NULL ;
2010-10-18 15:22:31 +08:00
return ret ;
}
2011-11-21 14:29:11 +08:00
# ifdef CONFIG_SUSPEND
/* save lbc registers */
2016-02-09 15:09:08 +05:30
static int fsl_lbc_syscore_suspend ( void )
2011-11-21 14:29:11 +08:00
{
2016-02-09 15:09:08 +05:30
struct fsl_lbc_ctrl * ctrl ;
struct fsl_lbc_regs __iomem * lbc ;
ctrl = fsl_lbc_ctrl_dev ;
if ( ! ctrl )
goto out ;
lbc = ctrl - > regs ;
if ( ! lbc )
goto out ;
2011-11-21 14:29:11 +08:00
ctrl - > saved_regs = kmalloc ( sizeof ( struct fsl_lbc_regs ) , GFP_KERNEL ) ;
if ( ! ctrl - > saved_regs )
return - ENOMEM ;
_memcpy_fromio ( ctrl - > saved_regs , lbc , sizeof ( struct fsl_lbc_regs ) ) ;
2016-02-09 15:09:08 +05:30
out :
2011-11-21 14:29:11 +08:00
return 0 ;
}
/* restore lbc registers */
2016-02-09 15:09:08 +05:30
static void fsl_lbc_syscore_resume ( void )
2011-11-21 14:29:11 +08:00
{
2016-02-09 15:09:08 +05:30
struct fsl_lbc_ctrl * ctrl ;
struct fsl_lbc_regs __iomem * lbc ;
ctrl = fsl_lbc_ctrl_dev ;
if ( ! ctrl )
goto out ;
lbc = ctrl - > regs ;
if ( ! lbc )
goto out ;
2011-11-21 14:29:11 +08:00
if ( ctrl - > saved_regs ) {
_memcpy_toio ( lbc , ctrl - > saved_regs ,
sizeof ( struct fsl_lbc_regs ) ) ;
kfree ( ctrl - > saved_regs ) ;
ctrl - > saved_regs = NULL ;
}
2016-02-09 15:09:08 +05:30
out :
return ;
2011-11-21 14:29:11 +08:00
}
# endif /* CONFIG_SUSPEND */
2010-10-18 15:22:31 +08:00
static const struct of_device_id fsl_lbc_match [ ] = {
{ . compatible = " fsl,elbc " , } ,
{ . compatible = " fsl,pq3-localbus " , } ,
{ . compatible = " fsl,pq2-localbus " , } ,
{ . compatible = " fsl,pq2pro-localbus " , } ,
{ } ,
} ;
2016-02-09 15:09:08 +05:30
# ifdef CONFIG_SUSPEND
static struct syscore_ops lbc_syscore_pm_ops = {
. suspend = fsl_lbc_syscore_suspend ,
. resume = fsl_lbc_syscore_resume ,
} ;
# endif
2010-10-18 15:22:31 +08:00
static struct platform_driver fsl_lbc_ctrl_driver = {
. driver = {
. name = " fsl-lbc " ,
. of_match_table = fsl_lbc_match ,
} ,
. probe = fsl_lbc_ctrl_probe ,
} ;
static int __init fsl_lbc_init ( void )
{
2016-02-09 15:09:08 +05:30
# ifdef CONFIG_SUSPEND
register_syscore_ops ( & lbc_syscore_pm_ops ) ;
# endif
2010-10-18 15:22:31 +08:00
return platform_driver_register ( & fsl_lbc_ctrl_driver ) ;
}
2015-05-01 20:08:21 -04:00
subsys_initcall ( fsl_lbc_init ) ;