PCI: aardvark: Rewrite IRQ code to chained IRQ handler
[platform/kernel/linux-rpi.git] / drivers / pci / controller / pci-aardvark.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for the Aardvark PCIe controller, used on Marvell Armada
4  * 3700.
5  *
6  * Copyright (C) 2016 Marvell
7  *
8  * Author: Hezi Shahmoon <hezi.shahmoon@marvell.com>
9  */
10
11 #include <linux/delay.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/irqdomain.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/pci.h>
19 #include <linux/pci-ecam.h>
20 #include <linux/init.h>
21 #include <linux/phy/phy.h>
22 #include <linux/platform_device.h>
23 #include <linux/msi.h>
24 #include <linux/of_address.h>
25 #include <linux/of_gpio.h>
26 #include <linux/of_pci.h>
27
28 #include "../pci.h"
29 #include "../pci-bridge-emul.h"
30
31 /* PCIe core registers */
32 #define PCIE_CORE_DEV_ID_REG                                    0x0
33 #define PCIE_CORE_CMD_STATUS_REG                                0x4
34 #define PCIE_CORE_DEV_REV_REG                                   0x8
35 #define PCIE_CORE_PCIEXP_CAP                                    0xc0
36 #define PCIE_CORE_ERR_CAPCTL_REG                                0x118
37 #define     PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX                    BIT(5)
38 #define     PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN                 BIT(6)
39 #define     PCIE_CORE_ERR_CAPCTL_ECRC_CHCK                      BIT(7)
40 #define     PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV                  BIT(8)
41 /* PIO registers base address and register offsets */
42 #define PIO_BASE_ADDR                           0x4000
43 #define PIO_CTRL                                (PIO_BASE_ADDR + 0x0)
44 #define   PIO_CTRL_TYPE_MASK                    GENMASK(3, 0)
45 #define   PIO_CTRL_ADDR_WIN_DISABLE             BIT(24)
46 #define PIO_STAT                                (PIO_BASE_ADDR + 0x4)
47 #define   PIO_COMPLETION_STATUS_SHIFT           7
48 #define   PIO_COMPLETION_STATUS_MASK            GENMASK(9, 7)
49 #define   PIO_COMPLETION_STATUS_OK              0
50 #define   PIO_COMPLETION_STATUS_UR              1
51 #define   PIO_COMPLETION_STATUS_CRS             2
52 #define   PIO_COMPLETION_STATUS_CA              4
53 #define   PIO_NON_POSTED_REQ                    BIT(10)
54 #define   PIO_ERR_STATUS                        BIT(11)
55 #define PIO_ADDR_LS                             (PIO_BASE_ADDR + 0x8)
56 #define PIO_ADDR_MS                             (PIO_BASE_ADDR + 0xc)
57 #define PIO_WR_DATA                             (PIO_BASE_ADDR + 0x10)
58 #define PIO_WR_DATA_STRB                        (PIO_BASE_ADDR + 0x14)
59 #define PIO_RD_DATA                             (PIO_BASE_ADDR + 0x18)
60 #define PIO_START                               (PIO_BASE_ADDR + 0x1c)
61 #define PIO_ISR                                 (PIO_BASE_ADDR + 0x20)
62 #define PIO_ISRM                                (PIO_BASE_ADDR + 0x24)
63
64 /* Aardvark Control registers */
65 #define CONTROL_BASE_ADDR                       0x4800
66 #define PCIE_CORE_CTRL0_REG                     (CONTROL_BASE_ADDR + 0x0)
67 #define     PCIE_GEN_SEL_MSK                    0x3
68 #define     PCIE_GEN_SEL_SHIFT                  0x0
69 #define     SPEED_GEN_1                         0
70 #define     SPEED_GEN_2                         1
71 #define     SPEED_GEN_3                         2
72 #define     IS_RC_MSK                           1
73 #define     IS_RC_SHIFT                         2
74 #define     LANE_CNT_MSK                        0x18
75 #define     LANE_CNT_SHIFT                      0x3
76 #define     LANE_COUNT_1                        (0 << LANE_CNT_SHIFT)
77 #define     LANE_COUNT_2                        (1 << LANE_CNT_SHIFT)
78 #define     LANE_COUNT_4                        (2 << LANE_CNT_SHIFT)
79 #define     LANE_COUNT_8                        (3 << LANE_CNT_SHIFT)
80 #define     LINK_TRAINING_EN                    BIT(6)
81 #define     LEGACY_INTA                         BIT(28)
82 #define     LEGACY_INTB                         BIT(29)
83 #define     LEGACY_INTC                         BIT(30)
84 #define     LEGACY_INTD                         BIT(31)
85 #define PCIE_CORE_CTRL1_REG                     (CONTROL_BASE_ADDR + 0x4)
86 #define     HOT_RESET_GEN                       BIT(0)
87 #define PCIE_CORE_CTRL2_REG                     (CONTROL_BASE_ADDR + 0x8)
88 #define     PCIE_CORE_CTRL2_RESERVED            0x7
89 #define     PCIE_CORE_CTRL2_TD_ENABLE           BIT(4)
90 #define     PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
91 #define     PCIE_CORE_CTRL2_OB_WIN_ENABLE       BIT(6)
92 #define     PCIE_CORE_CTRL2_MSI_ENABLE          BIT(10)
93 #define PCIE_CORE_REF_CLK_REG                   (CONTROL_BASE_ADDR + 0x14)
94 #define     PCIE_CORE_REF_CLK_TX_ENABLE         BIT(1)
95 #define     PCIE_CORE_REF_CLK_RX_ENABLE         BIT(2)
96 #define PCIE_MSG_LOG_REG                        (CONTROL_BASE_ADDR + 0x30)
97 #define PCIE_ISR0_REG                           (CONTROL_BASE_ADDR + 0x40)
98 #define PCIE_MSG_PM_PME_MASK                    BIT(7)
99 #define PCIE_ISR0_MASK_REG                      (CONTROL_BASE_ADDR + 0x44)
100 #define     PCIE_ISR0_MSI_INT_PENDING           BIT(24)
101 #define     PCIE_ISR0_INTX_ASSERT(val)          BIT(16 + (val))
102 #define     PCIE_ISR0_INTX_DEASSERT(val)        BIT(20 + (val))
103 #define     PCIE_ISR0_ALL_MASK                  GENMASK(31, 0)
104 #define PCIE_ISR1_REG                           (CONTROL_BASE_ADDR + 0x48)
105 #define PCIE_ISR1_MASK_REG                      (CONTROL_BASE_ADDR + 0x4C)
106 #define     PCIE_ISR1_POWER_STATE_CHANGE        BIT(4)
107 #define     PCIE_ISR1_FLUSH                     BIT(5)
108 #define     PCIE_ISR1_INTX_ASSERT(val)          BIT(8 + (val))
109 #define     PCIE_ISR1_ALL_MASK                  GENMASK(31, 0)
110 #define PCIE_MSI_ADDR_LOW_REG                   (CONTROL_BASE_ADDR + 0x50)
111 #define PCIE_MSI_ADDR_HIGH_REG                  (CONTROL_BASE_ADDR + 0x54)
112 #define PCIE_MSI_STATUS_REG                     (CONTROL_BASE_ADDR + 0x58)
113 #define PCIE_MSI_MASK_REG                       (CONTROL_BASE_ADDR + 0x5C)
114 #define     PCIE_MSI_ALL_MASK                   GENMASK(31, 0)
115 #define PCIE_MSI_PAYLOAD_REG                    (CONTROL_BASE_ADDR + 0x9C)
116 #define     PCIE_MSI_DATA_MASK                  GENMASK(15, 0)
117
118 /* PCIe window configuration */
119 #define OB_WIN_BASE_ADDR                        0x4c00
120 #define OB_WIN_BLOCK_SIZE                       0x20
121 #define OB_WIN_COUNT                            8
122 #define OB_WIN_REG_ADDR(win, offset)            (OB_WIN_BASE_ADDR + \
123                                                  OB_WIN_BLOCK_SIZE * (win) + \
124                                                  (offset))
125 #define OB_WIN_MATCH_LS(win)                    OB_WIN_REG_ADDR(win, 0x00)
126 #define     OB_WIN_ENABLE                       BIT(0)
127 #define OB_WIN_MATCH_MS(win)                    OB_WIN_REG_ADDR(win, 0x04)
128 #define OB_WIN_REMAP_LS(win)                    OB_WIN_REG_ADDR(win, 0x08)
129 #define OB_WIN_REMAP_MS(win)                    OB_WIN_REG_ADDR(win, 0x0c)
130 #define OB_WIN_MASK_LS(win)                     OB_WIN_REG_ADDR(win, 0x10)
131 #define OB_WIN_MASK_MS(win)                     OB_WIN_REG_ADDR(win, 0x14)
132 #define OB_WIN_ACTIONS(win)                     OB_WIN_REG_ADDR(win, 0x18)
133 #define OB_WIN_DEFAULT_ACTIONS                  (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4)
134 #define     OB_WIN_FUNC_NUM_MASK                GENMASK(31, 24)
135 #define     OB_WIN_FUNC_NUM_SHIFT               24
136 #define     OB_WIN_FUNC_NUM_ENABLE              BIT(23)
137 #define     OB_WIN_BUS_NUM_BITS_MASK            GENMASK(22, 20)
138 #define     OB_WIN_BUS_NUM_BITS_SHIFT           20
139 #define     OB_WIN_MSG_CODE_ENABLE              BIT(22)
140 #define     OB_WIN_MSG_CODE_MASK                GENMASK(21, 14)
141 #define     OB_WIN_MSG_CODE_SHIFT               14
142 #define     OB_WIN_MSG_PAYLOAD_LEN              BIT(12)
143 #define     OB_WIN_ATTR_ENABLE                  BIT(11)
144 #define     OB_WIN_ATTR_TC_MASK                 GENMASK(10, 8)
145 #define     OB_WIN_ATTR_TC_SHIFT                8
146 #define     OB_WIN_ATTR_RELAXED                 BIT(7)
147 #define     OB_WIN_ATTR_NOSNOOP                 BIT(6)
148 #define     OB_WIN_ATTR_POISON                  BIT(5)
149 #define     OB_WIN_ATTR_IDO                     BIT(4)
150 #define     OB_WIN_TYPE_MASK                    GENMASK(3, 0)
151 #define     OB_WIN_TYPE_SHIFT                   0
152 #define     OB_WIN_TYPE_MEM                     0x0
153 #define     OB_WIN_TYPE_IO                      0x4
154 #define     OB_WIN_TYPE_CONFIG_TYPE0            0x8
155 #define     OB_WIN_TYPE_CONFIG_TYPE1            0x9
156 #define     OB_WIN_TYPE_MSG                     0xc
157
158 /* LMI registers base address and register offsets */
159 #define LMI_BASE_ADDR                           0x6000
160 #define CFG_REG                                 (LMI_BASE_ADDR + 0x0)
161 #define     LTSSM_SHIFT                         24
162 #define     LTSSM_MASK                          0x3f
163 #define     RC_BAR_CONFIG                       0x300
164
165 /* LTSSM values in CFG_REG */
166 enum {
167         LTSSM_DETECT_QUIET                      = 0x0,
168         LTSSM_DETECT_ACTIVE                     = 0x1,
169         LTSSM_POLLING_ACTIVE                    = 0x2,
170         LTSSM_POLLING_COMPLIANCE                = 0x3,
171         LTSSM_POLLING_CONFIGURATION             = 0x4,
172         LTSSM_CONFIG_LINKWIDTH_START            = 0x5,
173         LTSSM_CONFIG_LINKWIDTH_ACCEPT           = 0x6,
174         LTSSM_CONFIG_LANENUM_ACCEPT             = 0x7,
175         LTSSM_CONFIG_LANENUM_WAIT               = 0x8,
176         LTSSM_CONFIG_COMPLETE                   = 0x9,
177         LTSSM_CONFIG_IDLE                       = 0xa,
178         LTSSM_RECOVERY_RCVR_LOCK                = 0xb,
179         LTSSM_RECOVERY_SPEED                    = 0xc,
180         LTSSM_RECOVERY_RCVR_CFG                 = 0xd,
181         LTSSM_RECOVERY_IDLE                     = 0xe,
182         LTSSM_L0                                = 0x10,
183         LTSSM_RX_L0S_ENTRY                      = 0x11,
184         LTSSM_RX_L0S_IDLE                       = 0x12,
185         LTSSM_RX_L0S_FTS                        = 0x13,
186         LTSSM_TX_L0S_ENTRY                      = 0x14,
187         LTSSM_TX_L0S_IDLE                       = 0x15,
188         LTSSM_TX_L0S_FTS                        = 0x16,
189         LTSSM_L1_ENTRY                          = 0x17,
190         LTSSM_L1_IDLE                           = 0x18,
191         LTSSM_L2_IDLE                           = 0x19,
192         LTSSM_L2_TRANSMIT_WAKE                  = 0x1a,
193         LTSSM_DISABLED                          = 0x20,
194         LTSSM_LOOPBACK_ENTRY_MASTER             = 0x21,
195         LTSSM_LOOPBACK_ACTIVE_MASTER            = 0x22,
196         LTSSM_LOOPBACK_EXIT_MASTER              = 0x23,
197         LTSSM_LOOPBACK_ENTRY_SLAVE              = 0x24,
198         LTSSM_LOOPBACK_ACTIVE_SLAVE             = 0x25,
199         LTSSM_LOOPBACK_EXIT_SLAVE               = 0x26,
200         LTSSM_HOT_RESET                         = 0x27,
201         LTSSM_RECOVERY_EQUALIZATION_PHASE0      = 0x28,
202         LTSSM_RECOVERY_EQUALIZATION_PHASE1      = 0x29,
203         LTSSM_RECOVERY_EQUALIZATION_PHASE2      = 0x2a,
204         LTSSM_RECOVERY_EQUALIZATION_PHASE3      = 0x2b,
205 };
206
207 #define VENDOR_ID_REG                           (LMI_BASE_ADDR + 0x44)
208
209 /* PCIe core controller registers */
210 #define CTRL_CORE_BASE_ADDR                     0x18000
211 #define CTRL_CONFIG_REG                         (CTRL_CORE_BASE_ADDR + 0x0)
212 #define     CTRL_MODE_SHIFT                     0x0
213 #define     CTRL_MODE_MASK                      0x1
214 #define     PCIE_CORE_MODE_DIRECT               0x0
215 #define     PCIE_CORE_MODE_COMMAND              0x1
216
217 /* PCIe Central Interrupts Registers */
218 #define CENTRAL_INT_BASE_ADDR                   0x1b000
219 #define HOST_CTRL_INT_STATUS_REG                (CENTRAL_INT_BASE_ADDR + 0x0)
220 #define HOST_CTRL_INT_MASK_REG                  (CENTRAL_INT_BASE_ADDR + 0x4)
221 #define     PCIE_IRQ_CMDQ_INT                   BIT(0)
222 #define     PCIE_IRQ_MSI_STATUS_INT             BIT(1)
223 #define     PCIE_IRQ_CMD_SENT_DONE              BIT(3)
224 #define     PCIE_IRQ_DMA_INT                    BIT(4)
225 #define     PCIE_IRQ_IB_DXFERDONE               BIT(5)
226 #define     PCIE_IRQ_OB_DXFERDONE               BIT(6)
227 #define     PCIE_IRQ_OB_RXFERDONE               BIT(7)
228 #define     PCIE_IRQ_COMPQ_INT                  BIT(12)
229 #define     PCIE_IRQ_DIR_RD_DDR_DET             BIT(13)
230 #define     PCIE_IRQ_DIR_WR_DDR_DET             BIT(14)
231 #define     PCIE_IRQ_CORE_INT                   BIT(16)
232 #define     PCIE_IRQ_CORE_INT_PIO               BIT(17)
233 #define     PCIE_IRQ_DPMU_INT                   BIT(18)
234 #define     PCIE_IRQ_PCIE_MIS_INT               BIT(19)
235 #define     PCIE_IRQ_MSI_INT1_DET               BIT(20)
236 #define     PCIE_IRQ_MSI_INT2_DET               BIT(21)
237 #define     PCIE_IRQ_RC_DBELL_DET               BIT(22)
238 #define     PCIE_IRQ_EP_STATUS                  BIT(23)
239 #define     PCIE_IRQ_ALL_MASK                   GENMASK(31, 0)
240 #define     PCIE_IRQ_ENABLE_INTS_MASK           PCIE_IRQ_CORE_INT
241
242 /* Transaction types */
243 #define PCIE_CONFIG_RD_TYPE0                    0x8
244 #define PCIE_CONFIG_RD_TYPE1                    0x9
245 #define PCIE_CONFIG_WR_TYPE0                    0xa
246 #define PCIE_CONFIG_WR_TYPE1                    0xb
247
248 #define PIO_RETRY_CNT                   750000 /* 1.5 s */
249 #define PIO_RETRY_DELAY                 2 /* 2 us*/
250
251 #define LINK_WAIT_MAX_RETRIES           10
252 #define LINK_WAIT_USLEEP_MIN            90000
253 #define LINK_WAIT_USLEEP_MAX            100000
254 #define RETRAIN_WAIT_MAX_RETRIES        10
255 #define RETRAIN_WAIT_USLEEP_US          2000
256
257 #define MSI_IRQ_NUM                     32
258
259 #define CFG_RD_CRS_VAL                  0xffff0001
260
261 struct advk_pcie {
262         struct platform_device *pdev;
263         void __iomem *base;
264         struct {
265                 phys_addr_t match;
266                 phys_addr_t remap;
267                 phys_addr_t mask;
268                 u32 actions;
269         } wins[OB_WIN_COUNT];
270         u8 wins_count;
271         int irq;
272         struct irq_domain *irq_domain;
273         struct irq_chip irq_chip;
274         raw_spinlock_t irq_lock;
275         struct irq_domain *msi_domain;
276         struct irq_domain *msi_inner_domain;
277         struct irq_chip msi_bottom_irq_chip;
278         struct irq_chip msi_irq_chip;
279         struct msi_domain_info msi_domain_info;
280         DECLARE_BITMAP(msi_used, MSI_IRQ_NUM);
281         struct mutex msi_used_lock;
282         u16 msi_msg;
283         int link_gen;
284         struct pci_bridge_emul bridge;
285         struct gpio_desc *reset_gpio;
286         struct phy *phy;
287 };
288
289 static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
290 {
291         writel(val, pcie->base + reg);
292 }
293
294 static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
295 {
296         return readl(pcie->base + reg);
297 }
298
299 static u8 advk_pcie_ltssm_state(struct advk_pcie *pcie)
300 {
301         u32 val;
302         u8 ltssm_state;
303
304         val = advk_readl(pcie, CFG_REG);
305         ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
306         return ltssm_state;
307 }
308
309 static inline bool advk_pcie_link_up(struct advk_pcie *pcie)
310 {
311         /* check if LTSSM is in normal operation - some L* state */
312         u8 ltssm_state = advk_pcie_ltssm_state(pcie);
313         return ltssm_state >= LTSSM_L0 && ltssm_state < LTSSM_DISABLED;
314 }
315
316 static inline bool advk_pcie_link_active(struct advk_pcie *pcie)
317 {
318         /*
319          * According to PCIe Base specification 3.0, Table 4-14: Link
320          * Status Mapped to the LTSSM, and 4.2.6.3.6 Configuration.Idle
321          * is Link Up mapped to LTSSM Configuration.Idle, Recovery, L0,
322          * L0s, L1 and L2 states. And according to 3.2.1. Data Link
323          * Control and Management State Machine Rules is DL Up status
324          * reported in DL Active state.
325          */
326         u8 ltssm_state = advk_pcie_ltssm_state(pcie);
327         return ltssm_state >= LTSSM_CONFIG_IDLE && ltssm_state < LTSSM_DISABLED;
328 }
329
330 static inline bool advk_pcie_link_training(struct advk_pcie *pcie)
331 {
332         /*
333          * According to PCIe Base specification 3.0, Table 4-14: Link
334          * Status Mapped to the LTSSM is Link Training mapped to LTSSM
335          * Configuration and Recovery states.
336          */
337         u8 ltssm_state = advk_pcie_ltssm_state(pcie);
338         return ((ltssm_state >= LTSSM_CONFIG_LINKWIDTH_START &&
339                  ltssm_state < LTSSM_L0) ||
340                 (ltssm_state >= LTSSM_RECOVERY_EQUALIZATION_PHASE0 &&
341                  ltssm_state <= LTSSM_RECOVERY_EQUALIZATION_PHASE3));
342 }
343
344 static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
345 {
346         int retries;
347
348         /* check if the link is up or not */
349         for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
350                 if (advk_pcie_link_up(pcie))
351                         return 0;
352
353                 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
354         }
355
356         return -ETIMEDOUT;
357 }
358
359 static void advk_pcie_wait_for_retrain(struct advk_pcie *pcie)
360 {
361         size_t retries;
362
363         for (retries = 0; retries < RETRAIN_WAIT_MAX_RETRIES; ++retries) {
364                 if (advk_pcie_link_training(pcie))
365                         break;
366                 udelay(RETRAIN_WAIT_USLEEP_US);
367         }
368 }
369
370 static void advk_pcie_issue_perst(struct advk_pcie *pcie)
371 {
372         if (!pcie->reset_gpio)
373                 return;
374
375         /* 10ms delay is needed for some cards */
376         dev_info(&pcie->pdev->dev, "issuing PERST via reset GPIO for 10ms\n");
377         gpiod_set_value_cansleep(pcie->reset_gpio, 1);
378         usleep_range(10000, 11000);
379         gpiod_set_value_cansleep(pcie->reset_gpio, 0);
380 }
381
382 static void advk_pcie_train_link(struct advk_pcie *pcie)
383 {
384         struct device *dev = &pcie->pdev->dev;
385         u32 reg;
386         int ret;
387
388         /*
389          * Setup PCIe rev / gen compliance based on device tree property
390          * 'max-link-speed' which also forces maximal link speed.
391          */
392         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
393         reg &= ~PCIE_GEN_SEL_MSK;
394         if (pcie->link_gen == 3)
395                 reg |= SPEED_GEN_3;
396         else if (pcie->link_gen == 2)
397                 reg |= SPEED_GEN_2;
398         else
399                 reg |= SPEED_GEN_1;
400         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
401
402         /*
403          * Set maximal link speed value also into PCIe Link Control 2 register.
404          * Armada 3700 Functional Specification says that default value is based
405          * on SPEED_GEN but tests showed that default value is always 8.0 GT/s.
406          */
407         reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2);
408         reg &= ~PCI_EXP_LNKCTL2_TLS;
409         if (pcie->link_gen == 3)
410                 reg |= PCI_EXP_LNKCTL2_TLS_8_0GT;
411         else if (pcie->link_gen == 2)
412                 reg |= PCI_EXP_LNKCTL2_TLS_5_0GT;
413         else
414                 reg |= PCI_EXP_LNKCTL2_TLS_2_5GT;
415         advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2);
416
417         /* Enable link training after selecting PCIe generation */
418         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
419         reg |= LINK_TRAINING_EN;
420         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
421
422         /*
423          * Reset PCIe card via PERST# signal. Some cards are not detected
424          * during link training when they are in some non-initial state.
425          */
426         advk_pcie_issue_perst(pcie);
427
428         /*
429          * PERST# signal could have been asserted by pinctrl subsystem before
430          * probe() callback has been called or issued explicitly by reset gpio
431          * function advk_pcie_issue_perst(), making the endpoint going into
432          * fundamental reset. As required by PCI Express spec (PCI Express
433          * Base Specification, REV. 4.0 PCI Express, February 19 2014, 6.6.1
434          * Conventional Reset) a delay for at least 100ms after such a reset
435          * before sending a Configuration Request to the device is needed.
436          * So wait until PCIe link is up. Function advk_pcie_wait_for_link()
437          * waits for link at least 900ms.
438          */
439         ret = advk_pcie_wait_for_link(pcie);
440         if (ret < 0)
441                 dev_err(dev, "link never came up\n");
442         else
443                 dev_info(dev, "link up\n");
444 }
445
446 /*
447  * Set PCIe address window register which could be used for memory
448  * mapping.
449  */
450 static void advk_pcie_set_ob_win(struct advk_pcie *pcie, u8 win_num,
451                                  phys_addr_t match, phys_addr_t remap,
452                                  phys_addr_t mask, u32 actions)
453 {
454         advk_writel(pcie, OB_WIN_ENABLE |
455                           lower_32_bits(match), OB_WIN_MATCH_LS(win_num));
456         advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num));
457         advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num));
458         advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num));
459         advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num));
460         advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num));
461         advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num));
462 }
463
464 static void advk_pcie_disable_ob_win(struct advk_pcie *pcie, u8 win_num)
465 {
466         advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num));
467         advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num));
468         advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num));
469         advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num));
470         advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num));
471         advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num));
472         advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num));
473 }
474
475 static void advk_pcie_setup_hw(struct advk_pcie *pcie)
476 {
477         u32 reg;
478         int i;
479
480         /*
481          * Configure PCIe Reference clock. Direction is from the PCIe
482          * controller to the endpoint card, so enable transmitting of
483          * Reference clock differential signal off-chip and disable
484          * receiving off-chip differential signal.
485          */
486         reg = advk_readl(pcie, PCIE_CORE_REF_CLK_REG);
487         reg |= PCIE_CORE_REF_CLK_TX_ENABLE;
488         reg &= ~PCIE_CORE_REF_CLK_RX_ENABLE;
489         advk_writel(pcie, reg, PCIE_CORE_REF_CLK_REG);
490
491         /* Set to Direct mode */
492         reg = advk_readl(pcie, CTRL_CONFIG_REG);
493         reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
494         reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
495         advk_writel(pcie, reg, CTRL_CONFIG_REG);
496
497         /* Set PCI global control register to RC mode */
498         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
499         reg |= (IS_RC_MSK << IS_RC_SHIFT);
500         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
501
502         /*
503          * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab.
504          * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor
505          * id in high 16 bits. Updating this register changes readback value of
506          * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround
507          * for erratum 4.1: "The value of device and vendor ID is incorrect".
508          */
509         reg = (PCI_VENDOR_ID_MARVELL << 16) | PCI_VENDOR_ID_MARVELL;
510         advk_writel(pcie, reg, VENDOR_ID_REG);
511
512         /*
513          * Change Class Code of PCI Bridge device to PCI Bridge (0x600400),
514          * because the default value is Mass storage controller (0x010400).
515          *
516          * Note that this Aardvark PCI Bridge does not have compliant Type 1
517          * Configuration Space and it even cannot be accessed via Aardvark's
518          * PCI config space access method. Something like config space is
519          * available in internal Aardvark registers starting at offset 0x0
520          * and is reported as Type 0. In range 0x10 - 0x34 it has totally
521          * different registers.
522          *
523          * Therefore driver uses emulation of PCI Bridge which emulates
524          * access to configuration space via internal Aardvark registers or
525          * emulated configuration buffer.
526          */
527         reg = advk_readl(pcie, PCIE_CORE_DEV_REV_REG);
528         reg &= ~0xffffff00;
529         reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8;
530         advk_writel(pcie, reg, PCIE_CORE_DEV_REV_REG);
531
532         /* Disable Root Bridge I/O space, memory space and bus mastering */
533         reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
534         reg &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
535         advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
536
537         /* Set Advanced Error Capabilities and Control PF0 register */
538         reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
539                 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
540                 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
541                 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
542         advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
543
544         /* Set PCIe Device Control register */
545         reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL);
546         reg &= ~PCI_EXP_DEVCTL_RELAX_EN;
547         reg &= ~PCI_EXP_DEVCTL_NOSNOOP_EN;
548         reg &= ~PCI_EXP_DEVCTL_PAYLOAD;
549         reg &= ~PCI_EXP_DEVCTL_READRQ;
550         reg |= PCI_EXP_DEVCTL_PAYLOAD_512B;
551         reg |= PCI_EXP_DEVCTL_READRQ_512B;
552         advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL);
553
554         /* Program PCIe Control 2 to disable strict ordering */
555         reg = PCIE_CORE_CTRL2_RESERVED |
556                 PCIE_CORE_CTRL2_TD_ENABLE;
557         advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
558
559         /* Set lane X1 */
560         reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
561         reg &= ~LANE_CNT_MSK;
562         reg |= LANE_COUNT_1;
563         advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
564
565         /* Enable MSI */
566         reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
567         reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
568         advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
569
570         /* Clear all interrupts */
571         advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG);
572         advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
573         advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
574         advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
575
576         /* Disable All ISR0/1 Sources */
577         reg = PCIE_ISR0_ALL_MASK;
578         reg &= ~PCIE_ISR0_MSI_INT_PENDING;
579         advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
580
581         advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
582
583         /* Unmask all MSIs */
584         advk_writel(pcie, ~(u32)PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG);
585
586         /* Enable summary interrupt for GIC SPI source */
587         reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
588         advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
589
590         /*
591          * Enable AXI address window location generation:
592          * When it is enabled, the default outbound window
593          * configurations (Default User Field: 0xD0074CFC)
594          * are used to transparent address translation for
595          * the outbound transactions. Thus, PCIe address
596          * windows are not required for transparent memory
597          * access when default outbound window configuration
598          * is set for memory access.
599          */
600         reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
601         reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
602         advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
603
604         /*
605          * Set memory access in Default User Field so it
606          * is not required to configure PCIe address for
607          * transparent memory access.
608          */
609         advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS);
610
611         /*
612          * Bypass the address window mapping for PIO:
613          * Since PIO access already contains all required
614          * info over AXI interface by PIO registers, the
615          * address window is not required.
616          */
617         reg = advk_readl(pcie, PIO_CTRL);
618         reg |= PIO_CTRL_ADDR_WIN_DISABLE;
619         advk_writel(pcie, reg, PIO_CTRL);
620
621         /*
622          * Configure PCIe address windows for non-memory or
623          * non-transparent access as by default PCIe uses
624          * transparent memory access.
625          */
626         for (i = 0; i < pcie->wins_count; i++)
627                 advk_pcie_set_ob_win(pcie, i,
628                                      pcie->wins[i].match, pcie->wins[i].remap,
629                                      pcie->wins[i].mask, pcie->wins[i].actions);
630
631         /* Disable remaining PCIe outbound windows */
632         for (i = pcie->wins_count; i < OB_WIN_COUNT; i++)
633                 advk_pcie_disable_ob_win(pcie, i);
634
635         advk_pcie_train_link(pcie);
636 }
637
638 static int advk_pcie_check_pio_status(struct advk_pcie *pcie, bool allow_crs, u32 *val)
639 {
640         struct device *dev = &pcie->pdev->dev;
641         u32 reg;
642         unsigned int status;
643         char *strcomp_status, *str_posted;
644         int ret;
645
646         reg = advk_readl(pcie, PIO_STAT);
647         status = (reg & PIO_COMPLETION_STATUS_MASK) >>
648                 PIO_COMPLETION_STATUS_SHIFT;
649
650         /*
651          * According to HW spec, the PIO status check sequence as below:
652          * 1) even if COMPLETION_STATUS(bit9:7) indicates successful,
653          *    it still needs to check Error Status(bit11), only when this bit
654          *    indicates no error happen, the operation is successful.
655          * 2) value Unsupported Request(1) of COMPLETION_STATUS(bit9:7) only
656          *    means a PIO write error, and for PIO read it is successful with
657          *    a read value of 0xFFFFFFFF.
658          * 3) value Completion Retry Status(CRS) of COMPLETION_STATUS(bit9:7)
659          *    only means a PIO write error, and for PIO read it is successful
660          *    with a read value of 0xFFFF0001.
661          * 4) value Completer Abort (CA) of COMPLETION_STATUS(bit9:7) means
662          *    error for both PIO read and PIO write operation.
663          * 5) other errors are indicated as 'unknown'.
664          */
665         switch (status) {
666         case PIO_COMPLETION_STATUS_OK:
667                 if (reg & PIO_ERR_STATUS) {
668                         strcomp_status = "COMP_ERR";
669                         ret = -EFAULT;
670                         break;
671                 }
672                 /* Get the read result */
673                 if (val)
674                         *val = advk_readl(pcie, PIO_RD_DATA);
675                 /* No error */
676                 strcomp_status = NULL;
677                 ret = 0;
678                 break;
679         case PIO_COMPLETION_STATUS_UR:
680                 strcomp_status = "UR";
681                 ret = -EOPNOTSUPP;
682                 break;
683         case PIO_COMPLETION_STATUS_CRS:
684                 if (allow_crs && val) {
685                         /* PCIe r4.0, sec 2.3.2, says:
686                          * If CRS Software Visibility is enabled:
687                          * For a Configuration Read Request that includes both
688                          * bytes of the Vendor ID field of a device Function's
689                          * Configuration Space Header, the Root Complex must
690                          * complete the Request to the host by returning a
691                          * read-data value of 0001h for the Vendor ID field and
692                          * all '1's for any additional bytes included in the
693                          * request.
694                          *
695                          * So CRS in this case is not an error status.
696                          */
697                         *val = CFG_RD_CRS_VAL;
698                         strcomp_status = NULL;
699                         ret = 0;
700                         break;
701                 }
702                 /* PCIe r4.0, sec 2.3.2, says:
703                  * If CRS Software Visibility is not enabled, the Root Complex
704                  * must re-issue the Configuration Request as a new Request.
705                  * If CRS Software Visibility is enabled: For a Configuration
706                  * Write Request or for any other Configuration Read Request,
707                  * the Root Complex must re-issue the Configuration Request as
708                  * a new Request.
709                  * A Root Complex implementation may choose to limit the number
710                  * of Configuration Request/CRS Completion Status loops before
711                  * determining that something is wrong with the target of the
712                  * Request and taking appropriate action, e.g., complete the
713                  * Request to the host as a failed transaction.
714                  *
715                  * So return -EAGAIN and caller (pci-aardvark.c driver) will
716                  * re-issue request again up to the PIO_RETRY_CNT retries.
717                  */
718                 strcomp_status = "CRS";
719                 ret = -EAGAIN;
720                 break;
721         case PIO_COMPLETION_STATUS_CA:
722                 strcomp_status = "CA";
723                 ret = -ECANCELED;
724                 break;
725         default:
726                 strcomp_status = "Unknown";
727                 ret = -EINVAL;
728                 break;
729         }
730
731         if (!strcomp_status)
732                 return ret;
733
734         if (reg & PIO_NON_POSTED_REQ)
735                 str_posted = "Non-posted";
736         else
737                 str_posted = "Posted";
738
739         dev_dbg(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
740                 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
741
742         return ret;
743 }
744
745 static int advk_pcie_wait_pio(struct advk_pcie *pcie)
746 {
747         struct device *dev = &pcie->pdev->dev;
748         int i;
749
750         for (i = 1; i <= PIO_RETRY_CNT; i++) {
751                 u32 start, isr;
752
753                 start = advk_readl(pcie, PIO_START);
754                 isr = advk_readl(pcie, PIO_ISR);
755                 if (!start && isr)
756                         return i;
757                 udelay(PIO_RETRY_DELAY);
758         }
759
760         dev_err(dev, "PIO read/write transfer time out\n");
761         return -ETIMEDOUT;
762 }
763
764 static pci_bridge_emul_read_status_t
765 advk_pci_bridge_emul_base_conf_read(struct pci_bridge_emul *bridge,
766                                     int reg, u32 *value)
767 {
768         struct advk_pcie *pcie = bridge->data;
769
770         switch (reg) {
771         case PCI_COMMAND:
772                 *value = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
773                 return PCI_BRIDGE_EMUL_HANDLED;
774
775         case PCI_INTERRUPT_LINE: {
776                 /*
777                  * From the whole 32bit register we support reading from HW only
778                  * one bit: PCI_BRIDGE_CTL_BUS_RESET.
779                  * Other bits are retrieved only from emulated config buffer.
780                  */
781                 __le32 *cfgspace = (__le32 *)&bridge->conf;
782                 u32 val = le32_to_cpu(cfgspace[PCI_INTERRUPT_LINE / 4]);
783                 if (advk_readl(pcie, PCIE_CORE_CTRL1_REG) & HOT_RESET_GEN)
784                         val |= PCI_BRIDGE_CTL_BUS_RESET << 16;
785                 else
786                         val &= ~(PCI_BRIDGE_CTL_BUS_RESET << 16);
787                 *value = val;
788                 return PCI_BRIDGE_EMUL_HANDLED;
789         }
790
791         default:
792                 return PCI_BRIDGE_EMUL_NOT_HANDLED;
793         }
794 }
795
796 static void
797 advk_pci_bridge_emul_base_conf_write(struct pci_bridge_emul *bridge,
798                                      int reg, u32 old, u32 new, u32 mask)
799 {
800         struct advk_pcie *pcie = bridge->data;
801
802         switch (reg) {
803         case PCI_COMMAND:
804                 advk_writel(pcie, new, PCIE_CORE_CMD_STATUS_REG);
805                 break;
806
807         case PCI_INTERRUPT_LINE:
808                 if (mask & (PCI_BRIDGE_CTL_BUS_RESET << 16)) {
809                         u32 val = advk_readl(pcie, PCIE_CORE_CTRL1_REG);
810                         if (new & (PCI_BRIDGE_CTL_BUS_RESET << 16))
811                                 val |= HOT_RESET_GEN;
812                         else
813                                 val &= ~HOT_RESET_GEN;
814                         advk_writel(pcie, val, PCIE_CORE_CTRL1_REG);
815                 }
816                 break;
817
818         default:
819                 break;
820         }
821 }
822
823 static pci_bridge_emul_read_status_t
824 advk_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
825                                     int reg, u32 *value)
826 {
827         struct advk_pcie *pcie = bridge->data;
828
829
830         switch (reg) {
831         case PCI_EXP_SLTCTL:
832                 *value = PCI_EXP_SLTSTA_PDS << 16;
833                 return PCI_BRIDGE_EMUL_HANDLED;
834
835         case PCI_EXP_RTCTL: {
836                 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG);
837                 *value = (val & PCIE_MSG_PM_PME_MASK) ? 0 : PCI_EXP_RTCTL_PMEIE;
838                 *value |= le16_to_cpu(bridge->pcie_conf.rootctl) & PCI_EXP_RTCTL_CRSSVE;
839                 *value |= PCI_EXP_RTCAP_CRSVIS << 16;
840                 return PCI_BRIDGE_EMUL_HANDLED;
841         }
842
843         case PCI_EXP_RTSTA: {
844                 u32 isr0 = advk_readl(pcie, PCIE_ISR0_REG);
845                 u32 msglog = advk_readl(pcie, PCIE_MSG_LOG_REG);
846                 *value = msglog >> 16;
847                 if (isr0 & PCIE_MSG_PM_PME_MASK)
848                         *value |= PCI_EXP_RTSTA_PME;
849                 return PCI_BRIDGE_EMUL_HANDLED;
850         }
851
852         case PCI_EXP_LNKCAP: {
853                 u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg);
854                 /*
855                  * PCI_EXP_LNKCAP_DLLLARC bit is hardwired in aardvark HW to 0.
856                  * But support for PCI_EXP_LNKSTA_DLLLA is emulated via ltssm
857                  * state so explicitly enable PCI_EXP_LNKCAP_DLLLARC flag.
858                  */
859                 val |= PCI_EXP_LNKCAP_DLLLARC;
860                 *value = val;
861                 return PCI_BRIDGE_EMUL_HANDLED;
862         }
863
864         case PCI_EXP_LNKCTL: {
865                 /* u32 contains both PCI_EXP_LNKCTL and PCI_EXP_LNKSTA */
866                 u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg) &
867                         ~(PCI_EXP_LNKSTA_LT << 16);
868                 if (advk_pcie_link_training(pcie))
869                         val |= (PCI_EXP_LNKSTA_LT << 16);
870                 if (advk_pcie_link_active(pcie))
871                         val |= (PCI_EXP_LNKSTA_DLLLA << 16);
872                 *value = val;
873                 return PCI_BRIDGE_EMUL_HANDLED;
874         }
875
876         case PCI_EXP_DEVCAP:
877         case PCI_EXP_DEVCTL:
878         case PCI_EXP_DEVCAP2:
879         case PCI_EXP_DEVCTL2:
880         case PCI_EXP_LNKCAP2:
881         case PCI_EXP_LNKCTL2:
882                 *value = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg);
883                 return PCI_BRIDGE_EMUL_HANDLED;
884
885         default:
886                 return PCI_BRIDGE_EMUL_NOT_HANDLED;
887         }
888
889 }
890
891 static void
892 advk_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge,
893                                      int reg, u32 old, u32 new, u32 mask)
894 {
895         struct advk_pcie *pcie = bridge->data;
896
897         switch (reg) {
898         case PCI_EXP_LNKCTL:
899                 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
900                 if (new & PCI_EXP_LNKCTL_RL)
901                         advk_pcie_wait_for_retrain(pcie);
902                 break;
903
904         case PCI_EXP_RTCTL: {
905                 /* Only mask/unmask PME interrupt */
906                 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG) &
907                         ~PCIE_MSG_PM_PME_MASK;
908                 if ((new & PCI_EXP_RTCTL_PMEIE) == 0)
909                         val |= PCIE_MSG_PM_PME_MASK;
910                 advk_writel(pcie, val, PCIE_ISR0_MASK_REG);
911                 break;
912         }
913
914         case PCI_EXP_RTSTA:
915                 new = (new & PCI_EXP_RTSTA_PME) >> 9;
916                 advk_writel(pcie, new, PCIE_ISR0_REG);
917                 break;
918
919         case PCI_EXP_DEVCTL:
920         case PCI_EXP_DEVCTL2:
921         case PCI_EXP_LNKCTL2:
922                 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
923                 break;
924
925         default:
926                 break;
927         }
928 }
929
930 static struct pci_bridge_emul_ops advk_pci_bridge_emul_ops = {
931         .read_base = advk_pci_bridge_emul_base_conf_read,
932         .write_base = advk_pci_bridge_emul_base_conf_write,
933         .read_pcie = advk_pci_bridge_emul_pcie_conf_read,
934         .write_pcie = advk_pci_bridge_emul_pcie_conf_write,
935 };
936
937 /*
938  * Initialize the configuration space of the PCI-to-PCI bridge
939  * associated with the given PCIe interface.
940  */
941 static int advk_sw_pci_bridge_init(struct advk_pcie *pcie)
942 {
943         struct pci_bridge_emul *bridge = &pcie->bridge;
944
945         bridge->conf.vendor =
946                 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) & 0xffff);
947         bridge->conf.device =
948                 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) >> 16);
949         bridge->conf.class_revision =
950                 cpu_to_le32(advk_readl(pcie, PCIE_CORE_DEV_REV_REG) & 0xff);
951
952         /* Support 32 bits I/O addressing */
953         bridge->conf.iobase = PCI_IO_RANGE_TYPE_32;
954         bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32;
955
956         /* Support 64 bits memory pref */
957         bridge->conf.pref_mem_base = cpu_to_le16(PCI_PREF_RANGE_TYPE_64);
958         bridge->conf.pref_mem_limit = cpu_to_le16(PCI_PREF_RANGE_TYPE_64);
959
960         /* Support interrupt A for MSI feature */
961         bridge->conf.intpin = PCI_INTERRUPT_INTA;
962
963         /* Aardvark HW provides PCIe Capability structure in version 2 */
964         bridge->pcie_conf.cap = cpu_to_le16(2);
965
966         /* Indicates supports for Completion Retry Status */
967         bridge->pcie_conf.rootcap = cpu_to_le16(PCI_EXP_RTCAP_CRSVIS);
968
969         bridge->has_pcie = true;
970         bridge->data = pcie;
971         bridge->ops = &advk_pci_bridge_emul_ops;
972
973         return pci_bridge_emul_init(bridge, 0);
974 }
975
976 static bool advk_pcie_valid_device(struct advk_pcie *pcie, struct pci_bus *bus,
977                                   int devfn)
978 {
979         if (pci_is_root_bus(bus) && PCI_SLOT(devfn) != 0)
980                 return false;
981
982         /*
983          * If the link goes down after we check for link-up, nothing bad
984          * happens but the config access times out.
985          */
986         if (!pci_is_root_bus(bus) && !advk_pcie_link_up(pcie))
987                 return false;
988
989         return true;
990 }
991
992 static bool advk_pcie_pio_is_running(struct advk_pcie *pcie)
993 {
994         struct device *dev = &pcie->pdev->dev;
995
996         /*
997          * Trying to start a new PIO transfer when previous has not completed
998          * cause External Abort on CPU which results in kernel panic:
999          *
1000          *     SError Interrupt on CPU0, code 0xbf000002 -- SError
1001          *     Kernel panic - not syncing: Asynchronous SError Interrupt
1002          *
1003          * Functions advk_pcie_rd_conf() and advk_pcie_wr_conf() are protected
1004          * by raw_spin_lock_irqsave() at pci_lock_config() level to prevent
1005          * concurrent calls at the same time. But because PIO transfer may take
1006          * about 1.5s when link is down or card is disconnected, it means that
1007          * advk_pcie_wait_pio() does not always have to wait for completion.
1008          *
1009          * Some versions of ARM Trusted Firmware handles this External Abort at
1010          * EL3 level and mask it to prevent kernel panic. Relevant TF-A commit:
1011          * https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=3c7dcdac5c50
1012          */
1013         if (advk_readl(pcie, PIO_START)) {
1014                 dev_err(dev, "Previous PIO read/write transfer is still running\n");
1015                 return true;
1016         }
1017
1018         return false;
1019 }
1020
1021 static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
1022                              int where, int size, u32 *val)
1023 {
1024         struct advk_pcie *pcie = bus->sysdata;
1025         int retry_count;
1026         bool allow_crs;
1027         u32 reg;
1028         int ret;
1029
1030         if (!advk_pcie_valid_device(pcie, bus, devfn)) {
1031                 *val = 0xffffffff;
1032                 return PCIBIOS_DEVICE_NOT_FOUND;
1033         }
1034
1035         if (pci_is_root_bus(bus))
1036                 return pci_bridge_emul_conf_read(&pcie->bridge, where,
1037                                                  size, val);
1038
1039         /*
1040          * Completion Retry Status is possible to return only when reading all
1041          * 4 bytes from PCI_VENDOR_ID and PCI_DEVICE_ID registers at once and
1042          * CRSSVE flag on Root Bridge is enabled.
1043          */
1044         allow_crs = (where == PCI_VENDOR_ID) && (size == 4) &&
1045                     (le16_to_cpu(pcie->bridge.pcie_conf.rootctl) &
1046                      PCI_EXP_RTCTL_CRSSVE);
1047
1048         if (advk_pcie_pio_is_running(pcie))
1049                 goto try_crs;
1050
1051         /* Program the control register */
1052         reg = advk_readl(pcie, PIO_CTRL);
1053         reg &= ~PIO_CTRL_TYPE_MASK;
1054         if (pci_is_root_bus(bus->parent))
1055                 reg |= PCIE_CONFIG_RD_TYPE0;
1056         else
1057                 reg |= PCIE_CONFIG_RD_TYPE1;
1058         advk_writel(pcie, reg, PIO_CTRL);
1059
1060         /* Program the address registers */
1061         reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4);
1062         advk_writel(pcie, reg, PIO_ADDR_LS);
1063         advk_writel(pcie, 0, PIO_ADDR_MS);
1064
1065         /* Program the data strobe */
1066         advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
1067
1068         retry_count = 0;
1069         do {
1070                 /* Clear PIO DONE ISR and start the transfer */
1071                 advk_writel(pcie, 1, PIO_ISR);
1072                 advk_writel(pcie, 1, PIO_START);
1073
1074                 ret = advk_pcie_wait_pio(pcie);
1075                 if (ret < 0)
1076                         goto try_crs;
1077
1078                 retry_count += ret;
1079
1080                 /* Check PIO status and get the read result */
1081                 ret = advk_pcie_check_pio_status(pcie, allow_crs, val);
1082         } while (ret == -EAGAIN && retry_count < PIO_RETRY_CNT);
1083
1084         if (ret < 0)
1085                 goto fail;
1086
1087         if (size == 1)
1088                 *val = (*val >> (8 * (where & 3))) & 0xff;
1089         else if (size == 2)
1090                 *val = (*val >> (8 * (where & 3))) & 0xffff;
1091
1092         return PCIBIOS_SUCCESSFUL;
1093
1094 try_crs:
1095         /*
1096          * If it is possible, return Completion Retry Status so that caller
1097          * tries to issue the request again instead of failing.
1098          */
1099         if (allow_crs) {
1100                 *val = CFG_RD_CRS_VAL;
1101                 return PCIBIOS_SUCCESSFUL;
1102         }
1103
1104 fail:
1105         *val = 0xffffffff;
1106         return PCIBIOS_SET_FAILED;
1107 }
1108
1109 static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
1110                                 int where, int size, u32 val)
1111 {
1112         struct advk_pcie *pcie = bus->sysdata;
1113         u32 reg;
1114         u32 data_strobe = 0x0;
1115         int retry_count;
1116         int offset;
1117         int ret;
1118
1119         if (!advk_pcie_valid_device(pcie, bus, devfn))
1120                 return PCIBIOS_DEVICE_NOT_FOUND;
1121
1122         if (pci_is_root_bus(bus))
1123                 return pci_bridge_emul_conf_write(&pcie->bridge, where,
1124                                                   size, val);
1125
1126         if (where % size)
1127                 return PCIBIOS_SET_FAILED;
1128
1129         if (advk_pcie_pio_is_running(pcie))
1130                 return PCIBIOS_SET_FAILED;
1131
1132         /* Program the control register */
1133         reg = advk_readl(pcie, PIO_CTRL);
1134         reg &= ~PIO_CTRL_TYPE_MASK;
1135         if (pci_is_root_bus(bus->parent))
1136                 reg |= PCIE_CONFIG_WR_TYPE0;
1137         else
1138                 reg |= PCIE_CONFIG_WR_TYPE1;
1139         advk_writel(pcie, reg, PIO_CTRL);
1140
1141         /* Program the address registers */
1142         reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4);
1143         advk_writel(pcie, reg, PIO_ADDR_LS);
1144         advk_writel(pcie, 0, PIO_ADDR_MS);
1145
1146         /* Calculate the write strobe */
1147         offset      = where & 0x3;
1148         reg         = val << (8 * offset);
1149         data_strobe = GENMASK(size - 1, 0) << offset;
1150
1151         /* Program the data register */
1152         advk_writel(pcie, reg, PIO_WR_DATA);
1153
1154         /* Program the data strobe */
1155         advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
1156
1157         retry_count = 0;
1158         do {
1159                 /* Clear PIO DONE ISR and start the transfer */
1160                 advk_writel(pcie, 1, PIO_ISR);
1161                 advk_writel(pcie, 1, PIO_START);
1162
1163                 ret = advk_pcie_wait_pio(pcie);
1164                 if (ret < 0)
1165                         return PCIBIOS_SET_FAILED;
1166
1167                 retry_count += ret;
1168
1169                 ret = advk_pcie_check_pio_status(pcie, false, NULL);
1170         } while (ret == -EAGAIN && retry_count < PIO_RETRY_CNT);
1171
1172         return ret < 0 ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL;
1173 }
1174
1175 static struct pci_ops advk_pcie_ops = {
1176         .read = advk_pcie_rd_conf,
1177         .write = advk_pcie_wr_conf,
1178 };
1179
1180 static void advk_msi_irq_compose_msi_msg(struct irq_data *data,
1181                                          struct msi_msg *msg)
1182 {
1183         struct advk_pcie *pcie = irq_data_get_irq_chip_data(data);
1184         phys_addr_t msi_msg = virt_to_phys(&pcie->msi_msg);
1185
1186         msg->address_lo = lower_32_bits(msi_msg);
1187         msg->address_hi = upper_32_bits(msi_msg);
1188         msg->data = data->hwirq;
1189 }
1190
1191 static int advk_msi_set_affinity(struct irq_data *irq_data,
1192                                  const struct cpumask *mask, bool force)
1193 {
1194         return -EINVAL;
1195 }
1196
1197 static int advk_msi_irq_domain_alloc(struct irq_domain *domain,
1198                                      unsigned int virq,
1199                                      unsigned int nr_irqs, void *args)
1200 {
1201         struct advk_pcie *pcie = domain->host_data;
1202         int hwirq, i;
1203
1204         mutex_lock(&pcie->msi_used_lock);
1205         hwirq = bitmap_find_free_region(pcie->msi_used, MSI_IRQ_NUM,
1206                                         order_base_2(nr_irqs));
1207         mutex_unlock(&pcie->msi_used_lock);
1208         if (hwirq < 0)
1209                 return -ENOSPC;
1210
1211         for (i = 0; i < nr_irqs; i++)
1212                 irq_domain_set_info(domain, virq + i, hwirq + i,
1213                                     &pcie->msi_bottom_irq_chip,
1214                                     domain->host_data, handle_simple_irq,
1215                                     NULL, NULL);
1216
1217         return 0;
1218 }
1219
1220 static void advk_msi_irq_domain_free(struct irq_domain *domain,
1221                                      unsigned int virq, unsigned int nr_irqs)
1222 {
1223         struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1224         struct advk_pcie *pcie = domain->host_data;
1225
1226         mutex_lock(&pcie->msi_used_lock);
1227         bitmap_release_region(pcie->msi_used, d->hwirq, order_base_2(nr_irqs));
1228         mutex_unlock(&pcie->msi_used_lock);
1229 }
1230
1231 static const struct irq_domain_ops advk_msi_domain_ops = {
1232         .alloc = advk_msi_irq_domain_alloc,
1233         .free = advk_msi_irq_domain_free,
1234 };
1235
1236 static void advk_pcie_irq_mask(struct irq_data *d)
1237 {
1238         struct advk_pcie *pcie = d->domain->host_data;
1239         irq_hw_number_t hwirq = irqd_to_hwirq(d);
1240         unsigned long flags;
1241         u32 mask;
1242
1243         raw_spin_lock_irqsave(&pcie->irq_lock, flags);
1244         mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
1245         mask |= PCIE_ISR1_INTX_ASSERT(hwirq);
1246         advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
1247         raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
1248 }
1249
1250 static void advk_pcie_irq_unmask(struct irq_data *d)
1251 {
1252         struct advk_pcie *pcie = d->domain->host_data;
1253         irq_hw_number_t hwirq = irqd_to_hwirq(d);
1254         unsigned long flags;
1255         u32 mask;
1256
1257         raw_spin_lock_irqsave(&pcie->irq_lock, flags);
1258         mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
1259         mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq);
1260         advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
1261         raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
1262 }
1263
1264 static int advk_pcie_irq_map(struct irq_domain *h,
1265                              unsigned int virq, irq_hw_number_t hwirq)
1266 {
1267         struct advk_pcie *pcie = h->host_data;
1268
1269         advk_pcie_irq_mask(irq_get_irq_data(virq));
1270         irq_set_status_flags(virq, IRQ_LEVEL);
1271         irq_set_chip_and_handler(virq, &pcie->irq_chip,
1272                                  handle_level_irq);
1273         irq_set_chip_data(virq, pcie);
1274
1275         return 0;
1276 }
1277
1278 static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
1279         .map = advk_pcie_irq_map,
1280         .xlate = irq_domain_xlate_onecell,
1281 };
1282
1283 static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
1284 {
1285         struct device *dev = &pcie->pdev->dev;
1286         struct device_node *node = dev->of_node;
1287         struct irq_chip *bottom_ic, *msi_ic;
1288         struct msi_domain_info *msi_di;
1289         phys_addr_t msi_msg_phys;
1290
1291         mutex_init(&pcie->msi_used_lock);
1292
1293         bottom_ic = &pcie->msi_bottom_irq_chip;
1294
1295         bottom_ic->name = "MSI";
1296         bottom_ic->irq_compose_msi_msg = advk_msi_irq_compose_msi_msg;
1297         bottom_ic->irq_set_affinity = advk_msi_set_affinity;
1298
1299         msi_ic = &pcie->msi_irq_chip;
1300         msi_ic->name = "advk-MSI";
1301
1302         msi_di = &pcie->msi_domain_info;
1303         msi_di->flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
1304                 MSI_FLAG_MULTI_PCI_MSI;
1305         msi_di->chip = msi_ic;
1306
1307         msi_msg_phys = virt_to_phys(&pcie->msi_msg);
1308
1309         advk_writel(pcie, lower_32_bits(msi_msg_phys),
1310                     PCIE_MSI_ADDR_LOW_REG);
1311         advk_writel(pcie, upper_32_bits(msi_msg_phys),
1312                     PCIE_MSI_ADDR_HIGH_REG);
1313
1314         pcie->msi_inner_domain =
1315                 irq_domain_add_linear(NULL, MSI_IRQ_NUM,
1316                                       &advk_msi_domain_ops, pcie);
1317         if (!pcie->msi_inner_domain)
1318                 return -ENOMEM;
1319
1320         pcie->msi_domain =
1321                 pci_msi_create_irq_domain(of_node_to_fwnode(node),
1322                                           msi_di, pcie->msi_inner_domain);
1323         if (!pcie->msi_domain) {
1324                 irq_domain_remove(pcie->msi_inner_domain);
1325                 return -ENOMEM;
1326         }
1327
1328         return 0;
1329 }
1330
1331 static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
1332 {
1333         irq_domain_remove(pcie->msi_domain);
1334         irq_domain_remove(pcie->msi_inner_domain);
1335 }
1336
1337 static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
1338 {
1339         struct device *dev = &pcie->pdev->dev;
1340         struct device_node *node = dev->of_node;
1341         struct device_node *pcie_intc_node;
1342         struct irq_chip *irq_chip;
1343         int ret = 0;
1344
1345         raw_spin_lock_init(&pcie->irq_lock);
1346
1347         pcie_intc_node =  of_get_next_child(node, NULL);
1348         if (!pcie_intc_node) {
1349                 dev_err(dev, "No PCIe Intc node found\n");
1350                 return -ENODEV;
1351         }
1352
1353         irq_chip = &pcie->irq_chip;
1354
1355         irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
1356                                         dev_name(dev));
1357         if (!irq_chip->name) {
1358                 ret = -ENOMEM;
1359                 goto out_put_node;
1360         }
1361
1362         irq_chip->irq_mask = advk_pcie_irq_mask;
1363         irq_chip->irq_mask_ack = advk_pcie_irq_mask;
1364         irq_chip->irq_unmask = advk_pcie_irq_unmask;
1365
1366         pcie->irq_domain =
1367                 irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
1368                                       &advk_pcie_irq_domain_ops, pcie);
1369         if (!pcie->irq_domain) {
1370                 dev_err(dev, "Failed to get a INTx IRQ domain\n");
1371                 ret = -ENOMEM;
1372                 goto out_put_node;
1373         }
1374
1375 out_put_node:
1376         of_node_put(pcie_intc_node);
1377         return ret;
1378 }
1379
1380 static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
1381 {
1382         irq_domain_remove(pcie->irq_domain);
1383 }
1384
1385 static void advk_pcie_handle_msi(struct advk_pcie *pcie)
1386 {
1387         u32 msi_val, msi_mask, msi_status, msi_idx;
1388
1389         msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
1390         msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
1391         msi_status = msi_val & ((~msi_mask) & PCIE_MSI_ALL_MASK);
1392
1393         for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
1394                 if (!(BIT(msi_idx) & msi_status))
1395                         continue;
1396
1397                 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
1398                 if (generic_handle_domain_irq(pcie->msi_inner_domain, msi_idx) == -EINVAL)
1399                         dev_err_ratelimited(&pcie->pdev->dev, "unexpected MSI 0x%02x\n", msi_idx);
1400         }
1401
1402         advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
1403                     PCIE_ISR0_REG);
1404 }
1405
1406 static void advk_pcie_handle_int(struct advk_pcie *pcie)
1407 {
1408         u32 isr0_val, isr0_mask, isr0_status;
1409         u32 isr1_val, isr1_mask, isr1_status;
1410         int i;
1411
1412         isr0_val = advk_readl(pcie, PCIE_ISR0_REG);
1413         isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
1414         isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK);
1415
1416         isr1_val = advk_readl(pcie, PCIE_ISR1_REG);
1417         isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
1418         isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK);
1419
1420         /* Process MSI interrupts */
1421         if (isr0_status & PCIE_ISR0_MSI_INT_PENDING)
1422                 advk_pcie_handle_msi(pcie);
1423
1424         /* Process legacy interrupts */
1425         for (i = 0; i < PCI_NUM_INTX; i++) {
1426                 if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i)))
1427                         continue;
1428
1429                 advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i),
1430                             PCIE_ISR1_REG);
1431
1432                 generic_handle_domain_irq(pcie->irq_domain, i);
1433         }
1434 }
1435
1436 static void advk_pcie_irq_handler(struct irq_desc *desc)
1437 {
1438         struct advk_pcie *pcie = irq_desc_get_handler_data(desc);
1439         struct irq_chip *chip = irq_desc_get_chip(desc);
1440         u32 val, mask, status;
1441
1442         chained_irq_enter(chip, desc);
1443
1444         val = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
1445         mask = advk_readl(pcie, HOST_CTRL_INT_MASK_REG);
1446         status = val & ((~mask) & PCIE_IRQ_ALL_MASK);
1447
1448         if (status & PCIE_IRQ_CORE_INT) {
1449                 advk_pcie_handle_int(pcie);
1450
1451                 /* Clear interrupt */
1452                 advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
1453         }
1454
1455         chained_irq_exit(chip, desc);
1456 }
1457
1458 static void __maybe_unused advk_pcie_disable_phy(struct advk_pcie *pcie)
1459 {
1460         phy_power_off(pcie->phy);
1461         phy_exit(pcie->phy);
1462 }
1463
1464 static int advk_pcie_enable_phy(struct advk_pcie *pcie)
1465 {
1466         int ret;
1467
1468         if (!pcie->phy)
1469                 return 0;
1470
1471         ret = phy_init(pcie->phy);
1472         if (ret)
1473                 return ret;
1474
1475         ret = phy_set_mode(pcie->phy, PHY_MODE_PCIE);
1476         if (ret) {
1477                 phy_exit(pcie->phy);
1478                 return ret;
1479         }
1480
1481         ret = phy_power_on(pcie->phy);
1482         if (ret == -EOPNOTSUPP) {
1483                 dev_warn(&pcie->pdev->dev, "PHY unsupported by firmware\n");
1484         } else if (ret) {
1485                 phy_exit(pcie->phy);
1486                 return ret;
1487         }
1488
1489         return 0;
1490 }
1491
1492 static int advk_pcie_setup_phy(struct advk_pcie *pcie)
1493 {
1494         struct device *dev = &pcie->pdev->dev;
1495         struct device_node *node = dev->of_node;
1496         int ret = 0;
1497
1498         pcie->phy = devm_of_phy_get(dev, node, NULL);
1499         if (IS_ERR(pcie->phy) && (PTR_ERR(pcie->phy) == -EPROBE_DEFER))
1500                 return PTR_ERR(pcie->phy);
1501
1502         /* Old bindings miss the PHY handle */
1503         if (IS_ERR(pcie->phy)) {
1504                 dev_warn(dev, "PHY unavailable (%ld)\n", PTR_ERR(pcie->phy));
1505                 pcie->phy = NULL;
1506                 return 0;
1507         }
1508
1509         ret = advk_pcie_enable_phy(pcie);
1510         if (ret)
1511                 dev_err(dev, "Failed to initialize PHY (%d)\n", ret);
1512
1513         return ret;
1514 }
1515
1516 static int advk_pcie_probe(struct platform_device *pdev)
1517 {
1518         struct device *dev = &pdev->dev;
1519         struct advk_pcie *pcie;
1520         struct pci_host_bridge *bridge;
1521         struct resource_entry *entry;
1522         int ret;
1523
1524         bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie));
1525         if (!bridge)
1526                 return -ENOMEM;
1527
1528         pcie = pci_host_bridge_priv(bridge);
1529         pcie->pdev = pdev;
1530         platform_set_drvdata(pdev, pcie);
1531
1532         resource_list_for_each_entry(entry, &bridge->windows) {
1533                 resource_size_t start = entry->res->start;
1534                 resource_size_t size = resource_size(entry->res);
1535                 unsigned long type = resource_type(entry->res);
1536                 u64 win_size;
1537
1538                 /*
1539                  * Aardvark hardware allows to configure also PCIe window
1540                  * for config type 0 and type 1 mapping, but driver uses
1541                  * only PIO for issuing configuration transfers which does
1542                  * not use PCIe window configuration.
1543                  */
1544                 if (type != IORESOURCE_MEM && type != IORESOURCE_IO)
1545                         continue;
1546
1547                 /*
1548                  * Skip transparent memory resources. Default outbound access
1549                  * configuration is set to transparent memory access so it
1550                  * does not need window configuration.
1551                  */
1552                 if (type == IORESOURCE_MEM && entry->offset == 0)
1553                         continue;
1554
1555                 /*
1556                  * The n-th PCIe window is configured by tuple (match, remap, mask)
1557                  * and an access to address A uses this window if A matches the
1558                  * match with given mask.
1559                  * So every PCIe window size must be a power of two and every start
1560                  * address must be aligned to window size. Minimal size is 64 KiB
1561                  * because lower 16 bits of mask must be zero. Remapped address
1562                  * may have set only bits from the mask.
1563                  */
1564                 while (pcie->wins_count < OB_WIN_COUNT && size > 0) {
1565                         /* Calculate the largest aligned window size */
1566                         win_size = (1ULL << (fls64(size)-1)) |
1567                                    (start ? (1ULL << __ffs64(start)) : 0);
1568                         win_size = 1ULL << __ffs64(win_size);
1569                         if (win_size < 0x10000)
1570                                 break;
1571
1572                         dev_dbg(dev,
1573                                 "Configuring PCIe window %d: [0x%llx-0x%llx] as %lu\n",
1574                                 pcie->wins_count, (unsigned long long)start,
1575                                 (unsigned long long)start + win_size, type);
1576
1577                         if (type == IORESOURCE_IO) {
1578                                 pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_IO;
1579                                 pcie->wins[pcie->wins_count].match = pci_pio_to_address(start);
1580                         } else {
1581                                 pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_MEM;
1582                                 pcie->wins[pcie->wins_count].match = start;
1583                         }
1584                         pcie->wins[pcie->wins_count].remap = start - entry->offset;
1585                         pcie->wins[pcie->wins_count].mask = ~(win_size - 1);
1586
1587                         if (pcie->wins[pcie->wins_count].remap & (win_size - 1))
1588                                 break;
1589
1590                         start += win_size;
1591                         size -= win_size;
1592                         pcie->wins_count++;
1593                 }
1594
1595                 if (size > 0) {
1596                         dev_err(&pcie->pdev->dev,
1597                                 "Invalid PCIe region [0x%llx-0x%llx]\n",
1598                                 (unsigned long long)entry->res->start,
1599                                 (unsigned long long)entry->res->end + 1);
1600                         return -EINVAL;
1601                 }
1602         }
1603
1604         pcie->base = devm_platform_ioremap_resource(pdev, 0);
1605         if (IS_ERR(pcie->base))
1606                 return PTR_ERR(pcie->base);
1607
1608         pcie->irq = platform_get_irq(pdev, 0);
1609         if (pcie->irq < 0)
1610                 return pcie->irq;
1611
1612         pcie->reset_gpio = devm_gpiod_get_from_of_node(dev, dev->of_node,
1613                                                        "reset-gpios", 0,
1614                                                        GPIOD_OUT_LOW,
1615                                                        "pcie1-reset");
1616         ret = PTR_ERR_OR_ZERO(pcie->reset_gpio);
1617         if (ret) {
1618                 if (ret == -ENOENT) {
1619                         pcie->reset_gpio = NULL;
1620                 } else {
1621                         if (ret != -EPROBE_DEFER)
1622                                 dev_err(dev, "Failed to get reset-gpio: %i\n",
1623                                         ret);
1624                         return ret;
1625                 }
1626         }
1627
1628         ret = of_pci_get_max_link_speed(dev->of_node);
1629         if (ret <= 0 || ret > 3)
1630                 pcie->link_gen = 3;
1631         else
1632                 pcie->link_gen = ret;
1633
1634         ret = advk_pcie_setup_phy(pcie);
1635         if (ret)
1636                 return ret;
1637
1638         advk_pcie_setup_hw(pcie);
1639
1640         ret = advk_sw_pci_bridge_init(pcie);
1641         if (ret) {
1642                 dev_err(dev, "Failed to register emulated root PCI bridge\n");
1643                 return ret;
1644         }
1645
1646         ret = advk_pcie_init_irq_domain(pcie);
1647         if (ret) {
1648                 dev_err(dev, "Failed to initialize irq\n");
1649                 return ret;
1650         }
1651
1652         ret = advk_pcie_init_msi_irq_domain(pcie);
1653         if (ret) {
1654                 dev_err(dev, "Failed to initialize irq\n");
1655                 advk_pcie_remove_irq_domain(pcie);
1656                 return ret;
1657         }
1658
1659         irq_set_chained_handler_and_data(pcie->irq, advk_pcie_irq_handler, pcie);
1660
1661         bridge->sysdata = pcie;
1662         bridge->ops = &advk_pcie_ops;
1663
1664         ret = pci_host_probe(bridge);
1665         if (ret < 0) {
1666                 irq_set_chained_handler_and_data(pcie->irq, NULL, NULL);
1667                 advk_pcie_remove_msi_irq_domain(pcie);
1668                 advk_pcie_remove_irq_domain(pcie);
1669                 return ret;
1670         }
1671
1672         return 0;
1673 }
1674
1675 static int advk_pcie_remove(struct platform_device *pdev)
1676 {
1677         struct advk_pcie *pcie = platform_get_drvdata(pdev);
1678         struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
1679         u32 val;
1680         int i;
1681
1682         /* Remove PCI bus with all devices */
1683         pci_lock_rescan_remove();
1684         pci_stop_root_bus(bridge->bus);
1685         pci_remove_root_bus(bridge->bus);
1686         pci_unlock_rescan_remove();
1687
1688         /* Disable Root Bridge I/O space, memory space and bus mastering */
1689         val = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
1690         val &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1691         advk_writel(pcie, val, PCIE_CORE_CMD_STATUS_REG);
1692
1693         /* Disable MSI */
1694         val = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
1695         val &= ~PCIE_CORE_CTRL2_MSI_ENABLE;
1696         advk_writel(pcie, val, PCIE_CORE_CTRL2_REG);
1697
1698         /* Clear MSI address */
1699         advk_writel(pcie, 0, PCIE_MSI_ADDR_LOW_REG);
1700         advk_writel(pcie, 0, PCIE_MSI_ADDR_HIGH_REG);
1701
1702         /* Mask all interrupts */
1703         advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG);
1704         advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_MASK_REG);
1705         advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
1706         advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_MASK_REG);
1707
1708         /* Clear all interrupts */
1709         advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG);
1710         advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
1711         advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
1712         advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
1713
1714         /* Remove IRQ handler */
1715         irq_set_chained_handler_and_data(pcie->irq, NULL, NULL);
1716
1717         /* Remove IRQ domains */
1718         advk_pcie_remove_msi_irq_domain(pcie);
1719         advk_pcie_remove_irq_domain(pcie);
1720
1721         /* Free config space for emulated root bridge */
1722         pci_bridge_emul_cleanup(&pcie->bridge);
1723
1724         /* Assert PERST# signal which prepares PCIe card for power down */
1725         if (pcie->reset_gpio)
1726                 gpiod_set_value_cansleep(pcie->reset_gpio, 1);
1727
1728         /* Disable link training */
1729         val = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
1730         val &= ~LINK_TRAINING_EN;
1731         advk_writel(pcie, val, PCIE_CORE_CTRL0_REG);
1732
1733         /* Disable outbound address windows mapping */
1734         for (i = 0; i < OB_WIN_COUNT; i++)
1735                 advk_pcie_disable_ob_win(pcie, i);
1736
1737         /* Disable phy */
1738         advk_pcie_disable_phy(pcie);
1739
1740         return 0;
1741 }
1742
1743 static const struct of_device_id advk_pcie_of_match_table[] = {
1744         { .compatible = "marvell,armada-3700-pcie", },
1745         {},
1746 };
1747 MODULE_DEVICE_TABLE(of, advk_pcie_of_match_table);
1748
1749 static struct platform_driver advk_pcie_driver = {
1750         .driver = {
1751                 .name = "advk-pcie",
1752                 .of_match_table = advk_pcie_of_match_table,
1753         },
1754         .probe = advk_pcie_probe,
1755         .remove = advk_pcie_remove,
1756 };
1757 module_platform_driver(advk_pcie_driver);
1758
1759 MODULE_DESCRIPTION("Aardvark PCIe controller");
1760 MODULE_LICENSE("GPL v2");