]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/misc/fsl_law.c
fsl: add register read-back to set_law()
[karo-tx-uboot.git] / drivers / misc / fsl_law.c
1 /*
2  * Copyright 2008 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 #define LAWAR_EN        0x80000000
33 /* number of LAWs in the hw implementation */
34 #if defined(CONFIG_MPC8540) || defined(CONFIG_MPC8541) || \
35     defined(CONFIG_MPC8560) || defined(CONFIG_MPC8555)
36 #define FSL_HW_NUM_LAWS 8
37 #elif defined(CONFIG_MPC8548) || defined(CONFIG_MPC8544) || \
38       defined(CONFIG_MPC8568) || defined(CONFIG_MPC8569) || \
39       defined(CONFIG_MPC8641) || defined(CONFIG_MPC8610)
40 #define FSL_HW_NUM_LAWS 10
41 #elif defined(CONFIG_MPC8536) || defined(CONFIG_MPC8572) || \
42       defined(CONFIG_P1011) || defined(CONFIG_P1020) || \
43       defined(CONFIG_P2010) || defined(CONFIG_P2020)
44 #define FSL_HW_NUM_LAWS 12
45 #else
46 #error FSL_HW_NUM_LAWS not defined for this platform
47 #endif
48
49 void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
50 {
51         volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
52         volatile u32 *lawbar = base + 8 * idx;
53         volatile u32 *lawar = base + 8 * idx + 2;
54
55         gd->used_laws |= (1 << idx);
56
57         out_be32(lawar, 0);
58         out_be32(lawbar, addr >> 12);
59         out_be32(lawar, LAWAR_EN | ((u32)id << 20) | (u32)sz);
60
61         /* Read back so that we sync the writes */
62         in_be32(lawar);
63 }
64
65 int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
66 {
67         u32 idx = ffz(gd->used_laws);
68
69         if (idx >= FSL_HW_NUM_LAWS)
70                 return -1;
71
72         set_law(idx, addr, sz, id);
73
74         return idx;
75 }
76
77 int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
78 {
79         u32 idx;
80
81         /* we have no LAWs free */
82         if (gd->used_laws == -1)
83                 return -1;
84
85         /* grab the last free law */
86         idx = __ilog2(~(gd->used_laws));
87
88         if (idx >= FSL_HW_NUM_LAWS)
89                 return -1;
90
91         set_law(idx, addr, sz, id);
92
93         return idx;
94 }
95
96 void disable_law(u8 idx)
97 {
98         volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
99         volatile u32 *lawbar = base + 8 * idx;
100         volatile u32 *lawar = base + 8 * idx + 2;
101
102         gd->used_laws &= ~(1 << idx);
103
104         out_be32(lawar, 0);
105         out_be32(lawbar, 0);
106
107         return;
108 }
109
110 void print_laws(void)
111 {
112         volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
113         volatile u32 *lawbar = base;
114         volatile u32 *lawar = base + 2;
115         int i;
116
117         printf("\nLocal Access Window Configuration\n");
118         for(i = 0; i < FSL_HW_NUM_LAWS; i++) {
119                 printf("\tLAWBAR%d : 0x%08x, LAWAR%d : 0x%08x\n",
120                        i, in_be32(lawbar), i, in_be32(lawar));
121                 lawbar += 8;
122                 lawar += 8;
123         }
124
125         return;
126 }
127
128 /* use up to 2 LAWs for DDR, used the last available LAWs */
129 int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
130 {
131         u64 start_align, law_sz;
132         int law_sz_enc;
133
134         if (start == 0)
135                 start_align = 1ull << (LAW_SIZE_32G + 1);
136         else
137                 start_align = 1ull << (ffs64(start) - 1);
138         law_sz = min(start_align, sz);
139         law_sz_enc = __ilog2_u64(law_sz) - 1;
140
141         if (set_last_law(start, law_sz_enc, id) < 0)
142                 return -1;
143
144         /* recalculate size based on what was actually covered by the law */
145         law_sz = 1ull << __ilog2_u64(law_sz);
146
147         /* do we still have anything to map */
148         sz = sz - law_sz;
149         if (sz) {
150                 start += law_sz;
151
152                 start_align = 1ull << (ffs64(start) - 1);
153                 law_sz = min(start_align, sz);
154                 law_sz_enc = __ilog2_u64(law_sz) - 1;
155
156                 if (set_last_law(start, law_sz_enc, id) < 0)
157                         return -1;
158         } else {
159                 return 0;
160         }
161
162         /* do we still have anything to map */
163         sz = sz - law_sz;
164         if (sz)
165                 return 1;
166
167         return 0;
168 }
169
170 void init_laws(void)
171 {
172         int i;
173
174         gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
175
176         for (i = 0; i < num_law_entries; i++) {
177                 if (law_table[i].index == -1)
178                         set_next_law(law_table[i].addr, law_table[i].size,
179                                         law_table[i].trgt_id);
180                 else
181                         set_law(law_table[i].index, law_table[i].addr,
182                                 law_table[i].size, law_table[i].trgt_id);
183         }
184
185         return ;
186 }