io.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Based on arch/arm/kernel/io.c
  4. *
  5. * Copyright (C) 2012 ARM Ltd.
  6. */
  7. #include <linux/export.h>
  8. #include <linux/types.h>
  9. #include <linux/io.h>
  10. /*
  11. * Copy data from IO memory space to "real" memory space.
  12. */
  13. void __memcpy_fromio(void *to, const volatile void __iomem *from, size_t count)
  14. {
  15. while (count && !IS_ALIGNED((unsigned long)from, 8)) {
  16. *(u8 *)to = __raw_readb(from);
  17. from++;
  18. to++;
  19. count--;
  20. }
  21. while (count >= 8) {
  22. *(u64 *)to = __raw_readq(from);
  23. from += 8;
  24. to += 8;
  25. count -= 8;
  26. }
  27. while (count) {
  28. *(u8 *)to = __raw_readb(from);
  29. from++;
  30. to++;
  31. count--;
  32. }
  33. }
  34. EXPORT_SYMBOL(__memcpy_fromio);
  35. /*
  36. * Copy data from "real" memory space to IO memory space.
  37. */
  38. void __memcpy_toio(volatile void __iomem *to, const void *from, size_t count)
  39. {
  40. while (count && !IS_ALIGNED((unsigned long)to, 8)) {
  41. __raw_writeb(*(u8 *)from, to);
  42. from++;
  43. to++;
  44. count--;
  45. }
  46. while (count >= 8) {
  47. __raw_writeq(*(u64 *)from, to);
  48. from += 8;
  49. to += 8;
  50. count -= 8;
  51. }
  52. while (count) {
  53. __raw_writeb(*(u8 *)from, to);
  54. from++;
  55. to++;
  56. count--;
  57. }
  58. }
  59. EXPORT_SYMBOL(__memcpy_toio);
  60. /*
  61. * "memset" on IO memory space.
  62. */
  63. void __memset_io(volatile void __iomem *dst, int c, size_t count)
  64. {
  65. u64 qc = (u8)c;
  66. qc |= qc << 8;
  67. qc |= qc << 16;
  68. qc |= qc << 32;
  69. while (count && !IS_ALIGNED((unsigned long)dst, 8)) {
  70. __raw_writeb(c, dst);
  71. dst++;
  72. count--;
  73. }
  74. while (count >= 8) {
  75. __raw_writeq(qc, dst);
  76. dst += 8;
  77. count -= 8;
  78. }
  79. while (count) {
  80. __raw_writeb(c, dst);
  81. dst++;
  82. count--;
  83. }
  84. }
  85. EXPORT_SYMBOL(__memset_io);