xref: /openbmc/u-boot/arch/arm/mach-imx/cache.c (revision db4a2999)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2552a848eSStefano Babic /*
3552a848eSStefano Babic  * Copyright 2015 Freescale Semiconductor, Inc.
4552a848eSStefano Babic  */
5552a848eSStefano Babic 
6552a848eSStefano Babic #include <common.h>
7552a848eSStefano Babic #include <asm/armv7.h>
8552a848eSStefano Babic #include <asm/pl310.h>
9552a848eSStefano Babic #include <asm/io.h>
10552a848eSStefano Babic #include <asm/mach-imx/sys_proto.h>
11552a848eSStefano Babic 
enable_ca7_smp(void)12c5437e5bSYe Li static void enable_ca7_smp(void)
13c5437e5bSYe Li {
14c5437e5bSYe Li 	u32 val;
15c5437e5bSYe Li 
16c5437e5bSYe Li 	/* Read MIDR */
17c5437e5bSYe Li 	asm volatile ("mrc p15, 0, %0, c0, c0, 0\n\t" : "=r"(val));
18c5437e5bSYe Li 	val = (val >> 4);
19c5437e5bSYe Li 	val &= 0xf;
20c5437e5bSYe Li 
21c5437e5bSYe Li 	/* Only set the SMP for Cortex A7 */
22c5437e5bSYe Li 	if (val == 0x7) {
23c5437e5bSYe Li 		/* Read auxiliary control register */
24c5437e5bSYe Li 		asm volatile ("mrc p15, 0, %0, c1, c0, 1\n\t" : "=r"(val));
25c5437e5bSYe Li 
26c5437e5bSYe Li 		if (val & (1 << 6))
27c5437e5bSYe Li 			return;
28c5437e5bSYe Li 
29c5437e5bSYe Li 		/* Enable SMP */
30c5437e5bSYe Li 		val |= (1 << 6);
31c5437e5bSYe Li 
32c5437e5bSYe Li 		/* Write auxiliary control register */
33c5437e5bSYe Li 		asm volatile ("mcr p15, 0, %0, c1, c0, 1\n\t" : : "r"(val));
34c5437e5bSYe Li 
35c5437e5bSYe Li 		DSB;
36c5437e5bSYe Li 		ISB;
37c5437e5bSYe Li 	}
38c5437e5bSYe Li }
39c5437e5bSYe Li 
40552a848eSStefano Babic #ifndef CONFIG_SYS_DCACHE_OFF
enable_caches(void)41552a848eSStefano Babic void enable_caches(void)
42552a848eSStefano Babic {
43552a848eSStefano Babic #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
44552a848eSStefano Babic 	enum dcache_option option = DCACHE_WRITETHROUGH;
45552a848eSStefano Babic #else
46552a848eSStefano Babic 	enum dcache_option option = DCACHE_WRITEBACK;
47552a848eSStefano Babic #endif
48552a848eSStefano Babic 	/* Avoid random hang when download by usb */
49552a848eSStefano Babic 	invalidate_dcache_all();
50552a848eSStefano Babic 
51c5437e5bSYe Li 	/* Set ACTLR.SMP bit for Cortex-A7 */
52c5437e5bSYe Li 	enable_ca7_smp();
53c5437e5bSYe Li 
54552a848eSStefano Babic 	/* Enable D-cache. I-cache is already enabled in start.S */
55552a848eSStefano Babic 	dcache_enable();
56552a848eSStefano Babic 
57552a848eSStefano Babic 	/* Enable caching on OCRAM and ROM */
58552a848eSStefano Babic 	mmu_set_region_dcache_behaviour(ROMCP_ARB_BASE_ADDR,
59552a848eSStefano Babic 					ROMCP_ARB_END_ADDR,
60552a848eSStefano Babic 					option);
61552a848eSStefano Babic 	mmu_set_region_dcache_behaviour(IRAM_BASE_ADDR,
62552a848eSStefano Babic 					IRAM_SIZE,
63552a848eSStefano Babic 					option);
64552a848eSStefano Babic }
65c5437e5bSYe Li #else
enable_caches(void)66c5437e5bSYe Li void enable_caches(void)
67c5437e5bSYe Li {
68c5437e5bSYe Li 	/*
69c5437e5bSYe Li 	 * Set ACTLR.SMP bit for Cortex-A7, even if the caches are
70c5437e5bSYe Li 	 * disabled by u-boot
71c5437e5bSYe Li 	 */
72c5437e5bSYe Li 	enable_ca7_smp();
73c5437e5bSYe Li 
74c5437e5bSYe Li 	puts("WARNING: Caches not enabled\n");
75c5437e5bSYe Li }
76552a848eSStefano Babic #endif
77552a848eSStefano Babic 
78552a848eSStefano Babic #ifndef CONFIG_SYS_L2CACHE_OFF
79552a848eSStefano Babic #ifdef CONFIG_SYS_L2_PL310
80552a848eSStefano Babic #define IOMUXC_GPR11_L2CACHE_AS_OCRAM 0x00000002
v7_outer_cache_enable(void)81552a848eSStefano Babic void v7_outer_cache_enable(void)
82552a848eSStefano Babic {
83552a848eSStefano Babic 	struct pl310_regs *const pl310 = (struct pl310_regs *)L2_PL310_BASE;
84552a848eSStefano Babic 	struct iomuxc *iomux = (struct iomuxc *)IOMUXC_BASE_ADDR;
85*d8bbf362SYe Li 	unsigned int val, cache_id;
86552a848eSStefano Babic 
87552a848eSStefano Babic 
88552a848eSStefano Babic 	/*
89552a848eSStefano Babic 	 * Must disable the L2 before changing the latency parameters
90552a848eSStefano Babic 	 * and auxiliary control register.
91552a848eSStefano Babic 	 */
92552a848eSStefano Babic 	clrbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
93552a848eSStefano Babic 
94552a848eSStefano Babic 	/*
95552a848eSStefano Babic 	 * Set bit 22 in the auxiliary control register. If this bit
96552a848eSStefano Babic 	 * is cleared, PL310 treats Normal Shared Non-cacheable
97552a848eSStefano Babic 	 * accesses as Cacheable no-allocate.
98552a848eSStefano Babic 	 */
99552a848eSStefano Babic 	setbits_le32(&pl310->pl310_aux_ctrl, L310_SHARED_ATT_OVERRIDE_ENABLE);
100552a848eSStefano Babic 
101552a848eSStefano Babic 	if (is_mx6sl() || is_mx6sll()) {
102552a848eSStefano Babic 		val = readl(&iomux->gpr[11]);
103552a848eSStefano Babic 		if (val & IOMUXC_GPR11_L2CACHE_AS_OCRAM) {
104552a848eSStefano Babic 			/* L2 cache configured as OCRAM, reset it */
105552a848eSStefano Babic 			val &= ~IOMUXC_GPR11_L2CACHE_AS_OCRAM;
106552a848eSStefano Babic 			writel(val, &iomux->gpr[11]);
107552a848eSStefano Babic 		}
108552a848eSStefano Babic 	}
109552a848eSStefano Babic 
110552a848eSStefano Babic 	writel(0x132, &pl310->pl310_tag_latency_ctrl);
111552a848eSStefano Babic 	writel(0x132, &pl310->pl310_data_latency_ctrl);
112552a848eSStefano Babic 
113552a848eSStefano Babic 	val = readl(&pl310->pl310_prefetch_ctrl);
114552a848eSStefano Babic 
115*d8bbf362SYe Li 	/* Turn on the L2 I/D prefetch, double linefill */
116*d8bbf362SYe Li 	/* Set prefetch offset with any value except 23 as per errata 765569 */
117*d8bbf362SYe Li 	val |= 0x7000000f;
118552a848eSStefano Babic 
119552a848eSStefano Babic 	/*
120552a848eSStefano Babic 	 * The L2 cache controller(PL310) version on the i.MX6D/Q is r3p1-50rel0
121*d8bbf362SYe Li 	 * The L2 cache controller(PL310) version on the i.MX6DL/SOLO/SL/SX/DQP
122*d8bbf362SYe Li 	 * is r3p2.
123552a848eSStefano Babic 	 * But according to ARM PL310 errata: 752271
124552a848eSStefano Babic 	 * ID: 752271: Double linefill feature can cause data corruption
125552a848eSStefano Babic 	 * Fault Status: Present in: r3p0, r3p1, r3p1-50rel0. Fixed in r3p2
126552a848eSStefano Babic 	 * Workaround: The only workaround to this erratum is to disable the
127552a848eSStefano Babic 	 * double linefill feature. This is the default behavior.
128552a848eSStefano Babic 	 */
129*d8bbf362SYe Li 	cache_id = readl(&pl310->pl310_cache_id);
130*d8bbf362SYe Li 	if (((cache_id & L2X0_CACHE_ID_PART_MASK) == L2X0_CACHE_ID_PART_L310)
131*d8bbf362SYe Li 	    && ((cache_id & L2X0_CACHE_ID_RTL_MASK) < L2X0_CACHE_ID_RTL_R3P2))
132*d8bbf362SYe Li 		val &= ~(1 << 30);
133552a848eSStefano Babic 	writel(val, &pl310->pl310_prefetch_ctrl);
134552a848eSStefano Babic 
135552a848eSStefano Babic 	val = readl(&pl310->pl310_power_ctrl);
136552a848eSStefano Babic 	val |= L2X0_DYNAMIC_CLK_GATING_EN;
137552a848eSStefano Babic 	val |= L2X0_STNDBY_MODE_EN;
138552a848eSStefano Babic 	writel(val, &pl310->pl310_power_ctrl);
139552a848eSStefano Babic 
140552a848eSStefano Babic 	setbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
141552a848eSStefano Babic }
142552a848eSStefano Babic 
v7_outer_cache_disable(void)143552a848eSStefano Babic void v7_outer_cache_disable(void)
144552a848eSStefano Babic {
145552a848eSStefano Babic 	struct pl310_regs *const pl310 = (struct pl310_regs *)L2_PL310_BASE;
146552a848eSStefano Babic 
147552a848eSStefano Babic 	clrbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
148552a848eSStefano Babic }
149552a848eSStefano Babic #endif /* !CONFIG_SYS_L2_PL310 */
150552a848eSStefano Babic #endif /* !CONFIG_SYS_L2CACHE_OFF */
151