]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/misc/fsl_law.c
ppc/85xx: get_law_entry isn't used in CONFIG_NAND_SPL
[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 #ifndef CONFIG_NAND_SPL
83 static 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 }
100 #endif
101 #else
102 void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
103 {
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;
107
108         gd->used_laws |= (1 << idx);
109
110         out_be32(lawar, 0);
111         out_be32(lawbar, addr >> 12);
112         out_be32(lawar, LAW_EN | ((u32)id << 20) | (u32)sz);
113
114         /* Read back so that we sync the writes */
115         in_be32(lawar);
116 }
117
118 void 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
135 #ifndef CONFIG_NAND_SPL
136 static 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
155 #endif
156
157 int 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
169 #ifndef CONFIG_NAND_SPL
170 int 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
189 struct law_entry find_law(phys_addr_t addr)
190 {
191         struct law_entry entry;
192         int i;
193
194         entry.index = -1;
195         entry.addr = 0;
196         entry.size = 0;
197         entry.trgt_id = 0;
198
199         for (i = 0; i < FSL_HW_NUM_LAWS; i++) {
200                 u64 upper;
201
202                 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;
213 }
214
215 void print_laws(void)
216 {
217         volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
218         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
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)
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
249         /* recalculate size based on what was actually covered by the law */
250         law_sz = 1ull << __ilog2_u64(law_sz);
251
252         /* 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 }
274 #endif
275
276 void init_laws(void)
277 {
278         int i;
279
280 #if FSL_HW_NUM_LAWS < 32
281         gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
282 #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
287
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);
292                 else
293                         set_law(law_table[i].index, law_table[i].addr,
294                                 law_table[i].size, law_table[i].trgt_id);
295         }
296
297         return ;
298 }