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