1 // SPDX-License-Identifier: GPL-2.0-only
3 * Based on arch/arm/kernel/io.c
5 * Copyright (C) 2012 ARM Ltd.
8 #include <linux/export.h>
9 #include <linux/types.h>
13 * Copy data from IO memory space to "real" memory space.
15 void __memcpy_fromio(void *to, const volatile void __iomem *from, size_t count)
17 while (count && !IS_ALIGNED((unsigned long)from, 8)) {
18 *(u8 *)to = __raw_readb(from);
25 *(u64 *)to = __raw_readq(from);
32 *(u8 *)to = __raw_readb(from);
38 EXPORT_SYMBOL(__memcpy_fromio);
41 * This generates a memcpy that works on a from/to address which is aligned to
42 * bits. Count is in terms of the number of bits sized quantities to copy. It
43 * optimizes to use the STR groupings when possible so that it is WC friendly.
45 #define memcpy_toio_aligned(to, from, count, bits) \
47 volatile u##bits __iomem *_to = to; \
48 const u##bits *_from = from; \
49 size_t _count = count; \
50 const u##bits *_end_from = _from + ALIGN_DOWN(_count, 8); \
52 for (; _from < _end_from; _from += 8, _to += 8) \
53 __const_memcpy_toio_aligned##bits(_to, _from, 8); \
54 if ((_count % 8) >= 4) { \
55 __const_memcpy_toio_aligned##bits(_to, _from, 4); \
59 if ((_count % 4) >= 2) { \
60 __const_memcpy_toio_aligned##bits(_to, _from, 2); \
65 __const_memcpy_toio_aligned##bits(_to, _from, 1); \
68 void __iowrite64_copy_full(void __iomem *to, const void *from, size_t count)
70 memcpy_toio_aligned(to, from, count, 64);
73 EXPORT_SYMBOL(__iowrite64_copy_full);
75 void __iowrite32_copy_full(void __iomem *to, const void *from, size_t count)
77 memcpy_toio_aligned(to, from, count, 32);
80 EXPORT_SYMBOL(__iowrite32_copy_full);
83 * Copy data from "real" memory space to IO memory space.
85 void __memcpy_toio(volatile void __iomem *to, const void *from, size_t count)
87 while (count && !IS_ALIGNED((unsigned long)to, 8)) {
88 __raw_writeb(*(u8 *)from, to);
95 __raw_writeq(*(u64 *)from, to);
102 __raw_writeb(*(u8 *)from, to);
108 EXPORT_SYMBOL(__memcpy_toio);
111 * "memset" on IO memory space.
113 void __memset_io(volatile void __iomem *dst, int c, size_t count)
121 while (count && !IS_ALIGNED((unsigned long)dst, 8)) {
122 __raw_writeb(c, dst);
128 __raw_writeq(qc, dst);
134 __raw_writeb(c, dst);
139 EXPORT_SYMBOL(__memset_io);