Skip to content

Commit 4688bb1

Browse files
Nicolas FrattaroliYuryNorov
authored andcommitted
phy: rockchip-pcie: switch to FIELD_PREP_WM16 macro
The era of hand-rolled HIWORD_UPDATE macros is over, at least for those drivers that use constant masks. The Rockchip PCIe PHY driver, used on the RK3399, has its own definition of HIWORD_UPDATE. Remove it, and replace instances of it with hw_bitfield.h's FIELD_PREP_WM16. To achieve this, some mask defines are reshuffled, as FIELD_PREP_WM16 uses the mask as both the mask of bits to write and to derive the shift amount from in order to shift the value. In order to ensure that the mask is always a constant, the inst->index shift is performed after the FIELD_PREP_WM16, as this is a runtime value. >From this, we gain compile-time error checking, and in my humble opinion nicer code, as well as a single definition of this macro across the entire codebase to aid in code comprehension. Tested on a RK3399 ROCKPro64, where PCIe still works as expected when accessing an NVMe drive. Signed-off-by: Nicolas Frattaroli <nicolas.frattaroli@collabora.com> Reviewed-by: Heiko Stuebner <heiko@sntech.de> Signed-off-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>
1 parent b8b5677 commit 4688bb1

1 file changed

Lines changed: 20 additions & 50 deletions

File tree

drivers/phy/rockchip/phy-rockchip-pcie.c

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <linux/clk.h>
1010
#include <linux/delay.h>
11+
#include <linux/hw_bitfield.h>
1112
#include <linux/io.h>
1213
#include <linux/mfd/syscon.h>
1314
#include <linux/module.h>
@@ -18,22 +19,13 @@
1819
#include <linux/regmap.h>
1920
#include <linux/reset.h>
2021

21-
/*
22-
* The higher 16-bit of this register is used for write protection
23-
* only if BIT(x + 16) set to 1 the BIT(x) can be written.
24-
*/
25-
#define HIWORD_UPDATE(val, mask, shift) \
26-
((val) << (shift) | (mask) << ((shift) + 16))
2722

2823
#define PHY_MAX_LANE_NUM 4
29-
#define PHY_CFG_DATA_SHIFT 7
30-
#define PHY_CFG_ADDR_SHIFT 1
31-
#define PHY_CFG_DATA_MASK 0xf
32-
#define PHY_CFG_ADDR_MASK 0x3f
24+
#define PHY_CFG_DATA_MASK GENMASK(10, 7)
25+
#define PHY_CFG_ADDR_MASK GENMASK(6, 1)
3326
#define PHY_CFG_WR_ENABLE 1
3427
#define PHY_CFG_WR_DISABLE 0
35-
#define PHY_CFG_WR_SHIFT 0
36-
#define PHY_CFG_WR_MASK 1
28+
#define PHY_CFG_WR_MASK BIT(0)
3729
#define PHY_CFG_PLL_LOCK 0x10
3830
#define PHY_CFG_CLK_TEST 0x10
3931
#define PHY_CFG_CLK_SCC 0x12
@@ -48,11 +40,7 @@
4840
#define PHY_LANE_RX_DET_SHIFT 11
4941
#define PHY_LANE_RX_DET_TH 0x1
5042
#define PHY_LANE_IDLE_OFF 0x1
51-
#define PHY_LANE_IDLE_MASK 0x1
52-
#define PHY_LANE_IDLE_A_SHIFT 3
53-
#define PHY_LANE_IDLE_B_SHIFT 4
54-
#define PHY_LANE_IDLE_C_SHIFT 5
55-
#define PHY_LANE_IDLE_D_SHIFT 6
43+
#define PHY_LANE_IDLE_MASK BIT(3)
5644

5745
struct rockchip_pcie_data {
5846
unsigned int pcie_conf;
@@ -99,22 +87,14 @@ static inline void phy_wr_cfg(struct rockchip_pcie_phy *rk_phy,
9987
u32 addr, u32 data)
10088
{
10189
regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
102-
HIWORD_UPDATE(data,
103-
PHY_CFG_DATA_MASK,
104-
PHY_CFG_DATA_SHIFT) |
105-
HIWORD_UPDATE(addr,
106-
PHY_CFG_ADDR_MASK,
107-
PHY_CFG_ADDR_SHIFT));
90+
FIELD_PREP_WM16(PHY_CFG_DATA_MASK, data) |
91+
FIELD_PREP_WM16(PHY_CFG_ADDR_MASK, addr));
10892
udelay(1);
10993
regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
110-
HIWORD_UPDATE(PHY_CFG_WR_ENABLE,
111-
PHY_CFG_WR_MASK,
112-
PHY_CFG_WR_SHIFT));
94+
FIELD_PREP_WM16(PHY_CFG_WR_MASK, PHY_CFG_WR_ENABLE));
11395
udelay(1);
11496
regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
115-
HIWORD_UPDATE(PHY_CFG_WR_DISABLE,
116-
PHY_CFG_WR_MASK,
117-
PHY_CFG_WR_SHIFT));
97+
FIELD_PREP_WM16(PHY_CFG_WR_MASK, PHY_CFG_WR_DISABLE));
11898
}
11999

