io.h 15 KB

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