io.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Based on arch/arm/kernel/io.c
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/export.h>
  19. #include <linux/types.h>
  20. #include <linux/io.h>
  21. /*
  22. * Copy data from IO memory space to "real" memory space.
  23. */
  24. void __memcpy_fromio(void *to, const volatile void __iomem *from, size_t count)
  25. {
  26. while (count && (!IS_ALIGNED((unsigned long)from, 8) ||
  27. !IS_ALIGNED((unsigned long)to, 8))) {
  28. *(u8 *)to = __raw_readb(from);
  29. from++;
  30. to++;
  31. count--;
  32. }
  33. while (count >= 8) {
  34. *(u64 *)to = __raw_readq(from);
  35. from += 8;
  36. to += 8;
  37. count -= 8;
  38. }
  39. while (count) {
  40. *(u8 *)to = __raw_readb(from);
  41. from++;
  42. to++;
  43. count--;
  44. }
  45. }
  46. EXPORT_SYMBOL(__memcpy_fromio);
  47. /*
  48. * Copy data from "real" memory space to IO memory space.
  49. */
  50. void __memcpy_toio(volatile void __iomem *to, const void *from, size_t count)
  51. {
  52. while (count && (!IS_ALIGNED((unsigned long)to, 8) ||
  53. !IS_ALIGNED((unsigned long)from, 8))) {
  54. __raw_writeb(*(volatile u8 *)from, to);
  55. from++;
  56. to++;
  57. count--;
  58. }
  59. while (count >= 8) {
  60. __raw_writeq(*(volatile u64 *)from, to);
  61. from += 8;
  62. to += 8;
  63. count -= 8;
  64. }
  65. while (count) {
  66. __raw_writeb(*(volatile u8 *)from, to);
  67. from++;
  68. to++;
  69. count--;
  70. }
  71. }
  72. EXPORT_SYMBOL(__memcpy_toio);
  73. /*
  74. * "memset" on IO memory space.
  75. */
  76. void __memset_io(volatile void __iomem *dst, int c, size_t count)
  77. {
  78. u64 qc = (u8)c;
  79. qc |= qc << 8;
  80. qc |= qc << 16;
  81. qc |= qc << 32;
  82. while (count && !IS_ALIGNED((unsigned long)dst, 8)) {
  83. __raw_writeb(c, dst);
  84. dst++;
  85. count--;
  86. }
  87. while (count >= 8) {
  88. __raw_writeq(qc, dst);
  89. dst += 8;
  90. count -= 8;
  91. }
  92. while (count) {
  93. __raw_writeb(c, dst);
  94. dst++;
  95. count--;
  96. }
  97. }
  98. EXPORT_SYMBOL(__memset_io);