blob: 4e3f900796d1983c0a8d3ca84faec05a8cdbcb81 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Gala83d40df2008-01-16 01:13:58 -06002/*
Poonam Aggrwalb8cdd012011-01-13 21:39:27 +05303 * Copyright 2008-2011 Freescale Semiconductor, Inc.
Kumar Gala83d40df2008-01-16 01:13:58 -06004 *
5 * (C) Copyright 2000
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Kumar Gala83d40df2008-01-16 01:13:58 -06007 */
8
9#include <common.h>
Simon Glass4e4bf942022-07-31 12:28:48 -060010#include <display_options.h>
Simon Glasscd93d622020-05-10 11:40:13 -060011#include <asm/bitops.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Kumar Gala76396752011-02-03 09:02:13 -060013#include <linux/compiler.h>
Kumar Gala83d40df2008-01-16 01:13:58 -060014#include <asm/fsl_law.h>
15#include <asm/io.h>
Fabio Estevam2d2f4902015-11-05 12:43:40 -020016#include <linux/log2.h>
Kumar Gala83d40df2008-01-16 01:13:58 -060017
Kumar Galaf0600542008-06-11 00:44:10 -050018DECLARE_GLOBAL_DATA_PTR;
19
Kumar Gala243be8e2011-01-19 03:05:26 -060020#define FSL_HW_NUM_LAWS CONFIG_SYS_FSL_NUM_LAWS
Kumar Gala83d40df2008-01-16 01:13:58 -060021
Kumar Gala418ec852009-03-19 02:32:23 -050022#ifdef CONFIG_FSL_CORENET
Becky Brucee71755f2010-06-17 11:37:23 -050023#define LAW_BASE (CONFIG_SYS_FSL_CORENET_CCM_ADDR)
24#define LAWAR_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawar)
25#define LAWBARH_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarh)
26#define LAWBARL_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarl)
27#define LAWBAR_SHIFT 0
28#else
29#define LAW_BASE (CONFIG_SYS_IMMR + 0xc08)
30#define LAWAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x + 2)
31#define LAWBAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x)
32#define LAWBAR_SHIFT 12
33#endif
34
35
36static inline phys_addr_t get_law_base_addr(int idx)
37{
38#ifdef CONFIG_FSL_CORENET
39 return (phys_addr_t)
40 ((u64)in_be32(LAWBARH_ADDR(idx)) << 32) |
41 in_be32(LAWBARL_ADDR(idx));
42#else
43 return (phys_addr_t)in_be32(LAWBAR_ADDR(idx)) << LAWBAR_SHIFT;
44#endif
45}
46
47static inline void set_law_base_addr(int idx, phys_addr_t addr)
48{
49#ifdef CONFIG_FSL_CORENET
50 out_be32(LAWBARL_ADDR(idx), addr & 0xffffffff);
51 out_be32(LAWBARH_ADDR(idx), (u64)addr >> 32);
52#else
53 out_be32(LAWBAR_ADDR(idx), addr >> LAWBAR_SHIFT);
54#endif
55}
56
Kumar Gala418ec852009-03-19 02:32:23 -050057void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
58{
Simon Glass8670dbc2012-12-13 20:48:51 +000059 gd->arch.used_laws |= (1 << idx);
Kumar Gala418ec852009-03-19 02:32:23 -050060
Becky Brucee71755f2010-06-17 11:37:23 -050061 out_be32(LAWAR_ADDR(idx), 0);
62 set_law_base_addr(idx, addr);
63 out_be32(LAWAR_ADDR(idx), LAW_EN | ((u32)id << 20) | (u32)sz);
Kumar Gala418ec852009-03-19 02:32:23 -050064
65 /* Read back so that we sync the writes */
Becky Brucee71755f2010-06-17 11:37:23 -050066 in_be32(LAWAR_ADDR(idx));
Kumar Gala418ec852009-03-19 02:32:23 -050067}
68
69void disable_law(u8 idx)
70{
Simon Glass8670dbc2012-12-13 20:48:51 +000071 gd->arch.used_laws &= ~(1 << idx);
Kumar Gala418ec852009-03-19 02:32:23 -050072
Becky Brucee71755f2010-06-17 11:37:23 -050073 out_be32(LAWAR_ADDR(idx), 0);
74 set_law_base_addr(idx, 0);
Kumar Gala418ec852009-03-19 02:32:23 -050075
76 /* Read back so that we sync the writes */
Becky Brucee71755f2010-06-17 11:37:23 -050077 in_be32(LAWAR_ADDR(idx));
Kumar Gala418ec852009-03-19 02:32:23 -050078
79 return;
80}
81
Ying Zhang0151d992013-08-16 15:16:10 +080082#if !defined(CONFIG_NAND_SPL) && \
Tom Rinib35316f2022-05-13 12:26:35 -040083 (!defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(INIT_MINIMAL))
Kumar Gala418ec852009-03-19 02:32:23 -050084static int get_law_entry(u8 i, struct law_entry *e)
85{
Kumar Gala418ec852009-03-19 02:32:23 -050086 u32 lawar;
87
Becky Brucee71755f2010-06-17 11:37:23 -050088 lawar = in_be32(LAWAR_ADDR(i));
Kumar Gala418ec852009-03-19 02:32:23 -050089
90 if (!(lawar & LAW_EN))
91 return 0;
92
Becky Brucee71755f2010-06-17 11:37:23 -050093 e->addr = get_law_base_addr(i);
Kumar Gala418ec852009-03-19 02:32:23 -050094 e->size = lawar & 0x3f;
95 e->trgt_id = (lawar >> 20) & 0xff;
96
97 return 1;
98}
Kumar Gala24b17d82009-09-30 08:39:44 -050099#endif
Kumar Gala418ec852009-03-19 02:32:23 -0500100
Kumar Galaf0600542008-06-11 00:44:10 -0500101int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
102{
Simon Glass8670dbc2012-12-13 20:48:51 +0000103 u32 idx = ffz(gd->arch.used_laws);
Kumar Galaf0600542008-06-11 00:44:10 -0500104
105 if (idx >= FSL_HW_NUM_LAWS)
106 return -1;
107
108 set_law(idx, addr, sz, id);
109
110 return idx;
111}
112
Ying Zhang0151d992013-08-16 15:16:10 +0800113#if !defined(CONFIG_NAND_SPL) && \
Tom Rinib35316f2022-05-13 12:26:35 -0400114 (!defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(INIT_MINIMAL))
Kumar Galaba04f702008-06-10 16:16:02 -0500115int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
116{
117 u32 idx;
118
119 /* we have no LAWs free */
Simon Glass8670dbc2012-12-13 20:48:51 +0000120 if (gd->arch.used_laws == -1)
Kumar Galaba04f702008-06-10 16:16:02 -0500121 return -1;
122
123 /* grab the last free law */
Simon Glass8670dbc2012-12-13 20:48:51 +0000124 idx = __ilog2(~(gd->arch.used_laws));
Kumar Galaba04f702008-06-10 16:16:02 -0500125
126 if (idx >= FSL_HW_NUM_LAWS)
127 return -1;
128
129 set_law(idx, addr, sz, id);
130
131 return idx;
132}
133
Kumar Gala418ec852009-03-19 02:32:23 -0500134struct law_entry find_law(phys_addr_t addr)
Kumar Gala83d40df2008-01-16 01:13:58 -0600135{
Kumar Gala418ec852009-03-19 02:32:23 -0500136 struct law_entry entry;
137 int i;
Kumar Gala83d40df2008-01-16 01:13:58 -0600138
Kumar Gala418ec852009-03-19 02:32:23 -0500139 entry.index = -1;
140 entry.addr = 0;
141 entry.size = 0;
142 entry.trgt_id = 0;
Kumar Galaf0600542008-06-11 00:44:10 -0500143
Kumar Gala418ec852009-03-19 02:32:23 -0500144 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
145 u64 upper;
Kumar Gala83d40df2008-01-16 01:13:58 -0600146
Kumar Gala418ec852009-03-19 02:32:23 -0500147 if (!get_law_entry(i, &entry))
148 continue;
149
150 upper = entry.addr + (2ull << entry.size);
151 if ((addr >= entry.addr) && (addr < upper)) {
152 entry.index = i;
153 break;
154 }
155 }
156
157 return entry;
Kumar Gala83d40df2008-01-16 01:13:58 -0600158}
159
Becky Bruceddcebcb2008-01-23 16:31:05 -0600160void print_laws(void)
161{
Becky Bruceddcebcb2008-01-23 16:31:05 -0600162 int i;
Becky Brucee71755f2010-06-17 11:37:23 -0500163 u32 lawar;
Becky Bruceddcebcb2008-01-23 16:31:05 -0600164
165 printf("\nLocal Access Window Configuration\n");
Becky Brucee71755f2010-06-17 11:37:23 -0500166 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
167 lawar = in_be32(LAWAR_ADDR(i));
Becky Bruce11a3de42010-06-17 11:37:24 -0500168#ifdef CONFIG_FSL_CORENET
169 printf("LAWBARH%02d: 0x%08x LAWBARL%02d: 0x%08x",
170 i, in_be32(LAWBARH_ADDR(i)),
171 i, in_be32(LAWBARL_ADDR(i)));
172#else
Becky Brucee71755f2010-06-17 11:37:23 -0500173 printf("LAWBAR%02d: 0x%08x", i, in_be32(LAWBAR_ADDR(i)));
Becky Bruce11a3de42010-06-17 11:37:24 -0500174#endif
Kumar Gala8e29eba2011-02-12 15:34:08 -0600175 printf(" LAWAR%02d: 0x%08x\n", i, lawar);
Becky Brucee71755f2010-06-17 11:37:23 -0500176 printf("\t(EN: %d TGT: 0x%02x SIZE: ",
177 (lawar & LAW_EN) ? 1 : 0, (lawar >> 20) & 0xff);
178 print_size(lawar_size(lawar), ")\n");
Becky Bruceddcebcb2008-01-23 16:31:05 -0600179 }
180
181 return;
182}
183
Kumar Galaf784e322008-08-26 15:01:28 -0500184/* use up to 2 LAWs for DDR, used the last available LAWs */
185int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
186{
187 u64 start_align, law_sz;
188 int law_sz_enc;
189
190 if (start == 0)
191 start_align = 1ull << (LAW_SIZE_32G + 1);
192 else
Ashish kumar43381472016-01-22 15:50:10 +0530193 start_align = 1ull << (__ffs64(start));
Kumar Galaf784e322008-08-26 15:01:28 -0500194 law_sz = min(start_align, sz);
195 law_sz_enc = __ilog2_u64(law_sz) - 1;
196
197 if (set_last_law(start, law_sz_enc, id) < 0)
198 return -1;
199
Kumar Galae6a67892009-04-04 10:21:02 -0500200 /* recalculate size based on what was actually covered by the law */
201 law_sz = 1ull << __ilog2_u64(law_sz);
202
Kumar Galaf784e322008-08-26 15:01:28 -0500203 /* do we still have anything to map */
204 sz = sz - law_sz;
205 if (sz) {
206 start += law_sz;
207
Ashish kumar43381472016-01-22 15:50:10 +0530208 start_align = 1ull << (__ffs64(start));
Kumar Galaf784e322008-08-26 15:01:28 -0500209 law_sz = min(start_align, sz);
210 law_sz_enc = __ilog2_u64(law_sz) - 1;
211
212 if (set_last_law(start, law_sz_enc, id) < 0)
213 return -1;
214 } else {
215 return 0;
216 }
217
218 /* do we still have anything to map */
219 sz = sz - law_sz;
220 if (sz)
221 return 1;
222
223 return 0;
224}
Scott Woodc97cd1b2012-09-20 19:02:18 -0500225#endif /* not SPL */
Kumar Galaf784e322008-08-26 15:01:28 -0500226
Prabhakar Kushwaha6b3d5882014-04-08 19:12:46 +0530227void disable_non_ddr_laws(void)
228{
229 int i;
230 int id;
231 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
232 u32 lawar = in_be32(LAWAR_ADDR(i));
233
234 if (lawar & LAW_EN) {
235 id = (lawar & ~LAW_EN) >> 20;
236 switch (id) {
237 case LAW_TRGT_IF_DDR_1:
238 case LAW_TRGT_IF_DDR_2:
239 case LAW_TRGT_IF_DDR_3:
240 case LAW_TRGT_IF_DDR_4:
241 case LAW_TRGT_IF_DDR_INTRLV:
242 case LAW_TRGT_IF_DDR_INTLV_34:
243 case LAW_TRGT_IF_DDR_INTLV_123:
244 case LAW_TRGT_IF_DDR_INTLV_1234:
245 continue;
246 default:
247 disable_law(i);
248 }
249 }
250 }
251}
252
Kumar Gala83d40df2008-01-16 01:13:58 -0600253void init_laws(void)
254{
255 int i;
Kumar Galaf0600542008-06-11 00:44:10 -0500256
Kumar Gala418ec852009-03-19 02:32:23 -0500257#if FSL_HW_NUM_LAWS < 32
Simon Glass8670dbc2012-12-13 20:48:51 +0000258 gd->arch.used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
Kumar Gala418ec852009-03-19 02:32:23 -0500259#elif FSL_HW_NUM_LAWS == 32
Simon Glass8670dbc2012-12-13 20:48:51 +0000260 gd->arch.used_laws = 0;
Kumar Gala418ec852009-03-19 02:32:23 -0500261#else
262#error FSL_HW_NUM_LAWS can not be greater than 32 w/o code changes
263#endif
Kumar Gala83d40df2008-01-16 01:13:58 -0600264
Udit Agarwalbef18452019-11-07 16:11:39 +0000265#if defined(CONFIG_NXP_ESBC) && defined(CONFIG_E500) && \
Aneesh Bansal7efb4b52014-03-11 23:21:45 +0530266 !defined(CONFIG_E500MC)
267 /* ISBC (Boot ROM) creates a LAW 0 entry for non PBL platforms,
268 * which is not disabled before transferring the control to uboot.
269 * Disable the LAW 0 entry here.
270 */
271 disable_law(0);
272#endif
273
Udit Agarwalbef18452019-11-07 16:11:39 +0000274#if !defined(CONFIG_NXP_ESBC)
Prabhakar Kushwaha6b3d5882014-04-08 19:12:46 +0530275 /*
276 * if any non DDR LAWs has been created earlier, remove them before
277 * LAW table is parsed.
278 */
279 disable_non_ddr_laws();
280#endif
Aneesh Bansal7efb4b52014-03-11 23:21:45 +0530281
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200282 /*
Kumar Gala76396752011-02-03 09:02:13 -0600283 * Any LAWs that were set up before we booted assume they are meant to
284 * be around and mark them used.
285 */
286 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
287 u32 lawar = in_be32(LAWAR_ADDR(i));
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200288
Kumar Gala76396752011-02-03 09:02:13 -0600289 if (lawar & LAW_EN)
Simon Glass8670dbc2012-12-13 20:48:51 +0000290 gd->arch.used_laws |= (1 << i);
Kumar Gala76396752011-02-03 09:02:13 -0600291 }
292
Kumar Gala83d40df2008-01-16 01:13:58 -0600293 for (i = 0; i < num_law_entries; i++) {
Kumar Galaf0600542008-06-11 00:44:10 -0500294 if (law_table[i].index == -1)
295 set_next_law(law_table[i].addr, law_table[i].size,
296 law_table[i].trgt_id);
297 else
298 set_law(law_table[i].index, law_table[i].addr,
299 law_table[i].size, law_table[i].trgt_id);
Kumar Gala83d40df2008-01-16 01:13:58 -0600300 }
301
Liu Gang461632b2012-08-09 05:10:03 +0000302#ifdef CONFIG_SRIO_PCIE_BOOT_SLAVE
Liu Gang81fa73b2012-08-09 05:10:00 +0000303 /* check RCW to get which port is used for boot */
Tom Rini51552072022-10-28 20:27:12 -0400304 ccsr_gur_t *gur = (void *)CFG_SYS_MPC85xx_GUTS_ADDR;
Liu Gang81fa73b2012-08-09 05:10:00 +0000305 u32 bootloc = in_be32(&gur->rcwsr[6]);
Liu Gang461632b2012-08-09 05:10:03 +0000306 /*
307 * in SRIO or PCIE boot we need to set specail LAWs for
308 * SRIO or PCIE interfaces.
309 */
Liu Gang81fa73b2012-08-09 05:10:00 +0000310 switch ((bootloc & FSL_CORENET_RCWSR6_BOOT_LOC) >> 23) {
Liu Gang461632b2012-08-09 05:10:03 +0000311 case 0x0: /* boot from PCIE1 */
312 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
313 LAW_SIZE_1M,
314 LAW_TRGT_IF_PCIE_1);
315 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
316 LAW_SIZE_1M,
317 LAW_TRGT_IF_PCIE_1);
318 break;
319 case 0x1: /* boot from PCIE2 */
320 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
321 LAW_SIZE_1M,
322 LAW_TRGT_IF_PCIE_2);
323 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
324 LAW_SIZE_1M,
325 LAW_TRGT_IF_PCIE_2);
326 break;
327 case 0x2: /* boot from PCIE3 */
328 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
329 LAW_SIZE_1M,
330 LAW_TRGT_IF_PCIE_3);
331 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
332 LAW_SIZE_1M,
333 LAW_TRGT_IF_PCIE_3);
334 break;
Liu Gang81fa73b2012-08-09 05:10:00 +0000335 case 0x8: /* boot from SRIO1 */
Liu Gang461632b2012-08-09 05:10:03 +0000336 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000337 LAW_SIZE_1M,
338 LAW_TRGT_IF_RIO_1);
Liu Gang461632b2012-08-09 05:10:03 +0000339 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000340 LAW_SIZE_1M,
341 LAW_TRGT_IF_RIO_1);
342 break;
343 case 0x9: /* boot from SRIO2 */
Liu Gang461632b2012-08-09 05:10:03 +0000344 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000345 LAW_SIZE_1M,
346 LAW_TRGT_IF_RIO_2);
Liu Gang461632b2012-08-09 05:10:03 +0000347 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000348 LAW_SIZE_1M,
349 LAW_TRGT_IF_RIO_2);
350 break;
351 default:
352 break;
353 }
354#endif
355
Bin Mengea253ad2022-10-26 12:40:07 +0800356 return;
Kumar Gala83d40df2008-01-16 01:13:58 -0600357}