Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/arm/plat-omap/sram.c |
| 3 | * |
| 4 | * OMAP SRAM detection and management |
| 5 | * |
| 6 | * Copyright (C) 2005 Nokia Corporation |
| 7 | * Written by Tony Lindgren <tony@atomide.com> |
| 8 | * |
R Sricharan | 05e152c | 2012-06-05 16:21:32 +0530 | [diff] [blame] | 9 | * Copyright (C) 2009-2012 Texas Instruments |
| 10 | * Added OMAP4/5 support - Santosh Shilimkar <santosh.shilimkar@ti.com> |
Santosh Shilimkar | 4416907 | 2009-05-28 14:16:04 -0700 | [diff] [blame] | 11 | * |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License version 2 as |
| 14 | * published by the Free Software Foundation. |
| 15 | */ |
Tony Lindgren | c2d43e3 | 2008-07-03 12:24:38 +0300 | [diff] [blame] | 16 | #undef DEBUG |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 17 | |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/init.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 21 | #include <linux/io.h> |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 22 | |
Tony Lindgren | bf027ca | 2012-10-29 13:54:06 -0700 | [diff] [blame] | 23 | #include <asm/fncpy.h> |
Tony Lindgren | 53d9cc7 | 2006-02-08 22:06:45 +0000 | [diff] [blame] | 24 | #include <asm/tlb.h> |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 25 | #include <asm/cacheflush.h> |
Tony Lindgren | eb85a35 | 2018-03-21 08:16:29 -0700 | [diff] [blame] | 26 | #include <asm/set_memory.h> |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 27 | |
Tony Lindgren | 670c104 | 2006-04-02 17:46:25 +0100 | [diff] [blame] | 28 | #include <asm/mach/map.h> |
| 29 | |
Paul Walmsley | 9fbe7c2 | 2012-12-28 02:09:15 -0700 | [diff] [blame] | 30 | #include <plat/sram.h> |
| 31 | |
Tony Lindgren | 670c104 | 2006-04-02 17:46:25 +0100 | [diff] [blame] | 32 | #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1))) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 33 | |
Tony Lindgren | a66cb34 | 2011-10-04 13:52:57 -0700 | [diff] [blame] | 34 | static void __iomem *omap_sram_base; |
Aaro Koskinen | b285673 | 2012-08-29 18:24:31 +0300 | [diff] [blame] | 35 | static unsigned long omap_sram_skip; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 36 | static unsigned long omap_sram_size; |
Tony Lindgren | a66cb34 | 2011-10-04 13:52:57 -0700 | [diff] [blame] | 37 | static void __iomem *omap_sram_ceil; |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 38 | |
Imre Deak | b7cc6d4 | 2007-03-06 03:16:36 -0800 | [diff] [blame] | 39 | /* |
Jean Pihet | b6338bd | 2011-02-02 16:38:06 +0100 | [diff] [blame] | 40 | * Memory allocator for SRAM: calculates the new ceiling address |
| 41 | * for pushing a function using the fncpy API. |
| 42 | * |
| 43 | * Note that fncpy requires the returned address to be aligned |
| 44 | * to an 8-byte boundary. |
| 45 | */ |
Tony Lindgren | eb85a35 | 2018-03-21 08:16:29 -0700 | [diff] [blame] | 46 | static void *omap_sram_push_address(unsigned long size) |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 47 | { |
Tony Lindgren | a66cb34 | 2011-10-04 13:52:57 -0700 | [diff] [blame] | 48 | unsigned long available, new_ceil = (unsigned long)omap_sram_ceil; |
| 49 | |
Aaro Koskinen | b285673 | 2012-08-29 18:24:31 +0300 | [diff] [blame] | 50 | available = omap_sram_ceil - (omap_sram_base + omap_sram_skip); |
Tony Lindgren | a66cb34 | 2011-10-04 13:52:57 -0700 | [diff] [blame] | 51 | |
| 52 | if (size > available) { |
Santosh Shilimkar | 26a510b | 2011-04-04 14:20:08 +0530 | [diff] [blame] | 53 | pr_err("Not enough space in SRAM\n"); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 54 | return NULL; |
| 55 | } |
Tony Lindgren | 670c104 | 2006-04-02 17:46:25 +0100 | [diff] [blame] | 56 | |
Tony Lindgren | a66cb34 | 2011-10-04 13:52:57 -0700 | [diff] [blame] | 57 | new_ceil -= size; |
| 58 | new_ceil = ROUND_DOWN(new_ceil, FNCPY_ALIGN); |
| 59 | omap_sram_ceil = IOMEM(new_ceil); |
Tony Lindgren | 92105bb | 2005-09-07 17:20:26 +0100 | [diff] [blame] | 60 | |
| 61 | return (void *)omap_sram_ceil; |
| 62 | } |
| 63 | |
Tony Lindgren | eb85a35 | 2018-03-21 08:16:29 -0700 | [diff] [blame] | 64 | void *omap_sram_push(void *funcp, unsigned long size) |
| 65 | { |
| 66 | void *sram; |
| 67 | unsigned long base; |
| 68 | int pages; |
| 69 | void *dst = NULL; |
| 70 | |
| 71 | sram = omap_sram_push_address(size); |
| 72 | if (!sram) |
| 73 | return NULL; |
| 74 | |
| 75 | base = (unsigned long)sram & PAGE_MASK; |
| 76 | pages = PAGE_ALIGN(size) / PAGE_SIZE; |
| 77 | |
| 78 | set_memory_rw(base, pages); |
| 79 | |
| 80 | dst = fncpy(sram, funcp, size); |
| 81 | |
| 82 | set_memory_ro(base, pages); |
| 83 | set_memory_x(base, pages); |
| 84 | |
| 85 | return dst; |
| 86 | } |
| 87 | |
Tony Lindgren | d8cfd6c | 2012-10-29 14:36:25 -0700 | [diff] [blame] | 88 | /* |
| 89 | * The SRAM context is lost during off-idle and stack |
| 90 | * needs to be reset. |
| 91 | */ |
| 92 | void omap_sram_reset(void) |
| 93 | { |
| 94 | omap_sram_ceil = omap_sram_base + omap_sram_size; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Note that we cannot use ioremap for SRAM, as clock init needs SRAM early. |
| 99 | */ |
| 100 | void __init omap_map_sram(unsigned long start, unsigned long size, |
| 101 | unsigned long skip, int cached) |
| 102 | { |
Tony Lindgren | eb85a35 | 2018-03-21 08:16:29 -0700 | [diff] [blame] | 103 | unsigned long base; |
| 104 | int pages; |
| 105 | |
Tony Lindgren | d8cfd6c | 2012-10-29 14:36:25 -0700 | [diff] [blame] | 106 | if (size == 0) |
| 107 | return; |
| 108 | |
| 109 | start = ROUND_DOWN(start, PAGE_SIZE); |
| 110 | omap_sram_size = size; |
| 111 | omap_sram_skip = skip; |
| 112 | omap_sram_base = __arm_ioremap_exec(start, size, cached); |
| 113 | if (!omap_sram_base) { |
| 114 | pr_err("SRAM: Could not map\n"); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | omap_sram_reset(); |
| 119 | |
| 120 | /* |
| 121 | * Looks like we need to preserve some bootloader code at the |
| 122 | * beginning of SRAM for jumping to flash for reboot to work... |
| 123 | */ |
| 124 | memset_io(omap_sram_base + omap_sram_skip, 0, |
| 125 | omap_sram_size - omap_sram_skip); |
Tony Lindgren | eb85a35 | 2018-03-21 08:16:29 -0700 | [diff] [blame] | 126 | |
| 127 | base = (unsigned long)omap_sram_base; |
| 128 | pages = PAGE_ALIGN(omap_sram_size) / PAGE_SIZE; |
| 129 | |
| 130 | set_memory_ro(base, pages); |
| 131 | set_memory_x(base, pages); |
Tony Lindgren | d8cfd6c | 2012-10-29 14:36:25 -0700 | [diff] [blame] | 132 | } |