blob: 9324c16ffdb8cb7139406e8b335d8c9e96c76e03 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2015 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23/*
24 * COPYRIGHT(c) 2015 STMicroelectronics
25 *
26 * Redistribution and use in source and binary forms, with or without modification,
27 * are permitted provided that the following conditions are met:
28 * 1. Redistributions of source code must retain the above copyright notice,
29 * this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright notice,
31 * this list of conditions and the following disclaimer in the documentation
32 * and/or other materials provided with the distribution.
33 * 3. Neither the name of STMicroelectronics nor the names of its contributors
34 * may be used to endorse or promote products derived from this software
35 * without specific prior written permission.
36 *
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
43 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
44 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 *
48 ******************************************************************************
49 */
50
51#include <err.h>
52#include <debug.h>
53#include <trace.h>
54#include <target.h>
55#include <compiler.h>
56#include <dev/gpio.h>
57#include <platform/stm32.h>
58#include <platform/sdram.h>
59
60/*
61 * sdram initialization sequence, taken from
62 * STM32Cube_FW_F7_V1.1.0/Drivers/BSP
63 */
64
65/**
66 * @brief SDRAM status structure definition
67 */
68#define SDRAM_OK ((uint8_t)0x00)
69#define SDRAM_ERROR ((uint8_t)0x01)
70
71/* SDRAM refresh counter (100Mhz SD clock) */
72#define REFRESH_COUNT ((uint32_t)0x0603)
73
74#define SDRAM_TIMEOUT ((uint32_t)0xFFFF)
75
76/* DMA definitions for SDRAM DMA transfer */
77#define __DMAx_CLK_ENABLE __HAL_RCC_DMA2_CLK_ENABLE
78#define __DMAx_CLK_DISABLE __HAL_RCC_DMA2_CLK_DISABLE
79#define SDRAM_DMAx_CHANNEL DMA_CHANNEL_0
80#define SDRAM_DMAx_STREAM DMA2_Stream0
81#define SDRAM_DMAx_IRQn DMA2_Stream0_IRQn
82#define SDRAM_DMAx_IRQHandler DMA2_Stream0_IRQHandler
83
84/**
85 * @brief FMC SDRAM Mode definition register defines
86 */
87#define SDRAM_MODEREG_BURST_LENGTH_1 ((uint16_t)0x0000)
88#define SDRAM_MODEREG_BURST_LENGTH_2 ((uint16_t)0x0001)
89#define SDRAM_MODEREG_BURST_LENGTH_4 ((uint16_t)0x0002)
90#define SDRAM_MODEREG_BURST_LENGTH_8 ((uint16_t)0x0004)
91#define SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL ((uint16_t)0x0000)
92#define SDRAM_MODEREG_BURST_TYPE_INTERLEAVED ((uint16_t)0x0008)
93#define SDRAM_MODEREG_CAS_LATENCY_1 ((uint16_t)0x0010)
94#define SDRAM_MODEREG_CAS_LATENCY_2 ((uint16_t)0x0020)
95#define SDRAM_MODEREG_CAS_LATENCY_3 ((uint16_t)0x0030)
96#define SDRAM_MODEREG_OPERATING_MODE_STANDARD ((uint16_t)0x0000)
97#define SDRAM_MODEREG_WRITEBURST_MODE_PROGRAMMED ((uint16_t)0x0000)
98#define SDRAM_MODEREG_WRITEBURST_MODE_SINGLE ((uint16_t)0x0200)
99
100static SDRAM_HandleTypeDef sdramHandle;
101
102/**
103 * @brief Programs the SDRAM device.
104 * @param RefreshCount: SDRAM refresh counter value
105 * @retval None
106 */
107static void BSP_SDRAM_Initialization_sequence(uint32_t RefreshCount,
108 uint32_t CasLatency)
109{
110 __IO uint32_t tmpmrd = 0;
111 FMC_SDRAM_CommandTypeDef Command;
112
113 /* Step 1: Configure a clock configuration enable command */
114 Command.CommandMode = FMC_SDRAM_CMD_CLK_ENABLE;
115 Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
116 Command.AutoRefreshNumber = 1;
117 Command.ModeRegisterDefinition = 0;
118
119 /* Send the command */
120 HAL_SDRAM_SendCommand(&sdramHandle, &Command, SDRAM_TIMEOUT);
121
122 /* Step 2: Insert 100 us minimum delay */
123 spin(1000);
124
125 /* Step 3: Configure a PALL (precharge all) command */
126 Command.CommandMode = FMC_SDRAM_CMD_PALL;
127 Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
128 Command.AutoRefreshNumber = 1;
129 Command.ModeRegisterDefinition = 0;
130
131 /* Send the command */
132 HAL_SDRAM_SendCommand(&sdramHandle, &Command, SDRAM_TIMEOUT);
133
134 /* Step 4: Configure an Auto Refresh command */
135 Command.CommandMode = FMC_SDRAM_CMD_AUTOREFRESH_MODE;
136 Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
137 Command.AutoRefreshNumber = 8;
138 Command.ModeRegisterDefinition = 0;
139
140 /* Send the command */
141 HAL_SDRAM_SendCommand(&sdramHandle, &Command, SDRAM_TIMEOUT);
142
143 /* Step 5: Program the external memory mode register */
144 tmpmrd = (uint32_t)SDRAM_MODEREG_BURST_LENGTH_1 |\
145 SDRAM_MODEREG_BURST_TYPE_SEQUENTIAL |\
146 SDRAM_MODEREG_OPERATING_MODE_STANDARD |\
147 SDRAM_MODEREG_WRITEBURST_MODE_SINGLE;
148
149 tmpmrd |= CasLatency;
150
151 Command.CommandMode = FMC_SDRAM_CMD_LOAD_MODE;
152 Command.CommandTarget = FMC_SDRAM_CMD_TARGET_BANK1;
153 Command.AutoRefreshNumber = 1;
154 Command.ModeRegisterDefinition = tmpmrd;
155
156 /* Send the command */
157 HAL_SDRAM_SendCommand(&sdramHandle, &Command, SDRAM_TIMEOUT);
158
159 /* Step 6: Set the refresh rate counter */
160 /* Set the device refresh rate */
161 HAL_SDRAM_ProgramRefreshRate(&sdramHandle, RefreshCount);
162}
163
164static uint32_t GetMemoryWidth(sdram_config_t* config)
165{
166 switch (config->bus_width) {
167 case SDRAM_BUS_WIDTH_8 : return FMC_SDRAM_MEM_BUS_WIDTH_8;
168 case SDRAM_BUS_WIDTH_16 : return FMC_SDRAM_MEM_BUS_WIDTH_16;
169 case SDRAM_BUS_WIDTH_32 : return FMC_SDRAM_MEM_BUS_WIDTH_32;
170 }
171 return 0;
172}
173
174static uint32_t GetColumnBitsNumber(sdram_config_t* config)
175{
176 switch (config->col_bits_num) {
177 case SDRAM_COLUMN_BITS_8 : return FMC_SDRAM_COLUMN_BITS_NUM_8;
178 case SDRAM_COLUMN_BITS_9 : return FMC_SDRAM_COLUMN_BITS_NUM_9;
179 case SDRAM_COLUMN_BITS_10 : return FMC_SDRAM_COLUMN_BITS_NUM_10;
180 case SDRAM_COLUMN_BITS_11 : return FMC_SDRAM_COLUMN_BITS_NUM_11;
181 }
182 return 0;
183}
184
185static uint32_t GetCasLatencyFMC(sdram_config_t* config)
186{
187 switch (config->cas_latency) {
188 case SDRAM_CAS_LATENCY_1 : return FMC_SDRAM_CAS_LATENCY_1;
189 case SDRAM_CAS_LATENCY_2 : return FMC_SDRAM_CAS_LATENCY_2;
190 case SDRAM_CAS_LATENCY_3 : return FMC_SDRAM_CAS_LATENCY_3;
191 }
192 return 0;
193}
194
195static uint32_t GetCasLatencyModeReg(sdram_config_t* config)
196{
197 switch (config->cas_latency) {
198 case SDRAM_CAS_LATENCY_1 : return SDRAM_MODEREG_CAS_LATENCY_1;
199 case SDRAM_CAS_LATENCY_2 : return SDRAM_MODEREG_CAS_LATENCY_2;
200 case SDRAM_CAS_LATENCY_3 : return SDRAM_MODEREG_CAS_LATENCY_3;
201 }
202 return 0;
203}
204
205/**
206 * @brief Initializes the SDRAM device.
207 * @retval SDRAM status
208 */
209uint8_t stm32_sdram_init(sdram_config_t* config)
210{
211 static uint8_t sdramstatus = SDRAM_ERROR;
212 static DMA_HandleTypeDef dma_handle;
213
214 /* SDRAM device configuration */
215 sdramHandle.Instance = FMC_SDRAM_DEVICE;
216
217 /* Timing configuration for 100Mhz as SDRAM clock frequency (System clock is up to 200Mhz) */
218 FMC_SDRAM_TimingTypeDef Timing;
219 Timing.LoadToActiveDelay = 2;
220 Timing.ExitSelfRefreshDelay = 7;
221 Timing.SelfRefreshTime = 4;
222 Timing.RowCycleDelay = 7;
223 Timing.WriteRecoveryTime = 2;
224 Timing.RPDelay = 2;
225 Timing.RCDDelay = 2;
226
227 sdramHandle.Init.SDBank = FMC_SDRAM_BANK1;
228 sdramHandle.Init.ColumnBitsNumber = GetColumnBitsNumber(config);
229 sdramHandle.Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
230 sdramHandle.Init.MemoryDataWidth = GetMemoryWidth(config);
231 sdramHandle.Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
232 sdramHandle.Init.CASLatency = GetCasLatencyFMC(config);
233 sdramHandle.Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
234 sdramHandle.Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
235 sdramHandle.Init.ReadBurst = FMC_SDRAM_RBURST_ENABLE;
236 sdramHandle.Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_0;
237
238 /* Enable FMC clock */
239 __HAL_RCC_FMC_CLK_ENABLE();
240
241 /* Enable chosen DMAx clock */
242 __DMAx_CLK_ENABLE();
243
244 /* SDRAM GPIO initialization */
245 stm_sdram_GPIO_init();
246
247 /* Configure common DMA parameters */
248 dma_handle.Init.Channel = SDRAM_DMAx_CHANNEL;
249 dma_handle.Init.Direction = DMA_MEMORY_TO_MEMORY;
250 dma_handle.Init.PeriphInc = DMA_PINC_ENABLE;
251 dma_handle.Init.MemInc = DMA_MINC_ENABLE;
252 dma_handle.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
253 dma_handle.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
254 dma_handle.Init.Mode = DMA_NORMAL;
255 dma_handle.Init.Priority = DMA_PRIORITY_HIGH;
256 dma_handle.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
257 dma_handle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
258 dma_handle.Init.MemBurst = DMA_MBURST_SINGLE;
259 dma_handle.Init.PeriphBurst = DMA_PBURST_SINGLE;
260
261 dma_handle.Instance = SDRAM_DMAx_STREAM;
262
263 /* Associate the DMA handle */
264 __HAL_LINKDMA(&sdramHandle, hdma, dma_handle);
265
266 /* Deinitialize the stream for new transfer */
267 HAL_DMA_DeInit(&dma_handle);
268
269 /* Configure the DMA stream */
270 HAL_DMA_Init(&dma_handle);
271
272#if 0
273 /* NVIC configuration for DMA transfer complete interrupt */
274 HAL_NVIC_SetPriority(SDRAM_DMAx_IRQn, 5, 0);
275 HAL_NVIC_EnableIRQ(SDRAM_DMAx_IRQn);
276#endif
277
278 if (HAL_SDRAM_Init(&sdramHandle, &Timing) != HAL_OK) {
279 sdramstatus = SDRAM_ERROR;
280 } else {
281 sdramstatus = SDRAM_OK;
282 }
283
284 /* SDRAM initialization sequence */
285 BSP_SDRAM_Initialization_sequence(REFRESH_COUNT,
286 GetCasLatencyModeReg(config));
287
288 return sdramstatus;
289}
290
291