io.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. #ifndef __ASM_SH_IO_H
  2. #define __ASM_SH_IO_H
  3. /*
  4. * Convention:
  5. * read{b,w,l,q}/write{b,w,l,q} are for PCI,
  6. * while in{b,w,l}/out{b,w,l} are for ISA
  7. *
  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. *
  11. * While read{b,w,l,q} and write{b,w,l,q} contain memory barriers
  12. * automatically, there are also __raw versions, which do not.
  13. *
  14. * Historically, we have also had ctrl_in{b,w,l,q}/ctrl_out{b,w,l,q} for
  15. * SuperH specific I/O (raw I/O to on-chip CPU peripherals). In practice
  16. * these have the same semantics as the __raw variants, and as such, all
  17. * new code should be using the __raw versions.
  18. *
  19. * All ISA I/O routines are wrapped through the machine vector. If a
  20. * board does not provide overrides, a generic set that are copied in
  21. * from the default machine vector are used instead. These are largely
  22. * for old compat code for I/O offseting to SuperIOs, all of which are
  23. * better handled through the machvec ioport mapping routines these days.
  24. */
  25. #include <linux/errno.h>
  26. #include <asm/cache.h>
  27. #include <asm/system.h>
  28. #include <asm/addrspace.h>
  29. #include <asm/machvec.h>
  30. #include <asm/pgtable.h>
  31. #include <asm-generic/iomap.h>
  32. #ifdef __KERNEL__
  33. /*
  34. * Depending on which platform we are running on, we need different
  35. * I/O functions.
  36. */
  37. #define __IO_PREFIX generic
  38. #include <asm/io_generic.h>
  39. #include <asm/io_trapped.h>
  40. #define inb(p) sh_mv.mv_inb((p))
  41. #define inw(p) sh_mv.mv_inw((p))
  42. #define inl(p) sh_mv.mv_inl((p))
  43. #define outb(x,p) sh_mv.mv_outb((x),(p))
  44. #define outw(x,p) sh_mv.mv_outw((x),(p))
  45. #define outl(x,p) sh_mv.mv_outl((x),(p))
  46. #define inb_p(p) sh_mv.mv_inb_p((p))
  47. #define inw_p(p) sh_mv.mv_inw_p((p))
  48. #define inl_p(p) sh_mv.mv_inl_p((p))
  49. #define outb_p(x,p) sh_mv.mv_outb_p((x),(p))
  50. #define outw_p(x,p) sh_mv.mv_outw_p((x),(p))
  51. #define outl_p(x,p) sh_mv.mv_outl_p((x),(p))
  52. #define insb(p,b,c) sh_mv.mv_insb((p), (b), (c))
  53. #define insw(p,b,c) sh_mv.mv_insw((p), (b), (c))
  54. #define insl(p,b,c) sh_mv.mv_insl((p), (b), (c))
  55. #define outsb(p,b,c) sh_mv.mv_outsb((p), (b), (c))
  56. #define outsw(p,b,c) sh_mv.mv_outsw((p), (b), (c))
  57. #define outsl(p,b,c) sh_mv.mv_outsl((p), (b), (c))
  58. #define __raw_writeb(v,a) (__chk_io_ptr(a), *(volatile u8 __force *)(a) = (v))
  59. #define __raw_writew(v,a) (__chk_io_ptr(a), *(volatile u16 __force *)(a) = (v))
  60. #define __raw_writel(v,a) (__chk_io_ptr(a), *(volatile u32 __force *)(a) = (v))
  61. #define __raw_writeq(v,a) (__chk_io_ptr(a), *(volatile u64 __force *)(a) = (v))
  62. #define __raw_readb(a) (__chk_io_ptr(a), *(volatile u8 __force *)(a))
  63. #define __raw_readw(a) (__chk_io_ptr(a), *(volatile u16 __force *)(a))
  64. #define __raw_readl(a) (__chk_io_ptr(a), *(volatile u32 __force *)(a))
  65. #define __raw_readq(a) (__chk_io_ptr(a), *(volatile u64 __force *)(a))
  66. #define readb(a) ({ u8 r_ = __raw_readb(a); mb(); r_; })
  67. #define readw(a) ({ u16 r_ = __raw_readw(a); mb(); r_; })
  68. #define readl(a) ({ u32 r_ = __raw_readl(a); mb(); r_; })
  69. #define readq(a) ({ u64 r_ = __raw_readq(a); mb(); r_; })
  70. #define writeb(v,a) ({ __raw_writeb((v),(a)); mb(); })
  71. #define writew(v,a) ({ __raw_writew((v),(a)); mb(); })
  72. #define writel(v,a) ({ __raw_writel((v),(a)); mb(); })
  73. #define writeq(v,a) ({ __raw_writeq((v),(a)); mb(); })
  74. /*
  75. * Legacy SuperH on-chip I/O functions
  76. *
  77. * These are all deprecated, all new (and especially cross-platform) code
  78. * should be using the __raw_xxx() routines directly.
  79. */
  80. static inline u8 __deprecated ctrl_inb(unsigned long addr)
  81. {
  82. return __raw_readb(addr);
  83. }
  84. static inline u16 __deprecated ctrl_inw(unsigned long addr)
  85. {
  86. return __raw_readw(addr);
  87. }
  88. static inline u32 __deprecated ctrl_inl(unsigned long addr)
  89. {
  90. return __raw_readl(addr);
  91. }
  92. static inline u64 __deprecated ctrl_inq(unsigned long addr)
  93. {
  94. return __raw_readq(addr);
  95. }
  96. static inline void __deprecated ctrl_outb(u8 v, unsigned long addr)
  97. {
  98. __raw_writeb(v, addr);
  99. }
  100. static inline void __deprecated ctrl_outw(u16 v, unsigned long addr)
  101. {
  102. __raw_writew(v, addr);
  103. }
  104. static inline void __deprecated ctrl_outl(u32 v, unsigned long addr)
  105. {
  106. __raw_writel(v, addr);
  107. }
  108. static inline void __deprecated ctrl_outq(u64 v, unsigned long addr)
  109. {
  110. __raw_writeq(v, addr);
  111. }
  112. extern unsigned long generic_io_base;
  113. static inline void ctrl_delay(void)
  114. {
  115. __raw_readw(generic_io_base);
  116. }
  117. #define __BUILD_UNCACHED_IO(bwlq, type) \
  118. static inline type read##bwlq##_uncached(unsigned long addr) \
  119. { \
  120. type ret; \
  121. jump_to_uncached(); \
  122. ret = __raw_read##bwlq(addr); \
  123. back_to_cached(); \
  124. return ret; \
  125. } \
  126. \
  127. static inline void write##bwlq##_uncached(type v, unsigned long addr) \
  128. { \
  129. jump_to_uncached(); \
  130. __raw_write##bwlq(v, addr); \
  131. back_to_cached(); \
  132. }
  133. __BUILD_UNCACHED_IO(b, u8)
  134. __BUILD_UNCACHED_IO(w, u16)
  135. __BUILD_UNCACHED_IO(l, u32)
  136. __BUILD_UNCACHED_IO(q, u64)
  137. #define __BUILD_MEMORY_STRING(bwlq, type) \
  138. \
  139. static inline void __raw_writes##bwlq(volatile void __iomem *mem, \
  140. const void *addr, unsigned int count) \
  141. { \
  142. const volatile type *__addr = addr; \
  143. \
  144. while (count--) { \
  145. __raw_write##bwlq(*__addr, mem); \
  146. __addr++; \
  147. } \
  148. } \
  149. \
  150. static inline void __raw_reads##bwlq(volatile void __iomem *mem, \
  151. void *addr, unsigned int count) \
  152. { \
  153. volatile type *__addr = addr; \
  154. \
  155. while (count--) { \
  156. *__addr = __raw_read##bwlq(mem); \
  157. __addr++; \
  158. } \
  159. }
  160. __BUILD_MEMORY_STRING(b, u8)
  161. __BUILD_MEMORY_STRING(w, u16)
  162. #ifdef CONFIG_SUPERH32
  163. void __raw_writesl(void __iomem *addr, const void *data, int longlen);
  164. void __raw_readsl(const void __iomem *addr, void *data, int longlen);
  165. #else
  166. __BUILD_MEMORY_STRING(l, u32)
  167. #endif
  168. __BUILD_MEMORY_STRING(q, u64)
  169. #define writesb __raw_writesb
  170. #define writesw __raw_writesw
  171. #define writesl __raw_writesl
  172. #define readsb __raw_readsb
  173. #define readsw __raw_readsw
  174. #define readsl __raw_readsl
  175. #define readb_relaxed(a) readb(a)
  176. #define readw_relaxed(a) readw(a)
  177. #define readl_relaxed(a) readl(a)
  178. #define readq_relaxed(a) readq(a)
  179. #ifndef CONFIG_GENERIC_IOMAP
  180. /* Simple MMIO */
  181. #define ioread8(a) __raw_readb(a)
  182. #define ioread16(a) __raw_readw(a)
  183. #define ioread16be(a) be16_to_cpu(__raw_readw((a)))
  184. #define ioread32(a) __raw_readl(a)
  185. #define ioread32be(a) be32_to_cpu(__raw_readl((a)))
  186. #define iowrite8(v,a) __raw_writeb((v),(a))
  187. #define iowrite16(v,a) __raw_writew((v),(a))
  188. #define iowrite16be(v,a) __raw_writew(cpu_to_be16((v)),(a))
  189. #define iowrite32(v,a) __raw_writel((v),(a))
  190. #define iowrite32be(v,a) __raw_writel(cpu_to_be32((v)),(a))
  191. #define ioread8_rep(a, d, c) __raw_readsb((a), (d), (c))
  192. #define ioread16_rep(a, d, c) __raw_readsw((a), (d), (c))
  193. #define ioread32_rep(a, d, c) __raw_readsl((a), (d), (c))
  194. #define iowrite8_rep(a, s, c) __raw_writesb((a), (s), (c))
  195. #define iowrite16_rep(a, s, c) __raw_writesw((a), (s), (c))
  196. #define iowrite32_rep(a, s, c) __raw_writesl((a), (s), (c))
  197. #endif
  198. #define mmio_insb(p,d,c) __raw_readsb(p,d,c)
  199. #define mmio_insw(p,d,c) __raw_readsw(p,d,c)
  200. #define mmio_insl(p,d,c) __raw_readsl(p,d,c)
  201. #define mmio_outsb(p,s,c) __raw_writesb(p,s,c)
  202. #define mmio_outsw(p,s,c) __raw_writesw(p,s,c)
  203. #define mmio_outsl(p,s,c) __raw_writesl(p,s,c)
  204. /* synco on SH-4A, otherwise a nop */
  205. #define mmiowb() wmb()
  206. #define IO_SPACE_LIMIT 0xffffffff
  207. /*
  208. * This function provides a method for the generic case where a
  209. * board-specific ioport_map simply needs to return the port + some
  210. * arbitrary port base.
  211. *
  212. * We use this at board setup time to implicitly set the port base, and
  213. * as a result, we can use the generic ioport_map.
  214. */
  215. static inline void __set_io_port_base(unsigned long pbase)
  216. {
  217. generic_io_base = pbase;
  218. }
  219. #define __ioport_map(p, n) sh_mv.mv_ioport_map((p), (n))
  220. /* We really want to try and get these to memcpy etc */
  221. void memcpy_fromio(void *, const volatile void __iomem *, unsigned long);
  222. void memcpy_toio(volatile void __iomem *, const void *, unsigned long);
  223. void memset_io(volatile void __iomem *, int, unsigned long);
  224. /* Quad-word real-mode I/O, don't ask.. */
  225. unsigned long long peek_real_address_q(unsigned long long addr);
  226. unsigned long long poke_real_address_q(unsigned long long addr,
  227. unsigned long long val);
  228. #if !defined(CONFIG_MMU)
  229. #define virt_to_phys(address) ((unsigned long)(address))
  230. #define phys_to_virt(address) ((void *)(address))
  231. #else
  232. #define virt_to_phys(address) (__pa(address))
  233. #define phys_to_virt(address) (__va(address))
  234. #endif
  235. /*
  236. * On 32-bit SH, we traditionally have the whole physical address space
  237. * mapped at all times (as MIPS does), so "ioremap()" and "iounmap()" do
  238. * not need to do anything but place the address in the proper segment.
  239. * This is true for P1 and P2 addresses, as well as some P3 ones.
  240. * However, most of the P3 addresses and newer cores using extended
  241. * addressing need to map through page tables, so the ioremap()
  242. * implementation becomes a bit more complicated.
  243. *
  244. * See arch/sh/mm/ioremap.c for additional notes on this.
  245. *
  246. * We cheat a bit and always return uncachable areas until we've fixed
  247. * the drivers to handle caching properly.
  248. *
  249. * On the SH-5 the concept of segmentation in the 1:1 PXSEG sense simply
  250. * doesn't exist, so everything must go through page tables.
  251. */
  252. #ifdef CONFIG_MMU
  253. void __iomem *__ioremap_caller(phys_addr_t offset, unsigned long size,
  254. pgprot_t prot, void *caller);
  255. void __iounmap(void __iomem *addr);
  256. static inline void __iomem *
  257. __ioremap(phys_addr_t offset, unsigned long size, pgprot_t prot)
  258. {
  259. return __ioremap_caller(offset, size, prot, __builtin_return_address(0));
  260. }
  261. static inline void __iomem *
  262. __ioremap_29bit(phys_addr_t offset, unsigned long size, pgprot_t prot)
  263. {
  264. #ifdef CONFIG_29BIT
  265. phys_addr_t last_addr = offset + size - 1;
  266. /*
  267. * For P1 and P2 space this is trivial, as everything is already
  268. * mapped. Uncached access for P1 addresses are done through P2.
  269. * In the P3 case or for addresses outside of the 29-bit space,
  270. * mapping must be done by the PMB or by using page tables.
  271. */
  272. if (likely(PXSEG(offset) < P3SEG && PXSEG(last_addr) < P3SEG)) {
  273. if (unlikely(pgprot_val(prot) & _PAGE_CACHABLE))
  274. return (void __iomem *)P1SEGADDR(offset);
  275. return (void __iomem *)P2SEGADDR(offset);
  276. }
  277. /* P4 above the store queues are always mapped. */
  278. if (unlikely(offset >= P3_ADDR_MAX))
  279. return (void __iomem *)P4SEGADDR(offset);
  280. #endif
  281. return NULL;
  282. }
  283. static inline void __iomem *
  284. __ioremap_mode(phys_addr_t offset, unsigned long size, pgprot_t prot)
  285. {
  286. void __iomem *ret;
  287. ret = __ioremap_trapped(offset, size);
  288. if (ret)
  289. return ret;
  290. ret = __ioremap_29bit(offset, size, prot);
  291. if (ret)
  292. return ret;
  293. return __ioremap(offset, size, prot);
  294. }
  295. #else
  296. #define __ioremap(offset, size, prot) ((void __iomem *)(offset))
  297. #define __ioremap_mode(offset, size, prot) ((void __iomem *)(offset))
  298. #define __iounmap(addr) do { } while (0)
  299. #endif /* CONFIG_MMU */
  300. static inline void __iomem *ioremap(phys_addr_t offset, unsigned long size)
  301. {
  302. return __ioremap_mode(offset, size, PAGE_KERNEL_NOCACHE);
  303. }
  304. static inline void __iomem *
  305. ioremap_cache(phys_addr_t offset, unsigned long size)
  306. {
  307. return __ioremap_mode(offset, size, PAGE_KERNEL);
  308. }
  309. #ifdef CONFIG_HAVE_IOREMAP_PROT
  310. static inline void __iomem *
  311. ioremap_prot(phys_addr_t offset, unsigned long size, unsigned long flags)
  312. {
  313. return __ioremap_mode(offset, size, __pgprot(flags));
  314. }
  315. #endif
  316. #ifdef CONFIG_IOREMAP_FIXED
  317. extern void __iomem *ioremap_fixed(phys_addr_t, unsigned long, pgprot_t);
  318. extern int iounmap_fixed(void __iomem *);
  319. extern void ioremap_fixed_init(void);
  320. #else
  321. static inline void __iomem *
  322. ioremap_fixed(phys_addr_t phys_addr, unsigned long size, pgprot_t prot)
  323. {
  324. BUG();
  325. return NULL;
  326. }
  327. static inline void ioremap_fixed_init(void) { }
  328. static inline int iounmap_fixed(void __iomem *addr) { return -EINVAL; }
  329. #endif
  330. #define ioremap_nocache ioremap
  331. #define iounmap __iounmap
  332. #define maybebadio(port) \
  333. printk(KERN_ERR "bad PC-like io %s:%u for port 0x%lx at 0x%08x\n", \
  334. __func__, __LINE__, (port), (u32)__builtin_return_address(0))
  335. /*
  336. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  337. * access
  338. */
  339. #define xlate_dev_mem_ptr(p) __va(p)
  340. /*
  341. * Convert a virtual cached pointer to an uncached pointer
  342. */
  343. #define xlate_dev_kmem_ptr(p) p
  344. #define ARCH_HAS_VALID_PHYS_ADDR_RANGE
  345. int valid_phys_addr_range(unsigned long addr, size_t size);
  346. int valid_mmap_phys_addr_range(unsigned long pfn, size_t size);
  347. #endif /* __KERNEL__ */
  348. #endif /* __ASM_SH_IO_H */