/
vt6307ohciver.c
323 lines (263 loc) · 8.17 KB
/
vt6307ohciver.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
/*
* Copyright (C) 2007 Krzysztof Halasa <khc@pm.waw.pl>
*
* 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 based on undocumented features of VT6307 IEEE-1394
* OHCI chip. Use at your own risk.
*/
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pci/pci.h>
#include <sys/io.h>
#include <sys/mman.h>
#define DEBUG 0
#define VENDOR_ID 0x1106
#define DEVICE_ID 0x3044
#define DEVICE_CLASS 0x0C0000UL
#define MEM_SIZE 0x800 /* bytes */
#define IO_SIZE 0x80 /* bytes */
#define EEPROM_SIZE 0x30 /* bytes */
#define I2C_DEVSEL_RD (0x50 << 1 | 1)
#define I2C_DEVSEL_WR (0x50 << 1)
static uint32_t io_ports;
static void output(int cs, int clock, int dataout)
{
uint32_t value = 0x10 /* enable outputs */ |
(cs ? 8 : 0) | (clock ? 4 : 0) | (dataout ? 2 : 0);
#if DEBUG
fprintf(stderr, " %c%c%c ", cs ? ' ' : '-', clock ? 'c' : ' ',
dataout ? '1' : '0');
#endif
outl_p(value, io_ports + 0x20);
}
/**************************************************************************
*
* I^2C version
*
**************************************************************************/
static void start_i2c(void)
{
output(1, 1, 0); usleep(1000); // stopped (SCL = 1, SDA = 1)
output(1, 0, 0); usleep(1000); // running (SCL = 0)
}
static void stop_i2c(void)
{
output(1, 0, 0); usleep(100); // running (SCL = 0)
output(1, 1, 0); usleep(100);
output(1, 1, 1); usleep(100); // stopped (SCL = 1, SDA = 1)
}
static void send_bit_i2c(uint32_t v)
{
output(1, 0, v); usleep(100); // running (SCL = 0)
output(1, 1, v); usleep(100);
output(1, 0, v); usleep(100); // running (SCL = 0)
}
static void write_byte_i2c(uint8_t value)
{
int i = 8;
while (i--) {
send_bit_i2c(value & 0x80);
value <<= 1;
}
send_bit_i2c(0); /* actually we should read it - ACK bit */
}
static void write_i2c(uint8_t address, uint8_t value)
{
printf("writing 0x%02X at address 0x%02X\n", value, address);
output(1, 0, 1); usleep(100); /* Init */
output(1, 1, 1); usleep(2000);
start_i2c();
write_byte_i2c(I2C_DEVSEL_WR);
write_byte_i2c(address);
write_byte_i2c(value); usleep(2000);
stop_i2c();
usleep(2000);
}
/**************************************************************************
*
* 4-wire version
*
**************************************************************************/
static void send_bits_4w(int bits, uint32_t value)
{
if (bits < 32)
value <<= 32 - bits;
while (bits--) {
output(1, 0, value & 0x80000000); usleep(10);
output(1, 1, value & 0x80000000); usleep(10);
output(1, 0, value & 0x80000000); usleep(10);
value <<= 1;
}
}
static void write_4w(uint8_t address, uint16_t value)
{
printf("writing 0x%04X at address 0x%02X\n", value, address);
output(0, 0, 0); usleep(10); /* Init */
output(1, 0, 0); usleep(10);
send_bits_4w(3, 0x4); /* CMD: EWEN = write enable */
send_bits_4w(6, 0x30);
send_bits_4w(16, 0x0000);
output(0, 0, 0); usleep(10);
output(1, 0, 0); usleep(10);
send_bits_4w(3, 0x5); /* CMD: WRITE */
send_bits_4w(6, address);
send_bits_4w(16, value);
output(0, 0, 0); usleep(1000);
output(1, 0, 0); usleep(10);
send_bits_4w(3, 0x4); /* CMD: EWDS = write disable */
send_bits_4w(6, 0x00);
send_bits_4w(16, 0x0000);
output(0, 0, 0); usleep(10);
}
/**************************************************************************
*
* display EEPROM contents
*
**************************************************************************/
static inline uint32_t readl(uint32_t *reg)
{
return *(volatile uint32_t *)reg;
}
static inline void writel(uint32_t *reg, uint32_t value)
{
*(volatile uint32_t *)reg = value;
readl(reg); /* flush buffers */
}
static void display(pciaddr_t mem_addr)
{
int i, page_size = getpagesize();
uint32_t mem_length, offset, *reg, cnt, v22 = 0xFF;
mem_addr += 4; /* Only interested in GUID PROM register */
mem_length = 4;
fprintf(stderr, "Page size is %i (0x%X) bytes\n"
"GUID PROM register address is 0x%08llx\n",
page_size, page_size, (unsigned long long)mem_addr);
offset = mem_addr & (page_size - 1);
mem_addr -= offset;
mem_length += offset;
fprintf(stderr, "Mapping %i (0x%X) bytes of memory at 0x%08llx\n",
mem_length, mem_length, (unsigned long long)mem_addr);
if ((i = open("/dev/mem", O_RDWR) ) < 0) {
fprintf(stderr, "Cannot open /dev/mem: %s\n", strerror(errno));
exit(1);
}
reg = mmap(0, mem_length, PROT_READ | PROT_WRITE, MAP_SHARED, i,
mem_addr);
if (reg == MAP_FAILED) {
fprintf(stderr, "Unable to mmap memory region #0: %s\n",
strerror(errno));
exit(1);
}
fprintf(stderr, "Mapped mem region #0 at virtual address %p\n", reg);
/* point to the GUID PROM register */
reg = (uint32_t*)(offset + (uint8_t *)reg);
fprintf(stderr, "GUID PROM register is at virtual address %p\n\n"
"EEPROM dump:\n", reg);
writel(reg, 0x80 << 24); /* reset internal counter */
while (readl(reg) & (0x80 << 24))
; /* wait */
for (cnt = 0; cnt < EEPROM_SIZE; cnt++) {
if (cnt % 16 == 0)
printf("%02X:", cnt);
writel(reg, 0x02 << 24); /* next byte */
while (readl(reg) & (0x02 << 24))
; /* wait */
printf(" %02X", i = (readl(reg) >> 16) & 0xFF);
if (cnt == 0x22)
v22 = i;
if (cnt % 16 == 15)
printf("\n");
}
printf("\n");
printf("Your VT6307 chip is in %s mode\n", (v22 == 0) ? "OHCI 1.0" :
(v22 == 8) ? "OHCI 1.1" : "unknown");
}
/**************************************************************************
*
* main
*
**************************************************************************/
int main (int argc, char *argv[])
{
struct pci_access *pacc;
struct pci_dev *dev;
struct pci_filter filter;
char *msg;
uint16_t command;
int v1_1 = 0, i2c;
if (argc != 2 &&
((argc != 3) ||
(!(v1_1 = !strcmp(argv[2], "1.1")) && strcmp(argv[2], "1.0")))) {
fprintf(stderr,
"VT6307 OHCI mode config\n"
"Version 0.9\n"
"Copyright (C) 2007 Krzysztof Halasa <khc@pm.waw.pl>\n"
"\n"
"Usage: vt6307ohciver <pci_device> [ 1.0 | 1.1 ]\n");
exit(1);
}
if (iopl(3)) {
fprintf(stderr, "iopl() failed (must be root)\n");
exit(1);
}
pacc = pci_alloc();
pci_filter_init(pacc, &filter);
if ((msg = pci_filter_parse_slot(&filter, argv[1]))) {
fprintf(stderr, "Invalid pci_device\n");
exit(1);
}
filter.vendor = VENDOR_ID;
filter.device = DEVICE_ID;
pci_init(pacc);
pci_scan_bus(pacc);
for (dev = pacc->devices; dev; dev = dev->next)
if (pci_filter_match(&filter, dev))
break;
if (!dev) {
fprintf(stderr, "Device %s not found\n", argv[2]);
exit(1);
}
pci_fill_info(dev, PCI_FILL_BASES | PCI_FILL_SIZES);
if (dev->size[0] != MEM_SIZE || dev->size[1] != IO_SIZE) {
fprintf(stderr, "Unexpected MEM/IO region size, is it"
" VT6307 chip?\n");
exit(1);
}
command = pci_read_word(dev, PCI_COMMAND);
if ((command & PCI_COMMAND_IO) == 0) {
fprintf(stderr, "Device disabled, trying to enable it\n");
pci_write_word(dev, PCI_COMMAND, command | PCI_COMMAND_IO);
}
io_ports = dev->base_addr[1] & PCI_BASE_ADDRESS_IO_MASK;
i2c = (inl(io_ports + 0x20) & 0x80) ? 0 : 1;
fprintf(stderr, "I/O region #1 is at %04X\n", io_ports);
fprintf(stderr, "It seems your VT6307 chip is connected to %s "
"EEPROM\n", i2c ? "I^2C (24c01 or similar)" : "93c46");
if (argc == 3) {
/* enable direct access to pins */
outl_p(inl(io_ports) | 0x80, io_ports);
if (i2c)
write_i2c(0x22, v1_1 ? 8 : 0);
else
write_4w(0x11, v1_1 ? 8 : 0);
/* disable direct access to pins */
outl_p(0x20, io_ports + 0x20);
fprintf(stderr, "Please reboot\n");
} else
display(dev->base_addr[0] & PCI_BASE_ADDRESS_MEM_MASK);
if ((command & PCI_COMMAND_IO) == 0) {
fprintf(stderr, "Disabling device\n");
pci_write_word(dev, PCI_COMMAND, command);
}
exit(0);
}