Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Gateworks Corporation |
| 3 | * Author: Tim Harvey <tharvey@gateworks.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <linux/types.h> |
| 10 | #include <asm/arch/mx6-ddr.h> |
| 11 | #include <asm/arch/sys_proto.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <asm/types.h> |
| 14 | |
| 15 | #if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6Q) || defined(CONFIG_MX6D) |
| 16 | /* Configure MX6DQ mmdc iomux */ |
| 17 | void mx6dq_dram_iocfg(unsigned width, |
| 18 | const struct mx6dq_iomux_ddr_regs *ddr, |
| 19 | const struct mx6dq_iomux_grp_regs *grp) |
| 20 | { |
| 21 | volatile struct mx6dq_iomux_ddr_regs *mx6_ddr_iomux; |
| 22 | volatile struct mx6dq_iomux_grp_regs *mx6_grp_iomux; |
| 23 | |
| 24 | mx6_ddr_iomux = (struct mx6dq_iomux_ddr_regs *)MX6DQ_IOM_DDR_BASE; |
| 25 | mx6_grp_iomux = (struct mx6dq_iomux_grp_regs *)MX6DQ_IOM_GRP_BASE; |
| 26 | |
| 27 | /* DDR IO Type */ |
| 28 | mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type; |
| 29 | mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke; |
| 30 | |
| 31 | /* Clock */ |
| 32 | mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0; |
| 33 | mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1; |
| 34 | |
| 35 | /* Address */ |
| 36 | mx6_ddr_iomux->dram_cas = ddr->dram_cas; |
| 37 | mx6_ddr_iomux->dram_ras = ddr->dram_ras; |
| 38 | mx6_grp_iomux->grp_addds = grp->grp_addds; |
| 39 | |
| 40 | /* Control */ |
| 41 | mx6_ddr_iomux->dram_reset = ddr->dram_reset; |
| 42 | mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0; |
| 43 | mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1; |
| 44 | mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2; |
| 45 | mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0; |
| 46 | mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1; |
| 47 | mx6_grp_iomux->grp_ctlds = grp->grp_ctlds; |
| 48 | |
| 49 | /* Data Strobes */ |
| 50 | mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl; |
| 51 | mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0; |
| 52 | mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1; |
| 53 | if (width >= 32) { |
| 54 | mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2; |
| 55 | mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3; |
| 56 | } |
| 57 | if (width >= 64) { |
| 58 | mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4; |
| 59 | mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5; |
| 60 | mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6; |
| 61 | mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7; |
| 62 | } |
| 63 | |
| 64 | /* Data */ |
| 65 | mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode; |
| 66 | mx6_grp_iomux->grp_b0ds = grp->grp_b0ds; |
| 67 | mx6_grp_iomux->grp_b1ds = grp->grp_b1ds; |
| 68 | if (width >= 32) { |
| 69 | mx6_grp_iomux->grp_b2ds = grp->grp_b2ds; |
| 70 | mx6_grp_iomux->grp_b3ds = grp->grp_b3ds; |
| 71 | } |
| 72 | if (width >= 64) { |
| 73 | mx6_grp_iomux->grp_b4ds = grp->grp_b4ds; |
| 74 | mx6_grp_iomux->grp_b5ds = grp->grp_b5ds; |
| 75 | mx6_grp_iomux->grp_b6ds = grp->grp_b6ds; |
| 76 | mx6_grp_iomux->grp_b7ds = grp->grp_b7ds; |
| 77 | } |
| 78 | mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0; |
| 79 | mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1; |
| 80 | if (width >= 32) { |
| 81 | mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2; |
| 82 | mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3; |
| 83 | } |
| 84 | if (width >= 64) { |
| 85 | mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4; |
| 86 | mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5; |
| 87 | mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6; |
| 88 | mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7; |
| 89 | } |
| 90 | } |
| 91 | #endif |
| 92 | |
| 93 | #if defined(CONFIG_MX6QDL) || defined(CONFIG_MX6DL) || defined(CONFIG_MX6S) |
| 94 | /* Configure MX6SDL mmdc iomux */ |
| 95 | void mx6sdl_dram_iocfg(unsigned width, |
| 96 | const struct mx6sdl_iomux_ddr_regs *ddr, |
| 97 | const struct mx6sdl_iomux_grp_regs *grp) |
| 98 | { |
| 99 | volatile struct mx6sdl_iomux_ddr_regs *mx6_ddr_iomux; |
| 100 | volatile struct mx6sdl_iomux_grp_regs *mx6_grp_iomux; |
| 101 | |
| 102 | mx6_ddr_iomux = (struct mx6sdl_iomux_ddr_regs *)MX6SDL_IOM_DDR_BASE; |
| 103 | mx6_grp_iomux = (struct mx6sdl_iomux_grp_regs *)MX6SDL_IOM_GRP_BASE; |
| 104 | |
| 105 | /* DDR IO Type */ |
| 106 | mx6_grp_iomux->grp_ddr_type = grp->grp_ddr_type; |
| 107 | mx6_grp_iomux->grp_ddrpke = grp->grp_ddrpke; |
| 108 | |
| 109 | /* Clock */ |
| 110 | mx6_ddr_iomux->dram_sdclk_0 = ddr->dram_sdclk_0; |
| 111 | mx6_ddr_iomux->dram_sdclk_1 = ddr->dram_sdclk_1; |
| 112 | |
| 113 | /* Address */ |
| 114 | mx6_ddr_iomux->dram_cas = ddr->dram_cas; |
| 115 | mx6_ddr_iomux->dram_ras = ddr->dram_ras; |
| 116 | mx6_grp_iomux->grp_addds = grp->grp_addds; |
| 117 | |
| 118 | /* Control */ |
| 119 | mx6_ddr_iomux->dram_reset = ddr->dram_reset; |
| 120 | mx6_ddr_iomux->dram_sdcke0 = ddr->dram_sdcke0; |
| 121 | mx6_ddr_iomux->dram_sdcke1 = ddr->dram_sdcke1; |
| 122 | mx6_ddr_iomux->dram_sdba2 = ddr->dram_sdba2; |
| 123 | mx6_ddr_iomux->dram_sdodt0 = ddr->dram_sdodt0; |
| 124 | mx6_ddr_iomux->dram_sdodt1 = ddr->dram_sdodt1; |
| 125 | mx6_grp_iomux->grp_ctlds = grp->grp_ctlds; |
| 126 | |
| 127 | /* Data Strobes */ |
| 128 | mx6_grp_iomux->grp_ddrmode_ctl = grp->grp_ddrmode_ctl; |
| 129 | mx6_ddr_iomux->dram_sdqs0 = ddr->dram_sdqs0; |
| 130 | mx6_ddr_iomux->dram_sdqs1 = ddr->dram_sdqs1; |
| 131 | if (width >= 32) { |
| 132 | mx6_ddr_iomux->dram_sdqs2 = ddr->dram_sdqs2; |
| 133 | mx6_ddr_iomux->dram_sdqs3 = ddr->dram_sdqs3; |
| 134 | } |
| 135 | if (width >= 64) { |
| 136 | mx6_ddr_iomux->dram_sdqs4 = ddr->dram_sdqs4; |
| 137 | mx6_ddr_iomux->dram_sdqs5 = ddr->dram_sdqs5; |
| 138 | mx6_ddr_iomux->dram_sdqs6 = ddr->dram_sdqs6; |
| 139 | mx6_ddr_iomux->dram_sdqs7 = ddr->dram_sdqs7; |
| 140 | } |
| 141 | |
| 142 | /* Data */ |
| 143 | mx6_grp_iomux->grp_ddrmode = grp->grp_ddrmode; |
| 144 | mx6_grp_iomux->grp_b0ds = grp->grp_b0ds; |
| 145 | mx6_grp_iomux->grp_b1ds = grp->grp_b1ds; |
| 146 | if (width >= 32) { |
| 147 | mx6_grp_iomux->grp_b2ds = grp->grp_b2ds; |
| 148 | mx6_grp_iomux->grp_b3ds = grp->grp_b3ds; |
| 149 | } |
| 150 | if (width >= 64) { |
| 151 | mx6_grp_iomux->grp_b4ds = grp->grp_b4ds; |
| 152 | mx6_grp_iomux->grp_b5ds = grp->grp_b5ds; |
| 153 | mx6_grp_iomux->grp_b6ds = grp->grp_b6ds; |
| 154 | mx6_grp_iomux->grp_b7ds = grp->grp_b7ds; |
| 155 | } |
| 156 | mx6_ddr_iomux->dram_dqm0 = ddr->dram_dqm0; |
| 157 | mx6_ddr_iomux->dram_dqm1 = ddr->dram_dqm1; |
| 158 | if (width >= 32) { |
| 159 | mx6_ddr_iomux->dram_dqm2 = ddr->dram_dqm2; |
| 160 | mx6_ddr_iomux->dram_dqm3 = ddr->dram_dqm3; |
| 161 | } |
| 162 | if (width >= 64) { |
| 163 | mx6_ddr_iomux->dram_dqm4 = ddr->dram_dqm4; |
| 164 | mx6_ddr_iomux->dram_dqm5 = ddr->dram_dqm5; |
| 165 | mx6_ddr_iomux->dram_dqm6 = ddr->dram_dqm6; |
| 166 | mx6_ddr_iomux->dram_dqm7 = ddr->dram_dqm7; |
| 167 | } |
| 168 | } |
| 169 | #endif |
| 170 | |
| 171 | /* |
| 172 | * Configure mx6 mmdc registers based on: |
| 173 | * - board-specific memory configuration |
| 174 | * - board-specific calibration data |
| 175 | * - ddr3 chip details |
| 176 | * |
| 177 | * The various calculations here are derived from the Freescale |
| 178 | * i.Mx6DQSDL DDR3 Script Aid spreadsheet (DOC-94917) designed to generate MMDC |
| 179 | * configuration registers based on memory system and memory chip parameters. |
| 180 | * |
| 181 | * The defaults here are those which were specified in the spreadsheet. |
| 182 | * For details on each register, refer to the IMX6DQRM and/or IMX6SDLRM |
| 183 | * section titled MMDC initialization |
| 184 | */ |
| 185 | #define MR(val, ba, cmd, cs1) \ |
| 186 | ((val << 16) | (1 << 15) | (cmd << 4) | (cs1 << 3) | ba) |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 187 | void mx6_dram_cfg(const struct mx6_ddr_sysinfo *sysinfo, |
| 188 | const struct mx6_mmdc_calibration *calib, |
| 189 | const struct mx6_ddr3_cfg *ddr3_cfg) |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 190 | { |
| 191 | volatile struct mmdc_p_regs *mmdc0; |
| 192 | volatile struct mmdc_p_regs *mmdc1; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 193 | u32 val; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 194 | u8 tcke, tcksrx, tcksre, txpdll, taofpd, taonpd, trrd; |
| 195 | u8 todtlon, taxpd, tanpd, tcwl, txp, tfaw, tcl; |
| 196 | u8 todt_idle_off = 0x4; /* from DDR3 Script Aid spreadsheet */ |
| 197 | u16 trcd, trc, tras, twr, tmrd, trtp, trp, twtr, trfc, txs, txpr; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 198 | u16 cs0_end; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 199 | u16 tdllk = 0x1ff; /* DLL locking time: 512 cycles (JEDEC DDR3) */ |
Marek Vasut | 4a46360 | 2014-08-04 01:47:10 +0200 | [diff] [blame] | 200 | u8 coladdr; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 201 | int clkper; /* clock period in picoseconds */ |
| 202 | int clock; /* clock freq in mHz */ |
| 203 | int cs; |
| 204 | |
| 205 | mmdc0 = (struct mmdc_p_regs *)MMDC_P0_BASE_ADDR; |
| 206 | mmdc1 = (struct mmdc_p_regs *)MMDC_P1_BASE_ADDR; |
| 207 | |
| 208 | /* MX6D/MX6Q: 1066 MHz memory clock, clkper = 1.894ns = 1894ps */ |
| 209 | if (is_cpu_type(MXC_CPU_MX6Q) || is_cpu_type(MXC_CPU_MX6D)) { |
| 210 | clock = 528; |
| 211 | tcwl = 4; |
| 212 | } |
| 213 | /* MX6S/MX6DL: 800 MHz memory clock, clkper = 2.5ns = 2500ps */ |
| 214 | else { |
| 215 | clock = 400; |
| 216 | tcwl = 3; |
| 217 | } |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 218 | clkper = (1000 * 1000) / clock; /* pico seconds */ |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 219 | todtlon = tcwl; |
| 220 | taxpd = tcwl; |
| 221 | tanpd = tcwl; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 222 | |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 223 | switch (ddr3_cfg->density) { |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 224 | case 1: /* 1Gb per chip */ |
| 225 | trfc = DIV_ROUND_UP(110000, clkper) - 1; |
| 226 | txs = DIV_ROUND_UP(120000, clkper) - 1; |
| 227 | break; |
| 228 | case 2: /* 2Gb per chip */ |
| 229 | trfc = DIV_ROUND_UP(160000, clkper) - 1; |
| 230 | txs = DIV_ROUND_UP(170000, clkper) - 1; |
| 231 | break; |
| 232 | case 4: /* 4Gb per chip */ |
| 233 | trfc = DIV_ROUND_UP(260000, clkper) - 1; |
| 234 | txs = DIV_ROUND_UP(270000, clkper) - 1; |
| 235 | break; |
| 236 | case 8: /* 8Gb per chip */ |
| 237 | trfc = DIV_ROUND_UP(350000, clkper) - 1; |
| 238 | txs = DIV_ROUND_UP(360000, clkper) - 1; |
| 239 | break; |
| 240 | default: |
| 241 | /* invalid density */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 242 | puts("invalid chip density\n"); |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 243 | hang(); |
| 244 | break; |
| 245 | } |
| 246 | txpr = txs; |
| 247 | |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 248 | switch (ddr3_cfg->mem_speed) { |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 249 | case 800: |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 250 | txp = DIV_ROUND_UP(MAX(3 * clkper, 7500), clkper) - 1; |
| 251 | tcke = DIV_ROUND_UP(MAX(3 * clkper, 7500), clkper) - 1; |
| 252 | if (ddr3_cfg->pagesz == 1) { |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 253 | tfaw = DIV_ROUND_UP(40000, clkper) - 1; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 254 | trrd = DIV_ROUND_UP(MAX(4 * clkper, 10000), clkper) - 1; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 255 | } else { |
| 256 | tfaw = DIV_ROUND_UP(50000, clkper) - 1; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 257 | trrd = DIV_ROUND_UP(MAX(4 * clkper, 10000), clkper) - 1; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 258 | } |
| 259 | break; |
| 260 | case 1066: |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 261 | txp = DIV_ROUND_UP(MAX(3 * clkper, 7500), clkper) - 1; |
| 262 | tcke = DIV_ROUND_UP(MAX(3 * clkper, 5625), clkper) - 1; |
| 263 | if (ddr3_cfg->pagesz == 1) { |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 264 | tfaw = DIV_ROUND_UP(37500, clkper) - 1; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 265 | trrd = DIV_ROUND_UP(MAX(4 * clkper, 7500), clkper) - 1; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 266 | } else { |
| 267 | tfaw = DIV_ROUND_UP(50000, clkper) - 1; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 268 | trrd = DIV_ROUND_UP(MAX(4 * clkper, 10000), clkper) - 1; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 269 | } |
| 270 | break; |
| 271 | case 1333: |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 272 | txp = DIV_ROUND_UP(MAX(3 * clkper, 6000), clkper) - 1; |
| 273 | tcke = DIV_ROUND_UP(MAX(3 * clkper, 5625), clkper) - 1; |
| 274 | if (ddr3_cfg->pagesz == 1) { |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 275 | tfaw = DIV_ROUND_UP(30000, clkper) - 1; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 276 | trrd = DIV_ROUND_UP(MAX(4 * clkper, 6000), clkper) - 1; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 277 | } else { |
| 278 | tfaw = DIV_ROUND_UP(45000, clkper) - 1; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 279 | trrd = DIV_ROUND_UP(MAX(4 * clkper, 7500), clkper) - 1; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 280 | } |
| 281 | break; |
| 282 | case 1600: |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 283 | txp = DIV_ROUND_UP(MAX(3 * clkper, 6000), clkper) - 1; |
| 284 | tcke = DIV_ROUND_UP(MAX(3 * clkper, 5000), clkper) - 1; |
| 285 | if (ddr3_cfg->pagesz == 1) { |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 286 | tfaw = DIV_ROUND_UP(30000, clkper) - 1; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 287 | trrd = DIV_ROUND_UP(MAX(4 * clkper, 6000), clkper) - 1; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 288 | } else { |
| 289 | tfaw = DIV_ROUND_UP(40000, clkper) - 1; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 290 | trrd = DIV_ROUND_UP(MAX(4 * clkper, 7500), clkper) - 1; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 291 | } |
| 292 | break; |
| 293 | default: |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 294 | puts("invalid memory speed\n"); |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 295 | hang(); |
| 296 | break; |
| 297 | } |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 298 | txpdll = DIV_ROUND_UP(MAX(10 * clkper, 24000), clkper) - 1; |
| 299 | tcksre = DIV_ROUND_UP(MAX(5 * clkper, 10000), clkper); |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 300 | taonpd = DIV_ROUND_UP(2000, clkper) - 1; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 301 | tcksrx = tcksre; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 302 | taofpd = taonpd; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 303 | twr = DIV_ROUND_UP(15000, clkper) - 1; |
| 304 | tmrd = DIV_ROUND_UP(MAX(12 * clkper, 15000), clkper) - 1; |
| 305 | trc = DIV_ROUND_UP(ddr3_cfg->trcmin, clkper / 10) - 1; |
| 306 | tras = DIV_ROUND_UP(ddr3_cfg->trasmin, clkper / 10) - 1; |
| 307 | tcl = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 3; |
| 308 | trp = DIV_ROUND_UP(ddr3_cfg->trcd, clkper / 10) - 1; |
| 309 | twtr = ROUND(MAX(4 * clkper, 7500) / clkper, 1) - 1; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 310 | trcd = trp; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 311 | trtp = twtr; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 312 | cs0_end = (4 * sysinfo->cs_density <= 120) ? |
| 313 | 4 * sysinfo->cs_density + 7 : |
| 314 | 127; |
| 315 | |
| 316 | debug("density:%d Gb (%d Gb per chip)\n", |
| 317 | sysinfo->cs_density, ddr3_cfg->density); |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 318 | debug("clock: %dMHz (%d ps)\n", clock, clkper); |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 319 | debug("memspd:%d\n", ddr3_cfg->mem_speed); |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 320 | debug("tcke=%d\n", tcke); |
| 321 | debug("tcksrx=%d\n", tcksrx); |
| 322 | debug("tcksre=%d\n", tcksre); |
| 323 | debug("taofpd=%d\n", taofpd); |
| 324 | debug("taonpd=%d\n", taonpd); |
| 325 | debug("todtlon=%d\n", todtlon); |
| 326 | debug("tanpd=%d\n", tanpd); |
| 327 | debug("taxpd=%d\n", taxpd); |
| 328 | debug("trfc=%d\n", trfc); |
| 329 | debug("txs=%d\n", txs); |
| 330 | debug("txp=%d\n", txp); |
| 331 | debug("txpdll=%d\n", txpdll); |
| 332 | debug("tfaw=%d\n", tfaw); |
| 333 | debug("tcl=%d\n", tcl); |
| 334 | debug("trcd=%d\n", trcd); |
| 335 | debug("trp=%d\n", trp); |
| 336 | debug("trc=%d\n", trc); |
| 337 | debug("tras=%d\n", tras); |
| 338 | debug("twr=%d\n", twr); |
| 339 | debug("tmrd=%d\n", tmrd); |
| 340 | debug("tcwl=%d\n", tcwl); |
| 341 | debug("tdllk=%d\n", tdllk); |
| 342 | debug("trtp=%d\n", trtp); |
| 343 | debug("twtr=%d\n", twtr); |
| 344 | debug("trrd=%d\n", trrd); |
| 345 | debug("txpr=%d\n", txpr); |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 346 | debug("cs0_end=%d\n", cs0_end); |
| 347 | debug("ncs=%d\n", sysinfo->ncs); |
| 348 | debug("Rtt_wr=%d\n", sysinfo->rtt_wr); |
| 349 | debug("Rtt_nom=%d\n", sysinfo->rtt_nom); |
| 350 | debug("SRT=%d\n", ddr3_cfg->SRT); |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 351 | debug("tcl=%d\n", tcl); |
| 352 | debug("twr=%d\n", twr); |
| 353 | |
| 354 | /* |
| 355 | * board-specific configuration: |
| 356 | * These values are determined empirically and vary per board layout |
| 357 | * see: |
| 358 | * appnote, ddr3 spreadsheet |
| 359 | */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 360 | mmdc0->mpwldectrl0 = calib->p0_mpwldectrl0; |
| 361 | mmdc0->mpwldectrl1 = calib->p0_mpwldectrl1; |
| 362 | mmdc0->mpdgctrl0 = calib->p0_mpdgctrl0; |
| 363 | mmdc0->mpdgctrl1 = calib->p0_mpdgctrl1; |
| 364 | mmdc0->mprddlctl = calib->p0_mprddlctl; |
| 365 | mmdc0->mpwrdlctl = calib->p0_mpwrdlctl; |
| 366 | if (sysinfo->dsize > 1) { |
| 367 | mmdc1->mpwldectrl0 = calib->p1_mpwldectrl0; |
| 368 | mmdc1->mpwldectrl1 = calib->p1_mpwldectrl1; |
| 369 | mmdc1->mpdgctrl0 = calib->p1_mpdgctrl0; |
| 370 | mmdc1->mpdgctrl1 = calib->p1_mpdgctrl1; |
| 371 | mmdc1->mprddlctl = calib->p1_mprddlctl; |
| 372 | mmdc1->mpwrdlctl = calib->p1_mpwrdlctl; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* Read data DQ Byte0-3 delay */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 376 | mmdc0->mprddqby0dl = 0x33333333; |
| 377 | mmdc0->mprddqby1dl = 0x33333333; |
| 378 | if (sysinfo->dsize > 0) { |
| 379 | mmdc0->mprddqby2dl = 0x33333333; |
| 380 | mmdc0->mprddqby3dl = 0x33333333; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 381 | } |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 382 | |
| 383 | if (sysinfo->dsize > 1) { |
| 384 | mmdc1->mprddqby0dl = 0x33333333; |
| 385 | mmdc1->mprddqby1dl = 0x33333333; |
| 386 | mmdc1->mprddqby2dl = 0x33333333; |
| 387 | mmdc1->mprddqby3dl = 0x33333333; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | /* MMDC Termination: rtt_nom:2 RZQ/2(120ohm), rtt_nom:1 RZQ/4(60ohm) */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 391 | val = (sysinfo->rtt_nom == 2) ? 0x00011117 : 0x00022227; |
| 392 | mmdc0->mpodtctrl = val; |
| 393 | if (sysinfo->dsize > 1) |
| 394 | mmdc1->mpodtctrl = val; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 395 | |
| 396 | /* complete calibration */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 397 | val = (1 << 11); /* Force measurement on delay-lines */ |
| 398 | mmdc0->mpmur0 = val; |
| 399 | if (sysinfo->dsize > 1) |
| 400 | mmdc1->mpmur0 = val; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 401 | |
| 402 | /* Step 1: configuration request */ |
| 403 | mmdc0->mdscr = (u32)(1 << 15); /* config request */ |
| 404 | |
| 405 | /* Step 2: Timing configuration */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 406 | mmdc0->mdcfg0 = (trfc << 24) | (txs << 16) | (txp << 13) | |
| 407 | (txpdll << 9) | (tfaw << 4) | tcl; |
| 408 | mmdc0->mdcfg1 = (trcd << 29) | (trp << 26) | (trc << 21) | |
| 409 | (tras << 16) | (1 << 15) /* trpa */ | |
| 410 | (twr << 9) | (tmrd << 5) | tcwl; |
| 411 | mmdc0->mdcfg2 = (tdllk << 16) | (trtp << 6) | (twtr << 3) | trrd; |
| 412 | mmdc0->mdotc = (taofpd << 27) | (taonpd << 24) | (tanpd << 20) | |
| 413 | (taxpd << 16) | (todtlon << 12) | (todt_idle_off << 4); |
| 414 | mmdc0->mdasp = cs0_end; /* CS addressing */ |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 415 | |
| 416 | /* Step 3: Configure DDR type */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 417 | mmdc0->mdmisc = (sysinfo->cs1_mirror << 19) | (sysinfo->walat << 16) | |
| 418 | (sysinfo->bi_on << 12) | (sysinfo->mif3_mode << 9) | |
| 419 | (sysinfo->ralat << 6); |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 420 | |
| 421 | /* Step 4: Configure delay while leaving reset */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 422 | mmdc0->mdor = (txpr << 16) | (sysinfo->sde_to_rst << 8) | |
| 423 | (sysinfo->rst_to_cke << 0); |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 424 | |
| 425 | /* Step 5: Configure DDR physical parameters (density and burst len) */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 426 | coladdr = ddr3_cfg->coladdr; |
| 427 | if (ddr3_cfg->coladdr == 8) /* 8-bit COL is 0x3 */ |
Marek Vasut | 4a46360 | 2014-08-04 01:47:10 +0200 | [diff] [blame] | 428 | coladdr += 4; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 429 | else if (ddr3_cfg->coladdr == 12) /* 12-bit COL is 0x4 */ |
Marek Vasut | 4a46360 | 2014-08-04 01:47:10 +0200 | [diff] [blame] | 430 | coladdr += 1; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 431 | mmdc0->mdctl = (ddr3_cfg->rowaddr - 11) << 24 | /* ROW */ |
| 432 | (coladdr - 9) << 20 | /* COL */ |
| 433 | (1 << 19) | /* Burst Length = 8 for DDR3 */ |
| 434 | (sysinfo->dsize << 16); /* DDR data bus size */ |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 435 | |
| 436 | /* Step 6: Perform ZQ calibration */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 437 | val = 0xa1390001; /* one-time HW ZQ calib */ |
| 438 | mmdc0->mpzqhwctrl = val; |
| 439 | if (sysinfo->dsize > 1) |
| 440 | mmdc1->mpzqhwctrl = val; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 441 | |
| 442 | /* Step 7: Enable MMDC with desired chip select */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 443 | mmdc0->mdctl |= (1 << 31) | /* SDE_0 for CS0 */ |
| 444 | ((sysinfo->ncs == 2) ? 1 : 0) << 30; /* SDE_1 for CS1 */ |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 445 | |
| 446 | /* Step 8: Write Mode Registers to Init DDR3 devices */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 447 | for (cs = 0; cs < sysinfo->ncs; cs++) { |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 448 | /* MR2 */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 449 | val = (sysinfo->rtt_wr & 3) << 9 | (ddr3_cfg->SRT & 1) << 7 | |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 450 | ((tcwl - 3) & 3) << 3; |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 451 | mmdc0->mdscr = MR(val, 2, 3, cs); |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 452 | /* MR3 */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 453 | mmdc0->mdscr = MR(0, 3, 3, cs); |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 454 | /* MR1 */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 455 | val = ((sysinfo->rtt_nom & 1) ? 1 : 0) << 2 | |
| 456 | ((sysinfo->rtt_nom & 2) ? 1 : 0) << 6; |
| 457 | mmdc0->mdscr = MR(val, 1, 3, cs); |
| 458 | /* MR0 */ |
| 459 | val = ((tcl - 1) << 4) | /* CAS */ |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 460 | (1 << 8) | /* DLL Reset */ |
| 461 | ((twr - 3) << 9); /* Write Recovery */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 462 | mmdc0->mdscr = MR(val, 0, 3, cs); |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 463 | /* ZQ calibration */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 464 | val = (1 << 10); |
| 465 | mmdc0->mdscr = MR(val, 0, 4, cs); |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /* Step 10: Power down control and self-refresh */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 469 | mmdc0->mdpdc = (tcke & 0x7) << 16 | |
| 470 | 5 << 12 | /* PWDT_1: 256 cycles */ |
| 471 | 5 << 8 | /* PWDT_0: 256 cycles */ |
| 472 | 1 << 6 | /* BOTH_CS_PD */ |
| 473 | (tcksrx & 0x7) << 3 | |
| 474 | (tcksre & 0x7); |
| 475 | mmdc0->mapsr = 0x00011006; /* ADOPT power down enabled */ |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 476 | |
| 477 | /* Step 11: Configure ZQ calibration: one-time and periodic 1ms */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 478 | val = 0xa1390003; |
| 479 | mmdc0->mpzqhwctrl = val; |
| 480 | if (sysinfo->dsize > 1) |
| 481 | mmdc1->mpzqhwctrl = val; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 482 | |
| 483 | /* Step 12: Configure and activate periodic refresh */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 484 | mmdc0->mdref = (1 << 14) | /* REF_SEL: Periodic refresh cycle: 32kHz */ |
| 485 | (7 << 11); /* REFR: Refresh Rate - 8 refreshes */ |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 486 | |
| 487 | /* Step 13: Deassert config request - init complete */ |
Nikita Kiryanov | c475346 | 2014-09-07 18:58:11 +0300 | [diff] [blame^] | 488 | mmdc0->mdscr = 0x00000000; |
Tim Harvey | 8ab871b | 2014-06-02 16:13:23 -0700 | [diff] [blame] | 489 | |
| 490 | /* wait for auto-ZQ calibration to complete */ |
| 491 | mdelay(1); |
| 492 | } |