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 | |
Jon Lin | 97be165 | 2023-07-22 13:30:20 +0000 | [diff] [blame] | 64 | #define PCIE_TYPE0_HDR_DBI2_OFFSET 0x100000 |
| 65 | |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 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 | e4e8745 | 2021-06-05 14:38:41 +0000 | [diff] [blame] | 70 | return -EOPNOTSUPP; |
Shawn Lin | c0649da | 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 | e4e8745 | 2021-06-05 14:38:41 +0000 | [diff] [blame] | 90 | return -EOPNOTSUPP; |
Shawn Lin | c0649da | 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 | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 112 | dev_err(rk_pcie->dw.dev, "Read APB address failed\n"); |
Shawn Lin | c0649da | 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 | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 124 | dev_err(rk_pcie->dw.dev, "Write APB address failed\n"); |
Shawn Lin | c0649da | 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 | c0649da | 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 | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 161 | dw_pcie_dbi_write_enable(&pci->dw, true); |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 162 | |
Jon Lin | 97be165 | 2023-07-22 13:30:20 +0000 | [diff] [blame] | 163 | /* Disable BAR 0 and BAR 1 */ |
| 164 | writel(0, pci->dw.dbi_base + PCIE_TYPE0_HDR_DBI2_OFFSET + |
| 165 | PCI_BASE_ADDRESS_0); |
| 166 | writel(0, pci->dw.dbi_base + PCIE_TYPE0_HDR_DBI2_OFFSET + |
| 167 | PCI_BASE_ADDRESS_1); |
| 168 | |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 169 | clrsetbits_le32(pci->dw.dbi_base + PCIE_LINK_CAPABILITY, |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 170 | TARGET_LINK_SPEED_MASK, cap_speed); |
| 171 | |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 172 | clrsetbits_le32(pci->dw.dbi_base + PCIE_LINK_CTL_2, |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 173 | TARGET_LINK_SPEED_MASK, cap_speed); |
| 174 | |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 175 | dw_pcie_dbi_write_enable(&pci->dw, false); |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 176 | } |
| 177 | |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 178 | static void rk_pcie_enable_debug(struct rk_pcie *rk_pcie) |
| 179 | { |
| 180 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_PTN_HIT_D0, |
| 181 | PCIE_CLIENT_DBG_TRANSITION_DATA); |
| 182 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_PTN_HIT_D1, |
| 183 | PCIE_CLIENT_DBG_TRANSITION_DATA); |
| 184 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_TRN_HIT_D0, |
| 185 | PCIE_CLIENT_DBG_TRANSITION_DATA); |
| 186 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_TRN_HIT_D1, |
| 187 | PCIE_CLIENT_DBG_TRANSITION_DATA); |
| 188 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_MODE_CON, |
| 189 | PCIE_CLIENT_DBF_EN); |
| 190 | } |
| 191 | |
| 192 | static void rk_pcie_debug_dump(struct rk_pcie *rk_pcie) |
| 193 | { |
| 194 | u32 loop; |
| 195 | |
| 196 | debug("ltssm = 0x%x\n", |
| 197 | rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_LTSSM_STATUS)); |
| 198 | for (loop = 0; loop < 64; loop++) |
| 199 | debug("fifo_status = 0x%x\n", |
| 200 | rk_pcie_readl_apb(rk_pcie, PCIE_CLIENT_DBG_FIFO_STATUS)); |
| 201 | } |
| 202 | |
| 203 | static inline void rk_pcie_link_status_clear(struct rk_pcie *rk_pcie) |
| 204 | { |
| 205 | rk_pcie_writel_apb(rk_pcie, PCIE_CLIENT_GENERAL_DEBUG, 0x0); |
| 206 | } |
| 207 | |
| 208 | static inline void rk_pcie_disable_ltssm(struct rk_pcie *rk_pcie) |
| 209 | { |
| 210 | rk_pcie_writel_apb(rk_pcie, 0x0, 0xc0008); |
| 211 | } |
| 212 | |
| 213 | static inline void rk_pcie_enable_ltssm(struct rk_pcie *rk_pcie) |
| 214 | { |
| 215 | rk_pcie_writel_apb(rk_pcie, 0x0, 0xc000c); |
| 216 | } |
| 217 | |
| 218 | static int is_link_up(struct rk_pcie *priv) |
| 219 | { |
| 220 | u32 val; |
| 221 | |
| 222 | val = rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS); |
| 223 | if ((val & (RDLH_LINKUP | SMLH_LINKUP)) == 0x30000 && |
| 224 | (val & GENMASK(5, 0)) == 0x11) |
| 225 | return 1; |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * rk_pcie_link_up() - Wait for the link to come up |
| 232 | * |
| 233 | * @rk_pcie: Pointer to the PCI controller state |
| 234 | * @cap_speed: Desired link speed |
| 235 | * |
| 236 | * Return: 1 (true) for active line and negetive (false) for no link (timeout) |
| 237 | */ |
| 238 | static int rk_pcie_link_up(struct rk_pcie *priv, u32 cap_speed) |
| 239 | { |
| 240 | int retries; |
| 241 | |
| 242 | if (is_link_up(priv)) { |
| 243 | printf("PCI Link already up before configuration!\n"); |
| 244 | return 1; |
| 245 | } |
| 246 | |
| 247 | /* DW pre link configurations */ |
| 248 | rk_pcie_configure(priv, cap_speed); |
| 249 | |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 250 | rk_pcie_disable_ltssm(priv); |
| 251 | rk_pcie_link_status_clear(priv); |
| 252 | rk_pcie_enable_debug(priv); |
| 253 | |
Jonas Karlman | 64942b7 | 2023-07-22 13:30:19 +0000 | [diff] [blame] | 254 | /* Reset the device */ |
| 255 | if (dm_gpio_is_valid(&priv->rst_gpio)) |
| 256 | dm_gpio_set_value(&priv->rst_gpio, 0); |
| 257 | |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 258 | /* Enable LTSSM */ |
| 259 | rk_pcie_enable_ltssm(priv); |
| 260 | |
Jonas Karlman | 64942b7 | 2023-07-22 13:30:19 +0000 | [diff] [blame] | 261 | /* |
| 262 | * PCIe requires the refclk to be stable for 100ms prior to releasing |
| 263 | * PERST. See table 2-4 in section 2.6.2 AC Specifications of the PCI |
| 264 | * Express Card Electromechanical Specification, 1.1. However, we don't |
| 265 | * know if the refclk is coming from RC's PHY or external OSC. If it's |
| 266 | * from RC, so enabling LTSSM is the just right place to release #PERST. |
| 267 | */ |
| 268 | mdelay(100); |
| 269 | if (dm_gpio_is_valid(&priv->rst_gpio)) |
| 270 | dm_gpio_set_value(&priv->rst_gpio, 1); |
| 271 | |
| 272 | /* Check if the link is up or not */ |
| 273 | for (retries = 0; retries < 10; retries++) { |
| 274 | if (is_link_up(priv)) |
| 275 | break; |
| 276 | |
| 277 | mdelay(100); |
| 278 | } |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 279 | |
Jonas Karlman | 64942b7 | 2023-07-22 13:30:19 +0000 | [diff] [blame] | 280 | if (retries >= 10) { |
| 281 | dev_err(priv->dw.dev, "PCIe-%d Link Fail\n", |
| 282 | dev_seq(priv->dw.dev)); |
| 283 | return -EIO; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 284 | } |
| 285 | |
Jonas Karlman | 64942b7 | 2023-07-22 13:30:19 +0000 | [diff] [blame] | 286 | dev_info(priv->dw.dev, "PCIe Link up, LTSSM is 0x%x\n", |
| 287 | rk_pcie_readl_apb(priv, PCIE_CLIENT_LTSSM_STATUS)); |
| 288 | rk_pcie_debug_dump(priv); |
| 289 | return 0; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static int rockchip_pcie_init_port(struct udevice *dev) |
| 293 | { |
| 294 | int ret; |
| 295 | u32 val; |
| 296 | struct rk_pcie *priv = dev_get_priv(dev); |
| 297 | |
Jonas Karlman | 64942b7 | 2023-07-22 13:30:19 +0000 | [diff] [blame] | 298 | ret = reset_assert_bulk(&priv->rsts); |
| 299 | if (ret) { |
| 300 | dev_err(dev, "failed to assert resets (ret=%d)\n", ret); |
| 301 | return ret; |
| 302 | } |
| 303 | |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 304 | /* Set power and maybe external ref clk input */ |
Jonas Karlman | 39993bc | 2023-07-22 13:30:18 +0000 | [diff] [blame] | 305 | ret = regulator_set_enable_if_allowed(priv->vpcie3v3, true); |
| 306 | if (ret && ret != -ENOSYS) { |
| 307 | dev_err(dev, "failed to enable vpcie3v3 (ret=%d)\n", ret); |
| 308 | return ret; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 309 | } |
| 310 | |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 311 | ret = generic_phy_init(&priv->phy); |
| 312 | if (ret) { |
| 313 | dev_err(dev, "failed to init phy (ret=%d)\n", ret); |
Jonas Karlman | 39993bc | 2023-07-22 13:30:18 +0000 | [diff] [blame] | 314 | goto err_disable_regulator; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | ret = generic_phy_power_on(&priv->phy); |
| 318 | if (ret) { |
| 319 | dev_err(dev, "failed to power on phy (ret=%d)\n", ret); |
| 320 | goto err_exit_phy; |
| 321 | } |
| 322 | |
| 323 | ret = reset_deassert_bulk(&priv->rsts); |
| 324 | if (ret) { |
| 325 | dev_err(dev, "failed to deassert resets (ret=%d)\n", ret); |
| 326 | goto err_power_off_phy; |
| 327 | } |
| 328 | |
| 329 | ret = clk_enable_bulk(&priv->clks); |
| 330 | if (ret) { |
| 331 | dev_err(dev, "failed to enable clks (ret=%d)\n", ret); |
| 332 | goto err_deassert_bulk; |
| 333 | } |
| 334 | |
| 335 | /* LTSSM EN ctrl mode */ |
| 336 | val = rk_pcie_readl_apb(priv, PCIE_CLIENT_HOT_RESET_CTRL); |
| 337 | val |= PCIE_LTSSM_ENABLE_ENHANCE | (PCIE_LTSSM_ENABLE_ENHANCE << 16); |
| 338 | rk_pcie_writel_apb(priv, PCIE_CLIENT_HOT_RESET_CTRL, val); |
| 339 | |
| 340 | /* Set RC mode */ |
| 341 | rk_pcie_writel_apb(priv, 0x0, 0xf00040); |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 342 | pcie_dw_setup_host(&priv->dw); |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 343 | |
Jon Lin | 793de19 | 2023-04-27 10:35:33 +0300 | [diff] [blame] | 344 | ret = rk_pcie_link_up(priv, priv->gen); |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 345 | if (ret < 0) |
| 346 | goto err_link_up; |
| 347 | |
| 348 | return 0; |
| 349 | err_link_up: |
| 350 | clk_disable_bulk(&priv->clks); |
| 351 | err_deassert_bulk: |
| 352 | reset_assert_bulk(&priv->rsts); |
| 353 | err_power_off_phy: |
| 354 | generic_phy_power_off(&priv->phy); |
| 355 | err_exit_phy: |
| 356 | generic_phy_exit(&priv->phy); |
Jonas Karlman | 39993bc | 2023-07-22 13:30:18 +0000 | [diff] [blame] | 357 | err_disable_regulator: |
| 358 | regulator_set_enable_if_allowed(priv->vpcie3v3, false); |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 359 | |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | static int rockchip_pcie_parse_dt(struct udevice *dev) |
| 364 | { |
| 365 | struct rk_pcie *priv = dev_get_priv(dev); |
| 366 | int ret; |
| 367 | |
Johan Jonker | 5ff8812 | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 368 | priv->dw.dbi_base = dev_read_addr_index_ptr(dev, 0); |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 369 | if (!priv->dw.dbi_base) |
Johan Jonker | 5ff8812 | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 370 | return -EINVAL; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 371 | |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 372 | 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] | 373 | |
Johan Jonker | 5ff8812 | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 374 | priv->apb_base = dev_read_addr_index_ptr(dev, 1); |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 375 | if (!priv->apb_base) |
Johan Jonker | 5ff8812 | 2023-03-13 01:31:49 +0100 | [diff] [blame] | 376 | return -EINVAL; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 377 | |
| 378 | dev_dbg(dev, "APB address is 0x%p\n", priv->apb_base); |
| 379 | |
Jonas Karlman | 8746d9c | 2023-07-22 13:30:16 +0000 | [diff] [blame] | 380 | priv->dw.cfg_base = dev_read_addr_size_index_ptr(dev, 2, |
| 381 | &priv->dw.cfg_size); |
| 382 | if (!priv->dw.cfg_base) |
| 383 | return -EINVAL; |
| 384 | |
| 385 | dev_dbg(dev, "CFG address is 0x%p\n", priv->dw.cfg_base); |
| 386 | |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 387 | ret = gpio_request_by_name(dev, "reset-gpios", 0, |
| 388 | &priv->rst_gpio, GPIOD_IS_OUT); |
| 389 | if (ret) { |
| 390 | dev_err(dev, "failed to find reset-gpios property\n"); |
| 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | ret = reset_get_bulk(dev, &priv->rsts); |
| 395 | if (ret) { |
| 396 | dev_err(dev, "Can't get reset: %d\n", ret); |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 397 | goto rockchip_pcie_parse_dt_err_reset_get_bulk; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | ret = clk_get_bulk(dev, &priv->clks); |
| 401 | if (ret) { |
| 402 | dev_err(dev, "Can't get clock: %d\n", ret); |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 403 | goto rockchip_pcie_parse_dt_err_clk_get_bulk; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | ret = device_get_supply_regulator(dev, "vpcie3v3-supply", |
| 407 | &priv->vpcie3v3); |
| 408 | if (ret && ret != -ENOENT) { |
| 409 | dev_err(dev, "failed to get vpcie3v3 supply (ret=%d)\n", ret); |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 410 | goto rockchip_pcie_parse_dt_err_supply_regulator; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | ret = generic_phy_get_by_index(dev, 0, &priv->phy); |
| 414 | if (ret) { |
| 415 | dev_err(dev, "failed to get pcie phy (ret=%d)\n", ret); |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 416 | goto rockchip_pcie_parse_dt_err_phy_get_by_index; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 417 | } |
| 418 | |
Jon Lin | 793de19 | 2023-04-27 10:35:33 +0300 | [diff] [blame] | 419 | priv->gen = dev_read_u32_default(dev, "max-link-speed", |
| 420 | LINK_SPEED_GEN_3); |
| 421 | |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 422 | return 0; |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 423 | |
| 424 | rockchip_pcie_parse_dt_err_phy_get_by_index: |
| 425 | /* regulators don't need release */ |
| 426 | rockchip_pcie_parse_dt_err_supply_regulator: |
| 427 | clk_release_bulk(&priv->clks); |
| 428 | rockchip_pcie_parse_dt_err_clk_get_bulk: |
| 429 | reset_release_bulk(&priv->rsts); |
| 430 | rockchip_pcie_parse_dt_err_reset_get_bulk: |
| 431 | dm_gpio_free(dev, &priv->rst_gpio); |
| 432 | return ret; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /** |
| 436 | * rockchip_pcie_probe() - Probe the PCIe bus for active link |
| 437 | * |
| 438 | * @dev: A pointer to the device being operated on |
| 439 | * |
| 440 | * Probe for an active link on the PCIe bus and configure the controller |
| 441 | * to enable this port. |
| 442 | * |
| 443 | * Return: 0 on success, else -ENODEV |
| 444 | */ |
| 445 | static int rockchip_pcie_probe(struct udevice *dev) |
| 446 | { |
| 447 | struct rk_pcie *priv = dev_get_priv(dev); |
| 448 | struct udevice *ctlr = pci_get_controller(dev); |
| 449 | struct pci_controller *hose = dev_get_uclass_priv(ctlr); |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 450 | int ret = 0; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 451 | |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 452 | priv->dw.first_busno = dev_seq(dev); |
| 453 | priv->dw.dev = dev; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 454 | |
| 455 | ret = rockchip_pcie_parse_dt(dev); |
| 456 | if (ret) |
| 457 | return ret; |
| 458 | |
| 459 | ret = rockchip_pcie_init_port(dev); |
| 460 | if (ret) |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 461 | goto rockchip_pcie_probe_err_init_port; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 462 | |
| 463 | 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] | 464 | dev_seq(dev), pcie_dw_get_link_speed(&priv->dw), |
| 465 | pcie_dw_get_link_width(&priv->dw), |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 466 | hose->first_busno); |
| 467 | |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 468 | |
| 469 | ret = pcie_dw_prog_outbound_atu_unroll(&priv->dw, |
| 470 | PCIE_ATU_REGION_INDEX0, |
| 471 | PCIE_ATU_TYPE_MEM, |
| 472 | priv->dw.mem.phys_start, |
| 473 | priv->dw.mem.bus_start, |
| 474 | priv->dw.mem.size); |
| 475 | if (!ret) |
| 476 | return ret; |
| 477 | |
| 478 | rockchip_pcie_probe_err_init_port: |
| 479 | clk_release_bulk(&priv->clks); |
| 480 | reset_release_bulk(&priv->rsts); |
| 481 | dm_gpio_free(dev, &priv->rst_gpio); |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 482 | |
Eugen Hristev | 32a5103 | 2023-04-13 17:11:03 +0300 | [diff] [blame] | 483 | return ret; |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | static const struct dm_pci_ops rockchip_pcie_ops = { |
Neil Armstrong | cf214c6 | 2021-03-25 15:49:20 +0100 | [diff] [blame] | 487 | .read_config = pcie_dw_read_config, |
| 488 | .write_config = pcie_dw_write_config, |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | static const struct udevice_id rockchip_pcie_ids[] = { |
| 492 | { .compatible = "rockchip,rk3568-pcie" }, |
Jon Lin | 2bdea35 | 2023-04-27 10:35:32 +0300 | [diff] [blame] | 493 | { .compatible = "rockchip,rk3588-pcie" }, |
Shawn Lin | c0649da | 2021-01-15 18:01:22 +0800 | [diff] [blame] | 494 | { } |
| 495 | }; |
| 496 | |
| 497 | U_BOOT_DRIVER(rockchip_dw_pcie) = { |
| 498 | .name = "pcie_dw_rockchip", |
| 499 | .id = UCLASS_PCI, |
| 500 | .of_match = rockchip_pcie_ids, |
| 501 | .ops = &rockchip_pcie_ops, |
| 502 | .probe = rockchip_pcie_probe, |
| 503 | .priv_auto = sizeof(struct rk_pcie), |
| 504 | }; |