5c03504bf65375646880da8f8eadd973fabbbf24
[pandora-kernel.git] / arch / m32r / kernel / io_mappi2.c
1 /*
2  *  linux/arch/m32r/kernel/io_mappi2.c
3  *
4  *  Typical I/O routines for Mappi2 board.
5  *
6  *  Copyright (c) 2001-2005  Hiroyuki Kondo, Hirokazu Takata,
7  *                           Hitoshi Yamamoto, Mamoru Sakugawa
8  */
9
10 #include <linux/config.h>
11 #include <asm/m32r.h>
12 #include <asm/page.h>
13 #include <asm/io.h>
14 #include <asm/byteorder.h>
15
16 #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
17 #include <linux/types.h>
18
19 #define M32R_PCC_IOMAP_SIZE 0x1000
20
21 #define M32R_PCC_IOSTART0 0x1000
22 #define M32R_PCC_IOEND0   (M32R_PCC_IOSTART0 + M32R_PCC_IOMAP_SIZE - 1)
23
24 extern void pcc_ioread_byte(int, unsigned long, void *, size_t, size_t, int);
25 extern void pcc_ioread_word(int, unsigned long, void *, size_t, size_t, int);
26 extern void pcc_iowrite_byte(int, unsigned long, void *, size_t, size_t, int);
27 extern void pcc_iowrite_word(int, unsigned long, void *, size_t, size_t, int);
28 #endif /* CONFIG_PCMCIA && CONFIG_M32R_CFC */
29
30 #define PORT2ADDR(port)      _port2addr(port)
31 #define PORT2ADDR_NE(port)   _port2addr_ne(port)
32 #define PORT2ADDR_USB(port)  _port2addr_usb(port)
33
34 static inline void *_port2addr(unsigned long port)
35 {
36         return (void *)(port + NONCACHE_OFFSET);
37 }
38
39 #define LAN_IOSTART     0x300
40 #define LAN_IOEND       0x320
41
42 #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
43 static inline void *__port2addr_ata(unsigned long port)
44 {
45         static int      dummy_reg;
46
47         switch (port) {
48         case 0x1f0:     return (void *)0xac002000;
49         case 0x1f1:     return (void *)0xac012800;
50         case 0x1f2:     return (void *)0xac012002;
51         case 0x1f3:     return (void *)0xac012802;
52         case 0x1f4:     return (void *)0xac012004;
53         case 0x1f5:     return (void *)0xac012804;
54         case 0x1f6:     return (void *)0xac012006;
55         case 0x1f7:     return (void *)0xac012806;
56         case 0x3f6:     return (void *)0xac01200e;
57         default:        return (void *)&dummy_reg;
58         }
59 }
60 #endif
61
62 #ifdef CONFIG_CHIP_OPSP
63 static inline void *_port2addr_ne(unsigned long port)
64 {
65         return (void *)(port + NONCACHE_OFFSET + 0x10000000);
66 }
67 #else
68 static inline void *_port2addr_ne(unsigned long port)
69 {
70         return (void *)(port + NONCACHE_OFFSET + 0x04000000);
71 }
72 #endif
73 static inline void *_port2addr_usb(unsigned long port)
74 {
75         return (void *)(port + NONCACHE_OFFSET + 0x14000000);
76 }
77 static inline void delay(void)
78 {
79         __asm__ __volatile__ ("push r0; \n\t pop r0;" : : :"memory");
80 }
81
82 /*
83  * NIC I/O function
84  */
85
86 static inline unsigned char _ne_inb(void *portp)
87 {
88         return (unsigned char) *(volatile unsigned char *)portp;
89 }
90
91 static inline unsigned short _ne_inw(void *portp)
92 {
93         return (unsigned short)le16_to_cpu(*(volatile unsigned short *)portp);
94 }
95
96 static inline void _ne_insb(void *portp, void * addr, unsigned long count)
97 {
98         unsigned char *buf = addr;
99
100         while (count--)
101                 *buf++ = *(volatile unsigned char *)portp;
102 }
103
104 static inline void _ne_outb(unsigned char b, void *portp)
105 {
106         *(volatile unsigned char *)portp = (unsigned char)b;
107 }
108
109 static inline void _ne_outw(unsigned short w, void *portp)
110 {
111         *(volatile unsigned short *)portp = cpu_to_le16(w);
112 }
113
114 unsigned char _inb(unsigned long port)
115 {
116         if (port >= LAN_IOSTART && port < LAN_IOEND)
117                 return _ne_inb(PORT2ADDR_NE(port));
118 #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
119         else if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
120                 return *(volatile unsigned char *)__port2addr_ata(port);
121         }
122 #endif
123 #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
124         else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
125                 unsigned char b;
126                 pcc_ioread_byte(0, port, &b, sizeof(b), 1, 0);
127                 return b;
128         } else
129 #endif
130
131         return *(volatile unsigned char *)PORT2ADDR(port);
132 }
133
134 unsigned short _inw(unsigned long port)
135 {
136         if (port >= LAN_IOSTART && port < LAN_IOEND)
137                 return _ne_inw(PORT2ADDR_NE(port));
138 #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
139         else if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
140                 return *(volatile unsigned short *)__port2addr_ata(port);
141         }
142 #endif
143 #if defined(CONFIG_USB)
144         else if (port >= 0x340 && port < 0x3a0)
145                 return *(volatile unsigned short *)PORT2ADDR_USB(port);
146 #endif
147
148 #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
149         else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
150                 unsigned short w;
151                 pcc_ioread_word(0, port, &w, sizeof(w), 1, 0);
152                 return w;
153         } else
154 #endif
155         return *(volatile unsigned short *)PORT2ADDR(port);
156 }
157
158 unsigned long _inl(unsigned long port)
159 {
160 #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
161         if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
162                 unsigned long l;
163                 pcc_ioread_word(0, port, &l, sizeof(l), 1, 0);
164                 return l;
165         } else
166 #endif
167         return *(volatile unsigned long *)PORT2ADDR(port);
168 }
169
170 unsigned char _inb_p(unsigned long port)
171 {
172         unsigned char v = _inb(port);
173         delay();
174         return (v);
175 }
176
177 unsigned short _inw_p(unsigned long port)
178 {
179         unsigned short v = _inw(port);
180         delay();
181         return (v);
182 }
183
184 unsigned long _inl_p(unsigned long port)
185 {
186         unsigned long v = _inl(port);
187         delay();
188         return (v);
189 }
190
191 void _outb(unsigned char b, unsigned long port)
192 {
193         if (port >= LAN_IOSTART && port < LAN_IOEND)
194                 _ne_outb(b, PORT2ADDR_NE(port));
195         else
196 #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
197         if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
198                 *(volatile unsigned char *)__port2addr_ata(port) = b;
199         } else
200 #endif
201 #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
202         if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
203                 pcc_iowrite_byte(0, port, &b, sizeof(b), 1, 0);
204         } else
205 #endif
206                 *(volatile unsigned char *)PORT2ADDR(port) = b;
207 }
208
209 void _outw(unsigned short w, unsigned long port)
210 {
211         if (port >= LAN_IOSTART && port < LAN_IOEND)
212                 _ne_outw(w, PORT2ADDR_NE(port));
213         else
214 #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
215         if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
216                 *(volatile unsigned short *)__port2addr_ata(port) = w;
217         } else
218 #endif
219 #if defined(CONFIG_USB)
220         if (port >= 0x340 && port < 0x3a0)
221                 *(volatile unsigned short *)PORT2ADDR_USB(port) = w;
222         else
223 #endif
224 #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
225         if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
226                 pcc_iowrite_word(0, port, &w, sizeof(w), 1, 0);
227         } else
228 #endif
229                 *(volatile unsigned short *)PORT2ADDR(port) = w;
230 }
231
232 void _outl(unsigned long l, unsigned long port)
233 {
234 #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
235         if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
236                 pcc_iowrite_word(0, port, &l, sizeof(l), 1, 0);
237         } else
238 #endif
239         *(volatile unsigned long *)PORT2ADDR(port) = l;
240 }
241
242 void _outb_p(unsigned char b, unsigned long port)
243 {
244         _outb(b, port);
245         delay();
246 }
247
248 void _outw_p(unsigned short w, unsigned long port)
249 {
250         _outw(w, port);
251         delay();
252 }
253
254 void _outl_p(unsigned long l, unsigned long port)
255 {
256         _outl(l, port);
257         delay();
258 }
259
260 void _insb(unsigned int port, void * addr, unsigned long count)
261 {
262         if (port >= LAN_IOSTART && port < LAN_IOEND)
263                 _ne_insb(PORT2ADDR_NE(port), addr, count);
264 #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
265         else if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
266                 unsigned char *buf = addr;
267                 unsigned char *portp = __port2addr_ata(port);
268                 while (count--)
269                         *buf++ = *(volatile unsigned char *)portp;
270         }
271 #endif
272 #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
273         else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
274                 pcc_ioread_byte(0, port, (void *)addr, sizeof(unsigned char),
275                                 count, 1);
276         }
277 #endif
278         else {
279                 unsigned char *buf = addr;
280                 unsigned char *portp = PORT2ADDR(port);
281                 while (count--)
282                         *buf++ = *(volatile unsigned char *)portp;
283         }
284 }
285
286 void _insw(unsigned int port, void * addr, unsigned long count)
287 {
288         unsigned short *buf = addr;
289         unsigned short *portp;
290
291         if (port >= LAN_IOSTART && port < LAN_IOEND) {
292                 portp = PORT2ADDR_NE(port);
293                 while (count--)
294                         *buf++ = *(volatile unsigned short *)portp;
295 #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
296         } else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
297                 pcc_ioread_word(9, port, (void *)addr, sizeof(unsigned short),
298                                 count, 1);
299 #endif
300 #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
301         } else if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
302                 portp = __port2addr_ata(port);
303                 while (count--)
304                         *buf++ = *(volatile unsigned short *)portp;
305 #endif
306         } else {
307                 portp = PORT2ADDR(port);
308                 while (count--)
309                         *buf++ = *(volatile unsigned short *)portp;
310         }
311 }
312
313 void _insl(unsigned int port, void * addr, unsigned long count)
314 {
315         unsigned long *buf = addr;
316         unsigned long *portp;
317
318         portp = PORT2ADDR(port);
319         while (count--)
320                 *buf++ = *(volatile unsigned long *)portp;
321 }
322
323 void _outsb(unsigned int port, const void * addr, unsigned long count)
324 {
325         const unsigned char *buf = addr;
326         unsigned char *portp;
327
328         if (port >= LAN_IOSTART && port < LAN_IOEND) {
329                 portp = PORT2ADDR_NE(port);
330                 while (count--)
331                         _ne_outb(*buf++, portp);
332 #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
333         } else if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
334                 portp = __port2addr_ata(port);
335                 while (count--)
336                         *(volatile unsigned char *)portp = *buf++;
337 #endif
338 #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
339         } else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
340                 pcc_iowrite_byte(0, port, (void *)addr, sizeof(unsigned char),
341                                  count, 1);
342 #endif
343         } else {
344                 portp = PORT2ADDR(port);
345                 while (count--)
346                         *(volatile unsigned char *)portp = *buf++;
347         }
348 }
349
350 void _outsw(unsigned int port, const void * addr, unsigned long count)
351 {
352         const unsigned short *buf = addr;
353         unsigned short *portp;
354
355         if (port >= LAN_IOSTART && port < LAN_IOEND) {
356                 portp = PORT2ADDR_NE(port);
357                 while (count--)
358                         *(volatile unsigned short *)portp = *buf++;
359 #if defined(CONFIG_IDE) && !defined(CONFIG_M32R_CFC)
360         } else if ((port >= 0x1f0 && port <=0x1f7) || port == 0x3f6) {
361                 portp = __port2addr_ata(port);
362                 while (count--)
363                         *(volatile unsigned short *)portp = *buf++;
364 #endif
365 #if defined(CONFIG_PCMCIA) && defined(CONFIG_M32R_CFC)
366         } else if (port >= M32R_PCC_IOSTART0 && port <= M32R_PCC_IOEND0) {
367                 pcc_iowrite_word(9, port, (void *)addr, sizeof(unsigned short),
368                                  count, 1);
369 #endif
370         } else {
371                 portp = PORT2ADDR(port);
372                 while (count--)
373                         *(volatile unsigned short *)portp = *buf++;
374         }
375 }
376
377 void _outsl(unsigned int port, const void * addr, unsigned long count)
378 {
379         const unsigned long *buf = addr;
380         unsigned char *portp;
381
382         portp = PORT2ADDR(port);
383         while (count--)
384                 *(volatile unsigned long *)portp = *buf++;
385 }