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