blob: 89a561e0aca3b5ed87ca745799f649546d3d976e [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
Scott Woodc97cd1b2012-09-20 19:02:18 -050079#if !defined(CONFIG_NAND_SPL) && !defined(CONFIG_SPL_BUILD)
Kumar Gala418ec852009-03-19 02:32:23 -050080static int get_law_entry(u8 i, struct law_entry *e)
81{
Kumar Gala418ec852009-03-19 02:32:23 -050082 u32 lawar;
83
Becky Brucee71755f2010-06-17 11:37:23 -050084 lawar = in_be32(LAWAR_ADDR(i));
Kumar Gala418ec852009-03-19 02:32:23 -050085
86 if (!(lawar & LAW_EN))
87 return 0;
88
Becky Brucee71755f2010-06-17 11:37:23 -050089 e->addr = get_law_base_addr(i);
Kumar Gala418ec852009-03-19 02:32:23 -050090 e->size = lawar & 0x3f;
91 e->trgt_id = (lawar >> 20) & 0xff;
92
93 return 1;
94}
Kumar Gala24b17d82009-09-30 08:39:44 -050095#endif
Kumar Gala418ec852009-03-19 02:32:23 -050096
Kumar Galaf0600542008-06-11 00:44:10 -050097int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
98{
Simon Glass8670dbc2012-12-13 20:48:51 +000099 u32 idx = ffz(gd->arch.used_laws);
Kumar Galaf0600542008-06-11 00:44:10 -0500100
101 if (idx >= FSL_HW_NUM_LAWS)
102 return -1;
103
104 set_law(idx, addr, sz, id);
105
106 return idx;
107}
108
Scott Woodc97cd1b2012-09-20 19:02:18 -0500109#if !defined(CONFIG_NAND_SPL) && !defined(CONFIG_SPL_BUILD)
Kumar Galaba04f702008-06-10 16:16:02 -0500110int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
111{
112 u32 idx;
113
114 /* we have no LAWs free */
Simon Glass8670dbc2012-12-13 20:48:51 +0000115 if (gd->arch.used_laws == -1)
Kumar Galaba04f702008-06-10 16:16:02 -0500116 return -1;
117
118 /* grab the last free law */
Simon Glass8670dbc2012-12-13 20:48:51 +0000119 idx = __ilog2(~(gd->arch.used_laws));
Kumar Galaba04f702008-06-10 16:16:02 -0500120
121 if (idx >= FSL_HW_NUM_LAWS)
122 return -1;
123
124 set_law(idx, addr, sz, id);
125
126 return idx;
127}
128
Kumar Gala418ec852009-03-19 02:32:23 -0500129struct law_entry find_law(phys_addr_t addr)
Kumar Gala83d40df2008-01-16 01:13:58 -0600130{
Kumar Gala418ec852009-03-19 02:32:23 -0500131 struct law_entry entry;
132 int i;
Kumar Gala83d40df2008-01-16 01:13:58 -0600133
Kumar Gala418ec852009-03-19 02:32:23 -0500134 entry.index = -1;
135 entry.addr = 0;
136 entry.size = 0;
137 entry.trgt_id = 0;
Kumar Galaf0600542008-06-11 00:44:10 -0500138
Kumar Gala418ec852009-03-19 02:32:23 -0500139 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
140 u64 upper;
Kumar Gala83d40df2008-01-16 01:13:58 -0600141
Kumar Gala418ec852009-03-19 02:32:23 -0500142 if (!get_law_entry(i, &entry))
143 continue;
144
145 upper = entry.addr + (2ull << entry.size);
146 if ((addr >= entry.addr) && (addr < upper)) {
147 entry.index = i;
148 break;
149 }
150 }
151
152 return entry;
Kumar Gala83d40df2008-01-16 01:13:58 -0600153}
154
Becky Bruceddcebcb2008-01-23 16:31:05 -0600155void print_laws(void)
156{
Becky Bruceddcebcb2008-01-23 16:31:05 -0600157 int i;
Becky Brucee71755f2010-06-17 11:37:23 -0500158 u32 lawar;
Becky Bruceddcebcb2008-01-23 16:31:05 -0600159
160 printf("\nLocal Access Window Configuration\n");
Becky Brucee71755f2010-06-17 11:37:23 -0500161 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
162 lawar = in_be32(LAWAR_ADDR(i));
Becky Bruce11a3de42010-06-17 11:37:24 -0500163#ifdef CONFIG_FSL_CORENET
164 printf("LAWBARH%02d: 0x%08x LAWBARL%02d: 0x%08x",
165 i, in_be32(LAWBARH_ADDR(i)),
166 i, in_be32(LAWBARL_ADDR(i)));
167#else
Becky Brucee71755f2010-06-17 11:37:23 -0500168 printf("LAWBAR%02d: 0x%08x", i, in_be32(LAWBAR_ADDR(i)));
Becky Bruce11a3de42010-06-17 11:37:24 -0500169#endif
Kumar Gala8e29eba2011-02-12 15:34:08 -0600170 printf(" LAWAR%02d: 0x%08x\n", i, lawar);
Becky Brucee71755f2010-06-17 11:37:23 -0500171 printf("\t(EN: %d TGT: 0x%02x SIZE: ",
172 (lawar & LAW_EN) ? 1 : 0, (lawar >> 20) & 0xff);
173 print_size(lawar_size(lawar), ")\n");
Becky Bruceddcebcb2008-01-23 16:31:05 -0600174 }
175
176 return;
177}
178
Kumar Galaf784e322008-08-26 15:01:28 -0500179/* use up to 2 LAWs for DDR, used the last available LAWs */
180int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
181{
182 u64 start_align, law_sz;
183 int law_sz_enc;
184
185 if (start == 0)
186 start_align = 1ull << (LAW_SIZE_32G + 1);
187 else
188 start_align = 1ull << (ffs64(start) - 1);
189 law_sz = min(start_align, sz);
190 law_sz_enc = __ilog2_u64(law_sz) - 1;
191
192 if (set_last_law(start, law_sz_enc, id) < 0)
193 return -1;
194
Kumar Galae6a67892009-04-04 10:21:02 -0500195 /* recalculate size based on what was actually covered by the law */
196 law_sz = 1ull << __ilog2_u64(law_sz);
197
Kumar Galaf784e322008-08-26 15:01:28 -0500198 /* do we still have anything to map */
199 sz = sz - law_sz;
200 if (sz) {
201 start += law_sz;
202
203 start_align = 1ull << (ffs64(start) - 1);
204 law_sz = min(start_align, sz);
205 law_sz_enc = __ilog2_u64(law_sz) - 1;
206
207 if (set_last_law(start, law_sz_enc, id) < 0)
208 return -1;
209 } else {
210 return 0;
211 }
212
213 /* do we still have anything to map */
214 sz = sz - law_sz;
215 if (sz)
216 return 1;
217
218 return 0;
219}
Scott Woodc97cd1b2012-09-20 19:02:18 -0500220#endif /* not SPL */
Kumar Galaf784e322008-08-26 15:01:28 -0500221
Kumar Gala83d40df2008-01-16 01:13:58 -0600222void init_laws(void)
223{
224 int i;
Kumar Galaf0600542008-06-11 00:44:10 -0500225
Kumar Gala418ec852009-03-19 02:32:23 -0500226#if FSL_HW_NUM_LAWS < 32
Simon Glass8670dbc2012-12-13 20:48:51 +0000227 gd->arch.used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
Kumar Gala418ec852009-03-19 02:32:23 -0500228#elif FSL_HW_NUM_LAWS == 32
Simon Glass8670dbc2012-12-13 20:48:51 +0000229 gd->arch.used_laws = 0;
Kumar Gala418ec852009-03-19 02:32:23 -0500230#else
231#error FSL_HW_NUM_LAWS can not be greater than 32 w/o code changes
232#endif
Kumar Gala83d40df2008-01-16 01:13:58 -0600233
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200234 /*
Kumar Gala76396752011-02-03 09:02:13 -0600235 * Any LAWs that were set up before we booted assume they are meant to
236 * be around and mark them used.
237 */
238 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
239 u32 lawar = in_be32(LAWAR_ADDR(i));
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200240
Kumar Gala76396752011-02-03 09:02:13 -0600241 if (lawar & LAW_EN)
Simon Glass8670dbc2012-12-13 20:48:51 +0000242 gd->arch.used_laws |= (1 << i);
Kumar Gala76396752011-02-03 09:02:13 -0600243 }
244
Scott Woodc97cd1b2012-09-20 19:02:18 -0500245#if (defined(CONFIG_NAND_U_BOOT) && !defined(CONFIG_NAND_SPL)) || \
246 (defined(CONFIG_SPL) && !defined(CONFIG_SPL_BUILD))
Kumar Gala76396752011-02-03 09:02:13 -0600247 /*
Scott Woodc97cd1b2012-09-20 19:02:18 -0500248 * in SPL boot we've already parsed the law_table and setup those LAWs
Kumar Gala76396752011-02-03 09:02:13 -0600249 * so don't do it again.
250 */
251 return;
252#endif
253
Kumar Gala83d40df2008-01-16 01:13:58 -0600254 for (i = 0; i < num_law_entries; i++) {
Kumar Galaf0600542008-06-11 00:44:10 -0500255 if (law_table[i].index == -1)
256 set_next_law(law_table[i].addr, law_table[i].size,
257 law_table[i].trgt_id);
258 else
259 set_law(law_table[i].index, law_table[i].addr,
260 law_table[i].size, law_table[i].trgt_id);
Kumar Gala83d40df2008-01-16 01:13:58 -0600261 }
262
Liu Gang461632b2012-08-09 05:10:03 +0000263#ifdef CONFIG_SRIO_PCIE_BOOT_SLAVE
Liu Gang81fa73b2012-08-09 05:10:00 +0000264 /* check RCW to get which port is used for boot */
265 ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
266 u32 bootloc = in_be32(&gur->rcwsr[6]);
Liu Gang461632b2012-08-09 05:10:03 +0000267 /*
268 * in SRIO or PCIE boot we need to set specail LAWs for
269 * SRIO or PCIE interfaces.
270 */
Liu Gang81fa73b2012-08-09 05:10:00 +0000271 switch ((bootloc & FSL_CORENET_RCWSR6_BOOT_LOC) >> 23) {
Liu Gang461632b2012-08-09 05:10:03 +0000272 case 0x0: /* boot from PCIE1 */
273 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
274 LAW_SIZE_1M,
275 LAW_TRGT_IF_PCIE_1);
276 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
277 LAW_SIZE_1M,
278 LAW_TRGT_IF_PCIE_1);
279 break;
280 case 0x1: /* boot from PCIE2 */
281 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
282 LAW_SIZE_1M,
283 LAW_TRGT_IF_PCIE_2);
284 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
285 LAW_SIZE_1M,
286 LAW_TRGT_IF_PCIE_2);
287 break;
288 case 0x2: /* boot from PCIE3 */
289 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
290 LAW_SIZE_1M,
291 LAW_TRGT_IF_PCIE_3);
292 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
293 LAW_SIZE_1M,
294 LAW_TRGT_IF_PCIE_3);
295 break;
Liu Gang81fa73b2012-08-09 05:10:00 +0000296 case 0x8: /* boot from SRIO1 */
Liu Gang461632b2012-08-09 05:10:03 +0000297 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000298 LAW_SIZE_1M,
299 LAW_TRGT_IF_RIO_1);
Liu Gang461632b2012-08-09 05:10:03 +0000300 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000301 LAW_SIZE_1M,
302 LAW_TRGT_IF_RIO_1);
303 break;
304 case 0x9: /* boot from SRIO2 */
Liu Gang461632b2012-08-09 05:10:03 +0000305 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000306 LAW_SIZE_1M,
307 LAW_TRGT_IF_RIO_2);
Liu Gang461632b2012-08-09 05:10:03 +0000308 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000309 LAW_SIZE_1M,
310 LAW_TRGT_IF_RIO_2);
311 break;
312 default:
313 break;
314 }
315#endif
316
Kumar Gala83d40df2008-01-16 01:13:58 -0600317 return ;
318}