-
Notifications
You must be signed in to change notification settings - Fork 7
/
port.c
346 lines (292 loc) · 7.2 KB
/
port.c
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* peeknpoke North complex registers file
*
* Copyright (c) 2012, Intel Corporation.
* Hari Kanigeri <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "pnp_utils_inc.h"
#define PUNIT_PORT 0x04
#define PCI_READ_COMMAND 0x10
#define PCI_WRITE_COMMAND 0x11
#define PCI_CTRL_REG_EXT 0xD8
#define CUNIT_PATH "/sys/devices/pci0000:00/0000:00:00.0/config"
#define PORT_DEV "/dev/port"
#define PCI_DEV_PATH "/sys/devices/pci0000:00/%04x:00:%02x.%x/config"
/*
* This function reads from the North complex port register
*/
int read_nc_port(int reg, int port, int *ret_val)
{
static int fd = -1;
int cmd;
int ret, value;
if (fd < 0)
fd = open(CUNIT_PATH, O_RDWR | O_SYNC);
if (fd < 0) {
printf("open CUNIT_PATH ret = %d line %d\n", fd, __LINE__);
goto error;
}
cmd = reg & 0xFFFFFF00;
ret = lseek(fd, PCI_CTRL_REG_EXT, SEEK_SET);
if (ret < 0) {
printf("lseek failed ret = %d line %d\n", ret, __LINE__);
goto error;
}
ret = write(fd, &cmd, 4);
if (ret < 0) {
printf("write failed ret = %d line %d\n", ret, __LINE__);
goto error;
}
cmd = (PCI_READ_COMMAND << 24) | (port << 16) | ((reg & 0xFF) << 8) | (0xf0);
ret = lseek(fd, 0xD0, SEEK_SET);
if (ret < 0) {
printf("lseek failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
ret = write(fd, &cmd, 4);
if (ret < 0) {
printf("write failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
ret = lseek(fd, 0xD4, SEEK_SET);
if (ret < 0) {
printf("lseek failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
ret = read(fd, &value, 4);
if (ret < 0) {
printf("lseek failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
printf("Port 0x%x Register 0x%x value is 0x%x \n", port, reg, value);
*ret_val = value;
return 0;
error:
return -1;
}
/*
* This function writes to the North complex port register
*/
int write_nc_port(int reg, int port, int value)
{
static int fd = -1;
int cmd;
int ret;
if (fd < 0)
fd = open(CUNIT_PATH, O_RDWR | O_SYNC);
if (fd < 0) {
printf("open CUNIT_PATH ret = %d line %d\n", fd, __LINE__);
return -1;
}
cmd = reg & 0xFFFFFF00;
ret = lseek(fd, PCI_CTRL_REG_EXT, SEEK_SET);
if (ret < 0) {
printf("lseek failed ret = %d line %d\n", ret, __LINE__);
goto error;
}
ret = write(fd, &cmd, 4);
if (ret < 0) {
printf("write failed ret = %d line %d\n", ret, __LINE__);
goto error;
}
ret = lseek(fd, 0xD4, SEEK_SET);
if (ret < 0) {
printf("lseek failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
ret = write(fd, &value, 4);
if (ret < 0) {
printf("write failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
cmd = (PCI_WRITE_COMMAND << 24) | (port << 16) | ((reg & 0xFF) << 8) | (0xf0);
ret = lseek(fd, 0xD0, SEEK_SET);
if (ret < 0) {
printf("lseek failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
ret = write(fd, &cmd, 4);
if (ret < 0) {
printf("write failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
return 0;
error:
return -1;
}
/*
* This function reads from the North complex port number
*/
int read_port(int port, int *port_value)
{
static int fd = -1;
unsigned int value;
int ret;
if (fd < 0)
fd = open(PORT_DEV, O_RDWR | O_SYNC);
if (fd < 0) {
printf("open CUNIT_PATH ret = %d line %d\n", fd, __LINE__);
return -1;
}
ret = lseek(fd, port, SEEK_SET);
if (ret < 0) {
printf("lseek failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
ret = read(fd, &value, 4);
if (ret < 0) {
printf("read failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
*port_value = value;
printf("Port 0x%x value is value 0x%x\n", port, value);
return 0;
error:
return -1;
}
/*
* This function reads from the North complex port number
*/
int write_port(int port, int port_value)
{
static int fd = -1;
int ret;
if (fd < 0)
fd = open(PORT_DEV, O_RDWR | O_SYNC);
if (fd < 0) {
printf("open CUNIT_PATH ret = %d line %d\n", fd, __LINE__);
return -1;
}
ret = lseek(fd, port, SEEK_SET);
if (ret < 0) {
printf("lseek failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
ret = write(fd, &port_value, 4);
if (ret < 0) {
printf("write failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
return 0;
error:
return -1;
}
/*
* This function reads from the PCI config register
*/
int read_pci_reg(int bus, int dev, int func, int reg, int *port_value)
{
static int fd = -1;
int ret;
int value;
char buf[128];
snprintf(buf, sizeof(buf), PCI_DEV_PATH, bus, dev, func);
fd = open(buf, O_RDWR | O_SYNC);
if (fd < 0) {
printf("open pci dev %s failed ret = %d line %d\n", buf, fd, __LINE__);
goto error;
}
ret = lseek(fd, reg, SEEK_SET);
if (ret < 0) {
printf("lseek failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
ret = read(fd, &value, 4);
if (ret < 0) {
printf("read failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
*port_value = value;
printf("PCI bus 0x%x dev 0x%x func 0x%x reg value 0x%x value 0x%x\n",
bus, dev, func, reg, value);
return 0;
error:
return -1;
}
/*
* This function dumps from the PCI config register
*/
int read_pci_dump(int bus, int dev, int func)
{
static int fd = -1;
int ret, i;
int value;
char *p;
int lim, lim1;
char buf[128];
snprintf(buf, sizeof(buf), PCI_DEV_PATH, bus, dev, func);
fd = open(buf, O_RDWR | O_SYNC);
if (fd < 0) {
printf("open pci dev %s failed ret = %d line %d\n", buf, fd, __LINE__);
goto error;
}
ret = lseek(fd, 0, SEEK_SET);
if (ret < 0) {
printf("lseek failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
for(lim = 0; lim <= 255; ) {
printf("%02x: ", lim);
for(lim1 = 0; lim1 <= 3; lim1++) {
lim += 4;
ret = read(fd, &value, 4);
if (ret < 0) {
printf("read failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
p = (char*)&value;
for (i = 0; i < 4; i++, p++)
printf("%02x ", *p & 0xff);
}
printf("\n");
}
return 0;
error:
return -1;
}
/*
* This function writes to the PCI config register
*/
int write_pci_reg(int bus, int dev, int func, int reg, int value)
{
static int fd = -1;
int ret;
char buf[128];
snprintf(buf, sizeof(buf), PCI_DEV_PATH, bus, dev, func);
fd = open(buf, O_RDWR | O_SYNC);
if (fd < 0) {
printf("open pci dev %s failed ret = %d line %d\n", buf, fd, __LINE__);
goto error;
}
ret = lseek(fd, reg, SEEK_SET);
if (ret < 0) {
printf("lseek failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
ret = write(fd, &value, 4);
if (ret < 0) {
printf("write failed ret = %d line %d\n", fd, __LINE__);
goto error;
}
read_pci_reg(bus, dev, func, reg, &ret);
if (ret == value)
printf("Write OK\n");
else
printf("Write FAILED\n");
return 0;
error:
return -1;
}