xref: /linux/arch/powerpc/include/asm/io.h (revision be140f1732b523947425aaafbe2e37b41b622d96)
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 
6b8b572e1SStephen Rothwell /*
7b8b572e1SStephen Rothwell  */
8b8b572e1SStephen Rothwell 
9b8b572e1SStephen Rothwell /* Check of existence of legacy devices */
10b8b572e1SStephen Rothwell extern int check_legacy_ioport(unsigned long base_port);
11b8b572e1SStephen Rothwell #define I8042_DATA_REG	0x60
12b8b572e1SStephen Rothwell #define FDC_BASE	0x3f0
13b8b572e1SStephen Rothwell 
14e1612de9SHaren Myneni #if defined(CONFIG_PPC64) && defined(CONFIG_PCI)
15e1612de9SHaren Myneni extern struct pci_dev *isa_bridge_pcidev;
16e1612de9SHaren Myneni /*
17e1612de9SHaren Myneni  * has legacy ISA devices ?
18e1612de9SHaren Myneni  */
19ac237b65SBenjamin Herrenschmidt #define arch_has_dev_port()	(isa_bridge_pcidev != NULL || isa_io_special)
20e1612de9SHaren Myneni #endif
21e1612de9SHaren Myneni 
22b8b572e1SStephen Rothwell #include <linux/device.h>
23b8b572e1SStephen Rothwell #include <linux/compiler.h>
246bf752daSChristophe Leroy #include <linux/mm.h>
25b8b572e1SStephen Rothwell #include <asm/page.h>
26b8b572e1SStephen Rothwell #include <asm/byteorder.h>
27b8b572e1SStephen Rothwell #include <asm/synch.h>
28b8b572e1SStephen Rothwell #include <asm/delay.h>
29420af155SWill Deacon #include <asm/mmiowb.h>
30b8b572e1SStephen Rothwell #include <asm/mmu.h>
31b8b572e1SStephen Rothwell 
32b8b572e1SStephen Rothwell #define SIO_CONFIG_RA	0x398
33b8b572e1SStephen Rothwell #define SIO_CONFIG_RD	0x399
34b8b572e1SStephen Rothwell 
35b8b572e1SStephen Rothwell /* 32 bits uses slightly different variables for the various IO
36b8b572e1SStephen Rothwell  * bases. Most of this file only uses _IO_BASE though which we
37b8b572e1SStephen Rothwell  * define properly based on the platform
38b8b572e1SStephen Rothwell  */
39b8b572e1SStephen Rothwell #ifndef CONFIG_PCI
40*be140f17SMichael Ellerman #define _IO_BASE	POISON_POINTER_DELTA
41b8b572e1SStephen Rothwell #define _ISA_MEM_BASE	0
42b8b572e1SStephen Rothwell #define PCI_DRAM_OFFSET 0
43b8b572e1SStephen Rothwell #elif defined(CONFIG_PPC32)
44b8b572e1SStephen Rothwell #define _IO_BASE	isa_io_base
45b8b572e1SStephen Rothwell #define _ISA_MEM_BASE	isa_mem_base
46b8b572e1SStephen Rothwell #define PCI_DRAM_OFFSET	pci_dram_offset
47b8b572e1SStephen Rothwell #else
48b8b572e1SStephen Rothwell #define _IO_BASE	pci_io_base
49b8b572e1SStephen Rothwell #define _ISA_MEM_BASE	isa_mem_base
50b8b572e1SStephen Rothwell #define PCI_DRAM_OFFSET	0
51b8b572e1SStephen Rothwell #endif
52b8b572e1SStephen Rothwell 
53b8b572e1SStephen Rothwell extern unsigned long isa_io_base;
54b8b572e1SStephen Rothwell extern unsigned long pci_io_base;
55b8b572e1SStephen Rothwell extern unsigned long pci_dram_offset;
56b8b572e1SStephen Rothwell 
57b8b572e1SStephen Rothwell extern resource_size_t isa_mem_base;
58b8b572e1SStephen Rothwell 
593fafe9c2SBenjamin Herrenschmidt /* Boolean set by platform if PIO accesses are suppored while _IO_BASE
603fafe9c2SBenjamin Herrenschmidt  * is not set or addresses cannot be translated to MMIO. This is typically
613fafe9c2SBenjamin Herrenschmidt  * set when the platform supports "special" PIO accesses via a non memory
623fafe9c2SBenjamin Herrenschmidt  * mapped mechanism, and allows things like the early udbg UART code to
633fafe9c2SBenjamin Herrenschmidt  * function.
643fafe9c2SBenjamin Herrenschmidt  */
653fafe9c2SBenjamin Herrenschmidt extern bool isa_io_special;
663fafe9c2SBenjamin Herrenschmidt 
67ecd73cc5SBenjamin Herrenschmidt #ifdef CONFIG_PPC32
68ecd73cc5SBenjamin Herrenschmidt #if defined(CONFIG_PPC_INDIRECT_PIO) || defined(CONFIG_PPC_INDIRECT_MMIO)
69ecd73cc5SBenjamin Herrenschmidt #error CONFIG_PPC_INDIRECT_{PIO,MMIO} are not yet supported on 32 bits
70ecd73cc5SBenjamin Herrenschmidt #endif
71b8b572e1SStephen Rothwell #endif
72b8b572e1SStephen Rothwell 
73b8b572e1SStephen Rothwell /*
74b8b572e1SStephen Rothwell  *
75b8b572e1SStephen Rothwell  * Low level MMIO accessors
76b8b572e1SStephen Rothwell  *
77b8b572e1SStephen Rothwell  * This provides the non-bus specific accessors to MMIO. Those are PowerPC
78b8b572e1SStephen Rothwell  * specific and thus shouldn't be used in generic code. The accessors
79b8b572e1SStephen Rothwell  * provided here are:
80b8b572e1SStephen Rothwell  *
81b8b572e1SStephen Rothwell  *	in_8, in_le16, in_be16, in_le32, in_be32, in_le64, in_be64
82b8b572e1SStephen Rothwell  *	out_8, out_le16, out_be16, out_le32, out_be32, out_le64, out_be64
83b8b572e1SStephen Rothwell  *	_insb, _insw_ns, _insl_ns, _outsb, _outsw_ns, _outsl_ns
84b8b572e1SStephen Rothwell  *
85b8b572e1SStephen Rothwell  * Those operate directly on a kernel virtual address. Note that the prototype
86b8b572e1SStephen Rothwell  * for the out_* accessors has the arguments in opposite order from the usual
87b8b572e1SStephen Rothwell  * linux PCI accessors. Unlike those, they take the address first and the value
88b8b572e1SStephen Rothwell  * next.
89b8b572e1SStephen Rothwell  *
90b8b572e1SStephen Rothwell  * Note: I might drop the _ns suffix on the stream operations soon as it is
91b8b572e1SStephen Rothwell  * simply normal for stream operations to not swap in the first place.
92b8b572e1SStephen Rothwell  *
93b8b572e1SStephen Rothwell  */
94b8b572e1SStephen Rothwell 
95dc5dac74SNicholas Piggin /* -mprefixed can generate offsets beyond range, fall back hack */
96dc5dac74SNicholas Piggin #ifdef CONFIG_PPC_KERNEL_PREFIXED
97dc5dac74SNicholas Piggin #define DEF_MMIO_IN_X(name, size, insn)				\
98dc5dac74SNicholas Piggin static inline u##size name(const volatile u##size __iomem *addr)	\
99dc5dac74SNicholas Piggin {									\
100dc5dac74SNicholas Piggin 	u##size ret;							\
101dc5dac74SNicholas Piggin 	__asm__ __volatile__("sync;"#insn" %0,0,%1;twi 0,%0,0;isync"	\
102dc5dac74SNicholas Piggin 		: "=r" (ret) : "r" (addr) : "memory");			\
103dc5dac74SNicholas Piggin 	return ret;							\
104dc5dac74SNicholas Piggin }
105dc5dac74SNicholas Piggin 
106dc5dac74SNicholas Piggin #define DEF_MMIO_OUT_X(name, size, insn)				\
107dc5dac74SNicholas Piggin static inline void name(volatile u##size __iomem *addr, u##size val)	\
108dc5dac74SNicholas Piggin {									\
109dc5dac74SNicholas Piggin 	__asm__ __volatile__("sync;"#insn" %1,0,%0"			\
110dc5dac74SNicholas Piggin 		: : "r" (addr), "r" (val) : "memory");			\
111dc5dac74SNicholas Piggin 	mmiowb_set_pending();						\
112dc5dac74SNicholas Piggin }
113dc5dac74SNicholas Piggin 
114dc5dac74SNicholas Piggin #define DEF_MMIO_IN_D(name, size, insn)				\
115dc5dac74SNicholas Piggin static inline u##size name(const volatile u##size __iomem *addr)	\
116dc5dac74SNicholas Piggin {									\
117dc5dac74SNicholas Piggin 	u##size ret;							\
118dc5dac74SNicholas Piggin 	__asm__ __volatile__("sync;"#insn" %0,0(%1);twi 0,%0,0;isync"\
119dc5dac74SNicholas Piggin 		: "=r" (ret) : "b" (addr) : "memory");	\
120dc5dac74SNicholas Piggin 	return ret;							\
121dc5dac74SNicholas Piggin }
122dc5dac74SNicholas Piggin 
123dc5dac74SNicholas Piggin #define DEF_MMIO_OUT_D(name, size, insn)				\
124dc5dac74SNicholas Piggin static inline void name(volatile u##size __iomem *addr, u##size val)	\
125dc5dac74SNicholas Piggin {									\
126dc5dac74SNicholas Piggin 	__asm__ __volatile__("sync;"#insn" %1,0(%0)"			\
127dc5dac74SNicholas Piggin 		: : "b" (addr), "r" (val) : "memory");	\
128dc5dac74SNicholas Piggin 	mmiowb_set_pending();						\
129dc5dac74SNicholas Piggin }
130dc5dac74SNicholas Piggin #else
13115cba23eSIan Munsie #define DEF_MMIO_IN_X(name, size, insn)				\
132b8b572e1SStephen Rothwell static inline u##size name(const volatile u##size __iomem *addr)	\
133b8b572e1SStephen Rothwell {									\
134b8b572e1SStephen Rothwell 	u##size ret;							\
135b8b572e1SStephen Rothwell 	__asm__ __volatile__("sync;"#insn" %0,%y1;twi 0,%0,0;isync"	\
136b8b572e1SStephen Rothwell 		: "=r" (ret) : "Z" (*addr) : "memory");			\
137b8b572e1SStephen Rothwell 	return ret;							\
138b8b572e1SStephen Rothwell }
139b8b572e1SStephen Rothwell 
14015cba23eSIan Munsie #define DEF_MMIO_OUT_X(name, size, insn)				\
141b8b572e1SStephen Rothwell static inline void name(volatile u##size __iomem *addr, u##size val)	\
142b8b572e1SStephen Rothwell {									\
143b8b572e1SStephen Rothwell 	__asm__ __volatile__("sync;"#insn" %1,%y0"			\
144b8b572e1SStephen Rothwell 		: "=Z" (*addr) : "r" (val) : "memory");			\
145420af155SWill Deacon 	mmiowb_set_pending();						\
146b8b572e1SStephen Rothwell }
147b8b572e1SStephen Rothwell 
14815cba23eSIan Munsie #define DEF_MMIO_IN_D(name, size, insn)				\
149b8b572e1SStephen Rothwell static inline u##size name(const volatile u##size __iomem *addr)	\
150b8b572e1SStephen Rothwell {									\
151b8b572e1SStephen Rothwell 	u##size ret;							\
152b8b572e1SStephen Rothwell 	__asm__ __volatile__("sync;"#insn"%U1%X1 %0,%1;twi 0,%0,0;isync"\
1532a24d80fSNick Desaulniers 		: "=r" (ret) : "m<>" (*addr) : "memory");	\
154b8b572e1SStephen Rothwell 	return ret;							\
155b8b572e1SStephen Rothwell }
156b8b572e1SStephen Rothwell 
15715cba23eSIan Munsie #define DEF_MMIO_OUT_D(name, size, insn)				\
158b8b572e1SStephen Rothwell static inline void name(volatile u##size __iomem *addr, u##size val)	\
159b8b572e1SStephen Rothwell {									\
160b8b572e1SStephen Rothwell 	__asm__ __volatile__("sync;"#insn"%U0%X0 %1,%0"			\
1612a24d80fSNick Desaulniers 		: "=m<>" (*addr) : "r" (val) : "memory");	\
162420af155SWill Deacon 	mmiowb_set_pending();						\
163b8b572e1SStephen Rothwell }
164dc5dac74SNicholas Piggin #endif
165b8b572e1SStephen Rothwell 
16615cba23eSIan Munsie DEF_MMIO_IN_D(in_8,     8, lbz);
16715cba23eSIan Munsie DEF_MMIO_OUT_D(out_8,   8, stb);
168b8b572e1SStephen Rothwell 
16915cba23eSIan Munsie #ifdef __BIG_ENDIAN__
17015cba23eSIan Munsie DEF_MMIO_IN_D(in_be16, 16, lhz);
17115cba23eSIan Munsie DEF_MMIO_IN_D(in_be32, 32, lwz);
17215cba23eSIan Munsie DEF_MMIO_IN_X(in_le16, 16, lhbrx);
17315cba23eSIan Munsie DEF_MMIO_IN_X(in_le32, 32, lwbrx);
174b8b572e1SStephen Rothwell 
17515cba23eSIan Munsie DEF_MMIO_OUT_D(out_be16, 16, sth);
17615cba23eSIan Munsie DEF_MMIO_OUT_D(out_be32, 32, stw);
17715cba23eSIan Munsie DEF_MMIO_OUT_X(out_le16, 16, sthbrx);
17815cba23eSIan Munsie DEF_MMIO_OUT_X(out_le32, 32, stwbrx);
17915cba23eSIan Munsie #else
18015cba23eSIan Munsie DEF_MMIO_IN_X(in_be16, 16, lhbrx);
18115cba23eSIan Munsie DEF_MMIO_IN_X(in_be32, 32, lwbrx);
18215cba23eSIan Munsie DEF_MMIO_IN_D(in_le16, 16, lhz);
18315cba23eSIan Munsie DEF_MMIO_IN_D(in_le32, 32, lwz);
18415cba23eSIan Munsie 
18515cba23eSIan Munsie DEF_MMIO_OUT_X(out_be16, 16, sthbrx);
18615cba23eSIan Munsie DEF_MMIO_OUT_X(out_be32, 32, stwbrx);
18715cba23eSIan Munsie DEF_MMIO_OUT_D(out_le16, 16, sth);
18815cba23eSIan Munsie DEF_MMIO_OUT_D(out_le32, 32, stw);
18915cba23eSIan Munsie 
19015cba23eSIan Munsie #endif /* __BIG_ENDIAN */
191b8b572e1SStephen Rothwell 
192b8b572e1SStephen Rothwell #ifdef __powerpc64__
19315cba23eSIan Munsie 
19415cba23eSIan Munsie #ifdef __BIG_ENDIAN__
19515cba23eSIan Munsie DEF_MMIO_OUT_D(out_be64, 64, std);
19615cba23eSIan Munsie DEF_MMIO_IN_D(in_be64, 64, ld);
197b8b572e1SStephen Rothwell 
198b8b572e1SStephen Rothwell /* There is no asm instructions for 64 bits reverse loads and stores */
199b8b572e1SStephen Rothwell static inline u64 in_le64(const volatile u64 __iomem *addr)
200b8b572e1SStephen Rothwell {
201b8b572e1SStephen Rothwell 	return swab64(in_be64(addr));
202b8b572e1SStephen Rothwell }
203b8b572e1SStephen Rothwell 
204b8b572e1SStephen Rothwell static inline void out_le64(volatile u64 __iomem *addr, u64 val)
205b8b572e1SStephen Rothwell {
206b8b572e1SStephen Rothwell 	out_be64(addr, swab64(val));
207b8b572e1SStephen Rothwell }
20815cba23eSIan Munsie #else
20915cba23eSIan Munsie DEF_MMIO_OUT_D(out_le64, 64, std);
21015cba23eSIan Munsie DEF_MMIO_IN_D(in_le64, 64, ld);
21115cba23eSIan Munsie 
21215cba23eSIan Munsie /* There is no asm instructions for 64 bits reverse loads and stores */
21315cba23eSIan Munsie static inline u64 in_be64(const volatile u64 __iomem *addr)
21415cba23eSIan Munsie {
21515cba23eSIan Munsie 	return swab64(in_le64(addr));
21615cba23eSIan Munsie }
21715cba23eSIan Munsie 
21815cba23eSIan Munsie static inline void out_be64(volatile u64 __iomem *addr, u64 val)
21915cba23eSIan Munsie {
22015cba23eSIan Munsie 	out_le64(addr, swab64(val));
22115cba23eSIan Munsie }
22215cba23eSIan Munsie 
22315cba23eSIan Munsie #endif
224b8b572e1SStephen Rothwell #endif /* __powerpc64__ */
225b8b572e1SStephen Rothwell 
226b8b572e1SStephen Rothwell /*
227b8b572e1SStephen Rothwell  * Low level IO stream instructions are defined out of line for now
228b8b572e1SStephen Rothwell  */
229b8b572e1SStephen Rothwell extern void _insb(const volatile u8 __iomem *addr, void *buf, long count);
230b8b572e1SStephen Rothwell extern void _outsb(volatile u8 __iomem *addr,const void *buf,long count);
231b8b572e1SStephen Rothwell extern void _insw_ns(const volatile u16 __iomem *addr, void *buf, long count);
232b8b572e1SStephen Rothwell extern void _outsw_ns(volatile u16 __iomem *addr, const void *buf, long count);
233b8b572e1SStephen Rothwell extern void _insl_ns(const volatile u32 __iomem *addr, void *buf, long count);
234b8b572e1SStephen Rothwell extern void _outsl_ns(volatile u32 __iomem *addr, const void *buf, long count);
235b8b572e1SStephen Rothwell 
236b8b572e1SStephen Rothwell /* The _ns naming is historical and will be removed. For now, just #define
237b8b572e1SStephen Rothwell  * the non _ns equivalent names
238b8b572e1SStephen Rothwell  */
239b8b572e1SStephen Rothwell #define _insw	_insw_ns
240b8b572e1SStephen Rothwell #define _insl	_insl_ns
241b8b572e1SStephen Rothwell #define _outsw	_outsw_ns
242b8b572e1SStephen Rothwell #define _outsl	_outsl_ns
243b8b572e1SStephen Rothwell 
244b8b572e1SStephen Rothwell 
245b8b572e1SStephen Rothwell /*
246b8b572e1SStephen Rothwell  * memset_io, memcpy_toio, memcpy_fromio base implementations are out of line
247b8b572e1SStephen Rothwell  */
248b8b572e1SStephen Rothwell 
249b8b572e1SStephen Rothwell extern void _memset_io(volatile void __iomem *addr, int c, unsigned long n);
250b8b572e1SStephen Rothwell extern void _memcpy_fromio(void *dest, const volatile void __iomem *src,
251b8b572e1SStephen Rothwell 			   unsigned long n);
252b8b572e1SStephen Rothwell extern void _memcpy_toio(volatile void __iomem *dest, const void *src,
253b8b572e1SStephen Rothwell 			 unsigned long n);
254b8b572e1SStephen Rothwell 
255b8b572e1SStephen Rothwell /*
256b8b572e1SStephen Rothwell  *
257b8b572e1SStephen Rothwell  * PCI and standard ISA accessors
258b8b572e1SStephen Rothwell  *
259b8b572e1SStephen Rothwell  * Those are globally defined linux accessors for devices on PCI or ISA
260b8b572e1SStephen Rothwell  * busses. They follow the Linux defined semantics. The current implementation
261b8b572e1SStephen Rothwell  * for PowerPC is as close as possible to the x86 version of these, and thus
262b8b572e1SStephen Rothwell  * provides fairly heavy weight barriers for the non-raw versions
263b8b572e1SStephen Rothwell  *
264ecd73cc5SBenjamin Herrenschmidt  * In addition, they support a hook mechanism when CONFIG_PPC_INDIRECT_MMIO
265ecd73cc5SBenjamin Herrenschmidt  * or CONFIG_PPC_INDIRECT_PIO are set allowing the platform to provide its
266ecd73cc5SBenjamin Herrenschmidt  * own implementation of some or all of the accessors.
267b8b572e1SStephen Rothwell  */
268b8b572e1SStephen Rothwell 
269b8b572e1SStephen Rothwell /*
270b8b572e1SStephen Rothwell  * Include the EEH definitions when EEH is enabled only so they don't get
271b8b572e1SStephen Rothwell  * in the way when building for 32 bits
272b8b572e1SStephen Rothwell  */
273b8b572e1SStephen Rothwell #ifdef CONFIG_EEH
274b8b572e1SStephen Rothwell #include <asm/eeh.h>
275b8b572e1SStephen Rothwell #endif
276b8b572e1SStephen Rothwell 
277b8b572e1SStephen Rothwell /* Shortcut to the MMIO argument pointer */
278b8b572e1SStephen Rothwell #define PCI_IO_ADDR	volatile void __iomem *
279b8b572e1SStephen Rothwell 
280b8b572e1SStephen Rothwell /* Indirect IO address tokens:
281b8b572e1SStephen Rothwell  *
282ecd73cc5SBenjamin Herrenschmidt  * When CONFIG_PPC_INDIRECT_MMIO is set, the platform can provide hooks
283ecd73cc5SBenjamin Herrenschmidt  * on all MMIOs. (Note that this is all 64 bits only for now)
284b8b572e1SStephen Rothwell  *
285446957baSAdam Buchbinder  * To help platforms who may need to differentiate MMIO addresses in
286b8b572e1SStephen Rothwell  * their hooks, a bitfield is reserved for use by the platform near the
287b8b572e1SStephen Rothwell  * top of MMIO addresses (not PIO, those have to cope the hard way).
288b8b572e1SStephen Rothwell  *
28943c6494fSMichael Ellerman  * The highest address in the kernel virtual space are:
290b8b572e1SStephen Rothwell  *
29143c6494fSMichael Ellerman  *  d0003fffffffffff	# with Hash MMU
29243c6494fSMichael Ellerman  *  c00fffffffffffff	# with Radix MMU
293b8b572e1SStephen Rothwell  *
29443c6494fSMichael Ellerman  * The top 4 bits are reserved as the region ID on hash, leaving us 8 bits
29543c6494fSMichael Ellerman  * that can be used for the field.
296b8b572e1SStephen Rothwell  *
297b8b572e1SStephen Rothwell  * The direct IO mapping operations will then mask off those bits
298b8b572e1SStephen Rothwell  * before doing the actual access, though that only happen when
299ecd73cc5SBenjamin Herrenschmidt  * CONFIG_PPC_INDIRECT_MMIO is set, thus be careful when you use that
300b8b572e1SStephen Rothwell  * mechanism
301ecd73cc5SBenjamin Herrenschmidt  *
302ecd73cc5SBenjamin Herrenschmidt  * For PIO, there is a separate CONFIG_PPC_INDIRECT_PIO which makes
303ecd73cc5SBenjamin Herrenschmidt  * all PIO functions call through a hook.
304b8b572e1SStephen Rothwell  */
305b8b572e1SStephen Rothwell 
306ecd73cc5SBenjamin Herrenschmidt #ifdef CONFIG_PPC_INDIRECT_MMIO
30743c6494fSMichael Ellerman #define PCI_IO_IND_TOKEN_SHIFT	52
30843c6494fSMichael Ellerman #define PCI_IO_IND_TOKEN_MASK	(0xfful << PCI_IO_IND_TOKEN_SHIFT)
309b8b572e1SStephen Rothwell #define PCI_FIX_ADDR(addr)						\
310b8b572e1SStephen Rothwell 	((PCI_IO_ADDR)(((unsigned long)(addr)) & ~PCI_IO_IND_TOKEN_MASK))
311b8b572e1SStephen Rothwell #define PCI_GET_ADDR_TOKEN(addr)					\
312b8b572e1SStephen Rothwell 	(((unsigned long)(addr) & PCI_IO_IND_TOKEN_MASK) >> 		\
313b8b572e1SStephen Rothwell 		PCI_IO_IND_TOKEN_SHIFT)
314b8b572e1SStephen Rothwell #define PCI_SET_ADDR_TOKEN(addr, token) 				\
315b8b572e1SStephen Rothwell do {									\
316b8b572e1SStephen Rothwell 	unsigned long __a = (unsigned long)(addr);			\
317b8b572e1SStephen Rothwell 	__a &= ~PCI_IO_IND_TOKEN_MASK;					\
318b8b572e1SStephen Rothwell 	__a |= ((unsigned long)(token)) << PCI_IO_IND_TOKEN_SHIFT;	\
319b8b572e1SStephen Rothwell 	(addr) = (void __iomem *)__a;					\
320b8b572e1SStephen Rothwell } while(0)
321b8b572e1SStephen Rothwell #else
322b8b572e1SStephen Rothwell #define PCI_FIX_ADDR(addr) (addr)
323b8b572e1SStephen Rothwell #endif
324b8b572e1SStephen Rothwell 
325b8b572e1SStephen Rothwell 
326b8b572e1SStephen Rothwell /*
327b8b572e1SStephen Rothwell  * Non ordered and non-swapping "raw" accessors
328b8b572e1SStephen Rothwell  */
329b8b572e1SStephen Rothwell 
330b8b572e1SStephen Rothwell static inline unsigned char __raw_readb(const volatile void __iomem *addr)
331b8b572e1SStephen Rothwell {
332b8b572e1SStephen Rothwell 	return *(volatile unsigned char __force *)PCI_FIX_ADDR(addr);
333b8b572e1SStephen Rothwell }
334894fa235SChristophe Leroy #define __raw_readb __raw_readb
335894fa235SChristophe Leroy 
336b8b572e1SStephen Rothwell static inline unsigned short __raw_readw(const volatile void __iomem *addr)
337b8b572e1SStephen Rothwell {
338b8b572e1SStephen Rothwell 	return *(volatile unsigned short __force *)PCI_FIX_ADDR(addr);
339b8b572e1SStephen Rothwell }
340894fa235SChristophe Leroy #define __raw_readw __raw_readw
341894fa235SChristophe Leroy 
342b8b572e1SStephen Rothwell static inline unsigned int __raw_readl(const volatile void __iomem *addr)
343b8b572e1SStephen Rothwell {
344b8b572e1SStephen Rothwell 	return *(volatile unsigned int __force *)PCI_FIX_ADDR(addr);
345b8b572e1SStephen Rothwell }
346894fa235SChristophe Leroy #define __raw_readl __raw_readl
347894fa235SChristophe Leroy 
348b8b572e1SStephen Rothwell static inline void __raw_writeb(unsigned char v, volatile void __iomem *addr)
349b8b572e1SStephen Rothwell {
350b8b572e1SStephen Rothwell 	*(volatile unsigned char __force *)PCI_FIX_ADDR(addr) = v;
351b8b572e1SStephen Rothwell }
352894fa235SChristophe Leroy #define __raw_writeb __raw_writeb
353894fa235SChristophe Leroy 
354b8b572e1SStephen Rothwell static inline void __raw_writew(unsigned short v, volatile void __iomem *addr)
355b8b572e1SStephen Rothwell {
356b8b572e1SStephen Rothwell 	*(volatile unsigned short __force *)PCI_FIX_ADDR(addr) = v;
357b8b572e1SStephen Rothwell }
358894fa235SChristophe Leroy #define __raw_writew __raw_writew
359894fa235SChristophe Leroy 
360b8b572e1SStephen Rothwell static inline void __raw_writel(unsigned int v, volatile void __iomem *addr)
361b8b572e1SStephen Rothwell {
362b8b572e1SStephen Rothwell 	*(volatile unsigned int __force *)PCI_FIX_ADDR(addr) = v;
363b8b572e1SStephen Rothwell }
364894fa235SChristophe Leroy #define __raw_writel __raw_writel
365b8b572e1SStephen Rothwell 
366b8b572e1SStephen Rothwell #ifdef __powerpc64__
367b8b572e1SStephen Rothwell static inline unsigned long __raw_readq(const volatile void __iomem *addr)
368b8b572e1SStephen Rothwell {
369b8b572e1SStephen Rothwell 	return *(volatile unsigned long __force *)PCI_FIX_ADDR(addr);
370b8b572e1SStephen Rothwell }
371894fa235SChristophe Leroy #define __raw_readq __raw_readq
372894fa235SChristophe Leroy 
373b8b572e1SStephen Rothwell static inline void __raw_writeq(unsigned long v, volatile void __iomem *addr)
374b8b572e1SStephen Rothwell {
375b8b572e1SStephen Rothwell 	*(volatile unsigned long __force *)PCI_FIX_ADDR(addr) = v;
376b8b572e1SStephen Rothwell }
377894fa235SChristophe Leroy #define __raw_writeq __raw_writeq
378a84bf321SAlistair Popple 
3798056fe28SMichael Ellerman static inline void __raw_writeq_be(unsigned long v, volatile void __iomem *addr)
3808056fe28SMichael Ellerman {
3818056fe28SMichael Ellerman 	__raw_writeq((__force unsigned long)cpu_to_be64(v), addr);
3828056fe28SMichael Ellerman }
383894fa235SChristophe Leroy #define __raw_writeq_be __raw_writeq_be
3848056fe28SMichael Ellerman 
385a84bf321SAlistair Popple /*
386d381d7caSBenjamin Herrenschmidt  * Real mode versions of the above. Those instructions are only supposed
387d381d7caSBenjamin Herrenschmidt  * to be used in hypervisor real mode as per the architecture spec.
388a84bf321SAlistair Popple  */
389d381d7caSBenjamin Herrenschmidt static inline void __raw_rm_writeb(u8 val, volatile void __iomem *paddr)
390d381d7caSBenjamin Herrenschmidt {
3918667d0d6SAnders Roxell 	__asm__ __volatile__(".machine push;   \
3928667d0d6SAnders Roxell 			      .machine power6; \
3938667d0d6SAnders Roxell 			      stbcix %0,0,%1;  \
3948667d0d6SAnders Roxell 			      .machine pop;"
395d381d7caSBenjamin Herrenschmidt 		: : "r" (val), "r" (paddr) : "memory");
396d381d7caSBenjamin Herrenschmidt }
397d381d7caSBenjamin Herrenschmidt 
398d381d7caSBenjamin Herrenschmidt static inline void __raw_rm_writew(u16 val, volatile void __iomem *paddr)
399d381d7caSBenjamin Herrenschmidt {
4008667d0d6SAnders Roxell 	__asm__ __volatile__(".machine push;   \
4018667d0d6SAnders Roxell 			      .machine power6; \
4028667d0d6SAnders Roxell 			      sthcix %0,0,%1;  \
4038667d0d6SAnders Roxell 			      .machine pop;"
404d381d7caSBenjamin Herrenschmidt 		: : "r" (val), "r" (paddr) : "memory");
405d381d7caSBenjamin Herrenschmidt }
406d381d7caSBenjamin Herrenschmidt 
407d381d7caSBenjamin Herrenschmidt static inline void __raw_rm_writel(u32 val, volatile void __iomem *paddr)
408d381d7caSBenjamin Herrenschmidt {
4098667d0d6SAnders Roxell 	__asm__ __volatile__(".machine push;   \
4108667d0d6SAnders Roxell 			      .machine power6; \
4118667d0d6SAnders Roxell 			      stwcix %0,0,%1;  \
4128667d0d6SAnders Roxell 			      .machine pop;"
413d381d7caSBenjamin Herrenschmidt 		: : "r" (val), "r" (paddr) : "memory");
414d381d7caSBenjamin Herrenschmidt }
415d381d7caSBenjamin Herrenschmidt 
416a84bf321SAlistair Popple static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
417a84bf321SAlistair Popple {
4188667d0d6SAnders Roxell 	__asm__ __volatile__(".machine push;   \
4198667d0d6SAnders Roxell 			      .machine power6; \
4208667d0d6SAnders Roxell 			      stdcix %0,0,%1;  \
4218667d0d6SAnders Roxell 			      .machine pop;"
422a84bf321SAlistair Popple 		: : "r" (val), "r" (paddr) : "memory");
423a84bf321SAlistair Popple }
424a84bf321SAlistair Popple 
4258056fe28SMichael Ellerman static inline void __raw_rm_writeq_be(u64 val, volatile void __iomem *paddr)
4268056fe28SMichael Ellerman {
4278056fe28SMichael Ellerman 	__raw_rm_writeq((__force u64)cpu_to_be64(val), paddr);
4288056fe28SMichael Ellerman }
4298056fe28SMichael Ellerman 
430d381d7caSBenjamin Herrenschmidt static inline u8 __raw_rm_readb(volatile void __iomem *paddr)
431d381d7caSBenjamin Herrenschmidt {
432d381d7caSBenjamin Herrenschmidt 	u8 ret;
4338667d0d6SAnders Roxell 	__asm__ __volatile__(".machine push;   \
4348667d0d6SAnders Roxell 			      .machine power6; \
4358667d0d6SAnders Roxell 			      lbzcix %0,0, %1; \
4368667d0d6SAnders Roxell 			      .machine pop;"
437d381d7caSBenjamin Herrenschmidt 			     : "=r" (ret) : "r" (paddr) : "memory");
438d381d7caSBenjamin Herrenschmidt 	return ret;
439d381d7caSBenjamin Herrenschmidt }
440d381d7caSBenjamin Herrenschmidt 
441d381d7caSBenjamin Herrenschmidt static inline u16 __raw_rm_readw(volatile void __iomem *paddr)
442d381d7caSBenjamin Herrenschmidt {
443d381d7caSBenjamin Herrenschmidt 	u16 ret;
4448667d0d6SAnders Roxell 	__asm__ __volatile__(".machine push;   \
4458667d0d6SAnders Roxell 			      .machine power6; \
4468667d0d6SAnders Roxell 			      lhzcix %0,0, %1; \
4478667d0d6SAnders Roxell 			      .machine pop;"
448d381d7caSBenjamin Herrenschmidt 			     : "=r" (ret) : "r" (paddr) : "memory");
449d381d7caSBenjamin Herrenschmidt 	return ret;
450d381d7caSBenjamin Herrenschmidt }
451d381d7caSBenjamin Herrenschmidt 
452d381d7caSBenjamin Herrenschmidt static inline u32 __raw_rm_readl(volatile void __iomem *paddr)
453d381d7caSBenjamin Herrenschmidt {
454d381d7caSBenjamin Herrenschmidt 	u32 ret;
4558667d0d6SAnders Roxell 	__asm__ __volatile__(".machine push;   \
4568667d0d6SAnders Roxell 			      .machine power6; \
4578667d0d6SAnders Roxell 			      lwzcix %0,0, %1; \
4588667d0d6SAnders Roxell 			      .machine pop;"
459d381d7caSBenjamin Herrenschmidt 			     : "=r" (ret) : "r" (paddr) : "memory");
460d381d7caSBenjamin Herrenschmidt 	return ret;
461d381d7caSBenjamin Herrenschmidt }
462d381d7caSBenjamin Herrenschmidt 
463d381d7caSBenjamin Herrenschmidt static inline u64 __raw_rm_readq(volatile void __iomem *paddr)
464d381d7caSBenjamin Herrenschmidt {
465d381d7caSBenjamin Herrenschmidt 	u64 ret;
4668667d0d6SAnders Roxell 	__asm__ __volatile__(".machine push;   \
4678667d0d6SAnders Roxell 			      .machine power6; \
4688667d0d6SAnders Roxell 			      ldcix %0,0, %1;  \
4698667d0d6SAnders Roxell 			      .machine pop;"
470d381d7caSBenjamin Herrenschmidt 			     : "=r" (ret) : "r" (paddr) : "memory");
471d381d7caSBenjamin Herrenschmidt 	return ret;
472d381d7caSBenjamin Herrenschmidt }
473b8b572e1SStephen Rothwell #endif /* __powerpc64__ */
474b8b572e1SStephen Rothwell 
475b8b572e1SStephen Rothwell /*
476b8b572e1SStephen Rothwell  *
477b8b572e1SStephen Rothwell  * PCI PIO and MMIO accessors.
478b8b572e1SStephen Rothwell  *
479b8b572e1SStephen Rothwell  *
480b8b572e1SStephen Rothwell  * On 32 bits, PIO operations have a recovery mechanism in case they trigger
481b8b572e1SStephen Rothwell  * machine checks (which they occasionally do when probing non existing
482b8b572e1SStephen Rothwell  * IO ports on some platforms, like PowerMac and 8xx).
483b8b572e1SStephen Rothwell  * I always found it to be of dubious reliability and I am tempted to get
484b8b572e1SStephen Rothwell  * rid of it one of these days. So if you think it's important to keep it,
485b8b572e1SStephen Rothwell  * please voice up asap. We never had it for 64 bits and I do not intend
486b8b572e1SStephen Rothwell  * to port it over
487b8b572e1SStephen Rothwell  */
488b8b572e1SStephen Rothwell 
489b8b572e1SStephen Rothwell #ifdef CONFIG_PPC32
490b8b572e1SStephen Rothwell 
491b8b572e1SStephen Rothwell #define __do_in_asm(name, op)				\
492b8b572e1SStephen Rothwell static inline unsigned int name(unsigned int port)	\
493b8b572e1SStephen Rothwell {							\
494b8b572e1SStephen Rothwell 	unsigned int x;					\
495b8b572e1SStephen Rothwell 	__asm__ __volatile__(				\
496b8b572e1SStephen Rothwell 		"sync\n"				\
497b8b572e1SStephen Rothwell 		"0:"	op "	%0,0,%1\n"		\
498b8b572e1SStephen Rothwell 		"1:	twi	0,%0,0\n"		\
499b8b572e1SStephen Rothwell 		"2:	isync\n"			\
500b8b572e1SStephen Rothwell 		"3:	nop\n"				\
501b8b572e1SStephen Rothwell 		"4:\n"					\
502b8b572e1SStephen Rothwell 		".section .fixup,\"ax\"\n"		\
503b8b572e1SStephen Rothwell 		"5:	li	%0,-1\n"		\
504b8b572e1SStephen Rothwell 		"	b	4b\n"			\
505b8b572e1SStephen Rothwell 		".previous\n"				\
50624bfa6a9SNicholas Piggin 		EX_TABLE(0b, 5b)			\
50724bfa6a9SNicholas Piggin 		EX_TABLE(1b, 5b)			\
50824bfa6a9SNicholas Piggin 		EX_TABLE(2b, 5b)			\
50924bfa6a9SNicholas Piggin 		EX_TABLE(3b, 5b)			\
510b8b572e1SStephen Rothwell 		: "=&r" (x)				\
511b8b572e1SStephen Rothwell 		: "r" (port + _IO_BASE)			\
512b8b572e1SStephen Rothwell 		: "memory");  				\
513b8b572e1SStephen Rothwell 	return x;					\
514b8b572e1SStephen Rothwell }
515b8b572e1SStephen Rothwell 
516b8b572e1SStephen Rothwell #define __do_out_asm(name, op)				\
517b8b572e1SStephen Rothwell static inline void name(unsigned int val, unsigned int port) \
518b8b572e1SStephen Rothwell {							\
519b8b572e1SStephen Rothwell 	__asm__ __volatile__(				\
520b8b572e1SStephen Rothwell 		"sync\n"				\
521b8b572e1SStephen Rothwell 		"0:" op " %0,0,%1\n"			\
522b8b572e1SStephen Rothwell 		"1:	sync\n"				\
523b8b572e1SStephen Rothwell 		"2:\n"					\
52424bfa6a9SNicholas Piggin 		EX_TABLE(0b, 2b)			\
52524bfa6a9SNicholas Piggin 		EX_TABLE(1b, 2b)			\
526b8b572e1SStephen Rothwell 		: : "r" (val), "r" (port + _IO_BASE)	\
527b8b572e1SStephen Rothwell 		: "memory");   	   	   		\
528b8b572e1SStephen Rothwell }
529b8b572e1SStephen Rothwell 
530b8b572e1SStephen Rothwell __do_in_asm(_rec_inb, "lbzx")
531b8b572e1SStephen Rothwell __do_in_asm(_rec_inw, "lhbrx")
532b8b572e1SStephen Rothwell __do_in_asm(_rec_inl, "lwbrx")
533b8b572e1SStephen Rothwell __do_out_asm(_rec_outb, "stbx")
534b8b572e1SStephen Rothwell __do_out_asm(_rec_outw, "sthbrx")
535b8b572e1SStephen Rothwell __do_out_asm(_rec_outl, "stwbrx")
536b8b572e1SStephen Rothwell 
537b8b572e1SStephen Rothwell #endif /* CONFIG_PPC32 */
538b8b572e1SStephen Rothwell 
539b8b572e1SStephen Rothwell /* The "__do_*" operations below provide the actual "base" implementation
54042b2aa86SJustin P. Mattock  * for each of the defined accessors. Some of them use the out_* functions
541b8b572e1SStephen Rothwell  * directly, some of them still use EEH, though we might change that in the
542b8b572e1SStephen Rothwell  * future. Those macros below provide the necessary argument swapping and
543b8b572e1SStephen Rothwell  * handling of the IO base for PIO.
544b8b572e1SStephen Rothwell  *
545b8b572e1SStephen Rothwell  * They are themselves used by the macros that define the actual accessors
546b8b572e1SStephen Rothwell  * and can be used by the hooks if any.
547b8b572e1SStephen Rothwell  *
548b8b572e1SStephen Rothwell  * Note that PIO operations are always defined in terms of their corresonding
549b8b572e1SStephen Rothwell  * MMIO operations. That allows platforms like iSeries who want to modify the
550b8b572e1SStephen Rothwell  * behaviour of both to only hook on the MMIO version and get both. It's also
551b8b572e1SStephen Rothwell  * possible to hook directly at the toplevel PIO operation if they have to
552b8b572e1SStephen Rothwell  * be handled differently
553b8b572e1SStephen Rothwell  */
554b8b572e1SStephen Rothwell #define __do_writeb(val, addr)	out_8(PCI_FIX_ADDR(addr), val)
555b8b572e1SStephen Rothwell #define __do_writew(val, addr)	out_le16(PCI_FIX_ADDR(addr), val)
556b8b572e1SStephen Rothwell #define __do_writel(val, addr)	out_le32(PCI_FIX_ADDR(addr), val)
557b8b572e1SStephen Rothwell #define __do_writeq(val, addr)	out_le64(PCI_FIX_ADDR(addr), val)
558b8b572e1SStephen Rothwell #define __do_writew_be(val, addr) out_be16(PCI_FIX_ADDR(addr), val)
559b8b572e1SStephen Rothwell #define __do_writel_be(val, addr) out_be32(PCI_FIX_ADDR(addr), val)
560b8b572e1SStephen Rothwell #define __do_writeq_be(val, addr) out_be64(PCI_FIX_ADDR(addr), val)
561b8b572e1SStephen Rothwell 
562b8b572e1SStephen Rothwell #ifdef CONFIG_EEH
563b8b572e1SStephen Rothwell #define __do_readb(addr)	eeh_readb(PCI_FIX_ADDR(addr))
564b8b572e1SStephen Rothwell #define __do_readw(addr)	eeh_readw(PCI_FIX_ADDR(addr))
565b8b572e1SStephen Rothwell #define __do_readl(addr)	eeh_readl(PCI_FIX_ADDR(addr))
566b8b572e1SStephen Rothwell #define __do_readq(addr)	eeh_readq(PCI_FIX_ADDR(addr))
567b8b572e1SStephen Rothwell #define __do_readw_be(addr)	eeh_readw_be(PCI_FIX_ADDR(addr))
568b8b572e1SStephen Rothwell #define __do_readl_be(addr)	eeh_readl_be(PCI_FIX_ADDR(addr))
569b8b572e1SStephen Rothwell #define __do_readq_be(addr)	eeh_readq_be(PCI_FIX_ADDR(addr))
570b8b572e1SStephen Rothwell #else /* CONFIG_EEH */
571b8b572e1SStephen Rothwell #define __do_readb(addr)	in_8(PCI_FIX_ADDR(addr))
572b8b572e1SStephen Rothwell #define __do_readw(addr)	in_le16(PCI_FIX_ADDR(addr))
573b8b572e1SStephen Rothwell #define __do_readl(addr)	in_le32(PCI_FIX_ADDR(addr))
574b8b572e1SStephen Rothwell #define __do_readq(addr)	in_le64(PCI_FIX_ADDR(addr))
575b8b572e1SStephen Rothwell #define __do_readw_be(addr)	in_be16(PCI_FIX_ADDR(addr))
576b8b572e1SStephen Rothwell #define __do_readl_be(addr)	in_be32(PCI_FIX_ADDR(addr))
577b8b572e1SStephen Rothwell #define __do_readq_be(addr)	in_be64(PCI_FIX_ADDR(addr))
578b8b572e1SStephen Rothwell #endif /* !defined(CONFIG_EEH) */
579b8b572e1SStephen Rothwell 
580b8b572e1SStephen Rothwell #ifdef CONFIG_PPC32
581b8b572e1SStephen Rothwell #define __do_outb(val, port)	_rec_outb(val, port)
582b8b572e1SStephen Rothwell #define __do_outw(val, port)	_rec_outw(val, port)
583b8b572e1SStephen Rothwell #define __do_outl(val, port)	_rec_outl(val, port)
584b8b572e1SStephen Rothwell #define __do_inb(port)		_rec_inb(port)
585b8b572e1SStephen Rothwell #define __do_inw(port)		_rec_inw(port)
586b8b572e1SStephen Rothwell #define __do_inl(port)		_rec_inl(port)
587b8b572e1SStephen Rothwell #else /* CONFIG_PPC32 */
58803c0f2c2SMichael Ellerman #define __do_outb(val, port)	writeb(val,(PCI_IO_ADDR)(_IO_BASE+port));
58903c0f2c2SMichael Ellerman #define __do_outw(val, port)	writew(val,(PCI_IO_ADDR)(_IO_BASE+port));
59003c0f2c2SMichael Ellerman #define __do_outl(val, port)	writel(val,(PCI_IO_ADDR)(_IO_BASE+port));
59103c0f2c2SMichael Ellerman #define __do_inb(port)		readb((PCI_IO_ADDR)(_IO_BASE + port));
59203c0f2c2SMichael Ellerman #define __do_inw(port)		readw((PCI_IO_ADDR)(_IO_BASE + port));
59303c0f2c2SMichael Ellerman #define __do_inl(port)		readl((PCI_IO_ADDR)(_IO_BASE + port));
594b8b572e1SStephen Rothwell #endif /* !CONFIG_PPC32 */
595b8b572e1SStephen Rothwell 
596b8b572e1SStephen Rothwell #ifdef CONFIG_EEH
597b8b572e1SStephen Rothwell #define __do_readsb(a, b, n)	eeh_readsb(PCI_FIX_ADDR(a), (b), (n))
598b8b572e1SStephen Rothwell #define __do_readsw(a, b, n)	eeh_readsw(PCI_FIX_ADDR(a), (b), (n))
599b8b572e1SStephen Rothwell #define __do_readsl(a, b, n)	eeh_readsl(PCI_FIX_ADDR(a), (b), (n))
600b8b572e1SStephen Rothwell #else /* CONFIG_EEH */
601b8b572e1SStephen Rothwell #define __do_readsb(a, b, n)	_insb(PCI_FIX_ADDR(a), (b), (n))
602b8b572e1SStephen Rothwell #define __do_readsw(a, b, n)	_insw(PCI_FIX_ADDR(a), (b), (n))
603b8b572e1SStephen Rothwell #define __do_readsl(a, b, n)	_insl(PCI_FIX_ADDR(a), (b), (n))
604b8b572e1SStephen Rothwell #endif /* !CONFIG_EEH */
605b8b572e1SStephen Rothwell #define __do_writesb(a, b, n)	_outsb(PCI_FIX_ADDR(a),(b),(n))
606b8b572e1SStephen Rothwell #define __do_writesw(a, b, n)	_outsw(PCI_FIX_ADDR(a),(b),(n))
607b8b572e1SStephen Rothwell #define __do_writesl(a, b, n)	_outsl(PCI_FIX_ADDR(a),(b),(n))
608b8b572e1SStephen Rothwell 
60903c0f2c2SMichael Ellerman #define __do_insb(p, b, n)	readsb((PCI_IO_ADDR)(_IO_BASE+(p)), (b), (n))
61003c0f2c2SMichael Ellerman #define __do_insw(p, b, n)	readsw((PCI_IO_ADDR)(_IO_BASE+(p)), (b), (n))
61103c0f2c2SMichael Ellerman #define __do_insl(p, b, n)	readsl((PCI_IO_ADDR)(_IO_BASE+(p)), (b), (n))
61203c0f2c2SMichael Ellerman #define __do_outsb(p, b, n)	writesb((PCI_IO_ADDR)(_IO_BASE+(p)),(b),(n))
61303c0f2c2SMichael Ellerman #define __do_outsw(p, b, n)	writesw((PCI_IO_ADDR)(_IO_BASE+(p)),(b),(n))
61403c0f2c2SMichael Ellerman #define __do_outsl(p, b, n)	writesl((PCI_IO_ADDR)(_IO_BASE+(p)),(b),(n))
615b8b572e1SStephen Rothwell 
616b8b572e1SStephen Rothwell #define __do_memset_io(addr, c, n)	\
617b8b572e1SStephen Rothwell 				_memset_io(PCI_FIX_ADDR(addr), c, n)
618b8b572e1SStephen Rothwell #define __do_memcpy_toio(dst, src, n)	\
619b8b572e1SStephen Rothwell 				_memcpy_toio(PCI_FIX_ADDR(dst), src, n)
620b8b572e1SStephen Rothwell 
621b8b572e1SStephen Rothwell #ifdef CONFIG_EEH
622b8b572e1SStephen Rothwell #define __do_memcpy_fromio(dst, src, n)	\
623b8b572e1SStephen Rothwell 				eeh_memcpy_fromio(dst, PCI_FIX_ADDR(src), n)
624b8b572e1SStephen Rothwell #else /* CONFIG_EEH */
625b8b572e1SStephen Rothwell #define __do_memcpy_fromio(dst, src, n)	\
626b8b572e1SStephen Rothwell 				_memcpy_fromio(dst,PCI_FIX_ADDR(src),n)
627b8b572e1SStephen Rothwell #endif /* !CONFIG_EEH */
628b8b572e1SStephen Rothwell 
62921176fedSMichael Ellerman #ifdef CONFIG_PPC_INDIRECT_PIO
63021176fedSMichael Ellerman #define DEF_PCI_HOOK_pio(x)	x
631b8b572e1SStephen Rothwell #else
63221176fedSMichael Ellerman #define DEF_PCI_HOOK_pio(x)	NULL
63321176fedSMichael Ellerman #endif
63421176fedSMichael Ellerman 
63521176fedSMichael Ellerman #ifdef CONFIG_PPC_INDIRECT_MMIO
63621176fedSMichael Ellerman #define DEF_PCI_HOOK_mem(x)	x
63721176fedSMichael Ellerman #else
63821176fedSMichael Ellerman #define DEF_PCI_HOOK_mem(x)	NULL
639b8b572e1SStephen Rothwell #endif
640b8b572e1SStephen Rothwell 
641b8b572e1SStephen Rothwell /* Structure containing all the hooks */
642b8b572e1SStephen Rothwell extern struct ppc_pci_io {
643b8b572e1SStephen Rothwell 
644b8b572e1SStephen Rothwell #define DEF_PCI_AC_RET(name, ret, at, al, space, aa)	ret (*name) at;
645b8b572e1SStephen Rothwell #define DEF_PCI_AC_NORET(name, at, al, space, aa)	void (*name) at;
646b8b572e1SStephen Rothwell 
647b8b572e1SStephen Rothwell #include <asm/io-defs.h>
648b8b572e1SStephen Rothwell 
649b8b572e1SStephen Rothwell #undef DEF_PCI_AC_RET
650b8b572e1SStephen Rothwell #undef DEF_PCI_AC_NORET
651b8b572e1SStephen Rothwell 
652b8b572e1SStephen Rothwell } ppc_pci_io;
653b8b572e1SStephen Rothwell 
654b8b572e1SStephen Rothwell /* The inline wrappers */
655b8b572e1SStephen Rothwell #define DEF_PCI_AC_RET(name, ret, at, al, space, aa)		\
656b8b572e1SStephen Rothwell static inline ret name at					\
657b8b572e1SStephen Rothwell {								\
65821176fedSMichael Ellerman 	if (DEF_PCI_HOOK_##space(ppc_pci_io.name) != NULL)	\
659b8b572e1SStephen Rothwell 		return ppc_pci_io.name al;			\
660b8b572e1SStephen Rothwell 	return __do_##name al;					\
661b8b572e1SStephen Rothwell }
662b8b572e1SStephen Rothwell 
663b8b572e1SStephen Rothwell #define DEF_PCI_AC_NORET(name, at, al, space, aa)		\
664b8b572e1SStephen Rothwell static inline void name at					\
665b8b572e1SStephen Rothwell {								\
66621176fedSMichael Ellerman 	if (DEF_PCI_HOOK_##space(ppc_pci_io.name) != NULL)		\
667b8b572e1SStephen Rothwell 		ppc_pci_io.name al;				\
668b8b572e1SStephen Rothwell 	else							\
669b8b572e1SStephen Rothwell 		__do_##name al;					\
670b8b572e1SStephen Rothwell }
671b8b572e1SStephen Rothwell 
672b8b572e1SStephen Rothwell #include <asm/io-defs.h>
673b8b572e1SStephen Rothwell 
674b8b572e1SStephen Rothwell #undef DEF_PCI_AC_RET
675b8b572e1SStephen Rothwell #undef DEF_PCI_AC_NORET
676b8b572e1SStephen Rothwell 
677b8b572e1SStephen Rothwell /* Some drivers check for the presence of readq & writeq with
678b8b572e1SStephen Rothwell  * a #ifdef, so we make them happy here.
679b8b572e1SStephen Rothwell  */
680894fa235SChristophe Leroy #define readb readb
681894fa235SChristophe Leroy #define readw readw
682894fa235SChristophe Leroy #define readl readl
683894fa235SChristophe Leroy #define writeb writeb
684894fa235SChristophe Leroy #define writew writew
685894fa235SChristophe Leroy #define writel writel
686894fa235SChristophe Leroy #define readsb readsb
687894fa235SChristophe Leroy #define readsw readsw
688894fa235SChristophe Leroy #define readsl readsl
689894fa235SChristophe Leroy #define writesb writesb
690894fa235SChristophe Leroy #define writesw writesw
691894fa235SChristophe Leroy #define writesl writesl
692894fa235SChristophe Leroy #define inb inb
693894fa235SChristophe Leroy #define inw inw
694894fa235SChristophe Leroy #define inl inl
695894fa235SChristophe Leroy #define outb outb
696894fa235SChristophe Leroy #define outw outw
697894fa235SChristophe Leroy #define outl outl
698894fa235SChristophe Leroy #define insb insb
699894fa235SChristophe Leroy #define insw insw
700894fa235SChristophe Leroy #define insl insl
701894fa235SChristophe Leroy #define outsb outsb
702894fa235SChristophe Leroy #define outsw outsw
703894fa235SChristophe Leroy #define outsl outsl
704b8b572e1SStephen Rothwell #ifdef __powerpc64__
705b8b572e1SStephen Rothwell #define readq	readq
706b8b572e1SStephen Rothwell #define writeq	writeq
707b8b572e1SStephen Rothwell #endif
708894fa235SChristophe Leroy #define memset_io memset_io
709894fa235SChristophe Leroy #define memcpy_fromio memcpy_fromio
710894fa235SChristophe Leroy #define memcpy_toio memcpy_toio
711b8b572e1SStephen Rothwell 
712b8b572e1SStephen Rothwell /*
713b8b572e1SStephen Rothwell  * We don't do relaxed operations yet, at least not with this semantic
714b8b572e1SStephen Rothwell  */
715b8b572e1SStephen Rothwell #define readb_relaxed(addr)	readb(addr)
716b8b572e1SStephen Rothwell #define readw_relaxed(addr)	readw(addr)
717b8b572e1SStephen Rothwell #define readl_relaxed(addr)	readl(addr)
718b8b572e1SStephen Rothwell #define readq_relaxed(addr)	readq(addr)
7195da59057SWill Deacon #define writeb_relaxed(v, addr)	writeb(v, addr)
7205da59057SWill Deacon #define writew_relaxed(v, addr)	writew(v, addr)
7215da59057SWill Deacon #define writel_relaxed(v, addr)	writel(v, addr)
7225da59057SWill Deacon #define writeq_relaxed(v, addr)	writeq(v, addr)
723b8b572e1SStephen Rothwell 
7240b1f77e7SBaoquan He #ifndef CONFIG_GENERIC_IOMAP
725894fa235SChristophe Leroy /*
726894fa235SChristophe Leroy  * Here comes the implementation of the IOMAP interfaces.
727894fa235SChristophe Leroy  */
728894fa235SChristophe Leroy static inline unsigned int ioread16be(const void __iomem *addr)
729894fa235SChristophe Leroy {
730894fa235SChristophe Leroy 	return readw_be(addr);
731894fa235SChristophe Leroy }
732894fa235SChristophe Leroy #define ioread16be ioread16be
733894fa235SChristophe Leroy 
734894fa235SChristophe Leroy static inline unsigned int ioread32be(const void __iomem *addr)
735894fa235SChristophe Leroy {
736894fa235SChristophe Leroy 	return readl_be(addr);
737894fa235SChristophe Leroy }
738894fa235SChristophe Leroy #define ioread32be ioread32be
739894fa235SChristophe Leroy 
740894fa235SChristophe Leroy #ifdef __powerpc64__
741894fa235SChristophe Leroy static inline u64 ioread64_lo_hi(const void __iomem *addr)
742894fa235SChristophe Leroy {
743894fa235SChristophe Leroy 	return readq(addr);
744894fa235SChristophe Leroy }
745894fa235SChristophe Leroy #define ioread64_lo_hi ioread64_lo_hi
746894fa235SChristophe Leroy 
747894fa235SChristophe Leroy static inline u64 ioread64_hi_lo(const void __iomem *addr)
748894fa235SChristophe Leroy {
749894fa235SChristophe Leroy 	return readq(addr);
750894fa235SChristophe Leroy }
751894fa235SChristophe Leroy #define ioread64_hi_lo ioread64_hi_lo
752894fa235SChristophe Leroy 
753894fa235SChristophe Leroy static inline u64 ioread64be(const void __iomem *addr)
754894fa235SChristophe Leroy {
755894fa235SChristophe Leroy 	return readq_be(addr);
756894fa235SChristophe Leroy }
757894fa235SChristophe Leroy #define ioread64be ioread64be
758894fa235SChristophe Leroy 
759894fa235SChristophe Leroy static inline u64 ioread64be_lo_hi(const void __iomem *addr)
760894fa235SChristophe Leroy {
761894fa235SChristophe Leroy 	return readq_be(addr);
762894fa235SChristophe Leroy }
763894fa235SChristophe Leroy #define ioread64be_lo_hi ioread64be_lo_hi
764894fa235SChristophe Leroy 
765894fa235SChristophe Leroy static inline u64 ioread64be_hi_lo(const void __iomem *addr)
766894fa235SChristophe Leroy {
767894fa235SChristophe Leroy 	return readq_be(addr);
768894fa235SChristophe Leroy }
769894fa235SChristophe Leroy #define ioread64be_hi_lo ioread64be_hi_lo
770894fa235SChristophe Leroy #endif /* __powerpc64__ */
771894fa235SChristophe Leroy 
772894fa235SChristophe Leroy static inline void iowrite16be(u16 val, void __iomem *addr)
773894fa235SChristophe Leroy {
774894fa235SChristophe Leroy 	writew_be(val, addr);
775894fa235SChristophe Leroy }
776894fa235SChristophe Leroy #define iowrite16be iowrite16be
777894fa235SChristophe Leroy 
778894fa235SChristophe Leroy static inline void iowrite32be(u32 val, void __iomem *addr)
779894fa235SChristophe Leroy {
780894fa235SChristophe Leroy 	writel_be(val, addr);
781894fa235SChristophe Leroy }
782894fa235SChristophe Leroy #define iowrite32be iowrite32be
783894fa235SChristophe Leroy 
784894fa235SChristophe Leroy #ifdef __powerpc64__
785894fa235SChristophe Leroy static inline void iowrite64_lo_hi(u64 val, void __iomem *addr)
786894fa235SChristophe Leroy {
787894fa235SChristophe Leroy 	writeq(val, addr);
788894fa235SChristophe Leroy }
789894fa235SChristophe Leroy #define iowrite64_lo_hi iowrite64_lo_hi
790894fa235SChristophe Leroy 
791894fa235SChristophe Leroy static inline void iowrite64_hi_lo(u64 val, void __iomem *addr)
792894fa235SChristophe Leroy {
793894fa235SChristophe Leroy 	writeq(val, addr);
794894fa235SChristophe Leroy }
795894fa235SChristophe Leroy #define iowrite64_hi_lo iowrite64_hi_lo
796894fa235SChristophe Leroy 
797894fa235SChristophe Leroy static inline void iowrite64be(u64 val, void __iomem *addr)
798894fa235SChristophe Leroy {
799894fa235SChristophe Leroy 	writeq_be(val, addr);
800894fa235SChristophe Leroy }
801894fa235SChristophe Leroy #define iowrite64be iowrite64be
802894fa235SChristophe Leroy 
803894fa235SChristophe Leroy static inline void iowrite64be_lo_hi(u64 val, void __iomem *addr)
804894fa235SChristophe Leroy {
805894fa235SChristophe Leroy 	writeq_be(val, addr);
806894fa235SChristophe Leroy }
807894fa235SChristophe Leroy #define iowrite64be_lo_hi iowrite64be_lo_hi
808894fa235SChristophe Leroy 
809894fa235SChristophe Leroy static inline void iowrite64be_hi_lo(u64 val, void __iomem *addr)
810894fa235SChristophe Leroy {
811894fa235SChristophe Leroy 	writeq_be(val, addr);
812894fa235SChristophe Leroy }
813894fa235SChristophe Leroy #define iowrite64be_hi_lo iowrite64be_hi_lo
814894fa235SChristophe Leroy #endif /* __powerpc64__ */
815894fa235SChristophe Leroy 
816894fa235SChristophe Leroy struct pci_dev;
817894fa235SChristophe Leroy void pci_iounmap(struct pci_dev *dev, void __iomem *addr);
818894fa235SChristophe Leroy #define pci_iounmap pci_iounmap
819894fa235SChristophe Leroy void __iomem *ioport_map(unsigned long port, unsigned int len);
820894fa235SChristophe Leroy #define ioport_map ioport_map
821894fa235SChristophe Leroy #endif
822ef237039SLogan Gunthorpe 
823b8b572e1SStephen Rothwell static inline void iosync(void)
824b8b572e1SStephen Rothwell {
825b8b572e1SStephen Rothwell         __asm__ __volatile__ ("sync" : : : "memory");
826b8b572e1SStephen Rothwell }
827b8b572e1SStephen Rothwell 
828b8b572e1SStephen Rothwell /* Enforce in-order execution of data I/O.
829b8b572e1SStephen Rothwell  * No distinction between read/write on PPC; use eieio for all three.
830b8b572e1SStephen Rothwell  * Those are fairly week though. They don't provide a barrier between
831b8b572e1SStephen Rothwell  * MMIO and cacheable storage nor do they provide a barrier vs. locks,
832b8b572e1SStephen Rothwell  * they only provide barriers between 2 __raw MMIO operations and
833b8b572e1SStephen Rothwell  * possibly break write combining.
834b8b572e1SStephen Rothwell  */
835b8b572e1SStephen Rothwell #define iobarrier_rw() eieio()
836b8b572e1SStephen Rothwell #define iobarrier_r()  eieio()
837b8b572e1SStephen Rothwell #define iobarrier_w()  eieio()
838b8b572e1SStephen Rothwell 
839b8b572e1SStephen Rothwell 
840b8b572e1SStephen Rothwell /*
841b8b572e1SStephen Rothwell  * output pause versions need a delay at least for the
842b8b572e1SStephen Rothwell  * w83c105 ide controller in a p610.
843b8b572e1SStephen Rothwell  */
844b8b572e1SStephen Rothwell #define inb_p(port)             inb(port)
845b8b572e1SStephen Rothwell #define outb_p(val, port)       (udelay(1), outb((val), (port)))
846b8b572e1SStephen Rothwell #define inw_p(port)             inw(port)
847b8b572e1SStephen Rothwell #define outw_p(val, port)       (udelay(1), outw((val), (port)))
848b8b572e1SStephen Rothwell #define inl_p(port)             inl(port)
849b8b572e1SStephen Rothwell #define outl_p(val, port)       (udelay(1), outl((val), (port)))
850b8b572e1SStephen Rothwell 
851b8b572e1SStephen Rothwell 
852b8b572e1SStephen Rothwell #define IO_SPACE_LIMIT ~(0UL)
853b8b572e1SStephen Rothwell 
854b8b572e1SStephen Rothwell /**
855b8b572e1SStephen Rothwell  * ioremap     -   map bus memory into CPU space
856b8b572e1SStephen Rothwell  * @address:   bus address of the memory
857b8b572e1SStephen Rothwell  * @size:      size of the resource to map
858b8b572e1SStephen Rothwell  *
859b8b572e1SStephen Rothwell  * ioremap performs a platform specific sequence of operations to
860b8b572e1SStephen Rothwell  * make bus memory CPU accessible via the readb/readw/readl/writeb/
861b8b572e1SStephen Rothwell  * writew/writel functions and the other mmio helpers. The returned
862b8b572e1SStephen Rothwell  * address is not guaranteed to be usable directly as a virtual
863b8b572e1SStephen Rothwell  * address.
864b8b572e1SStephen Rothwell  *
865b8b572e1SStephen Rothwell  * We provide a few variations of it:
866b8b572e1SStephen Rothwell  *
867b8b572e1SStephen Rothwell  * * ioremap is the standard one and provides non-cacheable guarded mappings
868b8b572e1SStephen Rothwell  *   and can be hooked by the platform via ppc_md
869b8b572e1SStephen Rothwell  *
87040f1ce7fSAnton Blanchard  * * ioremap_prot allows to specify the page flags as an argument and can
87140f1ce7fSAnton Blanchard  *   also be hooked by the platform via ppc_md.
872b8b572e1SStephen Rothwell  *
873be135f40SAnton Blanchard  * * ioremap_wc enables write combining
874be135f40SAnton Blanchard  *
87586c391bdSChristophe Leroy  * * ioremap_wt enables write through
87686c391bdSChristophe Leroy  *
87786c391bdSChristophe Leroy  * * ioremap_coherent maps coherent cached memory
87886c391bdSChristophe Leroy  *
879b8b572e1SStephen Rothwell  * * iounmap undoes such a mapping and can be hooked
880b8b572e1SStephen Rothwell  *
8811cdab55dSBenjamin Herrenschmidt  * * __ioremap_caller is the same as above but takes an explicit caller
8821cdab55dSBenjamin Herrenschmidt  *   reference rather than using __builtin_return_address(0)
8831cdab55dSBenjamin Herrenschmidt  *
884b8b572e1SStephen Rothwell  */
885b8b572e1SStephen Rothwell extern void __iomem *ioremap(phys_addr_t address, unsigned long size);
8868d05554dSChristophe Leroy #define ioremap ioremap
8878d05554dSChristophe Leroy #define ioremap_prot ioremap_prot
888be135f40SAnton Blanchard extern void __iomem *ioremap_wc(phys_addr_t address, unsigned long size);
889894fa235SChristophe Leroy #define ioremap_wc ioremap_wc
890894fa235SChristophe Leroy 
891894fa235SChristophe Leroy #ifdef CONFIG_PPC32
89286c391bdSChristophe Leroy void __iomem *ioremap_wt(phys_addr_t address, unsigned long size);
893894fa235SChristophe Leroy #define ioremap_wt ioremap_wt
894894fa235SChristophe Leroy #endif
895894fa235SChristophe Leroy 
89686c391bdSChristophe Leroy void __iomem *ioremap_coherent(phys_addr_t address, unsigned long size);
897f855b2f5SOliver O'Halloran #define ioremap_cache(addr, size) \
898f855b2f5SOliver O'Halloran 	ioremap_prot((addr), (size), pgprot_val(PAGE_KERNEL))
899b8b572e1SStephen Rothwell 
9008d05554dSChristophe Leroy #define iounmap iounmap
901b8b572e1SStephen Rothwell 
902b274014cSChristoph Hellwig void __iomem *ioremap_phb(phys_addr_t paddr, unsigned long size);
903b274014cSChristoph Hellwig 
904163918fcSChristophe Leroy int early_ioremap_range(unsigned long ea, phys_addr_t pa,
905163918fcSChristophe Leroy 			unsigned long size, pgprot_t prot);
906191e4206SChristophe Leroy 
9071cdab55dSBenjamin Herrenschmidt extern void __iomem *__ioremap_caller(phys_addr_t, unsigned long size,
908c766ee72SChristophe Leroy 				      pgprot_t prot, void *caller);
9091cdab55dSBenjamin Herrenschmidt 
910b8b572e1SStephen Rothwell /*
911ecd73cc5SBenjamin Herrenschmidt  * When CONFIG_PPC_INDIRECT_PIO is set, we use the generic iomap implementation
912b8b572e1SStephen Rothwell  * which needs some additional definitions here. They basically allow PIO
913b8b572e1SStephen Rothwell  * space overall to be 1GB. This will work as long as we never try to use
914b8b572e1SStephen Rothwell  * iomap to map MMIO below 1GB which should be fine on ppc64
915b8b572e1SStephen Rothwell  */
916b8b572e1SStephen Rothwell #define HAVE_ARCH_PIO_SIZE		1
917b8b572e1SStephen Rothwell #define PIO_OFFSET			0x00000000UL
918b8b572e1SStephen Rothwell #define PIO_MASK			(FULL_IO_SIZE - 1)
919b8b572e1SStephen Rothwell #define PIO_RESERVED			(FULL_IO_SIZE)
920b8b572e1SStephen Rothwell 
921b8b572e1SStephen Rothwell #define mmio_read16be(addr)		readw_be(addr)
922b8b572e1SStephen Rothwell #define mmio_read32be(addr)		readl_be(addr)
92379bf0cbdSLogan Gunthorpe #define mmio_read64be(addr)		readq_be(addr)
924b8b572e1SStephen Rothwell #define mmio_write16be(val, addr)	writew_be(val, addr)
925b8b572e1SStephen Rothwell #define mmio_write32be(val, addr)	writel_be(val, addr)
92679bf0cbdSLogan Gunthorpe #define mmio_write64be(val, addr)	writeq_be(val, addr)
927b8b572e1SStephen Rothwell #define mmio_insb(addr, dst, count)	readsb(addr, dst, count)
928b8b572e1SStephen Rothwell #define mmio_insw(addr, dst, count)	readsw(addr, dst, count)
929b8b572e1SStephen Rothwell #define mmio_insl(addr, dst, count)	readsl(addr, dst, count)
930b8b572e1SStephen Rothwell #define mmio_outsb(addr, src, count)	writesb(addr, src, count)
931b8b572e1SStephen Rothwell #define mmio_outsw(addr, src, count)	writesw(addr, src, count)
932b8b572e1SStephen Rothwell #define mmio_outsl(addr, src, count)	writesl(addr, src, count)
933b8b572e1SStephen Rothwell 
934b8b572e1SStephen Rothwell /**
935b8b572e1SStephen Rothwell  *	virt_to_phys	-	map virtual addresses to physical
936b8b572e1SStephen Rothwell  *	@address: address to remap
937b8b572e1SStephen Rothwell  *
938b8b572e1SStephen Rothwell  *	The returned physical address is the physical (CPU) mapping for
939b8b572e1SStephen Rothwell  *	the memory address given. It is only valid to use this function on
940b8b572e1SStephen Rothwell  *	addresses directly mapped or allocated via kmalloc.
941b8b572e1SStephen Rothwell  *
942b8b572e1SStephen Rothwell  *	This function does not give bus mappings for DMA transfers. In
943b8b572e1SStephen Rothwell  *	almost all conceivable cases a device driver should not be using
944b8b572e1SStephen Rothwell  *	this function
945b8b572e1SStephen Rothwell  */
946b28d1ccfSStanislav Kinsburskii static inline unsigned long virt_to_phys(const volatile void * address)
947b8b572e1SStephen Rothwell {
9486bf752daSChristophe Leroy 	WARN_ON(IS_ENABLED(CONFIG_DEBUG_VIRTUAL) && !virt_addr_valid(address));
9496bf752daSChristophe Leroy 
950b8b572e1SStephen Rothwell 	return __pa((unsigned long)address);
951b8b572e1SStephen Rothwell }
952894fa235SChristophe Leroy #define virt_to_phys virt_to_phys
953b8b572e1SStephen Rothwell 
954b8b572e1SStephen Rothwell /**
955b8b572e1SStephen Rothwell  *	phys_to_virt	-	map physical address to virtual
956b8b572e1SStephen Rothwell  *	@address: address to remap
957b8b572e1SStephen Rothwell  *
958b8b572e1SStephen Rothwell  *	The returned virtual address is a current CPU mapping for
959b8b572e1SStephen Rothwell  *	the memory address given. It is only valid to use this function on
960b8b572e1SStephen Rothwell  *	addresses that have a kernel mapping
961b8b572e1SStephen Rothwell  *
962b8b572e1SStephen Rothwell  *	This function does not handle bus mappings for DMA transfers. In
963b8b572e1SStephen Rothwell  *	almost all conceivable cases a device driver should not be using
964b8b572e1SStephen Rothwell  *	this function
965b8b572e1SStephen Rothwell  */
966b8b572e1SStephen Rothwell static inline void * phys_to_virt(unsigned long address)
967b8b572e1SStephen Rothwell {
968b8b572e1SStephen Rothwell 	return (void *)__va(address);
969b8b572e1SStephen Rothwell }
970894fa235SChristophe Leroy #define phys_to_virt phys_to_virt
971b8b572e1SStephen Rothwell 
972b8b572e1SStephen Rothwell /*
973b8b572e1SStephen Rothwell  * Change "struct page" to physical address.
974b8b572e1SStephen Rothwell  */
9756bf752daSChristophe Leroy static inline phys_addr_t page_to_phys(struct page *page)
9766bf752daSChristophe Leroy {
9776bf752daSChristophe Leroy 	unsigned long pfn = page_to_pfn(page);
9786bf752daSChristophe Leroy 
9796bf752daSChristophe Leroy 	WARN_ON(IS_ENABLED(CONFIG_DEBUG_VIRTUAL) && !pfn_valid(pfn));
9806bf752daSChristophe Leroy 
9816bf752daSChristophe Leroy 	return PFN_PHYS(pfn);
9826bf752daSChristophe Leroy }
983b8b572e1SStephen Rothwell 
984b8b572e1SStephen Rothwell /*
985b8b572e1SStephen Rothwell  * 32 bits still uses virt_to_bus() for it's implementation of DMA
986b8b572e1SStephen Rothwell  * mappings se we have to keep it defined here. We also have some old
987b8b572e1SStephen Rothwell  * drivers (shame shame shame) that use bus_to_virt() and haven't been
988b8b572e1SStephen Rothwell  * fixed yet so I need to define it here.
989b8b572e1SStephen Rothwell  */
990b8b572e1SStephen Rothwell #ifdef CONFIG_PPC32
991b8b572e1SStephen Rothwell 
992b8b572e1SStephen Rothwell static inline unsigned long virt_to_bus(volatile void * address)
993b8b572e1SStephen Rothwell {
994b8b572e1SStephen Rothwell         if (address == NULL)
995b8b572e1SStephen Rothwell 		return 0;
996b8b572e1SStephen Rothwell         return __pa(address) + PCI_DRAM_OFFSET;
997b8b572e1SStephen Rothwell }
998894fa235SChristophe Leroy #define virt_to_bus virt_to_bus
999b8b572e1SStephen Rothwell 
1000b8b572e1SStephen Rothwell static inline void * bus_to_virt(unsigned long address)
1001b8b572e1SStephen Rothwell {
1002b8b572e1SStephen Rothwell         if (address == 0)
1003b8b572e1SStephen Rothwell 		return NULL;
1004b8b572e1SStephen Rothwell         return __va(address - PCI_DRAM_OFFSET);
1005b8b572e1SStephen Rothwell }
1006894fa235SChristophe Leroy #define bus_to_virt bus_to_virt
1007b8b572e1SStephen Rothwell 
1008b8b572e1SStephen Rothwell #endif /* CONFIG_PPC32 */
1009b8b572e1SStephen Rothwell 
1010b8b572e1SStephen Rothwell /* access ports */
1011b8b572e1SStephen Rothwell #define setbits32(_addr, _v) out_be32((_addr), in_be32(_addr) |  (_v))
1012b8b572e1SStephen Rothwell #define clrbits32(_addr, _v) out_be32((_addr), in_be32(_addr) & ~(_v))
1013b8b572e1SStephen Rothwell 
1014b8b572e1SStephen Rothwell #define setbits16(_addr, _v) out_be16((_addr), in_be16(_addr) |  (_v))
1015b8b572e1SStephen Rothwell #define clrbits16(_addr, _v) out_be16((_addr), in_be16(_addr) & ~(_v))
1016b8b572e1SStephen Rothwell 
1017b8b572e1SStephen Rothwell #define setbits8(_addr, _v) out_8((_addr), in_8(_addr) |  (_v))
1018b8b572e1SStephen Rothwell #define clrbits8(_addr, _v) out_8((_addr), in_8(_addr) & ~(_v))
1019b8b572e1SStephen Rothwell 
1020b8b572e1SStephen Rothwell /* Clear and set bits in one shot.  These macros can be used to clear and
1021b8b572e1SStephen Rothwell  * set multiple bits in a register using a single read-modify-write.  These
1022b8b572e1SStephen Rothwell  * macros can also be used to set a multiple-bit bit pattern using a mask,
1023b8b572e1SStephen Rothwell  * by specifying the mask in the 'clear' parameter and the new bit pattern
1024b8b572e1SStephen Rothwell  * in the 'set' parameter.
1025b8b572e1SStephen Rothwell  */
1026b8b572e1SStephen Rothwell 
1027b8b572e1SStephen Rothwell #define clrsetbits(type, addr, clear, set) \
1028b8b572e1SStephen Rothwell 	out_##type((addr), (in_##type(addr) & ~(clear)) | (set))
1029b8b572e1SStephen Rothwell 
1030b8b572e1SStephen Rothwell #ifdef __powerpc64__
1031b8b572e1SStephen Rothwell #define clrsetbits_be64(addr, clear, set) clrsetbits(be64, addr, clear, set)
1032b8b572e1SStephen Rothwell #define clrsetbits_le64(addr, clear, set) clrsetbits(le64, addr, clear, set)
1033b8b572e1SStephen Rothwell #endif
1034b8b572e1SStephen Rothwell 
1035b8b572e1SStephen Rothwell #define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set)
1036b8b572e1SStephen Rothwell #define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set)
1037b8b572e1SStephen Rothwell 
1038b8b572e1SStephen Rothwell #define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set)
1039b8b572e1SStephen Rothwell #define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set)
1040b8b572e1SStephen Rothwell 
1041b8b572e1SStephen Rothwell #define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set)
1042b8b572e1SStephen Rothwell 
1043894fa235SChristophe Leroy #include <asm-generic/io.h>
1044894fa235SChristophe Leroy 
1045b8b572e1SStephen Rothwell #endif /* __KERNEL__ */
1046b8b572e1SStephen Rothwell 
1047b8b572e1SStephen Rothwell #endif /* _ASM_POWERPC_IO_H */
1048