Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 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 | |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 32 | #define LAWAR_EN 0x80000000 |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 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) || \ |
Haiying Wang | 22b6dbc | 2009-03-27 17:02:44 -0400 | [diff] [blame] | 38 | defined(CONFIG_MPC8568) || defined(CONFIG_MPC8569) || \ |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 39 | defined(CONFIG_MPC8641) || defined(CONFIG_MPC8610) |
| 40 | #define FSL_HW_NUM_LAWS 10 |
Srikanth Srinivasan | 8d949af | 2009-01-21 17:17:33 -0600 | [diff] [blame] | 41 | #elif defined(CONFIG_MPC8536) || defined(CONFIG_MPC8572) || \ |
Poonam Aggrwal | a713ba9 | 2009-08-20 18:57:45 +0530 | [diff] [blame] | 42 | defined(CONFIG_P1011) || defined(CONFIG_P1020) || \ |
| 43 | defined(CONFIG_P2010) || defined(CONFIG_P2020) |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 44 | #define FSL_HW_NUM_LAWS 12 |
| 45 | #else |
| 46 | #error FSL_HW_NUM_LAWS not defined for this platform |
| 47 | #endif |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 48 | |
| 49 | void set_law(u8 idx, phys_addr_t addr, enum law_size sz, enum law_trgt_if id) |
| 50 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 51 | volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08); |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 52 | volatile u32 *lawbar = base + 8 * idx; |
| 53 | volatile u32 *lawar = base + 8 * idx + 2; |
| 54 | |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 55 | gd->used_laws |= (1 << idx); |
| 56 | |
Ed Swarthout | e1f7d22 | 2008-10-09 01:25:55 -0500 | [diff] [blame] | 57 | out_be32(lawar, 0); |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 58 | out_be32(lawbar, addr >> 12); |
| 59 | out_be32(lawar, LAWAR_EN | ((u32)id << 20) | (u32)sz); |
| 60 | |
| 61 | return ; |
| 62 | } |
| 63 | |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 64 | int set_next_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id) |
| 65 | { |
| 66 | u32 idx = ffz(gd->used_laws); |
| 67 | |
| 68 | if (idx >= FSL_HW_NUM_LAWS) |
| 69 | return -1; |
| 70 | |
| 71 | set_law(idx, addr, sz, id); |
| 72 | |
| 73 | return idx; |
| 74 | } |
| 75 | |
Kumar Gala | ba04f70 | 2008-06-10 16:16:02 -0500 | [diff] [blame] | 76 | int set_last_law(phys_addr_t addr, enum law_size sz, enum law_trgt_if id) |
| 77 | { |
| 78 | u32 idx; |
| 79 | |
| 80 | /* we have no LAWs free */ |
| 81 | if (gd->used_laws == -1) |
| 82 | return -1; |
| 83 | |
| 84 | /* grab the last free law */ |
| 85 | idx = __ilog2(~(gd->used_laws)); |
| 86 | |
| 87 | if (idx >= FSL_HW_NUM_LAWS) |
| 88 | return -1; |
| 89 | |
| 90 | set_law(idx, addr, sz, id); |
| 91 | |
| 92 | return idx; |
| 93 | } |
| 94 | |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 95 | void disable_law(u8 idx) |
| 96 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 97 | volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08); |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 98 | volatile u32 *lawbar = base + 8 * idx; |
| 99 | volatile u32 *lawar = base + 8 * idx + 2; |
| 100 | |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 101 | gd->used_laws &= ~(1 << idx); |
| 102 | |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 103 | out_be32(lawar, 0); |
| 104 | out_be32(lawbar, 0); |
| 105 | |
| 106 | return; |
| 107 | } |
| 108 | |
Becky Bruce | ddcebcb | 2008-01-23 16:31:05 -0600 | [diff] [blame] | 109 | void print_laws(void) |
| 110 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 111 | volatile u32 *base = (volatile u32 *)(CONFIG_SYS_IMMR + 0xc08); |
Becky Bruce | ddcebcb | 2008-01-23 16:31:05 -0600 | [diff] [blame] | 112 | volatile u32 *lawbar = base; |
| 113 | volatile u32 *lawar = base + 2; |
| 114 | int i; |
| 115 | |
| 116 | printf("\nLocal Access Window Configuration\n"); |
| 117 | for(i = 0; i < FSL_HW_NUM_LAWS; i++) { |
| 118 | printf("\tLAWBAR%d : 0x%08x, LAWAR%d : 0x%08x\n", |
| 119 | i, in_be32(lawbar), i, in_be32(lawar)); |
| 120 | lawbar += 8; |
| 121 | lawar += 8; |
| 122 | } |
| 123 | |
| 124 | return; |
| 125 | } |
| 126 | |
Kumar Gala | f784e32 | 2008-08-26 15:01:28 -0500 | [diff] [blame] | 127 | /* use up to 2 LAWs for DDR, used the last available LAWs */ |
| 128 | int set_ddr_laws(u64 start, u64 sz, enum law_trgt_if id) |
| 129 | { |
| 130 | u64 start_align, law_sz; |
| 131 | int law_sz_enc; |
| 132 | |
| 133 | if (start == 0) |
| 134 | start_align = 1ull << (LAW_SIZE_32G + 1); |
| 135 | else |
| 136 | start_align = 1ull << (ffs64(start) - 1); |
| 137 | law_sz = min(start_align, sz); |
| 138 | law_sz_enc = __ilog2_u64(law_sz) - 1; |
| 139 | |
| 140 | if (set_last_law(start, law_sz_enc, id) < 0) |
| 141 | return -1; |
| 142 | |
Kumar Gala | e6a6789 | 2009-04-04 10:21:02 -0500 | [diff] [blame] | 143 | /* recalculate size based on what was actually covered by the law */ |
| 144 | law_sz = 1ull << __ilog2_u64(law_sz); |
| 145 | |
Kumar Gala | f784e32 | 2008-08-26 15:01:28 -0500 | [diff] [blame] | 146 | /* do we still have anything to map */ |
| 147 | sz = sz - law_sz; |
| 148 | if (sz) { |
| 149 | start += law_sz; |
| 150 | |
| 151 | start_align = 1ull << (ffs64(start) - 1); |
| 152 | law_sz = min(start_align, sz); |
| 153 | law_sz_enc = __ilog2_u64(law_sz) - 1; |
| 154 | |
| 155 | if (set_last_law(start, law_sz_enc, id) < 0) |
| 156 | return -1; |
| 157 | } else { |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /* do we still have anything to map */ |
| 162 | sz = sz - law_sz; |
| 163 | if (sz) |
| 164 | return 1; |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 169 | void init_laws(void) |
| 170 | { |
| 171 | int i; |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 172 | |
| 173 | gd->used_laws = ~((1 << FSL_HW_NUM_LAWS) - 1); |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 174 | |
| 175 | for (i = 0; i < num_law_entries; i++) { |
Kumar Gala | f060054 | 2008-06-11 00:44:10 -0500 | [diff] [blame] | 176 | if (law_table[i].index == -1) |
| 177 | set_next_law(law_table[i].addr, law_table[i].size, |
| 178 | law_table[i].trgt_id); |
| 179 | else |
| 180 | set_law(law_table[i].index, law_table[i].addr, |
| 181 | law_table[i].size, law_table[i].trgt_id); |
Kumar Gala | 83d40df | 2008-01-16 01:13:58 -0600 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | return ; |
| 185 | } |