Kumar Gala | 44a23cf | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 1 | /* |
Kumar Gala | d30f904 | 2009-09-11 11:27:00 -0500 | [diff] [blame] | 2 | * Copyright 2008-2009 Freescale Semiconductor, Inc. |
Kumar Gala | 44a23cf | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 3 | * |
| 4 | * (C) Copyright 2000 |
| 5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 6 | * |
| 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
| 25 | |
| 26 | #include <common.h> |
| 27 | #include <asm/processor.h> |
| 28 | #include <asm/mmu.h> |
Kumar Gala | ecf5b98 | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 29 | #ifdef CONFIG_ADDR_MAP |
| 30 | #include <addr_map.h> |
| 31 | #endif |
| 32 | |
| 33 | DECLARE_GLOBAL_DATA_PTR; |
Kumar Gala | 44a23cf | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 34 | |
Kumar Gala | b2eec28 | 2009-09-11 12:32:01 -0500 | [diff] [blame] | 35 | void invalidate_tlb(u8 tlb) |
| 36 | { |
| 37 | if (tlb == 0) |
| 38 | mtspr(MMUCSR0, 0x4); |
| 39 | if (tlb == 1) |
| 40 | mtspr(MMUCSR0, 0x2); |
| 41 | } |
| 42 | |
| 43 | void init_tlbs(void) |
| 44 | { |
| 45 | int i; |
| 46 | |
| 47 | for (i = 0; i < num_tlb_entries; i++) { |
| 48 | write_tlb(tlb_table[i].mas0, |
| 49 | tlb_table[i].mas1, |
| 50 | tlb_table[i].mas2, |
| 51 | tlb_table[i].mas3, |
| 52 | tlb_table[i].mas7); |
| 53 | } |
| 54 | |
| 55 | return ; |
| 56 | } |
| 57 | |
Mingkai Hu | 7da5335 | 2009-09-11 14:19:10 +0800 | [diff] [blame] | 58 | #ifndef CONFIG_NAND_SPL |
Kumar Gala | 94e9411 | 2009-11-12 10:26:16 -0600 | [diff] [blame] | 59 | static inline void use_tlb_cam(u8 idx) |
| 60 | { |
| 61 | int i = idx / 32; |
| 62 | int bit = idx % 32; |
| 63 | |
| 64 | gd->used_tlb_cams[i] |= (1 << bit); |
| 65 | } |
| 66 | |
| 67 | static inline void free_tlb_cam(u8 idx) |
| 68 | { |
| 69 | int i = idx / 32; |
| 70 | int bit = idx % 32; |
| 71 | |
| 72 | gd->used_tlb_cams[i] &= ~(1 << bit); |
| 73 | } |
| 74 | |
| 75 | void init_used_tlb_cams(void) |
| 76 | { |
| 77 | int i; |
| 78 | unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff; |
| 79 | |
| 80 | for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++) |
| 81 | gd->used_tlb_cams[i] = 0; |
| 82 | |
| 83 | /* walk all the entries */ |
| 84 | for (i = 0; i < num_cam; i++) { |
| 85 | u32 _mas1; |
| 86 | |
| 87 | mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0)); |
| 88 | |
| 89 | asm volatile("tlbre;isync"); |
| 90 | _mas1 = mfspr(MAS1); |
| 91 | |
| 92 | /* if the entry isn't valid skip it */ |
| 93 | if ((_mas1 & MAS1_VALID)) |
| 94 | use_tlb_cam(i); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | int find_free_tlbcam(void) |
| 99 | { |
| 100 | int i; |
| 101 | u32 idx; |
| 102 | |
| 103 | for (i = 0; i < ((CONFIG_SYS_NUM_TLBCAMS+31)/32); i++) { |
| 104 | idx = ffz(gd->used_tlb_cams[i]); |
| 105 | |
| 106 | if (idx != 32) |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | idx += i * 32; |
| 111 | |
| 112 | if (idx >= CONFIG_SYS_NUM_TLBCAMS) |
| 113 | return -1; |
| 114 | |
| 115 | return idx; |
| 116 | } |
| 117 | |
Kumar Gala | 44a23cf | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 118 | void set_tlb(u8 tlb, u32 epn, u64 rpn, |
| 119 | u8 perms, u8 wimge, |
| 120 | u8 ts, u8 esel, u8 tsize, u8 iprot) |
| 121 | { |
| 122 | u32 _mas0, _mas1, _mas2, _mas3, _mas7; |
| 123 | |
Kumar Gala | 94e9411 | 2009-11-12 10:26:16 -0600 | [diff] [blame] | 124 | if (tlb == 1) |
| 125 | use_tlb_cam(esel); |
| 126 | |
Kumar Gala | 44a23cf | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 127 | _mas0 = FSL_BOOKE_MAS0(tlb, esel, 0); |
| 128 | _mas1 = FSL_BOOKE_MAS1(1, iprot, 0, ts, tsize); |
| 129 | _mas2 = FSL_BOOKE_MAS2(epn, wimge); |
| 130 | _mas3 = FSL_BOOKE_MAS3(rpn, 0, perms); |
Kumar Gala | d30f904 | 2009-09-11 11:27:00 -0500 | [diff] [blame] | 131 | _mas7 = FSL_BOOKE_MAS7(rpn); |
Kumar Gala | 44a23cf | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 132 | |
Kumar Gala | d30f904 | 2009-09-11 11:27:00 -0500 | [diff] [blame] | 133 | write_tlb(_mas0, _mas1, _mas2, _mas3, _mas7); |
Kumar Gala | ecf5b98 | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 134 | |
| 135 | #ifdef CONFIG_ADDR_MAP |
| 136 | if ((tlb == 1) && (gd->flags & GD_FLG_RELOC)) |
| 137 | addrmap_set_entry(epn, rpn, (1UL << ((tsize * 2) + 10)), esel); |
| 138 | #endif |
Kumar Gala | 44a23cf | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void disable_tlb(u8 esel) |
| 142 | { |
| 143 | u32 _mas0, _mas1, _mas2, _mas3, _mas7; |
| 144 | |
Kumar Gala | 94e9411 | 2009-11-12 10:26:16 -0600 | [diff] [blame] | 145 | free_tlb_cam(esel); |
| 146 | |
Kumar Gala | 44a23cf | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 147 | _mas0 = FSL_BOOKE_MAS0(1, esel, 0); |
| 148 | _mas1 = 0; |
| 149 | _mas2 = 0; |
| 150 | _mas3 = 0; |
| 151 | _mas7 = 0; |
| 152 | |
| 153 | mtspr(MAS0, _mas0); |
| 154 | mtspr(MAS1, _mas1); |
| 155 | mtspr(MAS2, _mas2); |
| 156 | mtspr(MAS3, _mas3); |
| 157 | #ifdef CONFIG_ENABLE_36BIT_PHYS |
| 158 | mtspr(MAS7, _mas7); |
| 159 | #endif |
| 160 | asm volatile("isync;msync;tlbwe;isync"); |
Kumar Gala | ecf5b98 | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 161 | |
| 162 | #ifdef CONFIG_ADDR_MAP |
| 163 | if (gd->flags & GD_FLG_RELOC) |
| 164 | addrmap_set_entry(0, 0, 0, esel); |
| 165 | #endif |
Kumar Gala | 44a23cf | 2008-01-16 22:33:22 -0600 | [diff] [blame] | 166 | } |
| 167 | |
Kumar Gala | c2287af | 2009-09-03 08:20:24 -0500 | [diff] [blame] | 168 | static void tlbsx (const volatile unsigned *addr) |
| 169 | { |
| 170 | __asm__ __volatile__ ("tlbsx 0,%0" : : "r" (addr), "m" (*addr)); |
| 171 | } |
| 172 | |
| 173 | /* return -1 if we didn't find anything */ |
| 174 | int find_tlb_idx(void *addr, u8 tlbsel) |
| 175 | { |
| 176 | u32 _mas0, _mas1; |
| 177 | |
| 178 | /* zero out Search PID, AS */ |
| 179 | mtspr(MAS6, 0); |
| 180 | |
| 181 | tlbsx(addr); |
| 182 | |
| 183 | _mas0 = mfspr(MAS0); |
| 184 | _mas1 = mfspr(MAS1); |
| 185 | |
| 186 | /* we found something, and its in the TLB we expect */ |
| 187 | if ((MAS1_VALID & _mas1) && |
| 188 | (MAS0_TLBSEL(tlbsel) == (_mas0 & MAS0_TLBSEL_MSK))) { |
| 189 | return ((_mas0 & MAS0_ESEL_MSK) >> 16); |
| 190 | } |
| 191 | |
| 192 | return -1; |
| 193 | } |
| 194 | |
Kumar Gala | ecf5b98 | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 195 | #ifdef CONFIG_ADDR_MAP |
| 196 | void init_addr_map(void) |
| 197 | { |
| 198 | int i; |
Kumar Gala | cdbdbe6 | 2009-11-13 08:52:21 -0600 | [diff] [blame] | 199 | unsigned int num_cam = mfspr(SPRN_TLB1CFG) & 0xfff; |
Kumar Gala | ecf5b98 | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 200 | |
Kumar Gala | e393e2e | 2009-08-14 16:43:22 -0500 | [diff] [blame] | 201 | /* walk all the entries */ |
Kumar Gala | cdbdbe6 | 2009-11-13 08:52:21 -0600 | [diff] [blame] | 202 | for (i = 0; i < num_cam; i++) { |
Kumar Gala | e393e2e | 2009-08-14 16:43:22 -0500 | [diff] [blame] | 203 | unsigned long epn; |
Wolfgang Denk | 963f2f6 | 2009-08-22 23:27:26 +0200 | [diff] [blame] | 204 | u32 tsize, _mas1; |
Kumar Gala | e393e2e | 2009-08-14 16:43:22 -0500 | [diff] [blame] | 205 | phys_addr_t rpn; |
| 206 | |
| 207 | mtspr(MAS0, FSL_BOOKE_MAS0(1, i, 0)); |
| 208 | |
| 209 | asm volatile("tlbre;isync"); |
| 210 | _mas1 = mfspr(MAS1); |
| 211 | |
| 212 | /* if the entry isn't valid skip it */ |
| 213 | if (!(_mas1 & MAS1_VALID)) |
Kumar Gala | ecf5b98 | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 214 | continue; |
| 215 | |
Kumar Gala | e393e2e | 2009-08-14 16:43:22 -0500 | [diff] [blame] | 216 | tsize = (_mas1 >> 8) & 0xf; |
| 217 | epn = mfspr(MAS2) & MAS2_EPN; |
| 218 | rpn = mfspr(MAS3) & MAS3_RPN; |
| 219 | #ifdef CONFIG_ENABLE_36BIT_PHYS |
| 220 | rpn |= ((phys_addr_t)mfspr(MAS7)) << 32; |
| 221 | #endif |
| 222 | |
| 223 | addrmap_set_entry(epn, rpn, (1UL << ((tsize * 2) + 10)), i); |
Kumar Gala | ecf5b98 | 2008-12-16 14:59:20 -0600 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | return ; |
| 227 | } |
| 228 | #endif |
| 229 | |
Kumar Gala | 6fb1b73 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 230 | unsigned int setup_ddr_tlbs(unsigned int memsize_in_meg) |
| 231 | { |
Kumar Gala | 355f4f8 | 2009-11-13 09:04:19 -0600 | [diff] [blame] | 232 | int i; |
Kumar Gala | 6fb1b73 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 233 | unsigned int tlb_size; |
Kumar Gala | f8523cb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 234 | unsigned int ram_tlb_address = (unsigned int)CONFIG_SYS_DDR_SDRAM_BASE; |
Fredrik Arnerup | 90d13b8 | 2009-06-02 16:27:10 -0500 | [diff] [blame] | 235 | unsigned int max_cam = (mfspr(SPRN_TLB1CFG) >> 16) & 0xf; |
Kumar Gala | f8523cb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 236 | u64 size, memsize = (u64)memsize_in_meg << 20; |
Kumar Gala | 6fb1b73 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 237 | |
Kumar Gala | f8523cb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 238 | size = min(memsize, CONFIG_MAX_MEM_MAPPED); |
Kumar Gala | 6fb1b73 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 239 | |
Kumar Gala | f8523cb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 240 | /* Convert (4^max) kB to (2^max) bytes */ |
| 241 | max_cam = max_cam * 2 + 10; |
Kumar Gala | 6fb1b73 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 242 | |
Kumar Gala | 355f4f8 | 2009-11-13 09:04:19 -0600 | [diff] [blame] | 243 | for (i = 0; size && i < 8; i++) { |
| 244 | int ram_tlb_index = find_free_tlbcam(); |
Kumar Gala | f8523cb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 245 | u32 camsize = __ilog2_u64(size) & ~1U; |
| 246 | u32 align = __ilog2(ram_tlb_address) & ~1U; |
| 247 | |
Kumar Gala | 355f4f8 | 2009-11-13 09:04:19 -0600 | [diff] [blame] | 248 | if (ram_tlb_index == -1) |
| 249 | break; |
| 250 | |
Kumar Gala | f8523cb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 251 | if (align == -2) align = max_cam; |
| 252 | if (camsize > align) |
| 253 | camsize = align; |
| 254 | |
| 255 | if (camsize > max_cam) |
| 256 | camsize = max_cam; |
| 257 | |
| 258 | tlb_size = (camsize - 10) / 2; |
| 259 | |
Kumar Gala | 6fb1b73 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 260 | set_tlb(1, ram_tlb_address, ram_tlb_address, |
| 261 | MAS3_SX|MAS3_SW|MAS3_SR, 0, |
| 262 | 0, ram_tlb_index, tlb_size, 1); |
| 263 | |
Kumar Gala | f8523cb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 264 | size -= 1ULL << camsize; |
| 265 | memsize -= 1ULL << camsize; |
| 266 | ram_tlb_address += 1UL << camsize; |
Kumar Gala | 6fb1b73 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 267 | } |
| 268 | |
Kumar Gala | f8523cb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 269 | if (memsize) |
Kumar Gala | d4b130d | 2009-06-11 23:40:34 -0500 | [diff] [blame] | 270 | print_size(memsize, " left unmapped\n"); |
Kumar Gala | f8523cb | 2009-02-06 09:56:35 -0600 | [diff] [blame] | 271 | |
Kumar Gala | 6fb1b73 | 2008-06-09 11:07:46 -0500 | [diff] [blame] | 272 | /* |
| 273 | * Confirm that the requested amount of memory was mapped. |
| 274 | */ |
| 275 | return memsize_in_meg; |
| 276 | } |
Mingkai Hu | 7da5335 | 2009-09-11 14:19:10 +0800 | [diff] [blame] | 277 | #endif /* !CONFIG_NAND_SPL */ |