tlb.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * (C) Copyright 2007
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #if defined(CONFIG_440)
  25. #include <ppc4xx.h>
  26. #include <ppc440.h>
  27. #include <asm/io.h>
  28. #include <asm/mmu.h>
  29. typedef struct region {
  30. unsigned long base;
  31. unsigned long size;
  32. unsigned long tlb_word2_i_value;
  33. } region_t;
  34. static int add_tlb_entry(unsigned long base_addr,
  35. unsigned long tlb_word0_size_value,
  36. unsigned long tlb_word2_i_value)
  37. {
  38. int i;
  39. unsigned long tlb_word0_value;
  40. unsigned long tlb_word1_value;
  41. unsigned long tlb_word2_value;
  42. /* First, find the index of a TLB entry not being used */
  43. for (i=0; i<PPC4XX_TLB_SIZE; i++) {
  44. tlb_word0_value = mftlb1(i);
  45. if ((tlb_word0_value & TLB_WORD0_V_MASK) == TLB_WORD0_V_DISABLE)
  46. break;
  47. }
  48. if (i >= PPC4XX_TLB_SIZE)
  49. return -1;
  50. /* Second, create the TLB entry */
  51. tlb_word0_value = TLB_WORD0_EPN_ENCODE(base_addr) | TLB_WORD0_V_ENABLE |
  52. TLB_WORD0_TS_0 | tlb_word0_size_value;
  53. tlb_word1_value = TLB_WORD1_RPN_ENCODE(base_addr) | TLB_WORD1_ERPN_ENCODE(0);
  54. tlb_word2_value = TLB_WORD2_U0_DISABLE | TLB_WORD2_U1_DISABLE |
  55. TLB_WORD2_U2_DISABLE | TLB_WORD2_U3_DISABLE |
  56. TLB_WORD2_W_DISABLE | tlb_word2_i_value |
  57. TLB_WORD2_M_DISABLE | TLB_WORD2_G_DISABLE |
  58. TLB_WORD2_E_DISABLE | TLB_WORD2_UX_ENABLE |
  59. TLB_WORD2_UW_ENABLE | TLB_WORD2_UR_ENABLE |
  60. TLB_WORD2_SX_ENABLE | TLB_WORD2_SW_ENABLE |
  61. TLB_WORD2_SR_ENABLE;
  62. /* Wait for all memory accesses to complete */
  63. sync();
  64. /* Third, add the TLB entries */
  65. mttlb1(i, tlb_word0_value);
  66. mttlb2(i, tlb_word1_value);
  67. mttlb3(i, tlb_word2_value);
  68. /* Execute an ISYNC instruction so that the new TLB entry takes effect */
  69. asm("isync");
  70. return 0;
  71. }
  72. static void program_tlb_addr(unsigned long base_addr, unsigned long mem_size,
  73. unsigned long tlb_word2_i_value)
  74. {
  75. int rc;
  76. int tlb_i;
  77. tlb_i = tlb_word2_i_value;
  78. while (mem_size != 0) {
  79. rc = 0;
  80. /* Add the TLB entries in to map the region. */
  81. if (((base_addr & TLB_256MB_ALIGN_MASK) == base_addr) &&
  82. (mem_size >= TLB_256MB_SIZE)) {
  83. /* Add a 256MB TLB entry */
  84. if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_256MB, tlb_i)) == 0) {
  85. mem_size -= TLB_256MB_SIZE;
  86. base_addr += TLB_256MB_SIZE;
  87. }
  88. } else if (((base_addr & TLB_16MB_ALIGN_MASK) == base_addr) &&
  89. (mem_size >= TLB_16MB_SIZE)) {
  90. /* Add a 16MB TLB entry */
  91. if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_16MB, tlb_i)) == 0) {
  92. mem_size -= TLB_16MB_SIZE;
  93. base_addr += TLB_16MB_SIZE;
  94. }
  95. } else if (((base_addr & TLB_1MB_ALIGN_MASK) == base_addr) &&
  96. (mem_size >= TLB_1MB_SIZE)) {
  97. /* Add a 1MB TLB entry */
  98. if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_1MB, tlb_i)) == 0) {
  99. mem_size -= TLB_1MB_SIZE;
  100. base_addr += TLB_1MB_SIZE;
  101. }
  102. } else if (((base_addr & TLB_256KB_ALIGN_MASK) == base_addr) &&
  103. (mem_size >= TLB_256KB_SIZE)) {
  104. /* Add a 256KB TLB entry */
  105. if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_256KB, tlb_i)) == 0) {
  106. mem_size -= TLB_256KB_SIZE;
  107. base_addr += TLB_256KB_SIZE;
  108. }
  109. } else if (((base_addr & TLB_64KB_ALIGN_MASK) == base_addr) &&
  110. (mem_size >= TLB_64KB_SIZE)) {
  111. /* Add a 64KB TLB entry */
  112. if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_64KB, tlb_i)) == 0) {
  113. mem_size -= TLB_64KB_SIZE;
  114. base_addr += TLB_64KB_SIZE;
  115. }
  116. } else if (((base_addr & TLB_16KB_ALIGN_MASK) == base_addr) &&
  117. (mem_size >= TLB_16KB_SIZE)) {
  118. /* Add a 16KB TLB entry */
  119. if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_16KB, tlb_i)) == 0) {
  120. mem_size -= TLB_16KB_SIZE;
  121. base_addr += TLB_16KB_SIZE;
  122. }
  123. } else if (((base_addr & TLB_4KB_ALIGN_MASK) == base_addr) &&
  124. (mem_size >= TLB_4KB_SIZE)) {
  125. /* Add a 4KB TLB entry */
  126. if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_4KB, tlb_i)) == 0) {
  127. mem_size -= TLB_4KB_SIZE;
  128. base_addr += TLB_4KB_SIZE;
  129. }
  130. } else if (((base_addr & TLB_1KB_ALIGN_MASK) == base_addr) &&
  131. (mem_size >= TLB_1KB_SIZE)) {
  132. /* Add a 1KB TLB entry */
  133. if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_1KB, tlb_i)) == 0) {
  134. mem_size -= TLB_1KB_SIZE;
  135. base_addr += TLB_1KB_SIZE;
  136. }
  137. } else {
  138. printf("ERROR: no TLB size exists for the base address 0x%0X.\n",
  139. base_addr);
  140. }
  141. if (rc != 0)
  142. printf("ERROR: no TLB entries available for the base addr 0x%0X.\n",
  143. base_addr);
  144. }
  145. return;
  146. }
  147. /*
  148. * Program one (or multiple) TLB entries for one memory region
  149. *
  150. * Common usage for boards with SDRAM DIMM modules to dynamically
  151. * configure the TLB's for the SDRAM
  152. */
  153. void program_tlb(u32 start, u32 size)
  154. {
  155. region_t region_array;
  156. region_array.base = start;
  157. region_array.size = size;
  158. region_array.tlb_word2_i_value = TLB_WORD2_I_ENABLE; /* disable cache (for now) */
  159. /* Call the routine to add in the tlb entries for the memory regions */
  160. program_tlb_addr(region_array.base, region_array.size,
  161. region_array.tlb_word2_i_value);
  162. return;
  163. }
  164. #endif /* CONFIG_440 */