io.h 15 KB

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