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