From: Benjamin Robin <benjamin.robin@bootlin.com>
To: sashiko-reviews@lists.linux.dev
Cc: devicetree@vger.kernel.org, linux-phy@lists.infradead.org,
robh@kernel.org, conor+dt@kernel.org, vkoul@kernel.org,
neil.armstrong@linaro.org, olteanv@gmail.com
Subject: Re: [PATCH 4/4] phy: cadence-torrent: rewrite Torrent PHY driver
Date: Wed, 02 Dec 2026 17:02:18 +0100 [thread overview]
Message-ID: <5K7Md9-NTQeBZUihR_WipA@bootlin.com> (raw)
In-Reply-To: <sashiko-outbox-225247@kernel.org>
On Friday, November 20, 2026 at 2:33 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 14 potential
> issues to consider:
> - [High] phy: cadence-torrent: 32-bit shift overflow in
> tp_write_pll_regs()
> - [High] phy: cadence-torrent: CDB register access before apb_rst
> deassertion
> - [High] phy: cadence-torrent: DP init and configure break cdns-mhdp8546
> consumer
> - [High] phy: cadence-torrent: missing mlane shift in
> tp_dptx_set_pll_en() and lanes
> - [High] phy: cadence-torrent: nb_phy_on underflow in tp_phy_off() when
> preconfigured
> - [High] phy: cadence-torrent: tp_resume_noirq() leaves active links
> held in reset
> - [Medium] phy: cadence-torrent: REV_R110 enum typo in
> tp_write_pll_cp_padj()
> - [Medium] phy: cadence-torrent: unreachable FBR mode in
> tp_calc_pcie_pll_fb_div_mode()
> - [Medium] phy: cadence-torrent: stale SSC and divider regs in
> tp_write_pll_regs()
> - [Medium] phy: cadence-torrent: unbalanced clk disable in
> tp_phy_cleanup()
> - [Medium] phy: cadence-torrent: skipped refclk check in
> tp_ref_clks_init()
> - [Medium] phy: cadence-torrent: missed PCIe ML flag in
> tp_check_multiple_pcie_links()
> - [Medium] phy: cadence-torrent: rejected standard swap with clk1 in
> tp_find_link_cfg()
> - [Medium] phy: cadence-torrent: unchecked tp_dp_run() and missing reset
> rollback
> --
>
> commit 3480ec972d0cd2d542d547f1d45d7f4f38415bc8
> Author: Benjamin Robin <benjamin.robin@bootlin.com>
>
> phy: cadence-torrent: rewrite Torrent PHY driver
>
> This commit rewrites the Cadence Torrent PHY driver to compute clock and
> PLL register values at runtime and look up remaining protocol, revision,
> and process-node register settings per register instead of using static
> register tables.
>
> > diff --git a/drivers/phy/cadence/phy-cadence-torrent.c b/drivers/phy/cadence/phy-cadence-torrent.c
> > index 1e178aa2ffd76..74a65f0f4b242 100644
> > --- a/drivers/phy/cadence/phy-cadence-torrent.c
> > +++ b/drivers/phy/cadence/phy-cadence-torrent.c
>
> [ ... ]
>
> > @@ -394,5453 +515,4629 @@ enum phy_powerstate {
> > POWERSTATE_A3 = 3,
> > };
> >
> > +static const struct tp_link_cfg tp_link_cfgs[] = {
> > + {
> > + /* PLL 0 M0: PCIe, PLL 0 M1: PCIe, PLL 1: None */
> > + .std0 = STD0_PCIE_SL, .std1 = STD1_NONE,
> > + .pll_freq = { PLL_10_00_GHZ, PLL_8_00_GHZ, PLL_8_00_GHZ },
> > + .phy_pll_cfg = 0x0001,
> > + .pll_clk_sel = { 0x0601, 0x0400, 0x0400 },
> > + .xcvr_diag_plldrc_ctrl = { 0x0012, 0x0012 },
> > + .xcvr_diag_hsclk_sel = { 0x0000, 0x0000 },
> > + .xcvr_diag_hsclk_div = { 0x0001, 0x0001 },
> > + },
>
> [ ... ]
>
> > +static enum tp_pll_fb_div_mode tp_calc_pcie_pll_fb_div_mode(const struct tp_phy *tp_phy)
> > +{
> > + u32 tech_tag = tp_phy->tech_tag;
> > + enum tp_revision rev = tp_phy->rev;
> > + enum tp_pll_fb_div_mode div_mode;
> > +
> > + if (tech_tag & (TECH_TSMC_16 | TECH_TSMC_12)) {
> > + if (rev == REV_R100) {
> > + if (tp_phy->plls[PLL1_MODE0].freq_khz == 0)
> > + div_mode = PLL_FB_DIV_MODE_FBR;
> > + else
> > + div_mode = PLL_FB_DIV_MODE_D32;
> > + } else {
>
> [Severity: Medium]
> Can tp_phy->plls[PLL1_MODE0].freq_khz ever be 0 here?
>
> Every entry in tp_link_cfgs[], including single-link PCIe (STD0_PCIE_SL
> with STD1_NONE), sets pll_freq[PLL1_MODE0] to a non-zero frequency
> (PLL_8_00_GHZ) rather than PLL_UNUSED. Because tp_init_plls_data()
> populates plls[PLL1_MODE0].freq_khz from pll_freq[PLL1_MODE0] before
> calling tp_calc_pll_fb_div_modes(), single-link PCIe on TSMC 16nm/12nm
> REV_R100 hardware always selects PLL_FB_DIV_MODE_D32 instead of
> PLL_FB_DIV_MODE_FBR, misprogramming the PLL control, VCO calibration, and
> lock registers.
It was indeed a bug, and fixed in v2. If there is no second standard
we should select PLL_FB_DIV_MODE_FBR.
> [ ... ]
>
> > +static void tp_write_pll_cp_padj(const struct tp_phy *tp_phy, enum tp_pll_id pll_id)
> > +{
> > + u32 tech_tag = tp_phy->tech_tag;
> > + u32 rev_msk = BIT(tp_phy->rev);
> > + const struct tp_pll_data *pll = &tp_phy->plls[pll_id];
> > + u16 val;
> > +
> > + /* PLL charge pump proportional path adjust register */
> > + if (pll->freq_khz < 8000000) {
> > + val = 0x0B17;
> > + } else if (pll->fractional_mode) {
> > + if (tech_tag & TECH_TSMC_7)
> > + val = 0x0B17;
> > + else if (tech_tag & (TECH_SS_5 | TECH_SS_7))
> > + val = 0x0309;
> > + else
> > + val = 0x0509; /* TSMC_5, TSMC_12, TSMC_16, TSMC_16_0805 */
> > + } else if (pll->freq_khz >= 10000000) {
> > + if (tech_tag & TECH_TSMC_7)
> > + val = (rev_msk & (REV_MSK_R100 | REV_MSK_R110)) ? 0x0023 : 0x0035;
> > + else if (tech_tag & TECH_TSMC_5)
> > + val = (pll_id == PLL0_MODE0) ? 0x002D : 0x0035;
> > + else if (tech_tag & (TECH_SS_5 | TECH_SS_7))
> > + val = 0x013E;
> > + else
> > + val = 0x0028; /* TSMC_12, TSMC_16, TSMC_16_0805 */
> > + } else {
> > + if (tech_tag & TECH_TSMC_16)
> > + val = (rev_msk & REV_MSK_R100) ? 0x0028 : 0x0128;
> > + else if (tech_tag & TECH_TSMC_16_0805)
> > + val = 0x0128;
> > + else if (tech_tag & TECH_TSMC_12)
> > + val = 0x0028;
> > + else if (tech_tag & TECH_TSMC_7)
> > + val = (rev_msk & (REV_MSK_R100 | REV_R110)) ? 0x001D : 0x0031;
>
> [Severity: Medium]
> Should REV_R110 be REV_MSK_R110 in the TECH_TSMC_7 check? REV_R110 is
> the enum value 1 (equal to REV_MSK_R100) rather than BIT(REV_R110), so R110
> hardware (rev_msk == 0x2) never matches and programs
> CMN_PDIAG_PLLx_CP_PADJ_Mx with 0x0031 instead of 0x001D for 8 GHz
> integer-mode PLLs.
It was indeed a bug, and fixed in v2. Use the correct macro.
> [ ... ]
>
> > +static void tp_write_pll_fd_div_regs(const struct tp_phy *tp_phy, enum tp_pll_id pll_id,
> > + u32 div_ratio)
> > +{
> > + const struct tp_reg_base *base = &tp_phy->reg_common_cdb;
> > + const struct tp_pll_data *pll = &tp_phy->plls[pll_id];
> > + u32 tech_tag = tp_phy->tech_tag;
> > + u16 high_div;
> > + u16 low_div;
> > +
> > + /* In fractional mode, we use default/reset value */
> > + if (pll->fractional_mode)
> > + return;
> > +
> > + if (pll_id == PLL0_MODE0 && pll->ref_clk_khz == 100000 && pll->freq_khz == 10000000) {
> > + /* 100 MHz refclk, 10 GHz PLL */
> > + if (tech_tag & (TECH_TSMC_12 | TECH_TSMC_16 | TECH_TSMC_16_0805)) {
> > + high_div = 30;
> > + low_div = 12;
> > + } else {
> > + high_div = 36;
> > + low_div = 6;
> > + }
> > + } else if (pll->ref_clk_khz == 100000 && pll->freq_khz == 8000000) {
> > + /* 100 MHz refclk, 8 GHz PLL */
> > + if (tech_tag & (TECH_TSMC_12 | TECH_TSMC_16 | TECH_TSMC_16_0805)) {
> > + high_div = 16;
> > + low_div = 16;
> > + } else {
> > + high_div = 28;
> > + low_div = 4;
> > + }
> > + } else {
> > + /* Generic calculations */
> > +
> > + /* PLL high integer divider val: EVEN(div_ratio_2 * 67/100) - 4 */
> > + high_div = DIV_ROUND_UP((div_ratio >> 1) * 67U, 100U);
> > + high_div -= (high_div & 1U) ? 3U : 4U;
> > +
> > + /* PLL low integer divider val: div_ratio_2 - 8 - high_div */
> > + low_div = (div_ratio >> 1) - 8 - high_div;
> > + }
> > +
> > + tp_reg_write(base, CMN_PLLx_DSM_FBH_OVRD_Mx(pll_id), high_div);
> > + tp_reg_write(base, CMN_PLLx_DSM_FBL_OVRD_Mx(pll_id), low_div);
> > +}
> > +
> > +static void tp_write_pll_itrim_reg(const struct tp_phy *tp_phy, enum tp_pll_id pll_id)
> > +{
> > + const struct tp_reg_base *base = &tp_phy->reg_common_cdb;
> > + const struct tp_pll_data *pll = &tp_phy->plls[pll_id];
> > +
> > + if (tp_phy->tech_tag & TECH_TSMC_ALL) {
> > + u16 val = (pll->freq_khz >= 10000000) ? 0x003F : 0x007F;
> > +
> > + tp_reg_write(base, CMN_PDIAG_PLLx_ITRIM_Mx(pll_id), val);
> > + }
> > +}
> > +
> > +static void tp_write_pll_regs(const struct tp_phy *tp_phy, enum tp_pll_id pll_id)
> > +{
> > + const struct tp_reg_base *base = &tp_phy->reg_common_cdb;
> > + const struct tp_pll_data *pll = &tp_phy->plls[pll_id];
> > + u32 fd_div_prescale;
> > + u32 div_ratio;
> > + u16 fracdivl;
> > + u16 val;
> > +
> > + if (pll_id != PLL0_MODE1)
> > + tp_write_pll_vco_regs(tp_phy, pll_id);
> > +
> > + /* Divide ratio */
> > + div_ratio = pll->freq_khz / pll->ref_clk_khz;
> > +
> > + /* PLL feedback divider prescale */
> > + fd_div_prescale = ((!pll->fractional_mode) || (div_ratio > 512)) ? 1 : 0;
> > +
> > + /* PLL integer divider value */
> > + val = (pll->fractional_mode) ? (div_ratio >> fd_div_prescale) : 0;
> > + tp_reg_write(base, CMN_PLLx_INTDIV_Mx(pll_id), val);
> > +
> > + /* PLL fractional divider value */
> > + if (pll->fractional_mode) {
> > + u32 den = pll->ref_clk_khz << fd_div_prescale;
> > + u32 rem = pll->freq_khz % den;
> > +
> > + fracdivl = DIV_ROUND_CLOSEST(rem << 16, den);
> > + } else {
> > + fracdivl = 0;
> > + }
> > +
> > + tp_reg_write(base, CMN_PLLx_FRACDIVL_Mx(pll_id), fracdivl);
> > + tp_reg_write(base, CMN_PLLx_FRACDIVH_Mx(pll_id), pll->fractional_mode ? 2 : 0);
> > +
> > + /* PLL high threshold value */
> > + if (pll->fractional_mode) {
> > + /* EVEN(CEIL((fracdivl/65536 + (div_ratio >> fd_div_prescale)) * 2 / 3)) */
> > + u32 num = fracdivl + (div_ratio << (16 - fd_div_prescale));
>
> [Severity: High]
> Can rem << 16 overflow 32-bit u32 when computing fracdivl in
> tp_write_pll_regs()?
>
> When rem >= 65535 (for example, DisplayPort at the default 8100 Mbps rate
> with a supported 156.25 MHz reference clock, where den = 156250 and
> rem = 131250), rem << 16 overflows u32 and corrupts both
> CMN_PLLx_FRACDIVL_Mx and CMN_PLLx_HIGH_THR_Mx (producing fracdivl = 0x004B
> instead of 0xD70A). Also, when fd_div_prescale == 1 and div_ratio is odd
> (e.g. 20 MHz refclk with 10.3125 GHz USXGMII where div_ratio = 515),
> div_ratio << (16 - fd_div_prescale) retains bit 0 of div_ratio instead of
> using (div_ratio >> fd_div_prescale) << 16, double-counting the odd
> remainder already included in fracdivl.
It was indeed a bug, and fixed in v2. This part of the computation is now
done in 64 bits. Also the computation when fd_div_prescale == 1 and
div_ratio is odd was fixed.
> > +
> > + val = DIV_ROUND_UP(num, 3 << 15);
> > + if (val & 1)
> > + val++;
> > + } else {
> > + val = 0;
> > + }
> > +
> > + tp_reg_write(base, CMN_PLLx_HIGH_THR_Mx(pll_id), val);
> > +
> > + /* PLL delta sigma modulator diagnostics register */
> > + tp_reg_write(base, CMN_PLLx_DSM_DIAG_Mx(pll_id),
> > + pll->fractional_mode ? 0x0004 : 0x4004);
> > +
> > + /* PLL high and low integer divider value */
> > + tp_write_pll_fd_div_regs(tp_phy, pll_id, div_ratio);
> > +
> > + if (pll->ssc)
> > + tp_write_ssc_ctrl(tp_phy, pll_id, fd_div_prescale);
>
> [Severity: Medium]
> Does dynamic DisplayPort rate reconfiguration leave stale values in
> CMN_PLLx_SS_CTRL1_Mx..CTRL4_Mx and CMN_PLLx_DSM_FBH/FBL_OVRD_Mx?
>
> When tp_dp_update_pll_freq() -> tp_write_pll_regs() reconfigures a DP link
> from SSC enabled to SSC disabled, skipping tp_write_ssc_ctrl() leaves
> CMN_PLLx_SS_CTRL1_Mx..CTRL4_Mx enabled in hardware while VCO and lock
> counters are programmed for non-SSC operation. Similarly, if a DP PLL
> switches from integer mode (e.g. 2.7/5.4 Gbps with 100 MHz refclk and no
> SSC) to fractional mode, returning early in tp_write_pll_fd_div_regs()
> leaves the previous integer divider overrides in CMN_PLLx_DSM_FBH_OVRD_Mx
> and CMN_PLLx_DSM_FBL_OVRD_Mx instead of restoring their reset values.
It was indeed a bug, and fixed in v2. CMN_PLLx_SS_CTRL1_Mx is now written
to 2 when SSC is disabled.
But returning early in tp_write_pll_fd_div_regs() is fine, a comment was
added when returning early to explain why.
> [ ... ]
>
> > +static int tp_dptx_set_pll_en(const struct tp_phy *tp_phy, u32 num_lanes, bool enable)
> > +{
> > + void __iomem *addr;
> > + u32 pll_ack_val;
> > + u32 rd_val;
> > + int ret;
> > +
> > + /* Used to determine which bits to check for or enable in PHY_PMA_XCVR_PLLCLK_EN reg */
> > + u32 pll_bits;
> > + /* Used to enable or disable lanes. */
> > + u32 pll_val;
> > +
> > + /* Select reg value and mask, depending on enabled lane count. */
> > + pll_val = tp_reg_dptx_read(tp_phy, PHY_PMA_XCVR_PLLCLK_EN);
> > +
> > + if (enable) {
> > + pll_bits = ((1 << num_lanes) - 1);
> > + pll_val |= pll_bits;
> > + pll_ack_val = pll_bits;
> > + } else {
> > + pll_bits = ((1 << num_lanes) - 1);
> > + pll_val &= (~pll_bits);
> > + pll_ack_val = 0;
> > + }
>
> [ ... ]
>
> > +static void tp_dp_set_lanes(const struct tp_phy *tp_phy, struct tp_link *link, u32 num_lanes)
> > +{
> > + u32 pma_tx_elec_idle_mask;
> > + u8 used_lane_mask;
> > + u8 all_lane_mask;
> > + u32 pll_clk_en;
> > + u32 pwr_state;
> > + u32 value;
> > + u32 mlane = link->mlane;
> > +
> > + used_lane_mask = ((1U << num_lanes) - 1) << mlane;
> > + all_lane_mask = ((1U << link->max_lanes) - 1) << mlane;
>
> [ ... ]
>
> > + /* Clear requested power state set PLL clock enable to 0 on all used lanes. */
> > + pwr_state = tp_reg_dptx_read(tp_phy, PHY_PMA_XCVR_POWER_STATE_REQ);
> > + pll_clk_en = tp_reg_dptx_read(tp_phy, PHY_PMA_XCVR_PLLCLK_EN);
> > +
> > + for (u32 i = 0; i < num_lanes; ++i)
> > + pwr_state &= ~(PMA_XCVR_POWER_STATE_REQ_LN_MASK << PHY_POWER_STATE_LN(mlane + i));
> > +
> > + pll_clk_en &= ~used_lane_mask;
> > +
> > + tp_reg_dptx_write(tp_phy, PHY_PMA_XCVR_POWER_STATE_REQ, pwr_state);
> > + tp_reg_dptx_write(tp_phy, PHY_PMA_XCVR_PLLCLK_EN, pll_clk_en);
> > +
> > + /* release master lane reset */
> > + value |= 1 << mlane;
> > + tp_reg_dptx_write(tp_phy, PHY_RESET, value);
> > +
> > + /* update the number of used lanes */\n+ link->num_lanes = num_lanes;
> > +}
>
> [Severity: High]
> Should pll_bits in tp_dptx_set_pll_en() be shifted by link->mlane?
>
> Unlike tp_dp_set_lanes() and tp_dptx_set_power_state(), tp_dptx_set_pll_en()
> computes pll_bits = ((1 << num_lanes) - 1) without shifting by link->mlane,
> so any DisplayPort link with mlane > 0 modifies PHY_PMA_XCVR_PLLCLK_EN for
> lane 0 and times out polling PHY_PMA_XCVR_PLLCLK_EN_ACK.
>
> In addition, tp_dp_set_lanes() only clears PHY_PMA_XCVR_POWER_STATE_REQ and
> PHY_PMA_XCVR_PLLCLK_EN across used_lane_mask instead of all_lane_mask,
> leaving disabled lanes' PLLCLK_EN bits set when reducing the lane count.
> And when both set_lanes and set_rate are requested in tp_phy_configure(),
> running tp_dp_set_lanes() before tp_dp_set_rate() resets the lanes, clears
> PLLCLK_EN, and updates link->num_lanes before tp_dp_set_rate() transitions
> the previously active lanes to POWERSTATE_A3 (while tp_write_lanes_reg()
> also only programs link->num_lanes instead of link->max_lanes).
It was indeed a bug, and fixed in v2. Fixed various register values that
were not shifted by link->mlane. Also now clear PHY_PMA_XCVR_POWER_STATE_REQ
and PHY_PMA_XCVR_PLLCLK_EN across all lanes (max_lanes)
> [ ... ]
>
> > +static int tp_phy_configure(struct phy *phy, union phy_configure_opts *opts)
> > +{
> > + struct tp_phy *tp_phy = dev_get_drvdata(phy->dev.parent);
> > + struct tp_link *link = phy_get_drvdata(phy);
> > + bool run_needed = false;
> > + int ret;
> > +
> > + guard(mutex)(&tp_phy->lock);
> > +
> > + if (tp_phy->already_configured)
> > + return 0;
> > +
> > + if (link->type != STD_TYPE_DP)
> > + return -EINVAL;
> > +
> > + ret = tp_dp_verify_config(link, &opts->dp);
> > + if (ret) {
> > + dev_err(&phy->dev, "invalid params for phy configure\n");
> > + return ret;
> > + }
> > +
> > + if (opts->dp.set_lanes && opts->dp.lanes != link->num_lanes) {
> > + tp_dp_set_lanes(tp_phy, link, opts->dp.lanes);
> > + run_needed = true;
> > + }
> > +
> > + if (opts->dp.set_rate) {
> > + ret = tp_dp_set_rate(tp_phy, link, &opts->dp);
> > + if (ret) {
> > + dev_err(&phy->dev, "Set rate failed\n");
> > + return ret;
> > + }
> > + run_needed = true;
> > + }
> > +
> > + if (opts->dp.set_voltages)
> > + tp_dp_set_voltages(tp_phy, link, &opts->dp);
> > +
> > + if (phy->power_count && run_needed)
> > + ret = tp_dp_run(tp_phy, link);
> > +
> > + return ret;
> > +}
>
> [Severity: High]
> Does moving DisplayPort reset deassertion and tp_dp_run() out of
> tp_phy_init() and gating tp_dp_run() on phy->power_count break
> cdns-mhdp8546?
>
> The in-tree DisplayPort bridge consumer
> (drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c) calls phy_init() in
> cdns_mhdp_probe() and phy_configure() in cdns_mhdp_link_training_init(),
> but never calls phy_power_on(). Because tp_dp_phy_init() no longer
> deasserts phy_rst/lnk_rst or starts the DP PLL and lanes, and
> tp_phy_configure() skips tp_dp_run() when phy->power_count is 0, the PHY
> remains held in reset and tp_dp_set_rate() polls PHY_PMA_CMN_CTRL2 on an
> unstarted PHY.
This is fine, the series depends on a patch that add the calls to
phy_power_on() / phy_power_off()
> > +
> > +static int tp_phy_on(struct phy *phy)
> > +{
> > + struct tp_link *link = phy_get_drvdata(phy);
> > + struct tp_phy *tp_phy = dev_get_drvdata(phy->dev.parent);
> > + u16 val;
> > + int ret;
> > +
> > + guard(mutex)(&tp_phy->lock);
> > +
> > + if (tp_phy->already_configured) {
> > + /* Give 5ms to 10ms delay for the PIPE clock to be stable */
> > + usleep_range(5000, 10000);
> > + return 0;
> > + }
> > +
> > + if (tp_phy->nb_phy_on == 0) {
> > + /* Take the PHY out of reset */
> > + ret = reset_control_deassert(tp_phy->phy_rst);
> > + if (ret)
> > + return ret;
> > + }
> > +
> > + /* Take the PHY lane group out of reset */
> > + ret = reset_control_deassert(link->lnk_rst);
> > + if (ret)
> > + return ret;
> > +
> > + /*
> > + * Wait for cmn_ready assertion
> > + * PHY_PMA_CMN_CTRL1[0] == 1
> > + */
> > + ret = read_poll_timeout(tp_reg_read, val, val & 1, 1000, PLL_LOCK_TIMEOUT, false,
> > + &tp_phy->reg_phy_pma_common_cdb, PHY_PMA_CMN_CTRL1);
> > + if (ret) {
> > + dev_err(tp_phy->dev, "Timeout waiting for CMN ready\n");
> > + return ret;
> > + }
> > +
> > + if (link->type == STD_TYPE_DP) {
> > + tp_dp_run(tp_phy, link);
> > + } else if (link->type == STD_TYPE_PCIE || link->type == STD_TYPE_USB_TXRX) {
> > + struct tp_reg_base *reg_base;
> > +
> > + reg_base = &tp_phy->reg_phy_pcs_lane_cdb[link->mlane];
> > + ret = read_poll_timeout(tp_reg_read, val, (val & 2) == 0, 1000,
> > + PLL_LOCK_TIMEOUT, false, reg_base,
> > + PHY_PCS_ISO_LINK_CTRL);
> > + if (ret < 0) {
> > + dev_err(tp_phy->dev, "Timeout waiting for PHY status ready\n");
> > + return ret;
> > + }
> > + }
> > +
> > + tp_phy->nb_phy_on++;
> > + return 0;
> > +}
>
> [Severity: Medium]
> Should tp_phy_on() check the return value of tp_dp_run() and re-assert
> link->lnk_rst and tp_phy->phy_rst on error?
>
> Right now a timeout in tp_dp_run() is ignored and tp_phy_on() increments
> nb_phy_on and returns 0. Conversely, if deasserting link->lnk_rst or
> polling PHY_PMA_CMN_CTRL1 or PHY_PCS_ISO_LINK_CTRL fails, tp_phy_on()
> returns an error without re-asserting the resets it deasserted and without
> incrementing phy->power_count, so tp_phy_off() will not be called to clean
> them up.
It was indeed a bug, and fixed in v2. Return value of tp_dp_run() is now
taken into account. Also reset handling was completely rework.
> > +
> > +static int tp_phy_off(struct phy *phy)
> > +{
> > + struct tp_link *inst = phy_get_drvdata(phy);
> > + struct tp_phy *tp_phy = dev_get_drvdata(phy->dev.parent);
> > + int ret;
> > +
> > + guard(mutex)(&tp_phy->lock);
> > +
> > + ret = reset_control_assert(inst->lnk_rst);
> > + if (ret)
> > + return ret;
> > +
> > + if (tp_phy->nb_phy_on == 1) {
> > + ret = reset_control_assert(tp_phy->phy_rst);
> > + if (ret)
> > + return ret;
> > + }
> > +
> > + tp_phy->nb_phy_on--;
> > + return 0;
> > +}
>
> [Severity: High]
> Can tp_phy_off() underflow tp_phy->nb_phy_on when tp_phy->already_configured
> is true?
>
> When tp_phy->already_configured is true, tp_phy_on() returns early without
> incrementing nb_phy_on (leaving it at 0), but tp_phy_off() has no
> already_configured check: it asserts inst->lnk_rst and decrements the u8
> nb_phy_on counter from 0 to 255. A subsequent phy_power_on() then either
> returns early with inst->lnk_rst still asserted, or after system suspend
> clears already_configured, sees nb_phy_on != 0, skips deasserting
> tp_phy->phy_rst, and times out waiting for CMN ready.
It was indeed a bug, and fixed in v2. Increment nb_phy_on when returning early.
> [ ... ]
>
> > +static int tp_ref_clk_init(const struct tp_phy *tp_phy, struct clk *clk, const char *name,
> > + u32 *p_ref_clk_khz)
> > +{
> > + struct device *dev = tp_phy->dev;
> > + u32 ref_clk_rate;
> > + int ret;
> > +
> > + ret = clk_prepare_enable(clk);
> > + if (ret) {
> > + dev_err(dev, "Failed to prepare %s ref clock: %d\n", name, ret);
> > + return ret;
> > + }
> > +
> > + ref_clk_rate = clk_get_rate(clk);
> > + if (!ref_clk_rate) {
> > + dev_err(dev, "Failed to get %s ref clock rate\n", name);
> > + ret = -EINVAL;
> > + goto disable_clk;
> > + }
> > +
> > + for (u8 i = 0; i < tp_phy->nb_links; ++i) {
> > + if (!tp_is_ref_freq_supported(tp_phy->links[i].type, ref_clk_rate)) {
> > + dev_err(dev, "Invalid ref clock rate: %u Hz\n", ref_clk_rate);
> > + ret = -EINVAL;
> > + goto disable_clk;
> > + }
> > + }
>
> [Severity: Medium]
> Does this loop skip reference clock validation during probe and reject
> valid dual-refclk configurations during resume?
>
> In cdns_torrent_phy_probe(), tp_ref_clks_init() is called before the child
> subnodes are parsed, so tp_phy->nb_links is 0 and this loop does not run
> at probe time (allowing unsupported clock rates, or rates < 1000 Hz that
> set ref_clk_khz to 0 and divide by zero in tp_write_pll_regs()). Then in
> tp_resume_noirq(), where nb_links > 0, tp_ref_clk_init() checks every link
> against both tp_phy->clk and tp_phy->clk1 instead of only the links using
> that PLL, failing resume with -EINVAL when PLL0 and PLL1 use different
> protocol-specific reference frequencies.
It was indeed a bug, and fixed in v2. The functions call were reordered.
> [ ... ]
>
> > +static void tp_check_multiple_pcie_links(struct tp_phy *tp_phy)
> > +{
> > + struct tp_standard *std0 = &tp_phy->standards[0];
> > + u32 num_pcie_links = 0;
> > +
> > + /* First check if the first standard is PCIe */
> > + if (std0->type != STD_TYPE_PCIE)
> > + return;
> > +
> > + /* Count the number of links with PCIe protocol. */
> > + for (u8 i = 0; i < tp_phy->nb_links; ++i) {
> > + if (tp_phy->links[i].type == STD_TYPE_PCIE)
> > + num_pcie_links++;
> > + }
> > +
> > + /* PCIe Multilink configuration can be supported along with a non-PCIe protocol.
> > + * Such configurations are handled by adding a flag named STD_FLAG_PCIE_ML
> > + */
> > + if (num_pcie_links > 1)
> > + std0->flags |= STD_FLAG_PCIE_ML;
> > +}
>
> [Severity: Medium]
> Does tp_check_multiple_pcie_links() miss multi-link PCIe when a non-PCIe
> subnode appears first in Device Tree?
>
> tp_add_phy_from_of_subnode() populates tp_phy->standards[] in DT subnode
> order, so if phy@0 is USB or SGMII and phy@1 and phy@2 are PCIe, PCIe is in
> standards[1] when tp_check_multiple_pcie_links() runs. Because
> tp_check_multiple_pcie_links() only checks standards[0], it returns without
> setting STD_FLAG_PCIE_ML, and when tp_find_link_cfg() subsequently swaps
> standards[0] and standards[1], the driver matches single-link PCIe
> (STD0_PCIE_SL) instead of multi-link PCIe (STD0_PCIE_ML).
It was indeed a bug, and fixed in v2. Both standards are now checked.
> [ ... ]
>
> > +static int tp_find_link_cfg(struct tp_phy *tp_phy)
> > +{
> > + const struct tp_link_cfg *link_cfg;
> > + struct device *dev = tp_phy->dev;
> > + struct tp_standard *std0 = &tp_phy->standards[0];
> > + struct tp_standard *std1 = &tp_phy->standards[1];
> > +
> > + /* Verify the validity of standard index in links */
> > + for (u8 i = 0; i < tp_phy->nb_links; ++i) {
> > + const struct tp_link *link = &tp_phy->links[i];
> > +
> > + if (WARN_ON(link->std_idx >= MAX_NUM_STANDARDS))
> > + return -EINVAL;
> > + }
> > +
> > + link_cfg = tp_find_link_cfg_from_standards(std0, std1);
> > +
> > + /* Only swap the two standards if PLL1 has no dedicated reference clock */
> > + if (!link_cfg && !tp_phy->clk1) {
> > + link_cfg = tp_find_link_cfg_from_standards(std1, std0);
> > + if (link_cfg) {
> > + /* Swap the 2 standards */
> > + swap(tp_phy->standards[0], tp_phy->standards[1]);
> > +
> > + for (u8 i = 0; i < tp_phy->nb_links; ++i)
> > + tp_phy->links[i].std_idx ^= 1;
> > + }
> > + }
>
> [Severity: Medium]
> Why is swapping standards[0] and standards[1] disallowed when tp_phy->clk1
> is present?
>
> standards[0] and standards[1] are populated in DT child subnode order (lane
> order), whereas tp_link_cfgs[] orders entries by PLL assignment (std0 on
> PLL0 clocked by refclk, std1 on PLL1 clocked by pll1_refclk). When
> pll1_refclk is provided and the protocol on lower lanes (phy@0) uses PLL1
> (for example, USXGMII on phy@0 with PLL1 at 156.25 MHz and PCIe on phy@2
> with PLL0 at 100 MHz), refusing to swap standards[0] and standards[1]
> causes tp_find_link_cfg() to fail probe with -EINVAL.
A better comment was added to explain why swapping standards are disallowed
when clk1 is present.
> [ ... ]
>
> > +static void tp_phy_cleanup(const struct tp_phy *tp_phy)
> > +{
> > + reset_control_assert(tp_phy->phy_rst);
> > + reset_control_assert(tp_phy->apb_rst);
> > +
> > + for (u8 i = 0; i < tp_phy->nb_links; ++i) {
> > + reset_control_assert(tp_phy->links[i].lnk_rst);
> > + reset_control_put(tp_phy->links[i].lnk_rst);
> > + }
> > +
> > + clk_disable_unprepare(tp_phy->clk1);
> > + clk_disable_unprepare(tp_phy->clk);
> > + tp_clk_cleanup(tp_phy);
> > +}
>
> [Severity: Medium]
> Can tp_phy_cleanup() call clk_disable_unprepare() on clocks that were never
> enabled or were already disabled?
>
> In cdns_torrent_phy_probe(), jumping to cleanup when tp_of_get_clk() fails
> on pll1_refclk after acquiring tp_phy->clk, when tp_ref_clks_init() fails
> (after already disabling any clock it enabled), or when tp_phy->already_configured
> is true (where tp_ref_clks_init() is skipped, which also affects
> cdns_torrent_phy_remove()) unconditionally calls clk_disable_unprepare() on
> tp_phy->clk1 and tp_phy->clk, triggering clock enable/prepare count
> underflow warnings.
It was indeed a bug, and fixed in v2. A new variable was added named
ref_clks_initialized to track the state of ref clocks init.
> [ ... ]
>
> > +static int tp_suspend_noirq(struct device *dev)
> > +{
> > + struct tp_phy *tp_phy = dev_get_drvdata(dev);
> > +
> > + tp_refclk_driver_suspend(tp_phy);
> > +
> > + for (u8 i = 0; i < tp_phy->nb_links; ++i)
> > + reset_control_assert(tp_phy->links[i].lnk_rst);
> > +
> > + reset_control_assert(tp_phy->phy_rst);
> > + reset_control_assert(tp_phy->apb_rst);
> > +
> > + if (tp_phy->already_configured) {
> > + tp_phy->already_configured = false;
> > + } else {
> > + clk_disable_unprepare(tp_phy->clk1);
> > + clk_disable_unprepare(tp_phy->clk);
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +static int tp_resume_noirq(struct device *dev)
> > +{
> > + int ret;
> > + struct tp_phy *tp_phy = dev_get_drvdata(dev);
> > +
> > + ret = tp_refclk_driver_resume(tp_phy);
> > + if (ret)
> > + return ret;
> > +
> > + ret = tp_ref_clks_init(tp_phy);
> > + if (ret)
> > + return ret;
> > +
> > + /* Enable APB */
> > + reset_control_deassert(tp_phy->apb_rst);
> > +
> > + ret = tp_configure_links(tp_phy);
> > + if (ret)
> > + goto put_lnk_rst;
> > +
> > + return 0;
>
> [Severity: High]
> Does tp_resume_noirq() leave active links held in reset after suspend?
>
> tp_suspend_noirq() asserts lnk_rst for every link and asserts phy_rst
> without changing tp_phy->nb_phy_on, but tp_resume_noirq() only deasserts
> apb_rst and calls tp_configure_links() without deasserting phy_rst or
> lnk_rst. Consumers that keep their PHY powered on across suspend (such as
> am65-cpsw-nuss for SGMII/QSGMII/USXGMII) are left held in reset after
> resume, and sibling consumers on the same SERDES that call
> phy_power_off()/phy_power_on() across suspend/resume (such as pci-j721e)
> see tp_phy->nb_phy_on != 0 in tp_phy_on(), skip deasserting phy_rst, and
> time out waiting for CMN ready.
It was indeed a bug, and fixed in v2. The suspend/resume part was completely
reworked.
> > +
> > +put_lnk_rst:
> > +
> > + for (u8 i = 0; i < tp_phy->nb_links; ++i)
> > + reset_control_assert(tp_phy->links[i].lnk_rst);
> > +
> > + reset_control_assert(tp_phy->apb_rst);
> > + reset_control_assert(tp_phy->phy_rst);
> > +
> > + clk_disable_unprepare(tp_phy->clk1);
> > + clk_disable_unprepare(tp_phy->clk);
> > +
> > + return ret;
> > +}
> > +
> > +static DEFINE_NOIRQ_DEV_PM_OPS(tp_pm_ops, tp_suspend_noirq, tp_resume_noirq);
> > +
> > +static int cdns_torrent_phy_probe(struct platform_device *pdev)
> > +{
>
> [ ... ]
>
> > + tp_reg_bases_init(tp_phy);
> > + tp_reg_fields_init(tp_phy);
> > +
> > + ret = tp_read_techno_revision(tp_phy);
> > + if (ret)
> > + return ret;
> > +
> > + ret = tp_clk_register(tp_phy);
> > + if (ret)
> > + return ret;
> > +
> > + ret = tp_of_get_reset(tp_phy);
> > + if (ret)
> > + goto cleanup;
> > +
> > + ret = tp_of_get_clk(tp_phy);
> > + if (ret)
> > + goto cleanup;
> > +
> > + tp_phy->already_configured = tp_is_already_configured(tp_phy);
> > + if (!tp_phy->already_configured) {
> > + ret = tp_ref_clks_init(tp_phy);
> > + if (ret)
> > + goto cleanup;
> > +
> > + /* Enable APB */
> > + reset_control_deassert(tp_phy->apb_rst);
> > + }
>
> [Severity: High]
> Are CDB registers accessed here and in tp_resume_noirq() while
> tp_phy->apb_rst is still asserted?
>
> cdns_torrent_phy_probe() reads the PHY ID registers in
> tp_read_techno_revision(), writes CMN_CDIAG_REFCLK_DRV0_CTRL in
> tp_clk_register(), and reads PHY_PMA_CMN_CTRL1 in
> tp_is_already_configured() before tp_of_get_reset() and
> reset_control_deassert(tp_phy->apb_rst) run. Similarly, tp_resume_noirq()
> calls tp_refclk_driver_resume() (reading and writing
> CMN_CDIAG_REFCLK_DRV0_CTRL) before deasserting tp_phy->apb_rst, which was
> asserted in tp_suspend_noirq(). On platforms using the torrent_apb reset
> (or when re-probing after driver removal, which asserts apb_rst), accessing
> CDB registers while APB is in reset can fail probe with -ENODEV, lose
> register writes, or fault.
It was indeed a bug, and fixed in v2. APB reset is now deasserted before
accessing any register.
--
Benjamin Robin, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
WARNING: multiple messages have this Message-ID (diff)
From: Benjamin Robin <benjamin.robin@bootlin.com>
To: sashiko-reviews@lists.linux.dev
Cc: devicetree@vger.kernel.org, linux-phy@lists.infradead.org,
robh@kernel.org, conor+dt@kernel.org, vkoul@kernel.org,
neil.armstrong@linaro.org, olteanv@gmail.com
Subject: Re: [PATCH 4/4] phy: cadence-torrent: rewrite Torrent PHY driver
Date: Wed, 02 Dec 2026 17:02:18 +0100 [thread overview]
Message-ID: <5K7Md9-NTQeBZUihR_WipA@bootlin.com> (raw)
In-Reply-To: <sashiko-outbox-225247@kernel.org>
On Friday, November 20, 2026 at 2:33 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 14 potential
> issues to consider:
> - [High] phy: cadence-torrent: 32-bit shift overflow in
> tp_write_pll_regs()
> - [High] phy: cadence-torrent: CDB register access before apb_rst
> deassertion
> - [High] phy: cadence-torrent: DP init and configure break cdns-mhdp8546
> consumer
> - [High] phy: cadence-torrent: missing mlane shift in
> tp_dptx_set_pll_en() and lanes
> - [High] phy: cadence-torrent: nb_phy_on underflow in tp_phy_off() when
> preconfigured
> - [High] phy: cadence-torrent: tp_resume_noirq() leaves active links
> held in reset
> - [Medium] phy: cadence-torrent: REV_R110 enum typo in
> tp_write_pll_cp_padj()
> - [Medium] phy: cadence-torrent: unreachable FBR mode in
> tp_calc_pcie_pll_fb_div_mode()
> - [Medium] phy: cadence-torrent: stale SSC and divider regs in
> tp_write_pll_regs()
> - [Medium] phy: cadence-torrent: unbalanced clk disable in
> tp_phy_cleanup()
> - [Medium] phy: cadence-torrent: skipped refclk check in
> tp_ref_clks_init()
> - [Medium] phy: cadence-torrent: missed PCIe ML flag in
> tp_check_multiple_pcie_links()
> - [Medium] phy: cadence-torrent: rejected standard swap with clk1 in
> tp_find_link_cfg()
> - [Medium] phy: cadence-torrent: unchecked tp_dp_run() and missing reset
> rollback
> --
>
> commit 3480ec972d0cd2d542d547f1d45d7f4f38415bc8
> Author: Benjamin Robin <benjamin.robin@bootlin.com>
>
> phy: cadence-torrent: rewrite Torrent PHY driver
>
> This commit rewrites the Cadence Torrent PHY driver to compute clock and
> PLL register values at runtime and look up remaining protocol, revision,
> and process-node register settings per register instead of using static
> register tables.
>
> > diff --git a/drivers/phy/cadence/phy-cadence-torrent.c b/drivers/phy/cadence/phy-cadence-torrent.c
> > index 1e178aa2ffd76..74a65f0f4b242 100644
> > --- a/drivers/phy/cadence/phy-cadence-torrent.c
> > +++ b/drivers/phy/cadence/phy-cadence-torrent.c
>
> [ ... ]
>
> > @@ -394,5453 +515,4629 @@ enum phy_powerstate {
> > POWERSTATE_A3 = 3,
> > };
> >
> > +static const struct tp_link_cfg tp_link_cfgs[] = {
> > + {
> > + /* PLL 0 M0: PCIe, PLL 0 M1: PCIe, PLL 1: None */
> > + .std0 = STD0_PCIE_SL, .std1 = STD1_NONE,
> > + .pll_freq = { PLL_10_00_GHZ, PLL_8_00_GHZ, PLL_8_00_GHZ },
> > + .phy_pll_cfg = 0x0001,
> > + .pll_clk_sel = { 0x0601, 0x0400, 0x0400 },
> > + .xcvr_diag_plldrc_ctrl = { 0x0012, 0x0012 },
> > + .xcvr_diag_hsclk_sel = { 0x0000, 0x0000 },
> > + .xcvr_diag_hsclk_div = { 0x0001, 0x0001 },
> > + },
>
> [ ... ]
>
> > +static enum tp_pll_fb_div_mode tp_calc_pcie_pll_fb_div_mode(const struct tp_phy *tp_phy)
> > +{
> > + u32 tech_tag = tp_phy->tech_tag;
> > + enum tp_revision rev = tp_phy->rev;
> > + enum tp_pll_fb_div_mode div_mode;
> > +
> > + if (tech_tag & (TECH_TSMC_16 | TECH_TSMC_12)) {
> > + if (rev == REV_R100) {
> > + if (tp_phy->plls[PLL1_MODE0].freq_khz == 0)
> > + div_mode = PLL_FB_DIV_MODE_FBR;
> > + else
> > + div_mode = PLL_FB_DIV_MODE_D32;
> > + } else {
>
> [Severity: Medium]
> Can tp_phy->plls[PLL1_MODE0].freq_khz ever be 0 here?
>
> Every entry in tp_link_cfgs[], including single-link PCIe (STD0_PCIE_SL
> with STD1_NONE), sets pll_freq[PLL1_MODE0] to a non-zero frequency
> (PLL_8_00_GHZ) rather than PLL_UNUSED. Because tp_init_plls_data()
> populates plls[PLL1_MODE0].freq_khz from pll_freq[PLL1_MODE0] before
> calling tp_calc_pll_fb_div_modes(), single-link PCIe on TSMC 16nm/12nm
> REV_R100 hardware always selects PLL_FB_DIV_MODE_D32 instead of
> PLL_FB_DIV_MODE_FBR, misprogramming the PLL control, VCO calibration, and
> lock registers.
It was indeed a bug, and fixed in v2. If there is no second standard
we should select PLL_FB_DIV_MODE_FBR.
> [ ... ]
>
> > +static void tp_write_pll_cp_padj(const struct tp_phy *tp_phy, enum tp_pll_id pll_id)
> > +{
> > + u32 tech_tag = tp_phy->tech_tag;
> > + u32 rev_msk = BIT(tp_phy->rev);
> > + const struct tp_pll_data *pll = &tp_phy->plls[pll_id];
> > + u16 val;
> > +
> > + /* PLL charge pump proportional path adjust register */
> > + if (pll->freq_khz < 8000000) {
> > + val = 0x0B17;
> > + } else if (pll->fractional_mode) {
> > + if (tech_tag & TECH_TSMC_7)
> > + val = 0x0B17;
> > + else if (tech_tag & (TECH_SS_5 | TECH_SS_7))
> > + val = 0x0309;
> > + else
> > + val = 0x0509; /* TSMC_5, TSMC_12, TSMC_16, TSMC_16_0805 */
> > + } else if (pll->freq_khz >= 10000000) {
> > + if (tech_tag & TECH_TSMC_7)
> > + val = (rev_msk & (REV_MSK_R100 | REV_MSK_R110)) ? 0x0023 : 0x0035;
> > + else if (tech_tag & TECH_TSMC_5)
> > + val = (pll_id == PLL0_MODE0) ? 0x002D : 0x0035;
> > + else if (tech_tag & (TECH_SS_5 | TECH_SS_7))
> > + val = 0x013E;
> > + else
> > + val = 0x0028; /* TSMC_12, TSMC_16, TSMC_16_0805 */
> > + } else {
> > + if (tech_tag & TECH_TSMC_16)
> > + val = (rev_msk & REV_MSK_R100) ? 0x0028 : 0x0128;
> > + else if (tech_tag & TECH_TSMC_16_0805)
> > + val = 0x0128;
> > + else if (tech_tag & TECH_TSMC_12)
> > + val = 0x0028;
> > + else if (tech_tag & TECH_TSMC_7)
> > + val = (rev_msk & (REV_MSK_R100 | REV_R110)) ? 0x001D : 0x0031;
>
> [Severity: Medium]
> Should REV_R110 be REV_MSK_R110 in the TECH_TSMC_7 check? REV_R110 is
> the enum value 1 (equal to REV_MSK_R100) rather than BIT(REV_R110), so R110
> hardware (rev_msk == 0x2) never matches and programs
> CMN_PDIAG_PLLx_CP_PADJ_Mx with 0x0031 instead of 0x001D for 8 GHz
> integer-mode PLLs.
It was indeed a bug, and fixed in v2. Use the correct macro.
> [ ... ]
>
> > +static void tp_write_pll_fd_div_regs(const struct tp_phy *tp_phy, enum tp_pll_id pll_id,
> > + u32 div_ratio)
> > +{
> > + const struct tp_reg_base *base = &tp_phy->reg_common_cdb;
> > + const struct tp_pll_data *pll = &tp_phy->plls[pll_id];
> > + u32 tech_tag = tp_phy->tech_tag;
> > + u16 high_div;
> > + u16 low_div;
> > +
> > + /* In fractional mode, we use default/reset value */
> > + if (pll->fractional_mode)
> > + return;
> > +
> > + if (pll_id == PLL0_MODE0 && pll->ref_clk_khz == 100000 && pll->freq_khz == 10000000) {
> > + /* 100 MHz refclk, 10 GHz PLL */
> > + if (tech_tag & (TECH_TSMC_12 | TECH_TSMC_16 | TECH_TSMC_16_0805)) {
> > + high_div = 30;
> > + low_div = 12;
> > + } else {
> > + high_div = 36;
> > + low_div = 6;
> > + }
> > + } else if (pll->ref_clk_khz == 100000 && pll->freq_khz == 8000000) {
> > + /* 100 MHz refclk, 8 GHz PLL */
> > + if (tech_tag & (TECH_TSMC_12 | TECH_TSMC_16 | TECH_TSMC_16_0805)) {
> > + high_div = 16;
> > + low_div = 16;
> > + } else {
> > + high_div = 28;
> > + low_div = 4;
> > + }
> > + } else {
> > + /* Generic calculations */
> > +
> > + /* PLL high integer divider val: EVEN(div_ratio_2 * 67/100) - 4 */
> > + high_div = DIV_ROUND_UP((div_ratio >> 1) * 67U, 100U);
> > + high_div -= (high_div & 1U) ? 3U : 4U;
> > +
> > + /* PLL low integer divider val: div_ratio_2 - 8 - high_div */
> > + low_div = (div_ratio >> 1) - 8 - high_div;
> > + }
> > +
> > + tp_reg_write(base, CMN_PLLx_DSM_FBH_OVRD_Mx(pll_id), high_div);
> > + tp_reg_write(base, CMN_PLLx_DSM_FBL_OVRD_Mx(pll_id), low_div);
> > +}
> > +
> > +static void tp_write_pll_itrim_reg(const struct tp_phy *tp_phy, enum tp_pll_id pll_id)
> > +{
> > + const struct tp_reg_base *base = &tp_phy->reg_common_cdb;
> > + const struct tp_pll_data *pll = &tp_phy->plls[pll_id];
> > +
> > + if (tp_phy->tech_tag & TECH_TSMC_ALL) {
> > + u16 val = (pll->freq_khz >= 10000000) ? 0x003F : 0x007F;
> > +
> > + tp_reg_write(base, CMN_PDIAG_PLLx_ITRIM_Mx(pll_id), val);
> > + }
> > +}
> > +
> > +static void tp_write_pll_regs(const struct tp_phy *tp_phy, enum tp_pll_id pll_id)
> > +{
> > + const struct tp_reg_base *base = &tp_phy->reg_common_cdb;
> > + const struct tp_pll_data *pll = &tp_phy->plls[pll_id];
> > + u32 fd_div_prescale;
> > + u32 div_ratio;
> > + u16 fracdivl;
> > + u16 val;
> > +
> > + if (pll_id != PLL0_MODE1)
> > + tp_write_pll_vco_regs(tp_phy, pll_id);
> > +
> > + /* Divide ratio */
> > + div_ratio = pll->freq_khz / pll->ref_clk_khz;
> > +
> > + /* PLL feedback divider prescale */
> > + fd_div_prescale = ((!pll->fractional_mode) || (div_ratio > 512)) ? 1 : 0;
> > +
> > + /* PLL integer divider value */
> > + val = (pll->fractional_mode) ? (div_ratio >> fd_div_prescale) : 0;
> > + tp_reg_write(base, CMN_PLLx_INTDIV_Mx(pll_id), val);
> > +
> > + /* PLL fractional divider value */
> > + if (pll->fractional_mode) {
> > + u32 den = pll->ref_clk_khz << fd_div_prescale;
> > + u32 rem = pll->freq_khz % den;
> > +
> > + fracdivl = DIV_ROUND_CLOSEST(rem << 16, den);
> > + } else {
> > + fracdivl = 0;
> > + }
> > +
> > + tp_reg_write(base, CMN_PLLx_FRACDIVL_Mx(pll_id), fracdivl);
> > + tp_reg_write(base, CMN_PLLx_FRACDIVH_Mx(pll_id), pll->fractional_mode ? 2 : 0);
> > +
> > + /* PLL high threshold value */
> > + if (pll->fractional_mode) {
> > + /* EVEN(CEIL((fracdivl/65536 + (div_ratio >> fd_div_prescale)) * 2 / 3)) */
> > + u32 num = fracdivl + (div_ratio << (16 - fd_div_prescale));
>
> [Severity: High]
> Can rem << 16 overflow 32-bit u32 when computing fracdivl in
> tp_write_pll_regs()?
>
> When rem >= 65535 (for example, DisplayPort at the default 8100 Mbps rate
> with a supported 156.25 MHz reference clock, where den = 156250 and
> rem = 131250), rem << 16 overflows u32 and corrupts both
> CMN_PLLx_FRACDIVL_Mx and CMN_PLLx_HIGH_THR_Mx (producing fracdivl = 0x004B
> instead of 0xD70A). Also, when fd_div_prescale == 1 and div_ratio is odd
> (e.g. 20 MHz refclk with 10.3125 GHz USXGMII where div_ratio = 515),
> div_ratio << (16 - fd_div_prescale) retains bit 0 of div_ratio instead of
> using (div_ratio >> fd_div_prescale) << 16, double-counting the odd
> remainder already included in fracdivl.
It was indeed a bug, and fixed in v2. This part of the computation is now
done in 64 bits. Also the computation when fd_div_prescale == 1 and
div_ratio is odd was fixed.
> > +
> > + val = DIV_ROUND_UP(num, 3 << 15);
> > + if (val & 1)
> > + val++;
> > + } else {
> > + val = 0;
> > + }
> > +
> > + tp_reg_write(base, CMN_PLLx_HIGH_THR_Mx(pll_id), val);
> > +
> > + /* PLL delta sigma modulator diagnostics register */
> > + tp_reg_write(base, CMN_PLLx_DSM_DIAG_Mx(pll_id),
> > + pll->fractional_mode ? 0x0004 : 0x4004);
> > +
> > + /* PLL high and low integer divider value */
> > + tp_write_pll_fd_div_regs(tp_phy, pll_id, div_ratio);
> > +
> > + if (pll->ssc)
> > + tp_write_ssc_ctrl(tp_phy, pll_id, fd_div_prescale);
>
> [Severity: Medium]
> Does dynamic DisplayPort rate reconfiguration leave stale values in
> CMN_PLLx_SS_CTRL1_Mx..CTRL4_Mx and CMN_PLLx_DSM_FBH/FBL_OVRD_Mx?
>
> When tp_dp_update_pll_freq() -> tp_write_pll_regs() reconfigures a DP link
> from SSC enabled to SSC disabled, skipping tp_write_ssc_ctrl() leaves
> CMN_PLLx_SS_CTRL1_Mx..CTRL4_Mx enabled in hardware while VCO and lock
> counters are programmed for non-SSC operation. Similarly, if a DP PLL
> switches from integer mode (e.g. 2.7/5.4 Gbps with 100 MHz refclk and no
> SSC) to fractional mode, returning early in tp_write_pll_fd_div_regs()
> leaves the previous integer divider overrides in CMN_PLLx_DSM_FBH_OVRD_Mx
> and CMN_PLLx_DSM_FBL_OVRD_Mx instead of restoring their reset values.
It was indeed a bug, and fixed in v2. CMN_PLLx_SS_CTRL1_Mx is now written
to 2 when SSC is disabled.
But returning early in tp_write_pll_fd_div_regs() is fine, a comment was
added when returning early to explain why.
> [ ... ]
>
> > +static int tp_dptx_set_pll_en(const struct tp_phy *tp_phy, u32 num_lanes, bool enable)
> > +{
> > + void __iomem *addr;
> > + u32 pll_ack_val;
> > + u32 rd_val;
> > + int ret;
> > +
> > + /* Used to determine which bits to check for or enable in PHY_PMA_XCVR_PLLCLK_EN reg */
> > + u32 pll_bits;
> > + /* Used to enable or disable lanes. */
> > + u32 pll_val;
> > +
> > + /* Select reg value and mask, depending on enabled lane count. */
> > + pll_val = tp_reg_dptx_read(tp_phy, PHY_PMA_XCVR_PLLCLK_EN);
> > +
> > + if (enable) {
> > + pll_bits = ((1 << num_lanes) - 1);
> > + pll_val |= pll_bits;
> > + pll_ack_val = pll_bits;
> > + } else {
> > + pll_bits = ((1 << num_lanes) - 1);
> > + pll_val &= (~pll_bits);
> > + pll_ack_val = 0;
> > + }
>
> [ ... ]
>
> > +static void tp_dp_set_lanes(const struct tp_phy *tp_phy, struct tp_link *link, u32 num_lanes)
> > +{
> > + u32 pma_tx_elec_idle_mask;
> > + u8 used_lane_mask;
> > + u8 all_lane_mask;
> > + u32 pll_clk_en;
> > + u32 pwr_state;
> > + u32 value;
> > + u32 mlane = link->mlane;
> > +
> > + used_lane_mask = ((1U << num_lanes) - 1) << mlane;
> > + all_lane_mask = ((1U << link->max_lanes) - 1) << mlane;
>
> [ ... ]
>
> > + /* Clear requested power state set PLL clock enable to 0 on all used lanes. */
> > + pwr_state = tp_reg_dptx_read(tp_phy, PHY_PMA_XCVR_POWER_STATE_REQ);
> > + pll_clk_en = tp_reg_dptx_read(tp_phy, PHY_PMA_XCVR_PLLCLK_EN);
> > +
> > + for (u32 i = 0; i < num_lanes; ++i)
> > + pwr_state &= ~(PMA_XCVR_POWER_STATE_REQ_LN_MASK << PHY_POWER_STATE_LN(mlane + i));
> > +
> > + pll_clk_en &= ~used_lane_mask;
> > +
> > + tp_reg_dptx_write(tp_phy, PHY_PMA_XCVR_POWER_STATE_REQ, pwr_state);
> > + tp_reg_dptx_write(tp_phy, PHY_PMA_XCVR_PLLCLK_EN, pll_clk_en);
> > +
> > + /* release master lane reset */
> > + value |= 1 << mlane;
> > + tp_reg_dptx_write(tp_phy, PHY_RESET, value);
> > +
> > + /* update the number of used lanes */\n+ link->num_lanes = num_lanes;
> > +}
>
> [Severity: High]
> Should pll_bits in tp_dptx_set_pll_en() be shifted by link->mlane?
>
> Unlike tp_dp_set_lanes() and tp_dptx_set_power_state(), tp_dptx_set_pll_en()
> computes pll_bits = ((1 << num_lanes) - 1) without shifting by link->mlane,
> so any DisplayPort link with mlane > 0 modifies PHY_PMA_XCVR_PLLCLK_EN for
> lane 0 and times out polling PHY_PMA_XCVR_PLLCLK_EN_ACK.
>
> In addition, tp_dp_set_lanes() only clears PHY_PMA_XCVR_POWER_STATE_REQ and
> PHY_PMA_XCVR_PLLCLK_EN across used_lane_mask instead of all_lane_mask,
> leaving disabled lanes' PLLCLK_EN bits set when reducing the lane count.
> And when both set_lanes and set_rate are requested in tp_phy_configure(),
> running tp_dp_set_lanes() before tp_dp_set_rate() resets the lanes, clears
> PLLCLK_EN, and updates link->num_lanes before tp_dp_set_rate() transitions
> the previously active lanes to POWERSTATE_A3 (while tp_write_lanes_reg()
> also only programs link->num_lanes instead of link->max_lanes).
It was indeed a bug, and fixed in v2. Fixed various register values that
were not shifted by link->mlane. Also now clear PHY_PMA_XCVR_POWER_STATE_REQ
and PHY_PMA_XCVR_PLLCLK_EN across all lanes (max_lanes)
> [ ... ]
>
> > +static int tp_phy_configure(struct phy *phy, union phy_configure_opts *opts)
> > +{
> > + struct tp_phy *tp_phy = dev_get_drvdata(phy->dev.parent);
> > + struct tp_link *link = phy_get_drvdata(phy);
> > + bool run_needed = false;
> > + int ret;
> > +
> > + guard(mutex)(&tp_phy->lock);
> > +
> > + if (tp_phy->already_configured)
> > + return 0;
> > +
> > + if (link->type != STD_TYPE_DP)
> > + return -EINVAL;
> > +
> > + ret = tp_dp_verify_config(link, &opts->dp);
> > + if (ret) {
> > + dev_err(&phy->dev, "invalid params for phy configure\n");
> > + return ret;
> > + }
> > +
> > + if (opts->dp.set_lanes && opts->dp.lanes != link->num_lanes) {
> > + tp_dp_set_lanes(tp_phy, link, opts->dp.lanes);
> > + run_needed = true;
> > + }
> > +
> > + if (opts->dp.set_rate) {
> > + ret = tp_dp_set_rate(tp_phy, link, &opts->dp);
> > + if (ret) {
> > + dev_err(&phy->dev, "Set rate failed\n");
> > + return ret;
> > + }
> > + run_needed = true;
> > + }
> > +
> > + if (opts->dp.set_voltages)
> > + tp_dp_set_voltages(tp_phy, link, &opts->dp);
> > +
> > + if (phy->power_count && run_needed)
> > + ret = tp_dp_run(tp_phy, link);
> > +
> > + return ret;
> > +}
>
> [Severity: High]
> Does moving DisplayPort reset deassertion and tp_dp_run() out of
> tp_phy_init() and gating tp_dp_run() on phy->power_count break
> cdns-mhdp8546?
>
> The in-tree DisplayPort bridge consumer
> (drivers/gpu/drm/bridge/cadence/cdns-mhdp8546-core.c) calls phy_init() in
> cdns_mhdp_probe() and phy_configure() in cdns_mhdp_link_training_init(),
> but never calls phy_power_on(). Because tp_dp_phy_init() no longer
> deasserts phy_rst/lnk_rst or starts the DP PLL and lanes, and
> tp_phy_configure() skips tp_dp_run() when phy->power_count is 0, the PHY
> remains held in reset and tp_dp_set_rate() polls PHY_PMA_CMN_CTRL2 on an
> unstarted PHY.
This is fine, the series depends on a patch that add the calls to
phy_power_on() / phy_power_off()
> > +
> > +static int tp_phy_on(struct phy *phy)
> > +{
> > + struct tp_link *link = phy_get_drvdata(phy);
> > + struct tp_phy *tp_phy = dev_get_drvdata(phy->dev.parent);
> > + u16 val;
> > + int ret;
> > +
> > + guard(mutex)(&tp_phy->lock);
> > +
> > + if (tp_phy->already_configured) {
> > + /* Give 5ms to 10ms delay for the PIPE clock to be stable */
> > + usleep_range(5000, 10000);
> > + return 0;
> > + }
> > +
> > + if (tp_phy->nb_phy_on == 0) {
> > + /* Take the PHY out of reset */
> > + ret = reset_control_deassert(tp_phy->phy_rst);
> > + if (ret)
> > + return ret;
> > + }
> > +
> > + /* Take the PHY lane group out of reset */
> > + ret = reset_control_deassert(link->lnk_rst);
> > + if (ret)
> > + return ret;
> > +
> > + /*
> > + * Wait for cmn_ready assertion
> > + * PHY_PMA_CMN_CTRL1[0] == 1
> > + */
> > + ret = read_poll_timeout(tp_reg_read, val, val & 1, 1000, PLL_LOCK_TIMEOUT, false,
> > + &tp_phy->reg_phy_pma_common_cdb, PHY_PMA_CMN_CTRL1);
> > + if (ret) {
> > + dev_err(tp_phy->dev, "Timeout waiting for CMN ready\n");
> > + return ret;
> > + }
> > +
> > + if (link->type == STD_TYPE_DP) {
> > + tp_dp_run(tp_phy, link);
> > + } else if (link->type == STD_TYPE_PCIE || link->type == STD_TYPE_USB_TXRX) {
> > + struct tp_reg_base *reg_base;
> > +
> > + reg_base = &tp_phy->reg_phy_pcs_lane_cdb[link->mlane];
> > + ret = read_poll_timeout(tp_reg_read, val, (val & 2) == 0, 1000,
> > + PLL_LOCK_TIMEOUT, false, reg_base,
> > + PHY_PCS_ISO_LINK_CTRL);
> > + if (ret < 0) {
> > + dev_err(tp_phy->dev, "Timeout waiting for PHY status ready\n");
> > + return ret;
> > + }
> > + }
> > +
> > + tp_phy->nb_phy_on++;
> > + return 0;
> > +}
>
> [Severity: Medium]
> Should tp_phy_on() check the return value of tp_dp_run() and re-assert
> link->lnk_rst and tp_phy->phy_rst on error?
>
> Right now a timeout in tp_dp_run() is ignored and tp_phy_on() increments
> nb_phy_on and returns 0. Conversely, if deasserting link->lnk_rst or
> polling PHY_PMA_CMN_CTRL1 or PHY_PCS_ISO_LINK_CTRL fails, tp_phy_on()
> returns an error without re-asserting the resets it deasserted and without
> incrementing phy->power_count, so tp_phy_off() will not be called to clean
> them up.
It was indeed a bug, and fixed in v2. Return value of tp_dp_run() is now
taken into account. Also reset handling was completely rework.
> > +
> > +static int tp_phy_off(struct phy *phy)
> > +{
> > + struct tp_link *inst = phy_get_drvdata(phy);
> > + struct tp_phy *tp_phy = dev_get_drvdata(phy->dev.parent);
> > + int ret;
> > +
> > + guard(mutex)(&tp_phy->lock);
> > +
> > + ret = reset_control_assert(inst->lnk_rst);
> > + if (ret)
> > + return ret;
> > +
> > + if (tp_phy->nb_phy_on == 1) {
> > + ret = reset_control_assert(tp_phy->phy_rst);
> > + if (ret)
> > + return ret;
> > + }
> > +
> > + tp_phy->nb_phy_on--;
> > + return 0;
> > +}
>
> [Severity: High]
> Can tp_phy_off() underflow tp_phy->nb_phy_on when tp_phy->already_configured
> is true?
>
> When tp_phy->already_configured is true, tp_phy_on() returns early without
> incrementing nb_phy_on (leaving it at 0), but tp_phy_off() has no
> already_configured check: it asserts inst->lnk_rst and decrements the u8
> nb_phy_on counter from 0 to 255. A subsequent phy_power_on() then either
> returns early with inst->lnk_rst still asserted, or after system suspend
> clears already_configured, sees nb_phy_on != 0, skips deasserting
> tp_phy->phy_rst, and times out waiting for CMN ready.
It was indeed a bug, and fixed in v2. Increment nb_phy_on when returning early.
> [ ... ]
>
> > +static int tp_ref_clk_init(const struct tp_phy *tp_phy, struct clk *clk, const char *name,
> > + u32 *p_ref_clk_khz)
> > +{
> > + struct device *dev = tp_phy->dev;
> > + u32 ref_clk_rate;
> > + int ret;
> > +
> > + ret = clk_prepare_enable(clk);
> > + if (ret) {
> > + dev_err(dev, "Failed to prepare %s ref clock: %d\n", name, ret);
> > + return ret;
> > + }
> > +
> > + ref_clk_rate = clk_get_rate(clk);
> > + if (!ref_clk_rate) {
> > + dev_err(dev, "Failed to get %s ref clock rate\n", name);
> > + ret = -EINVAL;
> > + goto disable_clk;
> > + }
> > +
> > + for (u8 i = 0; i < tp_phy->nb_links; ++i) {
> > + if (!tp_is_ref_freq_supported(tp_phy->links[i].type, ref_clk_rate)) {
> > + dev_err(dev, "Invalid ref clock rate: %u Hz\n", ref_clk_rate);
> > + ret = -EINVAL;
> > + goto disable_clk;
> > + }
> > + }
>
> [Severity: Medium]
> Does this loop skip reference clock validation during probe and reject
> valid dual-refclk configurations during resume?
>
> In cdns_torrent_phy_probe(), tp_ref_clks_init() is called before the child
> subnodes are parsed, so tp_phy->nb_links is 0 and this loop does not run
> at probe time (allowing unsupported clock rates, or rates < 1000 Hz that
> set ref_clk_khz to 0 and divide by zero in tp_write_pll_regs()). Then in
> tp_resume_noirq(), where nb_links > 0, tp_ref_clk_init() checks every link
> against both tp_phy->clk and tp_phy->clk1 instead of only the links using
> that PLL, failing resume with -EINVAL when PLL0 and PLL1 use different
> protocol-specific reference frequencies.
It was indeed a bug, and fixed in v2. The functions call were reordered.
> [ ... ]
>
> > +static void tp_check_multiple_pcie_links(struct tp_phy *tp_phy)
> > +{
> > + struct tp_standard *std0 = &tp_phy->standards[0];
> > + u32 num_pcie_links = 0;
> > +
> > + /* First check if the first standard is PCIe */
> > + if (std0->type != STD_TYPE_PCIE)
> > + return;
> > +
> > + /* Count the number of links with PCIe protocol. */
> > + for (u8 i = 0; i < tp_phy->nb_links; ++i) {
> > + if (tp_phy->links[i].type == STD_TYPE_PCIE)
> > + num_pcie_links++;
> > + }
> > +
> > + /* PCIe Multilink configuration can be supported along with a non-PCIe protocol.
> > + * Such configurations are handled by adding a flag named STD_FLAG_PCIE_ML
> > + */
> > + if (num_pcie_links > 1)
> > + std0->flags |= STD_FLAG_PCIE_ML;
> > +}
>
> [Severity: Medium]
> Does tp_check_multiple_pcie_links() miss multi-link PCIe when a non-PCIe
> subnode appears first in Device Tree?
>
> tp_add_phy_from_of_subnode() populates tp_phy->standards[] in DT subnode
> order, so if phy@0 is USB or SGMII and phy@1 and phy@2 are PCIe, PCIe is in
> standards[1] when tp_check_multiple_pcie_links() runs. Because
> tp_check_multiple_pcie_links() only checks standards[0], it returns without
> setting STD_FLAG_PCIE_ML, and when tp_find_link_cfg() subsequently swaps
> standards[0] and standards[1], the driver matches single-link PCIe
> (STD0_PCIE_SL) instead of multi-link PCIe (STD0_PCIE_ML).
It was indeed a bug, and fixed in v2. Both standards are now checked.
> [ ... ]
>
> > +static int tp_find_link_cfg(struct tp_phy *tp_phy)
> > +{
> > + const struct tp_link_cfg *link_cfg;
> > + struct device *dev = tp_phy->dev;
> > + struct tp_standard *std0 = &tp_phy->standards[0];
> > + struct tp_standard *std1 = &tp_phy->standards[1];
> > +
> > + /* Verify the validity of standard index in links */
> > + for (u8 i = 0; i < tp_phy->nb_links; ++i) {
> > + const struct tp_link *link = &tp_phy->links[i];
> > +
> > + if (WARN_ON(link->std_idx >= MAX_NUM_STANDARDS))
> > + return -EINVAL;
> > + }
> > +
> > + link_cfg = tp_find_link_cfg_from_standards(std0, std1);
> > +
> > + /* Only swap the two standards if PLL1 has no dedicated reference clock */
> > + if (!link_cfg && !tp_phy->clk1) {
> > + link_cfg = tp_find_link_cfg_from_standards(std1, std0);
> > + if (link_cfg) {
> > + /* Swap the 2 standards */
> > + swap(tp_phy->standards[0], tp_phy->standards[1]);
> > +
> > + for (u8 i = 0; i < tp_phy->nb_links; ++i)
> > + tp_phy->links[i].std_idx ^= 1;
> > + }
> > + }
>
> [Severity: Medium]
> Why is swapping standards[0] and standards[1] disallowed when tp_phy->clk1
> is present?
>
> standards[0] and standards[1] are populated in DT child subnode order (lane
> order), whereas tp_link_cfgs[] orders entries by PLL assignment (std0 on
> PLL0 clocked by refclk, std1 on PLL1 clocked by pll1_refclk). When
> pll1_refclk is provided and the protocol on lower lanes (phy@0) uses PLL1
> (for example, USXGMII on phy@0 with PLL1 at 156.25 MHz and PCIe on phy@2
> with PLL0 at 100 MHz), refusing to swap standards[0] and standards[1]
> causes tp_find_link_cfg() to fail probe with -EINVAL.
A better comment was added to explain why swapping standards are disallowed
when clk1 is present.
> [ ... ]
>
> > +static void tp_phy_cleanup(const struct tp_phy *tp_phy)
> > +{
> > + reset_control_assert(tp_phy->phy_rst);
> > + reset_control_assert(tp_phy->apb_rst);
> > +
> > + for (u8 i = 0; i < tp_phy->nb_links; ++i) {
> > + reset_control_assert(tp_phy->links[i].lnk_rst);
> > + reset_control_put(tp_phy->links[i].lnk_rst);
> > + }
> > +
> > + clk_disable_unprepare(tp_phy->clk1);
> > + clk_disable_unprepare(tp_phy->clk);
> > + tp_clk_cleanup(tp_phy);
> > +}
>
> [Severity: Medium]
> Can tp_phy_cleanup() call clk_disable_unprepare() on clocks that were never
> enabled or were already disabled?
>
> In cdns_torrent_phy_probe(), jumping to cleanup when tp_of_get_clk() fails
> on pll1_refclk after acquiring tp_phy->clk, when tp_ref_clks_init() fails
> (after already disabling any clock it enabled), or when tp_phy->already_configured
> is true (where tp_ref_clks_init() is skipped, which also affects
> cdns_torrent_phy_remove()) unconditionally calls clk_disable_unprepare() on
> tp_phy->clk1 and tp_phy->clk, triggering clock enable/prepare count
> underflow warnings.
It was indeed a bug, and fixed in v2. A new variable was added named
ref_clks_initialized to track the state of ref clocks init.
> [ ... ]
>
> > +static int tp_suspend_noirq(struct device *dev)
> > +{
> > + struct tp_phy *tp_phy = dev_get_drvdata(dev);
> > +
> > + tp_refclk_driver_suspend(tp_phy);
> > +
> > + for (u8 i = 0; i < tp_phy->nb_links; ++i)
> > + reset_control_assert(tp_phy->links[i].lnk_rst);
> > +
> > + reset_control_assert(tp_phy->phy_rst);
> > + reset_control_assert(tp_phy->apb_rst);
> > +
> > + if (tp_phy->already_configured) {
> > + tp_phy->already_configured = false;
> > + } else {
> > + clk_disable_unprepare(tp_phy->clk1);
> > + clk_disable_unprepare(tp_phy->clk);
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +static int tp_resume_noirq(struct device *dev)
> > +{
> > + int ret;
> > + struct tp_phy *tp_phy = dev_get_drvdata(dev);
> > +
> > + ret = tp_refclk_driver_resume(tp_phy);
> > + if (ret)
> > + return ret;
> > +
> > + ret = tp_ref_clks_init(tp_phy);
> > + if (ret)
> > + return ret;
> > +
> > + /* Enable APB */
> > + reset_control_deassert(tp_phy->apb_rst);
> > +
> > + ret = tp_configure_links(tp_phy);
> > + if (ret)
> > + goto put_lnk_rst;
> > +
> > + return 0;
>
> [Severity: High]
> Does tp_resume_noirq() leave active links held in reset after suspend?
>
> tp_suspend_noirq() asserts lnk_rst for every link and asserts phy_rst
> without changing tp_phy->nb_phy_on, but tp_resume_noirq() only deasserts
> apb_rst and calls tp_configure_links() without deasserting phy_rst or
> lnk_rst. Consumers that keep their PHY powered on across suspend (such as
> am65-cpsw-nuss for SGMII/QSGMII/USXGMII) are left held in reset after
> resume, and sibling consumers on the same SERDES that call
> phy_power_off()/phy_power_on() across suspend/resume (such as pci-j721e)
> see tp_phy->nb_phy_on != 0 in tp_phy_on(), skip deasserting phy_rst, and
> time out waiting for CMN ready.
It was indeed a bug, and fixed in v2. The suspend/resume part was completely
reworked.
> > +
> > +put_lnk_rst:
> > +
> > + for (u8 i = 0; i < tp_phy->nb_links; ++i)
> > + reset_control_assert(tp_phy->links[i].lnk_rst);
> > +
> > + reset_control_assert(tp_phy->apb_rst);
> > + reset_control_assert(tp_phy->phy_rst);
> > +
> > + clk_disable_unprepare(tp_phy->clk1);
> > + clk_disable_unprepare(tp_phy->clk);
> > +
> > + return ret;
> > +}
> > +
> > +static DEFINE_NOIRQ_DEV_PM_OPS(tp_pm_ops, tp_suspend_noirq, tp_resume_noirq);
> > +
> > +static int cdns_torrent_phy_probe(struct platform_device *pdev)
> > +{
>
> [ ... ]
>
> > + tp_reg_bases_init(tp_phy);
> > + tp_reg_fields_init(tp_phy);
> > +
> > + ret = tp_read_techno_revision(tp_phy);
> > + if (ret)
> > + return ret;
> > +
> > + ret = tp_clk_register(tp_phy);
> > + if (ret)
> > + return ret;
> > +
> > + ret = tp_of_get_reset(tp_phy);
> > + if (ret)
> > + goto cleanup;
> > +
> > + ret = tp_of_get_clk(tp_phy);
> > + if (ret)
> > + goto cleanup;
> > +
> > + tp_phy->already_configured = tp_is_already_configured(tp_phy);
> > + if (!tp_phy->already_configured) {
> > + ret = tp_ref_clks_init(tp_phy);
> > + if (ret)
> > + goto cleanup;
> > +
> > + /* Enable APB */
> > + reset_control_deassert(tp_phy->apb_rst);
> > + }
>
> [Severity: High]
> Are CDB registers accessed here and in tp_resume_noirq() while
> tp_phy->apb_rst is still asserted?
>
> cdns_torrent_phy_probe() reads the PHY ID registers in
> tp_read_techno_revision(), writes CMN_CDIAG_REFCLK_DRV0_CTRL in
> tp_clk_register(), and reads PHY_PMA_CMN_CTRL1 in
> tp_is_already_configured() before tp_of_get_reset() and
> reset_control_deassert(tp_phy->apb_rst) run. Similarly, tp_resume_noirq()
> calls tp_refclk_driver_resume() (reading and writing
> CMN_CDIAG_REFCLK_DRV0_CTRL) before deasserting tp_phy->apb_rst, which was
> asserted in tp_suspend_noirq(). On platforms using the torrent_apb reset
> (or when re-probing after driver removal, which asserts apb_rst), accessing
> CDB registers while APB is in reset can fail probe with -ENODEV, lose
> register writes, or fault.
It was indeed a bug, and fixed in v2. APB reset is now deasserted before
accessing any register.
--
Benjamin Robin, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
--
linux-phy mailing list
linux-phy@lists.infradead.org
https://lists.infradead.org/mailman/listinfo/linux-phy
next prev parent reply other threads:[~2026-12-02 16:02 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 12:55 [PATCH 0/4] phy: cadence-torrent: rewrite Torrent PHY driver Benjamin Robin
2026-11-20 12:55 ` Benjamin Robin
2026-11-20 12:55 ` [PATCH 1/4] arm64: dts: ti: k3-j7200: Fix QSGMII overlay by adding SERDES PHY Benjamin Robin
2026-11-20 12:55 ` Benjamin Robin
2026-11-20 12:55 ` [PATCH 2/4] dt-bindings: phy: cadence-torrent: drop cdns,num-lanes default value Benjamin Robin
2026-11-20 12:55 ` Benjamin Robin
2026-11-21 16:19 ` Krzysztof Kozlowski
2026-11-21 16:19 ` Krzysztof Kozlowski
2026-11-24 16:11 ` Benjamin Robin
2026-11-24 16:11 ` Benjamin Robin
2026-12-01 20:59 ` Rob Herring
2026-12-01 20:59 ` Rob Herring
2026-11-20 12:55 ` [PATCH 3/4] dt-bindings: phy: cadence-torrent: add phy-mode constants Benjamin Robin
2026-11-20 12:55 ` Benjamin Robin
2026-11-20 13:10 ` sashiko-bot
2026-11-20 13:10 ` sashiko-bot
2026-11-21 16:20 ` Krzysztof Kozlowski
2026-11-21 16:20 ` Krzysztof Kozlowski
2026-11-24 16:41 ` Benjamin Robin
2026-11-24 16:41 ` Benjamin Robin
2026-11-20 12:55 ` [PATCH 4/4] phy: cadence-torrent: rewrite Torrent PHY driver Benjamin Robin
2026-11-20 12:55 ` Benjamin Robin
2026-11-20 13:33 ` sashiko-bot
2026-11-20 13:33 ` sashiko-bot
2026-12-02 16:02 ` Benjamin Robin [this message]
2026-12-02 16:02 ` Benjamin Robin
2026-11-21 16:16 ` Krzysztof Kozlowski
2026-11-21 16:16 ` Krzysztof Kozlowski
2026-11-24 17:18 ` Benjamin Robin
2026-11-24 17:18 ` Benjamin Robin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5K7Md9-NTQeBZUihR_WipA@bootlin.com \
--to=benjamin.robin@bootlin.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=linux-phy@lists.infradead.org \
--cc=neil.armstrong@linaro.org \
--cc=olteanv@gmail.com \
--cc=robh@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=vkoul@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.