Shawn Lin | c0649da | 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 | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Shawn Lin | c0649da | 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 | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 25 | #include "pcie_dw_common.h" |
| 26 | |
Shawn Lin | c0649da | 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 | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 33 | * @apb_base: The base address of vendor regs |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 34 | * @rst_gpio: The #PERST signal for slot |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 35 | */ |
| 36 | struct rk_pcie { |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 37 | /* Must be first member of the struct */ |
| 38 | struct pcie_dw dw; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 39 | struct udevice *vpcie3v3; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 40 | void *apb_base; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 41 | struct phy phy; |
| 42 | struct clk_bulk clks; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 43 | struct reset_ctl_bulk rsts; |
| 44 | struct gpio_desc rst_gpio; |
Jon Lin | 793de19 | 2023-04-27 10:35:33 +0300 | [diff] [blame] | 45 | u32 gen; |
Shawn Lin | c0649da | 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 | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 64 | /* Parameters for the waiting for #perst signal */ |
Anand Moon | 7df6464 | 2021-06-05 14:38:43 +0000 | [diff] [blame] | 65 | #define MACRO_US 1000 |
Shawn Lin | c0649da | 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 | e4e8745 | 2021-06-05 14:38:41 +0000 | [diff] [blame] | 71 | return -EOPNOTSUPP; |
Shawn Lin | c0649da | 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 | e4e8745 | 2021-06-05 14:38:41 +0000 | [diff] [blame] | 91 | return -EOPNOTSUPP; |
Shawn Lin | c0649da | 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 | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 113 | dev_err(rk_pcie->dw.dev, "Read APB address failed\n"); |
Shawn Lin | c0649da | 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 | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 125 | dev_err(rk_pcie->dw.dev, "Write APB address failed\n"); |
Shawn Lin | c0649da | 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 | c0649da | 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 | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 162 | dw_pcie_dbi_write_enable(&pci->dw, true); |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 163 | |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 164 | clrsetbits_le32(pci->dw.dbi_base + PCIE_LINK_CAPABILITY, |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 165 | TARGET_LINK_SPEED_MASK, cap_speed); |
| 166 | |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 167 | clrsetbits_le32(pci->dw.dbi_base + PCIE_LINK_CTL_2, |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 168 | TARGET_LINK_SPEED_MASK, cap_speed); |
| 169 | |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 170 | dw_pcie_dbi_write_enable(&pci->dw, false); |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 171 | } |
| 172 | |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 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 | 7df6464 | 2021-06-05 14:38:43 +0000 | [diff] [blame] | 253 | udelay(MACRO_US * 1000); |
Shawn Lin | c0649da | 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 | cf214c6 | 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 | c0649da | 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 | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 272 | dev_info(priv->dw.dev, "PCIe Linking... LTSSM is 0x%x\n", |
Shawn Lin | c0649da | 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 | 7df6464 | 2021-06-05 14:38:43 +0000 | [diff] [blame] | 275 | udelay(MACRO_US * 1000); |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 276 | } |
| 277 | |
Neil Armstrong | cf214c6 | 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 | c0649da | 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 | 7df6464 | 2021-06-05 14:38:43 +0000 | [diff] [blame] | 280 | udelay(MACRO_US * 1000); |
Shawn Lin | c0649da | 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 | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 294 | dev_err(priv->dw.dev, "failed to enable vpcie3v3 (ret=%d)\n", |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 295 | ret); |
| 296 | return ret; |
| 297 | } |
| 298 | } |
| 299 | |
Anand Moon | 7df6464 | 2021-06-05 14:38:43 +0000 | [diff] [blame] | 300 | udelay(MACRO_US * 1000); |
Shawn Lin | c0649da | 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 | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 333 | pcie_dw_setup_host(&priv->dw); |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 334 | |
Jon Lin | 793de19 | 2023-04-27 10:35:33 +0300 | [diff] [blame] | 335 | ret = rk_pcie_link_up(priv, priv->gen); |
Shawn Lin | c0649da | 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 | 5ff8812 | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 357 | priv->dw.dbi_base = dev_read_addr_index_ptr(dev, 0); |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 358 | if (!priv->dw.dbi_base) |
Johan Jonker | 5ff8812 | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 359 | return -EINVAL; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 360 | |
Neil Armstrong | cf214c6 | 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 | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 362 | |
Johan Jonker | 5ff8812 | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 363 | priv->apb_base = dev_read_addr_index_ptr(dev, 1); |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 364 | if (!priv->apb_base) |
Johan Jonker | 5ff8812 | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 365 | return -EINVAL; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 366 | |
| 367 | dev_dbg(dev, "APB address is 0x%p\n", priv->apb_base); |
| 368 | |
Jonas Karlman | 8746d9c | 2023-07-22 13:30:16 +0000 | [diff] [blame^] | 369 | priv->dw.cfg_base = dev_read_addr_size_index_ptr(dev, 2, |
| 370 | &priv->dw.cfg_size); |
| 371 | if (!priv->dw.cfg_base) |
| 372 | return -EINVAL; |
| 373 | |
| 374 | dev_dbg(dev, "CFG address is 0x%p\n", priv->dw.cfg_base); |
| 375 | |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 376 | ret = gpio_request_by_name(dev, "reset-gpios", 0, |
| 377 | &priv->rst_gpio, GPIOD_IS_OUT); |
| 378 | if (ret) { |
| 379 | dev_err(dev, "failed to find reset-gpios property\n"); |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | ret = reset_get_bulk(dev, &priv->rsts); |
| 384 | if (ret) { |
| 385 | dev_err(dev, "Can't get reset: %d\n", ret); |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 386 | goto rockchip_pcie_parse_dt_err_reset_get_bulk; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | ret = clk_get_bulk(dev, &priv->clks); |
| 390 | if (ret) { |
| 391 | dev_err(dev, "Can't get clock: %d\n", ret); |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 392 | goto rockchip_pcie_parse_dt_err_clk_get_bulk; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | ret = device_get_supply_regulator(dev, "vpcie3v3-supply", |
| 396 | &priv->vpcie3v3); |
| 397 | if (ret && ret != -ENOENT) { |
| 398 | dev_err(dev, "failed to get vpcie3v3 supply (ret=%d)\n", ret); |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 399 | goto rockchip_pcie_parse_dt_err_supply_regulator; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | ret = generic_phy_get_by_index(dev, 0, &priv->phy); |
| 403 | if (ret) { |
| 404 | dev_err(dev, "failed to get pcie phy (ret=%d)\n", ret); |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 405 | goto rockchip_pcie_parse_dt_err_phy_get_by_index; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 406 | } |
| 407 | |
Jon Lin | 793de19 | 2023-04-27 10:35:33 +0300 | [diff] [blame] | 408 | priv->gen = dev_read_u32_default(dev, "max-link-speed", |
| 409 | LINK_SPEED_GEN_3); |
| 410 | |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 411 | return 0; |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 412 | |
| 413 | rockchip_pcie_parse_dt_err_phy_get_by_index: |
| 414 | /* regulators don't need release */ |
| 415 | rockchip_pcie_parse_dt_err_supply_regulator: |
| 416 | clk_release_bulk(&priv->clks); |
| 417 | rockchip_pcie_parse_dt_err_clk_get_bulk: |
| 418 | reset_release_bulk(&priv->rsts); |
| 419 | rockchip_pcie_parse_dt_err_reset_get_bulk: |
| 420 | dm_gpio_free(dev, &priv->rst_gpio); |
| 421 | return ret; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /** |
| 425 | * rockchip_pcie_probe() - Probe the PCIe bus for active link |
| 426 | * |
| 427 | * @dev: A pointer to the device being operated on |
| 428 | * |
| 429 | * Probe for an active link on the PCIe bus and configure the controller |
| 430 | * to enable this port. |
| 431 | * |
| 432 | * Return: 0 on success, else -ENODEV |
| 433 | */ |
| 434 | static int rockchip_pcie_probe(struct udevice *dev) |
| 435 | { |
| 436 | struct rk_pcie *priv = dev_get_priv(dev); |
| 437 | struct udevice *ctlr = pci_get_controller(dev); |
| 438 | struct pci_controller *hose = dev_get_uclass_priv(ctlr); |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 439 | int ret = 0; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 440 | |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 441 | priv->dw.first_busno = dev_seq(dev); |
| 442 | priv->dw.dev = dev; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 443 | |
| 444 | ret = rockchip_pcie_parse_dt(dev); |
| 445 | if (ret) |
| 446 | return ret; |
| 447 | |
| 448 | ret = rockchip_pcie_init_port(dev); |
| 449 | if (ret) |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 450 | goto rockchip_pcie_probe_err_init_port; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 451 | |
| 452 | dev_info(dev, "PCIE-%d: Link up (Gen%d-x%d, Bus%d)\n", |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 453 | dev_seq(dev), pcie_dw_get_link_speed(&priv->dw), |
| 454 | pcie_dw_get_link_width(&priv->dw), |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 455 | hose->first_busno); |
| 456 | |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 457 | |
| 458 | ret = pcie_dw_prog_outbound_atu_unroll(&priv->dw, |
| 459 | PCIE_ATU_REGION_INDEX0, |
| 460 | PCIE_ATU_TYPE_MEM, |
| 461 | priv->dw.mem.phys_start, |
| 462 | priv->dw.mem.bus_start, |
| 463 | priv->dw.mem.size); |
| 464 | if (!ret) |
| 465 | return ret; |
| 466 | |
| 467 | rockchip_pcie_probe_err_init_port: |
| 468 | clk_release_bulk(&priv->clks); |
| 469 | reset_release_bulk(&priv->rsts); |
| 470 | dm_gpio_free(dev, &priv->rst_gpio); |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 471 | |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 472 | return ret; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static const struct dm_pci_ops rockchip_pcie_ops = { |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 476 | .read_config = pcie_dw_read_config, |
| 477 | .write_config = pcie_dw_write_config, |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 478 | }; |
| 479 | |
| 480 | static const struct udevice_id rockchip_pcie_ids[] = { |
| 481 | { .compatible = "rockchip,rk3568-pcie" }, |
Jon Lin | 2bdea35 | 2023-04-27 10:35:32 +0300 | [diff] [blame] | 482 | { .compatible = "rockchip,rk3588-pcie" }, |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 483 | { } |
| 484 | }; |
| 485 | |
| 486 | U_BOOT_DRIVER(rockchip_dw_pcie) = { |
| 487 | .name = "pcie_dw_rockchip", |
| 488 | .id = UCLASS_PCI, |
| 489 | .of_match = rockchip_pcie_ids, |
| 490 | .ops = &rockchip_pcie_ops, |
| 491 | .probe = rockchip_pcie_probe, |
| 492 | .priv_auto = sizeof(struct rk_pcie), |
| 493 | }; |