io.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. *(u8 *)to = __raw_readb(from);
  28. from++;
  29. to++;
  30. count--;
  31. }
  32. while (count >= 8) {
  33. *(u64 *)to = __raw_readq(from);
  34. from += 8;
  35. to += 8;
  36. count -= 8;
  37. }
  38. while (count) {
  39. *(u8 *)to = __raw_readb(from);
  40. from++;
  41. to++;
  42. count--;
  43. }
  44. }
  45. EXPORT_SYMBOL(__memcpy_fromio);
  46. /*
  47. * Copy data from "real" memory space to IO memory space.
  48. */
  49. void __memcpy_toio(volatile void __iomem *to, const void *from, size_t count)
  50. {
  51. while (count && !IS_ALIGNED((unsigned long)to, 8)) {
  52. __raw_writeb(*(u8 *)from, to);
  53. from++;
  54. to++;
  55. count--;
  56. }
  57. while (count >= 8) {
  58. __raw_writeq(*(u64 *)from, to);
  59. from += 8;
  60. to += 8;
  61. count -= 8;
  62. }
  63. while (count) {
  64. __raw_writeb(*(u8 *)from, to);
  65. from++;
  66. to++;
  67. count--;
  68. }
  69. }
  70. EXPORT_SYMBOL(__memcpy_toio);
  71. /*
  72. * "memset" on IO memory space.
  73. */
  74. void __memset_io(volatile void __iomem *dst, int c, size_t count)
  75. {
  76. u64 qc = (u8)c;
  77. qc |= qc << 8;
  78. qc |= qc << 16;
  79. qc |= qc << 32;
  80. while (count && !IS_ALIGNED((unsigned long)dst, 8)) {
  81. __raw_writeb(c, dst);
  82. dst++;
  83. count--;
  84. }
  85. while (count >= 8) {
  86. __raw_writeq(qc, dst);
  87. dst += 8;
  88. count -= 8;
  89. }
  90. while (count) {
  91. __raw_writeb(c, dst);
  92. dst++;
  93. count--;
  94. }
  95. }
  96. EXPORT_SYMBOL(__memset_io);