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