1 // SPDX-License-Identifier: GPL-2.0
3 * iomap.c - Implement iomap interface for PA-RISC
4 * Copyright (c) 2004 Matthew Wilcox
7 #include <linux/ioport.h>
9 #include <linux/export.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.
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)
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)
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
*);
52 u64 (*read64
)(const void __iomem
*);
53 u64 (*read64be
)(const void __iomem
*);
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
*);
61 void (*write64
)(u64
, void __iomem
*);
62 void (*write64be
)(u64
, void __iomem
*);
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
)
162 static unsigned int iomem_read16(const void __iomem
*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
)
177 static unsigned int iomem_read32be(const void __iomem
*addr
)
179 return __raw_readl(addr
);
183 static u64
iomem_read64(const void __iomem
*addr
)
188 static u64
iomem_read64be(const void __iomem
*addr
)
190 return __raw_readq(addr
);
194 static void iomem_write8(u8 datum
, void __iomem
*addr
)
199 static void iomem_write16(u16 datum
, void __iomem
*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
)
214 static void iomem_write32be(u32 datum
, void __iomem
*addr
)
216 __raw_writel(datum
, addr
);
220 static void iomem_write64(u64 datum
, void __iomem
*addr
)
225 static void iomem_write64be(u64 datum
, void __iomem
*addr
)
227 __raw_writeq(datum
, addr
);
231 static void iomem_read8r(const void __iomem
*addr
, void *dst
, unsigned long count
)
234 *(u8
*)dst
= __raw_readb(addr
);
239 static void iomem_read16r(const void __iomem
*addr
, void *dst
, unsigned long count
)
242 *(u16
*)dst
= __raw_readw(addr
);
247 static void iomem_read32r(const void __iomem
*addr
, void *dst
, unsigned long count
)
250 *(u32
*)dst
= __raw_readl(addr
);
255 static void iomem_write8r(void __iomem
*addr
, const void *s
, unsigned long n
)
258 __raw_writeb(*(u8
*)s
, addr
);
263 static void iomem_write16r(void __iomem
*addr
, const void *s
, unsigned long n
)
266 __raw_writew(*(u16
*)s
, addr
);
271 static void iomem_write32r(void __iomem
*addr
, const void *s
, unsigned long n
)
274 __raw_writel(*(u32
*)s
, addr
);
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
,
286 .read64
= iomem_read64
,
287 .read64be
= iomem_read64be
,
289 .write8
= iomem_write8
,
290 .write16
= iomem_write16
,
291 .write16be
= iomem_write16be
,
292 .write32
= iomem_write32
,
293 .write32be
= iomem_write32be
,
295 .write64
= iomem_write64
,
296 .write64be
= iomem_write64be
,
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] = {
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
);
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
);
363 void iowrite8(u8 datum
, void __iomem
*addr
)
365 if (unlikely(INDIRECT_ADDR(addr
))) {
366 iomap_ops
[ADDR_TO_REGION(addr
)]->write8(datum
, addr
);
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
);
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
);
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
);
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
);
404 *((u32
*)addr
) = datum
;
409 void iowrite64(u64 datum
, void __iomem
*addr
)
411 if (unlikely(INDIRECT_ADDR(addr
))) {
412 iomap_ops
[ADDR_TO_REGION(addr
)]->write64(datum
, addr
);
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
);
423 *((u64
*)addr
) = datum
;
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
);
436 *(u8
*)dst
= *(u8
*)addr
;
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
);
448 *(u16
*)dst
= *(u16
*)addr
;
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
);
460 *(u32
*)dst
= *(u32
*)addr
;
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
);
472 *(u8
*)addr
= *(u8
*)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
);
484 *(u16
*)addr
= *(u16
*)src
;
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
);
496 *(u32
*)addr
= *(u32
*)src
;
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
)) {
517 void pci_iounmap(struct pci_dev
*dev
, void __iomem
* addr
)
519 if (!INDIRECT_ADDR(addr
)) {
523 EXPORT_SYMBOL(pci_iounmap
);
526 EXPORT_SYMBOL(ioread8
);
527 EXPORT_SYMBOL(ioread16
);
528 EXPORT_SYMBOL(ioread16be
);
529 EXPORT_SYMBOL(ioread32
);
530 EXPORT_SYMBOL(ioread32be
);
532 EXPORT_SYMBOL(ioread64
);
533 EXPORT_SYMBOL(ioread64be
);
535 EXPORT_SYMBOL(iowrite8
);
536 EXPORT_SYMBOL(iowrite16
);
537 EXPORT_SYMBOL(iowrite16be
);
538 EXPORT_SYMBOL(iowrite32
);
539 EXPORT_SYMBOL(iowrite32be
);
541 EXPORT_SYMBOL(iowrite64
);
542 EXPORT_SYMBOL(iowrite64be
);
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
);