io.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #ifdef __KERNEL__
  2. #ifndef _PPC_IO_H
  3. #define _PPC_IO_H
  4. #include <linux/string.h>
  5. #include <linux/types.h>
  6. #include <asm/page.h>
  7. #include <asm/byteorder.h>
  8. #include <asm/synch.h>
  9. #include <asm/mmu.h>
  10. #define SIO_CONFIG_RA 0x398
  11. #define SIO_CONFIG_RD 0x399
  12. #define SLOW_DOWN_IO
  13. #define PMAC_ISA_MEM_BASE 0
  14. #define PMAC_PCI_DRAM_OFFSET 0
  15. #define CHRP_ISA_IO_BASE 0xf8000000
  16. #define CHRP_ISA_MEM_BASE 0xf7000000
  17. #define CHRP_PCI_DRAM_OFFSET 0
  18. #define PREP_ISA_IO_BASE 0x80000000
  19. #define PREP_ISA_MEM_BASE 0xc0000000
  20. #define PREP_PCI_DRAM_OFFSET 0x80000000
  21. #if defined(CONFIG_4xx)
  22. #include <asm/ibm4xx.h>
  23. #elif defined(CONFIG_8xx)
  24. #include <asm/mpc8xx.h>
  25. #elif defined(CONFIG_8260)
  26. #include <asm/mpc8260.h>
  27. #elif !defined(CONFIG_PCI)
  28. #define _IO_BASE 0
  29. #define _ISA_MEM_BASE 0
  30. #define PCI_DRAM_OFFSET 0
  31. #else /* Everyone else */
  32. #define _IO_BASE isa_io_base
  33. #define _ISA_MEM_BASE isa_mem_base
  34. #define PCI_DRAM_OFFSET pci_dram_offset
  35. #endif /* Platform-dependent I/O */
  36. #define ___IO_BASE ((void __iomem *)_IO_BASE)
  37. extern unsigned long isa_io_base;
  38. extern unsigned long isa_mem_base;
  39. extern unsigned long pci_dram_offset;
  40. /*
  41. * 8, 16 and 32 bit, big and little endian I/O operations, with barrier.
  42. *
  43. * Read operations have additional twi & isync to make sure the read
  44. * is actually performed (i.e. the data has come back) before we start
  45. * executing any following instructions.
  46. */
  47. extern inline int in_8(const volatile unsigned char __iomem *addr)
  48. {
  49. int ret;
  50. __asm__ __volatile__(
  51. "sync; lbz%U1%X1 %0,%1;\n"
  52. "twi 0,%0,0;\n"
  53. "isync" : "=r" (ret) : "m" (*addr));
  54. return ret;
  55. }
  56. extern inline void out_8(volatile unsigned char __iomem *addr, int val)
  57. {
  58. __asm__ __volatile__("stb%U0%X0 %1,%0; eieio" : "=m" (*addr) : "r" (val));
  59. }
  60. extern inline int in_le16(const volatile unsigned short __iomem *addr)
  61. {
  62. int ret;
  63. __asm__ __volatile__("sync; lhbrx %0,0,%1;\n"
  64. "twi 0,%0,0;\n"
  65. "isync" : "=r" (ret) :
  66. "r" (addr), "m" (*addr));
  67. return ret;
  68. }
  69. extern inline int in_be16(const volatile unsigned short __iomem *addr)
  70. {
  71. int ret;
  72. __asm__ __volatile__("sync; lhz%U1%X1 %0,%1;\n"
  73. "twi 0,%0,0;\n"
  74. "isync" : "=r" (ret) : "m" (*addr));
  75. return ret;
  76. }
  77. extern inline void out_le16(volatile unsigned short __iomem *addr, int val)
  78. {
  79. __asm__ __volatile__("sync; sthbrx %1,0,%2" : "=m" (*addr) :
  80. "r" (val), "r" (addr));
  81. }
  82. extern inline void out_be16(volatile unsigned short __iomem *addr, int val)
  83. {
  84. __asm__ __volatile__("sync; sth%U0%X0 %1,%0" : "=m" (*addr) : "r" (val));
  85. }
  86. extern inline unsigned in_le32(const volatile unsigned __iomem *addr)
  87. {
  88. unsigned ret;
  89. __asm__ __volatile__("sync; lwbrx %0,0,%1;\n"
  90. "twi 0,%0,0;\n"
  91. "isync" : "=r" (ret) :
  92. "r" (addr), "m" (*addr));
  93. return ret;
  94. }
  95. extern inline unsigned in_be32(const volatile unsigned __iomem *addr)
  96. {
  97. unsigned ret;
  98. __asm__ __volatile__("sync; lwz%U1%X1 %0,%1;\n"
  99. "twi 0,%0,0;\n"
  100. "isync" : "=r" (ret) : "m" (*addr));
  101. return ret;
  102. }
  103. extern inline void out_le32(volatile unsigned __iomem *addr, int val)
  104. {
  105. __asm__ __volatile__("sync; stwbrx %1,0,%2" : "=m" (*addr) :
  106. "r" (val), "r" (addr));
  107. }
  108. extern inline void out_be32(volatile unsigned __iomem *addr, int val)
  109. {
  110. __asm__ __volatile__("sync; stw%U0%X0 %1,%0" : "=m" (*addr) : "r" (val));
  111. }
  112. #if defined (CONFIG_8260_PCI9)
  113. #define readb(addr) in_8((volatile u8 *)(addr))
  114. #define writeb(b,addr) out_8((volatile u8 *)(addr), (b))
  115. #else
  116. static inline __u8 readb(const volatile void __iomem *addr)
  117. {
  118. return in_8(addr);
  119. }
  120. static inline void writeb(__u8 b, volatile void __iomem *addr)
  121. {
  122. out_8(addr, b);
  123. }
  124. #endif
  125. #if defined (CONFIG_8260_PCI9)
  126. /* Use macros if PCI9 workaround enabled */
  127. #define readw(addr) in_le16((volatile u16 *)(addr))
  128. #define readl(addr) in_le32((volatile u32 *)(addr))
  129. #define writew(b,addr) out_le16((volatile u16 *)(addr),(b))
  130. #define writel(b,addr) out_le32((volatile u32 *)(addr),(b))
  131. #else
  132. static inline __u16 readw(const volatile void __iomem *addr)
  133. {
  134. return in_le16(addr);
  135. }
  136. static inline __u32 readl(const volatile void __iomem *addr)
  137. {
  138. return in_le32(addr);
  139. }
  140. static inline void writew(__u16 b, volatile void __iomem *addr)
  141. {
  142. out_le16(addr, b);
  143. }
  144. static inline void writel(__u32 b, volatile void __iomem *addr)
  145. {
  146. out_le32(addr, b);
  147. }
  148. #endif /* CONFIG_8260_PCI9 */
  149. #define readb_relaxed(addr) readb(addr)
  150. #define readw_relaxed(addr) readw(addr)
  151. #define readl_relaxed(addr) readl(addr)
  152. static inline __u8 __raw_readb(const volatile void __iomem *addr)
  153. {
  154. return *(__force volatile __u8 *)(addr);
  155. }
  156. static inline __u16 __raw_readw(const volatile void __iomem *addr)
  157. {
  158. return *(__force volatile __u16 *)(addr);
  159. }
  160. static inline __u32 __raw_readl(const volatile void __iomem *addr)
  161. {
  162. return *(__force volatile __u32 *)(addr);
  163. }
  164. static inline void __raw_writeb(__u8 b, volatile void __iomem *addr)
  165. {
  166. *(__force volatile __u8 *)(addr) = b;
  167. }
  168. static inline void __raw_writew(__u16 b, volatile void __iomem *addr)
  169. {
  170. *(__force volatile __u16 *)(addr) = b;
  171. }
  172. static inline void __raw_writel(__u32 b, volatile void __iomem *addr)
  173. {
  174. *(__force volatile __u32 *)(addr) = b;
  175. }
  176. #define mmiowb()
  177. /*
  178. * The insw/outsw/insl/outsl macros don't do byte-swapping.
  179. * They are only used in practice for transferring buffers which
  180. * are arrays of bytes, and byte-swapping is not appropriate in
  181. * that case. - paulus
  182. */
  183. #define insb(port, buf, ns) _insb((port)+___IO_BASE, (buf), (ns))
  184. #define outsb(port, buf, ns) _outsb((port)+___IO_BASE, (buf), (ns))
  185. #define insw(port, buf, ns) _insw_ns((port)+___IO_BASE, (buf), (ns))
  186. #define outsw(port, buf, ns) _outsw_ns((port)+___IO_BASE, (buf), (ns))
  187. #define insl(port, buf, nl) _insl_ns((port)+___IO_BASE, (buf), (nl))
  188. #define outsl(port, buf, nl) _outsl_ns((port)+___IO_BASE, (buf), (nl))
  189. #define readsb(a, b, n) _insb((a), (b), (n))
  190. #define readsw(a, b, n) _insw_ns((a), (b), (n))
  191. #define readsl(a, b, n) _insl_ns((a), (b), (n))
  192. #define writesb(a, b, n) _outsb((a),(b),(n))
  193. #define writesw(a, b, n) _outsw_ns((a),(b),(n))
  194. #define writesl(a, b, n) _outsl_ns((a),(b),(n))
  195. /*
  196. * On powermacs and 8xx we will get a machine check exception
  197. * if we try to read data from a non-existent I/O port. Because
  198. * the machine check is an asynchronous exception, it isn't
  199. * well-defined which instruction SRR0 will point to when the
  200. * exception occurs.
  201. * With the sequence below (twi; isync; nop), we have found that
  202. * the machine check occurs on one of the three instructions on
  203. * all PPC implementations tested so far. The twi and isync are
  204. * needed on the 601 (in fact twi; sync works too), the isync and
  205. * nop are needed on 604[e|r], and any of twi, sync or isync will
  206. * work on 603[e], 750, 74xx.
  207. * The twi creates an explicit data dependency on the returned
  208. * value which seems to be needed to make the 601 wait for the
  209. * load to finish.
  210. */
  211. #define __do_in_asm(name, op) \
  212. extern __inline__ unsigned int name(unsigned int port) \
  213. { \
  214. unsigned int x; \
  215. __asm__ __volatile__( \
  216. "sync\n" \
  217. "0:" op " %0,0,%1\n" \
  218. "1: twi 0,%0,0\n" \
  219. "2: isync\n" \
  220. "3: nop\n" \
  221. "4:\n" \
  222. ".section .fixup,\"ax\"\n" \
  223. "5: li %0,-1\n" \
  224. " b 4b\n" \
  225. ".previous\n" \
  226. ".section __ex_table,\"a\"\n" \
  227. " .align 2\n" \
  228. " .long 0b,5b\n" \
  229. " .long 1b,5b\n" \
  230. " .long 2b,5b\n" \
  231. " .long 3b,5b\n" \
  232. ".previous" \
  233. : "=&r" (x) \
  234. : "r" (port + ___IO_BASE)); \
  235. return x; \
  236. }
  237. #define __do_out_asm(name, op) \
  238. extern __inline__ void name(unsigned int val, unsigned int port) \
  239. { \
  240. __asm__ __volatile__( \
  241. "sync\n" \
  242. "0:" op " %0,0,%1\n" \
  243. "1: sync\n" \
  244. "2:\n" \
  245. ".section __ex_table,\"a\"\n" \
  246. " .align 2\n" \
  247. " .long 0b,2b\n" \
  248. " .long 1b,2b\n" \
  249. ".previous" \
  250. : : "r" (val), "r" (port + ___IO_BASE)); \
  251. }
  252. __do_out_asm(outb, "stbx")
  253. #if defined (CONFIG_8260_PCI9)
  254. /* in asm cannot be defined if PCI9 workaround is used */
  255. #define inb(port) in_8((port)+___IO_BASE)
  256. #define inw(port) in_le16((port)+___IO_BASE)
  257. #define inl(port) in_le32((port)+___IO_BASE)
  258. __do_out_asm(outw, "sthbrx")
  259. __do_out_asm(outl, "stwbrx")
  260. #else
  261. __do_in_asm(inb, "lbzx")
  262. __do_in_asm(inw, "lhbrx")
  263. __do_in_asm(inl, "lwbrx")
  264. __do_out_asm(outw, "sthbrx")
  265. __do_out_asm(outl, "stwbrx")
  266. #endif
  267. #define inb_p(port) inb((port))
  268. #define outb_p(val, port) outb((val), (port))
  269. #define inw_p(port) inw((port))
  270. #define outw_p(val, port) outw((val), (port))
  271. #define inl_p(port) inl((port))
  272. #define outl_p(val, port) outl((val), (port))
  273. extern void _insb(const volatile u8 __iomem *addr, void *buf, long count);
  274. extern void _outsb(volatile u8 __iomem *addr,const void *buf,long count);
  275. extern void _insw_ns(const volatile u16 __iomem *addr, void *buf, long count);
  276. extern void _outsw_ns(volatile u16 __iomem *addr, const void *buf, long count);
  277. extern void _insl_ns(const volatile u32 __iomem *addr, void *buf, long count);
  278. extern void _outsl_ns(volatile u32 __iomem *addr, const void *buf, long count);
  279. #define IO_SPACE_LIMIT ~0
  280. #if defined (CONFIG_8260_PCI9)
  281. #define memset_io(a,b,c) memset((void *)(a),(b),(c))
  282. #define memcpy_fromio(a,b,c) memcpy((a),(void *)(b),(c))
  283. #define memcpy_toio(a,b,c) memcpy((void *)(a),(b),(c))
  284. #else
  285. static inline void memset_io(volatile void __iomem *addr, unsigned char val, int count)
  286. {
  287. memset((void __force *)addr, val, count);
  288. }
  289. static inline void memcpy_fromio(void *dst,const volatile void __iomem *src, int count)
  290. {
  291. memcpy(dst, (void __force *) src, count);
  292. }
  293. static inline void memcpy_toio(volatile void __iomem *dst, const void *src, int count)
  294. {
  295. memcpy((void __force *) dst, src, count);
  296. }
  297. #endif
  298. /*
  299. * Map in an area of physical address space, for accessing
  300. * I/O devices etc.
  301. */
  302. extern void __iomem *__ioremap(phys_addr_t address, unsigned long size,
  303. unsigned long flags);
  304. extern void __iomem *ioremap(phys_addr_t address, unsigned long size);
  305. #ifdef CONFIG_44x
  306. extern void __iomem *ioremap64(unsigned long long address, unsigned long size);
  307. #endif
  308. #define ioremap_nocache(addr, size) ioremap((addr), (size))
  309. extern void iounmap(volatile void __iomem *addr);
  310. extern unsigned long iopa(unsigned long addr);
  311. extern void io_block_mapping(unsigned long virt, phys_addr_t phys,
  312. unsigned int size, int flags);
  313. /*
  314. * The PCI bus is inherently Little-Endian. The PowerPC is being
  315. * run Big-Endian. Thus all values which cross the [PCI] barrier
  316. * must be endian-adjusted. Also, the local DRAM has a different
  317. * address from the PCI point of view, thus buffer addresses also
  318. * have to be modified [mapped] appropriately.
  319. */
  320. extern inline unsigned long virt_to_bus(volatile void * address)
  321. {
  322. if (address == (void *)0)
  323. return 0;
  324. return (unsigned long)address - KERNELBASE + PCI_DRAM_OFFSET;
  325. }
  326. extern inline void * bus_to_virt(unsigned long address)
  327. {
  328. if (address == 0)
  329. return NULL;
  330. return (void *)(address - PCI_DRAM_OFFSET + KERNELBASE);
  331. }
  332. /*
  333. * Change virtual addresses to physical addresses and vv, for
  334. * addresses in the area where the kernel has the RAM mapped.
  335. */
  336. extern inline unsigned long virt_to_phys(volatile void * address)
  337. {
  338. return (unsigned long) address - KERNELBASE;
  339. }
  340. extern inline void * phys_to_virt(unsigned long address)
  341. {
  342. return (void *) (address + KERNELBASE);
  343. }
  344. /*
  345. * Change "struct page" to physical address.
  346. */
  347. #define page_to_phys(page) (page_to_pfn(page) << PAGE_SHIFT)
  348. #define page_to_bus(page) (page_to_phys(page) + PCI_DRAM_OFFSET)
  349. /* Enforce in-order execution of data I/O.
  350. * No distinction between read/write on PPC; use eieio for all three.
  351. */
  352. #define iobarrier_rw() eieio()
  353. #define iobarrier_r() eieio()
  354. #define iobarrier_w() eieio()
  355. /*
  356. * Here comes the ppc implementation of the IOMAP
  357. * interfaces.
  358. */
  359. static inline unsigned int ioread8(void __iomem *addr)
  360. {
  361. return readb(addr);
  362. }
  363. static inline unsigned int ioread16(void __iomem *addr)
  364. {
  365. return readw(addr);
  366. }
  367. static inline unsigned int ioread32(void __iomem *addr)
  368. {
  369. return readl(addr);
  370. }
  371. static inline void iowrite8(u8 val, void __iomem *addr)
  372. {
  373. writeb(val, addr);
  374. }
  375. static inline void iowrite16(u16 val, void __iomem *addr)
  376. {
  377. writew(val, addr);
  378. }
  379. static inline void iowrite32(u32 val, void __iomem *addr)
  380. {
  381. writel(val, addr);
  382. }
  383. static inline void ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
  384. {
  385. _insb(addr, dst, count);
  386. }
  387. static inline void ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
  388. {
  389. _insw_ns(addr, dst, count);
  390. }
  391. static inline void ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
  392. {
  393. _insl_ns(addr, dst, count);
  394. }
  395. static inline void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
  396. {
  397. _outsb(addr, src, count);
  398. }
  399. static inline void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
  400. {
  401. _outsw_ns(addr, src, count);
  402. }
  403. static inline void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
  404. {
  405. _outsl_ns(addr, src, count);
  406. }
  407. /* Create a virtual mapping cookie for an IO port range */
  408. extern void __iomem *ioport_map(unsigned long port, unsigned int nr);
  409. extern void ioport_unmap(void __iomem *);
  410. /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */
  411. struct pci_dev;
  412. extern void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max);
  413. extern void pci_iounmap(struct pci_dev *dev, void __iomem *);
  414. #endif /* _PPC_IO_H */
  415. #ifdef CONFIG_8260_PCI9
  416. #include <asm/mpc8260_pci9.h>
  417. #endif
  418. /*
  419. * Convert a physical pointer to a virtual kernel pointer for /dev/mem
  420. * access
  421. */
  422. #define xlate_dev_mem_ptr(p) __va(p)
  423. /*
  424. * Convert a virtual cached pointer to an uncached pointer
  425. */
  426. #define xlate_dev_kmem_ptr(p) p
  427. /* access ports */
  428. #define setbits32(_addr, _v) out_be32((_addr), in_be32(_addr) | (_v))
  429. #define clrbits32(_addr, _v) out_be32((_addr), in_be32(_addr) & ~(_v))
  430. #define setbits16(_addr, _v) out_be16((_addr), in_be16(_addr) | (_v))
  431. #define clrbits16(_addr, _v) out_be16((_addr), in_be16(_addr) & ~(_v))
  432. #define setbits8(_addr, _v) out_8((_addr), in_8(_addr) | (_v))
  433. #define clrbits8(_addr, _v) out_8((_addr), in_8(_addr) & ~(_v))
  434. #endif /* __KERNEL__ */