io.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 const unsigned long mips_io_port_base;
  66. /*
  67. * Gcc will generate code to load the value of mips_io_port_base after each
  68. * function call which may be fairly wasteful in some cases. So we don't
  69. * play quite by the book. We tell gcc mips_io_port_base is a long variable
  70. * which solves the code generation issue. Now we need to violate the
  71. * aliasing rules a little to make initialization possible and finally we
  72. * will need the barrier() to fight side effects of the aliasing chat.
  73. * This trickery will eventually collapse under gcc's optimizer. Oh well.
  74. */
  75. static inline void set_io_port_base(unsigned long base)
  76. {
  77. * (unsigned long *) &mips_io_port_base = base;
  78. }
  79. /*
  80. * Thanks to James van Artsdalen for a better timing-fix than
  81. * the two short jumps: using outb's to a nonexistent port seems
  82. * to guarantee better timings even on fast machines.
  83. *
  84. * On the other hand, I'd like to be sure of a non-existent port:
  85. * I feel a bit unsafe about using 0x80 (should be safe, though)
  86. *
  87. * Linus
  88. *
  89. */
  90. #define __SLOW_DOWN_IO \
  91. __asm__ __volatile__( \
  92. "sb\t$0,0x80(%0)" \
  93. : : "r" (mips_io_port_base));
  94. #ifdef CONF_SLOWDOWN_IO
  95. #ifdef REALLY_SLOW_IO
  96. #define SLOW_DOWN_IO { __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; }
  97. #else
  98. #define SLOW_DOWN_IO __SLOW_DOWN_IO
  99. #endif
  100. #else
  101. #define SLOW_DOWN_IO
  102. #endif
  103. /*
  104. * Change virtual addresses to physical addresses and vv.
  105. * These are trivial on the 1:1 Linux/MIPS mapping
  106. */
  107. extern inline unsigned long virt_to_phys(volatile void * address)
  108. {
  109. return PHYSADDR(address);
  110. }
  111. extern inline void * phys_to_virt(unsigned long address)
  112. {
  113. return (void *)KSEG0ADDR(address);
  114. }
  115. /*
  116. * IO bus memory addresses are also 1:1 with the physical address
  117. */
  118. extern inline unsigned long virt_to_bus(volatile void * address)
  119. {
  120. return PHYSADDR(address);
  121. }
  122. extern inline void * bus_to_virt(unsigned long address)
  123. {
  124. return (void *)KSEG0ADDR(address);
  125. }
  126. /*
  127. * isa_slot_offset is the address where E(ISA) busaddress 0 is mapped
  128. * for the processor.
  129. */
  130. extern unsigned long isa_slot_offset;
  131. extern void * __ioremap(unsigned long offset, unsigned long size, unsigned long flags);
  132. #if 0
  133. extern inline void *ioremap(unsigned long offset, unsigned long size)
  134. {
  135. return __ioremap(offset, size, _CACHE_UNCACHED);
  136. }
  137. extern inline void *ioremap_nocache(unsigned long offset, unsigned long size)
  138. {
  139. return __ioremap(offset, size, _CACHE_UNCACHED);
  140. }
  141. extern void iounmap(void *addr);
  142. #endif
  143. /*
  144. * XXX We need system specific versions of these to handle EISA address bits
  145. * 24-31 on SNI.
  146. * XXX more SNI hacks.
  147. */
  148. #define readb(addr) (*(volatile unsigned char *)(addr))
  149. #define readw(addr) __ioswab16((*(volatile unsigned short *)(addr)))
  150. #define readl(addr) __ioswab32((*(volatile unsigned int *)(addr)))
  151. #define __raw_readb readb
  152. #define __raw_readw readw
  153. #define __raw_readl readl
  154. #define writeb(b,addr) (*(volatile unsigned char *)(addr)) = (b)
  155. #define writew(b,addr) (*(volatile unsigned short *)(addr)) = (__ioswab16(b))
  156. #define writel(b,addr) (*(volatile unsigned int *)(addr)) = (__ioswab32(b))
  157. #define __raw_writeb writeb
  158. #define __raw_writew writew
  159. #define __raw_writel writel
  160. #define memset_io(a,b,c) memset((void *)(a),(b),(c))
  161. #define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
  162. #define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
  163. /* END SNI HACKS ... */
  164. /*
  165. * ISA space is 'always mapped' on currently supported MIPS systems, no need
  166. * to explicitly ioremap() it. The fact that the ISA IO space is mapped
  167. * to PAGE_OFFSET is pure coincidence - it does not mean ISA values
  168. * are physical addresses. The following constant pointer can be
  169. * used as the IO-area pointer (it can be iounmapped as well, so the
  170. * analogy with PCI is quite large):
  171. */
  172. #define __ISA_IO_base ((char *)(PAGE_OFFSET))
  173. #define isa_readb(a) readb(a)
  174. #define isa_readw(a) readw(a)
  175. #define isa_readl(a) readl(a)
  176. #define isa_writeb(b,a) writeb(b,a)
  177. #define isa_writew(w,a) writew(w,a)
  178. #define isa_writel(l,a) writel(l,a)
  179. #define isa_memset_io(a,b,c) memset_io((a),(b),(c))
  180. #define isa_memcpy_fromio(a,b,c) memcpy_fromio((a),(b),(c))
  181. #define isa_memcpy_toio(a,b,c) memcpy_toio((a),(b),(c))
  182. /*
  183. * We don't have csum_partial_copy_fromio() yet, so we cheat here and
  184. * just copy it. The net code will then do the checksum later.
  185. */
  186. #define eth_io_copy_and_sum(skb,src,len,unused) memcpy_fromio((skb)->data,(src),(len))
  187. #define isa_eth_io_copy_and_sum(a,b,c,d) eth_copy_and_sum((a),(b),(c),(d))
  188. static inline int check_signature(unsigned long io_addr,
  189. const unsigned char *signature, int length)
  190. {
  191. int retval = 0;
  192. do {
  193. if (readb(io_addr) != *signature)
  194. goto out;
  195. io_addr++;
  196. signature++;
  197. length--;
  198. } while (length);
  199. retval = 1;
  200. out:
  201. return retval;
  202. }
  203. #define isa_check_signature(io, s, l) check_signature(i,s,l)
  204. /*
  205. * Talk about misusing macros..
  206. */
  207. #define __OUT1(s) \
  208. extern inline void __out##s(unsigned int value, unsigned int port) {
  209. #define __OUT2(m) \
  210. __asm__ __volatile__ ("s" #m "\t%0,%1(%2)"
  211. #define __OUT(m,s,w) \
  212. __OUT1(s) __OUT2(m) : : "r" (__ioswab##w(value)), "i" (0), "r" (mips_io_port_base+port)); } \
  213. __OUT1(s##c) __OUT2(m) : : "r" (__ioswab##w(value)), "ir" (port), "r" (mips_io_port_base)); } \
  214. __OUT1(s##_p) __OUT2(m) : : "r" (__ioswab##w(value)), "i" (0), "r" (mips_io_port_base+port)); \
  215. SLOW_DOWN_IO; } \
  216. __OUT1(s##c_p) __OUT2(m) : : "r" (__ioswab##w(value)), "ir" (port), "r" (mips_io_port_base)); \
  217. SLOW_DOWN_IO; }
  218. #define __IN1(t,s) \
  219. extern __inline__ t __in##s(unsigned int port) { t _v;
  220. /*
  221. * Required nops will be inserted by the assembler
  222. */
  223. #define __IN2(m) \
  224. __asm__ __volatile__ ("l" #m "\t%0,%1(%2)"
  225. #define __IN(t,m,s,w) \
  226. __IN1(t,s) __IN2(m) : "=r" (_v) : "i" (0), "r" (mips_io_port_base+port)); return __ioswab##w(_v); } \
  227. __IN1(t,s##c) __IN2(m) : "=r" (_v) : "ir" (port), "r" (mips_io_port_base)); return __ioswab##w(_v); } \
  228. __IN1(t,s##_p) __IN2(m) : "=r" (_v) : "i" (0), "r" (mips_io_port_base+port)); SLOW_DOWN_IO; return __ioswab##w(_v); } \
  229. __IN1(t,s##c_p) __IN2(m) : "=r" (_v) : "ir" (port), "r" (mips_io_port_base)); SLOW_DOWN_IO; return __ioswab##w(_v); }
  230. #define __INS1(s) \
  231. extern inline void __ins##s(unsigned int port, void * addr, unsigned long count) {
  232. #define __INS2(m) \
  233. if (count) \
  234. __asm__ __volatile__ ( \
  235. ".set\tnoreorder\n\t" \
  236. ".set\tnoat\n" \
  237. "1:\tl" #m "\t$1,%4(%5)\n\t" \
  238. "subu\t%1,1\n\t" \
  239. "s" #m "\t$1,(%0)\n\t" \
  240. "bne\t$0,%1,1b\n\t" \
  241. "addiu\t%0,%6\n\t" \
  242. ".set\tat\n\t" \
  243. ".set\treorder"
  244. #define __INS(m,s,i) \
  245. __INS1(s) __INS2(m) \
  246. : "=r" (addr), "=r" (count) \
  247. : "0" (addr), "1" (count), "i" (0), \
  248. "r" (mips_io_port_base+port), "I" (i) \
  249. : "$1");} \
  250. __INS1(s##c) __INS2(m) \
  251. : "=r" (addr), "=r" (count) \
  252. : "0" (addr), "1" (count), "ir" (port), \
  253. "r" (mips_io_port_base), "I" (i) \
  254. : "$1");}
  255. #define __OUTS1(s) \
  256. extern inline void __outs##s(unsigned int port, const void * addr, unsigned long count) {
  257. #define __OUTS2(m) \
  258. if (count) \
  259. __asm__ __volatile__ ( \
  260. ".set\tnoreorder\n\t" \
  261. ".set\tnoat\n" \
  262. "1:\tl" #m "\t$1,(%0)\n\t" \
  263. "subu\t%1,1\n\t" \
  264. "s" #m "\t$1,%4(%5)\n\t" \
  265. "bne\t$0,%1,1b\n\t" \
  266. "addiu\t%0,%6\n\t" \
  267. ".set\tat\n\t" \
  268. ".set\treorder"
  269. #define __OUTS(m,s,i) \
  270. __OUTS1(s) __OUTS2(m) \
  271. : "=r" (addr), "=r" (count) \
  272. : "0" (addr), "1" (count), "i" (0), "r" (mips_io_port_base+port), "I" (i) \
  273. : "$1");} \
  274. __OUTS1(s##c) __OUTS2(m) \
  275. : "=r" (addr), "=r" (count) \
  276. : "0" (addr), "1" (count), "ir" (port), "r" (mips_io_port_base), "I" (i) \
  277. : "$1");}
  278. __IN(unsigned char,b,b,8)
  279. __IN(unsigned short,h,w,16)
  280. __IN(unsigned int,w,l,32)
  281. __OUT(b,b,8)
  282. __OUT(h,w,16)
  283. __OUT(w,l,32)
  284. __INS(b,b,1)
  285. __INS(h,w,2)
  286. __INS(w,l,4)
  287. __OUTS(b,b,1)
  288. __OUTS(h,w,2)
  289. __OUTS(w,l,4)
  290. /*
  291. * Note that due to the way __builtin_constant_p() works, you
  292. * - can't use it inside an inline function (it will never be true)
  293. * - you don't have to worry about side effects within the __builtin..
  294. */
  295. #define outb(val,port) \
  296. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  297. __outbc((val),(port)) : \
  298. __outb((val),(port)))
  299. #define inb(port) \
  300. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  301. __inbc(port) : \
  302. __inb(port))
  303. #define outb_p(val,port) \
  304. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  305. __outbc_p((val),(port)) : \
  306. __outb_p((val),(port)))
  307. #define inb_p(port) \
  308. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  309. __inbc_p(port) : \
  310. __inb_p(port))
  311. #define outw(val,port) \
  312. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  313. __outwc((val),(port)) : \
  314. __outw((val),(port)))
  315. #define inw(port) \
  316. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  317. __inwc(port) : \
  318. __inw(port))
  319. #define outw_p(val,port) \
  320. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  321. __outwc_p((val),(port)) : \
  322. __outw_p((val),(port)))
  323. #define inw_p(port) \
  324. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  325. __inwc_p(port) : \
  326. __inw_p(port))
  327. #define outl(val,port) \
  328. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  329. __outlc((val),(port)) : \
  330. __outl((val),(port)))
  331. #define inl(port) \
  332. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  333. __inlc(port) : \
  334. __inl(port))
  335. #define outl_p(val,port) \
  336. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  337. __outlc_p((val),(port)) : \
  338. __outl_p((val),(port)))
  339. #define inl_p(port) \
  340. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  341. __inlc_p(port) : \
  342. __inl_p(port))
  343. #define outsb(port,addr,count) \
  344. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  345. __outsbc((port),(addr),(count)) : \
  346. __outsb ((port),(addr),(count)))
  347. #define insb(port,addr,count) \
  348. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  349. __insbc((port),(addr),(count)) : \
  350. __insb((port),(addr),(count)))
  351. #define outsw(port,addr,count) \
  352. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  353. __outswc((port),(addr),(count)) : \
  354. __outsw ((port),(addr),(count)))
  355. #define insw(port,addr,count) \
  356. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  357. __inswc((port),(addr),(count)) : \
  358. __insw((port),(addr),(count)))
  359. #define outsl(port,addr,count) \
  360. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  361. __outslc((port),(addr),(count)) : \
  362. __outsl ((port),(addr),(count)))
  363. #define insl(port,addr,count) \
  364. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  365. __inslc((port),(addr),(count)) : \
  366. __insl((port),(addr),(count)))
  367. #define IO_SPACE_LIMIT 0xffff
  368. /*
  369. * The caches on some architectures aren't dma-coherent and have need to
  370. * handle this in software. There are three types of operations that
  371. * can be applied to dma buffers.
  372. *
  373. * - dma_cache_wback_inv(start, size) makes caches and coherent by
  374. * writing the content of the caches back to memory, if necessary.
  375. * The function also invalidates the affected part of the caches as
  376. * necessary before DMA transfers from outside to memory.
  377. * - dma_cache_wback(start, size) makes caches and coherent by
  378. * writing the content of the caches back to memory, if necessary.
  379. * The function also invalidates the affected part of the caches as
  380. * necessary before DMA transfers from outside to memory.
  381. * - dma_cache_inv(start, size) invalidates the affected parts of the
  382. * caches. Dirty lines of the caches may be written back or simply
  383. * be discarded. This operation is necessary before dma operations
  384. * to the memory.
  385. */
  386. extern void (*_dma_cache_wback_inv)(unsigned long start, unsigned long size);
  387. extern void (*_dma_cache_wback)(unsigned long start, unsigned long size);
  388. extern void (*_dma_cache_inv)(unsigned long start, unsigned long size);
  389. #define dma_cache_wback_inv(start,size) _dma_cache_wback_inv(start,size)
  390. #define dma_cache_wback(start,size) _dma_cache_wback(start,size)
  391. #define dma_cache_inv(start,size) _dma_cache_inv(start,size)
  392. static inline void sync(void)
  393. {
  394. }
  395. #endif /* _ASM_IO_H */