blob: cba87f5093b98182a370ed9ef7dfe1acfe8b04ad [file] [log] [blame]
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
4 * Jean-Jacques Hiblot <jjhiblot@ti.com>
5 */
6
7#include <common.h>
8#include <clk-uclass.h>
9#include <dm.h>
10#include <dm/device_compat.h>
11#include <asm/gpio.h>
12#include <dm/lists.h>
13#include <dm/device-internal.h>
14#include <regmap.h>
15#include <reset-uclass.h>
16#include <dt-bindings/phy/phy.h>
17
18#include <dt-bindings/phy/phy-ti.h>
19
20#define WIZ_MAX_INPUT_CLOCKS 4
21/* To include mux clocks, divider clocks and gate clocks */
22#define WIZ_MAX_OUTPUT_CLOCKS 32
23
24#define WIZ_MAX_LANES 4
25#define WIZ_MUX_NUM_CLOCKS 3
26#define WIZ_DIV_NUM_CLOCKS_16G 2
27#define WIZ_DIV_NUM_CLOCKS_10G 1
28
29#define WIZ_SERDES_CTRL 0x404
30#define WIZ_SERDES_TOP_CTRL 0x408
31#define WIZ_SERDES_RST 0x40c
32#define WIZ_SERDES_TYPEC 0x410
33#define WIZ_LANECTL(n) (0x480 + (0x40 * (n)))
34#define WIZ_LANEDIV(n) (0x484 + (0x40 * (n)))
35
36#define WIZ_MAX_LANES 4
37#define WIZ_MUX_NUM_CLOCKS 3
38#define WIZ_DIV_NUM_CLOCKS_16G 2
39#define WIZ_DIV_NUM_CLOCKS_10G 1
40
41#define WIZ_SERDES_TYPEC_LN10_SWAP BIT(30)
Sinthu Rajadae83a42023-03-13 18:12:24 +053042#define WIZ_SERDES_TYPEC_LN23_SWAP BIT(31)
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +053043
44enum wiz_lane_standard_mode {
45 LANE_MODE_GEN1,
46 LANE_MODE_GEN2,
47 LANE_MODE_GEN3,
48 LANE_MODE_GEN4,
49};
50
51enum wiz_refclk_mux_sel {
52 PLL0_REFCLK,
53 PLL1_REFCLK,
54 REFCLK_DIG,
55};
56
57enum wiz_refclk_div_sel {
58 CMN_REFCLK,
59 CMN_REFCLK1,
60};
61
62enum wiz_clock_input {
63 WIZ_CORE_REFCLK,
64 WIZ_EXT_REFCLK,
65 WIZ_CORE_REFCLK1,
66 WIZ_EXT_REFCLK1,
67};
68
Sinthu Rajadae83a42023-03-13 18:12:24 +053069/*
70 * List of master lanes used for lane swapping
71 */
72enum wiz_typec_master_lane {
73 LANE0 = 0,
74 LANE2 = 2,
75};
76
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +053077static const struct reg_field por_en = REG_FIELD(WIZ_SERDES_CTRL, 31, 31);
78static const struct reg_field phy_reset_n = REG_FIELD(WIZ_SERDES_RST, 31, 31);
79static const struct reg_field pll1_refclk_mux_sel =
80 REG_FIELD(WIZ_SERDES_RST, 29, 29);
Matt Ranostayb7c6fbb2022-10-05 13:51:30 -070081static const struct reg_field pll1_refclk_mux_sel_2 =
82 REG_FIELD(WIZ_SERDES_RST, 22, 23);
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +053083static const struct reg_field pll0_refclk_mux_sel =
84 REG_FIELD(WIZ_SERDES_RST, 28, 28);
Matt Ranostayb7c6fbb2022-10-05 13:51:30 -070085static const struct reg_field pll0_refclk_mux_sel_2 =
86 REG_FIELD(WIZ_SERDES_RST, 28, 29);
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +053087static const struct reg_field refclk_dig_sel_16g =
88 REG_FIELD(WIZ_SERDES_RST, 24, 25);
89static const struct reg_field refclk_dig_sel_10g =
90 REG_FIELD(WIZ_SERDES_RST, 24, 24);
91static const struct reg_field pma_cmn_refclk_int_mode =
92 REG_FIELD(WIZ_SERDES_TOP_CTRL, 28, 29);
Matt Ranostayb7c6fbb2022-10-05 13:51:30 -070093static const struct reg_field pma_cmn_refclk1_int_mode =
94 REG_FIELD(WIZ_SERDES_TOP_CTRL, 20, 21);
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +053095static const struct reg_field pma_cmn_refclk_mode =
96 REG_FIELD(WIZ_SERDES_TOP_CTRL, 30, 31);
97static const struct reg_field pma_cmn_refclk_dig_div =
98 REG_FIELD(WIZ_SERDES_TOP_CTRL, 26, 27);
99static const struct reg_field pma_cmn_refclk1_dig_div =
100 REG_FIELD(WIZ_SERDES_TOP_CTRL, 24, 25);
101
102static const struct reg_field p_enable[WIZ_MAX_LANES] = {
103 REG_FIELD(WIZ_LANECTL(0), 30, 31),
104 REG_FIELD(WIZ_LANECTL(1), 30, 31),
105 REG_FIELD(WIZ_LANECTL(2), 30, 31),
106 REG_FIELD(WIZ_LANECTL(3), 30, 31),
107};
108
109enum p_enable { P_ENABLE = 2, P_ENABLE_FORCE = 1, P_ENABLE_DISABLE = 0 };
110
111static const struct reg_field p_align[WIZ_MAX_LANES] = {
112 REG_FIELD(WIZ_LANECTL(0), 29, 29),
113 REG_FIELD(WIZ_LANECTL(1), 29, 29),
114 REG_FIELD(WIZ_LANECTL(2), 29, 29),
115 REG_FIELD(WIZ_LANECTL(3), 29, 29),
116};
117
118static const struct reg_field p_raw_auto_start[WIZ_MAX_LANES] = {
119 REG_FIELD(WIZ_LANECTL(0), 28, 28),
120 REG_FIELD(WIZ_LANECTL(1), 28, 28),
121 REG_FIELD(WIZ_LANECTL(2), 28, 28),
122 REG_FIELD(WIZ_LANECTL(3), 28, 28),
123};
124
125static const struct reg_field p_standard_mode[WIZ_MAX_LANES] = {
126 REG_FIELD(WIZ_LANECTL(0), 24, 25),
127 REG_FIELD(WIZ_LANECTL(1), 24, 25),
128 REG_FIELD(WIZ_LANECTL(2), 24, 25),
129 REG_FIELD(WIZ_LANECTL(3), 24, 25),
130};
131
132static const struct reg_field p0_fullrt_div[WIZ_MAX_LANES] = {
133 REG_FIELD(WIZ_LANECTL(0), 22, 23),
134 REG_FIELD(WIZ_LANECTL(1), 22, 23),
135 REG_FIELD(WIZ_LANECTL(2), 22, 23),
136 REG_FIELD(WIZ_LANECTL(3), 22, 23),
137};
138
139static const struct reg_field p_mac_div_sel0[WIZ_MAX_LANES] = {
140 REG_FIELD(WIZ_LANEDIV(0), 16, 22),
141 REG_FIELD(WIZ_LANEDIV(1), 16, 22),
142 REG_FIELD(WIZ_LANEDIV(2), 16, 22),
143 REG_FIELD(WIZ_LANEDIV(3), 16, 22),
144};
145
146static const struct reg_field p_mac_div_sel1[WIZ_MAX_LANES] = {
147 REG_FIELD(WIZ_LANEDIV(0), 0, 8),
148 REG_FIELD(WIZ_LANEDIV(1), 0, 8),
149 REG_FIELD(WIZ_LANEDIV(2), 0, 8),
150 REG_FIELD(WIZ_LANEDIV(3), 0, 8),
151};
152
153struct wiz_clk_mux_sel {
154 enum wiz_refclk_mux_sel mux_sel;
155 u32 table[WIZ_MAX_INPUT_CLOCKS];
156 const char *node_name;
157 u32 num_parents;
158 u32 parents[WIZ_MAX_INPUT_CLOCKS];
159};
160
161struct wiz_clk_div_sel {
162 enum wiz_refclk_div_sel div_sel;
163 const char *node_name;
164};
165
166static struct wiz_clk_mux_sel clk_mux_sel_16g[] = {
167 {
168 /*
169 * Mux value to be configured for each of the input clocks
170 * in the order populated in device tree
171 */
172 .num_parents = 2,
173 .parents = { WIZ_CORE_REFCLK, WIZ_EXT_REFCLK },
174 .mux_sel = PLL0_REFCLK,
175 .table = { 1, 0 },
176 .node_name = "pll0-refclk",
177 },
178 {
179 .num_parents = 2,
180 .parents = { WIZ_CORE_REFCLK1, WIZ_EXT_REFCLK1 },
181 .mux_sel = PLL1_REFCLK,
182 .table = { 1, 0 },
183 .node_name = "pll1-refclk",
184 },
185 {
186 .num_parents = 4,
187 .parents = { WIZ_CORE_REFCLK, WIZ_CORE_REFCLK1, WIZ_EXT_REFCLK, WIZ_EXT_REFCLK1 },
188 .mux_sel = REFCLK_DIG,
189 .table = { 1, 3, 0, 2 },
190 .node_name = "refclk-dig",
191 },
192};
193
194static struct wiz_clk_mux_sel clk_mux_sel_10g[] = {
195 {
196 /*
197 * Mux value to be configured for each of the input clocks
198 * in the order populated in device tree
199 */
200 .num_parents = 2,
201 .parents = { WIZ_CORE_REFCLK, WIZ_EXT_REFCLK },
202 .mux_sel = PLL0_REFCLK,
203 .table = { 1, 0 },
204 .node_name = "pll0-refclk",
205 },
206 {
207 .num_parents = 2,
208 .parents = { WIZ_CORE_REFCLK, WIZ_EXT_REFCLK },
209 .mux_sel = PLL1_REFCLK,
210 .table = { 1, 0 },
211 .node_name = "pll1-refclk",
212 },
213 {
214 .num_parents = 2,
215 .parents = { WIZ_CORE_REFCLK, WIZ_EXT_REFCLK },
216 .mux_sel = REFCLK_DIG,
217 .table = { 1, 0 },
218 .node_name = "refclk-dig",
219 },
220};
221
Matt Ranostayb7c6fbb2022-10-05 13:51:30 -0700222static const struct wiz_clk_mux_sel clk_mux_sel_10g_2_refclk[] = {
223 {
224 .num_parents = 3,
225 .parents = { WIZ_CORE_REFCLK, WIZ_CORE_REFCLK1, WIZ_EXT_REFCLK },
226 .table = { 2, 3, 0 },
227 .node_name = "pll0-refclk",
228 },
229 {
230 .num_parents = 3,
231 .parents = { WIZ_CORE_REFCLK, WIZ_CORE_REFCLK1, WIZ_EXT_REFCLK },
232 .table = { 2, 3, 0 },
233 .node_name = "pll1-refclk",
234 },
235 {
236 .num_parents = 3,
237 .parents = { WIZ_CORE_REFCLK, WIZ_CORE_REFCLK1, WIZ_EXT_REFCLK },
238 .table = { 2, 3, 0 },
239 .node_name = "refclk-dig",
240 },
241};
242
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530243static struct wiz_clk_div_sel clk_div_sel[] = {
244 {
245 .div_sel = CMN_REFCLK,
246 .node_name = "cmn-refclk-dig-div",
247 },
248 {
249 .div_sel = CMN_REFCLK1,
250 .node_name = "cmn-refclk1-dig-div",
251 },
252};
253
254enum wiz_type {
255 J721E_WIZ_16G,
256 J721E_WIZ_10G,
257 AM64_WIZ_10G,
Matt Ranostayb7c6fbb2022-10-05 13:51:30 -0700258 J784S4_WIZ_10G,
Ravi Gunasekaran6a096e62023-05-15 16:20:40 +0530259 J721S2_WIZ_10G,
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530260};
261
Matt Ranostay6d255cc2022-07-07 23:41:52 -0700262struct wiz_data {
263 enum wiz_type type;
264 const struct reg_field *pll0_refclk_mux_sel;
265 const struct reg_field *pll1_refclk_mux_sel;
266 const struct reg_field *refclk_dig_sel;
267 const struct reg_field *pma_cmn_refclk1_dig_div;
Matt Ranostayb7c6fbb2022-10-05 13:51:30 -0700268 const struct reg_field *pma_cmn_refclk1_int_mode;
Matt Ranostay6d255cc2022-07-07 23:41:52 -0700269 const struct wiz_clk_mux_sel *clk_mux_sel;
270 unsigned int clk_div_sel_num;
271};
272
273static const struct wiz_data j721e_16g_data = {
274 .type = J721E_WIZ_16G,
275 .pll0_refclk_mux_sel = &pll0_refclk_mux_sel,
276 .pll1_refclk_mux_sel = &pll1_refclk_mux_sel,
277 .refclk_dig_sel = &refclk_dig_sel_16g,
278 .pma_cmn_refclk1_dig_div = &pma_cmn_refclk1_dig_div,
279 .clk_mux_sel = clk_mux_sel_16g,
280 .clk_div_sel_num = WIZ_DIV_NUM_CLOCKS_16G,
281};
282
283static const struct wiz_data j721e_10g_data = {
284 .type = J721E_WIZ_10G,
285 .pll0_refclk_mux_sel = &pll0_refclk_mux_sel,
286 .pll1_refclk_mux_sel = &pll1_refclk_mux_sel,
287 .refclk_dig_sel = &refclk_dig_sel_10g,
288 .clk_mux_sel = clk_mux_sel_10g,
289 .clk_div_sel_num = WIZ_DIV_NUM_CLOCKS_10G,
290};
291
292static struct wiz_data am64_10g_data = {
293 .type = AM64_WIZ_10G,
294 .pll0_refclk_mux_sel = &pll0_refclk_mux_sel,
295 .pll1_refclk_mux_sel = &pll1_refclk_mux_sel,
296 .refclk_dig_sel = &refclk_dig_sel_10g,
297 .clk_mux_sel = clk_mux_sel_10g,
298 .clk_div_sel_num = WIZ_DIV_NUM_CLOCKS_10G,
299};
300
Matt Ranostayb7c6fbb2022-10-05 13:51:30 -0700301static struct wiz_data j784s4_wiz_10g = {
302 .type = J784S4_WIZ_10G,
303 .pll0_refclk_mux_sel = &pll0_refclk_mux_sel_2,
304 .pll1_refclk_mux_sel = &pll1_refclk_mux_sel_2,
305 .refclk_dig_sel = &refclk_dig_sel_16g,
306 .pma_cmn_refclk1_int_mode = &pma_cmn_refclk1_int_mode,
307 .clk_mux_sel = clk_mux_sel_10g_2_refclk,
308 .clk_div_sel_num = WIZ_DIV_NUM_CLOCKS_10G,
309};
310
Ravi Gunasekaran6a096e62023-05-15 16:20:40 +0530311static struct wiz_data j721s2_10g_data = {
312 .type = J721S2_WIZ_10G,
313 .pll0_refclk_mux_sel = &pll0_refclk_mux_sel,
314 .pll1_refclk_mux_sel = &pll1_refclk_mux_sel,
315 .refclk_dig_sel = &refclk_dig_sel_10g,
316 .clk_mux_sel = clk_mux_sel_10g,
317 .clk_div_sel_num = WIZ_DIV_NUM_CLOCKS_10G,
318};
319
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530320#define WIZ_TYPEC_DIR_DEBOUNCE_MIN 100 /* ms */
321#define WIZ_TYPEC_DIR_DEBOUNCE_MAX 1000
322
323struct wiz {
324 struct regmap *regmap;
325 enum wiz_type type;
326 struct wiz_clk_mux_sel *clk_mux_sel;
327 struct wiz_clk_div_sel *clk_div_sel;
328 unsigned int clk_div_sel_num;
329 struct regmap_field *por_en;
330 struct regmap_field *phy_reset_n;
331 struct regmap_field *phy_en_refclk;
332 struct regmap_field *p_enable[WIZ_MAX_LANES];
333 struct regmap_field *p_align[WIZ_MAX_LANES];
334 struct regmap_field *p_raw_auto_start[WIZ_MAX_LANES];
335 struct regmap_field *p_standard_mode[WIZ_MAX_LANES];
336 struct regmap_field *p_mac_div_sel0[WIZ_MAX_LANES];
337 struct regmap_field *p_mac_div_sel1[WIZ_MAX_LANES];
338 struct regmap_field *p0_fullrt_div[WIZ_MAX_LANES];
339 struct regmap_field *pma_cmn_refclk_int_mode;
Matt Ranostayb7c6fbb2022-10-05 13:51:30 -0700340 struct regmap_field *pma_cmn_refclk1_int_mode;
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530341 struct regmap_field *pma_cmn_refclk_mode;
342 struct regmap_field *pma_cmn_refclk_dig_div;
343 struct regmap_field *pma_cmn_refclk1_dig_div;
344 struct regmap_field *div_sel_field[WIZ_DIV_NUM_CLOCKS_16G];
345 struct regmap_field *mux_sel_field[WIZ_MUX_NUM_CLOCKS];
346
347 struct udevice *dev;
348 u32 num_lanes;
349 struct gpio_desc *gpio_typec_dir;
350 u32 lane_phy_type[WIZ_MAX_LANES];
Sinthu Rajac5df75f2023-03-13 18:12:23 +0530351 u32 master_lane_num[WIZ_MAX_LANES];
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530352 struct clk *input_clks[WIZ_MAX_INPUT_CLOCKS];
353 unsigned int id;
Matt Ranostay6d255cc2022-07-07 23:41:52 -0700354 const struct wiz_data *data;
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530355};
356
357struct wiz_div_clk {
358 struct clk parent_clk;
359 struct wiz *wiz;
360};
361
362struct wiz_mux_clk {
363 struct clk parent_clks[4];
364 struct wiz *wiz;
365};
366
367struct wiz_clk {
368 struct wiz *wiz;
369};
370
371struct wiz_reset {
372 struct wiz *wiz;
373};
374
375static ulong wiz_div_clk_get_rate(struct clk *clk)
376{
377 struct udevice *dev = clk->dev;
378 struct wiz_div_clk *priv = dev_get_priv(dev);
379 struct wiz_clk_div_sel *data = dev_get_plat(dev);
380 struct wiz *wiz = priv->wiz;
381 ulong parent_rate = clk_get_rate(&priv->parent_clk);
382 u32 val;
383
384 regmap_field_read(wiz->div_sel_field[data->div_sel], &val);
385
386 return parent_rate >> val;
387}
388
389static ulong wiz_div_clk_set_rate(struct clk *clk, ulong rate)
390{
391 struct udevice *dev = clk->dev;
392 struct wiz_div_clk *priv = dev_get_priv(dev);
393 struct wiz_clk_div_sel *data = dev_get_plat(dev);
394 struct wiz *wiz = priv->wiz;
395 ulong parent_rate = clk_get_rate(&priv->parent_clk);
396 u32 div = parent_rate / rate;
397
398 div = __ffs(div);
399 regmap_field_write(wiz->div_sel_field[data->div_sel], div);
400
401 return parent_rate >> div;
402}
403
404const struct clk_ops wiz_div_clk_ops = {
405 .get_rate = wiz_div_clk_get_rate,
406 .set_rate = wiz_div_clk_set_rate,
407};
408
409int wiz_div_clk_probe(struct udevice *dev)
410{
411 struct wiz_div_clk *priv = dev_get_priv(dev);
412 struct clk parent_clk;
413 int rc;
414
415 rc = clk_get_by_index(dev, 0, &parent_clk);
416 if (rc) {
417 dev_err(dev, "unable to get parent clock. ret %d\n", rc);
418 return rc;
419 }
420 priv->parent_clk = parent_clk;
421 priv->wiz = dev_get_priv(dev->parent);
422 return 0;
423}
424
425U_BOOT_DRIVER(wiz_div_clk) = {
426 .name = "wiz_div_clk",
427 .id = UCLASS_CLK,
428 .priv_auto = sizeof(struct wiz_div_clk),
429 .ops = &wiz_div_clk_ops,
430 .probe = wiz_div_clk_probe,
431};
432
433static int wiz_clk_mux_set_parent(struct clk *clk, struct clk *parent)
434{
435 struct udevice *dev = clk->dev;
436 struct wiz_mux_clk *priv = dev_get_priv(dev);
437 struct wiz_clk_mux_sel *data = dev_get_plat(dev);
438 struct wiz *wiz = priv->wiz;
439 int i;
440
441 for (i = 0; i < ARRAY_SIZE(priv->parent_clks); i++)
442 if (parent->dev == priv->parent_clks[i].dev)
443 break;
444
445 if (i == ARRAY_SIZE(priv->parent_clks))
446 return -EINVAL;
447
448 regmap_field_write(wiz->mux_sel_field[data->mux_sel], data->table[i]);
449 return 0;
450}
451
452static int wiz_clk_xlate(struct clk *clk, struct ofnode_phandle_args *args)
453{
454 struct udevice *dev = clk->dev;
455 struct wiz_mux_clk *priv = dev_get_priv(dev);
456 struct wiz *wiz = priv->wiz;
457
458 clk->id = wiz->id;
459
460 return 0;
461}
462
463static const struct clk_ops wiz_clk_mux_ops = {
464 .set_parent = wiz_clk_mux_set_parent,
465 .of_xlate = wiz_clk_xlate,
466};
467
468int wiz_mux_clk_probe(struct udevice *dev)
469{
470 struct wiz_mux_clk *priv = dev_get_priv(dev);
471 int rc;
472 int i;
473
474 for (i = 0; i < ARRAY_SIZE(priv->parent_clks); i++) {
475 rc = clk_get_by_index(dev, i, &priv->parent_clks[i]);
476 if (rc)
477 priv->parent_clks[i].dev = NULL;
478 }
479 priv->wiz = dev_get_priv(dev->parent);
480 return 0;
481}
482
483U_BOOT_DRIVER(wiz_mux_clk) = {
484 .name = "wiz_mux_clk",
485 .id = UCLASS_CLK,
486 .priv_auto = sizeof(struct wiz_mux_clk),
487 .ops = &wiz_clk_mux_ops,
488 .probe = wiz_mux_clk_probe,
489};
490
491static int wiz_clk_set_parent(struct clk *clk, struct clk *parent)
492{
493 struct udevice *dev = clk->dev;
494 struct wiz_clk *priv = dev_get_priv(dev);
495 const struct wiz_clk_mux_sel *mux_sel;
496 struct wiz *wiz = priv->wiz;
497 int num_parents;
498 int i, j, id;
499
500 id = clk->id >> 10;
501
502 /* set_parent is applicable only for MUX clocks */
503 if (id > TI_WIZ_REFCLK_DIG)
504 return 0;
505
506 for (i = 0; i < WIZ_MAX_INPUT_CLOCKS; i++)
507 if (wiz->input_clks[i]->dev == parent->dev)
508 break;
509
510 if (i == WIZ_MAX_INPUT_CLOCKS)
511 return -EINVAL;
512
513 mux_sel = &wiz->clk_mux_sel[id];
514 num_parents = mux_sel->num_parents;
515 for (j = 0; j < num_parents; j++)
516 if (mux_sel->parents[j] == i)
517 break;
518
519 if (j == num_parents)
520 return -EINVAL;
521
522 regmap_field_write(wiz->mux_sel_field[id], mux_sel->table[j]);
523
524 return 0;
525}
526
527static int wiz_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
528{
529 struct udevice *dev = clk->dev;
530 struct wiz_clk *priv = dev_get_priv(dev);
531 struct wiz *wiz = priv->wiz;
532
533 clk->id = args->args[0] << 10 | wiz->id;
534
535 return 0;
536}
537
538static const struct clk_ops wiz_clk_ops = {
539 .set_parent = wiz_clk_set_parent,
540 .of_xlate = wiz_clk_of_xlate,
541};
542
543int wiz_clk_probe(struct udevice *dev)
544{
545 struct wiz_clk *priv = dev_get_priv(dev);
546
547 priv->wiz = dev_get_priv(dev->parent);
548
549 return 0;
550}
551
552U_BOOT_DRIVER(wiz_clk) = {
553 .name = "wiz_clk",
554 .id = UCLASS_CLK,
555 .priv_auto = sizeof(struct wiz_clk),
556 .ops = &wiz_clk_ops,
557 .probe = wiz_clk_probe,
558};
559
560static int wiz_reset_request(struct reset_ctl *reset_ctl)
561{
562 return 0;
563}
564
565static int wiz_reset_free(struct reset_ctl *reset_ctl)
566{
567 return 0;
568}
569
570static int wiz_reset_assert(struct reset_ctl *reset_ctl)
571{
572 struct wiz_reset *priv = dev_get_priv(reset_ctl->dev);
573 struct wiz *wiz = priv->wiz;
574 int ret;
575 int id = reset_ctl->id;
576
577 if (id == 0) {
578 ret = regmap_field_write(wiz->phy_reset_n, false);
579 return ret;
580 }
581
582 ret = regmap_field_write(wiz->p_enable[id - 1], P_ENABLE_DISABLE);
583 return ret;
584}
585
586static int wiz_phy_fullrt_div(struct wiz *wiz, int lane)
587{
Siddharth Vadapalli855a34e2023-08-02 13:47:27 +0530588 switch (wiz->type) {
589 case AM64_WIZ_10G:
590 if (wiz->lane_phy_type[lane] == PHY_TYPE_PCIE)
591 return regmap_field_write(wiz->p0_fullrt_div[lane], 0x1);
592 break;
593 case J721E_WIZ_10G:
594 if (wiz->lane_phy_type[lane] == PHY_TYPE_SGMII)
595 return regmap_field_write(wiz->p0_fullrt_div[lane], 0x2);
596 break;
597 default:
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530598 return 0;
Siddharth Vadapalli855a34e2023-08-02 13:47:27 +0530599 }
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530600 return 0;
601}
602
603static int wiz_reset_deassert(struct reset_ctl *reset_ctl)
604{
605 struct wiz_reset *priv = dev_get_priv(reset_ctl->dev);
606 struct wiz *wiz = priv->wiz;
607 int ret;
608 int id = reset_ctl->id;
609
610 ret = wiz_phy_fullrt_div(wiz, id - 1);
611 if (ret)
612 return ret;
613
614 /* if typec-dir gpio was specified, set LN10 SWAP bit based on that */
Sinthu Rajac5df75f2023-03-13 18:12:23 +0530615 if (id == 0) {
616 if (wiz->gpio_typec_dir) {
617 if (dm_gpio_get_value(wiz->gpio_typec_dir)) {
618 regmap_update_bits(wiz->regmap, WIZ_SERDES_TYPEC,
619 WIZ_SERDES_TYPEC_LN10_SWAP,
620 WIZ_SERDES_TYPEC_LN10_SWAP);
621 } else {
622 regmap_update_bits(wiz->regmap, WIZ_SERDES_TYPEC,
623 WIZ_SERDES_TYPEC_LN10_SWAP, 0);
624 }
625 }
626 } else {
627 /* if no typec-dir gpio was specified and PHY type is
628 * USB3 with master lane number is '0', set LN10 SWAP
629 * bit to '1'
630 */
631 u32 num_lanes = wiz->num_lanes;
632 int i;
633
634 for (i = 0; i < num_lanes; i++) {
Sinthu Rajadae83a42023-03-13 18:12:24 +0530635 if (wiz->lane_phy_type[i] == PHY_TYPE_USB3) {
636 switch (wiz->master_lane_num[i]) {
637 case LANE0:
Sinthu Rajac5df75f2023-03-13 18:12:23 +0530638 regmap_update_bits(wiz->regmap, WIZ_SERDES_TYPEC,
639 WIZ_SERDES_TYPEC_LN10_SWAP,
640 WIZ_SERDES_TYPEC_LN10_SWAP);
Sinthu Rajadae83a42023-03-13 18:12:24 +0530641 break;
642 case LANE2:
643 regmap_update_bits(wiz->regmap, WIZ_SERDES_TYPEC,
644 WIZ_SERDES_TYPEC_LN23_SWAP,
645 WIZ_SERDES_TYPEC_LN23_SWAP);
646 break;
647 default:
648 break;
649 }
650 }
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530651 }
652 }
653
654 if (id == 0) {
655 ret = regmap_field_write(wiz->phy_reset_n, true);
656 return ret;
657 }
658
Aswath Govindrajueaf8b512022-01-28 13:41:37 +0530659 if (wiz->lane_phy_type[id - 1] == PHY_TYPE_DP)
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530660 ret = regmap_field_write(wiz->p_enable[id - 1], P_ENABLE);
661 else
662 ret = regmap_field_write(wiz->p_enable[id - 1], P_ENABLE_FORCE);
663
664 return ret;
665}
666
667static struct reset_ops wiz_reset_ops = {
668 .request = wiz_reset_request,
669 .rfree = wiz_reset_free,
670 .rst_assert = wiz_reset_assert,
671 .rst_deassert = wiz_reset_deassert,
672};
673
674int wiz_reset_probe(struct udevice *dev)
675{
676 struct wiz_reset *priv = dev_get_priv(dev);
677
678 priv->wiz = dev_get_priv(dev->parent);
679
680 return 0;
681}
682
683U_BOOT_DRIVER(wiz_reset) = {
684 .name = "wiz-reset",
685 .id = UCLASS_RESET,
686 .probe = wiz_reset_probe,
687 .ops = &wiz_reset_ops,
688 .flags = DM_FLAG_LEAVE_PD_ON,
689};
690
691static int wiz_reset(struct wiz *wiz)
692{
693 int ret;
694
695 ret = regmap_field_write(wiz->por_en, 0x1);
696 if (ret)
697 return ret;
698
699 mdelay(1);
700
701 ret = regmap_field_write(wiz->por_en, 0x0);
702 if (ret)
703 return ret;
704
705 return 0;
706}
707
708static int wiz_p_mac_div_sel(struct wiz *wiz)
709{
710 u32 num_lanes = wiz->num_lanes;
711 int ret;
712 int i;
713
714 for (i = 0; i < num_lanes; i++) {
Siddharth Vadapalli855a34e2023-08-02 13:47:27 +0530715 if (wiz->lane_phy_type[i] == PHY_TYPE_SGMII ||
716 wiz->lane_phy_type[i] == PHY_TYPE_QSGMII) {
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530717 ret = regmap_field_write(wiz->p_mac_div_sel0[i], 1);
718 if (ret)
719 return ret;
720
721 ret = regmap_field_write(wiz->p_mac_div_sel1[i], 2);
722 if (ret)
723 return ret;
724 }
725 }
726
727 return 0;
728}
729
730static int wiz_mode_select(struct wiz *wiz)
731{
732 u32 num_lanes = wiz->num_lanes;
733 int ret;
734 int i;
735
736 for (i = 0; i < num_lanes; i++) {
737 if (wiz->lane_phy_type[i] == PHY_TYPE_QSGMII) {
738 ret = regmap_field_write(wiz->p_standard_mode[i],
739 LANE_MODE_GEN2);
740 if (ret)
741 return ret;
742 }
743 }
744
745 return 0;
746}
747
748static int wiz_init_raw_interface(struct wiz *wiz, bool enable)
749{
750 u32 num_lanes = wiz->num_lanes;
751 int i;
752 int ret;
753
754 for (i = 0; i < num_lanes; i++) {
755 ret = regmap_field_write(wiz->p_align[i], enable);
756 if (ret)
757 return ret;
758
759 ret = regmap_field_write(wiz->p_raw_auto_start[i], enable);
760 if (ret)
761 return ret;
762 }
763
764 return 0;
765}
766
767static int wiz_init(struct wiz *wiz)
768{
769 struct udevice *dev = wiz->dev;
770 int ret;
771
772 ret = wiz_reset(wiz);
773 if (ret) {
774 dev_err(dev, "WIZ reset failed\n");
775 return ret;
776 }
777
778 ret = wiz_mode_select(wiz);
779 if (ret) {
780 dev_err(dev, "WIZ mode select failed\n");
781 return ret;
782 }
783
784 ret = wiz_p_mac_div_sel(wiz);
785 if (ret) {
786 dev_err(dev, "Configuring P0 MAC DIV SEL failed\n");
787 return ret;
788 }
789
790 ret = wiz_init_raw_interface(wiz, true);
791 if (ret) {
792 dev_err(dev, "WIZ interface initialization failed\n");
793 return ret;
794 }
795
796 return 0;
797}
798
799static int wiz_regfield_init(struct wiz *wiz)
800{
801 struct regmap *regmap = wiz->regmap;
802 int num_lanes = wiz->num_lanes;
803 struct udevice *dev = wiz->dev;
Matt Ranostay6d255cc2022-07-07 23:41:52 -0700804 const struct wiz_data *data = wiz->data;
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530805 int i;
806
807 wiz->por_en = devm_regmap_field_alloc(dev, regmap, por_en);
808 if (IS_ERR(wiz->por_en)) {
809 dev_err(dev, "POR_EN reg field init failed\n");
810 return PTR_ERR(wiz->por_en);
811 }
812
813 wiz->phy_reset_n = devm_regmap_field_alloc(dev, regmap,
814 phy_reset_n);
815 if (IS_ERR(wiz->phy_reset_n)) {
816 dev_err(dev, "PHY_RESET_N reg field init failed\n");
817 return PTR_ERR(wiz->phy_reset_n);
818 }
819
820 wiz->pma_cmn_refclk_int_mode =
821 devm_regmap_field_alloc(dev, regmap, pma_cmn_refclk_int_mode);
822 if (IS_ERR(wiz->pma_cmn_refclk_int_mode)) {
823 dev_err(dev, "PMA_CMN_REFCLK_INT_MODE reg field init failed\n");
824 return PTR_ERR(wiz->pma_cmn_refclk_int_mode);
825 }
826
Matt Ranostayb7c6fbb2022-10-05 13:51:30 -0700827 if (data->pma_cmn_refclk1_int_mode) {
828 wiz->pma_cmn_refclk1_int_mode =
829 devm_regmap_field_alloc(dev, regmap, *data->pma_cmn_refclk1_int_mode);
830 if (IS_ERR(wiz->pma_cmn_refclk1_int_mode)) {
831 dev_err(dev, "PMA_CMN_REFCLK1_INT_MODE reg field init failed\n");
832 return PTR_ERR(wiz->pma_cmn_refclk1_int_mode);
833 }
834 }
835
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530836 wiz->pma_cmn_refclk_mode =
837 devm_regmap_field_alloc(dev, regmap, pma_cmn_refclk_mode);
838 if (IS_ERR(wiz->pma_cmn_refclk_mode)) {
839 dev_err(dev, "PMA_CMN_REFCLK_MODE reg field init failed\n");
840 return PTR_ERR(wiz->pma_cmn_refclk_mode);
841 }
842
843 wiz->div_sel_field[CMN_REFCLK] =
844 devm_regmap_field_alloc(dev, regmap, pma_cmn_refclk_dig_div);
845 if (IS_ERR(wiz->div_sel_field[CMN_REFCLK])) {
846 dev_err(dev, "PMA_CMN_REFCLK_DIG_DIV reg field init failed\n");
847 return PTR_ERR(wiz->div_sel_field[CMN_REFCLK]);
848 }
849
Matt Ranostay6d255cc2022-07-07 23:41:52 -0700850 if (data->pma_cmn_refclk1_dig_div) {
851 wiz->div_sel_field[CMN_REFCLK1] =
852 devm_regmap_field_alloc(dev, regmap, *data->pma_cmn_refclk1_dig_div);
853 if (IS_ERR(wiz->div_sel_field[CMN_REFCLK1])) {
854 dev_err(dev, "PMA_CMN_REFCLK1_DIG_DIV reg field init failed\n");
855 return PTR_ERR(wiz->div_sel_field[CMN_REFCLK1]);
856 }
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530857 }
858
859 wiz->mux_sel_field[PLL0_REFCLK] =
Matt Ranostay6d255cc2022-07-07 23:41:52 -0700860 devm_regmap_field_alloc(dev, regmap, *data->pll0_refclk_mux_sel);
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530861 if (IS_ERR(wiz->mux_sel_field[PLL0_REFCLK])) {
862 dev_err(dev, "PLL0_REFCLK_SEL reg field init failed\n");
863 return PTR_ERR(wiz->mux_sel_field[PLL0_REFCLK]);
864 }
865
866 wiz->mux_sel_field[PLL1_REFCLK] =
Matt Ranostay6d255cc2022-07-07 23:41:52 -0700867 devm_regmap_field_alloc(dev, regmap, *data->pll1_refclk_mux_sel);
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530868 if (IS_ERR(wiz->mux_sel_field[PLL1_REFCLK])) {
869 dev_err(dev, "PLL1_REFCLK_SEL reg field init failed\n");
870 return PTR_ERR(wiz->mux_sel_field[PLL1_REFCLK]);
871 }
872
Matt Ranostay6d255cc2022-07-07 23:41:52 -0700873 wiz->mux_sel_field[REFCLK_DIG] =
874 devm_regmap_field_alloc(dev, regmap, *data->refclk_dig_sel);
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530875 if (IS_ERR(wiz->mux_sel_field[REFCLK_DIG])) {
876 dev_err(dev, "REFCLK_DIG_SEL reg field init failed\n");
877 return PTR_ERR(wiz->mux_sel_field[REFCLK_DIG]);
878 }
879
880 for (i = 0; i < num_lanes; i++) {
881 wiz->p_enable[i] = devm_regmap_field_alloc(dev, regmap,
882 p_enable[i]);
883 if (IS_ERR(wiz->p_enable[i])) {
884 dev_err(dev, "P%d_ENABLE reg field init failed\n", i);
885 return PTR_ERR(wiz->p_enable[i]);
886 }
887
888 wiz->p_align[i] = devm_regmap_field_alloc(dev, regmap,
889 p_align[i]);
890 if (IS_ERR(wiz->p_align[i])) {
891 dev_err(dev, "P%d_ALIGN reg field init failed\n", i);
892 return PTR_ERR(wiz->p_align[i]);
893 }
894
895 wiz->p_raw_auto_start[i] =
896 devm_regmap_field_alloc(dev, regmap, p_raw_auto_start[i]);
897 if (IS_ERR(wiz->p_raw_auto_start[i])) {
898 dev_err(dev, "P%d_RAW_AUTO_START reg field init fail\n",
899 i);
900 return PTR_ERR(wiz->p_raw_auto_start[i]);
901 }
902
903 wiz->p_standard_mode[i] =
904 devm_regmap_field_alloc(dev, regmap, p_standard_mode[i]);
905 if (IS_ERR(wiz->p_standard_mode[i])) {
906 dev_err(dev, "P%d_STANDARD_MODE reg field init fail\n",
907 i);
908 return PTR_ERR(wiz->p_standard_mode[i]);
909 }
910
911 wiz->p0_fullrt_div[i] = devm_regmap_field_alloc(dev, regmap, p0_fullrt_div[i]);
912 if (IS_ERR(wiz->p0_fullrt_div[i])) {
913 dev_err(dev, "P%d_FULLRT_DIV reg field init failed\n", i);
914 return PTR_ERR(wiz->p0_fullrt_div[i]);
915 }
916
917 wiz->p_mac_div_sel0[i] =
918 devm_regmap_field_alloc(dev, regmap, p_mac_div_sel0[i]);
919 if (IS_ERR(wiz->p_mac_div_sel0[i])) {
920 dev_err(dev, "P%d_MAC_DIV_SEL0 reg field init fail\n",
921 i);
922 return PTR_ERR(wiz->p_mac_div_sel0[i]);
923 }
924
925 wiz->p_mac_div_sel1[i] =
926 devm_regmap_field_alloc(dev, regmap, p_mac_div_sel1[i]);
927 if (IS_ERR(wiz->p_mac_div_sel1[i])) {
928 dev_err(dev, "P%d_MAC_DIV_SEL1 reg field init fail\n",
929 i);
930 return PTR_ERR(wiz->p_mac_div_sel1[i]);
931 }
932 }
933
934 return 0;
935}
936
937static int wiz_clock_init(struct wiz *wiz)
938{
939 struct udevice *dev = wiz->dev;
940 unsigned long rate;
941 struct clk *clk;
942 int ret;
943
944 clk = devm_clk_get(dev, "core_ref_clk");
945 if (IS_ERR(clk)) {
946 dev_err(dev, "core_ref_clk clock not found\n");
947 ret = PTR_ERR(clk);
948 return ret;
949 }
950 wiz->input_clks[WIZ_CORE_REFCLK] = clk;
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530951
952 rate = clk_get_rate(clk);
953 if (rate >= 100000000)
954 regmap_field_write(wiz->pma_cmn_refclk_int_mode, 0x1);
955 else
956 regmap_field_write(wiz->pma_cmn_refclk_int_mode, 0x3);
957
Matt Ranostayb7c6fbb2022-10-05 13:51:30 -0700958 if (wiz->data->pma_cmn_refclk1_int_mode) {
959 clk = devm_clk_get(dev, "core_ref1_clk");
960 if (IS_ERR(clk)) {
961 dev_err(dev, "core_ref1_clk clock not found\n");
962 ret = PTR_ERR(clk);
963 return ret;
964 }
965 wiz->input_clks[WIZ_CORE_REFCLK1] = clk;
966
967 rate = clk_get_rate(clk);
968 if (rate >= 100000000)
969 regmap_field_write(wiz->pma_cmn_refclk1_int_mode, 0x1);
970 else
971 regmap_field_write(wiz->pma_cmn_refclk1_int_mode, 0x3);
972 } else {
973 /* Initialize CORE_REFCLK1 to the same clock reference to maintain old DT compatibility */
974 wiz->input_clks[WIZ_CORE_REFCLK1] = clk;
975 }
976
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +0530977 clk = devm_clk_get(dev, "ext_ref_clk");
978 if (IS_ERR(clk)) {
979 dev_err(dev, "ext_ref_clk clock not found\n");
980 ret = PTR_ERR(clk);
981 return ret;
982 }
983
984 wiz->input_clks[WIZ_EXT_REFCLK] = clk;
985 /* Initialize EXT_REFCLK1 to the same clock reference to maintain old DT compatibility */
986 wiz->input_clks[WIZ_EXT_REFCLK1] = clk;
987
988 rate = clk_get_rate(clk);
989 if (rate >= 100000000)
990 regmap_field_write(wiz->pma_cmn_refclk_mode, 0x0);
991 else
992 regmap_field_write(wiz->pma_cmn_refclk_mode, 0x2);
993
994 return 0;
995}
996
997static ofnode get_child_by_name(struct udevice *dev, const char *name)
998{
999 int l = strlen(name);
1000 ofnode node = dev_read_first_subnode(dev);
1001
1002 while (ofnode_valid(node)) {
1003 const char *child_name = ofnode_get_name(node);
1004
1005 if (!strncmp(child_name, name, l)) {
1006 if (child_name[l] == '\0' || child_name[l] == '@')
1007 return node;
1008 }
1009 node = dev_read_next_subnode(node);
1010 }
1011 return node;
1012}
1013
1014static int j721e_wiz_bind_clocks(struct wiz *wiz)
1015{
1016 struct udevice *dev = wiz->dev;
1017 struct driver *wiz_clk_drv;
1018 int i, rc;
1019
1020 wiz_clk_drv = lists_driver_lookup_name("wiz_clk");
1021 if (!wiz_clk_drv) {
1022 dev_err(dev, "Cannot find driver 'wiz_clk'\n");
1023 return -ENOENT;
1024 }
1025
1026 for (i = 0; i < WIZ_DIV_NUM_CLOCKS_10G; i++) {
1027 rc = device_bind(dev, wiz_clk_drv, clk_div_sel[i].node_name,
1028 &clk_div_sel[i], dev_ofnode(dev), NULL);
1029 if (rc) {
1030 dev_err(dev, "cannot bind driver for clock %s\n",
1031 clk_div_sel[i].node_name);
1032 }
1033 }
1034
1035 for (i = 0; i < WIZ_MUX_NUM_CLOCKS; i++) {
1036 rc = device_bind(dev, wiz_clk_drv, clk_mux_sel_10g[i].node_name,
1037 &clk_mux_sel_10g[i], dev_ofnode(dev), NULL);
1038 if (rc) {
1039 dev_err(dev, "cannot bind driver for clock %s\n",
1040 clk_mux_sel_10g[i].node_name);
1041 }
1042 }
1043
1044 return 0;
1045}
1046
1047static int j721e_wiz_bind_of_clocks(struct wiz *wiz)
1048{
1049 struct wiz_clk_mux_sel *clk_mux_sel = wiz->clk_mux_sel;
1050 struct udevice *dev = wiz->dev;
1051 enum wiz_type type = wiz->type;
1052 struct driver *div_clk_drv;
1053 struct driver *mux_clk_drv;
1054 ofnode node;
1055 int i, rc;
1056
Ravi Gunasekaran6a096e62023-05-15 16:20:40 +05301057 switch (type) {
1058 case AM64_WIZ_10G:
1059 case J784S4_WIZ_10G:
1060 case J721S2_WIZ_10G:
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +05301061 return j721e_wiz_bind_clocks(wiz);
Ravi Gunasekaran6a096e62023-05-15 16:20:40 +05301062 default:
1063 break;
1064 };
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +05301065
1066 div_clk_drv = lists_driver_lookup_name("wiz_div_clk");
1067 if (!div_clk_drv) {
1068 dev_err(dev, "Cannot find driver 'wiz_div_clk'\n");
1069 return -ENOENT;
1070 }
1071
1072 mux_clk_drv = lists_driver_lookup_name("wiz_mux_clk");
1073 if (!mux_clk_drv) {
1074 dev_err(dev, "Cannot find driver 'wiz_mux_clk'\n");
1075 return -ENOENT;
1076 }
1077
1078 for (i = 0; i < wiz->clk_div_sel_num; i++) {
1079 node = get_child_by_name(dev, clk_div_sel[i].node_name);
1080 if (!ofnode_valid(node)) {
1081 dev_err(dev, "cannot find node for clock %s\n",
1082 clk_div_sel[i].node_name);
1083 continue;
1084 }
1085 rc = device_bind(dev, div_clk_drv, clk_div_sel[i].node_name,
1086 &clk_div_sel[i], node, NULL);
1087 if (rc) {
1088 dev_err(dev, "cannot bind driver for clock %s\n",
1089 clk_div_sel[i].node_name);
1090 }
1091 }
1092
1093 for (i = 0; i < WIZ_MUX_NUM_CLOCKS; i++) {
1094 node = get_child_by_name(dev, clk_mux_sel[i].node_name);
1095 if (!ofnode_valid(node)) {
1096 dev_err(dev, "cannot find node for clock %s\n",
1097 clk_mux_sel[i].node_name);
1098 continue;
1099 }
1100 rc = device_bind(dev, mux_clk_drv, clk_mux_sel[i].node_name,
1101 &clk_mux_sel[i], node, NULL);
1102 if (rc) {
1103 dev_err(dev, "cannot bind driver for clock %s\n",
1104 clk_mux_sel[i].node_name);
1105 }
1106 }
1107
1108 return 0;
1109}
1110
1111static int j721e_wiz_bind_reset(struct udevice *dev)
1112{
1113 int rc;
1114 struct driver *drv;
1115
1116 drv = lists_driver_lookup_name("wiz-reset");
1117 if (!drv) {
1118 dev_err(dev, "Cannot find driver 'wiz-reset'\n");
1119 return -ENOENT;
1120 }
1121
1122 rc = device_bind(dev, drv, "wiz-reset", NULL, dev_ofnode(dev), NULL);
1123 if (rc) {
1124 dev_err(dev, "cannot bind driver for wiz-reset\n");
1125 return rc;
1126 }
1127
1128 return 0;
1129}
1130
1131static int j721e_wiz_bind(struct udevice *dev)
1132{
1133 dm_scan_fdt_dev(dev);
1134
1135 return 0;
1136}
1137
1138static int wiz_get_lane_phy_types(struct udevice *dev, struct wiz *wiz)
1139{
1140 ofnode child, serdes;
1141
1142 serdes = get_child_by_name(dev, "serdes");
1143 if (!ofnode_valid(serdes)) {
1144 dev_err(dev, "%s: Getting \"serdes\"-node failed\n", __func__);
1145 return -EINVAL;
1146 }
1147
1148 ofnode_for_each_subnode(child, serdes) {
1149 u32 reg, num_lanes = 1, phy_type = PHY_NONE;
1150 int ret, i;
1151
1152 ret = ofnode_read_u32(child, "reg", &reg);
1153 if (ret) {
1154 dev_err(dev, "%s: Reading \"reg\" from failed: %d\n",
1155 __func__, ret);
1156 return ret;
1157 }
1158 ofnode_read_u32(child, "cdns,num-lanes", &num_lanes);
1159 ofnode_read_u32(child, "cdns,phy-type", &phy_type);
1160
1161 dev_dbg(dev, "%s: Lanes %u-%u have phy-type %u\n", __func__,
1162 reg, reg + num_lanes - 1, phy_type);
1163
Sinthu Rajac5df75f2023-03-13 18:12:23 +05301164 for (i = reg; i < reg + num_lanes; i++) {
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +05301165 wiz->lane_phy_type[i] = phy_type;
Sinthu Rajac5df75f2023-03-13 18:12:23 +05301166 wiz->master_lane_num[i] = reg;
1167 }
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +05301168 }
1169
1170 return 0;
1171}
1172
1173static int j721e_wiz_probe(struct udevice *dev)
1174{
1175 struct wiz *wiz = dev_get_priv(dev);
1176 struct ofnode_phandle_args args;
1177 unsigned int val;
1178 int rc, i;
1179 ofnode node;
1180 struct regmap *regmap;
1181 u32 num_lanes;
1182
1183 node = get_child_by_name(dev, "serdes");
1184
1185 if (!ofnode_valid(node)) {
1186 dev_err(dev, "Failed to get SERDES child DT node\n");
1187 return -ENODEV;
1188 }
1189
1190 rc = regmap_init_mem(node, &regmap);
1191 if (rc) {
1192 dev_err(dev, "Failed to get memory resource\n");
1193 return rc;
1194 }
1195 rc = dev_read_u32(dev, "num-lanes", &num_lanes);
1196 if (rc) {
1197 dev_err(dev, "Failed to read num-lanes property\n");
1198 goto err_addr_to_resource;
1199 }
1200
1201 if (num_lanes > WIZ_MAX_LANES) {
1202 dev_err(dev, "Cannot support %d lanes\n", num_lanes);
1203 goto err_addr_to_resource;
1204 }
1205
1206 wiz->gpio_typec_dir = devm_gpiod_get_optional(dev, "typec-dir",
1207 GPIOD_IS_IN);
1208 if (IS_ERR(wiz->gpio_typec_dir)) {
1209 rc = PTR_ERR(wiz->gpio_typec_dir);
1210 dev_err(dev, "Failed to request typec-dir gpio: %d\n", rc);
1211 goto err_addr_to_resource;
1212 }
1213
1214 rc = dev_read_phandle_with_args(dev, "power-domains", "#power-domain-cells", 0, 0, &args);
1215 if (rc) {
1216 dev_err(dev, "Failed to get power domain: %d\n", rc);
1217 goto err_addr_to_resource;
1218 }
1219
1220 wiz->id = args.args[0];
1221 wiz->regmap = regmap;
1222 wiz->num_lanes = num_lanes;
1223 wiz->dev = dev;
1224 wiz->clk_div_sel = clk_div_sel;
Matt Ranostay6d255cc2022-07-07 23:41:52 -07001225
1226 wiz->data = (struct wiz_data *)dev_get_driver_data(dev);
1227 wiz->type = wiz->data->type;
1228
1229 wiz->clk_mux_sel = (struct wiz_clk_mux_sel *)wiz->data->clk_mux_sel;
1230 wiz->clk_div_sel_num = wiz->data->clk_div_sel_num;
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +05301231
1232 rc = wiz_get_lane_phy_types(dev, wiz);
1233 if (rc) {
1234 dev_err(dev, "Failed to get lane PHY types\n");
1235 goto err_addr_to_resource;
1236 }
1237
1238 rc = wiz_regfield_init(wiz);
1239 if (rc) {
1240 dev_err(dev, "Failed to initialize regfields\n");
1241 goto err_addr_to_resource;
1242 }
1243
1244 for (i = 0; i < wiz->num_lanes; i++) {
1245 regmap_field_read(wiz->p_enable[i], &val);
1246 if (val & (P_ENABLE | P_ENABLE_FORCE)) {
1247 dev_err(dev, "SERDES already configured\n");
1248 rc = -EBUSY;
1249 goto err_addr_to_resource;
1250 }
1251 }
1252
1253 rc = j721e_wiz_bind_of_clocks(wiz);
1254 if (rc) {
1255 dev_err(dev, "Failed to bind clocks\n");
1256 goto err_addr_to_resource;
1257 }
1258
1259 rc = j721e_wiz_bind_reset(dev);
1260 if (rc) {
1261 dev_err(dev, "Failed to bind reset\n");
1262 goto err_addr_to_resource;
1263 }
1264
1265 rc = wiz_clock_init(wiz);
1266 if (rc) {
1267 dev_warn(dev, "Failed to initialize clocks\n");
1268 goto err_addr_to_resource;
1269 }
1270
1271 rc = wiz_init(wiz);
1272 if (rc) {
1273 dev_err(dev, "WIZ initialization failed\n");
1274 goto err_addr_to_resource;
1275 }
1276
1277 return 0;
1278
1279err_addr_to_resource:
1280 free(regmap);
1281
1282 return rc;
1283}
1284
1285static int j721e_wiz_remove(struct udevice *dev)
1286{
1287 struct wiz *wiz = dev_get_priv(dev);
1288
1289 if (wiz->regmap)
1290 free(wiz->regmap);
1291
1292 return 0;
1293}
1294
1295static const struct udevice_id j721e_wiz_ids[] = {
1296 {
Matt Ranostay6d255cc2022-07-07 23:41:52 -07001297 .compatible = "ti,j721e-wiz-16g", .data = (ulong)&j721e_16g_data,
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +05301298 },
1299 {
Matt Ranostay6d255cc2022-07-07 23:41:52 -07001300 .compatible = "ti,j721e-wiz-10g", .data = (ulong)&j721e_10g_data,
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +05301301 },
1302 {
Matt Ranostay6d255cc2022-07-07 23:41:52 -07001303 .compatible = "ti,am64-wiz-10g", .data = (ulong)&am64_10g_data,
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +05301304 },
Matt Ranostayb7c6fbb2022-10-05 13:51:30 -07001305 {
1306 .compatible = "ti,j784s4-wiz-10g", .data = (ulong)&j784s4_wiz_10g,
1307 },
Ravi Gunasekaran6a096e62023-05-15 16:20:40 +05301308 {
1309 .compatible = "ti,j721s2-wiz-10g", .data = (ulong)&j721s2_10g_data,
1310 },
Jean-Jacques Hiblot065788e2021-07-21 21:28:38 +05301311 {}
1312};
1313
1314U_BOOT_DRIVER(phy_j721e_wiz) = {
1315 .name = "phy-j721e-wiz",
1316 .id = UCLASS_NOP,
1317 .of_match = j721e_wiz_ids,
1318 .bind = j721e_wiz_bind,
1319 .probe = j721e_wiz_probe,
1320 .remove = j721e_wiz_remove,
1321 .priv_auto = sizeof(struct wiz),
1322 .flags = DM_FLAG_LEAVE_PD_ON,
1323};