xref: /openbmc/linux/arch/alpha/kernel/io.c (revision 54505a1e2083fc54cbe8779b97479f969cd30a00)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Alpha IO and memory functions.
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <linux/kernel.h>
71da177e4SLinus Torvalds #include <linux/types.h>
81da177e4SLinus Torvalds #include <linux/string.h>
91da177e4SLinus Torvalds #include <linux/module.h>
101da177e4SLinus Torvalds #include <asm/io.h>
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds /* Out-of-line versions of the i/o routines that redirect into the
131da177e4SLinus Torvalds    platform-specific version.  Note that "platform-specific" may mean
141da177e4SLinus Torvalds    "generic", which bumps through the machine vector.  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds unsigned int
171da177e4SLinus Torvalds ioread8(void __iomem *addr)
181da177e4SLinus Torvalds {
19*54505a1eSMikulas Patocka 	unsigned int ret;
20*54505a1eSMikulas Patocka 	mb();
21*54505a1eSMikulas Patocka 	ret = IO_CONCAT(__IO_PREFIX,ioread8)(addr);
221da177e4SLinus Torvalds 	mb();
231da177e4SLinus Torvalds 	return ret;
241da177e4SLinus Torvalds }
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds unsigned int ioread16(void __iomem *addr)
271da177e4SLinus Torvalds {
28*54505a1eSMikulas Patocka 	unsigned int ret;
29*54505a1eSMikulas Patocka 	mb();
30*54505a1eSMikulas Patocka 	ret = IO_CONCAT(__IO_PREFIX,ioread16)(addr);
311da177e4SLinus Torvalds 	mb();
321da177e4SLinus Torvalds 	return ret;
331da177e4SLinus Torvalds }
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds unsigned int ioread32(void __iomem *addr)
361da177e4SLinus Torvalds {
37*54505a1eSMikulas Patocka 	unsigned int ret;
38*54505a1eSMikulas Patocka 	mb();
39*54505a1eSMikulas Patocka 	ret = IO_CONCAT(__IO_PREFIX,ioread32)(addr);
401da177e4SLinus Torvalds 	mb();
411da177e4SLinus Torvalds 	return ret;
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds void iowrite8(u8 b, void __iomem *addr)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds 	mb();
4792d7223aSSinan Kaya 	IO_CONCAT(__IO_PREFIX,iowrite8)(b, addr);
481da177e4SLinus Torvalds }
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds void iowrite16(u16 b, void __iomem *addr)
511da177e4SLinus Torvalds {
521da177e4SLinus Torvalds 	mb();
5392d7223aSSinan Kaya 	IO_CONCAT(__IO_PREFIX,iowrite16)(b, addr);
541da177e4SLinus Torvalds }
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds void iowrite32(u32 b, void __iomem *addr)
571da177e4SLinus Torvalds {
581da177e4SLinus Torvalds 	mb();
5992d7223aSSinan Kaya 	IO_CONCAT(__IO_PREFIX,iowrite32)(b, addr);
601da177e4SLinus Torvalds }
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds EXPORT_SYMBOL(ioread8);
631da177e4SLinus Torvalds EXPORT_SYMBOL(ioread16);
641da177e4SLinus Torvalds EXPORT_SYMBOL(ioread32);
651da177e4SLinus Torvalds EXPORT_SYMBOL(iowrite8);
661da177e4SLinus Torvalds EXPORT_SYMBOL(iowrite16);
671da177e4SLinus Torvalds EXPORT_SYMBOL(iowrite32);
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds u8 inb(unsigned long port)
701da177e4SLinus Torvalds {
711da177e4SLinus Torvalds 	return ioread8(ioport_map(port, 1));
721da177e4SLinus Torvalds }
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds u16 inw(unsigned long port)
751da177e4SLinus Torvalds {
761da177e4SLinus Torvalds 	return ioread16(ioport_map(port, 2));
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds u32 inl(unsigned long port)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	return ioread32(ioport_map(port, 4));
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds void outb(u8 b, unsigned long port)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	iowrite8(b, ioport_map(port, 1));
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds void outw(u16 b, unsigned long port)
901da177e4SLinus Torvalds {
911da177e4SLinus Torvalds 	iowrite16(b, ioport_map(port, 2));
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds void outl(u32 b, unsigned long port)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds 	iowrite32(b, ioport_map(port, 4));
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds EXPORT_SYMBOL(inb);
1001da177e4SLinus Torvalds EXPORT_SYMBOL(inw);
1011da177e4SLinus Torvalds EXPORT_SYMBOL(inl);
1021da177e4SLinus Torvalds EXPORT_SYMBOL(outb);
1031da177e4SLinus Torvalds EXPORT_SYMBOL(outw);
1041da177e4SLinus Torvalds EXPORT_SYMBOL(outl);
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds u8 __raw_readb(const volatile void __iomem *addr)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	return IO_CONCAT(__IO_PREFIX,readb)(addr);
1091da177e4SLinus Torvalds }
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds u16 __raw_readw(const volatile void __iomem *addr)
1121da177e4SLinus Torvalds {
1131da177e4SLinus Torvalds 	return IO_CONCAT(__IO_PREFIX,readw)(addr);
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds u32 __raw_readl(const volatile void __iomem *addr)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	return IO_CONCAT(__IO_PREFIX,readl)(addr);
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds u64 __raw_readq(const volatile void __iomem *addr)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	return IO_CONCAT(__IO_PREFIX,readq)(addr);
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds void __raw_writeb(u8 b, volatile void __iomem *addr)
1271da177e4SLinus Torvalds {
1281da177e4SLinus Torvalds 	IO_CONCAT(__IO_PREFIX,writeb)(b, addr);
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds void __raw_writew(u16 b, volatile void __iomem *addr)
1321da177e4SLinus Torvalds {
1331da177e4SLinus Torvalds 	IO_CONCAT(__IO_PREFIX,writew)(b, addr);
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds void __raw_writel(u32 b, volatile void __iomem *addr)
1371da177e4SLinus Torvalds {
1381da177e4SLinus Torvalds 	IO_CONCAT(__IO_PREFIX,writel)(b, addr);
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds void __raw_writeq(u64 b, volatile void __iomem *addr)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	IO_CONCAT(__IO_PREFIX,writeq)(b, addr);
1441da177e4SLinus Torvalds }
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_readb);
1471da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_readw);
1481da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_readl);
1491da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_readq);
1501da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_writeb);
1511da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_writew);
1521da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_writel);
1531da177e4SLinus Torvalds EXPORT_SYMBOL(__raw_writeq);
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds u8 readb(const volatile void __iomem *addr)
1561da177e4SLinus Torvalds {
157*54505a1eSMikulas Patocka 	u8 ret;
158*54505a1eSMikulas Patocka 	mb();
159*54505a1eSMikulas Patocka 	ret = __raw_readb(addr);
1601da177e4SLinus Torvalds 	mb();
1611da177e4SLinus Torvalds 	return ret;
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds u16 readw(const volatile void __iomem *addr)
1651da177e4SLinus Torvalds {
166*54505a1eSMikulas Patocka 	u16 ret;
167*54505a1eSMikulas Patocka 	mb();
168*54505a1eSMikulas Patocka 	ret = __raw_readw(addr);
1691da177e4SLinus Torvalds 	mb();
1701da177e4SLinus Torvalds 	return ret;
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds u32 readl(const volatile void __iomem *addr)
1741da177e4SLinus Torvalds {
175*54505a1eSMikulas Patocka 	u32 ret;
176*54505a1eSMikulas Patocka 	mb();
177*54505a1eSMikulas Patocka 	ret = __raw_readl(addr);
1781da177e4SLinus Torvalds 	mb();
1791da177e4SLinus Torvalds 	return ret;
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds u64 readq(const volatile void __iomem *addr)
1831da177e4SLinus Torvalds {
184*54505a1eSMikulas Patocka 	u64 ret;
185*54505a1eSMikulas Patocka 	mb();
186*54505a1eSMikulas Patocka 	ret = __raw_readq(addr);
1871da177e4SLinus Torvalds 	mb();
1881da177e4SLinus Torvalds 	return ret;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds void writeb(u8 b, volatile void __iomem *addr)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	mb();
19492d7223aSSinan Kaya 	__raw_writeb(b, addr);
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds void writew(u16 b, volatile void __iomem *addr)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	mb();
20092d7223aSSinan Kaya 	__raw_writew(b, addr);
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds void writel(u32 b, volatile void __iomem *addr)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	mb();
20692d7223aSSinan Kaya 	__raw_writel(b, addr);
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds void writeq(u64 b, volatile void __iomem *addr)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	mb();
21292d7223aSSinan Kaya 	__raw_writeq(b, addr);
2131da177e4SLinus Torvalds }
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds EXPORT_SYMBOL(readb);
2161da177e4SLinus Torvalds EXPORT_SYMBOL(readw);
2171da177e4SLinus Torvalds EXPORT_SYMBOL(readl);
2181da177e4SLinus Torvalds EXPORT_SYMBOL(readq);
2191da177e4SLinus Torvalds EXPORT_SYMBOL(writeb);
2201da177e4SLinus Torvalds EXPORT_SYMBOL(writew);
2211da177e4SLinus Torvalds EXPORT_SYMBOL(writel);
2221da177e4SLinus Torvalds EXPORT_SYMBOL(writeq);
2231da177e4SLinus Torvalds 
224*54505a1eSMikulas Patocka /*
225*54505a1eSMikulas Patocka  * The _relaxed functions must be ordered w.r.t. each other, but they don't
226*54505a1eSMikulas Patocka  * have to be ordered w.r.t. other memory accesses.
227*54505a1eSMikulas Patocka  */
228*54505a1eSMikulas Patocka u8 readb_relaxed(const volatile void __iomem *addr)
229*54505a1eSMikulas Patocka {
230*54505a1eSMikulas Patocka 	mb();
231*54505a1eSMikulas Patocka 	return __raw_readb(addr);
232*54505a1eSMikulas Patocka }
233*54505a1eSMikulas Patocka 
234*54505a1eSMikulas Patocka u16 readw_relaxed(const volatile void __iomem *addr)
235*54505a1eSMikulas Patocka {
236*54505a1eSMikulas Patocka 	mb();
237*54505a1eSMikulas Patocka 	return __raw_readw(addr);
238*54505a1eSMikulas Patocka }
239*54505a1eSMikulas Patocka 
240*54505a1eSMikulas Patocka u32 readl_relaxed(const volatile void __iomem *addr)
241*54505a1eSMikulas Patocka {
242*54505a1eSMikulas Patocka 	mb();
243*54505a1eSMikulas Patocka 	return __raw_readl(addr);
244*54505a1eSMikulas Patocka }
245*54505a1eSMikulas Patocka 
246*54505a1eSMikulas Patocka u64 readq_relaxed(const volatile void __iomem *addr)
247*54505a1eSMikulas Patocka {
248*54505a1eSMikulas Patocka 	mb();
249*54505a1eSMikulas Patocka 	return __raw_readq(addr);
250*54505a1eSMikulas Patocka }
251*54505a1eSMikulas Patocka 
252*54505a1eSMikulas Patocka EXPORT_SYMBOL(readb_relaxed);
253*54505a1eSMikulas Patocka EXPORT_SYMBOL(readw_relaxed);
254*54505a1eSMikulas Patocka EXPORT_SYMBOL(readl_relaxed);
255*54505a1eSMikulas Patocka EXPORT_SYMBOL(readq_relaxed);
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds /*
2581da177e4SLinus Torvalds  * Read COUNT 8-bit bytes from port PORT into memory starting at SRC.
2591da177e4SLinus Torvalds  */
2601da177e4SLinus Torvalds void ioread8_rep(void __iomem *port, void *dst, unsigned long count)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	while ((unsigned long)dst & 0x3) {
2631da177e4SLinus Torvalds 		if (!count)
2641da177e4SLinus Torvalds 			return;
2651da177e4SLinus Torvalds 		count--;
2661da177e4SLinus Torvalds 		*(unsigned char *)dst = ioread8(port);
2671da177e4SLinus Torvalds 		dst += 1;
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	while (count >= 4) {
2711da177e4SLinus Torvalds 		unsigned int w;
2721da177e4SLinus Torvalds 		count -= 4;
2731da177e4SLinus Torvalds 		w = ioread8(port);
2741da177e4SLinus Torvalds 		w |= ioread8(port) << 8;
2751da177e4SLinus Torvalds 		w |= ioread8(port) << 16;
2761da177e4SLinus Torvalds 		w |= ioread8(port) << 24;
2771da177e4SLinus Torvalds 		*(unsigned int *)dst = w;
2781da177e4SLinus Torvalds 		dst += 4;
2791da177e4SLinus Torvalds 	}
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	while (count) {
2821da177e4SLinus Torvalds 		--count;
2831da177e4SLinus Torvalds 		*(unsigned char *)dst = ioread8(port);
2841da177e4SLinus Torvalds 		dst += 1;
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds void insb(unsigned long port, void *dst, unsigned long count)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds 	ioread8_rep(ioport_map(port, 1), dst, count);
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds EXPORT_SYMBOL(ioread8_rep);
2941da177e4SLinus Torvalds EXPORT_SYMBOL(insb);
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds /*
2971da177e4SLinus Torvalds  * Read COUNT 16-bit words from port PORT into memory starting at
2981da177e4SLinus Torvalds  * SRC.  SRC must be at least short aligned.  This is used by the
2991da177e4SLinus Torvalds  * IDE driver to read disk sectors.  Performance is important, but
3001da177e4SLinus Torvalds  * the interfaces seems to be slow: just using the inlined version
3011da177e4SLinus Torvalds  * of the inw() breaks things.
3021da177e4SLinus Torvalds  */
3031da177e4SLinus Torvalds void ioread16_rep(void __iomem *port, void *dst, unsigned long count)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	if (unlikely((unsigned long)dst & 0x3)) {
3061da177e4SLinus Torvalds 		if (!count)
3071da177e4SLinus Torvalds 			return;
3081da177e4SLinus Torvalds 		BUG_ON((unsigned long)dst & 0x1);
3091da177e4SLinus Torvalds 		count--;
3101da177e4SLinus Torvalds 		*(unsigned short *)dst = ioread16(port);
3111da177e4SLinus Torvalds 		dst += 2;
3121da177e4SLinus Torvalds 	}
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	while (count >= 2) {
3151da177e4SLinus Torvalds 		unsigned int w;
3161da177e4SLinus Torvalds 		count -= 2;
3171da177e4SLinus Torvalds 		w = ioread16(port);
3181da177e4SLinus Torvalds 		w |= ioread16(port) << 16;
3191da177e4SLinus Torvalds 		*(unsigned int *)dst = w;
3201da177e4SLinus Torvalds 		dst += 4;
3211da177e4SLinus Torvalds 	}
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	if (count) {
3241da177e4SLinus Torvalds 		*(unsigned short*)dst = ioread16(port);
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds void insw(unsigned long port, void *dst, unsigned long count)
3291da177e4SLinus Torvalds {
3301da177e4SLinus Torvalds 	ioread16_rep(ioport_map(port, 2), dst, count);
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds EXPORT_SYMBOL(ioread16_rep);
3341da177e4SLinus Torvalds EXPORT_SYMBOL(insw);
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds /*
3381da177e4SLinus Torvalds  * Read COUNT 32-bit words from port PORT into memory starting at
3391da177e4SLinus Torvalds  * SRC. Now works with any alignment in SRC. Performance is important,
3401da177e4SLinus Torvalds  * but the interfaces seems to be slow: just using the inlined version
3411da177e4SLinus Torvalds  * of the inl() breaks things.
3421da177e4SLinus Torvalds  */
3431da177e4SLinus Torvalds void ioread32_rep(void __iomem *port, void *dst, unsigned long count)
3441da177e4SLinus Torvalds {
3451da177e4SLinus Torvalds 	if (unlikely((unsigned long)dst & 0x3)) {
3461da177e4SLinus Torvalds 		while (count--) {
3471da177e4SLinus Torvalds 			struct S { int x __attribute__((packed)); };
3481da177e4SLinus Torvalds 			((struct S *)dst)->x = ioread32(port);
3491da177e4SLinus Torvalds 			dst += 4;
3501da177e4SLinus Torvalds 		}
3511da177e4SLinus Torvalds 	} else {
3521da177e4SLinus Torvalds 		/* Buffer 32-bit aligned.  */
3531da177e4SLinus Torvalds 		while (count--) {
3541da177e4SLinus Torvalds 			*(unsigned int *)dst = ioread32(port);
3551da177e4SLinus Torvalds 			dst += 4;
3561da177e4SLinus Torvalds 		}
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds void insl(unsigned long port, void *dst, unsigned long count)
3611da177e4SLinus Torvalds {
3621da177e4SLinus Torvalds 	ioread32_rep(ioport_map(port, 4), dst, count);
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds EXPORT_SYMBOL(ioread32_rep);
3661da177e4SLinus Torvalds EXPORT_SYMBOL(insl);
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds /*
3701da177e4SLinus Torvalds  * Like insb but in the opposite direction.
3711da177e4SLinus Torvalds  * Don't worry as much about doing aligned memory transfers:
3721da177e4SLinus Torvalds  * doing byte reads the "slow" way isn't nearly as slow as
3731da177e4SLinus Torvalds  * doing byte writes the slow way (no r-m-w cycle).
3741da177e4SLinus Torvalds  */
3751da177e4SLinus Torvalds void iowrite8_rep(void __iomem *port, const void *xsrc, unsigned long count)
3761da177e4SLinus Torvalds {
3771da177e4SLinus Torvalds 	const unsigned char *src = xsrc;
3781da177e4SLinus Torvalds 	while (count--)
3791da177e4SLinus Torvalds 		iowrite8(*src++, port);
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds void outsb(unsigned long port, const void *src, unsigned long count)
3831da177e4SLinus Torvalds {
3841da177e4SLinus Torvalds 	iowrite8_rep(ioport_map(port, 1), src, count);
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds EXPORT_SYMBOL(iowrite8_rep);
3881da177e4SLinus Torvalds EXPORT_SYMBOL(outsb);
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds /*
3921da177e4SLinus Torvalds  * Like insw but in the opposite direction.  This is used by the IDE
3931da177e4SLinus Torvalds  * driver to write disk sectors.  Performance is important, but the
3941da177e4SLinus Torvalds  * interfaces seems to be slow: just using the inlined version of the
3951da177e4SLinus Torvalds  * outw() breaks things.
3961da177e4SLinus Torvalds  */
3971da177e4SLinus Torvalds void iowrite16_rep(void __iomem *port, const void *src, unsigned long count)
3981da177e4SLinus Torvalds {
3991da177e4SLinus Torvalds 	if (unlikely((unsigned long)src & 0x3)) {
4001da177e4SLinus Torvalds 		if (!count)
4011da177e4SLinus Torvalds 			return;
4021da177e4SLinus Torvalds 		BUG_ON((unsigned long)src & 0x1);
4031da177e4SLinus Torvalds 		iowrite16(*(unsigned short *)src, port);
4041da177e4SLinus Torvalds 		src += 2;
4051da177e4SLinus Torvalds 		--count;
4061da177e4SLinus Torvalds 	}
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	while (count >= 2) {
4091da177e4SLinus Torvalds 		unsigned int w;
4101da177e4SLinus Torvalds 		count -= 2;
4111da177e4SLinus Torvalds 		w = *(unsigned int *)src;
4121da177e4SLinus Torvalds 		src += 4;
4131da177e4SLinus Torvalds 		iowrite16(w >>  0, port);
4141da177e4SLinus Torvalds 		iowrite16(w >> 16, port);
4151da177e4SLinus Torvalds 	}
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	if (count) {
4181da177e4SLinus Torvalds 		iowrite16(*(unsigned short *)src, port);
4191da177e4SLinus Torvalds 	}
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds void outsw(unsigned long port, const void *src, unsigned long count)
4231da177e4SLinus Torvalds {
4241da177e4SLinus Torvalds 	iowrite16_rep(ioport_map(port, 2), src, count);
4251da177e4SLinus Torvalds }
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds EXPORT_SYMBOL(iowrite16_rep);
4281da177e4SLinus Torvalds EXPORT_SYMBOL(outsw);
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds /*
4321da177e4SLinus Torvalds  * Like insl but in the opposite direction.  This is used by the IDE
4331da177e4SLinus Torvalds  * driver to write disk sectors.  Works with any alignment in SRC.
4341da177e4SLinus Torvalds  * Performance is important, but the interfaces seems to be slow:
4351da177e4SLinus Torvalds  * just using the inlined version of the outl() breaks things.
4361da177e4SLinus Torvalds  */
4371da177e4SLinus Torvalds void iowrite32_rep(void __iomem *port, const void *src, unsigned long count)
4381da177e4SLinus Torvalds {
4391da177e4SLinus Torvalds 	if (unlikely((unsigned long)src & 0x3)) {
4401da177e4SLinus Torvalds 		while (count--) {
4411da177e4SLinus Torvalds 			struct S { int x __attribute__((packed)); };
4421da177e4SLinus Torvalds 			iowrite32(((struct S *)src)->x, port);
4431da177e4SLinus Torvalds 			src += 4;
4441da177e4SLinus Torvalds 		}
4451da177e4SLinus Torvalds 	} else {
4461da177e4SLinus Torvalds 		/* Buffer 32-bit aligned.  */
4471da177e4SLinus Torvalds 		while (count--) {
4481da177e4SLinus Torvalds 			iowrite32(*(unsigned int *)src, port);
4491da177e4SLinus Torvalds 			src += 4;
4501da177e4SLinus Torvalds 		}
4511da177e4SLinus Torvalds 	}
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds void outsl(unsigned long port, const void *src, unsigned long count)
4551da177e4SLinus Torvalds {
4561da177e4SLinus Torvalds 	iowrite32_rep(ioport_map(port, 4), src, count);
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds EXPORT_SYMBOL(iowrite32_rep);
4601da177e4SLinus Torvalds EXPORT_SYMBOL(outsl);
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds /*
4641da177e4SLinus Torvalds  * Copy data from IO memory space to "real" memory space.
4651da177e4SLinus Torvalds  * This needs to be optimized.
4661da177e4SLinus Torvalds  */
4671da177e4SLinus Torvalds void memcpy_fromio(void *to, const volatile void __iomem *from, long count)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds 	/* Optimize co-aligned transfers.  Everything else gets handled
4701da177e4SLinus Torvalds 	   a byte at a time. */
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	if (count >= 8 && ((u64)to & 7) == ((u64)from & 7)) {
4731da177e4SLinus Torvalds 		count -= 8;
4741da177e4SLinus Torvalds 		do {
4751da177e4SLinus Torvalds 			*(u64 *)to = __raw_readq(from);
4761da177e4SLinus Torvalds 			count -= 8;
4771da177e4SLinus Torvalds 			to += 8;
4781da177e4SLinus Torvalds 			from += 8;
4791da177e4SLinus Torvalds 		} while (count >= 0);
4801da177e4SLinus Torvalds 		count += 8;
4811da177e4SLinus Torvalds 	}
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	if (count >= 4 && ((u64)to & 3) == ((u64)from & 3)) {
4841da177e4SLinus Torvalds 		count -= 4;
4851da177e4SLinus Torvalds 		do {
4861da177e4SLinus Torvalds 			*(u32 *)to = __raw_readl(from);
4871da177e4SLinus Torvalds 			count -= 4;
4881da177e4SLinus Torvalds 			to += 4;
4891da177e4SLinus Torvalds 			from += 4;
4901da177e4SLinus Torvalds 		} while (count >= 0);
4911da177e4SLinus Torvalds 		count += 4;
4921da177e4SLinus Torvalds 	}
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	if (count >= 2 && ((u64)to & 1) == ((u64)from & 1)) {
4951da177e4SLinus Torvalds 		count -= 2;
4961da177e4SLinus Torvalds 		do {
4971da177e4SLinus Torvalds 			*(u16 *)to = __raw_readw(from);
4981da177e4SLinus Torvalds 			count -= 2;
4991da177e4SLinus Torvalds 			to += 2;
5001da177e4SLinus Torvalds 			from += 2;
5011da177e4SLinus Torvalds 		} while (count >= 0);
5021da177e4SLinus Torvalds 		count += 2;
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	while (count > 0) {
5061da177e4SLinus Torvalds 		*(u8 *) to = __raw_readb(from);
5071da177e4SLinus Torvalds 		count--;
5081da177e4SLinus Torvalds 		to++;
5091da177e4SLinus Torvalds 		from++;
5101da177e4SLinus Torvalds 	}
5111da177e4SLinus Torvalds 	mb();
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds EXPORT_SYMBOL(memcpy_fromio);
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds /*
5181da177e4SLinus Torvalds  * Copy data from "real" memory space to IO memory space.
5191da177e4SLinus Torvalds  * This needs to be optimized.
5201da177e4SLinus Torvalds  */
5211da177e4SLinus Torvalds void memcpy_toio(volatile void __iomem *to, const void *from, long count)
5221da177e4SLinus Torvalds {
5231da177e4SLinus Torvalds 	/* Optimize co-aligned transfers.  Everything else gets handled
5241da177e4SLinus Torvalds 	   a byte at a time. */
5251da177e4SLinus Torvalds 	/* FIXME -- align FROM.  */
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	if (count >= 8 && ((u64)to & 7) == ((u64)from & 7)) {
5281da177e4SLinus Torvalds 		count -= 8;
5291da177e4SLinus Torvalds 		do {
5301da177e4SLinus Torvalds 			__raw_writeq(*(const u64 *)from, to);
5311da177e4SLinus Torvalds 			count -= 8;
5321da177e4SLinus Torvalds 			to += 8;
5331da177e4SLinus Torvalds 			from += 8;
5341da177e4SLinus Torvalds 		} while (count >= 0);
5351da177e4SLinus Torvalds 		count += 8;
5361da177e4SLinus Torvalds 	}
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	if (count >= 4 && ((u64)to & 3) == ((u64)from & 3)) {
5391da177e4SLinus Torvalds 		count -= 4;
5401da177e4SLinus Torvalds 		do {
5411da177e4SLinus Torvalds 			__raw_writel(*(const u32 *)from, to);
5421da177e4SLinus Torvalds 			count -= 4;
5431da177e4SLinus Torvalds 			to += 4;
5441da177e4SLinus Torvalds 			from += 4;
5451da177e4SLinus Torvalds 		} while (count >= 0);
5461da177e4SLinus Torvalds 		count += 4;
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds 	if (count >= 2 && ((u64)to & 1) == ((u64)from & 1)) {
5501da177e4SLinus Torvalds 		count -= 2;
5511da177e4SLinus Torvalds 		do {
5521da177e4SLinus Torvalds 			__raw_writew(*(const u16 *)from, to);
5531da177e4SLinus Torvalds 			count -= 2;
5541da177e4SLinus Torvalds 			to += 2;
5551da177e4SLinus Torvalds 			from += 2;
5561da177e4SLinus Torvalds 		} while (count >= 0);
5571da177e4SLinus Torvalds 		count += 2;
5581da177e4SLinus Torvalds 	}
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	while (count > 0) {
5611da177e4SLinus Torvalds 		__raw_writeb(*(const u8 *) from, to);
5621da177e4SLinus Torvalds 		count--;
5631da177e4SLinus Torvalds 		to++;
5641da177e4SLinus Torvalds 		from++;
5651da177e4SLinus Torvalds 	}
5661da177e4SLinus Torvalds 	mb();
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds EXPORT_SYMBOL(memcpy_toio);
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds /*
5731da177e4SLinus Torvalds  * "memset" on IO memory space.
5741da177e4SLinus Torvalds  */
5751da177e4SLinus Torvalds void _memset_c_io(volatile void __iomem *to, unsigned long c, long count)
5761da177e4SLinus Torvalds {
5771da177e4SLinus Torvalds 	/* Handle any initial odd byte */
5781da177e4SLinus Torvalds 	if (count > 0 && ((u64)to & 1)) {
5791da177e4SLinus Torvalds 		__raw_writeb(c, to);
5801da177e4SLinus Torvalds 		to++;
5811da177e4SLinus Torvalds 		count--;
5821da177e4SLinus Torvalds 	}
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	/* Handle any initial odd halfword */
5851da177e4SLinus Torvalds 	if (count >= 2 && ((u64)to & 2)) {
5861da177e4SLinus Torvalds 		__raw_writew(c, to);
5871da177e4SLinus Torvalds 		to += 2;
5881da177e4SLinus Torvalds 		count -= 2;
5891da177e4SLinus Torvalds 	}
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	/* Handle any initial odd word */
5921da177e4SLinus Torvalds 	if (count >= 4 && ((u64)to & 4)) {
5931da177e4SLinus Torvalds 		__raw_writel(c, to);
5941da177e4SLinus Torvalds 		to += 4;
5951da177e4SLinus Torvalds 		count -= 4;
5961da177e4SLinus Torvalds 	}
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	/* Handle all full-sized quadwords: we're aligned
5991da177e4SLinus Torvalds 	   (or have a small count) */
6001da177e4SLinus Torvalds 	count -= 8;
6011da177e4SLinus Torvalds 	if (count >= 0) {
6021da177e4SLinus Torvalds 		do {
6031da177e4SLinus Torvalds 			__raw_writeq(c, to);
6041da177e4SLinus Torvalds 			to += 8;
6051da177e4SLinus Torvalds 			count -= 8;
6061da177e4SLinus Torvalds 		} while (count >= 0);
6071da177e4SLinus Torvalds 	}
6081da177e4SLinus Torvalds 	count += 8;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	/* The tail is word-aligned if we still have count >= 4 */
6111da177e4SLinus Torvalds 	if (count >= 4) {
6121da177e4SLinus Torvalds 		__raw_writel(c, to);
6131da177e4SLinus Torvalds 		to += 4;
6141da177e4SLinus Torvalds 		count -= 4;
6151da177e4SLinus Torvalds 	}
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds 	/* The tail is half-word aligned if we have count >= 2 */
6181da177e4SLinus Torvalds 	if (count >= 2) {
6191da177e4SLinus Torvalds 		__raw_writew(c, to);
6201da177e4SLinus Torvalds 		to += 2;
6211da177e4SLinus Torvalds 		count -= 2;
6221da177e4SLinus Torvalds 	}
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	/* And finally, one last byte.. */
6251da177e4SLinus Torvalds 	if (count) {
6261da177e4SLinus Torvalds 		__raw_writeb(c, to);
6271da177e4SLinus Torvalds 	}
6281da177e4SLinus Torvalds 	mb();
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds EXPORT_SYMBOL(_memset_c_io);
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds /* A version of memcpy used by the vga console routines to move data around
6341da177e4SLinus Torvalds    arbitrarily between screen and main memory.  */
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds void
6371da177e4SLinus Torvalds scr_memcpyw(u16 *d, const u16 *s, unsigned int count)
6381da177e4SLinus Torvalds {
6391da177e4SLinus Torvalds 	const u16 __iomem *ios = (const u16 __iomem *) s;
6401da177e4SLinus Torvalds 	u16 __iomem *iod = (u16 __iomem *) d;
6411da177e4SLinus Torvalds 	int s_isio = __is_ioaddr(s);
6421da177e4SLinus Torvalds 	int d_isio = __is_ioaddr(d);
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	if (s_isio) {
6451da177e4SLinus Torvalds 		if (d_isio) {
6461da177e4SLinus Torvalds 			/* FIXME: Should handle unaligned ops and
6471da177e4SLinus Torvalds 			   operation widening.  */
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 			count /= 2;
6501da177e4SLinus Torvalds 			while (count--) {
6511da177e4SLinus Torvalds 				u16 tmp = __raw_readw(ios++);
6521da177e4SLinus Torvalds 				__raw_writew(tmp, iod++);
6531da177e4SLinus Torvalds 			}
6541da177e4SLinus Torvalds 		}
6551da177e4SLinus Torvalds 		else
6561da177e4SLinus Torvalds 			memcpy_fromio(d, ios, count);
6571da177e4SLinus Torvalds 	} else {
6581da177e4SLinus Torvalds 		if (d_isio)
6591da177e4SLinus Torvalds 			memcpy_toio(iod, s, count);
6601da177e4SLinus Torvalds 		else
6611da177e4SLinus Torvalds 			memcpy(d, s, count);
6621da177e4SLinus Torvalds 	}
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds EXPORT_SYMBOL(scr_memcpyw);
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds void __iomem *ioport_map(unsigned long port, unsigned int size)
6681da177e4SLinus Torvalds {
6691da177e4SLinus Torvalds 	return IO_CONCAT(__IO_PREFIX,ioportmap) (port);
6701da177e4SLinus Torvalds }
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds void ioport_unmap(void __iomem *addr)
6731da177e4SLinus Torvalds {
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds EXPORT_SYMBOL(ioport_map);
6771da177e4SLinus Torvalds EXPORT_SYMBOL(ioport_unmap);
678