]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_SAMV71_Xplained/libchip_samv7/source/sdramc.c
Update version number ready for V8.2.1 release.
[freertos] / FreeRTOS / Demo / CORTEX_M7_SAMV71_Xplained / libchip_samv7 / source / sdramc.c
1 /* ----------------------------------------------------------------------------\r
2  *         ATMEL Microcontroller Software Support\r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 2010, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\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
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\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
16  *\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
28  */\r
29 \r
30 /**\r
31  * \file\r
32  *\r
33  * Implementation of memories configuration on board.\r
34  *\r
35  */\r
36 /*----------------------------------------------------------------------------\r
37 *        Headers\r
38 *----------------------------------------------------------------------------*/\r
39 #include "chip.h"\r
40 \r
41 /*----------------------------------------------------------------------------\r
42 *        Local functions\r
43 *----------------------------------------------------------------------------*/\r
44 /**\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
48  */\r
49 static uint32_t SDRAMC_compute_CR_value( SSdramc_Memory* pMemory )\r
50 {\r
51     uint32_t dw=0 ;\r
52 \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
64 \r
65     return dw ;\r
66 }\r
67 \r
68 /*----------------------------------------------------------------------------\r
69 *        Exported functions\r
70 *----------------------------------------------------------------------------*/\r
71 /**\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
75  */\r
76 extern void SDRAMC_Configure( SSdramc_Memory* pMemory, uint32_t dwClockFrequency )\r
77 {\r
78     volatile uint32_t dw ;\r
79 \r
80     /* SDRAM hardware init */\r
81     /* Enable peripheral clock */\r
82     PMC_EnablePeripheral( ID_SMC ) ;\r
83 \r
84     /* SDRAM device configure */\r
85     /* Step 1. */\r
86     /* Program the features of SDRAM device into the Configuration Register.*/\r
87     SDRAMC->SDRAMC_CR = SDRAMC_compute_CR_value( pMemory ) ;\r
88 \r
89     /* Step 2. */\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
94 \r
95     /* Step 3. */\r
96     /* Program the memory device type into the Memory Device Register */\r
97     SDRAMC->SDRAMC_MDR = SDRAMC_MDR_MD_SDRAM;\r
98 \r
99     /* Step 4 */\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
103     {\r
104         ;\r
105     }\r
106 \r
107     /* Step 5. */\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
114 \r
115     /* Step 6. */\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
122 \r
123     /* add some delays after precharge */\r
124     for ( dw = 0; dw < ((dwClockFrequency/1000000)*200/6) ; dw++ )\r
125     {\r
126         ;\r
127     }\r
128 \r
129     /* Step 7. */\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
133     be performed. */\r
134     SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;\r
135     *(uint16_t *)(EBI_SDRAMC_ADDR + 0 ) = 0x1;\r
136 \r
137     SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;\r
138     *(uint16_t *)(EBI_SDRAMC_ADDR + 0) = 0x2;\r
139 \r
140     SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;\r
141     *(uint16_t *)(EBI_SDRAMC_ADDR + 0 ) = 0x3;\r
142 \r
143     SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;\r
144     *(uint16_t *)(EBI_SDRAMC_ADDR + 0) = 0x4;\r
145 \r
146     SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;\r
147     *(uint16_t *)(EBI_SDRAMC_ADDR + 0 ) = 0x5;\r
148 \r
149     SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;\r
150     *(uint16_t *)(EBI_SDRAMC_ADDR + 0) = 0x6;\r
151 \r
152     SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;\r
153     *(uint16_t *)(EBI_SDRAMC_ADDR + 0 ) = 0x7;\r
154 \r
155     SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;\r
156     *(uint16_t *)(EBI_SDRAMC_ADDR + 0) = 0x8;\r
157 \r
158     /* Step 8. */\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
163 \r
164     /* Step 9. */\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
168     to 0 */\r
169     SDRAMC->SDRAMC_MR = SDRAMC_MR_MODE_EXT_LOAD_MODEREG;\r
170     *((uint16_t *)(EBI_SDRAMC_ADDR + (1 << pMemory->cfg.dwBK1))) = 0;\r
171 \r
172     /* Step 10. */\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
178 \r
179     /* Step 11. */\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
183     dw*=15625u ;\r
184     dw/=1000000u ;\r
185     SDRAMC->SDRAMC_TR = SDRAMC_TR_COUNT( dw ) ;\r
186 }\r
187 \r