Prafulla Wadaskar | a09bbe5 | 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 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <config.h> |
Prafulla Wadaskar | f8e4262 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 10 | #include <common.h> |
Lei Wen | 298ae91 | 2011-10-18 20:11:42 +0530 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <asm/arch/cpu.h> |
Stefan Roese | c243784 | 2014-10-22 12:13:06 +0200 | [diff] [blame] | 13 | #include <asm/arch/soc.h> |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 14 | |
Stefan Roese | 7a96ee9 | 2015-08-03 13:15:31 +0200 | [diff] [blame] | 15 | #ifdef CONFIG_SYS_MVEBU_DDR_A38X |
| 16 | #include "../../../drivers/ddr/marvell/a38x/ddr3_init.h" |
| 17 | #endif |
| 18 | #ifdef CONFIG_SYS_MVEBU_DDR_AXP |
| 19 | #include "../../../drivers/ddr/marvell/axp/ddr3_init.h" |
| 20 | #endif |
| 21 | |
Prafulla Wadaskar | f8e4262 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 24 | struct sdram_bank { |
Holger Brunck | 67393fc | 2012-07-20 02:34:24 +0000 | [diff] [blame] | 25 | u32 win_bar; |
| 26 | u32 win_sz; |
| 27 | }; |
| 28 | |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 29 | struct sdram_addr_dec { |
| 30 | struct sdram_bank sdram_bank[4]; |
Holger Brunck | 67393fc | 2012-07-20 02:34:24 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 33 | #define REG_CPUCS_WIN_ENABLE (1 << 0) |
| 34 | #define REG_CPUCS_WIN_WR_PROTECT (1 << 1) |
| 35 | #define REG_CPUCS_WIN_WIN0_CS(x) (((x) & 0x3) << 2) |
| 36 | #define REG_CPUCS_WIN_SIZE(x) (((x) & 0xff) << 24) |
Gerlando Falauto | ac935e2 | 2012-07-20 02:34:25 +0000 | [diff] [blame] | 37 | |
Stefan Roese | 420abea | 2015-08-10 15:11:27 +0200 | [diff] [blame^] | 38 | #define SDRAM_SIZE_MAX 0xc0000000 |
| 39 | |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 40 | /* |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 41 | * mvebu_sdram_bar - reads SDRAM Base Address Register |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 42 | */ |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 43 | u32 mvebu_sdram_bar(enum memory_bank bank) |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 44 | { |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 45 | struct sdram_addr_dec *base = |
| 46 | (struct sdram_addr_dec *)MVEBU_SDRAM_BASE; |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 47 | u32 result = 0; |
Holger Brunck | 67393fc | 2012-07-20 02:34:24 +0000 | [diff] [blame] | 48 | u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz); |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 49 | |
| 50 | if ((!enable) || (bank > BANK3)) |
| 51 | return 0; |
| 52 | |
Holger Brunck | 67393fc | 2012-07-20 02:34:24 +0000 | [diff] [blame] | 53 | result = readl(&base->sdram_bank[bank].win_bar); |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 54 | return result; |
| 55 | } |
| 56 | |
| 57 | /* |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 58 | * mvebu_sdram_bs_set - writes SDRAM Bank size |
Gerlando Falauto | ac935e2 | 2012-07-20 02:34:25 +0000 | [diff] [blame] | 59 | */ |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 60 | static void mvebu_sdram_bs_set(enum memory_bank bank, u32 size) |
Gerlando Falauto | ac935e2 | 2012-07-20 02:34:25 +0000 | [diff] [blame] | 61 | { |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 62 | struct sdram_addr_dec *base = |
| 63 | (struct sdram_addr_dec *)MVEBU_SDRAM_BASE; |
Gerlando Falauto | ac935e2 | 2012-07-20 02:34:25 +0000 | [diff] [blame] | 64 | /* Read current register value */ |
| 65 | u32 reg = readl(&base->sdram_bank[bank].win_sz); |
| 66 | |
| 67 | /* Clear window size */ |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 68 | reg &= ~REG_CPUCS_WIN_SIZE(0xFF); |
Gerlando Falauto | ac935e2 | 2012-07-20 02:34:25 +0000 | [diff] [blame] | 69 | |
| 70 | /* Set new window size */ |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 71 | reg |= REG_CPUCS_WIN_SIZE((size - 1) >> 24); |
Gerlando Falauto | ac935e2 | 2012-07-20 02:34:25 +0000 | [diff] [blame] | 72 | |
| 73 | writel(reg, &base->sdram_bank[bank].win_sz); |
| 74 | } |
| 75 | |
| 76 | /* |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 77 | * mvebu_sdram_bs - reads SDRAM Bank size |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 78 | */ |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 79 | u32 mvebu_sdram_bs(enum memory_bank bank) |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 80 | { |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 81 | struct sdram_addr_dec *base = |
| 82 | (struct sdram_addr_dec *)MVEBU_SDRAM_BASE; |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 83 | u32 result = 0; |
Holger Brunck | 67393fc | 2012-07-20 02:34:24 +0000 | [diff] [blame] | 84 | u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz); |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 85 | |
| 86 | if ((!enable) || (bank > BANK3)) |
| 87 | return 0; |
Holger Brunck | 67393fc | 2012-07-20 02:34:24 +0000 | [diff] [blame] | 88 | result = 0xff000000 & readl(&base->sdram_bank[bank].win_sz); |
Prafulla Wadaskar | a09bbe5 | 2009-06-20 11:01:53 +0200 | [diff] [blame] | 89 | result += 0x01000000; |
| 90 | return result; |
| 91 | } |
Prafulla Wadaskar | f8e4262 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 92 | |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 93 | void mvebu_sdram_size_adjust(enum memory_bank bank) |
Gerlando Falauto | ea32b7e | 2012-07-25 06:23:48 +0000 | [diff] [blame] | 94 | { |
| 95 | u32 size; |
| 96 | |
| 97 | /* probe currently equipped RAM size */ |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 98 | size = get_ram_size((void *)mvebu_sdram_bar(bank), |
| 99 | mvebu_sdram_bs(bank)); |
Gerlando Falauto | ea32b7e | 2012-07-25 06:23:48 +0000 | [diff] [blame] | 100 | |
| 101 | /* adjust SDRAM window size accordingly */ |
Stefan Roese | 0b74175 | 2014-10-22 12:13:13 +0200 | [diff] [blame] | 102 | mvebu_sdram_bs_set(bank, size); |
Gerlando Falauto | ea32b7e | 2012-07-25 06:23:48 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Prafulla Wadaskar | f8e4262 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 105 | int dram_init(void) |
| 106 | { |
Stefan Roese | 420abea | 2015-08-10 15:11:27 +0200 | [diff] [blame^] | 107 | u64 size = 0; |
Prafulla Wadaskar | f8e4262 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 108 | int i; |
| 109 | |
Prafulla Wadaskar | f8e4262 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 110 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
Prafulla Wadaskar | f8e4262 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 111 | /* |
| 112 | * It is assumed that all memory banks are consecutive |
| 113 | * and without gaps. |
| 114 | * If the gap is found, ram_size will be reported for |
| 115 | * consecutive memory only |
| 116 | */ |
Stefan Roese | 420abea | 2015-08-10 15:11:27 +0200 | [diff] [blame^] | 117 | if (mvebu_sdram_bar(i) != size) |
Prafulla Wadaskar | f8e4262 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 118 | break; |
| 119 | |
Stefan Roese | 2cc5fad | 2014-10-22 12:13:05 +0200 | [diff] [blame] | 120 | /* |
| 121 | * Don't report more than 3GiB of SDRAM, otherwise there is no |
| 122 | * address space left for the internal registers etc. |
| 123 | */ |
Stefan Roese | 420abea | 2015-08-10 15:11:27 +0200 | [diff] [blame^] | 124 | size += mvebu_sdram_bs(i); |
| 125 | if (size > SDRAM_SIZE_MAX) |
| 126 | size = SDRAM_SIZE_MAX; |
Prafulla Wadaskar | f8e4262 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 127 | } |
Tanmay Upadhyay | 9a5ff78 | 2010-10-28 20:06:22 +0530 | [diff] [blame] | 128 | |
| 129 | for (; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 130 | /* If above loop terminated prematurely, we need to set |
| 131 | * remaining banks' start address & size as 0. Otherwise other |
| 132 | * u-boot functions and Linux kernel gets wrong values which |
| 133 | * could result in crash */ |
| 134 | gd->bd->bi_dram[i].start = 0; |
| 135 | gd->bd->bi_dram[i].size = 0; |
| 136 | } |
| 137 | |
Stefan Roese | 420abea | 2015-08-10 15:11:27 +0200 | [diff] [blame^] | 138 | gd->ram_size = size; |
| 139 | |
Prafulla Wadaskar | f8e4262 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * If this function is not defined here, |
| 145 | * board.c alters dram bank zero configuration defined above. |
| 146 | */ |
| 147 | void dram_init_banksize(void) |
| 148 | { |
Stefan Roese | 420abea | 2015-08-10 15:11:27 +0200 | [diff] [blame^] | 149 | u64 size = 0; |
| 150 | int i; |
| 151 | |
| 152 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 153 | gd->bd->bi_dram[i].start = mvebu_sdram_bar(i); |
| 154 | gd->bd->bi_dram[i].size = mvebu_sdram_bs(i); |
| 155 | |
| 156 | /* Clip the banksize to 1GiB if it exceeds the max size */ |
| 157 | size += gd->bd->bi_dram[i].size; |
| 158 | if (size > SDRAM_SIZE_MAX) |
| 159 | mvebu_sdram_bs_set(i, 0x40000000); |
| 160 | } |
Prafulla Wadaskar | f8e4262 | 2010-09-30 19:33:19 +0530 | [diff] [blame] | 161 | } |
Stefan Roese | 7a96ee9 | 2015-08-03 13:15:31 +0200 | [diff] [blame] | 162 | |
| 163 | void board_add_ram_info(int use_default) |
| 164 | { |
| 165 | u32 reg; |
| 166 | |
| 167 | reg = reg_read(REG_SDRAM_CONFIG_ADDR); |
| 168 | if (reg & (1 << REG_SDRAM_CONFIG_ECC_OFFS)) |
| 169 | printf(" (ECC"); |
| 170 | else |
| 171 | printf(" (ECC not"); |
| 172 | printf(" enabled)"); |
| 173 | } |