Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ashish Kumar | 227b4bc | 2017-08-31 16:12:54 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2017 NXP |
Ashish Kumar | 227b4bc | 2017-08-31 16:12:54 +0530 | [diff] [blame] | 4 | */ |
| 5 | |
Ashish Kumar | 227b4bc | 2017-08-31 16:12:54 +0530 | [diff] [blame] | 6 | #include <fsl_ddr_sdram.h> |
| 7 | #include <fsl_ddr_dimm_params.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Ashish Kumar | 227b4bc | 2017-08-31 16:12:54 +0530 | [diff] [blame] | 9 | #include <asm/arch/soc.h> |
| 10 | #include <asm/arch/clock.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Ashish Kumar | 227b4bc | 2017-08-31 16:12:54 +0530 | [diff] [blame] | 12 | #include "ddr.h" |
| 13 | |
| 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Rajesh Bhagat | b1fdc93 | 2018-01-17 16:13:07 +0530 | [diff] [blame] | 16 | #if defined(CONFIG_VID) && (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)) |
| 17 | static void fsl_ddr_setup_0v9_volt(memctl_options_t *popts) |
| 18 | { |
| 19 | int vdd; |
| 20 | |
| 21 | vdd = get_core_volt_from_fuse(); |
| 22 | /* Nothing to do for silicons doesn't support VID */ |
| 23 | if (vdd < 0) |
| 24 | return; |
| 25 | |
| 26 | if (vdd == 900) { |
| 27 | popts->ddr_cdr1 |= DDR_CDR1_V0PT9_EN; |
| 28 | debug("VID: configure DDR to support 900 mV\n"); |
| 29 | } |
| 30 | } |
| 31 | #endif |
| 32 | |
Ashish Kumar | 227b4bc | 2017-08-31 16:12:54 +0530 | [diff] [blame] | 33 | void fsl_ddr_board_options(memctl_options_t *popts, |
| 34 | dimm_params_t *pdimm, |
| 35 | unsigned int ctrl_num) |
| 36 | { |
| 37 | const struct board_specific_parameters *pbsp, *pbsp_highest = NULL; |
| 38 | ulong ddr_freq; |
| 39 | |
| 40 | if (ctrl_num > 1) { |
| 41 | printf("Not supported controller number %d\n", ctrl_num); |
| 42 | return; |
| 43 | } |
| 44 | if (!pdimm->n_ranks) |
| 45 | return; |
| 46 | |
| 47 | /* |
| 48 | * we use identical timing for all slots. If needed, change the code |
| 49 | * to pbsp = rdimms[ctrl_num] or pbsp = udimms[ctrl_num]; |
| 50 | */ |
| 51 | pbsp = udimms[0]; |
| 52 | |
| 53 | /* Get clk_adjust, wrlvl_start, wrlvl_ctl, according to the board ddr |
| 54 | * freqency and n_banks specified in board_specific_parameters table. |
| 55 | */ |
| 56 | ddr_freq = get_ddr_freq(0) / 1000000; |
| 57 | while (pbsp->datarate_mhz_high) { |
| 58 | if (pbsp->n_ranks == pdimm->n_ranks) { |
| 59 | if (ddr_freq <= pbsp->datarate_mhz_high) { |
| 60 | popts->clk_adjust = pbsp->clk_adjust; |
| 61 | popts->wrlvl_start = pbsp->wrlvl_start; |
| 62 | popts->wrlvl_ctl_2 = pbsp->wrlvl_ctl_2; |
| 63 | popts->wrlvl_ctl_3 = pbsp->wrlvl_ctl_3; |
| 64 | goto found; |
| 65 | } |
| 66 | pbsp_highest = pbsp; |
| 67 | } |
| 68 | pbsp++; |
| 69 | } |
| 70 | |
| 71 | if (pbsp_highest) { |
| 72 | printf("Error: board specific timing not found for %lu MT/s\n", |
| 73 | ddr_freq); |
| 74 | printf("Trying to use the highest speed (%u) parameters\n", |
| 75 | pbsp_highest->datarate_mhz_high); |
| 76 | popts->clk_adjust = pbsp_highest->clk_adjust; |
| 77 | popts->wrlvl_start = pbsp_highest->wrlvl_start; |
| 78 | popts->wrlvl_ctl_2 = pbsp->wrlvl_ctl_2; |
| 79 | popts->wrlvl_ctl_3 = pbsp->wrlvl_ctl_3; |
| 80 | } else { |
| 81 | panic("DIMM is not supported by this board"); |
| 82 | } |
| 83 | found: |
| 84 | debug("Found timing match: n_ranks %d, data rate %d, rank_gb %d\n" |
| 85 | "\tclk_adjust %d, wrlvl_start %d, wrlvl_ctrl_2 0x%x, wrlvl_ctrl_3 0x%x\n", |
| 86 | pbsp->n_ranks, pbsp->datarate_mhz_high, pbsp->rank_gb, |
| 87 | pbsp->clk_adjust, pbsp->wrlvl_start, pbsp->wrlvl_ctl_2, |
| 88 | pbsp->wrlvl_ctl_3); |
| 89 | |
Ashish Kumar | 227b4bc | 2017-08-31 16:12:54 +0530 | [diff] [blame] | 90 | popts->half_strength_driver_enable = 0; |
| 91 | /* |
| 92 | * Write leveling override |
| 93 | */ |
| 94 | popts->wrlvl_override = 1; |
| 95 | popts->wrlvl_sample = 0xf; |
| 96 | |
| 97 | |
| 98 | /* Enable ZQ calibration */ |
| 99 | popts->zq_en = 1; |
| 100 | |
| 101 | /* Enable DDR hashing */ |
| 102 | popts->addr_hash = 1; |
| 103 | |
| 104 | popts->ddr_cdr1 = DDR_CDR1_DHC_EN | DDR_CDR1_ODT(DDR_CDR_ODT_60ohm); |
Rajesh Bhagat | b1fdc93 | 2018-01-17 16:13:07 +0530 | [diff] [blame] | 105 | #if defined(CONFIG_VID) && (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)) |
| 106 | fsl_ddr_setup_0v9_volt(popts); |
| 107 | #endif |
| 108 | |
Ashish Kumar | 227b4bc | 2017-08-31 16:12:54 +0530 | [diff] [blame] | 109 | popts->ddr_cdr2 = DDR_CDR2_ODT(DDR_CDR_ODT_60ohm) | |
| 110 | DDR_CDR2_VREF_TRAIN_EN | DDR_CDR2_VREF_RANGE_2; |
| 111 | } |
| 112 | |
Pankit Garg | f5c2a83 | 2018-12-27 04:37:55 +0000 | [diff] [blame] | 113 | #ifdef CONFIG_TFABOOT |
| 114 | int fsl_initdram(void) |
| 115 | { |
| 116 | gd->ram_size = tfa_get_dram_size(); |
| 117 | |
| 118 | if (!gd->ram_size) |
| 119 | gd->ram_size = fsl_ddr_sdram_size(); |
Ashish Kumar | 227b4bc | 2017-08-31 16:12:54 +0530 | [diff] [blame] | 120 | |
Pankit Garg | f5c2a83 | 2018-12-27 04:37:55 +0000 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | #else |
Ashish Kumar | 227b4bc | 2017-08-31 16:12:54 +0530 | [diff] [blame] | 124 | int fsl_initdram(void) |
| 125 | { |
| 126 | puts("Initializing DDR....using SPD\n"); |
| 127 | |
Ashish Kumar | 5676ceb | 2017-11-06 13:18:43 +0530 | [diff] [blame] | 128 | #if defined(CONFIG_SPL) && !defined(CONFIG_SPL_BUILD) |
| 129 | gd->ram_size = fsl_ddr_sdram_size(); |
| 130 | #else |
Ashish Kumar | 227b4bc | 2017-08-31 16:12:54 +0530 | [diff] [blame] | 131 | gd->ram_size = fsl_ddr_sdram(); |
Ashish Kumar | 5676ceb | 2017-11-06 13:18:43 +0530 | [diff] [blame] | 132 | #endif |
Ashish Kumar | 227b4bc | 2017-08-31 16:12:54 +0530 | [diff] [blame] | 133 | return 0; |
| 134 | } |
Pankit Garg | f5c2a83 | 2018-12-27 04:37:55 +0000 | [diff] [blame] | 135 | #endif /* CONFIG_TFABOOT */ |