blob: d5d6f92e5143a0cb8db92b6f9d9fa7a925cc5b9f [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02002/*
3 * Driver for the Aardvark PCIe controller, used on Marvell Armada
4 * 3700.
5 *
6 * Copyright (C) 2016 Marvell
7 *
Bjorn Helgaasa04bee82016-08-01 12:32:13 -05008 * Author: Hezi Shahmoon <hezi.shahmoon@marvell.com>
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02009 */
10
11#include <linux/delay.h>
Pali Rohárb32c0122020-09-07 13:10:34 +020012#include <linux/gpio/consumer.h>
Thomas Petazzoni8c39d712016-06-30 11:32:31 +020013#include <linux/interrupt.h>
14#include <linux/irq.h>
15#include <linux/irqdomain.h>
16#include <linux/kernel.h>
Pali Rohár526a7692020-09-07 13:10:37 +020017#include <linux/module.h>
Thomas Petazzoni8c39d712016-06-30 11:32:31 +020018#include <linux/pci.h>
Krzysztof Wilczyńskie7708f52020-11-29 23:07:39 +000019#include <linux/pci-ecam.h>
Bjorn Helgaasa04bee82016-08-01 12:32:13 -050020#include <linux/init.h>
Marek Behún36669702020-04-30 10:06:20 +020021#include <linux/phy/phy.h>
Thomas Petazzoni8c39d712016-06-30 11:32:31 +020022#include <linux/platform_device.h>
Krzysztof Wilczynskibbd8810d2019-09-03 13:30:59 +020023#include <linux/msi.h>
Thomas Petazzoni8c39d712016-06-30 11:32:31 +020024#include <linux/of_address.h>
Pali Rohár5169a982020-04-30 10:06:18 +020025#include <linux/of_gpio.h>
Thomas Petazzoni8c39d712016-06-30 11:32:31 +020026#include <linux/of_pci.h>
27
Rob Herring9e2aee82018-05-11 12:15:30 -050028#include "../pci.h"
Zachary Zhang8a3ebd82018-10-18 17:37:19 +020029#include "../pci-bridge-emul.h"
Rob Herring9e2aee82018-05-11 12:15:30 -050030
Thomas Petazzoni8c39d712016-06-30 11:32:31 +020031/* PCIe core registers */
Zachary Zhang8a3ebd82018-10-18 17:37:19 +020032#define PCIE_CORE_DEV_ID_REG 0x0
Thomas Petazzoni8c39d712016-06-30 11:32:31 +020033#define PCIE_CORE_CMD_STATUS_REG 0x4
34#define PCIE_CORE_CMD_IO_ACCESS_EN BIT(0)
35#define PCIE_CORE_CMD_MEM_ACCESS_EN BIT(1)
36#define PCIE_CORE_CMD_MEM_IO_REQ_EN BIT(2)
Zachary Zhang8a3ebd82018-10-18 17:37:19 +020037#define PCIE_CORE_DEV_REV_REG 0x8
38#define PCIE_CORE_PCIEXP_CAP 0xc0
Thomas Petazzoni8c39d712016-06-30 11:32:31 +020039#define PCIE_CORE_ERR_CAPCTL_REG 0x118
40#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
41#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
42#define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7)
43#define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8)
Zachary Zhang8a3ebd82018-10-18 17:37:19 +020044#define PCIE_CORE_INT_A_ASSERT_ENABLE 1
45#define PCIE_CORE_INT_B_ASSERT_ENABLE 2
46#define PCIE_CORE_INT_C_ASSERT_ENABLE 3
47#define PCIE_CORE_INT_D_ASSERT_ENABLE 4
Thomas Petazzoni8c39d712016-06-30 11:32:31 +020048/* PIO registers base address and register offsets */
49#define PIO_BASE_ADDR 0x4000
50#define PIO_CTRL (PIO_BASE_ADDR + 0x0)
51#define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
52#define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
53#define PIO_STAT (PIO_BASE_ADDR + 0x4)
54#define PIO_COMPLETION_STATUS_SHIFT 7
55#define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
56#define PIO_COMPLETION_STATUS_OK 0
57#define PIO_COMPLETION_STATUS_UR 1
58#define PIO_COMPLETION_STATUS_CRS 2
59#define PIO_COMPLETION_STATUS_CA 4
Pali Rohár8ceeac32021-06-24 23:33:43 +020060#define PIO_NON_POSTED_REQ BIT(10)
Evan Wangfcb461e2021-07-22 16:40:38 +020061#define PIO_ERR_STATUS BIT(11)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +020062#define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
63#define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
64#define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
65#define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
66#define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
67#define PIO_START (PIO_BASE_ADDR + 0x1c)
68#define PIO_ISR (PIO_BASE_ADDR + 0x20)
69#define PIO_ISRM (PIO_BASE_ADDR + 0x24)
70
71/* Aardvark Control registers */
72#define CONTROL_BASE_ADDR 0x4800
73#define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
74#define PCIE_GEN_SEL_MSK 0x3
75#define PCIE_GEN_SEL_SHIFT 0x0
76#define SPEED_GEN_1 0
77#define SPEED_GEN_2 1
78#define SPEED_GEN_3 2
79#define IS_RC_MSK 1
80#define IS_RC_SHIFT 2
81#define LANE_CNT_MSK 0x18
82#define LANE_CNT_SHIFT 0x3
83#define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
84#define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
85#define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
86#define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
87#define LINK_TRAINING_EN BIT(6)
88#define LEGACY_INTA BIT(28)
89#define LEGACY_INTB BIT(29)
90#define LEGACY_INTC BIT(30)
91#define LEGACY_INTD BIT(31)
92#define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4)
93#define HOT_RESET_GEN BIT(0)
94#define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
95#define PCIE_CORE_CTRL2_RESERVED 0x7
96#define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
97#define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
98#define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6)
99#define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10)
Marek Behún36669702020-04-30 10:06:20 +0200100#define PCIE_CORE_REF_CLK_REG (CONTROL_BASE_ADDR + 0x14)
101#define PCIE_CORE_REF_CLK_TX_ENABLE BIT(1)
Pali Rohár46ef6092021-10-05 20:09:44 +0200102#define PCIE_CORE_REF_CLK_RX_ENABLE BIT(2)
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200103#define PCIE_MSG_LOG_REG (CONTROL_BASE_ADDR + 0x30)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200104#define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40)
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200105#define PCIE_MSG_PM_PME_MASK BIT(7)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200106#define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44)
107#define PCIE_ISR0_MSI_INT_PENDING BIT(24)
108#define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val))
109#define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val))
110#define PCIE_ISR0_ALL_MASK GENMASK(26, 0)
111#define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48)
112#define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C)
113#define PCIE_ISR1_POWER_STATE_CHANGE BIT(4)
114#define PCIE_ISR1_FLUSH BIT(5)
Victor Gu3430f922018-04-06 16:55:33 +0200115#define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val))
116#define PCIE_ISR1_ALL_MASK GENMASK(11, 4)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200117#define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50)
118#define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54)
119#define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58)
120#define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C)
121#define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C)
122
Pali Rohár64f160e2021-06-24 23:55:45 +0200123/* PCIe window configuration */
124#define OB_WIN_BASE_ADDR 0x4c00
125#define OB_WIN_BLOCK_SIZE 0x20
126#define OB_WIN_COUNT 8
127#define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \
128 OB_WIN_BLOCK_SIZE * (win) + \
129 (offset))
130#define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00)
131#define OB_WIN_ENABLE BIT(0)
132#define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04)
133#define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08)
134#define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c)
135#define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10)
136#define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14)
137#define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18)
138#define OB_WIN_DEFAULT_ACTIONS (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4)
139#define OB_WIN_FUNC_NUM_MASK GENMASK(31, 24)
140#define OB_WIN_FUNC_NUM_SHIFT 24
141#define OB_WIN_FUNC_NUM_ENABLE BIT(23)
142#define OB_WIN_BUS_NUM_BITS_MASK GENMASK(22, 20)
143#define OB_WIN_BUS_NUM_BITS_SHIFT 20
144#define OB_WIN_MSG_CODE_ENABLE BIT(22)
145#define OB_WIN_MSG_CODE_MASK GENMASK(21, 14)
146#define OB_WIN_MSG_CODE_SHIFT 14
147#define OB_WIN_MSG_PAYLOAD_LEN BIT(12)
148#define OB_WIN_ATTR_ENABLE BIT(11)
149#define OB_WIN_ATTR_TC_MASK GENMASK(10, 8)
150#define OB_WIN_ATTR_TC_SHIFT 8
151#define OB_WIN_ATTR_RELAXED BIT(7)
152#define OB_WIN_ATTR_NOSNOOP BIT(6)
153#define OB_WIN_ATTR_POISON BIT(5)
154#define OB_WIN_ATTR_IDO BIT(4)
155#define OB_WIN_TYPE_MASK GENMASK(3, 0)
156#define OB_WIN_TYPE_SHIFT 0
157#define OB_WIN_TYPE_MEM 0x0
158#define OB_WIN_TYPE_IO 0x4
159#define OB_WIN_TYPE_CONFIG_TYPE0 0x8
160#define OB_WIN_TYPE_CONFIG_TYPE1 0x9
161#define OB_WIN_TYPE_MSG 0xc
162
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200163/* LMI registers base address and register offsets */
164#define LMI_BASE_ADDR 0x6000
165#define CFG_REG (LMI_BASE_ADDR + 0x0)
166#define LTSSM_SHIFT 24
167#define LTSSM_MASK 0x3f
168#define LTSSM_L0 0x10
169#define RC_BAR_CONFIG 0x300
Pali Rohár7f71a402021-06-25 00:26:20 +0200170#define VENDOR_ID_REG (LMI_BASE_ADDR + 0x44)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200171
172/* PCIe core controller registers */
173#define CTRL_CORE_BASE_ADDR 0x18000
174#define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
175#define CTRL_MODE_SHIFT 0x0
176#define CTRL_MODE_MASK 0x1
177#define PCIE_CORE_MODE_DIRECT 0x0
178#define PCIE_CORE_MODE_COMMAND 0x1
179
180/* PCIe Central Interrupts Registers */
181#define CENTRAL_INT_BASE_ADDR 0x1b000
182#define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
183#define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
184#define PCIE_IRQ_CMDQ_INT BIT(0)
185#define PCIE_IRQ_MSI_STATUS_INT BIT(1)
186#define PCIE_IRQ_CMD_SENT_DONE BIT(3)
187#define PCIE_IRQ_DMA_INT BIT(4)
188#define PCIE_IRQ_IB_DXFERDONE BIT(5)
189#define PCIE_IRQ_OB_DXFERDONE BIT(6)
190#define PCIE_IRQ_OB_RXFERDONE BIT(7)
191#define PCIE_IRQ_COMPQ_INT BIT(12)
192#define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
193#define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
194#define PCIE_IRQ_CORE_INT BIT(16)
195#define PCIE_IRQ_CORE_INT_PIO BIT(17)
196#define PCIE_IRQ_DPMU_INT BIT(18)
197#define PCIE_IRQ_PCIE_MIS_INT BIT(19)
198#define PCIE_IRQ_MSI_INT1_DET BIT(20)
199#define PCIE_IRQ_MSI_INT2_DET BIT(21)
200#define PCIE_IRQ_RC_DBELL_DET BIT(22)
201#define PCIE_IRQ_EP_STATUS BIT(23)
202#define PCIE_IRQ_ALL_MASK 0xfff0fb
203#define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
204
205/* Transaction types */
206#define PCIE_CONFIG_RD_TYPE0 0x8
207#define PCIE_CONFIG_RD_TYPE1 0x9
208#define PCIE_CONFIG_WR_TYPE0 0xa
209#define PCIE_CONFIG_WR_TYPE1 0xb
210
Pali Rohár02bcec32021-07-22 16:40:39 +0200211#define PIO_RETRY_CNT 750000 /* 1.5 s */
Remi Pommarel7fbcb5d2019-09-27 10:55:02 +0200212#define PIO_RETRY_DELAY 2 /* 2 us*/
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200213
214#define LINK_WAIT_MAX_RETRIES 10
215#define LINK_WAIT_USLEEP_MIN 90000
216#define LINK_WAIT_USLEEP_MAX 100000
Remi Pommarel364b3f12019-05-22 23:33:51 +0200217#define RETRAIN_WAIT_MAX_RETRIES 10
218#define RETRAIN_WAIT_USLEEP_US 2000
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200219
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200220#define MSI_IRQ_NUM 32
221
Pali Rohár43f5c772021-07-22 16:40:41 +0200222#define CFG_RD_CRS_VAL 0xffff0001
223
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200224struct advk_pcie {
225 struct platform_device *pdev;
226 void __iomem *base;
Pali Rohár64f160e2021-06-24 23:55:45 +0200227 struct {
228 phys_addr_t match;
229 phys_addr_t remap;
230 phys_addr_t mask;
231 u32 actions;
232 } wins[OB_WIN_COUNT];
233 u8 wins_count;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200234 struct irq_domain *irq_domain;
235 struct irq_chip irq_chip;
Pali Rohárd212dce2021-08-20 17:50:20 +0200236 raw_spinlock_t irq_lock;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200237 struct irq_domain *msi_domain;
Thomas Petazzonif21a8b12017-02-28 15:31:14 +0100238 struct irq_domain *msi_inner_domain;
239 struct irq_chip msi_bottom_irq_chip;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200240 struct irq_chip msi_irq_chip;
Thomas Petazzonif21a8b12017-02-28 15:31:14 +0100241 struct msi_domain_info msi_domain_info;
242 DECLARE_BITMAP(msi_used, MSI_IRQ_NUM);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200243 struct mutex msi_used_lock;
244 u16 msi_msg;
Marek Behún43fc6792020-04-30 10:06:17 +0200245 int link_gen;
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200246 struct pci_bridge_emul bridge;
Pali Rohár5169a982020-04-30 10:06:18 +0200247 struct gpio_desc *reset_gpio;
Marek Behún36669702020-04-30 10:06:20 +0200248 struct phy *phy;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200249};
250
251static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
252{
253 writel(val, pcie->base + reg);
254}
255
256static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
257{
258 return readl(pcie->base + reg);
259}
260
Pali Rohár96be36d2020-04-30 10:06:21 +0200261static inline u16 advk_read16(struct advk_pcie *pcie, u64 reg)
262{
263 return advk_readl(pcie, (reg & ~0x3)) >> ((reg & 0x3) * 8);
264}
265
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200266static int advk_pcie_link_up(struct advk_pcie *pcie)
267{
268 u32 val, ltssm_state;
269
270 val = advk_readl(pcie, CFG_REG);
271 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
272 return ltssm_state >= LTSSM_L0;
273}
274
275static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
276{
277 int retries;
278
279 /* check if the link is up or not */
280 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
Marek Behún43fc6792020-04-30 10:06:17 +0200281 if (advk_pcie_link_up(pcie))
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200282 return 0;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200283
284 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
285 }
286
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200287 return -ETIMEDOUT;
288}
289
Remi Pommarel364b3f12019-05-22 23:33:51 +0200290static void advk_pcie_wait_for_retrain(struct advk_pcie *pcie)
291{
292 size_t retries;
293
294 for (retries = 0; retries < RETRAIN_WAIT_MAX_RETRIES; ++retries) {
295 if (!advk_pcie_link_up(pcie))
296 break;
297 udelay(RETRAIN_WAIT_USLEEP_US);
298 }
299}
300
Pali Rohárd0c6a342020-09-07 13:10:38 +0200301static void advk_pcie_issue_perst(struct advk_pcie *pcie)
302{
303 u32 reg;
304
305 if (!pcie->reset_gpio)
306 return;
307
Pali Rohár1d1cd162020-12-02 19:46:59 +0100308 /*
309 * As required by PCI Express spec (PCI Express Base Specification, REV.
310 * 4.0 PCI Express, February 19 2014, 6.6.1 Conventional Reset) a delay
311 * for at least 100ms after de-asserting PERST# signal is needed before
312 * link training is enabled. So ensure that link training is disabled
313 * prior de-asserting PERST# signal to fulfill that PCI Express spec
314 * requirement.
315 */
Pali Rohárd0c6a342020-09-07 13:10:38 +0200316 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
317 reg &= ~LINK_TRAINING_EN;
318 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
319
320 /* 10ms delay is needed for some cards */
321 dev_info(&pcie->pdev->dev, "issuing PERST via reset GPIO for 10ms\n");
322 gpiod_set_value_cansleep(pcie->reset_gpio, 1);
323 usleep_range(10000, 11000);
324 gpiod_set_value_cansleep(pcie->reset_gpio, 0);
325}
326
Marek Behún43fc6792020-04-30 10:06:17 +0200327static int advk_pcie_train_at_gen(struct advk_pcie *pcie, int gen)
328{
329 int ret, neg_gen;
330 u32 reg;
331
332 /* Setup link speed */
333 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
334 reg &= ~PCIE_GEN_SEL_MSK;
335 if (gen == 3)
336 reg |= SPEED_GEN_3;
337 else if (gen == 2)
338 reg |= SPEED_GEN_2;
339 else
340 reg |= SPEED_GEN_1;
341 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
342
343 /*
344 * Enable link training. This is not needed in every call to this
345 * function, just once suffices, but it does not break anything either.
346 */
347 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
348 reg |= LINK_TRAINING_EN;
349 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
350
351 /*
352 * Start link training immediately after enabling it.
353 * This solves problems for some buggy cards.
354 */
Pali Rohár96be36d2020-04-30 10:06:21 +0200355 reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL);
356 reg |= PCI_EXP_LNKCTL_RL;
357 advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL);
Marek Behún43fc6792020-04-30 10:06:17 +0200358
359 ret = advk_pcie_wait_for_link(pcie);
360 if (ret)
361 return ret;
362
Pali Rohár96be36d2020-04-30 10:06:21 +0200363 reg = advk_read16(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKSTA);
364 neg_gen = reg & PCI_EXP_LNKSTA_CLS;
Marek Behún43fc6792020-04-30 10:06:17 +0200365
366 return neg_gen;
367}
368
369static void advk_pcie_train_link(struct advk_pcie *pcie)
370{
371 struct device *dev = &pcie->pdev->dev;
372 int neg_gen = -1, gen;
373
374 /*
Pali Rohárd0c6a342020-09-07 13:10:38 +0200375 * Reset PCIe card via PERST# signal. Some cards are not detected
376 * during link training when they are in some non-initial state.
377 */
378 advk_pcie_issue_perst(pcie);
379
380 /*
381 * PERST# signal could have been asserted by pinctrl subsystem before
382 * probe() callback has been called or issued explicitly by reset gpio
383 * function advk_pcie_issue_perst(), making the endpoint going into
384 * fundamental reset. As required by PCI Express spec a delay for at
385 * least 100ms after such a reset before link training is needed.
386 */
387 msleep(PCI_PM_D3COLD_WAIT);
388
389 /*
Marek Behún43fc6792020-04-30 10:06:17 +0200390 * Try link training at link gen specified by device tree property
391 * 'max-link-speed'. If this fails, iteratively train at lower gen.
392 */
393 for (gen = pcie->link_gen; gen > 0; --gen) {
394 neg_gen = advk_pcie_train_at_gen(pcie, gen);
395 if (neg_gen > 0)
396 break;
397 }
398
399 if (neg_gen < 0)
400 goto err;
401
402 /*
403 * After successful training if negotiated gen is lower than requested,
404 * train again on negotiated gen. This solves some stability issues for
405 * some buggy gen1 cards.
406 */
407 if (neg_gen < gen) {
408 gen = neg_gen;
409 neg_gen = advk_pcie_train_at_gen(pcie, gen);
410 }
411
412 if (neg_gen == gen) {
413 dev_info(dev, "link up at gen %i\n", gen);
414 return;
415 }
416
417err:
418 dev_err(dev, "link never came up\n");
419}
420
Pali Rohár64f160e2021-06-24 23:55:45 +0200421/*
422 * Set PCIe address window register which could be used for memory
423 * mapping.
424 */
425static void advk_pcie_set_ob_win(struct advk_pcie *pcie, u8 win_num,
426 phys_addr_t match, phys_addr_t remap,
427 phys_addr_t mask, u32 actions)
428{
429 advk_writel(pcie, OB_WIN_ENABLE |
430 lower_32_bits(match), OB_WIN_MATCH_LS(win_num));
431 advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num));
432 advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num));
433 advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num));
434 advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num));
435 advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num));
436 advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num));
437}
438
439static void advk_pcie_disable_ob_win(struct advk_pcie *pcie, u8 win_num)
440{
441 advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num));
442 advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num));
443 advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num));
444 advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num));
445 advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num));
446 advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num));
447 advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num));
448}
449
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200450static void advk_pcie_setup_hw(struct advk_pcie *pcie)
451{
452 u32 reg;
Pali Rohár64f160e2021-06-24 23:55:45 +0200453 int i;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200454
Pali Rohár46ef6092021-10-05 20:09:44 +0200455 /*
456 * Configure PCIe Reference clock. Direction is from the PCIe
457 * controller to the endpoint card, so enable transmitting of
458 * Reference clock differential signal off-chip and disable
459 * receiving off-chip differential signal.
460 */
Marek Behún36669702020-04-30 10:06:20 +0200461 reg = advk_readl(pcie, PCIE_CORE_REF_CLK_REG);
462 reg |= PCIE_CORE_REF_CLK_TX_ENABLE;
Pali Rohár46ef6092021-10-05 20:09:44 +0200463 reg &= ~PCIE_CORE_REF_CLK_RX_ENABLE;
Marek Behún36669702020-04-30 10:06:20 +0200464 advk_writel(pcie, reg, PCIE_CORE_REF_CLK_REG);
465
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200466 /* Set to Direct mode */
467 reg = advk_readl(pcie, CTRL_CONFIG_REG);
468 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
469 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
470 advk_writel(pcie, reg, CTRL_CONFIG_REG);
471
472 /* Set PCI global control register to RC mode */
473 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
474 reg |= (IS_RC_MSK << IS_RC_SHIFT);
475 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
476
Pali Rohár7f71a402021-06-25 00:26:20 +0200477 /*
478 * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab.
479 * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor
480 * id in high 16 bits. Updating this register changes readback value of
481 * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround
482 * for erratum 4.1: "The value of device and vendor ID is incorrect".
483 */
484 reg = (PCI_VENDOR_ID_MARVELL << 16) | PCI_VENDOR_ID_MARVELL;
485 advk_writel(pcie, reg, VENDOR_ID_REG);
486
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200487 /* Set Advanced Error Capabilities and Control PF0 register */
488 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
489 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
490 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
491 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
492 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
493
Pali Rohár96be36d2020-04-30 10:06:21 +0200494 /* Set PCIe Device Control register */
495 reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL);
496 reg &= ~PCI_EXP_DEVCTL_RELAX_EN;
497 reg &= ~PCI_EXP_DEVCTL_NOSNOOP_EN;
Pali Rohára4e17d62021-10-05 20:09:41 +0200498 reg &= ~PCI_EXP_DEVCTL_PAYLOAD;
Pali Rohár96be36d2020-04-30 10:06:21 +0200499 reg &= ~PCI_EXP_DEVCTL_READRQ;
Pali Rohára4e17d62021-10-05 20:09:41 +0200500 reg |= PCI_EXP_DEVCTL_PAYLOAD_512B;
Pali Rohár96be36d2020-04-30 10:06:21 +0200501 reg |= PCI_EXP_DEVCTL_READRQ_512B;
502 advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200503
504 /* Program PCIe Control 2 to disable strict ordering */
505 reg = PCIE_CORE_CTRL2_RESERVED |
506 PCIE_CORE_CTRL2_TD_ENABLE;
507 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
508
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200509 /* Set lane X1 */
510 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
511 reg &= ~LANE_CNT_MSK;
512 reg |= LANE_COUNT_1;
513 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
514
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200515 /* Enable MSI */
516 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
517 reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
518 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
519
520 /* Clear all interrupts */
521 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
522 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
523 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
524
525 /* Disable All ISR0/1 Sources */
526 reg = PCIE_ISR0_ALL_MASK;
527 reg &= ~PCIE_ISR0_MSI_INT_PENDING;
528 advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
529
530 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
531
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500532 /* Unmask all MSIs */
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200533 advk_writel(pcie, 0, PCIE_MSI_MASK_REG);
534
535 /* Enable summary interrupt for GIC SPI source */
536 reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
537 advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
538
Pali Rohár64f160e2021-06-24 23:55:45 +0200539 /*
540 * Enable AXI address window location generation:
541 * When it is enabled, the default outbound window
542 * configurations (Default User Field: 0xD0074CFC)
543 * are used to transparent address translation for
544 * the outbound transactions. Thus, PCIe address
545 * windows are not required for transparent memory
546 * access when default outbound window configuration
547 * is set for memory access.
548 */
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200549 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
550 reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
551 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
552
Pali Rohár64f160e2021-06-24 23:55:45 +0200553 /*
554 * Set memory access in Default User Field so it
555 * is not required to configure PCIe address for
556 * transparent memory access.
557 */
558 advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS);
559
560 /*
561 * Bypass the address window mapping for PIO:
562 * Since PIO access already contains all required
563 * info over AXI interface by PIO registers, the
564 * address window is not required.
565 */
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200566 reg = advk_readl(pcie, PIO_CTRL);
567 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
568 advk_writel(pcie, reg, PIO_CTRL);
569
Pali Rohár64f160e2021-06-24 23:55:45 +0200570 /*
571 * Configure PCIe address windows for non-memory or
572 * non-transparent access as by default PCIe uses
573 * transparent memory access.
574 */
575 for (i = 0; i < pcie->wins_count; i++)
576 advk_pcie_set_ob_win(pcie, i,
577 pcie->wins[i].match, pcie->wins[i].remap,
578 pcie->wins[i].mask, pcie->wins[i].actions);
579
580 /* Disable remaining PCIe outbound windows */
581 for (i = pcie->wins_count; i < OB_WIN_COUNT; i++)
582 advk_pcie_disable_ob_win(pcie, i);
583
Marek Behún43fc6792020-04-30 10:06:17 +0200584 advk_pcie_train_link(pcie);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200585
Pali Rohárb2a56462020-04-30 10:06:19 +0200586 /*
587 * FIXME: The following register update is suspicious. This register is
588 * applicable only when the PCI controller is configured for Endpoint
589 * mode, not as a Root Complex. But apparently when this code is
590 * removed, some cards stop working. This should be investigated and
591 * a comment explaining this should be put here.
592 */
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200593 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
594 reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
595 PCIE_CORE_CMD_IO_ACCESS_EN |
596 PCIE_CORE_CMD_MEM_IO_REQ_EN;
597 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
598}
599
Pali Rohár43f5c772021-07-22 16:40:41 +0200600static int advk_pcie_check_pio_status(struct advk_pcie *pcie, bool allow_crs, u32 *val)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200601{
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -0500602 struct device *dev = &pcie->pdev->dev;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200603 u32 reg;
604 unsigned int status;
605 char *strcomp_status, *str_posted;
606
607 reg = advk_readl(pcie, PIO_STAT);
608 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
609 PIO_COMPLETION_STATUS_SHIFT;
610
Evan Wangfcb461e2021-07-22 16:40:38 +0200611 /*
612 * According to HW spec, the PIO status check sequence as below:
613 * 1) even if COMPLETION_STATUS(bit9:7) indicates successful,
614 * it still needs to check Error Status(bit11), only when this bit
615 * indicates no error happen, the operation is successful.
616 * 2) value Unsupported Request(1) of COMPLETION_STATUS(bit9:7) only
617 * means a PIO write error, and for PIO read it is successful with
618 * a read value of 0xFFFFFFFF.
619 * 3) value Completion Retry Status(CRS) of COMPLETION_STATUS(bit9:7)
620 * only means a PIO write error, and for PIO read it is successful
621 * with a read value of 0xFFFF0001.
622 * 4) value Completer Abort (CA) of COMPLETION_STATUS(bit9:7) means
623 * error for both PIO read and PIO write operation.
624 * 5) other errors are indicated as 'unknown'.
625 */
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200626 switch (status) {
Evan Wangfcb461e2021-07-22 16:40:38 +0200627 case PIO_COMPLETION_STATUS_OK:
628 if (reg & PIO_ERR_STATUS) {
629 strcomp_status = "COMP_ERR";
630 break;
631 }
632 /* Get the read result */
633 if (val)
634 *val = advk_readl(pcie, PIO_RD_DATA);
635 /* No error */
636 strcomp_status = NULL;
637 break;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200638 case PIO_COMPLETION_STATUS_UR:
639 strcomp_status = "UR";
640 break;
641 case PIO_COMPLETION_STATUS_CRS:
Pali Rohár43f5c772021-07-22 16:40:41 +0200642 if (allow_crs && val) {
643 /* PCIe r4.0, sec 2.3.2, says:
644 * If CRS Software Visibility is enabled:
645 * For a Configuration Read Request that includes both
646 * bytes of the Vendor ID field of a device Function's
647 * Configuration Space Header, the Root Complex must
648 * complete the Request to the host by returning a
649 * read-data value of 0001h for the Vendor ID field and
650 * all '1's for any additional bytes included in the
651 * request.
652 *
653 * So CRS in this case is not an error status.
654 */
655 *val = CFG_RD_CRS_VAL;
656 strcomp_status = NULL;
657 break;
658 }
Evan Wangfcb461e2021-07-22 16:40:38 +0200659 /* PCIe r4.0, sec 2.3.2, says:
660 * If CRS Software Visibility is not enabled, the Root Complex
661 * must re-issue the Configuration Request as a new Request.
Pali Rohár43f5c772021-07-22 16:40:41 +0200662 * If CRS Software Visibility is enabled: For a Configuration
663 * Write Request or for any other Configuration Read Request,
664 * the Root Complex must re-issue the Configuration Request as
665 * a new Request.
Evan Wangfcb461e2021-07-22 16:40:38 +0200666 * A Root Complex implementation may choose to limit the number
667 * of Configuration Request/CRS Completion Status loops before
668 * determining that something is wrong with the target of the
669 * Request and taking appropriate action, e.g., complete the
670 * Request to the host as a failed transaction.
671 *
672 * To simplify implementation do not re-issue the Configuration
673 * Request and complete the Request as a failed transaction.
674 */
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200675 strcomp_status = "CRS";
676 break;
677 case PIO_COMPLETION_STATUS_CA:
678 strcomp_status = "CA";
679 break;
680 default:
681 strcomp_status = "Unknown";
682 break;
683 }
684
Evan Wangfcb461e2021-07-22 16:40:38 +0200685 if (!strcomp_status)
686 return 0;
687
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200688 if (reg & PIO_NON_POSTED_REQ)
689 str_posted = "Non-posted";
690 else
691 str_posted = "Posted";
692
Marek Behún464de7e2021-10-05 20:09:42 +0200693 dev_dbg(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200694 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
Evan Wangfcb461e2021-07-22 16:40:38 +0200695
696 return -EFAULT;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200697}
698
699static int advk_pcie_wait_pio(struct advk_pcie *pcie)
700{
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -0500701 struct device *dev = &pcie->pdev->dev;
Remi Pommarel7fbcb5d2019-09-27 10:55:02 +0200702 int i;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200703
Remi Pommarel7fbcb5d2019-09-27 10:55:02 +0200704 for (i = 0; i < PIO_RETRY_CNT; i++) {
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200705 u32 start, isr;
706
707 start = advk_readl(pcie, PIO_START);
708 isr = advk_readl(pcie, PIO_ISR);
709 if (!start && isr)
710 return 0;
Remi Pommarel7fbcb5d2019-09-27 10:55:02 +0200711 udelay(PIO_RETRY_DELAY);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200712 }
713
Pali Rohárf1813992021-06-08 22:36:55 +0200714 dev_err(dev, "PIO read/write transfer time out\n");
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200715 return -ETIMEDOUT;
716}
717
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200718
719static pci_bridge_emul_read_status_t
720advk_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
721 int reg, u32 *value)
722{
723 struct advk_pcie *pcie = bridge->data;
724
725
726 switch (reg) {
727 case PCI_EXP_SLTCTL:
728 *value = PCI_EXP_SLTSTA_PDS << 16;
729 return PCI_BRIDGE_EMUL_HANDLED;
730
731 case PCI_EXP_RTCTL: {
732 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG);
Remi Pommarelc0f05a62019-06-14 12:10:59 +0200733 *value = (val & PCIE_MSG_PM_PME_MASK) ? 0 : PCI_EXP_RTCTL_PMEIE;
Pali Rohárd4190522021-10-05 20:09:43 +0200734 *value |= le16_to_cpu(bridge->pcie_conf.rootctl) & PCI_EXP_RTCTL_CRSSVE;
Pali Rohár43f5c772021-07-22 16:40:41 +0200735 *value |= PCI_EXP_RTCAP_CRSVIS << 16;
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200736 return PCI_BRIDGE_EMUL_HANDLED;
737 }
738
739 case PCI_EXP_RTSTA: {
740 u32 isr0 = advk_readl(pcie, PCIE_ISR0_REG);
741 u32 msglog = advk_readl(pcie, PCIE_MSG_LOG_REG);
742 *value = (isr0 & PCIE_MSG_PM_PME_MASK) << 16 | (msglog >> 16);
743 return PCI_BRIDGE_EMUL_HANDLED;
744 }
745
Remi Pommarel364b3f12019-05-22 23:33:51 +0200746 case PCI_EXP_LNKCTL: {
747 /* u32 contains both PCI_EXP_LNKCTL and PCI_EXP_LNKSTA */
748 u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg) &
749 ~(PCI_EXP_LNKSTA_LT << 16);
750 if (!advk_pcie_link_up(pcie))
751 val |= (PCI_EXP_LNKSTA_LT << 16);
752 *value = val;
753 return PCI_BRIDGE_EMUL_HANDLED;
754 }
755
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200756 case PCI_CAP_LIST_ID:
757 case PCI_EXP_DEVCAP:
758 case PCI_EXP_DEVCTL:
759 case PCI_EXP_LNKCAP:
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200760 *value = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg);
761 return PCI_BRIDGE_EMUL_HANDLED;
762 default:
763 return PCI_BRIDGE_EMUL_NOT_HANDLED;
764 }
765
766}
767
768static void
769advk_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge,
770 int reg, u32 old, u32 new, u32 mask)
771{
772 struct advk_pcie *pcie = bridge->data;
773
774 switch (reg) {
775 case PCI_EXP_DEVCTL:
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200776 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
777 break;
778
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200779 case PCI_EXP_LNKCTL:
780 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
Remi Pommarel364b3f12019-05-22 23:33:51 +0200781 if (new & PCI_EXP_LNKCTL_RL)
782 advk_pcie_wait_for_retrain(pcie);
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200783 break;
784
Remi Pommarelc0f05a62019-06-14 12:10:59 +0200785 case PCI_EXP_RTCTL: {
786 /* Only mask/unmask PME interrupt */
787 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG) &
788 ~PCIE_MSG_PM_PME_MASK;
789 if ((new & PCI_EXP_RTCTL_PMEIE) == 0)
790 val |= PCIE_MSG_PM_PME_MASK;
791 advk_writel(pcie, val, PCIE_ISR0_MASK_REG);
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200792 break;
Remi Pommarelc0f05a62019-06-14 12:10:59 +0200793 }
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200794
795 case PCI_EXP_RTSTA:
796 new = (new & PCI_EXP_RTSTA_PME) >> 9;
797 advk_writel(pcie, new, PCIE_ISR0_REG);
798 break;
799
800 default:
801 break;
802 }
803}
804
Wei Yongjund3b34d042019-03-01 06:58:09 +0000805static struct pci_bridge_emul_ops advk_pci_bridge_emul_ops = {
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200806 .read_pcie = advk_pci_bridge_emul_pcie_conf_read,
807 .write_pcie = advk_pci_bridge_emul_pcie_conf_write,
808};
809
810/*
811 * Initialize the configuration space of the PCI-to-PCI bridge
812 * associated with the given PCIe interface.
813 */
Pali Rohár7862a612020-09-07 13:10:35 +0200814static int advk_sw_pci_bridge_init(struct advk_pcie *pcie)
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200815{
816 struct pci_bridge_emul *bridge = &pcie->bridge;
Pali Rohár43f5c772021-07-22 16:40:41 +0200817 int ret;
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200818
Grzegorz Jaszczyke0787232019-07-16 14:12:07 +0200819 bridge->conf.vendor =
820 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) & 0xffff);
821 bridge->conf.device =
822 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) >> 16);
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200823 bridge->conf.class_revision =
Grzegorz Jaszczyke0787232019-07-16 14:12:07 +0200824 cpu_to_le32(advk_readl(pcie, PCIE_CORE_DEV_REV_REG) & 0xff);
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200825
826 /* Support 32 bits I/O addressing */
827 bridge->conf.iobase = PCI_IO_RANGE_TYPE_32;
828 bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32;
829
830 /* Support 64 bits memory pref */
Grzegorz Jaszczyke0787232019-07-16 14:12:07 +0200831 bridge->conf.pref_mem_base = cpu_to_le16(PCI_PREF_RANGE_TYPE_64);
832 bridge->conf.pref_mem_limit = cpu_to_le16(PCI_PREF_RANGE_TYPE_64);
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200833
834 /* Support interrupt A for MSI feature */
835 bridge->conf.intpin = PCIE_CORE_INT_A_ASSERT_ENABLE;
836
837 bridge->has_pcie = true;
838 bridge->data = pcie;
839 bridge->ops = &advk_pci_bridge_emul_ops;
840
Pali Rohár43f5c772021-07-22 16:40:41 +0200841 /* PCIe config space can be initialized after pci_bridge_emul_init() */
842 ret = pci_bridge_emul_init(bridge, 0);
843 if (ret < 0)
844 return ret;
845
846 /* Indicates supports for Completion Retry Status */
847 bridge->pcie_conf.rootcap = cpu_to_le16(PCI_EXP_RTCAP_CRSVIS);
848
849 return 0;
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200850}
851
Thomas Petazzoni248d4e52018-04-06 16:55:35 +0200852static bool advk_pcie_valid_device(struct advk_pcie *pcie, struct pci_bus *bus,
853 int devfn)
854{
Rob Herring11e97972020-07-21 20:24:59 -0600855 if (pci_is_root_bus(bus) && PCI_SLOT(devfn) != 0)
Thomas Petazzoni248d4e52018-04-06 16:55:35 +0200856 return false;
857
Pali Rohár70e38022020-07-02 10:30:36 +0200858 /*
859 * If the link goes down after we check for link-up, nothing bad
860 * happens but the config access times out.
861 */
Bjorn Helgaas49e427e2020-08-05 18:24:21 -0500862 if (!pci_is_root_bus(bus) && !advk_pcie_link_up(pcie))
Pali Rohár70e38022020-07-02 10:30:36 +0200863 return false;
864
Thomas Petazzoni248d4e52018-04-06 16:55:35 +0200865 return true;
866}
867
Pali Rohárf1813992021-06-08 22:36:55 +0200868static bool advk_pcie_pio_is_running(struct advk_pcie *pcie)
869{
870 struct device *dev = &pcie->pdev->dev;
871
872 /*
873 * Trying to start a new PIO transfer when previous has not completed
874 * cause External Abort on CPU which results in kernel panic:
875 *
876 * SError Interrupt on CPU0, code 0xbf000002 -- SError
877 * Kernel panic - not syncing: Asynchronous SError Interrupt
878 *
879 * Functions advk_pcie_rd_conf() and advk_pcie_wr_conf() are protected
880 * by raw_spin_lock_irqsave() at pci_lock_config() level to prevent
881 * concurrent calls at the same time. But because PIO transfer may take
882 * about 1.5s when link is down or card is disconnected, it means that
883 * advk_pcie_wait_pio() does not always have to wait for completion.
884 *
885 * Some versions of ARM Trusted Firmware handles this External Abort at
886 * EL3 level and mask it to prevent kernel panic. Relevant TF-A commit:
887 * https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=3c7dcdac5c50
888 */
889 if (advk_readl(pcie, PIO_START)) {
890 dev_err(dev, "Previous PIO read/write transfer is still running\n");
891 return true;
892 }
893
894 return false;
895}
896
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200897static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
898 int where, int size, u32 *val)
899{
900 struct advk_pcie *pcie = bus->sysdata;
Pali Rohár43f5c772021-07-22 16:40:41 +0200901 bool allow_crs;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200902 u32 reg;
903 int ret;
904
Thomas Petazzoni248d4e52018-04-06 16:55:35 +0200905 if (!advk_pcie_valid_device(pcie, bus, devfn)) {
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200906 *val = 0xffffffff;
907 return PCIBIOS_DEVICE_NOT_FOUND;
908 }
909
Rob Herring11e97972020-07-21 20:24:59 -0600910 if (pci_is_root_bus(bus))
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200911 return pci_bridge_emul_conf_read(&pcie->bridge, where,
912 size, val);
913
Pali Rohár43f5c772021-07-22 16:40:41 +0200914 /*
915 * Completion Retry Status is possible to return only when reading all
916 * 4 bytes from PCI_VENDOR_ID and PCI_DEVICE_ID registers at once and
917 * CRSSVE flag on Root Bridge is enabled.
918 */
919 allow_crs = (where == PCI_VENDOR_ID) && (size == 4) &&
920 (le16_to_cpu(pcie->bridge.pcie_conf.rootctl) &
921 PCI_EXP_RTCTL_CRSSVE);
922
Pali Rohárf1813992021-06-08 22:36:55 +0200923 if (advk_pcie_pio_is_running(pcie)) {
Pali Rohár43f5c772021-07-22 16:40:41 +0200924 /*
925 * If it is possible return Completion Retry Status so caller
926 * tries to issue the request again instead of failing.
927 */
928 if (allow_crs) {
929 *val = CFG_RD_CRS_VAL;
930 return PCIBIOS_SUCCESSFUL;
931 }
Pali Rohárf1813992021-06-08 22:36:55 +0200932 *val = 0xffffffff;
933 return PCIBIOS_SET_FAILED;
934 }
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200935
936 /* Program the control register */
937 reg = advk_readl(pcie, PIO_CTRL);
938 reg &= ~PIO_CTRL_TYPE_MASK;
Rob Herring11e97972020-07-21 20:24:59 -0600939 if (pci_is_root_bus(bus->parent))
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200940 reg |= PCIE_CONFIG_RD_TYPE0;
941 else
942 reg |= PCIE_CONFIG_RD_TYPE1;
943 advk_writel(pcie, reg, PIO_CTRL);
944
945 /* Program the address registers */
Krzysztof Wilczyńskie7708f52020-11-29 23:07:39 +0000946 reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200947 advk_writel(pcie, reg, PIO_ADDR_LS);
948 advk_writel(pcie, 0, PIO_ADDR_MS);
949
950 /* Program the data strobe */
951 advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
952
Pali Rohárf1813992021-06-08 22:36:55 +0200953 /* Clear PIO DONE ISR and start the transfer */
954 advk_writel(pcie, 1, PIO_ISR);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200955 advk_writel(pcie, 1, PIO_START);
956
957 ret = advk_pcie_wait_pio(pcie);
Pali Rohárb1bd5712020-06-01 15:03:15 +0200958 if (ret < 0) {
Pali Rohár43f5c772021-07-22 16:40:41 +0200959 /*
960 * If it is possible return Completion Retry Status so caller
961 * tries to issue the request again instead of failing.
962 */
963 if (allow_crs) {
964 *val = CFG_RD_CRS_VAL;
965 return PCIBIOS_SUCCESSFUL;
966 }
Pali Rohárb1bd5712020-06-01 15:03:15 +0200967 *val = 0xffffffff;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200968 return PCIBIOS_SET_FAILED;
Pali Rohárb1bd5712020-06-01 15:03:15 +0200969 }
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200970
Evan Wangfcb461e2021-07-22 16:40:38 +0200971 /* Check PIO status and get the read result */
Pali Rohár43f5c772021-07-22 16:40:41 +0200972 ret = advk_pcie_check_pio_status(pcie, allow_crs, val);
Evan Wangfcb461e2021-07-22 16:40:38 +0200973 if (ret < 0) {
974 *val = 0xffffffff;
975 return PCIBIOS_SET_FAILED;
976 }
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200977
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200978 if (size == 1)
979 *val = (*val >> (8 * (where & 3))) & 0xff;
980 else if (size == 2)
981 *val = (*val >> (8 * (where & 3))) & 0xffff;
982
983 return PCIBIOS_SUCCESSFUL;
984}
985
986static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
987 int where, int size, u32 val)
988{
989 struct advk_pcie *pcie = bus->sysdata;
990 u32 reg;
991 u32 data_strobe = 0x0;
992 int offset;
993 int ret;
994
Thomas Petazzoni248d4e52018-04-06 16:55:35 +0200995 if (!advk_pcie_valid_device(pcie, bus, devfn))
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200996 return PCIBIOS_DEVICE_NOT_FOUND;
997
Rob Herring11e97972020-07-21 20:24:59 -0600998 if (pci_is_root_bus(bus))
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200999 return pci_bridge_emul_conf_write(&pcie->bridge, where,
1000 size, val);
1001
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001002 if (where % size)
1003 return PCIBIOS_SET_FAILED;
1004
Pali Rohárf1813992021-06-08 22:36:55 +02001005 if (advk_pcie_pio_is_running(pcie))
1006 return PCIBIOS_SET_FAILED;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001007
1008 /* Program the control register */
1009 reg = advk_readl(pcie, PIO_CTRL);
1010 reg &= ~PIO_CTRL_TYPE_MASK;
Rob Herring11e97972020-07-21 20:24:59 -06001011 if (pci_is_root_bus(bus->parent))
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001012 reg |= PCIE_CONFIG_WR_TYPE0;
1013 else
1014 reg |= PCIE_CONFIG_WR_TYPE1;
1015 advk_writel(pcie, reg, PIO_CTRL);
1016
1017 /* Program the address registers */
Krzysztof Wilczyńskie7708f52020-11-29 23:07:39 +00001018 reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001019 advk_writel(pcie, reg, PIO_ADDR_LS);
1020 advk_writel(pcie, 0, PIO_ADDR_MS);
1021
1022 /* Calculate the write strobe */
1023 offset = where & 0x3;
1024 reg = val << (8 * offset);
1025 data_strobe = GENMASK(size - 1, 0) << offset;
1026
1027 /* Program the data register */
1028 advk_writel(pcie, reg, PIO_WR_DATA);
1029
1030 /* Program the data strobe */
1031 advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
1032
Pali Rohárf1813992021-06-08 22:36:55 +02001033 /* Clear PIO DONE ISR and start the transfer */
1034 advk_writel(pcie, 1, PIO_ISR);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001035 advk_writel(pcie, 1, PIO_START);
1036
1037 ret = advk_pcie_wait_pio(pcie);
1038 if (ret < 0)
1039 return PCIBIOS_SET_FAILED;
1040
Pali Rohár43f5c772021-07-22 16:40:41 +02001041 ret = advk_pcie_check_pio_status(pcie, false, NULL);
Evan Wangfcb461e2021-07-22 16:40:38 +02001042 if (ret < 0)
1043 return PCIBIOS_SET_FAILED;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001044
1045 return PCIBIOS_SUCCESSFUL;
1046}
1047
1048static struct pci_ops advk_pcie_ops = {
1049 .read = advk_pcie_rd_conf,
1050 .write = advk_pcie_wr_conf,
1051};
1052
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001053static void advk_msi_irq_compose_msi_msg(struct irq_data *data,
1054 struct msi_msg *msg)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001055{
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001056 struct advk_pcie *pcie = irq_data_get_irq_chip_data(data);
1057 phys_addr_t msi_msg = virt_to_phys(&pcie->msi_msg);
1058
1059 msg->address_lo = lower_32_bits(msi_msg);
1060 msg->address_hi = upper_32_bits(msi_msg);
1061 msg->data = data->irq;
1062}
1063
1064static int advk_msi_set_affinity(struct irq_data *irq_data,
1065 const struct cpumask *mask, bool force)
1066{
1067 return -EINVAL;
1068}
1069
1070static int advk_msi_irq_domain_alloc(struct irq_domain *domain,
1071 unsigned int virq,
1072 unsigned int nr_irqs, void *args)
1073{
1074 struct advk_pcie *pcie = domain->host_data;
1075 int hwirq, i;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001076
1077 mutex_lock(&pcie->msi_used_lock);
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001078 hwirq = bitmap_find_next_zero_area(pcie->msi_used, MSI_IRQ_NUM,
1079 0, nr_irqs, 0);
1080 if (hwirq >= MSI_IRQ_NUM) {
1081 mutex_unlock(&pcie->msi_used_lock);
1082 return -ENOSPC;
1083 }
1084
1085 bitmap_set(pcie->msi_used, hwirq, nr_irqs);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001086 mutex_unlock(&pcie->msi_used_lock);
1087
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001088 for (i = 0; i < nr_irqs; i++)
1089 irq_domain_set_info(domain, virq + i, hwirq + i,
1090 &pcie->msi_bottom_irq_chip,
1091 domain->host_data, handle_simple_irq,
1092 NULL, NULL);
1093
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001094 return hwirq;
1095}
1096
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001097static void advk_msi_irq_domain_free(struct irq_domain *domain,
1098 unsigned int virq, unsigned int nr_irqs)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001099{
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001100 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1101 struct advk_pcie *pcie = domain->host_data;
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -05001102
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001103 mutex_lock(&pcie->msi_used_lock);
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001104 bitmap_clear(pcie->msi_used, d->hwirq, nr_irqs);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001105 mutex_unlock(&pcie->msi_used_lock);
1106}
1107
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001108static const struct irq_domain_ops advk_msi_domain_ops = {
1109 .alloc = advk_msi_irq_domain_alloc,
1110 .free = advk_msi_irq_domain_free,
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001111};
1112
1113static void advk_pcie_irq_mask(struct irq_data *d)
1114{
1115 struct advk_pcie *pcie = d->domain->host_data;
1116 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Pali Rohárd212dce2021-08-20 17:50:20 +02001117 unsigned long flags;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001118 u32 mask;
1119
Pali Rohárd212dce2021-08-20 17:50:20 +02001120 raw_spin_lock_irqsave(&pcie->irq_lock, flags);
Victor Gu3430f922018-04-06 16:55:33 +02001121 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
1122 mask |= PCIE_ISR1_INTX_ASSERT(hwirq);
1123 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
Pali Rohárd212dce2021-08-20 17:50:20 +02001124 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001125}
1126
1127static void advk_pcie_irq_unmask(struct irq_data *d)
1128{
1129 struct advk_pcie *pcie = d->domain->host_data;
1130 irq_hw_number_t hwirq = irqd_to_hwirq(d);
Pali Rohárd212dce2021-08-20 17:50:20 +02001131 unsigned long flags;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001132 u32 mask;
1133
Pali Rohárd212dce2021-08-20 17:50:20 +02001134 raw_spin_lock_irqsave(&pcie->irq_lock, flags);
Victor Gu3430f922018-04-06 16:55:33 +02001135 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
1136 mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq);
1137 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
Pali Rohárd212dce2021-08-20 17:50:20 +02001138 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001139}
1140
1141static int advk_pcie_irq_map(struct irq_domain *h,
1142 unsigned int virq, irq_hw_number_t hwirq)
1143{
1144 struct advk_pcie *pcie = h->host_data;
1145
1146 advk_pcie_irq_mask(irq_get_irq_data(virq));
1147 irq_set_status_flags(virq, IRQ_LEVEL);
1148 irq_set_chip_and_handler(virq, &pcie->irq_chip,
1149 handle_level_irq);
1150 irq_set_chip_data(virq, pcie);
1151
1152 return 0;
1153}
1154
1155static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
1156 .map = advk_pcie_irq_map,
1157 .xlate = irq_domain_xlate_onecell,
1158};
1159
1160static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
1161{
1162 struct device *dev = &pcie->pdev->dev;
1163 struct device_node *node = dev->of_node;
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001164 struct irq_chip *bottom_ic, *msi_ic;
1165 struct msi_domain_info *msi_di;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001166 phys_addr_t msi_msg_phys;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001167
1168 mutex_init(&pcie->msi_used_lock);
1169
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001170 bottom_ic = &pcie->msi_bottom_irq_chip;
1171
1172 bottom_ic->name = "MSI";
1173 bottom_ic->irq_compose_msi_msg = advk_msi_irq_compose_msi_msg;
1174 bottom_ic->irq_set_affinity = advk_msi_set_affinity;
1175
1176 msi_ic = &pcie->msi_irq_chip;
1177 msi_ic->name = "advk-MSI";
1178
1179 msi_di = &pcie->msi_domain_info;
1180 msi_di->flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
1181 MSI_FLAG_MULTI_PCI_MSI;
1182 msi_di->chip = msi_ic;
1183
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001184 msi_msg_phys = virt_to_phys(&pcie->msi_msg);
1185
1186 advk_writel(pcie, lower_32_bits(msi_msg_phys),
1187 PCIE_MSI_ADDR_LOW_REG);
1188 advk_writel(pcie, upper_32_bits(msi_msg_phys),
1189 PCIE_MSI_ADDR_HIGH_REG);
1190
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001191 pcie->msi_inner_domain =
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001192 irq_domain_add_linear(NULL, MSI_IRQ_NUM,
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001193 &advk_msi_domain_ops, pcie);
1194 if (!pcie->msi_inner_domain)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001195 return -ENOMEM;
1196
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001197 pcie->msi_domain =
1198 pci_msi_create_irq_domain(of_node_to_fwnode(node),
1199 msi_di, pcie->msi_inner_domain);
1200 if (!pcie->msi_domain) {
1201 irq_domain_remove(pcie->msi_inner_domain);
1202 return -ENOMEM;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001203 }
1204
1205 return 0;
1206}
1207
1208static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
1209{
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001210 irq_domain_remove(pcie->msi_domain);
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001211 irq_domain_remove(pcie->msi_inner_domain);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001212}
1213
1214static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
1215{
1216 struct device *dev = &pcie->pdev->dev;
1217 struct device_node *node = dev->of_node;
1218 struct device_node *pcie_intc_node;
1219 struct irq_chip *irq_chip;
Wen Yang3842f512019-02-27 12:40:40 +08001220 int ret = 0;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001221
Pali Rohárd212dce2021-08-20 17:50:20 +02001222 raw_spin_lock_init(&pcie->irq_lock);
1223
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001224 pcie_intc_node = of_get_next_child(node, NULL);
1225 if (!pcie_intc_node) {
1226 dev_err(dev, "No PCIe Intc node found\n");
1227 return -ENODEV;
1228 }
1229
1230 irq_chip = &pcie->irq_chip;
1231
1232 irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
1233 dev_name(dev));
1234 if (!irq_chip->name) {
Wen Yang3842f512019-02-27 12:40:40 +08001235 ret = -ENOMEM;
1236 goto out_put_node;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001237 }
1238
1239 irq_chip->irq_mask = advk_pcie_irq_mask;
1240 irq_chip->irq_mask_ack = advk_pcie_irq_mask;
1241 irq_chip->irq_unmask = advk_pcie_irq_unmask;
1242
1243 pcie->irq_domain =
Paul Burton0d2977a2017-08-15 16:26:03 -05001244 irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001245 &advk_pcie_irq_domain_ops, pcie);
1246 if (!pcie->irq_domain) {
1247 dev_err(dev, "Failed to get a INTx IRQ domain\n");
Wen Yang3842f512019-02-27 12:40:40 +08001248 ret = -ENOMEM;
1249 goto out_put_node;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001250 }
1251
Wen Yang3842f512019-02-27 12:40:40 +08001252out_put_node:
1253 of_node_put(pcie_intc_node);
1254 return ret;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001255}
1256
1257static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
1258{
1259 irq_domain_remove(pcie->irq_domain);
1260}
1261
1262static void advk_pcie_handle_msi(struct advk_pcie *pcie)
1263{
1264 u32 msi_val, msi_mask, msi_status, msi_idx;
1265 u16 msi_data;
1266
1267 msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
1268 msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
1269 msi_status = msi_val & ~msi_mask;
1270
1271 for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
1272 if (!(BIT(msi_idx) & msi_status))
1273 continue;
1274
1275 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
1276 msi_data = advk_readl(pcie, PCIE_MSI_PAYLOAD_REG) & 0xFF;
1277 generic_handle_irq(msi_data);
1278 }
1279
1280 advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
1281 PCIE_ISR0_REG);
1282}
1283
1284static void advk_pcie_handle_int(struct advk_pcie *pcie)
1285{
Victor Gu3430f922018-04-06 16:55:33 +02001286 u32 isr0_val, isr0_mask, isr0_status;
1287 u32 isr1_val, isr1_mask, isr1_status;
Marc Zyngierd21faba12021-08-02 17:26:19 +01001288 int i;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001289
Victor Gu3430f922018-04-06 16:55:33 +02001290 isr0_val = advk_readl(pcie, PCIE_ISR0_REG);
1291 isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
1292 isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001293
Victor Gu3430f922018-04-06 16:55:33 +02001294 isr1_val = advk_readl(pcie, PCIE_ISR1_REG);
1295 isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
1296 isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK);
1297
1298 if (!isr0_status && !isr1_status) {
1299 advk_writel(pcie, isr0_val, PCIE_ISR0_REG);
1300 advk_writel(pcie, isr1_val, PCIE_ISR1_REG);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001301 return;
1302 }
1303
1304 /* Process MSI interrupts */
Victor Gu3430f922018-04-06 16:55:33 +02001305 if (isr0_status & PCIE_ISR0_MSI_INT_PENDING)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001306 advk_pcie_handle_msi(pcie);
1307
1308 /* Process legacy interrupts */
Paul Burton0d2977a2017-08-15 16:26:03 -05001309 for (i = 0; i < PCI_NUM_INTX; i++) {
Victor Gu3430f922018-04-06 16:55:33 +02001310 if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i)))
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001311 continue;
1312
Victor Gu3430f922018-04-06 16:55:33 +02001313 advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i),
1314 PCIE_ISR1_REG);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001315
Marc Zyngierd21faba12021-08-02 17:26:19 +01001316 generic_handle_domain_irq(pcie->irq_domain, i);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001317 }
1318}
1319
1320static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
1321{
1322 struct advk_pcie *pcie = arg;
1323 u32 status;
1324
1325 status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
1326 if (!(status & PCIE_IRQ_CORE_INT))
1327 return IRQ_NONE;
1328
1329 advk_pcie_handle_int(pcie);
1330
1331 /* Clear interrupt */
1332 advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
1333
1334 return IRQ_HANDLED;
1335}
1336
Marek Behún36669702020-04-30 10:06:20 +02001337static void __maybe_unused advk_pcie_disable_phy(struct advk_pcie *pcie)
1338{
1339 phy_power_off(pcie->phy);
1340 phy_exit(pcie->phy);
1341}
1342
1343static int advk_pcie_enable_phy(struct advk_pcie *pcie)
1344{
1345 int ret;
1346
1347 if (!pcie->phy)
1348 return 0;
1349
1350 ret = phy_init(pcie->phy);
1351 if (ret)
1352 return ret;
1353
1354 ret = phy_set_mode(pcie->phy, PHY_MODE_PCIE);
1355 if (ret) {
1356 phy_exit(pcie->phy);
1357 return ret;
1358 }
1359
1360 ret = phy_power_on(pcie->phy);
Pali Rohárb0c6ae02020-09-02 16:43:44 +02001361 if (ret == -EOPNOTSUPP) {
1362 dev_warn(&pcie->pdev->dev, "PHY unsupported by firmware\n");
1363 } else if (ret) {
Marek Behún36669702020-04-30 10:06:20 +02001364 phy_exit(pcie->phy);
1365 return ret;
1366 }
1367
1368 return 0;
1369}
1370
1371static int advk_pcie_setup_phy(struct advk_pcie *pcie)
1372{
1373 struct device *dev = &pcie->pdev->dev;
1374 struct device_node *node = dev->of_node;
1375 int ret = 0;
1376
1377 pcie->phy = devm_of_phy_get(dev, node, NULL);
1378 if (IS_ERR(pcie->phy) && (PTR_ERR(pcie->phy) == -EPROBE_DEFER))
1379 return PTR_ERR(pcie->phy);
1380
1381 /* Old bindings miss the PHY handle */
1382 if (IS_ERR(pcie->phy)) {
1383 dev_warn(dev, "PHY unavailable (%ld)\n", PTR_ERR(pcie->phy));
1384 pcie->phy = NULL;
1385 return 0;
1386 }
1387
1388 ret = advk_pcie_enable_phy(pcie);
1389 if (ret)
1390 dev_err(dev, "Failed to initialize PHY (%d)\n", ret);
1391
1392 return ret;
1393}
1394
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001395static int advk_pcie_probe(struct platform_device *pdev)
1396{
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -05001397 struct device *dev = &pdev->dev;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001398 struct advk_pcie *pcie;
Lorenzo Pieralisi6b6de6a2017-06-28 15:13:56 -05001399 struct pci_host_bridge *bridge;
Pali Rohár64f160e2021-06-24 23:55:45 +02001400 struct resource_entry *entry;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001401 int ret, irq;
1402
Lorenzo Pieralisi6b6de6a2017-06-28 15:13:56 -05001403 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie));
1404 if (!bridge)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001405 return -ENOMEM;
1406
Lorenzo Pieralisi6b6de6a2017-06-28 15:13:56 -05001407 pcie = pci_host_bridge_priv(bridge);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001408 pcie->pdev = pdev;
Pali Rohár526a7692020-09-07 13:10:37 +02001409 platform_set_drvdata(pdev, pcie);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001410
Pali Rohár64f160e2021-06-24 23:55:45 +02001411 resource_list_for_each_entry(entry, &bridge->windows) {
1412 resource_size_t start = entry->res->start;
1413 resource_size_t size = resource_size(entry->res);
1414 unsigned long type = resource_type(entry->res);
1415 u64 win_size;
1416
1417 /*
1418 * Aardvark hardware allows to configure also PCIe window
1419 * for config type 0 and type 1 mapping, but driver uses
1420 * only PIO for issuing configuration transfers which does
1421 * not use PCIe window configuration.
1422 */
1423 if (type != IORESOURCE_MEM && type != IORESOURCE_MEM_64 &&
1424 type != IORESOURCE_IO)
1425 continue;
1426
1427 /*
1428 * Skip transparent memory resources. Default outbound access
1429 * configuration is set to transparent memory access so it
1430 * does not need window configuration.
1431 */
1432 if ((type == IORESOURCE_MEM || type == IORESOURCE_MEM_64) &&
1433 entry->offset == 0)
1434 continue;
1435
1436 /*
1437 * The n-th PCIe window is configured by tuple (match, remap, mask)
1438 * and an access to address A uses this window if A matches the
1439 * match with given mask.
1440 * So every PCIe window size must be a power of two and every start
1441 * address must be aligned to window size. Minimal size is 64 KiB
1442 * because lower 16 bits of mask must be zero. Remapped address
1443 * may have set only bits from the mask.
1444 */
1445 while (pcie->wins_count < OB_WIN_COUNT && size > 0) {
1446 /* Calculate the largest aligned window size */
1447 win_size = (1ULL << (fls64(size)-1)) |
1448 (start ? (1ULL << __ffs64(start)) : 0);
1449 win_size = 1ULL << __ffs64(win_size);
1450 if (win_size < 0x10000)
1451 break;
1452
1453 dev_dbg(dev,
1454 "Configuring PCIe window %d: [0x%llx-0x%llx] as %lu\n",
1455 pcie->wins_count, (unsigned long long)start,
1456 (unsigned long long)start + win_size, type);
1457
1458 if (type == IORESOURCE_IO) {
1459 pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_IO;
1460 pcie->wins[pcie->wins_count].match = pci_pio_to_address(start);
1461 } else {
1462 pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_MEM;
1463 pcie->wins[pcie->wins_count].match = start;
1464 }
1465 pcie->wins[pcie->wins_count].remap = start - entry->offset;
1466 pcie->wins[pcie->wins_count].mask = ~(win_size - 1);
1467
1468 if (pcie->wins[pcie->wins_count].remap & (win_size - 1))
1469 break;
1470
1471 start += win_size;
1472 size -= win_size;
1473 pcie->wins_count++;
1474 }
1475
1476 if (size > 0) {
1477 dev_err(&pcie->pdev->dev,
1478 "Invalid PCIe region [0x%llx-0x%llx]\n",
1479 (unsigned long long)entry->res->start,
1480 (unsigned long long)entry->res->end + 1);
1481 return -EINVAL;
1482 }
1483 }
1484
Dejin Zheng3cf0eea2020-07-08 23:56:14 +08001485 pcie->base = devm_platform_ioremap_resource(pdev, 0);
Wei Yongjun8b223352016-07-28 16:17:14 +00001486 if (IS_ERR(pcie->base))
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001487 return PTR_ERR(pcie->base);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001488
1489 irq = platform_get_irq(pdev, 0);
Aman Sharma0584bff2020-03-12 00:49:02 +05301490 if (irq < 0)
1491 return irq;
1492
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -05001493 ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001494 IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
1495 pcie);
1496 if (ret) {
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -05001497 dev_err(dev, "Failed to register interrupt\n");
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001498 return ret;
1499 }
1500
Pali Rohár5169a982020-04-30 10:06:18 +02001501 pcie->reset_gpio = devm_gpiod_get_from_of_node(dev, dev->of_node,
1502 "reset-gpios", 0,
1503 GPIOD_OUT_LOW,
1504 "pcie1-reset");
1505 ret = PTR_ERR_OR_ZERO(pcie->reset_gpio);
1506 if (ret) {
1507 if (ret == -ENOENT) {
1508 pcie->reset_gpio = NULL;
1509 } else {
1510 if (ret != -EPROBE_DEFER)
1511 dev_err(dev, "Failed to get reset-gpio: %i\n",
1512 ret);
1513 return ret;
1514 }
1515 }
1516
Marek Behún43fc6792020-04-30 10:06:17 +02001517 ret = of_pci_get_max_link_speed(dev->of_node);
1518 if (ret <= 0 || ret > 3)
1519 pcie->link_gen = 3;
1520 else
1521 pcie->link_gen = ret;
1522
Marek Behún36669702020-04-30 10:06:20 +02001523 ret = advk_pcie_setup_phy(pcie);
1524 if (ret)
1525 return ret;
1526
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001527 advk_pcie_setup_hw(pcie);
1528
Pali Rohár7862a612020-09-07 13:10:35 +02001529 ret = advk_sw_pci_bridge_init(pcie);
1530 if (ret) {
1531 dev_err(dev, "Failed to register emulated root PCI bridge\n");
1532 return ret;
1533 }
Zachary Zhang8a3ebd82018-10-18 17:37:19 +02001534
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001535 ret = advk_pcie_init_irq_domain(pcie);
1536 if (ret) {
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -05001537 dev_err(dev, "Failed to initialize irq\n");
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001538 return ret;
1539 }
1540
1541 ret = advk_pcie_init_msi_irq_domain(pcie);
1542 if (ret) {
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -05001543 dev_err(dev, "Failed to initialize irq\n");
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001544 advk_pcie_remove_irq_domain(pcie);
1545 return ret;
1546 }
1547
Lorenzo Pieralisi6b6de6a2017-06-28 15:13:56 -05001548 bridge->sysdata = pcie;
Lorenzo Pieralisi6b6de6a2017-06-28 15:13:56 -05001549 bridge->ops = &advk_pcie_ops;
1550
Thomas Petazzonic8e144f2018-06-29 11:16:20 +02001551 ret = pci_host_probe(bridge);
Lorenzo Pieralisi6b6de6a2017-06-28 15:13:56 -05001552 if (ret < 0) {
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001553 advk_pcie_remove_msi_irq_domain(pcie);
1554 advk_pcie_remove_irq_domain(pcie);
Lorenzo Pieralisi6b6de6a2017-06-28 15:13:56 -05001555 return ret;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001556 }
1557
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001558 return 0;
1559}
1560
Pali Rohár526a7692020-09-07 13:10:37 +02001561static int advk_pcie_remove(struct platform_device *pdev)
1562{
1563 struct advk_pcie *pcie = platform_get_drvdata(pdev);
1564 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
Pali Rohár64f160e2021-06-24 23:55:45 +02001565 int i;
Pali Rohár526a7692020-09-07 13:10:37 +02001566
1567 pci_lock_rescan_remove();
1568 pci_stop_root_bus(bridge->bus);
1569 pci_remove_root_bus(bridge->bus);
1570 pci_unlock_rescan_remove();
1571
1572 advk_pcie_remove_msi_irq_domain(pcie);
1573 advk_pcie_remove_irq_domain(pcie);
1574
Pali Rohár64f160e2021-06-24 23:55:45 +02001575 /* Disable outbound address windows mapping */
1576 for (i = 0; i < OB_WIN_COUNT; i++)
1577 advk_pcie_disable_ob_win(pcie, i);
1578
Pali Rohár526a7692020-09-07 13:10:37 +02001579 return 0;
1580}
1581
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001582static const struct of_device_id advk_pcie_of_match_table[] = {
1583 { .compatible = "marvell,armada-3700-pcie", },
1584 {},
1585};
Pali Rohár526a7692020-09-07 13:10:37 +02001586MODULE_DEVICE_TABLE(of, advk_pcie_of_match_table);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001587
1588static struct platform_driver advk_pcie_driver = {
1589 .driver = {
1590 .name = "advk-pcie",
1591 .of_match_table = advk_pcie_of_match_table,
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001592 },
1593 .probe = advk_pcie_probe,
Pali Rohár526a7692020-09-07 13:10:37 +02001594 .remove = advk_pcie_remove,
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001595};
Pali Rohár526a7692020-09-07 13:10:37 +02001596module_platform_driver(advk_pcie_driver);
1597
1598MODULE_DESCRIPTION("Aardvark PCIe controller");
1599MODULE_LICENSE("GPL v2");