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> 13f205cfafSTony Lindgren 14f205cfafSTony Lindgren #include <asm/fncpy.h> 15f205cfafSTony Lindgren #include <asm/tlb.h> 16f205cfafSTony Lindgren #include <asm/cacheflush.h> 1711237651SArnd Bergmann #include <asm/set_memory.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 */ 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 56*deb44711SArnd Bergmann return (void __force *)omap_sram_ceil; 5711237651SArnd Bergmann } 5811237651SArnd Bergmann 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 7711237651SArnd Bergmann set_memory_ro(base, pages); 7811237651SArnd Bergmann set_memory_x(base, pages); 7911237651SArnd Bergmann 8011237651SArnd Bergmann return dst; 8111237651SArnd Bergmann } 82f205cfafSTony Lindgren 83f205cfafSTony Lindgren /* 84f205cfafSTony Lindgren * The amount of SRAM depends on the core type. 85f205cfafSTony Lindgren * Note that we cannot try to test for SRAM here because writes 86f205cfafSTony Lindgren * to secure SRAM will hang the system. Also the SRAM is not 87f205cfafSTony Lindgren * yet mapped at this point. 8811237651SArnd Bergmann * Note that we cannot use ioremap for SRAM, as clock init needs SRAM early. 89f205cfafSTony Lindgren */ 90f205cfafSTony Lindgren static void __init omap_detect_and_map_sram(void) 91f205cfafSTony Lindgren { 9211237651SArnd Bergmann unsigned long base; 9311237651SArnd Bergmann int pages; 9411237651SArnd Bergmann 9511237651SArnd Bergmann omap_sram_skip = SRAM_BOOTLOADER_SZ; 9611237651SArnd Bergmann omap_sram_start = OMAP1_SRAM_PA; 97f205cfafSTony Lindgren 98f205cfafSTony Lindgren if (cpu_is_omap7xx()) 99f205cfafSTony Lindgren omap_sram_size = 0x32000; /* 200K */ 100f205cfafSTony Lindgren else if (cpu_is_omap15xx()) 101f205cfafSTony Lindgren omap_sram_size = 0x30000; /* 192K */ 102f205cfafSTony Lindgren else if (cpu_is_omap1610() || cpu_is_omap1611() || 103f205cfafSTony Lindgren cpu_is_omap1621() || cpu_is_omap1710()) 104f205cfafSTony Lindgren omap_sram_size = 0x4000; /* 16K */ 105f205cfafSTony Lindgren else { 106f205cfafSTony Lindgren pr_err("Could not detect SRAM size\n"); 107f205cfafSTony Lindgren omap_sram_size = 0x4000; 108f205cfafSTony Lindgren } 109f205cfafSTony Lindgren 11011237651SArnd Bergmann omap_sram_start = ROUND_DOWN(omap_sram_start, PAGE_SIZE); 11111237651SArnd Bergmann omap_sram_base = __arm_ioremap_exec(omap_sram_start, omap_sram_size, 1); 11211237651SArnd Bergmann if (!omap_sram_base) { 11311237651SArnd Bergmann pr_err("SRAM: Could not map\n"); 11411237651SArnd Bergmann return; 11511237651SArnd Bergmann } 11611237651SArnd Bergmann 11711237651SArnd Bergmann omap_sram_ceil = omap_sram_base + omap_sram_size; 11811237651SArnd Bergmann 11911237651SArnd Bergmann /* 12011237651SArnd Bergmann * Looks like we need to preserve some bootloader code at the 12111237651SArnd Bergmann * beginning of SRAM for jumping to flash for reboot to work... 12211237651SArnd Bergmann */ 12311237651SArnd Bergmann memset_io(omap_sram_base + omap_sram_skip, 0, 12411237651SArnd Bergmann omap_sram_size - omap_sram_skip); 12511237651SArnd Bergmann 12611237651SArnd Bergmann base = (unsigned long)omap_sram_base; 12711237651SArnd Bergmann pages = PAGE_ALIGN(omap_sram_size) / PAGE_SIZE; 12811237651SArnd Bergmann 12911237651SArnd Bergmann set_memory_ro(base, pages); 13011237651SArnd Bergmann set_memory_x(base, pages); 131f205cfafSTony Lindgren } 132f205cfafSTony Lindgren 133f205cfafSTony Lindgren static void (*_omap_sram_reprogram_clock)(u32 dpllctl, u32 ckctl); 134f205cfafSTony Lindgren 135f205cfafSTony Lindgren void omap_sram_reprogram_clock(u32 dpllctl, u32 ckctl) 136f205cfafSTony Lindgren { 137f205cfafSTony Lindgren BUG_ON(!_omap_sram_reprogram_clock); 138f205cfafSTony Lindgren /* On 730, bit 13 must always be 1 */ 139f205cfafSTony Lindgren if (cpu_is_omap7xx()) 140f205cfafSTony Lindgren ckctl |= 0x2000; 141f205cfafSTony Lindgren _omap_sram_reprogram_clock(dpllctl, ckctl); 142f205cfafSTony Lindgren } 143f205cfafSTony Lindgren 14411237651SArnd Bergmann int __init omap1_sram_init(void) 145f205cfafSTony Lindgren { 146f205cfafSTony Lindgren omap_detect_and_map_sram(); 147f205cfafSTony Lindgren _omap_sram_reprogram_clock = 148f205cfafSTony Lindgren omap_sram_push(omap1_sram_reprogram_clock, 149f205cfafSTony Lindgren omap1_sram_reprogram_clock_sz); 150f205cfafSTony Lindgren 151f205cfafSTony Lindgren return 0; 152f205cfafSTony Lindgren } 153