xref: /linux/arch/powerpc/include/asm/io.h (revision 191e42063a7241e5c3a1d1f36896a20b147517e9)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2b8b572e1SStephen Rothwell #ifndef _ASM_POWERPC_IO_H
3b8b572e1SStephen Rothwell #define _ASM_POWERPC_IO_H
4b8b572e1SStephen Rothwell #ifdef __KERNEL__
5b8b572e1SStephen Rothwell 
6be135f40SAnton Blanchard #define ARCH_HAS_IOREMAP_WC
786c391bdSChristophe Leroy #ifdef CONFIG_PPC32
886c391bdSChristophe Leroy #define ARCH_HAS_IOREMAP_WT
986c391bdSChristophe Leroy #endif
10be135f40SAnton Blanchard 
11b8b572e1SStephen Rothwell /*
12b8b572e1SStephen Rothwell  */
13b8b572e1SStephen Rothwell 
14b8b572e1SStephen Rothwell /* Check of existence of legacy devices */
15b8b572e1SStephen Rothwell extern int check_legacy_ioport(unsigned long base_port);
16b8b572e1SStephen Rothwell #define I8042_DATA_REG	0x60
17b8b572e1SStephen Rothwell #define FDC_BASE	0x3f0
18b8b572e1SStephen Rothwell 
19e1612de9SHaren Myneni #if defined(CONFIG_PPC64) && defined(CONFIG_PCI)
20e1612de9SHaren Myneni extern struct pci_dev *isa_bridge_pcidev;
21e1612de9SHaren Myneni /*
22e1612de9SHaren Myneni  * has legacy ISA devices ?
23e1612de9SHaren Myneni  */
24ac237b65SBenjamin Herrenschmidt #define arch_has_dev_port()	(isa_bridge_pcidev != NULL || isa_io_special)
25e1612de9SHaren Myneni #endif
26e1612de9SHaren Myneni 
27b8b572e1SStephen Rothwell #include <linux/device.h>
28b8b572e1SStephen Rothwell #include <linux/compiler.h>
296bf752daSChristophe Leroy #include <linux/mm.h>
30b8b572e1SStephen Rothwell #include <asm/page.h>
31b8b572e1SStephen Rothwell #include <asm/byteorder.h>
32b8b572e1SStephen Rothwell #include <asm/synch.h>
33b8b572e1SStephen Rothwell #include <asm/delay.h>
34420af155SWill Deacon #include <asm/mmiowb.h>
35b8b572e1SStephen Rothwell #include <asm/mmu.h>
3624bfa6a9SNicholas Piggin #include <asm/ppc_asm.h>
376bf752daSChristophe Leroy #include <asm/pgtable.h>
38b8b572e1SStephen Rothwell 
39b8b572e1SStephen Rothwell #define SIO_CONFIG_RA	0x398
40b8b572e1SStephen Rothwell #define SIO_CONFIG_RD	0x399
41b8b572e1SStephen Rothwell 
42b8b572e1SStephen Rothwell #define SLOW_DOWN_IO
43b8b572e1SStephen Rothwell 
44b8b572e1SStephen Rothwell /* 32 bits uses slightly different variables for the various IO
45b8b572e1SStephen Rothwell  * bases. Most of this file only uses _IO_BASE though which we
46b8b572e1SStephen Rothwell  * define properly based on the platform
47b8b572e1SStephen Rothwell  */
48b8b572e1SStephen Rothwell #ifndef CONFIG_PCI
49b8b572e1SStephen Rothwell #define _IO_BASE	0
50b8b572e1SStephen Rothwell #define _ISA_MEM_BASE	0
51b8b572e1SStephen Rothwell #define PCI_DRAM_OFFSET 0
52b8b572e1SStephen Rothwell #elif defined(CONFIG_PPC32)
53b8b572e1SStephen Rothwell #define _IO_BASE	isa_io_base
54b8b572e1SStephen Rothwell #define _ISA_MEM_BASE	isa_mem_base
55b8b572e1SStephen Rothwell #define PCI_DRAM_OFFSET	pci_dram_offset
56b8b572e1SStephen Rothwell #else
57b8b572e1SStephen Rothwell #define _IO_BASE	pci_io_base
58b8b572e1SStephen Rothwell #define _ISA_MEM_BASE	isa_mem_base
59b8b572e1SStephen Rothwell #define PCI_DRAM_OFFSET	0
60b8b572e1SStephen Rothwell #endif
61b8b572e1SStephen Rothwell 
62b8b572e1SStephen Rothwell extern unsigned long isa_io_base;
63b8b572e1SStephen Rothwell extern unsigned long pci_io_base;
64b8b572e1SStephen Rothwell extern unsigned long pci_dram_offset;
65b8b572e1SStephen Rothwell 
66b8b572e1SStephen Rothwell extern resource_size_t isa_mem_base;
67b8b572e1SStephen Rothwell 
683fafe9c2SBenjamin Herrenschmidt /* Boolean set by platform if PIO accesses are suppored while _IO_BASE
693fafe9c2SBenjamin Herrenschmidt  * is not set or addresses cannot be translated to MMIO. This is typically
703fafe9c2SBenjamin Herrenschmidt  * set when the platform supports "special" PIO accesses via a non memory
713fafe9c2SBenjamin Herrenschmidt  * mapped mechanism, and allows things like the early udbg UART code to
723fafe9c2SBenjamin Herrenschmidt  * function.
733fafe9c2SBenjamin Herrenschmidt  */
743fafe9c2SBenjamin Herrenschmidt extern bool isa_io_special;
753fafe9c2SBenjamin Herrenschmidt 
76ecd73cc5SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
77ecd73cc5SBenjamin Herrenschmidt #if defined(CONFIG_PPC_INDIRECT_PIO) || defined(CONFIG_PPC_INDIRECT_MMIO)
78ecd73cc5SBenjamin Herrenschmidt #error CONFIG_PPC_INDIRECT_{PIO,MMIO} are not yet supported on 32 bits
79ecd73cc5SBenjamin Herrenschmidt #endif
80b8b572e1SStephen Rothwell #endif
81b8b572e1SStephen Rothwell 
82b8b572e1SStephen Rothwell /*
83b8b572e1SStephen Rothwell  *
84b8b572e1SStephen Rothwell  * Low level MMIO accessors
85b8b572e1SStephen Rothwell  *
86b8b572e1SStephen Rothwell  * This provides the non-bus specific accessors to MMIO. Those are PowerPC
87b8b572e1SStephen Rothwell  * specific and thus shouldn't be used in generic code. The accessors
88b8b572e1SStephen Rothwell  * provided here are:
89b8b572e1SStephen Rothwell  *
90b8b572e1SStephen Rothwell  *	in_8, in_le16, in_be16, in_le32, in_be32, in_le64, in_be64
91b8b572e1SStephen Rothwell  *	out_8, out_le16, out_be16, out_le32, out_be32, out_le64, out_be64
92b8b572e1SStephen Rothwell  *	_insb, _insw_ns, _insl_ns, _outsb, _outsw_ns, _outsl_ns
93b8b572e1SStephen Rothwell  *
94b8b572e1SStephen Rothwell  * Those operate directly on a kernel virtual address. Note that the prototype
95b8b572e1SStephen Rothwell  * for the out_* accessors has the arguments in opposite order from the usual
96b8b572e1SStephen Rothwell  * linux PCI accessors. Unlike those, they take the address first and the value
97b8b572e1SStephen Rothwell  * next.
98b8b572e1SStephen Rothwell  *
99b8b572e1SStephen Rothwell  * Note: I might drop the _ns suffix on the stream operations soon as it is
100b8b572e1SStephen Rothwell  * simply normal for stream operations to not swap in the first place.
101b8b572e1SStephen Rothwell  *
102b8b572e1SStephen Rothwell  */
103b8b572e1SStephen Rothwell 
10415cba23eSIan Munsie #define DEF_MMIO_IN_X(name, size, insn)				\
105b8b572e1SStephen Rothwell static inline u##size name(const volatile u##size __iomem *addr)	\
106b8b572e1SStephen Rothwell {									\
107b8b572e1SStephen Rothwell 	u##size ret;							\
108b8b572e1SStephen Rothwell 	__asm__ __volatile__("sync;"#insn" %0,%y1;twi 0,%0,0;isync"	\
109b8b572e1SStephen Rothwell 		: "=r" (ret) : "Z" (*addr) : "memory");			\
110b8b572e1SStephen Rothwell 	return ret;							\
111b8b572e1SStephen Rothwell }
112b8b572e1SStephen Rothwell 
11315cba23eSIan Munsie #define DEF_MMIO_OUT_X(name, size, insn)				\
114b8b572e1SStephen Rothwell static inline void name(volatile u##size __iomem *addr, u##size val)	\
115b8b572e1SStephen Rothwell {									\
116b8b572e1SStephen Rothwell 	__asm__ __volatile__("sync;"#insn" %1,%y0"			\
117b8b572e1SStephen Rothwell 		: "=Z" (*addr) : "r" (val) : "memory");			\
118420af155SWill Deacon 	mmiowb_set_pending();						\
119b8b572e1SStephen Rothwell }
120b8b572e1SStephen Rothwell 
12115cba23eSIan Munsie #define DEF_MMIO_IN_D(name, size, insn)				\
122b8b572e1SStephen Rothwell static inline u##size name(const volatile u##size __iomem *addr)	\
123b8b572e1SStephen Rothwell {									\
124b8b572e1SStephen Rothwell 	u##size ret;							\
125b8b572e1SStephen Rothwell 	__asm__ __volatile__("sync;"#insn"%U1%X1 %0,%1;twi 0,%0,0;isync"\
126b8b572e1SStephen Rothwell 		: "=r" (ret) : "m" (*addr) : "memory");			\
127b8b572e1SStephen Rothwell 	return ret;							\
128b8b572e1SStephen Rothwell }
129b8b572e1SStephen Rothwell 
13015cba23eSIan Munsie #define DEF_MMIO_OUT_D(name, size, insn)				\
131b8b572e1SStephen Rothwell static inline void name(volatile u##size __iomem *addr, u##size val)	\
132b8b572e1SStephen Rothwell {									\
133b8b572e1SStephen Rothwell 	__asm__ __volatile__("sync;"#insn"%U0%X0 %1,%0"			\
134b8b572e1SStephen Rothwell 		: "=m" (*addr) : "r" (val) : "memory");			\
135420af155SWill Deacon 	mmiowb_set_pending();						\
136b8b572e1SStephen Rothwell }
137b8b572e1SStephen Rothwell 
13815cba23eSIan Munsie DEF_MMIO_IN_D(in_8,     8, lbz);
13915cba23eSIan Munsie DEF_MMIO_OUT_D(out_8,   8, stb);
140b8b572e1SStephen Rothwell 
14115cba23eSIan Munsie #ifdef __BIG_ENDIAN__
14215cba23eSIan Munsie DEF_MMIO_IN_D(in_be16, 16, lhz);
14315cba23eSIan Munsie DEF_MMIO_IN_D(in_be32, 32, lwz);
14415cba23eSIan Munsie DEF_MMIO_IN_X(in_le16, 16, lhbrx);
14515cba23eSIan Munsie DEF_MMIO_IN_X(in_le32, 32, lwbrx);
146b8b572e1SStephen Rothwell 
14715cba23eSIan Munsie DEF_MMIO_OUT_D(out_be16, 16, sth);
14815cba23eSIan Munsie DEF_MMIO_OUT_D(out_be32, 32, stw);
14915cba23eSIan Munsie DEF_MMIO_OUT_X(out_le16, 16, sthbrx);
15015cba23eSIan Munsie DEF_MMIO_OUT_X(out_le32, 32, stwbrx);
15115cba23eSIan Munsie #else
15215cba23eSIan Munsie DEF_MMIO_IN_X(in_be16, 16, lhbrx);
15315cba23eSIan Munsie DEF_MMIO_IN_X(in_be32, 32, lwbrx);
15415cba23eSIan Munsie DEF_MMIO_IN_D(in_le16, 16, lhz);
15515cba23eSIan Munsie DEF_MMIO_IN_D(in_le32, 32, lwz);
15615cba23eSIan Munsie 
15715cba23eSIan Munsie DEF_MMIO_OUT_X(out_be16, 16, sthbrx);
15815cba23eSIan Munsie DEF_MMIO_OUT_X(out_be32, 32, stwbrx);
15915cba23eSIan Munsie DEF_MMIO_OUT_D(out_le16, 16, sth);
16015cba23eSIan Munsie DEF_MMIO_OUT_D(out_le32, 32, stw);
16115cba23eSIan Munsie 
16215cba23eSIan Munsie #endif /* __BIG_ENDIAN */
163b8b572e1SStephen Rothwell 
164b8b572e1SStephen Rothwell #ifdef __powerpc64__
16515cba23eSIan Munsie 
16615cba23eSIan Munsie #ifdef __BIG_ENDIAN__
16715cba23eSIan Munsie DEF_MMIO_OUT_D(out_be64, 64, std);
16815cba23eSIan Munsie DEF_MMIO_IN_D(in_be64, 64, ld);
169b8b572e1SStephen Rothwell 
170b8b572e1SStephen Rothwell /* There is no asm instructions for 64 bits reverse loads and stores */
171b8b572e1SStephen Rothwell static inline u64 in_le64(const volatile u64 __iomem *addr)
172b8b572e1SStephen Rothwell {
173b8b572e1SStephen Rothwell 	return swab64(in_be64(addr));
174b8b572e1SStephen Rothwell }
175b8b572e1SStephen Rothwell 
176b8b572e1SStephen Rothwell static inline void out_le64(volatile u64 __iomem *addr, u64 val)
177b8b572e1SStephen Rothwell {
178b8b572e1SStephen Rothwell 	out_be64(addr, swab64(val));
179b8b572e1SStephen Rothwell }
18015cba23eSIan Munsie #else
18115cba23eSIan Munsie DEF_MMIO_OUT_D(out_le64, 64, std);
18215cba23eSIan Munsie DEF_MMIO_IN_D(in_le64, 64, ld);
18315cba23eSIan Munsie 
18415cba23eSIan Munsie /* There is no asm instructions for 64 bits reverse loads and stores */
18515cba23eSIan Munsie static inline u64 in_be64(const volatile u64 __iomem *addr)
18615cba23eSIan Munsie {
18715cba23eSIan Munsie 	return swab64(in_le64(addr));
18815cba23eSIan Munsie }
18915cba23eSIan Munsie 
19015cba23eSIan Munsie static inline void out_be64(volatile u64 __iomem *addr, u64 val)
19115cba23eSIan Munsie {
19215cba23eSIan Munsie 	out_le64(addr, swab64(val));
19315cba23eSIan Munsie }
19415cba23eSIan Munsie 
19515cba23eSIan Munsie #endif
196b8b572e1SStephen Rothwell #endif /* __powerpc64__ */
197b8b572e1SStephen Rothwell 
198b8b572e1SStephen Rothwell /*
199b8b572e1SStephen Rothwell  * Low level IO stream instructions are defined out of line for now
200b8b572e1SStephen Rothwell  */
201b8b572e1SStephen Rothwell extern void _insb(const volatile u8 __iomem *addr, void *buf, long count);
202b8b572e1SStephen Rothwell extern void _outsb(volatile u8 __iomem *addr,const void *buf,long count);
203b8b572e1SStephen Rothwell extern void _insw_ns(const volatile u16 __iomem *addr, void *buf, long count);
204b8b572e1SStephen Rothwell extern void _outsw_ns(volatile u16 __iomem *addr, const void *buf, long count);
205b8b572e1SStephen Rothwell extern void _insl_ns(const volatile u32 __iomem *addr, void *buf, long count);
206b8b572e1SStephen Rothwell extern void _outsl_ns(volatile u32 __iomem *addr, const void *buf, long count);
207b8b572e1SStephen Rothwell 
208b8b572e1SStephen Rothwell /* The _ns naming is historical and will be removed. For now, just #define
209b8b572e1SStephen Rothwell  * the non _ns equivalent names
210b8b572e1SStephen Rothwell  */
211b8b572e1SStephen Rothwell #define _insw	_insw_ns
212b8b572e1SStephen Rothwell #define _insl	_insl_ns
213b8b572e1SStephen Rothwell #define _outsw	_outsw_ns
214b8b572e1SStephen Rothwell #define _outsl	_outsl_ns
215b8b572e1SStephen Rothwell 
216b8b572e1SStephen Rothwell 
217b8b572e1SStephen Rothwell /*
218b8b572e1SStephen Rothwell  * memset_io, memcpy_toio, memcpy_fromio base implementations are out of line
219b8b572e1SStephen Rothwell  */
220b8b572e1SStephen Rothwell 
221b8b572e1SStephen Rothwell extern void _memset_io(volatile void __iomem *addr, int c, unsigned long n);
222b8b572e1SStephen Rothwell extern void _memcpy_fromio(void *dest, const volatile void __iomem *src,
223b8b572e1SStephen Rothwell 			   unsigned long n);
224b8b572e1SStephen Rothwell extern void _memcpy_toio(volatile void __iomem *dest, const void *src,
225b8b572e1SStephen Rothwell 			 unsigned long n);
226b8b572e1SStephen Rothwell 
227b8b572e1SStephen Rothwell /*
228b8b572e1SStephen Rothwell  *
229b8b572e1SStephen Rothwell  * PCI and standard ISA accessors
230b8b572e1SStephen Rothwell  *
231b8b572e1SStephen Rothwell  * Those are globally defined linux accessors for devices on PCI or ISA
232b8b572e1SStephen Rothwell  * busses. They follow the Linux defined semantics. The current implementation
233b8b572e1SStephen Rothwell  * for PowerPC is as close as possible to the x86 version of these, and thus
234b8b572e1SStephen Rothwell  * provides fairly heavy weight barriers for the non-raw versions
235b8b572e1SStephen Rothwell  *
236ecd73cc5SBenjamin Herrenschmidt  * In addition, they support a hook mechanism when CONFIG_PPC_INDIRECT_MMIO
237ecd73cc5SBenjamin Herrenschmidt  * or CONFIG_PPC_INDIRECT_PIO are set allowing the platform to provide its
238ecd73cc5SBenjamin Herrenschmidt  * own implementation of some or all of the accessors.
239b8b572e1SStephen Rothwell  */
240b8b572e1SStephen Rothwell 
241b8b572e1SStephen Rothwell /*
242b8b572e1SStephen Rothwell  * Include the EEH definitions when EEH is enabled only so they don't get
243b8b572e1SStephen Rothwell  * in the way when building for 32 bits
244b8b572e1SStephen Rothwell  */
245b8b572e1SStephen Rothwell #ifdef CONFIG_EEH
246b8b572e1SStephen Rothwell #include <asm/eeh.h>
247b8b572e1SStephen Rothwell #endif
248b8b572e1SStephen Rothwell 
249b8b572e1SStephen Rothwell /* Shortcut to the MMIO argument pointer */
250b8b572e1SStephen Rothwell #define PCI_IO_ADDR	volatile void __iomem *
251b8b572e1SStephen Rothwell 
252b8b572e1SStephen Rothwell /* Indirect IO address tokens:
253b8b572e1SStephen Rothwell  *
254ecd73cc5SBenjamin Herrenschmidt  * When CONFIG_PPC_INDIRECT_MMIO is set, the platform can provide hooks
255ecd73cc5SBenjamin Herrenschmidt  * on all MMIOs. (Note that this is all 64 bits only for now)
256b8b572e1SStephen Rothwell  *
257446957baSAdam Buchbinder  * To help platforms who may need to differentiate MMIO addresses in
258b8b572e1SStephen Rothwell  * their hooks, a bitfield is reserved for use by the platform near the
259b8b572e1SStephen Rothwell  * top of MMIO addresses (not PIO, those have to cope the hard way).
260b8b572e1SStephen Rothwell  *
26143c6494fSMichael Ellerman  * The highest address in the kernel virtual space are:
262b8b572e1SStephen Rothwell  *
26343c6494fSMichael Ellerman  *  d0003fffffffffff	# with Hash MMU
26443c6494fSMichael Ellerman  *  c00fffffffffffff	# with Radix MMU
265b8b572e1SStephen Rothwell  *
26643c6494fSMichael Ellerman  * The top 4 bits are reserved as the region ID on hash, leaving us 8 bits
26743c6494fSMichael Ellerman  * that can be used for the field.
268b8b572e1SStephen Rothwell  *
269b8b572e1SStephen Rothwell  * The direct IO mapping operations will then mask off those bits
270b8b572e1SStephen Rothwell  * before doing the actual access, though that only happen when
271ecd73cc5SBenjamin Herrenschmidt  * CONFIG_PPC_INDIRECT_MMIO is set, thus be careful when you use that
272b8b572e1SStephen Rothwell  * mechanism
273ecd73cc5SBenjamin Herrenschmidt  *
274ecd73cc5SBenjamin Herrenschmidt  * For PIO, there is a separate CONFIG_PPC_INDIRECT_PIO which makes
275ecd73cc5SBenjamin Herrenschmidt  * all PIO functions call through a hook.
276b8b572e1SStephen Rothwell  */
277b8b572e1SStephen Rothwell 
278ecd73cc5SBenjamin Herrenschmidt #ifdef CONFIG_PPC_INDIRECT_MMIO
27943c6494fSMichael Ellerman #define PCI_IO_IND_TOKEN_SHIFT	52
28043c6494fSMichael Ellerman #define PCI_IO_IND_TOKEN_MASK	(0xfful << PCI_IO_IND_TOKEN_SHIFT)
281b8b572e1SStephen Rothwell #define PCI_FIX_ADDR(addr)						\
282b8b572e1SStephen Rothwell 	((PCI_IO_ADDR)(((unsigned long)(addr)) & ~PCI_IO_IND_TOKEN_MASK))
283b8b572e1SStephen Rothwell #define PCI_GET_ADDR_TOKEN(addr)					\
284b8b572e1SStephen Rothwell 	(((unsigned long)(addr) & PCI_IO_IND_TOKEN_MASK) >> 		\
285b8b572e1SStephen Rothwell 		PCI_IO_IND_TOKEN_SHIFT)
286b8b572e1SStephen Rothwell #define PCI_SET_ADDR_TOKEN(addr, token) 				\
287b8b572e1SStephen Rothwell do {									\
288b8b572e1SStephen Rothwell 	unsigned long __a = (unsigned long)(addr);			\
289b8b572e1SStephen Rothwell 	__a &= ~PCI_IO_IND_TOKEN_MASK;					\
290b8b572e1SStephen Rothwell 	__a |= ((unsigned long)(token)) << PCI_IO_IND_TOKEN_SHIFT;	\
291b8b572e1SStephen Rothwell 	(addr) = (void __iomem *)__a;					\
292b8b572e1SStephen Rothwell } while(0)
293b8b572e1SStephen Rothwell #else
294b8b572e1SStephen Rothwell #define PCI_FIX_ADDR(addr) (addr)
295b8b572e1SStephen Rothwell #endif
296b8b572e1SStephen Rothwell 
297b8b572e1SStephen Rothwell 
298b8b572e1SStephen Rothwell /*
299b8b572e1SStephen Rothwell  * Non ordered and non-swapping "raw" accessors
300b8b572e1SStephen Rothwell  */
301b8b572e1SStephen Rothwell 
302b8b572e1SStephen Rothwell static inline unsigned char __raw_readb(const volatile void __iomem *addr)
303b8b572e1SStephen Rothwell {
304b8b572e1SStephen Rothwell 	return *(volatile unsigned char __force *)PCI_FIX_ADDR(addr);
305b8b572e1SStephen Rothwell }
306b8b572e1SStephen Rothwell static inline unsigned short __raw_readw(const volatile void __iomem *addr)
307b8b572e1SStephen Rothwell {
308b8b572e1SStephen Rothwell 	return *(volatile unsigned short __force *)PCI_FIX_ADDR(addr);
309b8b572e1SStephen Rothwell }
310b8b572e1SStephen Rothwell static inline unsigned int __raw_readl(const volatile void __iomem *addr)
311b8b572e1SStephen Rothwell {
312b8b572e1SStephen Rothwell 	return *(volatile unsigned int __force *)PCI_FIX_ADDR(addr);
313b8b572e1SStephen Rothwell }
314b8b572e1SStephen Rothwell static inline void __raw_writeb(unsigned char v, volatile void __iomem *addr)
315b8b572e1SStephen Rothwell {
316b8b572e1SStephen Rothwell 	*(volatile unsigned char __force *)PCI_FIX_ADDR(addr) = v;
317b8b572e1SStephen Rothwell }
318b8b572e1SStephen Rothwell static inline void __raw_writew(unsigned short v, volatile void __iomem *addr)
319b8b572e1SStephen Rothwell {
320b8b572e1SStephen Rothwell 	*(volatile unsigned short __force *)PCI_FIX_ADDR(addr) = v;
321b8b572e1SStephen Rothwell }
322b8b572e1SStephen Rothwell static inline void __raw_writel(unsigned int v, volatile void __iomem *addr)
323b8b572e1SStephen Rothwell {
324b8b572e1SStephen Rothwell 	*(volatile unsigned int __force *)PCI_FIX_ADDR(addr) = v;
325b8b572e1SStephen Rothwell }
326b8b572e1SStephen Rothwell 
327b8b572e1SStephen Rothwell #ifdef __powerpc64__
328b8b572e1SStephen Rothwell static inline unsigned long __raw_readq(const volatile void __iomem *addr)
329b8b572e1SStephen Rothwell {
330b8b572e1SStephen Rothwell 	return *(volatile unsigned long __force *)PCI_FIX_ADDR(addr);
331b8b572e1SStephen Rothwell }
332b8b572e1SStephen Rothwell static inline void __raw_writeq(unsigned long v, volatile void __iomem *addr)
333b8b572e1SStephen Rothwell {
334b8b572e1SStephen Rothwell 	*(volatile unsigned long __force *)PCI_FIX_ADDR(addr) = v;
335b8b572e1SStephen Rothwell }
336a84bf321SAlistair Popple 
3378056fe28SMichael Ellerman static inline void __raw_writeq_be(unsigned long v, volatile void __iomem *addr)
3388056fe28SMichael Ellerman {
3398056fe28SMichael Ellerman 	__raw_writeq((__force unsigned long)cpu_to_be64(v), addr);
3408056fe28SMichael Ellerman }
3418056fe28SMichael Ellerman 
342a84bf321SAlistair Popple /*
343d381d7caSBenjamin Herrenschmidt  * Real mode versions of the above. Those instructions are only supposed
344d381d7caSBenjamin Herrenschmidt  * to be used in hypervisor real mode as per the architecture spec.
345a84bf321SAlistair Popple  */
346d381d7caSBenjamin Herrenschmidt static inline void __raw_rm_writeb(u8 val, volatile void __iomem *paddr)
347d381d7caSBenjamin Herrenschmidt {
348d381d7caSBenjamin Herrenschmidt 	__asm__ __volatile__("stbcix %0,0,%1"
349d381d7caSBenjamin Herrenschmidt 		: : "r" (val), "r" (paddr) : "memory");
350d381d7caSBenjamin Herrenschmidt }
351d381d7caSBenjamin Herrenschmidt 
352d381d7caSBenjamin Herrenschmidt static inline void __raw_rm_writew(u16 val, volatile void __iomem *paddr)
353d381d7caSBenjamin Herrenschmidt {
354d381d7caSBenjamin Herrenschmidt 	__asm__ __volatile__("sthcix %0,0,%1"
355d381d7caSBenjamin Herrenschmidt 		: : "r" (val), "r" (paddr) : "memory");
356d381d7caSBenjamin Herrenschmidt }
357d381d7caSBenjamin Herrenschmidt 
358d381d7caSBenjamin Herrenschmidt static inline void __raw_rm_writel(u32 val, volatile void __iomem *paddr)
359d381d7caSBenjamin Herrenschmidt {
360d381d7caSBenjamin Herrenschmidt 	__asm__ __volatile__("stwcix %0,0,%1"
361d381d7caSBenjamin Herrenschmidt 		: : "r" (val), "r" (paddr) : "memory");
362d381d7caSBenjamin Herrenschmidt }
363d381d7caSBenjamin Herrenschmidt 
364a84bf321SAlistair Popple static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
365a84bf321SAlistair Popple {
366a84bf321SAlistair Popple 	__asm__ __volatile__("stdcix %0,0,%1"
367a84bf321SAlistair Popple 		: : "r" (val), "r" (paddr) : "memory");
368a84bf321SAlistair Popple }
369a84bf321SAlistair Popple 
3708056fe28SMichael Ellerman static inline void __raw_rm_writeq_be(u64 val, volatile void __iomem *paddr)
3718056fe28SMichael Ellerman {
3728056fe28SMichael Ellerman 	__raw_rm_writeq((__force u64)cpu_to_be64(val), paddr);
3738056fe28SMichael Ellerman }
3748056fe28SMichael Ellerman 
375d381d7caSBenjamin Herrenschmidt static inline u8 __raw_rm_readb(volatile void __iomem *paddr)
376d381d7caSBenjamin Herrenschmidt {
377d381d7caSBenjamin Herrenschmidt 	u8 ret;
378d381d7caSBenjamin Herrenschmidt 	__asm__ __volatile__("lbzcix %0,0, %1"
379d381d7caSBenjamin Herrenschmidt 			     : "=r" (ret) : "r" (paddr) : "memory");
380d381d7caSBenjamin Herrenschmidt 	return ret;
381d381d7caSBenjamin Herrenschmidt }
382d381d7caSBenjamin Herrenschmidt 
383d381d7caSBenjamin Herrenschmidt static inline u16 __raw_rm_readw(volatile void __iomem *paddr)
384d381d7caSBenjamin Herrenschmidt {
385d381d7caSBenjamin Herrenschmidt 	u16 ret;
386d381d7caSBenjamin Herrenschmidt 	__asm__ __volatile__("lhzcix %0,0, %1"
387d381d7caSBenjamin Herrenschmidt 			     : "=r" (ret) : "r" (paddr) : "memory");
388d381d7caSBenjamin Herrenschmidt 	return ret;
389d381d7caSBenjamin Herrenschmidt }
390d381d7caSBenjamin Herrenschmidt 
391d381d7caSBenjamin Herrenschmidt static inline u32 __raw_rm_readl(volatile void __iomem *paddr)
392d381d7caSBenjamin Herrenschmidt {
393d381d7caSBenjamin Herrenschmidt 	u32 ret;
394d381d7caSBenjamin Herrenschmidt 	__asm__ __volatile__("lwzcix %0,0, %1"
395d381d7caSBenjamin Herrenschmidt 			     : "=r" (ret) : "r" (paddr) : "memory");
396d381d7caSBenjamin Herrenschmidt 	return ret;
397d381d7caSBenjamin Herrenschmidt }
398d381d7caSBenjamin Herrenschmidt 
399d381d7caSBenjamin Herrenschmidt static inline u64 __raw_rm_readq(volatile void __iomem *paddr)
400d381d7caSBenjamin Herrenschmidt {
401d381d7caSBenjamin Herrenschmidt 	u64 ret;
402d381d7caSBenjamin Herrenschmidt 	__asm__ __volatile__("ldcix %0,0, %1"
403d381d7caSBenjamin Herrenschmidt 			     : "=r" (ret) : "r" (paddr) : "memory");
404d381d7caSBenjamin Herrenschmidt 	return ret;
405d381d7caSBenjamin Herrenschmidt }
406b8b572e1SStephen Rothwell #endif /* __powerpc64__ */
407b8b572e1SStephen Rothwell 
408b8b572e1SStephen Rothwell /*
409b8b572e1SStephen Rothwell  *
410b8b572e1SStephen Rothwell  * PCI PIO and MMIO accessors.
411b8b572e1SStephen Rothwell  *
412b8b572e1SStephen Rothwell  *
413b8b572e1SStephen Rothwell  * On 32 bits, PIO operations have a recovery mechanism in case they trigger
414b8b572e1SStephen Rothwell  * machine checks (which they occasionally do when probing non existing
415b8b572e1SStephen Rothwell  * IO ports on some platforms, like PowerMac and 8xx).
416b8b572e1SStephen Rothwell  * I always found it to be of dubious reliability and I am tempted to get
417b8b572e1SStephen Rothwell  * rid of it one of these days. So if you think it's important to keep it,
418b8b572e1SStephen Rothwell  * please voice up asap. We never had it for 64 bits and I do not intend
419b8b572e1SStephen Rothwell  * to port it over
420b8b572e1SStephen Rothwell  */
421b8b572e1SStephen Rothwell 
422b8b572e1SStephen Rothwell #ifdef CONFIG_PPC32
423b8b572e1SStephen Rothwell 
424b8b572e1SStephen Rothwell #define __do_in_asm(name, op)				\
425b8b572e1SStephen Rothwell static inline unsigned int name(unsigned int port)	\
426b8b572e1SStephen Rothwell {							\
427b8b572e1SStephen Rothwell 	unsigned int x;					\
428b8b572e1SStephen Rothwell 	__asm__ __volatile__(				\
429b8b572e1SStephen Rothwell 		"sync\n"				\
430b8b572e1SStephen Rothwell 		"0:"	op "	%0,0,%1\n"		\
431b8b572e1SStephen Rothwell 		"1:	twi	0,%0,0\n"		\
432b8b572e1SStephen Rothwell 		"2:	isync\n"			\
433b8b572e1SStephen Rothwell 		"3:	nop\n"				\
434b8b572e1SStephen Rothwell 		"4:\n"					\
435b8b572e1SStephen Rothwell 		".section .fixup,\"ax\"\n"		\
436b8b572e1SStephen Rothwell 		"5:	li	%0,-1\n"		\
437b8b572e1SStephen Rothwell 		"	b	4b\n"			\
438b8b572e1SStephen Rothwell 		".previous\n"				\
43924bfa6a9SNicholas Piggin 		EX_TABLE(0b, 5b)			\
44024bfa6a9SNicholas Piggin 		EX_TABLE(1b, 5b)			\
44124bfa6a9SNicholas Piggin 		EX_TABLE(2b, 5b)			\
44224bfa6a9SNicholas Piggin 		EX_TABLE(3b, 5b)			\
443b8b572e1SStephen Rothwell 		: "=&r" (x)				\
444b8b572e1SStephen Rothwell 		: "r" (port + _IO_BASE)			\
445b8b572e1SStephen Rothwell 		: "memory");  				\
446b8b572e1SStephen Rothwell 	return x;					\
447b8b572e1SStephen Rothwell }
448b8b572e1SStephen Rothwell 
449b8b572e1SStephen Rothwell #define __do_out_asm(name, op)				\
450b8b572e1SStephen Rothwell static inline void name(unsigned int val, unsigned int port) \
451b8b572e1SStephen Rothwell {							\
452b8b572e1SStephen Rothwell 	__asm__ __volatile__(				\
453b8b572e1SStephen Rothwell 		"sync\n"				\
454b8b572e1SStephen Rothwell 		"0:" op " %0,0,%1\n"			\
455b8b572e1SStephen Rothwell 		"1:	sync\n"				\
456b8b572e1SStephen Rothwell 		"2:\n"					\
45724bfa6a9SNicholas Piggin 		EX_TABLE(0b, 2b)			\
45824bfa6a9SNicholas Piggin 		EX_TABLE(1b, 2b)			\
459b8b572e1SStephen Rothwell 		: : "r" (val), "r" (port + _IO_BASE)	\
460b8b572e1SStephen Rothwell 		: "memory");   	   	   		\
461b8b572e1SStephen Rothwell }
462b8b572e1SStephen Rothwell 
463b8b572e1SStephen Rothwell __do_in_asm(_rec_inb, "lbzx")
464b8b572e1SStephen Rothwell __do_in_asm(_rec_inw, "lhbrx")
465b8b572e1SStephen Rothwell __do_in_asm(_rec_inl, "lwbrx")
466b8b572e1SStephen Rothwell __do_out_asm(_rec_outb, "stbx")
467b8b572e1SStephen Rothwell __do_out_asm(_rec_outw, "sthbrx")
468b8b572e1SStephen Rothwell __do_out_asm(_rec_outl, "stwbrx")
469b8b572e1SStephen Rothwell 
470b8b572e1SStephen Rothwell #endif /* CONFIG_PPC32 */
471b8b572e1SStephen Rothwell 
472b8b572e1SStephen Rothwell /* The "__do_*" operations below provide the actual "base" implementation
47342b2aa86SJustin P. Mattock  * for each of the defined accessors. Some of them use the out_* functions
474b8b572e1SStephen Rothwell  * directly, some of them still use EEH, though we might change that in the
475b8b572e1SStephen Rothwell  * future. Those macros below provide the necessary argument swapping and
476b8b572e1SStephen Rothwell  * handling of the IO base for PIO.
477b8b572e1SStephen Rothwell  *
478b8b572e1SStephen Rothwell  * They are themselves used by the macros that define the actual accessors
479b8b572e1SStephen Rothwell  * and can be used by the hooks if any.
480b8b572e1SStephen Rothwell  *
481b8b572e1SStephen Rothwell  * Note that PIO operations are always defined in terms of their corresonding
482b8b572e1SStephen Rothwell  * MMIO operations. That allows platforms like iSeries who want to modify the
483b8b572e1SStephen Rothwell  * behaviour of both to only hook on the MMIO version and get both. It's also
484b8b572e1SStephen Rothwell  * possible to hook directly at the toplevel PIO operation if they have to
485b8b572e1SStephen Rothwell  * be handled differently
486b8b572e1SStephen Rothwell  */
487b8b572e1SStephen Rothwell #define __do_writeb(val, addr)	out_8(PCI_FIX_ADDR(addr), val)
488b8b572e1SStephen Rothwell #define __do_writew(val, addr)	out_le16(PCI_FIX_ADDR(addr), val)
489b8b572e1SStephen Rothwell #define __do_writel(val, addr)	out_le32(PCI_FIX_ADDR(addr), val)
490b8b572e1SStephen Rothwell #define __do_writeq(val, addr)	out_le64(PCI_FIX_ADDR(addr), val)
491b8b572e1SStephen Rothwell #define __do_writew_be(val, addr) out_be16(PCI_FIX_ADDR(addr), val)
492b8b572e1SStephen Rothwell #define __do_writel_be(val, addr) out_be32(PCI_FIX_ADDR(addr), val)
493b8b572e1SStephen Rothwell #define __do_writeq_be(val, addr) out_be64(PCI_FIX_ADDR(addr), val)
494b8b572e1SStephen Rothwell 
495b8b572e1SStephen Rothwell #ifdef CONFIG_EEH
496b8b572e1SStephen Rothwell #define __do_readb(addr)	eeh_readb(PCI_FIX_ADDR(addr))
497b8b572e1SStephen Rothwell #define __do_readw(addr)	eeh_readw(PCI_FIX_ADDR(addr))
498b8b572e1SStephen Rothwell #define __do_readl(addr)	eeh_readl(PCI_FIX_ADDR(addr))
499b8b572e1SStephen Rothwell #define __do_readq(addr)	eeh_readq(PCI_FIX_ADDR(addr))
500b8b572e1SStephen Rothwell #define __do_readw_be(addr)	eeh_readw_be(PCI_FIX_ADDR(addr))
501b8b572e1SStephen Rothwell #define __do_readl_be(addr)	eeh_readl_be(PCI_FIX_ADDR(addr))
502b8b572e1SStephen Rothwell #define __do_readq_be(addr)	eeh_readq_be(PCI_FIX_ADDR(addr))
503b8b572e1SStephen Rothwell #else /* CONFIG_EEH */
504b8b572e1SStephen Rothwell #define __do_readb(addr)	in_8(PCI_FIX_ADDR(addr))
505b8b572e1SStephen Rothwell #define __do_readw(addr)	in_le16(PCI_FIX_ADDR(addr))
506b8b572e1SStephen Rothwell #define __do_readl(addr)	in_le32(PCI_FIX_ADDR(addr))
507b8b572e1SStephen Rothwell #define __do_readq(addr)	in_le64(PCI_FIX_ADDR(addr))
508b8b572e1SStephen Rothwell #define __do_readw_be(addr)	in_be16(PCI_FIX_ADDR(addr))
509b8b572e1SStephen Rothwell #define __do_readl_be(addr)	in_be32(PCI_FIX_ADDR(addr))
510b8b572e1SStephen Rothwell #define __do_readq_be(addr)	in_be64(PCI_FIX_ADDR(addr))
511b8b572e1SStephen Rothwell #endif /* !defined(CONFIG_EEH) */
512b8b572e1SStephen Rothwell 
513b8b572e1SStephen Rothwell #ifdef CONFIG_PPC32
514b8b572e1SStephen Rothwell #define __do_outb(val, port)	_rec_outb(val, port)
515b8b572e1SStephen Rothwell #define __do_outw(val, port)	_rec_outw(val, port)
516b8b572e1SStephen Rothwell #define __do_outl(val, port)	_rec_outl(val, port)
517b8b572e1SStephen Rothwell #define __do_inb(port)		_rec_inb(port)
518b8b572e1SStephen Rothwell #define __do_inw(port)		_rec_inw(port)
519b8b572e1SStephen Rothwell #define __do_inl(port)		_rec_inl(port)
520b8b572e1SStephen Rothwell #else /* CONFIG_PPC32 */
521b8b572e1SStephen Rothwell #define __do_outb(val, port)	writeb(val,(PCI_IO_ADDR)_IO_BASE+port);
522b8b572e1SStephen Rothwell #define __do_outw(val, port)	writew(val,(PCI_IO_ADDR)_IO_BASE+port);
523b8b572e1SStephen Rothwell #define __do_outl(val, port)	writel(val,(PCI_IO_ADDR)_IO_BASE+port);
524b8b572e1SStephen Rothwell #define __do_inb(port)		readb((PCI_IO_ADDR)_IO_BASE + port);
525b8b572e1SStephen Rothwell #define __do_inw(port)		readw((PCI_IO_ADDR)_IO_BASE + port);
526b8b572e1SStephen Rothwell #define __do_inl(port)		readl((PCI_IO_ADDR)_IO_BASE + port);
527b8b572e1SStephen Rothwell #endif /* !CONFIG_PPC32 */
528b8b572e1SStephen Rothwell 
529b8b572e1SStephen Rothwell #ifdef CONFIG_EEH
530b8b572e1SStephen Rothwell #define __do_readsb(a, b, n)	eeh_readsb(PCI_FIX_ADDR(a), (b), (n))
531b8b572e1SStephen Rothwell #define __do_readsw(a, b, n)	eeh_readsw(PCI_FIX_ADDR(a), (b), (n))
532b8b572e1SStephen Rothwell #define __do_readsl(a, b, n)	eeh_readsl(PCI_FIX_ADDR(a), (b), (n))
533b8b572e1SStephen Rothwell #else /* CONFIG_EEH */
534b8b572e1SStephen Rothwell #define __do_readsb(a, b, n)	_insb(PCI_FIX_ADDR(a), (b), (n))
535b8b572e1SStephen Rothwell #define __do_readsw(a, b, n)	_insw(PCI_FIX_ADDR(a), (b), (n))
536b8b572e1SStephen Rothwell #define __do_readsl(a, b, n)	_insl(PCI_FIX_ADDR(a), (b), (n))
537b8b572e1SStephen Rothwell #endif /* !CONFIG_EEH */
538b8b572e1SStephen Rothwell #define __do_writesb(a, b, n)	_outsb(PCI_FIX_ADDR(a),(b),(n))
539b8b572e1SStephen Rothwell #define __do_writesw(a, b, n)	_outsw(PCI_FIX_ADDR(a),(b),(n))
540b8b572e1SStephen Rothwell #define __do_writesl(a, b, n)	_outsl(PCI_FIX_ADDR(a),(b),(n))
541b8b572e1SStephen Rothwell 
542b8b572e1SStephen Rothwell #define __do_insb(p, b, n)	readsb((PCI_IO_ADDR)_IO_BASE+(p), (b), (n))
543b8b572e1SStephen Rothwell #define __do_insw(p, b, n)	readsw((PCI_IO_ADDR)_IO_BASE+(p), (b), (n))
544b8b572e1SStephen Rothwell #define __do_insl(p, b, n)	readsl((PCI_IO_ADDR)_IO_BASE+(p), (b), (n))
545b8b572e1SStephen Rothwell #define __do_outsb(p, b, n)	writesb((PCI_IO_ADDR)_IO_BASE+(p),(b),(n))
546b8b572e1SStephen Rothwell #define __do_outsw(p, b, n)	writesw((PCI_IO_ADDR)_IO_BASE+(p),(b),(n))
547b8b572e1SStephen Rothwell #define __do_outsl(p, b, n)	writesl((PCI_IO_ADDR)_IO_BASE+(p),(b),(n))
548b8b572e1SStephen Rothwell 
549b8b572e1SStephen Rothwell #define __do_memset_io(addr, c, n)	\
550b8b572e1SStephen Rothwell 				_memset_io(PCI_FIX_ADDR(addr), c, n)
551b8b572e1SStephen Rothwell #define __do_memcpy_toio(dst, src, n)	\
552b8b572e1SStephen Rothwell 				_memcpy_toio(PCI_FIX_ADDR(dst), src, n)
553b8b572e1SStephen Rothwell 
554b8b572e1SStephen Rothwell #ifdef CONFIG_EEH
555b8b572e1SStephen Rothwell #define __do_memcpy_fromio(dst, src, n)	\
556b8b572e1SStephen Rothwell 				eeh_memcpy_fromio(dst, PCI_FIX_ADDR(src), n)
557b8b572e1SStephen Rothwell #else /* CONFIG_EEH */
558b8b572e1SStephen Rothwell #define __do_memcpy_fromio(dst, src, n)	\
559b8b572e1SStephen Rothwell 				_memcpy_fromio(dst,PCI_FIX_ADDR(src),n)
560b8b572e1SStephen Rothwell #endif /* !CONFIG_EEH */
561b8b572e1SStephen Rothwell 
56221176fedSMichael Ellerman #ifdef CONFIG_PPC_INDIRECT_PIO
56321176fedSMichael Ellerman #define DEF_PCI_HOOK_pio(x)	x
564b8b572e1SStephen Rothwell #else
56521176fedSMichael Ellerman #define DEF_PCI_HOOK_pio(x)	NULL
56621176fedSMichael Ellerman #endif
56721176fedSMichael Ellerman 
56821176fedSMichael Ellerman #ifdef CONFIG_PPC_INDIRECT_MMIO
56921176fedSMichael Ellerman #define DEF_PCI_HOOK_mem(x)	x
57021176fedSMichael Ellerman #else
57121176fedSMichael Ellerman #define DEF_PCI_HOOK_mem(x)	NULL
572b8b572e1SStephen Rothwell #endif
573b8b572e1SStephen Rothwell 
574b8b572e1SStephen Rothwell /* Structure containing all the hooks */
575b8b572e1SStephen Rothwell extern struct ppc_pci_io {
576b8b572e1SStephen Rothwell 
577b8b572e1SStephen Rothwell #define DEF_PCI_AC_RET(name, ret, at, al, space, aa)	ret (*name) at;
578b8b572e1SStephen Rothwell #define DEF_PCI_AC_NORET(name, at, al, space, aa)	void (*name) at;
579b8b572e1SStephen Rothwell 
580b8b572e1SStephen Rothwell #include <asm/io-defs.h>
581b8b572e1SStephen Rothwell 
582b8b572e1SStephen Rothwell #undef DEF_PCI_AC_RET
583b8b572e1SStephen Rothwell #undef DEF_PCI_AC_NORET
584b8b572e1SStephen Rothwell 
585b8b572e1SStephen Rothwell } ppc_pci_io;
586b8b572e1SStephen Rothwell 
587b8b572e1SStephen Rothwell /* The inline wrappers */
588b8b572e1SStephen Rothwell #define DEF_PCI_AC_RET(name, ret, at, al, space, aa)		\
589b8b572e1SStephen Rothwell static inline ret name at					\
590b8b572e1SStephen Rothwell {								\
59121176fedSMichael Ellerman 	if (DEF_PCI_HOOK_##space(ppc_pci_io.name) != NULL)	\
592b8b572e1SStephen Rothwell 		return ppc_pci_io.name al;			\
593b8b572e1SStephen Rothwell 	return __do_##name al;					\
594b8b572e1SStephen Rothwell }
595b8b572e1SStephen Rothwell 
596b8b572e1SStephen Rothwell #define DEF_PCI_AC_NORET(name, at, al, space, aa)		\
597b8b572e1SStephen Rothwell static inline void name at					\
598b8b572e1SStephen Rothwell {								\
59921176fedSMichael Ellerman 	if (DEF_PCI_HOOK_##space(ppc_pci_io.name) != NULL)		\
600b8b572e1SStephen Rothwell 		ppc_pci_io.name al;				\
601b8b572e1SStephen Rothwell 	else							\
602b8b572e1SStephen Rothwell 		__do_##name al;					\
603b8b572e1SStephen Rothwell }
604b8b572e1SStephen Rothwell 
605b8b572e1SStephen Rothwell #include <asm/io-defs.h>
606b8b572e1SStephen Rothwell 
607b8b572e1SStephen Rothwell #undef DEF_PCI_AC_RET
608b8b572e1SStephen Rothwell #undef DEF_PCI_AC_NORET
609b8b572e1SStephen Rothwell 
610b8b572e1SStephen Rothwell /* Some drivers check for the presence of readq & writeq with
611b8b572e1SStephen Rothwell  * a #ifdef, so we make them happy here.
612b8b572e1SStephen Rothwell  */
613b8b572e1SStephen Rothwell #ifdef __powerpc64__
614b8b572e1SStephen Rothwell #define readq	readq
615b8b572e1SStephen Rothwell #define writeq	writeq
616b8b572e1SStephen Rothwell #endif
617b8b572e1SStephen Rothwell 
618b8b572e1SStephen Rothwell /*
619b8b572e1SStephen Rothwell  * Convert a physical pointer to a virtual kernel pointer for /dev/mem
620b8b572e1SStephen Rothwell  * access
621b8b572e1SStephen Rothwell  */
622b8b572e1SStephen Rothwell #define xlate_dev_mem_ptr(p)	__va(p)
623b8b572e1SStephen Rothwell 
624b8b572e1SStephen Rothwell /*
625b8b572e1SStephen Rothwell  * Convert a virtual cached pointer to an uncached pointer
626b8b572e1SStephen Rothwell  */
627b8b572e1SStephen Rothwell #define xlate_dev_kmem_ptr(p)	p
628b8b572e1SStephen Rothwell 
629b8b572e1SStephen Rothwell /*
630b8b572e1SStephen Rothwell  * We don't do relaxed operations yet, at least not with this semantic
631b8b572e1SStephen Rothwell  */
632b8b572e1SStephen Rothwell #define readb_relaxed(addr)	readb(addr)
633b8b572e1SStephen Rothwell #define readw_relaxed(addr)	readw(addr)
634b8b572e1SStephen Rothwell #define readl_relaxed(addr)	readl(addr)
635b8b572e1SStephen Rothwell #define readq_relaxed(addr)	readq(addr)
6365da59057SWill Deacon #define writeb_relaxed(v, addr)	writeb(v, addr)
6375da59057SWill Deacon #define writew_relaxed(v, addr)	writew(v, addr)
6385da59057SWill Deacon #define writel_relaxed(v, addr)	writel(v, addr)
6395da59057SWill Deacon #define writeq_relaxed(v, addr)	writeq(v, addr)
640b8b572e1SStephen Rothwell 
641ef237039SLogan Gunthorpe #include <asm-generic/iomap.h>
642ef237039SLogan Gunthorpe 
643b8b572e1SStephen Rothwell static inline void iosync(void)
644b8b572e1SStephen Rothwell {
645b8b572e1SStephen Rothwell         __asm__ __volatile__ ("sync" : : : "memory");
646b8b572e1SStephen Rothwell }
647b8b572e1SStephen Rothwell 
648b8b572e1SStephen Rothwell /* Enforce in-order execution of data I/O.
649b8b572e1SStephen Rothwell  * No distinction between read/write on PPC; use eieio for all three.
650b8b572e1SStephen Rothwell  * Those are fairly week though. They don't provide a barrier between
651b8b572e1SStephen Rothwell  * MMIO and cacheable storage nor do they provide a barrier vs. locks,
652b8b572e1SStephen Rothwell  * they only provide barriers between 2 __raw MMIO operations and
653b8b572e1SStephen Rothwell  * possibly break write combining.
654b8b572e1SStephen Rothwell  */
655b8b572e1SStephen Rothwell #define iobarrier_rw() eieio()
656b8b572e1SStephen Rothwell #define iobarrier_r()  eieio()
657b8b572e1SStephen Rothwell #define iobarrier_w()  eieio()
658b8b572e1SStephen Rothwell 
659b8b572e1SStephen Rothwell 
660b8b572e1SStephen Rothwell /*
661b8b572e1SStephen Rothwell  * output pause versions need a delay at least for the
662b8b572e1SStephen Rothwell  * w83c105 ide controller in a p610.
663b8b572e1SStephen Rothwell  */
664b8b572e1SStephen Rothwell #define inb_p(port)             inb(port)
665b8b572e1SStephen Rothwell #define outb_p(val, port)       (udelay(1), outb((val), (port)))
666b8b572e1SStephen Rothwell #define inw_p(port)             inw(port)
667b8b572e1SStephen Rothwell #define outw_p(val, port)       (udelay(1), outw((val), (port)))
668b8b572e1SStephen Rothwell #define inl_p(port)             inl(port)
669b8b572e1SStephen Rothwell #define outl_p(val, port)       (udelay(1), outl((val), (port)))
670b8b572e1SStephen Rothwell 
671b8b572e1SStephen Rothwell 
672b8b572e1SStephen Rothwell #define IO_SPACE_LIMIT ~(0UL)
673b8b572e1SStephen Rothwell 
674b8b572e1SStephen Rothwell 
675b8b572e1SStephen Rothwell /**
676b8b572e1SStephen Rothwell  * ioremap     -   map bus memory into CPU space
677b8b572e1SStephen Rothwell  * @address:   bus address of the memory
678b8b572e1SStephen Rothwell  * @size:      size of the resource to map
679b8b572e1SStephen Rothwell  *
680b8b572e1SStephen Rothwell  * ioremap performs a platform specific sequence of operations to
681b8b572e1SStephen Rothwell  * make bus memory CPU accessible via the readb/readw/readl/writeb/
682b8b572e1SStephen Rothwell  * writew/writel functions and the other mmio helpers. The returned
683b8b572e1SStephen Rothwell  * address is not guaranteed to be usable directly as a virtual
684b8b572e1SStephen Rothwell  * address.
685b8b572e1SStephen Rothwell  *
686b8b572e1SStephen Rothwell  * We provide a few variations of it:
687b8b572e1SStephen Rothwell  *
688b8b572e1SStephen Rothwell  * * ioremap is the standard one and provides non-cacheable guarded mappings
689b8b572e1SStephen Rothwell  *   and can be hooked by the platform via ppc_md
690b8b572e1SStephen Rothwell  *
69140f1ce7fSAnton Blanchard  * * ioremap_prot allows to specify the page flags as an argument and can
69240f1ce7fSAnton Blanchard  *   also be hooked by the platform via ppc_md.
693b8b572e1SStephen Rothwell  *
694b8b572e1SStephen Rothwell  * * ioremap_nocache is identical to ioremap
695b8b572e1SStephen Rothwell  *
696be135f40SAnton Blanchard  * * ioremap_wc enables write combining
697be135f40SAnton Blanchard  *
69886c391bdSChristophe Leroy  * * ioremap_wt enables write through
69986c391bdSChristophe Leroy  *
70086c391bdSChristophe Leroy  * * ioremap_coherent maps coherent cached memory
70186c391bdSChristophe Leroy  *
702b8b572e1SStephen Rothwell  * * iounmap undoes such a mapping and can be hooked
703b8b572e1SStephen Rothwell  *
704b8b572e1SStephen Rothwell  * * __ioremap_at (and the pending __iounmap_at) are low level functions to
705b8b572e1SStephen Rothwell  *   create hand-made mappings for use only by the PCI code and cannot
706b8b572e1SStephen Rothwell  *   currently be hooked. Must be page aligned.
707b8b572e1SStephen Rothwell  *
7081cdab55dSBenjamin Herrenschmidt  * * __ioremap_caller is the same as above but takes an explicit caller
7091cdab55dSBenjamin Herrenschmidt  *   reference rather than using __builtin_return_address(0)
7101cdab55dSBenjamin Herrenschmidt  *
711b8b572e1SStephen Rothwell  */
712b8b572e1SStephen Rothwell extern void __iomem *ioremap(phys_addr_t address, unsigned long size);
71340f1ce7fSAnton Blanchard extern void __iomem *ioremap_prot(phys_addr_t address, unsigned long size,
714b8b572e1SStephen Rothwell 				  unsigned long flags);
715be135f40SAnton Blanchard extern void __iomem *ioremap_wc(phys_addr_t address, unsigned long size);
71686c391bdSChristophe Leroy void __iomem *ioremap_wt(phys_addr_t address, unsigned long size);
71786c391bdSChristophe Leroy void __iomem *ioremap_coherent(phys_addr_t address, unsigned long size);
718b8b572e1SStephen Rothwell #define ioremap_nocache(addr, size)	ioremap((addr), (size))
7194c73e892SLuis R. Rodriguez #define ioremap_uc(addr, size)		ioremap((addr), (size))
720f855b2f5SOliver O'Halloran #define ioremap_cache(addr, size) \
721f855b2f5SOliver O'Halloran 	ioremap_prot((addr), (size), pgprot_val(PAGE_KERNEL))
722b8b572e1SStephen Rothwell 
723b8b572e1SStephen Rothwell extern void iounmap(volatile void __iomem *addr);
724b8b572e1SStephen Rothwell 
725*191e4206SChristophe Leroy int ioremap_range(unsigned long ea, phys_addr_t pa, unsigned long size, pgprot_t prot);
726*191e4206SChristophe Leroy 
7271cdab55dSBenjamin Herrenschmidt extern void __iomem *__ioremap_caller(phys_addr_t, unsigned long size,
728c766ee72SChristophe Leroy 				      pgprot_t prot, void *caller);
7291cdab55dSBenjamin Herrenschmidt 
730b8b572e1SStephen Rothwell extern void __iomem * __ioremap_at(phys_addr_t pa, void *ea,
731c766ee72SChristophe Leroy 				   unsigned long size, pgprot_t prot);
732b8b572e1SStephen Rothwell extern void __iounmap_at(void *ea, unsigned long size);
733b8b572e1SStephen Rothwell 
734b8b572e1SStephen Rothwell /*
735ecd73cc5SBenjamin Herrenschmidt  * When CONFIG_PPC_INDIRECT_PIO is set, we use the generic iomap implementation
736b8b572e1SStephen Rothwell  * which needs some additional definitions here. They basically allow PIO
737b8b572e1SStephen Rothwell  * space overall to be 1GB. This will work as long as we never try to use
738b8b572e1SStephen Rothwell  * iomap to map MMIO below 1GB which should be fine on ppc64
739b8b572e1SStephen Rothwell  */
740b8b572e1SStephen Rothwell #define HAVE_ARCH_PIO_SIZE		1
741b8b572e1SStephen Rothwell #define PIO_OFFSET			0x00000000UL
742b8b572e1SStephen Rothwell #define PIO_MASK			(FULL_IO_SIZE - 1)
743b8b572e1SStephen Rothwell #define PIO_RESERVED			(FULL_IO_SIZE)
744b8b572e1SStephen Rothwell 
745b8b572e1SStephen Rothwell #define mmio_read16be(addr)		readw_be(addr)
746b8b572e1SStephen Rothwell #define mmio_read32be(addr)		readl_be(addr)
74779bf0cbdSLogan Gunthorpe #define mmio_read64be(addr)		readq_be(addr)
748b8b572e1SStephen Rothwell #define mmio_write16be(val, addr)	writew_be(val, addr)
749b8b572e1SStephen Rothwell #define mmio_write32be(val, addr)	writel_be(val, addr)
75079bf0cbdSLogan Gunthorpe #define mmio_write64be(val, addr)	writeq_be(val, addr)
751b8b572e1SStephen Rothwell #define mmio_insb(addr, dst, count)	readsb(addr, dst, count)
752b8b572e1SStephen Rothwell #define mmio_insw(addr, dst, count)	readsw(addr, dst, count)
753b8b572e1SStephen Rothwell #define mmio_insl(addr, dst, count)	readsl(addr, dst, count)
754b8b572e1SStephen Rothwell #define mmio_outsb(addr, src, count)	writesb(addr, src, count)
755b8b572e1SStephen Rothwell #define mmio_outsw(addr, src, count)	writesw(addr, src, count)
756b8b572e1SStephen Rothwell #define mmio_outsl(addr, src, count)	writesl(addr, src, count)
757b8b572e1SStephen Rothwell 
758b8b572e1SStephen Rothwell /**
759b8b572e1SStephen Rothwell  *	virt_to_phys	-	map virtual addresses to physical
760b8b572e1SStephen Rothwell  *	@address: address to remap
761b8b572e1SStephen Rothwell  *
762b8b572e1SStephen Rothwell  *	The returned physical address is the physical (CPU) mapping for
763b8b572e1SStephen Rothwell  *	the memory address given. It is only valid to use this function on
764b8b572e1SStephen Rothwell  *	addresses directly mapped or allocated via kmalloc.
765b8b572e1SStephen Rothwell  *
766b8b572e1SStephen Rothwell  *	This function does not give bus mappings for DMA transfers. In
767b8b572e1SStephen Rothwell  *	almost all conceivable cases a device driver should not be using
768b8b572e1SStephen Rothwell  *	this function
769b8b572e1SStephen Rothwell  */
770b8b572e1SStephen Rothwell static inline unsigned long virt_to_phys(volatile void * address)
771b8b572e1SStephen Rothwell {
7726bf752daSChristophe Leroy 	WARN_ON(IS_ENABLED(CONFIG_DEBUG_VIRTUAL) && !virt_addr_valid(address));
7736bf752daSChristophe Leroy 
774b8b572e1SStephen Rothwell 	return __pa((unsigned long)address);
775b8b572e1SStephen Rothwell }
776b8b572e1SStephen Rothwell 
777b8b572e1SStephen Rothwell /**
778b8b572e1SStephen Rothwell  *	phys_to_virt	-	map physical address to virtual
779b8b572e1SStephen Rothwell  *	@address: address to remap
780b8b572e1SStephen Rothwell  *
781b8b572e1SStephen Rothwell  *	The returned virtual address is a current CPU mapping for
782b8b572e1SStephen Rothwell  *	the memory address given. It is only valid to use this function on
783b8b572e1SStephen Rothwell  *	addresses that have a kernel mapping
784b8b572e1SStephen Rothwell  *
785b8b572e1SStephen Rothwell  *	This function does not handle bus mappings for DMA transfers. In
786b8b572e1SStephen Rothwell  *	almost all conceivable cases a device driver should not be using
787b8b572e1SStephen Rothwell  *	this function
788b8b572e1SStephen Rothwell  */
789b8b572e1SStephen Rothwell static inline void * phys_to_virt(unsigned long address)
790b8b572e1SStephen Rothwell {
791b8b572e1SStephen Rothwell 	return (void *)__va(address);
792b8b572e1SStephen Rothwell }
793b8b572e1SStephen Rothwell 
794b8b572e1SStephen Rothwell /*
795b8b572e1SStephen Rothwell  * Change "struct page" to physical address.
796b8b572e1SStephen Rothwell  */
7976bf752daSChristophe Leroy static inline phys_addr_t page_to_phys(struct page *page)
7986bf752daSChristophe Leroy {
7996bf752daSChristophe Leroy 	unsigned long pfn = page_to_pfn(page);
8006bf752daSChristophe Leroy 
8016bf752daSChristophe Leroy 	WARN_ON(IS_ENABLED(CONFIG_DEBUG_VIRTUAL) && !pfn_valid(pfn));
8026bf752daSChristophe Leroy 
8036bf752daSChristophe Leroy 	return PFN_PHYS(pfn);
8046bf752daSChristophe Leroy }
805b8b572e1SStephen Rothwell 
806b8b572e1SStephen Rothwell /*
807b8b572e1SStephen Rothwell  * 32 bits still uses virt_to_bus() for it's implementation of DMA
808b8b572e1SStephen Rothwell  * mappings se we have to keep it defined here. We also have some old
809b8b572e1SStephen Rothwell  * drivers (shame shame shame) that use bus_to_virt() and haven't been
810b8b572e1SStephen Rothwell  * fixed yet so I need to define it here.
811b8b572e1SStephen Rothwell  */
812b8b572e1SStephen Rothwell #ifdef CONFIG_PPC32
813b8b572e1SStephen Rothwell 
814b8b572e1SStephen Rothwell static inline unsigned long virt_to_bus(volatile void * address)
815b8b572e1SStephen Rothwell {
816b8b572e1SStephen Rothwell         if (address == NULL)
817b8b572e1SStephen Rothwell 		return 0;
818b8b572e1SStephen Rothwell         return __pa(address) + PCI_DRAM_OFFSET;
819b8b572e1SStephen Rothwell }
820b8b572e1SStephen Rothwell 
821b8b572e1SStephen Rothwell static inline void * bus_to_virt(unsigned long address)
822b8b572e1SStephen Rothwell {
823b8b572e1SStephen Rothwell         if (address == 0)
824b8b572e1SStephen Rothwell 		return NULL;
825b8b572e1SStephen Rothwell         return __va(address - PCI_DRAM_OFFSET);
826b8b572e1SStephen Rothwell }
827b8b572e1SStephen Rothwell 
828b8b572e1SStephen Rothwell #define page_to_bus(page)	(page_to_phys(page) + PCI_DRAM_OFFSET)
829b8b572e1SStephen Rothwell 
830b8b572e1SStephen Rothwell #endif /* CONFIG_PPC32 */
831b8b572e1SStephen Rothwell 
832b8b572e1SStephen Rothwell /* access ports */
833b8b572e1SStephen Rothwell #define setbits32(_addr, _v) out_be32((_addr), in_be32(_addr) |  (_v))
834b8b572e1SStephen Rothwell #define clrbits32(_addr, _v) out_be32((_addr), in_be32(_addr) & ~(_v))
835b8b572e1SStephen Rothwell 
836b8b572e1SStephen Rothwell #define setbits16(_addr, _v) out_be16((_addr), in_be16(_addr) |  (_v))
837b8b572e1SStephen Rothwell #define clrbits16(_addr, _v) out_be16((_addr), in_be16(_addr) & ~(_v))
838b8b572e1SStephen Rothwell 
839b8b572e1SStephen Rothwell #define setbits8(_addr, _v) out_8((_addr), in_8(_addr) |  (_v))
840b8b572e1SStephen Rothwell #define clrbits8(_addr, _v) out_8((_addr), in_8(_addr) & ~(_v))
841b8b572e1SStephen Rothwell 
842b8b572e1SStephen Rothwell /* Clear and set bits in one shot.  These macros can be used to clear and
843b8b572e1SStephen Rothwell  * set multiple bits in a register using a single read-modify-write.  These
844b8b572e1SStephen Rothwell  * macros can also be used to set a multiple-bit bit pattern using a mask,
845b8b572e1SStephen Rothwell  * by specifying the mask in the 'clear' parameter and the new bit pattern
846b8b572e1SStephen Rothwell  * in the 'set' parameter.
847b8b572e1SStephen Rothwell  */
848b8b572e1SStephen Rothwell 
849b8b572e1SStephen Rothwell #define clrsetbits(type, addr, clear, set) \
850b8b572e1SStephen Rothwell 	out_##type((addr), (in_##type(addr) & ~(clear)) | (set))
851b8b572e1SStephen Rothwell 
852b8b572e1SStephen Rothwell #ifdef __powerpc64__
853b8b572e1SStephen Rothwell #define clrsetbits_be64(addr, clear, set) clrsetbits(be64, addr, clear, set)
854b8b572e1SStephen Rothwell #define clrsetbits_le64(addr, clear, set) clrsetbits(le64, addr, clear, set)
855b8b572e1SStephen Rothwell #endif
856b8b572e1SStephen Rothwell 
857b8b572e1SStephen Rothwell #define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set)
858b8b572e1SStephen Rothwell #define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set)
859b8b572e1SStephen Rothwell 
860b8b572e1SStephen Rothwell #define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set)
861b8b572e1SStephen Rothwell #define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set)
862b8b572e1SStephen Rothwell 
863b8b572e1SStephen Rothwell #define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set)
864b8b572e1SStephen Rothwell 
865b8b572e1SStephen Rothwell #endif /* __KERNEL__ */
866b8b572e1SStephen Rothwell 
867b8b572e1SStephen Rothwell #endif /* _ASM_POWERPC_IO_H */
868