summaryrefslogtreecommitdiff
path: root/arch/arm/cpu/armv7/am33xx/emif4.c
blob: 2f4164df8218a348bd82c0fd6439a529e6ddd06b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * emif4.c
 *
 * AM33XX emif4 configuration file
 *
 * Copyright (C) 2011, Texas Instruments, Incorporated - http://www.ti.com/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR /PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <common.h>
#include <asm/arch/cpu.h>
#include <asm/arch/ddr_defs.h>
#include <asm/arch/hardware.h>
#include <asm/arch/clock.h>
#include <asm/io.h>

DECLARE_GLOBAL_DATA_PTR;

struct ddr_regs *ddrregs = (struct ddr_regs *)DDR_PHY_BASE_ADDR;
struct vtp_reg *vtpreg = (struct vtp_reg *)VTP0_CTRL_ADDR;
struct ddr_ctrl *ddrctrl = (struct ddr_ctrl *)DDR_CTRL_ADDR;


int dram_init(void)
{
	/* dram_init must store complete ramsize in gd->ram_size */
	gd->ram_size = get_ram_size(
			(void *)CONFIG_SYS_SDRAM_BASE,
			CONFIG_MAX_RAM_BANK_SIZE);
	return 0;
}

void dram_init_banksize(void)
{
	gd->bd->bi_dram[0].start = CONFIG_SYS_SDRAM_BASE;
	gd->bd->bi_dram[0].size = gd->ram_size;
}


#ifdef CONFIG_SPL_BUILD
static void data_macro_config(int dataMacroNum)
{
	struct ddr_data data;

	data.datardsratio0 = ((DDR2_RD_DQS<<30)|(DDR2_RD_DQS<<20)
				|(DDR2_RD_DQS<<10)|(DDR2_RD_DQS<<0));
	data.datardsratio1 = DDR2_RD_DQS>>2;
	data.datawdsratio0 = ((DDR2_WR_DQS<<30)|(DDR2_WR_DQS<<20)
				|(DDR2_WR_DQS<<10)|(DDR2_WR_DQS<<0));
	data.datawdsratio1 = DDR2_WR_DQS>>2;
	data.datawiratio0 = ((DDR2_PHY_WRLVL<<30)|(DDR2_PHY_WRLVL<<20)
				|(DDR2_PHY_WRLVL<<10)|(DDR2_PHY_WRLVL<<0));
	data.datawiratio1 = DDR2_PHY_WRLVL>>2;
	data.datagiratio0 = ((DDR2_PHY_GATELVL<<30)|(DDR2_PHY_GATELVL<<20)
				|(DDR2_PHY_GATELVL<<10)|(DDR2_PHY_GATELVL<<0));
	data.datagiratio1 = DDR2_PHY_GATELVL>>2;
	data.datafwsratio0 = ((DDR2_PHY_FIFO_WE<<30)|(DDR2_PHY_FIFO_WE<<20)
				|(DDR2_PHY_FIFO_WE<<10)|(DDR2_PHY_FIFO_WE<<0));
	data.datafwsratio1 = DDR2_PHY_FIFO_WE>>2;
	data.datawrsratio0 = ((DDR2_PHY_WR_DATA<<30)|(DDR2_PHY_WR_DATA<<20)
				|(DDR2_PHY_WR_DATA<<10)|(DDR2_PHY_WR_DATA<<0));
	data.datawrsratio1 = DDR2_PHY_WR_DATA>>2;
	data.datadldiff0 = PHY_DLL_LOCK_DIFF;

	config_ddr_data(dataMacroNum, &data);
}

static void cmd_macro_config(void)
{
	struct cmd_control cmd;

	cmd.cmd0csratio = DDR2_RATIO;
	cmd.cmd0csforce = CMD_FORCE;
	cmd.cmd0csdelay = CMD_DELAY;
	cmd.cmd0dldiff = DDR2_DLL_LOCK_DIFF;
	cmd.cmd0iclkout = DDR2_INVERT_CLKOUT;

	cmd.cmd1csratio = DDR2_RATIO;
	cmd.cmd1csforce = CMD_FORCE;
	cmd.cmd1csdelay = CMD_DELAY;
	cmd.cmd1dldiff = DDR2_DLL_LOCK_DIFF;
	cmd.cmd1iclkout = DDR2_INVERT_CLKOUT;

	cmd.cmd2csratio = DDR2_RATIO;
	cmd.cmd2csforce = CMD_FORCE;
	cmd.cmd2csdelay = CMD_DELAY;
	cmd.cmd2dldiff = DDR2_DLL_LOCK_DIFF;
	cmd.cmd2iclkout = DDR2_INVERT_CLKOUT;

	config_cmd_ctrl(&cmd);

}

