io.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * I/O string operations
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. * Copyright (C) 2006 IBM Corporation
  5. *
  6. * Largely rewritten by Cort Dougan (cort@cs.nmt.edu)
  7. * and Paul Mackerras.
  8. *
  9. * Adapted for iSeries by Mike Corrigan (mikejc@us.ibm.com)
  10. * PPC64 updates by Dave Engebretsen (engebret@us.ibm.com)
  11. *
  12. * Rewritten in C by Stephen Rothwell.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version
  17. * 2 of the License, or (at your option) any later version.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/types.h>
  21. #include <linux/compiler.h>
  22. #include <linux/export.h>
  23. #include <asm/io.h>
  24. #include <asm/firmware.h>
  25. #include <asm/bug.h>
  26. void _insb(const volatile u8 __iomem *port, void *buf, long count)
  27. {
  28. u8 *tbuf = buf;
  29. u8 tmp;
  30. if (unlikely(count <= 0))
  31. return;
  32. asm volatile("sync");
  33. do {
  34. tmp = *port;
  35. eieio();
  36. *tbuf++ = tmp;
  37. } while (--count != 0);
  38. asm volatile("twi 0,%0,0; isync" : : "r" (tmp));
  39. }
  40. EXPORT_SYMBOL(_insb);
  41. void _outsb(volatile u8 __iomem *port, const void *buf, long count)
  42. {
  43. const u8 *tbuf = buf;
  44. if (unlikely(count <= 0))
  45. return;
  46. asm volatile("sync");
  47. do {
  48. *port = *tbuf++;
  49. } while (--count != 0);
  50. asm volatile("sync");
  51. }
  52. EXPORT_SYMBOL(_outsb);
  53. void _insw_ns(const volatile u16 __iomem *port, void *buf, long count)
  54. {
  55. u16 *tbuf = buf;
  56. u16 tmp;
  57. if (unlikely(count <= 0))
  58. return;
  59. asm volatile("sync");
  60. do {
  61. tmp = *port;
  62. eieio();
  63. *tbuf++ = tmp;
  64. } while (--count != 0);
  65. asm volatile("twi 0,%0,0; isync" : : "r" (tmp));
  66. }
  67. EXPORT_SYMBOL(_insw_ns);
  68. void _outsw_ns(volatile u16 __iomem *port, const void *buf, long count)
  69. {
  70. const u16 *tbuf = buf;
  71. if (unlikely(count <= 0))
  72. return;
  73. asm volatile("sync");
  74. do {
  75. *port = *tbuf++;
  76. } while (--count != 0);
  77. asm volatile("sync");
  78. }
  79. EXPORT_SYMBOL(_outsw_ns);
  80. void _insl_ns(const volatile u32 __iomem *port, void *buf, long count)
  81. {
  82. u32 *tbuf = buf;
  83. u32 tmp;
  84. if (unlikely(count <= 0))
  85. return;
  86. asm volatile("sync");
  87. do {
  88. tmp = *port;
  89. eieio();
  90. *tbuf++ = tmp;
  91. } while (--count != 0);
  92. asm volatile("twi 0,%0,0; isync" : : "r" (tmp));
  93. }
  94. EXPORT_SYMBOL(_insl_ns);
  95. void _outsl_ns(volatile u32 __iomem *port, const void *buf, long count)
  96. {
  97. const u32 *tbuf = buf;
  98. if (unlikely(count <= 0))
  99. return;
  100. asm volatile("sync");
  101. do {
  102. *port = *tbuf++;
  103. } while (--count != 0);
  104. asm volatile("sync");
  105. }
  106. EXPORT_SYMBOL(_outsl_ns);
  107. #define IO_CHECK_ALIGN(v,a) ((((unsigned long)(v)) & ((a) - 1)) == 0)
  108. notrace void
  109. _memset_io(volatile void __iomem *addr, int c, unsigned long n)
  110. {
  111. void *p = (void __force *)addr;
  112. u32 lc = c;
  113. lc |= lc << 8;
  114. lc |= lc << 16;
  115. __asm__ __volatile__ ("sync" : : : "memory");
  116. while(n && !IO_CHECK_ALIGN(p, 4)) {
  117. *((volatile u8 *)p) = c;
  118. p++;
  119. n--;
  120. }
  121. while(n >= 4) {
  122. *((volatile u32 *)p) = lc;
  123. p += 4;
  124. n -= 4;
  125. }
  126. while(n) {
  127. *((volatile u8 *)p) = c;
  128. p++;
  129. n--;
  130. }
  131. __asm__ __volatile__ ("sync" : : : "memory");
  132. }
  133. EXPORT_SYMBOL(_memset_io);
  134. void _memcpy_fromio(void *dest, const volatile void __iomem *src,
  135. unsigned long n)
  136. {
  137. void *vsrc = (void __force *) src;
  138. __asm__ __volatile__ ("sync" : : : "memory");
  139. while(n && (!IO_CHECK_ALIGN(vsrc, 4) || !IO_CHECK_ALIGN(dest, 4))) {
  140. *((u8 *)dest) = *((volatile u8 *)vsrc);
  141. eieio();
  142. vsrc++;
  143. dest++;
  144. n--;
  145. }
  146. while(n >= 4) {
  147. *((u32 *)dest) = *((volatile u32 *)vsrc);
  148. eieio();
  149. vsrc += 4;
  150. dest += 4;
  151. n -= 4;
  152. }
  153. while(n) {
  154. *((u8 *)dest) = *((volatile u8 *)vsrc);
  155. eieio();
  156. vsrc++;
  157. dest++;
  158. n--;
  159. }
  160. __asm__ __volatile__ ("sync" : : : "memory");
  161. }
  162. EXPORT_SYMBOL(_memcpy_fromio);
  163. void _memcpy_toio(volatile void __iomem *dest, const void *src, unsigned long n)
  164. {
  165. void *vdest = (void __force *) dest;
  166. __asm__ __volatile__ ("sync" : : : "memory");
  167. while(n && (!IO_CHECK_ALIGN(vdest, 4) || !IO_CHECK_ALIGN(src, 4))) {
  168. *((volatile u8 *)vdest) = *((u8 *)src);
  169. src++;
  170. vdest++;
  171. n--;
  172. }
  173. while(n >= 4) {
  174. *((volatile u32 *)vdest) = *((volatile u32 *)src);
  175. src += 4;
  176. vdest += 4;
  177. n-=4;
  178. }
  179. while(n) {
  180. *((volatile u8 *)vdest) = *((u8 *)src);
  181. src++;
  182. vdest++;
  183. n--;
  184. }
  185. __asm__ __volatile__ ("sync" : : : "memory");
  186. }
  187. EXPORT_SYMBOL(_memcpy_toio);