blob: 198b5b10a52c3e6e7057c47b669df016e4ddfc4a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Gala95fd2f62008-01-16 01:13:58 -06002/*
Poonam Aggrwal2ba3ee02011-01-13 21:39:27 +05303 * Copyright 2008-2011 Freescale Semiconductor, Inc.
Kumar Gala95fd2f62008-01-16 01:13:58 -06004 *
5 * (C) Copyright 2000
6 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Kumar Gala95fd2f62008-01-16 01:13:58 -06007 */
8
9#include <common.h>
Simon Glass1ab16922022-07-31 12:28:48 -060010#include <display_options.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060011#include <asm/bitops.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Kumar Gala6a74ade2011-02-03 09:02:13 -060013#include <linux/compiler.h>
Kumar Gala95fd2f62008-01-16 01:13:58 -060014#include <asm/fsl_law.h>
15#include <asm/io.h>
Fabio Estevam1a03a7e2015-11-05 12:43:40 -020016#include <linux/log2.h>
Kumar Gala95fd2f62008-01-16 01:13:58 -060017
Kumar Gala75639e02008-06-11 00:44:10 -050018DECLARE_GLOBAL_DATA_PTR;
19
Kumar Galafe137112011-01-19 03:05:26 -060020#define FSL_HW_NUM_LAWS CONFIG_SYS_FSL_NUM_LAWS
Kumar Gala95fd2f62008-01-16 01:13:58 -060021
Kumar Gala65e6c322009-03-19 02:32:23 -050022#ifdef CONFIG_FSL_CORENET
Becky Bruceeb891f02010-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
Kumar Gala65e6c322009-03-19 02:32:23 -050034
Kumar Gala65e6c322009-03-19 02:32:23 -050035
Becky Bruceeb891f02010-06-17 11:37:23 -050036static inline phys_addr_t get_law_base_addr(int idx)
Kumar Gala65e6c322009-03-19 02:32:23 -050037{
Becky Bruceeb891f02010-06-17 11:37:23 -050038#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
Kumar Gala65e6c322009-03-19 02:32:23 -050045}
46
Becky Bruceeb891f02010-06-17 11:37:23 -050047static inline void set_law_base_addr(int idx, phys_addr_t addr)
Kumar Gala65e6c322009-03-19 02:32:23 -050048{
Becky Bruceeb891f02010-06-17 11:37:23 -050049#ifdef CONFIG_FSL_CORENET
50 out_be32(LAWBARL_ADDR(idx), addr & 0xffffffff);
51 out_be32(LAWBARH_ADDR(idx), (u64)addr >> 32);
Kumar Gala65e6c322009-03-19 02:32:23 -050052#else
Becky Bruceeb891f02010-06-17 11:37:23 -050053 out_be32(LAWBAR_ADDR(idx), addr >> LAWBAR_SHIFT);
54#endif
55}
56
Kumar Gala65e6c322009-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 Glassc6622d62012-12-13 20:48:51 +000059 gd->arch.used_laws |= (1 << idx);
Kumar Gala75639e02008-06-11 00:44:10 -050060
Becky Bruceeb891f02010-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 Gala95fd2f62008-01-16 01:13:58 -060064
Timur Tabi52fa71f2009-09-04 17:05:24 -050065 /* Read back so that we sync the writes */
Becky Bruceeb891f02010-06-17 11:37:23 -050066 in_be32(LAWAR_ADDR(idx));
Kumar Gala95fd2f62008-01-16 01:13:58 -060067}
68
Kumar Gala65e6c322009-03-19 02:32:23 -050069void disable_law(u8 idx)
70{
Simon Glassc6622d62012-12-13 20:48:51 +000071 gd->arch.used_laws &= ~(1 << idx);
Kumar Gala65e6c322009-03-19 02:32:23 -050072
Becky Bruceeb891f02010-06-17 11:37:23 -050073 out_be32(LAWAR_ADDR(idx), 0);
74 set_law_base_addr(idx, 0);
Kumar Gala65e6c322009-03-19 02:32:23 -050075
76 /* Read back so that we sync the writes */
Becky Bruceeb891f02010-06-17 11:37:23 -050077 in_be32(LAWAR_ADDR(idx));
Kumar Gala65e6c322009-03-19 02:32:23 -050078
79 return;
80}
81
Ying Zhangffc86e22013-08-16 15:16:10 +080082#if !defined(CONFIG_NAND_SPL) && \
Tom Rini6b15c162022-05-13 12:26:35 -040083 (!defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(INIT_MINIMAL))
Kumar Gala65e6c322009-03-19 02:32:23 -050084static int get_law_entry(u8 i, struct law_entry *e)
85{
Becky Bruceeb891f02010-06-17 11:37:23 -050086 u32 lawar;
Kumar Gala65e6c322009-03-19 02:32:23 -050087
Becky Bruceeb891f02010-06-17 11:37:23 -050088 lawar = in_be32(LAWAR_ADDR(i));
Kumar Gala65e6c322009-03-19 02:32:23 -050089
Becky Bruceeb891f02010-06-17 11:37:23 -050090 if (!(lawar & LAW_EN))
Kumar Gala65e6c322009-03-19 02:32:23 -050091 return 0;
92
Becky Bruceeb891f02010-06-17 11:37:23 -050093 e->addr = get_law_base_addr(i);
94 e->size = lawar & 0x3f;
95 e->trgt_id = (lawar >> 20) & 0xff;
Kumar Gala65e6c322009-03-19 02:32:23 -050096
97 return 1;
98}
99#endif
100
Kumar Gala75639e02008-06-11 00:44:10 -0500101int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
102{
Simon Glassc6622d62012-12-13 20:48:51 +0000103 u32 idx = ffz(gd->arch.used_laws);
Kumar Gala75639e02008-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 Zhangffc86e22013-08-16 15:16:10 +0800113#if !defined(CONFIG_NAND_SPL) && \
Tom Rini6b15c162022-05-13 12:26:35 -0400114 (!defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(INIT_MINIMAL))
Kumar Galaa9abb002008-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 Glassc6622d62012-12-13 20:48:51 +0000120 if (gd->arch.used_laws == -1)
Kumar Galaa9abb002008-06-10 16:16:02 -0500121 return -1;
122
123 /* grab the last free law */
Simon Glassc6622d62012-12-13 20:48:51 +0000124 idx = __ilog2(~(gd->arch.used_laws));
Kumar Galaa9abb002008-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 Gala65e6c322009-03-19 02:32:23 -0500134struct law_entry find_law(phys_addr_t addr)
Kumar Gala95fd2f62008-01-16 01:13:58 -0600135{
Kumar Gala65e6c322009-03-19 02:32:23 -0500136 struct law_entry entry;
137 int i;
Kumar Gala95fd2f62008-01-16 01:13:58 -0600138
Kumar Gala65e6c322009-03-19 02:32:23 -0500139 entry.index = -1;
140 entry.addr = 0;
141 entry.size = 0;
142 entry.trgt_id = 0;
Kumar Gala75639e02008-06-11 00:44:10 -0500143
Kumar Gala65e6c322009-03-19 02:32:23 -0500144 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
145 u64 upper;
Kumar Gala95fd2f62008-01-16 01:13:58 -0600146
Kumar Gala65e6c322009-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 Gala95fd2f62008-01-16 01:13:58 -0600158}
159
Becky Bruce2a15f752008-01-23 16:31:05 -0600160void print_laws(void)
161{
Becky Bruce2a15f752008-01-23 16:31:05 -0600162 int i;
Becky Bruceeb891f02010-06-17 11:37:23 -0500163 u32 lawar;
Becky Bruce2a15f752008-01-23 16:31:05 -0600164
165 printf("\nLocal Access Window Configuration\n");
Becky Bruceeb891f02010-06-17 11:37:23 -0500166 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
167 lawar = in_be32(LAWAR_ADDR(i));
Becky Bruced58f8d52010-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 Bruceeb891f02010-06-17 11:37:23 -0500173 printf("LAWBAR%02d: 0x%08x", i, in_be32(LAWBAR_ADDR(i)));
Becky Bruced58f8d52010-06-17 11:37:24 -0500174#endif
Kumar Gala978bb792011-02-12 15:34:08 -0600175 printf(" LAWAR%02d: 0x%08x\n", i, lawar);
Becky Bruceeb891f02010-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 Bruce2a15f752008-01-23 16:31:05 -0600179 }
180
181 return;
182}
183
Kumar Gala61ed0532008-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 kumare7d15f62016-01-22 15:50:10 +0530193 start_align = 1ull << (__ffs64(start));
Kumar Gala61ed0532008-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 Gala894cc882009-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 Gala61ed0532008-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 kumare7d15f62016-01-22 15:50:10 +0530208 start_align = 1ull << (__ffs64(start));
Kumar Gala61ed0532008-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 Wood095b7122012-09-20 19:02:18 -0500225#endif /* not SPL */
Kumar Gala61ed0532008-08-26 15:01:28 -0500226
Prabhakar Kushwaha21a25902014-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 Gala95fd2f62008-01-16 01:13:58 -0600253void init_laws(void)
254{
255 int i;
Kumar Gala95fd2f62008-01-16 01:13:58 -0600256
Kumar Gala65e6c322009-03-19 02:32:23 -0500257#if FSL_HW_NUM_LAWS < 32
Simon Glassc6622d62012-12-13 20:48:51 +0000258 gd->arch.used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
Kumar Gala65e6c322009-03-19 02:32:23 -0500259#elif FSL_HW_NUM_LAWS == 32
Simon Glassc6622d62012-12-13 20:48:51 +0000260 gd->arch.used_laws = 0;
Kumar Gala65e6c322009-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 Gala95fd2f62008-01-16 01:13:58 -0600264
Udit Agarwald2dd2f72019-11-07 16:11:39 +0000265#if defined(CONFIG_NXP_ESBC) && defined(CONFIG_E500) && \
Aneesh Bansal060384a2014-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 Agarwald2dd2f72019-11-07 16:11:39 +0000274#if !defined(CONFIG_NXP_ESBC)
Prabhakar Kushwaha21a25902014-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 Bansal060384a2014-03-11 23:21:45 +0530281
Wolfgang Denk80f70212011-05-19 22:21:41 +0200282 /*
Kumar Gala6a74ade2011-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 Denk80f70212011-05-19 22:21:41 +0200288
Kumar Gala6a74ade2011-02-03 09:02:13 -0600289 if (lawar & LAW_EN)
Simon Glassc6622d62012-12-13 20:48:51 +0000290 gd->arch.used_laws |= (1 << i);
Kumar Gala6a74ade2011-02-03 09:02:13 -0600291 }
292
Kumar Gala75639e02008-06-11 00:44:10 -0500293 for (i = 0; i < num_law_entries; i++) {
294 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 Gala95fd2f62008-01-16 01:13:58 -0600300 }
301
Liu Gangb4611ee2012-08-09 05:10:03 +0000302#ifdef CONFIG_SRIO_PCIE_BOOT_SLAVE
Liu Gange86e1a32012-08-09 05:10:00 +0000303 /* check RCW to get which port is used for boot */
304 ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
305 u32 bootloc = in_be32(&gur->rcwsr[6]);
Liu Gangb4611ee2012-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 Gange86e1a32012-08-09 05:10:00 +0000310 switch ((bootloc & FSL_CORENET_RCWSR6_BOOT_LOC) >> 23) {
Liu Gangb4611ee2012-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 Gange86e1a32012-08-09 05:10:00 +0000335 case 0x8: /* boot from SRIO1 */
Liu Gangb4611ee2012-08-09 05:10:03 +0000336 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
Liu Gange86e1a32012-08-09 05:10:00 +0000337 LAW_SIZE_1M,
338 LAW_TRGT_IF_RIO_1);
Liu Gangb4611ee2012-08-09 05:10:03 +0000339 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
Liu Gange86e1a32012-08-09 05:10:00 +0000340 LAW_SIZE_1M,
341 LAW_TRGT_IF_RIO_1);
342 break;
343 case 0x9: /* boot from SRIO2 */
Liu Gangb4611ee2012-08-09 05:10:03 +0000344 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
Liu Gange86e1a32012-08-09 05:10:00 +0000345 LAW_SIZE_1M,
346 LAW_TRGT_IF_RIO_2);
Liu Gangb4611ee2012-08-09 05:10:03 +0000347 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
Liu Gange86e1a32012-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
Kumar Gala95fd2f62008-01-16 01:13:58 -0600356 return ;
357}