io.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #ifndef _H8300_IO_H
  2. #define _H8300_IO_H
  3. #ifdef __KERNEL__
  4. #include <linux/config.h>
  5. #include <asm/virtconvert.h>
  6. #if defined(CONFIG_H83007) || defined(CONFIG_H83068)
  7. #include <asm/regs306x.h>
  8. #elif defined(CONFIG_H8S2678)
  9. #include <asm/regs267x.h>
  10. #else
  11. #error UNKNOWN CPU TYPE
  12. #endif
  13. /*
  14. * These are for ISA/PCI shared memory _only_ and should never be used
  15. * on any other type of memory, including Zorro memory. They are meant to
  16. * access the bus in the bus byte order which is little-endian!.
  17. *
  18. * readX/writeX() are used to access memory mapped devices. On some
  19. * architectures the memory mapped IO stuff needs to be accessed
  20. * differently. On the m68k architecture, we just read/write the
  21. * memory location directly.
  22. */
  23. /* ++roman: The assignments to temp. vars avoid that gcc sometimes generates
  24. * two accesses to memory, which may be undesireable for some devices.
  25. */
  26. /*
  27. * swap functions are sometimes needed to interface little-endian hardware
  28. */
  29. static inline unsigned short _swapw(volatile unsigned short v)
  30. {
  31. #ifndef H8300_IO_NOSWAP
  32. unsigned short r;
  33. __asm__("xor.b %w0,%x0\n\t"
  34. "xor.b %x0,%w0\n\t"
  35. "xor.b %w0,%x0"
  36. :"=r"(r)
  37. :"0"(v));
  38. return r;
  39. #else
  40. return v;
  41. #endif
  42. }
  43. static inline unsigned long _swapl(volatile unsigned long v)
  44. {
  45. #ifndef H8300_IO_NOSWAP
  46. unsigned long r;
  47. __asm__("xor.b %w0,%x0\n\t"
  48. "xor.b %x0,%w0\n\t"
  49. "xor.b %w0,%x0\n\t"
  50. "xor.w %e0,%f0\n\t"
  51. "xor.w %f0,%e0\n\t"
  52. "xor.w %e0,%f0\n\t"
  53. "xor.b %w0,%x0\n\t"
  54. "xor.b %x0,%w0\n\t"
  55. "xor.b %w0,%x0"
  56. :"=r"(r)
  57. :"0"(v));
  58. return r;
  59. #else
  60. return v;
  61. #endif
  62. }
  63. #define readb(addr) \
  64. ({ unsigned char __v = \
  65. *(volatile unsigned char *)((unsigned long)(addr) & 0x00ffffff); \
  66. __v; })
  67. #define readw(addr) \
  68. ({ unsigned short __v = \
  69. *(volatile unsigned short *)((unsigned long)(addr) & 0x00ffffff); \
  70. __v; })
  71. #define readl(addr) \
  72. ({ unsigned long __v = \
  73. *(volatile unsigned long *)((unsigned long)(addr) & 0x00ffffff); \
  74. __v; })
  75. #define writeb(b,addr) (void)((*(volatile unsigned char *) \
  76. ((unsigned long)(addr) & 0x00ffffff)) = (b))
  77. #define writew(b,addr) (void)((*(volatile unsigned short *) \
  78. ((unsigned long)(addr) & 0x00ffffff)) = (b))
  79. #define writel(b,addr) (void)((*(volatile unsigned long *) \
  80. ((unsigned long)(addr) & 0x00ffffff)) = (b))
  81. #define readb_relaxed(addr) readb(addr)
  82. #define readw_relaxed(addr) readw(addr)
  83. #define readl_relaxed(addr) readl(addr)
  84. #define __raw_readb readb
  85. #define __raw_readw readw
  86. #define __raw_readl readl
  87. #define __raw_writeb writeb
  88. #define __raw_writew writew
  89. #define __raw_writel writel
  90. static inline int h8300_buswidth(unsigned int addr)
  91. {
  92. return (*(volatile unsigned char *)ABWCR & (1 << ((addr >> 21) & 7))) == 0;
  93. }
  94. static inline void io_outsb(unsigned int addr, const void *buf, int len)
  95. {
  96. volatile unsigned char *ap_b = (volatile unsigned char *) addr;
  97. volatile unsigned short *ap_w = (volatile unsigned short *) addr;
  98. unsigned char *bp = (unsigned char *) buf;
  99. if(h8300_buswidth(addr) && (addr & 1)) {
  100. while (len--)
  101. *ap_w = *bp++;
  102. } else {
  103. while (len--)
  104. *ap_b = *bp++;
  105. }
  106. }
  107. static inline void io_outsw(unsigned int addr, const void *buf, int len)
  108. {
  109. volatile unsigned short *ap = (volatile unsigned short *) addr;
  110. unsigned short *bp = (unsigned short *) buf;
  111. while (len--)
  112. *ap = _swapw(*bp++);
  113. }
  114. static inline void io_outsl(unsigned int addr, const void *buf, int len)
  115. {
  116. volatile unsigned long *ap = (volatile unsigned long *) addr;
  117. unsigned long *bp = (unsigned long *) buf;
  118. while (len--)
  119. *ap = _swapl(*bp++);
  120. }
  121. static inline void io_outsw_noswap(unsigned int addr, const void *buf, int len)
  122. {
  123. volatile unsigned short *ap = (volatile unsigned short *) addr;
  124. unsigned short *bp = (unsigned short *) buf;
  125. while (len--)
  126. *ap = *bp++;
  127. }
  128. static inline void io_outsl_noswap(unsigned int addr, const void *buf, int len)
  129. {
  130. volatile unsigned long *ap = (volatile unsigned long *) addr;
  131. unsigned long *bp = (unsigned long *) buf;
  132. while (len--)
  133. *ap = *bp++;
  134. }
  135. static inline void io_insb(unsigned int addr, void *buf, int len)
  136. {
  137. volatile unsigned char *ap_b;
  138. volatile unsigned short *ap_w;
  139. unsigned char *bp = (unsigned char *) buf;
  140. if(h8300_buswidth(addr)) {
  141. ap_w = (volatile unsigned short *)(addr & ~1);
  142. while (len--)
  143. *bp++ = *ap_w & 0xff;
  144. } else {
  145. ap_b = (volatile unsigned char *)addr;
  146. while (len--)
  147. *bp++ = *ap_b;
  148. }
  149. }
  150. static inline void io_insw(unsigned int addr, void *buf, int len)
  151. {
  152. volatile unsigned short *ap = (volatile unsigned short *) addr;
  153. unsigned short *bp = (unsigned short *) buf;
  154. while (len--)
  155. *bp++ = _swapw(*ap);
  156. }
  157. static inline void io_insl(unsigned int addr, void *buf, int len)
  158. {
  159. volatile unsigned long *ap = (volatile unsigned long *) addr;
  160. unsigned long *bp = (unsigned long *) buf;
  161. while (len--)
  162. *bp++ = _swapl(*ap);
  163. }
  164. static inline void io_insw_noswap(unsigned int addr, void *buf, int len)
  165. {
  166. volatile unsigned short *ap = (volatile unsigned short *) addr;
  167. unsigned short *bp = (unsigned short *) buf;
  168. while (len--)
  169. *bp++ = *ap;
  170. }
  171. static inline void io_insl_noswap(unsigned int addr, void *buf, int len)
  172. {
  173. volatile unsigned long *ap = (volatile unsigned long *) addr;
  174. unsigned long *bp = (unsigned long *) buf;
  175. while (len--)
  176. *bp++ = *ap;
  177. }
  178. /*
  179. * make the short names macros so specific devices
  180. * can override them as required
  181. */
  182. #define memset_io(a,b,c) memset((void *)(a),(b),(c))
  183. #define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
  184. #define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
  185. #define mmiowb()
  186. #define inb(addr) ((h8300_buswidth(addr))?readw((addr) & ~1) & 0xff:readb(addr))
  187. #define inw(addr) _swapw(readw(addr))
  188. #define inl(addr) _swapl(readl(addr))
  189. #define outb(x,addr) ((void)((h8300_buswidth(addr) && \
  190. ((addr) & 1))?writew(x,(addr) & ~1):writeb(x,addr)))
  191. #define outw(x,addr) ((void) writew(_swapw(x),addr))
  192. #define outl(x,addr) ((void) writel(_swapl(x),addr))
  193. #define inb_p(addr) inb(addr)
  194. #define inw_p(addr) inw(addr)
  195. #define inl_p(addr) inl(addr)
  196. #define outb_p(x,addr) outb(x,addr)
  197. #define outw_p(x,addr) outw(x,addr)
  198. #define outl_p(x,addr) outl(x,addr)
  199. #define outsb(a,b,l) io_outsb(a,b,l)
  200. #define outsw(a,b,l) io_outsw(a,b,l)
  201. #define outsl(a,b,l) io_outsl(a,b,l)
  202. #define insb(a,b,l) io_insb(a,b,l)
  203. #define insw(a,b,l) io_insw(a,b,l)
  204. #define insl(a,b,l) io_insl(a,b,l)
  205. #define IO_SPACE_LIMIT 0xffffff
  206. /* Values for nocacheflag and cmode */
  207. #define IOMAP_FULL_CACHING 0
  208. #define IOMAP_NOCACHE_SER 1
  209. #define IOMAP_NOCACHE_NONSER 2
  210. #define IOMAP_WRITETHROUGH 3
  211. extern void *__ioremap(unsigned long physaddr, unsigned long size, int cacheflag);
  212. extern void __iounmap(void *addr, unsigned long size);
  213. static inline void *ioremap(unsigned long physaddr, unsigned long size)
  214. {
  215. return __ioremap(physaddr, size, IOMAP_NOCACHE_SER);
  216. }
  217. static inline void *ioremap_nocache(unsigned long physaddr, unsigned long size)
  218. {
  219. return __ioremap(physaddr, size, IOMAP_NOCACHE_SER);
  220. }
  221. static inline void *ioremap_writethrough(unsigned long physaddr, unsigned long size)
  222. {
  223. return __ioremap(physaddr, size, IOMAP_WRITETHROUGH);
  224. }
  225. static inline void *ioremap_fullcache(unsigned long physaddr, unsigned long size)
  226. {
  227. return __ioremap(physaddr, size, IOMAP_FULL_CACHING);
  228. }
  229. extern void iounmap(void *addr);
  230. /* Nothing to do */
  231. #define dma_cache_inv(_start,_size) do { } while (0)
  232. #define dma_cache_wback(_start,_size) do { } while (0)
  233. #define dma_cache_wback_inv(_start,_size) do { } while (0)
  234. /* H8/300 internal I/O functions */
  235. static __inline__ unsigned char ctrl_inb(unsigned long addr)
  236. {
  237. return *(volatile unsigned char*)addr;
  238. }
  239. static __inline__ unsigned short ctrl_inw(unsigned long addr)
  240. {
  241. return *(volatile unsigned short*)addr;
  242. }
  243. static __inline__ unsigned long ctrl_inl(unsigned long addr)
  244. {
  245. return *(volatile unsigned long*)addr;
  246. }
  247. static __inline__ void ctrl_outb(unsigned char b, unsigned long addr)
  248. {
  249. *(volatile unsigned char*)addr = b;
  250. }
  251. static __inline__ void ctrl_outw(unsigned short b, unsigned long addr)
  252. {
  253. *(volatile unsigned short*)addr = b;
  254. }
  255. static __inline__ void ctrl_outl(unsigned long b, unsigned long addr)
  256. {
  257. *(volatile unsigned long*)addr = b;
  258. }
  259. /* Pages to physical address... */
  260. #define page_to_phys(page) ((page - mem_map) << PAGE_SHIFT)
  261. #define page_to_bus(page) ((page - mem_map) << PAGE_SHIFT)
  262. /*
  263. * Macros used for converting between virtual and physical mappings.
  264. */
  265. #define mm_ptov(vaddr) ((void *) (vaddr))
  266. #define mm_vtop(vaddr) ((unsigned long) (vaddr))
  267. #define phys_to_virt(vaddr) ((void *) (vaddr))
  268. #define virt_to_phys(vaddr) ((unsigned long) (vaddr))
  269. #define virt_to_bus virt_to_phys
  270. #define bus_to_virt phys_to_virt
  271. /*
  272. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  273. * access
  274. */
  275. #define xlate_dev_mem_ptr(p) __va(p)
  276. /*
  277. * Convert a virtual cached pointer to an uncached pointer
  278. */
  279. #define xlate_dev_kmem_ptr(p) p
  280. #endif /* __KERNEL__ */
  281. #endif /* _H8300_IO_H */