vserver 1.9.5.x5
[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/string.h>
7 #include <linux/types.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_85xx)
34 #include <asm/mpc85xx.h>
35 #elif defined(CONFIG_APUS)
36 #define _IO_BASE        0
37 #define _ISA_MEM_BASE   0
38 #define PCI_DRAM_OFFSET 0
39 #else /* Everyone else */
40 #define _IO_BASE        isa_io_base
41 #define _ISA_MEM_BASE   isa_mem_base
42 #define PCI_DRAM_OFFSET pci_dram_offset
43 #endif /* Platform-dependent I/O */
44
45 #define ___IO_BASE ((void __iomem *)_IO_BASE)
46 extern unsigned long isa_io_base;
47 extern unsigned long isa_mem_base;
48 extern unsigned long pci_dram_offset;
49
50 /*
51  * 8, 16 and 32 bit, big and little endian I/O operations, with barrier.
52  *
53  * Read operations have additional twi & isync to make sure the read
54  * is actually performed (i.e. the data has come back) before we start
55  * executing any following instructions.
56  */
57 extern inline int in_8(volatile unsigned char __iomem *addr)
58 {
59         int ret;
60
61         __asm__ __volatile__(
62                 "lbz%U1%X1 %0,%1;\n"
63                 "twi 0,%0,0;\n"
64                 "isync" : "=r" (ret) : "m" (*addr));
65         return ret;
66 }
67
68 extern inline void out_8(volatile unsigned char __iomem *addr, int val)
69 {
70         __asm__ __volatile__("stb%U0%X0 %1,%0; eieio" : "=m" (*addr) : "r" (val));
71 }
72
73 extern inline int in_le16(volatile unsigned short __iomem *addr)
74 {
75         int ret;
76
77         __asm__ __volatile__("lhbrx %0,0,%1;\n"
78                              "twi 0,%0,0;\n"
79                              "isync" : "=r" (ret) :
80                               "r" (addr), "m" (*addr));
81         return ret;
82 }
83
84 extern inline int in_be16(volatile unsigned short __iomem *addr)
85 {
86         int ret;
87
88         __asm__ __volatile__("lhz%U1%X1 %0,%1;\n"
89                              "twi 0,%0,0;\n"
90                              "isync" : "=r" (ret) : "m" (*addr));
91         return ret;
92 }
93
94 extern inline void out_le16(volatile unsigned short __iomem *addr, int val)
95 {
96         __asm__ __volatile__("sthbrx %1,0,%2; eieio" : "=m" (*addr) :
97                               "r" (val), "r" (addr));
98 }
99
100 extern inline void out_be16(volatile unsigned short __iomem *addr, int val)
101 {
102         __asm__ __volatile__("sth%U0%X0 %1,%0; eieio" : "=m" (*addr) : "r" (val));
103 }
104
105 extern inline unsigned in_le32(volatile unsigned __iomem *addr)
106 {
107         unsigned ret;
108
109         __asm__ __volatile__("lwbrx %0,0,%1;\n"
110                              "twi 0,%0,0;\n"
111                              "isync" : "=r" (ret) :
112                              "r" (addr), "m" (*addr));
113         return ret;
114 }
115
116 extern inline unsigned in_be32(volatile unsigned __iomem *addr)
117 {
118         unsigned ret;
119
120         __asm__ __volatile__("lwz%U1%X1 %0,%1;\n"
121                              "twi 0,%0,0;\n"
122                              "isync" : "=r" (ret) : "m" (*addr));
123         return ret;
124 }
125
126 extern inline void out_le32(volatile unsigned __iomem *addr, int val)
127 {
128         __asm__ __volatile__("stwbrx %1,0,%2; eieio" : "=m" (*addr) :
129                              "r" (val), "r" (addr));
130 }
131
132 extern inline void out_be32(volatile unsigned __iomem *addr, int val)
133 {
134         __asm__ __volatile__("stw%U0%X0 %1,%0; eieio" : "=m" (*addr) : "r" (val));
135 }
136 #if defined (CONFIG_8260_PCI9)
137 #define readb(addr) in_8((volatile u8 *)(addr))
138 #define writeb(b,addr) out_8((volatile u8 *)(addr), (b))
139 #else
140 static inline __u8 readb(volatile void __iomem *addr)
141 {
142         return in_8(addr);
143 }
144 static inline void writeb(__u8 b, volatile void __iomem *addr)
145 {
146         out_8(addr, b);
147 }
148 #endif
149
150 #if defined(CONFIG_APUS)
151 static inline __u16 readw(volatile void __iomem *addr)
152 {
153         return *(__force volatile __u16 *)(addr);
154 }
155 static inline __u32 readl(volatile void __iomem *addr)
156 {
157         return *(__force volatile __u32 *)(addr);
158 }
159 static inline void writew(__u16 b, volatile void __iomem *addr)
160 {
161         *(__force volatile __u16 *)(addr) = b;
162 }
163 static inline void writel(__u32 b, volatile void __iomem *addr)
164 {
165         *(__force volatile __u32 *)(addr) = b;
166 }
167 #elif defined (CONFIG_8260_PCI9)
168 /* Use macros if PCI9 workaround enabled */
169 #define readw(addr) in_le16((volatile u16 *)(addr))
170 #define readl(addr) in_le32((volatile u32 *)(addr))
171 #define writew(b,addr) out_le16((volatile u16 *)(addr),(b))
172 #define writel(b,addr) out_le32((volatile u32 *)(addr),(b))
173 #else
174 static inline __u16 readw(volatile void __iomem *addr)
175 {
176         return in_le16(addr);
177 }
178 static inline __u32 readl(volatile void __iomem *addr)
179 {
180         return in_le32(addr);
181 }
182 static inline void writew(__u16 b, volatile void __iomem *addr)
183 {
184         out_le16(addr, b);
185 }
186 static inline void writel(__u32 b, volatile void __iomem *addr)
187 {
188         out_le32(addr, b);
189 }
190 #endif /* CONFIG_APUS */
191
192 #define readb_relaxed(addr) readb(addr)
193 #define readw_relaxed(addr) readw(addr)
194 #define readl_relaxed(addr) readl(addr)
195
196 static inline __u8 __raw_readb(const volatile void __iomem *addr)
197 {
198         return *(__force volatile __u8 *)(addr);
199 }
200 static inline __u16 __raw_readw(const volatile void __iomem *addr)
201 {
202         return *(__force volatile __u16 *)(addr);
203 }
204 static inline __u32 __raw_readl(const volatile void __iomem *addr)
205 {
206         return *(__force volatile __u32 *)(addr);
207 }
208 static inline void __raw_writeb(__u8 b, volatile void __iomem *addr)
209 {
210         *(__force volatile __u8 *)(addr) = b;
211 }
212 static inline void __raw_writew(__u16 b, volatile void __iomem *addr)
213 {
214         *(__force volatile __u16 *)(addr) = b;
215 }
216 static inline void __raw_writel(__u32 b, volatile void __iomem *addr)
217 {
218         *(__force volatile __u32 *)(addr) = b;
219 }
220
221 #define mmiowb()
222
223 /*
224  * The insw/outsw/insl/outsl macros don't do byte-swapping.
225  * They are only used in practice for transferring buffers which
226  * are arrays of bytes, and byte-swapping is not appropriate in
227  * that case.  - paulus
228  */
229 #define insb(port, buf, ns)     _insb((port)+___IO_BASE, (buf), (ns))
230 #define outsb(port, buf, ns)    _outsb((port)+___IO_BASE, (buf), (ns))
231 #define insw(port, buf, ns)     _insw_ns((port)+___IO_BASE, (buf), (ns))
232 #define outsw(port, buf, ns)    _outsw_ns((port)+___IO_BASE, (buf), (ns))
233 #define insl(port, buf, nl)     _insl_ns((port)+___IO_BASE, (buf), (nl))
234 #define outsl(port, buf, nl)    _outsl_ns((port)+___IO_BASE, (buf), (nl))
235
236 /*
237  * On powermacs, we will get a machine check exception if we
238  * try to read data from a non-existent I/O port.  Because the
239  * machine check is an asynchronous exception, it isn't
240  * well-defined which instruction SRR0 will point to when the
241  * exception occurs.
242  * With the sequence below (twi; isync; nop), we have found that
243  * the machine check occurs on one of the three instructions on
244  * all PPC implementations tested so far.  The twi and isync are
245  * needed on the 601 (in fact twi; sync works too), the isync and
246  * nop are needed on 604[e|r], and any of twi, sync or isync will
247  * work on 603[e], 750, 74xx.
248  * The twi creates an explicit data dependency on the returned
249  * value which seems to be needed to make the 601 wait for the
250  * load to finish.
251  */
252
253 #define __do_in_asm(name, op)                           \
254 extern __inline__ unsigned int name(unsigned int port)  \
255 {                                                       \
256         unsigned int x;                                 \
257         __asm__ __volatile__(                           \
258                         op "    %0,0,%1\n"              \
259                 "1:     twi     0,%0,0\n"               \
260                 "2:     isync\n"                        \
261                 "3:     nop\n"                          \
262                 "4:\n"                                  \
263                 ".section .fixup,\"ax\"\n"              \
264                 "5:     li      %0,-1\n"                \
265                 "       b       4b\n"                   \
266                 ".previous\n"                           \
267                 ".section __ex_table,\"a\"\n"           \
268                 "       .align  2\n"                    \
269                 "       .long   1b,5b\n"                \
270                 "       .long   2b,5b\n"                \
271                 "       .long   3b,5b\n"                \
272                 ".previous"                             \
273                 : "=&r" (x)                             \
274                 : "r" (port + ___IO_BASE));             \
275         return x;                                       \
276 }
277
278 #define __do_out_asm(name, op)                          \
279 extern __inline__ void name(unsigned int val, unsigned int port) \
280 {                                                       \
281         __asm__ __volatile__(                           \
282                 op " %0,0,%1\n"                         \
283                 "1:     sync\n"                         \
284                 "2:\n"                                  \
285                 ".section __ex_table,\"a\"\n"           \
286                 "       .align  2\n"                    \
287                 "       .long   1b,2b\n"                \
288                 ".previous"                             \
289                 : : "r" (val), "r" (port + ___IO_BASE));        \
290 }
291
292 __do_out_asm(outb, "stbx")
293 #ifdef CONFIG_APUS
294 __do_in_asm(inb, "lbzx")
295 __do_in_asm(inw, "lhz%U1%X1")
296 __do_in_asm(inl, "lwz%U1%X1")
297 __do_out_asm(outl,"stw%U0%X0")
298 __do_out_asm(outw, "sth%U0%X0")
299 #elif defined (CONFIG_8260_PCI9)
300 /* in asm cannot be defined if PCI9 workaround is used */
301 #define inb(port)               in_8((port)+___IO_BASE)
302 #define inw(port)               in_le16((port)+___IO_BASE)
303 #define inl(port)               in_le32((port)+___IO_BASE)
304 __do_out_asm(outw, "sthbrx")
305 __do_out_asm(outl, "stwbrx")
306 #else
307 __do_in_asm(inb, "lbzx")
308 __do_in_asm(inw, "lhbrx")
309 __do_in_asm(inl, "lwbrx")
310 __do_out_asm(outw, "sthbrx")
311 __do_out_asm(outl, "stwbrx")
312
313 #endif
314
315 #define inb_p(port)             inb((port))
316 #define outb_p(val, port)       outb((val), (port))
317 #define inw_p(port)             inw((port))
318 #define outw_p(val, port)       outw((val), (port))
319 #define inl_p(port)             inl((port))
320 #define outl_p(val, port)       outl((val), (port))
321
322 extern void _insb(volatile u8 __iomem *port, void *buf, int ns);
323 extern void _outsb(volatile u8 __iomem *port, const void *buf, int ns);
324 extern void _insw(volatile u16 __iomem *port, void *buf, int ns);
325 extern void _outsw(volatile u16 __iomem *port, const void *buf, int ns);
326 extern void _insl(volatile u32 __iomem *port, void *buf, int nl);
327 extern void _outsl(volatile u32 __iomem *port, const void *buf, int nl);
328 extern void _insw_ns(volatile u16 __iomem *port, void *buf, int ns);
329 extern void _outsw_ns(volatile u16 __iomem *port, const void *buf, int ns);
330 extern void _insl_ns(volatile u32 __iomem *port, void *buf, int nl);
331 extern void _outsl_ns(volatile u32 __iomem *port, const void *buf, int nl);
332
333 /*
334  * The *_ns versions below don't do byte-swapping.
335  * Neither do the standard versions now, these are just here
336  * for older code.
337  */
338 #define insw_ns(port, buf, ns)  _insw_ns((port)+___IO_BASE, (buf), (ns))
339 #define outsw_ns(port, buf, ns) _outsw_ns((port)+___IO_BASE, (buf), (ns))
340 #define insl_ns(port, buf, nl)  _insl_ns((port)+___IO_BASE, (buf), (nl))
341 #define outsl_ns(port, buf, nl) _outsl_ns((port)+___IO_BASE, (buf), (nl))
342
343
344 #define IO_SPACE_LIMIT ~0
345
346 #if defined (CONFIG_8260_PCI9)
347 #define memset_io(a,b,c)       memset((void *)(a),(b),(c))
348 #define memcpy_fromio(a,b,c)   memcpy((a),(void *)(b),(c))
349 #define memcpy_toio(a,b,c)     memcpy((void *)(a),(b),(c))
350 #else
351 static inline void memset_io(volatile void __iomem *addr, unsigned char val, int count)
352 {
353         memset((void __force *)addr, val, count);
354 }
355 static inline void memcpy_fromio(void *dst,const volatile void __iomem *src, int count)
356 {
357         memcpy(dst, (void __force *) src, count);
358 }
359 static inline void memcpy_toio(volatile void __iomem *dst, const void *src, int count)
360 {
361         memcpy((void __force *) dst, src, count);
362 }
363 #endif
364
365 #define eth_io_copy_and_sum(a,b,c,d)            eth_copy_and_sum((a),(void __force *)(void __iomem *)(b),(c),(d))
366
367 /*
368  * Map in an area of physical address space, for accessing
369  * I/O devices etc.
370  */
371 extern void __iomem *__ioremap(phys_addr_t address, unsigned long size,
372                        unsigned long flags);
373 extern void __iomem *ioremap(phys_addr_t address, unsigned long size);
374 #ifdef CONFIG_44x
375 extern void __iomem *ioremap64(unsigned long long address, unsigned long size);
376 #endif
377 #define ioremap_nocache(addr, size)     ioremap((addr), (size))
378 extern void iounmap(volatile void __iomem *addr);
379 extern unsigned long iopa(unsigned long addr);
380 extern unsigned long mm_ptov(unsigned long addr) __attribute_const__;
381 extern void io_block_mapping(unsigned long virt, phys_addr_t phys,
382                              unsigned int size, int flags);
383
384 /*
385  * The PCI bus is inherently Little-Endian.  The PowerPC is being
386  * run Big-Endian.  Thus all values which cross the [PCI] barrier
387  * must be endian-adjusted.  Also, the local DRAM has a different
388  * address from the PCI point of view, thus buffer addresses also
389  * have to be modified [mapped] appropriately.
390  */
391 extern inline unsigned long virt_to_bus(volatile void * address)
392 {
393 #ifndef CONFIG_APUS
394         if (address == (void *)0)
395                 return 0;
396         return (unsigned long)address - KERNELBASE + PCI_DRAM_OFFSET;
397 #else
398         return iopa ((unsigned long) address);
399 #endif
400 }
401
402 extern inline void * bus_to_virt(unsigned long address)
403 {
404 #ifndef CONFIG_APUS
405         if (address == 0)
406                 return NULL;
407         return (void *)(address - PCI_DRAM_OFFSET + KERNELBASE);
408 #else
409         return (void*) mm_ptov (address);
410 #endif
411 }
412
413 /*
414  * Change virtual addresses to physical addresses and vv, for
415  * addresses in the area where the kernel has the RAM mapped.
416  */
417 extern inline unsigned long virt_to_phys(volatile void * address)
418 {
419 #ifndef CONFIG_APUS
420         return (unsigned long) address - KERNELBASE;
421 #else
422         return iopa ((unsigned long) address);
423 #endif
424 }
425
426 extern inline void * phys_to_virt(unsigned long address)
427 {
428 #ifndef CONFIG_APUS
429         return (void *) (address + KERNELBASE);
430 #else
431         return (void*) mm_ptov (address);
432 #endif
433 }
434
435 /*
436  * Change "struct page" to physical address.
437  */
438 #define page_to_phys(page)      (page_to_pfn(page) << PAGE_SHIFT)
439 #define page_to_bus(page)       (page_to_phys(page) + PCI_DRAM_OFFSET)
440
441 /*
442  * Enforce In-order Execution of I/O:
443  * Acts as a barrier to ensure all previous I/O accesses have
444  * completed before any further ones are issued.
445  */
446 extern inline void eieio(void)
447 {
448         __asm__ __volatile__ ("eieio" : : : "memory");
449 }
450
451 /* Enforce in-order execution of data I/O.
452  * No distinction between read/write on PPC; use eieio for all three.
453  */
454 #define iobarrier_rw() eieio()
455 #define iobarrier_r()  eieio()
456 #define iobarrier_w()  eieio()
457
458 static inline int check_signature(volatile void __iomem * io_addr,
459         const unsigned char *signature, int length)
460 {
461         int retval = 0;
462         do {
463                 if (readb(io_addr) != *signature)
464                         goto out;
465                 io_addr++;
466                 signature++;
467                 length--;
468         } while (length);
469         retval = 1;
470 out:
471         return retval;
472 }
473
474 /*
475  * Here comes the ppc implementation of the IOMAP 
476  * interfaces.
477  */
478 static inline unsigned int ioread8(void __iomem *addr)
479 {
480         return readb(addr);
481 }
482
483 static inline unsigned int ioread16(void __iomem *addr)
484 {
485         return readw(addr);
486 }
487
488 static inline unsigned int ioread32(void __iomem *addr)
489 {
490         return readl(addr);
491 }
492
493 static inline void iowrite8(u8 val, void __iomem *addr)
494 {
495         writeb(val, addr);
496 }
497
498 static inline void iowrite16(u16 val, void __iomem *addr)
499 {
500         writew(val, addr);
501 }
502
503 static inline void iowrite32(u32 val, void __iomem *addr)
504 {
505         writel(val, addr);
506 }
507
508 static inline void ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
509 {
510         _insb(addr, dst, count);
511 }
512
513 static inline void ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
514 {
515         _insw_ns(addr, dst, count);
516 }
517
518 static inline void ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
519 {
520         _insl_ns(addr, dst, count);
521 }
522
523 static inline void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
524 {
525         _outsb(addr, src, count);
526 }
527
528 static inline void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
529 {
530         _outsw_ns(addr, src, count);
531 }
532
533 static inline void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
534 {
535         _outsl_ns(addr, src, count);
536 }
537
538 /* Create a virtual mapping cookie for an IO port range */
539 extern void __iomem *ioport_map(unsigned long port, unsigned int nr);
540 extern void ioport_unmap(void __iomem *);
541
542 /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */
543 struct pci_dev;
544 extern void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max);
545 extern void pci_iounmap(struct pci_dev *dev, void __iomem *);
546
547 #endif /* _PPC_IO_H */
548
549 #ifdef CONFIG_8260_PCI9
550 #include <asm/mpc8260_pci9.h>
551 #endif
552
553 #endif /* __KERNEL__ */