blob: cf31eb2ef3a61a05b29d4a1ea9d5971144684355 [file] [log] [blame]
Kumar Gala95bb67f2008-01-16 22:33:22 -06001/*
Kumar Galad7ff6a82011-02-03 20:21:42 -06002 * Copyright 2008-2011 Freescale Semiconductor, Inc.
Kumar Gala95bb67f2008-01-16 22:33:22 -06003 *
4 * (C) Copyright 2000
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Kumar Gala95bb67f2008-01-16 22:33:22 -06008 */
9
10#include <common.h>
11#include <asm/processor.h>
12#include <asm/mmu.h>
Kumar Gala9ac287a2008-12-16 14:59:20 -060013#ifdef CONFIG_ADDR_MAP
14#include <addr_map.h>
15#endif
16
Fabio Estevam1a03a7e2015-11-05 12:43:40 -020017#include <linux/log2.h>
18
Kumar Gala9ac287a2008-12-16 14:59:20 -060019DECLARE_GLOBAL_DATA_PTR;
Kumar Gala95bb67f2008-01-16 22:33:22 -060020
Kumar Galaafc51ad2009-09-11 12:32:01 -050021void invalidate_tlb(u8 tlb)
22{
23 if (tlb == 0)
24 mtspr(MMUCSR0, 0x4);
25 if (tlb == 1)
26 mtspr(MMUCSR0, 0x2);
27}
28
Alexander Grafc3468482014-04-11 17:09:45 +020029__weak void init_tlbs(void)
Kumar Galaafc51ad2009-09-11 12:32:01 -050030{
31 int i;
32
33 for (i = 0; i < num_tlb_entries; i++) {
34 write_tlb(tlb_table[i].mas0,
35 tlb_table[i].mas1,
36 tlb_table[i].mas2,
37 tlb_table[i].mas3,
38 tlb_table[i].mas7);
39 }
40
41 return ;
42}
43
Ying Zhangffc86e22013-08-16 15:16:10 +080044#if !defined(CONFIG_NAND_SPL) && \
45 (!defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SPL_INIT_MINIMAL))
Becky Bruce9fa52d42010-06-17 11:37:21 -050046void read_tlbcam_entry(int idx, u32 *valid, u32 *tsize, unsigned long *epn,
47 phys_addr_t *rpn)
48{
49 u32 _mas1;
50
51 mtspr(MAS0, FSL_BOOKE_MAS0(1, idx, 0));
52 asm volatile("tlbre;isync");
53 _mas1 = mfspr(MAS1);
54
55 *valid = (_mas1 & MAS1_VALID);
Scott Wood33a619c2013-01-18 15:45:58 +000056 *tsize = (_mas1 >> 7) & 0x1f;
Becky Bruce9fa52d42010-06-17 11:37:21 -050057 *epn = mfspr(MAS2) & MAS2_EPN;
58 *rpn = mfspr(MAS3) & MAS3_RPN;
59#ifdef CONFIG_ENABLE_36BIT_PHYS
60 *rpn |= ((u64)mfspr(MAS7)) << 32;
61#endif
62}
63
Becky Bruce7b9cdb42010-06-17 11:37:22 -050064void print_tlbcam(void)
65{
66 int i;
67 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
68
69 /* walk all the entries */
70 printf("TLBCAM entries\n");
71 for (i = 0; i < num_cam; i++) {
72 unsigned long epn;
73 u32 tsize, valid;
74 phys_addr_t rpn;
75
76 read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
77 printf("entry %02d: V: %d EPN 0x%08x RPN 0x%08llx size:",
78 i, (valid == 0) ? 0 : 1, (unsigned int)epn,
79 (unsigned long long)rpn);
80 print_size(TSIZE_TO_BYTES(tsize), "\n");
81 }
82}
83
Kumar Gala42f99182009-11-12 10:26:16 -060084static inline void use_tlb_cam(u8 idx)
85{
86 int i = idx / 32;
87 int bit = idx % 32;
88
Simon Glass0b466582012-12-13 20:48:52 +000089 gd->arch.used_tlb_cams[i] |= (1 << bit);
Kumar Gala42f99182009-11-12 10:26:16 -060090}
91
92static inline void free_tlb_cam(u8 idx)
93{
94 int i = idx / 32;
95 int bit = idx % 32;
96
Simon Glass0b466582012-12-13 20:48:52 +000097 gd->arch.used_tlb_cams[i] &= ~(1 << bit);
Kumar Gala42f99182009-11-12 10:26:16 -060098}
99
100void init_used_tlb_cams(void)
101{
102 int i;
103 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
104
105 for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++)
Simon Glass0b466582012-12-13 20:48:52 +0000106 gd->arch.used_tlb_cams[i] = 0;
Kumar Gala42f99182009-11-12 10:26:16 -0600107
108 /* walk all the entries */
109 for (i = 0; i < num_cam; i++) {
Kumar Gala42f99182009-11-12 10:26:16 -0600110 mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0));
Kumar Gala42f99182009-11-12 10:26:16 -0600111 asm volatile("tlbre;isync");
Becky Bruce9fa52d42010-06-17 11:37:21 -0500112 if (mfspr(MAS1) & MAS1_VALID)
Kumar Gala42f99182009-11-12 10:26:16 -0600113 use_tlb_cam(i);
114 }
115}
116
117int find_free_tlbcam(void)
118{
119 int i;
120 u32 idx;
121
122 for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++) {
Simon Glass0b466582012-12-13 20:48:52 +0000123 idx = ffz(gd->arch.used_tlb_cams[i]);
Kumar Gala42f99182009-11-12 10:26:16 -0600124
125 if (idx != 32)
126 break;
127 }
128
129 idx += i * 32;
130
131 if (idx >= CONFIG_SYS_NUM_TLBCAMS)
132 return -1;
133
134 return idx;
135}
136
Kumar Gala95bb67f2008-01-16 22:33:22 -0600137void set_tlb(u8 tlb, u32 epn, u64 rpn,
138 u8 perms, u8 wimge,
139 u8 ts, u8 esel, u8 tsize, u8 iprot)
140{
141 u32 _mas0, _mas1, _mas2, _mas3, _mas7;
142
Kumar Gala42f99182009-11-12 10:26:16 -0600143 if (tlb == 1)
144 use_tlb_cam(esel);
145
Scott Wood33a619c2013-01-18 15:45:58 +0000146 if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1 &&
147 tsize & 1) {
148 printf("%s: bad tsize %d on entry %d at 0x%08x\n",
149 __func__, tsize, tlb, epn);
150 return;
151 }
152
Kumar Gala95bb67f2008-01-16 22:33:22 -0600153 _mas0 = FSL_BOOKE_MAS0(tlb, esel, 0);
154 _mas1 = FSL_BOOKE_MAS1(1, iprot, 0, ts, tsize);
155 _mas2 = FSL_BOOKE_MAS2(epn, wimge);
156 _mas3 = FSL_BOOKE_MAS3(rpn, 0, perms);
Kumar Galac417c912009-09-11 11:27:00 -0500157 _mas7 = FSL_BOOKE_MAS7(rpn);
Kumar Gala95bb67f2008-01-16 22:33:22 -0600158
Kumar Galac417c912009-09-11 11:27:00 -0500159 write_tlb(_mas0, _mas1, _mas2, _mas3, _mas7);
Kumar Gala9ac287a2008-12-16 14:59:20 -0600160
161#ifdef CONFIG_ADDR_MAP
162 if ((tlb == 1) && (gd->flags & GD_FLG_RELOC))
Becky Bruce9fa52d42010-06-17 11:37:21 -0500163 addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), esel);
Kumar Gala9ac287a2008-12-16 14:59:20 -0600164#endif
Kumar Gala95bb67f2008-01-16 22:33:22 -0600165}
166
167void disable_tlb(u8 esel)
168{
Kumar Gala4a272472011-11-09 09:59:32 -0600169 u32 _mas0, _mas1, _mas2, _mas3;
Kumar Gala95bb67f2008-01-16 22:33:22 -0600170
Kumar Gala42f99182009-11-12 10:26:16 -0600171 free_tlb_cam(esel);
172
Kumar Gala95bb67f2008-01-16 22:33:22 -0600173 _mas0 = FSL_BOOKE_MAS0(1, esel, 0);
174 _mas1 = 0;
175 _mas2 = 0;
176 _mas3 = 0;
Kumar Gala95bb67f2008-01-16 22:33:22 -0600177
178 mtspr(MAS0, _mas0);
179 mtspr(MAS1, _mas1);
180 mtspr(MAS2, _mas2);
181 mtspr(MAS3, _mas3);
182#ifdef CONFIG_ENABLE_36BIT_PHYS
Kumar Gala4a272472011-11-09 09:59:32 -0600183 mtspr(MAS7, 0);
Kumar Gala95bb67f2008-01-16 22:33:22 -0600184#endif
185 asm volatile("isync;msync;tlbwe;isync");
Kumar Gala9ac287a2008-12-16 14:59:20 -0600186
187#ifdef CONFIG_ADDR_MAP
188 if (gd->flags & GD_FLG_RELOC)
189 addrmap_set_entry(0, 0, 0, esel);
190#endif
Kumar Gala95bb67f2008-01-16 22:33:22 -0600191}
192
Kumar Galad13eb3c2009-09-03 08:20:24 -0500193static void tlbsx (const volatile unsigned *addr)
194{
195 __asm__ __volatile__ ("tlbsx 0,%0" : : "r" (addr), "m" (*addr));
196}
197
198/* return -1 if we didn't find anything */
199int find_tlb_idx(void *addr, u8 tlbsel)
200{
201 u32 _mas0, _mas1;
202
203 /* zero out Search PID, AS */
204 mtspr(MAS6, 0);
205
206 tlbsx(addr);
207
208 _mas0 = mfspr(MAS0);
209 _mas1 = mfspr(MAS1);
210
211 /* we found something, and its in the TLB we expect */
212 if ((MAS1_VALID & _mas1) &&
213 (MAS0_TLBSEL(tlbsel) == (_mas0 & MAS0_TLBSEL_MSK))) {
214 return ((_mas0 & MAS0_ESEL_MSK) >> 16);
215 }
216
217 return -1;
218}
219
Kumar Gala9ac287a2008-12-16 14:59:20 -0600220#ifdef CONFIG_ADDR_MAP
221void init_addr_map(void)
222{
223 int i;
Kumar Gala7601fb22009-11-13 08:52:21 -0600224 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
Kumar Gala9ac287a2008-12-16 14:59:20 -0600225
Kumar Gala87f57922009-08-14 16:43:22 -0500226 /* walk all the entries */
Kumar Gala7601fb22009-11-13 08:52:21 -0600227 for (i = 0; i < num_cam; i++) {
Kumar Gala87f57922009-08-14 16:43:22 -0500228 unsigned long epn;
Becky Bruce9fa52d42010-06-17 11:37:21 -0500229 u32 tsize, valid;
Kumar Gala87f57922009-08-14 16:43:22 -0500230 phys_addr_t rpn;
231
Becky Bruce9fa52d42010-06-17 11:37:21 -0500232 read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
233 if (valid & MAS1_VALID)
234 addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), i);
Kumar Gala9ac287a2008-12-16 14:59:20 -0600235 }
236
237 return ;
238}
239#endif
240
Alexander Graf4c5d4262014-04-11 17:09:43 +0200241uint64_t tlb_map_range(ulong v_addr, phys_addr_t p_addr, uint64_t size,
242 enum tlb_map_type map_type)
Kumar Gala80f4bc72008-06-09 11:07:46 -0500243{
Kumar Gala419083b2009-11-13 09:04:19 -0600244 int i;
Kumar Gala80f4bc72008-06-09 11:07:46 -0500245 unsigned int tlb_size;
Alexander Graf4c5d4262014-04-11 17:09:43 +0200246 unsigned int wimge;
247 unsigned int perm;
Scott Wood33a619c2013-01-18 15:45:58 +0000248 unsigned int max_cam, tsize_mask;
Kumar Gala80f4bc72008-06-09 11:07:46 -0500249
Alexander Graf4c5d4262014-04-11 17:09:43 +0200250 if (map_type == TLB_MAP_RAM) {
251 perm = MAS3_SX|MAS3_SW|MAS3_SR;
252 wimge = MAS2_M;
Becky Bruce92e163f2010-12-17 17:17:55 -0600253#ifdef CONFIG_SYS_PPC_DDR_WIMGE
Alexander Graf4c5d4262014-04-11 17:09:43 +0200254 wimge = CONFIG_SYS_PPC_DDR_WIMGE;
Becky Bruce92e163f2010-12-17 17:17:55 -0600255#endif
Alexander Graf4c5d4262014-04-11 17:09:43 +0200256 } else {
257 perm = MAS3_SW|MAS3_SR;
258 wimge = MAS2_I|MAS2_G;
259 }
260
Kumar Galaac7e8952011-10-31 22:13:26 -0500261 if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1) {
262 /* Convert (4^max) kB to (2^max) bytes */
263 max_cam = ((mfspr(SPRN_TLB1CFG) >> 16) & 0xf) * 2 + 10;
Scott Wood33a619c2013-01-18 15:45:58 +0000264 tsize_mask = ~1U;
Kumar Galaac7e8952011-10-31 22:13:26 -0500265 } else {
266 /* Convert (2^max) kB to (2^max) bytes */
267 max_cam = __ilog2(mfspr(SPRN_TLB1PS)) + 10;
Scott Wood33a619c2013-01-18 15:45:58 +0000268 tsize_mask = ~0U;
Kumar Galaac7e8952011-10-31 22:13:26 -0500269 }
Kumar Gala6630ffb2009-02-06 09:56:35 -0600270
Kumar Gala419083b2009-11-13 09:04:19 -0600271 for (i = 0; size && i < 8; i++) {
Alexander Graf4c5d4262014-04-11 17:09:43 +0200272 int tlb_index = find_free_tlbcam();
Scott Wood33a619c2013-01-18 15:45:58 +0000273 u32 camsize = __ilog2_u64(size) & tsize_mask;
Alexander Graf4c5d4262014-04-11 17:09:43 +0200274 u32 align = __ilog2(v_addr) & tsize_mask;
Kumar Gala6630ffb2009-02-06 09:56:35 -0600275
Alexander Graf4c5d4262014-04-11 17:09:43 +0200276 if (tlb_index == -1)
Kumar Gala419083b2009-11-13 09:04:19 -0600277 break;
278
Kumar Gala6630ffb2009-02-06 09:56:35 -0600279 if (align == -2) align = max_cam;
280 if (camsize > align)
281 camsize = align;
Kumar Gala80f4bc72008-06-09 11:07:46 -0500282
Kumar Gala6630ffb2009-02-06 09:56:35 -0600283 if (camsize > max_cam)
284 camsize = max_cam;
285
Scott Wood33a619c2013-01-18 15:45:58 +0000286 tlb_size = camsize - 10;
Kumar Gala6630ffb2009-02-06 09:56:35 -0600287
Alexander Graf4c5d4262014-04-11 17:09:43 +0200288 set_tlb(1, v_addr, p_addr, perm, wimge,
289 0, tlb_index, tlb_size, 1);
Kumar Gala80f4bc72008-06-09 11:07:46 -0500290
Kumar Gala6630ffb2009-02-06 09:56:35 -0600291 size -= 1ULL << camsize;
Alexander Graf4c5d4262014-04-11 17:09:43 +0200292 v_addr += 1UL << camsize;
York Sunba99a332010-09-28 15:20:32 -0700293 p_addr += 1UL << camsize;
Kumar Gala80f4bc72008-06-09 11:07:46 -0500294 }
295
Alexander Graf4c5d4262014-04-11 17:09:43 +0200296 return size;
297}
298
299unsigned int setup_ddr_tlbs_phys(phys_addr_t p_addr,
300 unsigned int memsize_in_meg)
301{
302 unsigned int ram_tlb_address = (unsigned int)CONFIG_SYS_DDR_SDRAM_BASE;
303 u64 memsize = (u64)memsize_in_meg << 20;
York Sun55ec4812014-12-02 11:21:09 -0800304 u64 size;
Alexander Graf4c5d4262014-04-11 17:09:43 +0200305
York Sun55ec4812014-12-02 11:21:09 -0800306 size = min(memsize, (u64)CONFIG_MAX_MEM_MAPPED);
307 size = tlb_map_range(ram_tlb_address, p_addr, size, TLB_MAP_RAM);
Alexander Graf4c5d4262014-04-11 17:09:43 +0200308
York Sun55ec4812014-12-02 11:21:09 -0800309 if (size || memsize > CONFIG_MAX_MEM_MAPPED) {
310 print_size(memsize > CONFIG_MAX_MEM_MAPPED ?
311 memsize - CONFIG_MAX_MEM_MAPPED + size : size,
312 " left unmapped\n");
313 }
Alexander Graf4c5d4262014-04-11 17:09:43 +0200314
Kumar Gala80f4bc72008-06-09 11:07:46 -0500315 return memsize_in_meg;
316}
York Sunba99a332010-09-28 15:20:32 -0700317
318unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg)
319{
320 return
321 setup_ddr_tlbs_phys(CONFIG_SYS_DDR_SDRAM_BASE, memsize_in_meg);
322}
Becky Bruce69694472011-07-18 18:49:15 -0500323
324/* Invalidate the DDR TLBs for the requested size */
325void clear_ddr_tlbs_phys(phys_addr_t p_addr, unsigned int memsize_in_meg)
326{
327 u32 vstart = CONFIG_SYS_DDR_SDRAM_BASE;
328 unsigned long epn;
329 u32 tsize, valid, ptr;
330 phys_addr_t rpn = 0;
331 int ddr_esel;
332 u64 memsize = (u64)memsize_in_meg << 20;
333
334 ptr = vstart;
335
336 while (ptr < (vstart + memsize)) {
337 ddr_esel = find_tlb_idx((void *)ptr, 1);
338 if (ddr_esel != -1) {
339 read_tlbcam_entry(ddr_esel, &valid, &tsize, &epn, &rpn);
340 disable_tlb(ddr_esel);
341 }
342 ptr += TSIZE_TO_BYTES(tsize);
343 }
344}
345
346void clear_ddr_tlbs(unsigned int memsize_in_meg)
347{
348 clear_ddr_tlbs_phys(CONFIG_SYS_DDR_SDRAM_BASE, memsize_in_meg);
349}
350
351
Scott Wood095b7122012-09-20 19:02:18 -0500352#endif /* not SPL */