blob: aa9b59d4871170a7b6d25c0a06a85b64c0a1ba16 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Gala95bb67f2008-01-16 22:33:22 -06002/*
Kumar Galad7ff6a82011-02-03 20:21:42 -06003 * Copyright 2008-2011 Freescale Semiconductor, Inc.
Kumar Gala95bb67f2008-01-16 22:33:22 -06004 *
5 * (C) Copyright 2000
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Kumar Gala95bb67f2008-01-16 22:33:22 -06007 */
8
9#include <common.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060010#include <asm/bitops.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Kumar Gala95bb67f2008-01-16 22:33:22 -060012#include <asm/processor.h>
13#include <asm/mmu.h>
Kumar Gala9ac287a2008-12-16 14:59:20 -060014#ifdef CONFIG_ADDR_MAP
15#include <addr_map.h>
16#endif
17
Fabio Estevam1a03a7e2015-11-05 12:43:40 -020018#include <linux/log2.h>
19
Kumar Gala9ac287a2008-12-16 14:59:20 -060020DECLARE_GLOBAL_DATA_PTR;
Kumar Gala95bb67f2008-01-16 22:33:22 -060021
Kumar Galaafc51ad2009-09-11 12:32:01 -050022void invalidate_tlb(u8 tlb)
23{
24 if (tlb == 0)
25 mtspr(MMUCSR0, 0x4);
26 if (tlb == 1)
27 mtspr(MMUCSR0, 0x2);
28}
29
Alexander Grafc3468482014-04-11 17:09:45 +020030__weak void init_tlbs(void)
Kumar Galaafc51ad2009-09-11 12:32:01 -050031{
32 int i;
33
34 for (i = 0; i < num_tlb_entries; i++) {
35 write_tlb(tlb_table[i].mas0,
36 tlb_table[i].mas1,
37 tlb_table[i].mas2,
38 tlb_table[i].mas3,
39 tlb_table[i].mas7);
40 }
41
42 return ;
43}
44
Ying Zhangffc86e22013-08-16 15:16:10 +080045#if !defined(CONFIG_NAND_SPL) && \
46 (!defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SPL_INIT_MINIMAL))
Becky Bruce9fa52d42010-06-17 11:37:21 -050047void read_tlbcam_entry(int idx, u32 *valid, u32 *tsize, unsigned long *epn,
48 phys_addr_t *rpn)
49{
50 u32 _mas1;
51
52 mtspr(MAS0, FSL_BOOKE_MAS0(1, idx, 0));
53 asm volatile("tlbre;isync");
54 _mas1 = mfspr(MAS1);
55
56 *valid = (_mas1 & MAS1_VALID);
Scott Wood33a619c2013-01-18 15:45:58 +000057 *tsize = (_mas1 >> 7) & 0x1f;
Becky Bruce9fa52d42010-06-17 11:37:21 -050058 *epn = mfspr(MAS2) & MAS2_EPN;
59 *rpn = mfspr(MAS3) & MAS3_RPN;
60#ifdef CONFIG_ENABLE_36BIT_PHYS
61 *rpn |= ((u64)mfspr(MAS7)) << 32;
62#endif
63}
64
Becky Bruce7b9cdb42010-06-17 11:37:22 -050065void print_tlbcam(void)
66{
67 int i;
68 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
69
70 /* walk all the entries */
71 printf("TLBCAM entries\n");
72 for (i = 0; i < num_cam; i++) {
73 unsigned long epn;
74 u32 tsize, valid;
75 phys_addr_t rpn;
76
77 read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
78 printf("entry %02d: V: %d EPN 0x%08x RPN 0x%08llx size:",
79 i, (valid == 0) ? 0 : 1, (unsigned int)epn,
80 (unsigned long long)rpn);
81 print_size(TSIZE_TO_BYTES(tsize), "\n");
82 }
83}
84
Kumar Gala42f99182009-11-12 10:26:16 -060085static inline void use_tlb_cam(u8 idx)
86{
87 int i = idx / 32;
88 int bit = idx % 32;
89
Simon Glass0b466582012-12-13 20:48:52 +000090 gd->arch.used_tlb_cams[i] |= (1 << bit);
Kumar Gala42f99182009-11-12 10:26:16 -060091}
92
93static inline void free_tlb_cam(u8 idx)
94{
95 int i = idx / 32;
96 int bit = idx % 32;
97
Simon Glass0b466582012-12-13 20:48:52 +000098 gd->arch.used_tlb_cams[i] &= ~(1 << bit);
Kumar Gala42f99182009-11-12 10:26:16 -060099}
100
101void init_used_tlb_cams(void)
102{
103 int i;
104 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
105
106 for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++)
Simon Glass0b466582012-12-13 20:48:52 +0000107 gd->arch.used_tlb_cams[i] = 0;
Kumar Gala42f99182009-11-12 10:26:16 -0600108
109 /* walk all the entries */
110 for (i = 0; i < num_cam; i++) {
Kumar Gala42f99182009-11-12 10:26:16 -0600111 mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0));
Kumar Gala42f99182009-11-12 10:26:16 -0600112 asm volatile("tlbre;isync");
Becky Bruce9fa52d42010-06-17 11:37:21 -0500113 if (mfspr(MAS1) & MAS1_VALID)
Kumar Gala42f99182009-11-12 10:26:16 -0600114 use_tlb_cam(i);
115 }
116}
117
118int find_free_tlbcam(void)
119{
120 int i;
121 u32 idx;
122
123 for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++) {
Simon Glass0b466582012-12-13 20:48:52 +0000124 idx = ffz(gd->arch.used_tlb_cams[i]);
Kumar Gala42f99182009-11-12 10:26:16 -0600125
126 if (idx != 32)
127 break;
128 }
129
130 idx += i * 32;
131
132 if (idx >= CONFIG_SYS_NUM_TLBCAMS)
133 return -1;
134
135 return idx;
136}
137
Kumar Gala95bb67f2008-01-16 22:33:22 -0600138void set_tlb(u8 tlb, u32 epn, u64 rpn,
139 u8 perms, u8 wimge,
140 u8 ts, u8 esel, u8 tsize, u8 iprot)
141{
142 u32 _mas0, _mas1, _mas2, _mas3, _mas7;
143
Kumar Gala42f99182009-11-12 10:26:16 -0600144 if (tlb == 1)
145 use_tlb_cam(esel);
146
Scott Wood33a619c2013-01-18 15:45:58 +0000147 if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1 &&
148 tsize & 1) {
149 printf("%s: bad tsize %d on entry %d at 0x%08x\n",
150 __func__, tsize, tlb, epn);
151 return;
152 }
153
Kumar Gala95bb67f2008-01-16 22:33:22 -0600154 _mas0 = FSL_BOOKE_MAS0(tlb, esel, 0);
155 _mas1 = FSL_BOOKE_MAS1(1, iprot, 0, ts, tsize);
156 _mas2 = FSL_BOOKE_MAS2(epn, wimge);
157 _mas3 = FSL_BOOKE_MAS3(rpn, 0, perms);
Kumar Galac417c912009-09-11 11:27:00 -0500158 _mas7 = FSL_BOOKE_MAS7(rpn);
Kumar Gala95bb67f2008-01-16 22:33:22 -0600159
Kumar Galac417c912009-09-11 11:27:00 -0500160 write_tlb(_mas0, _mas1, _mas2, _mas3, _mas7);
Kumar Gala9ac287a2008-12-16 14:59:20 -0600161
162#ifdef CONFIG_ADDR_MAP
163 if ((tlb == 1) && (gd->flags & GD_FLG_RELOC))
Becky Bruce9fa52d42010-06-17 11:37:21 -0500164 addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), esel);
Kumar Gala9ac287a2008-12-16 14:59:20 -0600165#endif
Kumar Gala95bb67f2008-01-16 22:33:22 -0600166}
167
168void disable_tlb(u8 esel)
169{
Kumar Gala4a272472011-11-09 09:59:32 -0600170 u32 _mas0, _mas1, _mas2, _mas3;
Kumar Gala95bb67f2008-01-16 22:33:22 -0600171
Kumar Gala42f99182009-11-12 10:26:16 -0600172 free_tlb_cam(esel);
173
Kumar Gala95bb67f2008-01-16 22:33:22 -0600174 _mas0 = FSL_BOOKE_MAS0(1, esel, 0);
175 _mas1 = 0;
176 _mas2 = 0;
177 _mas3 = 0;
Kumar Gala95bb67f2008-01-16 22:33:22 -0600178
179 mtspr(MAS0, _mas0);
180 mtspr(MAS1, _mas1);
181 mtspr(MAS2, _mas2);
182 mtspr(MAS3, _mas3);
183#ifdef CONFIG_ENABLE_36BIT_PHYS
Kumar Gala4a272472011-11-09 09:59:32 -0600184 mtspr(MAS7, 0);
Kumar Gala95bb67f2008-01-16 22:33:22 -0600185#endif
186 asm volatile("isync;msync;tlbwe;isync");
Kumar Gala9ac287a2008-12-16 14:59:20 -0600187
188#ifdef CONFIG_ADDR_MAP
189 if (gd->flags & GD_FLG_RELOC)
190 addrmap_set_entry(0, 0, 0, esel);
191#endif
Kumar Gala95bb67f2008-01-16 22:33:22 -0600192}
193
Kumar Galad13eb3c2009-09-03 08:20:24 -0500194static void tlbsx (const volatile unsigned *addr)
195{
196 __asm__ __volatile__ ("tlbsx 0,%0" : : "r" (addr), "m" (*addr));
197}
198
199/* return -1 if we didn't find anything */
200int find_tlb_idx(void *addr, u8 tlbsel)
201{
202 u32 _mas0, _mas1;
203
204 /* zero out Search PID, AS */
205 mtspr(MAS6, 0);
206
207 tlbsx(addr);
208
209 _mas0 = mfspr(MAS0);
210 _mas1 = mfspr(MAS1);
211
212 /* we found something, and its in the TLB we expect */
213 if ((MAS1_VALID & _mas1) &&
214 (MAS0_TLBSEL(tlbsel) == (_mas0 & MAS0_TLBSEL_MSK))) {
215 return ((_mas0 & MAS0_ESEL_MSK) >> 16);
216 }
217
218 return -1;
219}
220
Kumar Gala9ac287a2008-12-16 14:59:20 -0600221#ifdef CONFIG_ADDR_MAP
Ovidiu Panait9ef3c8a2022-01-01 19:13:28 +0200222int init_addr_map(void)
Kumar Gala9ac287a2008-12-16 14:59:20 -0600223{
224 int i;
Kumar Gala7601fb22009-11-13 08:52:21 -0600225 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
Kumar Gala9ac287a2008-12-16 14:59:20 -0600226
Kumar Gala87f57922009-08-14 16:43:22 -0500227 /* walk all the entries */
Kumar Gala7601fb22009-11-13 08:52:21 -0600228 for (i = 0; i < num_cam; i++) {
Kumar Gala87f57922009-08-14 16:43:22 -0500229 unsigned long epn;
Becky Bruce9fa52d42010-06-17 11:37:21 -0500230 u32 tsize, valid;
Kumar Gala87f57922009-08-14 16:43:22 -0500231 phys_addr_t rpn;
232
Becky Bruce9fa52d42010-06-17 11:37:21 -0500233 read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
234 if (valid & MAS1_VALID)
235 addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), i);
Kumar Gala9ac287a2008-12-16 14:59:20 -0600236 }
237
Ovidiu Panait9ef3c8a2022-01-01 19:13:28 +0200238 return 0;
Kumar Gala9ac287a2008-12-16 14:59:20 -0600239}
240#endif
241
Alexander Graf4c5d4262014-04-11 17:09:43 +0200242uint64_t tlb_map_range(ulong v_addr, phys_addr_t p_addr, uint64_t size,
243 enum tlb_map_type map_type)
Kumar Gala80f4bc72008-06-09 11:07:46 -0500244{
Kumar Gala419083b2009-11-13 09:04:19 -0600245 int i;
Kumar Gala80f4bc72008-06-09 11:07:46 -0500246 unsigned int tlb_size;
Alexander Graf4c5d4262014-04-11 17:09:43 +0200247 unsigned int wimge;
248 unsigned int perm;
Scott Wood33a619c2013-01-18 15:45:58 +0000249 unsigned int max_cam, tsize_mask;
Kumar Gala80f4bc72008-06-09 11:07:46 -0500250
Alexander Graf4c5d4262014-04-11 17:09:43 +0200251 if (map_type == TLB_MAP_RAM) {
252 perm = MAS3_SX|MAS3_SW|MAS3_SR;
253 wimge = MAS2_M;
Becky Bruce92e163f2010-12-17 17:17:55 -0600254#ifdef CONFIG_SYS_PPC_DDR_WIMGE
Alexander Graf4c5d4262014-04-11 17:09:43 +0200255 wimge = CONFIG_SYS_PPC_DDR_WIMGE;
Becky Bruce92e163f2010-12-17 17:17:55 -0600256#endif
Alexander Graf4c5d4262014-04-11 17:09:43 +0200257 } else {
258 perm = MAS3_SW|MAS3_SR;
259 wimge = MAS2_I|MAS2_G;
260 }
261
Kumar Galaac7e8952011-10-31 22:13:26 -0500262 if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1) {
263 /* Convert (4^max) kB to (2^max) bytes */
264 max_cam = ((mfspr(SPRN_TLB1CFG) >> 16) & 0xf) * 2 + 10;
Scott Wood33a619c2013-01-18 15:45:58 +0000265 tsize_mask = ~1U;
Kumar Galaac7e8952011-10-31 22:13:26 -0500266 } else {
267 /* Convert (2^max) kB to (2^max) bytes */
268 max_cam = __ilog2(mfspr(SPRN_TLB1PS)) + 10;
Scott Wood33a619c2013-01-18 15:45:58 +0000269 tsize_mask = ~0U;
Kumar Galaac7e8952011-10-31 22:13:26 -0500270 }
Kumar Gala6630ffb2009-02-06 09:56:35 -0600271
Kumar Gala419083b2009-11-13 09:04:19 -0600272 for (i = 0; size && i < 8; i++) {
Alexander Graf4c5d4262014-04-11 17:09:43 +0200273 int tlb_index = find_free_tlbcam();
Scott Wood33a619c2013-01-18 15:45:58 +0000274 u32 camsize = __ilog2_u64(size) & tsize_mask;
Alexander Graf4c5d4262014-04-11 17:09:43 +0200275 u32 align = __ilog2(v_addr) & tsize_mask;
Kumar Gala6630ffb2009-02-06 09:56:35 -0600276
Alexander Graf4c5d4262014-04-11 17:09:43 +0200277 if (tlb_index == -1)
Kumar Gala419083b2009-11-13 09:04:19 -0600278 break;
279
Kumar Gala6630ffb2009-02-06 09:56:35 -0600280 if (align == -2) align = max_cam;
281 if (camsize > align)
282 camsize = align;
Kumar Gala80f4bc72008-06-09 11:07:46 -0500283
Kumar Gala6630ffb2009-02-06 09:56:35 -0600284 if (camsize > max_cam)
285 camsize = max_cam;
286
Scott Wood33a619c2013-01-18 15:45:58 +0000287 tlb_size = camsize - 10;
Kumar Gala6630ffb2009-02-06 09:56:35 -0600288
Alexander Graf4c5d4262014-04-11 17:09:43 +0200289 set_tlb(1, v_addr, p_addr, perm, wimge,
290 0, tlb_index, tlb_size, 1);
Kumar Gala80f4bc72008-06-09 11:07:46 -0500291
Kumar Gala6630ffb2009-02-06 09:56:35 -0600292 size -= 1ULL << camsize;
Alexander Graf4c5d4262014-04-11 17:09:43 +0200293 v_addr += 1UL << camsize;
York Sunba99a332010-09-28 15:20:32 -0700294 p_addr += 1UL << camsize;
Kumar Gala80f4bc72008-06-09 11:07:46 -0500295 }
296
Alexander Graf4c5d4262014-04-11 17:09:43 +0200297 return size;
298}
299
300unsigned int setup_ddr_tlbs_phys(phys_addr_t p_addr,
301 unsigned int memsize_in_meg)
302{
303 unsigned int ram_tlb_address = (unsigned int)CONFIG_SYS_DDR_SDRAM_BASE;
304 u64 memsize = (u64)memsize_in_meg << 20;
York Sun55ec4812014-12-02 11:21:09 -0800305 u64 size;
Alexander Graf4c5d4262014-04-11 17:09:43 +0200306
York Sun55ec4812014-12-02 11:21:09 -0800307 size = min(memsize, (u64)CONFIG_MAX_MEM_MAPPED);
308 size = tlb_map_range(ram_tlb_address, p_addr, size, TLB_MAP_RAM);
Alexander Graf4c5d4262014-04-11 17:09:43 +0200309
York Sun55ec4812014-12-02 11:21:09 -0800310 if (size || memsize > CONFIG_MAX_MEM_MAPPED) {
311 print_size(memsize > CONFIG_MAX_MEM_MAPPED ?
312 memsize - CONFIG_MAX_MEM_MAPPED + size : size,
313 " left unmapped\n");
314 }
Alexander Graf4c5d4262014-04-11 17:09:43 +0200315
Kumar Gala80f4bc72008-06-09 11:07:46 -0500316 return memsize_in_meg;
317}
York Sunba99a332010-09-28 15:20:32 -0700318
319unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg)
320{
321 return
322 setup_ddr_tlbs_phys(CONFIG_SYS_DDR_SDRAM_BASE, memsize_in_meg);
323}
Becky Bruce69694472011-07-18 18:49:15 -0500324
325/* Invalidate the DDR TLBs for the requested size */
326void clear_ddr_tlbs_phys(phys_addr_t p_addr, unsigned int memsize_in_meg)
327{
328 u32 vstart = CONFIG_SYS_DDR_SDRAM_BASE;
329 unsigned long epn;
330 u32 tsize, valid, ptr;
331 phys_addr_t rpn = 0;
332 int ddr_esel;
333 u64 memsize = (u64)memsize_in_meg << 20;
334
335 ptr = vstart;
336
337 while (ptr < (vstart + memsize)) {
338 ddr_esel = find_tlb_idx((void *)ptr, 1);
339 if (ddr_esel != -1) {
340 read_tlbcam_entry(ddr_esel, &valid, &tsize, &epn, &rpn);
341 disable_tlb(ddr_esel);
342 }
343 ptr += TSIZE_TO_BYTES(tsize);
344 }
345}
346
347void clear_ddr_tlbs(unsigned int memsize_in_meg)
348{
349 clear_ddr_tlbs_phys(CONFIG_SYS_DDR_SDRAM_BASE, memsize_in_meg);
350}
351
352
Scott Wood095b7122012-09-20 19:02:18 -0500353#endif /* not SPL */