1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f205cfafSTony Lindgren /*
3f205cfafSTony Lindgren * OMAP SRAM detection and management
4f205cfafSTony Lindgren *
5f205cfafSTony Lindgren * Copyright (C) 2005 Nokia Corporation
6f205cfafSTony Lindgren * Written by Tony Lindgren <tony@atomide.com>
7f205cfafSTony Lindgren */
8f205cfafSTony Lindgren
9f205cfafSTony Lindgren #include <linux/module.h>
10f205cfafSTony Lindgren #include <linux/kernel.h>
11f205cfafSTony Lindgren #include <linux/init.h>
12f205cfafSTony Lindgren #include <linux/io.h>
13d48567c9SPeter Zijlstra #include <linux/set_memory.h>
14f205cfafSTony Lindgren
15f205cfafSTony Lindgren #include <asm/fncpy.h>
16f205cfafSTony Lindgren #include <asm/tlb.h>
17f205cfafSTony Lindgren #include <asm/cacheflush.h>
18f205cfafSTony Lindgren
19f205cfafSTony Lindgren #include <asm/mach/map.h>
20f205cfafSTony Lindgren
21f205cfafSTony Lindgren #include "soc.h"
22f205cfafSTony Lindgren #include "sram.h"
23f205cfafSTony Lindgren
24f205cfafSTony Lindgren #define OMAP1_SRAM_PA 0x20000000
25f205cfafSTony Lindgren #define SRAM_BOOTLOADER_SZ 0x80
2611237651SArnd Bergmann #define ROUND_DOWN(value, boundary) ((value) & (~((boundary) - 1)))
2711237651SArnd Bergmann
2811237651SArnd Bergmann static void __iomem *omap_sram_base;
2911237651SArnd Bergmann static unsigned long omap_sram_start;
3011237651SArnd Bergmann static unsigned long omap_sram_skip;
3111237651SArnd Bergmann static unsigned long omap_sram_size;
3211237651SArnd Bergmann static void __iomem *omap_sram_ceil;
3311237651SArnd Bergmann
3411237651SArnd Bergmann /*
3511237651SArnd Bergmann * Memory allocator for SRAM: calculates the new ceiling address
3611237651SArnd Bergmann * for pushing a function using the fncpy API.
3711237651SArnd Bergmann *
3811237651SArnd Bergmann * Note that fncpy requires the returned address to be aligned
3911237651SArnd Bergmann * to an 8-byte boundary.
4011237651SArnd Bergmann */
omap_sram_push_address(unsigned long size)4111237651SArnd Bergmann static void *omap_sram_push_address(unsigned long size)
4211237651SArnd Bergmann {
4311237651SArnd Bergmann unsigned long available, new_ceil = (unsigned long)omap_sram_ceil;
4411237651SArnd Bergmann
4511237651SArnd Bergmann available = omap_sram_ceil - (omap_sram_base + omap_sram_skip);
4611237651SArnd Bergmann
4711237651SArnd Bergmann if (size > available) {
4811237651SArnd Bergmann pr_err("Not enough space in SRAM\n");
4911237651SArnd Bergmann return NULL;
5011237651SArnd Bergmann }
5111237651SArnd Bergmann
5211237651SArnd Bergmann new_ceil -= size;
5311237651SArnd Bergmann new_ceil = ROUND_DOWN(new_ceil, FNCPY_ALIGN);
5411237651SArnd Bergmann omap_sram_ceil = IOMEM(new_ceil);
5511237651SArnd Bergmann
56deb44711SArnd Bergmann return (void __force *)omap_sram_ceil;
5711237651SArnd Bergmann }
5811237651SArnd Bergmann
omap_sram_push(void * funcp,unsigned long size)5911237651SArnd Bergmann void *omap_sram_push(void *funcp, unsigned long size)
6011237651SArnd Bergmann {
6111237651SArnd Bergmann void *sram;
6211237651SArnd Bergmann unsigned long base;
6311237651SArnd Bergmann int pages;
6411237651SArnd Bergmann void *dst = NULL;
6511237651SArnd Bergmann
6611237651SArnd Bergmann sram = omap_sram_push_address(size);
6711237651SArnd Bergmann if (!sram)
6811237651SArnd Bergmann return NULL;
6911237651SArnd Bergmann
7011237651SArnd Bergmann base = (unsigned long)sram & PAGE_MASK;
7111237651SArnd Bergmann pages = PAGE_ALIGN(size) / PAGE_SIZE;
7211237651SArnd Bergmann
7311237651SArnd Bergmann set_memory_rw(base, pages);
7411237651SArnd Bergmann
7511237651SArnd Bergmann dst = fncpy(sram, funcp, size);
7611237651SArnd Bergmann
77d48567c9SPeter Zijlstra set_memory_rox(base, pages);
7811237651SArnd Bergmann
7911237651SArnd Bergmann return dst;
8011237651SArnd Bergmann }
81f205cfafSTony Lindgren
82f205cfafSTony Lindgren /*
83f205cfafSTony Lindgren * The amount of SRAM depends on the core type.
84f205cfafSTony Lindgren * Note that we cannot try to test for SRAM here because writes
85f205cfafSTony Lindgren * to secure SRAM will hang the system. Also the SRAM is not
86f205cfafSTony Lindgren * yet mapped at this point.
8711237651SArnd Bergmann * Note that we cannot use ioremap for SRAM, as clock init needs SRAM early.
88f205cfafSTony Lindgren */
omap_detect_and_map_sram(void)89f205cfafSTony Lindgren static void __init omap_detect_and_map_sram(void)
90f205cfafSTony Lindgren {
9111237651SArnd Bergmann unsigned long base;
9211237651SArnd Bergmann int pages;
9311237651SArnd Bergmann
9411237651SArnd Bergmann omap_sram_skip = SRAM_BOOTLOADER_SZ;
9511237651SArnd Bergmann omap_sram_start = OMAP1_SRAM_PA;
96f205cfafSTony Lindgren
97*8825acd7SArnd Bergmann if (cpu_is_omap15xx())
98f205cfafSTony Lindgren omap_sram_size = 0x30000; /* 192K */
99f205cfafSTony Lindgren else if (cpu_is_omap1610() || cpu_is_omap1611() ||
100f205cfafSTony Lindgren cpu_is_omap1621() || cpu_is_omap1710())
101f205cfafSTony Lindgren omap_sram_size = 0x4000; /* 16K */
102f205cfafSTony Lindgren else {
103f205cfafSTony Lindgren pr_err("Could not detect SRAM size\n");
104f205cfafSTony Lindgren omap_sram_size = 0x4000;
105f205cfafSTony Lindgren }
106f205cfafSTony Lindgren
10711237651SArnd Bergmann omap_sram_start = ROUND_DOWN(omap_sram_start, PAGE_SIZE);
10811237651SArnd Bergmann omap_sram_base = __arm_ioremap_exec(omap_sram_start, omap_sram_size, 1);
10911237651SArnd Bergmann if (!omap_sram_base) {
11011237651SArnd Bergmann pr_err("SRAM: Could not map\n");
11111237651SArnd Bergmann return;
11211237651SArnd Bergmann }
11311237651SArnd Bergmann
11411237651SArnd Bergmann omap_sram_ceil = omap_sram_base + omap_sram_size;
11511237651SArnd Bergmann
11611237651SArnd Bergmann /*
11711237651SArnd Bergmann * Looks like we need to preserve some bootloader code at the
11811237651SArnd Bergmann * beginning of SRAM for jumping to flash for reboot to work...
11911237651SArnd Bergmann */
12011237651SArnd Bergmann memset_io(omap_sram_base + omap_sram_skip, 0,
12111237651SArnd Bergmann omap_sram_size - omap_sram_skip);
12211237651SArnd Bergmann
12311237651SArnd Bergmann base = (unsigned long)omap_sram_base;
12411237651SArnd Bergmann pages = PAGE_ALIGN(omap_sram_size) / PAGE_SIZE;
12511237651SArnd Bergmann
126d48567c9SPeter Zijlstra set_memory_rox(base, pages);
127f205cfafSTony Lindgren }
128f205cfafSTony Lindgren
129f205cfafSTony Lindgren static void (*_omap_sram_reprogram_clock)(u32 dpllctl, u32 ckctl);
130f205cfafSTony Lindgren
omap_sram_reprogram_clock(u32 dpllctl,u32 ckctl)131f205cfafSTony Lindgren void omap_sram_reprogram_clock(u32 dpllctl, u32 ckctl)
132f205cfafSTony Lindgren {
133f205cfafSTony Lindgren BUG_ON(!_omap_sram_reprogram_clock);
134f205cfafSTony Lindgren _omap_sram_reprogram_clock(dpllctl, ckctl);
135f205cfafSTony Lindgren }
136f205cfafSTony Lindgren
omap1_sram_init(void)13711237651SArnd Bergmann int __init omap1_sram_init(void)
138f205cfafSTony Lindgren {
139f205cfafSTony Lindgren omap_detect_and_map_sram();
140f205cfafSTony Lindgren _omap_sram_reprogram_clock =
141f205cfafSTony Lindgren omap_sram_push(omap1_sram_reprogram_clock,
142f205cfafSTony Lindgren omap1_sram_reprogram_clock_sz);
143f205cfafSTony Lindgren
144f205cfafSTony Lindgren return 0;
145f205cfafSTony Lindgren }
146