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