1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) Matthew Wilcox 2001 for Hewlett-Packard
8 * IO accessing functions which shouldn't be inlined because they're too big
11 #include <linux/kernel.h>
12 #include <linux/module.h>
15 /* Copies a block of memory to a device in an efficient manner.
16 * Assumes the device can cope with 32-bit transfers. If it can't,
17 * don't use this function.
19 void memcpy_toio(volatile void __iomem *dst, const void *src, int count)
21 if (((unsigned long)dst & 3) != ((unsigned long)src & 3))
23 while ((unsigned long)dst & 3) {
24 writeb(*(char *)src, dst++);
29 __raw_writel(*(u32 *)src, dst);
36 writeb(*(char *)src, dst++);
42 ** Copies a block of memory from a device in an efficient manner.
43 ** Assumes the device can cope with 32-bit transfers. If it can't,
44 ** don't use this function.
46 ** CR16 counts on C3000 reading 256 bytes from Symbios 896 RAM:
47 ** 27341/64 = 427 cyc per int
48 ** 61311/128 = 478 cyc per short
49 ** 122637/256 = 479 cyc per byte
50 ** Ergo bus latencies dominant (not transfer size).
51 ** Minimize total number of transfers at cost of CPU cycles.
52 ** TODO: only look at src alignment and adjust the stores to dest.
54 void memcpy_fromio(void *dst, const volatile void __iomem *src, int count)
56 /* first compare alignment of src/dst */
57 if ( (((unsigned long)dst ^ (unsigned long)src) & 1) || (count < 2) )
60 if ( (((unsigned long)dst ^ (unsigned long)src) & 2) || (count < 4) )
63 /* Then check for misaligned start address */
64 if ((unsigned long)src & 1) {
65 *(u8 *)dst = readb(src);
69 if (count < 2) goto bytecopy;
72 if ((unsigned long)src & 2) {
73 *(u16 *)dst = __raw_readw(src);
80 *(u32 *)dst = __raw_readl(src);
88 *(u16 *)dst = __raw_readw(src);
96 *(char *)dst = readb(src);
102 /* Sets a block of memory on a device to a given value.
103 * Assumes the device can cope with 32-bit transfers. If it can't,
104 * don't use this function.
106 void memset_io(volatile void __iomem *addr, unsigned char val, int count)
108 u32 val32 = (val << 24) | (val << 16) | (val << 8) | val;
109 while ((unsigned long)addr & 3) {
114 __raw_writel(val32, addr);
124 * Read COUNT 8-bit bytes from port PORT into memory starting at
127 void insb (unsigned long port, void *dst, unsigned long count)
131 p = (unsigned char *)dst;
133 while (((unsigned long)p) & 0x3) {
145 w |= inb(port) << 16;
148 *(unsigned int *) p = w;
161 * Read COUNT 16-bit words from port PORT into memory starting at
162 * SRC. SRC must be at least short aligned. This is used by the
163 * IDE driver to read disk sectors. Performance is important, but
164 * the interfaces seems to be slow: just using the inlined version
165 * of the inw() breaks things.
167 void insw (unsigned long port, void *dst, unsigned long count)
169 unsigned int l = 0, l2;
172 p = (unsigned char *)dst;
177 switch (((unsigned long)p) & 0x3)
179 case 0x00: /* Buffer 32-bit aligned */
183 l = cpu_to_le16(inw(port)) << 16;
184 l |= cpu_to_le16(inw(port));
185 *(unsigned int *)p = l;
189 *(unsigned short *)p = cpu_to_le16(inw(port));
193 case 0x02: /* Buffer 16-bit aligned */
194 *(unsigned short *)p = cpu_to_le16(inw(port));
200 l = cpu_to_le16(inw(port)) << 16;
201 l |= cpu_to_le16(inw(port));
202 *(unsigned int *)p = l;
206 *(unsigned short *)p = cpu_to_le16(inw(port));
210 case 0x01: /* Buffer 8-bit aligned */
212 /* I don't bother with 32bit transfers
213 * in this case, 16bit will have to do -- DE */
216 l = cpu_to_le16(inw(port));
221 l2 = cpu_to_le16(inw(port));
222 *(unsigned short *)p = (l & 0xff) << 8 | (l2 >> 8);
234 * Read COUNT 32-bit words from port PORT into memory starting at
235 * SRC. Now works with any alignment in SRC. Performance is important,
236 * but the interfaces seems to be slow: just using the inlined version
237 * of the inl() breaks things.
239 void insl (unsigned long port, void *dst, unsigned long count)
241 unsigned int l = 0, l2;
244 p = (unsigned char *)dst;
249 switch (((unsigned long) dst) & 0x3)
251 case 0x00: /* Buffer 32-bit aligned */
254 *(unsigned int *)p = cpu_to_le32(inl(port));
259 case 0x02: /* Buffer 16-bit aligned */
262 l = cpu_to_le32(inl(port));
263 *(unsigned short *)p = l >> 16;
268 l2 = cpu_to_le32(inl(port));
269 *(unsigned int *)p = (l & 0xffff) << 16 | (l2 >> 16);
273 *(unsigned short *)p = l & 0xffff;
275 case 0x01: /* Buffer 8-bit aligned */
278 l = cpu_to_le32(inl(port));
279 *(unsigned char *)p = l >> 24;
281 *(unsigned short *)p = (l >> 8) & 0xffff;
285 l2 = cpu_to_le32(inl(port));
286 *(unsigned int *)p = (l & 0xff) << 24 | (l2 >> 8);
292 case 0x03: /* Buffer 8-bit aligned */
295 l = cpu_to_le32(inl(port));
300 l2 = cpu_to_le32(inl(port));
301 *(unsigned int *)p = (l & 0xffffff) << 8 | l2 >> 24;
305 *(unsigned short *)p = (l >> 8) & 0xffff;
314 * Like insb but in the opposite direction.
315 * Don't worry as much about doing aligned memory transfers:
316 * doing byte reads the "slow" way isn't nearly as slow as
317 * doing byte writes the slow way (no r-m-w cycle).
319 void outsb(unsigned long port, const void * src, unsigned long count)
321 const unsigned char *p;
323 p = (const unsigned char *)src;
332 * Like insw but in the opposite direction. This is used by the IDE
333 * driver to write disk sectors. Performance is important, but the
334 * interfaces seems to be slow: just using the inlined version of the
335 * outw() breaks things.
337 void outsw (unsigned long port, const void *src, unsigned long count)
339 unsigned int l = 0, l2;
340 const unsigned char *p;
342 p = (const unsigned char *)src;
347 switch (((unsigned long)p) & 0x3)
349 case 0x00: /* Buffer 32-bit aligned */
352 l = *(unsigned int *)p;
354 outw(le16_to_cpu(l >> 16), port);
355 outw(le16_to_cpu(l & 0xffff), port);
358 outw(le16_to_cpu(*(unsigned short*)p), port);
362 case 0x02: /* Buffer 16-bit aligned */
364 outw(le16_to_cpu(*(unsigned short*)p), port);
370 l = *(unsigned int *)p;
372 outw(le16_to_cpu(l >> 16), port);
373 outw(le16_to_cpu(l & 0xffff), port);
376 outw(le16_to_cpu(*(unsigned short *)p), port);
380 case 0x01: /* Buffer 8-bit aligned */
381 /* I don't bother with 32bit transfers
382 * in this case, 16bit will have to do -- DE */
390 l2 = *(unsigned short *)p;
392 outw(le16_to_cpu(l | l2 >> 8), port);
395 l2 = *(unsigned char *)p;
396 outw (le16_to_cpu(l | l2>>8), port);
404 * Like insl but in the opposite direction. This is used by the IDE
405 * driver to write disk sectors. Works with any alignment in SRC.
406 * Performance is important, but the interfaces seems to be slow:
407 * just using the inlined version of the outl() breaks things.
409 void outsl (unsigned long port, const void *src, unsigned long count)
411 unsigned int l = 0, l2;
412 const unsigned char *p;
414 p = (const unsigned char *)src;
419 switch (((unsigned long)p) & 0x3)
421 case 0x00: /* Buffer 32-bit aligned */
424 outl(le32_to_cpu(*(unsigned int *)p), port);
429 case 0x02: /* Buffer 16-bit aligned */
432 l = *(unsigned short *)p;
437 l2 = *(unsigned int *)p;
439 outl (le32_to_cpu(l << 16 | l2 >> 16), port);
442 l2 = *(unsigned short *)p;
443 outl (le32_to_cpu(l << 16 | l2), port);
445 case 0x01: /* Buffer 8-bit aligned */
450 l |= *(unsigned short *)p << 8;
455 l2 = *(unsigned int *)p;
457 outl (le32_to_cpu(l | l2 >> 24), port);
461 outl (le32_to_cpu(l | l2), port);
463 case 0x03: /* Buffer 8-bit aligned */
471 l2 = *(unsigned int *)p;
473 outl (le32_to_cpu(l | l2 >> 8), port);
476 l2 = *(unsigned short *)p << 16;
479 outl (le32_to_cpu(l | l2), port);
487 EXPORT_SYMBOL(outsb);
488 EXPORT_SYMBOL(outsw);
489 EXPORT_SYMBOL(outsl);