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