Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Rockchip PCIE3.0 phy driver |
| 4 | * |
| 5 | * Copyright (C) 2021 Rockchip Electronics Co., Ltd. |
| 6 | */ |
| 7 | |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 8 | #include <clk.h> |
| 9 | #include <dm.h> |
| 10 | #include <generic-phy.h> |
| 11 | #include <regmap.h> |
| 12 | #include <reset-uclass.h> |
| 13 | #include <syscon.h> |
| 14 | #include <asm/io.h> |
| 15 | #include <dm/device_compat.h> |
| 16 | #include <dm/lists.h> |
| 17 | |
Jonas Karlman | b7ca884 | 2023-08-02 19:28:33 +0000 | [diff] [blame] | 18 | /* Register for RK3568 */ |
| 19 | #define GRF_PCIE30PHY_CON1 0x4 |
| 20 | #define GRF_PCIE30PHY_CON6 0x18 |
| 21 | #define GRF_PCIE30PHY_CON9 0x24 |
| 22 | #define GRF_PCIE30PHY_DA_OCM (BIT(15) | BIT(31)) |
| 23 | #define GRF_PCIE30PHY_STATUS0 0x80 |
| 24 | #define GRF_PCIE30PHY_WR_EN (0xf << 16) |
| 25 | #define SRAM_INIT_DONE(reg) (reg & BIT(14)) |
| 26 | |
| 27 | #define RK3568_BIFURCATION_LANE_0_1 BIT(0) |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 28 | |
Jonas Karlman | 3f24f8b | 2023-08-02 19:04:32 +0000 | [diff] [blame] | 29 | /* Register for RK3588 */ |
| 30 | #define PHP_GRF_PCIESEL_CON 0x100 |
| 31 | #define RK3588_PCIE3PHY_GRF_CMN_CON0 0x0 |
| 32 | #define RK3588_PCIE3PHY_GRF_PHY0_STATUS1 0x904 |
| 33 | #define RK3588_PCIE3PHY_GRF_PHY1_STATUS1 0xa04 |
| 34 | #define RK3588_SRAM_INIT_DONE(reg) (reg & BIT(0)) |
| 35 | |
| 36 | #define RK3588_BIFURCATION_LANE_0_1 BIT(0) |
| 37 | #define RK3588_BIFURCATION_LANE_2_3 BIT(1) |
| 38 | #define RK3588_LANE_AGGREGATION BIT(2) |
Sebastian Kropatsch | ea40d55 | 2024-07-14 23:23:43 +0200 | [diff] [blame] | 39 | #define RK3588_PCIE1LN_SEL_EN (GENMASK(1, 0) << 16) |
| 40 | #define RK3588_PCIE30_PHY_MODE_EN (GENMASK(2, 0) << 16) |
Jonas Karlman | 3f24f8b | 2023-08-02 19:04:32 +0000 | [diff] [blame] | 41 | |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 42 | /** |
| 43 | * struct rockchip_p3phy_priv - RK DW PCIe PHY state |
| 44 | * |
| 45 | * @mmio: The base address of PHY internal registers |
| 46 | * @phy_grf: The regmap for controlling pipe signal |
| 47 | * @p30phy: The reset signal for PHY |
Jonas Karlman | 6153aed | 2023-08-02 19:04:29 +0000 | [diff] [blame] | 48 | * @clks: The clocks for PHY |
Jonas Karlman | b7ca884 | 2023-08-02 19:28:33 +0000 | [diff] [blame] | 49 | * @num_lanes: The number of lane to controller mappings |
| 50 | * @lanes: The lane to controller mapping |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 51 | */ |
| 52 | struct rockchip_p3phy_priv { |
| 53 | void __iomem *mmio; |
| 54 | struct regmap *phy_grf; |
Jonas Karlman | 3f24f8b | 2023-08-02 19:04:32 +0000 | [diff] [blame] | 55 | struct regmap *pipe_grf; |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 56 | struct reset_ctl p30phy; |
Jonas Karlman | 6153aed | 2023-08-02 19:04:29 +0000 | [diff] [blame] | 57 | struct clk_bulk clks; |
Jonas Karlman | b7ca884 | 2023-08-02 19:28:33 +0000 | [diff] [blame] | 58 | int num_lanes; |
| 59 | u32 lanes[4]; |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 60 | }; |
| 61 | |
Jonas Karlman | a10080c | 2023-08-02 19:04:30 +0000 | [diff] [blame] | 62 | struct rockchip_p3phy_ops { |
| 63 | int (*phy_init)(struct phy *phy); |
| 64 | }; |
| 65 | |
| 66 | static int rockchip_p3phy_rk3568_init(struct phy *phy) |
| 67 | { |
| 68 | struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev); |
Jonas Karlman | b7ca884 | 2023-08-02 19:28:33 +0000 | [diff] [blame] | 69 | bool bifurcation = false; |
| 70 | int ret; |
| 71 | u32 reg; |
Jonas Karlman | a10080c | 2023-08-02 19:04:30 +0000 | [diff] [blame] | 72 | |
| 73 | /* Deassert PCIe PMA output clamp mode */ |
Jonas Karlman | b7ca884 | 2023-08-02 19:28:33 +0000 | [diff] [blame] | 74 | regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON9, GRF_PCIE30PHY_DA_OCM); |
| 75 | |
| 76 | for (int i = 0; i < priv->num_lanes; i++) { |
| 77 | if (priv->lanes[i] > 1) |
| 78 | bifurcation = true; |
| 79 | } |
| 80 | |
| 81 | /* Set bifurcation if needed, and it doesn't care RC/EP */ |
| 82 | if (bifurcation) { |
| 83 | regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON6, |
| 84 | GRF_PCIE30PHY_WR_EN | RK3568_BIFURCATION_LANE_0_1); |
| 85 | regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON1, |
| 86 | GRF_PCIE30PHY_DA_OCM); |
| 87 | } else { |
| 88 | regmap_write(priv->phy_grf, GRF_PCIE30PHY_CON6, |
| 89 | GRF_PCIE30PHY_WR_EN & ~RK3568_BIFURCATION_LANE_0_1); |
| 90 | } |
Jonas Karlman | a10080c | 2023-08-02 19:04:30 +0000 | [diff] [blame] | 91 | |
| 92 | reset_deassert(&priv->p30phy); |
| 93 | udelay(1); |
| 94 | |
Jonas Karlman | b7ca884 | 2023-08-02 19:28:33 +0000 | [diff] [blame] | 95 | ret = regmap_read_poll_timeout(priv->phy_grf, |
| 96 | GRF_PCIE30PHY_STATUS0, |
| 97 | reg, SRAM_INIT_DONE(reg), |
| 98 | 0, 500); |
| 99 | if (ret) |
| 100 | dev_err(phy->dev, "lock failed 0x%x\n", reg); |
| 101 | |
| 102 | return ret; |
Jonas Karlman | a10080c | 2023-08-02 19:04:30 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static const struct rockchip_p3phy_ops rk3568_ops = { |
| 106 | .phy_init = rockchip_p3phy_rk3568_init, |
| 107 | }; |
| 108 | |
Jonas Karlman | 3f24f8b | 2023-08-02 19:04:32 +0000 | [diff] [blame] | 109 | static int rockchip_p3phy_rk3588_init(struct phy *phy) |
| 110 | { |
| 111 | struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev); |
| 112 | u32 reg = 0; |
Sebastian Kropatsch | efa0214 | 2024-07-14 23:23:42 +0200 | [diff] [blame] | 113 | u8 mode = RK3588_LANE_AGGREGATION; /* Lane aggregation by default */ |
Jonas Karlman | 3f24f8b | 2023-08-02 19:04:32 +0000 | [diff] [blame] | 114 | int ret; |
| 115 | |
| 116 | /* Deassert PCIe PMA output clamp mode */ |
| 117 | regmap_write(priv->phy_grf, RK3588_PCIE3PHY_GRF_CMN_CON0, |
| 118 | BIT(8) | BIT(24)); |
| 119 | |
| 120 | /* Set bifurcation if needed */ |
| 121 | for (int i = 0; i < priv->num_lanes; i++) { |
Jonas Karlman | 3f24f8b | 2023-08-02 19:04:32 +0000 | [diff] [blame] | 122 | if (priv->lanes[i] > 1) |
Sebastian Kropatsch | efa0214 | 2024-07-14 23:23:42 +0200 | [diff] [blame] | 123 | mode &= ~RK3588_LANE_AGGREGATION; |
| 124 | if (priv->lanes[i] == 3) |
| 125 | mode |= RK3588_BIFURCATION_LANE_0_1; |
| 126 | if (priv->lanes[i] == 4) |
| 127 | mode |= RK3588_BIFURCATION_LANE_2_3; |
Jonas Karlman | 3f24f8b | 2023-08-02 19:04:32 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Sebastian Kropatsch | efa0214 | 2024-07-14 23:23:42 +0200 | [diff] [blame] | 130 | reg = mode; |
Jonas Karlman | 3f24f8b | 2023-08-02 19:04:32 +0000 | [diff] [blame] | 131 | regmap_write(priv->phy_grf, RK3588_PCIE3PHY_GRF_CMN_CON0, |
Sebastian Kropatsch | ea40d55 | 2024-07-14 23:23:43 +0200 | [diff] [blame] | 132 | RK3588_PCIE30_PHY_MODE_EN | reg); |
Jonas Karlman | 3f24f8b | 2023-08-02 19:04:32 +0000 | [diff] [blame] | 133 | |
| 134 | /* Set pcie1ln_sel in PHP_GRF_PCIESEL_CON */ |
Sebastian Kropatsch | ea40d55 | 2024-07-14 23:23:43 +0200 | [diff] [blame] | 135 | reg = mode & (RK3588_BIFURCATION_LANE_0_1 | RK3588_BIFURCATION_LANE_2_3); |
Jonas Karlman | 3f24f8b | 2023-08-02 19:04:32 +0000 | [diff] [blame] | 136 | if (reg) |
| 137 | regmap_write(priv->pipe_grf, PHP_GRF_PCIESEL_CON, |
Sebastian Kropatsch | ea40d55 | 2024-07-14 23:23:43 +0200 | [diff] [blame] | 138 | RK3588_PCIE1LN_SEL_EN | reg); |
Jonas Karlman | 3f24f8b | 2023-08-02 19:04:32 +0000 | [diff] [blame] | 139 | |
| 140 | reset_deassert(&priv->p30phy); |
| 141 | udelay(1); |
| 142 | |
| 143 | ret = regmap_read_poll_timeout(priv->phy_grf, |
| 144 | RK3588_PCIE3PHY_GRF_PHY0_STATUS1, |
| 145 | reg, RK3588_SRAM_INIT_DONE(reg), |
| 146 | 0, 500); |
| 147 | ret |= regmap_read_poll_timeout(priv->phy_grf, |
| 148 | RK3588_PCIE3PHY_GRF_PHY1_STATUS1, |
| 149 | reg, RK3588_SRAM_INIT_DONE(reg), |
| 150 | 0, 500); |
| 151 | if (ret) |
| 152 | dev_err(phy->dev, "lock failed 0x%x\n", reg); |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | static const struct rockchip_p3phy_ops rk3588_ops = { |
| 158 | .phy_init = rockchip_p3phy_rk3588_init, |
| 159 | }; |
| 160 | |
Sebastian Kropatsch | b4ab3e5 | 2024-07-14 23:23:41 +0200 | [diff] [blame] | 161 | static int rockchip_p3phy_init(struct phy *phy) |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 162 | { |
Jonas Karlman | a10080c | 2023-08-02 19:04:30 +0000 | [diff] [blame] | 163 | struct rockchip_p3phy_ops *ops = |
| 164 | (struct rockchip_p3phy_ops *)dev_get_driver_data(phy->dev); |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 165 | struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev); |
| 166 | int ret; |
| 167 | |
Jonas Karlman | 6153aed | 2023-08-02 19:04:29 +0000 | [diff] [blame] | 168 | ret = clk_enable_bulk(&priv->clks); |
| 169 | if (ret) |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 170 | return ret; |
| 171 | |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 172 | reset_assert(&priv->p30phy); |
| 173 | udelay(1); |
| 174 | |
Jonas Karlman | a10080c | 2023-08-02 19:04:30 +0000 | [diff] [blame] | 175 | ret = ops->phy_init(phy); |
| 176 | if (ret) |
| 177 | clk_disable_bulk(&priv->clks); |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 178 | |
Jonas Karlman | a10080c | 2023-08-02 19:04:30 +0000 | [diff] [blame] | 179 | return ret; |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 180 | } |
| 181 | |
Sebastian Kropatsch | b4ab3e5 | 2024-07-14 23:23:41 +0200 | [diff] [blame] | 182 | static int rockchip_p3phy_exit(struct phy *phy) |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 183 | { |
| 184 | struct rockchip_p3phy_priv *priv = dev_get_priv(phy->dev); |
| 185 | |
Jonas Karlman | 6153aed | 2023-08-02 19:04:29 +0000 | [diff] [blame] | 186 | clk_disable_bulk(&priv->clks); |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 187 | reset_assert(&priv->p30phy); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static int rockchip_p3phy_probe(struct udevice *dev) |
| 193 | { |
| 194 | struct rockchip_p3phy_priv *priv = dev_get_priv(dev); |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 195 | int ret; |
| 196 | |
Johan Jonker | 8d5d8e0 | 2023-03-13 01:32:04 +0100 | [diff] [blame] | 197 | priv->mmio = dev_read_addr_ptr(dev); |
| 198 | if (!priv->mmio) |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 199 | return -EINVAL; |
| 200 | |
Jonas Karlman | 6153aed | 2023-08-02 19:04:29 +0000 | [diff] [blame] | 201 | priv->phy_grf = syscon_regmap_lookup_by_phandle(dev, "rockchip,phy-grf"); |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 202 | if (IS_ERR(priv->phy_grf)) { |
| 203 | dev_err(dev, "failed to find rockchip,phy_grf regmap\n"); |
| 204 | return PTR_ERR(priv->phy_grf); |
| 205 | } |
| 206 | |
Jonas Karlman | 3f24f8b | 2023-08-02 19:04:32 +0000 | [diff] [blame] | 207 | if (device_is_compatible(dev, "rockchip,rk3588-pcie3-phy")) { |
| 208 | priv->pipe_grf = |
| 209 | syscon_regmap_lookup_by_phandle(dev, "rockchip,pipe-grf"); |
| 210 | if (IS_ERR(priv->pipe_grf)) { |
| 211 | dev_err(dev, "failed to find rockchip,pipe_grf regmap\n"); |
| 212 | return PTR_ERR(priv->pipe_grf); |
| 213 | } |
| 214 | } |
| 215 | |
Jonas Karlman | b7ca884 | 2023-08-02 19:28:33 +0000 | [diff] [blame] | 216 | ret = dev_read_size(dev, "data-lanes"); |
| 217 | if (ret > 0) { |
| 218 | priv->num_lanes = ret / sizeof(u32); |
| 219 | if (priv->num_lanes < 2 || |
| 220 | priv->num_lanes > ARRAY_SIZE(priv->lanes)) { |
| 221 | dev_err(dev, "unsupported data-lanes property size\n"); |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | |
| 225 | ret = dev_read_u32_array(dev, "data-lanes", priv->lanes, |
| 226 | priv->num_lanes); |
| 227 | if (ret) { |
| 228 | dev_err(dev, "failed to read data-lanes property\n"); |
| 229 | return ret; |
| 230 | } |
| 231 | } |
| 232 | |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 233 | ret = reset_get_by_name(dev, "phy", &priv->p30phy); |
| 234 | if (ret) { |
| 235 | dev_err(dev, "no phy reset control specified\n"); |
| 236 | return ret; |
| 237 | } |
| 238 | |
Jonas Karlman | 6153aed | 2023-08-02 19:04:29 +0000 | [diff] [blame] | 239 | ret = clk_get_bulk(dev, &priv->clks); |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 240 | if (ret) { |
Jonas Karlman | 6153aed | 2023-08-02 19:04:29 +0000 | [diff] [blame] | 241 | dev_err(dev, "failed to get clocks\n"); |
| 242 | return ret; |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
Sebastian Kropatsch | b4ab3e5 | 2024-07-14 23:23:41 +0200 | [diff] [blame] | 248 | static struct phy_ops rockchip_p3phy_ops = { |
| 249 | .init = rockchip_p3phy_init, |
| 250 | .exit = rockchip_p3phy_exit, |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | static const struct udevice_id rockchip_p3phy_of_match[] = { |
Jonas Karlman | a10080c | 2023-08-02 19:04:30 +0000 | [diff] [blame] | 254 | { |
| 255 | .compatible = "rockchip,rk3568-pcie3-phy", |
| 256 | .data = (ulong)&rk3568_ops, |
| 257 | }, |
Jonas Karlman | 3f24f8b | 2023-08-02 19:04:32 +0000 | [diff] [blame] | 258 | { |
| 259 | .compatible = "rockchip,rk3588-pcie3-phy", |
| 260 | .data = (ulong)&rk3588_ops, |
| 261 | }, |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 262 | { }, |
| 263 | }; |
| 264 | |
| 265 | U_BOOT_DRIVER(rockchip_pcie3phy) = { |
| 266 | .name = "rockchip_pcie3phy", |
| 267 | .id = UCLASS_PHY, |
| 268 | .of_match = rockchip_p3phy_of_match, |
Sebastian Kropatsch | b4ab3e5 | 2024-07-14 23:23:41 +0200 | [diff] [blame] | 269 | .ops = &rockchip_p3phy_ops, |
Shawn Lin | aa2bd2e | 2021-01-15 18:01:21 +0800 | [diff] [blame] | 270 | .probe = rockchip_p3phy_probe, |
| 271 | .priv_auto = sizeof(struct rockchip_p3phy_priv), |
| 272 | }; |