xref: /openbmc/linux/arch/arm/mach-omap2/sram.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2bb772094STony Lindgren /*
3bb772094STony Lindgren  *
4bb772094STony Lindgren  * OMAP SRAM detection and management
5bb772094STony Lindgren  *
6bb772094STony Lindgren  * Copyright (C) 2005 Nokia Corporation
7bb772094STony Lindgren  * Written by Tony Lindgren <tony@atomide.com>
8bb772094STony Lindgren  *
9bb772094STony Lindgren  * Copyright (C) 2009-2012 Texas Instruments
10bb772094STony Lindgren  * Added OMAP4/5 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
11bb772094STony Lindgren  */
12bb772094STony Lindgren 
13bb772094STony Lindgren #include <linux/module.h>
14bb772094STony Lindgren #include <linux/kernel.h>
15bb772094STony Lindgren #include <linux/init.h>
16bb772094STony Lindgren #include <linux/io.h>
17d48567c9SPeter Zijlstra #include <linux/set_memory.h>
18bb772094STony Lindgren 
19bb772094STony Lindgren #include <asm/fncpy.h>
20bb772094STony Lindgren #include <asm/tlb.h>
21bb772094STony Lindgren #include <asm/cacheflush.h>
22bb772094STony Lindgren 
23bb772094STony Lindgren #include <asm/mach/map.h>
24bb772094STony Lindgren 
25bb772094STony Lindgren #include "soc.h"
26bb772094STony Lindgren #include "iomap.h"
27bb772094STony Lindgren #include "prm2xxx_3xxx.h"
28bb772094STony Lindgren #include "sdrc.h"
29bb772094STony Lindgren #include "sram.h"
30bb772094STony Lindgren 
31bb772094STony Lindgren #define OMAP2_SRAM_PUB_PA	(OMAP2_SRAM_PA + 0xf800)
32bb772094STony Lindgren #define OMAP3_SRAM_PUB_PA       (OMAP3_SRAM_PA + 0x8000)
33bb772094STony Lindgren 
34bb772094STony Lindgren #define SRAM_BOOTLOADER_SZ	0x00
35bb772094STony Lindgren 
36bb772094STony Lindgren #define OMAP24XX_VA_REQINFOPERM0	OMAP2_L3_IO_ADDRESS(0x68005048)
37bb772094STony Lindgren #define OMAP24XX_VA_READPERM0		OMAP2_L3_IO_ADDRESS(0x68005050)
38bb772094STony Lindgren #define OMAP24XX_VA_WRITEPERM0		OMAP2_L3_IO_ADDRESS(0x68005058)
39bb772094STony Lindgren 
40bb772094STony Lindgren #define OMAP34XX_VA_REQINFOPERM0	OMAP2_L3_IO_ADDRESS(0x68012848)
41bb772094STony Lindgren #define OMAP34XX_VA_READPERM0		OMAP2_L3_IO_ADDRESS(0x68012850)
42bb772094STony Lindgren #define OMAP34XX_VA_WRITEPERM0		OMAP2_L3_IO_ADDRESS(0x68012858)
43bb772094STony Lindgren #define OMAP34XX_VA_ADDR_MATCH2		OMAP2_L3_IO_ADDRESS(0x68012880)
44bb772094STony Lindgren #define OMAP34XX_VA_SMS_RG_ATT0		OMAP2_L3_IO_ADDRESS(0x6C000048)
45bb772094STony Lindgren 
46bb772094STony Lindgren #define GP_DEVICE		0x300
47bb772094STony Lindgren 
48bb772094STony Lindgren #define ROUND_DOWN(value, boundary)	((value) & (~((boundary) - 1)))
49bb772094STony Lindgren 
50bb772094STony Lindgren static unsigned long omap_sram_start;
51bb772094STony Lindgren static unsigned long omap_sram_size;
5211237651SArnd Bergmann static void __iomem *omap_sram_base;
5311237651SArnd Bergmann static unsigned long omap_sram_skip;
5411237651SArnd Bergmann static void __iomem *omap_sram_ceil;
5511237651SArnd Bergmann 
5611237651SArnd Bergmann /*
5711237651SArnd Bergmann  * Memory allocator for SRAM: calculates the new ceiling address
5811237651SArnd Bergmann  * for pushing a function using the fncpy API.
5911237651SArnd Bergmann  *
6011237651SArnd Bergmann  * Note that fncpy requires the returned address to be aligned
6111237651SArnd Bergmann  * to an 8-byte boundary.
6211237651SArnd Bergmann  */
omap_sram_push_address(unsigned long size)6311237651SArnd Bergmann static void *omap_sram_push_address(unsigned long size)
6411237651SArnd Bergmann {
6511237651SArnd Bergmann 	unsigned long available, new_ceil = (unsigned long)omap_sram_ceil;
6611237651SArnd Bergmann 
6711237651SArnd Bergmann 	available = omap_sram_ceil - (omap_sram_base + omap_sram_skip);
6811237651SArnd Bergmann 
6911237651SArnd Bergmann 	if (size > available) {
7011237651SArnd Bergmann 		pr_err("Not enough space in SRAM\n");
7111237651SArnd Bergmann 		return NULL;
7211237651SArnd Bergmann 	}
7311237651SArnd Bergmann 
7411237651SArnd Bergmann 	new_ceil -= size;
7511237651SArnd Bergmann 	new_ceil = ROUND_DOWN(new_ceil, FNCPY_ALIGN);
7611237651SArnd Bergmann 	omap_sram_ceil = IOMEM(new_ceil);
7711237651SArnd Bergmann 
78deb44711SArnd Bergmann 	return (void __force *)omap_sram_ceil;
7911237651SArnd Bergmann }
8011237651SArnd Bergmann 
omap_sram_push(void * funcp,unsigned long size)8111237651SArnd Bergmann void *omap_sram_push(void *funcp, unsigned long size)
8211237651SArnd Bergmann {
8311237651SArnd Bergmann 	void *sram;
8411237651SArnd Bergmann 	unsigned long base;
8511237651SArnd Bergmann 	int pages;
8611237651SArnd Bergmann 	void *dst = NULL;
8711237651SArnd Bergmann 
8811237651SArnd Bergmann 	sram = omap_sram_push_address(size);
8911237651SArnd Bergmann 	if (!sram)
9011237651SArnd Bergmann 		return NULL;
9111237651SArnd Bergmann 
9211237651SArnd Bergmann 	base = (unsigned long)sram & PAGE_MASK;
9311237651SArnd Bergmann 	pages = PAGE_ALIGN(size) / PAGE_SIZE;
9411237651SArnd Bergmann 
9511237651SArnd Bergmann 	set_memory_rw(base, pages);
9611237651SArnd Bergmann 
9711237651SArnd Bergmann 	dst = fncpy(sram, funcp, size);
9811237651SArnd Bergmann 
99d48567c9SPeter Zijlstra 	set_memory_rox(base, pages);
10011237651SArnd Bergmann 
10111237651SArnd Bergmann 	return dst;
10211237651SArnd Bergmann }
10311237651SArnd Bergmann 
10411237651SArnd Bergmann /*
10511237651SArnd Bergmann  * The SRAM context is lost during off-idle and stack
10611237651SArnd Bergmann  * needs to be reset.
10711237651SArnd Bergmann  */
omap_sram_reset(void)10811237651SArnd Bergmann static void omap_sram_reset(void)
10911237651SArnd Bergmann {
11011237651SArnd Bergmann 	omap_sram_ceil = omap_sram_base + omap_sram_size;
11111237651SArnd Bergmann }
112bb772094STony Lindgren 
113bb772094STony Lindgren /*
114bb772094STony Lindgren  * Depending on the target RAMFS firewall setup, the public usable amount of
115bb772094STony Lindgren  * SRAM varies.  The default accessible size for all device types is 2k. A GP
116bb772094STony Lindgren  * device allows ARM11 but not other initiators for full size. This
117bb772094STony Lindgren  * functionality seems ok until some nice security API happens.
118bb772094STony Lindgren  */
is_sram_locked(void)119bb772094STony Lindgren static int is_sram_locked(void)
120bb772094STony Lindgren {
121*b5b2e006SFranziska Naepelt 	if (omap_type() == OMAP2_DEVICE_TYPE_GP) {
122bb772094STony Lindgren 		/* RAMFW: R/W access to all initiators for all qualifier sets */
123bb772094STony Lindgren 		if (cpu_is_omap242x()) {
124edfaf05cSVictor Kamensky 			writel_relaxed(0xFF, OMAP24XX_VA_REQINFOPERM0); /* all q-vects */
125edfaf05cSVictor Kamensky 			writel_relaxed(0xCFDE, OMAP24XX_VA_READPERM0);  /* all i-read */
126edfaf05cSVictor Kamensky 			writel_relaxed(0xCFDE, OMAP24XX_VA_WRITEPERM0); /* all i-write */
127bb772094STony Lindgren 		}
128bb772094STony Lindgren 		if (cpu_is_omap34xx()) {
129edfaf05cSVictor Kamensky 			writel_relaxed(0xFFFF, OMAP34XX_VA_REQINFOPERM0); /* all q-vects */
130edfaf05cSVictor Kamensky 			writel_relaxed(0xFFFF, OMAP34XX_VA_READPERM0);  /* all i-read */
131edfaf05cSVictor Kamensky 			writel_relaxed(0xFFFF, OMAP34XX_VA_WRITEPERM0); /* all i-write */
132edfaf05cSVictor Kamensky 			writel_relaxed(0x0, OMAP34XX_VA_ADDR_MATCH2);
133edfaf05cSVictor Kamensky 			writel_relaxed(0xFFFFFFFF, OMAP34XX_VA_SMS_RG_ATT0);
134bb772094STony Lindgren 		}
135bb772094STony Lindgren 		return 0;
136bb772094STony Lindgren 	} else
137bb772094STony Lindgren 		return 1; /* assume locked with no PPA or security driver */
138bb772094STony Lindgren }
139bb772094STony Lindgren 
140bb772094STony Lindgren /*
141bb772094STony Lindgren  * The amount of SRAM depends on the core type.
142bb772094STony Lindgren  * Note that we cannot try to test for SRAM here because writes
143bb772094STony Lindgren  * to secure SRAM will hang the system. Also the SRAM is not
144bb772094STony Lindgren  * yet mapped at this point.
145bb772094STony Lindgren  */
omap_detect_sram(void)146bb772094STony Lindgren static void __init omap_detect_sram(void)
147bb772094STony Lindgren {
148bb772094STony Lindgren 	omap_sram_skip = SRAM_BOOTLOADER_SZ;
149bb772094STony Lindgren 	if (is_sram_locked()) {
150bb772094STony Lindgren 		if (cpu_is_omap34xx()) {
151bb772094STony Lindgren 			omap_sram_start = OMAP3_SRAM_PUB_PA;
152bb772094STony Lindgren 			if ((omap_type() == OMAP2_DEVICE_TYPE_EMU) ||
153bb772094STony Lindgren 			    (omap_type() == OMAP2_DEVICE_TYPE_SEC)) {
154bb772094STony Lindgren 				omap_sram_size = 0x7000; /* 28K */
155bb772094STony Lindgren 				omap_sram_skip += SZ_16K;
156bb772094STony Lindgren 			} else {
157bb772094STony Lindgren 				omap_sram_size = 0x8000; /* 32K */
158bb772094STony Lindgren 			}
159bb772094STony Lindgren 		} else {
160bb772094STony Lindgren 			omap_sram_start = OMAP2_SRAM_PUB_PA;
161bb772094STony Lindgren 			omap_sram_size = 0x800; /* 2K */
162bb772094STony Lindgren 		}
163bb772094STony Lindgren 	} else {
1648b9a2810SRajendra Nayak 		if (cpu_is_omap34xx()) {
165bb772094STony Lindgren 			omap_sram_start = OMAP3_SRAM_PA;
166bb772094STony Lindgren 			omap_sram_size = 0x10000; /* 64K */
167bb772094STony Lindgren 		} else {
168bb772094STony Lindgren 			omap_sram_start = OMAP2_SRAM_PA;
169bb772094STony Lindgren 			if (cpu_is_omap242x())
170bb772094STony Lindgren 				omap_sram_size = 0xa0000; /* 640K */
171bb772094STony Lindgren 			else if (cpu_is_omap243x())
172bb772094STony Lindgren 				omap_sram_size = 0x10000; /* 64K */
173bb772094STony Lindgren 		}
174bb772094STony Lindgren 	}
175bb772094STony Lindgren }
176bb772094STony Lindgren 
177bb772094STony Lindgren /*
178bb772094STony Lindgren  * Note that we cannot use ioremap for SRAM, as clock init needs SRAM early.
179bb772094STony Lindgren  */
omap2_map_sram(void)180bb772094STony Lindgren static void __init omap2_map_sram(void)
181bb772094STony Lindgren {
18211237651SArnd Bergmann 	unsigned long base;
18311237651SArnd Bergmann 	int pages;
184bb772094STony Lindgren 	int cached = 1;
185bb772094STony Lindgren 
186bb772094STony Lindgren 	if (cpu_is_omap34xx()) {
187bb772094STony Lindgren 		/*
188bb772094STony Lindgren 		 * SRAM must be marked as non-cached on OMAP3 since the
189bb772094STony Lindgren 		 * CORE DPLL M2 divider change code (in SRAM) runs with the
190bb772094STony Lindgren 		 * SDRAM controller disabled, and if it is marked cached,
191bb772094STony Lindgren 		 * the ARM may attempt to write cache lines back to SDRAM
192bb772094STony Lindgren 		 * which will cause the system to hang.
193bb772094STony Lindgren 		 */
194bb772094STony Lindgren 		cached = 0;
195bb772094STony Lindgren 	}
196bb772094STony Lindgren 
19711237651SArnd Bergmann 	if (omap_sram_size == 0)
19811237651SArnd Bergmann 		return;
19911237651SArnd Bergmann 
20011237651SArnd Bergmann 	omap_sram_start = ROUND_DOWN(omap_sram_start, PAGE_SIZE);
20111237651SArnd Bergmann 	omap_sram_base = __arm_ioremap_exec(omap_sram_start, omap_sram_size, cached);
20211237651SArnd Bergmann 	if (!omap_sram_base) {
20311237651SArnd Bergmann 		pr_err("SRAM: Could not map\n");
20411237651SArnd Bergmann 		return;
20511237651SArnd Bergmann 	}
20611237651SArnd Bergmann 
20711237651SArnd Bergmann 	omap_sram_reset();
20811237651SArnd Bergmann 
20911237651SArnd Bergmann 	/*
21011237651SArnd Bergmann 	 * Looks like we need to preserve some bootloader code at the
21111237651SArnd Bergmann 	 * beginning of SRAM for jumping to flash for reboot to work...
21211237651SArnd Bergmann 	 */
21311237651SArnd Bergmann 	memset_io(omap_sram_base + omap_sram_skip, 0,
21411237651SArnd Bergmann 		  omap_sram_size - omap_sram_skip);
21511237651SArnd Bergmann 
21611237651SArnd Bergmann 	base = (unsigned long)omap_sram_base;
21711237651SArnd Bergmann 	pages = PAGE_ALIGN(omap_sram_size) / PAGE_SIZE;
21811237651SArnd Bergmann 
219d48567c9SPeter Zijlstra 	set_memory_rox(base, pages);
220bb772094STony Lindgren }
221bb772094STony Lindgren 
222bb772094STony Lindgren static void (*_omap2_sram_ddr_init)(u32 *slow_dll_ctrl, u32 fast_dll_ctrl,
223bb772094STony Lindgren 			      u32 base_cs, u32 force_unlock);
224bb772094STony Lindgren 
omap2_sram_ddr_init(u32 * slow_dll_ctrl,u32 fast_dll_ctrl,u32 base_cs,u32 force_unlock)225bb772094STony Lindgren void omap2_sram_ddr_init(u32 *slow_dll_ctrl, u32 fast_dll_ctrl,
226bb772094STony Lindgren 		   u32 base_cs, u32 force_unlock)
227bb772094STony Lindgren {
228bb772094STony Lindgren 	BUG_ON(!_omap2_sram_ddr_init);
229bb772094STony Lindgren 	_omap2_sram_ddr_init(slow_dll_ctrl, fast_dll_ctrl,
230bb772094STony Lindgren 			     base_cs, force_unlock);
231bb772094STony Lindgren }
232bb772094STony Lindgren 
233bb772094STony Lindgren static void (*_omap2_sram_reprogram_sdrc)(u32 perf_level, u32 dll_val,
234bb772094STony Lindgren 					  u32 mem_type);
235bb772094STony Lindgren 
omap2_sram_reprogram_sdrc(u32 perf_level,u32 dll_val,u32 mem_type)236bb772094STony Lindgren void omap2_sram_reprogram_sdrc(u32 perf_level, u32 dll_val, u32 mem_type)
237bb772094STony Lindgren {
238bb772094STony Lindgren 	BUG_ON(!_omap2_sram_reprogram_sdrc);
239bb772094STony Lindgren 	_omap2_sram_reprogram_sdrc(perf_level, dll_val, mem_type);
240bb772094STony Lindgren }
241bb772094STony Lindgren 
242bb772094STony Lindgren static u32 (*_omap2_set_prcm)(u32 dpll_ctrl_val, u32 sdrc_rfr_val, int bypass);
243bb772094STony Lindgren 
omap2_set_prcm(u32 dpll_ctrl_val,u32 sdrc_rfr_val,int bypass)244bb772094STony Lindgren u32 omap2_set_prcm(u32 dpll_ctrl_val, u32 sdrc_rfr_val, int bypass)
245bb772094STony Lindgren {
246bb772094STony Lindgren 	BUG_ON(!_omap2_set_prcm);
247bb772094STony Lindgren 	return _omap2_set_prcm(dpll_ctrl_val, sdrc_rfr_val, bypass);
248bb772094STony Lindgren }
249bb772094STony Lindgren 
250bb772094STony Lindgren #ifdef CONFIG_SOC_OMAP2420
omap242x_sram_init(void)251bb772094STony Lindgren static int __init omap242x_sram_init(void)
252bb772094STony Lindgren {
253bb772094STony Lindgren 	_omap2_sram_ddr_init = omap_sram_push(omap242x_sram_ddr_init,
254bb772094STony Lindgren 					omap242x_sram_ddr_init_sz);
255bb772094STony Lindgren 
256bb772094STony Lindgren 	_omap2_sram_reprogram_sdrc = omap_sram_push(omap242x_sram_reprogram_sdrc,
257bb772094STony Lindgren 					    omap242x_sram_reprogram_sdrc_sz);
258bb772094STony Lindgren 
259bb772094STony Lindgren 	_omap2_set_prcm = omap_sram_push(omap242x_sram_set_prcm,
260bb772094STony Lindgren 					 omap242x_sram_set_prcm_sz);
261bb772094STony Lindgren 
262bb772094STony Lindgren 	return 0;
263bb772094STony Lindgren }
264bb772094STony Lindgren #else
omap242x_sram_init(void)265bb772094STony Lindgren static inline int omap242x_sram_init(void)
266bb772094STony Lindgren {
267bb772094STony Lindgren 	return 0;
268bb772094STony Lindgren }
269bb772094STony Lindgren #endif
270bb772094STony Lindgren 
271bb772094STony Lindgren #ifdef CONFIG_SOC_OMAP2430
omap243x_sram_init(void)272bb772094STony Lindgren static int __init omap243x_sram_init(void)
273bb772094STony Lindgren {
274bb772094STony Lindgren 	_omap2_sram_ddr_init = omap_sram_push(omap243x_sram_ddr_init,
275bb772094STony Lindgren 					omap243x_sram_ddr_init_sz);
276bb772094STony Lindgren 
277bb772094STony Lindgren 	_omap2_sram_reprogram_sdrc = omap_sram_push(omap243x_sram_reprogram_sdrc,
278bb772094STony Lindgren 					    omap243x_sram_reprogram_sdrc_sz);
279bb772094STony Lindgren 
280bb772094STony Lindgren 	_omap2_set_prcm = omap_sram_push(omap243x_sram_set_prcm,
281bb772094STony Lindgren 					 omap243x_sram_set_prcm_sz);
282bb772094STony Lindgren 
283bb772094STony Lindgren 	return 0;
284bb772094STony Lindgren }
285bb772094STony Lindgren #else
omap243x_sram_init(void)286bb772094STony Lindgren static inline int omap243x_sram_init(void)
287bb772094STony Lindgren {
288bb772094STony Lindgren 	return 0;
289bb772094STony Lindgren }
290bb772094STony Lindgren #endif
291bb772094STony Lindgren 
292bb772094STony Lindgren #ifdef CONFIG_ARCH_OMAP3
293bb772094STony Lindgren 
omap3_sram_restore_context(void)294bb772094STony Lindgren void omap3_sram_restore_context(void)
295bb772094STony Lindgren {
296bb772094STony Lindgren 	omap_sram_reset();
297bb772094STony Lindgren 
298bb772094STony Lindgren 	omap_push_sram_idle();
299bb772094STony Lindgren }
300bb772094STony Lindgren 
omap34xx_sram_init(void)301bb772094STony Lindgren static inline int omap34xx_sram_init(void)
302bb772094STony Lindgren {
303bb772094STony Lindgren 	omap3_sram_restore_context();
304bb772094STony Lindgren 	return 0;
305bb772094STony Lindgren }
306bb772094STony Lindgren #else
omap34xx_sram_init(void)307bb772094STony Lindgren static inline int omap34xx_sram_init(void)
308bb772094STony Lindgren {
309bb772094STony Lindgren 	return 0;
310bb772094STony Lindgren }
311bb772094STony Lindgren #endif /* CONFIG_ARCH_OMAP3 */
312bb772094STony Lindgren 
omap_sram_init(void)313bb772094STony Lindgren int __init omap_sram_init(void)
314bb772094STony Lindgren {
315bb772094STony Lindgren 	omap_detect_sram();
316bb772094STony Lindgren 	omap2_map_sram();
317bb772094STony Lindgren 
318bb772094STony Lindgren 	if (cpu_is_omap242x())
319bb772094STony Lindgren 		omap242x_sram_init();
320bb772094STony Lindgren 	else if (cpu_is_omap2430())
321bb772094STony Lindgren 		omap243x_sram_init();
322bb772094STony Lindgren 	else if (cpu_is_omap34xx())
323bb772094STony Lindgren 		omap34xx_sram_init();
324bb772094STony Lindgren 
325bb772094STony Lindgren 	return 0;
326bb772094STony Lindgren }
327