61
61
#define GET_REG_SAMPLL_CLK1 (val ) ((val >> 22) & 0xfff)
62
62
#define GET_REG_SAMPLL_CLK2 (val ) ((val >> 12) & 0xfff)
63
63
64
+ #define CPG_WEN_BIT BIT(16)
65
+
64
66
#define MAX_VCLK_FREQ (148500000)
65
67
66
68
/**
67
69
* struct clk_hw_data - clock hardware data
68
70
* @hw: clock hw
69
71
* @conf: clock configuration (register offset, shift, width)
72
+ * @sconf: clock status configuration (register offset, shift, width)
70
73
* @priv: CPG private data structure
71
74
*/
72
75
struct clk_hw_data {
73
76
struct clk_hw hw ;
74
77
u32 conf ;
78
+ u32 sconf ;
75
79
struct rzg2l_cpg_priv * priv ;
76
80
};
77
81
78
82
#define to_clk_hw_data (_hw ) container_of(_hw, struct clk_hw_data, hw)
79
83
80
84
/**
81
- * struct sd_hw_data - SD clock hardware data
85
+ * struct sd_mux_hw_data - SD MUX clock hardware data
82
86
* @hw_data: clock hw data
87
+ * @mtable: clock mux table
83
88
*/
84
- struct sd_hw_data {
89
+ struct sd_mux_hw_data {
85
90
struct clk_hw_data hw_data ;
91
+ const u32 * mtable ;
86
92
};
87
93
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)
89
95
90
96
struct rzg2l_pll5_param {
91
97
u32 pl5_fracin ;
@@ -142,6 +148,76 @@ static void rzg2l_cpg_del_clk_provider(void *data)
142
148
of_clk_del_provider (data );
143
149
}
144
150
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
+
145
221
static struct clk * __init
146
222
rzg2l_cpg_div_clk_register (const struct cpg_core_clk * core ,
147
223
struct clk * * clks ,
@@ -205,62 +281,43 @@ rzg2l_cpg_mux_clk_register(const struct cpg_core_clk *core,
205
281
static int rzg2l_cpg_sd_clk_mux_set_parent (struct clk_hw * hw , u8 index )
206
282
{
207
283
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 );
208
285
struct rzg2l_cpg_priv * priv = clk_hw_data -> priv ;
209
286
u32 off = GET_REG_OFFSET (clk_hw_data -> conf );
210
287
u32 shift = GET_SHIFT (clk_hw_data -> conf );
211
- const u32 clk_src_266 = 2 ;
212
- u32 msk , val , bitmask ;
213
288
unsigned long flags ;
289
+ u32 val ;
214
290
int ret ;
215
291
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
+
229
294
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
- }
239
295
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 );
241
300
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 :
246
301
spin_unlock_irqrestore (& priv -> rmw_lock , flags );
247
302
248
303
if (ret )
249
- dev_err (priv -> dev , "failed to switch clk source \n" );
304
+ dev_err (priv -> dev , "Failed to switch parent \n" );
250
305
251
306
return ret ;
252
307
}
253
308
254
309
static u8 rzg2l_cpg_sd_clk_mux_get_parent (struct clk_hw * hw )
255
310
{
256
311
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 );
257
313
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 ;
259
315
316
+ val = readl (priv -> base + GET_REG_OFFSET (clk_hw_data -> conf ));
260
317
val >>= GET_SHIFT (clk_hw_data -> conf );
261
318
val &= GENMASK (GET_WIDTH (clk_hw_data -> conf ) - 1 , 0 );
262
319
263
- return val ? val - 1 : 0 ;
320
+ return clk_mux_val_to_index ( hw , sd_mux_hw_data -> mtable , CLK_MUX_ROUND_CLOSEST , val ) ;
264
321
}
265
322
266
323
static 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,
274
331
void __iomem * base ,
275
332
struct rzg2l_cpg_priv * priv )
276
333
{
277
- struct sd_hw_data * sd_hw_data ;
334
+ struct sd_mux_hw_data * sd_mux_hw_data ;
278
335
struct clk_init_data init ;
279
336
struct clk_hw * clk_hw ;
280
337
int ret ;
281
338
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 )
284
341
return ERR_PTR (- ENOMEM );
285
342
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 ;
288
347
289
348
init .name = core -> name ;
290
349
init .ops = & rzg2l_cpg_sd_clk_mux_ops ;
291
- init .flags = 0 ;
350
+ init .flags = core -> flag ;
292
351
init .num_parents = core -> num_parents ;
293
352
init .parent_names = core -> parent_names ;
294
353
295
- clk_hw = & sd_hw_data -> hw_data .hw ;
354
+ clk_hw = & sd_mux_hw_data -> hw_data .hw ;
296
355
clk_hw -> init = & init ;
297
356
298
357
ret = devm_clk_hw_register (priv -> dev , clk_hw );
299
358
if (ret )
300
359
return ERR_PTR (ret );
301
360
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
+
302
368
return clk_hw -> clk ;
303
369
}
304
370
0 commit comments