-
Notifications
You must be signed in to change notification settings - Fork 2
/
cwpci.c
290 lines (246 loc) · 7.12 KB
/
cwpci.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
/*
* Catweasel -- Advanced Floppy Controller
* PCI detection routines
*
* Copyright (C) 2002 by Timothy Mann
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*#define DEBUG_PCI(stmt) stmt*/
#define DEBUG_PCI(stmt)
#include <stdio.h>
#include <stdint.h>
#include "cwpci.h"
#if __DJGPP__
#include <dpmi.h>
#include <string.h>
/* Translate PCI BIOS error code to string.
* See http://www.delorie.com/djgpp/doc/rbinter/it/29/7.html
*/
const char *
pci_errstr(int code)
{
switch (code) {
case 0x00:
return "successful";
case 0x81:
return "unsupported function";
case 0x83:
return "bad vendor id";
case 0x86:
return "device not found";
case 0x87:
return "bad register number";
case -1:
return "PCI BIOS not present";
default:
return "unknown error code";
}
}
/* 1AB101
* INT 1A - PCI BIOS v2.0c+ - INSTALLATION CHECK
* See http://www.delorie.com/djgpp/doc/rbinter/id/81/23.html
*
* Check if PCI bus installed. Returns 0 if OK, -1 if error
* invoking the BIOS through DPMI or the BIOS is not present.
*/
int
pci_install_check(void)
{
__dpmi_regs r;
memset(&r, 0, sizeof(r));
r.x.ax = 0xb101;
r.d.edi = 0x0;
if (__dpmi_int(0x1a, &r) != 0) {
DEBUG_PCI( printf("pci_install_check: BIOS call failed\n"); )
return -1;
}
if (r.h.ah != 0 || r.d.edx != 0x20494350) {
DEBUG_PCI( printf("pci_install_check: PCI BIOS not found\n"); )
return -1;
}
DEBUG_PCI( printf("pci_install_check: PCI BIOS found\n"); )
return 0;
}
/* 1AB102
* INT 1A - PCI BIOS v2.0c+ - FIND PCI DEVICE
* http://www.delorie.com/djgpp/doc/rbinter/id/82/23.html
*
* Find the index'th PCI device with the given vendorID and deviceID.
* Returns 0 if OK, -1 if error invoking the BIOS through DPMI, or a
* PCI BIOS error code. If OK, sets bus, device, and func to the
* address of the device found.
*/
int
pci_find(int vendorID, int deviceID, int index,
int *bus, int *device, int *func)
{
__dpmi_regs r;
memset(&r, 0, sizeof(r));
r.x.ax = 0xb102;
r.x.cx = deviceID;
r.x.dx = vendorID;
r.x.si = index;
if (__dpmi_int(0x1a, &r) != 0) {
DEBUG_PCI( printf("pci_find: BIOS call failed\n"); )
return -1;
}
if (r.h.ah == 0) {
*bus = r.h.bh;
*device = (r.h.bl >> 3) & 0x1f;
*func = r.h.bl & 0x03;
DEBUG_PCI( printf("pci_install_check(0x%x, 0x%x, %d) -> (%d, %d, %d)\n",
vendorID, deviceID, index, *bus, *device, *func); )
} else {
DEBUG_PCI( printf("pci_find(0x%x, 0x%x, %d): %s\n",
vendorID, deviceID, index, pci_errstr(r.h.ah)); )
}
return r.h.ah;
}
/* 1AB10A
* INT 1A - PCI BIOS v2.0c+ - READ CONFIGURATION DWORD
* http://www.delorie.com/djgpp/doc/rbinter/id/87/23.html
*
* Read a 32-bit value from PCI configuration space. Returns 0 if OK,
* -1 if error invoking the BIOS through DPMI, or a PCI BIOS error
* code. If OK, sets value.
*/
int
pci_read_config_dword(int bus, int device, int func,
int reg, uint32_t *value)
{
__dpmi_regs r;
memset(&r, 0, sizeof(r));
r.x.ax = 0xb10a;
r.h.bh = bus;
r.h.bl = (device << 3) + func;
r.x.di = reg;
if (__dpmi_int(0x1a, &r) != 0) {
DEBUG_PCI( printf("pci_read_config_dword: BIOS call failed\n"); )
return -1;
}
if (r.h.ah == 0) {
*value = r.d.ecx;
DEBUG_PCI( printf("pci_read_config_dword(%d, %d, %d, %d) -> 0x%lx\n",
bus, device, func, reg, *value); )
} else {
DEBUG_PCI( printf("pci_read_config_dword(%d, %d, %d, %d): %s\n",
bus, device, func, reg, pci_errstr(r.h.ah)); )
}
return r.h.ah;
}
/*
* Find the I/O space for the index'th PCI catweasel card in the
* machine (0-origin). Return port address if found, -1 if not found.
*/
int
pci_find_catweasel(int index, int *cw_mk)
{
int i = 0, j = 0, res;
int bus, device, func, mk = 0;
uint32_t subsysID, baseAddr;
if (pci_install_check() != 0) return -1;
while (i <= index) {
/* Find the next card that uses the Tiger Jet Networks Tiger320 PCI chip */
res = pci_find(0xe159, 0x0001, j++, &bus, &device, &func);
if (res != 0) return -1;
/* Read the subsystem vendor ID + subsystem ID */
res = pci_read_config_dword(bus, device, func, 0x2c, &subsysID);
if (res != 0) continue;
/* Check if they match the Catweasel */
switch (subsysID) {
case 0x00021212: /* Catweasel MK3 */
case 0x00031212: /* Catweasel MK3 alternate */
mk = 3;
break;
case 0x00025213: /* Catweasel MK4 */
case 0x00035213: /* Catweasel MK4 alternate */
mk = 4;
break;
default:
DEBUG_PCI( printf("subsysID 0x%lx\n", subsysID); )
continue;
}
i++;
}
for (i = 0x10; i <= 0x24; i += 4) {
/* Read a base address */
res = pci_read_config_dword(bus, device, func, i, &baseAddr);
if (res != 0) return -1;
/* Check for I/O space */
if (baseAddr & 1) {
*cw_mk = mk;
return baseAddr & ~3;
}
DEBUG_PCI( printf("baseAddr 0x%lx not I/O space\n", baseAddr); )
}
return -1;
}
#else /* __DJGPP__ */
#include <pci/pci.h>
/*
* Find the I/O space for the index'th PCI catweasel card in the
* machine (0-origin). Return port address if found, -1 if not found.
*/
int
pci_find_catweasel(int index, int *cw_mk)
{
struct pci_access *pa;
struct pci_dev *pd;
int i, mk;
/* Initialize */
pa = pci_alloc();
pci_init(pa);
/* Scan buses */
pci_scan_bus(pa);
/* Iterate through devices */
for (pd = pa->devices; pd != NULL; pd = pd->next) {
pci_fill_info(pd, PCI_FILL_IDENT);
if (pd->vendor_id == 0xe159 && /* Tiger Jet Networks */
pd->device_id == 0x0001) { /* Tiger320 PCI chip */
switch (pci_read_long(pd, 0x2c)) {
case 0x00021212: /* Catweasel MK3 */
case 0x00031212: /* Catweasel MK3 alternate */
mk = 3;
break;
case 0x00025213: /* Catweasel MK4 */
case 0x00035213: /* Catweasel MK4 alternate */
mk = 4;
break;
default:
continue;
}
/* Is this the index'th card? */
if (index-- > 0) continue;
/* Find the I/O space */
pci_fill_info(pd, PCI_FILL_BASES);
for (i = 0; i < 6; i++) {
unsigned flg = pci_read_long(pd, PCI_BASE_ADDRESS_0 + 4 * i);
if (flg != 0xffffffff && (flg & PCI_BASE_ADDRESS_SPACE_IO)) {
u16 sval;
sval = pci_read_word(pd, PCI_COMMAND);
sval |= PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
pci_write_word(pd, PCI_COMMAND, sval);
*cw_mk = mk;
return pd->base_addr[i] & PCI_ADDR_IO_MASK;
}
DEBUG_PCI( printf("baseAddr %d 0x%llx not I/O space\n",
i, (long long)pd->base_addr[i]); )
}
}
}
return -1;
}
#endif /* __DJGPP__ */