Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kumar Gala | 95bb67f | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 2 | /* |
Kumar Gala | d7ff6a8 | 2011-02-03 20:21:42 -0600 | [diff] [blame] | 3 | * Copyright 2008-2011 Freescale Semiconductor, Inc. |
Kumar Gala | 95bb67f | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 4 | * |
| 5 | * (C) Copyright 2000 |
| 6 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Kumar Gala | 95bb67f | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame^] | 9 | #include <config.h> |
Simon Glass | 1ab1692 | 2022-07-31 12:28:48 -0600 | [diff] [blame] | 10 | #include <display_options.h> |
Ovidiu Panait | 7dbb021 | 2022-01-01 19:13:29 +0200 | [diff] [blame] | 11 | #include <init.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 12 | #include <asm/bitops.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Kumar Gala | 95bb67f | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 14 | #include <asm/processor.h> |
| 15 | #include <asm/mmu.h> |
Kumar Gala | 9ac287a | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 16 | #ifdef CONFIG_ADDR_MAP |
| 17 | #include <addr_map.h> |
| 18 | #endif |
| 19 | |
Fabio Estevam | 1a03a7e | 2015-11-05 12:43:40 -0200 | [diff] [blame] | 20 | #include <linux/log2.h> |
| 21 | |
Kumar Gala | 9ac287a | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 22 | DECLARE_GLOBAL_DATA_PTR; |
Kumar Gala | 95bb67f | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 23 | |
Kumar Gala | afc51ad | 2009-09-11 12:32:01 -0500 | [diff] [blame] | 24 | void invalidate_tlb(u8 tlb) |
| 25 | { |
| 26 | if (tlb == 0) |
| 27 | mtspr(MMUCSR0, 0x4); |
| 28 | if (tlb == 1) |
| 29 | mtspr(MMUCSR0, 0x2); |
| 30 | } |
| 31 | |
Alexander Graf | c346848 | 2014-04-11 17:09:45 +0200 | [diff] [blame] | 32 | __weak void init_tlbs(void) |
Kumar Gala | afc51ad | 2009-09-11 12:32:01 -0500 | [diff] [blame] | 33 | { |
| 34 | int i; |
| 35 | |
| 36 | for (i = 0; i < num_tlb_entries; i++) { |
| 37 | write_tlb(tlb_table[i].mas0, |
| 38 | tlb_table[i].mas1, |
| 39 | tlb_table[i].mas2, |
| 40 | tlb_table[i].mas3, |
| 41 | tlb_table[i].mas7); |
| 42 | } |
| 43 | |
Bin Meng | 75a6a37 | 2022-10-26 12:40:07 +0800 | [diff] [blame] | 44 | return; |
Kumar Gala | afc51ad | 2009-09-11 12:32:01 -0500 | [diff] [blame] | 45 | } |
| 46 | |
Ying Zhang | ffc86e2 | 2013-08-16 15:16:10 +0800 | [diff] [blame] | 47 | #if !defined(CONFIG_NAND_SPL) && \ |
Tom Rini | 6b15c16 | 2022-05-13 12:26:35 -0400 | [diff] [blame] | 48 | (!defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(INIT_MINIMAL)) |
Becky Bruce | 9fa52d4 | 2010-06-17 11:37:21 -0500 | [diff] [blame] | 49 | void read_tlbcam_entry(int idx, u32 *valid, u32 *tsize, unsigned long *epn, |
| 50 | phys_addr_t *rpn) |
| 51 | { |
| 52 | u32 _mas1; |
| 53 | |
| 54 | mtspr(MAS0, FSL_BOOKE_MAS0(1, idx, 0)); |
| 55 | asm volatile("tlbre;isync"); |
| 56 | _mas1 = mfspr(MAS1); |
| 57 | |
| 58 | *valid = (_mas1 & MAS1_VALID); |
Scott Wood | 33a619c | 2013-01-18 15:45:58 +0000 | [diff] [blame] | 59 | *tsize = (_mas1 >> 7) & 0x1f; |
Becky Bruce | 9fa52d4 | 2010-06-17 11:37:21 -0500 | [diff] [blame] | 60 | *epn = mfspr(MAS2) & MAS2_EPN; |
| 61 | *rpn = mfspr(MAS3) & MAS3_RPN; |
| 62 | #ifdef CONFIG_ENABLE_36BIT_PHYS |
| 63 | *rpn |= ((u64)mfspr(MAS7)) << 32; |
| 64 | #endif |
| 65 | } |
| 66 | |
Becky Bruce | 7b9cdb4 | 2010-06-17 11:37:22 -0500 | [diff] [blame] | 67 | void print_tlbcam(void) |
| 68 | { |
| 69 | int i; |
| 70 | unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff; |
| 71 | |
| 72 | /* walk all the entries */ |
| 73 | printf("TLBCAM entries\n"); |
| 74 | for (i = 0; i < num_cam; i++) { |
| 75 | unsigned long epn; |
| 76 | u32 tsize, valid; |
| 77 | phys_addr_t rpn; |
| 78 | |
| 79 | read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn); |
| 80 | printf("entry %02d: V: %d EPN 0x%08x RPN 0x%08llx size:", |
| 81 | i, (valid == 0) ? 0 : 1, (unsigned int)epn, |
| 82 | (unsigned long long)rpn); |
| 83 | print_size(TSIZE_TO_BYTES(tsize), "\n"); |
| 84 | } |
| 85 | } |
| 86 | |
Kumar Gala | 42f9918 | 2009-11-12 10:26:16 -0600 | [diff] [blame] | 87 | static inline void use_tlb_cam(u8 idx) |
| 88 | { |
| 89 | int i = idx / 32; |
| 90 | int bit = idx % 32; |
| 91 | |
Simon Glass | 0b46658 | 2012-12-13 20:48:52 +0000 | [diff] [blame] | 92 | gd->arch.used_tlb_cams[i] |= (1 << bit); |
Kumar Gala | 42f9918 | 2009-11-12 10:26:16 -0600 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static inline void free_tlb_cam(u8 idx) |
| 96 | { |
| 97 | int i = idx / 32; |
| 98 | int bit = idx % 32; |
| 99 | |
Simon Glass | 0b46658 | 2012-12-13 20:48:52 +0000 | [diff] [blame] | 100 | gd->arch.used_tlb_cams[i] &= ~(1 << bit); |
Kumar Gala | 42f9918 | 2009-11-12 10:26:16 -0600 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void init_used_tlb_cams(void) |
| 104 | { |
| 105 | int i; |
| 106 | unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff; |
| 107 | |
| 108 | for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++) |
Simon Glass | 0b46658 | 2012-12-13 20:48:52 +0000 | [diff] [blame] | 109 | gd->arch.used_tlb_cams[i] = 0; |
Kumar Gala | 42f9918 | 2009-11-12 10:26:16 -0600 | [diff] [blame] | 110 | |
| 111 | /* walk all the entries */ |
| 112 | for (i = 0; i < num_cam; i++) { |
Kumar Gala | 42f9918 | 2009-11-12 10:26:16 -0600 | [diff] [blame] | 113 | mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0)); |
Kumar Gala | 42f9918 | 2009-11-12 10:26:16 -0600 | [diff] [blame] | 114 | asm volatile("tlbre;isync"); |
Becky Bruce | 9fa52d4 | 2010-06-17 11:37:21 -0500 | [diff] [blame] | 115 | if (mfspr(MAS1) & MAS1_VALID) |
Kumar Gala | 42f9918 | 2009-11-12 10:26:16 -0600 | [diff] [blame] | 116 | use_tlb_cam(i); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | int find_free_tlbcam(void) |
| 121 | { |
| 122 | int i; |
| 123 | u32 idx; |
| 124 | |
| 125 | for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++) { |
Simon Glass | 0b46658 | 2012-12-13 20:48:52 +0000 | [diff] [blame] | 126 | idx = ffz(gd->arch.used_tlb_cams[i]); |
Kumar Gala | 42f9918 | 2009-11-12 10:26:16 -0600 | [diff] [blame] | 127 | |
| 128 | if (idx != 32) |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | idx += i * 32; |
| 133 | |
| 134 | if (idx >= CONFIG_SYS_NUM_TLBCAMS) |
| 135 | return -1; |
| 136 | |
| 137 | return idx; |
| 138 | } |
| 139 | |
Kumar Gala | 95bb67f | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 140 | void set_tlb(u8 tlb, u32 epn, u64 rpn, |
| 141 | u8 perms, u8 wimge, |
| 142 | u8 ts, u8 esel, u8 tsize, u8 iprot) |
| 143 | { |
| 144 | u32 _mas0, _mas1, _mas2, _mas3, _mas7; |
| 145 | |
Kumar Gala | 42f9918 | 2009-11-12 10:26:16 -0600 | [diff] [blame] | 146 | if (tlb == 1) |
| 147 | use_tlb_cam(esel); |
| 148 | |
Scott Wood | 33a619c | 2013-01-18 15:45:58 +0000 | [diff] [blame] | 149 | if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1 && |
| 150 | tsize & 1) { |
| 151 | printf("%s: bad tsize %d on entry %d at 0x%08x\n", |
| 152 | __func__, tsize, tlb, epn); |
| 153 | return; |
| 154 | } |
| 155 | |
Kumar Gala | 95bb67f | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 156 | _mas0 = FSL_BOOKE_MAS0(tlb, esel, 0); |
| 157 | _mas1 = FSL_BOOKE_MAS1(1, iprot, 0, ts, tsize); |
| 158 | _mas2 = FSL_BOOKE_MAS2(epn, wimge); |
| 159 | _mas3 = FSL_BOOKE_MAS3(rpn, 0, perms); |
Kumar Gala | c417c91 | 2009-09-11 11:27:00 -0500 | [diff] [blame] | 160 | _mas7 = FSL_BOOKE_MAS7(rpn); |
Kumar Gala | 95bb67f | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 161 | |
Kumar Gala | c417c91 | 2009-09-11 11:27:00 -0500 | [diff] [blame] | 162 | write_tlb(_mas0, _mas1, _mas2, _mas3, _mas7); |
Kumar Gala | 9ac287a | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 163 | |
| 164 | #ifdef CONFIG_ADDR_MAP |
| 165 | if ((tlb == 1) && (gd->flags & GD_FLG_RELOC)) |
Becky Bruce | 9fa52d4 | 2010-06-17 11:37:21 -0500 | [diff] [blame] | 166 | addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), esel); |
Kumar Gala | 9ac287a | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 167 | #endif |
Kumar Gala | 95bb67f | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void disable_tlb(u8 esel) |
| 171 | { |
Kumar Gala | 4a27247 | 2011-11-09 09:59:32 -0600 | [diff] [blame] | 172 | u32 _mas0, _mas1, _mas2, _mas3; |
Kumar Gala | 95bb67f | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 173 | |
Kumar Gala | 42f9918 | 2009-11-12 10:26:16 -0600 | [diff] [blame] | 174 | free_tlb_cam(esel); |
| 175 | |
Kumar Gala | 95bb67f | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 176 | _mas0 = FSL_BOOKE_MAS0(1, esel, 0); |
| 177 | _mas1 = 0; |
| 178 | _mas2 = 0; |
| 179 | _mas3 = 0; |
Kumar Gala | 95bb67f | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 180 | |
| 181 | mtspr(MAS0, _mas0); |
| 182 | mtspr(MAS1, _mas1); |
| 183 | mtspr(MAS2, _mas2); |
| 184 | mtspr(MAS3, _mas3); |
| 185 | #ifdef CONFIG_ENABLE_36BIT_PHYS |
Kumar Gala | 4a27247 | 2011-11-09 09:59:32 -0600 | [diff] [blame] | 186 | mtspr(MAS7, 0); |
Kumar Gala | 95bb67f | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 187 | #endif |
| 188 | asm volatile("isync;msync;tlbwe;isync"); |
Kumar Gala | 9ac287a | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 189 | |
| 190 | #ifdef CONFIG_ADDR_MAP |
| 191 | if (gd->flags & GD_FLG_RELOC) |
| 192 | addrmap_set_entry(0, 0, 0, esel); |
| 193 | #endif |
Kumar Gala | 95bb67f | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 194 | } |
| 195 | |
Kumar Gala | d13eb3c | 2009-09-03 08:20:24 -0500 | [diff] [blame] | 196 | static void tlbsx (const volatile unsigned *addr) |
| 197 | { |
| 198 | __asm__ __volatile__ ("tlbsx 0,%0" : : "r" (addr), "m" (*addr)); |
| 199 | } |
| 200 | |
| 201 | /* return -1 if we didn't find anything */ |
| 202 | int find_tlb_idx(void *addr, u8 tlbsel) |
| 203 | { |
| 204 | u32 _mas0, _mas1; |
| 205 | |
| 206 | /* zero out Search PID, AS */ |
| 207 | mtspr(MAS6, 0); |
| 208 | |
| 209 | tlbsx(addr); |
| 210 | |
| 211 | _mas0 = mfspr(MAS0); |
| 212 | _mas1 = mfspr(MAS1); |
| 213 | |
| 214 | /* we found something, and its in the TLB we expect */ |
| 215 | if ((MAS1_VALID & _mas1) && |
| 216 | (MAS0_TLBSEL(tlbsel) == (_mas0 & MAS0_TLBSEL_MSK))) { |
| 217 | return ((_mas0 & MAS0_ESEL_MSK) >> 16); |
| 218 | } |
| 219 | |
| 220 | return -1; |
| 221 | } |
| 222 | |
Kumar Gala | 9ac287a | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 223 | #ifdef CONFIG_ADDR_MAP |
Ovidiu Panait | 9ef3c8a | 2022-01-01 19:13:28 +0200 | [diff] [blame] | 224 | int init_addr_map(void) |
Kumar Gala | 9ac287a | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 225 | { |
| 226 | int i; |
Kumar Gala | 7601fb2 | 2009-11-13 08:52:21 -0600 | [diff] [blame] | 227 | unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff; |
Kumar Gala | 9ac287a | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 228 | |
Kumar Gala | 87f5792 | 2009-08-14 16:43:22 -0500 | [diff] [blame] | 229 | /* walk all the entries */ |
Kumar Gala | 7601fb2 | 2009-11-13 08:52:21 -0600 | [diff] [blame] | 230 | for (i = 0; i < num_cam; i++) { |
Kumar Gala | 87f5792 | 2009-08-14 16:43:22 -0500 | [diff] [blame] | 231 | unsigned long epn; |
Becky Bruce | 9fa52d4 | 2010-06-17 11:37:21 -0500 | [diff] [blame] | 232 | u32 tsize, valid; |
Kumar Gala | 87f5792 | 2009-08-14 16:43:22 -0500 | [diff] [blame] | 233 | phys_addr_t rpn; |
| 234 | |
Becky Bruce | 9fa52d4 | 2010-06-17 11:37:21 -0500 | [diff] [blame] | 235 | read_tlbcam_entry(i, &valid, &tsize, &epn, &rpn); |
| 236 | if (valid & MAS1_VALID) |
| 237 | addrmap_set_entry(epn, rpn, TSIZE_TO_BYTES(tsize), i); |
Kumar Gala | 9ac287a | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 238 | } |
| 239 | |
Ovidiu Panait | 9ef3c8a | 2022-01-01 19:13:28 +0200 | [diff] [blame] | 240 | return 0; |
Kumar Gala | 9ac287a | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 241 | } |
| 242 | #endif |
| 243 | |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 244 | uint64_t tlb_map_range(ulong v_addr, phys_addr_t p_addr, uint64_t size, |
| 245 | enum tlb_map_type map_type) |
Kumar Gala | 80f4bc7 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 246 | { |
Kumar Gala | 419083b | 2009-11-13 09:04:19 -0600 | [diff] [blame] | 247 | int i; |
Kumar Gala | 80f4bc7 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 248 | unsigned int tlb_size; |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 249 | unsigned int wimge; |
| 250 | unsigned int perm; |
Scott Wood | 33a619c | 2013-01-18 15:45:58 +0000 | [diff] [blame] | 251 | unsigned int max_cam, tsize_mask; |
Kumar Gala | 80f4bc7 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 252 | |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 253 | if (map_type == TLB_MAP_RAM) { |
| 254 | perm = MAS3_SX|MAS3_SW|MAS3_SR; |
| 255 | wimge = MAS2_M; |
Becky Bruce | 92e163f | 2010-12-17 17:17:55 -0600 | [diff] [blame] | 256 | #ifdef CONFIG_SYS_PPC_DDR_WIMGE |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 257 | wimge = CONFIG_SYS_PPC_DDR_WIMGE; |
Becky Bruce | 92e163f | 2010-12-17 17:17:55 -0600 | [diff] [blame] | 258 | #endif |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 259 | } else { |
| 260 | perm = MAS3_SW|MAS3_SR; |
| 261 | wimge = MAS2_I|MAS2_G; |
| 262 | } |
| 263 | |
Kumar Gala | ac7e895 | 2011-10-31 22:13:26 -0500 | [diff] [blame] | 264 | if ((mfspr(SPRN_MMUCFG) & MMUCFG_MAVN) == MMUCFG_MAVN_V1) { |
| 265 | /* Convert (4^max) kB to (2^max) bytes */ |
| 266 | max_cam = ((mfspr(SPRN_TLB1CFG) >> 16) & 0xf) * 2 + 10; |
Scott Wood | 33a619c | 2013-01-18 15:45:58 +0000 | [diff] [blame] | 267 | tsize_mask = ~1U; |
Kumar Gala | ac7e895 | 2011-10-31 22:13:26 -0500 | [diff] [blame] | 268 | } else { |
| 269 | /* Convert (2^max) kB to (2^max) bytes */ |
| 270 | max_cam = __ilog2(mfspr(SPRN_TLB1PS)) + 10; |
Scott Wood | 33a619c | 2013-01-18 15:45:58 +0000 | [diff] [blame] | 271 | tsize_mask = ~0U; |
Kumar Gala | ac7e895 | 2011-10-31 22:13:26 -0500 | [diff] [blame] | 272 | } |
Kumar Gala | 6630ffb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 273 | |
Kumar Gala | 419083b | 2009-11-13 09:04:19 -0600 | [diff] [blame] | 274 | for (i = 0; size && i < 8; i++) { |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 275 | int tlb_index = find_free_tlbcam(); |
Scott Wood | 33a619c | 2013-01-18 15:45:58 +0000 | [diff] [blame] | 276 | u32 camsize = __ilog2_u64(size) & tsize_mask; |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 277 | u32 align = __ilog2(v_addr) & tsize_mask; |
Kumar Gala | 6630ffb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 278 | |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 279 | if (tlb_index == -1) |
Kumar Gala | 419083b | 2009-11-13 09:04:19 -0600 | [diff] [blame] | 280 | break; |
| 281 | |
Kumar Gala | 6630ffb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 282 | if (align == -2) align = max_cam; |
| 283 | if (camsize > align) |
| 284 | camsize = align; |
Kumar Gala | 80f4bc7 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 285 | |
Kumar Gala | 6630ffb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 286 | if (camsize > max_cam) |
| 287 | camsize = max_cam; |
| 288 | |
Scott Wood | 33a619c | 2013-01-18 15:45:58 +0000 | [diff] [blame] | 289 | tlb_size = camsize - 10; |
Kumar Gala | 6630ffb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 290 | |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 291 | set_tlb(1, v_addr, p_addr, perm, wimge, |
| 292 | 0, tlb_index, tlb_size, 1); |
Kumar Gala | 80f4bc7 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 293 | |
Kumar Gala | 6630ffb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 294 | size -= 1ULL << camsize; |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 295 | v_addr += 1UL << camsize; |
York Sun | ba99a33 | 2010-09-28 15:20:32 -0700 | [diff] [blame] | 296 | p_addr += 1UL << camsize; |
Kumar Gala | 80f4bc7 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 297 | } |
| 298 | |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 299 | return size; |
| 300 | } |
| 301 | |
| 302 | unsigned int setup_ddr_tlbs_phys(phys_addr_t p_addr, |
| 303 | unsigned int memsize_in_meg) |
| 304 | { |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 305 | unsigned int ram_tlb_address = (unsigned int)CFG_SYS_DDR_SDRAM_BASE; |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 306 | u64 memsize = (u64)memsize_in_meg << 20; |
York Sun | 55ec481 | 2014-12-02 11:21:09 -0800 | [diff] [blame] | 307 | u64 size; |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 308 | |
Tom Rini | bc9d46b | 2022-12-04 10:04:50 -0500 | [diff] [blame] | 309 | size = min(memsize, (u64)CFG_MAX_MEM_MAPPED); |
York Sun | 55ec481 | 2014-12-02 11:21:09 -0800 | [diff] [blame] | 310 | size = tlb_map_range(ram_tlb_address, p_addr, size, TLB_MAP_RAM); |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 311 | |
Tom Rini | bc9d46b | 2022-12-04 10:04:50 -0500 | [diff] [blame] | 312 | if (size || memsize > CFG_MAX_MEM_MAPPED) { |
| 313 | print_size(memsize > CFG_MAX_MEM_MAPPED ? |
| 314 | memsize - CFG_MAX_MEM_MAPPED + size : size, |
Pali Rohár | 777ba48 | 2022-09-09 17:32:46 +0200 | [diff] [blame] | 315 | " of DDR memory left unmapped in U-Boot\n"); |
Pali Rohár | 16f862f | 2022-09-11 11:29:16 +0200 | [diff] [blame] | 316 | #ifndef CONFIG_SPL_BUILD |
| 317 | puts(" "); |
| 318 | #endif |
York Sun | 55ec481 | 2014-12-02 11:21:09 -0800 | [diff] [blame] | 319 | } |
Alexander Graf | 4c5d426 | 2014-04-11 17:09:43 +0200 | [diff] [blame] | 320 | |
Kumar Gala | 80f4bc7 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 321 | return memsize_in_meg; |
| 322 | } |
York Sun | ba99a33 | 2010-09-28 15:20:32 -0700 | [diff] [blame] | 323 | |
| 324 | unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg) |
| 325 | { |
| 326 | return |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 327 | setup_ddr_tlbs_phys(CFG_SYS_DDR_SDRAM_BASE, memsize_in_meg); |
York Sun | ba99a33 | 2010-09-28 15:20:32 -0700 | [diff] [blame] | 328 | } |
Becky Bruce | 6969447 | 2011-07-18 18:49:15 -0500 | [diff] [blame] | 329 | |
| 330 | /* Invalidate the DDR TLBs for the requested size */ |
| 331 | void clear_ddr_tlbs_phys(phys_addr_t p_addr, unsigned int memsize_in_meg) |
| 332 | { |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 333 | u32 vstart = CFG_SYS_DDR_SDRAM_BASE; |
Becky Bruce | 6969447 | 2011-07-18 18:49:15 -0500 | [diff] [blame] | 334 | unsigned long epn; |
| 335 | u32 tsize, valid, ptr; |
| 336 | phys_addr_t rpn = 0; |
| 337 | int ddr_esel; |
| 338 | u64 memsize = (u64)memsize_in_meg << 20; |
| 339 | |
| 340 | ptr = vstart; |
| 341 | |
| 342 | while (ptr < (vstart + memsize)) { |
| 343 | ddr_esel = find_tlb_idx((void *)ptr, 1); |
| 344 | if (ddr_esel != -1) { |
| 345 | read_tlbcam_entry(ddr_esel, &valid, &tsize, &epn, &rpn); |
| 346 | disable_tlb(ddr_esel); |
| 347 | } |
| 348 | ptr += TSIZE_TO_BYTES(tsize); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void clear_ddr_tlbs(unsigned int memsize_in_meg) |
| 353 | { |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 354 | clear_ddr_tlbs_phys(CFG_SYS_DDR_SDRAM_BASE, memsize_in_meg); |
Becky Bruce | 6969447 | 2011-07-18 18:49:15 -0500 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | |
Scott Wood | 095b712 | 2012-09-20 19:02:18 -0500 | [diff] [blame] | 358 | #endif /* not SPL */ |