static void config_vtp(void)
{
	writel(readl(&vtpreg->vtp0ctrlreg) | VTP_CTRL_ENABLE,
			&vtpreg->vtp0ctrlreg);
	writel(readl(&vtpreg->vtp0ctrlreg) & (~VTP_CTRL_START_EN),
			&vtpreg->vtp0ctrlreg);
	writel(readl(&vtpreg->vtp0ctrlreg) | VTP_CTRL_START_EN,
			&vtpreg->vtp0ctrlreg);

	/* Poll for READY */
	while ((readl(&vtpreg->vtp0ctrlreg) & VTP_CTRL_READY) !=
			VTP_CTRL_READY)
		;
}

static void config_emif_ddr2(void)
{
	int i;
	int ret;
	struct sdram_config cfg;
	struct sdram_timing tmg;
	struct ddr_phy_control phyc;

	/*Program EMIF0 CFG Registers*/
	phyc.reg = EMIF_READ_LATENCY;
	phyc.reg_sh = EMIF_READ_LATENCY;
	phyc.reg2 = EMIF_READ_LATENCY;

	tmg.time1 = EMIF_TIM1;
	tmg.time1_sh = EMIF_TIM1;
	tmg.time2 = EMIF_TIM2;
	tmg.time2_sh = EMIF_TIM2;
	tmg.time3 = EMIF_TIM3;
	tmg.time3_sh = EMIF_TIM3;

	cfg.sdrcr = EMIF_SDCFG;
	cfg.sdrcr2 = EMIF_SDCFG;
	cfg.refresh = 0x00004650;
	cfg.refresh_sh = 0x00004650;

	/* Program EMIF instance */
	ret = config_ddr_phy(&phyc);
	if (ret < 0)
		printf("Couldn't configure phyc\n");

	ret = config_sdram(&cfg);
	if (ret < 0)
		printf("Couldn't configure SDRAM\n");

	ret = set_sdram_timings(&tmg);
	if (ret < 0)
		printf("Couldn't configure timings\n");

	/* Delay */
	for (i = 0; i < 5000; i++)
		;

	cfg.refresh = EMIF_SDREF;
	cfg.refresh_sh = EMIF_SDREF;
	cfg.sdrcr = EMIF_SDCFG;
	cfg.sdrcr2 = EMIF_SDCFG;

	ret = config_sdram(&cfg);
	if (ret < 0)
		printf("Couldn't configure SDRAM\n");
}

void config_ddr(void)
{
	int data_macro_0 = 0;
	int data_macro_1 = 1;
	struct ddr_ioctrl ioctrl;

	enable_emif_clocks();

	config_vtp();

	cmd_macro_config();

	data_macro_config(data_macro_0);
	data_macro_config(data_macro_1);

	writel(PHY_RANK0_DELAY, &ddrregs->dt0rdelays0);
	writel(PHY_RANK0_DELAY, &ddrregs->dt1rdelays0);

	ioctrl.cmd1ctl = DDR_IOCTRL_VALUE;
	ioctrl.cmd2ctl = DDR_IOCTRL_VALUE;
	ioctrl.cmd3ctl = DDR_IOCTRL_VALUE;
	ioctrl.data1ctl = DDR_IOCTRL_VALUE;
	ioctrl.data2ctl = DDR_IOCTRL_VALUE;

	config_io_ctrl(&ioctrl);

	writel(readl(&ddrctrl->ddrioctrl) & 0xefffffff, &ddrctrl->ddrioctrl);
	writel(readl(&ddrctrl->ddrckectrl) | 0x00000001, &ddrctrl->ddrckectrl);

	config_emif_ddr2();
}
#endif