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