sram-init.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OMAP SRAM detection and management
  4. *
  5. * Copyright (C) 2005 Nokia Corporation
  6. * Written by Tony Lindgren <[email protected]>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/io.h>
  12. #include <asm/fncpy.h>
  13. #include <asm/tlb.h>
  14. #include <asm/cacheflush.h>
  15. #include <asm/set_memory.h>
  16. #include <asm/mach/map.h>
  17. #include "soc.h"
  18. #include "sram.h"
  19. #define OMAP1_SRAM_PA 0x20000000
  20. #define SRAM_BOOTLOADER_SZ 0x80
  21. #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
  22. static void __iomem *omap_sram_base;
  23. static unsigned long omap_sram_start;
  24. static unsigned long omap_sram_skip;
  25. static unsigned long omap_sram_size;
  26. static void __iomem *omap_sram_ceil;
  27. /*
  28. * Memory allocator for SRAM: calculates the new ceiling address
  29. * for pushing a function using the fncpy API.
  30. *
  31. * Note that fncpy requires the returned address to be aligned
  32. * to an 8-byte boundary.
  33. */
  34. static void *omap_sram_push_address(unsigned long size)
  35. {
  36. unsigned long available, new_ceil = (unsigned long)omap_sram_ceil;
  37. available = omap_sram_ceil - (omap_sram_base + omap_sram_skip);
  38. if (size > available) {
  39. pr_err("Not enough space in SRAM\n");
  40. return NULL;
  41. }
  42. new_ceil -= size;
  43. new_ceil = ROUND_DOWN(new_ceil, FNCPY_ALIGN);
  44. omap_sram_ceil = IOMEM(new_ceil);
  45. return (void __force *)omap_sram_ceil;
  46. }
  47. void *omap_sram_push(void *funcp, unsigned long size)
  48. {
  49. void *sram;
  50. unsigned long base;
  51. int pages;
  52. void *dst = NULL;
  53. sram = omap_sram_push_address(size);
  54. if (!sram)
  55. return NULL;
  56. base = (unsigned long)sram & PAGE_MASK;
  57. pages = PAGE_ALIGN(size) / PAGE_SIZE;
  58. set_memory_rw(base, pages);
  59. dst = fncpy(sram, funcp, size);
  60. set_memory_ro(base, pages);
  61. set_memory_x(base, pages);
  62. return dst;
  63. }
  64. /*
  65. * The amount of SRAM depends on the core type.
  66. * Note that we cannot try to test for SRAM here because writes
  67. * to secure SRAM will hang the system. Also the SRAM is not
  68. * yet mapped at this point.
  69. * Note that we cannot use ioremap for SRAM, as clock init needs SRAM early.
  70. */
  71. static void __init omap_detect_and_map_sram(void)
  72. {
  73. unsigned long base;
  74. int pages;
  75. omap_sram_skip = SRAM_BOOTLOADER_SZ;
  76. omap_sram_start = OMAP1_SRAM_PA;
  77. if (cpu_is_omap7xx())
  78. omap_sram_size = 0x32000; /* 200K */
  79. else if (cpu_is_omap15xx())
  80. omap_sram_size = 0x30000; /* 192K */
  81. else if (cpu_is_omap1610() || cpu_is_omap1611() ||
  82. cpu_is_omap1621() || cpu_is_omap1710())
  83. omap_sram_size = 0x4000; /* 16K */
  84. else {
  85. pr_err("Could not detect SRAM size\n");
  86. omap_sram_size = 0x4000;
  87. }
  88. omap_sram_start = ROUND_DOWN(omap_sram_start, PAGE_SIZE);
  89. omap_sram_base = __arm_ioremap_exec(omap_sram_start, omap_sram_size, 1);
  90. if (!omap_sram_base) {
  91. pr_err("SRAM: Could not map\n");
  92. return;
  93. }
  94. omap_sram_ceil = omap_sram_base + omap_sram_size;
  95. /*
  96. * Looks like we need to preserve some bootloader code at the
  97. * beginning of SRAM for jumping to flash for reboot to work...
  98. */
  99. memset_io(omap_sram_base + omap_sram_skip, 0,
  100. omap_sram_size - omap_sram_skip);
  101. base = (unsigned long)omap_sram_base;
  102. pages = PAGE_ALIGN(omap_sram_size) / PAGE_SIZE;
  103. set_memory_ro(base, pages);
  104. set_memory_x(base, pages);
  105. }
  106. static void (*_omap_sram_reprogram_clock)(u32 dpllctl, u32 ckctl);
  107. void omap_sram_reprogram_clock(u32 dpllctl, u32 ckctl)
  108. {
  109. BUG_ON(!_omap_sram_reprogram_clock);
  110. /* On 730, bit 13 must always be 1 */
  111. if (cpu_is_omap7xx())
  112. ckctl |= 0x2000;
  113. _omap_sram_reprogram_clock(dpllctl, ckctl);
  114. }
  115. int __init omap1_sram_init(void)
  116. {
  117. omap_detect_and_map_sram();
  118. _omap_sram_reprogram_clock =
  119. omap_sram_push(omap1_sram_reprogram_clock,
  120. omap1_sram_reprogram_clock_sz);
  121. return 0;
  122. }