1 /* ----------------------------------------------------------------------------
\r
2 * ATMEL Microcontroller Software Support
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2010, Atmel Corporation
\r
6 * All rights reserved.
\r
8 * Redistribution and use in source and binary forms, with or without
\r
9 * modification, are permitted provided that the following conditions are met:
\r
11 * - Redistributions of source code must retain the above copyright notice,
\r
12 * this list of conditions and the disclaimer below.
\r
14 * Atmel's name may not be used to endorse or promote products derived from
\r
15 * this software without specific prior written permission.
\r
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
\r
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
\r
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
27 * ----------------------------------------------------------------------------
\r
33 * Implementation of memories configuration on board.
\r
36 /*----------------------------------------------------------------------------
\r
38 *----------------------------------------------------------------------------*/
\r
41 /*----------------------------------------------------------------------------
\r
43 *----------------------------------------------------------------------------*/
\r
45 * \brief Calculate the sdram controller config register value.
\r
46 * \param pMemory Pointer to the sdram structure.
\r
47 * \return Configure register value.
\r
49 static uint32_t SDRAMC_compute_CR_value( SSdramc_Memory* pMemory )
\r
53 dw |= pMemory->cfg.dwColumnBits ;
\r
54 dw |= pMemory->cfg.dwRowBits ;
\r
55 dw |= pMemory->cfg.dwBanks ; //NB, number of banks
\r
56 dw |= pMemory->cfg.dwCAS ; //CAS, CAS latency
\r
57 dw |= pMemory->cfg.dwDataBusWidth ; //DBW, data bus width
\r
58 dw |= SDRAMC_CR_TWR( pMemory->cfg.dwWriteRecoveryDelay ) ; //TWR, Write Recovery Delay
\r
59 dw |= SDRAMC_CR_TRC_TRFC( pMemory->cfg.dwRowCycleDelay_RowRefreshCycle ) ; //TRC_TRFC,Row Cycle Delay and Row Refresh Cycle
\r
60 dw |= SDRAMC_CR_TRP( pMemory->cfg.dwRowPrechargeDelay ) ; //TRP, Row Precharge Delay
\r
61 dw |= SDRAMC_CR_TRCD( pMemory->cfg.dwRowColumnDelay ) ; //TRCD, Row to Column Delay
\r
62 dw |= SDRAMC_CR_TRAS( pMemory->cfg.dwActivePrechargeDelay ) ; //TRAS, Active to Precharge Delay
\r
63 dw |= SDRAMC_CR_TXSR( pMemory->cfg.dwExitSelfRefreshActiveDelay ) ; //TXSR, Exit Self Refresh to Active Delay
\r
68 /*----------------------------------------------------------------------------
\r
69 * Exported functions
\r
70 *----------------------------------------------------------------------------*/
\r
72 * \brief Configure and initialize the SDRAM controller.
\r
73 * \param pMemory Pointer to the sdram structure.
\r
74 * \param dwClockFrequency SDRAM clock frequency.
\r
76 extern void SDRAMC_Configure( SSdramc_Memory* pMemory, uint32_t dwClockFrequency )
\r
78 volatile uint32_t dw ;
\r
80 /* SDRAM hardware init */
\r
81 /* Enable peripheral clock */
\r
82 PMC_EnablePeripheral( ID_SMC ) ;
\r
84 /* SDRAM device configure */
\r
86 /* Program the features of SDRAM device into the Configuration Register.*/
\r
87 SDRAMC->SDRAMC_CR = SDRAMC_compute_CR_value( pMemory ) ;
\r
90 /* For low-power SDRAM, temperature-compensated self refresh (TCSR),
\r
91 drive strength (DS) and partial array self refresh (PASR) must be set
\r
92 in the Low-power Register.*/
\r
93 SDRAMC->SDRAMC_LPR = 0;
\r
96 /* Program the memory device type into the Memory Device Register */
\r
97 SDRAMC->SDRAMC_MDR = SDRAMC_MDR_MD_SDRAM;
\r
100 /* A minimum pause of 200 ¦Ìs is provided to precede any signal toggle.
\r
101 (6 core cycles per iteration) */
\r
102 for ( dw = 0; dw < ((dwClockFrequency/1000000)*200/6) ; dw++ )
\r
108 /* A NOP command is issued to the SDR-SDRAM. Program NOP command into
\r
109 Mode Register, the application must set Mode to 1 in the Mode Register.
\r
110 Perform a write access to any SDR-SDRAM address to acknowledge this command.
\r
111 Now the clock which drives SDR-SDRAM device is enabled.*/
\r
112 SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_NOP;
\r
113 *(uint16_t *)(EBI_SDRAMC_ADDR) = 0;
\r
116 /* An all banks precharge command is issued to the SDR-SDRAM. Program all
\r
117 banks precharge command into Mode Register, the application must set Mode to
\r
118 2 in the Mode Register . Perform a write access to any SDRSDRAM address to
\r
119 acknowledge this command. */
\r
120 SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_ALLBANKS_PRECHARGE;
\r
121 *(uint16_t *)(EBI_SDRAMC_ADDR) = 0x0;
\r
123 /* add some delays after precharge */
\r
124 for ( dw = 0; dw < ((dwClockFrequency/1000000)*200/6) ; dw++ )
\r
130 /* Eight auto-refresh (CBR) cycles are provided. Program the auto refresh
\r
131 command (CBR) into Mode Register, the application must set Mode to 4 in
\r
132 the Mode Register. Once in the idle state, eight AUTO REFRESH cycles must
\r
134 SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;
\r
135 *(uint16_t *)(EBI_SDRAMC_ADDR + 0 ) = 0x1;
\r
137 SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;
\r
138 *(uint16_t *)(EBI_SDRAMC_ADDR + 0) = 0x2;
\r
140 SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;
\r
141 *(uint16_t *)(EBI_SDRAMC_ADDR + 0 ) = 0x3;
\r
143 SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;
\r
144 *(uint16_t *)(EBI_SDRAMC_ADDR + 0) = 0x4;
\r
146 SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;
\r
147 *(uint16_t *)(EBI_SDRAMC_ADDR + 0 ) = 0x5;
\r
149 SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;
\r
150 *(uint16_t *)(EBI_SDRAMC_ADDR + 0) = 0x6;
\r
152 SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;
\r
153 *(uint16_t *)(EBI_SDRAMC_ADDR + 0 ) = 0x7;
\r
155 SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;
\r
156 *(uint16_t *)(EBI_SDRAMC_ADDR + 0) = 0x8;
\r
159 /* A Mode Register set (MRS) cycle is issued to program the parameters of
\r
160 the SDRAM devices, in particular CAS latency and burst length. */
\r
161 SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_LOAD_MODEREG;
\r
162 *(uint16_t *)(EBI_SDRAMC_ADDR + 0x22) = 0xcafe;
\r
165 /* For low-power SDR-SDRAM initialization, an Extended Mode Register set
\r
166 (EMRS) cycle is issued to program the SDR-SDRAM parameters (TCSR, PASR, DS).
\r
167 The write address must be chosen so that BA[1] is set to 1 and BA[0] is set
\r
169 SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_EXT_LOAD_MODEREG;
\r
170 *((uint16_t *)(EBI_SDRAMC_ADDR + (1 << pMemory->cfg.dwBK1))) = 0;
\r
173 /* The application must go into Normal Mode, setting Mode to 0 in the Mode
\r
174 Register and perform a write access at any location in the SDRAM to
\r
175 acknowledge this command. */
\r
176 SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_NORMAL;
\r
177 *(uint16_t *)(EBI_SDRAMC_ADDR ) = 0x0;
\r
180 /* Write the refresh rate into the count field in the SDRAMC Refresh
\r
181 Timer register. Set Refresh timer 15.625 us*/
\r
182 dw=dwClockFrequency/1000u ;
\r
185 SDRAMC->SDRAMC_TR = SDRAMC_TR_COUNT( dw ) ;
\r