io.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. #define out_arch(type, endian, a, v) __raw_write##type(cpu_to_##endian(v), a)
  140. #define in_arch(type, endian, a) endian##_to_cpu(__raw_read##type(a))
  141. #define out_le32(a, v) out_arch(l, le32, a, v)
  142. #define out_le16(a, v) out_arch(w, le16, a, v)
  143. #define in_le32(a) in_arch(l, le32, a)
  144. #define in_le16(a) in_arch(w, le16, a)
  145. #define out_be32(a, v) out_arch(l, be32, a, v)
  146. #define out_be16(a, v) out_arch(w, be16, a, v)
  147. #define in_be32(a) in_arch(l, be32, a)
  148. #define in_be16(a) in_arch(w, be16, a)
  149. #define out_8(a, v) __raw_writeb(v, a)
  150. #define in_8(a) __raw_readb(a)
  151. /*
  152. * Now, pick up the machine-defined IO definitions
  153. * #include <asm/arch/io.h>
  154. */
  155. /*
  156. * IO port access primitives
  157. * -------------------------
  158. *
  159. * The NDS32 doesn't have special IO access instructions just like ARM;
  160. * all IO is memory mapped.
  161. * Note that these are defined to perform little endian accesses
  162. * only. Their primary purpose is to access PCI and ISA peripherals.
  163. *
  164. * Note that for a big endian machine, this implies that the following
  165. * big endian mode connectivity is in place, as described by numerious
  166. * ARM documents:
  167. *
  168. * PCI: D0-D7 D8-D15 D16-D23 D24-D31
  169. * ARM: D24-D31 D16-D23 D8-D15 D0-D7
  170. *
  171. * The machine specific io.h include defines __io to translate an "IO"
  172. * address to a memory address.
  173. *
  174. * Note that we prevent GCC re-ordering or caching values in expressions
  175. * by introducing sequence points into the in*() definitions. Note that
  176. * __raw_* do not guarantee this behaviour.
  177. *
  178. * The {in,out}[bwl] macros are for emulating x86-style PCI/ISA IO space.
  179. */
  180. #ifdef __io
  181. #define outb(v, p) __raw_writeb(v, __io(p))
  182. #define outw(v, p) __raw_writew(cpu_to_le16(v), __io(p))
  183. #define outl(v, p) __raw_writel(cpu_to_le32(v), __io(p))
  184. #define inb(p) ({ unsigned int __v = __raw_readb(__io(p)); __v; })
  185. #define inw(p) ({ unsigned int __v = le16_to_cpu(__raw_readw(__io(p))); __v; })
  186. #define inl(p) ({ unsigned int __v = le32_to_cpu(__raw_readl(__io(p))); __v; })
  187. #define outsb(p, d, l) writesb(__io(p), d, l)
  188. #define outsw(p, d, l) writesw(__io(p), d, l)
  189. #define outsl(p, d, l) writesl(__io(p), d, l)
  190. #define insb(p, d, l) readsb(__io(p), d, l)
  191. #define insw(p, d, l) readsw(__io(p), d, l)
  192. #define insl(p, d, l) readsl(__io(p), d, l)
  193. static inline void readsb(unsigned int *addr, void * data, int bytelen)
  194. {
  195. unsigned char *ptr = (unsigned char *)addr;
  196. unsigned char *ptr2 = (unsigned char *)data;
  197. while (bytelen) {
  198. *ptr2 = *ptr;
  199. ptr2++;
  200. bytelen--;
  201. }
  202. }
  203. static inline void readsw(unsigned int *addr, void * data, int wordlen)
  204. {
  205. unsigned short *ptr = (unsigned short *)addr;
  206. unsigned short *ptr2 = (unsigned short *)data;
  207. while (wordlen) {
  208. *ptr2 = *ptr;
  209. ptr2++;
  210. wordlen--;
  211. }
  212. }
  213. static inline void readsl(unsigned int *addr, void * data, int longlen)
  214. {
  215. unsigned int *ptr = (unsigned int *)addr;
  216. unsigned int *ptr2 = (unsigned int *)data;
  217. while (longlen) {
  218. *ptr2 = *ptr;
  219. ptr2++;
  220. longlen--;
  221. }
  222. }
  223. static inline void writesb(unsigned int *addr, const void * data, int bytelen)
  224. {
  225. unsigned char *ptr = (unsigned char *)addr;
  226. unsigned char *ptr2 = (unsigned char *)data;
  227. while (bytelen) {
  228. *ptr = *ptr2;
  229. ptr2++;
  230. bytelen--;
  231. }
  232. }
  233. static inline void writesw(unsigned int *addr, const void * data, int wordlen)
  234. {
  235. unsigned short *ptr = (unsigned short *)addr;
  236. unsigned short *ptr2 = (unsigned short *)data;
  237. while (wordlen) {
  238. *ptr = *ptr2;
  239. ptr2++;
  240. wordlen--;
  241. }
  242. }
  243. static inline void writesl(unsigned int *addr, const void * data, int longlen)
  244. {
  245. unsigned int *ptr = (unsigned int *)addr;
  246. unsigned int *ptr2 = (unsigned int *)data;
  247. while (longlen) {
  248. *ptr = *ptr2;
  249. ptr2++;
  250. longlen--;
  251. }
  252. }
  253. #endif
  254. #define outb_p(val, port) outb((val), (port))
  255. #define outw_p(val, port) outw((val), (port))
  256. #define outl_p(val, port) outl((val), (port))
  257. #define inb_p(port) inb((port))
  258. #define inw_p(port) inw((port))
  259. #define inl_p(port) inl((port))
  260. #define outsb_p(port, from, len) outsb(port, from, len)
  261. #define outsw_p(port, from, len) outsw(port, from, len)
  262. #define outsl_p(port, from, len) outsl(port, from, len)
  263. #define insb_p(port, to, len) insb(port, to, len)
  264. #define insw_p(port, to, len) insw(port, to, len)
  265. #define insl_p(port, to, len) insl(port, to, len)
  266. /*
  267. * ioremap and friends.
  268. *
  269. * ioremap takes a PCI memory address, as specified in
  270. * linux/Documentation/IO-mapping.txt. If you want a
  271. * physical address, use __ioremap instead.
  272. */
  273. extern void *__ioremap(unsigned long offset, size_t size, unsigned long flags);
  274. extern void __iounmap(void *addr);
  275. /*
  276. * Generic ioremap support.
  277. *
  278. * Define:
  279. * iomem_valid_addr(off,size)
  280. * iomem_to_phys(off)
  281. */
  282. #ifdef iomem_valid_addr
  283. #define __arch_ioremap(off, sz, nocache) \
  284. ({ \
  285. unsigned long _off = (off), _size = (sz); \
  286. void *_ret = (void *)0; \
  287. if (iomem_valid_addr(_off, _size)) \
  288. _ret = __ioremap(iomem_to_phys(_off), _size, 0); \
  289. _ret; \
  290. })
  291. #define __arch_iounmap __iounmap
  292. #endif
  293. #define ioremap(off, sz) __arch_ioremap((off), (sz), 0)
  294. #define ioremap_nocache(off, sz) __arch_ioremap((off), (sz), 1)
  295. #define iounmap(_addr) __arch_iounmap(_addr)
  296. /*
  297. * DMA-consistent mapping functions. These allocate/free a region of
  298. * uncached, unwrite-buffered mapped memory space for use with DMA
  299. * devices. This is the "generic" version. The PCI specific version
  300. * is in pci.h
  301. */
  302. extern void *consistent_alloc(int gfp, size_t size, dma_addr_t *handle);
  303. extern void consistent_free(void *vaddr, size_t size, dma_addr_t handle);
  304. extern void consistent_sync(void *vaddr, size_t size, int rw);
  305. /*
  306. * String version of IO memory access ops:
  307. */
  308. extern void _memcpy_fromio(void *, unsigned long, size_t);
  309. extern void _memcpy_toio(unsigned long, const void *, size_t);
  310. extern void _memset_io(unsigned long, int, size_t);
  311. extern void __readwrite_bug(const char *fn);
  312. /*
  313. * If this architecture has PCI memory IO, then define the read/write
  314. * macros. These should only be used with the cookie passed from
  315. * ioremap.
  316. */
  317. #ifdef __mem_pci
  318. #define readb(c) ({ unsigned int __v = \
  319. __raw_readb(__mem_pci(c)); __v; })
  320. #define readw(c) ({ unsigned int __v = \
  321. le16_to_cpu(__raw_readw(__mem_pci(c))); __v; })
  322. #define readl(c) ({ unsigned int __v = \
  323. le32_to_cpu(__raw_readl(__mem_pci(c))); __v; })
  324. #define writeb(v, c) __raw_writeb(v, __mem_pci(c))
  325. #define writew(v, c) __raw_writew(cpu_to_le16(v), __mem_pci(c))
  326. #define writel(v, c) __raw_writel(cpu_to_le32(v), __mem_pci(c))
  327. #define memset_io(c, v, l) _memset_io(__mem_pci(c), (v), (l))
  328. #define memcpy_fromio(a, c, l) _memcpy_fromio((a), __mem_pci(c), (l))
  329. #define memcpy_toio(c, a, l) _memcpy_toio(__mem_pci(c), (a), (l))
  330. #define eth_io_copy_and_sum(s, c, l, b) \
  331. eth_copy_and_sum((s), __mem_pci(c), (l), (b))
  332. static inline int
  333. check_signature(unsigned long io_addr, const unsigned char *signature,
  334. int length)
  335. {
  336. int retval = 0;
  337. do {
  338. if (readb(io_addr) != *signature)
  339. goto out;
  340. io_addr++;
  341. signature++;
  342. length--;
  343. } while (length);
  344. retval = 1;
  345. out:
  346. return retval;
  347. }
  348. #endif /* __mem_pci */
  349. /*
  350. * If this architecture has ISA IO, then define the isa_read/isa_write
  351. * macros.
  352. */
  353. #ifdef __mem_isa
  354. #define isa_readb(addr) __raw_readb(__mem_isa(addr))
  355. #define isa_readw(addr) __raw_readw(__mem_isa(addr))
  356. #define isa_readl(addr) __raw_readl(__mem_isa(addr))
  357. #define isa_writeb(val, addr) __raw_writeb(val, __mem_isa(addr))
  358. #define isa_writew(val, addr) __raw_writew(val, __mem_isa(addr))
  359. #define isa_writel(val, addr) __raw_writel(val, __mem_isa(addr))
  360. #define isa_memset_io(a, b, c) _memset_io(__mem_isa(a), (b), (c))
  361. #define isa_memcpy_fromio(a, b, c) _memcpy_fromio((a), __mem_isa(b), (c))
  362. #define isa_memcpy_toio(a, b, c) _memcpy_toio(__mem_isa((a)), (b), (c))
  363. #define isa_eth_io_copy_and_sum(a, b, c, d) \
  364. eth_copy_and_sum((a), __mem_isa(b), (c), (d))
  365. static inline int
  366. isa_check_signature(unsigned long io_addr, const unsigned char *signature,
  367. int length)
  368. {
  369. int retval = 0;
  370. do {
  371. if (isa_readb(io_addr) != *signature)
  372. goto out;
  373. io_addr++;
  374. signature++;
  375. length--;
  376. } while (length);
  377. retval = 1;
  378. out:
  379. return retval;
  380. }
  381. #else /* __mem_isa */
  382. #define isa_readb(addr) (__readwrite_bug("isa_readb"), 0)
  383. #define isa_readw(addr) (__readwrite_bug("isa_readw"), 0)
  384. #define isa_readl(addr) (__readwrite_bug("isa_readl"), 0)
  385. #define isa_writeb(val, addr) __readwrite_bug("isa_writeb")
  386. #define isa_writew(val, addr) __readwrite_bug("isa_writew")
  387. #define isa_writel(val, addr) __readwrite_bug("isa_writel")
  388. #define isa_memset_io(a, b, c) __readwrite_bug("isa_memset_io")
  389. #define isa_memcpy_fromio(a, b, c) __readwrite_bug("isa_memcpy_fromio")
  390. #define isa_memcpy_toio(a, b, c) __readwrite_bug("isa_memcpy_toio")
  391. #define isa_eth_io_copy_and_sum(a, b, c, d) \
  392. __readwrite_bug("isa_eth_io_copy_and_sum")
  393. #define isa_check_signature(io, sig, len) (0)
  394. #endif /* __mem_isa */
  395. #endif /* __KERNEL__ */
  396. #endif /* __ASM_NDS_IO_H */