sram.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/set_memory.h>
  25. #include <asm/mach/map.h>
  26. #include <plat/sram.h>
  27. #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
  28. static void __iomem *omap_sram_base;
  29. static unsigned long omap_sram_skip;
  30. static unsigned long omap_sram_size;
  31. static void __iomem *omap_sram_ceil;
  32. /*
  33. * Memory allocator for SRAM: calculates the new ceiling address
  34. * for pushing a function using the fncpy API.
  35. *
  36. * Note that fncpy requires the returned address to be aligned
  37. * to an 8-byte boundary.
  38. */
  39. static void *omap_sram_push_address(unsigned long size)
  40. {
  41. unsigned long available, new_ceil = (unsigned long)omap_sram_ceil;
  42. available = omap_sram_ceil - (omap_sram_base + omap_sram_skip);
  43. if (size > available) {
  44. pr_err("Not enough space in SRAM\n");
  45. return NULL;
  46. }
  47. new_ceil -= size;
  48. new_ceil = ROUND_DOWN(new_ceil, FNCPY_ALIGN);
  49. omap_sram_ceil = IOMEM(new_ceil);
  50. return (void *)omap_sram_ceil;
  51. }
  52. void *omap_sram_push(void *funcp, unsigned long size)
  53. {
  54. void *sram;
  55. unsigned long base;
  56. int pages;
  57. void *dst = NULL;
  58. sram = omap_sram_push_address(size);
  59. if (!sram)
  60. return NULL;
  61. base = (unsigned long)sram & PAGE_MASK;
  62. pages = PAGE_ALIGN(size) / PAGE_SIZE;
  63. set_memory_rw(base, pages);
  64. dst = fncpy(sram, funcp, size);
  65. set_memory_ro(base, pages);
  66. set_memory_x(base, pages);
  67. return dst;
  68. }
  69. /*
  70. * The SRAM context is lost during off-idle and stack
  71. * needs to be reset.
  72. */
  73. void omap_sram_reset(void)
  74. {
  75. omap_sram_ceil = omap_sram_base + omap_sram_size;
  76. }
  77. /*
  78. * Note that we cannot use ioremap for SRAM, as clock init needs SRAM early.
  79. */
  80. void __init omap_map_sram(unsigned long start, unsigned long size,
  81. unsigned long skip, int cached)
  82. {
  83. unsigned long base;
  84. int pages;
  85. if (size == 0)
  86. return;
  87. start = ROUND_DOWN(start, PAGE_SIZE);
  88. omap_sram_size = size;
  89. omap_sram_skip = skip;
  90. omap_sram_base = __arm_ioremap_exec(start, size, cached);
  91. if (!omap_sram_base) {
  92. pr_err("SRAM: Could not map\n");
  93. return;
  94. }
  95. omap_sram_reset();
  96. /*
  97. * Looks like we need to preserve some bootloader code at the
  98. * beginning of SRAM for jumping to flash for reboot to work...
  99. */
  100. memset_io(omap_sram_base + omap_sram_skip, 0,
  101. omap_sram_size - omap_sram_skip);
  102. base = (unsigned long)omap_sram_base;
  103. pages = PAGE_ALIGN(omap_sram_size) / PAGE_SIZE;
  104. set_memory_ro(base, pages);
  105. set_memory_x(base, pages);
  106. }