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