io.h 19 KB

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