blob: 6155f0eb459166f7b970d43273835d1d1b61695c [file] [log] [blame]
Matthias Kaehlcke279437d2010-02-01 21:29:48 +01001/*
2 * Copyright (C) 2009 Matthias Kaehlcke <matthias@kaehlcke.net>
3 *
4 * Copyright (C) 2006 Dominic Rath <Dominic.Rath@gmx.de>
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., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25#include <asm/io.h>
26#include "sdram_cfg.h"
27#include "early_udelay.h"
28
29#define PROGRAM_MODE_REG(bank) (*(volatile uint32_t *) \
30 (SDRAM_BASE_ADDR | SDRAM_BANK_SEL_##bank | SDRAM_MODE_REG_VAL))
31
32#define PRECHARGE_BANK(bank) (*(volatile uint32_t *) \
33 (SDRAM_BASE_ADDR | SDRAM_BANK_SEL_##bank))
34
35static void force_precharge(void);
36static void setup_refresh_timer(void);
37static void program_mode_registers(void);
38
39void sdram_cfg(void)
40{
41 struct sdram_regs *sdram = (struct sdram_regs *)SDRAM_BASE;
42
43 writel(SDRAM_DEVCFG_VAL, &sdram->SDRAM_DEVCFG_REG);
44
45 /* Issue continous NOP commands */
46 writel(GLCONFIG_INIT | GLCONFIG_MRS | GLCONFIG_CKE, &sdram->glconfig);
47
48 early_udelay(200);
49
50 force_precharge();
51
52 setup_refresh_timer();
53
54 program_mode_registers();
55
56 /* Select normal operation mode */
57 writel(GLCONFIG_CKE, &sdram->glconfig);
58}
59
60static void force_precharge(void)
61{
62 /*
63 * Errata most EP93xx revisions say that PRECHARGE ALL isn't always
64 * issued.
65 *
66 * Do a read from each bank to make sure they're precharged
67 */
68
69 PRECHARGE_BANK(0);
70 PRECHARGE_BANK(1);
71 PRECHARGE_BANK(2);
72 PRECHARGE_BANK(3);
73}
74
75static void setup_refresh_timer(void)
76{
77 struct sdram_regs *sdram = (struct sdram_regs *)SDRAM_BASE;
78
79 /* Load refresh timer with 10 to issue refresh every 10 cycles */
80 writel(0x0a, &sdram->refrshtimr);
81
82 /*
83 * Wait at least 80 clock cycles to provide 8 refresh cycles
84 * to all SDRAMs
85 */
86 early_udelay(1);
87
88 /*
89 * Program refresh timer with normal value
90 * We need 8192 refresh cycles every 64ms
91 * at 15ns (HCLK >= 66MHz) per cycle:
92 * 64ms / 8192 = 7.8125us
93 * 7.8125us / 15ns = 520 (0x208)
94 */
95 /*
96 * TODO: redboot uses 0x1e0 for the slowest possible device
97 * but i don't understand how this value is calculated
98 */
99 writel(0x208, &sdram->refrshtimr);
100}
101
102static void program_mode_registers(void)
103{
104 /*
105 * The mode registers are programmed by performing a read from each
106 * SDRAM bank. The value of the address that is read defines the value
107 * that is written into the mode register
108 */
109
110 PROGRAM_MODE_REG(0);
111
112#if (CONFIG_NR_DRAM_BANKS >= 2)
113 PROGRAM_MODE_REG(1);
114#endif
115
116#if (CONFIG_NR_DRAM_BANKS >= 3)
117 PROGRAM_MODE_REG(2);
118#endif
119
120#if (CONFIG_NR_DRAM_BANKS == 4)
121 PROGRAM_MODE_REG(3);
122#endif
123}