io.h 16 KB

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