blob: a936e03166c1d81de52c760962b642c933ee92f5 [file] [log] [blame]
Wolfgang Denk72a087e2006-10-24 14:27:35 +02001/*
2 * Copyright (C) 2005-2006 Atmel Corporation
3 *
4 * See file CREDITS for list of people who contributed to this
5 * project.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 * MA 02111-1307 USA
21 */
22#include <common.h>
23
24#ifdef CFG_HSDRAMC
25#include <asm/io.h>
26#include <asm/sdram.h>
27
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010028#include <asm/arch/clk.h>
29#include <asm/arch/memory-map.h>
Wolfgang Denk72a087e2006-10-24 14:27:35 +020030
31#include "hsdramc1.h"
32
Wolfgang Denk72a087e2006-10-24 14:27:35 +020033unsigned long sdram_init(const struct sdram_info *info)
34{
35 unsigned long *sdram = (unsigned long *)uncached(info->phys_addr);
36 unsigned long sdram_size;
37 unsigned long tmp;
38 unsigned long bus_hz;
39 unsigned int i;
40
Wolfgang Denk72a087e2006-10-24 14:27:35 +020041 tmp = (HSDRAMC1_BF(NC, info->col_bits - 8)
42 | HSDRAMC1_BF(NR, info->row_bits - 11)
43 | HSDRAMC1_BF(NB, info->bank_bits - 1)
44 | HSDRAMC1_BF(CAS, info->cas)
45 | HSDRAMC1_BF(TWR, info->twr)
46 | HSDRAMC1_BF(TRC, info->trc)
47 | HSDRAMC1_BF(TRP, info->trp)
48 | HSDRAMC1_BF(TRCD, info->trcd)
49 | HSDRAMC1_BF(TRAS, info->tras)
50 | HSDRAMC1_BF(TXSR, info->txsr));
51
52#ifdef CFG_SDRAM_16BIT
53 tmp |= HSDRAMC1_BIT(DBW);
54 sdram_size = 1 << (info->row_bits + info->col_bits
55 + info->bank_bits + 1);
56#else
57 sdram_size = 1 << (info->row_bits + info->col_bits
58 + info->bank_bits + 2);
59#endif
60
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010061 hsdramc1_writel(CR, tmp);
Wolfgang Denk72a087e2006-10-24 14:27:35 +020062
63 /*
64 * Initialization sequence for SDRAM, from the data sheet:
65 *
66 * 1. A minimum pause of 200 us is provided to precede any
67 * signal toggle.
68 */
69 udelay(200);
70
71 /*
72 * 2. A Precharge All command is issued to the SDRAM
73 */
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010074 hsdramc1_writel(MR, HSDRAMC1_MODE_BANKS_PRECHARGE);
75 hsdramc1_readl(MR);
Wolfgang Denk72a087e2006-10-24 14:27:35 +020076 writel(0, sdram);
77
78 /*
79 * 3. Eight auto-refresh (CBR) cycles are provided
80 */
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010081 hsdramc1_writel(MR, HSDRAMC1_MODE_AUTO_REFRESH);
82 hsdramc1_readl(MR);
Wolfgang Denk72a087e2006-10-24 14:27:35 +020083 for (i = 0; i < 8; i++)
84 writel(0, sdram);
85
86 /*
87 * 4. A mode register set (MRS) cycle is issued to program
88 * SDRAM parameters, in particular CAS latency and burst
89 * length.
90 *
91 * CAS from info struct, burst length 1, serial burst type
92 */
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010093 hsdramc1_writel(MR, HSDRAMC1_MODE_LOAD_MODE);
94 hsdramc1_readl(MR);
Wolfgang Denk72a087e2006-10-24 14:27:35 +020095 writel(0, sdram + (info->cas << 4));
96
97 /*
98 * 5. A Normal Mode command is provided, 3 clocks after tMRD
99 * is met.
100 *
101 * From the timing diagram, it looks like tMRD is 3
102 * cycles...try a dummy read from the peripheral bus.
103 */
Haavard Skinnemoendf548d32006-11-19 18:06:53 +0100104 hsdramc1_readl(MR);
105 hsdramc1_writel(MR, HSDRAMC1_MODE_NORMAL);
106 hsdramc1_readl(MR);
Wolfgang Denk72a087e2006-10-24 14:27:35 +0200107 writel(0, sdram);
108
109 /*
110 * 6. Write refresh rate into SDRAMC refresh timer count
111 * register (refresh rate = timing between refresh cycles).
112 *
113 * 15.6 us is a typical value for a burst of length one
114 */
Haavard Skinnemoendf548d32006-11-19 18:06:53 +0100115 bus_hz = get_sdram_clk_rate();
116 hsdramc1_writel(TR, (156 * (bus_hz / 1000)) / 10000);
Wolfgang Denk72a087e2006-10-24 14:27:35 +0200117
118 printf("SDRAM: %u MB at address 0x%08lx\n",
119 sdram_size >> 20, info->phys_addr);
120
121 printf("Testing SDRAM...");
122 for (i = 0; i < sdram_size / 4; i++)
123 sdram[i] = i;
124
125 for (i = 0; i < sdram_size / 4; i++) {
126 tmp = sdram[i];
127 if (tmp != i) {
128 printf("FAILED at address 0x%08lx\n",
129 info->phys_addr + i * 4);
130 printf("SDRAM: read 0x%lx, expected 0x%lx\n", tmp, i);
131 return 0;
132 }
133 }
134
135 puts("OK\n");
136
137 return sdram_size;
138}
139
140#endif /* CFG_HSDRAMC */