]> 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
bfff8b1b 1/* Copyright (C) 1998-2017 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>
3d72808b
UD
40#include <unistd.h>
41
42#include <sys/types.h>
43#include <sys/mman.h>
44
aba75488 45#include <sys/sysctl.h>
3d72808b 46
3d72808b
UD
47#define MAX_PORT 0x10000
48
49static struct {
50 unsigned long int base;
51 unsigned long int io_base;
52 unsigned int shift;
53 unsigned int initdone; /* since all the above could be 0 */
54} io;
55
3d72808b
UD
56#define IO_ADDR(port) (io.base + ((port) << io.shift))
57
58/*
cc42170e
AJ
59 * Initialize I/O system. The io_bae and port_shift values are fetched
60 * using sysctl (CTL_BUS, CTL_BUS_ISA, ISA_*).
3d72808b
UD
61 */
62
63static int
64init_iosys (void)
65{
5db08777
AJ
66 static int iobase_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_BASE };
67 static int ioshift_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_SHIFT };
aba75488
UD
68 size_t len = sizeof(io.base);
69
5de92c17
JM
70 if (! __sysctl (iobase_name, 3, &io.io_base, &len, NULL, 0)
71 && ! __sysctl (ioshift_name, 3, &io.shift, &len, NULL, 0))
aba75488
UD
72 {
73 io.initdone = 1;
74 return 0;
75 }
3d72808b 76
cc42170e 77 /* sysctl has failed... */
2b6aa9b3 78 __set_errno (ENODEV);
3d72808b
UD
79 return -1;
80}
81
82int
83_ioperm (unsigned long int from, unsigned long int num, int turn_on)
84{
aba75488 85 if (! io.initdone && init_iosys () < 0)
3d72808b
UD
86 return -1;
87
88 /* this test isn't as silly as it may look like; consider overflows! */
89 if (from >= MAX_PORT || from + num > MAX_PORT)
90 {
91 __set_errno (EINVAL);
92 return -1;
93 }
94
95 if (turn_on)
96 {
97 if (! io.base)
98 {
99 int fd;
100
5de92c17 101 fd = __open ("/dev/mem", O_RDWR);
3d72808b
UD
102 if (fd < 0)
103 return -1;
104
105 io.base =
2b6aa9b3
UD
106 (unsigned long int) __mmap (0, MAX_PORT << io.shift,
107 PROT_READ | PROT_WRITE,
3d72808b 108 MAP_SHARED, fd, io.io_base);
5de92c17 109 __close (fd);
3d72808b
UD
110 if ((long) io.base == -1)
111 return -1;
112 }
3d72808b 113 }
3d72808b 114
aba75488 115 return 0;
3d72808b
UD
116}
117
118
119int
120_iopl (unsigned int level)
121{
122 if (level > 3)
123 {
124 __set_errno (EINVAL);
125 return -1;
126 }
127 if (level)
128 {
129 return _ioperm (0, MAX_PORT, 1);
130 }
131 return 0;
132}
133
134
135void
136_outb (unsigned char b, unsigned long int port)
137{
3d72808b
UD
138 *((volatile unsigned char *)(IO_ADDR (port))) = b;
139}
140
141
142void
143_outw (unsigned short b, unsigned long int port)
144{
3d72808b
UD
145 *((volatile unsigned short *)(IO_ADDR (port))) = b;
146}
147
148
149void
150_outl (unsigned int b, unsigned long int port)
151{
3d72808b
UD
152 *((volatile unsigned long *)(IO_ADDR (port))) = b;
153}
154
155
156unsigned int
157_inb (unsigned long int port)
158{
159 return *((volatile unsigned char *)(IO_ADDR (port)));
160}
161
162
163unsigned int
164_inw (unsigned long int port)
165{
166 return *((volatile unsigned short *)(IO_ADDR (port)));
167}
168
169
170unsigned int
171_inl (unsigned long int port)
172{
173 return *((volatile unsigned long *)(IO_ADDR (port)));
174}
175
176weak_alias (_ioperm, ioperm);
177weak_alias (_iopl, iopl);
178weak_alias (_inb, inb);
179weak_alias (_inw, inw);
180weak_alias (_inl, inl);
181weak_alias (_outb, outb);
182weak_alias (_outw, outw);
183weak_alias (_outl, outl);