io.c 1.5 KB

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