This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / sh64 / lib / io.c
1 /*
2  * Copyright (C) 2000 David J. Mckay (david.mckay@st.com)
3  *
4  * May be copied or modified under the terms of the GNU General Public
5  * License.  See linux/COPYING for more information.
6  *
7  * This file contains the I/O routines for use on the overdrive board
8  *
9  */
10
11 #include <linux/config.h>
12 #include <linux/types.h>
13 #include <linux/delay.h>
14 #include <asm/system.h>
15 #include <asm/processor.h>
16 #include <asm/io.h>
17 #ifdef CONFIG_SH_CAYMAN
18 #include <asm/cayman.h>
19 #endif
20
21 /*
22  * readX/writeX() are used to access memory mapped devices. On some
23  * architectures the memory mapped IO stuff needs to be accessed
24  * differently. On the SuperH architecture, we just read/write the
25  * memory location directly.
26  */
27
28 #define dprintk(x...)
29
30 static int io_addr(int x) {
31         if (x < 0x400) {
32 #ifdef CONFIG_SH_CAYMAN
33                 return (x << 2) | smsc_superio_virt;
34 #else
35                 panic ("Illegal access to I/O port 0x%04x\n", x);
36                 return 0;
37 #endif
38         } else {
39 #ifdef CONFIG_PCI
40                 return (x + pciio_virt);
41 #else
42                 panic ("Illegal access to I/O port 0x%04x\n", x);
43                 return 0;
44 #endif
45         }
46 }
47
48 unsigned long inb(unsigned long port)
49 {
50         unsigned long r;
51
52         r = ctrl_inb(io_addr(port));
53         dprintk("inb(0x%x)=0x%x (0x%x)\n", port, r, io_addr(port));
54         return r;
55 }
56
57 unsigned long inw(unsigned long port)
58 {
59         unsigned long r;
60
61         r = ctrl_inw(io_addr(port));
62         dprintk("inw(0x%x)=0x%x (0x%x)\n", port, r, io_addr(port));
63         return r;
64 }
65
66 unsigned long inl(unsigned long port)
67 {
68         unsigned long r;
69
70         r = ctrl_inl(io_addr(port));
71         dprintk("inl(0x%x)=0x%x (0x%x)\n", port, r, io_addr(port));
72         return r;
73 }
74
75 void outb(unsigned long value, unsigned long port)
76 {
77         dprintk("outb(0x%x,0x%x) (0x%x)\n", value, port, io_addr(port));
78         ctrl_outb(value, io_addr(port));
79 }
80
81 void outw(unsigned long value, unsigned long port)
82 {
83         dprintk("outw(0x%x,0x%x) (0x%x)\n", value, port, io_addr(port));
84         ctrl_outw(value, io_addr(port));
85 }
86
87 void outl(unsigned long value, unsigned long port)
88 {
89         dprintk("outw(0x%x,0x%x) (0x%x)\n", value, port, io_addr(port));
90         ctrl_outl(value, io_addr(port));
91 }
92
93 /* This is horrible at the moment - needs more work to do something sensible */
94 #define IO_DELAY()
95
96 #define OUT_DELAY(x,type) \
97 void out##x##_p(unsigned type value,unsigned long port){out##x(value,port);IO_DELAY();}
98
99 #define IN_DELAY(x,type) \
100 unsigned type in##x##_p(unsigned long port) {unsigned type tmp=in##x(port);IO_DELAY();return tmp;}
101
102 #if 1
103 OUT_DELAY(b, long) OUT_DELAY(w, long) OUT_DELAY(l, long)
104  IN_DELAY(b, long) IN_DELAY(w, long) IN_DELAY(l, long)
105 #endif
106 /*  Now for the string version of these functions */
107 void outsb(unsigned long port, const void *addr, unsigned long count)
108 {
109         int i;
110         unsigned char *p = (unsigned char *) addr;
111
112         for (i = 0; i < count; i++, p++) {
113                 outb(*p, port);
114         }
115 }
116
117 void insb(unsigned long port, void *addr, unsigned long count)
118 {
119         int i;
120         unsigned char *p = (unsigned char *) addr;
121
122         for (i = 0; i < count; i++, p++) {
123                 *p = inb(port);
124         }
125 }
126
127 /* For the 16 and 32 bit string functions, we have to worry about alignment.
128  * The SH does not do unaligned accesses, so we have to read as bytes and
129  * then write as a word or dword.
130  * This can be optimised a lot more, especially in the case where the data
131  * is aligned
132  */
133
134 void outsw(unsigned long port, const void *addr, unsigned long count)
135 {
136         int i;
137         unsigned short tmp;
138         unsigned char *p = (unsigned char *) addr;
139
140         for (i = 0; i < count; i++, p += 2) {
141                 tmp = (*p) | ((*(p + 1)) << 8);
142                 outw(tmp, port);
143         }
144 }
145
146 void insw(unsigned long port, void *addr, unsigned long count)
147 {
148         int i;
149         unsigned short tmp;
150         unsigned char *p = (unsigned char *) addr;
151
152         for (i = 0; i < count; i++, p += 2) {
153                 tmp = inw(port);
154                 p[0] = tmp & 0xff;
155                 p[1] = (tmp >> 8) & 0xff;
156         }
157 }
158
159 void outsl(unsigned long port, const void *addr, unsigned long count)
160 {
161         int i;
162         unsigned tmp;
163         unsigned char *p = (unsigned char *) addr;
164
165         for (i = 0; i < count; i++, p += 4) {
166                 tmp = (*p) | ((*(p + 1)) << 8) | ((*(p + 2)) << 16) |
167                     ((*(p + 3)) << 24);
168                 outl(tmp, port);
169         }
170 }
171
172 void insl(unsigned long port, void *addr, unsigned long count)
173 {
174         int i;
175         unsigned tmp;
176         unsigned char *p = (unsigned char *) addr;
177
178         for (i = 0; i < count; i++, p += 4) {
179                 tmp = inl(port);
180                 p[0] = tmp & 0xff;
181                 p[1] = (tmp >> 8) & 0xff;
182                 p[2] = (tmp >> 16) & 0xff;
183                 p[3] = (tmp >> 24) & 0xff;
184
185         }
186 }
187
188 void memcpy_toio(unsigned long to, const void *from, long count)
189 {
190         unsigned char *p = (unsigned char *) from;
191
192         while (count) {
193                 count--;
194                 writeb(*p++, to++);
195         }
196 }
197
198 void memcpy_fromio(void *to, unsigned long from, long count)
199 {
200         int i;
201         unsigned char *p = (unsigned char *) to;
202
203         for (i = 0; i < count; i++) {
204                 p[i] = readb(from);
205                 from++;
206         }
207 }