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