blob: db18791a8627e77f9baba41199a9ba5fba3af37c [file] [log] [blame]
Prafulla Wadaskar4efb77d2009-06-20 11:01:53 +02001/*
2 * (C) Copyright 2009
3 * Marvell Semiconductor <www.marvell.com>
4 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskar4efb77d2009-06-20 11:01:53 +02007 */
8
9#include <config.h>
Prafulla Wadaskarbeeb2582010-09-30 19:33:19 +053010#include <common.h>
Lei Wena7efd712011-10-18 20:11:42 +053011#include <asm/io.h>
12#include <asm/arch/cpu.h>
Stefan Roese3dc23f72014-10-22 12:13:06 +020013#include <asm/arch/soc.h>
Prafulla Wadaskar4efb77d2009-06-20 11:01:53 +020014
Prafulla Wadaskarbeeb2582010-09-30 19:33:19 +053015DECLARE_GLOBAL_DATA_PTR;
16
Stefan Roese96c5f082014-10-22 12:13:13 +020017struct sdram_bank {
Holger Brunckcf37c5d2012-07-20 02:34:24 +000018 u32 win_bar;
19 u32 win_sz;
20};
21
Stefan Roese96c5f082014-10-22 12:13:13 +020022struct sdram_addr_dec {
23 struct sdram_bank sdram_bank[4];
Holger Brunckcf37c5d2012-07-20 02:34:24 +000024};
25
Stefan Roese96c5f082014-10-22 12:13:13 +020026#define REG_CPUCS_WIN_ENABLE (1 << 0)
27#define REG_CPUCS_WIN_WR_PROTECT (1 << 1)
28#define REG_CPUCS_WIN_WIN0_CS(x) (((x) & 0x3) << 2)
29#define REG_CPUCS_WIN_SIZE(x) (((x) & 0xff) << 24)
Gerlando Falauto45515162012-07-20 02:34:25 +000030
Prafulla Wadaskar4efb77d2009-06-20 11:01:53 +020031/*
Stefan Roese96c5f082014-10-22 12:13:13 +020032 * mvebu_sdram_bar - reads SDRAM Base Address Register
Prafulla Wadaskar4efb77d2009-06-20 11:01:53 +020033 */
Stefan Roese96c5f082014-10-22 12:13:13 +020034u32 mvebu_sdram_bar(enum memory_bank bank)
Prafulla Wadaskar4efb77d2009-06-20 11:01:53 +020035{
Stefan Roese96c5f082014-10-22 12:13:13 +020036 struct sdram_addr_dec *base =
37 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
Prafulla Wadaskar4efb77d2009-06-20 11:01:53 +020038 u32 result = 0;
Holger Brunckcf37c5d2012-07-20 02:34:24 +000039 u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskar4efb77d2009-06-20 11:01:53 +020040
41 if ((!enable) || (bank > BANK3))
42 return 0;
43
Holger Brunckcf37c5d2012-07-20 02:34:24 +000044 result = readl(&base->sdram_bank[bank].win_bar);
Prafulla Wadaskar4efb77d2009-06-20 11:01:53 +020045 return result;
46}
47
48/*
Stefan Roese96c5f082014-10-22 12:13:13 +020049 * mvebu_sdram_bs_set - writes SDRAM Bank size
Gerlando Falauto45515162012-07-20 02:34:25 +000050 */
Stefan Roese96c5f082014-10-22 12:13:13 +020051static void mvebu_sdram_bs_set(enum memory_bank bank, u32 size)
Gerlando Falauto45515162012-07-20 02:34:25 +000052{
Stefan Roese96c5f082014-10-22 12:13:13 +020053 struct sdram_addr_dec *base =
54 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
Gerlando Falauto45515162012-07-20 02:34:25 +000055 /* Read current register value */
56 u32 reg = readl(&base->sdram_bank[bank].win_sz);
57
58 /* Clear window size */
Stefan Roese96c5f082014-10-22 12:13:13 +020059 reg &= ~REG_CPUCS_WIN_SIZE(0xFF);
Gerlando Falauto45515162012-07-20 02:34:25 +000060
61 /* Set new window size */
Stefan Roese96c5f082014-10-22 12:13:13 +020062 reg |= REG_CPUCS_WIN_SIZE((size - 1) >> 24);
Gerlando Falauto45515162012-07-20 02:34:25 +000063
64 writel(reg, &base->sdram_bank[bank].win_sz);
65}
66
67/*
Stefan Roese96c5f082014-10-22 12:13:13 +020068 * mvebu_sdram_bs - reads SDRAM Bank size
Prafulla Wadaskar4efb77d2009-06-20 11:01:53 +020069 */
Stefan Roese96c5f082014-10-22 12:13:13 +020070u32 mvebu_sdram_bs(enum memory_bank bank)
Prafulla Wadaskar4efb77d2009-06-20 11:01:53 +020071{
Stefan Roese96c5f082014-10-22 12:13:13 +020072 struct sdram_addr_dec *base =
73 (struct sdram_addr_dec *)MVEBU_SDRAM_BASE;
Prafulla Wadaskar4efb77d2009-06-20 11:01:53 +020074 u32 result = 0;
Holger Brunckcf37c5d2012-07-20 02:34:24 +000075 u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskar4efb77d2009-06-20 11:01:53 +020076
77 if ((!enable) || (bank > BANK3))
78 return 0;
Holger Brunckcf37c5d2012-07-20 02:34:24 +000079 result = 0xff000000 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskar4efb77d2009-06-20 11:01:53 +020080 result += 0x01000000;
81 return result;
82}
Prafulla Wadaskarbeeb2582010-09-30 19:33:19 +053083
Stefan Roese96c5f082014-10-22 12:13:13 +020084void mvebu_sdram_size_adjust(enum memory_bank bank)
Gerlando Falautob3168f42012-07-25 06:23:48 +000085{
86 u32 size;
87
88 /* probe currently equipped RAM size */
Stefan Roese96c5f082014-10-22 12:13:13 +020089 size = get_ram_size((void *)mvebu_sdram_bar(bank),
90 mvebu_sdram_bs(bank));
Gerlando Falautob3168f42012-07-25 06:23:48 +000091
92 /* adjust SDRAM window size accordingly */
Stefan Roese96c5f082014-10-22 12:13:13 +020093 mvebu_sdram_bs_set(bank, size);
Gerlando Falautob3168f42012-07-25 06:23:48 +000094}
95
Prafulla Wadaskarbeeb2582010-09-30 19:33:19 +053096#ifndef CONFIG_SYS_BOARD_DRAM_INIT
97int dram_init(void)
98{
99 int i;
100
101 gd->ram_size = 0;
102 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
Stefan Roese96c5f082014-10-22 12:13:13 +0200103 gd->bd->bi_dram[i].start = mvebu_sdram_bar(i);
104 gd->bd->bi_dram[i].size = mvebu_sdram_bs(i);
Prafulla Wadaskarbeeb2582010-09-30 19:33:19 +0530105 /*
106 * It is assumed that all memory banks are consecutive
107 * and without gaps.
108 * If the gap is found, ram_size will be reported for
109 * consecutive memory only
110 */
111 if (gd->bd->bi_dram[i].start != gd->ram_size)
112 break;
113
Stefan Roesed80cca22014-10-22 12:13:05 +0200114 /*
115 * Don't report more than 3GiB of SDRAM, otherwise there is no
116 * address space left for the internal registers etc.
117 */
118 if ((gd->ram_size + gd->bd->bi_dram[i].size != 0) &&
119 (gd->ram_size + gd->bd->bi_dram[i].size <= (3 << 30)))
120 gd->ram_size += gd->bd->bi_dram[i].size;
Prafulla Wadaskarbeeb2582010-09-30 19:33:19 +0530121
122 }
Tanmay Upadhyay28e57102010-10-28 20:06:22 +0530123
124 for (; i < CONFIG_NR_DRAM_BANKS; i++) {
125 /* If above loop terminated prematurely, we need to set
126 * remaining banks' start address & size as 0. Otherwise other
127 * u-boot functions and Linux kernel gets wrong values which
128 * could result in crash */
129 gd->bd->bi_dram[i].start = 0;
130 gd->bd->bi_dram[i].size = 0;
131 }
132
Prafulla Wadaskarbeeb2582010-09-30 19:33:19 +0530133 return 0;
134}
135
136/*
137 * If this function is not defined here,
138 * board.c alters dram bank zero configuration defined above.
139 */
140void dram_init_banksize(void)
141{
142 dram_init();
143}
144#endif /* CONFIG_SYS_BOARD_DRAM_INIT */