blob: 808d953c525907beecdbb9f18fd05029c4ffaab2 [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>
10#include <asm/processor.h>
11#include <asm/mmu.h>
Kumar Gala9ac287a2008-12-16 14:59:20 -060012#ifdef CONFIG_ADDR_MAP
13#include <addr_map.h>
14#endif
15
Fabio Estevam1a03a7e2015-11-05 12:43:40 -020016#include <linux/log2.h>
17
Kumar Gala9ac287a2008-12-16 14:59:20 -060018DECLARE_GLOBAL_DATA_PTR;
Kumar Gala95bb67f2008-01-16 22:33:22 -060019
Kumar Galaafc51ad2009-09-11 12:32:01 -050020void invalidate_tlb(u8 tlb)
21{
22 if (tlb == 0)
23 mtspr(MMUCSR0, 0x4);
24 if (tlb == 1)
25 mtspr(MMUCSR0, 0x2);
26}
27
Alexander Grafc3468482014-04-11 17:09:45 +020028__weak void init_tlbs(void)
Kumar Galaafc51ad2009-09-11 12:32:01 -050029{
30 int i;
31
32 for (i = 0; i < num_tlb_entries; i++) {
33 write_tlb(tlb_table[i].mas0,
34 tlb_table[i].mas1,
35 tlb_table[i].mas2,
36 tlb_table[i].mas3,
37 tlb_table[i].mas7);
38 }
39
40 return ;
41}
42
Ying Zhangffc86e22013-08-16 15:16:10 +080043#if !defined(CONFIG_NAND_SPL) && \
44 (!defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SPL_INIT_MINIMAL))
Becky Bruce9fa52d42010-06-17 11:37:21 -050045void read_tlbcam_entry(int idx, u32 *valid, u32 *tsize, unsigned long *epn,
46 phys_addr_t *rpn)
47{
48 u32 _mas1;
49
50 mtspr(MAS0, FSL_BOOKE_MAS0(1, idx, 0));
51 asm volatile("tlbre;isync");
52 _mas1 = mfspr(MAS1);
53
54 *valid = (_mas1 & MAS1_VALID);
Scott Wood33a619c2013-01-18 15:45:58 +000055 *tsize = (_mas1 >> 7) & 0x1f;
Becky Bruce9fa52d42010-06-17 11:37:21 -050056 *epn = mfspr(MAS2) & MAS2_EPN;
57 *rpn = mfspr(MAS3) & MAS3_RPN;
58#ifdef CONFIG_ENABLE_36BIT_PHYS
59 *rpn |= ((u64)mfspr(MAS7)) << 32;
60#endif
61}
62
Becky Bruce7b9cdb42010-06-17 11:37:22 -050063void print_tlbcam(void)
64{
65 int i;
66 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
67
68 /* walk all the entries */
69 printf("TLBCAM entries\n");
70 for (i = 0; i < num_cam; i++) {
71 unsigned long epn;
72 u32 tsize, valid;
73 phys_addr_t rpn;
74
75 read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
76 printf("entry %02d: V: %d EPN 0x%08x RPN 0x%08llx size:",
77 i, (valid == 0) ? 0 : 1, (unsigned int)epn,
78 (unsigned long long)rpn);
79 print_size(TSIZE_TO_BYTES(tsize), "\n");
80 }
81}
82
Kumar Gala42f99182009-11-12 10:26:16 -060083static inline void use_tlb_cam(u8 idx)
84{
85 int i = idx / 32;
86 int bit = idx % 32;
87
Simon Glass0b466582012-12-13 20:48:52 +000088 gd->arch.used_tlb_cams[i] |= (1 << bit);
Kumar Gala42f99182009-11-12 10:26:16 -060089}
90
91static inline void free_tlb_cam(u8 idx)
92{
93 int i = idx / 32;
94 int bit = idx % 32;
95
Simon Glass0b466582012-12-13 20:48:52 +000096 gd->arch.used_tlb_cams[i] &= ~(1 << bit);
Kumar Gala42f99182009-11-12 10:26:16 -060097}
98
99void init_used_tlb_cams(void)
100{
101 int i;
102 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
103
104 for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++)
Simon Glass0b466582012-12-13 20:48:52 +0000105 gd->arch.used_tlb_cams[i] = 0;
Kumar Gala42f99182009-11-12 10:26:16 -0600106
107 /* walk all the entries */
108 for (i = 0; i < num_cam; i++) {
Kumar Gala42f99182009-11-12 10:26:16 -0600109 mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0));
Kumar Gala42f99182009-11-12 10:26:16 -0600110 asm volatile("tlbre;isync");
Becky Bruce9fa52d42010-06-17 11:37:21 -0500111 if (mfspr(MAS1) & MAS1_VALID)
Kumar Gala42f99182009-11-12 10:26:16 -0600112 use_tlb_cam(i);
113 }
114}
115
116int find_free_tlbcam(void)
117{
118 int i;
119 u32 idx;
120
121 for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++) {
Simon Glass0b466582012-12-13 20:48:52 +0000122 idx = ffz(gd->arch.used_tlb_cams[i]);
Kumar Gala42f99182009-11-12 10:26:16 -0600123
124 if (idx != 32)
125 break;
126 }
127
128 idx += i * 32;
129
130 if (idx >= CONFIG_SYS_NUM_TLBCAMS)
131 return -1;
132
133 return idx;
134}
135
Kumar Gala95bb67f2008-01-16 22:33:22 -0600136void set_tlb(u8 tlb, u32 epn, u64 rpn,
137 u8 perms, u8 wimge,
138 u8 ts, u8 esel, u8 tsize, u8 iprot)
139{
140 u32 _mas0, _mas1, _mas2, _mas3, _mas7;
141
Kumar Gala42f99182009-11-12 10:26:16 -0600142 if (tlb == 1)
143 use_tlb_cam(esel);
144
Scott Wood33a619c2013-01-18 15:45:58 +0000145 if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1 &&
146 tsize & 1) {
147 printf("%s: bad tsize %d on entry %d at 0x%08x\n",
148 __func__, tsize, tlb, epn);
149 return;
150 }
151
Kumar Gala95bb67f2008-01-16 22:33:22 -0600152 _mas0 = FSL_BOOKE_MAS0(tlb, esel, 0);
153 _mas1 = FSL_BOOKE_MAS1(1, iprot, 0, ts, tsize);
154 _mas2 = FSL_BOOKE_MAS2(epn, wimge);
155 _mas3 = FSL_BOOKE_MAS3(rpn, 0, perms);
Kumar Galac417c912009-09-11 11:27:00 -0500156 _mas7 = FSL_BOOKE_MAS7(rpn);
Kumar Gala95bb67f2008-01-16 22:33:22 -0600157
Kumar Galac417c912009-09-11 11:27:00 -0500158 write_tlb(_mas0, _mas1, _mas2, _mas3, _mas7);
Kumar Gala9ac287a2008-12-16 14:59:20 -0600159
160#ifdef CONFIG_ADDR_MAP
161 if ((tlb == 1) && (gd->flags & GD_FLG_RELOC))
Becky Bruce9fa52d42010-06-17 11:37:21 -0500162 addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), esel);
Kumar Gala9ac287a2008-12-16 14:59:20 -0600163#endif
Kumar Gala95bb67f2008-01-16 22:33:22 -0600164}
165
166void disable_tlb(u8 esel)
167{
Kumar Gala4a272472011-11-09 09:59:32 -0600168 u32 _mas0, _mas1, _mas2, _mas3;
Kumar Gala95bb67f2008-01-16 22:33:22 -0600169
Kumar Gala42f99182009-11-12 10:26:16 -0600170 free_tlb_cam(esel);
171
Kumar Gala95bb67f2008-01-16 22:33:22 -0600172 _mas0 = FSL_BOOKE_MAS0(1, esel, 0);
173 _mas1 = 0;
174 _mas2 = 0;
175 _mas3 = 0;
Kumar Gala95bb67f2008-01-16 22:33:22 -0600176
177 mtspr(MAS0, _mas0);
178 mtspr(MAS1, _mas1);
179 mtspr(MAS2, _mas2);
180 mtspr(MAS3, _mas3);
181#ifdef CONFIG_ENABLE_36BIT_PHYS
Kumar Gala4a272472011-11-09 09:59:32 -0600182 mtspr(MAS7, 0);
Kumar Gala95bb67f2008-01-16 22:33:22 -0600183#endif
184 asm volatile("isync;msync;tlbwe;isync");
Kumar Gala9ac287a2008-12-16 14:59:20 -0600185
186#ifdef CONFIG_ADDR_MAP
187 if (gd->flags & GD_FLG_RELOC)
188 addrmap_set_entry(0, 0, 0, esel);
189#endif
Kumar Gala95bb67f2008-01-16 22:33:22 -0600190}
191
Kumar Galad13eb3c2009-09-03 08:20:24 -0500192static void tlbsx (const volatile unsigned *addr)
193{
194 __asm__ __volatile__ ("tlbsx 0,%0" : : "r" (addr), "m" (*addr));
195}
196
197/* return -1 if we didn't find anything */
198int find_tlb_idx(void *addr, u8 tlbsel)
199{
200 u32 _mas0, _mas1;
201
202 /* zero out Search PID, AS */
203 mtspr(MAS6, 0);
204
205 tlbsx(addr);
206
207 _mas0 = mfspr(MAS0);
208 _mas1 = mfspr(MAS1);
209
210 /* we found something, and its in the TLB we expect */
211 if ((MAS1_VALID & _mas1) &&
212 (MAS0_TLBSEL(tlbsel) == (_mas0 & MAS0_TLBSEL_MSK))) {
213 return ((_mas0 & MAS0_ESEL_MSK) >> 16);
214 }
215
216 return -1;
217}
218
Kumar Gala9ac287a2008-12-16 14:59:20 -0600219#ifdef CONFIG_ADDR_MAP
220void init_addr_map(void)
221{
222 int i;
Kumar Gala7601fb22009-11-13 08:52:21 -0600223 unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff;
Kumar Gala9ac287a2008-12-16 14:59:20 -0600224
Kumar Gala87f57922009-08-14 16:43:22 -0500225 /* walk all the entries */
Kumar Gala7601fb22009-11-13 08:52:21 -0600226 for (i = 0; i < num_cam; i++) {
Kumar Gala87f57922009-08-14 16:43:22 -0500227 unsigned long epn;
Becky Bruce9fa52d42010-06-17 11:37:21 -0500228 u32 tsize, valid;
Kumar Gala87f57922009-08-14 16:43:22 -0500229 phys_addr_t rpn;
230
Becky Bruce9fa52d42010-06-17 11:37:21 -0500231 read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn);
232 if (valid & MAS1_VALID)
233 addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), i);
Kumar Gala9ac287a2008-12-16 14:59:20 -0600234 }
235
236 return ;
237}
238#endif
239
Alexander Graf4c5d4262014-04-11 17:09:43 +0200240uint64_t tlb_map_range(ulong v_addr, phys_addr_t p_addr, uint64_t size,
241 enum tlb_map_type map_type)
Kumar Gala80f4bc72008-06-09 11:07:46 -0500242{
Kumar Gala419083b2009-11-13 09:04:19 -0600243 int i;
Kumar Gala80f4bc72008-06-09 11:07:46 -0500244 unsigned int tlb_size;
Alexander Graf4c5d4262014-04-11 17:09:43 +0200245 unsigned int wimge;
246 unsigned int perm;
Scott Wood33a619c2013-01-18 15:45:58 +0000247 unsigned int max_cam, tsize_mask;
Kumar Gala80f4bc72008-06-09 11:07:46 -0500248
Alexander Graf4c5d4262014-04-11 17:09:43 +0200249 if (map_type == TLB_MAP_RAM) {
250 perm = MAS3_SX|MAS3_SW|MAS3_SR;
251 wimge = MAS2_M;
Becky Bruce92e163f2010-12-17 17:17:55 -0600252#ifdef CONFIG_SYS_PPC_DDR_WIMGE
Alexander Graf4c5d4262014-04-11 17:09:43 +0200253 wimge = CONFIG_SYS_PPC_DDR_WIMGE;
Becky Bruce92e163f2010-12-17 17:17:55 -0600254#endif
Alexander Graf4c5d4262014-04-11 17:09:43 +0200255 } else {
256 perm = MAS3_SW|MAS3_SR;
257 wimge = MAS2_I|MAS2_G;
258 }
259
Kumar Galaac7e8952011-10-31 22:13:26 -0500260 if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1) {
261 /* Convert (4^max) kB to (2^max) bytes */
262 max_cam = ((mfspr(SPRN_TLB1CFG) >> 16) & 0xf) * 2 + 10;
Scott Wood33a619c2013-01-18 15:45:58 +0000263 tsize_mask = ~1U;
Kumar Galaac7e8952011-10-31 22:13:26 -0500264 } else {
265 /* Convert (2^max) kB to (2^max) bytes */
266 max_cam = __ilog2(mfspr(SPRN_TLB1PS)) + 10;
Scott Wood33a619c2013-01-18 15:45:58 +0000267 tsize_mask = ~0U;
Kumar Galaac7e8952011-10-31 22:13:26 -0500268 }
Kumar Gala6630ffb2009-02-06 09:56:35 -0600269
Kumar Gala419083b2009-11-13 09:04:19 -0600270 for (i = 0; size && i < 8; i++) {
Alexander Graf4c5d4262014-04-11 17:09:43 +0200271 int tlb_index = find_free_tlbcam();
Scott Wood33a619c2013-01-18 15:45:58 +0000272 u32 camsize = __ilog2_u64(size) & tsize_mask;
Alexander Graf4c5d4262014-04-11 17:09:43 +0200273 u32 align = __ilog2(v_addr) & tsize_mask;
Kumar Gala6630ffb2009-02-06 09:56:35 -0600274
Alexander Graf4c5d4262014-04-11 17:09:43 +0200275 if (tlb_index == -1)
Kumar Gala419083b2009-11-13 09:04:19 -0600276 break;
277
Kumar Gala6630ffb2009-02-06 09:56:35 -0600278 if (align == -2) align = max_cam;
279 if (camsize > align)
280 camsize = align;
Kumar Gala80f4bc72008-06-09 11:07:46 -0500281
Kumar Gala6630ffb2009-02-06 09:56:35 -0600282 if (camsize > max_cam)
283 camsize = max_cam;
284
Scott Wood33a619c2013-01-18 15:45:58 +0000285 tlb_size = camsize - 10;
Kumar Gala6630ffb2009-02-06 09:56:35 -0600286
Alexander Graf4c5d4262014-04-11 17:09:43 +0200287 set_tlb(1, v_addr, p_addr, perm, wimge,
288 0, tlb_index, tlb_size, 1);
Kumar Gala80f4bc72008-06-09 11:07:46 -0500289
Kumar Gala6630ffb2009-02-06 09:56:35 -0600290 size -= 1ULL << camsize;
Alexander Graf4c5d4262014-04-11 17:09:43 +0200291 v_addr += 1UL << camsize;
York Sunba99a332010-09-28 15:20:32 -0700292 p_addr += 1UL << camsize;
Kumar Gala80f4bc72008-06-09 11:07:46 -0500293 }
294
Alexander Graf4c5d4262014-04-11 17:09:43 +0200295 return size;
296}
297
298unsigned int setup_ddr_tlbs_phys(phys_addr_t p_addr,
299 unsigned int memsize_in_meg)
300{
301 unsigned int ram_tlb_address = (unsigned int)CONFIG_SYS_DDR_SDRAM_BASE;
302 u64 memsize = (u64)memsize_in_meg << 20;
York Sun55ec4812014-12-02 11:21:09 -0800303 u64 size;
Alexander Graf4c5d4262014-04-11 17:09:43 +0200304
York Sun55ec4812014-12-02 11:21:09 -0800305 size = min(memsize, (u64)CONFIG_MAX_MEM_MAPPED);
306 size = tlb_map_range(ram_tlb_address, p_addr, size, TLB_MAP_RAM);
Alexander Graf4c5d4262014-04-11 17:09:43 +0200307
York Sun55ec4812014-12-02 11:21:09 -0800308 if (size || memsize > CONFIG_MAX_MEM_MAPPED) {
309 print_size(memsize > CONFIG_MAX_MEM_MAPPED ?
310 memsize - CONFIG_MAX_MEM_MAPPED + size : size,
311 " left unmapped\n");
312 }
Alexander Graf4c5d4262014-04-11 17:09:43 +0200313
Kumar Gala80f4bc72008-06-09 11:07:46 -0500314 return memsize_in_meg;
315}
York Sunba99a332010-09-28 15:20:32 -0700316
317unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg)
318{
319 return
320 setup_ddr_tlbs_phys(CONFIG_SYS_DDR_SDRAM_BASE, memsize_in_meg);
321}
Becky Bruce69694472011-07-18 18:49:15 -0500322
323/* Invalidate the DDR TLBs for the requested size */
324void clear_ddr_tlbs_phys(phys_addr_t p_addr, unsigned int memsize_in_meg)
325{
326 u32 vstart = CONFIG_SYS_DDR_SDRAM_BASE;
327 unsigned long epn;
328 u32 tsize, valid, ptr;
329 phys_addr_t rpn = 0;
330 int ddr_esel;
331 u64 memsize = (u64)memsize_in_meg << 20;
332
333 ptr = vstart;
334
335 while (ptr < (vstart + memsize)) {
336 ddr_esel = find_tlb_idx((void *)ptr, 1);
337 if (ddr_esel != -1) {
338 read_tlbcam_entry(ddr_esel, &valid, &tsize, &epn, &rpn);
339 disable_tlb(ddr_esel);
340 }
341 ptr += TSIZE_TO_BYTES(tsize);
342 }
343}
344
345void clear_ddr_tlbs(unsigned int memsize_in_meg)
346{
347 clear_ddr_tlbs_phys(CONFIG_SYS_DDR_SDRAM_BASE, memsize_in_meg);
348}
349
350
Scott Wood095b7122012-09-20 19:02:18 -0500351#endif /* not SPL */