ioperm.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* Copyright (C) 1998, 1999 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.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. /* I/O port access on the ARM is something of a fiction. What we do is to
  18. map an appropriate area of /dev/mem into user space so that a program
  19. can blast away at the hardware in such a way as to generate I/O cycles
  20. on the bus. To insulate user code from dependencies on particular
  21. hardware we don't allow calls to inb() and friends to be inlined, but
  22. force them to come through code in here every time. Performance-critical
  23. registers tend to be memory mapped these days so this should be no big
  24. problem. */
  25. /* Once upon a time this file used mprotect to enable and disable
  26. access to particular areas of I/O space. Unfortunately the
  27. mprotect syscall also has the side effect of enabling caching for
  28. the area affected (this is a kernel limitation). So we now just
  29. enable all the ports all of the time. */
  30. #include <errno.h>
  31. #include <fcntl.h>
  32. #include <stdio.h>
  33. #include <ctype.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include <sys/types.h>
  38. #include <sys/mman.h>
  39. #include <asm/page.h>
  40. #include <sys/sysctl.h>
  41. #include <sys/io.h>
  42. libc_hidden_proto(ioperm)
  43. libc_hidden_proto(readlink)
  44. libc_hidden_proto(mmap)
  45. libc_hidden_proto(sscanf)
  46. libc_hidden_proto(fscanf)
  47. libc_hidden_proto(fprintf)
  48. libc_hidden_proto(fgets)
  49. libc_hidden_proto(fopen)
  50. libc_hidden_proto(fclose)
  51. libc_hidden_proto(strcmp)
  52. libc_hidden_proto(open)
  53. libc_hidden_proto(close)
  54. #include <linux/version.h>
  55. #define PATH_ARM_SYSTYPE "/etc/arm_systype"
  56. #define PATH_CPUINFO "/proc/cpuinfo"
  57. #define MAX_PORT 0x10000
  58. static struct {
  59. unsigned long int base;
  60. unsigned long int io_base;
  61. unsigned int shift;
  62. unsigned int initdone; /* since all the above could be 0 */
  63. } io;
  64. #define IO_BASE_FOOTBRIDGE 0x7c000000
  65. #define IO_SHIFT_FOOTBRIDGE 0
  66. static struct platform {
  67. const char *name;
  68. unsigned long int io_base;
  69. unsigned int shift;
  70. } platform[] = {
  71. /* All currently supported platforms are in fact the same. :-) */
  72. {"Chalice-CATS", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
  73. {"DEC-EBSA285", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
  74. {"Corel-NetWinder", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
  75. {"Rebel-NetWinder", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
  76. };
  77. #define IO_ADDR(port) (io.base + ((port) << io.shift))
  78. /*
  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. *
  82. * 1. Sysctl (CTL_BUS, BUS_ISA, ISA_*). This is the preferred method
  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>".
  91. *
  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.
  95. *
  96. * 4. BUS_ISA is changed to CTL_BUS_ISA (for kernel since 2.4.23).
  97. */
  98. static int
  99. init_iosys (void)
  100. {
  101. char systype[256];
  102. int i, n;
  103. #if LINUX_VERSION_CODE < 132119
  104. static int iobase_name[] = { CTL_BUS, BUS_ISA, BUS_ISA_PORT_BASE };
  105. static int ioshift_name[] = { CTL_BUS, BUS_ISA, BUS_ISA_PORT_SHIFT };
  106. #else
  107. static int iobase_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_BASE };
  108. static int ioshift_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_SHIFT };
  109. #endif
  110. size_t len = sizeof(io.base);
  111. if (! sysctl (iobase_name, 3, &io.io_base, &len, NULL, 0)
  112. && ! sysctl (ioshift_name, 3, &io.shift, &len, NULL, 0)) {
  113. io.initdone = 1;
  114. return 0;
  115. }
  116. n = readlink (PATH_ARM_SYSTYPE, systype, sizeof (systype) - 1);
  117. if (n > 0) {
  118. systype[n] = '\0';
  119. if (isdigit (systype[0])) {
  120. if (sscanf (systype, "%li,%i", &io.io_base, &io.shift) == 2) {
  121. io.initdone = 1;
  122. return 0;
  123. }
  124. /* else we're likely going to fail with the system match below */
  125. }
  126. }
  127. else {
  128. FILE * fp;
  129. fp = fopen (PATH_CPUINFO, "r");
  130. if (! fp)
  131. return -1;
  132. while ((n = fscanf (fp, "Hardware\t: %256[^\n]\n", systype)) != EOF) {
  133. if (n == 1)
  134. break;
  135. else
  136. fgets (systype, 256, fp);
  137. }
  138. fclose (fp);
  139. if (n == EOF) {
  140. /* this can happen if the format of /proc/cpuinfo changes... */
  141. fprintf (stderr, "ioperm: Unable to determine system type.\n"
  142. "\t(May need " PATH_ARM_SYSTYPE " symlink?)\n");
  143. __set_errno (ENODEV);
  144. return -1;
  145. }
  146. }
  147. /* translate systype name into i/o system: */
  148. for (i = 0; i < sizeof (platform) / sizeof (platform[0]); ++i) {
  149. if (strcmp (platform[i].name, systype) == 0) {
  150. io.shift = platform[i].shift;
  151. io.io_base = platform[i].io_base;
  152. io.initdone = 1;
  153. return 0;
  154. }
  155. }
  156. /* systype is not a known platform name... */
  157. __set_errno (EINVAL);
  158. return -1;
  159. }
  160. int ioperm (unsigned long int from, unsigned long int num, int turn_on)
  161. {
  162. if (! io.initdone && init_iosys () < 0)
  163. return -1;
  164. /* this test isn't as silly as it may look like; consider overflows! */
  165. if (from >= MAX_PORT || from + num > MAX_PORT) {
  166. __set_errno (EINVAL);
  167. return -1;
  168. }
  169. if (turn_on) {
  170. if (! io.base) {
  171. int fd;
  172. fd = open ("/dev/mem", O_RDWR);
  173. if (fd < 0)
  174. return -1;
  175. io.base = (unsigned long int) mmap (0, MAX_PORT << io.shift,
  176. PROT_READ | PROT_WRITE,
  177. MAP_SHARED, fd, io.io_base);
  178. close (fd);
  179. if ((long) io.base == -1)
  180. return -1;
  181. }
  182. }
  183. return 0;
  184. }
  185. libc_hidden_def(ioperm)
  186. void
  187. outb(unsigned char b, unsigned long int port)
  188. {
  189. *((__volatile__ unsigned char *)(IO_ADDR (port))) = b;
  190. }
  191. void
  192. outw(unsigned short b, unsigned long int port)
  193. {
  194. *((__volatile__ unsigned short *)(IO_ADDR (port))) = b;
  195. }
  196. void
  197. outl(unsigned long b, unsigned long int port)
  198. {
  199. *((__volatile__ unsigned long *)(IO_ADDR (port))) = b;
  200. }
  201. unsigned char
  202. inb (unsigned long int port)
  203. {
  204. return *((__volatile__ unsigned char *)(IO_ADDR (port)));
  205. }
  206. unsigned short int
  207. inw(unsigned long int port)
  208. {
  209. return *((__volatile__ unsigned short *)(IO_ADDR (port)));
  210. }
  211. unsigned long int
  212. inl(unsigned long int port)
  213. {
  214. return *((__volatile__ unsigned long *)(IO_ADDR (port)));
  215. }