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