2 * linux/include/asm-nds/io.h
4 * Copyright (C) 1996-2000 Russell King
6 * Copyright (C) 2011 Andes Technology Corporation
7 * Shawn Lin, Andes Technology Corporation <nobuhiro@andestech.com>
8 * Macpaul Lin, Andes Technology Corporation <macpaul@andestech.com>
10 * SPDX-License-Identifier: GPL-2.0
13 * 16-Sep-1996 RMK Inlined the inx/outx functions & optimised for both
14 * constant addresses and variable addresses.
15 * 04-Dec-1997 RMK Moved a lot of this stuff to the new architecture
16 * specific IO header files.
17 * 27-Mar-1999 PJB Second parameter of memcpy_toio is const..
18 * 04-Apr-1999 PJB Added check_signature.
19 * 12-Dec-1999 RMK More cleanups
20 * 18-Jun-2000 RMK Removed virt_to_* and friends definitions
22 #ifndef __ASM_NDS_IO_H
23 #define __ASM_NDS_IO_H
27 * - do not implement for NDS32 Arch yet.
28 * - cmd_pci.c, cmd_scsi.c, Lynxkdi.c, usb.c, usb_storage.c, etc...
34 #include <linux/types.h>
35 #include <asm/byteorder.h>
37 static inline void sync(void)
41 #ifdef CONFIG_ARCH_MAP_SYSMEM
42 static inline void *map_sysmem(phys_addr_t paddr, unsigned long len)
44 if(paddr <PHYS_SDRAM_0_SIZE + PHYS_SDRAM_1_SIZE)
45 paddr = paddr | 0x40000000;
46 return (void *)(uintptr_t)paddr;
49 static inline void *unmap_sysmem(const void *vaddr)
51 phys_addr_t paddr = (phys_addr_t)vaddr;
52 paddr = paddr & ~0x40000000;
53 return (void *)(uintptr_t)paddr;
56 static inline phys_addr_t map_to_sysmem(const void *ptr)
58 return (phys_addr_t)(uintptr_t)ptr;
63 * Generic virtual read/write. Note that we don't support half-word
64 * read/writes. We define __arch_*[bl] here, and leave __arch_*w
65 * to the architecture specific code.
67 #define __arch_getb(a) (*(unsigned char *)(a))
68 #define __arch_getw(a) (*(unsigned short *)(a))
69 #define __arch_getl(a) (*(unsigned int *)(a))
71 #define __arch_putb(v, a) (*(unsigned char *)(a) = (v))
72 #define __arch_putw(v, a) (*(unsigned short *)(a) = (v))
73 #define __arch_putl(v, a) (*(unsigned int *)(a) = (v))
75 extern void __raw_writesb(unsigned int addr, const void *data, int bytelen);
76 extern void __raw_writesw(unsigned int addr, const void *data, int wordlen);
77 extern void __raw_writesl(unsigned int addr, const void *data, int longlen);
79 extern void __raw_readsb(unsigned int addr, void *data, int bytelen);
80 extern void __raw_readsw(unsigned int addr, void *data, int wordlen);
81 extern void __raw_readsl(unsigned int addr, void *data, int longlen);
83 #define __raw_writeb(v, a) __arch_putb(v, a)
84 #define __raw_writew(v, a) __arch_putw(v, a)
85 #define __raw_writel(v, a) __arch_putl(v, a)
87 #define __raw_readb(a) __arch_getb(a)
88 #define __raw_readw(a) __arch_getw(a)
89 #define __raw_readl(a) __arch_getl(a)
92 * TODO: The kernel offers some more advanced versions of barriers, it might
93 * have some advantages to use them instead of the simple one here.
95 #define dmb() __asm__ __volatile__ ("" : : : "memory")
96 #define __iormb() dmb()
97 #define __iowmb() dmb()
99 static inline void writeb(u8 val, volatile void __iomem *addr)
102 __arch_putb(val, addr);
105 static inline void writew(u16 val, volatile void __iomem *addr)
108 __arch_putw(val, addr);
112 static inline void writel(u32 val, volatile void __iomem *addr)
115 __arch_putl(val, addr);
118 static inline u8 readb(const volatile void __iomem *addr)
122 val = __arch_getb(addr);
127 static inline u16 readw(const volatile void __iomem *addr)
131 val = __arch_getw(addr);
136 static inline u32 readl(const volatile void __iomem *addr)
140 val = __arch_getl(addr);
146 * The compiler seems to be incapable of optimising constants
147 * properly. Spell it out to the compiler in some cases.
148 * These are only valid for small values of "off" (< 1<<12)
150 #define __raw_base_writeb(val, base, off) __arch_base_putb(val, base, off)
151 #define __raw_base_writew(val, base, off) __arch_base_putw(val, base, off)
152 #define __raw_base_writel(val, base, off) __arch_base_putl(val, base, off)
154 #define __raw_base_readb(base, off) __arch_base_getb(base, off)
155 #define __raw_base_readw(base, off) __arch_base_getw(base, off)
156 #define __raw_base_readl(base, off) __arch_base_getl(base, off)
158 #define out_arch(type, endian, a, v) __raw_write##type(cpu_to_##endian(v), a)
159 #define in_arch(type, endian, a) endian##_to_cpu(__raw_read##type(a))
161 #define out_le32(a, v) out_arch(l, le32, a, v)
162 #define out_le16(a, v) out_arch(w, le16, a, v)
164 #define in_le32(a) in_arch(l, le32, a)
165 #define in_le16(a) in_arch(w, le16, a)
167 #define out_be32(a, v) out_arch(l, be32, a, v)
168 #define out_be16(a, v) out_arch(w, be16, a, v)
170 #define in_be32(a) in_arch(l, be32, a)
171 #define in_be16(a) in_arch(w, be16, a)
173 #define out_8(a, v) __raw_writeb(v, a)
174 #define in_8(a) __raw_readb(a)
177 * Clear and set bits in one shot. These macros can be used to clear and
178 * set multiple bits in a register using a single call. These macros can
179 * also be used to set a multiple-bit bit pattern using a mask, by
180 * specifying the mask in the 'clear' parameter and the new bit pattern
181 * in the 'set' parameter.
184 #define clrbits(type, addr, clear) \
185 out_##type((addr), in_##type(addr) & ~(clear))
187 #define setbits(type, addr, set) \
188 out_##type((addr), in_##type(addr) | (set))
190 #define clrsetbits(type, addr, clear, set) \
191 out_##type((addr), (in_##type(addr) & ~(clear)) | (set))
193 #define clrbits_be32(addr, clear) clrbits(be32, addr, clear)
194 #define setbits_be32(addr, set) setbits(be32, addr, set)
195 #define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set)
197 #define clrbits_le32(addr, clear) clrbits(le32, addr, clear)
198 #define setbits_le32(addr, set) setbits(le32, addr, set)
199 #define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set)
201 #define clrbits_be16(addr, clear) clrbits(be16, addr, clear)
202 #define setbits_be16(addr, set) setbits(be16, addr, set)
203 #define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set)
205 #define clrbits_le16(addr, clear) clrbits(le16, addr, clear)
206 #define setbits_le16(addr, set) setbits(le16, addr, set)
207 #define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set)
209 #define clrbits_8(addr, clear) clrbits(8, addr, clear)
210 #define setbits_8(addr, set) setbits(8, addr, set)
211 #define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set)
214 * Now, pick up the machine-defined IO definitions
215 * #include <asm/arch/io.h>
219 * IO port access primitives
220 * -------------------------
222 * The NDS32 doesn't have special IO access instructions just like ARM;
223 * all IO is memory mapped.
224 * Note that these are defined to perform little endian accesses
225 * only. Their primary purpose is to access PCI and ISA peripherals.
227 * Note that for a big endian machine, this implies that the following
228 * big endian mode connectivity is in place, as described by numerious
231 * PCI: D0-D7 D8-D15 D16-D23 D24-D31
232 * ARM: D24-D31 D16-D23 D8-D15 D0-D7
234 * The machine specific io.h include defines __io to translate an "IO"
235 * address to a memory address.
237 * Note that we prevent GCC re-ordering or caching values in expressions
238 * by introducing sequence points into the in*() definitions. Note that
239 * __raw_* do not guarantee this behaviour.
241 * The {in,out}[bwl] macros are for emulating x86-style PCI/ISA IO space.
244 #define outb(v, p) __raw_writeb(v, __io(p))
245 #define outw(v, p) __raw_writew(cpu_to_le16(v), __io(p))
246 #define outl(v, p) __raw_writel(cpu_to_le32(v), __io(p))
248 #define inb(p) ({ unsigned int __v = __raw_readb(__io(p)); __v; })
249 #define inw(p) ({ unsigned int __v = le16_to_cpu(__raw_readw(__io(p))); __v; })
250 #define inl(p) ({ unsigned int __v = le32_to_cpu(__raw_readl(__io(p))); __v; })
252 #define outsb(p, d, l) writesb(__io(p), d, l)
253 #define outsw(p, d, l) writesw(__io(p), d, l)
254 #define outsl(p, d, l) writesl(__io(p), d, l)
256 #define insb(p, d, l) readsb(__io(p), d, l)
257 #define insw(p, d, l) readsw(__io(p), d, l)
258 #define insl(p, d, l) readsl(__io(p), d, l)
260 static inline void readsb(unsigned int *addr, void * data, int bytelen)
262 unsigned char *ptr = (unsigned char *)addr;
263 unsigned char *ptr2 = (unsigned char *)data;
271 static inline void readsw(unsigned int *addr, void * data, int wordlen)
273 unsigned short *ptr = (unsigned short *)addr;
274 unsigned short *ptr2 = (unsigned short *)data;
282 static inline void readsl(unsigned int *addr, void * data, int longlen)
284 unsigned int *ptr = (unsigned int *)addr;
285 unsigned int *ptr2 = (unsigned int *)data;
292 static inline void writesb(unsigned int *addr, const void * data, int bytelen)
294 unsigned char *ptr = (unsigned char *)addr;
295 unsigned char *ptr2 = (unsigned char *)data;
302 static inline void writesw(unsigned int *addr, const void * data, int wordlen)
304 unsigned short *ptr = (unsigned short *)addr;
305 unsigned short *ptr2 = (unsigned short *)data;
312 static inline void writesl(unsigned int *addr, const void * data, int longlen)
314 unsigned int *ptr = (unsigned int *)addr;
315 unsigned int *ptr2 = (unsigned int *)data;
324 #define outb_p(val, port) outb((val), (port))
325 #define outw_p(val, port) outw((val), (port))
326 #define outl_p(val, port) outl((val), (port))
327 #define inb_p(port) inb((port))
328 #define inw_p(port) inw((port))
329 #define inl_p(port) inl((port))
331 #define outsb_p(port, from, len) outsb(port, from, len)
332 #define outsw_p(port, from, len) outsw(port, from, len)
333 #define outsl_p(port, from, len) outsl(port, from, len)
334 #define insb_p(port, to, len) insb(port, to, len)
335 #define insw_p(port, to, len) insw(port, to, len)
336 #define insl_p(port, to, len) insl(port, to, len)
339 * DMA-consistent mapping functions. These allocate/free a region of
340 * uncached, unwrite-buffered mapped memory space for use with DMA
341 * devices. This is the "generic" version. The PCI specific version
344 extern void *consistent_alloc(int gfp, size_t size, dma_addr_t *handle);
345 extern void consistent_free(void *vaddr, size_t size, dma_addr_t handle);
346 extern void consistent_sync(void *vaddr, size_t size, int rw);
349 * String version of IO memory access ops:
351 extern void _memcpy_fromio(void *, unsigned long, size_t);
352 extern void _memcpy_toio(unsigned long, const void *, size_t);
353 extern void _memset_io(unsigned long, int, size_t);
355 extern void __readwrite_bug(const char *fn);
358 * If this architecture has PCI memory IO, then define the read/write
359 * macros. These should only be used with the cookie passed from
364 #define readb(c) ({ unsigned int __v = \
365 __raw_readb(__mem_pci(c)); __v; })
366 #define readw(c) ({ unsigned int __v = \
367 le16_to_cpu(__raw_readw(__mem_pci(c))); __v; })
368 #define readl(c) ({ unsigned int __v = \
369 le32_to_cpu(__raw_readl(__mem_pci(c))); __v; })
371 #define writeb(v, c) __raw_writeb(v, __mem_pci(c))
372 #define writew(v, c) __raw_writew(cpu_to_le16(v), __mem_pci(c))
373 #define writel(v, c) __raw_writel(cpu_to_le32(v), __mem_pci(c))
375 #define memset_io(c, v, l) _memset_io(__mem_pci(c), (v), (l))
376 #define memcpy_fromio(a, c, l) _memcpy_fromio((a), __mem_pci(c), (l))
377 #define memcpy_toio(c, a, l) _memcpy_toio(__mem_pci(c), (a), (l))
379 #define eth_io_copy_and_sum(s, c, l, b) \
380 eth_copy_and_sum((s), __mem_pci(c), (l), (b))
383 check_signature(unsigned long io_addr, const unsigned char *signature,
388 if (readb(io_addr) != *signature)
398 #endif /* __mem_pci */
401 * If this architecture has ISA IO, then define the isa_read/isa_write
406 #define isa_readb(addr) __raw_readb(__mem_isa(addr))
407 #define isa_readw(addr) __raw_readw(__mem_isa(addr))
408 #define isa_readl(addr) __raw_readl(__mem_isa(addr))
409 #define isa_writeb(val, addr) __raw_writeb(val, __mem_isa(addr))
410 #define isa_writew(val, addr) __raw_writew(val, __mem_isa(addr))
411 #define isa_writel(val, addr) __raw_writel(val, __mem_isa(addr))
412 #define isa_memset_io(a, b, c) _memset_io(__mem_isa(a), (b), (c))
413 #define isa_memcpy_fromio(a, b, c) _memcpy_fromio((a), __mem_isa(b), (c))
414 #define isa_memcpy_toio(a, b, c) _memcpy_toio(__mem_isa((a)), (b), (c))
416 #define isa_eth_io_copy_and_sum(a, b, c, d) \
417 eth_copy_and_sum((a), __mem_isa(b), (c), (d))
420 isa_check_signature(unsigned long io_addr, const unsigned char *signature,
425 if (isa_readb(io_addr) != *signature)
436 #else /* __mem_isa */
438 #define isa_readb(addr) (__readwrite_bug("isa_readb"), 0)
439 #define isa_readw(addr) (__readwrite_bug("isa_readw"), 0)
440 #define isa_readl(addr) (__readwrite_bug("isa_readl"), 0)
441 #define isa_writeb(val, addr) __readwrite_bug("isa_writeb")
442 #define isa_writew(val, addr) __readwrite_bug("isa_writew")
443 #define isa_writel(val, addr) __readwrite_bug("isa_writel")
444 #define isa_memset_io(a, b, c) __readwrite_bug("isa_memset_io")
445 #define isa_memcpy_fromio(a, b, c) __readwrite_bug("isa_memcpy_fromio")
446 #define isa_memcpy_toio(a, b, c) __readwrite_bug("isa_memcpy_toio")
448 #define isa_eth_io_copy_and_sum(a, b, c, d) \
449 __readwrite_bug("isa_eth_io_copy_and_sum")
451 #define isa_check_signature(io, sig, len) (0)
453 #endif /* __mem_isa */
455 #include <asm-generic/io.h>
457 #endif /* __KERNEL__ */
458 #endif /* __ASM_NDS_IO_H */