Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Andy Fleming | 3c98e7b | 2015-11-04 15:48:32 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Based on corenet_ds ddr code |
Andy Fleming | 3c98e7b | 2015-11-04 15:48:32 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <i2c.h> |
| 8 | #include <hwconfig.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 9 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Andy Fleming | 3c98e7b | 2015-11-04 15:48:32 -0600 | [diff] [blame] | 11 | #include <asm/mmu.h> |
| 12 | #include <fsl_ddr_sdram.h> |
| 13 | #include <fsl_ddr_dimm_params.h> |
| 14 | #include <asm/fsl_law.h> |
| 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | |
| 19 | struct board_specific_parameters { |
| 20 | u32 n_ranks; |
| 21 | u32 datarate_mhz_high; |
| 22 | u32 clk_adjust; |
| 23 | u32 wrlvl_start; |
| 24 | u32 cpo; |
| 25 | u32 write_data_delay; |
| 26 | u32 force_2t; |
| 27 | }; |
| 28 | |
| 29 | /* |
| 30 | * This table contains all valid speeds we want to override with board |
| 31 | * specific parameters. datarate_mhz_high values need to be in ascending order |
| 32 | * for each n_ranks group. |
| 33 | */ |
| 34 | static const struct board_specific_parameters udimm0[] = { |
| 35 | /* |
| 36 | * memory controller 0 |
| 37 | * num| hi| clk| wrlvl | cpo |wrdata|2T |
| 38 | * ranks| mhz|adjst| start | |delay | |
| 39 | */ |
| 40 | {4, 850, 4, 6, 0xff, 2, 0}, |
| 41 | {4, 950, 5, 7, 0xff, 2, 0}, |
| 42 | {4, 1050, 5, 8, 0xff, 2, 0}, |
| 43 | {4, 1250, 5, 10, 0xff, 2, 0}, |
| 44 | {4, 1350, 5, 11, 0xff, 2, 0}, |
| 45 | {4, 1666, 5, 12, 0xff, 2, 0}, |
| 46 | {2, 850, 5, 6, 0xff, 2, 0}, |
| 47 | {2, 1050, 5, 7, 0xff, 2, 0}, |
| 48 | {2, 1250, 4, 6, 0xff, 2, 0}, |
| 49 | {2, 1350, 5, 7, 0xff, 2, 0}, |
| 50 | {2, 1666, 5, 8, 0xff, 2, 0}, |
| 51 | {1, 1250, 4, 6, 0xff, 2, 0}, |
| 52 | {1, 1335, 4, 7, 0xff, 2, 0}, |
| 53 | {1, 1666, 4, 8, 0xff, 2, 0}, |
| 54 | {} |
| 55 | }; |
| 56 | |
| 57 | /* |
| 58 | * The two slots have slightly different timing. The center values are good |
| 59 | * for both slots. We use identical speed tables for them. In future use, if |
| 60 | * DIMMs have fewer center values that require two separated tables, copy the |
| 61 | * udimm0 table to udimm1 and make changes to clk_adjust and wrlvl_start. |
| 62 | */ |
| 63 | static const struct board_specific_parameters *udimms[] = { |
| 64 | udimm0, |
| 65 | udimm0, |
| 66 | }; |
| 67 | |
| 68 | static const struct board_specific_parameters rdimm0[] = { |
| 69 | /* |
| 70 | * memory controller 0 |
| 71 | * num| hi| clk| wrlvl | cpo |wrdata|2T |
| 72 | * ranks| mhz|adjst| start | |delay | |
| 73 | */ |
| 74 | {4, 850, 4, 6, 0xff, 2, 0}, |
| 75 | {4, 950, 5, 7, 0xff, 2, 0}, |
| 76 | {4, 1050, 5, 8, 0xff, 2, 0}, |
| 77 | {4, 1250, 5, 10, 0xff, 2, 0}, |
| 78 | {4, 1350, 5, 11, 0xff, 2, 0}, |
| 79 | {4, 1666, 5, 12, 0xff, 2, 0}, |
| 80 | {2, 850, 4, 6, 0xff, 2, 0}, |
| 81 | {2, 1050, 4, 7, 0xff, 2, 0}, |
| 82 | {2, 1666, 4, 8, 0xff, 2, 0}, |
| 83 | {1, 850, 4, 5, 0xff, 2, 0}, |
| 84 | {1, 950, 4, 7, 0xff, 2, 0}, |
| 85 | {1, 1666, 4, 8, 0xff, 2, 0}, |
| 86 | {} |
| 87 | }; |
| 88 | |
| 89 | /* |
| 90 | * The two slots have slightly different timing. See comments above. |
| 91 | */ |
| 92 | static const struct board_specific_parameters *rdimms[] = { |
| 93 | rdimm0, |
| 94 | rdimm0, |
| 95 | }; |
| 96 | |
| 97 | void fsl_ddr_board_options(memctl_options_t *popts, |
| 98 | dimm_params_t *pdimm, |
| 99 | unsigned int ctrl_num) |
| 100 | { |
| 101 | const struct board_specific_parameters *pbsp, *pbsp_highest = NULL; |
| 102 | ulong ddr_freq; |
| 103 | |
| 104 | if (ctrl_num > 1) { |
| 105 | printf("Wrong parameter for controller number %d", ctrl_num); |
| 106 | return; |
| 107 | } |
| 108 | if (!pdimm->n_ranks) |
| 109 | return; |
| 110 | |
| 111 | if (popts->registered_dimm_en) |
| 112 | pbsp = rdimms[ctrl_num]; |
| 113 | else |
| 114 | pbsp = udimms[ctrl_num]; |
| 115 | |
| 116 | |
| 117 | /* Get clk_adjust, cpo, write_data_delay,2T, according to the board ddr |
| 118 | * freqency and n_banks specified in board_specific_parameters table. |
| 119 | */ |
| 120 | ddr_freq = get_ddr_freq(0) / 1000000; |
| 121 | while (pbsp->datarate_mhz_high) { |
| 122 | if (pbsp->n_ranks == pdimm->n_ranks) { |
| 123 | if (ddr_freq <= pbsp->datarate_mhz_high) { |
| 124 | popts->cpo_override = pbsp->cpo; |
| 125 | popts->write_data_delay = |
| 126 | pbsp->write_data_delay; |
| 127 | popts->clk_adjust = pbsp->clk_adjust; |
| 128 | popts->wrlvl_start = pbsp->wrlvl_start; |
| 129 | popts->twot_en = pbsp->force_2t; |
| 130 | goto found; |
| 131 | } |
| 132 | pbsp_highest = pbsp; |
| 133 | } |
| 134 | pbsp++; |
| 135 | } |
| 136 | |
| 137 | if (pbsp_highest) { |
| 138 | printf("Error: board specific timing not found for data rate %lu MT/s!\nTrying to use the highest speed (%u) parameters\n", |
| 139 | ddr_freq, pbsp_highest->datarate_mhz_high); |
| 140 | popts->cpo_override = pbsp_highest->cpo; |
| 141 | popts->write_data_delay = pbsp_highest->write_data_delay; |
| 142 | popts->clk_adjust = pbsp_highest->clk_adjust; |
| 143 | popts->wrlvl_start = pbsp_highest->wrlvl_start; |
| 144 | popts->twot_en = pbsp_highest->force_2t; |
| 145 | } else { |
| 146 | panic("DIMM is not supported by this board"); |
| 147 | } |
| 148 | found: |
| 149 | /* |
| 150 | * Factors to consider for half-strength driver enable: |
| 151 | * - number of DIMMs installed |
| 152 | */ |
| 153 | popts->half_strength_driver_enable = 0; |
| 154 | /* |
| 155 | * Write leveling override |
| 156 | */ |
| 157 | popts->wrlvl_override = 1; |
| 158 | popts->wrlvl_sample = 0xf; |
| 159 | |
| 160 | /* |
| 161 | * Rtt and Rtt_WR override |
| 162 | */ |
| 163 | popts->rtt_override = 0; |
| 164 | |
| 165 | /* Enable ZQ calibration */ |
| 166 | popts->zq_en = 1; |
| 167 | |
| 168 | /* DHC_EN =1, ODT = 60 Ohm */ |
| 169 | popts->ddr_cdr1 = DDR_CDR1_DHC_EN; |
| 170 | } |
| 171 | |
Simon Glass | d35f338 | 2017-04-06 12:47:05 -0600 | [diff] [blame] | 172 | int dram_init(void) |
Andy Fleming | 3c98e7b | 2015-11-04 15:48:32 -0600 | [diff] [blame] | 173 | { |
| 174 | phys_size_t dram_size; |
| 175 | |
| 176 | puts("Initializing...."); |
| 177 | |
| 178 | if (!fsl_use_spd()) |
| 179 | panic("Cyrus only supports using SPD for DRAM\n"); |
| 180 | |
| 181 | puts("using SPD\n"); |
| 182 | dram_size = fsl_ddr_sdram(); |
| 183 | |
| 184 | dram_size = setup_ddr_tlbs(dram_size / 0x100000); |
| 185 | dram_size *= 0x100000; |
| 186 | |
| 187 | debug(" DDR: "); |
Simon Glass | 39f90ba | 2017-03-31 08:40:25 -0600 | [diff] [blame] | 188 | gd->ram_size = dram_size; |
| 189 | |
| 190 | return 0; |
Andy Fleming | 3c98e7b | 2015-11-04 15:48:32 -0600 | [diff] [blame] | 191 | } |