blob: bb5989b3e85afcabad545c21e53d31a4e0e23e53 [file] [log] [blame]
Prafulla Wadaskara09bbe52009-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 Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +02007 */
8
9#include <config.h>
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +053010#include <common.h>
Lei Wen298ae912011-10-18 20:11:42 +053011#include <asm/io.h>
12#include <asm/arch/cpu.h>
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020013#include <asm/arch/kirkwood.h>
14
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +053015DECLARE_GLOBAL_DATA_PTR;
16
Holger Brunck67393fc2012-07-20 02:34:24 +000017struct kw_sdram_bank {
18 u32 win_bar;
19 u32 win_sz;
20};
21
22struct kw_sdram_addr_dec {
23 struct kw_sdram_bank sdram_bank[4];
24};
25
Gerlando Falautoac935e22012-07-20 02:34:25 +000026#define KW_REG_CPUCS_WIN_ENABLE (1 << 0)
27#define KW_REG_CPUCS_WIN_WR_PROTECT (1 << 1)
28#define KW_REG_CPUCS_WIN_WIN0_CS(x) (((x) & 0x3) << 2)
29#define KW_REG_CPUCS_WIN_SIZE(x) (((x) & 0xff) << 24)
30
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020031/*
32 * kw_sdram_bar - reads SDRAM Base Address Register
33 */
34u32 kw_sdram_bar(enum memory_bank bank)
35{
Holger Brunck67393fc2012-07-20 02:34:24 +000036 struct kw_sdram_addr_dec *base =
37 (struct kw_sdram_addr_dec *)KW_REGISTER(0x1500);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020038 u32 result = 0;
Holger Brunck67393fc2012-07-20 02:34:24 +000039 u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020040
41 if ((!enable) || (bank > BANK3))
42 return 0;
43
Holger Brunck67393fc2012-07-20 02:34:24 +000044 result = readl(&base->sdram_bank[bank].win_bar);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020045 return result;
46}
47
48/*
Gerlando Falautoac935e22012-07-20 02:34:25 +000049 * kw_sdram_bs_set - writes SDRAM Bank size
50 */
51static void kw_sdram_bs_set(enum memory_bank bank, u32 size)
52{
53 struct kw_sdram_addr_dec *base =
54 (struct kw_sdram_addr_dec *)KW_REGISTER(0x1500);
55 /* Read current register value */
56 u32 reg = readl(&base->sdram_bank[bank].win_sz);
57
58 /* Clear window size */
59 reg &= ~KW_REG_CPUCS_WIN_SIZE(0xFF);
60
61 /* Set new window size */
62 reg |= KW_REG_CPUCS_WIN_SIZE((size - 1) >> 24);
63
64 writel(reg, &base->sdram_bank[bank].win_sz);
65}
66
67/*
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020068 * kw_sdram_bs - reads SDRAM Bank size
69 */
70u32 kw_sdram_bs(enum memory_bank bank)
71{
Holger Brunck67393fc2012-07-20 02:34:24 +000072 struct kw_sdram_addr_dec *base =
73 (struct kw_sdram_addr_dec *)KW_REGISTER(0x1500);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020074 u32 result = 0;
Holger Brunck67393fc2012-07-20 02:34:24 +000075 u32 enable = 0x01 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020076
77 if ((!enable) || (bank > BANK3))
78 return 0;
Holger Brunck67393fc2012-07-20 02:34:24 +000079 result = 0xff000000 & readl(&base->sdram_bank[bank].win_sz);
Prafulla Wadaskara09bbe52009-06-20 11:01:53 +020080 result += 0x01000000;
81 return result;
82}
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +053083
Gerlando Falautoea32b7e2012-07-25 06:23:48 +000084void kw_sdram_size_adjust(enum memory_bank bank)
85{
86 u32 size;
87
88 /* probe currently equipped RAM size */
89 size = get_ram_size((void *)kw_sdram_bar(bank), kw_sdram_bs(bank));
90
91 /* adjust SDRAM window size accordingly */
92 kw_sdram_bs_set(bank, size);
93}
94
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +053095#ifndef CONFIG_SYS_BOARD_DRAM_INIT
96int dram_init(void)
97{
98 int i;
99
100 gd->ram_size = 0;
101 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
102 gd->bd->bi_dram[i].start = kw_sdram_bar(i);
103 gd->bd->bi_dram[i].size = kw_sdram_bs(i);
104 /*
105 * It is assumed that all memory banks are consecutive
106 * and without gaps.
107 * If the gap is found, ram_size will be reported for
108 * consecutive memory only
109 */
110 if (gd->bd->bi_dram[i].start != gd->ram_size)
111 break;
112
Stefan Roese2cc5fad2014-10-22 12:13:05 +0200113 /*
114 * Don't report more than 3GiB of SDRAM, otherwise there is no
115 * address space left for the internal registers etc.
116 */
117 if ((gd->ram_size + gd->bd->bi_dram[i].size != 0) &&
118 (gd->ram_size + gd->bd->bi_dram[i].size <= (3 << 30)))
119 gd->ram_size += gd->bd->bi_dram[i].size;
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530120
121 }
Tanmay Upadhyay9a5ff782010-10-28 20:06:22 +0530122
123 for (; i < CONFIG_NR_DRAM_BANKS; i++) {
124 /* If above loop terminated prematurely, we need to set
125 * remaining banks' start address & size as 0. Otherwise other
126 * u-boot functions and Linux kernel gets wrong values which
127 * could result in crash */
128 gd->bd->bi_dram[i].start = 0;
129 gd->bd->bi_dram[i].size = 0;
130 }
131
Prafulla Wadaskarf8e42622010-09-30 19:33:19 +0530132 return 0;
133}
134
135/*
136 * If this function is not defined here,
137 * board.c alters dram bank zero configuration defined above.
138 */
139void dram_init_banksize(void)
140{
141 dram_init();
142}
143#endif /* CONFIG_SYS_BOARD_DRAM_INIT */