ioperm.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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, see
  15. <http://www.gnu.org/licenses/>. */
  16. /* I/O port access on the ARM is something of a fiction. What we do is to
  17. map an appropriate area of /dev/mem into user space so that a program
  18. can blast away at the hardware in such a way as to generate I/O cycles
  19. on the bus. To insulate user code from dependencies on particular
  20. hardware we don't allow calls to inb() and friends to be inlined, but
  21. force them to come through code in here every time. Performance-critical
  22. registers tend to be memory mapped these days so this should be no big
  23. problem. */
  24. /* Once upon a time this file used mprotect to enable and disable
  25. access to particular areas of I/O space. Unfortunately the
  26. mprotect syscall also has the side effect of enabling caching for
  27. the area affected (this is a kernel limitation). So we now just
  28. enable all the ports all of the time. */
  29. #include <sys/io.h>
  30. #include <sys/mman.h>
  31. #include <sys/sysctl.h>
  32. #include <paths.h>
  33. #include <errno.h>
  34. #include <ctype.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #include <fcntl.h>
  39. #include <linux/version.h>
  40. #define PATH_ARM_SYSTYPE "/etc/arm_systype"
  41. #define PATH_CPUINFO "/proc/cpuinfo"
  42. #define MAX_PORT 0x10000
  43. static struct {
  44. unsigned long int base;
  45. unsigned long int io_base;
  46. unsigned int shift;
  47. unsigned int initdone; /* since all the above could be 0 */
  48. } io;
  49. #define IO_BASE_FOOTBRIDGE 0x7c000000
  50. #define IO_SHIFT_FOOTBRIDGE 0
  51. static struct platform {
  52. const char *name;
  53. unsigned long int io_base;
  54. unsigned int shift;
  55. } platform[] = {
  56. /* All currently supported platforms are in fact the same. :-) */
  57. {"Chalice-CATS", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
  58. {"DEC-EBSA285", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
  59. {"Corel-NetWinder", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
  60. {"Rebel-NetWinder", IO_BASE_FOOTBRIDGE, IO_SHIFT_FOOTBRIDGE},
  61. };
  62. #define IO_ADDR(port) (io.base + ((port) << io.shift))
  63. /*
  64. * Initialize I/O system. There are several ways to get the information
  65. * we need. Each is tried in turn until one succeeds.
  66. *
  67. * 1. Sysctl (CTL_BUS, BUS_ISA, ISA_*). This is the preferred method
  68. * but not all kernels support it.
  69. *
  70. * 2. Read the value (not the contents) of symlink PATH_ARM_SYSTYPE.
  71. * - If it matches one of the entries in the table above, use the
  72. * corresponding values.
  73. * - If it begins with a number, assume this is a previously
  74. * unsupported system and the values encode, in order,
  75. * "<io_base>,<port_shift>".
  76. *
  77. * 3. Lookup the "system type" field in /proc/cpuinfo. Again, if it
  78. * matches an entry in the platform[] table, use the corresponding
  79. * values.
  80. *
  81. * 4. BUS_ISA is changed to CTL_BUS_ISA (for kernel since 2.4.23).
  82. */
  83. static int
  84. init_iosys (void)
  85. {
  86. char systype[256];
  87. int i, n;
  88. #if LINUX_VERSION_CODE < 132119
  89. static int iobase_name[] = { CTL_BUS, BUS_ISA, BUS_ISA_PORT_BASE };
  90. static int ioshift_name[] = { CTL_BUS, BUS_ISA, BUS_ISA_PORT_SHIFT };
  91. #else
  92. static int iobase_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_BASE };
  93. static int ioshift_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_SHIFT };
  94. #endif
  95. size_t len = sizeof(io.base);
  96. if (! sysctl (iobase_name, 3, &io.io_base, &len, NULL, 0)
  97. && ! sysctl (ioshift_name, 3, &io.shift, &len, NULL, 0)) {
  98. io.initdone = 1;
  99. return 0;
  100. }
  101. n = readlink (PATH_ARM_SYSTYPE, systype, sizeof (systype) - 1);
  102. if (n > 0) {
  103. systype[n] = '\0';
  104. if (isdigit (systype[0])) {
  105. if (sscanf (systype, "%li,%i", &io.io_base, &io.shift) == 2) {
  106. io.initdone = 1;
  107. return 0;
  108. }
  109. /* else we're likely going to fail with the system match below */
  110. }
  111. }
  112. else {
  113. FILE * fp;
  114. fp = fopen (PATH_CPUINFO, "r");
  115. if (! fp)
  116. return -1;
  117. while ((n = fscanf (fp, "Hardware\t: %256[^\n]\n", systype)) != EOF) {
  118. if (n == 1)
  119. break;
  120. else
  121. fgets (systype, 256, fp);
  122. }
  123. fclose (fp);
  124. if (n == EOF) {
  125. /* this can happen if the format of /proc/cpuinfo changes... */
  126. fprintf (stderr, "ioperm: Unable to determine system type.\n"
  127. "\t(May need " PATH_ARM_SYSTYPE " symlink?)\n");
  128. __set_errno (ENODEV);
  129. return -1;
  130. }
  131. }
  132. /* translate systype name into i/o system: */
  133. for (i = 0; i < sizeof (platform) / sizeof (platform[0]); ++i) {
  134. if (strcmp (platform[i].name, systype) == 0) {
  135. io.shift = platform[i].shift;
  136. io.io_base = platform[i].io_base;
  137. io.initdone = 1;
  138. return 0;
  139. }
  140. }
  141. /* systype is not a known platform name... */
  142. __set_errno (EINVAL);
  143. return -1;
  144. }
  145. int ioperm (unsigned long int from, unsigned long int num, int turn_on)
  146. {
  147. if (! io.initdone && init_iosys () < 0)
  148. return -1;
  149. /* this test isn't as silly as it may look like; consider overflows! */
  150. if (from >= MAX_PORT || from + num > MAX_PORT) {
  151. __set_errno (EINVAL);
  152. return -1;
  153. }
  154. if (turn_on) {
  155. if (! io.base) {
  156. int fd;
  157. fd = open (_PATH_MEM, O_RDWR);
  158. if (fd < 0)
  159. return -1;
  160. io.base = (unsigned long int) mmap (0, MAX_PORT << io.shift,
  161. PROT_READ | PROT_WRITE,
  162. MAP_SHARED, fd, io.io_base);
  163. close (fd);
  164. if ((long) io.base == -1)
  165. return -1;
  166. }
  167. }
  168. return 0;
  169. }
  170. libc_hidden_def(ioperm)
  171. void
  172. outb(unsigned char b, unsigned long int port)
  173. {
  174. *((__volatile__ unsigned char *)(IO_ADDR (port))) = b;
  175. }
  176. void
  177. outw(unsigned short b, unsigned long int port)
  178. {
  179. *((__volatile__ unsigned short *)(IO_ADDR (port))) = b;
  180. }
  181. void
  182. outl(unsigned long b, unsigned long int port)
  183. {
  184. *((__volatile__ unsigned long *)(IO_ADDR (port))) = b;
  185. }
  186. unsigned char
  187. inb (unsigned long int port)
  188. {
  189. return *((__volatile__ unsigned char *)(IO_ADDR (port)));
  190. }
  191. unsigned short int
  192. inw(unsigned long int port)
  193. {
  194. return *((__volatile__ unsigned short *)(IO_ADDR (port)));
  195. }
  196. unsigned long int
  197. inl(unsigned long int port)
  198. {
  199. return *((__volatile__ unsigned long *)(IO_ADDR (port)));
  200. }