6161#define GET_REG_SAMPLL_CLK1 (val ) ((val >> 22) & 0xfff)
6262#define GET_REG_SAMPLL_CLK2 (val ) ((val >> 12) & 0xfff)
6363
64+ #define CPG_WEN_BIT BIT(16)
65+
6466#define MAX_VCLK_FREQ (148500000)
6567
6668/**
6769 * struct clk_hw_data - clock hardware data
6870 * @hw: clock hw
6971 * @conf: clock configuration (register offset, shift, width)
72+ * @sconf: clock status configuration (register offset, shift, width)
7073 * @priv: CPG private data structure
7174 */
7275struct clk_hw_data {
7376 struct clk_hw hw ;
7477 u32 conf ;
78+ u32 sconf ;
7579 struct rzg2l_cpg_priv * priv ;
7680};
7781
7882#define to_clk_hw_data (_hw ) container_of(_hw, struct clk_hw_data, hw)
7983
8084/**
81- * struct sd_hw_data - SD clock hardware data
85+ * struct sd_mux_hw_data - SD MUX clock hardware data
8286 * @hw_data: clock hw data
87+ * @mtable: clock mux table
8388 */
84- struct sd_hw_data {
89+ struct sd_mux_hw_data {
8590 struct clk_hw_data hw_data ;
91+ const u32 * mtable ;
8692};
8793
88- #define to_sd_hw_data (_hw ) container_of(_hw, struct sd_hw_data , hw_data)
94+ #define to_sd_mux_hw_data (_hw ) container_of(_hw, struct sd_mux_hw_data , hw_data)
8995
9096struct rzg2l_pll5_param {
9197 u32 pl5_fracin ;
@@ -142,6 +148,76 @@ static void rzg2l_cpg_del_clk_provider(void *data)
142148 of_clk_del_provider (data );
143149}
144150
151+ /* Must be called in atomic context. */
152+ static int rzg2l_cpg_wait_clk_update_done (void __iomem * base , u32 conf )
153+ {
154+ u32 bitmask = GENMASK (GET_WIDTH (conf ) - 1 , 0 ) << GET_SHIFT (conf );
155+ u32 off = GET_REG_OFFSET (conf );
156+ u32 val ;
157+
158+ return readl_poll_timeout_atomic (base + off , val , !(val & bitmask ), 10 , 200 );
159+ }
160+
161+ int rzg2l_cpg_sd_clk_mux_notifier (struct notifier_block * nb , unsigned long event ,
162+ void * data )
163+ {
164+ struct clk_notifier_data * cnd = data ;
165+ struct clk_hw * hw = __clk_get_hw (cnd -> clk );
166+ struct clk_hw_data * clk_hw_data = to_clk_hw_data (hw );
167+ struct rzg2l_cpg_priv * priv = clk_hw_data -> priv ;
168+ u32 off = GET_REG_OFFSET (clk_hw_data -> conf );
169+ u32 shift = GET_SHIFT (clk_hw_data -> conf );
170+ const u32 clk_src_266 = 3 ;
171+ unsigned long flags ;
172+ int ret ;
173+
174+ if (event != PRE_RATE_CHANGE || (cnd -> new_rate / MEGA == 266 ))
175+ return NOTIFY_DONE ;
176+
177+ spin_lock_irqsave (& priv -> rmw_lock , flags );
178+
179+ /*
180+ * As per the HW manual, we should not directly switch from 533 MHz to
181+ * 400 MHz and vice versa. To change the setting from 2’b01 (533 MHz)
182+ * to 2’b10 (400 MHz) or vice versa, Switch to 2’b11 (266 MHz) first,
183+ * and then switch to the target setting (2’b01 (533 MHz) or 2’b10
184+ * (400 MHz)).
185+ * Setting a value of '0' to the SEL_SDHI0_SET or SEL_SDHI1_SET clock
186+ * switching register is prohibited.
187+ * The clock mux has 3 input clocks(533 MHz, 400 MHz, and 266 MHz), and
188+ * the index to value mapping is done by adding 1 to the index.
189+ */
190+
191+ writel ((CPG_WEN_BIT | clk_src_266 ) << shift , priv -> base + off );
192+
193+ /* Wait for the update done. */
194+ ret = rzg2l_cpg_wait_clk_update_done (priv -> base , clk_hw_data -> sconf );
195+
196+ spin_unlock_irqrestore (& priv -> rmw_lock , flags );
197+
198+ if (ret )
199+ dev_err (priv -> dev , "failed to switch to safe clk source\n" );
200+
201+ return notifier_from_errno (ret );
202+ }
203+
204+ static int rzg2l_register_notifier (struct clk_hw * hw , const struct cpg_core_clk * core ,
205+ struct rzg2l_cpg_priv * priv )
206+ {
207+ struct notifier_block * nb ;
208+
209+ if (!core -> notifier )
210+ return 0 ;
211+
212+ nb = devm_kzalloc (priv -> dev , sizeof (* nb ), GFP_KERNEL );
213+ if (!nb )
214+ return - ENOMEM ;
215+
216+ nb -> notifier_call = core -> notifier ;
217+
218+ return clk_notifier_register (hw -> clk , nb );
219+ }
220+
145221static struct clk * __init
146222rzg2l_cpg_div_clk_register (const struct cpg_core_clk * core ,
147223 struct clk * * clks ,
@@ -205,62 +281,43 @@ rzg2l_cpg_mux_clk_register(const struct cpg_core_clk *core,
205281static int rzg2l_cpg_sd_clk_mux_set_parent (struct clk_hw * hw , u8 index )
206282{
207283 struct clk_hw_data * clk_hw_data = to_clk_hw_data (hw );
284+ struct sd_mux_hw_data * sd_mux_hw_data = to_sd_mux_hw_data (clk_hw_data );
208285 struct rzg2l_cpg_priv * priv = clk_hw_data -> priv ;
209286 u32 off = GET_REG_OFFSET (clk_hw_data -> conf );
210287 u32 shift = GET_SHIFT (clk_hw_data -> conf );
211- const u32 clk_src_266 = 2 ;
212- u32 msk , val , bitmask ;
213288 unsigned long flags ;
289+ u32 val ;
214290 int ret ;
215291
216- /*
217- * As per the HW manual, we should not directly switch from 533 MHz to
218- * 400 MHz and vice versa. To change the setting from 2’b01 (533 MHz)
219- * to 2’b10 (400 MHz) or vice versa, Switch to 2’b11 (266 MHz) first,
220- * and then switch to the target setting (2’b01 (533 MHz) or 2’b10
221- * (400 MHz)).
222- * Setting a value of '0' to the SEL_SDHI0_SET or SEL_SDHI1_SET clock
223- * switching register is prohibited.
224- * The clock mux has 3 input clocks(533 MHz, 400 MHz, and 266 MHz), and
225- * the index to value mapping is done by adding 1 to the index.
226- */
227- bitmask = (GENMASK (GET_WIDTH (clk_hw_data -> conf ) - 1 , 0 ) << shift ) << 16 ;
228- msk = off ? CPG_CLKSTATUS_SELSDHI1_STS : CPG_CLKSTATUS_SELSDHI0_STS ;
292+ val = clk_mux_index_to_val (sd_mux_hw_data -> mtable , CLK_MUX_ROUND_CLOSEST , index );
293+
229294 spin_lock_irqsave (& priv -> rmw_lock , flags );
230- if (index != clk_src_266 ) {
231- writel (bitmask | ((clk_src_266 + 1 ) << shift ), priv -> base + off );
232-
233- ret = readl_poll_timeout_atomic (priv -> base + CPG_CLKSTATUS , val ,
234- !(val & msk ), 10 ,
235- CPG_SDHI_CLK_SWITCH_STATUS_TIMEOUT_US );
236- if (ret )
237- goto unlock ;
238- }
239295
240- writel (bitmask | ((index + 1 ) << shift ), priv -> base + off );
296+ writel ((CPG_WEN_BIT | val ) << shift , priv -> base + off );
297+
298+ /* Wait for the update done. */
299+ ret = rzg2l_cpg_wait_clk_update_done (priv -> base , clk_hw_data -> sconf );
241300
242- ret = readl_poll_timeout_atomic (priv -> base + CPG_CLKSTATUS , val ,
243- !(val & msk ), 10 ,
244- CPG_SDHI_CLK_SWITCH_STATUS_TIMEOUT_US );
245- unlock :
246301 spin_unlock_irqrestore (& priv -> rmw_lock , flags );
247302
248303 if (ret )
249- dev_err (priv -> dev , "failed to switch clk source \n" );
304+ dev_err (priv -> dev , "Failed to switch parent \n" );
250305
251306 return ret ;
252307}
253308
254309static u8 rzg2l_cpg_sd_clk_mux_get_parent (struct clk_hw * hw )
255310{
256311 struct clk_hw_data * clk_hw_data = to_clk_hw_data (hw );
312+ struct sd_mux_hw_data * sd_mux_hw_data = to_sd_mux_hw_data (clk_hw_data );
257313 struct rzg2l_cpg_priv * priv = clk_hw_data -> priv ;
258- u32 val = readl ( priv -> base + GET_REG_OFFSET ( clk_hw_data -> conf )) ;
314+ u32 val ;
259315
316+ val = readl (priv -> base + GET_REG_OFFSET (clk_hw_data -> conf ));
260317 val >>= GET_SHIFT (clk_hw_data -> conf );
261318 val &= GENMASK (GET_WIDTH (clk_hw_data -> conf ) - 1 , 0 );
262319
263- return val ? val - 1 : 0 ;
320+ return clk_mux_val_to_index ( hw , sd_mux_hw_data -> mtable , CLK_MUX_ROUND_CLOSEST , val ) ;
264321}
265322
266323static const struct clk_ops rzg2l_cpg_sd_clk_mux_ops = {
@@ -274,31 +331,40 @@ rzg2l_cpg_sd_mux_clk_register(const struct cpg_core_clk *core,
274331 void __iomem * base ,
275332 struct rzg2l_cpg_priv * priv )
276333{
277- struct sd_hw_data * sd_hw_data ;
334+ struct sd_mux_hw_data * sd_mux_hw_data ;
278335 struct clk_init_data init ;
279336 struct clk_hw * clk_hw ;
280337 int ret ;
281338
282- sd_hw_data = devm_kzalloc (priv -> dev , sizeof (* sd_hw_data ), GFP_KERNEL );
283- if (!sd_hw_data )
339+ sd_mux_hw_data = devm_kzalloc (priv -> dev , sizeof (* sd_mux_hw_data ), GFP_KERNEL );
340+ if (!sd_mux_hw_data )
284341 return ERR_PTR (- ENOMEM );
285342
286- sd_hw_data -> hw_data .priv = priv ;
287- sd_hw_data -> hw_data .conf = core -> conf ;
343+ sd_mux_hw_data -> hw_data .priv = priv ;
344+ sd_mux_hw_data -> hw_data .conf = core -> conf ;
345+ sd_mux_hw_data -> hw_data .sconf = core -> sconf ;
346+ sd_mux_hw_data -> mtable = core -> mtable ;
288347
289348 init .name = core -> name ;
290349 init .ops = & rzg2l_cpg_sd_clk_mux_ops ;
291- init .flags = 0 ;
350+ init .flags = core -> flag ;
292351 init .num_parents = core -> num_parents ;
293352 init .parent_names = core -> parent_names ;
294353
295- clk_hw = & sd_hw_data -> hw_data .hw ;
354+ clk_hw = & sd_mux_hw_data -> hw_data .hw ;
296355 clk_hw -> init = & init ;
297356
298357 ret = devm_clk_hw_register (priv -> dev , clk_hw );
299358 if (ret )
300359 return ERR_PTR (ret );
301360
361+ ret = rzg2l_register_notifier (clk_hw , core , priv );
362+ if (ret ) {
363+ dev_err (priv -> dev , "Failed to register notifier for %s\n" ,
364+ core -> name );
365+ return ERR_PTR (ret );
366+ }
367+
302368 return clk_hw -> clk ;
303369}
304370
0 commit comments