Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Atmel Corporation |
| 4 | * Bo Shen <voice.shen@atmel.com> |
| 5 | * |
Wenyou Yang | 0b32638 | 2016-02-01 18:12:16 +0800 | [diff] [blame] | 6 | * Copyright (C) 2015 Atmel Corporation |
| 7 | * Wenyou Yang <wenyou.yang@atmel.com> |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <asm/io.h> |
| 12 | #include <asm/arch/atmel_mpddrc.h> |
| 13 | |
Wenyou Yang | 2dd7da9 | 2016-02-01 18:12:17 +0800 | [diff] [blame] | 14 | #define SAMA5D3_MPDDRC_VERSION 0x140 |
| 15 | |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 16 | static inline void atmel_mpddr_op(const struct atmel_mpddr *mpddr, |
| 17 | int mode, |
| 18 | u32 ram_address) |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 19 | { |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 20 | writel(mode, &mpddr->mr); |
| 21 | writel(0, ram_address); |
| 22 | } |
| 23 | |
Wenyou Yang | 2dd7da9 | 2016-02-01 18:12:17 +0800 | [diff] [blame] | 24 | static int ddr2_decodtype_is_seq(const unsigned int base, u32 cr) |
Heiko Schocher | 4e3976b | 2014-10-31 08:30:58 +0100 | [diff] [blame] | 25 | { |
Wenyou Yang | 2dd7da9 | 2016-02-01 18:12:17 +0800 | [diff] [blame] | 26 | struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base; |
| 27 | u16 version = readl(&mpddr->version) & 0xffff; |
| 28 | |
| 29 | if ((version >= SAMA5D3_MPDDRC_VERSION) && |
| 30 | (cr & ATMEL_MPDDRC_CR_DECOD_INTERLEAVED)) |
Heiko Schocher | 4e3976b | 2014-10-31 08:30:58 +0100 | [diff] [blame] | 31 | return 0; |
Wenyou Yang | 2dd7da9 | 2016-02-01 18:12:17 +0800 | [diff] [blame] | 32 | |
Heiko Schocher | 4e3976b | 2014-10-31 08:30:58 +0100 | [diff] [blame] | 33 | return 1; |
| 34 | } |
| 35 | |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 36 | |
| 37 | int ddr2_init(const unsigned int base, |
| 38 | const unsigned int ram_address, |
Wenyou Yang | aa0a58d | 2016-02-01 18:12:15 +0800 | [diff] [blame] | 39 | const struct atmel_mpddrc_config *mpddr_value) |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 40 | { |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 41 | const struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base; |
| 42 | |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 43 | u32 ba_off, cr; |
| 44 | |
| 45 | /* Compute bank offset according to NC in configuration register */ |
| 46 | ba_off = (mpddr_value->cr & ATMEL_MPDDRC_CR_NC_MASK) + 9; |
Wenyou Yang | 2dd7da9 | 2016-02-01 18:12:17 +0800 | [diff] [blame] | 47 | if (ddr2_decodtype_is_seq(base, mpddr_value->cr)) |
Heiko Schocher | a7f517a | 2014-10-31 08:30:57 +0100 | [diff] [blame] | 48 | ba_off += ((mpddr_value->cr & ATMEL_MPDDRC_CR_NR_MASK) >> 2) + 11; |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 49 | |
| 50 | ba_off += (mpddr_value->md & ATMEL_MPDDRC_MD_DBW_MASK) ? 1 : 2; |
| 51 | |
| 52 | /* Program the memory device type into the memory device register */ |
| 53 | writel(mpddr_value->md, &mpddr->md); |
| 54 | |
| 55 | /* Program the configuration register */ |
| 56 | writel(mpddr_value->cr, &mpddr->cr); |
| 57 | |
| 58 | /* Program the timing register */ |
| 59 | writel(mpddr_value->tpr0, &mpddr->tpr0); |
| 60 | writel(mpddr_value->tpr1, &mpddr->tpr1); |
| 61 | writel(mpddr_value->tpr2, &mpddr->tpr2); |
| 62 | |
| 63 | /* Issue a NOP command */ |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 64 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address); |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 65 | |
| 66 | /* A 200 us is provided to precede any signal toggle */ |
| 67 | udelay(200); |
| 68 | |
| 69 | /* Issue a NOP command */ |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 70 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address); |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 71 | |
| 72 | /* Issue an all banks precharge command */ |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 73 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_PRCGALL_CMD, ram_address); |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 74 | |
| 75 | /* Issue an extended mode register set(EMRS2) to choose operation */ |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 76 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD, |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 77 | ram_address + (0x2 << ba_off)); |
| 78 | |
| 79 | /* Issue an extended mode register set(EMRS3) to set EMSR to 0 */ |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 80 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD, |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 81 | ram_address + (0x3 << ba_off)); |
| 82 | |
| 83 | /* |
| 84 | * Issue an extended mode register set(EMRS1) to enable DLL and |
| 85 | * program D.I.C (output driver impedance control) |
| 86 | */ |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 87 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD, |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 88 | ram_address + (0x1 << ba_off)); |
| 89 | |
| 90 | /* Enable DLL reset */ |
| 91 | cr = readl(&mpddr->cr); |
| 92 | writel(cr | ATMEL_MPDDRC_CR_DLL_RESET_ENABLED, &mpddr->cr); |
| 93 | |
| 94 | /* A mode register set(MRS) cycle is issued to reset DLL */ |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 95 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LMR_CMD, ram_address); |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 96 | |
| 97 | /* Issue an all banks precharge command */ |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 98 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_PRCGALL_CMD, ram_address); |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 99 | |
| 100 | /* Two auto-refresh (CBR) cycles are provided */ |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 101 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_RFSH_CMD, ram_address); |
| 102 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_RFSH_CMD, ram_address); |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 103 | |
| 104 | /* Disable DLL reset */ |
| 105 | cr = readl(&mpddr->cr); |
| 106 | writel(cr & (~ATMEL_MPDDRC_CR_DLL_RESET_ENABLED), &mpddr->cr); |
| 107 | |
| 108 | /* A mode register set (MRS) cycle is issued to disable DLL reset */ |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 109 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LMR_CMD, ram_address); |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 110 | |
| 111 | /* Set OCD calibration in default state */ |
| 112 | cr = readl(&mpddr->cr); |
| 113 | writel(cr | ATMEL_MPDDRC_CR_OCD_DEFAULT, &mpddr->cr); |
| 114 | |
| 115 | /* |
| 116 | * An extended mode register set (EMRS1) cycle is issued |
| 117 | * to OCD default value |
| 118 | */ |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 119 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD, |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 120 | ram_address + (0x1 << ba_off)); |
| 121 | |
| 122 | /* OCD calibration mode exit */ |
| 123 | cr = readl(&mpddr->cr); |
| 124 | writel(cr & (~ATMEL_MPDDRC_CR_OCD_DEFAULT), &mpddr->cr); |
| 125 | |
| 126 | /* |
| 127 | * An extended mode register set (EMRS1) cycle is issued |
| 128 | * to enable OCD exit |
| 129 | */ |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 130 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD, |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 131 | ram_address + (0x1 << ba_off)); |
| 132 | |
| 133 | /* A nornal mode command is provided */ |
Erik van Luijk | 59d780a | 2015-08-13 15:43:18 +0200 | [diff] [blame] | 134 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NORMAL_CMD, ram_address); |
Bo Shen | fd12de9 | 2013-11-15 11:12:37 +0800 | [diff] [blame] | 135 | |
| 136 | /* Perform a write access to any DDR2-SDRAM address */ |
| 137 | writel(0, ram_address); |
| 138 | |
| 139 | /* Write the refresh rate */ |
| 140 | writel(mpddr_value->rtr, &mpddr->rtr); |
| 141 | |
| 142 | return 0; |
| 143 | } |
Wenyou Yang | 0b32638 | 2016-02-01 18:12:16 +0800 | [diff] [blame] | 144 | |
| 145 | int ddr3_init(const unsigned int base, |
| 146 | const unsigned int ram_address, |
| 147 | const struct atmel_mpddrc_config *mpddr_value) |
| 148 | { |
| 149 | struct atmel_mpddr *mpddr = (struct atmel_mpddr *)base; |
| 150 | u32 ba_off; |
| 151 | |
| 152 | /* Compute bank offset according to NC in configuration register */ |
| 153 | ba_off = (mpddr_value->cr & ATMEL_MPDDRC_CR_NC_MASK) + 9; |
Wenyou Yang | 2dd7da9 | 2016-02-01 18:12:17 +0800 | [diff] [blame] | 154 | if (ddr2_decodtype_is_seq(base, mpddr_value->cr)) |
Wenyou Yang | 0b32638 | 2016-02-01 18:12:16 +0800 | [diff] [blame] | 155 | ba_off += ((mpddr_value->cr & |
| 156 | ATMEL_MPDDRC_CR_NR_MASK) >> 2) + 11; |
| 157 | |
| 158 | ba_off += (mpddr_value->md & ATMEL_MPDDRC_MD_DBW_MASK) ? 1 : 2; |
| 159 | |
| 160 | /* Program the memory device type */ |
| 161 | writel(mpddr_value->md, &mpddr->md); |
| 162 | |
| 163 | /* |
| 164 | * Program features of the DDR3-SDRAM device and timing parameters |
| 165 | */ |
| 166 | writel(mpddr_value->cr, &mpddr->cr); |
| 167 | |
| 168 | writel(mpddr_value->tpr0, &mpddr->tpr0); |
| 169 | writel(mpddr_value->tpr1, &mpddr->tpr1); |
| 170 | writel(mpddr_value->tpr2, &mpddr->tpr2); |
| 171 | |
| 172 | /* A NOP command is issued to the DDR3-SRAM */ |
| 173 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address); |
| 174 | |
| 175 | /* A pause of at least 500us must be observed before a single toggle. */ |
| 176 | udelay(500); |
| 177 | |
| 178 | /* A NOP command is issued to the DDR3-SDRAM */ |
| 179 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NOP_CMD, ram_address); |
| 180 | |
| 181 | /* |
| 182 | * An Extended Mode Register Set (EMRS2) cycle is issued to choose |
| 183 | * between commercial or high temperature operations. |
| 184 | */ |
| 185 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD, |
| 186 | ram_address + (0x2 << ba_off)); |
| 187 | /* |
| 188 | * Step 7: An Extended Mode Register Set (EMRS3) cycle is issued to set |
| 189 | * the Extended Mode Register to 0. |
| 190 | */ |
| 191 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD, |
| 192 | ram_address + (0x3 << ba_off)); |
| 193 | /* |
| 194 | * An Extended Mode Register Set (EMRS1) cycle is issued to disable and |
| 195 | * to program O.D.S. (Output Driver Strength). |
| 196 | */ |
| 197 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_EXT_LMR_CMD, |
| 198 | ram_address + (0x1 << ba_off)); |
| 199 | |
| 200 | /* |
| 201 | * Write a one to the DLL bit (enable DLL reset) in the MPDDRC |
| 202 | * Configuration Register. |
| 203 | */ |
| 204 | |
| 205 | /* A Mode Register Set (MRS) cycle is issued to reset DLL. */ |
| 206 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_LMR_CMD, ram_address); |
| 207 | |
| 208 | udelay(50); |
| 209 | |
| 210 | /* |
| 211 | * A Calibration command (MRS) is issued to calibrate RTT and RON |
| 212 | * values for the Process Voltage Temperature (PVT). |
| 213 | */ |
| 214 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_DEEP_CMD, ram_address); |
| 215 | |
| 216 | /* A Normal Mode command is provided. */ |
| 217 | atmel_mpddr_op(mpddr, ATMEL_MPDDRC_MR_MODE_NORMAL_CMD, ram_address); |
| 218 | |
| 219 | /* Perform a write access to any DDR3-SDRAM address. */ |
| 220 | writel(0, ram_address); |
| 221 | |
| 222 | /* |
| 223 | * Write the refresh rate into the COUNT field in the MPDDRC |
| 224 | * Refresh Timer Register (MPDDRC_RTR): |
| 225 | */ |
| 226 | writel(mpddr_value->rtr, &mpddr->rtr); |
| 227 | |
| 228 | return 0; |
| 229 | } |