Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2007-2008 coresystems GmbH |
| 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 |
Paul Menzel | a46a712 | 2013-02-23 18:37:27 +0100 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "raminit.h" |
| 21 | |
| 22 | /** |
| 23 | * sample the strobes signal |
| 24 | */ |
| 25 | static u32 sample_strobes(int channel_offset, struct sys_info *sysinfo) |
| 26 | { |
| 27 | u32 reg32, addr; |
| 28 | int i; |
| 29 | |
| 30 | MCHBAR32(C0DRC1 + channel_offset) |= (1 << 6); |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 31 | |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 32 | MCHBAR32(C0DRC1 + channel_offset) &= ~(1 << 6); |
| 33 | |
| 34 | addr = 0; |
| 35 | |
| 36 | if (channel_offset != 0) { /* must be dual channel */ |
| 37 | if (sysinfo->interleaved == 1) { |
| 38 | addr |= (1 << 6); |
| 39 | } else { |
| 40 | addr = ((u32)MCHBAR8(C0DRB3)) << 25; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | for (i = 0; i < 28; i++) { |
| 45 | read32(addr); |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 46 | read32(addr + 0x80); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | reg32 = MCHBAR32(RCVENMT); |
| 50 | if (channel_offset == 0) { |
| 51 | reg32 = reg32 << 2; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * [19] = 1: all bits are high |
| 56 | * [18] = 1: all bits are low |
| 57 | * [19:18] = 00: bits are mixed high, low |
| 58 | */ |
| 59 | return reg32; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * This function sets receive enable coarse and medium timing parameters |
| 64 | */ |
| 65 | |
| 66 | static void set_receive_enable(int channel_offset, u8 medium, u8 coarse) |
| 67 | { |
| 68 | u32 reg32; |
| 69 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 70 | printk(BIOS_SPEW, " set_receive_enable() medium=0x%x, coarse=0x%x\n", medium, coarse); |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 71 | |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 72 | reg32 = MCHBAR32(C0DRT1 + channel_offset); |
| 73 | reg32 &= 0xf0ffffff; |
| 74 | reg32 |= ((u32)coarse & 0x0f) << 24; |
| 75 | MCHBAR32(C0DRT1 + channel_offset) = reg32; |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 76 | |
| 77 | /* This should never happen: */ |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 78 | if (coarse > 0x0f) |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 79 | printk(BIOS_DEBUG, "set_receive_enable: coarse overflow: 0x%02x.\n", coarse); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 80 | |
| 81 | /* medium control |
| 82 | * |
| 83 | * 00 - 1/4 clock |
| 84 | * 01 - 1/2 clock |
| 85 | * 10 - 3/4 clock |
| 86 | * 11 - 1 clock |
| 87 | */ |
| 88 | |
| 89 | reg32 = MCHBAR32(RCVENMT); |
| 90 | if (!channel_offset) { |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 91 | /* Channel 0 */ |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 92 | reg32 &= ~(3 << 2); |
| 93 | reg32 |= medium << 2; |
| 94 | } else { |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 95 | /* Channel 1 */ |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 96 | reg32 &= ~(3 << 0); |
| 97 | reg32 |= medium; |
| 98 | } |
| 99 | MCHBAR32(RCVENMT) = reg32; |
| 100 | |
| 101 | } |
| 102 | |
| 103 | static int normalize(int channel_offset, u8 * mediumcoarse, u8 * fine) |
| 104 | { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 105 | printk(BIOS_SPEW, " normalize()\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 106 | |
| 107 | if (*fine < 0x80) |
| 108 | return 0; |
| 109 | |
| 110 | *fine -= 0x80; |
| 111 | *mediumcoarse += 1; |
| 112 | |
| 113 | if (*mediumcoarse >= 0x40) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 114 | printk(BIOS_DEBUG, "Normalize Error\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 119 | *mediumcoarse >> 2); |
| 120 | |
| 121 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int find_preamble(int channel_offset, u8 * mediumcoarse, |
| 127 | struct sys_info *sysinfo) |
| 128 | { |
| 129 | /* find start of the data phase */ |
| 130 | u32 reg32; |
| 131 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 132 | printk(BIOS_SPEW, " find_preamble()\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 133 | |
| 134 | do { |
| 135 | if (*mediumcoarse < 4) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 136 | printk(BIOS_DEBUG, "No Preamble found.\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 137 | return -1; |
| 138 | } |
| 139 | *mediumcoarse -= 4; |
| 140 | |
| 141 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 142 | *mediumcoarse >> 2); |
| 143 | |
| 144 | reg32 = sample_strobes(channel_offset, sysinfo); |
| 145 | |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 146 | } while (reg32 & (1 << 19)); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 147 | |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 148 | if (!(reg32 & (1 << 18))) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 149 | printk(BIOS_DEBUG, "No Preamble found (neither high nor low).\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * add a quarter clock to the current receive enable settings |
| 158 | */ |
| 159 | |
| 160 | static int add_quarter_clock(int channel_offset, u8 * mediumcoarse, u8 * fine) |
| 161 | { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 162 | printk(BIOS_SPEW, " add_quarter_clock() mediumcoarse=%02x fine=%02x\n", |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 163 | *mediumcoarse, *fine); |
| 164 | if (*fine >= 0x80) { |
| 165 | *fine -= 0x80; |
| 166 | |
| 167 | *mediumcoarse += 2; |
| 168 | if (*mediumcoarse >= 0x40) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 169 | printk(BIOS_DEBUG, "clocks at max.\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 174 | *mediumcoarse >> 2); |
| 175 | } else { |
| 176 | *fine += 0x80; |
| 177 | } |
| 178 | |
| 179 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int find_strobes_low(int channel_offset, u8 * mediumcoarse, u8 * fine, |
| 185 | struct sys_info *sysinfo) |
| 186 | { |
| 187 | u32 rcvenmt; |
| 188 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 189 | printk(BIOS_SPEW, " find_strobes_low()\n"); |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 190 | |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 191 | for (;;) { |
| 192 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 193 | |
| 194 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 195 | *mediumcoarse >> 2); |
| 196 | |
| 197 | rcvenmt = sample_strobes(channel_offset, sysinfo); |
| 198 | |
| 199 | if (((rcvenmt & (1 << 18)) != 0)) |
| 200 | return 0; |
| 201 | |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 202 | *fine -= 0x80; |
| 203 | if (*fine == 0) |
| 204 | continue; |
| 205 | |
| 206 | *mediumcoarse -= 2; |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 207 | if (*mediumcoarse < 0xfe) |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 208 | continue; |
| 209 | |
| 210 | break; |
| 211 | |
| 212 | } |
| 213 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 214 | printk(BIOS_DEBUG, "Could not find low strobe\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int find_strobes_edge(int channel_offset, u8 * mediumcoarse, u8 * fine, |
| 219 | struct sys_info *sysinfo) |
| 220 | { |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 221 | |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 222 | int counter; |
| 223 | u32 rcvenmt; |
| 224 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 225 | printk(BIOS_SPEW, " find_strobes_edge()\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 226 | |
| 227 | counter = 8; |
| 228 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 229 | *mediumcoarse >> 2); |
| 230 | |
| 231 | for (;;) { |
| 232 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 233 | rcvenmt = sample_strobes(channel_offset, sysinfo); |
| 234 | |
| 235 | if ((rcvenmt & (1 << 19)) == 0) { |
| 236 | counter = 8; |
| 237 | } else { |
| 238 | counter--; |
| 239 | if (!counter) |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | *fine = *fine + 1; |
| 244 | if (*fine < 0xf8) { |
| 245 | if (*fine & (1 << 3)) { |
| 246 | *fine &= ~(1 << 3); |
| 247 | *fine += 0x10; |
| 248 | } |
| 249 | continue; |
| 250 | } |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 251 | |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 252 | *fine = 0; |
| 253 | *mediumcoarse += 2; |
| 254 | if (*mediumcoarse <= 0x40) { |
| 255 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 256 | *mediumcoarse >> 2); |
| 257 | continue; |
| 258 | } |
| 259 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 260 | printk(BIOS_DEBUG, "Could not find rising edge.\n"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 261 | return -1; |
| 262 | } |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 263 | |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 264 | *fine -= 7; |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 265 | if (*fine >= 0xf9) { |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 266 | *mediumcoarse -= 2; |
| 267 | set_receive_enable(channel_offset, *mediumcoarse & 3, |
| 268 | *mediumcoarse >> 2); |
| 269 | } |
| 270 | |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 271 | *fine &= ~(1 << 3); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 272 | MCHBAR8(C0WL0REOST + channel_offset) = *fine; |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /** |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 278 | * Here we use a trick. The RCVEN channel 0 registers are all at an |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 279 | * offset of 0x80 to the channel 0 registers. We don't want to waste |
| 280 | * a lot of if()s so let's just pass 0 or 0x80 for the channel offset. |
| 281 | */ |
| 282 | |
| 283 | static int receive_enable_autoconfig(int channel_offset, |
| 284 | struct sys_info *sysinfo) |
| 285 | { |
| 286 | u8 mediumcoarse; |
| 287 | u8 fine; |
| 288 | |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 289 | printk(BIOS_SPEW, "receive_enable_autoconfig() for channel %d\n", |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 290 | channel_offset ? 1 : 0); |
| 291 | |
| 292 | /* Set initial values */ |
Stefan Reinauer | 71a3d96 | 2009-07-21 21:44:24 +0000 | [diff] [blame] | 293 | mediumcoarse = (sysinfo->cas << 2) | 3; |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 294 | fine = 0; |
| 295 | |
| 296 | if (find_strobes_low(channel_offset, &mediumcoarse, &fine, sysinfo)) |
| 297 | return -1; |
| 298 | |
| 299 | if (find_strobes_edge(channel_offset, &mediumcoarse, &fine, sysinfo)) |
| 300 | return -1; |
| 301 | |
| 302 | if (add_quarter_clock(channel_offset, &mediumcoarse, &fine)) |
| 303 | return -1; |
| 304 | |
| 305 | if (find_preamble(channel_offset, &mediumcoarse, sysinfo)) |
| 306 | return -1; |
| 307 | |
| 308 | if (add_quarter_clock(channel_offset, &mediumcoarse, &fine)) |
| 309 | return -1; |
| 310 | |
| 311 | if (normalize(channel_offset, &mediumcoarse, &fine)) |
| 312 | return -1; |
| 313 | |
| 314 | /* This is a debug check to see if the rcven code is fully working. |
| 315 | * It can be removed when the output message is not printed anymore |
| 316 | */ |
| 317 | if (MCHBAR8(C0WL0REOST + channel_offset) == 0) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 318 | printk(BIOS_DEBUG, "Weird. No C%sWL0REOST\n", channel_offset?"1":"0"); |
Stefan Reinauer | 278534d | 2008-10-29 04:51:07 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | void receive_enable_adjust(struct sys_info *sysinfo) |
| 325 | { |
| 326 | /* Is channel 0 populated? */ |
| 327 | if (sysinfo->dimm[0] != SYSINFO_DIMM_NOT_POPULATED |
| 328 | || sysinfo->dimm[1] != SYSINFO_DIMM_NOT_POPULATED) |
| 329 | if (receive_enable_autoconfig(0, sysinfo)) |
| 330 | return; |
| 331 | |
| 332 | /* Is channel 1 populated? */ |
| 333 | if (sysinfo->dimm[2] != SYSINFO_DIMM_NOT_POPULATED |
| 334 | || sysinfo->dimm[3] != SYSINFO_DIMM_NOT_POPULATED) |
| 335 | if (receive_enable_autoconfig(0x80, sysinfo)) |
| 336 | return; |
| 337 | } |