18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Driver for the Aardvark PCIe controller, used on Marvell Armada 48c2ecf20Sopenharmony_ci * 3700. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright (C) 2016 Marvell 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Author: Hezi Shahmoon <hezi.shahmoon@marvell.com> 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/delay.h> 128c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 138c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 148c2ecf20Sopenharmony_ci#include <linux/irq.h> 158c2ecf20Sopenharmony_ci#include <linux/irqdomain.h> 168c2ecf20Sopenharmony_ci#include <linux/kernel.h> 178c2ecf20Sopenharmony_ci#include <linux/module.h> 188c2ecf20Sopenharmony_ci#include <linux/pci.h> 198c2ecf20Sopenharmony_ci#include <linux/init.h> 208c2ecf20Sopenharmony_ci#include <linux/phy/phy.h> 218c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 228c2ecf20Sopenharmony_ci#include <linux/msi.h> 238c2ecf20Sopenharmony_ci#include <linux/of_address.h> 248c2ecf20Sopenharmony_ci#include <linux/of_gpio.h> 258c2ecf20Sopenharmony_ci#include <linux/of_pci.h> 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#include "../pci.h" 288c2ecf20Sopenharmony_ci#include "../pci-bridge-emul.h" 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci/* PCIe core registers */ 318c2ecf20Sopenharmony_ci#define PCIE_CORE_DEV_ID_REG 0x0 328c2ecf20Sopenharmony_ci#define PCIE_CORE_CMD_STATUS_REG 0x4 338c2ecf20Sopenharmony_ci#define PCIE_CORE_DEV_REV_REG 0x8 348c2ecf20Sopenharmony_ci#define PCIE_CORE_PCIEXP_CAP 0xc0 358c2ecf20Sopenharmony_ci#define PCIE_CORE_ERR_CAPCTL_REG 0x118 368c2ecf20Sopenharmony_ci#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5) 378c2ecf20Sopenharmony_ci#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6) 388c2ecf20Sopenharmony_ci#define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7) 398c2ecf20Sopenharmony_ci#define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8) 408c2ecf20Sopenharmony_ci#define PCIE_CORE_INT_A_ASSERT_ENABLE 1 418c2ecf20Sopenharmony_ci#define PCIE_CORE_INT_B_ASSERT_ENABLE 2 428c2ecf20Sopenharmony_ci#define PCIE_CORE_INT_C_ASSERT_ENABLE 3 438c2ecf20Sopenharmony_ci#define PCIE_CORE_INT_D_ASSERT_ENABLE 4 448c2ecf20Sopenharmony_ci/* PIO registers base address and register offsets */ 458c2ecf20Sopenharmony_ci#define PIO_BASE_ADDR 0x4000 468c2ecf20Sopenharmony_ci#define PIO_CTRL (PIO_BASE_ADDR + 0x0) 478c2ecf20Sopenharmony_ci#define PIO_CTRL_TYPE_MASK GENMASK(3, 0) 488c2ecf20Sopenharmony_ci#define PIO_CTRL_ADDR_WIN_DISABLE BIT(24) 498c2ecf20Sopenharmony_ci#define PIO_STAT (PIO_BASE_ADDR + 0x4) 508c2ecf20Sopenharmony_ci#define PIO_COMPLETION_STATUS_SHIFT 7 518c2ecf20Sopenharmony_ci#define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7) 528c2ecf20Sopenharmony_ci#define PIO_COMPLETION_STATUS_OK 0 538c2ecf20Sopenharmony_ci#define PIO_COMPLETION_STATUS_UR 1 548c2ecf20Sopenharmony_ci#define PIO_COMPLETION_STATUS_CRS 2 558c2ecf20Sopenharmony_ci#define PIO_COMPLETION_STATUS_CA 4 568c2ecf20Sopenharmony_ci#define PIO_NON_POSTED_REQ BIT(10) 578c2ecf20Sopenharmony_ci#define PIO_ERR_STATUS BIT(11) 588c2ecf20Sopenharmony_ci#define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8) 598c2ecf20Sopenharmony_ci#define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc) 608c2ecf20Sopenharmony_ci#define PIO_WR_DATA (PIO_BASE_ADDR + 0x10) 618c2ecf20Sopenharmony_ci#define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14) 628c2ecf20Sopenharmony_ci#define PIO_RD_DATA (PIO_BASE_ADDR + 0x18) 638c2ecf20Sopenharmony_ci#define PIO_START (PIO_BASE_ADDR + 0x1c) 648c2ecf20Sopenharmony_ci#define PIO_ISR (PIO_BASE_ADDR + 0x20) 658c2ecf20Sopenharmony_ci#define PIO_ISRM (PIO_BASE_ADDR + 0x24) 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci/* Aardvark Control registers */ 688c2ecf20Sopenharmony_ci#define CONTROL_BASE_ADDR 0x4800 698c2ecf20Sopenharmony_ci#define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0) 708c2ecf20Sopenharmony_ci#define PCIE_GEN_SEL_MSK 0x3 718c2ecf20Sopenharmony_ci#define PCIE_GEN_SEL_SHIFT 0x0 728c2ecf20Sopenharmony_ci#define SPEED_GEN_1 0 738c2ecf20Sopenharmony_ci#define SPEED_GEN_2 1 748c2ecf20Sopenharmony_ci#define SPEED_GEN_3 2 758c2ecf20Sopenharmony_ci#define IS_RC_MSK 1 768c2ecf20Sopenharmony_ci#define IS_RC_SHIFT 2 778c2ecf20Sopenharmony_ci#define LANE_CNT_MSK 0x18 788c2ecf20Sopenharmony_ci#define LANE_CNT_SHIFT 0x3 798c2ecf20Sopenharmony_ci#define LANE_COUNT_1 (0 << LANE_CNT_SHIFT) 808c2ecf20Sopenharmony_ci#define LANE_COUNT_2 (1 << LANE_CNT_SHIFT) 818c2ecf20Sopenharmony_ci#define LANE_COUNT_4 (2 << LANE_CNT_SHIFT) 828c2ecf20Sopenharmony_ci#define LANE_COUNT_8 (3 << LANE_CNT_SHIFT) 838c2ecf20Sopenharmony_ci#define LINK_TRAINING_EN BIT(6) 848c2ecf20Sopenharmony_ci#define LEGACY_INTA BIT(28) 858c2ecf20Sopenharmony_ci#define LEGACY_INTB BIT(29) 868c2ecf20Sopenharmony_ci#define LEGACY_INTC BIT(30) 878c2ecf20Sopenharmony_ci#define LEGACY_INTD BIT(31) 888c2ecf20Sopenharmony_ci#define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4) 898c2ecf20Sopenharmony_ci#define HOT_RESET_GEN BIT(0) 908c2ecf20Sopenharmony_ci#define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8) 918c2ecf20Sopenharmony_ci#define PCIE_CORE_CTRL2_RESERVED 0x7 928c2ecf20Sopenharmony_ci#define PCIE_CORE_CTRL2_TD_ENABLE BIT(4) 938c2ecf20Sopenharmony_ci#define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5) 948c2ecf20Sopenharmony_ci#define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6) 958c2ecf20Sopenharmony_ci#define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10) 968c2ecf20Sopenharmony_ci#define PCIE_CORE_REF_CLK_REG (CONTROL_BASE_ADDR + 0x14) 978c2ecf20Sopenharmony_ci#define PCIE_CORE_REF_CLK_TX_ENABLE BIT(1) 988c2ecf20Sopenharmony_ci#define PCIE_CORE_REF_CLK_RX_ENABLE BIT(2) 998c2ecf20Sopenharmony_ci#define PCIE_MSG_LOG_REG (CONTROL_BASE_ADDR + 0x30) 1008c2ecf20Sopenharmony_ci#define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40) 1018c2ecf20Sopenharmony_ci#define PCIE_MSG_PM_PME_MASK BIT(7) 1028c2ecf20Sopenharmony_ci#define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44) 1038c2ecf20Sopenharmony_ci#define PCIE_ISR0_MSI_INT_PENDING BIT(24) 1048c2ecf20Sopenharmony_ci#define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val)) 1058c2ecf20Sopenharmony_ci#define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val)) 1068c2ecf20Sopenharmony_ci#define PCIE_ISR0_ALL_MASK GENMASK(31, 0) 1078c2ecf20Sopenharmony_ci#define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48) 1088c2ecf20Sopenharmony_ci#define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C) 1098c2ecf20Sopenharmony_ci#define PCIE_ISR1_POWER_STATE_CHANGE BIT(4) 1108c2ecf20Sopenharmony_ci#define PCIE_ISR1_FLUSH BIT(5) 1118c2ecf20Sopenharmony_ci#define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val)) 1128c2ecf20Sopenharmony_ci#define PCIE_ISR1_ALL_MASK GENMASK(31, 0) 1138c2ecf20Sopenharmony_ci#define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50) 1148c2ecf20Sopenharmony_ci#define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54) 1158c2ecf20Sopenharmony_ci#define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58) 1168c2ecf20Sopenharmony_ci#define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C) 1178c2ecf20Sopenharmony_ci#define PCIE_MSI_ALL_MASK GENMASK(31, 0) 1188c2ecf20Sopenharmony_ci#define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C) 1198c2ecf20Sopenharmony_ci#define PCIE_MSI_DATA_MASK GENMASK(15, 0) 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci/* PCIe window configuration */ 1228c2ecf20Sopenharmony_ci#define OB_WIN_BASE_ADDR 0x4c00 1238c2ecf20Sopenharmony_ci#define OB_WIN_BLOCK_SIZE 0x20 1248c2ecf20Sopenharmony_ci#define OB_WIN_COUNT 8 1258c2ecf20Sopenharmony_ci#define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \ 1268c2ecf20Sopenharmony_ci OB_WIN_BLOCK_SIZE * (win) + \ 1278c2ecf20Sopenharmony_ci (offset)) 1288c2ecf20Sopenharmony_ci#define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00) 1298c2ecf20Sopenharmony_ci#define OB_WIN_ENABLE BIT(0) 1308c2ecf20Sopenharmony_ci#define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04) 1318c2ecf20Sopenharmony_ci#define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08) 1328c2ecf20Sopenharmony_ci#define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c) 1338c2ecf20Sopenharmony_ci#define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10) 1348c2ecf20Sopenharmony_ci#define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14) 1358c2ecf20Sopenharmony_ci#define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18) 1368c2ecf20Sopenharmony_ci#define OB_WIN_DEFAULT_ACTIONS (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4) 1378c2ecf20Sopenharmony_ci#define OB_WIN_FUNC_NUM_MASK GENMASK(31, 24) 1388c2ecf20Sopenharmony_ci#define OB_WIN_FUNC_NUM_SHIFT 24 1398c2ecf20Sopenharmony_ci#define OB_WIN_FUNC_NUM_ENABLE BIT(23) 1408c2ecf20Sopenharmony_ci#define OB_WIN_BUS_NUM_BITS_MASK GENMASK(22, 20) 1418c2ecf20Sopenharmony_ci#define OB_WIN_BUS_NUM_BITS_SHIFT 20 1428c2ecf20Sopenharmony_ci#define OB_WIN_MSG_CODE_ENABLE BIT(22) 1438c2ecf20Sopenharmony_ci#define OB_WIN_MSG_CODE_MASK GENMASK(21, 14) 1448c2ecf20Sopenharmony_ci#define OB_WIN_MSG_CODE_SHIFT 14 1458c2ecf20Sopenharmony_ci#define OB_WIN_MSG_PAYLOAD_LEN BIT(12) 1468c2ecf20Sopenharmony_ci#define OB_WIN_ATTR_ENABLE BIT(11) 1478c2ecf20Sopenharmony_ci#define OB_WIN_ATTR_TC_MASK GENMASK(10, 8) 1488c2ecf20Sopenharmony_ci#define OB_WIN_ATTR_TC_SHIFT 8 1498c2ecf20Sopenharmony_ci#define OB_WIN_ATTR_RELAXED BIT(7) 1508c2ecf20Sopenharmony_ci#define OB_WIN_ATTR_NOSNOOP BIT(6) 1518c2ecf20Sopenharmony_ci#define OB_WIN_ATTR_POISON BIT(5) 1528c2ecf20Sopenharmony_ci#define OB_WIN_ATTR_IDO BIT(4) 1538c2ecf20Sopenharmony_ci#define OB_WIN_TYPE_MASK GENMASK(3, 0) 1548c2ecf20Sopenharmony_ci#define OB_WIN_TYPE_SHIFT 0 1558c2ecf20Sopenharmony_ci#define OB_WIN_TYPE_MEM 0x0 1568c2ecf20Sopenharmony_ci#define OB_WIN_TYPE_IO 0x4 1578c2ecf20Sopenharmony_ci#define OB_WIN_TYPE_CONFIG_TYPE0 0x8 1588c2ecf20Sopenharmony_ci#define OB_WIN_TYPE_CONFIG_TYPE1 0x9 1598c2ecf20Sopenharmony_ci#define OB_WIN_TYPE_MSG 0xc 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci/* LMI registers base address and register offsets */ 1628c2ecf20Sopenharmony_ci#define LMI_BASE_ADDR 0x6000 1638c2ecf20Sopenharmony_ci#define CFG_REG (LMI_BASE_ADDR + 0x0) 1648c2ecf20Sopenharmony_ci#define LTSSM_SHIFT 24 1658c2ecf20Sopenharmony_ci#define LTSSM_MASK 0x3f 1668c2ecf20Sopenharmony_ci#define RC_BAR_CONFIG 0x300 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci/* LTSSM values in CFG_REG */ 1698c2ecf20Sopenharmony_cienum { 1708c2ecf20Sopenharmony_ci LTSSM_DETECT_QUIET = 0x0, 1718c2ecf20Sopenharmony_ci LTSSM_DETECT_ACTIVE = 0x1, 1728c2ecf20Sopenharmony_ci LTSSM_POLLING_ACTIVE = 0x2, 1738c2ecf20Sopenharmony_ci LTSSM_POLLING_COMPLIANCE = 0x3, 1748c2ecf20Sopenharmony_ci LTSSM_POLLING_CONFIGURATION = 0x4, 1758c2ecf20Sopenharmony_ci LTSSM_CONFIG_LINKWIDTH_START = 0x5, 1768c2ecf20Sopenharmony_ci LTSSM_CONFIG_LINKWIDTH_ACCEPT = 0x6, 1778c2ecf20Sopenharmony_ci LTSSM_CONFIG_LANENUM_ACCEPT = 0x7, 1788c2ecf20Sopenharmony_ci LTSSM_CONFIG_LANENUM_WAIT = 0x8, 1798c2ecf20Sopenharmony_ci LTSSM_CONFIG_COMPLETE = 0x9, 1808c2ecf20Sopenharmony_ci LTSSM_CONFIG_IDLE = 0xa, 1818c2ecf20Sopenharmony_ci LTSSM_RECOVERY_RCVR_LOCK = 0xb, 1828c2ecf20Sopenharmony_ci LTSSM_RECOVERY_SPEED = 0xc, 1838c2ecf20Sopenharmony_ci LTSSM_RECOVERY_RCVR_CFG = 0xd, 1848c2ecf20Sopenharmony_ci LTSSM_RECOVERY_IDLE = 0xe, 1858c2ecf20Sopenharmony_ci LTSSM_L0 = 0x10, 1868c2ecf20Sopenharmony_ci LTSSM_RX_L0S_ENTRY = 0x11, 1878c2ecf20Sopenharmony_ci LTSSM_RX_L0S_IDLE = 0x12, 1888c2ecf20Sopenharmony_ci LTSSM_RX_L0S_FTS = 0x13, 1898c2ecf20Sopenharmony_ci LTSSM_TX_L0S_ENTRY = 0x14, 1908c2ecf20Sopenharmony_ci LTSSM_TX_L0S_IDLE = 0x15, 1918c2ecf20Sopenharmony_ci LTSSM_TX_L0S_FTS = 0x16, 1928c2ecf20Sopenharmony_ci LTSSM_L1_ENTRY = 0x17, 1938c2ecf20Sopenharmony_ci LTSSM_L1_IDLE = 0x18, 1948c2ecf20Sopenharmony_ci LTSSM_L2_IDLE = 0x19, 1958c2ecf20Sopenharmony_ci LTSSM_L2_TRANSMIT_WAKE = 0x1a, 1968c2ecf20Sopenharmony_ci LTSSM_DISABLED = 0x20, 1978c2ecf20Sopenharmony_ci LTSSM_LOOPBACK_ENTRY_MASTER = 0x21, 1988c2ecf20Sopenharmony_ci LTSSM_LOOPBACK_ACTIVE_MASTER = 0x22, 1998c2ecf20Sopenharmony_ci LTSSM_LOOPBACK_EXIT_MASTER = 0x23, 2008c2ecf20Sopenharmony_ci LTSSM_LOOPBACK_ENTRY_SLAVE = 0x24, 2018c2ecf20Sopenharmony_ci LTSSM_LOOPBACK_ACTIVE_SLAVE = 0x25, 2028c2ecf20Sopenharmony_ci LTSSM_LOOPBACK_EXIT_SLAVE = 0x26, 2038c2ecf20Sopenharmony_ci LTSSM_HOT_RESET = 0x27, 2048c2ecf20Sopenharmony_ci LTSSM_RECOVERY_EQUALIZATION_PHASE0 = 0x28, 2058c2ecf20Sopenharmony_ci LTSSM_RECOVERY_EQUALIZATION_PHASE1 = 0x29, 2068c2ecf20Sopenharmony_ci LTSSM_RECOVERY_EQUALIZATION_PHASE2 = 0x2a, 2078c2ecf20Sopenharmony_ci LTSSM_RECOVERY_EQUALIZATION_PHASE3 = 0x2b, 2088c2ecf20Sopenharmony_ci}; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci#define VENDOR_ID_REG (LMI_BASE_ADDR + 0x44) 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci/* PCIe core controller registers */ 2138c2ecf20Sopenharmony_ci#define CTRL_CORE_BASE_ADDR 0x18000 2148c2ecf20Sopenharmony_ci#define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0) 2158c2ecf20Sopenharmony_ci#define CTRL_MODE_SHIFT 0x0 2168c2ecf20Sopenharmony_ci#define CTRL_MODE_MASK 0x1 2178c2ecf20Sopenharmony_ci#define PCIE_CORE_MODE_DIRECT 0x0 2188c2ecf20Sopenharmony_ci#define PCIE_CORE_MODE_COMMAND 0x1 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_ci/* PCIe Central Interrupts Registers */ 2218c2ecf20Sopenharmony_ci#define CENTRAL_INT_BASE_ADDR 0x1b000 2228c2ecf20Sopenharmony_ci#define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0) 2238c2ecf20Sopenharmony_ci#define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4) 2248c2ecf20Sopenharmony_ci#define PCIE_IRQ_CMDQ_INT BIT(0) 2258c2ecf20Sopenharmony_ci#define PCIE_IRQ_MSI_STATUS_INT BIT(1) 2268c2ecf20Sopenharmony_ci#define PCIE_IRQ_CMD_SENT_DONE BIT(3) 2278c2ecf20Sopenharmony_ci#define PCIE_IRQ_DMA_INT BIT(4) 2288c2ecf20Sopenharmony_ci#define PCIE_IRQ_IB_DXFERDONE BIT(5) 2298c2ecf20Sopenharmony_ci#define PCIE_IRQ_OB_DXFERDONE BIT(6) 2308c2ecf20Sopenharmony_ci#define PCIE_IRQ_OB_RXFERDONE BIT(7) 2318c2ecf20Sopenharmony_ci#define PCIE_IRQ_COMPQ_INT BIT(12) 2328c2ecf20Sopenharmony_ci#define PCIE_IRQ_DIR_RD_DDR_DET BIT(13) 2338c2ecf20Sopenharmony_ci#define PCIE_IRQ_DIR_WR_DDR_DET BIT(14) 2348c2ecf20Sopenharmony_ci#define PCIE_IRQ_CORE_INT BIT(16) 2358c2ecf20Sopenharmony_ci#define PCIE_IRQ_CORE_INT_PIO BIT(17) 2368c2ecf20Sopenharmony_ci#define PCIE_IRQ_DPMU_INT BIT(18) 2378c2ecf20Sopenharmony_ci#define PCIE_IRQ_PCIE_MIS_INT BIT(19) 2388c2ecf20Sopenharmony_ci#define PCIE_IRQ_MSI_INT1_DET BIT(20) 2398c2ecf20Sopenharmony_ci#define PCIE_IRQ_MSI_INT2_DET BIT(21) 2408c2ecf20Sopenharmony_ci#define PCIE_IRQ_RC_DBELL_DET BIT(22) 2418c2ecf20Sopenharmony_ci#define PCIE_IRQ_EP_STATUS BIT(23) 2428c2ecf20Sopenharmony_ci#define PCIE_IRQ_ALL_MASK GENMASK(31, 0) 2438c2ecf20Sopenharmony_ci#define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci/* Transaction types */ 2468c2ecf20Sopenharmony_ci#define PCIE_CONFIG_RD_TYPE0 0x8 2478c2ecf20Sopenharmony_ci#define PCIE_CONFIG_RD_TYPE1 0x9 2488c2ecf20Sopenharmony_ci#define PCIE_CONFIG_WR_TYPE0 0xa 2498c2ecf20Sopenharmony_ci#define PCIE_CONFIG_WR_TYPE1 0xb 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci#define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20) 2528c2ecf20Sopenharmony_ci#define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15) 2538c2ecf20Sopenharmony_ci#define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12) 2548c2ecf20Sopenharmony_ci#define PCIE_CONF_REG(reg) ((reg) & 0xffc) 2558c2ecf20Sopenharmony_ci#define PCIE_CONF_ADDR(bus, devfn, where) \ 2568c2ecf20Sopenharmony_ci (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \ 2578c2ecf20Sopenharmony_ci PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where)) 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci#define PIO_RETRY_CNT 750000 /* 1.5 s */ 2608c2ecf20Sopenharmony_ci#define PIO_RETRY_DELAY 2 /* 2 us*/ 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci#define LINK_WAIT_MAX_RETRIES 10 2638c2ecf20Sopenharmony_ci#define LINK_WAIT_USLEEP_MIN 90000 2648c2ecf20Sopenharmony_ci#define LINK_WAIT_USLEEP_MAX 100000 2658c2ecf20Sopenharmony_ci#define RETRAIN_WAIT_MAX_RETRIES 10 2668c2ecf20Sopenharmony_ci#define RETRAIN_WAIT_USLEEP_US 2000 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci#define MSI_IRQ_NUM 32 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci#define CFG_RD_CRS_VAL 0xffff0001 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_cistruct advk_pcie { 2738c2ecf20Sopenharmony_ci struct platform_device *pdev; 2748c2ecf20Sopenharmony_ci void __iomem *base; 2758c2ecf20Sopenharmony_ci struct { 2768c2ecf20Sopenharmony_ci phys_addr_t match; 2778c2ecf20Sopenharmony_ci phys_addr_t remap; 2788c2ecf20Sopenharmony_ci phys_addr_t mask; 2798c2ecf20Sopenharmony_ci u32 actions; 2808c2ecf20Sopenharmony_ci } wins[OB_WIN_COUNT]; 2818c2ecf20Sopenharmony_ci u8 wins_count; 2828c2ecf20Sopenharmony_ci struct irq_domain *irq_domain; 2838c2ecf20Sopenharmony_ci struct irq_chip irq_chip; 2848c2ecf20Sopenharmony_ci raw_spinlock_t irq_lock; 2858c2ecf20Sopenharmony_ci struct irq_domain *msi_domain; 2868c2ecf20Sopenharmony_ci struct irq_domain *msi_inner_domain; 2878c2ecf20Sopenharmony_ci struct irq_chip msi_bottom_irq_chip; 2888c2ecf20Sopenharmony_ci struct irq_chip msi_irq_chip; 2898c2ecf20Sopenharmony_ci struct msi_domain_info msi_domain_info; 2908c2ecf20Sopenharmony_ci DECLARE_BITMAP(msi_used, MSI_IRQ_NUM); 2918c2ecf20Sopenharmony_ci struct mutex msi_used_lock; 2928c2ecf20Sopenharmony_ci u16 msi_msg; 2938c2ecf20Sopenharmony_ci int link_gen; 2948c2ecf20Sopenharmony_ci struct pci_bridge_emul bridge; 2958c2ecf20Sopenharmony_ci struct gpio_desc *reset_gpio; 2968c2ecf20Sopenharmony_ci struct phy *phy; 2978c2ecf20Sopenharmony_ci}; 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_cistatic inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg) 3008c2ecf20Sopenharmony_ci{ 3018c2ecf20Sopenharmony_ci writel(val, pcie->base + reg); 3028c2ecf20Sopenharmony_ci} 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_cistatic inline u32 advk_readl(struct advk_pcie *pcie, u64 reg) 3058c2ecf20Sopenharmony_ci{ 3068c2ecf20Sopenharmony_ci return readl(pcie->base + reg); 3078c2ecf20Sopenharmony_ci} 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_cistatic u8 advk_pcie_ltssm_state(struct advk_pcie *pcie) 3108c2ecf20Sopenharmony_ci{ 3118c2ecf20Sopenharmony_ci u32 val; 3128c2ecf20Sopenharmony_ci u8 ltssm_state; 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci val = advk_readl(pcie, CFG_REG); 3158c2ecf20Sopenharmony_ci ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK; 3168c2ecf20Sopenharmony_ci return ltssm_state; 3178c2ecf20Sopenharmony_ci} 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_cistatic inline bool advk_pcie_link_up(struct advk_pcie *pcie) 3208c2ecf20Sopenharmony_ci{ 3218c2ecf20Sopenharmony_ci /* check if LTSSM is in normal operation - some L* state */ 3228c2ecf20Sopenharmony_ci u8 ltssm_state = advk_pcie_ltssm_state(pcie); 3238c2ecf20Sopenharmony_ci return ltssm_state >= LTSSM_L0 && ltssm_state < LTSSM_DISABLED; 3248c2ecf20Sopenharmony_ci} 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_cistatic inline bool advk_pcie_link_active(struct advk_pcie *pcie) 3278c2ecf20Sopenharmony_ci{ 3288c2ecf20Sopenharmony_ci /* 3298c2ecf20Sopenharmony_ci * According to PCIe Base specification 3.0, Table 4-14: Link 3308c2ecf20Sopenharmony_ci * Status Mapped to the LTSSM, and 4.2.6.3.6 Configuration.Idle 3318c2ecf20Sopenharmony_ci * is Link Up mapped to LTSSM Configuration.Idle, Recovery, L0, 3328c2ecf20Sopenharmony_ci * L0s, L1 and L2 states. And according to 3.2.1. Data Link 3338c2ecf20Sopenharmony_ci * Control and Management State Machine Rules is DL Up status 3348c2ecf20Sopenharmony_ci * reported in DL Active state. 3358c2ecf20Sopenharmony_ci */ 3368c2ecf20Sopenharmony_ci u8 ltssm_state = advk_pcie_ltssm_state(pcie); 3378c2ecf20Sopenharmony_ci return ltssm_state >= LTSSM_CONFIG_IDLE && ltssm_state < LTSSM_DISABLED; 3388c2ecf20Sopenharmony_ci} 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_cistatic inline bool advk_pcie_link_training(struct advk_pcie *pcie) 3418c2ecf20Sopenharmony_ci{ 3428c2ecf20Sopenharmony_ci /* 3438c2ecf20Sopenharmony_ci * According to PCIe Base specification 3.0, Table 4-14: Link 3448c2ecf20Sopenharmony_ci * Status Mapped to the LTSSM is Link Training mapped to LTSSM 3458c2ecf20Sopenharmony_ci * Configuration and Recovery states. 3468c2ecf20Sopenharmony_ci */ 3478c2ecf20Sopenharmony_ci u8 ltssm_state = advk_pcie_ltssm_state(pcie); 3488c2ecf20Sopenharmony_ci return ((ltssm_state >= LTSSM_CONFIG_LINKWIDTH_START && 3498c2ecf20Sopenharmony_ci ltssm_state < LTSSM_L0) || 3508c2ecf20Sopenharmony_ci (ltssm_state >= LTSSM_RECOVERY_EQUALIZATION_PHASE0 && 3518c2ecf20Sopenharmony_ci ltssm_state <= LTSSM_RECOVERY_EQUALIZATION_PHASE3)); 3528c2ecf20Sopenharmony_ci} 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_cistatic int advk_pcie_wait_for_link(struct advk_pcie *pcie) 3558c2ecf20Sopenharmony_ci{ 3568c2ecf20Sopenharmony_ci int retries; 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci /* check if the link is up or not */ 3598c2ecf20Sopenharmony_ci for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) { 3608c2ecf20Sopenharmony_ci if (advk_pcie_link_up(pcie)) 3618c2ecf20Sopenharmony_ci return 0; 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX); 3648c2ecf20Sopenharmony_ci } 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci return -ETIMEDOUT; 3678c2ecf20Sopenharmony_ci} 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_cistatic void advk_pcie_wait_for_retrain(struct advk_pcie *pcie) 3708c2ecf20Sopenharmony_ci{ 3718c2ecf20Sopenharmony_ci size_t retries; 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci for (retries = 0; retries < RETRAIN_WAIT_MAX_RETRIES; ++retries) { 3748c2ecf20Sopenharmony_ci if (advk_pcie_link_training(pcie)) 3758c2ecf20Sopenharmony_ci break; 3768c2ecf20Sopenharmony_ci udelay(RETRAIN_WAIT_USLEEP_US); 3778c2ecf20Sopenharmony_ci } 3788c2ecf20Sopenharmony_ci} 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_cistatic void advk_pcie_issue_perst(struct advk_pcie *pcie) 3818c2ecf20Sopenharmony_ci{ 3828c2ecf20Sopenharmony_ci if (!pcie->reset_gpio) 3838c2ecf20Sopenharmony_ci return; 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_ci /* 10ms delay is needed for some cards */ 3868c2ecf20Sopenharmony_ci dev_info(&pcie->pdev->dev, "issuing PERST via reset GPIO for 10ms\n"); 3878c2ecf20Sopenharmony_ci gpiod_set_value_cansleep(pcie->reset_gpio, 1); 3888c2ecf20Sopenharmony_ci usleep_range(10000, 11000); 3898c2ecf20Sopenharmony_ci gpiod_set_value_cansleep(pcie->reset_gpio, 0); 3908c2ecf20Sopenharmony_ci} 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_cistatic void advk_pcie_train_link(struct advk_pcie *pcie) 3938c2ecf20Sopenharmony_ci{ 3948c2ecf20Sopenharmony_ci struct device *dev = &pcie->pdev->dev; 3958c2ecf20Sopenharmony_ci u32 reg; 3968c2ecf20Sopenharmony_ci int ret; 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci /* 3998c2ecf20Sopenharmony_ci * Setup PCIe rev / gen compliance based on device tree property 4008c2ecf20Sopenharmony_ci * 'max-link-speed' which also forces maximal link speed. 4018c2ecf20Sopenharmony_ci */ 4028c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 4038c2ecf20Sopenharmony_ci reg &= ~PCIE_GEN_SEL_MSK; 4048c2ecf20Sopenharmony_ci if (pcie->link_gen == 3) 4058c2ecf20Sopenharmony_ci reg |= SPEED_GEN_3; 4068c2ecf20Sopenharmony_ci else if (pcie->link_gen == 2) 4078c2ecf20Sopenharmony_ci reg |= SPEED_GEN_2; 4088c2ecf20Sopenharmony_ci else 4098c2ecf20Sopenharmony_ci reg |= SPEED_GEN_1; 4108c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_ci /* 4138c2ecf20Sopenharmony_ci * Set maximal link speed value also into PCIe Link Control 2 register. 4148c2ecf20Sopenharmony_ci * Armada 3700 Functional Specification says that default value is based 4158c2ecf20Sopenharmony_ci * on SPEED_GEN but tests showed that default value is always 8.0 GT/s. 4168c2ecf20Sopenharmony_ci */ 4178c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2); 4188c2ecf20Sopenharmony_ci reg &= ~PCI_EXP_LNKCTL2_TLS; 4198c2ecf20Sopenharmony_ci if (pcie->link_gen == 3) 4208c2ecf20Sopenharmony_ci reg |= PCI_EXP_LNKCTL2_TLS_8_0GT; 4218c2ecf20Sopenharmony_ci else if (pcie->link_gen == 2) 4228c2ecf20Sopenharmony_ci reg |= PCI_EXP_LNKCTL2_TLS_5_0GT; 4238c2ecf20Sopenharmony_ci else 4248c2ecf20Sopenharmony_ci reg |= PCI_EXP_LNKCTL2_TLS_2_5GT; 4258c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2); 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci /* Enable link training after selecting PCIe generation */ 4288c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 4298c2ecf20Sopenharmony_ci reg |= LINK_TRAINING_EN; 4308c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci /* 4338c2ecf20Sopenharmony_ci * Reset PCIe card via PERST# signal. Some cards are not detected 4348c2ecf20Sopenharmony_ci * during link training when they are in some non-initial state. 4358c2ecf20Sopenharmony_ci */ 4368c2ecf20Sopenharmony_ci advk_pcie_issue_perst(pcie); 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci /* 4398c2ecf20Sopenharmony_ci * PERST# signal could have been asserted by pinctrl subsystem before 4408c2ecf20Sopenharmony_ci * probe() callback has been called or issued explicitly by reset gpio 4418c2ecf20Sopenharmony_ci * function advk_pcie_issue_perst(), making the endpoint going into 4428c2ecf20Sopenharmony_ci * fundamental reset. As required by PCI Express spec (PCI Express 4438c2ecf20Sopenharmony_ci * Base Specification, REV. 4.0 PCI Express, February 19 2014, 6.6.1 4448c2ecf20Sopenharmony_ci * Conventional Reset) a delay for at least 100ms after such a reset 4458c2ecf20Sopenharmony_ci * before sending a Configuration Request to the device is needed. 4468c2ecf20Sopenharmony_ci * So wait until PCIe link is up. Function advk_pcie_wait_for_link() 4478c2ecf20Sopenharmony_ci * waits for link at least 900ms. 4488c2ecf20Sopenharmony_ci */ 4498c2ecf20Sopenharmony_ci ret = advk_pcie_wait_for_link(pcie); 4508c2ecf20Sopenharmony_ci if (ret < 0) 4518c2ecf20Sopenharmony_ci dev_err(dev, "link never came up\n"); 4528c2ecf20Sopenharmony_ci else 4538c2ecf20Sopenharmony_ci dev_info(dev, "link up\n"); 4548c2ecf20Sopenharmony_ci} 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci/* 4578c2ecf20Sopenharmony_ci * Set PCIe address window register which could be used for memory 4588c2ecf20Sopenharmony_ci * mapping. 4598c2ecf20Sopenharmony_ci */ 4608c2ecf20Sopenharmony_cistatic void advk_pcie_set_ob_win(struct advk_pcie *pcie, u8 win_num, 4618c2ecf20Sopenharmony_ci phys_addr_t match, phys_addr_t remap, 4628c2ecf20Sopenharmony_ci phys_addr_t mask, u32 actions) 4638c2ecf20Sopenharmony_ci{ 4648c2ecf20Sopenharmony_ci advk_writel(pcie, OB_WIN_ENABLE | 4658c2ecf20Sopenharmony_ci lower_32_bits(match), OB_WIN_MATCH_LS(win_num)); 4668c2ecf20Sopenharmony_ci advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num)); 4678c2ecf20Sopenharmony_ci advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num)); 4688c2ecf20Sopenharmony_ci advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num)); 4698c2ecf20Sopenharmony_ci advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num)); 4708c2ecf20Sopenharmony_ci advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num)); 4718c2ecf20Sopenharmony_ci advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num)); 4728c2ecf20Sopenharmony_ci} 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_cistatic void advk_pcie_disable_ob_win(struct advk_pcie *pcie, u8 win_num) 4758c2ecf20Sopenharmony_ci{ 4768c2ecf20Sopenharmony_ci advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num)); 4778c2ecf20Sopenharmony_ci advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num)); 4788c2ecf20Sopenharmony_ci advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num)); 4798c2ecf20Sopenharmony_ci advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num)); 4808c2ecf20Sopenharmony_ci advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num)); 4818c2ecf20Sopenharmony_ci advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num)); 4828c2ecf20Sopenharmony_ci advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num)); 4838c2ecf20Sopenharmony_ci} 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_cistatic void advk_pcie_setup_hw(struct advk_pcie *pcie) 4868c2ecf20Sopenharmony_ci{ 4878c2ecf20Sopenharmony_ci u32 reg; 4888c2ecf20Sopenharmony_ci int i; 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci /* 4918c2ecf20Sopenharmony_ci * Configure PCIe Reference clock. Direction is from the PCIe 4928c2ecf20Sopenharmony_ci * controller to the endpoint card, so enable transmitting of 4938c2ecf20Sopenharmony_ci * Reference clock differential signal off-chip and disable 4948c2ecf20Sopenharmony_ci * receiving off-chip differential signal. 4958c2ecf20Sopenharmony_ci */ 4968c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_REF_CLK_REG); 4978c2ecf20Sopenharmony_ci reg |= PCIE_CORE_REF_CLK_TX_ENABLE; 4988c2ecf20Sopenharmony_ci reg &= ~PCIE_CORE_REF_CLK_RX_ENABLE; 4998c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_REF_CLK_REG); 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci /* Set to Direct mode */ 5028c2ecf20Sopenharmony_ci reg = advk_readl(pcie, CTRL_CONFIG_REG); 5038c2ecf20Sopenharmony_ci reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT); 5048c2ecf20Sopenharmony_ci reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT); 5058c2ecf20Sopenharmony_ci advk_writel(pcie, reg, CTRL_CONFIG_REG); 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci /* Set PCI global control register to RC mode */ 5088c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 5098c2ecf20Sopenharmony_ci reg |= (IS_RC_MSK << IS_RC_SHIFT); 5108c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_ci /* 5138c2ecf20Sopenharmony_ci * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab. 5148c2ecf20Sopenharmony_ci * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor 5158c2ecf20Sopenharmony_ci * id in high 16 bits. Updating this register changes readback value of 5168c2ecf20Sopenharmony_ci * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround 5178c2ecf20Sopenharmony_ci * for erratum 4.1: "The value of device and vendor ID is incorrect". 5188c2ecf20Sopenharmony_ci */ 5198c2ecf20Sopenharmony_ci reg = (PCI_VENDOR_ID_MARVELL << 16) | PCI_VENDOR_ID_MARVELL; 5208c2ecf20Sopenharmony_ci advk_writel(pcie, reg, VENDOR_ID_REG); 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_ci /* 5238c2ecf20Sopenharmony_ci * Change Class Code of PCI Bridge device to PCI Bridge (0x600400), 5248c2ecf20Sopenharmony_ci * because the default value is Mass storage controller (0x010400). 5258c2ecf20Sopenharmony_ci * 5268c2ecf20Sopenharmony_ci * Note that this Aardvark PCI Bridge does not have compliant Type 1 5278c2ecf20Sopenharmony_ci * Configuration Space and it even cannot be accessed via Aardvark's 5288c2ecf20Sopenharmony_ci * PCI config space access method. Something like config space is 5298c2ecf20Sopenharmony_ci * available in internal Aardvark registers starting at offset 0x0 5308c2ecf20Sopenharmony_ci * and is reported as Type 0. In range 0x10 - 0x34 it has totally 5318c2ecf20Sopenharmony_ci * different registers. 5328c2ecf20Sopenharmony_ci * 5338c2ecf20Sopenharmony_ci * Therefore driver uses emulation of PCI Bridge which emulates 5348c2ecf20Sopenharmony_ci * access to configuration space via internal Aardvark registers or 5358c2ecf20Sopenharmony_ci * emulated configuration buffer. 5368c2ecf20Sopenharmony_ci */ 5378c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_DEV_REV_REG); 5388c2ecf20Sopenharmony_ci reg &= ~0xffffff00; 5398c2ecf20Sopenharmony_ci reg |= (PCI_CLASS_BRIDGE_PCI << 8) << 8; 5408c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_DEV_REV_REG); 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci /* Disable Root Bridge I/O space, memory space and bus mastering */ 5438c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG); 5448c2ecf20Sopenharmony_ci reg &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); 5458c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG); 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci /* Set Advanced Error Capabilities and Control PF0 register */ 5488c2ecf20Sopenharmony_ci reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX | 5498c2ecf20Sopenharmony_ci PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN | 5508c2ecf20Sopenharmony_ci PCIE_CORE_ERR_CAPCTL_ECRC_CHCK | 5518c2ecf20Sopenharmony_ci PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV; 5528c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG); 5538c2ecf20Sopenharmony_ci 5548c2ecf20Sopenharmony_ci /* Set PCIe Device Control register */ 5558c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL); 5568c2ecf20Sopenharmony_ci reg &= ~PCI_EXP_DEVCTL_RELAX_EN; 5578c2ecf20Sopenharmony_ci reg &= ~PCI_EXP_DEVCTL_NOSNOOP_EN; 5588c2ecf20Sopenharmony_ci reg &= ~PCI_EXP_DEVCTL_PAYLOAD; 5598c2ecf20Sopenharmony_ci reg &= ~PCI_EXP_DEVCTL_READRQ; 5608c2ecf20Sopenharmony_ci reg |= PCI_EXP_DEVCTL_PAYLOAD_512B; 5618c2ecf20Sopenharmony_ci reg |= PCI_EXP_DEVCTL_READRQ_512B; 5628c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL); 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ci /* Program PCIe Control 2 to disable strict ordering */ 5658c2ecf20Sopenharmony_ci reg = PCIE_CORE_CTRL2_RESERVED | 5668c2ecf20Sopenharmony_ci PCIE_CORE_CTRL2_TD_ENABLE; 5678c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci /* Set lane X1 */ 5708c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 5718c2ecf20Sopenharmony_ci reg &= ~LANE_CNT_MSK; 5728c2ecf20Sopenharmony_ci reg |= LANE_COUNT_1; 5738c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ci /* Enable MSI */ 5768c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG); 5778c2ecf20Sopenharmony_ci reg |= PCIE_CORE_CTRL2_MSI_ENABLE; 5788c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); 5798c2ecf20Sopenharmony_ci 5808c2ecf20Sopenharmony_ci /* Clear all interrupts */ 5818c2ecf20Sopenharmony_ci advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG); 5828c2ecf20Sopenharmony_ci advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG); 5838c2ecf20Sopenharmony_ci advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG); 5848c2ecf20Sopenharmony_ci advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG); 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci /* Disable All ISR0/1 Sources */ 5878c2ecf20Sopenharmony_ci reg = PCIE_ISR0_ALL_MASK; 5888c2ecf20Sopenharmony_ci reg &= ~PCIE_ISR0_MSI_INT_PENDING; 5898c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PCIE_ISR0_MASK_REG); 5908c2ecf20Sopenharmony_ci 5918c2ecf20Sopenharmony_ci advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG); 5928c2ecf20Sopenharmony_ci 5938c2ecf20Sopenharmony_ci /* Unmask all MSIs */ 5948c2ecf20Sopenharmony_ci advk_writel(pcie, ~(u32)PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG); 5958c2ecf20Sopenharmony_ci 5968c2ecf20Sopenharmony_ci /* Enable summary interrupt for GIC SPI source */ 5978c2ecf20Sopenharmony_ci reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK); 5988c2ecf20Sopenharmony_ci advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG); 5998c2ecf20Sopenharmony_ci 6008c2ecf20Sopenharmony_ci /* 6018c2ecf20Sopenharmony_ci * Enable AXI address window location generation: 6028c2ecf20Sopenharmony_ci * When it is enabled, the default outbound window 6038c2ecf20Sopenharmony_ci * configurations (Default User Field: 0xD0074CFC) 6048c2ecf20Sopenharmony_ci * are used to transparent address translation for 6058c2ecf20Sopenharmony_ci * the outbound transactions. Thus, PCIe address 6068c2ecf20Sopenharmony_ci * windows are not required for transparent memory 6078c2ecf20Sopenharmony_ci * access when default outbound window configuration 6088c2ecf20Sopenharmony_ci * is set for memory access. 6098c2ecf20Sopenharmony_ci */ 6108c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG); 6118c2ecf20Sopenharmony_ci reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE; 6128c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); 6138c2ecf20Sopenharmony_ci 6148c2ecf20Sopenharmony_ci /* 6158c2ecf20Sopenharmony_ci * Set memory access in Default User Field so it 6168c2ecf20Sopenharmony_ci * is not required to configure PCIe address for 6178c2ecf20Sopenharmony_ci * transparent memory access. 6188c2ecf20Sopenharmony_ci */ 6198c2ecf20Sopenharmony_ci advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS); 6208c2ecf20Sopenharmony_ci 6218c2ecf20Sopenharmony_ci /* 6228c2ecf20Sopenharmony_ci * Bypass the address window mapping for PIO: 6238c2ecf20Sopenharmony_ci * Since PIO access already contains all required 6248c2ecf20Sopenharmony_ci * info over AXI interface by PIO registers, the 6258c2ecf20Sopenharmony_ci * address window is not required. 6268c2ecf20Sopenharmony_ci */ 6278c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PIO_CTRL); 6288c2ecf20Sopenharmony_ci reg |= PIO_CTRL_ADDR_WIN_DISABLE; 6298c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PIO_CTRL); 6308c2ecf20Sopenharmony_ci 6318c2ecf20Sopenharmony_ci /* 6328c2ecf20Sopenharmony_ci * Configure PCIe address windows for non-memory or 6338c2ecf20Sopenharmony_ci * non-transparent access as by default PCIe uses 6348c2ecf20Sopenharmony_ci * transparent memory access. 6358c2ecf20Sopenharmony_ci */ 6368c2ecf20Sopenharmony_ci for (i = 0; i < pcie->wins_count; i++) 6378c2ecf20Sopenharmony_ci advk_pcie_set_ob_win(pcie, i, 6388c2ecf20Sopenharmony_ci pcie->wins[i].match, pcie->wins[i].remap, 6398c2ecf20Sopenharmony_ci pcie->wins[i].mask, pcie->wins[i].actions); 6408c2ecf20Sopenharmony_ci 6418c2ecf20Sopenharmony_ci /* Disable remaining PCIe outbound windows */ 6428c2ecf20Sopenharmony_ci for (i = pcie->wins_count; i < OB_WIN_COUNT; i++) 6438c2ecf20Sopenharmony_ci advk_pcie_disable_ob_win(pcie, i); 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_ci advk_pcie_train_link(pcie); 6468c2ecf20Sopenharmony_ci} 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_cistatic int advk_pcie_check_pio_status(struct advk_pcie *pcie, bool allow_crs, u32 *val) 6498c2ecf20Sopenharmony_ci{ 6508c2ecf20Sopenharmony_ci struct device *dev = &pcie->pdev->dev; 6518c2ecf20Sopenharmony_ci u32 reg; 6528c2ecf20Sopenharmony_ci unsigned int status; 6538c2ecf20Sopenharmony_ci char *strcomp_status, *str_posted; 6548c2ecf20Sopenharmony_ci int ret; 6558c2ecf20Sopenharmony_ci 6568c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PIO_STAT); 6578c2ecf20Sopenharmony_ci status = (reg & PIO_COMPLETION_STATUS_MASK) >> 6588c2ecf20Sopenharmony_ci PIO_COMPLETION_STATUS_SHIFT; 6598c2ecf20Sopenharmony_ci 6608c2ecf20Sopenharmony_ci /* 6618c2ecf20Sopenharmony_ci * According to HW spec, the PIO status check sequence as below: 6628c2ecf20Sopenharmony_ci * 1) even if COMPLETION_STATUS(bit9:7) indicates successful, 6638c2ecf20Sopenharmony_ci * it still needs to check Error Status(bit11), only when this bit 6648c2ecf20Sopenharmony_ci * indicates no error happen, the operation is successful. 6658c2ecf20Sopenharmony_ci * 2) value Unsupported Request(1) of COMPLETION_STATUS(bit9:7) only 6668c2ecf20Sopenharmony_ci * means a PIO write error, and for PIO read it is successful with 6678c2ecf20Sopenharmony_ci * a read value of 0xFFFFFFFF. 6688c2ecf20Sopenharmony_ci * 3) value Completion Retry Status(CRS) of COMPLETION_STATUS(bit9:7) 6698c2ecf20Sopenharmony_ci * only means a PIO write error, and for PIO read it is successful 6708c2ecf20Sopenharmony_ci * with a read value of 0xFFFF0001. 6718c2ecf20Sopenharmony_ci * 4) value Completer Abort (CA) of COMPLETION_STATUS(bit9:7) means 6728c2ecf20Sopenharmony_ci * error for both PIO read and PIO write operation. 6738c2ecf20Sopenharmony_ci * 5) other errors are indicated as 'unknown'. 6748c2ecf20Sopenharmony_ci */ 6758c2ecf20Sopenharmony_ci switch (status) { 6768c2ecf20Sopenharmony_ci case PIO_COMPLETION_STATUS_OK: 6778c2ecf20Sopenharmony_ci if (reg & PIO_ERR_STATUS) { 6788c2ecf20Sopenharmony_ci strcomp_status = "COMP_ERR"; 6798c2ecf20Sopenharmony_ci ret = -EFAULT; 6808c2ecf20Sopenharmony_ci break; 6818c2ecf20Sopenharmony_ci } 6828c2ecf20Sopenharmony_ci /* Get the read result */ 6838c2ecf20Sopenharmony_ci if (val) 6848c2ecf20Sopenharmony_ci *val = advk_readl(pcie, PIO_RD_DATA); 6858c2ecf20Sopenharmony_ci /* No error */ 6868c2ecf20Sopenharmony_ci strcomp_status = NULL; 6878c2ecf20Sopenharmony_ci ret = 0; 6888c2ecf20Sopenharmony_ci break; 6898c2ecf20Sopenharmony_ci case PIO_COMPLETION_STATUS_UR: 6908c2ecf20Sopenharmony_ci strcomp_status = "UR"; 6918c2ecf20Sopenharmony_ci ret = -EOPNOTSUPP; 6928c2ecf20Sopenharmony_ci break; 6938c2ecf20Sopenharmony_ci case PIO_COMPLETION_STATUS_CRS: 6948c2ecf20Sopenharmony_ci if (allow_crs && val) { 6958c2ecf20Sopenharmony_ci /* PCIe r4.0, sec 2.3.2, says: 6968c2ecf20Sopenharmony_ci * If CRS Software Visibility is enabled: 6978c2ecf20Sopenharmony_ci * For a Configuration Read Request that includes both 6988c2ecf20Sopenharmony_ci * bytes of the Vendor ID field of a device Function's 6998c2ecf20Sopenharmony_ci * Configuration Space Header, the Root Complex must 7008c2ecf20Sopenharmony_ci * complete the Request to the host by returning a 7018c2ecf20Sopenharmony_ci * read-data value of 0001h for the Vendor ID field and 7028c2ecf20Sopenharmony_ci * all '1's for any additional bytes included in the 7038c2ecf20Sopenharmony_ci * request. 7048c2ecf20Sopenharmony_ci * 7058c2ecf20Sopenharmony_ci * So CRS in this case is not an error status. 7068c2ecf20Sopenharmony_ci */ 7078c2ecf20Sopenharmony_ci *val = CFG_RD_CRS_VAL; 7088c2ecf20Sopenharmony_ci strcomp_status = NULL; 7098c2ecf20Sopenharmony_ci ret = 0; 7108c2ecf20Sopenharmony_ci break; 7118c2ecf20Sopenharmony_ci } 7128c2ecf20Sopenharmony_ci /* PCIe r4.0, sec 2.3.2, says: 7138c2ecf20Sopenharmony_ci * If CRS Software Visibility is not enabled, the Root Complex 7148c2ecf20Sopenharmony_ci * must re-issue the Configuration Request as a new Request. 7158c2ecf20Sopenharmony_ci * If CRS Software Visibility is enabled: For a Configuration 7168c2ecf20Sopenharmony_ci * Write Request or for any other Configuration Read Request, 7178c2ecf20Sopenharmony_ci * the Root Complex must re-issue the Configuration Request as 7188c2ecf20Sopenharmony_ci * a new Request. 7198c2ecf20Sopenharmony_ci * A Root Complex implementation may choose to limit the number 7208c2ecf20Sopenharmony_ci * of Configuration Request/CRS Completion Status loops before 7218c2ecf20Sopenharmony_ci * determining that something is wrong with the target of the 7228c2ecf20Sopenharmony_ci * Request and taking appropriate action, e.g., complete the 7238c2ecf20Sopenharmony_ci * Request to the host as a failed transaction. 7248c2ecf20Sopenharmony_ci * 7258c2ecf20Sopenharmony_ci * So return -EAGAIN and caller (pci-aardvark.c driver) will 7268c2ecf20Sopenharmony_ci * re-issue request again up to the PIO_RETRY_CNT retries. 7278c2ecf20Sopenharmony_ci */ 7288c2ecf20Sopenharmony_ci strcomp_status = "CRS"; 7298c2ecf20Sopenharmony_ci ret = -EAGAIN; 7308c2ecf20Sopenharmony_ci break; 7318c2ecf20Sopenharmony_ci case PIO_COMPLETION_STATUS_CA: 7328c2ecf20Sopenharmony_ci strcomp_status = "CA"; 7338c2ecf20Sopenharmony_ci ret = -ECANCELED; 7348c2ecf20Sopenharmony_ci break; 7358c2ecf20Sopenharmony_ci default: 7368c2ecf20Sopenharmony_ci strcomp_status = "Unknown"; 7378c2ecf20Sopenharmony_ci ret = -EINVAL; 7388c2ecf20Sopenharmony_ci break; 7398c2ecf20Sopenharmony_ci } 7408c2ecf20Sopenharmony_ci 7418c2ecf20Sopenharmony_ci if (!strcomp_status) 7428c2ecf20Sopenharmony_ci return ret; 7438c2ecf20Sopenharmony_ci 7448c2ecf20Sopenharmony_ci if (reg & PIO_NON_POSTED_REQ) 7458c2ecf20Sopenharmony_ci str_posted = "Non-posted"; 7468c2ecf20Sopenharmony_ci else 7478c2ecf20Sopenharmony_ci str_posted = "Posted"; 7488c2ecf20Sopenharmony_ci 7498c2ecf20Sopenharmony_ci dev_dbg(dev, "%s PIO Response Status: %s, %#x @ %#x\n", 7508c2ecf20Sopenharmony_ci str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS)); 7518c2ecf20Sopenharmony_ci 7528c2ecf20Sopenharmony_ci return ret; 7538c2ecf20Sopenharmony_ci} 7548c2ecf20Sopenharmony_ci 7558c2ecf20Sopenharmony_cistatic int advk_pcie_wait_pio(struct advk_pcie *pcie) 7568c2ecf20Sopenharmony_ci{ 7578c2ecf20Sopenharmony_ci struct device *dev = &pcie->pdev->dev; 7588c2ecf20Sopenharmony_ci int i; 7598c2ecf20Sopenharmony_ci 7608c2ecf20Sopenharmony_ci for (i = 1; i <= PIO_RETRY_CNT; i++) { 7618c2ecf20Sopenharmony_ci u32 start, isr; 7628c2ecf20Sopenharmony_ci 7638c2ecf20Sopenharmony_ci start = advk_readl(pcie, PIO_START); 7648c2ecf20Sopenharmony_ci isr = advk_readl(pcie, PIO_ISR); 7658c2ecf20Sopenharmony_ci if (!start && isr) 7668c2ecf20Sopenharmony_ci return i; 7678c2ecf20Sopenharmony_ci udelay(PIO_RETRY_DELAY); 7688c2ecf20Sopenharmony_ci } 7698c2ecf20Sopenharmony_ci 7708c2ecf20Sopenharmony_ci dev_err(dev, "PIO read/write transfer time out\n"); 7718c2ecf20Sopenharmony_ci return -ETIMEDOUT; 7728c2ecf20Sopenharmony_ci} 7738c2ecf20Sopenharmony_ci 7748c2ecf20Sopenharmony_cistatic pci_bridge_emul_read_status_t 7758c2ecf20Sopenharmony_ciadvk_pci_bridge_emul_base_conf_read(struct pci_bridge_emul *bridge, 7768c2ecf20Sopenharmony_ci int reg, u32 *value) 7778c2ecf20Sopenharmony_ci{ 7788c2ecf20Sopenharmony_ci struct advk_pcie *pcie = bridge->data; 7798c2ecf20Sopenharmony_ci 7808c2ecf20Sopenharmony_ci switch (reg) { 7818c2ecf20Sopenharmony_ci case PCI_COMMAND: 7828c2ecf20Sopenharmony_ci *value = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG); 7838c2ecf20Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_ci case PCI_INTERRUPT_LINE: { 7868c2ecf20Sopenharmony_ci /* 7878c2ecf20Sopenharmony_ci * From the whole 32bit register we support reading from HW only 7888c2ecf20Sopenharmony_ci * one bit: PCI_BRIDGE_CTL_BUS_RESET. 7898c2ecf20Sopenharmony_ci * Other bits are retrieved only from emulated config buffer. 7908c2ecf20Sopenharmony_ci */ 7918c2ecf20Sopenharmony_ci __le32 *cfgspace = (__le32 *)&bridge->conf; 7928c2ecf20Sopenharmony_ci u32 val = le32_to_cpu(cfgspace[PCI_INTERRUPT_LINE / 4]); 7938c2ecf20Sopenharmony_ci if (advk_readl(pcie, PCIE_CORE_CTRL1_REG) & HOT_RESET_GEN) 7948c2ecf20Sopenharmony_ci val |= PCI_BRIDGE_CTL_BUS_RESET << 16; 7958c2ecf20Sopenharmony_ci else 7968c2ecf20Sopenharmony_ci val &= ~(PCI_BRIDGE_CTL_BUS_RESET << 16); 7978c2ecf20Sopenharmony_ci *value = val; 7988c2ecf20Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 7998c2ecf20Sopenharmony_ci } 8008c2ecf20Sopenharmony_ci 8018c2ecf20Sopenharmony_ci default: 8028c2ecf20Sopenharmony_ci return PCI_BRIDGE_EMUL_NOT_HANDLED; 8038c2ecf20Sopenharmony_ci } 8048c2ecf20Sopenharmony_ci} 8058c2ecf20Sopenharmony_ci 8068c2ecf20Sopenharmony_cistatic void 8078c2ecf20Sopenharmony_ciadvk_pci_bridge_emul_base_conf_write(struct pci_bridge_emul *bridge, 8088c2ecf20Sopenharmony_ci int reg, u32 old, u32 new, u32 mask) 8098c2ecf20Sopenharmony_ci{ 8108c2ecf20Sopenharmony_ci struct advk_pcie *pcie = bridge->data; 8118c2ecf20Sopenharmony_ci 8128c2ecf20Sopenharmony_ci switch (reg) { 8138c2ecf20Sopenharmony_ci case PCI_COMMAND: 8148c2ecf20Sopenharmony_ci advk_writel(pcie, new, PCIE_CORE_CMD_STATUS_REG); 8158c2ecf20Sopenharmony_ci break; 8168c2ecf20Sopenharmony_ci 8178c2ecf20Sopenharmony_ci case PCI_INTERRUPT_LINE: 8188c2ecf20Sopenharmony_ci if (mask & (PCI_BRIDGE_CTL_BUS_RESET << 16)) { 8198c2ecf20Sopenharmony_ci u32 val = advk_readl(pcie, PCIE_CORE_CTRL1_REG); 8208c2ecf20Sopenharmony_ci if (new & (PCI_BRIDGE_CTL_BUS_RESET << 16)) 8218c2ecf20Sopenharmony_ci val |= HOT_RESET_GEN; 8228c2ecf20Sopenharmony_ci else 8238c2ecf20Sopenharmony_ci val &= ~HOT_RESET_GEN; 8248c2ecf20Sopenharmony_ci advk_writel(pcie, val, PCIE_CORE_CTRL1_REG); 8258c2ecf20Sopenharmony_ci } 8268c2ecf20Sopenharmony_ci break; 8278c2ecf20Sopenharmony_ci 8288c2ecf20Sopenharmony_ci default: 8298c2ecf20Sopenharmony_ci break; 8308c2ecf20Sopenharmony_ci } 8318c2ecf20Sopenharmony_ci} 8328c2ecf20Sopenharmony_ci 8338c2ecf20Sopenharmony_cistatic pci_bridge_emul_read_status_t 8348c2ecf20Sopenharmony_ciadvk_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge, 8358c2ecf20Sopenharmony_ci int reg, u32 *value) 8368c2ecf20Sopenharmony_ci{ 8378c2ecf20Sopenharmony_ci struct advk_pcie *pcie = bridge->data; 8388c2ecf20Sopenharmony_ci 8398c2ecf20Sopenharmony_ci 8408c2ecf20Sopenharmony_ci switch (reg) { 8418c2ecf20Sopenharmony_ci case PCI_EXP_SLTCTL: 8428c2ecf20Sopenharmony_ci *value = PCI_EXP_SLTSTA_PDS << 16; 8438c2ecf20Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 8448c2ecf20Sopenharmony_ci 8458c2ecf20Sopenharmony_ci case PCI_EXP_RTCTL: { 8468c2ecf20Sopenharmony_ci u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG); 8478c2ecf20Sopenharmony_ci *value = (val & PCIE_MSG_PM_PME_MASK) ? 0 : PCI_EXP_RTCTL_PMEIE; 8488c2ecf20Sopenharmony_ci *value |= le16_to_cpu(bridge->pcie_conf.rootctl) & PCI_EXP_RTCTL_CRSSVE; 8498c2ecf20Sopenharmony_ci *value |= PCI_EXP_RTCAP_CRSVIS << 16; 8508c2ecf20Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 8518c2ecf20Sopenharmony_ci } 8528c2ecf20Sopenharmony_ci 8538c2ecf20Sopenharmony_ci case PCI_EXP_RTSTA: { 8548c2ecf20Sopenharmony_ci u32 isr0 = advk_readl(pcie, PCIE_ISR0_REG); 8558c2ecf20Sopenharmony_ci u32 msglog = advk_readl(pcie, PCIE_MSG_LOG_REG); 8568c2ecf20Sopenharmony_ci *value = msglog >> 16; 8578c2ecf20Sopenharmony_ci if (isr0 & PCIE_MSG_PM_PME_MASK) 8588c2ecf20Sopenharmony_ci *value |= PCI_EXP_RTSTA_PME; 8598c2ecf20Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 8608c2ecf20Sopenharmony_ci } 8618c2ecf20Sopenharmony_ci 8628c2ecf20Sopenharmony_ci case PCI_EXP_LNKCAP: { 8638c2ecf20Sopenharmony_ci u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg); 8648c2ecf20Sopenharmony_ci /* 8658c2ecf20Sopenharmony_ci * PCI_EXP_LNKCAP_DLLLARC bit is hardwired in aardvark HW to 0. 8668c2ecf20Sopenharmony_ci * But support for PCI_EXP_LNKSTA_DLLLA is emulated via ltssm 8678c2ecf20Sopenharmony_ci * state so explicitly enable PCI_EXP_LNKCAP_DLLLARC flag. 8688c2ecf20Sopenharmony_ci */ 8698c2ecf20Sopenharmony_ci val |= PCI_EXP_LNKCAP_DLLLARC; 8708c2ecf20Sopenharmony_ci *value = val; 8718c2ecf20Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 8728c2ecf20Sopenharmony_ci } 8738c2ecf20Sopenharmony_ci 8748c2ecf20Sopenharmony_ci case PCI_EXP_LNKCTL: { 8758c2ecf20Sopenharmony_ci /* u32 contains both PCI_EXP_LNKCTL and PCI_EXP_LNKSTA */ 8768c2ecf20Sopenharmony_ci u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg) & 8778c2ecf20Sopenharmony_ci ~(PCI_EXP_LNKSTA_LT << 16); 8788c2ecf20Sopenharmony_ci if (advk_pcie_link_training(pcie)) 8798c2ecf20Sopenharmony_ci val |= (PCI_EXP_LNKSTA_LT << 16); 8808c2ecf20Sopenharmony_ci if (advk_pcie_link_active(pcie)) 8818c2ecf20Sopenharmony_ci val |= (PCI_EXP_LNKSTA_DLLLA << 16); 8828c2ecf20Sopenharmony_ci *value = val; 8838c2ecf20Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 8848c2ecf20Sopenharmony_ci } 8858c2ecf20Sopenharmony_ci 8868c2ecf20Sopenharmony_ci case PCI_EXP_DEVCAP: 8878c2ecf20Sopenharmony_ci case PCI_EXP_DEVCTL: 8888c2ecf20Sopenharmony_ci *value = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg); 8898c2ecf20Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 8908c2ecf20Sopenharmony_ci default: 8918c2ecf20Sopenharmony_ci return PCI_BRIDGE_EMUL_NOT_HANDLED; 8928c2ecf20Sopenharmony_ci } 8938c2ecf20Sopenharmony_ci 8948c2ecf20Sopenharmony_ci} 8958c2ecf20Sopenharmony_ci 8968c2ecf20Sopenharmony_cistatic void 8978c2ecf20Sopenharmony_ciadvk_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge, 8988c2ecf20Sopenharmony_ci int reg, u32 old, u32 new, u32 mask) 8998c2ecf20Sopenharmony_ci{ 9008c2ecf20Sopenharmony_ci struct advk_pcie *pcie = bridge->data; 9018c2ecf20Sopenharmony_ci 9028c2ecf20Sopenharmony_ci switch (reg) { 9038c2ecf20Sopenharmony_ci case PCI_EXP_DEVCTL: 9048c2ecf20Sopenharmony_ci advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg); 9058c2ecf20Sopenharmony_ci break; 9068c2ecf20Sopenharmony_ci 9078c2ecf20Sopenharmony_ci case PCI_EXP_LNKCTL: 9088c2ecf20Sopenharmony_ci advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg); 9098c2ecf20Sopenharmony_ci if (new & PCI_EXP_LNKCTL_RL) 9108c2ecf20Sopenharmony_ci advk_pcie_wait_for_retrain(pcie); 9118c2ecf20Sopenharmony_ci break; 9128c2ecf20Sopenharmony_ci 9138c2ecf20Sopenharmony_ci case PCI_EXP_RTCTL: { 9148c2ecf20Sopenharmony_ci /* Only mask/unmask PME interrupt */ 9158c2ecf20Sopenharmony_ci u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG) & 9168c2ecf20Sopenharmony_ci ~PCIE_MSG_PM_PME_MASK; 9178c2ecf20Sopenharmony_ci if ((new & PCI_EXP_RTCTL_PMEIE) == 0) 9188c2ecf20Sopenharmony_ci val |= PCIE_MSG_PM_PME_MASK; 9198c2ecf20Sopenharmony_ci advk_writel(pcie, val, PCIE_ISR0_MASK_REG); 9208c2ecf20Sopenharmony_ci break; 9218c2ecf20Sopenharmony_ci } 9228c2ecf20Sopenharmony_ci 9238c2ecf20Sopenharmony_ci case PCI_EXP_RTSTA: 9248c2ecf20Sopenharmony_ci new = (new & PCI_EXP_RTSTA_PME) >> 9; 9258c2ecf20Sopenharmony_ci advk_writel(pcie, new, PCIE_ISR0_REG); 9268c2ecf20Sopenharmony_ci break; 9278c2ecf20Sopenharmony_ci 9288c2ecf20Sopenharmony_ci default: 9298c2ecf20Sopenharmony_ci break; 9308c2ecf20Sopenharmony_ci } 9318c2ecf20Sopenharmony_ci} 9328c2ecf20Sopenharmony_ci 9338c2ecf20Sopenharmony_cistatic struct pci_bridge_emul_ops advk_pci_bridge_emul_ops = { 9348c2ecf20Sopenharmony_ci .read_base = advk_pci_bridge_emul_base_conf_read, 9358c2ecf20Sopenharmony_ci .write_base = advk_pci_bridge_emul_base_conf_write, 9368c2ecf20Sopenharmony_ci .read_pcie = advk_pci_bridge_emul_pcie_conf_read, 9378c2ecf20Sopenharmony_ci .write_pcie = advk_pci_bridge_emul_pcie_conf_write, 9388c2ecf20Sopenharmony_ci}; 9398c2ecf20Sopenharmony_ci 9408c2ecf20Sopenharmony_ci/* 9418c2ecf20Sopenharmony_ci * Initialize the configuration space of the PCI-to-PCI bridge 9428c2ecf20Sopenharmony_ci * associated with the given PCIe interface. 9438c2ecf20Sopenharmony_ci */ 9448c2ecf20Sopenharmony_cistatic int advk_sw_pci_bridge_init(struct advk_pcie *pcie) 9458c2ecf20Sopenharmony_ci{ 9468c2ecf20Sopenharmony_ci struct pci_bridge_emul *bridge = &pcie->bridge; 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_ci bridge->conf.vendor = 9498c2ecf20Sopenharmony_ci cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) & 0xffff); 9508c2ecf20Sopenharmony_ci bridge->conf.device = 9518c2ecf20Sopenharmony_ci cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) >> 16); 9528c2ecf20Sopenharmony_ci bridge->conf.class_revision = 9538c2ecf20Sopenharmony_ci cpu_to_le32(advk_readl(pcie, PCIE_CORE_DEV_REV_REG) & 0xff); 9548c2ecf20Sopenharmony_ci 9558c2ecf20Sopenharmony_ci /* Support 32 bits I/O addressing */ 9568c2ecf20Sopenharmony_ci bridge->conf.iobase = PCI_IO_RANGE_TYPE_32; 9578c2ecf20Sopenharmony_ci bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32; 9588c2ecf20Sopenharmony_ci 9598c2ecf20Sopenharmony_ci /* Support 64 bits memory pref */ 9608c2ecf20Sopenharmony_ci bridge->conf.pref_mem_base = cpu_to_le16(PCI_PREF_RANGE_TYPE_64); 9618c2ecf20Sopenharmony_ci bridge->conf.pref_mem_limit = cpu_to_le16(PCI_PREF_RANGE_TYPE_64); 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_ci /* Support interrupt A for MSI feature */ 9648c2ecf20Sopenharmony_ci bridge->conf.intpin = PCIE_CORE_INT_A_ASSERT_ENABLE; 9658c2ecf20Sopenharmony_ci 9668c2ecf20Sopenharmony_ci /* Aardvark HW provides PCIe Capability structure in version 2 */ 9678c2ecf20Sopenharmony_ci bridge->pcie_conf.cap = cpu_to_le16(2); 9688c2ecf20Sopenharmony_ci 9698c2ecf20Sopenharmony_ci /* Indicates supports for Completion Retry Status */ 9708c2ecf20Sopenharmony_ci bridge->pcie_conf.rootcap = cpu_to_le16(PCI_EXP_RTCAP_CRSVIS); 9718c2ecf20Sopenharmony_ci 9728c2ecf20Sopenharmony_ci bridge->has_pcie = true; 9738c2ecf20Sopenharmony_ci bridge->data = pcie; 9748c2ecf20Sopenharmony_ci bridge->ops = &advk_pci_bridge_emul_ops; 9758c2ecf20Sopenharmony_ci 9768c2ecf20Sopenharmony_ci return pci_bridge_emul_init(bridge, 0); 9778c2ecf20Sopenharmony_ci} 9788c2ecf20Sopenharmony_ci 9798c2ecf20Sopenharmony_cistatic bool advk_pcie_valid_device(struct advk_pcie *pcie, struct pci_bus *bus, 9808c2ecf20Sopenharmony_ci int devfn) 9818c2ecf20Sopenharmony_ci{ 9828c2ecf20Sopenharmony_ci if (pci_is_root_bus(bus) && PCI_SLOT(devfn) != 0) 9838c2ecf20Sopenharmony_ci return false; 9848c2ecf20Sopenharmony_ci 9858c2ecf20Sopenharmony_ci /* 9868c2ecf20Sopenharmony_ci * If the link goes down after we check for link-up, nothing bad 9878c2ecf20Sopenharmony_ci * happens but the config access times out. 9888c2ecf20Sopenharmony_ci */ 9898c2ecf20Sopenharmony_ci if (!pci_is_root_bus(bus) && !advk_pcie_link_up(pcie)) 9908c2ecf20Sopenharmony_ci return false; 9918c2ecf20Sopenharmony_ci 9928c2ecf20Sopenharmony_ci return true; 9938c2ecf20Sopenharmony_ci} 9948c2ecf20Sopenharmony_ci 9958c2ecf20Sopenharmony_cistatic bool advk_pcie_pio_is_running(struct advk_pcie *pcie) 9968c2ecf20Sopenharmony_ci{ 9978c2ecf20Sopenharmony_ci struct device *dev = &pcie->pdev->dev; 9988c2ecf20Sopenharmony_ci 9998c2ecf20Sopenharmony_ci /* 10008c2ecf20Sopenharmony_ci * Trying to start a new PIO transfer when previous has not completed 10018c2ecf20Sopenharmony_ci * cause External Abort on CPU which results in kernel panic: 10028c2ecf20Sopenharmony_ci * 10038c2ecf20Sopenharmony_ci * SError Interrupt on CPU0, code 0xbf000002 -- SError 10048c2ecf20Sopenharmony_ci * Kernel panic - not syncing: Asynchronous SError Interrupt 10058c2ecf20Sopenharmony_ci * 10068c2ecf20Sopenharmony_ci * Functions advk_pcie_rd_conf() and advk_pcie_wr_conf() are protected 10078c2ecf20Sopenharmony_ci * by raw_spin_lock_irqsave() at pci_lock_config() level to prevent 10088c2ecf20Sopenharmony_ci * concurrent calls at the same time. But because PIO transfer may take 10098c2ecf20Sopenharmony_ci * about 1.5s when link is down or card is disconnected, it means that 10108c2ecf20Sopenharmony_ci * advk_pcie_wait_pio() does not always have to wait for completion. 10118c2ecf20Sopenharmony_ci * 10128c2ecf20Sopenharmony_ci * Some versions of ARM Trusted Firmware handles this External Abort at 10138c2ecf20Sopenharmony_ci * EL3 level and mask it to prevent kernel panic. Relevant TF-A commit: 10148c2ecf20Sopenharmony_ci * https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=3c7dcdac5c50 10158c2ecf20Sopenharmony_ci */ 10168c2ecf20Sopenharmony_ci if (advk_readl(pcie, PIO_START)) { 10178c2ecf20Sopenharmony_ci dev_err(dev, "Previous PIO read/write transfer is still running\n"); 10188c2ecf20Sopenharmony_ci return true; 10198c2ecf20Sopenharmony_ci } 10208c2ecf20Sopenharmony_ci 10218c2ecf20Sopenharmony_ci return false; 10228c2ecf20Sopenharmony_ci} 10238c2ecf20Sopenharmony_ci 10248c2ecf20Sopenharmony_cistatic int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn, 10258c2ecf20Sopenharmony_ci int where, int size, u32 *val) 10268c2ecf20Sopenharmony_ci{ 10278c2ecf20Sopenharmony_ci struct advk_pcie *pcie = bus->sysdata; 10288c2ecf20Sopenharmony_ci int retry_count; 10298c2ecf20Sopenharmony_ci bool allow_crs; 10308c2ecf20Sopenharmony_ci u32 reg; 10318c2ecf20Sopenharmony_ci int ret; 10328c2ecf20Sopenharmony_ci 10338c2ecf20Sopenharmony_ci if (!advk_pcie_valid_device(pcie, bus, devfn)) { 10348c2ecf20Sopenharmony_ci *val = 0xffffffff; 10358c2ecf20Sopenharmony_ci return PCIBIOS_DEVICE_NOT_FOUND; 10368c2ecf20Sopenharmony_ci } 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_ci if (pci_is_root_bus(bus)) 10398c2ecf20Sopenharmony_ci return pci_bridge_emul_conf_read(&pcie->bridge, where, 10408c2ecf20Sopenharmony_ci size, val); 10418c2ecf20Sopenharmony_ci 10428c2ecf20Sopenharmony_ci /* 10438c2ecf20Sopenharmony_ci * Completion Retry Status is possible to return only when reading all 10448c2ecf20Sopenharmony_ci * 4 bytes from PCI_VENDOR_ID and PCI_DEVICE_ID registers at once and 10458c2ecf20Sopenharmony_ci * CRSSVE flag on Root Bridge is enabled. 10468c2ecf20Sopenharmony_ci */ 10478c2ecf20Sopenharmony_ci allow_crs = (where == PCI_VENDOR_ID) && (size == 4) && 10488c2ecf20Sopenharmony_ci (le16_to_cpu(pcie->bridge.pcie_conf.rootctl) & 10498c2ecf20Sopenharmony_ci PCI_EXP_RTCTL_CRSSVE); 10508c2ecf20Sopenharmony_ci 10518c2ecf20Sopenharmony_ci if (advk_pcie_pio_is_running(pcie)) 10528c2ecf20Sopenharmony_ci goto try_crs; 10538c2ecf20Sopenharmony_ci 10548c2ecf20Sopenharmony_ci /* Program the control register */ 10558c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PIO_CTRL); 10568c2ecf20Sopenharmony_ci reg &= ~PIO_CTRL_TYPE_MASK; 10578c2ecf20Sopenharmony_ci if (pci_is_root_bus(bus->parent)) 10588c2ecf20Sopenharmony_ci reg |= PCIE_CONFIG_RD_TYPE0; 10598c2ecf20Sopenharmony_ci else 10608c2ecf20Sopenharmony_ci reg |= PCIE_CONFIG_RD_TYPE1; 10618c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PIO_CTRL); 10628c2ecf20Sopenharmony_ci 10638c2ecf20Sopenharmony_ci /* Program the address registers */ 10648c2ecf20Sopenharmony_ci reg = PCIE_CONF_ADDR(bus->number, devfn, where); 10658c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PIO_ADDR_LS); 10668c2ecf20Sopenharmony_ci advk_writel(pcie, 0, PIO_ADDR_MS); 10678c2ecf20Sopenharmony_ci 10688c2ecf20Sopenharmony_ci /* Program the data strobe */ 10698c2ecf20Sopenharmony_ci advk_writel(pcie, 0xf, PIO_WR_DATA_STRB); 10708c2ecf20Sopenharmony_ci 10718c2ecf20Sopenharmony_ci retry_count = 0; 10728c2ecf20Sopenharmony_ci do { 10738c2ecf20Sopenharmony_ci /* Clear PIO DONE ISR and start the transfer */ 10748c2ecf20Sopenharmony_ci advk_writel(pcie, 1, PIO_ISR); 10758c2ecf20Sopenharmony_ci advk_writel(pcie, 1, PIO_START); 10768c2ecf20Sopenharmony_ci 10778c2ecf20Sopenharmony_ci ret = advk_pcie_wait_pio(pcie); 10788c2ecf20Sopenharmony_ci if (ret < 0) 10798c2ecf20Sopenharmony_ci goto try_crs; 10808c2ecf20Sopenharmony_ci 10818c2ecf20Sopenharmony_ci retry_count += ret; 10828c2ecf20Sopenharmony_ci 10838c2ecf20Sopenharmony_ci /* Check PIO status and get the read result */ 10848c2ecf20Sopenharmony_ci ret = advk_pcie_check_pio_status(pcie, allow_crs, val); 10858c2ecf20Sopenharmony_ci } while (ret == -EAGAIN && retry_count < PIO_RETRY_CNT); 10868c2ecf20Sopenharmony_ci 10878c2ecf20Sopenharmony_ci if (ret < 0) 10888c2ecf20Sopenharmony_ci goto fail; 10898c2ecf20Sopenharmony_ci 10908c2ecf20Sopenharmony_ci if (size == 1) 10918c2ecf20Sopenharmony_ci *val = (*val >> (8 * (where & 3))) & 0xff; 10928c2ecf20Sopenharmony_ci else if (size == 2) 10938c2ecf20Sopenharmony_ci *val = (*val >> (8 * (where & 3))) & 0xffff; 10948c2ecf20Sopenharmony_ci 10958c2ecf20Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_citry_crs: 10988c2ecf20Sopenharmony_ci /* 10998c2ecf20Sopenharmony_ci * If it is possible, return Completion Retry Status so that caller 11008c2ecf20Sopenharmony_ci * tries to issue the request again instead of failing. 11018c2ecf20Sopenharmony_ci */ 11028c2ecf20Sopenharmony_ci if (allow_crs) { 11038c2ecf20Sopenharmony_ci *val = CFG_RD_CRS_VAL; 11048c2ecf20Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 11058c2ecf20Sopenharmony_ci } 11068c2ecf20Sopenharmony_ci 11078c2ecf20Sopenharmony_cifail: 11088c2ecf20Sopenharmony_ci *val = 0xffffffff; 11098c2ecf20Sopenharmony_ci return PCIBIOS_SET_FAILED; 11108c2ecf20Sopenharmony_ci} 11118c2ecf20Sopenharmony_ci 11128c2ecf20Sopenharmony_cistatic int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn, 11138c2ecf20Sopenharmony_ci int where, int size, u32 val) 11148c2ecf20Sopenharmony_ci{ 11158c2ecf20Sopenharmony_ci struct advk_pcie *pcie = bus->sysdata; 11168c2ecf20Sopenharmony_ci u32 reg; 11178c2ecf20Sopenharmony_ci u32 data_strobe = 0x0; 11188c2ecf20Sopenharmony_ci int retry_count; 11198c2ecf20Sopenharmony_ci int offset; 11208c2ecf20Sopenharmony_ci int ret; 11218c2ecf20Sopenharmony_ci 11228c2ecf20Sopenharmony_ci if (!advk_pcie_valid_device(pcie, bus, devfn)) 11238c2ecf20Sopenharmony_ci return PCIBIOS_DEVICE_NOT_FOUND; 11248c2ecf20Sopenharmony_ci 11258c2ecf20Sopenharmony_ci if (pci_is_root_bus(bus)) 11268c2ecf20Sopenharmony_ci return pci_bridge_emul_conf_write(&pcie->bridge, where, 11278c2ecf20Sopenharmony_ci size, val); 11288c2ecf20Sopenharmony_ci 11298c2ecf20Sopenharmony_ci if (where % size) 11308c2ecf20Sopenharmony_ci return PCIBIOS_SET_FAILED; 11318c2ecf20Sopenharmony_ci 11328c2ecf20Sopenharmony_ci if (advk_pcie_pio_is_running(pcie)) 11338c2ecf20Sopenharmony_ci return PCIBIOS_SET_FAILED; 11348c2ecf20Sopenharmony_ci 11358c2ecf20Sopenharmony_ci /* Program the control register */ 11368c2ecf20Sopenharmony_ci reg = advk_readl(pcie, PIO_CTRL); 11378c2ecf20Sopenharmony_ci reg &= ~PIO_CTRL_TYPE_MASK; 11388c2ecf20Sopenharmony_ci if (pci_is_root_bus(bus->parent)) 11398c2ecf20Sopenharmony_ci reg |= PCIE_CONFIG_WR_TYPE0; 11408c2ecf20Sopenharmony_ci else 11418c2ecf20Sopenharmony_ci reg |= PCIE_CONFIG_WR_TYPE1; 11428c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PIO_CTRL); 11438c2ecf20Sopenharmony_ci 11448c2ecf20Sopenharmony_ci /* Program the address registers */ 11458c2ecf20Sopenharmony_ci reg = PCIE_CONF_ADDR(bus->number, devfn, where); 11468c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PIO_ADDR_LS); 11478c2ecf20Sopenharmony_ci advk_writel(pcie, 0, PIO_ADDR_MS); 11488c2ecf20Sopenharmony_ci 11498c2ecf20Sopenharmony_ci /* Calculate the write strobe */ 11508c2ecf20Sopenharmony_ci offset = where & 0x3; 11518c2ecf20Sopenharmony_ci reg = val << (8 * offset); 11528c2ecf20Sopenharmony_ci data_strobe = GENMASK(size - 1, 0) << offset; 11538c2ecf20Sopenharmony_ci 11548c2ecf20Sopenharmony_ci /* Program the data register */ 11558c2ecf20Sopenharmony_ci advk_writel(pcie, reg, PIO_WR_DATA); 11568c2ecf20Sopenharmony_ci 11578c2ecf20Sopenharmony_ci /* Program the data strobe */ 11588c2ecf20Sopenharmony_ci advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB); 11598c2ecf20Sopenharmony_ci 11608c2ecf20Sopenharmony_ci retry_count = 0; 11618c2ecf20Sopenharmony_ci do { 11628c2ecf20Sopenharmony_ci /* Clear PIO DONE ISR and start the transfer */ 11638c2ecf20Sopenharmony_ci advk_writel(pcie, 1, PIO_ISR); 11648c2ecf20Sopenharmony_ci advk_writel(pcie, 1, PIO_START); 11658c2ecf20Sopenharmony_ci 11668c2ecf20Sopenharmony_ci ret = advk_pcie_wait_pio(pcie); 11678c2ecf20Sopenharmony_ci if (ret < 0) 11688c2ecf20Sopenharmony_ci return PCIBIOS_SET_FAILED; 11698c2ecf20Sopenharmony_ci 11708c2ecf20Sopenharmony_ci retry_count += ret; 11718c2ecf20Sopenharmony_ci 11728c2ecf20Sopenharmony_ci ret = advk_pcie_check_pio_status(pcie, false, NULL); 11738c2ecf20Sopenharmony_ci } while (ret == -EAGAIN && retry_count < PIO_RETRY_CNT); 11748c2ecf20Sopenharmony_ci 11758c2ecf20Sopenharmony_ci return ret < 0 ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL; 11768c2ecf20Sopenharmony_ci} 11778c2ecf20Sopenharmony_ci 11788c2ecf20Sopenharmony_cistatic struct pci_ops advk_pcie_ops = { 11798c2ecf20Sopenharmony_ci .read = advk_pcie_rd_conf, 11808c2ecf20Sopenharmony_ci .write = advk_pcie_wr_conf, 11818c2ecf20Sopenharmony_ci}; 11828c2ecf20Sopenharmony_ci 11838c2ecf20Sopenharmony_cistatic void advk_msi_irq_compose_msi_msg(struct irq_data *data, 11848c2ecf20Sopenharmony_ci struct msi_msg *msg) 11858c2ecf20Sopenharmony_ci{ 11868c2ecf20Sopenharmony_ci struct advk_pcie *pcie = irq_data_get_irq_chip_data(data); 11878c2ecf20Sopenharmony_ci phys_addr_t msi_msg = virt_to_phys(&pcie->msi_msg); 11888c2ecf20Sopenharmony_ci 11898c2ecf20Sopenharmony_ci msg->address_lo = lower_32_bits(msi_msg); 11908c2ecf20Sopenharmony_ci msg->address_hi = upper_32_bits(msi_msg); 11918c2ecf20Sopenharmony_ci msg->data = data->hwirq; 11928c2ecf20Sopenharmony_ci} 11938c2ecf20Sopenharmony_ci 11948c2ecf20Sopenharmony_cistatic int advk_msi_set_affinity(struct irq_data *irq_data, 11958c2ecf20Sopenharmony_ci const struct cpumask *mask, bool force) 11968c2ecf20Sopenharmony_ci{ 11978c2ecf20Sopenharmony_ci return -EINVAL; 11988c2ecf20Sopenharmony_ci} 11998c2ecf20Sopenharmony_ci 12008c2ecf20Sopenharmony_cistatic int advk_msi_irq_domain_alloc(struct irq_domain *domain, 12018c2ecf20Sopenharmony_ci unsigned int virq, 12028c2ecf20Sopenharmony_ci unsigned int nr_irqs, void *args) 12038c2ecf20Sopenharmony_ci{ 12048c2ecf20Sopenharmony_ci struct advk_pcie *pcie = domain->host_data; 12058c2ecf20Sopenharmony_ci int hwirq, i; 12068c2ecf20Sopenharmony_ci 12078c2ecf20Sopenharmony_ci mutex_lock(&pcie->msi_used_lock); 12088c2ecf20Sopenharmony_ci hwirq = bitmap_find_free_region(pcie->msi_used, MSI_IRQ_NUM, 12098c2ecf20Sopenharmony_ci order_base_2(nr_irqs)); 12108c2ecf20Sopenharmony_ci mutex_unlock(&pcie->msi_used_lock); 12118c2ecf20Sopenharmony_ci if (hwirq < 0) 12128c2ecf20Sopenharmony_ci return -ENOSPC; 12138c2ecf20Sopenharmony_ci 12148c2ecf20Sopenharmony_ci for (i = 0; i < nr_irqs; i++) 12158c2ecf20Sopenharmony_ci irq_domain_set_info(domain, virq + i, hwirq + i, 12168c2ecf20Sopenharmony_ci &pcie->msi_bottom_irq_chip, 12178c2ecf20Sopenharmony_ci domain->host_data, handle_simple_irq, 12188c2ecf20Sopenharmony_ci NULL, NULL); 12198c2ecf20Sopenharmony_ci 12208c2ecf20Sopenharmony_ci return 0; 12218c2ecf20Sopenharmony_ci} 12228c2ecf20Sopenharmony_ci 12238c2ecf20Sopenharmony_cistatic void advk_msi_irq_domain_free(struct irq_domain *domain, 12248c2ecf20Sopenharmony_ci unsigned int virq, unsigned int nr_irqs) 12258c2ecf20Sopenharmony_ci{ 12268c2ecf20Sopenharmony_ci struct irq_data *d = irq_domain_get_irq_data(domain, virq); 12278c2ecf20Sopenharmony_ci struct advk_pcie *pcie = domain->host_data; 12288c2ecf20Sopenharmony_ci 12298c2ecf20Sopenharmony_ci mutex_lock(&pcie->msi_used_lock); 12308c2ecf20Sopenharmony_ci bitmap_release_region(pcie->msi_used, d->hwirq, order_base_2(nr_irqs)); 12318c2ecf20Sopenharmony_ci mutex_unlock(&pcie->msi_used_lock); 12328c2ecf20Sopenharmony_ci} 12338c2ecf20Sopenharmony_ci 12348c2ecf20Sopenharmony_cistatic const struct irq_domain_ops advk_msi_domain_ops = { 12358c2ecf20Sopenharmony_ci .alloc = advk_msi_irq_domain_alloc, 12368c2ecf20Sopenharmony_ci .free = advk_msi_irq_domain_free, 12378c2ecf20Sopenharmony_ci}; 12388c2ecf20Sopenharmony_ci 12398c2ecf20Sopenharmony_cistatic void advk_pcie_irq_mask(struct irq_data *d) 12408c2ecf20Sopenharmony_ci{ 12418c2ecf20Sopenharmony_ci struct advk_pcie *pcie = d->domain->host_data; 12428c2ecf20Sopenharmony_ci irq_hw_number_t hwirq = irqd_to_hwirq(d); 12438c2ecf20Sopenharmony_ci unsigned long flags; 12448c2ecf20Sopenharmony_ci u32 mask; 12458c2ecf20Sopenharmony_ci 12468c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&pcie->irq_lock, flags); 12478c2ecf20Sopenharmony_ci mask = advk_readl(pcie, PCIE_ISR1_MASK_REG); 12488c2ecf20Sopenharmony_ci mask |= PCIE_ISR1_INTX_ASSERT(hwirq); 12498c2ecf20Sopenharmony_ci advk_writel(pcie, mask, PCIE_ISR1_MASK_REG); 12508c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&pcie->irq_lock, flags); 12518c2ecf20Sopenharmony_ci} 12528c2ecf20Sopenharmony_ci 12538c2ecf20Sopenharmony_cistatic void advk_pcie_irq_unmask(struct irq_data *d) 12548c2ecf20Sopenharmony_ci{ 12558c2ecf20Sopenharmony_ci struct advk_pcie *pcie = d->domain->host_data; 12568c2ecf20Sopenharmony_ci irq_hw_number_t hwirq = irqd_to_hwirq(d); 12578c2ecf20Sopenharmony_ci unsigned long flags; 12588c2ecf20Sopenharmony_ci u32 mask; 12598c2ecf20Sopenharmony_ci 12608c2ecf20Sopenharmony_ci raw_spin_lock_irqsave(&pcie->irq_lock, flags); 12618c2ecf20Sopenharmony_ci mask = advk_readl(pcie, PCIE_ISR1_MASK_REG); 12628c2ecf20Sopenharmony_ci mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq); 12638c2ecf20Sopenharmony_ci advk_writel(pcie, mask, PCIE_ISR1_MASK_REG); 12648c2ecf20Sopenharmony_ci raw_spin_unlock_irqrestore(&pcie->irq_lock, flags); 12658c2ecf20Sopenharmony_ci} 12668c2ecf20Sopenharmony_ci 12678c2ecf20Sopenharmony_cistatic int advk_pcie_irq_map(struct irq_domain *h, 12688c2ecf20Sopenharmony_ci unsigned int virq, irq_hw_number_t hwirq) 12698c2ecf20Sopenharmony_ci{ 12708c2ecf20Sopenharmony_ci struct advk_pcie *pcie = h->host_data; 12718c2ecf20Sopenharmony_ci 12728c2ecf20Sopenharmony_ci advk_pcie_irq_mask(irq_get_irq_data(virq)); 12738c2ecf20Sopenharmony_ci irq_set_status_flags(virq, IRQ_LEVEL); 12748c2ecf20Sopenharmony_ci irq_set_chip_and_handler(virq, &pcie->irq_chip, 12758c2ecf20Sopenharmony_ci handle_level_irq); 12768c2ecf20Sopenharmony_ci irq_set_chip_data(virq, pcie); 12778c2ecf20Sopenharmony_ci 12788c2ecf20Sopenharmony_ci return 0; 12798c2ecf20Sopenharmony_ci} 12808c2ecf20Sopenharmony_ci 12818c2ecf20Sopenharmony_cistatic const struct irq_domain_ops advk_pcie_irq_domain_ops = { 12828c2ecf20Sopenharmony_ci .map = advk_pcie_irq_map, 12838c2ecf20Sopenharmony_ci .xlate = irq_domain_xlate_onecell, 12848c2ecf20Sopenharmony_ci}; 12858c2ecf20Sopenharmony_ci 12868c2ecf20Sopenharmony_cistatic int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie) 12878c2ecf20Sopenharmony_ci{ 12888c2ecf20Sopenharmony_ci struct device *dev = &pcie->pdev->dev; 12898c2ecf20Sopenharmony_ci struct device_node *node = dev->of_node; 12908c2ecf20Sopenharmony_ci struct irq_chip *bottom_ic, *msi_ic; 12918c2ecf20Sopenharmony_ci struct msi_domain_info *msi_di; 12928c2ecf20Sopenharmony_ci phys_addr_t msi_msg_phys; 12938c2ecf20Sopenharmony_ci 12948c2ecf20Sopenharmony_ci mutex_init(&pcie->msi_used_lock); 12958c2ecf20Sopenharmony_ci 12968c2ecf20Sopenharmony_ci bottom_ic = &pcie->msi_bottom_irq_chip; 12978c2ecf20Sopenharmony_ci 12988c2ecf20Sopenharmony_ci bottom_ic->name = "MSI"; 12998c2ecf20Sopenharmony_ci bottom_ic->irq_compose_msi_msg = advk_msi_irq_compose_msi_msg; 13008c2ecf20Sopenharmony_ci bottom_ic->irq_set_affinity = advk_msi_set_affinity; 13018c2ecf20Sopenharmony_ci 13028c2ecf20Sopenharmony_ci msi_ic = &pcie->msi_irq_chip; 13038c2ecf20Sopenharmony_ci msi_ic->name = "advk-MSI"; 13048c2ecf20Sopenharmony_ci 13058c2ecf20Sopenharmony_ci msi_di = &pcie->msi_domain_info; 13068c2ecf20Sopenharmony_ci msi_di->flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS | 13078c2ecf20Sopenharmony_ci MSI_FLAG_MULTI_PCI_MSI; 13088c2ecf20Sopenharmony_ci msi_di->chip = msi_ic; 13098c2ecf20Sopenharmony_ci 13108c2ecf20Sopenharmony_ci msi_msg_phys = virt_to_phys(&pcie->msi_msg); 13118c2ecf20Sopenharmony_ci 13128c2ecf20Sopenharmony_ci advk_writel(pcie, lower_32_bits(msi_msg_phys), 13138c2ecf20Sopenharmony_ci PCIE_MSI_ADDR_LOW_REG); 13148c2ecf20Sopenharmony_ci advk_writel(pcie, upper_32_bits(msi_msg_phys), 13158c2ecf20Sopenharmony_ci PCIE_MSI_ADDR_HIGH_REG); 13168c2ecf20Sopenharmony_ci 13178c2ecf20Sopenharmony_ci pcie->msi_inner_domain = 13188c2ecf20Sopenharmony_ci irq_domain_add_linear(NULL, MSI_IRQ_NUM, 13198c2ecf20Sopenharmony_ci &advk_msi_domain_ops, pcie); 13208c2ecf20Sopenharmony_ci if (!pcie->msi_inner_domain) 13218c2ecf20Sopenharmony_ci return -ENOMEM; 13228c2ecf20Sopenharmony_ci 13238c2ecf20Sopenharmony_ci pcie->msi_domain = 13248c2ecf20Sopenharmony_ci pci_msi_create_irq_domain(of_node_to_fwnode(node), 13258c2ecf20Sopenharmony_ci msi_di, pcie->msi_inner_domain); 13268c2ecf20Sopenharmony_ci if (!pcie->msi_domain) { 13278c2ecf20Sopenharmony_ci irq_domain_remove(pcie->msi_inner_domain); 13288c2ecf20Sopenharmony_ci return -ENOMEM; 13298c2ecf20Sopenharmony_ci } 13308c2ecf20Sopenharmony_ci 13318c2ecf20Sopenharmony_ci return 0; 13328c2ecf20Sopenharmony_ci} 13338c2ecf20Sopenharmony_ci 13348c2ecf20Sopenharmony_cistatic void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie) 13358c2ecf20Sopenharmony_ci{ 13368c2ecf20Sopenharmony_ci irq_domain_remove(pcie->msi_domain); 13378c2ecf20Sopenharmony_ci irq_domain_remove(pcie->msi_inner_domain); 13388c2ecf20Sopenharmony_ci} 13398c2ecf20Sopenharmony_ci 13408c2ecf20Sopenharmony_cistatic int advk_pcie_init_irq_domain(struct advk_pcie *pcie) 13418c2ecf20Sopenharmony_ci{ 13428c2ecf20Sopenharmony_ci struct device *dev = &pcie->pdev->dev; 13438c2ecf20Sopenharmony_ci struct device_node *node = dev->of_node; 13448c2ecf20Sopenharmony_ci struct device_node *pcie_intc_node; 13458c2ecf20Sopenharmony_ci struct irq_chip *irq_chip; 13468c2ecf20Sopenharmony_ci int ret = 0; 13478c2ecf20Sopenharmony_ci 13488c2ecf20Sopenharmony_ci raw_spin_lock_init(&pcie->irq_lock); 13498c2ecf20Sopenharmony_ci 13508c2ecf20Sopenharmony_ci pcie_intc_node = of_get_next_child(node, NULL); 13518c2ecf20Sopenharmony_ci if (!pcie_intc_node) { 13528c2ecf20Sopenharmony_ci dev_err(dev, "No PCIe Intc node found\n"); 13538c2ecf20Sopenharmony_ci return -ENODEV; 13548c2ecf20Sopenharmony_ci } 13558c2ecf20Sopenharmony_ci 13568c2ecf20Sopenharmony_ci irq_chip = &pcie->irq_chip; 13578c2ecf20Sopenharmony_ci 13588c2ecf20Sopenharmony_ci irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq", 13598c2ecf20Sopenharmony_ci dev_name(dev)); 13608c2ecf20Sopenharmony_ci if (!irq_chip->name) { 13618c2ecf20Sopenharmony_ci ret = -ENOMEM; 13628c2ecf20Sopenharmony_ci goto out_put_node; 13638c2ecf20Sopenharmony_ci } 13648c2ecf20Sopenharmony_ci 13658c2ecf20Sopenharmony_ci irq_chip->irq_mask = advk_pcie_irq_mask; 13668c2ecf20Sopenharmony_ci irq_chip->irq_mask_ack = advk_pcie_irq_mask; 13678c2ecf20Sopenharmony_ci irq_chip->irq_unmask = advk_pcie_irq_unmask; 13688c2ecf20Sopenharmony_ci 13698c2ecf20Sopenharmony_ci pcie->irq_domain = 13708c2ecf20Sopenharmony_ci irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX, 13718c2ecf20Sopenharmony_ci &advk_pcie_irq_domain_ops, pcie); 13728c2ecf20Sopenharmony_ci if (!pcie->irq_domain) { 13738c2ecf20Sopenharmony_ci dev_err(dev, "Failed to get a INTx IRQ domain\n"); 13748c2ecf20Sopenharmony_ci ret = -ENOMEM; 13758c2ecf20Sopenharmony_ci goto out_put_node; 13768c2ecf20Sopenharmony_ci } 13778c2ecf20Sopenharmony_ci 13788c2ecf20Sopenharmony_ciout_put_node: 13798c2ecf20Sopenharmony_ci of_node_put(pcie_intc_node); 13808c2ecf20Sopenharmony_ci return ret; 13818c2ecf20Sopenharmony_ci} 13828c2ecf20Sopenharmony_ci 13838c2ecf20Sopenharmony_cistatic void advk_pcie_remove_irq_domain(struct advk_pcie *pcie) 13848c2ecf20Sopenharmony_ci{ 13858c2ecf20Sopenharmony_ci irq_domain_remove(pcie->irq_domain); 13868c2ecf20Sopenharmony_ci} 13878c2ecf20Sopenharmony_ci 13888c2ecf20Sopenharmony_cistatic void advk_pcie_handle_msi(struct advk_pcie *pcie) 13898c2ecf20Sopenharmony_ci{ 13908c2ecf20Sopenharmony_ci u32 msi_val, msi_mask, msi_status, msi_idx; 13918c2ecf20Sopenharmony_ci int virq; 13928c2ecf20Sopenharmony_ci 13938c2ecf20Sopenharmony_ci msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG); 13948c2ecf20Sopenharmony_ci msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG); 13958c2ecf20Sopenharmony_ci msi_status = msi_val & ((~msi_mask) & PCIE_MSI_ALL_MASK); 13968c2ecf20Sopenharmony_ci 13978c2ecf20Sopenharmony_ci for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) { 13988c2ecf20Sopenharmony_ci if (!(BIT(msi_idx) & msi_status)) 13998c2ecf20Sopenharmony_ci continue; 14008c2ecf20Sopenharmony_ci 14018c2ecf20Sopenharmony_ci advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG); 14028c2ecf20Sopenharmony_ci virq = irq_find_mapping(pcie->msi_inner_domain, msi_idx); 14038c2ecf20Sopenharmony_ci generic_handle_irq(virq); 14048c2ecf20Sopenharmony_ci } 14058c2ecf20Sopenharmony_ci 14068c2ecf20Sopenharmony_ci advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING, 14078c2ecf20Sopenharmony_ci PCIE_ISR0_REG); 14088c2ecf20Sopenharmony_ci} 14098c2ecf20Sopenharmony_ci 14108c2ecf20Sopenharmony_cistatic void advk_pcie_handle_int(struct advk_pcie *pcie) 14118c2ecf20Sopenharmony_ci{ 14128c2ecf20Sopenharmony_ci u32 isr0_val, isr0_mask, isr0_status; 14138c2ecf20Sopenharmony_ci u32 isr1_val, isr1_mask, isr1_status; 14148c2ecf20Sopenharmony_ci int i, virq; 14158c2ecf20Sopenharmony_ci 14168c2ecf20Sopenharmony_ci isr0_val = advk_readl(pcie, PCIE_ISR0_REG); 14178c2ecf20Sopenharmony_ci isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG); 14188c2ecf20Sopenharmony_ci isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK); 14198c2ecf20Sopenharmony_ci 14208c2ecf20Sopenharmony_ci isr1_val = advk_readl(pcie, PCIE_ISR1_REG); 14218c2ecf20Sopenharmony_ci isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG); 14228c2ecf20Sopenharmony_ci isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK); 14238c2ecf20Sopenharmony_ci 14248c2ecf20Sopenharmony_ci /* Process MSI interrupts */ 14258c2ecf20Sopenharmony_ci if (isr0_status & PCIE_ISR0_MSI_INT_PENDING) 14268c2ecf20Sopenharmony_ci advk_pcie_handle_msi(pcie); 14278c2ecf20Sopenharmony_ci 14288c2ecf20Sopenharmony_ci /* Process legacy interrupts */ 14298c2ecf20Sopenharmony_ci for (i = 0; i < PCI_NUM_INTX; i++) { 14308c2ecf20Sopenharmony_ci if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i))) 14318c2ecf20Sopenharmony_ci continue; 14328c2ecf20Sopenharmony_ci 14338c2ecf20Sopenharmony_ci advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i), 14348c2ecf20Sopenharmony_ci PCIE_ISR1_REG); 14358c2ecf20Sopenharmony_ci 14368c2ecf20Sopenharmony_ci virq = irq_find_mapping(pcie->irq_domain, i); 14378c2ecf20Sopenharmony_ci generic_handle_irq(virq); 14388c2ecf20Sopenharmony_ci } 14398c2ecf20Sopenharmony_ci} 14408c2ecf20Sopenharmony_ci 14418c2ecf20Sopenharmony_cistatic irqreturn_t advk_pcie_irq_handler(int irq, void *arg) 14428c2ecf20Sopenharmony_ci{ 14438c2ecf20Sopenharmony_ci struct advk_pcie *pcie = arg; 14448c2ecf20Sopenharmony_ci u32 status; 14458c2ecf20Sopenharmony_ci 14468c2ecf20Sopenharmony_ci status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG); 14478c2ecf20Sopenharmony_ci if (!(status & PCIE_IRQ_CORE_INT)) 14488c2ecf20Sopenharmony_ci return IRQ_NONE; 14498c2ecf20Sopenharmony_ci 14508c2ecf20Sopenharmony_ci advk_pcie_handle_int(pcie); 14518c2ecf20Sopenharmony_ci 14528c2ecf20Sopenharmony_ci /* Clear interrupt */ 14538c2ecf20Sopenharmony_ci advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG); 14548c2ecf20Sopenharmony_ci 14558c2ecf20Sopenharmony_ci return IRQ_HANDLED; 14568c2ecf20Sopenharmony_ci} 14578c2ecf20Sopenharmony_ci 14588c2ecf20Sopenharmony_cistatic void __maybe_unused advk_pcie_disable_phy(struct advk_pcie *pcie) 14598c2ecf20Sopenharmony_ci{ 14608c2ecf20Sopenharmony_ci phy_power_off(pcie->phy); 14618c2ecf20Sopenharmony_ci phy_exit(pcie->phy); 14628c2ecf20Sopenharmony_ci} 14638c2ecf20Sopenharmony_ci 14648c2ecf20Sopenharmony_cistatic int advk_pcie_enable_phy(struct advk_pcie *pcie) 14658c2ecf20Sopenharmony_ci{ 14668c2ecf20Sopenharmony_ci int ret; 14678c2ecf20Sopenharmony_ci 14688c2ecf20Sopenharmony_ci if (!pcie->phy) 14698c2ecf20Sopenharmony_ci return 0; 14708c2ecf20Sopenharmony_ci 14718c2ecf20Sopenharmony_ci ret = phy_init(pcie->phy); 14728c2ecf20Sopenharmony_ci if (ret) 14738c2ecf20Sopenharmony_ci return ret; 14748c2ecf20Sopenharmony_ci 14758c2ecf20Sopenharmony_ci ret = phy_set_mode(pcie->phy, PHY_MODE_PCIE); 14768c2ecf20Sopenharmony_ci if (ret) { 14778c2ecf20Sopenharmony_ci phy_exit(pcie->phy); 14788c2ecf20Sopenharmony_ci return ret; 14798c2ecf20Sopenharmony_ci } 14808c2ecf20Sopenharmony_ci 14818c2ecf20Sopenharmony_ci ret = phy_power_on(pcie->phy); 14828c2ecf20Sopenharmony_ci if (ret == -EOPNOTSUPP) { 14838c2ecf20Sopenharmony_ci dev_warn(&pcie->pdev->dev, "PHY unsupported by firmware\n"); 14848c2ecf20Sopenharmony_ci } else if (ret) { 14858c2ecf20Sopenharmony_ci phy_exit(pcie->phy); 14868c2ecf20Sopenharmony_ci return ret; 14878c2ecf20Sopenharmony_ci } 14888c2ecf20Sopenharmony_ci 14898c2ecf20Sopenharmony_ci return 0; 14908c2ecf20Sopenharmony_ci} 14918c2ecf20Sopenharmony_ci 14928c2ecf20Sopenharmony_cistatic int advk_pcie_setup_phy(struct advk_pcie *pcie) 14938c2ecf20Sopenharmony_ci{ 14948c2ecf20Sopenharmony_ci struct device *dev = &pcie->pdev->dev; 14958c2ecf20Sopenharmony_ci struct device_node *node = dev->of_node; 14968c2ecf20Sopenharmony_ci int ret = 0; 14978c2ecf20Sopenharmony_ci 14988c2ecf20Sopenharmony_ci pcie->phy = devm_of_phy_get(dev, node, NULL); 14998c2ecf20Sopenharmony_ci if (IS_ERR(pcie->phy) && (PTR_ERR(pcie->phy) == -EPROBE_DEFER)) 15008c2ecf20Sopenharmony_ci return PTR_ERR(pcie->phy); 15018c2ecf20Sopenharmony_ci 15028c2ecf20Sopenharmony_ci /* Old bindings miss the PHY handle */ 15038c2ecf20Sopenharmony_ci if (IS_ERR(pcie->phy)) { 15048c2ecf20Sopenharmony_ci dev_warn(dev, "PHY unavailable (%ld)\n", PTR_ERR(pcie->phy)); 15058c2ecf20Sopenharmony_ci pcie->phy = NULL; 15068c2ecf20Sopenharmony_ci return 0; 15078c2ecf20Sopenharmony_ci } 15088c2ecf20Sopenharmony_ci 15098c2ecf20Sopenharmony_ci ret = advk_pcie_enable_phy(pcie); 15108c2ecf20Sopenharmony_ci if (ret) 15118c2ecf20Sopenharmony_ci dev_err(dev, "Failed to initialize PHY (%d)\n", ret); 15128c2ecf20Sopenharmony_ci 15138c2ecf20Sopenharmony_ci return ret; 15148c2ecf20Sopenharmony_ci} 15158c2ecf20Sopenharmony_ci 15168c2ecf20Sopenharmony_cistatic int advk_pcie_probe(struct platform_device *pdev) 15178c2ecf20Sopenharmony_ci{ 15188c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 15198c2ecf20Sopenharmony_ci struct advk_pcie *pcie; 15208c2ecf20Sopenharmony_ci struct pci_host_bridge *bridge; 15218c2ecf20Sopenharmony_ci struct resource_entry *entry; 15228c2ecf20Sopenharmony_ci int ret, irq; 15238c2ecf20Sopenharmony_ci 15248c2ecf20Sopenharmony_ci bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie)); 15258c2ecf20Sopenharmony_ci if (!bridge) 15268c2ecf20Sopenharmony_ci return -ENOMEM; 15278c2ecf20Sopenharmony_ci 15288c2ecf20Sopenharmony_ci pcie = pci_host_bridge_priv(bridge); 15298c2ecf20Sopenharmony_ci pcie->pdev = pdev; 15308c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, pcie); 15318c2ecf20Sopenharmony_ci 15328c2ecf20Sopenharmony_ci resource_list_for_each_entry(entry, &bridge->windows) { 15338c2ecf20Sopenharmony_ci resource_size_t start = entry->res->start; 15348c2ecf20Sopenharmony_ci resource_size_t size = resource_size(entry->res); 15358c2ecf20Sopenharmony_ci unsigned long type = resource_type(entry->res); 15368c2ecf20Sopenharmony_ci u64 win_size; 15378c2ecf20Sopenharmony_ci 15388c2ecf20Sopenharmony_ci /* 15398c2ecf20Sopenharmony_ci * Aardvark hardware allows to configure also PCIe window 15408c2ecf20Sopenharmony_ci * for config type 0 and type 1 mapping, but driver uses 15418c2ecf20Sopenharmony_ci * only PIO for issuing configuration transfers which does 15428c2ecf20Sopenharmony_ci * not use PCIe window configuration. 15438c2ecf20Sopenharmony_ci */ 15448c2ecf20Sopenharmony_ci if (type != IORESOURCE_MEM && type != IORESOURCE_MEM_64 && 15458c2ecf20Sopenharmony_ci type != IORESOURCE_IO) 15468c2ecf20Sopenharmony_ci continue; 15478c2ecf20Sopenharmony_ci 15488c2ecf20Sopenharmony_ci /* 15498c2ecf20Sopenharmony_ci * Skip transparent memory resources. Default outbound access 15508c2ecf20Sopenharmony_ci * configuration is set to transparent memory access so it 15518c2ecf20Sopenharmony_ci * does not need window configuration. 15528c2ecf20Sopenharmony_ci */ 15538c2ecf20Sopenharmony_ci if ((type == IORESOURCE_MEM || type == IORESOURCE_MEM_64) && 15548c2ecf20Sopenharmony_ci entry->offset == 0) 15558c2ecf20Sopenharmony_ci continue; 15568c2ecf20Sopenharmony_ci 15578c2ecf20Sopenharmony_ci /* 15588c2ecf20Sopenharmony_ci * The n-th PCIe window is configured by tuple (match, remap, mask) 15598c2ecf20Sopenharmony_ci * and an access to address A uses this window if A matches the 15608c2ecf20Sopenharmony_ci * match with given mask. 15618c2ecf20Sopenharmony_ci * So every PCIe window size must be a power of two and every start 15628c2ecf20Sopenharmony_ci * address must be aligned to window size. Minimal size is 64 KiB 15638c2ecf20Sopenharmony_ci * because lower 16 bits of mask must be zero. Remapped address 15648c2ecf20Sopenharmony_ci * may have set only bits from the mask. 15658c2ecf20Sopenharmony_ci */ 15668c2ecf20Sopenharmony_ci while (pcie->wins_count < OB_WIN_COUNT && size > 0) { 15678c2ecf20Sopenharmony_ci /* Calculate the largest aligned window size */ 15688c2ecf20Sopenharmony_ci win_size = (1ULL << (fls64(size)-1)) | 15698c2ecf20Sopenharmony_ci (start ? (1ULL << __ffs64(start)) : 0); 15708c2ecf20Sopenharmony_ci win_size = 1ULL << __ffs64(win_size); 15718c2ecf20Sopenharmony_ci if (win_size < 0x10000) 15728c2ecf20Sopenharmony_ci break; 15738c2ecf20Sopenharmony_ci 15748c2ecf20Sopenharmony_ci dev_dbg(dev, 15758c2ecf20Sopenharmony_ci "Configuring PCIe window %d: [0x%llx-0x%llx] as %lu\n", 15768c2ecf20Sopenharmony_ci pcie->wins_count, (unsigned long long)start, 15778c2ecf20Sopenharmony_ci (unsigned long long)start + win_size, type); 15788c2ecf20Sopenharmony_ci 15798c2ecf20Sopenharmony_ci if (type == IORESOURCE_IO) { 15808c2ecf20Sopenharmony_ci pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_IO; 15818c2ecf20Sopenharmony_ci pcie->wins[pcie->wins_count].match = pci_pio_to_address(start); 15828c2ecf20Sopenharmony_ci } else { 15838c2ecf20Sopenharmony_ci pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_MEM; 15848c2ecf20Sopenharmony_ci pcie->wins[pcie->wins_count].match = start; 15858c2ecf20Sopenharmony_ci } 15868c2ecf20Sopenharmony_ci pcie->wins[pcie->wins_count].remap = start - entry->offset; 15878c2ecf20Sopenharmony_ci pcie->wins[pcie->wins_count].mask = ~(win_size - 1); 15888c2ecf20Sopenharmony_ci 15898c2ecf20Sopenharmony_ci if (pcie->wins[pcie->wins_count].remap & (win_size - 1)) 15908c2ecf20Sopenharmony_ci break; 15918c2ecf20Sopenharmony_ci 15928c2ecf20Sopenharmony_ci start += win_size; 15938c2ecf20Sopenharmony_ci size -= win_size; 15948c2ecf20Sopenharmony_ci pcie->wins_count++; 15958c2ecf20Sopenharmony_ci } 15968c2ecf20Sopenharmony_ci 15978c2ecf20Sopenharmony_ci if (size > 0) { 15988c2ecf20Sopenharmony_ci dev_err(&pcie->pdev->dev, 15998c2ecf20Sopenharmony_ci "Invalid PCIe region [0x%llx-0x%llx]\n", 16008c2ecf20Sopenharmony_ci (unsigned long long)entry->res->start, 16018c2ecf20Sopenharmony_ci (unsigned long long)entry->res->end + 1); 16028c2ecf20Sopenharmony_ci return -EINVAL; 16038c2ecf20Sopenharmony_ci } 16048c2ecf20Sopenharmony_ci } 16058c2ecf20Sopenharmony_ci 16068c2ecf20Sopenharmony_ci pcie->base = devm_platform_ioremap_resource(pdev, 0); 16078c2ecf20Sopenharmony_ci if (IS_ERR(pcie->base)) 16088c2ecf20Sopenharmony_ci return PTR_ERR(pcie->base); 16098c2ecf20Sopenharmony_ci 16108c2ecf20Sopenharmony_ci irq = platform_get_irq(pdev, 0); 16118c2ecf20Sopenharmony_ci if (irq < 0) 16128c2ecf20Sopenharmony_ci return irq; 16138c2ecf20Sopenharmony_ci 16148c2ecf20Sopenharmony_ci ret = devm_request_irq(dev, irq, advk_pcie_irq_handler, 16158c2ecf20Sopenharmony_ci IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie", 16168c2ecf20Sopenharmony_ci pcie); 16178c2ecf20Sopenharmony_ci if (ret) { 16188c2ecf20Sopenharmony_ci dev_err(dev, "Failed to register interrupt\n"); 16198c2ecf20Sopenharmony_ci return ret; 16208c2ecf20Sopenharmony_ci } 16218c2ecf20Sopenharmony_ci 16228c2ecf20Sopenharmony_ci pcie->reset_gpio = devm_gpiod_get_from_of_node(dev, dev->of_node, 16238c2ecf20Sopenharmony_ci "reset-gpios", 0, 16248c2ecf20Sopenharmony_ci GPIOD_OUT_LOW, 16258c2ecf20Sopenharmony_ci "pcie1-reset"); 16268c2ecf20Sopenharmony_ci ret = PTR_ERR_OR_ZERO(pcie->reset_gpio); 16278c2ecf20Sopenharmony_ci if (ret) { 16288c2ecf20Sopenharmony_ci if (ret == -ENOENT) { 16298c2ecf20Sopenharmony_ci pcie->reset_gpio = NULL; 16308c2ecf20Sopenharmony_ci } else { 16318c2ecf20Sopenharmony_ci if (ret != -EPROBE_DEFER) 16328c2ecf20Sopenharmony_ci dev_err(dev, "Failed to get reset-gpio: %i\n", 16338c2ecf20Sopenharmony_ci ret); 16348c2ecf20Sopenharmony_ci return ret; 16358c2ecf20Sopenharmony_ci } 16368c2ecf20Sopenharmony_ci } 16378c2ecf20Sopenharmony_ci 16388c2ecf20Sopenharmony_ci ret = of_pci_get_max_link_speed(dev->of_node); 16398c2ecf20Sopenharmony_ci if (ret <= 0 || ret > 3) 16408c2ecf20Sopenharmony_ci pcie->link_gen = 3; 16418c2ecf20Sopenharmony_ci else 16428c2ecf20Sopenharmony_ci pcie->link_gen = ret; 16438c2ecf20Sopenharmony_ci 16448c2ecf20Sopenharmony_ci ret = advk_pcie_setup_phy(pcie); 16458c2ecf20Sopenharmony_ci if (ret) 16468c2ecf20Sopenharmony_ci return ret; 16478c2ecf20Sopenharmony_ci 16488c2ecf20Sopenharmony_ci advk_pcie_setup_hw(pcie); 16498c2ecf20Sopenharmony_ci 16508c2ecf20Sopenharmony_ci ret = advk_sw_pci_bridge_init(pcie); 16518c2ecf20Sopenharmony_ci if (ret) { 16528c2ecf20Sopenharmony_ci dev_err(dev, "Failed to register emulated root PCI bridge\n"); 16538c2ecf20Sopenharmony_ci return ret; 16548c2ecf20Sopenharmony_ci } 16558c2ecf20Sopenharmony_ci 16568c2ecf20Sopenharmony_ci ret = advk_pcie_init_irq_domain(pcie); 16578c2ecf20Sopenharmony_ci if (ret) { 16588c2ecf20Sopenharmony_ci dev_err(dev, "Failed to initialize irq\n"); 16598c2ecf20Sopenharmony_ci return ret; 16608c2ecf20Sopenharmony_ci } 16618c2ecf20Sopenharmony_ci 16628c2ecf20Sopenharmony_ci ret = advk_pcie_init_msi_irq_domain(pcie); 16638c2ecf20Sopenharmony_ci if (ret) { 16648c2ecf20Sopenharmony_ci dev_err(dev, "Failed to initialize irq\n"); 16658c2ecf20Sopenharmony_ci advk_pcie_remove_irq_domain(pcie); 16668c2ecf20Sopenharmony_ci return ret; 16678c2ecf20Sopenharmony_ci } 16688c2ecf20Sopenharmony_ci 16698c2ecf20Sopenharmony_ci bridge->sysdata = pcie; 16708c2ecf20Sopenharmony_ci bridge->ops = &advk_pcie_ops; 16718c2ecf20Sopenharmony_ci 16728c2ecf20Sopenharmony_ci ret = pci_host_probe(bridge); 16738c2ecf20Sopenharmony_ci if (ret < 0) { 16748c2ecf20Sopenharmony_ci advk_pcie_remove_msi_irq_domain(pcie); 16758c2ecf20Sopenharmony_ci advk_pcie_remove_irq_domain(pcie); 16768c2ecf20Sopenharmony_ci return ret; 16778c2ecf20Sopenharmony_ci } 16788c2ecf20Sopenharmony_ci 16798c2ecf20Sopenharmony_ci return 0; 16808c2ecf20Sopenharmony_ci} 16818c2ecf20Sopenharmony_ci 16828c2ecf20Sopenharmony_cistatic int advk_pcie_remove(struct platform_device *pdev) 16838c2ecf20Sopenharmony_ci{ 16848c2ecf20Sopenharmony_ci struct advk_pcie *pcie = platform_get_drvdata(pdev); 16858c2ecf20Sopenharmony_ci struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); 16868c2ecf20Sopenharmony_ci int i; 16878c2ecf20Sopenharmony_ci 16888c2ecf20Sopenharmony_ci pci_lock_rescan_remove(); 16898c2ecf20Sopenharmony_ci pci_stop_root_bus(bridge->bus); 16908c2ecf20Sopenharmony_ci pci_remove_root_bus(bridge->bus); 16918c2ecf20Sopenharmony_ci pci_unlock_rescan_remove(); 16928c2ecf20Sopenharmony_ci 16938c2ecf20Sopenharmony_ci advk_pcie_remove_msi_irq_domain(pcie); 16948c2ecf20Sopenharmony_ci advk_pcie_remove_irq_domain(pcie); 16958c2ecf20Sopenharmony_ci 16968c2ecf20Sopenharmony_ci /* Disable outbound address windows mapping */ 16978c2ecf20Sopenharmony_ci for (i = 0; i < OB_WIN_COUNT; i++) 16988c2ecf20Sopenharmony_ci advk_pcie_disable_ob_win(pcie, i); 16998c2ecf20Sopenharmony_ci 17008c2ecf20Sopenharmony_ci return 0; 17018c2ecf20Sopenharmony_ci} 17028c2ecf20Sopenharmony_ci 17038c2ecf20Sopenharmony_cistatic const struct of_device_id advk_pcie_of_match_table[] = { 17048c2ecf20Sopenharmony_ci { .compatible = "marvell,armada-3700-pcie", }, 17058c2ecf20Sopenharmony_ci {}, 17068c2ecf20Sopenharmony_ci}; 17078c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, advk_pcie_of_match_table); 17088c2ecf20Sopenharmony_ci 17098c2ecf20Sopenharmony_cistatic struct platform_driver advk_pcie_driver = { 17108c2ecf20Sopenharmony_ci .driver = { 17118c2ecf20Sopenharmony_ci .name = "advk-pcie", 17128c2ecf20Sopenharmony_ci .of_match_table = advk_pcie_of_match_table, 17138c2ecf20Sopenharmony_ci }, 17148c2ecf20Sopenharmony_ci .probe = advk_pcie_probe, 17158c2ecf20Sopenharmony_ci .remove = advk_pcie_remove, 17168c2ecf20Sopenharmony_ci}; 17178c2ecf20Sopenharmony_cimodule_platform_driver(advk_pcie_driver); 17188c2ecf20Sopenharmony_ci 17198c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Aardvark PCIe controller"); 17208c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 1721