blob: 4f5b44827d21329d94ffe63a7efd3dc3e08f8283 [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
Zachary Zhang8a3ebd82018-10-18 17:37:19 +020034#define PCIE_CORE_DEV_REV_REG 0x8
35#define PCIE_CORE_PCIEXP_CAP 0xc0
Thomas Petazzoni8c39d712016-06-30 11:32:31 +020036#define PCIE_CORE_ERR_CAPCTL_REG 0x118
37#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
38#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
39#define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7)
40#define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8)
Zachary Zhang8a3ebd82018-10-18 17:37:19 +020041#define PCIE_CORE_INT_A_ASSERT_ENABLE 1
42#define PCIE_CORE_INT_B_ASSERT_ENABLE 2
43#define PCIE_CORE_INT_C_ASSERT_ENABLE 3
44#define PCIE_CORE_INT_D_ASSERT_ENABLE 4
Thomas Petazzoni8c39d712016-06-30 11:32:31 +020045/* PIO registers base address and register offsets */
46#define PIO_BASE_ADDR 0x4000
47#define PIO_CTRL (PIO_BASE_ADDR + 0x0)
48#define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
49#define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
50#define PIO_STAT (PIO_BASE_ADDR + 0x4)
51#define PIO_COMPLETION_STATUS_SHIFT 7
52#define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
53#define PIO_COMPLETION_STATUS_OK 0
54#define PIO_COMPLETION_STATUS_UR 1
55#define PIO_COMPLETION_STATUS_CRS 2
56#define PIO_COMPLETION_STATUS_CA 4
Pali Rohár8ceeac32021-06-24 23:33:43 +020057#define PIO_NON_POSTED_REQ BIT(10)
Evan Wangfcb461e2021-07-22 16:40:38 +020058#define PIO_ERR_STATUS BIT(11)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +020059#define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
60#define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
61#define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
62#define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
63#define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
64#define PIO_START (PIO_BASE_ADDR + 0x1c)
65#define PIO_ISR (PIO_BASE_ADDR + 0x20)
66#define PIO_ISRM (PIO_BASE_ADDR + 0x24)
67
68/* Aardvark Control registers */
69#define CONTROL_BASE_ADDR 0x4800
70#define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
71#define PCIE_GEN_SEL_MSK 0x3
72#define PCIE_GEN_SEL_SHIFT 0x0
73#define SPEED_GEN_1 0
74#define SPEED_GEN_2 1
75#define SPEED_GEN_3 2
76#define IS_RC_MSK 1
77#define IS_RC_SHIFT 2
78#define LANE_CNT_MSK 0x18
79#define LANE_CNT_SHIFT 0x3
80#define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
81#define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
82#define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
83#define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
84#define LINK_TRAINING_EN BIT(6)
85#define LEGACY_INTA BIT(28)
86#define LEGACY_INTB BIT(29)
87#define LEGACY_INTC BIT(30)
88#define LEGACY_INTD BIT(31)
89#define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4)
90#define HOT_RESET_GEN BIT(0)
91#define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
92#define PCIE_CORE_CTRL2_RESERVED 0x7
93#define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
94#define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
95#define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6)
96#define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10)
Marek Behún36669702020-04-30 10:06:20 +020097#define PCIE_CORE_REF_CLK_REG (CONTROL_BASE_ADDR + 0x14)
98#define PCIE_CORE_REF_CLK_TX_ENABLE BIT(1)
Pali Rohár46ef6092021-10-05 20:09:44 +020099#define PCIE_CORE_REF_CLK_RX_ENABLE BIT(2)
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200100#define PCIE_MSG_LOG_REG (CONTROL_BASE_ADDR + 0x30)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200101#define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40)
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200102#define PCIE_MSG_PM_PME_MASK BIT(7)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200103#define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44)
104#define PCIE_ISR0_MSI_INT_PENDING BIT(24)
105#define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val))
106#define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val))
Pali Rohár1fb95d72021-10-05 20:09:46 +0200107#define PCIE_ISR0_ALL_MASK GENMASK(31, 0)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200108#define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48)
109#define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C)
110#define PCIE_ISR1_POWER_STATE_CHANGE BIT(4)
111#define PCIE_ISR1_FLUSH BIT(5)
Victor Gu3430f922018-04-06 16:55:33 +0200112#define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val))
Pali Rohár1fb95d72021-10-05 20:09:46 +0200113#define PCIE_ISR1_ALL_MASK GENMASK(31, 0)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200114#define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50)
115#define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54)
116#define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58)
117#define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C)
Pali Rohár7d8dc1f2021-11-30 18:29:06 +0100118#define PCIE_MSI_ALL_MASK GENMASK(31, 0)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200119#define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C)
Marek Behún95997722021-10-28 20:56:55 +0200120#define PCIE_MSI_DATA_MASK GENMASK(15, 0)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200121
Pali Rohár64f160e2021-06-24 23:55:45 +0200122/* PCIe window configuration */
123#define OB_WIN_BASE_ADDR 0x4c00
124#define OB_WIN_BLOCK_SIZE 0x20
125#define OB_WIN_COUNT 8
126#define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \
127 OB_WIN_BLOCK_SIZE * (win) + \
128 (offset))
129#define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00)
130#define OB_WIN_ENABLE BIT(0)
131#define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04)
132#define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08)
133#define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c)
134#define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10)
135#define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14)
136#define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18)
137#define OB_WIN_DEFAULT_ACTIONS (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4)
138#define OB_WIN_FUNC_NUM_MASK GENMASK(31, 24)
139#define OB_WIN_FUNC_NUM_SHIFT 24
140#define OB_WIN_FUNC_NUM_ENABLE BIT(23)
141#define OB_WIN_BUS_NUM_BITS_MASK GENMASK(22, 20)
142#define OB_WIN_BUS_NUM_BITS_SHIFT 20
143#define OB_WIN_MSG_CODE_ENABLE BIT(22)
144#define OB_WIN_MSG_CODE_MASK GENMASK(21, 14)
145#define OB_WIN_MSG_CODE_SHIFT 14
146#define OB_WIN_MSG_PAYLOAD_LEN BIT(12)
147#define OB_WIN_ATTR_ENABLE BIT(11)
148#define OB_WIN_ATTR_TC_MASK GENMASK(10, 8)
149#define OB_WIN_ATTR_TC_SHIFT 8
150#define OB_WIN_ATTR_RELAXED BIT(7)
151#define OB_WIN_ATTR_NOSNOOP BIT(6)
152#define OB_WIN_ATTR_POISON BIT(5)
153#define OB_WIN_ATTR_IDO BIT(4)
154#define OB_WIN_TYPE_MASK GENMASK(3, 0)
155#define OB_WIN_TYPE_SHIFT 0
156#define OB_WIN_TYPE_MEM 0x0
157#define OB_WIN_TYPE_IO 0x4
158#define OB_WIN_TYPE_CONFIG_TYPE0 0x8
159#define OB_WIN_TYPE_CONFIG_TYPE1 0x9
160#define OB_WIN_TYPE_MSG 0xc
161
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200162/* LMI registers base address and register offsets */
163#define LMI_BASE_ADDR 0x6000
164#define CFG_REG (LMI_BASE_ADDR + 0x0)
165#define LTSSM_SHIFT 24
166#define LTSSM_MASK 0x3f
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200167#define RC_BAR_CONFIG 0x300
Pali Rohár661c3992021-10-05 20:09:51 +0200168
169/* LTSSM values in CFG_REG */
170enum {
171 LTSSM_DETECT_QUIET = 0x0,
172 LTSSM_DETECT_ACTIVE = 0x1,
173 LTSSM_POLLING_ACTIVE = 0x2,
174 LTSSM_POLLING_COMPLIANCE = 0x3,
175 LTSSM_POLLING_CONFIGURATION = 0x4,
176 LTSSM_CONFIG_LINKWIDTH_START = 0x5,
177 LTSSM_CONFIG_LINKWIDTH_ACCEPT = 0x6,
178 LTSSM_CONFIG_LANENUM_ACCEPT = 0x7,
179 LTSSM_CONFIG_LANENUM_WAIT = 0x8,
180 LTSSM_CONFIG_COMPLETE = 0x9,
181 LTSSM_CONFIG_IDLE = 0xa,
182 LTSSM_RECOVERY_RCVR_LOCK = 0xb,
183 LTSSM_RECOVERY_SPEED = 0xc,
184 LTSSM_RECOVERY_RCVR_CFG = 0xd,
185 LTSSM_RECOVERY_IDLE = 0xe,
186 LTSSM_L0 = 0x10,
187 LTSSM_RX_L0S_ENTRY = 0x11,
188 LTSSM_RX_L0S_IDLE = 0x12,
189 LTSSM_RX_L0S_FTS = 0x13,
190 LTSSM_TX_L0S_ENTRY = 0x14,
191 LTSSM_TX_L0S_IDLE = 0x15,
192 LTSSM_TX_L0S_FTS = 0x16,
193 LTSSM_L1_ENTRY = 0x17,
194 LTSSM_L1_IDLE = 0x18,
195 LTSSM_L2_IDLE = 0x19,
196 LTSSM_L2_TRANSMIT_WAKE = 0x1a,
197 LTSSM_DISABLED = 0x20,
198 LTSSM_LOOPBACK_ENTRY_MASTER = 0x21,
199 LTSSM_LOOPBACK_ACTIVE_MASTER = 0x22,
200 LTSSM_LOOPBACK_EXIT_MASTER = 0x23,
201 LTSSM_LOOPBACK_ENTRY_SLAVE = 0x24,
202 LTSSM_LOOPBACK_ACTIVE_SLAVE = 0x25,
203 LTSSM_LOOPBACK_EXIT_SLAVE = 0x26,
204 LTSSM_HOT_RESET = 0x27,
205 LTSSM_RECOVERY_EQUALIZATION_PHASE0 = 0x28,
206 LTSSM_RECOVERY_EQUALIZATION_PHASE1 = 0x29,
207 LTSSM_RECOVERY_EQUALIZATION_PHASE2 = 0x2a,
208 LTSSM_RECOVERY_EQUALIZATION_PHASE3 = 0x2b,
209};
210
Pali Rohár7f71a402021-06-25 00:26:20 +0200211#define VENDOR_ID_REG (LMI_BASE_ADDR + 0x44)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200212
213/* PCIe core controller registers */
214#define CTRL_CORE_BASE_ADDR 0x18000
215#define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
216#define CTRL_MODE_SHIFT 0x0
217#define CTRL_MODE_MASK 0x1
218#define PCIE_CORE_MODE_DIRECT 0x0
219#define PCIE_CORE_MODE_COMMAND 0x1
220
221/* PCIe Central Interrupts Registers */
222#define CENTRAL_INT_BASE_ADDR 0x1b000
223#define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
224#define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
225#define PCIE_IRQ_CMDQ_INT BIT(0)
226#define PCIE_IRQ_MSI_STATUS_INT BIT(1)
227#define PCIE_IRQ_CMD_SENT_DONE BIT(3)
228#define PCIE_IRQ_DMA_INT BIT(4)
229#define PCIE_IRQ_IB_DXFERDONE BIT(5)
230#define PCIE_IRQ_OB_DXFERDONE BIT(6)
231#define PCIE_IRQ_OB_RXFERDONE BIT(7)
232#define PCIE_IRQ_COMPQ_INT BIT(12)
233#define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
234#define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
235#define PCIE_IRQ_CORE_INT BIT(16)
236#define PCIE_IRQ_CORE_INT_PIO BIT(17)
237#define PCIE_IRQ_DPMU_INT BIT(18)
238#define PCIE_IRQ_PCIE_MIS_INT BIT(19)
239#define PCIE_IRQ_MSI_INT1_DET BIT(20)
240#define PCIE_IRQ_MSI_INT2_DET BIT(21)
241#define PCIE_IRQ_RC_DBELL_DET BIT(22)
242#define PCIE_IRQ_EP_STATUS BIT(23)
Pali Rohár1fb95d72021-10-05 20:09:46 +0200243#define PCIE_IRQ_ALL_MASK GENMASK(31, 0)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200244#define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
245
246/* Transaction types */
247#define PCIE_CONFIG_RD_TYPE0 0x8
248#define PCIE_CONFIG_RD_TYPE1 0x9
249#define PCIE_CONFIG_WR_TYPE0 0xa
250#define PCIE_CONFIG_WR_TYPE1 0xb
251
Pali Rohár02bcec32021-07-22 16:40:39 +0200252#define PIO_RETRY_CNT 750000 /* 1.5 s */
Remi Pommarel7fbcb5d2019-09-27 10:55:02 +0200253#define PIO_RETRY_DELAY 2 /* 2 us*/
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200254
255#define LINK_WAIT_MAX_RETRIES 10
256#define LINK_WAIT_USLEEP_MIN 90000
257#define LINK_WAIT_USLEEP_MAX 100000
Remi Pommarel364b3f12019-05-22 23:33:51 +0200258#define RETRAIN_WAIT_MAX_RETRIES 10
259#define RETRAIN_WAIT_USLEEP_US 2000
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200260
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200261#define MSI_IRQ_NUM 32
262
Pali Rohár43f5c772021-07-22 16:40:41 +0200263#define CFG_RD_CRS_VAL 0xffff0001
264
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200265struct advk_pcie {
266 struct platform_device *pdev;
267 void __iomem *base;
Pali Rohár64f160e2021-06-24 23:55:45 +0200268 struct {
269 phys_addr_t match;
270 phys_addr_t remap;
271 phys_addr_t mask;
272 u32 actions;
273 } wins[OB_WIN_COUNT];
274 u8 wins_count;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200275 struct irq_domain *irq_domain;
276 struct irq_chip irq_chip;
Pali Rohárd212dce2021-08-20 17:50:20 +0200277 raw_spinlock_t irq_lock;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200278 struct irq_domain *msi_domain;
Thomas Petazzonif21a8b12017-02-28 15:31:14 +0100279 struct irq_domain *msi_inner_domain;
280 struct irq_chip msi_bottom_irq_chip;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200281 struct irq_chip msi_irq_chip;
Thomas Petazzonif21a8b12017-02-28 15:31:14 +0100282 struct msi_domain_info msi_domain_info;
283 DECLARE_BITMAP(msi_used, MSI_IRQ_NUM);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200284 struct mutex msi_used_lock;
285 u16 msi_msg;
Marek Behún43fc6792020-04-30 10:06:17 +0200286 int link_gen;
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200287 struct pci_bridge_emul bridge;
Pali Rohár5169a982020-04-30 10:06:18 +0200288 struct gpio_desc *reset_gpio;
Marek Behún36669702020-04-30 10:06:20 +0200289 struct phy *phy;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200290};
291
292static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
293{
294 writel(val, pcie->base + reg);
295}
296
297static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
298{
299 return readl(pcie->base + reg);
300}
301
Pali Rohár661c3992021-10-05 20:09:51 +0200302static u8 advk_pcie_ltssm_state(struct advk_pcie *pcie)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200303{
Pali Rohár661c3992021-10-05 20:09:51 +0200304 u32 val;
305 u8 ltssm_state;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200306
307 val = advk_readl(pcie, CFG_REG);
308 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
Pali Rohár661c3992021-10-05 20:09:51 +0200309 return ltssm_state;
310}
311
312static inline bool advk_pcie_link_up(struct advk_pcie *pcie)
313{
314 /* check if LTSSM is in normal operation - some L* state */
315 u8 ltssm_state = advk_pcie_ltssm_state(pcie);
316 return ltssm_state >= LTSSM_L0 && ltssm_state < LTSSM_DISABLED;
317}
318
Pali Rohár2b650b72021-10-05 20:09:52 +0200319static inline bool advk_pcie_link_active(struct advk_pcie *pcie)
320{
321 /*
322 * According to PCIe Base specification 3.0, Table 4-14: Link
323 * Status Mapped to the LTSSM, and 4.2.6.3.6 Configuration.Idle
324 * is Link Up mapped to LTSSM Configuration.Idle, Recovery, L0,
325 * L0s, L1 and L2 states. And according to 3.2.1. Data Link
326 * Control and Management State Machine Rules is DL Up status
327 * reported in DL Active state.
328 */
329 u8 ltssm_state = advk_pcie_ltssm_state(pcie);
330 return ltssm_state >= LTSSM_CONFIG_IDLE && ltssm_state < LTSSM_DISABLED;
331}
332
Pali Rohár661c3992021-10-05 20:09:51 +0200333static inline bool advk_pcie_link_training(struct advk_pcie *pcie)
334{
335 /*
336 * According to PCIe Base specification 3.0, Table 4-14: Link
337 * Status Mapped to the LTSSM is Link Training mapped to LTSSM
338 * Configuration and Recovery states.
339 */
340 u8 ltssm_state = advk_pcie_ltssm_state(pcie);
341 return ((ltssm_state >= LTSSM_CONFIG_LINKWIDTH_START &&
342 ltssm_state < LTSSM_L0) ||
343 (ltssm_state >= LTSSM_RECOVERY_EQUALIZATION_PHASE0 &&
344 ltssm_state <= LTSSM_RECOVERY_EQUALIZATION_PHASE3));
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200345}
346
347static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
348{
349 int retries;
350
351 /* check if the link is up or not */
352 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
Marek Behún43fc6792020-04-30 10:06:17 +0200353 if (advk_pcie_link_up(pcie))
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200354 return 0;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200355
356 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
357 }
358
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200359 return -ETIMEDOUT;
360}
361
Remi Pommarel364b3f12019-05-22 23:33:51 +0200362static void advk_pcie_wait_for_retrain(struct advk_pcie *pcie)
363{
364 size_t retries;
365
366 for (retries = 0; retries < RETRAIN_WAIT_MAX_RETRIES; ++retries) {
Pali Rohár661c3992021-10-05 20:09:51 +0200367 if (advk_pcie_link_training(pcie))
Remi Pommarel364b3f12019-05-22 23:33:51 +0200368 break;
369 udelay(RETRAIN_WAIT_USLEEP_US);
370 }
371}
372
Pali Rohárd0c6a342020-09-07 13:10:38 +0200373static void advk_pcie_issue_perst(struct advk_pcie *pcie)
374{
Pali Rohárd0c6a342020-09-07 13:10:38 +0200375 if (!pcie->reset_gpio)
376 return;
377
Pali Rohárd0c6a342020-09-07 13:10:38 +0200378 /* 10ms delay is needed for some cards */
379 dev_info(&pcie->pdev->dev, "issuing PERST via reset GPIO for 10ms\n");
380 gpiod_set_value_cansleep(pcie->reset_gpio, 1);
381 usleep_range(10000, 11000);
382 gpiod_set_value_cansleep(pcie->reset_gpio, 0);
383}
384
Pali Rohárf76b36d2021-10-05 20:09:50 +0200385static void advk_pcie_train_link(struct advk_pcie *pcie)
Marek Behún43fc6792020-04-30 10:06:17 +0200386{
Pali Rohárf76b36d2021-10-05 20:09:50 +0200387 struct device *dev = &pcie->pdev->dev;
Marek Behún43fc6792020-04-30 10:06:17 +0200388 u32 reg;
Pali Rohárf76b36d2021-10-05 20:09:50 +0200389 int ret;
Marek Behún43fc6792020-04-30 10:06:17 +0200390
Pali Rohárf76b36d2021-10-05 20:09:50 +0200391 /*
392 * Setup PCIe rev / gen compliance based on device tree property
393 * 'max-link-speed' which also forces maximal link speed.
394 */
Marek Behún43fc6792020-04-30 10:06:17 +0200395 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
396 reg &= ~PCIE_GEN_SEL_MSK;
Pali Rohárf76b36d2021-10-05 20:09:50 +0200397 if (pcie->link_gen == 3)
Marek Behún43fc6792020-04-30 10:06:17 +0200398 reg |= SPEED_GEN_3;
Pali Rohárf76b36d2021-10-05 20:09:50 +0200399 else if (pcie->link_gen == 2)
Marek Behún43fc6792020-04-30 10:06:17 +0200400 reg |= SPEED_GEN_2;
401 else
402 reg |= SPEED_GEN_1;
403 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
404
405 /*
Pali Rohárf76b36d2021-10-05 20:09:50 +0200406 * Set maximal link speed value also into PCIe Link Control 2 register.
407 * Armada 3700 Functional Specification says that default value is based
408 * on SPEED_GEN but tests showed that default value is always 8.0 GT/s.
Marek Behún43fc6792020-04-30 10:06:17 +0200409 */
Pali Rohárf76b36d2021-10-05 20:09:50 +0200410 reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2);
411 reg &= ~PCI_EXP_LNKCTL2_TLS;
412 if (pcie->link_gen == 3)
413 reg |= PCI_EXP_LNKCTL2_TLS_8_0GT;
414 else if (pcie->link_gen == 2)
415 reg |= PCI_EXP_LNKCTL2_TLS_5_0GT;
416 else
417 reg |= PCI_EXP_LNKCTL2_TLS_2_5GT;
418 advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2);
419
420 /* Enable link training after selecting PCIe generation */
Marek Behún43fc6792020-04-30 10:06:17 +0200421 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
422 reg |= LINK_TRAINING_EN;
423 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
424
425 /*
Pali Rohárd0c6a342020-09-07 13:10:38 +0200426 * Reset PCIe card via PERST# signal. Some cards are not detected
427 * during link training when they are in some non-initial state.
428 */
429 advk_pcie_issue_perst(pcie);
430
431 /*
432 * PERST# signal could have been asserted by pinctrl subsystem before
433 * probe() callback has been called or issued explicitly by reset gpio
434 * function advk_pcie_issue_perst(), making the endpoint going into
Pali Rohárf76b36d2021-10-05 20:09:50 +0200435 * fundamental reset. As required by PCI Express spec (PCI Express
436 * Base Specification, REV. 4.0 PCI Express, February 19 2014, 6.6.1
437 * Conventional Reset) a delay for at least 100ms after such a reset
438 * before sending a Configuration Request to the device is needed.
439 * So wait until PCIe link is up. Function advk_pcie_wait_for_link()
440 * waits for link at least 900ms.
Pali Rohárd0c6a342020-09-07 13:10:38 +0200441 */
Pali Rohárf76b36d2021-10-05 20:09:50 +0200442 ret = advk_pcie_wait_for_link(pcie);
443 if (ret < 0)
444 dev_err(dev, "link never came up\n");
445 else
446 dev_info(dev, "link up\n");
Marek Behún43fc6792020-04-30 10:06:17 +0200447}
448
Pali Rohár64f160e2021-06-24 23:55:45 +0200449/*
450 * Set PCIe address window register which could be used for memory
451 * mapping.
452 */
453static void advk_pcie_set_ob_win(struct advk_pcie *pcie, u8 win_num,
454 phys_addr_t match, phys_addr_t remap,
455 phys_addr_t mask, u32 actions)
456{
457 advk_writel(pcie, OB_WIN_ENABLE |
458 lower_32_bits(match), OB_WIN_MATCH_LS(win_num));
459 advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num));
460 advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num));
461 advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num));
462 advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num));
463 advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num));
464 advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num));
465}
466
467static void advk_pcie_disable_ob_win(struct advk_pcie *pcie, u8 win_num)
468{
469 advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num));
470 advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num));
471 advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num));
472 advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num));
473 advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num));
474 advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num));
475 advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num));
476}
477
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200478static void advk_pcie_setup_hw(struct advk_pcie *pcie)
479{
480 u32 reg;
Pali Rohár64f160e2021-06-24 23:55:45 +0200481 int i;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200482
Pali Rohár46ef6092021-10-05 20:09:44 +0200483 /*
484 * Configure PCIe Reference clock. Direction is from the PCIe
485 * controller to the endpoint card, so enable transmitting of
486 * Reference clock differential signal off-chip and disable
487 * receiving off-chip differential signal.
488 */
Marek Behún36669702020-04-30 10:06:20 +0200489 reg = advk_readl(pcie, PCIE_CORE_REF_CLK_REG);
490 reg |= PCIE_CORE_REF_CLK_TX_ENABLE;
Pali Rohár46ef6092021-10-05 20:09:44 +0200491 reg &= ~PCIE_CORE_REF_CLK_RX_ENABLE;
Marek Behún36669702020-04-30 10:06:20 +0200492 advk_writel(pcie, reg, PCIE_CORE_REF_CLK_REG);
493
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200494 /* Set to Direct mode */
495 reg = advk_readl(pcie, CTRL_CONFIG_REG);
496 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
497 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
498 advk_writel(pcie, reg, CTRL_CONFIG_REG);
499
500 /* Set PCI global control register to RC mode */
501 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
502 reg |= (IS_RC_MSK << IS_RC_SHIFT);
503 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
504
Pali Rohár7f71a402021-06-25 00:26:20 +0200505 /*
506 * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab.
507 * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor
508 * id in high 16 bits. Updating this register changes readback value of
509 * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround
510 * for erratum 4.1: "The value of device and vendor ID is incorrect".
511 */
512 reg = (PCI_VENDOR_ID_MARVELL << 16) | PCI_VENDOR_ID_MARVELL;
513 advk_writel(pcie, reg, VENDOR_ID_REG);
514
Pali Rohár84e1b402021-10-28 20:56:57 +0200515 /*
516 * Change Class Code of PCI Bridge device to PCI Bridge (0x600400),
517 * because the default value is Mass storage controller (0x010400).
518 *
519 * Note that this Aardvark PCI Bridge does not have compliant Type 1
520 * Configuration Space and it even cannot be accessed via Aardvark's
521 * PCI config space access method. Something like config space is
522 * available in internal Aardvark registers starting at offset 0x0
523 * and is reported as Type 0. In range 0x10 - 0x34 it has totally
524 * different registers.
525 *
526 * Therefore driver uses emulation of PCI Bridge which emulates
527 * access to configuration space via internal Aardvark registers or
528 * emulated configuration buffer.
529 */
530 reg = advk_readl(pcie, PCIE_CORE_DEV_REV_REG);
531 reg &= ~0xffffff00;
532 reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8;
533 advk_writel(pcie, reg, PCIE_CORE_DEV_REV_REG);
534
Pali Rohár771153f2021-10-28 20:56:56 +0200535 /* Disable Root Bridge I/O space, memory space and bus mastering */
536 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
537 reg &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
538 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
539
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200540 /* Set Advanced Error Capabilities and Control PF0 register */
541 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
542 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
543 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
544 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
545 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
546
Pali Rohár96be36d2020-04-30 10:06:21 +0200547 /* Set PCIe Device Control register */
548 reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL);
549 reg &= ~PCI_EXP_DEVCTL_RELAX_EN;
550 reg &= ~PCI_EXP_DEVCTL_NOSNOOP_EN;
Pali Rohára4e17d62021-10-05 20:09:41 +0200551 reg &= ~PCI_EXP_DEVCTL_PAYLOAD;
Pali Rohár96be36d2020-04-30 10:06:21 +0200552 reg &= ~PCI_EXP_DEVCTL_READRQ;
Pali Rohára4e17d62021-10-05 20:09:41 +0200553 reg |= PCI_EXP_DEVCTL_PAYLOAD_512B;
Pali Rohár96be36d2020-04-30 10:06:21 +0200554 reg |= PCI_EXP_DEVCTL_READRQ_512B;
555 advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200556
557 /* Program PCIe Control 2 to disable strict ordering */
558 reg = PCIE_CORE_CTRL2_RESERVED |
559 PCIE_CORE_CTRL2_TD_ENABLE;
560 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
561
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200562 /* Set lane X1 */
563 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
564 reg &= ~LANE_CNT_MSK;
565 reg |= LANE_COUNT_1;
566 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
567
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200568 /* Enable MSI */
569 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
570 reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
571 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
572
573 /* Clear all interrupts */
Pali Rohár7d8dc1f2021-11-30 18:29:06 +0100574 advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200575 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
576 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
577 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
578
579 /* Disable All ISR0/1 Sources */
580 reg = PCIE_ISR0_ALL_MASK;
581 reg &= ~PCIE_ISR0_MSI_INT_PENDING;
582 advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
583
584 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
585
Bjorn Helgaasf6b6aef2019-05-30 08:05:58 -0500586 /* Unmask all MSIs */
Pali Rohár7d8dc1f2021-11-30 18:29:06 +0100587 advk_writel(pcie, ~(u32)PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200588
589 /* Enable summary interrupt for GIC SPI source */
590 reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
591 advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
592
Pali Rohár64f160e2021-06-24 23:55:45 +0200593 /*
594 * Enable AXI address window location generation:
595 * When it is enabled, the default outbound window
596 * configurations (Default User Field: 0xD0074CFC)
597 * are used to transparent address translation for
598 * the outbound transactions. Thus, PCIe address
599 * windows are not required for transparent memory
600 * access when default outbound window configuration
601 * is set for memory access.
602 */
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200603 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
604 reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
605 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
606
Pali Rohár64f160e2021-06-24 23:55:45 +0200607 /*
608 * Set memory access in Default User Field so it
609 * is not required to configure PCIe address for
610 * transparent memory access.
611 */
612 advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS);
613
614 /*
615 * Bypass the address window mapping for PIO:
616 * Since PIO access already contains all required
617 * info over AXI interface by PIO registers, the
618 * address window is not required.
619 */
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200620 reg = advk_readl(pcie, PIO_CTRL);
621 reg |= PIO_CTRL_ADDR_WIN_DISABLE;
622 advk_writel(pcie, reg, PIO_CTRL);
623
Pali Rohár64f160e2021-06-24 23:55:45 +0200624 /*
625 * Configure PCIe address windows for non-memory or
626 * non-transparent access as by default PCIe uses
627 * transparent memory access.
628 */
629 for (i = 0; i < pcie->wins_count; i++)
630 advk_pcie_set_ob_win(pcie, i,
631 pcie->wins[i].match, pcie->wins[i].remap,
632 pcie->wins[i].mask, pcie->wins[i].actions);
633
634 /* Disable remaining PCIe outbound windows */
635 for (i = pcie->wins_count; i < OB_WIN_COUNT; i++)
636 advk_pcie_disable_ob_win(pcie, i);
637
Marek Behún43fc6792020-04-30 10:06:17 +0200638 advk_pcie_train_link(pcie);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200639}
640
Pali Rohár43f5c772021-07-22 16:40:41 +0200641static int advk_pcie_check_pio_status(struct advk_pcie *pcie, bool allow_crs, u32 *val)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200642{
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -0500643 struct device *dev = &pcie->pdev->dev;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200644 u32 reg;
645 unsigned int status;
646 char *strcomp_status, *str_posted;
Pali Rohár223dec12021-10-05 20:09:48 +0200647 int ret;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200648
649 reg = advk_readl(pcie, PIO_STAT);
650 status = (reg & PIO_COMPLETION_STATUS_MASK) >>
651 PIO_COMPLETION_STATUS_SHIFT;
652
Evan Wangfcb461e2021-07-22 16:40:38 +0200653 /*
654 * According to HW spec, the PIO status check sequence as below:
655 * 1) even if COMPLETION_STATUS(bit9:7) indicates successful,
656 * it still needs to check Error Status(bit11), only when this bit
657 * indicates no error happen, the operation is successful.
658 * 2) value Unsupported Request(1) of COMPLETION_STATUS(bit9:7) only
659 * means a PIO write error, and for PIO read it is successful with
660 * a read value of 0xFFFFFFFF.
661 * 3) value Completion Retry Status(CRS) of COMPLETION_STATUS(bit9:7)
662 * only means a PIO write error, and for PIO read it is successful
663 * with a read value of 0xFFFF0001.
664 * 4) value Completer Abort (CA) of COMPLETION_STATUS(bit9:7) means
665 * error for both PIO read and PIO write operation.
666 * 5) other errors are indicated as 'unknown'.
667 */
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200668 switch (status) {
Evan Wangfcb461e2021-07-22 16:40:38 +0200669 case PIO_COMPLETION_STATUS_OK:
670 if (reg & PIO_ERR_STATUS) {
671 strcomp_status = "COMP_ERR";
Pali Rohár223dec12021-10-05 20:09:48 +0200672 ret = -EFAULT;
Evan Wangfcb461e2021-07-22 16:40:38 +0200673 break;
674 }
675 /* Get the read result */
676 if (val)
677 *val = advk_readl(pcie, PIO_RD_DATA);
678 /* No error */
679 strcomp_status = NULL;
Pali Rohár223dec12021-10-05 20:09:48 +0200680 ret = 0;
Evan Wangfcb461e2021-07-22 16:40:38 +0200681 break;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200682 case PIO_COMPLETION_STATUS_UR:
683 strcomp_status = "UR";
Pali Rohár223dec12021-10-05 20:09:48 +0200684 ret = -EOPNOTSUPP;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200685 break;
686 case PIO_COMPLETION_STATUS_CRS:
Pali Rohár43f5c772021-07-22 16:40:41 +0200687 if (allow_crs && val) {
688 /* PCIe r4.0, sec 2.3.2, says:
689 * If CRS Software Visibility is enabled:
690 * For a Configuration Read Request that includes both
691 * bytes of the Vendor ID field of a device Function's
692 * Configuration Space Header, the Root Complex must
693 * complete the Request to the host by returning a
694 * read-data value of 0001h for the Vendor ID field and
695 * all '1's for any additional bytes included in the
696 * request.
697 *
698 * So CRS in this case is not an error status.
699 */
700 *val = CFG_RD_CRS_VAL;
701 strcomp_status = NULL;
Pali Rohár223dec12021-10-05 20:09:48 +0200702 ret = 0;
Pali Rohár43f5c772021-07-22 16:40:41 +0200703 break;
704 }
Evan Wangfcb461e2021-07-22 16:40:38 +0200705 /* PCIe r4.0, sec 2.3.2, says:
706 * If CRS Software Visibility is not enabled, the Root Complex
707 * must re-issue the Configuration Request as a new Request.
Pali Rohár43f5c772021-07-22 16:40:41 +0200708 * If CRS Software Visibility is enabled: For a Configuration
709 * Write Request or for any other Configuration Read Request,
710 * the Root Complex must re-issue the Configuration Request as
711 * a new Request.
Evan Wangfcb461e2021-07-22 16:40:38 +0200712 * A Root Complex implementation may choose to limit the number
713 * of Configuration Request/CRS Completion Status loops before
714 * determining that something is wrong with the target of the
715 * Request and taking appropriate action, e.g., complete the
716 * Request to the host as a failed transaction.
717 *
Pali Rohár223dec12021-10-05 20:09:48 +0200718 * So return -EAGAIN and caller (pci-aardvark.c driver) will
719 * re-issue request again up to the PIO_RETRY_CNT retries.
Evan Wangfcb461e2021-07-22 16:40:38 +0200720 */
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200721 strcomp_status = "CRS";
Pali Rohár223dec12021-10-05 20:09:48 +0200722 ret = -EAGAIN;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200723 break;
724 case PIO_COMPLETION_STATUS_CA:
725 strcomp_status = "CA";
Pali Rohár223dec12021-10-05 20:09:48 +0200726 ret = -ECANCELED;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200727 break;
728 default:
729 strcomp_status = "Unknown";
Pali Rohár223dec12021-10-05 20:09:48 +0200730 ret = -EINVAL;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200731 break;
732 }
733
Evan Wangfcb461e2021-07-22 16:40:38 +0200734 if (!strcomp_status)
Pali Rohár223dec12021-10-05 20:09:48 +0200735 return ret;
Evan Wangfcb461e2021-07-22 16:40:38 +0200736
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200737 if (reg & PIO_NON_POSTED_REQ)
738 str_posted = "Non-posted";
739 else
740 str_posted = "Posted";
741
Marek Behún464de7e2021-10-05 20:09:42 +0200742 dev_dbg(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200743 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
Evan Wangfcb461e2021-07-22 16:40:38 +0200744
Pali Rohár223dec12021-10-05 20:09:48 +0200745 return ret;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200746}
747
748static int advk_pcie_wait_pio(struct advk_pcie *pcie)
749{
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -0500750 struct device *dev = &pcie->pdev->dev;
Remi Pommarel7fbcb5d2019-09-27 10:55:02 +0200751 int i;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200752
Pali Rohár223dec12021-10-05 20:09:48 +0200753 for (i = 1; i <= PIO_RETRY_CNT; i++) {
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200754 u32 start, isr;
755
756 start = advk_readl(pcie, PIO_START);
757 isr = advk_readl(pcie, PIO_ISR);
758 if (!start && isr)
Pali Rohár223dec12021-10-05 20:09:48 +0200759 return i;
Remi Pommarel7fbcb5d2019-09-27 10:55:02 +0200760 udelay(PIO_RETRY_DELAY);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200761 }
762
Pali Rohárf1813992021-06-08 22:36:55 +0200763 dev_err(dev, "PIO read/write transfer time out\n");
Thomas Petazzoni8c39d712016-06-30 11:32:31 +0200764 return -ETIMEDOUT;
765}
766
Pali Rohár771153f2021-10-28 20:56:56 +0200767static pci_bridge_emul_read_status_t
768advk_pci_bridge_emul_base_conf_read(struct pci_bridge_emul *bridge,
769 int reg, u32 *value)
770{
771 struct advk_pcie *pcie = bridge->data;
772
773 switch (reg) {
774 case PCI_COMMAND:
775 *value = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
776 return PCI_BRIDGE_EMUL_HANDLED;
777
Pali Rohárbc4fac42021-10-28 20:56:58 +0200778 case PCI_INTERRUPT_LINE: {
779 /*
780 * From the whole 32bit register we support reading from HW only
781 * one bit: PCI_BRIDGE_CTL_BUS_RESET.
782 * Other bits are retrieved only from emulated config buffer.
783 */
784 __le32 *cfgspace = (__le32 *)&bridge->conf;
785 u32 val = le32_to_cpu(cfgspace[PCI_INTERRUPT_LINE / 4]);
786 if (advk_readl(pcie, PCIE_CORE_CTRL1_REG) & HOT_RESET_GEN)
787 val |= PCI_BRIDGE_CTL_BUS_RESET << 16;
788 else
789 val &= ~(PCI_BRIDGE_CTL_BUS_RESET << 16);
790 *value = val;
791 return PCI_BRIDGE_EMUL_HANDLED;
792 }
793
Pali Rohár771153f2021-10-28 20:56:56 +0200794 default:
795 return PCI_BRIDGE_EMUL_NOT_HANDLED;
796 }
797}
798
799static void
800advk_pci_bridge_emul_base_conf_write(struct pci_bridge_emul *bridge,
801 int reg, u32 old, u32 new, u32 mask)
802{
803 struct advk_pcie *pcie = bridge->data;
804
805 switch (reg) {
806 case PCI_COMMAND:
807 advk_writel(pcie, new, PCIE_CORE_CMD_STATUS_REG);
808 break;
809
Pali Rohárbc4fac42021-10-28 20:56:58 +0200810 case PCI_INTERRUPT_LINE:
811 if (mask & (PCI_BRIDGE_CTL_BUS_RESET << 16)) {
812 u32 val = advk_readl(pcie, PCIE_CORE_CTRL1_REG);
813 if (new & (PCI_BRIDGE_CTL_BUS_RESET << 16))
814 val |= HOT_RESET_GEN;
815 else
816 val &= ~HOT_RESET_GEN;
817 advk_writel(pcie, val, PCIE_CORE_CTRL1_REG);
818 }
819 break;
820
Pali Rohár771153f2021-10-28 20:56:56 +0200821 default:
822 break;
823 }
824}
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200825
826static pci_bridge_emul_read_status_t
827advk_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
828 int reg, u32 *value)
829{
830 struct advk_pcie *pcie = bridge->data;
831
832
833 switch (reg) {
834 case PCI_EXP_SLTCTL:
835 *value = PCI_EXP_SLTSTA_PDS << 16;
836 return PCI_BRIDGE_EMUL_HANDLED;
837
838 case PCI_EXP_RTCTL: {
839 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG);
Remi Pommarelc0f05a62019-06-14 12:10:59 +0200840 *value = (val & PCIE_MSG_PM_PME_MASK) ? 0 : PCI_EXP_RTCTL_PMEIE;
Pali Rohárd4190522021-10-05 20:09:43 +0200841 *value |= le16_to_cpu(bridge->pcie_conf.rootctl) & PCI_EXP_RTCTL_CRSSVE;
Pali Rohár43f5c772021-07-22 16:40:41 +0200842 *value |= PCI_EXP_RTCAP_CRSVIS << 16;
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200843 return PCI_BRIDGE_EMUL_HANDLED;
844 }
845
846 case PCI_EXP_RTSTA: {
847 u32 isr0 = advk_readl(pcie, PCIE_ISR0_REG);
848 u32 msglog = advk_readl(pcie, PCIE_MSG_LOG_REG);
849 *value = (isr0 & PCIE_MSG_PM_PME_MASK) << 16 | (msglog >> 16);
850 return PCI_BRIDGE_EMUL_HANDLED;
851 }
852
Pali Rohár2b650b72021-10-05 20:09:52 +0200853 case PCI_EXP_LNKCAP: {
854 u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg);
855 /*
856 * PCI_EXP_LNKCAP_DLLLARC bit is hardwired in aardvark HW to 0.
857 * But support for PCI_EXP_LNKSTA_DLLLA is emulated via ltssm
858 * state so explicitly enable PCI_EXP_LNKCAP_DLLLARC flag.
859 */
860 val |= PCI_EXP_LNKCAP_DLLLARC;
861 *value = val;
862 return PCI_BRIDGE_EMUL_HANDLED;
863 }
864
Remi Pommarel364b3f12019-05-22 23:33:51 +0200865 case PCI_EXP_LNKCTL: {
866 /* u32 contains both PCI_EXP_LNKCTL and PCI_EXP_LNKSTA */
867 u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg) &
868 ~(PCI_EXP_LNKSTA_LT << 16);
Pali Rohár661c3992021-10-05 20:09:51 +0200869 if (advk_pcie_link_training(pcie))
Remi Pommarel364b3f12019-05-22 23:33:51 +0200870 val |= (PCI_EXP_LNKSTA_LT << 16);
Pali Rohár2b650b72021-10-05 20:09:52 +0200871 if (advk_pcie_link_active(pcie))
872 val |= (PCI_EXP_LNKSTA_DLLLA << 16);
Remi Pommarel364b3f12019-05-22 23:33:51 +0200873 *value = val;
874 return PCI_BRIDGE_EMUL_HANDLED;
875 }
876
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200877 case PCI_EXP_DEVCAP:
878 case PCI_EXP_DEVCTL:
Pali Rohár1d3e1702021-11-30 18:29:05 +0100879 case PCI_EXP_DEVCAP2:
880 case PCI_EXP_DEVCTL2:
881 case PCI_EXP_LNKCAP2:
882 case PCI_EXP_LNKCTL2:
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200883 *value = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg);
884 return PCI_BRIDGE_EMUL_HANDLED;
Pali Rohár1d3e1702021-11-30 18:29:05 +0100885
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200886 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) {
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200899 case PCI_EXP_LNKCTL:
900 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
Remi Pommarel364b3f12019-05-22 23:33:51 +0200901 if (new & PCI_EXP_LNKCTL_RL)
902 advk_pcie_wait_for_retrain(pcie);
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200903 break;
904
Remi Pommarelc0f05a62019-06-14 12:10:59 +0200905 case PCI_EXP_RTCTL: {
906 /* Only mask/unmask PME interrupt */
907 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG) &
908 ~PCIE_MSG_PM_PME_MASK;
909 if ((new & PCI_EXP_RTCTL_PMEIE) == 0)
910 val |= PCIE_MSG_PM_PME_MASK;
911 advk_writel(pcie, val, PCIE_ISR0_MASK_REG);
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200912 break;
Remi Pommarelc0f05a62019-06-14 12:10:59 +0200913 }
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200914
915 case PCI_EXP_RTSTA:
916 new = (new & PCI_EXP_RTSTA_PME) >> 9;
917 advk_writel(pcie, new, PCIE_ISR0_REG);
918 break;
919
Pali Rohár1d3e1702021-11-30 18:29:05 +0100920 case PCI_EXP_DEVCTL:
921 case PCI_EXP_DEVCTL2:
922 case PCI_EXP_LNKCTL2:
923 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
924 break;
925
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200926 default:
927 break;
928 }
929}
930
Wei Yongjund3b34d042019-03-01 06:58:09 +0000931static struct pci_bridge_emul_ops advk_pci_bridge_emul_ops = {
Pali Rohár771153f2021-10-28 20:56:56 +0200932 .read_base = advk_pci_bridge_emul_base_conf_read,
933 .write_base = advk_pci_bridge_emul_base_conf_write,
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200934 .read_pcie = advk_pci_bridge_emul_pcie_conf_read,
935 .write_pcie = advk_pci_bridge_emul_pcie_conf_write,
936};
937
938/*
939 * Initialize the configuration space of the PCI-to-PCI bridge
940 * associated with the given PCIe interface.
941 */
Pali Rohár7862a612020-09-07 13:10:35 +0200942static int advk_sw_pci_bridge_init(struct advk_pcie *pcie)
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200943{
944 struct pci_bridge_emul *bridge = &pcie->bridge;
945
Grzegorz Jaszczyke0787232019-07-16 14:12:07 +0200946 bridge->conf.vendor =
947 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) & 0xffff);
948 bridge->conf.device =
949 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) >> 16);
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200950 bridge->conf.class_revision =
Grzegorz Jaszczyke0787232019-07-16 14:12:07 +0200951 cpu_to_le32(advk_readl(pcie, PCIE_CORE_DEV_REV_REG) & 0xff);
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200952
953 /* Support 32 bits I/O addressing */
954 bridge->conf.iobase = PCI_IO_RANGE_TYPE_32;
955 bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32;
956
957 /* Support 64 bits memory pref */
Grzegorz Jaszczyke0787232019-07-16 14:12:07 +0200958 bridge->conf.pref_mem_base = cpu_to_le16(PCI_PREF_RANGE_TYPE_64);
959 bridge->conf.pref_mem_limit = cpu_to_le16(PCI_PREF_RANGE_TYPE_64);
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200960
961 /* Support interrupt A for MSI feature */
962 bridge->conf.intpin = PCIE_CORE_INT_A_ASSERT_ENABLE;
963
Pali Rohár1f1050c2021-11-24 16:59:43 +0100964 /* Aardvark HW provides PCIe Capability structure in version 2 */
965 bridge->pcie_conf.cap = cpu_to_le16(2);
966
Pali Rohár454c5322021-10-05 20:09:49 +0200967 /* Indicates supports for Completion Retry Status */
968 bridge->pcie_conf.rootcap = cpu_to_le16(PCI_EXP_RTCAP_CRSVIS);
969
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200970 bridge->has_pcie = true;
971 bridge->data = pcie;
972 bridge->ops = &advk_pci_bridge_emul_ops;
973
Pali Rohár454c5322021-10-05 20:09:49 +0200974 return pci_bridge_emul_init(bridge, 0);
Zachary Zhang8a3ebd82018-10-18 17:37:19 +0200975}
976
Thomas Petazzoni248d4e52018-04-06 16:55:35 +0200977static bool advk_pcie_valid_device(struct advk_pcie *pcie, struct pci_bus *bus,
978 int devfn)
979{
Rob Herring11e97972020-07-21 20:24:59 -0600980 if (pci_is_root_bus(bus) && PCI_SLOT(devfn) != 0)
Thomas Petazzoni248d4e52018-04-06 16:55:35 +0200981 return false;
982
Pali Rohár70e38022020-07-02 10:30:36 +0200983 /*
984 * If the link goes down after we check for link-up, nothing bad
985 * happens but the config access times out.
986 */
Bjorn Helgaas49e427e2020-08-05 18:24:21 -0500987 if (!pci_is_root_bus(bus) && !advk_pcie_link_up(pcie))
Pali Rohár70e38022020-07-02 10:30:36 +0200988 return false;
989
Thomas Petazzoni248d4e52018-04-06 16:55:35 +0200990 return true;
991}
992
Pali Rohárf1813992021-06-08 22:36:55 +0200993static bool advk_pcie_pio_is_running(struct advk_pcie *pcie)
994{
995 struct device *dev = &pcie->pdev->dev;
996
997 /*
998 * Trying to start a new PIO transfer when previous has not completed
999 * cause External Abort on CPU which results in kernel panic:
1000 *
1001 * SError Interrupt on CPU0, code 0xbf000002 -- SError
1002 * Kernel panic - not syncing: Asynchronous SError Interrupt
1003 *
1004 * Functions advk_pcie_rd_conf() and advk_pcie_wr_conf() are protected
1005 * by raw_spin_lock_irqsave() at pci_lock_config() level to prevent
1006 * concurrent calls at the same time. But because PIO transfer may take
1007 * about 1.5s when link is down or card is disconnected, it means that
1008 * advk_pcie_wait_pio() does not always have to wait for completion.
1009 *
1010 * Some versions of ARM Trusted Firmware handles this External Abort at
1011 * EL3 level and mask it to prevent kernel panic. Relevant TF-A commit:
1012 * https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=3c7dcdac5c50
1013 */
1014 if (advk_readl(pcie, PIO_START)) {
1015 dev_err(dev, "Previous PIO read/write transfer is still running\n");
1016 return true;
1017 }
1018
1019 return false;
1020}
1021
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001022static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
1023 int where, int size, u32 *val)
1024{
1025 struct advk_pcie *pcie = bus->sysdata;
Pali Rohár223dec12021-10-05 20:09:48 +02001026 int retry_count;
Pali Rohár43f5c772021-07-22 16:40:41 +02001027 bool allow_crs;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001028 u32 reg;
1029 int ret;
1030
Naveen Naidu5a50b8b2021-11-18 19:33:21 +05301031 if (!advk_pcie_valid_device(pcie, bus, devfn))
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001032 return PCIBIOS_DEVICE_NOT_FOUND;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001033
Rob Herring11e97972020-07-21 20:24:59 -06001034 if (pci_is_root_bus(bus))
Zachary Zhang8a3ebd82018-10-18 17:37:19 +02001035 return pci_bridge_emul_conf_read(&pcie->bridge, where,
1036 size, val);
1037
Pali Rohár43f5c772021-07-22 16:40:41 +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
Marek Behún67cb2a42021-10-05 20:09:47 +02001047 if (advk_pcie_pio_is_running(pcie))
1048 goto try_crs;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001049
1050 /* Program the control register */
1051 reg = advk_readl(pcie, PIO_CTRL);
1052 reg &= ~PIO_CTRL_TYPE_MASK;
Rob Herring11e97972020-07-21 20:24:59 -06001053 if (pci_is_root_bus(bus->parent))
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001054 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 */
Krzysztof Wilczyńskie7708f52020-11-29 23:07:39 +00001060 reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001061 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
Pali Rohár223dec12021-10-05 20:09:48 +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);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001072
Pali Rohár223dec12021-10-05 20:09:48 +02001073 ret = advk_pcie_wait_pio(pcie);
1074 if (ret < 0)
1075 goto try_crs;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001076
Pali Rohár223dec12021-10-05 20:09:48 +02001077 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
Marek Behún67cb2a42021-10-05 20:09:47 +02001083 if (ret < 0)
1084 goto fail;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001085
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001086 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;
Marek Behún67cb2a42021-10-05 20:09:47 +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;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001106}
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;
Pali Rohár223dec12021-10-05 20:09:48 +02001114 int retry_count;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001115 int offset;
1116 int ret;
1117
Thomas Petazzoni248d4e52018-04-06 16:55:35 +02001118 if (!advk_pcie_valid_device(pcie, bus, devfn))
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001119 return PCIBIOS_DEVICE_NOT_FOUND;
1120
Rob Herring11e97972020-07-21 20:24:59 -06001121 if (pci_is_root_bus(bus))
Zachary Zhang8a3ebd82018-10-18 17:37:19 +02001122 return pci_bridge_emul_conf_write(&pcie->bridge, where,
1123 size, val);
1124
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001125 if (where % size)
1126 return PCIBIOS_SET_FAILED;
1127
Pali Rohárf1813992021-06-08 22:36:55 +02001128 if (advk_pcie_pio_is_running(pcie))
1129 return PCIBIOS_SET_FAILED;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001130
1131 /* Program the control register */
1132 reg = advk_readl(pcie, PIO_CTRL);
1133 reg &= ~PIO_CTRL_TYPE_MASK;
Rob Herring11e97972020-07-21 20:24:59 -06001134 if (pci_is_root_bus(bus->parent))
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001135 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 */
Krzysztof Wilczyńskie7708f52020-11-29 23:07:39 +00001141 reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001142 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
Pali Rohár223dec12021-10-05 20:09:48 +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);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001161
Pali Rohár223dec12021-10-05 20:09:48 +02001162 ret = advk_pcie_wait_pio(pcie);
1163 if (ret < 0)
1164 return PCIBIOS_SET_FAILED;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001165
Pali Rohár223dec12021-10-05 20:09:48 +02001166 retry_count += ret;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001167
Pali Rohár223dec12021-10-05 20:09:48 +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;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001172}
1173
1174static struct pci_ops advk_pcie_ops = {
1175 .read = advk_pcie_rd_conf,
1176 .write = advk_pcie_wr_conf,
1177};
1178
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001179static void advk_msi_irq_compose_msi_msg(struct irq_data *data,
1180 struct msi_msg *msg)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001181{
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001182 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;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001202
1203 mutex_lock(&pcie->msi_used_lock);
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001204 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);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001212 mutex_unlock(&pcie->msi_used_lock);
1213
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001214 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
Marek Behúne4313be2021-10-28 20:56:54 +02001220 return 0;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001221}
1222
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001223static void advk_msi_irq_domain_free(struct irq_domain *domain,
1224 unsigned int virq, unsigned int nr_irqs)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001225{
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001226 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1227 struct advk_pcie *pcie = domain->host_data;
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -05001228
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001229 mutex_lock(&pcie->msi_used_lock);
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001230 bitmap_clear(pcie->msi_used, d->hwirq, nr_irqs);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001231 mutex_unlock(&pcie->msi_used_lock);
1232}
1233
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001234static const struct irq_domain_ops advk_msi_domain_ops = {
1235 .alloc = advk_msi_irq_domain_alloc,
1236 .free = advk_msi_irq_domain_free,
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001237};
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);
Pali Rohárd212dce2021-08-20 17:50:20 +02001243 unsigned long flags;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001244 u32 mask;
1245
Pali Rohárd212dce2021-08-20 17:50:20 +02001246 raw_spin_lock_irqsave(&pcie->irq_lock, flags);
Victor Gu3430f922018-04-06 16:55:33 +02001247 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
1248 mask |= PCIE_ISR1_INTX_ASSERT(hwirq);
1249 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
Pali Rohárd212dce2021-08-20 17:50:20 +02001250 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001251}
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);
Pali Rohárd212dce2021-08-20 17:50:20 +02001257 unsigned long flags;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001258 u32 mask;
1259
Pali Rohárd212dce2021-08-20 17:50:20 +02001260 raw_spin_lock_irqsave(&pcie->irq_lock, flags);
Victor Gu3430f922018-04-06 16:55:33 +02001261 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
1262 mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq);
1263 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
Pali Rohárd212dce2021-08-20 17:50:20 +02001264 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001265}
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;
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001290 struct irq_chip *bottom_ic, *msi_ic;
1291 struct msi_domain_info *msi_di;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001292 phys_addr_t msi_msg_phys;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001293
1294 mutex_init(&pcie->msi_used_lock);
1295
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001296 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
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001310 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
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001317 pcie->msi_inner_domain =
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001318 irq_domain_add_linear(NULL, MSI_IRQ_NUM,
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001319 &advk_msi_domain_ops, pcie);
1320 if (!pcie->msi_inner_domain)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001321 return -ENOMEM;
1322
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001323 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;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001329 }
1330
1331 return 0;
1332}
1333
1334static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
1335{
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001336 irq_domain_remove(pcie->msi_domain);
Thomas Petazzonif21a8b12017-02-28 15:31:14 +01001337 irq_domain_remove(pcie->msi_inner_domain);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001338}
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;
Wen Yang3842f512019-02-27 12:40:40 +08001346 int ret = 0;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001347
Pali Rohárd212dce2021-08-20 17:50:20 +02001348 raw_spin_lock_init(&pcie->irq_lock);
1349
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001350 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) {
Wen Yang3842f512019-02-27 12:40:40 +08001361 ret = -ENOMEM;
1362 goto out_put_node;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001363 }
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 =
Paul Burton0d2977a2017-08-15 16:26:03 -05001370 irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001371 &advk_pcie_irq_domain_ops, pcie);
1372 if (!pcie->irq_domain) {
1373 dev_err(dev, "Failed to get a INTx IRQ domain\n");
Wen Yang3842f512019-02-27 12:40:40 +08001374 ret = -ENOMEM;
1375 goto out_put_node;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001376 }
1377
Wen Yang3842f512019-02-27 12:40:40 +08001378out_put_node:
1379 of_node_put(pcie_intc_node);
1380 return ret;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001381}
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);
Pali Rohár7d8dc1f2021-11-30 18:29:06 +01001395 msi_status = msi_val & ((~msi_mask) & PCIE_MSI_ALL_MASK);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001396
1397 for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
1398 if (!(BIT(msi_idx) & msi_status))
1399 continue;
1400
Marek Behún95997722021-10-28 20:56:55 +02001401 /*
1402 * msi_idx contains bits [4:0] of the msi_data and msi_data
1403 * contains 16bit MSI interrupt number
1404 */
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001405 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
Marek Behún95997722021-10-28 20:56:55 +02001406 msi_data = advk_readl(pcie, PCIE_MSI_PAYLOAD_REG) & PCIE_MSI_DATA_MASK;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001407 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{
Victor Gu3430f922018-04-06 16:55:33 +02001416 u32 isr0_val, isr0_mask, isr0_status;
1417 u32 isr1_val, isr1_mask, isr1_status;
Marc Zyngierd21faba12021-08-02 17:26:19 +01001418 int i;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001419
Victor Gu3430f922018-04-06 16:55:33 +02001420 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);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001423
Victor Gu3430f922018-04-06 16:55:33 +02001424 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
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001428 /* Process MSI interrupts */
Victor Gu3430f922018-04-06 16:55:33 +02001429 if (isr0_status & PCIE_ISR0_MSI_INT_PENDING)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001430 advk_pcie_handle_msi(pcie);
1431
1432 /* Process legacy interrupts */
Paul Burton0d2977a2017-08-15 16:26:03 -05001433 for (i = 0; i < PCI_NUM_INTX; i++) {
Victor Gu3430f922018-04-06 16:55:33 +02001434 if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i)))
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001435 continue;
1436
Victor Gu3430f922018-04-06 16:55:33 +02001437 advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i),
1438 PCIE_ISR1_REG);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001439
Marc Zyngierd21faba12021-08-02 17:26:19 +01001440 generic_handle_domain_irq(pcie->irq_domain, i);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001441 }
1442}
1443
1444static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
1445{
1446 struct advk_pcie *pcie = arg;
1447 u32 status;
1448
1449 status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
1450 if (!(status & PCIE_IRQ_CORE_INT))
1451 return IRQ_NONE;
1452
1453 advk_pcie_handle_int(pcie);
1454
1455 /* Clear interrupt */
1456 advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
1457
1458 return IRQ_HANDLED;
1459}
1460
Marek Behún36669702020-04-30 10:06:20 +02001461static void __maybe_unused advk_pcie_disable_phy(struct advk_pcie *pcie)
1462{
1463 phy_power_off(pcie->phy);
1464 phy_exit(pcie->phy);
1465}
1466
1467static int advk_pcie_enable_phy(struct advk_pcie *pcie)
1468{
1469 int ret;
1470
1471 if (!pcie->phy)
1472 return 0;
1473
1474 ret = phy_init(pcie->phy);
1475 if (ret)
1476 return ret;
1477
1478 ret = phy_set_mode(pcie->phy, PHY_MODE_PCIE);
1479 if (ret) {
1480 phy_exit(pcie->phy);
1481 return ret;
1482 }
1483
1484 ret = phy_power_on(pcie->phy);
Pali Rohárb0c6ae02020-09-02 16:43:44 +02001485 if (ret == -EOPNOTSUPP) {
1486 dev_warn(&pcie->pdev->dev, "PHY unsupported by firmware\n");
1487 } else if (ret) {
Marek Behún36669702020-04-30 10:06:20 +02001488 phy_exit(pcie->phy);
1489 return ret;
1490 }
1491
1492 return 0;
1493}
1494
1495static int advk_pcie_setup_phy(struct advk_pcie *pcie)
1496{
1497 struct device *dev = &pcie->pdev->dev;
1498 struct device_node *node = dev->of_node;
1499 int ret = 0;
1500
1501 pcie->phy = devm_of_phy_get(dev, node, NULL);
1502 if (IS_ERR(pcie->phy) && (PTR_ERR(pcie->phy) == -EPROBE_DEFER))
1503 return PTR_ERR(pcie->phy);
1504
1505 /* Old bindings miss the PHY handle */
1506 if (IS_ERR(pcie->phy)) {
1507 dev_warn(dev, "PHY unavailable (%ld)\n", PTR_ERR(pcie->phy));
1508 pcie->phy = NULL;
1509 return 0;
1510 }
1511
1512 ret = advk_pcie_enable_phy(pcie);
1513 if (ret)
1514 dev_err(dev, "Failed to initialize PHY (%d)\n", ret);
1515
1516 return ret;
1517}
1518
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001519static int advk_pcie_probe(struct platform_device *pdev)
1520{
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -05001521 struct device *dev = &pdev->dev;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001522 struct advk_pcie *pcie;
Lorenzo Pieralisi6b6de6a2017-06-28 15:13:56 -05001523 struct pci_host_bridge *bridge;
Pali Rohár64f160e2021-06-24 23:55:45 +02001524 struct resource_entry *entry;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001525 int ret, irq;
1526
Lorenzo Pieralisi6b6de6a2017-06-28 15:13:56 -05001527 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie));
1528 if (!bridge)
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001529 return -ENOMEM;
1530
Lorenzo Pieralisi6b6de6a2017-06-28 15:13:56 -05001531 pcie = pci_host_bridge_priv(bridge);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001532 pcie->pdev = pdev;
Pali Rohár526a7692020-09-07 13:10:37 +02001533 platform_set_drvdata(pdev, pcie);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001534
Pali Rohár64f160e2021-06-24 23:55:45 +02001535 resource_list_for_each_entry(entry, &bridge->windows) {
1536 resource_size_t start = entry->res->start;
1537 resource_size_t size = resource_size(entry->res);
1538 unsigned long type = resource_type(entry->res);
1539 u64 win_size;
1540
1541 /*
1542 * Aardvark hardware allows to configure also PCIe window
1543 * for config type 0 and type 1 mapping, but driver uses
1544 * only PIO for issuing configuration transfers which does
1545 * not use PCIe window configuration.
1546 */
Pali Rohár2070b2d2021-11-25 17:01:47 +01001547 if (type != IORESOURCE_MEM && type != IORESOURCE_IO)
Pali Rohár64f160e2021-06-24 23:55:45 +02001548 continue;
1549
1550 /*
1551 * Skip transparent memory resources. Default outbound access
1552 * configuration is set to transparent memory access so it
1553 * does not need window configuration.
1554 */
Pali Rohár2070b2d2021-11-25 17:01:47 +01001555 if (type == IORESOURCE_MEM && entry->offset == 0)
Pali Rohár64f160e2021-06-24 23:55:45 +02001556 continue;
1557
1558 /*
1559 * The n-th PCIe window is configured by tuple (match, remap, mask)
1560 * and an access to address A uses this window if A matches the
1561 * match with given mask.
1562 * So every PCIe window size must be a power of two and every start
1563 * address must be aligned to window size. Minimal size is 64 KiB
1564 * because lower 16 bits of mask must be zero. Remapped address
1565 * may have set only bits from the mask.
1566 */
1567 while (pcie->wins_count < OB_WIN_COUNT && size > 0) {
1568 /* Calculate the largest aligned window size */
1569 win_size = (1ULL << (fls64(size)-1)) |
1570 (start ? (1ULL << __ffs64(start)) : 0);
1571 win_size = 1ULL << __ffs64(win_size);
1572 if (win_size < 0x10000)
1573 break;
1574
1575 dev_dbg(dev,
1576 "Configuring PCIe window %d: [0x%llx-0x%llx] as %lu\n",
1577 pcie->wins_count, (unsigned long long)start,
1578 (unsigned long long)start + win_size, type);
1579
1580 if (type == IORESOURCE_IO) {
1581 pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_IO;
1582 pcie->wins[pcie->wins_count].match = pci_pio_to_address(start);
1583 } else {
1584 pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_MEM;
1585 pcie->wins[pcie->wins_count].match = start;
1586 }
1587 pcie->wins[pcie->wins_count].remap = start - entry->offset;
1588 pcie->wins[pcie->wins_count].mask = ~(win_size - 1);
1589
1590 if (pcie->wins[pcie->wins_count].remap & (win_size - 1))
1591 break;
1592
1593 start += win_size;
1594 size -= win_size;
1595 pcie->wins_count++;
1596 }
1597
1598 if (size > 0) {
1599 dev_err(&pcie->pdev->dev,
1600 "Invalid PCIe region [0x%llx-0x%llx]\n",
1601 (unsigned long long)entry->res->start,
1602 (unsigned long long)entry->res->end + 1);
1603 return -EINVAL;
1604 }
1605 }
1606
Dejin Zheng3cf0eea2020-07-08 23:56:14 +08001607 pcie->base = devm_platform_ioremap_resource(pdev, 0);
Wei Yongjun8b223352016-07-28 16:17:14 +00001608 if (IS_ERR(pcie->base))
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001609 return PTR_ERR(pcie->base);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001610
1611 irq = platform_get_irq(pdev, 0);
Aman Sharma0584bff2020-03-12 00:49:02 +05301612 if (irq < 0)
1613 return irq;
1614
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -05001615 ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001616 IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
1617 pcie);
1618 if (ret) {
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -05001619 dev_err(dev, "Failed to register interrupt\n");
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001620 return ret;
1621 }
1622
Pali Rohár5169a982020-04-30 10:06:18 +02001623 pcie->reset_gpio = devm_gpiod_get_from_of_node(dev, dev->of_node,
1624 "reset-gpios", 0,
1625 GPIOD_OUT_LOW,
1626 "pcie1-reset");
1627 ret = PTR_ERR_OR_ZERO(pcie->reset_gpio);
1628 if (ret) {
1629 if (ret == -ENOENT) {
1630 pcie->reset_gpio = NULL;
1631 } else {
1632 if (ret != -EPROBE_DEFER)
1633 dev_err(dev, "Failed to get reset-gpio: %i\n",
1634 ret);
1635 return ret;
1636 }
1637 }
1638
Marek Behún43fc6792020-04-30 10:06:17 +02001639 ret = of_pci_get_max_link_speed(dev->of_node);
1640 if (ret <= 0 || ret > 3)
1641 pcie->link_gen = 3;
1642 else
1643 pcie->link_gen = ret;
1644
Marek Behún36669702020-04-30 10:06:20 +02001645 ret = advk_pcie_setup_phy(pcie);
1646 if (ret)
1647 return ret;
1648
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001649 advk_pcie_setup_hw(pcie);
1650
Pali Rohár7862a612020-09-07 13:10:35 +02001651 ret = advk_sw_pci_bridge_init(pcie);
1652 if (ret) {
1653 dev_err(dev, "Failed to register emulated root PCI bridge\n");
1654 return ret;
1655 }
Zachary Zhang8a3ebd82018-10-18 17:37:19 +02001656
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001657 ret = advk_pcie_init_irq_domain(pcie);
1658 if (ret) {
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -05001659 dev_err(dev, "Failed to initialize irq\n");
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001660 return ret;
1661 }
1662
1663 ret = advk_pcie_init_msi_irq_domain(pcie);
1664 if (ret) {
Bjorn Helgaas9aec2fe2016-10-06 13:27:46 -05001665 dev_err(dev, "Failed to initialize irq\n");
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001666 advk_pcie_remove_irq_domain(pcie);
1667 return ret;
1668 }
1669
Lorenzo Pieralisi6b6de6a2017-06-28 15:13:56 -05001670 bridge->sysdata = pcie;
Lorenzo Pieralisi6b6de6a2017-06-28 15:13:56 -05001671 bridge->ops = &advk_pcie_ops;
1672
Thomas Petazzonic8e144f2018-06-29 11:16:20 +02001673 ret = pci_host_probe(bridge);
Lorenzo Pieralisi6b6de6a2017-06-28 15:13:56 -05001674 if (ret < 0) {
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001675 advk_pcie_remove_msi_irq_domain(pcie);
1676 advk_pcie_remove_irq_domain(pcie);
Lorenzo Pieralisi6b6de6a2017-06-28 15:13:56 -05001677 return ret;
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001678 }
1679
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001680 return 0;
1681}
1682
Pali Rohár526a7692020-09-07 13:10:37 +02001683static int advk_pcie_remove(struct platform_device *pdev)
1684{
1685 struct advk_pcie *pcie = platform_get_drvdata(pdev);
1686 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
Pali Rohára46f2f62021-11-30 18:29:08 +01001687 u32 val;
Pali Rohár64f160e2021-06-24 23:55:45 +02001688 int i;
Pali Rohár526a7692020-09-07 13:10:37 +02001689
Pali Rohára4ca7942021-11-30 18:29:07 +01001690 /* Remove PCI bus with all devices */
Pali Rohár526a7692020-09-07 13:10:37 +02001691 pci_lock_rescan_remove();
1692 pci_stop_root_bus(bridge->bus);
1693 pci_remove_root_bus(bridge->bus);
1694 pci_unlock_rescan_remove();
1695
Pali Rohára46f2f62021-11-30 18:29:08 +01001696 /* Disable Root Bridge I/O space, memory space and bus mastering */
1697 val = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
1698 val &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
1699 advk_writel(pcie, val, PCIE_CORE_CMD_STATUS_REG);
1700
Pali Rohár13bcdf02021-11-30 18:29:09 +01001701 /* Disable MSI */
1702 val = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
1703 val &= ~PCIE_CORE_CTRL2_MSI_ENABLE;
1704 advk_writel(pcie, val, PCIE_CORE_CTRL2_REG);
1705
1706 /* Clear MSI address */
1707 advk_writel(pcie, 0, PCIE_MSI_ADDR_LOW_REG);
1708 advk_writel(pcie, 0, PCIE_MSI_ADDR_HIGH_REG);
1709
1710 /* Mask all interrupts */
1711 advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG);
1712 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_MASK_REG);
1713 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
1714 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_MASK_REG);
1715
1716 /* Clear all interrupts */
1717 advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG);
1718 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
1719 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
1720 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
1721
Pali Rohára4ca7942021-11-30 18:29:07 +01001722 /* Remove IRQ domains */
Pali Rohár526a7692020-09-07 13:10:37 +02001723 advk_pcie_remove_msi_irq_domain(pcie);
1724 advk_pcie_remove_irq_domain(pcie);
1725
Pali Rohár2f040a12021-11-30 18:29:10 +01001726 /* Free config space for emulated root bridge */
1727 pci_bridge_emul_cleanup(&pcie->bridge);
1728
Pali Rohár1f543912021-11-30 18:29:11 +01001729 /* Assert PERST# signal which prepares PCIe card for power down */
1730 if (pcie->reset_gpio)
1731 gpiod_set_value_cansleep(pcie->reset_gpio, 1);
1732
Pali Rohár759dec2e2021-11-30 18:29:12 +01001733 /* Disable link training */
1734 val = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
1735 val &= ~LINK_TRAINING_EN;
1736 advk_writel(pcie, val, PCIE_CORE_CTRL0_REG);
1737
Pali Rohár64f160e2021-06-24 23:55:45 +02001738 /* Disable outbound address windows mapping */
1739 for (i = 0; i < OB_WIN_COUNT; i++)
1740 advk_pcie_disable_ob_win(pcie, i);
1741
Pali Rohárfdbbe242021-11-30 18:29:13 +01001742 /* Disable phy */
1743 advk_pcie_disable_phy(pcie);
1744
Pali Rohár526a7692020-09-07 13:10:37 +02001745 return 0;
1746}
1747
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001748static const struct of_device_id advk_pcie_of_match_table[] = {
1749 { .compatible = "marvell,armada-3700-pcie", },
1750 {},
1751};
Pali Rohár526a7692020-09-07 13:10:37 +02001752MODULE_DEVICE_TABLE(of, advk_pcie_of_match_table);
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001753
1754static struct platform_driver advk_pcie_driver = {
1755 .driver = {
1756 .name = "advk-pcie",
1757 .of_match_table = advk_pcie_of_match_table,
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001758 },
1759 .probe = advk_pcie_probe,
Pali Rohár526a7692020-09-07 13:10:37 +02001760 .remove = advk_pcie_remove,
Thomas Petazzoni8c39d712016-06-30 11:32:31 +02001761};
Pali Rohár526a7692020-09-07 13:10:37 +02001762module_platform_driver(advk_pcie_driver);
1763
1764MODULE_DESCRIPTION("Aardvark PCIe controller");
1765MODULE_LICENSE("GPL v2");