Home | History | Annotate | Download | only in PcieInitDxe
      1 /** @file
      2 *
      3 *  Copyright (c) 2016, Hisilicon Limited. All rights reserved.
      4 *  Copyright (c) 2016, Linaro Limited. All rights reserved.
      5 *
      6 *  This program and the accompanying materials
      7 *  are licensed and made available under the terms and conditions of the BSD License
      8 *  which accompanies this distribution.  The full text of the license may be found at
      9 *  http://opensource.org/licenses/bsd-license.php
     10 *
     11 *  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     12 *  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     13 *
     14 **/
     15 
     16 #ifndef __PCIE_INIT_LIB_H__
     17 #define __PCIE_INIT_LIB_H__
     18 
     19 #include <Uefi.h>
     20 #include <Regs/HisiPcieV1RegOffset.h>
     21 #include "PcieKernelApi.h"
     22 
     23 #define PCIE_AXI_SLAVE_BASE             (0xb3000000)
     24 #define PCIE_MAX_AXI_SIZE               (0x1000000)
     25 #define PCIE_AXI_BASE(port)             (PCIE_AXI_SLAVE_BASE + port * PCIE_MAX_AXI_SIZE)
     26 #define PCIE_SMMU_BASE                  (0xb0040000)
     27 
     28 
     29 #define PCIE_DMA_CHANNEL_NUM            (2)
     30 #define PCIE_DMA_RESOURCE_MODE_SIZE     (0x40000)
     31 #define PCIE_DMA_BURST_SIZE             (0x80000000)
     32 
     33 #define PCIE_ADDR_BASE_OFFSET                       0x46C00000
     34 #define PCIE_ADDR_BASE_HOST_ADDR                    (PCIE_ADDR_BASE_OFFSET + NP_DDR_BASE_ADDR_HOST)
     35 #define NP_DDR_BASE_ADDR_HOST                       0x236E00000ULL
     36 
     37 
     38 
     39 #define PCIE_GIC_MSI_ITS_BASE       (0xb7010040)
     40 #define PCIE_INT_BASE               (13824)
     41 #define PCIE_INT_LIMIT              (PCIE_INT_BASE + 64)
     42 
     43 #define PCIE_NTB_MEM_SIZE             (0x1000000)
     44 #define PCIE_NTB_BAR01_SIZE           (0x10000) // 64K
     45 #define PCIE_NTB_BAR23_SIZE           (0x800000) // 8M
     46 #define PCIE_NTB_BAR45_SIZE           (0x800000)
     47 
     48 #define PCIE_IATU_END               {PCIE_IATU_OUTBOUND,0,0,0}
     49 #define PCIE_IATU_INBOUND_MASK      (0x80000000)
     50 #define PCIE_IATU_INDEX_MASK         (0x7f)
     51 #define PCIE_IATU_TYPE_MASK         (0x1f)
     52 #define PCIE_IATU_EN                     (0x1 << 0)
     53 #define PCIE_IATU_SHIFT_MODE             (0x1 << 1)
     54 #define PCIE_IATU_BAR_MODE               (0x1 << 2)
     55 #define PCIE_IATU_FUNC_MODE               (0x1 << 3)
     56 #define PCIE_IATU_AT_MODE                 (0x1 << 4)
     57 #define PCIE_IATU_ATTR_MODE               (0x1 << 5)
     58 #define PCIE_IATU_TD_MODE                 (0x1 << 6) //TD
     59 #define PCIE_IATU_TC_MODE                 (0x1 << 7) // TC
     60 #define PCIE_IATU_PREFETCH_MODE             (0x1 << 8)
     61 #define PCIE_IATU_DMA_BY_PASS_MODE          (0x1 << 9) //DMA bypass untranslate
     62 
     63 #define PCIE_BAR_MASK_SIZE          (0x800000)
     64 #define PCIE_BAR_TYPE_32            (0)
     65 #define PCIE_BAR_TYPE_64            (2)
     66 #define PCIE_BAR_PREFETCH_MODE      (1)
     67 
     68 #define RegWrite(addr,data)            (*(volatile UINT32*)(UINTN)(addr) = (data))
     69 #define RegRead(addr,data)             ((data) = *(volatile UINT32*)(UINTN)(addr))
     70 
     71 
     72 typedef struct tagPcieDebugInfo
     73 {
     74     UINT32 pcie_rdma_start_cnt;
     75     UINT32 pcie_wdma_start_cnt;
     76     UINT64 pcie_wdma_transfer_len;
     77     UINT64 pcie_rdma_transfer_len;
     78     UINT32 pcie_rdma_fail_cnt;
     79     UINT32 pcie_wdma_fail_cnt;
     80 }pcie_debug_info_s;
     81 
     82 
     83 #define bdf_2_b(bdf)    ((bdf >> 8) & 0xFF)
     84 #define bdf_2_d(bdf)    ((bdf >> 3) & 0x1F)
     85 #define bdf_2_f(bdf)    ((bdf >> 0) & 0x7)
     86 #define b_d_f_2_bdf(b,d,f)    (((b & 0xff) << 8 ) | ((d & 0x1f) << 3) | ((f & 0x7) << 0))
     87 
     88 
     89 
     90 typedef UINT32 (*pcie_dma_func_int)(UINT32  ulErrno, UINT32 ulReserved);
     91 
     92 
     93 typedef struct {
     94      UINT32             ViewPort;            //iATU Viewport Register
     95      UINT32             RegionCtrl1;         //Region Control 1 Register
     96      UINT32             RegionCtrl2;         //Region Control 2 Register
     97      UINT32             BaseLow;             //Lower Base Address Register
     98      UINT32             BaseHigh;            //Upper Base Address Register
     99      UINT32             Limit;               //Limit Address Register
    100      UINT32             TargetLow;           //Lower Target Address Register
    101      UINT32             TargetHigh;          //Upper Target Address Register
    102 } PCIE_IATU_VA;
    103 
    104 typedef enum {
    105     PCIE_IATU_OUTBOUND    = 0x0,
    106     PCIE_IATU_INBOUND     = 0x1,
    107 } PCIE_IATU_DIR;
    108 
    109 typedef struct {
    110     PCIE_IATU_DIR       IatuType;
    111     UINT64              IatuBase;
    112     UINT64              IatuSize;
    113     UINT64              IatuTarget;
    114 } PCIE_IATU;
    115 
    116 typedef struct {
    117     UINT32              IatuType;
    118     UINT64              IatuBase;
    119     UINT32              IatuLimit;
    120     UINT64              IatuTarget;
    121     UINT32              Valid;
    122 } PCIE_IATU_HW;
    123 
    124 typedef struct {
    125     UINT32              PortIndex;
    126     PCIE_PORT_INFO      PortInfo;
    127     PCIE_IATU_HW        OutBound[PCIE_MAX_OUTBOUND];
    128     PCIE_IATU_HW        InBound[PCIE_MAX_INBOUND];
    129 } PCIE_DRIVER_CFG;
    130 
    131 typedef enum {
    132     PCIE_CONFIG_REG     = 0x0,
    133     PCIE_SYS_CONTROL    = 0x1,
    134     PCIE_SLV_CONTENT_MODE = 0x2,
    135 } PCIE_RW_MODE;
    136 
    137 typedef union {
    138     PCIE_DRIVER_CFG     PcieDevice;
    139     PCIE_NTB_CFG        NtbDevice;
    140 } DRIVER_CFG_U;
    141 
    142 typedef struct {
    143     VOID                *MappedOutbound[PCIE_MAX_OUTBOUND];
    144     UINT32              OutboundType[PCIE_MAX_OUTBOUND];
    145     UINT32              OutboundEn[PCIE_MAX_OUTBOUND];
    146 } PCIE_MAPPED_IATU_ADDR;
    147 
    148 typedef struct {
    149     BOOLEAN             PortIsInitilized[PCIE_MAX_PORT_NUM];
    150     DRIVER_CFG_U        Dev[PCIE_MAX_PORT_NUM];
    151     VOID                *DmaResource[PCIE_MAX_PORT_NUM];
    152     UINT32              DmaChannel[PCIE_MAX_PORT_NUM][2];
    153     VOID                *RegResource[PCIE_MAX_PORT_NUM];
    154     VOID                *CfgResource[PCIE_MAX_PORT_NUM];
    155 } PCIE_INIT_CFG;
    156 
    157 typedef enum {
    158     PCIE_MMIO_IEP_CFG  = 0x1000,
    159     PCIE_MMIO_IEP_CTRL = 0x0,
    160     PCIE_MMIO_EEP_CFG  = 0x9000,
    161     PCIE_MMIO_EEP_CTRL = 0x8000,
    162 } NTB_MMIO_MODE;
    163 
    164 typedef struct tagPcieDmaDes
    165 {
    166     UINT32 uwChanCtrl;
    167     UINT32 uwLen;
    168     UINT32 uwLocalLow;
    169     UINT32 uwLocalHigh;
    170     UINT32 uwTagetLow;
    171     UINT32 uwTagetHigh;
    172 }pcie_dma_des_s,*pcie_dma_des_ps;
    173 
    174 typedef enum {
    175     PCIE_IATU_MEM,
    176     PCIE_IATU_CFG = 0x4,
    177     PCIE_IATU_IO
    178 } PCIE_IATU_OUT_TYPE;
    179 
    180 typedef enum {
    181     PCIE_PAYLOAD_128B = 0,
    182     PCIE_PAYLOAD_256B,
    183     PCIE_PAYLOAD_512B,
    184     PCIE_PAYLOAD_1024B,
    185     PCIE_PAYLOAD_2048B,
    186     PCIE_PAYLOAD_4096B,
    187     PCIE_RESERVED_PAYLOAD
    188 } PCIE_PAYLOAD_SIZE;
    189 
    190 typedef struct tagPcieDfxInfo
    191 {
    192     PCIE_EP_AER_CAP0_U aer_cap0;
    193     PCIE_EP_AER_CAP1_U aer_cap1;
    194     PCIE_EP_AER_CAP2_U aer_cap2;
    195     PCIE_EP_AER_CAP3_U aer_cap3;
    196     PCIE_EP_AER_CAP4_U aer_cap4;
    197     PCIE_EP_AER_CAP5_U aer_cap5;
    198     PCIE_EP_AER_CAP6_U aer_cap6;
    199     UINT32 hdr_log0;
    200     UINT32 hdr_log1;
    201     UINT32 hdr_log2;
    202     UINT32 hdr_log3;
    203     PCIE_EP_AER_CAP11_U aer_cap11;
    204     PCIE_EP_AER_CAP12_U aer_cap12;
    205     PCIE_EP_AER_CAP13_U aer_cap13;
    206 
    207     PCIE_EP_PORTLOGIC62_U port_logic62;
    208     PCIE_EP_PORTLOGIC64_U port_logic64;
    209     PCIE_EP_PORTLOGIC66_U port_logic66;
    210     PCIE_EP_PORTLOGIC67_U port_logic67;
    211     PCIE_EP_PORTLOGIC69_U port_logic69;
    212     PCIE_EP_PORTLOGIC75_U port_logic75;
    213     PCIE_EP_PORTLOGIC76_U port_logic76;
    214     PCIE_EP_PORTLOGIC77_U port_logic77;
    215     PCIE_EP_PORTLOGIC79_U port_logic79;
    216     PCIE_EP_PORTLOGIC80_U port_logic80;
    217     PCIE_EP_PORTLOGIC81_U port_logic81;
    218     PCIE_EP_PORTLOGIC87_U port_logic87;
    219 
    220     PCIE_CTRL_10_U  pcie_ctrl10;
    221     UINT32 slve_rerr_addr_low;
    222     UINT32 slve_rerr_addr_up;
    223     UINT32 slve_werr_addr_low;
    224     UINT32 slve_werr_addr_up;
    225     UINT32 pcie_state4;
    226     UINT32 pcie_state5;
    227 }PCIE_DFX_INFO_S;
    228 
    229 VOID PcieChangeRwMode(UINT32 HostBridgeNum, UINT32 Port, PCIE_RW_MODE Mode);
    230 
    231 UINT32 PcieIsLinkDown(UINT32 Port);
    232 
    233 BOOLEAN PcieIsLinkUp(UINT32 HostBridgeNum, UINT32 Port);
    234 
    235 EFI_STATUS PcieWaitLinkUp(UINT32 Port);
    236 
    237 EFI_STATUS PcieSetDBICS2Enable(UINT32 HostBridgeNum, UINT32 Port, UINT32 Enable);
    238 
    239 #endif
    240