[processor-sdk/performance-audio-sr.git] / psdk_cust / pdk_k2g_1_0_1_1_eng / packages / ti / boot / sbl / tools / flashWriter / qspi / src / sbl_qspi_writer.c
1 /**
2 * \file sbl_qspi.c
3 *
4 * \brief This file contains functions for QSPI read/write operations for SBL
5 *
6 */
8 /*
9 * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 *
18 * Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the
21 * distribution.
22 *
23 * Neither the name of Texas Instruments Incorporated nor the names of
24 * its contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 */
41 /* ========================================================================== */
42 /* Include Files */
43 /* ========================================================================== */
45 /* ========================================================================== */
46 /* Include Files */
47 /* ========================================================================== */
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <stdlib.h>
52 /* TI-RTOS Header files */
53 #include <ti/drv/uart/UART.h>
54 #include <ti/drv/uart/src/UART_osal.h>
55 #include <ti/drv/uart/UART_stdio.h>
56 #include <ti/csl/tistdtypes.h>
58 #include <ti/drv/mmcsd/MMCSD.h>
59 #include <ti/drv/mmcsd/soc/MMCSD_v1.h>
60 #include <ti/drv/mmcsd/src/MMCSD_osal.h>
62 #include <ti/fs/fatfs/ff.h>
63 #include <ti/fs/fatfs/FATFS.h>
65 /* TI-RTOS Header files */
66 #include <ti/drv/spi/SPI.h>
67 #include <ti/drv/spi/src/SPI_osal.h>
68 #include <ti/drv/spi/soc/SPI_soc.h>
69 #include <ti/drv/spi/test/qspi_flash/src/QSPI_board.h>
70 #include <ti/drv/spi/test/qspi_flash/src/SPI_log.h>
72 #include <ti/board/src/flash/include/board_flash.h>
74 #include "sbl_soc.h"
76 #define S_PASS (0U)
78 #define E_FAIL (-1U)
80 /** \brief Maximum buffer size. */
81 #define MMCSD_DATA_BUF_SIZE (512U)
84 /* ========================================================================== */
85 /* Global Variables */
86 /* ========================================================================== */
88 FATFS_Handle fatfsHandle = NULL;
90 /* MMCSD function table for MMCSD implementation */
91 FATFS_DrvFxnTable FATFS_drvFxnTable = {
92 MMCSD_close,
93 MMCSD_control,
94 MMCSD_init,
95 MMCSD_open,
96 MMCSD_write,
97 MMCSD_read
98 };
100 /* FATFS configuration structure */
101 FATFS_HwAttrs FATFS_initCfg[_VOLUMES] =
102 {
103 {
104 0U
105 },
106 {
107 1U
108 },
109 {
110 2U
111 },
112 {
113 3U
114 }
115 };
117 /* FATFS objects */
118 FATFS_Object FATFS_objects[_VOLUMES];
120 /* FATFS configuration structure */
121 const FATFS_Config FATFS_config[_VOLUMES + 1] = {
122 {
123 &FATFS_drvFxnTable,
124 &FATFS_objects[0],
125 &FATFS_initCfg[0]
126 },
128 {
129 &FATFS_drvFxnTable,
130 &FATFS_objects[1],
131 &FATFS_initCfg[1]
132 },
134 {
135 &FATFS_drvFxnTable,
136 &FATFS_objects[2],
137 &FATFS_initCfg[2]
138 },
140 {NULL, NULL, NULL},
142 {NULL, NULL, NULL}
143 };
145 /**
146 * \brief Temporary data buffer to be used while copying data
147 * from SD card to DDR.
148 */
149 static char gTmpBuf[MMCSD_DATA_BUF_SIZE];
151 /* ========================================================================== */
152 /* Structures and Enums */
153 /* ========================================================================== */
155 /* None */
157 /* ========================================================================== */
158 /* Internal Function Declarations */
159 /* ========================================================================== */
162 /**
163 * \brief This API copies the flash image from MMCSD to DDR
164 *
165 * \param fileName Name of the file to be copied.
166 * \param destAddr Address where the file is to be copied
167 * \param fileSize Total size of the file read
168 *
169 * \retval status S_PASS Image copy passed
170 * \retval status E_FAIL Image copy failed
171 */
172 static int32_t MmcsdImageCopy(char * fileName,
173 uint32_t destAddr,
174 uint32_t * fileSize);
176 /* ========================================================================== */
177 /* Function Definitions */
178 /* ========================================================================== */
180 int main(void)
181 {
182 FRESULT fresult;
183 FIL fileObject;
185 /* File name which need to be copied from mmcsd to QSPI */
186 char fileName[9U];
187 /* Offset address where the flash image is to be written */
188 int offsetAddr = 0U;
189 /* DDR address where image has to be copied from mmcsd card.
190 * The image is then copied from this address to QSPI flash
191 */
192 uint32_t destAddr = 0x82000000U;
193 uint32_t length = 0U;
194 int32_t status = E_FAIL;
196 void *boardHandle;
198 int32_t ret;
200 if (SBL_socInit())
201 {
202 UART_printf("Error initialzing soc!\n");
203 return -1;
204 }
205 else
206 {
207 UART_printf("\n*** PDK QSPI Flash Writer ***\n");
208 }
210 if (SBL_qspiInit(&boardHandle))
211 {
212 UART_printf("BOARD_flashOpen failed. \n");
213 return -1;
214 }
216 /* Initialization of the driver. */
217 fresult = FATFS_init();
219 /* MMCSD FATFS initialization */
220 fresult = FATFS_open(0U, NULL, fatfsHandle);
222 if (FR_OK == fresult)
223 {
224 /* Open the config file and read the file name and parameters */
225 fresult = f_open(&fileObject, "config", FA_READ);
227 if(FR_OK != fresult)
228 {
229 /* If there was some problem opening the file, then return an error. */
230 UART_printf("\r\n Unable to open config file \r\n");
231 }
232 else
233 {
234 while(0U != f_gets(gTmpBuf, sizeof(gTmpBuf), &fileObject))
235 {
236 if ( -1 != sscanf(gTmpBuf,"%s %x", fileName, &offsetAddr))
237 {
238 status = S_PASS;
239 }
240 else
241 {
242 status = E_FAIL;
243 UART_printf("\nSorry! Something wrong in config file\n");
244 UART_printf("\nPlease check if file is in format shown below\n");
245 UART_printf("\nboot 0\napp 80000\n");
246 }
247 if (S_PASS == status)
248 {
249 length = 0U;
250 status = MmcsdImageCopy(fileName,
251 destAddr,
252 &length);
253 }
254 if (S_PASS == status)
255 {
256 UART_printf("Begin flashing '%s' into QSPI\n", fileName);
257 if ((ret = SBL_qspiFlashWrite(&boardHandle, (uint8_t *) destAddr, length, offsetAddr)))
258 {
259 UART_printf("SBL_qspiFlash: received ERROR %d\n", ret);
260 return -1;
261 }
262 else
263 {
264 UART_printf("Finished flashing '%s' with size %x at offset %x\n", fileName, length, offsetAddr);
265 }
266 }
267 else
268 {
269 UART_printf("ERROR: Unable to load '%s' into local memory!\n", fileName);
270 return -1;
271 }
272 }
273 /* Close the file here */
274 f_close(&fileObject);
275 }
277 UART_printf("Flashing completed! \n");
278 }
279 SBL_qspiClose(&boardHandle);
281 return (S_PASS);
282 }
284 /* -------------------------------------------------------------------------- */
285 /* Internal Function Definitions */
286 /* -------------------------------------------------------------------------- */
288 static int32_t MmcsdImageCopy(char * fileName,
289 uint32_t destAddr,
290 uint32_t * fileSize)
291 {
292 FRESULT fResult;
293 static FIL fileObject;
294 uint32_t bytesRead = 0U;
295 int32_t status = E_FAIL;
296 /* Open the file for reading */
297 fResult = f_open(&fileObject, fileName, FA_READ);
299 if(FR_OK == fResult)
300 {
301 status = S_PASS;
302 }
303 /* If there was some problem opening the file, then return an error. */
304 else
305 {
306 UART_printf("\r\n Unable to open file %s \r\n", fileName);
307 }
309 if(S_PASS == status)
310 {
311 /*
312 * Enter a loop to repeatedly read data from the file and display it, until
313 * the end of the file is reached.
314 */
315 UART_printf("Copying '%s' to local memory\n", fileName);
316 do
317 {
318 /*
319 * Read a block of data from the file. Read as much as can fit in the
320 * temporary buffer, including a space for the trailing null.
321 */
322 fResult = f_read(&fileObject, gTmpBuf, sizeof(gTmpBuf) - 1,
323 (uint32_t *) &bytesRead);
325 /*
326 * If there was an error reading, then print a newline and return the
327 * error to the user.
328 */
329 if(fResult != FR_OK)
330 {
331 UART_printf("\r\n Error reading application file\r\n");
332 status = E_FAIL;
333 }
335 if(bytesRead >= sizeof(gTmpBuf))
336 {
337 status = E_FAIL;
338 }
340 /* Read the last chunk of the file that was received. */
341 memcpy((uint32_t *)destAddr, gTmpBuf, (sizeof(gTmpBuf) - 1));
342 destAddr += (sizeof(gTmpBuf) - 1);
343 /*
344 * Continue reading until less than the full number of bytes are read.
345 * That means the end of the buffer was reached.
346 */
347 *(fileSize) += bytesRead;
348 }
349 while((bytesRead == sizeof(gTmpBuf) - 1) && (S_PASS == status));
350 /* Close the file. */
351 fResult = f_close(&fileObject);
352 if (E_FAIL == status)
353 {
354 UART_printf(" Read failed for %s\n",fileName);
355 }
356 }
357 return status;
358 }