blob: f7d454dce457d75e4677fd0710c6b064cbabd44f [file] [log] [blame]
Kumar Gala83d40df2008-01-16 01:13:58 -06001/*
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
Kumar Galaf0600542008-06-11 00:44:10 -050030DECLARE_GLOBAL_DATA_PTR;
31
Kumar Gala83d40df2008-01-16 01:13:58 -060032#define LAWAR_EN 0x80000000
Kumar Galaf0600542008-06-11 00:44:10 -050033/* 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) || \
Haiying Wang22b6dbc2009-03-27 17:02:44 -040038 defined(CONFIG_MPC8568) || defined(CONFIG_MPC8569) || \
Kumar Galaf0600542008-06-11 00:44:10 -050039 defined(CONFIG_MPC8641) || defined(CONFIG_MPC8610)
40#define FSL_HW_NUM_LAWS 10
Srikanth Srinivasan8d949af2009-01-21 17:17:33 -060041#elif defined(CONFIG_MPC8536) || defined(CONFIG_MPC8572) || \
42 defined(CONFIG_P2020)
Kumar Galaf0600542008-06-11 00:44:10 -050043#define FSL_HW_NUM_LAWS 12
44#else
45#error FSL_HW_NUM_LAWS not defined for this platform
46#endif
Kumar Gala83d40df2008-01-16 01:13:58 -060047
48void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
49{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020050 volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
Kumar Gala83d40df2008-01-16 01:13:58 -060051 volatile u32 *lawbar = base + 8 * idx;
52 volatile u32 *lawar = base + 8 * idx + 2;
53
Kumar Galaf0600542008-06-11 00:44:10 -050054 gd->used_laws |= (1 << idx);
55
Ed Swarthoute1f7d222008-10-09 01:25:55 -050056 out_be32(lawar, 0);
Kumar Gala83d40df2008-01-16 01:13:58 -060057 out_be32(lawbar, addr >> 12);
58 out_be32(lawar, LAWAR_EN | ((u32)id << 20) | (u32)sz);
59
60 return ;
61}
62
Kumar Galaf0600542008-06-11 00:44:10 -050063int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
64{
65 u32 idx = ffz(gd->used_laws);
66
67 if (idx >= FSL_HW_NUM_LAWS)
68 return -1;
69
70 set_law(idx, addr, sz, id);
71
72 return idx;
73}
74
Kumar Galaba04f702008-06-10 16:16:02 -050075int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
76{
77 u32 idx;
78
79 /* we have no LAWs free */
80 if (gd->used_laws == -1)
81 return -1;
82
83 /* grab the last free law */
84 idx = __ilog2(~(gd->used_laws));
85
86 if (idx >= FSL_HW_NUM_LAWS)
87 return -1;
88
89 set_law(idx, addr, sz, id);
90
91 return idx;
92}
93
Kumar Gala83d40df2008-01-16 01:13:58 -060094void disable_law(u8 idx)
95{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020096 volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
Kumar Gala83d40df2008-01-16 01:13:58 -060097 volatile u32 *lawbar = base + 8 * idx;
98 volatile u32 *lawar = base + 8 * idx + 2;
99
Kumar Galaf0600542008-06-11 00:44:10 -0500100 gd->used_laws &= ~(1 << idx);
101
Kumar Gala83d40df2008-01-16 01:13:58 -0600102 out_be32(lawar, 0);
103 out_be32(lawbar, 0);
104
105 return;
106}
107
Becky Bruceddcebcb2008-01-23 16:31:05 -0600108void print_laws(void)
109{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200110 volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
Becky Bruceddcebcb2008-01-23 16:31:05 -0600111 volatile u32 *lawbar = base;
112 volatile u32 *lawar = base + 2;
113 int i;
114
115 printf("\nLocal Access Window Configuration\n");
116 for(i = 0; i < FSL_HW_NUM_LAWS; i++) {
117 printf("\tLAWBAR%d : 0x%08x, LAWAR%d : 0x%08x\n",
118 i, in_be32(lawbar), i, in_be32(lawar));
119 lawbar += 8;
120 lawar += 8;
121 }
122
123 return;
124}
125
Kumar Galaf784e322008-08-26 15:01:28 -0500126/* use up to 2 LAWs for DDR, used the last available LAWs */
127int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
128{
129 u64 start_align, law_sz;
130 int law_sz_enc;
131
132 if (start == 0)
133 start_align = 1ull << (LAW_SIZE_32G + 1);
134 else
135 start_align = 1ull << (ffs64(start) - 1);
136 law_sz = min(start_align, sz);
137 law_sz_enc = __ilog2_u64(law_sz) - 1;
138
139 if (set_last_law(start, law_sz_enc, id) < 0)
140 return -1;
141
Kumar Galae6a67892009-04-04 10:21:02 -0500142 /* recalculate size based on what was actually covered by the law */
143 law_sz = 1ull << __ilog2_u64(law_sz);
144
Kumar Galaf784e322008-08-26 15:01:28 -0500145 /* do we still have anything to map */
146 sz = sz - law_sz;
147 if (sz) {
148 start += law_sz;
149
150 start_align = 1ull << (ffs64(start) - 1);
151 law_sz = min(start_align, sz);
152 law_sz_enc = __ilog2_u64(law_sz) - 1;
153
154 if (set_last_law(start, law_sz_enc, id) < 0)
155 return -1;
156 } else {
157 return 0;
158 }
159
160 /* do we still have anything to map */
161 sz = sz - law_sz;
162 if (sz)
163 return 1;
164
165 return 0;
166}
167
Kumar Gala83d40df2008-01-16 01:13:58 -0600168void init_laws(void)
169{
170 int i;
Kumar Galaf0600542008-06-11 00:44:10 -0500171
172 gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
Kumar Gala83d40df2008-01-16 01:13:58 -0600173
174 for (i = 0; i < num_law_entries; i++) {
Kumar Galaf0600542008-06-11 00:44:10 -0500175 if (law_table[i].index == -1)
176 set_next_law(law_table[i].addr, law_table[i].size,
177 law_table[i].trgt_id);
178 else
179 set_law(law_table[i].index, law_table[i].addr,
180 law_table[i].size, law_table[i].trgt_id);
Kumar Gala83d40df2008-01-16 01:13:58 -0600181 }
182
183 return ;
184}