blob: 23d33574a09d7a3779ee244cc1062d5891d5014c [file] [log] [blame]
Kumar Gala44a23cf2008-01-16 22:33:22 -06001/*
Kumar Galaebc73942011-02-03 20:21:42 -06002 * Copyright 2008-2011 Freescale Semiconductor, Inc.
Kumar Gala44a23cf2008-01-16 22:33:22 -06003 *
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
26#include <common.h>
27#include <asm/processor.h>
28#include <asm/mmu.h>
Kumar Galaecf5b982008-12-16 14:59:20 -060029#ifdef CONFIG_ADDR_MAP
30#include <addr_map.h>
31#endif
32
33DECLARE_GLOBAL_DATA_PTR;
Kumar Gala44a23cf2008-01-16 22:33:22 -060034
Kumar Galab2eec282009-09-11 12:32:01 -050035void invalidate_tlb(u8 tlb)
36{
37 if (tlb == 0)
38 mtspr(MMUCSR0, 0x4);
39 if (tlb == 1)
40 mtspr(MMUCSR0, 0x2);
41}
42
43void init_tlbs(void)
44{
45 int i;
46
47 for (i = 0; i < num_tlb_entries; i++) {
48 write_tlb(tlb_table[i].mas0,
49 tlb_table[i].mas1,
50 tlb_table[i].mas2,
51 tlb_table[i].mas3,
52 tlb_table[i].mas7);
53 }
54
55 return ;
56}
57
Scott Woodc97cd1b2012-09-20 19:02:18 -050058#if !defined(CONFIG_NAND_SPL) && !defined(CONFIG_SPL_BUILD)
Becky Bruce4e63df32010-06-17 11:37:21 -050059void read_tlbcam_entry(int idx, u32 *valid, u32 *tsize, unsigned long *epn,
60 phys_addr_t *rpn)
61{
62 u32 _mas1;
63
64 mtspr(MAS0, FSL_BOOKE_MAS0(1, idx, 0));
65 asm volatile("tlbre;isync");
66 _mas1 = mfspr(MAS1);
67
68 *valid = (_mas1 & MAS1_VALID);
Scott Wood31d084d2013-01-18 15:45:58 +000069 *tsize = (_mas1 >> 7) & 0x1f;
Becky Bruce4e63df32010-06-17 11:37:21 -050070 *epn = mfspr(MAS2) & MAS2_EPN;
71 *rpn = mfspr(MAS3) & MAS3_RPN;
72#ifdef CONFIG_ENABLE_36BIT_PHYS
73 *rpn |= ((u64)mfspr(MAS7)) << 32;
74#endif
75}
76
Becky Bruce70e02bc2010-06-17 11:37:22 -050077void print_tlbcam(void)
78{
79 int i;
80 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
81
82 /* walk all the entries */
83 printf("TLBCAM entries\n");
84 for (i = 0; i < num_cam; i++) {
85 unsigned long epn;
86 u32 tsize, valid;
87 phys_addr_t rpn;
88
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");
94 }
95}
96
Kumar Gala94e94112009-11-12 10:26:16 -060097static inline void use_tlb_cam(u8 idx)
98{
99 int i = idx / 32;
100 int bit = idx % 32;
101
102 gd->used_tlb_cams[i] |= (1 << bit);
103}
104
105static inline void free_tlb_cam(u8 idx)
106{
107 int i = idx / 32;
108 int bit = idx % 32;
109
110 gd->used_tlb_cams[i] &= ~(1 << bit);
111}
112
113void init_used_tlb_cams(void)
114{
115 int i;
116 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
117
118 for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++)
119 gd->used_tlb_cams[i] = 0;
120
121 /* walk all the entries */
122 for (i = 0; i < num_cam; i++) {
Kumar Gala94e94112009-11-12 10:26:16 -0600123 mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0));
Kumar Gala94e94112009-11-12 10:26:16 -0600124 asm volatile("tlbre;isync");
Becky Bruce4e63df32010-06-17 11:37:21 -0500125 if (mfspr(MAS1) & MAS1_VALID)
Kumar Gala94e94112009-11-12 10:26:16 -0600126 use_tlb_cam(i);
127 }
128}
129
130int find_free_tlbcam(void)
131{
132 int i;
133 u32 idx;
134
135 for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++) {
136 idx = ffz(gd->used_tlb_cams[i]);
137
138 if (idx != 32)
139 break;
140 }
141
142 idx += i * 32;
143
144 if (idx >= CONFIG_SYS_NUM_TLBCAMS)
145 return -1;
146
147 return idx;
148}
149
Kumar Gala44a23cf2008-01-16 22:33:22 -0600150void set_tlb(u8 tlb, u32 epn, u64 rpn,
151 u8 perms, u8 wimge,
152 u8 ts, u8 esel, u8 tsize, u8 iprot)
153{
154 u32 _mas0, _mas1, _mas2, _mas3, _mas7;
155
Kumar Gala94e94112009-11-12 10:26:16 -0600156 if (tlb == 1)
157 use_tlb_cam(esel);
158
Scott Wood31d084d2013-01-18 15:45:58 +0000159 if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1 &&
160 tsize & 1) {
161 printf("%s: bad tsize %d on entry %d at 0x%08x\n",
162 __func__, tsize, tlb, epn);
163 return;
164 }
165
Kumar Gala44a23cf2008-01-16 22:33:22 -0600166 _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);
Kumar Galad30f9042009-09-11 11:27:00 -0500170 _mas7 = FSL_BOOKE_MAS7(rpn);
Kumar Gala44a23cf2008-01-16 22:33:22 -0600171
Kumar Galad30f9042009-09-11 11:27:00 -0500172 write_tlb(_mas0, _mas1, _mas2, _mas3, _mas7);
Kumar Galaecf5b982008-12-16 14:59:20 -0600173
174#ifdef CONFIG_ADDR_MAP
175 if ((tlb == 1) && (gd->flags & GD_FLG_RELOC))
Becky Bruce4e63df32010-06-17 11:37:21 -0500176 addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), esel);
Kumar Galaecf5b982008-12-16 14:59:20 -0600177#endif
Kumar Gala44a23cf2008-01-16 22:33:22 -0600178}
179
180void disable_tlb(u8 esel)
181{
Kumar Gala3d6d9c32011-11-09 09:59:32 -0600182 u32 _mas0, _mas1, _mas2, _mas3;
Kumar Gala44a23cf2008-01-16 22:33:22 -0600183
Kumar Gala94e94112009-11-12 10:26:16 -0600184 free_tlb_cam(esel);
185
Kumar Gala44a23cf2008-01-16 22:33:22 -0600186 _mas0 = FSL_BOOKE_MAS0(1, esel, 0);
187 _mas1 = 0;
188 _mas2 = 0;
189 _mas3 = 0;
Kumar Gala44a23cf2008-01-16 22:33:22 -0600190
191 mtspr(MAS0, _mas0);
192 mtspr(MAS1, _mas1);
193 mtspr(MAS2, _mas2);
194 mtspr(MAS3, _mas3);
195#ifdef CONFIG_ENABLE_36BIT_PHYS
Kumar Gala3d6d9c32011-11-09 09:59:32 -0600196 mtspr(MAS7, 0);
Kumar Gala44a23cf2008-01-16 22:33:22 -0600197#endif
198 asm volatile("isync;msync;tlbwe;isync");
Kumar Galaecf5b982008-12-16 14:59:20 -0600199
200#ifdef CONFIG_ADDR_MAP
201 if (gd->flags & GD_FLG_RELOC)
202 addrmap_set_entry(0, 0, 0, esel);
203#endif
Kumar Gala44a23cf2008-01-16 22:33:22 -0600204}
205
Kumar Galac2287af2009-09-03 08:20:24 -0500206static void tlbsx (const volatile unsigned *addr)
207{
208 __asm__ __volatile__ ("tlbsx 0,%0" : : "r" (addr), "m" (*addr));
209}
210
211/* return -1 if we didn't find anything */
212int find_tlb_idx(void *addr, u8 tlbsel)
213{
214 u32 _mas0, _mas1;
215
216 /* zero out Search PID, AS */
217 mtspr(MAS6, 0);
218
219 tlbsx(addr);
220
221 _mas0 = mfspr(MAS0);
222 _mas1 = mfspr(MAS1);
223
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);
228 }
229
230 return -1;
231}
232
Kumar Galaecf5b982008-12-16 14:59:20 -0600233#ifdef CONFIG_ADDR_MAP
234void init_addr_map(void)
235{
236 int i;
Kumar Galacdbdbe62009-11-13 08:52:21 -0600237 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
Kumar Galaecf5b982008-12-16 14:59:20 -0600238
Kumar Galae393e2e2009-08-14 16:43:22 -0500239 /* walk all the entries */
Kumar Galacdbdbe62009-11-13 08:52:21 -0600240 for (i = 0; i < num_cam; i++) {
Kumar Galae393e2e2009-08-14 16:43:22 -0500241 unsigned long epn;
Becky Bruce4e63df32010-06-17 11:37:21 -0500242 u32 tsize, valid;
Kumar Galae393e2e2009-08-14 16:43:22 -0500243 phys_addr_t rpn;
244
Becky Bruce4e63df32010-06-17 11:37:21 -0500245 read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
246 if (valid & MAS1_VALID)
247 addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), i);
Kumar Galaecf5b982008-12-16 14:59:20 -0600248 }
249
250 return ;
251}
252#endif
253
York Sunc02ce6e2010-09-28 15:20:32 -0700254unsigned int
255setup_ddr_tlbs_phys(phys_addr_t p_addr, unsigned int memsize_in_meg)
Kumar Gala6fb1b732008-06-09 11:07:46 -0500256{
Kumar Gala355f4f82009-11-13 09:04:19 -0600257 int i;
Kumar Gala6fb1b732008-06-09 11:07:46 -0500258 unsigned int tlb_size;
York Sunffd06e02012-10-08 07:44:30 +0000259 unsigned int wimge = MAS2_M;
Kumar Galaf8523cb2009-02-06 09:56:35 -0600260 unsigned int ram_tlb_address = (unsigned int)CONFIG_SYS_DDR_SDRAM_BASE;
Scott Wood31d084d2013-01-18 15:45:58 +0000261 unsigned int max_cam, tsize_mask;
Kumar Galaf8523cb2009-02-06 09:56:35 -0600262 u64 size, memsize = (u64)memsize_in_meg << 20;
Kumar Gala6fb1b732008-06-09 11:07:46 -0500263
Becky Bruce6b1ef2a2010-12-17 17:17:55 -0600264#ifdef CONFIG_SYS_PPC_DDR_WIMGE
265 wimge = CONFIG_SYS_PPC_DDR_WIMGE;
266#endif
Kumar Galaf8523cb2009-02-06 09:56:35 -0600267 size = min(memsize, CONFIG_MAX_MEM_MAPPED);
Kumar Gala50cf3d12011-10-31 22:13:26 -0500268 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;
Scott Wood31d084d2013-01-18 15:45:58 +0000271 tsize_mask = ~1U;
Kumar Gala50cf3d12011-10-31 22:13:26 -0500272 } else {
273 /* Convert (2^max) kB to (2^max) bytes */
274 max_cam = __ilog2(mfspr(SPRN_TLB1PS)) + 10;
Scott Wood31d084d2013-01-18 15:45:58 +0000275 tsize_mask = ~0U;
Kumar Gala50cf3d12011-10-31 22:13:26 -0500276 }
Kumar Gala6fb1b732008-06-09 11:07:46 -0500277
Kumar Gala355f4f82009-11-13 09:04:19 -0600278 for (i = 0; size && i < 8; i++) {
279 int ram_tlb_index = find_free_tlbcam();
Scott Wood31d084d2013-01-18 15:45:58 +0000280 u32 camsize = __ilog2_u64(size) & tsize_mask;
281 u32 align = __ilog2(ram_tlb_address) & tsize_mask;
Kumar Galaf8523cb2009-02-06 09:56:35 -0600282
Kumar Gala355f4f82009-11-13 09:04:19 -0600283 if (ram_tlb_index == -1)
284 break;
285
Kumar Galaf8523cb2009-02-06 09:56:35 -0600286 if (align == -2) align = max_cam;
287 if (camsize > align)
288 camsize = align;
289
290 if (camsize > max_cam)
291 camsize = max_cam;
292
Scott Wood31d084d2013-01-18 15:45:58 +0000293 tlb_size = camsize - 10;
Kumar Galaf8523cb2009-02-06 09:56:35 -0600294
York Sunc02ce6e2010-09-28 15:20:32 -0700295 set_tlb(1, ram_tlb_address, p_addr,
Becky Bruce6b1ef2a2010-12-17 17:17:55 -0600296 MAS3_SX|MAS3_SW|MAS3_SR, wimge,
Kumar Gala6fb1b732008-06-09 11:07:46 -0500297 0, ram_tlb_index, tlb_size, 1);
298
Kumar Galaf8523cb2009-02-06 09:56:35 -0600299 size -= 1ULL << camsize;
300 memsize -= 1ULL << camsize;
301 ram_tlb_address += 1UL << camsize;
York Sunc02ce6e2010-09-28 15:20:32 -0700302 p_addr += 1UL << camsize;
Kumar Gala6fb1b732008-06-09 11:07:46 -0500303 }
304
Kumar Galaf8523cb2009-02-06 09:56:35 -0600305 if (memsize)
Kumar Galad4b130d2009-06-11 23:40:34 -0500306 print_size(memsize, " left unmapped\n");
Kumar Gala6fb1b732008-06-09 11:07:46 -0500307 return memsize_in_meg;
308}
York Sunc02ce6e2010-09-28 15:20:32 -0700309
310unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg)
311{
312 return
313 setup_ddr_tlbs_phys(CONFIG_SYS_DDR_SDRAM_BASE, memsize_in_meg);
314}
Becky Bruce9cdfe282011-07-18 18:49:15 -0500315
316/* Invalidate the DDR TLBs for the requested size */
317void clear_ddr_tlbs_phys(phys_addr_t p_addr, unsigned int memsize_in_meg)
318{
319 u32 vstart = CONFIG_SYS_DDR_SDRAM_BASE;
320 unsigned long epn;
321 u32 tsize, valid, ptr;
322 phys_addr_t rpn = 0;
323 int ddr_esel;
324 u64 memsize = (u64)memsize_in_meg << 20;
325
326 ptr = vstart;
327
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);
333 }
334 ptr += TSIZE_TO_BYTES(tsize);
335 }
336}
337
338void clear_ddr_tlbs(unsigned int memsize_in_meg)
339{
340 clear_ddr_tlbs_phys(CONFIG_SYS_DDR_SDRAM_BASE, memsize_in_meg);
341}
342
343
Scott Woodc97cd1b2012-09-20 19:02:18 -0500344#endif /* not SPL */