blob: 89c8d02336aba3fc4c7a7528bf145744335a170b [file] [log] [blame]
Adam Forddb7b2f42021-06-04 12:26:06 -05001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Driver for IDT Versaclock 5/6
4 *
5 * Derived from code Copyright (C) 2017 Marek Vasut <marek.vasut@gmail.com>
6 */
7
8#include <common.h>
9#include <clk.h>
10#include <clk-uclass.h>
11#include <dm.h>
12#include <errno.h>
13#include <i2c.h>
14#include <dm/device_compat.h>
15#include <log.h>
16#include <linux/clk-provider.h>
17#include <linux/kernel.h>
18#include <linux/math64.h>
19
20#include <dt-bindings/clk/versaclock.h>
21
22/* VersaClock5 registers */
23#define VC5_OTP_CONTROL 0x00
24
25/* Factory-reserved register block */
26#define VC5_RSVD_DEVICE_ID 0x01
27#define VC5_RSVD_ADC_GAIN_7_0 0x02
28#define VC5_RSVD_ADC_GAIN_15_8 0x03
29#define VC5_RSVD_ADC_OFFSET_7_0 0x04
30#define VC5_RSVD_ADC_OFFSET_15_8 0x05
31#define VC5_RSVD_TEMPY 0x06
32#define VC5_RSVD_OFFSET_TBIN 0x07
33#define VC5_RSVD_GAIN 0x08
34#define VC5_RSVD_TEST_NP 0x09
35#define VC5_RSVD_UNUSED 0x0a
36#define VC5_RSVD_BANDGAP_TRIM_UP 0x0b
37#define VC5_RSVD_BANDGAP_TRIM_DN 0x0c
38#define VC5_RSVD_CLK_R_12_CLK_AMP_4 0x0d
39#define VC5_RSVD_CLK_R_34_CLK_AMP_4 0x0e
40#define VC5_RSVD_CLK_AMP_123 0x0f
41
42/* Configuration register block */
43#define VC5_PRIM_SRC_SHDN 0x10
44#define VC5_PRIM_SRC_SHDN_EN_XTAL BIT(7)
45#define VC5_PRIM_SRC_SHDN_EN_CLKIN BIT(6)
46#define VC5_PRIM_SRC_SHDN_EN_DOUBLE_XTAL_FREQ BIT(3)
47#define VC5_PRIM_SRC_SHDN_SP BIT(1)
48#define VC5_PRIM_SRC_SHDN_EN_GBL_SHDN BIT(0)
49
50#define VC5_VCO_BAND 0x11
51#define VC5_XTAL_X1_LOAD_CAP 0x12
52#define VC5_XTAL_X2_LOAD_CAP 0x13
53#define VC5_REF_DIVIDER 0x15
54#define VC5_REF_DIVIDER_SEL_PREDIV2 BIT(7)
55#define VC5_REF_DIVIDER_REF_DIV(n) ((n) & 0x3f)
56
57#define VC5_VCO_CTRL_AND_PREDIV 0x16
58#define VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV BIT(7)
59
60#define VC5_FEEDBACK_INT_DIV 0x17
61#define VC5_FEEDBACK_INT_DIV_BITS 0x18
62#define VC5_FEEDBACK_FRAC_DIV(n) (0x19 + (n))
63#define VC5_RC_CONTROL0 0x1e
64#define VC5_RC_CONTROL1 0x1f
65/* Register 0x20 is factory reserved */
66
67/* Output divider control for divider 1,2,3,4 */
68#define VC5_OUT_DIV_CONTROL(idx) (0x21 + ((idx) * 0x10))
69#define VC5_OUT_DIV_CONTROL_RESET BIT(7)
70#define VC5_OUT_DIV_CONTROL_SELB_NORM BIT(3)
71#define VC5_OUT_DIV_CONTROL_SEL_EXT BIT(2)
72#define VC5_OUT_DIV_CONTROL_INT_MODE BIT(1)
73#define VC5_OUT_DIV_CONTROL_EN_FOD BIT(0)
74
75#define VC5_OUT_DIV_FRAC(idx, n) (0x22 + ((idx) * 0x10) + (n))
76#define VC5_OUT_DIV_FRAC4_OD_SCEE BIT(1)
77
78#define VC5_OUT_DIV_STEP_SPREAD(idx, n) (0x26 + ((idx) * 0x10) + (n))
79#define VC5_OUT_DIV_SPREAD_MOD(idx, n) (0x29 + ((idx) * 0x10) + (n))
80#define VC5_OUT_DIV_SKEW_INT(idx, n) (0x2b + ((idx) * 0x10) + (n))
81#define VC5_OUT_DIV_INT(idx, n) (0x2d + ((idx) * 0x10) + (n))
82#define VC5_OUT_DIV_SKEW_FRAC(idx) (0x2f + ((idx) * 0x10))
83/* Registers 0x30, 0x40, 0x50 are factory reserved */
84
85/* Clock control register for clock 1,2 */
86#define VC5_CLK_OUTPUT_CFG(idx, n) (0x60 + ((idx) * 0x2) + (n))
87#define VC5_CLK_OUTPUT_CFG0_CFG_SHIFT 5
88#define VC5_CLK_OUTPUT_CFG0_CFG_MASK GENMASK(7, VC5_CLK_OUTPUT_CFG0_CFG_SHIFT)
89
90#define VC5_CLK_OUTPUT_CFG0_CFG_LVPECL (VC5_LVPECL)
91#define VC5_CLK_OUTPUT_CFG0_CFG_CMOS (VC5_CMOS)
92#define VC5_CLK_OUTPUT_CFG0_CFG_HCSL33 (VC5_HCSL33)
93#define VC5_CLK_OUTPUT_CFG0_CFG_LVDS (VC5_LVDS)
94#define VC5_CLK_OUTPUT_CFG0_CFG_CMOS2 (VC5_CMOS2)
95#define VC5_CLK_OUTPUT_CFG0_CFG_CMOSD (VC5_CMOSD)
96#define VC5_CLK_OUTPUT_CFG0_CFG_HCSL25 (VC5_HCSL25)
97
98#define VC5_CLK_OUTPUT_CFG0_PWR_SHIFT 3
99#define VC5_CLK_OUTPUT_CFG0_PWR_MASK GENMASK(4, VC5_CLK_OUTPUT_CFG0_PWR_SHIFT)
100#define VC5_CLK_OUTPUT_CFG0_PWR_18 (0 << VC5_CLK_OUTPUT_CFG0_PWR_SHIFT)
101#define VC5_CLK_OUTPUT_CFG0_PWR_25 (2 << VC5_CLK_OUTPUT_CFG0_PWR_SHIFT)
102#define VC5_CLK_OUTPUT_CFG0_PWR_33 (3 << VC5_CLK_OUTPUT_CFG0_PWR_SHIFT)
103#define VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT 0
104#define VC5_CLK_OUTPUT_CFG0_SLEW_MASK GENMASK(1, VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
105#define VC5_CLK_OUTPUT_CFG0_SLEW_80 (0 << VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
106#define VC5_CLK_OUTPUT_CFG0_SLEW_85 (1 << VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
107#define VC5_CLK_OUTPUT_CFG0_SLEW_90 (2 << VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
108#define VC5_CLK_OUTPUT_CFG0_SLEW_100 (3 << VC5_CLK_OUTPUT_CFG0_SLEW_SHIFT)
109#define VC5_CLK_OUTPUT_CFG1_EN_CLKBUF BIT(0)
110
111#define VC5_CLK_OE_SHDN 0x68
112#define VC5_CLK_OS_SHDN 0x69
113
114#define VC5_GLOBAL_REGISTER 0x76
115#define VC5_GLOBAL_REGISTER_GLOBAL_RESET BIT(5)
116
117/* PLL/VCO runs between 2.5 GHz and 3.0 GHz */
118#define VC5_PLL_VCO_MIN 2500000000UL
119#define VC5_PLL_VCO_MAX 3000000000UL
120
121/* VC5 Input mux settings */
122#define VC5_MUX_IN_XIN BIT(0)
123#define VC5_MUX_IN_CLKIN BIT(1)
124
125/* Maximum number of clk_out supported by this driver */
126#define VC5_MAX_CLK_OUT_NUM 5
127
128/* Maximum number of FODs supported by this driver */
129#define VC5_MAX_FOD_NUM 4
130
131/* flags to describe chip features */
132/* chip has built-in oscilator */
133#define VC5_HAS_INTERNAL_XTAL BIT(0)
134/* chip has PFD requency doubler */
135#define VC5_HAS_PFD_FREQ_DBL BIT(1)
136
137/* Supported IDT VC5 models. */
138enum vc5_model {
139 IDT_VC5_5P49V5923,
140 IDT_VC5_5P49V5925,
141 IDT_VC5_5P49V5933,
142 IDT_VC5_5P49V5935,
143 IDT_VC6_5P49V6901,
144 IDT_VC6_5P49V6965,
145};
146
147/* Structure to describe features of a particular VC5 model */
148struct vc5_chip_info {
149 const enum vc5_model model;
150 const unsigned int clk_fod_cnt;
151 const unsigned int clk_out_cnt;
152 const u32 flags;
153};
154
155struct vc5_driver_data;
156
157struct vc5_hw_data {
158 struct clk hw;
159 struct vc5_driver_data *vc5;
160 u32 div_int;
161 u32 div_frc;
162 unsigned int num;
163};
164
165struct vc5_out_data {
166 struct clk hw;
167 struct vc5_driver_data *vc5;
168 unsigned int num;
169 unsigned int clk_output_cfg0;
170 unsigned int clk_output_cfg0_mask;
171};
172
173struct vc5_driver_data {
174 struct udevice *i2c;
175 const struct vc5_chip_info *chip_info;
176
177 struct clk *pin_xin;
178 struct clk *pin_clkin;
179 unsigned char clk_mux_ins;
180 struct clk clk_mux;
181 struct clk clk_mul;
182 struct clk clk_pfd;
183 struct vc5_hw_data clk_pll;
184 struct vc5_hw_data clk_fod[VC5_MAX_FOD_NUM];
185 struct vc5_out_data clk_out[VC5_MAX_CLK_OUT_NUM];
186};
187
188static const struct vc5_chip_info idt_5p49v5923_info = {
189 .model = IDT_VC5_5P49V5923,
190 .clk_fod_cnt = 2,
191 .clk_out_cnt = 3,
192 .flags = 0,
193};
194
195static const struct vc5_chip_info idt_5p49v5925_info = {
196 .model = IDT_VC5_5P49V5925,
197 .clk_fod_cnt = 4,
198 .clk_out_cnt = 5,
199 .flags = 0,
200};
201
202static const struct vc5_chip_info idt_5p49v5933_info = {
203 .model = IDT_VC5_5P49V5933,
204 .clk_fod_cnt = 2,
205 .clk_out_cnt = 3,
206 .flags = VC5_HAS_INTERNAL_XTAL,
207};
208
209static const struct vc5_chip_info idt_5p49v5935_info = {
210 .model = IDT_VC5_5P49V5935,
211 .clk_fod_cnt = 4,
212 .clk_out_cnt = 5,
213 .flags = VC5_HAS_INTERNAL_XTAL,
214};
215
216static const struct vc5_chip_info idt_5p49v6901_info = {
217 .model = IDT_VC6_5P49V6901,
218 .clk_fod_cnt = 4,
219 .clk_out_cnt = 5,
220 .flags = VC5_HAS_PFD_FREQ_DBL,
221};
222
223static const struct vc5_chip_info idt_5p49v6965_info = {
224 .model = IDT_VC6_5P49V6965,
225 .clk_fod_cnt = 4,
226 .clk_out_cnt = 5,
227 .flags = 0,
228};
229
230static int vc5_update_bits(struct udevice *dev, unsigned int reg, unsigned int mask,
231 unsigned int src)
232{
233 int ret;
234 unsigned char cache;
235
236 ret = dm_i2c_read(dev, reg, &cache, 1);
237 if (ret < 0)
238 return ret;
239
240 cache &= ~mask;
241 cache |= mask & src;
242 ret = dm_i2c_write(dev, reg, (uchar *)&cache, 1);
243
244 return ret;
245}
246
247static unsigned long vc5_mux_get_rate(struct clk *hw)
248{
249 return clk_get_rate(clk_get_parent(hw));
250}
251
252static int vc5_mux_set_parent(struct clk *hw, unsigned char index)
253{
254 struct vc5_driver_data *vc5 = container_of(hw, struct vc5_driver_data, clk_mux);
255 const u8 mask = VC5_PRIM_SRC_SHDN_EN_XTAL | VC5_PRIM_SRC_SHDN_EN_CLKIN;
256 u8 src;
257
258 if (index > 1 || !vc5->clk_mux_ins)
259 return -EINVAL;
260
261 if (vc5->clk_mux_ins == (VC5_MUX_IN_CLKIN | VC5_MUX_IN_XIN)) {
262 if (index == 0)
263 src = VC5_PRIM_SRC_SHDN_EN_XTAL;
264 if (index == 1)
265 src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
266 } else {
267 if (index != 0)
268 return -EINVAL;
269
270 if (vc5->clk_mux_ins == VC5_MUX_IN_XIN)
271 src = VC5_PRIM_SRC_SHDN_EN_XTAL;
272 else if (vc5->clk_mux_ins == VC5_MUX_IN_CLKIN)
273 src = VC5_PRIM_SRC_SHDN_EN_CLKIN;
274 else /* Invalid; should have been caught by vc5_probe() */
275 return -EINVAL;
276 }
277
278 return vc5_update_bits(vc5->i2c, VC5_PRIM_SRC_SHDN, mask, src);
279}
280
281static const struct clk_ops vc5_mux_ops = {
282 .get_rate = vc5_mux_get_rate,
283};
284
285static unsigned long vc5_pfd_round_rate(struct clk *hw, unsigned long rate)
286{
287 struct clk *clk_parent = clk_get_parent(hw);
288 unsigned long parent_rate = clk_get_rate(clk_parent);
289 unsigned long idiv;
290
291 /* PLL cannot operate with input clock above 50 MHz. */
292 if (rate > 50000000)
293 return -EINVAL;
294
295 /* CLKIN within range of PLL input, feed directly to PLL. */
296 if (parent_rate <= 50000000)
297 return parent_rate;
298
299 idiv = DIV_ROUND_UP(parent_rate, rate);
300 if (idiv > 127)
301 return -EINVAL;
302
303 return parent_rate / idiv;
304}
305
306static unsigned long vc5_pfd_recalc_rate(struct clk *hw)
307{
308 struct vc5_driver_data *vc5 =
309 container_of(hw, struct vc5_driver_data, clk_pfd);
310 unsigned int prediv, div;
311 struct clk *clk_parent = clk_get_parent(hw);
312 unsigned long parent_rate = clk_get_rate(clk_parent);
313
314 dm_i2c_read(vc5->i2c, VC5_VCO_CTRL_AND_PREDIV, (uchar *)&prediv, 1);
315
316 /* The bypass_prediv is set, PLL fed from Ref_in directly. */
317 if (prediv & VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV)
318 return parent_rate;
319
320 dm_i2c_read(vc5->i2c, VC5_REF_DIVIDER, (uchar *)&div, 1);
321
322 /* The Sel_prediv2 is set, PLL fed from prediv2 (Ref_in / 2) */
323 if (div & VC5_REF_DIVIDER_SEL_PREDIV2)
324 return parent_rate / 2;
325 else
326 return parent_rate / VC5_REF_DIVIDER_REF_DIV(div);
327}
328
329static unsigned long vc5_pfd_set_rate(struct clk *hw, unsigned long rate)
330{
331 struct vc5_driver_data *vc5 =
332 container_of(hw, struct vc5_driver_data, clk_pfd);
333 unsigned long idiv;
334 u8 div;
335 struct clk *clk_parent = clk_get_parent(hw);
336 unsigned long parent_rate = clk_get_rate(clk_parent);
337
338 /* CLKIN within range of PLL input, feed directly to PLL. */
339 if (parent_rate <= 50000000) {
340 vc5_update_bits(vc5->i2c, VC5_VCO_CTRL_AND_PREDIV,
341 VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV,
342 VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV);
343 vc5_update_bits(vc5->i2c, VC5_REF_DIVIDER, 0xff, 0x00);
344 return 0;
345 }
346
347 idiv = DIV_ROUND_UP(parent_rate, rate);
348
349 /* We have dedicated div-2 predivider. */
350 if (idiv == 2)
351 div = VC5_REF_DIVIDER_SEL_PREDIV2;
352 else
353 div = VC5_REF_DIVIDER_REF_DIV(idiv);
354
355 vc5_update_bits(vc5->i2c, VC5_REF_DIVIDER, 0xff, div);
356 vc5_update_bits(vc5->i2c, VC5_VCO_CTRL_AND_PREDIV,
357 VC5_VCO_CTRL_AND_PREDIV_BYPASS_PREDIV, 0);
358
359 return 0;
360}
361
362static const struct clk_ops vc5_pfd_ops = {
363 .round_rate = vc5_pfd_round_rate,
364 .get_rate = vc5_pfd_recalc_rate,
365 .set_rate = vc5_pfd_set_rate,
366};
367
368/*
369 * VersaClock5 PLL/VCO
370 */
371static unsigned long vc5_pll_recalc_rate(struct clk *hw)
372{
373 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
374 struct vc5_driver_data *vc = hwdata->vc5;
375 struct clk *clk_parent = clk_get_parent(hw);
376 unsigned long parent_rate = clk_get_rate(clk_parent);
377 u32 div_int, div_frc;
378 u8 fb[5];
379
380 dm_i2c_read(vc->i2c, VC5_FEEDBACK_INT_DIV, fb, 5);
381
382 div_int = (fb[0] << 4) | (fb[1] >> 4);
383 div_frc = (fb[2] << 16) | (fb[3] << 8) | fb[4];
384
385 /* The PLL divider has 12 integer bits and 24 fractional bits */
386 return (parent_rate * div_int) + ((parent_rate * div_frc) >> 24);
387}
388
389static unsigned long vc5_pll_round_rate(struct clk *hw, unsigned long rate)
390{
391 struct clk *clk_parent = clk_get_parent(hw);
392 unsigned long parent_rate = clk_get_rate(clk_parent);
393 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
394 u32 div_int;
395 u64 div_frc;
396
397 if (rate < VC5_PLL_VCO_MIN)
398 rate = VC5_PLL_VCO_MIN;
399 if (rate > VC5_PLL_VCO_MAX)
400 rate = VC5_PLL_VCO_MAX;
401
402 /* Determine integer part, which is 12 bit wide */
403 div_int = rate / parent_rate;
404 if (div_int > 0xfff)
405 rate = parent_rate * 0xfff;
406
407 /* Determine best fractional part, which is 24 bit wide */
408 div_frc = rate % parent_rate;
409 div_frc *= BIT(24) - 1;
410 do_div(div_frc, parent_rate);
411
412 hwdata->div_int = div_int;
413 hwdata->div_frc = (u32)div_frc;
414
415 return (parent_rate * div_int) + ((parent_rate * div_frc) >> 24);
416}
417
418static unsigned long vc5_pll_set_rate(struct clk *hw, unsigned long rate)
419{
420 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
421 struct vc5_driver_data *vc5 = hwdata->vc5;
422 u8 fb[5];
423
424 fb[0] = hwdata->div_int >> 4;
425 fb[1] = hwdata->div_int << 4;
426 fb[2] = hwdata->div_frc >> 16;
427 fb[3] = hwdata->div_frc >> 8;
428 fb[4] = hwdata->div_frc;
429
430 return dm_i2c_write(vc5->i2c, VC5_FEEDBACK_INT_DIV, fb, 5);
431}
432
433static const struct clk_ops vc5_pll_ops = {
434 .round_rate = vc5_pll_round_rate,
435 .get_rate = vc5_pll_recalc_rate,
436 .set_rate = vc5_pll_set_rate,
437};
438
439static unsigned long vc5_fod_recalc_rate(struct clk *hw)
440{
441 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
442 struct vc5_driver_data *vc = hwdata->vc5;
443 struct clk *parent = &vc->clk_pll.hw;
444 unsigned long parent_rate = vc5_pll_recalc_rate(parent);
445
446 /* VCO frequency is divided by two before entering FOD */
447 u32 f_in = parent_rate / 2;
448 u32 div_int, div_frc;
449 u8 od_int[2];
450 u8 od_frc[4];
451
452 dm_i2c_read(vc->i2c, VC5_OUT_DIV_INT(hwdata->num, 0), od_int, 2);
453 dm_i2c_read(vc->i2c, VC5_OUT_DIV_FRAC(hwdata->num, 0), od_frc, 4);
454
455 div_int = (od_int[0] << 4) | (od_int[1] >> 4);
456 div_frc = (od_frc[0] << 22) | (od_frc[1] << 14) |
457 (od_frc[2] << 6) | (od_frc[3] >> 2);
458
459 /* Avoid division by zero if the output is not configured. */
460 if (div_int == 0 && div_frc == 0)
461 return 0;
462
463 /* The PLL divider has 12 integer bits and 30 fractional bits */
464 return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
465}
466
467static unsigned long vc5_fod_round_rate(struct clk *hw, unsigned long rate)
468{
469 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
470 struct vc5_driver_data *vc = hwdata->vc5;
471 struct clk *parent = &vc->clk_pll.hw;
472 unsigned long parent_rate = vc5_pll_recalc_rate(parent);
473
474 /* VCO frequency is divided by two before entering FOD */
475 u32 f_in = parent_rate / 2;
476 u32 div_int;
477 u64 div_frc;
478
479 /* Determine integer part, which is 12 bit wide */
480 div_int = f_in / rate;
481
482 /*
483 * WARNING: The clock chip does not output signal if the integer part
484 * of the divider is 0xfff and fractional part is non-zero.
485 * Clamp the divider at 0xffe to keep the code simple.
486 */
487 if (div_int > 0xffe) {
488 div_int = 0xffe;
489 rate = f_in / div_int;
490 }
491
492 /* Determine best fractional part, which is 30 bit wide */
493 div_frc = f_in % rate;
494 div_frc <<= 24;
495 do_div(div_frc, rate);
496
497 hwdata->div_int = div_int;
498 hwdata->div_frc = (u32)div_frc;
499
500 return div64_u64((u64)f_in << 24ULL, ((u64)div_int << 24ULL) + div_frc);
501}
502
503static unsigned long vc5_fod_set_rate(struct clk *hw, unsigned long rate)
504{
505 struct vc5_hw_data *hwdata = container_of(hw, struct vc5_hw_data, hw);
506 struct vc5_driver_data *vc5 = hwdata->vc5;
507
508 u8 data[14] = {
509 hwdata->div_frc >> 22, hwdata->div_frc >> 14,
510 hwdata->div_frc >> 6, hwdata->div_frc << 2,
511 0, 0, 0, 0, 0,
512 0, 0,
513 hwdata->div_int >> 4, hwdata->div_int << 4,
514 0
515 };
516
517 dm_i2c_write(vc5->i2c, VC5_OUT_DIV_FRAC(hwdata->num, 0), data, 14);
518
519 /*
520 * Toggle magic bit in undocumented register for unknown reason.
521 * This is what the IDT timing commander tool does and the chip
522 * datasheet somewhat implies this is needed, but the register
523 * and the bit is not documented.
524 */
525 vc5_update_bits(vc5->i2c, VC5_GLOBAL_REGISTER,
526 VC5_GLOBAL_REGISTER_GLOBAL_RESET, 0);
527 vc5_update_bits(vc5->i2c, VC5_GLOBAL_REGISTER,
528 VC5_GLOBAL_REGISTER_GLOBAL_RESET,
529 VC5_GLOBAL_REGISTER_GLOBAL_RESET);
530
531 return 0;
532}
533
534static const struct clk_ops vc5_fod_ops = {
535 .round_rate = vc5_fod_round_rate,
536 .get_rate = vc5_fod_recalc_rate,
537 .set_rate = vc5_fod_set_rate,
538};
539
540static int vc5_clk_out_prepare(struct clk *hw)
541{
542 struct udevice *dev;
543 struct vc5_driver_data *vc5;
544 struct vc5_out_data *hwdata;
545
546 const u8 mask = VC5_OUT_DIV_CONTROL_SELB_NORM |
547 VC5_OUT_DIV_CONTROL_SEL_EXT |
548 VC5_OUT_DIV_CONTROL_EN_FOD;
549 unsigned int src;
550 int ret;
551
552 uclass_get_device_by_name(UCLASS_CLK, clk_hw_get_name(hw), &dev);
553 vc5 = dev_get_priv(dev);
554 hwdata = &vc5->clk_out[hw->id];
555
556 /*
557 * If the input mux is disabled, enable it first and
558 * select source from matching FOD.
559 */
560
561 dm_i2c_read(vc5->i2c, VC5_OUT_DIV_CONTROL(hwdata->num), (uchar *)&src, 1);
562
563 if ((src & mask) == 0) {
564 src = VC5_OUT_DIV_CONTROL_RESET | VC5_OUT_DIV_CONTROL_EN_FOD;
565 ret = vc5_update_bits(vc5->i2c,
566 VC5_OUT_DIV_CONTROL(hwdata->num),
567 mask | VC5_OUT_DIV_CONTROL_RESET, src);
568 if (ret)
569 return ret;
570 }
571
572 /* Enable the clock buffer */
573 vc5_update_bits(vc5->i2c, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
574 VC5_CLK_OUTPUT_CFG1_EN_CLKBUF,
575 VC5_CLK_OUTPUT_CFG1_EN_CLKBUF);
576 if (hwdata->clk_output_cfg0_mask) {
577 vc5_update_bits(vc5->i2c, VC5_CLK_OUTPUT_CFG(hwdata->num, 0),
578 hwdata->clk_output_cfg0_mask,
579 hwdata->clk_output_cfg0);
580 }
581
582 return 0;
583}
584
585static int vc5_clk_out_unprepare(struct clk *hw)
586{
587 struct udevice *dev;
588 struct vc5_driver_data *vc5;
589 struct vc5_out_data *hwdata;
590 int ret;
591
592 uclass_get_device_by_name(UCLASS_CLK, clk_hw_get_name(hw), &dev);
593 vc5 = dev_get_priv(dev);
594 hwdata = &vc5->clk_out[hw->id];
595
596 /* Disable the clock buffer */
597 ret = vc5_update_bits(vc5->i2c, VC5_CLK_OUTPUT_CFG(hwdata->num, 1),
598 VC5_CLK_OUTPUT_CFG1_EN_CLKBUF, 0);
599
600 return ret;
601}
602
603static int vc5_clk_out_set_parent(struct vc5_driver_data *vc, u8 num, u8 index)
604{
605 const u8 mask = VC5_OUT_DIV_CONTROL_RESET |
606 VC5_OUT_DIV_CONTROL_SELB_NORM |
607 VC5_OUT_DIV_CONTROL_SEL_EXT |
608 VC5_OUT_DIV_CONTROL_EN_FOD;
609 const u8 extclk = VC5_OUT_DIV_CONTROL_SELB_NORM |
610 VC5_OUT_DIV_CONTROL_SEL_EXT;
611 u8 src = VC5_OUT_DIV_CONTROL_RESET;
612
613 if (index == 0)
614 src |= VC5_OUT_DIV_CONTROL_EN_FOD;
615 else
616 src |= extclk;
617
618 return vc5_update_bits(vc->i2c, VC5_OUT_DIV_CONTROL(num), mask, src);
619}
620
621/*
622 * The device references to the Versaclock point to the head, so xlate needs to
623 * redirect it to clk_out[idx]
624 */
625static int vc5_clk_out_xlate(struct clk *hw, struct ofnode_phandle_args *args)
626{
627 unsigned int idx = args->args[0];
628
629 if (args->args_count != 1) {
Sean Andersona1b654b2021-12-01 14:26:53 -0500630 debug("Invalid args_count: %d\n", args->args_count);
Adam Forddb7b2f42021-06-04 12:26:06 -0500631 return -EINVAL;
632 }
633
634 hw->id = idx;
635
636 return 0;
637}
638
639static unsigned long vc5_clk_out_set_rate(struct clk *hw, unsigned long rate)
640{
641 struct udevice *dev;
642 struct vc5_driver_data *vc;
643 struct clk *parent;
644
645 uclass_get_device_by_name(UCLASS_CLK, clk_hw_get_name(hw), &dev);
646 vc = dev_get_priv(dev);
647 parent = clk_get_parent(&vc->clk_out[hw->id].hw);
648
649 /* setting the output rate really means setting the parent FOD rate */
650 return clk_set_rate(parent, clk_round_rate(parent, rate));
651}
652
653static unsigned long vc5_clk_out_get_rate(struct clk *hw)
654{
655 return clk_get_parent_rate(hw);
656}
657
658static const struct clk_ops vc5_clk_out_ops = {
659 .enable = vc5_clk_out_prepare,
660 .disable = vc5_clk_out_unprepare,
661 .set_rate = vc5_clk_out_set_rate,
662 .get_rate = vc5_clk_out_get_rate,
663};
664
665static const struct clk_ops vc5_clk_out_sel_ops = {
666 .enable = vc5_clk_out_prepare,
667 .disable = vc5_clk_out_unprepare,
668 .get_rate = vc5_clk_out_get_rate,
669};
670
671static const struct clk_ops vc5_clk_ops = {
672 .enable = vc5_clk_out_prepare,
673 .disable = vc5_clk_out_unprepare,
674 .of_xlate = vc5_clk_out_xlate,
675 .set_rate = vc5_clk_out_set_rate,
676 .get_rate = vc5_clk_out_get_rate,
677};
678
679static int vc5_map_index_to_output(const enum vc5_model model,
680 const unsigned int n)
681{
682 switch (model) {
683 case IDT_VC5_5P49V5933:
684 return (n == 0) ? 0 : 3;
685 case IDT_VC5_5P49V5923:
686 case IDT_VC5_5P49V5925:
687 case IDT_VC5_5P49V5935:
688 case IDT_VC6_5P49V6901:
689 case IDT_VC6_5P49V6965:
690 default:
691 return n;
692 }
693}
694
695static int vc5_update_mode(ofnode np_output,
696 struct vc5_out_data *clk_out)
697{
698 u32 value;
699
700 if (!ofnode_read_u32(np_output, "idt,mode", &value)) {
701 clk_out->clk_output_cfg0_mask |= VC5_CLK_OUTPUT_CFG0_CFG_MASK;
702 switch (value) {
703 case VC5_CLK_OUTPUT_CFG0_CFG_LVPECL:
704 case VC5_CLK_OUTPUT_CFG0_CFG_CMOS:
705 case VC5_CLK_OUTPUT_CFG0_CFG_HCSL33:
706 case VC5_CLK_OUTPUT_CFG0_CFG_LVDS:
707 case VC5_CLK_OUTPUT_CFG0_CFG_CMOS2:
708 case VC5_CLK_OUTPUT_CFG0_CFG_CMOSD:
709 case VC5_CLK_OUTPUT_CFG0_CFG_HCSL25:
710 clk_out->clk_output_cfg0 |=
711 value << VC5_CLK_OUTPUT_CFG0_CFG_SHIFT;
712 break;
713 default:
714 return -EINVAL;
715 }
716 }
717
718 return 0;
719}
720
721static int vc5_update_power(ofnode np_output, struct vc5_out_data *clk_out)
722{
723 u32 value;
724
725 if (!ofnode_read_u32(np_output, "idt,voltage-microvolt", &value)) {
726 clk_out->clk_output_cfg0_mask |= VC5_CLK_OUTPUT_CFG0_PWR_MASK;
727 switch (value) {
728 case 1800000:
729 clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_PWR_18;
730 break;
731 case 2500000:
732 clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_PWR_25;
733 break;
734 case 3300000:
735 clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_PWR_33;
736 break;
737 default:
738 return -EINVAL;
739 }
740 }
741 return 0;
742}
743
744static int vc5_map_cap_value(u32 femtofarads)
745{
746 int mapped_value;
747
748 /*
749 * The datasheet explicitly states 9000 - 25000 with 0.5pF
750 * steps, but the Programmer's guide shows the steps are 0.430pF.
751 * After getting feedback from Renesas, the .5pF steps were the
752 * goal, but 430nF was the actual values.
753 * Because of this, the actual range goes to 22760 instead of 25000
754 */
755 if (femtofarads < 9000 || femtofarads > 22760)
756 return -EINVAL;
757
758 /*
759 * The Programmer's guide shows XTAL[5:0] but in reality,
760 * XTAL[0] and XTAL[1] are both LSB which makes the math
761 * strange. With clarfication from Renesas, setting the
762 * values should be simpler by ignoring XTAL[0]
763 */
764 mapped_value = DIV_ROUND_CLOSEST(femtofarads - 9000, 430);
765
766 /*
767 * Since the calculation ignores XTAL[0], there is one
768 * special case where mapped_value = 32. In reality, this means
769 * the real mapped value should be 111111b. In other cases,
770 * the mapped_value needs to be shifted 1 to the left.
771 */
772 if (mapped_value > 31)
773 mapped_value = 0x3f;
774 else
775 mapped_value <<= 1;
776
777 return mapped_value;
778}
779
780static int vc5_update_cap_load(ofnode node, struct vc5_driver_data *vc5)
781{
782 u32 value;
783 int mapped_value;
784
785 if (!ofnode_read_u32(node, "idt,xtal-load-femtofarads", &value)) {
786 mapped_value = vc5_map_cap_value(value);
787
788 if (mapped_value < 0)
789 return mapped_value;
790
791 /*
792 * The mapped_value is really the high 6 bits of
793 * VC5_XTAL_X1_LOAD_CAP and VC5_XTAL_X2_LOAD_CAP, so
794 * shift the value 2 places.
795 */
796 vc5_update_bits(vc5->i2c, VC5_XTAL_X1_LOAD_CAP, ~0x03, mapped_value << 2);
797 vc5_update_bits(vc5->i2c, VC5_XTAL_X2_LOAD_CAP, ~0x03, mapped_value << 2);
798 }
799
800 return 0;
801}
802
803static int vc5_update_slew(ofnode np_output, struct vc5_out_data *clk_out)
804{
805 u32 value;
806
807 if (!ofnode_read_u32(np_output, "idt,slew-percent", &value)) {
808 clk_out->clk_output_cfg0_mask |= VC5_CLK_OUTPUT_CFG0_SLEW_MASK;
809
810 switch (value) {
811 case 80:
812 clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_SLEW_80;
813 break;
814 case 85:
815 clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_SLEW_85;
816 break;
817 case 90:
818 clk_out->clk_output_cfg0 |= VC5_CLK_OUTPUT_CFG0_SLEW_90;
819 break;
820 case 100:
821 clk_out->clk_output_cfg0 |=
822 VC5_CLK_OUTPUT_CFG0_SLEW_100;
823 break;
824 default:
825 return -EINVAL;
826 }
827 }
828 return 0;
829}
830
831static int vc5_get_output_config(struct udevice *dev,
832 struct vc5_out_data *clk_out)
833{
834 ofnode np_output;
835 char child_name[5];
836 int ret = 0;
837
838 sprintf(child_name, "OUT%d", clk_out->num + 1);
839
840 np_output = dev_read_subnode(dev, child_name);
841
842 if (!ofnode_valid(np_output)) {
843 dev_dbg(dev, "Invalid clock output configuration OUT%d\n",
844 clk_out->num + 1);
845 return 0;
846 }
847
848 ret = vc5_update_mode(np_output, clk_out);
849 if (ret)
850 return ret;
851
852 ret = vc5_update_power(np_output, clk_out);
853 if (ret)
854 return ret;
855
856 ret = vc5_update_slew(np_output, clk_out);
857
858 return ret;
859}
860
861static char *versaclock_get_name(const char *dev_name, const char *clk_name, int index)
862{
863 int length;
864 char *buf;
865
866 if (index < 0)
867 length = snprintf(NULL, 0, "%s.%s", dev_name, clk_name) + 1;
868 else
869 length = snprintf(NULL, 0, "%s.%s%d", dev_name, clk_name, index) + 1;
870
871 buf = malloc(length);
872 if (!buf)
873 ERR_PTR(-ENOMEM);
874
875 if (index < 0)
876 snprintf(buf, length, "%s.%s", dev_name, clk_name);
877 else
878 snprintf(buf, length, "%s.%s%d", dev_name, clk_name, index);
879
880 return buf;
881}
882
883int versaclock_probe(struct udevice *dev)
884{
885 struct vc5_driver_data *vc5 = dev_get_priv(dev);
886 struct vc5_chip_info *chip = (void *)dev_get_driver_data(dev);
887 unsigned int n, idx = 0;
888 char *mux_name, *pfd_name, *pll_name, *outsel_name;
889 char *out_name[VC5_MAX_CLK_OUT_NUM];
890 char *fod_name[VC5_MAX_FOD_NUM];
891 int ret;
892 u64 val;
893
894 val = (u64)dev_read_addr_ptr(dev);
895 ret = i2c_get_chip(dev->parent, val, 1, &vc5->i2c);
896
897 if (ret) {
898 dev_dbg(dev, "I2C probe failed.\n");
899 return ret;
900 }
901
902 vc5->chip_info = chip;
903 vc5->pin_xin = devm_clk_get(dev, "xin");
904
905 if (IS_ERR(vc5->pin_xin))
906 dev_dbg(dev, "failed to get xin clock\n");
907
908 ret = clk_enable(vc5->pin_xin);
909 if (ret)
910 dev_dbg(dev, "failed to enable XIN clock\n");
911
912 vc5->pin_clkin = devm_clk_get(dev, "clkin");
913
914 /* Register clock input mux */
915 if (!IS_ERR(vc5->pin_xin)) {
916 vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
917 } else if (vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL) {
918 if (IS_ERR(vc5->pin_xin))
919 return PTR_ERR(vc5->pin_xin);
920 vc5->clk_mux_ins |= VC5_MUX_IN_XIN;
921 }
922
923 mux_name = versaclock_get_name(dev->name, "mux", -1);
924 if (IS_ERR(mux_name))
925 return PTR_ERR(mux_name);
926
927 clk_register(&vc5->clk_mux, "versaclock-mux", mux_name, vc5->pin_xin->dev->name);
928
929 if (!IS_ERR(vc5->pin_xin))
930 vc5_mux_set_parent(&vc5->clk_mux, 1);
931 else
932 vc5_mux_set_parent(&vc5->clk_mux, 0);
933
934 /* Configure Optional Loading Capacitance for external XTAL */
935 if (!(vc5->chip_info->flags & VC5_HAS_INTERNAL_XTAL)) {
936 ret = vc5_update_cap_load(dev_ofnode(dev), vc5);
937 if (ret)
938 dev_dbg(dev, "failed to vc5_update_cap_load\n");
939 }
940
941 /* Register PFD */
942 pfd_name = versaclock_get_name(dev->name, "pfd", -1);
943 if (IS_ERR(pfd_name)) {
944 ret = PTR_ERR(pfd_name);
945 goto free_mux;
946 }
947
948 ret = clk_register(&vc5->clk_pfd, "versaclock-pfd", pfd_name, vc5->clk_mux.dev->name);
949 if (ret)
950 goto free_pfd;
951
952 /* Register PLL */
953 vc5->clk_pll.num = 0;
954 vc5->clk_pll.vc5 = vc5;
955 pll_name = versaclock_get_name(dev->name, "pll", -1);
956 if (IS_ERR(pll_name)) {
957 ret = PTR_ERR(pll_name);
958 goto free_pfd;
959 }
960
961 ret = clk_register(&vc5->clk_pll.hw, "versaclock-pll", pll_name, vc5->clk_pfd.dev->name);
962 if (ret)
963 goto free_pll;
964
965 /* Register FODs */
966 for (n = 0; n < vc5->chip_info->clk_fod_cnt; n++) {
967 fod_name[n] = versaclock_get_name(dev->name, "fod", n);
968 if (IS_ERR(pll_name)) {
969 ret = PTR_ERR(fod_name[n]);
970 goto free_fod;
971 }
972 idx = vc5_map_index_to_output(vc5->chip_info->model, n);
973 vc5->clk_fod[n].num = idx;
974 vc5->clk_fod[n].vc5 = vc5;
975 ret = clk_register(&vc5->clk_fod[n].hw, "versaclock-fod", fod_name[n],
976 vc5->clk_pll.hw.dev->name);
977 if (ret)
978 goto free_fod;
979 }
980
981 /* Register MUX-connected OUT0_I2C_SELB output */
982 vc5->clk_out[0].num = idx;
983 vc5->clk_out[0].vc5 = vc5;
984 outsel_name = versaclock_get_name(dev->name, "out0_sel_i2cb", -1);
985 if (IS_ERR(outsel_name)) {
986 ret = PTR_ERR(outsel_name);
987 goto free_fod;
988 };
989
990 ret = clk_register(&vc5->clk_out[0].hw, "versaclock-outsel", outsel_name,
991 vc5->clk_mux.dev->name);
992 if (ret)
993 goto free_selb;
994
995 /* Register FOD-connected OUTx outputs */
996 for (n = 1; n < vc5->chip_info->clk_out_cnt; n++) {
997 idx = vc5_map_index_to_output(vc5->chip_info->model, n - 1);
998 out_name[n] = versaclock_get_name(dev->name, "out", n);
999 if (IS_ERR(out_name[n])) {
1000 ret = PTR_ERR(out_name[n]);
1001 goto free_selb;
1002 }
1003 vc5->clk_out[n].num = idx;
1004 vc5->clk_out[n].vc5 = vc5;
1005 ret = clk_register(&vc5->clk_out[n].hw, "versaclock-out", out_name[n],
1006 vc5->clk_fod[idx].hw.dev->name);
1007 if (ret)
1008 goto free_out;
1009 vc5_clk_out_set_parent(vc5, idx, 0);
1010
1011 /* Fetch Clock Output configuration from DT (if specified) */
1012 ret = vc5_get_output_config(dev, &vc5->clk_out[n]);
1013 if (ret) {
1014 dev_dbg(dev, "failed to vc5_get_output_config()\n");
1015 goto free_out;
1016 }
1017 }
1018
1019 return 0;
1020
1021free_out:
1022 for (n = 1; n < vc5->chip_info->clk_out_cnt; n++) {
1023 clk_free(&vc5->clk_out[n].hw);
1024 free(out_name[n]);
1025 }
1026free_selb:
1027 clk_free(&vc5->clk_out[0].hw);
1028 free(outsel_name);
1029free_fod:
1030 for (n = 0; n < vc5->chip_info->clk_fod_cnt; n++) {
1031 clk_free(&vc5->clk_fod[n].hw);
1032 free(fod_name[n]);
1033 }
1034free_pll:
1035 clk_free(&vc5->clk_pll.hw);
1036 free(pll_name);
1037free_pfd:
1038 clk_free(&vc5->clk_pfd);
1039 free(pfd_name);
1040free_mux:
1041 clk_free(&vc5->clk_mux);
1042 free(mux_name);
1043
1044 return ret;
1045}
1046
1047static const struct udevice_id versaclock_ids[] = {
1048 { .compatible = "idt,5p49v5923", .data = (ulong)&idt_5p49v5923_info },
1049 { .compatible = "idt,5p49v5925", .data = (ulong)&idt_5p49v5925_info },
1050 { .compatible = "idt,5p49v5933", .data = (ulong)&idt_5p49v5933_info },
1051 { .compatible = "idt,5p49v5935", .data = (ulong)&idt_5p49v5935_info },
1052 { .compatible = "idt,5p49v6901", .data = (ulong)&idt_5p49v6901_info },
1053 { .compatible = "idt,5p49v6965", .data = (ulong)&idt_5p49v6965_info },
1054 {},
1055};
1056
1057U_BOOT_DRIVER(versaclock) = {
1058 .name = "versaclock",
1059 .id = UCLASS_CLK,
1060 .ops = &vc5_clk_ops,
1061 .of_match = versaclock_ids,
1062 .probe = versaclock_probe,
1063 .priv_auto = sizeof(struct vc5_driver_data),
1064};
1065
1066U_BOOT_DRIVER(versaclock_mux) = {
1067 .name = "versaclock-mux",
1068 .id = UCLASS_CLK,
1069 .ops = &vc5_mux_ops,
1070};
1071
1072U_BOOT_DRIVER(versaclock_pfd) = {
1073 .name = "versaclock-pfd",
1074 .id = UCLASS_CLK,
1075 .ops = &vc5_pfd_ops,
1076};
1077
1078U_BOOT_DRIVER(versaclock_pll) = {
1079 .name = "versaclock-pll",
1080 .id = UCLASS_CLK,
1081 .ops = &vc5_pll_ops,
1082};
1083
1084U_BOOT_DRIVER(versaclock_fod) = {
1085 .name = "versaclock-fod",
1086 .id = UCLASS_CLK,
1087 .ops = &vc5_fod_ops,
1088};
1089
1090U_BOOT_DRIVER(versaclock_out) = {
1091 .name = "versaclock-out",
1092 .id = UCLASS_CLK,
1093 .ops = &vc5_clk_out_ops,
1094};
1095
1096U_BOOT_DRIVER(versaclock_outsel) = {
1097 .name = "versaclock-outsel",
1098 .id = UCLASS_CLK,
1099 .ops = &vc5_clk_out_sel_ops,
1100};