tlb.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2008 Freescale Semiconductor, Inc.
  3. *
  4. * (C) Copyright 2000
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <asm/processor.h>
  27. #include <asm/mmu.h>
  28. #ifdef CONFIG_ADDR_MAP
  29. #include <addr_map.h>
  30. #endif
  31. DECLARE_GLOBAL_DATA_PTR;
  32. void set_tlb(u8 tlb, u32 epn, u64 rpn,
  33. u8 perms, u8 wimge,
  34. u8 ts, u8 esel, u8 tsize, u8 iprot)
  35. {
  36. u32 _mas0, _mas1, _mas2, _mas3, _mas7;
  37. _mas0 = FSL_BOOKE_MAS0(tlb, esel, 0);
  38. _mas1 = FSL_BOOKE_MAS1(1, iprot, 0, ts, tsize);
  39. _mas2 = FSL_BOOKE_MAS2(epn, wimge);
  40. _mas3 = FSL_BOOKE_MAS3(rpn, 0, perms);
  41. _mas7 = rpn >> 32;
  42. mtspr(MAS0, _mas0);
  43. mtspr(MAS1, _mas1);
  44. mtspr(MAS2, _mas2);
  45. mtspr(MAS3, _mas3);
  46. #ifdef CONFIG_ENABLE_36BIT_PHYS
  47. mtspr(MAS7, _mas7);
  48. #endif
  49. asm volatile("isync;msync;tlbwe;isync");
  50. #ifdef CONFIG_ADDR_MAP
  51. if ((tlb == 1) && (gd->flags & GD_FLG_RELOC))
  52. addrmap_set_entry(epn, rpn, (1UL << ((tsize * 2) + 10)), esel);
  53. #endif
  54. }
  55. void disable_tlb(u8 esel)
  56. {
  57. u32 _mas0, _mas1, _mas2, _mas3, _mas7;
  58. _mas0 = FSL_BOOKE_MAS0(1, esel, 0);
  59. _mas1 = 0;
  60. _mas2 = 0;
  61. _mas3 = 0;
  62. _mas7 = 0;
  63. mtspr(MAS0, _mas0);
  64. mtspr(MAS1, _mas1);
  65. mtspr(MAS2, _mas2);
  66. mtspr(MAS3, _mas3);
  67. #ifdef CONFIG_ENABLE_36BIT_PHYS
  68. mtspr(MAS7, _mas7);
  69. #endif
  70. asm volatile("isync;msync;tlbwe;isync");
  71. #ifdef CONFIG_ADDR_MAP
  72. if (gd->flags & GD_FLG_RELOC)
  73. addrmap_set_entry(0, 0, 0, esel);
  74. #endif
  75. }
  76. void invalidate_tlb(u8 tlb)
  77. {
  78. if (tlb == 0)
  79. mtspr(MMUCSR0, 0x4);
  80. if (tlb == 1)
  81. mtspr(MMUCSR0, 0x2);
  82. }
  83. void init_tlbs(void)
  84. {
  85. int i;
  86. for (i = 0; i < num_tlb_entries; i++) {
  87. set_tlb(tlb_table[i].tlb, tlb_table[i].epn, tlb_table[i].rpn,
  88. tlb_table[i].perms, tlb_table[i].wimge,
  89. tlb_table[i].ts, tlb_table[i].esel, tlb_table[i].tsize,
  90. tlb_table[i].iprot);
  91. }
  92. return ;
  93. }
  94. #ifdef CONFIG_ADDR_MAP
  95. void init_addr_map(void)
  96. {
  97. int i;
  98. for (i = 0; i < num_tlb_entries; i++) {
  99. if (tlb_table[i].tlb == 0)
  100. continue;
  101. addrmap_set_entry(tlb_table[i].epn,
  102. tlb_table[i].rpn,
  103. (1UL << ((tlb_table[i].tsize * 2) + 10)),
  104. tlb_table[i].esel);
  105. }
  106. return ;
  107. }
  108. #endif
  109. #ifndef CONFIG_SYS_DDR_TLB_START
  110. #define CONFIG_SYS_DDR_TLB_START 8
  111. #endif
  112. unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg)
  113. {
  114. unsigned int tlb_size;
  115. unsigned int ram_tlb_index = CONFIG_SYS_DDR_TLB_START;
  116. unsigned int ram_tlb_address = (unsigned int)CONFIG_SYS_DDR_SDRAM_BASE;
  117. unsigned int max_cam = (mfspr(SPRN_TLB1CFG) >> 16) & 0xf;
  118. u64 size, memsize = (u64)memsize_in_meg << 20;
  119. size = min(memsize, CONFIG_MAX_MEM_MAPPED);
  120. /* Convert (4^max) kB to (2^max) bytes */
  121. max_cam = max_cam * 2 + 10;
  122. for (; size && ram_tlb_index < 16; ram_tlb_index++) {
  123. u32 camsize = __ilog2_u64(size) & ~1U;
  124. u32 align = __ilog2(ram_tlb_address) & ~1U;
  125. if (align == -2) align = max_cam;
  126. if (camsize > align)
  127. camsize = align;
  128. if (camsize > max_cam)
  129. camsize = max_cam;
  130. tlb_size = (camsize - 10) / 2;
  131. set_tlb(1, ram_tlb_address, ram_tlb_address,
  132. MAS3_SX|MAS3_SW|MAS3_SR, 0,
  133. 0, ram_tlb_index, tlb_size, 1);
  134. size -= 1ULL << camsize;
  135. memsize -= 1ULL << camsize;
  136. ram_tlb_address += 1UL << camsize;
  137. }
  138. if (memsize)
  139. print_size(memsize, " left unmapped\n");
  140. /*
  141. * Confirm that the requested amount of memory was mapped.
  142. */
  143. return memsize_in_meg;
  144. }