blob: 31711fdd4981998cf1ba9b107a1365485d6c9cb6 [file] [log] [blame]
Chris Packham1a07d212018-05-10 13:28:29 +12001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) Marvell International Ltd. and its affiliates
4 */
Chris Packham4bf81db2018-12-03 14:26:49 +13005#include "ddr_ml_wrapper.h"
6#include "mv_ddr_plat.h"
Chris Packham1a07d212018-05-10 13:28:29 +12007
8#include "mv_ddr_topology.h"
9#include "mv_ddr_common.h"
10#include "mv_ddr_spd.h"
Chris Packham1a07d212018-05-10 13:28:29 +120011#include "ddr_topology_def.h"
12#include "ddr3_training_ip_db.h"
13#include "ddr3_training_ip.h"
Chris Packham4bf81db2018-12-03 14:26:49 +130014#include "mv_ddr_training_db.h"
Chris Packham1a07d212018-05-10 13:28:29 +120015
16unsigned int mv_ddr_cl_calc(unsigned int taa_min, unsigned int tclk)
17{
18 unsigned int cl = ceil_div(taa_min, tclk);
19
20 return mv_ddr_spd_supported_cl_get(cl);
21
22}
23
24unsigned int mv_ddr_cwl_calc(unsigned int tclk)
25{
26 unsigned int cwl;
27
28 if (tclk >= 1250)
29 cwl = 9;
30 else if (tclk >= 1071)
31 cwl = 10;
32 else if (tclk >= 938)
33 cwl = 11;
34 else if (tclk >= 833)
35 cwl = 12;
Chris Packham4bf81db2018-12-03 14:26:49 +130036 else if (tclk >= 750)
37 cwl = 14;
38 else if (tclk >= 625)
39 cwl = 16;
Chris Packham1a07d212018-05-10 13:28:29 +120040 else
41 cwl = 0;
42
43 return cwl;
44}
45
Chris Packham4bf81db2018-12-03 14:26:49 +130046int mv_ddr_topology_map_update(void)
Chris Packham1a07d212018-05-10 13:28:29 +120047{
48 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
Chris Packham4bf81db2018-12-03 14:26:49 +130049 struct if_params *iface_params = &(tm->interface_params[0]);
Chris Packham1a07d212018-05-10 13:28:29 +120050 unsigned int octets_per_if_num = ddr3_tip_dev_attr_get(0, MV_ATTR_OCTET_PER_INTERFACE);
Chris Packham4bf81db2018-12-03 14:26:49 +130051 enum mv_ddr_speed_bin speed_bin_index;
52 enum mv_ddr_freq freq = MV_DDR_FREQ_LAST;
Chris Packham1a07d212018-05-10 13:28:29 +120053 unsigned int tclk;
54 unsigned char val = 0;
55 int i;
56
Chris Packham4bf81db2018-12-03 14:26:49 +130057 if (iface_params->memory_freq == MV_DDR_FREQ_SAR)
58 iface_params->memory_freq = mv_ddr_init_freq_get();
Chris Packham1a07d212018-05-10 13:28:29 +120059
60 if (tm->cfg_src == MV_DDR_CFG_SPD) {
61 /* check dram device type */
62 val = mv_ddr_spd_dev_type_get(&tm->spd_data);
63 if (val != MV_DDR_SPD_DEV_TYPE_DDR4) {
64 printf("mv_ddr: unsupported dram device type found\n");
Chris Packham4bf81db2018-12-03 14:26:49 +130065 return -1;
Chris Packham1a07d212018-05-10 13:28:29 +120066 }
67
68 /* update topology map with timing data */
69 if (mv_ddr_spd_timing_calc(&tm->spd_data, tm->timing_data) > 0) {
70 printf("mv_ddr: negative timing data found\n");
Chris Packham4bf81db2018-12-03 14:26:49 +130071 return -1;
Chris Packham1a07d212018-05-10 13:28:29 +120072 }
73
74 /* update device width in topology map */
Chris Packham4bf81db2018-12-03 14:26:49 +130075 iface_params->bus_width = mv_ddr_spd_dev_width_get(&tm->spd_data);
Chris Packham1a07d212018-05-10 13:28:29 +120076
77 /* update die capacity in topology map */
Chris Packham4bf81db2018-12-03 14:26:49 +130078 iface_params->memory_size = mv_ddr_spd_die_capacity_get(&tm->spd_data);
Chris Packham1a07d212018-05-10 13:28:29 +120079
80 /* update bus bit mask in topology map */
81 tm->bus_act_mask = mv_ddr_bus_bit_mask_get();
82
83 /* update cs bit mask in topology map */
84 val = mv_ddr_spd_cs_bit_mask_get(&tm->spd_data);
Chris Packham4bf81db2018-12-03 14:26:49 +130085 for (i = 0; i < octets_per_if_num; i++)
86 iface_params->as_bus_params[i].cs_bitmask = val;
Chris Packham1a07d212018-05-10 13:28:29 +120087
88 /* check dram module type */
89 val = mv_ddr_spd_module_type_get(&tm->spd_data);
90 switch (val) {
91 case MV_DDR_SPD_MODULE_TYPE_UDIMM:
92 case MV_DDR_SPD_MODULE_TYPE_SO_DIMM:
93 case MV_DDR_SPD_MODULE_TYPE_MINI_UDIMM:
94 case MV_DDR_SPD_MODULE_TYPE_72BIT_SO_UDIMM:
95 case MV_DDR_SPD_MODULE_TYPE_16BIT_SO_DIMM:
96 case MV_DDR_SPD_MODULE_TYPE_32BIT_SO_DIMM:
97 break;
98 default:
99 printf("mv_ddr: unsupported dram module type found\n");
Chris Packham4bf81db2018-12-03 14:26:49 +1300100 return -1;
Chris Packham1a07d212018-05-10 13:28:29 +1200101 }
102
103 /* update mirror bit mask in topology map */
104 val = mv_ddr_spd_mem_mirror_get(&tm->spd_data);
Chris Packham4bf81db2018-12-03 14:26:49 +1300105 for (i = 0; i < octets_per_if_num; i++)
106 iface_params->as_bus_params[i].mirror_enable_bitmask = val << 1;
Chris Packham1a07d212018-05-10 13:28:29 +1200107
Chris Packham4bf81db2018-12-03 14:26:49 +1300108 tclk = 1000000 / mv_ddr_freq_get(iface_params->memory_freq);
Chris Packham1a07d212018-05-10 13:28:29 +1200109 /* update cas write latency (cwl) */
110 val = mv_ddr_cwl_calc(tclk);
111 if (val == 0) {
112 printf("mv_ddr: unsupported cas write latency value found\n");
Chris Packham4bf81db2018-12-03 14:26:49 +1300113 return -1;
Chris Packham1a07d212018-05-10 13:28:29 +1200114 }
Chris Packham4bf81db2018-12-03 14:26:49 +1300115 iface_params->cas_wl = val;
Chris Packham1a07d212018-05-10 13:28:29 +1200116
117 /* update cas latency (cl) */
118 mv_ddr_spd_supported_cls_calc(&tm->spd_data);
119 val = mv_ddr_cl_calc(tm->timing_data[MV_DDR_TAA_MIN], tclk);
120 if (val == 0) {
121 printf("mv_ddr: unsupported cas latency value found\n");
Chris Packham4bf81db2018-12-03 14:26:49 +1300122 return -1;
Chris Packham1a07d212018-05-10 13:28:29 +1200123 }
Chris Packham4bf81db2018-12-03 14:26:49 +1300124 iface_params->cas_l = val;
Chris Packham1a07d212018-05-10 13:28:29 +1200125 } else if (tm->cfg_src == MV_DDR_CFG_DEFAULT) {
126 /* set cas and cas-write latencies per speed bin, if they unset */
Chris Packham4bf81db2018-12-03 14:26:49 +1300127 speed_bin_index = iface_params->speed_bin_index;
128 freq = iface_params->memory_freq;
Chris Packham1a07d212018-05-10 13:28:29 +1200129
Chris Packham4bf81db2018-12-03 14:26:49 +1300130 if (iface_params->cas_l == 0)
131 iface_params->cas_l = mv_ddr_cl_val_get(speed_bin_index, freq);
Chris Packham1a07d212018-05-10 13:28:29 +1200132
Chris Packham4bf81db2018-12-03 14:26:49 +1300133 if (iface_params->cas_wl == 0)
134 iface_params->cas_wl = mv_ddr_cwl_val_get(speed_bin_index, freq);
Chris Packham1a07d212018-05-10 13:28:29 +1200135 }
136
Chris Packham4bf81db2018-12-03 14:26:49 +1300137 return 0;
Chris Packham1a07d212018-05-10 13:28:29 +1200138}
139
140unsigned short mv_ddr_bus_bit_mask_get(void)
141{
142 unsigned short pri_and_ext_bus_width = 0x0;
143 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
144 unsigned int octets_per_if_num = ddr3_tip_dev_attr_get(0, MV_ATTR_OCTET_PER_INTERFACE);
145
146 if (tm->cfg_src == MV_DDR_CFG_SPD) {
Alex Leibovich7eccedb2021-02-19 17:11:12 +0100147 if (tm->bus_act_mask == MV_DDR_32BIT_ECC_PUP8_BUS_MASK)
Moti Buskila4b58bf02021-02-19 17:11:14 +0100148 tm->spd_data.byte_fields.byte_13.bit_fields.primary_bus_width = MV_DDR_PRI_BUS_WIDTH_32;
Alex Leibovich05ee0ad2021-02-19 17:11:11 +0100149
Chris Packham1a07d212018-05-10 13:28:29 +1200150 enum mv_ddr_pri_bus_width pri_bus_width = mv_ddr_spd_pri_bus_width_get(&tm->spd_data);
151 enum mv_ddr_bus_width_ext bus_width_ext = mv_ddr_spd_bus_width_ext_get(&tm->spd_data);
152
153 switch (pri_bus_width) {
154 case MV_DDR_PRI_BUS_WIDTH_16:
155 pri_and_ext_bus_width = BUS_MASK_16BIT;
156 break;
Alex Leibovich05ee0ad2021-02-19 17:11:11 +0100157 case MV_DDR_PRI_BUS_WIDTH_32: /*each bit represents byte, so 0xf-is means 4 bytes-32 bit*/
Chris Packham1a07d212018-05-10 13:28:29 +1200158 pri_and_ext_bus_width = BUS_MASK_32BIT;
159 break;
160 case MV_DDR_PRI_BUS_WIDTH_64:
161 pri_and_ext_bus_width = MV_DDR_64BIT_BUS_MASK;
162 break;
163 default:
164 pri_and_ext_bus_width = 0x0;
165 }
166
167 if (bus_width_ext == MV_DDR_BUS_WIDTH_EXT_8)
168 pri_and_ext_bus_width |= 1 << (octets_per_if_num - 1);
169 }
170
171 return pri_and_ext_bus_width;
172}
173
174unsigned int mv_ddr_if_bus_width_get(void)
175{
176 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
177 unsigned int bus_width;
178
179 switch (tm->bus_act_mask) {
180 case BUS_MASK_16BIT:
181 case BUS_MASK_16BIT_ECC:
182 case BUS_MASK_16BIT_ECC_PUP3:
183 bus_width = 16;
184 break;
185 case BUS_MASK_32BIT:
186 case BUS_MASK_32BIT_ECC:
187 case MV_DDR_32BIT_ECC_PUP8_BUS_MASK:
188 bus_width = 32;
189 break;
190 case MV_DDR_64BIT_BUS_MASK:
191 case MV_DDR_64BIT_ECC_PUP8_BUS_MASK:
192 bus_width = 64;
193 break;
194 default:
195 printf("mv_ddr: unsupported bus active mask parameter found\n");
196 bus_width = 0;
197 }
198
199 return bus_width;
200}
Chris Packham4bf81db2018-12-03 14:26:49 +1300201
202unsigned int mv_ddr_cs_num_get(void)
203{
204 unsigned int cs_num = 0;
205 unsigned int cs, sphy;
206 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
207 struct if_params *iface_params = &(tm->interface_params[0]);
208 unsigned int sphy_max = ddr3_tip_dev_attr_get(0, MV_ATTR_OCTET_PER_INTERFACE);
209
210 for (sphy = 0; sphy < sphy_max; sphy++) {
211 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, sphy);
212 break;
213 }
214
215 for (cs = 0; cs < MAX_CS_NUM; cs++) {
216 VALIDATE_ACTIVE(iface_params->as_bus_params[sphy].cs_bitmask, cs);
217 cs_num++;
218 }
219
220 return cs_num;
221}
222
223int mv_ddr_is_ecc_ena(void)
224{
225 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
226
227 if (DDR3_IS_ECC_PUP4_MODE(tm->bus_act_mask) ||
228 DDR3_IS_ECC_PUP3_MODE(tm->bus_act_mask) ||
229 DDR3_IS_ECC_PUP8_MODE(tm->bus_act_mask))
230 return 1;
231 else
232 return 0;
233}
234
Chris Packhame422adc2020-01-30 12:50:44 +1300235int mv_ddr_ck_delay_get(void)
236{
237 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
238
239 if (tm->ck_delay)
240 return tm->ck_delay;
241
242 return -1;
243}
244
Chris Packham4bf81db2018-12-03 14:26:49 +1300245/* translate topology map definition to real memory size in bits */
246static unsigned int mem_size[] = {
247 ADDR_SIZE_512MB,
248 ADDR_SIZE_1GB,
249 ADDR_SIZE_2GB,
250 ADDR_SIZE_4GB,
251 ADDR_SIZE_8GB
252 /* TODO: add capacity up to 256GB */
253};
254
255unsigned long long mv_ddr_mem_sz_per_cs_get(void)
256{
257 unsigned long long mem_sz_per_cs;
258 unsigned int i, sphys, sphys_per_dunit;
259 unsigned int sphy_max = ddr3_tip_dev_attr_get(0, MV_ATTR_OCTET_PER_INTERFACE);
260 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
261 struct if_params *iface_params = &(tm->interface_params[0]);
262
263 /* calc number of active subphys excl. ecc one */
264 for (i = 0, sphys = 0; i < sphy_max - 1; i++) {
265 VALIDATE_BUS_ACTIVE(tm->bus_act_mask, i);
266 sphys++;
267 }
268
269 /* calc number of subphys per ddr unit */
270 if (iface_params->bus_width == MV_DDR_DEV_WIDTH_8BIT)
271 sphys_per_dunit = MV_DDR_ONE_SPHY_PER_DUNIT;
272 else if (iface_params->bus_width == MV_DDR_DEV_WIDTH_16BIT)
273 sphys_per_dunit = MV_DDR_TWO_SPHY_PER_DUNIT;
274 else {
275 printf("mv_ddr: unsupported bus width type found\n");
276 return 0;
277 }
278
279 /* calc dram size per cs */
280 mem_sz_per_cs = (unsigned long long)mem_size[iface_params->memory_size] *
281 (unsigned long long)sphys /
282 (unsigned long long)sphys_per_dunit;
283
284 return mem_sz_per_cs;
285}
286
287unsigned long long mv_ddr_mem_sz_get(void)
288{
289 unsigned long long tot_mem_sz = 0;
290 unsigned long long mem_sz_per_cs = 0;
291 unsigned long long max_cs = mv_ddr_cs_num_get();
292
293 mem_sz_per_cs = mv_ddr_mem_sz_per_cs_get();
294 tot_mem_sz = max_cs * mem_sz_per_cs;
295
296 return tot_mem_sz;
297}
298
299unsigned int mv_ddr_rtt_nom_get(void)
300{
301 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
302 unsigned int rtt_nom = tm->edata.mem_edata.rtt_nom;
303
304 if (rtt_nom >= MV_DDR_RTT_NOM_PARK_RZQ_LAST) {
305 printf("error: %s: unsupported rtt_nom parameter found\n", __func__);
306 rtt_nom = PARAM_UNDEFINED;
307 }
308
309 return rtt_nom;
310}
311
312unsigned int mv_ddr_rtt_park_get(void)
313{
314 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
315 unsigned int cs_num = mv_ddr_cs_num_get();
316 unsigned int rtt_park = MV_DDR_RTT_NOM_PARK_RZQ_LAST;
317
318 if (cs_num > 0 && cs_num <= MAX_CS_NUM)
319 rtt_park = tm->edata.mem_edata.rtt_park[cs_num - 1];
320
321 if (rtt_park >= MV_DDR_RTT_NOM_PARK_RZQ_LAST) {
322 printf("error: %s: unsupported rtt_park parameter found\n", __func__);
323 rtt_park = PARAM_UNDEFINED;
324 }
325
326 return rtt_park;
327}
328
329unsigned int mv_ddr_rtt_wr_get(void)
330{
331 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
332 unsigned int cs_num = mv_ddr_cs_num_get();
333 unsigned int rtt_wr = MV_DDR_RTT_WR_RZQ_LAST;
334
335 if (cs_num > 0 && cs_num <= MAX_CS_NUM)
336 rtt_wr = tm->edata.mem_edata.rtt_wr[cs_num - 1];
337
338 if (rtt_wr >= MV_DDR_RTT_WR_RZQ_LAST) {
339 printf("error: %s: unsupported rtt_wr parameter found\n", __func__);
340 rtt_wr = PARAM_UNDEFINED;
341 }
342
343 return rtt_wr;
344}
345
346unsigned int mv_ddr_dic_get(void)
347{
348 struct mv_ddr_topology_map *tm = mv_ddr_topology_map_get();
349 unsigned int dic = tm->edata.mem_edata.dic;
350
351 if (dic >= MV_DDR_DIC_RZQ_LAST) {
352 printf("error: %s: unsupported dic parameter found\n", __func__);
353 dic = PARAM_UNDEFINED;
354 }
355
356 return dic;
357}