sram.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/plat-omap/sram.c
  4. *
  5. * OMAP SRAM detection and management
  6. *
  7. * Copyright (C) 2005 Nokia Corporation
  8. * Written by Tony Lindgren <tony@atomide.com>
  9. *
  10. * Copyright (C) 2009-2012 Texas Instruments
  11. * Added OMAP4/5 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  12. */
  13. #undef DEBUG
  14. #include <linux/module.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/io.h>
  18. #include <asm/fncpy.h>
  19. #include <asm/tlb.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/set_memory.h>
  22. #include <asm/mach/map.h>
  23. #include <plat/sram.h>
  24. #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
  25. static void __iomem *omap_sram_base;
  26. static unsigned long omap_sram_skip;
  27. static unsigned long omap_sram_size;
  28. static void __iomem *omap_sram_ceil;
  29. /*
  30. * Memory allocator for SRAM: calculates the new ceiling address
  31. * for pushing a function using the fncpy API.
  32. *
  33. * Note that fncpy requires the returned address to be aligned
  34. * to an 8-byte boundary.
  35. */
  36. static void *omap_sram_push_address(unsigned long size)
  37. {
  38. unsigned long available, new_ceil = (unsigned long)omap_sram_ceil;
  39. available = omap_sram_ceil - (omap_sram_base + omap_sram_skip);
  40. if (size > available) {
  41. pr_err("Not enough space in SRAM\n");
  42. return NULL;
  43. }
  44. new_ceil -= size;
  45. new_ceil = ROUND_DOWN(new_ceil, FNCPY_ALIGN);
  46. omap_sram_ceil = IOMEM(new_ceil);
  47. return (void *)omap_sram_ceil;
  48. }
  49. void *omap_sram_push(void *funcp, unsigned long size)
  50. {
  51. void *sram;
  52. unsigned long base;
  53. int pages;
  54. void *dst = NULL;
  55. sram = omap_sram_push_address(size);
  56. if (!sram)
  57. return NULL;
  58. base = (unsigned long)sram & PAGE_MASK;
  59. pages = PAGE_ALIGN(size) / PAGE_SIZE;
  60. set_memory_rw(base, pages);
  61. dst = fncpy(sram, funcp, size);
  62. set_memory_ro(base, pages);
  63. set_memory_x(base, pages);
  64. return dst;
  65. }
  66. /*
  67. * The SRAM context is lost during off-idle and stack
  68. * needs to be reset.
  69. */
  70. void omap_sram_reset(void)
  71. {
  72. omap_sram_ceil = omap_sram_base + omap_sram_size;
  73. }
  74. /*
  75. * Note that we cannot use ioremap for SRAM, as clock init needs SRAM early.
  76. */
  77. void __init omap_map_sram(unsigned long start, unsigned long size,
  78. unsigned long skip, int cached)
  79. {
  80. unsigned long base;
  81. int pages;
  82. if (size == 0)
  83. return;
  84. start = ROUND_DOWN(start, PAGE_SIZE);
  85. omap_sram_size = size;
  86. omap_sram_skip = skip;
  87. omap_sram_base = __arm_ioremap_exec(start, size, cached);
  88. if (!omap_sram_base) {
  89. pr_err("SRAM: Could not map\n");
  90. return;
  91. }
  92. omap_sram_reset();
  93. /*
  94. * Looks like we need to preserve some bootloader code at the
  95. * beginning of SRAM for jumping to flash for reboot to work...
  96. */
  97. memset_io(omap_sram_base + omap_sram_skip, 0,
  98. omap_sram_size - omap_sram_skip);
  99. base = (unsigned long)omap_sram_base;
  100. pages = PAGE_ALIGN(omap_sram_size) / PAGE_SIZE;
  101. set_memory_ro(base, pages);
  102. set_memory_x(base, pages);
  103. }