summaryrefslogtreecommitdiff
path: root/libc/sysdeps/linux/arm/ioperm.c
blob: 0b13e6c92b8b246e79650c2907d2778b8a77f36b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
   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
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

/* 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.  */

/* 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.  */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
//#include <sys/mman.h>
//#include <asm/page.h>


#define IO_BASE			0x7c000000
#define IO_SHIFT		0
#define IO_ADDR(port)	(IO_BASE + ((port) << IO_SHIFT))


#if 0
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 MAX_PORT	0x10000
int ioperm(unsigned long int from, unsigned long int num, int turn_on)
{
  /* 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;

	  fd = open ("/dev/mem", O_RDWR);
	  if (fd < 0)
	    return -1;

	  io.base = (unsigned long int) mmap (0, MAX_PORT << io.shift, 
					PROT_READ | PROT_WRITE, 
					MAP_SHARED, fd, io.io_base);
	  close (fd);
	  if ((long) io.base == -1)
	    return -1;
	}
    }

  return 0;
}


int iopl (unsigned int level)
{
    if (level > 3)
      {
	__set_errno (EINVAL);
	return -1;
      }
    if (level)
      {
	return _ioperm (0, MAX_PORT, 1);
      }
    return 0;
}
#endif

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)));
}