sram-init.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <tony@atomide.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/io.h>
  12. #include <linux/set_memory.h>
  13. #include <asm/fncpy.h>
  14. #include <asm/tlb.h>
  15. #include <asm/cacheflush.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_rox(base, pages);
  61. return dst;
  62. }
  63. /*
  64. * The amount of SRAM depends on the core type.
  65. * Note that we cannot try to test for SRAM here because writes
  66. * to secure SRAM will hang the system. Also the SRAM is not
  67. * yet mapped at this point.
  68. * Note that we cannot use ioremap for SRAM, as clock init needs SRAM early.
  69. */
  70. static void __init omap_detect_and_map_sram(void)
  71. {
  72. unsigned long base;
  73. int pages;
  74. omap_sram_skip = SRAM_BOOTLOADER_SZ;
  75. omap_sram_start = OMAP1_SRAM_PA;
  76. if (cpu_is_omap15xx())
  77. omap_sram_size = 0x30000; /* 192K */
  78. else if (cpu_is_omap1610() || cpu_is_omap1611() ||
  79. cpu_is_omap1621() || cpu_is_omap1710())
  80. omap_sram_size = 0x4000; /* 16K */
  81. else {
  82. pr_err("Could not detect SRAM size\n");
  83. omap_sram_size = 0x4000;
  84. }
  85. omap_sram_start = ROUND_DOWN(omap_sram_start, PAGE_SIZE);
  86. omap_sram_base = __arm_ioremap_exec(omap_sram_start, omap_sram_size, 1);
  87. if (!omap_sram_base) {
  88. pr_err("SRAM: Could not map\n");
  89. return;
  90. }
  91. omap_sram_ceil = omap_sram_base + omap_sram_size;
  92. /*
  93. * Looks like we need to preserve some bootloader code at the
  94. * beginning of SRAM for jumping to flash for reboot to work...
  95. */
  96. memset_io(omap_sram_base + omap_sram_skip, 0,
  97. omap_sram_size - omap_sram_skip);
  98. base = (unsigned long)omap_sram_base;
  99. pages = PAGE_ALIGN(omap_sram_size) / PAGE_SIZE;
  100. set_memory_rox(base, pages);
  101. }
  102. static void (*_omap_sram_reprogram_clock)(u32 dpllctl, u32 ckctl);
  103. void omap_sram_reprogram_clock(u32 dpllctl, u32 ckctl)
  104. {
  105. BUG_ON(!_omap_sram_reprogram_clock);
  106. _omap_sram_reprogram_clock(dpllctl, ckctl);
  107. }
  108. int __init omap1_sram_init(void)
  109. {
  110. omap_detect_and_map_sram();
  111. _omap_sram_reprogram_clock =
  112. omap_sram_push(omap1_sram_reprogram_clock,
  113. omap1_sram_reprogram_clock_sz);
  114. return 0;
  115. }