18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_IO_H
38c2ecf20Sopenharmony_ci#define _ASM_POWERPC_IO_H
48c2ecf20Sopenharmony_ci#ifdef __KERNEL__
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#define ARCH_HAS_IOREMAP_WC
78c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC32
88c2ecf20Sopenharmony_ci#define ARCH_HAS_IOREMAP_WT
98c2ecf20Sopenharmony_ci#endif
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/*
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/* Check of existence of legacy devices */
158c2ecf20Sopenharmony_ciextern int check_legacy_ioport(unsigned long base_port);
168c2ecf20Sopenharmony_ci#define I8042_DATA_REG	0x60
178c2ecf20Sopenharmony_ci#define FDC_BASE	0x3f0
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#if defined(CONFIG_PPC64) && defined(CONFIG_PCI)
208c2ecf20Sopenharmony_ciextern struct pci_dev *isa_bridge_pcidev;
218c2ecf20Sopenharmony_ci/*
228c2ecf20Sopenharmony_ci * has legacy ISA devices ?
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci#define arch_has_dev_port()	(isa_bridge_pcidev != NULL || isa_io_special)
258c2ecf20Sopenharmony_ci#endif
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include <linux/device.h>
288c2ecf20Sopenharmony_ci#include <linux/compiler.h>
298c2ecf20Sopenharmony_ci#include <linux/mm.h>
308c2ecf20Sopenharmony_ci#include <asm/page.h>
318c2ecf20Sopenharmony_ci#include <asm/byteorder.h>
328c2ecf20Sopenharmony_ci#include <asm/synch.h>
338c2ecf20Sopenharmony_ci#include <asm/delay.h>
348c2ecf20Sopenharmony_ci#include <asm/mmiowb.h>
358c2ecf20Sopenharmony_ci#include <asm/mmu.h>
368c2ecf20Sopenharmony_ci#include <asm/ppc_asm.h>
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define SIO_CONFIG_RA	0x398
398c2ecf20Sopenharmony_ci#define SIO_CONFIG_RD	0x399
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define SLOW_DOWN_IO
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* 32 bits uses slightly different variables for the various IO
448c2ecf20Sopenharmony_ci * bases. Most of this file only uses _IO_BASE though which we
458c2ecf20Sopenharmony_ci * define properly based on the platform
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_ci#ifndef CONFIG_PCI
488c2ecf20Sopenharmony_ci#define _IO_BASE	0
498c2ecf20Sopenharmony_ci#define _ISA_MEM_BASE	0
508c2ecf20Sopenharmony_ci#define PCI_DRAM_OFFSET 0
518c2ecf20Sopenharmony_ci#elif defined(CONFIG_PPC32)
528c2ecf20Sopenharmony_ci#define _IO_BASE	isa_io_base
538c2ecf20Sopenharmony_ci#define _ISA_MEM_BASE	isa_mem_base
548c2ecf20Sopenharmony_ci#define PCI_DRAM_OFFSET	pci_dram_offset
558c2ecf20Sopenharmony_ci#else
568c2ecf20Sopenharmony_ci#define _IO_BASE	pci_io_base
578c2ecf20Sopenharmony_ci#define _ISA_MEM_BASE	isa_mem_base
588c2ecf20Sopenharmony_ci#define PCI_DRAM_OFFSET	0
598c2ecf20Sopenharmony_ci#endif
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ciextern unsigned long isa_io_base;
628c2ecf20Sopenharmony_ciextern unsigned long pci_io_base;
638c2ecf20Sopenharmony_ciextern unsigned long pci_dram_offset;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ciextern resource_size_t isa_mem_base;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/* Boolean set by platform if PIO accesses are suppored while _IO_BASE
688c2ecf20Sopenharmony_ci * is not set or addresses cannot be translated to MMIO. This is typically
698c2ecf20Sopenharmony_ci * set when the platform supports "special" PIO accesses via a non memory
708c2ecf20Sopenharmony_ci * mapped mechanism, and allows things like the early udbg UART code to
718c2ecf20Sopenharmony_ci * function.
728c2ecf20Sopenharmony_ci */
738c2ecf20Sopenharmony_ciextern bool isa_io_special;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC32
768c2ecf20Sopenharmony_ci#if defined(CONFIG_PPC_INDIRECT_PIO) || defined(CONFIG_PPC_INDIRECT_MMIO)
778c2ecf20Sopenharmony_ci#error CONFIG_PPC_INDIRECT_{PIO,MMIO} are not yet supported on 32 bits
788c2ecf20Sopenharmony_ci#endif
798c2ecf20Sopenharmony_ci#endif
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci/*
828c2ecf20Sopenharmony_ci *
838c2ecf20Sopenharmony_ci * Low level MMIO accessors
848c2ecf20Sopenharmony_ci *
858c2ecf20Sopenharmony_ci * This provides the non-bus specific accessors to MMIO. Those are PowerPC
868c2ecf20Sopenharmony_ci * specific and thus shouldn't be used in generic code. The accessors
878c2ecf20Sopenharmony_ci * provided here are:
888c2ecf20Sopenharmony_ci *
898c2ecf20Sopenharmony_ci *	in_8, in_le16, in_be16, in_le32, in_be32, in_le64, in_be64
908c2ecf20Sopenharmony_ci *	out_8, out_le16, out_be16, out_le32, out_be32, out_le64, out_be64
918c2ecf20Sopenharmony_ci *	_insb, _insw_ns, _insl_ns, _outsb, _outsw_ns, _outsl_ns
928c2ecf20Sopenharmony_ci *
938c2ecf20Sopenharmony_ci * Those operate directly on a kernel virtual address. Note that the prototype
948c2ecf20Sopenharmony_ci * for the out_* accessors has the arguments in opposite order from the usual
958c2ecf20Sopenharmony_ci * linux PCI accessors. Unlike those, they take the address first and the value
968c2ecf20Sopenharmony_ci * next.
978c2ecf20Sopenharmony_ci *
988c2ecf20Sopenharmony_ci * Note: I might drop the _ns suffix on the stream operations soon as it is
998c2ecf20Sopenharmony_ci * simply normal for stream operations to not swap in the first place.
1008c2ecf20Sopenharmony_ci *
1018c2ecf20Sopenharmony_ci */
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#define DEF_MMIO_IN_X(name, size, insn)				\
1048c2ecf20Sopenharmony_cistatic inline u##size name(const volatile u##size __iomem *addr)	\
1058c2ecf20Sopenharmony_ci{									\
1068c2ecf20Sopenharmony_ci	u##size ret;							\
1078c2ecf20Sopenharmony_ci	__asm__ __volatile__("sync;"#insn" %0,%y1;twi 0,%0,0;isync"	\
1088c2ecf20Sopenharmony_ci		: "=r" (ret) : "Z" (*addr) : "memory");			\
1098c2ecf20Sopenharmony_ci	return ret;							\
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci#define DEF_MMIO_OUT_X(name, size, insn)				\
1138c2ecf20Sopenharmony_cistatic inline void name(volatile u##size __iomem *addr, u##size val)	\
1148c2ecf20Sopenharmony_ci{									\
1158c2ecf20Sopenharmony_ci	__asm__ __volatile__("sync;"#insn" %1,%y0"			\
1168c2ecf20Sopenharmony_ci		: "=Z" (*addr) : "r" (val) : "memory");			\
1178c2ecf20Sopenharmony_ci	mmiowb_set_pending();						\
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#define DEF_MMIO_IN_D(name, size, insn)				\
1218c2ecf20Sopenharmony_cistatic inline u##size name(const volatile u##size __iomem *addr)	\
1228c2ecf20Sopenharmony_ci{									\
1238c2ecf20Sopenharmony_ci	u##size ret;							\
1248c2ecf20Sopenharmony_ci	__asm__ __volatile__("sync;"#insn"%U1%X1 %0,%1;twi 0,%0,0;isync"\
1258c2ecf20Sopenharmony_ci		: "=r" (ret) : "m" (*addr) : "memory");			\
1268c2ecf20Sopenharmony_ci	return ret;							\
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci#define DEF_MMIO_OUT_D(name, size, insn)				\
1308c2ecf20Sopenharmony_cistatic inline void name(volatile u##size __iomem *addr, u##size val)	\
1318c2ecf20Sopenharmony_ci{									\
1328c2ecf20Sopenharmony_ci	__asm__ __volatile__("sync;"#insn"%U0%X0 %1,%0"			\
1338c2ecf20Sopenharmony_ci		: "=m" (*addr) : "r" (val) : "memory");			\
1348c2ecf20Sopenharmony_ci	mmiowb_set_pending();						\
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ciDEF_MMIO_IN_D(in_8,     8, lbz);
1388c2ecf20Sopenharmony_ciDEF_MMIO_OUT_D(out_8,   8, stb);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN__
1418c2ecf20Sopenharmony_ciDEF_MMIO_IN_D(in_be16, 16, lhz);
1428c2ecf20Sopenharmony_ciDEF_MMIO_IN_D(in_be32, 32, lwz);
1438c2ecf20Sopenharmony_ciDEF_MMIO_IN_X(in_le16, 16, lhbrx);
1448c2ecf20Sopenharmony_ciDEF_MMIO_IN_X(in_le32, 32, lwbrx);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ciDEF_MMIO_OUT_D(out_be16, 16, sth);
1478c2ecf20Sopenharmony_ciDEF_MMIO_OUT_D(out_be32, 32, stw);
1488c2ecf20Sopenharmony_ciDEF_MMIO_OUT_X(out_le16, 16, sthbrx);
1498c2ecf20Sopenharmony_ciDEF_MMIO_OUT_X(out_le32, 32, stwbrx);
1508c2ecf20Sopenharmony_ci#else
1518c2ecf20Sopenharmony_ciDEF_MMIO_IN_X(in_be16, 16, lhbrx);
1528c2ecf20Sopenharmony_ciDEF_MMIO_IN_X(in_be32, 32, lwbrx);
1538c2ecf20Sopenharmony_ciDEF_MMIO_IN_D(in_le16, 16, lhz);
1548c2ecf20Sopenharmony_ciDEF_MMIO_IN_D(in_le32, 32, lwz);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ciDEF_MMIO_OUT_X(out_be16, 16, sthbrx);
1578c2ecf20Sopenharmony_ciDEF_MMIO_OUT_X(out_be32, 32, stwbrx);
1588c2ecf20Sopenharmony_ciDEF_MMIO_OUT_D(out_le16, 16, sth);
1598c2ecf20Sopenharmony_ciDEF_MMIO_OUT_D(out_le32, 32, stw);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci#endif /* __BIG_ENDIAN */
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci#ifdef __powerpc64__
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN__
1668c2ecf20Sopenharmony_ciDEF_MMIO_OUT_D(out_be64, 64, std);
1678c2ecf20Sopenharmony_ciDEF_MMIO_IN_D(in_be64, 64, ld);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci/* There is no asm instructions for 64 bits reverse loads and stores */
1708c2ecf20Sopenharmony_cistatic inline u64 in_le64(const volatile u64 __iomem *addr)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	return swab64(in_be64(addr));
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic inline void out_le64(volatile u64 __iomem *addr, u64 val)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	out_be64(addr, swab64(val));
1788c2ecf20Sopenharmony_ci}
1798c2ecf20Sopenharmony_ci#else
1808c2ecf20Sopenharmony_ciDEF_MMIO_OUT_D(out_le64, 64, std);
1818c2ecf20Sopenharmony_ciDEF_MMIO_IN_D(in_le64, 64, ld);
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci/* There is no asm instructions for 64 bits reverse loads and stores */
1848c2ecf20Sopenharmony_cistatic inline u64 in_be64(const volatile u64 __iomem *addr)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	return swab64(in_le64(addr));
1878c2ecf20Sopenharmony_ci}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cistatic inline void out_be64(volatile u64 __iomem *addr, u64 val)
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	out_le64(addr, swab64(val));
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci#endif
1958c2ecf20Sopenharmony_ci#endif /* __powerpc64__ */
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci/*
1988c2ecf20Sopenharmony_ci * Low level IO stream instructions are defined out of line for now
1998c2ecf20Sopenharmony_ci */
2008c2ecf20Sopenharmony_ciextern void _insb(const volatile u8 __iomem *addr, void *buf, long count);
2018c2ecf20Sopenharmony_ciextern void _outsb(volatile u8 __iomem *addr,const void *buf,long count);
2028c2ecf20Sopenharmony_ciextern void _insw_ns(const volatile u16 __iomem *addr, void *buf, long count);
2038c2ecf20Sopenharmony_ciextern void _outsw_ns(volatile u16 __iomem *addr, const void *buf, long count);
2048c2ecf20Sopenharmony_ciextern void _insl_ns(const volatile u32 __iomem *addr, void *buf, long count);
2058c2ecf20Sopenharmony_ciextern void _outsl_ns(volatile u32 __iomem *addr, const void *buf, long count);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci/* The _ns naming is historical and will be removed. For now, just #define
2088c2ecf20Sopenharmony_ci * the non _ns equivalent names
2098c2ecf20Sopenharmony_ci */
2108c2ecf20Sopenharmony_ci#define _insw	_insw_ns
2118c2ecf20Sopenharmony_ci#define _insl	_insl_ns
2128c2ecf20Sopenharmony_ci#define _outsw	_outsw_ns
2138c2ecf20Sopenharmony_ci#define _outsl	_outsl_ns
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci/*
2178c2ecf20Sopenharmony_ci * memset_io, memcpy_toio, memcpy_fromio base implementations are out of line
2188c2ecf20Sopenharmony_ci */
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ciextern void _memset_io(volatile void __iomem *addr, int c, unsigned long n);
2218c2ecf20Sopenharmony_ciextern void _memcpy_fromio(void *dest, const volatile void __iomem *src,
2228c2ecf20Sopenharmony_ci			   unsigned long n);
2238c2ecf20Sopenharmony_ciextern void _memcpy_toio(volatile void __iomem *dest, const void *src,
2248c2ecf20Sopenharmony_ci			 unsigned long n);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci/*
2278c2ecf20Sopenharmony_ci *
2288c2ecf20Sopenharmony_ci * PCI and standard ISA accessors
2298c2ecf20Sopenharmony_ci *
2308c2ecf20Sopenharmony_ci * Those are globally defined linux accessors for devices on PCI or ISA
2318c2ecf20Sopenharmony_ci * busses. They follow the Linux defined semantics. The current implementation
2328c2ecf20Sopenharmony_ci * for PowerPC is as close as possible to the x86 version of these, and thus
2338c2ecf20Sopenharmony_ci * provides fairly heavy weight barriers for the non-raw versions
2348c2ecf20Sopenharmony_ci *
2358c2ecf20Sopenharmony_ci * In addition, they support a hook mechanism when CONFIG_PPC_INDIRECT_MMIO
2368c2ecf20Sopenharmony_ci * or CONFIG_PPC_INDIRECT_PIO are set allowing the platform to provide its
2378c2ecf20Sopenharmony_ci * own implementation of some or all of the accessors.
2388c2ecf20Sopenharmony_ci */
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci/*
2418c2ecf20Sopenharmony_ci * Include the EEH definitions when EEH is enabled only so they don't get
2428c2ecf20Sopenharmony_ci * in the way when building for 32 bits
2438c2ecf20Sopenharmony_ci */
2448c2ecf20Sopenharmony_ci#ifdef CONFIG_EEH
2458c2ecf20Sopenharmony_ci#include <asm/eeh.h>
2468c2ecf20Sopenharmony_ci#endif
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci/* Shortcut to the MMIO argument pointer */
2498c2ecf20Sopenharmony_ci#define PCI_IO_ADDR	volatile void __iomem *
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci/* Indirect IO address tokens:
2528c2ecf20Sopenharmony_ci *
2538c2ecf20Sopenharmony_ci * When CONFIG_PPC_INDIRECT_MMIO is set, the platform can provide hooks
2548c2ecf20Sopenharmony_ci * on all MMIOs. (Note that this is all 64 bits only for now)
2558c2ecf20Sopenharmony_ci *
2568c2ecf20Sopenharmony_ci * To help platforms who may need to differentiate MMIO addresses in
2578c2ecf20Sopenharmony_ci * their hooks, a bitfield is reserved for use by the platform near the
2588c2ecf20Sopenharmony_ci * top of MMIO addresses (not PIO, those have to cope the hard way).
2598c2ecf20Sopenharmony_ci *
2608c2ecf20Sopenharmony_ci * The highest address in the kernel virtual space are:
2618c2ecf20Sopenharmony_ci *
2628c2ecf20Sopenharmony_ci *  d0003fffffffffff	# with Hash MMU
2638c2ecf20Sopenharmony_ci *  c00fffffffffffff	# with Radix MMU
2648c2ecf20Sopenharmony_ci *
2658c2ecf20Sopenharmony_ci * The top 4 bits are reserved as the region ID on hash, leaving us 8 bits
2668c2ecf20Sopenharmony_ci * that can be used for the field.
2678c2ecf20Sopenharmony_ci *
2688c2ecf20Sopenharmony_ci * The direct IO mapping operations will then mask off those bits
2698c2ecf20Sopenharmony_ci * before doing the actual access, though that only happen when
2708c2ecf20Sopenharmony_ci * CONFIG_PPC_INDIRECT_MMIO is set, thus be careful when you use that
2718c2ecf20Sopenharmony_ci * mechanism
2728c2ecf20Sopenharmony_ci *
2738c2ecf20Sopenharmony_ci * For PIO, there is a separate CONFIG_PPC_INDIRECT_PIO which makes
2748c2ecf20Sopenharmony_ci * all PIO functions call through a hook.
2758c2ecf20Sopenharmony_ci */
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_INDIRECT_MMIO
2788c2ecf20Sopenharmony_ci#define PCI_IO_IND_TOKEN_SHIFT	52
2798c2ecf20Sopenharmony_ci#define PCI_IO_IND_TOKEN_MASK	(0xfful << PCI_IO_IND_TOKEN_SHIFT)
2808c2ecf20Sopenharmony_ci#define PCI_FIX_ADDR(addr)						\
2818c2ecf20Sopenharmony_ci	((PCI_IO_ADDR)(((unsigned long)(addr)) & ~PCI_IO_IND_TOKEN_MASK))
2828c2ecf20Sopenharmony_ci#define PCI_GET_ADDR_TOKEN(addr)					\
2838c2ecf20Sopenharmony_ci	(((unsigned long)(addr) & PCI_IO_IND_TOKEN_MASK) >> 		\
2848c2ecf20Sopenharmony_ci		PCI_IO_IND_TOKEN_SHIFT)
2858c2ecf20Sopenharmony_ci#define PCI_SET_ADDR_TOKEN(addr, token) 				\
2868c2ecf20Sopenharmony_cido {									\
2878c2ecf20Sopenharmony_ci	unsigned long __a = (unsigned long)(addr);			\
2888c2ecf20Sopenharmony_ci	__a &= ~PCI_IO_IND_TOKEN_MASK;					\
2898c2ecf20Sopenharmony_ci	__a |= ((unsigned long)(token)) << PCI_IO_IND_TOKEN_SHIFT;	\
2908c2ecf20Sopenharmony_ci	(addr) = (void __iomem *)__a;					\
2918c2ecf20Sopenharmony_ci} while(0)
2928c2ecf20Sopenharmony_ci#else
2938c2ecf20Sopenharmony_ci#define PCI_FIX_ADDR(addr) (addr)
2948c2ecf20Sopenharmony_ci#endif
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci/*
2988c2ecf20Sopenharmony_ci * Non ordered and non-swapping "raw" accessors
2998c2ecf20Sopenharmony_ci */
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic inline unsigned char __raw_readb(const volatile void __iomem *addr)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	return *(volatile unsigned char __force *)PCI_FIX_ADDR(addr);
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_cistatic inline unsigned short __raw_readw(const volatile void __iomem *addr)
3068c2ecf20Sopenharmony_ci{
3078c2ecf20Sopenharmony_ci	return *(volatile unsigned short __force *)PCI_FIX_ADDR(addr);
3088c2ecf20Sopenharmony_ci}
3098c2ecf20Sopenharmony_cistatic inline unsigned int __raw_readl(const volatile void __iomem *addr)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	return *(volatile unsigned int __force *)PCI_FIX_ADDR(addr);
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_cistatic inline void __raw_writeb(unsigned char v, volatile void __iomem *addr)
3148c2ecf20Sopenharmony_ci{
3158c2ecf20Sopenharmony_ci	*(volatile unsigned char __force *)PCI_FIX_ADDR(addr) = v;
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_cistatic inline void __raw_writew(unsigned short v, volatile void __iomem *addr)
3188c2ecf20Sopenharmony_ci{
3198c2ecf20Sopenharmony_ci	*(volatile unsigned short __force *)PCI_FIX_ADDR(addr) = v;
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_cistatic inline void __raw_writel(unsigned int v, volatile void __iomem *addr)
3228c2ecf20Sopenharmony_ci{
3238c2ecf20Sopenharmony_ci	*(volatile unsigned int __force *)PCI_FIX_ADDR(addr) = v;
3248c2ecf20Sopenharmony_ci}
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci#ifdef __powerpc64__
3278c2ecf20Sopenharmony_cistatic inline unsigned long __raw_readq(const volatile void __iomem *addr)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	return *(volatile unsigned long __force *)PCI_FIX_ADDR(addr);
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_cistatic inline void __raw_writeq(unsigned long v, volatile void __iomem *addr)
3328c2ecf20Sopenharmony_ci{
3338c2ecf20Sopenharmony_ci	*(volatile unsigned long __force *)PCI_FIX_ADDR(addr) = v;
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic inline void __raw_writeq_be(unsigned long v, volatile void __iomem *addr)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	__raw_writeq((__force unsigned long)cpu_to_be64(v), addr);
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci/*
3428c2ecf20Sopenharmony_ci * Real mode versions of the above. Those instructions are only supposed
3438c2ecf20Sopenharmony_ci * to be used in hypervisor real mode as per the architecture spec.
3448c2ecf20Sopenharmony_ci */
3458c2ecf20Sopenharmony_cistatic inline void __raw_rm_writeb(u8 val, volatile void __iomem *paddr)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	__asm__ __volatile__(".machine push;   \
3488c2ecf20Sopenharmony_ci			      .machine power6; \
3498c2ecf20Sopenharmony_ci			      stbcix %0,0,%1;  \
3508c2ecf20Sopenharmony_ci			      .machine pop;"
3518c2ecf20Sopenharmony_ci		: : "r" (val), "r" (paddr) : "memory");
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic inline void __raw_rm_writew(u16 val, volatile void __iomem *paddr)
3558c2ecf20Sopenharmony_ci{
3568c2ecf20Sopenharmony_ci	__asm__ __volatile__(".machine push;   \
3578c2ecf20Sopenharmony_ci			      .machine power6; \
3588c2ecf20Sopenharmony_ci			      sthcix %0,0,%1;  \
3598c2ecf20Sopenharmony_ci			      .machine pop;"
3608c2ecf20Sopenharmony_ci		: : "r" (val), "r" (paddr) : "memory");
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic inline void __raw_rm_writel(u32 val, volatile void __iomem *paddr)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	__asm__ __volatile__(".machine push;   \
3668c2ecf20Sopenharmony_ci			      .machine power6; \
3678c2ecf20Sopenharmony_ci			      stwcix %0,0,%1;  \
3688c2ecf20Sopenharmony_ci			      .machine pop;"
3698c2ecf20Sopenharmony_ci		: : "r" (val), "r" (paddr) : "memory");
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_cistatic inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	__asm__ __volatile__(".machine push;   \
3758c2ecf20Sopenharmony_ci			      .machine power6; \
3768c2ecf20Sopenharmony_ci			      stdcix %0,0,%1;  \
3778c2ecf20Sopenharmony_ci			      .machine pop;"
3788c2ecf20Sopenharmony_ci		: : "r" (val), "r" (paddr) : "memory");
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_cistatic inline void __raw_rm_writeq_be(u64 val, volatile void __iomem *paddr)
3828c2ecf20Sopenharmony_ci{
3838c2ecf20Sopenharmony_ci	__raw_rm_writeq((__force u64)cpu_to_be64(val), paddr);
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic inline u8 __raw_rm_readb(volatile void __iomem *paddr)
3878c2ecf20Sopenharmony_ci{
3888c2ecf20Sopenharmony_ci	u8 ret;
3898c2ecf20Sopenharmony_ci	__asm__ __volatile__(".machine push;   \
3908c2ecf20Sopenharmony_ci			      .machine power6; \
3918c2ecf20Sopenharmony_ci			      lbzcix %0,0, %1; \
3928c2ecf20Sopenharmony_ci			      .machine pop;"
3938c2ecf20Sopenharmony_ci			     : "=r" (ret) : "r" (paddr) : "memory");
3948c2ecf20Sopenharmony_ci	return ret;
3958c2ecf20Sopenharmony_ci}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_cistatic inline u16 __raw_rm_readw(volatile void __iomem *paddr)
3988c2ecf20Sopenharmony_ci{
3998c2ecf20Sopenharmony_ci	u16 ret;
4008c2ecf20Sopenharmony_ci	__asm__ __volatile__(".machine push;   \
4018c2ecf20Sopenharmony_ci			      .machine power6; \
4028c2ecf20Sopenharmony_ci			      lhzcix %0,0, %1; \
4038c2ecf20Sopenharmony_ci			      .machine pop;"
4048c2ecf20Sopenharmony_ci			     : "=r" (ret) : "r" (paddr) : "memory");
4058c2ecf20Sopenharmony_ci	return ret;
4068c2ecf20Sopenharmony_ci}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistatic inline u32 __raw_rm_readl(volatile void __iomem *paddr)
4098c2ecf20Sopenharmony_ci{
4108c2ecf20Sopenharmony_ci	u32 ret;
4118c2ecf20Sopenharmony_ci	__asm__ __volatile__(".machine push;   \
4128c2ecf20Sopenharmony_ci			      .machine power6; \
4138c2ecf20Sopenharmony_ci			      lwzcix %0,0, %1; \
4148c2ecf20Sopenharmony_ci			      .machine pop;"
4158c2ecf20Sopenharmony_ci			     : "=r" (ret) : "r" (paddr) : "memory");
4168c2ecf20Sopenharmony_ci	return ret;
4178c2ecf20Sopenharmony_ci}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic inline u64 __raw_rm_readq(volatile void __iomem *paddr)
4208c2ecf20Sopenharmony_ci{
4218c2ecf20Sopenharmony_ci	u64 ret;
4228c2ecf20Sopenharmony_ci	__asm__ __volatile__(".machine push;   \
4238c2ecf20Sopenharmony_ci			      .machine power6; \
4248c2ecf20Sopenharmony_ci			      ldcix %0,0, %1;  \
4258c2ecf20Sopenharmony_ci			      .machine pop;"
4268c2ecf20Sopenharmony_ci			     : "=r" (ret) : "r" (paddr) : "memory");
4278c2ecf20Sopenharmony_ci	return ret;
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci#endif /* __powerpc64__ */
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci/*
4328c2ecf20Sopenharmony_ci *
4338c2ecf20Sopenharmony_ci * PCI PIO and MMIO accessors.
4348c2ecf20Sopenharmony_ci *
4358c2ecf20Sopenharmony_ci *
4368c2ecf20Sopenharmony_ci * On 32 bits, PIO operations have a recovery mechanism in case they trigger
4378c2ecf20Sopenharmony_ci * machine checks (which they occasionally do when probing non existing
4388c2ecf20Sopenharmony_ci * IO ports on some platforms, like PowerMac and 8xx).
4398c2ecf20Sopenharmony_ci * I always found it to be of dubious reliability and I am tempted to get
4408c2ecf20Sopenharmony_ci * rid of it one of these days. So if you think it's important to keep it,
4418c2ecf20Sopenharmony_ci * please voice up asap. We never had it for 64 bits and I do not intend
4428c2ecf20Sopenharmony_ci * to port it over
4438c2ecf20Sopenharmony_ci */
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC32
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci#define __do_in_asm(name, op)				\
4488c2ecf20Sopenharmony_cistatic inline unsigned int name(unsigned int port)	\
4498c2ecf20Sopenharmony_ci{							\
4508c2ecf20Sopenharmony_ci	unsigned int x;					\
4518c2ecf20Sopenharmony_ci	__asm__ __volatile__(				\
4528c2ecf20Sopenharmony_ci		"sync\n"				\
4538c2ecf20Sopenharmony_ci		"0:"	op "	%0,0,%1\n"		\
4548c2ecf20Sopenharmony_ci		"1:	twi	0,%0,0\n"		\
4558c2ecf20Sopenharmony_ci		"2:	isync\n"			\
4568c2ecf20Sopenharmony_ci		"3:	nop\n"				\
4578c2ecf20Sopenharmony_ci		"4:\n"					\
4588c2ecf20Sopenharmony_ci		".section .fixup,\"ax\"\n"		\
4598c2ecf20Sopenharmony_ci		"5:	li	%0,-1\n"		\
4608c2ecf20Sopenharmony_ci		"	b	4b\n"			\
4618c2ecf20Sopenharmony_ci		".previous\n"				\
4628c2ecf20Sopenharmony_ci		EX_TABLE(0b, 5b)			\
4638c2ecf20Sopenharmony_ci		EX_TABLE(1b, 5b)			\
4648c2ecf20Sopenharmony_ci		EX_TABLE(2b, 5b)			\
4658c2ecf20Sopenharmony_ci		EX_TABLE(3b, 5b)			\
4668c2ecf20Sopenharmony_ci		: "=&r" (x)				\
4678c2ecf20Sopenharmony_ci		: "r" (port + _IO_BASE)			\
4688c2ecf20Sopenharmony_ci		: "memory");  				\
4698c2ecf20Sopenharmony_ci	return x;					\
4708c2ecf20Sopenharmony_ci}
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci#define __do_out_asm(name, op)				\
4738c2ecf20Sopenharmony_cistatic inline void name(unsigned int val, unsigned int port) \
4748c2ecf20Sopenharmony_ci{							\
4758c2ecf20Sopenharmony_ci	__asm__ __volatile__(				\
4768c2ecf20Sopenharmony_ci		"sync\n"				\
4778c2ecf20Sopenharmony_ci		"0:" op " %0,0,%1\n"			\
4788c2ecf20Sopenharmony_ci		"1:	sync\n"				\
4798c2ecf20Sopenharmony_ci		"2:\n"					\
4808c2ecf20Sopenharmony_ci		EX_TABLE(0b, 2b)			\
4818c2ecf20Sopenharmony_ci		EX_TABLE(1b, 2b)			\
4828c2ecf20Sopenharmony_ci		: : "r" (val), "r" (port + _IO_BASE)	\
4838c2ecf20Sopenharmony_ci		: "memory");   	   	   		\
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci__do_in_asm(_rec_inb, "lbzx")
4878c2ecf20Sopenharmony_ci__do_in_asm(_rec_inw, "lhbrx")
4888c2ecf20Sopenharmony_ci__do_in_asm(_rec_inl, "lwbrx")
4898c2ecf20Sopenharmony_ci__do_out_asm(_rec_outb, "stbx")
4908c2ecf20Sopenharmony_ci__do_out_asm(_rec_outw, "sthbrx")
4918c2ecf20Sopenharmony_ci__do_out_asm(_rec_outl, "stwbrx")
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC32 */
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci/* The "__do_*" operations below provide the actual "base" implementation
4968c2ecf20Sopenharmony_ci * for each of the defined accessors. Some of them use the out_* functions
4978c2ecf20Sopenharmony_ci * directly, some of them still use EEH, though we might change that in the
4988c2ecf20Sopenharmony_ci * future. Those macros below provide the necessary argument swapping and
4998c2ecf20Sopenharmony_ci * handling of the IO base for PIO.
5008c2ecf20Sopenharmony_ci *
5018c2ecf20Sopenharmony_ci * They are themselves used by the macros that define the actual accessors
5028c2ecf20Sopenharmony_ci * and can be used by the hooks if any.
5038c2ecf20Sopenharmony_ci *
5048c2ecf20Sopenharmony_ci * Note that PIO operations are always defined in terms of their corresonding
5058c2ecf20Sopenharmony_ci * MMIO operations. That allows platforms like iSeries who want to modify the
5068c2ecf20Sopenharmony_ci * behaviour of both to only hook on the MMIO version and get both. It's also
5078c2ecf20Sopenharmony_ci * possible to hook directly at the toplevel PIO operation if they have to
5088c2ecf20Sopenharmony_ci * be handled differently
5098c2ecf20Sopenharmony_ci */
5108c2ecf20Sopenharmony_ci#define __do_writeb(val, addr)	out_8(PCI_FIX_ADDR(addr), val)
5118c2ecf20Sopenharmony_ci#define __do_writew(val, addr)	out_le16(PCI_FIX_ADDR(addr), val)
5128c2ecf20Sopenharmony_ci#define __do_writel(val, addr)	out_le32(PCI_FIX_ADDR(addr), val)
5138c2ecf20Sopenharmony_ci#define __do_writeq(val, addr)	out_le64(PCI_FIX_ADDR(addr), val)
5148c2ecf20Sopenharmony_ci#define __do_writew_be(val, addr) out_be16(PCI_FIX_ADDR(addr), val)
5158c2ecf20Sopenharmony_ci#define __do_writel_be(val, addr) out_be32(PCI_FIX_ADDR(addr), val)
5168c2ecf20Sopenharmony_ci#define __do_writeq_be(val, addr) out_be64(PCI_FIX_ADDR(addr), val)
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci#ifdef CONFIG_EEH
5198c2ecf20Sopenharmony_ci#define __do_readb(addr)	eeh_readb(PCI_FIX_ADDR(addr))
5208c2ecf20Sopenharmony_ci#define __do_readw(addr)	eeh_readw(PCI_FIX_ADDR(addr))
5218c2ecf20Sopenharmony_ci#define __do_readl(addr)	eeh_readl(PCI_FIX_ADDR(addr))
5228c2ecf20Sopenharmony_ci#define __do_readq(addr)	eeh_readq(PCI_FIX_ADDR(addr))
5238c2ecf20Sopenharmony_ci#define __do_readw_be(addr)	eeh_readw_be(PCI_FIX_ADDR(addr))
5248c2ecf20Sopenharmony_ci#define __do_readl_be(addr)	eeh_readl_be(PCI_FIX_ADDR(addr))
5258c2ecf20Sopenharmony_ci#define __do_readq_be(addr)	eeh_readq_be(PCI_FIX_ADDR(addr))
5268c2ecf20Sopenharmony_ci#else /* CONFIG_EEH */
5278c2ecf20Sopenharmony_ci#define __do_readb(addr)	in_8(PCI_FIX_ADDR(addr))
5288c2ecf20Sopenharmony_ci#define __do_readw(addr)	in_le16(PCI_FIX_ADDR(addr))
5298c2ecf20Sopenharmony_ci#define __do_readl(addr)	in_le32(PCI_FIX_ADDR(addr))
5308c2ecf20Sopenharmony_ci#define __do_readq(addr)	in_le64(PCI_FIX_ADDR(addr))
5318c2ecf20Sopenharmony_ci#define __do_readw_be(addr)	in_be16(PCI_FIX_ADDR(addr))
5328c2ecf20Sopenharmony_ci#define __do_readl_be(addr)	in_be32(PCI_FIX_ADDR(addr))
5338c2ecf20Sopenharmony_ci#define __do_readq_be(addr)	in_be64(PCI_FIX_ADDR(addr))
5348c2ecf20Sopenharmony_ci#endif /* !defined(CONFIG_EEH) */
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC32
5378c2ecf20Sopenharmony_ci#define __do_outb(val, port)	_rec_outb(val, port)
5388c2ecf20Sopenharmony_ci#define __do_outw(val, port)	_rec_outw(val, port)
5398c2ecf20Sopenharmony_ci#define __do_outl(val, port)	_rec_outl(val, port)
5408c2ecf20Sopenharmony_ci#define __do_inb(port)		_rec_inb(port)
5418c2ecf20Sopenharmony_ci#define __do_inw(port)		_rec_inw(port)
5428c2ecf20Sopenharmony_ci#define __do_inl(port)		_rec_inl(port)
5438c2ecf20Sopenharmony_ci#else /* CONFIG_PPC32 */
5448c2ecf20Sopenharmony_ci#define __do_outb(val, port)	writeb(val,(PCI_IO_ADDR)_IO_BASE+port);
5458c2ecf20Sopenharmony_ci#define __do_outw(val, port)	writew(val,(PCI_IO_ADDR)_IO_BASE+port);
5468c2ecf20Sopenharmony_ci#define __do_outl(val, port)	writel(val,(PCI_IO_ADDR)_IO_BASE+port);
5478c2ecf20Sopenharmony_ci#define __do_inb(port)		readb((PCI_IO_ADDR)_IO_BASE + port);
5488c2ecf20Sopenharmony_ci#define __do_inw(port)		readw((PCI_IO_ADDR)_IO_BASE + port);
5498c2ecf20Sopenharmony_ci#define __do_inl(port)		readl((PCI_IO_ADDR)_IO_BASE + port);
5508c2ecf20Sopenharmony_ci#endif /* !CONFIG_PPC32 */
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci#ifdef CONFIG_EEH
5538c2ecf20Sopenharmony_ci#define __do_readsb(a, b, n)	eeh_readsb(PCI_FIX_ADDR(a), (b), (n))
5548c2ecf20Sopenharmony_ci#define __do_readsw(a, b, n)	eeh_readsw(PCI_FIX_ADDR(a), (b), (n))
5558c2ecf20Sopenharmony_ci#define __do_readsl(a, b, n)	eeh_readsl(PCI_FIX_ADDR(a), (b), (n))
5568c2ecf20Sopenharmony_ci#else /* CONFIG_EEH */
5578c2ecf20Sopenharmony_ci#define __do_readsb(a, b, n)	_insb(PCI_FIX_ADDR(a), (b), (n))
5588c2ecf20Sopenharmony_ci#define __do_readsw(a, b, n)	_insw(PCI_FIX_ADDR(a), (b), (n))
5598c2ecf20Sopenharmony_ci#define __do_readsl(a, b, n)	_insl(PCI_FIX_ADDR(a), (b), (n))
5608c2ecf20Sopenharmony_ci#endif /* !CONFIG_EEH */
5618c2ecf20Sopenharmony_ci#define __do_writesb(a, b, n)	_outsb(PCI_FIX_ADDR(a),(b),(n))
5628c2ecf20Sopenharmony_ci#define __do_writesw(a, b, n)	_outsw(PCI_FIX_ADDR(a),(b),(n))
5638c2ecf20Sopenharmony_ci#define __do_writesl(a, b, n)	_outsl(PCI_FIX_ADDR(a),(b),(n))
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci#define __do_insb(p, b, n)	readsb((PCI_IO_ADDR)_IO_BASE+(p), (b), (n))
5668c2ecf20Sopenharmony_ci#define __do_insw(p, b, n)	readsw((PCI_IO_ADDR)_IO_BASE+(p), (b), (n))
5678c2ecf20Sopenharmony_ci#define __do_insl(p, b, n)	readsl((PCI_IO_ADDR)_IO_BASE+(p), (b), (n))
5688c2ecf20Sopenharmony_ci#define __do_outsb(p, b, n)	writesb((PCI_IO_ADDR)_IO_BASE+(p),(b),(n))
5698c2ecf20Sopenharmony_ci#define __do_outsw(p, b, n)	writesw((PCI_IO_ADDR)_IO_BASE+(p),(b),(n))
5708c2ecf20Sopenharmony_ci#define __do_outsl(p, b, n)	writesl((PCI_IO_ADDR)_IO_BASE+(p),(b),(n))
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci#define __do_memset_io(addr, c, n)	\
5738c2ecf20Sopenharmony_ci				_memset_io(PCI_FIX_ADDR(addr), c, n)
5748c2ecf20Sopenharmony_ci#define __do_memcpy_toio(dst, src, n)	\
5758c2ecf20Sopenharmony_ci				_memcpy_toio(PCI_FIX_ADDR(dst), src, n)
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci#ifdef CONFIG_EEH
5788c2ecf20Sopenharmony_ci#define __do_memcpy_fromio(dst, src, n)	\
5798c2ecf20Sopenharmony_ci				eeh_memcpy_fromio(dst, PCI_FIX_ADDR(src), n)
5808c2ecf20Sopenharmony_ci#else /* CONFIG_EEH */
5818c2ecf20Sopenharmony_ci#define __do_memcpy_fromio(dst, src, n)	\
5828c2ecf20Sopenharmony_ci				_memcpy_fromio(dst,PCI_FIX_ADDR(src),n)
5838c2ecf20Sopenharmony_ci#endif /* !CONFIG_EEH */
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_INDIRECT_PIO
5868c2ecf20Sopenharmony_ci#define DEF_PCI_HOOK_pio(x)	x
5878c2ecf20Sopenharmony_ci#else
5888c2ecf20Sopenharmony_ci#define DEF_PCI_HOOK_pio(x)	NULL
5898c2ecf20Sopenharmony_ci#endif
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_INDIRECT_MMIO
5928c2ecf20Sopenharmony_ci#define DEF_PCI_HOOK_mem(x)	x
5938c2ecf20Sopenharmony_ci#else
5948c2ecf20Sopenharmony_ci#define DEF_PCI_HOOK_mem(x)	NULL
5958c2ecf20Sopenharmony_ci#endif
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci/* Structure containing all the hooks */
5988c2ecf20Sopenharmony_ciextern struct ppc_pci_io {
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci#define DEF_PCI_AC_RET(name, ret, at, al, space, aa)	ret (*name) at;
6018c2ecf20Sopenharmony_ci#define DEF_PCI_AC_NORET(name, at, al, space, aa)	void (*name) at;
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci#include <asm/io-defs.h>
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci#undef DEF_PCI_AC_RET
6068c2ecf20Sopenharmony_ci#undef DEF_PCI_AC_NORET
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci} ppc_pci_io;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci/* The inline wrappers */
6118c2ecf20Sopenharmony_ci#define DEF_PCI_AC_RET(name, ret, at, al, space, aa)		\
6128c2ecf20Sopenharmony_cistatic inline ret name at					\
6138c2ecf20Sopenharmony_ci{								\
6148c2ecf20Sopenharmony_ci	if (DEF_PCI_HOOK_##space(ppc_pci_io.name) != NULL)	\
6158c2ecf20Sopenharmony_ci		return ppc_pci_io.name al;			\
6168c2ecf20Sopenharmony_ci	return __do_##name al;					\
6178c2ecf20Sopenharmony_ci}
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci#define DEF_PCI_AC_NORET(name, at, al, space, aa)		\
6208c2ecf20Sopenharmony_cistatic inline void name at					\
6218c2ecf20Sopenharmony_ci{								\
6228c2ecf20Sopenharmony_ci	if (DEF_PCI_HOOK_##space(ppc_pci_io.name) != NULL)		\
6238c2ecf20Sopenharmony_ci		ppc_pci_io.name al;				\
6248c2ecf20Sopenharmony_ci	else							\
6258c2ecf20Sopenharmony_ci		__do_##name al;					\
6268c2ecf20Sopenharmony_ci}
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci#include <asm/io-defs.h>
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci#undef DEF_PCI_AC_RET
6318c2ecf20Sopenharmony_ci#undef DEF_PCI_AC_NORET
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci/* Some drivers check for the presence of readq & writeq with
6348c2ecf20Sopenharmony_ci * a #ifdef, so we make them happy here.
6358c2ecf20Sopenharmony_ci */
6368c2ecf20Sopenharmony_ci#ifdef __powerpc64__
6378c2ecf20Sopenharmony_ci#define readq	readq
6388c2ecf20Sopenharmony_ci#define writeq	writeq
6398c2ecf20Sopenharmony_ci#endif
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci/*
6428c2ecf20Sopenharmony_ci * Convert a physical pointer to a virtual kernel pointer for /dev/mem
6438c2ecf20Sopenharmony_ci * access
6448c2ecf20Sopenharmony_ci */
6458c2ecf20Sopenharmony_ci#define xlate_dev_mem_ptr(p)	__va(p)
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci/*
6488c2ecf20Sopenharmony_ci * Convert a virtual cached pointer to an uncached pointer
6498c2ecf20Sopenharmony_ci */
6508c2ecf20Sopenharmony_ci#define xlate_dev_kmem_ptr(p)	p
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci/*
6538c2ecf20Sopenharmony_ci * We don't do relaxed operations yet, at least not with this semantic
6548c2ecf20Sopenharmony_ci */
6558c2ecf20Sopenharmony_ci#define readb_relaxed(addr)	readb(addr)
6568c2ecf20Sopenharmony_ci#define readw_relaxed(addr)	readw(addr)
6578c2ecf20Sopenharmony_ci#define readl_relaxed(addr)	readl(addr)
6588c2ecf20Sopenharmony_ci#define readq_relaxed(addr)	readq(addr)
6598c2ecf20Sopenharmony_ci#define writeb_relaxed(v, addr)	writeb(v, addr)
6608c2ecf20Sopenharmony_ci#define writew_relaxed(v, addr)	writew(v, addr)
6618c2ecf20Sopenharmony_ci#define writel_relaxed(v, addr)	writel(v, addr)
6628c2ecf20Sopenharmony_ci#define writeq_relaxed(v, addr)	writeq(v, addr)
6638c2ecf20Sopenharmony_ci
6648c2ecf20Sopenharmony_ci#include <asm-generic/iomap.h>
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_cistatic inline void iosync(void)
6678c2ecf20Sopenharmony_ci{
6688c2ecf20Sopenharmony_ci        __asm__ __volatile__ ("sync" : : : "memory");
6698c2ecf20Sopenharmony_ci}
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci/* Enforce in-order execution of data I/O.
6728c2ecf20Sopenharmony_ci * No distinction between read/write on PPC; use eieio for all three.
6738c2ecf20Sopenharmony_ci * Those are fairly week though. They don't provide a barrier between
6748c2ecf20Sopenharmony_ci * MMIO and cacheable storage nor do they provide a barrier vs. locks,
6758c2ecf20Sopenharmony_ci * they only provide barriers between 2 __raw MMIO operations and
6768c2ecf20Sopenharmony_ci * possibly break write combining.
6778c2ecf20Sopenharmony_ci */
6788c2ecf20Sopenharmony_ci#define iobarrier_rw() eieio()
6798c2ecf20Sopenharmony_ci#define iobarrier_r()  eieio()
6808c2ecf20Sopenharmony_ci#define iobarrier_w()  eieio()
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci/*
6848c2ecf20Sopenharmony_ci * output pause versions need a delay at least for the
6858c2ecf20Sopenharmony_ci * w83c105 ide controller in a p610.
6868c2ecf20Sopenharmony_ci */
6878c2ecf20Sopenharmony_ci#define inb_p(port)             inb(port)
6888c2ecf20Sopenharmony_ci#define outb_p(val, port)       (udelay(1), outb((val), (port)))
6898c2ecf20Sopenharmony_ci#define inw_p(port)             inw(port)
6908c2ecf20Sopenharmony_ci#define outw_p(val, port)       (udelay(1), outw((val), (port)))
6918c2ecf20Sopenharmony_ci#define inl_p(port)             inl(port)
6928c2ecf20Sopenharmony_ci#define outl_p(val, port)       (udelay(1), outl((val), (port)))
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci#define IO_SPACE_LIMIT ~(0UL)
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci/**
6998c2ecf20Sopenharmony_ci * ioremap     -   map bus memory into CPU space
7008c2ecf20Sopenharmony_ci * @address:   bus address of the memory
7018c2ecf20Sopenharmony_ci * @size:      size of the resource to map
7028c2ecf20Sopenharmony_ci *
7038c2ecf20Sopenharmony_ci * ioremap performs a platform specific sequence of operations to
7048c2ecf20Sopenharmony_ci * make bus memory CPU accessible via the readb/readw/readl/writeb/
7058c2ecf20Sopenharmony_ci * writew/writel functions and the other mmio helpers. The returned
7068c2ecf20Sopenharmony_ci * address is not guaranteed to be usable directly as a virtual
7078c2ecf20Sopenharmony_ci * address.
7088c2ecf20Sopenharmony_ci *
7098c2ecf20Sopenharmony_ci * We provide a few variations of it:
7108c2ecf20Sopenharmony_ci *
7118c2ecf20Sopenharmony_ci * * ioremap is the standard one and provides non-cacheable guarded mappings
7128c2ecf20Sopenharmony_ci *   and can be hooked by the platform via ppc_md
7138c2ecf20Sopenharmony_ci *
7148c2ecf20Sopenharmony_ci * * ioremap_prot allows to specify the page flags as an argument and can
7158c2ecf20Sopenharmony_ci *   also be hooked by the platform via ppc_md.
7168c2ecf20Sopenharmony_ci *
7178c2ecf20Sopenharmony_ci * * ioremap_wc enables write combining
7188c2ecf20Sopenharmony_ci *
7198c2ecf20Sopenharmony_ci * * ioremap_wt enables write through
7208c2ecf20Sopenharmony_ci *
7218c2ecf20Sopenharmony_ci * * ioremap_coherent maps coherent cached memory
7228c2ecf20Sopenharmony_ci *
7238c2ecf20Sopenharmony_ci * * iounmap undoes such a mapping and can be hooked
7248c2ecf20Sopenharmony_ci *
7258c2ecf20Sopenharmony_ci * * __ioremap_caller is the same as above but takes an explicit caller
7268c2ecf20Sopenharmony_ci *   reference rather than using __builtin_return_address(0)
7278c2ecf20Sopenharmony_ci *
7288c2ecf20Sopenharmony_ci */
7298c2ecf20Sopenharmony_ciextern void __iomem *ioremap(phys_addr_t address, unsigned long size);
7308c2ecf20Sopenharmony_ciextern void __iomem *ioremap_prot(phys_addr_t address, unsigned long size,
7318c2ecf20Sopenharmony_ci				  unsigned long flags);
7328c2ecf20Sopenharmony_ciextern void __iomem *ioremap_wc(phys_addr_t address, unsigned long size);
7338c2ecf20Sopenharmony_civoid __iomem *ioremap_wt(phys_addr_t address, unsigned long size);
7348c2ecf20Sopenharmony_civoid __iomem *ioremap_coherent(phys_addr_t address, unsigned long size);
7358c2ecf20Sopenharmony_ci#define ioremap_uc(addr, size)		ioremap((addr), (size))
7368c2ecf20Sopenharmony_ci#define ioremap_cache(addr, size) \
7378c2ecf20Sopenharmony_ci	ioremap_prot((addr), (size), pgprot_val(PAGE_KERNEL))
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ciextern void iounmap(volatile void __iomem *addr);
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_civoid __iomem *ioremap_phb(phys_addr_t paddr, unsigned long size);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ciint early_ioremap_range(unsigned long ea, phys_addr_t pa,
7448c2ecf20Sopenharmony_ci			unsigned long size, pgprot_t prot);
7458c2ecf20Sopenharmony_civoid __iomem *do_ioremap(phys_addr_t pa, phys_addr_t offset, unsigned long size,
7468c2ecf20Sopenharmony_ci			 pgprot_t prot, void *caller);
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_ciextern void __iomem *__ioremap_caller(phys_addr_t, unsigned long size,
7498c2ecf20Sopenharmony_ci				      pgprot_t prot, void *caller);
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci/*
7528c2ecf20Sopenharmony_ci * When CONFIG_PPC_INDIRECT_PIO is set, we use the generic iomap implementation
7538c2ecf20Sopenharmony_ci * which needs some additional definitions here. They basically allow PIO
7548c2ecf20Sopenharmony_ci * space overall to be 1GB. This will work as long as we never try to use
7558c2ecf20Sopenharmony_ci * iomap to map MMIO below 1GB which should be fine on ppc64
7568c2ecf20Sopenharmony_ci */
7578c2ecf20Sopenharmony_ci#define HAVE_ARCH_PIO_SIZE		1
7588c2ecf20Sopenharmony_ci#define PIO_OFFSET			0x00000000UL
7598c2ecf20Sopenharmony_ci#define PIO_MASK			(FULL_IO_SIZE - 1)
7608c2ecf20Sopenharmony_ci#define PIO_RESERVED			(FULL_IO_SIZE)
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci#define mmio_read16be(addr)		readw_be(addr)
7638c2ecf20Sopenharmony_ci#define mmio_read32be(addr)		readl_be(addr)
7648c2ecf20Sopenharmony_ci#define mmio_read64be(addr)		readq_be(addr)
7658c2ecf20Sopenharmony_ci#define mmio_write16be(val, addr)	writew_be(val, addr)
7668c2ecf20Sopenharmony_ci#define mmio_write32be(val, addr)	writel_be(val, addr)
7678c2ecf20Sopenharmony_ci#define mmio_write64be(val, addr)	writeq_be(val, addr)
7688c2ecf20Sopenharmony_ci#define mmio_insb(addr, dst, count)	readsb(addr, dst, count)
7698c2ecf20Sopenharmony_ci#define mmio_insw(addr, dst, count)	readsw(addr, dst, count)
7708c2ecf20Sopenharmony_ci#define mmio_insl(addr, dst, count)	readsl(addr, dst, count)
7718c2ecf20Sopenharmony_ci#define mmio_outsb(addr, src, count)	writesb(addr, src, count)
7728c2ecf20Sopenharmony_ci#define mmio_outsw(addr, src, count)	writesw(addr, src, count)
7738c2ecf20Sopenharmony_ci#define mmio_outsl(addr, src, count)	writesl(addr, src, count)
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci/**
7768c2ecf20Sopenharmony_ci *	virt_to_phys	-	map virtual addresses to physical
7778c2ecf20Sopenharmony_ci *	@address: address to remap
7788c2ecf20Sopenharmony_ci *
7798c2ecf20Sopenharmony_ci *	The returned physical address is the physical (CPU) mapping for
7808c2ecf20Sopenharmony_ci *	the memory address given. It is only valid to use this function on
7818c2ecf20Sopenharmony_ci *	addresses directly mapped or allocated via kmalloc.
7828c2ecf20Sopenharmony_ci *
7838c2ecf20Sopenharmony_ci *	This function does not give bus mappings for DMA transfers. In
7848c2ecf20Sopenharmony_ci *	almost all conceivable cases a device driver should not be using
7858c2ecf20Sopenharmony_ci *	this function
7868c2ecf20Sopenharmony_ci */
7878c2ecf20Sopenharmony_cistatic inline unsigned long virt_to_phys(volatile void * address)
7888c2ecf20Sopenharmony_ci{
7898c2ecf20Sopenharmony_ci	WARN_ON(IS_ENABLED(CONFIG_DEBUG_VIRTUAL) && !virt_addr_valid(address));
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	return __pa((unsigned long)address);
7928c2ecf20Sopenharmony_ci}
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci/**
7958c2ecf20Sopenharmony_ci *	phys_to_virt	-	map physical address to virtual
7968c2ecf20Sopenharmony_ci *	@address: address to remap
7978c2ecf20Sopenharmony_ci *
7988c2ecf20Sopenharmony_ci *	The returned virtual address is a current CPU mapping for
7998c2ecf20Sopenharmony_ci *	the memory address given. It is only valid to use this function on
8008c2ecf20Sopenharmony_ci *	addresses that have a kernel mapping
8018c2ecf20Sopenharmony_ci *
8028c2ecf20Sopenharmony_ci *	This function does not handle bus mappings for DMA transfers. In
8038c2ecf20Sopenharmony_ci *	almost all conceivable cases a device driver should not be using
8048c2ecf20Sopenharmony_ci *	this function
8058c2ecf20Sopenharmony_ci */
8068c2ecf20Sopenharmony_cistatic inline void * phys_to_virt(unsigned long address)
8078c2ecf20Sopenharmony_ci{
8088c2ecf20Sopenharmony_ci	return (void *)__va(address);
8098c2ecf20Sopenharmony_ci}
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci/*
8128c2ecf20Sopenharmony_ci * Change "struct page" to physical address.
8138c2ecf20Sopenharmony_ci */
8148c2ecf20Sopenharmony_cistatic inline phys_addr_t page_to_phys(struct page *page)
8158c2ecf20Sopenharmony_ci{
8168c2ecf20Sopenharmony_ci	unsigned long pfn = page_to_pfn(page);
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	WARN_ON(IS_ENABLED(CONFIG_DEBUG_VIRTUAL) && !pfn_valid(pfn));
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_ci	return PFN_PHYS(pfn);
8218c2ecf20Sopenharmony_ci}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci/*
8248c2ecf20Sopenharmony_ci * 32 bits still uses virt_to_bus() for it's implementation of DMA
8258c2ecf20Sopenharmony_ci * mappings se we have to keep it defined here. We also have some old
8268c2ecf20Sopenharmony_ci * drivers (shame shame shame) that use bus_to_virt() and haven't been
8278c2ecf20Sopenharmony_ci * fixed yet so I need to define it here.
8288c2ecf20Sopenharmony_ci */
8298c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC32
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_cistatic inline unsigned long virt_to_bus(volatile void * address)
8328c2ecf20Sopenharmony_ci{
8338c2ecf20Sopenharmony_ci        if (address == NULL)
8348c2ecf20Sopenharmony_ci		return 0;
8358c2ecf20Sopenharmony_ci        return __pa(address) + PCI_DRAM_OFFSET;
8368c2ecf20Sopenharmony_ci}
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_cistatic inline void * bus_to_virt(unsigned long address)
8398c2ecf20Sopenharmony_ci{
8408c2ecf20Sopenharmony_ci        if (address == 0)
8418c2ecf20Sopenharmony_ci		return NULL;
8428c2ecf20Sopenharmony_ci        return __va(address - PCI_DRAM_OFFSET);
8438c2ecf20Sopenharmony_ci}
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci#define page_to_bus(page)	(page_to_phys(page) + PCI_DRAM_OFFSET)
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC32 */
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci/* access ports */
8508c2ecf20Sopenharmony_ci#define setbits32(_addr, _v) out_be32((_addr), in_be32(_addr) |  (_v))
8518c2ecf20Sopenharmony_ci#define clrbits32(_addr, _v) out_be32((_addr), in_be32(_addr) & ~(_v))
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci#define setbits16(_addr, _v) out_be16((_addr), in_be16(_addr) |  (_v))
8548c2ecf20Sopenharmony_ci#define clrbits16(_addr, _v) out_be16((_addr), in_be16(_addr) & ~(_v))
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci#define setbits8(_addr, _v) out_8((_addr), in_8(_addr) |  (_v))
8578c2ecf20Sopenharmony_ci#define clrbits8(_addr, _v) out_8((_addr), in_8(_addr) & ~(_v))
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci/* Clear and set bits in one shot.  These macros can be used to clear and
8608c2ecf20Sopenharmony_ci * set multiple bits in a register using a single read-modify-write.  These
8618c2ecf20Sopenharmony_ci * macros can also be used to set a multiple-bit bit pattern using a mask,
8628c2ecf20Sopenharmony_ci * by specifying the mask in the 'clear' parameter and the new bit pattern
8638c2ecf20Sopenharmony_ci * in the 'set' parameter.
8648c2ecf20Sopenharmony_ci */
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci#define clrsetbits(type, addr, clear, set) \
8678c2ecf20Sopenharmony_ci	out_##type((addr), (in_##type(addr) & ~(clear)) | (set))
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci#ifdef __powerpc64__
8708c2ecf20Sopenharmony_ci#define clrsetbits_be64(addr, clear, set) clrsetbits(be64, addr, clear, set)
8718c2ecf20Sopenharmony_ci#define clrsetbits_le64(addr, clear, set) clrsetbits(le64, addr, clear, set)
8728c2ecf20Sopenharmony_ci#endif
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci#define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set)
8758c2ecf20Sopenharmony_ci#define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set)
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci#define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set)
8788c2ecf20Sopenharmony_ci#define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set)
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci#define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set)
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci#endif /* __KERNEL__ */
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci#endif /* _ASM_POWERPC_IO_H */
885