blob: 0f1eacad7fe37d9047071ea7b52120e50a4b2403 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Tony Lindgren92105bb2005-09-07 17:20:26 +01002/*
3 * linux/arch/arm/plat-omap/sram.c
4 *
5 * OMAP SRAM detection and management
6 *
7 * Copyright (C) 2005 Nokia Corporation
8 * Written by Tony Lindgren <tony@atomide.com>
9 *
R Sricharan05e152c2012-06-05 16:21:32 +053010 * Copyright (C) 2009-2012 Texas Instruments
11 * Added OMAP4/5 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
Tony Lindgren92105bb2005-09-07 17:20:26 +010012 */
Tony Lindgrenc2d43e32008-07-03 12:24:38 +030013#undef DEBUG
Tony Lindgren92105bb2005-09-07 17:20:26 +010014
Tony Lindgren92105bb2005-09-07 17:20:26 +010015#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
Russell Kingfced80c2008-09-06 12:10:45 +010018#include <linux/io.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010019
Tony Lindgrenbf027ca2012-10-29 13:54:06 -070020#include <asm/fncpy.h>
Tony Lindgren53d9cc72006-02-08 22:06:45 +000021#include <asm/tlb.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010022#include <asm/cacheflush.h>
Tony Lindgreneb85a352018-03-21 08:16:29 -070023#include <asm/set_memory.h>
Tony Lindgren92105bb2005-09-07 17:20:26 +010024
Tony Lindgren670c1042006-04-02 17:46:25 +010025#include <asm/mach/map.h>
26
Paul Walmsley9fbe7c22012-12-28 02:09:15 -070027#include <plat/sram.h>
28
Tony Lindgren670c1042006-04-02 17:46:25 +010029#define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
Tony Lindgren92105bb2005-09-07 17:20:26 +010030
Tony Lindgrena66cb342011-10-04 13:52:57 -070031static void __iomem *omap_sram_base;
Aaro Koskinenb2856732012-08-29 18:24:31 +030032static unsigned long omap_sram_skip;
Tony Lindgren92105bb2005-09-07 17:20:26 +010033static unsigned long omap_sram_size;
Tony Lindgrena66cb342011-10-04 13:52:57 -070034static void __iomem *omap_sram_ceil;
Tony Lindgren92105bb2005-09-07 17:20:26 +010035
Imre Deakb7cc6d42007-03-06 03:16:36 -080036/*
Jean Pihetb6338bd2011-02-02 16:38:06 +010037 * Memory allocator for SRAM: calculates the new ceiling address
38 * for pushing a function using the fncpy API.
39 *
40 * Note that fncpy requires the returned address to be aligned
41 * to an 8-byte boundary.
42 */
Tony Lindgreneb85a352018-03-21 08:16:29 -070043static void *omap_sram_push_address(unsigned long size)
Tony Lindgren92105bb2005-09-07 17:20:26 +010044{
Tony Lindgrena66cb342011-10-04 13:52:57 -070045 unsigned long available, new_ceil = (unsigned long)omap_sram_ceil;
46
Aaro Koskinenb2856732012-08-29 18:24:31 +030047 available = omap_sram_ceil - (omap_sram_base + omap_sram_skip);
Tony Lindgrena66cb342011-10-04 13:52:57 -070048
49 if (size > available) {
Santosh Shilimkar26a510b2011-04-04 14:20:08 +053050 pr_err("Not enough space in SRAM\n");
Tony Lindgren92105bb2005-09-07 17:20:26 +010051 return NULL;
52 }
Tony Lindgren670c1042006-04-02 17:46:25 +010053
Tony Lindgrena66cb342011-10-04 13:52:57 -070054 new_ceil -= size;
55 new_ceil = ROUND_DOWN(new_ceil, FNCPY_ALIGN);
56 omap_sram_ceil = IOMEM(new_ceil);
Tony Lindgren92105bb2005-09-07 17:20:26 +010057
58 return (void *)omap_sram_ceil;
59}
60
Tony Lindgreneb85a352018-03-21 08:16:29 -070061void *omap_sram_push(void *funcp, unsigned long size)
62{
63 void *sram;
64 unsigned long base;
65 int pages;
66 void *dst = NULL;
67
68 sram = omap_sram_push_address(size);
69 if (!sram)
70 return NULL;
71
72 base = (unsigned long)sram & PAGE_MASK;
73 pages = PAGE_ALIGN(size) / PAGE_SIZE;
74
75 set_memory_rw(base, pages);
76
77 dst = fncpy(sram, funcp, size);
78
79 set_memory_ro(base, pages);
80 set_memory_x(base, pages);
81
82 return dst;
83}
84
Tony Lindgrend8cfd6c2012-10-29 14:36:25 -070085/*
86 * The SRAM context is lost during off-idle and stack
87 * needs to be reset.
88 */
89void omap_sram_reset(void)
90{
91 omap_sram_ceil = omap_sram_base + omap_sram_size;
92}
93
94/*
95 * Note that we cannot use ioremap for SRAM, as clock init needs SRAM early.
96 */
97void __init omap_map_sram(unsigned long start, unsigned long size,
98 unsigned long skip, int cached)
99{
Tony Lindgreneb85a352018-03-21 08:16:29 -0700100 unsigned long base;
101 int pages;
102
Tony Lindgrend8cfd6c2012-10-29 14:36:25 -0700103 if (size == 0)
104 return;
105
106 start = ROUND_DOWN(start, PAGE_SIZE);
107 omap_sram_size = size;
108 omap_sram_skip = skip;
109 omap_sram_base = __arm_ioremap_exec(start, size, cached);
110 if (!omap_sram_base) {
111 pr_err("SRAM: Could not map\n");
112 return;
113 }
114
115 omap_sram_reset();
116
117 /*
118 * Looks like we need to preserve some bootloader code at the
119 * beginning of SRAM for jumping to flash for reboot to work...
120 */
121 memset_io(omap_sram_base + omap_sram_skip, 0,
122 omap_sram_size - omap_sram_skip);
Tony Lindgreneb85a352018-03-21 08:16:29 -0700123
124 base = (unsigned long)omap_sram_base;
125 pages = PAGE_ALIGN(omap_sram_size) / PAGE_SIZE;
126
127 set_memory_ro(base, pages);
128 set_memory_x(base, pages);
Tony Lindgrend8cfd6c2012-10-29 14:36:25 -0700129}