]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/unix/sysv/linux/arm/ioperm.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / arm / ioperm.c
CommitLineData
b168057a 1/* Copyright (C) 1998-2015 Free Software Foundation, Inc.
3d72808b
UD
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
3214b89b
AJ
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.
3d72808b
UD
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
3214b89b 14 Lesser General Public License for more details.
3d72808b 15
3214b89b 16 You should have received a copy of the GNU Lesser General Public
ab84e3ff
PE
17 License along with the GNU C Library. If not, see
18 <http://www.gnu.org/licenses/>. */
3d72808b
UD
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
aba75488
UD
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
3d72808b
UD
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>
42
43#include <sys/types.h>
44#include <sys/mman.h>
45
efaef362 46#include <linux/version.h>
aba75488 47#include <sys/sysctl.h>
3d72808b
UD
48
49#define PATH_ARM_SYSTYPE "/etc/arm_systype"
50#define PATH_CPUINFO "/proc/cpuinfo"
51
52#define MAX_PORT 0x10000
53
54static 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;
60
61#define IO_BASE_FOOTBRIDGE 0x7c000000
62#define IO_SHIFT_FOOTBRIDGE 0
63
64static 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},
58d7604e 73 {"Rebel-NetWinder", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
3d72808b
UD
74};
75
76#define IO_ADDR(port) (io.base + ((port) << io.shift))
77
78/*
aba75488
UD
79 * Initialize I/O system. There are several ways to get the information
80 * we need. Each is tried in turn until one succeeds.
81 *
efaef362 82 * 1. Sysctl (CTL_BUS, CTL_BUS_ISA, ISA_*). This is the preferred method
aba75488
UD
83 * but not all kernels support it.
84 *
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>".
3d72808b 91 *
aba75488
UD
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.
3d72808b
UD
95 */
96
efaef362
PB
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
102
3d72808b
UD
103static int
104init_iosys (void)
105{
106 char systype[256];
107 int i, n;
aba75488
UD
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);
111
5de92c17
JM
112 if (! __sysctl (iobase_name, 3, &io.io_base, &len, NULL, 0)
113 && ! __sysctl (ioshift_name, 3, &io.shift, &len, NULL, 0))
aba75488
UD
114 {
115 io.initdone = 1;
116 return 0;
117 }
3d72808b 118
5de92c17 119 n = __readlink (PATH_ARM_SYSTYPE, systype, sizeof (systype) - 1);
3d72808b
UD
120 if (n > 0)
121 {
122 systype[n] = '\0';
123 if (isdigit (systype[0]))
124 {
125 if (sscanf (systype, "%li,%i", &io.io_base, &io.shift) == 2)
126 {
127 io.initdone = 1;
128 return 0;
129 }
130 /* else we're likely going to fail with the system match below */
131 }
132 }
133 else
134 {
135 FILE * fp;
136
84715d22 137 fp = fopen (PATH_CPUINFO, "rce");
aba75488 138 if (! fp)
3d72808b
UD
139 return -1;
140 while ((n = fscanf (fp, "Hardware\t: %256[^\n]\n", systype))
141 != EOF)
142 {
143 if (n == 1)
144 break;
145 else
5de92c17 146 fgets_unlocked (systype, 256, fp);
3d72808b
UD
147 }
148 fclose (fp);
149
150 if (n == EOF)
151 {
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;
158 }
159 }
160
161 /* translate systype name into i/o system: */
162 for (i = 0; i < sizeof (platform) / sizeof (platform[0]); ++i)
163 {
164 if (strcmp (platform[i].name, systype) == 0)
165 {
166 io.shift = platform[i].shift;
167 io.io_base = platform[i].io_base;
168 io.initdone = 1;
169 return 0;
170 }
171 }
172
173 /* systype is not a known platform name... */
2b6aa9b3 174 __set_errno (ENODEV);
3d72808b
UD
175 return -1;
176}
177
178int
179_ioperm (unsigned long int from, unsigned long int num, int turn_on)
180{
aba75488 181 if (! io.initdone && init_iosys () < 0)
3d72808b
UD
182 return -1;
183
184 /* this test isn't as silly as it may look like; consider overflows! */
185 if (from >= MAX_PORT || from + num > MAX_PORT)
186 {
187 __set_errno (EINVAL);
188 return -1;
189 }
190
191 if (turn_on)
192 {
193 if (! io.base)
194 {
195 int fd;
196
5de92c17 197 fd = __open ("/dev/mem", O_RDWR);
3d72808b
UD
198 if (fd < 0)
199 return -1;
200
201 io.base =
2b6aa9b3
UD
202 (unsigned long int) __mmap (0, MAX_PORT << io.shift,
203 PROT_READ | PROT_WRITE,
3d72808b 204 MAP_SHARED, fd, io.io_base);
5de92c17 205 __close (fd);
3d72808b
UD
206 if ((long) io.base == -1)
207 return -1;
208 }
3d72808b 209 }
3d72808b 210
aba75488 211 return 0;
3d72808b
UD
212}
213
214
215int
216_iopl (unsigned int level)
217{
218 if (level > 3)
219 {
220 __set_errno (EINVAL);
221 return -1;
222 }
223 if (level)
224 {
225 return _ioperm (0, MAX_PORT, 1);
226 }
227 return 0;
228}
229
230
231void
232_outb (unsigned char b, unsigned long int port)
233{
3d72808b
UD
234 *((volatile unsigned char *)(IO_ADDR (port))) = b;
235}
236
237
238void
239_outw (unsigned short b, unsigned long int port)
240{
3d72808b
UD
241 *((volatile unsigned short *)(IO_ADDR (port))) = b;
242}
243
244
245void
246_outl (unsigned int b, unsigned long int port)
247{
3d72808b
UD
248 *((volatile unsigned long *)(IO_ADDR (port))) = b;
249}
250
251
252unsigned int
253_inb (unsigned long int port)
254{
255 return *((volatile unsigned char *)(IO_ADDR (port)));
256}
257
258
259unsigned int
260_inw (unsigned long int port)
261{
262 return *((volatile unsigned short *)(IO_ADDR (port)));
263}
264
265
266unsigned int
267_inl (unsigned long int port)
268{
269 return *((volatile unsigned long *)(IO_ADDR (port)));
270}
271
272weak_alias (_ioperm, ioperm);
273weak_alias (_iopl, iopl);
274weak_alias (_inb, inb);
275weak_alias (_inw, inw);
276weak_alias (_inl, inl);
277weak_alias (_outb, outb);
278weak_alias (_outw, outw);
279weak_alias (_outl, outl);