OSDN Git Service

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