OSDN Git Service

Clean up 1st round.
[uclinux-h8/linux.git] / arch / h8300 / include / asm / io.h
1 #ifndef _H8300_IO_H
2 #define _H8300_IO_H
3
4 #ifdef __KERNEL__
5
6 /*
7  * These are for ISA/PCI shared memory _only_ and should never be used
8  * on any other type of memory, including Zorro memory. They are meant to
9  * access the bus in the bus byte order which is little-endian!.
10  *
11  * readX/writeX() are used to access memory mapped devices. On some
12  * architectures the memory mapped IO stuff needs to be accessed
13  * differently. On the m68k architecture, we just read/write the
14  * memory location directly.
15  */
16 /* ++roman: The assignments to temp. vars avoid that gcc sometimes generates
17  * two accesses to memory, which may be undesirable for some devices.
18  */
19
20 /*
21  * swap functions are sometimes needed to interface little-endian hardware
22  */
23
24 static inline unsigned short _swapw(volatile unsigned short v)
25 {
26 #ifndef H8300_IO_NOSWAP
27         unsigned short r;
28
29         __asm__("xor.b %w0,%x0\n\t"
30                 "xor.b %x0,%w0\n\t"
31                 "xor.b %w0,%x0"
32                 : "=r"(r)
33                 : "0"(v));
34         return r;
35 #else
36         return v;
37 #endif
38 }
39
40 static inline unsigned long _swapl(volatile unsigned long v)
41 {
42 #ifndef H8300_IO_NOSWAP
43         unsigned long r;
44
45         __asm__("xor.b %w0,%x0\n\t"
46                 "xor.b %x0,%w0\n\t"
47                 "xor.b %w0,%x0\n\t"
48                 "xor.w %e0,%f0\n\t"
49                 "xor.w %f0,%e0\n\t"
50                 "xor.w %e0,%f0\n\t"
51                 "xor.b %w0,%x0\n\t"
52                 "xor.b %x0,%w0\n\t"
53                 "xor.b %w0,%x0"
54                 : "=r"(r)
55                 : "0"(v));
56         return r;
57 #else
58         return v;
59 #endif
60 }
61
62 #define readb(addr) \
63         ({ u8 __v = *(volatile u8 *)((uintptr_t)(addr) & 0x00ffffff); __v; })
64
65 #define readw(addr) \
66         ({ u16 __v = *(volatile u16 *)((uintptr_t)(addr) & 0x00ffffff); __v; })
67
68 #define readl(addr) \
69         ({ u32 __v = *(volatile u32 *)((uintptr_t)(addr) & 0x00ffffff); __v; })
70
71 #define writeb(b, addr) (void)((*(volatile u8 *) \
72                                 ((uintptr_t)(addr) & 0x00ffffff)) = (b))
73
74 #define writew(b, addr) (void)((*(volatile u16 *) \
75                                 ((uintptr_t)(addr) & 0x00ffffff)) = (b))
76
77 #define writel(b, addr) (void)((*(volatile u32 *)                       \
78                                 ((uintptr_t)(addr) & 0x00ffffff)) = (b))
79
80 #define readb_relaxed(addr) readb(addr)
81 #define readw_relaxed(addr) readw(addr)
82 #define readl_relaxed(addr) readl(addr)
83 #define writeb_relaxed(b, addr) writeb(b, addr)
84 #define writew_relaxed(b, addr) writew(b, addr)
85 #define writel_relaxed(b, addr) writel(b, addr)
86
87 #define __raw_readb readb
88 #define __raw_readw readw
89 #define __raw_readl readl
90 #define __raw_writeb writeb
91 #define __raw_writew writew
92 #define __raw_writel writel
93
94 #if defined(CONFIG_H83069)
95 #define ABWCR  0xFEE020
96 #elif defined(CONFIG_H8S2678)
97 #define ABWCR  0xFFFEC0
98 #endif
99
100 static inline int h8300_buswidth(unsigned int addr)
101 {
102         return (*(volatile u8 *)ABWCR & (1 << ((addr >> 21) & 7))) == 0;
103 }
104
105 static inline void io_outsb(unsigned int addr, const void *buf, int len)
106 {
107         volatile unsigned char  *ap_b = (volatile unsigned char *) addr;
108         volatile unsigned short *ap_w = (volatile unsigned short *) addr;
109         unsigned char *bp = (unsigned char *) buf;
110
111         if (h8300_buswidth(addr) && (addr & 1)) {
112                 while (len--)
113                         *ap_w = *bp++;
114         } else {
115                 while (len--)
116                         *ap_b = *bp++;
117         }
118 }
119
120 static inline void io_outsw(unsigned int addr, const void *buf, int len)
121 {
122         volatile unsigned short *ap = (volatile unsigned short *) addr;
123         unsigned short *bp = (unsigned short *) buf;
124
125         while (len--)
126                 *ap = _swapw(*bp++);
127 }
128
129 static inline void io_outsl(unsigned int addr, const void *buf, int len)
130 {
131         volatile unsigned long *ap = (volatile unsigned long *) addr;
132         unsigned long *bp = (unsigned long *) buf;
133
134         while (len--)
135                 *ap = _swapl(*bp++);
136 }
137
138 static inline void io_outsw_noswap(unsigned int addr, const void *buf, int len)
139 {
140         volatile unsigned short *ap = (volatile unsigned short *) addr;
141         unsigned short *bp = (unsigned short *) buf;
142
143         while (len--)
144                 *ap = *bp++;
145 }
146
147 static inline void io_outsl_noswap(unsigned int addr, const void *buf, int len)
148 {
149         volatile unsigned long *ap = (volatile unsigned long *) addr;
150         unsigned long *bp = (unsigned long *) buf;
151
152         while (len--)
153                 *ap = *bp++;
154 }
155
156 static inline void io_insb(unsigned int addr, void *buf, int len)
157 {
158         volatile unsigned char  *ap_b;
159         volatile unsigned short *ap_w;
160         unsigned char *bp = (unsigned char *) buf;
161
162         if (h8300_buswidth(addr)) {
163                 ap_w = (volatile unsigned short *)(addr & ~1);
164                 while (len--)
165                         *bp++ = *ap_w & 0xff;
166         } else {
167                 ap_b = (volatile unsigned char *)addr;
168                 while (len--)
169                         *bp++ = *ap_b;
170         }
171 }
172
173 static inline void io_insw(unsigned int addr, void *buf, int len)
174 {
175         volatile unsigned short *ap = (volatile unsigned short *) addr;
176         unsigned short *bp = (unsigned short *) buf;
177
178         while (len--)
179                 *bp++ = _swapw(*ap);
180 }
181
182 static inline void io_insl(unsigned int addr, void *buf, int len)
183 {
184         volatile unsigned long *ap = (volatile unsigned long *) addr;
185         unsigned long *bp = (unsigned long *) buf;
186
187         while (len--)
188                 *bp++ = _swapl(*ap);
189 }
190
191 static inline void io_insw_noswap(unsigned int addr, void *buf, int len)
192 {
193         volatile unsigned short *ap = (volatile unsigned short *) addr;
194         unsigned short *bp = (unsigned short *) buf;
195
196         while (len--)
197                 *bp++ = *ap;
198 }
199
200 static inline void io_insl_noswap(unsigned int addr, void *buf, int len)
201 {
202         volatile unsigned long *ap = (volatile unsigned long *) addr;
203         unsigned long *bp = (unsigned long *) buf;
204
205         while (len--)
206                 *bp++ = *ap;
207 }
208
209 /*
210  *      make the short names macros so specific devices
211  *      can override them as required
212  */
213
214 #define memset_io(a, b, c)      memset((void *)(a), (b), (c))
215 #define memcpy_fromio(a, b, c)  memcpy((a), (void *)(b), (c))
216 #define memcpy_toio(a, b, c)    memcpy((void *)(a), (b), (c))
217
218 #define mmiowb()
219
220 #define inb(addr)    ((h8300_buswidth(addr)) ? \
221                       readw((addr) & ~1) & 0xff:readb(addr))
222 #define inw(addr)    _swapw(readw(addr))
223 #define inl(addr)    _swapl(readl(addr))
224 #define outb(x, addr) ((void)((h8300_buswidth(addr) && \
225                                ((addr) & 1)) ? \
226                               writew(x, (addr) & ~1) : writeb(x, addr)))
227 #define outw(x, addr) ((void) writew(_swapw(x), addr))
228 #define outl(x, addr) ((void) writel(_swapl(x), addr))
229
230 #define inb_p(addr)    inb(addr)
231 #define inw_p(addr)    inw(addr)
232 #define inl_p(addr)    inl(addr)
233 #define outb_p(x, addr) outb(x, addr)
234 #define outw_p(x, addr) outw(x, addr)
235 #define outl_p(x, addr) outl(x, addr)
236
237 #define outsb(a, b, l) io_outsb(a, b, l)
238 #define outsw(a, b, l) io_outsw(a, b, l)
239 #define outsl(a, b, l) io_outsl(a, b, l)
240
241 #define insb(a, b, l) io_insb(a, b, l)
242 #define insw(a, b, l) io_insw(a, b, l)
243 #define insl(a, b, l) io_insl(a, b, l)
244
245 #define ioread8(a)              __raw_readb(a)
246 #define ioread16(a)             __raw_readw(a)
247 #define ioread32(a)             __raw_readl(a)
248
249 #define iowrite8(v, a)          __raw_writeb((v), (a))
250 #define iowrite16(v, a)         __raw_writew((v), (a))
251 #define iowrite32(v, a)         __raw_writel((v), (a))
252 #define IO_SPACE_LIMIT 0xffffff
253
254
255 /* Values for nocacheflag and cmode */
256 #define IOMAP_FULL_CACHING              0
257 #define IOMAP_NOCACHE_SER               1
258 #define IOMAP_NOCACHE_NONSER            2
259 #define IOMAP_WRITETHROUGH              3
260
261 extern void *__ioremap(unsigned long physaddr, unsigned long size,
262                        int cacheflag);
263 extern void __iounmap(void *addr, unsigned long size);
264
265 static inline void *ioremap(unsigned long physaddr, unsigned long size)
266 {
267         return __ioremap(physaddr, size, IOMAP_NOCACHE_SER);
268 }
269 static inline void *ioremap_nocache(unsigned long physaddr,
270                                     unsigned long size)
271 {
272         return __ioremap(physaddr, size, IOMAP_NOCACHE_SER);
273 }
274 static inline void *ioremap_writethrough(unsigned long physaddr,
275                                          unsigned long size)
276 {
277         return __ioremap(physaddr, size, IOMAP_WRITETHROUGH);
278 }
279 static inline void *ioremap_fullcache(unsigned long physaddr,
280                                       unsigned long size)
281 {
282         return __ioremap(physaddr, size, IOMAP_FULL_CACHING);
283 }
284
285 extern void iounmap(void *addr);
286
287 /* H8/300 internal I/O functions */
288 static inline unsigned char ctrl_inb(unsigned long addr)
289 {
290         return *(volatile unsigned char *)addr;
291 }
292
293 static inline unsigned short ctrl_inw(unsigned long addr)
294 {
295         return *(volatile unsigned short *)addr;
296 }
297
298 static inline unsigned long ctrl_inl(unsigned long addr)
299 {
300         return *(volatile unsigned long *)addr;
301 }
302
303 static inline void ctrl_outb(unsigned char b, unsigned long addr)
304 {
305         *(volatile unsigned char *)addr = b;
306 }
307
308 static inline void ctrl_outw(unsigned short b, unsigned long addr)
309 {
310         *(volatile unsigned short *)addr = b;
311 }
312
313 static inline void ctrl_outl(unsigned long b, unsigned long addr)
314 {
315         *(volatile unsigned long *)addr = b;
316 }
317
318 static inline void ctrl_bclr(int b, unsigned long addr)
319 {
320         if (__builtin_constant_p(b))
321                 switch (b) {
322                 case 0: __asm__("bclr #0,@%0" : : "m"(addr)); break;
323                 case 1: __asm__("bclr #1,@%0" : : "m"(addr)); break;
324                 case 2: __asm__("bclr #2,@%0" : : "m"(addr)); break;
325                 case 3: __asm__("bclr #3,@%0" : : "m"(addr)); break;
326                 case 4: __asm__("bclr #4,@%0" : : "m"(addr)); break;
327                 case 5: __asm__("bclr #5,@%0" : : "m"(addr)); break;
328                 case 6: __asm__("bclr #6,@%0" : : "m"(addr)); break;
329                 case 7: __asm__("bclr #7,@%0" : : "m"(addr)); break;
330                 }
331         else
332                 __asm__("bclr %w0,@%1" : : "r"(b), "m"(addr));
333 }
334
335 static inline void ctrl_bset(int b, unsigned long addr)
336 {
337         if (__builtin_constant_p(b))
338                 switch (b) {
339                 case 0: __asm__("bset #0,@%0" : : "m"(addr)); break;
340                 case 1: __asm__("bset #1,@%0" : : "m"(addr)); break;
341                 case 2: __asm__("bset #2,@%0" : : "m"(addr)); break;
342                 case 3: __asm__("bset #3,@%0" : : "m"(addr)); break;
343                 case 4: __asm__("bset #4,@%0" : : "m"(addr)); break;
344                 case 5: __asm__("bset #5,@%0" : : "m"(addr)); break;
345                 case 6: __asm__("bset #6,@%0" : : "m"(addr)); break;
346                 case 7: __asm__("bset #7,@%0" : : "m"(addr)); break;
347                 }
348         else
349                 __asm__("bset %w0,@%1" : : "r"(b), "m"(addr));
350 }
351
352 /*
353  * Macros used for converting between virtual and physical mappings.
354  */
355 #define phys_to_virt(vaddr)     ((void *) (vaddr))
356 #define virt_to_phys(vaddr)     ((unsigned long) (vaddr))
357
358 #define virt_to_bus virt_to_phys
359 #define bus_to_virt phys_to_virt
360 /*
361  * Convert a physical pointer to a virtual kernel pointer for /dev/mem
362  * access
363  */
364 #define xlate_dev_mem_ptr(p)    __va(p)
365
366 /*
367  * Convert a virtual cached pointer to an uncached pointer
368  */
369 #define xlate_dev_kmem_ptr(p)   (p)
370
371 #endif /* __KERNEL__ */
372
373 #endif /* _H8300_IO_H */