ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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  */
10 #ifndef _ASM_IO_H
11 #define _ASM_IO_H
12
13 #include <linux/config.h>
14 #include <linux/types.h>
15
16 #include <asm/addrspace.h>
17 #include <asm/cpu.h>
18 #include <asm/cpu-features.h>
19 #include <asm/page.h>
20 #include <asm/pgtable-bits.h>
21 #include <asm/processor.h>
22 #include <asm/byteorder.h>
23 #include <mangle-port.h>
24
25 /*
26  * Slowdown I/O port space accesses for antique hardware.
27  */
28 #undef CONF_SLOWDOWN_IO
29
30 /*
31  * Sane hardware offers swapping of I/O space accesses in hardware; less
32  * sane hardware forces software to fiddle with this ...
33  */
34 #if defined(CONFIG_SWAP_IO_SPACE) && defined(__MIPSEB__)
35
36 #define __ioswab8(x) (x)
37
38 #ifdef CONFIG_SGI_IP22
39 /*
40  * IP22 seems braindead enough to swap 16bits values in hardware, but
41  * not 32bits.  Go figure... Can't tell without documentation.
42  */
43 #define __ioswab16(x) (x)
44 #else
45 #define __ioswab16(x) swab16(x)
46 #endif
47 #define __ioswab32(x) swab32(x)
48 #define __ioswab64(x) swab64(x)
49
50 #else
51
52 #define __ioswab8(x) (x)
53 #define __ioswab16(x) (x)
54 #define __ioswab32(x) (x)
55 #define __ioswab64(x) (x)
56
57 #endif
58
59 #define IO_SPACE_LIMIT 0xffff
60
61 /*
62  * On MIPS I/O ports are memory mapped, so we access them using normal
63  * load/store instructions. mips_io_port_base is the virtual address to
64  * which all ports are being mapped.  For sake of efficiency some code
65  * assumes that this is an address that can be loaded with a single lui
66  * instruction, so the lower 16 bits must be zero.  Should be true on
67  * on any sane architecture; generic code does not use this assumption.
68  */
69 extern const unsigned long mips_io_port_base;
70
71 #define set_io_port_base(base)  \
72         do { * (unsigned long *) &mips_io_port_base = (base); } while (0)
73
74 /*
75  * Thanks to James van Artsdalen for a better timing-fix than
76  * the two short jumps: using outb's to a nonexistent port seems
77  * to guarantee better timings even on fast machines.
78  *
79  * On the other hand, I'd like to be sure of a non-existent port:
80  * I feel a bit unsafe about using 0x80 (should be safe, though)
81  *
82  *              Linus
83  *
84  */
85
86 #define __SLOW_DOWN_IO \
87         __asm__ __volatile__( \
88                 "sb\t$0,0x80(%0)" \
89                 : : "r" (mips_io_port_base));
90
91 #ifdef CONF_SLOWDOWN_IO
92 #ifdef REALLY_SLOW_IO
93 #define SLOW_DOWN_IO { __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; }
94 #else
95 #define SLOW_DOWN_IO __SLOW_DOWN_IO
96 #endif
97 #else
98 #define SLOW_DOWN_IO
99 #endif
100
101 /*
102  *     virt_to_phys    -       map virtual addresses to physical
103  *     @address: address to remap
104  *
105  *     The returned physical address is the physical (CPU) mapping for
106  *     the memory address given. It is only valid to use this function on
107  *     addresses directly mapped or allocated via kmalloc.
108  *
109  *     This function does not give bus mappings for DMA transfers. In
110  *     almost all conceivable cases a device driver should not be using
111  *     this function
112  */
113 static inline unsigned long virt_to_phys(volatile void * address)
114 {
115         return (unsigned long)address - PAGE_OFFSET;
116 }
117
118 /*
119  *     phys_to_virt    -       map physical address to virtual
120  *     @address: address to remap
121  *
122  *     The returned virtual address is a current CPU mapping for
123  *     the memory address given. It is only valid to use this function on
124  *     addresses that have a kernel mapping
125  *
126  *     This function does not handle bus mappings for DMA transfers. In
127  *     almost all conceivable cases a device driver should not be using
128  *     this function
129  */
130 static inline void * phys_to_virt(unsigned long address)
131 {
132         return (void *)(address + PAGE_OFFSET);
133 }
134
135 /*
136  * ISA I/O bus memory addresses are 1:1 with the physical address.
137  */
138 static inline unsigned long isa_virt_to_bus(volatile void * address)
139 {
140         return (unsigned long)address - PAGE_OFFSET;
141 }
142
143 static inline void * isa_bus_to_virt(unsigned long address)
144 {
145         return (void *)(address + PAGE_OFFSET);
146 }
147
148 #define isa_page_to_bus page_to_phys
149
150 /*
151  * However PCI ones are not necessarily 1:1 and therefore these interfaces
152  * are forbidden in portable PCI drivers.
153  *
154  * Allow them for x86 for legacy drivers, though.
155  */
156 #define virt_to_bus virt_to_phys
157 #define bus_to_virt phys_to_virt
158
159 /*
160  * isa_slot_offset is the address where E(ISA) busaddress 0 is mapped
161  * for the processor.  This implies the assumption that there is only
162  * one of these busses.
163  */
164 extern unsigned long isa_slot_offset;
165
166 /*
167  * Change "struct page" to physical address.
168  */
169 #define page_to_phys(page)      ((dma_addr_t)page_to_pfn(page) << PAGE_SHIFT)
170
171 extern void * __ioremap(phys_t offset, phys_t size, unsigned long flags);
172 extern void __iounmap(void *addr);
173
174 static inline void * __ioremap_mode(unsigned long offset, unsigned long size,
175         unsigned long flags)
176 {
177         if (cpu_has_64bit_addresses) {
178                 u64 base = UNCAC_BASE;
179
180                 /*
181                  * R10000 supports a 2 bit uncached attribute therefore
182                  * UNCAC_BASE may not equal IO_BASE.
183                  */
184                 if (flags == _CACHE_UNCACHED)
185                         base = (u64) IO_BASE;
186                 return (void *) (unsigned long) (base + offset);
187         }
188
189         return __ioremap(offset, size, flags);
190 }
191
192 /*
193  * ioremap     -   map bus memory into CPU space
194  * @offset:    bus address of the memory
195  * @size:      size of the resource to map
196  *
197  * ioremap performs a platform specific sequence of operations to
198  * make bus memory CPU accessible via the readb/readw/readl/writeb/
199  * writew/writel functions and the other mmio helpers. The returned
200  * address is not guaranteed to be usable directly as a virtual
201  * address.
202  */
203 #define ioremap(offset, size)                                           \
204         __ioremap_mode((offset), (size), _CACHE_UNCACHED)
205
206 /*
207  * ioremap_nocache     -   map bus memory into CPU space
208  * @offset:    bus address of the memory
209  * @size:      size of the resource to map
210  *
211  * ioremap_nocache performs a platform specific sequence of operations to
212  * make bus memory CPU accessible via the readb/readw/readl/writeb/
213  * writew/writel functions and the other mmio helpers. The returned
214  * address is not guaranteed to be usable directly as a virtual
215  * address.
216  *
217  * This version of ioremap ensures that the memory is marked uncachable
218  * on the CPU as well as honouring existing caching rules from things like
219  * the PCI bus. Note that there are other caches and buffers on many
220  * busses. In paticular driver authors should read up on PCI writes
221  *
222  * It's useful if some control registers are in such an area and
223  * write combining or read caching is not desirable:
224  */
225 #define ioremap_nocache(offset, size)                                   \
226         __ioremap_mode((offset), (size), _CACHE_UNCACHED)
227
228 /*
229  * These two are MIPS specific ioremap variant.  ioremap_cacheable_cow
230  * requests a cachable mapping, ioremap_uncached_accelerated requests a
231  * mapping using the uncached accelerated mode which isn't supported on
232  * all processors.
233  */
234 #define ioremap_cacheable_cow(offset, size)                             \
235         __ioremap_mode((offset), (size), _CACHE_CACHABLE_COW)
236 #define ioremap_uncached_accelerated(offset, size)                      \
237         __ioremap_mode((offset), (size), _CACHE_UNCACHED_ACCELERATED)
238
239 static inline void iounmap(void *addr)
240 {
241         if (cpu_has_64bits)
242                 return;
243
244         __iounmap(addr);
245 }
246
247 #define __raw_readb(addr)       (*(volatile unsigned char *)(addr))
248 #define __raw_readw(addr)       (*(volatile unsigned short *)(addr))
249 #define __raw_readl(addr)       (*(volatile unsigned int *)(addr))
250 #ifdef CONFIG_MIPS32
251 #define ____raw_readq(addr)                                             \
252 ({                                                                      \
253         u64 __res;                                                      \
254                                                                         \
255         __asm__ __volatile__ (                                          \
256                 "       .set    mips3           # ____raw_readq \n"     \
257                 "       ld      %L0, (%1)                       \n"     \
258                 "       dsra32  %M0, %L0, 0                     \n"     \
259                 "       sll     %L0, %L0, 0                     \n"     \
260                 "       .set    mips0                           \n"     \
261                 : "=r" (__res)                                          \
262                 : "r" (addr));                                          \
263         __res;                                                          \
264 })
265 #define __raw_readq(addr)                                               \
266 ({                                                                      \
267         unsigned long __flags;                                          \
268         u64 __res;                                                      \
269                                                                         \
270         local_irq_save(__flags);                                        \
271         __res = ____raw_readq(addr);                                    \
272         local_irq_restore(__flags);                                     \
273         __res;                                                          \
274 })
275 #endif
276 #ifdef CONFIG_MIPS64
277 #define ____raw_readq(addr)     (*(volatile unsigned long *)(addr))
278 #define __raw_readq(addr)       ____raw_readq(addr)
279 #endif
280
281 #define readb(addr)             __ioswab8(__raw_readb(addr))
282 #define readw(addr)             __ioswab16(__raw_readw(addr))
283 #define readl(addr)             __ioswab32(__raw_readl(addr))
284 #define readq(addr)             __ioswab64(__raw_readq(addr))
285 #define readb_relaxed(addr)     readb(addr)
286 #define readw_relaxed(addr)     readw(addr)
287 #define readl_relaxed(addr)     readl(addr)
288 #define readq_relaxed(addr)     readq(addr)
289
290 #define __raw_writeb(b,addr)    ((*(volatile unsigned char *)(addr)) = (b))
291 #define __raw_writew(w,addr)    ((*(volatile unsigned short *)(addr)) = (w))
292 #define __raw_writel(l,addr)    ((*(volatile unsigned int *)(addr)) = (l))
293 #ifdef CONFIG_MIPS32
294 #define ____raw_writeq(val,addr)                                                \
295 ({                                                                      \
296         u64 __tmp;                                                      \
297                                                                         \
298         __asm__ __volatile__ (                                          \
299                 "       .set    mips3                           \n"     \
300                 "       dsll32  %L0, %L0, 0     # ____raw_writeq\n"     \
301                 "       dsrl32  %L0, %L0, 0                     \n"     \
302                 "       dsll32  %M0, %M0, 0                     \n"     \
303                 "       or      %L0, %L0, %M0                   \n"     \
304                 "       sd      %L0, (%2)                       \n"     \
305                 "       .set    mips0                           \n"     \
306                 : "=r" (__tmp)                                          \
307                 : "0" ((unsigned long long)val), "r" (addr));           \
308 })
309 #define __raw_writeq(val,addr)                                          \
310 ({                                                                      \
311         unsigned long __flags;                                          \
312                                                                         \
313         local_irq_save(__flags);                                        \
314         ____raw_writeq(val, addr);                                      \
315         local_irq_restore(__flags);                                     \
316 })
317 #endif
318 #ifdef CONFIG_MIPS64
319 #define ____raw_writeq(q,addr)  ((*(volatile unsigned long *)(addr)) = (q))
320 #define __raw_writeq(q,addr)    ____raw_writeq(q, addr)
321 #endif
322
323 #define writeb(b,addr)          __raw_writeb(__ioswab8(b),(addr))
324 #define writew(w,addr)          __raw_writew(__ioswab16(w),(addr))
325 #define writel(l,addr)          __raw_writel(__ioswab32(l),(addr))
326 #define writeq(q,addr)          __raw_writeq(__ioswab64(q),(addr))
327
328 #define memset_io(a,b,c)        memset((void *)(a),(b),(c))
329 #define memcpy_fromio(a,b,c)    memcpy((a),(void *)(b),(c))
330 #define memcpy_toio(a,b,c)      memcpy((void *)(a),(b),(c))
331
332 /*
333  * ISA space is 'always mapped' on currently supported MIPS systems, no need
334  * to explicitly ioremap() it. The fact that the ISA IO space is mapped
335  * to PAGE_OFFSET is pure coincidence - it does not mean ISA values
336  * are physical addresses. The following constant pointer can be
337  * used as the IO-area pointer (it can be iounmapped as well, so the
338  * analogy with PCI is quite large):
339  */
340 #define __ISA_IO_base ((char *)(isa_slot_offset))
341
342 #define isa_readb(a)            readb(__ISA_IO_base + (a))
343 #define isa_readw(a)            readw(__ISA_IO_base + (a))
344 #define isa_readl(a)            readl(__ISA_IO_base + (a))
345 #define isa_readq(a)            readq(__ISA_IO_base + (a))
346 #define isa_writeb(b,a)         writeb(b,__ISA_IO_base + (a))
347 #define isa_writew(w,a)         writew(w,__ISA_IO_base + (a))
348 #define isa_writel(l,a)         writel(l,__ISA_IO_base + (a))
349 #define isa_writeq(q,a)         writeq(q,__ISA_IO_base + (a))
350 #define isa_memset_io(a,b,c)    memset_io(__ISA_IO_base + (a),(b),(c))
351 #define isa_memcpy_fromio(a,b,c) memcpy_fromio((a),__ISA_IO_base + (b),(c))
352 #define isa_memcpy_toio(a,b,c)  memcpy_toio(__ISA_IO_base + (a),(b),(c))
353
354 /*
355  * We don't have csum_partial_copy_fromio() yet, so we cheat here and
356  * just copy it. The net code will then do the checksum later.
357  */
358 #define eth_io_copy_and_sum(skb,src,len,unused) memcpy_fromio((skb)->data,(src),(len))
359 #define isa_eth_io_copy_and_sum(a,b,c,d) eth_copy_and_sum((a),(b),(c),(d))
360
361 /*
362  *     check_signature         -       find BIOS signatures
363  *     @io_addr: mmio address to check
364  *     @signature:  signature block
365  *     @length: length of signature
366  *
367  *     Perform a signature comparison with the mmio address io_addr. This
368  *     address should have been obtained by ioremap.
369  *     Returns 1 on a match.
370  */
371 static inline int check_signature(unsigned long io_addr,
372         const unsigned char *signature, int length)
373 {
374         int retval = 0;
375         do {
376                 if (readb(io_addr) != *signature)
377                         goto out;
378                 io_addr++;
379                 signature++;
380                 length--;
381         } while (length);
382         retval = 1;
383 out:
384         return retval;
385 }
386
387 /*
388  *     isa_check_signature             -       find BIOS signatures
389  *     @io_addr: mmio address to check
390  *     @signature:  signature block
391  *     @length: length of signature
392  *
393  *     Perform a signature comparison with the ISA mmio address io_addr.
394  *     Returns 1 on a match.
395  *
396  *     This function is deprecated. New drivers should use ioremap and
397  *     check_signature.
398  */
399 #define isa_check_signature(io, s, l)   check_signature(i,s,l)
400
401 static inline void __outb(unsigned char val, unsigned long port)
402 {
403         port = __swizzle_addr_b(port);
404
405         *(volatile u8 *)(mips_io_port_base + port) = __ioswab8(val);
406 }
407
408 static inline void __outw(unsigned short val, unsigned long port)
409 {
410         port = __swizzle_addr_w(port);
411
412         *(volatile u16 *)(mips_io_port_base + port) = __ioswab16(val);
413 }
414
415 static inline void __outl(unsigned int val, unsigned long port)
416 {
417         port = __swizzle_addr_l(port);
418
419         *(volatile u32 *)(mips_io_port_base + port) = __ioswab32(val);
420 }
421
422 static inline void __outb_p(unsigned char val, unsigned long port)
423 {
424         port = __swizzle_addr_b(port);
425
426         *(volatile u8 *)(mips_io_port_base + port) = __ioswab8(val);
427         SLOW_DOWN_IO;
428 }
429
430 static inline void __outw_p(unsigned short val, unsigned long port)
431 {
432         port = __swizzle_addr_w(port);
433
434         *(volatile u16 *)(mips_io_port_base + port) = __ioswab16(val);
435         SLOW_DOWN_IO;
436 }
437
438 static inline void __outl_p(unsigned int val, unsigned long port)
439 {
440         port = __swizzle_addr_l(port);
441
442         *(volatile u32 *)(mips_io_port_base + port) = __ioswab32(val);
443         SLOW_DOWN_IO;
444 }
445
446 #define outb(val, port)         __outb(val, port)
447 #define outw(val, port)         __outw(val, port)
448 #define outl(val, port)         __outl(val, port)
449 #define outb_p(val, port)       __outb_p(val, port)
450 #define outw_p(val, port)       __outw_p(val, port)
451 #define outl_p(val, port)       __outl_p(val, port)
452
453 static inline unsigned char __inb(unsigned long port)
454 {
455         port = __swizzle_addr_b(port);
456
457         return __ioswab8(*(volatile u8 *)(mips_io_port_base + port));
458 }
459
460 static inline unsigned short __inw(unsigned long port)
461 {
462         port = __swizzle_addr_w(port);
463
464         return __ioswab16(*(volatile u16 *)(mips_io_port_base + port));
465 }
466
467 static inline unsigned int __inl(unsigned long port)
468 {
469         port = __swizzle_addr_l(port);
470
471         return __ioswab32(*(volatile u32 *)(mips_io_port_base + port));
472 }
473
474 static inline unsigned char __inb_p(unsigned long port)
475 {
476         u8 __val;
477
478         port = __swizzle_addr_b(port);
479
480         __val = *(volatile u8 *)(mips_io_port_base + port);
481         SLOW_DOWN_IO;
482
483         return __ioswab8(__val);
484 }
485
486 static inline unsigned short __inw_p(unsigned long port)
487 {
488         u16 __val;
489
490         port = __swizzle_addr_w(port);
491
492         __val = *(volatile u16 *)(mips_io_port_base + port);
493         SLOW_DOWN_IO;
494
495         return __ioswab16(__val);
496 }
497
498 static inline unsigned int __inl_p(unsigned long port)
499 {
500         u32 __val;
501
502         port = __swizzle_addr_l(port);
503
504         __val = *(volatile u32 *)(mips_io_port_base + port);
505         SLOW_DOWN_IO;
506
507         return __ioswab32(__val);
508 }
509
510 #define inb(port)       __inb(port)
511 #define inw(port)       __inw(port)
512 #define inl(port)       __inl(port)
513 #define inb_p(port)     __inb_p(port)
514 #define inw_p(port)     __inw_p(port)
515 #define inl_p(port)     __inl_p(port)
516
517 static inline void __outsb(unsigned long port, void *addr, unsigned int count)
518 {
519         while (count--) {
520                 outb(*(u8 *)addr, port);
521                 addr++;
522         }
523 }
524
525 static inline void __insb(unsigned long port, void *addr, unsigned int count)
526 {
527         while (count--) {
528                 *(u8 *)addr = inb(port);
529                 addr++;
530         }
531 }
532
533 static inline void __outsw(unsigned long port, void *addr, unsigned int count)
534 {
535         while (count--) {
536                 outw(*(u16 *)addr, port);
537                 addr += 2;
538         }
539 }
540
541 static inline void __insw(unsigned long port, void *addr, unsigned int count)
542 {
543         while (count--) {
544                 *(u16 *)addr = inw(port);
545                 addr += 2;
546         }
547 }
548
549 static inline void __outsl(unsigned long port, void *addr, unsigned int count)
550 {
551         while (count--) {
552                 outl(*(u32 *)addr, port);
553                 addr += 4;
554         }
555 }
556
557 static inline void __insl(unsigned long port, void *addr, unsigned int count)
558 {
559         while (count--) {
560                 *(u32 *)addr = inl(port);
561                 addr += 4;
562         }
563 }
564
565 #define outsb(port, addr, count)        __outsb(port, addr, count)
566 #define insb(port, addr, count)         __insb(port, addr, count)
567 #define outsw(port, addr, count)        __outsw(port, addr, count)
568 #define insw(port, addr, count)         __insw(port, addr, count)
569 #define outsl(port, addr, count)        __outsl(port, addr, count)
570 #define insl(port, addr, count)         __insl(port, addr, count)
571
572 /*
573  * The caches on some architectures aren't dma-coherent and have need to
574  * handle this in software.  There are three types of operations that
575  * can be applied to dma buffers.
576  *
577  *  - dma_cache_wback_inv(start, size) makes caches and coherent by
578  *    writing the content of the caches back to memory, if necessary.
579  *    The function also invalidates the affected part of the caches as
580  *    necessary before DMA transfers from outside to memory.
581  *  - dma_cache_wback(start, size) makes caches and coherent by
582  *    writing the content of the caches back to memory, if necessary.
583  *    The function also invalidates the affected part of the caches as
584  *    necessary before DMA transfers from outside to memory.
585  *  - dma_cache_inv(start, size) invalidates the affected parts of the
586  *    caches.  Dirty lines of the caches may be written back or simply
587  *    be discarded.  This operation is necessary before dma operations
588  *    to the memory.
589  */
590 #ifdef CONFIG_DMA_NONCOHERENT
591
592 extern void (*_dma_cache_wback_inv)(unsigned long start, unsigned long size);
593 extern void (*_dma_cache_wback)(unsigned long start, unsigned long size);
594 extern void (*_dma_cache_inv)(unsigned long start, unsigned long size);
595
596 #define dma_cache_wback_inv(start, size)        _dma_cache_wback_inv(start,size)
597 #define dma_cache_wback(start, size)            _dma_cache_wback(start,size)
598 #define dma_cache_inv(start, size)              _dma_cache_inv(start,size)
599
600 #else /* Sane hardware */
601
602 #define dma_cache_wback_inv(start,size) \
603         do { (void) (start); (void) (size); } while (0)
604 #define dma_cache_wback(start,size)     \
605         do { (void) (start); (void) (size); } while (0)
606 #define dma_cache_inv(start,size)       \
607         do { (void) (start); (void) (size); } while (0)
608
609 #endif /* CONFIG_DMA_NONCOHERENT */
610
611 /*
612  * Read a 32-bit register that requires a 64-bit read cycle on the bus.
613  * Avoid interrupt mucking, just adjust the address for 4-byte access.
614  * Assume the addresses are 8-byte aligned.
615  */
616 #ifdef __MIPSEB__
617 #define __CSR_32_ADJUST 4
618 #else
619 #define __CSR_32_ADJUST 0
620 #endif
621
622 #define csr_out32(v,a) (*(volatile u32 *)((unsigned long)(a) + __CSR_32_ADJUST) = (v))
623 #define csr_in32(a)    (*(volatile u32 *)((unsigned long)(a) + __CSR_32_ADJUST))
624
625 #endif /* _ASM_IO_H */