cache.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <kernel/thread.h>
  24. #include <platform/mt_typedefs.h>
  25. #include <arch/arm/cache.h>
  26. #include <platform/mt_reg_base.h>
  27. volatile struct pl310_regs *PL310_L2CC = (struct pl310_regs *)0xC000E000;
  28. void dsb(void);
  29. static unsigned int way_mask;
  30. static void l2_cache_sync(void)
  31. {
  32. enter_critical_section();
  33. PL310_L2CC->CacheSync = 0;
  34. exit_critical_section();
  35. }
  36. static void l2_inv_all(void)
  37. {
  38. /* invalidate all ways */
  39. enter_critical_section();
  40. PL310_L2CC->InvalidateByWay = way_mask;
  41. while (PL310_L2CC->InvalidateByWay & way_mask);
  42. PL310_L2CC->CacheSync = 0;
  43. exit_critical_section();
  44. }
  45. void l2_clean_range(unsigned long start, unsigned long len)
  46. {
  47. unsigned long end = start + len;
  48. CACHE_ALLIGN(start);
  49. enter_critical_section();
  50. while (start < end) {
  51. PL310_L2CC->CleanLineByPA = start;
  52. start += CACHE_LINE_SIZE;
  53. }
  54. PL310_L2CC->CacheSync = 0;
  55. exit_critical_section();
  56. }
  57. void l2_flush_range(unsigned long start, unsigned long len)
  58. {
  59. unsigned long end = start + len;
  60. CACHE_ALLIGN(start);
  61. enter_critical_section();
  62. PL310_L2CC->DebugControl = 0x3;
  63. while (start < end) {
  64. #if MACH_TYPE == 6575
  65. PL310_L2CC->CleanLineByPA = start;
  66. PL310_L2CC->InvalidateLineByPA = start;
  67. #else
  68. PL310_L2CC->FlushLineByPA = start;
  69. #endif
  70. start += CACHE_LINE_SIZE;
  71. }
  72. PL310_L2CC->DebugControl = 0x0;
  73. PL310_L2CC->CacheSync = 0;
  74. exit_critical_section();
  75. }
  76. void l2_disable(void)
  77. {
  78. // flush all
  79. PL310_L2CC->DebugControl = 0x3;
  80. PL310_L2CC->FlushByWay = way_mask;
  81. while (PL310_L2CC->FlushByWay & way_mask);
  82. PL310_L2CC->CacheSync = 0;
  83. PL310_L2CC->DebugControl = 0x0;
  84. //disable l2
  85. PL310_L2CC->Control = 0x0;
  86. dsb();
  87. }
  88. unsigned int get_num_ways(void)
  89. {
  90. unsigned int aux_val;
  91. unsigned int num_ways;
  92. aux_val = PL310_L2CC->AuxControl;
  93. if (aux_val & (1 << 16))
  94. num_ways = 16;
  95. else
  96. num_ways = 8;
  97. return num_ways;
  98. }
  99. #define Auxiliary_VALUE 0x70000000
  100. void arch_enable_l2cache(void)
  101. {
  102. PL310_L2CC->PowerControl = DYNAMIC_CLOCK_GATING_ENABLE;
  103. PL310_L2CC->PrefetchControl = (PL310_L2CC->PrefetchControl) | 0x40000000 ;
  104. PL310_L2CC->AuxControl = (PL310_L2CC->AuxControl & 0X8FFFFFFF) | Auxiliary_VALUE;
  105. way_mask = (1 << get_num_ways()) - 1;
  106. l2_inv_all();
  107. PL310_L2CC->Control = 0x1;
  108. }