blob: 7e4817203869d13d15b73e8d797b08b24470f0bc [file] [log] [blame]
Wolfgang Denk72a087e2006-10-24 14:27:35 +02001/*
2 * Copyright (C) 2005-2006 Atmel Corporation
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Wolfgang Denk72a087e2006-10-24 14:27:35 +02005 */
6#include <common.h>
7
Wolfgang Denk72a087e2006-10-24 14:27:35 +02008#include <asm/io.h>
9#include <asm/sdram.h>
10
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010011#include <asm/arch/clk.h>
Andreas Bießmann5d73bc72010-11-04 23:15:30 +000012#include <asm/arch/hardware.h>
Wolfgang Denk72a087e2006-10-24 14:27:35 +020013
14#include "hsdramc1.h"
15
Haavard Skinnemoena23e2772008-05-19 11:36:28 +020016unsigned long sdram_init(void *sdram_base, const struct sdram_config *config)
Wolfgang Denk72a087e2006-10-24 14:27:35 +020017{
Wolfgang Denk72a087e2006-10-24 14:27:35 +020018 unsigned long sdram_size;
Haavard Skinnemoena23e2772008-05-19 11:36:28 +020019 uint32_t cfgreg;
Wolfgang Denk72a087e2006-10-24 14:27:35 +020020 unsigned int i;
21
Haavard Skinnemoena23e2772008-05-19 11:36:28 +020022 cfgreg = (HSDRAMC1_BF(NC, config->col_bits - 8)
23 | HSDRAMC1_BF(NR, config->row_bits - 11)
24 | HSDRAMC1_BF(NB, config->bank_bits - 1)
25 | HSDRAMC1_BF(CAS, config->cas)
26 | HSDRAMC1_BF(TWR, config->twr)
27 | HSDRAMC1_BF(TRC, config->trc)
28 | HSDRAMC1_BF(TRP, config->trp)
29 | HSDRAMC1_BF(TRCD, config->trcd)
30 | HSDRAMC1_BF(TRAS, config->tras)
31 | HSDRAMC1_BF(TXSR, config->txsr));
Haavard Skinnemoend38da532008-01-23 17:20:14 +010032
Haavard Skinnemoena23e2772008-05-19 11:36:28 +020033 if (config->data_bits == SDRAM_DATA_16BIT)
34 cfgreg |= HSDRAMC1_BIT(DBW);
Wolfgang Denk72a087e2006-10-24 14:27:35 +020035
Haavard Skinnemoena23e2772008-05-19 11:36:28 +020036 hsdramc1_writel(CR, cfgreg);
Wolfgang Denk72a087e2006-10-24 14:27:35 +020037
Haavard Skinnemoena23e2772008-05-19 11:36:28 +020038 /* Send a NOP to turn on the clock (necessary on some chips) */
39 hsdramc1_writel(MR, HSDRAMC1_MODE_NOP);
40 hsdramc1_readl(MR);
41 writel(0, sdram_base);
Wolfgang Denk72a087e2006-10-24 14:27:35 +020042
43 /*
44 * Initialization sequence for SDRAM, from the data sheet:
45 *
46 * 1. A minimum pause of 200 us is provided to precede any
47 * signal toggle.
48 */
49 udelay(200);
50
51 /*
52 * 2. A Precharge All command is issued to the SDRAM
53 */
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010054 hsdramc1_writel(MR, HSDRAMC1_MODE_BANKS_PRECHARGE);
55 hsdramc1_readl(MR);
Haavard Skinnemoena23e2772008-05-19 11:36:28 +020056 writel(0, sdram_base);
Wolfgang Denk72a087e2006-10-24 14:27:35 +020057
58 /*
59 * 3. Eight auto-refresh (CBR) cycles are provided
60 */
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010061 hsdramc1_writel(MR, HSDRAMC1_MODE_AUTO_REFRESH);
62 hsdramc1_readl(MR);
Wolfgang Denk72a087e2006-10-24 14:27:35 +020063 for (i = 0; i < 8; i++)
Haavard Skinnemoena23e2772008-05-19 11:36:28 +020064 writel(0, sdram_base);
Wolfgang Denk72a087e2006-10-24 14:27:35 +020065
66 /*
67 * 4. A mode register set (MRS) cycle is issued to program
68 * SDRAM parameters, in particular CAS latency and burst
69 * length.
70 *
Haavard Skinnemoena23e2772008-05-19 11:36:28 +020071 * The address will be chosen by the SDRAMC automatically; we
72 * just have to make sure BA[1:0] are set to 0.
Wolfgang Denk72a087e2006-10-24 14:27:35 +020073 */
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010074 hsdramc1_writel(MR, HSDRAMC1_MODE_LOAD_MODE);
75 hsdramc1_readl(MR);
Haavard Skinnemoena23e2772008-05-19 11:36:28 +020076 writel(0, sdram_base);
Wolfgang Denk72a087e2006-10-24 14:27:35 +020077
78 /*
Haavard Skinnemoena23e2772008-05-19 11:36:28 +020079 * 5. The application must go into Normal Mode, setting Mode
80 * to 0 in the Mode Register and performing a write access
81 * at any location in the SDRAM.
Wolfgang Denk72a087e2006-10-24 14:27:35 +020082 */
Haavard Skinnemoendf548d32006-11-19 18:06:53 +010083 hsdramc1_writel(MR, HSDRAMC1_MODE_NORMAL);
84 hsdramc1_readl(MR);
Haavard Skinnemoena23e2772008-05-19 11:36:28 +020085 writel(0, sdram_base);
Wolfgang Denk72a087e2006-10-24 14:27:35 +020086
87 /*
88 * 6. Write refresh rate into SDRAMC refresh timer count
89 * register (refresh rate = timing between refresh cycles).
Wolfgang Denk72a087e2006-10-24 14:27:35 +020090 */
Haavard Skinnemoena23e2772008-05-19 11:36:28 +020091 hsdramc1_writel(TR, config->refresh_period);
Wolfgang Denk72a087e2006-10-24 14:27:35 +020092
Haavard Skinnemoena23e2772008-05-19 11:36:28 +020093 if (config->data_bits == SDRAM_DATA_16BIT)
94 sdram_size = 1 << (config->row_bits + config->col_bits
95 + config->bank_bits + 1);
96 else
97 sdram_size = 1 << (config->row_bits + config->col_bits
98 + config->bank_bits + 2);
Wolfgang Denk72a087e2006-10-24 14:27:35 +020099
100 return sdram_size;
101}