Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Rockchip AXI PCIe host controller driver |
| 4 | * |
| 5 | * Copyright (c) 2016 Rockchip, Inc. |
| 6 | * Copyright (c) 2020 Amarula Solutions(India) |
| 7 | * Copyright (c) 2020 Jagan Teki <jagan@amarulasolutions.com> |
| 8 | * Copyright (c) 2019 Patrick Wildt <patrick@blueri.se> |
| 9 | * Copyright (c) 2018 Mark Kettenis <kettenis@openbsd.org> |
| 10 | * |
| 11 | * Bits taken from Linux Rockchip PCIe host controller. |
| 12 | */ |
| 13 | |
| 14 | #include <common.h> |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 15 | #include <dm.h> |
| 16 | #include <dm/device_compat.h> |
Jagan Teki | 0400f5e | 2020-07-09 23:41:03 +0530 | [diff] [blame] | 17 | #include <generic-phy.h> |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 18 | #include <pci.h> |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 19 | #include <power/regulator.h> |
| 20 | #include <reset.h> |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 21 | #include <asm-generic/gpio.h> |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 22 | #include <linux/iopoll.h> |
| 23 | |
Jagan Teki | 0400f5e | 2020-07-09 23:41:03 +0530 | [diff] [blame] | 24 | #define HIWORD_UPDATE(mask, val) (((mask) << 16) | (val)) |
| 25 | #define HIWORD_UPDATE_BIT(val) HIWORD_UPDATE(val, val) |
| 26 | |
| 27 | #define ENCODE_LANES(x) ((((x) >> 1) & 3) << 4) |
| 28 | #define PCIE_CLIENT_BASE 0x0 |
| 29 | #define PCIE_CLIENT_CONFIG (PCIE_CLIENT_BASE + 0x00) |
| 30 | #define PCIE_CLIENT_CONF_ENABLE HIWORD_UPDATE_BIT(0x0001) |
| 31 | #define PCIE_CLIENT_LINK_TRAIN_ENABLE HIWORD_UPDATE_BIT(0x0002) |
| 32 | #define PCIE_CLIENT_MODE_RC HIWORD_UPDATE_BIT(0x0040) |
| 33 | #define PCIE_CLIENT_GEN_SEL_1 HIWORD_UPDATE(0x0080, 0) |
| 34 | #define PCIE_CLIENT_BASIC_STATUS1 0x0048 |
| 35 | #define PCIE_CLIENT_LINK_STATUS_UP GENMASK(21, 20) |
| 36 | #define PCIE_CLIENT_LINK_STATUS_MASK GENMASK(21, 20) |
| 37 | #define PCIE_LINK_UP(x) \ |
| 38 | (((x) & PCIE_CLIENT_LINK_STATUS_MASK) == PCIE_CLIENT_LINK_STATUS_UP) |
| 39 | #define PCIE_RC_NORMAL_BASE 0x800000 |
| 40 | #define PCIE_LM_BASE 0x900000 |
| 41 | #define PCIE_LM_VENDOR_ID (PCIE_LM_BASE + 0x44) |
| 42 | #define PCIE_LM_VENDOR_ROCKCHIP 0x1d87 |
| 43 | #define PCIE_LM_RCBAR (PCIE_LM_BASE + 0x300) |
| 44 | #define PCIE_LM_RCBARPIE BIT(19) |
| 45 | #define PCIE_LM_RCBARPIS BIT(20) |
| 46 | #define PCIE_RC_BASE 0xa00000 |
| 47 | #define PCIE_RC_CONFIG_DCR (PCIE_RC_BASE + 0x0c4) |
| 48 | #define PCIE_RC_CONFIG_DCR_CSPL_SHIFT 18 |
| 49 | #define PCIE_RC_CONFIG_DCR_CPLS_SHIFT 26 |
| 50 | #define PCIE_RC_PCIE_LCAP (PCIE_RC_BASE + 0x0cc) |
| 51 | #define PCIE_RC_PCIE_LCAP_APMS_L0S BIT(10) |
| 52 | #define PCIE_ATR_BASE 0xc00000 |
| 53 | #define PCIE_ATR_OB_ADDR0(i) (PCIE_ATR_BASE + 0x000 + (i) * 0x20) |
| 54 | #define PCIE_ATR_OB_ADDR1(i) (PCIE_ATR_BASE + 0x004 + (i) * 0x20) |
| 55 | #define PCIE_ATR_OB_DESC0(i) (PCIE_ATR_BASE + 0x008 + (i) * 0x20) |
| 56 | #define PCIE_ATR_OB_DESC1(i) (PCIE_ATR_BASE + 0x00c + (i) * 0x20) |
| 57 | #define PCIE_ATR_IB_ADDR0(i) (PCIE_ATR_BASE + 0x800 + (i) * 0x8) |
| 58 | #define PCIE_ATR_IB_ADDR1(i) (PCIE_ATR_BASE + 0x804 + (i) * 0x8) |
| 59 | #define PCIE_ATR_HDR_MEM 0x2 |
| 60 | #define PCIE_ATR_HDR_IO 0x6 |
| 61 | #define PCIE_ATR_HDR_CFG_TYPE0 0xa |
| 62 | #define PCIE_ATR_HDR_CFG_TYPE1 0xb |
| 63 | #define PCIE_ATR_HDR_RID BIT(23) |
| 64 | |
| 65 | #define PCIE_ATR_OB_REGION0_SIZE (32 * 1024 * 1024) |
| 66 | #define PCIE_ATR_OB_REGION_SIZE (1 * 1024 * 1024) |
| 67 | |
| 68 | struct rockchip_pcie { |
| 69 | fdt_addr_t axi_base; |
| 70 | fdt_addr_t apb_base; |
| 71 | int first_busno; |
| 72 | struct udevice *dev; |
| 73 | |
| 74 | /* resets */ |
| 75 | struct reset_ctl core_rst; |
| 76 | struct reset_ctl mgmt_rst; |
| 77 | struct reset_ctl mgmt_sticky_rst; |
| 78 | struct reset_ctl pipe_rst; |
| 79 | struct reset_ctl pm_rst; |
| 80 | struct reset_ctl pclk_rst; |
| 81 | struct reset_ctl aclk_rst; |
| 82 | |
| 83 | /* gpio */ |
| 84 | struct gpio_desc ep_gpio; |
| 85 | |
| 86 | /* vpcie regulators */ |
| 87 | struct udevice *vpcie12v; |
| 88 | struct udevice *vpcie3v3; |
| 89 | struct udevice *vpcie1v8; |
| 90 | struct udevice *vpcie0v9; |
| 91 | |
| 92 | /* phy */ |
| 93 | struct phy pcie_phy; |
| 94 | }; |
| 95 | |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 96 | static int rockchip_pcie_rd_conf(const struct udevice *udev, pci_dev_t bdf, |
| 97 | uint offset, ulong *valuep, |
| 98 | enum pci_size_t size) |
| 99 | { |
| 100 | struct rockchip_pcie *priv = dev_get_priv(udev); |
| 101 | unsigned int bus = PCI_BUS(bdf); |
| 102 | unsigned int dev = PCI_DEV(bdf); |
Pali Rohár | 2376935 | 2021-11-03 01:01:05 +0100 | [diff] [blame] | 103 | int where = PCIE_ECAM_OFFSET(PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), offset & ~0x3); |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 104 | ulong value; |
| 105 | |
| 106 | if (bus == priv->first_busno && dev == 0) { |
| 107 | value = readl(priv->apb_base + PCIE_RC_NORMAL_BASE + where); |
| 108 | *valuep = pci_conv_32_to_size(value, offset, size); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | if ((bus == priv->first_busno + 1) && dev == 0) { |
| 113 | value = readl(priv->axi_base + where); |
| 114 | *valuep = pci_conv_32_to_size(value, offset, size); |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | *valuep = pci_get_ff(size); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int rockchip_pcie_wr_conf(struct udevice *udev, pci_dev_t bdf, |
| 124 | uint offset, ulong value, |
| 125 | enum pci_size_t size) |
| 126 | { |
| 127 | struct rockchip_pcie *priv = dev_get_priv(udev); |
| 128 | unsigned int bus = PCI_BUS(bdf); |
| 129 | unsigned int dev = PCI_DEV(bdf); |
Pali Rohár | 2376935 | 2021-11-03 01:01:05 +0100 | [diff] [blame] | 130 | int where = PCIE_ECAM_OFFSET(PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf), offset & ~0x3); |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 131 | ulong old; |
| 132 | |
| 133 | if (bus == priv->first_busno && dev == 0) { |
| 134 | old = readl(priv->apb_base + PCIE_RC_NORMAL_BASE + where); |
| 135 | value = pci_conv_size_to_32(old, value, offset, size); |
| 136 | writel(value, priv->apb_base + PCIE_RC_NORMAL_BASE + where); |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | if ((bus == priv->first_busno + 1) && dev == 0) { |
| 141 | old = readl(priv->axi_base + where); |
| 142 | value = pci_conv_size_to_32(old, value, offset, size); |
| 143 | writel(value, priv->axi_base + where); |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static int rockchip_pcie_atr_init(struct rockchip_pcie *priv) |
| 151 | { |
| 152 | struct udevice *ctlr = pci_get_controller(priv->dev); |
| 153 | struct pci_controller *hose = dev_get_uclass_priv(ctlr); |
| 154 | u64 addr, size, offset; |
| 155 | u32 type; |
| 156 | int i, region; |
| 157 | |
| 158 | /* Use region 0 to map PCI configuration space. */ |
| 159 | writel(25 - 1, priv->apb_base + PCIE_ATR_OB_ADDR0(0)); |
| 160 | writel(0, priv->apb_base + PCIE_ATR_OB_ADDR1(0)); |
| 161 | writel(PCIE_ATR_HDR_CFG_TYPE0 | PCIE_ATR_HDR_RID, |
| 162 | priv->apb_base + PCIE_ATR_OB_DESC0(0)); |
| 163 | writel(0, priv->apb_base + PCIE_ATR_OB_DESC1(0)); |
| 164 | |
| 165 | for (i = 0; i < hose->region_count; i++) { |
| 166 | if (hose->regions[i].flags == PCI_REGION_SYS_MEMORY) |
| 167 | continue; |
| 168 | |
| 169 | if (hose->regions[i].flags == PCI_REGION_IO) |
| 170 | type = PCIE_ATR_HDR_IO; |
| 171 | else |
| 172 | type = PCIE_ATR_HDR_MEM; |
| 173 | |
| 174 | /* Only support identity mappings. */ |
| 175 | if (hose->regions[i].bus_start != |
| 176 | hose->regions[i].phys_start) |
| 177 | return -EINVAL; |
| 178 | |
| 179 | /* Only support mappings aligned on a region boundary. */ |
| 180 | addr = hose->regions[i].bus_start; |
| 181 | if (addr & (PCIE_ATR_OB_REGION_SIZE - 1)) |
| 182 | return -EINVAL; |
| 183 | |
| 184 | /* Mappings should lie between AXI and APB regions. */ |
| 185 | size = hose->regions[i].size; |
| 186 | if (addr < (u64)priv->axi_base + PCIE_ATR_OB_REGION0_SIZE) |
| 187 | return -EINVAL; |
| 188 | if (addr + size > (u64)priv->apb_base) |
| 189 | return -EINVAL; |
| 190 | |
| 191 | offset = addr - (u64)priv->axi_base - PCIE_ATR_OB_REGION0_SIZE; |
| 192 | region = 1 + (offset / PCIE_ATR_OB_REGION_SIZE); |
| 193 | while (size > 0) { |
| 194 | writel(32 - 1, |
| 195 | priv->apb_base + PCIE_ATR_OB_ADDR0(region)); |
| 196 | writel(0, priv->apb_base + PCIE_ATR_OB_ADDR1(region)); |
| 197 | writel(type | PCIE_ATR_HDR_RID, |
| 198 | priv->apb_base + PCIE_ATR_OB_DESC0(region)); |
| 199 | writel(0, priv->apb_base + PCIE_ATR_OB_DESC1(region)); |
| 200 | |
| 201 | addr += PCIE_ATR_OB_REGION_SIZE; |
| 202 | size -= PCIE_ATR_OB_REGION_SIZE; |
| 203 | region++; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /* Passthrough inbound translations unmodified. */ |
| 208 | writel(32 - 1, priv->apb_base + PCIE_ATR_IB_ADDR0(2)); |
| 209 | writel(0, priv->apb_base + PCIE_ATR_IB_ADDR1(2)); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static int rockchip_pcie_init_port(struct udevice *dev) |
| 215 | { |
| 216 | struct rockchip_pcie *priv = dev_get_priv(dev); |
| 217 | u32 cr, val, status; |
| 218 | int ret; |
| 219 | |
| 220 | if (dm_gpio_is_valid(&priv->ep_gpio)) |
| 221 | dm_gpio_set_value(&priv->ep_gpio, 0); |
| 222 | |
| 223 | ret = reset_assert(&priv->aclk_rst); |
| 224 | if (ret) { |
| 225 | dev_err(dev, "failed to assert aclk reset (ret=%d)\n", ret); |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | ret = reset_assert(&priv->pclk_rst); |
| 230 | if (ret) { |
| 231 | dev_err(dev, "failed to assert pclk reset (ret=%d)\n", ret); |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | ret = reset_assert(&priv->pm_rst); |
| 236 | if (ret) { |
| 237 | dev_err(dev, "failed to assert pm reset (ret=%d)\n", ret); |
| 238 | return ret; |
| 239 | } |
| 240 | |
Jagan Teki | 427603b | 2020-07-09 23:41:02 +0530 | [diff] [blame] | 241 | ret = generic_phy_init(&priv->pcie_phy); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 242 | if (ret) { |
| 243 | dev_err(dev, "failed to init phy (ret=%d)\n", ret); |
| 244 | goto err_exit_phy; |
| 245 | } |
| 246 | |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 247 | ret = reset_assert(&priv->core_rst); |
| 248 | if (ret) { |
| 249 | dev_err(dev, "failed to assert core reset (ret=%d)\n", ret); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 250 | goto err_exit_phy; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | ret = reset_assert(&priv->mgmt_rst); |
| 254 | if (ret) { |
| 255 | dev_err(dev, "failed to assert mgmt reset (ret=%d)\n", ret); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 256 | goto err_exit_phy; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | ret = reset_assert(&priv->mgmt_sticky_rst); |
| 260 | if (ret) { |
| 261 | dev_err(dev, "failed to assert mgmt-sticky reset (ret=%d)\n", |
| 262 | ret); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 263 | goto err_exit_phy; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | ret = reset_assert(&priv->pipe_rst); |
| 267 | if (ret) { |
| 268 | dev_err(dev, "failed to assert pipe reset (ret=%d)\n", ret); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 269 | goto err_exit_phy; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | udelay(10); |
| 273 | |
| 274 | ret = reset_deassert(&priv->pm_rst); |
| 275 | if (ret) { |
| 276 | dev_err(dev, "failed to deassert pm reset (ret=%d)\n", ret); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 277 | goto err_exit_phy; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | ret = reset_deassert(&priv->aclk_rst); |
| 281 | if (ret) { |
| 282 | dev_err(dev, "failed to deassert aclk reset (ret=%d)\n", ret); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 283 | goto err_exit_phy; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | ret = reset_deassert(&priv->pclk_rst); |
| 287 | if (ret) { |
| 288 | dev_err(dev, "failed to deassert pclk reset (ret=%d)\n", ret); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 289 | goto err_exit_phy; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /* Select GEN1 for now */ |
| 293 | cr = PCIE_CLIENT_GEN_SEL_1; |
| 294 | /* Set Root complex mode */ |
| 295 | cr |= PCIE_CLIENT_CONF_ENABLE | PCIE_CLIENT_MODE_RC; |
| 296 | writel(cr, priv->apb_base + PCIE_CLIENT_CONFIG); |
| 297 | |
Jagan Teki | 427603b | 2020-07-09 23:41:02 +0530 | [diff] [blame] | 298 | ret = generic_phy_power_on(&priv->pcie_phy); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 299 | if (ret) { |
| 300 | dev_err(dev, "failed to power on phy (ret=%d)\n", ret); |
| 301 | goto err_power_off_phy; |
| 302 | } |
| 303 | |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 304 | ret = reset_deassert(&priv->mgmt_sticky_rst); |
| 305 | if (ret) { |
| 306 | dev_err(dev, "failed to deassert mgmt-sticky reset (ret=%d)\n", |
| 307 | ret); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 308 | goto err_power_off_phy; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | ret = reset_deassert(&priv->core_rst); |
| 312 | if (ret) { |
| 313 | dev_err(dev, "failed to deassert core reset (ret=%d)\n", ret); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 314 | goto err_power_off_phy; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | ret = reset_deassert(&priv->mgmt_rst); |
| 318 | if (ret) { |
| 319 | dev_err(dev, "failed to deassert mgmt reset (ret=%d)\n", ret); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 320 | goto err_power_off_phy; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | ret = reset_deassert(&priv->pipe_rst); |
| 324 | if (ret) { |
| 325 | dev_err(dev, "failed to deassert pipe reset (ret=%d)\n", ret); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 326 | goto err_power_off_phy; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* Enable Gen1 training */ |
| 330 | writel(PCIE_CLIENT_LINK_TRAIN_ENABLE, |
| 331 | priv->apb_base + PCIE_CLIENT_CONFIG); |
| 332 | |
| 333 | if (dm_gpio_is_valid(&priv->ep_gpio)) |
| 334 | dm_gpio_set_value(&priv->ep_gpio, 1); |
| 335 | |
| 336 | ret = readl_poll_sleep_timeout |
| 337 | (priv->apb_base + PCIE_CLIENT_BASIC_STATUS1, |
| 338 | status, PCIE_LINK_UP(status), 20, 500 * 1000); |
| 339 | if (ret) { |
| 340 | dev_err(dev, "PCIe link training gen1 timeout!\n"); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 341 | goto err_power_off_phy; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* Initialize Root Complex registers. */ |
| 345 | writel(PCIE_LM_VENDOR_ROCKCHIP, priv->apb_base + PCIE_LM_VENDOR_ID); |
Pali Rohár | 25781e2 | 2022-02-18 13:18:40 +0100 | [diff] [blame] | 346 | writel(PCI_CLASS_BRIDGE_PCI_NORMAL << 8, |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 347 | priv->apb_base + PCIE_RC_BASE + PCI_CLASS_REVISION); |
| 348 | writel(PCIE_LM_RCBARPIE | PCIE_LM_RCBARPIS, |
| 349 | priv->apb_base + PCIE_LM_RCBAR); |
| 350 | |
| 351 | if (dev_read_bool(dev, "aspm-no-l0s")) { |
| 352 | val = readl(priv->apb_base + PCIE_RC_PCIE_LCAP); |
| 353 | val &= ~PCIE_RC_PCIE_LCAP_APMS_L0S; |
| 354 | writel(val, priv->apb_base + PCIE_RC_PCIE_LCAP); |
| 355 | } |
| 356 | |
| 357 | /* Configure Address Translation. */ |
| 358 | ret = rockchip_pcie_atr_init(priv); |
| 359 | if (ret) { |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 360 | dev_err(dev, "PCIE-%d: ATR init failed\n", dev_seq(dev)); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 361 | goto err_power_off_phy; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | return 0; |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 365 | |
| 366 | err_power_off_phy: |
Jagan Teki | 427603b | 2020-07-09 23:41:02 +0530 | [diff] [blame] | 367 | generic_phy_power_off(&priv->pcie_phy); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 368 | err_exit_phy: |
Jagan Teki | 427603b | 2020-07-09 23:41:02 +0530 | [diff] [blame] | 369 | generic_phy_exit(&priv->pcie_phy); |
Jagan Teki | 5f14232 | 2020-05-09 22:26:22 +0530 | [diff] [blame] | 370 | return ret; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | static int rockchip_pcie_set_vpcie(struct udevice *dev) |
| 374 | { |
| 375 | struct rockchip_pcie *priv = dev_get_priv(dev); |
| 376 | int ret; |
| 377 | |
Jonas Karlman | 97b8216 | 2023-07-11 23:13:56 +0000 | [diff] [blame] | 378 | ret = regulator_set_enable_if_allowed(priv->vpcie12v, true); |
| 379 | if (ret && ret != -ENOSYS) { |
| 380 | dev_err(dev, "failed to enable vpcie12v (ret=%d)\n", ret); |
| 381 | return ret; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 382 | } |
| 383 | |
Jonas Karlman | 97b8216 | 2023-07-11 23:13:56 +0000 | [diff] [blame] | 384 | ret = regulator_set_enable_if_allowed(priv->vpcie3v3, true); |
| 385 | if (ret && ret != -ENOSYS) { |
| 386 | dev_err(dev, "failed to enable vpcie3v3 (ret=%d)\n", ret); |
| 387 | goto err_disable_12v; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 388 | } |
| 389 | |
Jonas Karlman | 97b8216 | 2023-07-11 23:13:56 +0000 | [diff] [blame] | 390 | ret = regulator_set_enable_if_allowed(priv->vpcie1v8, true); |
| 391 | if (ret && ret != -ENOSYS) { |
| 392 | dev_err(dev, "failed to enable vpcie1v8 (ret=%d)\n", ret); |
| 393 | goto err_disable_3v3; |
| 394 | } |
| 395 | |
| 396 | ret = regulator_set_enable_if_allowed(priv->vpcie0v9, true); |
| 397 | if (ret && ret != -ENOSYS) { |
| 398 | dev_err(dev, "failed to enable vpcie0v9 (ret=%d)\n", ret); |
| 399 | goto err_disable_1v8; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | return 0; |
| 403 | |
| 404 | err_disable_1v8: |
Jonas Karlman | 97b8216 | 2023-07-11 23:13:56 +0000 | [diff] [blame] | 405 | regulator_set_enable_if_allowed(priv->vpcie1v8, false); |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 406 | err_disable_3v3: |
Jonas Karlman | 97b8216 | 2023-07-11 23:13:56 +0000 | [diff] [blame] | 407 | regulator_set_enable_if_allowed(priv->vpcie3v3, false); |
| 408 | err_disable_12v: |
| 409 | regulator_set_enable_if_allowed(priv->vpcie12v, false); |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | static int rockchip_pcie_parse_dt(struct udevice *dev) |
| 414 | { |
| 415 | struct rockchip_pcie *priv = dev_get_priv(dev); |
| 416 | int ret; |
| 417 | |
| 418 | priv->axi_base = dev_read_addr_name(dev, "axi-base"); |
Jonas Karlman | 97b8216 | 2023-07-11 23:13:56 +0000 | [diff] [blame] | 419 | if (priv->axi_base == FDT_ADDR_T_NONE) |
| 420 | return -EINVAL; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 421 | |
| 422 | priv->apb_base = dev_read_addr_name(dev, "apb-base"); |
Jonas Karlman | 97b8216 | 2023-07-11 23:13:56 +0000 | [diff] [blame] | 423 | if (priv->apb_base == FDT_ADDR_T_NONE) |
| 424 | return -EINVAL; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 425 | |
| 426 | ret = reset_get_by_name(dev, "core", &priv->core_rst); |
| 427 | if (ret) { |
| 428 | dev_err(dev, "failed to get core reset (ret=%d)\n", ret); |
| 429 | return ret; |
| 430 | } |
| 431 | |
| 432 | ret = reset_get_by_name(dev, "mgmt", &priv->mgmt_rst); |
| 433 | if (ret) { |
| 434 | dev_err(dev, "failed to get mgmt reset (ret=%d)\n", ret); |
| 435 | return ret; |
| 436 | } |
| 437 | |
| 438 | ret = reset_get_by_name(dev, "mgmt-sticky", &priv->mgmt_sticky_rst); |
| 439 | if (ret) { |
| 440 | dev_err(dev, "failed to get mgmt-sticky reset (ret=%d)\n", ret); |
| 441 | return ret; |
| 442 | } |
| 443 | |
| 444 | ret = reset_get_by_name(dev, "pipe", &priv->pipe_rst); |
| 445 | if (ret) { |
| 446 | dev_err(dev, "failed to get pipe reset (ret=%d)\n", ret); |
| 447 | return ret; |
| 448 | } |
| 449 | |
| 450 | ret = reset_get_by_name(dev, "pm", &priv->pm_rst); |
| 451 | if (ret) { |
| 452 | dev_err(dev, "failed to get pm reset (ret=%d)\n", ret); |
| 453 | return ret; |
| 454 | } |
| 455 | |
| 456 | ret = reset_get_by_name(dev, "pclk", &priv->pclk_rst); |
| 457 | if (ret) { |
| 458 | dev_err(dev, "failed to get pclk reset (ret=%d)\n", ret); |
| 459 | return ret; |
| 460 | } |
| 461 | |
| 462 | ret = reset_get_by_name(dev, "aclk", &priv->aclk_rst); |
| 463 | if (ret) { |
| 464 | dev_err(dev, "failed to get aclk reset (ret=%d)\n", ret); |
| 465 | return ret; |
| 466 | } |
| 467 | |
Jonas Karlman | 97b8216 | 2023-07-11 23:13:56 +0000 | [diff] [blame] | 468 | ret = device_get_supply_regulator(dev, "vpcie12v-supply", |
| 469 | &priv->vpcie12v); |
| 470 | if (ret && ret != -ENOENT) { |
| 471 | dev_err(dev, "failed to get vpcie12v supply (ret=%d)\n", ret); |
| 472 | return ret; |
| 473 | } |
| 474 | |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 475 | ret = device_get_supply_regulator(dev, "vpcie3v3-supply", |
| 476 | &priv->vpcie3v3); |
| 477 | if (ret && ret != -ENOENT) { |
| 478 | dev_err(dev, "failed to get vpcie3v3 supply (ret=%d)\n", ret); |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | ret = device_get_supply_regulator(dev, "vpcie1v8-supply", |
| 483 | &priv->vpcie1v8); |
Mark Kettenis | 15b4006 | 2020-05-24 22:32:51 +0200 | [diff] [blame] | 484 | if (ret && ret != -ENOENT) { |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 485 | dev_err(dev, "failed to get vpcie1v8 supply (ret=%d)\n", ret); |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | ret = device_get_supply_regulator(dev, "vpcie0v9-supply", |
| 490 | &priv->vpcie0v9); |
Mark Kettenis | 15b4006 | 2020-05-24 22:32:51 +0200 | [diff] [blame] | 491 | if (ret && ret != -ENOENT) { |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 492 | dev_err(dev, "failed to get vpcie0v9 supply (ret=%d)\n", ret); |
| 493 | return ret; |
| 494 | } |
| 495 | |
Jagan Teki | 427603b | 2020-07-09 23:41:02 +0530 | [diff] [blame] | 496 | ret = generic_phy_get_by_index(dev, 0, &priv->pcie_phy); |
| 497 | if (ret) { |
| 498 | dev_err(dev, "failed to get pcie-phy (ret=%d)\n", ret); |
| 499 | return ret; |
| 500 | } |
| 501 | |
Jonas Karlman | 97b8216 | 2023-07-11 23:13:56 +0000 | [diff] [blame] | 502 | ret = gpio_request_by_name(dev, "ep-gpios", 0, |
| 503 | &priv->ep_gpio, GPIOD_IS_OUT); |
| 504 | if (ret) { |
| 505 | dev_err(dev, "failed to find ep-gpios property\n"); |
| 506 | return ret; |
| 507 | } |
| 508 | |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | static int rockchip_pcie_probe(struct udevice *dev) |
| 513 | { |
| 514 | struct rockchip_pcie *priv = dev_get_priv(dev); |
| 515 | struct udevice *ctlr = pci_get_controller(dev); |
| 516 | struct pci_controller *hose = dev_get_uclass_priv(ctlr); |
| 517 | int ret; |
| 518 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 519 | priv->first_busno = dev_seq(dev); |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 520 | priv->dev = dev; |
| 521 | |
| 522 | ret = rockchip_pcie_parse_dt(dev); |
| 523 | if (ret) |
| 524 | return ret; |
| 525 | |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 526 | ret = rockchip_pcie_set_vpcie(dev); |
| 527 | if (ret) |
Jonas Karlman | 97b8216 | 2023-07-11 23:13:56 +0000 | [diff] [blame] | 528 | goto err_gpio_free; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 529 | |
| 530 | ret = rockchip_pcie_init_port(dev); |
| 531 | if (ret) |
Jonas Karlman | 97b8216 | 2023-07-11 23:13:56 +0000 | [diff] [blame] | 532 | goto err_disable_vpcie; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 533 | |
| 534 | dev_info(dev, "PCIE-%d: Link up (Bus%d)\n", |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 535 | dev_seq(dev), hose->first_busno); |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 536 | |
| 537 | return 0; |
Jonas Karlman | 97b8216 | 2023-07-11 23:13:56 +0000 | [diff] [blame] | 538 | |
| 539 | err_disable_vpcie: |
| 540 | regulator_set_enable_if_allowed(priv->vpcie0v9, false); |
| 541 | regulator_set_enable_if_allowed(priv->vpcie1v8, false); |
| 542 | regulator_set_enable_if_allowed(priv->vpcie3v3, false); |
| 543 | regulator_set_enable_if_allowed(priv->vpcie12v, false); |
| 544 | err_gpio_free: |
| 545 | if (dm_gpio_is_valid(&priv->ep_gpio)) |
| 546 | dm_gpio_free(dev, &priv->ep_gpio); |
| 547 | return ret; |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | static const struct dm_pci_ops rockchip_pcie_ops = { |
| 551 | .read_config = rockchip_pcie_rd_conf, |
| 552 | .write_config = rockchip_pcie_wr_conf, |
| 553 | }; |
| 554 | |
| 555 | static const struct udevice_id rockchip_pcie_ids[] = { |
| 556 | { .compatible = "rockchip,rk3399-pcie" }, |
| 557 | { } |
| 558 | }; |
| 559 | |
| 560 | U_BOOT_DRIVER(rockchip_pcie) = { |
Jonas Karlman | 97b8216 | 2023-07-11 23:13:56 +0000 | [diff] [blame] | 561 | .name = "rockchip_pcie", |
| 562 | .id = UCLASS_PCI, |
| 563 | .of_match = rockchip_pcie_ids, |
| 564 | .ops = &rockchip_pcie_ops, |
| 565 | .probe = rockchip_pcie_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 566 | .priv_auto = sizeof(struct rockchip_pcie), |
Jagan Teki | 0226247 | 2020-05-09 22:26:21 +0530 | [diff] [blame] | 567 | }; |