162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Driver for the Aardvark PCIe controller, used on Marvell Armada 462306a36Sopenharmony_ci * 3700. 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * Copyright (C) 2016 Marvell 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * Author: Hezi Shahmoon <hezi.shahmoon@marvell.com> 962306a36Sopenharmony_ci */ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#include <linux/bitfield.h> 1262306a36Sopenharmony_ci#include <linux/delay.h> 1362306a36Sopenharmony_ci#include <linux/gpio/consumer.h> 1462306a36Sopenharmony_ci#include <linux/interrupt.h> 1562306a36Sopenharmony_ci#include <linux/irq.h> 1662306a36Sopenharmony_ci#include <linux/irqdomain.h> 1762306a36Sopenharmony_ci#include <linux/kernel.h> 1862306a36Sopenharmony_ci#include <linux/module.h> 1962306a36Sopenharmony_ci#include <linux/pci.h> 2062306a36Sopenharmony_ci#include <linux/pci-ecam.h> 2162306a36Sopenharmony_ci#include <linux/init.h> 2262306a36Sopenharmony_ci#include <linux/phy/phy.h> 2362306a36Sopenharmony_ci#include <linux/platform_device.h> 2462306a36Sopenharmony_ci#include <linux/msi.h> 2562306a36Sopenharmony_ci#include <linux/of_address.h> 2662306a36Sopenharmony_ci#include <linux/of_gpio.h> 2762306a36Sopenharmony_ci#include <linux/of_pci.h> 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci#include "../pci.h" 3062306a36Sopenharmony_ci#include "../pci-bridge-emul.h" 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ci/* PCIe core registers */ 3362306a36Sopenharmony_ci#define PCIE_CORE_DEV_ID_REG 0x0 3462306a36Sopenharmony_ci#define PCIE_CORE_CMD_STATUS_REG 0x4 3562306a36Sopenharmony_ci#define PCIE_CORE_DEV_REV_REG 0x8 3662306a36Sopenharmony_ci#define PCIE_CORE_SSDEV_ID_REG 0x2c 3762306a36Sopenharmony_ci#define PCIE_CORE_PCIEXP_CAP 0xc0 3862306a36Sopenharmony_ci#define PCIE_CORE_PCIERR_CAP 0x100 3962306a36Sopenharmony_ci#define PCIE_CORE_ERR_CAPCTL_REG 0x118 4062306a36Sopenharmony_ci#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5) 4162306a36Sopenharmony_ci#define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6) 4262306a36Sopenharmony_ci#define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7) 4362306a36Sopenharmony_ci#define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8) 4462306a36Sopenharmony_ci/* PIO registers base address and register offsets */ 4562306a36Sopenharmony_ci#define PIO_BASE_ADDR 0x4000 4662306a36Sopenharmony_ci#define PIO_CTRL (PIO_BASE_ADDR + 0x0) 4762306a36Sopenharmony_ci#define PIO_CTRL_TYPE_MASK GENMASK(3, 0) 4862306a36Sopenharmony_ci#define PIO_CTRL_ADDR_WIN_DISABLE BIT(24) 4962306a36Sopenharmony_ci#define PIO_STAT (PIO_BASE_ADDR + 0x4) 5062306a36Sopenharmony_ci#define PIO_COMPLETION_STATUS_SHIFT 7 5162306a36Sopenharmony_ci#define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7) 5262306a36Sopenharmony_ci#define PIO_COMPLETION_STATUS_OK 0 5362306a36Sopenharmony_ci#define PIO_COMPLETION_STATUS_UR 1 5462306a36Sopenharmony_ci#define PIO_COMPLETION_STATUS_CRS 2 5562306a36Sopenharmony_ci#define PIO_COMPLETION_STATUS_CA 4 5662306a36Sopenharmony_ci#define PIO_NON_POSTED_REQ BIT(10) 5762306a36Sopenharmony_ci#define PIO_ERR_STATUS BIT(11) 5862306a36Sopenharmony_ci#define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8) 5962306a36Sopenharmony_ci#define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc) 6062306a36Sopenharmony_ci#define PIO_WR_DATA (PIO_BASE_ADDR + 0x10) 6162306a36Sopenharmony_ci#define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14) 6262306a36Sopenharmony_ci#define PIO_RD_DATA (PIO_BASE_ADDR + 0x18) 6362306a36Sopenharmony_ci#define PIO_START (PIO_BASE_ADDR + 0x1c) 6462306a36Sopenharmony_ci#define PIO_ISR (PIO_BASE_ADDR + 0x20) 6562306a36Sopenharmony_ci#define PIO_ISRM (PIO_BASE_ADDR + 0x24) 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci/* Aardvark Control registers */ 6862306a36Sopenharmony_ci#define CONTROL_BASE_ADDR 0x4800 6962306a36Sopenharmony_ci#define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0) 7062306a36Sopenharmony_ci#define PCIE_GEN_SEL_MSK 0x3 7162306a36Sopenharmony_ci#define PCIE_GEN_SEL_SHIFT 0x0 7262306a36Sopenharmony_ci#define SPEED_GEN_1 0 7362306a36Sopenharmony_ci#define SPEED_GEN_2 1 7462306a36Sopenharmony_ci#define SPEED_GEN_3 2 7562306a36Sopenharmony_ci#define IS_RC_MSK 1 7662306a36Sopenharmony_ci#define IS_RC_SHIFT 2 7762306a36Sopenharmony_ci#define LANE_CNT_MSK 0x18 7862306a36Sopenharmony_ci#define LANE_CNT_SHIFT 0x3 7962306a36Sopenharmony_ci#define LANE_COUNT_1 (0 << LANE_CNT_SHIFT) 8062306a36Sopenharmony_ci#define LANE_COUNT_2 (1 << LANE_CNT_SHIFT) 8162306a36Sopenharmony_ci#define LANE_COUNT_4 (2 << LANE_CNT_SHIFT) 8262306a36Sopenharmony_ci#define LANE_COUNT_8 (3 << LANE_CNT_SHIFT) 8362306a36Sopenharmony_ci#define LINK_TRAINING_EN BIT(6) 8462306a36Sopenharmony_ci#define LEGACY_INTA BIT(28) 8562306a36Sopenharmony_ci#define LEGACY_INTB BIT(29) 8662306a36Sopenharmony_ci#define LEGACY_INTC BIT(30) 8762306a36Sopenharmony_ci#define LEGACY_INTD BIT(31) 8862306a36Sopenharmony_ci#define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4) 8962306a36Sopenharmony_ci#define HOT_RESET_GEN BIT(0) 9062306a36Sopenharmony_ci#define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8) 9162306a36Sopenharmony_ci#define PCIE_CORE_CTRL2_RESERVED 0x7 9262306a36Sopenharmony_ci#define PCIE_CORE_CTRL2_TD_ENABLE BIT(4) 9362306a36Sopenharmony_ci#define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5) 9462306a36Sopenharmony_ci#define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6) 9562306a36Sopenharmony_ci#define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10) 9662306a36Sopenharmony_ci#define PCIE_CORE_REF_CLK_REG (CONTROL_BASE_ADDR + 0x14) 9762306a36Sopenharmony_ci#define PCIE_CORE_REF_CLK_TX_ENABLE BIT(1) 9862306a36Sopenharmony_ci#define PCIE_CORE_REF_CLK_RX_ENABLE BIT(2) 9962306a36Sopenharmony_ci#define PCIE_MSG_LOG_REG (CONTROL_BASE_ADDR + 0x30) 10062306a36Sopenharmony_ci#define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40) 10162306a36Sopenharmony_ci#define PCIE_MSG_PM_PME_MASK BIT(7) 10262306a36Sopenharmony_ci#define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44) 10362306a36Sopenharmony_ci#define PCIE_ISR0_MSI_INT_PENDING BIT(24) 10462306a36Sopenharmony_ci#define PCIE_ISR0_CORR_ERR BIT(11) 10562306a36Sopenharmony_ci#define PCIE_ISR0_NFAT_ERR BIT(12) 10662306a36Sopenharmony_ci#define PCIE_ISR0_FAT_ERR BIT(13) 10762306a36Sopenharmony_ci#define PCIE_ISR0_ERR_MASK GENMASK(13, 11) 10862306a36Sopenharmony_ci#define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val)) 10962306a36Sopenharmony_ci#define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val)) 11062306a36Sopenharmony_ci#define PCIE_ISR0_ALL_MASK GENMASK(31, 0) 11162306a36Sopenharmony_ci#define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48) 11262306a36Sopenharmony_ci#define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C) 11362306a36Sopenharmony_ci#define PCIE_ISR1_POWER_STATE_CHANGE BIT(4) 11462306a36Sopenharmony_ci#define PCIE_ISR1_FLUSH BIT(5) 11562306a36Sopenharmony_ci#define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val)) 11662306a36Sopenharmony_ci#define PCIE_ISR1_ALL_MASK GENMASK(31, 0) 11762306a36Sopenharmony_ci#define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50) 11862306a36Sopenharmony_ci#define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54) 11962306a36Sopenharmony_ci#define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58) 12062306a36Sopenharmony_ci#define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C) 12162306a36Sopenharmony_ci#define PCIE_MSI_ALL_MASK GENMASK(31, 0) 12262306a36Sopenharmony_ci#define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C) 12362306a36Sopenharmony_ci#define PCIE_MSI_DATA_MASK GENMASK(15, 0) 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ci/* PCIe window configuration */ 12662306a36Sopenharmony_ci#define OB_WIN_BASE_ADDR 0x4c00 12762306a36Sopenharmony_ci#define OB_WIN_BLOCK_SIZE 0x20 12862306a36Sopenharmony_ci#define OB_WIN_COUNT 8 12962306a36Sopenharmony_ci#define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \ 13062306a36Sopenharmony_ci OB_WIN_BLOCK_SIZE * (win) + \ 13162306a36Sopenharmony_ci (offset)) 13262306a36Sopenharmony_ci#define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00) 13362306a36Sopenharmony_ci#define OB_WIN_ENABLE BIT(0) 13462306a36Sopenharmony_ci#define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04) 13562306a36Sopenharmony_ci#define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08) 13662306a36Sopenharmony_ci#define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c) 13762306a36Sopenharmony_ci#define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10) 13862306a36Sopenharmony_ci#define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14) 13962306a36Sopenharmony_ci#define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18) 14062306a36Sopenharmony_ci#define OB_WIN_DEFAULT_ACTIONS (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4) 14162306a36Sopenharmony_ci#define OB_WIN_FUNC_NUM_MASK GENMASK(31, 24) 14262306a36Sopenharmony_ci#define OB_WIN_FUNC_NUM_SHIFT 24 14362306a36Sopenharmony_ci#define OB_WIN_FUNC_NUM_ENABLE BIT(23) 14462306a36Sopenharmony_ci#define OB_WIN_BUS_NUM_BITS_MASK GENMASK(22, 20) 14562306a36Sopenharmony_ci#define OB_WIN_BUS_NUM_BITS_SHIFT 20 14662306a36Sopenharmony_ci#define OB_WIN_MSG_CODE_ENABLE BIT(22) 14762306a36Sopenharmony_ci#define OB_WIN_MSG_CODE_MASK GENMASK(21, 14) 14862306a36Sopenharmony_ci#define OB_WIN_MSG_CODE_SHIFT 14 14962306a36Sopenharmony_ci#define OB_WIN_MSG_PAYLOAD_LEN BIT(12) 15062306a36Sopenharmony_ci#define OB_WIN_ATTR_ENABLE BIT(11) 15162306a36Sopenharmony_ci#define OB_WIN_ATTR_TC_MASK GENMASK(10, 8) 15262306a36Sopenharmony_ci#define OB_WIN_ATTR_TC_SHIFT 8 15362306a36Sopenharmony_ci#define OB_WIN_ATTR_RELAXED BIT(7) 15462306a36Sopenharmony_ci#define OB_WIN_ATTR_NOSNOOP BIT(6) 15562306a36Sopenharmony_ci#define OB_WIN_ATTR_POISON BIT(5) 15662306a36Sopenharmony_ci#define OB_WIN_ATTR_IDO BIT(4) 15762306a36Sopenharmony_ci#define OB_WIN_TYPE_MASK GENMASK(3, 0) 15862306a36Sopenharmony_ci#define OB_WIN_TYPE_SHIFT 0 15962306a36Sopenharmony_ci#define OB_WIN_TYPE_MEM 0x0 16062306a36Sopenharmony_ci#define OB_WIN_TYPE_IO 0x4 16162306a36Sopenharmony_ci#define OB_WIN_TYPE_CONFIG_TYPE0 0x8 16262306a36Sopenharmony_ci#define OB_WIN_TYPE_CONFIG_TYPE1 0x9 16362306a36Sopenharmony_ci#define OB_WIN_TYPE_MSG 0xc 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci/* LMI registers base address and register offsets */ 16662306a36Sopenharmony_ci#define LMI_BASE_ADDR 0x6000 16762306a36Sopenharmony_ci#define CFG_REG (LMI_BASE_ADDR + 0x0) 16862306a36Sopenharmony_ci#define LTSSM_SHIFT 24 16962306a36Sopenharmony_ci#define LTSSM_MASK 0x3f 17062306a36Sopenharmony_ci#define RC_BAR_CONFIG 0x300 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci/* LTSSM values in CFG_REG */ 17362306a36Sopenharmony_cienum { 17462306a36Sopenharmony_ci LTSSM_DETECT_QUIET = 0x0, 17562306a36Sopenharmony_ci LTSSM_DETECT_ACTIVE = 0x1, 17662306a36Sopenharmony_ci LTSSM_POLLING_ACTIVE = 0x2, 17762306a36Sopenharmony_ci LTSSM_POLLING_COMPLIANCE = 0x3, 17862306a36Sopenharmony_ci LTSSM_POLLING_CONFIGURATION = 0x4, 17962306a36Sopenharmony_ci LTSSM_CONFIG_LINKWIDTH_START = 0x5, 18062306a36Sopenharmony_ci LTSSM_CONFIG_LINKWIDTH_ACCEPT = 0x6, 18162306a36Sopenharmony_ci LTSSM_CONFIG_LANENUM_ACCEPT = 0x7, 18262306a36Sopenharmony_ci LTSSM_CONFIG_LANENUM_WAIT = 0x8, 18362306a36Sopenharmony_ci LTSSM_CONFIG_COMPLETE = 0x9, 18462306a36Sopenharmony_ci LTSSM_CONFIG_IDLE = 0xa, 18562306a36Sopenharmony_ci LTSSM_RECOVERY_RCVR_LOCK = 0xb, 18662306a36Sopenharmony_ci LTSSM_RECOVERY_SPEED = 0xc, 18762306a36Sopenharmony_ci LTSSM_RECOVERY_RCVR_CFG = 0xd, 18862306a36Sopenharmony_ci LTSSM_RECOVERY_IDLE = 0xe, 18962306a36Sopenharmony_ci LTSSM_L0 = 0x10, 19062306a36Sopenharmony_ci LTSSM_RX_L0S_ENTRY = 0x11, 19162306a36Sopenharmony_ci LTSSM_RX_L0S_IDLE = 0x12, 19262306a36Sopenharmony_ci LTSSM_RX_L0S_FTS = 0x13, 19362306a36Sopenharmony_ci LTSSM_TX_L0S_ENTRY = 0x14, 19462306a36Sopenharmony_ci LTSSM_TX_L0S_IDLE = 0x15, 19562306a36Sopenharmony_ci LTSSM_TX_L0S_FTS = 0x16, 19662306a36Sopenharmony_ci LTSSM_L1_ENTRY = 0x17, 19762306a36Sopenharmony_ci LTSSM_L1_IDLE = 0x18, 19862306a36Sopenharmony_ci LTSSM_L2_IDLE = 0x19, 19962306a36Sopenharmony_ci LTSSM_L2_TRANSMIT_WAKE = 0x1a, 20062306a36Sopenharmony_ci LTSSM_DISABLED = 0x20, 20162306a36Sopenharmony_ci LTSSM_LOOPBACK_ENTRY_MASTER = 0x21, 20262306a36Sopenharmony_ci LTSSM_LOOPBACK_ACTIVE_MASTER = 0x22, 20362306a36Sopenharmony_ci LTSSM_LOOPBACK_EXIT_MASTER = 0x23, 20462306a36Sopenharmony_ci LTSSM_LOOPBACK_ENTRY_SLAVE = 0x24, 20562306a36Sopenharmony_ci LTSSM_LOOPBACK_ACTIVE_SLAVE = 0x25, 20662306a36Sopenharmony_ci LTSSM_LOOPBACK_EXIT_SLAVE = 0x26, 20762306a36Sopenharmony_ci LTSSM_HOT_RESET = 0x27, 20862306a36Sopenharmony_ci LTSSM_RECOVERY_EQUALIZATION_PHASE0 = 0x28, 20962306a36Sopenharmony_ci LTSSM_RECOVERY_EQUALIZATION_PHASE1 = 0x29, 21062306a36Sopenharmony_ci LTSSM_RECOVERY_EQUALIZATION_PHASE2 = 0x2a, 21162306a36Sopenharmony_ci LTSSM_RECOVERY_EQUALIZATION_PHASE3 = 0x2b, 21262306a36Sopenharmony_ci}; 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ci#define VENDOR_ID_REG (LMI_BASE_ADDR + 0x44) 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ci/* PCIe core controller registers */ 21762306a36Sopenharmony_ci#define CTRL_CORE_BASE_ADDR 0x18000 21862306a36Sopenharmony_ci#define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0) 21962306a36Sopenharmony_ci#define CTRL_MODE_SHIFT 0x0 22062306a36Sopenharmony_ci#define CTRL_MODE_MASK 0x1 22162306a36Sopenharmony_ci#define PCIE_CORE_MODE_DIRECT 0x0 22262306a36Sopenharmony_ci#define PCIE_CORE_MODE_COMMAND 0x1 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci/* PCIe Central Interrupts Registers */ 22562306a36Sopenharmony_ci#define CENTRAL_INT_BASE_ADDR 0x1b000 22662306a36Sopenharmony_ci#define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0) 22762306a36Sopenharmony_ci#define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4) 22862306a36Sopenharmony_ci#define PCIE_IRQ_CMDQ_INT BIT(0) 22962306a36Sopenharmony_ci#define PCIE_IRQ_MSI_STATUS_INT BIT(1) 23062306a36Sopenharmony_ci#define PCIE_IRQ_CMD_SENT_DONE BIT(3) 23162306a36Sopenharmony_ci#define PCIE_IRQ_DMA_INT BIT(4) 23262306a36Sopenharmony_ci#define PCIE_IRQ_IB_DXFERDONE BIT(5) 23362306a36Sopenharmony_ci#define PCIE_IRQ_OB_DXFERDONE BIT(6) 23462306a36Sopenharmony_ci#define PCIE_IRQ_OB_RXFERDONE BIT(7) 23562306a36Sopenharmony_ci#define PCIE_IRQ_COMPQ_INT BIT(12) 23662306a36Sopenharmony_ci#define PCIE_IRQ_DIR_RD_DDR_DET BIT(13) 23762306a36Sopenharmony_ci#define PCIE_IRQ_DIR_WR_DDR_DET BIT(14) 23862306a36Sopenharmony_ci#define PCIE_IRQ_CORE_INT BIT(16) 23962306a36Sopenharmony_ci#define PCIE_IRQ_CORE_INT_PIO BIT(17) 24062306a36Sopenharmony_ci#define PCIE_IRQ_DPMU_INT BIT(18) 24162306a36Sopenharmony_ci#define PCIE_IRQ_PCIE_MIS_INT BIT(19) 24262306a36Sopenharmony_ci#define PCIE_IRQ_MSI_INT1_DET BIT(20) 24362306a36Sopenharmony_ci#define PCIE_IRQ_MSI_INT2_DET BIT(21) 24462306a36Sopenharmony_ci#define PCIE_IRQ_RC_DBELL_DET BIT(22) 24562306a36Sopenharmony_ci#define PCIE_IRQ_EP_STATUS BIT(23) 24662306a36Sopenharmony_ci#define PCIE_IRQ_ALL_MASK GENMASK(31, 0) 24762306a36Sopenharmony_ci#define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT 24862306a36Sopenharmony_ci 24962306a36Sopenharmony_ci/* Transaction types */ 25062306a36Sopenharmony_ci#define PCIE_CONFIG_RD_TYPE0 0x8 25162306a36Sopenharmony_ci#define PCIE_CONFIG_RD_TYPE1 0x9 25262306a36Sopenharmony_ci#define PCIE_CONFIG_WR_TYPE0 0xa 25362306a36Sopenharmony_ci#define PCIE_CONFIG_WR_TYPE1 0xb 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ci#define PIO_RETRY_CNT 750000 /* 1.5 s */ 25662306a36Sopenharmony_ci#define PIO_RETRY_DELAY 2 /* 2 us*/ 25762306a36Sopenharmony_ci 25862306a36Sopenharmony_ci#define LINK_WAIT_MAX_RETRIES 10 25962306a36Sopenharmony_ci#define LINK_WAIT_USLEEP_MIN 90000 26062306a36Sopenharmony_ci#define LINK_WAIT_USLEEP_MAX 100000 26162306a36Sopenharmony_ci#define RETRAIN_WAIT_MAX_RETRIES 10 26262306a36Sopenharmony_ci#define RETRAIN_WAIT_USLEEP_US 2000 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci#define MSI_IRQ_NUM 32 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ci#define CFG_RD_CRS_VAL 0xffff0001 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_cistruct advk_pcie { 26962306a36Sopenharmony_ci struct platform_device *pdev; 27062306a36Sopenharmony_ci void __iomem *base; 27162306a36Sopenharmony_ci struct { 27262306a36Sopenharmony_ci phys_addr_t match; 27362306a36Sopenharmony_ci phys_addr_t remap; 27462306a36Sopenharmony_ci phys_addr_t mask; 27562306a36Sopenharmony_ci u32 actions; 27662306a36Sopenharmony_ci } wins[OB_WIN_COUNT]; 27762306a36Sopenharmony_ci u8 wins_count; 27862306a36Sopenharmony_ci struct irq_domain *rp_irq_domain; 27962306a36Sopenharmony_ci struct irq_domain *irq_domain; 28062306a36Sopenharmony_ci struct irq_chip irq_chip; 28162306a36Sopenharmony_ci raw_spinlock_t irq_lock; 28262306a36Sopenharmony_ci struct irq_domain *msi_domain; 28362306a36Sopenharmony_ci struct irq_domain *msi_inner_domain; 28462306a36Sopenharmony_ci raw_spinlock_t msi_irq_lock; 28562306a36Sopenharmony_ci DECLARE_BITMAP(msi_used, MSI_IRQ_NUM); 28662306a36Sopenharmony_ci struct mutex msi_used_lock; 28762306a36Sopenharmony_ci int link_gen; 28862306a36Sopenharmony_ci struct pci_bridge_emul bridge; 28962306a36Sopenharmony_ci struct gpio_desc *reset_gpio; 29062306a36Sopenharmony_ci struct phy *phy; 29162306a36Sopenharmony_ci}; 29262306a36Sopenharmony_ci 29362306a36Sopenharmony_cistatic inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg) 29462306a36Sopenharmony_ci{ 29562306a36Sopenharmony_ci writel(val, pcie->base + reg); 29662306a36Sopenharmony_ci} 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_cistatic inline u32 advk_readl(struct advk_pcie *pcie, u64 reg) 29962306a36Sopenharmony_ci{ 30062306a36Sopenharmony_ci return readl(pcie->base + reg); 30162306a36Sopenharmony_ci} 30262306a36Sopenharmony_ci 30362306a36Sopenharmony_cistatic u8 advk_pcie_ltssm_state(struct advk_pcie *pcie) 30462306a36Sopenharmony_ci{ 30562306a36Sopenharmony_ci u32 val; 30662306a36Sopenharmony_ci u8 ltssm_state; 30762306a36Sopenharmony_ci 30862306a36Sopenharmony_ci val = advk_readl(pcie, CFG_REG); 30962306a36Sopenharmony_ci ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK; 31062306a36Sopenharmony_ci return ltssm_state; 31162306a36Sopenharmony_ci} 31262306a36Sopenharmony_ci 31362306a36Sopenharmony_cistatic inline bool advk_pcie_link_up(struct advk_pcie *pcie) 31462306a36Sopenharmony_ci{ 31562306a36Sopenharmony_ci /* check if LTSSM is in normal operation - some L* state */ 31662306a36Sopenharmony_ci u8 ltssm_state = advk_pcie_ltssm_state(pcie); 31762306a36Sopenharmony_ci return ltssm_state >= LTSSM_L0 && ltssm_state < LTSSM_DISABLED; 31862306a36Sopenharmony_ci} 31962306a36Sopenharmony_ci 32062306a36Sopenharmony_cistatic inline bool advk_pcie_link_active(struct advk_pcie *pcie) 32162306a36Sopenharmony_ci{ 32262306a36Sopenharmony_ci /* 32362306a36Sopenharmony_ci * According to PCIe Base specification 3.0, Table 4-14: Link 32462306a36Sopenharmony_ci * Status Mapped to the LTSSM, and 4.2.6.3.6 Configuration.Idle 32562306a36Sopenharmony_ci * is Link Up mapped to LTSSM Configuration.Idle, Recovery, L0, 32662306a36Sopenharmony_ci * L0s, L1 and L2 states. And according to 3.2.1. Data Link 32762306a36Sopenharmony_ci * Control and Management State Machine Rules is DL Up status 32862306a36Sopenharmony_ci * reported in DL Active state. 32962306a36Sopenharmony_ci */ 33062306a36Sopenharmony_ci u8 ltssm_state = advk_pcie_ltssm_state(pcie); 33162306a36Sopenharmony_ci return ltssm_state >= LTSSM_CONFIG_IDLE && ltssm_state < LTSSM_DISABLED; 33262306a36Sopenharmony_ci} 33362306a36Sopenharmony_ci 33462306a36Sopenharmony_cistatic inline bool advk_pcie_link_training(struct advk_pcie *pcie) 33562306a36Sopenharmony_ci{ 33662306a36Sopenharmony_ci /* 33762306a36Sopenharmony_ci * According to PCIe Base specification 3.0, Table 4-14: Link 33862306a36Sopenharmony_ci * Status Mapped to the LTSSM is Link Training mapped to LTSSM 33962306a36Sopenharmony_ci * Configuration and Recovery states. 34062306a36Sopenharmony_ci */ 34162306a36Sopenharmony_ci u8 ltssm_state = advk_pcie_ltssm_state(pcie); 34262306a36Sopenharmony_ci return ((ltssm_state >= LTSSM_CONFIG_LINKWIDTH_START && 34362306a36Sopenharmony_ci ltssm_state < LTSSM_L0) || 34462306a36Sopenharmony_ci (ltssm_state >= LTSSM_RECOVERY_EQUALIZATION_PHASE0 && 34562306a36Sopenharmony_ci ltssm_state <= LTSSM_RECOVERY_EQUALIZATION_PHASE3)); 34662306a36Sopenharmony_ci} 34762306a36Sopenharmony_ci 34862306a36Sopenharmony_cistatic int advk_pcie_wait_for_link(struct advk_pcie *pcie) 34962306a36Sopenharmony_ci{ 35062306a36Sopenharmony_ci int retries; 35162306a36Sopenharmony_ci 35262306a36Sopenharmony_ci /* check if the link is up or not */ 35362306a36Sopenharmony_ci for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) { 35462306a36Sopenharmony_ci if (advk_pcie_link_up(pcie)) 35562306a36Sopenharmony_ci return 0; 35662306a36Sopenharmony_ci 35762306a36Sopenharmony_ci usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX); 35862306a36Sopenharmony_ci } 35962306a36Sopenharmony_ci 36062306a36Sopenharmony_ci return -ETIMEDOUT; 36162306a36Sopenharmony_ci} 36262306a36Sopenharmony_ci 36362306a36Sopenharmony_cistatic void advk_pcie_wait_for_retrain(struct advk_pcie *pcie) 36462306a36Sopenharmony_ci{ 36562306a36Sopenharmony_ci size_t retries; 36662306a36Sopenharmony_ci 36762306a36Sopenharmony_ci for (retries = 0; retries < RETRAIN_WAIT_MAX_RETRIES; ++retries) { 36862306a36Sopenharmony_ci if (advk_pcie_link_training(pcie)) 36962306a36Sopenharmony_ci break; 37062306a36Sopenharmony_ci udelay(RETRAIN_WAIT_USLEEP_US); 37162306a36Sopenharmony_ci } 37262306a36Sopenharmony_ci} 37362306a36Sopenharmony_ci 37462306a36Sopenharmony_cistatic void advk_pcie_issue_perst(struct advk_pcie *pcie) 37562306a36Sopenharmony_ci{ 37662306a36Sopenharmony_ci if (!pcie->reset_gpio) 37762306a36Sopenharmony_ci return; 37862306a36Sopenharmony_ci 37962306a36Sopenharmony_ci /* 10ms delay is needed for some cards */ 38062306a36Sopenharmony_ci dev_info(&pcie->pdev->dev, "issuing PERST via reset GPIO for 10ms\n"); 38162306a36Sopenharmony_ci gpiod_set_value_cansleep(pcie->reset_gpio, 1); 38262306a36Sopenharmony_ci usleep_range(10000, 11000); 38362306a36Sopenharmony_ci gpiod_set_value_cansleep(pcie->reset_gpio, 0); 38462306a36Sopenharmony_ci} 38562306a36Sopenharmony_ci 38662306a36Sopenharmony_cistatic void advk_pcie_train_link(struct advk_pcie *pcie) 38762306a36Sopenharmony_ci{ 38862306a36Sopenharmony_ci struct device *dev = &pcie->pdev->dev; 38962306a36Sopenharmony_ci u32 reg; 39062306a36Sopenharmony_ci int ret; 39162306a36Sopenharmony_ci 39262306a36Sopenharmony_ci /* 39362306a36Sopenharmony_ci * Setup PCIe rev / gen compliance based on device tree property 39462306a36Sopenharmony_ci * 'max-link-speed' which also forces maximal link speed. 39562306a36Sopenharmony_ci */ 39662306a36Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 39762306a36Sopenharmony_ci reg &= ~PCIE_GEN_SEL_MSK; 39862306a36Sopenharmony_ci if (pcie->link_gen == 3) 39962306a36Sopenharmony_ci reg |= SPEED_GEN_3; 40062306a36Sopenharmony_ci else if (pcie->link_gen == 2) 40162306a36Sopenharmony_ci reg |= SPEED_GEN_2; 40262306a36Sopenharmony_ci else 40362306a36Sopenharmony_ci reg |= SPEED_GEN_1; 40462306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 40562306a36Sopenharmony_ci 40662306a36Sopenharmony_ci /* 40762306a36Sopenharmony_ci * Set maximal link speed value also into PCIe Link Control 2 register. 40862306a36Sopenharmony_ci * Armada 3700 Functional Specification says that default value is based 40962306a36Sopenharmony_ci * on SPEED_GEN but tests showed that default value is always 8.0 GT/s. 41062306a36Sopenharmony_ci */ 41162306a36Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2); 41262306a36Sopenharmony_ci reg &= ~PCI_EXP_LNKCTL2_TLS; 41362306a36Sopenharmony_ci if (pcie->link_gen == 3) 41462306a36Sopenharmony_ci reg |= PCI_EXP_LNKCTL2_TLS_8_0GT; 41562306a36Sopenharmony_ci else if (pcie->link_gen == 2) 41662306a36Sopenharmony_ci reg |= PCI_EXP_LNKCTL2_TLS_5_0GT; 41762306a36Sopenharmony_ci else 41862306a36Sopenharmony_ci reg |= PCI_EXP_LNKCTL2_TLS_2_5GT; 41962306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2); 42062306a36Sopenharmony_ci 42162306a36Sopenharmony_ci /* Enable link training after selecting PCIe generation */ 42262306a36Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 42362306a36Sopenharmony_ci reg |= LINK_TRAINING_EN; 42462306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 42562306a36Sopenharmony_ci 42662306a36Sopenharmony_ci /* 42762306a36Sopenharmony_ci * Reset PCIe card via PERST# signal. Some cards are not detected 42862306a36Sopenharmony_ci * during link training when they are in some non-initial state. 42962306a36Sopenharmony_ci */ 43062306a36Sopenharmony_ci advk_pcie_issue_perst(pcie); 43162306a36Sopenharmony_ci 43262306a36Sopenharmony_ci /* 43362306a36Sopenharmony_ci * PERST# signal could have been asserted by pinctrl subsystem before 43462306a36Sopenharmony_ci * probe() callback has been called or issued explicitly by reset gpio 43562306a36Sopenharmony_ci * function advk_pcie_issue_perst(), making the endpoint going into 43662306a36Sopenharmony_ci * fundamental reset. As required by PCI Express spec (PCI Express 43762306a36Sopenharmony_ci * Base Specification, REV. 4.0 PCI Express, February 19 2014, 6.6.1 43862306a36Sopenharmony_ci * Conventional Reset) a delay for at least 100ms after such a reset 43962306a36Sopenharmony_ci * before sending a Configuration Request to the device is needed. 44062306a36Sopenharmony_ci * So wait until PCIe link is up. Function advk_pcie_wait_for_link() 44162306a36Sopenharmony_ci * waits for link at least 900ms. 44262306a36Sopenharmony_ci */ 44362306a36Sopenharmony_ci ret = advk_pcie_wait_for_link(pcie); 44462306a36Sopenharmony_ci if (ret < 0) 44562306a36Sopenharmony_ci dev_err(dev, "link never came up\n"); 44662306a36Sopenharmony_ci else 44762306a36Sopenharmony_ci dev_info(dev, "link up\n"); 44862306a36Sopenharmony_ci} 44962306a36Sopenharmony_ci 45062306a36Sopenharmony_ci/* 45162306a36Sopenharmony_ci * Set PCIe address window register which could be used for memory 45262306a36Sopenharmony_ci * mapping. 45362306a36Sopenharmony_ci */ 45462306a36Sopenharmony_cistatic void advk_pcie_set_ob_win(struct advk_pcie *pcie, u8 win_num, 45562306a36Sopenharmony_ci phys_addr_t match, phys_addr_t remap, 45662306a36Sopenharmony_ci phys_addr_t mask, u32 actions) 45762306a36Sopenharmony_ci{ 45862306a36Sopenharmony_ci advk_writel(pcie, OB_WIN_ENABLE | 45962306a36Sopenharmony_ci lower_32_bits(match), OB_WIN_MATCH_LS(win_num)); 46062306a36Sopenharmony_ci advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num)); 46162306a36Sopenharmony_ci advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num)); 46262306a36Sopenharmony_ci advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num)); 46362306a36Sopenharmony_ci advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num)); 46462306a36Sopenharmony_ci advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num)); 46562306a36Sopenharmony_ci advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num)); 46662306a36Sopenharmony_ci} 46762306a36Sopenharmony_ci 46862306a36Sopenharmony_cistatic void advk_pcie_disable_ob_win(struct advk_pcie *pcie, u8 win_num) 46962306a36Sopenharmony_ci{ 47062306a36Sopenharmony_ci advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num)); 47162306a36Sopenharmony_ci advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num)); 47262306a36Sopenharmony_ci advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num)); 47362306a36Sopenharmony_ci advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num)); 47462306a36Sopenharmony_ci advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num)); 47562306a36Sopenharmony_ci advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num)); 47662306a36Sopenharmony_ci advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num)); 47762306a36Sopenharmony_ci} 47862306a36Sopenharmony_ci 47962306a36Sopenharmony_cistatic void advk_pcie_setup_hw(struct advk_pcie *pcie) 48062306a36Sopenharmony_ci{ 48162306a36Sopenharmony_ci phys_addr_t msi_addr; 48262306a36Sopenharmony_ci u32 reg; 48362306a36Sopenharmony_ci int i; 48462306a36Sopenharmony_ci 48562306a36Sopenharmony_ci /* 48662306a36Sopenharmony_ci * Configure PCIe Reference clock. Direction is from the PCIe 48762306a36Sopenharmony_ci * controller to the endpoint card, so enable transmitting of 48862306a36Sopenharmony_ci * Reference clock differential signal off-chip and disable 48962306a36Sopenharmony_ci * receiving off-chip differential signal. 49062306a36Sopenharmony_ci */ 49162306a36Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_REF_CLK_REG); 49262306a36Sopenharmony_ci reg |= PCIE_CORE_REF_CLK_TX_ENABLE; 49362306a36Sopenharmony_ci reg &= ~PCIE_CORE_REF_CLK_RX_ENABLE; 49462306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_REF_CLK_REG); 49562306a36Sopenharmony_ci 49662306a36Sopenharmony_ci /* Set to Direct mode */ 49762306a36Sopenharmony_ci reg = advk_readl(pcie, CTRL_CONFIG_REG); 49862306a36Sopenharmony_ci reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT); 49962306a36Sopenharmony_ci reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT); 50062306a36Sopenharmony_ci advk_writel(pcie, reg, CTRL_CONFIG_REG); 50162306a36Sopenharmony_ci 50262306a36Sopenharmony_ci /* Set PCI global control register to RC mode */ 50362306a36Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 50462306a36Sopenharmony_ci reg |= (IS_RC_MSK << IS_RC_SHIFT); 50562306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 50662306a36Sopenharmony_ci 50762306a36Sopenharmony_ci /* 50862306a36Sopenharmony_ci * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab. 50962306a36Sopenharmony_ci * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor 51062306a36Sopenharmony_ci * id in high 16 bits. Updating this register changes readback value of 51162306a36Sopenharmony_ci * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround 51262306a36Sopenharmony_ci * for erratum 4.1: "The value of device and vendor ID is incorrect". 51362306a36Sopenharmony_ci */ 51462306a36Sopenharmony_ci reg = (PCI_VENDOR_ID_MARVELL << 16) | PCI_VENDOR_ID_MARVELL; 51562306a36Sopenharmony_ci advk_writel(pcie, reg, VENDOR_ID_REG); 51662306a36Sopenharmony_ci 51762306a36Sopenharmony_ci /* 51862306a36Sopenharmony_ci * Change Class Code of PCI Bridge device to PCI Bridge (0x600400), 51962306a36Sopenharmony_ci * because the default value is Mass storage controller (0x010400). 52062306a36Sopenharmony_ci * 52162306a36Sopenharmony_ci * Note that this Aardvark PCI Bridge does not have compliant Type 1 52262306a36Sopenharmony_ci * Configuration Space and it even cannot be accessed via Aardvark's 52362306a36Sopenharmony_ci * PCI config space access method. Something like config space is 52462306a36Sopenharmony_ci * available in internal Aardvark registers starting at offset 0x0 52562306a36Sopenharmony_ci * and is reported as Type 0. In range 0x10 - 0x34 it has totally 52662306a36Sopenharmony_ci * different registers. 52762306a36Sopenharmony_ci * 52862306a36Sopenharmony_ci * Therefore driver uses emulation of PCI Bridge which emulates 52962306a36Sopenharmony_ci * access to configuration space via internal Aardvark registers or 53062306a36Sopenharmony_ci * emulated configuration buffer. 53162306a36Sopenharmony_ci */ 53262306a36Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_DEV_REV_REG); 53362306a36Sopenharmony_ci reg &= ~0xffffff00; 53462306a36Sopenharmony_ci reg |= PCI_CLASS_BRIDGE_PCI_NORMAL << 8; 53562306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_DEV_REV_REG); 53662306a36Sopenharmony_ci 53762306a36Sopenharmony_ci /* Disable Root Bridge I/O space, memory space and bus mastering */ 53862306a36Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG); 53962306a36Sopenharmony_ci reg &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); 54062306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG); 54162306a36Sopenharmony_ci 54262306a36Sopenharmony_ci /* Set Advanced Error Capabilities and Control PF0 register */ 54362306a36Sopenharmony_ci reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX | 54462306a36Sopenharmony_ci PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN | 54562306a36Sopenharmony_ci PCIE_CORE_ERR_CAPCTL_ECRC_CHCK | 54662306a36Sopenharmony_ci PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV; 54762306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG); 54862306a36Sopenharmony_ci 54962306a36Sopenharmony_ci /* Set PCIe Device Control register */ 55062306a36Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL); 55162306a36Sopenharmony_ci reg &= ~PCI_EXP_DEVCTL_RELAX_EN; 55262306a36Sopenharmony_ci reg &= ~PCI_EXP_DEVCTL_NOSNOOP_EN; 55362306a36Sopenharmony_ci reg &= ~PCI_EXP_DEVCTL_PAYLOAD; 55462306a36Sopenharmony_ci reg &= ~PCI_EXP_DEVCTL_READRQ; 55562306a36Sopenharmony_ci reg |= PCI_EXP_DEVCTL_PAYLOAD_512B; 55662306a36Sopenharmony_ci reg |= PCI_EXP_DEVCTL_READRQ_512B; 55762306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL); 55862306a36Sopenharmony_ci 55962306a36Sopenharmony_ci /* Program PCIe Control 2 to disable strict ordering */ 56062306a36Sopenharmony_ci reg = PCIE_CORE_CTRL2_RESERVED | 56162306a36Sopenharmony_ci PCIE_CORE_CTRL2_TD_ENABLE; 56262306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); 56362306a36Sopenharmony_ci 56462306a36Sopenharmony_ci /* Set lane X1 */ 56562306a36Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 56662306a36Sopenharmony_ci reg &= ~LANE_CNT_MSK; 56762306a36Sopenharmony_ci reg |= LANE_COUNT_1; 56862306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 56962306a36Sopenharmony_ci 57062306a36Sopenharmony_ci /* Set MSI address */ 57162306a36Sopenharmony_ci msi_addr = virt_to_phys(pcie); 57262306a36Sopenharmony_ci advk_writel(pcie, lower_32_bits(msi_addr), PCIE_MSI_ADDR_LOW_REG); 57362306a36Sopenharmony_ci advk_writel(pcie, upper_32_bits(msi_addr), PCIE_MSI_ADDR_HIGH_REG); 57462306a36Sopenharmony_ci 57562306a36Sopenharmony_ci /* Enable MSI */ 57662306a36Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG); 57762306a36Sopenharmony_ci reg |= PCIE_CORE_CTRL2_MSI_ENABLE; 57862306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); 57962306a36Sopenharmony_ci 58062306a36Sopenharmony_ci /* Clear all interrupts */ 58162306a36Sopenharmony_ci advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG); 58262306a36Sopenharmony_ci advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG); 58362306a36Sopenharmony_ci advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG); 58462306a36Sopenharmony_ci advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG); 58562306a36Sopenharmony_ci 58662306a36Sopenharmony_ci /* Disable All ISR0/1 and MSI Sources */ 58762306a36Sopenharmony_ci advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_MASK_REG); 58862306a36Sopenharmony_ci advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG); 58962306a36Sopenharmony_ci advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG); 59062306a36Sopenharmony_ci 59162306a36Sopenharmony_ci /* Unmask summary MSI interrupt */ 59262306a36Sopenharmony_ci reg = advk_readl(pcie, PCIE_ISR0_MASK_REG); 59362306a36Sopenharmony_ci reg &= ~PCIE_ISR0_MSI_INT_PENDING; 59462306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_ISR0_MASK_REG); 59562306a36Sopenharmony_ci 59662306a36Sopenharmony_ci /* Unmask PME interrupt for processing of PME requester */ 59762306a36Sopenharmony_ci reg = advk_readl(pcie, PCIE_ISR0_MASK_REG); 59862306a36Sopenharmony_ci reg &= ~PCIE_MSG_PM_PME_MASK; 59962306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_ISR0_MASK_REG); 60062306a36Sopenharmony_ci 60162306a36Sopenharmony_ci /* Enable summary interrupt for GIC SPI source */ 60262306a36Sopenharmony_ci reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK); 60362306a36Sopenharmony_ci advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG); 60462306a36Sopenharmony_ci 60562306a36Sopenharmony_ci /* 60662306a36Sopenharmony_ci * Enable AXI address window location generation: 60762306a36Sopenharmony_ci * When it is enabled, the default outbound window 60862306a36Sopenharmony_ci * configurations (Default User Field: 0xD0074CFC) 60962306a36Sopenharmony_ci * are used to transparent address translation for 61062306a36Sopenharmony_ci * the outbound transactions. Thus, PCIe address 61162306a36Sopenharmony_ci * windows are not required for transparent memory 61262306a36Sopenharmony_ci * access when default outbound window configuration 61362306a36Sopenharmony_ci * is set for memory access. 61462306a36Sopenharmony_ci */ 61562306a36Sopenharmony_ci reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG); 61662306a36Sopenharmony_ci reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE; 61762306a36Sopenharmony_ci advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); 61862306a36Sopenharmony_ci 61962306a36Sopenharmony_ci /* 62062306a36Sopenharmony_ci * Set memory access in Default User Field so it 62162306a36Sopenharmony_ci * is not required to configure PCIe address for 62262306a36Sopenharmony_ci * transparent memory access. 62362306a36Sopenharmony_ci */ 62462306a36Sopenharmony_ci advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS); 62562306a36Sopenharmony_ci 62662306a36Sopenharmony_ci /* 62762306a36Sopenharmony_ci * Bypass the address window mapping for PIO: 62862306a36Sopenharmony_ci * Since PIO access already contains all required 62962306a36Sopenharmony_ci * info over AXI interface by PIO registers, the 63062306a36Sopenharmony_ci * address window is not required. 63162306a36Sopenharmony_ci */ 63262306a36Sopenharmony_ci reg = advk_readl(pcie, PIO_CTRL); 63362306a36Sopenharmony_ci reg |= PIO_CTRL_ADDR_WIN_DISABLE; 63462306a36Sopenharmony_ci advk_writel(pcie, reg, PIO_CTRL); 63562306a36Sopenharmony_ci 63662306a36Sopenharmony_ci /* 63762306a36Sopenharmony_ci * Configure PCIe address windows for non-memory or 63862306a36Sopenharmony_ci * non-transparent access as by default PCIe uses 63962306a36Sopenharmony_ci * transparent memory access. 64062306a36Sopenharmony_ci */ 64162306a36Sopenharmony_ci for (i = 0; i < pcie->wins_count; i++) 64262306a36Sopenharmony_ci advk_pcie_set_ob_win(pcie, i, 64362306a36Sopenharmony_ci pcie->wins[i].match, pcie->wins[i].remap, 64462306a36Sopenharmony_ci pcie->wins[i].mask, pcie->wins[i].actions); 64562306a36Sopenharmony_ci 64662306a36Sopenharmony_ci /* Disable remaining PCIe outbound windows */ 64762306a36Sopenharmony_ci for (i = pcie->wins_count; i < OB_WIN_COUNT; i++) 64862306a36Sopenharmony_ci advk_pcie_disable_ob_win(pcie, i); 64962306a36Sopenharmony_ci 65062306a36Sopenharmony_ci advk_pcie_train_link(pcie); 65162306a36Sopenharmony_ci} 65262306a36Sopenharmony_ci 65362306a36Sopenharmony_cistatic int advk_pcie_check_pio_status(struct advk_pcie *pcie, bool allow_crs, u32 *val) 65462306a36Sopenharmony_ci{ 65562306a36Sopenharmony_ci struct device *dev = &pcie->pdev->dev; 65662306a36Sopenharmony_ci u32 reg; 65762306a36Sopenharmony_ci unsigned int status; 65862306a36Sopenharmony_ci char *strcomp_status, *str_posted; 65962306a36Sopenharmony_ci int ret; 66062306a36Sopenharmony_ci 66162306a36Sopenharmony_ci reg = advk_readl(pcie, PIO_STAT); 66262306a36Sopenharmony_ci status = (reg & PIO_COMPLETION_STATUS_MASK) >> 66362306a36Sopenharmony_ci PIO_COMPLETION_STATUS_SHIFT; 66462306a36Sopenharmony_ci 66562306a36Sopenharmony_ci /* 66662306a36Sopenharmony_ci * According to HW spec, the PIO status check sequence as below: 66762306a36Sopenharmony_ci * 1) even if COMPLETION_STATUS(bit9:7) indicates successful, 66862306a36Sopenharmony_ci * it still needs to check Error Status(bit11), only when this bit 66962306a36Sopenharmony_ci * indicates no error happen, the operation is successful. 67062306a36Sopenharmony_ci * 2) value Unsupported Request(1) of COMPLETION_STATUS(bit9:7) only 67162306a36Sopenharmony_ci * means a PIO write error, and for PIO read it is successful with 67262306a36Sopenharmony_ci * a read value of 0xFFFFFFFF. 67362306a36Sopenharmony_ci * 3) value Completion Retry Status(CRS) of COMPLETION_STATUS(bit9:7) 67462306a36Sopenharmony_ci * only means a PIO write error, and for PIO read it is successful 67562306a36Sopenharmony_ci * with a read value of 0xFFFF0001. 67662306a36Sopenharmony_ci * 4) value Completer Abort (CA) of COMPLETION_STATUS(bit9:7) means 67762306a36Sopenharmony_ci * error for both PIO read and PIO write operation. 67862306a36Sopenharmony_ci * 5) other errors are indicated as 'unknown'. 67962306a36Sopenharmony_ci */ 68062306a36Sopenharmony_ci switch (status) { 68162306a36Sopenharmony_ci case PIO_COMPLETION_STATUS_OK: 68262306a36Sopenharmony_ci if (reg & PIO_ERR_STATUS) { 68362306a36Sopenharmony_ci strcomp_status = "COMP_ERR"; 68462306a36Sopenharmony_ci ret = -EFAULT; 68562306a36Sopenharmony_ci break; 68662306a36Sopenharmony_ci } 68762306a36Sopenharmony_ci /* Get the read result */ 68862306a36Sopenharmony_ci if (val) 68962306a36Sopenharmony_ci *val = advk_readl(pcie, PIO_RD_DATA); 69062306a36Sopenharmony_ci /* No error */ 69162306a36Sopenharmony_ci strcomp_status = NULL; 69262306a36Sopenharmony_ci ret = 0; 69362306a36Sopenharmony_ci break; 69462306a36Sopenharmony_ci case PIO_COMPLETION_STATUS_UR: 69562306a36Sopenharmony_ci strcomp_status = "UR"; 69662306a36Sopenharmony_ci ret = -EOPNOTSUPP; 69762306a36Sopenharmony_ci break; 69862306a36Sopenharmony_ci case PIO_COMPLETION_STATUS_CRS: 69962306a36Sopenharmony_ci if (allow_crs && val) { 70062306a36Sopenharmony_ci /* PCIe r4.0, sec 2.3.2, says: 70162306a36Sopenharmony_ci * If CRS Software Visibility is enabled: 70262306a36Sopenharmony_ci * For a Configuration Read Request that includes both 70362306a36Sopenharmony_ci * bytes of the Vendor ID field of a device Function's 70462306a36Sopenharmony_ci * Configuration Space Header, the Root Complex must 70562306a36Sopenharmony_ci * complete the Request to the host by returning a 70662306a36Sopenharmony_ci * read-data value of 0001h for the Vendor ID field and 70762306a36Sopenharmony_ci * all '1's for any additional bytes included in the 70862306a36Sopenharmony_ci * request. 70962306a36Sopenharmony_ci * 71062306a36Sopenharmony_ci * So CRS in this case is not an error status. 71162306a36Sopenharmony_ci */ 71262306a36Sopenharmony_ci *val = CFG_RD_CRS_VAL; 71362306a36Sopenharmony_ci strcomp_status = NULL; 71462306a36Sopenharmony_ci ret = 0; 71562306a36Sopenharmony_ci break; 71662306a36Sopenharmony_ci } 71762306a36Sopenharmony_ci /* PCIe r4.0, sec 2.3.2, says: 71862306a36Sopenharmony_ci * If CRS Software Visibility is not enabled, the Root Complex 71962306a36Sopenharmony_ci * must re-issue the Configuration Request as a new Request. 72062306a36Sopenharmony_ci * If CRS Software Visibility is enabled: For a Configuration 72162306a36Sopenharmony_ci * Write Request or for any other Configuration Read Request, 72262306a36Sopenharmony_ci * the Root Complex must re-issue the Configuration Request as 72362306a36Sopenharmony_ci * a new Request. 72462306a36Sopenharmony_ci * A Root Complex implementation may choose to limit the number 72562306a36Sopenharmony_ci * of Configuration Request/CRS Completion Status loops before 72662306a36Sopenharmony_ci * determining that something is wrong with the target of the 72762306a36Sopenharmony_ci * Request and taking appropriate action, e.g., complete the 72862306a36Sopenharmony_ci * Request to the host as a failed transaction. 72962306a36Sopenharmony_ci * 73062306a36Sopenharmony_ci * So return -EAGAIN and caller (pci-aardvark.c driver) will 73162306a36Sopenharmony_ci * re-issue request again up to the PIO_RETRY_CNT retries. 73262306a36Sopenharmony_ci */ 73362306a36Sopenharmony_ci strcomp_status = "CRS"; 73462306a36Sopenharmony_ci ret = -EAGAIN; 73562306a36Sopenharmony_ci break; 73662306a36Sopenharmony_ci case PIO_COMPLETION_STATUS_CA: 73762306a36Sopenharmony_ci strcomp_status = "CA"; 73862306a36Sopenharmony_ci ret = -ECANCELED; 73962306a36Sopenharmony_ci break; 74062306a36Sopenharmony_ci default: 74162306a36Sopenharmony_ci strcomp_status = "Unknown"; 74262306a36Sopenharmony_ci ret = -EINVAL; 74362306a36Sopenharmony_ci break; 74462306a36Sopenharmony_ci } 74562306a36Sopenharmony_ci 74662306a36Sopenharmony_ci if (!strcomp_status) 74762306a36Sopenharmony_ci return ret; 74862306a36Sopenharmony_ci 74962306a36Sopenharmony_ci if (reg & PIO_NON_POSTED_REQ) 75062306a36Sopenharmony_ci str_posted = "Non-posted"; 75162306a36Sopenharmony_ci else 75262306a36Sopenharmony_ci str_posted = "Posted"; 75362306a36Sopenharmony_ci 75462306a36Sopenharmony_ci dev_dbg(dev, "%s PIO Response Status: %s, %#x @ %#x\n", 75562306a36Sopenharmony_ci str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS)); 75662306a36Sopenharmony_ci 75762306a36Sopenharmony_ci return ret; 75862306a36Sopenharmony_ci} 75962306a36Sopenharmony_ci 76062306a36Sopenharmony_cistatic int advk_pcie_wait_pio(struct advk_pcie *pcie) 76162306a36Sopenharmony_ci{ 76262306a36Sopenharmony_ci struct device *dev = &pcie->pdev->dev; 76362306a36Sopenharmony_ci int i; 76462306a36Sopenharmony_ci 76562306a36Sopenharmony_ci for (i = 1; i <= PIO_RETRY_CNT; i++) { 76662306a36Sopenharmony_ci u32 start, isr; 76762306a36Sopenharmony_ci 76862306a36Sopenharmony_ci start = advk_readl(pcie, PIO_START); 76962306a36Sopenharmony_ci isr = advk_readl(pcie, PIO_ISR); 77062306a36Sopenharmony_ci if (!start && isr) 77162306a36Sopenharmony_ci return i; 77262306a36Sopenharmony_ci udelay(PIO_RETRY_DELAY); 77362306a36Sopenharmony_ci } 77462306a36Sopenharmony_ci 77562306a36Sopenharmony_ci dev_err(dev, "PIO read/write transfer time out\n"); 77662306a36Sopenharmony_ci return -ETIMEDOUT; 77762306a36Sopenharmony_ci} 77862306a36Sopenharmony_ci 77962306a36Sopenharmony_cistatic pci_bridge_emul_read_status_t 78062306a36Sopenharmony_ciadvk_pci_bridge_emul_base_conf_read(struct pci_bridge_emul *bridge, 78162306a36Sopenharmony_ci int reg, u32 *value) 78262306a36Sopenharmony_ci{ 78362306a36Sopenharmony_ci struct advk_pcie *pcie = bridge->data; 78462306a36Sopenharmony_ci 78562306a36Sopenharmony_ci switch (reg) { 78662306a36Sopenharmony_ci case PCI_COMMAND: 78762306a36Sopenharmony_ci *value = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG); 78862306a36Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 78962306a36Sopenharmony_ci 79062306a36Sopenharmony_ci case PCI_INTERRUPT_LINE: { 79162306a36Sopenharmony_ci /* 79262306a36Sopenharmony_ci * From the whole 32bit register we support reading from HW only 79362306a36Sopenharmony_ci * two bits: PCI_BRIDGE_CTL_BUS_RESET and PCI_BRIDGE_CTL_SERR. 79462306a36Sopenharmony_ci * Other bits are retrieved only from emulated config buffer. 79562306a36Sopenharmony_ci */ 79662306a36Sopenharmony_ci __le32 *cfgspace = (__le32 *)&bridge->conf; 79762306a36Sopenharmony_ci u32 val = le32_to_cpu(cfgspace[PCI_INTERRUPT_LINE / 4]); 79862306a36Sopenharmony_ci if (advk_readl(pcie, PCIE_ISR0_MASK_REG) & PCIE_ISR0_ERR_MASK) 79962306a36Sopenharmony_ci val &= ~(PCI_BRIDGE_CTL_SERR << 16); 80062306a36Sopenharmony_ci else 80162306a36Sopenharmony_ci val |= PCI_BRIDGE_CTL_SERR << 16; 80262306a36Sopenharmony_ci if (advk_readl(pcie, PCIE_CORE_CTRL1_REG) & HOT_RESET_GEN) 80362306a36Sopenharmony_ci val |= PCI_BRIDGE_CTL_BUS_RESET << 16; 80462306a36Sopenharmony_ci else 80562306a36Sopenharmony_ci val &= ~(PCI_BRIDGE_CTL_BUS_RESET << 16); 80662306a36Sopenharmony_ci *value = val; 80762306a36Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 80862306a36Sopenharmony_ci } 80962306a36Sopenharmony_ci 81062306a36Sopenharmony_ci default: 81162306a36Sopenharmony_ci return PCI_BRIDGE_EMUL_NOT_HANDLED; 81262306a36Sopenharmony_ci } 81362306a36Sopenharmony_ci} 81462306a36Sopenharmony_ci 81562306a36Sopenharmony_cistatic void 81662306a36Sopenharmony_ciadvk_pci_bridge_emul_base_conf_write(struct pci_bridge_emul *bridge, 81762306a36Sopenharmony_ci int reg, u32 old, u32 new, u32 mask) 81862306a36Sopenharmony_ci{ 81962306a36Sopenharmony_ci struct advk_pcie *pcie = bridge->data; 82062306a36Sopenharmony_ci 82162306a36Sopenharmony_ci switch (reg) { 82262306a36Sopenharmony_ci case PCI_COMMAND: 82362306a36Sopenharmony_ci advk_writel(pcie, new, PCIE_CORE_CMD_STATUS_REG); 82462306a36Sopenharmony_ci break; 82562306a36Sopenharmony_ci 82662306a36Sopenharmony_ci case PCI_INTERRUPT_LINE: 82762306a36Sopenharmony_ci /* 82862306a36Sopenharmony_ci * According to Figure 6-3: Pseudo Logic Diagram for Error 82962306a36Sopenharmony_ci * Message Controls in PCIe base specification, SERR# Enable bit 83062306a36Sopenharmony_ci * in Bridge Control register enable receiving of ERR_* messages 83162306a36Sopenharmony_ci */ 83262306a36Sopenharmony_ci if (mask & (PCI_BRIDGE_CTL_SERR << 16)) { 83362306a36Sopenharmony_ci u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG); 83462306a36Sopenharmony_ci if (new & (PCI_BRIDGE_CTL_SERR << 16)) 83562306a36Sopenharmony_ci val &= ~PCIE_ISR0_ERR_MASK; 83662306a36Sopenharmony_ci else 83762306a36Sopenharmony_ci val |= PCIE_ISR0_ERR_MASK; 83862306a36Sopenharmony_ci advk_writel(pcie, val, PCIE_ISR0_MASK_REG); 83962306a36Sopenharmony_ci } 84062306a36Sopenharmony_ci if (mask & (PCI_BRIDGE_CTL_BUS_RESET << 16)) { 84162306a36Sopenharmony_ci u32 val = advk_readl(pcie, PCIE_CORE_CTRL1_REG); 84262306a36Sopenharmony_ci if (new & (PCI_BRIDGE_CTL_BUS_RESET << 16)) 84362306a36Sopenharmony_ci val |= HOT_RESET_GEN; 84462306a36Sopenharmony_ci else 84562306a36Sopenharmony_ci val &= ~HOT_RESET_GEN; 84662306a36Sopenharmony_ci advk_writel(pcie, val, PCIE_CORE_CTRL1_REG); 84762306a36Sopenharmony_ci } 84862306a36Sopenharmony_ci break; 84962306a36Sopenharmony_ci 85062306a36Sopenharmony_ci default: 85162306a36Sopenharmony_ci break; 85262306a36Sopenharmony_ci } 85362306a36Sopenharmony_ci} 85462306a36Sopenharmony_ci 85562306a36Sopenharmony_cistatic pci_bridge_emul_read_status_t 85662306a36Sopenharmony_ciadvk_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge, 85762306a36Sopenharmony_ci int reg, u32 *value) 85862306a36Sopenharmony_ci{ 85962306a36Sopenharmony_ci struct advk_pcie *pcie = bridge->data; 86062306a36Sopenharmony_ci 86162306a36Sopenharmony_ci 86262306a36Sopenharmony_ci switch (reg) { 86362306a36Sopenharmony_ci /* 86462306a36Sopenharmony_ci * PCI_EXP_SLTCAP, PCI_EXP_SLTCTL, PCI_EXP_RTCTL and PCI_EXP_RTSTA are 86562306a36Sopenharmony_ci * also supported, but do not need to be handled here, because their 86662306a36Sopenharmony_ci * values are stored in emulated config space buffer, and we read them 86762306a36Sopenharmony_ci * from there when needed. 86862306a36Sopenharmony_ci */ 86962306a36Sopenharmony_ci 87062306a36Sopenharmony_ci case PCI_EXP_LNKCAP: { 87162306a36Sopenharmony_ci u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg); 87262306a36Sopenharmony_ci /* 87362306a36Sopenharmony_ci * PCI_EXP_LNKCAP_DLLLARC bit is hardwired in aardvark HW to 0. 87462306a36Sopenharmony_ci * But support for PCI_EXP_LNKSTA_DLLLA is emulated via ltssm 87562306a36Sopenharmony_ci * state so explicitly enable PCI_EXP_LNKCAP_DLLLARC flag. 87662306a36Sopenharmony_ci */ 87762306a36Sopenharmony_ci val |= PCI_EXP_LNKCAP_DLLLARC; 87862306a36Sopenharmony_ci *value = val; 87962306a36Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 88062306a36Sopenharmony_ci } 88162306a36Sopenharmony_ci 88262306a36Sopenharmony_ci case PCI_EXP_LNKCTL: { 88362306a36Sopenharmony_ci /* u32 contains both PCI_EXP_LNKCTL and PCI_EXP_LNKSTA */ 88462306a36Sopenharmony_ci u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg) & 88562306a36Sopenharmony_ci ~(PCI_EXP_LNKSTA_LT << 16); 88662306a36Sopenharmony_ci if (advk_pcie_link_training(pcie)) 88762306a36Sopenharmony_ci val |= (PCI_EXP_LNKSTA_LT << 16); 88862306a36Sopenharmony_ci if (advk_pcie_link_active(pcie)) 88962306a36Sopenharmony_ci val |= (PCI_EXP_LNKSTA_DLLLA << 16); 89062306a36Sopenharmony_ci *value = val; 89162306a36Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 89262306a36Sopenharmony_ci } 89362306a36Sopenharmony_ci 89462306a36Sopenharmony_ci case PCI_EXP_DEVCAP: 89562306a36Sopenharmony_ci case PCI_EXP_DEVCTL: 89662306a36Sopenharmony_ci case PCI_EXP_DEVCAP2: 89762306a36Sopenharmony_ci case PCI_EXP_DEVCTL2: 89862306a36Sopenharmony_ci case PCI_EXP_LNKCAP2: 89962306a36Sopenharmony_ci case PCI_EXP_LNKCTL2: 90062306a36Sopenharmony_ci *value = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg); 90162306a36Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 90262306a36Sopenharmony_ci 90362306a36Sopenharmony_ci default: 90462306a36Sopenharmony_ci return PCI_BRIDGE_EMUL_NOT_HANDLED; 90562306a36Sopenharmony_ci } 90662306a36Sopenharmony_ci 90762306a36Sopenharmony_ci} 90862306a36Sopenharmony_ci 90962306a36Sopenharmony_cistatic void 91062306a36Sopenharmony_ciadvk_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge, 91162306a36Sopenharmony_ci int reg, u32 old, u32 new, u32 mask) 91262306a36Sopenharmony_ci{ 91362306a36Sopenharmony_ci struct advk_pcie *pcie = bridge->data; 91462306a36Sopenharmony_ci 91562306a36Sopenharmony_ci switch (reg) { 91662306a36Sopenharmony_ci case PCI_EXP_LNKCTL: 91762306a36Sopenharmony_ci advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg); 91862306a36Sopenharmony_ci if (new & PCI_EXP_LNKCTL_RL) 91962306a36Sopenharmony_ci advk_pcie_wait_for_retrain(pcie); 92062306a36Sopenharmony_ci break; 92162306a36Sopenharmony_ci 92262306a36Sopenharmony_ci case PCI_EXP_RTCTL: { 92362306a36Sopenharmony_ci u16 rootctl = le16_to_cpu(bridge->pcie_conf.rootctl); 92462306a36Sopenharmony_ci /* Only emulation of PMEIE and CRSSVE bits is provided */ 92562306a36Sopenharmony_ci rootctl &= PCI_EXP_RTCTL_PMEIE | PCI_EXP_RTCTL_CRSSVE; 92662306a36Sopenharmony_ci bridge->pcie_conf.rootctl = cpu_to_le16(rootctl); 92762306a36Sopenharmony_ci break; 92862306a36Sopenharmony_ci } 92962306a36Sopenharmony_ci 93062306a36Sopenharmony_ci /* 93162306a36Sopenharmony_ci * PCI_EXP_RTSTA is also supported, but does not need to be handled 93262306a36Sopenharmony_ci * here, because its value is stored in emulated config space buffer, 93362306a36Sopenharmony_ci * and we write it there when needed. 93462306a36Sopenharmony_ci */ 93562306a36Sopenharmony_ci 93662306a36Sopenharmony_ci case PCI_EXP_DEVCTL: 93762306a36Sopenharmony_ci case PCI_EXP_DEVCTL2: 93862306a36Sopenharmony_ci case PCI_EXP_LNKCTL2: 93962306a36Sopenharmony_ci advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg); 94062306a36Sopenharmony_ci break; 94162306a36Sopenharmony_ci 94262306a36Sopenharmony_ci default: 94362306a36Sopenharmony_ci break; 94462306a36Sopenharmony_ci } 94562306a36Sopenharmony_ci} 94662306a36Sopenharmony_ci 94762306a36Sopenharmony_cistatic pci_bridge_emul_read_status_t 94862306a36Sopenharmony_ciadvk_pci_bridge_emul_ext_conf_read(struct pci_bridge_emul *bridge, 94962306a36Sopenharmony_ci int reg, u32 *value) 95062306a36Sopenharmony_ci{ 95162306a36Sopenharmony_ci struct advk_pcie *pcie = bridge->data; 95262306a36Sopenharmony_ci 95362306a36Sopenharmony_ci switch (reg) { 95462306a36Sopenharmony_ci case 0: 95562306a36Sopenharmony_ci *value = advk_readl(pcie, PCIE_CORE_PCIERR_CAP + reg); 95662306a36Sopenharmony_ci 95762306a36Sopenharmony_ci /* 95862306a36Sopenharmony_ci * PCI_EXT_CAP_NEXT bits are set to offset 0x150, but Armada 95962306a36Sopenharmony_ci * 3700 Functional Specification does not document registers 96062306a36Sopenharmony_ci * at those addresses. 96162306a36Sopenharmony_ci * 96262306a36Sopenharmony_ci * Thus we clear PCI_EXT_CAP_NEXT bits to make Advanced Error 96362306a36Sopenharmony_ci * Reporting Capability header the last Extended Capability. 96462306a36Sopenharmony_ci * If we obtain documentation for those registers in the 96562306a36Sopenharmony_ci * future, this can be changed. 96662306a36Sopenharmony_ci */ 96762306a36Sopenharmony_ci *value &= 0x000fffff; 96862306a36Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 96962306a36Sopenharmony_ci 97062306a36Sopenharmony_ci case PCI_ERR_UNCOR_STATUS: 97162306a36Sopenharmony_ci case PCI_ERR_UNCOR_MASK: 97262306a36Sopenharmony_ci case PCI_ERR_UNCOR_SEVER: 97362306a36Sopenharmony_ci case PCI_ERR_COR_STATUS: 97462306a36Sopenharmony_ci case PCI_ERR_COR_MASK: 97562306a36Sopenharmony_ci case PCI_ERR_CAP: 97662306a36Sopenharmony_ci case PCI_ERR_HEADER_LOG + 0: 97762306a36Sopenharmony_ci case PCI_ERR_HEADER_LOG + 4: 97862306a36Sopenharmony_ci case PCI_ERR_HEADER_LOG + 8: 97962306a36Sopenharmony_ci case PCI_ERR_HEADER_LOG + 12: 98062306a36Sopenharmony_ci case PCI_ERR_ROOT_COMMAND: 98162306a36Sopenharmony_ci case PCI_ERR_ROOT_STATUS: 98262306a36Sopenharmony_ci case PCI_ERR_ROOT_ERR_SRC: 98362306a36Sopenharmony_ci *value = advk_readl(pcie, PCIE_CORE_PCIERR_CAP + reg); 98462306a36Sopenharmony_ci return PCI_BRIDGE_EMUL_HANDLED; 98562306a36Sopenharmony_ci 98662306a36Sopenharmony_ci default: 98762306a36Sopenharmony_ci return PCI_BRIDGE_EMUL_NOT_HANDLED; 98862306a36Sopenharmony_ci } 98962306a36Sopenharmony_ci} 99062306a36Sopenharmony_ci 99162306a36Sopenharmony_cistatic void 99262306a36Sopenharmony_ciadvk_pci_bridge_emul_ext_conf_write(struct pci_bridge_emul *bridge, 99362306a36Sopenharmony_ci int reg, u32 old, u32 new, u32 mask) 99462306a36Sopenharmony_ci{ 99562306a36Sopenharmony_ci struct advk_pcie *pcie = bridge->data; 99662306a36Sopenharmony_ci 99762306a36Sopenharmony_ci switch (reg) { 99862306a36Sopenharmony_ci /* These are W1C registers, so clear other bits */ 99962306a36Sopenharmony_ci case PCI_ERR_UNCOR_STATUS: 100062306a36Sopenharmony_ci case PCI_ERR_COR_STATUS: 100162306a36Sopenharmony_ci case PCI_ERR_ROOT_STATUS: 100262306a36Sopenharmony_ci new &= mask; 100362306a36Sopenharmony_ci fallthrough; 100462306a36Sopenharmony_ci 100562306a36Sopenharmony_ci case PCI_ERR_UNCOR_MASK: 100662306a36Sopenharmony_ci case PCI_ERR_UNCOR_SEVER: 100762306a36Sopenharmony_ci case PCI_ERR_COR_MASK: 100862306a36Sopenharmony_ci case PCI_ERR_CAP: 100962306a36Sopenharmony_ci case PCI_ERR_HEADER_LOG + 0: 101062306a36Sopenharmony_ci case PCI_ERR_HEADER_LOG + 4: 101162306a36Sopenharmony_ci case PCI_ERR_HEADER_LOG + 8: 101262306a36Sopenharmony_ci case PCI_ERR_HEADER_LOG + 12: 101362306a36Sopenharmony_ci case PCI_ERR_ROOT_COMMAND: 101462306a36Sopenharmony_ci case PCI_ERR_ROOT_ERR_SRC: 101562306a36Sopenharmony_ci advk_writel(pcie, new, PCIE_CORE_PCIERR_CAP + reg); 101662306a36Sopenharmony_ci break; 101762306a36Sopenharmony_ci 101862306a36Sopenharmony_ci default: 101962306a36Sopenharmony_ci break; 102062306a36Sopenharmony_ci } 102162306a36Sopenharmony_ci} 102262306a36Sopenharmony_ci 102362306a36Sopenharmony_cistatic const struct pci_bridge_emul_ops advk_pci_bridge_emul_ops = { 102462306a36Sopenharmony_ci .read_base = advk_pci_bridge_emul_base_conf_read, 102562306a36Sopenharmony_ci .write_base = advk_pci_bridge_emul_base_conf_write, 102662306a36Sopenharmony_ci .read_pcie = advk_pci_bridge_emul_pcie_conf_read, 102762306a36Sopenharmony_ci .write_pcie = advk_pci_bridge_emul_pcie_conf_write, 102862306a36Sopenharmony_ci .read_ext = advk_pci_bridge_emul_ext_conf_read, 102962306a36Sopenharmony_ci .write_ext = advk_pci_bridge_emul_ext_conf_write, 103062306a36Sopenharmony_ci}; 103162306a36Sopenharmony_ci 103262306a36Sopenharmony_ci/* 103362306a36Sopenharmony_ci * Initialize the configuration space of the PCI-to-PCI bridge 103462306a36Sopenharmony_ci * associated with the given PCIe interface. 103562306a36Sopenharmony_ci */ 103662306a36Sopenharmony_cistatic int advk_sw_pci_bridge_init(struct advk_pcie *pcie) 103762306a36Sopenharmony_ci{ 103862306a36Sopenharmony_ci struct pci_bridge_emul *bridge = &pcie->bridge; 103962306a36Sopenharmony_ci 104062306a36Sopenharmony_ci bridge->conf.vendor = 104162306a36Sopenharmony_ci cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) & 0xffff); 104262306a36Sopenharmony_ci bridge->conf.device = 104362306a36Sopenharmony_ci cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) >> 16); 104462306a36Sopenharmony_ci bridge->conf.class_revision = 104562306a36Sopenharmony_ci cpu_to_le32(advk_readl(pcie, PCIE_CORE_DEV_REV_REG) & 0xff); 104662306a36Sopenharmony_ci 104762306a36Sopenharmony_ci /* Support 32 bits I/O addressing */ 104862306a36Sopenharmony_ci bridge->conf.iobase = PCI_IO_RANGE_TYPE_32; 104962306a36Sopenharmony_ci bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32; 105062306a36Sopenharmony_ci 105162306a36Sopenharmony_ci /* Support 64 bits memory pref */ 105262306a36Sopenharmony_ci bridge->conf.pref_mem_base = cpu_to_le16(PCI_PREF_RANGE_TYPE_64); 105362306a36Sopenharmony_ci bridge->conf.pref_mem_limit = cpu_to_le16(PCI_PREF_RANGE_TYPE_64); 105462306a36Sopenharmony_ci 105562306a36Sopenharmony_ci /* Support interrupt A for MSI feature */ 105662306a36Sopenharmony_ci bridge->conf.intpin = PCI_INTERRUPT_INTA; 105762306a36Sopenharmony_ci 105862306a36Sopenharmony_ci /* 105962306a36Sopenharmony_ci * Aardvark HW provides PCIe Capability structure in version 2 and 106062306a36Sopenharmony_ci * indicate slot support, which is emulated. 106162306a36Sopenharmony_ci */ 106262306a36Sopenharmony_ci bridge->pcie_conf.cap = cpu_to_le16(2 | PCI_EXP_FLAGS_SLOT); 106362306a36Sopenharmony_ci 106462306a36Sopenharmony_ci /* 106562306a36Sopenharmony_ci * Set Presence Detect State bit permanently since there is no support 106662306a36Sopenharmony_ci * for unplugging the card nor detecting whether it is plugged. (If a 106762306a36Sopenharmony_ci * platform exists in the future that supports it, via a GPIO for 106862306a36Sopenharmony_ci * example, it should be implemented via this bit.) 106962306a36Sopenharmony_ci * 107062306a36Sopenharmony_ci * Set physical slot number to 1 since there is only one port and zero 107162306a36Sopenharmony_ci * value is reserved for ports within the same silicon as Root Port 107262306a36Sopenharmony_ci * which is not our case. 107362306a36Sopenharmony_ci */ 107462306a36Sopenharmony_ci bridge->pcie_conf.slotcap = cpu_to_le32(FIELD_PREP(PCI_EXP_SLTCAP_PSN, 107562306a36Sopenharmony_ci 1)); 107662306a36Sopenharmony_ci bridge->pcie_conf.slotsta = cpu_to_le16(PCI_EXP_SLTSTA_PDS); 107762306a36Sopenharmony_ci 107862306a36Sopenharmony_ci /* Indicates supports for Completion Retry Status */ 107962306a36Sopenharmony_ci bridge->pcie_conf.rootcap = cpu_to_le16(PCI_EXP_RTCAP_CRSVIS); 108062306a36Sopenharmony_ci 108162306a36Sopenharmony_ci bridge->subsystem_vendor_id = advk_readl(pcie, PCIE_CORE_SSDEV_ID_REG) & 0xffff; 108262306a36Sopenharmony_ci bridge->subsystem_id = advk_readl(pcie, PCIE_CORE_SSDEV_ID_REG) >> 16; 108362306a36Sopenharmony_ci bridge->has_pcie = true; 108462306a36Sopenharmony_ci bridge->pcie_start = PCIE_CORE_PCIEXP_CAP; 108562306a36Sopenharmony_ci bridge->data = pcie; 108662306a36Sopenharmony_ci bridge->ops = &advk_pci_bridge_emul_ops; 108762306a36Sopenharmony_ci 108862306a36Sopenharmony_ci return pci_bridge_emul_init(bridge, 0); 108962306a36Sopenharmony_ci} 109062306a36Sopenharmony_ci 109162306a36Sopenharmony_cistatic bool advk_pcie_valid_device(struct advk_pcie *pcie, struct pci_bus *bus, 109262306a36Sopenharmony_ci int devfn) 109362306a36Sopenharmony_ci{ 109462306a36Sopenharmony_ci if (pci_is_root_bus(bus) && PCI_SLOT(devfn) != 0) 109562306a36Sopenharmony_ci return false; 109662306a36Sopenharmony_ci 109762306a36Sopenharmony_ci /* 109862306a36Sopenharmony_ci * If the link goes down after we check for link-up, we have a problem: 109962306a36Sopenharmony_ci * if a PIO request is executed while link-down, the whole controller 110062306a36Sopenharmony_ci * gets stuck in a non-functional state, and even after link comes up 110162306a36Sopenharmony_ci * again, PIO requests won't work anymore, and a reset of the whole PCIe 110262306a36Sopenharmony_ci * controller is needed. Therefore we need to prevent sending PIO 110362306a36Sopenharmony_ci * requests while the link is down. 110462306a36Sopenharmony_ci */ 110562306a36Sopenharmony_ci if (!pci_is_root_bus(bus) && !advk_pcie_link_up(pcie)) 110662306a36Sopenharmony_ci return false; 110762306a36Sopenharmony_ci 110862306a36Sopenharmony_ci return true; 110962306a36Sopenharmony_ci} 111062306a36Sopenharmony_ci 111162306a36Sopenharmony_cistatic bool advk_pcie_pio_is_running(struct advk_pcie *pcie) 111262306a36Sopenharmony_ci{ 111362306a36Sopenharmony_ci struct device *dev = &pcie->pdev->dev; 111462306a36Sopenharmony_ci 111562306a36Sopenharmony_ci /* 111662306a36Sopenharmony_ci * Trying to start a new PIO transfer when previous has not completed 111762306a36Sopenharmony_ci * cause External Abort on CPU which results in kernel panic: 111862306a36Sopenharmony_ci * 111962306a36Sopenharmony_ci * SError Interrupt on CPU0, code 0xbf000002 -- SError 112062306a36Sopenharmony_ci * Kernel panic - not syncing: Asynchronous SError Interrupt 112162306a36Sopenharmony_ci * 112262306a36Sopenharmony_ci * Functions advk_pcie_rd_conf() and advk_pcie_wr_conf() are protected 112362306a36Sopenharmony_ci * by raw_spin_lock_irqsave() at pci_lock_config() level to prevent 112462306a36Sopenharmony_ci * concurrent calls at the same time. But because PIO transfer may take 112562306a36Sopenharmony_ci * about 1.5s when link is down or card is disconnected, it means that 112662306a36Sopenharmony_ci * advk_pcie_wait_pio() does not always have to wait for completion. 112762306a36Sopenharmony_ci * 112862306a36Sopenharmony_ci * Some versions of ARM Trusted Firmware handles this External Abort at 112962306a36Sopenharmony_ci * EL3 level and mask it to prevent kernel panic. Relevant TF-A commit: 113062306a36Sopenharmony_ci * https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=3c7dcdac5c50 113162306a36Sopenharmony_ci */ 113262306a36Sopenharmony_ci if (advk_readl(pcie, PIO_START)) { 113362306a36Sopenharmony_ci dev_err(dev, "Previous PIO read/write transfer is still running\n"); 113462306a36Sopenharmony_ci return true; 113562306a36Sopenharmony_ci } 113662306a36Sopenharmony_ci 113762306a36Sopenharmony_ci return false; 113862306a36Sopenharmony_ci} 113962306a36Sopenharmony_ci 114062306a36Sopenharmony_cistatic int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn, 114162306a36Sopenharmony_ci int where, int size, u32 *val) 114262306a36Sopenharmony_ci{ 114362306a36Sopenharmony_ci struct advk_pcie *pcie = bus->sysdata; 114462306a36Sopenharmony_ci int retry_count; 114562306a36Sopenharmony_ci bool allow_crs; 114662306a36Sopenharmony_ci u32 reg; 114762306a36Sopenharmony_ci int ret; 114862306a36Sopenharmony_ci 114962306a36Sopenharmony_ci if (!advk_pcie_valid_device(pcie, bus, devfn)) 115062306a36Sopenharmony_ci return PCIBIOS_DEVICE_NOT_FOUND; 115162306a36Sopenharmony_ci 115262306a36Sopenharmony_ci if (pci_is_root_bus(bus)) 115362306a36Sopenharmony_ci return pci_bridge_emul_conf_read(&pcie->bridge, where, 115462306a36Sopenharmony_ci size, val); 115562306a36Sopenharmony_ci 115662306a36Sopenharmony_ci /* 115762306a36Sopenharmony_ci * Completion Retry Status is possible to return only when reading all 115862306a36Sopenharmony_ci * 4 bytes from PCI_VENDOR_ID and PCI_DEVICE_ID registers at once and 115962306a36Sopenharmony_ci * CRSSVE flag on Root Bridge is enabled. 116062306a36Sopenharmony_ci */ 116162306a36Sopenharmony_ci allow_crs = (where == PCI_VENDOR_ID) && (size == 4) && 116262306a36Sopenharmony_ci (le16_to_cpu(pcie->bridge.pcie_conf.rootctl) & 116362306a36Sopenharmony_ci PCI_EXP_RTCTL_CRSSVE); 116462306a36Sopenharmony_ci 116562306a36Sopenharmony_ci if (advk_pcie_pio_is_running(pcie)) 116662306a36Sopenharmony_ci goto try_crs; 116762306a36Sopenharmony_ci 116862306a36Sopenharmony_ci /* Program the control register */ 116962306a36Sopenharmony_ci reg = advk_readl(pcie, PIO_CTRL); 117062306a36Sopenharmony_ci reg &= ~PIO_CTRL_TYPE_MASK; 117162306a36Sopenharmony_ci if (pci_is_root_bus(bus->parent)) 117262306a36Sopenharmony_ci reg |= PCIE_CONFIG_RD_TYPE0; 117362306a36Sopenharmony_ci else 117462306a36Sopenharmony_ci reg |= PCIE_CONFIG_RD_TYPE1; 117562306a36Sopenharmony_ci advk_writel(pcie, reg, PIO_CTRL); 117662306a36Sopenharmony_ci 117762306a36Sopenharmony_ci /* Program the address registers */ 117862306a36Sopenharmony_ci reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4); 117962306a36Sopenharmony_ci advk_writel(pcie, reg, PIO_ADDR_LS); 118062306a36Sopenharmony_ci advk_writel(pcie, 0, PIO_ADDR_MS); 118162306a36Sopenharmony_ci 118262306a36Sopenharmony_ci /* Program the data strobe */ 118362306a36Sopenharmony_ci advk_writel(pcie, 0xf, PIO_WR_DATA_STRB); 118462306a36Sopenharmony_ci 118562306a36Sopenharmony_ci retry_count = 0; 118662306a36Sopenharmony_ci do { 118762306a36Sopenharmony_ci /* Clear PIO DONE ISR and start the transfer */ 118862306a36Sopenharmony_ci advk_writel(pcie, 1, PIO_ISR); 118962306a36Sopenharmony_ci advk_writel(pcie, 1, PIO_START); 119062306a36Sopenharmony_ci 119162306a36Sopenharmony_ci ret = advk_pcie_wait_pio(pcie); 119262306a36Sopenharmony_ci if (ret < 0) 119362306a36Sopenharmony_ci goto try_crs; 119462306a36Sopenharmony_ci 119562306a36Sopenharmony_ci retry_count += ret; 119662306a36Sopenharmony_ci 119762306a36Sopenharmony_ci /* Check PIO status and get the read result */ 119862306a36Sopenharmony_ci ret = advk_pcie_check_pio_status(pcie, allow_crs, val); 119962306a36Sopenharmony_ci } while (ret == -EAGAIN && retry_count < PIO_RETRY_CNT); 120062306a36Sopenharmony_ci 120162306a36Sopenharmony_ci if (ret < 0) 120262306a36Sopenharmony_ci goto fail; 120362306a36Sopenharmony_ci 120462306a36Sopenharmony_ci if (size == 1) 120562306a36Sopenharmony_ci *val = (*val >> (8 * (where & 3))) & 0xff; 120662306a36Sopenharmony_ci else if (size == 2) 120762306a36Sopenharmony_ci *val = (*val >> (8 * (where & 3))) & 0xffff; 120862306a36Sopenharmony_ci 120962306a36Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 121062306a36Sopenharmony_ci 121162306a36Sopenharmony_citry_crs: 121262306a36Sopenharmony_ci /* 121362306a36Sopenharmony_ci * If it is possible, return Completion Retry Status so that caller 121462306a36Sopenharmony_ci * tries to issue the request again instead of failing. 121562306a36Sopenharmony_ci */ 121662306a36Sopenharmony_ci if (allow_crs) { 121762306a36Sopenharmony_ci *val = CFG_RD_CRS_VAL; 121862306a36Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 121962306a36Sopenharmony_ci } 122062306a36Sopenharmony_ci 122162306a36Sopenharmony_cifail: 122262306a36Sopenharmony_ci *val = 0xffffffff; 122362306a36Sopenharmony_ci return PCIBIOS_SET_FAILED; 122462306a36Sopenharmony_ci} 122562306a36Sopenharmony_ci 122662306a36Sopenharmony_cistatic int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn, 122762306a36Sopenharmony_ci int where, int size, u32 val) 122862306a36Sopenharmony_ci{ 122962306a36Sopenharmony_ci struct advk_pcie *pcie = bus->sysdata; 123062306a36Sopenharmony_ci u32 reg; 123162306a36Sopenharmony_ci u32 data_strobe = 0x0; 123262306a36Sopenharmony_ci int retry_count; 123362306a36Sopenharmony_ci int offset; 123462306a36Sopenharmony_ci int ret; 123562306a36Sopenharmony_ci 123662306a36Sopenharmony_ci if (!advk_pcie_valid_device(pcie, bus, devfn)) 123762306a36Sopenharmony_ci return PCIBIOS_DEVICE_NOT_FOUND; 123862306a36Sopenharmony_ci 123962306a36Sopenharmony_ci if (pci_is_root_bus(bus)) 124062306a36Sopenharmony_ci return pci_bridge_emul_conf_write(&pcie->bridge, where, 124162306a36Sopenharmony_ci size, val); 124262306a36Sopenharmony_ci 124362306a36Sopenharmony_ci if (where % size) 124462306a36Sopenharmony_ci return PCIBIOS_SET_FAILED; 124562306a36Sopenharmony_ci 124662306a36Sopenharmony_ci if (advk_pcie_pio_is_running(pcie)) 124762306a36Sopenharmony_ci return PCIBIOS_SET_FAILED; 124862306a36Sopenharmony_ci 124962306a36Sopenharmony_ci /* Program the control register */ 125062306a36Sopenharmony_ci reg = advk_readl(pcie, PIO_CTRL); 125162306a36Sopenharmony_ci reg &= ~PIO_CTRL_TYPE_MASK; 125262306a36Sopenharmony_ci if (pci_is_root_bus(bus->parent)) 125362306a36Sopenharmony_ci reg |= PCIE_CONFIG_WR_TYPE0; 125462306a36Sopenharmony_ci else 125562306a36Sopenharmony_ci reg |= PCIE_CONFIG_WR_TYPE1; 125662306a36Sopenharmony_ci advk_writel(pcie, reg, PIO_CTRL); 125762306a36Sopenharmony_ci 125862306a36Sopenharmony_ci /* Program the address registers */ 125962306a36Sopenharmony_ci reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4); 126062306a36Sopenharmony_ci advk_writel(pcie, reg, PIO_ADDR_LS); 126162306a36Sopenharmony_ci advk_writel(pcie, 0, PIO_ADDR_MS); 126262306a36Sopenharmony_ci 126362306a36Sopenharmony_ci /* Calculate the write strobe */ 126462306a36Sopenharmony_ci offset = where & 0x3; 126562306a36Sopenharmony_ci reg = val << (8 * offset); 126662306a36Sopenharmony_ci data_strobe = GENMASK(size - 1, 0) << offset; 126762306a36Sopenharmony_ci 126862306a36Sopenharmony_ci /* Program the data register */ 126962306a36Sopenharmony_ci advk_writel(pcie, reg, PIO_WR_DATA); 127062306a36Sopenharmony_ci 127162306a36Sopenharmony_ci /* Program the data strobe */ 127262306a36Sopenharmony_ci advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB); 127362306a36Sopenharmony_ci 127462306a36Sopenharmony_ci retry_count = 0; 127562306a36Sopenharmony_ci do { 127662306a36Sopenharmony_ci /* Clear PIO DONE ISR and start the transfer */ 127762306a36Sopenharmony_ci advk_writel(pcie, 1, PIO_ISR); 127862306a36Sopenharmony_ci advk_writel(pcie, 1, PIO_START); 127962306a36Sopenharmony_ci 128062306a36Sopenharmony_ci ret = advk_pcie_wait_pio(pcie); 128162306a36Sopenharmony_ci if (ret < 0) 128262306a36Sopenharmony_ci return PCIBIOS_SET_FAILED; 128362306a36Sopenharmony_ci 128462306a36Sopenharmony_ci retry_count += ret; 128562306a36Sopenharmony_ci 128662306a36Sopenharmony_ci ret = advk_pcie_check_pio_status(pcie, false, NULL); 128762306a36Sopenharmony_ci } while (ret == -EAGAIN && retry_count < PIO_RETRY_CNT); 128862306a36Sopenharmony_ci 128962306a36Sopenharmony_ci return ret < 0 ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL; 129062306a36Sopenharmony_ci} 129162306a36Sopenharmony_ci 129262306a36Sopenharmony_cistatic struct pci_ops advk_pcie_ops = { 129362306a36Sopenharmony_ci .read = advk_pcie_rd_conf, 129462306a36Sopenharmony_ci .write = advk_pcie_wr_conf, 129562306a36Sopenharmony_ci}; 129662306a36Sopenharmony_ci 129762306a36Sopenharmony_cistatic void advk_msi_irq_compose_msi_msg(struct irq_data *data, 129862306a36Sopenharmony_ci struct msi_msg *msg) 129962306a36Sopenharmony_ci{ 130062306a36Sopenharmony_ci struct advk_pcie *pcie = irq_data_get_irq_chip_data(data); 130162306a36Sopenharmony_ci phys_addr_t msi_addr = virt_to_phys(pcie); 130262306a36Sopenharmony_ci 130362306a36Sopenharmony_ci msg->address_lo = lower_32_bits(msi_addr); 130462306a36Sopenharmony_ci msg->address_hi = upper_32_bits(msi_addr); 130562306a36Sopenharmony_ci msg->data = data->hwirq; 130662306a36Sopenharmony_ci} 130762306a36Sopenharmony_ci 130862306a36Sopenharmony_cistatic int advk_msi_set_affinity(struct irq_data *irq_data, 130962306a36Sopenharmony_ci const struct cpumask *mask, bool force) 131062306a36Sopenharmony_ci{ 131162306a36Sopenharmony_ci return -EINVAL; 131262306a36Sopenharmony_ci} 131362306a36Sopenharmony_ci 131462306a36Sopenharmony_cistatic void advk_msi_irq_mask(struct irq_data *d) 131562306a36Sopenharmony_ci{ 131662306a36Sopenharmony_ci struct advk_pcie *pcie = d->domain->host_data; 131762306a36Sopenharmony_ci irq_hw_number_t hwirq = irqd_to_hwirq(d); 131862306a36Sopenharmony_ci unsigned long flags; 131962306a36Sopenharmony_ci u32 mask; 132062306a36Sopenharmony_ci 132162306a36Sopenharmony_ci raw_spin_lock_irqsave(&pcie->msi_irq_lock, flags); 132262306a36Sopenharmony_ci mask = advk_readl(pcie, PCIE_MSI_MASK_REG); 132362306a36Sopenharmony_ci mask |= BIT(hwirq); 132462306a36Sopenharmony_ci advk_writel(pcie, mask, PCIE_MSI_MASK_REG); 132562306a36Sopenharmony_ci raw_spin_unlock_irqrestore(&pcie->msi_irq_lock, flags); 132662306a36Sopenharmony_ci} 132762306a36Sopenharmony_ci 132862306a36Sopenharmony_cistatic void advk_msi_irq_unmask(struct irq_data *d) 132962306a36Sopenharmony_ci{ 133062306a36Sopenharmony_ci struct advk_pcie *pcie = d->domain->host_data; 133162306a36Sopenharmony_ci irq_hw_number_t hwirq = irqd_to_hwirq(d); 133262306a36Sopenharmony_ci unsigned long flags; 133362306a36Sopenharmony_ci u32 mask; 133462306a36Sopenharmony_ci 133562306a36Sopenharmony_ci raw_spin_lock_irqsave(&pcie->msi_irq_lock, flags); 133662306a36Sopenharmony_ci mask = advk_readl(pcie, PCIE_MSI_MASK_REG); 133762306a36Sopenharmony_ci mask &= ~BIT(hwirq); 133862306a36Sopenharmony_ci advk_writel(pcie, mask, PCIE_MSI_MASK_REG); 133962306a36Sopenharmony_ci raw_spin_unlock_irqrestore(&pcie->msi_irq_lock, flags); 134062306a36Sopenharmony_ci} 134162306a36Sopenharmony_ci 134262306a36Sopenharmony_cistatic void advk_msi_top_irq_mask(struct irq_data *d) 134362306a36Sopenharmony_ci{ 134462306a36Sopenharmony_ci pci_msi_mask_irq(d); 134562306a36Sopenharmony_ci irq_chip_mask_parent(d); 134662306a36Sopenharmony_ci} 134762306a36Sopenharmony_ci 134862306a36Sopenharmony_cistatic void advk_msi_top_irq_unmask(struct irq_data *d) 134962306a36Sopenharmony_ci{ 135062306a36Sopenharmony_ci pci_msi_unmask_irq(d); 135162306a36Sopenharmony_ci irq_chip_unmask_parent(d); 135262306a36Sopenharmony_ci} 135362306a36Sopenharmony_ci 135462306a36Sopenharmony_cistatic struct irq_chip advk_msi_bottom_irq_chip = { 135562306a36Sopenharmony_ci .name = "MSI", 135662306a36Sopenharmony_ci .irq_compose_msi_msg = advk_msi_irq_compose_msi_msg, 135762306a36Sopenharmony_ci .irq_set_affinity = advk_msi_set_affinity, 135862306a36Sopenharmony_ci .irq_mask = advk_msi_irq_mask, 135962306a36Sopenharmony_ci .irq_unmask = advk_msi_irq_unmask, 136062306a36Sopenharmony_ci}; 136162306a36Sopenharmony_ci 136262306a36Sopenharmony_cistatic int advk_msi_irq_domain_alloc(struct irq_domain *domain, 136362306a36Sopenharmony_ci unsigned int virq, 136462306a36Sopenharmony_ci unsigned int nr_irqs, void *args) 136562306a36Sopenharmony_ci{ 136662306a36Sopenharmony_ci struct advk_pcie *pcie = domain->host_data; 136762306a36Sopenharmony_ci int hwirq, i; 136862306a36Sopenharmony_ci 136962306a36Sopenharmony_ci mutex_lock(&pcie->msi_used_lock); 137062306a36Sopenharmony_ci hwirq = bitmap_find_free_region(pcie->msi_used, MSI_IRQ_NUM, 137162306a36Sopenharmony_ci order_base_2(nr_irqs)); 137262306a36Sopenharmony_ci mutex_unlock(&pcie->msi_used_lock); 137362306a36Sopenharmony_ci if (hwirq < 0) 137462306a36Sopenharmony_ci return -ENOSPC; 137562306a36Sopenharmony_ci 137662306a36Sopenharmony_ci for (i = 0; i < nr_irqs; i++) 137762306a36Sopenharmony_ci irq_domain_set_info(domain, virq + i, hwirq + i, 137862306a36Sopenharmony_ci &advk_msi_bottom_irq_chip, 137962306a36Sopenharmony_ci domain->host_data, handle_simple_irq, 138062306a36Sopenharmony_ci NULL, NULL); 138162306a36Sopenharmony_ci 138262306a36Sopenharmony_ci return 0; 138362306a36Sopenharmony_ci} 138462306a36Sopenharmony_ci 138562306a36Sopenharmony_cistatic void advk_msi_irq_domain_free(struct irq_domain *domain, 138662306a36Sopenharmony_ci unsigned int virq, unsigned int nr_irqs) 138762306a36Sopenharmony_ci{ 138862306a36Sopenharmony_ci struct irq_data *d = irq_domain_get_irq_data(domain, virq); 138962306a36Sopenharmony_ci struct advk_pcie *pcie = domain->host_data; 139062306a36Sopenharmony_ci 139162306a36Sopenharmony_ci mutex_lock(&pcie->msi_used_lock); 139262306a36Sopenharmony_ci bitmap_release_region(pcie->msi_used, d->hwirq, order_base_2(nr_irqs)); 139362306a36Sopenharmony_ci mutex_unlock(&pcie->msi_used_lock); 139462306a36Sopenharmony_ci} 139562306a36Sopenharmony_ci 139662306a36Sopenharmony_cistatic const struct irq_domain_ops advk_msi_domain_ops = { 139762306a36Sopenharmony_ci .alloc = advk_msi_irq_domain_alloc, 139862306a36Sopenharmony_ci .free = advk_msi_irq_domain_free, 139962306a36Sopenharmony_ci}; 140062306a36Sopenharmony_ci 140162306a36Sopenharmony_cistatic void advk_pcie_irq_mask(struct irq_data *d) 140262306a36Sopenharmony_ci{ 140362306a36Sopenharmony_ci struct advk_pcie *pcie = d->domain->host_data; 140462306a36Sopenharmony_ci irq_hw_number_t hwirq = irqd_to_hwirq(d); 140562306a36Sopenharmony_ci unsigned long flags; 140662306a36Sopenharmony_ci u32 mask; 140762306a36Sopenharmony_ci 140862306a36Sopenharmony_ci raw_spin_lock_irqsave(&pcie->irq_lock, flags); 140962306a36Sopenharmony_ci mask = advk_readl(pcie, PCIE_ISR1_MASK_REG); 141062306a36Sopenharmony_ci mask |= PCIE_ISR1_INTX_ASSERT(hwirq); 141162306a36Sopenharmony_ci advk_writel(pcie, mask, PCIE_ISR1_MASK_REG); 141262306a36Sopenharmony_ci raw_spin_unlock_irqrestore(&pcie->irq_lock, flags); 141362306a36Sopenharmony_ci} 141462306a36Sopenharmony_ci 141562306a36Sopenharmony_cistatic void advk_pcie_irq_unmask(struct irq_data *d) 141662306a36Sopenharmony_ci{ 141762306a36Sopenharmony_ci struct advk_pcie *pcie = d->domain->host_data; 141862306a36Sopenharmony_ci irq_hw_number_t hwirq = irqd_to_hwirq(d); 141962306a36Sopenharmony_ci unsigned long flags; 142062306a36Sopenharmony_ci u32 mask; 142162306a36Sopenharmony_ci 142262306a36Sopenharmony_ci raw_spin_lock_irqsave(&pcie->irq_lock, flags); 142362306a36Sopenharmony_ci mask = advk_readl(pcie, PCIE_ISR1_MASK_REG); 142462306a36Sopenharmony_ci mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq); 142562306a36Sopenharmony_ci advk_writel(pcie, mask, PCIE_ISR1_MASK_REG); 142662306a36Sopenharmony_ci raw_spin_unlock_irqrestore(&pcie->irq_lock, flags); 142762306a36Sopenharmony_ci} 142862306a36Sopenharmony_ci 142962306a36Sopenharmony_cistatic int advk_pcie_irq_map(struct irq_domain *h, 143062306a36Sopenharmony_ci unsigned int virq, irq_hw_number_t hwirq) 143162306a36Sopenharmony_ci{ 143262306a36Sopenharmony_ci struct advk_pcie *pcie = h->host_data; 143362306a36Sopenharmony_ci 143462306a36Sopenharmony_ci irq_set_status_flags(virq, IRQ_LEVEL); 143562306a36Sopenharmony_ci irq_set_chip_and_handler(virq, &pcie->irq_chip, 143662306a36Sopenharmony_ci handle_level_irq); 143762306a36Sopenharmony_ci irq_set_chip_data(virq, pcie); 143862306a36Sopenharmony_ci 143962306a36Sopenharmony_ci return 0; 144062306a36Sopenharmony_ci} 144162306a36Sopenharmony_ci 144262306a36Sopenharmony_cistatic const struct irq_domain_ops advk_pcie_irq_domain_ops = { 144362306a36Sopenharmony_ci .map = advk_pcie_irq_map, 144462306a36Sopenharmony_ci .xlate = irq_domain_xlate_onecell, 144562306a36Sopenharmony_ci}; 144662306a36Sopenharmony_ci 144762306a36Sopenharmony_cistatic struct irq_chip advk_msi_irq_chip = { 144862306a36Sopenharmony_ci .name = "advk-MSI", 144962306a36Sopenharmony_ci .irq_mask = advk_msi_top_irq_mask, 145062306a36Sopenharmony_ci .irq_unmask = advk_msi_top_irq_unmask, 145162306a36Sopenharmony_ci}; 145262306a36Sopenharmony_ci 145362306a36Sopenharmony_cistatic struct msi_domain_info advk_msi_domain_info = { 145462306a36Sopenharmony_ci .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS | 145562306a36Sopenharmony_ci MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX, 145662306a36Sopenharmony_ci .chip = &advk_msi_irq_chip, 145762306a36Sopenharmony_ci}; 145862306a36Sopenharmony_ci 145962306a36Sopenharmony_cistatic int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie) 146062306a36Sopenharmony_ci{ 146162306a36Sopenharmony_ci struct device *dev = &pcie->pdev->dev; 146262306a36Sopenharmony_ci 146362306a36Sopenharmony_ci raw_spin_lock_init(&pcie->msi_irq_lock); 146462306a36Sopenharmony_ci mutex_init(&pcie->msi_used_lock); 146562306a36Sopenharmony_ci 146662306a36Sopenharmony_ci pcie->msi_inner_domain = 146762306a36Sopenharmony_ci irq_domain_add_linear(NULL, MSI_IRQ_NUM, 146862306a36Sopenharmony_ci &advk_msi_domain_ops, pcie); 146962306a36Sopenharmony_ci if (!pcie->msi_inner_domain) 147062306a36Sopenharmony_ci return -ENOMEM; 147162306a36Sopenharmony_ci 147262306a36Sopenharmony_ci pcie->msi_domain = 147362306a36Sopenharmony_ci pci_msi_create_irq_domain(dev_fwnode(dev), 147462306a36Sopenharmony_ci &advk_msi_domain_info, 147562306a36Sopenharmony_ci pcie->msi_inner_domain); 147662306a36Sopenharmony_ci if (!pcie->msi_domain) { 147762306a36Sopenharmony_ci irq_domain_remove(pcie->msi_inner_domain); 147862306a36Sopenharmony_ci return -ENOMEM; 147962306a36Sopenharmony_ci } 148062306a36Sopenharmony_ci 148162306a36Sopenharmony_ci return 0; 148262306a36Sopenharmony_ci} 148362306a36Sopenharmony_ci 148462306a36Sopenharmony_cistatic void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie) 148562306a36Sopenharmony_ci{ 148662306a36Sopenharmony_ci irq_domain_remove(pcie->msi_domain); 148762306a36Sopenharmony_ci irq_domain_remove(pcie->msi_inner_domain); 148862306a36Sopenharmony_ci} 148962306a36Sopenharmony_ci 149062306a36Sopenharmony_cistatic int advk_pcie_init_irq_domain(struct advk_pcie *pcie) 149162306a36Sopenharmony_ci{ 149262306a36Sopenharmony_ci struct device *dev = &pcie->pdev->dev; 149362306a36Sopenharmony_ci struct device_node *node = dev->of_node; 149462306a36Sopenharmony_ci struct device_node *pcie_intc_node; 149562306a36Sopenharmony_ci struct irq_chip *irq_chip; 149662306a36Sopenharmony_ci int ret = 0; 149762306a36Sopenharmony_ci 149862306a36Sopenharmony_ci raw_spin_lock_init(&pcie->irq_lock); 149962306a36Sopenharmony_ci 150062306a36Sopenharmony_ci pcie_intc_node = of_get_next_child(node, NULL); 150162306a36Sopenharmony_ci if (!pcie_intc_node) { 150262306a36Sopenharmony_ci dev_err(dev, "No PCIe Intc node found\n"); 150362306a36Sopenharmony_ci return -ENODEV; 150462306a36Sopenharmony_ci } 150562306a36Sopenharmony_ci 150662306a36Sopenharmony_ci irq_chip = &pcie->irq_chip; 150762306a36Sopenharmony_ci 150862306a36Sopenharmony_ci irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq", 150962306a36Sopenharmony_ci dev_name(dev)); 151062306a36Sopenharmony_ci if (!irq_chip->name) { 151162306a36Sopenharmony_ci ret = -ENOMEM; 151262306a36Sopenharmony_ci goto out_put_node; 151362306a36Sopenharmony_ci } 151462306a36Sopenharmony_ci 151562306a36Sopenharmony_ci irq_chip->irq_mask = advk_pcie_irq_mask; 151662306a36Sopenharmony_ci irq_chip->irq_unmask = advk_pcie_irq_unmask; 151762306a36Sopenharmony_ci 151862306a36Sopenharmony_ci pcie->irq_domain = 151962306a36Sopenharmony_ci irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX, 152062306a36Sopenharmony_ci &advk_pcie_irq_domain_ops, pcie); 152162306a36Sopenharmony_ci if (!pcie->irq_domain) { 152262306a36Sopenharmony_ci dev_err(dev, "Failed to get a INTx IRQ domain\n"); 152362306a36Sopenharmony_ci ret = -ENOMEM; 152462306a36Sopenharmony_ci goto out_put_node; 152562306a36Sopenharmony_ci } 152662306a36Sopenharmony_ci 152762306a36Sopenharmony_ciout_put_node: 152862306a36Sopenharmony_ci of_node_put(pcie_intc_node); 152962306a36Sopenharmony_ci return ret; 153062306a36Sopenharmony_ci} 153162306a36Sopenharmony_ci 153262306a36Sopenharmony_cistatic void advk_pcie_remove_irq_domain(struct advk_pcie *pcie) 153362306a36Sopenharmony_ci{ 153462306a36Sopenharmony_ci irq_domain_remove(pcie->irq_domain); 153562306a36Sopenharmony_ci} 153662306a36Sopenharmony_ci 153762306a36Sopenharmony_cistatic struct irq_chip advk_rp_irq_chip = { 153862306a36Sopenharmony_ci .name = "advk-RP", 153962306a36Sopenharmony_ci}; 154062306a36Sopenharmony_ci 154162306a36Sopenharmony_cistatic int advk_pcie_rp_irq_map(struct irq_domain *h, 154262306a36Sopenharmony_ci unsigned int virq, irq_hw_number_t hwirq) 154362306a36Sopenharmony_ci{ 154462306a36Sopenharmony_ci struct advk_pcie *pcie = h->host_data; 154562306a36Sopenharmony_ci 154662306a36Sopenharmony_ci irq_set_chip_and_handler(virq, &advk_rp_irq_chip, handle_simple_irq); 154762306a36Sopenharmony_ci irq_set_chip_data(virq, pcie); 154862306a36Sopenharmony_ci 154962306a36Sopenharmony_ci return 0; 155062306a36Sopenharmony_ci} 155162306a36Sopenharmony_ci 155262306a36Sopenharmony_cistatic const struct irq_domain_ops advk_pcie_rp_irq_domain_ops = { 155362306a36Sopenharmony_ci .map = advk_pcie_rp_irq_map, 155462306a36Sopenharmony_ci .xlate = irq_domain_xlate_onecell, 155562306a36Sopenharmony_ci}; 155662306a36Sopenharmony_ci 155762306a36Sopenharmony_cistatic int advk_pcie_init_rp_irq_domain(struct advk_pcie *pcie) 155862306a36Sopenharmony_ci{ 155962306a36Sopenharmony_ci pcie->rp_irq_domain = irq_domain_add_linear(NULL, 1, 156062306a36Sopenharmony_ci &advk_pcie_rp_irq_domain_ops, 156162306a36Sopenharmony_ci pcie); 156262306a36Sopenharmony_ci if (!pcie->rp_irq_domain) { 156362306a36Sopenharmony_ci dev_err(&pcie->pdev->dev, "Failed to add Root Port IRQ domain\n"); 156462306a36Sopenharmony_ci return -ENOMEM; 156562306a36Sopenharmony_ci } 156662306a36Sopenharmony_ci 156762306a36Sopenharmony_ci return 0; 156862306a36Sopenharmony_ci} 156962306a36Sopenharmony_ci 157062306a36Sopenharmony_cistatic void advk_pcie_remove_rp_irq_domain(struct advk_pcie *pcie) 157162306a36Sopenharmony_ci{ 157262306a36Sopenharmony_ci irq_domain_remove(pcie->rp_irq_domain); 157362306a36Sopenharmony_ci} 157462306a36Sopenharmony_ci 157562306a36Sopenharmony_cistatic void advk_pcie_handle_pme(struct advk_pcie *pcie) 157662306a36Sopenharmony_ci{ 157762306a36Sopenharmony_ci u32 requester = advk_readl(pcie, PCIE_MSG_LOG_REG) >> 16; 157862306a36Sopenharmony_ci 157962306a36Sopenharmony_ci advk_writel(pcie, PCIE_MSG_PM_PME_MASK, PCIE_ISR0_REG); 158062306a36Sopenharmony_ci 158162306a36Sopenharmony_ci /* 158262306a36Sopenharmony_ci * PCIE_MSG_LOG_REG contains the last inbound message, so store 158362306a36Sopenharmony_ci * the requester ID only when PME was not asserted yet. 158462306a36Sopenharmony_ci * Also do not trigger PME interrupt when PME is still asserted. 158562306a36Sopenharmony_ci */ 158662306a36Sopenharmony_ci if (!(le32_to_cpu(pcie->bridge.pcie_conf.rootsta) & PCI_EXP_RTSTA_PME)) { 158762306a36Sopenharmony_ci pcie->bridge.pcie_conf.rootsta = cpu_to_le32(requester | PCI_EXP_RTSTA_PME); 158862306a36Sopenharmony_ci 158962306a36Sopenharmony_ci /* 159062306a36Sopenharmony_ci * Trigger PME interrupt only if PMEIE bit in Root Control is set. 159162306a36Sopenharmony_ci * Aardvark HW returns zero for PCI_EXP_FLAGS_IRQ, so use PCIe interrupt 0. 159262306a36Sopenharmony_ci */ 159362306a36Sopenharmony_ci if (!(le16_to_cpu(pcie->bridge.pcie_conf.rootctl) & PCI_EXP_RTCTL_PMEIE)) 159462306a36Sopenharmony_ci return; 159562306a36Sopenharmony_ci 159662306a36Sopenharmony_ci if (generic_handle_domain_irq(pcie->rp_irq_domain, 0) == -EINVAL) 159762306a36Sopenharmony_ci dev_err_ratelimited(&pcie->pdev->dev, "unhandled PME IRQ\n"); 159862306a36Sopenharmony_ci } 159962306a36Sopenharmony_ci} 160062306a36Sopenharmony_ci 160162306a36Sopenharmony_cistatic void advk_pcie_handle_msi(struct advk_pcie *pcie) 160262306a36Sopenharmony_ci{ 160362306a36Sopenharmony_ci u32 msi_val, msi_mask, msi_status, msi_idx; 160462306a36Sopenharmony_ci 160562306a36Sopenharmony_ci msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG); 160662306a36Sopenharmony_ci msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG); 160762306a36Sopenharmony_ci msi_status = msi_val & ((~msi_mask) & PCIE_MSI_ALL_MASK); 160862306a36Sopenharmony_ci 160962306a36Sopenharmony_ci for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) { 161062306a36Sopenharmony_ci if (!(BIT(msi_idx) & msi_status)) 161162306a36Sopenharmony_ci continue; 161262306a36Sopenharmony_ci 161362306a36Sopenharmony_ci advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG); 161462306a36Sopenharmony_ci if (generic_handle_domain_irq(pcie->msi_inner_domain, msi_idx) == -EINVAL) 161562306a36Sopenharmony_ci dev_err_ratelimited(&pcie->pdev->dev, "unexpected MSI 0x%02x\n", msi_idx); 161662306a36Sopenharmony_ci } 161762306a36Sopenharmony_ci 161862306a36Sopenharmony_ci advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING, 161962306a36Sopenharmony_ci PCIE_ISR0_REG); 162062306a36Sopenharmony_ci} 162162306a36Sopenharmony_ci 162262306a36Sopenharmony_cistatic void advk_pcie_handle_int(struct advk_pcie *pcie) 162362306a36Sopenharmony_ci{ 162462306a36Sopenharmony_ci u32 isr0_val, isr0_mask, isr0_status; 162562306a36Sopenharmony_ci u32 isr1_val, isr1_mask, isr1_status; 162662306a36Sopenharmony_ci int i; 162762306a36Sopenharmony_ci 162862306a36Sopenharmony_ci isr0_val = advk_readl(pcie, PCIE_ISR0_REG); 162962306a36Sopenharmony_ci isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG); 163062306a36Sopenharmony_ci isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK); 163162306a36Sopenharmony_ci 163262306a36Sopenharmony_ci isr1_val = advk_readl(pcie, PCIE_ISR1_REG); 163362306a36Sopenharmony_ci isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG); 163462306a36Sopenharmony_ci isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK); 163562306a36Sopenharmony_ci 163662306a36Sopenharmony_ci /* Process PME interrupt as the first one to do not miss PME requester id */ 163762306a36Sopenharmony_ci if (isr0_status & PCIE_MSG_PM_PME_MASK) 163862306a36Sopenharmony_ci advk_pcie_handle_pme(pcie); 163962306a36Sopenharmony_ci 164062306a36Sopenharmony_ci /* Process ERR interrupt */ 164162306a36Sopenharmony_ci if (isr0_status & PCIE_ISR0_ERR_MASK) { 164262306a36Sopenharmony_ci advk_writel(pcie, PCIE_ISR0_ERR_MASK, PCIE_ISR0_REG); 164362306a36Sopenharmony_ci 164462306a36Sopenharmony_ci /* 164562306a36Sopenharmony_ci * Aardvark HW returns zero for PCI_ERR_ROOT_AER_IRQ, so use 164662306a36Sopenharmony_ci * PCIe interrupt 0 164762306a36Sopenharmony_ci */ 164862306a36Sopenharmony_ci if (generic_handle_domain_irq(pcie->rp_irq_domain, 0) == -EINVAL) 164962306a36Sopenharmony_ci dev_err_ratelimited(&pcie->pdev->dev, "unhandled ERR IRQ\n"); 165062306a36Sopenharmony_ci } 165162306a36Sopenharmony_ci 165262306a36Sopenharmony_ci /* Process MSI interrupts */ 165362306a36Sopenharmony_ci if (isr0_status & PCIE_ISR0_MSI_INT_PENDING) 165462306a36Sopenharmony_ci advk_pcie_handle_msi(pcie); 165562306a36Sopenharmony_ci 165662306a36Sopenharmony_ci /* Process legacy interrupts */ 165762306a36Sopenharmony_ci for (i = 0; i < PCI_NUM_INTX; i++) { 165862306a36Sopenharmony_ci if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i))) 165962306a36Sopenharmony_ci continue; 166062306a36Sopenharmony_ci 166162306a36Sopenharmony_ci advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i), 166262306a36Sopenharmony_ci PCIE_ISR1_REG); 166362306a36Sopenharmony_ci 166462306a36Sopenharmony_ci if (generic_handle_domain_irq(pcie->irq_domain, i) == -EINVAL) 166562306a36Sopenharmony_ci dev_err_ratelimited(&pcie->pdev->dev, "unexpected INT%c IRQ\n", 166662306a36Sopenharmony_ci (char)i + 'A'); 166762306a36Sopenharmony_ci } 166862306a36Sopenharmony_ci} 166962306a36Sopenharmony_ci 167062306a36Sopenharmony_cistatic irqreturn_t advk_pcie_irq_handler(int irq, void *arg) 167162306a36Sopenharmony_ci{ 167262306a36Sopenharmony_ci struct advk_pcie *pcie = arg; 167362306a36Sopenharmony_ci u32 status; 167462306a36Sopenharmony_ci 167562306a36Sopenharmony_ci status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG); 167662306a36Sopenharmony_ci if (!(status & PCIE_IRQ_CORE_INT)) 167762306a36Sopenharmony_ci return IRQ_NONE; 167862306a36Sopenharmony_ci 167962306a36Sopenharmony_ci advk_pcie_handle_int(pcie); 168062306a36Sopenharmony_ci 168162306a36Sopenharmony_ci /* Clear interrupt */ 168262306a36Sopenharmony_ci advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG); 168362306a36Sopenharmony_ci 168462306a36Sopenharmony_ci return IRQ_HANDLED; 168562306a36Sopenharmony_ci} 168662306a36Sopenharmony_ci 168762306a36Sopenharmony_cistatic int advk_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin) 168862306a36Sopenharmony_ci{ 168962306a36Sopenharmony_ci struct advk_pcie *pcie = dev->bus->sysdata; 169062306a36Sopenharmony_ci 169162306a36Sopenharmony_ci /* 169262306a36Sopenharmony_ci * Emulated root bridge has its own emulated irq chip and irq domain. 169362306a36Sopenharmony_ci * Argument pin is the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD) and 169462306a36Sopenharmony_ci * hwirq for irq_create_mapping() is indexed from zero. 169562306a36Sopenharmony_ci */ 169662306a36Sopenharmony_ci if (pci_is_root_bus(dev->bus)) 169762306a36Sopenharmony_ci return irq_create_mapping(pcie->rp_irq_domain, pin - 1); 169862306a36Sopenharmony_ci else 169962306a36Sopenharmony_ci return of_irq_parse_and_map_pci(dev, slot, pin); 170062306a36Sopenharmony_ci} 170162306a36Sopenharmony_ci 170262306a36Sopenharmony_cistatic void advk_pcie_disable_phy(struct advk_pcie *pcie) 170362306a36Sopenharmony_ci{ 170462306a36Sopenharmony_ci phy_power_off(pcie->phy); 170562306a36Sopenharmony_ci phy_exit(pcie->phy); 170662306a36Sopenharmony_ci} 170762306a36Sopenharmony_ci 170862306a36Sopenharmony_cistatic int advk_pcie_enable_phy(struct advk_pcie *pcie) 170962306a36Sopenharmony_ci{ 171062306a36Sopenharmony_ci int ret; 171162306a36Sopenharmony_ci 171262306a36Sopenharmony_ci if (!pcie->phy) 171362306a36Sopenharmony_ci return 0; 171462306a36Sopenharmony_ci 171562306a36Sopenharmony_ci ret = phy_init(pcie->phy); 171662306a36Sopenharmony_ci if (ret) 171762306a36Sopenharmony_ci return ret; 171862306a36Sopenharmony_ci 171962306a36Sopenharmony_ci ret = phy_set_mode(pcie->phy, PHY_MODE_PCIE); 172062306a36Sopenharmony_ci if (ret) { 172162306a36Sopenharmony_ci phy_exit(pcie->phy); 172262306a36Sopenharmony_ci return ret; 172362306a36Sopenharmony_ci } 172462306a36Sopenharmony_ci 172562306a36Sopenharmony_ci ret = phy_power_on(pcie->phy); 172662306a36Sopenharmony_ci if (ret) { 172762306a36Sopenharmony_ci phy_exit(pcie->phy); 172862306a36Sopenharmony_ci return ret; 172962306a36Sopenharmony_ci } 173062306a36Sopenharmony_ci 173162306a36Sopenharmony_ci return 0; 173262306a36Sopenharmony_ci} 173362306a36Sopenharmony_ci 173462306a36Sopenharmony_cistatic int advk_pcie_setup_phy(struct advk_pcie *pcie) 173562306a36Sopenharmony_ci{ 173662306a36Sopenharmony_ci struct device *dev = &pcie->pdev->dev; 173762306a36Sopenharmony_ci struct device_node *node = dev->of_node; 173862306a36Sopenharmony_ci int ret = 0; 173962306a36Sopenharmony_ci 174062306a36Sopenharmony_ci pcie->phy = devm_of_phy_get(dev, node, NULL); 174162306a36Sopenharmony_ci if (IS_ERR(pcie->phy) && (PTR_ERR(pcie->phy) == -EPROBE_DEFER)) 174262306a36Sopenharmony_ci return PTR_ERR(pcie->phy); 174362306a36Sopenharmony_ci 174462306a36Sopenharmony_ci /* Old bindings miss the PHY handle */ 174562306a36Sopenharmony_ci if (IS_ERR(pcie->phy)) { 174662306a36Sopenharmony_ci dev_warn(dev, "PHY unavailable (%ld)\n", PTR_ERR(pcie->phy)); 174762306a36Sopenharmony_ci pcie->phy = NULL; 174862306a36Sopenharmony_ci return 0; 174962306a36Sopenharmony_ci } 175062306a36Sopenharmony_ci 175162306a36Sopenharmony_ci ret = advk_pcie_enable_phy(pcie); 175262306a36Sopenharmony_ci if (ret) 175362306a36Sopenharmony_ci dev_err(dev, "Failed to initialize PHY (%d)\n", ret); 175462306a36Sopenharmony_ci 175562306a36Sopenharmony_ci return ret; 175662306a36Sopenharmony_ci} 175762306a36Sopenharmony_ci 175862306a36Sopenharmony_cistatic int advk_pcie_probe(struct platform_device *pdev) 175962306a36Sopenharmony_ci{ 176062306a36Sopenharmony_ci struct device *dev = &pdev->dev; 176162306a36Sopenharmony_ci struct advk_pcie *pcie; 176262306a36Sopenharmony_ci struct pci_host_bridge *bridge; 176362306a36Sopenharmony_ci struct resource_entry *entry; 176462306a36Sopenharmony_ci int ret, irq; 176562306a36Sopenharmony_ci 176662306a36Sopenharmony_ci bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie)); 176762306a36Sopenharmony_ci if (!bridge) 176862306a36Sopenharmony_ci return -ENOMEM; 176962306a36Sopenharmony_ci 177062306a36Sopenharmony_ci pcie = pci_host_bridge_priv(bridge); 177162306a36Sopenharmony_ci pcie->pdev = pdev; 177262306a36Sopenharmony_ci platform_set_drvdata(pdev, pcie); 177362306a36Sopenharmony_ci 177462306a36Sopenharmony_ci resource_list_for_each_entry(entry, &bridge->windows) { 177562306a36Sopenharmony_ci resource_size_t start = entry->res->start; 177662306a36Sopenharmony_ci resource_size_t size = resource_size(entry->res); 177762306a36Sopenharmony_ci unsigned long type = resource_type(entry->res); 177862306a36Sopenharmony_ci u64 win_size; 177962306a36Sopenharmony_ci 178062306a36Sopenharmony_ci /* 178162306a36Sopenharmony_ci * Aardvark hardware allows to configure also PCIe window 178262306a36Sopenharmony_ci * for config type 0 and type 1 mapping, but driver uses 178362306a36Sopenharmony_ci * only PIO for issuing configuration transfers which does 178462306a36Sopenharmony_ci * not use PCIe window configuration. 178562306a36Sopenharmony_ci */ 178662306a36Sopenharmony_ci if (type != IORESOURCE_MEM && type != IORESOURCE_IO) 178762306a36Sopenharmony_ci continue; 178862306a36Sopenharmony_ci 178962306a36Sopenharmony_ci /* 179062306a36Sopenharmony_ci * Skip transparent memory resources. Default outbound access 179162306a36Sopenharmony_ci * configuration is set to transparent memory access so it 179262306a36Sopenharmony_ci * does not need window configuration. 179362306a36Sopenharmony_ci */ 179462306a36Sopenharmony_ci if (type == IORESOURCE_MEM && entry->offset == 0) 179562306a36Sopenharmony_ci continue; 179662306a36Sopenharmony_ci 179762306a36Sopenharmony_ci /* 179862306a36Sopenharmony_ci * The n-th PCIe window is configured by tuple (match, remap, mask) 179962306a36Sopenharmony_ci * and an access to address A uses this window if A matches the 180062306a36Sopenharmony_ci * match with given mask. 180162306a36Sopenharmony_ci * So every PCIe window size must be a power of two and every start 180262306a36Sopenharmony_ci * address must be aligned to window size. Minimal size is 64 KiB 180362306a36Sopenharmony_ci * because lower 16 bits of mask must be zero. Remapped address 180462306a36Sopenharmony_ci * may have set only bits from the mask. 180562306a36Sopenharmony_ci */ 180662306a36Sopenharmony_ci while (pcie->wins_count < OB_WIN_COUNT && size > 0) { 180762306a36Sopenharmony_ci /* Calculate the largest aligned window size */ 180862306a36Sopenharmony_ci win_size = (1ULL << (fls64(size)-1)) | 180962306a36Sopenharmony_ci (start ? (1ULL << __ffs64(start)) : 0); 181062306a36Sopenharmony_ci win_size = 1ULL << __ffs64(win_size); 181162306a36Sopenharmony_ci if (win_size < 0x10000) 181262306a36Sopenharmony_ci break; 181362306a36Sopenharmony_ci 181462306a36Sopenharmony_ci dev_dbg(dev, 181562306a36Sopenharmony_ci "Configuring PCIe window %d: [0x%llx-0x%llx] as %lu\n", 181662306a36Sopenharmony_ci pcie->wins_count, (unsigned long long)start, 181762306a36Sopenharmony_ci (unsigned long long)start + win_size, type); 181862306a36Sopenharmony_ci 181962306a36Sopenharmony_ci if (type == IORESOURCE_IO) { 182062306a36Sopenharmony_ci pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_IO; 182162306a36Sopenharmony_ci pcie->wins[pcie->wins_count].match = pci_pio_to_address(start); 182262306a36Sopenharmony_ci } else { 182362306a36Sopenharmony_ci pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_MEM; 182462306a36Sopenharmony_ci pcie->wins[pcie->wins_count].match = start; 182562306a36Sopenharmony_ci } 182662306a36Sopenharmony_ci pcie->wins[pcie->wins_count].remap = start - entry->offset; 182762306a36Sopenharmony_ci pcie->wins[pcie->wins_count].mask = ~(win_size - 1); 182862306a36Sopenharmony_ci 182962306a36Sopenharmony_ci if (pcie->wins[pcie->wins_count].remap & (win_size - 1)) 183062306a36Sopenharmony_ci break; 183162306a36Sopenharmony_ci 183262306a36Sopenharmony_ci start += win_size; 183362306a36Sopenharmony_ci size -= win_size; 183462306a36Sopenharmony_ci pcie->wins_count++; 183562306a36Sopenharmony_ci } 183662306a36Sopenharmony_ci 183762306a36Sopenharmony_ci if (size > 0) { 183862306a36Sopenharmony_ci dev_err(&pcie->pdev->dev, 183962306a36Sopenharmony_ci "Invalid PCIe region [0x%llx-0x%llx]\n", 184062306a36Sopenharmony_ci (unsigned long long)entry->res->start, 184162306a36Sopenharmony_ci (unsigned long long)entry->res->end + 1); 184262306a36Sopenharmony_ci return -EINVAL; 184362306a36Sopenharmony_ci } 184462306a36Sopenharmony_ci } 184562306a36Sopenharmony_ci 184662306a36Sopenharmony_ci pcie->base = devm_platform_ioremap_resource(pdev, 0); 184762306a36Sopenharmony_ci if (IS_ERR(pcie->base)) 184862306a36Sopenharmony_ci return PTR_ERR(pcie->base); 184962306a36Sopenharmony_ci 185062306a36Sopenharmony_ci irq = platform_get_irq(pdev, 0); 185162306a36Sopenharmony_ci if (irq < 0) 185262306a36Sopenharmony_ci return irq; 185362306a36Sopenharmony_ci 185462306a36Sopenharmony_ci ret = devm_request_irq(dev, irq, advk_pcie_irq_handler, 185562306a36Sopenharmony_ci IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie", 185662306a36Sopenharmony_ci pcie); 185762306a36Sopenharmony_ci if (ret) { 185862306a36Sopenharmony_ci dev_err(dev, "Failed to register interrupt\n"); 185962306a36Sopenharmony_ci return ret; 186062306a36Sopenharmony_ci } 186162306a36Sopenharmony_ci 186262306a36Sopenharmony_ci pcie->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); 186362306a36Sopenharmony_ci ret = PTR_ERR_OR_ZERO(pcie->reset_gpio); 186462306a36Sopenharmony_ci if (ret) { 186562306a36Sopenharmony_ci if (ret != -EPROBE_DEFER) 186662306a36Sopenharmony_ci dev_err(dev, "Failed to get reset-gpio: %i\n", ret); 186762306a36Sopenharmony_ci return ret; 186862306a36Sopenharmony_ci } 186962306a36Sopenharmony_ci 187062306a36Sopenharmony_ci ret = gpiod_set_consumer_name(pcie->reset_gpio, "pcie1-reset"); 187162306a36Sopenharmony_ci if (ret) { 187262306a36Sopenharmony_ci dev_err(dev, "Failed to set reset gpio name: %d\n", ret); 187362306a36Sopenharmony_ci return ret; 187462306a36Sopenharmony_ci } 187562306a36Sopenharmony_ci 187662306a36Sopenharmony_ci ret = of_pci_get_max_link_speed(dev->of_node); 187762306a36Sopenharmony_ci if (ret <= 0 || ret > 3) 187862306a36Sopenharmony_ci pcie->link_gen = 3; 187962306a36Sopenharmony_ci else 188062306a36Sopenharmony_ci pcie->link_gen = ret; 188162306a36Sopenharmony_ci 188262306a36Sopenharmony_ci ret = advk_pcie_setup_phy(pcie); 188362306a36Sopenharmony_ci if (ret) 188462306a36Sopenharmony_ci return ret; 188562306a36Sopenharmony_ci 188662306a36Sopenharmony_ci advk_pcie_setup_hw(pcie); 188762306a36Sopenharmony_ci 188862306a36Sopenharmony_ci ret = advk_sw_pci_bridge_init(pcie); 188962306a36Sopenharmony_ci if (ret) { 189062306a36Sopenharmony_ci dev_err(dev, "Failed to register emulated root PCI bridge\n"); 189162306a36Sopenharmony_ci return ret; 189262306a36Sopenharmony_ci } 189362306a36Sopenharmony_ci 189462306a36Sopenharmony_ci ret = advk_pcie_init_irq_domain(pcie); 189562306a36Sopenharmony_ci if (ret) { 189662306a36Sopenharmony_ci dev_err(dev, "Failed to initialize irq\n"); 189762306a36Sopenharmony_ci return ret; 189862306a36Sopenharmony_ci } 189962306a36Sopenharmony_ci 190062306a36Sopenharmony_ci ret = advk_pcie_init_msi_irq_domain(pcie); 190162306a36Sopenharmony_ci if (ret) { 190262306a36Sopenharmony_ci dev_err(dev, "Failed to initialize irq\n"); 190362306a36Sopenharmony_ci advk_pcie_remove_irq_domain(pcie); 190462306a36Sopenharmony_ci return ret; 190562306a36Sopenharmony_ci } 190662306a36Sopenharmony_ci 190762306a36Sopenharmony_ci ret = advk_pcie_init_rp_irq_domain(pcie); 190862306a36Sopenharmony_ci if (ret) { 190962306a36Sopenharmony_ci dev_err(dev, "Failed to initialize irq\n"); 191062306a36Sopenharmony_ci advk_pcie_remove_msi_irq_domain(pcie); 191162306a36Sopenharmony_ci advk_pcie_remove_irq_domain(pcie); 191262306a36Sopenharmony_ci return ret; 191362306a36Sopenharmony_ci } 191462306a36Sopenharmony_ci 191562306a36Sopenharmony_ci bridge->sysdata = pcie; 191662306a36Sopenharmony_ci bridge->ops = &advk_pcie_ops; 191762306a36Sopenharmony_ci bridge->map_irq = advk_pcie_map_irq; 191862306a36Sopenharmony_ci 191962306a36Sopenharmony_ci ret = pci_host_probe(bridge); 192062306a36Sopenharmony_ci if (ret < 0) { 192162306a36Sopenharmony_ci advk_pcie_remove_rp_irq_domain(pcie); 192262306a36Sopenharmony_ci advk_pcie_remove_msi_irq_domain(pcie); 192362306a36Sopenharmony_ci advk_pcie_remove_irq_domain(pcie); 192462306a36Sopenharmony_ci return ret; 192562306a36Sopenharmony_ci } 192662306a36Sopenharmony_ci 192762306a36Sopenharmony_ci return 0; 192862306a36Sopenharmony_ci} 192962306a36Sopenharmony_ci 193062306a36Sopenharmony_cistatic void advk_pcie_remove(struct platform_device *pdev) 193162306a36Sopenharmony_ci{ 193262306a36Sopenharmony_ci struct advk_pcie *pcie = platform_get_drvdata(pdev); 193362306a36Sopenharmony_ci struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); 193462306a36Sopenharmony_ci u32 val; 193562306a36Sopenharmony_ci int i; 193662306a36Sopenharmony_ci 193762306a36Sopenharmony_ci /* Remove PCI bus with all devices */ 193862306a36Sopenharmony_ci pci_lock_rescan_remove(); 193962306a36Sopenharmony_ci pci_stop_root_bus(bridge->bus); 194062306a36Sopenharmony_ci pci_remove_root_bus(bridge->bus); 194162306a36Sopenharmony_ci pci_unlock_rescan_remove(); 194262306a36Sopenharmony_ci 194362306a36Sopenharmony_ci /* Disable Root Bridge I/O space, memory space and bus mastering */ 194462306a36Sopenharmony_ci val = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG); 194562306a36Sopenharmony_ci val &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); 194662306a36Sopenharmony_ci advk_writel(pcie, val, PCIE_CORE_CMD_STATUS_REG); 194762306a36Sopenharmony_ci 194862306a36Sopenharmony_ci /* Disable MSI */ 194962306a36Sopenharmony_ci val = advk_readl(pcie, PCIE_CORE_CTRL2_REG); 195062306a36Sopenharmony_ci val &= ~PCIE_CORE_CTRL2_MSI_ENABLE; 195162306a36Sopenharmony_ci advk_writel(pcie, val, PCIE_CORE_CTRL2_REG); 195262306a36Sopenharmony_ci 195362306a36Sopenharmony_ci /* Clear MSI address */ 195462306a36Sopenharmony_ci advk_writel(pcie, 0, PCIE_MSI_ADDR_LOW_REG); 195562306a36Sopenharmony_ci advk_writel(pcie, 0, PCIE_MSI_ADDR_HIGH_REG); 195662306a36Sopenharmony_ci 195762306a36Sopenharmony_ci /* Mask all interrupts */ 195862306a36Sopenharmony_ci advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG); 195962306a36Sopenharmony_ci advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_MASK_REG); 196062306a36Sopenharmony_ci advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG); 196162306a36Sopenharmony_ci advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_MASK_REG); 196262306a36Sopenharmony_ci 196362306a36Sopenharmony_ci /* Clear all interrupts */ 196462306a36Sopenharmony_ci advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG); 196562306a36Sopenharmony_ci advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG); 196662306a36Sopenharmony_ci advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG); 196762306a36Sopenharmony_ci advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG); 196862306a36Sopenharmony_ci 196962306a36Sopenharmony_ci /* Remove IRQ domains */ 197062306a36Sopenharmony_ci advk_pcie_remove_rp_irq_domain(pcie); 197162306a36Sopenharmony_ci advk_pcie_remove_msi_irq_domain(pcie); 197262306a36Sopenharmony_ci advk_pcie_remove_irq_domain(pcie); 197362306a36Sopenharmony_ci 197462306a36Sopenharmony_ci /* Free config space for emulated root bridge */ 197562306a36Sopenharmony_ci pci_bridge_emul_cleanup(&pcie->bridge); 197662306a36Sopenharmony_ci 197762306a36Sopenharmony_ci /* Assert PERST# signal which prepares PCIe card for power down */ 197862306a36Sopenharmony_ci if (pcie->reset_gpio) 197962306a36Sopenharmony_ci gpiod_set_value_cansleep(pcie->reset_gpio, 1); 198062306a36Sopenharmony_ci 198162306a36Sopenharmony_ci /* Disable link training */ 198262306a36Sopenharmony_ci val = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 198362306a36Sopenharmony_ci val &= ~LINK_TRAINING_EN; 198462306a36Sopenharmony_ci advk_writel(pcie, val, PCIE_CORE_CTRL0_REG); 198562306a36Sopenharmony_ci 198662306a36Sopenharmony_ci /* Disable outbound address windows mapping */ 198762306a36Sopenharmony_ci for (i = 0; i < OB_WIN_COUNT; i++) 198862306a36Sopenharmony_ci advk_pcie_disable_ob_win(pcie, i); 198962306a36Sopenharmony_ci 199062306a36Sopenharmony_ci /* Disable phy */ 199162306a36Sopenharmony_ci advk_pcie_disable_phy(pcie); 199262306a36Sopenharmony_ci} 199362306a36Sopenharmony_ci 199462306a36Sopenharmony_cistatic const struct of_device_id advk_pcie_of_match_table[] = { 199562306a36Sopenharmony_ci { .compatible = "marvell,armada-3700-pcie", }, 199662306a36Sopenharmony_ci {}, 199762306a36Sopenharmony_ci}; 199862306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, advk_pcie_of_match_table); 199962306a36Sopenharmony_ci 200062306a36Sopenharmony_cistatic struct platform_driver advk_pcie_driver = { 200162306a36Sopenharmony_ci .driver = { 200262306a36Sopenharmony_ci .name = "advk-pcie", 200362306a36Sopenharmony_ci .of_match_table = advk_pcie_of_match_table, 200462306a36Sopenharmony_ci }, 200562306a36Sopenharmony_ci .probe = advk_pcie_probe, 200662306a36Sopenharmony_ci .remove_new = advk_pcie_remove, 200762306a36Sopenharmony_ci}; 200862306a36Sopenharmony_cimodule_platform_driver(advk_pcie_driver); 200962306a36Sopenharmony_ci 201062306a36Sopenharmony_ciMODULE_DESCRIPTION("Aardvark PCIe controller"); 201162306a36Sopenharmony_ciMODULE_LICENSE("GPL v2"); 2012