blob: bd79297f0f7a562c28a83b7aeb8036f1f130c29a [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>
Fabio Estevam2d2f4902015-11-05 12:43:40 -020014#include <linux/log2.h>
Kumar Gala83d40df2008-01-16 01:13:58 -060015
Kumar Galaf0600542008-06-11 00:44:10 -050016DECLARE_GLOBAL_DATA_PTR;
17
Kumar Gala243be8e2011-01-19 03:05:26 -060018#define FSL_HW_NUM_LAWS CONFIG_SYS_FSL_NUM_LAWS
Kumar Gala83d40df2008-01-16 01:13:58 -060019
Kumar Gala418ec852009-03-19 02:32:23 -050020#ifdef CONFIG_FSL_CORENET
Becky Brucee71755f2010-06-17 11:37:23 -050021#define LAW_BASE (CONFIG_SYS_FSL_CORENET_CCM_ADDR)
22#define LAWAR_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawar)
23#define LAWBARH_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarh)
24#define LAWBARL_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarl)
25#define LAWBAR_SHIFT 0
26#else
27#define LAW_BASE (CONFIG_SYS_IMMR + 0xc08)
28#define LAWAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x + 2)
29#define LAWBAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x)
30#define LAWBAR_SHIFT 12
31#endif
32
33
34static inline phys_addr_t get_law_base_addr(int idx)
35{
36#ifdef CONFIG_FSL_CORENET
37 return (phys_addr_t)
38 ((u64)in_be32(LAWBARH_ADDR(idx)) << 32) |
39 in_be32(LAWBARL_ADDR(idx));
40#else
41 return (phys_addr_t)in_be32(LAWBAR_ADDR(idx)) << LAWBAR_SHIFT;
42#endif
43}
44
45static inline void set_law_base_addr(int idx, phys_addr_t addr)
46{
47#ifdef CONFIG_FSL_CORENET
48 out_be32(LAWBARL_ADDR(idx), addr & 0xffffffff);
49 out_be32(LAWBARH_ADDR(idx), (u64)addr >> 32);
50#else
51 out_be32(LAWBAR_ADDR(idx), addr >> LAWBAR_SHIFT);
52#endif
53}
54
Kumar Gala418ec852009-03-19 02:32:23 -050055void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
56{
Simon Glass8670dbc2012-12-13 20:48:51 +000057 gd->arch.used_laws |= (1 << idx);
Kumar Gala418ec852009-03-19 02:32:23 -050058
Becky Brucee71755f2010-06-17 11:37:23 -050059 out_be32(LAWAR_ADDR(idx), 0);
60 set_law_base_addr(idx, addr);
61 out_be32(LAWAR_ADDR(idx), LAW_EN | ((u32)id << 20) | (u32)sz);
Kumar Gala418ec852009-03-19 02:32:23 -050062
63 /* Read back so that we sync the writes */
Becky Brucee71755f2010-06-17 11:37:23 -050064 in_be32(LAWAR_ADDR(idx));
Kumar Gala418ec852009-03-19 02:32:23 -050065}
66
67void disable_law(u8 idx)
68{
Simon Glass8670dbc2012-12-13 20:48:51 +000069 gd->arch.used_laws &= ~(1 << idx);
Kumar Gala418ec852009-03-19 02:32:23 -050070
Becky Brucee71755f2010-06-17 11:37:23 -050071 out_be32(LAWAR_ADDR(idx), 0);
72 set_law_base_addr(idx, 0);
Kumar Gala418ec852009-03-19 02:32:23 -050073
74 /* Read back so that we sync the writes */
Becky Brucee71755f2010-06-17 11:37:23 -050075 in_be32(LAWAR_ADDR(idx));
Kumar Gala418ec852009-03-19 02:32:23 -050076
77 return;
78}
79
Ying Zhang0151d992013-08-16 15:16:10 +080080#if !defined(CONFIG_NAND_SPL) && \
81 (!defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SPL_INIT_MINIMAL))
Kumar Gala418ec852009-03-19 02:32:23 -050082static int get_law_entry(u8 i, struct law_entry *e)
83{
Kumar Gala418ec852009-03-19 02:32:23 -050084 u32 lawar;
85
Becky Brucee71755f2010-06-17 11:37:23 -050086 lawar = in_be32(LAWAR_ADDR(i));
Kumar Gala418ec852009-03-19 02:32:23 -050087
88 if (!(lawar & LAW_EN))
89 return 0;
90
Becky Brucee71755f2010-06-17 11:37:23 -050091 e->addr = get_law_base_addr(i);
Kumar Gala418ec852009-03-19 02:32:23 -050092 e->size = lawar & 0x3f;
93 e->trgt_id = (lawar >> 20) & 0xff;
94
95 return 1;
96}
Kumar Gala24b17d82009-09-30 08:39:44 -050097#endif
Kumar Gala418ec852009-03-19 02:32:23 -050098
Kumar Galaf0600542008-06-11 00:44:10 -050099int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
100{
Simon Glass8670dbc2012-12-13 20:48:51 +0000101 u32 idx = ffz(gd->arch.used_laws);
Kumar Galaf0600542008-06-11 00:44:10 -0500102
103 if (idx >= FSL_HW_NUM_LAWS)
104 return -1;
105
106 set_law(idx, addr, sz, id);
107
108 return idx;
109}
110
Ying Zhang0151d992013-08-16 15:16:10 +0800111#if !defined(CONFIG_NAND_SPL) && \
112 (!defined(CONFIG_SPL_BUILD) || !defined(CONFIG_SPL_INIT_MINIMAL))
Kumar Galaba04f702008-06-10 16:16:02 -0500113int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
114{
115 u32 idx;
116
117 /* we have no LAWs free */
Simon Glass8670dbc2012-12-13 20:48:51 +0000118 if (gd->arch.used_laws == -1)
Kumar Galaba04f702008-06-10 16:16:02 -0500119 return -1;
120
121 /* grab the last free law */
Simon Glass8670dbc2012-12-13 20:48:51 +0000122 idx = __ilog2(~(gd->arch.used_laws));
Kumar Galaba04f702008-06-10 16:16:02 -0500123
124 if (idx >= FSL_HW_NUM_LAWS)
125 return -1;
126
127 set_law(idx, addr, sz, id);
128
129 return idx;
130}
131
Kumar Gala418ec852009-03-19 02:32:23 -0500132struct law_entry find_law(phys_addr_t addr)
Kumar Gala83d40df2008-01-16 01:13:58 -0600133{
Kumar Gala418ec852009-03-19 02:32:23 -0500134 struct law_entry entry;
135 int i;
Kumar Gala83d40df2008-01-16 01:13:58 -0600136
Kumar Gala418ec852009-03-19 02:32:23 -0500137 entry.index = -1;
138 entry.addr = 0;
139 entry.size = 0;
140 entry.trgt_id = 0;
Kumar Galaf0600542008-06-11 00:44:10 -0500141
Kumar Gala418ec852009-03-19 02:32:23 -0500142 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
143 u64 upper;
Kumar Gala83d40df2008-01-16 01:13:58 -0600144
Kumar Gala418ec852009-03-19 02:32:23 -0500145 if (!get_law_entry(i, &entry))
146 continue;
147
148 upper = entry.addr + (2ull << entry.size);
149 if ((addr >= entry.addr) && (addr < upper)) {
150 entry.index = i;
151 break;
152 }
153 }
154
155 return entry;
Kumar Gala83d40df2008-01-16 01:13:58 -0600156}
157
Becky Bruceddcebcb2008-01-23 16:31:05 -0600158void print_laws(void)
159{
Becky Bruceddcebcb2008-01-23 16:31:05 -0600160 int i;
Becky Brucee71755f2010-06-17 11:37:23 -0500161 u32 lawar;
Becky Bruceddcebcb2008-01-23 16:31:05 -0600162
163 printf("\nLocal Access Window Configuration\n");
Becky Brucee71755f2010-06-17 11:37:23 -0500164 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
165 lawar = in_be32(LAWAR_ADDR(i));
Becky Bruce11a3de42010-06-17 11:37:24 -0500166#ifdef CONFIG_FSL_CORENET
167 printf("LAWBARH%02d: 0x%08x LAWBARL%02d: 0x%08x",
168 i, in_be32(LAWBARH_ADDR(i)),
169 i, in_be32(LAWBARL_ADDR(i)));
170#else
Becky Brucee71755f2010-06-17 11:37:23 -0500171 printf("LAWBAR%02d: 0x%08x", i, in_be32(LAWBAR_ADDR(i)));
Becky Bruce11a3de42010-06-17 11:37:24 -0500172#endif
Kumar Gala8e29eba2011-02-12 15:34:08 -0600173 printf(" LAWAR%02d: 0x%08x\n", i, lawar);
Becky Brucee71755f2010-06-17 11:37:23 -0500174 printf("\t(EN: %d TGT: 0x%02x SIZE: ",
175 (lawar & LAW_EN) ? 1 : 0, (lawar >> 20) & 0xff);
176 print_size(lawar_size(lawar), ")\n");
Becky Bruceddcebcb2008-01-23 16:31:05 -0600177 }
178
179 return;
180}
181
Kumar Galaf784e322008-08-26 15:01:28 -0500182/* use up to 2 LAWs for DDR, used the last available LAWs */
183int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
184{
185 u64 start_align, law_sz;
186 int law_sz_enc;
187
188 if (start == 0)
189 start_align = 1ull << (LAW_SIZE_32G + 1);
190 else
Ashish kumar43381472016-01-22 15:50:10 +0530191 start_align = 1ull << (__ffs64(start));
Kumar Galaf784e322008-08-26 15:01:28 -0500192 law_sz = min(start_align, sz);
193 law_sz_enc = __ilog2_u64(law_sz) - 1;
194
195 if (set_last_law(start, law_sz_enc, id) < 0)
196 return -1;
197
Kumar Galae6a67892009-04-04 10:21:02 -0500198 /* recalculate size based on what was actually covered by the law */
199 law_sz = 1ull << __ilog2_u64(law_sz);
200
Kumar Galaf784e322008-08-26 15:01:28 -0500201 /* do we still have anything to map */
202 sz = sz - law_sz;
203 if (sz) {
204 start += law_sz;
205
Ashish kumar43381472016-01-22 15:50:10 +0530206 start_align = 1ull << (__ffs64(start));
Kumar Galaf784e322008-08-26 15:01:28 -0500207 law_sz = min(start_align, sz);
208 law_sz_enc = __ilog2_u64(law_sz) - 1;
209
210 if (set_last_law(start, law_sz_enc, id) < 0)
211 return -1;
212 } else {
213 return 0;
214 }
215
216 /* do we still have anything to map */
217 sz = sz - law_sz;
218 if (sz)
219 return 1;
220
221 return 0;
222}
Scott Woodc97cd1b2012-09-20 19:02:18 -0500223#endif /* not SPL */
Kumar Galaf784e322008-08-26 15:01:28 -0500224
Prabhakar Kushwaha6b3d5882014-04-08 19:12:46 +0530225void disable_non_ddr_laws(void)
226{
227 int i;
228 int id;
229 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
230 u32 lawar = in_be32(LAWAR_ADDR(i));
231
232 if (lawar & LAW_EN) {
233 id = (lawar & ~LAW_EN) >> 20;
234 switch (id) {
235 case LAW_TRGT_IF_DDR_1:
236 case LAW_TRGT_IF_DDR_2:
237 case LAW_TRGT_IF_DDR_3:
238 case LAW_TRGT_IF_DDR_4:
239 case LAW_TRGT_IF_DDR_INTRLV:
240 case LAW_TRGT_IF_DDR_INTLV_34:
241 case LAW_TRGT_IF_DDR_INTLV_123:
242 case LAW_TRGT_IF_DDR_INTLV_1234:
243 continue;
244 default:
245 disable_law(i);
246 }
247 }
248 }
249}
250
Kumar Gala83d40df2008-01-16 01:13:58 -0600251void init_laws(void)
252{
253 int i;
Kumar Galaf0600542008-06-11 00:44:10 -0500254
Kumar Gala418ec852009-03-19 02:32:23 -0500255#if FSL_HW_NUM_LAWS < 32
Simon Glass8670dbc2012-12-13 20:48:51 +0000256 gd->arch.used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
Kumar Gala418ec852009-03-19 02:32:23 -0500257#elif FSL_HW_NUM_LAWS == 32
Simon Glass8670dbc2012-12-13 20:48:51 +0000258 gd->arch.used_laws = 0;
Kumar Gala418ec852009-03-19 02:32:23 -0500259#else
260#error FSL_HW_NUM_LAWS can not be greater than 32 w/o code changes
261#endif
Kumar Gala83d40df2008-01-16 01:13:58 -0600262
Aneesh Bansal7efb4b52014-03-11 23:21:45 +0530263#if defined(CONFIG_SECURE_BOOT) && defined(CONFIG_E500) && \
264 !defined(CONFIG_E500MC)
265 /* ISBC (Boot ROM) creates a LAW 0 entry for non PBL platforms,
266 * which is not disabled before transferring the control to uboot.
267 * Disable the LAW 0 entry here.
268 */
269 disable_law(0);
270#endif
271
Prabhakar Kushwaha6b3d5882014-04-08 19:12:46 +0530272#if !defined(CONFIG_SECURE_BOOT)
273 /*
274 * if any non DDR LAWs has been created earlier, remove them before
275 * LAW table is parsed.
276 */
277 disable_non_ddr_laws();
278#endif
Aneesh Bansal7efb4b52014-03-11 23:21:45 +0530279
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200280 /*
Kumar Gala76396752011-02-03 09:02:13 -0600281 * Any LAWs that were set up before we booted assume they are meant to
282 * be around and mark them used.
283 */
284 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
285 u32 lawar = in_be32(LAWAR_ADDR(i));
Wolfgang Denkcd6881b2011-05-19 22:21:41 +0200286
Kumar Gala76396752011-02-03 09:02:13 -0600287 if (lawar & LAW_EN)
Simon Glass8670dbc2012-12-13 20:48:51 +0000288 gd->arch.used_laws |= (1 << i);
Kumar Gala76396752011-02-03 09:02:13 -0600289 }
290
Kumar Gala83d40df2008-01-16 01:13:58 -0600291 for (i = 0; i < num_law_entries; i++) {
Kumar Galaf0600542008-06-11 00:44:10 -0500292 if (law_table[i].index == -1)
293 set_next_law(law_table[i].addr, law_table[i].size,
294 law_table[i].trgt_id);
295 else
296 set_law(law_table[i].index, law_table[i].addr,
297 law_table[i].size, law_table[i].trgt_id);
Kumar Gala83d40df2008-01-16 01:13:58 -0600298 }
299
Liu Gang461632b2012-08-09 05:10:03 +0000300#ifdef CONFIG_SRIO_PCIE_BOOT_SLAVE
Liu Gang81fa73b2012-08-09 05:10:00 +0000301 /* check RCW to get which port is used for boot */
302 ccsr_gur_t *gur = (void *)CONFIG_SYS_MPC85xx_GUTS_ADDR;
303 u32 bootloc = in_be32(&gur->rcwsr[6]);
Liu Gang461632b2012-08-09 05:10:03 +0000304 /*
305 * in SRIO or PCIE boot we need to set specail LAWs for
306 * SRIO or PCIE interfaces.
307 */
Liu Gang81fa73b2012-08-09 05:10:00 +0000308 switch ((bootloc & FSL_CORENET_RCWSR6_BOOT_LOC) >> 23) {
Liu Gang461632b2012-08-09 05:10:03 +0000309 case 0x0: /* boot from PCIE1 */
310 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
311 LAW_SIZE_1M,
312 LAW_TRGT_IF_PCIE_1);
313 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
314 LAW_SIZE_1M,
315 LAW_TRGT_IF_PCIE_1);
316 break;
317 case 0x1: /* boot from PCIE2 */
318 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
319 LAW_SIZE_1M,
320 LAW_TRGT_IF_PCIE_2);
321 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
322 LAW_SIZE_1M,
323 LAW_TRGT_IF_PCIE_2);
324 break;
325 case 0x2: /* boot from PCIE3 */
326 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
327 LAW_SIZE_1M,
328 LAW_TRGT_IF_PCIE_3);
329 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
330 LAW_SIZE_1M,
331 LAW_TRGT_IF_PCIE_3);
332 break;
Liu Gang81fa73b2012-08-09 05:10:00 +0000333 case 0x8: /* boot from SRIO1 */
Liu Gang461632b2012-08-09 05:10:03 +0000334 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000335 LAW_SIZE_1M,
336 LAW_TRGT_IF_RIO_1);
Liu Gang461632b2012-08-09 05:10:03 +0000337 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000338 LAW_SIZE_1M,
339 LAW_TRGT_IF_RIO_1);
340 break;
341 case 0x9: /* boot from SRIO2 */
Liu Gang461632b2012-08-09 05:10:03 +0000342 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_SLAVE_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000343 LAW_SIZE_1M,
344 LAW_TRGT_IF_RIO_2);
Liu Gang461632b2012-08-09 05:10:03 +0000345 set_next_law(CONFIG_SYS_SRIO_PCIE_BOOT_UCODE_ENV_ADDR_PHYS,
Liu Gang81fa73b2012-08-09 05:10:00 +0000346 LAW_SIZE_1M,
347 LAW_TRGT_IF_RIO_2);
348 break;
349 default:
350 break;
351 }
352#endif
353
Kumar Gala83d40df2008-01-16 01:13:58 -0600354 return ;
355}