4 * Copyright (c) Matthew Wilcox 2001 for Hewlett-Packard
5 * Copyright (c) Randolph Chung 2001 <tausq@debian.org>
7 * IO accessing functions which shouldn't be inlined because they're too big
10 #include <linux/kernel.h>
11 #include <linux/module.h>
14 /* Copies a block of memory to a device in an efficient manner.
15 * Assumes the device can cope with 32-bit transfers. If it can't,
16 * don't use this function.
18 void memcpy_toio(volatile void __iomem *dst, const void *src, int count)
20 if (((unsigned long)dst & 3) != ((unsigned long)src & 3))
22 while ((unsigned long)dst & 3) {
23 writeb(*(char *)src, dst++);
28 __raw_writel(*(u32 *)src, dst);
35 writeb(*(char *)src, dst++);
41 ** Copies a block of memory from a device in an efficient manner.
42 ** Assumes the device can cope with 32-bit transfers. If it can't,
43 ** don't use this function.
45 ** CR16 counts on C3000 reading 256 bytes from Symbios 896 RAM:
46 ** 27341/64 = 427 cyc per int
47 ** 61311/128 = 478 cyc per short
48 ** 122637/256 = 479 cyc per byte
49 ** Ergo bus latencies dominant (not transfer size).
50 ** Minimize total number of transfers at cost of CPU cycles.
51 ** TODO: only look at src alignment and adjust the stores to dest.
53 void memcpy_fromio(void *dst, const volatile void __iomem *src, int count)
55 /* first compare alignment of src/dst */
56 if ( (((unsigned long)dst ^ (unsigned long)src) & 1) || (count < 2) )
59 if ( (((unsigned long)dst ^ (unsigned long)src) & 2) || (count < 4) )
62 /* Then check for misaligned start address */
63 if ((unsigned long)src & 1) {
64 *(u8 *)dst = readb(src);
68 if (count < 2) goto bytecopy;
71 if ((unsigned long)src & 2) {
72 *(u16 *)dst = __raw_readw(src);
79 *(u32 *)dst = __raw_readl(src);
87 *(u16 *)dst = __raw_readw(src);
95 *(char *)dst = readb(src);
101 /* Sets a block of memory on a device to a given value.
102 * Assumes the device can cope with 32-bit transfers. If it can't,
103 * don't use this function.
105 void memset_io(volatile void __iomem *addr, unsigned char val, int count)
107 u32 val32 = (val << 24) | (val << 16) | (val << 8) | val;
108 while ((unsigned long)addr & 3) {
113 __raw_writel(val32, addr);
123 * Read COUNT 8-bit bytes from port PORT into memory starting at
126 void insb (unsigned long port, void *dst, unsigned long count)
130 p = (unsigned char *)dst;
132 while (((unsigned long)p) & 0x3) {
144 w |= inb(port) << 16;
147 *(unsigned int *) p = w;
160 * Read COUNT 16-bit words from port PORT into memory starting at
161 * SRC. SRC must be at least short aligned. This is used by the
162 * IDE driver to read disk sectors. Performance is important, but
163 * the interfaces seems to be slow: just using the inlined version
164 * of the inw() breaks things.
166 void insw (unsigned long port, void *dst, unsigned long count)
168 unsigned int l = 0, l2;
171 p = (unsigned char *)dst;
176 switch (((unsigned long)p) & 0x3)
178 case 0x00: /* Buffer 32-bit aligned */
182 l = cpu_to_le16(inw(port)) << 16;
183 l |= cpu_to_le16(inw(port));
184 *(unsigned int *)p = l;
188 *(unsigned short *)p = cpu_to_le16(inw(port));
192 case 0x02: /* Buffer 16-bit aligned */
193 *(unsigned short *)p = cpu_to_le16(inw(port));
199 l = cpu_to_le16(inw(port)) << 16;
200 l |= cpu_to_le16(inw(port));
201 *(unsigned int *)p = l;
205 *(unsigned short *)p = cpu_to_le16(inw(port));
209 case 0x01: /* Buffer 8-bit aligned */
211 /* I don't bother with 32bit transfers
212 * in this case, 16bit will have to do -- DE */
215 l = cpu_to_le16(inw(port));
220 l2 = cpu_to_le16(inw(port));
221 *(unsigned short *)p = (l & 0xff) << 8 | (l2 >> 8);
233 * Read COUNT 32-bit words from port PORT into memory starting at
234 * SRC. Now works with any alignment in SRC. Performance is important,
235 * but the interfaces seems to be slow: just using the inlined version
236 * of the inl() breaks things.
238 void insl (unsigned long port, void *dst, unsigned long count)
240 unsigned int l = 0, l2;
243 p = (unsigned char *)dst;
248 switch (((unsigned long) dst) & 0x3)
250 case 0x00: /* Buffer 32-bit aligned */
253 *(unsigned int *)p = cpu_to_le32(inl(port));
258 case 0x02: /* Buffer 16-bit aligned */
261 l = cpu_to_le32(inl(port));
262 *(unsigned short *)p = l >> 16;
267 l2 = cpu_to_le32(inl(port));
268 *(unsigned int *)p = (l & 0xffff) << 16 | (l2 >> 16);
272 *(unsigned short *)p = l & 0xffff;
274 case 0x01: /* Buffer 8-bit aligned */
277 l = cpu_to_le32(inl(port));
278 *(unsigned char *)p = l >> 24;
280 *(unsigned short *)p = (l >> 8) & 0xffff;
284 l2 = cpu_to_le32(inl(port));
285 *(unsigned int *)p = (l & 0xff) << 24 | (l2 >> 8);
291 case 0x03: /* Buffer 8-bit aligned */
294 l = cpu_to_le32(inl(port));
299 l2 = cpu_to_le32(inl(port));
300 *(unsigned int *)p = (l & 0xffffff) << 8 | l2 >> 24;
304 *(unsigned short *)p = (l >> 8) & 0xffff;
313 * Like insb but in the opposite direction.
314 * Don't worry as much about doing aligned memory transfers:
315 * doing byte reads the "slow" way isn't nearly as slow as
316 * doing byte writes the slow way (no r-m-w cycle).
318 void outsb(unsigned long port, const void * src, unsigned long count)
320 const unsigned char *p;
322 p = (const unsigned char *)src;
331 * Like insw but in the opposite direction. This is used by the IDE
332 * driver to write disk sectors. Performance is important, but the
333 * interfaces seems to be slow: just using the inlined version of the
334 * outw() breaks things.
336 void outsw (unsigned long port, const void *src, unsigned long count)
338 unsigned int l = 0, l2;
339 const unsigned char *p;
341 p = (const unsigned char *)src;
346 switch (((unsigned long)p) & 0x3)
348 case 0x00: /* Buffer 32-bit aligned */
351 l = *(unsigned int *)p;
353 outw(le16_to_cpu(l >> 16), port);
354 outw(le16_to_cpu(l & 0xffff), port);
357 outw(le16_to_cpu(*(unsigned short*)p), port);
361 case 0x02: /* Buffer 16-bit aligned */
363 outw(le16_to_cpu(*(unsigned short*)p), port);
369 l = *(unsigned int *)p;
371 outw(le16_to_cpu(l >> 16), port);
372 outw(le16_to_cpu(l & 0xffff), port);
375 outw(le16_to_cpu(*(unsigned short *)p), port);
379 case 0x01: /* Buffer 8-bit aligned */
380 /* I don't bother with 32bit transfers
381 * in this case, 16bit will have to do -- DE */
389 l2 = *(unsigned short *)p;
391 outw(le16_to_cpu(l | l2 >> 8), port);
394 l2 = *(unsigned char *)p;
395 outw (le16_to_cpu(l | l2>>8), port);
403 * Like insl but in the opposite direction. This is used by the IDE
404 * driver to write disk sectors. Works with any alignment in SRC.
405 * Performance is important, but the interfaces seems to be slow:
406 * just using the inlined version of the outl() breaks things.
408 void outsl (unsigned long port, const void *src, unsigned long count)
410 unsigned int l = 0, l2;
411 const unsigned char *p;
413 p = (const unsigned char *)src;
418 switch (((unsigned long)p) & 0x3)
420 case 0x00: /* Buffer 32-bit aligned */
423 outl(le32_to_cpu(*(unsigned int *)p), port);
428 case 0x02: /* Buffer 16-bit aligned */
431 l = *(unsigned short *)p;
436 l2 = *(unsigned int *)p;
438 outl (le32_to_cpu(l << 16 | l2 >> 16), port);
441 l2 = *(unsigned short *)p;
442 outl (le32_to_cpu(l << 16 | l2), port);
444 case 0x01: /* Buffer 8-bit aligned */
449 l |= *(unsigned short *)p << 8;
454 l2 = *(unsigned int *)p;
456 outl (le32_to_cpu(l | l2 >> 24), port);
460 outl (le32_to_cpu(l | l2), port);
462 case 0x03: /* Buffer 8-bit aligned */
470 l2 = *(unsigned int *)p;
472 outl (le32_to_cpu(l | l2 >> 8), port);
475 l2 = *(unsigned short *)p << 16;
478 outl (le32_to_cpu(l | l2), port);
486 EXPORT_SYMBOL(outsb);
487 EXPORT_SYMBOL(outsw);
488 EXPORT_SYMBOL(outsl);