xref: /openbmc/u-boot/arch/sh/include/asm/io.h (revision 505fe9cdd0e9613ee1f081b58d2fb69bdf1c1eae)
1819833afSPeter Tyser /*
2819833afSPeter Tyser  *  linux/include/asm-sh/io.h
3819833afSPeter Tyser  *
4819833afSPeter Tyser  *  Copyright (C) 1996-2000 Russell King
5819833afSPeter Tyser  *
6819833afSPeter Tyser  * This program is free software; you can redistribute it and/or modify
7819833afSPeter Tyser  * it under the terms of the GNU General Public License version 2 as
8819833afSPeter Tyser  * published by the Free Software Foundation.
9819833afSPeter Tyser  *
10819833afSPeter Tyser  * Modifications:
11819833afSPeter Tyser  *  16-Sep-1996	RMK	Inlined the inx/outx functions & optimised for both
12819833afSPeter Tyser  *			constant addresses and variable addresses.
13819833afSPeter Tyser  *  04-Dec-1997	RMK	Moved a lot of this stuff to the new architecture
14819833afSPeter Tyser  *			specific IO header files.
15819833afSPeter Tyser  *  27-Mar-1999	PJB	Second parameter of memcpy_toio is const..
16819833afSPeter Tyser  *  04-Apr-1999	PJB	Added check_signature.
17819833afSPeter Tyser  *  12-Dec-1999	RMK	More cleanups
18819833afSPeter Tyser  *  18-Jun-2000 RMK	Removed virt_to_* and friends definitions
19819833afSPeter Tyser  */
20819833afSPeter Tyser #ifndef __ASM_SH_IO_H
21819833afSPeter Tyser #define __ASM_SH_IO_H
22819833afSPeter Tyser 
23819833afSPeter Tyser #ifdef __KERNEL__
24819833afSPeter Tyser 
25819833afSPeter Tyser #include <linux/types.h>
26819833afSPeter Tyser #include <asm/byteorder.h>
27819833afSPeter Tyser 
28819833afSPeter Tyser /*
29819833afSPeter Tyser  * Generic virtual read/write.  Note that we don't support half-word
30819833afSPeter Tyser  * read/writes.  We define __arch_*[bl] here, and leave __arch_*w
31819833afSPeter Tyser  * to the architecture specific code.
32819833afSPeter Tyser  */
33819833afSPeter Tyser #define __arch_getb(a)			(*(volatile unsigned char *)(a))
34819833afSPeter Tyser #define __arch_getw(a)			(*(volatile unsigned short *)(a))
35819833afSPeter Tyser #define __arch_getl(a)			(*(volatile unsigned int *)(a))
36819833afSPeter Tyser 
37819833afSPeter Tyser #define __arch_putb(v, a)		(*(volatile unsigned char *)(a) = (v))
38819833afSPeter Tyser #define __arch_putw(v, a)		(*(volatile unsigned short *)(a) = (v))
39819833afSPeter Tyser #define __arch_putl(v, a)		(*(volatile unsigned int *)(a) = (v))
40819833afSPeter Tyser 
41819833afSPeter Tyser extern void __raw_writesb(unsigned int addr, const void *data, int bytelen);
42819833afSPeter Tyser extern void __raw_writesw(unsigned int addr, const void *data, int wordlen);
43819833afSPeter Tyser extern void __raw_writesl(unsigned int addr, const void *data, int longlen);
44819833afSPeter Tyser 
45819833afSPeter Tyser extern void __raw_readsb(unsigned int addr, void *data, int bytelen);
46819833afSPeter Tyser extern void __raw_readsw(unsigned int addr, void *data, int wordlen);
47819833afSPeter Tyser extern void __raw_readsl(unsigned int addr, void *data, int longlen);
48819833afSPeter Tyser 
49819833afSPeter Tyser #define __raw_writeb(v, a)		__arch_putb(v, a)
50819833afSPeter Tyser #define __raw_writew(v, a)		__arch_putw(v, a)
51819833afSPeter Tyser #define __raw_writel(v, a)		__arch_putl(v, a)
52819833afSPeter Tyser 
53819833afSPeter Tyser #define __raw_readb(a)			__arch_getb(a)
54819833afSPeter Tyser #define __raw_readw(a)			__arch_getw(a)
55819833afSPeter Tyser #define __raw_readl(a)			__arch_getl(a)
56819833afSPeter Tyser 
57819833afSPeter Tyser /*
58819833afSPeter Tyser  * The compiler seems to be incapable of optimising constants
59819833afSPeter Tyser  * properly.  Spell it out to the compiler in some cases.
60819833afSPeter Tyser  * These are only valid for small values of "off" (< 1<<12)
61819833afSPeter Tyser  */
62819833afSPeter Tyser #define __raw_base_writeb(val, base, off)	__arch_base_putb(val, base, off)
63819833afSPeter Tyser #define __raw_base_writew(val, base, off)	__arch_base_putw(val, base, off)
64819833afSPeter Tyser #define __raw_base_writel(val, base, off)	__arch_base_putl(val, base, off)
65819833afSPeter Tyser 
66819833afSPeter Tyser #define __raw_base_readb(base, off)	__arch_base_getb(base, off)
67819833afSPeter Tyser #define __raw_base_readw(base, off)	__arch_base_getw(base, off)
68819833afSPeter Tyser #define __raw_base_readl(base, off)	__arch_base_getl(base, off)
69819833afSPeter Tyser 
70819833afSPeter Tyser /*
71819833afSPeter Tyser  *  IO port access primitives
72819833afSPeter Tyser  *  -------------------------
73819833afSPeter Tyser  *
74819833afSPeter Tyser  * The SH doesn't have special IO access instructions; all IO is memory
75819833afSPeter Tyser  * mapped.  Note that these are defined to perform little endian accesses
76819833afSPeter Tyser  * only.  Their primary purpose is to access PCI and ISA peripherals.
77819833afSPeter Tyser  *
78819833afSPeter Tyser  * Note that we prevent GCC re-ordering or caching values in expressions
79819833afSPeter Tyser  * by introducing sequence points into the in*() definitions.  Note that
80819833afSPeter Tyser  * __raw_* do not guarantee this behaviour.
81819833afSPeter Tyser  *
82819833afSPeter Tyser  * The {in,out}[bwl] macros are for emulating x86-style PCI/ISA IO space.
83819833afSPeter Tyser  */
84819833afSPeter Tyser #define outb(v, p)               __raw_writeb(v, p)
85819833afSPeter Tyser #define outw(v, p)               __raw_writew(cpu_to_le16(v), p)
86819833afSPeter Tyser #define outl(v, p)               __raw_writel(cpu_to_le32(v), p)
87819833afSPeter Tyser 
88819833afSPeter Tyser #define inb(p)  ({ unsigned int __v = __raw_readb(p); __v; })
89819833afSPeter Tyser #define inw(p)  ({ unsigned int __v = __le16_to_cpu(__raw_readw(p)); __v; })
90819833afSPeter Tyser #define inl(p)  ({ unsigned int __v = __le32_to_cpu(__raw_readl(p)); __v; })
91819833afSPeter Tyser 
92819833afSPeter Tyser #define outsb(p, d, l)			__raw_writesb(p, d, l)
93819833afSPeter Tyser #define outsw(p, d, l)			__raw_writesw(p, d, l)
94819833afSPeter Tyser #define outsl(p, d, l)			__raw_writesl(p, d, l)
95819833afSPeter Tyser 
96819833afSPeter Tyser #define insb(p, d, l)			__raw_readsb(p, d, l)
97819833afSPeter Tyser #define insw(p, d, l)			__raw_readsw(p, d, l)
98819833afSPeter Tyser #define insl(p, d, l)			__raw_readsl(p, d, l)
99819833afSPeter Tyser 
100819833afSPeter Tyser #define outb_p(val, port)		outb((val), (port))
101819833afSPeter Tyser #define outw_p(val, port)		outw((val), (port))
102819833afSPeter Tyser #define outl_p(val, port)		outl((val), (port))
103819833afSPeter Tyser #define inb_p(port)			inb((port))
104819833afSPeter Tyser #define inw_p(port)			inw((port))
105819833afSPeter Tyser #define inl_p(port)			inl((port))
106819833afSPeter Tyser 
107819833afSPeter Tyser #define outsb_p(port, from, len)		outsb(port, from, len)
108819833afSPeter Tyser #define outsw_p(port, from, len)		outsw(port, from, len)
109819833afSPeter Tyser #define outsl_p(port, from, len)		outsl(port, from, len)
110819833afSPeter Tyser #define insb_p(port, to, len)		insb(port, to, len)
111819833afSPeter Tyser #define insw_p(port, to, len)		insw(port, to, len)
112819833afSPeter Tyser #define insl_p(port, to, len)		insl(port, to, len)
113819833afSPeter Tyser 
114819833afSPeter Tyser /* for U-Boot PCI */
115819833afSPeter Tyser #define out_8(port, val)	outb(val, port)
116819833afSPeter Tyser #define out_le16(port, val)	outw(val, port)
117819833afSPeter Tyser #define out_le32(port, val)	outl(val, port)
118819833afSPeter Tyser #define in_8(port)			inb(port)
119819833afSPeter Tyser #define in_le16(port)		inw(port)
120819833afSPeter Tyser #define in_le32(port)		inl(port)
121819833afSPeter Tyser 
122819833afSPeter Tyser /*
123819833afSPeter Tyser  * DMA-consistent mapping functions.  These allocate/free a region of
124819833afSPeter Tyser  * uncached, unwrite-buffered mapped memory space for use with DMA
125819833afSPeter Tyser  * devices.  This is the "generic" version.  The PCI specific version
126819833afSPeter Tyser  * is in pci.h
127819833afSPeter Tyser  */
128819833afSPeter Tyser extern void *consistent_alloc(int gfp, size_t size, dma_addr_t *handle);
129819833afSPeter Tyser extern void consistent_free(void *vaddr, size_t size, dma_addr_t handle);
130819833afSPeter Tyser extern void consistent_sync(void *vaddr, size_t size, int rw);
131819833afSPeter Tyser 
132819833afSPeter Tyser /*
133819833afSPeter Tyser  * String version of IO memory access ops:
134819833afSPeter Tyser  */
135819833afSPeter Tyser extern void _memcpy_fromio(void *, unsigned long, size_t);
136819833afSPeter Tyser extern void _memcpy_toio(unsigned long, const void *, size_t);
137819833afSPeter Tyser extern void _memset_io(unsigned long, int, size_t);
138819833afSPeter Tyser 
139819833afSPeter Tyser /*
140819833afSPeter Tyser  * If this architecture has PCI memory IO, then define the read/write
141819833afSPeter Tyser  * macros.  These should only be used with the cookie passed from
142819833afSPeter Tyser  * ioremap.
143819833afSPeter Tyser  */
144819833afSPeter Tyser #ifdef __mem_pci
145819833afSPeter Tyser 
146819833afSPeter Tyser #define readb(c) ({ unsigned int __v = __raw_readb(__mem_pci(c)); __v; })
147819833afSPeter Tyser #define readw(c)\
148819833afSPeter Tyser 	({ unsigned int __v = le16_to_cpu(__raw_readw(__mem_pci(c))); __v; })
149819833afSPeter Tyser #define readl(c)\
150819833afSPeter Tyser 	({ unsigned int __v = le32_to_cpu(__raw_readl(__mem_pci(c))); __v; })
151819833afSPeter Tyser 
152819833afSPeter Tyser #define writeb(v, c)		__raw_writeb(v, __mem_pci(c))
153819833afSPeter Tyser #define writew(v, c)		__raw_writew(cpu_to_le16(v), __mem_pci(c))
154819833afSPeter Tyser #define writel(v, c)		__raw_writel(cpu_to_le32(v), __mem_pci(c))
155819833afSPeter Tyser 
156819833afSPeter Tyser #define memset_io(c, v, l)		_memset_io(__mem_pci(c), (v), (l))
157819833afSPeter Tyser #define memcpy_fromio(a, c, l)	_memcpy_fromio((a), __mem_pci(c), (l))
158819833afSPeter Tyser #define memcpy_toio(c, a, l)	_memcpy_toio(__mem_pci(c), (a), (l))
159819833afSPeter Tyser 
160819833afSPeter Tyser #define eth_io_copy_and_sum(s, c, l, b) \
161819833afSPeter Tyser 				eth_copy_and_sum((s), __mem_pci(c), (l), (b))
162819833afSPeter Tyser 
163819833afSPeter Tyser static inline int
check_signature(unsigned long io_addr,const unsigned char * signature,int length)164819833afSPeter Tyser check_signature(unsigned long io_addr, const unsigned char *signature,
165819833afSPeter Tyser 		int length)
166819833afSPeter Tyser {
167819833afSPeter Tyser 	int retval = 0;
168819833afSPeter Tyser 	do {
169819833afSPeter Tyser 		if (readb(io_addr) != *signature)
170819833afSPeter Tyser 			goto out;
171819833afSPeter Tyser 		io_addr++;
172819833afSPeter Tyser 		signature++;
173819833afSPeter Tyser 		length--;
174819833afSPeter Tyser 	} while (length);
175819833afSPeter Tyser 	retval = 1;
176819833afSPeter Tyser out:
177819833afSPeter Tyser 	return retval;
178819833afSPeter Tyser }
179819833afSPeter Tyser 
180819833afSPeter Tyser #elif !defined(readb)
181819833afSPeter Tyser 
182819833afSPeter Tyser #define readb(addr)	__raw_readb(addr)
183819833afSPeter Tyser #define readw(addr)	__raw_readw(addr)
184819833afSPeter Tyser #define readl(addr)	__raw_readl(addr)
185819833afSPeter Tyser #define writeb(v, addr)	__raw_writeb(v, addr)
186819833afSPeter Tyser #define writew(v, addr)	__raw_writew(v, addr)
187819833afSPeter Tyser #define writel(v, addr)	__raw_writel(v, addr)
188819833afSPeter Tyser 
189819833afSPeter Tyser #define check_signature(io, sig, len)	(0)
190819833afSPeter Tyser 
191819833afSPeter Tyser #endif	/* __mem_pci */
192819833afSPeter Tyser 
sync(void)193819833afSPeter Tyser static inline void sync(void)
194819833afSPeter Tyser {
195819833afSPeter Tyser }
196819833afSPeter Tyser 
197819833afSPeter Tyser /*
1981379c51eSNobuhiro Iwamatsu  * Clear and set bits in one shot. These macros can be used to clear and
1991379c51eSNobuhiro Iwamatsu  * set multiple bits in a register using a single call. These macros can
2001379c51eSNobuhiro Iwamatsu  * also be used to set a multiple-bit bit pattern using a mask, by
2011379c51eSNobuhiro Iwamatsu  * specifying the mask in the 'clear' parameter and the new bit pattern
2021379c51eSNobuhiro Iwamatsu  * in the 'set' parameter.
2031379c51eSNobuhiro Iwamatsu  */
2041379c51eSNobuhiro Iwamatsu 
2051379c51eSNobuhiro Iwamatsu #define clrbits(type, addr, clear) \
2061379c51eSNobuhiro Iwamatsu 		out_##type((addr), in_##type(addr) & ~(clear))
2071379c51eSNobuhiro Iwamatsu 
2081379c51eSNobuhiro Iwamatsu #define setbits(type, addr, set) \
2091379c51eSNobuhiro Iwamatsu 		out_##type((addr), in_##type(addr) | (set))
2101379c51eSNobuhiro Iwamatsu 
2111379c51eSNobuhiro Iwamatsu #define clrsetbits(type, addr, clear, set) \
2121379c51eSNobuhiro Iwamatsu 		out_##type((addr), (in_##type(addr) & ~(clear)) | (set))
2131379c51eSNobuhiro Iwamatsu 
2141379c51eSNobuhiro Iwamatsu #define clrbits_be32(addr, clear) clrbits(be32, addr, clear)
2151379c51eSNobuhiro Iwamatsu #define setbits_be32(addr, set) setbits(be32, addr, set)
2161379c51eSNobuhiro Iwamatsu #define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set)
2171379c51eSNobuhiro Iwamatsu 
2181379c51eSNobuhiro Iwamatsu #define clrbits_le32(addr, clear) clrbits(le32, addr, clear)
2191379c51eSNobuhiro Iwamatsu #define setbits_le32(addr, set) setbits(le32, addr, set)
2201379c51eSNobuhiro Iwamatsu #define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set)
2211379c51eSNobuhiro Iwamatsu 
2221379c51eSNobuhiro Iwamatsu #define clrbits_be16(addr, clear) clrbits(be16, addr, clear)
2231379c51eSNobuhiro Iwamatsu #define setbits_be16(addr, set) setbits(be16, addr, set)
2241379c51eSNobuhiro Iwamatsu #define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set)
2251379c51eSNobuhiro Iwamatsu 
2261379c51eSNobuhiro Iwamatsu #define clrbits_le16(addr, clear) clrbits(le16, addr, clear)
2271379c51eSNobuhiro Iwamatsu #define setbits_le16(addr, set) setbits(le16, addr, set)
2281379c51eSNobuhiro Iwamatsu #define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set)
2291379c51eSNobuhiro Iwamatsu 
2301379c51eSNobuhiro Iwamatsu #define clrbits_8(addr, clear) clrbits(8, addr, clear)
2311379c51eSNobuhiro Iwamatsu #define setbits_8(addr, set) setbits(8, addr, set)
2321379c51eSNobuhiro Iwamatsu #define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set)
2331379c51eSNobuhiro Iwamatsu 
234*505fe9cdSPaul Burton #include <asm-generic/io.h>
235819833afSPeter Tyser 
236819833afSPeter Tyser #endif	/* __KERNEL__ */
237819833afSPeter Tyser #endif	/* __ASM_SH_IO_H */
238