blob: 287e5559005172fd4cd4dbfedf11c1d4d63dcff0 [file] [log] [blame]
Kumar Gala83d40df2008-01-16 01:13:58 -06001/*
Kumar Gala418ec852009-03-19 02:32:23 -05002 * Copyright 2008-2009 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 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
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.
14 *
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.
19 *
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,
23 * MA 02111-1307 USA
24 */
25
26#include <common.h>
27#include <asm/fsl_law.h>
28#include <asm/io.h>
29
Kumar Galaf0600542008-06-11 00:44:10 -050030DECLARE_GLOBAL_DATA_PTR;
31
Kumar Galaf0600542008-06-11 00:44:10 -050032/* 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) || \
Haiying Wang22b6dbc2009-03-27 17:02:44 -040037 defined(CONFIG_MPC8568) || defined(CONFIG_MPC8569) || \
Kumar Galaf0600542008-06-11 00:44:10 -050038 defined(CONFIG_MPC8641) || defined(CONFIG_MPC8610)
39#define FSL_HW_NUM_LAWS 10
Srikanth Srinivasan8d949af2009-01-21 17:17:33 -060040#elif defined(CONFIG_MPC8536) || defined(CONFIG_MPC8572) || \
Poonam Aggrwala713ba92009-08-20 18:57:45 +053041 defined(CONFIG_P1011) || defined(CONFIG_P1020) || \
42 defined(CONFIG_P2010) || defined(CONFIG_P2020)
Kumar Galaf0600542008-06-11 00:44:10 -050043#define FSL_HW_NUM_LAWS 12
Kumar Gala7e4259b2009-03-19 02:39:17 -050044#elif defined(CONFIG_PPC_P4080)
45#define FSL_HW_NUM_LAWS 32
Kumar Galaf0600542008-06-11 00:44:10 -050046#else
47#error FSL_HW_NUM_LAWS not defined for this platform
48#endif
Kumar Gala83d40df2008-01-16 01:13:58 -060049
Kumar Gala418ec852009-03-19 02:32:23 -050050#ifdef CONFIG_FSL_CORENET
51void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
52{
53 volatile ccsr_local_t *ccm = (void *)(CONFIG_SYS_FSL_CORENET_CCM_ADDR);
54
55 gd->used_laws |= (1 << idx);
56
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);
61
62 /* Read back so that we sync the writes */
63 in_be32(&ccm->law[idx].lawar);
64}
65
66void disable_law(u8 idx)
67{
68 volatile ccsr_local_t *ccm = (void *)(CONFIG_SYS_FSL_CORENET_CCM_ADDR);
69
70 gd->used_laws &= ~(1 << idx);
71
72 out_be32(&ccm->law[idx].lawar, 0);
73 out_be32(&ccm->law[idx].lawbarh, 0);
74 out_be32(&ccm->law[idx].lawbarl, 0);
75
76 /* Read back so that we sync the writes */
77 in_be32(&ccm->law[idx].lawar);
78
79 return;
80}
81
Kumar Gala24b17d82009-09-30 08:39:44 -050082#ifndef CONFIG_NAND_SPL
Kumar Gala418ec852009-03-19 02:32:23 -050083static int get_law_entry(u8 i, struct law_entry *e)
84{
85 volatile ccsr_local_t *ccm = (void *)(CONFIG_SYS_FSL_CORENET_CCM_ADDR);
86 u32 lawar;
87
88 lawar = in_be32(&ccm->law[i].lawar);
89
90 if (!(lawar & LAW_EN))
91 return 0;
92
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;
97
98 return 1;
99}
Kumar Gala24b17d82009-09-30 08:39:44 -0500100#endif
Kumar Gala418ec852009-03-19 02:32:23 -0500101#else
Kumar Gala83d40df2008-01-16 01:13:58 -0600102void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
103{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200104 volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
Kumar Gala83d40df2008-01-16 01:13:58 -0600105 volatile u32 *lawbar = base + 8 * idx;
106 volatile u32 *lawar = base + 8 * idx + 2;
107
Kumar Galaf0600542008-06-11 00:44:10 -0500108 gd->used_laws |= (1 << idx);
109
Ed Swarthoute1f7d222008-10-09 01:25:55 -0500110 out_be32(lawar, 0);
Kumar Gala83d40df2008-01-16 01:13:58 -0600111 out_be32(lawbar, addr >> 12);
Kumar Gala418ec852009-03-19 02:32:23 -0500112 out_be32(lawar, LAW_EN | ((u32)id << 20) | (u32)sz);
Kumar Gala83d40df2008-01-16 01:13:58 -0600113
Timur Tabi74c5dfd2009-09-04 17:05:24 -0500114 /* Read back so that we sync the writes */
115 in_be32(lawar);
Kumar Gala83d40df2008-01-16 01:13:58 -0600116}
117
Kumar Gala418ec852009-03-19 02:32:23 -0500118void disable_law(u8 idx)
119{
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;
123
124 gd->used_laws &= ~(1 << idx);
125
126 out_be32(lawar, 0);
127 out_be32(lawbar, 0);
128
129 /* Read back so that we sync the writes */
130 in_be32(lawar);
131
132 return;
133}
134
Kumar Gala24b17d82009-09-30 08:39:44 -0500135#ifndef CONFIG_NAND_SPL
Kumar Gala418ec852009-03-19 02:32:23 -0500136static int get_law_entry(u8 i, struct law_entry *e)
137{
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;
141 u32 temp;
142
143 temp = in_be32(lawar);
144
145 if (!(temp & LAW_EN))
146 return 0;
147
148 e->addr = (u64)in_be32(lawbar) << 12;
149 e->size = temp & 0x3f;
150 e->trgt_id = (temp >> 20) & 0xff;
151
152 return 1;
153}
154#endif
Kumar Gala24b17d82009-09-30 08:39:44 -0500155#endif
Kumar Gala418ec852009-03-19 02:32:23 -0500156
Kumar Galaf0600542008-06-11 00:44:10 -0500157int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
158{
159 u32 idx = ffz(gd->used_laws);
160
161 if (idx >= FSL_HW_NUM_LAWS)
162 return -1;
163
164 set_law(idx, addr, sz, id);
165
166 return idx;
167}
168
Mingkai Hu7da53352009-09-11 14:19:10 +0800169#ifndef CONFIG_NAND_SPL
Kumar Galaba04f702008-06-10 16:16:02 -0500170int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
171{
172 u32 idx;
173
174 /* we have no LAWs free */
175 if (gd->used_laws == -1)
176 return -1;
177
178 /* grab the last free law */
179 idx = __ilog2(~(gd->used_laws));
180
181 if (idx >= FSL_HW_NUM_LAWS)
182 return -1;
183
184 set_law(idx, addr, sz, id);
185
186 return idx;
187}
188
Kumar Gala418ec852009-03-19 02:32:23 -0500189struct law_entry find_law(phys_addr_t addr)
Kumar Gala83d40df2008-01-16 01:13:58 -0600190{
Kumar Gala418ec852009-03-19 02:32:23 -0500191 struct law_entry entry;
192 int i;
Kumar Gala83d40df2008-01-16 01:13:58 -0600193
Kumar Gala418ec852009-03-19 02:32:23 -0500194 entry.index = -1;
195 entry.addr = 0;
196 entry.size = 0;
197 entry.trgt_id = 0;
Kumar Galaf0600542008-06-11 00:44:10 -0500198
Kumar Gala418ec852009-03-19 02:32:23 -0500199 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
200 u64 upper;
Kumar Gala83d40df2008-01-16 01:13:58 -0600201
Kumar Gala418ec852009-03-19 02:32:23 -0500202 if (!get_law_entry(i, &entry))
203 continue;
204
205 upper = entry.addr + (2ull << entry.size);
206 if ((addr >= entry.addr) && (addr < upper)) {
207 entry.index = i;
208 break;
209 }
210 }
211
212 return entry;
Kumar Gala83d40df2008-01-16 01:13:58 -0600213}
214
Becky Bruceddcebcb2008-01-23 16:31:05 -0600215void print_laws(void)
216{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200217 volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
Becky Bruceddcebcb2008-01-23 16:31:05 -0600218 volatile u32 *lawbar = base;
219 volatile u32 *lawar = base + 2;
220 int i;
221
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));
226 lawbar += 8;
227 lawar += 8;
228 }
229
230 return;
231}
232
Kumar Galaf784e322008-08-26 15:01:28 -0500233/* use up to 2 LAWs for DDR, used the last available LAWs */
234int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
235{
236 u64 start_align, law_sz;
237 int law_sz_enc;
238
239 if (start == 0)
240 start_align = 1ull << (LAW_SIZE_32G + 1);
241 else
242 start_align = 1ull << (ffs64(start) - 1);
243 law_sz = min(start_align, sz);
244 law_sz_enc = __ilog2_u64(law_sz) - 1;
245
246 if (set_last_law(start, law_sz_enc, id) < 0)
247 return -1;
248
Kumar Galae6a67892009-04-04 10:21:02 -0500249 /* recalculate size based on what was actually covered by the law */
250 law_sz = 1ull << __ilog2_u64(law_sz);
251
Kumar Galaf784e322008-08-26 15:01:28 -0500252 /* do we still have anything to map */
253 sz = sz - law_sz;
254 if (sz) {
255 start += law_sz;
256
257 start_align = 1ull << (ffs64(start) - 1);
258 law_sz = min(start_align, sz);
259 law_sz_enc = __ilog2_u64(law_sz) - 1;
260
261 if (set_last_law(start, law_sz_enc, id) < 0)
262 return -1;
263 } else {
264 return 0;
265 }
266
267 /* do we still have anything to map */
268 sz = sz - law_sz;
269 if (sz)
270 return 1;
271
272 return 0;
273}
Mingkai Hu7da53352009-09-11 14:19:10 +0800274#endif
Kumar Galaf784e322008-08-26 15:01:28 -0500275
Kumar Gala83d40df2008-01-16 01:13:58 -0600276void init_laws(void)
277{
278 int i;
Kumar Galaf0600542008-06-11 00:44:10 -0500279
Kumar Gala418ec852009-03-19 02:32:23 -0500280#if FSL_HW_NUM_LAWS < 32
Kumar Galaf0600542008-06-11 00:44:10 -0500281 gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
Kumar Gala418ec852009-03-19 02:32:23 -0500282#elif FSL_HW_NUM_LAWS == 32
283 gd->used_laws = 0;
284#else
285#error FSL_HW_NUM_LAWS can not be greater than 32 w/o code changes
286#endif
Kumar Gala83d40df2008-01-16 01:13:58 -0600287
288 for (i = 0; i < num_law_entries; i++) {
Kumar Galaf0600542008-06-11 00:44:10 -0500289 if (law_table[i].index == -1)
290 set_next_law(law_table[i].addr, law_table[i].size,
291 law_table[i].trgt_id);
292 else
293 set_law(law_table[i].index, law_table[i].addr,
294 law_table[i].size, law_table[i].trgt_id);
Kumar Gala83d40df2008-01-16 01:13:58 -0600295 }
296
297 return ;
298}