3 * Stefan Roese, DENX Software Engineering, sr@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
10 #if defined(CONFIG_440)
12 #include <asm/ppc440.h>
13 #include <asm/cache.h>
17 typedef struct region {
20 u32 tlb_word2_i_value;
23 void remove_tlb(u32 vaddr, u32 size)
30 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)) {
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.
40 switch (tlb_word0_value & TLB_WORD0_SIZE_MASK) {
41 case TLB_WORD0_SIZE_1KB:
44 case TLB_WORD0_SIZE_4KB:
47 case TLB_WORD0_SIZE_16KB:
50 case TLB_WORD0_SIZE_64KB:
53 case TLB_WORD0_SIZE_256KB:
56 case TLB_WORD0_SIZE_1MB:
59 case TLB_WORD0_SIZE_16MB:
62 case TLB_WORD0_SIZE_256MB:
68 * Now check the end-address if it's in the range
70 if ((tlb_vaddr + tlb_size - 1) <= (vaddr + size - 1))
72 * Found a TLB in the range.
73 * Disable it by writing 0 to tlb0 word.
79 /* Execute an ISYNC instruction so that the new TLB entry takes effect */
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
88 void change_tlb(u32 vaddr, u32 size, u32 tlb_word2_i_value)
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)) {
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.
106 switch (tlb_word0_value & TLB_WORD0_SIZE_MASK) {
107 case TLB_WORD0_SIZE_1KB:
110 case TLB_WORD0_SIZE_4KB:
113 case TLB_WORD0_SIZE_16KB:
116 case TLB_WORD0_SIZE_64KB:
119 case TLB_WORD0_SIZE_256KB:
120 tlb_size = 256 << 10;
122 case TLB_WORD0_SIZE_1MB:
125 case TLB_WORD0_SIZE_16MB:
128 case TLB_WORD0_SIZE_256MB:
129 tlb_size = 256 << 20;
134 * Now check the end-address if it's in the range
136 if (((tlb_vaddr + tlb_size - 1) <= (vaddr + size - 1)) ||
137 ((tlb_vaddr < (vaddr + size - 1)) &&
138 ((tlb_vaddr + tlb_size - 1) > (vaddr + size - 1)))) {
140 * Found a TLB in the range.
141 * Change cache attribute in tlb2 word.
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 |
154 * Now either flush or invalidate the dcache
156 if (tlb_word2_i_value)
161 mttlb3(i, tlb_word2_value);
167 /* Execute an ISYNC instruction so that the new TLB entry takes effect */
171 static int add_tlb_entry(u64 phys_addr,
173 u32 tlb_word0_size_value,
174 u32 tlb_word2_i_value)
177 unsigned long tlb_word0_value;
178 unsigned long tlb_word1_value;
179 unsigned long tlb_word2_value;
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)
187 if (i >= PPC4XX_TLB_SIZE)
190 /* Second, create the TLB entry */
191 tlb_word0_value = TLB_WORD0_EPN_ENCODE(virt_addr) | TLB_WORD0_V_ENABLE |
192 TLB_WORD0_TS_0 | tlb_word0_size_value;
193 tlb_word1_value = TLB_WORD1_RPN_ENCODE((u32)phys_addr) |
194 TLB_WORD1_ERPN_ENCODE(phys_addr >> 32);
195 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 |
204 /* Wait for all memory accesses to complete */
207 /* Third, add the TLB entries */
208 mttlb1(i, tlb_word0_value);
209 mttlb2(i, tlb_word1_value);
210 mttlb3(i, tlb_word2_value);
212 /* Execute an ISYNC instruction so that the new TLB entry takes effect */
218 static void program_tlb_addr(u64 phys_addr,
221 u32 tlb_word2_i_value)
226 tlb_i = tlb_word2_i_value;
227 while (mem_size != 0) {
229 /* Add the TLB entries in to map the region. */
230 if (((phys_addr & TLB_256MB_ALIGN_MASK) == phys_addr) &&
231 (mem_size >= TLB_256MB_SIZE)) {
232 /* Add a 256MB TLB entry */
233 if ((rc = add_tlb_entry(phys_addr, virt_addr,
234 TLB_WORD0_SIZE_256MB, tlb_i)) == 0) {
235 mem_size -= TLB_256MB_SIZE;
236 phys_addr += TLB_256MB_SIZE;
237 virt_addr += TLB_256MB_SIZE;
239 } else if (((phys_addr & TLB_16MB_ALIGN_MASK) == phys_addr) &&
240 (mem_size >= TLB_16MB_SIZE)) {
241 /* Add a 16MB TLB entry */
242 if ((rc = add_tlb_entry(phys_addr, virt_addr,
243 TLB_WORD0_SIZE_16MB, tlb_i)) == 0) {
244 mem_size -= TLB_16MB_SIZE;
245 phys_addr += TLB_16MB_SIZE;
246 virt_addr += TLB_16MB_SIZE;
248 } else if (((phys_addr & TLB_1MB_ALIGN_MASK) == phys_addr) &&
249 (mem_size >= TLB_1MB_SIZE)) {
250 /* Add a 1MB TLB entry */
251 if ((rc = add_tlb_entry(phys_addr, virt_addr,
252 TLB_WORD0_SIZE_1MB, tlb_i)) == 0) {
253 mem_size -= TLB_1MB_SIZE;
254 phys_addr += TLB_1MB_SIZE;
255 virt_addr += TLB_1MB_SIZE;
257 } else if (((phys_addr & TLB_256KB_ALIGN_MASK) == phys_addr) &&
258 (mem_size >= TLB_256KB_SIZE)) {
259 /* Add a 256KB TLB entry */
260 if ((rc = add_tlb_entry(phys_addr, virt_addr,
261 TLB_WORD0_SIZE_256KB, tlb_i)) == 0) {
262 mem_size -= TLB_256KB_SIZE;
263 phys_addr += TLB_256KB_SIZE;
264 virt_addr += TLB_256KB_SIZE;
266 } else if (((phys_addr & TLB_64KB_ALIGN_MASK) == phys_addr) &&
267 (mem_size >= TLB_64KB_SIZE)) {
268 /* Add a 64KB TLB entry */
269 if ((rc = add_tlb_entry(phys_addr, virt_addr,
270 TLB_WORD0_SIZE_64KB, tlb_i)) == 0) {
271 mem_size -= TLB_64KB_SIZE;
272 phys_addr += TLB_64KB_SIZE;
273 virt_addr += TLB_64KB_SIZE;
275 } else if (((phys_addr & TLB_16KB_ALIGN_MASK) == phys_addr) &&
276 (mem_size >= TLB_16KB_SIZE)) {
277 /* Add a 16KB TLB entry */
278 if ((rc = add_tlb_entry(phys_addr, virt_addr,
279 TLB_WORD0_SIZE_16KB, tlb_i)) == 0) {
280 mem_size -= TLB_16KB_SIZE;
281 phys_addr += TLB_16KB_SIZE;
282 virt_addr += TLB_16KB_SIZE;
284 } else if (((phys_addr & TLB_4KB_ALIGN_MASK) == phys_addr) &&
285 (mem_size >= TLB_4KB_SIZE)) {
286 /* Add a 4KB TLB entry */
287 if ((rc = add_tlb_entry(phys_addr, virt_addr,
288 TLB_WORD0_SIZE_4KB, tlb_i)) == 0) {
289 mem_size -= TLB_4KB_SIZE;
290 phys_addr += TLB_4KB_SIZE;
291 virt_addr += TLB_4KB_SIZE;
293 } else if (((phys_addr & TLB_1KB_ALIGN_MASK) == phys_addr) &&
294 (mem_size >= TLB_1KB_SIZE)) {
295 /* Add a 1KB TLB entry */
296 if ((rc = add_tlb_entry(phys_addr, virt_addr,
297 TLB_WORD0_SIZE_1KB, tlb_i)) == 0) {
298 mem_size -= TLB_1KB_SIZE;
299 phys_addr += TLB_1KB_SIZE;
300 virt_addr += TLB_1KB_SIZE;
303 printf("ERROR: no TLB size exists for the base address 0x%llx.\n",
308 printf("ERROR: no TLB entries available for the base addr 0x%llx.\n",
316 * Program one (or multiple) TLB entries for one memory region
318 * Common usage for boards with SDRAM DIMM modules to dynamically
319 * configure the TLB's for the SDRAM
321 void program_tlb(u64 phys_addr, u32 virt_addr, u32 size, u32 tlb_word2_i_value)
323 region_t region_array;
325 region_array.base = phys_addr;
326 region_array.size = size;
327 region_array.tlb_word2_i_value = tlb_word2_i_value; /* en-/disable cache */
329 /* Call the routine to add in the tlb entries for the memory regions */
330 program_tlb_addr(region_array.base, virt_addr, region_array.size,
331 region_array.tlb_word2_i_value);
336 #endif /* CONFIG_440 */