Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 1 | /* |
Poonam Aggrwal | b8cdd01 | 2011-01-13 21:39:27 +0530 | [diff] [blame] | 2 | * Copyright 2008-2011 Freescale Semiconductor, Inc. |
Kumar Gala | 83d40df | 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 | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Kumar Gala | f060054 | 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 | 22b6dbc | 2009-03-27 17:02:44 -0400 | [diff] [blame] | 37 | defined(CONFIG_MPC8568) || defined(CONFIG_MPC8569) || \ |
Kumar Gala | f060054 | 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 | 8d949af | 2009-01-21 17:17:33 -0600 | [diff] [blame] | 40 | #elif defined(CONFIG_MPC8536) || defined(CONFIG_MPC8572) || \ |
Poonam Aggrwal | b5debec | 2011-01-13 21:40:05 +0530 | [diff] [blame] | 41 | defined(CONFIG_P1010) || defined(CONFIG_P1014) || \ |
Poonam Aggrwal | a713ba9 | 2009-08-20 18:57:45 +0530 | [diff] [blame] | 42 | defined(CONFIG_P1011) || defined(CONFIG_P1020) || \ |
Kumar Gala | 2160827 | 2010-03-30 23:06:53 -0500 | [diff] [blame] | 43 | defined(CONFIG_P1012) || defined(CONFIG_P1021) || \ |
| 44 | defined(CONFIG_P1013) || defined(CONFIG_P1022) || \ |
Poonam Aggrwal | a713ba9 | 2009-08-20 18:57:45 +0530 | [diff] [blame] | 45 | defined(CONFIG_P2010) || defined(CONFIG_P2020) |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 46 | #define FSL_HW_NUM_LAWS 12 |
Kumar Gala | f193e3d | 2010-06-01 10:29:11 -0500 | [diff] [blame] | 47 | #elif defined(CONFIG_PPC_P2040) || defined(CONFIG_PPC_P3041) || \ |
| 48 | defined(CONFIG_PPC_P4080) || defined(CONFIG_PPC_P5020) |
Kumar Gala | 7e4259b | 2009-03-19 02:39:17 -0500 | [diff] [blame] | 49 | #define FSL_HW_NUM_LAWS 32 |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 50 | #else |
| 51 | #error FSL_HW_NUM_LAWS not defined for this platform |
| 52 | #endif |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 53 | |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 54 | #ifdef CONFIG_FSL_CORENET |
Becky Bruce | e71755f | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 55 | #define LAW_BASE (CONFIG_SYS_FSL_CORENET_CCM_ADDR) |
| 56 | #define LAWAR_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawar) |
| 57 | #define LAWBARH_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarh) |
| 58 | #define LAWBARL_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarl) |
| 59 | #define LAWBAR_SHIFT 0 |
| 60 | #else |
| 61 | #define LAW_BASE (CONFIG_SYS_IMMR + 0xc08) |
| 62 | #define LAWAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x + 2) |
| 63 | #define LAWBAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x) |
| 64 | #define LAWBAR_SHIFT 12 |
| 65 | #endif |
| 66 | |
| 67 | |
| 68 | static inline phys_addr_t get_law_base_addr(int idx) |
| 69 | { |
| 70 | #ifdef CONFIG_FSL_CORENET |
| 71 | return (phys_addr_t) |
| 72 | ((u64)in_be32(LAWBARH_ADDR(idx)) << 32) | |
| 73 | in_be32(LAWBARL_ADDR(idx)); |
| 74 | #else |
| 75 | return (phys_addr_t)in_be32(LAWBAR_ADDR(idx)) << LAWBAR_SHIFT; |
| 76 | #endif |
| 77 | } |
| 78 | |
| 79 | static inline void set_law_base_addr(int idx, phys_addr_t addr) |
| 80 | { |
| 81 | #ifdef CONFIG_FSL_CORENET |
| 82 | out_be32(LAWBARL_ADDR(idx), addr & 0xffffffff); |
| 83 | out_be32(LAWBARH_ADDR(idx), (u64)addr >> 32); |
| 84 | #else |
| 85 | out_be32(LAWBAR_ADDR(idx), addr >> LAWBAR_SHIFT); |
| 86 | #endif |
| 87 | } |
| 88 | |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 89 | void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id) |
| 90 | { |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 91 | gd->used_laws |= (1 << idx); |
| 92 | |
Becky Bruce | e71755f | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 93 | out_be32(LAWAR_ADDR(idx), 0); |
| 94 | set_law_base_addr(idx, addr); |
| 95 | out_be32(LAWAR_ADDR(idx), LAW_EN | ((u32)id << 20) | (u32)sz); |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 96 | |
| 97 | /* Read back so that we sync the writes */ |
Becky Bruce | e71755f | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 98 | in_be32(LAWAR_ADDR(idx)); |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void disable_law(u8 idx) |
| 102 | { |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 103 | gd->used_laws &= ~(1 << idx); |
| 104 | |
Becky Bruce | e71755f | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 105 | out_be32(LAWAR_ADDR(idx), 0); |
| 106 | set_law_base_addr(idx, 0); |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 107 | |
| 108 | /* Read back so that we sync the writes */ |
Becky Bruce | e71755f | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 109 | in_be32(LAWAR_ADDR(idx)); |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 110 | |
| 111 | return; |
| 112 | } |
| 113 | |
Kumar Gala | 24b17d8 | 2009-09-30 08:39:44 -0500 | [diff] [blame] | 114 | #ifndef CONFIG_NAND_SPL |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 115 | static int get_law_entry(u8 i, struct law_entry *e) |
| 116 | { |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 117 | u32 lawar; |
| 118 | |
Becky Bruce | e71755f | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 119 | lawar = in_be32(LAWAR_ADDR(i)); |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 120 | |
| 121 | if (!(lawar & LAW_EN)) |
| 122 | return 0; |
| 123 | |
Becky Bruce | e71755f | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 124 | e->addr = get_law_base_addr(i); |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 125 | e->size = lawar & 0x3f; |
| 126 | e->trgt_id = (lawar >> 20) & 0xff; |
| 127 | |
| 128 | return 1; |
| 129 | } |
Kumar Gala | 24b17d8 | 2009-09-30 08:39:44 -0500 | [diff] [blame] | 130 | #endif |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 131 | |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 132 | int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id) |
| 133 | { |
| 134 | u32 idx = ffz(gd->used_laws); |
| 135 | |
| 136 | if (idx >= FSL_HW_NUM_LAWS) |
| 137 | return -1; |
| 138 | |
| 139 | set_law(idx, addr, sz, id); |
| 140 | |
| 141 | return idx; |
| 142 | } |
| 143 | |
Mingkai Hu | 7da5335 | 2009-09-11 14:19:10 +0800 | [diff] [blame] | 144 | #ifndef CONFIG_NAND_SPL |
Kumar Gala | ba04f70 | 2008-06-10 16:16:02 -0500 | [diff] [blame] | 145 | int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id) |
| 146 | { |
| 147 | u32 idx; |
| 148 | |
| 149 | /* we have no LAWs free */ |
| 150 | if (gd->used_laws == -1) |
| 151 | return -1; |
| 152 | |
| 153 | /* grab the last free law */ |
| 154 | idx = __ilog2(~(gd->used_laws)); |
| 155 | |
| 156 | if (idx >= FSL_HW_NUM_LAWS) |
| 157 | return -1; |
| 158 | |
| 159 | set_law(idx, addr, sz, id); |
| 160 | |
| 161 | return idx; |
| 162 | } |
| 163 | |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 164 | struct law_entry find_law(phys_addr_t addr) |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 165 | { |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 166 | struct law_entry entry; |
| 167 | int i; |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 168 | |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 169 | entry.index = -1; |
| 170 | entry.addr = 0; |
| 171 | entry.size = 0; |
| 172 | entry.trgt_id = 0; |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 173 | |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 174 | for (i = 0; i < FSL_HW_NUM_LAWS; i++) { |
| 175 | u64 upper; |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 176 | |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 177 | if (!get_law_entry(i, &entry)) |
| 178 | continue; |
| 179 | |
| 180 | upper = entry.addr + (2ull << entry.size); |
| 181 | if ((addr >= entry.addr) && (addr < upper)) { |
| 182 | entry.index = i; |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return entry; |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 188 | } |
| 189 | |
Becky Bruce | ddcebcb | 2008-01-23 16:31:05 -0600 | [diff] [blame] | 190 | void print_laws(void) |
| 191 | { |
Becky Bruce | ddcebcb | 2008-01-23 16:31:05 -0600 | [diff] [blame] | 192 | int i; |
Becky Bruce | e71755f | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 193 | u32 lawar; |
Becky Bruce | ddcebcb | 2008-01-23 16:31:05 -0600 | [diff] [blame] | 194 | |
| 195 | printf("\nLocal Access Window Configuration\n"); |
Becky Bruce | e71755f | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 196 | for (i = 0; i < FSL_HW_NUM_LAWS; i++) { |
| 197 | lawar = in_be32(LAWAR_ADDR(i)); |
Becky Bruce | 11a3de4 | 2010-06-17 11:37:24 -0500 | [diff] [blame] | 198 | #ifdef CONFIG_FSL_CORENET |
| 199 | printf("LAWBARH%02d: 0x%08x LAWBARL%02d: 0x%08x", |
| 200 | i, in_be32(LAWBARH_ADDR(i)), |
| 201 | i, in_be32(LAWBARL_ADDR(i))); |
| 202 | #else |
Becky Bruce | e71755f | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 203 | printf("LAWBAR%02d: 0x%08x", i, in_be32(LAWBAR_ADDR(i))); |
Becky Bruce | 11a3de4 | 2010-06-17 11:37:24 -0500 | [diff] [blame] | 204 | #endif |
Becky Bruce | e71755f | 2010-06-17 11:37:23 -0500 | [diff] [blame] | 205 | printf(" LAWAR0x%02d: 0x%08x\n", i, lawar); |
| 206 | printf("\t(EN: %d TGT: 0x%02x SIZE: ", |
| 207 | (lawar & LAW_EN) ? 1 : 0, (lawar >> 20) & 0xff); |
| 208 | print_size(lawar_size(lawar), ")\n"); |
Becky Bruce | ddcebcb | 2008-01-23 16:31:05 -0600 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | return; |
| 212 | } |
| 213 | |
Kumar Gala | f784e32 | 2008-08-26 15:01:28 -0500 | [diff] [blame] | 214 | /* use up to 2 LAWs for DDR, used the last available LAWs */ |
| 215 | int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id) |
| 216 | { |
| 217 | u64 start_align, law_sz; |
| 218 | int law_sz_enc; |
| 219 | |
| 220 | if (start == 0) |
| 221 | start_align = 1ull << (LAW_SIZE_32G + 1); |
| 222 | else |
| 223 | start_align = 1ull << (ffs64(start) - 1); |
| 224 | law_sz = min(start_align, sz); |
| 225 | law_sz_enc = __ilog2_u64(law_sz) - 1; |
| 226 | |
| 227 | if (set_last_law(start, law_sz_enc, id) < 0) |
| 228 | return -1; |
| 229 | |
Kumar Gala | e6a6789 | 2009-04-04 10:21:02 -0500 | [diff] [blame] | 230 | /* recalculate size based on what was actually covered by the law */ |
| 231 | law_sz = 1ull << __ilog2_u64(law_sz); |
| 232 | |
Kumar Gala | f784e32 | 2008-08-26 15:01:28 -0500 | [diff] [blame] | 233 | /* do we still have anything to map */ |
| 234 | sz = sz - law_sz; |
| 235 | if (sz) { |
| 236 | start += law_sz; |
| 237 | |
| 238 | start_align = 1ull << (ffs64(start) - 1); |
| 239 | law_sz = min(start_align, sz); |
| 240 | law_sz_enc = __ilog2_u64(law_sz) - 1; |
| 241 | |
| 242 | if (set_last_law(start, law_sz_enc, id) < 0) |
| 243 | return -1; |
| 244 | } else { |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | /* do we still have anything to map */ |
| 249 | sz = sz - law_sz; |
| 250 | if (sz) |
| 251 | return 1; |
| 252 | |
| 253 | return 0; |
| 254 | } |
Mingkai Hu | 7da5335 | 2009-09-11 14:19:10 +0800 | [diff] [blame] | 255 | #endif |
Kumar Gala | f784e32 | 2008-08-26 15:01:28 -0500 | [diff] [blame] | 256 | |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 257 | void init_laws(void) |
| 258 | { |
| 259 | int i; |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 260 | |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 261 | #if FSL_HW_NUM_LAWS < 32 |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 262 | gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1); |
Kumar Gala | 418ec85 | 2009-03-19 02:32:23 -0500 | [diff] [blame] | 263 | #elif FSL_HW_NUM_LAWS == 32 |
| 264 | gd->used_laws = 0; |
| 265 | #else |
| 266 | #error FSL_HW_NUM_LAWS can not be greater than 32 w/o code changes |
| 267 | #endif |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 268 | |
| 269 | for (i = 0; i < num_law_entries; i++) { |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 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 | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | return ; |
| 279 | } |