Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2014 Hauke Mehrtens <hauke@hauke-m.de>
0004  * Copyright (C) 2015 Broadcom Corporation
0005  */
0006 
0007 #include <linux/kernel.h>
0008 #include <linux/pci.h>
0009 #include <linux/pci-ecam.h>
0010 #include <linux/msi.h>
0011 #include <linux/clk.h>
0012 #include <linux/module.h>
0013 #include <linux/mbus.h>
0014 #include <linux/slab.h>
0015 #include <linux/delay.h>
0016 #include <linux/interrupt.h>
0017 #include <linux/irqchip/arm-gic-v3.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/of_address.h>
0020 #include <linux/of_pci.h>
0021 #include <linux/of_irq.h>
0022 #include <linux/of_platform.h>
0023 #include <linux/phy/phy.h>
0024 
0025 #include "pcie-iproc.h"
0026 
0027 #define EP_PERST_SOURCE_SELECT_SHIFT    2
0028 #define EP_PERST_SOURCE_SELECT      BIT(EP_PERST_SOURCE_SELECT_SHIFT)
0029 #define EP_MODE_SURVIVE_PERST_SHIFT 1
0030 #define EP_MODE_SURVIVE_PERST       BIT(EP_MODE_SURVIVE_PERST_SHIFT)
0031 #define RC_PCIE_RST_OUTPUT_SHIFT    0
0032 #define RC_PCIE_RST_OUTPUT      BIT(RC_PCIE_RST_OUTPUT_SHIFT)
0033 #define PAXC_RESET_MASK         0x7f
0034 
0035 #define GIC_V3_CFG_SHIFT        0
0036 #define GIC_V3_CFG          BIT(GIC_V3_CFG_SHIFT)
0037 
0038 #define MSI_ENABLE_CFG_SHIFT        0
0039 #define MSI_ENABLE_CFG          BIT(MSI_ENABLE_CFG_SHIFT)
0040 
0041 #define CFG_IND_ADDR_MASK       0x00001ffc
0042 
0043 #define CFG_ADDR_REG_NUM_MASK       0x00000ffc
0044 #define CFG_ADDR_CFG_TYPE_1     1
0045 
0046 #define SYS_RC_INTX_MASK        0xf
0047 
0048 #define PCIE_PHYLINKUP_SHIFT        3
0049 #define PCIE_PHYLINKUP          BIT(PCIE_PHYLINKUP_SHIFT)
0050 #define PCIE_DL_ACTIVE_SHIFT        2
0051 #define PCIE_DL_ACTIVE          BIT(PCIE_DL_ACTIVE_SHIFT)
0052 
0053 #define APB_ERR_EN_SHIFT        0
0054 #define APB_ERR_EN          BIT(APB_ERR_EN_SHIFT)
0055 
0056 #define CFG_RD_SUCCESS          0
0057 #define CFG_RD_UR           1
0058 #define CFG_RD_CRS          2
0059 #define CFG_RD_CA           3
0060 #define CFG_RETRY_STATUS        0xffff0001
0061 #define CFG_RETRY_STATUS_TIMEOUT_US 500000 /* 500 milliseconds */
0062 
0063 /* derive the enum index of the outbound/inbound mapping registers */
0064 #define MAP_REG(base_reg, index)    ((base_reg) + (index) * 2)
0065 
0066 /*
0067  * Maximum number of outbound mapping window sizes that can be supported by any
0068  * OARR/OMAP mapping pair
0069  */
0070 #define MAX_NUM_OB_WINDOW_SIZES     4
0071 
0072 #define OARR_VALID_SHIFT        0
0073 #define OARR_VALID          BIT(OARR_VALID_SHIFT)
0074 #define OARR_SIZE_CFG_SHIFT     1
0075 
0076 /*
0077  * Maximum number of inbound mapping region sizes that can be supported by an
0078  * IARR
0079  */
0080 #define MAX_NUM_IB_REGION_SIZES     9
0081 
0082 #define IMAP_VALID_SHIFT        0
0083 #define IMAP_VALID          BIT(IMAP_VALID_SHIFT)
0084 
0085 #define IPROC_PCI_PM_CAP        0x48
0086 #define IPROC_PCI_PM_CAP_MASK       0xffff
0087 #define IPROC_PCI_EXP_CAP       0xac
0088 
0089 #define IPROC_PCIE_REG_INVALID      0xffff
0090 
0091 /**
0092  * struct iproc_pcie_ob_map - iProc PCIe outbound mapping controller-specific
0093  * parameters
0094  * @window_sizes: list of supported outbound mapping window sizes in MB
0095  * @nr_sizes: number of supported outbound mapping window sizes
0096  */
0097 struct iproc_pcie_ob_map {
0098     resource_size_t window_sizes[MAX_NUM_OB_WINDOW_SIZES];
0099     unsigned int nr_sizes;
0100 };
0101 
0102 static const struct iproc_pcie_ob_map paxb_ob_map[] = {
0103     {
0104         /* OARR0/OMAP0 */
0105         .window_sizes = { 128, 256 },
0106         .nr_sizes = 2,
0107     },
0108     {
0109         /* OARR1/OMAP1 */
0110         .window_sizes = { 128, 256 },
0111         .nr_sizes = 2,
0112     },
0113 };
0114 
0115 static const struct iproc_pcie_ob_map paxb_v2_ob_map[] = {
0116     {
0117         /* OARR0/OMAP0 */
0118         .window_sizes = { 128, 256 },
0119         .nr_sizes = 2,
0120     },
0121     {
0122         /* OARR1/OMAP1 */
0123         .window_sizes = { 128, 256 },
0124         .nr_sizes = 2,
0125     },
0126     {
0127         /* OARR2/OMAP2 */
0128         .window_sizes = { 128, 256, 512, 1024 },
0129         .nr_sizes = 4,
0130     },
0131     {
0132         /* OARR3/OMAP3 */
0133         .window_sizes = { 128, 256, 512, 1024 },
0134         .nr_sizes = 4,
0135     },
0136 };
0137 
0138 /**
0139  * enum iproc_pcie_ib_map_type - iProc PCIe inbound mapping type
0140  * @IPROC_PCIE_IB_MAP_MEM: DDR memory
0141  * @IPROC_PCIE_IB_MAP_IO: device I/O memory
0142  * @IPROC_PCIE_IB_MAP_INVALID: invalid or unused
0143  */
0144 enum iproc_pcie_ib_map_type {
0145     IPROC_PCIE_IB_MAP_MEM = 0,
0146     IPROC_PCIE_IB_MAP_IO,
0147     IPROC_PCIE_IB_MAP_INVALID
0148 };
0149 
0150 /**
0151  * struct iproc_pcie_ib_map - iProc PCIe inbound mapping controller-specific
0152  * parameters
0153  * @type: inbound mapping region type
0154  * @size_unit: inbound mapping region size unit, could be SZ_1K, SZ_1M, or
0155  * SZ_1G
0156  * @region_sizes: list of supported inbound mapping region sizes in KB, MB, or
0157  * GB, depending on the size unit
0158  * @nr_sizes: number of supported inbound mapping region sizes
0159  * @nr_windows: number of supported inbound mapping windows for the region
0160  * @imap_addr_offset: register offset between the upper and lower 32-bit
0161  * IMAP address registers
0162  * @imap_window_offset: register offset between each IMAP window
0163  */
0164 struct iproc_pcie_ib_map {
0165     enum iproc_pcie_ib_map_type type;
0166     unsigned int size_unit;
0167     resource_size_t region_sizes[MAX_NUM_IB_REGION_SIZES];
0168     unsigned int nr_sizes;
0169     unsigned int nr_windows;
0170     u16 imap_addr_offset;
0171     u16 imap_window_offset;
0172 };
0173 
0174 static const struct iproc_pcie_ib_map paxb_v2_ib_map[] = {
0175     {
0176         /* IARR0/IMAP0 */
0177         .type = IPROC_PCIE_IB_MAP_IO,
0178         .size_unit = SZ_1K,
0179         .region_sizes = { 32 },
0180         .nr_sizes = 1,
0181         .nr_windows = 8,
0182         .imap_addr_offset = 0x40,
0183         .imap_window_offset = 0x4,
0184     },
0185     {
0186         /* IARR1/IMAP1 */
0187         .type = IPROC_PCIE_IB_MAP_MEM,
0188         .size_unit = SZ_1M,
0189         .region_sizes = { 8 },
0190         .nr_sizes = 1,
0191         .nr_windows = 8,
0192         .imap_addr_offset = 0x4,
0193         .imap_window_offset = 0x8,
0194 
0195     },
0196     {
0197         /* IARR2/IMAP2 */
0198         .type = IPROC_PCIE_IB_MAP_MEM,
0199         .size_unit = SZ_1M,
0200         .region_sizes = { 64, 128, 256, 512, 1024, 2048, 4096, 8192,
0201                   16384 },
0202         .nr_sizes = 9,
0203         .nr_windows = 1,
0204         .imap_addr_offset = 0x4,
0205         .imap_window_offset = 0x8,
0206     },
0207     {
0208         /* IARR3/IMAP3 */
0209         .type = IPROC_PCIE_IB_MAP_MEM,
0210         .size_unit = SZ_1G,
0211         .region_sizes = { 1, 2, 4, 8, 16, 32 },
0212         .nr_sizes = 6,
0213         .nr_windows = 8,
0214         .imap_addr_offset = 0x4,
0215         .imap_window_offset = 0x8,
0216     },
0217     {
0218         /* IARR4/IMAP4 */
0219         .type = IPROC_PCIE_IB_MAP_MEM,
0220         .size_unit = SZ_1G,
0221         .region_sizes = { 32, 64, 128, 256, 512 },
0222         .nr_sizes = 5,
0223         .nr_windows = 8,
0224         .imap_addr_offset = 0x4,
0225         .imap_window_offset = 0x8,
0226     },
0227 };
0228 
0229 /*
0230  * iProc PCIe host registers
0231  */
0232 enum iproc_pcie_reg {
0233     /* clock/reset signal control */
0234     IPROC_PCIE_CLK_CTRL = 0,
0235 
0236     /*
0237      * To allow MSI to be steered to an external MSI controller (e.g., ARM
0238      * GICv3 ITS)
0239      */
0240     IPROC_PCIE_MSI_GIC_MODE,
0241 
0242     /*
0243      * IPROC_PCIE_MSI_BASE_ADDR and IPROC_PCIE_MSI_WINDOW_SIZE define the
0244      * window where the MSI posted writes are written, for the writes to be
0245      * interpreted as MSI writes.
0246      */
0247     IPROC_PCIE_MSI_BASE_ADDR,
0248     IPROC_PCIE_MSI_WINDOW_SIZE,
0249 
0250     /*
0251      * To hold the address of the register where the MSI writes are
0252      * programmed.  When ARM GICv3 ITS is used, this should be programmed
0253      * with the address of the GITS_TRANSLATER register.
0254      */
0255     IPROC_PCIE_MSI_ADDR_LO,
0256     IPROC_PCIE_MSI_ADDR_HI,
0257 
0258     /* enable MSI */
0259     IPROC_PCIE_MSI_EN_CFG,
0260 
0261     /* allow access to root complex configuration space */
0262     IPROC_PCIE_CFG_IND_ADDR,
0263     IPROC_PCIE_CFG_IND_DATA,
0264 
0265     /* allow access to device configuration space */
0266     IPROC_PCIE_CFG_ADDR,
0267     IPROC_PCIE_CFG_DATA,
0268 
0269     /* enable INTx */
0270     IPROC_PCIE_INTX_EN,
0271 
0272     /* outbound address mapping */
0273     IPROC_PCIE_OARR0,
0274     IPROC_PCIE_OMAP0,
0275     IPROC_PCIE_OARR1,
0276     IPROC_PCIE_OMAP1,
0277     IPROC_PCIE_OARR2,
0278     IPROC_PCIE_OMAP2,
0279     IPROC_PCIE_OARR3,
0280     IPROC_PCIE_OMAP3,
0281 
0282     /* inbound address mapping */
0283     IPROC_PCIE_IARR0,
0284     IPROC_PCIE_IMAP0,
0285     IPROC_PCIE_IARR1,
0286     IPROC_PCIE_IMAP1,
0287     IPROC_PCIE_IARR2,
0288     IPROC_PCIE_IMAP2,
0289     IPROC_PCIE_IARR3,
0290     IPROC_PCIE_IMAP3,
0291     IPROC_PCIE_IARR4,
0292     IPROC_PCIE_IMAP4,
0293 
0294     /* config read status */
0295     IPROC_PCIE_CFG_RD_STATUS,
0296 
0297     /* link status */
0298     IPROC_PCIE_LINK_STATUS,
0299 
0300     /* enable APB error for unsupported requests */
0301     IPROC_PCIE_APB_ERR_EN,
0302 
0303     /* total number of core registers */
0304     IPROC_PCIE_MAX_NUM_REG,
0305 };
0306 
0307 /* iProc PCIe PAXB BCMA registers */
0308 static const u16 iproc_pcie_reg_paxb_bcma[IPROC_PCIE_MAX_NUM_REG] = {
0309     [IPROC_PCIE_CLK_CTRL]       = 0x000,
0310     [IPROC_PCIE_CFG_IND_ADDR]   = 0x120,
0311     [IPROC_PCIE_CFG_IND_DATA]   = 0x124,
0312     [IPROC_PCIE_CFG_ADDR]       = 0x1f8,
0313     [IPROC_PCIE_CFG_DATA]       = 0x1fc,
0314     [IPROC_PCIE_INTX_EN]        = 0x330,
0315     [IPROC_PCIE_LINK_STATUS]    = 0xf0c,
0316 };
0317 
0318 /* iProc PCIe PAXB registers */
0319 static const u16 iproc_pcie_reg_paxb[IPROC_PCIE_MAX_NUM_REG] = {
0320     [IPROC_PCIE_CLK_CTRL]       = 0x000,
0321     [IPROC_PCIE_CFG_IND_ADDR]   = 0x120,
0322     [IPROC_PCIE_CFG_IND_DATA]   = 0x124,
0323     [IPROC_PCIE_CFG_ADDR]       = 0x1f8,
0324     [IPROC_PCIE_CFG_DATA]       = 0x1fc,
0325     [IPROC_PCIE_INTX_EN]        = 0x330,
0326     [IPROC_PCIE_OARR0]      = 0xd20,
0327     [IPROC_PCIE_OMAP0]      = 0xd40,
0328     [IPROC_PCIE_OARR1]      = 0xd28,
0329     [IPROC_PCIE_OMAP1]      = 0xd48,
0330     [IPROC_PCIE_LINK_STATUS]    = 0xf0c,
0331     [IPROC_PCIE_APB_ERR_EN]     = 0xf40,
0332 };
0333 
0334 /* iProc PCIe PAXB v2 registers */
0335 static const u16 iproc_pcie_reg_paxb_v2[IPROC_PCIE_MAX_NUM_REG] = {
0336     [IPROC_PCIE_CLK_CTRL]       = 0x000,
0337     [IPROC_PCIE_CFG_IND_ADDR]   = 0x120,
0338     [IPROC_PCIE_CFG_IND_DATA]   = 0x124,
0339     [IPROC_PCIE_CFG_ADDR]       = 0x1f8,
0340     [IPROC_PCIE_CFG_DATA]       = 0x1fc,
0341     [IPROC_PCIE_INTX_EN]        = 0x330,
0342     [IPROC_PCIE_OARR0]      = 0xd20,
0343     [IPROC_PCIE_OMAP0]      = 0xd40,
0344     [IPROC_PCIE_OARR1]      = 0xd28,
0345     [IPROC_PCIE_OMAP1]      = 0xd48,
0346     [IPROC_PCIE_OARR2]      = 0xd60,
0347     [IPROC_PCIE_OMAP2]      = 0xd68,
0348     [IPROC_PCIE_OARR3]      = 0xdf0,
0349     [IPROC_PCIE_OMAP3]      = 0xdf8,
0350     [IPROC_PCIE_IARR0]      = 0xd00,
0351     [IPROC_PCIE_IMAP0]      = 0xc00,
0352     [IPROC_PCIE_IARR1]      = 0xd08,
0353     [IPROC_PCIE_IMAP1]      = 0xd70,
0354     [IPROC_PCIE_IARR2]      = 0xd10,
0355     [IPROC_PCIE_IMAP2]      = 0xcc0,
0356     [IPROC_PCIE_IARR3]      = 0xe00,
0357     [IPROC_PCIE_IMAP3]      = 0xe08,
0358     [IPROC_PCIE_IARR4]      = 0xe68,
0359     [IPROC_PCIE_IMAP4]      = 0xe70,
0360     [IPROC_PCIE_CFG_RD_STATUS]  = 0xee0,
0361     [IPROC_PCIE_LINK_STATUS]    = 0xf0c,
0362     [IPROC_PCIE_APB_ERR_EN]     = 0xf40,
0363 };
0364 
0365 /* iProc PCIe PAXC v1 registers */
0366 static const u16 iproc_pcie_reg_paxc[IPROC_PCIE_MAX_NUM_REG] = {
0367     [IPROC_PCIE_CLK_CTRL]       = 0x000,
0368     [IPROC_PCIE_CFG_IND_ADDR]   = 0x1f0,
0369     [IPROC_PCIE_CFG_IND_DATA]   = 0x1f4,
0370     [IPROC_PCIE_CFG_ADDR]       = 0x1f8,
0371     [IPROC_PCIE_CFG_DATA]       = 0x1fc,
0372 };
0373 
0374 /* iProc PCIe PAXC v2 registers */
0375 static const u16 iproc_pcie_reg_paxc_v2[IPROC_PCIE_MAX_NUM_REG] = {
0376     [IPROC_PCIE_MSI_GIC_MODE]   = 0x050,
0377     [IPROC_PCIE_MSI_BASE_ADDR]  = 0x074,
0378     [IPROC_PCIE_MSI_WINDOW_SIZE]    = 0x078,
0379     [IPROC_PCIE_MSI_ADDR_LO]    = 0x07c,
0380     [IPROC_PCIE_MSI_ADDR_HI]    = 0x080,
0381     [IPROC_PCIE_MSI_EN_CFG]     = 0x09c,
0382     [IPROC_PCIE_CFG_IND_ADDR]   = 0x1f0,
0383     [IPROC_PCIE_CFG_IND_DATA]   = 0x1f4,
0384     [IPROC_PCIE_CFG_ADDR]       = 0x1f8,
0385     [IPROC_PCIE_CFG_DATA]       = 0x1fc,
0386 };
0387 
0388 /*
0389  * List of device IDs of controllers that have corrupted capability list that
0390  * require SW fixup
0391  */
0392 static const u16 iproc_pcie_corrupt_cap_did[] = {
0393     0x16cd,
0394     0x16f0,
0395     0xd802,
0396     0xd804
0397 };
0398 
0399 static inline struct iproc_pcie *iproc_data(struct pci_bus *bus)
0400 {
0401     struct iproc_pcie *pcie = bus->sysdata;
0402     return pcie;
0403 }
0404 
0405 static inline bool iproc_pcie_reg_is_invalid(u16 reg_offset)
0406 {
0407     return !!(reg_offset == IPROC_PCIE_REG_INVALID);
0408 }
0409 
0410 static inline u16 iproc_pcie_reg_offset(struct iproc_pcie *pcie,
0411                     enum iproc_pcie_reg reg)
0412 {
0413     return pcie->reg_offsets[reg];
0414 }
0415 
0416 static inline u32 iproc_pcie_read_reg(struct iproc_pcie *pcie,
0417                       enum iproc_pcie_reg reg)
0418 {
0419     u16 offset = iproc_pcie_reg_offset(pcie, reg);
0420 
0421     if (iproc_pcie_reg_is_invalid(offset))
0422         return 0;
0423 
0424     return readl(pcie->base + offset);
0425 }
0426 
0427 static inline void iproc_pcie_write_reg(struct iproc_pcie *pcie,
0428                     enum iproc_pcie_reg reg, u32 val)
0429 {
0430     u16 offset = iproc_pcie_reg_offset(pcie, reg);
0431 
0432     if (iproc_pcie_reg_is_invalid(offset))
0433         return;
0434 
0435     writel(val, pcie->base + offset);
0436 }
0437 
0438 /*
0439  * APB error forwarding can be disabled during access of configuration
0440  * registers of the endpoint device, to prevent unsupported requests
0441  * (typically seen during enumeration with multi-function devices) from
0442  * triggering a system exception.
0443  */
0444 static inline void iproc_pcie_apb_err_disable(struct pci_bus *bus,
0445                           bool disable)
0446 {
0447     struct iproc_pcie *pcie = iproc_data(bus);
0448     u32 val;
0449 
0450     if (bus->number && pcie->has_apb_err_disable) {
0451         val = iproc_pcie_read_reg(pcie, IPROC_PCIE_APB_ERR_EN);
0452         if (disable)
0453             val &= ~APB_ERR_EN;
0454         else
0455             val |= APB_ERR_EN;
0456         iproc_pcie_write_reg(pcie, IPROC_PCIE_APB_ERR_EN, val);
0457     }
0458 }
0459 
0460 static void __iomem *iproc_pcie_map_ep_cfg_reg(struct iproc_pcie *pcie,
0461                            unsigned int busno,
0462                            unsigned int devfn,
0463                            int where)
0464 {
0465     u16 offset;
0466     u32 val;
0467 
0468     /* EP device access */
0469     val = ALIGN_DOWN(PCIE_ECAM_OFFSET(busno, devfn, where), 4) |
0470         CFG_ADDR_CFG_TYPE_1;
0471 
0472     iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_ADDR, val);
0473     offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_DATA);
0474 
0475     if (iproc_pcie_reg_is_invalid(offset))
0476         return NULL;
0477 
0478     return (pcie->base + offset);
0479 }
0480 
0481 static unsigned int iproc_pcie_cfg_retry(struct iproc_pcie *pcie,
0482                      void __iomem *cfg_data_p)
0483 {
0484     int timeout = CFG_RETRY_STATUS_TIMEOUT_US;
0485     unsigned int data;
0486     u32 status;
0487 
0488     /*
0489      * As per PCIe spec r3.1, sec 2.3.2, CRS Software Visibility only
0490      * affects config reads of the Vendor ID.  For config writes or any
0491      * other config reads, the Root may automatically reissue the
0492      * configuration request again as a new request.
0493      *
0494      * For config reads, this hardware returns CFG_RETRY_STATUS data
0495      * when it receives a CRS completion, regardless of the address of
0496      * the read or the CRS Software Visibility Enable bit.  As a
0497      * partial workaround for this, we retry in software any read that
0498      * returns CFG_RETRY_STATUS.
0499      *
0500      * Note that a non-Vendor ID config register may have a value of
0501      * CFG_RETRY_STATUS.  If we read that, we can't distinguish it from
0502      * a CRS completion, so we will incorrectly retry the read and
0503      * eventually return the wrong data (0xffffffff).
0504      */
0505     data = readl(cfg_data_p);
0506     while (data == CFG_RETRY_STATUS && timeout--) {
0507         /*
0508          * CRS state is set in CFG_RD status register
0509          * This will handle the case where CFG_RETRY_STATUS is
0510          * valid config data.
0511          */
0512         status = iproc_pcie_read_reg(pcie, IPROC_PCIE_CFG_RD_STATUS);
0513         if (status != CFG_RD_CRS)
0514             return data;
0515 
0516         udelay(1);
0517         data = readl(cfg_data_p);
0518     }
0519 
0520     if (data == CFG_RETRY_STATUS)
0521         data = 0xffffffff;
0522 
0523     return data;
0524 }
0525 
0526 static void iproc_pcie_fix_cap(struct iproc_pcie *pcie, int where, u32 *val)
0527 {
0528     u32 i, dev_id;
0529 
0530     switch (where & ~0x3) {
0531     case PCI_VENDOR_ID:
0532         dev_id = *val >> 16;
0533 
0534         /*
0535          * Activate fixup for those controllers that have corrupted
0536          * capability list registers
0537          */
0538         for (i = 0; i < ARRAY_SIZE(iproc_pcie_corrupt_cap_did); i++)
0539             if (dev_id == iproc_pcie_corrupt_cap_did[i])
0540                 pcie->fix_paxc_cap = true;
0541         break;
0542 
0543     case IPROC_PCI_PM_CAP:
0544         if (pcie->fix_paxc_cap) {
0545             /* advertise PM, force next capability to PCIe */
0546             *val &= ~IPROC_PCI_PM_CAP_MASK;
0547             *val |= IPROC_PCI_EXP_CAP << 8 | PCI_CAP_ID_PM;
0548         }
0549         break;
0550 
0551     case IPROC_PCI_EXP_CAP:
0552         if (pcie->fix_paxc_cap) {
0553             /* advertise root port, version 2, terminate here */
0554             *val = (PCI_EXP_TYPE_ROOT_PORT << 4 | 2) << 16 |
0555                 PCI_CAP_ID_EXP;
0556         }
0557         break;
0558 
0559     case IPROC_PCI_EXP_CAP + PCI_EXP_RTCTL:
0560         /* Don't advertise CRS SV support */
0561         *val &= ~(PCI_EXP_RTCAP_CRSVIS << 16);
0562         break;
0563 
0564     default:
0565         break;
0566     }
0567 }
0568 
0569 static int iproc_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
0570                   int where, int size, u32 *val)
0571 {
0572     struct iproc_pcie *pcie = iproc_data(bus);
0573     unsigned int busno = bus->number;
0574     void __iomem *cfg_data_p;
0575     unsigned int data;
0576     int ret;
0577 
0578     /* root complex access */
0579     if (busno == 0) {
0580         ret = pci_generic_config_read32(bus, devfn, where, size, val);
0581         if (ret == PCIBIOS_SUCCESSFUL)
0582             iproc_pcie_fix_cap(pcie, where, val);
0583 
0584         return ret;
0585     }
0586 
0587     cfg_data_p = iproc_pcie_map_ep_cfg_reg(pcie, busno, devfn, where);
0588 
0589     if (!cfg_data_p)
0590         return PCIBIOS_DEVICE_NOT_FOUND;
0591 
0592     data = iproc_pcie_cfg_retry(pcie, cfg_data_p);
0593 
0594     *val = data;
0595     if (size <= 2)
0596         *val = (data >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
0597 
0598     /*
0599      * For PAXC and PAXCv2, the total number of PFs that one can enumerate
0600      * depends on the firmware configuration. Unfortunately, due to an ASIC
0601      * bug, unconfigured PFs cannot be properly hidden from the root
0602      * complex. As a result, write access to these PFs will cause bus lock
0603      * up on the embedded processor
0604      *
0605      * Since all unconfigured PFs are left with an incorrect, staled device
0606      * ID of 0x168e (PCI_DEVICE_ID_NX2_57810), we try to catch those access
0607      * early here and reject them all
0608      */
0609 #define DEVICE_ID_MASK     0xffff0000
0610 #define DEVICE_ID_SHIFT    16
0611     if (pcie->rej_unconfig_pf &&
0612         (where & CFG_ADDR_REG_NUM_MASK) == PCI_VENDOR_ID)
0613         if ((*val & DEVICE_ID_MASK) ==
0614             (PCI_DEVICE_ID_NX2_57810 << DEVICE_ID_SHIFT))
0615             return PCIBIOS_FUNC_NOT_SUPPORTED;
0616 
0617     return PCIBIOS_SUCCESSFUL;
0618 }
0619 
0620 /*
0621  * Note access to the configuration registers are protected at the higher layer
0622  * by 'pci_lock' in drivers/pci/access.c
0623  */
0624 static void __iomem *iproc_pcie_map_cfg_bus(struct iproc_pcie *pcie,
0625                         int busno, unsigned int devfn,
0626                         int where)
0627 {
0628     u16 offset;
0629 
0630     /* root complex access */
0631     if (busno == 0) {
0632         if (PCIE_ECAM_DEVFN(devfn) > 0)
0633             return NULL;
0634 
0635         iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_IND_ADDR,
0636                      where & CFG_IND_ADDR_MASK);
0637         offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_IND_DATA);
0638         if (iproc_pcie_reg_is_invalid(offset))
0639             return NULL;
0640         else
0641             return (pcie->base + offset);
0642     }
0643 
0644     return iproc_pcie_map_ep_cfg_reg(pcie, busno, devfn, where);
0645 }
0646 
0647 static void __iomem *iproc_pcie_bus_map_cfg_bus(struct pci_bus *bus,
0648                         unsigned int devfn,
0649                         int where)
0650 {
0651     return iproc_pcie_map_cfg_bus(iproc_data(bus), bus->number, devfn,
0652                       where);
0653 }
0654 
0655 static int iproc_pci_raw_config_read32(struct iproc_pcie *pcie,
0656                        unsigned int devfn, int where,
0657                        int size, u32 *val)
0658 {
0659     void __iomem *addr;
0660 
0661     addr = iproc_pcie_map_cfg_bus(pcie, 0, devfn, where & ~0x3);
0662     if (!addr)
0663         return PCIBIOS_DEVICE_NOT_FOUND;
0664 
0665     *val = readl(addr);
0666 
0667     if (size <= 2)
0668         *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
0669 
0670     return PCIBIOS_SUCCESSFUL;
0671 }
0672 
0673 static int iproc_pci_raw_config_write32(struct iproc_pcie *pcie,
0674                     unsigned int devfn, int where,
0675                     int size, u32 val)
0676 {
0677     void __iomem *addr;
0678     u32 mask, tmp;
0679 
0680     addr = iproc_pcie_map_cfg_bus(pcie, 0, devfn, where & ~0x3);
0681     if (!addr)
0682         return PCIBIOS_DEVICE_NOT_FOUND;
0683 
0684     if (size == 4) {
0685         writel(val, addr);
0686         return PCIBIOS_SUCCESSFUL;
0687     }
0688 
0689     mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
0690     tmp = readl(addr) & mask;
0691     tmp |= val << ((where & 0x3) * 8);
0692     writel(tmp, addr);
0693 
0694     return PCIBIOS_SUCCESSFUL;
0695 }
0696 
0697 static int iproc_pcie_config_read32(struct pci_bus *bus, unsigned int devfn,
0698                     int where, int size, u32 *val)
0699 {
0700     int ret;
0701     struct iproc_pcie *pcie = iproc_data(bus);
0702 
0703     iproc_pcie_apb_err_disable(bus, true);
0704     if (pcie->iproc_cfg_read)
0705         ret = iproc_pcie_config_read(bus, devfn, where, size, val);
0706     else
0707         ret = pci_generic_config_read32(bus, devfn, where, size, val);
0708     iproc_pcie_apb_err_disable(bus, false);
0709 
0710     return ret;
0711 }
0712 
0713 static int iproc_pcie_config_write32(struct pci_bus *bus, unsigned int devfn,
0714                      int where, int size, u32 val)
0715 {
0716     int ret;
0717 
0718     iproc_pcie_apb_err_disable(bus, true);
0719     ret = pci_generic_config_write32(bus, devfn, where, size, val);
0720     iproc_pcie_apb_err_disable(bus, false);
0721 
0722     return ret;
0723 }
0724 
0725 static struct pci_ops iproc_pcie_ops = {
0726     .map_bus = iproc_pcie_bus_map_cfg_bus,
0727     .read = iproc_pcie_config_read32,
0728     .write = iproc_pcie_config_write32,
0729 };
0730 
0731 static void iproc_pcie_perst_ctrl(struct iproc_pcie *pcie, bool assert)
0732 {
0733     u32 val;
0734 
0735     /*
0736      * PAXC and the internal emulated endpoint device downstream should not
0737      * be reset.  If firmware has been loaded on the endpoint device at an
0738      * earlier boot stage, reset here causes issues.
0739      */
0740     if (pcie->ep_is_internal)
0741         return;
0742 
0743     if (assert) {
0744         val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL);
0745         val &= ~EP_PERST_SOURCE_SELECT & ~EP_MODE_SURVIVE_PERST &
0746             ~RC_PCIE_RST_OUTPUT;
0747         iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
0748         udelay(250);
0749     } else {
0750         val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL);
0751         val |= RC_PCIE_RST_OUTPUT;
0752         iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
0753         msleep(100);
0754     }
0755 }
0756 
0757 int iproc_pcie_shutdown(struct iproc_pcie *pcie)
0758 {
0759     iproc_pcie_perst_ctrl(pcie, true);
0760     msleep(500);
0761 
0762     return 0;
0763 }
0764 EXPORT_SYMBOL_GPL(iproc_pcie_shutdown);
0765 
0766 static int iproc_pcie_check_link(struct iproc_pcie *pcie)
0767 {
0768     struct device *dev = pcie->dev;
0769     u32 hdr_type, link_ctrl, link_status, class, val;
0770     bool link_is_active = false;
0771 
0772     /*
0773      * PAXC connects to emulated endpoint devices directly and does not
0774      * have a Serdes.  Therefore skip the link detection logic here.
0775      */
0776     if (pcie->ep_is_internal)
0777         return 0;
0778 
0779     val = iproc_pcie_read_reg(pcie, IPROC_PCIE_LINK_STATUS);
0780     if (!(val & PCIE_PHYLINKUP) || !(val & PCIE_DL_ACTIVE)) {
0781         dev_err(dev, "PHY or data link is INACTIVE!\n");
0782         return -ENODEV;
0783     }
0784 
0785     /* make sure we are not in EP mode */
0786     iproc_pci_raw_config_read32(pcie, 0, PCI_HEADER_TYPE, 1, &hdr_type);
0787     if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE) {
0788         dev_err(dev, "in EP mode, hdr=%#02x\n", hdr_type);
0789         return -EFAULT;
0790     }
0791 
0792     /* force class to PCI_CLASS_BRIDGE_PCI_NORMAL (0x060400) */
0793 #define PCI_BRIDGE_CTRL_REG_OFFSET  0x43c
0794 #define PCI_BRIDGE_CTRL_REG_CLASS_MASK  0xffffff
0795     iproc_pci_raw_config_read32(pcie, 0, PCI_BRIDGE_CTRL_REG_OFFSET,
0796                     4, &class);
0797     class &= ~PCI_BRIDGE_CTRL_REG_CLASS_MASK;
0798     class |= PCI_CLASS_BRIDGE_PCI_NORMAL;
0799     iproc_pci_raw_config_write32(pcie, 0, PCI_BRIDGE_CTRL_REG_OFFSET,
0800                      4, class);
0801 
0802     /* check link status to see if link is active */
0803     iproc_pci_raw_config_read32(pcie, 0, IPROC_PCI_EXP_CAP + PCI_EXP_LNKSTA,
0804                     2, &link_status);
0805     if (link_status & PCI_EXP_LNKSTA_NLW)
0806         link_is_active = true;
0807 
0808     if (!link_is_active) {
0809         /* try GEN 1 link speed */
0810 #define PCI_TARGET_LINK_SPEED_MASK  0xf
0811 #define PCI_TARGET_LINK_SPEED_GEN2  0x2
0812 #define PCI_TARGET_LINK_SPEED_GEN1  0x1
0813         iproc_pci_raw_config_read32(pcie, 0,
0814                         IPROC_PCI_EXP_CAP + PCI_EXP_LNKCTL2,
0815                         4, &link_ctrl);
0816         if ((link_ctrl & PCI_TARGET_LINK_SPEED_MASK) ==
0817             PCI_TARGET_LINK_SPEED_GEN2) {
0818             link_ctrl &= ~PCI_TARGET_LINK_SPEED_MASK;
0819             link_ctrl |= PCI_TARGET_LINK_SPEED_GEN1;
0820             iproc_pci_raw_config_write32(pcie, 0,
0821                     IPROC_PCI_EXP_CAP + PCI_EXP_LNKCTL2,
0822                     4, link_ctrl);
0823             msleep(100);
0824 
0825             iproc_pci_raw_config_read32(pcie, 0,
0826                     IPROC_PCI_EXP_CAP + PCI_EXP_LNKSTA,
0827                     2, &link_status);
0828             if (link_status & PCI_EXP_LNKSTA_NLW)
0829                 link_is_active = true;
0830         }
0831     }
0832 
0833     dev_info(dev, "link: %s\n", link_is_active ? "UP" : "DOWN");
0834 
0835     return link_is_active ? 0 : -ENODEV;
0836 }
0837 
0838 static void iproc_pcie_enable(struct iproc_pcie *pcie)
0839 {
0840     iproc_pcie_write_reg(pcie, IPROC_PCIE_INTX_EN, SYS_RC_INTX_MASK);
0841 }
0842 
0843 static inline bool iproc_pcie_ob_is_valid(struct iproc_pcie *pcie,
0844                       int window_idx)
0845 {
0846     u32 val;
0847 
0848     val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_OARR0, window_idx));
0849 
0850     return !!(val & OARR_VALID);
0851 }
0852 
0853 static inline int iproc_pcie_ob_write(struct iproc_pcie *pcie, int window_idx,
0854                       int size_idx, u64 axi_addr, u64 pci_addr)
0855 {
0856     struct device *dev = pcie->dev;
0857     u16 oarr_offset, omap_offset;
0858 
0859     /*
0860      * Derive the OARR/OMAP offset from the first pair (OARR0/OMAP0) based
0861      * on window index.
0862      */
0863     oarr_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OARR0,
0864                               window_idx));
0865     omap_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OMAP0,
0866                               window_idx));
0867     if (iproc_pcie_reg_is_invalid(oarr_offset) ||
0868         iproc_pcie_reg_is_invalid(omap_offset))
0869         return -EINVAL;
0870 
0871     /*
0872      * Program the OARR registers.  The upper 32-bit OARR register is
0873      * always right after the lower 32-bit OARR register.
0874      */
0875     writel(lower_32_bits(axi_addr) | (size_idx << OARR_SIZE_CFG_SHIFT) |
0876            OARR_VALID, pcie->base + oarr_offset);
0877     writel(upper_32_bits(axi_addr), pcie->base + oarr_offset + 4);
0878 
0879     /* now program the OMAP registers */
0880     writel(lower_32_bits(pci_addr), pcie->base + omap_offset);
0881     writel(upper_32_bits(pci_addr), pcie->base + omap_offset + 4);
0882 
0883     dev_dbg(dev, "ob window [%d]: offset 0x%x axi %pap pci %pap\n",
0884         window_idx, oarr_offset, &axi_addr, &pci_addr);
0885     dev_dbg(dev, "oarr lo 0x%x oarr hi 0x%x\n",
0886         readl(pcie->base + oarr_offset),
0887         readl(pcie->base + oarr_offset + 4));
0888     dev_dbg(dev, "omap lo 0x%x omap hi 0x%x\n",
0889         readl(pcie->base + omap_offset),
0890         readl(pcie->base + omap_offset + 4));
0891 
0892     return 0;
0893 }
0894 
0895 /*
0896  * Some iProc SoCs require the SW to configure the outbound address mapping
0897  *
0898  * Outbound address translation:
0899  *
0900  * iproc_pcie_address = axi_address - axi_offset
0901  * OARR = iproc_pcie_address
0902  * OMAP = pci_addr
0903  *
0904  * axi_addr -> iproc_pcie_address -> OARR -> OMAP -> pci_address
0905  */
0906 static int iproc_pcie_setup_ob(struct iproc_pcie *pcie, u64 axi_addr,
0907                    u64 pci_addr, resource_size_t size)
0908 {
0909     struct iproc_pcie_ob *ob = &pcie->ob;
0910     struct device *dev = pcie->dev;
0911     int ret = -EINVAL, window_idx, size_idx;
0912 
0913     if (axi_addr < ob->axi_offset) {
0914         dev_err(dev, "axi address %pap less than offset %pap\n",
0915             &axi_addr, &ob->axi_offset);
0916         return -EINVAL;
0917     }
0918 
0919     /*
0920      * Translate the AXI address to the internal address used by the iProc
0921      * PCIe core before programming the OARR
0922      */
0923     axi_addr -= ob->axi_offset;
0924 
0925     /* iterate through all OARR/OMAP mapping windows */
0926     for (window_idx = ob->nr_windows - 1; window_idx >= 0; window_idx--) {
0927         const struct iproc_pcie_ob_map *ob_map =
0928             &pcie->ob_map[window_idx];
0929 
0930         /*
0931          * If current outbound window is already in use, move on to the
0932          * next one.
0933          */
0934         if (iproc_pcie_ob_is_valid(pcie, window_idx))
0935             continue;
0936 
0937         /*
0938          * Iterate through all supported window sizes within the
0939          * OARR/OMAP pair to find a match.  Go through the window sizes
0940          * in a descending order.
0941          */
0942         for (size_idx = ob_map->nr_sizes - 1; size_idx >= 0;
0943              size_idx--) {
0944             resource_size_t window_size =
0945                 ob_map->window_sizes[size_idx] * SZ_1M;
0946 
0947             /*
0948              * Keep iterating until we reach the last window and
0949              * with the minimal window size at index zero. In this
0950              * case, we take a compromise by mapping it using the
0951              * minimum window size that can be supported
0952              */
0953             if (size < window_size) {
0954                 if (size_idx > 0 || window_idx > 0)
0955                     continue;
0956 
0957                 /*
0958                  * For the corner case of reaching the minimal
0959                  * window size that can be supported on the
0960                  * last window
0961                  */
0962                 axi_addr = ALIGN_DOWN(axi_addr, window_size);
0963                 pci_addr = ALIGN_DOWN(pci_addr, window_size);
0964                 size = window_size;
0965             }
0966 
0967             if (!IS_ALIGNED(axi_addr, window_size) ||
0968                 !IS_ALIGNED(pci_addr, window_size)) {
0969                 dev_err(dev,
0970                     "axi %pap or pci %pap not aligned\n",
0971                     &axi_addr, &pci_addr);
0972                 return -EINVAL;
0973             }
0974 
0975             /*
0976              * Match found!  Program both OARR and OMAP and mark
0977              * them as a valid entry.
0978              */
0979             ret = iproc_pcie_ob_write(pcie, window_idx, size_idx,
0980                           axi_addr, pci_addr);
0981             if (ret)
0982                 goto err_ob;
0983 
0984             size -= window_size;
0985             if (size == 0)
0986                 return 0;
0987 
0988             /*
0989              * If we are here, we are done with the current window,
0990              * but not yet finished all mappings.  Need to move on
0991              * to the next window.
0992              */
0993             axi_addr += window_size;
0994             pci_addr += window_size;
0995             break;
0996         }
0997     }
0998 
0999 err_ob:
1000     dev_err(dev, "unable to configure outbound mapping\n");
1001     dev_err(dev,
1002         "axi %pap, axi offset %pap, pci %pap, res size %pap\n",
1003         &axi_addr, &ob->axi_offset, &pci_addr, &size);
1004 
1005     return ret;
1006 }
1007 
1008 static int iproc_pcie_map_ranges(struct iproc_pcie *pcie,
1009                  struct list_head *resources)
1010 {
1011     struct device *dev = pcie->dev;
1012     struct resource_entry *window;
1013     int ret;
1014 
1015     resource_list_for_each_entry(window, resources) {
1016         struct resource *res = window->res;
1017         u64 res_type = resource_type(res);
1018 
1019         switch (res_type) {
1020         case IORESOURCE_IO:
1021         case IORESOURCE_BUS:
1022             break;
1023         case IORESOURCE_MEM:
1024             ret = iproc_pcie_setup_ob(pcie, res->start,
1025                           res->start - window->offset,
1026                           resource_size(res));
1027             if (ret)
1028                 return ret;
1029             break;
1030         default:
1031             dev_err(dev, "invalid resource %pR\n", res);
1032             return -EINVAL;
1033         }
1034     }
1035 
1036     return 0;
1037 }
1038 
1039 static inline bool iproc_pcie_ib_is_in_use(struct iproc_pcie *pcie,
1040                        int region_idx)
1041 {
1042     const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
1043     u32 val;
1044 
1045     val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_IARR0, region_idx));
1046 
1047     return !!(val & (BIT(ib_map->nr_sizes) - 1));
1048 }
1049 
1050 static inline bool iproc_pcie_ib_check_type(const struct iproc_pcie_ib_map *ib_map,
1051                         enum iproc_pcie_ib_map_type type)
1052 {
1053     return !!(ib_map->type == type);
1054 }
1055 
1056 static int iproc_pcie_ib_write(struct iproc_pcie *pcie, int region_idx,
1057                    int size_idx, int nr_windows, u64 axi_addr,
1058                    u64 pci_addr, resource_size_t size)
1059 {
1060     struct device *dev = pcie->dev;
1061     const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
1062     u16 iarr_offset, imap_offset;
1063     u32 val;
1064     int window_idx;
1065 
1066     iarr_offset = iproc_pcie_reg_offset(pcie,
1067                 MAP_REG(IPROC_PCIE_IARR0, region_idx));
1068     imap_offset = iproc_pcie_reg_offset(pcie,
1069                 MAP_REG(IPROC_PCIE_IMAP0, region_idx));
1070     if (iproc_pcie_reg_is_invalid(iarr_offset) ||
1071         iproc_pcie_reg_is_invalid(imap_offset))
1072         return -EINVAL;
1073 
1074     dev_dbg(dev, "ib region [%d]: offset 0x%x axi %pap pci %pap\n",
1075         region_idx, iarr_offset, &axi_addr, &pci_addr);
1076 
1077     /*
1078      * Program the IARR registers.  The upper 32-bit IARR register is
1079      * always right after the lower 32-bit IARR register.
1080      */
1081     writel(lower_32_bits(pci_addr) | BIT(size_idx),
1082            pcie->base + iarr_offset);
1083     writel(upper_32_bits(pci_addr), pcie->base + iarr_offset + 4);
1084 
1085     dev_dbg(dev, "iarr lo 0x%x iarr hi 0x%x\n",
1086         readl(pcie->base + iarr_offset),
1087         readl(pcie->base + iarr_offset + 4));
1088 
1089     /*
1090      * Now program the IMAP registers.  Each IARR region may have one or
1091      * more IMAP windows.
1092      */
1093     size >>= ilog2(nr_windows);
1094     for (window_idx = 0; window_idx < nr_windows; window_idx++) {
1095         val = readl(pcie->base + imap_offset);
1096         val |= lower_32_bits(axi_addr) | IMAP_VALID;
1097         writel(val, pcie->base + imap_offset);
1098         writel(upper_32_bits(axi_addr),
1099                pcie->base + imap_offset + ib_map->imap_addr_offset);
1100 
1101         dev_dbg(dev, "imap window [%d] lo 0x%x hi 0x%x\n",
1102             window_idx, readl(pcie->base + imap_offset),
1103             readl(pcie->base + imap_offset +
1104                   ib_map->imap_addr_offset));
1105 
1106         imap_offset += ib_map->imap_window_offset;
1107         axi_addr += size;
1108     }
1109 
1110     return 0;
1111 }
1112 
1113 static int iproc_pcie_setup_ib(struct iproc_pcie *pcie,
1114                    struct resource_entry *entry,
1115                    enum iproc_pcie_ib_map_type type)
1116 {
1117     struct device *dev = pcie->dev;
1118     struct iproc_pcie_ib *ib = &pcie->ib;
1119     int ret;
1120     unsigned int region_idx, size_idx;
1121     u64 axi_addr = entry->res->start;
1122     u64 pci_addr = entry->res->start - entry->offset;
1123     resource_size_t size = resource_size(entry->res);
1124 
1125     /* iterate through all IARR mapping regions */
1126     for (region_idx = 0; region_idx < ib->nr_regions; region_idx++) {
1127         const struct iproc_pcie_ib_map *ib_map =
1128             &pcie->ib_map[region_idx];
1129 
1130         /*
1131          * If current inbound region is already in use or not a
1132          * compatible type, move on to the next.
1133          */
1134         if (iproc_pcie_ib_is_in_use(pcie, region_idx) ||
1135             !iproc_pcie_ib_check_type(ib_map, type))
1136             continue;
1137 
1138         /* iterate through all supported region sizes to find a match */
1139         for (size_idx = 0; size_idx < ib_map->nr_sizes; size_idx++) {
1140             resource_size_t region_size =
1141             ib_map->region_sizes[size_idx] * ib_map->size_unit;
1142 
1143             if (size != region_size)
1144                 continue;
1145 
1146             if (!IS_ALIGNED(axi_addr, region_size) ||
1147                 !IS_ALIGNED(pci_addr, region_size)) {
1148                 dev_err(dev,
1149                     "axi %pap or pci %pap not aligned\n",
1150                     &axi_addr, &pci_addr);
1151                 return -EINVAL;
1152             }
1153 
1154             /* Match found!  Program IARR and all IMAP windows. */
1155             ret = iproc_pcie_ib_write(pcie, region_idx, size_idx,
1156                           ib_map->nr_windows, axi_addr,
1157                           pci_addr, size);
1158             if (ret)
1159                 goto err_ib;
1160             else
1161                 return 0;
1162 
1163         }
1164     }
1165     ret = -EINVAL;
1166 
1167 err_ib:
1168     dev_err(dev, "unable to configure inbound mapping\n");
1169     dev_err(dev, "axi %pap, pci %pap, res size %pap\n",
1170         &axi_addr, &pci_addr, &size);
1171 
1172     return ret;
1173 }
1174 
1175 static int iproc_pcie_map_dma_ranges(struct iproc_pcie *pcie)
1176 {
1177     struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1178     struct resource_entry *entry;
1179     int ret = 0;
1180 
1181     resource_list_for_each_entry(entry, &host->dma_ranges) {
1182         /* Each range entry corresponds to an inbound mapping region */
1183         ret = iproc_pcie_setup_ib(pcie, entry, IPROC_PCIE_IB_MAP_MEM);
1184         if (ret)
1185             break;
1186     }
1187 
1188     return ret;
1189 }
1190 
1191 static void iproc_pcie_invalidate_mapping(struct iproc_pcie *pcie)
1192 {
1193     struct iproc_pcie_ib *ib = &pcie->ib;
1194     struct iproc_pcie_ob *ob = &pcie->ob;
1195     int idx;
1196 
1197     if (pcie->ep_is_internal)
1198         return;
1199 
1200     if (pcie->need_ob_cfg) {
1201         /* iterate through all OARR mapping regions */
1202         for (idx = ob->nr_windows - 1; idx >= 0; idx--) {
1203             iproc_pcie_write_reg(pcie,
1204                          MAP_REG(IPROC_PCIE_OARR0, idx), 0);
1205         }
1206     }
1207 
1208     if (pcie->need_ib_cfg) {
1209         /* iterate through all IARR mapping regions */
1210         for (idx = 0; idx < ib->nr_regions; idx++) {
1211             iproc_pcie_write_reg(pcie,
1212                          MAP_REG(IPROC_PCIE_IARR0, idx), 0);
1213         }
1214     }
1215 }
1216 
1217 static int iproce_pcie_get_msi(struct iproc_pcie *pcie,
1218                    struct device_node *msi_node,
1219                    u64 *msi_addr)
1220 {
1221     struct device *dev = pcie->dev;
1222     int ret;
1223     struct resource res;
1224 
1225     /*
1226      * Check if 'msi-map' points to ARM GICv3 ITS, which is the only
1227      * supported external MSI controller that requires steering.
1228      */
1229     if (!of_device_is_compatible(msi_node, "arm,gic-v3-its")) {
1230         dev_err(dev, "unable to find compatible MSI controller\n");
1231         return -ENODEV;
1232     }
1233 
1234     /* derive GITS_TRANSLATER address from GICv3 */
1235     ret = of_address_to_resource(msi_node, 0, &res);
1236     if (ret < 0) {
1237         dev_err(dev, "unable to obtain MSI controller resources\n");
1238         return ret;
1239     }
1240 
1241     *msi_addr = res.start + GITS_TRANSLATER;
1242     return 0;
1243 }
1244 
1245 static int iproc_pcie_paxb_v2_msi_steer(struct iproc_pcie *pcie, u64 msi_addr)
1246 {
1247     int ret;
1248     struct resource_entry entry;
1249 
1250     memset(&entry, 0, sizeof(entry));
1251     entry.res = &entry.__res;
1252 
1253     msi_addr &= ~(SZ_32K - 1);
1254     entry.res->start = msi_addr;
1255     entry.res->end = msi_addr + SZ_32K - 1;
1256 
1257     ret = iproc_pcie_setup_ib(pcie, &entry, IPROC_PCIE_IB_MAP_IO);
1258     return ret;
1259 }
1260 
1261 static void iproc_pcie_paxc_v2_msi_steer(struct iproc_pcie *pcie, u64 msi_addr,
1262                      bool enable)
1263 {
1264     u32 val;
1265 
1266     if (!enable) {
1267         /*
1268          * Disable PAXC MSI steering. All write transfers will be
1269          * treated as non-MSI transfers
1270          */
1271         val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_EN_CFG);
1272         val &= ~MSI_ENABLE_CFG;
1273         iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_EN_CFG, val);
1274         return;
1275     }
1276 
1277     /*
1278      * Program bits [43:13] of address of GITS_TRANSLATER register into
1279      * bits [30:0] of the MSI base address register.  In fact, in all iProc
1280      * based SoCs, all I/O register bases are well below the 32-bit
1281      * boundary, so we can safely assume bits [43:32] are always zeros.
1282      */
1283     iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_BASE_ADDR,
1284                  (u32)(msi_addr >> 13));
1285 
1286     /* use a default 8K window size */
1287     iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_WINDOW_SIZE, 0);
1288 
1289     /* steering MSI to GICv3 ITS */
1290     val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_GIC_MODE);
1291     val |= GIC_V3_CFG;
1292     iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_GIC_MODE, val);
1293 
1294     /*
1295      * Program bits [43:2] of address of GITS_TRANSLATER register into the
1296      * iProc MSI address registers.
1297      */
1298     msi_addr >>= 2;
1299     iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_ADDR_HI,
1300                  upper_32_bits(msi_addr));
1301     iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_ADDR_LO,
1302                  lower_32_bits(msi_addr));
1303 
1304     /* enable MSI */
1305     val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_EN_CFG);
1306     val |= MSI_ENABLE_CFG;
1307     iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_EN_CFG, val);
1308 }
1309 
1310 static int iproc_pcie_msi_steer(struct iproc_pcie *pcie,
1311                 struct device_node *msi_node)
1312 {
1313     struct device *dev = pcie->dev;
1314     int ret;
1315     u64 msi_addr;
1316 
1317     ret = iproce_pcie_get_msi(pcie, msi_node, &msi_addr);
1318     if (ret < 0) {
1319         dev_err(dev, "msi steering failed\n");
1320         return ret;
1321     }
1322 
1323     switch (pcie->type) {
1324     case IPROC_PCIE_PAXB_V2:
1325         ret = iproc_pcie_paxb_v2_msi_steer(pcie, msi_addr);
1326         if (ret)
1327             return ret;
1328         break;
1329     case IPROC_PCIE_PAXC_V2:
1330         iproc_pcie_paxc_v2_msi_steer(pcie, msi_addr, true);
1331         break;
1332     default:
1333         return -EINVAL;
1334     }
1335 
1336     return 0;
1337 }
1338 
1339 static int iproc_pcie_msi_enable(struct iproc_pcie *pcie)
1340 {
1341     struct device_node *msi_node;
1342     int ret;
1343 
1344     /*
1345      * Either the "msi-parent" or the "msi-map" phandle needs to exist
1346      * for us to obtain the MSI node.
1347      */
1348 
1349     msi_node = of_parse_phandle(pcie->dev->of_node, "msi-parent", 0);
1350     if (!msi_node) {
1351         const __be32 *msi_map = NULL;
1352         int len;
1353         u32 phandle;
1354 
1355         msi_map = of_get_property(pcie->dev->of_node, "msi-map", &len);
1356         if (!msi_map)
1357             return -ENODEV;
1358 
1359         phandle = be32_to_cpup(msi_map + 1);
1360         msi_node = of_find_node_by_phandle(phandle);
1361         if (!msi_node)
1362             return -ENODEV;
1363     }
1364 
1365     /*
1366      * Certain revisions of the iProc PCIe controller require additional
1367      * configurations to steer the MSI writes towards an external MSI
1368      * controller.
1369      */
1370     if (pcie->need_msi_steer) {
1371         ret = iproc_pcie_msi_steer(pcie, msi_node);
1372         if (ret)
1373             goto out_put_node;
1374     }
1375 
1376     /*
1377      * If another MSI controller is being used, the call below should fail
1378      * but that is okay
1379      */
1380     ret = iproc_msi_init(pcie, msi_node);
1381 
1382 out_put_node:
1383     of_node_put(msi_node);
1384     return ret;
1385 }
1386 
1387 static void iproc_pcie_msi_disable(struct iproc_pcie *pcie)
1388 {
1389     iproc_msi_exit(pcie);
1390 }
1391 
1392 static int iproc_pcie_rev_init(struct iproc_pcie *pcie)
1393 {
1394     struct device *dev = pcie->dev;
1395     unsigned int reg_idx;
1396     const u16 *regs;
1397 
1398     switch (pcie->type) {
1399     case IPROC_PCIE_PAXB_BCMA:
1400         regs = iproc_pcie_reg_paxb_bcma;
1401         break;
1402     case IPROC_PCIE_PAXB:
1403         regs = iproc_pcie_reg_paxb;
1404         pcie->has_apb_err_disable = true;
1405         if (pcie->need_ob_cfg) {
1406             pcie->ob_map = paxb_ob_map;
1407             pcie->ob.nr_windows = ARRAY_SIZE(paxb_ob_map);
1408         }
1409         break;
1410     case IPROC_PCIE_PAXB_V2:
1411         regs = iproc_pcie_reg_paxb_v2;
1412         pcie->iproc_cfg_read = true;
1413         pcie->has_apb_err_disable = true;
1414         if (pcie->need_ob_cfg) {
1415             pcie->ob_map = paxb_v2_ob_map;
1416             pcie->ob.nr_windows = ARRAY_SIZE(paxb_v2_ob_map);
1417         }
1418         pcie->ib.nr_regions = ARRAY_SIZE(paxb_v2_ib_map);
1419         pcie->ib_map = paxb_v2_ib_map;
1420         pcie->need_msi_steer = true;
1421         dev_warn(dev, "reads of config registers that contain %#x return incorrect data\n",
1422              CFG_RETRY_STATUS);
1423         break;
1424     case IPROC_PCIE_PAXC:
1425         regs = iproc_pcie_reg_paxc;
1426         pcie->ep_is_internal = true;
1427         pcie->iproc_cfg_read = true;
1428         pcie->rej_unconfig_pf = true;
1429         break;
1430     case IPROC_PCIE_PAXC_V2:
1431         regs = iproc_pcie_reg_paxc_v2;
1432         pcie->ep_is_internal = true;
1433         pcie->iproc_cfg_read = true;
1434         pcie->rej_unconfig_pf = true;
1435         pcie->need_msi_steer = true;
1436         break;
1437     default:
1438         dev_err(dev, "incompatible iProc PCIe interface\n");
1439         return -EINVAL;
1440     }
1441 
1442     pcie->reg_offsets = devm_kcalloc(dev, IPROC_PCIE_MAX_NUM_REG,
1443                      sizeof(*pcie->reg_offsets),
1444                      GFP_KERNEL);
1445     if (!pcie->reg_offsets)
1446         return -ENOMEM;
1447 
1448     /* go through the register table and populate all valid registers */
1449     pcie->reg_offsets[0] = (pcie->type == IPROC_PCIE_PAXC_V2) ?
1450         IPROC_PCIE_REG_INVALID : regs[0];
1451     for (reg_idx = 1; reg_idx < IPROC_PCIE_MAX_NUM_REG; reg_idx++)
1452         pcie->reg_offsets[reg_idx] = regs[reg_idx] ?
1453             regs[reg_idx] : IPROC_PCIE_REG_INVALID;
1454 
1455     return 0;
1456 }
1457 
1458 int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res)
1459 {
1460     struct device *dev;
1461     int ret;
1462     struct pci_dev *pdev;
1463     struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1464 
1465     dev = pcie->dev;
1466 
1467     ret = iproc_pcie_rev_init(pcie);
1468     if (ret) {
1469         dev_err(dev, "unable to initialize controller parameters\n");
1470         return ret;
1471     }
1472 
1473     ret = phy_init(pcie->phy);
1474     if (ret) {
1475         dev_err(dev, "unable to initialize PCIe PHY\n");
1476         return ret;
1477     }
1478 
1479     ret = phy_power_on(pcie->phy);
1480     if (ret) {
1481         dev_err(dev, "unable to power on PCIe PHY\n");
1482         goto err_exit_phy;
1483     }
1484 
1485     iproc_pcie_perst_ctrl(pcie, true);
1486     iproc_pcie_perst_ctrl(pcie, false);
1487 
1488     iproc_pcie_invalidate_mapping(pcie);
1489 
1490     if (pcie->need_ob_cfg) {
1491         ret = iproc_pcie_map_ranges(pcie, res);
1492         if (ret) {
1493             dev_err(dev, "map failed\n");
1494             goto err_power_off_phy;
1495         }
1496     }
1497 
1498     if (pcie->need_ib_cfg) {
1499         ret = iproc_pcie_map_dma_ranges(pcie);
1500         if (ret && ret != -ENOENT)
1501             goto err_power_off_phy;
1502     }
1503 
1504     ret = iproc_pcie_check_link(pcie);
1505     if (ret) {
1506         dev_err(dev, "no PCIe EP device detected\n");
1507         goto err_power_off_phy;
1508     }
1509 
1510     iproc_pcie_enable(pcie);
1511 
1512     if (IS_ENABLED(CONFIG_PCI_MSI))
1513         if (iproc_pcie_msi_enable(pcie))
1514             dev_info(dev, "not using iProc MSI\n");
1515 
1516     host->ops = &iproc_pcie_ops;
1517     host->sysdata = pcie;
1518     host->map_irq = pcie->map_irq;
1519 
1520     ret = pci_host_probe(host);
1521     if (ret < 0) {
1522         dev_err(dev, "failed to scan host: %d\n", ret);
1523         goto err_power_off_phy;
1524     }
1525 
1526     for_each_pci_bridge(pdev, host->bus) {
1527         if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT)
1528             pcie_print_link_status(pdev);
1529     }
1530 
1531     return 0;
1532 
1533 err_power_off_phy:
1534     phy_power_off(pcie->phy);
1535 err_exit_phy:
1536     phy_exit(pcie->phy);
1537     return ret;
1538 }
1539 EXPORT_SYMBOL(iproc_pcie_setup);
1540 
1541 int iproc_pcie_remove(struct iproc_pcie *pcie)
1542 {
1543     struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1544 
1545     pci_stop_root_bus(host->bus);
1546     pci_remove_root_bus(host->bus);
1547 
1548     iproc_pcie_msi_disable(pcie);
1549 
1550     phy_power_off(pcie->phy);
1551     phy_exit(pcie->phy);
1552 
1553     return 0;
1554 }
1555 EXPORT_SYMBOL(iproc_pcie_remove);
1556 
1557 /*
1558  * The MSI parsing logic in certain revisions of Broadcom PAXC based root
1559  * complex does not work and needs to be disabled
1560  */
1561 static void quirk_paxc_disable_msi_parsing(struct pci_dev *pdev)
1562 {
1563     struct iproc_pcie *pcie = iproc_data(pdev->bus);
1564 
1565     if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
1566         iproc_pcie_paxc_v2_msi_steer(pcie, 0, false);
1567 }
1568 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0x16f0,
1569             quirk_paxc_disable_msi_parsing);
1570 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd802,
1571             quirk_paxc_disable_msi_parsing);
1572 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd804,
1573             quirk_paxc_disable_msi_parsing);
1574 
1575 static void quirk_paxc_bridge(struct pci_dev *pdev)
1576 {
1577     /*
1578      * The PCI config space is shared with the PAXC root port and the first
1579      * Ethernet device.  So, we need to workaround this by telling the PCI
1580      * code that the bridge is not an Ethernet device.
1581      */
1582     if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
1583         pdev->class = PCI_CLASS_BRIDGE_PCI_NORMAL;
1584 
1585     /*
1586      * MPSS is not being set properly (as it is currently 0).  This is
1587      * because that area of the PCI config space is hard coded to zero, and
1588      * is not modifiable by firmware.  Set this to 2 (e.g., 512 byte MPS)
1589      * so that the MPS can be set to the real max value.
1590      */
1591     pdev->pcie_mpss = 2;
1592 }
1593 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0x16cd, quirk_paxc_bridge);
1594 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0x16f0, quirk_paxc_bridge);
1595 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd750, quirk_paxc_bridge);
1596 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd802, quirk_paxc_bridge);
1597 DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd804, quirk_paxc_bridge);
1598 
1599 MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
1600 MODULE_DESCRIPTION("Broadcom iPROC PCIe common driver");
1601 MODULE_LICENSE("GPL v2");