Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 1 | /* |
Kumar Gala | 16a276e | 2010-03-30 23:06:53 -0500 | [diff] [blame] | 2 | * Copyright 2008-2010 Freescale Semiconductor, Inc. |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -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/fsl_law.h> |
| 28 | #include <asm/io.h> |
| 29 | |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 32 | /* number of LAWs in the hw implementation */ |
| 33 | #if defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || \ |
| 34 | defined(CONFIG_MPC8560) || defined(CONFIG_MPC8555) |
| 35 | #define FSL_HW_NUM_LAWS 8 |
| 36 | #elif defined(CONFIG_MPC8548) || defined(CONFIG_MPC8544) || \ |
Haiying Wang | c984913 | 2009-03-27 17:02:44 -0400 | [diff] [blame] | 37 | defined(CONFIG_MPC8568) || defined(CONFIG_MPC8569) || \ |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 38 | defined(CONFIG_MPC8641) || defined(CONFIG_MPC8610) |
| 39 | #define FSL_HW_NUM_LAWS 10 |
Srikanth Srinivasan | a864f32 | 2009-01-21 17:17:33 -0600 | [diff] [blame] | 40 | #elif defined(CONFIG_MPC8536) || defined(CONFIG_MPC8572) || \ |
Poonam Aggrwal | 13e21b1 | 2009-08-20 18:57:45 +0530 | [diff] [blame] | 41 | defined(CONFIG_P1011) || defined(CONFIG_P1020) || \ |
Kumar Gala | 16a276e | 2010-03-30 23:06:53 -0500 | [diff] [blame] | 42 | defined(CONFIG_P1012) || defined(CONFIG_P1021) || \ |
| 43 | defined(CONFIG_P1013) || defined(CONFIG_P1022) || \ |
Poonam Aggrwal | 13e21b1 | 2009-08-20 18:57:45 +0530 | [diff] [blame] | 44 | defined(CONFIG_P2010) || defined(CONFIG_P2020) |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 45 | #define FSL_HW_NUM_LAWS 12 |
Kumar Gala | bb5409c | 2009-03-19 02:39:17 -0500 | [diff] [blame] | 46 | #elif defined(CONFIG_PPC_P4080) |
| 47 | #define FSL_HW_NUM_LAWS 32 |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 48 | #else |
| 49 | #error FSL_HW_NUM_LAWS not defined for this platform |
| 50 | #endif |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 51 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 52 | #ifdef CONFIG_FSL_CORENET |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 53 | void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id) |
| 54 | { |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 55 | volatile ccsr_local_t *ccm = (void *)(CONFIG_SYS_FSL_CORENET_CCM_ADDR); |
| 56 | |
| 57 | gd->used_laws |= (1 << idx); |
| 58 | |
| 59 | out_be32(&ccm->law[idx].lawar, 0); |
| 60 | out_be32(&ccm->law[idx].lawbarh, ((u64)addr >> 32)); |
| 61 | out_be32(&ccm->law[idx].lawbarl, addr & 0xffffffff); |
| 62 | out_be32(&ccm->law[idx].lawar, LAW_EN | ((u32)id << 20) | (u32)sz); |
| 63 | |
| 64 | /* Read back so that we sync the writes */ |
| 65 | in_be32(&ccm->law[idx].lawar); |
| 66 | } |
| 67 | |
| 68 | void disable_law(u8 idx) |
| 69 | { |
| 70 | volatile ccsr_local_t *ccm = (void *)(CONFIG_SYS_FSL_CORENET_CCM_ADDR); |
| 71 | |
| 72 | gd->used_laws &= ~(1 << idx); |
| 73 | |
| 74 | out_be32(&ccm->law[idx].lawar, 0); |
| 75 | out_be32(&ccm->law[idx].lawbarh, 0); |
| 76 | out_be32(&ccm->law[idx].lawbarl, 0); |
| 77 | |
| 78 | /* Read back so that we sync the writes */ |
| 79 | in_be32(&ccm->law[idx].lawar); |
| 80 | |
| 81 | return; |
| 82 | } |
| 83 | |
Kumar Gala | 93f5381 | 2009-09-30 08:39:44 -0500 | [diff] [blame] | 84 | #ifndef CONFIG_NAND_SPL |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 85 | static int get_law_entry(u8 i, struct law_entry *e) |
| 86 | { |
| 87 | volatile ccsr_local_t *ccm = (void *)(CONFIG_SYS_FSL_CORENET_CCM_ADDR); |
| 88 | u32 lawar; |
| 89 | |
| 90 | lawar = in_be32(&ccm->law[i].lawar); |
| 91 | |
| 92 | if (!(lawar & LAW_EN)) |
| 93 | return 0; |
| 94 | |
| 95 | e->addr = ((u64)in_be32(&ccm->law[i].lawbarh) << 32) | |
| 96 | in_be32(&ccm->law[i].lawbarl); |
| 97 | e->size = lawar & 0x3f; |
| 98 | e->trgt_id = (lawar >> 20) & 0xff; |
| 99 | |
| 100 | return 1; |
| 101 | } |
Kumar Gala | 93f5381 | 2009-09-30 08:39:44 -0500 | [diff] [blame] | 102 | #endif |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 103 | #else |
| 104 | void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id) |
| 105 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 106 | volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08); |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 107 | volatile u32 *lawbar = base + 8 * idx; |
| 108 | volatile u32 *lawar = base + 8 * idx + 2; |
| 109 | |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 110 | gd->used_laws |= (1 << idx); |
| 111 | |
Ed Swarthout | cf92b98 | 2008-10-09 01:25:55 -0500 | [diff] [blame] | 112 | out_be32(lawar, 0); |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 113 | out_be32(lawbar, addr >> 12); |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 114 | out_be32(lawar, LAW_EN | ((u32)id << 20) | (u32)sz); |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 115 | |
Timur Tabi | 52fa71f | 2009-09-04 17:05:24 -0500 | [diff] [blame] | 116 | /* Read back so that we sync the writes */ |
| 117 | in_be32(lawar); |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 118 | } |
| 119 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 120 | void disable_law(u8 idx) |
| 121 | { |
| 122 | volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08); |
| 123 | volatile u32 *lawbar = base + 8 * idx; |
| 124 | volatile u32 *lawar = base + 8 * idx + 2; |
| 125 | |
| 126 | gd->used_laws &= ~(1 << idx); |
| 127 | |
| 128 | out_be32(lawar, 0); |
| 129 | out_be32(lawbar, 0); |
| 130 | |
| 131 | /* Read back so that we sync the writes */ |
| 132 | in_be32(lawar); |
| 133 | |
| 134 | return; |
| 135 | } |
| 136 | |
Kumar Gala | 93f5381 | 2009-09-30 08:39:44 -0500 | [diff] [blame] | 137 | #ifndef CONFIG_NAND_SPL |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 138 | static int get_law_entry(u8 i, struct law_entry *e) |
| 139 | { |
| 140 | volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08); |
| 141 | volatile u32 *lawbar = base + 8 * i; |
| 142 | volatile u32 *lawar = base + 8 * i + 2; |
| 143 | u32 temp; |
| 144 | |
| 145 | temp = in_be32(lawar); |
| 146 | |
| 147 | if (!(temp & LAW_EN)) |
| 148 | return 0; |
| 149 | |
| 150 | e->addr = (u64)in_be32(lawbar) << 12; |
| 151 | e->size = temp & 0x3f; |
| 152 | e->trgt_id = (temp >> 20) & 0xff; |
| 153 | |
| 154 | return 1; |
| 155 | } |
| 156 | #endif |
Kumar Gala | 93f5381 | 2009-09-30 08:39:44 -0500 | [diff] [blame] | 157 | #endif |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 158 | |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 159 | int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id) |
| 160 | { |
| 161 | u32 idx = ffz(gd->used_laws); |
| 162 | |
| 163 | if (idx >= FSL_HW_NUM_LAWS) |
| 164 | return -1; |
| 165 | |
| 166 | set_law(idx, addr, sz, id); |
| 167 | |
| 168 | return idx; |
| 169 | } |
| 170 | |
Mingkai Hu | 0255cd7 | 2009-09-11 14:19:10 +0800 | [diff] [blame] | 171 | #ifndef CONFIG_NAND_SPL |
Kumar Gala | a9abb00 | 2008-06-10 16:16:02 -0500 | [diff] [blame] | 172 | int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id) |
| 173 | { |
| 174 | u32 idx; |
| 175 | |
| 176 | /* we have no LAWs free */ |
| 177 | if (gd->used_laws == -1) |
| 178 | return -1; |
| 179 | |
| 180 | /* grab the last free law */ |
| 181 | idx = __ilog2(~(gd->used_laws)); |
| 182 | |
| 183 | if (idx >= FSL_HW_NUM_LAWS) |
| 184 | return -1; |
| 185 | |
| 186 | set_law(idx, addr, sz, id); |
| 187 | |
| 188 | return idx; |
| 189 | } |
| 190 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 191 | struct law_entry find_law(phys_addr_t addr) |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 192 | { |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 193 | struct law_entry entry; |
| 194 | int i; |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 195 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 196 | entry.index = -1; |
| 197 | entry.addr = 0; |
| 198 | entry.size = 0; |
| 199 | entry.trgt_id = 0; |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 200 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 201 | for (i = 0; i < FSL_HW_NUM_LAWS; i++) { |
| 202 | u64 upper; |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 203 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 204 | if (!get_law_entry(i, &entry)) |
| 205 | continue; |
| 206 | |
| 207 | upper = entry.addr + (2ull << entry.size); |
| 208 | if ((addr >= entry.addr) && (addr < upper)) { |
| 209 | entry.index = i; |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return entry; |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 215 | } |
| 216 | |
Becky Bruce | 2a15f75 | 2008-01-23 16:31:05 -0600 | [diff] [blame] | 217 | void print_laws(void) |
| 218 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 219 | volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08); |
Becky Bruce | 2a15f75 | 2008-01-23 16:31:05 -0600 | [diff] [blame] | 220 | volatile u32 *lawbar = base; |
| 221 | volatile u32 *lawar = base + 2; |
| 222 | int i; |
| 223 | |
| 224 | printf("\nLocal Access Window Configuration\n"); |
| 225 | for(i = 0; i < FSL_HW_NUM_LAWS; i++) { |
| 226 | printf("\tLAWBAR%d : 0x%08x, LAWAR%d : 0x%08x\n", |
| 227 | i, in_be32(lawbar), i, in_be32(lawar)); |
| 228 | lawbar += 8; |
| 229 | lawar += 8; |
| 230 | } |
| 231 | |
| 232 | return; |
| 233 | } |
| 234 | |
Kumar Gala | 61ed053 | 2008-08-26 15:01:28 -0500 | [diff] [blame] | 235 | /* use up to 2 LAWs for DDR, used the last available LAWs */ |
| 236 | int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id) |
| 237 | { |
| 238 | u64 start_align, law_sz; |
| 239 | int law_sz_enc; |
| 240 | |
| 241 | if (start == 0) |
| 242 | start_align = 1ull << (LAW_SIZE_32G + 1); |
| 243 | else |
| 244 | start_align = 1ull << (ffs64(start) - 1); |
| 245 | law_sz = min(start_align, sz); |
| 246 | law_sz_enc = __ilog2_u64(law_sz) - 1; |
| 247 | |
| 248 | if (set_last_law(start, law_sz_enc, id) < 0) |
| 249 | return -1; |
| 250 | |
Kumar Gala | 894cc88 | 2009-04-04 10:21:02 -0500 | [diff] [blame] | 251 | /* recalculate size based on what was actually covered by the law */ |
| 252 | law_sz = 1ull << __ilog2_u64(law_sz); |
| 253 | |
Kumar Gala | 61ed053 | 2008-08-26 15:01:28 -0500 | [diff] [blame] | 254 | /* do we still have anything to map */ |
| 255 | sz = sz - law_sz; |
| 256 | if (sz) { |
| 257 | start += law_sz; |
| 258 | |
| 259 | start_align = 1ull << (ffs64(start) - 1); |
| 260 | law_sz = min(start_align, sz); |
| 261 | law_sz_enc = __ilog2_u64(law_sz) - 1; |
| 262 | |
| 263 | if (set_last_law(start, law_sz_enc, id) < 0) |
| 264 | return -1; |
| 265 | } else { |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | /* do we still have anything to map */ |
| 270 | sz = sz - law_sz; |
| 271 | if (sz) |
| 272 | return 1; |
| 273 | |
| 274 | return 0; |
| 275 | } |
Mingkai Hu | 0255cd7 | 2009-09-11 14:19:10 +0800 | [diff] [blame] | 276 | #endif |
Kumar Gala | 61ed053 | 2008-08-26 15:01:28 -0500 | [diff] [blame] | 277 | |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 278 | void init_laws(void) |
| 279 | { |
| 280 | int i; |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 281 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 282 | #if FSL_HW_NUM_LAWS < 32 |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 283 | gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1); |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 284 | #elif FSL_HW_NUM_LAWS == 32 |
| 285 | gd->used_laws = 0; |
| 286 | #else |
| 287 | #error FSL_HW_NUM_LAWS can not be greater than 32 w/o code changes |
| 288 | #endif |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 289 | |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 290 | for (i = 0; i < num_law_entries; i++) { |
| 291 | if (law_table[i].index == -1) |
| 292 | set_next_law(law_table[i].addr, law_table[i].size, |
| 293 | law_table[i].trgt_id); |
| 294 | else |
| 295 | set_law(law_table[i].index, law_table[i].addr, |
| 296 | law_table[i].size, law_table[i].trgt_id); |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | return ; |
| 300 | } |