Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Rockchip DesignWare based PCIe host controller driver |
| 4 | * |
| 5 | * Copyright (c) 2021 Rockchip, Inc. |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <clk.h> |
| 10 | #include <dm.h> |
| 11 | #include <generic-phy.h> |
| 12 | #include <pci.h> |
| 13 | #include <power-domain.h> |
| 14 | #include <reset.h> |
| 15 | #include <syscon.h> |
| 16 | #include <asm/arch-rockchip/clock.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 18 | #include <asm/io.h> |
| 19 | #include <asm-generic/gpio.h> |
| 20 | #include <dm/device_compat.h> |
| 21 | #include <linux/iopoll.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <power/regulator.h> |
| 24 | |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 25 | #include "pcie_dw_common.h" |
| 26 | |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
| 29 | /** |
| 30 | * struct rk_pcie - RK DW PCIe controller state |
| 31 | * |
| 32 | * @vpcie3v3: The 3.3v power supply for slot |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 33 | * @apb_base: The base address of vendor regs |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 34 | * @rst_gpio: The #PERST signal for slot |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 35 | */ |
| 36 | struct rk_pcie { |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 37 | /* Must be first member of the struct */ |
| 38 | struct pcie_dw dw; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 39 | struct udevice *vpcie3v3; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 40 | void *apb_base; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 41 | struct phy phy; |
| 42 | struct clk_bulk clks; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 43 | struct reset_ctl_bulk rsts; |
| 44 | struct gpio_desc rst_gpio; |
Jon Lin | 014a319 | 2023-04-27 10:35:33 +0300 | [diff] [blame] | 45 | u32 gen; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /* Parameters for the waiting for iATU enabled routine */ |
| 49 | #define PCIE_CLIENT_GENERAL_DEBUG 0x104 |
| 50 | #define PCIE_CLIENT_HOT_RESET_CTRL 0x180 |
| 51 | #define PCIE_LTSSM_ENABLE_ENHANCE BIT(4) |
| 52 | #define PCIE_CLIENT_LTSSM_STATUS 0x300 |
| 53 | #define SMLH_LINKUP BIT(16) |
| 54 | #define RDLH_LINKUP BIT(17) |
| 55 | #define PCIE_CLIENT_DBG_FIFO_MODE_CON 0x310 |
| 56 | #define PCIE_CLIENT_DBG_FIFO_PTN_HIT_D0 0x320 |
| 57 | #define PCIE_CLIENT_DBG_FIFO_PTN_HIT_D1 0x324 |
| 58 | #define PCIE_CLIENT_DBG_FIFO_TRN_HIT_D0 0x328 |
| 59 | #define PCIE_CLIENT_DBG_FIFO_TRN_HIT_D1 0x32c |
| 60 | #define PCIE_CLIENT_DBG_FIFO_STATUS 0x350 |
| 61 | #define PCIE_CLIENT_DBG_TRANSITION_DATA 0xffff0000 |
| 62 | #define PCIE_CLIENT_DBF_EN 0xffff0003 |
| 63 | |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 64 | static int rk_pcie_read(void __iomem *addr, int size, u32 *val) |
| 65 | { |
| 66 | if ((uintptr_t)addr & (size - 1)) { |
| 67 | *val = 0; |
Anand Moon | a122d3a | 2021-06-05 14:38:41 +0000 | [diff] [blame] | 68 | return -EOPNOTSUPP; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | if (size == 4) { |
| 72 | *val = readl(addr); |
| 73 | } else if (size == 2) { |
| 74 | *val = readw(addr); |
| 75 | } else if (size == 1) { |
| 76 | *val = readb(addr); |
| 77 | } else { |
| 78 | *val = 0; |
| 79 | return -ENODEV; |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static int rk_pcie_write(void __iomem *addr, int size, u32 val) |
| 86 | { |
| 87 | if ((uintptr_t)addr & (size - 1)) |
Anand Moon | a122d3a | 2021-06-05 14:38:41 +0000 | [diff] [blame] | 88 | return -EOPNOTSUPP; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 89 | |
| 90 | if (size == 4) |
| 91 | writel(val, addr); |
| 92 | else if (size == 2) |
| 93 | writew(val, addr); |
| 94 | else if (size == 1) |
| 95 | writeb(val, addr); |
| 96 | else |
| 97 | return -ENODEV; |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static u32 __rk_pcie_read_apb(struct rk_pcie *rk_pcie, void __iomem *base, |
| 103 | u32 reg, size_t size) |
| 104 | { |
| 105 | int ret; |
| 106 | u32 val; |
| 107 | |
| 108 | ret = rk_pcie_read(base + reg, size, &val); |
| 109 | if (ret) |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 110 | dev_err(rk_pcie->dw.dev, "Read APB address failed\n"); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 111 | |
| 112 | return val; |
| 113 | } |
| 114 | |
| 115 | static void __rk_pcie_write_apb(struct rk_pcie *rk_pcie, void __iomem *base, |
| 116 | u32 reg, size_t size, u32 val) |
| 117 | { |
| 118 | int ret; |
| 119 | |
| 120 | ret = rk_pcie_write(base + reg, size, val); |
| 121 | if (ret) |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 122 | dev_err(rk_pcie->dw.dev, "Write APB address failed\n"); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
| 126 | * rk_pcie_readl_apb() - Read vendor regs |
| 127 | * |
| 128 | * @rk_pcie: Pointer to the PCI controller state |
| 129 | * @reg: Offset of regs |
| 130 | */ |
| 131 | static inline u32 rk_pcie_readl_apb(struct rk_pcie *rk_pcie, u32 reg) |
| 132 | { |
| 133 | return __rk_pcie_read_apb(rk_pcie, rk_pcie->apb_base, reg, 0x4); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * rk_pcie_writel_apb() - Write vendor regs |
| 138 | * |
| 139 | * @rk_pcie: Pointer to the PCI controller state |
| 140 | * @reg: Offset of regs |
| 141 | * @val: Value to be writen |
| 142 | */ |
| 143 | static inline void rk_pcie_writel_apb(struct rk_pcie *rk_pcie, u32 reg, |
| 144 | u32 val) |
| 145 | { |
| 146 | __rk_pcie_write_apb(rk_pcie, rk_pcie->apb_base, reg, 0x4, val); |
| 147 | } |
| 148 | |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 149 | /** |
| 150 | * rk_pcie_configure() - Configure link capabilities and speed |
| 151 | * |
| 152 | * @rk_pcie: Pointer to the PCI controller state |
| 153 | * @cap_speed: The capabilities and speed to configure |
| 154 | * |
| 155 | * Configure the link capabilities and speed in the PCIe root complex. |
| 156 | */ |
| 157 | static void rk_pcie_configure(struct rk_pcie *pci, u32 cap_speed) |
| 158 | { |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 159 | dw_pcie_dbi_write_enable(&pci->dw, true); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 160 | |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 161 | clrsetbits_le32(pci->dw.dbi_base + PCIE_LINK_CAPABILITY, |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 162 | TARGET_LINK_SPEED_MASK, cap_speed); |
| 163 | |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 164 | clrsetbits_le32(pci->dw.dbi_base + PCIE_LINK_CTL_2, |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 165 | TARGET_LINK_SPEED_MASK, cap_speed); |
| 166 | |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 167 | dw_pcie_dbi_write_enable(&pci->dw, false); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static void rk_pcie_enable_debug(struct rk_pcie *rk_pcie) |
| 171 | { |
| 172 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_PTN_HIT_D0, |
| 173 | PCIE_CLIENT_DBG_TRANSITION_DATA); |
| 174 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_PTN_HIT_D1, |
| 175 | PCIE_CLIENT_DBG_TRANSITION_DATA); |
| 176 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_TRN_HIT_D0, |
| 177 | PCIE_CLIENT_DBG_TRANSITION_DATA); |
| 178 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_TRN_HIT_D1, |
| 179 | PCIE_CLIENT_DBG_TRANSITION_DATA); |
| 180 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_MODE_CON, |
| 181 | PCIE_CLIENT_DBF_EN); |
| 182 | } |
| 183 | |
| 184 | static void rk_pcie_debug_dump(struct rk_pcie *rk_pcie) |
| 185 | { |
| 186 | u32 loop; |
| 187 | |
| 188 | debug("ltssm = 0x%x\n", |
| 189 | rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_LTSSM_STATUS)); |
| 190 | for (loop = 0; loop < 64; loop++) |
| 191 | debug("fifo_status = 0x%x\n", |
| 192 | rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_STATUS)); |
| 193 | } |
| 194 | |
| 195 | static inline void rk_pcie_link_status_clear(struct rk_pcie *rk_pcie) |
| 196 | { |
| 197 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_GENERAL_DEBUG, 0x0); |
| 198 | } |
| 199 | |
| 200 | static inline void rk_pcie_disable_ltssm(struct rk_pcie *rk_pcie) |
| 201 | { |
| 202 | rk_pcie_writel_apb(rk_pcie, 0x0, 0xc0008); |
| 203 | } |
| 204 | |
| 205 | static inline void rk_pcie_enable_ltssm(struct rk_pcie *rk_pcie) |
| 206 | { |
| 207 | rk_pcie_writel_apb(rk_pcie, 0x0, 0xc000c); |
| 208 | } |
| 209 | |
| 210 | static int is_link_up(struct rk_pcie *priv) |
| 211 | { |
| 212 | u32 val; |
| 213 | |
| 214 | val = rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS); |
| 215 | if ((val & (RDLH_LINKUP | SMLH_LINKUP)) == 0x30000 && |
| 216 | (val & GENMASK(5, 0)) == 0x11) |
| 217 | return 1; |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * rk_pcie_link_up() - Wait for the link to come up |
| 224 | * |
| 225 | * @rk_pcie: Pointer to the PCI controller state |
| 226 | * @cap_speed: Desired link speed |
| 227 | * |
| 228 | * Return: 1 (true) for active line and negetive (false) for no link (timeout) |
| 229 | */ |
| 230 | static int rk_pcie_link_up(struct rk_pcie *priv, u32 cap_speed) |
| 231 | { |
| 232 | int retries; |
| 233 | |
| 234 | if (is_link_up(priv)) { |
| 235 | printf("PCI Link already up before configuration!\n"); |
| 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | /* DW pre link configurations */ |
| 240 | rk_pcie_configure(priv, cap_speed); |
| 241 | |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 242 | rk_pcie_disable_ltssm(priv); |
| 243 | rk_pcie_link_status_clear(priv); |
| 244 | rk_pcie_enable_debug(priv); |
| 245 | |
Jonas Karlman | 7ce186a | 2023-07-22 13:30:19 +0000 | [diff] [blame^] | 246 | /* Reset the device */ |
| 247 | if (dm_gpio_is_valid(&priv->rst_gpio)) |
| 248 | dm_gpio_set_value(&priv->rst_gpio, 0); |
| 249 | |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 250 | /* Enable LTSSM */ |
| 251 | rk_pcie_enable_ltssm(priv); |
| 252 | |
Jonas Karlman | 7ce186a | 2023-07-22 13:30:19 +0000 | [diff] [blame^] | 253 | /* |
| 254 | * PCIe requires the refclk to be stable for 100ms prior to releasing |
| 255 | * PERST. See table 2-4 in section 2.6.2 AC Specifications of the PCI |
| 256 | * Express Card Electromechanical Specification, 1.1. However, we don't |
| 257 | * know if the refclk is coming from RC's PHY or external OSC. If it's |
| 258 | * from RC, so enabling LTSSM is the just right place to release #PERST. |
| 259 | */ |
| 260 | mdelay(100); |
| 261 | if (dm_gpio_is_valid(&priv->rst_gpio)) |
| 262 | dm_gpio_set_value(&priv->rst_gpio, 1); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 263 | |
Jonas Karlman | 7ce186a | 2023-07-22 13:30:19 +0000 | [diff] [blame^] | 264 | /* Check if the link is up or not */ |
| 265 | for (retries = 0; retries < 10; retries++) { |
| 266 | if (is_link_up(priv)) |
| 267 | break; |
| 268 | |
| 269 | mdelay(100); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 270 | } |
| 271 | |
Jonas Karlman | 7ce186a | 2023-07-22 13:30:19 +0000 | [diff] [blame^] | 272 | if (retries >= 10) { |
| 273 | dev_err(priv->dw.dev, "PCIe-%d Link Fail\n", |
| 274 | dev_seq(priv->dw.dev)); |
| 275 | return -EIO; |
| 276 | } |
| 277 | |
| 278 | dev_info(priv->dw.dev, "PCIe Link up, LTSSM is 0x%x\n", |
| 279 | rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS)); |
| 280 | rk_pcie_debug_dump(priv); |
| 281 | return 0; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | static int rockchip_pcie_init_port(struct udevice *dev) |
| 285 | { |
| 286 | int ret; |
| 287 | u32 val; |
| 288 | struct rk_pcie *priv = dev_get_priv(dev); |
| 289 | |
Jonas Karlman | 7ce186a | 2023-07-22 13:30:19 +0000 | [diff] [blame^] | 290 | ret = reset_assert_bulk(&priv->rsts); |
| 291 | if (ret) { |
| 292 | dev_err(dev, "failed to assert resets (ret=%d)\n", ret); |
| 293 | return ret; |
| 294 | } |
| 295 | |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 296 | /* Set power and maybe external ref clk input */ |
Jonas Karlman | 8b001ee | 2023-07-22 13:30:18 +0000 | [diff] [blame] | 297 | ret = regulator_set_enable_if_allowed(priv->vpcie3v3, true); |
| 298 | if (ret && ret != -ENOSYS) { |
| 299 | dev_err(dev, "failed to enable vpcie3v3 (ret=%d)\n", ret); |
| 300 | return ret; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 301 | } |
| 302 | |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 303 | ret = generic_phy_init(&priv->phy); |
| 304 | if (ret) { |
| 305 | dev_err(dev, "failed to init phy (ret=%d)\n", ret); |
Jonas Karlman | 8b001ee | 2023-07-22 13:30:18 +0000 | [diff] [blame] | 306 | goto err_disable_regulator; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | ret = generic_phy_power_on(&priv->phy); |
| 310 | if (ret) { |
| 311 | dev_err(dev, "failed to power on phy (ret=%d)\n", ret); |
| 312 | goto err_exit_phy; |
| 313 | } |
| 314 | |
| 315 | ret = reset_deassert_bulk(&priv->rsts); |
| 316 | if (ret) { |
| 317 | dev_err(dev, "failed to deassert resets (ret=%d)\n", ret); |
| 318 | goto err_power_off_phy; |
| 319 | } |
| 320 | |
| 321 | ret = clk_enable_bulk(&priv->clks); |
| 322 | if (ret) { |
| 323 | dev_err(dev, "failed to enable clks (ret=%d)\n", ret); |
| 324 | goto err_deassert_bulk; |
| 325 | } |
| 326 | |
| 327 | /* LTSSM EN ctrl mode */ |
| 328 | val = rk_pcie_readl_apb(priv, PCIE_CLIENT_HOT_RESET_CTRL); |
| 329 | val |= PCIE_LTSSM_ENABLE_ENHANCE | (PCIE_LTSSM_ENABLE_ENHANCE << 16); |
| 330 | rk_pcie_writel_apb(priv, PCIE_CLIENT_HOT_RESET_CTRL, val); |
| 331 | |
| 332 | /* Set RC mode */ |
| 333 | rk_pcie_writel_apb(priv, 0x0, 0xf00040); |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 334 | pcie_dw_setup_host(&priv->dw); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 335 | |
Jon Lin | 014a319 | 2023-04-27 10:35:33 +0300 | [diff] [blame] | 336 | ret = rk_pcie_link_up(priv, priv->gen); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 337 | if (ret < 0) |
| 338 | goto err_link_up; |
| 339 | |
| 340 | return 0; |
| 341 | err_link_up: |
| 342 | clk_disable_bulk(&priv->clks); |
| 343 | err_deassert_bulk: |
| 344 | reset_assert_bulk(&priv->rsts); |
| 345 | err_power_off_phy: |
| 346 | generic_phy_power_off(&priv->phy); |
| 347 | err_exit_phy: |
| 348 | generic_phy_exit(&priv->phy); |
Jonas Karlman | 8b001ee | 2023-07-22 13:30:18 +0000 | [diff] [blame] | 349 | err_disable_regulator: |
| 350 | regulator_set_enable_if_allowed(priv->vpcie3v3, false); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 351 | |
| 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | static int rockchip_pcie_parse_dt(struct udevice *dev) |
| 356 | { |
| 357 | struct rk_pcie *priv = dev_get_priv(dev); |
| 358 | int ret; |
| 359 | |
Johan Jonker | e5822ec | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 360 | priv->dw.dbi_base = dev_read_addr_index_ptr(dev, 0); |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 361 | if (!priv->dw.dbi_base) |
Johan Jonker | e5822ec | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 362 | return -EINVAL; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 363 | |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 364 | dev_dbg(dev, "DBI address is 0x%p\n", priv->dw.dbi_base); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 365 | |
Johan Jonker | e5822ec | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 366 | priv->apb_base = dev_read_addr_index_ptr(dev, 1); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 367 | if (!priv->apb_base) |
Johan Jonker | e5822ec | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 368 | return -EINVAL; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 369 | |
| 370 | dev_dbg(dev, "APB address is 0x%p\n", priv->apb_base); |
| 371 | |
Jonas Karlman | bed7b2f | 2023-07-22 13:30:16 +0000 | [diff] [blame] | 372 | priv->dw.cfg_base = dev_read_addr_size_index_ptr(dev, 2, |
| 373 | &priv->dw.cfg_size); |
| 374 | if (!priv->dw.cfg_base) |
| 375 | return -EINVAL; |
| 376 | |
| 377 | dev_dbg(dev, "CFG address is 0x%p\n", priv->dw.cfg_base); |
| 378 | |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 379 | ret = gpio_request_by_name(dev, "reset-gpios", 0, |
| 380 | &priv->rst_gpio, GPIOD_IS_OUT); |
| 381 | if (ret) { |
| 382 | dev_err(dev, "failed to find reset-gpios property\n"); |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | ret = reset_get_bulk(dev, &priv->rsts); |
| 387 | if (ret) { |
| 388 | dev_err(dev, "Can't get reset: %d\n", ret); |
Eugen Hristev | e04b67a | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 389 | goto rockchip_pcie_parse_dt_err_reset_get_bulk; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | ret = clk_get_bulk(dev, &priv->clks); |
| 393 | if (ret) { |
| 394 | dev_err(dev, "Can't get clock: %d\n", ret); |
Eugen Hristev | e04b67a | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 395 | goto rockchip_pcie_parse_dt_err_clk_get_bulk; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | ret = device_get_supply_regulator(dev, "vpcie3v3-supply", |
| 399 | &priv->vpcie3v3); |
| 400 | if (ret && ret != -ENOENT) { |
| 401 | dev_err(dev, "failed to get vpcie3v3 supply (ret=%d)\n", ret); |
Eugen Hristev | e04b67a | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 402 | goto rockchip_pcie_parse_dt_err_supply_regulator; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | ret = generic_phy_get_by_index(dev, 0, &priv->phy); |
| 406 | if (ret) { |
| 407 | dev_err(dev, "failed to get pcie phy (ret=%d)\n", ret); |
Eugen Hristev | e04b67a | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 408 | goto rockchip_pcie_parse_dt_err_phy_get_by_index; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 409 | } |
| 410 | |
Jon Lin | 014a319 | 2023-04-27 10:35:33 +0300 | [diff] [blame] | 411 | priv->gen = dev_read_u32_default(dev, "max-link-speed", |
| 412 | LINK_SPEED_GEN_3); |
| 413 | |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 414 | return 0; |
Eugen Hristev | e04b67a | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 415 | |
| 416 | rockchip_pcie_parse_dt_err_phy_get_by_index: |
| 417 | /* regulators don't need release */ |
| 418 | rockchip_pcie_parse_dt_err_supply_regulator: |
| 419 | clk_release_bulk(&priv->clks); |
| 420 | rockchip_pcie_parse_dt_err_clk_get_bulk: |
| 421 | reset_release_bulk(&priv->rsts); |
| 422 | rockchip_pcie_parse_dt_err_reset_get_bulk: |
| 423 | dm_gpio_free(dev, &priv->rst_gpio); |
| 424 | return ret; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /** |
| 428 | * rockchip_pcie_probe() - Probe the PCIe bus for active link |
| 429 | * |
| 430 | * @dev: A pointer to the device being operated on |
| 431 | * |
| 432 | * Probe for an active link on the PCIe bus and configure the controller |
| 433 | * to enable this port. |
| 434 | * |
| 435 | * Return: 0 on success, else -ENODEV |
| 436 | */ |
| 437 | static int rockchip_pcie_probe(struct udevice *dev) |
| 438 | { |
| 439 | struct rk_pcie *priv = dev_get_priv(dev); |
| 440 | struct udevice *ctlr = pci_get_controller(dev); |
| 441 | struct pci_controller *hose = dev_get_uclass_priv(ctlr); |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 442 | int ret = 0; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 443 | |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 444 | priv->dw.first_busno = dev_seq(dev); |
| 445 | priv->dw.dev = dev; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 446 | |
| 447 | ret = rockchip_pcie_parse_dt(dev); |
| 448 | if (ret) |
| 449 | return ret; |
| 450 | |
| 451 | ret = rockchip_pcie_init_port(dev); |
| 452 | if (ret) |
Eugen Hristev | e04b67a | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 453 | goto rockchip_pcie_probe_err_init_port; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 454 | |
| 455 | dev_info(dev, "PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n", |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 456 | dev_seq(dev), pcie_dw_get_link_speed(&priv->dw), |
| 457 | pcie_dw_get_link_width(&priv->dw), |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 458 | hose->first_busno); |
| 459 | |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 460 | |
Eugen Hristev | e04b67a | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 461 | ret = pcie_dw_prog_outbound_atu_unroll(&priv->dw, |
| 462 | PCIE_ATU_REGION_INDEX0, |
| 463 | PCIE_ATU_TYPE_MEM, |
| 464 | priv->dw.mem.phys_start, |
| 465 | priv->dw.mem.bus_start, |
| 466 | priv->dw.mem.size); |
| 467 | if (!ret) |
| 468 | return ret; |
| 469 | |
| 470 | rockchip_pcie_probe_err_init_port: |
| 471 | clk_release_bulk(&priv->clks); |
| 472 | reset_release_bulk(&priv->rsts); |
| 473 | dm_gpio_free(dev, &priv->rst_gpio); |
| 474 | |
| 475 | return ret; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | static const struct dm_pci_ops rockchip_pcie_ops = { |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 479 | .read_config = pcie_dw_read_config, |
| 480 | .write_config = pcie_dw_write_config, |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 481 | }; |
| 482 | |
| 483 | static const struct udevice_id rockchip_pcie_ids[] = { |
| 484 | { .compatible = "rockchip,rk3568-pcie" }, |
Jon Lin | 5374480 | 2023-04-27 10:35:32 +0300 | [diff] [blame] | 485 | { .compatible = "rockchip,rk3588-pcie" }, |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 486 | { } |
| 487 | }; |
| 488 | |
| 489 | U_BOOT_DRIVER(rockchip_dw_pcie) = { |
| 490 | .name = "pcie_dw_rockchip", |
| 491 | .id = UCLASS_PCI, |
| 492 | .of_match = rockchip_pcie_ids, |
| 493 | .ops = &rockchip_pcie_ops, |
| 494 | .probe = rockchip_pcie_probe, |
| 495 | .priv_auto = sizeof(struct rk_pcie), |
| 496 | }; |