2 * Copyright 2008-2011 Freescale Semiconductor, Inc.
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
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.
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.
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,
27 #include <asm/processor.h>
29 #ifdef CONFIG_ADDR_MAP
33 DECLARE_GLOBAL_DATA_PTR;
35 void invalidate_tlb(u8 tlb)
47 for (i = 0; i < num_tlb_entries; i++) {
48 write_tlb(tlb_table[i].mas0,
58 #if !defined(CONFIG_NAND_SPL) && !defined(CONFIG_SPL_BUILD)
59 void read_tlbcam_entry(int idx, u32 *valid, u32 *tsize, unsigned long *epn,
64 mtspr(MAS0, FSL_BOOKE_MAS0(1, idx, 0));
65 asm volatile("tlbre;isync");
68 *valid = (_mas1 & MAS1_VALID);
69 *tsize = (_mas1 >> 7) & 0x1f;
70 *epn = mfspr(MAS2) & MAS2_EPN;
71 *rpn = mfspr(MAS3) & MAS3_RPN;
72 #ifdef CONFIG_ENABLE_36BIT_PHYS
73 *rpn |= ((u64)mfspr(MAS7)) << 32;
77 void print_tlbcam(void)
80 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
82 /* walk all the entries */
83 printf("TLBCAM entries\n");
84 for (i = 0; i < num_cam; i++) {
89 read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
90 printf("entry %02d: V: %d EPN 0x%08x RPN 0x%08llx size:",
91 i, (valid == 0) ? 0 : 1, (unsigned int)epn,
92 (unsigned long long)rpn);
93 print_size(TSIZE_TO_BYTES(tsize), "\n");
97 static inline void use_tlb_cam(u8 idx)
102 gd->arch.used_tlb_cams[i] |= (1 << bit);
105 static inline void free_tlb_cam(u8 idx)
110 gd->arch.used_tlb_cams[i] &= ~(1 << bit);
113 void init_used_tlb_cams(void)
116 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
118 for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++)
119 gd->arch.used_tlb_cams[i] = 0;
121 /* walk all the entries */
122 for (i = 0; i < num_cam; i++) {
123 mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0));
124 asm volatile("tlbre;isync");
125 if (mfspr(MAS1) & MAS1_VALID)
130 int find_free_tlbcam(void)
135 for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++) {
136 idx = ffz(gd->arch.used_tlb_cams[i]);
144 if (idx >= CONFIG_SYS_NUM_TLBCAMS)
150 void set_tlb(u8 tlb, u32 epn, u64 rpn,
152 u8 ts, u8 esel, u8 tsize, u8 iprot)
154 u32 _mas0, _mas1, _mas2, _mas3, _mas7;
159 if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1 &&
161 printf("%s: bad tsize %d on entry %d at 0x%08x\n",
162 __func__, tsize, tlb, epn);
166 _mas0 = FSL_BOOKE_MAS0(tlb, esel, 0);
167 _mas1 = FSL_BOOKE_MAS1(1, iprot, 0, ts, tsize);
168 _mas2 = FSL_BOOKE_MAS2(epn, wimge);
169 _mas3 = FSL_BOOKE_MAS3(rpn, 0, perms);
170 _mas7 = FSL_BOOKE_MAS7(rpn);
172 write_tlb(_mas0, _mas1, _mas2, _mas3, _mas7);
174 #ifdef CONFIG_ADDR_MAP
175 if ((tlb == 1) && (gd->flags & GD_FLG_RELOC))
176 addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), esel);
180 void disable_tlb(u8 esel)
182 u32 _mas0, _mas1, _mas2, _mas3;
186 _mas0 = FSL_BOOKE_MAS0(1, esel, 0);
195 #ifdef CONFIG_ENABLE_36BIT_PHYS
198 asm volatile("isync;msync;tlbwe;isync");
200 #ifdef CONFIG_ADDR_MAP
201 if (gd->flags & GD_FLG_RELOC)
202 addrmap_set_entry(0, 0, 0, esel);
206 static void tlbsx (const volatile unsigned *addr)
208 __asm__ __volatile__ ("tlbsx 0,%0" : : "r" (addr), "m" (*addr));
211 /* return -1 if we didn't find anything */
212 int find_tlb_idx(void *addr, u8 tlbsel)
216 /* zero out Search PID, AS */
224 /* we found something, and its in the TLB we expect */
225 if ((MAS1_VALID & _mas1) &&
226 (MAS0_TLBSEL(tlbsel) == (_mas0 & MAS0_TLBSEL_MSK))) {
227 return ((_mas0 & MAS0_ESEL_MSK) >> 16);
233 #ifdef CONFIG_ADDR_MAP
234 void init_addr_map(void)
237 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
239 /* walk all the entries */
240 for (i = 0; i < num_cam; i++) {
245 read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
246 if (valid & MAS1_VALID)
247 addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), i);
255 setup_ddr_tlbs_phys(phys_addr_t p_addr, unsigned int memsize_in_meg)
258 unsigned int tlb_size;
259 unsigned int wimge = MAS2_M;
260 unsigned int ram_tlb_address = (unsigned int)CONFIG_SYS_DDR_SDRAM_BASE;
261 unsigned int max_cam, tsize_mask;
262 u64 size, memsize = (u64)memsize_in_meg << 20;
264 #ifdef CONFIG_SYS_PPC_DDR_WIMGE
265 wimge = CONFIG_SYS_PPC_DDR_WIMGE;
267 size = min(memsize, CONFIG_MAX_MEM_MAPPED);
268 if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1) {
269 /* Convert (4^max) kB to (2^max) bytes */
270 max_cam = ((mfspr(SPRN_TLB1CFG) >> 16) & 0xf) * 2 + 10;
273 /* Convert (2^max) kB to (2^max) bytes */
274 max_cam = __ilog2(mfspr(SPRN_TLB1PS)) + 10;
278 for (i = 0; size && i < 8; i++) {
279 int ram_tlb_index = find_free_tlbcam();
280 u32 camsize = __ilog2_u64(size) & tsize_mask;
281 u32 align = __ilog2(ram_tlb_address) & tsize_mask;
283 if (ram_tlb_index == -1)
286 if (align == -2) align = max_cam;
290 if (camsize > max_cam)
293 tlb_size = camsize - 10;
295 set_tlb(1, ram_tlb_address, p_addr,
296 MAS3_SX|MAS3_SW|MAS3_SR, wimge,
297 0, ram_tlb_index, tlb_size, 1);
299 size -= 1ULL << camsize;
300 memsize -= 1ULL << camsize;
301 ram_tlb_address += 1UL << camsize;
302 p_addr += 1UL << camsize;
306 print_size(memsize, " left unmapped\n");
307 return memsize_in_meg;
310 unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg)
313 setup_ddr_tlbs_phys(CONFIG_SYS_DDR_SDRAM_BASE, memsize_in_meg);
316 /* Invalidate the DDR TLBs for the requested size */
317 void clear_ddr_tlbs_phys(phys_addr_t p_addr, unsigned int memsize_in_meg)
319 u32 vstart = CONFIG_SYS_DDR_SDRAM_BASE;
321 u32 tsize, valid, ptr;
324 u64 memsize = (u64)memsize_in_meg << 20;
328 while (ptr < (vstart + memsize)) {
329 ddr_esel = find_tlb_idx((void *)ptr, 1);
330 if (ddr_esel != -1) {
331 read_tlbcam_entry(ddr_esel, &valid, &tsize, &epn, &rpn);
332 disable_tlb(ddr_esel);
334 ptr += TSIZE_TO_BYTES(tsize);
338 void clear_ddr_tlbs(unsigned int memsize_in_meg)
340 clear_ddr_tlbs_phys(CONFIG_SYS_DDR_SDRAM_BASE, memsize_in_meg);