blob: e630dc5ae3e857966a57acc6e6dfc134c6ddd8e8 [file] [log] [blame]
Aaron Williams345b9b42020-09-02 08:29:05 +02001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2020 Marvell International Ltd.
4 */
5
6#ifndef __OCTEON_DDR_H_
7#define __OCTEON_DDR_H_
8
9#include <env.h>
10#include <linux/compat.h>
11#include <linux/delay.h>
12#include <linux/io.h>
13#include <mach/octeon-model.h>
14#include <mach/cvmx/cvmx-lmcx-defs.h>
Stefan Roese1a035f82020-12-11 17:05:56 +010015#include <mach/cvmx-regs.h>
16#include <mach/cvmx-l2c-defs.h>
Aaron Williams345b9b42020-09-02 08:29:05 +020017
18/* Some "external" (non-LMC) registers */
19#define CVMX_IPD_CLK_COUNT 0x00014F0000000338
20#define CVMX_FPA_CLK_COUNT 0x00012800000000F0
21
22#define CVMX_NODE_MEM_SHIFT 40
23
24#define DDR_INTERFACE_MAX 4
25
26/* Private data struct */
27struct ddr_priv {
28 void __iomem *lmc_base;
29 void __iomem *l2c_base;
30
31 bool ddr_clock_initialized[DDR_INTERFACE_MAX];
32 bool ddr_memory_preserved;
33 u32 flags;
34
35 struct ram_info info;
36};
37
38/* Short cut to convert a number to megabytes */
39#define MB(X) ((u64)(X) * (u64)(1024 * 1024))
40
41#define octeon_is_cpuid(x) (__OCTEON_IS_MODEL_COMPILE__(x, read_c0_prid()))
42
43#define strtoull simple_strtoull
44
45/* Access LMC registers */
46static inline u64 lmc_rd(struct ddr_priv *priv, u64 addr)
47{
48 return ioread64(priv->lmc_base + addr);
49}
50
51static inline void lmc_wr(struct ddr_priv *priv, u64 addr, u64 val)
52{
53 iowrite64(val, priv->lmc_base + addr);
54}
55
56/* Access L2C registers */
57static inline u64 l2c_rd(struct ddr_priv *priv, u64 addr)
58{
59 return ioread64(priv->l2c_base + addr);
60}
61
62static inline void l2c_wr(struct ddr_priv *priv, u64 addr, u64 val)
63{
64 iowrite64(val, priv->l2c_base + addr);
65}
66
Aaron Williams345b9b42020-09-02 08:29:05 +020067/* Failsafe mode */
68#define FLAG_FAILSAFE_MODE 0x01000
69/* Note that the DDR clock initialized flags must be contiguous */
70/* Clock for DDR 0 initialized */
71#define FLAG_DDR0_CLK_INITIALIZED 0x02000
72/* Clock for DDR 1 initialized */
73#define FLAG_DDR1_CLK_INITIALIZED 0x04000
74/* Clock for DDR 2 initialized */
75#define FLAG_DDR2_CLK_INITIALIZED 0x08000
76/* Clock for DDR 3 initialized */
77#define FLAG_DDR3_CLK_INITIALIZED 0x10000
78/* Loaded into RAM externally */
79#define FLAG_RAM_RESIDENT 0x20000
80/* Verbose DDR information */
81#define FLAG_DDR_VERBOSE 0x40000
82/* Check env. for DDR variables */
83#define FLAG_DDR_DEBUG 0x80000
84#define FLAG_DDR_TRACE_INIT 0x100000
85#define FLAG_MEMORY_PRESERVED 0x200000
86#define FLAG_DFM_VERBOSE 0x400000
87#define FLAG_DFM_TRACE_INIT 0x800000
88/* DFM memory clock initialized */
89#define FLAG_DFM_CLK_INITIALIZED 0x1000000
90/* EEPROM clock descr. missing */
91#define FLAG_CLOCK_DESC_MISSING 0x2000000
92/* EEPROM board descr. missing */
93#define FLAG_BOARD_DESC_MISSING 0x4000000
94#define FLAG_DDR_PROMPT 0x8000000
95
96#ifndef DDR_NO_DEBUG
97static inline int ddr_verbose(struct ddr_priv *priv)
98{
99 return !!(priv->flags & FLAG_DDR_VERBOSE);
100}
101
102static inline char *ddr_getenv_debug(struct ddr_priv *priv, char *name)
103{
104 if (priv->flags & FLAG_FAILSAFE_MODE)
105 return NULL;
106
107 if (priv->flags & FLAG_DDR_DEBUG)
108 return env_get(name);
109
110 return NULL;
111}
112#else
113static inline int ddr_verbose(void)
114{
115 return 0;
116}
117#endif
118
119/* turn the variable name into a string */
120#define CVMX_TMP_STR(x) CVMX_TMP_STR2(x)
121#define CVMX_TMP_STR2(x) #x
122
123#define CVMX_SYNC asm volatile ("sync" : : : "memory")
124
125#define CVMX_CACHE(op, address, offset) \
126 asm volatile ("cache " CVMX_TMP_STR(op) ", " \
127 CVMX_TMP_STR(offset) "(%[rbase])" \
128 : : [rbase] "d" (address))
129
130/* unlock the state */
131#define CVMX_CACHE_WBIL2(address, offset) \
132 CVMX_CACHE(23, address, offset)
133
134/* complete prefetches, invalidate entire dcache */
135#define CVMX_DCACHE_INVALIDATE \
136 { CVMX_SYNC; asm volatile ("cache 9, 0($0)" : : ); }
137
Aaron Williams345b9b42020-09-02 08:29:05 +0200138#define DEBUG_VALIDATE_BITMASK 0
139#if DEBUG_VALIDATE_BITMASK
140#define debug_bitmask_print printf
141#else
142#define debug_bitmask_print(...)
143#endif
144
145#define RLEVEL_BITMASK_TRAILING_BITS_ERROR 5
146// FIXME? now less than TOOLONG
147#define RLEVEL_BITMASK_BUBBLE_BITS_ERROR 11
148#define RLEVEL_BITMASK_NARROW_ERROR 6
149#define RLEVEL_BITMASK_BLANK_ERROR 100
150#define RLEVEL_BITMASK_TOOLONG_ERROR 12
151#define RLEVEL_NONSEQUENTIAL_DELAY_ERROR 50
152#define RLEVEL_ADJACENT_DELAY_ERROR 30
153
154/*
155 * Apply a filter to the BITMASK results returned from Octeon
156 * read-leveling to determine the most likely delay result. This
157 * computed delay may be used to qualify the delay result returned by
158 * Octeon. Accumulate an error penalty for invalid characteristics of
159 * the bitmask so that they can be used to select the most reliable
160 * results.
161 *
162 * The algorithm searches for the largest contiguous MASK within a
163 * maximum RANGE of bits beginning with the MSB.
164 *
165 * 1. a MASK with a WIDTH less than 4 will be penalized
166 * 2. Bubbles in the bitmask that occur before or after the MASK
167 * will be penalized
168 * 3. If there are no trailing bubbles then extra bits that occur
169 * beyond the maximum RANGE will be penalized.
170 *
171 * +++++++++++++++++++++++++++++++++++++++++++++++++++
172 * + +
173 * + e.g. bitmask = 27B00 +
174 * + +
175 * + 63 +--- mstart 0 +
176 * + | | | +
177 * + | +---------+ +--- fb | +
178 * + | | range | | | +
179 * + V V V V V +
180 * + +
181 * + 0 0 ... 1 0 0 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 +
182 * + +
183 * + ^ ^ ^ +
184 * + | | mask| +
185 * + lb ---+ +-----+ +
186 * + width +
187 * + +
188 * +++++++++++++++++++++++++++++++++++++++++++++++++++
189 */
190
191struct rlevel_bitmask {
192 u64 bm;
193 u8 mstart;
194 u8 width;
195 int errs;
196};
197
198#define MASKRANGE_BITS 6
199#define MASKRANGE ((1 << MASKRANGE_BITS) - 1)
200
201/* data field addresses in the DDR2 SPD eeprom */
202enum ddr2_spd_addrs {
203 DDR2_SPD_BYTES_PROGRAMMED = 0,
204 DDR2_SPD_TOTAL_BYTES = 1,
205 DDR2_SPD_MEM_TYPE = 2,
206 DDR2_SPD_NUM_ROW_BITS = 3,
207 DDR2_SPD_NUM_COL_BITS = 4,
208 DDR2_SPD_NUM_RANKS = 5,
209 DDR2_SPD_CYCLE_CLX = 9,
210 DDR2_SPD_CONFIG_TYPE = 11,
211 DDR2_SPD_REFRESH = 12,
212 DDR2_SPD_SDRAM_WIDTH = 13,
213 DDR2_SPD_BURST_LENGTH = 16,
214 DDR2_SPD_NUM_BANKS = 17,
215 DDR2_SPD_CAS_LATENCY = 18,
216 DDR2_SPD_DIMM_TYPE = 20,
217 DDR2_SPD_CYCLE_CLX1 = 23,
218 DDR2_SPD_CYCLE_CLX2 = 25,
219 DDR2_SPD_TRP = 27,
220 DDR2_SPD_TRRD = 28,
221 DDR2_SPD_TRCD = 29,
222 DDR2_SPD_TRAS = 30,
223 DDR2_SPD_TWR = 36,
224 DDR2_SPD_TWTR = 37,
225 DDR2_SPD_TRFC_EXT = 40,
226 DDR2_SPD_TRFC = 42,
227 DDR2_SPD_CHECKSUM = 63,
228 DDR2_SPD_MFR_ID = 64
229};
230
231/* data field addresses in the DDR2 SPD eeprom */
232enum ddr3_spd_addrs {
233 DDR3_SPD_BYTES_PROGRAMMED = 0,
234 DDR3_SPD_REVISION = 1,
235 DDR3_SPD_KEY_BYTE_DEVICE_TYPE = 2,
236 DDR3_SPD_KEY_BYTE_MODULE_TYPE = 3,
237 DDR3_SPD_DENSITY_BANKS = 4,
238 DDR3_SPD_ADDRESSING_ROW_COL_BITS = 5,
239 DDR3_SPD_NOMINAL_VOLTAGE = 6,
240 DDR3_SPD_MODULE_ORGANIZATION = 7,
241 DDR3_SPD_MEMORY_BUS_WIDTH = 8,
242 DDR3_SPD_FINE_TIMEBASE_DIVIDEND_DIVISOR = 9,
243 DDR3_SPD_MEDIUM_TIMEBASE_DIVIDEND = 10,
244 DDR3_SPD_MEDIUM_TIMEBASE_DIVISOR = 11,
245 DDR3_SPD_MINIMUM_CYCLE_TIME_TCKMIN = 12,
246 DDR3_SPD_CAS_LATENCIES_LSB = 14,
247 DDR3_SPD_CAS_LATENCIES_MSB = 15,
248 DDR3_SPD_MIN_CAS_LATENCY_TAAMIN = 16,
249 DDR3_SPD_MIN_WRITE_RECOVERY_TWRMIN = 17,
250 DDR3_SPD_MIN_RAS_CAS_DELAY_TRCDMIN = 18,
251 DDR3_SPD_MIN_ROW_ACTIVE_DELAY_TRRDMIN = 19,
252 DDR3_SPD_MIN_ROW_PRECHARGE_DELAY_TRPMIN = 20,
253 DDR3_SPD_UPPER_NIBBLES_TRAS_TRC = 21,
254 DDR3_SPD_MIN_ACTIVE_PRECHARGE_LSB_TRASMIN = 22,
255 DDR3_SPD_MIN_ACTIVE_REFRESH_LSB_TRCMIN = 23,
256 DDR3_SPD_MIN_REFRESH_RECOVERY_LSB_TRFCMIN = 24,
257 DDR3_SPD_MIN_REFRESH_RECOVERY_MSB_TRFCMIN = 25,
258 DDR3_SPD_MIN_INTERNAL_WRITE_READ_CMD_TWTRMIN = 26,
259 DDR3_SPD_MIN_INTERNAL_READ_PRECHARGE_CMD_TRTPMIN = 27,
260 DDR3_SPD_UPPER_NIBBLE_TFAW = 28,
261 DDR3_SPD_MIN_FOUR_ACTIVE_WINDOW_TFAWMIN = 29,
262 DDR3_SPD_SDRAM_OPTIONAL_FEATURES = 30,
263 DDR3_SPD_SDRAM_THERMAL_REFRESH_OPTIONS = 31,
264 DDR3_SPD_MODULE_THERMAL_SENSOR = 32,
265 DDR3_SPD_SDRAM_DEVICE_TYPE = 33,
266 DDR3_SPD_MINIMUM_CYCLE_TIME_FINE_TCKMIN = 34,
267 DDR3_SPD_MIN_CAS_LATENCY_FINE_TAAMIN = 35,
268 DDR3_SPD_MIN_RAS_CAS_DELAY_FINE_TRCDMIN = 36,
269 DDR3_SPD_MIN_ROW_PRECHARGE_DELAY_FINE_TRPMIN = 37,
270 DDR3_SPD_MIN_ACTIVE_REFRESH_LSB_FINE_TRCMIN = 38,
271 DDR3_SPD_REFERENCE_RAW_CARD = 62,
272 DDR3_SPD_ADDRESS_MAPPING = 63,
273 DDR3_SPD_REGISTER_MANUFACTURER_ID_LSB = 65,
274 DDR3_SPD_REGISTER_MANUFACTURER_ID_MSB = 66,
275 DDR3_SPD_REGISTER_REVISION_NUMBER = 67,
276 DDR3_SPD_MODULE_SERIAL_NUMBER = 122,
277 DDR3_SPD_CYCLICAL_REDUNDANCY_CODE_LOWER_NIBBLE = 126,
278 DDR3_SPD_CYCLICAL_REDUNDANCY_CODE_UPPER_NIBBLE = 127,
279 DDR3_SPD_MODULE_PART_NUMBER = 128
280};
281
282/* data field addresses in the DDR4 SPD eeprom */
283enum ddr4_spd_addrs {
284 DDR4_SPD_BYTES_PROGRAMMED = 0,
285 DDR4_SPD_REVISION = 1,
286 DDR4_SPD_KEY_BYTE_DEVICE_TYPE = 2,
287 DDR4_SPD_KEY_BYTE_MODULE_TYPE = 3,
288 DDR4_SPD_DENSITY_BANKS = 4,
289 DDR4_SPD_ADDRESSING_ROW_COL_BITS = 5,
290 DDR4_SPD_PACKAGE_TYPE = 6,
291 DDR4_SPD_OPTIONAL_FEATURES = 7,
292 DDR4_SPD_THERMAL_REFRESH_OPTIONS = 8,
293 DDR4_SPD_OTHER_OPTIONAL_FEATURES = 9,
294 DDR4_SPD_SECONDARY_PACKAGE_TYPE = 10,
295 DDR4_SPD_MODULE_NOMINAL_VOLTAGE = 11,
296 DDR4_SPD_MODULE_ORGANIZATION = 12,
297 DDR4_SPD_MODULE_MEMORY_BUS_WIDTH = 13,
298 DDR4_SPD_MODULE_THERMAL_SENSOR = 14,
299 DDR4_SPD_RESERVED_BYTE15 = 15,
300 DDR4_SPD_RESERVED_BYTE16 = 16,
301 DDR4_SPD_TIMEBASES = 17,
302 DDR4_SPD_MINIMUM_CYCLE_TIME_TCKAVGMIN = 18,
303 DDR4_SPD_MAXIMUM_CYCLE_TIME_TCKAVGMAX = 19,
304 DDR4_SPD_CAS_LATENCIES_BYTE0 = 20,
305 DDR4_SPD_CAS_LATENCIES_BYTE1 = 21,
306 DDR4_SPD_CAS_LATENCIES_BYTE2 = 22,
307 DDR4_SPD_CAS_LATENCIES_BYTE3 = 23,
308 DDR4_SPD_MIN_CAS_LATENCY_TAAMIN = 24,
309 DDR4_SPD_MIN_RAS_CAS_DELAY_TRCDMIN = 25,
310 DDR4_SPD_MIN_ROW_PRECHARGE_DELAY_TRPMIN = 26,
311 DDR4_SPD_UPPER_NIBBLES_TRAS_TRC = 27,
312 DDR4_SPD_MIN_ACTIVE_PRECHARGE_LSB_TRASMIN = 28,
313 DDR4_SPD_MIN_ACTIVE_REFRESH_LSB_TRCMIN = 29,
314 DDR4_SPD_MIN_REFRESH_RECOVERY_LSB_TRFC1MIN = 30,
315 DDR4_SPD_MIN_REFRESH_RECOVERY_MSB_TRFC1MIN = 31,
316 DDR4_SPD_MIN_REFRESH_RECOVERY_LSB_TRFC2MIN = 32,
317 DDR4_SPD_MIN_REFRESH_RECOVERY_MSB_TRFC2MIN = 33,
318 DDR4_SPD_MIN_REFRESH_RECOVERY_LSB_TRFC4MIN = 34,
319 DDR4_SPD_MIN_REFRESH_RECOVERY_MSB_TRFC4MIN = 35,
320 DDR4_SPD_MIN_FOUR_ACTIVE_WINDOW_MSN_TFAWMIN = 36,
321 DDR4_SPD_MIN_FOUR_ACTIVE_WINDOW_LSB_TFAWMIN = 37,
322 DDR4_SPD_MIN_ROW_ACTIVE_DELAY_SAME_TRRD_SMIN = 38,
323 DDR4_SPD_MIN_ROW_ACTIVE_DELAY_DIFF_TRRD_LMIN = 39,
324 DDR4_SPD_MIN_CAS_TO_CAS_DELAY_TCCD_LMIN = 40,
325 DDR4_SPD_MIN_CAS_TO_CAS_DELAY_FINE_TCCD_LMIN = 117,
326 DDR4_SPD_MIN_ACT_TO_ACT_DELAY_SAME_FINE_TRRD_LMIN = 118,
327 DDR4_SPD_MIN_ACT_TO_ACT_DELAY_DIFF_FINE_TRRD_SMIN = 119,
328 DDR4_SPD_MIN_ACT_TO_ACT_REFRESH_DELAY_FINE_TRCMIN = 120,
329 DDR4_SPD_MIN_ROW_PRECHARGE_DELAY_FINE_TRPMIN = 121,
330 DDR4_SPD_MIN_RAS_TO_CAS_DELAY_FINE_TRCDMIN = 122,
331 DDR4_SPD_MIN_CAS_LATENCY_FINE_TAAMIN = 123,
332 DDR4_SPD_MAX_CYCLE_TIME_FINE_TCKAVGMAX = 124,
333 DDR4_SPD_MIN_CYCLE_TIME_FINE_TCKAVGMIN = 125,
334 DDR4_SPD_CYCLICAL_REDUNDANCY_CODE_LOWER_NIBBLE = 126,
335 DDR4_SPD_CYCLICAL_REDUNDANCY_CODE_UPPER_NIBBLE = 127,
336 DDR4_SPD_REFERENCE_RAW_CARD = 130,
337 DDR4_SPD_UDIMM_ADDR_MAPPING_FROM_EDGE = 131,
338 DDR4_SPD_REGISTER_MANUFACTURER_ID_LSB = 133,
339 DDR4_SPD_REGISTER_MANUFACTURER_ID_MSB = 134,
340 DDR4_SPD_REGISTER_REVISION_NUMBER = 135,
341 DDR4_SPD_RDIMM_ADDR_MAPPING_FROM_REGISTER_TO_DRAM = 136,
342 DDR4_SPD_RDIMM_REGISTER_DRIVE_STRENGTH_CTL = 137,
343 DDR4_SPD_RDIMM_REGISTER_DRIVE_STRENGTH_CK = 138,
344};
345
346#define SPD_EEPROM_SIZE (DDR4_SPD_RDIMM_REGISTER_DRIVE_STRENGTH_CK + 1)
347
348struct impedence_values {
349 unsigned char *rodt_ohms;
350 unsigned char *rtt_nom_ohms;
351 unsigned char *rtt_nom_table;
352 unsigned char *rtt_wr_ohms;
353 unsigned char *dic_ohms;
354 short *drive_strength;
355 short *dqx_strength;
356};
357
358#define RODT_OHMS_COUNT 8
359#define RTT_NOM_OHMS_COUNT 8
360#define RTT_NOM_TABLE_COUNT 8
361#define RTT_WR_OHMS_COUNT 8
362#define DIC_OHMS_COUNT 3
363#define DRIVE_STRENGTH_COUNT 15
364
365/*
366 * Structure that provides DIMM information, either in the form of an SPD
367 * TWSI address, or a pointer to an array that contains SPD data. One of
368 * the two fields must be valid.
369 */
370struct dimm_config {
371 u16 spd_addrs[2]; /* TWSI address of SPD, 0 if not used */
372 u8 *spd_ptrs[2]; /* pointer to SPD data array, NULL if not used */
373 int spd_cached[2];
374 u8 spd_data[2][SPD_EEPROM_SIZE];
375};
376
377struct dimm_odt_config {
378 u8 odt_ena; /* FIX: dqx_ctl for Octeon 3 DDR4 */
379 u64 odt_mask; /* FIX: wodt_mask for Octeon 3 */
380 union cvmx_lmcx_modereg_params1 modereg_params1;
381 union cvmx_lmcx_modereg_params2 modereg_params2;
382 u8 qs_dic; /* FIX: rodt_ctl for Octeon 3 */
383 u64 rodt_ctl; /* FIX: rodt_mask for Octeon 3 */
384 u8 dic;
385};
386
387struct ddr_delay_config {
388 u32 ddr_board_delay;
389 u8 lmc_delay_clk;
390 u8 lmc_delay_cmd;
391 u8 lmc_delay_dq;
392};
393
394/*
395 * The parameters below make up the custom_lmc_config data structure.
396 * This structure is used to customize the way that the LMC DRAM
397 * Controller is configured for a particular board design.
398 *
399 * The HRM describes LMC Read Leveling which supports automatic
400 * selection of per byte-lane delays. When measuring the read delays
401 * the LMC configuration software sweeps through a range of settings
402 * for LMC0_COMP_CTL2[RODT_CTL], the Octeon II on-die-termination
403 * resistance and LMC0_MODEREG_PARAMS1[RTT_NOM_XX], the DRAM
404 * on-die-termination resistance. The minimum and maximum parameters
405 * for rtt_nom_idx and rodt_ctl listed below determine the ranges of
406 * ODT settings used for the measurements. Note that for rtt_nom an
407 * index is used into a sorted table rather than the direct csr setting
408 * in order to optimize the sweep.
409 *
410 * .min_rtt_nom_idx: 1=120ohms, 2=60ohms, 3=40ohms, 4=30ohms, 5=20ohms
411 * .max_rtt_nom_idx: 1=120ohms, 2=60ohms, 3=40ohms, 4=30ohms, 5=20ohms
412 * .min_rodt_ctl: 1=20ohms, 2=30ohms, 3=40ohms, 4=60ohms, 5=120ohms
413 * .max_rodt_ctl: 1=20ohms, 2=30ohms, 3=40ohms, 4=60ohms, 5=120ohms
414 *
415 * The settings below control the Octeon II drive strength for the CK,
416 * ADD/CMD, and DQ/DQS signals. 1=24ohms, 2=26.67ohms, 3=30ohms,
417 * 4=34.3ohms, 5=40ohms, 6=48ohms, 6=60ohms.
418 *
419 * .dqx_ctl: Drive strength control for DDR_DQX/DDR_DQS_X_P/N drivers.
420 * .ck_ctl: Drive strength control for
421 * DDR_CK_X_P/DDR_DIMMX_CSX_L/DDR_DIMMX_ODT_X drivers.
422 * .cmd_ctl: Drive strength control for CMD/A/RESET_L/CKEX drivers.
423 *
424 * The LMC controller software selects the most optimal CAS Latency
425 * that complies with the appropriate SPD values and the frequency
426 * that the DRAMS are being operated. When operating the DRAMs at
427 * frequencies substantially lower than their rated frequencies it
428 * might be necessary to limit the minimum CAS Latency the LMC
429 * controller software is allowed to select in order to make the DRAM
430 * work reliably.
431 *
432 * .min_cas_latency: Minimum allowed CAS Latency
433 *
434 * The value used for LMC0_RLEVEL_CTL[OFFSET_EN] determine how the
435 * read-leveling information that the Octeon II gathers is interpreted
436 * to determine the per-byte read delays.
437 *
438 * .offset_en: Value used for LMC0_RLEVEL_CTL[OFFSET_EN].
439 * .offset_udimm: Value used for LMC0_RLEVEL_CTL[OFFSET] for UDIMMS.
440 * .offset_rdimm: Value used for LMC0_RLEVEL_CTL[OFFSET] for RDIMMS.
441 *
442 * The LMC configuration software sweeps through a range of ODT
443 * settings while measuring the per-byte read delays. During those
444 * measurements the software makes an assessment of the quality of the
445 * measurements in order to determine which measurements provide the
446 * most accurate delays. The automatic settings provide the option to
447 * allow that same assessment to determine the most optimal RODT_CTL
448 * and/or RTT_NOM settings.
449 *
450 * The automatic approach might provide the best means to determine
451 * the settings used for initial poweron of a new design. However,
452 * the final settings should be determined by board analysis, testing,
453 * and experience.
454 *
455 * .ddr_rtt_nom_auto: 1 means automatically set RTT_NOM value.
456 * .ddr_rodt_ctl_auto: 1 means automatically set RODT_CTL value.
457 *
458 * .rlevel_compute: Enables software interpretation of per-byte read
459 * delays using the measurements collected by the
460 * Octeon II rather than completely relying on the
461 * Octeon II to determine the delays. 1=software
462 * computation is recomended since a more complete
463 * analysis is implemented in software.
464 *
465 * .rlevel_comp_offset: Set to 2 unless instructed differently by Cavium.
466 *
467 * .rlevel_average_loops: Determines the number of times the read-leveling
468 * sequence is run for each rank. The results is
469 * then averaged across the number of loops. The
470 * default setting is 1.
471 *
472 * .ddr2t_udimm:
473 * .ddr2t_rdimm: Turn on the DDR 2T mode. 2-cycle window for CMD and
474 * address. This mode helps relieve setup time pressure
475 * on the address and command bus. Please refer to
476 * Micron's tech note tn_47_01 titled DDR2-533 Memory
477 * Design Guide for Two Dimm Unbuffered Systems for
478 * physical details.
479 *
480 * .disable_sequential_delay_check: As result of the flyby topology
481 * prescribed in the JEDEC specifications the byte delays should
482 * maintain a consistent increasing or decreasing trend across
483 * the bytes on standard dimms. This setting can be used disable
484 * that check for unusual circumstances where the check is not
485 * useful.
486 *
487 * .maximum_adjacent_rlevel_delay_increment: An additional sequential
488 * delay check for the delays that result from the flyby
489 * topology. This value specifies the maximum difference between
490 * the delays of adjacent bytes. A value of 0 disables this
491 * check.
492 *
493 * .fprch2 Front Porch Enable: When set, the turn-off
494 * time for the default DDR_DQ/DQS drivers is FPRCH2 CKs earlier.
495 * 00 = 0 CKs
496 * 01 = 1 CKs
497 * 10 = 2 CKs
498 *
499 * .parity: The parity input signal PAR_IN on each dimm must be
500 * strapped high or low on the board. This bit is programmed
501 * into LMC0_DIMM_CTL[PARITY] and it must be set to match the
502 * board strapping. This signal is typically strapped low.
503 *
504 * .mode32b: Enable 32-bit datapath mode. Set to 1 if only 32 DQ pins
505 * are used. (cn61xx, cn71xx)
506 *
507 * .measured_vref: Set to 1 to measure VREF; set to 0 to compute VREF.
508 *
509 * .dram_connection: Set to 1 if discrete DRAMs; set to 0 if using DIMMs.
510 * This changes the algorithms used to compute VREF.
511 *
512 * .dll_write_offset: FIXME: Add description
513 * .dll_read_offset: FIXME: Add description
514 */
515
516struct rlevel_table {
517 const char part[20];
518 int speed;
519 u64 rl_rank[4][4];
520};
521
522struct ddr3_custom_config {
523 u8 min_rtt_nom_idx;
524 u8 max_rtt_nom_idx;
525 u8 min_rodt_ctl;
526 u8 max_rodt_ctl;
527 u8 dqx_ctl;
528 u8 ck_ctl;
529 u8 cmd_ctl;
530 u8 ctl_ctl;
531 u8 min_cas_latency;
532 u8 offset_en;
533 u8 offset_udimm;
534 u8 offset_rdimm;
535 u8 rlevel_compute;
536 u8 ddr_rtt_nom_auto;
537 u8 ddr_rodt_ctl_auto;
538 u8 rlevel_comp_offset_udimm;
539 u8 rlevel_comp_offset_rdimm;
540 int8_t ptune_offset;
541 int8_t ntune_offset;
542 u8 rlevel_average_loops;
543 u8 ddr2t_udimm;
544 u8 ddr2t_rdimm;
545 u8 disable_sequential_delay_check;
546 u8 maximum_adjacent_rlevel_delay_increment;
547 u8 parity;
548 u8 fprch2;
549 u8 mode32b;
550 u8 measured_vref;
551 u8 dram_connection;
552 const int8_t *dll_write_offset;
553 const int8_t *dll_read_offset;
554 struct rlevel_table *rl_tbl;
555};
556
557#define DDR_CFG_T_MAX_DIMMS 5
558
559struct ddr_conf {
560 struct dimm_config dimm_config_table[DDR_CFG_T_MAX_DIMMS];
561 struct dimm_odt_config odt_1rank_config[4];
562 struct dimm_odt_config odt_2rank_config[4];
563 struct dimm_odt_config odt_4rank_config[4];
564 struct ddr_delay_config unbuffered;
565 struct ddr_delay_config registered;
566 struct ddr3_custom_config custom_lmc_config;
567};
568
569/* Divide and round results to the nearest integer. */
570static inline u64 divide_nint(u64 dividend, u64 divisor)
571{
572 u64 quotent, remainder;
573
574 quotent = dividend / divisor;
575 remainder = dividend % divisor;
576 return (quotent + ((remainder * 2) >= divisor));
577}
578
579/* Divide and round results up to the next higher integer. */
580static inline u64 divide_roundup(u64 dividend, u64 divisor)
581{
582 return ((dividend + divisor - 1) / divisor);
583}
584
585enum ddr_type {
586 DDR3_DRAM = 3,
587 DDR4_DRAM = 4,
588};
589
590#define rttnom_none 0 /* Rtt_Nom disabled */
591#define rttnom_60ohm 1 /* RZQ/4 = 240/4 = 60 ohms */
592#define rttnom_120ohm 2 /* RZQ/2 = 240/2 = 120 ohms */
593#define rttnom_40ohm 3 /* RZQ/6 = 240/6 = 40 ohms */
594#define rttnom_20ohm 4 /* RZQ/12 = 240/12 = 20 ohms */
595#define rttnom_30ohm 5 /* RZQ/8 = 240/8 = 30 ohms */
596#define rttnom_rsrv1 6 /* Reserved */
597#define rttnom_rsrv2 7 /* Reserved */
598
599#define rttwr_none 0 /* Dynamic ODT off */
600#define rttwr_60ohm 1 /* RZQ/4 = 240/4 = 60 ohms */
601#define rttwr_120ohm 2 /* RZQ/2 = 240/2 = 120 ohms */
602#define rttwr_rsrv1 3 /* Reserved */
603
604#define dic_40ohm 0 /* RZQ/6 = 240/6 = 40 ohms */
605#define dic_34ohm 1 /* RZQ/7 = 240/7 = 34 ohms */
606
607#define driver_24_ohm 1
608#define driver_27_ohm 2
609#define driver_30_ohm 3
610#define driver_34_ohm 4
611#define driver_40_ohm 5
612#define driver_48_ohm 6
613#define driver_60_ohm 7
614
615#define rodt_ctl_none 0
616#define rodt_ctl_20_ohm 1
617#define rodt_ctl_30_ohm 2
618#define rodt_ctl_40_ohm 3
619#define rodt_ctl_60_ohm 4
620#define rodt_ctl_120_ohm 5
621
622#define ddr4_rttnom_none 0 /* Rtt_Nom disabled */
623#define ddr4_rttnom_60ohm 1 /* RZQ/4 = 240/4 = 60 ohms */
624#define ddr4_rttnom_120ohm 2 /* RZQ/2 = 240/2 = 120 ohms */
625#define ddr4_rttnom_40ohm 3 /* RZQ/6 = 240/6 = 40 ohms */
626#define ddr4_rttnom_240ohm 4 /* RZQ/1 = 240/1 = 240 ohms */
627#define ddr4_rttnom_48ohm 5 /* RZQ/5 = 240/5 = 48 ohms */
628#define ddr4_rttnom_80ohm 6 /* RZQ/3 = 240/3 = 80 ohms */
629#define ddr4_rttnom_34ohm 7 /* RZQ/7 = 240/7 = 34 ohms */
630
631#define ddr4_rttwr_none 0 /* Dynamic ODT off */
632#define ddr4_rttwr_120ohm 1 /* RZQ/2 = 240/2 = 120 ohms */
633#define ddr4_rttwr_240ohm 2 /* RZQ/1 = 240/1 = 240 ohms */
634#define ddr4_rttwr_hiz 3 /* HiZ */
635/* This setting is available for cn78xx pass 2, and cn73xx & cnf75xx pass 1 */
636#define ddr4_rttwr_80ohm 4 /* RZQ/3 = 240/3 = 80 ohms */
637
638#define ddr4_dic_34ohm 0 /* RZQ/7 = 240/7 = 34 ohms */
639#define ddr4_dic_48ohm 1 /* RZQ/5 = 240/5 = 48 ohms */
640
641#define ddr4_rttpark_none 0 /* Rtt_Park disabled */
642#define ddr4_rttpark_60ohm 1 /* RZQ/4 = 240/4 = 60 ohms */
643#define ddr4_rttpark_120ohm 2 /* RZQ/2 = 240/2 = 120 ohms */
644#define ddr4_rttpark_40ohm 3 /* RZQ/6 = 240/6 = 40 ohms */
645#define ddr4_rttpark_240ohm 4 /* RZQ/1 = 240/1 = 240 ohms */
646#define ddr4_rttpark_48ohm 5 /* RZQ/5 = 240/5 = 48 ohms */
647#define ddr4_rttpark_80ohm 6 /* RZQ/3 = 240/3 = 80 ohms */
648#define ddr4_rttpark_34ohm 7 /* RZQ/7 = 240/7 = 34 ohms */
649
650#define ddr4_driver_26_ohm 2
651#define ddr4_driver_30_ohm 3
652#define ddr4_driver_34_ohm 4
653#define ddr4_driver_40_ohm 5
654#define ddr4_driver_48_ohm 6
655
656#define ddr4_dqx_driver_24_ohm 1
657#define ddr4_dqx_driver_27_ohm 2
658#define ddr4_dqx_driver_30_ohm 3
659#define ddr4_dqx_driver_34_ohm 4
660#define ddr4_dqx_driver_40_ohm 5
661#define ddr4_dqx_driver_48_ohm 6
662#define ddr4_dqx_driver_60_ohm 7
663
664#define ddr4_rodt_ctl_none 0
665#define ddr4_rodt_ctl_40_ohm 1
666#define ddr4_rodt_ctl_60_ohm 2
667#define ddr4_rodt_ctl_80_ohm 3
668#define ddr4_rodt_ctl_120_ohm 4
669#define ddr4_rodt_ctl_240_ohm 5
670#define ddr4_rodt_ctl_34_ohm 6
671#define ddr4_rodt_ctl_48_ohm 7
672
673#define DIMM_CONFIG_TERMINATOR { {0, 0}, {NULL, NULL} }
674
675#define SET_DDR_DLL_CTL3(field, expr) \
676 do { \
677 if (octeon_is_cpuid(OCTEON_CN66XX) || \
678 octeon_is_cpuid(OCTEON_CN63XX)) \
679 ddr_dll_ctl3.cn63xx.field = (expr); \
680 else if (octeon_is_cpuid(OCTEON_CN68XX) || \
681 octeon_is_cpuid(OCTEON_CN61XX) || \
682 octeon_is_cpuid(OCTEON_CNF71XX)) \
683 ddr_dll_ctl3.cn61xx.field = (expr); \
684 else if (octeon_is_cpuid(OCTEON_CN70XX) || \
685 octeon_is_cpuid(OCTEON_CN78XX)) \
686 ddr_dll_ctl3.cn70xx.field = (expr); \
687 else if (octeon_is_cpuid(OCTEON_CN73XX) || \
688 octeon_is_cpuid(OCTEON_CNF75XX)) \
689 ddr_dll_ctl3.cn73xx.field = (expr); \
690 else \
691 debug("%s(): " #field \
692 "not set for unknown chip\n", \
693 __func__); \
694 } while (0)
695
696#define ENCODE_DLL90_BYTE_SEL(byte_sel) \
697 (octeon_is_cpuid(OCTEON_CN70XX) ? ((9 + 7 - (byte_sel)) % 9) : \
698 ((byte_sel) + 1))
699
700/**
701 * If debugging is disabled the ddr_print macro is not compatible
702 * with this macro.
703 */
704# define GET_DDR_DLL_CTL3(field) \
705 ((octeon_is_cpuid(OCTEON_CN66XX) || \
706 octeon_is_cpuid(OCTEON_CN63XX)) ? \
707 ddr_dll_ctl3.cn63xx.field : \
708 (octeon_is_cpuid(OCTEON_CN68XX) || \
709 octeon_is_cpuid(OCTEON_CN61XX) || \
710 octeon_is_cpuid(OCTEON_CNF71XX)) ? \
711 ddr_dll_ctl3.cn61xx.field : \
712 (octeon_is_cpuid(OCTEON_CN70XX) || \
713 octeon_is_cpuid(OCTEON_CN78XX)) ? \
714 ddr_dll_ctl3.cn70xx.field : \
715 (octeon_is_cpuid(OCTEON_CN73XX) || \
716 octeon_is_cpuid(OCTEON_CNF75XX)) ? \
717 ddr_dll_ctl3.cn73xx.field : 0)
718
719extern const char *ddr3_dimm_types[];
720extern const char *ddr4_dimm_types[];
721
722extern const struct dimm_odt_config disable_odt_config[];
723
724#define RLEVEL_BYTE_BITS 6
725#define RLEVEL_BYTE_MSK ((1ULL << 6) - 1)
726
727/* Prototypes */
728int get_ddr_type(struct dimm_config *dimm_config, int upper_dimm);
729int get_dimm_module_type(struct dimm_config *dimm_config, int upper_dimm,
730 int ddr_type);
731int read_spd(struct dimm_config *dimm_config, int dimm_index, int spd_field);
732int read_spd_init(struct dimm_config *dimm_config, int dimm_index);
733void report_dimm(struct dimm_config *dimm_config, int upper_dimm,
734 int dimm, int if_num);
735int validate_dimm(struct ddr_priv *priv, struct dimm_config *dimm_config,
736 int dimm_index);
737char *printable_rank_spec(char *buffer, int num_ranks, int dram_width,
738 int spd_package);
739
740bool ddr_memory_preserved(struct ddr_priv *priv);
741
742int get_wl_rank(union cvmx_lmcx_wlevel_rankx *lmc_wlevel_rank, int byte);
743int get_rl_rank(union cvmx_lmcx_rlevel_rankx *lmc_rlevel_rank, int byte);
744void upd_wl_rank(union cvmx_lmcx_wlevel_rankx *lmc_wlevel_rank, int byte,
745 int delay);
746void upd_rl_rank(union cvmx_lmcx_rlevel_rankx *lmc_rlevel_rank, int byte,
747 int delay);
748
749int compute_ddr3_rlevel_delay(u8 mstart, u8 width,
750 union cvmx_lmcx_rlevel_ctl rlevel_ctl);
751
752int encode_row_lsb_ddr3(int row_lsb);
753int encode_pbank_lsb_ddr3(int pbank_lsb);
754
755int initialize_ddr_clock(struct ddr_priv *priv, struct ddr_conf *ddr_conf,
756 u32 cpu_hertz, u32 ddr_hertz, u32 ddr_ref_hertz,
757 int if_num, u32 if_mask);
758
759void process_custom_dll_offsets(struct ddr_priv *priv, int if_num,
760 const char *enable_str,
761 const int8_t *offsets, const char *byte_str,
762 int mode);
763int nonseq_del(struct rlevel_byte_data *rlevel_byte, int start, int end,
764 int max_adj_delay_inc);
765int roundup_ddr3_wlevel_bitmask(int bitmask);
766
767void oct3_ddr3_seq(struct ddr_priv *priv, int rank_mask, int if_num,
768 int sequence);
769void ddr_init_seq(struct ddr_priv *priv, int rank_mask, int if_num);
770
771void rlevel_to_wlevel(union cvmx_lmcx_rlevel_rankx *lmc_rlevel_rank,
772 union cvmx_lmcx_wlevel_rankx *lmc_wlevel_rank, int byte);
773
774int validate_ddr3_rlevel_bitmask(struct rlevel_bitmask *rlevel_bitmask_p,
775 int ddr_type);
776
777void change_dll_offset_enable(struct ddr_priv *priv, int if_num, int change);
778unsigned short load_dll_offset(struct ddr_priv *priv, int if_num,
779 int dll_offset_mode,
780 int byte_offset, int byte);
781
782u64 lmc_ddr3_rl_dbg_read(struct ddr_priv *priv, int if_num, int idx);
783u64 lmc_ddr3_wl_dbg_read(struct ddr_priv *priv, int if_num, int idx);
784
785void cvmx_maybe_tune_node(struct ddr_priv *priv, u32 ddr_speed);
786void cvmx_dbi_switchover(struct ddr_priv *priv);
787
788int init_octeon3_ddr3_interface(struct ddr_priv *priv,
789 struct ddr_conf *ddr_conf,
790 u32 ddr_hertz, u32 cpu_hertz, u32 ddr_ref_hertz,
791 int if_num, u32 if_mask);
792
793char *lookup_env(struct ddr_priv *priv, const char *format, ...);
794char *lookup_env_ull(struct ddr_priv *priv, const char *format, ...);
795
796/* Each board provides a board-specific config table via this function */
797struct ddr_conf *octeon_ddr_conf_table_get(int *count, int *def_ddr_freq);
798
799#endif /* __OCTEON_DDR_H_ */