forked from k5mwe/DtCyber
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pci_channel_linux.c
427 lines (383 loc) · 10.9 KB
/
pci_channel_linux.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*--------------------------------------------------------------------------
**
** Copyright (c) 2003-2011, Tom Hunter
**
** Name: pci_channel.c
**
** Description:
** Interface to PCI channel adapter.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License version 3 as
** published by the Free Software Foundation.
**
** 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
** GNU General Public License version 3 for more details.
**
** You should have received a copy of the GNU General Public License
** version 3 along with this program in file "license-gpl-3.0.txt".
** If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
**
**--------------------------------------------------------------------------
*/
#define DEBUG 0
/*
** -------------
** Include Files
** -------------
*/
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <errno.h>
#include <sys/ioctl.h>
#include "const.h"
#include "types.h"
#include "proto.h"
#include "cyber_channel_linux.h"
/*
** -----------------
** Private Constants
** -----------------
*/
#define PciCmdNop 0x0000
#define PciCmdFunction 0x2000
#define PciCmdFull 0x4000
#define PciCmdEmpty 0x6000
#define PciCmdActive 0x8000
#define PciCmdInactive 0xA000
#define PciCmdClear 0xC000
#define PciCmdMasterClear 0xE000
#define PciStaFull 0x2000
#define PciStaActive 0x4000
#define PciStaBusy 0x8000
#define PciMaskData 0x0FFF
#define PciMaskParity 0x1000
#define PciShiftParity 12
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
typedef struct pciParam
{
int fdPci;
int data;
} PciParam;
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static FcStatus pciFunc(PpWord funcCode);
static void pciIo(void);
static PpWord pciIn(void);
static void pciOut(PpWord data);
static void pciFull(void);
static void pciEmpty(void);
static void pciActivate(void);
static void pciDisconnect(void);
static u16 pciFlags(void);
static void pciCmd(u16 data);
static u16 pciStatus(void);
static u16 pciParity(PpWord val);
/*
** ----------------
** Public Variables
** ----------------
*/
/*
** -----------------
** Private Variables
** -----------------
*/
static PciParam *pci;
static IoCB io;
#if DEBUG
static FILE *pciLog = NULL;
static bool active = FALSE;
#endif
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Initialise PCI channel interface.
**
** Parameters: Name Description.
** eqNo equipment number
** unitNo unit number
** channelNo channel number the device is attached to
** deviceName optional device file name
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void pciInit(u8 eqNo, u8 unitNo, u8 channelNo, char *deviceName)
{
DevSlot *dp;
(void)unitNo;
(void)deviceName;
#if DEBUG
if (pciLog == NULL)
{
pciLog = fopen("pcilog.txt", "wt");
}
#endif
/*
** Attach device to channel and initialise device control block.
*/
dp = channelAttach(channelNo, eqNo, DtPciChannel);
dp->activate = pciActivate;
dp->disconnect = pciDisconnect;
dp->func = pciFunc;
dp->io = pciIo;
dp->flags = pciFlags;
dp->in = pciIn;
dp->out = pciOut;
dp->full = pciFull;
dp->empty = pciEmpty;
/*
** Allocate and initialise channel parameters.
*/
pci = calloc(1, sizeof(PciParam));
if (pci == NULL)
{
fprintf(stderr, "Failed to allocate PCI channel context block\n");
exit(1);
}
pci->fdPci = open(DEVICE_NODE, 0);
if (pci->fdPci < 0)
{
fprintf(stderr, "Can't open %s - error %s\n", DEVICE_NODE, strerror(errno));
exit(1);
}
pciCmd(PciCmdMasterClear);
/*
** Print a friendly message.
*/
printf("PCI channel interface initialised on channel %o unit %o\n", channelNo, unitNo);
}
/*
**--------------------------------------------------------------------------
**
** Private Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Execute function code on channel.
**
** Parameters: Name Description.
** funcCode function code
**
** Returns: FcStatus
**
**------------------------------------------------------------------------*/
static FcStatus pciFunc(PpWord funcCode)
{
#if DEBUG
fprintf(pciLog, "\n%06d PP:%02o CH:%02o f:%04o > ",
traceSequenceNo,
activePpu->id,
activeChannel->id,
funcCode);
#endif
pciCmd(PciCmdFunction | funcCode | (pciParity(funcCode) << PciShiftParity));
return(FcAccepted);
}
/*--------------------------------------------------------------------------
** Purpose: Perform I/O on channel (not used).
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void pciIo(void)
{
}
/*--------------------------------------------------------------------------
** Purpose: Perform input from PCI channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static PpWord pciIn(void)
{
PpWord data = pciStatus() & Mask12;
#if DEBUG
fprintf(pciLog, " I(%03X)", data);
#endif
return(data);
}
/*--------------------------------------------------------------------------
** Purpose: Save output for PCI channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void pciOut(PpWord data)
{
pci->data = data;
}
/*--------------------------------------------------------------------------
** Purpose: Set the channel full with data previously saved
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void pciFull(void)
{
#if DEBUG
fprintf(pciLog, " O(%03X)", pci->data);
#endif
pciCmd(PciCmdFull | pci->data | (pciParity(pci->data) << PciShiftParity));
}
/*--------------------------------------------------------------------------
** Purpose: Set the channel empty
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void pciEmpty(void)
{
#if DEBUG
fprintf(pciLog, " E");
#endif
pciCmd(PciCmdEmpty);
}
/*--------------------------------------------------------------------------
** Purpose: Handle channel activation.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void pciActivate(void)
{
#if DEBUG
fprintf(pciLog, " A");
active = TRUE;
#endif
pciCmd(PciCmdActive);
}
/*--------------------------------------------------------------------------
** Purpose: Handle disconnecting of channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void pciDisconnect(void)
{
#if DEBUG
fprintf(pciLog, " D");
active = FALSE;
#endif
pciCmd(PciCmdInactive);
}
/*--------------------------------------------------------------------------
** Purpose: Update full/active channel flags.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static u16 pciFlags(void)
{
#if DEBUG
u16 s = pciStatus();
if (active)
{
// fprintf(pciLog, " S(%04X)", s);
}
return(s);
#else
return(pciStatus());
#endif
}
/*--------------------------------------------------------------------------
** Purpose: Send a PCI command
**
** Parameters: Name Description.
** data command data
**
** Returns: nothing
**
**------------------------------------------------------------------------*/
static void pciCmd(u16 data)
{
IoCB io;
io.address = 0;
do
{
ioctl(pci->fdPci, IOCTL_FPGA_READ, &io);
} while ((io.data & PciStaBusy) != 0);
io.data = data;
ioctl(pci->fdPci, IOCTL_FPGA_WRITE, &io);
}
/*--------------------------------------------------------------------------
** Purpose: Get PCI status.
**
** Parameters: Name Description.
**
** Returns: PCI status word
**
**------------------------------------------------------------------------*/
static u16 pciStatus(void)
{
IoCB io;
io.address = 0;
ioctl(pci->fdPci, IOCTL_FPGA_READ, &io);
return(io.data);
}
/*--------------------------------------------------------------------------
** Purpose: Calculate odd parity over 12 bit PP word.
**
** Parameters: Name Description.
** data 12 bit PP word
**
** Returns: Parity
**
**------------------------------------------------------------------------*/
static u16 pciParity(PpWord data)
{
u16 ret = 1;
while (data != 0)
{
ret ^= data & 1;
data >>= 1;
}
return(ret);
}
/*--------------------------- End Of File ------------------------------*/