2016-01-04 16:05:18 +00:00
|
|
|
/* Copyright (C) 1998-2016 Free Software Foundation, Inc.
|
1998-12-22 17:26:00 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
Contributed by Phil Blundell, based on the Alpha version by
|
|
|
|
David Mosberger.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 04:56:23 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
1998-12-22 17:26:00 +00:00
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 04:56:23 +00:00
|
|
|
Lesser General Public License for more details.
|
1998-12-22 17:26:00 +00:00
|
|
|
|
2001-07-06 04:56:23 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-03-09 23:56:38 +00:00
|
|
|
License along with the GNU C Library. If not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
1998-12-22 17:26:00 +00:00
|
|
|
|
|
|
|
/* I/O port access on the ARM is something of a fiction. What we do is to
|
|
|
|
map an appropriate area of /dev/mem into user space so that a program
|
|
|
|
can blast away at the hardware in such a way as to generate I/O cycles
|
|
|
|
on the bus. To insulate user code from dependencies on particular
|
|
|
|
hardware we don't allow calls to inb() and friends to be inlined, but
|
|
|
|
force them to come through code in here every time. Performance-critical
|
|
|
|
registers tend to be memory mapped these days so this should be no big
|
|
|
|
problem. */
|
|
|
|
|
2000-01-04 23:54:35 +00:00
|
|
|
/* Once upon a time this file used mprotect to enable and disable
|
|
|
|
access to particular areas of I/O space. Unfortunately the
|
|
|
|
mprotect syscall also has the side effect of enabling caching for
|
|
|
|
the area affected (this is a kernel limitation). So we now just
|
|
|
|
enable all the ports all of the time. */
|
|
|
|
|
1998-12-22 17:26:00 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2000-01-04 23:54:35 +00:00
|
|
|
#include <sys/sysctl.h>
|
1998-12-22 17:26:00 +00:00
|
|
|
|
|
|
|
#define MAX_PORT 0x10000
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
unsigned long int base;
|
|
|
|
unsigned long int io_base;
|
|
|
|
unsigned int shift;
|
|
|
|
unsigned int initdone; /* since all the above could be 0 */
|
|
|
|
} io;
|
|
|
|
|
|
|
|
#define IO_ADDR(port) (io.base + ((port) << io.shift))
|
|
|
|
|
|
|
|
/*
|
2015-12-29 14:27:56 +00:00
|
|
|
* Initialize I/O system. The io_bae and port_shift values are fetched
|
|
|
|
* using sysctl (CTL_BUS, CTL_BUS_ISA, ISA_*).
|
1998-12-22 17:26:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
init_iosys (void)
|
|
|
|
{
|
2015-12-11 21:16:34 +00:00
|
|
|
static int iobase_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_BASE };
|
|
|
|
static int ioshift_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_SHIFT };
|
2000-01-04 23:54:35 +00:00
|
|
|
size_t len = sizeof(io.base);
|
|
|
|
|
2008-08-19 15:59:07 +00:00
|
|
|
if (! __sysctl (iobase_name, 3, &io.io_base, &len, NULL, 0)
|
|
|
|
&& ! __sysctl (ioshift_name, 3, &io.shift, &len, NULL, 0))
|
2000-01-04 23:54:35 +00:00
|
|
|
{
|
|
|
|
io.initdone = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
1998-12-22 17:26:00 +00:00
|
|
|
|
2015-12-29 14:27:56 +00:00
|
|
|
/* sysctl has failed... */
|
2003-02-20 22:22:35 +00:00
|
|
|
__set_errno (ENODEV);
|
1998-12-22 17:26:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_ioperm (unsigned long int from, unsigned long int num, int turn_on)
|
|
|
|
{
|
2000-01-04 23:54:35 +00:00
|
|
|
if (! io.initdone && init_iosys () < 0)
|
1998-12-22 17:26:00 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* this test isn't as silly as it may look like; consider overflows! */
|
|
|
|
if (from >= MAX_PORT || from + num > MAX_PORT)
|
|
|
|
{
|
|
|
|
__set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (turn_on)
|
|
|
|
{
|
|
|
|
if (! io.base)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2008-08-19 15:59:07 +00:00
|
|
|
fd = __open ("/dev/mem", O_RDWR);
|
1998-12-22 17:26:00 +00:00
|
|
|
if (fd < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
io.base =
|
2003-02-20 22:22:35 +00:00
|
|
|
(unsigned long int) __mmap (0, MAX_PORT << io.shift,
|
|
|
|
PROT_READ | PROT_WRITE,
|
1998-12-22 17:26:00 +00:00
|
|
|
MAP_SHARED, fd, io.io_base);
|
2008-08-19 15:59:07 +00:00
|
|
|
__close (fd);
|
1998-12-22 17:26:00 +00:00
|
|
|
if ((long) io.base == -1)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-04 23:54:35 +00:00
|
|
|
return 0;
|
1998-12-22 17:26:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
_iopl (unsigned int level)
|
|
|
|
{
|
|
|
|
if (level > 3)
|
|
|
|
{
|
|
|
|
__set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (level)
|
|
|
|
{
|
|
|
|
return _ioperm (0, MAX_PORT, 1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
_outb (unsigned char b, unsigned long int port)
|
|
|
|
{
|
|
|
|
*((volatile unsigned char *)(IO_ADDR (port))) = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
_outw (unsigned short b, unsigned long int port)
|
|
|
|
{
|
|
|
|
*((volatile unsigned short *)(IO_ADDR (port))) = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
_outl (unsigned int b, unsigned long int port)
|
|
|
|
{
|
|
|
|
*((volatile unsigned long *)(IO_ADDR (port))) = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
_inb (unsigned long int port)
|
|
|
|
{
|
|
|
|
return *((volatile unsigned char *)(IO_ADDR (port)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
_inw (unsigned long int port)
|
|
|
|
{
|
|
|
|
return *((volatile unsigned short *)(IO_ADDR (port)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
_inl (unsigned long int port)
|
|
|
|
{
|
|
|
|
return *((volatile unsigned long *)(IO_ADDR (port)));
|
|
|
|
}
|
|
|
|
|
|
|
|
weak_alias (_ioperm, ioperm);
|
|
|
|
weak_alias (_iopl, iopl);
|
|
|
|
weak_alias (_inb, inb);
|
|
|
|
weak_alias (_inw, inw);
|
|
|
|
weak_alias (_inl, inl);
|
|
|
|
weak_alias (_outb, outb);
|
|
|
|
weak_alias (_outw, outw);
|
|
|
|
weak_alias (_outl, outl);
|