io.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/module.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. void _memset_io(volatile void __iomem *addr, int c, unsigned long n)
  109. {
  110. void *p = (void __force *)addr;
  111. u32 lc = c;
  112. lc |= lc << 8;
  113. lc |= lc << 16;
  114. __asm__ __volatile__ ("sync" : : : "memory");
  115. while(n && !IO_CHECK_ALIGN(p, 4)) {
  116. *((volatile u8 *)p) = c;
  117. p++;
  118. n--;
  119. }
  120. while(n >= 4) {
  121. *((volatile u32 *)p) = lc;
  122. p += 4;
  123. n -= 4;
  124. }
  125. while(n) {
  126. *((volatile u8 *)p) = c;
  127. p++;
  128. n--;
  129. }
  130. __asm__ __volatile__ ("sync" : : : "memory");
  131. }
  132. EXPORT_SYMBOL(_memset_io);
  133. void _memcpy_fromio(void *dest, const volatile void __iomem *src,
  134. unsigned long n)
  135. {
  136. void *vsrc = (void __force *) src;
  137. __asm__ __volatile__ ("sync" : : : "memory");
  138. while(n && (!IO_CHECK_ALIGN(vsrc, 4) || !IO_CHECK_ALIGN(dest, 4))) {
  139. *((u8 *)dest) = *((volatile u8 *)vsrc);
  140. eieio();
  141. vsrc++;
  142. dest++;
  143. n--;
  144. }
  145. while(n > 4) {
  146. *((u32 *)dest) = *((volatile u32 *)vsrc);
  147. eieio();
  148. vsrc += 4;
  149. dest += 4;
  150. n -= 4;
  151. }
  152. while(n) {
  153. *((u8 *)dest) = *((volatile u8 *)vsrc);
  154. eieio();
  155. vsrc++;
  156. dest++;
  157. n--;
  158. }
  159. __asm__ __volatile__ ("sync" : : : "memory");
  160. }
  161. EXPORT_SYMBOL(_memcpy_fromio);
  162. void _memcpy_toio(volatile void __iomem *dest, const void *src, unsigned long n)
  163. {
  164. void *vdest = (void __force *) dest;
  165. __asm__ __volatile__ ("sync" : : : "memory");
  166. while(n && (!IO_CHECK_ALIGN(vdest, 4) || !IO_CHECK_ALIGN(src, 4))) {
  167. *((volatile u8 *)vdest) = *((u8 *)src);
  168. src++;
  169. vdest++;
  170. n--;
  171. }
  172. while(n > 4) {
  173. *((volatile u32 *)vdest) = *((volatile u32 *)src);
  174. src += 4;
  175. vdest += 4;
  176. n-=4;
  177. }
  178. while(n) {
  179. *((volatile u8 *)vdest) = *((u8 *)src);
  180. src++;
  181. vdest++;
  182. n--;
  183. }
  184. __asm__ __volatile__ ("sync" : : : "memory");
  185. }
  186. EXPORT_SYMBOL(_memcpy_toio);