Shawn Lin | e77f847 | 2016-09-03 11:41:09 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Rockchip AXI PCIe host controller driver |
| 3 | * |
| 4 | * Copyright (c) 2016 Rockchip, Inc. |
| 5 | * |
| 6 | * Author: Shawn Lin <shawn.lin@rock-chips.com> |
| 7 | * Wenrui Li <wenrui.li@rock-chips.com> |
| 8 | * |
| 9 | * Bits taken from Synopsys Designware Host controller driver and |
| 10 | * ARM PCI Host generic driver. |
| 11 | * |
| 12 | * This program is free software: you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation, either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/clk.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/gpio/consumer.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/irq.h> |
| 24 | #include <linux/irqchip/chained_irq.h> |
| 25 | #include <linux/irqdomain.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/mfd/syscon.h> |
| 28 | #include <linux/of_address.h> |
| 29 | #include <linux/of_device.h> |
| 30 | #include <linux/of_pci.h> |
| 31 | #include <linux/of_platform.h> |
| 32 | #include <linux/of_irq.h> |
| 33 | #include <linux/pci.h> |
| 34 | #include <linux/pci_ids.h> |
| 35 | #include <linux/phy/phy.h> |
| 36 | #include <linux/platform_device.h> |
| 37 | #include <linux/reset.h> |
| 38 | #include <linux/regmap.h> |
| 39 | |
| 40 | /* |
| 41 | * The upper 16 bits of PCIE_CLIENT_CONFIG are a write mask for the lower 16 |
| 42 | * bits. This allows atomic updates of the register without locking. |
| 43 | */ |
| 44 | #define HIWORD_UPDATE(mask, val) (((mask) << 16) | (val)) |
| 45 | #define HIWORD_UPDATE_BIT(val) HIWORD_UPDATE(val, val) |
| 46 | |
| 47 | #define ENCODE_LANES(x) ((((x) >> 1) & 3) << 4) |
| 48 | |
| 49 | #define PCIE_CLIENT_BASE 0x0 |
| 50 | #define PCIE_CLIENT_CONFIG (PCIE_CLIENT_BASE + 0x00) |
| 51 | #define PCIE_CLIENT_CONF_ENABLE HIWORD_UPDATE_BIT(0x0001) |
| 52 | #define PCIE_CLIENT_LINK_TRAIN_ENABLE HIWORD_UPDATE_BIT(0x0002) |
| 53 | #define PCIE_CLIENT_ARI_ENABLE HIWORD_UPDATE_BIT(0x0008) |
| 54 | #define PCIE_CLIENT_CONF_LANE_NUM(x) HIWORD_UPDATE(0x0030, ENCODE_LANES(x)) |
| 55 | #define PCIE_CLIENT_MODE_RC HIWORD_UPDATE_BIT(0x0040) |
| 56 | #define PCIE_CLIENT_GEN_SEL_2 HIWORD_UPDATE_BIT(0x0080) |
| 57 | #define PCIE_CLIENT_BASIC_STATUS1 (PCIE_CLIENT_BASE + 0x48) |
| 58 | #define PCIE_CLIENT_LINK_STATUS_UP 0x00300000 |
| 59 | #define PCIE_CLIENT_LINK_STATUS_MASK 0x00300000 |
| 60 | #define PCIE_CLIENT_INT_MASK (PCIE_CLIENT_BASE + 0x4c) |
| 61 | #define PCIE_CLIENT_INT_STATUS (PCIE_CLIENT_BASE + 0x50) |
| 62 | #define PCIE_CLIENT_INTR_MASK GENMASK(8, 5) |
| 63 | #define PCIE_CLIENT_INTR_SHIFT 5 |
| 64 | #define PCIE_CLIENT_INT_LEGACY_DONE BIT(15) |
| 65 | #define PCIE_CLIENT_INT_MSG BIT(14) |
| 66 | #define PCIE_CLIENT_INT_HOT_RST BIT(13) |
| 67 | #define PCIE_CLIENT_INT_DPA BIT(12) |
| 68 | #define PCIE_CLIENT_INT_FATAL_ERR BIT(11) |
| 69 | #define PCIE_CLIENT_INT_NFATAL_ERR BIT(10) |
| 70 | #define PCIE_CLIENT_INT_CORR_ERR BIT(9) |
| 71 | #define PCIE_CLIENT_INT_INTD BIT(8) |
| 72 | #define PCIE_CLIENT_INT_INTC BIT(7) |
| 73 | #define PCIE_CLIENT_INT_INTB BIT(6) |
| 74 | #define PCIE_CLIENT_INT_INTA BIT(5) |
| 75 | #define PCIE_CLIENT_INT_LOCAL BIT(4) |
| 76 | #define PCIE_CLIENT_INT_UDMA BIT(3) |
| 77 | #define PCIE_CLIENT_INT_PHY BIT(2) |
| 78 | #define PCIE_CLIENT_INT_HOT_PLUG BIT(1) |
| 79 | #define PCIE_CLIENT_INT_PWR_STCG BIT(0) |
| 80 | |
| 81 | #define PCIE_CLIENT_INT_LEGACY \ |
| 82 | (PCIE_CLIENT_INT_INTA | PCIE_CLIENT_INT_INTB | \ |
| 83 | PCIE_CLIENT_INT_INTC | PCIE_CLIENT_INT_INTD) |
| 84 | |
| 85 | #define PCIE_CLIENT_INT_CLI \ |
| 86 | (PCIE_CLIENT_INT_CORR_ERR | PCIE_CLIENT_INT_NFATAL_ERR | \ |
| 87 | PCIE_CLIENT_INT_FATAL_ERR | PCIE_CLIENT_INT_DPA | \ |
| 88 | PCIE_CLIENT_INT_HOT_RST | PCIE_CLIENT_INT_MSG | \ |
| 89 | PCIE_CLIENT_INT_LEGACY_DONE | PCIE_CLIENT_INT_LEGACY | \ |
| 90 | PCIE_CLIENT_INT_PHY) |
| 91 | |
| 92 | #define PCIE_CORE_CTRL_MGMT_BASE 0x900000 |
| 93 | #define PCIE_CORE_CTRL (PCIE_CORE_CTRL_MGMT_BASE + 0x000) |
| 94 | #define PCIE_CORE_PL_CONF_SPEED_5G 0x00000008 |
| 95 | #define PCIE_CORE_PL_CONF_SPEED_MASK 0x00000018 |
| 96 | #define PCIE_CORE_PL_CONF_LANE_MASK 0x00000006 |
| 97 | #define PCIE_CORE_PL_CONF_LANE_SHIFT 1 |
Shawn Lin | ca19890 | 2016-10-04 12:20:22 -0500 | [diff] [blame] | 98 | #define PCIE_CORE_CTRL_PLC1 (PCIE_CORE_CTRL_MGMT_BASE + 0x004) |
| 99 | #define PCIE_CORE_CTRL_PLC1_FTS_MASK GENMASK(23, 8) |
| 100 | #define PCIE_CORE_CTRL_PLC1_FTS_SHIFT 8 |
| 101 | #define PCIE_CORE_CTRL_PLC1_FTS_CNT 0xffff |
Rajat Jain | 277743e | 2016-09-22 17:50:42 -0700 | [diff] [blame] | 102 | #define PCIE_CORE_TXCREDIT_CFG1 (PCIE_CORE_CTRL_MGMT_BASE + 0x020) |
| 103 | #define PCIE_CORE_TXCREDIT_CFG1_MUI_MASK 0xFFFF0000 |
| 104 | #define PCIE_CORE_TXCREDIT_CFG1_MUI_SHIFT 16 |
| 105 | #define PCIE_CORE_TXCREDIT_CFG1_MUI_ENCODE(x) \ |
| 106 | (((x) >> 3) << PCIE_CORE_TXCREDIT_CFG1_MUI_SHIFT) |
Shawn Lin | e77f847 | 2016-09-03 11:41:09 -0500 | [diff] [blame] | 107 | #define PCIE_CORE_INT_STATUS (PCIE_CORE_CTRL_MGMT_BASE + 0x20c) |
| 108 | #define PCIE_CORE_INT_PRFPE BIT(0) |
| 109 | #define PCIE_CORE_INT_CRFPE BIT(1) |
| 110 | #define PCIE_CORE_INT_RRPE BIT(2) |
| 111 | #define PCIE_CORE_INT_PRFO BIT(3) |
| 112 | #define PCIE_CORE_INT_CRFO BIT(4) |
| 113 | #define PCIE_CORE_INT_RT BIT(5) |
| 114 | #define PCIE_CORE_INT_RTR BIT(6) |
| 115 | #define PCIE_CORE_INT_PE BIT(7) |
| 116 | #define PCIE_CORE_INT_MTR BIT(8) |
| 117 | #define PCIE_CORE_INT_UCR BIT(9) |
| 118 | #define PCIE_CORE_INT_FCE BIT(10) |
| 119 | #define PCIE_CORE_INT_CT BIT(11) |
| 120 | #define PCIE_CORE_INT_UTC BIT(18) |
| 121 | #define PCIE_CORE_INT_MMVC BIT(19) |
| 122 | #define PCIE_CORE_INT_MASK (PCIE_CORE_CTRL_MGMT_BASE + 0x210) |
| 123 | #define PCIE_RC_BAR_CONF (PCIE_CORE_CTRL_MGMT_BASE + 0x300) |
| 124 | |
| 125 | #define PCIE_CORE_INT \ |
| 126 | (PCIE_CORE_INT_PRFPE | PCIE_CORE_INT_CRFPE | \ |
| 127 | PCIE_CORE_INT_RRPE | PCIE_CORE_INT_CRFO | \ |
| 128 | PCIE_CORE_INT_RT | PCIE_CORE_INT_RTR | \ |
| 129 | PCIE_CORE_INT_PE | PCIE_CORE_INT_MTR | \ |
| 130 | PCIE_CORE_INT_UCR | PCIE_CORE_INT_FCE | \ |
| 131 | PCIE_CORE_INT_CT | PCIE_CORE_INT_UTC | \ |
| 132 | PCIE_CORE_INT_MMVC) |
| 133 | |
| 134 | #define PCIE_RC_CONFIG_BASE 0xa00000 |
| 135 | #define PCIE_RC_CONFIG_VENDOR (PCIE_RC_CONFIG_BASE + 0x00) |
| 136 | #define PCIE_RC_CONFIG_RID_CCR (PCIE_RC_CONFIG_BASE + 0x08) |
| 137 | #define PCIE_RC_CONFIG_SCC_SHIFT 16 |
| 138 | #define PCIE_RC_CONFIG_LCS (PCIE_RC_CONFIG_BASE + 0xd0) |
| 139 | #define PCIE_RC_CONFIG_LCS_RETRAIN_LINK BIT(5) |
| 140 | #define PCIE_RC_CONFIG_LCS_LBMIE BIT(10) |
| 141 | #define PCIE_RC_CONFIG_LCS_LABIE BIT(11) |
| 142 | #define PCIE_RC_CONFIG_LCS_LBMS BIT(30) |
| 143 | #define PCIE_RC_CONFIG_LCS_LAMS BIT(31) |
| 144 | #define PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2 (PCIE_RC_CONFIG_BASE + 0x90c) |
| 145 | |
| 146 | #define PCIE_CORE_AXI_CONF_BASE 0xc00000 |
| 147 | #define PCIE_CORE_OB_REGION_ADDR0 (PCIE_CORE_AXI_CONF_BASE + 0x0) |
| 148 | #define PCIE_CORE_OB_REGION_ADDR0_NUM_BITS 0x3f |
| 149 | #define PCIE_CORE_OB_REGION_ADDR0_LO_ADDR 0xffffff00 |
| 150 | #define PCIE_CORE_OB_REGION_ADDR1 (PCIE_CORE_AXI_CONF_BASE + 0x4) |
| 151 | #define PCIE_CORE_OB_REGION_DESC0 (PCIE_CORE_AXI_CONF_BASE + 0x8) |
| 152 | #define PCIE_CORE_OB_REGION_DESC1 (PCIE_CORE_AXI_CONF_BASE + 0xc) |
| 153 | |
| 154 | #define PCIE_CORE_AXI_INBOUND_BASE 0xc00800 |
| 155 | #define PCIE_RP_IB_ADDR0 (PCIE_CORE_AXI_INBOUND_BASE + 0x0) |
| 156 | #define PCIE_CORE_IB_REGION_ADDR0_NUM_BITS 0x3f |
| 157 | #define PCIE_CORE_IB_REGION_ADDR0_LO_ADDR 0xffffff00 |
| 158 | #define PCIE_RP_IB_ADDR1 (PCIE_CORE_AXI_INBOUND_BASE + 0x4) |
| 159 | |
| 160 | /* Size of one AXI Region (not Region 0) */ |
| 161 | #define AXI_REGION_SIZE BIT(20) |
| 162 | /* Size of Region 0, equal to sum of sizes of other regions */ |
| 163 | #define AXI_REGION_0_SIZE (32 * (0x1 << 20)) |
| 164 | #define OB_REG_SIZE_SHIFT 5 |
| 165 | #define IB_ROOT_PORT_REG_SIZE_SHIFT 3 |
| 166 | #define AXI_WRAPPER_IO_WRITE 0x6 |
| 167 | #define AXI_WRAPPER_MEM_WRITE 0x2 |
| 168 | |
| 169 | #define MAX_AXI_IB_ROOTPORT_REGION_NUM 3 |
| 170 | #define MIN_AXI_ADDR_BITS_PASSED 8 |
| 171 | #define ROCKCHIP_VENDOR_ID 0x1d87 |
| 172 | #define PCIE_ECAM_BUS(x) (((x) & 0xff) << 20) |
| 173 | #define PCIE_ECAM_DEV(x) (((x) & 0x1f) << 15) |
| 174 | #define PCIE_ECAM_FUNC(x) (((x) & 0x7) << 12) |
| 175 | #define PCIE_ECAM_REG(x) (((x) & 0xfff) << 0) |
| 176 | #define PCIE_ECAM_ADDR(bus, dev, func, reg) \ |
| 177 | (PCIE_ECAM_BUS(bus) | PCIE_ECAM_DEV(dev) | \ |
| 178 | PCIE_ECAM_FUNC(func) | PCIE_ECAM_REG(reg)) |
| 179 | |
| 180 | #define RC_REGION_0_ADDR_TRANS_H 0x00000000 |
| 181 | #define RC_REGION_0_ADDR_TRANS_L 0x00000000 |
| 182 | #define RC_REGION_0_PASS_BITS (25 - 1) |
| 183 | #define MAX_AXI_WRAPPER_REGION_NUM 33 |
| 184 | |
| 185 | struct rockchip_pcie { |
| 186 | void __iomem *reg_base; /* DT axi-base */ |
| 187 | void __iomem *apb_base; /* DT apb-base */ |
| 188 | struct phy *phy; |
| 189 | struct reset_control *core_rst; |
| 190 | struct reset_control *mgmt_rst; |
| 191 | struct reset_control *mgmt_sticky_rst; |
| 192 | struct reset_control *pipe_rst; |
Shawn Lin | 31a3a7b | 2016-11-10 11:14:37 -0600 | [diff] [blame] | 193 | struct reset_control *pm_rst; |
| 194 | struct reset_control *aclk_rst; |
| 195 | struct reset_control *pclk_rst; |
Shawn Lin | e77f847 | 2016-09-03 11:41:09 -0500 | [diff] [blame] | 196 | struct clk *aclk_pcie; |
| 197 | struct clk *aclk_perf_pcie; |
| 198 | struct clk *hclk_pcie; |
| 199 | struct clk *clk_pcie_pm; |
| 200 | struct regulator *vpcie3v3; /* 3.3V power supply */ |
| 201 | struct regulator *vpcie1v8; /* 1.8V power supply */ |
| 202 | struct regulator *vpcie0v9; /* 0.9V power supply */ |
| 203 | struct gpio_desc *ep_gpio; |
| 204 | u32 lanes; |
| 205 | u8 root_bus_nr; |
| 206 | struct device *dev; |
| 207 | struct irq_domain *irq_domain; |
| 208 | }; |
| 209 | |
| 210 | static u32 rockchip_pcie_read(struct rockchip_pcie *rockchip, u32 reg) |
| 211 | { |
| 212 | return readl(rockchip->apb_base + reg); |
| 213 | } |
| 214 | |
| 215 | static void rockchip_pcie_write(struct rockchip_pcie *rockchip, u32 val, |
| 216 | u32 reg) |
| 217 | { |
| 218 | writel(val, rockchip->apb_base + reg); |
| 219 | } |
| 220 | |
| 221 | static void rockchip_pcie_enable_bw_int(struct rockchip_pcie *rockchip) |
| 222 | { |
| 223 | u32 status; |
| 224 | |
| 225 | status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS); |
| 226 | status |= (PCIE_RC_CONFIG_LCS_LBMIE | PCIE_RC_CONFIG_LCS_LABIE); |
| 227 | rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS); |
| 228 | } |
| 229 | |
| 230 | static void rockchip_pcie_clr_bw_int(struct rockchip_pcie *rockchip) |
| 231 | { |
| 232 | u32 status; |
| 233 | |
| 234 | status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS); |
| 235 | status |= (PCIE_RC_CONFIG_LCS_LBMS | PCIE_RC_CONFIG_LCS_LAMS); |
| 236 | rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS); |
| 237 | } |
| 238 | |
Rajat Jain | 277743e | 2016-09-22 17:50:42 -0700 | [diff] [blame] | 239 | static void rockchip_pcie_update_txcredit_mui(struct rockchip_pcie *rockchip) |
| 240 | { |
| 241 | u32 val; |
| 242 | |
| 243 | /* Update Tx credit maximum update interval */ |
| 244 | val = rockchip_pcie_read(rockchip, PCIE_CORE_TXCREDIT_CFG1); |
| 245 | val &= ~PCIE_CORE_TXCREDIT_CFG1_MUI_MASK; |
| 246 | val |= PCIE_CORE_TXCREDIT_CFG1_MUI_ENCODE(24000); /* ns */ |
| 247 | rockchip_pcie_write(rockchip, val, PCIE_CORE_TXCREDIT_CFG1); |
| 248 | } |
| 249 | |
Shawn Lin | e77f847 | 2016-09-03 11:41:09 -0500 | [diff] [blame] | 250 | static int rockchip_pcie_valid_device(struct rockchip_pcie *rockchip, |
| 251 | struct pci_bus *bus, int dev) |
| 252 | { |
| 253 | /* access only one slot on each root port */ |
| 254 | if (bus->number == rockchip->root_bus_nr && dev > 0) |
| 255 | return 0; |
| 256 | |
| 257 | /* |
| 258 | * do not read more than one device on the bus directly attached |
| 259 | * to RC's downstream side. |
| 260 | */ |
| 261 | if (bus->primary == rockchip->root_bus_nr && dev > 0) |
| 262 | return 0; |
| 263 | |
| 264 | return 1; |
| 265 | } |
| 266 | |
| 267 | static int rockchip_pcie_rd_own_conf(struct rockchip_pcie *rockchip, |
| 268 | int where, int size, u32 *val) |
| 269 | { |
| 270 | void __iomem *addr = rockchip->apb_base + PCIE_RC_CONFIG_BASE + where; |
| 271 | |
| 272 | if (!IS_ALIGNED((uintptr_t)addr, size)) { |
| 273 | *val = 0; |
| 274 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 275 | } |
| 276 | |
| 277 | if (size == 4) { |
| 278 | *val = readl(addr); |
| 279 | } else if (size == 2) { |
| 280 | *val = readw(addr); |
| 281 | } else if (size == 1) { |
| 282 | *val = readb(addr); |
| 283 | } else { |
| 284 | *val = 0; |
| 285 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 286 | } |
| 287 | return PCIBIOS_SUCCESSFUL; |
| 288 | } |
| 289 | |
| 290 | static int rockchip_pcie_wr_own_conf(struct rockchip_pcie *rockchip, |
| 291 | int where, int size, u32 val) |
| 292 | { |
| 293 | u32 mask, tmp, offset; |
| 294 | |
| 295 | offset = where & ~0x3; |
| 296 | |
| 297 | if (size == 4) { |
| 298 | writel(val, rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset); |
| 299 | return PCIBIOS_SUCCESSFUL; |
| 300 | } |
| 301 | |
| 302 | mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8)); |
| 303 | |
| 304 | /* |
| 305 | * N.B. This read/modify/write isn't safe in general because it can |
| 306 | * corrupt RW1C bits in adjacent registers. But the hardware |
| 307 | * doesn't support smaller writes. |
| 308 | */ |
| 309 | tmp = readl(rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset) & mask; |
| 310 | tmp |= val << ((where & 0x3) * 8); |
| 311 | writel(tmp, rockchip->apb_base + PCIE_RC_CONFIG_BASE + offset); |
| 312 | |
| 313 | return PCIBIOS_SUCCESSFUL; |
| 314 | } |
| 315 | |
| 316 | static int rockchip_pcie_rd_other_conf(struct rockchip_pcie *rockchip, |
| 317 | struct pci_bus *bus, u32 devfn, |
| 318 | int where, int size, u32 *val) |
| 319 | { |
| 320 | u32 busdev; |
| 321 | |
| 322 | busdev = PCIE_ECAM_ADDR(bus->number, PCI_SLOT(devfn), |
| 323 | PCI_FUNC(devfn), where); |
| 324 | |
| 325 | if (!IS_ALIGNED(busdev, size)) { |
| 326 | *val = 0; |
| 327 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 328 | } |
| 329 | |
| 330 | if (size == 4) { |
| 331 | *val = readl(rockchip->reg_base + busdev); |
| 332 | } else if (size == 2) { |
| 333 | *val = readw(rockchip->reg_base + busdev); |
| 334 | } else if (size == 1) { |
| 335 | *val = readb(rockchip->reg_base + busdev); |
| 336 | } else { |
| 337 | *val = 0; |
| 338 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 339 | } |
| 340 | return PCIBIOS_SUCCESSFUL; |
| 341 | } |
| 342 | |
| 343 | static int rockchip_pcie_wr_other_conf(struct rockchip_pcie *rockchip, |
| 344 | struct pci_bus *bus, u32 devfn, |
| 345 | int where, int size, u32 val) |
| 346 | { |
| 347 | u32 busdev; |
| 348 | |
| 349 | busdev = PCIE_ECAM_ADDR(bus->number, PCI_SLOT(devfn), |
| 350 | PCI_FUNC(devfn), where); |
| 351 | if (!IS_ALIGNED(busdev, size)) |
| 352 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 353 | |
| 354 | if (size == 4) |
| 355 | writel(val, rockchip->reg_base + busdev); |
| 356 | else if (size == 2) |
| 357 | writew(val, rockchip->reg_base + busdev); |
| 358 | else if (size == 1) |
| 359 | writeb(val, rockchip->reg_base + busdev); |
| 360 | else |
| 361 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 362 | |
| 363 | return PCIBIOS_SUCCESSFUL; |
| 364 | } |
| 365 | |
| 366 | static int rockchip_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where, |
| 367 | int size, u32 *val) |
| 368 | { |
| 369 | struct rockchip_pcie *rockchip = bus->sysdata; |
| 370 | |
| 371 | if (!rockchip_pcie_valid_device(rockchip, bus, PCI_SLOT(devfn))) { |
| 372 | *val = 0xffffffff; |
| 373 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 374 | } |
| 375 | |
| 376 | if (bus->number == rockchip->root_bus_nr) |
| 377 | return rockchip_pcie_rd_own_conf(rockchip, where, size, val); |
| 378 | |
| 379 | return rockchip_pcie_rd_other_conf(rockchip, bus, devfn, where, size, val); |
| 380 | } |
| 381 | |
| 382 | static int rockchip_pcie_wr_conf(struct pci_bus *bus, u32 devfn, |
| 383 | int where, int size, u32 val) |
| 384 | { |
| 385 | struct rockchip_pcie *rockchip = bus->sysdata; |
| 386 | |
| 387 | if (!rockchip_pcie_valid_device(rockchip, bus, PCI_SLOT(devfn))) |
| 388 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 389 | |
| 390 | if (bus->number == rockchip->root_bus_nr) |
| 391 | return rockchip_pcie_wr_own_conf(rockchip, where, size, val); |
| 392 | |
| 393 | return rockchip_pcie_wr_other_conf(rockchip, bus, devfn, where, size, val); |
| 394 | } |
| 395 | |
| 396 | static struct pci_ops rockchip_pcie_ops = { |
| 397 | .read = rockchip_pcie_rd_conf, |
| 398 | .write = rockchip_pcie_wr_conf, |
| 399 | }; |
| 400 | |
| 401 | /** |
| 402 | * rockchip_pcie_init_port - Initialize hardware |
| 403 | * @rockchip: PCIe port information |
| 404 | */ |
| 405 | static int rockchip_pcie_init_port(struct rockchip_pcie *rockchip) |
| 406 | { |
| 407 | struct device *dev = rockchip->dev; |
| 408 | int err; |
| 409 | u32 status; |
| 410 | unsigned long timeout; |
| 411 | |
| 412 | gpiod_set_value(rockchip->ep_gpio, 0); |
| 413 | |
Shawn Lin | 31a3a7b | 2016-11-10 11:14:37 -0600 | [diff] [blame] | 414 | err = reset_control_assert(rockchip->aclk_rst); |
| 415 | if (err) { |
| 416 | dev_err(dev, "assert aclk_rst err %d\n", err); |
| 417 | return err; |
| 418 | } |
| 419 | |
| 420 | err = reset_control_assert(rockchip->pclk_rst); |
| 421 | if (err) { |
| 422 | dev_err(dev, "assert pclk_rst err %d\n", err); |
| 423 | return err; |
| 424 | } |
| 425 | |
| 426 | err = reset_control_assert(rockchip->pm_rst); |
| 427 | if (err) { |
| 428 | dev_err(dev, "assert pm_rst err %d\n", err); |
| 429 | return err; |
| 430 | } |
| 431 | |
| 432 | udelay(10); |
| 433 | |
| 434 | err = reset_control_deassert(rockchip->pm_rst); |
| 435 | if (err) { |
| 436 | dev_err(dev, "deassert pm_rst err %d\n", err); |
| 437 | return err; |
| 438 | } |
| 439 | |
| 440 | err = reset_control_deassert(rockchip->aclk_rst); |
| 441 | if (err) { |
| 442 | dev_err(dev, "deassert mgmt_sticky_rst err %d\n", err); |
| 443 | return err; |
| 444 | } |
| 445 | |
| 446 | err = reset_control_deassert(rockchip->pclk_rst); |
| 447 | if (err) { |
| 448 | dev_err(dev, "deassert mgmt_sticky_rst err %d\n", err); |
| 449 | return err; |
| 450 | } |
| 451 | |
Shawn Lin | e77f847 | 2016-09-03 11:41:09 -0500 | [diff] [blame] | 452 | err = phy_init(rockchip->phy); |
| 453 | if (err < 0) { |
| 454 | dev_err(dev, "fail to init phy, err %d\n", err); |
| 455 | return err; |
| 456 | } |
| 457 | |
| 458 | err = reset_control_assert(rockchip->core_rst); |
| 459 | if (err) { |
| 460 | dev_err(dev, "assert core_rst err %d\n", err); |
| 461 | return err; |
| 462 | } |
| 463 | |
| 464 | err = reset_control_assert(rockchip->mgmt_rst); |
| 465 | if (err) { |
| 466 | dev_err(dev, "assert mgmt_rst err %d\n", err); |
| 467 | return err; |
| 468 | } |
| 469 | |
| 470 | err = reset_control_assert(rockchip->mgmt_sticky_rst); |
| 471 | if (err) { |
| 472 | dev_err(dev, "assert mgmt_sticky_rst err %d\n", err); |
| 473 | return err; |
| 474 | } |
| 475 | |
| 476 | err = reset_control_assert(rockchip->pipe_rst); |
| 477 | if (err) { |
| 478 | dev_err(dev, "assert pipe_rst err %d\n", err); |
| 479 | return err; |
| 480 | } |
| 481 | |
| 482 | rockchip_pcie_write(rockchip, |
| 483 | PCIE_CLIENT_CONF_ENABLE | |
| 484 | PCIE_CLIENT_LINK_TRAIN_ENABLE | |
| 485 | PCIE_CLIENT_ARI_ENABLE | |
| 486 | PCIE_CLIENT_CONF_LANE_NUM(rockchip->lanes) | |
| 487 | PCIE_CLIENT_MODE_RC | |
| 488 | PCIE_CLIENT_GEN_SEL_2, |
| 489 | PCIE_CLIENT_CONFIG); |
| 490 | |
| 491 | err = phy_power_on(rockchip->phy); |
| 492 | if (err) { |
| 493 | dev_err(dev, "fail to power on phy, err %d\n", err); |
| 494 | return err; |
| 495 | } |
| 496 | |
Shawn Lin | 58c6990 | 2016-09-23 10:05:59 +0800 | [diff] [blame] | 497 | /* |
| 498 | * Please don't reorder the deassert sequence of the following |
| 499 | * four reset pins. |
| 500 | */ |
| 501 | err = reset_control_deassert(rockchip->mgmt_sticky_rst); |
| 502 | if (err) { |
| 503 | dev_err(dev, "deassert mgmt_sticky_rst err %d\n", err); |
| 504 | return err; |
| 505 | } |
| 506 | |
Shawn Lin | e77f847 | 2016-09-03 11:41:09 -0500 | [diff] [blame] | 507 | err = reset_control_deassert(rockchip->core_rst); |
| 508 | if (err) { |
| 509 | dev_err(dev, "deassert core_rst err %d\n", err); |
| 510 | return err; |
| 511 | } |
| 512 | |
| 513 | err = reset_control_deassert(rockchip->mgmt_rst); |
| 514 | if (err) { |
| 515 | dev_err(dev, "deassert mgmt_rst err %d\n", err); |
| 516 | return err; |
| 517 | } |
| 518 | |
Shawn Lin | e77f847 | 2016-09-03 11:41:09 -0500 | [diff] [blame] | 519 | err = reset_control_deassert(rockchip->pipe_rst); |
| 520 | if (err) { |
| 521 | dev_err(dev, "deassert pipe_rst err %d\n", err); |
| 522 | return err; |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * We need to read/write PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2 before |
| 527 | * enabling ASPM. Otherwise L1PwrOnSc and L1PwrOnVal isn't |
| 528 | * reliable and enabling ASPM doesn't work. This is a controller |
| 529 | * bug we need to work around. |
| 530 | */ |
| 531 | status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2); |
| 532 | rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_L1_SUBSTATE_CTRL2); |
| 533 | |
Shawn Lin | ca19890 | 2016-10-04 12:20:22 -0500 | [diff] [blame] | 534 | /* Fix the transmitted FTS count desired to exit from L0s. */ |
| 535 | status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL_PLC1); |
| 536 | status = (status & PCIE_CORE_CTRL_PLC1_FTS_MASK) | |
| 537 | (PCIE_CORE_CTRL_PLC1_FTS_CNT << PCIE_CORE_CTRL_PLC1_FTS_SHIFT); |
| 538 | rockchip_pcie_write(rockchip, status, PCIE_CORE_CTRL_PLC1); |
| 539 | |
Shawn Lin | e77f847 | 2016-09-03 11:41:09 -0500 | [diff] [blame] | 540 | /* Enable Gen1 training */ |
| 541 | rockchip_pcie_write(rockchip, PCIE_CLIENT_LINK_TRAIN_ENABLE, |
| 542 | PCIE_CLIENT_CONFIG); |
| 543 | |
| 544 | gpiod_set_value(rockchip->ep_gpio, 1); |
| 545 | |
| 546 | /* 500ms timeout value should be enough for Gen1/2 training */ |
| 547 | timeout = jiffies + msecs_to_jiffies(500); |
| 548 | |
| 549 | for (;;) { |
| 550 | status = rockchip_pcie_read(rockchip, |
| 551 | PCIE_CLIENT_BASIC_STATUS1); |
| 552 | if ((status & PCIE_CLIENT_LINK_STATUS_MASK) == |
| 553 | PCIE_CLIENT_LINK_STATUS_UP) { |
| 554 | dev_dbg(dev, "PCIe link training gen1 pass!\n"); |
| 555 | break; |
| 556 | } |
| 557 | |
| 558 | if (time_after(jiffies, timeout)) { |
| 559 | dev_err(dev, "PCIe link training gen1 timeout!\n"); |
| 560 | return -ETIMEDOUT; |
| 561 | } |
| 562 | |
| 563 | msleep(20); |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Enable retrain for gen2. This should be configured only after |
| 568 | * gen1 finished. |
| 569 | */ |
| 570 | status = rockchip_pcie_read(rockchip, PCIE_RC_CONFIG_LCS); |
| 571 | status |= PCIE_RC_CONFIG_LCS_RETRAIN_LINK; |
| 572 | rockchip_pcie_write(rockchip, status, PCIE_RC_CONFIG_LCS); |
| 573 | |
| 574 | timeout = jiffies + msecs_to_jiffies(500); |
| 575 | for (;;) { |
| 576 | status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL); |
| 577 | if ((status & PCIE_CORE_PL_CONF_SPEED_MASK) == |
| 578 | PCIE_CORE_PL_CONF_SPEED_5G) { |
| 579 | dev_dbg(dev, "PCIe link training gen2 pass!\n"); |
| 580 | break; |
| 581 | } |
| 582 | |
| 583 | if (time_after(jiffies, timeout)) { |
| 584 | dev_dbg(dev, "PCIe link training gen2 timeout, fall back to gen1!\n"); |
| 585 | break; |
| 586 | } |
| 587 | |
| 588 | msleep(20); |
| 589 | } |
| 590 | |
| 591 | /* Check the final link width from negotiated lane counter from MGMT */ |
| 592 | status = rockchip_pcie_read(rockchip, PCIE_CORE_CTRL); |
| 593 | status = 0x1 << ((status & PCIE_CORE_PL_CONF_LANE_MASK) >> |
| 594 | PCIE_CORE_PL_CONF_LANE_MASK); |
| 595 | dev_dbg(dev, "current link width is x%d\n", status); |
| 596 | |
| 597 | rockchip_pcie_write(rockchip, ROCKCHIP_VENDOR_ID, |
| 598 | PCIE_RC_CONFIG_VENDOR); |
| 599 | rockchip_pcie_write(rockchip, |
| 600 | PCI_CLASS_BRIDGE_PCI << PCIE_RC_CONFIG_SCC_SHIFT, |
| 601 | PCIE_RC_CONFIG_RID_CCR); |
| 602 | rockchip_pcie_write(rockchip, 0x0, PCIE_RC_BAR_CONF); |
| 603 | |
| 604 | rockchip_pcie_write(rockchip, |
| 605 | (RC_REGION_0_ADDR_TRANS_L + RC_REGION_0_PASS_BITS), |
| 606 | PCIE_CORE_OB_REGION_ADDR0); |
| 607 | rockchip_pcie_write(rockchip, RC_REGION_0_ADDR_TRANS_H, |
| 608 | PCIE_CORE_OB_REGION_ADDR1); |
| 609 | rockchip_pcie_write(rockchip, 0x0080000a, PCIE_CORE_OB_REGION_DESC0); |
| 610 | rockchip_pcie_write(rockchip, 0x0, PCIE_CORE_OB_REGION_DESC1); |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static irqreturn_t rockchip_pcie_subsys_irq_handler(int irq, void *arg) |
| 616 | { |
| 617 | struct rockchip_pcie *rockchip = arg; |
| 618 | struct device *dev = rockchip->dev; |
| 619 | u32 reg; |
| 620 | u32 sub_reg; |
| 621 | |
| 622 | reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS); |
| 623 | if (reg & PCIE_CLIENT_INT_LOCAL) { |
| 624 | dev_dbg(dev, "local interrupt received\n"); |
| 625 | sub_reg = rockchip_pcie_read(rockchip, PCIE_CORE_INT_STATUS); |
| 626 | if (sub_reg & PCIE_CORE_INT_PRFPE) |
| 627 | dev_dbg(dev, "parity error detected while reading from the PNP receive FIFO RAM\n"); |
| 628 | |
| 629 | if (sub_reg & PCIE_CORE_INT_CRFPE) |
| 630 | dev_dbg(dev, "parity error detected while reading from the Completion Receive FIFO RAM\n"); |
| 631 | |
| 632 | if (sub_reg & PCIE_CORE_INT_RRPE) |
| 633 | dev_dbg(dev, "parity error detected while reading from replay buffer RAM\n"); |
| 634 | |
| 635 | if (sub_reg & PCIE_CORE_INT_PRFO) |
| 636 | dev_dbg(dev, "overflow occurred in the PNP receive FIFO\n"); |
| 637 | |
| 638 | if (sub_reg & PCIE_CORE_INT_CRFO) |
| 639 | dev_dbg(dev, "overflow occurred in the completion receive FIFO\n"); |
| 640 | |
| 641 | if (sub_reg & PCIE_CORE_INT_RT) |
| 642 | dev_dbg(dev, "replay timer timed out\n"); |
| 643 | |
| 644 | if (sub_reg & PCIE_CORE_INT_RTR) |
| 645 | dev_dbg(dev, "replay timer rolled over after 4 transmissions of the same TLP\n"); |
| 646 | |
| 647 | if (sub_reg & PCIE_CORE_INT_PE) |
| 648 | dev_dbg(dev, "phy error detected on receive side\n"); |
| 649 | |
| 650 | if (sub_reg & PCIE_CORE_INT_MTR) |
| 651 | dev_dbg(dev, "malformed TLP received from the link\n"); |
| 652 | |
| 653 | if (sub_reg & PCIE_CORE_INT_UCR) |
| 654 | dev_dbg(dev, "malformed TLP received from the link\n"); |
| 655 | |
| 656 | if (sub_reg & PCIE_CORE_INT_FCE) |
| 657 | dev_dbg(dev, "an error was observed in the flow control advertisements from the other side\n"); |
| 658 | |
| 659 | if (sub_reg & PCIE_CORE_INT_CT) |
| 660 | dev_dbg(dev, "a request timed out waiting for completion\n"); |
| 661 | |
| 662 | if (sub_reg & PCIE_CORE_INT_UTC) |
| 663 | dev_dbg(dev, "unmapped TC error\n"); |
| 664 | |
| 665 | if (sub_reg & PCIE_CORE_INT_MMVC) |
| 666 | dev_dbg(dev, "MSI mask register changes\n"); |
| 667 | |
| 668 | rockchip_pcie_write(rockchip, sub_reg, PCIE_CORE_INT_STATUS); |
| 669 | } else if (reg & PCIE_CLIENT_INT_PHY) { |
| 670 | dev_dbg(dev, "phy link changes\n"); |
Rajat Jain | 277743e | 2016-09-22 17:50:42 -0700 | [diff] [blame] | 671 | rockchip_pcie_update_txcredit_mui(rockchip); |
Shawn Lin | e77f847 | 2016-09-03 11:41:09 -0500 | [diff] [blame] | 672 | rockchip_pcie_clr_bw_int(rockchip); |
| 673 | } |
| 674 | |
| 675 | rockchip_pcie_write(rockchip, reg & PCIE_CLIENT_INT_LOCAL, |
| 676 | PCIE_CLIENT_INT_STATUS); |
| 677 | |
| 678 | return IRQ_HANDLED; |
| 679 | } |
| 680 | |
| 681 | static irqreturn_t rockchip_pcie_client_irq_handler(int irq, void *arg) |
| 682 | { |
| 683 | struct rockchip_pcie *rockchip = arg; |
| 684 | struct device *dev = rockchip->dev; |
| 685 | u32 reg; |
| 686 | |
| 687 | reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS); |
| 688 | if (reg & PCIE_CLIENT_INT_LEGACY_DONE) |
| 689 | dev_dbg(dev, "legacy done interrupt received\n"); |
| 690 | |
| 691 | if (reg & PCIE_CLIENT_INT_MSG) |
| 692 | dev_dbg(dev, "message done interrupt received\n"); |
| 693 | |
| 694 | if (reg & PCIE_CLIENT_INT_HOT_RST) |
| 695 | dev_dbg(dev, "hot reset interrupt received\n"); |
| 696 | |
| 697 | if (reg & PCIE_CLIENT_INT_DPA) |
| 698 | dev_dbg(dev, "dpa interrupt received\n"); |
| 699 | |
| 700 | if (reg & PCIE_CLIENT_INT_FATAL_ERR) |
| 701 | dev_dbg(dev, "fatal error interrupt received\n"); |
| 702 | |
| 703 | if (reg & PCIE_CLIENT_INT_NFATAL_ERR) |
| 704 | dev_dbg(dev, "no fatal error interrupt received\n"); |
| 705 | |
| 706 | if (reg & PCIE_CLIENT_INT_CORR_ERR) |
| 707 | dev_dbg(dev, "correctable error interrupt received\n"); |
| 708 | |
| 709 | if (reg & PCIE_CLIENT_INT_PHY) |
| 710 | dev_dbg(dev, "phy interrupt received\n"); |
| 711 | |
| 712 | rockchip_pcie_write(rockchip, reg & (PCIE_CLIENT_INT_LEGACY_DONE | |
| 713 | PCIE_CLIENT_INT_MSG | PCIE_CLIENT_INT_HOT_RST | |
| 714 | PCIE_CLIENT_INT_DPA | PCIE_CLIENT_INT_FATAL_ERR | |
| 715 | PCIE_CLIENT_INT_NFATAL_ERR | |
| 716 | PCIE_CLIENT_INT_CORR_ERR | |
| 717 | PCIE_CLIENT_INT_PHY), |
| 718 | PCIE_CLIENT_INT_STATUS); |
| 719 | |
| 720 | return IRQ_HANDLED; |
| 721 | } |
| 722 | |
| 723 | static void rockchip_pcie_legacy_int_handler(struct irq_desc *desc) |
| 724 | { |
| 725 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 726 | struct rockchip_pcie *rockchip = irq_desc_get_handler_data(desc); |
| 727 | struct device *dev = rockchip->dev; |
| 728 | u32 reg; |
| 729 | u32 hwirq; |
| 730 | u32 virq; |
| 731 | |
| 732 | chained_irq_enter(chip, desc); |
| 733 | |
| 734 | reg = rockchip_pcie_read(rockchip, PCIE_CLIENT_INT_STATUS); |
| 735 | reg = (reg & PCIE_CLIENT_INTR_MASK) >> PCIE_CLIENT_INTR_SHIFT; |
| 736 | |
| 737 | while (reg) { |
| 738 | hwirq = ffs(reg) - 1; |
| 739 | reg &= ~BIT(hwirq); |
| 740 | |
| 741 | virq = irq_find_mapping(rockchip->irq_domain, hwirq); |
| 742 | if (virq) |
| 743 | generic_handle_irq(virq); |
| 744 | else |
| 745 | dev_err(dev, "unexpected IRQ, INT%d\n", hwirq); |
| 746 | } |
| 747 | |
| 748 | chained_irq_exit(chip, desc); |
| 749 | } |
| 750 | |
| 751 | |
| 752 | /** |
| 753 | * rockchip_pcie_parse_dt - Parse Device Tree |
| 754 | * @rockchip: PCIe port information |
| 755 | * |
| 756 | * Return: '0' on success and error value on failure |
| 757 | */ |
| 758 | static int rockchip_pcie_parse_dt(struct rockchip_pcie *rockchip) |
| 759 | { |
| 760 | struct device *dev = rockchip->dev; |
| 761 | struct platform_device *pdev = to_platform_device(dev); |
| 762 | struct device_node *node = dev->of_node; |
| 763 | struct resource *regs; |
| 764 | int irq; |
| 765 | int err; |
| 766 | |
| 767 | regs = platform_get_resource_byname(pdev, |
| 768 | IORESOURCE_MEM, |
| 769 | "axi-base"); |
| 770 | rockchip->reg_base = devm_ioremap_resource(dev, regs); |
| 771 | if (IS_ERR(rockchip->reg_base)) |
| 772 | return PTR_ERR(rockchip->reg_base); |
| 773 | |
| 774 | regs = platform_get_resource_byname(pdev, |
| 775 | IORESOURCE_MEM, |
| 776 | "apb-base"); |
| 777 | rockchip->apb_base = devm_ioremap_resource(dev, regs); |
| 778 | if (IS_ERR(rockchip->apb_base)) |
| 779 | return PTR_ERR(rockchip->apb_base); |
| 780 | |
| 781 | rockchip->phy = devm_phy_get(dev, "pcie-phy"); |
| 782 | if (IS_ERR(rockchip->phy)) { |
| 783 | if (PTR_ERR(rockchip->phy) != -EPROBE_DEFER) |
| 784 | dev_err(dev, "missing phy\n"); |
| 785 | return PTR_ERR(rockchip->phy); |
| 786 | } |
| 787 | |
| 788 | rockchip->lanes = 1; |
| 789 | err = of_property_read_u32(node, "num-lanes", &rockchip->lanes); |
| 790 | if (!err && (rockchip->lanes == 0 || |
| 791 | rockchip->lanes == 3 || |
| 792 | rockchip->lanes > 4)) { |
| 793 | dev_warn(dev, "invalid num-lanes, default to use one lane\n"); |
| 794 | rockchip->lanes = 1; |
| 795 | } |
| 796 | |
| 797 | rockchip->core_rst = devm_reset_control_get(dev, "core"); |
| 798 | if (IS_ERR(rockchip->core_rst)) { |
| 799 | if (PTR_ERR(rockchip->core_rst) != -EPROBE_DEFER) |
| 800 | dev_err(dev, "missing core reset property in node\n"); |
| 801 | return PTR_ERR(rockchip->core_rst); |
| 802 | } |
| 803 | |
| 804 | rockchip->mgmt_rst = devm_reset_control_get(dev, "mgmt"); |
| 805 | if (IS_ERR(rockchip->mgmt_rst)) { |
| 806 | if (PTR_ERR(rockchip->mgmt_rst) != -EPROBE_DEFER) |
| 807 | dev_err(dev, "missing mgmt reset property in node\n"); |
| 808 | return PTR_ERR(rockchip->mgmt_rst); |
| 809 | } |
| 810 | |
| 811 | rockchip->mgmt_sticky_rst = devm_reset_control_get(dev, "mgmt-sticky"); |
| 812 | if (IS_ERR(rockchip->mgmt_sticky_rst)) { |
| 813 | if (PTR_ERR(rockchip->mgmt_sticky_rst) != -EPROBE_DEFER) |
| 814 | dev_err(dev, "missing mgmt-sticky reset property in node\n"); |
| 815 | return PTR_ERR(rockchip->mgmt_sticky_rst); |
| 816 | } |
| 817 | |
| 818 | rockchip->pipe_rst = devm_reset_control_get(dev, "pipe"); |
| 819 | if (IS_ERR(rockchip->pipe_rst)) { |
| 820 | if (PTR_ERR(rockchip->pipe_rst) != -EPROBE_DEFER) |
| 821 | dev_err(dev, "missing pipe reset property in node\n"); |
| 822 | return PTR_ERR(rockchip->pipe_rst); |
| 823 | } |
| 824 | |
Shawn Lin | 31a3a7b | 2016-11-10 11:14:37 -0600 | [diff] [blame] | 825 | rockchip->pm_rst = devm_reset_control_get(dev, "pm"); |
| 826 | if (IS_ERR(rockchip->pm_rst)) { |
| 827 | if (PTR_ERR(rockchip->pm_rst) != -EPROBE_DEFER) |
| 828 | dev_err(dev, "missing pm reset property in node\n"); |
| 829 | return PTR_ERR(rockchip->pm_rst); |
| 830 | } |
| 831 | |
| 832 | rockchip->pclk_rst = devm_reset_control_get(dev, "pclk"); |
| 833 | if (IS_ERR(rockchip->pclk_rst)) { |
| 834 | if (PTR_ERR(rockchip->pclk_rst) != -EPROBE_DEFER) |
| 835 | dev_err(dev, "missing pclk reset property in node\n"); |
| 836 | return PTR_ERR(rockchip->pclk_rst); |
| 837 | } |
| 838 | |
| 839 | rockchip->aclk_rst = devm_reset_control_get(dev, "aclk"); |
| 840 | if (IS_ERR(rockchip->aclk_rst)) { |
| 841 | if (PTR_ERR(rockchip->aclk_rst) != -EPROBE_DEFER) |
| 842 | dev_err(dev, "missing aclk reset property in node\n"); |
| 843 | return PTR_ERR(rockchip->aclk_rst); |
| 844 | } |
| 845 | |
Shawn Lin | e77f847 | 2016-09-03 11:41:09 -0500 | [diff] [blame] | 846 | rockchip->ep_gpio = devm_gpiod_get(dev, "ep", GPIOD_OUT_HIGH); |
| 847 | if (IS_ERR(rockchip->ep_gpio)) { |
| 848 | dev_err(dev, "missing ep-gpios property in node\n"); |
| 849 | return PTR_ERR(rockchip->ep_gpio); |
| 850 | } |
| 851 | |
| 852 | rockchip->aclk_pcie = devm_clk_get(dev, "aclk"); |
| 853 | if (IS_ERR(rockchip->aclk_pcie)) { |
| 854 | dev_err(dev, "aclk clock not found\n"); |
| 855 | return PTR_ERR(rockchip->aclk_pcie); |
| 856 | } |
| 857 | |
| 858 | rockchip->aclk_perf_pcie = devm_clk_get(dev, "aclk-perf"); |
| 859 | if (IS_ERR(rockchip->aclk_perf_pcie)) { |
| 860 | dev_err(dev, "aclk_perf clock not found\n"); |
| 861 | return PTR_ERR(rockchip->aclk_perf_pcie); |
| 862 | } |
| 863 | |
| 864 | rockchip->hclk_pcie = devm_clk_get(dev, "hclk"); |
| 865 | if (IS_ERR(rockchip->hclk_pcie)) { |
| 866 | dev_err(dev, "hclk clock not found\n"); |
| 867 | return PTR_ERR(rockchip->hclk_pcie); |
| 868 | } |
| 869 | |
| 870 | rockchip->clk_pcie_pm = devm_clk_get(dev, "pm"); |
| 871 | if (IS_ERR(rockchip->clk_pcie_pm)) { |
| 872 | dev_err(dev, "pm clock not found\n"); |
| 873 | return PTR_ERR(rockchip->clk_pcie_pm); |
| 874 | } |
| 875 | |
| 876 | irq = platform_get_irq_byname(pdev, "sys"); |
| 877 | if (irq < 0) { |
| 878 | dev_err(dev, "missing sys IRQ resource\n"); |
| 879 | return -EINVAL; |
| 880 | } |
| 881 | |
| 882 | err = devm_request_irq(dev, irq, rockchip_pcie_subsys_irq_handler, |
| 883 | IRQF_SHARED, "pcie-sys", rockchip); |
| 884 | if (err) { |
| 885 | dev_err(dev, "failed to request PCIe subsystem IRQ\n"); |
| 886 | return err; |
| 887 | } |
| 888 | |
| 889 | irq = platform_get_irq_byname(pdev, "legacy"); |
| 890 | if (irq < 0) { |
| 891 | dev_err(dev, "missing legacy IRQ resource\n"); |
| 892 | return -EINVAL; |
| 893 | } |
| 894 | |
| 895 | irq_set_chained_handler_and_data(irq, |
| 896 | rockchip_pcie_legacy_int_handler, |
| 897 | rockchip); |
| 898 | |
| 899 | irq = platform_get_irq_byname(pdev, "client"); |
| 900 | if (irq < 0) { |
| 901 | dev_err(dev, "missing client IRQ resource\n"); |
| 902 | return -EINVAL; |
| 903 | } |
| 904 | |
| 905 | err = devm_request_irq(dev, irq, rockchip_pcie_client_irq_handler, |
| 906 | IRQF_SHARED, "pcie-client", rockchip); |
| 907 | if (err) { |
| 908 | dev_err(dev, "failed to request PCIe client IRQ\n"); |
| 909 | return err; |
| 910 | } |
| 911 | |
| 912 | rockchip->vpcie3v3 = devm_regulator_get_optional(dev, "vpcie3v3"); |
| 913 | if (IS_ERR(rockchip->vpcie3v3)) { |
| 914 | if (PTR_ERR(rockchip->vpcie3v3) == -EPROBE_DEFER) |
| 915 | return -EPROBE_DEFER; |
| 916 | dev_info(dev, "no vpcie3v3 regulator found\n"); |
| 917 | } |
| 918 | |
| 919 | rockchip->vpcie1v8 = devm_regulator_get_optional(dev, "vpcie1v8"); |
| 920 | if (IS_ERR(rockchip->vpcie1v8)) { |
| 921 | if (PTR_ERR(rockchip->vpcie1v8) == -EPROBE_DEFER) |
| 922 | return -EPROBE_DEFER; |
| 923 | dev_info(dev, "no vpcie1v8 regulator found\n"); |
| 924 | } |
| 925 | |
| 926 | rockchip->vpcie0v9 = devm_regulator_get_optional(dev, "vpcie0v9"); |
| 927 | if (IS_ERR(rockchip->vpcie0v9)) { |
| 928 | if (PTR_ERR(rockchip->vpcie0v9) == -EPROBE_DEFER) |
| 929 | return -EPROBE_DEFER; |
| 930 | dev_info(dev, "no vpcie0v9 regulator found\n"); |
| 931 | } |
| 932 | |
| 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | static int rockchip_pcie_set_vpcie(struct rockchip_pcie *rockchip) |
| 937 | { |
| 938 | struct device *dev = rockchip->dev; |
| 939 | int err; |
| 940 | |
| 941 | if (!IS_ERR(rockchip->vpcie3v3)) { |
| 942 | err = regulator_enable(rockchip->vpcie3v3); |
| 943 | if (err) { |
| 944 | dev_err(dev, "fail to enable vpcie3v3 regulator\n"); |
| 945 | goto err_out; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | if (!IS_ERR(rockchip->vpcie1v8)) { |
| 950 | err = regulator_enable(rockchip->vpcie1v8); |
| 951 | if (err) { |
| 952 | dev_err(dev, "fail to enable vpcie1v8 regulator\n"); |
| 953 | goto err_disable_3v3; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | if (!IS_ERR(rockchip->vpcie0v9)) { |
| 958 | err = regulator_enable(rockchip->vpcie0v9); |
| 959 | if (err) { |
| 960 | dev_err(dev, "fail to enable vpcie0v9 regulator\n"); |
| 961 | goto err_disable_1v8; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | return 0; |
| 966 | |
| 967 | err_disable_1v8: |
| 968 | if (!IS_ERR(rockchip->vpcie1v8)) |
| 969 | regulator_disable(rockchip->vpcie1v8); |
| 970 | err_disable_3v3: |
| 971 | if (!IS_ERR(rockchip->vpcie3v3)) |
| 972 | regulator_disable(rockchip->vpcie3v3); |
| 973 | err_out: |
| 974 | return err; |
| 975 | } |
| 976 | |
| 977 | static void rockchip_pcie_enable_interrupts(struct rockchip_pcie *rockchip) |
| 978 | { |
| 979 | rockchip_pcie_write(rockchip, (PCIE_CLIENT_INT_CLI << 16) & |
| 980 | (~PCIE_CLIENT_INT_CLI), PCIE_CLIENT_INT_MASK); |
| 981 | rockchip_pcie_write(rockchip, (u32)(~PCIE_CORE_INT), |
| 982 | PCIE_CORE_INT_MASK); |
| 983 | |
| 984 | rockchip_pcie_enable_bw_int(rockchip); |
| 985 | } |
| 986 | |
| 987 | static int rockchip_pcie_intx_map(struct irq_domain *domain, unsigned int irq, |
| 988 | irq_hw_number_t hwirq) |
| 989 | { |
| 990 | irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq); |
| 991 | irq_set_chip_data(irq, domain->host_data); |
| 992 | |
| 993 | return 0; |
| 994 | } |
| 995 | |
| 996 | static const struct irq_domain_ops intx_domain_ops = { |
| 997 | .map = rockchip_pcie_intx_map, |
| 998 | }; |
| 999 | |
| 1000 | static int rockchip_pcie_init_irq_domain(struct rockchip_pcie *rockchip) |
| 1001 | { |
| 1002 | struct device *dev = rockchip->dev; |
| 1003 | struct device_node *intc = of_get_next_child(dev->of_node, NULL); |
| 1004 | |
| 1005 | if (!intc) { |
| 1006 | dev_err(dev, "missing child interrupt-controller node\n"); |
| 1007 | return -EINVAL; |
| 1008 | } |
| 1009 | |
| 1010 | rockchip->irq_domain = irq_domain_add_linear(intc, 4, |
| 1011 | &intx_domain_ops, rockchip); |
| 1012 | if (!rockchip->irq_domain) { |
| 1013 | dev_err(dev, "failed to get a INTx IRQ domain\n"); |
| 1014 | return -EINVAL; |
| 1015 | } |
| 1016 | |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | static int rockchip_pcie_prog_ob_atu(struct rockchip_pcie *rockchip, |
| 1021 | int region_no, int type, u8 num_pass_bits, |
| 1022 | u32 lower_addr, u32 upper_addr) |
| 1023 | { |
| 1024 | u32 ob_addr_0; |
| 1025 | u32 ob_addr_1; |
| 1026 | u32 ob_desc_0; |
| 1027 | u32 aw_offset; |
| 1028 | |
| 1029 | if (region_no >= MAX_AXI_WRAPPER_REGION_NUM) |
| 1030 | return -EINVAL; |
| 1031 | if (num_pass_bits + 1 < 8) |
| 1032 | return -EINVAL; |
| 1033 | if (num_pass_bits > 63) |
| 1034 | return -EINVAL; |
| 1035 | if (region_no == 0) { |
| 1036 | if (AXI_REGION_0_SIZE < (2ULL << num_pass_bits)) |
Dan Carpenter | 08015ee | 2016-10-12 07:14:09 -0500 | [diff] [blame] | 1037 | return -EINVAL; |
Shawn Lin | e77f847 | 2016-09-03 11:41:09 -0500 | [diff] [blame] | 1038 | } |
| 1039 | if (region_no != 0) { |
| 1040 | if (AXI_REGION_SIZE < (2ULL << num_pass_bits)) |
| 1041 | return -EINVAL; |
| 1042 | } |
| 1043 | |
| 1044 | aw_offset = (region_no << OB_REG_SIZE_SHIFT); |
| 1045 | |
| 1046 | ob_addr_0 = num_pass_bits & PCIE_CORE_OB_REGION_ADDR0_NUM_BITS; |
| 1047 | ob_addr_0 |= lower_addr & PCIE_CORE_OB_REGION_ADDR0_LO_ADDR; |
| 1048 | ob_addr_1 = upper_addr; |
| 1049 | ob_desc_0 = (1 << 23 | type); |
| 1050 | |
| 1051 | rockchip_pcie_write(rockchip, ob_addr_0, |
| 1052 | PCIE_CORE_OB_REGION_ADDR0 + aw_offset); |
| 1053 | rockchip_pcie_write(rockchip, ob_addr_1, |
| 1054 | PCIE_CORE_OB_REGION_ADDR1 + aw_offset); |
| 1055 | rockchip_pcie_write(rockchip, ob_desc_0, |
| 1056 | PCIE_CORE_OB_REGION_DESC0 + aw_offset); |
| 1057 | rockchip_pcie_write(rockchip, 0, |
| 1058 | PCIE_CORE_OB_REGION_DESC1 + aw_offset); |
| 1059 | |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
| 1063 | static int rockchip_pcie_prog_ib_atu(struct rockchip_pcie *rockchip, |
| 1064 | int region_no, u8 num_pass_bits, |
| 1065 | u32 lower_addr, u32 upper_addr) |
| 1066 | { |
| 1067 | u32 ib_addr_0; |
| 1068 | u32 ib_addr_1; |
| 1069 | u32 aw_offset; |
| 1070 | |
| 1071 | if (region_no > MAX_AXI_IB_ROOTPORT_REGION_NUM) |
| 1072 | return -EINVAL; |
| 1073 | if (num_pass_bits + 1 < MIN_AXI_ADDR_BITS_PASSED) |
| 1074 | return -EINVAL; |
| 1075 | if (num_pass_bits > 63) |
| 1076 | return -EINVAL; |
| 1077 | |
| 1078 | aw_offset = (region_no << IB_ROOT_PORT_REG_SIZE_SHIFT); |
| 1079 | |
| 1080 | ib_addr_0 = num_pass_bits & PCIE_CORE_IB_REGION_ADDR0_NUM_BITS; |
| 1081 | ib_addr_0 |= (lower_addr << 8) & PCIE_CORE_IB_REGION_ADDR0_LO_ADDR; |
| 1082 | ib_addr_1 = upper_addr; |
| 1083 | |
| 1084 | rockchip_pcie_write(rockchip, ib_addr_0, PCIE_RP_IB_ADDR0 + aw_offset); |
| 1085 | rockchip_pcie_write(rockchip, ib_addr_1, PCIE_RP_IB_ADDR1 + aw_offset); |
| 1086 | |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
| 1090 | static int rockchip_pcie_probe(struct platform_device *pdev) |
| 1091 | { |
| 1092 | struct rockchip_pcie *rockchip; |
| 1093 | struct device *dev = &pdev->dev; |
| 1094 | struct pci_bus *bus, *child; |
| 1095 | struct resource_entry *win; |
| 1096 | resource_size_t io_base; |
| 1097 | struct resource *mem; |
| 1098 | struct resource *io; |
| 1099 | phys_addr_t io_bus_addr = 0; |
| 1100 | u32 io_size; |
| 1101 | phys_addr_t mem_bus_addr = 0; |
| 1102 | u32 mem_size = 0; |
| 1103 | int reg_no; |
| 1104 | int err; |
| 1105 | int offset; |
| 1106 | |
| 1107 | LIST_HEAD(res); |
| 1108 | |
| 1109 | if (!dev->of_node) |
| 1110 | return -ENODEV; |
| 1111 | |
| 1112 | rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL); |
| 1113 | if (!rockchip) |
| 1114 | return -ENOMEM; |
| 1115 | |
| 1116 | rockchip->dev = dev; |
| 1117 | |
| 1118 | err = rockchip_pcie_parse_dt(rockchip); |
| 1119 | if (err) |
| 1120 | return err; |
| 1121 | |
| 1122 | err = clk_prepare_enable(rockchip->aclk_pcie); |
| 1123 | if (err) { |
| 1124 | dev_err(dev, "unable to enable aclk_pcie clock\n"); |
| 1125 | goto err_aclk_pcie; |
| 1126 | } |
| 1127 | |
| 1128 | err = clk_prepare_enable(rockchip->aclk_perf_pcie); |
| 1129 | if (err) { |
| 1130 | dev_err(dev, "unable to enable aclk_perf_pcie clock\n"); |
| 1131 | goto err_aclk_perf_pcie; |
| 1132 | } |
| 1133 | |
| 1134 | err = clk_prepare_enable(rockchip->hclk_pcie); |
| 1135 | if (err) { |
| 1136 | dev_err(dev, "unable to enable hclk_pcie clock\n"); |
| 1137 | goto err_hclk_pcie; |
| 1138 | } |
| 1139 | |
| 1140 | err = clk_prepare_enable(rockchip->clk_pcie_pm); |
| 1141 | if (err) { |
| 1142 | dev_err(dev, "unable to enable hclk_pcie clock\n"); |
| 1143 | goto err_pcie_pm; |
| 1144 | } |
| 1145 | |
| 1146 | err = rockchip_pcie_set_vpcie(rockchip); |
| 1147 | if (err) { |
| 1148 | dev_err(dev, "failed to set vpcie regulator\n"); |
| 1149 | goto err_set_vpcie; |
| 1150 | } |
| 1151 | |
| 1152 | err = rockchip_pcie_init_port(rockchip); |
| 1153 | if (err) |
| 1154 | goto err_vpcie; |
| 1155 | |
Shawn Lin | e77f847 | 2016-09-03 11:41:09 -0500 | [diff] [blame] | 1156 | rockchip_pcie_enable_interrupts(rockchip); |
| 1157 | |
| 1158 | err = rockchip_pcie_init_irq_domain(rockchip); |
| 1159 | if (err < 0) |
| 1160 | goto err_vpcie; |
| 1161 | |
| 1162 | err = of_pci_get_host_bridge_resources(dev->of_node, 0, 0xff, |
| 1163 | &res, &io_base); |
| 1164 | if (err) |
| 1165 | goto err_vpcie; |
| 1166 | |
| 1167 | err = devm_request_pci_bus_resources(dev, &res); |
| 1168 | if (err) |
| 1169 | goto err_vpcie; |
| 1170 | |
| 1171 | /* Get the I/O and memory ranges from DT */ |
| 1172 | io_size = 0; |
| 1173 | resource_list_for_each_entry(win, &res) { |
| 1174 | switch (resource_type(win->res)) { |
| 1175 | case IORESOURCE_IO: |
| 1176 | io = win->res; |
| 1177 | io->name = "I/O"; |
| 1178 | io_size = resource_size(io); |
| 1179 | io_bus_addr = io->start - win->offset; |
| 1180 | err = pci_remap_iospace(io, io_base); |
| 1181 | if (err) { |
| 1182 | dev_warn(dev, "error %d: failed to map resource %pR\n", |
| 1183 | err, io); |
| 1184 | continue; |
| 1185 | } |
| 1186 | break; |
| 1187 | case IORESOURCE_MEM: |
| 1188 | mem = win->res; |
| 1189 | mem->name = "MEM"; |
| 1190 | mem_size = resource_size(mem); |
| 1191 | mem_bus_addr = mem->start - win->offset; |
| 1192 | break; |
| 1193 | case IORESOURCE_BUS: |
| 1194 | rockchip->root_bus_nr = win->res->start; |
| 1195 | break; |
| 1196 | default: |
| 1197 | continue; |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | if (mem_size) { |
| 1202 | for (reg_no = 0; reg_no < (mem_size >> 20); reg_no++) { |
| 1203 | err = rockchip_pcie_prog_ob_atu(rockchip, reg_no + 1, |
| 1204 | AXI_WRAPPER_MEM_WRITE, |
| 1205 | 20 - 1, |
| 1206 | mem_bus_addr + |
| 1207 | (reg_no << 20), |
| 1208 | 0); |
| 1209 | if (err) { |
| 1210 | dev_err(dev, "program RC mem outbound ATU failed\n"); |
| 1211 | goto err_vpcie; |
| 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | err = rockchip_pcie_prog_ib_atu(rockchip, 2, 32 - 1, 0x0, 0); |
| 1217 | if (err) { |
| 1218 | dev_err(dev, "program RC mem inbound ATU failed\n"); |
| 1219 | goto err_vpcie; |
| 1220 | } |
| 1221 | |
| 1222 | offset = mem_size >> 20; |
| 1223 | |
| 1224 | if (io_size) { |
| 1225 | for (reg_no = 0; reg_no < (io_size >> 20); reg_no++) { |
| 1226 | err = rockchip_pcie_prog_ob_atu(rockchip, |
| 1227 | reg_no + 1 + offset, |
| 1228 | AXI_WRAPPER_IO_WRITE, |
| 1229 | 20 - 1, |
| 1230 | io_bus_addr + |
| 1231 | (reg_no << 20), |
| 1232 | 0); |
| 1233 | if (err) { |
| 1234 | dev_err(dev, "program RC io outbound ATU failed\n"); |
| 1235 | goto err_vpcie; |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | bus = pci_scan_root_bus(&pdev->dev, 0, &rockchip_pcie_ops, rockchip, &res); |
| 1241 | if (!bus) { |
| 1242 | err = -ENOMEM; |
| 1243 | goto err_vpcie; |
| 1244 | } |
| 1245 | |
| 1246 | pci_bus_size_bridges(bus); |
| 1247 | pci_bus_assign_resources(bus); |
| 1248 | list_for_each_entry(child, &bus->children, node) |
| 1249 | pcie_bus_configure_settings(child); |
| 1250 | |
| 1251 | pci_bus_add_devices(bus); |
| 1252 | |
| 1253 | dev_warn(dev, "only 32-bit config accesses supported; smaller writes may corrupt adjacent RW1C fields\n"); |
| 1254 | |
| 1255 | return err; |
| 1256 | |
| 1257 | err_vpcie: |
| 1258 | if (!IS_ERR(rockchip->vpcie3v3)) |
| 1259 | regulator_disable(rockchip->vpcie3v3); |
| 1260 | if (!IS_ERR(rockchip->vpcie1v8)) |
| 1261 | regulator_disable(rockchip->vpcie1v8); |
| 1262 | if (!IS_ERR(rockchip->vpcie0v9)) |
| 1263 | regulator_disable(rockchip->vpcie0v9); |
| 1264 | err_set_vpcie: |
| 1265 | clk_disable_unprepare(rockchip->clk_pcie_pm); |
| 1266 | err_pcie_pm: |
| 1267 | clk_disable_unprepare(rockchip->hclk_pcie); |
| 1268 | err_hclk_pcie: |
| 1269 | clk_disable_unprepare(rockchip->aclk_perf_pcie); |
| 1270 | err_aclk_perf_pcie: |
| 1271 | clk_disable_unprepare(rockchip->aclk_pcie); |
| 1272 | err_aclk_pcie: |
| 1273 | return err; |
| 1274 | } |
| 1275 | |
| 1276 | static const struct of_device_id rockchip_pcie_of_match[] = { |
| 1277 | { .compatible = "rockchip,rk3399-pcie", }, |
| 1278 | {} |
| 1279 | }; |
| 1280 | |
| 1281 | static struct platform_driver rockchip_pcie_driver = { |
| 1282 | .driver = { |
| 1283 | .name = "rockchip-pcie", |
| 1284 | .of_match_table = rockchip_pcie_of_match, |
| 1285 | }, |
| 1286 | .probe = rockchip_pcie_probe, |
| 1287 | |
| 1288 | }; |
| 1289 | builtin_platform_driver(rockchip_pcie_driver); |