1*d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */ 21162b070SVineet Gupta /* 31162b070SVineet Gupta * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com) 41162b070SVineet Gupta */ 51162b070SVineet Gupta 61162b070SVineet Gupta #ifndef _ASM_ARC_IO_H 71162b070SVineet Gupta #define _ASM_ARC_IO_H 81162b070SVineet Gupta 91162b070SVineet Gupta #include <linux/types.h> 101162b070SVineet Gupta #include <asm/byteorder.h> 111162b070SVineet Gupta #include <asm/page.h> 1210d44343SJose Abreu #include <asm/unaligned.h> 131162b070SVineet Gupta 14e5bc0478SVineet Gupta #ifdef CONFIG_ISA_ARCV2 15e5bc0478SVineet Gupta #include <asm/barrier.h> 16e5bc0478SVineet Gupta #define __iormb() rmb() 17e5bc0478SVineet Gupta #define __iowmb() wmb() 18e5bc0478SVineet Gupta #else 19e5bc0478SVineet Gupta #define __iormb() do { } while (0) 20e5bc0478SVineet Gupta #define __iowmb() do { } while (0) 21e5bc0478SVineet Gupta #endif 22e5bc0478SVineet Gupta 23f5db19e9SVineet Gupta extern void __iomem *ioremap(phys_addr_t paddr, unsigned long size); 24f5db19e9SVineet Gupta extern void __iomem *ioremap_prot(phys_addr_t paddr, unsigned long size, 254368902bSGilad Ben-Yossef unsigned long flags); 26c1678ffcSJoao Pinto static inline void __iomem *ioport_map(unsigned long port, unsigned int nr) 27c1678ffcSJoao Pinto { 28c1678ffcSJoao Pinto return (void __iomem *)port; 29c1678ffcSJoao Pinto } 30c1678ffcSJoao Pinto 31c1678ffcSJoao Pinto static inline void ioport_unmap(void __iomem *addr) 32c1678ffcSJoao Pinto { 33c1678ffcSJoao Pinto } 34c1678ffcSJoao Pinto 351162b070SVineet Gupta extern void iounmap(const void __iomem *addr); 361162b070SVineet Gupta 371162b070SVineet Gupta #define ioremap_nocache(phy, sz) ioremap(phy, sz) 381162b070SVineet Gupta #define ioremap_wc(phy, sz) ioremap(phy, sz) 39556269c1SToshi Kani #define ioremap_wt(phy, sz) ioremap(phy, sz) 401162b070SVineet Gupta 41e5bc0478SVineet Gupta /* 42e5bc0478SVineet Gupta * io{read,write}{16,32}be() macros 43e5bc0478SVineet Gupta */ 44e5bc0478SVineet Gupta #define ioread16be(p) ({ u16 __v = be16_to_cpu((__force __be16)__raw_readw(p)); __iormb(); __v; }) 45e5bc0478SVineet Gupta #define ioread32be(p) ({ u32 __v = be32_to_cpu((__force __be32)__raw_readl(p)); __iormb(); __v; }) 46e5bc0478SVineet Gupta 47e5bc0478SVineet Gupta #define iowrite16be(v,p) ({ __iowmb(); __raw_writew((__force u16)cpu_to_be16(v), p); }) 48e5bc0478SVineet Gupta #define iowrite32be(v,p) ({ __iowmb(); __raw_writel((__force u32)cpu_to_be32(v), p); }) 49e5bc0478SVineet Gupta 501162b070SVineet Gupta /* Change struct page to physical address */ 511162b070SVineet Gupta #define page_to_phys(page) (page_to_pfn(page) << PAGE_SHIFT) 521162b070SVineet Gupta 531162b070SVineet Gupta #define __raw_readb __raw_readb 541162b070SVineet Gupta static inline u8 __raw_readb(const volatile void __iomem *addr) 551162b070SVineet Gupta { 561162b070SVineet Gupta u8 b; 571162b070SVineet Gupta 581162b070SVineet Gupta __asm__ __volatile__( 591162b070SVineet Gupta " ldb%U1 %0, %1 \n" 601162b070SVineet Gupta : "=r" (b) 611162b070SVineet Gupta : "m" (*(volatile u8 __force *)addr) 621162b070SVineet Gupta : "memory"); 631162b070SVineet Gupta 641162b070SVineet Gupta return b; 651162b070SVineet Gupta } 661162b070SVineet Gupta 671162b070SVineet Gupta #define __raw_readw __raw_readw 681162b070SVineet Gupta static inline u16 __raw_readw(const volatile void __iomem *addr) 691162b070SVineet Gupta { 701162b070SVineet Gupta u16 s; 711162b070SVineet Gupta 721162b070SVineet Gupta __asm__ __volatile__( 731162b070SVineet Gupta " ldw%U1 %0, %1 \n" 741162b070SVineet Gupta : "=r" (s) 751162b070SVineet Gupta : "m" (*(volatile u16 __force *)addr) 761162b070SVineet Gupta : "memory"); 771162b070SVineet Gupta 781162b070SVineet Gupta return s; 791162b070SVineet Gupta } 801162b070SVineet Gupta 811162b070SVineet Gupta #define __raw_readl __raw_readl 821162b070SVineet Gupta static inline u32 __raw_readl(const volatile void __iomem *addr) 831162b070SVineet Gupta { 841162b070SVineet Gupta u32 w; 851162b070SVineet Gupta 861162b070SVineet Gupta __asm__ __volatile__( 871162b070SVineet Gupta " ld%U1 %0, %1 \n" 881162b070SVineet Gupta : "=r" (w) 891162b070SVineet Gupta : "m" (*(volatile u32 __force *)addr) 901162b070SVineet Gupta : "memory"); 911162b070SVineet Gupta 921162b070SVineet Gupta return w; 931162b070SVineet Gupta } 941162b070SVineet Gupta 9510d44343SJose Abreu /* 9610d44343SJose Abreu * {read,write}s{b,w,l}() repeatedly access the same IO address in 9710d44343SJose Abreu * native endianness in 8-, 16-, 32-bit chunks {into,from} memory, 9810d44343SJose Abreu * @count times 9910d44343SJose Abreu */ 10010d44343SJose Abreu #define __raw_readsx(t,f) \ 10110d44343SJose Abreu static inline void __raw_reads##f(const volatile void __iomem *addr, \ 10210d44343SJose Abreu void *ptr, unsigned int count) \ 10310d44343SJose Abreu { \ 10410d44343SJose Abreu bool is_aligned = ((unsigned long)ptr % ((t) / 8)) == 0; \ 10510d44343SJose Abreu u##t *buf = ptr; \ 10610d44343SJose Abreu \ 10710d44343SJose Abreu if (!count) \ 10810d44343SJose Abreu return; \ 10910d44343SJose Abreu \ 11010d44343SJose Abreu /* Some ARC CPU's don't support unaligned accesses */ \ 11110d44343SJose Abreu if (is_aligned) { \ 11210d44343SJose Abreu do { \ 11310d44343SJose Abreu u##t x = __raw_read##f(addr); \ 11410d44343SJose Abreu *buf++ = x; \ 11510d44343SJose Abreu } while (--count); \ 11610d44343SJose Abreu } else { \ 11710d44343SJose Abreu do { \ 11810d44343SJose Abreu u##t x = __raw_read##f(addr); \ 11910d44343SJose Abreu put_unaligned(x, buf++); \ 12010d44343SJose Abreu } while (--count); \ 12110d44343SJose Abreu } \ 12210d44343SJose Abreu } 12310d44343SJose Abreu 12410d44343SJose Abreu #define __raw_readsb __raw_readsb 12510d44343SJose Abreu __raw_readsx(8, b) 12610d44343SJose Abreu #define __raw_readsw __raw_readsw 12710d44343SJose Abreu __raw_readsx(16, w) 12810d44343SJose Abreu #define __raw_readsl __raw_readsl 12910d44343SJose Abreu __raw_readsx(32, l) 13010d44343SJose Abreu 1311162b070SVineet Gupta #define __raw_writeb __raw_writeb 1321162b070SVineet Gupta static inline void __raw_writeb(u8 b, volatile void __iomem *addr) 1331162b070SVineet Gupta { 1341162b070SVineet Gupta __asm__ __volatile__( 1351162b070SVineet Gupta " stb%U1 %0, %1 \n" 1361162b070SVineet Gupta : 1371162b070SVineet Gupta : "r" (b), "m" (*(volatile u8 __force *)addr) 1381162b070SVineet Gupta : "memory"); 1391162b070SVineet Gupta } 1401162b070SVineet Gupta 1411162b070SVineet Gupta #define __raw_writew __raw_writew 1421162b070SVineet Gupta static inline void __raw_writew(u16 s, volatile void __iomem *addr) 1431162b070SVineet Gupta { 1441162b070SVineet Gupta __asm__ __volatile__( 1451162b070SVineet Gupta " stw%U1 %0, %1 \n" 1461162b070SVineet Gupta : 1471162b070SVineet Gupta : "r" (s), "m" (*(volatile u16 __force *)addr) 1481162b070SVineet Gupta : "memory"); 1491162b070SVineet Gupta 1501162b070SVineet Gupta } 1511162b070SVineet Gupta 1521162b070SVineet Gupta #define __raw_writel __raw_writel 1531162b070SVineet Gupta static inline void __raw_writel(u32 w, volatile void __iomem *addr) 1541162b070SVineet Gupta { 1551162b070SVineet Gupta __asm__ __volatile__( 1561162b070SVineet Gupta " st%U1 %0, %1 \n" 1571162b070SVineet Gupta : 1581162b070SVineet Gupta : "r" (w), "m" (*(volatile u32 __force *)addr) 1591162b070SVineet Gupta : "memory"); 1601162b070SVineet Gupta 1611162b070SVineet Gupta } 1621162b070SVineet Gupta 16310d44343SJose Abreu #define __raw_writesx(t,f) \ 16410d44343SJose Abreu static inline void __raw_writes##f(volatile void __iomem *addr, \ 16510d44343SJose Abreu const void *ptr, unsigned int count) \ 16610d44343SJose Abreu { \ 16710d44343SJose Abreu bool is_aligned = ((unsigned long)ptr % ((t) / 8)) == 0; \ 16810d44343SJose Abreu const u##t *buf = ptr; \ 16910d44343SJose Abreu \ 17010d44343SJose Abreu if (!count) \ 17110d44343SJose Abreu return; \ 17210d44343SJose Abreu \ 17310d44343SJose Abreu /* Some ARC CPU's don't support unaligned accesses */ \ 17410d44343SJose Abreu if (is_aligned) { \ 17510d44343SJose Abreu do { \ 17610d44343SJose Abreu __raw_write##f(*buf++, addr); \ 17710d44343SJose Abreu } while (--count); \ 17810d44343SJose Abreu } else { \ 17910d44343SJose Abreu do { \ 18010d44343SJose Abreu __raw_write##f(get_unaligned(buf++), addr); \ 18110d44343SJose Abreu } while (--count); \ 18210d44343SJose Abreu } \ 18310d44343SJose Abreu } 18410d44343SJose Abreu 18510d44343SJose Abreu #define __raw_writesb __raw_writesb 18610d44343SJose Abreu __raw_writesx(8, b) 18710d44343SJose Abreu #define __raw_writesw __raw_writesw 18810d44343SJose Abreu __raw_writesx(16, w) 18910d44343SJose Abreu #define __raw_writesl __raw_writesl 19010d44343SJose Abreu __raw_writesx(32, l) 19110d44343SJose Abreu 192b8a03302SVineet Gupta /* 193b8a03302SVineet Gupta * MMIO can also get buffered/optimized in micro-arch, so barriers needed 194b8a03302SVineet Gupta * Based on ARM model for the typical use case 195b8a03302SVineet Gupta * 196b8a03302SVineet Gupta * <ST [DMA buffer]> 197b8a03302SVineet Gupta * <writel MMIO "go" reg> 198b8a03302SVineet Gupta * or: 199b8a03302SVineet Gupta * <readl MMIO "status" reg> 200b8a03302SVineet Gupta * <LD [DMA buffer]> 201b8a03302SVineet Gupta * 202b8a03302SVineet Gupta * http://lkml.kernel.org/r/20150622133656.GG1583@arm.com 203b8a03302SVineet Gupta */ 204b8a03302SVineet Gupta #define readb(c) ({ u8 __v = readb_relaxed(c); __iormb(); __v; }) 205b8a03302SVineet Gupta #define readw(c) ({ u16 __v = readw_relaxed(c); __iormb(); __v; }) 206b8a03302SVineet Gupta #define readl(c) ({ u32 __v = readl_relaxed(c); __iormb(); __v; }) 20710d44343SJose Abreu #define readsb(p,d,l) ({ __raw_readsb(p,d,l); __iormb(); }) 20810d44343SJose Abreu #define readsw(p,d,l) ({ __raw_readsw(p,d,l); __iormb(); }) 20910d44343SJose Abreu #define readsl(p,d,l) ({ __raw_readsl(p,d,l); __iormb(); }) 210b8a03302SVineet Gupta 211b8a03302SVineet Gupta #define writeb(v,c) ({ __iowmb(); writeb_relaxed(v,c); }) 212b8a03302SVineet Gupta #define writew(v,c) ({ __iowmb(); writew_relaxed(v,c); }) 213b8a03302SVineet Gupta #define writel(v,c) ({ __iowmb(); writel_relaxed(v,c); }) 21410d44343SJose Abreu #define writesb(p,d,l) ({ __iowmb(); __raw_writesb(p,d,l); }) 21510d44343SJose Abreu #define writesw(p,d,l) ({ __iowmb(); __raw_writesw(p,d,l); }) 21610d44343SJose Abreu #define writesl(p,d,l) ({ __iowmb(); __raw_writesl(p,d,l); }) 217b8a03302SVineet Gupta 218b8a03302SVineet Gupta /* 219f778cc65SLada Trimasova * Relaxed API for drivers which can handle barrier ordering themselves 220f778cc65SLada Trimasova * 221f778cc65SLada Trimasova * Also these are defined to perform little endian accesses. 222f778cc65SLada Trimasova * To provide the typical device register semantics of fixed endian, 223f778cc65SLada Trimasova * swap the byte order for Big Endian 224f778cc65SLada Trimasova * 225f778cc65SLada Trimasova * http://lkml.kernel.org/r/201603100845.30602.arnd@arndb.de 226b8a03302SVineet Gupta */ 227b8a03302SVineet Gupta #define readb_relaxed(c) __raw_readb(c) 228f778cc65SLada Trimasova #define readw_relaxed(c) ({ u16 __r = le16_to_cpu((__force __le16) \ 229f778cc65SLada Trimasova __raw_readw(c)); __r; }) 230f778cc65SLada Trimasova #define readl_relaxed(c) ({ u32 __r = le32_to_cpu((__force __le32) \ 231f778cc65SLada Trimasova __raw_readl(c)); __r; }) 232b8a03302SVineet Gupta 233b8a03302SVineet Gupta #define writeb_relaxed(v,c) __raw_writeb(v,c) 234f778cc65SLada Trimasova #define writew_relaxed(v,c) __raw_writew((__force u16) cpu_to_le16(v),c) 235f778cc65SLada Trimasova #define writel_relaxed(v,c) __raw_writel((__force u32) cpu_to_le32(v),c) 2366532b02fSMischa Jonker 2371162b070SVineet Gupta #include <asm-generic/io.h> 2381162b070SVineet Gupta 2391162b070SVineet Gupta #endif /* _ASM_ARC_IO_H */ 240