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