Stefan Roese | 43f3247 | 2007-02-20 10:43:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007 |
| 3 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | |
| 26 | #if defined(CONFIG_440) |
| 27 | |
| 28 | #include <ppc4xx.h> |
| 29 | #include <ppc440.h> |
| 30 | #include <asm/io.h> |
| 31 | #include <asm/mmu.h> |
| 32 | |
| 33 | typedef struct region { |
| 34 | unsigned long base; |
| 35 | unsigned long size; |
| 36 | unsigned long tlb_word2_i_value; |
| 37 | } region_t; |
| 38 | |
| 39 | static int add_tlb_entry(unsigned long base_addr, |
| 40 | unsigned long tlb_word0_size_value, |
| 41 | unsigned long tlb_word2_i_value) |
| 42 | { |
| 43 | int i; |
| 44 | unsigned long tlb_word0_value; |
| 45 | unsigned long tlb_word1_value; |
| 46 | unsigned long tlb_word2_value; |
| 47 | |
| 48 | /* First, find the index of a TLB entry not being used */ |
| 49 | for (i=0; i<PPC4XX_TLB_SIZE; i++) { |
| 50 | tlb_word0_value = mftlb1(i); |
| 51 | if ((tlb_word0_value & TLB_WORD0_V_MASK) == TLB_WORD0_V_DISABLE) |
| 52 | break; |
| 53 | } |
| 54 | if (i >= PPC4XX_TLB_SIZE) |
| 55 | return -1; |
| 56 | |
| 57 | /* Second, create the TLB entry */ |
| 58 | tlb_word0_value = TLB_WORD0_EPN_ENCODE(base_addr) | TLB_WORD0_V_ENABLE | |
| 59 | TLB_WORD0_TS_0 | tlb_word0_size_value; |
| 60 | tlb_word1_value = TLB_WORD1_RPN_ENCODE(base_addr) | TLB_WORD1_ERPN_ENCODE(0); |
| 61 | tlb_word2_value = TLB_WORD2_U0_DISABLE | TLB_WORD2_U1_DISABLE | |
| 62 | TLB_WORD2_U2_DISABLE | TLB_WORD2_U3_DISABLE | |
| 63 | TLB_WORD2_W_DISABLE | tlb_word2_i_value | |
| 64 | TLB_WORD2_M_DISABLE | TLB_WORD2_G_DISABLE | |
| 65 | TLB_WORD2_E_DISABLE | TLB_WORD2_UX_ENABLE | |
| 66 | TLB_WORD2_UW_ENABLE | TLB_WORD2_UR_ENABLE | |
| 67 | TLB_WORD2_SX_ENABLE | TLB_WORD2_SW_ENABLE | |
| 68 | TLB_WORD2_SR_ENABLE; |
| 69 | |
| 70 | /* Wait for all memory accesses to complete */ |
| 71 | sync(); |
| 72 | |
| 73 | /* Third, add the TLB entries */ |
| 74 | mttlb1(i, tlb_word0_value); |
| 75 | mttlb2(i, tlb_word1_value); |
| 76 | mttlb3(i, tlb_word2_value); |
| 77 | |
| 78 | /* Execute an ISYNC instruction so that the new TLB entry takes effect */ |
| 79 | asm("isync"); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static void program_tlb_addr(unsigned long base_addr, unsigned long mem_size, |
| 85 | unsigned long tlb_word2_i_value) |
| 86 | { |
| 87 | int rc; |
| 88 | int tlb_i; |
| 89 | |
| 90 | tlb_i = tlb_word2_i_value; |
| 91 | while (mem_size != 0) { |
| 92 | rc = 0; |
| 93 | /* Add the TLB entries in to map the region. */ |
| 94 | if (((base_addr & TLB_256MB_ALIGN_MASK) == base_addr) && |
| 95 | (mem_size >= TLB_256MB_SIZE)) { |
| 96 | /* Add a 256MB TLB entry */ |
| 97 | if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_256MB, tlb_i)) == 0) { |
| 98 | mem_size -= TLB_256MB_SIZE; |
| 99 | base_addr += TLB_256MB_SIZE; |
| 100 | } |
| 101 | } else if (((base_addr & TLB_16MB_ALIGN_MASK) == base_addr) && |
| 102 | (mem_size >= TLB_16MB_SIZE)) { |
| 103 | /* Add a 16MB TLB entry */ |
| 104 | if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_16MB, tlb_i)) == 0) { |
| 105 | mem_size -= TLB_16MB_SIZE; |
| 106 | base_addr += TLB_16MB_SIZE; |
| 107 | } |
| 108 | } else if (((base_addr & TLB_1MB_ALIGN_MASK) == base_addr) && |
| 109 | (mem_size >= TLB_1MB_SIZE)) { |
| 110 | /* Add a 1MB TLB entry */ |
| 111 | if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_1MB, tlb_i)) == 0) { |
| 112 | mem_size -= TLB_1MB_SIZE; |
| 113 | base_addr += TLB_1MB_SIZE; |
| 114 | } |
| 115 | } else if (((base_addr & TLB_256KB_ALIGN_MASK) == base_addr) && |
| 116 | (mem_size >= TLB_256KB_SIZE)) { |
| 117 | /* Add a 256KB TLB entry */ |
| 118 | if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_256KB, tlb_i)) == 0) { |
| 119 | mem_size -= TLB_256KB_SIZE; |
| 120 | base_addr += TLB_256KB_SIZE; |
| 121 | } |
| 122 | } else if (((base_addr & TLB_64KB_ALIGN_MASK) == base_addr) && |
| 123 | (mem_size >= TLB_64KB_SIZE)) { |
| 124 | /* Add a 64KB TLB entry */ |
| 125 | if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_64KB, tlb_i)) == 0) { |
| 126 | mem_size -= TLB_64KB_SIZE; |
| 127 | base_addr += TLB_64KB_SIZE; |
| 128 | } |
| 129 | } else if (((base_addr & TLB_16KB_ALIGN_MASK) == base_addr) && |
| 130 | (mem_size >= TLB_16KB_SIZE)) { |
| 131 | /* Add a 16KB TLB entry */ |
| 132 | if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_16KB, tlb_i)) == 0) { |
| 133 | mem_size -= TLB_16KB_SIZE; |
| 134 | base_addr += TLB_16KB_SIZE; |
| 135 | } |
| 136 | } else if (((base_addr & TLB_4KB_ALIGN_MASK) == base_addr) && |
| 137 | (mem_size >= TLB_4KB_SIZE)) { |
| 138 | /* Add a 4KB TLB entry */ |
| 139 | if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_4KB, tlb_i)) == 0) { |
| 140 | mem_size -= TLB_4KB_SIZE; |
| 141 | base_addr += TLB_4KB_SIZE; |
| 142 | } |
| 143 | } else if (((base_addr & TLB_1KB_ALIGN_MASK) == base_addr) && |
| 144 | (mem_size >= TLB_1KB_SIZE)) { |
| 145 | /* Add a 1KB TLB entry */ |
| 146 | if ((rc = add_tlb_entry(base_addr, TLB_WORD0_SIZE_1KB, tlb_i)) == 0) { |
| 147 | mem_size -= TLB_1KB_SIZE; |
| 148 | base_addr += TLB_1KB_SIZE; |
| 149 | } |
| 150 | } else { |
| 151 | printf("ERROR: no TLB size exists for the base address 0x%0X.\n", |
| 152 | base_addr); |
| 153 | } |
| 154 | |
| 155 | if (rc != 0) |
| 156 | printf("ERROR: no TLB entries available for the base addr 0x%0X.\n", |
| 157 | base_addr); |
| 158 | } |
| 159 | |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Program one (or multiple) TLB entries for one memory region |
| 165 | * |
| 166 | * Common usage for boards with SDRAM DIMM modules to dynamically |
| 167 | * configure the TLB's for the SDRAM |
| 168 | */ |
| 169 | void program_tlb(u32 start, u32 size) |
| 170 | { |
| 171 | region_t region_array; |
| 172 | |
| 173 | region_array.base = start; |
| 174 | region_array.size = size; |
| 175 | region_array.tlb_word2_i_value = TLB_WORD2_I_ENABLE; /* disable cache (for now) */ |
| 176 | |
| 177 | /* Call the routine to add in the tlb entries for the memory regions */ |
| 178 | program_tlb_addr(region_array.base, region_array.size, |
| 179 | region_array.tlb_word2_i_value); |
| 180 | |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | #endif /* CONFIG_440 */ |