Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 1 | /* |
Poonam Aggrwal | 2ba3ee0 | 2011-01-13 21:39:27 +0530 | [diff] [blame] | 2 | * Copyright 2008-2011 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> |
Kumar Gala | 6a74ade | 2011-02-03 09:02:13 -0600 | [diff] [blame] | 27 | #include <linux/compiler.h> |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 28 | #include <asm/fsl_law.h> |
| 29 | #include <asm/io.h> |
| 30 | |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 31 | DECLARE_GLOBAL_DATA_PTR; |
| 32 | |
Kumar Gala | fe13711 | 2011-01-19 03:05:26 -0600 | [diff] [blame] | 33 | #define FSL_HW_NUM_LAWS CONFIG_SYS_FSL_NUM_LAWS |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 34 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 35 | #ifdef CONFIG_FSL_CORENET |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 36 | #define LAW_BASE (CONFIG_SYS_FSL_CORENET_CCM_ADDR) |
| 37 | #define LAWAR_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawar) |
| 38 | #define LAWBARH_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarh) |
| 39 | #define LAWBARL_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarl) |
| 40 | #define LAWBAR_SHIFT 0 |
| 41 | #else |
| 42 | #define LAW_BASE (CONFIG_SYS_IMMR + 0xc08) |
| 43 | #define LAWAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x + 2) |
| 44 | #define LAWBAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x) |
| 45 | #define LAWBAR_SHIFT 12 |
| 46 | #endif |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 47 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 48 | |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 49 | static inline phys_addr_t get_law_base_addr(int idx) |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 50 | { |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 51 | #ifdef CONFIG_FSL_CORENET |
| 52 | return (phys_addr_t) |
| 53 | ((u64)in_be32(LAWBARH_ADDR(idx)) << 32) | |
| 54 | in_be32(LAWBARL_ADDR(idx)); |
| 55 | #else |
| 56 | return (phys_addr_t)in_be32(LAWBAR_ADDR(idx)) << LAWBAR_SHIFT; |
| 57 | #endif |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 60 | static inline void set_law_base_addr(int idx, phys_addr_t addr) |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 61 | { |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 62 | #ifdef CONFIG_FSL_CORENET |
| 63 | out_be32(LAWBARL_ADDR(idx), addr & 0xffffffff); |
| 64 | out_be32(LAWBARH_ADDR(idx), (u64)addr >> 32); |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 65 | #else |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 66 | out_be32(LAWBAR_ADDR(idx), addr >> LAWBAR_SHIFT); |
| 67 | #endif |
| 68 | } |
| 69 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 70 | void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id) |
| 71 | { |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 72 | gd->used_laws |= (1 << idx); |
| 73 | |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 74 | out_be32(LAWAR_ADDR(idx), 0); |
| 75 | set_law_base_addr(idx, addr); |
| 76 | out_be32(LAWAR_ADDR(idx), LAW_EN | ((u32)id << 20) | (u32)sz); |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 77 | |
Timur Tabi | 52fa71f | 2009-09-04 17:05:24 -0500 | [diff] [blame] | 78 | /* Read back so that we sync the writes */ |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 79 | in_be32(LAWAR_ADDR(idx)); |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 80 | } |
| 81 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 82 | void disable_law(u8 idx) |
| 83 | { |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 84 | gd->used_laws &= ~(1 << idx); |
| 85 | |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 86 | out_be32(LAWAR_ADDR(idx), 0); |
| 87 | set_law_base_addr(idx, 0); |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 88 | |
| 89 | /* Read back so that we sync the writes */ |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 90 | in_be32(LAWAR_ADDR(idx)); |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 91 | |
| 92 | return; |
| 93 | } |
| 94 | |
Kumar Gala | 93f5381 | 2009-09-30 08:39:44 -0500 | [diff] [blame] | 95 | #ifndef CONFIG_NAND_SPL |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 96 | static int get_law_entry(u8 i, struct law_entry *e) |
| 97 | { |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 98 | u32 lawar; |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 99 | |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 100 | lawar = in_be32(LAWAR_ADDR(i)); |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 101 | |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 102 | if (!(lawar & LAW_EN)) |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 103 | return 0; |
| 104 | |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 105 | e->addr = get_law_base_addr(i); |
| 106 | e->size = lawar & 0x3f; |
| 107 | e->trgt_id = (lawar >> 20) & 0xff; |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 108 | |
| 109 | return 1; |
| 110 | } |
| 111 | #endif |
| 112 | |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 113 | int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id) |
| 114 | { |
| 115 | u32 idx = ffz(gd->used_laws); |
| 116 | |
| 117 | if (idx >= FSL_HW_NUM_LAWS) |
| 118 | return -1; |
| 119 | |
| 120 | set_law(idx, addr, sz, id); |
| 121 | |
| 122 | return idx; |
| 123 | } |
| 124 | |
Mingkai Hu | 0255cd7 | 2009-09-11 14:19:10 +0800 | [diff] [blame] | 125 | #ifndef CONFIG_NAND_SPL |
Kumar Gala | a9abb00 | 2008-06-10 16:16:02 -0500 | [diff] [blame] | 126 | int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id) |
| 127 | { |
| 128 | u32 idx; |
| 129 | |
| 130 | /* we have no LAWs free */ |
| 131 | if (gd->used_laws == -1) |
| 132 | return -1; |
| 133 | |
| 134 | /* grab the last free law */ |
| 135 | idx = __ilog2(~(gd->used_laws)); |
| 136 | |
| 137 | if (idx >= FSL_HW_NUM_LAWS) |
| 138 | return -1; |
| 139 | |
| 140 | set_law(idx, addr, sz, id); |
| 141 | |
| 142 | return idx; |
| 143 | } |
| 144 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 145 | struct law_entry find_law(phys_addr_t addr) |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 146 | { |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 147 | struct law_entry entry; |
| 148 | int i; |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 149 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 150 | entry.index = -1; |
| 151 | entry.addr = 0; |
| 152 | entry.size = 0; |
| 153 | entry.trgt_id = 0; |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 154 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 155 | for (i = 0; i < FSL_HW_NUM_LAWS; i++) { |
| 156 | u64 upper; |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 157 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 158 | if (!get_law_entry(i, &entry)) |
| 159 | continue; |
| 160 | |
| 161 | upper = entry.addr + (2ull << entry.size); |
| 162 | if ((addr >= entry.addr) && (addr < upper)) { |
| 163 | entry.index = i; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return entry; |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 169 | } |
| 170 | |
Becky Bruce | 2a15f75 | 2008-01-23 16:31:05 -0600 | [diff] [blame] | 171 | void print_laws(void) |
| 172 | { |
Becky Bruce | 2a15f75 | 2008-01-23 16:31:05 -0600 | [diff] [blame] | 173 | int i; |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 174 | u32 lawar; |
Becky Bruce | 2a15f75 | 2008-01-23 16:31:05 -0600 | [diff] [blame] | 175 | |
| 176 | printf("\nLocal Access Window Configuration\n"); |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 177 | for (i = 0; i < FSL_HW_NUM_LAWS; i++) { |
| 178 | lawar = in_be32(LAWAR_ADDR(i)); |
Becky Bruce | d58f8d5 | 2010-06-17 11:37:24 -0500 | [diff] [blame] | 179 | #ifdef CONFIG_FSL_CORENET |
| 180 | printf("LAWBARH%02d: 0x%08x LAWBARL%02d: 0x%08x", |
| 181 | i, in_be32(LAWBARH_ADDR(i)), |
| 182 | i, in_be32(LAWBARL_ADDR(i))); |
| 183 | #else |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 184 | printf("LAWBAR%02d: 0x%08x", i, in_be32(LAWBAR_ADDR(i))); |
Becky Bruce | d58f8d5 | 2010-06-17 11:37:24 -0500 | [diff] [blame] | 185 | #endif |
Kumar Gala | 978bb79 | 2011-02-12 15:34:08 -0600 | [diff] [blame] | 186 | printf(" LAWAR%02d: 0x%08x\n", i, lawar); |
Becky Bruce | eb891f0 | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 187 | printf("\t(EN: %d TGT: 0x%02x SIZE: ", |
| 188 | (lawar & LAW_EN) ? 1 : 0, (lawar >> 20) & 0xff); |
| 189 | print_size(lawar_size(lawar), ")\n"); |
Becky Bruce | 2a15f75 | 2008-01-23 16:31:05 -0600 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return; |
| 193 | } |
| 194 | |
Kumar Gala | 61ed053 | 2008-08-26 15:01:28 -0500 | [diff] [blame] | 195 | /* use up to 2 LAWs for DDR, used the last available LAWs */ |
| 196 | int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id) |
| 197 | { |
| 198 | u64 start_align, law_sz; |
| 199 | int law_sz_enc; |
| 200 | |
| 201 | if (start == 0) |
| 202 | start_align = 1ull << (LAW_SIZE_32G + 1); |
| 203 | else |
| 204 | start_align = 1ull << (ffs64(start) - 1); |
| 205 | law_sz = min(start_align, sz); |
| 206 | law_sz_enc = __ilog2_u64(law_sz) - 1; |
| 207 | |
| 208 | if (set_last_law(start, law_sz_enc, id) < 0) |
| 209 | return -1; |
| 210 | |
Kumar Gala | 894cc88 | 2009-04-04 10:21:02 -0500 | [diff] [blame] | 211 | /* recalculate size based on what was actually covered by the law */ |
| 212 | law_sz = 1ull << __ilog2_u64(law_sz); |
| 213 | |
Kumar Gala | 61ed053 | 2008-08-26 15:01:28 -0500 | [diff] [blame] | 214 | /* do we still have anything to map */ |
| 215 | sz = sz - law_sz; |
| 216 | if (sz) { |
| 217 | start += law_sz; |
| 218 | |
| 219 | start_align = 1ull << (ffs64(start) - 1); |
| 220 | law_sz = min(start_align, sz); |
| 221 | law_sz_enc = __ilog2_u64(law_sz) - 1; |
| 222 | |
| 223 | if (set_last_law(start, law_sz_enc, id) < 0) |
| 224 | return -1; |
| 225 | } else { |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /* do we still have anything to map */ |
| 230 | sz = sz - law_sz; |
| 231 | if (sz) |
| 232 | return 1; |
| 233 | |
| 234 | return 0; |
| 235 | } |
Mingkai Hu | 0255cd7 | 2009-09-11 14:19:10 +0800 | [diff] [blame] | 236 | #endif |
Kumar Gala | 61ed053 | 2008-08-26 15:01:28 -0500 | [diff] [blame] | 237 | |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 238 | void init_laws(void) |
| 239 | { |
| 240 | int i; |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 241 | |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 242 | #if FSL_HW_NUM_LAWS < 32 |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 243 | gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1); |
Kumar Gala | 65e6c32 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 244 | #elif FSL_HW_NUM_LAWS == 32 |
| 245 | gd->used_laws = 0; |
| 246 | #else |
| 247 | #error FSL_HW_NUM_LAWS can not be greater than 32 w/o code changes |
| 248 | #endif |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 249 | |
Wolfgang Denk | 80f7021 | 2011-05-19 22:21:41 +0200 | [diff] [blame] | 250 | /* |
Kumar Gala | 6a74ade | 2011-02-03 09:02:13 -0600 | [diff] [blame] | 251 | * Any LAWs that were set up before we booted assume they are meant to |
| 252 | * be around and mark them used. |
| 253 | */ |
| 254 | for (i = 0; i < FSL_HW_NUM_LAWS; i++) { |
| 255 | u32 lawar = in_be32(LAWAR_ADDR(i)); |
Wolfgang Denk | 80f7021 | 2011-05-19 22:21:41 +0200 | [diff] [blame] | 256 | |
Kumar Gala | 6a74ade | 2011-02-03 09:02:13 -0600 | [diff] [blame] | 257 | if (lawar & LAW_EN) |
| 258 | gd->used_laws |= (1 << i); |
| 259 | } |
| 260 | |
| 261 | #if defined(CONFIG_NAND_U_BOOT) && !defined(CONFIG_NAND_SPL) |
| 262 | /* |
| 263 | * in NAND boot we've already parsed the law_table and setup those LAWs |
| 264 | * so don't do it again. |
| 265 | */ |
| 266 | return; |
| 267 | #endif |
| 268 | |
Kumar Gala | 75639e0 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 269 | for (i = 0; i < num_law_entries; i++) { |
| 270 | if (law_table[i].index == -1) |
| 271 | set_next_law(law_table[i].addr, law_table[i].size, |
| 272 | law_table[i].trgt_id); |
| 273 | else |
| 274 | set_law(law_table[i].index, law_table[i].addr, |
| 275 | law_table[i].size, law_table[i].trgt_id); |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 276 | } |
| 277 | |
Liu Gang | e86e1a3 | 2012-08-09 05:10:00 +0000 | [diff] [blame^] | 278 | #ifdef CONFIG_SRIOBOOT_SLAVE |
| 279 | /* check RCW to get which port is used for boot */ |
| 280 | ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR; |
| 281 | u32 bootloc = in_be32(&gur->rcwsr[6]); |
| 282 | /* in SRIO boot we need to set specail LAWs for SRIO interfaces */ |
| 283 | switch ((bootloc & FSL_CORENET_RCWSR6_BOOT_LOC) >> 23) { |
| 284 | case 0x8: /* boot from SRIO1 */ |
| 285 | set_next_law(CONFIG_SYS_SRIOBOOT_SLAVE_ADDR_PHYS, |
| 286 | LAW_SIZE_1M, |
| 287 | LAW_TRGT_IF_RIO_1); |
| 288 | set_next_law(CONFIG_SYS_SRIOBOOT_UCODE_ENV_ADDR_PHYS, |
| 289 | LAW_SIZE_1M, |
| 290 | LAW_TRGT_IF_RIO_1); |
| 291 | break; |
| 292 | case 0x9: /* boot from SRIO2 */ |
| 293 | set_next_law(CONFIG_SYS_SRIOBOOT_SLAVE_ADDR_PHYS, |
| 294 | LAW_SIZE_1M, |
| 295 | LAW_TRGT_IF_RIO_2); |
| 296 | set_next_law(CONFIG_SYS_SRIOBOOT_UCODE_ENV_ADDR_PHYS, |
| 297 | LAW_SIZE_1M, |
| 298 | LAW_TRGT_IF_RIO_2); |
| 299 | break; |
| 300 | default: |
| 301 | break; |
| 302 | } |
| 303 | #endif |
| 304 | |
Kumar Gala | 95fd2f6 | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 305 | return ; |
| 306 | } |