iomap.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Implement the default iomap interfaces
  4. *
  5. * (C) Copyright 2004 Linus Torvalds
  6. * (C) Copyright 2006 Ralf Baechle <ralf@linux-mips.org>
  7. * (C) Copyright 2007 MIPS Technologies, Inc.
  8. * written by Ralf Baechle <ralf@linux-mips.org>
  9. */
  10. #include <linux/export.h>
  11. #include <asm/io.h>
  12. /*
  13. * Read/write from/to an (offsettable) iomem cookie. It might be a PIO
  14. * access or a MMIO access, these functions don't care. The info is
  15. * encoded in the hardware mapping set up by the mapping functions
  16. * (or the cookie itself, depending on implementation and hw).
  17. *
  18. * The generic routines don't assume any hardware mappings, and just
  19. * encode the PIO/MMIO as part of the cookie. They coldly assume that
  20. * the MMIO IO mappings are not in the low address range.
  21. *
  22. * Architectures for which this is not true can't use this generic
  23. * implementation and should do their own copy.
  24. */
  25. #define PIO_MASK 0x0ffffUL
  26. unsigned int ioread8(void __iomem *addr)
  27. {
  28. return readb(addr);
  29. }
  30. EXPORT_SYMBOL(ioread8);
  31. unsigned int ioread16(void __iomem *addr)
  32. {
  33. return readw(addr);
  34. }
  35. EXPORT_SYMBOL(ioread16);
  36. unsigned int ioread16be(void __iomem *addr)
  37. {
  38. return be16_to_cpu(__raw_readw(addr));
  39. }
  40. EXPORT_SYMBOL(ioread16be);
  41. unsigned int ioread32(void __iomem *addr)
  42. {
  43. return readl(addr);
  44. }
  45. EXPORT_SYMBOL(ioread32);
  46. unsigned int ioread32be(void __iomem *addr)
  47. {
  48. return be32_to_cpu(__raw_readl(addr));
  49. }
  50. EXPORT_SYMBOL(ioread32be);
  51. void iowrite8(u8 val, void __iomem *addr)
  52. {
  53. writeb(val, addr);
  54. }
  55. EXPORT_SYMBOL(iowrite8);
  56. void iowrite16(u16 val, void __iomem *addr)
  57. {
  58. writew(val, addr);
  59. }
  60. EXPORT_SYMBOL(iowrite16);
  61. void iowrite16be(u16 val, void __iomem *addr)
  62. {
  63. __raw_writew(cpu_to_be16(val), addr);
  64. }
  65. EXPORT_SYMBOL(iowrite16be);
  66. void iowrite32(u32 val, void __iomem *addr)
  67. {
  68. writel(val, addr);
  69. }
  70. EXPORT_SYMBOL(iowrite32);
  71. void iowrite32be(u32 val, void __iomem *addr)
  72. {
  73. __raw_writel(cpu_to_be32(val), addr);
  74. }
  75. EXPORT_SYMBOL(iowrite32be);
  76. /*
  77. * These are the "repeat MMIO read/write" functions.
  78. * Note the "__mem" accesses, since we want to convert
  79. * to CPU byte order if the host bus happens to not match the
  80. * endianness of PCI/ISA (see mach-generic/mangle-port.h).
  81. */
  82. static inline void mmio_insb(void __iomem *addr, u8 *dst, int count)
  83. {
  84. while (--count >= 0) {
  85. u8 data = __mem_readb(addr);
  86. *dst = data;
  87. dst++;
  88. }
  89. }
  90. static inline void mmio_insw(void __iomem *addr, u16 *dst, int count)
  91. {
  92. while (--count >= 0) {
  93. u16 data = __mem_readw(addr);
  94. *dst = data;
  95. dst++;
  96. }
  97. }
  98. static inline void mmio_insl(void __iomem *addr, u32 *dst, int count)
  99. {
  100. while (--count >= 0) {
  101. u32 data = __mem_readl(addr);
  102. *dst = data;
  103. dst++;
  104. }
  105. }
  106. static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count)
  107. {
  108. while (--count >= 0) {
  109. __mem_writeb(*src, addr);
  110. src++;
  111. }
  112. }
  113. static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count)
  114. {
  115. while (--count >= 0) {
  116. __mem_writew(*src, addr);
  117. src++;
  118. }
  119. }
  120. static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)
  121. {
  122. while (--count >= 0) {
  123. __mem_writel(*src, addr);
  124. src++;
  125. }
  126. }
  127. void ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
  128. {
  129. mmio_insb(addr, dst, count);
  130. }
  131. EXPORT_SYMBOL(ioread8_rep);
  132. void ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
  133. {
  134. mmio_insw(addr, dst, count);
  135. }
  136. EXPORT_SYMBOL(ioread16_rep);
  137. void ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
  138. {
  139. mmio_insl(addr, dst, count);
  140. }
  141. EXPORT_SYMBOL(ioread32_rep);
  142. void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
  143. {
  144. mmio_outsb(addr, src, count);
  145. }
  146. EXPORT_SYMBOL(iowrite8_rep);
  147. void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
  148. {
  149. mmio_outsw(addr, src, count);
  150. }
  151. EXPORT_SYMBOL(iowrite16_rep);
  152. void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
  153. {
  154. mmio_outsl(addr, src, count);
  155. }
  156. EXPORT_SYMBOL(iowrite32_rep);
  157. /*
  158. * Create a virtual mapping cookie for an IO port range
  159. *
  160. * This uses the same mapping are as the in/out family which has to be setup
  161. * by the platform initialization code.
  162. *
  163. * Just to make matters somewhat more interesting on MIPS systems with
  164. * multiple host bridge each will have it's own ioport address space.
  165. */
  166. static void __iomem *ioport_map_legacy(unsigned long port, unsigned int nr)
  167. {
  168. return (void __iomem *) (mips_io_port_base + port);
  169. }
  170. void __iomem *ioport_map(unsigned long port, unsigned int nr)
  171. {
  172. if (port > PIO_MASK)
  173. return NULL;
  174. return ioport_map_legacy(port, nr);
  175. }
  176. EXPORT_SYMBOL(ioport_map);
  177. void ioport_unmap(void __iomem *addr)
  178. {
  179. /* Nothing to do */
  180. }
  181. EXPORT_SYMBOL(ioport_unmap);