blob: 6529f86575976e39f69ba1d7e427a7bd4c0a158d [file] [log] [blame]
Adrian Bunkb00dc832008-05-19 16:52:27 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * PeeCeeI.c: The emerging standard...
3 *
4 * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
5 */
6
Sam Ravnborg917c3662009-01-08 16:58:20 -08007#include <linux/module.h>
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/io.h>
10#include <asm/byteorder.h>
11
David S. Miller8a368952005-08-31 15:01:33 -070012void outsb(unsigned long __addr, const void *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070013{
David S. Miller8a368952005-08-31 15:01:33 -070014 void __iomem *addr = (void __iomem *) __addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 const u8 *p = src;
16
David S. Miller8a368952005-08-31 15:01:33 -070017 while (count--)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 outb(*p++, addr);
19}
Sam Ravnborg917c3662009-01-08 16:58:20 -080020EXPORT_SYMBOL(outsb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
David S. Miller8a368952005-08-31 15:01:33 -070022void outsw(unsigned long __addr, const void *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
David S. Miller8a368952005-08-31 15:01:33 -070024 void __iomem *addr = (void __iomem *) __addr;
25
Harvey Harrison1758ef62008-10-29 15:33:28 -070026 while (count--) {
27 __raw_writew(*(u16 *)src, addr);
28 src += sizeof(u16);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 }
30}
Sam Ravnborg917c3662009-01-08 16:58:20 -080031EXPORT_SYMBOL(outsw);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
David S. Miller8a368952005-08-31 15:01:33 -070033void outsl(unsigned long __addr, const void *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
David S. Miller8a368952005-08-31 15:01:33 -070035 void __iomem *addr = (void __iomem *) __addr;
Harvey Harrison1758ef62008-10-29 15:33:28 -070036 u32 l, l2;
David S. Miller8a368952005-08-31 15:01:33 -070037
Harvey Harrison1758ef62008-10-29 15:33:28 -070038 if (!count)
39 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Harvey Harrison1758ef62008-10-29 15:33:28 -070041 switch (((unsigned long)src) & 0x3) {
42 case 0x0:
43 /* src is naturally aligned */
44 while (count--) {
45 __raw_writel(*(u32 *)src, addr);
46 src += sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
Harvey Harrison1758ef62008-10-29 15:33:28 -070048 break;
49 case 0x2:
50 /* 2-byte alignment */
51 while (count--) {
52 l = (*(u16 *)src) << 16;
53 l |= *(u16 *)(src + sizeof(u16));
54 __raw_writel(l, addr);
55 src += sizeof(u32);
56 }
57 break;
58 case 0x1:
59 /* Hold three bytes in l each time, grab a byte from l2 */
60 l = (*(u8 *)src) << 24;
61 l |= (*(u16 *)(src + sizeof(u8))) << 8;
62 src += sizeof(u8) + sizeof(u16);
63 while (count--) {
64 l2 = *(u32 *)src;
65 l |= (l2 >> 24);
66 __raw_writel(l, addr);
67 l = l2 << 8;
68 src += sizeof(u32);
69 }
70 break;
71 case 0x3:
72 /* Hold a byte in l each time, grab 3 bytes from l2 */
73 l = (*(u8 *)src) << 24;
74 src += sizeof(u8);
75 while (count--) {
76 l2 = *(u32 *)src;
77 l |= (l2 >> 8);
78 __raw_writel(l, addr);
79 l = l2 << 24;
80 src += sizeof(u32);
81 }
82 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84}
Sam Ravnborg917c3662009-01-08 16:58:20 -080085EXPORT_SYMBOL(outsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
David S. Miller8a368952005-08-31 15:01:33 -070087void insb(unsigned long __addr, void *dst, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
David S. Miller8a368952005-08-31 15:01:33 -070089 void __iomem *addr = (void __iomem *) __addr;
90
91 if (count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 u32 *pi;
93 u8 *pb = dst;
94
David S. Miller8a368952005-08-31 15:01:33 -070095 while ((((unsigned long)pb) & 0x3) && count--)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *pb++ = inb(addr);
97 pi = (u32 *)pb;
David S. Miller8a368952005-08-31 15:01:33 -070098 while (count >= 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 u32 w;
100
101 w = (inb(addr) << 24);
102 w |= (inb(addr) << 16);
103 w |= (inb(addr) << 8);
104 w |= (inb(addr) << 0);
105 *pi++ = w;
106 count -= 4;
107 }
108 pb = (u8 *)pi;
David S. Miller8a368952005-08-31 15:01:33 -0700109 while (count--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *pb++ = inb(addr);
111 }
112}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800113EXPORT_SYMBOL(insb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
David S. Miller8a368952005-08-31 15:01:33 -0700115void insw(unsigned long __addr, void *dst, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
David S. Miller8a368952005-08-31 15:01:33 -0700117 void __iomem *addr = (void __iomem *) __addr;
118
119 if (count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 u16 *ps = dst;
121 u32 *pi;
122
David S. Miller8a368952005-08-31 15:01:33 -0700123 if (((unsigned long)ps) & 0x2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 *ps++ = le16_to_cpu(inw(addr));
125 count--;
126 }
127 pi = (u32 *)ps;
David S. Miller8a368952005-08-31 15:01:33 -0700128 while (count >= 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 u32 w;
130
131 w = (le16_to_cpu(inw(addr)) << 16);
132 w |= (le16_to_cpu(inw(addr)) << 0);
133 *pi++ = w;
134 count -= 2;
135 }
136 ps = (u16 *)pi;
David S. Miller8a368952005-08-31 15:01:33 -0700137 if (count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 *ps = le16_to_cpu(inw(addr));
139 }
140}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800141EXPORT_SYMBOL(insw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
David S. Miller8a368952005-08-31 15:01:33 -0700143void insl(unsigned long __addr, void *dst, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
David S. Miller8a368952005-08-31 15:01:33 -0700145 void __iomem *addr = (void __iomem *) __addr;
146
147 if (count) {
148 if ((((unsigned long)dst) & 0x3) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 u32 *pi = dst;
David S. Miller8a368952005-08-31 15:01:33 -0700150 while (count--)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 *pi++ = le32_to_cpu(inl(addr));
152 } else {
153 u32 l = 0, l2, *pi;
154 u16 *ps;
155 u8 *pb;
156
David S. Miller8a368952005-08-31 15:01:33 -0700157 switch (((unsigned long)dst) & 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 case 0x2:
159 ps = dst;
160 count -= 1;
161 l = le32_to_cpu(inl(addr));
162 *ps++ = l;
163 pi = (u32 *)ps;
David S. Miller8a368952005-08-31 15:01:33 -0700164 while (count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 l2 = le32_to_cpu(inl(addr));
166 *pi++ = (l << 16) | (l2 >> 16);
167 l = l2;
168 }
169 ps = (u16 *)pi;
170 *ps = l;
171 break;
172
173 case 0x1:
174 pb = dst;
175 count -= 1;
176 l = le32_to_cpu(inl(addr));
177 *pb++ = l >> 24;
178 ps = (u16 *)pb;
179 *ps++ = ((l >> 8) & 0xffff);
180 pi = (u32 *)ps;
David S. Miller8a368952005-08-31 15:01:33 -0700181 while (count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 l2 = le32_to_cpu(inl(addr));
183 *pi++ = (l << 24) | (l2 >> 8);
184 l = l2;
185 }
186 pb = (u8 *)pi;
187 *pb = l;
188 break;
189
190 case 0x3:
191 pb = (u8 *)dst;
192 count -= 1;
193 l = le32_to_cpu(inl(addr));
194 *pb++ = l >> 24;
195 pi = (u32 *)pb;
David S. Miller8a368952005-08-31 15:01:33 -0700196 while (count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 l2 = le32_to_cpu(inl(addr));
198 *pi++ = (l << 8) | (l2 >> 24);
199 l = l2;
200 }
201 ps = (u16 *)pi;
202 *ps++ = ((l >> 8) & 0xffff);
203 pb = (u8 *)ps;
204 *pb = l;
205 break;
206 }
207 }
208 }
209}
Sam Ravnborg917c3662009-01-08 16:58:20 -0800210EXPORT_SYMBOL(insl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211