Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / arch / parisc / lib / iomap.c
blob915c0c4da66385c2e5297fc6950bbf7fc56cff4b
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * iomap.c - Implement iomap interface for PA-RISC
4 * Copyright (c) 2004 Matthew Wilcox
5 */
7 #include <linux/ioport.h>
8 #include <linux/pci.h>
9 #include <linux/export.h>
10 #include <asm/io.h>
13 * The iomap space on 32-bit PA-RISC is intended to look like this:
14 * 00000000-7fffffff virtual mapped IO
15 * 80000000-8fffffff ISA/EISA port space that can't be virtually mapped
16 * 90000000-9fffffff Dino port space
17 * a0000000-afffffff Astro port space
18 * b0000000-bfffffff PAT port space
19 * c0000000-cfffffff non-swapped memory IO
20 * f0000000-ffffffff legacy IO memory pointers
22 * For the moment, here's what it looks like:
23 * 80000000-8fffffff All ISA/EISA port space
24 * f0000000-ffffffff legacy IO memory pointers
26 * On 64-bit, everything is extended, so:
27 * 8000000000000000-8fffffffffffffff All ISA/EISA port space
28 * f000000000000000-ffffffffffffffff legacy IO memory pointers
32 * Technically, this should be 'if (VMALLOC_START < addr < VMALLOC_END),
33 * but that's slow and we know it'll be within the first 2GB.
35 #ifdef CONFIG_64BIT
36 #define INDIRECT_ADDR(addr) (((unsigned long)(addr) & 1UL<<63) != 0)
37 #define ADDR_TO_REGION(addr) (((unsigned long)addr >> 60) & 7)
38 #define IOPORT_MAP_BASE (8UL << 60)
39 #else
40 #define INDIRECT_ADDR(addr) (((unsigned long)(addr) & 1UL<<31) != 0)
41 #define ADDR_TO_REGION(addr) (((unsigned long)addr >> 28) & 7)
42 #define IOPORT_MAP_BASE (8UL << 28)
43 #endif
45 struct iomap_ops {
46 unsigned int (*read8)(const void __iomem *);
47 unsigned int (*read16)(const void __iomem *);
48 unsigned int (*read16be)(const void __iomem *);
49 unsigned int (*read32)(const void __iomem *);
50 unsigned int (*read32be)(const void __iomem *);
51 #ifdef CONFIG_64BIT
52 u64 (*read64)(const void __iomem *);
53 u64 (*read64be)(const void __iomem *);
54 #endif
55 void (*write8)(u8, void __iomem *);
56 void (*write16)(u16, void __iomem *);
57 void (*write16be)(u16, void __iomem *);
58 void (*write32)(u32, void __iomem *);
59 void (*write32be)(u32, void __iomem *);
60 #ifdef CONFIG_64BIT
61 void (*write64)(u64, void __iomem *);
62 void (*write64be)(u64, void __iomem *);
63 #endif
64 void (*read8r)(const void __iomem *, void *, unsigned long);
65 void (*read16r)(const void __iomem *, void *, unsigned long);
66 void (*read32r)(const void __iomem *, void *, unsigned long);
67 void (*write8r)(void __iomem *, const void *, unsigned long);
68 void (*write16r)(void __iomem *, const void *, unsigned long);
69 void (*write32r)(void __iomem *, const void *, unsigned long);
72 /* Generic ioport ops. To be replaced later by specific dino/elroy/wax code */
74 #define ADDR2PORT(addr) ((unsigned long __force)(addr) & 0xffffff)
76 static unsigned int ioport_read8(const void __iomem *addr)
78 return inb(ADDR2PORT(addr));
81 static unsigned int ioport_read16(const void __iomem *addr)
83 return inw(ADDR2PORT(addr));
86 static unsigned int ioport_read32(const void __iomem *addr)
88 return inl(ADDR2PORT(addr));
91 static void ioport_write8(u8 datum, void __iomem *addr)
93 outb(datum, ADDR2PORT(addr));
96 static void ioport_write16(u16 datum, void __iomem *addr)
98 outw(datum, ADDR2PORT(addr));
101 static void ioport_write32(u32 datum, void __iomem *addr)
103 outl(datum, ADDR2PORT(addr));
106 static void ioport_read8r(const void __iomem *addr, void *dst, unsigned long count)
108 insb(ADDR2PORT(addr), dst, count);
111 static void ioport_read16r(const void __iomem *addr, void *dst, unsigned long count)
113 insw(ADDR2PORT(addr), dst, count);
116 static void ioport_read32r(const void __iomem *addr, void *dst, unsigned long count)
118 insl(ADDR2PORT(addr), dst, count);
121 static void ioport_write8r(void __iomem *addr, const void *s, unsigned long n)
123 outsb(ADDR2PORT(addr), s, n);
126 static void ioport_write16r(void __iomem *addr, const void *s, unsigned long n)
128 outsw(ADDR2PORT(addr), s, n);
131 static void ioport_write32r(void __iomem *addr, const void *s, unsigned long n)
133 outsl(ADDR2PORT(addr), s, n);
136 static const struct iomap_ops ioport_ops = {
137 .read8 = ioport_read8,
138 .read16 = ioport_read16,
139 .read16be = ioport_read16,
140 .read32 = ioport_read32,
141 .read32be = ioport_read32,
142 .write8 = ioport_write8,
143 .write16 = ioport_write16,
144 .write16be = ioport_write16,
145 .write32 = ioport_write32,
146 .write32be = ioport_write32,
147 .read8r = ioport_read8r,
148 .read16r = ioport_read16r,
149 .read32r = ioport_read32r,
150 .write8r = ioport_write8r,
151 .write16r = ioport_write16r,
152 .write32r = ioport_write32r,
155 /* Legacy I/O memory ops */
157 static unsigned int iomem_read8(const void __iomem *addr)
159 return readb(addr);
162 static unsigned int iomem_read16(const void __iomem *addr)
164 return readw(addr);
167 static unsigned int iomem_read16be(const void __iomem *addr)
169 return __raw_readw(addr);
172 static unsigned int iomem_read32(const void __iomem *addr)
174 return readl(addr);
177 static unsigned int iomem_read32be(const void __iomem *addr)
179 return __raw_readl(addr);
182 #ifdef CONFIG_64BIT
183 static u64 iomem_read64(const void __iomem *addr)
185 return readq(addr);
188 static u64 iomem_read64be(const void __iomem *addr)
190 return __raw_readq(addr);
192 #endif
194 static void iomem_write8(u8 datum, void __iomem *addr)
196 writeb(datum, addr);
199 static void iomem_write16(u16 datum, void __iomem *addr)
201 writew(datum, addr);
204 static void iomem_write16be(u16 datum, void __iomem *addr)
206 __raw_writew(datum, addr);
209 static void iomem_write32(u32 datum, void __iomem *addr)
211 writel(datum, addr);
214 static void iomem_write32be(u32 datum, void __iomem *addr)
216 __raw_writel(datum, addr);
219 #ifdef CONFIG_64BIT
220 static void iomem_write64(u64 datum, void __iomem *addr)
222 writeq(datum, addr);
225 static void iomem_write64be(u64 datum, void __iomem *addr)
227 __raw_writeq(datum, addr);
229 #endif
231 static void iomem_read8r(const void __iomem *addr, void *dst, unsigned long count)
233 while (count--) {
234 *(u8 *)dst = __raw_readb(addr);
235 dst++;
239 static void iomem_read16r(const void __iomem *addr, void *dst, unsigned long count)
241 while (count--) {
242 *(u16 *)dst = __raw_readw(addr);
243 dst += 2;
247 static void iomem_read32r(const void __iomem *addr, void *dst, unsigned long count)
249 while (count--) {
250 *(u32 *)dst = __raw_readl(addr);
251 dst += 4;
255 static void iomem_write8r(void __iomem *addr, const void *s, unsigned long n)
257 while (n--) {
258 __raw_writeb(*(u8 *)s, addr);
259 s++;
263 static void iomem_write16r(void __iomem *addr, const void *s, unsigned long n)
265 while (n--) {
266 __raw_writew(*(u16 *)s, addr);
267 s += 2;
271 static void iomem_write32r(void __iomem *addr, const void *s, unsigned long n)
273 while (n--) {
274 __raw_writel(*(u32 *)s, addr);
275 s += 4;
279 static const struct iomap_ops iomem_ops = {
280 .read8 = iomem_read8,
281 .read16 = iomem_read16,
282 .read16be = iomem_read16be,
283 .read32 = iomem_read32,
284 .read32be = iomem_read32be,
285 #ifdef CONFIG_64BIT
286 .read64 = iomem_read64,
287 .read64be = iomem_read64be,
288 #endif
289 .write8 = iomem_write8,
290 .write16 = iomem_write16,
291 .write16be = iomem_write16be,
292 .write32 = iomem_write32,
293 .write32be = iomem_write32be,
294 #ifdef CONFIG_64BIT
295 .write64 = iomem_write64,
296 .write64be = iomem_write64be,
297 #endif
298 .read8r = iomem_read8r,
299 .read16r = iomem_read16r,
300 .read32r = iomem_read32r,
301 .write8r = iomem_write8r,
302 .write16r = iomem_write16r,
303 .write32r = iomem_write32r,
306 static const struct iomap_ops *iomap_ops[8] = {
307 [0] = &ioport_ops,
308 [7] = &iomem_ops
312 unsigned int ioread8(const void __iomem *addr)
314 if (unlikely(INDIRECT_ADDR(addr)))
315 return iomap_ops[ADDR_TO_REGION(addr)]->read8(addr);
316 return *((u8 *)addr);
319 unsigned int ioread16(const void __iomem *addr)
321 if (unlikely(INDIRECT_ADDR(addr)))
322 return iomap_ops[ADDR_TO_REGION(addr)]->read16(addr);
323 return le16_to_cpup((u16 *)addr);
326 unsigned int ioread16be(const void __iomem *addr)
328 if (unlikely(INDIRECT_ADDR(addr)))
329 return iomap_ops[ADDR_TO_REGION(addr)]->read16be(addr);
330 return *((u16 *)addr);
333 unsigned int ioread32(const void __iomem *addr)
335 if (unlikely(INDIRECT_ADDR(addr)))
336 return iomap_ops[ADDR_TO_REGION(addr)]->read32(addr);
337 return le32_to_cpup((u32 *)addr);
340 unsigned int ioread32be(const void __iomem *addr)
342 if (unlikely(INDIRECT_ADDR(addr)))
343 return iomap_ops[ADDR_TO_REGION(addr)]->read32be(addr);
344 return *((u32 *)addr);
347 #ifdef CONFIG_64BIT
348 u64 ioread64(const void __iomem *addr)
350 if (unlikely(INDIRECT_ADDR(addr)))
351 return iomap_ops[ADDR_TO_REGION(addr)]->read64(addr);
352 return le64_to_cpup((u64 *)addr);
355 u64 ioread64be(const void __iomem *addr)
357 if (unlikely(INDIRECT_ADDR(addr)))
358 return iomap_ops[ADDR_TO_REGION(addr)]->read64be(addr);
359 return *((u64 *)addr);
361 #endif
363 void iowrite8(u8 datum, void __iomem *addr)
365 if (unlikely(INDIRECT_ADDR(addr))) {
366 iomap_ops[ADDR_TO_REGION(addr)]->write8(datum, addr);
367 } else {
368 *((u8 *)addr) = datum;
372 void iowrite16(u16 datum, void __iomem *addr)
374 if (unlikely(INDIRECT_ADDR(addr))) {
375 iomap_ops[ADDR_TO_REGION(addr)]->write16(datum, addr);
376 } else {
377 *((u16 *)addr) = cpu_to_le16(datum);
381 void iowrite16be(u16 datum, void __iomem *addr)
383 if (unlikely(INDIRECT_ADDR(addr))) {
384 iomap_ops[ADDR_TO_REGION(addr)]->write16be(datum, addr);
385 } else {
386 *((u16 *)addr) = datum;
390 void iowrite32(u32 datum, void __iomem *addr)
392 if (unlikely(INDIRECT_ADDR(addr))) {
393 iomap_ops[ADDR_TO_REGION(addr)]->write32(datum, addr);
394 } else {
395 *((u32 *)addr) = cpu_to_le32(datum);
399 void iowrite32be(u32 datum, void __iomem *addr)
401 if (unlikely(INDIRECT_ADDR(addr))) {
402 iomap_ops[ADDR_TO_REGION(addr)]->write32be(datum, addr);
403 } else {
404 *((u32 *)addr) = datum;
408 #ifdef CONFIG_64BIT
409 void iowrite64(u64 datum, void __iomem *addr)
411 if (unlikely(INDIRECT_ADDR(addr))) {
412 iomap_ops[ADDR_TO_REGION(addr)]->write64(datum, addr);
413 } else {
414 *((u64 *)addr) = cpu_to_le64(datum);
418 void iowrite64be(u64 datum, void __iomem *addr)
420 if (unlikely(INDIRECT_ADDR(addr))) {
421 iomap_ops[ADDR_TO_REGION(addr)]->write64be(datum, addr);
422 } else {
423 *((u64 *)addr) = datum;
426 #endif
428 /* Repeating interfaces */
430 void ioread8_rep(const void __iomem *addr, void *dst, unsigned long count)
432 if (unlikely(INDIRECT_ADDR(addr))) {
433 iomap_ops[ADDR_TO_REGION(addr)]->read8r(addr, dst, count);
434 } else {
435 while (count--) {
436 *(u8 *)dst = *(u8 *)addr;
437 dst++;
442 void ioread16_rep(const void __iomem *addr, void *dst, unsigned long count)
444 if (unlikely(INDIRECT_ADDR(addr))) {
445 iomap_ops[ADDR_TO_REGION(addr)]->read16r(addr, dst, count);
446 } else {
447 while (count--) {
448 *(u16 *)dst = *(u16 *)addr;
449 dst += 2;
454 void ioread32_rep(const void __iomem *addr, void *dst, unsigned long count)
456 if (unlikely(INDIRECT_ADDR(addr))) {
457 iomap_ops[ADDR_TO_REGION(addr)]->read32r(addr, dst, count);
458 } else {
459 while (count--) {
460 *(u32 *)dst = *(u32 *)addr;
461 dst += 4;
466 void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
468 if (unlikely(INDIRECT_ADDR(addr))) {
469 iomap_ops[ADDR_TO_REGION(addr)]->write8r(addr, src, count);
470 } else {
471 while (count--) {
472 *(u8 *)addr = *(u8 *)src;
473 src++;
478 void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
480 if (unlikely(INDIRECT_ADDR(addr))) {
481 iomap_ops[ADDR_TO_REGION(addr)]->write16r(addr, src, count);
482 } else {
483 while (count--) {
484 *(u16 *)addr = *(u16 *)src;
485 src += 2;
490 void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
492 if (unlikely(INDIRECT_ADDR(addr))) {
493 iomap_ops[ADDR_TO_REGION(addr)]->write32r(addr, src, count);
494 } else {
495 while (count--) {
496 *(u32 *)addr = *(u32 *)src;
497 src += 4;
502 /* Mapping interfaces */
504 void __iomem *ioport_map(unsigned long port, unsigned int nr)
506 return (void __iomem *)(IOPORT_MAP_BASE | port);
509 void ioport_unmap(void __iomem *addr)
511 if (!INDIRECT_ADDR(addr)) {
512 iounmap(addr);
516 #ifdef CONFIG_PCI
517 void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
519 if (!INDIRECT_ADDR(addr)) {
520 iounmap(addr);
523 EXPORT_SYMBOL(pci_iounmap);
524 #endif
526 EXPORT_SYMBOL(ioread8);
527 EXPORT_SYMBOL(ioread16);
528 EXPORT_SYMBOL(ioread16be);
529 EXPORT_SYMBOL(ioread32);
530 EXPORT_SYMBOL(ioread32be);
531 #ifdef CONFIG_64BIT
532 EXPORT_SYMBOL(ioread64);
533 EXPORT_SYMBOL(ioread64be);
534 #endif
535 EXPORT_SYMBOL(iowrite8);
536 EXPORT_SYMBOL(iowrite16);
537 EXPORT_SYMBOL(iowrite16be);
538 EXPORT_SYMBOL(iowrite32);
539 EXPORT_SYMBOL(iowrite32be);
540 #ifdef CONFIG_64BIT
541 EXPORT_SYMBOL(iowrite64);
542 EXPORT_SYMBOL(iowrite64be);
543 #endif
544 EXPORT_SYMBOL(ioread8_rep);
545 EXPORT_SYMBOL(ioread16_rep);
546 EXPORT_SYMBOL(ioread32_rep);
547 EXPORT_SYMBOL(iowrite8_rep);
548 EXPORT_SYMBOL(iowrite16_rep);
549 EXPORT_SYMBOL(iowrite32_rep);
550 EXPORT_SYMBOL(ioport_map);
551 EXPORT_SYMBOL(ioport_unmap);