blob: 63c08bf2ffca33360b7adf4aa9b06c3ebf131993 [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 *
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 Gala243be8e2011-01-19 03:05:26 -060032#define FSL_HW_NUM_LAWS CONFIG_SYS_FSL_NUM_LAWS
Kumar Gala83d40df2008-01-16 01:13:58 -060033
Kumar Gala418ec852009-03-19 02:32:23 -050034#ifdef CONFIG_FSL_CORENET
Becky Brucee71755f2010-06-17 11:37:23 -050035#define LAW_BASE (CONFIG_SYS_FSL_CORENET_CCM_ADDR)
36#define LAWAR_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawar)
37#define LAWBARH_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarh)
38#define LAWBARL_ADDR(x) (&((ccsr_local_t *)LAW_BASE)->law[x].lawbarl)
39#define LAWBAR_SHIFT 0
40#else
41#define LAW_BASE (CONFIG_SYS_IMMR + 0xc08)
42#define LAWAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x + 2)
43#define LAWBAR_ADDR(x) ((u32 *)LAW_BASE + 8 * x)
44#define LAWBAR_SHIFT 12
45#endif
46
47
48static inline phys_addr_t get_law_base_addr(int idx)
49{
50#ifdef CONFIG_FSL_CORENET
51 return (phys_addr_t)
52 ((u64)in_be32(LAWBARH_ADDR(idx)) << 32) |
53 in_be32(LAWBARL_ADDR(idx));
54#else
55 return (phys_addr_t)in_be32(LAWBAR_ADDR(idx)) << LAWBAR_SHIFT;
56#endif
57}
58
59static inline void set_law_base_addr(int idx, phys_addr_t addr)
60{
61#ifdef CONFIG_FSL_CORENET
62 out_be32(LAWBARL_ADDR(idx), addr & 0xffffffff);
63 out_be32(LAWBARH_ADDR(idx), (u64)addr >> 32);
64#else
65 out_be32(LAWBAR_ADDR(idx), addr >> LAWBAR_SHIFT);
66#endif
67}
68
Kumar Gala418ec852009-03-19 02:32:23 -050069void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
70{
Kumar Gala418ec852009-03-19 02:32:23 -050071 gd->used_laws |= (1 << idx);
72
Becky Brucee71755f2010-06-17 11:37:23 -050073 out_be32(LAWAR_ADDR(idx), 0);
74 set_law_base_addr(idx, addr);
75 out_be32(LAWAR_ADDR(idx), LAW_EN | ((u32)id << 20) | (u32)sz);
Kumar Gala418ec852009-03-19 02:32:23 -050076
77 /* Read back so that we sync the writes */
Becky Brucee71755f2010-06-17 11:37:23 -050078 in_be32(LAWAR_ADDR(idx));
Kumar Gala418ec852009-03-19 02:32:23 -050079}
80
81void disable_law(u8 idx)
82{
Kumar Gala418ec852009-03-19 02:32:23 -050083 gd->used_laws &= ~(1 << idx);
84
Becky Brucee71755f2010-06-17 11:37:23 -050085 out_be32(LAWAR_ADDR(idx), 0);
86 set_law_base_addr(idx, 0);
Kumar Gala418ec852009-03-19 02:32:23 -050087
88 /* Read back so that we sync the writes */
Becky Brucee71755f2010-06-17 11:37:23 -050089 in_be32(LAWAR_ADDR(idx));
Kumar Gala418ec852009-03-19 02:32:23 -050090
91 return;
92}
93
Kumar Gala24b17d82009-09-30 08:39:44 -050094#ifndef CONFIG_NAND_SPL
Kumar Gala418ec852009-03-19 02:32:23 -050095static int get_law_entry(u8 i, struct law_entry *e)
96{
Kumar Gala418ec852009-03-19 02:32:23 -050097 u32 lawar;
98
Becky Brucee71755f2010-06-17 11:37:23 -050099 lawar = in_be32(LAWAR_ADDR(i));
Kumar Gala418ec852009-03-19 02:32:23 -0500100
101 if (!(lawar & LAW_EN))
102 return 0;
103
Becky Brucee71755f2010-06-17 11:37:23 -0500104 e->addr = get_law_base_addr(i);
Kumar Gala418ec852009-03-19 02:32:23 -0500105 e->size = lawar & 0x3f;
106 e->trgt_id = (lawar >> 20) & 0xff;
107
108 return 1;
109}
Kumar Gala24b17d82009-09-30 08:39:44 -0500110#endif
Kumar Gala418ec852009-03-19 02:32:23 -0500111
Kumar Galaf0600542008-06-11 00:44:10 -0500112int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
113{
114 u32 idx = ffz(gd->used_laws);
115
116 if (idx >= FSL_HW_NUM_LAWS)
117 return -1;
118
119 set_law(idx, addr, sz, id);
120
121 return idx;
122}
123
Mingkai Hu7da53352009-09-11 14:19:10 +0800124#ifndef CONFIG_NAND_SPL
Kumar Galaba04f702008-06-10 16:16:02 -0500125int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
126{
127 u32 idx;
128
129 /* we have no LAWs free */
130 if (gd->used_laws == -1)
131 return -1;
132
133 /* grab the last free law */
134 idx = __ilog2(~(gd->used_laws));
135
136 if (idx >= FSL_HW_NUM_LAWS)
137 return -1;
138
139 set_law(idx, addr, sz, id);
140
141 return idx;
142}
143
Kumar Gala418ec852009-03-19 02:32:23 -0500144struct law_entry find_law(phys_addr_t addr)
Kumar Gala83d40df2008-01-16 01:13:58 -0600145{
Kumar Gala418ec852009-03-19 02:32:23 -0500146 struct law_entry entry;
147 int i;
Kumar Gala83d40df2008-01-16 01:13:58 -0600148
Kumar Gala418ec852009-03-19 02:32:23 -0500149 entry.index = -1;
150 entry.addr = 0;
151 entry.size = 0;
152 entry.trgt_id = 0;
Kumar Galaf0600542008-06-11 00:44:10 -0500153
Kumar Gala418ec852009-03-19 02:32:23 -0500154 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
155 u64 upper;
Kumar Gala83d40df2008-01-16 01:13:58 -0600156
Kumar Gala418ec852009-03-19 02:32:23 -0500157 if (!get_law_entry(i, &entry))
158 continue;
159
160 upper = entry.addr + (2ull << entry.size);
161 if ((addr >= entry.addr) && (addr < upper)) {
162 entry.index = i;
163 break;
164 }
165 }
166
167 return entry;
Kumar Gala83d40df2008-01-16 01:13:58 -0600168}
169
Becky Bruceddcebcb2008-01-23 16:31:05 -0600170void print_laws(void)
171{
Becky Bruceddcebcb2008-01-23 16:31:05 -0600172 int i;
Becky Brucee71755f2010-06-17 11:37:23 -0500173 u32 lawar;
Becky Bruceddcebcb2008-01-23 16:31:05 -0600174
175 printf("\nLocal Access Window Configuration\n");
Becky Brucee71755f2010-06-17 11:37:23 -0500176 for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
177 lawar = in_be32(LAWAR_ADDR(i));
Becky Bruce11a3de42010-06-17 11:37:24 -0500178#ifdef CONFIG_FSL_CORENET
179 printf("LAWBARH%02d: 0x%08x LAWBARL%02d: 0x%08x",
180 i, in_be32(LAWBARH_ADDR(i)),
181 i, in_be32(LAWBARL_ADDR(i)));
182#else
Becky Brucee71755f2010-06-17 11:37:23 -0500183 printf("LAWBAR%02d: 0x%08x", i, in_be32(LAWBAR_ADDR(i)));
Becky Bruce11a3de42010-06-17 11:37:24 -0500184#endif
Becky Brucee71755f2010-06-17 11:37:23 -0500185 printf(" LAWAR0x%02d: 0x%08x\n", i, lawar);
186 printf("\t(EN: %d TGT: 0x%02x SIZE: ",
187 (lawar & LAW_EN) ? 1 : 0, (lawar >> 20) & 0xff);
188 print_size(lawar_size(lawar), ")\n");
Becky Bruceddcebcb2008-01-23 16:31:05 -0600189 }
190
191 return;
192}
193
Kumar Galaf784e322008-08-26 15:01:28 -0500194/* use up to 2 LAWs for DDR, used the last available LAWs */
195int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
196{
197 u64 start_align, law_sz;
198 int law_sz_enc;
199
200 if (start == 0)
201 start_align = 1ull << (LAW_SIZE_32G + 1);
202 else
203 start_align = 1ull << (ffs64(start) - 1);
204 law_sz = min(start_align, sz);
205 law_sz_enc = __ilog2_u64(law_sz) - 1;
206
207 if (set_last_law(start, law_sz_enc, id) < 0)
208 return -1;
209
Kumar Galae6a67892009-04-04 10:21:02 -0500210 /* recalculate size based on what was actually covered by the law */
211 law_sz = 1ull << __ilog2_u64(law_sz);
212
Kumar Galaf784e322008-08-26 15:01:28 -0500213 /* do we still have anything to map */
214 sz = sz - law_sz;
215 if (sz) {
216 start += law_sz;
217
218 start_align = 1ull << (ffs64(start) - 1);
219 law_sz = min(start_align, sz);
220 law_sz_enc = __ilog2_u64(law_sz) - 1;
221
222 if (set_last_law(start, law_sz_enc, id) < 0)
223 return -1;
224 } else {
225 return 0;
226 }
227
228 /* do we still have anything to map */
229 sz = sz - law_sz;
230 if (sz)
231 return 1;
232
233 return 0;
234}
Mingkai Hu7da53352009-09-11 14:19:10 +0800235#endif
Kumar Galaf784e322008-08-26 15:01:28 -0500236
Kumar Gala83d40df2008-01-16 01:13:58 -0600237void init_laws(void)
238{
239 int i;
Kumar Galaf0600542008-06-11 00:44:10 -0500240
Kumar Gala418ec852009-03-19 02:32:23 -0500241#if FSL_HW_NUM_LAWS < 32
Kumar Galaf0600542008-06-11 00:44:10 -0500242 gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
Kumar Gala418ec852009-03-19 02:32:23 -0500243#elif FSL_HW_NUM_LAWS == 32
244 gd->used_laws = 0;
245#else
246#error FSL_HW_NUM_LAWS can not be greater than 32 w/o code changes
247#endif
Kumar Gala83d40df2008-01-16 01:13:58 -0600248
249 for (i = 0; i < num_law_entries; i++) {
Kumar Galaf0600542008-06-11 00:44:10 -0500250 if (law_table[i].index == -1)
251 set_next_law(law_table[i].addr, law_table[i].size,
252 law_table[i].trgt_id);
253 else
254 set_law(law_table[i].index, law_table[i].addr,
255 law_table[i].size, law_table[i].trgt_id);
Kumar Gala83d40df2008-01-16 01:13:58 -0600256 }
257
258 return ;
259}