blob: 55ee12573f711bd94cec7aa71ae7a27a23aaef43 [file] [log] [blame]
Kumar Gala83d40df2008-01-16 01:13:58 -06001/*
Poonam Aggrwalb8cdd012011-01-13 21:39:27 +05302 * Copyright 2008-2011 Freescale Semiconductor, Inc.
Kumar Gala83d40df2008-01-16 01:13:58 -06003 *
4 * (C) Copyright 2000
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Kumar Gala83d40df2008-01-16 01:13:58 -06008 */
9
10#include <common.h>
Kumar Gala76396752011-02-03 09:02:13 -060011#include <linux/compiler.h>
Kumar Gala83d40df2008-01-16 01:13:58 -060012#include <asm/fsl_law.h>
13#include <asm/io.h>
14
Kumar Galaf0600542008-06-11 00:44:10 -050015DECLARE_GLOBAL_DATA_PTR;
16
Kumar Gala243be8e2011-01-19 03:05:26 -060017#define FSL_HW_NUM_LAWS CONFIG_SYS_FSL_NUM_LAWS
Kumar Gala83d40df2008-01-16 01:13:58 -060018
Kumar Gala418ec852009-03-19 02:32:23 -050019#ifdef CONFIG_FSL_CORENET
Becky Brucee71755f2010-06-17 11:37:23 -050020#define LAW_BASE (CONFIG_SYS_FSL_CORENET_CCM_ADDR)
21#define LAWAR_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawar)
22#define LAWBARH_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarh)
23#define LAWBARL_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarl)
24#define LAWBAR_SHIFT 0
25#else
26#define LAW_BASE (CONFIG_SYS_IMMR + 0xc08)
27#define LAWAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x + 2)
28#define LAWBAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x)
29#define LAWBAR_SHIFT 12
30#endif
31
32
33static inline phys_addr_t get_law_base_addr(int idx)
34{
35#ifdef CONFIG_FSL_CORENET
36 return (phys_addr_t)
37 ((u64)in_be32(LAWBARH_ADDR(idx)) << 32) |
38 in_be32(LAWBARL_ADDR(idx));
39#else
40 return (phys_addr_t)in_be32(LAWBAR_ADDR(idx)) << LAWBAR_SHIFT;
41#endif
42}
43
44static inline void set_law_base_addr(int idx, phys_addr_t addr)
45{
46#ifdef CONFIG_FSL_CORENET
47 out_be32(LAWBARL_ADDR(idx), addr & 0xffffffff);
48 out_be32(LAWBARH_ADDR(idx), (u64)addr >> 32);
49#else
50 out_be32(LAWBAR_ADDR(idx), addr >> LAWBAR_SHIFT);
51#endif
52}
53
Kumar Gala418ec852009-03-19 02:32:23 -050054void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
55{
Simon Glass8670dbc2012-12-13 20:48:51 +000056 gd->arch.used_laws |= (1 << idx);
Kumar Gala418ec852009-03-19 02:32:23 -050057
Becky Brucee71755f2010-06-17 11:37:23 -050058 out_be32(LAWAR_ADDR(idx), 0);
59 set_law_base_addr(idx, addr);
60 out_be32(LAWAR_ADDR(idx), LAW_EN | ((u32)id << 20) | (u32)sz);
Kumar Gala418ec852009-03-19 02:32:23 -050061
62 /* Read back so that we sync the writes */
Becky Brucee71755f2010-06-17 11:37:23 -050063 in_be32(LAWAR_ADDR(idx));
Kumar Gala418ec852009-03-19 02:32:23 -050064}
65
66void disable_law(u8 idx)
67{
Simon Glass8670dbc2012-12-13 20:48:51 +000068 gd->arch.used_laws &= ~(1 << idx);
Kumar Gala418ec852009-03-19 02:32:23 -050069
Becky Brucee71755f2010-06-17 11:37:23 -050070 out_be32(LAWAR_ADDR(idx), 0);
71 set_law_base_addr(idx, 0);
Kumar Gala418ec852009-03-19 02:32:23 -050072
73 /* Read back so that we sync the writes */
Becky Brucee71755f2010-06-17 11:37:23 -050074 in_be32(LAWAR_ADDR(idx));
Kumar Gala418ec852009-03-19 02:32:23 -050075
76 return;
77}
78
Ying Zhang0151d992013-08-16 15:16:10 +080079#if !defined(CONFIG_NAND_SPL) && \
80 (!defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SPL_INIT_MINIMAL))
Kumar Gala418ec852009-03-19 02:32:23 -050081static int get_law_entry(u8 i, struct law_entry *e)
82{
Kumar Gala418ec852009-03-19 02:32:23 -050083 u32 lawar;
84
Becky Brucee71755f2010-06-17 11:37:23 -050085 lawar = in_be32(LAWAR_ADDR(i));
Kumar Gala418ec852009-03-19 02:32:23 -050086
87 if (!(lawar & LAW_EN))
88 return 0;
89
Becky Brucee71755f2010-06-17 11:37:23 -050090 e->addr = get_law_base_addr(i);
Kumar Gala418ec852009-03-19 02:32:23 -050091 e->size = lawar & 0x3f;
92 e->trgt_id = (lawar >> 20) & 0xff;
93
94 return 1;
95}
Kumar Gala24b17d82009-09-30 08:39:44 -050096#endif
Kumar Gala418ec852009-03-19 02:32:23 -050097
Kumar Galaf0600542008-06-11 00:44:10 -050098int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
99{
Simon Glass8670dbc2012-12-13 20:48:51 +0000100 u32 idx = ffz(gd->arch.used_laws);
Kumar Galaf0600542008-06-11 00:44:10 -0500101
102 if (idx >= FSL_HW_NUM_LAWS)
103 return -1;
104
105 set_law(idx, addr, sz, id);
106
107 return idx;
108}
109
Ying Zhang0151d992013-08-16 15:16:10 +0800110#if !defined(CONFIG_NAND_SPL) && \
111 (!defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SPL_INIT_MINIMAL))
Kumar Galaba04f702008-06-10 16:16:02 -0500112int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
113{
114 u32 idx;
115
116 /* we have no LAWs free */
Simon Glass8670dbc2012-12-13 20:48:51 +0000117 if (gd->arch.used_laws == -1)
Kumar Galaba04f702008-06-10 16:16:02 -0500118 return -1;
119
120 /* grab the last free law */
Simon Glass8670dbc2012-12-13 20:48:51 +0000121 idx = __ilog2(~(gd->arch.used_laws));
Kumar Galaba04f702008-06-10 16:16:02 -0500122
123 if (idx >= FSL_HW_NUM_LAWS)
124 return -1;
125
126 set_law(idx, addr, sz, id);
127
128 return idx;
129}
130
Kumar Gala418ec852009-03-19 02:32:23 -0500131struct law_entry find_law(phys_addr_t addr)
Kumar Gala83d40df2008-01-16 01:13:58 -0600132{
Kumar Gala418ec852009-03-19 02:32:23 -0500133 struct law_entry entry;
134 int i;
Kumar Gala83d40df2008-01-16 01:13:58 -0600135
Kumar Gala418ec852009-03-19 02:32:23 -0500136 entry.index = -1;
137 entry.addr = 0;
138 entry.size = 0;
139 entry.trgt_id = 0;
Kumar Galaf0600542008-06-11 00:44:10 -0500140
Kumar Gala418ec852009-03-19 02:32:23 -0500141 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
142 u64 upper;
Kumar Gala83d40df2008-01-16 01:13:58 -0600143
Kumar Gala418ec852009-03-19 02:32:23 -0500144 if (!get_law_entry(i, &entry))
145 continue;
146
147 upper = entry.addr + (2ull << entry.size);
148 if ((addr >= entry.addr) && (addr < upper)) {
149 entry.index = i;
150 break;
151 }
152 }
153
154 return entry;
Kumar Gala83d40df2008-01-16 01:13:58 -0600155}
156
Becky Bruceddcebcb2008-01-23 16:31:05 -0600157void print_laws(void)
158{
Becky Bruceddcebcb2008-01-23 16:31:05 -0600159 int i;
Becky Brucee71755f2010-06-17 11:37:23 -0500160 u32 lawar;
Becky Bruceddcebcb2008-01-23 16:31:05 -0600161
162 printf("\nLocal Access Window Configuration\n");
Becky Brucee71755f2010-06-17 11:37:23 -0500163 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
164 lawar = in_be32(LAWAR_ADDR(i));
Becky Bruce11a3de42010-06-17 11:37:24 -0500165#ifdef CONFIG_FSL_CORENET
166 printf("LAWBARH%02d: 0x%08x LAWBARL%02d: 0x%08x",
167 i, in_be32(LAWBARH_ADDR(i)),
168 i, in_be32(LAWBARL_ADDR(i)));
169#else
Becky Brucee71755f2010-06-17 11:37:23 -0500170 printf("LAWBAR%02d: 0x%08x", i, in_be32(LAWBAR_ADDR(i)));
Becky Bruce11a3de42010-06-17 11:37:24 -0500171#endif
Kumar Gala8e29eba2011-02-12 15:34:08 -0600172 printf(" LAWAR%02d: 0x%08x\n", i, lawar);
Becky Brucee71755f2010-06-17 11:37:23 -0500173 printf("\t(EN: %d TGT: 0x%02x SIZE: ",
174 (lawar & LAW_EN) ? 1 : 0, (lawar >> 20) & 0xff);
175 print_size(lawar_size(lawar), ")\n");
Becky Bruceddcebcb2008-01-23 16:31:05 -0600176 }
177
178 return;
179}
180
Kumar Galaf784e322008-08-26 15:01:28 -0500181/* use up to 2 LAWs for DDR, used the last available LAWs */
182int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
183{
184 u64 start_align, law_sz;
185 int law_sz_enc;
186
187 if (start == 0)
188 start_align = 1ull << (LAW_SIZE_32G + 1);
189 else
190 start_align = 1ull << (ffs64(start) - 1);
191 law_sz = min(start_align, sz);
192 law_sz_enc = __ilog2_u64(law_sz) - 1;
193
194 if (set_last_law(start, law_sz_enc, id) < 0)
195 return -1;
196
Kumar Galae6a67892009-04-04 10:21:02 -0500197 /* recalculate size based on what was actually covered by the law */
198 law_sz = 1ull << __ilog2_u64(law_sz);
199
Kumar Galaf784e322008-08-26 15:01:28 -0500200 /* do we still have anything to map */
201 sz = sz - law_sz;
202 if (sz) {
203 start += law_sz;
204
205 start_align = 1ull << (ffs64(start) - 1);
206 law_sz = min(start_align, sz);
207 law_sz_enc = __ilog2_u64(law_sz) - 1;
208
209 if (set_last_law(start, law_sz_enc, id) < 0)
210 return -1;
211 } else {
212 return 0;
213 }
214
215 /* do we still have anything to map */
216 sz = sz - law_sz;
217 if (sz)
218 return 1;
219
220 return 0;
221}
Scott Woodc97cd1b2012-09-20 19:02:18 -0500222#endif /* not SPL */
Kumar Galaf784e322008-08-26 15:01:28 -0500223
Kumar Gala83d40df2008-01-16 01:13:58 -0600224void init_laws(void)
225{
226 int i;
Kumar Galaf0600542008-06-11 00:44:10 -0500227
Kumar Gala418ec852009-03-19 02:32:23 -0500228#if FSL_HW_NUM_LAWS < 32
Simon Glass8670dbc2012-12-13 20:48:51 +0000229 gd->arch.used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
Kumar Gala418ec852009-03-19 02:32:23 -0500230#elif FSL_HW_NUM_LAWS == 32
Simon Glass8670dbc2012-12-13 20:48:51 +0000231 gd->arch.used_laws = 0;
Kumar Gala418ec852009-03-19 02:32:23 -0500232#else
233#error FSL_HW_NUM_LAWS can not be greater than 32 w/o code changes
234#endif
Kumar Gala83d40df2008-01-16 01:13:58 -0600235
Aneesh Bansal7efb4b52014-03-11 23:21:45 +0530236#if defined(CONFIG_SECURE_BOOT) && defined(CONFIG_E500) && \
237 !defined(CONFIG_E500MC)
238 /* ISBC (Boot ROM) creates a LAW 0 entry for non PBL platforms,
239 * which is not disabled before transferring the control to uboot.
240 * Disable the LAW 0 entry here.
241 */
242 disable_law(0);
243#endif
244
245
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200246 /*
Kumar Gala76396752011-02-03 09:02:13 -0600247 * Any LAWs that were set up before we booted assume they are meant to
248 * be around and mark them used.
249 */
250 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
251 u32 lawar = in_be32(LAWAR_ADDR(i));
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200252
Kumar Gala76396752011-02-03 09:02:13 -0600253 if (lawar & LAW_EN)
Simon Glass8670dbc2012-12-13 20:48:51 +0000254 gd->arch.used_laws |= (1 << i);
Kumar Gala76396752011-02-03 09:02:13 -0600255 }
256
Scott Woodc97cd1b2012-09-20 19:02:18 -0500257#if (defined(CONFIG_NAND_U_BOOT) && !defined(CONFIG_NAND_SPL)) || \
258 (defined(CONFIG_SPL) && !defined(CONFIG_SPL_BUILD))
Kumar Gala76396752011-02-03 09:02:13 -0600259 /*
Scott Woodc97cd1b2012-09-20 19:02:18 -0500260 * in SPL boot we've already parsed the law_table and setup those LAWs
Kumar Gala76396752011-02-03 09:02:13 -0600261 * so don't do it again.
262 */
263 return;
264#endif
265
Kumar Gala83d40df2008-01-16 01:13:58 -0600266 for (i = 0; i < num_law_entries; i++) {
Kumar Galaf0600542008-06-11 00:44:10 -0500267 if (law_table[i].index == -1)
268 set_next_law(law_table[i].addr, law_table[i].size,
269 law_table[i].trgt_id);
270 else
271 set_law(law_table[i].index, law_table[i].addr,
272 law_table[i].size, law_table[i].trgt_id);
Kumar Gala83d40df2008-01-16 01:13:58 -0600273 }
274
Liu Gang461632b2012-08-09 05:10:03 +0000275#ifdef CONFIG_SRIO_PCIE_BOOT_SLAVE
Liu Gang81fa73b2012-08-09 05:10:00 +0000276 /* check RCW to get which port is used for boot */
277 ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
278 u32 bootloc = in_be32(&gur->rcwsr[6]);
Liu Gang461632b2012-08-09 05:10:03 +0000279 /*
280 * in SRIO or PCIE boot we need to set specail LAWs for
281 * SRIO or PCIE interfaces.
282 */
Liu Gang81fa73b2012-08-09 05:10:00 +0000283 switch ((bootloc & FSL_CORENET_RCWSR6_BOOT_LOC) >> 23) {
Liu Gang461632b2012-08-09 05:10:03 +0000284 case 0x0: /* boot from PCIE1 */
285 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
286 LAW_SIZE_1M,
287 LAW_TRGT_IF_PCIE_1);
288 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
289 LAW_SIZE_1M,
290 LAW_TRGT_IF_PCIE_1);
291 break;
292 case 0x1: /* boot from PCIE2 */
293 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
294 LAW_SIZE_1M,
295 LAW_TRGT_IF_PCIE_2);
296 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
297 LAW_SIZE_1M,
298 LAW_TRGT_IF_PCIE_2);
299 break;
300 case 0x2: /* boot from PCIE3 */
301 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
302 LAW_SIZE_1M,
303 LAW_TRGT_IF_PCIE_3);
304 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
305 LAW_SIZE_1M,
306 LAW_TRGT_IF_PCIE_3);
307 break;
Liu Gang81fa73b2012-08-09 05:10:00 +0000308 case 0x8: /* boot from SRIO1 */
Liu Gang461632b2012-08-09 05:10:03 +0000309 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000310 LAW_SIZE_1M,
311 LAW_TRGT_IF_RIO_1);
Liu Gang461632b2012-08-09 05:10:03 +0000312 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000313 LAW_SIZE_1M,
314 LAW_TRGT_IF_RIO_1);
315 break;
316 case 0x9: /* boot from SRIO2 */
Liu Gang461632b2012-08-09 05:10:03 +0000317 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000318 LAW_SIZE_1M,
319 LAW_TRGT_IF_RIO_2);
Liu Gang461632b2012-08-09 05:10:03 +0000320 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000321 LAW_SIZE_1M,
322 LAW_TRGT_IF_RIO_2);
323 break;
324 default:
325 break;
326 }
327#endif
328
Kumar Gala83d40df2008-01-16 01:13:58 -0600329 return ;
330}