1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*
* Copyright : (C) 2022 Phytium Information Technology, Inc.
* All Rights Reserved.
*
* This program is OPEN SOURCE software: you can redistribute it and/or modify it
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
* either version 1.0 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Phytium Public License for more details.
*
*
* FilePath: fpcie_hw.c
* Date: 2022-02-10 14:55:11
* LastEditTime: 2022-02-18 08:58:12
* Description: This files is for
*
* Modify History:
* Ver Who Date Changes
* ----- ------ -------- --------------------------------------
*/
#include "fpcie_hw.h"
#include "fpcie.h"
#include "fpcie_common.h"
#include "fparameters.h"
/***************************** Include Files *********************************/
/************************** Constant Definitions *****************************/
/* Access sizes for PCI reads and writes */
enum FPcieSize
{
FPCIE_SIZE_8,
FPCIE_SIZE_16,
FPCIE_SIZE_32,
};
/**************************** Type Definitions *******************************/
/************************** Variable Definitions *****************************/
/***************** Macros (Inline Functions) Definitions *********************/
/************************** Function Prototypes ******************************/
/**
* @name: FPcieEcamConfigAddress
* @msg: 提供一个可用于ECAM 访问机制的地址
* @param {FPcie} *instance_p is a pointer to the FPcie instance.
* @param {s32} bdf {Bus Device function}
* @param {u32} offset 配置空间中的偏移量
* @param {void **} addrp 用于存放输出有效访问空间的地址
* @return FError
*/
static FError FPcieEcamConfigAddress(uintptr addr, s32 bdf, u32 offset, void **addrp)
{
u32 bus_no = FPCIE_BUS(bdf);
u32 dev_no = FPCIE_DEV(bdf);
u32 vendor = 0;
s32 bdf_parent ;
uintptr ecam_addr = addr;
addr += FPCIE_BUS(bdf) << 20;
addr += FPCIE_DEV(bdf) << 15;
addr += FPCIE_FUNC(bdf) << 12;
bdf_parent = FPCIE_BDF(bus_no - 1, 0, 0);
vendor = FPCIE_READREG(addr, FPCIE_CFG_ID_REG) ;
if ((bus_no > 0) && (dev_no > 0))
{
if ((FPCIE_READREG_BYTE(addr, FPCIE_HEADER_TYPE_REG) & 0x7f) != FPCIE_HEADER_TYPE_BRIDGE)
{
if (vendor != 0x1d17)
{
return FPCIE_NEED_SKIP;
}
if (FPcieSkipDevice(ecam_addr, bdf_parent))
{
return FPCIE_NEED_SKIP;
}
}
}
addr += offset ;
*addrp = (void *)addr ;
return FT_SUCCESS;
}
/**
* @name: FPcieSkipDevice
* @msg: 跳过部分无效设备
* @param {FPcie} *instance_p is a pointer to the FPcie instance.
* @param {s32} bdf {Bus Device function}
* @return FError
*/
FError FPcieSkipDevice(uintptr ecam_addr, s32 bdf)
{
u8 pos, id;
u16 capreg;
u8 port_type;
uintptr addr ;
addr = ecam_addr ;
addr += FPCIE_BUS(bdf) << 20;
addr += FPCIE_DEV(bdf) << 15;
addr += FPCIE_FUNC(bdf) << 12;
pos = 0x34 ; /* Capabilites Pointer */
while (1)
{
pos = FPCIE_READREG_BYTE(addr, pos) ;
if (pos < 0x40)/* 超过Capability Pointer所代表的空间offset最大范围 */
break;
pos &= ~3 ; /* offset 第两位对齐 */
id = FPCIE_READREG_BYTE(addr, pos) ; /* PCI Express Cap ID */
if (id == 0xff)
{
break ;
}
if (id == 0x10) /* 找到 PCIe设备的cap structure */
{
capreg = FPCIE_READREG(addr, pos + 2) ;
port_type = (capreg >> 4) & 0xf ; /* Device/Port type */
if ((port_type == 0x5) && (FPCIE_DEV(bdf) != 0))
{
return FPCIE_NEED_SKIP ;
}
else
{
return FT_SUCCESS;
}
}
pos + 1 ;
}
return FT_SUCCESS;
}
static s32 FPcieGetFf(enum FPcieSize size)
{
switch (size)
{
case FPCIE_SIZE_8:
return 0xff;
case FPCIE_SIZE_16:
return 0xffff;
default:
return 0xffffffff;
}
}
/**
* @name: FPcieEcamReadConfig8bit
* @msg: 基于ECAM机制读取配置空间中偏移量的值
* @param {FPcie} *instance_p is a pointer to the FPcie instance.
* @param {s32} bdf {Bus Device function}
* @param {u32} offset 配置空间中的偏移量
* @param {u8} *value_p pointer store date available in the offset
* @return FError
*/
void FPcieEcamReadConfig8bit(uintptr ecam_addr, s32 bdf, u32 offset, u8 *value_p)
{
uintptr addr ;
if (FPcieEcamConfigAddress(ecam_addr, bdf, offset, (void *)&addr) != FT_SUCCESS)
{
*value_p = FPcieGetFf(FPCIE_SIZE_8) ;
return ;
}
*value_p = FPCIE_READREG_BYTE(addr, 0);
return ;
}
/**
* @name: FPcieEcamReadConfig16bit
* @msg: 基于ECAM机制读取配置空间中偏移量的值
* @param {FPcie} *instance_p is a pointer to the FPcie instance.
* @param {s32} bdf {Bus Device function}
* @param {u32} offset 配置空间中的偏移量
* @param {u16} *value_p pointer store date available in the offset
* @return FError
*/
void FPcieEcamReadConfig16bit(uintptr ecam_addr, s32 bdf, u32 offset, u16 *value_p)
{
uintptr addr ;
if (FPcieEcamConfigAddress(ecam_addr, bdf, offset, (void *)&addr) != FT_SUCCESS)
{
*value_p = FPcieGetFf(FPCIE_SIZE_16) ;
return ;
}
*value_p = FPCIE_READREG_SHORT(addr, 0);
return ;
}
/**
* @name: FPcieEcamReadConfig32bit
* @msg: 基于ECAM机制读取配置空间中偏移量的值
* @param {FPcie} *instance_p is a pointer to the FPcie instance.
* @param {s32} bdf {Bus Device function}
* @param {u32} offset 配置空间中的偏移量
* @param {u32} *value_p pointer store date available in the offset
* @return FError
*/
void FPcieEcamReadConfig32bit(uintptr ecam_addr, s32 bdf, u32 offset, u32 *value_p)
{
uintptr addr ;
if (FPcieEcamConfigAddress(ecam_addr, bdf, offset, (void *)&addr) != FT_SUCCESS)
{
*value_p = FPcieGetFf(FPCIE_SIZE_32) ;
return ;
}
*value_p = FPCIE_READREG(addr, 0);
return ;
}
/**
* @name: FPcieEcamWriteConfig8bit
* @msg: 基于ECAM机制写入配置空间中偏移量的值
* @param {FPcie} *instance_p is a pointer to the FPcie instance.
* @param {s32} bdf {Bus Device function}
* @param {u32} offset 配置空间中的偏移量
* @param {u8} value to be written on to the offset
* @return FError
*/
void FPcieEcamWriteConfig8bit(uintptr ecam_addr, s32 bdf, u32 offset, u8 value)
{
uintptr addr ;
if (FPcieEcamConfigAddress(ecam_addr, bdf, offset, (void *)&addr) != FT_SUCCESS)
{
return ;
}
FPCIE_WRITEREG_BYTE(addr, 0, value) ;
return ;
}
/**
* @name: FPcieEcamWriteConfig16bit
* @msg: 基于ECAM机制写入配置空间中偏移量的值
* @param {FPcie} *instance_p is a pointer to the FPcie instance.
* @param {s32} bdf {Bus Device function}
* @param {u32} offset 配置空间中的偏移量
* @param {u16} value to be written on to the offset
* @return FError
*/
void FPcieEcamWriteConfig16bit(uintptr ecam_addr, s32 bdf, u32 offset, u16 value)
{
uintptr addr ;
if (FPcieEcamConfigAddress(ecam_addr, bdf, offset, (void *)&addr) != FT_SUCCESS)
{
return ;
}
FPCIE_WRITEREG_SHORT(addr, 0, value) ;
return ;
}
/**
* @name: FPcieEcamWriteConfig32bit
* @msg: 基于ECAM机制写入配置空间中偏移量的值
* @param {FPcie} *instance_p is a pointer to the FPcie instance.
* @param {s32} bdf {Bus Device function}
* @param {u32} offset 配置空间中的偏移量
* @param {u32} value to be written on to the offset
* @return FError
*/
void FPcieEcamWriteConfig32bit(uintptr ecam_addr, s32 bdf, u32 offset, u32 value)
{
uintptr addr ;
if (FPcieEcamConfigAddress(ecam_addr, bdf, offset, (void *)&addr) != FT_SUCCESS)
{
return ;
}
FPCIE_WRITEREG(addr, 0, value) ;
return ;
}
|