]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/misc/fsl_law.c
Merge branch 'master' of /home/wd/git/u-boot/custodians
[karo-tx-uboot.git] / drivers / misc / fsl_law.c
1 /*
2  * Copyright 2008-2009 Freescale Semiconductor, Inc.
3  *
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
30 DECLARE_GLOBAL_DATA_PTR;
31
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
46 #else
47 #error FSL_HW_NUM_LAWS not defined for this platform
48 #endif
49
50 #ifdef CONFIG_FSL_CORENET
51 void 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
66 void 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
82 static int get_law_entry(u8 i, struct law_entry *e)
83 {
84         volatile ccsr_local_t *ccm = (void *)(CONFIG_SYS_FSL_CORENET_CCM_ADDR);
85         u32 lawar;
86
87         lawar = in_be32(&ccm->law[i].lawar);
88
89         if (!(lawar & LAW_EN))
90                 return 0;
91
92         e->addr = ((u64)in_be32(&ccm->law[i].lawbarh) << 32) |
93                         in_be32(&ccm->law[i].lawbarl);
94         e->size = lawar & 0x3f;
95         e->trgt_id = (lawar >> 20) & 0xff;
96
97         return 1;
98 }
99 #else
100 void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
101 {
102         volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
103         volatile u32 *lawbar = base + 8 * idx;
104         volatile u32 *lawar = base + 8 * idx + 2;
105
106         gd->used_laws |= (1 << idx);
107
108         out_be32(lawar, 0);
109         out_be32(lawbar, addr >> 12);
110         out_be32(lawar, LAW_EN | ((u32)id << 20) | (u32)sz);
111
112         /* Read back so that we sync the writes */
113         in_be32(lawar);
114 }
115
116 void disable_law(u8 idx)
117 {
118         volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
119         volatile u32 *lawbar = base + 8 * idx;
120         volatile u32 *lawar = base + 8 * idx + 2;
121
122         gd->used_laws &= ~(1 << idx);
123
124         out_be32(lawar, 0);
125         out_be32(lawbar, 0);
126
127         /* Read back so that we sync the writes */
128         in_be32(lawar);
129
130         return;
131 }
132
133 static int get_law_entry(u8 i, struct law_entry *e)
134 {
135         volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
136         volatile u32 *lawbar = base + 8 * i;
137         volatile u32 *lawar = base + 8 * i + 2;
138         u32 temp;
139
140         temp = in_be32(lawar);
141
142         if (!(temp & LAW_EN))
143                 return 0;
144
145         e->addr = (u64)in_be32(lawbar) << 12;
146         e->size = temp & 0x3f;
147         e->trgt_id = (temp >> 20) & 0xff;
148
149         return 1;
150 }
151 #endif
152
153 int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
154 {
155         u32 idx = ffz(gd->used_laws);
156
157         if (idx >= FSL_HW_NUM_LAWS)
158                 return -1;
159
160         set_law(idx, addr, sz, id);
161
162         return idx;
163 }
164
165 #ifndef CONFIG_NAND_SPL
166 int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
167 {
168         u32 idx;
169
170         /* we have no LAWs free */
171         if (gd->used_laws == -1)
172                 return -1;
173
174         /* grab the last free law */
175         idx = __ilog2(~(gd->used_laws));
176
177         if (idx >= FSL_HW_NUM_LAWS)
178                 return -1;
179
180         set_law(idx, addr, sz, id);
181
182         return idx;
183 }
184
185 struct law_entry find_law(phys_addr_t addr)
186 {
187         struct law_entry entry;
188         int i;
189
190         entry.index = -1;
191         entry.addr = 0;
192         entry.size = 0;
193         entry.trgt_id = 0;
194
195         for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
196                 u64 upper;
197
198                 if (!get_law_entry(i, &entry))
199                         continue;
200
201                 upper = entry.addr + (2ull << entry.size);
202                 if ((addr >= entry.addr) && (addr < upper)) {
203                         entry.index = i;
204                         break;
205                 }
206         }
207
208         return entry;
209 }
210
211 void print_laws(void)
212 {
213         volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
214         volatile u32 *lawbar = base;
215         volatile u32 *lawar = base + 2;
216         int i;
217
218         printf("\nLocal Access Window Configuration\n");
219         for(i = 0; i < FSL_HW_NUM_LAWS; i++) {
220                 printf("\tLAWBAR%d : 0x%08x, LAWAR%d : 0x%08x\n",
221                        i, in_be32(lawbar), i, in_be32(lawar));
222                 lawbar += 8;
223                 lawar += 8;
224         }
225
226         return;
227 }
228
229 /* use up to 2 LAWs for DDR, used the last available LAWs */
230 int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
231 {
232         u64 start_align, law_sz;
233         int law_sz_enc;
234
235         if (start == 0)
236                 start_align = 1ull << (LAW_SIZE_32G + 1);
237         else
238                 start_align = 1ull << (ffs64(start) - 1);
239         law_sz = min(start_align, sz);
240         law_sz_enc = __ilog2_u64(law_sz) - 1;
241
242         if (set_last_law(start, law_sz_enc, id) < 0)
243                 return -1;
244
245         /* recalculate size based on what was actually covered by the law */
246         law_sz = 1ull << __ilog2_u64(law_sz);
247
248         /* do we still have anything to map */
249         sz = sz - law_sz;
250         if (sz) {
251                 start += law_sz;
252
253                 start_align = 1ull << (ffs64(start) - 1);
254                 law_sz = min(start_align, sz);
255                 law_sz_enc = __ilog2_u64(law_sz) - 1;
256
257                 if (set_last_law(start, law_sz_enc, id) < 0)
258                         return -1;
259         } else {
260                 return 0;
261         }
262
263         /* do we still have anything to map */
264         sz = sz - law_sz;
265         if (sz)
266                 return 1;
267
268         return 0;
269 }
270 #endif
271
272 void init_laws(void)
273 {
274         int i;
275
276 #if FSL_HW_NUM_LAWS < 32
277         gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
278 #elif FSL_HW_NUM_LAWS == 32
279         gd->used_laws = 0;
280 #else
281 #error FSL_HW_NUM_LAWS can not be greater than 32 w/o code changes
282 #endif
283
284         for (i = 0; i < num_law_entries; i++) {
285                 if (law_table[i].index == -1)
286                         set_next_law(law_table[i].addr, law_table[i].size,
287                                         law_table[i].trgt_id);
288                 else
289                         set_law(law_table[i].index, law_table[i].addr,
290                                 law_table[i].size, law_table[i].trgt_id);
291         }
292
293         return ;
294 }