io.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * linux/include/asm-arm/io.h
  3. *
  4. * Copyright (C) 1996-2000 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Modifications:
  11. * 16-Sep-1996 RMK Inlined the inx/outx functions & optimised for both
  12. * constant addresses and variable addresses.
  13. * 04-Dec-1997 RMK Moved a lot of this stuff to the new architecture
  14. * specific IO header files.
  15. * 27-Mar-1999 PJB Second parameter of memcpy_toio is const..
  16. * 04-Apr-1999 PJB Added check_signature.
  17. * 12-Dec-1999 RMK More cleanups
  18. * 18-Jun-2000 RMK Removed virt_to_* and friends definitions
  19. */
  20. #ifndef __ASM_ARM_IO_H
  21. #define __ASM_ARM_IO_H
  22. #ifdef __KERNEL__
  23. #include <linux/types.h>
  24. #include <asm/byteorder.h>
  25. #include <asm/memory.h>
  26. #include <asm/hardware.h>
  27. /*
  28. * Generic IO read/write. These perform native-endian accesses. Note
  29. * that some architectures will want to re-define __raw_{read,write}w.
  30. */
  31. extern void __raw_writesb(unsigned int addr, const void *data, int bytelen);
  32. extern void __raw_writesw(unsigned int addr, const void *data, int wordlen);
  33. extern void __raw_writesl(unsigned int addr, const void *data, int longlen);
  34. extern void __raw_readsb(unsigned int addr, void *data, int bytelen);
  35. extern void __raw_readsw(unsigned int addr, void *data, int wordlen);
  36. extern void __raw_readsl(unsigned int addr, void *data, int longlen);
  37. #define __raw_writeb(v,a) (*(volatile unsigned char *)(a) = (v))
  38. #define __raw_writew(v,a) (*(volatile unsigned short *)(a) = (v))
  39. #define __raw_writel(v,a) (*(volatile unsigned int *)(a) = (v))
  40. #define __raw_readb(a) (*(volatile unsigned char *)(a))
  41. #define __raw_readw(a) (*(volatile unsigned short *)(a))
  42. #define __raw_readl(a) (*(volatile unsigned int *)(a))
  43. /*
  44. * Bad read/write accesses...
  45. */
  46. extern void __readwrite_bug(const char *fn);
  47. /*
  48. * Now, pick up the machine-defined IO definitions
  49. */
  50. #define IO_SPACE_LIMIT 0xffffffff
  51. /*
  52. * GCC is totally crap at loading/storing data. We try to persuade it
  53. * to do the right thing by using these whereever possible instead of
  54. * the above.
  55. */
  56. #define __arch_base_getb(b,o) \
  57. ({ \
  58. unsigned int v, r = (b); \
  59. __asm__ __volatile__( \
  60. "ldrb %0, [%1, %2]" \
  61. : "=r" (v) \
  62. : "r" (r), "Ir" (o)); \
  63. v; \
  64. })
  65. #define __arch_base_getl(b,o) \
  66. ({ \
  67. unsigned int v, r = (b); \
  68. __asm__ __volatile__( \
  69. "ldr %0, [%1, %2]" \
  70. : "=r" (v) \
  71. : "r" (r), "Ir" (o)); \
  72. v; \
  73. })
  74. #define __arch_base_putb(v,b,o) \
  75. ({ \
  76. unsigned int r = (b); \
  77. __asm__ __volatile__( \
  78. "strb %0, [%1, %2]" \
  79. : \
  80. : "r" (v), "r" (r), "Ir" (o)); \
  81. })
  82. #define __arch_base_putl(v,b,o) \
  83. ({ \
  84. unsigned int r = (b); \
  85. __asm__ __volatile__( \
  86. "str %0, [%1, %2]" \
  87. : \
  88. : "r" (v), "r" (r), "Ir" (o)); \
  89. })
  90. /*
  91. * We use two different types of addressing - PC style addresses, and ARM
  92. * addresses. PC style accesses the PC hardware with the normal PC IO
  93. * addresses, eg 0x3f8 for serial#1. ARM addresses are 0x80000000+
  94. * and are translated to the start of IO. Note that all addresses are
  95. * shifted left!
  96. */
  97. #define __PORT_PCIO(x) (!((x) & 0x80000000))
  98. /*
  99. * Dynamic IO functions - let the compiler
  100. * optimize the expressions
  101. */
  102. static inline void __outb (unsigned int value, unsigned int port)
  103. {
  104. unsigned long temp;
  105. __asm__ __volatile__(
  106. "tst %2, #0x80000000\n\t"
  107. "mov %0, %4\n\t"
  108. "addeq %0, %0, %3\n\t"
  109. "strb %1, [%0, %2, lsl #2] @ outb"
  110. : "=&r" (temp)
  111. : "r" (value), "r" (port), "Ir" (PCIO_BASE - IO_BASE), "Ir" (IO_BASE)
  112. : "cc");
  113. }
  114. static inline void __outw (unsigned int value, unsigned int port)
  115. {
  116. unsigned long temp;
  117. __asm__ __volatile__(
  118. "tst %2, #0x80000000\n\t"
  119. "mov %0, %4\n\t"
  120. "addeq %0, %0, %3\n\t"
  121. "str %1, [%0, %2, lsl #2] @ outw"
  122. : "=&r" (temp)
  123. : "r" (value|value<<16), "r" (port), "Ir" (PCIO_BASE - IO_BASE), "Ir" (IO_BASE)
  124. : "cc");
  125. }
  126. static inline void __outl (unsigned int value, unsigned int port)
  127. {
  128. unsigned long temp;
  129. __asm__ __volatile__(
  130. "tst %2, #0x80000000\n\t"
  131. "mov %0, %4\n\t"
  132. "addeq %0, %0, %3\n\t"
  133. "str %1, [%0, %2, lsl #2] @ outl"
  134. : "=&r" (temp)
  135. : "r" (value), "r" (port), "Ir" (PCIO_BASE - IO_BASE), "Ir" (IO_BASE)
  136. : "cc");
  137. }
  138. #define DECLARE_DYN_IN(sz,fnsuffix,instr) \
  139. static inline unsigned sz __in##fnsuffix (unsigned int port) \
  140. { \
  141. unsigned long temp, value; \
  142. __asm__ __volatile__( \
  143. "tst %2, #0x80000000\n\t" \
  144. "mov %0, %4\n\t" \
  145. "addeq %0, %0, %3\n\t" \
  146. "ldr" instr " %1, [%0, %2, lsl #2] @ in" #fnsuffix \
  147. : "=&r" (temp), "=r" (value) \
  148. : "r" (port), "Ir" (PCIO_BASE - IO_BASE), "Ir" (IO_BASE) \
  149. : "cc"); \
  150. return (unsigned sz)value; \
  151. }
  152. static inline unsigned int __ioaddr (unsigned int port) \
  153. { \
  154. if (__PORT_PCIO(port)) \
  155. return (unsigned int)(PCIO_BASE + (port << 2)); \
  156. else \
  157. return (unsigned int)(IO_BASE + (port << 2)); \
  158. }
  159. #define DECLARE_IO(sz,fnsuffix,instr) \
  160. DECLARE_DYN_IN(sz,fnsuffix,instr)
  161. DECLARE_IO(char,b,"b")
  162. DECLARE_IO(short,w,"")
  163. DECLARE_IO(int,l,"")
  164. #undef DECLARE_IO
  165. #undef DECLARE_DYN_IN
  166. /*
  167. * Constant address IO functions
  168. *
  169. * These have to be macros for the 'J' constraint to work -
  170. * +/-4096 immediate operand.
  171. */
  172. #define __outbc(value,port) \
  173. ({ \
  174. if (__PORT_PCIO((port))) \
  175. __asm__ __volatile__( \
  176. "strb %0, [%1, %2] @ outbc" \
  177. : : "r" (value), "r" (PCIO_BASE), "Jr" ((port) << 2)); \
  178. else \
  179. __asm__ __volatile__( \
  180. "strb %0, [%1, %2] @ outbc" \
  181. : : "r" (value), "r" (IO_BASE), "r" ((port) << 2)); \
  182. })
  183. #define __inbc(port) \
  184. ({ \
  185. unsigned char result; \
  186. if (__PORT_PCIO((port))) \
  187. __asm__ __volatile__( \
  188. "ldrb %0, [%1, %2] @ inbc" \
  189. : "=r" (result) : "r" (PCIO_BASE), "Jr" ((port) << 2)); \
  190. else \
  191. __asm__ __volatile__( \
  192. "ldrb %0, [%1, %2] @ inbc" \
  193. : "=r" (result) : "r" (IO_BASE), "r" ((port) << 2)); \
  194. result; \
  195. })
  196. #define __outwc(value,port) \
  197. ({ \
  198. unsigned long v = value; \
  199. if (__PORT_PCIO((port))) \
  200. __asm__ __volatile__( \
  201. "str %0, [%1, %2] @ outwc" \
  202. : : "r" (v|v<<16), "r" (PCIO_BASE), "Jr" ((port) << 2)); \
  203. else \
  204. __asm__ __volatile__( \
  205. "str %0, [%1, %2] @ outwc" \
  206. : : "r" (v|v<<16), "r" (IO_BASE), "r" ((port) << 2)); \
  207. })
  208. #define __inwc(port) \
  209. ({ \
  210. unsigned short result; \
  211. if (__PORT_PCIO((port))) \
  212. __asm__ __volatile__( \
  213. "ldr %0, [%1, %2] @ inwc" \
  214. : "=r" (result) : "r" (PCIO_BASE), "Jr" ((port) << 2)); \
  215. else \
  216. __asm__ __volatile__( \
  217. "ldr %0, [%1, %2] @ inwc" \
  218. : "=r" (result) : "r" (IO_BASE), "r" ((port) << 2)); \
  219. result & 0xffff; \
  220. })
  221. #define __outlc(value,port) \
  222. ({ \
  223. unsigned long v = value; \
  224. if (__PORT_PCIO((port))) \
  225. __asm__ __volatile__( \
  226. "str %0, [%1, %2] @ outlc" \
  227. : : "r" (v), "r" (PCIO_BASE), "Jr" ((port) << 2)); \
  228. else \
  229. __asm__ __volatile__( \
  230. "str %0, [%1, %2] @ outlc" \
  231. : : "r" (v), "r" (IO_BASE), "r" ((port) << 2)); \
  232. })
  233. #define __inlc(port) \
  234. ({ \
  235. unsigned long result; \
  236. if (__PORT_PCIO((port))) \
  237. __asm__ __volatile__( \
  238. "ldr %0, [%1, %2] @ inlc" \
  239. : "=r" (result) : "r" (PCIO_BASE), "Jr" ((port) << 2)); \
  240. else \
  241. __asm__ __volatile__( \
  242. "ldr %0, [%1, %2] @ inlc" \
  243. : "=r" (result) : "r" (IO_BASE), "r" ((port) << 2)); \
  244. result; \
  245. })
  246. #define __ioaddrc(port) \
  247. ({ \
  248. unsigned long addr; \
  249. if (__PORT_PCIO((port))) \
  250. addr = PCIO_BASE + ((port) << 2); \
  251. else \
  252. addr = IO_BASE + ((port) << 2); \
  253. addr; \
  254. })
  255. #define inb(p) (__builtin_constant_p((p)) ? __inbc(p) : __inb(p))
  256. #define inw(p) (__builtin_constant_p((p)) ? __inwc(p) : __inw(p))
  257. #define inl(p) (__builtin_constant_p((p)) ? __inlc(p) : __inl(p))
  258. #define outb(v,p) (__builtin_constant_p((p)) ? __outbc(v,p) : __outb(v,p))
  259. #define outw(v,p) (__builtin_constant_p((p)) ? __outwc(v,p) : __outw(v,p))
  260. #define outl(v,p) (__builtin_constant_p((p)) ? __outlc(v,p) : __outl(v,p))
  261. #define __ioaddr(p) (__builtin_constant_p((p)) ? __ioaddr(p) : __ioaddrc(p))
  262. /* JMA 18.02.03 added sb,sl from arm/io.h, changing io to ioaddr */
  263. #define outsb(p,d,l) __raw_writesb(__ioaddr(p),d,l)
  264. #define outsw(p,d,l) __raw_writesw(__ioaddr(p),d,l)
  265. #define outsl(p,d,l) __raw_writesl(__ioaddr(p),d,l)
  266. #define insb(p,d,l) __raw_readsb(__ioaddr(p),d,l)
  267. #define insw(p,d,l) __raw_readsw(__ioaddr(p),d,l)
  268. #define insl(p,d,l) __raw_readsl(__ioaddr(p),d,l)
  269. #define insw(p,d,l) __raw_readsw(__ioaddr(p),d,l)
  270. #define outsw(p,d,l) __raw_writesw(__ioaddr(p),d,l)
  271. #define readb(c) (__readwrite_bug("readb"),0)
  272. #define readw(c) (__readwrite_bug("readw"),0)
  273. #define readl(c) (__readwrite_bug("readl"),0)
  274. #define readb_relaxed(addr) readb(addr)
  275. #define readw_relaxed(addr) readw(addr)
  276. #define readl_relaxed(addr) readl(addr)
  277. #define writeb(v,c) __readwrite_bug("writeb")
  278. #define writew(v,c) __readwrite_bug("writew")
  279. #define writel(v,c) __readwrite_bug("writel")
  280. #define readsw(p,d,l) (__readwrite_bug("readsw"),0)
  281. #define readsl(p,d,l) (__readwrite_bug("readsl"),0)
  282. #define writesw(p,d,l) __readwrite_bug("writesw")
  283. #define writesl(p,d,l) __readwrite_bug("writesl")
  284. #define mmiowb()
  285. /* the following macro is depreciated */
  286. #define ioaddr(port) __ioaddr((port))
  287. /*
  288. * No ioremap support here.
  289. */
  290. #define __arch_ioremap(c,s,f,a) ((void *)(c))
  291. #define __arch_iounmap(c) do { } while (0)
  292. #if defined(__arch_putb) || defined(__arch_putw) || defined(__arch_putl) || \
  293. defined(__arch_getb) || defined(__arch_getw) || defined(__arch_getl)
  294. #warning machine class uses old __arch_putw or __arch_getw
  295. #endif
  296. /*
  297. * IO port access primitives
  298. * -------------------------
  299. *
  300. * The ARM doesn't have special IO access instructions; all IO is memory
  301. * mapped. Note that these are defined to perform little endian accesses
  302. * only. Their primary purpose is to access PCI and ISA peripherals.
  303. *
  304. * Note that for a big endian machine, this implies that the following
  305. * big endian mode connectivity is in place, as described by numerious
  306. * ARM documents:
  307. *
  308. * PCI: D0-D7 D8-D15 D16-D23 D24-D31
  309. * ARM: D24-D31 D16-D23 D8-D15 D0-D7
  310. *
  311. * The machine specific io.h include defines __io to translate an "IO"
  312. * address to a memory address.
  313. *
  314. * Note that we prevent GCC re-ordering or caching values in expressions
  315. * by introducing sequence points into the in*() definitions. Note that
  316. * __raw_* do not guarantee this behaviour.
  317. */
  318. /*
  319. #define outsb(p,d,l) __raw_writesb(__io(p),d,l)
  320. #define outsw(p,d,l) __raw_writesw(__io(p),d,l)
  321. #define insb(p,d,l) __raw_readsb(__io(p),d,l)
  322. #define insw(p,d,l) __raw_readsw(__io(p),d,l)
  323. */
  324. #define outb_p(val,port) outb((val),(port))
  325. #define outw_p(val,port) outw((val),(port))
  326. #define inb_p(port) inb((port))
  327. #define inw_p(port) inw((port))
  328. #define inl_p(port) inl((port))
  329. #define outsb_p(port,from,len) outsb(port,from,len)
  330. #define outsw_p(port,from,len) outsw(port,from,len)
  331. #define insb_p(port,to,len) insb(port,to,len)
  332. #define insw_p(port,to,len) insw(port,to,len)
  333. /*
  334. * String version of IO memory access ops:
  335. */
  336. extern void _memcpy_fromio(void *, unsigned long, size_t);
  337. extern void _memcpy_toio(unsigned long, const void *, size_t);
  338. extern void _memset_io(unsigned long, int, size_t);
  339. /*
  340. * ioremap and friends.
  341. *
  342. * ioremap takes a PCI memory address, as specified in
  343. * Documentation/IO-mapping.txt.
  344. */
  345. extern void * __ioremap(unsigned long, size_t, unsigned long, unsigned long);
  346. extern void __iounmap(void *addr);
  347. #ifndef __arch_ioremap
  348. #define ioremap(cookie,size) __ioremap(cookie,size,0,1)
  349. #define ioremap_nocache(cookie,size) __ioremap(cookie,size,0,1)
  350. #define iounmap(cookie) __iounmap(cookie)
  351. #else
  352. #define ioremap(cookie,size) __arch_ioremap((cookie),(size),0,1)
  353. #define ioremap_nocache(cookie,size) __arch_ioremap((cookie),(size),0,1)
  354. #define iounmap(cookie) __arch_iounmap(cookie)
  355. #endif
  356. /*
  357. * DMA-consistent mapping functions. These allocate/free a region of
  358. * uncached, unwrite-buffered mapped memory space for use with DMA
  359. * devices. This is the "generic" version. The PCI specific version
  360. * is in pci.h
  361. */
  362. extern void *consistent_alloc(int gfp, size_t size, dma_addr_t *handle);
  363. extern void consistent_free(void *vaddr, size_t size, dma_addr_t handle);
  364. extern void consistent_sync(void *vaddr, size_t size, int rw);
  365. /*
  366. * can the hardware map this into one segment or not, given no other
  367. * constraints.
  368. */
  369. #define BIOVEC_MERGEABLE(vec1, vec2) \
  370. ((bvec_to_phys((vec1)) + (vec1)->bv_len) == bvec_to_phys((vec2)))
  371. /*
  372. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  373. * access
  374. */
  375. #define xlate_dev_mem_ptr(p) __va(p)
  376. /*
  377. * Convert a virtual cached pointer to an uncached pointer
  378. */
  379. #define xlate_dev_kmem_ptr(p) p
  380. #endif /* __KERNEL__ */
  381. #endif /* __ASM_ARM_IO_H */