io.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * linux/include/asm-nds/io.h
  3. *
  4. * Copyright (C) 1996-2000 Russell King
  5. *
  6. * Copyright (C) 2011 Andes Technology Corporation
  7. * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
  8. * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * Modifications:
  15. * 16-Sep-1996 RMK Inlined the inx/outx functions & optimised for both
  16. * constant addresses and variable addresses.
  17. * 04-Dec-1997 RMK Moved a lot of this stuff to the new architecture
  18. * specific IO header files.
  19. * 27-Mar-1999 PJB Second parameter of memcpy_toio is const..
  20. * 04-Apr-1999 PJB Added check_signature.
  21. * 12-Dec-1999 RMK More cleanups
  22. * 18-Jun-2000 RMK Removed virt_to_* and friends definitions
  23. */
  24. #ifndef __ASM_NDS_IO_H
  25. #define __ASM_NDS_IO_H
  26. /*
  27. * CAUTION:
  28. * - do not implement for NDS32 Arch yet.
  29. * - cmd_pci.c, cmd_scsi.c, Lynxkdi.c, usb.c, usb_storage.c, etc...
  30. * iinclude asm/io.h
  31. */
  32. #ifdef __KERNEL__
  33. #include <linux/types.h>
  34. #include <asm/byteorder.h>
  35. static inline void sync(void)
  36. {
  37. }
  38. /*
  39. * Given a physical address and a length, return a virtual address
  40. * that can be used to access the memory range with the caching
  41. * properties specified by "flags".
  42. */
  43. #define MAP_NOCACHE (0)
  44. #define MAP_WRCOMBINE (0)
  45. #define MAP_WRBACK (0)
  46. #define MAP_WRTHROUGH (0)
  47. static inline void *
  48. map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
  49. {
  50. return (void *)paddr;
  51. }
  52. /*
  53. * Take down a mapping set up by map_physmem().
  54. */
  55. static inline void unmap_physmem(void *vaddr, unsigned long flags)
  56. {
  57. }
  58. static inline phys_addr_t virt_to_phys(void *vaddr)
  59. {
  60. return (phys_addr_t)(vaddr);
  61. }
  62. /*
  63. * Generic virtual read/write. Note that we don't support half-word
  64. * read/writes. We define __arch_*[bl] here, and leave __arch_*w
  65. * to the architecture specific code.
  66. */
  67. #define __arch_getb(a) (*(unsigned char *)(a))
  68. #define __arch_getw(a) (*(unsigned short *)(a))
  69. #define __arch_getl(a) (*(unsigned int *)(a))
  70. #define __arch_putb(v, a) (*(unsigned char *)(a) = (v))
  71. #define __arch_putw(v, a) (*(unsigned short *)(a) = (v))
  72. #define __arch_putl(v, a) (*(unsigned int *)(a) = (v))
  73. extern void __raw_writesb(unsigned int addr, const void *data, int bytelen);
  74. extern void __raw_writesw(unsigned int addr, const void *data, int wordlen);
  75. extern void __raw_writesl(unsigned int addr, const void *data, int longlen);
  76. extern void __raw_readsb(unsigned int addr, void *data, int bytelen);
  77. extern void __raw_readsw(unsigned int addr, void *data, int wordlen);
  78. extern void __raw_readsl(unsigned int addr, void *data, int longlen);
  79. #define __raw_writeb(v, a) __arch_putb(v, a)
  80. #define __raw_writew(v, a) __arch_putw(v, a)
  81. #define __raw_writel(v, a) __arch_putl(v, a)
  82. #define __raw_readb(a) __arch_getb(a)
  83. #define __raw_readw(a) __arch_getw(a)
  84. #define __raw_readl(a) __arch_getl(a)
  85. #define writeb(v, a) __arch_putb(v, a)
  86. #define writew(v, a) __arch_putw(v, a)
  87. #define writel(v, a) __arch_putl(v, a)
  88. #define readb(a) __arch_getb(a)
  89. #define readw(a) __arch_getw(a)
  90. #define readl(a) __arch_getl(a)
  91. /*
  92. * The compiler seems to be incapable of optimising constants
  93. * properly. Spell it out to the compiler in some cases.
  94. * These are only valid for small values of "off" (< 1<<12)
  95. */
  96. #define __raw_base_writeb(val, base, off) __arch_base_putb(val, base, off)
  97. #define __raw_base_writew(val, base, off) __arch_base_putw(val, base, off)
  98. #define __raw_base_writel(val, base, off) __arch_base_putl(val, base, off)
  99. #define __raw_base_readb(base, off) __arch_base_getb(base, off)
  100. #define __raw_base_readw(base, off) __arch_base_getw(base, off)
  101. #define __raw_base_readl(base, off) __arch_base_getl(base, off)
  102. /*
  103. * Now, pick up the machine-defined IO definitions
  104. * #include <asm/arch/io.h>
  105. */
  106. /*
  107. * IO port access primitives
  108. * -------------------------
  109. *
  110. * The NDS32 doesn't have special IO access instructions just like ARM;
  111. * all IO is memory mapped.
  112. * Note that these are defined to perform little endian accesses
  113. * only. Their primary purpose is to access PCI and ISA peripherals.
  114. *
  115. * Note that for a big endian machine, this implies that the following
  116. * big endian mode connectivity is in place, as described by numerious
  117. * ARM documents:
  118. *
  119. * PCI: D0-D7 D8-D15 D16-D23 D24-D31
  120. * ARM: D24-D31 D16-D23 D8-D15 D0-D7
  121. *
  122. * The machine specific io.h include defines __io to translate an "IO"
  123. * address to a memory address.
  124. *
  125. * Note that we prevent GCC re-ordering or caching values in expressions
  126. * by introducing sequence points into the in*() definitions. Note that
  127. * __raw_* do not guarantee this behaviour.
  128. *
  129. * The {in,out}[bwl] macros are for emulating x86-style PCI/ISA IO space.
  130. */
  131. #ifdef __io
  132. #define outb(v, p) __raw_writeb(v, __io(p))
  133. #define outw(v, p) __raw_writew(cpu_to_le16(v), __io(p))
  134. #define outl(v, p) __raw_writel(cpu_to_le32(v), __io(p))
  135. #define inb(p) ({ unsigned int __v = __raw_readb(__io(p)); __v; })
  136. #define inw(p) ({ unsigned int __v = le16_to_cpu(__raw_readw(__io(p))); __v; })
  137. #define inl(p) ({ unsigned int __v = le32_to_cpu(__raw_readl(__io(p))); __v; })
  138. #define outsb(p, d, l) writesb(__io(p), d, l)
  139. #define outsw(p, d, l) writesw(__io(p), d, l)
  140. #define outsl(p, d, l) writesl(__io(p), d, l)
  141. #define insb(p, d, l) readsb(__io(p), d, l)
  142. #define insw(p, d, l) readsw(__io(p), d, l)
  143. #define insl(p, d, l) readsl(__io(p), d, l)
  144. static inline void readsb(unsigned int *addr, void * data, int bytelen)
  145. {
  146. unsigned char *ptr = (unsigned char *)addr;
  147. unsigned char *ptr2 = (unsigned char *)data;
  148. while (bytelen) {
  149. *ptr2 = *ptr;
  150. ptr2++;
  151. bytelen--;
  152. }
  153. }
  154. static inline void readsw(unsigned int *addr, void * data, int wordlen)
  155. {
  156. unsigned short *ptr = (unsigned short *)addr;
  157. unsigned short *ptr2 = (unsigned short *)data;
  158. while (wordlen) {
  159. *ptr2 = *ptr;
  160. ptr2++;
  161. wordlen--;
  162. }
  163. }
  164. static inline void readsl(unsigned int *addr, void * data, int longlen)
  165. {
  166. unsigned int *ptr = (unsigned int *)addr;
  167. unsigned int *ptr2 = (unsigned int *)data;
  168. while (longlen) {
  169. *ptr2 = *ptr;
  170. ptr2++;
  171. longlen--;
  172. }
  173. }
  174. static inline void writesb(unsigned int *addr, const void * data, int bytelen)
  175. {
  176. unsigned char *ptr = (unsigned char *)addr;
  177. unsigned char *ptr2 = (unsigned char *)data;
  178. while (bytelen) {
  179. *ptr = *ptr2;
  180. ptr2++;
  181. bytelen--;
  182. }
  183. }
  184. static inline void writesw(unsigned int *addr, const void * data, int wordlen)
  185. {
  186. unsigned short *ptr = (unsigned short *)addr;
  187. unsigned short *ptr2 = (unsigned short *)data;
  188. while (wordlen) {
  189. *ptr = *ptr2;
  190. ptr2++;
  191. wordlen--;
  192. }
  193. }
  194. static inline void writesl(unsigned int *addr, const void * data, int longlen)
  195. {
  196. unsigned int *ptr = (unsigned int *)addr;
  197. unsigned int *ptr2 = (unsigned int *)data;
  198. while (longlen) {
  199. *ptr = *ptr2;
  200. ptr2++;
  201. longlen--;
  202. }
  203. }
  204. #endif
  205. #define outb_p(val, port) outb((val), (port))
  206. #define outw_p(val, port) outw((val), (port))
  207. #define outl_p(val, port) outl((val), (port))
  208. #define inb_p(port) inb((port))
  209. #define inw_p(port) inw((port))
  210. #define inl_p(port) inl((port))
  211. #define outsb_p(port, from, len) outsb(port, from, len)
  212. #define outsw_p(port, from, len) outsw(port, from, len)
  213. #define outsl_p(port, from, len) outsl(port, from, len)
  214. #define insb_p(port, to, len) insb(port, to, len)
  215. #define insw_p(port, to, len) insw(port, to, len)
  216. #define insl_p(port, to, len) insl(port, to, len)
  217. /*
  218. * ioremap and friends.
  219. *
  220. * ioremap takes a PCI memory address, as specified in
  221. * linux/Documentation/IO-mapping.txt. If you want a
  222. * physical address, use __ioremap instead.
  223. */
  224. extern void *__ioremap(unsigned long offset, size_t size, unsigned long flags);
  225. extern void __iounmap(void *addr);
  226. /*
  227. * Generic ioremap support.
  228. *
  229. * Define:
  230. * iomem_valid_addr(off,size)
  231. * iomem_to_phys(off)
  232. */
  233. #ifdef iomem_valid_addr
  234. #define __arch_ioremap(off, sz, nocache) \
  235. ({ \
  236. unsigned long _off = (off), _size = (sz); \
  237. void *_ret = (void *)0; \
  238. if (iomem_valid_addr(_off, _size)) \
  239. _ret = __ioremap(iomem_to_phys(_off), _size, 0); \
  240. _ret; \
  241. })
  242. #define __arch_iounmap __iounmap
  243. #endif
  244. #define ioremap(off, sz) __arch_ioremap((off), (sz), 0)
  245. #define ioremap_nocache(off, sz) __arch_ioremap((off), (sz), 1)
  246. #define iounmap(_addr) __arch_iounmap(_addr)
  247. /*
  248. * DMA-consistent mapping functions. These allocate/free a region of
  249. * uncached, unwrite-buffered mapped memory space for use with DMA
  250. * devices. This is the "generic" version. The PCI specific version
  251. * is in pci.h
  252. */
  253. extern void *consistent_alloc(int gfp, size_t size, dma_addr_t *handle);
  254. extern void consistent_free(void *vaddr, size_t size, dma_addr_t handle);
  255. extern void consistent_sync(void *vaddr, size_t size, int rw);
  256. /*
  257. * String version of IO memory access ops:
  258. */
  259. extern void _memcpy_fromio(void *, unsigned long, size_t);
  260. extern void _memcpy_toio(unsigned long, const void *, size_t);
  261. extern void _memset_io(unsigned long, int, size_t);
  262. extern void __readwrite_bug(const char *fn);
  263. /*
  264. * If this architecture has PCI memory IO, then define the read/write
  265. * macros. These should only be used with the cookie passed from
  266. * ioremap.
  267. */
  268. #ifdef __mem_pci
  269. #define readb(c) ({ unsigned int __v = \
  270. __raw_readb(__mem_pci(c)); __v; })
  271. #define readw(c) ({ unsigned int __v = \
  272. le16_to_cpu(__raw_readw(__mem_pci(c))); __v; })
  273. #define readl(c) ({ unsigned int __v = \
  274. le32_to_cpu(__raw_readl(__mem_pci(c))); __v; })
  275. #define writeb(v, c) __raw_writeb(v, __mem_pci(c))
  276. #define writew(v, c) __raw_writew(cpu_to_le16(v), __mem_pci(c))
  277. #define writel(v, c) __raw_writel(cpu_to_le32(v), __mem_pci(c))
  278. #define memset_io(c, v, l) _memset_io(__mem_pci(c), (v), (l))
  279. #define memcpy_fromio(a, c, l) _memcpy_fromio((a), __mem_pci(c), (l))
  280. #define memcpy_toio(c, a, l) _memcpy_toio(__mem_pci(c), (a), (l))
  281. #define eth_io_copy_and_sum(s, c, l, b) \
  282. eth_copy_and_sum((s), __mem_pci(c), (l), (b))
  283. static inline int
  284. check_signature(unsigned long io_addr, const unsigned char *signature,
  285. int length)
  286. {
  287. int retval = 0;
  288. do {
  289. if (readb(io_addr) != *signature)
  290. goto out;
  291. io_addr++;
  292. signature++;
  293. length--;
  294. } while (length);
  295. retval = 1;
  296. out:
  297. return retval;
  298. }
  299. #elif !defined(readb)
  300. #define readb(addr) (__readwrite_bug("readb"), 0)
  301. #define readw(addr) (__readwrite_bug("readw"), 0)
  302. #define readl(addr) (__readwrite_bug("readl"), 0)
  303. #define writeb(v, addr) __readwrite_bug("writeb")
  304. #define writew(v, addr) __readwrite_bug("writew")
  305. #define writel(v, addr) __readwrite_bug("writel")
  306. #define eth_io_copy_and_sum(a, b, c, d) __readwrite_bug("eth_io_copy_and_sum")
  307. #define check_signature(io, sig, len) (0)
  308. #endif /* __mem_pci */
  309. /*
  310. * If this architecture has ISA IO, then define the isa_read/isa_write
  311. * macros.
  312. */
  313. #ifdef __mem_isa
  314. #define isa_readb(addr) __raw_readb(__mem_isa(addr))
  315. #define isa_readw(addr) __raw_readw(__mem_isa(addr))
  316. #define isa_readl(addr) __raw_readl(__mem_isa(addr))
  317. #define isa_writeb(val, addr) __raw_writeb(val, __mem_isa(addr))
  318. #define isa_writew(val, addr) __raw_writew(val, __mem_isa(addr))
  319. #define isa_writel(val, addr) __raw_writel(val, __mem_isa(addr))
  320. #define isa_memset_io(a, b, c) _memset_io(__mem_isa(a), (b), (c))
  321. #define isa_memcpy_fromio(a, b, c) _memcpy_fromio((a), __mem_isa(b), (c))
  322. #define isa_memcpy_toio(a, b, c) _memcpy_toio(__mem_isa((a)), (b), (c))
  323. #define isa_eth_io_copy_and_sum(a, b, c, d) \
  324. eth_copy_and_sum((a), __mem_isa(b), (c), (d))
  325. static inline int
  326. isa_check_signature(unsigned long io_addr, const unsigned char *signature,
  327. int length)
  328. {
  329. int retval = 0;
  330. do {
  331. if (isa_readb(io_addr) != *signature)
  332. goto out;
  333. io_addr++;
  334. signature++;
  335. length--;
  336. } while (length);
  337. retval = 1;
  338. out:
  339. return retval;
  340. }
  341. #else /* __mem_isa */
  342. #define isa_readb(addr) (__readwrite_bug("isa_readb"), 0)
  343. #define isa_readw(addr) (__readwrite_bug("isa_readw"), 0)
  344. #define isa_readl(addr) (__readwrite_bug("isa_readl"), 0)
  345. #define isa_writeb(val, addr) __readwrite_bug("isa_writeb")
  346. #define isa_writew(val, addr) __readwrite_bug("isa_writew")
  347. #define isa_writel(val, addr) __readwrite_bug("isa_writel")
  348. #define isa_memset_io(a, b, c) __readwrite_bug("isa_memset_io")
  349. #define isa_memcpy_fromio(a, b, c) __readwrite_bug("isa_memcpy_fromio")
  350. #define isa_memcpy_toio(a, b, c) __readwrite_bug("isa_memcpy_toio")
  351. #define isa_eth_io_copy_and_sum(a, b, c, d) \
  352. __readwrite_bug("isa_eth_io_copy_and_sum")
  353. #define isa_check_signature(io, sig, len) (0)
  354. #endif /* __mem_isa */
  355. #endif /* __KERNEL__ */
  356. #endif /* __ASM_NDS_IO_H */