1b8b572e1SStephen Rothwell #ifndef _ASM_POWERPC_IO_H 2b8b572e1SStephen Rothwell #define _ASM_POWERPC_IO_H 3b8b572e1SStephen Rothwell #ifdef __KERNEL__ 4b8b572e1SStephen Rothwell 5be135f40SAnton Blanchard #define ARCH_HAS_IOREMAP_WC 686c391bdSChristophe Leroy #ifdef CONFIG_PPC32 786c391bdSChristophe Leroy #define ARCH_HAS_IOREMAP_WT 886c391bdSChristophe Leroy #endif 9be135f40SAnton Blanchard 10b8b572e1SStephen Rothwell /* 11b8b572e1SStephen Rothwell * This program is free software; you can redistribute it and/or 12b8b572e1SStephen Rothwell * modify it under the terms of the GNU General Public License 13b8b572e1SStephen Rothwell * as published by the Free Software Foundation; either version 14b8b572e1SStephen Rothwell * 2 of the License, or (at your option) any later version. 15b8b572e1SStephen Rothwell */ 16b8b572e1SStephen Rothwell 17b8b572e1SStephen Rothwell /* Check of existence of legacy devices */ 18b8b572e1SStephen Rothwell extern int check_legacy_ioport(unsigned long base_port); 19b8b572e1SStephen Rothwell #define I8042_DATA_REG 0x60 20b8b572e1SStephen Rothwell #define FDC_BASE 0x3f0 21b8b572e1SStephen Rothwell 22e1612de9SHaren Myneni #if defined(CONFIG_PPC64) && defined(CONFIG_PCI) 23e1612de9SHaren Myneni extern struct pci_dev *isa_bridge_pcidev; 24e1612de9SHaren Myneni /* 25e1612de9SHaren Myneni * has legacy ISA devices ? 26e1612de9SHaren Myneni */ 27ac237b65SBenjamin Herrenschmidt #define arch_has_dev_port() (isa_bridge_pcidev != NULL || isa_io_special) 28e1612de9SHaren Myneni #endif 29e1612de9SHaren Myneni 30b8b572e1SStephen Rothwell #include <linux/device.h> 31b8b572e1SStephen Rothwell #include <linux/compiler.h> 32*6bf752daSChristophe Leroy #include <linux/mm.h> 33b8b572e1SStephen Rothwell #include <asm/page.h> 34b8b572e1SStephen Rothwell #include <asm/byteorder.h> 35b8b572e1SStephen Rothwell #include <asm/synch.h> 36b8b572e1SStephen Rothwell #include <asm/delay.h> 37b8b572e1SStephen Rothwell #include <asm/mmu.h> 3824bfa6a9SNicholas Piggin #include <asm/ppc_asm.h> 39*6bf752daSChristophe Leroy #include <asm/pgtable.h> 40b8b572e1SStephen Rothwell 41b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64 42b8b572e1SStephen Rothwell #include <asm/paca.h> 43b8b572e1SStephen Rothwell #endif 44b8b572e1SStephen Rothwell 45b8b572e1SStephen Rothwell #define SIO_CONFIG_RA 0x398 46b8b572e1SStephen Rothwell #define SIO_CONFIG_RD 0x399 47b8b572e1SStephen Rothwell 48b8b572e1SStephen Rothwell #define SLOW_DOWN_IO 49b8b572e1SStephen Rothwell 50b8b572e1SStephen Rothwell /* 32 bits uses slightly different variables for the various IO 51b8b572e1SStephen Rothwell * bases. Most of this file only uses _IO_BASE though which we 52b8b572e1SStephen Rothwell * define properly based on the platform 53b8b572e1SStephen Rothwell */ 54b8b572e1SStephen Rothwell #ifndef CONFIG_PCI 55b8b572e1SStephen Rothwell #define _IO_BASE 0 56b8b572e1SStephen Rothwell #define _ISA_MEM_BASE 0 57b8b572e1SStephen Rothwell #define PCI_DRAM_OFFSET 0 58b8b572e1SStephen Rothwell #elif defined(CONFIG_PPC32) 59b8b572e1SStephen Rothwell #define _IO_BASE isa_io_base 60b8b572e1SStephen Rothwell #define _ISA_MEM_BASE isa_mem_base 61b8b572e1SStephen Rothwell #define PCI_DRAM_OFFSET pci_dram_offset 62b8b572e1SStephen Rothwell #else 63b8b572e1SStephen Rothwell #define _IO_BASE pci_io_base 64b8b572e1SStephen Rothwell #define _ISA_MEM_BASE isa_mem_base 65b8b572e1SStephen Rothwell #define PCI_DRAM_OFFSET 0 66b8b572e1SStephen Rothwell #endif 67b8b572e1SStephen Rothwell 68b8b572e1SStephen Rothwell extern unsigned long isa_io_base; 69b8b572e1SStephen Rothwell extern unsigned long pci_io_base; 70b8b572e1SStephen Rothwell extern unsigned long pci_dram_offset; 71b8b572e1SStephen Rothwell 72b8b572e1SStephen Rothwell extern resource_size_t isa_mem_base; 73b8b572e1SStephen Rothwell 743fafe9c2SBenjamin Herrenschmidt /* Boolean set by platform if PIO accesses are suppored while _IO_BASE 753fafe9c2SBenjamin Herrenschmidt * is not set or addresses cannot be translated to MMIO. This is typically 763fafe9c2SBenjamin Herrenschmidt * set when the platform supports "special" PIO accesses via a non memory 773fafe9c2SBenjamin Herrenschmidt * mapped mechanism, and allows things like the early udbg UART code to 783fafe9c2SBenjamin Herrenschmidt * function. 793fafe9c2SBenjamin Herrenschmidt */ 803fafe9c2SBenjamin Herrenschmidt extern bool isa_io_special; 813fafe9c2SBenjamin Herrenschmidt 82ecd73cc5SBenjamin Herrenschmidt #ifdef CONFIG_PPC32 83ecd73cc5SBenjamin Herrenschmidt #if defined(CONFIG_PPC_INDIRECT_PIO) || defined(CONFIG_PPC_INDIRECT_MMIO) 84ecd73cc5SBenjamin Herrenschmidt #error CONFIG_PPC_INDIRECT_{PIO,MMIO} are not yet supported on 32 bits 85ecd73cc5SBenjamin Herrenschmidt #endif 86b8b572e1SStephen Rothwell #endif 87b8b572e1SStephen Rothwell 88b8b572e1SStephen Rothwell /* 89b8b572e1SStephen Rothwell * 90b8b572e1SStephen Rothwell * Low level MMIO accessors 91b8b572e1SStephen Rothwell * 92b8b572e1SStephen Rothwell * This provides the non-bus specific accessors to MMIO. Those are PowerPC 93b8b572e1SStephen Rothwell * specific and thus shouldn't be used in generic code. The accessors 94b8b572e1SStephen Rothwell * provided here are: 95b8b572e1SStephen Rothwell * 96b8b572e1SStephen Rothwell * in_8, in_le16, in_be16, in_le32, in_be32, in_le64, in_be64 97b8b572e1SStephen Rothwell * out_8, out_le16, out_be16, out_le32, out_be32, out_le64, out_be64 98b8b572e1SStephen Rothwell * _insb, _insw_ns, _insl_ns, _outsb, _outsw_ns, _outsl_ns 99b8b572e1SStephen Rothwell * 100b8b572e1SStephen Rothwell * Those operate directly on a kernel virtual address. Note that the prototype 101b8b572e1SStephen Rothwell * for the out_* accessors has the arguments in opposite order from the usual 102b8b572e1SStephen Rothwell * linux PCI accessors. Unlike those, they take the address first and the value 103b8b572e1SStephen Rothwell * next. 104b8b572e1SStephen Rothwell * 105b8b572e1SStephen Rothwell * Note: I might drop the _ns suffix on the stream operations soon as it is 106b8b572e1SStephen Rothwell * simply normal for stream operations to not swap in the first place. 107b8b572e1SStephen Rothwell * 108b8b572e1SStephen Rothwell */ 109b8b572e1SStephen Rothwell 110b8b572e1SStephen Rothwell #ifdef CONFIG_PPC64 111b8b572e1SStephen Rothwell #define IO_SET_SYNC_FLAG() do { local_paca->io_sync = 1; } while(0) 112b8b572e1SStephen Rothwell #else 113b8b572e1SStephen Rothwell #define IO_SET_SYNC_FLAG() 114b8b572e1SStephen Rothwell #endif 115b8b572e1SStephen Rothwell 11615cba23eSIan Munsie #define DEF_MMIO_IN_X(name, size, insn) \ 117b8b572e1SStephen Rothwell static inline u##size name(const volatile u##size __iomem *addr) \ 118b8b572e1SStephen Rothwell { \ 119b8b572e1SStephen Rothwell u##size ret; \ 120b8b572e1SStephen Rothwell __asm__ __volatile__("sync;"#insn" %0,%y1;twi 0,%0,0;isync" \ 121b8b572e1SStephen Rothwell : "=r" (ret) : "Z" (*addr) : "memory"); \ 122b8b572e1SStephen Rothwell return ret; \ 123b8b572e1SStephen Rothwell } 124b8b572e1SStephen Rothwell 12515cba23eSIan Munsie #define DEF_MMIO_OUT_X(name, size, insn) \ 126b8b572e1SStephen Rothwell static inline void name(volatile u##size __iomem *addr, u##size val) \ 127b8b572e1SStephen Rothwell { \ 128b8b572e1SStephen Rothwell __asm__ __volatile__("sync;"#insn" %1,%y0" \ 129b8b572e1SStephen Rothwell : "=Z" (*addr) : "r" (val) : "memory"); \ 130b8b572e1SStephen Rothwell IO_SET_SYNC_FLAG(); \ 131b8b572e1SStephen Rothwell } 132b8b572e1SStephen Rothwell 13315cba23eSIan Munsie #define DEF_MMIO_IN_D(name, size, insn) \ 134b8b572e1SStephen Rothwell static inline u##size name(const volatile u##size __iomem *addr) \ 135b8b572e1SStephen Rothwell { \ 136b8b572e1SStephen Rothwell u##size ret; \ 137b8b572e1SStephen Rothwell __asm__ __volatile__("sync;"#insn"%U1%X1 %0,%1;twi 0,%0,0;isync"\ 138b8b572e1SStephen Rothwell : "=r" (ret) : "m" (*addr) : "memory"); \ 139b8b572e1SStephen Rothwell return ret; \ 140b8b572e1SStephen Rothwell } 141b8b572e1SStephen Rothwell 14215cba23eSIan Munsie #define DEF_MMIO_OUT_D(name, size, insn) \ 143b8b572e1SStephen Rothwell static inline void name(volatile u##size __iomem *addr, u##size val) \ 144b8b572e1SStephen Rothwell { \ 145b8b572e1SStephen Rothwell __asm__ __volatile__("sync;"#insn"%U0%X0 %1,%0" \ 146b8b572e1SStephen Rothwell : "=m" (*addr) : "r" (val) : "memory"); \ 147b8b572e1SStephen Rothwell IO_SET_SYNC_FLAG(); \ 148b8b572e1SStephen Rothwell } 149b8b572e1SStephen Rothwell 15015cba23eSIan Munsie DEF_MMIO_IN_D(in_8, 8, lbz); 15115cba23eSIan Munsie DEF_MMIO_OUT_D(out_8, 8, stb); 152b8b572e1SStephen Rothwell 15315cba23eSIan Munsie #ifdef __BIG_ENDIAN__ 15415cba23eSIan Munsie DEF_MMIO_IN_D(in_be16, 16, lhz); 15515cba23eSIan Munsie DEF_MMIO_IN_D(in_be32, 32, lwz); 15615cba23eSIan Munsie DEF_MMIO_IN_X(in_le16, 16, lhbrx); 15715cba23eSIan Munsie DEF_MMIO_IN_X(in_le32, 32, lwbrx); 158b8b572e1SStephen Rothwell 15915cba23eSIan Munsie DEF_MMIO_OUT_D(out_be16, 16, sth); 16015cba23eSIan Munsie DEF_MMIO_OUT_D(out_be32, 32, stw); 16115cba23eSIan Munsie DEF_MMIO_OUT_X(out_le16, 16, sthbrx); 16215cba23eSIan Munsie DEF_MMIO_OUT_X(out_le32, 32, stwbrx); 16315cba23eSIan Munsie #else 16415cba23eSIan Munsie DEF_MMIO_IN_X(in_be16, 16, lhbrx); 16515cba23eSIan Munsie DEF_MMIO_IN_X(in_be32, 32, lwbrx); 16615cba23eSIan Munsie DEF_MMIO_IN_D(in_le16, 16, lhz); 16715cba23eSIan Munsie DEF_MMIO_IN_D(in_le32, 32, lwz); 16815cba23eSIan Munsie 16915cba23eSIan Munsie DEF_MMIO_OUT_X(out_be16, 16, sthbrx); 17015cba23eSIan Munsie DEF_MMIO_OUT_X(out_be32, 32, stwbrx); 17115cba23eSIan Munsie DEF_MMIO_OUT_D(out_le16, 16, sth); 17215cba23eSIan Munsie DEF_MMIO_OUT_D(out_le32, 32, stw); 17315cba23eSIan Munsie 17415cba23eSIan Munsie #endif /* __BIG_ENDIAN */ 175b8b572e1SStephen Rothwell 176b8b572e1SStephen Rothwell #ifdef __powerpc64__ 17715cba23eSIan Munsie 17815cba23eSIan Munsie #ifdef __BIG_ENDIAN__ 17915cba23eSIan Munsie DEF_MMIO_OUT_D(out_be64, 64, std); 18015cba23eSIan Munsie DEF_MMIO_IN_D(in_be64, 64, ld); 181b8b572e1SStephen Rothwell 182b8b572e1SStephen Rothwell /* There is no asm instructions for 64 bits reverse loads and stores */ 183b8b572e1SStephen Rothwell static inline u64 in_le64(const volatile u64 __iomem *addr) 184b8b572e1SStephen Rothwell { 185b8b572e1SStephen Rothwell return swab64(in_be64(addr)); 186b8b572e1SStephen Rothwell } 187b8b572e1SStephen Rothwell 188b8b572e1SStephen Rothwell static inline void out_le64(volatile u64 __iomem *addr, u64 val) 189b8b572e1SStephen Rothwell { 190b8b572e1SStephen Rothwell out_be64(addr, swab64(val)); 191b8b572e1SStephen Rothwell } 19215cba23eSIan Munsie #else 19315cba23eSIan Munsie DEF_MMIO_OUT_D(out_le64, 64, std); 19415cba23eSIan Munsie DEF_MMIO_IN_D(in_le64, 64, ld); 19515cba23eSIan Munsie 19615cba23eSIan Munsie /* There is no asm instructions for 64 bits reverse loads and stores */ 19715cba23eSIan Munsie static inline u64 in_be64(const volatile u64 __iomem *addr) 19815cba23eSIan Munsie { 19915cba23eSIan Munsie return swab64(in_le64(addr)); 20015cba23eSIan Munsie } 20115cba23eSIan Munsie 20215cba23eSIan Munsie static inline void out_be64(volatile u64 __iomem *addr, u64 val) 20315cba23eSIan Munsie { 20415cba23eSIan Munsie out_le64(addr, swab64(val)); 20515cba23eSIan Munsie } 20615cba23eSIan Munsie 20715cba23eSIan Munsie #endif 208b8b572e1SStephen Rothwell #endif /* __powerpc64__ */ 209b8b572e1SStephen Rothwell 210b8b572e1SStephen Rothwell /* 211b8b572e1SStephen Rothwell * Low level IO stream instructions are defined out of line for now 212b8b572e1SStephen Rothwell */ 213b8b572e1SStephen Rothwell extern void _insb(const volatile u8 __iomem *addr, void *buf, long count); 214b8b572e1SStephen Rothwell extern void _outsb(volatile u8 __iomem *addr,const void *buf,long count); 215b8b572e1SStephen Rothwell extern void _insw_ns(const volatile u16 __iomem *addr, void *buf, long count); 216b8b572e1SStephen Rothwell extern void _outsw_ns(volatile u16 __iomem *addr, const void *buf, long count); 217b8b572e1SStephen Rothwell extern void _insl_ns(const volatile u32 __iomem *addr, void *buf, long count); 218b8b572e1SStephen Rothwell extern void _outsl_ns(volatile u32 __iomem *addr, const void *buf, long count); 219b8b572e1SStephen Rothwell 220b8b572e1SStephen Rothwell /* The _ns naming is historical and will be removed. For now, just #define 221b8b572e1SStephen Rothwell * the non _ns equivalent names 222b8b572e1SStephen Rothwell */ 223b8b572e1SStephen Rothwell #define _insw _insw_ns 224b8b572e1SStephen Rothwell #define _insl _insl_ns 225b8b572e1SStephen Rothwell #define _outsw _outsw_ns 226b8b572e1SStephen Rothwell #define _outsl _outsl_ns 227b8b572e1SStephen Rothwell 228b8b572e1SStephen Rothwell 229b8b572e1SStephen Rothwell /* 230b8b572e1SStephen Rothwell * memset_io, memcpy_toio, memcpy_fromio base implementations are out of line 231b8b572e1SStephen Rothwell */ 232b8b572e1SStephen Rothwell 233b8b572e1SStephen Rothwell extern void _memset_io(volatile void __iomem *addr, int c, unsigned long n); 234b8b572e1SStephen Rothwell extern void _memcpy_fromio(void *dest, const volatile void __iomem *src, 235b8b572e1SStephen Rothwell unsigned long n); 236b8b572e1SStephen Rothwell extern void _memcpy_toio(volatile void __iomem *dest, const void *src, 237b8b572e1SStephen Rothwell unsigned long n); 238b8b572e1SStephen Rothwell 239b8b572e1SStephen Rothwell /* 240b8b572e1SStephen Rothwell * 241b8b572e1SStephen Rothwell * PCI and standard ISA accessors 242b8b572e1SStephen Rothwell * 243b8b572e1SStephen Rothwell * Those are globally defined linux accessors for devices on PCI or ISA 244b8b572e1SStephen Rothwell * busses. They follow the Linux defined semantics. The current implementation 245b8b572e1SStephen Rothwell * for PowerPC is as close as possible to the x86 version of these, and thus 246b8b572e1SStephen Rothwell * provides fairly heavy weight barriers for the non-raw versions 247b8b572e1SStephen Rothwell * 248ecd73cc5SBenjamin Herrenschmidt * In addition, they support a hook mechanism when CONFIG_PPC_INDIRECT_MMIO 249ecd73cc5SBenjamin Herrenschmidt * or CONFIG_PPC_INDIRECT_PIO are set allowing the platform to provide its 250ecd73cc5SBenjamin Herrenschmidt * own implementation of some or all of the accessors. 251b8b572e1SStephen Rothwell */ 252b8b572e1SStephen Rothwell 253b8b572e1SStephen Rothwell /* 254b8b572e1SStephen Rothwell * Include the EEH definitions when EEH is enabled only so they don't get 255b8b572e1SStephen Rothwell * in the way when building for 32 bits 256b8b572e1SStephen Rothwell */ 257b8b572e1SStephen Rothwell #ifdef CONFIG_EEH 258b8b572e1SStephen Rothwell #include <asm/eeh.h> 259b8b572e1SStephen Rothwell #endif 260b8b572e1SStephen Rothwell 261b8b572e1SStephen Rothwell /* Shortcut to the MMIO argument pointer */ 262b8b572e1SStephen Rothwell #define PCI_IO_ADDR volatile void __iomem * 263b8b572e1SStephen Rothwell 264b8b572e1SStephen Rothwell /* Indirect IO address tokens: 265b8b572e1SStephen Rothwell * 266ecd73cc5SBenjamin Herrenschmidt * When CONFIG_PPC_INDIRECT_MMIO is set, the platform can provide hooks 267ecd73cc5SBenjamin Herrenschmidt * on all MMIOs. (Note that this is all 64 bits only for now) 268b8b572e1SStephen Rothwell * 269446957baSAdam Buchbinder * To help platforms who may need to differentiate MMIO addresses in 270b8b572e1SStephen Rothwell * their hooks, a bitfield is reserved for use by the platform near the 271b8b572e1SStephen Rothwell * top of MMIO addresses (not PIO, those have to cope the hard way). 272b8b572e1SStephen Rothwell * 27343c6494fSMichael Ellerman * The highest address in the kernel virtual space are: 274b8b572e1SStephen Rothwell * 27543c6494fSMichael Ellerman * d0003fffffffffff # with Hash MMU 27643c6494fSMichael Ellerman * c00fffffffffffff # with Radix MMU 277b8b572e1SStephen Rothwell * 27843c6494fSMichael Ellerman * The top 4 bits are reserved as the region ID on hash, leaving us 8 bits 27943c6494fSMichael Ellerman * that can be used for the field. 280b8b572e1SStephen Rothwell * 281b8b572e1SStephen Rothwell * The direct IO mapping operations will then mask off those bits 282b8b572e1SStephen Rothwell * before doing the actual access, though that only happen when 283ecd73cc5SBenjamin Herrenschmidt * CONFIG_PPC_INDIRECT_MMIO is set, thus be careful when you use that 284b8b572e1SStephen Rothwell * mechanism 285ecd73cc5SBenjamin Herrenschmidt * 286ecd73cc5SBenjamin Herrenschmidt * For PIO, there is a separate CONFIG_PPC_INDIRECT_PIO which makes 287ecd73cc5SBenjamin Herrenschmidt * all PIO functions call through a hook. 288b8b572e1SStephen Rothwell */ 289b8b572e1SStephen Rothwell 290ecd73cc5SBenjamin Herrenschmidt #ifdef CONFIG_PPC_INDIRECT_MMIO 29143c6494fSMichael Ellerman #define PCI_IO_IND_TOKEN_SHIFT 52 29243c6494fSMichael Ellerman #define PCI_IO_IND_TOKEN_MASK (0xfful << PCI_IO_IND_TOKEN_SHIFT) 293b8b572e1SStephen Rothwell #define PCI_FIX_ADDR(addr) \ 294b8b572e1SStephen Rothwell ((PCI_IO_ADDR)(((unsigned long)(addr)) & ~PCI_IO_IND_TOKEN_MASK)) 295b8b572e1SStephen Rothwell #define PCI_GET_ADDR_TOKEN(addr) \ 296b8b572e1SStephen Rothwell (((unsigned long)(addr) & PCI_IO_IND_TOKEN_MASK) >> \ 297b8b572e1SStephen Rothwell PCI_IO_IND_TOKEN_SHIFT) 298b8b572e1SStephen Rothwell #define PCI_SET_ADDR_TOKEN(addr, token) \ 299b8b572e1SStephen Rothwell do { \ 300b8b572e1SStephen Rothwell unsigned long __a = (unsigned long)(addr); \ 301b8b572e1SStephen Rothwell __a &= ~PCI_IO_IND_TOKEN_MASK; \ 302b8b572e1SStephen Rothwell __a |= ((unsigned long)(token)) << PCI_IO_IND_TOKEN_SHIFT; \ 303b8b572e1SStephen Rothwell (addr) = (void __iomem *)__a; \ 304b8b572e1SStephen Rothwell } while(0) 305b8b572e1SStephen Rothwell #else 306b8b572e1SStephen Rothwell #define PCI_FIX_ADDR(addr) (addr) 307b8b572e1SStephen Rothwell #endif 308b8b572e1SStephen Rothwell 309b8b572e1SStephen Rothwell 310b8b572e1SStephen Rothwell /* 311b8b572e1SStephen Rothwell * Non ordered and non-swapping "raw" accessors 312b8b572e1SStephen Rothwell */ 313b8b572e1SStephen Rothwell 314b8b572e1SStephen Rothwell static inline unsigned char __raw_readb(const volatile void __iomem *addr) 315b8b572e1SStephen Rothwell { 316b8b572e1SStephen Rothwell return *(volatile unsigned char __force *)PCI_FIX_ADDR(addr); 317b8b572e1SStephen Rothwell } 318b8b572e1SStephen Rothwell static inline unsigned short __raw_readw(const volatile void __iomem *addr) 319b8b572e1SStephen Rothwell { 320b8b572e1SStephen Rothwell return *(volatile unsigned short __force *)PCI_FIX_ADDR(addr); 321b8b572e1SStephen Rothwell } 322b8b572e1SStephen Rothwell static inline unsigned int __raw_readl(const volatile void __iomem *addr) 323b8b572e1SStephen Rothwell { 324b8b572e1SStephen Rothwell return *(volatile unsigned int __force *)PCI_FIX_ADDR(addr); 325b8b572e1SStephen Rothwell } 326b8b572e1SStephen Rothwell static inline void __raw_writeb(unsigned char v, volatile void __iomem *addr) 327b8b572e1SStephen Rothwell { 328b8b572e1SStephen Rothwell *(volatile unsigned char __force *)PCI_FIX_ADDR(addr) = v; 329b8b572e1SStephen Rothwell } 330b8b572e1SStephen Rothwell static inline void __raw_writew(unsigned short v, volatile void __iomem *addr) 331b8b572e1SStephen Rothwell { 332b8b572e1SStephen Rothwell *(volatile unsigned short __force *)PCI_FIX_ADDR(addr) = v; 333b8b572e1SStephen Rothwell } 334b8b572e1SStephen Rothwell static inline void __raw_writel(unsigned int v, volatile void __iomem *addr) 335b8b572e1SStephen Rothwell { 336b8b572e1SStephen Rothwell *(volatile unsigned int __force *)PCI_FIX_ADDR(addr) = v; 337b8b572e1SStephen Rothwell } 338b8b572e1SStephen Rothwell 339b8b572e1SStephen Rothwell #ifdef __powerpc64__ 340b8b572e1SStephen Rothwell static inline unsigned long __raw_readq(const volatile void __iomem *addr) 341b8b572e1SStephen Rothwell { 342b8b572e1SStephen Rothwell return *(volatile unsigned long __force *)PCI_FIX_ADDR(addr); 343b8b572e1SStephen Rothwell } 344b8b572e1SStephen Rothwell static inline void __raw_writeq(unsigned long v, volatile void __iomem *addr) 345b8b572e1SStephen Rothwell { 346b8b572e1SStephen Rothwell *(volatile unsigned long __force *)PCI_FIX_ADDR(addr) = v; 347b8b572e1SStephen Rothwell } 348a84bf321SAlistair Popple 3498056fe28SMichael Ellerman static inline void __raw_writeq_be(unsigned long v, volatile void __iomem *addr) 3508056fe28SMichael Ellerman { 3518056fe28SMichael Ellerman __raw_writeq((__force unsigned long)cpu_to_be64(v), addr); 3528056fe28SMichael Ellerman } 3538056fe28SMichael Ellerman 354a84bf321SAlistair Popple /* 355d381d7caSBenjamin Herrenschmidt * Real mode versions of the above. Those instructions are only supposed 356d381d7caSBenjamin Herrenschmidt * to be used in hypervisor real mode as per the architecture spec. 357a84bf321SAlistair Popple */ 358d381d7caSBenjamin Herrenschmidt static inline void __raw_rm_writeb(u8 val, volatile void __iomem *paddr) 359d381d7caSBenjamin Herrenschmidt { 360d381d7caSBenjamin Herrenschmidt __asm__ __volatile__("stbcix %0,0,%1" 361d381d7caSBenjamin Herrenschmidt : : "r" (val), "r" (paddr) : "memory"); 362d381d7caSBenjamin Herrenschmidt } 363d381d7caSBenjamin Herrenschmidt 364d381d7caSBenjamin Herrenschmidt static inline void __raw_rm_writew(u16 val, volatile void __iomem *paddr) 365d381d7caSBenjamin Herrenschmidt { 366d381d7caSBenjamin Herrenschmidt __asm__ __volatile__("sthcix %0,0,%1" 367d381d7caSBenjamin Herrenschmidt : : "r" (val), "r" (paddr) : "memory"); 368d381d7caSBenjamin Herrenschmidt } 369d381d7caSBenjamin Herrenschmidt 370d381d7caSBenjamin Herrenschmidt static inline void __raw_rm_writel(u32 val, volatile void __iomem *paddr) 371d381d7caSBenjamin Herrenschmidt { 372d381d7caSBenjamin Herrenschmidt __asm__ __volatile__("stwcix %0,0,%1" 373d381d7caSBenjamin Herrenschmidt : : "r" (val), "r" (paddr) : "memory"); 374d381d7caSBenjamin Herrenschmidt } 375d381d7caSBenjamin Herrenschmidt 376a84bf321SAlistair Popple static inline void __raw_rm_writeq(u64 val, volatile void __iomem *paddr) 377a84bf321SAlistair Popple { 378a84bf321SAlistair Popple __asm__ __volatile__("stdcix %0,0,%1" 379a84bf321SAlistair Popple : : "r" (val), "r" (paddr) : "memory"); 380a84bf321SAlistair Popple } 381a84bf321SAlistair Popple 3828056fe28SMichael Ellerman static inline void __raw_rm_writeq_be(u64 val, volatile void __iomem *paddr) 3838056fe28SMichael Ellerman { 3848056fe28SMichael Ellerman __raw_rm_writeq((__force u64)cpu_to_be64(val), paddr); 3858056fe28SMichael Ellerman } 3868056fe28SMichael Ellerman 387d381d7caSBenjamin Herrenschmidt static inline u8 __raw_rm_readb(volatile void __iomem *paddr) 388d381d7caSBenjamin Herrenschmidt { 389d381d7caSBenjamin Herrenschmidt u8 ret; 390d381d7caSBenjamin Herrenschmidt __asm__ __volatile__("lbzcix %0,0, %1" 391d381d7caSBenjamin Herrenschmidt : "=r" (ret) : "r" (paddr) : "memory"); 392d381d7caSBenjamin Herrenschmidt return ret; 393d381d7caSBenjamin Herrenschmidt } 394d381d7caSBenjamin Herrenschmidt 395d381d7caSBenjamin Herrenschmidt static inline u16 __raw_rm_readw(volatile void __iomem *paddr) 396d381d7caSBenjamin Herrenschmidt { 397d381d7caSBenjamin Herrenschmidt u16 ret; 398d381d7caSBenjamin Herrenschmidt __asm__ __volatile__("lhzcix %0,0, %1" 399d381d7caSBenjamin Herrenschmidt : "=r" (ret) : "r" (paddr) : "memory"); 400d381d7caSBenjamin Herrenschmidt return ret; 401d381d7caSBenjamin Herrenschmidt } 402d381d7caSBenjamin Herrenschmidt 403d381d7caSBenjamin Herrenschmidt static inline u32 __raw_rm_readl(volatile void __iomem *paddr) 404d381d7caSBenjamin Herrenschmidt { 405d381d7caSBenjamin Herrenschmidt u32 ret; 406d381d7caSBenjamin Herrenschmidt __asm__ __volatile__("lwzcix %0,0, %1" 407d381d7caSBenjamin Herrenschmidt : "=r" (ret) : "r" (paddr) : "memory"); 408d381d7caSBenjamin Herrenschmidt return ret; 409d381d7caSBenjamin Herrenschmidt } 410d381d7caSBenjamin Herrenschmidt 411d381d7caSBenjamin Herrenschmidt static inline u64 __raw_rm_readq(volatile void __iomem *paddr) 412d381d7caSBenjamin Herrenschmidt { 413d381d7caSBenjamin Herrenschmidt u64 ret; 414d381d7caSBenjamin Herrenschmidt __asm__ __volatile__("ldcix %0,0, %1" 415d381d7caSBenjamin Herrenschmidt : "=r" (ret) : "r" (paddr) : "memory"); 416d381d7caSBenjamin Herrenschmidt return ret; 417d381d7caSBenjamin Herrenschmidt } 418b8b572e1SStephen Rothwell #endif /* __powerpc64__ */ 419b8b572e1SStephen Rothwell 420b8b572e1SStephen Rothwell /* 421b8b572e1SStephen Rothwell * 422b8b572e1SStephen Rothwell * PCI PIO and MMIO accessors. 423b8b572e1SStephen Rothwell * 424b8b572e1SStephen Rothwell * 425b8b572e1SStephen Rothwell * On 32 bits, PIO operations have a recovery mechanism in case they trigger 426b8b572e1SStephen Rothwell * machine checks (which they occasionally do when probing non existing 427b8b572e1SStephen Rothwell * IO ports on some platforms, like PowerMac and 8xx). 428b8b572e1SStephen Rothwell * I always found it to be of dubious reliability and I am tempted to get 429b8b572e1SStephen Rothwell * rid of it one of these days. So if you think it's important to keep it, 430b8b572e1SStephen Rothwell * please voice up asap. We never had it for 64 bits and I do not intend 431b8b572e1SStephen Rothwell * to port it over 432b8b572e1SStephen Rothwell */ 433b8b572e1SStephen Rothwell 434b8b572e1SStephen Rothwell #ifdef CONFIG_PPC32 435b8b572e1SStephen Rothwell 436b8b572e1SStephen Rothwell #define __do_in_asm(name, op) \ 437b8b572e1SStephen Rothwell static inline unsigned int name(unsigned int port) \ 438b8b572e1SStephen Rothwell { \ 439b8b572e1SStephen Rothwell unsigned int x; \ 440b8b572e1SStephen Rothwell __asm__ __volatile__( \ 441b8b572e1SStephen Rothwell "sync\n" \ 442b8b572e1SStephen Rothwell "0:" op " %0,0,%1\n" \ 443b8b572e1SStephen Rothwell "1: twi 0,%0,0\n" \ 444b8b572e1SStephen Rothwell "2: isync\n" \ 445b8b572e1SStephen Rothwell "3: nop\n" \ 446b8b572e1SStephen Rothwell "4:\n" \ 447b8b572e1SStephen Rothwell ".section .fixup,\"ax\"\n" \ 448b8b572e1SStephen Rothwell "5: li %0,-1\n" \ 449b8b572e1SStephen Rothwell " b 4b\n" \ 450b8b572e1SStephen Rothwell ".previous\n" \ 45124bfa6a9SNicholas Piggin EX_TABLE(0b, 5b) \ 45224bfa6a9SNicholas Piggin EX_TABLE(1b, 5b) \ 45324bfa6a9SNicholas Piggin EX_TABLE(2b, 5b) \ 45424bfa6a9SNicholas Piggin EX_TABLE(3b, 5b) \ 455b8b572e1SStephen Rothwell : "=&r" (x) \ 456b8b572e1SStephen Rothwell : "r" (port + _IO_BASE) \ 457b8b572e1SStephen Rothwell : "memory"); \ 458b8b572e1SStephen Rothwell return x; \ 459b8b572e1SStephen Rothwell } 460b8b572e1SStephen Rothwell 461b8b572e1SStephen Rothwell #define __do_out_asm(name, op) \ 462b8b572e1SStephen Rothwell static inline void name(unsigned int val, unsigned int port) \ 463b8b572e1SStephen Rothwell { \ 464b8b572e1SStephen Rothwell __asm__ __volatile__( \ 465b8b572e1SStephen Rothwell "sync\n" \ 466b8b572e1SStephen Rothwell "0:" op " %0,0,%1\n" \ 467b8b572e1SStephen Rothwell "1: sync\n" \ 468b8b572e1SStephen Rothwell "2:\n" \ 46924bfa6a9SNicholas Piggin EX_TABLE(0b, 2b) \ 47024bfa6a9SNicholas Piggin EX_TABLE(1b, 2b) \ 471b8b572e1SStephen Rothwell : : "r" (val), "r" (port + _IO_BASE) \ 472b8b572e1SStephen Rothwell : "memory"); \ 473b8b572e1SStephen Rothwell } 474b8b572e1SStephen Rothwell 475b8b572e1SStephen Rothwell __do_in_asm(_rec_inb, "lbzx") 476b8b572e1SStephen Rothwell __do_in_asm(_rec_inw, "lhbrx") 477b8b572e1SStephen Rothwell __do_in_asm(_rec_inl, "lwbrx") 478b8b572e1SStephen Rothwell __do_out_asm(_rec_outb, "stbx") 479b8b572e1SStephen Rothwell __do_out_asm(_rec_outw, "sthbrx") 480b8b572e1SStephen Rothwell __do_out_asm(_rec_outl, "stwbrx") 481b8b572e1SStephen Rothwell 482b8b572e1SStephen Rothwell #endif /* CONFIG_PPC32 */ 483b8b572e1SStephen Rothwell 484b8b572e1SStephen Rothwell /* The "__do_*" operations below provide the actual "base" implementation 48542b2aa86SJustin P. Mattock * for each of the defined accessors. Some of them use the out_* functions 486b8b572e1SStephen Rothwell * directly, some of them still use EEH, though we might change that in the 487b8b572e1SStephen Rothwell * future. Those macros below provide the necessary argument swapping and 488b8b572e1SStephen Rothwell * handling of the IO base for PIO. 489b8b572e1SStephen Rothwell * 490b8b572e1SStephen Rothwell * They are themselves used by the macros that define the actual accessors 491b8b572e1SStephen Rothwell * and can be used by the hooks if any. 492b8b572e1SStephen Rothwell * 493b8b572e1SStephen Rothwell * Note that PIO operations are always defined in terms of their corresonding 494b8b572e1SStephen Rothwell * MMIO operations. That allows platforms like iSeries who want to modify the 495b8b572e1SStephen Rothwell * behaviour of both to only hook on the MMIO version and get both. It's also 496b8b572e1SStephen Rothwell * possible to hook directly at the toplevel PIO operation if they have to 497b8b572e1SStephen Rothwell * be handled differently 498b8b572e1SStephen Rothwell */ 499b8b572e1SStephen Rothwell #define __do_writeb(val, addr) out_8(PCI_FIX_ADDR(addr), val) 500b8b572e1SStephen Rothwell #define __do_writew(val, addr) out_le16(PCI_FIX_ADDR(addr), val) 501b8b572e1SStephen Rothwell #define __do_writel(val, addr) out_le32(PCI_FIX_ADDR(addr), val) 502b8b572e1SStephen Rothwell #define __do_writeq(val, addr) out_le64(PCI_FIX_ADDR(addr), val) 503b8b572e1SStephen Rothwell #define __do_writew_be(val, addr) out_be16(PCI_FIX_ADDR(addr), val) 504b8b572e1SStephen Rothwell #define __do_writel_be(val, addr) out_be32(PCI_FIX_ADDR(addr), val) 505b8b572e1SStephen Rothwell #define __do_writeq_be(val, addr) out_be64(PCI_FIX_ADDR(addr), val) 506b8b572e1SStephen Rothwell 507b8b572e1SStephen Rothwell #ifdef CONFIG_EEH 508b8b572e1SStephen Rothwell #define __do_readb(addr) eeh_readb(PCI_FIX_ADDR(addr)) 509b8b572e1SStephen Rothwell #define __do_readw(addr) eeh_readw(PCI_FIX_ADDR(addr)) 510b8b572e1SStephen Rothwell #define __do_readl(addr) eeh_readl(PCI_FIX_ADDR(addr)) 511b8b572e1SStephen Rothwell #define __do_readq(addr) eeh_readq(PCI_FIX_ADDR(addr)) 512b8b572e1SStephen Rothwell #define __do_readw_be(addr) eeh_readw_be(PCI_FIX_ADDR(addr)) 513b8b572e1SStephen Rothwell #define __do_readl_be(addr) eeh_readl_be(PCI_FIX_ADDR(addr)) 514b8b572e1SStephen Rothwell #define __do_readq_be(addr) eeh_readq_be(PCI_FIX_ADDR(addr)) 515b8b572e1SStephen Rothwell #else /* CONFIG_EEH */ 516b8b572e1SStephen Rothwell #define __do_readb(addr) in_8(PCI_FIX_ADDR(addr)) 517b8b572e1SStephen Rothwell #define __do_readw(addr) in_le16(PCI_FIX_ADDR(addr)) 518b8b572e1SStephen Rothwell #define __do_readl(addr) in_le32(PCI_FIX_ADDR(addr)) 519b8b572e1SStephen Rothwell #define __do_readq(addr) in_le64(PCI_FIX_ADDR(addr)) 520b8b572e1SStephen Rothwell #define __do_readw_be(addr) in_be16(PCI_FIX_ADDR(addr)) 521b8b572e1SStephen Rothwell #define __do_readl_be(addr) in_be32(PCI_FIX_ADDR(addr)) 522b8b572e1SStephen Rothwell #define __do_readq_be(addr) in_be64(PCI_FIX_ADDR(addr)) 523b8b572e1SStephen Rothwell #endif /* !defined(CONFIG_EEH) */ 524b8b572e1SStephen Rothwell 525b8b572e1SStephen Rothwell #ifdef CONFIG_PPC32 526b8b572e1SStephen Rothwell #define __do_outb(val, port) _rec_outb(val, port) 527b8b572e1SStephen Rothwell #define __do_outw(val, port) _rec_outw(val, port) 528b8b572e1SStephen Rothwell #define __do_outl(val, port) _rec_outl(val, port) 529b8b572e1SStephen Rothwell #define __do_inb(port) _rec_inb(port) 530b8b572e1SStephen Rothwell #define __do_inw(port) _rec_inw(port) 531b8b572e1SStephen Rothwell #define __do_inl(port) _rec_inl(port) 532b8b572e1SStephen Rothwell #else /* CONFIG_PPC32 */ 533b8b572e1SStephen Rothwell #define __do_outb(val, port) writeb(val,(PCI_IO_ADDR)_IO_BASE+port); 534b8b572e1SStephen Rothwell #define __do_outw(val, port) writew(val,(PCI_IO_ADDR)_IO_BASE+port); 535b8b572e1SStephen Rothwell #define __do_outl(val, port) writel(val,(PCI_IO_ADDR)_IO_BASE+port); 536b8b572e1SStephen Rothwell #define __do_inb(port) readb((PCI_IO_ADDR)_IO_BASE + port); 537b8b572e1SStephen Rothwell #define __do_inw(port) readw((PCI_IO_ADDR)_IO_BASE + port); 538b8b572e1SStephen Rothwell #define __do_inl(port) readl((PCI_IO_ADDR)_IO_BASE + port); 539b8b572e1SStephen Rothwell #endif /* !CONFIG_PPC32 */ 540b8b572e1SStephen Rothwell 541b8b572e1SStephen Rothwell #ifdef CONFIG_EEH 542b8b572e1SStephen Rothwell #define __do_readsb(a, b, n) eeh_readsb(PCI_FIX_ADDR(a), (b), (n)) 543b8b572e1SStephen Rothwell #define __do_readsw(a, b, n) eeh_readsw(PCI_FIX_ADDR(a), (b), (n)) 544b8b572e1SStephen Rothwell #define __do_readsl(a, b, n) eeh_readsl(PCI_FIX_ADDR(a), (b), (n)) 545b8b572e1SStephen Rothwell #else /* CONFIG_EEH */ 546b8b572e1SStephen Rothwell #define __do_readsb(a, b, n) _insb(PCI_FIX_ADDR(a), (b), (n)) 547b8b572e1SStephen Rothwell #define __do_readsw(a, b, n) _insw(PCI_FIX_ADDR(a), (b), (n)) 548b8b572e1SStephen Rothwell #define __do_readsl(a, b, n) _insl(PCI_FIX_ADDR(a), (b), (n)) 549b8b572e1SStephen Rothwell #endif /* !CONFIG_EEH */ 550b8b572e1SStephen Rothwell #define __do_writesb(a, b, n) _outsb(PCI_FIX_ADDR(a),(b),(n)) 551b8b572e1SStephen Rothwell #define __do_writesw(a, b, n) _outsw(PCI_FIX_ADDR(a),(b),(n)) 552b8b572e1SStephen Rothwell #define __do_writesl(a, b, n) _outsl(PCI_FIX_ADDR(a),(b),(n)) 553b8b572e1SStephen Rothwell 554b8b572e1SStephen Rothwell #define __do_insb(p, b, n) readsb((PCI_IO_ADDR)_IO_BASE+(p), (b), (n)) 555b8b572e1SStephen Rothwell #define __do_insw(p, b, n) readsw((PCI_IO_ADDR)_IO_BASE+(p), (b), (n)) 556b8b572e1SStephen Rothwell #define __do_insl(p, b, n) readsl((PCI_IO_ADDR)_IO_BASE+(p), (b), (n)) 557b8b572e1SStephen Rothwell #define __do_outsb(p, b, n) writesb((PCI_IO_ADDR)_IO_BASE+(p),(b),(n)) 558b8b572e1SStephen Rothwell #define __do_outsw(p, b, n) writesw((PCI_IO_ADDR)_IO_BASE+(p),(b),(n)) 559b8b572e1SStephen Rothwell #define __do_outsl(p, b, n) writesl((PCI_IO_ADDR)_IO_BASE+(p),(b),(n)) 560b8b572e1SStephen Rothwell 561b8b572e1SStephen Rothwell #define __do_memset_io(addr, c, n) \ 562b8b572e1SStephen Rothwell _memset_io(PCI_FIX_ADDR(addr), c, n) 563b8b572e1SStephen Rothwell #define __do_memcpy_toio(dst, src, n) \ 564b8b572e1SStephen Rothwell _memcpy_toio(PCI_FIX_ADDR(dst), src, n) 565b8b572e1SStephen Rothwell 566b8b572e1SStephen Rothwell #ifdef CONFIG_EEH 567b8b572e1SStephen Rothwell #define __do_memcpy_fromio(dst, src, n) \ 568b8b572e1SStephen Rothwell eeh_memcpy_fromio(dst, PCI_FIX_ADDR(src), n) 569b8b572e1SStephen Rothwell #else /* CONFIG_EEH */ 570b8b572e1SStephen Rothwell #define __do_memcpy_fromio(dst, src, n) \ 571b8b572e1SStephen Rothwell _memcpy_fromio(dst,PCI_FIX_ADDR(src),n) 572b8b572e1SStephen Rothwell #endif /* !CONFIG_EEH */ 573b8b572e1SStephen Rothwell 57421176fedSMichael Ellerman #ifdef CONFIG_PPC_INDIRECT_PIO 57521176fedSMichael Ellerman #define DEF_PCI_HOOK_pio(x) x 576b8b572e1SStephen Rothwell #else 57721176fedSMichael Ellerman #define DEF_PCI_HOOK_pio(x) NULL 57821176fedSMichael Ellerman #endif 57921176fedSMichael Ellerman 58021176fedSMichael Ellerman #ifdef CONFIG_PPC_INDIRECT_MMIO 58121176fedSMichael Ellerman #define DEF_PCI_HOOK_mem(x) x 58221176fedSMichael Ellerman #else 58321176fedSMichael Ellerman #define DEF_PCI_HOOK_mem(x) NULL 584b8b572e1SStephen Rothwell #endif 585b8b572e1SStephen Rothwell 586b8b572e1SStephen Rothwell /* Structure containing all the hooks */ 587b8b572e1SStephen Rothwell extern struct ppc_pci_io { 588b8b572e1SStephen Rothwell 589b8b572e1SStephen Rothwell #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) ret (*name) at; 590b8b572e1SStephen Rothwell #define DEF_PCI_AC_NORET(name, at, al, space, aa) void (*name) at; 591b8b572e1SStephen Rothwell 592b8b572e1SStephen Rothwell #include <asm/io-defs.h> 593b8b572e1SStephen Rothwell 594b8b572e1SStephen Rothwell #undef DEF_PCI_AC_RET 595b8b572e1SStephen Rothwell #undef DEF_PCI_AC_NORET 596b8b572e1SStephen Rothwell 597b8b572e1SStephen Rothwell } ppc_pci_io; 598b8b572e1SStephen Rothwell 599b8b572e1SStephen Rothwell /* The inline wrappers */ 600b8b572e1SStephen Rothwell #define DEF_PCI_AC_RET(name, ret, at, al, space, aa) \ 601b8b572e1SStephen Rothwell static inline ret name at \ 602b8b572e1SStephen Rothwell { \ 60321176fedSMichael Ellerman if (DEF_PCI_HOOK_##space(ppc_pci_io.name) != NULL) \ 604b8b572e1SStephen Rothwell return ppc_pci_io.name al; \ 605b8b572e1SStephen Rothwell return __do_##name al; \ 606b8b572e1SStephen Rothwell } 607b8b572e1SStephen Rothwell 608b8b572e1SStephen Rothwell #define DEF_PCI_AC_NORET(name, at, al, space, aa) \ 609b8b572e1SStephen Rothwell static inline void name at \ 610b8b572e1SStephen Rothwell { \ 61121176fedSMichael Ellerman if (DEF_PCI_HOOK_##space(ppc_pci_io.name) != NULL) \ 612b8b572e1SStephen Rothwell ppc_pci_io.name al; \ 613b8b572e1SStephen Rothwell else \ 614b8b572e1SStephen Rothwell __do_##name al; \ 615b8b572e1SStephen Rothwell } 616b8b572e1SStephen Rothwell 617b8b572e1SStephen Rothwell #include <asm/io-defs.h> 618b8b572e1SStephen Rothwell 619b8b572e1SStephen Rothwell #undef DEF_PCI_AC_RET 620b8b572e1SStephen Rothwell #undef DEF_PCI_AC_NORET 621b8b572e1SStephen Rothwell 622b8b572e1SStephen Rothwell /* Some drivers check for the presence of readq & writeq with 623b8b572e1SStephen Rothwell * a #ifdef, so we make them happy here. 624b8b572e1SStephen Rothwell */ 625b8b572e1SStephen Rothwell #ifdef __powerpc64__ 626b8b572e1SStephen Rothwell #define readq readq 627b8b572e1SStephen Rothwell #define writeq writeq 628b8b572e1SStephen Rothwell #endif 629b8b572e1SStephen Rothwell 630b8b572e1SStephen Rothwell /* 631b8b572e1SStephen Rothwell * Convert a physical pointer to a virtual kernel pointer for /dev/mem 632b8b572e1SStephen Rothwell * access 633b8b572e1SStephen Rothwell */ 634b8b572e1SStephen Rothwell #define xlate_dev_mem_ptr(p) __va(p) 635b8b572e1SStephen Rothwell 636b8b572e1SStephen Rothwell /* 637b8b572e1SStephen Rothwell * Convert a virtual cached pointer to an uncached pointer 638b8b572e1SStephen Rothwell */ 639b8b572e1SStephen Rothwell #define xlate_dev_kmem_ptr(p) p 640b8b572e1SStephen Rothwell 641b8b572e1SStephen Rothwell /* 642b8b572e1SStephen Rothwell * We don't do relaxed operations yet, at least not with this semantic 643b8b572e1SStephen Rothwell */ 644b8b572e1SStephen Rothwell #define readb_relaxed(addr) readb(addr) 645b8b572e1SStephen Rothwell #define readw_relaxed(addr) readw(addr) 646b8b572e1SStephen Rothwell #define readl_relaxed(addr) readl(addr) 647b8b572e1SStephen Rothwell #define readq_relaxed(addr) readq(addr) 6485da59057SWill Deacon #define writeb_relaxed(v, addr) writeb(v, addr) 6495da59057SWill Deacon #define writew_relaxed(v, addr) writew(v, addr) 6505da59057SWill Deacon #define writel_relaxed(v, addr) writel(v, addr) 6515da59057SWill Deacon #define writeq_relaxed(v, addr) writeq(v, addr) 652b8b572e1SStephen Rothwell 653ef237039SLogan Gunthorpe #include <asm-generic/iomap.h> 654ef237039SLogan Gunthorpe 655b8b572e1SStephen Rothwell #ifdef CONFIG_PPC32 656b8b572e1SStephen Rothwell #define mmiowb() 657b8b572e1SStephen Rothwell #else 658b8b572e1SStephen Rothwell /* 659b8b572e1SStephen Rothwell * Enforce synchronisation of stores vs. spin_unlock 660b8b572e1SStephen Rothwell * (this does it explicitly, though our implementation of spin_unlock 661b8b572e1SStephen Rothwell * does it implicitely too) 662b8b572e1SStephen Rothwell */ 663b8b572e1SStephen Rothwell static inline void mmiowb(void) 664b8b572e1SStephen Rothwell { 665b8b572e1SStephen Rothwell unsigned long tmp; 666b8b572e1SStephen Rothwell 667b8b572e1SStephen Rothwell __asm__ __volatile__("sync; li %0,0; stb %0,%1(13)" 668b8b572e1SStephen Rothwell : "=&r" (tmp) : "i" (offsetof(struct paca_struct, io_sync)) 669b8b572e1SStephen Rothwell : "memory"); 670b8b572e1SStephen Rothwell } 671b8b572e1SStephen Rothwell #endif /* !CONFIG_PPC32 */ 672b8b572e1SStephen Rothwell 673b8b572e1SStephen Rothwell static inline void iosync(void) 674b8b572e1SStephen Rothwell { 675b8b572e1SStephen Rothwell __asm__ __volatile__ ("sync" : : : "memory"); 676b8b572e1SStephen Rothwell } 677b8b572e1SStephen Rothwell 678b8b572e1SStephen Rothwell /* Enforce in-order execution of data I/O. 679b8b572e1SStephen Rothwell * No distinction between read/write on PPC; use eieio for all three. 680b8b572e1SStephen Rothwell * Those are fairly week though. They don't provide a barrier between 681b8b572e1SStephen Rothwell * MMIO and cacheable storage nor do they provide a barrier vs. locks, 682b8b572e1SStephen Rothwell * they only provide barriers between 2 __raw MMIO operations and 683b8b572e1SStephen Rothwell * possibly break write combining. 684b8b572e1SStephen Rothwell */ 685b8b572e1SStephen Rothwell #define iobarrier_rw() eieio() 686b8b572e1SStephen Rothwell #define iobarrier_r() eieio() 687b8b572e1SStephen Rothwell #define iobarrier_w() eieio() 688b8b572e1SStephen Rothwell 689b8b572e1SStephen Rothwell 690b8b572e1SStephen Rothwell /* 691b8b572e1SStephen Rothwell * output pause versions need a delay at least for the 692b8b572e1SStephen Rothwell * w83c105 ide controller in a p610. 693b8b572e1SStephen Rothwell */ 694b8b572e1SStephen Rothwell #define inb_p(port) inb(port) 695b8b572e1SStephen Rothwell #define outb_p(val, port) (udelay(1), outb((val), (port))) 696b8b572e1SStephen Rothwell #define inw_p(port) inw(port) 697b8b572e1SStephen Rothwell #define outw_p(val, port) (udelay(1), outw((val), (port))) 698b8b572e1SStephen Rothwell #define inl_p(port) inl(port) 699b8b572e1SStephen Rothwell #define outl_p(val, port) (udelay(1), outl((val), (port))) 700b8b572e1SStephen Rothwell 701b8b572e1SStephen Rothwell 702b8b572e1SStephen Rothwell #define IO_SPACE_LIMIT ~(0UL) 703b8b572e1SStephen Rothwell 704b8b572e1SStephen Rothwell 705b8b572e1SStephen Rothwell /** 706b8b572e1SStephen Rothwell * ioremap - map bus memory into CPU space 707b8b572e1SStephen Rothwell * @address: bus address of the memory 708b8b572e1SStephen Rothwell * @size: size of the resource to map 709b8b572e1SStephen Rothwell * 710b8b572e1SStephen Rothwell * ioremap performs a platform specific sequence of operations to 711b8b572e1SStephen Rothwell * make bus memory CPU accessible via the readb/readw/readl/writeb/ 712b8b572e1SStephen Rothwell * writew/writel functions and the other mmio helpers. The returned 713b8b572e1SStephen Rothwell * address is not guaranteed to be usable directly as a virtual 714b8b572e1SStephen Rothwell * address. 715b8b572e1SStephen Rothwell * 716b8b572e1SStephen Rothwell * We provide a few variations of it: 717b8b572e1SStephen Rothwell * 718b8b572e1SStephen Rothwell * * ioremap is the standard one and provides non-cacheable guarded mappings 719b8b572e1SStephen Rothwell * and can be hooked by the platform via ppc_md 720b8b572e1SStephen Rothwell * 72140f1ce7fSAnton Blanchard * * ioremap_prot allows to specify the page flags as an argument and can 72240f1ce7fSAnton Blanchard * also be hooked by the platform via ppc_md. 723b8b572e1SStephen Rothwell * 724b8b572e1SStephen Rothwell * * ioremap_nocache is identical to ioremap 725b8b572e1SStephen Rothwell * 726be135f40SAnton Blanchard * * ioremap_wc enables write combining 727be135f40SAnton Blanchard * 72886c391bdSChristophe Leroy * * ioremap_wt enables write through 72986c391bdSChristophe Leroy * 73086c391bdSChristophe Leroy * * ioremap_coherent maps coherent cached memory 73186c391bdSChristophe Leroy * 732b8b572e1SStephen Rothwell * * iounmap undoes such a mapping and can be hooked 733b8b572e1SStephen Rothwell * 734b8b572e1SStephen Rothwell * * __ioremap_at (and the pending __iounmap_at) are low level functions to 735b8b572e1SStephen Rothwell * create hand-made mappings for use only by the PCI code and cannot 736b8b572e1SStephen Rothwell * currently be hooked. Must be page aligned. 737b8b572e1SStephen Rothwell * 738b8b572e1SStephen Rothwell * * __ioremap is the low level implementation used by ioremap and 73940f1ce7fSAnton Blanchard * ioremap_prot and cannot be hooked (but can be used by a hook on one 740b8b572e1SStephen Rothwell * of the previous ones) 741b8b572e1SStephen Rothwell * 7421cdab55dSBenjamin Herrenschmidt * * __ioremap_caller is the same as above but takes an explicit caller 7431cdab55dSBenjamin Herrenschmidt * reference rather than using __builtin_return_address(0) 7441cdab55dSBenjamin Herrenschmidt * 745b8b572e1SStephen Rothwell * * __iounmap, is the low level implementation used by iounmap and cannot 746b8b572e1SStephen Rothwell * be hooked (but can be used by a hook on iounmap) 747b8b572e1SStephen Rothwell * 748b8b572e1SStephen Rothwell */ 749b8b572e1SStephen Rothwell extern void __iomem *ioremap(phys_addr_t address, unsigned long size); 75040f1ce7fSAnton Blanchard extern void __iomem *ioremap_prot(phys_addr_t address, unsigned long size, 751b8b572e1SStephen Rothwell unsigned long flags); 752be135f40SAnton Blanchard extern void __iomem *ioremap_wc(phys_addr_t address, unsigned long size); 75386c391bdSChristophe Leroy void __iomem *ioremap_wt(phys_addr_t address, unsigned long size); 75486c391bdSChristophe Leroy void __iomem *ioremap_coherent(phys_addr_t address, unsigned long size); 755b8b572e1SStephen Rothwell #define ioremap_nocache(addr, size) ioremap((addr), (size)) 7564c73e892SLuis R. Rodriguez #define ioremap_uc(addr, size) ioremap((addr), (size)) 757f855b2f5SOliver O'Halloran #define ioremap_cache(addr, size) \ 758f855b2f5SOliver O'Halloran ioremap_prot((addr), (size), pgprot_val(PAGE_KERNEL)) 759b8b572e1SStephen Rothwell 760b8b572e1SStephen Rothwell extern void iounmap(volatile void __iomem *addr); 761b8b572e1SStephen Rothwell 762b8b572e1SStephen Rothwell extern void __iomem *__ioremap(phys_addr_t, unsigned long size, 763b8b572e1SStephen Rothwell unsigned long flags); 7641cdab55dSBenjamin Herrenschmidt extern void __iomem *__ioremap_caller(phys_addr_t, unsigned long size, 765c766ee72SChristophe Leroy pgprot_t prot, void *caller); 7661cdab55dSBenjamin Herrenschmidt 767b8b572e1SStephen Rothwell extern void __iounmap(volatile void __iomem *addr); 768b8b572e1SStephen Rothwell 769b8b572e1SStephen Rothwell extern void __iomem * __ioremap_at(phys_addr_t pa, void *ea, 770c766ee72SChristophe Leroy unsigned long size, pgprot_t prot); 771b8b572e1SStephen Rothwell extern void __iounmap_at(void *ea, unsigned long size); 772b8b572e1SStephen Rothwell 773b8b572e1SStephen Rothwell /* 774ecd73cc5SBenjamin Herrenschmidt * When CONFIG_PPC_INDIRECT_PIO is set, we use the generic iomap implementation 775b8b572e1SStephen Rothwell * which needs some additional definitions here. They basically allow PIO 776b8b572e1SStephen Rothwell * space overall to be 1GB. This will work as long as we never try to use 777b8b572e1SStephen Rothwell * iomap to map MMIO below 1GB which should be fine on ppc64 778b8b572e1SStephen Rothwell */ 779b8b572e1SStephen Rothwell #define HAVE_ARCH_PIO_SIZE 1 780b8b572e1SStephen Rothwell #define PIO_OFFSET 0x00000000UL 781b8b572e1SStephen Rothwell #define PIO_MASK (FULL_IO_SIZE - 1) 782b8b572e1SStephen Rothwell #define PIO_RESERVED (FULL_IO_SIZE) 783b8b572e1SStephen Rothwell 784b8b572e1SStephen Rothwell #define mmio_read16be(addr) readw_be(addr) 785b8b572e1SStephen Rothwell #define mmio_read32be(addr) readl_be(addr) 786b8b572e1SStephen Rothwell #define mmio_write16be(val, addr) writew_be(val, addr) 787b8b572e1SStephen Rothwell #define mmio_write32be(val, addr) writel_be(val, addr) 788b8b572e1SStephen Rothwell #define mmio_insb(addr, dst, count) readsb(addr, dst, count) 789b8b572e1SStephen Rothwell #define mmio_insw(addr, dst, count) readsw(addr, dst, count) 790b8b572e1SStephen Rothwell #define mmio_insl(addr, dst, count) readsl(addr, dst, count) 791b8b572e1SStephen Rothwell #define mmio_outsb(addr, src, count) writesb(addr, src, count) 792b8b572e1SStephen Rothwell #define mmio_outsw(addr, src, count) writesw(addr, src, count) 793b8b572e1SStephen Rothwell #define mmio_outsl(addr, src, count) writesl(addr, src, count) 794b8b572e1SStephen Rothwell 795b8b572e1SStephen Rothwell /** 796b8b572e1SStephen Rothwell * virt_to_phys - map virtual addresses to physical 797b8b572e1SStephen Rothwell * @address: address to remap 798b8b572e1SStephen Rothwell * 799b8b572e1SStephen Rothwell * The returned physical address is the physical (CPU) mapping for 800b8b572e1SStephen Rothwell * the memory address given. It is only valid to use this function on 801b8b572e1SStephen Rothwell * addresses directly mapped or allocated via kmalloc. 802b8b572e1SStephen Rothwell * 803b8b572e1SStephen Rothwell * This function does not give bus mappings for DMA transfers. In 804b8b572e1SStephen Rothwell * almost all conceivable cases a device driver should not be using 805b8b572e1SStephen Rothwell * this function 806b8b572e1SStephen Rothwell */ 807b8b572e1SStephen Rothwell static inline unsigned long virt_to_phys(volatile void * address) 808b8b572e1SStephen Rothwell { 809*6bf752daSChristophe Leroy WARN_ON(IS_ENABLED(CONFIG_DEBUG_VIRTUAL) && !virt_addr_valid(address)); 810*6bf752daSChristophe Leroy 811b8b572e1SStephen Rothwell return __pa((unsigned long)address); 812b8b572e1SStephen Rothwell } 813b8b572e1SStephen Rothwell 814b8b572e1SStephen Rothwell /** 815b8b572e1SStephen Rothwell * phys_to_virt - map physical address to virtual 816b8b572e1SStephen Rothwell * @address: address to remap 817b8b572e1SStephen Rothwell * 818b8b572e1SStephen Rothwell * The returned virtual address is a current CPU mapping for 819b8b572e1SStephen Rothwell * the memory address given. It is only valid to use this function on 820b8b572e1SStephen Rothwell * addresses that have a kernel mapping 821b8b572e1SStephen Rothwell * 822b8b572e1SStephen Rothwell * This function does not handle bus mappings for DMA transfers. In 823b8b572e1SStephen Rothwell * almost all conceivable cases a device driver should not be using 824b8b572e1SStephen Rothwell * this function 825b8b572e1SStephen Rothwell */ 826b8b572e1SStephen Rothwell static inline void * phys_to_virt(unsigned long address) 827b8b572e1SStephen Rothwell { 828b8b572e1SStephen Rothwell return (void *)__va(address); 829b8b572e1SStephen Rothwell } 830b8b572e1SStephen Rothwell 831b8b572e1SStephen Rothwell /* 832b8b572e1SStephen Rothwell * Change "struct page" to physical address. 833b8b572e1SStephen Rothwell */ 834*6bf752daSChristophe Leroy static inline phys_addr_t page_to_phys(struct page *page) 835*6bf752daSChristophe Leroy { 836*6bf752daSChristophe Leroy unsigned long pfn = page_to_pfn(page); 837*6bf752daSChristophe Leroy 838*6bf752daSChristophe Leroy WARN_ON(IS_ENABLED(CONFIG_DEBUG_VIRTUAL) && !pfn_valid(pfn)); 839*6bf752daSChristophe Leroy 840*6bf752daSChristophe Leroy return PFN_PHYS(pfn); 841*6bf752daSChristophe Leroy } 842b8b572e1SStephen Rothwell 843b8b572e1SStephen Rothwell /* 844b8b572e1SStephen Rothwell * 32 bits still uses virt_to_bus() for it's implementation of DMA 845b8b572e1SStephen Rothwell * mappings se we have to keep it defined here. We also have some old 846b8b572e1SStephen Rothwell * drivers (shame shame shame) that use bus_to_virt() and haven't been 847b8b572e1SStephen Rothwell * fixed yet so I need to define it here. 848b8b572e1SStephen Rothwell */ 849b8b572e1SStephen Rothwell #ifdef CONFIG_PPC32 850b8b572e1SStephen Rothwell 851b8b572e1SStephen Rothwell static inline unsigned long virt_to_bus(volatile void * address) 852b8b572e1SStephen Rothwell { 853b8b572e1SStephen Rothwell if (address == NULL) 854b8b572e1SStephen Rothwell return 0; 855b8b572e1SStephen Rothwell return __pa(address) + PCI_DRAM_OFFSET; 856b8b572e1SStephen Rothwell } 857b8b572e1SStephen Rothwell 858b8b572e1SStephen Rothwell static inline void * bus_to_virt(unsigned long address) 859b8b572e1SStephen Rothwell { 860b8b572e1SStephen Rothwell if (address == 0) 861b8b572e1SStephen Rothwell return NULL; 862b8b572e1SStephen Rothwell return __va(address - PCI_DRAM_OFFSET); 863b8b572e1SStephen Rothwell } 864b8b572e1SStephen Rothwell 865b8b572e1SStephen Rothwell #define page_to_bus(page) (page_to_phys(page) + PCI_DRAM_OFFSET) 866b8b572e1SStephen Rothwell 867b8b572e1SStephen Rothwell #endif /* CONFIG_PPC32 */ 868b8b572e1SStephen Rothwell 869b8b572e1SStephen Rothwell /* access ports */ 870b8b572e1SStephen Rothwell #define setbits32(_addr, _v) out_be32((_addr), in_be32(_addr) | (_v)) 871b8b572e1SStephen Rothwell #define clrbits32(_addr, _v) out_be32((_addr), in_be32(_addr) & ~(_v)) 872b8b572e1SStephen Rothwell 873b8b572e1SStephen Rothwell #define setbits16(_addr, _v) out_be16((_addr), in_be16(_addr) | (_v)) 874b8b572e1SStephen Rothwell #define clrbits16(_addr, _v) out_be16((_addr), in_be16(_addr) & ~(_v)) 875b8b572e1SStephen Rothwell 876b8b572e1SStephen Rothwell #define setbits8(_addr, _v) out_8((_addr), in_8(_addr) | (_v)) 877b8b572e1SStephen Rothwell #define clrbits8(_addr, _v) out_8((_addr), in_8(_addr) & ~(_v)) 878b8b572e1SStephen Rothwell 879b8b572e1SStephen Rothwell /* Clear and set bits in one shot. These macros can be used to clear and 880b8b572e1SStephen Rothwell * set multiple bits in a register using a single read-modify-write. These 881b8b572e1SStephen Rothwell * macros can also be used to set a multiple-bit bit pattern using a mask, 882b8b572e1SStephen Rothwell * by specifying the mask in the 'clear' parameter and the new bit pattern 883b8b572e1SStephen Rothwell * in the 'set' parameter. 884b8b572e1SStephen Rothwell */ 885b8b572e1SStephen Rothwell 886b8b572e1SStephen Rothwell #define clrsetbits(type, addr, clear, set) \ 887b8b572e1SStephen Rothwell out_##type((addr), (in_##type(addr) & ~(clear)) | (set)) 888b8b572e1SStephen Rothwell 889b8b572e1SStephen Rothwell #ifdef __powerpc64__ 890b8b572e1SStephen Rothwell #define clrsetbits_be64(addr, clear, set) clrsetbits(be64, addr, clear, set) 891b8b572e1SStephen Rothwell #define clrsetbits_le64(addr, clear, set) clrsetbits(le64, addr, clear, set) 892b8b572e1SStephen Rothwell #endif 893b8b572e1SStephen Rothwell 894b8b572e1SStephen Rothwell #define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set) 895b8b572e1SStephen Rothwell #define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set) 896b8b572e1SStephen Rothwell 897b8b572e1SStephen Rothwell #define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set) 898b8b572e1SStephen Rothwell #define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set) 899b8b572e1SStephen Rothwell 900b8b572e1SStephen Rothwell #define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set) 901b8b572e1SStephen Rothwell 902b8b572e1SStephen Rothwell #endif /* __KERNEL__ */ 903b8b572e1SStephen Rothwell 904b8b572e1SStephen Rothwell #endif /* _ASM_POWERPC_IO_H */ 905