io.h 15 KB

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