1 /*******************************************************************************
2 Copyright (C) Marvell International Ltd. and its affiliates
4 This software file (the "File") is owned and distributed by Marvell
5 International Ltd. and/or its affiliates ("Marvell") under the following
6 alternative licensing terms. Once you have made an election to distribute the
7 File under one of the following license alternatives, please (i) delete this
8 introductory statement regarding license alternatives, (ii) delete the two
9 license alternatives that you have not elected to use and (iii) preserve the
10 Marvell copyright notice above.
12 ********************************************************************************
13 Marvell Commercial License Option
15 If you received this File from Marvell and you have entered into a commercial
16 license agreement (a "Commercial License") with Marvell, the File is licensed
17 to you under the terms of the applicable Commercial License.
19 ********************************************************************************
20 Marvell GPL License Option
22 If you received this File from Marvell, you may opt to use, redistribute and/or
23 modify this File in accordance with the terms and conditions of the General
24 Public License Version 2, June 1991 (the "GPL License"), a copy of which is
25 available along with the File in the license.txt file or by writing to the Free
26 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 or
27 on the worldwide web at http://www.gnu.org/licenses/gpl.txt.
29 THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE IMPLIED
30 WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY
31 DISCLAIMED. The GPL License provides additional details about this warranty
33 ********************************************************************************
34 Marvell BSD License Option
36 If you received this File from Marvell, you may opt to use, redistribute and/or
37 modify this File under the following licensing terms.
38 Redistribution and use in source and binary forms, with or without modification,
39 are permitted provided that the following conditions are met:
41 * Redistributions of source code must retain the above copyright notice,
42 this list of conditions and the following disclaimer.
44 * Redistributions in binary form must reproduce the above copyright
45 notice, this list of conditions and the following disclaimer in the
46 documentation and/or other materials provided with the distribution.
48 * Neither the name of Marvell nor the names of its contributors may be
49 used to endorse or promote products derived from this software without
50 specific prior written permission.
52 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
53 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
54 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
55 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
56 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
57 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
58 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
59 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
61 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *******************************************************************************/
67 #include "ddr2/mvDramIf.h"
68 #include "ctrlEnv/sys/mvCpuIf.h"
70 #include "ddr2/mvDramIfStaticInit.h"
72 /* #define MV_DEBUG */
79 /* DRAM bank presence encoding */
80 #define BANK_PRESENT_CS0 0x1
81 #define BANK_PRESENT_CS0_CS1 0x3
82 #define BANK_PRESENT_CS0_CS2 0x5
83 #define BANK_PRESENT_CS0_CS1_CS2 0x7
84 #define BANK_PRESENT_CS0_CS2_CS3 0xd
85 #define BANK_PRESENT_CS0_CS2_CS3_CS4 0xf
88 #ifndef MV_STATIC_DRAM_ON_BOARD
89 static void sdramDDr2OdtConfig(MV_DRAM_BANK_INFO
*pBankInfo
);
90 static MV_U32
dunitCtrlLowRegCalc(MV_DRAM_BANK_INFO
*pBankInfo
, MV_U32 minCas
, MV_U32 busClk
, MV_STATUS TTmode
);
91 static MV_U32
dunitCtrlHighRegCalc(MV_DRAM_BANK_INFO
*pBankInfo
, MV_U32 busClk
);
92 static MV_U32
sdramModeRegCalc(MV_U32 minCas
);
93 static MV_U32
sdramExtModeRegCalc(MV_DRAM_BANK_INFO
*pBankInfo
, MV_U32 busClk
);
94 static MV_U32
sdramAddrCtrlRegCalc(MV_DRAM_BANK_INFO
*pBankInfo
, MV_DRAM_BANK_INFO
*pBankInfoDIMM1
);
95 static MV_U32
sdramConfigRegCalc(MV_DRAM_BANK_INFO
*pBankInfo
, MV_DRAM_BANK_INFO
*pBankInfo2
, MV_U32 busClk
);
96 static MV_U32
minCasCalc(MV_DRAM_BANK_INFO
*pBankInfo
,MV_DRAM_BANK_INFO
*pBankInfo2
, MV_U32 busClk
, MV_U32 forcedCl
);
97 static MV_U32
sdramTimeCtrlLowRegCalc(MV_DRAM_BANK_INFO
*pBankInfo
, MV_U32 minCas
, MV_U32 busClk
);
98 static MV_U32
sdramTimeCtrlHighRegCalc(MV_DRAM_BANK_INFO
*pBankInfo
, MV_U32 busClk
);
99 static MV_U32
sdramDdr2TimeLoRegCalc(MV_U32 minCas
);
100 static MV_U32
sdramDdr2TimeHiRegCalc(MV_U32 minCas
);
102 MV_32 DRAM_CS_Order
[MV_DRAM_MAX_CS
] = {N_A
104 #ifdef MV_INCLUDE_SDRAM_CS1
107 #ifdef MV_INCLUDE_SDRAM_CS2
110 #ifdef MV_INCLUDE_SDRAM_CS3
114 /* Get DRAM size of CS num */
115 MV_U32
mvDramCsSizeGet(MV_U32 csNum
)
117 MV_DRAM_BANK_INFO bankInfo
;
118 MV_U32 size
, deviceW
, dimmW
;
123 if(MV_OK
== mvDramBankInfoGet(csNum
, &bankInfo
))
125 if (0 == bankInfo
.size
)
128 /* Note that the Dimm width might be different then the device DRAM width */
130 temp
= MV_REG_READ(SDRAM_CONFIG_REG
);
131 deviceW
= ((temp
& SDRAM_DWIDTH_MASK
) == SDRAM_DWIDTH_32BIT
)? 32 : 64;
133 deviceW
= 16 /* KW family */;
135 dimmW
= bankInfo
.dataWidth
- (bankInfo
.dataWidth
% 16);
136 size
= ((bankInfo
.size
<< 20) / (dimmW
/deviceW
));
142 /*******************************************************************************
143 * mvDramIfDetect - Prepare DRAM interface configuration values.
146 * This function implements the full DRAM detection and timing
147 * configuration for best system performance.
148 * Since this routine runs from a ROM device (Boot Flash), its stack
149 * resides on RAM, that might be the system DRAM. Changing DRAM
150 * configuration values while keeping vital data in DRAM is risky. That
151 * is why the function does not preform the configuration setting but
152 * prepare those in predefined 32bit registers (in this case IDMA
153 * registers are used) for other routine to perform the settings.
154 * The function will call for board DRAM SPD information for each DRAM
155 * chip select. The function will then analyze those SPD parameters of
156 * all DRAM banks in order to decide on DRAM configuration compatible
157 * for all DRAM banks.
158 * The function will set the CPU DRAM address decode registers.
159 * Note: This routine prepares values that will overide configuration of
160 * mvDramBasicAsmInit().
163 * forcedCl - Forced CAL Latency. If equal to zero, do not force.
164 * eccDisable - Force down the ECC.
172 *******************************************************************************/
173 MV_STATUS
mvDramIfDetect(MV_U32 forcedCl
, MV_BOOL eccDisable
)
175 MV_32 MV_DRAM_CS_order
[MV_DRAM_MAX_CS
] = {
177 #ifdef MV_INCLUDE_SDRAM_CS1
180 #ifdef MV_INCLUDE_SDRAM_CS2
183 #ifdef MV_INCLUDE_SDRAM_CS3
187 MV_U32 busClk
, deviceW
, dimmW
;
188 MV_U32 numOfAllDevices
= 0;
190 #ifndef MV_STATIC_DRAM_ON_BOARD
191 MV_DRAM_BANK_INFO bankInfo
[MV_DRAM_MAX_CS
];
192 MV_U32 size
, base
= 0, i
, j
, temp
, busClkPs
;
194 MV_CPU_DEC_WIN dramDecWin
;
195 dramDecWin
.addrWin
.baseHigh
= 0;
198 busClk
= mvBoardSysClkGet();
202 mvOsPrintf("Dram: ERR. Can't detect system clock! \n");
206 #ifndef MV_STATIC_DRAM_ON_BOARD
208 busClkPs
= 1000000000 / (busClk
/ 1000); /* in ps units */
209 /* we will use bank 0 as the representative of the all the DRAM banks, */
210 /* since bank 0 must exist. */
211 for(i
= 0; i
< MV_DRAM_MAX_CS
; i
++)
214 if(MV_OK
== mvDramBankInfoGet(i
, &bankInfo
[i
]))
216 DB(mvOsPrintf("Dram: Find bank %d\n", i
));
217 /* check it isn't SDRAM */
218 if(bankInfo
[i
].memoryType
!= MEM_TYPE_DDR2
)
220 mvOsOutput("Dram: ERR. SDRAM type not supported !!!\n");
224 /* All banks must support the Mclk freqency */
225 if(bankInfo
[i
].minCycleTimeAtMaxCasLatPs
> busClkPs
)
227 mvOsOutput("Dram: ERR. Bank %d doesn't support memory clock!!!\n", i
);
231 /* All banks must support registry in order to activate it */
232 if(bankInfo
[i
].registeredAddrAndControlInputs
!=
233 bankInfo
[0].registeredAddrAndControlInputs
)
235 mvOsOutput("Dram: ERR. different Registered settings !!!\n");
239 /* All banks must support same ECC mode */
240 if(bankInfo
[i
].errorCheckType
!=
241 bankInfo
[0].errorCheckType
)
243 mvOsOutput("Dram: ERR. different ECC settings !!!\n");
250 if( i
== 0 ) /* bank 0 doesn't exist */
252 mvOsOutput("Dram: ERR. Fail to detect bank 0 !!!\n");
257 DB(mvOsPrintf("Dram: Could not find bank %d\n", i
));
258 bankInfo
[i
].size
= 0; /* Mark this bank as non exist */
263 #ifdef MV_INCLUDE_SDRAM_CS2
264 if (bankInfo
[SDRAM_CS0
].size
< bankInfo
[SDRAM_CS2
].size
)
266 MV_DRAM_CS_order
[0] = SDRAM_CS2
;
267 MV_DRAM_CS_order
[1] = SDRAM_CS3
;
268 MV_DRAM_CS_order
[2] = SDRAM_CS0
;
269 MV_DRAM_CS_order
[3] = SDRAM_CS1
;
270 DRAM_CS_Order
[0] = SDRAM_CS2
;
271 DRAM_CS_Order
[1] = SDRAM_CS3
;
272 DRAM_CS_Order
[2] = SDRAM_CS0
;
273 DRAM_CS_Order
[3] = SDRAM_CS1
;
279 MV_DRAM_CS_order
[0] = SDRAM_CS0
;
280 MV_DRAM_CS_order
[1] = SDRAM_CS1
;
281 DRAM_CS_Order
[0] = SDRAM_CS0
;
282 DRAM_CS_Order
[1] = SDRAM_CS1
;
283 #ifdef MV_INCLUDE_SDRAM_CS2
284 MV_DRAM_CS_order
[2] = SDRAM_CS2
;
285 MV_DRAM_CS_order
[3] = SDRAM_CS3
;
286 DRAM_CS_Order
[2] = SDRAM_CS2
;
287 DRAM_CS_Order
[3] = SDRAM_CS3
;
291 for(j
= 0; j
< MV_DRAM_MAX_CS
; j
++)
293 i
= MV_DRAM_CS_order
[j
];
295 if (0 == bankInfo
[i
].size
)
298 /* Init the CPU window decode */
299 /* Note that the Dimm width might be different then the device DRAM width */
301 temp
= MV_REG_READ(SDRAM_CONFIG_REG
);
302 deviceW
= ((temp
& SDRAM_DWIDTH_MASK
) == SDRAM_DWIDTH_32BIT
)? 32 : 64;
304 deviceW
= 16 /* KW family */;
306 dimmW
= bankInfo
[0].dataWidth
- (bankInfo
[0].dataWidth
% 16);
307 size
= ((bankInfo
[i
].size
<< 20) / (dimmW
/deviceW
));
309 /* We can not change DRAM window settings while excecuting */
310 /* code from it. That is why we skip the DRAM CS[0], saving */
311 /* it to the ROM configuration routine */
313 numOfAllDevices
+= bankInfo
[i
].numberOfDevices
;
314 if (i
== MV_DRAM_CS_order
[0])
317 /* Translate the given window size to register format */
318 sizeToReg
= ctrlSizeToReg(size
, SCSR_SIZE_ALIGNMENT
);
319 /* Size parameter validity check. */
322 mvOsOutput("DRAM: mvCtrlAddrDecToReg: ERR. Win %d size invalid.\n"
327 DB(mvOsPrintf("Dram: Bank 0 Size - %x\n",sizeToReg
);)
328 sizeToReg
= (sizeToReg
<< SCSR_SIZE_OFFS
);
329 sizeToReg
|= SCSR_WIN_EN
;
330 MV_REG_WRITE(DRAM_BUF_REG0
, sizeToReg
);
334 dramDecWin
.addrWin
.baseLow
= base
;
335 dramDecWin
.addrWin
.size
= size
;
336 dramDecWin
.enable
= MV_TRUE
;
337 DB(mvOsPrintf("Dram: Enable window %d base 0x%x, size=0x%x\n",i
, base
, size
));
339 /* Check if the DRAM size is more then 3GByte */
340 if (base
< 0xC0000000)
342 DB(mvOsPrintf("Dram: Enable window %d base 0x%x, size=0x%x\n",i
, base
, size
));
343 if (MV_OK
!= mvCpuIfTargetWinSet(i
, &dramDecWin
))
345 mvOsPrintf("Dram: ERR. Fail to set bank %d!!!\n", SDRAM_CS0
+ i
);
353 /* update the suportedCasLatencies mask */
354 bankInfo
[0].suportedCasLatencies
&= bankInfo
[i
].suportedCasLatencies
;
357 /* calculate minimum CAS */
358 minCas
= minCasCalc(&bankInfo
[0], &bankInfo
[2], busClk
, forcedCl
);
361 mvOsOutput("Dram: Warn: Could not find CAS compatible to SysClk %dMhz\n",
364 minCas
= DDR2_CL_4
; /* Continue with this CAS */
365 mvOsOutput("Set default CAS latency 4\n");
368 /* calc SDRAM_CONFIG_REG and save it to temp register */
369 temp
= sdramConfigRegCalc(&bankInfo
[0],&bankInfo
[2], busClk
);
372 mvOsOutput("Dram: ERR. sdramConfigRegCalc failed !!!\n");
376 /* check if ECC is enabled by the user */
382 DB(mvOsPrintf("Dram: sdramConfigRegCalc - %x\n",temp
);)
383 MV_REG_WRITE(DRAM_BUF_REG1
, temp
);
385 /* calc SDRAM_MODE_REG and save it to temp register */
386 temp
= sdramModeRegCalc(minCas
);
389 mvOsOutput("Dram: ERR. sdramModeRegCalc failed !!!\n");
392 DB(mvOsPrintf("Dram: sdramModeRegCalc - %x\n",temp
);)
393 MV_REG_WRITE(DRAM_BUF_REG2
, temp
);
395 /* calc SDRAM_EXTENDED_MODE_REG and save it to temp register */
396 temp
= sdramExtModeRegCalc(&bankInfo
[0], busClk
);
399 mvOsOutput("Dram: ERR. sdramExtModeRegCalc failed !!!\n");
402 DB(mvOsPrintf("Dram: sdramExtModeRegCalc - %x\n",temp
);)
403 MV_REG_WRITE(DRAM_BUF_REG10
, temp
);
405 /* calc D_UNIT_CONTROL_LOW and save it to temp register */
407 DB(mvOsPrintf("Dram: numOfAllDevices = %x\n",numOfAllDevices
);)
408 if( (numOfAllDevices
> 9) && (bankInfo
[0].registeredAddrAndControlInputs
== MV_FALSE
) )
410 if ( ( (numOfAllDevices
> 9) && (busClk
> MV_BOARD_SYSCLK_200MHZ
) ) ||
411 (numOfAllDevices
> 18) )
413 mvOsOutput("Enable 2T ");
418 temp
= dunitCtrlLowRegCalc(&bankInfo
[0], minCas
, busClk
, TTMode
);
421 mvOsOutput("Dram: ERR. dunitCtrlLowRegCalc failed !!!\n");
424 DB(mvOsPrintf("Dram: dunitCtrlLowRegCalc - %x\n",temp
);)
425 MV_REG_WRITE(DRAM_BUF_REG3
, temp
);
427 /* calc D_UNIT_CONTROL_HIGH and save it to temp register */
428 temp
= dunitCtrlHighRegCalc(&bankInfo
[0], busClk
);
431 mvOsOutput("Dram: ERR. dunitCtrlHighRegCalc failed !!!\n");
434 DB(mvOsPrintf("Dram: dunitCtrlHighRegCalc - %x\n",temp
);)
435 /* check if ECC is enabled by the user */
438 /* turn off sample stage if no ecc */
439 temp
&= ~SDRAM__D2P_EN
;;
441 MV_REG_WRITE(DRAM_BUF_REG13
, temp
);
443 /* calc SDRAM_ADDR_CTRL_REG and save it to temp register */
444 temp
= sdramAddrCtrlRegCalc(&bankInfo
[0],&bankInfo
[2]);
447 mvOsOutput("Dram: ERR. sdramAddrCtrlRegCalc failed !!!\n");
450 DB(mvOsPrintf("Dram: sdramAddrCtrlRegCalc - %x\n",temp
);)
451 MV_REG_WRITE(DRAM_BUF_REG4
, temp
);
453 /* calc SDRAM_TIMING_CTRL_LOW_REG and save it to temp register */
454 temp
= sdramTimeCtrlLowRegCalc(&bankInfo
[0], minCas
, busClk
);
457 mvOsOutput("Dram: ERR. sdramTimeCtrlLowRegCalc failed !!!\n");
460 DB(mvOsPrintf("Dram: sdramTimeCtrlLowRegCalc - %x\n",temp
);)
461 MV_REG_WRITE(DRAM_BUF_REG5
, temp
);
463 /* calc SDRAM_TIMING_CTRL_HIGH_REG and save it to temp register */
464 temp
= sdramTimeCtrlHighRegCalc(&bankInfo
[0], busClk
);
467 mvOsOutput("Dram: ERR. sdramTimeCtrlHighRegCalc failed !!!\n");
470 DB(mvOsPrintf("Dram: sdramTimeCtrlHighRegCalc - %x\n",temp
);)
471 MV_REG_WRITE(DRAM_BUF_REG6
, temp
);
473 sdramDDr2OdtConfig(bankInfo
);
475 /* calc DDR2_SDRAM_TIMING_LOW_REG and save it to temp register */
476 temp
= sdramDdr2TimeLoRegCalc(minCas
);
479 mvOsOutput("Dram: ERR. sdramDdr2TimeLoRegCalc failed !!!\n");
482 DB(mvOsPrintf("Dram: sdramDdr2TimeLoRegCalc - %x\n",temp
);)
483 MV_REG_WRITE(DRAM_BUF_REG11
, temp
);
485 /* calc DDR2_SDRAM_TIMING_HIGH_REG and save it to temp register */
486 temp
= sdramDdr2TimeHiRegCalc(minCas
);
489 mvOsOutput("Dram: ERR. sdramDdr2TimeHiRegCalc failed !!!\n");
492 DB(mvOsPrintf("Dram: sdramDdr2TimeHiRegCalc - %x\n",temp
);)
493 MV_REG_WRITE(DRAM_BUF_REG12
, temp
);
496 /* Note that DDR SDRAM Address/Control and Data pad calibration */
497 /* settings is done in mvSdramIfConfig.s */
503 /*******************************************************************************
504 * mvDramIfBankBaseGet - Get DRAM interface bank base.
507 * This function returns the 32 bit base address of a given DRAM bank.
510 * bankNum - Bank number.
516 * DRAM bank size. If bank is disabled or paramter is invalid, the
517 * function returns -1.
519 *******************************************************************************/
520 MV_U32
mvDramIfBankBaseGet(MV_U32 bankNum
)
522 DB(mvOsPrintf("Dram: mvDramIfBankBaseGet Bank %d base addr is %x \n",
523 bankNum
, mvCpuIfTargetWinBaseLowGet(SDRAM_CS0
+ bankNum
)));
524 return mvCpuIfTargetWinBaseLowGet(SDRAM_CS0
+ bankNum
);
527 /*******************************************************************************
528 * mvDramIfBankSizeGet - Get DRAM interface bank size.
531 * This function returns the size of a given DRAM bank.
534 * bankNum - Bank number.
540 * DRAM bank size. If bank is disabled the function return '0'. In case
541 * or paramter is invalid, the function returns -1.
543 *******************************************************************************/
544 MV_U32
mvDramIfBankSizeGet(MV_U32 bankNum
)
546 DB(mvOsPrintf("Dram: mvDramIfBankSizeGet Bank %d size is %x \n",
547 bankNum
, mvCpuIfTargetWinSizeGet(SDRAM_CS0
+ bankNum
)));
548 return mvCpuIfTargetWinSizeGet(SDRAM_CS0
+ bankNum
);
552 /*******************************************************************************
553 * mvDramIfSizeGet - Get DRAM interface total size.
556 * This function get the DRAM total size.
565 * DRAM total size. In case or paramter is invalid, the function
568 *******************************************************************************/
569 MV_U32
mvDramIfSizeGet(MV_VOID
)
573 for(i
= 0; i
< MV_DRAM_MAX_CS
; i
++)
574 size
+= mvDramIfBankSizeGet(i
);
576 DB(mvOsPrintf("Dram: mvDramIfSizeGet size is %x \n",size
));
580 /*******************************************************************************
581 * mvDramIfSingleBitErrThresholdSet - Set single bit ECC threshold.
584 * The ECC single bit error threshold is the number of single bit
585 * errors to happen before the Dunit generates an interrupt.
586 * This function set single bit ECC threshold.
589 * threshold - threshold.
595 * MV_BAD_PARAM if threshold is to big, MV_OK otherwise.
597 *******************************************************************************/
598 MV_STATUS
mvDramIfSingleBitErrThresholdSet(MV_U32 threshold
)
602 if (threshold
> SECR_THRECC_MAX
)
607 regVal
= MV_REG_READ(SDRAM_ECC_CONTROL_REG
);
608 regVal
&= ~SECR_THRECC_MASK
;
609 regVal
|= ((SECR_THRECC(threshold
) & SECR_THRECC_MASK
));
610 MV_REG_WRITE(SDRAM_ECC_CONTROL_REG
, regVal
);
615 #ifndef MV_STATIC_DRAM_ON_BOARD
616 /*******************************************************************************
617 * minCasCalc - Calculate the Minimum CAS latency which can be used.
620 * Calculate the minimum CAS latency that can be used, base on the DRAM
621 * parameters and the SDRAM bus Clock freq.
624 * busClk - the DRAM bus Clock.
625 * pBankInfo - bank info parameters.
626 * forcedCl - Forced CAS Latency multiplied by 10. If equal to zero, do not force.
632 * The minimum CAS Latency. The function returns 0 if max CAS latency
633 * supported by banks is incompatible with system bus clock frequancy.
635 *******************************************************************************/
637 static MV_U32
minCasCalc(MV_DRAM_BANK_INFO
*pBankInfo
,MV_DRAM_BANK_INFO
*pBankInfo2
, MV_U32 busClk
, MV_U32 forcedCl
)
640 MV_U32 busClkPs
= 1000000000 / (busClk
/ 1000); /* in ps units */
641 MV_U32 startBit
, stopBit
;
642 MV_U32 minCas0
= 0, minCas2
= 0;
646 *******-******-******-******-******-******-******-*******
647 * bit7 | bit6 | bit5 | bit4 | bit3 | bit2 | bit1 | bit0 *
648 *******-******-******-******-******-******-******-*******
649 CAS = * TBD | TBD | 5 | 4 | 3 | 2 | TBD | TBD *
650 Disco VI= * TBD | TBD | 5 | 4 | 3 | TBD | TBD | TBD *
651 Disco Duo= * TBD | 6 | 5 | 4 | 3 | TBD | TBD | TBD *
652 *********************************************************/
655 /* If we are asked to use the forced CAL we change the suported CAL to be forcedCl only */
658 mvOsOutput("DRAM: Using forced CL %d.%d\n", (forcedCl
/ 10), (forcedCl
% 10));
661 pBankInfo
->suportedCasLatencies
= 0x08;
662 else if (forcedCl
== 40)
663 pBankInfo
->suportedCasLatencies
= 0x10;
664 else if (forcedCl
== 50)
665 pBankInfo
->suportedCasLatencies
= 0x20;
666 else if (forcedCl
== 60)
667 pBankInfo
->suportedCasLatencies
= 0x40;
670 mvOsPrintf("Forced CL %d.%d not supported. Set default CL 4\n",
671 (forcedCl
/ 10), (forcedCl
% 10));
672 pBankInfo
->suportedCasLatencies
= 0x10;
675 return pBankInfo
->suportedCasLatencies
;
678 /* go over the supported cas mask from Max Cas down and check if the */
679 /* SysClk stands in its time requirments. */
681 DB(mvOsPrintf("Dram: minCasCalc supported mask = %x busClkPs = %x \n",
682 pBankInfo
->suportedCasLatencies
,busClkPs
));
684 for(j
= 7; j
> 0; j
--)
686 if((pBankInfo
->suportedCasLatencies
>> j
) & BIT0
)
688 /* Reset the bits for CL incompatible for the sysClk */
692 if (pBankInfo
->minCycleTimeAtMaxCasLatPs
> busClkPs
)
693 pBankInfo
->suportedCasLatencies
&= ~(BIT0
<< j
);
697 if (pBankInfo
->minCycleTimeAtMaxCasLatMinus1Ps
> busClkPs
)
698 pBankInfo
->suportedCasLatencies
&= ~(BIT0
<< j
);
702 if (pBankInfo
->minCycleTimeAtMaxCasLatMinus2Ps
> busClkPs
)
703 pBankInfo
->suportedCasLatencies
&= ~(BIT0
<< j
);
707 pBankInfo
->suportedCasLatencies
&= ~(BIT0
<< j
);
713 DB(mvOsPrintf("Dram: minCasCalc support = %x (after SysCC calc)\n",
714 pBankInfo
->suportedCasLatencies
));
717 DB(mvOsPrintf("Dram2: minCasCalc supported mask = %x busClkPs = %x \n",
718 pBankInfo2
->suportedCasLatencies
,busClkPs
));
719 for(j
= 7; j
> 0; j
--)
721 if((pBankInfo2
->suportedCasLatencies
>> j
) & BIT0
)
723 /* Reset the bits for CL incompatible for the sysClk */
727 if (pBankInfo2
->minCycleTimeAtMaxCasLatPs
> busClkPs
)
728 pBankInfo2
->suportedCasLatencies
&= ~(BIT0
<< j
);
732 if (pBankInfo2
->minCycleTimeAtMaxCasLatMinus1Ps
> busClkPs
)
733 pBankInfo2
->suportedCasLatencies
&= ~(BIT0
<< j
);
737 if (pBankInfo2
->minCycleTimeAtMaxCasLatMinus2Ps
> busClkPs
)
738 pBankInfo2
->suportedCasLatencies
&= ~(BIT0
<< j
);
742 pBankInfo2
->suportedCasLatencies
&= ~(BIT0
<< j
);
748 DB(mvOsPrintf("Dram2: minCasCalc support = %x (after SysCC calc)\n",
749 pBankInfo2
->suportedCasLatencies
));
751 startBit
= 3; /* DDR2 support CL start with CL3 (bit 3) */
752 stopBit
= 6; /* DDR2 support CL stops with CL6 (bit 6) */
754 for(j
= startBit
; j
<= stopBit
; j
++)
756 if((pBankInfo
->suportedCasLatencies
>> j
) & BIT0
)
758 DB(mvOsPrintf("Dram: minCasCalc choose CAS %x \n",(BIT0
<< j
)));
759 minCas0
= (BIT0
<< j
);
764 for(j
= startBit
; j
<= stopBit
; j
++)
766 if((pBankInfo2
->suportedCasLatencies
>> j
) & BIT0
)
768 DB(mvOsPrintf("Dram: minCasCalc choose CAS %x \n",(BIT0
<< j
)));
769 minCas2
= (BIT0
<< j
);
774 if (minCas2
> minCas0
)
782 /*******************************************************************************
783 * sdramConfigRegCalc - Calculate sdram config register
785 * DESCRIPTION: Calculate sdram config register optimized value based
786 * on the bank info parameters.
789 * busClk - the DRAM bus Clock.
790 * pBankInfo - sdram bank parameters
796 * sdram config reg value.
798 *******************************************************************************/
799 static MV_U32
sdramConfigRegCalc(MV_DRAM_BANK_INFO
*pBankInfo
,MV_DRAM_BANK_INFO
*pBankInfo2
, MV_U32 busClk
)
801 MV_U32 sdramConfig
= 0;
802 MV_U32 refreshPeriod
;
804 busClk
/= 1000000; /* we work with busClk in MHz */
806 sdramConfig
= MV_REG_READ(SDRAM_CONFIG_REG
);
808 /* figure out the memory refresh internal */
809 switch (pBankInfo
->refreshInterval
& 0xf)
811 case 0x0: /* refresh period is 15.625 usec */
812 refreshPeriod
= 15625;
814 case 0x1: /* refresh period is 3.9 usec */
815 refreshPeriod
= 3900;
817 case 0x2: /* refresh period is 7.8 usec */
818 refreshPeriod
= 7800;
820 case 0x3: /* refresh period is 31.3 usec */
821 refreshPeriod
= 31300;
823 case 0x4: /* refresh period is 62.5 usec */
824 refreshPeriod
= 62500;
826 case 0x5: /* refresh period is 125 usec */
827 refreshPeriod
= 125000;
829 default: /* refresh period undefined */
830 mvOsPrintf("Dram: ERR. DRAM refresh period is unknown!\n");
834 /* Now the refreshPeriod is in register format value */
835 refreshPeriod
= (busClk
* refreshPeriod
) / 1000;
837 DB(mvOsPrintf("Dram: sdramConfigRegCalc calculated refresh interval %0x\n",
840 /* make sure the refresh value is only 14 bits */
841 if(refreshPeriod
> SDRAM_REFRESH_MAX
)
843 refreshPeriod
= SDRAM_REFRESH_MAX
;
844 DB(mvOsPrintf("Dram: sdramConfigRegCalc adjusted refresh interval %0x\n",
848 /* Clear the refresh field */
849 sdramConfig
&= ~SDRAM_REFRESH_MASK
;
851 /* Set new value to refresh field */
852 sdramConfig
|= (refreshPeriod
& SDRAM_REFRESH_MASK
);
854 /* registered DRAM ? */
855 if ( pBankInfo
->registeredAddrAndControlInputs
)
857 /* it's registered DRAM, so set the reg. DRAM bit */
858 sdramConfig
|= SDRAM_REGISTERED
;
859 DB(mvOsPrintf("DRAM Attribute: Registered address and control inputs.\n");)
862 /* ECC and IERR support */
863 sdramConfig
&= ~SDRAM_ECC_MASK
; /* Clear ECC field */
864 sdramConfig
&= ~SDRAM_IERR_MASK
; /* Clear IErr field */
866 if ( pBankInfo
->errorCheckType
)
868 sdramConfig
|= SDRAM_ECC_EN
;
869 sdramConfig
|= SDRAM_IERR_REPORTE
;
870 DB(mvOsPrintf("Dram: mvDramIfDetect Enabling ECC\n"));
874 sdramConfig
|= SDRAM_ECC_DIS
;
875 sdramConfig
|= SDRAM_IERR_IGNORE
;
876 DB(mvOsPrintf("Dram: mvDramIfDetect Disabling ECC!\n"));
878 /* Set static default settings */
879 sdramConfig
|= SDRAM_CONFIG_DV
;
881 DB(mvOsPrintf("Dram: sdramConfigRegCalc set sdramConfig to 0x%x\n",
887 /*******************************************************************************
888 * sdramModeRegCalc - Calculate sdram mode register
890 * DESCRIPTION: Calculate sdram mode register optimized value based
891 * on the bank info parameters and the minCas.
894 * minCas - minimum CAS supported.
900 * sdram mode reg value.
902 *******************************************************************************/
903 static MV_U32
sdramModeRegCalc(MV_U32 minCas
)
907 sdramMode
= MV_REG_READ(SDRAM_MODE_REG
);
909 /* Clear CAS Latency field */
910 sdramMode
&= ~SDRAM_CL_MASK
;
912 DB(mvOsPrintf("DRAM CAS Latency ");)
917 sdramMode
|= SDRAM_DDR2_CL_3
;
918 DB(mvOsPrintf("3.\n");)
921 sdramMode
|= SDRAM_DDR2_CL_4
;
922 DB(mvOsPrintf("4.\n");)
925 sdramMode
|= SDRAM_DDR2_CL_5
;
926 DB(mvOsPrintf("5.\n");)
929 sdramMode
|= SDRAM_DDR2_CL_6
;
930 DB(mvOsPrintf("6.\n");)
933 mvOsOutput("\nsdramModeRegCalc ERROR: Max. CL out of range\n");
937 DB(mvOsPrintf("\nsdramModeRegCalc register 0x%x\n", sdramMode
));
941 /*******************************************************************************
942 * sdramExtModeRegCalc - Calculate sdram Extended mode register
945 * Return sdram Extended mode register value based
946 * on the bank info parameters and bank presence.
949 * pBankInfo - sdram bank parameters
950 * busClk - DRAM frequency
956 * sdram Extended mode reg value.
958 *******************************************************************************/
959 static MV_U32
sdramExtModeRegCalc(MV_DRAM_BANK_INFO
*pBankInfo
, MV_U32 busClk
)
961 MV_U32 populateBanks
= 0;
964 /* Represent the populate banks in binary form */
965 for(bankNum
= 0; bankNum
< MV_DRAM_MAX_CS
; bankNum
++)
967 if (0 != pBankInfo
[bankNum
].size
)
969 populateBanks
|= (1 << bankNum
);
973 switch(populateBanks
)
975 case(BANK_PRESENT_CS0
):
976 case(BANK_PRESENT_CS0_CS1
):
977 return DDR_SDRAM_EXT_MODE_CS0_CS1_DV
;
979 case(BANK_PRESENT_CS0_CS2
):
980 case(BANK_PRESENT_CS0_CS1_CS2
):
981 case(BANK_PRESENT_CS0_CS2_CS3
):
982 case(BANK_PRESENT_CS0_CS2_CS3_CS4
):
983 if (busClk
>= MV_BOARD_SYSCLK_267MHZ
)
984 return DDR_SDRAM_EXT_MODE_FAST_CS0_CS1_CS2_CS3_DV
;
986 return DDR_SDRAM_EXT_MODE_CS0_CS1_CS2_CS3_DV
;
989 mvOsOutput("sdramExtModeRegCalc: Invalid DRAM bank presence\n");
995 /*******************************************************************************
996 * dunitCtrlLowRegCalc - Calculate sdram dunit control low register
998 * DESCRIPTION: Calculate sdram dunit control low register optimized value based
999 * on the bank info parameters and the minCas.
1002 * pBankInfo - sdram bank parameters
1003 * minCas - minimum CAS supported.
1009 * sdram dunit control low reg value.
1011 *******************************************************************************/
1012 static MV_U32
dunitCtrlLowRegCalc(MV_DRAM_BANK_INFO
*pBankInfo
, MV_U32 minCas
, MV_U32 busClk
, MV_STATUS TTMode
)
1014 MV_U32 dunitCtrlLow
, cl
;
1015 MV_U32 sbOutR
[4]={3,5,7,9} ;
1016 MV_U32 sbOutU
[4]={1,3,5,7} ;
1018 dunitCtrlLow
= MV_REG_READ(SDRAM_DUNIT_CTRL_REG
);
1020 DB(mvOsPrintf("Dram: dunitCtrlLowRegCalc\n"));
1022 /* Clear StBurstOutDel field */
1023 dunitCtrlLow
&= ~SDRAM_SB_OUT_MASK
;
1025 /* Clear StBurstInDel field */
1026 dunitCtrlLow
&= ~SDRAM_SB_IN_MASK
;
1028 /* Clear CtrlPos field */
1029 dunitCtrlLow
&= ~SDRAM_CTRL_POS_MASK
;
1031 /* Clear 2T field */
1032 dunitCtrlLow
&= ~SDRAM_2T_MASK
;
1033 if (TTMode
== MV_TRUE
)
1035 dunitCtrlLow
|= SDRAM_2T_MODE
;
1038 /* For proper sample of read data set the Dunit Control register's */
1039 /* stBurstInDel bits [27:24] */
1040 /* 200MHz - 267MHz None reg = CL + 1 */
1041 /* 200MHz - 267MHz reg = CL + 2 */
1042 /* > 267MHz None reg = CL + 2 */
1043 /* > 267MHz reg = CL + 3 */
1045 /* For proper sample of read data set the Dunit Control register's */
1046 /* stBurstOutDel bits [23:20] */
1047 /********-********-********-********-
1048 * CL=3 | CL=4 | CL=5 | CL=6 |
1049 *********-********-********-********-
1050 Not Reg. * 0001 | 0011 | 0101 | 0111 |
1051 *********-********-********-********-
1052 Registered * 0011 | 0101 | 0111 | 1001 |
1053 *********-********-********-********/
1055 /* Set Dunit Control low default value */
1056 dunitCtrlLow
|= SDRAM_DUNIT_CTRL_LOW_DDR2_DV
;
1060 case DDR2_CL_3
: cl
= 3; break;
1061 case DDR2_CL_4
: cl
= 4; break;
1062 case DDR2_CL_5
: cl
= 5; break;
1063 case DDR2_CL_6
: cl
= 6; break;
1065 mvOsOutput("Dram: dunitCtrlLowRegCalc Max. CL out of range %d\n", minCas
);
1069 /* registerd DDR SDRAM? */
1070 if (pBankInfo
->registeredAddrAndControlInputs
== MV_TRUE
)
1072 dunitCtrlLow
|= (sbOutR
[cl
-3]) << SDRAM_SB_OUT_DEL_OFFS
;
1076 dunitCtrlLow
|= (sbOutU
[cl
-3]) << SDRAM_SB_OUT_DEL_OFFS
;
1079 DB(mvOsPrintf("\n\ndunitCtrlLowRegCalc: CL = %d, frequencies=%d\n", cl
, busClk
));
1081 if (busClk
<= MV_BOARD_SYSCLK_267MHZ
)
1083 if (pBankInfo
->registeredAddrAndControlInputs
== MV_TRUE
)
1090 if (pBankInfo
->registeredAddrAndControlInputs
== MV_TRUE
)
1096 DB(mvOsPrintf("dunitCtrlLowRegCalc: SDRAM_SB_IN_DEL_OFFS = %d \n", cl
));
1097 dunitCtrlLow
|= cl
<< SDRAM_SB_IN_DEL_OFFS
;
1099 DB(mvOsPrintf("Dram: Reg dunit control low = %x\n", dunitCtrlLow
));
1101 return dunitCtrlLow
;
1104 /*******************************************************************************
1105 * dunitCtrlHighRegCalc - Calculate sdram dunit control high register
1107 * DESCRIPTION: Calculate sdram dunit control high register optimized value based
1111 * busClk - DRAM frequency.
1117 * sdram dunit control high reg value.
1119 *******************************************************************************/
1120 static MV_U32
dunitCtrlHighRegCalc(MV_DRAM_BANK_INFO
*pBankInfo
, MV_U32 busClk
)
1122 MV_U32 dunitCtrlHigh
;
1123 dunitCtrlHigh
= MV_REG_READ(SDRAM_DUNIT_CTRL_HI_REG
);
1124 if(busClk
> MV_BOARD_SYSCLK_300MHZ
)
1125 dunitCtrlHigh
|= SDRAM__P2D_EN
;
1127 dunitCtrlHigh
&= ~SDRAM__P2D_EN
;
1129 if(busClk
> MV_BOARD_SYSCLK_267MHZ
)
1130 dunitCtrlHigh
|= (SDRAM__WR_MESH_DELAY_EN
| SDRAM__PUP_ZERO_SKEW_EN
| SDRAM__ADD_HALF_FCC_EN
);
1132 /* If ECC support we turn on D2P sample */
1133 dunitCtrlHigh
&= ~SDRAM__D2P_EN
; /* Clear D2P bit */
1134 if (( pBankInfo
->errorCheckType
) && (busClk
> MV_BOARD_SYSCLK_267MHZ
))
1135 dunitCtrlHigh
|= SDRAM__D2P_EN
;
1137 return dunitCtrlHigh
;
1140 /*******************************************************************************
1141 * sdramAddrCtrlRegCalc - Calculate sdram address control register
1143 * DESCRIPTION: Calculate sdram address control register optimized value based
1144 * on the bank info parameters and the minCas.
1147 * pBankInfo - sdram bank parameters
1153 * sdram address control reg value.
1155 *******************************************************************************/
1156 static MV_U32
sdramAddrCtrlRegCalc(MV_DRAM_BANK_INFO
*pBankInfo
, MV_DRAM_BANK_INFO
*pBankInfoDIMM1
)
1158 MV_U32 addrCtrl
= 0;
1160 if (pBankInfoDIMM1
->size
)
1162 switch (pBankInfoDIMM1
->sdramWidth
)
1164 case 4: /* memory is x4 */
1165 mvOsOutput("sdramAddrCtrlRegCalc: Error - x4 not supported!\n");
1168 case 8: /* memory is x8 */
1169 addrCtrl
|= SDRAM_ADDRSEL_X8(2) | SDRAM_ADDRSEL_X8(3);
1170 DB(mvOsPrintf("sdramAddrCtrlRegCalc: sdramAddrCtrlRegCalc SDRAM device DIMM2 width x8\n"));
1173 addrCtrl
|= SDRAM_ADDRSEL_X16(2) | SDRAM_ADDRSEL_X16(3);
1174 DB(mvOsPrintf("sdramAddrCtrlRegCalc: sdramAddrCtrlRegCalc SDRAM device DIMM2 width x16\n"));
1176 default: /* memory width unsupported */
1177 mvOsOutput("sdramAddrCtrlRegCalc: ERR. DRAM chip width is unknown!\n");
1182 switch (pBankInfo
->sdramWidth
)
1184 case 4: /* memory is x4 */
1185 mvOsOutput("sdramAddrCtrlRegCalc: Error - x4 not supported!\n");
1188 case 8: /* memory is x8 */
1189 addrCtrl
|= SDRAM_ADDRSEL_X8(0) | SDRAM_ADDRSEL_X8(1);
1190 DB(mvOsPrintf("sdramAddrCtrlRegCalc: sdramAddrCtrlRegCalc SDRAM device width x8\n"));
1193 addrCtrl
|= SDRAM_ADDRSEL_X16(0) | SDRAM_ADDRSEL_X16(1);
1194 DB(mvOsPrintf("sdramAddrCtrlRegCalc: sdramAddrCtrlRegCalc SDRAM device width x16\n"));
1196 default: /* memory width unsupported */
1197 mvOsOutput("sdramAddrCtrlRegCalc: ERR. DRAM chip width is unknown!\n");
1201 /* Note that density is in MB units */
1202 switch (pBankInfo
->deviceDensity
)
1204 case 256: /* 256 Mbit */
1205 DB(mvOsPrintf("DRAM Device Density 256Mbit\n"));
1206 addrCtrl
|= SDRAM_DSIZE_256Mb(0) | SDRAM_DSIZE_256Mb(1);
1208 case 512: /* 512 Mbit */
1209 DB(mvOsPrintf("DRAM Device Density 512Mbit\n"));
1210 addrCtrl
|= SDRAM_DSIZE_512Mb(0) | SDRAM_DSIZE_512Mb(1);
1212 case 1024: /* 1 Gbit */
1213 DB(mvOsPrintf("DRAM Device Density 1Gbit\n"));
1214 addrCtrl
|= SDRAM_DSIZE_1Gb(0) | SDRAM_DSIZE_1Gb(1);
1216 case 2048: /* 2 Gbit */
1217 DB(mvOsPrintf("DRAM Device Density 2Gbit\n"));
1218 addrCtrl
|= SDRAM_DSIZE_2Gb(0) | SDRAM_DSIZE_2Gb(1);
1221 mvOsOutput("Dram: sdramAddrCtrl unsupported RAM-Device size %d\n",
1222 pBankInfo
->deviceDensity
);
1226 if (pBankInfoDIMM1
->size
)
1228 switch (pBankInfoDIMM1
->deviceDensity
)
1230 case 256: /* 256 Mbit */
1231 DB(mvOsPrintf("DIMM2: DRAM Device Density 256Mbit\n"));
1232 addrCtrl
|= SDRAM_DSIZE_256Mb(2) | SDRAM_DSIZE_256Mb(3);
1234 case 512: /* 512 Mbit */
1235 DB(mvOsPrintf("DIMM2: DRAM Device Density 512Mbit\n"));
1236 addrCtrl
|= SDRAM_DSIZE_512Mb(2) | SDRAM_DSIZE_512Mb(3);
1238 case 1024: /* 1 Gbit */
1239 DB(mvOsPrintf("DIMM2: DRAM Device Density 1Gbit\n"));
1240 addrCtrl
|= SDRAM_DSIZE_1Gb(2) | SDRAM_DSIZE_1Gb(3);
1242 case 2048: /* 2 Gbit */
1243 DB(mvOsPrintf("DIMM2: DRAM Device Density 2Gbit\n"));
1244 addrCtrl
|= SDRAM_DSIZE_2Gb(2) | SDRAM_DSIZE_2Gb(3);
1247 mvOsOutput("DIMM2: Dram: sdramAddrCtrl unsupported RAM-Device size %d\n",
1248 pBankInfoDIMM1
->deviceDensity
);
1252 /* SDRAM address control */
1253 DB(mvOsPrintf("Dram: setting sdram address control with: %x \n", addrCtrl
));
1258 /*******************************************************************************
1259 * sdramTimeCtrlLowRegCalc - Calculate sdram timing control low register
1262 * This function calculates sdram timing control low register
1263 * optimized value based on the bank info parameters and the minCas.
1266 * pBankInfo - sdram bank parameters
1267 * minCas - minimum CAS supported.
1268 * busClk - Bus clock
1274 * sdram timing control low reg value.
1276 *******************************************************************************/
1277 static MV_U32
sdramTimeCtrlLowRegCalc(MV_DRAM_BANK_INFO
*pBankInfo
, MV_U32 minCas
, MV_U32 busClk
)
1286 MV_U32 timeCtrlLow
= 0;
1290 busClk
= busClk
/ 1000000; /* In MHz */
1292 /* Scan all DRAM banks to find maximum timing values */
1293 for (bankNum
= 0; bankNum
< MV_DRAM_MAX_CS
; bankNum
++)
1295 tRp
= MV_MAX(tRp
, pBankInfo
[bankNum
].minRowPrechargeTime
);
1296 tRrd
= MV_MAX(tRrd
, pBankInfo
[bankNum
].minRowActiveToRowActive
);
1297 tRcd
= MV_MAX(tRcd
, pBankInfo
[bankNum
].minRasToCasDelay
);
1298 tRas
= MV_MAX(tRas
, pBankInfo
[bankNum
].minRasPulseWidth
);
1301 /* Extract timing (in ns) from SPD value. We ignore the tenth ns part. */
1302 /* by shifting the data two bits right. */
1303 tRp
= tRp
>> 2; /* For example 0x50 -> 20ns */
1307 /* Extract clock cycles from time parameter. We need to round up */
1308 tRp
= ((busClk
* tRp
) / 1000) + (((busClk
* tRp
) % 1000) ? 1 : 0);
1309 DB(mvOsPrintf("Dram Timing Low: tRp = %d ", tRp
));
1310 tRrd
= ((busClk
* tRrd
) / 1000) + (((busClk
* tRrd
) % 1000) ? 1 : 0);
1311 /* JEDEC min reqeirments tRrd = 2 */
1314 DB(mvOsPrintf("tRrd = %d ", tRrd
));
1315 tRcd
= ((busClk
* tRcd
) / 1000) + (((busClk
* tRcd
) % 1000) ? 1 : 0);
1316 DB(mvOsPrintf("tRcd = %d ", tRcd
));
1317 tRas
= ((busClk
* tRas
) / 1000) + (((busClk
* tRas
) % 1000) ? 1 : 0);
1318 DB(mvOsPrintf("tRas = %d ", tRas
));
1320 /* tWr and tWtr is different for DDR1 and DDR2. tRtp is only for DDR2 */
1321 /* Scan all DRAM banks to find maximum timing values */
1322 for (bankNum
= 0; bankNum
< MV_DRAM_MAX_CS
; bankNum
++)
1324 tWr
= MV_MAX(tWr
, pBankInfo
[bankNum
].minWriteRecoveryTime
);
1325 tWtr
= MV_MAX(tWtr
, pBankInfo
[bankNum
].minWriteToReadCmdDelay
);
1326 tRtp
= MV_MAX(tRtp
, pBankInfo
[bankNum
].minReadToPrechCmdDelay
);
1329 /* Extract timing (in ns) from SPD value. We ignore the tenth ns */
1330 /* part by shifting the data two bits right. */
1331 tWr
= tWr
>> 2; /* For example 0x50 -> 20ns */
1334 /* Extract clock cycles from time parameter. We need to round up */
1335 tWr
= ((busClk
* tWr
) / 1000) + (((busClk
* tWr
) % 1000) ? 1 : 0);
1336 DB(mvOsPrintf("tWr = %d ", tWr
));
1337 tWtr
= ((busClk
* tWtr
) / 1000) + (((busClk
* tWtr
) % 1000) ? 1 : 0);
1338 /* JEDEC min reqeirments tWtr = 2 */
1341 DB(mvOsPrintf("tWtr = %d ", tWtr
));
1342 tRtp
= ((busClk
* tRtp
) / 1000) + (((busClk
* tRtp
) % 1000) ? 1 : 0);
1343 /* JEDEC min reqeirments tRtp = 2 */
1346 DB(mvOsPrintf("tRtp = %d ", tRtp
));
1348 /* Note: value of 0 in register means one cycle, 1 means two and so on */
1349 timeCtrlLow
= (((tRp
- 1) << SDRAM_TRP_OFFS
) |
1350 ((tRrd
- 1) << SDRAM_TRRD_OFFS
) |
1351 ((tRcd
- 1) << SDRAM_TRCD_OFFS
) |
1352 (((tRas
- 1) << SDRAM_TRAS_OFFS
) & SDRAM_TRAS_MASK
)|
1353 ((tWr
- 1) << SDRAM_TWR_OFFS
) |
1354 ((tWtr
- 1) << SDRAM_TWTR_OFFS
) |
1355 ((tRtp
- 1) << SDRAM_TRTP_OFFS
));
1357 /* Check extended tRas bit */
1358 if ((tRas
- 1) & BIT4
)
1359 timeCtrlLow
|= (1 << SDRAM_EXT_TRAS_OFFS
);
1364 /*******************************************************************************
1365 * sdramTimeCtrlHighRegCalc - Calculate sdram timing control high register
1368 * This function calculates sdram timing control high register
1369 * optimized value based on the bank info parameters and the bus clock.
1372 * pBankInfo - sdram bank parameters
1373 * busClk - Bus clock
1379 * sdram timing control high reg value.
1381 *******************************************************************************/
1382 static MV_U32
sdramTimeCtrlHighRegCalc(MV_DRAM_BANK_INFO
*pBankInfo
, MV_U32 busClk
)
1389 busClk
= busClk
/ 1000000; /* In MHz */
1391 /* Set DDR timing high register static configuration bits */
1392 timingHigh
= MV_REG_READ(SDRAM_TIMING_CTRL_HIGH_REG
);
1394 /* Set DDR timing high register default value */
1395 timingHigh
|= SDRAM_TIMING_CTRL_HIGH_REG_DV
;
1397 /* Clear tRfc field */
1398 timingHigh
&= ~SDRAM_TRFC_MASK
;
1400 /* Scan all DRAM banks to find maximum timing values */
1401 for (bankNum
= 0; bankNum
< MV_DRAM_MAX_CS
; bankNum
++)
1403 timeNs
= MV_MAX(timeNs
, pBankInfo
[bankNum
].minRefreshToActiveCmd
);
1404 DB(mvOsPrintf("Dram: Timing High: minRefreshToActiveCmd = %d\n",
1405 pBankInfo
[bankNum
].minRefreshToActiveCmd
));
1407 if(busClk
>= 333 && mvCtrlModelGet() == MV_78XX0_A1_REV
)
1409 timingHigh
|= 0x1 << SDRAM_TR2W_W2R_OFFS
;
1412 tRfc
= ((busClk
* timeNs
) / 1000) + (((busClk
* timeNs
) % 1000) ? 1 : 0);
1413 /* Note: value of 0 in register means one cycle, 1 means two and so on */
1414 DB(mvOsPrintf("Dram: Timing High: tRfc = %d\n", tRfc
));
1415 timingHigh
|= (((tRfc
- 1) & SDRAM_TRFC_MASK
) << SDRAM_TRFC_OFFS
);
1416 DB(mvOsPrintf("Dram: Timing High: tRfc = %d\n", tRfc
));
1418 /* SDRAM timing high */
1419 DB(mvOsPrintf("Dram: setting timing high with: %x \n", timingHigh
));
1423 /*******************************************************************************
1424 * sdramDDr2OdtConfig - Set DRAM DDR2 On Die Termination registers.
1427 * This function config DDR2 On Die Termination (ODT) registers.
1430 * pBankInfo - bank info parameters.
1437 *******************************************************************************/
1438 static void sdramDDr2OdtConfig(MV_DRAM_BANK_INFO
*pBankInfo
)
1440 MV_U32 populateBanks
= 0;
1441 MV_U32 odtCtrlLow
, odtCtrlHigh
, dunitOdtCtrl
;
1444 /* Represent the populate banks in binary form */
1445 for(bankNum
= 0; bankNum
< MV_DRAM_MAX_CS
; bankNum
++)
1447 if (0 != pBankInfo
[bankNum
].size
)
1449 populateBanks
|= (1 << bankNum
);
1453 switch(populateBanks
)
1455 case(BANK_PRESENT_CS0
):
1456 case(BANK_PRESENT_CS0_CS1
):
1457 odtCtrlLow
= DDR2_ODT_CTRL_LOW_CS0_CS1_DV
;
1458 odtCtrlHigh
= DDR2_ODT_CTRL_HIGH_CS0_CS1_DV
;
1459 dunitOdtCtrl
= DDR2_DUNIT_ODT_CTRL_CS0_CS1_DV
;
1461 case(BANK_PRESENT_CS0_CS2
):
1462 case(BANK_PRESENT_CS0_CS1_CS2
):
1463 case(BANK_PRESENT_CS0_CS2_CS3
):
1464 case(BANK_PRESENT_CS0_CS2_CS3_CS4
):
1465 odtCtrlLow
= DDR2_ODT_CTRL_LOW_CS0_CS1_CS2_CS3_DV
;
1466 odtCtrlHigh
= DDR2_ODT_CTRL_HIGH_CS0_CS1_CS2_CS3_DV
;
1467 dunitOdtCtrl
= DDR2_DUNIT_ODT_CTRL_CS0_CS1_CS2_CS3_DV
;
1470 DB(mvOsPrintf("sdramDDr2OdtConfig: Invalid DRAM bank presence\n"));
1473 /* DDR2 SDRAM ODT ctrl low */
1474 DB(mvOsPrintf("Dram: DDR2 setting ODT ctrl low with: %x \n", odtCtrlLow
));
1475 MV_REG_WRITE(DRAM_BUF_REG7
, odtCtrlLow
);
1477 /* DDR2 SDRAM ODT ctrl high */
1478 DB(mvOsPrintf("Dram: DDR2 setting ODT ctrl high with: %x \n", odtCtrlHigh
));
1479 MV_REG_WRITE(DRAM_BUF_REG8
, odtCtrlHigh
);
1481 /* DDR2 DUNIT ODT ctrl */
1482 if ( ((mvCtrlModelGet() == MV_78XX0_DEV_ID
) && (mvCtrlRevGet() == MV_78XX0_Y0_REV
)) ||
1483 (mvCtrlModelGet() == MV_76100_DEV_ID
) ||
1484 (mvCtrlModelGet() == MV_78100_DEV_ID
) ||
1485 (mvCtrlModelGet() == MV_78200_DEV_ID
) )
1486 dunitOdtCtrl
&= ~(BIT9
|BIT8
); /* Clear ODT always on */
1488 DB(mvOsPrintf("DUNIT: DDR2 setting ODT ctrl with: %x \n", dunitOdtCtrl
));
1489 MV_REG_WRITE(DRAM_BUF_REG9
, dunitOdtCtrl
);
1492 /*******************************************************************************
1493 * sdramDdr2TimeLoRegCalc - Set DDR2 DRAM Timing Low registers.
1496 * This function config DDR2 DRAM Timing low registers.
1499 * minCas - minimum CAS supported.
1505 * DDR2 sdram timing low reg value.
1506 *******************************************************************************/
1507 static MV_U32
sdramDdr2TimeLoRegCalc(MV_U32 minCas
)
1510 MV_U32 ddr2TimeLoReg
;
1512 /* read and clear the feilds we are going to set */
1513 ddr2TimeLoReg
= MV_REG_READ(SDRAM_DDR2_TIMING_LO_REG
);
1514 ddr2TimeLoReg
&= ~(SD2TLR_TODT_ON_RD_MASK
|
1515 SD2TLR_TODT_OFF_RD_MASK
|
1516 SD2TLR_TODT_ON_CTRL_RD_MASK
|
1517 SD2TLR_TODT_OFF_CTRL_RD_MASK
);
1519 if( minCas
== DDR2_CL_3
)
1523 else if( minCas
== DDR2_CL_4
)
1527 else if( minCas
== DDR2_CL_5
)
1531 else if( minCas
== DDR2_CL_6
)
1537 DB(mvOsPrintf("sdramDdr2TimeLoRegCalc: CAS latency %d unsupported. using CAS latency 4\n",
1542 ddr2TimeLoReg
|= ((cl
-3) << SD2TLR_TODT_ON_RD_OFFS
);
1543 ddr2TimeLoReg
|= ( cl
<< SD2TLR_TODT_OFF_RD_OFFS
);
1544 ddr2TimeLoReg
|= ( cl
<< SD2TLR_TODT_ON_CTRL_RD_OFFS
);
1545 ddr2TimeLoReg
|= ((cl
+3) << SD2TLR_TODT_OFF_CTRL_RD_OFFS
);
1547 /* DDR2 SDRAM timing low */
1548 DB(mvOsPrintf("Dram: DDR2 setting timing low with: %x \n", ddr2TimeLoReg
));
1550 return ddr2TimeLoReg
;
1553 /*******************************************************************************
1554 * sdramDdr2TimeHiRegCalc - Set DDR2 DRAM Timing High registers.
1557 * This function config DDR2 DRAM Timing high registers.
1560 * minCas - minimum CAS supported.
1566 * DDR2 sdram timing high reg value.
1567 *******************************************************************************/
1568 static MV_U32
sdramDdr2TimeHiRegCalc(MV_U32 minCas
)
1571 MV_U32 ddr2TimeHiReg
;
1573 /* read and clear the feilds we are going to set */
1574 ddr2TimeHiReg
= MV_REG_READ(SDRAM_DDR2_TIMING_HI_REG
);
1575 ddr2TimeHiReg
&= ~(SD2THR_TODT_ON_WR_MASK
|
1576 SD2THR_TODT_OFF_WR_MASK
|
1577 SD2THR_TODT_ON_CTRL_WR_MASK
|
1578 SD2THR_TODT_OFF_CTRL_WR_MASK
);
1580 if( minCas
== DDR2_CL_3
)
1584 else if( minCas
== DDR2_CL_4
)
1588 else if( minCas
== DDR2_CL_5
)
1592 else if( minCas
== DDR2_CL_6
)
1598 mvOsOutput("sdramDdr2TimeHiRegCalc: CAS latency %d unsupported. using CAS latency 4\n",
1603 ddr2TimeHiReg
|= ((cl
-3) << SD2THR_TODT_ON_WR_OFFS
);
1604 ddr2TimeHiReg
|= ( cl
<< SD2THR_TODT_OFF_WR_OFFS
);
1605 ddr2TimeHiReg
|= ( cl
<< SD2THR_TODT_ON_CTRL_WR_OFFS
);
1606 ddr2TimeHiReg
|= ((cl
+3) << SD2THR_TODT_OFF_CTRL_WR_OFFS
);
1608 /* DDR2 SDRAM timin high */
1609 DB(mvOsPrintf("Dram: DDR2 setting timing high with: %x \n", ddr2TimeHiReg
));
1611 return ddr2TimeHiReg
;
1615 /*******************************************************************************
1616 * mvDramIfCalGet - Get CAS Latency
1619 * This function get the CAS Latency.
1628 * CAS latency times 10 (to avoid using floating point).
1630 *******************************************************************************/
1631 MV_U32
mvDramIfCalGet(void)
1633 MV_U32 sdramCasLat
, casLatMask
;
1635 casLatMask
= (MV_REG_READ(SDRAM_MODE_REG
) & SDRAM_CL_MASK
);
1639 case SDRAM_DDR2_CL_3
:
1642 case SDRAM_DDR2_CL_4
:
1645 case SDRAM_DDR2_CL_5
:
1648 case SDRAM_DDR2_CL_6
:
1652 mvOsOutput("mvDramIfCalGet: Err, unknown DDR2 CAL\n");
1660 /*******************************************************************************
1661 * mvDramIfSelfRefreshSet - Put the dram in self refresh mode -
1664 * add support in power management.
1676 *******************************************************************************/
1678 MV_VOID
mvDramIfSelfRefreshSet()
1682 operReg
= MV_REG_READ(SDRAM_OPERATION_REG
);
1683 MV_REG_WRITE(SDRAM_OPERATION_REG
,operReg
|SDRAM_CMD_SLF_RFRSH
);
1684 /* Read until register is reset to 0 */
1685 while(MV_REG_READ(SDRAM_OPERATION_REG
));
1687 /*******************************************************************************
1688 * mvDramIfDimGetSPDversion - return DIMM SPD version.
1691 * This function prints the DRAM controller information.
1702 *******************************************************************************/
1703 static void mvDramIfDimGetSPDversion(MV_U32
*pMajor
, MV_U32
*pMinor
, MV_U32 bankNum
)
1705 MV_DIMM_INFO dimmInfo
;
1706 if (bankNum
>= MV_DRAM_MAX_CS
)
1708 DB(mvOsPrintf("Dram: mvDramIfDimGetSPDversion bad params \n"));
1711 memset(&dimmInfo
,0,sizeof(dimmInfo
));
1712 if ( MV_OK
!= dimmSpdGet((MV_U32
)(bankNum
/2), &dimmInfo
))
1714 DB(mvOsPrintf("Dram: ERR dimmSpdGet failed to get dimm info \n"));
1717 *pMajor
= dimmInfo
.spdRawData
[DIMM_SPD_VERSION
]/10;
1718 *pMinor
= dimmInfo
.spdRawData
[DIMM_SPD_VERSION
]%10;
1720 /*******************************************************************************
1721 * mvDramIfShow - Show DRAM controller information.
1724 * This function prints the DRAM controller information.
1735 *******************************************************************************/
1736 void mvDramIfShow(void)
1738 int i
, sdramCasLat
, sdramCsSize
;
1739 MV_U32 Major
=0, Minor
=0;
1741 mvOsOutput("DRAM Controller info:\n");
1743 mvOsOutput("Total DRAM ");
1744 mvSizePrint(mvDramIfSizeGet());
1747 for(i
= 0; i
< MV_DRAM_MAX_CS
; i
++)
1749 sdramCsSize
= mvDramIfBankSizeGet(i
);
1754 mvDramIfDimGetSPDversion(&Major
, &Minor
,i
);
1755 mvOsOutput("DIMM %d version %d.%d\n", i
/2, Major
, Minor
);
1757 mvOsOutput("\tDRAM CS[%d] ", i
);
1758 mvSizePrint(sdramCsSize
);
1762 sdramCasLat
= mvDramIfCalGet();
1764 if (MV_REG_READ(SDRAM_CONFIG_REG
) & SDRAM_ECC_EN
)
1766 mvOsOutput("ECC enabled, ");
1770 mvOsOutput("ECC Disabled, ");
1773 if (MV_REG_READ(SDRAM_CONFIG_REG
) & SDRAM_REGISTERED
)
1775 mvOsOutput("Registered DIMM\n");
1779 mvOsOutput("Non registered DIMM\n");
1782 mvOsOutput("Configured CAS Latency %d.%d\n", sdramCasLat
/10, sdramCasLat
%10);
1784 /*******************************************************************************
1785 * mvDramIfGetFirstCS - find the DRAM bank on the lower address
1789 * This function return the fisrt CS on address 0
1798 * SDRAM_CS0 or SDRAM_CS2
1800 *******************************************************************************/
1801 MV_U32
mvDramIfGetFirstCS(void)
1803 MV_DRAM_BANK_INFO bankInfo
[MV_DRAM_MAX_CS
];
1805 if (DRAM_CS_Order
[0] == N_A
)
1807 mvDramBankInfoGet(SDRAM_CS0
, &bankInfo
[SDRAM_CS0
]);
1808 #ifdef MV_INCLUDE_SDRAM_CS2
1809 mvDramBankInfoGet(SDRAM_CS2
, &bankInfo
[SDRAM_CS2
]);
1812 #ifdef MV_INCLUDE_SDRAM_CS2
1813 if (bankInfo
[SDRAM_CS0
].size
< bankInfo
[SDRAM_CS2
].size
)
1815 DRAM_CS_Order
[0] = SDRAM_CS2
;
1816 DRAM_CS_Order
[1] = SDRAM_CS3
;
1817 DRAM_CS_Order
[2] = SDRAM_CS0
;
1818 DRAM_CS_Order
[3] = SDRAM_CS1
;
1823 DRAM_CS_Order
[0] = SDRAM_CS0
;
1824 DRAM_CS_Order
[1] = SDRAM_CS1
;
1825 #ifdef MV_INCLUDE_SDRAM_CS2
1826 DRAM_CS_Order
[2] = SDRAM_CS2
;
1827 DRAM_CS_Order
[3] = SDRAM_CS3
;
1831 return DRAM_CS_Order
[0];
1833 /*******************************************************************************
1834 * mvDramIfGetCSorder -
1838 * This function return the fisrt CS on address 0
1847 * SDRAM_CS0 or SDRAM_CS2
1849 * NOTE: mvDramIfGetFirstCS must be caled before this subroutine
1850 *******************************************************************************/
1851 MV_U32
mvDramIfGetCSorder(MV_U32 csOrder
)
1853 return DRAM_CS_Order
[csOrder
];