io.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1994, 1995 Waldorf GmbH
  7. * Copyright (C) 1994 - 2000 Ralf Baechle
  8. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  9. * Copyright (C) 2000 FSMLabs, Inc.
  10. */
  11. #ifndef _ASM_IO_H
  12. #define _ASM_IO_H
  13. #include <linux/config.h>
  14. #if 0
  15. #include <linux/pagemap.h>
  16. #endif
  17. #include <asm/addrspace.h>
  18. #include <asm/byteorder.h>
  19. /*
  20. * Slowdown I/O port space accesses for antique hardware.
  21. */
  22. #undef CONF_SLOWDOWN_IO
  23. /*
  24. * Sane hardware offers swapping of I/O space accesses in hardware; less
  25. * sane hardware forces software to fiddle with this ...
  26. */
  27. #if defined(CONFIG_SWAP_IO_SPACE) && defined(__MIPSEB__)
  28. #define __ioswab8(x) (x)
  29. #define __ioswab16(x) swab16(x)
  30. #define __ioswab32(x) swab32(x)
  31. #else
  32. #define __ioswab8(x) (x)
  33. #define __ioswab16(x) (x)
  34. #define __ioswab32(x) (x)
  35. #endif
  36. /*
  37. * This file contains the definitions for the MIPS counterpart of the
  38. * x86 in/out instructions. This heap of macros and C results in much
  39. * better code than the approach of doing it in plain C. The macros
  40. * result in code that is to fast for certain hardware. On the other
  41. * side the performance of the string functions should be improved for
  42. * sake of certain devices like EIDE disks that do highspeed polled I/O.
  43. *
  44. * Ralf
  45. *
  46. * This file contains the definitions for the x86 IO instructions
  47. * inb/inw/inl/outb/outw/outl and the "string versions" of the same
  48. * (insb/insw/insl/outsb/outsw/outsl). You can also use "pausing"
  49. * versions of the single-IO instructions (inb_p/inw_p/..).
  50. *
  51. * This file is not meant to be obfuscating: it's just complicated
  52. * to (a) handle it all in a way that makes gcc able to optimize it
  53. * as well as possible and (b) trying to avoid writing the same thing
  54. * over and over again with slight variations and possibly making a
  55. * mistake somewhere.
  56. */
  57. /*
  58. * On MIPS I/O ports are memory mapped, so we access them using normal
  59. * load/store instructions. mips_io_port_base is the virtual address to
  60. * which all ports are being mapped. For sake of efficiency some code
  61. * assumes that this is an address that can be loaded with a single lui
  62. * instruction, so the lower 16 bits must be zero. Should be true on
  63. * on any sane architecture; generic code does not use this assumption.
  64. */
  65. extern unsigned long mips_io_port_base;
  66. /*
  67. * Thanks to James van Artsdalen for a better timing-fix than
  68. * the two short jumps: using outb's to a nonexistent port seems
  69. * to guarantee better timings even on fast machines.
  70. *
  71. * On the other hand, I'd like to be sure of a non-existent port:
  72. * I feel a bit unsafe about using 0x80 (should be safe, though)
  73. *
  74. * Linus
  75. *
  76. */
  77. #define __SLOW_DOWN_IO \
  78. __asm__ __volatile__( \
  79. "sb\t$0,0x80(%0)" \
  80. : : "r" (mips_io_port_base));
  81. #ifdef CONF_SLOWDOWN_IO
  82. #ifdef REALLY_SLOW_IO
  83. #define SLOW_DOWN_IO { __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; }
  84. #else
  85. #define SLOW_DOWN_IO __SLOW_DOWN_IO
  86. #endif
  87. #else
  88. #define SLOW_DOWN_IO
  89. #endif
  90. /*
  91. * Change virtual addresses to physical addresses and vv.
  92. * These are trivial on the 1:1 Linux/MIPS mapping
  93. */
  94. extern inline unsigned long virt_to_phys(volatile void * address)
  95. {
  96. return PHYSADDR(address);
  97. }
  98. extern inline void * phys_to_virt(unsigned long address)
  99. {
  100. return (void *)KSEG0ADDR(address);
  101. }
  102. /*
  103. * IO bus memory addresses are also 1:1 with the physical address
  104. */
  105. extern inline unsigned long virt_to_bus(volatile void * address)
  106. {
  107. return PHYSADDR(address);
  108. }
  109. extern inline void * bus_to_virt(unsigned long address)
  110. {
  111. return (void *)KSEG0ADDR(address);
  112. }
  113. /*
  114. * isa_slot_offset is the address where E(ISA) busaddress 0 is mapped
  115. * for the processor.
  116. */
  117. extern unsigned long isa_slot_offset;
  118. extern void * __ioremap(unsigned long offset, unsigned long size, unsigned long flags);
  119. #if 0
  120. extern inline void *ioremap(unsigned long offset, unsigned long size)
  121. {
  122. return __ioremap(offset, size, _CACHE_UNCACHED);
  123. }
  124. extern inline void *ioremap_nocache(unsigned long offset, unsigned long size)
  125. {
  126. return __ioremap(offset, size, _CACHE_UNCACHED);
  127. }
  128. extern void iounmap(void *addr);
  129. #endif
  130. /*
  131. * XXX We need system specific versions of these to handle EISA address bits
  132. * 24-31 on SNI.
  133. * XXX more SNI hacks.
  134. */
  135. #define readb(addr) (*(volatile unsigned char *)(addr))
  136. #define readw(addr) __ioswab16((*(volatile unsigned short *)(addr)))
  137. #define readl(addr) __ioswab32((*(volatile unsigned int *)(addr)))
  138. #define __raw_readb readb
  139. #define __raw_readw readw
  140. #define __raw_readl readl
  141. #define writeb(b,addr) (*(volatile unsigned char *)(addr)) = (b)
  142. #define writew(b,addr) (*(volatile unsigned short *)(addr)) = (__ioswab16(b))
  143. #define writel(b,addr) (*(volatile unsigned int *)(addr)) = (__ioswab32(b))
  144. #define __raw_writeb writeb
  145. #define __raw_writew writew
  146. #define __raw_writel writel
  147. #define memset_io(a,b,c) memset((void *)(a),(b),(c))
  148. #define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
  149. #define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
  150. /* END SNI HACKS ... */
  151. /*
  152. * ISA space is 'always mapped' on currently supported MIPS systems, no need
  153. * to explicitly ioremap() it. The fact that the ISA IO space is mapped
  154. * to PAGE_OFFSET is pure coincidence - it does not mean ISA values
  155. * are physical addresses. The following constant pointer can be
  156. * used as the IO-area pointer (it can be iounmapped as well, so the
  157. * analogy with PCI is quite large):
  158. */
  159. #define __ISA_IO_base ((char *)(PAGE_OFFSET))
  160. #define isa_readb(a) readb(a)
  161. #define isa_readw(a) readw(a)
  162. #define isa_readl(a) readl(a)
  163. #define isa_writeb(b,a) writeb(b,a)
  164. #define isa_writew(w,a) writew(w,a)
  165. #define isa_writel(l,a) writel(l,a)
  166. #define isa_memset_io(a,b,c) memset_io((a),(b),(c))
  167. #define isa_memcpy_fromio(a,b,c) memcpy_fromio((a),(b),(c))
  168. #define isa_memcpy_toio(a,b,c) memcpy_toio((a),(b),(c))
  169. /*
  170. * We don't have csum_partial_copy_fromio() yet, so we cheat here and
  171. * just copy it. The net code will then do the checksum later.
  172. */
  173. #define eth_io_copy_and_sum(skb,src,len,unused) memcpy_fromio((skb)->data,(src),(len))
  174. #define isa_eth_io_copy_and_sum(a,b,c,d) eth_copy_and_sum((a),(b),(c),(d))
  175. static inline int check_signature(unsigned long io_addr,
  176. const unsigned char *signature, int length)
  177. {
  178. int retval = 0;
  179. do {
  180. if (readb(io_addr) != *signature)
  181. goto out;
  182. io_addr++;
  183. signature++;
  184. length--;
  185. } while (length);
  186. retval = 1;
  187. out:
  188. return retval;
  189. }
  190. #define isa_check_signature(io, s, l) check_signature(i,s,l)
  191. /*
  192. * Talk about misusing macros..
  193. */
  194. #define __OUT1(s) \
  195. extern inline void __out##s(unsigned int value, unsigned int port) {
  196. #define __OUT2(m) \
  197. __asm__ __volatile__ ("s" #m "\t%0,%1(%2)"
  198. #define __OUT(m,s,w) \
  199. __OUT1(s) __OUT2(m) : : "r" (__ioswab##w(value)), "i" (0), "r" (mips_io_port_base+port)); } \
  200. __OUT1(s##c) __OUT2(m) : : "r" (__ioswab##w(value)), "ir" (port), "r" (mips_io_port_base)); } \
  201. __OUT1(s##_p) __OUT2(m) : : "r" (__ioswab##w(value)), "i" (0), "r" (mips_io_port_base+port)); \
  202. SLOW_DOWN_IO; } \
  203. __OUT1(s##c_p) __OUT2(m) : : "r" (__ioswab##w(value)), "ir" (port), "r" (mips_io_port_base)); \
  204. SLOW_DOWN_IO; }
  205. #define __IN1(t,s) \
  206. extern __inline__ t __in##s(unsigned int port) { t _v;
  207. /*
  208. * Required nops will be inserted by the assembler
  209. */
  210. #define __IN2(m) \
  211. __asm__ __volatile__ ("l" #m "\t%0,%1(%2)"
  212. #define __IN(t,m,s,w) \
  213. __IN1(t,s) __IN2(m) : "=r" (_v) : "i" (0), "r" (mips_io_port_base+port)); return __ioswab##w(_v); } \
  214. __IN1(t,s##c) __IN2(m) : "=r" (_v) : "ir" (port), "r" (mips_io_port_base)); return __ioswab##w(_v); } \
  215. __IN1(t,s##_p) __IN2(m) : "=r" (_v) : "i" (0), "r" (mips_io_port_base+port)); SLOW_DOWN_IO; return __ioswab##w(_v); } \
  216. __IN1(t,s##c_p) __IN2(m) : "=r" (_v) : "ir" (port), "r" (mips_io_port_base)); SLOW_DOWN_IO; return __ioswab##w(_v); }
  217. #define __INS1(s) \
  218. extern inline void __ins##s(unsigned int port, void * addr, unsigned long count) {
  219. #define __INS2(m) \
  220. if (count) \
  221. __asm__ __volatile__ ( \
  222. ".set\tnoreorder\n\t" \
  223. ".set\tnoat\n" \
  224. "1:\tl" #m "\t$1,%4(%5)\n\t" \
  225. "subu\t%1,1\n\t" \
  226. "s" #m "\t$1,(%0)\n\t" \
  227. "bne\t$0,%1,1b\n\t" \
  228. "addiu\t%0,%6\n\t" \
  229. ".set\tat\n\t" \
  230. ".set\treorder"
  231. #define __INS(m,s,i) \
  232. __INS1(s) __INS2(m) \
  233. : "=r" (addr), "=r" (count) \
  234. : "0" (addr), "1" (count), "i" (0), \
  235. "r" (mips_io_port_base+port), "I" (i) \
  236. : "$1");} \
  237. __INS1(s##c) __INS2(m) \
  238. : "=r" (addr), "=r" (count) \
  239. : "0" (addr), "1" (count), "ir" (port), \
  240. "r" (mips_io_port_base), "I" (i) \
  241. : "$1");}
  242. #define __OUTS1(s) \
  243. extern inline void __outs##s(unsigned int port, const void * addr, unsigned long count) {
  244. #define __OUTS2(m) \
  245. if (count) \
  246. __asm__ __volatile__ ( \
  247. ".set\tnoreorder\n\t" \
  248. ".set\tnoat\n" \
  249. "1:\tl" #m "\t$1,(%0)\n\t" \
  250. "subu\t%1,1\n\t" \
  251. "s" #m "\t$1,%4(%5)\n\t" \
  252. "bne\t$0,%1,1b\n\t" \
  253. "addiu\t%0,%6\n\t" \
  254. ".set\tat\n\t" \
  255. ".set\treorder"
  256. #define __OUTS(m,s,i) \
  257. __OUTS1(s) __OUTS2(m) \
  258. : "=r" (addr), "=r" (count) \
  259. : "0" (addr), "1" (count), "i" (0), "r" (mips_io_port_base+port), "I" (i) \
  260. : "$1");} \
  261. __OUTS1(s##c) __OUTS2(m) \
  262. : "=r" (addr), "=r" (count) \
  263. : "0" (addr), "1" (count), "ir" (port), "r" (mips_io_port_base), "I" (i) \
  264. : "$1");}
  265. __IN(unsigned char,b,b,8)
  266. __IN(unsigned short,h,w,16)
  267. __IN(unsigned int,w,l,32)
  268. __OUT(b,b,8)
  269. __OUT(h,w,16)
  270. __OUT(w,l,32)
  271. __INS(b,b,1)
  272. __INS(h,w,2)
  273. __INS(w,l,4)
  274. __OUTS(b,b,1)
  275. __OUTS(h,w,2)
  276. __OUTS(w,l,4)
  277. /*
  278. * Note that due to the way __builtin_constant_p() works, you
  279. * - can't use it inside an inline function (it will never be true)
  280. * - you don't have to worry about side effects within the __builtin..
  281. */
  282. #define outb(val,port) \
  283. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  284. __outbc((val),(port)) : \
  285. __outb((val),(port)))
  286. #define inb(port) \
  287. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  288. __inbc(port) : \
  289. __inb(port))
  290. #define outb_p(val,port) \
  291. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  292. __outbc_p((val),(port)) : \
  293. __outb_p((val),(port)))
  294. #define inb_p(port) \
  295. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  296. __inbc_p(port) : \
  297. __inb_p(port))
  298. #define outw(val,port) \
  299. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  300. __outwc((val),(port)) : \
  301. __outw((val),(port)))
  302. #define inw(port) \
  303. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  304. __inwc(port) : \
  305. __inw(port))
  306. #define outw_p(val,port) \
  307. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  308. __outwc_p((val),(port)) : \
  309. __outw_p((val),(port)))
  310. #define inw_p(port) \
  311. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  312. __inwc_p(port) : \
  313. __inw_p(port))
  314. #define outl(val,port) \
  315. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  316. __outlc((val),(port)) : \
  317. __outl((val),(port)))
  318. #define inl(port) \
  319. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  320. __inlc(port) : \
  321. __inl(port))
  322. #define outl_p(val,port) \
  323. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  324. __outlc_p((val),(port)) : \
  325. __outl_p((val),(port)))
  326. #define inl_p(port) \
  327. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  328. __inlc_p(port) : \
  329. __inl_p(port))
  330. #define outsb(port,addr,count) \
  331. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  332. __outsbc((port),(addr),(count)) : \
  333. __outsb ((port),(addr),(count)))
  334. #define insb(port,addr,count) \
  335. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  336. __insbc((port),(addr),(count)) : \
  337. __insb((port),(addr),(count)))
  338. #define outsw(port,addr,count) \
  339. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  340. __outswc((port),(addr),(count)) : \
  341. __outsw ((port),(addr),(count)))
  342. #define insw(port,addr,count) \
  343. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  344. __inswc((port),(addr),(count)) : \
  345. __insw((port),(addr),(count)))
  346. #define outsl(port,addr,count) \
  347. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  348. __outslc((port),(addr),(count)) : \
  349. __outsl ((port),(addr),(count)))
  350. #define insl(port,addr,count) \
  351. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  352. __inslc((port),(addr),(count)) : \
  353. __insl((port),(addr),(count)))
  354. #define IO_SPACE_LIMIT 0xffff
  355. /*
  356. * The caches on some architectures aren't dma-coherent and have need to
  357. * handle this in software. There are three types of operations that
  358. * can be applied to dma buffers.
  359. *
  360. * - dma_cache_wback_inv(start, size) makes caches and coherent by
  361. * writing the content of the caches back to memory, if necessary.
  362. * The function also invalidates the affected part of the caches as
  363. * necessary before DMA transfers from outside to memory.
  364. * - dma_cache_wback(start, size) makes caches and coherent by
  365. * writing the content of the caches back to memory, if necessary.
  366. * The function also invalidates the affected part of the caches as
  367. * necessary before DMA transfers from outside to memory.
  368. * - dma_cache_inv(start, size) invalidates the affected parts of the
  369. * caches. Dirty lines of the caches may be written back or simply
  370. * be discarded. This operation is necessary before dma operations
  371. * to the memory.
  372. */
  373. extern void (*_dma_cache_wback_inv)(unsigned long start, unsigned long size);
  374. extern void (*_dma_cache_wback)(unsigned long start, unsigned long size);
  375. extern void (*_dma_cache_inv)(unsigned long start, unsigned long size);
  376. #define dma_cache_wback_inv(start,size) _dma_cache_wback_inv(start,size)
  377. #define dma_cache_wback(start,size) _dma_cache_wback(start,size)
  378. #define dma_cache_inv(start,size) _dma_cache_inv(start,size)
  379. #endif /* _ASM_IO_H */