2 * linux/arch/arm/plat-omap/sram.c
4 * OMAP SRAM detection and management
6 * Copyright (C) 2005 Nokia Corporation
7 * Written by Tony Lindgren <tony@atomide.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
21 #include <asm/cacheflush.h>
23 #include <asm/mach/map.h>
25 #include <asm/arch/sram.h>
26 #include <asm/arch/board.h>
28 #include <asm/arch/control.h>
30 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
31 # include "../mach-omap2/prm.h"
32 # include "../mach-omap2/cm.h"
33 # include "../mach-omap2/sdrc.h"
36 #define OMAP1_SRAM_PA 0x20000000
37 #define OMAP1_SRAM_VA VMALLOC_END
38 #define OMAP2_SRAM_PA 0x40200000
39 #define OMAP2_SRAM_PUB_PA 0x4020f800
40 #define OMAP2_SRAM_VA VMALLOC_END
41 #define OMAP2_SRAM_PUB_VA (VMALLOC_END + 0x800)
43 #if defined(CONFIG_ARCH_OMAP24XX) || defined(CONFIG_ARCH_OMAP34XX)
44 #define SRAM_BOOTLOADER_SZ 0x00
46 #define SRAM_BOOTLOADER_SZ 0x80
49 #define VA_REQINFOPERM0 IO_ADDRESS(0x68005048)
50 #define VA_READPERM0 IO_ADDRESS(0x68005050)
51 #define VA_WRITEPERM0 IO_ADDRESS(0x68005058)
52 #define GP_DEVICE 0x300
54 #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
56 static unsigned long omap_sram_start;
57 static unsigned long omap_sram_base;
58 static unsigned long omap_sram_size;
59 static unsigned long omap_sram_ceil;
61 extern unsigned long omapfb_reserve_sram(unsigned long sram_pstart,
62 unsigned long sram_vstart,
63 unsigned long sram_size,
64 unsigned long pstart_avail,
65 unsigned long size_avail);
67 /* Global symbols in sram-fn.S to be patched with omap_sram_patch_va() */
68 extern void *omap2_sdi_cm_clksel2_pll;
69 extern void *omap2_sdi_sdrc_dlla_ctrl;
70 extern void *omap2_sdi_prcm_voltctrl;
71 extern void *omap2_sdi_timer_32ksynct_cr;
72 extern void *omap2_srs_cm_clksel2_pll;
73 extern void *omap2_srs_sdrc_dlla_ctrl;
74 extern void *omap2_srs_sdrc_rfr_ctrl;
75 extern void *omap2_srs_prcm_voltctrl;
76 extern void *omap2_srs_timer_32ksynct;
77 extern void *omap2_ssp_set_config;
78 extern void *omap2_ssp_pll_ctl;
79 extern void *omap2_ssp_pll_stat;
80 extern void *omap2_ssp_pll_div;
81 extern void *omap2_ssp_sdrc_rfr;
82 extern void *omap2_ssp_dlla_ctrl;
86 * Depending on the target RAMFS firewall setup, the public usable amount of
87 * SRAM varies. The default accessible size for all device types is 2k. A GP
88 * device allows ARM11 but not other initiators for full size. This
89 * functionality seems ok until some nice security API happens.
91 static int is_sram_locked(void)
95 if (cpu_is_omap242x())
96 type = system_rev & OMAP2_DEVICETYPE_MASK;
98 if (type == GP_DEVICE) {
99 /* RAMFW: R/W access to all initiators for all qualifier sets */
100 if (cpu_is_omap242x()) {
101 __raw_writel(0xFF, VA_REQINFOPERM0); /* all q-vects */
102 __raw_writel(0xCFDE, VA_READPERM0); /* all i-read */
103 __raw_writel(0xCFDE, VA_WRITEPERM0); /* all i-write */
107 return 1; /* assume locked with no PPA or security driver */
111 * The amount of SRAM depends on the core type.
112 * Note that we cannot try to test for SRAM here because writes
113 * to secure SRAM will hang the system. Also the SRAM is not
114 * yet mapped at this point.
116 void __init omap_detect_sram(void)
118 unsigned long reserved;
120 if (cpu_class_is_omap2()) {
121 if (is_sram_locked()) {
122 omap_sram_base = OMAP2_SRAM_PUB_VA;
123 omap_sram_start = OMAP2_SRAM_PUB_PA;
124 omap_sram_size = 0x800; /* 2K */
126 omap_sram_base = OMAP2_SRAM_VA;
127 omap_sram_start = OMAP2_SRAM_PA;
128 if (cpu_is_omap242x())
129 omap_sram_size = 0xa0000; /* 640K */
130 else if (cpu_is_omap243x() || cpu_is_omap34xx())
131 omap_sram_size = 0x10000; /* 64K */
134 omap_sram_base = OMAP1_SRAM_VA;
135 omap_sram_start = OMAP1_SRAM_PA;
137 if (cpu_is_omap730())
138 omap_sram_size = 0x32000; /* 200K */
139 else if (cpu_is_omap15xx())
140 omap_sram_size = 0x30000; /* 192K */
141 else if (cpu_is_omap1610() || cpu_is_omap1621() ||
143 omap_sram_size = 0x4000; /* 16K */
144 else if (cpu_is_omap1611())
145 omap_sram_size = 0x3e800; /* 250K */
147 printk(KERN_ERR "Could not detect SRAM size\n");
148 omap_sram_size = 0x4000;
151 reserved = omapfb_reserve_sram(omap_sram_start, omap_sram_base,
153 omap_sram_start + SRAM_BOOTLOADER_SZ,
154 omap_sram_size - SRAM_BOOTLOADER_SZ);
155 omap_sram_size -= reserved;
156 omap_sram_ceil = omap_sram_base + omap_sram_size;
159 static struct map_desc omap_sram_io_desc[] __initdata = {
160 { /* .length gets filled in at runtime */
161 .virtual = OMAP1_SRAM_VA,
162 .pfn = __phys_to_pfn(OMAP1_SRAM_PA),
168 * Note that we cannot use ioremap for SRAM, as clock init needs SRAM early.
170 void __init omap_map_sram(void)
174 if (omap_sram_size == 0)
177 if (cpu_class_is_omap2()) {
178 omap_sram_io_desc[0].virtual = OMAP2_SRAM_VA;
180 base = OMAP2_SRAM_PA;
181 base = ROUND_DOWN(base, PAGE_SIZE);
182 omap_sram_io_desc[0].pfn = __phys_to_pfn(base);
185 omap_sram_io_desc[0].length = 1024 * 1024; /* Use section desc */
186 iotable_init(omap_sram_io_desc, ARRAY_SIZE(omap_sram_io_desc));
188 printk(KERN_INFO "SRAM: Mapped pa 0x%08lx to va 0x%08lx size: 0x%lx\n",
189 __pfn_to_phys(omap_sram_io_desc[0].pfn),
190 omap_sram_io_desc[0].virtual,
191 omap_sram_io_desc[0].length);
194 * Normally devicemaps_init() would flush caches and tlb after
195 * mdesc->map_io(), but since we're called from map_io(), we
198 local_flush_tlb_all();
202 * Looks like we need to preserve some bootloader code at the
203 * beginning of SRAM for jumping to flash for reboot to work...
205 memset((void *)omap_sram_base + SRAM_BOOTLOADER_SZ, 0,
206 omap_sram_size - SRAM_BOOTLOADER_SZ);
209 void * omap_sram_push(void * start, unsigned long size)
211 if (size > (omap_sram_ceil - (omap_sram_base + SRAM_BOOTLOADER_SZ))) {
212 printk(KERN_ERR "Not enough space in SRAM\n");
216 omap_sram_ceil -= size;
217 omap_sram_ceil = ROUND_DOWN(omap_sram_ceil, sizeof(void *));
218 memcpy((void *)omap_sram_ceil, start, size);
219 flush_icache_range((unsigned long)start, (unsigned long)(start + size));
221 return (void *)omap_sram_ceil;
225 * omap_sram_patch_va - patch a virtual address into SRAM code
226 * @srcfn: original start address (in DRAM) of function to patch
227 * @srcd: original address (in DRAM) of location to patch
228 * @sramfn: start address (in SRAM) of function to patch
229 * @d: virtual address to insert
231 * Replace a location in SRAM containing a magic number
232 * (SRAM_VA_MAGIC) with a caller-specified virtual address. Used to
233 * dynamically patch SRAM code at runtime for multiboot, since some
234 * register addresses change depending on the OMAP chip in use.
235 * Returns 1 upon success, 0 upon failure.
237 int omap_sram_patch_va(void *srcfn, void *srcd, void *sramfn, void __iomem *d)
239 unsigned long sram_addr;
242 offs = (unsigned long)srcd - (unsigned long)srcfn;
243 sram_addr = (unsigned long)sramfn + offs;
245 #ifdef CONFIG_OMAP_DEBUG_SRAM_PATCH
247 printk(KERN_ERR "sram: patch address 0x%0lx < function start "
248 "address 0x%0lx\n", (unsigned long)srcd,
249 (unsigned long)srcfn);
255 * REVISIT: We should probably pass in the function's size also,
256 * so we can verify that the address to patch exists within
259 if (sram_addr > omap_sram_base + omap_sram_size ||
260 sram_addr < omap_sram_base + SRAM_BOOTLOADER_SZ) {
261 printk(KERN_ERR "sram: invalid patch address 0x%0lx\n",
267 if (*(typeof(SRAM_VA_MAGIC) *)sram_addr != SRAM_VA_MAGIC) {
268 printk(KERN_ERR "sram: will not patch address 0x%0lx: "
269 "no magic\n", sram_addr);
273 #endif /* CONFIG_OMAP_DEBUG_SRAM_PATCH */
275 pr_debug("sram: patching 0x%0lx with 0x%0lx\n", sram_addr,
278 *(unsigned long *)sram_addr = (unsigned long)d;
284 static void omap_sram_error(void)
286 panic("Uninitialized SRAM function\n");
289 #ifdef CONFIG_ARCH_OMAP1
291 static void (*_omap_sram_reprogram_clock)(u32 dpllctl, u32 ckctl);
293 void omap_sram_reprogram_clock(u32 dpllctl, u32 ckctl)
295 if (!_omap_sram_reprogram_clock)
298 return _omap_sram_reprogram_clock(dpllctl, ckctl);
301 int __init omap1_sram_init(void)
303 _omap_sram_reprogram_clock = omap_sram_push(sram_reprogram_clock,
304 sram_reprogram_clock_sz);
310 #define omap1_sram_init() do {} while (0)
313 #if defined(CONFIG_ARCH_OMAP2) || defined(CONFIG_ARCH_OMAP3)
315 static void (*_omap2_sram_ddr_init)(u32 *slow_dll_ctrl, u32 fast_dll_ctrl,
316 u32 base_cs, u32 force_unlock);
318 void omap2_sram_ddr_init(u32 *slow_dll_ctrl, u32 fast_dll_ctrl,
319 u32 base_cs, u32 force_unlock)
321 if (!_omap2_sram_ddr_init)
324 return _omap2_sram_ddr_init(slow_dll_ctrl, fast_dll_ctrl,
325 base_cs, force_unlock);
328 static void (*_omap2_sram_reprogram_sdrc)(u32 perf_level, u32 dll_val,
331 void omap2_sram_reprogram_sdrc(u32 perf_level, u32 dll_val, u32 mem_type)
333 if (!_omap2_sram_reprogram_sdrc)
336 return _omap2_sram_reprogram_sdrc(perf_level, dll_val, mem_type);
339 static u32 (*_omap2_set_prcm)(u32 dpll_ctrl_val, u32 sdrc_rfr_val, int bypass);
341 u32 omap2_set_prcm(u32 dpll_ctrl_val, u32 sdrc_rfr_val, int bypass)
343 if (!_omap2_set_prcm)
346 return _omap2_set_prcm(dpll_ctrl_val, sdrc_rfr_val, bypass);
349 int __init omap2_sram_init(void)
351 _omap2_sram_ddr_init = omap_sram_push(sram_ddr_init, sram_ddr_init_sz);
353 /* Patch in the correct register addresses for multiboot */
354 omap_sram_patch_va(sram_ddr_init, &omap2_sdi_cm_clksel2_pll,
355 _omap2_sram_ddr_init,
356 OMAP_CM_REGADDR(PLL_MOD, CM_CLKSEL2));
357 omap_sram_patch_va(sram_ddr_init, &omap2_sdi_sdrc_dlla_ctrl,
358 _omap2_sram_ddr_init,
359 OMAP_SDRC_REGADDR(SDRC_DLLA_CTRL));
360 omap_sram_patch_va(sram_ddr_init, &omap2_sdi_prcm_voltctrl,
361 _omap2_sram_ddr_init, OMAP24XX_PRCM_VOLTCTRL);
362 omap_sram_patch_va(sram_ddr_init, &omap2_sdi_timer_32ksynct_cr,
363 _omap2_sram_ddr_init,
364 (void __iomem *)IO_ADDRESS(OMAP2_32KSYNCT_BASE + 0x010));
366 _omap2_sram_reprogram_sdrc = omap_sram_push(sram_reprogram_sdrc,
367 sram_reprogram_sdrc_sz);
369 omap_sram_patch_va(sram_reprogram_sdrc, &omap2_srs_cm_clksel2_pll,
370 _omap2_sram_reprogram_sdrc,
371 OMAP_CM_REGADDR(PLL_MOD, CM_CLKSEL2));
372 omap_sram_patch_va(sram_reprogram_sdrc, &omap2_srs_sdrc_dlla_ctrl,
373 _omap2_sram_reprogram_sdrc,
374 OMAP_SDRC_REGADDR(SDRC_DLLA_CTRL));
375 omap_sram_patch_va(sram_reprogram_sdrc, &omap2_srs_sdrc_rfr_ctrl,
376 _omap2_sram_reprogram_sdrc,
377 OMAP_SDRC_REGADDR(SDRC_RFR_CTRL_0));
378 omap_sram_patch_va(sram_reprogram_sdrc, &omap2_srs_prcm_voltctrl,
379 _omap2_sram_reprogram_sdrc,
380 OMAP24XX_PRCM_VOLTCTRL);
381 omap_sram_patch_va(sram_reprogram_sdrc, &omap2_srs_timer_32ksynct,
382 _omap2_sram_reprogram_sdrc,
383 (void __iomem *)IO_ADDRESS(OMAP2_32KSYNCT_BASE + 0x010));
385 _omap2_set_prcm = omap_sram_push(sram_set_prcm, sram_set_prcm_sz);
387 /* REVISIT: prefix all these symbols with omap2_sram_ */
388 omap_sram_patch_va(sram_set_prcm, &omap2_ssp_set_config,
390 OMAP24XX_PRCM_CLKCFG_CTRL);
391 omap_sram_patch_va(sram_set_prcm, &omap2_ssp_pll_ctl,
393 OMAP_CM_REGADDR(PLL_MOD, CM_CLKEN));
394 omap_sram_patch_va(sram_set_prcm, &omap2_ssp_pll_stat,
396 OMAP_CM_REGADDR(PLL_MOD, CM_IDLEST));
397 omap_sram_patch_va(sram_set_prcm, &omap2_ssp_pll_div,
399 OMAP_CM_REGADDR(PLL_MOD, CM_CLKSEL1));
400 omap_sram_patch_va(sram_set_prcm, &omap2_ssp_sdrc_rfr,
402 OMAP_SDRC_REGADDR(SDRC_RFR_CTRL_0));
403 omap_sram_patch_va(sram_set_prcm, &omap2_ssp_dlla_ctrl,
405 OMAP_SDRC_REGADDR(SDRC_DLLA_CTRL));
410 #define omap2_sram_init() do {} while (0)
413 int __init omap_sram_init(void)
418 if (!(cpu_class_is_omap2()))