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> 17*11237651SArnd 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 26*11237651SArnd Bergmann #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1))) 27*11237651SArnd Bergmann 28*11237651SArnd Bergmann static void __iomem *omap_sram_base; 29*11237651SArnd Bergmann static unsigned long omap_sram_start; 30*11237651SArnd Bergmann static unsigned long omap_sram_skip; 31*11237651SArnd Bergmann static unsigned long omap_sram_size; 32*11237651SArnd Bergmann static void __iomem *omap_sram_ceil; 33*11237651SArnd Bergmann 34*11237651SArnd Bergmann /* 35*11237651SArnd Bergmann * Memory allocator for SRAM: calculates the new ceiling address 36*11237651SArnd Bergmann * for pushing a function using the fncpy API. 37*11237651SArnd Bergmann * 38*11237651SArnd Bergmann * Note that fncpy requires the returned address to be aligned 39*11237651SArnd Bergmann * to an 8-byte boundary. 40*11237651SArnd Bergmann */ 41*11237651SArnd Bergmann static void *omap_sram_push_address(unsigned long size) 42*11237651SArnd Bergmann { 43*11237651SArnd Bergmann unsigned long available, new_ceil = (unsigned long)omap_sram_ceil; 44*11237651SArnd Bergmann 45*11237651SArnd Bergmann available = omap_sram_ceil - (omap_sram_base + omap_sram_skip); 46*11237651SArnd Bergmann 47*11237651SArnd Bergmann if (size > available) { 48*11237651SArnd Bergmann pr_err("Not enough space in SRAM\n"); 49*11237651SArnd Bergmann return NULL; 50*11237651SArnd Bergmann } 51*11237651SArnd Bergmann 52*11237651SArnd Bergmann new_ceil -= size; 53*11237651SArnd Bergmann new_ceil = ROUND_DOWN(new_ceil, FNCPY_ALIGN); 54*11237651SArnd Bergmann omap_sram_ceil = IOMEM(new_ceil); 55*11237651SArnd Bergmann 56*11237651SArnd Bergmann return (void *)omap_sram_ceil; 57*11237651SArnd Bergmann } 58*11237651SArnd Bergmann 59*11237651SArnd Bergmann void *omap_sram_push(void *funcp, unsigned long size) 60*11237651SArnd Bergmann { 61*11237651SArnd Bergmann void *sram; 62*11237651SArnd Bergmann unsigned long base; 63*11237651SArnd Bergmann int pages; 64*11237651SArnd Bergmann void *dst = NULL; 65*11237651SArnd Bergmann 66*11237651SArnd Bergmann sram = omap_sram_push_address(size); 67*11237651SArnd Bergmann if (!sram) 68*11237651SArnd Bergmann return NULL; 69*11237651SArnd Bergmann 70*11237651SArnd Bergmann base = (unsigned long)sram & PAGE_MASK; 71*11237651SArnd Bergmann pages = PAGE_ALIGN(size) / PAGE_SIZE; 72*11237651SArnd Bergmann 73*11237651SArnd Bergmann set_memory_rw(base, pages); 74*11237651SArnd Bergmann 75*11237651SArnd Bergmann dst = fncpy(sram, funcp, size); 76*11237651SArnd Bergmann 77*11237651SArnd Bergmann set_memory_ro(base, pages); 78*11237651SArnd Bergmann set_memory_x(base, pages); 79*11237651SArnd Bergmann 80*11237651SArnd Bergmann return dst; 81*11237651SArnd 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. 88*11237651SArnd 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 { 92*11237651SArnd Bergmann unsigned long base; 93*11237651SArnd Bergmann int pages; 94*11237651SArnd Bergmann 95*11237651SArnd Bergmann omap_sram_skip = SRAM_BOOTLOADER_SZ; 96*11237651SArnd 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 110*11237651SArnd Bergmann omap_sram_start = ROUND_DOWN(omap_sram_start, PAGE_SIZE); 111*11237651SArnd Bergmann omap_sram_base = __arm_ioremap_exec(omap_sram_start, omap_sram_size, 1); 112*11237651SArnd Bergmann if (!omap_sram_base) { 113*11237651SArnd Bergmann pr_err("SRAM: Could not map\n"); 114*11237651SArnd Bergmann return; 115*11237651SArnd Bergmann } 116*11237651SArnd Bergmann 117*11237651SArnd Bergmann omap_sram_ceil = omap_sram_base + omap_sram_size; 118*11237651SArnd Bergmann 119*11237651SArnd Bergmann /* 120*11237651SArnd Bergmann * Looks like we need to preserve some bootloader code at the 121*11237651SArnd Bergmann * beginning of SRAM for jumping to flash for reboot to work... 122*11237651SArnd Bergmann */ 123*11237651SArnd Bergmann memset_io(omap_sram_base + omap_sram_skip, 0, 124*11237651SArnd Bergmann omap_sram_size - omap_sram_skip); 125*11237651SArnd Bergmann 126*11237651SArnd Bergmann base = (unsigned long)omap_sram_base; 127*11237651SArnd Bergmann pages = PAGE_ALIGN(omap_sram_size) / PAGE_SIZE; 128*11237651SArnd Bergmann 129*11237651SArnd Bergmann set_memory_ro(base, pages); 130*11237651SArnd 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 144*11237651SArnd 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