1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Renesas RZ/V2H(P) Clock Pulse Generator
4 *
5 * Copyright (C) 2024 Renesas Electronics Corp.
6 *
7 * Based on rzg2l-cpg.c
8 *
9 * Copyright (C) 2015 Glider bvba
10 * Copyright (C) 2013 Ideas On Board SPRL
11 * Copyright (C) 2015 Renesas Electronics Corp.
12 */
13
14 #include <linux/bitfield.h>
15 #include <linux/clk.h>
16 #include <linux/clk-provider.h>
17 #include <linux/clk/renesas.h>
18 #include <linux/delay.h>
19 #include <linux/init.h>
20 #include <linux/iopoll.h>
21 #include <linux/limits.h>
22 #include <linux/math.h>
23 #include <linux/math64.h>
24 #include <linux/minmax.h>
25 #include <linux/module.h>
26 #include <linux/of.h>
27 #include <linux/platform_device.h>
28 #include <linux/pm_clock.h>
29 #include <linux/pm_domain.h>
30 #include <linux/refcount.h>
31 #include <linux/reset-controller.h>
32 #include <linux/string_choices.h>
33 #include <linux/units.h>
34
35 #include <dt-bindings/clock/renesas-cpg-mssr.h>
36
37 #include "rzv2h-cpg.h"
38
39 #ifdef DEBUG
40 #define WARN_DEBUG(x) WARN_ON(x)
41 #else
42 #define WARN_DEBUG(x) do { } while (0)
43 #endif
44
45 #define GET_CLK_ON_OFFSET(x) (0x600 + ((x) * 4))
46 #define GET_CLK_MON_OFFSET(x) (0x800 + ((x) * 4))
47 #define GET_RST_OFFSET(x) (0x900 + ((x) * 4))
48 #define GET_RST_MON_OFFSET(x) (0xA00 + ((x) * 4))
49
50 #define CPG_BUS_1_MSTOP (0xd00)
51 #define CPG_BUS_MSTOP(m) (CPG_BUS_1_MSTOP + ((m) - 1) * 4)
52
53 #define CPG_PLL_STBY(x) ((x))
54 #define CPG_PLL_STBY_RESETB BIT(0)
55 #define CPG_PLL_STBY_SSC_EN BIT(2)
56 #define CPG_PLL_STBY_RESETB_WEN BIT(16)
57 #define CPG_PLL_STBY_SSC_EN_WEN BIT(18)
58 #define CPG_PLL_CLK1(x) ((x) + 0x004)
59 #define CPG_PLL_CLK1_KDIV GENMASK(31, 16)
60 #define CPG_PLL_CLK1_MDIV GENMASK(15, 6)
61 #define CPG_PLL_CLK1_PDIV GENMASK(5, 0)
62 #define CPG_PLL_CLK2(x) ((x) + 0x008)
63 #define CPG_PLL_CLK2_SDIV GENMASK(2, 0)
64 #define CPG_PLL_MON(x) ((x) + 0x010)
65 #define CPG_PLL_MON_RESETB BIT(0)
66 #define CPG_PLL_MON_LOCK BIT(4)
67
68 #define DDIV_DIVCTL_WEN(shift) BIT((shift) + 16)
69
70 #define GET_MOD_CLK_ID(base, index, bit) \
71 ((base) + ((((index) * (16))) + (bit)))
72
73 #define CPG_CLKSTATUS0 (0x700)
74
75 /* On RZ/G3E SoC we have two DSI PLLs */
76 #define MAX_CPG_DSI_PLL 2
77
78 #define CPG_PLLDSI_SMUX_LVDS_DUTY_NUM 4
79 #define CPG_PLLDSI_SMUX_LVDS_DUTY_DEN 7
80 #define CPG_PLLDSI_SMUX_DSI_RGB_DUTY_NUM 1
81 #define CPG_PLLDSI_SMUX_DSI_RGB_DUTY_DEN 2
82
83 /**
84 * struct rzv2h_pll_dsi_info - PLL DSI information, holds the limits and parameters
85 *
86 * @pll_dsi_limits: PLL DSI parameters limits
87 * @pll_dsi_parameters: Calculated PLL DSI parameters
88 * @req_pll_dsi_rate: Requested PLL DSI rate
89 */
90 struct rzv2h_pll_dsi_info {
91 const struct rzv2h_pll_limits *pll_dsi_limits;
92 struct rzv2h_pll_div_pars pll_dsi_parameters;
93 unsigned long req_pll_dsi_rate;
94 };
95
96 /**
97 * struct rzv2h_cpg_priv - Clock Pulse Generator Private Data
98 *
99 * @dev: CPG device
100 * @base: CPG register block base address
101 * @rmw_lock: protects register accesses
102 * @clks: Array containing all Core and Module Clocks
103 * @num_core_clks: Number of Core Clocks in clks[]
104 * @num_mod_clks: Number of Module Clocks in clks[]
105 * @resets: Array of resets
106 * @num_resets: Number of Module Resets in info->resets[]
107 * @last_dt_core_clk: ID of the last Core Clock exported to DT
108 * @ff_mod_status_ops: Fixed Factor Module Status Clock operations
109 * @mstop_count: Array of mstop values
110 * @rcdev: Reset controller entity
111 * @pll_dsi_info: Array of PLL DSI information, holds the limits and parameters
112 */
113 struct rzv2h_cpg_priv {
114 struct device *dev;
115 void __iomem *base;
116 spinlock_t rmw_lock;
117
118 struct clk **clks;
119 unsigned int num_core_clks;
120 unsigned int num_mod_clks;
121 struct rzv2h_reset *resets;
122 unsigned int num_resets;
123 unsigned int last_dt_core_clk;
124
125 struct clk_ops *ff_mod_status_ops;
126
127 atomic_t *mstop_count;
128
129 struct reset_controller_dev rcdev;
130
131 struct rzv2h_pll_dsi_info pll_dsi_info[MAX_CPG_DSI_PLL];
132 };
133
134 #define rcdev_to_priv(x) container_of(x, struct rzv2h_cpg_priv, rcdev)
135
136 struct pll_clk {
137 struct rzv2h_cpg_priv *priv;
138 struct clk_hw hw;
139 struct pll pll;
140 };
141
142 #define to_pll(_hw) container_of(_hw, struct pll_clk, hw)
143
144 /**
145 * struct mod_clock - Module clock
146 *
147 * @priv: CPG private data
148 * @mstop_data: mstop data relating to module clock
149 * @hw: handle between common and hardware-specific interfaces
150 * @no_pm: flag to indicate PM is not supported
151 * @on_index: register offset
152 * @on_bit: ON/MON bit
153 * @mon_index: monitor register offset
154 * @mon_bit: monitor bit
155 * @ext_clk_mux_index: mux index for external clock source, or -1 if internal
156 */
157 struct mod_clock {
158 struct rzv2h_cpg_priv *priv;
159 unsigned int mstop_data;
160 struct clk_hw hw;
161 bool no_pm;
162 u8 on_index;
163 u8 on_bit;
164 s8 mon_index;
165 u8 mon_bit;
166 s8 ext_clk_mux_index;
167 };
168
169 #define to_mod_clock(_hw) container_of(_hw, struct mod_clock, hw)
170
171 /**
172 * struct ddiv_clk - DDIV clock
173 *
174 * @priv: CPG private data
175 * @div: divider clk
176 * @mon: monitor bit in CPG_CLKSTATUS0 register
177 */
178 struct ddiv_clk {
179 struct rzv2h_cpg_priv *priv;
180 struct clk_divider div;
181 u8 mon;
182 };
183
184 #define to_ddiv_clock(_div) container_of(_div, struct ddiv_clk, div)
185
186 /**
187 * struct rzv2h_ff_mod_status_clk - Fixed Factor Module Status Clock
188 *
189 * @priv: CPG private data
190 * @conf: fixed mod configuration
191 * @fix: fixed factor clock
192 */
193 struct rzv2h_ff_mod_status_clk {
194 struct rzv2h_cpg_priv *priv;
195 struct fixed_mod_conf conf;
196 struct clk_fixed_factor fix;
197 };
198
199 #define to_rzv2h_ff_mod_status_clk(_hw) \
200 container_of(_hw, struct rzv2h_ff_mod_status_clk, fix.hw)
201
202 /**
203 * struct rzv2h_plldsi_div_clk - PLL DSI DDIV clock
204 *
205 * @dtable: divider table
206 * @priv: CPG private data
207 * @hw: divider clk
208 * @ddiv: divider configuration
209 */
210 struct rzv2h_plldsi_div_clk {
211 const struct clk_div_table *dtable;
212 struct rzv2h_cpg_priv *priv;
213 struct clk_hw hw;
214 struct ddiv ddiv;
215 };
216
217 #define to_plldsi_div_clk(_hw) \
218 container_of(_hw, struct rzv2h_plldsi_div_clk, hw)
219
220 #define RZV2H_MAX_DIV_TABLES (16)
221
222 /**
223 * struct rzv2h_plldsi_mux_clk - PLL DSI MUX clock
224 *
225 * @priv: CPG private data
226 * @mux: mux clk
227 */
228 struct rzv2h_plldsi_mux_clk {
229 struct rzv2h_cpg_priv *priv;
230 struct clk_mux mux;
231 };
232
233 #define to_plldsi_clk_mux(_mux) \
234 container_of(_mux, struct rzv2h_plldsi_mux_clk, mux)
235
rzv2h_cpg_plldsi_div_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)236 static unsigned long rzv2h_cpg_plldsi_div_recalc_rate(struct clk_hw *hw,
237 unsigned long parent_rate)
238 {
239 struct rzv2h_plldsi_div_clk *dsi_div = to_plldsi_div_clk(hw);
240 struct rzv2h_cpg_priv *priv = dsi_div->priv;
241 struct ddiv ddiv = dsi_div->ddiv;
242 u32 div;
243
244 div = readl(priv->base + ddiv.offset);
245 div >>= ddiv.shift;
246 div &= clk_div_mask(ddiv.width);
247 div = dsi_div->dtable[div].div;
248
249 return DIV_ROUND_CLOSEST_ULL(parent_rate, div);
250 }
251
rzv2h_cpg_plldsi_div_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)252 static int rzv2h_cpg_plldsi_div_determine_rate(struct clk_hw *hw,
253 struct clk_rate_request *req)
254 {
255 struct rzv2h_plldsi_div_clk *dsi_div = to_plldsi_div_clk(hw);
256 struct pll_clk *pll_clk = to_pll(clk_hw_get_parent(hw));
257 struct rzv2h_cpg_priv *priv = dsi_div->priv;
258 u8 table[RZV2H_MAX_DIV_TABLES] = { 0 };
259 struct rzv2h_pll_div_pars *dsi_params;
260 struct rzv2h_pll_dsi_info *dsi_info;
261 const struct clk_div_table *div;
262 unsigned int i = 0;
263 u64 rate_millihz;
264
265 dsi_info = &priv->pll_dsi_info[pll_clk->pll.instance];
266 dsi_params = &dsi_info->pll_dsi_parameters;
267
268 rate_millihz = mul_u32_u32(req->rate, MILLI);
269 if (rate_millihz == dsi_params->div.error_millihz + dsi_params->div.freq_millihz)
270 goto exit_determine_rate;
271
272 for (div = dsi_div->dtable; div->div; div++) {
273 if (i >= RZV2H_MAX_DIV_TABLES)
274 return -EINVAL;
275 table[i++] = div->div;
276 }
277
278 if (!rzv2h_get_pll_divs_pars(dsi_info->pll_dsi_limits, dsi_params, table, i,
279 rate_millihz)) {
280 dev_err(priv->dev, "failed to determine rate for req->rate: %lu\n",
281 req->rate);
282 return -EINVAL;
283 }
284
285 exit_determine_rate:
286 req->rate = DIV_ROUND_CLOSEST_ULL(dsi_params->div.freq_millihz, MILLI);
287 req->best_parent_rate = req->rate * dsi_params->div.divider_value;
288 dsi_info->req_pll_dsi_rate = req->best_parent_rate;
289
290 return 0;
291 }
292
rzv2h_cpg_plldsi_div_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)293 static int rzv2h_cpg_plldsi_div_set_rate(struct clk_hw *hw,
294 unsigned long rate,
295 unsigned long parent_rate)
296 {
297 struct rzv2h_plldsi_div_clk *dsi_div = to_plldsi_div_clk(hw);
298 struct pll_clk *pll_clk = to_pll(clk_hw_get_parent(hw));
299 struct rzv2h_cpg_priv *priv = dsi_div->priv;
300 struct rzv2h_pll_div_pars *dsi_params;
301 struct rzv2h_pll_dsi_info *dsi_info;
302 struct ddiv ddiv = dsi_div->ddiv;
303 const struct clk_div_table *clkt;
304 bool divider_found = false;
305 u32 val, shift;
306
307 dsi_info = &priv->pll_dsi_info[pll_clk->pll.instance];
308 dsi_params = &dsi_info->pll_dsi_parameters;
309
310 for (clkt = dsi_div->dtable; clkt->div; clkt++) {
311 if (clkt->div == dsi_params->div.divider_value) {
312 divider_found = true;
313 break;
314 }
315 }
316
317 if (!divider_found)
318 return -EINVAL;
319
320 shift = ddiv.shift;
321 val = readl(priv->base + ddiv.offset) | DDIV_DIVCTL_WEN(shift);
322 val &= ~(clk_div_mask(ddiv.width) << shift);
323 val |= clkt->val << shift;
324 writel(val, priv->base + ddiv.offset);
325
326 return 0;
327 }
328
329 static const struct clk_ops rzv2h_cpg_plldsi_div_ops = {
330 .recalc_rate = rzv2h_cpg_plldsi_div_recalc_rate,
331 .determine_rate = rzv2h_cpg_plldsi_div_determine_rate,
332 .set_rate = rzv2h_cpg_plldsi_div_set_rate,
333 };
334
335 static struct clk * __init
rzv2h_cpg_plldsi_div_clk_register(const struct cpg_core_clk * core,struct rzv2h_cpg_priv * priv)336 rzv2h_cpg_plldsi_div_clk_register(const struct cpg_core_clk *core,
337 struct rzv2h_cpg_priv *priv)
338 {
339 struct rzv2h_plldsi_div_clk *clk_hw_data;
340 struct clk **clks = priv->clks;
341 struct clk_init_data init;
342 const struct clk *parent;
343 const char *parent_name;
344 struct clk_hw *clk_hw;
345 int ret;
346
347 parent = clks[core->parent];
348 if (IS_ERR(parent))
349 return ERR_CAST(parent);
350
351 clk_hw_data = devm_kzalloc(priv->dev, sizeof(*clk_hw_data), GFP_KERNEL);
352 if (!clk_hw_data)
353 return ERR_PTR(-ENOMEM);
354
355 clk_hw_data->priv = priv;
356 clk_hw_data->ddiv = core->cfg.ddiv;
357 clk_hw_data->dtable = core->dtable;
358
359 parent_name = __clk_get_name(parent);
360 init.name = core->name;
361 init.ops = &rzv2h_cpg_plldsi_div_ops;
362 init.flags = core->flag;
363 init.parent_names = &parent_name;
364 init.num_parents = 1;
365
366 clk_hw = &clk_hw_data->hw;
367 clk_hw->init = &init;
368
369 ret = devm_clk_hw_register(priv->dev, clk_hw);
370 if (ret)
371 return ERR_PTR(ret);
372
373 return clk_hw->clk;
374 }
375
rzv2h_cpg_plldsi_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)376 static int rzv2h_cpg_plldsi_determine_rate(struct clk_hw *hw,
377 struct clk_rate_request *req)
378 {
379 struct pll_clk *pll_clk = to_pll(hw);
380 struct rzv2h_cpg_priv *priv = pll_clk->priv;
381 struct rzv2h_pll_dsi_info *dsi_info;
382 u64 rate_millihz;
383
384 dsi_info = &priv->pll_dsi_info[pll_clk->pll.instance];
385 /* check if the divider has already invoked the algorithm */
386 if (req->rate == dsi_info->req_pll_dsi_rate)
387 return 0;
388
389 /* If the req->rate doesn't match we do the calculation assuming there is no divider */
390 rate_millihz = mul_u32_u32(req->rate, MILLI);
391 if (!rzv2h_get_pll_pars(dsi_info->pll_dsi_limits,
392 &dsi_info->pll_dsi_parameters.pll, rate_millihz)) {
393 dev_err(priv->dev,
394 "failed to determine rate for req->rate: %lu\n",
395 req->rate);
396 return -EINVAL;
397 }
398
399 req->rate = DIV_ROUND_CLOSEST_ULL(dsi_info->pll_dsi_parameters.pll.freq_millihz, MILLI);
400 dsi_info->req_pll_dsi_rate = req->rate;
401
402 return 0;
403 }
404
rzv2h_cpg_pll_set_rate(struct pll_clk * pll_clk,struct rzv2h_pll_pars * params,bool ssc_disable)405 static int rzv2h_cpg_pll_set_rate(struct pll_clk *pll_clk,
406 struct rzv2h_pll_pars *params,
407 bool ssc_disable)
408 {
409 struct rzv2h_cpg_priv *priv = pll_clk->priv;
410 u16 offset = pll_clk->pll.offset;
411 u32 val;
412 int ret;
413
414 /* Put PLL into standby mode */
415 writel(CPG_PLL_STBY_RESETB_WEN, priv->base + CPG_PLL_STBY(offset));
416 ret = readl_poll_timeout_atomic(priv->base + CPG_PLL_MON(offset),
417 val, !(val & CPG_PLL_MON_LOCK),
418 100, 2000);
419 if (ret) {
420 dev_err(priv->dev, "Failed to put PLLDSI into standby mode\n");
421 return ret;
422 }
423
424 /* Output clock setting 1 */
425 writel(FIELD_PREP(CPG_PLL_CLK1_KDIV, (u16)params->k) |
426 FIELD_PREP(CPG_PLL_CLK1_MDIV, params->m) |
427 FIELD_PREP(CPG_PLL_CLK1_PDIV, params->p),
428 priv->base + CPG_PLL_CLK1(offset));
429
430 /* Output clock setting 2 */
431 val = readl(priv->base + CPG_PLL_CLK2(offset));
432 writel((val & ~CPG_PLL_CLK2_SDIV) | FIELD_PREP(CPG_PLL_CLK2_SDIV, params->s),
433 priv->base + CPG_PLL_CLK2(offset));
434
435 /* Put PLL to normal mode */
436 if (ssc_disable)
437 val = CPG_PLL_STBY_SSC_EN_WEN;
438 else
439 val = CPG_PLL_STBY_SSC_EN_WEN | CPG_PLL_STBY_SSC_EN;
440 writel(val | CPG_PLL_STBY_RESETB_WEN | CPG_PLL_STBY_RESETB,
441 priv->base + CPG_PLL_STBY(offset));
442
443 /* PLL normal mode transition, output clock stability check */
444 ret = readl_poll_timeout_atomic(priv->base + CPG_PLL_MON(offset),
445 val, (val & CPG_PLL_MON_LOCK),
446 100, 2000);
447 if (ret) {
448 dev_err(priv->dev, "Failed to put PLLDSI into normal mode\n");
449 return ret;
450 }
451
452 return 0;
453 }
454
rzv2h_cpg_plldsi_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)455 static int rzv2h_cpg_plldsi_set_rate(struct clk_hw *hw, unsigned long rate,
456 unsigned long parent_rate)
457 {
458 struct pll_clk *pll_clk = to_pll(hw);
459 struct rzv2h_pll_dsi_info *dsi_info;
460 struct rzv2h_cpg_priv *priv = pll_clk->priv;
461
462 dsi_info = &priv->pll_dsi_info[pll_clk->pll.instance];
463
464 return rzv2h_cpg_pll_set_rate(pll_clk, &dsi_info->pll_dsi_parameters.pll, true);
465 }
466
rzv2h_cpg_plldsi_smux_get_parent(struct clk_hw * hw)467 static u8 rzv2h_cpg_plldsi_smux_get_parent(struct clk_hw *hw)
468 {
469 return clk_mux_ops.get_parent(hw);
470 }
471
rzv2h_cpg_plldsi_smux_set_parent(struct clk_hw * hw,u8 index)472 static int rzv2h_cpg_plldsi_smux_set_parent(struct clk_hw *hw, u8 index)
473 {
474 return clk_mux_ops.set_parent(hw, index);
475 }
476
rzv2h_cpg_plldsi_smux_lvds_determine_rate(struct rzv2h_cpg_priv * priv,struct pll_clk * pll_clk,struct clk_rate_request * req)477 static int rzv2h_cpg_plldsi_smux_lvds_determine_rate(struct rzv2h_cpg_priv *priv,
478 struct pll_clk *pll_clk,
479 struct clk_rate_request *req)
480 {
481 struct rzv2h_pll_div_pars *dsi_params;
482 struct rzv2h_pll_dsi_info *dsi_info;
483 u8 lvds_table[] = { 7 };
484 u64 rate_millihz;
485
486 dsi_info = &priv->pll_dsi_info[pll_clk->pll.instance];
487 dsi_params = &dsi_info->pll_dsi_parameters;
488
489 rate_millihz = mul_u32_u32(req->rate, MILLI);
490 if (!rzv2h_get_pll_divs_pars(dsi_info->pll_dsi_limits, dsi_params,
491 lvds_table, ARRAY_SIZE(lvds_table), rate_millihz)) {
492 dev_err(priv->dev, "failed to determine rate for req->rate: %lu\n",
493 req->rate);
494 return -EINVAL;
495 }
496
497 req->rate = DIV_ROUND_CLOSEST_ULL(dsi_params->div.freq_millihz, MILLI);
498 req->best_parent_rate = req->rate;
499 dsi_info->req_pll_dsi_rate = req->best_parent_rate * dsi_params->div.divider_value;
500
501 return 0;
502 }
503
rzv2h_cpg_plldsi_smux_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)504 static int rzv2h_cpg_plldsi_smux_determine_rate(struct clk_hw *hw,
505 struct clk_rate_request *req)
506 {
507 struct clk_mux *mux = to_clk_mux(hw);
508 struct rzv2h_plldsi_mux_clk *dsi_mux = to_plldsi_clk_mux(mux);
509 struct pll_clk *pll_clk = to_pll(clk_hw_get_parent(hw));
510 struct rzv2h_cpg_priv *priv = dsi_mux->priv;
511
512 /*
513 * For LVDS output (parent index 0), calculate PLL parameters with
514 * fixed divider value of 7. For DSI/RGB output (parent index 1) skip
515 * PLL calculation here as it's handled by determine_rate of the
516 * divider (up one level).
517 */
518 if (!clk_mux_ops.get_parent(hw))
519 return rzv2h_cpg_plldsi_smux_lvds_determine_rate(priv, pll_clk, req);
520
521 req->best_parent_rate = req->rate;
522 return 0;
523 }
524
rzv2h_cpg_plldsi_smux_get_duty_cycle(struct clk_hw * hw,struct clk_duty * duty)525 static int rzv2h_cpg_plldsi_smux_get_duty_cycle(struct clk_hw *hw,
526 struct clk_duty *duty)
527 {
528 u8 parent = clk_mux_ops.get_parent(hw);
529
530 /*
531 * CDIV7_DSIx_CLK - LVDS path (div7) - duty 4/7.
532 * CSDIV_DSIx - DSI/RGB path (csdiv) - duty 1/2.
533 */
534 if (parent == 0) {
535 duty->num = CPG_PLLDSI_SMUX_LVDS_DUTY_NUM;
536 duty->den = CPG_PLLDSI_SMUX_LVDS_DUTY_DEN;
537 } else {
538 duty->num = CPG_PLLDSI_SMUX_DSI_RGB_DUTY_NUM;
539 duty->den = CPG_PLLDSI_SMUX_DSI_RGB_DUTY_DEN;
540 }
541
542 return 0;
543 }
544
rzv2h_cpg_plldsi_smux_set_duty_cycle(struct clk_hw * hw,struct clk_duty * duty)545 static int rzv2h_cpg_plldsi_smux_set_duty_cycle(struct clk_hw *hw,
546 struct clk_duty *duty)
547 {
548 struct clk_hw *parent_hw;
549 u8 parent_idx;
550
551 /*
552 * Select parent based on requested duty cycle:
553 * - If duty > 50% (num/den > 1/2), select LVDS path (parent 0)
554 * - Otherwise, select DSI/RGB path (parent 1)
555 */
556 if (duty->num * CPG_PLLDSI_SMUX_DSI_RGB_DUTY_DEN >
557 duty->den * CPG_PLLDSI_SMUX_DSI_RGB_DUTY_NUM)
558 parent_idx = 0;
559 else
560 parent_idx = 1;
561
562 if (parent_idx >= clk_hw_get_num_parents(hw))
563 return -EINVAL;
564
565 parent_hw = clk_hw_get_parent_by_index(hw, parent_idx);
566 if (!parent_hw)
567 return -EINVAL;
568
569 return clk_hw_set_parent(hw, parent_hw);
570 }
571
572 static const struct clk_ops rzv2h_cpg_plldsi_smux_ops = {
573 .determine_rate = rzv2h_cpg_plldsi_smux_determine_rate,
574 .get_parent = rzv2h_cpg_plldsi_smux_get_parent,
575 .set_parent = rzv2h_cpg_plldsi_smux_set_parent,
576 .get_duty_cycle = rzv2h_cpg_plldsi_smux_get_duty_cycle,
577 .set_duty_cycle = rzv2h_cpg_plldsi_smux_set_duty_cycle,
578 };
579
580 static struct clk * __init
rzv2h_cpg_plldsi_smux_clk_register(const struct cpg_core_clk * core,struct rzv2h_cpg_priv * priv)581 rzv2h_cpg_plldsi_smux_clk_register(const struct cpg_core_clk *core,
582 struct rzv2h_cpg_priv *priv)
583 {
584 struct rzv2h_plldsi_mux_clk *clk_hw_data;
585 struct clk_init_data init;
586 struct clk_hw *clk_hw;
587 struct smuxed smux;
588 int ret;
589
590 smux = core->cfg.smux;
591
592 if (smux.shift + smux.width > 16) {
593 dev_err(priv->dev, "mux value exceeds LOWORD field\n");
594 return ERR_PTR(-EINVAL);
595 }
596
597 clk_hw_data = devm_kzalloc(priv->dev, sizeof(*clk_hw_data), GFP_KERNEL);
598 if (!clk_hw_data)
599 return ERR_PTR(-ENOMEM);
600
601 clk_hw_data->priv = priv;
602
603 init.name = core->name;
604 init.ops = &rzv2h_cpg_plldsi_smux_ops;
605 init.flags = core->flag;
606 init.parent_names = core->parent_names;
607 init.num_parents = core->num_parents;
608
609 clk_hw_data->mux.reg = priv->base + smux.offset;
610
611 clk_hw_data->mux.shift = smux.shift;
612 clk_hw_data->mux.mask = clk_div_mask(smux.width);
613 clk_hw_data->mux.flags = core->mux_flags;
614 clk_hw_data->mux.lock = &priv->rmw_lock;
615
616 clk_hw = &clk_hw_data->mux.hw;
617 clk_hw->init = &init;
618
619 ret = devm_clk_hw_register(priv->dev, clk_hw);
620 if (ret)
621 return ERR_PTR(ret);
622
623 return clk_hw->clk;
624 }
625
rzv2h_cpg_pll_clk_is_enabled(struct clk_hw * hw)626 static int rzv2h_cpg_pll_clk_is_enabled(struct clk_hw *hw)
627 {
628 struct pll_clk *pll_clk = to_pll(hw);
629 struct rzv2h_cpg_priv *priv = pll_clk->priv;
630 u32 val = readl(priv->base + CPG_PLL_MON(pll_clk->pll.offset));
631
632 /* Ensure both RESETB and LOCK bits are set */
633 return (val & (CPG_PLL_MON_RESETB | CPG_PLL_MON_LOCK)) ==
634 (CPG_PLL_MON_RESETB | CPG_PLL_MON_LOCK);
635 }
636
rzv2h_cpg_pll_clk_enable(struct clk_hw * hw)637 static int rzv2h_cpg_pll_clk_enable(struct clk_hw *hw)
638 {
639 struct pll_clk *pll_clk = to_pll(hw);
640 struct rzv2h_cpg_priv *priv = pll_clk->priv;
641 struct pll pll = pll_clk->pll;
642 u32 stby_offset;
643 u32 mon_offset;
644 u32 val;
645 int ret;
646
647 if (rzv2h_cpg_pll_clk_is_enabled(hw))
648 return 0;
649
650 stby_offset = CPG_PLL_STBY(pll.offset);
651 mon_offset = CPG_PLL_MON(pll.offset);
652
653 writel(CPG_PLL_STBY_RESETB_WEN | CPG_PLL_STBY_RESETB,
654 priv->base + stby_offset);
655
656 /*
657 * Ensure PLL enters into normal mode
658 *
659 * Note: There is no HW information about the worst case latency.
660 *
661 * Since this latency might depend on external crystal or PLL rate,
662 * use a "super" safe timeout value.
663 */
664 ret = readl_poll_timeout_atomic(priv->base + mon_offset, val,
665 (val & (CPG_PLL_MON_RESETB | CPG_PLL_MON_LOCK)) ==
666 (CPG_PLL_MON_RESETB | CPG_PLL_MON_LOCK), 200, 2000);
667 if (ret)
668 dev_err(priv->dev, "Failed to enable PLL 0x%x/%pC\n",
669 stby_offset, hw->clk);
670
671 return ret;
672 }
673
rzv2h_cpg_pll_clk_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)674 static unsigned long rzv2h_cpg_pll_clk_recalc_rate(struct clk_hw *hw,
675 unsigned long parent_rate)
676 {
677 struct pll_clk *pll_clk = to_pll(hw);
678 struct rzv2h_cpg_priv *priv = pll_clk->priv;
679 struct pll pll = pll_clk->pll;
680 unsigned int clk1, clk2;
681 u64 rate;
682
683 if (!pll.has_clkn)
684 return 0;
685
686 clk1 = readl(priv->base + CPG_PLL_CLK1(pll.offset));
687 clk2 = readl(priv->base + CPG_PLL_CLK2(pll.offset));
688
689 rate = mul_u64_u32_shr(parent_rate, (FIELD_GET(CPG_PLL_CLK1_MDIV, clk1) << 16) +
690 (s16)FIELD_GET(CPG_PLL_CLK1_KDIV, clk1),
691 16 + FIELD_GET(CPG_PLL_CLK2_SDIV, clk2));
692
693 return DIV_ROUND_CLOSEST_ULL(rate, FIELD_GET(CPG_PLL_CLK1_PDIV, clk1));
694 }
695
696 static const struct clk_ops rzv2h_cpg_plldsi_ops = {
697 .recalc_rate = rzv2h_cpg_pll_clk_recalc_rate,
698 .determine_rate = rzv2h_cpg_plldsi_determine_rate,
699 .set_rate = rzv2h_cpg_plldsi_set_rate,
700 };
701
702 static const struct clk_ops rzv2h_cpg_pll_ops = {
703 .is_enabled = rzv2h_cpg_pll_clk_is_enabled,
704 .enable = rzv2h_cpg_pll_clk_enable,
705 .recalc_rate = rzv2h_cpg_pll_clk_recalc_rate,
706 };
707
708 static struct clk * __init
rzv2h_cpg_pll_clk_register(const struct cpg_core_clk * core,struct rzv2h_cpg_priv * priv,const struct clk_ops * ops)709 rzv2h_cpg_pll_clk_register(const struct cpg_core_clk *core,
710 struct rzv2h_cpg_priv *priv,
711 const struct clk_ops *ops)
712 {
713 struct device *dev = priv->dev;
714 struct clk_init_data init;
715 const struct clk *parent;
716 const char *parent_name;
717 struct pll_clk *pll_clk;
718 int ret;
719
720 parent = priv->clks[core->parent];
721 if (IS_ERR(parent))
722 return ERR_CAST(parent);
723
724 pll_clk = devm_kzalloc(dev, sizeof(*pll_clk), GFP_KERNEL);
725 if (!pll_clk)
726 return ERR_PTR(-ENOMEM);
727
728 if (core->type == CLK_TYPE_PLLDSI)
729 priv->pll_dsi_info[core->cfg.pll.instance].pll_dsi_limits =
730 core->cfg.pll.limits;
731
732 parent_name = __clk_get_name(parent);
733 init.name = core->name;
734 init.ops = ops;
735 init.flags = 0;
736 init.parent_names = &parent_name;
737 init.num_parents = 1;
738
739 pll_clk->hw.init = &init;
740 pll_clk->pll = core->cfg.pll;
741 pll_clk->priv = priv;
742
743 ret = devm_clk_hw_register(dev, &pll_clk->hw);
744 if (ret)
745 return ERR_PTR(ret);
746
747 return pll_clk->hw.clk;
748 }
749
rzv2h_ddiv_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)750 static unsigned long rzv2h_ddiv_recalc_rate(struct clk_hw *hw,
751 unsigned long parent_rate)
752 {
753 struct clk_divider *divider = to_clk_divider(hw);
754 unsigned int val;
755
756 val = readl(divider->reg) >> divider->shift;
757 val &= clk_div_mask(divider->width);
758
759 return divider_recalc_rate(hw, parent_rate, val, divider->table,
760 divider->flags, divider->width);
761 }
762
rzv2h_ddiv_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)763 static int rzv2h_ddiv_determine_rate(struct clk_hw *hw,
764 struct clk_rate_request *req)
765 {
766 struct clk_divider *divider = to_clk_divider(hw);
767
768 return divider_determine_rate(hw, req, divider->table, divider->width,
769 divider->flags);
770 }
771
rzv2h_cpg_wait_ddiv_clk_update_done(void __iomem * base,u8 mon)772 static inline int rzv2h_cpg_wait_ddiv_clk_update_done(void __iomem *base, u8 mon)
773 {
774 u32 bitmask = BIT(mon);
775 u32 val;
776
777 if (mon == CSDIV_NO_MON)
778 return 0;
779
780 return readl_poll_timeout_atomic(base + CPG_CLKSTATUS0, val, !(val & bitmask), 10, 200);
781 }
782
rzv2h_ddiv_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)783 static int rzv2h_ddiv_set_rate(struct clk_hw *hw, unsigned long rate,
784 unsigned long parent_rate)
785 {
786 struct clk_divider *divider = to_clk_divider(hw);
787 struct ddiv_clk *ddiv = to_ddiv_clock(divider);
788 struct rzv2h_cpg_priv *priv = ddiv->priv;
789 unsigned long flags = 0;
790 int value;
791 u32 val;
792 int ret;
793
794 value = divider_get_val(rate, parent_rate, divider->table,
795 divider->width, divider->flags);
796 if (value < 0)
797 return value;
798
799 spin_lock_irqsave(divider->lock, flags);
800
801 ret = rzv2h_cpg_wait_ddiv_clk_update_done(priv->base, ddiv->mon);
802 if (ret)
803 goto ddiv_timeout;
804
805 val = readl(divider->reg) | DDIV_DIVCTL_WEN(divider->shift);
806 val &= ~(clk_div_mask(divider->width) << divider->shift);
807 val |= (u32)value << divider->shift;
808 writel(val, divider->reg);
809
810 ret = rzv2h_cpg_wait_ddiv_clk_update_done(priv->base, ddiv->mon);
811
812 ddiv_timeout:
813 spin_unlock_irqrestore(divider->lock, flags);
814 return ret;
815 }
816
817 static const struct clk_ops rzv2h_ddiv_clk_divider_ops = {
818 .recalc_rate = rzv2h_ddiv_recalc_rate,
819 .determine_rate = rzv2h_ddiv_determine_rate,
820 .set_rate = rzv2h_ddiv_set_rate,
821 };
822
823 static struct clk * __init
rzv2h_cpg_ddiv_clk_register(const struct cpg_core_clk * core,struct rzv2h_cpg_priv * priv)824 rzv2h_cpg_ddiv_clk_register(const struct cpg_core_clk *core,
825 struct rzv2h_cpg_priv *priv)
826 {
827 struct ddiv cfg_ddiv = core->cfg.ddiv;
828 struct clk_init_data init = {};
829 struct device *dev = priv->dev;
830 u8 shift = cfg_ddiv.shift;
831 u8 width = cfg_ddiv.width;
832 const struct clk *parent;
833 const char *parent_name;
834 struct clk_divider *div;
835 struct ddiv_clk *ddiv;
836 int ret;
837
838 parent = priv->clks[core->parent];
839 if (IS_ERR(parent))
840 return ERR_CAST(parent);
841
842 parent_name = __clk_get_name(parent);
843
844 if ((shift + width) > 16)
845 return ERR_PTR(-EINVAL);
846
847 ddiv = devm_kzalloc(priv->dev, sizeof(*ddiv), GFP_KERNEL);
848 if (!ddiv)
849 return ERR_PTR(-ENOMEM);
850
851 init.name = core->name;
852 if (cfg_ddiv.no_rmw)
853 init.ops = &clk_divider_ops;
854 else
855 init.ops = &rzv2h_ddiv_clk_divider_ops;
856 init.parent_names = &parent_name;
857 init.num_parents = 1;
858 init.flags = CLK_SET_RATE_PARENT;
859
860 ddiv->priv = priv;
861 ddiv->mon = cfg_ddiv.monbit;
862 div = &ddiv->div;
863 div->reg = priv->base + cfg_ddiv.offset;
864 div->shift = shift;
865 div->width = width;
866 div->flags = core->flag;
867 div->lock = &priv->rmw_lock;
868 div->hw.init = &init;
869 div->table = core->dtable;
870
871 ret = devm_clk_hw_register(dev, &div->hw);
872 if (ret)
873 return ERR_PTR(ret);
874
875 return div->hw.clk;
876 }
877
878 static struct clk * __init
rzv2h_cpg_mux_clk_register(const struct cpg_core_clk * core,struct rzv2h_cpg_priv * priv)879 rzv2h_cpg_mux_clk_register(const struct cpg_core_clk *core,
880 struct rzv2h_cpg_priv *priv)
881 {
882 struct smuxed mux = core->cfg.smux;
883 const struct clk_hw *clk_hw;
884
885 clk_hw = devm_clk_hw_register_mux(priv->dev, core->name,
886 core->parent_names, core->num_parents,
887 core->flag, priv->base + mux.offset,
888 mux.shift, mux.width,
889 core->mux_flags, &priv->rmw_lock);
890 if (IS_ERR(clk_hw))
891 return ERR_CAST(clk_hw);
892
893 return clk_hw->clk;
894 }
895
896 static int
rzv2h_clk_ff_mod_status_is_enabled(struct clk_hw * hw)897 rzv2h_clk_ff_mod_status_is_enabled(struct clk_hw *hw)
898 {
899 struct rzv2h_ff_mod_status_clk *fix = to_rzv2h_ff_mod_status_clk(hw);
900 struct rzv2h_cpg_priv *priv = fix->priv;
901 u32 offset = GET_CLK_MON_OFFSET(fix->conf.mon_index);
902 u32 bitmask = BIT(fix->conf.mon_bit);
903 u32 val;
904
905 val = readl(priv->base + offset);
906 return !!(val & bitmask);
907 }
908
909 static struct clk * __init
rzv2h_cpg_fixed_mod_status_clk_register(const struct cpg_core_clk * core,struct rzv2h_cpg_priv * priv)910 rzv2h_cpg_fixed_mod_status_clk_register(const struct cpg_core_clk *core,
911 struct rzv2h_cpg_priv *priv)
912 {
913 struct rzv2h_ff_mod_status_clk *clk_hw_data;
914 struct clk_init_data init = { };
915 struct clk_fixed_factor *fix;
916 const struct clk *parent;
917 const char *parent_name;
918 int ret;
919
920 WARN_DEBUG(core->parent >= priv->num_core_clks);
921 parent = priv->clks[core->parent];
922 if (IS_ERR(parent))
923 return ERR_CAST(parent);
924
925 parent_name = __clk_get_name(parent);
926 parent = priv->clks[core->parent];
927 if (IS_ERR(parent))
928 return ERR_CAST(parent);
929
930 clk_hw_data = devm_kzalloc(priv->dev, sizeof(*clk_hw_data), GFP_KERNEL);
931 if (!clk_hw_data)
932 return ERR_PTR(-ENOMEM);
933
934 clk_hw_data->priv = priv;
935 clk_hw_data->conf = core->cfg.fixed_mod;
936
937 init.name = core->name;
938 init.ops = priv->ff_mod_status_ops;
939 init.flags = CLK_SET_RATE_PARENT;
940 init.parent_names = &parent_name;
941 init.num_parents = 1;
942
943 fix = &clk_hw_data->fix;
944 fix->hw.init = &init;
945 fix->mult = core->mult;
946 fix->div = core->div;
947
948 ret = devm_clk_hw_register(priv->dev, &clk_hw_data->fix.hw);
949 if (ret)
950 return ERR_PTR(ret);
951
952 return clk_hw_data->fix.hw.clk;
953 }
954
955 static struct clk
rzv2h_cpg_clk_src_twocell_get(struct of_phandle_args * clkspec,void * data)956 *rzv2h_cpg_clk_src_twocell_get(struct of_phandle_args *clkspec,
957 void *data)
958 {
959 unsigned int clkidx = clkspec->args[1];
960 struct rzv2h_cpg_priv *priv = data;
961 struct device *dev = priv->dev;
962 const char *type;
963 struct clk *clk;
964
965 switch (clkspec->args[0]) {
966 case CPG_CORE:
967 type = "core";
968 if (clkidx > priv->last_dt_core_clk) {
969 dev_err(dev, "Invalid %s clock index %u\n", type, clkidx);
970 return ERR_PTR(-EINVAL);
971 }
972 clk = priv->clks[clkidx];
973 break;
974
975 case CPG_MOD:
976 type = "module";
977 if (clkidx >= priv->num_mod_clks) {
978 dev_err(dev, "Invalid %s clock index %u\n", type, clkidx);
979 return ERR_PTR(-EINVAL);
980 }
981 clk = priv->clks[priv->num_core_clks + clkidx];
982 break;
983
984 default:
985 dev_err(dev, "Invalid CPG clock type %u\n", clkspec->args[0]);
986 return ERR_PTR(-EINVAL);
987 }
988
989 if (IS_ERR(clk))
990 dev_err(dev, "Cannot get %s clock %u: %ld\n", type, clkidx,
991 PTR_ERR(clk));
992 else
993 dev_dbg(dev, "clock (%u, %u) is %pC at %lu Hz\n",
994 clkspec->args[0], clkspec->args[1], clk,
995 clk_get_rate(clk));
996 return clk;
997 }
998
999 static void __init
rzv2h_cpg_register_core_clk(const struct cpg_core_clk * core,struct rzv2h_cpg_priv * priv)1000 rzv2h_cpg_register_core_clk(const struct cpg_core_clk *core,
1001 struct rzv2h_cpg_priv *priv)
1002 {
1003 struct clk *clk = ERR_PTR(-EOPNOTSUPP), *parent;
1004 unsigned int id = core->id, div = core->div;
1005 struct device *dev = priv->dev;
1006 const char *parent_name;
1007 struct clk_hw *clk_hw;
1008
1009 WARN_DEBUG(id >= priv->num_core_clks);
1010 WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT);
1011
1012 switch (core->type) {
1013 case CLK_TYPE_IN:
1014 clk = of_clk_get_by_name(priv->dev->of_node, core->name);
1015 break;
1016 case CLK_TYPE_FF:
1017 WARN_DEBUG(core->parent >= priv->num_core_clks);
1018 parent = priv->clks[core->parent];
1019 if (IS_ERR(parent)) {
1020 clk = parent;
1021 goto fail;
1022 }
1023
1024 parent_name = __clk_get_name(parent);
1025 clk_hw = devm_clk_hw_register_fixed_factor(dev, core->name,
1026 parent_name, CLK_SET_RATE_PARENT,
1027 core->mult, div);
1028 if (IS_ERR(clk_hw))
1029 clk = ERR_CAST(clk_hw);
1030 else
1031 clk = clk_hw->clk;
1032 break;
1033 case CLK_TYPE_FF_MOD_STATUS:
1034 if (!priv->ff_mod_status_ops) {
1035 priv->ff_mod_status_ops =
1036 devm_kzalloc(dev, sizeof(*priv->ff_mod_status_ops), GFP_KERNEL);
1037 if (!priv->ff_mod_status_ops) {
1038 clk = ERR_PTR(-ENOMEM);
1039 goto fail;
1040 }
1041 memcpy(priv->ff_mod_status_ops, &clk_fixed_factor_ops,
1042 sizeof(const struct clk_ops));
1043 priv->ff_mod_status_ops->is_enabled = rzv2h_clk_ff_mod_status_is_enabled;
1044 }
1045 clk = rzv2h_cpg_fixed_mod_status_clk_register(core, priv);
1046 break;
1047 case CLK_TYPE_PLL:
1048 clk = rzv2h_cpg_pll_clk_register(core, priv, &rzv2h_cpg_pll_ops);
1049 break;
1050 case CLK_TYPE_DDIV:
1051 clk = rzv2h_cpg_ddiv_clk_register(core, priv);
1052 break;
1053 case CLK_TYPE_SMUX:
1054 clk = rzv2h_cpg_mux_clk_register(core, priv);
1055 break;
1056 case CLK_TYPE_PLLDSI:
1057 clk = rzv2h_cpg_pll_clk_register(core, priv, &rzv2h_cpg_plldsi_ops);
1058 break;
1059 case CLK_TYPE_PLLDSI_DIV:
1060 clk = rzv2h_cpg_plldsi_div_clk_register(core, priv);
1061 break;
1062 case CLK_TYPE_PLLDSI_SMUX:
1063 clk = rzv2h_cpg_plldsi_smux_clk_register(core, priv);
1064 break;
1065 default:
1066 goto fail;
1067 }
1068
1069 if (IS_ERR(clk))
1070 goto fail;
1071
1072 dev_dbg(dev, "Core clock %pC at %lu Hz\n", clk, clk_get_rate(clk));
1073 priv->clks[id] = clk;
1074 return;
1075
1076 fail:
1077 dev_err(dev, "Failed to register core clock %s: %ld\n",
1078 core->name, PTR_ERR(clk));
1079 }
1080
rzv2h_mod_clock_mstop_enable(struct rzv2h_cpg_priv * priv,u32 mstop_data)1081 static void rzv2h_mod_clock_mstop_enable(struct rzv2h_cpg_priv *priv,
1082 u32 mstop_data)
1083 {
1084 unsigned long mstop_mask = FIELD_GET(BUS_MSTOP_BITS_MASK, mstop_data);
1085 u16 mstop_index = FIELD_GET(BUS_MSTOP_IDX_MASK, mstop_data);
1086 atomic_t *mstop = &priv->mstop_count[mstop_index * 16];
1087 unsigned long flags;
1088 unsigned int i;
1089 u32 val = 0;
1090
1091 spin_lock_irqsave(&priv->rmw_lock, flags);
1092 for_each_set_bit(i, &mstop_mask, 16) {
1093 if (!atomic_read(&mstop[i]))
1094 val |= BIT(i) << 16;
1095 atomic_inc(&mstop[i]);
1096 }
1097 if (val)
1098 writel(val, priv->base + CPG_BUS_MSTOP(mstop_index));
1099 spin_unlock_irqrestore(&priv->rmw_lock, flags);
1100 }
1101
rzv2h_mod_clock_mstop_disable(struct rzv2h_cpg_priv * priv,u32 mstop_data)1102 static void rzv2h_mod_clock_mstop_disable(struct rzv2h_cpg_priv *priv,
1103 u32 mstop_data)
1104 {
1105 unsigned long mstop_mask = FIELD_GET(BUS_MSTOP_BITS_MASK, mstop_data);
1106 u16 mstop_index = FIELD_GET(BUS_MSTOP_IDX_MASK, mstop_data);
1107 atomic_t *mstop = &priv->mstop_count[mstop_index * 16];
1108 unsigned long flags;
1109 unsigned int i;
1110 u32 val = 0;
1111
1112 spin_lock_irqsave(&priv->rmw_lock, flags);
1113 for_each_set_bit(i, &mstop_mask, 16) {
1114 if (!atomic_read(&mstop[i]) ||
1115 atomic_dec_and_test(&mstop[i]))
1116 val |= BIT(i) << 16 | BIT(i);
1117 }
1118 if (val)
1119 writel(val, priv->base + CPG_BUS_MSTOP(mstop_index));
1120 spin_unlock_irqrestore(&priv->rmw_lock, flags);
1121 }
1122
rzv2h_parent_clk_mux_to_index(struct clk_hw * hw)1123 static int rzv2h_parent_clk_mux_to_index(struct clk_hw *hw)
1124 {
1125 struct clk_hw *parent_hw;
1126 struct clk *parent_clk;
1127 struct clk_mux *mux;
1128 u32 val;
1129
1130 /* This will always succeed, so no need to check for IS_ERR() */
1131 parent_clk = clk_get_parent(hw->clk);
1132
1133 parent_hw = __clk_get_hw(parent_clk);
1134 mux = to_clk_mux(parent_hw);
1135
1136 val = readl(mux->reg) >> mux->shift;
1137 val &= mux->mask;
1138 return clk_mux_val_to_index(parent_hw, mux->table, 0, val);
1139 }
1140
rzv2h_mod_clock_is_enabled(struct clk_hw * hw)1141 static int rzv2h_mod_clock_is_enabled(struct clk_hw *hw)
1142 {
1143 struct mod_clock *clock = to_mod_clock(hw);
1144 struct rzv2h_cpg_priv *priv = clock->priv;
1145 int mon_index = clock->mon_index;
1146 u32 bitmask;
1147 u32 offset;
1148
1149 if (clock->ext_clk_mux_index >= 0 &&
1150 rzv2h_parent_clk_mux_to_index(hw) == clock->ext_clk_mux_index)
1151 mon_index = -1;
1152
1153 if (mon_index >= 0) {
1154 offset = GET_CLK_MON_OFFSET(mon_index);
1155 bitmask = BIT(clock->mon_bit);
1156
1157 if (!(readl(priv->base + offset) & bitmask))
1158 return 0;
1159 }
1160
1161 offset = GET_CLK_ON_OFFSET(clock->on_index);
1162 bitmask = BIT(clock->on_bit);
1163
1164 return readl(priv->base + offset) & bitmask;
1165 }
1166
rzv2h_mod_clock_endisable(struct clk_hw * hw,bool enable)1167 static int rzv2h_mod_clock_endisable(struct clk_hw *hw, bool enable)
1168 {
1169 bool enabled = rzv2h_mod_clock_is_enabled(hw);
1170 struct mod_clock *clock = to_mod_clock(hw);
1171 unsigned int reg = GET_CLK_ON_OFFSET(clock->on_index);
1172 struct rzv2h_cpg_priv *priv = clock->priv;
1173 u32 bitmask = BIT(clock->on_bit);
1174 struct device *dev = priv->dev;
1175 u32 value;
1176 int error;
1177
1178 dev_dbg(dev, "CLK_ON 0x%x/%pC %s\n", reg, hw->clk,
1179 str_on_off(enable));
1180
1181 if (enabled == enable)
1182 return 0;
1183
1184 value = bitmask << 16;
1185 if (enable) {
1186 value |= bitmask;
1187 writel(value, priv->base + reg);
1188 if (clock->mstop_data != BUS_MSTOP_NONE)
1189 rzv2h_mod_clock_mstop_enable(priv, clock->mstop_data);
1190 } else {
1191 if (clock->mstop_data != BUS_MSTOP_NONE)
1192 rzv2h_mod_clock_mstop_disable(priv, clock->mstop_data);
1193 writel(value, priv->base + reg);
1194 }
1195
1196 if (!enable || clock->mon_index < 0)
1197 return 0;
1198
1199 reg = GET_CLK_MON_OFFSET(clock->mon_index);
1200 bitmask = BIT(clock->mon_bit);
1201 error = readl_poll_timeout_atomic(priv->base + reg, value,
1202 value & bitmask, 0, 10);
1203 if (error)
1204 dev_err(dev, "Failed to enable CLK_ON 0x%x/%pC\n",
1205 GET_CLK_ON_OFFSET(clock->on_index), hw->clk);
1206
1207 return error;
1208 }
1209
rzv2h_mod_clock_enable(struct clk_hw * hw)1210 static int rzv2h_mod_clock_enable(struct clk_hw *hw)
1211 {
1212 return rzv2h_mod_clock_endisable(hw, true);
1213 }
1214
rzv2h_mod_clock_disable(struct clk_hw * hw)1215 static void rzv2h_mod_clock_disable(struct clk_hw *hw)
1216 {
1217 rzv2h_mod_clock_endisable(hw, false);
1218 }
1219
1220 static const struct clk_ops rzv2h_mod_clock_ops = {
1221 .enable = rzv2h_mod_clock_enable,
1222 .disable = rzv2h_mod_clock_disable,
1223 .is_enabled = rzv2h_mod_clock_is_enabled,
1224 };
1225
1226 static void __init
rzv2h_cpg_register_mod_clk(const struct rzv2h_mod_clk * mod,struct rzv2h_cpg_priv * priv)1227 rzv2h_cpg_register_mod_clk(const struct rzv2h_mod_clk *mod,
1228 struct rzv2h_cpg_priv *priv)
1229 {
1230 struct mod_clock *clock = NULL;
1231 struct device *dev = priv->dev;
1232 struct clk_init_data init;
1233 struct clk *parent, *clk;
1234 const char *parent_name;
1235 unsigned int id;
1236 int ret;
1237
1238 id = GET_MOD_CLK_ID(priv->num_core_clks, mod->on_index, mod->on_bit);
1239 WARN_DEBUG(id >= priv->num_core_clks + priv->num_mod_clks);
1240 WARN_DEBUG(mod->parent >= priv->num_core_clks + priv->num_mod_clks);
1241 WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT);
1242
1243 parent = priv->clks[mod->parent];
1244 if (IS_ERR(parent)) {
1245 clk = parent;
1246 goto fail;
1247 }
1248
1249 clock = devm_kzalloc(dev, sizeof(*clock), GFP_KERNEL);
1250 if (!clock) {
1251 clk = ERR_PTR(-ENOMEM);
1252 goto fail;
1253 }
1254
1255 init.name = mod->name;
1256 init.ops = &rzv2h_mod_clock_ops;
1257 init.flags = CLK_SET_RATE_PARENT;
1258 if (mod->critical)
1259 init.flags |= CLK_IS_CRITICAL;
1260
1261 parent_name = __clk_get_name(parent);
1262 init.parent_names = &parent_name;
1263 init.num_parents = 1;
1264
1265 clock->on_index = mod->on_index;
1266 clock->on_bit = mod->on_bit;
1267 clock->mon_index = mod->mon_index;
1268 clock->mon_bit = mod->mon_bit;
1269 clock->no_pm = mod->no_pm;
1270 clock->ext_clk_mux_index = mod->ext_clk_mux_index;
1271 clock->priv = priv;
1272 clock->hw.init = &init;
1273 clock->mstop_data = mod->mstop_data;
1274
1275 ret = devm_clk_hw_register(dev, &clock->hw);
1276 if (ret) {
1277 clk = ERR_PTR(ret);
1278 goto fail;
1279 }
1280
1281 priv->clks[id] = clock->hw.clk;
1282
1283 /*
1284 * Ensure the module clocks and MSTOP bits are synchronized when they are
1285 * turned ON by the bootloader. Enable MSTOP bits for module clocks that were
1286 * turned ON in an earlier boot stage.
1287 */
1288 if (clock->mstop_data != BUS_MSTOP_NONE &&
1289 !mod->critical && rzv2h_mod_clock_is_enabled(&clock->hw)) {
1290 rzv2h_mod_clock_mstop_enable(priv, clock->mstop_data);
1291 } else if (clock->mstop_data != BUS_MSTOP_NONE && mod->critical) {
1292 unsigned long mstop_mask = FIELD_GET(BUS_MSTOP_BITS_MASK, clock->mstop_data);
1293 u16 mstop_index = FIELD_GET(BUS_MSTOP_IDX_MASK, clock->mstop_data);
1294 atomic_t *mstop = &priv->mstop_count[mstop_index * 16];
1295 unsigned long flags;
1296 unsigned int i;
1297 u32 val = 0;
1298
1299 /*
1300 * Critical clocks are turned ON immediately upon registration, and the
1301 * MSTOP counter is updated through the rzv2h_mod_clock_enable() path.
1302 * However, if the critical clocks were already turned ON by the initial
1303 * bootloader, synchronize the atomic counter here and clear the MSTOP bit.
1304 */
1305 spin_lock_irqsave(&priv->rmw_lock, flags);
1306 for_each_set_bit(i, &mstop_mask, 16) {
1307 if (atomic_read(&mstop[i]))
1308 continue;
1309 val |= BIT(i) << 16;
1310 atomic_inc(&mstop[i]);
1311 }
1312 if (val)
1313 writel(val, priv->base + CPG_BUS_MSTOP(mstop_index));
1314 spin_unlock_irqrestore(&priv->rmw_lock, flags);
1315 }
1316
1317 return;
1318
1319 fail:
1320 dev_err(dev, "Failed to register module clock %s: %ld\n",
1321 mod->name, PTR_ERR(clk));
1322 }
1323
__rzv2h_cpg_assert(struct reset_controller_dev * rcdev,unsigned long id,bool assert)1324 static int __rzv2h_cpg_assert(struct reset_controller_dev *rcdev,
1325 unsigned long id, bool assert)
1326 {
1327 struct rzv2h_cpg_priv *priv = rcdev_to_priv(rcdev);
1328 unsigned int reg = GET_RST_OFFSET(priv->resets[id].reset_index);
1329 u32 mask = BIT(priv->resets[id].reset_bit);
1330 u8 monbit = priv->resets[id].mon_bit;
1331 u32 value = mask << 16;
1332 u32 mon;
1333 int ret;
1334
1335 dev_dbg(rcdev->dev, "%s id:%ld offset:0x%x\n",
1336 assert ? "assert" : "deassert", id, reg);
1337
1338 if (!assert)
1339 value |= mask;
1340 writel(value, priv->base + reg);
1341
1342 reg = GET_RST_MON_OFFSET(priv->resets[id].mon_index);
1343 mask = BIT(monbit);
1344
1345 ret = readl_poll_timeout_atomic(priv->base + reg, mon,
1346 assert == !!(mon & mask), 10, 200);
1347 if (ret) {
1348 value ^= mask;
1349 writel(value, priv->base + GET_RST_OFFSET(priv->resets[id].reset_index));
1350 }
1351
1352 return ret;
1353 }
1354
rzv2h_cpg_assert(struct reset_controller_dev * rcdev,unsigned long id)1355 static int rzv2h_cpg_assert(struct reset_controller_dev *rcdev,
1356 unsigned long id)
1357 {
1358 return __rzv2h_cpg_assert(rcdev, id, true);
1359 }
1360
rzv2h_cpg_deassert(struct reset_controller_dev * rcdev,unsigned long id)1361 static int rzv2h_cpg_deassert(struct reset_controller_dev *rcdev,
1362 unsigned long id)
1363 {
1364 return __rzv2h_cpg_assert(rcdev, id, false);
1365 }
1366
rzv2h_cpg_reset(struct reset_controller_dev * rcdev,unsigned long id)1367 static int rzv2h_cpg_reset(struct reset_controller_dev *rcdev,
1368 unsigned long id)
1369 {
1370 int ret;
1371
1372 ret = rzv2h_cpg_assert(rcdev, id);
1373 if (ret)
1374 return ret;
1375
1376 return rzv2h_cpg_deassert(rcdev, id);
1377 }
1378
rzv2h_cpg_status(struct reset_controller_dev * rcdev,unsigned long id)1379 static int rzv2h_cpg_status(struct reset_controller_dev *rcdev,
1380 unsigned long id)
1381 {
1382 struct rzv2h_cpg_priv *priv = rcdev_to_priv(rcdev);
1383 unsigned int reg = GET_RST_MON_OFFSET(priv->resets[id].mon_index);
1384 u8 monbit = priv->resets[id].mon_bit;
1385
1386 return !!(readl(priv->base + reg) & BIT(monbit));
1387 }
1388
1389 static const struct reset_control_ops rzv2h_cpg_reset_ops = {
1390 .reset = rzv2h_cpg_reset,
1391 .assert = rzv2h_cpg_assert,
1392 .deassert = rzv2h_cpg_deassert,
1393 .status = rzv2h_cpg_status,
1394 };
1395
rzv2h_cpg_reset_xlate(struct reset_controller_dev * rcdev,const struct of_phandle_args * reset_spec)1396 static int rzv2h_cpg_reset_xlate(struct reset_controller_dev *rcdev,
1397 const struct of_phandle_args *reset_spec)
1398 {
1399 struct rzv2h_cpg_priv *priv = rcdev_to_priv(rcdev);
1400 unsigned int id = reset_spec->args[0];
1401 u8 rst_index = id / 16;
1402 u8 rst_bit = id % 16;
1403 unsigned int i;
1404
1405 for (i = 0; i < rcdev->nr_resets; i++) {
1406 if (rst_index == priv->resets[i].reset_index &&
1407 rst_bit == priv->resets[i].reset_bit)
1408 return i;
1409 }
1410
1411 return -EINVAL;
1412 }
1413
rzv2h_cpg_reset_controller_register(struct rzv2h_cpg_priv * priv)1414 static int rzv2h_cpg_reset_controller_register(struct rzv2h_cpg_priv *priv)
1415 {
1416 priv->rcdev.ops = &rzv2h_cpg_reset_ops;
1417 priv->rcdev.of_node = priv->dev->of_node;
1418 priv->rcdev.dev = priv->dev;
1419 priv->rcdev.of_reset_n_cells = 1;
1420 priv->rcdev.of_xlate = rzv2h_cpg_reset_xlate;
1421 priv->rcdev.nr_resets = priv->num_resets;
1422
1423 return devm_reset_controller_register(priv->dev, &priv->rcdev);
1424 }
1425
1426 /**
1427 * struct rzv2h_cpg_pd - RZ/V2H power domain data structure
1428 * @priv: pointer to CPG private data structure
1429 * @genpd: generic PM domain
1430 */
1431 struct rzv2h_cpg_pd {
1432 struct rzv2h_cpg_priv *priv;
1433 struct generic_pm_domain genpd;
1434 };
1435
rzv2h_cpg_is_pm_clk(struct rzv2h_cpg_pd * pd,const struct of_phandle_args * clkspec)1436 static bool rzv2h_cpg_is_pm_clk(struct rzv2h_cpg_pd *pd,
1437 const struct of_phandle_args *clkspec)
1438 {
1439 if (clkspec->np != pd->genpd.dev.of_node || clkspec->args_count != 2)
1440 return false;
1441
1442 switch (clkspec->args[0]) {
1443 case CPG_MOD: {
1444 struct rzv2h_cpg_priv *priv = pd->priv;
1445 unsigned int id = clkspec->args[1];
1446 struct mod_clock *clock;
1447
1448 if (id >= priv->num_mod_clks)
1449 return false;
1450
1451 if (priv->clks[priv->num_core_clks + id] == ERR_PTR(-ENOENT))
1452 return false;
1453
1454 clock = to_mod_clock(__clk_get_hw(priv->clks[priv->num_core_clks + id]));
1455
1456 return !clock->no_pm;
1457 }
1458
1459 case CPG_CORE:
1460 default:
1461 return false;
1462 }
1463 }
1464
rzv2h_cpg_attach_dev(struct generic_pm_domain * domain,struct device * dev)1465 static int rzv2h_cpg_attach_dev(struct generic_pm_domain *domain, struct device *dev)
1466 {
1467 struct rzv2h_cpg_pd *pd = container_of(domain, struct rzv2h_cpg_pd, genpd);
1468 struct device_node *np = dev->of_node;
1469 struct of_phandle_args clkspec;
1470 bool once = true;
1471 struct clk *clk;
1472 unsigned int i;
1473 int error;
1474
1475 for (i = 0; !of_parse_phandle_with_args(np, "clocks", "#clock-cells", i, &clkspec); i++) {
1476 if (!rzv2h_cpg_is_pm_clk(pd, &clkspec)) {
1477 of_node_put(clkspec.np);
1478 continue;
1479 }
1480
1481 if (once) {
1482 once = false;
1483 error = pm_clk_create(dev);
1484 if (error) {
1485 of_node_put(clkspec.np);
1486 goto err;
1487 }
1488 }
1489 clk = of_clk_get_from_provider(&clkspec);
1490 of_node_put(clkspec.np);
1491 if (IS_ERR(clk)) {
1492 error = PTR_ERR(clk);
1493 goto fail_destroy;
1494 }
1495
1496 error = pm_clk_add_clk(dev, clk);
1497 if (error) {
1498 dev_err(dev, "pm_clk_add_clk failed %d\n",
1499 error);
1500 goto fail_put;
1501 }
1502 }
1503
1504 return 0;
1505
1506 fail_put:
1507 clk_put(clk);
1508
1509 fail_destroy:
1510 pm_clk_destroy(dev);
1511 err:
1512 return error;
1513 }
1514
rzv2h_cpg_detach_dev(struct generic_pm_domain * unused,struct device * dev)1515 static void rzv2h_cpg_detach_dev(struct generic_pm_domain *unused, struct device *dev)
1516 {
1517 if (!pm_clk_no_clocks(dev))
1518 pm_clk_destroy(dev);
1519 }
1520
rzv2h_cpg_genpd_remove_simple(void * data)1521 static void rzv2h_cpg_genpd_remove_simple(void *data)
1522 {
1523 pm_genpd_remove(data);
1524 }
1525
rzv2h_cpg_add_pm_domains(struct rzv2h_cpg_priv * priv)1526 static int __init rzv2h_cpg_add_pm_domains(struct rzv2h_cpg_priv *priv)
1527 {
1528 struct device *dev = priv->dev;
1529 struct device_node *np = dev->of_node;
1530 struct rzv2h_cpg_pd *pd;
1531 int ret;
1532
1533 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
1534 if (!pd)
1535 return -ENOMEM;
1536
1537 pd->genpd.name = np->name;
1538 pd->priv = priv;
1539 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON | GENPD_FLAG_PM_CLK | GENPD_FLAG_ACTIVE_WAKEUP;
1540 pd->genpd.attach_dev = rzv2h_cpg_attach_dev;
1541 pd->genpd.detach_dev = rzv2h_cpg_detach_dev;
1542 ret = pm_genpd_init(&pd->genpd, &pm_domain_always_on_gov, false);
1543 if (ret)
1544 return ret;
1545
1546 ret = devm_add_action_or_reset(dev, rzv2h_cpg_genpd_remove_simple, &pd->genpd);
1547 if (ret)
1548 return ret;
1549
1550 return of_genpd_add_provider_simple(np, &pd->genpd);
1551 }
1552
rzv2h_cpg_del_clk_provider(void * data)1553 static void rzv2h_cpg_del_clk_provider(void *data)
1554 {
1555 of_clk_del_provider(data);
1556 }
1557
rzv2h_cpg_probe(struct platform_device * pdev)1558 static int __init rzv2h_cpg_probe(struct platform_device *pdev)
1559 {
1560 struct device *dev = &pdev->dev;
1561 struct device_node *np = dev->of_node;
1562 const struct rzv2h_cpg_info *info;
1563 struct rzv2h_cpg_priv *priv;
1564 unsigned int nclks, i;
1565 struct clk **clks;
1566 int error;
1567
1568 info = of_device_get_match_data(dev);
1569
1570 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1571 if (!priv)
1572 return -ENOMEM;
1573
1574 spin_lock_init(&priv->rmw_lock);
1575
1576 priv->dev = dev;
1577
1578 priv->base = devm_platform_ioremap_resource(pdev, 0);
1579 if (IS_ERR(priv->base))
1580 return PTR_ERR(priv->base);
1581
1582 nclks = info->num_total_core_clks + info->num_hw_mod_clks;
1583 clks = devm_kmalloc_array(dev, nclks, sizeof(*clks), GFP_KERNEL);
1584 if (!clks)
1585 return -ENOMEM;
1586
1587 priv->mstop_count = devm_kcalloc(dev, info->num_mstop_bits,
1588 sizeof(*priv->mstop_count), GFP_KERNEL);
1589 if (!priv->mstop_count)
1590 return -ENOMEM;
1591
1592 /* Adjust for CPG_BUS_m_MSTOP starting from m = 1 */
1593 priv->mstop_count -= 16;
1594
1595 priv->resets = devm_kmemdup_array(dev, info->resets, info->num_resets,
1596 sizeof(*info->resets), GFP_KERNEL);
1597 if (!priv->resets)
1598 return -ENOMEM;
1599
1600 dev_set_drvdata(dev, priv);
1601 priv->clks = clks;
1602 priv->num_core_clks = info->num_total_core_clks;
1603 priv->num_mod_clks = info->num_hw_mod_clks;
1604 priv->last_dt_core_clk = info->last_dt_core_clk;
1605 priv->num_resets = info->num_resets;
1606
1607 for (i = 0; i < nclks; i++)
1608 clks[i] = ERR_PTR(-ENOENT);
1609
1610 for (i = 0; i < info->num_core_clks; i++)
1611 rzv2h_cpg_register_core_clk(&info->core_clks[i], priv);
1612
1613 for (i = 0; i < info->num_mod_clks; i++)
1614 rzv2h_cpg_register_mod_clk(&info->mod_clks[i], priv);
1615
1616 error = of_clk_add_provider(np, rzv2h_cpg_clk_src_twocell_get, priv);
1617 if (error)
1618 return error;
1619
1620 error = devm_add_action_or_reset(dev, rzv2h_cpg_del_clk_provider, np);
1621 if (error)
1622 return error;
1623
1624 error = rzv2h_cpg_add_pm_domains(priv);
1625 if (error)
1626 return error;
1627
1628 error = rzv2h_cpg_reset_controller_register(priv);
1629 if (error)
1630 return error;
1631
1632 return 0;
1633 }
1634
1635 static const struct of_device_id rzv2h_cpg_match[] = {
1636 #ifdef CONFIG_CLK_R9A09G047
1637 {
1638 .compatible = "renesas,r9a09g047-cpg",
1639 .data = &r9a09g047_cpg_info,
1640 },
1641 #endif
1642 #ifdef CONFIG_CLK_R9A09G056
1643 {
1644 .compatible = "renesas,r9a09g056-cpg",
1645 .data = &r9a09g056_cpg_info,
1646 },
1647 #endif
1648 #ifdef CONFIG_CLK_R9A09G057
1649 {
1650 .compatible = "renesas,r9a09g057-cpg",
1651 .data = &r9a09g057_cpg_info,
1652 },
1653 #endif
1654 { /* sentinel */ }
1655 };
1656
1657 static struct platform_driver rzv2h_cpg_driver = {
1658 .driver = {
1659 .name = "rzv2h-cpg",
1660 .of_match_table = rzv2h_cpg_match,
1661 },
1662 };
1663
rzv2h_cpg_init(void)1664 static int __init rzv2h_cpg_init(void)
1665 {
1666 return platform_driver_probe(&rzv2h_cpg_driver, rzv2h_cpg_probe);
1667 }
1668
1669 subsys_initcall(rzv2h_cpg_init);
1670
1671 MODULE_DESCRIPTION("Renesas RZ/V2H CPG Driver");
1672