tlb.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright 2008-2009 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 invalidate_tlb(u8 tlb)
  33. {
  34. if (tlb == 0)
  35. mtspr(MMUCSR0, 0x4);
  36. if (tlb == 1)
  37. mtspr(MMUCSR0, 0x2);
  38. }
  39. void init_tlbs(void)
  40. {
  41. int i;
  42. for (i = 0; i < num_tlb_entries; i++) {
  43. write_tlb(tlb_table[i].mas0,
  44. tlb_table[i].mas1,
  45. tlb_table[i].mas2,
  46. tlb_table[i].mas3,
  47. tlb_table[i].mas7);
  48. }
  49. return ;
  50. }
  51. #ifndef CONFIG_NAND_SPL
  52. static inline void use_tlb_cam(u8 idx)
  53. {
  54. int i = idx / 32;
  55. int bit = idx % 32;
  56. gd->used_tlb_cams[i] |= (1 << bit);
  57. }
  58. static inline void free_tlb_cam(u8 idx)
  59. {
  60. int i = idx / 32;
  61. int bit = idx % 32;
  62. gd->used_tlb_cams[i] &= ~(1 << bit);
  63. }
  64. void init_used_tlb_cams(void)
  65. {
  66. int i;
  67. unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
  68. for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++)
  69. gd->used_tlb_cams[i] = 0;
  70. /* walk all the entries */
  71. for (i = 0; i < num_cam; i++) {
  72. u32 _mas1;
  73. mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0));
  74. asm volatile("tlbre;isync");
  75. _mas1 = mfspr(MAS1);
  76. /* if the entry isn't valid skip it */
  77. if ((_mas1 & MAS1_VALID))
  78. use_tlb_cam(i);
  79. }
  80. }
  81. int find_free_tlbcam(void)
  82. {
  83. int i;
  84. u32 idx;
  85. for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++) {
  86. idx = ffz(gd->used_tlb_cams[i]);
  87. if (idx != 32)
  88. break;
  89. }
  90. idx += i * 32;
  91. if (idx >= CONFIG_SYS_NUM_TLBCAMS)
  92. return -1;
  93. return idx;
  94. }
  95. void set_tlb(u8 tlb, u32 epn, u64 rpn,
  96. u8 perms, u8 wimge,
  97. u8 ts, u8 esel, u8 tsize, u8 iprot)
  98. {
  99. u32 _mas0, _mas1, _mas2, _mas3, _mas7;
  100. if (tlb == 1)
  101. use_tlb_cam(esel);
  102. _mas0 = FSL_BOOKE_MAS0(tlb, esel, 0);
  103. _mas1 = FSL_BOOKE_MAS1(1, iprot, 0, ts, tsize);
  104. _mas2 = FSL_BOOKE_MAS2(epn, wimge);
  105. _mas3 = FSL_BOOKE_MAS3(rpn, 0, perms);
  106. _mas7 = FSL_BOOKE_MAS7(rpn);
  107. write_tlb(_mas0, _mas1, _mas2, _mas3, _mas7);
  108. #ifdef CONFIG_ADDR_MAP
  109. if ((tlb == 1) && (gd->flags & GD_FLG_RELOC))
  110. addrmap_set_entry(epn, rpn, (1UL << ((tsize * 2) + 10)), esel);
  111. #endif
  112. }
  113. void disable_tlb(u8 esel)
  114. {
  115. u32 _mas0, _mas1, _mas2, _mas3, _mas7;
  116. free_tlb_cam(esel);
  117. _mas0 = FSL_BOOKE_MAS0(1, esel, 0);
  118. _mas1 = 0;
  119. _mas2 = 0;
  120. _mas3 = 0;
  121. _mas7 = 0;
  122. mtspr(MAS0, _mas0);
  123. mtspr(MAS1, _mas1);
  124. mtspr(MAS2, _mas2);
  125. mtspr(MAS3, _mas3);
  126. #ifdef CONFIG_ENABLE_36BIT_PHYS
  127. mtspr(MAS7, _mas7);
  128. #endif
  129. asm volatile("isync;msync;tlbwe;isync");
  130. #ifdef CONFIG_ADDR_MAP
  131. if (gd->flags & GD_FLG_RELOC)
  132. addrmap_set_entry(0, 0, 0, esel);
  133. #endif
  134. }
  135. static void tlbsx (const volatile unsigned *addr)
  136. {
  137. __asm__ __volatile__ ("tlbsx 0,%0" : : "r" (addr), "m" (*addr));
  138. }
  139. /* return -1 if we didn't find anything */
  140. int find_tlb_idx(void *addr, u8 tlbsel)
  141. {
  142. u32 _mas0, _mas1;
  143. /* zero out Search PID, AS */
  144. mtspr(MAS6, 0);
  145. tlbsx(addr);
  146. _mas0 = mfspr(MAS0);
  147. _mas1 = mfspr(MAS1);
  148. /* we found something, and its in the TLB we expect */
  149. if ((MAS1_VALID & _mas1) &&
  150. (MAS0_TLBSEL(tlbsel) == (_mas0 & MAS0_TLBSEL_MSK))) {
  151. return ((_mas0 & MAS0_ESEL_MSK) >> 16);
  152. }
  153. return -1;
  154. }
  155. #ifdef CONFIG_ADDR_MAP
  156. void init_addr_map(void)
  157. {
  158. int i;
  159. unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
  160. /* walk all the entries */
  161. for (i = 0; i < num_cam; i++) {
  162. unsigned long epn;
  163. u32 tsize, _mas1;
  164. phys_addr_t rpn;
  165. mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0));
  166. asm volatile("tlbre;isync");
  167. _mas1 = mfspr(MAS1);
  168. /* if the entry isn't valid skip it */
  169. if (!(_mas1 & MAS1_VALID))
  170. continue;
  171. tsize = (_mas1 >> 8) & 0xf;
  172. epn = mfspr(MAS2) & MAS2_EPN;
  173. rpn = mfspr(MAS3) & MAS3_RPN;
  174. #ifdef CONFIG_ENABLE_36BIT_PHYS
  175. rpn |= ((phys_addr_t)mfspr(MAS7)) << 32;
  176. #endif
  177. addrmap_set_entry(epn, rpn, (1UL << ((tsize * 2) + 10)), i);
  178. }
  179. return ;
  180. }
  181. #endif
  182. #ifndef CONFIG_SYS_DDR_TLB_START
  183. #define CONFIG_SYS_DDR_TLB_START 8
  184. #endif
  185. unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg)
  186. {
  187. unsigned int tlb_size;
  188. unsigned int ram_tlb_index = CONFIG_SYS_DDR_TLB_START;
  189. unsigned int ram_tlb_address = (unsigned int)CONFIG_SYS_DDR_SDRAM_BASE;
  190. unsigned int max_cam = (mfspr(SPRN_TLB1CFG) >> 16) & 0xf;
  191. u64 size, memsize = (u64)memsize_in_meg << 20;
  192. size = min(memsize, CONFIG_MAX_MEM_MAPPED);
  193. /* Convert (4^max) kB to (2^max) bytes */
  194. max_cam = max_cam * 2 + 10;
  195. for (; size && ram_tlb_index < 16; ram_tlb_index++) {
  196. u32 camsize = __ilog2_u64(size) & ~1U;
  197. u32 align = __ilog2(ram_tlb_address) & ~1U;
  198. if (align == -2) align = max_cam;
  199. if (camsize > align)
  200. camsize = align;
  201. if (camsize > max_cam)
  202. camsize = max_cam;
  203. tlb_size = (camsize - 10) / 2;
  204. set_tlb(1, ram_tlb_address, ram_tlb_address,
  205. MAS3_SX|MAS3_SW|MAS3_SR, 0,
  206. 0, ram_tlb_index, tlb_size, 1);
  207. size -= 1ULL << camsize;
  208. memsize -= 1ULL << camsize;
  209. ram_tlb_address += 1UL << camsize;
  210. }
  211. if (memsize)
  212. print_size(memsize, " left unmapped\n");
  213. /*
  214. * Confirm that the requested amount of memory was mapped.
  215. */
  216. return memsize_in_meg;
  217. }
  218. #endif /* !CONFIG_NAND_SPL */