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