120100
static int rockchip_pcie_phy_power_off(struct phy *phy)
@@ -125,11 +105,9 @@ static int rockchip_pcie_phy_power_off(struct phy *phy)
125105

126106
guard(mutex)(&rk_phy->pcie_mutex);
127107

128-
regmap_write(rk_phy->reg_base,
129-
rk_phy->phy_data->pcie_laneoff,
130-
HIWORD_UPDATE(PHY_LANE_IDLE_OFF,
131-
PHY_LANE_IDLE_MASK,
132-
PHY_LANE_IDLE_A_SHIFT + inst->index));
108+
regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_laneoff,
109+
FIELD_PREP_WM16(PHY_LANE_IDLE_MASK,
110+
PHY_LANE_IDLE_OFF) << inst->index);
133111

134112
if (--rk_phy->pwr_cnt) {
135113
return 0;
@@ -139,11 +117,9 @@ static int rockchip_pcie_phy_power_off(struct phy *phy)
139117
if (err) {
140118
dev_err(&phy->dev, "assert phy_rst err %d\n", err);
141119
rk_phy->pwr_cnt++;
142-
regmap_write(rk_phy->reg_base,
143-
rk_phy->phy_data->pcie_laneoff,
144-
HIWORD_UPDATE(!PHY_LANE_IDLE_OFF,
145-
PHY_LANE_IDLE_MASK,
146-
PHY_LANE_IDLE_A_SHIFT + inst->index));
120+
regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_laneoff,
121+
FIELD_PREP_WM16(PHY_LANE_IDLE_MASK,
122+
!PHY_LANE_IDLE_OFF) << inst->index);
147123
return err;
148124
}
149125

@@ -159,11 +135,9 @@ static int rockchip_pcie_phy_power_on(struct phy *phy)
159135

160136
guard(mutex)(&rk_phy->pcie_mutex);
161137

162-
regmap_write(rk_phy->reg_base,
163-
rk_phy->phy_data->pcie_laneoff,
164-
HIWORD_UPDATE(!PHY_LANE_IDLE_OFF,
165-
PHY_LANE_IDLE_MASK,
166-
PHY_LANE_IDLE_A_SHIFT + inst->index));
138+
regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_laneoff,
139+
FIELD_PREP_WM16(PHY_LANE_IDLE_MASK,
140+
!PHY_LANE_IDLE_OFF) << inst->index);
167141

168142
if (rk_phy->pwr_cnt++) {
169143
return 0;
@@ -177,9 +151,7 @@ static int rockchip_pcie_phy_power_on(struct phy *phy)
177151
}
178152

179153
regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
180-
HIWORD_UPDATE(PHY_CFG_PLL_LOCK,
181-
PHY_CFG_ADDR_MASK,
182-
PHY_CFG_ADDR_SHIFT));
154+
FIELD_PREP_WM16(PHY_CFG_ADDR_MASK, PHY_CFG_PLL_LOCK));
183155

184156
/*
185157
* No documented timeout value for phy operation below,
@@ -210,9 +182,7 @@ static int rockchip_pcie_phy_power_on(struct phy *phy)
210182
}
211183

212184
regmap_write(rk_phy->reg_base, rk_phy->phy_data->pcie_conf,
213-
HIWORD_UPDATE(PHY_CFG_PLL_LOCK,
214-
PHY_CFG_ADDR_MASK,
215-
PHY_CFG_ADDR_SHIFT));
185+
FIELD_PREP_WM16(PHY_CFG_ADDR_MASK, PHY_CFG_PLL_LOCK));
216186

217187
err = regmap_read_poll_timeout(rk_phy->reg_base,
218188
rk_phy->phy_data->pcie_status,

0 commit comments

Comments
 (0)