Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005-2006 Atmel Corporation |
| 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 5 | */ |
| 6 | #include <common.h> |
| 7 | |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 8 | #include <asm/io.h> |
| 9 | #include <asm/sdram.h> |
| 10 | |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 11 | #include <asm/arch/clk.h> |
Andreas Bießmann | 5d73bc7 | 2010-11-04 23:15:30 +0000 | [diff] [blame] | 12 | #include <asm/arch/hardware.h> |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 13 | |
| 14 | #include "hsdramc1.h" |
| 15 | |
Haavard Skinnemoen | a23e277 | 2008-05-19 11:36:28 +0200 | [diff] [blame] | 16 | unsigned long sdram_init(void *sdram_base, const struct sdram_config *config) |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 17 | { |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 18 | unsigned long sdram_size; |
Haavard Skinnemoen | a23e277 | 2008-05-19 11:36:28 +0200 | [diff] [blame] | 19 | uint32_t cfgreg; |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 20 | unsigned int i; |
| 21 | |
Haavard Skinnemoen | a23e277 | 2008-05-19 11:36:28 +0200 | [diff] [blame] | 22 | 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 Skinnemoen | d38da53 | 2008-01-23 17:20:14 +0100 | [diff] [blame] | 32 | |
Haavard Skinnemoen | a23e277 | 2008-05-19 11:36:28 +0200 | [diff] [blame] | 33 | if (config->data_bits == SDRAM_DATA_16BIT) |
| 34 | cfgreg |= HSDRAMC1_BIT(DBW); |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 35 | |
Haavard Skinnemoen | a23e277 | 2008-05-19 11:36:28 +0200 | [diff] [blame] | 36 | hsdramc1_writel(CR, cfgreg); |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 37 | |
Haavard Skinnemoen | a23e277 | 2008-05-19 11:36:28 +0200 | [diff] [blame] | 38 | /* 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 Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 42 | |
| 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 Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 54 | hsdramc1_writel(MR, HSDRAMC1_MODE_BANKS_PRECHARGE); |
| 55 | hsdramc1_readl(MR); |
Haavard Skinnemoen | a23e277 | 2008-05-19 11:36:28 +0200 | [diff] [blame] | 56 | writel(0, sdram_base); |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * 3. Eight auto-refresh (CBR) cycles are provided |
| 60 | */ |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 61 | hsdramc1_writel(MR, HSDRAMC1_MODE_AUTO_REFRESH); |
| 62 | hsdramc1_readl(MR); |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 63 | for (i = 0; i < 8; i++) |
Haavard Skinnemoen | a23e277 | 2008-05-19 11:36:28 +0200 | [diff] [blame] | 64 | writel(0, sdram_base); |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 65 | |
| 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 Skinnemoen | a23e277 | 2008-05-19 11:36:28 +0200 | [diff] [blame] | 71 | * The address will be chosen by the SDRAMC automatically; we |
| 72 | * just have to make sure BA[1:0] are set to 0. |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 73 | */ |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 74 | hsdramc1_writel(MR, HSDRAMC1_MODE_LOAD_MODE); |
| 75 | hsdramc1_readl(MR); |
Haavard Skinnemoen | a23e277 | 2008-05-19 11:36:28 +0200 | [diff] [blame] | 76 | writel(0, sdram_base); |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 77 | |
| 78 | /* |
Haavard Skinnemoen | a23e277 | 2008-05-19 11:36:28 +0200 | [diff] [blame] | 79 | * 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 Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 82 | */ |
Haavard Skinnemoen | df548d3 | 2006-11-19 18:06:53 +0100 | [diff] [blame] | 83 | hsdramc1_writel(MR, HSDRAMC1_MODE_NORMAL); |
| 84 | hsdramc1_readl(MR); |
Haavard Skinnemoen | a23e277 | 2008-05-19 11:36:28 +0200 | [diff] [blame] | 85 | writel(0, sdram_base); |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * 6. Write refresh rate into SDRAMC refresh timer count |
| 89 | * register (refresh rate = timing between refresh cycles). |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 90 | */ |
Haavard Skinnemoen | a23e277 | 2008-05-19 11:36:28 +0200 | [diff] [blame] | 91 | hsdramc1_writel(TR, config->refresh_period); |
Wolfgang Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 92 | |
Haavard Skinnemoen | a23e277 | 2008-05-19 11:36:28 +0200 | [diff] [blame] | 93 | 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 Denk | 72a087e | 2006-10-24 14:27:35 +0200 | [diff] [blame] | 99 | |
| 100 | return sdram_size; |
| 101 | } |