2 * Copyright 2008-2009 Freescale Semiconductor, Inc.
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <asm/fsl_law.h>
30 DECLARE_GLOBAL_DATA_PTR;
32 /* number of LAWs in the hw implementation */
33 #if defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || \
34 defined(CONFIG_MPC8560) || defined(CONFIG_MPC8555)
35 #define FSL_HW_NUM_LAWS 8
36 #elif defined(CONFIG_MPC8548) || defined(CONFIG_MPC8544) || \
37 defined(CONFIG_MPC8568) || defined(CONFIG_MPC8569) || \
38 defined(CONFIG_MPC8641) || defined(CONFIG_MPC8610)
39 #define FSL_HW_NUM_LAWS 10
40 #elif defined(CONFIG_MPC8536) || defined(CONFIG_MPC8572) || \
41 defined(CONFIG_P1011) || defined(CONFIG_P1020) || \
42 defined(CONFIG_P2010) || defined(CONFIG_P2020)
43 #define FSL_HW_NUM_LAWS 12
44 #elif defined(CONFIG_PPC_P4080)
45 #define FSL_HW_NUM_LAWS 32
47 #error FSL_HW_NUM_LAWS not defined for this platform
50 #ifdef CONFIG_FSL_CORENET
51 void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
53 volatile ccsr_local_t *ccm = (void *)(CONFIG_SYS_FSL_CORENET_CCM_ADDR);
55 gd->used_laws |= (1 << idx);
57 out_be32(&ccm->law[idx].lawar, 0);
58 out_be32(&ccm->law[idx].lawbarh, ((u64)addr >> 32));
59 out_be32(&ccm->law[idx].lawbarl, addr & 0xffffffff);
60 out_be32(&ccm->law[idx].lawar, LAW_EN | ((u32)id << 20) | (u32)sz);
62 /* Read back so that we sync the writes */
63 in_be32(&ccm->law[idx].lawar);
66 void disable_law(u8 idx)
68 volatile ccsr_local_t *ccm = (void *)(CONFIG_SYS_FSL_CORENET_CCM_ADDR);
70 gd->used_laws &= ~(1 << idx);
72 out_be32(&ccm->law[idx].lawar, 0);
73 out_be32(&ccm->law[idx].lawbarh, 0);
74 out_be32(&ccm->law[idx].lawbarl, 0);
76 /* Read back so that we sync the writes */
77 in_be32(&ccm->law[idx].lawar);
82 #ifndef CONFIG_NAND_SPL
83 static int get_law_entry(u8 i, struct law_entry *e)
85 volatile ccsr_local_t *ccm = (void *)(CONFIG_SYS_FSL_CORENET_CCM_ADDR);
88 lawar = in_be32(&ccm->law[i].lawar);
90 if (!(lawar & LAW_EN))
93 e->addr = ((u64)in_be32(&ccm->law[i].lawbarh) << 32) |
94 in_be32(&ccm->law[i].lawbarl);
95 e->size = lawar & 0x3f;
96 e->trgt_id = (lawar >> 20) & 0xff;
102 void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
104 volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
105 volatile u32 *lawbar = base + 8 * idx;
106 volatile u32 *lawar = base + 8 * idx + 2;
108 gd->used_laws |= (1 << idx);
111 out_be32(lawbar, addr >> 12);
112 out_be32(lawar, LAW_EN | ((u32)id << 20) | (u32)sz);
114 /* Read back so that we sync the writes */
118 void disable_law(u8 idx)
120 volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
121 volatile u32 *lawbar = base + 8 * idx;
122 volatile u32 *lawar = base + 8 * idx + 2;
124 gd->used_laws &= ~(1 << idx);
129 /* Read back so that we sync the writes */
135 #ifndef CONFIG_NAND_SPL
136 static int get_law_entry(u8 i, struct law_entry *e)
138 volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
139 volatile u32 *lawbar = base + 8 * i;
140 volatile u32 *lawar = base + 8 * i + 2;
143 temp = in_be32(lawar);
145 if (!(temp & LAW_EN))
148 e->addr = (u64)in_be32(lawbar) << 12;
149 e->size = temp & 0x3f;
150 e->trgt_id = (temp >> 20) & 0xff;
157 int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
159 u32 idx = ffz(gd->used_laws);
161 if (idx >= FSL_HW_NUM_LAWS)
164 set_law(idx, addr, sz, id);
169 #ifndef CONFIG_NAND_SPL
170 int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
174 /* we have no LAWs free */
175 if (gd->used_laws == -1)
178 /* grab the last free law */
179 idx = __ilog2(~(gd->used_laws));
181 if (idx >= FSL_HW_NUM_LAWS)
184 set_law(idx, addr, sz, id);
189 struct law_entry find_law(phys_addr_t addr)
191 struct law_entry entry;
199 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
202 if (!get_law_entry(i, &entry))
205 upper = entry.addr + (2ull << entry.size);
206 if ((addr >= entry.addr) && (addr < upper)) {
215 void print_laws(void)
217 volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
218 volatile u32 *lawbar = base;
219 volatile u32 *lawar = base + 2;
222 printf("\nLocal Access Window Configuration\n");
223 for(i = 0; i < FSL_HW_NUM_LAWS; i++) {
224 printf("\tLAWBAR%d : 0x%08x, LAWAR%d : 0x%08x\n",
225 i, in_be32(lawbar), i, in_be32(lawar));
233 /* use up to 2 LAWs for DDR, used the last available LAWs */
234 int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
236 u64 start_align, law_sz;
240 start_align = 1ull << (LAW_SIZE_32G + 1);
242 start_align = 1ull << (ffs64(start) - 1);
243 law_sz = min(start_align, sz);
244 law_sz_enc = __ilog2_u64(law_sz) - 1;
246 if (set_last_law(start, law_sz_enc, id) < 0)
249 /* recalculate size based on what was actually covered by the law */
250 law_sz = 1ull << __ilog2_u64(law_sz);
252 /* do we still have anything to map */
257 start_align = 1ull << (ffs64(start) - 1);
258 law_sz = min(start_align, sz);
259 law_sz_enc = __ilog2_u64(law_sz) - 1;
261 if (set_last_law(start, law_sz_enc, id) < 0)
267 /* do we still have anything to map */
280 #if FSL_HW_NUM_LAWS < 32
281 gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
282 #elif FSL_HW_NUM_LAWS == 32
285 #error FSL_HW_NUM_LAWS can not be greater than 32 w/o code changes
288 for (i = 0; i < num_law_entries; i++) {
289 if (law_table[i].index == -1)
290 set_next_law(law_table[i].addr, law_table[i].size,
291 law_table[i].trgt_id);
293 set_law(law_table[i].index, law_table[i].addr,
294 law_table[i].size, law_table[i].trgt_id);