blob: 28a663289a23d0ecadaa2ff14987d6132e6f4569 [file] [log] [blame]
Mario Six538b5752018-08-06 10:23:30 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2018
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5 */
6
Mario Six538b5752018-08-06 10:23:30 +02007#include <dm.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -07008#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Mario Six538b5752018-08-06 10:23:30 +020010#include <ram.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060011#include <asm/bitops.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Mario Six538b5752018-08-06 10:23:30 +020013#include <dt-bindings/memory/mpc83xx-sdram.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
17/* Masks for the CS config register */
18static const u32 CSCONFIG_ENABLE = 0x80000000;
19
20static const u32 BANK_BITS_2;
21static const u32 BANK_BITS_3 = 0x00004000;
22
23static const u32 ROW_BITS_12;
24static const u32 ROW_BITS_13 = 0x00000100;
25static const u32 ROW_BITS_14 = 0x00000200;
26
27static const u32 COL_BITS_8;
28static const u32 COL_BITS_9 = 0x00000001;
29static const u32 COL_BITS_10 = 0x00000002;
30static const u32 COL_BITS_11 = 0x00000003;
31
32/* Shifts for the DDR SDRAM Timing Configuration 3 register */
33static const uint TIMING_CFG3_EXT_REFREC_SHIFT = (31 - 15);
34
35/* Shifts for the DDR SDRAM Timing Configuration 0 register */
36static const uint TIMING_CFG0_RWT_SHIFT = (31 - 1);
37static const uint TIMING_CFG0_WRT_SHIFT = (31 - 3);
38static const uint TIMING_CFG0_RRT_SHIFT = (31 - 5);
39static const uint TIMING_CFG0_WWT_SHIFT = (31 - 7);
40static const uint TIMING_CFG0_ACT_PD_EXIT_SHIFT = (31 - 11);
41static const uint TIMING_CFG0_PRE_PD_EXIT_SHIFT = (31 - 15);
42static const uint TIMING_CFG0_ODT_PD_EXIT_SHIFT = (31 - 23);
43static const uint TIMING_CFG0_MRS_CYC_SHIFT = (31 - 31);
44
45/* Shifts for the DDR SDRAM Timing Configuration 1 register */
46static const uint TIMING_CFG1_PRETOACT_SHIFT = (31 - 3);
47static const uint TIMING_CFG1_ACTTOPRE_SHIFT = (31 - 7);
48static const uint TIMING_CFG1_ACTTORW_SHIFT = (31 - 11);
49static const uint TIMING_CFG1_CASLAT_SHIFT = (31 - 15);
50static const uint TIMING_CFG1_REFREC_SHIFT = (31 - 19);
51static const uint TIMING_CFG1_WRREC_SHIFT = (31 - 23);
52static const uint TIMING_CFG1_ACTTOACT_SHIFT = (31 - 27);
53static const uint TIMING_CFG1_WRTORD_SHIFT = (31 - 31);
54
55/* Shifts for the DDR SDRAM Timing Configuration 2 register */
56static const uint TIMING_CFG2_CPO_SHIFT = (31 - 8);
57static const uint TIMING_CFG2_WR_DATA_DELAY_SHIFT = (31 - 21);
58static const uint TIMING_CFG2_ADD_LAT_SHIFT = (31 - 3);
59static const uint TIMING_CFG2_WR_LAT_DELAY_SHIFT = (31 - 12);
60static const uint TIMING_CFG2_RD_TO_PRE_SHIFT = (31 - 18);
61static const uint TIMING_CFG2_CKE_PLS_SHIFT = (31 - 25);
62static const uint TIMING_CFG2_FOUR_ACT_SHIFT;
63
64/* Shifts for the DDR SDRAM Control Configuration register */
65static const uint SDRAM_CFG_SREN_SHIFT = (31 - 1);
66static const uint SDRAM_CFG_ECC_EN_SHIFT = (31 - 2);
67static const uint SDRAM_CFG_RD_EN_SHIFT = (31 - 3);
68static const uint SDRAM_CFG_SDRAM_TYPE_SHIFT = (31 - 7);
69static const uint SDRAM_CFG_DYN_PWR_SHIFT = (31 - 10);
70static const uint SDRAM_CFG_DBW_SHIFT = (31 - 12);
71static const uint SDRAM_CFG_NCAP_SHIFT = (31 - 14);
72static const uint SDRAM_CFG_2T_EN_SHIFT = (31 - 16);
73static const uint SDRAM_CFG_BA_INTLV_CTL_SHIFT = (31 - 23);
74static const uint SDRAM_CFG_PCHB8_SHIFT = (31 - 27);
75static const uint SDRAM_CFG_HSE_SHIFT = (31 - 28);
76static const uint SDRAM_CFG_BI_SHIFT = (31 - 31);
77
78/* Shifts for the DDR SDRAM Control Configuration 2 register */
79static const uint SDRAM_CFG2_FRC_SR_SHIFT = (31 - 0);
80static const uint SDRAM_CFG2_DLL_RST_DIS = (31 - 2);
81static const uint SDRAM_CFG2_DQS_CFG = (31 - 5);
82static const uint SDRAM_CFG2_ODT_CFG = (31 - 10);
83static const uint SDRAM_CFG2_NUM_PR = (31 - 19);
84
85/* Shifts for the DDR SDRAM Mode register */
86static const uint SDRAM_MODE_ESD_SHIFT = (31 - 15);
87static const uint SDRAM_MODE_SD_SHIFT = (31 - 31);
88
89/* Shifts for the DDR SDRAM Mode 2 register */
90static const uint SDRAM_MODE2_ESD2_SHIFT = (31 - 15);
91static const uint SDRAM_MODE2_ESD3_SHIFT = (31 - 31);
92
93/* Shifts for the DDR SDRAM Interval Configuration register */
94static const uint SDRAM_INTERVAL_REFINT_SHIFT = (31 - 15);
95static const uint SDRAM_INTERVAL_BSTOPRE_SHIFT = (31 - 31);
96
97/* Mask for the DDR SDRAM Mode Control register */
98static const u32 SDRAM_CFG_MEM_EN = 0x80000000;
99
100int dram_init(void)
101{
102 struct udevice *ram_ctrl;
103 int ret;
104
105 /* Current assumption: There is only one RAM controller */
106 ret = uclass_first_device_err(UCLASS_RAM, &ram_ctrl);
107 if (ret) {
108 debug("%s: uclass_first_device_err failed: %d\n",
109 __func__, ret);
110 return ret;
111 }
112
113 /* FIXME(mario.six@gdsys.cc): Set gd->ram_size? */
114
115 return 0;
116}
117
118phys_size_t get_effective_memsize(void)
119{
Tom Riniaf1a3e92022-12-02 16:42:31 -0500120 return gd->ram_size;
Mario Six538b5752018-08-06 10:23:30 +0200121}
122
123/**
124 * struct mpc83xx_sdram_priv - Private data for MPC83xx RAM controllers
125 * @total_size: The total size of all RAM modules associated with this RAM
126 * controller in bytes
127 */
128struct mpc83xx_sdram_priv {
129 ulong total_size;
130};
131
132/**
133 * mpc83xx_sdram_static_init() - Statically initialize a RAM module.
134 * @node: Device tree node associated with ths module in question
135 * @cs: The chip select to use for this RAM module
136 * @mapaddr: The address where the RAM module should be mapped
137 * @size: The size of the RAM module to be mapped in bytes
138 *
139 * Return: 0 if OK, -ve on error
140 */
141static int mpc83xx_sdram_static_init(ofnode node, u32 cs, u32 mapaddr, u32 size)
142{
143 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
144 u32 msize = size;
145 u32 msize_log2 = __ilog2(msize);
146 u32 auto_precharge, odt_rd_cfg, odt_wr_cfg, bank_bits, row_bits,
147 col_bits;
148 u32 bank_bits_mask, row_bits_mask, col_bits_mask;
149
150 /* Configure the DDR local access window */
151 out_be32(&im->sysconf.ddrlaw[cs].bar, mapaddr & 0xfffff000);
152 out_be32(&im->sysconf.ddrlaw[cs].ar, LBLAWAR_EN | (msize_log2 - 1));
153
154 out_be32(&im->ddr.csbnds[cs].csbnds, (msize - 1) >> 24);
155
156 auto_precharge = ofnode_read_u32_default(node, "auto_precharge", 0);
157 switch (auto_precharge) {
158 case AUTO_PRECHARGE_ENABLE:
159 case AUTO_PRECHARGE_DISABLE:
160 break;
161 default:
162 debug("%s: auto_precharge value %d invalid.\n",
163 ofnode_get_name(node), auto_precharge);
164 return -EINVAL;
165 }
166
167 odt_rd_cfg = ofnode_read_u32_default(node, "odt_rd_cfg", 0);
168 switch (odt_rd_cfg) {
169 case ODT_RD_ONLY_OTHER_DIMM:
Mario Six84eb4312019-01-21 09:17:28 +0100170 if (!IS_ENABLED(CONFIG_ARCH_MPC8360) &&
Mario Six60b11232019-01-21 09:17:29 +0100171 !IS_ENABLED(CONFIG_ARCH_MPC837X)) {
Mario Six538b5752018-08-06 10:23:30 +0200172 debug("%s: odt_rd_cfg value %d invalid.\n",
173 ofnode_get_name(node), odt_rd_cfg);
174 return -EINVAL;
175 }
176 /* fall through */
177 case ODT_RD_NEVER:
178 case ODT_RD_ONLY_CURRENT:
179 case ODT_RD_ONLY_OTHER_CS:
Mario Sixb2e701c2019-01-21 09:17:24 +0100180 if (!IS_ENABLED(CONFIG_ARCH_MPC830X) &&
Mario Six9164bdd2019-01-21 09:17:25 +0100181 !IS_ENABLED(CONFIG_ARCH_MPC831X) &&
Mario Six84eb4312019-01-21 09:17:28 +0100182 !IS_ENABLED(CONFIG_ARCH_MPC8360) &&
Mario Six60b11232019-01-21 09:17:29 +0100183 !IS_ENABLED(CONFIG_ARCH_MPC837X)) {
Mario Six538b5752018-08-06 10:23:30 +0200184 debug("%s: odt_rd_cfg value %d invalid.\n",
185 ofnode_get_name(node), odt_rd_cfg);
186 return -EINVAL;
187 }
188 /* fall through */
189 /* Only MPC832x knows this value */
190 case ODT_RD_ALL:
191 break;
192 default:
193 debug("%s: odt_rd_cfg value %d invalid.\n",
194 ofnode_get_name(node), odt_rd_cfg);
195 return -EINVAL;
196 }
197
198 odt_wr_cfg = ofnode_read_u32_default(node, "odt_wr_cfg", 0);
199 switch (odt_wr_cfg) {
200 case ODT_WR_ONLY_OTHER_DIMM:
Mario Six84eb4312019-01-21 09:17:28 +0100201 if (!IS_ENABLED(CONFIG_ARCH_MPC8360) &&
Mario Six60b11232019-01-21 09:17:29 +0100202 !IS_ENABLED(CONFIG_ARCH_MPC837X)) {
Mario Six538b5752018-08-06 10:23:30 +0200203 debug("%s: odt_wr_cfg value %d invalid.\n",
204 ofnode_get_name(node), odt_wr_cfg);
205 return -EINVAL;
206 }
207 /* fall through */
208 case ODT_WR_NEVER:
209 case ODT_WR_ONLY_CURRENT:
210 case ODT_WR_ONLY_OTHER_CS:
Mario Sixb2e701c2019-01-21 09:17:24 +0100211 if (!IS_ENABLED(CONFIG_ARCH_MPC830X) &&
Mario Six9164bdd2019-01-21 09:17:25 +0100212 !IS_ENABLED(CONFIG_ARCH_MPC831X) &&
Mario Six84eb4312019-01-21 09:17:28 +0100213 !IS_ENABLED(CONFIG_ARCH_MPC8360) &&
Mario Six60b11232019-01-21 09:17:29 +0100214 !IS_ENABLED(CONFIG_ARCH_MPC837X)) {
Mario Six538b5752018-08-06 10:23:30 +0200215 debug("%s: odt_wr_cfg value %d invalid.\n",
216 ofnode_get_name(node), odt_wr_cfg);
217 return -EINVAL;
218 }
219 /* fall through */
220 /* MPC832x only knows this value */
221 case ODT_WR_ALL:
222 break;
223 default:
224 debug("%s: odt_wr_cfg value %d invalid.\n",
225 ofnode_get_name(node), odt_wr_cfg);
226 return -EINVAL;
227 }
228
229 bank_bits = ofnode_read_u32_default(node, "bank_bits", 0);
230 switch (bank_bits) {
231 case 2:
232 bank_bits_mask = BANK_BITS_2;
233 break;
234 case 3:
235 bank_bits_mask = BANK_BITS_3;
236 break;
237 default:
238 debug("%s: bank_bits value %d invalid.\n",
239 ofnode_get_name(node), bank_bits);
240 return -EINVAL;
241 }
242
243 row_bits = ofnode_read_u32_default(node, "row_bits", 0);
244 switch (row_bits) {
245 case 12:
246 row_bits_mask = ROW_BITS_12;
247 break;
248 case 13:
249 row_bits_mask = ROW_BITS_13;
250 break;
251 case 14:
252 row_bits_mask = ROW_BITS_14;
253 break;
254 default:
255 debug("%s: row_bits value %d invalid.\n",
256 ofnode_get_name(node), row_bits);
257 return -EINVAL;
258 }
259
260 col_bits = ofnode_read_u32_default(node, "col_bits", 0);
261 switch (col_bits) {
262 case 8:
263 col_bits_mask = COL_BITS_8;
264 break;
265 case 9:
266 col_bits_mask = COL_BITS_9;
267 break;
268 case 10:
269 col_bits_mask = COL_BITS_10;
270 break;
271 case 11:
272 col_bits_mask = COL_BITS_11;
273 break;
274 default:
275 debug("%s: col_bits value %d invalid.\n",
276 ofnode_get_name(node), col_bits);
277 return -EINVAL;
278 }
279
280 /* Write CS config value */
281 out_be32(&im->ddr.cs_config[cs], CSCONFIG_ENABLE | auto_precharge |
282 odt_rd_cfg | odt_wr_cfg |
283 bank_bits_mask | row_bits_mask |
284 col_bits_mask);
285 return 0;
286}
287
288/**
289 * mpc83xx_sdram_spd_init() - Initialize a RAM module using a SPD flash.
290 * @node: Device tree node associated with ths module in question
291 * @cs: The chip select to use for this RAM module
292 * @mapaddr: The address where the RAM module should be mapped
293 * @size: The size of the RAM module to be mapped in bytes
294 *
295 * Return: 0 if OK, -ve on error
296 */
297static int mpc83xx_sdram_spd_init(ofnode node, u32 cs, u32 mapaddr, u32 size)
298{
299 /* TODO(mario.six@gdsys.cc): Implement */
300 return 0;
301}
302
Simon Glassaad29ae2020-12-03 16:55:21 -0700303static int mpc83xx_sdram_of_to_plat(struct udevice *dev)
Mario Six538b5752018-08-06 10:23:30 +0200304{
305 return 0;
306}
307
308static int mpc83xx_sdram_probe(struct udevice *dev)
309{
310 struct mpc83xx_sdram_priv *priv = dev_get_priv(dev);
311 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
312 int ret = 0;
313 ofnode subnode;
314 /* DDR control driver register values */
315 u32 dso, pz_override, nz_override, odt_term, ddr_type, mvref_sel, m_odr;
316 u32 ddrcdr;
317 /* DDR SDRAM Clock Control register values */
318 u32 clock_adjust;
319 /* DDR SDRAM Timing Configuration 3 register values */
320 u32 ext_refresh_rec, ext_refresh_rec_mask;
321 /* DDR SDRAM Timing Configuration 0 register values */
322 u32 read_to_write, write_to_read, read_to_read, write_to_write,
323 active_powerdown_exit, precharge_powerdown_exit,
324 odt_powerdown_exit, mode_reg_set_cycle;
325 u32 timing_cfg_0;
326 /* DDR SDRAM Timing Configuration 1 register values */
327 u32 precharge_to_activate, activate_to_precharge,
328 activate_to_readwrite, mcas_latency, refresh_recovery,
329 last_data_to_precharge, activate_to_activate,
330 last_write_data_to_read;
331 u32 timing_cfg_1;
332 /* DDR SDRAM Timing Configuration 2 register values */
333 u32 additive_latency, mcas_to_preamble_override, write_latency,
334 read_to_precharge, write_cmd_to_write_data,
335 minimum_cke_pulse_width, four_activates_window;
336 u32 timing_cfg_2;
337 /* DDR SDRAM Control Configuration register values */
338 u32 self_refresh, ecc, registered_dram, sdram_type,
339 dynamic_power_management, databus_width, nc_auto_precharge,
340 timing_2t, bank_interleaving_ctrl, precharge_bit_8, half_strength,
341 bypass_initialization;
342 u32 sdram_cfg;
343 /* DDR SDRAM Control Configuration 2 register values */
344 u32 force_self_refresh, dll_reset, dqs_config, odt_config,
345 posted_refreshes;
346 u32 sdram_cfg2;
347 /* DDR SDRAM Mode Configuration register values */
348 u32 sdmode, esdmode;
349 u32 sdram_mode;
350 /* DDR SDRAM Mode Configuration 2 register values */
351 u32 esdmode2, esdmode3;
352 u32 sdram_mode2;
353 /* DDR SDRAM Interval Configuration register values */
354 u32 refresh_interval, precharge_interval;
355 u32 sdram_interval;
356
357 priv->total_size = 0;
358
359 /* Disable both banks initially (might be re-enabled in loop below) */
360 out_be32(&im->ddr.cs_config[0], 0);
361 out_be32(&im->ddr.cs_config[1], 0);
362
363 dso = dev_read_u32_default(dev, "driver_software_override", 0);
364 if (dso > 1) {
365 debug("%s: driver_software_override value %d invalid.\n",
366 dev->name, dso);
367 return -EINVAL;
368 }
369
370 pz_override = dev_read_u32_default(dev, "p_impedance_override", 0);
371
372 switch (pz_override) {
373 case DSO_P_IMPEDANCE_HIGHEST_Z:
374 case DSO_P_IMPEDANCE_MUCH_HIGHER_Z:
375 case DSO_P_IMPEDANCE_HIGHER_Z:
376 case DSO_P_IMPEDANCE_NOMINAL:
377 case DSO_P_IMPEDANCE_LOWER_Z:
378 break;
379 default:
380 debug("%s: p_impedance_override value %d invalid.\n",
381 dev->name, pz_override);
382 return -EINVAL;
383 }
384
385 nz_override = dev_read_u32_default(dev, "n_impedance_override", 0);
386
387 switch (nz_override) {
388 case DSO_N_IMPEDANCE_HIGHEST_Z:
389 case DSO_N_IMPEDANCE_MUCH_HIGHER_Z:
390 case DSO_N_IMPEDANCE_HIGHER_Z:
391 case DSO_N_IMPEDANCE_NOMINAL:
392 case DSO_N_IMPEDANCE_LOWER_Z:
393 break;
394 default:
395 debug("%s: n_impedance_override value %d invalid.\n",
396 dev->name, nz_override);
397 return -EINVAL;
398 }
399
400 odt_term = dev_read_u32_default(dev, "odt_termination_value", 0);
401 if (odt_term > 1) {
402 debug("%s: odt_termination_value value %d invalid.\n",
403 dev->name, odt_term);
404 return -EINVAL;
405 }
406
407 ddr_type = dev_read_u32_default(dev, "ddr_type", 0);
408 if (ddr_type > 1) {
409 debug("%s: ddr_type value %d invalid.\n",
410 dev->name, ddr_type);
411 return -EINVAL;
412 }
413
414 mvref_sel = dev_read_u32_default(dev, "mvref_sel", 0);
415 if (mvref_sel > 1) {
416 debug("%s: mvref_sel value %d invalid.\n",
417 dev->name, mvref_sel);
418 return -EINVAL;
419 }
420
421 m_odr = dev_read_u32_default(dev, "m_odr", 0);
422 if (mvref_sel > 1) {
423 debug("%s: m_odr value %d invalid.\n",
424 dev->name, m_odr);
425 return -EINVAL;
426 }
427
428 ddrcdr = dso << (31 - 1) |
429 pz_override << (31 - 5) |
430 nz_override << (31 - 9) |
431 odt_term << (31 - 12) |
432 ddr_type << (31 - 13) |
433 mvref_sel << (31 - 29) |
434 m_odr << (31 - 30) | 1;
435
436 /* Configure the DDR control driver register */
437 out_be32(&im->sysconf.ddrcdr, ddrcdr);
438
439 dev_for_each_subnode(subnode, dev) {
440 u32 val[3];
441 u32 cs, addr, size;
442
443 /* CS, map address, size -> three values */
444 ofnode_read_u32_array(subnode, "reg", val, 3);
445
446 cs = val[0];
447 addr = val[1];
448 size = val[2];
449
450 if (cs > 1) {
451 debug("%s: chip select value %d invalid.\n",
452 dev->name, cs);
453 return -EINVAL;
454 }
455
456 /* TODO(mario.six@gdsys.cc): Sanity check for size. */
457
458 if (ofnode_read_bool(subnode, "read-spd"))
459 ret = mpc83xx_sdram_spd_init(subnode, cs, addr, size);
460 else
461 ret = mpc83xx_sdram_static_init(subnode, cs, addr,
462 size);
463 if (ret) {
464 debug("%s: RAM init failed.\n", dev->name);
465 return ret;
466 }
467 };
468
469 /*
470 * TODO(mario.six@gdsys.cc): This should only occur for static
471 * configuration
472 */
473
474 clock_adjust = dev_read_u32_default(dev, "clock_adjust", 0);
475 switch (clock_adjust) {
476 case CLOCK_ADJUST_025:
477 case CLOCK_ADJUST_05:
478 case CLOCK_ADJUST_075:
479 case CLOCK_ADJUST_1:
480 break;
481 default:
482 debug("%s: clock_adjust value %d invalid.\n",
483 dev->name, clock_adjust);
484 return -EINVAL;
485 }
486
487 /* Configure the DDR SDRAM Clock Control register */
488 out_be32(&im->ddr.sdram_clk_cntl, clock_adjust);
489
490 ext_refresh_rec = dev_read_u32_default(dev, "ext_refresh_rec", 0);
491 switch (ext_refresh_rec) {
492 case 0:
493 ext_refresh_rec_mask = 0 << TIMING_CFG3_EXT_REFREC_SHIFT;
494 break;
495 case 16:
496 ext_refresh_rec_mask = 1 << TIMING_CFG3_EXT_REFREC_SHIFT;
497 break;
498 case 32:
499 ext_refresh_rec_mask = 2 << TIMING_CFG3_EXT_REFREC_SHIFT;
500 break;
501 case 48:
502 ext_refresh_rec_mask = 3 << TIMING_CFG3_EXT_REFREC_SHIFT;
503 break;
504 case 64:
505 ext_refresh_rec_mask = 4 << TIMING_CFG3_EXT_REFREC_SHIFT;
506 break;
507 case 80:
508 ext_refresh_rec_mask = 5 << TIMING_CFG3_EXT_REFREC_SHIFT;
509 break;
510 case 96:
511 ext_refresh_rec_mask = 6 << TIMING_CFG3_EXT_REFREC_SHIFT;
512 break;
513 case 112:
514 ext_refresh_rec_mask = 7 << TIMING_CFG3_EXT_REFREC_SHIFT;
515 break;
516 default:
517 debug("%s: ext_refresh_rec value %d invalid.\n",
518 dev->name, ext_refresh_rec);
519 return -EINVAL;
520 }
521
522 /* Configure the DDR SDRAM Timing Configuration 3 register */
523 out_be32(&im->ddr.timing_cfg_3, ext_refresh_rec_mask);
524
525 read_to_write = dev_read_u32_default(dev, "read_to_write", 0);
526 if (read_to_write > 3) {
527 debug("%s: read_to_write value %d invalid.\n",
528 dev->name, read_to_write);
529 return -EINVAL;
530 }
531
532 write_to_read = dev_read_u32_default(dev, "write_to_read", 0);
533 if (write_to_read > 3) {
534 debug("%s: write_to_read value %d invalid.\n",
535 dev->name, write_to_read);
536 return -EINVAL;
537 }
538
539 read_to_read = dev_read_u32_default(dev, "read_to_read", 0);
540 if (read_to_read > 3) {
541 debug("%s: read_to_read value %d invalid.\n",
542 dev->name, read_to_read);
543 return -EINVAL;
544 }
545
546 write_to_write = dev_read_u32_default(dev, "write_to_write", 0);
547 if (write_to_write > 3) {
548 debug("%s: write_to_write value %d invalid.\n",
549 dev->name, write_to_write);
550 return -EINVAL;
551 }
552
553 active_powerdown_exit =
554 dev_read_u32_default(dev, "active_powerdown_exit", 0);
555 if (active_powerdown_exit > 7) {
556 debug("%s: active_powerdown_exit value %d invalid.\n",
557 dev->name, active_powerdown_exit);
558 return -EINVAL;
559 }
560
561 precharge_powerdown_exit =
562 dev_read_u32_default(dev, "precharge_powerdown_exit", 0);
563 if (precharge_powerdown_exit > 7) {
564 debug("%s: precharge_powerdown_exit value %d invalid.\n",
565 dev->name, precharge_powerdown_exit);
566 return -EINVAL;
567 }
568
569 odt_powerdown_exit = dev_read_u32_default(dev, "odt_powerdown_exit", 0);
570 if (odt_powerdown_exit > 15) {
571 debug("%s: odt_powerdown_exit value %d invalid.\n",
572 dev->name, odt_powerdown_exit);
573 return -EINVAL;
574 }
575
576 mode_reg_set_cycle = dev_read_u32_default(dev, "mode_reg_set_cycle", 0);
577 if (mode_reg_set_cycle > 15) {
578 debug("%s: mode_reg_set_cycle value %d invalid.\n",
579 dev->name, mode_reg_set_cycle);
580 return -EINVAL;
581 }
582
583 timing_cfg_0 = read_to_write << TIMING_CFG0_RWT_SHIFT |
584 write_to_read << TIMING_CFG0_WRT_SHIFT |
585 read_to_read << TIMING_CFG0_RRT_SHIFT |
586 write_to_write << TIMING_CFG0_WWT_SHIFT |
587 active_powerdown_exit << TIMING_CFG0_ACT_PD_EXIT_SHIFT |
588 precharge_powerdown_exit << TIMING_CFG0_PRE_PD_EXIT_SHIFT |
589 odt_powerdown_exit << TIMING_CFG0_ODT_PD_EXIT_SHIFT |
590 mode_reg_set_cycle << TIMING_CFG0_MRS_CYC_SHIFT;
591
592 out_be32(&im->ddr.timing_cfg_0, timing_cfg_0);
593
594 precharge_to_activate =
595 dev_read_u32_default(dev, "precharge_to_activate", 0);
596 if (precharge_to_activate > 7 || precharge_to_activate == 0) {
597 debug("%s: precharge_to_activate value %d invalid.\n",
598 dev->name, precharge_to_activate);
599 return -EINVAL;
600 }
601
602 activate_to_precharge =
603 dev_read_u32_default(dev, "activate_to_precharge", 0);
604 if (activate_to_precharge > 19) {
605 debug("%s: activate_to_precharge value %d invalid.\n",
606 dev->name, activate_to_precharge);
607 return -EINVAL;
608 }
609
610 activate_to_readwrite =
611 dev_read_u32_default(dev, "activate_to_readwrite", 0);
612 if (activate_to_readwrite > 7 || activate_to_readwrite == 0) {
613 debug("%s: activate_to_readwrite value %d invalid.\n",
614 dev->name, activate_to_readwrite);
615 return -EINVAL;
616 }
617
618 mcas_latency = dev_read_u32_default(dev, "mcas_latency", 0);
619 switch (mcas_latency) {
620 case CASLAT_20:
621 case CASLAT_25:
622 if (!IS_ENABLED(CONFIG_ARCH_MPC8308)) {
623 debug("%s: MCAS latency < 3.0 unsupported on MPC8308\n",
624 dev->name);
625 return -EINVAL;
626 }
627 /* fall through */
628 case CASLAT_30:
629 case CASLAT_35:
630 case CASLAT_40:
631 case CASLAT_45:
632 case CASLAT_50:
633 case CASLAT_55:
634 case CASLAT_60:
635 case CASLAT_65:
636 case CASLAT_70:
637 case CASLAT_75:
638 case CASLAT_80:
639 break;
640 default:
641 debug("%s: mcas_latency value %d invalid.\n",
642 dev->name, mcas_latency);
643 return -EINVAL;
644 }
645
646 refresh_recovery = dev_read_u32_default(dev, "refresh_recovery", 0);
647 if (refresh_recovery > 23 || refresh_recovery < 8) {
648 debug("%s: refresh_recovery value %d invalid.\n",
649 dev->name, refresh_recovery);
650 return -EINVAL;
651 }
652
653 last_data_to_precharge =
654 dev_read_u32_default(dev, "last_data_to_precharge", 0);
655 if (last_data_to_precharge > 7 || last_data_to_precharge == 0) {
656 debug("%s: last_data_to_precharge value %d invalid.\n",
657 dev->name, last_data_to_precharge);
658 return -EINVAL;
659 }
660
661 activate_to_activate =
662 dev_read_u32_default(dev, "activate_to_activate", 0);
663 if (activate_to_activate > 7 || activate_to_activate == 0) {
664 debug("%s: activate_to_activate value %d invalid.\n",
665 dev->name, activate_to_activate);
666 return -EINVAL;
667 }
668
669 last_write_data_to_read =
670 dev_read_u32_default(dev, "last_write_data_to_read", 0);
671 if (last_write_data_to_read > 7 || last_write_data_to_read == 0) {
672 debug("%s: last_write_data_to_read value %d invalid.\n",
673 dev->name, last_write_data_to_read);
674 return -EINVAL;
675 }
676
677 timing_cfg_1 = precharge_to_activate << TIMING_CFG1_PRETOACT_SHIFT |
678 (activate_to_precharge > 15 ?
679 activate_to_precharge - 16 :
680 activate_to_precharge) << TIMING_CFG1_ACTTOPRE_SHIFT |
681 activate_to_readwrite << TIMING_CFG1_ACTTORW_SHIFT |
682 mcas_latency << TIMING_CFG1_CASLAT_SHIFT |
683 (refresh_recovery - 8) << TIMING_CFG1_REFREC_SHIFT |
684 last_data_to_precharge << TIMING_CFG1_WRREC_SHIFT |
685 activate_to_activate << TIMING_CFG1_ACTTOACT_SHIFT |
686 last_write_data_to_read << TIMING_CFG1_WRTORD_SHIFT;
687
688 /* Configure the DDR SDRAM Timing Configuration 1 register */
689 out_be32(&im->ddr.timing_cfg_1, timing_cfg_1);
690
691 additive_latency = dev_read_u32_default(dev, "additive_latency", 0);
692 if (additive_latency > 5) {
693 debug("%s: additive_latency value %d invalid.\n",
694 dev->name, additive_latency);
695 return -EINVAL;
696 }
697
698 mcas_to_preamble_override =
699 dev_read_u32_default(dev, "mcas_to_preamble_override", 0);
700 switch (mcas_to_preamble_override) {
701 case READ_LAT_PLUS_1:
702 case READ_LAT:
703 case READ_LAT_PLUS_1_4:
704 case READ_LAT_PLUS_1_2:
705 case READ_LAT_PLUS_3_4:
706 case READ_LAT_PLUS_5_4:
707 case READ_LAT_PLUS_3_2:
708 case READ_LAT_PLUS_7_4:
709 case READ_LAT_PLUS_2:
710 case READ_LAT_PLUS_9_4:
711 case READ_LAT_PLUS_5_2:
712 case READ_LAT_PLUS_11_4:
713 case READ_LAT_PLUS_3:
714 case READ_LAT_PLUS_13_4:
715 case READ_LAT_PLUS_7_2:
716 case READ_LAT_PLUS_15_4:
717 case READ_LAT_PLUS_4:
718 case READ_LAT_PLUS_17_4:
719 case READ_LAT_PLUS_9_2:
720 case READ_LAT_PLUS_19_4:
721 break;
722 default:
723 debug("%s: mcas_to_preamble_override value %d invalid.\n",
724 dev->name, mcas_to_preamble_override);
725 return -EINVAL;
726 }
727
728 write_latency = dev_read_u32_default(dev, "write_latency", 0);
729 if (write_latency > 7 || write_latency == 0) {
730 debug("%s: write_latency value %d invalid.\n",
731 dev->name, write_latency);
732 return -EINVAL;
733 }
734
735 read_to_precharge = dev_read_u32_default(dev, "read_to_precharge", 0);
736 if (read_to_precharge > 4 || read_to_precharge == 0) {
737 debug("%s: read_to_precharge value %d invalid.\n",
738 dev->name, read_to_precharge);
739 return -EINVAL;
740 }
741
742 write_cmd_to_write_data =
743 dev_read_u32_default(dev, "write_cmd_to_write_data", 0);
744 switch (write_cmd_to_write_data) {
745 case CLOCK_DELAY_0:
746 case CLOCK_DELAY_1_4:
747 case CLOCK_DELAY_1_2:
748 case CLOCK_DELAY_3_4:
749 case CLOCK_DELAY_1:
750 case CLOCK_DELAY_5_4:
751 case CLOCK_DELAY_3_2:
752 break;
753 default:
754 debug("%s: write_cmd_to_write_data value %d invalid.\n",
755 dev->name, write_cmd_to_write_data);
756 return -EINVAL;
757 }
758
759 minimum_cke_pulse_width =
760 dev_read_u32_default(dev, "minimum_cke_pulse_width", 0);
761 if (minimum_cke_pulse_width > 4 || minimum_cke_pulse_width == 0) {
762 debug("%s: minimum_cke_pulse_width value %d invalid.\n",
763 dev->name, minimum_cke_pulse_width);
764 return -EINVAL;
765 }
766
767 four_activates_window =
768 dev_read_u32_default(dev, "four_activates_window", 0);
769 if (four_activates_window > 20 || four_activates_window == 0) {
770 debug("%s: four_activates_window value %d invalid.\n",
771 dev->name, four_activates_window);
772 return -EINVAL;
773 }
774
775 timing_cfg_2 = additive_latency << TIMING_CFG2_ADD_LAT_SHIFT |
776 mcas_to_preamble_override << TIMING_CFG2_CPO_SHIFT |
777 write_latency << TIMING_CFG2_WR_LAT_DELAY_SHIFT |
778 read_to_precharge << TIMING_CFG2_RD_TO_PRE_SHIFT |
779 write_cmd_to_write_data << TIMING_CFG2_WR_DATA_DELAY_SHIFT |
780 minimum_cke_pulse_width << TIMING_CFG2_CKE_PLS_SHIFT |
781 four_activates_window << TIMING_CFG2_FOUR_ACT_SHIFT;
782
783 out_be32(&im->ddr.timing_cfg_2, timing_cfg_2);
784
785 self_refresh = dev_read_u32_default(dev, "self_refresh", 0);
786 switch (self_refresh) {
787 case SREN_DISABLE:
788 case SREN_ENABLE:
789 break;
790 default:
791 debug("%s: self_refresh value %d invalid.\n",
792 dev->name, self_refresh);
793 return -EINVAL;
794 }
795
796 ecc = dev_read_u32_default(dev, "ecc", 0);
797 switch (ecc) {
798 case ECC_DISABLE:
799 case ECC_ENABLE:
800 break;
801 default:
802 debug("%s: ecc value %d invalid.\n", dev->name, ecc);
803 return -EINVAL;
804 }
805
806 registered_dram = dev_read_u32_default(dev, "registered_dram", 0);
807 switch (registered_dram) {
808 case RD_DISABLE:
809 case RD_ENABLE:
810 break;
811 default:
812 debug("%s: registered_dram value %d invalid.\n",
813 dev->name, registered_dram);
814 return -EINVAL;
815 }
816
817 sdram_type = dev_read_u32_default(dev, "sdram_type", 0);
818 switch (sdram_type) {
819 case TYPE_DDR1:
820 case TYPE_DDR2:
821 break;
822 default:
823 debug("%s: sdram_type value %d invalid.\n",
824 dev->name, sdram_type);
825 return -EINVAL;
826 }
827
828 dynamic_power_management =
829 dev_read_u32_default(dev, "dynamic_power_management", 0);
830 switch (dynamic_power_management) {
831 case DYN_PWR_DISABLE:
832 case DYN_PWR_ENABLE:
833 break;
834 default:
835 debug("%s: dynamic_power_management value %d invalid.\n",
836 dev->name, dynamic_power_management);
837 return -EINVAL;
838 }
839
840 databus_width = dev_read_u32_default(dev, "databus_width", 0);
841 switch (databus_width) {
842 case DATA_BUS_WIDTH_16:
843 case DATA_BUS_WIDTH_32:
844 break;
845 default:
846 debug("%s: databus_width value %d invalid.\n",
847 dev->name, databus_width);
848 return -EINVAL;
849 }
850
851 nc_auto_precharge = dev_read_u32_default(dev, "nc_auto_precharge", 0);
852 switch (nc_auto_precharge) {
853 case NCAP_DISABLE:
854 case NCAP_ENABLE:
855 break;
856 default:
857 debug("%s: nc_auto_precharge value %d invalid.\n",
858 dev->name, nc_auto_precharge);
859 return -EINVAL;
860 }
861
862 timing_2t = dev_read_u32_default(dev, "timing_2t", 0);
863 switch (timing_2t) {
864 case TIMING_1T:
865 case TIMING_2T:
866 break;
867 default:
868 debug("%s: timing_2t value %d invalid.\n",
869 dev->name, timing_2t);
870 return -EINVAL;
871 }
872
873 bank_interleaving_ctrl =
874 dev_read_u32_default(dev, "bank_interleaving_ctrl", 0);
875 switch (bank_interleaving_ctrl) {
876 case INTERLEAVE_NONE:
877 case INTERLEAVE_1_AND_2:
878 break;
879 default:
880 debug("%s: bank_interleaving_ctrl value %d invalid.\n",
881 dev->name, bank_interleaving_ctrl);
882 return -EINVAL;
883 }
884
885 precharge_bit_8 = dev_read_u32_default(dev, "precharge_bit_8", 0);
886 switch (precharge_bit_8) {
887 case PRECHARGE_MA_10:
888 case PRECHARGE_MA_8:
889 break;
890 default:
891 debug("%s: precharge_bit_8 value %d invalid.\n",
892 dev->name, precharge_bit_8);
893 return -EINVAL;
894 }
895
896 half_strength = dev_read_u32_default(dev, "half_strength", 0);
897 switch (half_strength) {
898 case STRENGTH_FULL:
899 case STRENGTH_HALF:
900 break;
901 default:
902 debug("%s: half_strength value %d invalid.\n",
903 dev->name, half_strength);
904 return -EINVAL;
905 }
906
907 bypass_initialization =
908 dev_read_u32_default(dev, "bypass_initialization", 0);
909 switch (bypass_initialization) {
910 case INITIALIZATION_DONT_BYPASS:
911 case INITIALIZATION_BYPASS:
912 break;
913 default:
914 debug("%s: bypass_initialization value %d invalid.\n",
915 dev->name, bypass_initialization);
916 return -EINVAL;
917 }
918
919 sdram_cfg = self_refresh << SDRAM_CFG_SREN_SHIFT |
920 ecc << SDRAM_CFG_ECC_EN_SHIFT |
921 registered_dram << SDRAM_CFG_RD_EN_SHIFT |
922 sdram_type << SDRAM_CFG_SDRAM_TYPE_SHIFT |
923 dynamic_power_management << SDRAM_CFG_DYN_PWR_SHIFT |
924 databus_width << SDRAM_CFG_DBW_SHIFT |
925 nc_auto_precharge << SDRAM_CFG_NCAP_SHIFT |
926 timing_2t << SDRAM_CFG_2T_EN_SHIFT |
927 bank_interleaving_ctrl << SDRAM_CFG_BA_INTLV_CTL_SHIFT |
928 precharge_bit_8 << SDRAM_CFG_PCHB8_SHIFT |
929 half_strength << SDRAM_CFG_HSE_SHIFT |
930 bypass_initialization << SDRAM_CFG_BI_SHIFT;
931
932 out_be32(&im->ddr.sdram_cfg, sdram_cfg);
933
934 force_self_refresh = dev_read_u32_default(dev, "force_self_refresh", 0);
935 switch (force_self_refresh) {
936 case MODE_NORMAL:
937 case MODE_REFRESH:
938 break;
939 default:
940 debug("%s: force_self_refresh value %d invalid.\n",
941 dev->name, force_self_refresh);
942 return -EINVAL;
943 }
944
945 dll_reset = dev_read_u32_default(dev, "dll_reset", 0);
946 switch (dll_reset) {
947 case DLL_RESET_ENABLE:
948 case DLL_RESET_DISABLE:
949 break;
950 default:
951 debug("%s: dll_reset value %d invalid.\n",
952 dev->name, dll_reset);
953 return -EINVAL;
954 }
955
956 dqs_config = dev_read_u32_default(dev, "dqs_config", 0);
957 switch (dqs_config) {
958 case DQS_TRUE:
959 break;
960 default:
961 debug("%s: dqs_config value %d invalid.\n",
962 dev->name, dqs_config);
963 return -EINVAL;
964 }
965
966 odt_config = dev_read_u32_default(dev, "odt_config", 0);
967 switch (odt_config) {
968 case ODT_ASSERT_NEVER:
969 case ODT_ASSERT_WRITES:
970 case ODT_ASSERT_READS:
971 case ODT_ASSERT_ALWAYS:
972 break;
973 default:
974 debug("%s: odt_config value %d invalid.\n",
975 dev->name, odt_config);
976 return -EINVAL;
977 }
978
979 posted_refreshes = dev_read_u32_default(dev, "posted_refreshes", 0);
980 if (posted_refreshes > 8 || posted_refreshes == 0) {
981 debug("%s: posted_refreshes value %d invalid.\n",
982 dev->name, posted_refreshes);
983 return -EINVAL;
984 }
985
986 sdram_cfg2 = force_self_refresh << SDRAM_CFG2_FRC_SR_SHIFT |
987 dll_reset << SDRAM_CFG2_DLL_RST_DIS |
988 dqs_config << SDRAM_CFG2_DQS_CFG |
989 odt_config << SDRAM_CFG2_ODT_CFG |
990 posted_refreshes << SDRAM_CFG2_NUM_PR;
991
992 out_be32(&im->ddr.sdram_cfg2, sdram_cfg2);
993
994 sdmode = dev_read_u32_default(dev, "sdmode", 0);
995 if (sdmode > 0xFFFF) {
996 debug("%s: sdmode value %d invalid.\n",
997 dev->name, sdmode);
998 return -EINVAL;
999 }
1000
1001 esdmode = dev_read_u32_default(dev, "esdmode", 0);
1002 if (esdmode > 0xFFFF) {
1003 debug("%s: esdmode value %d invalid.\n", dev->name, esdmode);
1004 return -EINVAL;
1005 }
1006
1007 sdram_mode = sdmode << SDRAM_MODE_SD_SHIFT |
1008 esdmode << SDRAM_MODE_ESD_SHIFT;
1009
1010 out_be32(&im->ddr.sdram_mode, sdram_mode);
1011
1012 esdmode2 = dev_read_u32_default(dev, "esdmode2", 0);
1013 if (esdmode2 > 0xFFFF) {
1014 debug("%s: esdmode2 value %d invalid.\n", dev->name, esdmode2);
1015 return -EINVAL;
1016 }
1017
1018 esdmode3 = dev_read_u32_default(dev, "esdmode3", 0);
1019 if (esdmode3 > 0xFFFF) {
1020 debug("%s: esdmode3 value %d invalid.\n", dev->name, esdmode3);
1021 return -EINVAL;
1022 }
1023
1024 sdram_mode2 = esdmode2 << SDRAM_MODE2_ESD2_SHIFT |
1025 esdmode3 << SDRAM_MODE2_ESD3_SHIFT;
1026
1027 out_be32(&im->ddr.sdram_mode2, sdram_mode2);
1028
1029 refresh_interval = dev_read_u32_default(dev, "refresh_interval", 0);
1030 if (refresh_interval > 0xFFFF) {
1031 debug("%s: refresh_interval value %d invalid.\n",
1032 dev->name, refresh_interval);
1033 return -EINVAL;
1034 }
1035
1036 precharge_interval = dev_read_u32_default(dev, "precharge_interval", 0);
1037 if (precharge_interval > 0x3FFF) {
1038 debug("%s: precharge_interval value %d invalid.\n",
1039 dev->name, precharge_interval);
1040 return -EINVAL;
1041 }
1042
1043 sdram_interval = refresh_interval << SDRAM_INTERVAL_REFINT_SHIFT |
1044 precharge_interval << SDRAM_INTERVAL_BSTOPRE_SHIFT;
1045
1046 out_be32(&im->ddr.sdram_interval, sdram_interval);
1047 sync();
1048
1049 /* Enable DDR controller */
1050 setbits_be32(&im->ddr.sdram_cfg, SDRAM_CFG_MEM_EN);
1051 sync();
1052
1053 dev_for_each_subnode(subnode, dev) {
1054 u32 val[3];
1055 u32 addr, size;
1056
1057 /* CS, map address, size -> three values */
1058 ofnode_read_u32_array(subnode, "reg", val, 3);
1059
1060 addr = val[1];
1061 size = val[2];
1062
1063 priv->total_size += get_ram_size((long int *)addr, size);
1064 };
1065
1066 gd->ram_size = priv->total_size;
1067
1068 return 0;
1069}
1070
1071static int mpc83xx_sdram_get_info(struct udevice *dev, struct ram_info *info)
1072{
1073 /* TODO(mario.six@gdsys.cc): Implement */
1074 return 0;
1075}
1076
1077static struct ram_ops mpc83xx_sdram_ops = {
1078 .get_info = mpc83xx_sdram_get_info,
1079};
1080
1081static const struct udevice_id mpc83xx_sdram_ids[] = {
1082 { .compatible = "fsl,mpc83xx-mem-controller" },
1083 { /* sentinel */ }
1084};
1085
1086U_BOOT_DRIVER(mpc83xx_sdram) = {
1087 .name = "mpc83xx_sdram",
1088 .id = UCLASS_RAM,
1089 .of_match = mpc83xx_sdram_ids,
1090 .ops = &mpc83xx_sdram_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -07001091 .of_to_plat = mpc83xx_sdram_of_to_plat,
Mario Six538b5752018-08-06 10:23:30 +02001092 .probe = mpc83xx_sdram_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001093 .priv_auto = sizeof(struct mpc83xx_sdram_priv),
Mario Six538b5752018-08-06 10:23:30 +02001094};