patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / include / asm-ppc / io.h
1 #ifdef __KERNEL__
2 #ifndef _PPC_IO_H
3 #define _PPC_IO_H
4
5 #include <linux/config.h>
6 #include <linux/types.h>
7 #include <linux/mm.h>
8
9 #include <asm/page.h>
10 #include <asm/byteorder.h>
11 #include <asm/mmu.h>
12
13 #define SIO_CONFIG_RA   0x398
14 #define SIO_CONFIG_RD   0x399
15
16 #define SLOW_DOWN_IO
17
18 #define PMAC_ISA_MEM_BASE       0
19 #define PMAC_PCI_DRAM_OFFSET    0
20 #define CHRP_ISA_IO_BASE        0xf8000000
21 #define CHRP_ISA_MEM_BASE       0xf7000000
22 #define CHRP_PCI_DRAM_OFFSET    0
23 #define PREP_ISA_IO_BASE        0x80000000
24 #define PREP_ISA_MEM_BASE       0xc0000000
25 #define PREP_PCI_DRAM_OFFSET    0x80000000
26
27 #if defined(CONFIG_4xx)
28 #include <asm/ibm4xx.h>
29 #elif defined(CONFIG_8xx)
30 #include <asm/mpc8xx.h>
31 #elif defined(CONFIG_8260)
32 #include <asm/mpc8260.h>
33 #elif defined(CONFIG_APUS)
34 #define _IO_BASE        0
35 #define _ISA_MEM_BASE   0
36 #define PCI_DRAM_OFFSET 0
37 #else /* Everyone else */
38 #define _IO_BASE        isa_io_base
39 #define _ISA_MEM_BASE   isa_mem_base
40 #define PCI_DRAM_OFFSET pci_dram_offset
41 #endif /* Platform-dependent I/O */
42
43 extern unsigned long isa_io_base;
44 extern unsigned long isa_mem_base;
45 extern unsigned long pci_dram_offset;
46
47 #define readb(addr) in_8((volatile u8 *)(addr))
48 #define writeb(b,addr) out_8((volatile u8 *)(addr), (b))
49 #if defined(CONFIG_APUS)
50 #define readw(addr) (*(volatile u16 *) (addr))
51 #define readl(addr) (*(volatile u32 *) (addr))
52 #define writew(b,addr) ((*(volatile u16 *) (addr)) = (b))
53 #define writel(b,addr) ((*(volatile u32 *) (addr)) = (b))
54 #else
55 #define readw(addr) in_le16((volatile u16 *)(addr))
56 #define readl(addr) in_le32((volatile u32 *)(addr))
57 #define writew(b,addr) out_le16((volatile u16 *)(addr),(b))
58 #define writel(b,addr) out_le32((volatile u32 *)(addr),(b))
59 #endif /* CONFIG_APUS */
60
61 #define readb_relaxed(addr) readb(addr)
62 #define readw_relaxed(addr) readw(addr)
63 #define readl_relaxed(addr) readl(addr)
64
65 #define __raw_readb(addr)       (*(volatile unsigned char *)(addr))
66 #define __raw_readw(addr)       (*(volatile unsigned short *)(addr))
67 #define __raw_readl(addr)       (*(volatile unsigned int *)(addr))
68 #define __raw_writeb(v, addr)   (*(volatile unsigned char *)(addr) = (v))
69 #define __raw_writew(v, addr)   (*(volatile unsigned short *)(addr) = (v))
70 #define __raw_writel(v, addr)   (*(volatile unsigned int *)(addr) = (v))
71
72 /*
73  * The insw/outsw/insl/outsl macros don't do byte-swapping.
74  * They are only used in practice for transferring buffers which
75  * are arrays of bytes, and byte-swapping is not appropriate in
76  * that case.  - paulus
77  */
78 #define insb(port, buf, ns)     _insb((u8 *)((port)+_IO_BASE), (buf), (ns))
79 #define outsb(port, buf, ns)    _outsb((u8 *)((port)+_IO_BASE), (buf), (ns))
80 #define insw(port, buf, ns)     _insw_ns((u16 *)((port)+_IO_BASE), (buf), (ns))
81 #define outsw(port, buf, ns)    _outsw_ns((u16 *)((port)+_IO_BASE), (buf), (ns))
82 #define insl(port, buf, nl)     _insl_ns((u32 *)((port)+_IO_BASE), (buf), (nl))
83 #define outsl(port, buf, nl)    _outsl_ns((u32 *)((port)+_IO_BASE), (buf), (nl))
84
85 /*
86  * On powermacs, we will get a machine check exception if we
87  * try to read data from a non-existent I/O port.  Because the
88  * machine check is an asynchronous exception, it isn't
89  * well-defined which instruction SRR0 will point to when the
90  * exception occurs.
91  * With the sequence below (twi; isync; nop), we have found that
92  * the machine check occurs on one of the three instructions on
93  * all PPC implementations tested so far.  The twi and isync are
94  * needed on the 601 (in fact twi; sync works too), the isync and
95  * nop are needed on 604[e|r], and any of twi, sync or isync will
96  * work on 603[e], 750, 74xx.
97  * The twi creates an explicit data dependency on the returned
98  * value which seems to be needed to make the 601 wait for the
99  * load to finish.
100  */
101
102 #define __do_in_asm(name, op)                           \
103 extern __inline__ unsigned int name(unsigned int port)  \
104 {                                                       \
105         unsigned int x;                                 \
106         __asm__ __volatile__(                           \
107                         op "    %0,0,%1\n"              \
108                 "1:     twi     0,%0,0\n"               \
109                 "2:     isync\n"                        \
110                 "3:     nop\n"                          \
111                 "4:\n"                                  \
112                 ".section .fixup,\"ax\"\n"              \
113                 "5:     li      %0,-1\n"                \
114                 "       b       4b\n"                   \
115                 ".previous\n"                           \
116                 ".section __ex_table,\"a\"\n"           \
117                 "       .align  2\n"                    \
118                 "       .long   1b,5b\n"                \
119                 "       .long   2b,5b\n"                \
120                 "       .long   3b,5b\n"                \
121                 ".previous"                             \
122                 : "=&r" (x)                             \
123                 : "r" (port + _IO_BASE));               \
124         return x;                                       \
125 }
126
127 #define __do_out_asm(name, op)                          \
128 extern __inline__ void name(unsigned int val, unsigned int port) \
129 {                                                       \
130         __asm__ __volatile__(                           \
131                 op " %0,0,%1\n"                         \
132                 "1:     sync\n"                         \
133                 "2:\n"                                  \
134                 ".section __ex_table,\"a\"\n"           \
135                 "       .align  2\n"                    \
136                 "       .long   1b,2b\n"                \
137                 ".previous"                             \
138                 : : "r" (val), "r" (port + _IO_BASE));  \
139 }
140
141 __do_in_asm(inb, "lbzx")
142 __do_out_asm(outb, "stbx")
143 #ifdef CONFIG_APUS
144 __do_in_asm(inw, "lhz%U1%X1")
145 __do_in_asm(inl, "lwz%U1%X1")
146 __do_out_asm(outl,"stw%U0%X0")
147 __do_out_asm(outw, "sth%U0%X0")
148 #else
149 __do_in_asm(inw, "lhbrx")
150 __do_in_asm(inl, "lwbrx")
151 __do_out_asm(outw, "sthbrx")
152 __do_out_asm(outl, "stwbrx")
153 #endif
154
155 #define inb_p(port)             inb((port))
156 #define outb_p(val, port)       outb((val), (port))
157 #define inw_p(port)             inw((port))
158 #define outw_p(val, port)       outw((val), (port))
159 #define inl_p(port)             inl((port))
160 #define outl_p(val, port)       outl((val), (port))
161
162 extern void _insb(volatile u8 *port, void *buf, int ns);
163 extern void _outsb(volatile u8 *port, const void *buf, int ns);
164 extern void _insw(volatile u16 *port, void *buf, int ns);
165 extern void _outsw(volatile u16 *port, const void *buf, int ns);
166 extern void _insl(volatile u32 *port, void *buf, int nl);
167 extern void _outsl(volatile u32 *port, const void *buf, int nl);
168 extern void _insw_ns(volatile u16 *port, void *buf, int ns);
169 extern void _outsw_ns(volatile u16 *port, const void *buf, int ns);
170 extern void _insl_ns(volatile u32 *port, void *buf, int nl);
171 extern void _outsl_ns(volatile u32 *port, const void *buf, int nl);
172
173 /*
174  * The *_ns versions below don't do byte-swapping.
175  * Neither do the standard versions now, these are just here
176  * for older code.
177  */
178 #define insw_ns(port, buf, ns)  _insw_ns((u16 *)((port)+_IO_BASE), (buf), (ns))
179 #define outsw_ns(port, buf, ns) _outsw_ns((u16 *)((port)+_IO_BASE), (buf), (ns))
180 #define insl_ns(port, buf, nl)  _insl_ns((u32 *)((port)+_IO_BASE), (buf), (nl))
181 #define outsl_ns(port, buf, nl) _outsl_ns((u32 *)((port)+_IO_BASE), (buf), (nl))
182
183
184 #define IO_SPACE_LIMIT ~0
185
186 #define memset_io(a,b,c)       memset((void *)(a),(b),(c))
187 #define memcpy_fromio(a,b,c)   memcpy((a),(void *)(b),(c))
188 #define memcpy_toio(a,b,c)      memcpy((void *)(a),(b),(c))
189
190 /*
191  * Map in an area of physical address space, for accessing
192  * I/O devices etc.
193  */
194 extern void *__ioremap(phys_addr_t address, unsigned long size,
195                        unsigned long flags);
196 extern void *ioremap(phys_addr_t address, unsigned long size);
197 #ifdef CONFIG_44x
198 extern void *ioremap64(unsigned long long address, unsigned long size);
199 #endif
200 #define ioremap_nocache(addr, size)     ioremap((addr), (size))
201 extern void iounmap(void *addr);
202 extern unsigned long iopa(unsigned long addr);
203 extern unsigned long mm_ptov(unsigned long addr) __attribute_const__;
204 extern void io_block_mapping(unsigned long virt, phys_addr_t phys,
205                              unsigned int size, int flags);
206
207 /*
208  * The PCI bus is inherently Little-Endian.  The PowerPC is being
209  * run Big-Endian.  Thus all values which cross the [PCI] barrier
210  * must be endian-adjusted.  Also, the local DRAM has a different
211  * address from the PCI point of view, thus buffer addresses also
212  * have to be modified [mapped] appropriately.
213  */
214 extern inline unsigned long virt_to_bus(volatile void * address)
215 {
216 #ifndef CONFIG_APUS
217         if (address == (void *)0)
218                 return 0;
219         return (unsigned long)address - KERNELBASE + PCI_DRAM_OFFSET;
220 #else
221         return iopa ((unsigned long) address);
222 #endif
223 }
224
225 extern inline void * bus_to_virt(unsigned long address)
226 {
227 #ifndef CONFIG_APUS
228         if (address == 0)
229                 return 0;
230         return (void *)(address - PCI_DRAM_OFFSET + KERNELBASE);
231 #else
232         return (void*) mm_ptov (address);
233 #endif
234 }
235
236 /*
237  * Change virtual addresses to physical addresses and vv, for
238  * addresses in the area where the kernel has the RAM mapped.
239  */
240 extern inline unsigned long virt_to_phys(volatile void * address)
241 {
242 #ifndef CONFIG_APUS
243         return (unsigned long) address - KERNELBASE;
244 #else
245         return iopa ((unsigned long) address);
246 #endif
247 }
248
249 extern inline void * phys_to_virt(unsigned long address)
250 {
251 #ifndef CONFIG_APUS
252         return (void *) (address + KERNELBASE);
253 #else
254         return (void*) mm_ptov (address);
255 #endif
256 }
257
258 /*
259  * Change "struct page" to physical address.
260  */
261 #define page_to_phys(page)      (page_to_pfn(page) << PAGE_SHIFT)
262 #define page_to_bus(page)       (page_to_phys(page) + PCI_DRAM_OFFSET)
263
264 /*
265  * Enforce In-order Execution of I/O:
266  * Acts as a barrier to ensure all previous I/O accesses have
267  * completed before any further ones are issued.
268  */
269 extern inline void eieio(void)
270 {
271         __asm__ __volatile__ ("eieio" : : : "memory");
272 }
273
274 /* Enforce in-order execution of data I/O.
275  * No distinction between read/write on PPC; use eieio for all three.
276  */
277 #define iobarrier_rw() eieio()
278 #define iobarrier_r()  eieio()
279 #define iobarrier_w()  eieio()
280
281 /*
282  * 8, 16 and 32 bit, big and little endian I/O operations, with barrier.
283  *
284  * Read operations have additional twi & isync to make sure the read
285  * is actually performed (i.e. the data has come back) before we start
286  * executing any following instructions.
287  */
288 extern inline int in_8(volatile unsigned char *addr)
289 {
290         int ret;
291
292         __asm__ __volatile__(
293                 "lbz%U1%X1 %0,%1;\n"
294                 "twi 0,%0,0;\n"
295                 "isync" : "=r" (ret) : "m" (*addr));
296         return ret;
297 }
298
299 extern inline void out_8(volatile unsigned char *addr, int val)
300 {
301         __asm__ __volatile__("stb%U0%X0 %1,%0; eieio" : "=m" (*addr) : "r" (val));
302 }
303
304 extern inline int in_le16(volatile unsigned short *addr)
305 {
306         int ret;
307
308         __asm__ __volatile__("lhbrx %0,0,%1;\n"
309                              "twi 0,%0,0;\n"
310                              "isync" : "=r" (ret) :
311                               "r" (addr), "m" (*addr));
312         return ret;
313 }
314
315 extern inline int in_be16(volatile unsigned short *addr)
316 {
317         int ret;
318
319         __asm__ __volatile__("lhz%U1%X1 %0,%1;\n"
320                              "twi 0,%0,0;\n"
321                              "isync" : "=r" (ret) : "m" (*addr));
322         return ret;
323 }
324
325 extern inline void out_le16(volatile unsigned short *addr, int val)
326 {
327         __asm__ __volatile__("sthbrx %1,0,%2; eieio" : "=m" (*addr) :
328                               "r" (val), "r" (addr));
329 }
330
331 extern inline void out_be16(volatile unsigned short *addr, int val)
332 {
333         __asm__ __volatile__("sth%U0%X0 %1,%0; eieio" : "=m" (*addr) : "r" (val));
334 }
335
336 extern inline unsigned in_le32(volatile unsigned *addr)
337 {
338         unsigned ret;
339
340         __asm__ __volatile__("lwbrx %0,0,%1;\n"
341                              "twi 0,%0,0;\n"
342                              "isync" : "=r" (ret) :
343                              "r" (addr), "m" (*addr));
344         return ret;
345 }
346
347 extern inline unsigned in_be32(volatile unsigned *addr)
348 {
349         unsigned ret;
350
351         __asm__ __volatile__("lwz%U1%X1 %0,%1;\n"
352                              "twi 0,%0,0;\n"
353                              "isync" : "=r" (ret) : "m" (*addr));
354         return ret;
355 }
356
357 extern inline void out_le32(volatile unsigned *addr, int val)
358 {
359         __asm__ __volatile__("stwbrx %1,0,%2; eieio" : "=m" (*addr) :
360                              "r" (val), "r" (addr));
361 }
362
363 extern inline void out_be32(volatile unsigned *addr, int val)
364 {
365         __asm__ __volatile__("stw%U0%X0 %1,%0; eieio" : "=m" (*addr) : "r" (val));
366 }
367
368 static inline int check_signature(unsigned long io_addr,
369         const unsigned char *signature, int length)
370 {
371         int retval = 0;
372         do {
373                 if (readb(io_addr) != *signature)
374                         goto out;
375                 io_addr++;
376                 signature++;
377                 length--;
378         } while (length);
379         retval = 1;
380 out:
381         return retval;
382 }
383
384 /* Make some pcmcia drivers happy */
385 static inline int isa_check_signature(unsigned long io_addr,
386         const unsigned char *signature, int length)
387 {
388         return 0;
389 }
390
391 #endif /* _PPC_IO_H */
392 #endif /* __KERNEL__ */