sram.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. *
  9. * Copyright (C) 2009-2012 Texas Instruments
  10. * Added OMAP4/5 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  11. *
  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. */
  16. #undef DEBUG
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/io.h>
  21. #include <asm/fncpy.h>
  22. #include <asm/tlb.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/mach/map.h>
  25. #include "../mach-omap2/soc.h"
  26. #include "../mach-omap2/sram.h"
  27. /* XXX These "sideways" includes will disappear when sram.c becomes a driver */
  28. #include "../mach-omap2/iomap.h"
  29. #include "../mach-omap2/prm2xxx_3xxx.h"
  30. #include "../mach-omap2/sdrc.h"
  31. #define OMAP2_SRAM_PUB_PA (OMAP2_SRAM_PA + 0xf800)
  32. #define OMAP3_SRAM_PUB_PA (OMAP3_SRAM_PA + 0x8000)
  33. #ifdef CONFIG_OMAP4_ERRATA_I688
  34. #define OMAP4_SRAM_PUB_PA OMAP4_SRAM_PA
  35. #else
  36. #define OMAP4_SRAM_PUB_PA (OMAP4_SRAM_PA + 0x4000)
  37. #endif
  38. #define OMAP5_SRAM_PA 0x40300000
  39. #if defined(CONFIG_ARCH_OMAP2PLUS)
  40. #define SRAM_BOOTLOADER_SZ 0x00
  41. #else
  42. #define SRAM_BOOTLOADER_SZ 0x80
  43. #endif
  44. #define OMAP24XX_VA_REQINFOPERM0 OMAP2_L3_IO_ADDRESS(0x68005048)
  45. #define OMAP24XX_VA_READPERM0 OMAP2_L3_IO_ADDRESS(0x68005050)
  46. #define OMAP24XX_VA_WRITEPERM0 OMAP2_L3_IO_ADDRESS(0x68005058)
  47. #define OMAP34XX_VA_REQINFOPERM0 OMAP2_L3_IO_ADDRESS(0x68012848)
  48. #define OMAP34XX_VA_READPERM0 OMAP2_L3_IO_ADDRESS(0x68012850)
  49. #define OMAP34XX_VA_WRITEPERM0 OMAP2_L3_IO_ADDRESS(0x68012858)
  50. #define OMAP34XX_VA_ADDR_MATCH2 OMAP2_L3_IO_ADDRESS(0x68012880)
  51. #define OMAP34XX_VA_SMS_RG_ATT0 OMAP2_L3_IO_ADDRESS(0x6C000048)
  52. #define GP_DEVICE 0x300
  53. #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
  54. static unsigned long omap_sram_start;
  55. static void __iomem *omap_sram_base;
  56. static unsigned long omap_sram_skip;
  57. static unsigned long omap_sram_size;
  58. static void __iomem *omap_sram_ceil;
  59. /*
  60. * Depending on the target RAMFS firewall setup, the public usable amount of
  61. * SRAM varies. The default accessible size for all device types is 2k. A GP
  62. * device allows ARM11 but not other initiators for full size. This
  63. * functionality seems ok until some nice security API happens.
  64. */
  65. static int is_sram_locked(void)
  66. {
  67. if (OMAP2_DEVICE_TYPE_GP == omap_type()) {
  68. /* RAMFW: R/W access to all initiators for all qualifier sets */
  69. if (cpu_is_omap242x()) {
  70. __raw_writel(0xFF, OMAP24XX_VA_REQINFOPERM0); /* all q-vects */
  71. __raw_writel(0xCFDE, OMAP24XX_VA_READPERM0); /* all i-read */
  72. __raw_writel(0xCFDE, OMAP24XX_VA_WRITEPERM0); /* all i-write */
  73. }
  74. if (cpu_is_omap34xx()) {
  75. __raw_writel(0xFFFF, OMAP34XX_VA_REQINFOPERM0); /* all q-vects */
  76. __raw_writel(0xFFFF, OMAP34XX_VA_READPERM0); /* all i-read */
  77. __raw_writel(0xFFFF, OMAP34XX_VA_WRITEPERM0); /* all i-write */
  78. __raw_writel(0x0, OMAP34XX_VA_ADDR_MATCH2);
  79. __raw_writel(0xFFFFFFFF, OMAP34XX_VA_SMS_RG_ATT0);
  80. }
  81. return 0;
  82. } else
  83. return 1; /* assume locked with no PPA or security driver */
  84. }
  85. /*
  86. * The amount of SRAM depends on the core type.
  87. * Note that we cannot try to test for SRAM here because writes
  88. * to secure SRAM will hang the system. Also the SRAM is not
  89. * yet mapped at this point.
  90. */
  91. static void __init omap_detect_sram(void)
  92. {
  93. omap_sram_skip = SRAM_BOOTLOADER_SZ;
  94. if (cpu_class_is_omap2()) {
  95. if (is_sram_locked()) {
  96. if (cpu_is_omap34xx()) {
  97. omap_sram_start = OMAP3_SRAM_PUB_PA;
  98. if ((omap_type() == OMAP2_DEVICE_TYPE_EMU) ||
  99. (omap_type() == OMAP2_DEVICE_TYPE_SEC)) {
  100. omap_sram_size = 0x7000; /* 28K */
  101. omap_sram_skip += SZ_16K;
  102. } else {
  103. omap_sram_size = 0x8000; /* 32K */
  104. }
  105. } else if (cpu_is_omap44xx()) {
  106. omap_sram_start = OMAP4_SRAM_PUB_PA;
  107. omap_sram_size = 0xa000; /* 40K */
  108. } else if (soc_is_omap54xx()) {
  109. omap_sram_start = OMAP5_SRAM_PA;
  110. omap_sram_size = SZ_128K; /* 128KB */
  111. } else {
  112. omap_sram_start = OMAP2_SRAM_PUB_PA;
  113. omap_sram_size = 0x800; /* 2K */
  114. }
  115. } else {
  116. if (soc_is_am33xx()) {
  117. omap_sram_start = AM33XX_SRAM_PA;
  118. omap_sram_size = 0x10000; /* 64K */
  119. } else if (cpu_is_omap34xx()) {
  120. omap_sram_start = OMAP3_SRAM_PA;
  121. omap_sram_size = 0x10000; /* 64K */
  122. } else if (cpu_is_omap44xx()) {
  123. omap_sram_start = OMAP4_SRAM_PA;
  124. omap_sram_size = 0xe000; /* 56K */
  125. } else if (soc_is_omap54xx()) {
  126. omap_sram_start = OMAP5_SRAM_PA;
  127. omap_sram_size = SZ_128K; /* 128KB */
  128. } else {
  129. omap_sram_start = OMAP2_SRAM_PA;
  130. if (cpu_is_omap242x())
  131. omap_sram_size = 0xa0000; /* 640K */
  132. else if (cpu_is_omap243x())
  133. omap_sram_size = 0x10000; /* 64K */
  134. }
  135. }
  136. }
  137. }
  138. /*
  139. * Note that we cannot use ioremap for SRAM, as clock init needs SRAM early.
  140. */
  141. static void __init omap_fix_and_map_sram(void)
  142. {
  143. int cached = 1;
  144. #ifdef CONFIG_OMAP4_ERRATA_I688
  145. if (cpu_is_omap44xx()) {
  146. omap_sram_start += PAGE_SIZE;
  147. omap_sram_size -= SZ_16K;
  148. }
  149. #endif
  150. if (cpu_is_omap34xx()) {
  151. /*
  152. * SRAM must be marked as non-cached on OMAP3 since the
  153. * CORE DPLL M2 divider change code (in SRAM) runs with the
  154. * SDRAM controller disabled, and if it is marked cached,
  155. * the ARM may attempt to write cache lines back to SDRAM
  156. * which will cause the system to hang.
  157. */
  158. cached = 0;
  159. }
  160. omap_map_sram(omap_sram_start, omap_sram_size,
  161. omap_sram_skip, cached);
  162. }
  163. /*
  164. * Memory allocator for SRAM: calculates the new ceiling address
  165. * for pushing a function using the fncpy API.
  166. *
  167. * Note that fncpy requires the returned address to be aligned
  168. * to an 8-byte boundary.
  169. */
  170. void *omap_sram_push_address(unsigned long size)
  171. {
  172. unsigned long available, new_ceil = (unsigned long)omap_sram_ceil;
  173. available = omap_sram_ceil - (omap_sram_base + omap_sram_skip);
  174. if (size > available) {
  175. pr_err("Not enough space in SRAM\n");
  176. return NULL;
  177. }
  178. new_ceil -= size;
  179. new_ceil = ROUND_DOWN(new_ceil, FNCPY_ALIGN);
  180. omap_sram_ceil = IOMEM(new_ceil);
  181. return (void *)omap_sram_ceil;
  182. }
  183. /*
  184. * The SRAM context is lost during off-idle and stack
  185. * needs to be reset.
  186. */
  187. void omap_sram_reset(void)
  188. {
  189. omap_sram_ceil = omap_sram_base + omap_sram_size;
  190. }
  191. /*
  192. * Note that we cannot use ioremap for SRAM, as clock init needs SRAM early.
  193. */
  194. void __init omap_map_sram(unsigned long start, unsigned long size,
  195. unsigned long skip, int cached)
  196. {
  197. if (size == 0)
  198. return;
  199. start = ROUND_DOWN(start, PAGE_SIZE);
  200. omap_sram_size = size;
  201. omap_sram_skip = skip;
  202. omap_sram_base = __arm_ioremap_exec(start, size, cached);
  203. if (!omap_sram_base) {
  204. pr_err("SRAM: Could not map\n");
  205. return;
  206. }
  207. omap_sram_reset();
  208. /*
  209. * Looks like we need to preserve some bootloader code at the
  210. * beginning of SRAM for jumping to flash for reboot to work...
  211. */
  212. memset_io(omap_sram_base + omap_sram_skip, 0,
  213. omap_sram_size - omap_sram_skip);
  214. }
  215. #if defined(CONFIG_ARCH_OMAP2)
  216. static void (*_omap2_sram_ddr_init)(u32 *slow_dll_ctrl, u32 fast_dll_ctrl,
  217. u32 base_cs, u32 force_unlock);
  218. void omap2_sram_ddr_init(u32 *slow_dll_ctrl, u32 fast_dll_ctrl,
  219. u32 base_cs, u32 force_unlock)
  220. {
  221. BUG_ON(!_omap2_sram_ddr_init);
  222. _omap2_sram_ddr_init(slow_dll_ctrl, fast_dll_ctrl,
  223. base_cs, force_unlock);
  224. }
  225. static void (*_omap2_sram_reprogram_sdrc)(u32 perf_level, u32 dll_val,
  226. u32 mem_type);
  227. void omap2_sram_reprogram_sdrc(u32 perf_level, u32 dll_val, u32 mem_type)
  228. {
  229. BUG_ON(!_omap2_sram_reprogram_sdrc);
  230. _omap2_sram_reprogram_sdrc(perf_level, dll_val, mem_type);
  231. }
  232. static u32 (*_omap2_set_prcm)(u32 dpll_ctrl_val, u32 sdrc_rfr_val, int bypass);
  233. u32 omap2_set_prcm(u32 dpll_ctrl_val, u32 sdrc_rfr_val, int bypass)
  234. {
  235. BUG_ON(!_omap2_set_prcm);
  236. return _omap2_set_prcm(dpll_ctrl_val, sdrc_rfr_val, bypass);
  237. }
  238. #endif
  239. #ifdef CONFIG_SOC_OMAP2420
  240. static int __init omap242x_sram_init(void)
  241. {
  242. _omap2_sram_ddr_init = omap_sram_push(omap242x_sram_ddr_init,
  243. omap242x_sram_ddr_init_sz);
  244. _omap2_sram_reprogram_sdrc = omap_sram_push(omap242x_sram_reprogram_sdrc,
  245. omap242x_sram_reprogram_sdrc_sz);
  246. _omap2_set_prcm = omap_sram_push(omap242x_sram_set_prcm,
  247. omap242x_sram_set_prcm_sz);
  248. return 0;
  249. }
  250. #else
  251. static inline int omap242x_sram_init(void)
  252. {
  253. return 0;
  254. }
  255. #endif
  256. #ifdef CONFIG_SOC_OMAP2430
  257. static int __init omap243x_sram_init(void)
  258. {
  259. _omap2_sram_ddr_init = omap_sram_push(omap243x_sram_ddr_init,
  260. omap243x_sram_ddr_init_sz);
  261. _omap2_sram_reprogram_sdrc = omap_sram_push(omap243x_sram_reprogram_sdrc,
  262. omap243x_sram_reprogram_sdrc_sz);
  263. _omap2_set_prcm = omap_sram_push(omap243x_sram_set_prcm,
  264. omap243x_sram_set_prcm_sz);
  265. return 0;
  266. }
  267. #else
  268. static inline int omap243x_sram_init(void)
  269. {
  270. return 0;
  271. }
  272. #endif
  273. #ifdef CONFIG_ARCH_OMAP3
  274. static u32 (*_omap3_sram_configure_core_dpll)(
  275. u32 m2, u32 unlock_dll, u32 f, u32 inc,
  276. u32 sdrc_rfr_ctrl_0, u32 sdrc_actim_ctrl_a_0,
  277. u32 sdrc_actim_ctrl_b_0, u32 sdrc_mr_0,
  278. u32 sdrc_rfr_ctrl_1, u32 sdrc_actim_ctrl_a_1,
  279. u32 sdrc_actim_ctrl_b_1, u32 sdrc_mr_1);
  280. u32 omap3_configure_core_dpll(u32 m2, u32 unlock_dll, u32 f, u32 inc,
  281. u32 sdrc_rfr_ctrl_0, u32 sdrc_actim_ctrl_a_0,
  282. u32 sdrc_actim_ctrl_b_0, u32 sdrc_mr_0,
  283. u32 sdrc_rfr_ctrl_1, u32 sdrc_actim_ctrl_a_1,
  284. u32 sdrc_actim_ctrl_b_1, u32 sdrc_mr_1)
  285. {
  286. BUG_ON(!_omap3_sram_configure_core_dpll);
  287. return _omap3_sram_configure_core_dpll(
  288. m2, unlock_dll, f, inc,
  289. sdrc_rfr_ctrl_0, sdrc_actim_ctrl_a_0,
  290. sdrc_actim_ctrl_b_0, sdrc_mr_0,
  291. sdrc_rfr_ctrl_1, sdrc_actim_ctrl_a_1,
  292. sdrc_actim_ctrl_b_1, sdrc_mr_1);
  293. }
  294. void omap3_sram_restore_context(void)
  295. {
  296. omap_sram_reset();
  297. _omap3_sram_configure_core_dpll =
  298. omap_sram_push(omap3_sram_configure_core_dpll,
  299. omap3_sram_configure_core_dpll_sz);
  300. omap_push_sram_idle();
  301. }
  302. static inline int omap34xx_sram_init(void)
  303. {
  304. omap3_sram_restore_context();
  305. return 0;
  306. }
  307. #else
  308. static inline int omap34xx_sram_init(void)
  309. {
  310. return 0;
  311. }
  312. #endif /* CONFIG_ARCH_OMAP3 */
  313. static inline int am33xx_sram_init(void)
  314. {
  315. return 0;
  316. }
  317. #ifdef CONFIG_ARCH_OMAP2PLUS
  318. int __init omap_sram_init(void)
  319. {
  320. omap_detect_sram();
  321. omap_fix_and_map_sram();
  322. if (cpu_is_omap242x())
  323. omap242x_sram_init();
  324. else if (cpu_is_omap2430())
  325. omap243x_sram_init();
  326. else if (soc_is_am33xx())
  327. am33xx_sram_init();
  328. else if (cpu_is_omap34xx())
  329. omap34xx_sram_init();
  330. return 0;
  331. }
  332. #endif