Andrew Wu | 0651072 | 2013-06-21 21:37:05 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2013 DMP Electronics Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | static u16 get_mask(u16 bit_width, u16 bit_offset) |
| 21 | { |
| 22 | u16 mask = (((1 << bit_width) - 1) << bit_offset); |
| 23 | return mask; |
| 24 | } |
| 25 | |
| 26 | static u16 set_bitfield(u16 val, u16 bits, u16 bit_width, u16 bit_offset) |
| 27 | { |
| 28 | u16 mask = get_mask(bit_width, bit_offset); |
| 29 | val = (val & ~mask) | (bits << bit_offset); |
| 30 | return val; |
| 31 | } |
| 32 | |
| 33 | static u16 get_bitfield(u16 val, u16 bit_width, u16 bit_offset) |
| 34 | { |
| 35 | u16 mask = get_mask(bit_width, bit_offset); |
| 36 | return (val & mask) >> bit_offset; |
| 37 | } |
| 38 | |
| 39 | static u8 check_address_bit(int addr_bit) |
| 40 | { |
| 41 | u16 dummy; |
| 42 | *(volatile u16 *)(0) = 0; |
| 43 | dummy = *(volatile u16 *)(0); // read push write |
| 44 | *(volatile u16 *)(1 << addr_bit) = 0x5a5a; |
| 45 | dummy = *(volatile u16 *)(1 << addr_bit); // read push write |
| 46 | if ((*(volatile u16 *)(0)) != 0) |
| 47 | return 0; // address bit wrapped. |
| 48 | return 1; // address bit not wrapped. |
| 49 | } |
| 50 | |
| 51 | static u8 check_dram_side(int addr_bit) |
| 52 | { |
| 53 | *(volatile u16 *)(1 << addr_bit) = 0x5a5a; |
| 54 | *(volatile u16 *)(0) = 0; |
| 55 | if ((*(volatile u16 *)(1 << addr_bit)) != 0x5a5a) |
| 56 | return 0; // DRAM only one side. |
| 57 | return 1; // two sides. |
| 58 | } |
| 59 | |
| 60 | // DDRIII memory bank register control: |
| 61 | // bit : |
| 62 | // 2 - 0 : DRAMC_COLSIZE : DDRIII Column Address Type : 0 0 0 = 10bit |
| 63 | // : 0 0 1 = 11bit |
| 64 | // 7 - 5 : DRAMC_ROWSIZE : DDRIII Row Address Type : 0 0 0 = 13bit |
| 65 | // : 0 0 1 = 14bit |
| 66 | // : 0 1 0 = 15bit |
| 67 | // : 0 1 1 = 16bit |
| 68 | // 11 - 8 : DRAM_SIZE : DDRIII Size : 0 1 0 1 = 64M |
| 69 | // : 0 1 1 0 = 128M |
| 70 | // : 0 1 1 1 = 256M |
| 71 | // : 1 0 0 0 = 512M |
| 72 | // : 1 0 0 1 = 1GB |
| 73 | // : 1 0 1 0 = 2GB |
| 74 | // 13 : DRAMC_CSMASK : DDRIII CS#[1] Mask : 1 = Mask CS1 enable |
| 75 | |
| 76 | #define DDR3_COL_10BIT 0 |
| 77 | #define DDR3_COL_11BIT 1 |
| 78 | #define DDR3_ROW_13BIT 0 |
| 79 | #define DDR3_ROW_14BIT 1 |
| 80 | #define DDR3_ROW_15BIT 2 |
| 81 | #define DDR3_ROW_16BIT 3 |
| 82 | #define DDR3_SIZE_64M 5 |
| 83 | #define DDR3_SIZE_128M 6 |
| 84 | #define DDR3_SIZE_256M 7 |
| 85 | #define DDR3_SIZE_512M 8 |
| 86 | #define DDR3_SIZE_1GB 9 |
| 87 | #define DDR3_SIZE_2GB 10 |
| 88 | #define DDR3_C1M_ACTIVE 0 |
| 89 | #define DDR3_C1M_MASK 1 |
| 90 | |
| 91 | static u16 set_ddr3_mem_reg_col(u16 reg, u16 col) |
| 92 | { |
| 93 | return set_bitfield(reg, col, 3, 0); |
| 94 | } |
| 95 | |
| 96 | static u16 get_ddr3_mem_reg_col(u16 reg) |
| 97 | { |
| 98 | return get_bitfield(reg, 3, 0); |
| 99 | } |
| 100 | |
| 101 | static u16 set_ddr3_mem_reg_row(u16 reg, u16 row) |
| 102 | { |
| 103 | return set_bitfield(reg, row, 3, 5); |
| 104 | } |
| 105 | |
| 106 | static u16 get_ddr3_mem_reg_row(u16 reg) |
| 107 | { |
| 108 | return get_bitfield(reg, 3, 5); |
| 109 | } |
| 110 | |
| 111 | static u16 set_ddr3_mem_reg_size(u16 reg, u16 size) |
| 112 | { |
| 113 | return set_bitfield(reg, size, 4, 8); |
| 114 | } |
| 115 | |
| 116 | static u16 get_ddr3_mem_reg_size(u16 reg) |
| 117 | { |
| 118 | return get_bitfield(reg, 4, 8); |
| 119 | } |
| 120 | |
| 121 | static u16 set_ddr3_mem_reg_c1m(u16 reg, u16 c1m) |
| 122 | { |
| 123 | return set_bitfield(reg, c1m, 1, 13); |
| 124 | } |
| 125 | |
| 126 | static u16 get_ddr3_mem_reg_c1m(u16 reg) |
| 127 | { |
| 128 | return get_bitfield(reg, 1, 13); |
| 129 | } |
| 130 | |
| 131 | static u16 auto_set_ddr3_mem_reg_size(u16 reg) |
| 132 | { |
| 133 | u8 ss = 0; |
| 134 | // If reg is the minimum DRAM size, |
| 135 | // SS is also the minimum size 128M. |
| 136 | // If size in reg is bigger, SS is also bigger. |
| 137 | ss += get_ddr3_mem_reg_col(reg); |
| 138 | ss += get_ddr3_mem_reg_row(reg); |
| 139 | ss += (1 - get_ddr3_mem_reg_c1m(reg)); |
| 140 | ss += DDR3_SIZE_128M; |
| 141 | return set_ddr3_mem_reg_size(reg, ss); |
| 142 | } |
| 143 | |
| 144 | static u16 get_ddr3_mem_reg(u16 col, u16 row, u16 c1m) |
| 145 | { |
| 146 | u16 reg; |
| 147 | reg = 0; |
| 148 | reg = set_ddr3_mem_reg_col(reg, col); |
| 149 | reg = set_ddr3_mem_reg_row(reg, row); |
| 150 | reg = set_ddr3_mem_reg_c1m(reg, c1m); |
| 151 | reg = auto_set_ddr3_mem_reg_size(reg); |
| 152 | return reg; |
| 153 | } |
| 154 | |
| 155 | static void ddr3_phy_reset(void) |
| 156 | { |
| 157 | // PCI N/B reg FAh bit 6 = RST_DRAM_PHY. |
| 158 | pci_write_config8(NB1, NB1_REG_RESET_DRAMC_PHY, 0x40); |
| 159 | while ((pci_read_config8(NB1, NB1_REG_RESET_DRAMC_PHY) & 0x40) == 0x40) { |
| 160 | } |
| 161 | // reload mode. |
| 162 | u32 ddr3_cfg = pci_read_config32(NB, NB_REG_DDR3_CFG); |
| 163 | pci_write_config32(NB, NB_REG_DDR3_CFG, ddr3_cfg); |
| 164 | } |
| 165 | |
| 166 | static u8 detect_ddr3_dram_cs(u16 reg, u8 base_addr_bit) |
| 167 | { |
| 168 | reg = set_ddr3_mem_reg_c1m(reg, DDR3_C1M_ACTIVE); |
| 169 | reg = auto_set_ddr3_mem_reg_size(reg); |
| 170 | pci_write_config16(NB, NB_REG_MBR, reg); |
| 171 | if (check_dram_side(base_addr_bit + 1)) { |
| 172 | base_addr_bit += 1; |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | reg = set_ddr3_mem_reg_c1m(reg, DDR3_C1M_MASK); |
| 177 | reg = auto_set_ddr3_mem_reg_size(reg); |
| 178 | pci_write_config16(NB, NB_REG_MBR, reg); |
| 179 | // no need to check CS = 0. |
| 180 | // Need to reset DDR3 PHY. |
| 181 | ddr3_phy_reset(); |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static u8 detect_ddr3_dram_row(u16 reg, u8 base_addr_bit) |
| 186 | { |
| 187 | reg = set_ddr3_mem_reg_row(reg, DDR3_ROW_16BIT); |
| 188 | reg = auto_set_ddr3_mem_reg_size(reg); |
| 189 | pci_write_config16(NB, NB_REG_MBR, reg); |
| 190 | if (check_address_bit(base_addr_bit + 16)) { |
| 191 | base_addr_bit += 16; |
| 192 | return detect_ddr3_dram_cs(reg, base_addr_bit); |
| 193 | } |
| 194 | |
| 195 | reg = set_ddr3_mem_reg_row(reg, DDR3_ROW_15BIT); |
| 196 | reg = auto_set_ddr3_mem_reg_size(reg); |
| 197 | pci_write_config16(NB, NB_REG_MBR, reg); |
| 198 | if (check_address_bit(base_addr_bit + 15)) { |
| 199 | base_addr_bit += 15; |
| 200 | return detect_ddr3_dram_cs(reg, base_addr_bit); |
| 201 | } |
| 202 | |
| 203 | reg = set_ddr3_mem_reg_row(reg, DDR3_ROW_14BIT); |
| 204 | reg = auto_set_ddr3_mem_reg_size(reg); |
| 205 | pci_write_config16(NB, NB_REG_MBR, reg); |
| 206 | if (check_address_bit(base_addr_bit + 14)) { |
| 207 | base_addr_bit += 14; |
| 208 | return detect_ddr3_dram_cs(reg, base_addr_bit); |
| 209 | } |
| 210 | |
| 211 | reg = set_ddr3_mem_reg_row(reg, DDR3_ROW_13BIT); |
| 212 | reg = auto_set_ddr3_mem_reg_size(reg); |
| 213 | pci_write_config16(NB, NB_REG_MBR, reg); |
| 214 | if (check_address_bit(base_addr_bit + 13)) { |
| 215 | base_addr_bit += 13; |
| 216 | return detect_ddr3_dram_cs(reg, base_addr_bit); |
| 217 | } |
| 218 | // row test error. |
| 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | static u8 detect_ddr3_dram_bank(u16 reg, u8 base_addr_bit) |
| 223 | { |
| 224 | /* DDR3 is always 3 bank bits */ |
| 225 | base_addr_bit += 3; |
| 226 | return detect_ddr3_dram_row(reg, base_addr_bit); |
| 227 | } |
| 228 | |
| 229 | static u8 detect_ddr3_dram_col(u16 reg, u8 base_addr_bit) |
| 230 | { |
| 231 | reg = set_ddr3_mem_reg_col(reg, DDR3_COL_11BIT); |
| 232 | reg = auto_set_ddr3_mem_reg_size(reg); |
| 233 | pci_write_config16(NB, NB_REG_MBR, reg); |
| 234 | if (check_address_bit(base_addr_bit + 11)) { |
| 235 | base_addr_bit += 11; |
| 236 | return detect_ddr3_dram_bank(reg, base_addr_bit); |
| 237 | } |
| 238 | |
| 239 | reg = set_ddr3_mem_reg_col(reg, DDR3_COL_10BIT); |
| 240 | reg = auto_set_ddr3_mem_reg_size(reg); |
| 241 | pci_write_config16(NB, NB_REG_MBR, reg); |
| 242 | if (check_address_bit(base_addr_bit + 10)) { |
| 243 | base_addr_bit += 10; |
| 244 | return detect_ddr3_dram_bank(reg, base_addr_bit); |
| 245 | } |
| 246 | // col test error. |
| 247 | return 1; |
| 248 | } |
| 249 | |
| 250 | static u8 detect_ddr3_dram_size(void) |
| 251 | { |
| 252 | u16 reg; |
| 253 | u8 base_addr_bit = 0; |
| 254 | reg = get_ddr3_mem_reg(DDR3_COL_10BIT, DDR3_ROW_13BIT, DDR3_C1M_MASK); |
| 255 | return detect_ddr3_dram_col(reg, base_addr_bit); |
| 256 | } |
| 257 | |
| 258 | static void print_ddr3_memory_setup(void) |
| 259 | { |
| 260 | #if CONFIG_DEBUG_RAM_SETUP |
| 261 | print_debug("DDR3 Timing Reg 0-3:\n"); |
| 262 | print_debug("NB 6e : "); |
| 263 | print_debug_hex16(pci_read_config16(NB, 0x6e)); |
| 264 | print_debug("\nNB 74 : "); |
| 265 | print_debug_hex32(pci_read_config32(NB, 0x74)); |
| 266 | print_debug("\nNB 78 : "); |
| 267 | print_debug_hex32(pci_read_config32(NB, 0x78)); |
| 268 | print_debug("\nNB 7c : "); |
| 269 | print_debug_hex32(pci_read_config32(NB, 0x7c)); |
| 270 | u16 mbr = pci_read_config16(NB, 0x6c); |
| 271 | print_debug("\nNB 6c(MBR) : "); |
| 272 | print_debug_hex16(mbr); |
| 273 | const char *s; |
| 274 | u8 col = get_ddr3_mem_reg_col(mbr); |
| 275 | if (col == DDR3_COL_10BIT) |
| 276 | s = " (COL=10"; |
| 277 | else |
| 278 | s = " (COL=11"; |
| 279 | print_debug(s); |
| 280 | u8 row = get_ddr3_mem_reg_row(mbr); |
| 281 | switch (row) { |
| 282 | case DDR3_ROW_13BIT: |
| 283 | s = ", ROW = 13"; |
| 284 | break; |
| 285 | case DDR3_ROW_14BIT: |
| 286 | s = ", ROW = 14"; |
| 287 | break; |
| 288 | case DDR3_ROW_15BIT: |
| 289 | s = ", ROW = 15"; |
| 290 | break; |
| 291 | default: |
| 292 | s = ", ROW = 16"; |
| 293 | break; |
| 294 | } |
| 295 | print_debug(s); |
| 296 | u8 size = get_ddr3_mem_reg_size(mbr); |
| 297 | switch (size) { |
| 298 | case DDR3_SIZE_64M: |
| 299 | s = ", 64M"; |
| 300 | break; |
| 301 | case DDR3_SIZE_128M: |
| 302 | s = ", 128M"; |
| 303 | break; |
| 304 | case DDR3_SIZE_256M: |
| 305 | s = ", 256M"; |
| 306 | break; |
| 307 | case DDR3_SIZE_512M: |
| 308 | s = ", 512M"; |
| 309 | break; |
| 310 | case DDR3_SIZE_1GB: |
| 311 | s = ", 1GB"; |
| 312 | break; |
| 313 | case DDR3_SIZE_2GB: |
| 314 | s = ", 2GB"; |
| 315 | break; |
| 316 | } |
| 317 | print_debug(s); |
| 318 | u8 mask = get_ddr3_mem_reg_c1m(mbr); |
| 319 | if (mask == DDR3_C1M_ACTIVE) |
| 320 | s = ", CS MASK Enable)\n"; |
| 321 | else |
| 322 | s = ", CS Mask Disable)\n"; |
| 323 | print_debug(s); |
| 324 | #endif |
| 325 | } |