io.h 9.7 KB

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