xref: /rk3399_rockchip-uboot/arch/arm/cpu/armv7/cache_v7.c (revision 7e781998565925d5b20e86f38c43d2d2917cb068)
12c451f78SAneesh V /*
22c451f78SAneesh V  * (C) Copyright 2010
32c451f78SAneesh V  * Texas Instruments, <www.ti.com>
42c451f78SAneesh V  * Aneesh V <aneesh@ti.com>
52c451f78SAneesh V  *
61a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
72c451f78SAneesh V  */
82c451f78SAneesh V #include <linux/types.h>
92c451f78SAneesh V #include <common.h>
102c451f78SAneesh V #include <asm/armv7.h>
112c451f78SAneesh V #include <asm/utils.h>
122c451f78SAneesh V 
13df120142SHans de Goede #define ARMV7_DCACHE_INVAL_RANGE	1
14df120142SHans de Goede #define ARMV7_DCACHE_CLEAN_INVAL_RANGE	2
152c451f78SAneesh V 
162c451f78SAneesh V #ifndef CONFIG_SYS_DCACHE_OFF
17c09d2905SHans de Goede 
18c09d2905SHans de Goede /* Asm functions from cache_v7_asm.S */
19c09d2905SHans de Goede void v7_flush_dcache_all(void);
20df120142SHans de Goede void v7_invalidate_dcache_all(void);
21c09d2905SHans de Goede 
get_ccsidr(void)222c451f78SAneesh V static u32 get_ccsidr(void)
232c451f78SAneesh V {
242c451f78SAneesh V 	u32 ccsidr;
252c451f78SAneesh V 
262c451f78SAneesh V 	/* Read current CP15 Cache Size ID Register */
272c451f78SAneesh V 	asm volatile ("mrc p15, 1, %0, c0, c0, 0" : "=r" (ccsidr));
282c451f78SAneesh V 	return ccsidr;
292c451f78SAneesh V }
302c451f78SAneesh V 
v7_dcache_clean_inval_range(u32 start,u32 stop,u32 line_len)31b9297c22SThierry Reding static void v7_dcache_clean_inval_range(u32 start, u32 stop, u32 line_len)
322c451f78SAneesh V {
332c451f78SAneesh V 	u32 mva;
342c451f78SAneesh V 
352c451f78SAneesh V 	/* Align start to cache line boundary */
362c451f78SAneesh V 	start &= ~(line_len - 1);
372c451f78SAneesh V 	for (mva = start; mva < stop; mva = mva + line_len) {
382c451f78SAneesh V 		/* DCCIMVAC - Clean & Invalidate data cache by MVA to PoC */
392c451f78SAneesh V 		asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva));
402c451f78SAneesh V 	}
412c451f78SAneesh V }
422c451f78SAneesh V 
v7_dcache_inval_range(u32 start,u32 stop,u32 line_len)432c451f78SAneesh V static void v7_dcache_inval_range(u32 start, u32 stop, u32 line_len)
442c451f78SAneesh V {
452c451f78SAneesh V 	u32 mva;
462c451f78SAneesh V 
47*7e781998SJoseph Chen #ifdef DEBUG
48*7e781998SJoseph Chen 	check_cache_range(start, stop);
49*7e781998SJoseph Chen #endif
50*7e781998SJoseph Chen 	/* aligned ? backward and flush a line_len */
51*7e781998SJoseph Chen 	if (start & (line_len - 1)) {
52*7e781998SJoseph Chen 		mva = start & ~(line_len - 1);
53*7e781998SJoseph Chen 		asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva));
54*7e781998SJoseph Chen 		start = mva + line_len;
55*7e781998SJoseph Chen 	}
56*7e781998SJoseph Chen 
57*7e781998SJoseph Chen 	/* aligned ? forward and flush a line_len */
58*7e781998SJoseph Chen 	if (stop & (line_len - 1)) {
59*7e781998SJoseph Chen 		mva = stop & ~(line_len - 1);
60*7e781998SJoseph Chen 		asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva));
61*7e781998SJoseph Chen 		stop = mva;
62*7e781998SJoseph Chen 	}
632c451f78SAneesh V 
642c451f78SAneesh V 	for (mva = start; mva < stop; mva = mva + line_len) {
652c451f78SAneesh V 		/* DCIMVAC - Invalidate data cache by MVA to PoC */
662c451f78SAneesh V 		asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (mva));
672c451f78SAneesh V 	}
682c451f78SAneesh V }
692c451f78SAneesh V 
v7_dcache_maint_range(u32 start,u32 stop,u32 range_op)702c451f78SAneesh V static void v7_dcache_maint_range(u32 start, u32 stop, u32 range_op)
712c451f78SAneesh V {
722c451f78SAneesh V 	u32 line_len, ccsidr;
732c451f78SAneesh V 
742c451f78SAneesh V 	ccsidr = get_ccsidr();
752c451f78SAneesh V 	line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >>
762c451f78SAneesh V 			CCSIDR_LINE_SIZE_OFFSET) + 2;
772c451f78SAneesh V 	/* Converting from words to bytes */
782c451f78SAneesh V 	line_len += 2;
792c451f78SAneesh V 	/* converting from log2(linelen) to linelen */
802c451f78SAneesh V 	line_len = 1 << line_len;
812c451f78SAneesh V 
822c451f78SAneesh V 	switch (range_op) {
832c451f78SAneesh V 	case ARMV7_DCACHE_CLEAN_INVAL_RANGE:
842c451f78SAneesh V 		v7_dcache_clean_inval_range(start, stop, line_len);
852c451f78SAneesh V 		break;
862c451f78SAneesh V 	case ARMV7_DCACHE_INVAL_RANGE:
872c451f78SAneesh V 		v7_dcache_inval_range(start, stop, line_len);
882c451f78SAneesh V 		break;
892c451f78SAneesh V 	}
902c451f78SAneesh V 
91882f80b9SAneesh V 	/* DSB to make sure the operation is complete */
92a78cd861STom Rini 	dsb();
932c451f78SAneesh V }
942c451f78SAneesh V 
952c451f78SAneesh V /* Invalidate TLB */
v7_inval_tlb(void)962c451f78SAneesh V static void v7_inval_tlb(void)
972c451f78SAneesh V {
982c451f78SAneesh V 	/* Invalidate entire unified TLB */
992c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0));
1002c451f78SAneesh V 	/* Invalidate entire data TLB */
1012c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c8, c6, 0" : : "r" (0));
1022c451f78SAneesh V 	/* Invalidate entire instruction TLB */
1032c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c8, c5, 0" : : "r" (0));
1042c451f78SAneesh V 	/* Full system DSB - make sure that the invalidation is complete */
105a78cd861STom Rini 	dsb();
1062c451f78SAneesh V 	/* Full system ISB - make sure the instruction stream sees it */
107a78cd861STom Rini 	isb();
1082c451f78SAneesh V }
1092c451f78SAneesh V 
invalidate_dcache_all(void)1102c451f78SAneesh V void invalidate_dcache_all(void)
1112c451f78SAneesh V {
112df120142SHans de Goede 	v7_invalidate_dcache_all();
1132c451f78SAneesh V 
1142c451f78SAneesh V 	v7_outer_cache_inval_all();
1152c451f78SAneesh V }
1162c451f78SAneesh V 
1172c451f78SAneesh V /*
1182c451f78SAneesh V  * Performs a clean & invalidation of the entire data cache
1192c451f78SAneesh V  * at all levels
1202c451f78SAneesh V  */
flush_dcache_all(void)1212c451f78SAneesh V void flush_dcache_all(void)
1222c451f78SAneesh V {
123c09d2905SHans de Goede 	v7_flush_dcache_all();
1242c451f78SAneesh V 
1252c451f78SAneesh V 	v7_outer_cache_flush_all();
1262c451f78SAneesh V }
1272c451f78SAneesh V 
1282c451f78SAneesh V /*
1292c451f78SAneesh V  * Invalidates range in all levels of D-cache/unified cache used:
1302c451f78SAneesh V  * Affects the range [start, stop - 1]
1312c451f78SAneesh V  */
invalidate_dcache_range(unsigned long start,unsigned long stop)1322c451f78SAneesh V void invalidate_dcache_range(unsigned long start, unsigned long stop)
1332c451f78SAneesh V {
134*7e781998SJoseph Chen #ifdef DEBUG
13511aa6a32SMarek Vasut 	check_cache_range(start, stop);
136*7e781998SJoseph Chen #endif
1372c451f78SAneesh V 	v7_dcache_maint_range(start, stop, ARMV7_DCACHE_INVAL_RANGE);
1382c451f78SAneesh V 
1392c451f78SAneesh V 	v7_outer_cache_inval_range(start, stop);
1402c451f78SAneesh V }
1412c451f78SAneesh V 
1422c451f78SAneesh V /*
1432c451f78SAneesh V  * Flush range(clean & invalidate) from all levels of D-cache/unified
1442c451f78SAneesh V  * cache used:
1452c451f78SAneesh V  * Affects the range [start, stop - 1]
1462c451f78SAneesh V  */
flush_dcache_range(unsigned long start,unsigned long stop)1472c451f78SAneesh V void flush_dcache_range(unsigned long start, unsigned long stop)
1482c451f78SAneesh V {
149*7e781998SJoseph Chen #ifdef DEBUG
15011aa6a32SMarek Vasut 	check_cache_range(start, stop);
151*7e781998SJoseph Chen #endif
1522c451f78SAneesh V 	v7_dcache_maint_range(start, stop, ARMV7_DCACHE_CLEAN_INVAL_RANGE);
1532c451f78SAneesh V 
1542c451f78SAneesh V 	v7_outer_cache_flush_range(start, stop);
1552c451f78SAneesh V }
1562c451f78SAneesh V 
arm_init_before_mmu(void)1572c451f78SAneesh V void arm_init_before_mmu(void)
1582c451f78SAneesh V {
1592c451f78SAneesh V 	v7_outer_cache_enable();
1602c451f78SAneesh V 	invalidate_dcache_all();
1612c451f78SAneesh V 	v7_inval_tlb();
1622c451f78SAneesh V }
1632c451f78SAneesh V 
mmu_page_table_flush(unsigned long start,unsigned long stop)1640dde7f53SSimon Glass void mmu_page_table_flush(unsigned long start, unsigned long stop)
1650dde7f53SSimon Glass {
1660dde7f53SSimon Glass 	flush_dcache_range(start, stop);
1670dde7f53SSimon Glass 	v7_inval_tlb();
1680dde7f53SSimon Glass }
1692c451f78SAneesh V #else /* #ifndef CONFIG_SYS_DCACHE_OFF */
invalidate_dcache_all(void)1702c451f78SAneesh V void invalidate_dcache_all(void)
1712c451f78SAneesh V {
1722c451f78SAneesh V }
1732c451f78SAneesh V 
flush_dcache_all(void)1742c451f78SAneesh V void flush_dcache_all(void)
1752c451f78SAneesh V {
1762c451f78SAneesh V }
1772c451f78SAneesh V 
invalidate_dcache_range(unsigned long start,unsigned long stop)178ec6f6100SDaniel Allred void invalidate_dcache_range(unsigned long start, unsigned long stop)
179ec6f6100SDaniel Allred {
180ec6f6100SDaniel Allred }
181ec6f6100SDaniel Allred 
flush_dcache_range(unsigned long start,unsigned long stop)182ec6f6100SDaniel Allred void flush_dcache_range(unsigned long start, unsigned long stop)
183ec6f6100SDaniel Allred {
184ec6f6100SDaniel Allred }
185ec6f6100SDaniel Allred 
arm_init_before_mmu(void)1862c451f78SAneesh V void arm_init_before_mmu(void)
1872c451f78SAneesh V {
1882c451f78SAneesh V }
1892c451f78SAneesh V 
mmu_page_table_flush(unsigned long start,unsigned long stop)1900dde7f53SSimon Glass void mmu_page_table_flush(unsigned long start, unsigned long stop)
1910dde7f53SSimon Glass {
1920dde7f53SSimon Glass }
1930dde7f53SSimon Glass 
arm_init_domains(void)194de63ac27SR Sricharan void arm_init_domains(void)
195de63ac27SR Sricharan {
196de63ac27SR Sricharan }
1972c451f78SAneesh V #endif /* #ifndef CONFIG_SYS_DCACHE_OFF */
1982c451f78SAneesh V 
1992c451f78SAneesh V #ifndef CONFIG_SYS_ICACHE_OFF
2002c451f78SAneesh V /* Invalidate entire I-cache and branch predictor array */
invalidate_icache_all(void)2012c451f78SAneesh V void invalidate_icache_all(void)
2022c451f78SAneesh V {
2032c451f78SAneesh V 	/*
2042c451f78SAneesh V 	 * Invalidate all instruction caches to PoU.
2052c451f78SAneesh V 	 * Also flushes branch target cache.
2062c451f78SAneesh V 	 */
2072c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
2082c451f78SAneesh V 
2092c451f78SAneesh V 	/* Invalidate entire branch predictor array */
2102c451f78SAneesh V 	asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0));
2112c451f78SAneesh V 
2122c451f78SAneesh V 	/* Full system DSB - make sure that the invalidation is complete */
213a78cd861STom Rini 	dsb();
2142c451f78SAneesh V 
2152c451f78SAneesh V 	/* ISB - make sure the instruction stream sees it */
216a78cd861STom Rini 	isb();
2172c451f78SAneesh V }
2182c451f78SAneesh V #else
invalidate_icache_all(void)2192c451f78SAneesh V void invalidate_icache_all(void)
2202c451f78SAneesh V {
2212c451f78SAneesh V }
2222c451f78SAneesh V #endif
2232c451f78SAneesh V 
224fcfddfd5SJeroen Hofstee /*  Stub implementations for outer cache operations */
v7_outer_cache_enable(void)225fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_enable(void) {}
v7_outer_cache_disable(void)226fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_disable(void) {}
v7_outer_cache_flush_all(void)227fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_flush_all(void) {}
v7_outer_cache_inval_all(void)228fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_inval_all(void) {}
v7_outer_cache_flush_range(u32 start,u32 end)229fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_flush_range(u32 start, u32 end) {}
v7_outer_cache_inval_range(u32 start,u32 end)230fcfddfd5SJeroen Hofstee __weak void v7_outer_cache_inval_range(u32 start, u32 end) {}
231