Add Changelog ...
[glibc.git] / sysdeps / unix / sysv / linux / arm / ioperm.c
blobedb29461a3b0e0fcef14395a9b92f7b907b9e14b
1 /* Copyright (C) 1998, 1999, 2003, 2005, 2008, 2011
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Phil Blundell, based on the Alpha version by
5 David Mosberger.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library. If not, see
19 <http://www.gnu.org/licenses/>. */
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. */
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. */
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>
44 #include <sys/types.h>
45 #include <sys/mman.h>
47 #include <linux/version.h>
48 #include <sys/sysctl.h>
50 #define PATH_ARM_SYSTYPE "/etc/arm_systype"
51 #define PATH_CPUINFO "/proc/cpuinfo"
53 #define MAX_PORT 0x10000
55 static struct {
56 unsigned long int base;
57 unsigned long int io_base;
58 unsigned int shift;
59 unsigned int initdone; /* since all the above could be 0 */
60 } io;
62 #define IO_BASE_FOOTBRIDGE 0x7c000000
63 #define IO_SHIFT_FOOTBRIDGE 0
65 static struct platform {
66 const char *name;
67 unsigned long int io_base;
68 unsigned int shift;
69 } platform[] = {
70 /* All currently supported platforms are in fact the same. :-) */
71 {"Chalice-CATS", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
72 {"DEC-EBSA285", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
73 {"Corel-NetWinder", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
74 {"Rebel-NetWinder", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
77 #define IO_ADDR(port) (io.base + ((port) << io.shift))
80 * Initialize I/O system. There are several ways to get the information
81 * we need. Each is tried in turn until one succeeds.
83 * 1. Sysctl (CTL_BUS, CTL_BUS_ISA, ISA_*). This is the preferred method
84 * but not all kernels support it.
86 * 2. Read the value (not the contents) of symlink PATH_ARM_SYSTYPE.
87 * - If it matches one of the entries in the table above, use the
88 * corresponding values.
89 * - If it begins with a number, assume this is a previously
90 * unsupported system and the values encode, in order,
91 * "<io_base>,<port_shift>".
93 * 3. Lookup the "system type" field in /proc/cpuinfo. Again, if it
94 * matches an entry in the platform[] table, use the corresponding
95 * values.
98 /* The Linux kernel headers renamed this constant between 2.5.26 and
99 2.5.27. It was backported to 2.4 between 2.4.22 and 2.4.23. */
100 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,23)
101 # define BUS_ISA CTL_BUS_ISA
102 #endif
104 static int
105 init_iosys (void)
107 char systype[256];
108 int i, n;
109 static int iobase_name[] = { CTL_BUS, BUS_ISA, BUS_ISA_PORT_BASE };
110 static int ioshift_name[] = { CTL_BUS, BUS_ISA, BUS_ISA_PORT_SHIFT };
111 size_t len = sizeof(io.base);
113 if (! __sysctl (iobase_name, 3, &io.io_base, &len, NULL, 0)
114 && ! __sysctl (ioshift_name, 3, &io.shift, &len, NULL, 0))
116 io.initdone = 1;
117 return 0;
120 n = __readlink (PATH_ARM_SYSTYPE, systype, sizeof (systype) - 1);
121 if (n > 0)
123 systype[n] = '\0';
124 if (isdigit (systype[0]))
126 if (sscanf (systype, "%li,%i", &io.io_base, &io.shift) == 2)
128 io.initdone = 1;
129 return 0;
131 /* else we're likely going to fail with the system match below */
134 else
136 FILE * fp;
138 fp = fopen (PATH_CPUINFO, "rce");
139 if (! fp)
140 return -1;
141 while ((n = fscanf (fp, "Hardware\t: %256[^\n]\n", systype))
142 != EOF)
144 if (n == 1)
145 break;
146 else
147 fgets_unlocked (systype, 256, fp);
149 fclose (fp);
151 if (n == EOF)
153 /* this can happen if the format of /proc/cpuinfo changes... */
154 fprintf (stderr,
155 "ioperm: Unable to determine system type.\n"
156 "\t(May need " PATH_ARM_SYSTYPE " symlink?)\n");
157 __set_errno (ENODEV);
158 return -1;
162 /* translate systype name into i/o system: */
163 for (i = 0; i < sizeof (platform) / sizeof (platform[0]); ++i)
165 if (strcmp (platform[i].name, systype) == 0)
167 io.shift = platform[i].shift;
168 io.io_base = platform[i].io_base;
169 io.initdone = 1;
170 return 0;
174 /* systype is not a known platform name... */
175 __set_errno (ENODEV);
176 return -1;
180 _ioperm (unsigned long int from, unsigned long int num, int turn_on)
182 if (! io.initdone && init_iosys () < 0)
183 return -1;
185 /* this test isn't as silly as it may look like; consider overflows! */
186 if (from >= MAX_PORT || from + num > MAX_PORT)
188 __set_errno (EINVAL);
189 return -1;
192 if (turn_on)
194 if (! io.base)
196 int fd;
198 fd = __open ("/dev/mem", O_RDWR);
199 if (fd < 0)
200 return -1;
202 io.base =
203 (unsigned long int) __mmap (0, MAX_PORT << io.shift,
204 PROT_READ | PROT_WRITE,
205 MAP_SHARED, fd, io.io_base);
206 __close (fd);
207 if ((long) io.base == -1)
208 return -1;
212 return 0;
217 _iopl (unsigned int level)
219 if (level > 3)
221 __set_errno (EINVAL);
222 return -1;
224 if (level)
226 return _ioperm (0, MAX_PORT, 1);
228 return 0;
232 void
233 _outb (unsigned char b, unsigned long int port)
235 *((volatile unsigned char *)(IO_ADDR (port))) = b;
239 void
240 _outw (unsigned short b, unsigned long int port)
242 *((volatile unsigned short *)(IO_ADDR (port))) = b;
246 void
247 _outl (unsigned int b, unsigned long int port)
249 *((volatile unsigned long *)(IO_ADDR (port))) = b;
253 unsigned int
254 _inb (unsigned long int port)
256 return *((volatile unsigned char *)(IO_ADDR (port)));
260 unsigned int
261 _inw (unsigned long int port)
263 return *((volatile unsigned short *)(IO_ADDR (port)));
267 unsigned int
268 _inl (unsigned long int port)
270 return *((volatile unsigned long *)(IO_ADDR (port)));
273 weak_alias (_ioperm, ioperm);
274 weak_alias (_iopl, iopl);
275 weak_alias (_inb, inb);
276 weak_alias (_inw, inw);
277 weak_alias (_inl, inl);
278 weak_alias (_outb, outb);
279 weak_alias (_outw, outw);
280 weak_alias (_outl, outl);