Skip to content

Commit dc0092c

Browse files
3x380Vgregkh
authored andcommitted
usb: dwc3: dwc3-octeon: Move node parsing into driver probe
Parse and verify device tree node first, then setup UCTL bridge using verified values. This avoids needless allocations in case DT misconfiguration was found in the middle of setup. Signed-off-by: Ladislav Michl <[email protected]> Acked-by: Thinh Nguyen <[email protected]> Link: https://lore.kernel.org/r/ZMd/x3MHA4/QowMO@lenoch Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent c611016 commit dc0092c

File tree

1 file changed

+60
-75
lines changed

1 file changed

+60
-75
lines changed

drivers/usb/dwc3/dwc3-octeon.c

Lines changed: 60 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -261,69 +261,15 @@ static int dwc3_octeon_get_divider(void)
261261
}
262262

263263
static int dwc3_octeon_setup(struct dwc3_octeon *octeon,
264+
int ref_clk_sel, int ref_clk_fsel, int mpll_mul,
264265
int power_gpio, int power_active_low)
265266
{
266-
int i, div, mpll_mul, ref_clk_fsel, ref_clk_sel = 2;
267-
u32 clock_rate;
268267
u64 val;
268+
int div;
269269
struct device *dev = octeon->dev;
270270
void __iomem *uctl_ctl_reg = octeon->base + USBDRD_UCTL_CTL;
271271
void __iomem *uctl_host_cfg_reg = octeon->base + USBDRD_UCTL_HOST_CFG;
272272

273-
if (dev->of_node) {
274-
const char *ss_clock_type;
275-
const char *hs_clock_type;
276-
277-
i = of_property_read_u32(dev->of_node,
278-
"refclk-frequency", &clock_rate);
279-
if (i) {
280-
dev_err(dev, "No UCTL \"refclk-frequency\"\n");
281-
return -EINVAL;
282-
}
283-
i = of_property_read_string(dev->of_node,
284-
"refclk-type-ss", &ss_clock_type);
285-
if (i) {
286-
dev_err(dev, "No UCTL \"refclk-type-ss\"\n");
287-
return -EINVAL;
288-
}
289-
i = of_property_read_string(dev->of_node,
290-
"refclk-type-hs", &hs_clock_type);
291-
if (i) {
292-
dev_err(dev, "No UCTL \"refclk-type-hs\"\n");
293-
return -EINVAL;
294-
}
295-
if (strcmp("dlmc_ref_clk0", ss_clock_type) == 0) {
296-
if (strcmp(hs_clock_type, "dlmc_ref_clk0") == 0)
297-
ref_clk_sel = 0;
298-
else if (strcmp(hs_clock_type, "pll_ref_clk") == 0)
299-
ref_clk_sel = 2;
300-
else
301-
dev_warn(dev, "Invalid HS clock type %s, using pll_ref_clk instead\n",
302-
hs_clock_type);
303-
} else if (strcmp(ss_clock_type, "dlmc_ref_clk1") == 0) {
304-
if (strcmp(hs_clock_type, "dlmc_ref_clk1") == 0)
305-
ref_clk_sel = 1;
306-
else if (strcmp(hs_clock_type, "pll_ref_clk") == 0)
307-
ref_clk_sel = 3;
308-
else {
309-
dev_warn(dev, "Invalid HS clock type %s, using pll_ref_clk instead\n",
310-
hs_clock_type);
311-
ref_clk_sel = 3;
312-
}
313-
} else
314-
dev_warn(dev, "Invalid SS clock type %s, using dlmc_ref_clk0 instead\n",
315-
ss_clock_type);
316-
317-
if ((ref_clk_sel == 0 || ref_clk_sel == 1) &&
318-
(clock_rate != 100000000))
319-
dev_warn(dev, "Invalid UCTL clock rate of %u, using 100000000 instead\n",
320-
clock_rate);
321-
322-
} else {
323-
dev_err(dev, "No USB UCTL device node\n");
324-
return -EINVAL;
325-
}
326-
327273
/*
328274
* Step 1: Wait for all voltages to be stable...that surely
329275
* happened before starting the kernel. SKIP
@@ -367,24 +313,6 @@ static int dwc3_octeon_setup(struct dwc3_octeon *octeon,
367313
val &= ~USBDRD_UCTL_CTL_REF_CLK_SEL;
368314
val |= FIELD_PREP(USBDRD_UCTL_CTL_REF_CLK_SEL, ref_clk_sel);
369315

370-
ref_clk_fsel = 0x07;
371-
switch (clock_rate) {
372-
default:
373-
dev_warn(dev, "Invalid ref_clk %u, using 100000000 instead\n",
374-
clock_rate);
375-
fallthrough;
376-
case 100000000:
377-
mpll_mul = 0x19;
378-
if (ref_clk_sel < 2)
379-
ref_clk_fsel = 0x27;
380-
break;
381-
case 50000000:
382-
mpll_mul = 0x32;
383-
break;
384-
case 125000000:
385-
mpll_mul = 0x28;
386-
break;
387-
}
388316
val &= ~USBDRD_UCTL_CTL_REF_CLK_FSEL;
389317
val |= FIELD_PREP(USBDRD_UCTL_CTL_REF_CLK_FSEL, ref_clk_fsel);
390318

@@ -483,8 +411,64 @@ static int dwc3_octeon_probe(struct platform_device *pdev)
483411
struct device *dev = &pdev->dev;
484412
struct device_node *node = dev->of_node;
485413
struct dwc3_octeon *octeon;
414+
const char *hs_clock_type, *ss_clock_type;
415+
int ref_clk_sel, ref_clk_fsel, mpll_mul;
486416
int power_active_low, power_gpio;
487417
int err, len;
418+
u32 clock_rate;
419+
420+
if (of_property_read_u32(node, "refclk-frequency", &clock_rate)) {
421+
dev_err(dev, "No UCTL \"refclk-frequency\"\n");
422+
return -EINVAL;
423+
}
424+
if (of_property_read_string(node, "refclk-type-ss", &ss_clock_type)) {
425+
dev_err(dev, "No UCTL \"refclk-type-ss\"\n");
426+
return -EINVAL;
427+
}
428+
if (of_property_read_string(node, "refclk-type-hs", &hs_clock_type)) {
429+
dev_err(dev, "No UCTL \"refclk-type-hs\"\n");
430+
return -EINVAL;
431+
}
432+
433+
ref_clk_sel = 2;
434+
if (strcmp("dlmc_ref_clk0", ss_clock_type) == 0) {
435+
if (strcmp(hs_clock_type, "dlmc_ref_clk0") == 0)
436+
ref_clk_sel = 0;
437+
else if (strcmp(hs_clock_type, "pll_ref_clk"))
438+
dev_warn(dev, "Invalid HS clock type %s, using pll_ref_clk instead\n",
439+
hs_clock_type);
440+
} else if (strcmp(ss_clock_type, "dlmc_ref_clk1") == 0) {
441+
if (strcmp(hs_clock_type, "dlmc_ref_clk1") == 0) {
442+
ref_clk_sel = 1;
443+
} else {
444+
ref_clk_sel = 3;
445+
if (strcmp(hs_clock_type, "pll_ref_clk"))
446+
dev_warn(dev, "Invalid HS clock type %s, using pll_ref_clk instead\n",
447+
hs_clock_type);
448+
}
449+
} else {
450+
dev_warn(dev, "Invalid SS clock type %s, using dlmc_ref_clk0 instead\n",
451+
ss_clock_type);
452+
}
453+
454+
ref_clk_fsel = 0x07;
455+
switch (clock_rate) {
456+
default:
457+
dev_warn(dev, "Invalid ref_clk %u, using 100000000 instead\n",
458+
clock_rate);
459+
fallthrough;
460+
case 100000000:
461+
mpll_mul = 0x19;
462+
if (ref_clk_sel < 2)
463+
ref_clk_fsel = 0x27;
464+
break;
465+
case 50000000:
466+
mpll_mul = 0x32;
467+
break;
468+
case 125000000:
469+
mpll_mul = 0x28;
470+
break;
471+
}
488472

489473
power_gpio = DWC3_GPIO_POWER_NONE;
490474
power_active_low = 0;
@@ -515,7 +499,8 @@ static int dwc3_octeon_probe(struct platform_device *pdev)
515499
if (IS_ERR(octeon->base))
516500
return PTR_ERR(octeon->base);
517501

518-
err = dwc3_octeon_setup(octeon, power_gpio, power_active_low);
502+
err = dwc3_octeon_setup(octeon, ref_clk_sel, ref_clk_fsel, mpll_mul,
503+
power_gpio, power_active_low);
519504
if (err)
520505
return err;
521506

0 commit comments

Comments
 (0)