io.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #ifndef __ASM_AVR32_IO_H
  2. #define __ASM_AVR32_IO_H
  3. #include <linux/bug.h>
  4. #include <linux/kernel.h>
  5. #include <linux/string.h>
  6. #include <linux/types.h>
  7. #include <asm/addrspace.h>
  8. #include <asm/byteorder.h>
  9. #include <mach/io.h>
  10. /* virt_to_phys will only work when address is in P1 or P2 */
  11. static __inline__ unsigned long virt_to_phys(volatile void *address)
  12. {
  13. return PHYSADDR(address);
  14. }
  15. static __inline__ void * phys_to_virt(unsigned long address)
  16. {
  17. return (void *)P1SEGADDR(address);
  18. }
  19. #define cached_to_phys(addr) ((unsigned long)PHYSADDR(addr))
  20. #define uncached_to_phys(addr) ((unsigned long)PHYSADDR(addr))
  21. #define phys_to_cached(addr) ((void *)P1SEGADDR(addr))
  22. #define phys_to_uncached(addr) ((void *)P2SEGADDR(addr))
  23. /*
  24. * Generic IO read/write. These perform native-endian accesses. Note
  25. * that some architectures will want to re-define __raw_{read,write}w.
  26. */
  27. extern void __raw_writesb(void __iomem *addr, const void *data, int bytelen);
  28. extern void __raw_writesw(void __iomem *addr, const void *data, int wordlen);
  29. extern void __raw_writesl(void __iomem *addr, const void *data, int longlen);
  30. extern void __raw_readsb(const void __iomem *addr, void *data, int bytelen);
  31. extern void __raw_readsw(const void __iomem *addr, void *data, int wordlen);
  32. extern void __raw_readsl(const void __iomem *addr, void *data, int longlen);
  33. static inline void __raw_writeb(u8 v, volatile void __iomem *addr)
  34. {
  35. *(volatile u8 __force *)addr = v;
  36. }
  37. static inline void __raw_writew(u16 v, volatile void __iomem *addr)
  38. {
  39. *(volatile u16 __force *)addr = v;
  40. }
  41. static inline void __raw_writel(u32 v, volatile void __iomem *addr)
  42. {
  43. *(volatile u32 __force *)addr = v;
  44. }
  45. static inline u8 __raw_readb(const volatile void __iomem *addr)
  46. {
  47. return *(const volatile u8 __force *)addr;
  48. }
  49. static inline u16 __raw_readw(const volatile void __iomem *addr)
  50. {
  51. return *(const volatile u16 __force *)addr;
  52. }
  53. static inline u32 __raw_readl(const volatile void __iomem *addr)
  54. {
  55. return *(const volatile u32 __force *)addr;
  56. }
  57. /* Convert I/O port address to virtual address */
  58. #ifndef __io
  59. # define __io(p) ((void *)phys_to_uncached(p))
  60. #endif
  61. /*
  62. * Not really sure about the best way to slow down I/O on
  63. * AVR32. Defining it as a no-op until we have an actual test case.
  64. */
  65. #define SLOW_DOWN_IO do { } while (0)
  66. #define __BUILD_MEMORY_SINGLE(pfx, bwl, type) \
  67. static inline void \
  68. pfx##write##bwl(type val, volatile void __iomem *addr) \
  69. { \
  70. volatile type *__addr; \
  71. type __val; \
  72. \
  73. __addr = (void *)__swizzle_addr_##bwl((unsigned long)(addr)); \
  74. __val = pfx##ioswab##bwl(__addr, val); \
  75. \
  76. BUILD_BUG_ON(sizeof(type) > sizeof(unsigned long)); \
  77. \
  78. *__addr = __val; \
  79. } \
  80. \
  81. static inline type pfx##read##bwl(const volatile void __iomem *addr) \
  82. { \
  83. volatile type *__addr; \
  84. type __val; \
  85. \
  86. __addr = (void *)__swizzle_addr_##bwl((unsigned long)(addr)); \
  87. \
  88. BUILD_BUG_ON(sizeof(type) > sizeof(unsigned long)); \
  89. \
  90. __val = *__addr; \
  91. return pfx##ioswab##bwl(__addr, __val); \
  92. }
  93. #define __BUILD_IOPORT_SINGLE(pfx, bwl, type, p, slow) \
  94. static inline void pfx##out##bwl##p(type val, unsigned long port) \
  95. { \
  96. volatile type *__addr; \
  97. type __val; \
  98. \
  99. __addr = __io(__swizzle_addr_##bwl(port)); \
  100. __val = pfx##ioswab##bwl(__addr, val); \
  101. \
  102. BUILD_BUG_ON(sizeof(type) > sizeof(unsigned long)); \
  103. \
  104. *__addr = __val; \
  105. slow; \
  106. } \
  107. \
  108. static inline type pfx##in##bwl##p(unsigned long port) \
  109. { \
  110. volatile type *__addr; \
  111. type __val; \
  112. \
  113. __addr = __io(__swizzle_addr_##bwl(port)); \
  114. \
  115. BUILD_BUG_ON(sizeof(type) > sizeof(unsigned long)); \
  116. \
  117. __val = *__addr; \
  118. slow; \
  119. \
  120. return pfx##ioswab##bwl(__addr, __val); \
  121. }
  122. #define __BUILD_MEMORY_PFX(bus, bwl, type) \
  123. __BUILD_MEMORY_SINGLE(bus, bwl, type)
  124. #define BUILDIO_MEM(bwl, type) \
  125. __BUILD_MEMORY_PFX(, bwl, type) \
  126. __BUILD_MEMORY_PFX(__mem_, bwl, type)
  127. #define __BUILD_IOPORT_PFX(bus, bwl, type) \
  128. __BUILD_IOPORT_SINGLE(bus, bwl, type, ,) \
  129. __BUILD_IOPORT_SINGLE(bus, bwl, type, _p, SLOW_DOWN_IO)
  130. #define BUILDIO_IOPORT(bwl, type) \
  131. __BUILD_IOPORT_PFX(, bwl, type) \
  132. __BUILD_IOPORT_PFX(__mem_, bwl, type)
  133. BUILDIO_MEM(b, u8)
  134. BUILDIO_MEM(w, u16)
  135. BUILDIO_MEM(l, u32)
  136. BUILDIO_IOPORT(b, u8)
  137. BUILDIO_IOPORT(w, u16)
  138. BUILDIO_IOPORT(l, u32)
  139. #define readb_relaxed readb
  140. #define readw_relaxed readw
  141. #define readl_relaxed readl
  142. #define readb_be __raw_readb
  143. #define readw_be __raw_readw
  144. #define readl_be __raw_readl
  145. #define writeb_relaxed writeb
  146. #define writew_relaxed writew
  147. #define writel_relaxed writel
  148. #define writeb_be __raw_writeb
  149. #define writew_be __raw_writew
  150. #define writel_be __raw_writel
  151. #define __BUILD_MEMORY_STRING(bwl, type) \
  152. static inline void writes##bwl(volatile void __iomem *addr, \
  153. const void *data, unsigned int count) \
  154. { \
  155. const type *__data = data; \
  156. \
  157. while (count--) \
  158. __mem_write##bwl(*__data++, addr); \
  159. } \
  160. \
  161. static inline void reads##bwl(const volatile void __iomem *addr, \
  162. void *data, unsigned int count) \
  163. { \
  164. type *__data = data; \
  165. \
  166. while (count--) \
  167. *__data++ = __mem_read##bwl(addr); \
  168. }
  169. #define __BUILD_IOPORT_STRING(bwl, type) \
  170. static inline void outs##bwl(unsigned long port, const void *data, \
  171. unsigned int count) \
  172. { \
  173. const type *__data = data; \
  174. \
  175. while (count--) \
  176. __mem_out##bwl(*__data++, port); \
  177. } \
  178. \
  179. static inline void ins##bwl(unsigned long port, void *data, \
  180. unsigned int count) \
  181. { \
  182. type *__data = data; \
  183. \
  184. while (count--) \
  185. *__data++ = __mem_in##bwl(port); \
  186. }
  187. #define BUILDSTRING(bwl, type) \
  188. __BUILD_MEMORY_STRING(bwl, type) \
  189. __BUILD_IOPORT_STRING(bwl, type)
  190. BUILDSTRING(b, u8)
  191. BUILDSTRING(w, u16)
  192. BUILDSTRING(l, u32)
  193. /*
  194. * io{read,write}{8,16,32} macros in both le (for PCI style consumers) and native be
  195. */
  196. #ifndef ioread8
  197. #define ioread8(p) ((unsigned int)readb(p))
  198. #define ioread16(p) ((unsigned int)readw(p))
  199. #define ioread16be(p) ((unsigned int)__raw_readw(p))
  200. #define ioread32(p) ((unsigned int)readl(p))
  201. #define ioread32be(p) ((unsigned int)__raw_readl(p))
  202. #define iowrite8(v,p) writeb(v, p)
  203. #define iowrite16(v,p) writew(v, p)
  204. #define iowrite16be(v,p) __raw_writew(v, p)
  205. #define iowrite32(v,p) writel(v, p)
  206. #define iowrite32be(v,p) __raw_writel(v, p)
  207. #define ioread8_rep(p,d,c) readsb(p,d,c)
  208. #define ioread16_rep(p,d,c) readsw(p,d,c)
  209. #define ioread32_rep(p,d,c) readsl(p,d,c)
  210. #define iowrite8_rep(p,s,c) writesb(p,s,c)
  211. #define iowrite16_rep(p,s,c) writesw(p,s,c)
  212. #define iowrite32_rep(p,s,c) writesl(p,s,c)
  213. #endif
  214. static inline void memcpy_fromio(void * to, const volatile void __iomem *from,
  215. unsigned long count)
  216. {
  217. memcpy(to, (const void __force *)from, count);
  218. }
  219. static inline void memcpy_toio(volatile void __iomem *to, const void * from,
  220. unsigned long count)
  221. {
  222. memcpy((void __force *)to, from, count);
  223. }
  224. static inline void memset_io(volatile void __iomem *addr, unsigned char val,
  225. unsigned long count)
  226. {
  227. memset((void __force *)addr, val, count);
  228. }
  229. #define mmiowb()
  230. #define IO_SPACE_LIMIT 0xffffffff
  231. extern void __iomem *__ioremap(unsigned long offset, size_t size,
  232. unsigned long flags);
  233. extern void __iounmap(void __iomem *addr);
  234. /*
  235. * ioremap - map bus memory into CPU space
  236. * @offset bus address of the memory
  237. * @size size of the resource to map
  238. *
  239. * ioremap performs a platform specific sequence of operations to make
  240. * bus memory CPU accessible via the readb/.../writel functions and
  241. * the other mmio helpers. The returned address is not guaranteed to
  242. * be usable directly as a virtual address.
  243. */
  244. #define ioremap(offset, size) \
  245. __ioremap((offset), (size), 0)
  246. #define ioremap_nocache(offset, size) \
  247. __ioremap((offset), (size), 0)
  248. #define iounmap(addr) \
  249. __iounmap(addr)
  250. #define ioremap_wc ioremap_nocache
  251. #define ioremap_wt ioremap_nocache
  252. #define ioremap_uc ioremap_nocache
  253. #define cached(addr) P1SEGADDR(addr)
  254. #define uncached(addr) P2SEGADDR(addr)
  255. #define virt_to_bus virt_to_phys
  256. #define bus_to_virt phys_to_virt
  257. #define page_to_bus page_to_phys
  258. #define bus_to_page phys_to_page
  259. /*
  260. * Create a virtual mapping cookie for an IO port range. There exists
  261. * no such thing as port-based I/O on AVR32, so a regular ioremap()
  262. * should do what we need.
  263. */
  264. #define ioport_map(port, nr) ioremap(port, nr)
  265. #define ioport_unmap(port) iounmap(port)
  266. /*
  267. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  268. * access
  269. */
  270. #define xlate_dev_mem_ptr(p) __va(p)
  271. /*
  272. * Convert a virtual cached pointer to an uncached pointer
  273. */
  274. #define xlate_dev_kmem_ptr(p) p
  275. #endif /* __ASM_AVR32_IO_H */