OSDN Git Service

Revert "Fix redefinition of ioperm and iopl in stubs.c and arm/{ioperm.c,iopl.c}"
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / arm / ioperm.c
1 /* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Phil Blundell, based on the Alpha version by
4    David Mosberger.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 /* I/O port access on the ARM is something of a fiction.  What we do is to
22    map an appropriate area of /dev/mem into user space so that a program
23    can blast away at the hardware in such a way as to generate I/O cycles
24    on the bus.  To insulate user code from dependencies on particular
25    hardware we don't allow calls to inb() and friends to be inlined, but
26    force them to come through code in here every time.  Performance-critical
27    registers tend to be memory mapped these days so this should be no big
28    problem.  */
29
30 /* Once upon a time this file used mprotect to enable and disable
31    access to particular areas of I/O space.  Unfortunately the
32    mprotect syscall also has the side effect of enabling caching for
33    the area affected (this is a kernel limitation).  So we now just
34    enable all the ports all of the time.  */
35
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <ctype.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 #include <sys/types.h>
45 #include <sys/mman.h>
46 #include <sys/sysctl.h>
47 #include <sys/io.h>
48
49
50
51 #include <linux/version.h>
52
53 #define PATH_ARM_SYSTYPE        "/etc/arm_systype"
54 #define PATH_CPUINFO            "/proc/cpuinfo"
55
56 #define MAX_PORT        0x10000
57
58 static struct {
59     unsigned long int   base;
60     unsigned long int   io_base;
61     unsigned int                shift;
62     unsigned int                initdone;       /* since all the above could be 0 */
63 } io;
64
65 #define IO_BASE_FOOTBRIDGE      0x7c000000
66 #define IO_SHIFT_FOOTBRIDGE     0
67
68 static struct platform {
69     const char          *name;
70     unsigned long int   io_base;
71     unsigned int                shift;
72 } platform[] = {
73     /* All currently supported platforms are in fact the same. :-)  */
74     {"Chalice-CATS",    IO_BASE_FOOTBRIDGE,     IO_SHIFT_FOOTBRIDGE},
75     {"DEC-EBSA285",     IO_BASE_FOOTBRIDGE,     IO_SHIFT_FOOTBRIDGE},
76     {"Corel-NetWinder", IO_BASE_FOOTBRIDGE,     IO_SHIFT_FOOTBRIDGE},
77     {"Rebel-NetWinder", IO_BASE_FOOTBRIDGE,     IO_SHIFT_FOOTBRIDGE},
78 };
79
80 #define IO_ADDR(port)   (io.base + ((port) << io.shift))
81
82 /*
83  * Initialize I/O system.  There are several ways to get the information
84  * we need.  Each is tried in turn until one succeeds.
85  *
86  * 1. Sysctl (CTL_BUS, BUS_ISA, ISA_*).  This is the preferred method
87  *    but not all kernels support it.
88  *
89  * 2. Read the value (not the contents) of symlink PATH_ARM_SYSTYPE.
90  *    - If it matches one of the entries in the table above, use the
91  *      corresponding values.
92  *    - If it begins with a number, assume this is a previously
93  *      unsupported system and the values encode, in order,
94  *      "<io_base>,<port_shift>".
95  *
96  * 3. Lookup the "system type" field in /proc/cpuinfo.  Again, if it
97  *    matches an entry in the platform[] table, use the corresponding
98  *    values.
99  *
100  * 4. BUS_ISA is changed to CTL_BUS_ISA (for kernel since 2.4.23).
101  */
102
103 static int
104 init_iosys (void)
105 {
106     char systype[256];
107     int i, n;
108
109 #if LINUX_VERSION_CODE < 132119
110     static int iobase_name[] = { CTL_BUS, BUS_ISA, BUS_ISA_PORT_BASE };
111     static int ioshift_name[] = { CTL_BUS, BUS_ISA, BUS_ISA_PORT_SHIFT };
112 #else
113     static int iobase_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_BASE };
114     static int ioshift_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_SHIFT };
115 #endif
116
117     size_t len = sizeof(io.base);
118
119     if (! sysctl (iobase_name, 3, &io.io_base, &len, NULL, 0)
120         && ! sysctl (ioshift_name, 3, &io.shift, &len, NULL, 0)) {
121         io.initdone = 1;
122         return 0;
123     }
124
125     n = readlink (PATH_ARM_SYSTYPE, systype, sizeof (systype) - 1);
126     if (n > 0) {
127         systype[n] = '\0';
128         if (isdigit (systype[0])) {
129             if (sscanf (systype, "%li,%i", &io.io_base, &io.shift) == 2) {
130                 io.initdone = 1;
131                 return 0;
132             }
133             /* else we're likely going to fail with the system match below */
134         }
135     }
136     else {
137         FILE * fp;
138
139         fp = fopen (PATH_CPUINFO, "r");
140         if (! fp)
141             return -1;
142         while ((n = fscanf (fp, "Hardware\t: %256[^\n]\n", systype)) != EOF) {
143             if (n == 1)
144                 break;
145             else
146                 fgets (systype, 256, fp);
147         }
148         fclose (fp);
149
150         if (n == EOF) {
151             /* this can happen if the format of /proc/cpuinfo changes...  */
152             fprintf (stderr, "ioperm: Unable to determine system type.\n"
153                      "\t(May need " PATH_ARM_SYSTYPE " symlink?)\n");
154             __set_errno (ENODEV);
155             return -1;
156         }
157     }
158
159     /* translate systype name into i/o system: */
160     for (i = 0; i < sizeof (platform) / sizeof (platform[0]); ++i) {
161         if (strcmp (platform[i].name, systype) == 0) {
162             io.shift = platform[i].shift;
163             io.io_base = platform[i].io_base;
164             io.initdone = 1;
165             return 0;
166         }
167     }
168
169     /* systype is not a known platform name... */
170     __set_errno (EINVAL);
171     return -1;
172 }
173
174 int ioperm (unsigned long int from, unsigned long int num, int turn_on)
175 {
176     if (! io.initdone && init_iosys () < 0)
177         return -1;
178
179     /* this test isn't as silly as it may look like; consider overflows! */
180     if (from >= MAX_PORT || from + num > MAX_PORT) {
181         __set_errno (EINVAL);
182         return -1;
183     }
184
185     if (turn_on) {
186         if (! io.base) {
187             int fd;
188
189             fd = open ("/dev/mem", O_RDWR);
190             if (fd < 0)
191                 return -1;
192
193             io.base = (unsigned long int) mmap (0, MAX_PORT << io.shift,
194                                           PROT_READ | PROT_WRITE,
195                                           MAP_SHARED, fd, io.io_base);
196             close (fd);
197             if ((long) io.base == -1)
198                 return -1;
199         }
200     }
201
202     return 0;
203 }
204 libc_hidden_def(ioperm)
205
206
207 void
208 outb(unsigned char b, unsigned long int port)
209 {
210     *((__volatile__ unsigned char *)(IO_ADDR (port))) = b;
211 }
212
213
214 void
215 outw(unsigned short b, unsigned long int port)
216 {
217     *((__volatile__ unsigned short *)(IO_ADDR (port))) = b;
218 }
219
220
221 void
222 outl(unsigned long b, unsigned long int port)
223 {
224     *((__volatile__ unsigned long *)(IO_ADDR (port))) = b;
225 }
226
227
228 unsigned char
229 inb (unsigned long int port)
230 {
231     return *((__volatile__ unsigned char *)(IO_ADDR (port)));
232 }
233
234
235 unsigned short int
236 inw(unsigned long int port)
237 {
238     return *((__volatile__ unsigned short *)(IO_ADDR (port)));
239 }
240
241
242 unsigned long int
243 inl(unsigned long int port)
244 {
245     return *((__volatile__ unsigned long *)(IO_ADDR (port)));
246 }