1 // SPDX-License-Identifier: GPL-2.0-only
3 * OMAP SRAM detection and management
5 * Copyright (C) 2005 Nokia Corporation
6 * Written by Tony Lindgren <tony@atomide.com>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
14 #include <asm/fncpy.h>
16 #include <asm/cacheflush.h>
17 #include <asm/set_memory.h>
19 #include <asm/mach/map.h>
24 #define OMAP1_SRAM_PA 0x20000000
25 #define SRAM_BOOTLOADER_SZ 0x80
26 #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
28 static void __iomem *omap_sram_base;
29 static unsigned long omap_sram_start;
30 static unsigned long omap_sram_skip;
31 static unsigned long omap_sram_size;
32 static void __iomem *omap_sram_ceil;
35 * Memory allocator for SRAM: calculates the new ceiling address
36 * for pushing a function using the fncpy API.
38 * Note that fncpy requires the returned address to be aligned
39 * to an 8-byte boundary.
41 static void *omap_sram_push_address(unsigned long size)
43 unsigned long available, new_ceil = (unsigned long)omap_sram_ceil;
45 available = omap_sram_ceil - (omap_sram_base + omap_sram_skip);
47 if (size > available) {
48 pr_err("Not enough space in SRAM\n");
53 new_ceil = ROUND_DOWN(new_ceil, FNCPY_ALIGN);
54 omap_sram_ceil = IOMEM(new_ceil);
56 return (void __force *)omap_sram_ceil;
59 void *omap_sram_push(void *funcp, unsigned long size)
66 sram = omap_sram_push_address(size);
70 base = (unsigned long)sram & PAGE_MASK;
71 pages = PAGE_ALIGN(size) / PAGE_SIZE;
73 set_memory_rw(base, pages);
75 dst = fncpy(sram, funcp, size);
77 set_memory_ro(base, pages);
78 set_memory_x(base, pages);
84 * The amount of SRAM depends on the core type.
85 * Note that we cannot try to test for SRAM here because writes
86 * to secure SRAM will hang the system. Also the SRAM is not
87 * yet mapped at this point.
88 * Note that we cannot use ioremap for SRAM, as clock init needs SRAM early.
90 static void __init omap_detect_and_map_sram(void)
95 omap_sram_skip = SRAM_BOOTLOADER_SZ;
96 omap_sram_start = OMAP1_SRAM_PA;
99 omap_sram_size = 0x32000; /* 200K */
100 else if (cpu_is_omap15xx())
101 omap_sram_size = 0x30000; /* 192K */
102 else if (cpu_is_omap1610() || cpu_is_omap1611() ||
103 cpu_is_omap1621() || cpu_is_omap1710())
104 omap_sram_size = 0x4000; /* 16K */
106 pr_err("Could not detect SRAM size\n");
107 omap_sram_size = 0x4000;
110 omap_sram_start = ROUND_DOWN(omap_sram_start, PAGE_SIZE);
111 omap_sram_base = __arm_ioremap_exec(omap_sram_start, omap_sram_size, 1);
112 if (!omap_sram_base) {
113 pr_err("SRAM: Could not map\n");
117 omap_sram_ceil = omap_sram_base + omap_sram_size;
120 * Looks like we need to preserve some bootloader code at the
121 * beginning of SRAM for jumping to flash for reboot to work...
123 memset_io(omap_sram_base + omap_sram_skip, 0,
124 omap_sram_size - omap_sram_skip);
126 base = (unsigned long)omap_sram_base;
127 pages = PAGE_ALIGN(omap_sram_size) / PAGE_SIZE;
129 set_memory_ro(base, pages);
130 set_memory_x(base, pages);
133 static void (*_omap_sram_reprogram_clock)(u32 dpllctl, u32 ckctl);
135 void omap_sram_reprogram_clock(u32 dpllctl, u32 ckctl)
137 BUG_ON(!_omap_sram_reprogram_clock);
138 /* On 730, bit 13 must always be 1 */
139 if (cpu_is_omap7xx())
141 _omap_sram_reprogram_clock(dpllctl, ckctl);
144 int __init omap1_sram_init(void)
146 omap_detect_and_map_sram();
147 _omap_sram_reprogram_clock =
148 omap_sram_push(omap1_sram_reprogram_clock,
149 omap1_sram_reprogram_clock_sz);