Update copyright dates with scripts/update-copyrights.
[glibc.git] / sysdeps / unix / sysv / linux / arm / ioperm.c
blob2291defc925e8682c0b5b558a1eaa16abf1650fd
1 /* Copyright (C) 1998-2015 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.
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.
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.
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/>. */
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. */
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. */
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <ctype.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
43 #include <sys/types.h>
44 #include <sys/mman.h>
46 #include <linux/version.h>
47 #include <sys/sysctl.h>
49 #define PATH_ARM_SYSTYPE "/etc/arm_systype"
50 #define PATH_CPUINFO "/proc/cpuinfo"
52 #define MAX_PORT 0x10000
54 static struct {
55 unsigned long int base;
56 unsigned long int io_base;
57 unsigned int shift;
58 unsigned int initdone; /* since all the above could be 0 */
59 } io;
61 #define IO_BASE_FOOTBRIDGE 0x7c000000
62 #define IO_SHIFT_FOOTBRIDGE 0
64 static struct platform {
65 const char *name;
66 unsigned long int io_base;
67 unsigned int shift;
68 } platform[] = {
69 /* All currently supported platforms are in fact the same. :-) */
70 {"Chalice-CATS", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
71 {"DEC-EBSA285", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
72 {"Corel-NetWinder", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
73 {"Rebel-NetWinder", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
76 #define IO_ADDR(port) (io.base + ((port) << io.shift))
79 * Initialize I/O system. There are several ways to get the information
80 * we need. Each is tried in turn until one succeeds.
82 * 1. Sysctl (CTL_BUS, CTL_BUS_ISA, ISA_*). This is the preferred method
83 * but not all kernels support it.
85 * 2. Read the value (not the contents) of symlink PATH_ARM_SYSTYPE.
86 * - If it matches one of the entries in the table above, use the
87 * corresponding values.
88 * - If it begins with a number, assume this is a previously
89 * unsupported system and the values encode, in order,
90 * "<io_base>,<port_shift>".
92 * 3. Lookup the "system type" field in /proc/cpuinfo. Again, if it
93 * matches an entry in the platform[] table, use the corresponding
94 * values.
97 /* The Linux kernel headers renamed this constant between 2.5.26 and
98 2.5.27. It was backported to 2.4 between 2.4.22 and 2.4.23. */
99 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,23)
100 # define BUS_ISA CTL_BUS_ISA
101 #endif
103 static int
104 init_iosys (void)
106 char systype[256];
107 int i, n;
108 static int iobase_name[] = { CTL_BUS, BUS_ISA, BUS_ISA_PORT_BASE };
109 static int ioshift_name[] = { CTL_BUS, BUS_ISA, BUS_ISA_PORT_SHIFT };
110 size_t len = sizeof(io.base);
112 if (! __sysctl (iobase_name, 3, &io.io_base, &len, NULL, 0)
113 && ! __sysctl (ioshift_name, 3, &io.shift, &len, NULL, 0))
115 io.initdone = 1;
116 return 0;
119 n = __readlink (PATH_ARM_SYSTYPE, systype, sizeof (systype) - 1);
120 if (n > 0)
122 systype[n] = '\0';
123 if (isdigit (systype[0]))
125 if (sscanf (systype, "%li,%i", &io.io_base, &io.shift) == 2)
127 io.initdone = 1;
128 return 0;
130 /* else we're likely going to fail with the system match below */
133 else
135 FILE * fp;
137 fp = fopen (PATH_CPUINFO, "rce");
138 if (! fp)
139 return -1;
140 while ((n = fscanf (fp, "Hardware\t: %256[^\n]\n", systype))
141 != EOF)
143 if (n == 1)
144 break;
145 else
146 fgets_unlocked (systype, 256, fp);
148 fclose (fp);
150 if (n == EOF)
152 /* this can happen if the format of /proc/cpuinfo changes... */
153 fprintf (stderr,
154 "ioperm: Unable to determine system type.\n"
155 "\t(May need " PATH_ARM_SYSTYPE " symlink?)\n");
156 __set_errno (ENODEV);
157 return -1;
161 /* translate systype name into i/o system: */
162 for (i = 0; i < sizeof (platform) / sizeof (platform[0]); ++i)
164 if (strcmp (platform[i].name, systype) == 0)
166 io.shift = platform[i].shift;
167 io.io_base = platform[i].io_base;
168 io.initdone = 1;
169 return 0;
173 /* systype is not a known platform name... */
174 __set_errno (ENODEV);
175 return -1;
179 _ioperm (unsigned long int from, unsigned long int num, int turn_on)
181 if (! io.initdone && init_iosys () < 0)
182 return -1;
184 /* this test isn't as silly as it may look like; consider overflows! */
185 if (from >= MAX_PORT || from + num > MAX_PORT)
187 __set_errno (EINVAL);
188 return -1;
191 if (turn_on)
193 if (! io.base)
195 int fd;
197 fd = __open ("/dev/mem", O_RDWR);
198 if (fd < 0)
199 return -1;
201 io.base =
202 (unsigned long int) __mmap (0, MAX_PORT << io.shift,
203 PROT_READ | PROT_WRITE,
204 MAP_SHARED, fd, io.io_base);
205 __close (fd);
206 if ((long) io.base == -1)
207 return -1;
211 return 0;
216 _iopl (unsigned int level)
218 if (level > 3)
220 __set_errno (EINVAL);
221 return -1;
223 if (level)
225 return _ioperm (0, MAX_PORT, 1);
227 return 0;
231 void
232 _outb (unsigned char b, unsigned long int port)
234 *((volatile unsigned char *)(IO_ADDR (port))) = b;
238 void
239 _outw (unsigned short b, unsigned long int port)
241 *((volatile unsigned short *)(IO_ADDR (port))) = b;
245 void
246 _outl (unsigned int b, unsigned long int port)
248 *((volatile unsigned long *)(IO_ADDR (port))) = b;
252 unsigned int
253 _inb (unsigned long int port)
255 return *((volatile unsigned char *)(IO_ADDR (port)));
259 unsigned int
260 _inw (unsigned long int port)
262 return *((volatile unsigned short *)(IO_ADDR (port)));
266 unsigned int
267 _inl (unsigned long int port)
269 return *((volatile unsigned long *)(IO_ADDR (port)));
272 weak_alias (_ioperm, ioperm);
273 weak_alias (_iopl, iopl);
274 weak_alias (_inb, inb);
275 weak_alias (_inw, inw);
276 weak_alias (_inl, inl);
277 weak_alias (_outb, outb);
278 weak_alias (_outw, outw);
279 weak_alias (_outl, outl);