1 /*
2  * $Id: io.h,v 1.29 2001/11/10 09:28:34 davem Exp $
3  */
4 #ifndef __SPARC_IO_H
5 #define __SPARC_IO_H
6 
7 #include <linux/kernel.h>
8 #include <linux/types.h>
9 #include <linux/ioport.h>  /* struct resource */
10 
11 #include <asm/page.h>      /* IO address mapping routines need this */
12 #include <asm/system.h>
13 
14 #define virt_to_bus virt_to_phys
15 #define bus_to_virt phys_to_virt
16 #define page_to_phys(page)     ((((page) - mem_map) << PAGE_SHIFT)+phys_base)
17 
flip_dword(u32 d)18 static __inline__ u32 flip_dword (u32 d)
19 {
20 	return ((d&0xff)<<24) | (((d>>8)&0xff)<<16) | (((d>>16)&0xff)<<8)| ((d>>24)&0xff);
21 }
22 
flip_word(u16 d)23 static __inline__ u16 flip_word (u16 d)
24 {
25 	return ((d&0xff) << 8) | ((d>>8)&0xff);
26 }
27 
28 /*
29  * Memory mapped I/O to PCI
30  */
readb(unsigned long addr)31 static __inline__ u8 readb(unsigned long addr)
32 {
33 	return *(volatile u8 *)addr;
34 }
35 
readw(unsigned long addr)36 static __inline__ u16 readw(unsigned long addr)
37 {
38 	return flip_word(*(volatile u16 *)addr);
39 }
40 
readl(unsigned long addr)41 static __inline__ u32 readl(unsigned long addr)
42 {
43 	return flip_dword(*(volatile u32 *)addr);
44 }
45 
writeb(u8 b,unsigned long addr)46 static __inline__ void writeb(u8 b, unsigned long addr)
47 {
48 	*(volatile u8 *)addr = b;
49 }
50 
writew(u16 b,unsigned long addr)51 static __inline__ void writew(u16 b, unsigned long addr)
52 {
53 	*(volatile u16 *)addr = flip_word(b);
54 }
55 
writel(u32 b,unsigned long addr)56 static __inline__ void writel(u32 b, unsigned long addr)
57 {
58 	*(volatile u32 *)addr = flip_dword(b);
59 }
60 
61 /* Now the 'raw' versions. */
__raw_readb(unsigned long addr)62 static __inline__ u8 __raw_readb(unsigned long addr)
63 {
64 	return *(volatile u8 *)addr;
65 }
66 
__raw_readw(unsigned long addr)67 static __inline__ u16 __raw_readw(unsigned long addr)
68 {
69 	return *(volatile u16 *)addr;
70 }
71 
__raw_readl(unsigned long addr)72 static __inline__ u32 __raw_readl(unsigned long addr)
73 {
74 	return *(volatile u32 *)addr;
75 }
76 
__raw_writeb(u8 b,unsigned long addr)77 static __inline__ void __raw_writeb(u8 b, unsigned long addr)
78 {
79 	*(volatile u8 *)addr = b;
80 }
81 
__raw_writew(u16 b,unsigned long addr)82 static __inline__ void __raw_writew(u16 b, unsigned long addr)
83 {
84 	*(volatile u16 *)addr = b;
85 }
86 
__raw_writel(u32 b,unsigned long addr)87 static __inline__ void __raw_writel(u32 b, unsigned long addr)
88 {
89 	*(volatile u32 *)addr = b;
90 }
91 
92 /*
93  * I/O space operations
94  *
95  * Arrangement on a Sun is somewhat complicated.
96  *
97  * First of all, we want to use standard Linux drivers
98  * for keyboard, PC serial, etc. These drivers think
99  * they access I/O space and use inb/outb.
100  * On the other hand, EBus bridge accepts PCI *memory*
101  * cycles and converts them into ISA *I/O* cycles.
102  * Ergo, we want inb & outb to generate PCI memory cycles.
103  *
104  * If we want to issue PCI *I/O* cycles, we do this
105  * with a low 64K fixed window in PCIC. This window gets
106  * mapped somewhere into virtual kernel space and we
107  * can use inb/outb again.
108  */
109 #define inb_local(addr)		readb(addr)
110 #define inb(addr)		readb(addr)
111 #define inw(addr)		readw(addr)
112 #define inl(addr)		readl(addr)
113 #define inb_p(addr)		readb(addr)
114 
115 #define outb_local(b, addr)	writeb(b, addr)
116 #define outb(b, addr)		writeb(b, addr)
117 #define outw(b, addr)		writew(b, addr)
118 #define outl(b, addr)		writel(b, addr)
119 #define outb_p(b, addr)		writeb(b, addr)
120 
121 extern void outsb(unsigned long addr, const void *src, unsigned long cnt);
122 extern void outsw(unsigned long addr, const void *src, unsigned long cnt);
123 extern void outsl(unsigned long addr, const void *src, unsigned long cnt);
124 extern void insb(unsigned long addr, void *dst, unsigned long count);
125 extern void insw(unsigned long addr, void *dst, unsigned long count);
126 extern void insl(unsigned long addr, void *dst, unsigned long count);
127 
128 #define IO_SPACE_LIMIT 0xffffffff
129 
130 /*
131  * SBus accessors.
132  *
133  * SBus has only one, memory mapped, I/O space.
134  * We do not need to flip bytes for SBus of course.
135  */
_sbus_readb(unsigned long addr)136 static __inline__ u8 _sbus_readb(unsigned long addr)
137 {
138 	return *(volatile u8 *)addr;
139 }
140 
_sbus_readw(unsigned long addr)141 static __inline__ u16 _sbus_readw(unsigned long addr)
142 {
143 	return *(volatile u16 *)addr;
144 }
145 
_sbus_readl(unsigned long addr)146 static __inline__ u32 _sbus_readl(unsigned long addr)
147 {
148 	return *(volatile u32 *)addr;
149 }
150 
_sbus_writeb(u8 b,unsigned long addr)151 static __inline__ void _sbus_writeb(u8 b, unsigned long addr)
152 {
153 	*(volatile u8 *)addr = b;
154 }
155 
_sbus_writew(u16 b,unsigned long addr)156 static __inline__ void _sbus_writew(u16 b, unsigned long addr)
157 {
158 	*(volatile u16 *)addr = b;
159 }
160 
_sbus_writel(u32 b,unsigned long addr)161 static __inline__ void _sbus_writel(u32 b, unsigned long addr)
162 {
163 	*(volatile u32 *)addr = b;
164 }
165 
166 /*
167  * The only reason for #define's is to hide casts to unsigned long.
168  * XXX Rewrite drivers without structures for registers.
169  */
170 #define sbus_readb(a)		_sbus_readb((unsigned long)(a))
171 #define sbus_readw(a)		_sbus_readw((unsigned long)(a))
172 #define sbus_readl(a)		_sbus_readl((unsigned long)(a))
173 #define sbus_writeb(v, a)	_sbus_writeb(v, (unsigned long)(a))
174 #define sbus_writew(v, a)	_sbus_writew(v, (unsigned long)(a))
175 #define sbus_writel(v, a)	_sbus_writel(v, (unsigned long)(a))
176 
sbus_memset_io(void * __dst,int c,__kernel_size_t n)177 static inline void *sbus_memset_io(void *__dst, int c, __kernel_size_t n)
178 {
179 	unsigned long dst = (unsigned long)__dst;
180 
181 	while(n--) {
182 		sbus_writeb(c, dst);
183 		dst++;
184 	}
185 	return (void *) dst;
186 }
187 
188 #ifdef __KERNEL__
189 
190 /*
191  * Bus number may be embedded in the higher bits of the physical address.
192  * This is why we have no bus number argument to ioremap().
193  */
194 extern void *ioremap(unsigned long offset, unsigned long size);
195 #define ioremap_nocache(X,Y)	ioremap((X),(Y))
196 extern void iounmap(void *addr);
197 
198 /* P3: talk davem into dropping "name" argument in favor of res->name */
199 /*
200  * Bus number may be in res->flags... somewhere.
201  */
202 extern unsigned long sbus_ioremap(struct resource *res, unsigned long offset,
203     unsigned long size, char *name);
204 /* XXX Partial deallocations? I think not! */
205 extern void sbus_iounmap(unsigned long vaddr, unsigned long size);
206 
207 
208 #define virt_to_phys(x) __pa((unsigned long)(x))
209 #define phys_to_virt(x) __va((unsigned long)(x))
210 
211 /*
212  * At the moment, we do not use CMOS_READ anywhere outside of rtc.c,
213  * so rtc_port is static in it. This should not change unless a new
214  * hardware pops up.
215  */
216 #define RTC_PORT(x)   (rtc_port + (x))
217 #define RTC_ALWAYS_BCD  0
218 
219 /* Nothing to do */
220 /* P3: Only IDE DMA may need these. */
221 
222 #define dma_cache_inv(_start,_size)		do { } while (0)
223 #define dma_cache_wback(_start,_size)		do { } while (0)
224 #define dma_cache_wback_inv(_start,_size)	do { } while (0)
225 
226 #endif
227 
228 #endif /* !(__SPARC_IO_H) */
229