io.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include <linux/export.h>
  2. #include <linux/types.h>
  3. #include <linux/io.h>
  4. #include <linux/spinlock.h>
  5. static DEFINE_RAW_SPINLOCK(__io_lock);
  6. /*
  7. * Generic atomic MMIO modify.
  8. *
  9. * Allows thread-safe access to registers shared by unrelated subsystems.
  10. * The access is protected by a single MMIO-wide lock.
  11. */
  12. void atomic_io_modify_relaxed(void __iomem *reg, u32 mask, u32 set)
  13. {
  14. unsigned long flags;
  15. u32 value;
  16. raw_spin_lock_irqsave(&__io_lock, flags);
  17. value = readl_relaxed(reg) & ~mask;
  18. value |= (set & mask);
  19. writel_relaxed(value, reg);
  20. raw_spin_unlock_irqrestore(&__io_lock, flags);
  21. }
  22. EXPORT_SYMBOL(atomic_io_modify_relaxed);
  23. void atomic_io_modify(void __iomem *reg, u32 mask, u32 set)
  24. {
  25. unsigned long flags;
  26. u32 value;
  27. raw_spin_lock_irqsave(&__io_lock, flags);
  28. value = readl_relaxed(reg) & ~mask;
  29. value |= (set & mask);
  30. writel(value, reg);
  31. raw_spin_unlock_irqrestore(&__io_lock, flags);
  32. }
  33. EXPORT_SYMBOL(atomic_io_modify);
  34. /*
  35. * Copy data from IO memory space to "real" memory space.
  36. * This needs to be optimized.
  37. */
  38. void _memcpy_fromio(void *to, const volatile void __iomem *from, size_t count)
  39. {
  40. unsigned char *t = to;
  41. while (count) {
  42. count--;
  43. *t = readb(from);
  44. t++;
  45. from++;
  46. }
  47. }
  48. EXPORT_SYMBOL(_memcpy_fromio);
  49. /*
  50. * Copy data from "real" memory space to IO memory space.
  51. * This needs to be optimized.
  52. */
  53. void _memcpy_toio(volatile void __iomem *to, const void *from, size_t count)
  54. {
  55. const unsigned char *f = from;
  56. while (count) {
  57. count--;
  58. writeb(*f, to);
  59. f++;
  60. to++;
  61. }
  62. }
  63. EXPORT_SYMBOL(_memcpy_toio);
  64. /*
  65. * "memset" on IO memory space.
  66. * This needs to be optimized.
  67. */
  68. void _memset_io(volatile void __iomem *dst, int c, size_t count)
  69. {
  70. while (count) {
  71. count--;
  72. writeb(c, dst);
  73. dst++;
  74. }
  75. }
  76. EXPORT_SYMBOL(_memset_io);