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