io.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #ifndef __ASM_SH_IO_H
  2. #define __ASM_SH_IO_H
  3. /*
  4. * Convention:
  5. * read{b,w,l}/write{b,w,l} are for PCI,
  6. * while in{b,w,l}/out{b,w,l} are for ISA
  7. * These may (will) be platform specific function.
  8. * In addition we have 'pausing' versions: in{b,w,l}_p/out{b,w,l}_p
  9. * and 'string' versions: ins{b,w,l}/outs{b,w,l}
  10. * For read{b,w,l} and write{b,w,l} there are also __raw versions, which
  11. * do not have a memory barrier after them.
  12. *
  13. * In addition, we have
  14. * ctrl_in{b,w,l}/ctrl_out{b,w,l} for SuperH specific I/O.
  15. * which are processor specific.
  16. */
  17. /*
  18. * We follow the Alpha convention here:
  19. * __inb expands to an inline function call (which calls via the mv)
  20. * _inb is a real function call (note ___raw fns are _ version of __raw)
  21. * inb by default expands to _inb, but the machine specific code may
  22. * define it to __inb if it chooses.
  23. */
  24. #include <linux/config.h>
  25. #include <asm/cache.h>
  26. #include <asm/system.h>
  27. #include <asm/addrspace.h>
  28. #include <asm/machvec.h>
  29. #include <asm/pgtable.h>
  30. #include <asm-generic/iomap.h>
  31. #ifdef __KERNEL__
  32. /*
  33. * Depending on which platform we are running on, we need different
  34. * I/O functions.
  35. */
  36. #define __IO_PREFIX generic
  37. #include <asm/io_generic.h>
  38. #define maybebadio(port) \
  39. printk(KERN_ERR "bad PC-like io %s:%u for port 0x%lx at 0x%08x\n", \
  40. __FUNCTION__, __LINE__, (port), (u32)__builtin_return_address(0))
  41. /*
  42. * Since boards are able to define their own set of I/O routines through
  43. * their respective machine vector, we always wrap through the mv.
  44. *
  45. * Also, in the event that a board hasn't provided its own definition for
  46. * a given routine, it will be wrapped to generic code at run-time.
  47. */
  48. #define __inb(p) sh_mv.mv_inb((p))
  49. #define __inw(p) sh_mv.mv_inw((p))
  50. #define __inl(p) sh_mv.mv_inl((p))
  51. #define __outb(x,p) sh_mv.mv_outb((x),(p))
  52. #define __outw(x,p) sh_mv.mv_outw((x),(p))
  53. #define __outl(x,p) sh_mv.mv_outl((x),(p))
  54. #define __inb_p(p) sh_mv.mv_inb_p((p))
  55. #define __inw_p(p) sh_mv.mv_inw_p((p))
  56. #define __inl_p(p) sh_mv.mv_inl_p((p))
  57. #define __outb_p(x,p) sh_mv.mv_outb_p((x),(p))
  58. #define __outw_p(x,p) sh_mv.mv_outw_p((x),(p))
  59. #define __outl_p(x,p) sh_mv.mv_outl_p((x),(p))
  60. #define __insb(p,b,c) sh_mv.mv_insb((p), (b), (c))
  61. #define __insw(p,b,c) sh_mv.mv_insw((p), (b), (c))
  62. #define __insl(p,b,c) sh_mv.mv_insl((p), (b), (c))
  63. #define __outsb(p,b,c) sh_mv.mv_outsb((p), (b), (c))
  64. #define __outsw(p,b,c) sh_mv.mv_outsw((p), (b), (c))
  65. #define __outsl(p,b,c) sh_mv.mv_outsl((p), (b), (c))
  66. #define __readb(a) sh_mv.mv_readb((a))
  67. #define __readw(a) sh_mv.mv_readw((a))
  68. #define __readl(a) sh_mv.mv_readl((a))
  69. #define __writeb(v,a) sh_mv.mv_writeb((v),(a))
  70. #define __writew(v,a) sh_mv.mv_writew((v),(a))
  71. #define __writel(v,a) sh_mv.mv_writel((v),(a))
  72. #define inb __inb
  73. #define inw __inw
  74. #define inl __inl
  75. #define outb __outb
  76. #define outw __outw
  77. #define outl __outl
  78. #define inb_p __inb_p
  79. #define inw_p __inw_p
  80. #define inl_p __inl_p
  81. #define outb_p __outb_p
  82. #define outw_p __outw_p
  83. #define outl_p __outl_p
  84. #define insb __insb
  85. #define insw __insw
  86. #define insl __insl
  87. #define outsb __outsb
  88. #define outsw __outsw
  89. #define outsl __outsl
  90. #define __raw_readb(a) __readb((void __iomem *)(a))
  91. #define __raw_readw(a) __readw((void __iomem *)(a))
  92. #define __raw_readl(a) __readl((void __iomem *)(a))
  93. #define __raw_writeb(v, a) __writeb(v, (void __iomem *)(a))
  94. #define __raw_writew(v, a) __writew(v, (void __iomem *)(a))
  95. #define __raw_writel(v, a) __writel(v, (void __iomem *)(a))
  96. /*
  97. * The platform header files may define some of these macros to use
  98. * the inlined versions where appropriate. These macros may also be
  99. * redefined by userlevel programs.
  100. */
  101. #ifdef __readb
  102. # define readb(a) ({ unsigned long r_ = __raw_readb(a); mb(); r_; })
  103. #endif
  104. #ifdef __raw_readw
  105. # define readw(a) ({ unsigned long r_ = __raw_readw(a); mb(); r_; })
  106. #endif
  107. #ifdef __raw_readl
  108. # define readl(a) ({ unsigned long r_ = __raw_readl(a); mb(); r_; })
  109. #endif
  110. #ifdef __raw_writeb
  111. # define writeb(v,a) ({ __raw_writeb((v),(a)); mb(); })
  112. #endif
  113. #ifdef __raw_writew
  114. # define writew(v,a) ({ __raw_writew((v),(a)); mb(); })
  115. #endif
  116. #ifdef __raw_writel
  117. # define writel(v,a) ({ __raw_writel((v),(a)); mb(); })
  118. #endif
  119. #define readb_relaxed(a) readb(a)
  120. #define readw_relaxed(a) readw(a)
  121. #define readl_relaxed(a) readl(a)
  122. /* Simple MMIO */
  123. #define ioread8(a) readb(a)
  124. #define ioread16(a) readw(a)
  125. #define ioread16be(a) be16_to_cpu(__raw_readw((a)))
  126. #define ioread32(a) readl(a)
  127. #define ioread32be(a) be32_to_cpu(__raw_readl((a)))
  128. #define iowrite8(v,a) writeb((v),(a))
  129. #define iowrite16(v,a) writew((v),(a))
  130. #define iowrite16be(v,a) __raw_writew(cpu_to_be16((v)),(a))
  131. #define iowrite32(v,a) writel((v),(a))
  132. #define iowrite32be(v,a) __raw_writel(cpu_to_be32((v)),(a))
  133. #define ioread8_rep(a,d,c) insb((a),(d),(c))
  134. #define ioread16_rep(a,d,c) insw((a),(d),(c))
  135. #define ioread32_rep(a,d,c) insl((a),(d),(c))
  136. #define iowrite8_rep(a,s,c) outsb((a),(s),(c))
  137. #define iowrite16_rep(a,s,c) outsw((a),(s),(c))
  138. #define iowrite32_rep(a,s,c) outsl((a),(s),(c))
  139. #define mmiowb() wmb() /* synco on SH-4A, otherwise a nop */
  140. /*
  141. * This function provides a method for the generic case where a board-specific
  142. * ioport_map simply needs to return the port + some arbitrary port base.
  143. *
  144. * We use this at board setup time to implicitly set the port base, and
  145. * as a result, we can use the generic ioport_map.
  146. */
  147. static inline void __set_io_port_base(unsigned long pbase)
  148. {
  149. extern unsigned long generic_io_base;
  150. generic_io_base = pbase;
  151. }
  152. #define isa_readb(a) readb(ioport_map(a, 1))
  153. #define isa_readw(a) readw(ioport_map(a, 2))
  154. #define isa_readl(a) readl(ioport_map(a, 4))
  155. #define isa_writeb(b,a) writeb(b,ioport_map(a, 1))
  156. #define isa_writew(w,a) writew(w,ioport_map(a, 2))
  157. #define isa_writel(l,a) writel(l,ioport_map(a, 4))
  158. #define isa_memset_io(a,b,c) \
  159. memset((void *)(ioport_map((unsigned long)(a), 1)),(b),(c))
  160. #define isa_memcpy_fromio(a,b,c) \
  161. memcpy((a),(void *)(ioport_map((unsigned long)(b), 1)),(c))
  162. #define isa_memcpy_toio(a,b,c) \
  163. memcpy((void *)(ioport_map((unsigned long)(a), 1)),(b),(c))
  164. /* We really want to try and get these to memcpy etc */
  165. extern void memcpy_fromio(void *, volatile void __iomem *, unsigned long);
  166. extern void memcpy_toio(volatile void __iomem *, const void *, unsigned long);
  167. extern void memset_io(volatile void __iomem *, int, unsigned long);
  168. /* SuperH on-chip I/O functions */
  169. static inline unsigned char ctrl_inb(unsigned long addr)
  170. {
  171. return *(volatile unsigned char*)addr;
  172. }
  173. static inline unsigned short ctrl_inw(unsigned long addr)
  174. {
  175. return *(volatile unsigned short*)addr;
  176. }
  177. static inline unsigned int ctrl_inl(unsigned long addr)
  178. {
  179. return *(volatile unsigned long*)addr;
  180. }
  181. static inline void ctrl_outb(unsigned char b, unsigned long addr)
  182. {
  183. *(volatile unsigned char*)addr = b;
  184. }
  185. static inline void ctrl_outw(unsigned short b, unsigned long addr)
  186. {
  187. *(volatile unsigned short*)addr = b;
  188. }
  189. static inline void ctrl_outl(unsigned int b, unsigned long addr)
  190. {
  191. *(volatile unsigned long*)addr = b;
  192. }
  193. #define IO_SPACE_LIMIT 0xffffffff
  194. /*
  195. * Change virtual addresses to physical addresses and vv.
  196. * These are trivial on the 1:1 Linux/SuperH mapping
  197. */
  198. static inline unsigned long virt_to_phys(volatile void *address)
  199. {
  200. return PHYSADDR(address);
  201. }
  202. static inline void *phys_to_virt(unsigned long address)
  203. {
  204. return (void *)P1SEGADDR(address);
  205. }
  206. #define virt_to_bus virt_to_phys
  207. #define bus_to_virt phys_to_virt
  208. #define page_to_bus page_to_phys
  209. /*
  210. * readX/writeX() are used to access memory mapped devices. On some
  211. * architectures the memory mapped IO stuff needs to be accessed
  212. * differently. On the x86 architecture, we just read/write the
  213. * memory location directly.
  214. *
  215. * On SH, we traditionally have the whole physical address space mapped
  216. * at all times (as MIPS does), so "ioremap()" and "iounmap()" do not
  217. * need to do anything but place the address in the proper segment. This
  218. * is true for P1 and P2 addresses, as well as some P3 ones. However,
  219. * most of the P3 addresses and newer cores using extended addressing
  220. * need to map through page tables, so the ioremap() implementation
  221. * becomes a bit more complicated. See arch/sh/mm/ioremap.c for
  222. * additional notes on this.
  223. *
  224. * We cheat a bit and always return uncachable areas until we've fixed
  225. * the drivers to handle caching properly.
  226. */
  227. #ifdef CONFIG_MMU
  228. void __iomem *__ioremap(unsigned long offset, unsigned long size,
  229. unsigned long flags);
  230. void __iounmap(void __iomem *addr);
  231. #else
  232. #define __ioremap(offset, size, flags) ((void __iomem *)(offset))
  233. #define __iounmap(addr) do { } while (0)
  234. #endif /* CONFIG_MMU */
  235. static inline void __iomem *
  236. __ioremap_mode(unsigned long offset, unsigned long size, unsigned long flags)
  237. {
  238. unsigned long last_addr = offset + size - 1;
  239. /*
  240. * For P1 and P2 space this is trivial, as everything is already
  241. * mapped. Uncached access for P1 addresses are done through P2.
  242. * In the P3 case or for addresses outside of the 29-bit space,
  243. * mapping must be done by the PMB or by using page tables.
  244. */
  245. if (likely(PXSEG(offset) < P3SEG && PXSEG(last_addr) < P3SEG)) {
  246. if (unlikely(flags & _PAGE_CACHABLE))
  247. return (void __iomem *)P1SEGADDR(offset);
  248. return (void __iomem *)P2SEGADDR(offset);
  249. }
  250. return __ioremap(offset, size, flags);
  251. }
  252. #define ioremap(offset, size) \
  253. __ioremap_mode((offset), (size), 0)
  254. #define ioremap_nocache(offset, size) \
  255. __ioremap_mode((offset), (size), 0)
  256. #define ioremap_cache(offset, size) \
  257. __ioremap_mode((offset), (size), _PAGE_CACHABLE)
  258. #define p3_ioremap(offset, size, flags) \
  259. __ioremap((offset), (size), (flags))
  260. #define iounmap(addr) \
  261. __iounmap((addr))
  262. static inline int check_signature(char __iomem *io_addr,
  263. const unsigned char *signature, int length)
  264. {
  265. int retval = 0;
  266. do {
  267. if (readb(io_addr) != *signature)
  268. goto out;
  269. io_addr++;
  270. signature++;
  271. length--;
  272. } while (length);
  273. retval = 1;
  274. out:
  275. return retval;
  276. }
  277. /*
  278. * The caches on some architectures aren't dma-coherent and have need to
  279. * handle this in software. There are three types of operations that
  280. * can be applied to dma buffers.
  281. *
  282. * - dma_cache_wback_inv(start, size) makes caches and RAM coherent by
  283. * writing the content of the caches back to memory, if necessary.
  284. * The function also invalidates the affected part of the caches as
  285. * necessary before DMA transfers from outside to memory.
  286. * - dma_cache_inv(start, size) invalidates the affected parts of the
  287. * caches. Dirty lines of the caches may be written back or simply
  288. * be discarded. This operation is necessary before dma operations
  289. * to the memory.
  290. * - dma_cache_wback(start, size) writes back any dirty lines but does
  291. * not invalidate the cache. This can be used before DMA reads from
  292. * memory,
  293. */
  294. #define dma_cache_wback_inv(_start,_size) \
  295. __flush_purge_region(_start,_size)
  296. #define dma_cache_inv(_start,_size) \
  297. __flush_invalidate_region(_start,_size)
  298. #define dma_cache_wback(_start,_size) \
  299. __flush_wback_region(_start,_size)
  300. /*
  301. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  302. * access
  303. */
  304. #define xlate_dev_mem_ptr(p) __va(p)
  305. /*
  306. * Convert a virtual cached pointer to an uncached pointer
  307. */
  308. #define xlate_dev_kmem_ptr(p) p
  309. #endif /* __KERNEL__ */
  310. #endif /* __ASM_SH_IO_H */