2011-04-19 10:33:45 +04:00
/*
* Copyright ( C ) 2010 - 2011 Canonical Ltd < jeremy . kerr @ canonical . com >
* Copyright ( C ) 2011 - 2012 Mike Turquette , Linaro Ltd < mturquette @ linaro . org >
*
* 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 .
*
* Gated clock implementation
*/
# include <linux/clk-provider.h>
# include <linux/module.h>
# include <linux/slab.h>
# include <linux/io.h>
# include <linux/err.h>
# include <linux/string.h>
2013-03-25 16:20:35 +04:00
# include "clk.h"
2011-04-19 10:33:45 +04:00
/**
* DOC : basic gatable clock which can gate and ungate it ' s ouput
*
* Traits of this clock :
* prepare - clk_ ( un ) prepare only ensures parent is ( un ) prepared
* enable - clk_enable and clk_disable are functional & control gating
* rate - inherits rate from parent . No clk_set_rate support
* parent - fixed parent . No clk_set_parent support
*/
2014-04-18 11:55:16 +04:00
struct clk_gate2 {
struct clk_hw hw ;
void __iomem * reg ;
u8 bit_idx ;
2016-03-10 05:16:47 +03:00
u8 cgr_val ;
2014-04-18 11:55:16 +04:00
u8 flags ;
spinlock_t * lock ;
2014-04-19 06:58:22 +04:00
unsigned int * share_count ;
2014-04-18 11:55:16 +04:00
} ;
# define to_clk_gate2(_hw) container_of(_hw, struct clk_gate2, hw)
2011-04-19 10:33:45 +04:00
static int clk_gate2_enable ( struct clk_hw * hw )
{
2014-04-18 11:55:16 +04:00
struct clk_gate2 * gate = to_clk_gate2 ( hw ) ;
2011-04-19 10:33:45 +04:00
u32 reg ;
unsigned long flags = 0 ;
2014-04-18 12:07:44 +04:00
spin_lock_irqsave ( gate - > lock , flags ) ;
2011-04-19 10:33:45 +04:00
2014-04-19 06:58:22 +04:00
if ( gate - > share_count & & ( * gate - > share_count ) + + > 0 )
goto out ;
2011-04-19 10:33:45 +04:00
reg = readl ( gate - > reg ) ;
2016-03-10 05:16:47 +03:00
reg & = ~ ( 3 < < gate - > bit_idx ) ;
reg | = gate - > cgr_val < < gate - > bit_idx ;
2011-04-19 10:33:45 +04:00
writel ( reg , gate - > reg ) ;
2014-04-19 06:58:22 +04:00
out :
2014-04-18 12:07:44 +04:00
spin_unlock_irqrestore ( gate - > lock , flags ) ;
2011-04-19 10:33:45 +04:00
return 0 ;
}
static void clk_gate2_disable ( struct clk_hw * hw )
{
2014-04-18 11:55:16 +04:00
struct clk_gate2 * gate = to_clk_gate2 ( hw ) ;
2011-04-19 10:33:45 +04:00
u32 reg ;
unsigned long flags = 0 ;
2014-04-18 12:07:44 +04:00
spin_lock_irqsave ( gate - > lock , flags ) ;
2011-04-19 10:33:45 +04:00
2014-07-07 06:53:51 +04:00
if ( gate - > share_count ) {
if ( WARN_ON ( * gate - > share_count = = 0 ) )
goto out ;
else if ( - - ( * gate - > share_count ) > 0 )
goto out ;
}
2014-04-19 06:58:22 +04:00
2011-04-19 10:33:45 +04:00
reg = readl ( gate - > reg ) ;
reg & = ~ ( 3 < < gate - > bit_idx ) ;
writel ( reg , gate - > reg ) ;
2014-04-19 06:58:22 +04:00
out :
2014-04-18 12:07:44 +04:00
spin_unlock_irqrestore ( gate - > lock , flags ) ;
2011-04-19 10:33:45 +04:00
}
2014-07-07 06:53:51 +04:00
static int clk_gate2_reg_is_enabled ( void __iomem * reg , u8 bit_idx )
2011-04-19 10:33:45 +04:00
{
2014-07-07 06:53:51 +04:00
u32 val = readl ( reg ) ;
2011-04-19 10:33:45 +04:00
2014-07-07 06:53:51 +04:00
if ( ( ( val > > bit_idx ) & 1 ) = = 1 )
2011-04-19 10:33:45 +04:00
return 1 ;
return 0 ;
}
2014-07-07 06:53:51 +04:00
static int clk_gate2_is_enabled ( struct clk_hw * hw )
{
struct clk_gate2 * gate = to_clk_gate2 ( hw ) ;
ARM: imx: correct the hardware clock gate setting for shared nodes
For those clk gates which hold share count, since its is_enabled
callback is only checking the share count rather than reading
the hardware register setting, in the late phase of kernel bootup,
the clk_disable_unused action will NOT handle the scenario of
share_count is 0 but the hardware setting is enabled, actually,
U-Boot normally enables all clk gates, then those shared clk gates
will be always enabled until they are used by some modules.
So the problem would be: when kernel boot up, the usecount cat
from clk tree is 0, but the clk gates actually is enabled in
hardware register, it will confuse user and bring unnecessary power
consumption.
This patch adds .disable_unused callback and using hardware register
check for .is_enabled callback of shared nodes to handle such scenario
in late phase of kernel boot up, then the hardware status will match the
clk tree info.
Signed-off-by: Anson Huang <b20788@freescale.com>
Signed-off-by: Shawn Guo <shawn.guo@linaro.org>
2014-12-10 12:51:42 +03:00
return clk_gate2_reg_is_enabled ( gate - > reg , gate - > bit_idx ) ;
}
static void clk_gate2_disable_unused ( struct clk_hw * hw )
{
struct clk_gate2 * gate = to_clk_gate2 ( hw ) ;
unsigned long flags = 0 ;
u32 reg ;
spin_lock_irqsave ( gate - > lock , flags ) ;
if ( ! gate - > share_count | | * gate - > share_count = = 0 ) {
reg = readl ( gate - > reg ) ;
reg & = ~ ( 3 < < gate - > bit_idx ) ;
writel ( reg , gate - > reg ) ;
}
spin_unlock_irqrestore ( gate - > lock , flags ) ;
2014-07-07 06:53:51 +04:00
}
2017-08-22 16:18:29 +03:00
static const struct clk_ops clk_gate2_ops = {
2011-04-19 10:33:45 +04:00
. enable = clk_gate2_enable ,
. disable = clk_gate2_disable ,
ARM: imx: correct the hardware clock gate setting for shared nodes
For those clk gates which hold share count, since its is_enabled
callback is only checking the share count rather than reading
the hardware register setting, in the late phase of kernel bootup,
the clk_disable_unused action will NOT handle the scenario of
share_count is 0 but the hardware setting is enabled, actually,
U-Boot normally enables all clk gates, then those shared clk gates
will be always enabled until they are used by some modules.
So the problem would be: when kernel boot up, the usecount cat
from clk tree is 0, but the clk gates actually is enabled in
hardware register, it will confuse user and bring unnecessary power
consumption.
This patch adds .disable_unused callback and using hardware register
check for .is_enabled callback of shared nodes to handle such scenario
in late phase of kernel boot up, then the hardware status will match the
clk tree info.
Signed-off-by: Anson Huang <b20788@freescale.com>
Signed-off-by: Shawn Guo <shawn.guo@linaro.org>
2014-12-10 12:51:42 +03:00
. disable_unused = clk_gate2_disable_unused ,
2011-04-19 10:33:45 +04:00
. is_enabled = clk_gate2_is_enabled ,
} ;
struct clk * clk_register_gate2 ( struct device * dev , const char * name ,
const char * parent_name , unsigned long flags ,
2016-03-10 05:16:47 +03:00
void __iomem * reg , u8 bit_idx , u8 cgr_val ,
2014-04-19 06:58:22 +04:00
u8 clk_gate2_flags , spinlock_t * lock ,
unsigned int * share_count )
2011-04-19 10:33:45 +04:00
{
2014-04-18 11:55:16 +04:00
struct clk_gate2 * gate ;
2011-04-19 10:33:45 +04:00
struct clk * clk ;
struct clk_init_data init ;
2014-04-18 11:55:16 +04:00
gate = kzalloc ( sizeof ( struct clk_gate2 ) , GFP_KERNEL ) ;
2011-04-19 10:33:45 +04:00
if ( ! gate )
return ERR_PTR ( - ENOMEM ) ;
2014-04-18 11:55:16 +04:00
/* struct clk_gate2 assignments */
2011-04-19 10:33:45 +04:00
gate - > reg = reg ;
gate - > bit_idx = bit_idx ;
2016-03-10 05:16:47 +03:00
gate - > cgr_val = cgr_val ;
2011-04-19 10:33:45 +04:00
gate - > flags = clk_gate2_flags ;
gate - > lock = lock ;
2014-04-19 06:58:22 +04:00
gate - > share_count = share_count ;
2011-04-19 10:33:45 +04:00
init . name = name ;
init . ops = & clk_gate2_ops ;
init . flags = flags ;
init . parent_names = parent_name ? & parent_name : NULL ;
init . num_parents = parent_name ? 1 : 0 ;
gate - > hw . init = & init ;
clk = clk_register ( dev , & gate - > hw ) ;
if ( IS_ERR ( clk ) )
2012-10-25 19:02:18 +04:00
kfree ( gate ) ;
2011-04-19 10:33:45 +04:00
return clk ;
}