1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Copyright (C) 2019 Microchip Technology Inc.
4  *
5  */
6 
7 #include <linux/bitfield.h>
8 #include <linux/clk-provider.h>
9 #include <linux/clkdev.h>
10 #include <linux/clk/at91_pmc.h>
11 #include <linux/of.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/regmap.h>
14 
15 #include "pmc.h"
16 
17 #define	PMC_PLL_CTRL0_DIV_MSK	GENMASK(7, 0)
18 #define	PMC_PLL_CTRL1_MUL_MSK	GENMASK(31, 24)
19 #define	PMC_PLL_CTRL1_FRACR_MSK	GENMASK(21, 0)
20 
21 #define PLL_DIV_MAX		(FIELD_GET(PMC_PLL_CTRL0_DIV_MSK, UINT_MAX) + 1)
22 #define UPLL_DIV		2
23 #define PLL_MUL_MAX		(FIELD_GET(PMC_PLL_CTRL1_MUL_MSK, UINT_MAX) + 1)
24 
25 #define FCORE_MIN		(600000000)
26 #define FCORE_MAX		(1200000000)
27 
28 #define PLL_MAX_ID		7
29 
30 struct sam9x60_pll_core {
31 	struct regmap *regmap;
32 	spinlock_t *lock;
33 	const struct clk_pll_characteristics *characteristics;
34 	const struct clk_pll_layout *layout;
35 	struct clk_hw hw;
36 	u8 id;
37 };
38 
39 struct sam9x60_frac {
40 	struct sam9x60_pll_core core;
41 	u32 frac;
42 	u16 mul;
43 };
44 
45 struct sam9x60_div {
46 	struct sam9x60_pll_core core;
47 	u8 div;
48 };
49 
50 #define to_sam9x60_pll_core(hw)	container_of(hw, struct sam9x60_pll_core, hw)
51 #define to_sam9x60_frac(core)	container_of(core, struct sam9x60_frac, core)
52 #define to_sam9x60_div(core)	container_of(core, struct sam9x60_div, core)
53 
54 static inline bool sam9x60_pll_ready(struct regmap *regmap, int id)
55 {
56 	unsigned int status;
57 
58 	regmap_read(regmap, AT91_PMC_PLL_ISR0, &status);
59 
60 	return !!(status & BIT(id));
61 }
62 
63 static bool sam9x60_frac_pll_ready(struct regmap *regmap, u8 id)
64 {
65 	return sam9x60_pll_ready(regmap, id);
66 }
67 
68 static unsigned long sam9x60_frac_pll_recalc_rate(struct clk_hw *hw,
69 						  unsigned long parent_rate)
70 {
71 	struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
72 	struct sam9x60_frac *frac = to_sam9x60_frac(core);
73 
74 	return (parent_rate * (frac->mul + 1) +
75 		((u64)parent_rate * frac->frac >> 22));
76 }
77 
78 static int sam9x60_frac_pll_prepare(struct clk_hw *hw)
79 {
80 	struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
81 	struct sam9x60_frac *frac = to_sam9x60_frac(core);
82 	struct regmap *regmap = core->regmap;
83 	unsigned int val, cfrac, cmul;
84 	unsigned long flags;
85 
86 	spin_lock_irqsave(core->lock, flags);
87 
88 	regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
89 			   AT91_PMC_PLL_UPDT_ID_MSK, core->id);
90 	regmap_read(regmap, AT91_PMC_PLL_CTRL1, &val);
91 	cmul = (val & core->layout->mul_mask) >> core->layout->mul_shift;
92 	cfrac = (val & core->layout->frac_mask) >> core->layout->frac_shift;
93 
94 	if (sam9x60_frac_pll_ready(regmap, core->id) &&
95 	    (cmul == frac->mul && cfrac == frac->frac))
96 		goto unlock;
97 
98 	/* Recommended value for PMC_PLL_ACR */
99 	if (core->characteristics->upll)
100 		val = AT91_PMC_PLL_ACR_DEFAULT_UPLL;
101 	else
102 		val = AT91_PMC_PLL_ACR_DEFAULT_PLLA;
103 	regmap_write(regmap, AT91_PMC_PLL_ACR, val);
104 
105 	regmap_write(regmap, AT91_PMC_PLL_CTRL1,
106 		     (frac->mul << core->layout->mul_shift) |
107 		     (frac->frac << core->layout->frac_shift));
108 
109 	if (core->characteristics->upll) {
110 		/* Enable the UTMI internal bandgap */
111 		val |= AT91_PMC_PLL_ACR_UTMIBG;
112 		regmap_write(regmap, AT91_PMC_PLL_ACR, val);
113 
114 		udelay(10);
115 
116 		/* Enable the UTMI internal regulator */
117 		val |= AT91_PMC_PLL_ACR_UTMIVR;
118 		regmap_write(regmap, AT91_PMC_PLL_ACR, val);
119 
120 		udelay(10);
121 	}
122 
123 	regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
124 			   AT91_PMC_PLL_UPDT_UPDATE | AT91_PMC_PLL_UPDT_ID_MSK,
125 			   AT91_PMC_PLL_UPDT_UPDATE | core->id);
126 
127 	regmap_update_bits(regmap, AT91_PMC_PLL_CTRL0,
128 			   AT91_PMC_PLL_CTRL0_ENLOCK | AT91_PMC_PLL_CTRL0_ENPLL,
129 			   AT91_PMC_PLL_CTRL0_ENLOCK | AT91_PMC_PLL_CTRL0_ENPLL);
130 
131 	regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
132 			   AT91_PMC_PLL_UPDT_UPDATE | AT91_PMC_PLL_UPDT_ID_MSK,
133 			   AT91_PMC_PLL_UPDT_UPDATE | core->id);
134 
135 	while (!sam9x60_pll_ready(regmap, core->id))
136 		cpu_relax();
137 
138 unlock:
139 	spin_unlock_irqrestore(core->lock, flags);
140 
141 	return 0;
142 }
143 
144 static void sam9x60_frac_pll_unprepare(struct clk_hw *hw)
145 {
146 	struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
147 	struct regmap *regmap = core->regmap;
148 	unsigned long flags;
149 
150 	spin_lock_irqsave(core->lock, flags);
151 
152 	regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
153 			   AT91_PMC_PLL_UPDT_ID_MSK, core->id);
154 
155 	regmap_update_bits(regmap, AT91_PMC_PLL_CTRL0, AT91_PMC_PLL_CTRL0_ENPLL, 0);
156 
157 	if (core->characteristics->upll)
158 		regmap_update_bits(regmap, AT91_PMC_PLL_ACR,
159 				   AT91_PMC_PLL_ACR_UTMIBG | AT91_PMC_PLL_ACR_UTMIVR, 0);
160 
161 	regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
162 			   AT91_PMC_PLL_UPDT_UPDATE | AT91_PMC_PLL_UPDT_ID_MSK,
163 			   AT91_PMC_PLL_UPDT_UPDATE | core->id);
164 
165 	spin_unlock_irqrestore(core->lock, flags);
166 }
167 
168 static int sam9x60_frac_pll_is_prepared(struct clk_hw *hw)
169 {
170 	struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
171 
172 	return sam9x60_pll_ready(core->regmap, core->id);
173 }
174 
175 static long sam9x60_frac_pll_compute_mul_frac(struct sam9x60_pll_core *core,
176 					      unsigned long rate,
177 					      unsigned long parent_rate,
178 					      bool update)
179 {
180 	struct sam9x60_frac *frac = to_sam9x60_frac(core);
181 	unsigned long tmprate, remainder;
182 	unsigned long nmul = 0;
183 	unsigned long nfrac = 0;
184 
185 	if (rate < FCORE_MIN || rate > FCORE_MAX)
186 		return -ERANGE;
187 
188 	/*
189 	 * Calculate the multiplier associated with the current
190 	 * divider that provide the closest rate to the requested one.
191 	 */
192 	nmul = mult_frac(rate, 1, parent_rate);
193 	tmprate = mult_frac(parent_rate, nmul, 1);
194 	remainder = rate - tmprate;
195 
196 	if (remainder) {
197 		nfrac = DIV_ROUND_CLOSEST_ULL((u64)remainder * (1 << 22),
198 					      parent_rate);
199 
200 		tmprate += DIV_ROUND_CLOSEST_ULL((u64)nfrac * parent_rate,
201 						 (1 << 22));
202 	}
203 
204 	/* Check if resulted rate is a valid.  */
205 	if (tmprate < FCORE_MIN || tmprate > FCORE_MAX)
206 		return -ERANGE;
207 
208 	if (update) {
209 		frac->mul = nmul - 1;
210 		frac->frac = nfrac;
211 	}
212 
213 	return tmprate;
214 }
215 
216 static long sam9x60_frac_pll_round_rate(struct clk_hw *hw, unsigned long rate,
217 					unsigned long *parent_rate)
218 {
219 	struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
220 
221 	return sam9x60_frac_pll_compute_mul_frac(core, rate, *parent_rate, false);
222 }
223 
224 static int sam9x60_frac_pll_set_rate(struct clk_hw *hw, unsigned long rate,
225 				     unsigned long parent_rate)
226 {
227 	struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
228 
229 	return sam9x60_frac_pll_compute_mul_frac(core, rate, parent_rate, true);
230 }
231 
232 static const struct clk_ops sam9x60_frac_pll_ops = {
233 	.prepare = sam9x60_frac_pll_prepare,
234 	.unprepare = sam9x60_frac_pll_unprepare,
235 	.is_prepared = sam9x60_frac_pll_is_prepared,
236 	.recalc_rate = sam9x60_frac_pll_recalc_rate,
237 	.round_rate = sam9x60_frac_pll_round_rate,
238 	.set_rate = sam9x60_frac_pll_set_rate,
239 };
240 
241 static int sam9x60_div_pll_prepare(struct clk_hw *hw)
242 {
243 	struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
244 	struct sam9x60_div *div = to_sam9x60_div(core);
245 	struct regmap *regmap = core->regmap;
246 	unsigned long flags;
247 	unsigned int val, cdiv;
248 
249 	spin_lock_irqsave(core->lock, flags);
250 	regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
251 			   AT91_PMC_PLL_UPDT_ID_MSK, core->id);
252 	regmap_read(regmap, AT91_PMC_PLL_CTRL0, &val);
253 	cdiv = (val & core->layout->div_mask) >> core->layout->div_shift;
254 
255 	/* Stop if enabled an nothing changed. */
256 	if (!!(val & core->layout->endiv_mask) && cdiv == div->div)
257 		goto unlock;
258 
259 	regmap_update_bits(regmap, AT91_PMC_PLL_CTRL0,
260 			   core->layout->div_mask | core->layout->endiv_mask,
261 			   (div->div << core->layout->div_shift) |
262 			   (1 << core->layout->endiv_shift));
263 
264 	regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
265 			   AT91_PMC_PLL_UPDT_UPDATE | AT91_PMC_PLL_UPDT_ID_MSK,
266 			   AT91_PMC_PLL_UPDT_UPDATE | core->id);
267 
268 	while (!sam9x60_pll_ready(regmap, core->id))
269 		cpu_relax();
270 
271 unlock:
272 	spin_unlock_irqrestore(core->lock, flags);
273 
274 	return 0;
275 }
276 
277 static void sam9x60_div_pll_unprepare(struct clk_hw *hw)
278 {
279 	struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
280 	struct regmap *regmap = core->regmap;
281 	unsigned long flags;
282 
283 	spin_lock_irqsave(core->lock, flags);
284 
285 	regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
286 			   AT91_PMC_PLL_UPDT_ID_MSK, core->id);
287 
288 	regmap_update_bits(regmap, AT91_PMC_PLL_CTRL0,
289 			   core->layout->endiv_mask, 0);
290 
291 	regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
292 			   AT91_PMC_PLL_UPDT_UPDATE | AT91_PMC_PLL_UPDT_ID_MSK,
293 			   AT91_PMC_PLL_UPDT_UPDATE | core->id);
294 
295 	spin_unlock_irqrestore(core->lock, flags);
296 }
297 
298 static int sam9x60_div_pll_is_prepared(struct clk_hw *hw)
299 {
300 	struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
301 	struct regmap *regmap = core->regmap;
302 	unsigned long flags;
303 	unsigned int val;
304 
305 	spin_lock_irqsave(core->lock, flags);
306 
307 	regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
308 			   AT91_PMC_PLL_UPDT_ID_MSK, core->id);
309 	regmap_read(regmap, AT91_PMC_PLL_CTRL0, &val);
310 
311 	spin_unlock_irqrestore(core->lock, flags);
312 
313 	return !!(val & core->layout->endiv_mask);
314 }
315 
316 static unsigned long sam9x60_div_pll_recalc_rate(struct clk_hw *hw,
317 						 unsigned long parent_rate)
318 {
319 	struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
320 	struct sam9x60_div *div = to_sam9x60_div(core);
321 
322 	return DIV_ROUND_CLOSEST_ULL(parent_rate, (div->div + 1));
323 }
324 
325 static long sam9x60_div_pll_compute_div(struct sam9x60_pll_core *core,
326 					unsigned long *parent_rate,
327 					unsigned long rate)
328 {
329 	const struct clk_pll_characteristics *characteristics =
330 							core->characteristics;
331 	struct clk_hw *parent = clk_hw_get_parent(&core->hw);
332 	unsigned long tmp_rate, tmp_parent_rate, tmp_diff;
333 	long best_diff = -1, best_rate = -EINVAL;
334 	u32 divid, best_div;
335 
336 	if (!rate)
337 		return 0;
338 
339 	if (rate < characteristics->output[0].min ||
340 	    rate > characteristics->output[0].max)
341 		return -ERANGE;
342 
343 	for (divid = 1; divid < core->layout->div_mask; divid++) {
344 		tmp_parent_rate = clk_hw_round_rate(parent, rate * divid);
345 		if (!tmp_parent_rate)
346 			continue;
347 
348 		tmp_rate = DIV_ROUND_CLOSEST_ULL(tmp_parent_rate, divid);
349 		tmp_diff = abs(rate - tmp_rate);
350 
351 		if (best_diff < 0 || best_diff > tmp_diff) {
352 			*parent_rate = tmp_parent_rate;
353 			best_rate = tmp_rate;
354 			best_diff = tmp_diff;
355 			best_div = divid;
356 		}
357 
358 		if (!best_diff)
359 			break;
360 	}
361 
362 	if (best_rate < characteristics->output[0].min ||
363 	    best_rate > characteristics->output[0].max)
364 		return -ERANGE;
365 
366 	return best_rate;
367 }
368 
369 static long sam9x60_div_pll_round_rate(struct clk_hw *hw, unsigned long rate,
370 				       unsigned long *parent_rate)
371 {
372 	struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
373 
374 	return sam9x60_div_pll_compute_div(core, parent_rate, rate);
375 }
376 
377 static int sam9x60_div_pll_set_rate(struct clk_hw *hw, unsigned long rate,
378 				    unsigned long parent_rate)
379 {
380 	struct sam9x60_pll_core *core = to_sam9x60_pll_core(hw);
381 	struct sam9x60_div *div = to_sam9x60_div(core);
382 
383 	div->div = DIV_ROUND_CLOSEST(parent_rate, rate) - 1;
384 
385 	return 0;
386 }
387 
388 static const struct clk_ops sam9x60_div_pll_ops = {
389 	.prepare = sam9x60_div_pll_prepare,
390 	.unprepare = sam9x60_div_pll_unprepare,
391 	.is_prepared = sam9x60_div_pll_is_prepared,
392 	.recalc_rate = sam9x60_div_pll_recalc_rate,
393 	.round_rate = sam9x60_div_pll_round_rate,
394 	.set_rate = sam9x60_div_pll_set_rate,
395 };
396 
397 struct clk_hw * __init
398 sam9x60_clk_register_frac_pll(struct regmap *regmap, spinlock_t *lock,
399 			      const char *name, const char *parent_name,
400 			      struct clk_hw *parent_hw, u8 id,
401 			      const struct clk_pll_characteristics *characteristics,
402 			      const struct clk_pll_layout *layout, bool critical)
403 {
404 	struct sam9x60_frac *frac;
405 	struct clk_hw *hw;
406 	struct clk_init_data init;
407 	unsigned long parent_rate, flags;
408 	unsigned int val;
409 	int ret;
410 
411 	if (id > PLL_MAX_ID || !lock || !parent_hw)
412 		return ERR_PTR(-EINVAL);
413 
414 	frac = kzalloc(sizeof(*frac), GFP_KERNEL);
415 	if (!frac)
416 		return ERR_PTR(-ENOMEM);
417 
418 	init.name = name;
419 	init.parent_names = &parent_name;
420 	init.num_parents = 1;
421 	init.ops = &sam9x60_frac_pll_ops;
422 	init.flags = CLK_SET_RATE_GATE;
423 	if (critical)
424 		init.flags |= CLK_IS_CRITICAL;
425 
426 	frac->core.id = id;
427 	frac->core.hw.init = &init;
428 	frac->core.characteristics = characteristics;
429 	frac->core.layout = layout;
430 	frac->core.regmap = regmap;
431 	frac->core.lock = lock;
432 
433 	spin_lock_irqsave(frac->core.lock, flags);
434 	if (sam9x60_pll_ready(regmap, id)) {
435 		regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
436 				   AT91_PMC_PLL_UPDT_ID_MSK, id);
437 		regmap_read(regmap, AT91_PMC_PLL_CTRL1, &val);
438 		frac->mul = FIELD_GET(PMC_PLL_CTRL1_MUL_MSK, val);
439 		frac->frac = FIELD_GET(PMC_PLL_CTRL1_FRACR_MSK, val);
440 	} else {
441 		/*
442 		 * This means the PLL is not setup by bootloaders. In this
443 		 * case we need to set the minimum rate for it. Otherwise
444 		 * a clock child of this PLL may be enabled before setting
445 		 * its rate leading to enabling this PLL with unsupported
446 		 * rate. This will lead to PLL not being locked at all.
447 		 */
448 		parent_rate = clk_hw_get_rate(parent_hw);
449 		if (!parent_rate) {
450 			hw = ERR_PTR(-EINVAL);
451 			goto free;
452 		}
453 
454 		ret = sam9x60_frac_pll_compute_mul_frac(&frac->core, FCORE_MIN,
455 							parent_rate, true);
456 		if (ret <= 0) {
457 			hw = ERR_PTR(ret);
458 			goto free;
459 		}
460 	}
461 	spin_unlock_irqrestore(frac->core.lock, flags);
462 
463 	hw = &frac->core.hw;
464 	ret = clk_hw_register(NULL, hw);
465 	if (ret) {
466 		kfree(frac);
467 		hw = ERR_PTR(ret);
468 	}
469 
470 	return hw;
471 
472 free:
473 	spin_unlock_irqrestore(frac->core.lock, flags);
474 	kfree(frac);
475 	return hw;
476 }
477 
478 struct clk_hw * __init
479 sam9x60_clk_register_div_pll(struct regmap *regmap, spinlock_t *lock,
480 			     const char *name, const char *parent_name, u8 id,
481 			     const struct clk_pll_characteristics *characteristics,
482 			     const struct clk_pll_layout *layout, bool critical)
483 {
484 	struct sam9x60_div *div;
485 	struct clk_hw *hw;
486 	struct clk_init_data init;
487 	unsigned long flags;
488 	unsigned int val;
489 	int ret;
490 
491 	if (id > PLL_MAX_ID || !lock)
492 		return ERR_PTR(-EINVAL);
493 
494 	div = kzalloc(sizeof(*div), GFP_KERNEL);
495 	if (!div)
496 		return ERR_PTR(-ENOMEM);
497 
498 	init.name = name;
499 	init.parent_names = &parent_name;
500 	init.num_parents = 1;
501 	init.ops = &sam9x60_div_pll_ops;
502 	init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
503 		     CLK_SET_RATE_PARENT;
504 	if (critical)
505 		init.flags |= CLK_IS_CRITICAL;
506 
507 	div->core.id = id;
508 	div->core.hw.init = &init;
509 	div->core.characteristics = characteristics;
510 	div->core.layout = layout;
511 	div->core.regmap = regmap;
512 	div->core.lock = lock;
513 
514 	spin_lock_irqsave(div->core.lock, flags);
515 
516 	regmap_update_bits(regmap, AT91_PMC_PLL_UPDT,
517 			   AT91_PMC_PLL_UPDT_ID_MSK, id);
518 	regmap_read(regmap, AT91_PMC_PLL_CTRL0, &val);
519 	div->div = FIELD_GET(PMC_PLL_CTRL0_DIV_MSK, val);
520 
521 	spin_unlock_irqrestore(div->core.lock, flags);
522 
523 	hw = &div->core.hw;
524 	ret = clk_hw_register(NULL, hw);
525 	if (ret) {
526 		kfree(div);
527 		hw = ERR_PTR(ret);
528 	}
529 
530 	return hw;
531 }
532 
533