xref: /rk3399_ARM-atf/include/lib/mmio.h (revision fd904df14b1ab4304bd02c00f82f0c6888f8e7a8)
14ecca339SDan Handley /*
24ecca339SDan Handley  * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
34ecca339SDan Handley  *
44ecca339SDan Handley  * Redistribution and use in source and binary forms, with or without
54ecca339SDan Handley  * modification, are permitted provided that the following conditions are met:
64ecca339SDan Handley  *
74ecca339SDan Handley  * Redistributions of source code must retain the above copyright notice, this
84ecca339SDan Handley  * list of conditions and the following disclaimer.
94ecca339SDan Handley  *
104ecca339SDan Handley  * Redistributions in binary form must reproduce the above copyright notice,
114ecca339SDan Handley  * this list of conditions and the following disclaimer in the documentation
124ecca339SDan Handley  * and/or other materials provided with the distribution.
134ecca339SDan Handley  *
144ecca339SDan Handley  * Neither the name of ARM nor the names of its contributors may be used
154ecca339SDan Handley  * to endorse or promote products derived from this software without specific
164ecca339SDan Handley  * prior written permission.
174ecca339SDan Handley  *
184ecca339SDan Handley  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
194ecca339SDan Handley  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
204ecca339SDan Handley  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
214ecca339SDan Handley  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
224ecca339SDan Handley  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
234ecca339SDan Handley  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
244ecca339SDan Handley  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
254ecca339SDan Handley  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
264ecca339SDan Handley  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
274ecca339SDan Handley  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
284ecca339SDan Handley  * POSSIBILITY OF SUCH DAMAGE.
294ecca339SDan Handley  */
304ecca339SDan Handley 
314ecca339SDan Handley #ifndef __MMIO_H__
324ecca339SDan Handley #define __MMIO_H__
334ecca339SDan Handley 
344ecca339SDan Handley #include <stdint.h>
354ecca339SDan Handley 
365e113753SAndrew Thoelke static inline void mmio_write_8(uintptr_t addr, uint8_t value)
375e113753SAndrew Thoelke {
385e113753SAndrew Thoelke 	*(volatile uint8_t*)addr = value;
395e113753SAndrew Thoelke }
404ecca339SDan Handley 
415e113753SAndrew Thoelke static inline uint8_t mmio_read_8(uintptr_t addr)
425e113753SAndrew Thoelke {
435e113753SAndrew Thoelke 	return *(volatile uint8_t*)addr;
445e113753SAndrew Thoelke }
454ecca339SDan Handley 
46*fd904df1SJimmy Huang static inline void mmio_write_16(uintptr_t addr, uint16_t value)
47*fd904df1SJimmy Huang {
48*fd904df1SJimmy Huang 	*(volatile uint16_t*)addr = value;
49*fd904df1SJimmy Huang }
50*fd904df1SJimmy Huang 
51*fd904df1SJimmy Huang static inline uint16_t mmio_read_16(uintptr_t addr)
52*fd904df1SJimmy Huang {
53*fd904df1SJimmy Huang 	return *(volatile uint16_t*)addr;
54*fd904df1SJimmy Huang }
55*fd904df1SJimmy Huang 
565e113753SAndrew Thoelke static inline void mmio_write_32(uintptr_t addr, uint32_t value)
575e113753SAndrew Thoelke {
585e113753SAndrew Thoelke 	*(volatile uint32_t*)addr = value;
595e113753SAndrew Thoelke }
605e113753SAndrew Thoelke 
615e113753SAndrew Thoelke static inline uint32_t mmio_read_32(uintptr_t addr)
625e113753SAndrew Thoelke {
635e113753SAndrew Thoelke 	return *(volatile uint32_t*)addr;
645e113753SAndrew Thoelke }
655e113753SAndrew Thoelke 
665e113753SAndrew Thoelke static inline void mmio_write_64(uintptr_t addr, uint64_t value)
675e113753SAndrew Thoelke {
685e113753SAndrew Thoelke 	*(volatile uint64_t*)addr = value;
695e113753SAndrew Thoelke }
705e113753SAndrew Thoelke 
715e113753SAndrew Thoelke static inline uint64_t mmio_read_64(uintptr_t addr)
725e113753SAndrew Thoelke {
735e113753SAndrew Thoelke 	return *(volatile uint64_t*)addr;
745e113753SAndrew Thoelke }
754ecca339SDan Handley 
76*fd904df1SJimmy Huang static inline void mmio_clrbits_32(uintptr_t addr, uint32_t clear)
77*fd904df1SJimmy Huang {
78*fd904df1SJimmy Huang 	mmio_write_32(addr, mmio_read_32(addr) & ~clear);
79*fd904df1SJimmy Huang }
80*fd904df1SJimmy Huang 
81*fd904df1SJimmy Huang static inline void mmio_setbits_32(uintptr_t addr, uint32_t set)
82*fd904df1SJimmy Huang {
83*fd904df1SJimmy Huang 	mmio_write_32(addr, mmio_read_32(addr) | set);
84*fd904df1SJimmy Huang }
85*fd904df1SJimmy Huang 
86*fd904df1SJimmy Huang static inline void mmio_clrsetbits_32(uintptr_t addr,
87*fd904df1SJimmy Huang 				uint32_t clear,
88*fd904df1SJimmy Huang 				uint32_t set)
89*fd904df1SJimmy Huang {
90*fd904df1SJimmy Huang 	mmio_write_32(addr, (mmio_read_32(addr) & ~clear) | set);
91*fd904df1SJimmy Huang }
92*fd904df1SJimmy Huang 
934ecca339SDan Handley #endif /* __MMIO_H__ */
94