1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3 * IO header file
4 *
5 * Copyright (C) 2001-2007 Tensilica Inc.
6 * Based on the Linux/Xtensa version of this header.
7 */
8
9 #ifndef _XTENSA_IO_H
10 #define _XTENSA_IO_H
11
12 #include <linux/types.h>
13 #include <asm/byteorder.h>
14
15 #include <asm/addrspace.h>
16
17 /*
18 * swap functions to change byte order from little-endian to big-endian and
19 * vice versa.
20 */
21
_swapw(unsigned short v)22 static inline unsigned short _swapw(unsigned short v)
23 {
24 return (v << 8) | (v >> 8);
25 }
26
_swapl(unsigned int v)27 static inline unsigned int _swapl(unsigned int v)
28 {
29 return (v << 24) | ((v & 0xff00) << 8) |
30 ((v >> 8) & 0xff00) | (v >> 24);
31 }
32
33 /*
34 * Generic I/O
35 */
36
37 #define readb(addr) \
38 ({ unsigned char __v = (*(volatile unsigned char *)(addr)); __v; })
39 #define readw(addr) \
40 ({ unsigned short __v = (*(volatile unsigned short *)(addr)); __v; })
41 #define readl(addr) \
42 ({ unsigned int __v = (*(volatile unsigned int *)(addr)); __v; })
43 #define writeb(b, addr) (void)((*(volatile unsigned char *)(addr)) = (b))
44 #define writew(b, addr) (void)((*(volatile unsigned short *)(addr)) = (b))
45 #define writel(b, addr) (void)((*(volatile unsigned int *)(addr)) = (b))
46
47 #define __raw_readb readb
48 #define __raw_readw readw
49 #define __raw_readl readl
50 #define __raw_writeb writeb
51 #define __raw_writew writew
52 #define __raw_writel writel
53
54 /* These are the definitions for the x86 IO instructions
55 * inb/inw/inl/outb/outw/outl, the "string" versions
56 * insb/insw/insl/outsb/outsw/outsl, and the "pausing" versions
57 * inb_p/inw_p/...
58 * The macros don't do byte-swapping.
59 */
60
61 #define inb(port) readb((u8 *)((port)))
62 #define outb(val, port) writeb((val), (u8 *)((unsigned long)(port)))
63 #define inw(port) readw((u16 *)((port)))
64 #define outw(val, port) writew((val), (u16 *)((unsigned long)(port)))
65 #define inl(port) readl((u32 *)((port)))
66 #define outl(val, port) writel((val), (u32 *)((unsigned long)(port)))
67
68 #define inb_p(port) inb((port))
69 #define outb_p(val, port) outb((val), (port))
70 #define inw_p(port) inw((port))
71 #define outw_p(val, port) outw((val), (port))
72 #define inl_p(port) inl((port))
73 #define outl_p(val, port) outl((val), (port))
74
75 void insb(unsigned long port, void *dst, unsigned long count);
76 void insw(unsigned long port, void *dst, unsigned long count);
77 void insl(unsigned long port, void *dst, unsigned long count);
78 void outsb(unsigned long port, const void *src, unsigned long count);
79 void outsw(unsigned long port, const void *src, unsigned long count);
80 void outsl(unsigned long port, const void *src, unsigned long count);
81 #define insb insb
82 #define insw insw
83 #define insl insl
84 #define outsb outsb
85 #define outsw outsw
86 #define outsl outsl
87
88 #define IO_SPACE_LIMIT ~0
89
90 #define memset_io(a, b, c) memset((void *)(a), (b), (c))
91 #define memcpy_fromio(a, b, c) memcpy((a), (void *)(b), (c))
92 #define memcpy_toio(a, b, c) memcpy((void *)(a), (b), (c))
93
94 /* At this point the Xtensa doesn't provide byte swap instructions */
95
96 #ifdef __XTENSA_EB__
97 # define in_8(addr) (*(u8 *)(addr))
98 # define in_le16(addr) _swapw(*(u16 *)(addr))
99 # define in_le32(addr) _swapl(*(u32 *)(addr))
100 # define out_8(b, addr) *(u8 *)(addr) = (b)
101 # define out_le16(b, addr) *(u16 *)(addr) = _swapw(b)
102 # define out_le32(b, addr) *(u32 *)(addr) = _swapl(b)
103 #elif defined(__XTENSA_EL__)
104 # define in_8(addr) (*(u8 *)(addr))
105 # define in_le16(addr) (*(u16 *)(addr))
106 # define in_le32(addr) (*(u32 *)(addr))
107 # define out_8(b, addr) *(u8 *)(addr) = (b)
108 # define out_le16(b, addr) *(u16 *)(addr) = (b)
109 # define out_le32(b, addr) *(u32 *)(addr) = (b)
110 #else
111 # error processor byte order undefined!
112 #endif
113
114 /*
115 * Convert a physical pointer to a virtual kernel pointer for /dev/mem access
116 */
117 #define xlate_dev_mem_ptr(p) __va(p)
118
119 /*
120 * Convert a virtual cached pointer to an uncached pointer
121 */
122 #define xlate_dev_kmem_ptr(p) p
123
124 /*
125 * Dummy function to keep U-Boot's cfi_flash.c driver happy.
126 */
sync(void)127 static inline void sync(void)
128 {
129 }
130
131 #if XCHAL_HAVE_PTP_MMU
phys_to_virt(phys_addr_t paddr)132 static inline void *phys_to_virt(phys_addr_t paddr)
133 {
134 if (paddr >= CFG_SYS_IO_BASE)
135 return (void *)(unsigned long)paddr;
136
137 if (paddr < CFG_MAX_MEM_MAPPED)
138 return (void *)(unsigned long)MEMADDR(paddr);
139
140 return NULL;
141 }
142
143 #define phys_to_virt phys_to_virt
144
virt_to_phys(void * vaddr)145 static inline phys_addr_t virt_to_phys(void *vaddr)
146 {
147 unsigned long addr = (unsigned long)vaddr;
148
149 if (addr >= CFG_SYS_IO_BASE)
150 return addr;
151
152 if (addr >= CFG_SYS_SDRAM_BASE && addr < MEMADDR(CFG_MAX_MEM_MAPPED))
153 return PHYSADDR(addr);
154
155 return 0;
156 }
157
158 #define virt_to_phys virt_to_phys
159 #endif /* XCHAL_HAVE_PTP_MMU */
160
161 #include <asm-generic/io.h>
162
163 #endif /* _XTENSA_IO_H */
164