Prafulla Wadaskar | 4efb77d | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2009 |
| 3 | * Marvell Semiconductor <www.marvell.com> |
| 4 | * Written-by: Prafulla Wadaskar <prafulla@marvell.com> |
| 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 22 | * MA 02110-1301 USA |
| 23 | */ |
| 24 | |
| 25 | #include <config.h> |
Prafulla Wadaskar | beeb258 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 26 | #include <common.h> |
Lei Wen | a7efd71 | 2011-10-18 20:11:42 +0530 | [diff] [blame] | 27 | #include <asm/io.h> |
| 28 | #include <asm/arch/cpu.h> |
Prafulla Wadaskar | 4efb77d | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 29 | #include <asm/arch/kirkwood.h> |
| 30 | |
Prafulla Wadaskar | beeb258 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 31 | DECLARE_GLOBAL_DATA_PTR; |
| 32 | |
Holger Brunck | cf37c5d | 2012-07-20 02:34:24 +0000 | [diff] [blame] | 33 | struct kw_sdram_bank { |
| 34 | u32 win_bar; |
| 35 | u32 win_sz; |
| 36 | }; |
| 37 | |
| 38 | struct kw_sdram_addr_dec { |
| 39 | struct kw_sdram_bank sdram_bank[4]; |
| 40 | }; |
| 41 | |
Gerlando Falauto | 4551516 | 2012-07-20 02:34:25 +0000 | [diff] [blame] | 42 | #define KW_REG_CPUCS_WIN_ENABLE (1 << 0) |
| 43 | #define KW_REG_CPUCS_WIN_WR_PROTECT (1 << 1) |
| 44 | #define KW_REG_CPUCS_WIN_WIN0_CS(x) (((x) & 0x3) << 2) |
| 45 | #define KW_REG_CPUCS_WIN_SIZE(x) (((x) & 0xff) << 24) |
| 46 | |
Prafulla Wadaskar | 4efb77d | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 47 | /* |
| 48 | * kw_sdram_bar - reads SDRAM Base Address Register |
| 49 | */ |
| 50 | u32 kw_sdram_bar(enum memory_bank bank) |
| 51 | { |
Holger Brunck | cf37c5d | 2012-07-20 02:34:24 +0000 | [diff] [blame] | 52 | struct kw_sdram_addr_dec *base = |
| 53 | (struct kw_sdram_addr_dec *)KW_REGISTER(0x1500); |
Prafulla Wadaskar | 4efb77d | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 54 | u32 result = 0; |
Holger Brunck | cf37c5d | 2012-07-20 02:34:24 +0000 | [diff] [blame] | 55 | u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz); |
Prafulla Wadaskar | 4efb77d | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 56 | |
| 57 | if ((!enable) || (bank > BANK3)) |
| 58 | return 0; |
| 59 | |
Holger Brunck | cf37c5d | 2012-07-20 02:34:24 +0000 | [diff] [blame] | 60 | result = readl(&base->sdram_bank[bank].win_bar); |
Prafulla Wadaskar | 4efb77d | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 61 | return result; |
| 62 | } |
| 63 | |
| 64 | /* |
Gerlando Falauto | 4551516 | 2012-07-20 02:34:25 +0000 | [diff] [blame] | 65 | * kw_sdram_bs_set - writes SDRAM Bank size |
| 66 | */ |
| 67 | static void kw_sdram_bs_set(enum memory_bank bank, u32 size) |
| 68 | { |
| 69 | struct kw_sdram_addr_dec *base = |
| 70 | (struct kw_sdram_addr_dec *)KW_REGISTER(0x1500); |
| 71 | /* Read current register value */ |
| 72 | u32 reg = readl(&base->sdram_bank[bank].win_sz); |
| 73 | |
| 74 | /* Clear window size */ |
| 75 | reg &= ~KW_REG_CPUCS_WIN_SIZE(0xFF); |
| 76 | |
| 77 | /* Set new window size */ |
| 78 | reg |= KW_REG_CPUCS_WIN_SIZE((size - 1) >> 24); |
| 79 | |
| 80 | writel(reg, &base->sdram_bank[bank].win_sz); |
| 81 | } |
| 82 | |
| 83 | /* |
Prafulla Wadaskar | 4efb77d | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 84 | * kw_sdram_bs - reads SDRAM Bank size |
| 85 | */ |
| 86 | u32 kw_sdram_bs(enum memory_bank bank) |
| 87 | { |
Holger Brunck | cf37c5d | 2012-07-20 02:34:24 +0000 | [diff] [blame] | 88 | struct kw_sdram_addr_dec *base = |
| 89 | (struct kw_sdram_addr_dec *)KW_REGISTER(0x1500); |
Prafulla Wadaskar | 4efb77d | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 90 | u32 result = 0; |
Holger Brunck | cf37c5d | 2012-07-20 02:34:24 +0000 | [diff] [blame] | 91 | u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz); |
Prafulla Wadaskar | 4efb77d | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 92 | |
| 93 | if ((!enable) || (bank > BANK3)) |
| 94 | return 0; |
Holger Brunck | cf37c5d | 2012-07-20 02:34:24 +0000 | [diff] [blame] | 95 | result = 0xff000000 & readl(&base->sdram_bank[bank].win_sz); |
Prafulla Wadaskar | 4efb77d | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 96 | result += 0x01000000; |
| 97 | return result; |
| 98 | } |
Prafulla Wadaskar | beeb258 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 99 | |
Gerlando Falauto | b3168f4 | 2012-07-25 06:23:48 +0000 | [diff] [blame] | 100 | void kw_sdram_size_adjust(enum memory_bank bank) |
| 101 | { |
| 102 | u32 size; |
| 103 | |
| 104 | /* probe currently equipped RAM size */ |
| 105 | size = get_ram_size((void *)kw_sdram_bar(bank), kw_sdram_bs(bank)); |
| 106 | |
| 107 | /* adjust SDRAM window size accordingly */ |
| 108 | kw_sdram_bs_set(bank, size); |
| 109 | } |
| 110 | |
Prafulla Wadaskar | beeb258 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 111 | #ifndef CONFIG_SYS_BOARD_DRAM_INIT |
| 112 | int dram_init(void) |
| 113 | { |
| 114 | int i; |
| 115 | |
| 116 | gd->ram_size = 0; |
| 117 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 118 | gd->bd->bi_dram[i].start = kw_sdram_bar(i); |
| 119 | gd->bd->bi_dram[i].size = kw_sdram_bs(i); |
| 120 | /* |
| 121 | * It is assumed that all memory banks are consecutive |
| 122 | * and without gaps. |
| 123 | * If the gap is found, ram_size will be reported for |
| 124 | * consecutive memory only |
| 125 | */ |
| 126 | if (gd->bd->bi_dram[i].start != gd->ram_size) |
| 127 | break; |
| 128 | |
| 129 | gd->ram_size += gd->bd->bi_dram[i].size; |
| 130 | |
| 131 | } |
Tanmay Upadhyay | 28e5710 | 2010-10-28 20:06:22 +0530 | [diff] [blame] | 132 | |
| 133 | for (; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 134 | /* If above loop terminated prematurely, we need to set |
| 135 | * remaining banks' start address & size as 0. Otherwise other |
| 136 | * u-boot functions and Linux kernel gets wrong values which |
| 137 | * could result in crash */ |
| 138 | gd->bd->bi_dram[i].start = 0; |
| 139 | gd->bd->bi_dram[i].size = 0; |
| 140 | } |
| 141 | |
Prafulla Wadaskar | beeb258 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * If this function is not defined here, |
| 147 | * board.c alters dram bank zero configuration defined above. |
| 148 | */ |
| 149 | void dram_init_banksize(void) |
| 150 | { |
| 151 | dram_init(); |
| 152 | } |
| 153 | #endif /* CONFIG_SYS_BOARD_DRAM_INIT */ |