blob: aa877c65fd389fc68853e39150a8907d08b8fecd [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) || \
Poonam Aggrwala713ba92009-08-20 18:57:45 +053042 defined(CONFIG_P1011) || defined(CONFIG_P1020) || \
43 defined(CONFIG_P2010) || defined(CONFIG_P2020)
Kumar Galaf0600542008-06-11 00:44:10 -050044#define FSL_HW_NUM_LAWS 12
45#else
46#error FSL_HW_NUM_LAWS not defined for this platform
47#endif
Kumar Gala83d40df2008-01-16 01:13:58 -060048
49void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
50{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020051 volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
Kumar Gala83d40df2008-01-16 01:13:58 -060052 volatile u32 *lawbar = base + 8 * idx;
53 volatile u32 *lawar = base + 8 * idx + 2;
54
Kumar Galaf0600542008-06-11 00:44:10 -050055 gd->used_laws |= (1 << idx);
56
Ed Swarthoute1f7d222008-10-09 01:25:55 -050057 out_be32(lawar, 0);
Kumar Gala83d40df2008-01-16 01:13:58 -060058 out_be32(lawbar, addr >> 12);
59 out_be32(lawar, LAWAR_EN | ((u32)id << 20) | (u32)sz);
60
Timur Tabi74c5dfd2009-09-04 17:05:24 -050061 /* Read back so that we sync the writes */
62 in_be32(lawar);
Kumar Gala83d40df2008-01-16 01:13:58 -060063}
64
Kumar Galaf0600542008-06-11 00:44:10 -050065int 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
Mingkai Hu7da53352009-09-11 14:19:10 +080077#ifndef CONFIG_NAND_SPL
Kumar Galaba04f702008-06-10 16:16:02 -050078int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id)
79{
80 u32 idx;
81
82 /* we have no LAWs free */
83 if (gd->used_laws == -1)
84 return -1;
85
86 /* grab the last free law */
87 idx = __ilog2(~(gd->used_laws));
88
89 if (idx >= FSL_HW_NUM_LAWS)
90 return -1;
91
92 set_law(idx, addr, sz, id);
93
94 return idx;
95}
96
Kumar Gala83d40df2008-01-16 01:13:58 -060097void disable_law(u8 idx)
98{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020099 volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
Kumar Gala83d40df2008-01-16 01:13:58 -0600100 volatile u32 *lawbar = base + 8 * idx;
101 volatile u32 *lawar = base + 8 * idx + 2;
102
Kumar Galaf0600542008-06-11 00:44:10 -0500103 gd->used_laws &= ~(1 << idx);
104
Kumar Gala83d40df2008-01-16 01:13:58 -0600105 out_be32(lawar, 0);
106 out_be32(lawbar, 0);
107
108 return;
109}
110
Becky Bruceddcebcb2008-01-23 16:31:05 -0600111void print_laws(void)
112{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200113 volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08);
Becky Bruceddcebcb2008-01-23 16:31:05 -0600114 volatile u32 *lawbar = base;
115 volatile u32 *lawar = base + 2;
116 int i;
117
118 printf("\nLocal Access Window Configuration\n");
119 for(i = 0; i < FSL_HW_NUM_LAWS; i++) {
120 printf("\tLAWBAR%d : 0x%08x, LAWAR%d : 0x%08x\n",
121 i, in_be32(lawbar), i, in_be32(lawar));
122 lawbar += 8;
123 lawar += 8;
124 }
125
126 return;
127}
128
Kumar Galaf784e322008-08-26 15:01:28 -0500129/* use up to 2 LAWs for DDR, used the last available LAWs */
130int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id)
131{
132 u64 start_align, law_sz;
133 int law_sz_enc;
134
135 if (start == 0)
136 start_align = 1ull << (LAW_SIZE_32G + 1);
137 else
138 start_align = 1ull << (ffs64(start) - 1);
139 law_sz = min(start_align, sz);
140 law_sz_enc = __ilog2_u64(law_sz) - 1;
141
142 if (set_last_law(start, law_sz_enc, id) < 0)
143 return -1;
144
Kumar Galae6a67892009-04-04 10:21:02 -0500145 /* recalculate size based on what was actually covered by the law */
146 law_sz = 1ull << __ilog2_u64(law_sz);
147
Kumar Galaf784e322008-08-26 15:01:28 -0500148 /* do we still have anything to map */
149 sz = sz - law_sz;
150 if (sz) {
151 start += law_sz;
152
153 start_align = 1ull << (ffs64(start) - 1);
154 law_sz = min(start_align, sz);
155 law_sz_enc = __ilog2_u64(law_sz) - 1;
156
157 if (set_last_law(start, law_sz_enc, id) < 0)
158 return -1;
159 } else {
160 return 0;
161 }
162
163 /* do we still have anything to map */
164 sz = sz - law_sz;
165 if (sz)
166 return 1;
167
168 return 0;
169}
Mingkai Hu7da53352009-09-11 14:19:10 +0800170#endif
Kumar Galaf784e322008-08-26 15:01:28 -0500171
Kumar Gala83d40df2008-01-16 01:13:58 -0600172void init_laws(void)
173{
174 int i;
Kumar Galaf0600542008-06-11 00:44:10 -0500175
176 gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1);
Kumar Gala83d40df2008-01-16 01:13:58 -0600177
178 for (i = 0; i < num_law_entries; i++) {
Kumar Galaf0600542008-06-11 00:44:10 -0500179 if (law_table[i].index == -1)
180 set_next_law(law_table[i].addr, law_table[i].size,
181 law_table[i].trgt_id);
182 else
183 set_law(law_table[i].index, law_table[i].addr,
184 law_table[i].size, law_table[i].trgt_id);
Kumar Gala83d40df2008-01-16 01:13:58 -0600185 }
186
187 return ;
188}