io.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. /*
  86. * TODO: The kernel offers some more advanced versions of barriers, it might
  87. * have some advantages to use them instead of the simple one here.
  88. */
  89. #define dmb() __asm__ __volatile__ ("" : : : "memory")
  90. #define __iormb() dmb()
  91. #define __iowmb() dmb()
  92. static inline void writeb(unsigned char val, unsigned char *addr)
  93. {
  94. __iowmb();
  95. __arch_putb(val, addr);
  96. }
  97. static inline void writew(unsigned short val, unsigned short *addr)
  98. {
  99. __iowmb();
  100. __arch_putw(val, addr);
  101. }
  102. static inline void writel(unsigned int val, unsigned int *addr)
  103. {
  104. __iowmb();
  105. __arch_putl(val, addr);
  106. }
  107. static inline unsigned char readb(unsigned char *addr)
  108. {
  109. u8 val;
  110. val = __arch_getb(addr);
  111. __iormb();
  112. return val;
  113. }
  114. static inline unsigned short readw(unsigned short *addr)
  115. {
  116. u16 val;
  117. val = __arch_getw(addr);
  118. __iormb();
  119. return val;
  120. }
  121. static inline unsigned int readl(unsigned int *addr)
  122. {
  123. u32 val;
  124. val = __arch_getl(addr);
  125. __iormb();
  126. return val;
  127. }
  128. /*
  129. * The compiler seems to be incapable of optimising constants
  130. * properly. Spell it out to the compiler in some cases.
  131. * These are only valid for small values of "off" (< 1<<12)
  132. */
  133. #define __raw_base_writeb(val, base, off) __arch_base_putb(val, base, off)
  134. #define __raw_base_writew(val, base, off) __arch_base_putw(val, base, off)
  135. #define __raw_base_writel(val, base, off) __arch_base_putl(val, base, off)
  136. #define __raw_base_readb(base, off) __arch_base_getb(base, off)
  137. #define __raw_base_readw(base, off) __arch_base_getw(base, off)
  138. #define __raw_base_readl(base, off) __arch_base_getl(base, off)
  139. /*
  140. * Now, pick up the machine-defined IO definitions
  141. * #include <asm/arch/io.h>
  142. */
  143. /*
  144. * IO port access primitives
  145. * -------------------------
  146. *
  147. * The NDS32 doesn't have special IO access instructions just like ARM;
  148. * all IO is memory mapped.
  149. * Note that these are defined to perform little endian accesses
  150. * only. Their primary purpose is to access PCI and ISA peripherals.
  151. *
  152. * Note that for a big endian machine, this implies that the following
  153. * big endian mode connectivity is in place, as described by numerious
  154. * ARM documents:
  155. *
  156. * PCI: D0-D7 D8-D15 D16-D23 D24-D31
  157. * ARM: D24-D31 D16-D23 D8-D15 D0-D7
  158. *
  159. * The machine specific io.h include defines __io to translate an "IO"
  160. * address to a memory address.
  161. *
  162. * Note that we prevent GCC re-ordering or caching values in expressions
  163. * by introducing sequence points into the in*() definitions. Note that
  164. * __raw_* do not guarantee this behaviour.
  165. *
  166. * The {in,out}[bwl] macros are for emulating x86-style PCI/ISA IO space.
  167. */
  168. #ifdef __io
  169. #define outb(v, p) __raw_writeb(v, __io(p))
  170. #define outw(v, p) __raw_writew(cpu_to_le16(v), __io(p))
  171. #define outl(v, p) __raw_writel(cpu_to_le32(v), __io(p))
  172. #define inb(p) ({ unsigned int __v = __raw_readb(__io(p)); __v; })
  173. #define inw(p) ({ unsigned int __v = le16_to_cpu(__raw_readw(__io(p))); __v; })
  174. #define inl(p) ({ unsigned int __v = le32_to_cpu(__raw_readl(__io(p))); __v; })
  175. #define outsb(p, d, l) writesb(__io(p), d, l)
  176. #define outsw(p, d, l) writesw(__io(p), d, l)
  177. #define outsl(p, d, l) writesl(__io(p), d, l)
  178. #define insb(p, d, l) readsb(__io(p), d, l)
  179. #define insw(p, d, l) readsw(__io(p), d, l)
  180. #define insl(p, d, l) readsl(__io(p), d, l)
  181. static inline void readsb(unsigned int *addr, void * data, int bytelen)
  182. {
  183. unsigned char *ptr = (unsigned char *)addr;
  184. unsigned char *ptr2 = (unsigned char *)data;
  185. while (bytelen) {
  186. *ptr2 = *ptr;
  187. ptr2++;
  188. bytelen--;
  189. }
  190. }
  191. static inline void readsw(unsigned int *addr, void * data, int wordlen)
  192. {
  193. unsigned short *ptr = (unsigned short *)addr;
  194. unsigned short *ptr2 = (unsigned short *)data;
  195. while (wordlen) {
  196. *ptr2 = *ptr;
  197. ptr2++;
  198. wordlen--;
  199. }
  200. }
  201. static inline void readsl(unsigned int *addr, void * data, int longlen)
  202. {
  203. unsigned int *ptr = (unsigned int *)addr;
  204. unsigned int *ptr2 = (unsigned int *)data;
  205. while (longlen) {
  206. *ptr2 = *ptr;
  207. ptr2++;
  208. longlen--;
  209. }
  210. }
  211. static inline void writesb(unsigned int *addr, const void * data, int bytelen)
  212. {
  213. unsigned char *ptr = (unsigned char *)addr;
  214. unsigned char *ptr2 = (unsigned char *)data;
  215. while (bytelen) {
  216. *ptr = *ptr2;
  217. ptr2++;
  218. bytelen--;
  219. }
  220. }
  221. static inline void writesw(unsigned int *addr, const void * data, int wordlen)
  222. {
  223. unsigned short *ptr = (unsigned short *)addr;
  224. unsigned short *ptr2 = (unsigned short *)data;
  225. while (wordlen) {
  226. *ptr = *ptr2;
  227. ptr2++;
  228. wordlen--;
  229. }
  230. }
  231. static inline void writesl(unsigned int *addr, const void * data, int longlen)
  232. {
  233. unsigned int *ptr = (unsigned int *)addr;
  234. unsigned int *ptr2 = (unsigned int *)data;
  235. while (longlen) {
  236. *ptr = *ptr2;
  237. ptr2++;
  238. longlen--;
  239. }
  240. }
  241. #endif
  242. #define outb_p(val, port) outb((val), (port))
  243. #define outw_p(val, port) outw((val), (port))
  244. #define outl_p(val, port) outl((val), (port))
  245. #define inb_p(port) inb((port))
  246. #define inw_p(port) inw((port))
  247. #define inl_p(port) inl((port))
  248. #define outsb_p(port, from, len) outsb(port, from, len)
  249. #define outsw_p(port, from, len) outsw(port, from, len)
  250. #define outsl_p(port, from, len) outsl(port, from, len)
  251. #define insb_p(port, to, len) insb(port, to, len)
  252. #define insw_p(port, to, len) insw(port, to, len)
  253. #define insl_p(port, to, len) insl(port, to, len)
  254. /*
  255. * ioremap and friends.
  256. *
  257. * ioremap takes a PCI memory address, as specified in
  258. * linux/Documentation/IO-mapping.txt. If you want a
  259. * physical address, use __ioremap instead.
  260. */
  261. extern void *__ioremap(unsigned long offset, size_t size, unsigned long flags);
  262. extern void __iounmap(void *addr);
  263. /*
  264. * Generic ioremap support.
  265. *
  266. * Define:
  267. * iomem_valid_addr(off,size)
  268. * iomem_to_phys(off)
  269. */
  270. #ifdef iomem_valid_addr
  271. #define __arch_ioremap(off, sz, nocache) \
  272. ({ \
  273. unsigned long _off = (off), _size = (sz); \
  274. void *_ret = (void *)0; \
  275. if (iomem_valid_addr(_off, _size)) \
  276. _ret = __ioremap(iomem_to_phys(_off), _size, 0); \
  277. _ret; \
  278. })
  279. #define __arch_iounmap __iounmap
  280. #endif
  281. #define ioremap(off, sz) __arch_ioremap((off), (sz), 0)
  282. #define ioremap_nocache(off, sz) __arch_ioremap((off), (sz), 1)
  283. #define iounmap(_addr) __arch_iounmap(_addr)
  284. /*
  285. * DMA-consistent mapping functions. These allocate/free a region of
  286. * uncached, unwrite-buffered mapped memory space for use with DMA
  287. * devices. This is the "generic" version. The PCI specific version
  288. * is in pci.h
  289. */
  290. extern void *consistent_alloc(int gfp, size_t size, dma_addr_t *handle);
  291. extern void consistent_free(void *vaddr, size_t size, dma_addr_t handle);
  292. extern void consistent_sync(void *vaddr, size_t size, int rw);
  293. /*
  294. * String version of IO memory access ops:
  295. */
  296. extern void _memcpy_fromio(void *, unsigned long, size_t);
  297. extern void _memcpy_toio(unsigned long, const void *, size_t);
  298. extern void _memset_io(unsigned long, int, size_t);
  299. extern void __readwrite_bug(const char *fn);
  300. /*
  301. * If this architecture has PCI memory IO, then define the read/write
  302. * macros. These should only be used with the cookie passed from
  303. * ioremap.
  304. */
  305. #ifdef __mem_pci
  306. #define readb(c) ({ unsigned int __v = \
  307. __raw_readb(__mem_pci(c)); __v; })
  308. #define readw(c) ({ unsigned int __v = \
  309. le16_to_cpu(__raw_readw(__mem_pci(c))); __v; })
  310. #define readl(c) ({ unsigned int __v = \
  311. le32_to_cpu(__raw_readl(__mem_pci(c))); __v; })
  312. #define writeb(v, c) __raw_writeb(v, __mem_pci(c))
  313. #define writew(v, c) __raw_writew(cpu_to_le16(v), __mem_pci(c))
  314. #define writel(v, c) __raw_writel(cpu_to_le32(v), __mem_pci(c))
  315. #define memset_io(c, v, l) _memset_io(__mem_pci(c), (v), (l))
  316. #define memcpy_fromio(a, c, l) _memcpy_fromio((a), __mem_pci(c), (l))
  317. #define memcpy_toio(c, a, l) _memcpy_toio(__mem_pci(c), (a), (l))
  318. #define eth_io_copy_and_sum(s, c, l, b) \
  319. eth_copy_and_sum((s), __mem_pci(c), (l), (b))
  320. static inline int
  321. check_signature(unsigned long io_addr, const unsigned char *signature,
  322. int length)
  323. {
  324. int retval = 0;
  325. do {
  326. if (readb(io_addr) != *signature)
  327. goto out;
  328. io_addr++;
  329. signature++;
  330. length--;
  331. } while (length);
  332. retval = 1;
  333. out:
  334. return retval;
  335. }
  336. #endif /* __mem_pci */
  337. /*
  338. * If this architecture has ISA IO, then define the isa_read/isa_write
  339. * macros.
  340. */
  341. #ifdef __mem_isa
  342. #define isa_readb(addr) __raw_readb(__mem_isa(addr))
  343. #define isa_readw(addr) __raw_readw(__mem_isa(addr))
  344. #define isa_readl(addr) __raw_readl(__mem_isa(addr))
  345. #define isa_writeb(val, addr) __raw_writeb(val, __mem_isa(addr))
  346. #define isa_writew(val, addr) __raw_writew(val, __mem_isa(addr))
  347. #define isa_writel(val, addr) __raw_writel(val, __mem_isa(addr))
  348. #define isa_memset_io(a, b, c) _memset_io(__mem_isa(a), (b), (c))
  349. #define isa_memcpy_fromio(a, b, c) _memcpy_fromio((a), __mem_isa(b), (c))
  350. #define isa_memcpy_toio(a, b, c) _memcpy_toio(__mem_isa((a)), (b), (c))
  351. #define isa_eth_io_copy_and_sum(a, b, c, d) \
  352. eth_copy_and_sum((a), __mem_isa(b), (c), (d))
  353. static inline int
  354. isa_check_signature(unsigned long io_addr, const unsigned char *signature,
  355. int length)
  356. {
  357. int retval = 0;
  358. do {
  359. if (isa_readb(io_addr) != *signature)
  360. goto out;
  361. io_addr++;
  362. signature++;
  363. length--;
  364. } while (length);
  365. retval = 1;
  366. out:
  367. return retval;
  368. }
  369. #else /* __mem_isa */
  370. #define isa_readb(addr) (__readwrite_bug("isa_readb"), 0)
  371. #define isa_readw(addr) (__readwrite_bug("isa_readw"), 0)
  372. #define isa_readl(addr) (__readwrite_bug("isa_readl"), 0)
  373. #define isa_writeb(val, addr) __readwrite_bug("isa_writeb")
  374. #define isa_writew(val, addr) __readwrite_bug("isa_writew")
  375. #define isa_writel(val, addr) __readwrite_bug("isa_writel")
  376. #define isa_memset_io(a, b, c) __readwrite_bug("isa_memset_io")
  377. #define isa_memcpy_fromio(a, b, c) __readwrite_bug("isa_memcpy_fromio")
  378. #define isa_memcpy_toio(a, b, c) __readwrite_bug("isa_memcpy_toio")
  379. #define isa_eth_io_copy_and_sum(a, b, c, d) \
  380. __readwrite_bug("isa_eth_io_copy_and_sum")
  381. #define isa_check_signature(io, sig, len) (0)
  382. #endif /* __mem_isa */
  383. #endif /* __KERNEL__ */
  384. #endif /* __ASM_NDS_IO_H */