1 /* 2 * linux/include/asm-nds/io.h 3 * 4 * Copyright (C) 1996-2000 Russell King 5 * 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> 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License version 2 as 12 * published by the Free Software Foundation. 13 * 14 * Modifications: 15 * 16-Sep-1996 RMK Inlined the inx/outx functions & optimised for both 16 * constant addresses and variable addresses. 17 * 04-Dec-1997 RMK Moved a lot of this stuff to the new architecture 18 * specific IO header files. 19 * 27-Mar-1999 PJB Second parameter of memcpy_toio is const.. 20 * 04-Apr-1999 PJB Added check_signature. 21 * 12-Dec-1999 RMK More cleanups 22 * 18-Jun-2000 RMK Removed virt_to_* and friends definitions 23 */ 24 #ifndef __ASM_NDS_IO_H 25 #define __ASM_NDS_IO_H 26 27 /* 28 * CAUTION: 29 * - do not implement for NDS32 Arch yet. 30 * - cmd_pci.c, cmd_scsi.c, Lynxkdi.c, usb.c, usb_storage.c, etc... 31 * iinclude asm/io.h 32 */ 33 34 #ifdef __KERNEL__ 35 36 #include <linux/types.h> 37 #include <asm/byteorder.h> 38 39 static inline void sync(void) 40 { 41 } 42 43 /* 44 * Given a physical address and a length, return a virtual address 45 * that can be used to access the memory range with the caching 46 * properties specified by "flags". 47 */ 48 #define MAP_NOCACHE (0) 49 #define MAP_WRCOMBINE (0) 50 #define MAP_WRBACK (0) 51 #define MAP_WRTHROUGH (0) 52 53 static inline void * 54 map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags) 55 { 56 return (void *)paddr; 57 } 58 59 /* 60 * Take down a mapping set up by map_physmem(). 61 */ 62 static inline void unmap_physmem(void *vaddr, unsigned long flags) 63 { 64 65 } 66 67 static inline phys_addr_t virt_to_phys(void *vaddr) 68 { 69 return (phys_addr_t)(vaddr); 70 } 71 72 /* 73 * Generic virtual read/write. Note that we don't support half-word 74 * read/writes. We define __arch_*[bl] here, and leave __arch_*w 75 * to the architecture specific code. 76 */ 77 #define __arch_getb(a) (*(unsigned char *)(a)) 78 #define __arch_getw(a) (*(unsigned short *)(a)) 79 #define __arch_getl(a) (*(unsigned int *)(a)) 80 81 #define __arch_putb(v, a) (*(unsigned char *)(a) = (v)) 82 #define __arch_putw(v, a) (*(unsigned short *)(a) = (v)) 83 #define __arch_putl(v, a) (*(unsigned int *)(a) = (v)) 84 85 extern void __raw_writesb(unsigned int addr, const void *data, int bytelen); 86 extern void __raw_writesw(unsigned int addr, const void *data, int wordlen); 87 extern void __raw_writesl(unsigned int addr, const void *data, int longlen); 88 89 extern void __raw_readsb(unsigned int addr, void *data, int bytelen); 90 extern void __raw_readsw(unsigned int addr, void *data, int wordlen); 91 extern void __raw_readsl(unsigned int addr, void *data, int longlen); 92 93 #define __raw_writeb(v, a) __arch_putb(v, a) 94 #define __raw_writew(v, a) __arch_putw(v, a) 95 #define __raw_writel(v, a) __arch_putl(v, a) 96 97 #define __raw_readb(a) __arch_getb(a) 98 #define __raw_readw(a) __arch_getw(a) 99 #define __raw_readl(a) __arch_getl(a) 100 101 #define writeb(v, a) __arch_putb(v, a) 102 #define writew(v, a) __arch_putw(v, a) 103 #define writel(v, a) __arch_putl(v, a) 104 105 #define readb(a) __arch_getb(a) 106 #define readw(a) __arch_getw(a) 107 #define readl(a) __arch_getl(a) 108 109 /* 110 * The compiler seems to be incapable of optimising constants 111 * properly. Spell it out to the compiler in some cases. 112 * These are only valid for small values of "off" (< 1<<12) 113 */ 114 #define __raw_base_writeb(val, base, off) __arch_base_putb(val, base, off) 115 #define __raw_base_writew(val, base, off) __arch_base_putw(val, base, off) 116 #define __raw_base_writel(val, base, off) __arch_base_putl(val, base, off) 117 118 #define __raw_base_readb(base, off) __arch_base_getb(base, off) 119 #define __raw_base_readw(base, off) __arch_base_getw(base, off) 120 #define __raw_base_readl(base, off) __arch_base_getl(base, off) 121 122 /* 123 * Now, pick up the machine-defined IO definitions 124 * #include <asm/arch/io.h> 125 */ 126 127 /* 128 * IO port access primitives 129 * ------------------------- 130 * 131 * The NDS32 doesn't have special IO access instructions just like ARM; 132 * all IO is memory mapped. 133 * Note that these are defined to perform little endian accesses 134 * only. Their primary purpose is to access PCI and ISA peripherals. 135 * 136 * Note that for a big endian machine, this implies that the following 137 * big endian mode connectivity is in place, as described by numerious 138 * ARM documents: 139 * 140 * PCI: D0-D7 D8-D15 D16-D23 D24-D31 141 * ARM: D24-D31 D16-D23 D8-D15 D0-D7 142 * 143 * The machine specific io.h include defines __io to translate an "IO" 144 * address to a memory address. 145 * 146 * Note that we prevent GCC re-ordering or caching values in expressions 147 * by introducing sequence points into the in*() definitions. Note that 148 * __raw_* do not guarantee this behaviour. 149 * 150 * The {in,out}[bwl] macros are for emulating x86-style PCI/ISA IO space. 151 */ 152 #ifdef __io 153 #define outb(v, p) __raw_writeb(v, __io(p)) 154 #define outw(v, p) __raw_writew(cpu_to_le16(v), __io(p)) 155 #define outl(v, p) __raw_writel(cpu_to_le32(v), __io(p)) 156 157 #define inb(p) ({ unsigned int __v = __raw_readb(__io(p)); __v; }) 158 #define inw(p) ({ unsigned int __v = le16_to_cpu(__raw_readw(__io(p))); __v; }) 159 #define inl(p) ({ unsigned int __v = le32_to_cpu(__raw_readl(__io(p))); __v; }) 160 161 #define outsb(p, d, l) writesb(__io(p), d, l) 162 #define outsw(p, d, l) writesw(__io(p), d, l) 163 #define outsl(p, d, l) writesl(__io(p), d, l) 164 165 #define insb(p, d, l) readsb(__io(p), d, l) 166 #define insw(p, d, l) readsw(__io(p), d, l) 167 #define insl(p, d, l) readsl(__io(p), d, l) 168 169 static inline void readsb(unsigned int *addr, void * data, int bytelen) 170 { 171 unsigned char *ptr = (unsigned char *)addr; 172 unsigned char *ptr2 = (unsigned char *)data; 173 while (bytelen) { 174 *ptr2 = *ptr; 175 ptr2++; 176 bytelen--; 177 } 178 } 179 180 static inline void readsw(unsigned int *addr, void * data, int wordlen) 181 { 182 unsigned short *ptr = (unsigned short *)addr; 183 unsigned short *ptr2 = (unsigned short *)data; 184 while (wordlen) { 185 *ptr2 = *ptr; 186 ptr2++; 187 wordlen--; 188 } 189 } 190 191 static inline void readsl(unsigned int *addr, void * data, int longlen) 192 { 193 unsigned int *ptr = (unsigned int *)addr; 194 unsigned int *ptr2 = (unsigned int *)data; 195 while (longlen) { 196 *ptr2 = *ptr; 197 ptr2++; 198 longlen--; 199 } 200 } 201 static inline void writesb(unsigned int *addr, const void * data, int bytelen) 202 { 203 unsigned char *ptr = (unsigned char *)addr; 204 unsigned char *ptr2 = (unsigned char *)data; 205 while (bytelen) { 206 *ptr = *ptr2; 207 ptr2++; 208 bytelen--; 209 } 210 } 211 static inline void writesw(unsigned int *addr, const void * data, int wordlen) 212 { 213 unsigned short *ptr = (unsigned short *)addr; 214 unsigned short *ptr2 = (unsigned short *)data; 215 while (wordlen) { 216 *ptr = *ptr2; 217 ptr2++; 218 wordlen--; 219 } 220 } 221 static inline void writesl(unsigned int *addr, const void * data, int longlen) 222 { 223 unsigned int *ptr = (unsigned int *)addr; 224 unsigned int *ptr2 = (unsigned int *)data; 225 while (longlen) { 226 *ptr = *ptr2; 227 ptr2++; 228 longlen--; 229 } 230 } 231 #endif 232 233 #define outb_p(val, port) outb((val), (port)) 234 #define outw_p(val, port) outw((val), (port)) 235 #define outl_p(val, port) outl((val), (port)) 236 #define inb_p(port) inb((port)) 237 #define inw_p(port) inw((port)) 238 #define inl_p(port) inl((port)) 239 240 #define outsb_p(port, from, len) outsb(port, from, len) 241 #define outsw_p(port, from, len) outsw(port, from, len) 242 #define outsl_p(port, from, len) outsl(port, from, len) 243 #define insb_p(port, to, len) insb(port, to, len) 244 #define insw_p(port, to, len) insw(port, to, len) 245 #define insl_p(port, to, len) insl(port, to, len) 246 247 /* 248 * ioremap and friends. 249 * 250 * ioremap takes a PCI memory address, as specified in 251 * linux/Documentation/IO-mapping.txt. If you want a 252 * physical address, use __ioremap instead. 253 */ 254 extern void *__ioremap(unsigned long offset, size_t size, unsigned long flags); 255 extern void __iounmap(void *addr); 256 257 /* 258 * Generic ioremap support. 259 * 260 * Define: 261 * iomem_valid_addr(off,size) 262 * iomem_to_phys(off) 263 */ 264 #ifdef iomem_valid_addr 265 #define __arch_ioremap(off, sz, nocache) \ 266 ({ \ 267 unsigned long _off = (off), _size = (sz); \ 268 void *_ret = (void *)0; \ 269 if (iomem_valid_addr(_off, _size)) \ 270 _ret = __ioremap(iomem_to_phys(_off), _size, 0); \ 271 _ret; \ 272 }) 273 274 #define __arch_iounmap __iounmap 275 #endif 276 277 #define ioremap(off, sz) __arch_ioremap((off), (sz), 0) 278 #define ioremap_nocache(off, sz) __arch_ioremap((off), (sz), 1) 279 #define iounmap(_addr) __arch_iounmap(_addr) 280 281 /* 282 * DMA-consistent mapping functions. These allocate/free a region of 283 * uncached, unwrite-buffered mapped memory space for use with DMA 284 * devices. This is the "generic" version. The PCI specific version 285 * is in pci.h 286 */ 287 extern void *consistent_alloc(int gfp, size_t size, dma_addr_t *handle); 288 extern void consistent_free(void *vaddr, size_t size, dma_addr_t handle); 289 extern void consistent_sync(void *vaddr, size_t size, int rw); 290 291 /* 292 * String version of IO memory access ops: 293 */ 294 extern void _memcpy_fromio(void *, unsigned long, size_t); 295 extern void _memcpy_toio(unsigned long, const void *, size_t); 296 extern void _memset_io(unsigned long, int, size_t); 297 298 extern void __readwrite_bug(const char *fn); 299 300 /* 301 * If this architecture has PCI memory IO, then define the read/write 302 * macros. These should only be used with the cookie passed from 303 * ioremap. 304 */ 305 #ifdef __mem_pci 306 307 #define readb(c) ({ unsigned int __v = \ 308 __raw_readb(__mem_pci(c)); __v; }) 309 #define readw(c) ({ unsigned int __v = \ 310 le16_to_cpu(__raw_readw(__mem_pci(c))); __v; }) 311 #define readl(c) ({ unsigned int __v = \ 312 le32_to_cpu(__raw_readl(__mem_pci(c))); __v; }) 313 314 #define writeb(v, c) __raw_writeb(v, __mem_pci(c)) 315 #define writew(v, c) __raw_writew(cpu_to_le16(v), __mem_pci(c)) 316 #define writel(v, c) __raw_writel(cpu_to_le32(v), __mem_pci(c)) 317 318 #define memset_io(c, v, l) _memset_io(__mem_pci(c), (v), (l)) 319 #define memcpy_fromio(a, c, l) _memcpy_fromio((a), __mem_pci(c), (l)) 320 #define memcpy_toio(c, a, l) _memcpy_toio(__mem_pci(c), (a), (l)) 321 322 #define eth_io_copy_and_sum(s, c, l, b) \ 323 eth_copy_and_sum((s), __mem_pci(c), (l), (b)) 324 325 static inline int 326 check_signature(unsigned long io_addr, const unsigned char *signature, 327 int length) 328 { 329 int retval = 0; 330 do { 331 if (readb(io_addr) != *signature) 332 goto out; 333 io_addr++; 334 signature++; 335 length--; 336 } while (length); 337 retval = 1; 338 out: 339 return retval; 340 } 341 342 #elif !defined(readb) 343 344 #define readb(addr) (__readwrite_bug("readb"), 0) 345 #define readw(addr) (__readwrite_bug("readw"), 0) 346 #define readl(addr) (__readwrite_bug("readl"), 0) 347 #define writeb(v, addr) __readwrite_bug("writeb") 348 #define writew(v, addr) __readwrite_bug("writew") 349 #define writel(v, addr) __readwrite_bug("writel") 350 351 #define eth_io_copy_and_sum(a, b, c, d) __readwrite_bug("eth_io_copy_and_sum") 352 353 #define check_signature(io, sig, len) (0) 354 355 #endif /* __mem_pci */ 356 357 /* 358 * If this architecture has ISA IO, then define the isa_read/isa_write 359 * macros. 360 */ 361 #ifdef __mem_isa 362 363 #define isa_readb(addr) __raw_readb(__mem_isa(addr)) 364 #define isa_readw(addr) __raw_readw(__mem_isa(addr)) 365 #define isa_readl(addr) __raw_readl(__mem_isa(addr)) 366 #define isa_writeb(val, addr) __raw_writeb(val, __mem_isa(addr)) 367 #define isa_writew(val, addr) __raw_writew(val, __mem_isa(addr)) 368 #define isa_writel(val, addr) __raw_writel(val, __mem_isa(addr)) 369 #define isa_memset_io(a, b, c) _memset_io(__mem_isa(a), (b), (c)) 370 #define isa_memcpy_fromio(a, b, c) _memcpy_fromio((a), __mem_isa(b), (c)) 371 #define isa_memcpy_toio(a, b, c) _memcpy_toio(__mem_isa((a)), (b), (c)) 372 373 #define isa_eth_io_copy_and_sum(a, b, c, d) \ 374 eth_copy_and_sum((a), __mem_isa(b), (c), (d)) 375 376 static inline int 377 isa_check_signature(unsigned long io_addr, const unsigned char *signature, 378 int length) 379 { 380 int retval = 0; 381 do { 382 if (isa_readb(io_addr) != *signature) 383 goto out; 384 io_addr++; 385 signature++; 386 length--; 387 } while (length); 388 retval = 1; 389 out: 390 return retval; 391 } 392 393 #else /* __mem_isa */ 394 395 #define isa_readb(addr) (__readwrite_bug("isa_readb"), 0) 396 #define isa_readw(addr) (__readwrite_bug("isa_readw"), 0) 397 #define isa_readl(addr) (__readwrite_bug("isa_readl"), 0) 398 #define isa_writeb(val, addr) __readwrite_bug("isa_writeb") 399 #define isa_writew(val, addr) __readwrite_bug("isa_writew") 400 #define isa_writel(val, addr) __readwrite_bug("isa_writel") 401 #define isa_memset_io(a, b, c) __readwrite_bug("isa_memset_io") 402 #define isa_memcpy_fromio(a, b, c) __readwrite_bug("isa_memcpy_fromio") 403 #define isa_memcpy_toio(a, b, c) __readwrite_bug("isa_memcpy_toio") 404 405 #define isa_eth_io_copy_and_sum(a, b, c, d) \ 406 __readwrite_bug("isa_eth_io_copy_and_sum") 407 408 #define isa_check_signature(io, sig, len) (0) 409 410 #endif /* __mem_isa */ 411 #endif /* __KERNEL__ */ 412 #endif /* __ASM_NDS_IO_H */ 413