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