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