tlb.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. }
  92. } else if (((phys_addr & TLB_16MB_ALIGN_MASK) == phys_addr) &&
  93. (mem_size >= TLB_16MB_SIZE)) {
  94. /* Add a 16MB TLB entry */
  95. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  96. TLB_WORD0_SIZE_16MB, tlb_i)) == 0) {
  97. mem_size -= TLB_16MB_SIZE;
  98. phys_addr += TLB_16MB_SIZE;
  99. }
  100. } else if (((phys_addr & TLB_1MB_ALIGN_MASK) == phys_addr) &&
  101. (mem_size >= TLB_1MB_SIZE)) {
  102. /* Add a 1MB TLB entry */
  103. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  104. TLB_WORD0_SIZE_1MB, tlb_i)) == 0) {
  105. mem_size -= TLB_1MB_SIZE;
  106. phys_addr += TLB_1MB_SIZE;
  107. }
  108. } else if (((phys_addr & TLB_256KB_ALIGN_MASK) == phys_addr) &&
  109. (mem_size >= TLB_256KB_SIZE)) {
  110. /* Add a 256KB TLB entry */
  111. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  112. TLB_WORD0_SIZE_256KB, tlb_i)) == 0) {
  113. mem_size -= TLB_256KB_SIZE;
  114. phys_addr += TLB_256KB_SIZE;
  115. }
  116. } else if (((phys_addr & TLB_64KB_ALIGN_MASK) == phys_addr) &&
  117. (mem_size >= TLB_64KB_SIZE)) {
  118. /* Add a 64KB TLB entry */
  119. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  120. TLB_WORD0_SIZE_64KB, tlb_i)) == 0) {
  121. mem_size -= TLB_64KB_SIZE;
  122. phys_addr += TLB_64KB_SIZE;
  123. }
  124. } else if (((phys_addr & TLB_16KB_ALIGN_MASK) == phys_addr) &&
  125. (mem_size >= TLB_16KB_SIZE)) {
  126. /* Add a 16KB TLB entry */
  127. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  128. TLB_WORD0_SIZE_16KB, tlb_i)) == 0) {
  129. mem_size -= TLB_16KB_SIZE;
  130. phys_addr += TLB_16KB_SIZE;
  131. }
  132. } else if (((phys_addr & TLB_4KB_ALIGN_MASK) == phys_addr) &&
  133. (mem_size >= TLB_4KB_SIZE)) {
  134. /* Add a 4KB TLB entry */
  135. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  136. TLB_WORD0_SIZE_4KB, tlb_i)) == 0) {
  137. mem_size -= TLB_4KB_SIZE;
  138. phys_addr += TLB_4KB_SIZE;
  139. }
  140. } else if (((phys_addr & TLB_1KB_ALIGN_MASK) == phys_addr) &&
  141. (mem_size >= TLB_1KB_SIZE)) {
  142. /* Add a 1KB TLB entry */
  143. if ((rc = add_tlb_entry(phys_addr, virt_addr,
  144. TLB_WORD0_SIZE_1KB, tlb_i)) == 0) {
  145. mem_size -= TLB_1KB_SIZE;
  146. phys_addr += TLB_1KB_SIZE;
  147. }
  148. } else {
  149. printf("ERROR: no TLB size exists for the base address 0x%0X.\n",
  150. phys_addr);
  151. }
  152. if (rc != 0)
  153. printf("ERROR: no TLB entries available for the base addr 0x%0X.\n",
  154. phys_addr);
  155. }
  156. return;
  157. }
  158. /*
  159. * Program one (or multiple) TLB entries for one memory region
  160. *
  161. * Common usage for boards with SDRAM DIMM modules to dynamically
  162. * configure the TLB's for the SDRAM
  163. */
  164. void program_tlb(u32 phys_addr, u32 virt_addr, u32 size, u32 tlb_word2_i_value)
  165. {
  166. region_t region_array;
  167. region_array.base = phys_addr;
  168. region_array.size = size;
  169. region_array.tlb_word2_i_value = tlb_word2_i_value; /* en-/disable cache */
  170. /* Call the routine to add in the tlb entries for the memory regions */
  171. program_tlb_addr(region_array.base, virt_addr, region_array.size,
  172. region_array.tlb_word2_i_value);
  173. return;
  174. }
  175. #endif /* CONFIG_440 */