Bjorn Helgaas | 8cfab3c | 2018-01-26 12:50:27 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014 Hauke Mehrtens <hauke@hauke-m.de> |
Florian Fainelli | be908d2 | 2015-10-16 12:04:04 -0700 | [diff] [blame] | 4 | * Copyright (C) 2015 Broadcom Corporation |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/pci.h> |
| 9 | #include <linux/msi.h> |
| 10 | #include <linux/clk.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/mbus.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/interrupt.h> |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 16 | #include <linux/irqchip/arm-gic-v3.h> |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/of_address.h> |
| 19 | #include <linux/of_pci.h> |
| 20 | #include <linux/of_irq.h> |
| 21 | #include <linux/of_platform.h> |
| 22 | #include <linux/phy/phy.h> |
| 23 | |
| 24 | #include "pcie-iproc.h" |
| 25 | |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 26 | #define EP_PERST_SOURCE_SELECT_SHIFT 2 |
| 27 | #define EP_PERST_SOURCE_SELECT BIT(EP_PERST_SOURCE_SELECT_SHIFT) |
| 28 | #define EP_MODE_SURVIVE_PERST_SHIFT 1 |
| 29 | #define EP_MODE_SURVIVE_PERST BIT(EP_MODE_SURVIVE_PERST_SHIFT) |
| 30 | #define RC_PCIE_RST_OUTPUT_SHIFT 0 |
| 31 | #define RC_PCIE_RST_OUTPUT BIT(RC_PCIE_RST_OUTPUT_SHIFT) |
| 32 | #define PAXC_RESET_MASK 0x7f |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 33 | |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 34 | #define GIC_V3_CFG_SHIFT 0 |
| 35 | #define GIC_V3_CFG BIT(GIC_V3_CFG_SHIFT) |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 36 | |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 37 | #define MSI_ENABLE_CFG_SHIFT 0 |
| 38 | #define MSI_ENABLE_CFG BIT(MSI_ENABLE_CFG_SHIFT) |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 39 | |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 40 | #define CFG_IND_ADDR_MASK 0x00001ffc |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 41 | |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 42 | #define CFG_ADDR_BUS_NUM_SHIFT 20 |
| 43 | #define CFG_ADDR_BUS_NUM_MASK 0x0ff00000 |
| 44 | #define CFG_ADDR_DEV_NUM_SHIFT 15 |
| 45 | #define CFG_ADDR_DEV_NUM_MASK 0x000f8000 |
| 46 | #define CFG_ADDR_FUNC_NUM_SHIFT 12 |
| 47 | #define CFG_ADDR_FUNC_NUM_MASK 0x00007000 |
| 48 | #define CFG_ADDR_REG_NUM_SHIFT 2 |
| 49 | #define CFG_ADDR_REG_NUM_MASK 0x00000ffc |
| 50 | #define CFG_ADDR_CFG_TYPE_SHIFT 0 |
| 51 | #define CFG_ADDR_CFG_TYPE_MASK 0x00000003 |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 52 | |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 53 | #define SYS_RC_INTX_MASK 0xf |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 54 | |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 55 | #define PCIE_PHYLINKUP_SHIFT 3 |
| 56 | #define PCIE_PHYLINKUP BIT(PCIE_PHYLINKUP_SHIFT) |
| 57 | #define PCIE_DL_ACTIVE_SHIFT 2 |
| 58 | #define PCIE_DL_ACTIVE BIT(PCIE_DL_ACTIVE_SHIFT) |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 59 | |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 60 | #define APB_ERR_EN_SHIFT 0 |
| 61 | #define APB_ERR_EN BIT(APB_ERR_EN_SHIFT) |
Ray Jui | 538928f | 2016-10-31 17:38:33 -0700 | [diff] [blame] | 62 | |
Srinath Mannam | 73b9e4d | 2019-03-01 10:22:15 +0530 | [diff] [blame^] | 63 | #define CFG_RD_SUCCESS 0 |
| 64 | #define CFG_RD_UR 1 |
| 65 | #define CFG_RD_CRS 2 |
| 66 | #define CFG_RD_CA 3 |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 67 | #define CFG_RETRY_STATUS 0xffff0001 |
| 68 | #define CFG_RETRY_STATUS_TIMEOUT_US 500000 /* 500 milliseconds */ |
Oza Pawandeep | 39b7a4f | 2017-08-28 16:43:30 -0500 | [diff] [blame] | 69 | |
Ray Jui | 4213e15 | 2016-10-31 17:38:37 -0700 | [diff] [blame] | 70 | /* derive the enum index of the outbound/inbound mapping registers */ |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 71 | #define MAP_REG(base_reg, index) ((base_reg) + (index) * 2) |
Ray Jui | 4213e15 | 2016-10-31 17:38:37 -0700 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * Maximum number of outbound mapping window sizes that can be supported by any |
| 75 | * OARR/OMAP mapping pair |
| 76 | */ |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 77 | #define MAX_NUM_OB_WINDOW_SIZES 4 |
Ray Jui | 4213e15 | 2016-10-31 17:38:37 -0700 | [diff] [blame] | 78 | |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 79 | #define OARR_VALID_SHIFT 0 |
| 80 | #define OARR_VALID BIT(OARR_VALID_SHIFT) |
| 81 | #define OARR_SIZE_CFG_SHIFT 1 |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 82 | |
Ray Jui | dd9d4e7 | 2016-10-31 17:38:39 -0700 | [diff] [blame] | 83 | /* |
| 84 | * Maximum number of inbound mapping region sizes that can be supported by an |
| 85 | * IARR |
| 86 | */ |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 87 | #define MAX_NUM_IB_REGION_SIZES 9 |
Ray Jui | dd9d4e7 | 2016-10-31 17:38:39 -0700 | [diff] [blame] | 88 | |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 89 | #define IMAP_VALID_SHIFT 0 |
| 90 | #define IMAP_VALID BIT(IMAP_VALID_SHIFT) |
Ray Jui | dd9d4e7 | 2016-10-31 17:38:39 -0700 | [diff] [blame] | 91 | |
Ray Jui | 3bc7082 | 2018-06-11 17:21:04 -0700 | [diff] [blame] | 92 | #define IPROC_PCI_PM_CAP 0x48 |
| 93 | #define IPROC_PCI_PM_CAP_MASK 0xffff |
Bjorn Helgaas | d8fa934 | 2017-09-05 12:27:11 -0500 | [diff] [blame] | 94 | #define IPROC_PCI_EXP_CAP 0xac |
Bjorn Helgaas | e3a1698 | 2016-10-06 13:36:07 -0500 | [diff] [blame] | 95 | |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 96 | #define IPROC_PCIE_REG_INVALID 0xffff |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 97 | |
Ray Jui | 4213e15 | 2016-10-31 17:38:37 -0700 | [diff] [blame] | 98 | /** |
| 99 | * iProc PCIe outbound mapping controller specific parameters |
| 100 | * |
| 101 | * @window_sizes: list of supported outbound mapping window sizes in MB |
| 102 | * @nr_sizes: number of supported outbound mapping window sizes |
| 103 | */ |
| 104 | struct iproc_pcie_ob_map { |
| 105 | resource_size_t window_sizes[MAX_NUM_OB_WINDOW_SIZES]; |
| 106 | unsigned int nr_sizes; |
| 107 | }; |
| 108 | |
| 109 | static const struct iproc_pcie_ob_map paxb_ob_map[] = { |
| 110 | { |
| 111 | /* OARR0/OMAP0 */ |
| 112 | .window_sizes = { 128, 256 }, |
| 113 | .nr_sizes = 2, |
| 114 | }, |
| 115 | { |
| 116 | /* OARR1/OMAP1 */ |
| 117 | .window_sizes = { 128, 256 }, |
| 118 | .nr_sizes = 2, |
| 119 | }, |
| 120 | }; |
| 121 | |
Ray Jui | c7c4452 | 2016-10-31 17:38:41 -0700 | [diff] [blame] | 122 | static const struct iproc_pcie_ob_map paxb_v2_ob_map[] = { |
| 123 | { |
| 124 | /* OARR0/OMAP0 */ |
| 125 | .window_sizes = { 128, 256 }, |
| 126 | .nr_sizes = 2, |
| 127 | }, |
| 128 | { |
| 129 | /* OARR1/OMAP1 */ |
| 130 | .window_sizes = { 128, 256 }, |
| 131 | .nr_sizes = 2, |
| 132 | }, |
| 133 | { |
| 134 | /* OARR2/OMAP2 */ |
| 135 | .window_sizes = { 128, 256, 512, 1024 }, |
| 136 | .nr_sizes = 4, |
| 137 | }, |
| 138 | { |
| 139 | /* OARR3/OMAP3 */ |
| 140 | .window_sizes = { 128, 256, 512, 1024 }, |
| 141 | .nr_sizes = 4, |
| 142 | }, |
| 143 | }; |
| 144 | |
Ray Jui | dd9d4e7 | 2016-10-31 17:38:39 -0700 | [diff] [blame] | 145 | /** |
| 146 | * iProc PCIe inbound mapping type |
| 147 | */ |
| 148 | enum iproc_pcie_ib_map_type { |
| 149 | /* for DDR memory */ |
| 150 | IPROC_PCIE_IB_MAP_MEM = 0, |
| 151 | |
| 152 | /* for device I/O memory */ |
| 153 | IPROC_PCIE_IB_MAP_IO, |
| 154 | |
| 155 | /* invalid or unused */ |
| 156 | IPROC_PCIE_IB_MAP_INVALID |
| 157 | }; |
| 158 | |
| 159 | /** |
| 160 | * iProc PCIe inbound mapping controller specific parameters |
| 161 | * |
| 162 | * @type: inbound mapping region type |
| 163 | * @size_unit: inbound mapping region size unit, could be SZ_1K, SZ_1M, or |
| 164 | * SZ_1G |
| 165 | * @region_sizes: list of supported inbound mapping region sizes in KB, MB, or |
| 166 | * GB, depedning on the size unit |
| 167 | * @nr_sizes: number of supported inbound mapping region sizes |
| 168 | * @nr_windows: number of supported inbound mapping windows for the region |
| 169 | * @imap_addr_offset: register offset between the upper and lower 32-bit |
| 170 | * IMAP address registers |
| 171 | * @imap_window_offset: register offset between each IMAP window |
| 172 | */ |
| 173 | struct iproc_pcie_ib_map { |
| 174 | enum iproc_pcie_ib_map_type type; |
| 175 | unsigned int size_unit; |
| 176 | resource_size_t region_sizes[MAX_NUM_IB_REGION_SIZES]; |
| 177 | unsigned int nr_sizes; |
| 178 | unsigned int nr_windows; |
| 179 | u16 imap_addr_offset; |
| 180 | u16 imap_window_offset; |
| 181 | }; |
| 182 | |
Ray Jui | c7c4452 | 2016-10-31 17:38:41 -0700 | [diff] [blame] | 183 | static const struct iproc_pcie_ib_map paxb_v2_ib_map[] = { |
| 184 | { |
| 185 | /* IARR0/IMAP0 */ |
| 186 | .type = IPROC_PCIE_IB_MAP_IO, |
| 187 | .size_unit = SZ_1K, |
| 188 | .region_sizes = { 32 }, |
| 189 | .nr_sizes = 1, |
| 190 | .nr_windows = 8, |
| 191 | .imap_addr_offset = 0x40, |
| 192 | .imap_window_offset = 0x4, |
| 193 | }, |
| 194 | { |
| 195 | /* IARR1/IMAP1 (currently unused) */ |
| 196 | .type = IPROC_PCIE_IB_MAP_INVALID, |
| 197 | }, |
| 198 | { |
| 199 | /* IARR2/IMAP2 */ |
| 200 | .type = IPROC_PCIE_IB_MAP_MEM, |
| 201 | .size_unit = SZ_1M, |
| 202 | .region_sizes = { 64, 128, 256, 512, 1024, 2048, 4096, 8192, |
| 203 | 16384 }, |
| 204 | .nr_sizes = 9, |
| 205 | .nr_windows = 1, |
| 206 | .imap_addr_offset = 0x4, |
| 207 | .imap_window_offset = 0x8, |
| 208 | }, |
| 209 | { |
| 210 | /* IARR3/IMAP3 */ |
| 211 | .type = IPROC_PCIE_IB_MAP_MEM, |
| 212 | .size_unit = SZ_1G, |
| 213 | .region_sizes = { 1, 2, 4, 8, 16, 32 }, |
| 214 | .nr_sizes = 6, |
| 215 | .nr_windows = 8, |
| 216 | .imap_addr_offset = 0x4, |
| 217 | .imap_window_offset = 0x8, |
| 218 | }, |
| 219 | { |
| 220 | /* IARR4/IMAP4 */ |
| 221 | .type = IPROC_PCIE_IB_MAP_MEM, |
| 222 | .size_unit = SZ_1G, |
| 223 | .region_sizes = { 32, 64, 128, 256, 512 }, |
| 224 | .nr_sizes = 5, |
| 225 | .nr_windows = 8, |
| 226 | .imap_addr_offset = 0x4, |
| 227 | .imap_window_offset = 0x8, |
| 228 | }, |
| 229 | }; |
| 230 | |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 231 | /* |
| 232 | * iProc PCIe host registers |
| 233 | */ |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 234 | enum iproc_pcie_reg { |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 235 | /* clock/reset signal control */ |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 236 | IPROC_PCIE_CLK_CTRL = 0, |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 237 | |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 238 | /* |
| 239 | * To allow MSI to be steered to an external MSI controller (e.g., ARM |
| 240 | * GICv3 ITS) |
| 241 | */ |
| 242 | IPROC_PCIE_MSI_GIC_MODE, |
| 243 | |
| 244 | /* |
| 245 | * IPROC_PCIE_MSI_BASE_ADDR and IPROC_PCIE_MSI_WINDOW_SIZE define the |
| 246 | * window where the MSI posted writes are written, for the writes to be |
| 247 | * interpreted as MSI writes. |
| 248 | */ |
| 249 | IPROC_PCIE_MSI_BASE_ADDR, |
| 250 | IPROC_PCIE_MSI_WINDOW_SIZE, |
| 251 | |
| 252 | /* |
| 253 | * To hold the address of the register where the MSI writes are |
| 254 | * programed. When ARM GICv3 ITS is used, this should be programmed |
| 255 | * with the address of the GITS_TRANSLATER register. |
| 256 | */ |
| 257 | IPROC_PCIE_MSI_ADDR_LO, |
| 258 | IPROC_PCIE_MSI_ADDR_HI, |
| 259 | |
| 260 | /* enable MSI */ |
| 261 | IPROC_PCIE_MSI_EN_CFG, |
| 262 | |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 263 | /* allow access to root complex configuration space */ |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 264 | IPROC_PCIE_CFG_IND_ADDR, |
| 265 | IPROC_PCIE_CFG_IND_DATA, |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 266 | |
| 267 | /* allow access to device configuration space */ |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 268 | IPROC_PCIE_CFG_ADDR, |
| 269 | IPROC_PCIE_CFG_DATA, |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 270 | |
| 271 | /* enable INTx */ |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 272 | IPROC_PCIE_INTX_EN, |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 273 | |
| 274 | /* outbound address mapping */ |
Ray Jui | 4213e15 | 2016-10-31 17:38:37 -0700 | [diff] [blame] | 275 | IPROC_PCIE_OARR0, |
| 276 | IPROC_PCIE_OMAP0, |
| 277 | IPROC_PCIE_OARR1, |
| 278 | IPROC_PCIE_OMAP1, |
| 279 | IPROC_PCIE_OARR2, |
| 280 | IPROC_PCIE_OMAP2, |
| 281 | IPROC_PCIE_OARR3, |
| 282 | IPROC_PCIE_OMAP3, |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 283 | |
Ray Jui | dd9d4e7 | 2016-10-31 17:38:39 -0700 | [diff] [blame] | 284 | /* inbound address mapping */ |
| 285 | IPROC_PCIE_IARR0, |
| 286 | IPROC_PCIE_IMAP0, |
| 287 | IPROC_PCIE_IARR1, |
| 288 | IPROC_PCIE_IMAP1, |
| 289 | IPROC_PCIE_IARR2, |
| 290 | IPROC_PCIE_IMAP2, |
| 291 | IPROC_PCIE_IARR3, |
| 292 | IPROC_PCIE_IMAP3, |
| 293 | IPROC_PCIE_IARR4, |
| 294 | IPROC_PCIE_IMAP4, |
| 295 | |
Srinath Mannam | 73b9e4d | 2019-03-01 10:22:15 +0530 | [diff] [blame^] | 296 | /* config read status */ |
| 297 | IPROC_PCIE_CFG_RD_STATUS, |
| 298 | |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 299 | /* link status */ |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 300 | IPROC_PCIE_LINK_STATUS, |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 301 | |
Ray Jui | 538928f | 2016-10-31 17:38:33 -0700 | [diff] [blame] | 302 | /* enable APB error for unsupported requests */ |
| 303 | IPROC_PCIE_APB_ERR_EN, |
| 304 | |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 305 | /* total number of core registers */ |
| 306 | IPROC_PCIE_MAX_NUM_REG, |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 307 | }; |
| 308 | |
Ray Jui | 404349c | 2016-10-31 17:38:32 -0700 | [diff] [blame] | 309 | /* iProc PCIe PAXB BCMA registers */ |
| 310 | static const u16 iproc_pcie_reg_paxb_bcma[] = { |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 311 | [IPROC_PCIE_CLK_CTRL] = 0x000, |
| 312 | [IPROC_PCIE_CFG_IND_ADDR] = 0x120, |
| 313 | [IPROC_PCIE_CFG_IND_DATA] = 0x124, |
| 314 | [IPROC_PCIE_CFG_ADDR] = 0x1f8, |
| 315 | [IPROC_PCIE_CFG_DATA] = 0x1fc, |
| 316 | [IPROC_PCIE_INTX_EN] = 0x330, |
| 317 | [IPROC_PCIE_LINK_STATUS] = 0xf0c, |
Ray Jui | 404349c | 2016-10-31 17:38:32 -0700 | [diff] [blame] | 318 | }; |
| 319 | |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 320 | /* iProc PCIe PAXB registers */ |
| 321 | static const u16 iproc_pcie_reg_paxb[] = { |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 322 | [IPROC_PCIE_CLK_CTRL] = 0x000, |
| 323 | [IPROC_PCIE_CFG_IND_ADDR] = 0x120, |
| 324 | [IPROC_PCIE_CFG_IND_DATA] = 0x124, |
| 325 | [IPROC_PCIE_CFG_ADDR] = 0x1f8, |
| 326 | [IPROC_PCIE_CFG_DATA] = 0x1fc, |
| 327 | [IPROC_PCIE_INTX_EN] = 0x330, |
| 328 | [IPROC_PCIE_OARR0] = 0xd20, |
| 329 | [IPROC_PCIE_OMAP0] = 0xd40, |
| 330 | [IPROC_PCIE_OARR1] = 0xd28, |
| 331 | [IPROC_PCIE_OMAP1] = 0xd48, |
| 332 | [IPROC_PCIE_LINK_STATUS] = 0xf0c, |
| 333 | [IPROC_PCIE_APB_ERR_EN] = 0xf40, |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 334 | }; |
| 335 | |
Ray Jui | c7c4452 | 2016-10-31 17:38:41 -0700 | [diff] [blame] | 336 | /* iProc PCIe PAXB v2 registers */ |
| 337 | static const u16 iproc_pcie_reg_paxb_v2[] = { |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 338 | [IPROC_PCIE_CLK_CTRL] = 0x000, |
| 339 | [IPROC_PCIE_CFG_IND_ADDR] = 0x120, |
| 340 | [IPROC_PCIE_CFG_IND_DATA] = 0x124, |
| 341 | [IPROC_PCIE_CFG_ADDR] = 0x1f8, |
| 342 | [IPROC_PCIE_CFG_DATA] = 0x1fc, |
| 343 | [IPROC_PCIE_INTX_EN] = 0x330, |
| 344 | [IPROC_PCIE_OARR0] = 0xd20, |
| 345 | [IPROC_PCIE_OMAP0] = 0xd40, |
| 346 | [IPROC_PCIE_OARR1] = 0xd28, |
| 347 | [IPROC_PCIE_OMAP1] = 0xd48, |
| 348 | [IPROC_PCIE_OARR2] = 0xd60, |
| 349 | [IPROC_PCIE_OMAP2] = 0xd68, |
| 350 | [IPROC_PCIE_OARR3] = 0xdf0, |
| 351 | [IPROC_PCIE_OMAP3] = 0xdf8, |
| 352 | [IPROC_PCIE_IARR0] = 0xd00, |
| 353 | [IPROC_PCIE_IMAP0] = 0xc00, |
| 354 | [IPROC_PCIE_IARR2] = 0xd10, |
| 355 | [IPROC_PCIE_IMAP2] = 0xcc0, |
| 356 | [IPROC_PCIE_IARR3] = 0xe00, |
| 357 | [IPROC_PCIE_IMAP3] = 0xe08, |
| 358 | [IPROC_PCIE_IARR4] = 0xe68, |
| 359 | [IPROC_PCIE_IMAP4] = 0xe70, |
Srinath Mannam | 73b9e4d | 2019-03-01 10:22:15 +0530 | [diff] [blame^] | 360 | [IPROC_PCIE_CFG_RD_STATUS] = 0xee0, |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 361 | [IPROC_PCIE_LINK_STATUS] = 0xf0c, |
| 362 | [IPROC_PCIE_APB_ERR_EN] = 0xf40, |
Ray Jui | c7c4452 | 2016-10-31 17:38:41 -0700 | [diff] [blame] | 363 | }; |
| 364 | |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 365 | /* iProc PCIe PAXC v1 registers */ |
| 366 | static const u16 iproc_pcie_reg_paxc[] = { |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 367 | [IPROC_PCIE_CLK_CTRL] = 0x000, |
| 368 | [IPROC_PCIE_CFG_IND_ADDR] = 0x1f0, |
| 369 | [IPROC_PCIE_CFG_IND_DATA] = 0x1f4, |
| 370 | [IPROC_PCIE_CFG_ADDR] = 0x1f8, |
| 371 | [IPROC_PCIE_CFG_DATA] = 0x1fc, |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 372 | }; |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 373 | |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 374 | /* iProc PCIe PAXC v2 registers */ |
| 375 | static const u16 iproc_pcie_reg_paxc_v2[] = { |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 376 | [IPROC_PCIE_MSI_GIC_MODE] = 0x050, |
| 377 | [IPROC_PCIE_MSI_BASE_ADDR] = 0x074, |
| 378 | [IPROC_PCIE_MSI_WINDOW_SIZE] = 0x078, |
| 379 | [IPROC_PCIE_MSI_ADDR_LO] = 0x07c, |
| 380 | [IPROC_PCIE_MSI_ADDR_HI] = 0x080, |
| 381 | [IPROC_PCIE_MSI_EN_CFG] = 0x09c, |
| 382 | [IPROC_PCIE_CFG_IND_ADDR] = 0x1f0, |
| 383 | [IPROC_PCIE_CFG_IND_DATA] = 0x1f4, |
| 384 | [IPROC_PCIE_CFG_ADDR] = 0x1f8, |
| 385 | [IPROC_PCIE_CFG_DATA] = 0x1fc, |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 386 | }; |
| 387 | |
Ray Jui | 3bc7082 | 2018-06-11 17:21:04 -0700 | [diff] [blame] | 388 | /* |
| 389 | * List of device IDs of controllers that have corrupted capability list that |
| 390 | * require SW fixup |
| 391 | */ |
| 392 | static const u16 iproc_pcie_corrupt_cap_did[] = { |
| 393 | 0x16cd, |
| 394 | 0x16f0, |
| 395 | 0xd802, |
| 396 | 0xd804 |
| 397 | }; |
| 398 | |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 399 | static inline struct iproc_pcie *iproc_data(struct pci_bus *bus) |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 400 | { |
Rob Herring | a1b363a | 2018-03-07 09:42:36 -0600 | [diff] [blame] | 401 | struct iproc_pcie *pcie = bus->sysdata; |
Ray Jui | 8d9bfe3 | 2015-07-21 18:29:40 -0700 | [diff] [blame] | 402 | return pcie; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 405 | static inline bool iproc_pcie_reg_is_invalid(u16 reg_offset) |
| 406 | { |
| 407 | return !!(reg_offset == IPROC_PCIE_REG_INVALID); |
| 408 | } |
| 409 | |
| 410 | static inline u16 iproc_pcie_reg_offset(struct iproc_pcie *pcie, |
| 411 | enum iproc_pcie_reg reg) |
| 412 | { |
| 413 | return pcie->reg_offsets[reg]; |
| 414 | } |
| 415 | |
| 416 | static inline u32 iproc_pcie_read_reg(struct iproc_pcie *pcie, |
| 417 | enum iproc_pcie_reg reg) |
| 418 | { |
| 419 | u16 offset = iproc_pcie_reg_offset(pcie, reg); |
| 420 | |
| 421 | if (iproc_pcie_reg_is_invalid(offset)) |
| 422 | return 0; |
| 423 | |
| 424 | return readl(pcie->base + offset); |
| 425 | } |
| 426 | |
| 427 | static inline void iproc_pcie_write_reg(struct iproc_pcie *pcie, |
| 428 | enum iproc_pcie_reg reg, u32 val) |
| 429 | { |
| 430 | u16 offset = iproc_pcie_reg_offset(pcie, reg); |
| 431 | |
| 432 | if (iproc_pcie_reg_is_invalid(offset)) |
| 433 | return; |
| 434 | |
| 435 | writel(val, pcie->base + offset); |
| 436 | } |
| 437 | |
Ray Jui | 538928f | 2016-10-31 17:38:33 -0700 | [diff] [blame] | 438 | /** |
| 439 | * APB error forwarding can be disabled during access of configuration |
| 440 | * registers of the endpoint device, to prevent unsupported requests |
| 441 | * (typically seen during enumeration with multi-function devices) from |
| 442 | * triggering a system exception. |
| 443 | */ |
| 444 | static inline void iproc_pcie_apb_err_disable(struct pci_bus *bus, |
| 445 | bool disable) |
| 446 | { |
| 447 | struct iproc_pcie *pcie = iproc_data(bus); |
| 448 | u32 val; |
| 449 | |
| 450 | if (bus->number && pcie->has_apb_err_disable) { |
| 451 | val = iproc_pcie_read_reg(pcie, IPROC_PCIE_APB_ERR_EN); |
| 452 | if (disable) |
| 453 | val &= ~APB_ERR_EN; |
| 454 | else |
| 455 | val |= APB_ERR_EN; |
| 456 | iproc_pcie_write_reg(pcie, IPROC_PCIE_APB_ERR_EN, val); |
| 457 | } |
| 458 | } |
| 459 | |
Oza Pawandeep | d005045 | 2017-08-28 16:43:24 -0500 | [diff] [blame] | 460 | static void __iomem *iproc_pcie_map_ep_cfg_reg(struct iproc_pcie *pcie, |
| 461 | unsigned int busno, |
| 462 | unsigned int slot, |
| 463 | unsigned int fn, |
| 464 | int where) |
| 465 | { |
| 466 | u16 offset; |
| 467 | u32 val; |
| 468 | |
| 469 | /* EP device access */ |
| 470 | val = (busno << CFG_ADDR_BUS_NUM_SHIFT) | |
| 471 | (slot << CFG_ADDR_DEV_NUM_SHIFT) | |
| 472 | (fn << CFG_ADDR_FUNC_NUM_SHIFT) | |
| 473 | (where & CFG_ADDR_REG_NUM_MASK) | |
| 474 | (1 & CFG_ADDR_CFG_TYPE_MASK); |
| 475 | |
| 476 | iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_ADDR, val); |
| 477 | offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_DATA); |
| 478 | |
| 479 | if (iproc_pcie_reg_is_invalid(offset)) |
| 480 | return NULL; |
| 481 | |
| 482 | return (pcie->base + offset); |
| 483 | } |
| 484 | |
Srinath Mannam | 73b9e4d | 2019-03-01 10:22:15 +0530 | [diff] [blame^] | 485 | static unsigned int iproc_pcie_cfg_retry(struct iproc_pcie *pcie, |
| 486 | void __iomem *cfg_data_p) |
Oza Pawandeep | 39b7a4f | 2017-08-28 16:43:30 -0500 | [diff] [blame] | 487 | { |
| 488 | int timeout = CFG_RETRY_STATUS_TIMEOUT_US; |
| 489 | unsigned int data; |
Srinath Mannam | 73b9e4d | 2019-03-01 10:22:15 +0530 | [diff] [blame^] | 490 | u32 status; |
Oza Pawandeep | 39b7a4f | 2017-08-28 16:43:30 -0500 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * As per PCIe spec r3.1, sec 2.3.2, CRS Software Visibility only |
| 494 | * affects config reads of the Vendor ID. For config writes or any |
| 495 | * other config reads, the Root may automatically reissue the |
| 496 | * configuration request again as a new request. |
| 497 | * |
| 498 | * For config reads, this hardware returns CFG_RETRY_STATUS data |
| 499 | * when it receives a CRS completion, regardless of the address of |
| 500 | * the read or the CRS Software Visibility Enable bit. As a |
| 501 | * partial workaround for this, we retry in software any read that |
| 502 | * returns CFG_RETRY_STATUS. |
| 503 | * |
| 504 | * Note that a non-Vendor ID config register may have a value of |
| 505 | * CFG_RETRY_STATUS. If we read that, we can't distinguish it from |
| 506 | * a CRS completion, so we will incorrectly retry the read and |
| 507 | * eventually return the wrong data (0xffffffff). |
| 508 | */ |
| 509 | data = readl(cfg_data_p); |
| 510 | while (data == CFG_RETRY_STATUS && timeout--) { |
Srinath Mannam | 73b9e4d | 2019-03-01 10:22:15 +0530 | [diff] [blame^] | 511 | /* |
| 512 | * CRS state is set in CFG_RD status register |
| 513 | * This will handle the case where CFG_RETRY_STATUS is |
| 514 | * valid config data. |
| 515 | */ |
| 516 | status = iproc_pcie_read_reg(pcie, IPROC_PCIE_CFG_RD_STATUS); |
| 517 | if (status != CFG_RD_CRS) |
| 518 | return data; |
| 519 | |
Oza Pawandeep | 39b7a4f | 2017-08-28 16:43:30 -0500 | [diff] [blame] | 520 | udelay(1); |
| 521 | data = readl(cfg_data_p); |
| 522 | } |
| 523 | |
| 524 | if (data == CFG_RETRY_STATUS) |
| 525 | data = 0xffffffff; |
| 526 | |
| 527 | return data; |
| 528 | } |
| 529 | |
Ray Jui | 3bc7082 | 2018-06-11 17:21:04 -0700 | [diff] [blame] | 530 | static void iproc_pcie_fix_cap(struct iproc_pcie *pcie, int where, u32 *val) |
| 531 | { |
| 532 | u32 i, dev_id; |
| 533 | |
| 534 | switch (where & ~0x3) { |
| 535 | case PCI_VENDOR_ID: |
| 536 | dev_id = *val >> 16; |
| 537 | |
| 538 | /* |
| 539 | * Activate fixup for those controllers that have corrupted |
| 540 | * capability list registers |
| 541 | */ |
| 542 | for (i = 0; i < ARRAY_SIZE(iproc_pcie_corrupt_cap_did); i++) |
| 543 | if (dev_id == iproc_pcie_corrupt_cap_did[i]) |
| 544 | pcie->fix_paxc_cap = true; |
| 545 | break; |
| 546 | |
| 547 | case IPROC_PCI_PM_CAP: |
| 548 | if (pcie->fix_paxc_cap) { |
| 549 | /* advertise PM, force next capability to PCIe */ |
| 550 | *val &= ~IPROC_PCI_PM_CAP_MASK; |
| 551 | *val |= IPROC_PCI_EXP_CAP << 8 | PCI_CAP_ID_PM; |
| 552 | } |
| 553 | break; |
| 554 | |
| 555 | case IPROC_PCI_EXP_CAP: |
| 556 | if (pcie->fix_paxc_cap) { |
| 557 | /* advertise root port, version 2, terminate here */ |
| 558 | *val = (PCI_EXP_TYPE_ROOT_PORT << 4 | 2) << 16 | |
| 559 | PCI_CAP_ID_EXP; |
| 560 | } |
| 561 | break; |
| 562 | |
| 563 | case IPROC_PCI_EXP_CAP + PCI_EXP_RTCTL: |
| 564 | /* Don't advertise CRS SV support */ |
| 565 | *val &= ~(PCI_EXP_RTCAP_CRSVIS << 16); |
| 566 | break; |
| 567 | |
| 568 | default: |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | |
Oza Pawandeep | 39b7a4f | 2017-08-28 16:43:30 -0500 | [diff] [blame] | 573 | static int iproc_pcie_config_read(struct pci_bus *bus, unsigned int devfn, |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 574 | int where, int size, u32 *val) |
Oza Pawandeep | 39b7a4f | 2017-08-28 16:43:30 -0500 | [diff] [blame] | 575 | { |
| 576 | struct iproc_pcie *pcie = iproc_data(bus); |
| 577 | unsigned int slot = PCI_SLOT(devfn); |
| 578 | unsigned int fn = PCI_FUNC(devfn); |
| 579 | unsigned int busno = bus->number; |
| 580 | void __iomem *cfg_data_p; |
| 581 | unsigned int data; |
| 582 | int ret; |
| 583 | |
| 584 | /* root complex access */ |
| 585 | if (busno == 0) { |
| 586 | ret = pci_generic_config_read32(bus, devfn, where, size, val); |
Ray Jui | 3bc7082 | 2018-06-11 17:21:04 -0700 | [diff] [blame] | 587 | if (ret == PCIBIOS_SUCCESSFUL) |
| 588 | iproc_pcie_fix_cap(pcie, where, val); |
Oza Pawandeep | 39b7a4f | 2017-08-28 16:43:30 -0500 | [diff] [blame] | 589 | |
Ray Jui | 3bc7082 | 2018-06-11 17:21:04 -0700 | [diff] [blame] | 590 | return ret; |
Oza Pawandeep | 39b7a4f | 2017-08-28 16:43:30 -0500 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | cfg_data_p = iproc_pcie_map_ep_cfg_reg(pcie, busno, slot, fn, where); |
| 594 | |
| 595 | if (!cfg_data_p) |
| 596 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 597 | |
Srinath Mannam | 73b9e4d | 2019-03-01 10:22:15 +0530 | [diff] [blame^] | 598 | data = iproc_pcie_cfg_retry(pcie, cfg_data_p); |
Oza Pawandeep | 39b7a4f | 2017-08-28 16:43:30 -0500 | [diff] [blame] | 599 | |
| 600 | *val = data; |
| 601 | if (size <= 2) |
| 602 | *val = (data >> (8 * (where & 3))) & ((1 << (size * 8)) - 1); |
| 603 | |
Ray Jui | f78e60a | 2018-06-11 17:21:06 -0700 | [diff] [blame] | 604 | /* |
| 605 | * For PAXC and PAXCv2, the total number of PFs that one can enumerate |
| 606 | * depends on the firmware configuration. Unfortunately, due to an ASIC |
| 607 | * bug, unconfigured PFs cannot be properly hidden from the root |
| 608 | * complex. As a result, write access to these PFs will cause bus lock |
| 609 | * up on the embedded processor |
| 610 | * |
| 611 | * Since all unconfigured PFs are left with an incorrect, staled device |
| 612 | * ID of 0x168e (PCI_DEVICE_ID_NX2_57810), we try to catch those access |
| 613 | * early here and reject them all |
| 614 | */ |
| 615 | #define DEVICE_ID_MASK 0xffff0000 |
| 616 | #define DEVICE_ID_SHIFT 16 |
| 617 | if (pcie->rej_unconfig_pf && |
| 618 | (where & CFG_ADDR_REG_NUM_MASK) == PCI_VENDOR_ID) |
| 619 | if ((*val & DEVICE_ID_MASK) == |
| 620 | (PCI_DEVICE_ID_NX2_57810 << DEVICE_ID_SHIFT)) |
| 621 | return PCIBIOS_FUNC_NOT_SUPPORTED; |
| 622 | |
Oza Pawandeep | 39b7a4f | 2017-08-28 16:43:30 -0500 | [diff] [blame] | 623 | return PCIBIOS_SUCCESSFUL; |
| 624 | } |
| 625 | |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 626 | /** |
| 627 | * Note access to the configuration registers are protected at the higher layer |
| 628 | * by 'pci_lock' in drivers/pci/access.c |
| 629 | */ |
Lorenzo Pieralisi | 022adcf | 2017-06-28 15:13:50 -0500 | [diff] [blame] | 630 | static void __iomem *iproc_pcie_map_cfg_bus(struct iproc_pcie *pcie, |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 631 | int busno, unsigned int devfn, |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 632 | int where) |
| 633 | { |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 634 | unsigned slot = PCI_SLOT(devfn); |
| 635 | unsigned fn = PCI_FUNC(devfn); |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 636 | u16 offset; |
| 637 | |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 638 | /* root complex access */ |
| 639 | if (busno == 0) { |
Ray Jui | 4656038 | 2016-01-27 16:52:24 -0600 | [diff] [blame] | 640 | if (slot > 0 || fn > 0) |
| 641 | return NULL; |
| 642 | |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 643 | iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_IND_ADDR, |
| 644 | where & CFG_IND_ADDR_MASK); |
| 645 | offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_IND_DATA); |
| 646 | if (iproc_pcie_reg_is_invalid(offset)) |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 647 | return NULL; |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 648 | else |
| 649 | return (pcie->base + offset); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Oza Pawandeep | d005045 | 2017-08-28 16:43:24 -0500 | [diff] [blame] | 652 | return iproc_pcie_map_ep_cfg_reg(pcie, busno, slot, fn, where); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Lorenzo Pieralisi | 022adcf | 2017-06-28 15:13:50 -0500 | [diff] [blame] | 655 | static void __iomem *iproc_pcie_bus_map_cfg_bus(struct pci_bus *bus, |
| 656 | unsigned int devfn, |
| 657 | int where) |
| 658 | { |
| 659 | return iproc_pcie_map_cfg_bus(iproc_data(bus), bus->number, devfn, |
| 660 | where); |
| 661 | } |
| 662 | |
| 663 | static int iproc_pci_raw_config_read32(struct iproc_pcie *pcie, |
| 664 | unsigned int devfn, int where, |
| 665 | int size, u32 *val) |
| 666 | { |
| 667 | void __iomem *addr; |
| 668 | |
| 669 | addr = iproc_pcie_map_cfg_bus(pcie, 0, devfn, where & ~0x3); |
| 670 | if (!addr) { |
| 671 | *val = ~0; |
| 672 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 673 | } |
| 674 | |
| 675 | *val = readl(addr); |
| 676 | |
| 677 | if (size <= 2) |
| 678 | *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1); |
| 679 | |
| 680 | return PCIBIOS_SUCCESSFUL; |
| 681 | } |
| 682 | |
| 683 | static int iproc_pci_raw_config_write32(struct iproc_pcie *pcie, |
| 684 | unsigned int devfn, int where, |
| 685 | int size, u32 val) |
| 686 | { |
| 687 | void __iomem *addr; |
| 688 | u32 mask, tmp; |
| 689 | |
| 690 | addr = iproc_pcie_map_cfg_bus(pcie, 0, devfn, where & ~0x3); |
| 691 | if (!addr) |
| 692 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 693 | |
| 694 | if (size == 4) { |
| 695 | writel(val, addr); |
| 696 | return PCIBIOS_SUCCESSFUL; |
| 697 | } |
| 698 | |
| 699 | mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8)); |
| 700 | tmp = readl(addr) & mask; |
| 701 | tmp |= val << ((where & 0x3) * 8); |
| 702 | writel(tmp, addr); |
| 703 | |
| 704 | return PCIBIOS_SUCCESSFUL; |
| 705 | } |
| 706 | |
Ray Jui | 538928f | 2016-10-31 17:38:33 -0700 | [diff] [blame] | 707 | static int iproc_pcie_config_read32(struct pci_bus *bus, unsigned int devfn, |
| 708 | int where, int size, u32 *val) |
| 709 | { |
| 710 | int ret; |
Oza Pawandeep | 39b7a4f | 2017-08-28 16:43:30 -0500 | [diff] [blame] | 711 | struct iproc_pcie *pcie = iproc_data(bus); |
Ray Jui | 538928f | 2016-10-31 17:38:33 -0700 | [diff] [blame] | 712 | |
| 713 | iproc_pcie_apb_err_disable(bus, true); |
Ray Jui | f78e60a | 2018-06-11 17:21:06 -0700 | [diff] [blame] | 714 | if (pcie->iproc_cfg_read) |
Oza Pawandeep | 39b7a4f | 2017-08-28 16:43:30 -0500 | [diff] [blame] | 715 | ret = iproc_pcie_config_read(bus, devfn, where, size, val); |
| 716 | else |
| 717 | ret = pci_generic_config_read32(bus, devfn, where, size, val); |
Ray Jui | 538928f | 2016-10-31 17:38:33 -0700 | [diff] [blame] | 718 | iproc_pcie_apb_err_disable(bus, false); |
| 719 | |
| 720 | return ret; |
| 721 | } |
| 722 | |
| 723 | static int iproc_pcie_config_write32(struct pci_bus *bus, unsigned int devfn, |
| 724 | int where, int size, u32 val) |
| 725 | { |
| 726 | int ret; |
| 727 | |
| 728 | iproc_pcie_apb_err_disable(bus, true); |
| 729 | ret = pci_generic_config_write32(bus, devfn, where, size, val); |
| 730 | iproc_pcie_apb_err_disable(bus, false); |
| 731 | |
| 732 | return ret; |
| 733 | } |
| 734 | |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 735 | static struct pci_ops iproc_pcie_ops = { |
Lorenzo Pieralisi | 022adcf | 2017-06-28 15:13:50 -0500 | [diff] [blame] | 736 | .map_bus = iproc_pcie_bus_map_cfg_bus, |
Ray Jui | 538928f | 2016-10-31 17:38:33 -0700 | [diff] [blame] | 737 | .read = iproc_pcie_config_read32, |
| 738 | .write = iproc_pcie_config_write32, |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 739 | }; |
| 740 | |
Oza Pawandeep | b91c26c | 2017-08-28 16:43:35 -0500 | [diff] [blame] | 741 | static void iproc_pcie_perst_ctrl(struct iproc_pcie *pcie, bool assert) |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 742 | { |
| 743 | u32 val; |
| 744 | |
Ray Jui | 7cbd50d | 2016-10-31 17:38:31 -0700 | [diff] [blame] | 745 | /* |
| 746 | * PAXC and the internal emulated endpoint device downstream should not |
| 747 | * be reset. If firmware has been loaded on the endpoint device at an |
| 748 | * earlier boot stage, reset here causes issues. |
| 749 | */ |
| 750 | if (pcie->ep_is_internal) |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 751 | return; |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 752 | |
Oza Pawandeep | b91c26c | 2017-08-28 16:43:35 -0500 | [diff] [blame] | 753 | if (assert) { |
| 754 | val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL); |
| 755 | val &= ~EP_PERST_SOURCE_SELECT & ~EP_MODE_SURVIVE_PERST & |
| 756 | ~RC_PCIE_RST_OUTPUT; |
| 757 | iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val); |
| 758 | udelay(250); |
| 759 | } else { |
| 760 | val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL); |
| 761 | val |= RC_PCIE_RST_OUTPUT; |
| 762 | iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val); |
| 763 | msleep(100); |
| 764 | } |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 765 | } |
| 766 | |
Oza Pawandeep | b91c26c | 2017-08-28 16:43:35 -0500 | [diff] [blame] | 767 | int iproc_pcie_shutdown(struct iproc_pcie *pcie) |
| 768 | { |
| 769 | iproc_pcie_perst_ctrl(pcie, true); |
| 770 | msleep(500); |
| 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | EXPORT_SYMBOL_GPL(iproc_pcie_shutdown); |
| 775 | |
Lorenzo Pieralisi | 022adcf | 2017-06-28 15:13:50 -0500 | [diff] [blame] | 776 | static int iproc_pcie_check_link(struct iproc_pcie *pcie) |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 777 | { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 778 | struct device *dev = pcie->dev; |
Lorenzo Pieralisi | 022adcf | 2017-06-28 15:13:50 -0500 | [diff] [blame] | 779 | u32 hdr_type, link_ctrl, link_status, class, val; |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 780 | bool link_is_active = false; |
| 781 | |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 782 | /* |
| 783 | * PAXC connects to emulated endpoint devices directly and does not |
| 784 | * have a Serdes. Therefore skip the link detection logic here. |
| 785 | */ |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 786 | if (pcie->ep_is_internal) |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 787 | return 0; |
| 788 | |
| 789 | val = iproc_pcie_read_reg(pcie, IPROC_PCIE_LINK_STATUS); |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 790 | if (!(val & PCIE_PHYLINKUP) || !(val & PCIE_DL_ACTIVE)) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 791 | dev_err(dev, "PHY or data link is INACTIVE!\n"); |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 792 | return -ENODEV; |
| 793 | } |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 794 | |
| 795 | /* make sure we are not in EP mode */ |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 796 | iproc_pci_raw_config_read32(pcie, 0, PCI_HEADER_TYPE, 1, &hdr_type); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 797 | if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 798 | dev_err(dev, "in EP mode, hdr=%#02x\n", hdr_type); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 799 | return -EFAULT; |
| 800 | } |
| 801 | |
| 802 | /* force class to PCI_CLASS_BRIDGE_PCI (0x0604) */ |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 803 | #define PCI_BRIDGE_CTRL_REG_OFFSET 0x43c |
| 804 | #define PCI_CLASS_BRIDGE_MASK 0xffff00 |
| 805 | #define PCI_CLASS_BRIDGE_SHIFT 8 |
Lorenzo Pieralisi | 022adcf | 2017-06-28 15:13:50 -0500 | [diff] [blame] | 806 | iproc_pci_raw_config_read32(pcie, 0, PCI_BRIDGE_CTRL_REG_OFFSET, |
| 807 | 4, &class); |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 808 | class &= ~PCI_CLASS_BRIDGE_MASK; |
| 809 | class |= (PCI_CLASS_BRIDGE_PCI << PCI_CLASS_BRIDGE_SHIFT); |
Lorenzo Pieralisi | 022adcf | 2017-06-28 15:13:50 -0500 | [diff] [blame] | 810 | iproc_pci_raw_config_write32(pcie, 0, PCI_BRIDGE_CTRL_REG_OFFSET, |
| 811 | 4, class); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 812 | |
| 813 | /* check link status to see if link is active */ |
Bjorn Helgaas | d8fa934 | 2017-09-05 12:27:11 -0500 | [diff] [blame] | 814 | iproc_pci_raw_config_read32(pcie, 0, IPROC_PCI_EXP_CAP + PCI_EXP_LNKSTA, |
Lorenzo Pieralisi | 022adcf | 2017-06-28 15:13:50 -0500 | [diff] [blame] | 815 | 2, &link_status); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 816 | if (link_status & PCI_EXP_LNKSTA_NLW) |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 817 | link_is_active = true; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 818 | |
| 819 | if (!link_is_active) { |
| 820 | /* try GEN 1 link speed */ |
Bjorn Helgaas | ef685b3 | 2017-09-05 12:33:33 -0500 | [diff] [blame] | 821 | #define PCI_TARGET_LINK_SPEED_MASK 0xf |
| 822 | #define PCI_TARGET_LINK_SPEED_GEN2 0x2 |
| 823 | #define PCI_TARGET_LINK_SPEED_GEN1 0x1 |
Lorenzo Pieralisi | 022adcf | 2017-06-28 15:13:50 -0500 | [diff] [blame] | 824 | iproc_pci_raw_config_read32(pcie, 0, |
Bjorn Helgaas | d8fa934 | 2017-09-05 12:27:11 -0500 | [diff] [blame] | 825 | IPROC_PCI_EXP_CAP + PCI_EXP_LNKCTL2, |
| 826 | 4, &link_ctrl); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 827 | if ((link_ctrl & PCI_TARGET_LINK_SPEED_MASK) == |
| 828 | PCI_TARGET_LINK_SPEED_GEN2) { |
| 829 | link_ctrl &= ~PCI_TARGET_LINK_SPEED_MASK; |
| 830 | link_ctrl |= PCI_TARGET_LINK_SPEED_GEN1; |
Lorenzo Pieralisi | 022adcf | 2017-06-28 15:13:50 -0500 | [diff] [blame] | 831 | iproc_pci_raw_config_write32(pcie, 0, |
Bjorn Helgaas | d8fa934 | 2017-09-05 12:27:11 -0500 | [diff] [blame] | 832 | IPROC_PCI_EXP_CAP + PCI_EXP_LNKCTL2, |
| 833 | 4, link_ctrl); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 834 | msleep(100); |
| 835 | |
Lorenzo Pieralisi | 022adcf | 2017-06-28 15:13:50 -0500 | [diff] [blame] | 836 | iproc_pci_raw_config_read32(pcie, 0, |
Bjorn Helgaas | d8fa934 | 2017-09-05 12:27:11 -0500 | [diff] [blame] | 837 | IPROC_PCI_EXP_CAP + PCI_EXP_LNKSTA, |
| 838 | 2, &link_status); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 839 | if (link_status & PCI_EXP_LNKSTA_NLW) |
Ray Jui | aaf22ab | 2015-09-15 17:39:19 -0700 | [diff] [blame] | 840 | link_is_active = true; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 844 | dev_info(dev, "link: %s\n", link_is_active ? "UP" : "DOWN"); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 845 | |
| 846 | return link_is_active ? 0 : -ENODEV; |
| 847 | } |
| 848 | |
| 849 | static void iproc_pcie_enable(struct iproc_pcie *pcie) |
| 850 | { |
Ray Jui | 943ebae | 2015-12-04 09:34:59 -0800 | [diff] [blame] | 851 | iproc_pcie_write_reg(pcie, IPROC_PCIE_INTX_EN, SYS_RC_INTX_MASK); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Ray Jui | 4213e15 | 2016-10-31 17:38:37 -0700 | [diff] [blame] | 854 | static inline bool iproc_pcie_ob_is_valid(struct iproc_pcie *pcie, |
| 855 | int window_idx) |
| 856 | { |
| 857 | u32 val; |
| 858 | |
| 859 | val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_OARR0, window_idx)); |
| 860 | |
| 861 | return !!(val & OARR_VALID); |
| 862 | } |
| 863 | |
| 864 | static inline int iproc_pcie_ob_write(struct iproc_pcie *pcie, int window_idx, |
| 865 | int size_idx, u64 axi_addr, u64 pci_addr) |
| 866 | { |
| 867 | struct device *dev = pcie->dev; |
| 868 | u16 oarr_offset, omap_offset; |
| 869 | |
| 870 | /* |
| 871 | * Derive the OARR/OMAP offset from the first pair (OARR0/OMAP0) based |
| 872 | * on window index. |
| 873 | */ |
| 874 | oarr_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OARR0, |
| 875 | window_idx)); |
| 876 | omap_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OMAP0, |
| 877 | window_idx)); |
| 878 | if (iproc_pcie_reg_is_invalid(oarr_offset) || |
| 879 | iproc_pcie_reg_is_invalid(omap_offset)) |
| 880 | return -EINVAL; |
| 881 | |
| 882 | /* |
| 883 | * Program the OARR registers. The upper 32-bit OARR register is |
| 884 | * always right after the lower 32-bit OARR register. |
| 885 | */ |
| 886 | writel(lower_32_bits(axi_addr) | (size_idx << OARR_SIZE_CFG_SHIFT) | |
| 887 | OARR_VALID, pcie->base + oarr_offset); |
| 888 | writel(upper_32_bits(axi_addr), pcie->base + oarr_offset + 4); |
| 889 | |
| 890 | /* now program the OMAP registers */ |
| 891 | writel(lower_32_bits(pci_addr), pcie->base + omap_offset); |
| 892 | writel(upper_32_bits(pci_addr), pcie->base + omap_offset + 4); |
| 893 | |
Ray Jui | 0043d4a | 2018-06-11 17:21:07 -0700 | [diff] [blame] | 894 | dev_dbg(dev, "ob window [%d]: offset 0x%x axi %pap pci %pap\n", |
| 895 | window_idx, oarr_offset, &axi_addr, &pci_addr); |
| 896 | dev_dbg(dev, "oarr lo 0x%x oarr hi 0x%x\n", |
| 897 | readl(pcie->base + oarr_offset), |
| 898 | readl(pcie->base + oarr_offset + 4)); |
| 899 | dev_dbg(dev, "omap lo 0x%x omap hi 0x%x\n", |
| 900 | readl(pcie->base + omap_offset), |
| 901 | readl(pcie->base + omap_offset + 4)); |
Ray Jui | 4213e15 | 2016-10-31 17:38:37 -0700 | [diff] [blame] | 902 | |
| 903 | return 0; |
| 904 | } |
| 905 | |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 906 | /** |
| 907 | * Some iProc SoCs require the SW to configure the outbound address mapping |
| 908 | * |
| 909 | * Outbound address translation: |
| 910 | * |
| 911 | * iproc_pcie_address = axi_address - axi_offset |
| 912 | * OARR = iproc_pcie_address |
| 913 | * OMAP = pci_addr |
| 914 | * |
| 915 | * axi_addr -> iproc_pcie_address -> OARR -> OMAP -> pci_address |
| 916 | */ |
| 917 | static int iproc_pcie_setup_ob(struct iproc_pcie *pcie, u64 axi_addr, |
| 918 | u64 pci_addr, resource_size_t size) |
| 919 | { |
| 920 | struct iproc_pcie_ob *ob = &pcie->ob; |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 921 | struct device *dev = pcie->dev; |
Ray Jui | 4213e15 | 2016-10-31 17:38:37 -0700 | [diff] [blame] | 922 | int ret = -EINVAL, window_idx, size_idx; |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 923 | |
| 924 | if (axi_addr < ob->axi_offset) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 925 | dev_err(dev, "axi address %pap less than offset %pap\n", |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 926 | &axi_addr, &ob->axi_offset); |
| 927 | return -EINVAL; |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * Translate the AXI address to the internal address used by the iProc |
| 932 | * PCIe core before programming the OARR |
| 933 | */ |
| 934 | axi_addr -= ob->axi_offset; |
| 935 | |
Ray Jui | 4213e15 | 2016-10-31 17:38:37 -0700 | [diff] [blame] | 936 | /* iterate through all OARR/OMAP mapping windows */ |
| 937 | for (window_idx = ob->nr_windows - 1; window_idx >= 0; window_idx--) { |
| 938 | const struct iproc_pcie_ob_map *ob_map = |
| 939 | &pcie->ob_map[window_idx]; |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 940 | |
Ray Jui | 4213e15 | 2016-10-31 17:38:37 -0700 | [diff] [blame] | 941 | /* |
| 942 | * If current outbound window is already in use, move on to the |
| 943 | * next one. |
| 944 | */ |
| 945 | if (iproc_pcie_ob_is_valid(pcie, window_idx)) |
| 946 | continue; |
| 947 | |
| 948 | /* |
| 949 | * Iterate through all supported window sizes within the |
| 950 | * OARR/OMAP pair to find a match. Go through the window sizes |
| 951 | * in a descending order. |
| 952 | */ |
| 953 | for (size_idx = ob_map->nr_sizes - 1; size_idx >= 0; |
| 954 | size_idx--) { |
| 955 | resource_size_t window_size = |
| 956 | ob_map->window_sizes[size_idx] * SZ_1M; |
| 957 | |
| 958 | if (size < window_size) |
| 959 | continue; |
| 960 | |
| 961 | if (!IS_ALIGNED(axi_addr, window_size) || |
| 962 | !IS_ALIGNED(pci_addr, window_size)) { |
| 963 | dev_err(dev, |
| 964 | "axi %pap or pci %pap not aligned\n", |
| 965 | &axi_addr, &pci_addr); |
| 966 | return -EINVAL; |
| 967 | } |
| 968 | |
| 969 | /* |
| 970 | * Match found! Program both OARR and OMAP and mark |
| 971 | * them as a valid entry. |
| 972 | */ |
| 973 | ret = iproc_pcie_ob_write(pcie, window_idx, size_idx, |
| 974 | axi_addr, pci_addr); |
| 975 | if (ret) |
| 976 | goto err_ob; |
| 977 | |
| 978 | size -= window_size; |
| 979 | if (size == 0) |
| 980 | return 0; |
| 981 | |
| 982 | /* |
| 983 | * If we are here, we are done with the current window, |
| 984 | * but not yet finished all mappings. Need to move on |
| 985 | * to the next window. |
| 986 | */ |
| 987 | axi_addr += window_size; |
| 988 | pci_addr += window_size; |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 989 | break; |
Ray Jui | 4213e15 | 2016-10-31 17:38:37 -0700 | [diff] [blame] | 990 | } |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 991 | } |
| 992 | |
Ray Jui | 4213e15 | 2016-10-31 17:38:37 -0700 | [diff] [blame] | 993 | err_ob: |
| 994 | dev_err(dev, "unable to configure outbound mapping\n"); |
| 995 | dev_err(dev, |
| 996 | "axi %pap, axi offset %pap, pci %pap, res size %pap\n", |
| 997 | &axi_addr, &ob->axi_offset, &pci_addr, &size); |
| 998 | |
| 999 | return ret; |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | static int iproc_pcie_map_ranges(struct iproc_pcie *pcie, |
| 1003 | struct list_head *resources) |
| 1004 | { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 1005 | struct device *dev = pcie->dev; |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 1006 | struct resource_entry *window; |
| 1007 | int ret; |
| 1008 | |
| 1009 | resource_list_for_each_entry(window, resources) { |
| 1010 | struct resource *res = window->res; |
| 1011 | u64 res_type = resource_type(res); |
| 1012 | |
| 1013 | switch (res_type) { |
| 1014 | case IORESOURCE_IO: |
| 1015 | case IORESOURCE_BUS: |
| 1016 | break; |
| 1017 | case IORESOURCE_MEM: |
| 1018 | ret = iproc_pcie_setup_ob(pcie, res->start, |
| 1019 | res->start - window->offset, |
| 1020 | resource_size(res)); |
| 1021 | if (ret) |
| 1022 | return ret; |
| 1023 | break; |
| 1024 | default: |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 1025 | dev_err(dev, "invalid resource %pR\n", res); |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 1026 | return -EINVAL; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
Ray Jui | dd9d4e7 | 2016-10-31 17:38:39 -0700 | [diff] [blame] | 1033 | static inline bool iproc_pcie_ib_is_in_use(struct iproc_pcie *pcie, |
| 1034 | int region_idx) |
| 1035 | { |
| 1036 | const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx]; |
| 1037 | u32 val; |
| 1038 | |
| 1039 | val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_IARR0, region_idx)); |
| 1040 | |
| 1041 | return !!(val & (BIT(ib_map->nr_sizes) - 1)); |
| 1042 | } |
| 1043 | |
| 1044 | static inline bool iproc_pcie_ib_check_type(const struct iproc_pcie_ib_map *ib_map, |
| 1045 | enum iproc_pcie_ib_map_type type) |
| 1046 | { |
| 1047 | return !!(ib_map->type == type); |
| 1048 | } |
| 1049 | |
| 1050 | static int iproc_pcie_ib_write(struct iproc_pcie *pcie, int region_idx, |
| 1051 | int size_idx, int nr_windows, u64 axi_addr, |
| 1052 | u64 pci_addr, resource_size_t size) |
| 1053 | { |
| 1054 | struct device *dev = pcie->dev; |
| 1055 | const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx]; |
| 1056 | u16 iarr_offset, imap_offset; |
| 1057 | u32 val; |
| 1058 | int window_idx; |
| 1059 | |
| 1060 | iarr_offset = iproc_pcie_reg_offset(pcie, |
| 1061 | MAP_REG(IPROC_PCIE_IARR0, region_idx)); |
| 1062 | imap_offset = iproc_pcie_reg_offset(pcie, |
| 1063 | MAP_REG(IPROC_PCIE_IMAP0, region_idx)); |
| 1064 | if (iproc_pcie_reg_is_invalid(iarr_offset) || |
| 1065 | iproc_pcie_reg_is_invalid(imap_offset)) |
| 1066 | return -EINVAL; |
| 1067 | |
Ray Jui | 0043d4a | 2018-06-11 17:21:07 -0700 | [diff] [blame] | 1068 | dev_dbg(dev, "ib region [%d]: offset 0x%x axi %pap pci %pap\n", |
| 1069 | region_idx, iarr_offset, &axi_addr, &pci_addr); |
Ray Jui | dd9d4e7 | 2016-10-31 17:38:39 -0700 | [diff] [blame] | 1070 | |
| 1071 | /* |
| 1072 | * Program the IARR registers. The upper 32-bit IARR register is |
| 1073 | * always right after the lower 32-bit IARR register. |
| 1074 | */ |
| 1075 | writel(lower_32_bits(pci_addr) | BIT(size_idx), |
| 1076 | pcie->base + iarr_offset); |
| 1077 | writel(upper_32_bits(pci_addr), pcie->base + iarr_offset + 4); |
| 1078 | |
Ray Jui | 0043d4a | 2018-06-11 17:21:07 -0700 | [diff] [blame] | 1079 | dev_dbg(dev, "iarr lo 0x%x iarr hi 0x%x\n", |
| 1080 | readl(pcie->base + iarr_offset), |
| 1081 | readl(pcie->base + iarr_offset + 4)); |
Ray Jui | dd9d4e7 | 2016-10-31 17:38:39 -0700 | [diff] [blame] | 1082 | |
| 1083 | /* |
| 1084 | * Now program the IMAP registers. Each IARR region may have one or |
| 1085 | * more IMAP windows. |
| 1086 | */ |
| 1087 | size >>= ilog2(nr_windows); |
| 1088 | for (window_idx = 0; window_idx < nr_windows; window_idx++) { |
| 1089 | val = readl(pcie->base + imap_offset); |
| 1090 | val |= lower_32_bits(axi_addr) | IMAP_VALID; |
| 1091 | writel(val, pcie->base + imap_offset); |
| 1092 | writel(upper_32_bits(axi_addr), |
| 1093 | pcie->base + imap_offset + ib_map->imap_addr_offset); |
| 1094 | |
Ray Jui | 0043d4a | 2018-06-11 17:21:07 -0700 | [diff] [blame] | 1095 | dev_dbg(dev, "imap window [%d] lo 0x%x hi 0x%x\n", |
| 1096 | window_idx, readl(pcie->base + imap_offset), |
| 1097 | readl(pcie->base + imap_offset + |
| 1098 | ib_map->imap_addr_offset)); |
Ray Jui | dd9d4e7 | 2016-10-31 17:38:39 -0700 | [diff] [blame] | 1099 | |
| 1100 | imap_offset += ib_map->imap_window_offset; |
| 1101 | axi_addr += size; |
| 1102 | } |
| 1103 | |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | static int iproc_pcie_setup_ib(struct iproc_pcie *pcie, |
| 1108 | struct of_pci_range *range, |
| 1109 | enum iproc_pcie_ib_map_type type) |
| 1110 | { |
| 1111 | struct device *dev = pcie->dev; |
| 1112 | struct iproc_pcie_ib *ib = &pcie->ib; |
| 1113 | int ret; |
| 1114 | unsigned int region_idx, size_idx; |
| 1115 | u64 axi_addr = range->cpu_addr, pci_addr = range->pci_addr; |
| 1116 | resource_size_t size = range->size; |
| 1117 | |
| 1118 | /* iterate through all IARR mapping regions */ |
| 1119 | for (region_idx = 0; region_idx < ib->nr_regions; region_idx++) { |
| 1120 | const struct iproc_pcie_ib_map *ib_map = |
| 1121 | &pcie->ib_map[region_idx]; |
| 1122 | |
| 1123 | /* |
| 1124 | * If current inbound region is already in use or not a |
| 1125 | * compatible type, move on to the next. |
| 1126 | */ |
| 1127 | if (iproc_pcie_ib_is_in_use(pcie, region_idx) || |
| 1128 | !iproc_pcie_ib_check_type(ib_map, type)) |
| 1129 | continue; |
| 1130 | |
| 1131 | /* iterate through all supported region sizes to find a match */ |
| 1132 | for (size_idx = 0; size_idx < ib_map->nr_sizes; size_idx++) { |
| 1133 | resource_size_t region_size = |
| 1134 | ib_map->region_sizes[size_idx] * ib_map->size_unit; |
| 1135 | |
| 1136 | if (size != region_size) |
| 1137 | continue; |
| 1138 | |
| 1139 | if (!IS_ALIGNED(axi_addr, region_size) || |
| 1140 | !IS_ALIGNED(pci_addr, region_size)) { |
| 1141 | dev_err(dev, |
| 1142 | "axi %pap or pci %pap not aligned\n", |
| 1143 | &axi_addr, &pci_addr); |
| 1144 | return -EINVAL; |
| 1145 | } |
| 1146 | |
| 1147 | /* Match found! Program IARR and all IMAP windows. */ |
| 1148 | ret = iproc_pcie_ib_write(pcie, region_idx, size_idx, |
| 1149 | ib_map->nr_windows, axi_addr, |
| 1150 | pci_addr, size); |
| 1151 | if (ret) |
| 1152 | goto err_ib; |
| 1153 | else |
| 1154 | return 0; |
| 1155 | |
| 1156 | } |
| 1157 | } |
| 1158 | ret = -EINVAL; |
| 1159 | |
| 1160 | err_ib: |
| 1161 | dev_err(dev, "unable to configure inbound mapping\n"); |
| 1162 | dev_err(dev, "axi %pap, pci %pap, res size %pap\n", |
| 1163 | &axi_addr, &pci_addr, &size); |
| 1164 | |
| 1165 | return ret; |
| 1166 | } |
| 1167 | |
Ray Jui | dd9d4e7 | 2016-10-31 17:38:39 -0700 | [diff] [blame] | 1168 | static int iproc_pcie_map_dma_ranges(struct iproc_pcie *pcie) |
| 1169 | { |
| 1170 | struct of_pci_range range; |
| 1171 | struct of_pci_range_parser parser; |
| 1172 | int ret; |
| 1173 | |
| 1174 | /* Get the dma-ranges from DT */ |
Marc Gonzalez | 1e61a57 | 2017-09-26 12:26:55 +0200 | [diff] [blame] | 1175 | ret = of_pci_dma_range_parser_init(&parser, pcie->dev->of_node); |
Ray Jui | dd9d4e7 | 2016-10-31 17:38:39 -0700 | [diff] [blame] | 1176 | if (ret) |
| 1177 | return ret; |
| 1178 | |
| 1179 | for_each_of_pci_range(&parser, &range) { |
| 1180 | /* Each range entry corresponds to an inbound mapping region */ |
| 1181 | ret = iproc_pcie_setup_ib(pcie, &range, IPROC_PCIE_IB_MAP_MEM); |
| 1182 | if (ret) |
| 1183 | return ret; |
| 1184 | } |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 1189 | static int iproce_pcie_get_msi(struct iproc_pcie *pcie, |
| 1190 | struct device_node *msi_node, |
| 1191 | u64 *msi_addr) |
| 1192 | { |
| 1193 | struct device *dev = pcie->dev; |
| 1194 | int ret; |
| 1195 | struct resource res; |
| 1196 | |
| 1197 | /* |
| 1198 | * Check if 'msi-map' points to ARM GICv3 ITS, which is the only |
| 1199 | * supported external MSI controller that requires steering. |
| 1200 | */ |
| 1201 | if (!of_device_is_compatible(msi_node, "arm,gic-v3-its")) { |
| 1202 | dev_err(dev, "unable to find compatible MSI controller\n"); |
| 1203 | return -ENODEV; |
| 1204 | } |
| 1205 | |
| 1206 | /* derive GITS_TRANSLATER address from GICv3 */ |
| 1207 | ret = of_address_to_resource(msi_node, 0, &res); |
| 1208 | if (ret < 0) { |
| 1209 | dev_err(dev, "unable to obtain MSI controller resources\n"); |
| 1210 | return ret; |
| 1211 | } |
| 1212 | |
| 1213 | *msi_addr = res.start + GITS_TRANSLATER; |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
Ray Jui | c7c4452 | 2016-10-31 17:38:41 -0700 | [diff] [blame] | 1217 | static int iproc_pcie_paxb_v2_msi_steer(struct iproc_pcie *pcie, u64 msi_addr) |
| 1218 | { |
| 1219 | int ret; |
| 1220 | struct of_pci_range range; |
| 1221 | |
| 1222 | memset(&range, 0, sizeof(range)); |
| 1223 | range.size = SZ_32K; |
Ray Jui | feacdb4 | 2016-11-21 17:48:30 -0800 | [diff] [blame] | 1224 | range.pci_addr = range.cpu_addr = msi_addr & ~(range.size - 1); |
Ray Jui | c7c4452 | 2016-10-31 17:38:41 -0700 | [diff] [blame] | 1225 | |
| 1226 | ret = iproc_pcie_setup_ib(pcie, &range, IPROC_PCIE_IB_MAP_IO); |
| 1227 | return ret; |
| 1228 | } |
| 1229 | |
Ray Jui | 1e5748c | 2018-06-11 17:21:05 -0700 | [diff] [blame] | 1230 | static void iproc_pcie_paxc_v2_msi_steer(struct iproc_pcie *pcie, u64 msi_addr, |
| 1231 | bool enable) |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 1232 | { |
| 1233 | u32 val; |
| 1234 | |
Ray Jui | 1e5748c | 2018-06-11 17:21:05 -0700 | [diff] [blame] | 1235 | if (!enable) { |
| 1236 | /* |
| 1237 | * Disable PAXC MSI steering. All write transfers will be |
| 1238 | * treated as non-MSI transfers |
| 1239 | */ |
| 1240 | val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_EN_CFG); |
| 1241 | val &= ~MSI_ENABLE_CFG; |
| 1242 | iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_EN_CFG, val); |
| 1243 | return; |
| 1244 | } |
| 1245 | |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 1246 | /* |
| 1247 | * Program bits [43:13] of address of GITS_TRANSLATER register into |
| 1248 | * bits [30:0] of the MSI base address register. In fact, in all iProc |
| 1249 | * based SoCs, all I/O register bases are well below the 32-bit |
| 1250 | * boundary, so we can safely assume bits [43:32] are always zeros. |
| 1251 | */ |
| 1252 | iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_BASE_ADDR, |
| 1253 | (u32)(msi_addr >> 13)); |
| 1254 | |
| 1255 | /* use a default 8K window size */ |
| 1256 | iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_WINDOW_SIZE, 0); |
| 1257 | |
| 1258 | /* steering MSI to GICv3 ITS */ |
| 1259 | val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_GIC_MODE); |
| 1260 | val |= GIC_V3_CFG; |
| 1261 | iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_GIC_MODE, val); |
| 1262 | |
| 1263 | /* |
| 1264 | * Program bits [43:2] of address of GITS_TRANSLATER register into the |
| 1265 | * iProc MSI address registers. |
| 1266 | */ |
| 1267 | msi_addr >>= 2; |
| 1268 | iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_ADDR_HI, |
| 1269 | upper_32_bits(msi_addr)); |
| 1270 | iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_ADDR_LO, |
| 1271 | lower_32_bits(msi_addr)); |
| 1272 | |
| 1273 | /* enable MSI */ |
| 1274 | val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_EN_CFG); |
| 1275 | val |= MSI_ENABLE_CFG; |
| 1276 | iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_EN_CFG, val); |
| 1277 | } |
| 1278 | |
| 1279 | static int iproc_pcie_msi_steer(struct iproc_pcie *pcie, |
| 1280 | struct device_node *msi_node) |
| 1281 | { |
| 1282 | struct device *dev = pcie->dev; |
| 1283 | int ret; |
| 1284 | u64 msi_addr; |
| 1285 | |
| 1286 | ret = iproce_pcie_get_msi(pcie, msi_node, &msi_addr); |
| 1287 | if (ret < 0) { |
| 1288 | dev_err(dev, "msi steering failed\n"); |
| 1289 | return ret; |
| 1290 | } |
| 1291 | |
| 1292 | switch (pcie->type) { |
Ray Jui | c7c4452 | 2016-10-31 17:38:41 -0700 | [diff] [blame] | 1293 | case IPROC_PCIE_PAXB_V2: |
| 1294 | ret = iproc_pcie_paxb_v2_msi_steer(pcie, msi_addr); |
| 1295 | if (ret) |
| 1296 | return ret; |
| 1297 | break; |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 1298 | case IPROC_PCIE_PAXC_V2: |
Ray Jui | 1e5748c | 2018-06-11 17:21:05 -0700 | [diff] [blame] | 1299 | iproc_pcie_paxc_v2_msi_steer(pcie, msi_addr, true); |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 1300 | break; |
| 1301 | default: |
| 1302 | return -EINVAL; |
| 1303 | } |
| 1304 | |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
Ray Jui | 3bc2b23 | 2016-01-06 18:04:35 -0600 | [diff] [blame] | 1308 | static int iproc_pcie_msi_enable(struct iproc_pcie *pcie) |
| 1309 | { |
| 1310 | struct device_node *msi_node; |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 1311 | int ret; |
| 1312 | |
| 1313 | /* |
| 1314 | * Either the "msi-parent" or the "msi-map" phandle needs to exist |
| 1315 | * for us to obtain the MSI node. |
| 1316 | */ |
Ray Jui | 3bc2b23 | 2016-01-06 18:04:35 -0600 | [diff] [blame] | 1317 | |
| 1318 | msi_node = of_parse_phandle(pcie->dev->of_node, "msi-parent", 0); |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 1319 | if (!msi_node) { |
| 1320 | const __be32 *msi_map = NULL; |
| 1321 | int len; |
| 1322 | u32 phandle; |
| 1323 | |
| 1324 | msi_map = of_get_property(pcie->dev->of_node, "msi-map", &len); |
| 1325 | if (!msi_map) |
| 1326 | return -ENODEV; |
| 1327 | |
| 1328 | phandle = be32_to_cpup(msi_map + 1); |
| 1329 | msi_node = of_find_node_by_phandle(phandle); |
| 1330 | if (!msi_node) |
| 1331 | return -ENODEV; |
| 1332 | } |
| 1333 | |
| 1334 | /* |
| 1335 | * Certain revisions of the iProc PCIe controller require additional |
| 1336 | * configurations to steer the MSI writes towards an external MSI |
| 1337 | * controller. |
| 1338 | */ |
| 1339 | if (pcie->need_msi_steer) { |
| 1340 | ret = iproc_pcie_msi_steer(pcie, msi_node); |
| 1341 | if (ret) |
| 1342 | return ret; |
| 1343 | } |
Ray Jui | 3bc2b23 | 2016-01-06 18:04:35 -0600 | [diff] [blame] | 1344 | |
| 1345 | /* |
| 1346 | * If another MSI controller is being used, the call below should fail |
| 1347 | * but that is okay |
| 1348 | */ |
| 1349 | return iproc_msi_init(pcie, msi_node); |
| 1350 | } |
| 1351 | |
| 1352 | static void iproc_pcie_msi_disable(struct iproc_pcie *pcie) |
| 1353 | { |
| 1354 | iproc_msi_exit(pcie); |
| 1355 | } |
| 1356 | |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 1357 | static int iproc_pcie_rev_init(struct iproc_pcie *pcie) |
| 1358 | { |
| 1359 | struct device *dev = pcie->dev; |
| 1360 | unsigned int reg_idx; |
| 1361 | const u16 *regs; |
| 1362 | |
| 1363 | switch (pcie->type) { |
Ray Jui | 404349c | 2016-10-31 17:38:32 -0700 | [diff] [blame] | 1364 | case IPROC_PCIE_PAXB_BCMA: |
| 1365 | regs = iproc_pcie_reg_paxb_bcma; |
| 1366 | break; |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 1367 | case IPROC_PCIE_PAXB: |
| 1368 | regs = iproc_pcie_reg_paxb; |
Ray Jui | f78e60a | 2018-06-11 17:21:06 -0700 | [diff] [blame] | 1369 | pcie->iproc_cfg_read = true; |
Ray Jui | 538928f | 2016-10-31 17:38:33 -0700 | [diff] [blame] | 1370 | pcie->has_apb_err_disable = true; |
Ray Jui | 4213e15 | 2016-10-31 17:38:37 -0700 | [diff] [blame] | 1371 | if (pcie->need_ob_cfg) { |
| 1372 | pcie->ob_map = paxb_ob_map; |
| 1373 | pcie->ob.nr_windows = ARRAY_SIZE(paxb_ob_map); |
| 1374 | } |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 1375 | break; |
Ray Jui | c7c4452 | 2016-10-31 17:38:41 -0700 | [diff] [blame] | 1376 | case IPROC_PCIE_PAXB_V2: |
| 1377 | regs = iproc_pcie_reg_paxb_v2; |
| 1378 | pcie->has_apb_err_disable = true; |
| 1379 | if (pcie->need_ob_cfg) { |
| 1380 | pcie->ob_map = paxb_v2_ob_map; |
| 1381 | pcie->ob.nr_windows = ARRAY_SIZE(paxb_v2_ob_map); |
| 1382 | } |
| 1383 | pcie->ib.nr_regions = ARRAY_SIZE(paxb_v2_ib_map); |
| 1384 | pcie->ib_map = paxb_v2_ib_map; |
| 1385 | pcie->need_msi_steer = true; |
Oza Pawandeep | 39b7a4f | 2017-08-28 16:43:30 -0500 | [diff] [blame] | 1386 | dev_warn(dev, "reads of config registers that contain %#x return incorrect data\n", |
| 1387 | CFG_RETRY_STATUS); |
Ray Jui | c7c4452 | 2016-10-31 17:38:41 -0700 | [diff] [blame] | 1388 | break; |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 1389 | case IPROC_PCIE_PAXC: |
| 1390 | regs = iproc_pcie_reg_paxc; |
| 1391 | pcie->ep_is_internal = true; |
Ray Jui | f78e60a | 2018-06-11 17:21:06 -0700 | [diff] [blame] | 1392 | pcie->iproc_cfg_read = true; |
| 1393 | pcie->rej_unconfig_pf = true; |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 1394 | break; |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 1395 | case IPROC_PCIE_PAXC_V2: |
| 1396 | regs = iproc_pcie_reg_paxc_v2; |
| 1397 | pcie->ep_is_internal = true; |
Ray Jui | f78e60a | 2018-06-11 17:21:06 -0700 | [diff] [blame] | 1398 | pcie->iproc_cfg_read = true; |
| 1399 | pcie->rej_unconfig_pf = true; |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 1400 | pcie->need_msi_steer = true; |
| 1401 | break; |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 1402 | default: |
| 1403 | dev_err(dev, "incompatible iProc PCIe interface\n"); |
| 1404 | return -EINVAL; |
| 1405 | } |
| 1406 | |
| 1407 | pcie->reg_offsets = devm_kcalloc(dev, IPROC_PCIE_MAX_NUM_REG, |
| 1408 | sizeof(*pcie->reg_offsets), |
| 1409 | GFP_KERNEL); |
| 1410 | if (!pcie->reg_offsets) |
| 1411 | return -ENOMEM; |
| 1412 | |
| 1413 | /* go through the register table and populate all valid registers */ |
Ray Jui | 787b3c4 | 2016-10-31 17:38:35 -0700 | [diff] [blame] | 1414 | pcie->reg_offsets[0] = (pcie->type == IPROC_PCIE_PAXC_V2) ? |
| 1415 | IPROC_PCIE_REG_INVALID : regs[0]; |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 1416 | for (reg_idx = 1; reg_idx < IPROC_PCIE_MAX_NUM_REG; reg_idx++) |
| 1417 | pcie->reg_offsets[reg_idx] = regs[reg_idx] ? |
| 1418 | regs[reg_idx] : IPROC_PCIE_REG_INVALID; |
| 1419 | |
| 1420 | return 0; |
| 1421 | } |
| 1422 | |
Hauke Mehrtens | 18c4342 | 2015-05-24 22:37:02 +0200 | [diff] [blame] | 1423 | int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res) |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1424 | { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 1425 | struct device *dev; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1426 | int ret; |
Lorenzo Pieralisi | 5277407 | 2017-06-28 15:13:57 -0500 | [diff] [blame] | 1427 | struct pci_bus *child; |
| 1428 | struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1429 | |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 1430 | dev = pcie->dev; |
Ray Jui | 06324ed | 2016-10-31 17:38:30 -0700 | [diff] [blame] | 1431 | |
| 1432 | ret = iproc_pcie_rev_init(pcie); |
| 1433 | if (ret) { |
| 1434 | dev_err(dev, "unable to initialize controller parameters\n"); |
| 1435 | return ret; |
| 1436 | } |
| 1437 | |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 1438 | ret = devm_request_pci_bus_resources(dev, res); |
Bjorn Helgaas | c3245a5 | 2016-05-28 18:22:24 -0500 | [diff] [blame] | 1439 | if (ret) |
| 1440 | return ret; |
| 1441 | |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 1442 | ret = phy_init(pcie->phy); |
| 1443 | if (ret) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 1444 | dev_err(dev, "unable to initialize PCIe PHY\n"); |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 1445 | return ret; |
| 1446 | } |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1447 | |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 1448 | ret = phy_power_on(pcie->phy); |
| 1449 | if (ret) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 1450 | dev_err(dev, "unable to power on PCIe PHY\n"); |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 1451 | goto err_exit_phy; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1452 | } |
| 1453 | |
Oza Pawandeep | b91c26c | 2017-08-28 16:43:35 -0500 | [diff] [blame] | 1454 | iproc_pcie_perst_ctrl(pcie, true); |
| 1455 | iproc_pcie_perst_ctrl(pcie, false); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1456 | |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 1457 | if (pcie->need_ob_cfg) { |
| 1458 | ret = iproc_pcie_map_ranges(pcie, res); |
| 1459 | if (ret) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 1460 | dev_err(dev, "map failed\n"); |
Ray Jui | e99a187 | 2015-10-16 08:18:24 -0500 | [diff] [blame] | 1461 | goto err_power_off_phy; |
| 1462 | } |
| 1463 | } |
| 1464 | |
Ray Jui | 3b65ca5 | 2018-01-11 12:36:16 -0800 | [diff] [blame] | 1465 | if (pcie->need_ib_cfg) { |
| 1466 | ret = iproc_pcie_map_dma_ranges(pcie); |
| 1467 | if (ret && ret != -ENOENT) |
| 1468 | goto err_power_off_phy; |
| 1469 | } |
Ray Jui | dd9d4e7 | 2016-10-31 17:38:39 -0700 | [diff] [blame] | 1470 | |
Lorenzo Pieralisi | 022adcf | 2017-06-28 15:13:50 -0500 | [diff] [blame] | 1471 | ret = iproc_pcie_check_link(pcie); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1472 | if (ret) { |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 1473 | dev_err(dev, "no PCIe EP device detected\n"); |
Lorenzo Pieralisi | 5277407 | 2017-06-28 15:13:57 -0500 | [diff] [blame] | 1474 | goto err_power_off_phy; |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | iproc_pcie_enable(pcie); |
| 1478 | |
Ray Jui | 3bc2b23 | 2016-01-06 18:04:35 -0600 | [diff] [blame] | 1479 | if (IS_ENABLED(CONFIG_PCI_MSI)) |
| 1480 | if (iproc_pcie_msi_enable(pcie)) |
Bjorn Helgaas | 786aecc | 2016-10-06 13:36:08 -0500 | [diff] [blame] | 1481 | dev_info(dev, "not using iProc MSI\n"); |
Ray Jui | 3bc2b23 | 2016-01-06 18:04:35 -0600 | [diff] [blame] | 1482 | |
Lorenzo Pieralisi | 5277407 | 2017-06-28 15:13:57 -0500 | [diff] [blame] | 1483 | list_splice_init(res, &host->windows); |
| 1484 | host->busnr = 0; |
| 1485 | host->dev.parent = dev; |
| 1486 | host->ops = &iproc_pcie_ops; |
Rob Herring | a1b363a | 2018-03-07 09:42:36 -0600 | [diff] [blame] | 1487 | host->sysdata = pcie; |
Lorenzo Pieralisi | 64bcd00 | 2017-06-28 15:14:07 -0500 | [diff] [blame] | 1488 | host->map_irq = pcie->map_irq; |
| 1489 | host->swizzle_irq = pci_common_swizzle; |
Lorenzo Pieralisi | 5277407 | 2017-06-28 15:13:57 -0500 | [diff] [blame] | 1490 | |
| 1491 | ret = pci_scan_root_bus_bridge(host); |
| 1492 | if (ret < 0) { |
| 1493 | dev_err(dev, "failed to scan host: %d\n", ret); |
| 1494 | goto err_power_off_phy; |
| 1495 | } |
Andy Gospodarek | ffbd796 | 2016-12-01 15:34:52 -0500 | [diff] [blame] | 1496 | |
Lorenzo Pieralisi | 5277407 | 2017-06-28 15:13:57 -0500 | [diff] [blame] | 1497 | pci_assign_unassigned_bus_resources(host->bus); |
| 1498 | |
| 1499 | pcie->root_bus = host->bus; |
| 1500 | |
| 1501 | list_for_each_entry(child, &host->bus->children, node) |
Jon Mason | 4d4836a | 2017-01-27 16:44:08 -0500 | [diff] [blame] | 1502 | pcie_bus_configure_settings(child); |
| 1503 | |
Lorenzo Pieralisi | 5277407 | 2017-06-28 15:13:57 -0500 | [diff] [blame] | 1504 | pci_bus_add_devices(host->bus); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1505 | |
| 1506 | return 0; |
| 1507 | |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1508 | err_power_off_phy: |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 1509 | phy_power_off(pcie->phy); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1510 | err_exit_phy: |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 1511 | phy_exit(pcie->phy); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1512 | return ret; |
| 1513 | } |
| 1514 | EXPORT_SYMBOL(iproc_pcie_setup); |
| 1515 | |
| 1516 | int iproc_pcie_remove(struct iproc_pcie *pcie) |
| 1517 | { |
| 1518 | pci_stop_root_bus(pcie->root_bus); |
| 1519 | pci_remove_root_bus(pcie->root_bus); |
| 1520 | |
Ray Jui | 3bc2b23 | 2016-01-06 18:04:35 -0600 | [diff] [blame] | 1521 | iproc_pcie_msi_disable(pcie); |
| 1522 | |
Markus Elfring | 93972d1 | 2015-06-28 16:42:04 +0200 | [diff] [blame] | 1523 | phy_power_off(pcie->phy); |
| 1524 | phy_exit(pcie->phy); |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1525 | |
| 1526 | return 0; |
| 1527 | } |
| 1528 | EXPORT_SYMBOL(iproc_pcie_remove); |
| 1529 | |
Ray Jui | 1e5748c | 2018-06-11 17:21:05 -0700 | [diff] [blame] | 1530 | /* |
| 1531 | * The MSI parsing logic in certain revisions of Broadcom PAXC based root |
| 1532 | * complex does not work and needs to be disabled |
| 1533 | */ |
| 1534 | static void quirk_paxc_disable_msi_parsing(struct pci_dev *pdev) |
| 1535 | { |
| 1536 | struct iproc_pcie *pcie = iproc_data(pdev->bus); |
| 1537 | |
| 1538 | if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE) |
| 1539 | iproc_pcie_paxc_v2_msi_steer(pcie, 0, false); |
| 1540 | } |
| 1541 | DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0x16f0, |
| 1542 | quirk_paxc_disable_msi_parsing); |
| 1543 | DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd802, |
| 1544 | quirk_paxc_disable_msi_parsing); |
| 1545 | DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd804, |
| 1546 | quirk_paxc_disable_msi_parsing); |
| 1547 | |
Ray Jui | 1fb37a8 | 2015-04-08 11:21:35 -0700 | [diff] [blame] | 1548 | MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>"); |
| 1549 | MODULE_DESCRIPTION("Broadcom iPROC PCIe common driver"); |
| 1550 | MODULE_LICENSE("GPL v2"); |