OSDN Git Service

Last portion of libc_hidden_proto removal.
[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 /* libc_hidden_proto(ioperm) */
50
51 /* libc_hidden_proto(readlink) */
52 /* libc_hidden_proto(mmap) */
53 /* libc_hidden_proto(sscanf) */
54 /* libc_hidden_proto(fscanf) */
55 /* libc_hidden_proto(fprintf) */
56 /* libc_hidden_proto(fgets) */
57 /* libc_hidden_proto(fopen) */
58 /* libc_hidden_proto(fclose) */
59 /* Experimentally off - libc_hidden_proto(strcmp) */
60 /* libc_hidden_proto(open) */
61 /* libc_hidden_proto(close) */
62
63 #include <linux/version.h>
64
65 #define PATH_ARM_SYSTYPE        "/etc/arm_systype"
66 #define PATH_CPUINFO            "/proc/cpuinfo"
67
68 #define MAX_PORT        0x10000
69
70 static struct {
71     unsigned long int   base;
72     unsigned long int   io_base;
73     unsigned int                shift;
74     unsigned int                initdone;       /* since all the above could be 0 */
75 } io;
76
77 #define IO_BASE_FOOTBRIDGE      0x7c000000
78 #define IO_SHIFT_FOOTBRIDGE     0
79
80 static struct platform {
81     const char          *name;
82     unsigned long int   io_base;
83     unsigned int                shift;
84 } platform[] = {
85     /* All currently supported platforms are in fact the same. :-)  */
86     {"Chalice-CATS",    IO_BASE_FOOTBRIDGE,     IO_SHIFT_FOOTBRIDGE},
87     {"DEC-EBSA285",     IO_BASE_FOOTBRIDGE,     IO_SHIFT_FOOTBRIDGE},
88     {"Corel-NetWinder", IO_BASE_FOOTBRIDGE,     IO_SHIFT_FOOTBRIDGE},
89     {"Rebel-NetWinder", IO_BASE_FOOTBRIDGE,     IO_SHIFT_FOOTBRIDGE},
90 };
91
92 #define IO_ADDR(port)   (io.base + ((port) << io.shift))
93
94 /*
95  * Initialize I/O system.  There are several ways to get the information
96  * we need.  Each is tried in turn until one succeeds.
97  *
98  * 1. Sysctl (CTL_BUS, BUS_ISA, ISA_*).  This is the preferred method
99  *    but not all kernels support it.
100  *
101  * 2. Read the value (not the contents) of symlink PATH_ARM_SYSTYPE.
102  *    - If it matches one of the entries in the table above, use the
103  *      corresponding values.
104  *    - If it begins with a number, assume this is a previously
105  *      unsupported system and the values encode, in order,
106  *      "<io_base>,<port_shift>".
107  *
108  * 3. Lookup the "system type" field in /proc/cpuinfo.  Again, if it
109  *    matches an entry in the platform[] table, use the corresponding
110  *    values.
111  *
112  * 4. BUS_ISA is changed to CTL_BUS_ISA (for kernel since 2.4.23).
113  */
114
115 static int
116 init_iosys (void)
117 {
118     char systype[256];
119     int i, n;
120
121 #if LINUX_VERSION_CODE < 132119
122     static int iobase_name[] = { CTL_BUS, BUS_ISA, BUS_ISA_PORT_BASE };
123     static int ioshift_name[] = { CTL_BUS, BUS_ISA, BUS_ISA_PORT_SHIFT };
124 #else
125     static int iobase_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_BASE };
126     static int ioshift_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_SHIFT };
127 #endif
128
129     size_t len = sizeof(io.base);
130
131     if (! sysctl (iobase_name, 3, &io.io_base, &len, NULL, 0)
132         && ! sysctl (ioshift_name, 3, &io.shift, &len, NULL, 0)) {
133         io.initdone = 1;
134         return 0;
135     }
136
137     n = readlink (PATH_ARM_SYSTYPE, systype, sizeof (systype) - 1);
138     if (n > 0) {
139         systype[n] = '\0';
140         if (isdigit (systype[0])) {
141             if (sscanf (systype, "%li,%i", &io.io_base, &io.shift) == 2) {
142                 io.initdone = 1;
143                 return 0;
144             }
145             /* else we're likely going to fail with the system match below */
146         }
147     }
148     else {
149         FILE * fp;
150
151         fp = fopen (PATH_CPUINFO, "r");
152         if (! fp)
153             return -1;
154         while ((n = fscanf (fp, "Hardware\t: %256[^\n]\n", systype)) != EOF) {
155             if (n == 1)
156                 break;
157             else
158                 fgets (systype, 256, fp);
159         }
160         fclose (fp);
161
162         if (n == EOF) {
163             /* this can happen if the format of /proc/cpuinfo changes...  */
164             fprintf (stderr, "ioperm: Unable to determine system type.\n"
165                      "\t(May need " PATH_ARM_SYSTYPE " symlink?)\n");
166             __set_errno (ENODEV);
167             return -1;
168         }
169     }
170
171     /* translate systype name into i/o system: */
172     for (i = 0; i < sizeof (platform) / sizeof (platform[0]); ++i) {
173         if (strcmp (platform[i].name, systype) == 0) {
174             io.shift = platform[i].shift;
175             io.io_base = platform[i].io_base;
176             io.initdone = 1;
177             return 0;
178         }
179     }
180
181     /* systype is not a known platform name... */
182     __set_errno (EINVAL);
183     return -1;
184 }
185
186 int ioperm (unsigned long int from, unsigned long int num, int turn_on)
187 {
188     if (! io.initdone && init_iosys () < 0)
189         return -1;
190
191     /* this test isn't as silly as it may look like; consider overflows! */
192     if (from >= MAX_PORT || from + num > MAX_PORT) {
193         __set_errno (EINVAL);
194         return -1;
195     }
196
197     if (turn_on) {
198         if (! io.base) {
199             int fd;
200
201             fd = open ("/dev/mem", O_RDWR);
202             if (fd < 0)
203                 return -1;
204
205             io.base = (unsigned long int) mmap (0, MAX_PORT << io.shift,
206                                           PROT_READ | PROT_WRITE,
207                                           MAP_SHARED, fd, io.io_base);
208             close (fd);
209             if ((long) io.base == -1)
210                 return -1;
211         }
212     }
213
214     return 0;
215 }
216 libc_hidden_def(ioperm)
217
218
219 void
220 outb(unsigned char b, unsigned long int port)
221 {
222     *((__volatile__ unsigned char *)(IO_ADDR (port))) = b;
223 }
224
225
226 void
227 outw(unsigned short b, unsigned long int port)
228 {
229     *((__volatile__ unsigned short *)(IO_ADDR (port))) = b;
230 }
231
232
233 void
234 outl(unsigned long b, unsigned long int port)
235 {
236     *((__volatile__ unsigned long *)(IO_ADDR (port))) = b;
237 }
238
239
240 unsigned char
241 inb (unsigned long int port)
242 {
243     return *((__volatile__ unsigned char *)(IO_ADDR (port)));
244 }
245
246
247 unsigned short int
248 inw(unsigned long int port)
249 {
250     return *((__volatile__ unsigned short *)(IO_ADDR (port)));
251 }
252
253
254 unsigned long int
255 inl(unsigned long int port)
256 {
257     return *((__volatile__ unsigned long *)(IO_ADDR (port)));
258 }