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