io.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 phys_addr_t virt_to_phys(volatile void * address)
  108. {
  109. #ifndef CONFIG_64BIT
  110. return CPHYSADDR(address);
  111. #else
  112. return XPHYSADDR(address);
  113. #endif
  114. }
  115. extern inline void * phys_to_virt(unsigned long address)
  116. {
  117. #ifndef CONFIG_64BIT
  118. return (void *)KSEG0ADDR(address);
  119. #else
  120. return (void *)CKSEG0ADDR(address);
  121. #endif
  122. }
  123. /*
  124. * IO bus memory addresses are also 1:1 with the physical address
  125. */
  126. extern inline unsigned long virt_to_bus(volatile void * address)
  127. {
  128. #ifndef CONFIG_64BIT
  129. return CPHYSADDR(address);
  130. #else
  131. return XPHYSADDR(address);
  132. #endif
  133. }
  134. extern inline void * bus_to_virt(unsigned long address)
  135. {
  136. #ifndef CONFIG_64BIT
  137. return (void *)KSEG0ADDR(address);
  138. #else
  139. return (void *)CKSEG0ADDR(address);
  140. #endif
  141. }
  142. /*
  143. * isa_slot_offset is the address where E(ISA) busaddress 0 is mapped
  144. * for the processor.
  145. */
  146. extern unsigned long isa_slot_offset;
  147. extern void * __ioremap(unsigned long offset, unsigned long size, unsigned long flags);
  148. #if 0
  149. extern inline void *ioremap(unsigned long offset, unsigned long size)
  150. {
  151. return __ioremap(offset, size, _CACHE_UNCACHED);
  152. }
  153. extern inline void *ioremap_nocache(unsigned long offset, unsigned long size)
  154. {
  155. return __ioremap(offset, size, _CACHE_UNCACHED);
  156. }
  157. extern void iounmap(void *addr);
  158. #endif
  159. /*
  160. * XXX We need system specific versions of these to handle EISA address bits
  161. * 24-31 on SNI.
  162. * XXX more SNI hacks.
  163. */
  164. #define __raw_readb(addr) (*(volatile unsigned char *)(addr))
  165. #define __raw_readw(addr) (*(volatile unsigned short *)(addr))
  166. #define __raw_readl(addr) (*(volatile unsigned int *)(addr))
  167. #define readb(addr) __raw_readb((addr))
  168. #define readw(addr) __ioswab16(__raw_readw((addr)))
  169. #define readl(addr) __ioswab32(__raw_readl((addr)))
  170. #define __raw_writeb(b, addr) (*(volatile unsigned char *)(addr)) = (b)
  171. #define __raw_writew(b, addr) (*(volatile unsigned short *)(addr)) = (b)
  172. #define __raw_writel(b, addr) (*(volatile unsigned int *)(addr)) = (b)
  173. #define writeb(b, addr) __raw_writeb((b), (addr))
  174. #define writew(b, addr) __raw_writew(__ioswab16(b), (addr))
  175. #define writel(b, addr) __raw_writel(__ioswab32(b), (addr))
  176. #define memset_io(a,b,c) memset((void *)(a),(b),(c))
  177. #define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
  178. #define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
  179. /* END SNI HACKS ... */
  180. /*
  181. * ISA space is 'always mapped' on currently supported MIPS systems, no need
  182. * to explicitly ioremap() it. The fact that the ISA IO space is mapped
  183. * to PAGE_OFFSET is pure coincidence - it does not mean ISA values
  184. * are physical addresses. The following constant pointer can be
  185. * used as the IO-area pointer (it can be iounmapped as well, so the
  186. * analogy with PCI is quite large):
  187. */
  188. #define __ISA_IO_base ((char *)(PAGE_OFFSET))
  189. #define isa_readb(a) readb(a)
  190. #define isa_readw(a) readw(a)
  191. #define isa_readl(a) readl(a)
  192. #define isa_writeb(b,a) writeb(b,a)
  193. #define isa_writew(w,a) writew(w,a)
  194. #define isa_writel(l,a) writel(l,a)
  195. #define isa_memset_io(a,b,c) memset_io((a),(b),(c))
  196. #define isa_memcpy_fromio(a,b,c) memcpy_fromio((a),(b),(c))
  197. #define isa_memcpy_toio(a,b,c) memcpy_toio((a),(b),(c))
  198. /*
  199. * We don't have csum_partial_copy_fromio() yet, so we cheat here and
  200. * just copy it. The net code will then do the checksum later.
  201. */
  202. #define eth_io_copy_and_sum(skb,src,len,unused) memcpy_fromio((skb)->data,(src),(len))
  203. #define isa_eth_io_copy_and_sum(a,b,c,d) eth_copy_and_sum((a),(b),(c),(d))
  204. static inline int check_signature(unsigned long io_addr,
  205. const unsigned char *signature, int length)
  206. {
  207. int retval = 0;
  208. do {
  209. if (readb(io_addr) != *signature)
  210. goto out;
  211. io_addr++;
  212. signature++;
  213. length--;
  214. } while (length);
  215. retval = 1;
  216. out:
  217. return retval;
  218. }
  219. #define isa_check_signature(io, s, l) check_signature(i,s,l)
  220. /*
  221. * Talk about misusing macros..
  222. */
  223. #define __OUT1(s) \
  224. static inline void __out##s(unsigned int value, unsigned int port) {
  225. #define __OUT2(m) \
  226. __asm__ __volatile__ ("s" #m "\t%0,%1(%2)"
  227. #define __OUT(m,s,w) \
  228. __OUT1(s) __OUT2(m) : : "r" (__ioswab##w(value)), "i" (0), "r" (mips_io_port_base+port)); } \
  229. __OUT1(s##c) __OUT2(m) : : "r" (__ioswab##w(value)), "ir" (port), "r" (mips_io_port_base)); } \
  230. __OUT1(s##_p) __OUT2(m) : : "r" (__ioswab##w(value)), "i" (0), "r" (mips_io_port_base+port)); \
  231. SLOW_DOWN_IO; } \
  232. __OUT1(s##c_p) __OUT2(m) : : "r" (__ioswab##w(value)), "ir" (port), "r" (mips_io_port_base)); \
  233. SLOW_DOWN_IO; }
  234. #define __IN1(t,s) \
  235. static inline t __in##s(unsigned int port) { t _v;
  236. /*
  237. * Required nops will be inserted by the assembler
  238. */
  239. #define __IN2(m) \
  240. __asm__ __volatile__ ("l" #m "\t%0,%1(%2)"
  241. #define __IN(t,m,s,w) \
  242. __IN1(t,s) __IN2(m) : "=r" (_v) : "i" (0), "r" (mips_io_port_base+port)); return __ioswab##w(_v); } \
  243. __IN1(t,s##c) __IN2(m) : "=r" (_v) : "ir" (port), "r" (mips_io_port_base)); return __ioswab##w(_v); } \
  244. __IN1(t,s##_p) __IN2(m) : "=r" (_v) : "i" (0), "r" (mips_io_port_base+port)); SLOW_DOWN_IO; return __ioswab##w(_v); } \
  245. __IN1(t,s##c_p) __IN2(m) : "=r" (_v) : "ir" (port), "r" (mips_io_port_base)); SLOW_DOWN_IO; return __ioswab##w(_v); }
  246. #define __INS1(s) \
  247. static inline void __ins##s(unsigned int port, void * addr, unsigned long count) {
  248. #define __INS2(m) \
  249. if (count) \
  250. __asm__ __volatile__ ( \
  251. ".set\tnoreorder\n\t" \
  252. ".set\tnoat\n" \
  253. "1:\tl" #m "\t$1,%4(%5)\n\t" \
  254. "subu\t%1,1\n\t" \
  255. "s" #m "\t$1,(%0)\n\t" \
  256. "bne\t$0,%1,1b\n\t" \
  257. "addiu\t%0,%6\n\t" \
  258. ".set\tat\n\t" \
  259. ".set\treorder"
  260. #define __INS(m,s,i) \
  261. __INS1(s) __INS2(m) \
  262. : "=r" (addr), "=r" (count) \
  263. : "0" (addr), "1" (count), "i" (0), \
  264. "r" (mips_io_port_base+port), "I" (i) \
  265. : "$1");} \
  266. __INS1(s##c) __INS2(m) \
  267. : "=r" (addr), "=r" (count) \
  268. : "0" (addr), "1" (count), "ir" (port), \
  269. "r" (mips_io_port_base), "I" (i) \
  270. : "$1");}
  271. #define __OUTS1(s) \
  272. static inline void __outs##s(unsigned int port, const void * addr, unsigned long count) {
  273. #define __OUTS2(m) \
  274. if (count) \
  275. __asm__ __volatile__ ( \
  276. ".set\tnoreorder\n\t" \
  277. ".set\tnoat\n" \
  278. "1:\tl" #m "\t$1,(%0)\n\t" \
  279. "subu\t%1,1\n\t" \
  280. "s" #m "\t$1,%4(%5)\n\t" \
  281. "bne\t$0,%1,1b\n\t" \
  282. "addiu\t%0,%6\n\t" \
  283. ".set\tat\n\t" \
  284. ".set\treorder"
  285. #define __OUTS(m,s,i) \
  286. __OUTS1(s) __OUTS2(m) \
  287. : "=r" (addr), "=r" (count) \
  288. : "0" (addr), "1" (count), "i" (0), "r" (mips_io_port_base+port), "I" (i) \
  289. : "$1");} \
  290. __OUTS1(s##c) __OUTS2(m) \
  291. : "=r" (addr), "=r" (count) \
  292. : "0" (addr), "1" (count), "ir" (port), "r" (mips_io_port_base), "I" (i) \
  293. : "$1");}
  294. __IN(unsigned char,b,b,8)
  295. __IN(unsigned short,h,w,16)
  296. __IN(unsigned int,w,l,32)
  297. __OUT(b,b,8)
  298. __OUT(h,w,16)
  299. __OUT(w,l,32)
  300. __INS(b,b,1)
  301. __INS(h,w,2)
  302. __INS(w,l,4)
  303. __OUTS(b,b,1)
  304. __OUTS(h,w,2)
  305. __OUTS(w,l,4)
  306. /*
  307. * Note that due to the way __builtin_constant_p() works, you
  308. * - can't use it inside an inline function (it will never be true)
  309. * - you don't have to worry about side effects within the __builtin..
  310. */
  311. #define outb(val,port) \
  312. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  313. __outbc((val),(port)) : \
  314. __outb((val),(port)))
  315. #define inb(port) \
  316. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  317. __inbc(port) : \
  318. __inb(port))
  319. #define outb_p(val,port) \
  320. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  321. __outbc_p((val),(port)) : \
  322. __outb_p((val),(port)))
  323. #define inb_p(port) \
  324. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  325. __inbc_p(port) : \
  326. __inb_p(port))
  327. #define outw(val,port) \
  328. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  329. __outwc((val),(port)) : \
  330. __outw((val),(port)))
  331. #define inw(port) \
  332. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  333. __inwc(port) : \
  334. __inw(port))
  335. #define outw_p(val,port) \
  336. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  337. __outwc_p((val),(port)) : \
  338. __outw_p((val),(port)))
  339. #define inw_p(port) \
  340. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  341. __inwc_p(port) : \
  342. __inw_p(port))
  343. #define outl(val,port) \
  344. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  345. __outlc((val),(port)) : \
  346. __outl((val),(port)))
  347. #define inl(port) \
  348. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  349. __inlc(port) : \
  350. __inl(port))
  351. #define outl_p(val,port) \
  352. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  353. __outlc_p((val),(port)) : \
  354. __outl_p((val),(port)))
  355. #define inl_p(port) \
  356. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  357. __inlc_p(port) : \
  358. __inl_p(port))
  359. #define outsb(port,addr,count) \
  360. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  361. __outsbc((port),(addr),(count)) : \
  362. __outsb ((port),(addr),(count)))
  363. #define insb(port,addr,count) \
  364. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  365. __insbc((port),(addr),(count)) : \
  366. __insb((port),(addr),(count)))
  367. #define outsw(port,addr,count) \
  368. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  369. __outswc((port),(addr),(count)) : \
  370. __outsw ((port),(addr),(count)))
  371. #define insw(port,addr,count) \
  372. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  373. __inswc((port),(addr),(count)) : \
  374. __insw((port),(addr),(count)))
  375. #define outsl(port,addr,count) \
  376. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  377. __outslc((port),(addr),(count)) : \
  378. __outsl ((port),(addr),(count)))
  379. #define insl(port,addr,count) \
  380. ((__builtin_constant_p((port)) && (port) < 32768) ? \
  381. __inslc((port),(addr),(count)) : \
  382. __insl((port),(addr),(count)))
  383. #define IO_SPACE_LIMIT 0xffff
  384. /*
  385. * The caches on some architectures aren't dma-coherent and have need to
  386. * handle this in software. There are three types of operations that
  387. * can be applied to dma buffers.
  388. *
  389. * - dma_cache_wback_inv(start, size) makes caches and coherent by
  390. * writing the content of the caches back to memory, if necessary.
  391. * The function also invalidates the affected part of the caches as
  392. * necessary before DMA transfers from outside to memory.
  393. * - dma_cache_wback(start, size) makes caches and coherent by
  394. * writing the content of the caches back to memory, if necessary.
  395. * The function also invalidates the affected part of the caches as
  396. * necessary before DMA transfers from outside to memory.
  397. * - dma_cache_inv(start, size) invalidates the affected parts of the
  398. * caches. Dirty lines of the caches may be written back or simply
  399. * be discarded. This operation is necessary before dma operations
  400. * to the memory.
  401. */
  402. extern void (*_dma_cache_wback_inv)(unsigned long start, unsigned long size);
  403. extern void (*_dma_cache_wback)(unsigned long start, unsigned long size);
  404. extern void (*_dma_cache_inv)(unsigned long start, unsigned long size);
  405. #define dma_cache_wback_inv(start,size) _dma_cache_wback_inv(start,size)
  406. #define dma_cache_wback(start,size) _dma_cache_wback(start,size)
  407. #define dma_cache_inv(start,size) _dma_cache_inv(start,size)
  408. static inline void sync(void)
  409. {
  410. }
  411. /*
  412. * Given a physical address and a length, return a virtual address
  413. * that can be used to access the memory range with the caching
  414. * properties specified by "flags".
  415. */
  416. #define MAP_NOCACHE (0)
  417. #define MAP_WRCOMBINE (0)
  418. #define MAP_WRBACK (0)
  419. #define MAP_WRTHROUGH (0)
  420. static inline void *
  421. map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
  422. {
  423. return (void *)paddr;
  424. }
  425. /*
  426. * Take down a mapping set up by map_physmem().
  427. */
  428. static inline void unmap_physmem(void *vaddr, unsigned long flags)
  429. {
  430. }
  431. #endif /* _ASM_IO_H */