make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / include / asm-mips / io.h
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1994, 1995 Waldorf GmbH
7  * Copyright (C) 1994 - 2000 Ralf Baechle
8  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9  * Copyright (C) 2000 FSMLabs, Inc.
10  */
11 #ifndef _ASM_IO_H
12 #define _ASM_IO_H
13
14 #include <linux/config.h>
15 #include <linux/pagemap.h>
16 #include <linux/types.h>
17 #include <asm/addrspace.h>
18 #include <asm/pgtable-bits.h>
19 #include <asm/byteorder.h>
20
21 #ifdef CONFIG_SGI_IP27
22 extern unsigned long bus_to_baddr[256];
23
24 #define bus_to_baddr(hwdev, addr) (bus_to_baddr[(hwdev)->bus->number] + (addr))
25 #define baddr_to_bus(hwdev, addr) ((addr) - bus_to_baddr[(hwdev)->bus->number])
26 #else
27 #define bus_to_baddr(hwdev, addr) (addr)
28 #define baddr_to_bus(hwdev, addr) (addr)
29 #endif
30
31 /*
32  * Slowdown I/O port space accesses for antique hardware.
33  */
34 #undef CONF_SLOWDOWN_IO
35
36 /*
37  * Sane hardware offers swapping of I/O space accesses in hardware; less
38  * sane hardware forces software to fiddle with this ...
39  */
40 #if defined(CONFIG_SWAP_IO_SPACE) && defined(__MIPSEB__)
41
42 #define __ioswab8(x) (x)
43 #ifdef CONFIG_SGI_IP22
44 /* IP22 seems braindead enough to swap 16bits values in hardware, but
45    not 32bits.  Go figure... Can't tell without documentation. */
46 #define __ioswab16(x) (x)
47 #else
48 #define __ioswab16(x) swab16(x)
49 #endif
50 #define __ioswab32(x) swab32(x)
51
52 #else
53
54 #define __ioswab8(x) (x)
55 #define __ioswab16(x) (x)
56 #define __ioswab32(x) (x)
57
58 #endif
59
60 /*
61  * <Bacchus> Historically I wrote this stuff the same way as Linus did
62  * because I was young and clueless.  And now it's so jucky that I
63  * don't want to put my eyes on it again to get rid of it :-)
64  *
65  * I'll do it then, because this code offends both me and my compiler
66  * - particularly the bits of inline asm which end up doing crap like
67  * 'lb $2,$2($5)' -- dwmw2
68  */
69
70 #define IO_SPACE_LIMIT 0xffff
71
72 /*
73  * On MIPS I/O ports are memory mapped, so we access them using normal
74  * load/store instructions. mips_io_port_base is the virtual address to
75  * which all ports are being mapped.  For sake of efficiency some code
76  * assumes that this is an address that can be loaded with a single lui
77  * instruction, so the lower 16 bits must be zero.  Should be true on
78  * on any sane architecture; generic code does not use this assumption.
79  */
80 extern const unsigned long mips_io_port_base;
81
82 #define set_io_port_base(base)  \
83         do { * (unsigned long *) &mips_io_port_base = (base); } while (0)
84
85 /*
86  * Thanks to James van Artsdalen for a better timing-fix than
87  * the two short jumps: using outb's to a nonexistent port seems
88  * to guarantee better timings even on fast machines.
89  *
90  * On the other hand, I'd like to be sure of a non-existent port:
91  * I feel a bit unsafe about using 0x80 (should be safe, though)
92  *
93  *              Linus
94  *
95  */
96
97 #define __SLOW_DOWN_IO \
98         __asm__ __volatile__( \
99                 "sb\t$0,0x80(%0)" \
100                 : : "r" (mips_io_port_base));
101
102 #ifdef CONF_SLOWDOWN_IO
103 #ifdef REALLY_SLOW_IO
104 #define SLOW_DOWN_IO { __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; }
105 #else
106 #define SLOW_DOWN_IO __SLOW_DOWN_IO
107 #endif
108 #else
109 #define SLOW_DOWN_IO
110 #endif
111
112 /*
113  *     virt_to_phys    -       map virtual addresses to physical
114  *     @address: address to remap
115  *
116  *     The returned physical address is the physical (CPU) mapping for
117  *     the memory address given. It is only valid to use this function on
118  *     addresses directly mapped or allocated via kmalloc.
119  *
120  *     This function does not give bus mappings for DMA transfers. In
121  *     almost all conceivable cases a device driver should not be using
122  *     this function
123  */
124
125 static inline unsigned long virt_to_phys(volatile void * address)
126 {
127         return PHYSADDR(address);
128 }
129
130 /*
131  *     phys_to_virt    -       map physical address to virtual
132  *     @address: address to remap
133  *
134  *     The returned virtual address is a current CPU mapping for
135  *     the memory address given. It is only valid to use this function on
136  *     addresses that have a kernel mapping
137  *
138  *     This function does not handle bus mappings for DMA transfers. In
139  *     almost all conceivable cases a device driver should not be using
140  *     this function
141  */
142
143 static inline void * phys_to_virt(unsigned long address)
144 {
145         return (void *)KSEG0ADDR(address);
146 }
147
148 /*
149  * IO bus memory addresses are also 1:1 with the physical address
150  */
151 static inline unsigned long virt_to_bus(volatile void * address)
152 {
153         return PHYSADDR(address);
154 }
155
156 static inline void * bus_to_virt(unsigned long address)
157 {
158         return (void *)KSEG0ADDR(address);
159 }
160
161 #define page_to_bus page_to_phys
162
163 /*
164  * isa_slot_offset is the address where E(ISA) busaddress 0 is mapped
165  * for the processor.
166  */
167 extern unsigned long isa_slot_offset;
168
169 /*
170  * Change "struct page" to physical address.
171  */
172 #ifdef CONFIG_64BIT_PHYS_ADDR
173 #define page_to_phys(page)      ((u64)(page - mem_map) << PAGE_SHIFT)
174 #else
175 #define page_to_phys(page)      ((page - mem_map) << PAGE_SHIFT)
176 #endif
177
178
179 extern void * __ioremap(phys_t offset, phys_t size, unsigned long flags);
180
181 /*
182  *     ioremap         -       map bus memory into CPU space
183  *     @offset:        bus address of the memory
184  *     @size:          size of the resource to map
185  *
186  *     ioremap performs a platform specific sequence of operations to
187  *     make bus memory CPU accessible via the readb/readw/readl/writeb/
188  *     writew/writel functions and the other mmio helpers. The returned
189  *     address is not guaranteed to be usable directly as a virtual
190  *     address.
191  */
192
193 #define ioremap(offset, size)                                           \
194         __ioremap((offset), (size), _CACHE_UNCACHED)
195
196 /*
197  *     ioremap_nocache         -       map bus memory into CPU space
198  *     @offset:        bus address of the memory
199  *     @size:          size of the resource to map
200  *
201  *     ioremap_nocache performs a platform specific sequence of operations to
202  *     make bus memory CPU accessible via the readb/readw/readl/writeb/
203  *     writew/writel functions and the other mmio helpers. The returned
204  *     address is not guaranteed to be usable directly as a virtual
205  *     address.
206  *
207  *     This version of ioremap ensures that the memory is marked uncachable
208  *     on the CPU as well as honouring existing caching rules from things like
209  *     the PCI bus. Note that there are other caches and buffers on many
210  *     busses. In paticular driver authors should read up on PCI writes
211  *
212  *     It's useful if some control registers are in such an area and
213  *     write combining or read caching is not desirable:
214  */
215 #define ioremap_nocache(offset, size)                                   \
216         __ioremap((offset), (size), _CACHE_UNCACHED)
217 #define ioremap_cacheable_cow(offset, size)                             \
218         __ioremap((offset), (size), _CACHE_CACHABLE_COW)
219 #define ioremap_uncached_accelerated(offset, size)                      \
220         __ioremap((offset), (size), _CACHE_UNCACHED_ACCELERATED)
221
222 extern void iounmap(void *addr);
223
224 /*
225  * XXX We need system specific versions of these to handle EISA address bits
226  * 24-31 on SNI.
227  * XXX more SNI hacks.
228  */
229 #define readb(addr)             (*(volatile unsigned char *)(addr))
230 #define readw(addr)             __ioswab16((*(volatile unsigned short *)(addr)))
231 #define readl(addr)             __ioswab32((*(volatile unsigned int *)(addr)))
232
233 #define __raw_readb(addr)       (*(volatile unsigned char *)(addr))
234 #define __raw_readw(addr)       (*(volatile unsigned short *)(addr))
235 #define __raw_readl(addr)       (*(volatile unsigned int *)(addr))
236
237 #define writeb(b,addr) ((*(volatile unsigned char *)(addr)) = (__ioswab8(b)))
238 #define writew(b,addr) ((*(volatile unsigned short *)(addr)) = (__ioswab16(b)))
239 #define writel(b,addr) ((*(volatile unsigned int *)(addr)) = (__ioswab32(b)))
240
241 #define __raw_writeb(b,addr)    ((*(volatile unsigned char *)(addr)) = (b))
242 #define __raw_writew(w,addr)    ((*(volatile unsigned short *)(addr)) = (w))
243 #define __raw_writel(l,addr)    ((*(volatile unsigned int *)(addr)) = (l))
244
245 #define memset_io(a,b,c)        memset((void *)(a),(b),(c))
246 #define memcpy_fromio(a,b,c)    memcpy((a),(void *)(b),(c))
247 #define memcpy_toio(a,b,c)      memcpy((void *)(a),(b),(c))
248
249 /*
250  * ISA space is 'always mapped' on currently supported MIPS systems, no need
251  * to explicitly ioremap() it. The fact that the ISA IO space is mapped
252  * to PAGE_OFFSET is pure coincidence - it does not mean ISA values
253  * are physical addresses. The following constant pointer can be
254  * used as the IO-area pointer (it can be iounmapped as well, so the
255  * analogy with PCI is quite large):
256  */
257 #define __ISA_IO_base ((char *)(isa_slot_offset))
258
259 #define isa_readb(a) readb(__ISA_IO_base + (a))
260 #define isa_readw(a) readw(__ISA_IO_base + (a))
261 #define isa_readl(a) readl(__ISA_IO_base + (a))
262 #define isa_writeb(b,a) writeb(b,__ISA_IO_base + (a))
263 #define isa_writew(w,a) writew(w,__ISA_IO_base + (a))
264 #define isa_writel(l,a) writel(l,__ISA_IO_base + (a))
265 #define isa_memset_io(a,b,c)            memset_io(__ISA_IO_base + (a),(b),(c))
266 #define isa_memcpy_fromio(a,b,c)        memcpy_fromio((a),__ISA_IO_base + (b),(c))
267 #define isa_memcpy_toio(a,b,c)          memcpy_toio(__ISA_IO_base + (a),(b),(c))
268
269 /*
270  * We don't have csum_partial_copy_fromio() yet, so we cheat here and
271  * just copy it. The net code will then do the checksum later.
272  */
273 #define eth_io_copy_and_sum(skb,src,len,unused) memcpy_fromio((skb)->data,(src),(len))
274 #define isa_eth_io_copy_and_sum(a,b,c,d) eth_copy_and_sum((a),(b),(c),(d))
275
276 /*
277  *     check_signature         -       find BIOS signatures
278  *     @io_addr: mmio address to check
279  *     @signature:  signature block
280  *     @length: length of signature
281  *
282  *     Perform a signature comparison with the mmio address io_addr. This
283  *     address should have been obtained by ioremap.
284  *     Returns 1 on a match.
285  */
286 static inline int check_signature(unsigned long io_addr,
287                                   const unsigned char *signature, int length)
288 {
289         int retval = 0;
290         do {
291                 if (readb(io_addr) != *signature)
292                         goto out;
293                 io_addr++;
294                 signature++;
295                 length--;
296         } while (length);
297         retval = 1;
298 out:
299         return retval;
300 }
301
302 /*
303  *     isa_check_signature             -       find BIOS signatures
304  *     @io_addr: mmio address to check
305  *     @signature:  signature block
306  *     @length: length of signature
307  *
308  *     Perform a signature comparison with the ISA mmio address io_addr.
309  *     Returns 1 on a match.
310  *
311  *     This function is deprecated. New drivers should use ioremap and
312  *     check_signature.
313  */
314
315 static inline int isa_check_signature(unsigned long io_addr,
316         const unsigned char *signature, int length)
317 {
318         int retval = 0;
319         do {
320                 if (isa_readb(io_addr) != *signature)
321                         goto out;
322                 io_addr++;
323                 signature++;
324                 length--;
325         } while (length);
326         retval = 1;
327 out:
328         return retval;
329 }
330
331
332
333
334 #define outb(val,port)                                                  \
335 do {                                                                    \
336         *(volatile u8 *)(mips_io_port_base + (port)) = __ioswab8(val);  \
337 } while(0)
338
339 #define outw(val,port)                                                  \
340 do {                                                                    \
341         *(volatile u16 *)(mips_io_port_base + (port)) = __ioswab16(val);        \
342 } while(0)
343
344 #define outl(val,port)                                                  \
345 do {                                                                    \
346         *(volatile u32 *)(mips_io_port_base + (port)) = __ioswab32(val);\
347 } while(0)
348
349 #define outb_p(val,port)                                                \
350 do {                                                                    \
351         *(volatile u8 *)(mips_io_port_base + (port)) = __ioswab8(val);  \
352         SLOW_DOWN_IO;                                                   \
353 } while(0)
354
355 #define outw_p(val,port)                                                \
356 do {                                                                    \
357         *(volatile u16 *)(mips_io_port_base + (port)) = __ioswab16(val);\
358         SLOW_DOWN_IO;                                                   \
359 } while(0)
360
361 #define outl_p(val,port)                                                \
362 do {                                                                    \
363         *(volatile u32 *)(mips_io_port_base + (port)) = __ioswab32(val);\
364         SLOW_DOWN_IO;                                                   \
365 } while(0)
366
367 #define inb(port) __inb(port)
368 #define inw(port) __inw(port)
369 #define inl(port) __inl(port)
370 #define inb_p(port) __inb_p(port)
371 #define inw_p(port) __inw_p(port)
372 #define inl_p(port) __inl_p(port)
373
374 static inline unsigned char __inb(unsigned long port)
375 {
376         return __ioswab8(*(volatile u8 *)(mips_io_port_base + port));
377 }
378
379 static inline unsigned short __inw(unsigned long port)
380 {
381         return __ioswab16(*(volatile u16 *)(mips_io_port_base + port));
382 }
383
384 static inline unsigned int __inl(unsigned long port)
385 {
386         return __ioswab32(*(volatile u32 *)(mips_io_port_base + port));
387 }
388
389 static inline unsigned char __inb_p(unsigned long port)
390 {
391         u8 __val;
392
393         __val = *(volatile u8 *)(mips_io_port_base + port);
394         SLOW_DOWN_IO;
395
396         return __ioswab8(__val);
397 }
398
399 static inline unsigned short __inw_p(unsigned long port)
400 {
401         u16 __val;
402
403         __val = *(volatile u16 *)(mips_io_port_base + port);
404         SLOW_DOWN_IO;
405
406         return __ioswab16(__val);
407 }
408
409 static inline unsigned int __inl_p(unsigned long port)
410 {
411         u32 __val;
412
413         __val = *(volatile u32 *)(mips_io_port_base + port);
414         SLOW_DOWN_IO;
415         return __ioswab32(__val);
416 }
417
418 #define outsb(port, addr, count) __outsb(port, addr, count)
419 #define insb(port, addr, count) __insb(port, addr, count)
420 #define outsw(port, addr, count) __outsw(port, addr, count)
421 #define insw(port, addr, count) __insw(port, addr, count)
422 #define outsl(port, addr, count) __outsl(port, addr, count)
423 #define insl(port, addr, count) __insl(port, addr, count)
424
425 static inline void __outsb(unsigned long port, void *addr, unsigned int count)
426 {
427         while (count--) {
428                 outb(*(u8 *)addr, port);
429                 addr++;
430         }
431 }
432
433 static inline void __insb(unsigned long port, void *addr, unsigned int count)
434 {
435         while (count--) {
436                 *(u8 *)addr = inb(port);
437                 addr++;
438         }
439 }
440
441 static inline void __outsw(unsigned long port, void *addr, unsigned int count)
442 {
443         while (count--) {
444                 outw(*(u16 *)addr, port);
445                 addr += 2;
446         }
447 }
448
449 static inline void __insw(unsigned long port, void *addr, unsigned int count)
450 {
451         while (count--) {
452                 *(u16 *)addr = inw(port);
453                 addr += 2;
454         }
455 }
456
457 static inline void __outsl(unsigned long port, void *addr, unsigned int count)
458 {
459         while (count--) {
460                 outl(*(u32 *)addr, port);
461                 addr += 4;
462         }
463 }
464
465 static inline void __insl(unsigned long port, void *addr, unsigned int count)
466 {
467         while (count--) {
468                 *(u32 *)addr = inl(port);
469                 addr += 4;
470         }
471 }
472
473 /*
474  * The caches on some architectures aren't dma-coherent and have need to
475  * handle this in software.  There are three types of operations that
476  * can be applied to dma buffers.
477  *
478  *  - dma_cache_wback_inv(start, size) makes caches and coherent by
479  *    writing the content of the caches back to memory, if necessary.
480  *    The function also invalidates the affected part of the caches as
481  *    necessary before DMA transfers from outside to memory.
482  *  - dma_cache_wback(start, size) makes caches and coherent by
483  *    writing the content of the caches back to memory, if necessary.
484  *    The function also invalidates the affected part of the caches as
485  *    necessary before DMA transfers from outside to memory.
486  *  - dma_cache_inv(start, size) invalidates the affected parts of the
487  *    caches.  Dirty lines of the caches may be written back or simply
488  *    be discarded.  This operation is necessary before dma operations
489  *    to the memory.
490  */
491 #ifdef CONFIG_NONCOHERENT_IO
492
493 extern void (*_dma_cache_wback_inv)(unsigned long start, unsigned long size);
494 extern void (*_dma_cache_wback)(unsigned long start, unsigned long size);
495 extern void (*_dma_cache_inv)(unsigned long start, unsigned long size);
496
497 #define dma_cache_wback_inv(start,size) _dma_cache_wback_inv(start,size)
498 #define dma_cache_wback(start,size)     _dma_cache_wback(start,size)
499 #define dma_cache_inv(start,size)       _dma_cache_inv(start,size)
500
501 #else /* Sane hardware */
502
503 #define dma_cache_wback_inv(start,size) do { (start); (size); } while (0)
504 #define dma_cache_wback(start,size)     do { (start); (size); } while (0)
505 #define dma_cache_inv(start,size)       do { (start); (size); } while (0)
506
507 #endif /* CONFIG_NONCOHERENT_IO */
508
509 #endif /* _ASM_IO_H */