summaryrefslogtreecommitdiff
path: root/src/video_out/libdha/sysdep/pci_powerpc.c
blob: 667b4db375b08ff49907fb374df088ec57b48ac2 (plain)
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
/*
   This file is based on:
   $XFree86: xc/programs/Xserver/hw/xfree86/etc/scanpci.c,v 3.34.2.17 1998/11/10 11:55:40 dawes Exp $
   Modified for readability by Nick Kurshev
*/

static int pci_config_type( void ) { return 1; }
#ifdef linux
#include <fcntl.h>
#include <sys/io.h>
#include <linux/pci.h>
#include "../../bswap.h"
#endif

#ifdef linux
static int pci_get_vendor(
	  unsigned char bus,
	  unsigned char dev,
	  int func)
{
    int retval;
    char path[100];
    int fd;
    short vendor, device;
    sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev);
    fd = open(path,O_RDONLY|O_SYNC);
    if (fd == -1) {
	    retval=0xFFFF;
    }
    else if (pread(fd, &vendor, 2, PCI_VENDOR_ID) == 2 &&
             pread(fd, &device, 2, PCI_DEVICE_ID) == 2) {
	    vendor = bswap_16(vendor);
	    device = bswap_16(device);
	    retval = vendor + (device<<16); /*no worries about byte order, 
	    				      all ppc are bigendian*/
    } else {
	    retval = 0xFFFF;
    }   
    if (fd > 0) {
	    close(fd);
    }
    return retval;
}
#else
static int pci_get_vendor(
          unsigned char bus,
          unsigned char dev,
          int func)
{
    int retval;
    pciconfig_read(bus, dev<<3, PCI_ID_REG, 4, &retval);
    return retval;
}
#endif
#ifdef linux
static long pci_config_read_long(
	  unsigned char bus,
	  unsigned char dev,
	  int func, 
	  unsigned cmd)
{
    long retval;
    char path[100];
    int fd;
    sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev);
    fd = open(path,O_RDONLY|O_SYNC);
    if (fd == -1) {
	    retval=0;
    }
    else if (pread(fd, &retval, 4, cmd) == 4) {
	    retval = bswap_32(retval);
    } else {
	    retval = 0;
    }   
    if (fd > 0) {
	    close(fd);
    }
    return retval;
}

static long pci_config_read_word(
	  unsigned char bus,
	  unsigned char dev,
	  int func, 
	  unsigned cmd)
{
    long retval;
    char path[100];
    int fd;
    sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev);
    fd = open(path,O_RDONLY|O_SYNC);
    if (fd == -1) {
	    retval=0;
    }
    else if (pread(fd, &retval, 2, cmd) == 2) {
	    retval = bswap_16(retval);
    } else {
	    retval = 0;
    }   
    if (fd > 0) {
	    close(fd);
    }
    return retval;
}

static long pci_config_read_byte(
	  unsigned char bus,
	  unsigned char dev,
	  int func, 
	  unsigned cmd)
{
    long retval;
    char path[100];
    int fd;
    sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev);
    fd = open(path,O_RDONLY|O_SYNC);
    if (fd == -1) {
	    retval=0;
    }
    else if (pread(fd, &retval, 1, cmd) != 1) {
	    retval = 0;
    }   
    if (fd > 0) {
	    close(fd);
    }
    return retval;
}

static void pci_config_write_long(
	  unsigned char bus,
	  unsigned char dev,
	  int func, 
	  unsigned cmd,
	  long val)
{
    char path[100];
    int fd;
    val = bswap_32(val);
    sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev);
    fd = open(path,O_RDONLY|O_SYNC);
    if (fd > 0) {
	    pwrite(fd, &val, 4, cmd);
	    close(fd);
    }
}

static void pci_config_write_word(
	  unsigned char bus,
	  unsigned char dev,
	  int func, 
	  unsigned cmd,
	  long val)
{
    char path[100];
    int fd;
    val = bswap_16(val);
    sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev);
    fd = open(path,O_RDONLY|O_SYNC);
    if (fd > 0) {
	    pwrite(fd, &val, 2, cmd);
	    close(fd);
    }
}

static void pci_config_write_byte(
	  unsigned char bus,
	  unsigned char dev,
	  int func, 
	  unsigned cmd,
	  long val)
{
    char path[100];
    int fd;
    sprintf(path,"/proc/bus/pci/%02d/%02x.0", bus, dev);
    fd = open(path,O_RDONLY|O_SYNC);
    if (fd > 0) {
	    pwrite(fd, &val, 1, cmd);
	    close(fd);
    }
}
#else
static long pci_config_read_long(
          unsigned char bus,
          unsigned char dev,
          int func, 
          unsigned cmd)
{
    long retval;
    pciconfig_read(bus, dev<<3, cmd, 4, &retval);
    return retval;
}

static long pci_config_read_word(
          unsigned char bus,
          unsigned char dev,
          int func, 
          unsigned cmd)
{
    long retval;
    pciconfig_read(bus, dev<<3, cmd, 2, &retval);
    return retval;
}

static long pci_config_read_byte(
          unsigned char bus,
          unsigned char dev,
          int func, 
          unsigned cmd)
{
    long retval;
    pciconfig_read(bus, dev<<3, cmd, 1, &retval);
    return retval;
}

static void pci_config_write_long(
          unsigned char bus,
          unsigned char dev,
          int func, 
          unsigned cmd,
	  long val)
{
    long retval;
    pciconfig_write(bus, dev<<3, cmd, 4, val);
    return retval;
}

static void pci_config_write_word(
          unsigned char bus,
          unsigned char dev,
          int func, 
          unsigned cmd,
	  long val)
{
    long retval;
    pciconfig_write(bus, dev<<3, cmd, 2, val);
    return retval;
}

static void pci_config_write_byte(
          unsigned char bus,
          unsigned char dev,
          int func, 
          unsigned cmd,
	  long val)
{
    long retval;
    pciconfig_write(bus, dev<<3, cmd, 1, val);
    return retval;
}
#endif