Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

memory: emif: handle frequency and voltage change events

Change SDRAM timings and other settings as necessary
on voltage and frequency changes. We calculate these
register settings based on data from the device data
sheet and inputs such a frequency, voltage state(stable
or ramping), temperature level etc.

TODO: frequency and voltage change handling needs to
be integrated with clock framework and regulator
framework respectively. This is not done today
due to missing pieces in the kernel.

Signed-off-by: Aneesh V <aneesh@ti.com>
Reviewed-by: Santosh Shilimkar <santosh.shilimkar@ti.com>
Reviewed-by: Benoit Cousson <b-cousson@ti.com>
[santosh.shilimkar@ti.com: Moved to drivers/memory from drivers/misc]
Signed-off-by: Santosh Shilimkar <santosh.shilimkar@ti.com>
Tested-by: Lokesh Vutla <lokeshvutla@ti.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Aneesh V and committed by
Greg Kroah-Hartman
a93de288 7ec94453

+1020 -4
+891 -3
drivers/memory/emif.c
··· 21 21 #include <linux/seq_file.h> 22 22 #include <linux/module.h> 23 23 #include <linux/list.h> 24 + #include <linux/spinlock.h> 24 25 #include <memory/jedec_ddr.h> 25 26 #include "emif.h" 26 27 ··· 38 37 * @node: node in the device list 39 38 * @base: base address of memory-mapped IO registers. 40 39 * @dev: device pointer. 40 + * @addressing table with addressing information from the spec 41 + * @regs_cache: An array of 'struct emif_regs' that stores 42 + * calculated register values for different 43 + * frequencies, to avoid re-calculating them on 44 + * each DVFS transition. 45 + * @curr_regs: The set of register values used in the last 46 + * frequency change (i.e. corresponding to the 47 + * frequency in effect at the moment) 41 48 * @plat_data: Pointer to saved platform data. 42 49 */ 43 50 struct emif_data { 44 51 u8 duplicate; 45 52 u8 temperature_level; 53 + u8 lpmode; 46 54 struct list_head node; 55 + unsigned long irq_state; 47 56 void __iomem *base; 48 57 struct device *dev; 58 + const struct lpddr2_addressing *addressing; 59 + struct emif_regs *regs_cache[EMIF_MAX_NUM_FREQUENCIES]; 60 + struct emif_regs *curr_regs; 49 61 struct emif_platform_data *plat_data; 50 62 }; 51 63 52 64 static struct emif_data *emif1; 65 + static spinlock_t emif_lock; 66 + static unsigned long irq_state; 67 + static u32 t_ck; /* DDR clock period in ps */ 53 68 static LIST_HEAD(device_list); 69 + 70 + /* 71 + * Calculate the period of DDR clock from frequency value 72 + */ 73 + static void set_ddr_clk_period(u32 freq) 74 + { 75 + /* Divide 10^12 by frequency to get period in ps */ 76 + t_ck = (u32)DIV_ROUND_UP_ULL(1000000000000ull, freq); 77 + } 78 + 79 + /* 80 + * Get the CL from SDRAM_CONFIG register 81 + */ 82 + static u32 get_cl(struct emif_data *emif) 83 + { 84 + u32 cl; 85 + void __iomem *base = emif->base; 86 + 87 + cl = (readl(base + EMIF_SDRAM_CONFIG) & CL_MASK) >> CL_SHIFT; 88 + 89 + return cl; 90 + } 91 + 92 + static void set_lpmode(struct emif_data *emif, u8 lpmode) 93 + { 94 + u32 temp; 95 + void __iomem *base = emif->base; 96 + 97 + temp = readl(base + EMIF_POWER_MANAGEMENT_CONTROL); 98 + temp &= ~LP_MODE_MASK; 99 + temp |= (lpmode << LP_MODE_SHIFT); 100 + writel(temp, base + EMIF_POWER_MANAGEMENT_CONTROL); 101 + } 102 + 103 + static void do_freq_update(void) 104 + { 105 + struct emif_data *emif; 106 + 107 + /* 108 + * Workaround for errata i728: Disable LPMODE during FREQ_UPDATE 109 + * 110 + * i728 DESCRIPTION: 111 + * The EMIF automatically puts the SDRAM into self-refresh mode 112 + * after the EMIF has not performed accesses during 113 + * EMIF_PWR_MGMT_CTRL[7:4] REG_SR_TIM number of DDR clock cycles 114 + * and the EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE bit field is set 115 + * to 0x2. If during a small window the following three events 116 + * occur: 117 + * - The SR_TIMING counter expires 118 + * - And frequency change is requested 119 + * - And OCP access is requested 120 + * Then it causes instable clock on the DDR interface. 121 + * 122 + * WORKAROUND 123 + * To avoid the occurrence of the three events, the workaround 124 + * is to disable the self-refresh when requesting a frequency 125 + * change. Before requesting a frequency change the software must 126 + * program EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE to 0x0. When the 127 + * frequency change has been done, the software can reprogram 128 + * EMIF_PWR_MGMT_CTRL[10:8] REG_LP_MODE to 0x2 129 + */ 130 + list_for_each_entry(emif, &device_list, node) { 131 + if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH) 132 + set_lpmode(emif, EMIF_LP_MODE_DISABLE); 133 + } 134 + 135 + /* 136 + * TODO: Do FREQ_UPDATE here when an API 137 + * is available for this as part of the new 138 + * clock framework 139 + */ 140 + 141 + list_for_each_entry(emif, &device_list, node) { 142 + if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH) 143 + set_lpmode(emif, EMIF_LP_MODE_SELF_REFRESH); 144 + } 145 + } 146 + 147 + /* Find addressing table entry based on the device's type and density */ 148 + static const struct lpddr2_addressing *get_addressing_table( 149 + const struct ddr_device_info *device_info) 150 + { 151 + u32 index, type, density; 152 + 153 + type = device_info->type; 154 + density = device_info->density; 155 + 156 + switch (type) { 157 + case DDR_TYPE_LPDDR2_S4: 158 + index = density - 1; 159 + break; 160 + case DDR_TYPE_LPDDR2_S2: 161 + switch (density) { 162 + case DDR_DENSITY_1Gb: 163 + case DDR_DENSITY_2Gb: 164 + index = density + 3; 165 + break; 166 + default: 167 + index = density - 1; 168 + } 169 + break; 170 + default: 171 + return NULL; 172 + } 173 + 174 + return &lpddr2_jedec_addressing_table[index]; 175 + } 176 + 177 + /* 178 + * Find the the right timing table from the array of timing 179 + * tables of the device using DDR clock frequency 180 + */ 181 + static const struct lpddr2_timings *get_timings_table(struct emif_data *emif, 182 + u32 freq) 183 + { 184 + u32 i, min, max, freq_nearest; 185 + const struct lpddr2_timings *timings = NULL; 186 + const struct lpddr2_timings *timings_arr = emif->plat_data->timings; 187 + struct device *dev = emif->dev; 188 + 189 + /* Start with a very high frequency - 1GHz */ 190 + freq_nearest = 1000000000; 191 + 192 + /* 193 + * Find the timings table such that: 194 + * 1. the frequency range covers the required frequency(safe) AND 195 + * 2. the max_freq is closest to the required frequency(optimal) 196 + */ 197 + for (i = 0; i < emif->plat_data->timings_arr_size; i++) { 198 + max = timings_arr[i].max_freq; 199 + min = timings_arr[i].min_freq; 200 + if ((freq >= min) && (freq <= max) && (max < freq_nearest)) { 201 + freq_nearest = max; 202 + timings = &timings_arr[i]; 203 + } 204 + } 205 + 206 + if (!timings) 207 + dev_err(dev, "%s: couldn't find timings for - %dHz\n", 208 + __func__, freq); 209 + 210 + dev_dbg(dev, "%s: timings table: freq %d, speed bin freq %d\n", 211 + __func__, freq, freq_nearest); 212 + 213 + return timings; 214 + } 215 + 216 + static u32 get_sdram_ref_ctrl_shdw(u32 freq, 217 + const struct lpddr2_addressing *addressing) 218 + { 219 + u32 ref_ctrl_shdw = 0, val = 0, freq_khz, t_refi; 220 + 221 + /* Scale down frequency and t_refi to avoid overflow */ 222 + freq_khz = freq / 1000; 223 + t_refi = addressing->tREFI_ns / 100; 224 + 225 + /* 226 + * refresh rate to be set is 'tREFI(in us) * freq in MHz 227 + * division by 10000 to account for change in units 228 + */ 229 + val = t_refi * freq_khz / 10000; 230 + ref_ctrl_shdw |= val << REFRESH_RATE_SHIFT; 231 + 232 + return ref_ctrl_shdw; 233 + } 234 + 235 + static u32 get_sdram_tim_1_shdw(const struct lpddr2_timings *timings, 236 + const struct lpddr2_min_tck *min_tck, 237 + const struct lpddr2_addressing *addressing) 238 + { 239 + u32 tim1 = 0, val = 0; 240 + 241 + val = max(min_tck->tWTR, DIV_ROUND_UP(timings->tWTR, t_ck)) - 1; 242 + tim1 |= val << T_WTR_SHIFT; 243 + 244 + if (addressing->num_banks == B8) 245 + val = DIV_ROUND_UP(timings->tFAW, t_ck*4); 246 + else 247 + val = max(min_tck->tRRD, DIV_ROUND_UP(timings->tRRD, t_ck)); 248 + tim1 |= (val - 1) << T_RRD_SHIFT; 249 + 250 + val = DIV_ROUND_UP(timings->tRAS_min + timings->tRPab, t_ck) - 1; 251 + tim1 |= val << T_RC_SHIFT; 252 + 253 + val = max(min_tck->tRASmin, DIV_ROUND_UP(timings->tRAS_min, t_ck)); 254 + tim1 |= (val - 1) << T_RAS_SHIFT; 255 + 256 + val = max(min_tck->tWR, DIV_ROUND_UP(timings->tWR, t_ck)) - 1; 257 + tim1 |= val << T_WR_SHIFT; 258 + 259 + val = max(min_tck->tRCD, DIV_ROUND_UP(timings->tRCD, t_ck)) - 1; 260 + tim1 |= val << T_RCD_SHIFT; 261 + 262 + val = max(min_tck->tRPab, DIV_ROUND_UP(timings->tRPab, t_ck)) - 1; 263 + tim1 |= val << T_RP_SHIFT; 264 + 265 + return tim1; 266 + } 267 + 268 + static u32 get_sdram_tim_1_shdw_derated(const struct lpddr2_timings *timings, 269 + const struct lpddr2_min_tck *min_tck, 270 + const struct lpddr2_addressing *addressing) 271 + { 272 + u32 tim1 = 0, val = 0; 273 + 274 + val = max(min_tck->tWTR, DIV_ROUND_UP(timings->tWTR, t_ck)) - 1; 275 + tim1 = val << T_WTR_SHIFT; 276 + 277 + /* 278 + * tFAW is approximately 4 times tRRD. So add 1875*4 = 7500ps 279 + * to tFAW for de-rating 280 + */ 281 + if (addressing->num_banks == B8) { 282 + val = DIV_ROUND_UP(timings->tFAW + 7500, 4 * t_ck) - 1; 283 + } else { 284 + val = DIV_ROUND_UP(timings->tRRD + 1875, t_ck); 285 + val = max(min_tck->tRRD, val) - 1; 286 + } 287 + tim1 |= val << T_RRD_SHIFT; 288 + 289 + val = DIV_ROUND_UP(timings->tRAS_min + timings->tRPab + 1875, t_ck); 290 + tim1 |= (val - 1) << T_RC_SHIFT; 291 + 292 + val = DIV_ROUND_UP(timings->tRAS_min + 1875, t_ck); 293 + val = max(min_tck->tRASmin, val) - 1; 294 + tim1 |= val << T_RAS_SHIFT; 295 + 296 + val = max(min_tck->tWR, DIV_ROUND_UP(timings->tWR, t_ck)) - 1; 297 + tim1 |= val << T_WR_SHIFT; 298 + 299 + val = max(min_tck->tRCD, DIV_ROUND_UP(timings->tRCD + 1875, t_ck)); 300 + tim1 |= (val - 1) << T_RCD_SHIFT; 301 + 302 + val = max(min_tck->tRPab, DIV_ROUND_UP(timings->tRPab + 1875, t_ck)); 303 + tim1 |= (val - 1) << T_RP_SHIFT; 304 + 305 + return tim1; 306 + } 307 + 308 + static u32 get_sdram_tim_2_shdw(const struct lpddr2_timings *timings, 309 + const struct lpddr2_min_tck *min_tck, 310 + const struct lpddr2_addressing *addressing, 311 + u32 type) 312 + { 313 + u32 tim2 = 0, val = 0; 314 + 315 + val = min_tck->tCKE - 1; 316 + tim2 |= val << T_CKE_SHIFT; 317 + 318 + val = max(min_tck->tRTP, DIV_ROUND_UP(timings->tRTP, t_ck)) - 1; 319 + tim2 |= val << T_RTP_SHIFT; 320 + 321 + /* tXSNR = tRFCab_ps + 10 ns(tRFCab_ps for LPDDR2). */ 322 + val = DIV_ROUND_UP(addressing->tRFCab_ps + 10000, t_ck) - 1; 323 + tim2 |= val << T_XSNR_SHIFT; 324 + 325 + /* XSRD same as XSNR for LPDDR2 */ 326 + tim2 |= val << T_XSRD_SHIFT; 327 + 328 + val = max(min_tck->tXP, DIV_ROUND_UP(timings->tXP, t_ck)) - 1; 329 + tim2 |= val << T_XP_SHIFT; 330 + 331 + return tim2; 332 + } 333 + 334 + static u32 get_sdram_tim_3_shdw(const struct lpddr2_timings *timings, 335 + const struct lpddr2_min_tck *min_tck, 336 + const struct lpddr2_addressing *addressing, 337 + u32 type, u32 ip_rev, u32 derated) 338 + { 339 + u32 tim3 = 0, val = 0, t_dqsck; 340 + 341 + val = timings->tRAS_max_ns / addressing->tREFI_ns - 1; 342 + val = val > 0xF ? 0xF : val; 343 + tim3 |= val << T_RAS_MAX_SHIFT; 344 + 345 + val = DIV_ROUND_UP(addressing->tRFCab_ps, t_ck) - 1; 346 + tim3 |= val << T_RFC_SHIFT; 347 + 348 + t_dqsck = (derated == EMIF_DERATED_TIMINGS) ? 349 + timings->tDQSCK_max_derated : timings->tDQSCK_max; 350 + if (ip_rev == EMIF_4D5) 351 + val = DIV_ROUND_UP(t_dqsck + 1000, t_ck) - 1; 352 + else 353 + val = DIV_ROUND_UP(t_dqsck, t_ck) - 1; 354 + 355 + tim3 |= val << T_TDQSCKMAX_SHIFT; 356 + 357 + val = DIV_ROUND_UP(timings->tZQCS, t_ck) - 1; 358 + tim3 |= val << ZQ_ZQCS_SHIFT; 359 + 360 + val = DIV_ROUND_UP(timings->tCKESR, t_ck); 361 + val = max(min_tck->tCKESR, val) - 1; 362 + tim3 |= val << T_CKESR_SHIFT; 363 + 364 + if (ip_rev == EMIF_4D5) { 365 + tim3 |= (EMIF_T_CSTA - 1) << T_CSTA_SHIFT; 366 + 367 + val = DIV_ROUND_UP(EMIF_T_PDLL_UL, 128) - 1; 368 + tim3 |= val << T_PDLL_UL_SHIFT; 369 + } 370 + 371 + return tim3; 372 + } 373 + 374 + static u32 get_read_idle_ctrl_shdw(u8 volt_ramp) 375 + { 376 + u32 idle = 0, val = 0; 377 + 378 + /* 379 + * Maximum value in normal conditions and increased frequency 380 + * when voltage is ramping 381 + */ 382 + if (volt_ramp) 383 + val = READ_IDLE_INTERVAL_DVFS / t_ck / 64 - 1; 384 + else 385 + val = 0x1FF; 386 + 387 + /* 388 + * READ_IDLE_CTRL register in EMIF4D has same offset and fields 389 + * as DLL_CALIB_CTRL in EMIF4D5, so use the same shifts 390 + */ 391 + idle |= val << DLL_CALIB_INTERVAL_SHIFT; 392 + idle |= EMIF_READ_IDLE_LEN_VAL << ACK_WAIT_SHIFT; 393 + 394 + return idle; 395 + } 396 + 397 + static u32 get_dll_calib_ctrl_shdw(u8 volt_ramp) 398 + { 399 + u32 calib = 0, val = 0; 400 + 401 + if (volt_ramp == DDR_VOLTAGE_RAMPING) 402 + val = DLL_CALIB_INTERVAL_DVFS / t_ck / 16 - 1; 403 + else 404 + val = 0; /* Disabled when voltage is stable */ 405 + 406 + calib |= val << DLL_CALIB_INTERVAL_SHIFT; 407 + calib |= DLL_CALIB_ACK_WAIT_VAL << ACK_WAIT_SHIFT; 408 + 409 + return calib; 410 + } 411 + 412 + static u32 get_ddr_phy_ctrl_1_attilaphy_4d(const struct lpddr2_timings *timings, 413 + u32 freq, u8 RL) 414 + { 415 + u32 phy = EMIF_DDR_PHY_CTRL_1_BASE_VAL_ATTILAPHY, val = 0; 416 + 417 + val = RL + DIV_ROUND_UP(timings->tDQSCK_max, t_ck) - 1; 418 + phy |= val << READ_LATENCY_SHIFT_4D; 419 + 420 + if (freq <= 100000000) 421 + val = EMIF_DLL_SLAVE_DLY_CTRL_100_MHZ_AND_LESS_ATTILAPHY; 422 + else if (freq <= 200000000) 423 + val = EMIF_DLL_SLAVE_DLY_CTRL_200_MHZ_ATTILAPHY; 424 + else 425 + val = EMIF_DLL_SLAVE_DLY_CTRL_400_MHZ_ATTILAPHY; 426 + 427 + phy |= val << DLL_SLAVE_DLY_CTRL_SHIFT_4D; 428 + 429 + return phy; 430 + } 431 + 432 + static u32 get_phy_ctrl_1_intelliphy_4d5(u32 freq, u8 cl) 433 + { 434 + u32 phy = EMIF_DDR_PHY_CTRL_1_BASE_VAL_INTELLIPHY, half_delay; 435 + 436 + /* 437 + * DLL operates at 266 MHz. If DDR frequency is near 266 MHz, 438 + * half-delay is not needed else set half-delay 439 + */ 440 + if (freq >= 265000000 && freq < 267000000) 441 + half_delay = 0; 442 + else 443 + half_delay = 1; 444 + 445 + phy |= half_delay << DLL_HALF_DELAY_SHIFT_4D5; 446 + phy |= ((cl + DIV_ROUND_UP(EMIF_PHY_TOTAL_READ_LATENCY_INTELLIPHY_PS, 447 + t_ck) - 1) << READ_LATENCY_SHIFT_4D5); 448 + 449 + return phy; 450 + } 451 + 452 + static u32 get_ext_phy_ctrl_2_intelliphy_4d5(void) 453 + { 454 + u32 fifo_we_slave_ratio; 455 + 456 + fifo_we_slave_ratio = DIV_ROUND_CLOSEST( 457 + EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck); 458 + 459 + return fifo_we_slave_ratio | fifo_we_slave_ratio << 11 | 460 + fifo_we_slave_ratio << 22; 461 + } 462 + 463 + static u32 get_ext_phy_ctrl_3_intelliphy_4d5(void) 464 + { 465 + u32 fifo_we_slave_ratio; 466 + 467 + fifo_we_slave_ratio = DIV_ROUND_CLOSEST( 468 + EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck); 469 + 470 + return fifo_we_slave_ratio >> 10 | fifo_we_slave_ratio << 1 | 471 + fifo_we_slave_ratio << 12 | fifo_we_slave_ratio << 23; 472 + } 473 + 474 + static u32 get_ext_phy_ctrl_4_intelliphy_4d5(void) 475 + { 476 + u32 fifo_we_slave_ratio; 477 + 478 + fifo_we_slave_ratio = DIV_ROUND_CLOSEST( 479 + EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS * 256 , t_ck); 480 + 481 + return fifo_we_slave_ratio >> 9 | fifo_we_slave_ratio << 2 | 482 + fifo_we_slave_ratio << 13; 483 + } 484 + 485 + static u32 get_pwr_mgmt_ctrl(u32 freq, struct emif_data *emif, u32 ip_rev) 486 + { 487 + u32 pwr_mgmt_ctrl = 0, timeout; 488 + u32 lpmode = EMIF_LP_MODE_SELF_REFRESH; 489 + u32 timeout_perf = EMIF_LP_MODE_TIMEOUT_PERFORMANCE; 490 + u32 timeout_pwr = EMIF_LP_MODE_TIMEOUT_POWER; 491 + u32 freq_threshold = EMIF_LP_MODE_FREQ_THRESHOLD; 492 + 493 + struct emif_custom_configs *cust_cfgs = emif->plat_data->custom_configs; 494 + 495 + if (cust_cfgs && (cust_cfgs->mask & EMIF_CUSTOM_CONFIG_LPMODE)) { 496 + lpmode = cust_cfgs->lpmode; 497 + timeout_perf = cust_cfgs->lpmode_timeout_performance; 498 + timeout_pwr = cust_cfgs->lpmode_timeout_power; 499 + freq_threshold = cust_cfgs->lpmode_freq_threshold; 500 + } 501 + 502 + /* Timeout based on DDR frequency */ 503 + timeout = freq >= freq_threshold ? timeout_perf : timeout_pwr; 504 + 505 + /* The value to be set in register is "log2(timeout) - 3" */ 506 + if (timeout < 16) { 507 + timeout = 0; 508 + } else { 509 + timeout = __fls(timeout) - 3; 510 + if (timeout & (timeout - 1)) 511 + timeout++; 512 + } 513 + 514 + switch (lpmode) { 515 + case EMIF_LP_MODE_CLOCK_STOP: 516 + pwr_mgmt_ctrl = (timeout << CS_TIM_SHIFT) | 517 + SR_TIM_MASK | PD_TIM_MASK; 518 + break; 519 + case EMIF_LP_MODE_SELF_REFRESH: 520 + /* Workaround for errata i735 */ 521 + if (timeout < 6) 522 + timeout = 6; 523 + 524 + pwr_mgmt_ctrl = (timeout << SR_TIM_SHIFT) | 525 + CS_TIM_MASK | PD_TIM_MASK; 526 + break; 527 + case EMIF_LP_MODE_PWR_DN: 528 + pwr_mgmt_ctrl = (timeout << PD_TIM_SHIFT) | 529 + CS_TIM_MASK | SR_TIM_MASK; 530 + break; 531 + case EMIF_LP_MODE_DISABLE: 532 + default: 533 + pwr_mgmt_ctrl = CS_TIM_MASK | 534 + PD_TIM_MASK | SR_TIM_MASK; 535 + } 536 + 537 + /* No CS_TIM in EMIF_4D5 */ 538 + if (ip_rev == EMIF_4D5) 539 + pwr_mgmt_ctrl &= ~CS_TIM_MASK; 540 + 541 + pwr_mgmt_ctrl |= lpmode << LP_MODE_SHIFT; 542 + 543 + return pwr_mgmt_ctrl; 544 + } 545 + 546 + /* 547 + * Program EMIF shadow registers that are not dependent on temperature 548 + * or voltage 549 + */ 550 + static void setup_registers(struct emif_data *emif, struct emif_regs *regs) 551 + { 552 + void __iomem *base = emif->base; 553 + 554 + writel(regs->sdram_tim2_shdw, base + EMIF_SDRAM_TIMING_2_SHDW); 555 + writel(regs->phy_ctrl_1_shdw, base + EMIF_DDR_PHY_CTRL_1_SHDW); 556 + 557 + /* Settings specific for EMIF4D5 */ 558 + if (emif->plat_data->ip_rev != EMIF_4D5) 559 + return; 560 + writel(regs->ext_phy_ctrl_2_shdw, base + EMIF_EXT_PHY_CTRL_2_SHDW); 561 + writel(regs->ext_phy_ctrl_3_shdw, base + EMIF_EXT_PHY_CTRL_3_SHDW); 562 + writel(regs->ext_phy_ctrl_4_shdw, base + EMIF_EXT_PHY_CTRL_4_SHDW); 563 + } 564 + 565 + /* 566 + * When voltage ramps dll calibration and forced read idle should 567 + * happen more often 568 + */ 569 + static void setup_volt_sensitive_regs(struct emif_data *emif, 570 + struct emif_regs *regs, u32 volt_state) 571 + { 572 + u32 calib_ctrl; 573 + void __iomem *base = emif->base; 574 + 575 + /* 576 + * EMIF_READ_IDLE_CTRL in EMIF4D refers to the same register as 577 + * EMIF_DLL_CALIB_CTRL in EMIF4D5 and dll_calib_ctrl_shadow_* 578 + * is an alias of the respective read_idle_ctrl_shdw_* (members of 579 + * a union). So, the below code takes care of both cases 580 + */ 581 + if (volt_state == DDR_VOLTAGE_RAMPING) 582 + calib_ctrl = regs->dll_calib_ctrl_shdw_volt_ramp; 583 + else 584 + calib_ctrl = regs->dll_calib_ctrl_shdw_normal; 585 + 586 + writel(calib_ctrl, base + EMIF_DLL_CALIB_CTRL_SHDW); 587 + } 588 + 589 + /* 590 + * setup_temperature_sensitive_regs() - set the timings for temperature 591 + * sensitive registers. This happens once at initialisation time based 592 + * on the temperature at boot time and subsequently based on the temperature 593 + * alert interrupt. Temperature alert can happen when the temperature 594 + * increases or drops. So this function can have the effect of either 595 + * derating the timings or going back to nominal values. 596 + */ 597 + static void setup_temperature_sensitive_regs(struct emif_data *emif, 598 + struct emif_regs *regs) 599 + { 600 + u32 tim1, tim3, ref_ctrl, type; 601 + void __iomem *base = emif->base; 602 + u32 temperature; 603 + 604 + type = emif->plat_data->device_info->type; 605 + 606 + tim1 = regs->sdram_tim1_shdw; 607 + tim3 = regs->sdram_tim3_shdw; 608 + ref_ctrl = regs->ref_ctrl_shdw; 609 + 610 + /* No de-rating for non-lpddr2 devices */ 611 + if (type != DDR_TYPE_LPDDR2_S2 && type != DDR_TYPE_LPDDR2_S4) 612 + goto out; 613 + 614 + temperature = emif->temperature_level; 615 + if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH) { 616 + ref_ctrl = regs->ref_ctrl_shdw_derated; 617 + } else if (temperature == SDRAM_TEMP_HIGH_DERATE_REFRESH_AND_TIMINGS) { 618 + tim1 = regs->sdram_tim1_shdw_derated; 619 + tim3 = regs->sdram_tim3_shdw_derated; 620 + ref_ctrl = regs->ref_ctrl_shdw_derated; 621 + } 622 + 623 + out: 624 + writel(tim1, base + EMIF_SDRAM_TIMING_1_SHDW); 625 + writel(tim3, base + EMIF_SDRAM_TIMING_3_SHDW); 626 + writel(ref_ctrl, base + EMIF_SDRAM_REFRESH_CTRL_SHDW); 627 + } 54 628 55 629 static void get_default_timings(struct emif_data *emif) 56 630 { ··· 810 234 goto error; 811 235 } 812 236 813 - if (!emif1) 814 - emif1 = emif; 815 - 816 237 list_add(&emif->node, &device_list); 238 + emif->addressing = get_addressing_table(emif->plat_data->device_info); 817 239 818 240 /* Save pointers to each other in emif and device structures */ 819 241 emif->dev = &pdev->dev; ··· 831 257 goto error; 832 258 } 833 259 260 + /* One-time actions taken on probing the first device */ 261 + if (!emif1) { 262 + emif1 = emif; 263 + spin_lock_init(&emif_lock); 264 + 265 + /* 266 + * TODO: register notifiers for frequency and voltage 267 + * change here once the respective frameworks are 268 + * available 269 + */ 270 + } 271 + 834 272 dev_info(&pdev->dev, "%s: device configured with addr = %p\n", 835 273 __func__, emif->base); 836 274 837 275 return 0; 838 276 error: 839 277 return -ENODEV; 278 + } 279 + 280 + static int get_emif_reg_values(struct emif_data *emif, u32 freq, 281 + struct emif_regs *regs) 282 + { 283 + u32 cs1_used, ip_rev, phy_type; 284 + u32 cl, type; 285 + const struct lpddr2_timings *timings; 286 + const struct lpddr2_min_tck *min_tck; 287 + const struct ddr_device_info *device_info; 288 + const struct lpddr2_addressing *addressing; 289 + struct emif_data *emif_for_calc; 290 + struct device *dev; 291 + const struct emif_custom_configs *custom_configs; 292 + 293 + dev = emif->dev; 294 + /* 295 + * If the devices on this EMIF instance is duplicate of EMIF1, 296 + * use EMIF1 details for the calculation 297 + */ 298 + emif_for_calc = emif->duplicate ? emif1 : emif; 299 + timings = get_timings_table(emif_for_calc, freq); 300 + addressing = emif_for_calc->addressing; 301 + if (!timings || !addressing) { 302 + dev_err(dev, "%s: not enough data available for %dHz", 303 + __func__, freq); 304 + return -1; 305 + } 306 + 307 + device_info = emif_for_calc->plat_data->device_info; 308 + type = device_info->type; 309 + cs1_used = device_info->cs1_used; 310 + ip_rev = emif_for_calc->plat_data->ip_rev; 311 + phy_type = emif_for_calc->plat_data->phy_type; 312 + 313 + min_tck = emif_for_calc->plat_data->min_tck; 314 + custom_configs = emif_for_calc->plat_data->custom_configs; 315 + 316 + set_ddr_clk_period(freq); 317 + 318 + regs->ref_ctrl_shdw = get_sdram_ref_ctrl_shdw(freq, addressing); 319 + regs->sdram_tim1_shdw = get_sdram_tim_1_shdw(timings, min_tck, 320 + addressing); 321 + regs->sdram_tim2_shdw = get_sdram_tim_2_shdw(timings, min_tck, 322 + addressing, type); 323 + regs->sdram_tim3_shdw = get_sdram_tim_3_shdw(timings, min_tck, 324 + addressing, type, ip_rev, EMIF_NORMAL_TIMINGS); 325 + 326 + cl = get_cl(emif); 327 + 328 + if (phy_type == EMIF_PHY_TYPE_ATTILAPHY && ip_rev == EMIF_4D) { 329 + regs->phy_ctrl_1_shdw = get_ddr_phy_ctrl_1_attilaphy_4d( 330 + timings, freq, cl); 331 + } else if (phy_type == EMIF_PHY_TYPE_INTELLIPHY && ip_rev == EMIF_4D5) { 332 + regs->phy_ctrl_1_shdw = get_phy_ctrl_1_intelliphy_4d5(freq, cl); 333 + regs->ext_phy_ctrl_2_shdw = get_ext_phy_ctrl_2_intelliphy_4d5(); 334 + regs->ext_phy_ctrl_3_shdw = get_ext_phy_ctrl_3_intelliphy_4d5(); 335 + regs->ext_phy_ctrl_4_shdw = get_ext_phy_ctrl_4_intelliphy_4d5(); 336 + } else { 337 + return -1; 338 + } 339 + 340 + /* Only timeout values in pwr_mgmt_ctrl_shdw register */ 341 + regs->pwr_mgmt_ctrl_shdw = 342 + get_pwr_mgmt_ctrl(freq, emif_for_calc, ip_rev) & 343 + (CS_TIM_MASK | SR_TIM_MASK | PD_TIM_MASK); 344 + 345 + if (ip_rev & EMIF_4D) { 346 + regs->read_idle_ctrl_shdw_normal = 347 + get_read_idle_ctrl_shdw(DDR_VOLTAGE_STABLE); 348 + 349 + regs->read_idle_ctrl_shdw_volt_ramp = 350 + get_read_idle_ctrl_shdw(DDR_VOLTAGE_RAMPING); 351 + } else if (ip_rev & EMIF_4D5) { 352 + regs->dll_calib_ctrl_shdw_normal = 353 + get_dll_calib_ctrl_shdw(DDR_VOLTAGE_STABLE); 354 + 355 + regs->dll_calib_ctrl_shdw_volt_ramp = 356 + get_dll_calib_ctrl_shdw(DDR_VOLTAGE_RAMPING); 357 + } 358 + 359 + if (type == DDR_TYPE_LPDDR2_S2 || type == DDR_TYPE_LPDDR2_S4) { 360 + regs->ref_ctrl_shdw_derated = get_sdram_ref_ctrl_shdw(freq / 4, 361 + addressing); 362 + 363 + regs->sdram_tim1_shdw_derated = 364 + get_sdram_tim_1_shdw_derated(timings, min_tck, 365 + addressing); 366 + 367 + regs->sdram_tim3_shdw_derated = get_sdram_tim_3_shdw(timings, 368 + min_tck, addressing, type, ip_rev, 369 + EMIF_DERATED_TIMINGS); 370 + } 371 + 372 + regs->freq = freq; 373 + 374 + return 0; 375 + } 376 + 377 + /* 378 + * get_regs() - gets the cached emif_regs structure for a given EMIF instance 379 + * given frequency(freq): 380 + * 381 + * As an optimisation, every EMIF instance other than EMIF1 shares the 382 + * register cache with EMIF1 if the devices connected on this instance 383 + * are same as that on EMIF1(indicated by the duplicate flag) 384 + * 385 + * If we do not have an entry corresponding to the frequency given, we 386 + * allocate a new entry and calculate the values 387 + * 388 + * Upon finding the right reg dump, save it in curr_regs. It can be 389 + * directly used for thermal de-rating and voltage ramping changes. 390 + */ 391 + static struct emif_regs *get_regs(struct emif_data *emif, u32 freq) 392 + { 393 + int i; 394 + struct emif_regs **regs_cache; 395 + struct emif_regs *regs = NULL; 396 + struct device *dev; 397 + 398 + dev = emif->dev; 399 + if (emif->curr_regs && emif->curr_regs->freq == freq) { 400 + dev_dbg(dev, "%s: using curr_regs - %u Hz", __func__, freq); 401 + return emif->curr_regs; 402 + } 403 + 404 + if (emif->duplicate) 405 + regs_cache = emif1->regs_cache; 406 + else 407 + regs_cache = emif->regs_cache; 408 + 409 + for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++) { 410 + if (regs_cache[i]->freq == freq) { 411 + regs = regs_cache[i]; 412 + dev_dbg(dev, 413 + "%s: reg dump found in reg cache for %u Hz\n", 414 + __func__, freq); 415 + break; 416 + } 417 + } 418 + 419 + /* 420 + * If we don't have an entry for this frequency in the cache create one 421 + * and calculate the values 422 + */ 423 + if (!regs) { 424 + regs = devm_kzalloc(emif->dev, sizeof(*regs), GFP_ATOMIC); 425 + if (!regs) 426 + return NULL; 427 + 428 + if (get_emif_reg_values(emif, freq, regs)) { 429 + devm_kfree(emif->dev, regs); 430 + return NULL; 431 + } 432 + 433 + /* 434 + * Now look for an un-used entry in the cache and save the 435 + * newly created struct. If there are no free entries 436 + * over-write the last entry 437 + */ 438 + for (i = 0; i < EMIF_MAX_NUM_FREQUENCIES && regs_cache[i]; i++) 439 + ; 440 + 441 + if (i >= EMIF_MAX_NUM_FREQUENCIES) { 442 + dev_warn(dev, "%s: regs_cache full - reusing a slot!!\n", 443 + __func__); 444 + i = EMIF_MAX_NUM_FREQUENCIES - 1; 445 + devm_kfree(emif->dev, regs_cache[i]); 446 + } 447 + regs_cache[i] = regs; 448 + } 449 + 450 + return regs; 451 + } 452 + 453 + static void do_volt_notify_handling(struct emif_data *emif, u32 volt_state) 454 + { 455 + dev_dbg(emif->dev, "%s: voltage notification : %d", __func__, 456 + volt_state); 457 + 458 + if (!emif->curr_regs) { 459 + dev_err(emif->dev, 460 + "%s: volt-notify before registers are ready: %d\n", 461 + __func__, volt_state); 462 + return; 463 + } 464 + 465 + setup_volt_sensitive_regs(emif, emif->curr_regs, volt_state); 466 + } 467 + 468 + /* 469 + * TODO: voltage notify handling should be hooked up to 470 + * regulator framework as soon as the necessary support 471 + * is available in mainline kernel. This function is un-used 472 + * right now. 473 + */ 474 + static void __attribute__((unused)) volt_notify_handling(u32 volt_state) 475 + { 476 + struct emif_data *emif; 477 + 478 + spin_lock_irqsave(&emif_lock, irq_state); 479 + 480 + list_for_each_entry(emif, &device_list, node) 481 + do_volt_notify_handling(emif, volt_state); 482 + do_freq_update(); 483 + 484 + spin_unlock_irqrestore(&emif_lock, irq_state); 485 + } 486 + 487 + static void do_freq_pre_notify_handling(struct emif_data *emif, u32 new_freq) 488 + { 489 + struct emif_regs *regs; 490 + 491 + regs = get_regs(emif, new_freq); 492 + if (!regs) 493 + return; 494 + 495 + emif->curr_regs = regs; 496 + 497 + /* 498 + * Update the shadow registers: 499 + * Temperature and voltage-ramp sensitive settings are also configured 500 + * in terms of DDR cycles. So, we need to update them too when there 501 + * is a freq change 502 + */ 503 + dev_dbg(emif->dev, "%s: setting up shadow registers for %uHz", 504 + __func__, new_freq); 505 + setup_registers(emif, regs); 506 + setup_temperature_sensitive_regs(emif, regs); 507 + setup_volt_sensitive_regs(emif, regs, DDR_VOLTAGE_STABLE); 508 + 509 + /* 510 + * Part of workaround for errata i728. See do_freq_update() 511 + * for more details 512 + */ 513 + if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH) 514 + set_lpmode(emif, EMIF_LP_MODE_DISABLE); 515 + } 516 + 517 + /* 518 + * TODO: frequency notify handling should be hooked up to 519 + * clock framework as soon as the necessary support is 520 + * available in mainline kernel. This function is un-used 521 + * right now. 522 + */ 523 + static void __attribute__((unused)) freq_pre_notify_handling(u32 new_freq) 524 + { 525 + struct emif_data *emif; 526 + 527 + /* 528 + * NOTE: we are taking the spin-lock here and releases it 529 + * only in post-notifier. This doesn't look good and 530 + * Sparse complains about it, but this seems to be 531 + * un-avoidable. We need to lock a sequence of events 532 + * that is split between EMIF and clock framework. 533 + * 534 + * 1. EMIF driver updates EMIF timings in shadow registers in the 535 + * frequency pre-notify callback from clock framework 536 + * 2. clock framework sets up the registers for the new frequency 537 + * 3. clock framework initiates a hw-sequence that updates 538 + * the frequency EMIF timings synchronously. 539 + * 540 + * All these 3 steps should be performed as an atomic operation 541 + * vis-a-vis similar sequence in the EMIF interrupt handler 542 + * for temperature events. Otherwise, there could be race 543 + * conditions that could result in incorrect EMIF timings for 544 + * a given frequency 545 + */ 546 + spin_lock_irqsave(&emif_lock, irq_state); 547 + 548 + list_for_each_entry(emif, &device_list, node) 549 + do_freq_pre_notify_handling(emif, new_freq); 550 + } 551 + 552 + static void do_freq_post_notify_handling(struct emif_data *emif) 553 + { 554 + /* 555 + * Part of workaround for errata i728. See do_freq_update() 556 + * for more details 557 + */ 558 + if (emif->lpmode == EMIF_LP_MODE_SELF_REFRESH) 559 + set_lpmode(emif, EMIF_LP_MODE_SELF_REFRESH); 560 + } 561 + 562 + /* 563 + * TODO: frequency notify handling should be hooked up to 564 + * clock framework as soon as the necessary support is 565 + * available in mainline kernel. This function is un-used 566 + * right now. 567 + */ 568 + static void __attribute__((unused)) freq_post_notify_handling(void) 569 + { 570 + struct emif_data *emif; 571 + 572 + list_for_each_entry(emif, &device_list, node) 573 + do_freq_post_notify_handling(emif); 574 + 575 + /* 576 + * Lock is done in pre-notify handler. See freq_pre_notify_handling() 577 + * for more details 578 + */ 579 + spin_unlock_irqrestore(&emif_lock, irq_state); 840 580 } 841 581 842 582 static struct platform_driver emif_driver = {
+129 -1
drivers/memory/emif.h
··· 19 19 */ 20 20 #define EMIF_MAX_NUM_FREQUENCIES 6 21 21 22 + /* State of the core voltage */ 23 + #define DDR_VOLTAGE_STABLE 0 24 + #define DDR_VOLTAGE_RAMPING 1 25 + 26 + /* Defines for timing De-rating */ 27 + #define EMIF_NORMAL_TIMINGS 0 28 + #define EMIF_DERATED_TIMINGS 1 29 + 30 + /* Length of the forced read idle period in terms of cycles */ 31 + #define EMIF_READ_IDLE_LEN_VAL 5 32 + 33 + /* 34 + * forced read idle interval to be used when voltage 35 + * is changed as part of DVFS/DPS - 1ms 36 + */ 37 + #define READ_IDLE_INTERVAL_DVFS (1*1000000) 38 + 39 + /* 40 + * Forced read idle interval to be used when voltage is stable 41 + * 50us - or maximum value will do 42 + */ 43 + #define READ_IDLE_INTERVAL_NORMAL (50*1000000) 44 + 45 + /* DLL calibration interval when voltage is NOT stable - 1us */ 46 + #define DLL_CALIB_INTERVAL_DVFS (1*1000000) 47 + 48 + #define DLL_CALIB_ACK_WAIT_VAL 5 49 + 50 + /* Interval between ZQCS commands - hw team recommended value */ 51 + #define EMIF_ZQCS_INTERVAL_US (50*1000) 52 + /* Enable ZQ Calibration on exiting Self-refresh */ 53 + #define ZQ_SFEXITEN_ENABLE 1 54 + /* 55 + * ZQ Calibration simultaneously on both chip-selects: 56 + * Needs one calibration resistor per CS 57 + */ 58 + #define ZQ_DUALCALEN_DISABLE 0 59 + #define ZQ_DUALCALEN_ENABLE 1 60 + 61 + #define T_ZQCS_DEFAULT_NS 90 62 + #define T_ZQCL_DEFAULT_NS 360 63 + #define T_ZQINIT_DEFAULT_NS 1000 64 + 65 + /* DPD_EN */ 66 + #define DPD_DISABLE 0 67 + #define DPD_ENABLE 1 68 + 69 + /* 70 + * Default values for the low-power entry to be used if not provided by user. 71 + * OMAP4/5 has a hw bug(i735) due to which this value can not be less than 512 72 + * Timeout values are in DDR clock 'cycles' and frequency threshold in Hz 73 + */ 74 + #define EMIF_LP_MODE_TIMEOUT_PERFORMANCE 2048 75 + #define EMIF_LP_MODE_TIMEOUT_POWER 512 76 + #define EMIF_LP_MODE_FREQ_THRESHOLD 400000000 77 + 78 + /* DDR_PHY_CTRL_1 values for EMIF4D - ATTILA PHY combination */ 79 + #define EMIF_DDR_PHY_CTRL_1_BASE_VAL_ATTILAPHY 0x049FF000 80 + #define EMIF_DLL_SLAVE_DLY_CTRL_400_MHZ_ATTILAPHY 0x41 81 + #define EMIF_DLL_SLAVE_DLY_CTRL_200_MHZ_ATTILAPHY 0x80 82 + #define EMIF_DLL_SLAVE_DLY_CTRL_100_MHZ_AND_LESS_ATTILAPHY 0xFF 83 + 84 + /* DDR_PHY_CTRL_1 values for EMIF4D5 INTELLIPHY combination */ 85 + #define EMIF_DDR_PHY_CTRL_1_BASE_VAL_INTELLIPHY 0x0E084200 86 + #define EMIF_PHY_TOTAL_READ_LATENCY_INTELLIPHY_PS 10000 87 + 88 + /* TEMP_ALERT_CONFIG - corresponding to temp gradient 5 C/s */ 89 + #define TEMP_ALERT_POLL_INTERVAL_DEFAULT_MS 360 90 + 91 + #define EMIF_T_CSTA 3 92 + #define EMIF_T_PDLL_UL 128 93 + 94 + /* External PHY control registers magic values */ 95 + #define EMIF_EXT_PHY_CTRL_1_VAL 0x04020080 96 + #define EMIF_EXT_PHY_CTRL_5_VAL 0x04010040 97 + #define EMIF_EXT_PHY_CTRL_6_VAL 0x01004010 98 + #define EMIF_EXT_PHY_CTRL_7_VAL 0x00001004 99 + #define EMIF_EXT_PHY_CTRL_8_VAL 0x04010040 100 + #define EMIF_EXT_PHY_CTRL_9_VAL 0x01004010 101 + #define EMIF_EXT_PHY_CTRL_10_VAL 0x00001004 102 + #define EMIF_EXT_PHY_CTRL_11_VAL 0x00000000 103 + #define EMIF_EXT_PHY_CTRL_12_VAL 0x00000000 104 + #define EMIF_EXT_PHY_CTRL_13_VAL 0x00000000 105 + #define EMIF_EXT_PHY_CTRL_14_VAL 0x80080080 106 + #define EMIF_EXT_PHY_CTRL_15_VAL 0x00800800 107 + #define EMIF_EXT_PHY_CTRL_16_VAL 0x08102040 108 + #define EMIF_EXT_PHY_CTRL_17_VAL 0x00000001 109 + #define EMIF_EXT_PHY_CTRL_18_VAL 0x540A8150 110 + #define EMIF_EXT_PHY_CTRL_19_VAL 0xA81502A0 111 + #define EMIF_EXT_PHY_CTRL_20_VAL 0x002A0540 112 + #define EMIF_EXT_PHY_CTRL_21_VAL 0x00000000 113 + #define EMIF_EXT_PHY_CTRL_22_VAL 0x00000000 114 + #define EMIF_EXT_PHY_CTRL_23_VAL 0x00000000 115 + #define EMIF_EXT_PHY_CTRL_24_VAL 0x00000077 116 + 117 + #define EMIF_INTELLI_PHY_DQS_GATE_OPENING_DELAY_PS 1200 118 + 22 119 /* Registers offset */ 23 120 #define EMIF_MODULE_ID_AND_REVISION 0x0000 24 121 #define EMIF_STATUS 0x0004 ··· 555 458 #define READ_LATENCY_SHDW_SHIFT 0 556 459 #define READ_LATENCY_SHDW_MASK (0x1f << 0) 557 460 558 - #endif 461 + #ifndef __ASSEMBLY__ 462 + /* 463 + * Structure containing shadow of important registers in EMIF 464 + * The calculation function fills in this structure to be later used for 465 + * initialisation and DVFS 466 + */ 467 + struct emif_regs { 468 + u32 freq; 469 + u32 ref_ctrl_shdw; 470 + u32 ref_ctrl_shdw_derated; 471 + u32 sdram_tim1_shdw; 472 + u32 sdram_tim1_shdw_derated; 473 + u32 sdram_tim2_shdw; 474 + u32 sdram_tim3_shdw; 475 + u32 sdram_tim3_shdw_derated; 476 + u32 pwr_mgmt_ctrl_shdw; 477 + union { 478 + u32 read_idle_ctrl_shdw_normal; 479 + u32 dll_calib_ctrl_shdw_normal; 480 + }; 481 + union { 482 + u32 read_idle_ctrl_shdw_volt_ramp; 483 + u32 dll_calib_ctrl_shdw_volt_ramp; 484 + }; 485 + 486 + u32 phy_ctrl_1_shdw; 487 + u32 ext_phy_ctrl_2_shdw; 488 + u32 ext_phy_ctrl_3_shdw; 489 + u32 ext_phy_ctrl_4_shdw; 490 + }; 491 + #endif /* __ASSEMBLY__ */ 492 + #endif /* __EMIF_H */