tlb.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 phys_addr,
  35. unsigned long virt_addr,
  36. unsigned long tlb_word0_size_value,
  37. unsigned long tlb_word2_i_value)
  38. {
  39. int i;
  40. unsigned long tlb_word0_value;
  41. unsigned long tlb_word1_value;
  42. unsigned long tlb_word2_value;
  43. /* First, find the index of a TLB entry not being used */
  44. for (i=0; i<PPC4XX_TLB_SIZE; i++) {
  45. tlb_word0_value = mftlb1(i);
  46. if ((tlb_word0_value & TLB_WORD0_V_MASK) == TLB_WORD0_V_DISABLE)
  47. break;
  48. }
  49. if (i >= PPC4XX_TLB_SIZE)
  50. return -1;
  51. /* Second, create the TLB entry */
  52. tlb_word0_value = TLB_WORD0_EPN_ENCODE(virt_addr) | TLB_WORD0_V_ENABLE |
  53. TLB_WORD0_TS_0 | tlb_word0_size_value;
  54. tlb_word1_value = TLB_WORD1_RPN_ENCODE(phys_addr) | TLB_WORD1_ERPN_ENCODE(0);
  55. tlb_word2_value = TLB_WORD2_U0_DISABLE | TLB_WORD2_U1_DISABLE |
  56. TLB_WORD2_U2_DISABLE | TLB_WORD2_U3_DISABLE |
  57. TLB_WORD2_W_DISABLE | tlb_word2_i_value |
  58. TLB_WORD2_M_DISABLE | TLB_WORD2_G_DISABLE |
  59. TLB_WORD2_E_DISABLE | TLB_WORD2_UX_ENABLE |
  60. TLB_WORD2_UW_ENABLE | TLB_WORD2_UR_ENABLE |
  61. TLB_WORD2_SX_ENABLE | TLB_WORD2_SW_ENABLE |
  62. TLB_WORD2_SR_ENABLE;
  63. /* Wait for all memory accesses to complete */
  64. sync();
  65. /* Third, add the TLB entries */
  66. mttlb1(i, tlb_word0_value);
  67. mttlb2(i, tlb_word1_value);
  68. mttlb3(i, tlb_word2_value);
  69. /* Execute an ISYNC instruction so that the new TLB entry takes effect */
  70. asm("isync");
  71. return 0;
  72. }
  73. static void program_tlb_addr(unsigned long phys_addr,
  74. unsigned long virt_addr,
  75. unsigned long mem_size,
  76. unsigned long tlb_word2_i_value)
  77. {
  78. int rc;
  79. int tlb_i;
  80. tlb_i = tlb_word2_i_value;
  81. while (mem_size != 0) {
  82. rc = 0;
  83. /* Add the TLB entries in to map the region. */
  84. if (((phys_addr & TLB_256MB_ALIGN_MASK) == phys_addr) &&
  85. (mem_size >= TLB_256MB_SIZE)) {
  86. /* Add a 256MB TLB entry */
  87. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  88. TLB_WORD0_SIZE_256MB, tlb_i)) == 0) {
  89. mem_size -= TLB_256MB_SIZE;
  90. phys_addr += TLB_256MB_SIZE;
  91. virt_addr += TLB_256MB_SIZE;
  92. }
  93. } else if (((phys_addr & TLB_16MB_ALIGN_MASK) == phys_addr) &&
  94. (mem_size >= TLB_16MB_SIZE)) {
  95. /* Add a 16MB TLB entry */
  96. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  97. TLB_WORD0_SIZE_16MB, tlb_i)) == 0) {
  98. mem_size -= TLB_16MB_SIZE;
  99. phys_addr += TLB_16MB_SIZE;
  100. virt_addr += TLB_16MB_SIZE;
  101. }
  102. } else if (((phys_addr & TLB_1MB_ALIGN_MASK) == phys_addr) &&
  103. (mem_size >= TLB_1MB_SIZE)) {
  104. /* Add a 1MB TLB entry */
  105. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  106. TLB_WORD0_SIZE_1MB, tlb_i)) == 0) {
  107. mem_size -= TLB_1MB_SIZE;
  108. phys_addr += TLB_1MB_SIZE;
  109. virt_addr += TLB_1MB_SIZE;
  110. }
  111. } else if (((phys_addr & TLB_256KB_ALIGN_MASK) == phys_addr) &&
  112. (mem_size >= TLB_256KB_SIZE)) {
  113. /* Add a 256KB TLB entry */
  114. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  115. TLB_WORD0_SIZE_256KB, tlb_i)) == 0) {
  116. mem_size -= TLB_256KB_SIZE;
  117. phys_addr += TLB_256KB_SIZE;
  118. virt_addr += TLB_256KB_SIZE;
  119. }
  120. } else if (((phys_addr & TLB_64KB_ALIGN_MASK) == phys_addr) &&
  121. (mem_size >= TLB_64KB_SIZE)) {
  122. /* Add a 64KB TLB entry */
  123. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  124. TLB_WORD0_SIZE_64KB, tlb_i)) == 0) {
  125. mem_size -= TLB_64KB_SIZE;
  126. phys_addr += TLB_64KB_SIZE;
  127. virt_addr += TLB_64KB_SIZE;
  128. }
  129. } else if (((phys_addr & TLB_16KB_ALIGN_MASK) == phys_addr) &&
  130. (mem_size >= TLB_16KB_SIZE)) {
  131. /* Add a 16KB TLB entry */
  132. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  133. TLB_WORD0_SIZE_16KB, tlb_i)) == 0) {
  134. mem_size -= TLB_16KB_SIZE;
  135. phys_addr += TLB_16KB_SIZE;
  136. virt_addr += TLB_16KB_SIZE;
  137. }
  138. } else if (((phys_addr & TLB_4KB_ALIGN_MASK) == phys_addr) &&
  139. (mem_size >= TLB_4KB_SIZE)) {
  140. /* Add a 4KB TLB entry */
  141. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  142. TLB_WORD0_SIZE_4KB, tlb_i)) == 0) {
  143. mem_size -= TLB_4KB_SIZE;
  144. phys_addr += TLB_4KB_SIZE;
  145. virt_addr += TLB_4KB_SIZE;
  146. }
  147. } else if (((phys_addr & TLB_1KB_ALIGN_MASK) == phys_addr) &&
  148. (mem_size >= TLB_1KB_SIZE)) {
  149. /* Add a 1KB TLB entry */
  150. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  151. TLB_WORD0_SIZE_1KB, tlb_i)) == 0) {
  152. mem_size -= TLB_1KB_SIZE;
  153. phys_addr += TLB_1KB_SIZE;
  154. virt_addr += TLB_1KB_SIZE;
  155. }
  156. } else {
  157. printf("ERROR: no TLB size exists for the base address 0x%0X.\n",
  158. phys_addr);
  159. }
  160. if (rc != 0)
  161. printf("ERROR: no TLB entries available for the base addr 0x%0X.\n",
  162. phys_addr);
  163. }
  164. return;
  165. }
  166. /*
  167. * Program one (or multiple) TLB entries for one memory region
  168. *
  169. * Common usage for boards with SDRAM DIMM modules to dynamically
  170. * configure the TLB's for the SDRAM
  171. */
  172. void program_tlb(u32 phys_addr, u32 virt_addr, u32 size, u32 tlb_word2_i_value)
  173. {
  174. region_t region_array;
  175. region_array.base = phys_addr;
  176. region_array.size = size;
  177. region_array.tlb_word2_i_value = tlb_word2_i_value; /* en-/disable cache */
  178. /* Call the routine to add in the tlb entries for the memory regions */
  179. program_tlb_addr(region_array.base, virt_addr, region_array.size,
  180. region_array.tlb_word2_i_value);
  181. return;
  182. }
  183. #endif /* CONFIG_440 */