blob: 3cb09bda4308213342d99ef7d0106426a014d86a [file] [log] [blame]
Stefan Roese4037ed32007-02-20 10:43:34 +01001/*
2 * (C) Copyright 2007
3 * Stefan Roese, DENX Software Engineering, sr@denx.de.
4 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02005 * SPDX-License-Identifier: GPL-2.0+
Stefan Roese4037ed32007-02-20 10:43:34 +01006 */
7
8#include <common.h>
9
10#if defined(CONFIG_440)
11
Stefan Roeseb36df562010-09-09 19:18:00 +020012#include <asm/ppc440.h>
Stefan Roese483e09a2007-10-31 17:59:22 +010013#include <asm/cache.h>
Stefan Roese4037ed32007-02-20 10:43:34 +010014#include <asm/io.h>
15#include <asm/mmu.h>
16
17typedef struct region {
Stefan Roese84a999b2008-02-19 22:01:57 +010018 u64 base;
19 u32 size;
20 u32 tlb_word2_i_value;
Stefan Roese4037ed32007-02-20 10:43:34 +010021} region_t;
22
Stefan Roese5743a922007-07-16 08:53:51 +020023void remove_tlb(u32 vaddr, u32 size)
24{
25 int i;
26 u32 tlb_word0_value;
27 u32 tlb_vaddr;
28 u32 tlb_size = 0;
29
Stefan Roese5743a922007-07-16 08:53:51 +020030 for (i=0; i<PPC4XX_TLB_SIZE; i++) {
31 tlb_word0_value = mftlb1(i);
32 tlb_vaddr = TLB_WORD0_EPN_DECODE(tlb_word0_value);
33 if (((tlb_word0_value & TLB_WORD0_V_MASK) == TLB_WORD0_V_ENABLE) &&
34 (tlb_vaddr >= vaddr)) {
35 /*
36 * TLB is enabled and start address is lower or equal
37 * than the area we are looking for. Now we only have
38 * to check the size/end address for a match.
39 */
40 switch (tlb_word0_value & TLB_WORD0_SIZE_MASK) {
41 case TLB_WORD0_SIZE_1KB:
42 tlb_size = 1 << 10;
43 break;
44 case TLB_WORD0_SIZE_4KB:
45 tlb_size = 4 << 10;
46 break;
47 case TLB_WORD0_SIZE_16KB:
48 tlb_size = 16 << 10;
49 break;
50 case TLB_WORD0_SIZE_64KB:
51 tlb_size = 64 << 10;
52 break;
53 case TLB_WORD0_SIZE_256KB:
54 tlb_size = 256 << 10;
55 break;
56 case TLB_WORD0_SIZE_1MB:
57 tlb_size = 1 << 20;
58 break;
59 case TLB_WORD0_SIZE_16MB:
60 tlb_size = 16 << 20;
61 break;
62 case TLB_WORD0_SIZE_256MB:
63 tlb_size = 256 << 20;
64 break;
65 }
66
67 /*
68 * Now check the end-address if it's in the range
69 */
70 if ((tlb_vaddr + tlb_size - 1) <= (vaddr + size - 1))
71 /*
72 * Found a TLB in the range.
73 * Disable it by writing 0 to tlb0 word.
74 */
75 mttlb1(i, 0);
76 }
77 }
78
79 /* Execute an ISYNC instruction so that the new TLB entry takes effect */
80 asm("isync");
81}
82
Stefan Roese483e09a2007-10-31 17:59:22 +010083/*
84 * Change the I attribute (cache inhibited) of a TLB or multiple TLB's.
85 * This function is used to either turn cache on or off in a specific
86 * memory area.
87 */
88void change_tlb(u32 vaddr, u32 size, u32 tlb_word2_i_value)
89{
90 int i;
91 u32 tlb_word0_value;
92 u32 tlb_word2_value;
93 u32 tlb_vaddr;
94 u32 tlb_size = 0;
95
96 for (i=0; i<PPC4XX_TLB_SIZE; i++) {
97 tlb_word0_value = mftlb1(i);
98 tlb_vaddr = TLB_WORD0_EPN_DECODE(tlb_word0_value);
99 if (((tlb_word0_value & TLB_WORD0_V_MASK) == TLB_WORD0_V_ENABLE) &&
100 (tlb_vaddr >= vaddr)) {
101 /*
102 * TLB is enabled and start address is lower or equal
103 * than the area we are looking for. Now we only have
104 * to check the size/end address for a match.
105 */
106 switch (tlb_word0_value & TLB_WORD0_SIZE_MASK) {
107 case TLB_WORD0_SIZE_1KB:
108 tlb_size = 1 << 10;
109 break;
110 case TLB_WORD0_SIZE_4KB:
111 tlb_size = 4 << 10;
112 break;
113 case TLB_WORD0_SIZE_16KB:
114 tlb_size = 16 << 10;
115 break;
116 case TLB_WORD0_SIZE_64KB:
117 tlb_size = 64 << 10;
118 break;
119 case TLB_WORD0_SIZE_256KB:
120 tlb_size = 256 << 10;
121 break;
122 case TLB_WORD0_SIZE_1MB:
123 tlb_size = 1 << 20;
124 break;
125 case TLB_WORD0_SIZE_16MB:
126 tlb_size = 16 << 20;
127 break;
128 case TLB_WORD0_SIZE_256MB:
129 tlb_size = 256 << 20;
130 break;
131 }
132
133 /*
134 * Now check the end-address if it's in the range
135 */
Anatolij Gustschinaccf7352008-04-17 18:15:27 +0200136 if (((tlb_vaddr + tlb_size - 1) <= (vaddr + size - 1)) ||
137 ((tlb_vaddr < (vaddr + size - 1)) &&
138 ((tlb_vaddr + tlb_size - 1) > (vaddr + size - 1)))) {
Stefan Roese483e09a2007-10-31 17:59:22 +0100139 /*
140 * Found a TLB in the range.
141 * Change cache attribute in tlb2 word.
142 */
143 tlb_word2_value =
144 TLB_WORD2_U0_DISABLE | TLB_WORD2_U1_DISABLE |
145 TLB_WORD2_U2_DISABLE | TLB_WORD2_U3_DISABLE |
146 TLB_WORD2_W_DISABLE | tlb_word2_i_value |
147 TLB_WORD2_M_DISABLE | TLB_WORD2_G_DISABLE |
148 TLB_WORD2_E_DISABLE | TLB_WORD2_UX_ENABLE |
149 TLB_WORD2_UW_ENABLE | TLB_WORD2_UR_ENABLE |
150 TLB_WORD2_SX_ENABLE | TLB_WORD2_SW_ENABLE |
151 TLB_WORD2_SR_ENABLE;
152
153 /*
154 * Now either flush or invalidate the dcache
155 */
156 if (tlb_word2_i_value)
157 flush_dcache();
158 else
159 invalidate_dcache();
160
161 mttlb3(i, tlb_word2_value);
162 asm("iccci 0,0");
163 }
164 }
165 }
166
167 /* Execute an ISYNC instruction so that the new TLB entry takes effect */
168 asm("isync");
169}
170
Stefan Roese84a999b2008-02-19 22:01:57 +0100171static int add_tlb_entry(u64 phys_addr,
172 u32 virt_addr,
173 u32 tlb_word0_size_value,
174 u32 tlb_word2_i_value)
Stefan Roese4037ed32007-02-20 10:43:34 +0100175{
176 int i;
177 unsigned long tlb_word0_value;
178 unsigned long tlb_word1_value;
179 unsigned long tlb_word2_value;
180
181 /* First, find the index of a TLB entry not being used */
182 for (i=0; i<PPC4XX_TLB_SIZE; i++) {
183 tlb_word0_value = mftlb1(i);
184 if ((tlb_word0_value & TLB_WORD0_V_MASK) == TLB_WORD0_V_DISABLE)
185 break;
186 }
187 if (i >= PPC4XX_TLB_SIZE)
188 return -1;
189
190 /* Second, create the TLB entry */
Stefan Roesedbca2082007-06-14 11:14:32 +0200191 tlb_word0_value = TLB_WORD0_EPN_ENCODE(virt_addr) | TLB_WORD0_V_ENABLE |
Stefan Roese4037ed32007-02-20 10:43:34 +0100192 TLB_WORD0_TS_0 | tlb_word0_size_value;
Stefan Roese84a999b2008-02-19 22:01:57 +0100193 tlb_word1_value = TLB_WORD1_RPN_ENCODE((u32)phys_addr) |
194 TLB_WORD1_ERPN_ENCODE(phys_addr >> 32);
Stefan Roese4037ed32007-02-20 10:43:34 +0100195 tlb_word2_value = TLB_WORD2_U0_DISABLE | TLB_WORD2_U1_DISABLE |
196 TLB_WORD2_U2_DISABLE | TLB_WORD2_U3_DISABLE |
197 TLB_WORD2_W_DISABLE | tlb_word2_i_value |
198 TLB_WORD2_M_DISABLE | TLB_WORD2_G_DISABLE |
199 TLB_WORD2_E_DISABLE | TLB_WORD2_UX_ENABLE |
200 TLB_WORD2_UW_ENABLE | TLB_WORD2_UR_ENABLE |
201 TLB_WORD2_SX_ENABLE | TLB_WORD2_SW_ENABLE |
202 TLB_WORD2_SR_ENABLE;
203
204 /* Wait for all memory accesses to complete */
205 sync();
206
207 /* Third, add the TLB entries */
208 mttlb1(i, tlb_word0_value);
209 mttlb2(i, tlb_word1_value);
210 mttlb3(i, tlb_word2_value);
211
212 /* Execute an ISYNC instruction so that the new TLB entry takes effect */
213 asm("isync");
214
215 return 0;
216}
217
Stefan Roese84a999b2008-02-19 22:01:57 +0100218static void program_tlb_addr(u64 phys_addr,
219 u32 virt_addr,
220 u32 mem_size,
221 u32 tlb_word2_i_value)
Stefan Roese4037ed32007-02-20 10:43:34 +0100222{
223 int rc;
224 int tlb_i;
225
226 tlb_i = tlb_word2_i_value;
227 while (mem_size != 0) {
228 rc = 0;
229 /* Add the TLB entries in to map the region. */
Stefan Roesedbca2082007-06-14 11:14:32 +0200230 if (((phys_addr & TLB_256MB_ALIGN_MASK) == phys_addr) &&
Stefan Roese4037ed32007-02-20 10:43:34 +0100231 (mem_size >= TLB_256MB_SIZE)) {
232 /* Add a 256MB TLB entry */
Stefan Roesedbca2082007-06-14 11:14:32 +0200233 if ((rc = add_tlb_entry(phys_addr, virt_addr,
234 TLB_WORD0_SIZE_256MB, tlb_i)) == 0) {
Stefan Roese4037ed32007-02-20 10:43:34 +0100235 mem_size -= TLB_256MB_SIZE;
Stefan Roesedbca2082007-06-14 11:14:32 +0200236 phys_addr += TLB_256MB_SIZE;
Stefan Roese3a1f5c82007-06-22 16:58:40 +0200237 virt_addr += TLB_256MB_SIZE;
Stefan Roese4037ed32007-02-20 10:43:34 +0100238 }
Stefan Roesedbca2082007-06-14 11:14:32 +0200239 } else if (((phys_addr & TLB_16MB_ALIGN_MASK) == phys_addr) &&
Stefan Roese4037ed32007-02-20 10:43:34 +0100240 (mem_size >= TLB_16MB_SIZE)) {
241 /* Add a 16MB TLB entry */
Stefan Roesedbca2082007-06-14 11:14:32 +0200242 if ((rc = add_tlb_entry(phys_addr, virt_addr,
243 TLB_WORD0_SIZE_16MB, tlb_i)) == 0) {
Stefan Roese4037ed32007-02-20 10:43:34 +0100244 mem_size -= TLB_16MB_SIZE;
Stefan Roesedbca2082007-06-14 11:14:32 +0200245 phys_addr += TLB_16MB_SIZE;
Stefan Roese3a1f5c82007-06-22 16:58:40 +0200246 virt_addr += TLB_16MB_SIZE;
Stefan Roese4037ed32007-02-20 10:43:34 +0100247 }
Stefan Roesedbca2082007-06-14 11:14:32 +0200248 } else if (((phys_addr & TLB_1MB_ALIGN_MASK) == phys_addr) &&
Stefan Roese4037ed32007-02-20 10:43:34 +0100249 (mem_size >= TLB_1MB_SIZE)) {
250 /* Add a 1MB TLB entry */
Stefan Roesedbca2082007-06-14 11:14:32 +0200251 if ((rc = add_tlb_entry(phys_addr, virt_addr,
252 TLB_WORD0_SIZE_1MB, tlb_i)) == 0) {
Stefan Roese4037ed32007-02-20 10:43:34 +0100253 mem_size -= TLB_1MB_SIZE;
Stefan Roesedbca2082007-06-14 11:14:32 +0200254 phys_addr += TLB_1MB_SIZE;
Stefan Roese3a1f5c82007-06-22 16:58:40 +0200255 virt_addr += TLB_1MB_SIZE;
Stefan Roese4037ed32007-02-20 10:43:34 +0100256 }
Stefan Roesedbca2082007-06-14 11:14:32 +0200257 } else if (((phys_addr & TLB_256KB_ALIGN_MASK) == phys_addr) &&
Stefan Roese4037ed32007-02-20 10:43:34 +0100258 (mem_size >= TLB_256KB_SIZE)) {
259 /* Add a 256KB TLB entry */
Stefan Roesedbca2082007-06-14 11:14:32 +0200260 if ((rc = add_tlb_entry(phys_addr, virt_addr,
261 TLB_WORD0_SIZE_256KB, tlb_i)) == 0) {
Stefan Roese4037ed32007-02-20 10:43:34 +0100262 mem_size -= TLB_256KB_SIZE;
Stefan Roesedbca2082007-06-14 11:14:32 +0200263 phys_addr += TLB_256KB_SIZE;
Stefan Roese3a1f5c82007-06-22 16:58:40 +0200264 virt_addr += TLB_256KB_SIZE;
Stefan Roese4037ed32007-02-20 10:43:34 +0100265 }
Stefan Roesedbca2082007-06-14 11:14:32 +0200266 } else if (((phys_addr & TLB_64KB_ALIGN_MASK) == phys_addr) &&
Stefan Roese4037ed32007-02-20 10:43:34 +0100267 (mem_size >= TLB_64KB_SIZE)) {
268 /* Add a 64KB TLB entry */
Stefan Roesedbca2082007-06-14 11:14:32 +0200269 if ((rc = add_tlb_entry(phys_addr, virt_addr,
270 TLB_WORD0_SIZE_64KB, tlb_i)) == 0) {
Stefan Roese4037ed32007-02-20 10:43:34 +0100271 mem_size -= TLB_64KB_SIZE;
Stefan Roesedbca2082007-06-14 11:14:32 +0200272 phys_addr += TLB_64KB_SIZE;
Stefan Roese3a1f5c82007-06-22 16:58:40 +0200273 virt_addr += TLB_64KB_SIZE;
Stefan Roese4037ed32007-02-20 10:43:34 +0100274 }
Stefan Roesedbca2082007-06-14 11:14:32 +0200275 } else if (((phys_addr & TLB_16KB_ALIGN_MASK) == phys_addr) &&
Stefan Roese4037ed32007-02-20 10:43:34 +0100276 (mem_size >= TLB_16KB_SIZE)) {
277 /* Add a 16KB TLB entry */
Stefan Roesedbca2082007-06-14 11:14:32 +0200278 if ((rc = add_tlb_entry(phys_addr, virt_addr,
279 TLB_WORD0_SIZE_16KB, tlb_i)) == 0) {
Stefan Roese4037ed32007-02-20 10:43:34 +0100280 mem_size -= TLB_16KB_SIZE;
Stefan Roesedbca2082007-06-14 11:14:32 +0200281 phys_addr += TLB_16KB_SIZE;
Stefan Roese3a1f5c82007-06-22 16:58:40 +0200282 virt_addr += TLB_16KB_SIZE;
Stefan Roese4037ed32007-02-20 10:43:34 +0100283 }
Stefan Roesedbca2082007-06-14 11:14:32 +0200284 } else if (((phys_addr & TLB_4KB_ALIGN_MASK) == phys_addr) &&
Stefan Roese4037ed32007-02-20 10:43:34 +0100285 (mem_size >= TLB_4KB_SIZE)) {
286 /* Add a 4KB TLB entry */
Stefan Roesedbca2082007-06-14 11:14:32 +0200287 if ((rc = add_tlb_entry(phys_addr, virt_addr,
288 TLB_WORD0_SIZE_4KB, tlb_i)) == 0) {
Stefan Roese4037ed32007-02-20 10:43:34 +0100289 mem_size -= TLB_4KB_SIZE;
Stefan Roesedbca2082007-06-14 11:14:32 +0200290 phys_addr += TLB_4KB_SIZE;
Stefan Roese3a1f5c82007-06-22 16:58:40 +0200291 virt_addr += TLB_4KB_SIZE;
Stefan Roese4037ed32007-02-20 10:43:34 +0100292 }
Stefan Roesedbca2082007-06-14 11:14:32 +0200293 } else if (((phys_addr & TLB_1KB_ALIGN_MASK) == phys_addr) &&
Stefan Roese4037ed32007-02-20 10:43:34 +0100294 (mem_size >= TLB_1KB_SIZE)) {
295 /* Add a 1KB TLB entry */
Stefan Roesedbca2082007-06-14 11:14:32 +0200296 if ((rc = add_tlb_entry(phys_addr, virt_addr,
297 TLB_WORD0_SIZE_1KB, tlb_i)) == 0) {
Stefan Roese4037ed32007-02-20 10:43:34 +0100298 mem_size -= TLB_1KB_SIZE;
Stefan Roesedbca2082007-06-14 11:14:32 +0200299 phys_addr += TLB_1KB_SIZE;
Stefan Roese3a1f5c82007-06-22 16:58:40 +0200300 virt_addr += TLB_1KB_SIZE;
Stefan Roese4037ed32007-02-20 10:43:34 +0100301 }
302 } else {
Stefan Roeseb0021442008-07-10 09:58:06 +0200303 printf("ERROR: no TLB size exists for the base address 0x%llx.\n",
Stefan Roesedbca2082007-06-14 11:14:32 +0200304 phys_addr);
Stefan Roese4037ed32007-02-20 10:43:34 +0100305 }
306
307 if (rc != 0)
Stefan Roeseb0021442008-07-10 09:58:06 +0200308 printf("ERROR: no TLB entries available for the base addr 0x%llx.\n",
Stefan Roesedbca2082007-06-14 11:14:32 +0200309 phys_addr);
Stefan Roese4037ed32007-02-20 10:43:34 +0100310 }
311
312 return;
313}
314
315/*
316 * Program one (or multiple) TLB entries for one memory region
317 *
318 * Common usage for boards with SDRAM DIMM modules to dynamically
319 * configure the TLB's for the SDRAM
320 */
Stefan Roese84a999b2008-02-19 22:01:57 +0100321void program_tlb(u64 phys_addr, u32 virt_addr, u32 size, u32 tlb_word2_i_value)
Stefan Roese4037ed32007-02-20 10:43:34 +0100322{
323 region_t region_array;
324
Stefan Roesedbca2082007-06-14 11:14:32 +0200325 region_array.base = phys_addr;
Stefan Roese4037ed32007-02-20 10:43:34 +0100326 region_array.size = size;
Stefan Roeseba58e4c2007-03-01 21:11:36 +0100327 region_array.tlb_word2_i_value = tlb_word2_i_value; /* en-/disable cache */
Stefan Roese4037ed32007-02-20 10:43:34 +0100328
329 /* Call the routine to add in the tlb entries for the memory regions */
Stefan Roesedbca2082007-06-14 11:14:32 +0200330 program_tlb_addr(region_array.base, virt_addr, region_array.size,
Stefan Roese4037ed32007-02-20 10:43:34 +0100331 region_array.tlb_word2_i_value);
332
333 return;
334}
335
336#endif /* CONFIG_440 */