blob: 713ff172bceeb9693f468a43d5d6d18bf571e63f [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 Glass4dcacfc2020-05-10 11:40:13 -060010#include <asm/bitops.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Kumar Gala6a74ade2011-02-03 09:02:13 -060012#include <linux/compiler.h>
Kumar Gala95fd2f62008-01-16 01:13:58 -060013#include <asm/fsl_law.h>
14#include <asm/io.h>
Fabio Estevam1a03a7e2015-11-05 12:43:40 -020015#include <linux/log2.h>
Kumar Gala95fd2f62008-01-16 01:13:58 -060016
Kumar Gala75639e02008-06-11 00:44:10 -050017DECLARE_GLOBAL_DATA_PTR;
18
Kumar Galafe137112011-01-19 03:05:26 -060019#define FSL_HW_NUM_LAWS CONFIG_SYS_FSL_NUM_LAWS
Kumar Gala95fd2f62008-01-16 01:13:58 -060020
Kumar Gala65e6c322009-03-19 02:32:23 -050021#ifdef CONFIG_FSL_CORENET
Becky Bruceeb891f02010-06-17 11:37:23 -050022#define LAW_BASE (CONFIG_SYS_FSL_CORENET_CCM_ADDR)
23#define LAWAR_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawar)
24#define LAWBARH_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarh)
25#define LAWBARL_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarl)
26#define LAWBAR_SHIFT 0
27#else
28#define LAW_BASE (CONFIG_SYS_IMMR + 0xc08)
29#define LAWAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x + 2)
30#define LAWBAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x)
31#define LAWBAR_SHIFT 12
32#endif
Kumar Gala65e6c322009-03-19 02:32:23 -050033
Kumar Gala65e6c322009-03-19 02:32:23 -050034
Becky Bruceeb891f02010-06-17 11:37:23 -050035static inline phys_addr_t get_law_base_addr(int idx)
Kumar Gala65e6c322009-03-19 02:32:23 -050036{
Becky Bruceeb891f02010-06-17 11:37:23 -050037#ifdef CONFIG_FSL_CORENET
38 return (phys_addr_t)
39 ((u64)in_be32(LAWBARH_ADDR(idx)) << 32) |
40 in_be32(LAWBARL_ADDR(idx));
41#else
42 return (phys_addr_t)in_be32(LAWBAR_ADDR(idx)) << LAWBAR_SHIFT;
43#endif
Kumar Gala65e6c322009-03-19 02:32:23 -050044}
45
Becky Bruceeb891f02010-06-17 11:37:23 -050046static inline void set_law_base_addr(int idx, phys_addr_t addr)
Kumar Gala65e6c322009-03-19 02:32:23 -050047{
Becky Bruceeb891f02010-06-17 11:37:23 -050048#ifdef CONFIG_FSL_CORENET
49 out_be32(LAWBARL_ADDR(idx), addr & 0xffffffff);
50 out_be32(LAWBARH_ADDR(idx), (u64)addr >> 32);
Kumar Gala65e6c322009-03-19 02:32:23 -050051#else
Becky Bruceeb891f02010-06-17 11:37:23 -050052 out_be32(LAWBAR_ADDR(idx), addr >> LAWBAR_SHIFT);
53#endif
54}
55
Kumar Gala65e6c322009-03-19 02:32:23 -050056void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
57{
Simon Glassc6622d62012-12-13 20:48:51 +000058 gd->arch.used_laws |= (1 << idx);
Kumar Gala75639e02008-06-11 00:44:10 -050059
Becky Bruceeb891f02010-06-17 11:37:23 -050060 out_be32(LAWAR_ADDR(idx), 0);
61 set_law_base_addr(idx, addr);
62 out_be32(LAWAR_ADDR(idx), LAW_EN | ((u32)id << 20) | (u32)sz);
Kumar Gala95fd2f62008-01-16 01:13:58 -060063
Timur Tabi52fa71f2009-09-04 17:05:24 -050064 /* Read back so that we sync the writes */
Becky Bruceeb891f02010-06-17 11:37:23 -050065 in_be32(LAWAR_ADDR(idx));
Kumar Gala95fd2f62008-01-16 01:13:58 -060066}
67
Kumar Gala65e6c322009-03-19 02:32:23 -050068void disable_law(u8 idx)
69{
Simon Glassc6622d62012-12-13 20:48:51 +000070 gd->arch.used_laws &= ~(1 << idx);
Kumar Gala65e6c322009-03-19 02:32:23 -050071
Becky Bruceeb891f02010-06-17 11:37:23 -050072 out_be32(LAWAR_ADDR(idx), 0);
73 set_law_base_addr(idx, 0);
Kumar Gala65e6c322009-03-19 02:32:23 -050074
75 /* Read back so that we sync the writes */
Becky Bruceeb891f02010-06-17 11:37:23 -050076 in_be32(LAWAR_ADDR(idx));
Kumar Gala65e6c322009-03-19 02:32:23 -050077
78 return;
79}
80
Ying Zhangffc86e22013-08-16 15:16:10 +080081#if !defined(CONFIG_NAND_SPL) && \
Tom Rini6b15c162022-05-13 12:26:35 -040082 (!defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(INIT_MINIMAL))
Kumar Gala65e6c322009-03-19 02:32:23 -050083static int get_law_entry(u8 i, struct law_entry *e)
84{
Becky Bruceeb891f02010-06-17 11:37:23 -050085 u32 lawar;
Kumar Gala65e6c322009-03-19 02:32:23 -050086
Becky Bruceeb891f02010-06-17 11:37:23 -050087 lawar = in_be32(LAWAR_ADDR(i));
Kumar Gala65e6c322009-03-19 02:32:23 -050088
Becky Bruceeb891f02010-06-17 11:37:23 -050089 if (!(lawar & LAW_EN))
Kumar Gala65e6c322009-03-19 02:32:23 -050090 return 0;
91
Becky Bruceeb891f02010-06-17 11:37:23 -050092 e->addr = get_law_base_addr(i);
93 e->size = lawar & 0x3f;
94 e->trgt_id = (lawar >> 20) & 0xff;
Kumar Gala65e6c322009-03-19 02:32:23 -050095
96 return 1;
97}
98#endif
99
Kumar Gala75639e02008-06-11 00:44:10 -0500100int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
101{
Simon Glassc6622d62012-12-13 20:48:51 +0000102 u32 idx = ffz(gd->arch.used_laws);
Kumar Gala75639e02008-06-11 00:44:10 -0500103
104 if (idx >= FSL_HW_NUM_LAWS)
105 return -1;
106
107 set_law(idx, addr, sz, id);
108
109 return idx;
110}
111
Ying Zhangffc86e22013-08-16 15:16:10 +0800112#if !defined(CONFIG_NAND_SPL) && \
Tom Rini6b15c162022-05-13 12:26:35 -0400113 (!defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(INIT_MINIMAL))
Kumar Galaa9abb002008-06-10 16:16:02 -0500114int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
115{
116 u32 idx;
117
118 /* we have no LAWs free */
Simon Glassc6622d62012-12-13 20:48:51 +0000119 if (gd->arch.used_laws == -1)
Kumar Galaa9abb002008-06-10 16:16:02 -0500120 return -1;
121
122 /* grab the last free law */
Simon Glassc6622d62012-12-13 20:48:51 +0000123 idx = __ilog2(~(gd->arch.used_laws));
Kumar Galaa9abb002008-06-10 16:16:02 -0500124
125 if (idx >= FSL_HW_NUM_LAWS)
126 return -1;
127
128 set_law(idx, addr, sz, id);
129
130 return idx;
131}
132
Kumar Gala65e6c322009-03-19 02:32:23 -0500133struct law_entry find_law(phys_addr_t addr)
Kumar Gala95fd2f62008-01-16 01:13:58 -0600134{
Kumar Gala65e6c322009-03-19 02:32:23 -0500135 struct law_entry entry;
136 int i;
Kumar Gala95fd2f62008-01-16 01:13:58 -0600137
Kumar Gala65e6c322009-03-19 02:32:23 -0500138 entry.index = -1;
139 entry.addr = 0;
140 entry.size = 0;
141 entry.trgt_id = 0;
Kumar Gala75639e02008-06-11 00:44:10 -0500142
Kumar Gala65e6c322009-03-19 02:32:23 -0500143 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
144 u64 upper;
Kumar Gala95fd2f62008-01-16 01:13:58 -0600145
Kumar Gala65e6c322009-03-19 02:32:23 -0500146 if (!get_law_entry(i, &entry))
147 continue;
148
149 upper = entry.addr + (2ull << entry.size);
150 if ((addr >= entry.addr) && (addr < upper)) {
151 entry.index = i;
152 break;
153 }
154 }
155
156 return entry;
Kumar Gala95fd2f62008-01-16 01:13:58 -0600157}
158
Becky Bruce2a15f752008-01-23 16:31:05 -0600159void print_laws(void)
160{
Becky Bruce2a15f752008-01-23 16:31:05 -0600161 int i;
Becky Bruceeb891f02010-06-17 11:37:23 -0500162 u32 lawar;
Becky Bruce2a15f752008-01-23 16:31:05 -0600163
164 printf("\nLocal Access Window Configuration\n");
Becky Bruceeb891f02010-06-17 11:37:23 -0500165 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
166 lawar = in_be32(LAWAR_ADDR(i));
Becky Bruced58f8d52010-06-17 11:37:24 -0500167#ifdef CONFIG_FSL_CORENET
168 printf("LAWBARH%02d: 0x%08x LAWBARL%02d: 0x%08x",
169 i, in_be32(LAWBARH_ADDR(i)),
170 i, in_be32(LAWBARL_ADDR(i)));
171#else
Becky Bruceeb891f02010-06-17 11:37:23 -0500172 printf("LAWBAR%02d: 0x%08x", i, in_be32(LAWBAR_ADDR(i)));
Becky Bruced58f8d52010-06-17 11:37:24 -0500173#endif
Kumar Gala978bb792011-02-12 15:34:08 -0600174 printf(" LAWAR%02d: 0x%08x\n", i, lawar);
Becky Bruceeb891f02010-06-17 11:37:23 -0500175 printf("\t(EN: %d TGT: 0x%02x SIZE: ",
176 (lawar & LAW_EN) ? 1 : 0, (lawar >> 20) & 0xff);
177 print_size(lawar_size(lawar), ")\n");
Becky Bruce2a15f752008-01-23 16:31:05 -0600178 }
179
180 return;
181}
182
Kumar Gala61ed0532008-08-26 15:01:28 -0500183/* use up to 2 LAWs for DDR, used the last available LAWs */
184int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
185{
186 u64 start_align, law_sz;
187 int law_sz_enc;
188
189 if (start == 0)
190 start_align = 1ull << (LAW_SIZE_32G + 1);
191 else
Ashish kumare7d15f62016-01-22 15:50:10 +0530192 start_align = 1ull << (__ffs64(start));
Kumar Gala61ed0532008-08-26 15:01:28 -0500193 law_sz = min(start_align, sz);
194 law_sz_enc = __ilog2_u64(law_sz) - 1;
195
196 if (set_last_law(start, law_sz_enc, id) < 0)
197 return -1;
198
Kumar Gala894cc882009-04-04 10:21:02 -0500199 /* recalculate size based on what was actually covered by the law */
200 law_sz = 1ull << __ilog2_u64(law_sz);
201
Kumar Gala61ed0532008-08-26 15:01:28 -0500202 /* do we still have anything to map */
203 sz = sz - law_sz;
204 if (sz) {
205 start += law_sz;
206
Ashish kumare7d15f62016-01-22 15:50:10 +0530207 start_align = 1ull << (__ffs64(start));
Kumar Gala61ed0532008-08-26 15:01:28 -0500208 law_sz = min(start_align, sz);
209 law_sz_enc = __ilog2_u64(law_sz) - 1;
210
211 if (set_last_law(start, law_sz_enc, id) < 0)
212 return -1;
213 } else {
214 return 0;
215 }
216
217 /* do we still have anything to map */
218 sz = sz - law_sz;
219 if (sz)
220 return 1;
221
222 return 0;
223}
Scott Wood095b7122012-09-20 19:02:18 -0500224#endif /* not SPL */
Kumar Gala61ed0532008-08-26 15:01:28 -0500225
Prabhakar Kushwaha21a25902014-04-08 19:12:46 +0530226void disable_non_ddr_laws(void)
227{
228 int i;
229 int id;
230 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
231 u32 lawar = in_be32(LAWAR_ADDR(i));
232
233 if (lawar & LAW_EN) {
234 id = (lawar & ~LAW_EN) >> 20;
235 switch (id) {
236 case LAW_TRGT_IF_DDR_1:
237 case LAW_TRGT_IF_DDR_2:
238 case LAW_TRGT_IF_DDR_3:
239 case LAW_TRGT_IF_DDR_4:
240 case LAW_TRGT_IF_DDR_INTRLV:
241 case LAW_TRGT_IF_DDR_INTLV_34:
242 case LAW_TRGT_IF_DDR_INTLV_123:
243 case LAW_TRGT_IF_DDR_INTLV_1234:
244 continue;
245 default:
246 disable_law(i);
247 }
248 }
249 }
250}
251
Kumar Gala95fd2f62008-01-16 01:13:58 -0600252void init_laws(void)
253{
254 int i;
Kumar Gala95fd2f62008-01-16 01:13:58 -0600255
Kumar Gala65e6c322009-03-19 02:32:23 -0500256#if FSL_HW_NUM_LAWS < 32
Simon Glassc6622d62012-12-13 20:48:51 +0000257 gd->arch.used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
Kumar Gala65e6c322009-03-19 02:32:23 -0500258#elif FSL_HW_NUM_LAWS == 32
Simon Glassc6622d62012-12-13 20:48:51 +0000259 gd->arch.used_laws = 0;
Kumar Gala65e6c322009-03-19 02:32:23 -0500260#else
261#error FSL_HW_NUM_LAWS can not be greater than 32 w/o code changes
262#endif
Kumar Gala95fd2f62008-01-16 01:13:58 -0600263
Udit Agarwald2dd2f72019-11-07 16:11:39 +0000264#if defined(CONFIG_NXP_ESBC) && defined(CONFIG_E500) && \
Aneesh Bansal060384a2014-03-11 23:21:45 +0530265 !defined(CONFIG_E500MC)
266 /* ISBC (Boot ROM) creates a LAW 0 entry for non PBL platforms,
267 * which is not disabled before transferring the control to uboot.
268 * Disable the LAW 0 entry here.
269 */
270 disable_law(0);
271#endif
272
Udit Agarwald2dd2f72019-11-07 16:11:39 +0000273#if !defined(CONFIG_NXP_ESBC)
Prabhakar Kushwaha21a25902014-04-08 19:12:46 +0530274 /*
275 * if any non DDR LAWs has been created earlier, remove them before
276 * LAW table is parsed.
277 */
278 disable_non_ddr_laws();
279#endif
Aneesh Bansal060384a2014-03-11 23:21:45 +0530280
Wolfgang Denk80f70212011-05-19 22:21:41 +0200281 /*
Kumar Gala6a74ade2011-02-03 09:02:13 -0600282 * Any LAWs that were set up before we booted assume they are meant to
283 * be around and mark them used.
284 */
285 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
286 u32 lawar = in_be32(LAWAR_ADDR(i));
Wolfgang Denk80f70212011-05-19 22:21:41 +0200287
Kumar Gala6a74ade2011-02-03 09:02:13 -0600288 if (lawar & LAW_EN)
Simon Glassc6622d62012-12-13 20:48:51 +0000289 gd->arch.used_laws |= (1 << i);
Kumar Gala6a74ade2011-02-03 09:02:13 -0600290 }
291
Kumar Gala75639e02008-06-11 00:44:10 -0500292 for (i = 0; i < num_law_entries; i++) {
293 if (law_table[i].index == -1)
294 set_next_law(law_table[i].addr, law_table[i].size,
295 law_table[i].trgt_id);
296 else
297 set_law(law_table[i].index, law_table[i].addr,
298 law_table[i].size, law_table[i].trgt_id);
Kumar Gala95fd2f62008-01-16 01:13:58 -0600299 }
300
Liu Gangb4611ee2012-08-09 05:10:03 +0000301#ifdef CONFIG_SRIO_PCIE_BOOT_SLAVE
Liu Gange86e1a32012-08-09 05:10:00 +0000302 /* check RCW to get which port is used for boot */
303 ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
304 u32 bootloc = in_be32(&gur->rcwsr[6]);
Liu Gangb4611ee2012-08-09 05:10:03 +0000305 /*
306 * in SRIO or PCIE boot we need to set specail LAWs for
307 * SRIO or PCIE interfaces.
308 */
Liu Gange86e1a32012-08-09 05:10:00 +0000309 switch ((bootloc & FSL_CORENET_RCWSR6_BOOT_LOC) >> 23) {
Liu Gangb4611ee2012-08-09 05:10:03 +0000310 case 0x0: /* boot from PCIE1 */
311 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
312 LAW_SIZE_1M,
313 LAW_TRGT_IF_PCIE_1);
314 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
315 LAW_SIZE_1M,
316 LAW_TRGT_IF_PCIE_1);
317 break;
318 case 0x1: /* boot from PCIE2 */
319 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
320 LAW_SIZE_1M,
321 LAW_TRGT_IF_PCIE_2);
322 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
323 LAW_SIZE_1M,
324 LAW_TRGT_IF_PCIE_2);
325 break;
326 case 0x2: /* boot from PCIE3 */
327 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
328 LAW_SIZE_1M,
329 LAW_TRGT_IF_PCIE_3);
330 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
331 LAW_SIZE_1M,
332 LAW_TRGT_IF_PCIE_3);
333 break;
Liu Gange86e1a32012-08-09 05:10:00 +0000334 case 0x8: /* boot from SRIO1 */
Liu Gangb4611ee2012-08-09 05:10:03 +0000335 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
Liu Gange86e1a32012-08-09 05:10:00 +0000336 LAW_SIZE_1M,
337 LAW_TRGT_IF_RIO_1);
Liu Gangb4611ee2012-08-09 05:10:03 +0000338 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
Liu Gange86e1a32012-08-09 05:10:00 +0000339 LAW_SIZE_1M,
340 LAW_TRGT_IF_RIO_1);
341 break;
342 case 0x9: /* boot from SRIO2 */
Liu Gangb4611ee2012-08-09 05:10:03 +0000343 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
Liu Gange86e1a32012-08-09 05:10:00 +0000344 LAW_SIZE_1M,
345 LAW_TRGT_IF_RIO_2);
Liu Gangb4611ee2012-08-09 05:10:03 +0000346 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
Liu Gange86e1a32012-08-09 05:10:00 +0000347 LAW_SIZE_1M,
348 LAW_TRGT_IF_RIO_2);
349 break;
350 default:
351 break;
352 }
353#endif
354
Kumar Gala95fd2f62008-01-16 01:13:58 -0600355 return ;
356}