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 | /* Parameters for the waiting for #perst signal */ |
Anand Moon | 88647f0 | 2021-06-05 14:38:43 +0000 | [diff] [blame] | 65 | #define MACRO_US 1000 |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 66 | |
| 67 | static int rk_pcie_read(void __iomem *addr, int size, u32 *val) |
| 68 | { |
| 69 | if ((uintptr_t)addr & (size - 1)) { |
| 70 | *val = 0; |
Anand Moon | a122d3a | 2021-06-05 14:38:41 +0000 | [diff] [blame] | 71 | return -EOPNOTSUPP; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | if (size == 4) { |
| 75 | *val = readl(addr); |
| 76 | } else if (size == 2) { |
| 77 | *val = readw(addr); |
| 78 | } else if (size == 1) { |
| 79 | *val = readb(addr); |
| 80 | } else { |
| 81 | *val = 0; |
| 82 | return -ENODEV; |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static int rk_pcie_write(void __iomem *addr, int size, u32 val) |
| 89 | { |
| 90 | if ((uintptr_t)addr & (size - 1)) |
Anand Moon | a122d3a | 2021-06-05 14:38:41 +0000 | [diff] [blame] | 91 | return -EOPNOTSUPP; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 92 | |
| 93 | if (size == 4) |
| 94 | writel(val, addr); |
| 95 | else if (size == 2) |
| 96 | writew(val, addr); |
| 97 | else if (size == 1) |
| 98 | writeb(val, addr); |
| 99 | else |
| 100 | return -ENODEV; |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static u32 __rk_pcie_read_apb(struct rk_pcie *rk_pcie, void __iomem *base, |
| 106 | u32 reg, size_t size) |
| 107 | { |
| 108 | int ret; |
| 109 | u32 val; |
| 110 | |
| 111 | ret = rk_pcie_read(base + reg, size, &val); |
| 112 | if (ret) |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 113 | dev_err(rk_pcie->dw.dev, "Read APB address failed\n"); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 114 | |
| 115 | return val; |
| 116 | } |
| 117 | |
| 118 | static void __rk_pcie_write_apb(struct rk_pcie *rk_pcie, void __iomem *base, |
| 119 | u32 reg, size_t size, u32 val) |
| 120 | { |
| 121 | int ret; |
| 122 | |
| 123 | ret = rk_pcie_write(base + reg, size, val); |
| 124 | if (ret) |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 125 | dev_err(rk_pcie->dw.dev, "Write APB address failed\n"); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * rk_pcie_readl_apb() - Read vendor regs |
| 130 | * |
| 131 | * @rk_pcie: Pointer to the PCI controller state |
| 132 | * @reg: Offset of regs |
| 133 | */ |
| 134 | static inline u32 rk_pcie_readl_apb(struct rk_pcie *rk_pcie, u32 reg) |
| 135 | { |
| 136 | return __rk_pcie_read_apb(rk_pcie, rk_pcie->apb_base, reg, 0x4); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * rk_pcie_writel_apb() - Write vendor regs |
| 141 | * |
| 142 | * @rk_pcie: Pointer to the PCI controller state |
| 143 | * @reg: Offset of regs |
| 144 | * @val: Value to be writen |
| 145 | */ |
| 146 | static inline void rk_pcie_writel_apb(struct rk_pcie *rk_pcie, u32 reg, |
| 147 | u32 val) |
| 148 | { |
| 149 | __rk_pcie_write_apb(rk_pcie, rk_pcie->apb_base, reg, 0x4, val); |
| 150 | } |
| 151 | |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 152 | /** |
| 153 | * rk_pcie_configure() - Configure link capabilities and speed |
| 154 | * |
| 155 | * @rk_pcie: Pointer to the PCI controller state |
| 156 | * @cap_speed: The capabilities and speed to configure |
| 157 | * |
| 158 | * Configure the link capabilities and speed in the PCIe root complex. |
| 159 | */ |
| 160 | static void rk_pcie_configure(struct rk_pcie *pci, u32 cap_speed) |
| 161 | { |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 162 | dw_pcie_dbi_write_enable(&pci->dw, true); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 163 | |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 164 | clrsetbits_le32(pci->dw.dbi_base + PCIE_LINK_CAPABILITY, |
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 | clrsetbits_le32(pci->dw.dbi_base + PCIE_LINK_CTL_2, |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 168 | TARGET_LINK_SPEED_MASK, cap_speed); |
| 169 | |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 170 | dw_pcie_dbi_write_enable(&pci->dw, false); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static void rk_pcie_enable_debug(struct rk_pcie *rk_pcie) |
| 174 | { |
| 175 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_PTN_HIT_D0, |
| 176 | PCIE_CLIENT_DBG_TRANSITION_DATA); |
| 177 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_PTN_HIT_D1, |
| 178 | PCIE_CLIENT_DBG_TRANSITION_DATA); |
| 179 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_TRN_HIT_D0, |
| 180 | PCIE_CLIENT_DBG_TRANSITION_DATA); |
| 181 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_TRN_HIT_D1, |
| 182 | PCIE_CLIENT_DBG_TRANSITION_DATA); |
| 183 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_MODE_CON, |
| 184 | PCIE_CLIENT_DBF_EN); |
| 185 | } |
| 186 | |
| 187 | static void rk_pcie_debug_dump(struct rk_pcie *rk_pcie) |
| 188 | { |
| 189 | u32 loop; |
| 190 | |
| 191 | debug("ltssm = 0x%x\n", |
| 192 | rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_LTSSM_STATUS)); |
| 193 | for (loop = 0; loop < 64; loop++) |
| 194 | debug("fifo_status = 0x%x\n", |
| 195 | rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_STATUS)); |
| 196 | } |
| 197 | |
| 198 | static inline void rk_pcie_link_status_clear(struct rk_pcie *rk_pcie) |
| 199 | { |
| 200 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_GENERAL_DEBUG, 0x0); |
| 201 | } |
| 202 | |
| 203 | static inline void rk_pcie_disable_ltssm(struct rk_pcie *rk_pcie) |
| 204 | { |
| 205 | rk_pcie_writel_apb(rk_pcie, 0x0, 0xc0008); |
| 206 | } |
| 207 | |
| 208 | static inline void rk_pcie_enable_ltssm(struct rk_pcie *rk_pcie) |
| 209 | { |
| 210 | rk_pcie_writel_apb(rk_pcie, 0x0, 0xc000c); |
| 211 | } |
| 212 | |
| 213 | static int is_link_up(struct rk_pcie *priv) |
| 214 | { |
| 215 | u32 val; |
| 216 | |
| 217 | val = rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS); |
| 218 | if ((val & (RDLH_LINKUP | SMLH_LINKUP)) == 0x30000 && |
| 219 | (val & GENMASK(5, 0)) == 0x11) |
| 220 | return 1; |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * rk_pcie_link_up() - Wait for the link to come up |
| 227 | * |
| 228 | * @rk_pcie: Pointer to the PCI controller state |
| 229 | * @cap_speed: Desired link speed |
| 230 | * |
| 231 | * Return: 1 (true) for active line and negetive (false) for no link (timeout) |
| 232 | */ |
| 233 | static int rk_pcie_link_up(struct rk_pcie *priv, u32 cap_speed) |
| 234 | { |
| 235 | int retries; |
| 236 | |
| 237 | if (is_link_up(priv)) { |
| 238 | printf("PCI Link already up before configuration!\n"); |
| 239 | return 1; |
| 240 | } |
| 241 | |
| 242 | /* DW pre link configurations */ |
| 243 | rk_pcie_configure(priv, cap_speed); |
| 244 | |
| 245 | /* Rest the device */ |
| 246 | if (dm_gpio_is_valid(&priv->rst_gpio)) { |
| 247 | dm_gpio_set_value(&priv->rst_gpio, 0); |
| 248 | /* |
| 249 | * Minimal is 100ms from spec but we see |
| 250 | * some wired devices need much more, such as 600ms. |
| 251 | * Add a enough delay to cover all cases. |
| 252 | */ |
Anand Moon | 88647f0 | 2021-06-05 14:38:43 +0000 | [diff] [blame] | 253 | udelay(MACRO_US * 1000); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 254 | dm_gpio_set_value(&priv->rst_gpio, 1); |
| 255 | } |
| 256 | |
| 257 | rk_pcie_disable_ltssm(priv); |
| 258 | rk_pcie_link_status_clear(priv); |
| 259 | rk_pcie_enable_debug(priv); |
| 260 | |
| 261 | /* Enable LTSSM */ |
| 262 | rk_pcie_enable_ltssm(priv); |
| 263 | |
| 264 | for (retries = 0; retries < 5; retries++) { |
| 265 | if (is_link_up(priv)) { |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 266 | dev_info(priv->dw.dev, "PCIe Link up, LTSSM is 0x%x\n", |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 267 | rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS)); |
| 268 | rk_pcie_debug_dump(priv); |
| 269 | return 0; |
| 270 | } |
| 271 | |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 272 | dev_info(priv->dw.dev, "PCIe Linking... LTSSM is 0x%x\n", |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 273 | rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS)); |
| 274 | rk_pcie_debug_dump(priv); |
Anand Moon | 88647f0 | 2021-06-05 14:38:43 +0000 | [diff] [blame] | 275 | udelay(MACRO_US * 1000); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 276 | } |
| 277 | |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 278 | dev_err(priv->dw.dev, "PCIe-%d Link Fail\n", dev_seq(priv->dw.dev)); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 279 | /* Link maybe in Gen switch recovery but we need to wait more 1s */ |
Anand Moon | 88647f0 | 2021-06-05 14:38:43 +0000 | [diff] [blame] | 280 | udelay(MACRO_US * 1000); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 281 | return -EIO; |
| 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 | |
| 290 | /* Set power and maybe external ref clk input */ |
| 291 | if (priv->vpcie3v3) { |
| 292 | ret = regulator_set_value(priv->vpcie3v3, 3300000); |
| 293 | if (ret) { |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 294 | dev_err(priv->dw.dev, "failed to enable vpcie3v3 (ret=%d)\n", |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 295 | ret); |
| 296 | return ret; |
| 297 | } |
| 298 | } |
| 299 | |
Anand Moon | 88647f0 | 2021-06-05 14:38:43 +0000 | [diff] [blame] | 300 | udelay(MACRO_US * 1000); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 301 | |
| 302 | ret = generic_phy_init(&priv->phy); |
| 303 | if (ret) { |
| 304 | dev_err(dev, "failed to init phy (ret=%d)\n", ret); |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | ret = generic_phy_power_on(&priv->phy); |
| 309 | if (ret) { |
| 310 | dev_err(dev, "failed to power on phy (ret=%d)\n", ret); |
| 311 | goto err_exit_phy; |
| 312 | } |
| 313 | |
| 314 | ret = reset_deassert_bulk(&priv->rsts); |
| 315 | if (ret) { |
| 316 | dev_err(dev, "failed to deassert resets (ret=%d)\n", ret); |
| 317 | goto err_power_off_phy; |
| 318 | } |
| 319 | |
| 320 | ret = clk_enable_bulk(&priv->clks); |
| 321 | if (ret) { |
| 322 | dev_err(dev, "failed to enable clks (ret=%d)\n", ret); |
| 323 | goto err_deassert_bulk; |
| 324 | } |
| 325 | |
| 326 | /* LTSSM EN ctrl mode */ |
| 327 | val = rk_pcie_readl_apb(priv, PCIE_CLIENT_HOT_RESET_CTRL); |
| 328 | val |= PCIE_LTSSM_ENABLE_ENHANCE | (PCIE_LTSSM_ENABLE_ENHANCE << 16); |
| 329 | rk_pcie_writel_apb(priv, PCIE_CLIENT_HOT_RESET_CTRL, val); |
| 330 | |
| 331 | /* Set RC mode */ |
| 332 | rk_pcie_writel_apb(priv, 0x0, 0xf00040); |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 333 | pcie_dw_setup_host(&priv->dw); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 334 | |
Jon Lin | 014a319 | 2023-04-27 10:35:33 +0300 | [diff] [blame^] | 335 | ret = rk_pcie_link_up(priv, priv->gen); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 336 | if (ret < 0) |
| 337 | goto err_link_up; |
| 338 | |
| 339 | return 0; |
| 340 | err_link_up: |
| 341 | clk_disable_bulk(&priv->clks); |
| 342 | err_deassert_bulk: |
| 343 | reset_assert_bulk(&priv->rsts); |
| 344 | err_power_off_phy: |
| 345 | generic_phy_power_off(&priv->phy); |
| 346 | err_exit_phy: |
| 347 | generic_phy_exit(&priv->phy); |
| 348 | |
| 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | static int rockchip_pcie_parse_dt(struct udevice *dev) |
| 353 | { |
| 354 | struct rk_pcie *priv = dev_get_priv(dev); |
| 355 | int ret; |
| 356 | |
Johan Jonker | e5822ec | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 357 | priv->dw.dbi_base = dev_read_addr_index_ptr(dev, 0); |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 358 | if (!priv->dw.dbi_base) |
Johan Jonker | e5822ec | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 359 | return -EINVAL; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 360 | |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 361 | 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] | 362 | |
Johan Jonker | e5822ec | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 363 | priv->apb_base = dev_read_addr_index_ptr(dev, 1); |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 364 | if (!priv->apb_base) |
Johan Jonker | e5822ec | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 365 | return -EINVAL; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 366 | |
| 367 | dev_dbg(dev, "APB address is 0x%p\n", priv->apb_base); |
| 368 | |
| 369 | ret = gpio_request_by_name(dev, "reset-gpios", 0, |
| 370 | &priv->rst_gpio, GPIOD_IS_OUT); |
| 371 | if (ret) { |
| 372 | dev_err(dev, "failed to find reset-gpios property\n"); |
| 373 | return ret; |
| 374 | } |
| 375 | |
| 376 | ret = reset_get_bulk(dev, &priv->rsts); |
| 377 | if (ret) { |
| 378 | dev_err(dev, "Can't get reset: %d\n", ret); |
Eugen Hristev | e04b67a | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 379 | goto rockchip_pcie_parse_dt_err_reset_get_bulk; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | ret = clk_get_bulk(dev, &priv->clks); |
| 383 | if (ret) { |
| 384 | dev_err(dev, "Can't get clock: %d\n", ret); |
Eugen Hristev | e04b67a | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 385 | goto rockchip_pcie_parse_dt_err_clk_get_bulk; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | ret = device_get_supply_regulator(dev, "vpcie3v3-supply", |
| 389 | &priv->vpcie3v3); |
| 390 | if (ret && ret != -ENOENT) { |
| 391 | dev_err(dev, "failed to get vpcie3v3 supply (ret=%d)\n", ret); |
Eugen Hristev | e04b67a | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 392 | goto rockchip_pcie_parse_dt_err_supply_regulator; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | ret = generic_phy_get_by_index(dev, 0, &priv->phy); |
| 396 | if (ret) { |
| 397 | dev_err(dev, "failed to get pcie phy (ret=%d)\n", ret); |
Eugen Hristev | e04b67a | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 398 | goto rockchip_pcie_parse_dt_err_phy_get_by_index; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 399 | } |
| 400 | |
Jon Lin | 014a319 | 2023-04-27 10:35:33 +0300 | [diff] [blame^] | 401 | priv->gen = dev_read_u32_default(dev, "max-link-speed", |
| 402 | LINK_SPEED_GEN_3); |
| 403 | |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 404 | return 0; |
Eugen Hristev | e04b67a | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 405 | |
| 406 | rockchip_pcie_parse_dt_err_phy_get_by_index: |
| 407 | /* regulators don't need release */ |
| 408 | rockchip_pcie_parse_dt_err_supply_regulator: |
| 409 | clk_release_bulk(&priv->clks); |
| 410 | rockchip_pcie_parse_dt_err_clk_get_bulk: |
| 411 | reset_release_bulk(&priv->rsts); |
| 412 | rockchip_pcie_parse_dt_err_reset_get_bulk: |
| 413 | dm_gpio_free(dev, &priv->rst_gpio); |
| 414 | return ret; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /** |
| 418 | * rockchip_pcie_probe() - Probe the PCIe bus for active link |
| 419 | * |
| 420 | * @dev: A pointer to the device being operated on |
| 421 | * |
| 422 | * Probe for an active link on the PCIe bus and configure the controller |
| 423 | * to enable this port. |
| 424 | * |
| 425 | * Return: 0 on success, else -ENODEV |
| 426 | */ |
| 427 | static int rockchip_pcie_probe(struct udevice *dev) |
| 428 | { |
| 429 | struct rk_pcie *priv = dev_get_priv(dev); |
| 430 | struct udevice *ctlr = pci_get_controller(dev); |
| 431 | struct pci_controller *hose = dev_get_uclass_priv(ctlr); |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 432 | int ret = 0; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 433 | |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 434 | priv->dw.first_busno = dev_seq(dev); |
| 435 | priv->dw.dev = dev; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 436 | |
| 437 | ret = rockchip_pcie_parse_dt(dev); |
| 438 | if (ret) |
| 439 | return ret; |
| 440 | |
| 441 | ret = rockchip_pcie_init_port(dev); |
| 442 | if (ret) |
Eugen Hristev | e04b67a | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 443 | goto rockchip_pcie_probe_err_init_port; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 444 | |
| 445 | 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] | 446 | dev_seq(dev), pcie_dw_get_link_speed(&priv->dw), |
| 447 | pcie_dw_get_link_width(&priv->dw), |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 448 | hose->first_busno); |
| 449 | |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 450 | |
Eugen Hristev | e04b67a | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 451 | ret = pcie_dw_prog_outbound_atu_unroll(&priv->dw, |
| 452 | PCIE_ATU_REGION_INDEX0, |
| 453 | PCIE_ATU_TYPE_MEM, |
| 454 | priv->dw.mem.phys_start, |
| 455 | priv->dw.mem.bus_start, |
| 456 | priv->dw.mem.size); |
| 457 | if (!ret) |
| 458 | return ret; |
| 459 | |
| 460 | rockchip_pcie_probe_err_init_port: |
| 461 | clk_release_bulk(&priv->clks); |
| 462 | reset_release_bulk(&priv->rsts); |
| 463 | dm_gpio_free(dev, &priv->rst_gpio); |
| 464 | |
| 465 | return ret; |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static const struct dm_pci_ops rockchip_pcie_ops = { |
Neil Armstrong | c90f3d0 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 469 | .read_config = pcie_dw_read_config, |
| 470 | .write_config = pcie_dw_write_config, |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 471 | }; |
| 472 | |
| 473 | static const struct udevice_id rockchip_pcie_ids[] = { |
| 474 | { .compatible = "rockchip,rk3568-pcie" }, |
Jon Lin | 5374480 | 2023-04-27 10:35:32 +0300 | [diff] [blame] | 475 | { .compatible = "rockchip,rk3588-pcie" }, |
Shawn Lin | 9ddc078 | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 476 | { } |
| 477 | }; |
| 478 | |
| 479 | U_BOOT_DRIVER(rockchip_dw_pcie) = { |
| 480 | .name = "pcie_dw_rockchip", |
| 481 | .id = UCLASS_PCI, |
| 482 | .of_match = rockchip_pcie_ids, |
| 483 | .ops = &rockchip_pcie_ops, |
| 484 | .probe = rockchip_pcie_probe, |
| 485 | .priv_auto = sizeof(struct rk_pcie), |
| 486 | }; |