[processor-sdk/performance-audio-sr.git] / pdk_k2g_1_0_1 / packages / ti / platform / evmk2g / platform_lib / src / platform.c
1 /*
2 * Copyright (c) 2010-2015, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
34 /**
35 *
36 * \file platform.c
37 *
38 * \brief This contains platform common APIs. This file acts as entry
39 * point for all the platform library modules.
40 *
41 ******************************************************************************/
43 #include "platform_internal.h"
45 /* Errno value */
46 uint32_t platform_errno = 0;
47 uint32_t platform_init_return_code = 0;
48 uint8_t uart_port_num = PLATFORM_UART_PORT_0;
50 /* Platform Library Version*/
51 #if (PLATFORM_VERSTRING_IN)
52 #pragma DATA_SECTION(platform_library_version,"platform_lib");
53 static char platform_library_version[16] = PLATFORM_LIB_VERSION;
54 #endif
56 /* Information we need to keep around for access */
57 #pragma DATA_SECTION(platform_mcb,"platform_lib");
58 static struct platform_mcb_t {
59 uint32_t frequency;
60 int32_t board_version;
61 int32_t mastercore;
62 } platform_mcb = {0, 0, 0};
64 #if (PLATFORM_WRITE_IN)
65 #pragma DATA_SECTION(write_type,"platform_lib");
66 static WRITE_info write_type;
67 #pragma DATA_SECTION(write_buffer,"platform_lib");
68 static char write_buffer[MAX_WRITE_LEN];
69 #endif
71 #if (PLATFORM_READ_IN)
72 #pragma DATA_SECTION(read_type,"platform_lib");
73 READ_info read_type = PLATFORM_READ_SCANF;
74 #endif
77 /* This structure holds information about the devices on the platform */
78 #if (PLATFORM_NAND_IN)
79 #pragma DATA_SECTION(gDeviceNandBBlist,"platform_lib");
80 uint8_t gDeviceNandBBlist[BLOCKS_PER_DEVICE];
82 #pragma DATA_SECTION(gDeviceNand,"platform_lib");
83 PLATFORM_DEVICE_info gDeviceNand = {0x2C, 0xCA, PLATFORM_DEVICE_NAND, 16, BLOCKS_PER_DEVICE, PAGES_PER_BLOCK, BYTES_PER_PAGE, SPARE_BYTES_PER_PAGE, PLATFORM_DEVID_MT29F2G16ABAFA, 5, 0x400, 0, NULL, NULL};
84 #endif
86 #if (PLATFORM_NOR_IN)
87 #pragma DATA_SECTION(gDeviceNor,"platform_lib");
88 PLATFORM_DEVICE_info gDeviceNor = {0x20, 0xBA, PLATFORM_DEVICE_NOR, 8, SPI_NOR_SECTOR_COUNT, (SPI_NOR_PAGE_COUNT / SPI_NOR_SECTOR_COUNT), SPI_NOR_PAGE_SIZE, 0, PLATFORM_DEVID_NORN25Q128A13ESF40F, 0, 0, 0, NULL, NULL};
89 #endif
91 #if (PLATFORM_I2C_EEPROM_IN)
92 #pragma DATA_SECTION(gDeviceEeprom0,"platform_lib");
93 PLATFORM_DEVICE_info gDeviceEeprom0 = {PLATFORM_I2C_EEPROM_MANUFACTURER_ID,PLATFORM_I2C_EEPROM_DEVICE_ID_1,PLATFORM_DEVICE_EEPROM, 8, 1, 1, 65536, 0, PLATFORM_DEVID_EEPROM50, 0, 0, 0, NULL, NULL};
94 #pragma DATA_SECTION(gDeviceEeprom1,"platform_lib");
95 PLATFORM_DEVICE_info gDeviceEeprom1 = {PLATFORM_I2C_EEPROM_MANUFACTURER_ID,PLATFORM_I2C_EEPROM_DEVICE_ID_2,PLATFORM_DEVICE_EEPROM, 8, 1, 1, 65536, 0, PLATFORM_DEVID_EEPROM51, 0, 0, 0, NULL, NULL};
96 #endif
98 #if (PLATFORM_MMCHS_IN)
100 #pragma DATA_SECTION(gDeviceSd,"platform_lib");
101 PLATFORM_DEVICE_info gDeviceSd = {0, 0, PLATFORM_DEVICE_MAX, 0, 0, 0, 0, 0, PLATFORM_DEVID_SD, 0, 0, 0, NULL, NULL};
103 #pragma DATA_SECTION(gDeviceEmmc,"platform_lib");
104 PLATFORM_DEVICE_info gDeviceEmmc = {0, 0, PLATFORM_DEVICE_MAX, 0, 0, 0, 0, 0, PLATFORM_DEVID_EMMC, 0, 0, 0, NULL, NULL};
106 mmchsInfo *mmcInfo;
107 mmchsInfo *sdInfo;
108 mmchsCardInfo *mmcCard;
109 mmchsCardInfo *sdCard;
111 #endif
113 #if (PLATFORM_QSPI_FLASH_IN)
114 #pragma DATA_SECTION(gDeviceQspiFlash,"platform_lib");
115 PLATFORM_DEVICE_info gDeviceQspiFlash = {0, 0, PLATFORM_DEVICE_QSPI_FLASH, 8, QSPI_FLASH_NUM_SECTORS, (QSPI_FLASH_SECTOR_SIZE / QSPI_FLASH_PAGE_SIZE), QSPI_FLASH_PAGE_SIZE, 0, PLATFORM_DEVID_QSPIFLASH_S25FL512S, 0, 0, 0, NULL, NULL};
116 uint8_t qspiReadMode = PLATFORM_QSPI_IO_MODE_QUAD;
117 uint8_t qspiWriteMode = PLATFORM_QSPI_IO_MODE_QUAD;
118 #endif
120 /* This structure holds information about the EMAC port on the platform */
121 #pragma DATA_SECTION(emac_port_mode,"platform_lib");
122 PLATFORM_EMAC_PORT_MODE emac_port_mode[PLATFORM_MAX_EMAC_PORT_NUM] =
123 {
124 PLATFORM_EMAC_PORT_MODE_PHY,
125 PLATFORM_EMAC_PORT_MODE_PHY
126 };
128 #if (PLATFORM_EXTMEMTEST_IN)
129 static inline int32_t platform_memory_test (uint32_t start_address, uint32_t end_address);
130 #endif
132 uint8_t gSysClkSel = 0;
134 /******************************************************************************
135 * platform_get_frequency
136 *
137 * Internal function to read frequency from PLL.
138 *
139 ******************************************************************************/
140 static inline uint32_t platform_get_frequency(void)
141 {
142 CSL_Status status;
143 PllcHwSetup hwSetupRead;
144 uint32_t dsp_freq;
146 status = CorePllcGetHwSetup (&hwSetupRead);
148 if (status != CSL_SOK) {
149 IFPRINT(platform_write("platform_get_frequency: Hardware setup parameters reading... Failed.\n"));
150 IFPRINT(platform_write("\tReason: Error setting in hardware validation."\
151 " [status = 0x%x].\n", status));
152 platform_errno = PLATFORM_ERRNO_GENERIC;
153 return (uint32_t)-1;
154 } else {
155 /* Compute the real dsp freq (*100) */
156 dsp_freq = (hwSetupRead.pllM + 1)/(hwSetupRead.preDiv + 1);
157 dsp_freq = (dsp_freq * PLATFORM_BASE_CLK_RATE_MHZ)/(hwSetupRead.postDiv + 1);
158 }
160 return (dsp_freq);
162 }
164 Bool serial_num_isvalid(char c)
165 {
166 if (
167 ((c >= '0') && (c <= '9')) ||
168 ((c >= 'a') && (c <= 'z')) ||
169 ((c >= 'A') && (c <= 'Z'))
170 )
171 {
172 return TRUE;
173 }
174 else
175 {
176 return FALSE;
177 }
178 }
180 static void getSerialNumber(char *buf)
181 {
182 #if (PLATFORM_I2C_EEPROM_IN)
183 PLATFORM_DEVICE_info *p_device;
184 uint32_t i;
186 buf[0] = 0;
188 p_device = platform_device_open(PLATFORM_DEVID_EEPROM50, 0);
189 if (p_device != NULL)
190 {
191 /* Serial number stored in the last 128 bytes of the EEPROM 0x50 */
192 if (platform_device_read(p_device->handle, gDeviceEeprom0.page_size-MAX_SN_STORE_SIZE, (uint8_t *)buf, 16) == Platform_EOK)
193 {
194 for (i = 0; i < MAX_SN_SIZE; i++)
195 {
196 if(!serial_num_isvalid(buf[i]))
197 {
198 break;
199 }
200 }
201 buf[i] = 0;
202 }
203 else
204 {
205 IFPRINT(platform_write("Unable to read board serial number."));
206 }
207 }
209 platform_device_close(p_device->handle);
210 #endif
212 return;
213 }
215 /******************************************************************************
216 * platform_get_info
217 ******************************************************************************/
218 #if (PLATFORM_GETINFO_IN)
219 void platform_get_info(platform_info * p_info)
220 {
221 uint32_t csr = CSL_chipReadCSR();
222 volatile uint32_t *megm_rev = (uint32_t *) (MEGM_REV_ID_REGISTER);
223 uint32_t i;
225 if (p_info == 0) {
226 IFPRINT(platform_write("p_info argument is NULL\n"));
227 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
228 return;
229 }
231 memset(p_info, 0, sizeof(platform_info));
233 strncpy(p_info->version, platform_library_version, 16);
235 p_info->cpu.core_count = PLATFORM_CORE_COUNT;
236 p_info->cpu.id = (uint16_t) CSL_FEXT(csr, CHIP_CSR_CPU_ID);
237 p_info->cpu.revision_id = (uint16_t) CSL_FEXT(csr, CHIP_CSR_REV_ID);
238 strncpy(p_info->cpu.name, PLATFORM_INFO_CPU_NAME, 32);
240 p_info->cpu.megamodule_revision_major =
241 (uint16_t)(((*megm_rev) & MEGM_REV_ID_MAJ_MASK) >> MEGM_REV_ID_MAJ_SHIFT);
242 p_info->cpu.megamodule_revision_minor =
243 (uint16_t)(((*megm_rev) & MEGM_REV_ID_MIN_MASK) >> MEGM_REV_ID_MIN_SHIFT);
245 strncpy(p_info->board_name, PLATFORM_INFO_BOARD_NAME, 32);
247 /* Only little endian mode is supported by the device */
248 p_info->cpu.endian = PLATFORM_LE;
250 p_info->emac.port_count = PLATFORM_MAX_EMAC_PORT_NUM;
252 p_info->frequency = platform_get_frequency();
253 platform_mcb.frequency = p_info->frequency;
254 p_info->board_rev = getBoardVersion();
255 platform_mcb.board_version = p_info->board_rev;
256 getSerialNumber(p_info->serial_nbr);
258 platform_get_macaddr(PLATFORM_MAC_TYPE_EFUSE, &(p_info->emac.efuse_mac_address[0]));
259 platform_get_macaddr(PLATFORM_MAC_TYPE_EEPROM, &(p_info->emac.eeprom_mac_address[0]));
261 for (i = PLATFORM_USER_LED_CLASS; i < PLATFORM_END_LED_CLASS; i++ ) {
262 switch (i) {
263 case PLATFORM_USER_LED_CLASS:
264 p_info->led[i].count = PLATFORM_SOC_LED_COUNT;
265 break;
266 case PLATFORM_SYSTEM_LED_CLASS:
267 p_info->led[i].count = PLATFORM_I2C_LED_COUNT;
268 break;
269 default:
270 IFPRINT(platform_write("Can't read LED Class information\n"));
271 platform_errno = PLATFORM_ERRNO_LED;
272 break;
273 }
274 }
275 }
276 #endif
278 /******************************************************************************
279 * platform_init
280 ******************************************************************************/
281 #if (PLATFORM_INIT_IN)
283 /*Enable EDC on MSMC*/
284 /* Note: Once MSMC EDC is enabled, error correction stays enabled until
285 * the MSMC is reset
286 */
287 static int MSMC_enableEDC ()
288 {
289 unsigned int status = 0;
291 *(unsigned int *)(SMEDCC) &= 0x7FFFFFFF; //Clear SEN(bit31)=0
292 *(unsigned int *)(SMEDCC) |= 0x40000000; //Set ECM(bit30)=1
294 /* Check the status */
295 status = *(unsigned int *)(SMEDCC);
298 if ((status>>30)==0x1)
299 /* Enabled */
300 return 1;
302 /* Failed */
303 return 0;
304 }
307 /*Enable EDC on L1P*/
308 static int enableL1PEDC ()
309 {
310 unsigned int status = 0;
312 *(unsigned int *)(L1PEDCMD) = 0x1; //Set EN(bit0)=1
314 /* Check the status */
315 status = *(unsigned int *)(L1PEDSTAT);
317 if ((status<<28) == 0x10000000)
318 /* Enabled */
319 return 1;
321 /* Failed */
322 return 0;
323 }
326 /*Enable EDC on L2*/
327 static int enableL2EDC ()
328 {
329 unsigned int status = 0;
331 *(unsigned int *)(L2EDCMD) = 0x1;
333 /* Check the status */
334 status = *(unsigned int *)(L2EDSTAT);
336 if ((status<<28) == 0x10000000)
337 /* Enabled */
338 return 1;
340 /* Failed */
341 return 0;
342 }
344 /*Enable all bits in L2EDCEN*/
345 static int enableEDCL2EDCEN ()
346 {
347 /* Set DL2CEN(bit0),PL2CEN(bit1),DL2SEN(bit2),PL2SEN(bit3),SDMAEN(bit4)=1 */
348 *(unsigned int *)(L2EDCEN) |= 0x1F;
349 return 1;
350 }
352 void configSoCGpio(void)
353 {
354 /* FLASH_HOLD */
355 gpioSetDirection(GPIO_PORT_1, 41, GPIO_OUT);
356 //gpioClearOutput(GPIO_PORT_1, 41);
357 gpioSetOutput(GPIO_PORT_1, 41);
359 // Set GPIO1_54 to output, drive low.
360 // This enables UART connection to RS232 tranceiver / DB9 connector on K2G EVM.
361 gpioSetDirection(GPIO_PORT_1, 54, GPIO_OUT);
362 gpioClearOutput(GPIO_PORT_1, 54);
363 }
365 #define ENABLE_CLK_DEBUG
367 void platform_info_print(void)
368 {
369 platform_write("\n\nPlatform Info:\n");
370 platform_write("**************************************\n");
371 platform_write("66AK2G02 - C66 Core\n");
372 platform_write("\nClock Selection - ");
373 if(gSysClkSel)
374 {
375 platform_write("External Clock on SYSCLKP\n\n");
376 }
377 else
378 {
379 platform_write("Internal Clock on SYSOSC_IN\n\n");
380 }
382 platform_write("Core PLL Clock - 600MHz\n");
383 platform_write("DDR PLL Clock - 200MHz\n");
384 platform_write("UART PLL Clock - 384MHz\n");
385 platform_write("DSS PLL Clock - 72.4MHz\n");
386 platform_write("NSS PLL Clock - 1000MHz\n");
387 platform_write("ICSS PLL Clock - 200MHz\n");
388 platform_write("**************************************\n\n\n");
389 }
391 Platform_STATUS platform_init(platform_init_flags * p_flags,
392 platform_init_config * p_config)
393 {
394 CSL_Status status;
395 PllcHwSetup pllc_hwSetup;
396 PllcHwSetup pllc_hwSetupRead;
397 volatile uint32_t i;
398 uint8_t pData[50];
400 struct pll_init_data ddr_pll_data = {DDR3A_PLL, PLLM_DDR3,PLLD_DDR3,PLLOD_DDR3};
401 pll_init_data pll_data;
403 #ifdef PLATFORM_PLL_REINIT
404 int loop_count;
405 #endif
407 /*************************************************************************
408 * This routine may be called before BIOS or the application has loaded.
409 * Do not try and write debug statements from here.
410 ***********************************************************************/
412 if ((p_flags == 0) || (p_config == 0)){
413 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
414 return ( (Platform_STATUS) Platform_EFAIL);
415 }
417 /* Start TCSL so its free running */
418 CSL_chipWriteTSCL(0);
420 /* Unlock the Boot Config */
421 CSL_BootCfgUnlockKicker();
423 /* Get the sys clock selection based on SYSCLKSEL pin status */
424 gSysClkSel = CSL_FEXT (hBootCfg->PLLCLKSEL_STAT,
425 BOOTCFG_PLLCLKSEL_STAT_SYSCLKSEL_STAT);
426 if(gSysClkSel)
427 {
428 //printf("Clock Input is SYSCLKP\n");
430 ddr_pll_data.pll = 0;
431 ddr_pll_data.pll_m = DDRCLKP_PLLM_DDR3;
432 ddr_pll_data.pll_d = DDRCLKP_PLLD_DDR3;
433 ddr_pll_data.pll_od = DDRCLKP_PLLOD_DDR3;
434 }
435 else
436 {
437 //printf("Clock Input is HF OSC\n");
438 }
440 #if defined(SODIMM_CONFIG)
441 uint8_t buf[256];
442 uint8_t i2cportnumber=1;
443 if(readSPD(0x53,buf,i2cportnumber) == Platform_EOK)
444 {
445 uint8_t Dividend,Divisor;
446 uint16_t ddrClock;
447 float MTB,tCK;
448 Dividend = buf[10];
449 Divisor = buf[11];
450 MTB = (float)Dividend/Divisor;
451 tCK = buf[12]*MTB; // minimum cycle time tCK
452 ddrClock = ((1/tCK)*1000)*2;
453 if(ddrClock == 800)
454 {
455 ddr_pll_data.pll = 0;
456 ddr_pll_data.pll_m = 4;
457 ddr_pll_data.pll_d = 1;
458 ddr_pll_data.pll_od = 2;
459 }
460 else if(ddrClock == 1066)
461 {
462 ddr_pll_data.pll = 0;
463 ddr_pll_data.pll_m = 16;
464 ddr_pll_data.pll_d = 1;
465 ddr_pll_data.pll_od = 6;
466 }
467 else if(ddrClock == 1333)
468 {
469 ddr_pll_data.pll = 0;
470 ddr_pll_data.pll_m = 20;
471 ddr_pll_data.pll_d = 1;
472 ddr_pll_data.pll_od = 6;
473 }
474 else if(ddrClock == 1600)
475 {
476 ddr_pll_data.pll = 0;
477 ddr_pll_data.pll_m = 8;
478 ddr_pll_data.pll_d = 1;
479 ddr_pll_data.pll_od = 2;
480 }
481 else
482 {
483 printf("ddrClock --> %d is not valid\n",ddrClock);
484 return ( (Platform_STATUS) Platform_EFAIL);
485 }
486 }
487 #endif
489 #ifdef PLATFORM_PLL_REINIT
490 for (loop_count = 0; loop_count < 10; loop_count++) {
491 platform_errno = 0;
492 #endif
494 PowerUpDomains();
496 /* Initialise default pin muxing */
497 pinMuxInit();
499 /* PLLC module handle structure */
500 if (p_flags->pll)
501 {
502 /* Set the Core PLL */
503 /* Clear local data structures */
504 memset(&pllc_hwSetup, 0, sizeof(PllcHwSetup));
506 /* Setup PLLC hardware parameters */
507 pllc_hwSetup.divEnable = (CSL_BitMask32) (PLLC_DIVEN_PLLDIV2 |
508 PLLC_DIVEN_PLLDIV3) ;
510 /* Setup PLLC hardware parameters */
511 pllc_hwSetup.pllM =
512 (((p_config->pllm) ? p_config->pllm : PLATFORM_PLL1_PLLM_val) - 1);
513 pllc_hwSetup.preDiv = PLATFORM_PLL_PREDIV_val - 1;
514 pllc_hwSetup.pllDiv2 = PLATFORM_PLLDIV2_val - 1;
515 pllc_hwSetup.pllDiv3 = PLATFORM_PLLDIV3_val - 1;
516 pllc_hwSetup.postDiv = PLATFORM_PLL_POSTDIV_val -1;
518 /* Configure PLL muxing and selection */
519 configPllClkSelection(gSysClkSel);
521 #ifdef ENABLE_CLK_DEBUG
522 /* Enable Main PLL on OBSCLK pin and SYSCLKOUT */
523 enablePllObsClk(OBSCLK_MAIN_PLL);
524 CSL_FINS(hBootCfg->DEVCFG, BOOTCFG_DEVCFG_SYSCLKOUTEN, 1);
525 #endif
526 /* set Pll */
527 status = CorePllcHwSetup (&pllc_hwSetup);
529 if (status != CSL_SOK) {
530 platform_errno = PLATFORM_ERRNO_PLL_SETUP;
531 return ( (Platform_STATUS) Platform_EFAIL);
532 }
534 /* Read back */
535 status = CorePllcGetHwSetup (&pllc_hwSetupRead);
537 if (status != CSL_SOK) {
538 platform_errno = PLATFORM_ERRNO_PLL_SETUP;
539 return ( (Platform_STATUS) Platform_EFAIL);
540 }
542 #ifdef ENABLE_CLK_DEBUG
543 /* Enable NSS PLL on OBSCLK pin */
544 enablePllObsClk(OBSCLK_NSS_PLL);
545 #endif
546 pll_data.pll_m = PLLM_NSS;
547 pll_data.pll_d = PLLD_NSS;
548 pll_data.pll_od = CLKOD_NSS;
550 /* Set the NSS PLL */
551 status = SetNssPllConfig(&pll_data);
552 if (status != CSL_SOK) {
553 platform_errno = PLATFORM_ERRNO_PLL_SETUP;
554 return ( (Platform_STATUS) Platform_EFAIL);
555 }
557 #ifdef ENABLE_CLK_DEBUG
558 /* Enable DDR3 PLL on OBSCLK pin */
559 enablePllObsClk(OBSCLK_DDR3A_PLL);
560 #endif
561 /* Set the DDR3 PLL */
562 status = SetDDR3PllConfig(&ddr_pll_data);
563 if (status != CSL_SOK)
564 {
565 platform_errno = PLATFORM_ERRNO_PLL_SETUP;
566 return ( (Platform_STATUS) Platform_EFAIL);
567 }
569 #ifdef ENABLE_CLK_DEBUG
570 /* Enable DSS PLL on OBSCLK pin */
571 enablePllObsClk(OBSCLK_DSS_PLL);
572 #endif
574 pll_data.pll_m = PLLM_DSS;
575 pll_data.pll_d = PLLD_DSS;
576 pll_data.pll_od = CLKOD_DSS;
578 /* Set the DSS PLL */
579 status = SetDssPllConfig(&pll_data);
580 if (status != CSL_SOK) {
581 platform_errno = PLATFORM_ERRNO_PLL_SETUP;
582 return ( (Platform_STATUS) Platform_EFAIL);
583 }
585 #ifdef ENABLE_CLK_DEBUG
586 /* Enable ICSS PLL on OBSCLK pin */
587 enablePllObsClk(OBSCLK_ICSS_PLL);
588 #endif
590 pll_data.pll_m = PLLM_ICSS;
591 pll_data.pll_d = PLLD_ICSS;
592 pll_data.pll_od = CLKOD_ICSS;
594 /* Set the ICSS PLL */
595 status = SetIcssPllConfig(&pll_data);
596 if (status != CSL_SOK) {
597 platform_errno = PLATFORM_ERRNO_PLL_SETUP;
598 return ( (Platform_STATUS) Platform_EFAIL);
599 }
601 #ifdef ENABLE_CLK_DEBUG
602 /* Enable UART PLL on OBSCLK pin */
603 enablePllObsClk(OBSCLK_UART_PLL);
604 #endif
606 pll_data.pll_m = PLLM_UART;
607 pll_data.pll_d = PLLD_UART;
608 pll_data.pll_od = CLKOD_UART;
610 /* Set the UART PLL */
611 status = SetUartPllConfig(&pll_data);
612 if (status != CSL_SOK) {
613 platform_errno = PLATFORM_ERRNO_PLL_SETUP;
614 return ( (Platform_STATUS) Platform_EFAIL);
615 }
616 }
618 /* Save frequency, its needed by platform_delay */
619 if(!platform_mcb.frequency) {
620 platform_mcb.frequency = platform_get_frequency();
621 }
623 /* Initialize DDR */
624 if (p_flags->ddr) {
626 xmc_setup();
628 /* Delay 10msec */
629 for (i=0; i<1000; i++)
630 platform_delay (10);
631 #if defined(SODIMM_CONFIG)
632 status = init_ddr3param(buf);
633 #else
635 status = DDR3Init();
636 #endif
638 if (status != CSL_SOK) {
639 platform_errno = PLATFORM_ERRNO_GENERIC;
640 return ( (Platform_STATUS) Platform_EFAIL);
641 }
642 }
644 #ifdef PLATFORM_PLL_REINIT
645 if (!p_flags->pll || !p_flags->ddr) {
646 break;
647 }
649 /* Run DDR3 test */
650 if (platform_memory_test(PLL_REINIT_DDR3_TEST_START_ADDR,
651 PLL_REINIT_DDR3_TEST_END_ADDR) == Platform_EOK) {
652 break;
653 }
654 }
655 platform_init_return_code = loop_count;
657 if (loop_count == 10) {
658 platform_errno = PLATFORM_ERRNO_GENERIC;
659 return ( (Platform_STATUS) Platform_EFAIL);
660 }
661 #endif
663 /* Enable Error Correction for memory */
664 if (p_flags->ecc) {
665 enableL1PEDC();
666 enableEDCL2EDCEN();
667 enableL2EDC();
668 MSMC_enableEDC();
669 }
671 //TODO: Need to change SGMII to RGMII
672 if (p_flags->phy) {
673 //configSerdes();
674 //Init_SGMII(0);
675 //Init_SGMII(1);
676 }
678 #if (PLATFORM_GPIO_IN)
679 gpioInit(GPIO_PORT_0);
680 gpioInit(GPIO_PORT_1);
681 configSoCGpio();
682 #endif
684 #if (PLATFORM_LED_IN)
685 /* Enable GPIO mode for LED0 and LED1 */
686 pinMuxSetMode(SOC_LED0_PADCONFIG, PADCONFIG_MUX_MODE_QUATERNARY);
687 pinMuxSetMode(SOC_LED1_PADCONFIG, PADCONFIG_MUX_MODE_QUATERNARY);
689 /* Configure LED0 and LED1 GPIO pins as output */
690 gpioSetDirection(GPIO_PORT_0, SOC_LED0_GPIO, GPIO_OUT);
691 gpioSetDirection(GPIO_PORT_1, SOC_LED1_GPIO, GPIO_OUT);
692 #endif
694 #if (PLATFORM_I2C_IN)
695 /* Initialize all the I2C ports - Needed for different I2C devices on the board */
696 evmI2CInit(I2C_PORT_0);
697 evmI2CInit(I2C_PORT_1);
698 evmI2CInit(I2C_PORT_2);
699 //status = i2cProbe (0x0, 0x5C, 0x0900, pData, 0x2 );
700 //status = i2cProbe (0x0, 0x5C, 0x0000, pData, 0x2 );
701 #endif
703 #if (PLATFORM_I2C_IO_EXP_IN)
704 /* Initialize IO expander */
705 i2cIoExpanderInit();
706 #endif
708 return Platform_EOK;
709 }
710 #endif
712 /******************************************************************************
713 * platform_get_coreid
714 ******************************************************************************/
715 #if (PLATFORM_GETCOREID_IN)
716 uint32_t platform_get_coreid(void)
717 {
718 return (CSL_chipReadDNUM());
719 }
720 #endif
722 uint32_t platform_get_clksel(void)
723 {
724 return (gSysClkSel);
725 }
727 /******************************************************************************
728 * platform_getmacaddr
729 ******************************************************************************/
730 #if (PLATFORM_GETMACADDR_IN)
731 Platform_STATUS platform_get_emac_info(uint32_t port_num, PLATFORM_EMAC_EXT_info * emac_info)
732 {
733 uint32_t mac_addr2, mac_addr1;
735 IFPRINT(platform_write("platform_get_emac_info called \n"));
737 emac_info->port_num = port_num;
738 emac_info->mode = emac_port_mode[port_num];
740 CSL_BootCfgGetMacIdentifier(&mac_addr1, &mac_addr2);
741 emac_info->mac_address[0] = ((mac_addr2 & 0x0000ff00) >> 8);
742 emac_info->mac_address[1] = (mac_addr2 & 0x000000ff);
744 emac_info->mac_address[2] = ((mac_addr1 & 0xff000000) >> 24);
745 emac_info->mac_address[3] = ((mac_addr1 & 0x00ff0000) >> 16);
746 emac_info->mac_address[4] = ((mac_addr1 & 0x0000ff00) >> 8);
747 emac_info->mac_address[5] = (mac_addr1 & 0x000000ff);
749 return Platform_EOK;
750 }
752 /*
753 * August 15, 2011 - platform_get_macaddr() is deprecated, application needs to call
754 * the new API platform_get_emac_info() to get the MAC address of the port
755 */
756 Platform_STATUS platform_get_macaddr(PLATFORM_MAC_TYPE type, uint8_t * p_mac_address)
757 {
758 IFPRINT(platform_write("platform_get_macaddr called \n"));
760 switch (type) {
762 case PLATFORM_MAC_TYPE_EFUSE:
763 {
764 uint32_t mac_addr2, mac_addr1;
766 CSL_BootCfgGetMacIdentifier(&mac_addr1, &mac_addr2);
767 p_mac_address[0] = ((mac_addr2 & 0x0000ff00) >> 8);
768 p_mac_address[1] = (mac_addr2 & 0x000000ff);
770 p_mac_address[2] = ((mac_addr1 & 0xff000000) >> 24);
771 p_mac_address[3] = ((mac_addr1 & 0x00ff0000) >> 16);
772 p_mac_address[4] = ((mac_addr1 & 0x0000ff00) >> 8);
773 p_mac_address[5] = (mac_addr1 & 0x000000ff);
775 return Platform_EOK;
776 }
778 default:
779 case PLATFORM_MAC_TYPE_EEPROM:
780 {
781 memset(p_mac_address, 0, 6);
782 return ((Platform_STATUS) Platform_EUNSUPPORTED);
783 }
784 }
785 }
786 #endif
788 /******************************************************************************
789 * platform_get_phy_addr
790 ******************************************************************************/
791 #if (PLATFORM_GETPHYADDR_IN)
792 int32_t platform_get_phy_addr(uint32_t port_num)
793 {
794 IFPRINT(platform_write("platform_get_phy_addr called \n"));
796 return port_num;
797 }
798 #endif
800 /******************************************************************************
801 * platform_phy_link_status
802 ******************************************************************************/
803 #if (PLATFORM_PHYLINKSTATUS_IN)
804 Platform_STATUS platform_phy_link_status(uint32_t port_num)
805 {
806 uint32_t phy_addr;
807 CSL_MDIO_USERACCESS user_access_reg;
809 IFPRINT(platform_write("platform_get_phy_link_status (portnum = %d) called \n", port_num));
811 phy_addr = platform_get_phy_addr( port_num);
813 CSL_MDIO_getUserAccessRegister(port_num, &user_access_reg);
815 user_access_reg.phyAddr = phy_addr;
816 user_access_reg.regAddr = 24; //The LED Control Reg address
817 user_access_reg.data &= 0xFFBE; // Need to check this in ?
818 user_access_reg.data |= 1;
820 CSL_MDIO_setUserAccessRegister(port_num, &user_access_reg);
822 return Platform_EOK;
823 }
824 #endif
826 /******************************************************************************
827 * platform_get_switch_state
828 ******************************************************************************/
829 #if (PLATFORM_GETSWITCHSTATE_IN)
830 uint32_t platform_get_switch_state(uint32_t id)
831 {
832 IFPRINT(platform_write("platform_get_switch_state(id=%d) called \n", id));
834 return (bmcGetUserSwitch(id));
835 }
836 #endif
839 /******************************************************************************
840 * platform_uart_read
841 ******************************************************************************/
842 #if (PLATFORM_UART_IN)
843 Platform_STATUS platform_uart_read(uint8_t *buf, uint32_t delay)
844 {
845 uint32_t delayCount = delay;
847 if (buf == NULL){
848 return ((Platform_STATUS) Platform_EINVALID);
849 }
851 while( (UartIsDataReady(uart_port_num)) != 1)
852 {
853 if (delayCount--)
854 {
855 platform_delay(1);
856 }
857 else
858 {
859 IFPRINT(platform_write("platform_uart_read: Read timeout\n"));
860 platform_errno = PLATFORM_ERRNO_READTO;
861 *buf = UartReadData(uart_port_num);
862 return ( (Platform_STATUS) Platform_EFAIL);
863 }
864 }
866 *buf = UartReadData (uart_port_num);
868 return Platform_EOK;
869 }
871 /******************************************************************************
872 * platform_uart_write
873 ******************************************************************************/
874 Platform_STATUS platform_uart_write(uint8_t buf)
875 {
876 UART_RET ret;
878 ret = UartWriteData(uart_port_num, buf);
879 if(ret != UART_RET_OK)
880 {
881 platform_errno = PLATFORM_ERRNO_DEV_FAIL;
882 return Platform_EFAIL;
883 }
885 return Platform_EOK;
886 }
888 /******************************************************************************
889 * platform_uart_set_baudrate
890 ******************************************************************************/
891 Platform_STATUS platform_uart_set_baudrate(uint32_t baudrate)
892 {
893 uint16_t brate;
894 UART_RET ret;
896 IFPRINT(platform_write("platform_uart_set_baudrate(baudrate=%d) called \n", baudrate));
898 brate = ((uint16_t) (PLATFORM_UART_INPUT_CLOCK_RATE/(baudrate * 16)));
900 ret = UartSetBaudRate(uart_port_num, brate);
901 if(ret != UART_RET_OK)
902 {
903 IFPRINT(platform_write("platform_uart_set_baudrate: Failed with error - %d\n", ret));
904 platform_errno = PLATFORM_ERRNO_DEV_FAIL;
905 return Platform_EFAIL;
906 }
908 return Platform_EOK;
909 }
911 /******************************************************************************
912 * platform_uart_init
913 ******************************************************************************/
914 Platform_STATUS platform_uart_init(void) {
916 UART_RET ret;
917 Platform_STATUS staus;
918 IFPRINT(platform_write("platform_uart_init called \n"));
920 ret = UartInit(uart_port_num);
921 if(ret != UART_RET_OK)
922 {
923 platform_errno = PLATFORM_ERRNO_DEV_FAIL;
924 return Platform_EFAIL;
925 }
927 staus = platform_uart_set_baudrate(115200);
929 return (staus);
930 }
932 /******************************************************************************
933 * platform_uart_set_params
934 *
935 * UART platform library supports three HW instances of UART controller but
936 * platform uart APIs does not provide choose UART port number.
937 * Set UART port number using this function and subsequent calls to platform
938 * uart APIs will use the uart port number set.
939 ******************************************************************************/
940 Platform_STATUS platform_uart_set_params(PLATFORM_UART_Params *params) {
942 IFPRINT(platform_write("platform_uart_set_params called \n"));
944 if(params->uart_port <= PLATFORM_UART_PORT_2)
945 {
946 uart_port_num = params->uart_port;
947 }
948 else
949 {
950 return Platform_EFAIL;
951 }
953 return Platform_EOK;
954 }
956 #endif
959 #if (PLATFORM_LED_IN)
961 uint8_t soc_led_pin_num [PLATFORM_SOC_LED_COUNT] = {SOC_LED0_GPIO, SOC_LED1_GPIO, IO_EXP_SOC_LED2, IO_EXP_SOC_LED3, IO_EXP_SOC_LED4};
962 uint8_t soc_led_gpio_port [2] = {GPIO_PORT_0, GPIO_PORT_1};
964 /******************************************************************************
965 * platform_user_led_ctrl
966 ******************************************************************************/
967 static Platform_STATUS platform_user_led_ctrl(uint32_t led_id, PLATFORM_LED_OP operation)
968 {
969 GPIO_RET gpioRet;
970 I2C_RET i2cRet;
971 Platform_STATUS status;
973 status = Platform_EOK;
975 IFPRINT(platform_write("platform_user_led_ctrl called \n"));
977 switch(led_id) {
978 case PLATFORM_SOC_LED0:
979 case PLATFORM_SOC_LED1:
980 if (operation == PLATFORM_LED_OFF) {
981 gpioRet = gpioClearOutput(soc_led_gpio_port[led_id],
982 soc_led_pin_num[led_id]);
983 }
984 else {
985 gpioRet = gpioSetOutput(soc_led_gpio_port[led_id],
986 soc_led_pin_num[led_id]);
987 }
989 if(gpioRet != GPIO_RET_OK)
990 {
991 IFPRINT(platform_write("platform_user_led_ctrl: Configuring LED %d Failed!\n", led_id));
992 platform_errno = PLATFORM_ERRNO_DEV_FAIL;
993 status = Platform_EFAIL;
994 }
996 break;
998 case PLATFORM_SOC_LED2:
999 case PLATFORM_SOC_LED3:
1000 case PLATFORM_SOC_LED4:
1001 if (operation == PLATFORM_LED_ON) {
1002 i2cRet = i2cIoExpanderWritePin(
1003 (I2cIoExpPins) soc_led_pin_num[led_id],
1004 I2C_IO_EXP_PIN_LOW); // LOW to LED ON
1005 }
1006 else {
1007 i2cRet = i2cIoExpanderWritePin(
1008 (I2cIoExpPins) soc_led_pin_num[led_id],
1009 I2C_IO_EXP_PIN_HIGH); // HIGH to LED OFF
1010 }
1012 if(i2cRet != I2C_RET_OK)
1013 {
1014 IFPRINT(platform_write("platform_user_led_ctrl: Configuring LED %d Failed!\n", led_id));
1015 platform_errno = PLATFORM_ERRNO_DEV_FAIL;
1016 status = Platform_EFAIL;
1017 }
1019 break;
1021 default:
1022 IFPRINT(platform_write("platform_user_led_ctrl: Invalid led_id %d\n", led_id));
1023 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
1024 return ( (Platform_STATUS) Platform_EUNSUPPORTED);
1025 }
1027 return (status);
1028 }
1030 /******************************************************************************
1031 * platform_led
1032 ******************************************************************************/
1033 Platform_STATUS platform_led(uint32_t led_id, PLATFORM_LED_OP operation, LED_CLASS_E led_class)
1034 {
1035 Platform_STATUS status;
1037 IFPRINT(platform_write("platform_led(ledid=%d,operation=%d,class=%d) called \n", led_id, operation, led_class));
1039 switch (led_class) {
1040 case PLATFORM_USER_LED_CLASS:
1041 status = platform_user_led_ctrl(led_id, operation);
1042 break;
1043 case PLATFORM_SYSTEM_LED_CLASS:
1044 return ( (Platform_STATUS) Platform_EUNSUPPORTED);
1045 default:
1046 IFPRINT(platform_write("platform_led: Invalid led_class %d\n", led_class));
1047 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
1048 return ( (Platform_STATUS) Platform_EUNSUPPORTED);
1049 }
1051 return (status);
1052 }
1053 #endif
1055 /******************************************************************************
1056 * platform_delay
1057 ******************************************************************************/
1058 #if (PLATFORM_DELAY_IN)
1059 Platform_STATUS platform_delay(uint32_t usecs)
1060 {
1061 int32_t delayCount = (int32_t) usecs * platform_mcb.frequency;
1062 int32_t start_val = (int32_t) CSL_chipReadTSCL();
1064 while (((int32_t)CSL_chipReadTSCL() - start_val) < delayCount);
1066 return Platform_EOK;
1067 }
1069 void platform_delaycycles(uint32_t cycles)
1070 {
1071 uint32_t start_val = CSL_chipReadTSCL();
1073 while ((CSL_chipReadTSCL() - start_val) < cycles);
1075 return;
1076 }
1077 #endif
1079 /******************************************************************************
1080 * platform_memory_test
1081 ******************************************************************************/
1082 #if (PLATFORM_EXTMEMTEST_IN)
1083 static inline int32_t platform_memory_test (uint32_t start_address, uint32_t end_address)
1084 {
1085 uint32_t index, value;
1087 /* Write a pattern */
1088 for (index = start_address; index < end_address; index += 4) {
1089 *(volatile uint32_t *) index = (uint32_t)index;
1090 }
1092 /* Read and check the pattern */
1093 for (index = start_address; index < end_address; index += 4) {
1095 value = *(uint32_t *) index;
1097 if (value != index) {
1098 IFPRINT(platform_write("platform_memory_test: Failed at address index = 0x%x value = 0x%x *(index) = 0x%x\n",
1099 index, value, *(volatile uint32_t *) index));
1100 platform_errno = index;
1101 return (Platform_EFAIL);
1102 }
1103 }
1105 /* Write a pattern for complementary values */
1106 for (index = start_address; index < end_address; index += 4) {
1107 *(volatile uint32_t *) index = (uint32_t)~index;
1108 }
1110 /* Read and check the pattern */
1111 for (index = start_address; index < end_address; index += 4) {
1113 value = *(uint32_t *) index;
1115 if (value != ~index) {
1116 IFPRINT(platform_write("platform_memory_test: Failed at address index = 0x%x value = 0x%x *(index) = 0x%x\n",
1117 index, value, *(volatile uint32_t *) index));
1118 platform_errno = index;
1119 return (Platform_EFAIL);
1120 }
1121 }
1123 return Platform_EOK;
1124 }
1125 #endif
1127 /******************************************************************************
1128 * platform_external_memory_test
1129 ******************************************************************************/
1130 #if (PLATFORM_EXTMEMTEST_IN)
1131 Platform_STATUS platform_external_memory_test(uint32_t start_address, uint32_t end_address)
1132 {
1133 IFPRINT(platform_write("platform_external_memory_test(start=0x%x,end=0x%x) called \n", start_address, end_address));
1135 if((start_address == 0) && (end_address == 0)) {
1136 start_address = PLATFORM_DDR3_SDRAM_START;
1137 end_address = PLATFORM_DDR3_SDRAM_END - 1;
1138 }
1140 if ((start_address < PLATFORM_DDR3_SDRAM_START) ||
1141 (end_address > PLATFORM_DDR3_SDRAM_END) ||
1142 (start_address >= end_address)){
1143 IFPRINT(platform_write("platform_external_memory_test: Start address (0x%08x) or end address (0x%08x)\n",
1144 start_address, end_address));
1145 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
1146 return ((Platform_STATUS) Platform_EINVALID);
1147 }
1149 return platform_memory_test(start_address, end_address);
1150 }
1151 #endif
1153 /******************************************************************************
1154 * platform_write - Printf or echo to UART or both
1155 ******************************************************************************/
1156 #if (PLATFORM_WRITE_IN)
1157 WRITE_info platform_write_configure (WRITE_info wtype) {
1158 WRITE_info original;
1159 IFPRINT(platform_write("platform_write_configure(write_type=%d) called \n", wtype));
1160 original = write_type;
1161 write_type = wtype;
1162 return original;
1163 }
1165 void platform_write(const char *fmt, ... )
1166 {
1167 va_list arg_ptr;
1168 uint32_t i, length;
1170 /* Initial platform_write to temporary buffer.. at least try some sort of sanity check so we don't write all over
1171 * memory if the print is too large.
1172 */
1173 if (strlen(fmt) > MAX_WRITE_LEN) {platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT; return;}
1175 va_start( arg_ptr, fmt );
1176 length = vsprintf( (char *)write_buffer, fmt, arg_ptr );
1177 va_end( arg_ptr );
1179 if ((write_type == PLATFORM_WRITE_PRINTF) || (write_type == PLATFORM_WRITE_ALL)) {
1180 printf( "%s", write_buffer );
1181 fflush(stdout);
1182 }
1184 if ((write_type == PLATFORM_WRITE_UART) || (write_type == PLATFORM_WRITE_ALL)) {
1185 /* Log to console port (routine only sends a byte at a time) */
1186 for (i=0; i < length; i++) {
1187 if (write_buffer[i] == '\n') {
1188 UartWriteData(PLATFORM_UART_DBG_PORT, (uint8_t)0x0D);
1189 UartWriteData(PLATFORM_UART_DBG_PORT, (uint8_t)0x0A);
1190 }
1191 else {
1192 UartWriteData(PLATFORM_UART_DBG_PORT, (uint8_t)write_buffer[i]);
1193 }
1194 }
1195 }
1197 return;
1198 }
1199 #endif
1201 /******************************************************************************
1202 * platform_read_configure - Connfigures the input source for platform_read
1203 ******************************************************************************/
1204 #if (PLATFORM_READ_IN)
1205 READ_info platform_read_configure (READ_info rdype) {
1206 READ_info original;
1208 IFPRINT(platform_write("platform_read_configure(read_type=%d) called \n", rdype));
1210 original = read_type;
1211 read_type = rdype;
1213 return original;
1214 }
1216 /******************************************************************************
1217 * platform_read - scanf or read from UART
1218 ******************************************************************************/
1219 uint32_t platform_read(uint8_t *data, uint32_t length)
1220 {
1221 Platform_STATUS status = Platform_EOK;
1222 uint32_t count = 0;
1224 if (read_type == PLATFORM_READ_UART)
1225 {
1226 /* Wait till an input is provided */
1227 do
1228 {
1229 status = platform_uart_read(data, PLATFORM_READ_DELAY);
1230 if(status == Platform_EOK)
1231 {
1232 /* Check if user pressed enter */
1233 if((*data == 0xD) || (*data == 0x1B))
1234 {
1235 *data = '\0';
1236 break;
1237 }
1239 platform_uart_write(*data);
1241 count++;
1242 data++;
1243 }
1245 } while ((status != Platform_EOK) || (count != length));
1246 }
1248 if (read_type == PLATFORM_READ_SCANF)
1249 {
1250 count = scanf("%s", data);
1251 }
1253 return (count);
1254 }
1255 #endif /* #if (PLATFORM_READ_IN) */
1257 /******************************************************************************
1258 * platform_device_open
1259 ******************************************************************************/
1260 #if (PLATFORM_NAND_IN) || (PLATFORM_NOR_IN) || (PLATFORM_I2C_EEPROM_IN) || (PLATFORM_MMCHS_IN) || (PLATFORM_QSPI_FLASH_IN)
1261 PLATFORM_DEVICE_info *platform_device_open(uint32_t deviceid, uint32_t flags ) {
1263 PLATFORM_DEVICE_info *p_info;
1265 IFPRINT(platform_write("platform_device_open(deviceid=0x%x,flags=0x%x) called \n", deviceid, flags));
1267 #if (PLATFORM_NAND_IN)
1269 if (deviceid == PLATFORM_DEVID_MT29F2G16ABAFA) {
1270 /* Disable Write protect in NAND */
1271 if (nandInit() != SUCCESS) {
1272 IFPRINT(platform_write("platform_dveice_open: Initialization failed.\n"));
1273 return NULL;
1274 }
1276 /* Store the open flags */
1277 gDeviceNand.flags = flags;
1279 /* Set the device to point to its bad block list */
1280 gDeviceNand.bblist = (uint8_t *)&gDeviceNandBBlist;
1282 p_info = &gDeviceNand;
1284 if (NandGetDetails(p_info) != SUCCESS) {
1285 IFPRINT(platform_write("platform_device_open: Unable to read device information.\n"));
1286 platform_errno = PLATFORM_ERRNO_BADFLASHDEV;
1287 return NULL;
1288 }
1290 p_info->handle = deviceid;
1292 return p_info;
1293 }
1295 #endif
1297 #if (PLATFORM_NOR_IN)
1299 NOR_STATUS nor_status;
1301 if (deviceid == PLATFORM_DEVID_NORN25Q128A13ESF40F) {
1303 /* Disable Write protect in NOR */
1304 nor_status = nor_init();
1305 if (nor_status != NOR_EOK) {
1306 IFPRINT(platform_write("platform_device_open: Initialization failed.\n"));
1307 return NULL;
1308 }
1310 /* Store the open flags */
1311 gDeviceNor.flags = flags;
1313 p_info = &gDeviceNor;
1315 if (nor_get_details(p_info) != SUCCESS) {
1316 IFPRINT(platform_write("platform_device_open: Unable to read device information.\n"));
1317 platform_errno = PLATFORM_ERRNO_BADFLASHDEV;
1318 return NULL;
1319 }
1321 p_info->handle = deviceid;
1323 return p_info;
1324 }
1325 #endif
1327 #if (PLATFORM_I2C_EEPROM_IN)
1328 uint8_t i2cportnumber=0;
1329 if (deviceid == PLATFORM_DEVID_EEPROM50) {
1330 /* Store the open flags */
1331 gDeviceEeprom0.flags = flags;
1333 evmI2CInit(i2cportnumber);
1334 p_info = &gDeviceEeprom0;
1335 p_info->handle = deviceid;
1336 return p_info;
1337 }
1339 if (deviceid == PLATFORM_DEVID_EEPROM51) {
1340 /* Store the open flags */
1341 gDeviceEeprom1.flags = flags;
1342 evmI2CInit(i2cportnumber);
1343 p_info = &gDeviceEeprom1;
1344 p_info->handle = deviceid;
1345 return p_info;
1346 }
1348 #endif
1350 #if (PLATFORM_MMCHS_IN)
1352 if (deviceid == PLATFORM_DEVID_EMMC) {
1353 mmcInfo = mmchsInit(MMCHS_MMC_INST);
1354 if (mmcInfo == NULL) {
1355 IFPRINT(platform_write("platform_device_open: Initialization failed.\n"));
1356 return NULL;
1357 }
1359 /* Store the open flags */
1360 gDeviceEmmc.flags = flags;
1361 mmcCard = mmcInfo->card;
1363 p_info = &gDeviceEmmc;
1364 p_info->handle = deviceid;
1366 if (mmcCard->cardType == MMCHS_MMC)
1367 {
1368 p_info->type = PLATFORM_DEVICE_EMMC;
1369 p_info->manufacturer_id = mmcCard->cid.manfid;
1370 p_info->device_id = mmcCard->cid.oemid;
1371 p_info->width = mmcCard->busWidth;
1372 p_info->block_count = mmcCard->nBlks;
1373 p_info->page_size = mmcCard->blkLen;
1374 }
1375 return p_info;
1376 }
1378 if (deviceid == PLATFORM_DEVID_SD) {
1379 sdInfo = mmchsInit(MMCHS_SD_CARD_INST);
1380 if (sdInfo == NULL) {
1381 IFPRINT(platform_write("platform_device_open: Initialization failed.\n"));
1382 return NULL;
1383 }
1385 /* Store the open flags */
1386 gDeviceSd.flags = flags;
1387 sdCard = sdInfo->card;
1389 p_info = &gDeviceSd;
1390 p_info->handle = deviceid;
1392 if (sdCard->cardType == MMCHS_SD_CARD)
1393 {
1394 p_info->type = PLATFORM_DEVICE_SD;
1395 p_info->manufacturer_id = sdCard->cid.manfid;
1396 p_info->device_id = sdCard->cid.oemid;
1397 p_info->width = sdCard->busWidth;
1398 p_info->block_count = sdCard->nBlks;
1399 p_info->page_size = sdCard->blkLen;
1401 }
1402 return p_info;
1403 }
1405 #endif
1407 #if (PLATFORM_QSPI_FLASH_IN)
1408 QSPI_STATUS qspiStatus;
1410 if (deviceid == PLATFORM_DEVID_QSPIFLASH_S25FL512S) {
1412 /* Initialize the qspi flash */
1413 qspiStatus = qspiFlashInit(&gDeviceQspiFlash);
1414 if (qspiStatus != QSPI_SUCCESS) {
1415 IFPRINT(platform_write("platform_device_open: QSPI Flash Initialization failed.\n"));
1416 return NULL;
1417 }
1419 /* Store the open flags */
1420 gDeviceQspiFlash.flags = flags;
1422 p_info = &gDeviceQspiFlash;
1424 if (qspiFlashReadId(p_info) != SUCCESS) {
1425 IFPRINT(platform_write("platform_device_open: Unable to read device information.\n"));
1426 platform_errno = PLATFORM_ERRNO_BADFLASHDEV;
1427 return NULL;
1428 }
1430 p_info->handle = deviceid;
1432 return p_info;
1433 }
1434 #endif
1436 platform_errno = PLATFORM_ERRNO_UNSUPPORTED;
1438 return NULL;
1439 }
1440 #endif
1442 /******************************************************************************
1443 * platform_flash_close
1444 ******************************************************************************/
1445 #if (PLATFORM_NAND_IN) || (PLATFORM_NOR_IN) || (PLATFORM_I2C_EEPROM_IN) || (PLATFORM_MMCHS_IN) || (PLATFORM_QSPI_FLASH_IN)
1446 Platform_STATUS platform_device_close(PLATFORM_DEVHANDLE deviceid) {
1448 IFPRINT(platform_write("platform_device_close(handle=0x%x) called \n", deviceid));
1450 #if (PLATFORM_NAND_IN)
1451 if (deviceid == PLATFORM_DEVID_MT29F2G16ABAFA) {
1452 gDeviceNand.handle = 0;
1453 return Platform_EOK;
1454 }
1455 #endif
1457 #if (PLATFORM_NOR_IN)
1458 if (deviceid == PLATFORM_DEVID_NORN25Q128A13ESF40F) {
1459 gDeviceNor.handle = 0;
1460 return Platform_EOK;
1461 }
1462 #endif
1464 #if (PLATFORM_I2C_EEPROM_IN)
1466 if (deviceid == PLATFORM_DEVID_EEPROM50) {
1467 gDeviceEeprom0.handle = 0;
1468 return Platform_EOK;
1469 }
1471 if (deviceid == PLATFORM_DEVID_EEPROM51) {
1472 gDeviceEeprom1.handle = 0;
1473 return Platform_EOK;
1474 }
1476 #endif
1478 #if (PLATFORM_MMCHS_IN)
1480 if (deviceid == PLATFORM_DEVID_EMMC) {
1481 gDeviceEmmc.handle = 0;
1482 return Platform_EOK;
1483 }
1485 if (deviceid == PLATFORM_DEVID_SD) {
1486 gDeviceSd.handle = 0;
1487 return Platform_EOK;
1488 }
1490 #endif
1492 #if (PLATFORM_QSPI_FLASH_IN)
1493 if (deviceid == PLATFORM_DEVID_QSPIFLASH_S25FL512S) {
1494 gDeviceQspiFlash.handle = 0;
1495 return Platform_EOK;
1496 }
1497 #endif
1499 return Platform_EUNSUPPORTED;
1500 }
1501 #endif
1503 /******************************************************************************
1504 * platform_flash_read_spare_data
1505 ******************************************************************************/
1506 #if (PLATFORM_NAND_IN) || (PLATFORM_NOR_IN) || (PLATFORM_I2C_EEPROM_IN)
1507 Platform_STATUS platform_device_read_spare_data(PLATFORM_DEVHANDLE deviceid,
1508 uint32_t block_number,
1509 uint32_t page_number,
1510 uint8_t *buf) {
1512 IFPRINT(platform_write("platform_device_read_spare_data(handle=0x%x, block=%d, page=%d,buf=0x%x) called \n",
1513 deviceid, block_number, page_number, buf));
1515 if (buf == NULL) {
1516 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
1517 return Platform_EINVALID;
1518 }
1520 #if (PLATFORM_NAND_IN)
1521 if (deviceid == PLATFORM_DEVID_MT29F2G16ABAFA) {
1522 if ((block_number >= gDeviceNand.block_count) || (page_number >= gDeviceNand.page_count))
1523 {
1524 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
1525 return Platform_EINVALID;
1526 }
1528 if (NandReadSpareArea (&gDeviceNand, block_number, page_number, buf) != SUCCESS) {
1529 return ( (Platform_STATUS) Platform_EFAIL);
1530 }
1531 return Platform_EOK;
1532 }
1533 #endif
1535 return Platform_EUNSUPPORTED;
1537 }
1538 #endif
1540 /******************************************************************************
1541 * platform_device_mark_block_bad
1542 ******************************************************************************/
1543 #if PLATFORM_NAND_WRITE_IN
1544 Platform_STATUS platform_device_mark_block_bad(PLATFORM_DEVHANDLE deviceid,
1545 uint32_t block_number) {
1547 uint8_t *buf = NULL;
1548 PLATFORM_DEVICE_info *p_device;
1550 if (deviceid == PLATFORM_DEVID_MT29F2G16ABAFA) {
1551 p_device = &gDeviceNand;
1552 }
1553 else {
1554 p_device = NULL;
1555 }
1557 if (p_device == NULL) {
1558 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
1559 return Platform_EINVALID;
1560 }
1562 buf = (uint8_t *) Osal_platformMalloc(platform_roundup(p_device->spare_size, PLATFORM_CACHE_LINE_SIZE),
1563 PLATFORM_CACHE_LINE_SIZE);
1565 platform_device_read_spare_data(deviceid, block_number, 0, buf);
1567 buf[p_device->bboffset] = 0xA5; /* BI indicator byte*/
1569 if ( block_number >= gDeviceNand.block_count ) {
1570 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
1571 Osal_platformFree(buf, platform_roundup(p_device->spare_size, PLATFORM_CACHE_LINE_SIZE));
1572 return Platform_EINVALID;
1573 }
1575 if (NandWriteSpareArea (p_device, block_number, 0, buf) != SUCCESS) {
1576 Osal_platformFree(buf, platform_roundup(p_device->spare_size, PLATFORM_CACHE_LINE_SIZE));
1577 return ( (Platform_STATUS) Platform_EFAIL);
1578 }
1580 Osal_platformFree(buf, platform_roundup(p_device->spare_size, PLATFORM_CACHE_LINE_SIZE));
1582 return Platform_EOK;
1584 }
1585 #endif
1587 /******************************************************************************
1588 * platform_flash_write_spare_data
1589 ******************************************************************************/
1590 #if (PLATFORM_NAND_IN) || (PLATFORM_NOR_IN) || (PLATFORM_I2C_EEPROM_IN)
1591 Platform_STATUS platform_device_write_spare_data(PLATFORM_DEVHANDLE deviceid,
1592 uint32_t block_number,
1593 uint32_t page_number,
1594 uint8_t *buf) {
1596 IFPRINT(platform_write("platform_device_write_spare_data(handle=0x%x, block=%d, page=%d,buf=0x%x) called \n",
1597 deviceid, block_number, page_number, buf));
1599 if (buf == NULL) {
1600 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
1601 return Platform_EINVALID;
1602 }
1604 #if (PLATFORM_NAND_WRITE_IN)
1605 if (deviceid == PLATFORM_DEVID_MT29F2G16ABAFA) {
1606 if ((block_number >= gDeviceNand.block_count) || (page_number >= gDeviceNand.page_count))
1607 {
1608 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
1609 return Platform_EINVALID;
1610 }
1612 if (NandWriteSpareArea (&gDeviceNand, block_number, page_number, buf) != SUCCESS) {
1613 return ( (Platform_STATUS) Platform_EFAIL);
1614 }
1615 return Platform_EOK;
1616 }
1617 #endif
1619 return Platform_EUNSUPPORTED;
1621 }
1622 #endif
1624 /******************************************************************************
1625 * platform_device_read
1626 ******************************************************************************/
1627 #if (PLATFORM_NAND_IN) || (PLATFORM_NOR_IN) || (PLATFORM_I2C_EEPROM_IN) || (PLATFORM_MMCHS_IN) || (PLATFORM_QSPI_FLASH_IN)
1628 Platform_STATUS platform_device_read(PLATFORM_DEVHANDLE deviceid,
1629 uint32_t offset,
1630 uint8_t *buf,
1631 uint32_t len) {
1633 IFPRINT(platform_write("platform_device_read(handle=0x%x, offset=%d, len=%d, buf=0x%x) called \n",
1634 deviceid, offset, len, buf));
1636 if (buf == NULL || len == 0) {
1637 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
1638 return Platform_EINVALID;
1639 }
1641 #if (PLATFORM_NAND_IN)
1642 if (deviceid == PLATFORM_DEVID_MT29F2G16ABAFA) {
1643 NAND_ADDR addr;
1644 uint32_t read_len = 0;
1646 addr.uiColumnAddr= 0;
1648 for (read_len = 0; read_len < len; read_len += gDeviceNand.page_size)
1649 {
1650 if (platform_offset_to_blocknpage(deviceid, offset+read_len, &addr.uiBlockAddr, &addr.uiPageAddr) != Platform_EOK) {
1651 return Platform_EUNSUPPORTED;
1652 }
1654 if (readNandPage (&gDeviceNand, addr, &buf[read_len]) != SUCCESS) {
1655 return ( (Platform_STATUS) Platform_EFAIL);
1656 }
1657 }
1659 return Platform_EOK;
1660 }
1661 #endif
1663 #if (PLATFORM_NOR_IN)
1664 if (deviceid == PLATFORM_DEVID_NORN25Q128A13ESF40F) {
1665 if (nor_read (&gDeviceNor, offset, len, buf) != SUCCESS) {
1666 return ( (Platform_STATUS) Platform_EFAIL);
1667 }
1668 return Platform_EOK;
1669 }
1670 #endif
1672 #if (PLATFORM_I2C_EEPROM_IN)
1673 uint8_t i2cportnumber=0;
1674 if (deviceid == PLATFORM_DEVID_EEPROM50) {
1675 if (i2cEepromRead (offset, len, buf, 0x50,i2cportnumber) != I2C_RET_OK) {
1676 IFPRINT(platform_write("platform_eeprom_read: EEPROM read failed\n"));
1677 return ( (Platform_STATUS) Platform_EFAIL);
1678 }
1679 return Platform_EOK;
1680 }
1682 if (deviceid == PLATFORM_DEVID_EEPROM51) {
1683 if (i2cEepromRead (offset, len, buf, 0x51,i2cportnumber) != I2C_RET_OK) {
1684 IFPRINT(platform_write("platform_eeprom_read: EEPROM read failed\n"));
1685 return ( (Platform_STATUS) Platform_EFAIL);
1686 }
1687 return Platform_EOK;
1688 }
1690 #endif
1692 #if (PLATFORM_MMCHS_IN)
1694 if (deviceid == PLATFORM_DEVID_EMMC){
1695 //TODO: Need to add support for reading data which is not multiple of block length
1696 if (mmchsBlockRead (mmcCard, offset, (len/MMCHS_BLK_LEN), buf) != SUCCESS) {
1697 return ( (Platform_STATUS) Platform_EFAIL);
1698 }
1700 return Platform_EOK;
1701 }
1703 if (deviceid == PLATFORM_DEVID_SD) {
1704 //TODO: Need to add support for reading data which is not multiple of block length
1705 if (mmchsBlockRead (sdCard, offset, (len/MMCHS_BLK_LEN), buf) != SUCCESS) {
1706 return ( (Platform_STATUS) Platform_EFAIL);
1707 }
1709 return Platform_EOK;
1710 }
1712 #endif
1714 #if (PLATFORM_QSPI_FLASH_IN)
1715 if (deviceid == PLATFORM_DEVID_QSPIFLASH_S25FL512S) {
1716 if (qspiFlashRead (&gDeviceQspiFlash, offset, len, buf, qspiReadMode)
1717 != SUCCESS) {
1718 return ( (Platform_STATUS) Platform_EFAIL);
1719 }
1720 return Platform_EOK;
1721 }
1722 #endif
1724 return Platform_EUNSUPPORTED;
1726 }
1727 #endif
1729 /******************************************************************************
1730 * Computes a block and page based on an offset
1731 ******************************************************************************/
1732 #if (PLATFORM_NAND_IN) || (PLATFORM_NOR_IN) || (PLATFORM_QSPI_FLASH_IN)
1734 Platform_STATUS platform_offset_to_blocknpage(PLATFORM_DEVHANDLE deviceid,
1735 uint32_t offset,
1736 uint32_t *block,
1737 uint32_t *page) {
1738 uint32_t leftover;
1739 uint32_t block_size;
1740 uint32_t block_count, page_size, page_count;
1742 IFPRINT(platform_write("platform_offset_to_blocknpage(handle=0x%x, offset=%d) called \n",
1743 deviceid, offset));
1745 if (deviceid == PLATFORM_DEVID_MT29F2G16ABAFA) {
1746 block_count = gDeviceNand.block_count;
1747 page_size = gDeviceNand.page_size;
1748 page_count = gDeviceNand.page_count;
1749 }
1750 else if (deviceid == PLATFORM_DEVID_NORN25Q128A13ESF40F) {
1751 block_count = gDeviceNor.block_count;
1752 page_size = gDeviceNor.page_size;
1753 page_count = gDeviceNor.page_count;
1754 }
1755 else if (deviceid == PLATFORM_DEVID_QSPIFLASH_S25FL512S) {
1756 block_count = gDeviceQspiFlash.block_count;
1757 page_size = gDeviceQspiFlash.page_size;
1758 page_count = gDeviceQspiFlash.page_count;
1759 }
1760 else {
1761 return Platform_EUNSUPPORTED;
1762 }
1764 block_size = (page_count * page_size);
1766 *block = offset / block_size;
1767 leftover = offset % block_size;
1768 *page = leftover / page_size;
1769 if (leftover % page_size) {
1770 /* All writes must be page aligned for now */
1771 return Platform_EUNSUPPORTED;
1772 }
1773 if (*block > block_count) {
1774 return Platform_EINVALID;
1775 }
1776 if (*page > page_count) {
1777 return Platform_EINVALID;
1778 }
1780 IFPRINT(platform_write("platform_offset_to_blocknpage: offset = %d block = %d page = %d \n", offset, *block, *page));
1782 return Platform_EOK;
1784 }
1786 #endif
1788 /******************************************************************************
1789 * Computes a block and page based on an offset
1790 ******************************************************************************/
1791 #if (PLATFORM_NAND_IN) || (PLATFORM_NOR_IN) || (PLATFORM_QSPI_FLASH_IN)
1793 Platform_STATUS platform_blocknpage_to_offset(PLATFORM_DEVHANDLE deviceid,
1794 uint32_t *offset,
1795 uint32_t block,
1796 uint32_t page) {
1798 uint32_t block_count, page_size, page_count;
1800 IFPRINT(platform_write("platform_blocknpage_to_offset(handle=0x%x, block=%d, page=%d) called \n",
1801 deviceid, block, page));
1803 if (deviceid == PLATFORM_DEVID_MT29F2G16ABAFA) {
1804 block_count = gDeviceNand.block_count;
1805 page_size = gDeviceNand.page_size;
1806 page_count = gDeviceNand.page_count;
1807 }
1808 else if (deviceid == PLATFORM_DEVID_NORN25Q128A13ESF40F) {
1809 block_count = gDeviceNor.block_count;
1810 page_size = gDeviceNor.page_size;
1811 page_count = gDeviceNor.page_count;
1812 }
1813 else if (deviceid == PLATFORM_DEVID_QSPIFLASH_S25FL512S) {
1814 block_count = gDeviceQspiFlash.block_count;
1815 page_size = gDeviceQspiFlash.page_size;
1816 page_count = gDeviceQspiFlash.page_count;
1817 }
1818 else {
1819 return Platform_EUNSUPPORTED;
1820 }
1822 if (block >block_count || page > page_count) {
1823 return Platform_EINVALID;
1824 }
1826 *offset = (block * (page_count * page_size)) + (page * page_size);
1828 IFPRINT(platform_write("platform_blocknpage_to_offset: offset = %d block = %d page = %d \n", *offset, block, page));
1830 return Platform_EOK;
1831 }
1833 #endif
1835 /******************************************************************************
1836 * platform_device_write
1837 ******************************************************************************/
1839 #if (PLATFORM_NAND_IN) || (PLATFORM_NOR_IN) || (PLATFORM_I2C_EEPROM_IN) || (PLATFORM_MMCHS_IN) || (PLATFORM_QSPI_FLASH_IN)
1841 Platform_STATUS platform_device_write(PLATFORM_DEVHANDLE deviceid,
1842 uint32_t offset,
1843 uint8_t *buf,
1844 uint32_t len) {
1846 IFPRINT(platform_write("platform_device_write(handle=0x%x, offset=%d, len=%d, buf=0x%x) called \n",
1847 deviceid, offset, len, buf));
1849 /* Check general args */
1850 if (buf == NULL || len == 0) {
1851 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
1852 return Platform_EINVALID;
1853 }
1855 #if (PLATFORM_NAND_WRITE_IN || PLATFORM_NOR_WRITE_IN || PLATFORM_QSPI_FLASH_IN)
1856 if ((deviceid == PLATFORM_DEVID_MT29F2G16ABAFA) ||
1857 (deviceid == PLATFORM_DEVID_NORN25Q128A13ESF40F) ||
1858 (deviceid == PLATFORM_DEVID_QSPIFLASH_S25FL512S))
1859 {
1860 NAND_ADDR addr;
1861 NAND_ADDR StartAddress;
1862 Platform_STATUS retval = Platform_EOK;
1863 PLATFORM_DEVICE_info *p_device;
1864 int32_t block, page;
1865 int32_t bufindex;
1866 int32_t index;
1867 int32_t useable_page_size;
1868 int32_t block_size;
1869 uint32_t noraddress;
1870 uint32_t qspiflashaddress;
1871 uint8_t *pBlock = NULL;
1872 uint8_t *pPage = NULL;
1874 if (deviceid == PLATFORM_DEVID_MT29F2G16ABAFA) {
1875 p_device = &gDeviceNand;
1876 }
1877 else if (deviceid == PLATFORM_DEVID_NORN25Q128A13ESF40F) {
1878 p_device = &gDeviceNor;
1879 }
1880 else {
1881 p_device = &gDeviceQspiFlash;
1882 }
1885 #if (PLATFORM_QSPI_FLASH_WRITE_IN)
1886 if (deviceid == PLATFORM_DEVID_QSPIFLASH_S25FL512S) {
1887 if (qspiFlashWrite (&gDeviceQspiFlash, offset, len, buf, qspiWriteMode)
1888 != SUCCESS) {
1889 return ( (Platform_STATUS) Platform_EFAIL);
1890 }
1891 return Platform_EOK;
1892 }
1893 #endif
1894 /* Calculate the usable page area.. Its the size of the page */
1895 useable_page_size = p_device->page_size;
1897 /* allocate a buffer big enough to hold one block (must include spare area) */
1898 block_size = p_device->page_size * p_device->page_count;
1900 pBlock = Osal_platformMalloc(platform_roundup(block_size, PLATFORM_CACHE_LINE_SIZE),
1901 PLATFORM_CACHE_LINE_SIZE);
1903 if ( !pBlock ){
1904 platform_errno = PLATFORM_ERRNO_OOM;
1905 retval = Platform_EFAIL;
1906 goto FLASHWRITE_ERROR;
1907 }
1909 /* allocate a buffer big enough to hold one page of the flash */
1910 pPage = Osal_platformMalloc(platform_roundup(p_device->page_size, PLATFORM_CACHE_LINE_SIZE),
1911 PLATFORM_CACHE_LINE_SIZE);
1913 if ( !pPage ){
1914 platform_errno = PLATFORM_ERRNO_OOM;
1915 retval = Platform_EFAIL;
1916 goto FLASHWRITE_ERROR;
1917 }
1919 /* Get starting address */
1920 addr.uiColumnAddr= 0;
1921 if (platform_offset_to_blocknpage(deviceid, offset, &addr.uiBlockAddr, &addr.uiPageAddr) != Platform_EOK) {
1922 retval = Platform_EUNSUPPORTED;
1923 goto FLASHWRITE_ERROR;
1924 }
1926 /*
1927 * We can now write the file to flash. The basic algorithm is this:
1928 * Read the bad block table
1929 * While we have data to write do
1930 * skip block if bad
1931 * read the block (page level)
1932 * erase the block (block level)
1933 * write the block (page level)
1934 */
1936 /* set index to start of the data to write */
1937 bufindex = 0;
1939 /* Initialize the starting block we will be using */
1940 block = addr.uiBlockAddr;
1941 StartAddress.uiBlockAddr = addr.uiBlockAddr;
1942 StartAddress.uiPageAddr = addr.uiPageAddr;
1943 StartAddress.uiColumnAddr = 0;
1945 while (len) {
1947 /* Skip bad blocks */
1948 if (p_device->bblist) {
1949 if (p_device->bblist[block] == 0x00) {
1950 /* Block is bad, skip to next good one */
1951 IFPRINT(platform_write( "platform_device_write: Skipping bad block %d. \n", block));
1952 block++;
1953 while (p_device->bblist[block] == 0x00) {
1954 block++;
1955 }
1956 }
1957 }
1959 /* Make sure we didn't exceed number of blocks */
1960 if (block > p_device->block_count) {
1961 IFPRINT(platform_write( "Due to bad blocks, you have run out of room in the Flash for this image. \n"));
1962 platform_errno = PLATFORM_ERRNO_NOFREEBLOCKS;
1963 retval = Platform_EFAIL;
1964 goto FLASHWRITE_ERROR;
1965 }
1967 IFPRINT(platform_write( "platform_device_write: Writing block %d \n", block));
1969 /*
1970 * Read the block..
1971 * may need to preserve what we don't actually write out.
1972 */
1973 for (page=0, index = 0; page < p_device->page_count;
1974 page++, index +=p_device->page_size) {
1975 addr.uiBlockAddr = block;
1976 addr.uiPageAddr = page;
1977 addr.uiColumnAddr= 0;
1978 if (p_device->handle == PLATFORM_DEVID_MT29F2G16ABAFA) {
1979 #if (PLATFORM_NAND_WRITE_IN)
1981 if (readNandPage (&gDeviceNand, addr, (uint8_t *)(pBlock + index)) != SUCCESS) {
1982 retval = Platform_EFAIL;
1983 IFPRINT(platform_write( "platform_device_write: Error reading block %d page %d. \n", block, page));
1984 goto FLASHWRITE_ERROR;
1985 }
1986 #endif
1987 }
1988 else if (p_device->handle == PLATFORM_DEVID_NORN25Q128A13ESF40F) {
1989 #if (PLATFORM_NOR_WRITE_IN)
1990 (void) platform_blocknpage_to_offset(p_device->handle, &noraddress, addr.uiBlockAddr, addr.uiPageAddr);
1991 if (nor_read (&gDeviceNor, noraddress, p_device->page_size, (uint8_t *)(pBlock + index)) != SUCCESS) {
1992 retval = Platform_EFAIL;
1993 platform_write( "platform_device_write: Error reading block %d page %d. \n", block, page);
1994 goto FLASHWRITE_ERROR;
1995 }
1996 #endif
1997 }
1998 else {
1999 #if (PLATFORM_QSPI_FLASH_WRITE_IN)
2000 (void) platform_blocknpage_to_offset(p_device->handle, &qspiflashaddress, addr.uiBlockAddr, addr.uiPageAddr);
2001 if (qspiFlashRead (&gDeviceQspiFlash, qspiflashaddress, p_device->page_size, (uint8_t *)(pBlock + index), qspiReadMode) != SUCCESS) {
2002 retval = Platform_EFAIL;
2003 platform_write( "platform_device_write: Error reading block %d page %d. \n", block, page);
2004 goto FLASHWRITE_ERROR;
2005 }
2006 #endif
2007 }
2008 }
2010 /* erase the block */
2011 if (p_device->handle == PLATFORM_DEVID_MT29F2G16ABAFA) {
2012 #if (PLATFORM_NAND_WRITE_IN)
2013 if (nandFlashBlockErase (&gDeviceNand, block) != SUCCESS) {
2014 IFPRINT(platform_write( "platform_device_write: Could not erase block %d. \n", block));
2015 retval = Platform_EFAIL;
2016 goto FLASHWRITE_ERROR;
2017 }
2018 #endif
2019 }
2020 else if (p_device->handle == PLATFORM_DEVID_NORN25Q128A13ESF40F) {
2021 #if (PLATFORM_NOR_WRITE_IN)
2022 if (nor_erase (&gDeviceNor, block) != SUCCESS) {
2023 IFPRINT(platform_write( "platform_device_write: Could not erase block %d. \n", block));
2024 retval = Platform_EFAIL;
2025 goto FLASHWRITE_ERROR;
2026 }
2027 #endif
2028 }
2029 else {
2030 #if (PLATFORM_QSPI_FLASH_WRITE_IN)
2031 if (qspiFlashErase (&gDeviceQspiFlash, block) != SUCCESS) {
2032 IFPRINT(platform_write( "platform_device_write: Could not erase block %d. \n", block));
2033 retval = Platform_EFAIL;
2034 goto FLASHWRITE_ERROR;
2035 }
2036 #endif
2037 }
2039 /*
2040 * Write the block...
2041 * - Get one page of data and write out or
2042 * - if we run out of data re-write what we previously read.
2043 */
2044 for (page = 0, index = 0; page < p_device->page_count;
2045 page++, index +=p_device->page_size) {
2047 IFPRINT(platform_write( "platform_device_write: Writing to block %d page %d \n", block, page));
2049 if (block == StartAddress.uiBlockAddr && page < StartAddress.uiPageAddr) {
2050 /* if we are within the first block we are writing, then we may have a start
2051 * page other than page 0, so re-write what was there.
2052 */
2053 memcpy(pPage, (pBlock+bufindex), useable_page_size);
2054 }
2055 else
2056 if (len == 0) {
2057 /* we wrote all the data, so write back any pages we pre-read that are left */
2058 memcpy(pPage, (pBlock+bufindex), useable_page_size);
2059 }
2060 else
2061 if (len < useable_page_size) {
2062 /* we have less than a full page of data */
2063 memset(pPage, 0xFF, p_device->page_size);
2064 memcpy(pPage, (buf+bufindex), len);
2065 len = 0;
2066 }
2067 else {
2068 /* get a full page of data */
2069 memcpy(pPage, (buf+bufindex), useable_page_size);
2070 len -= useable_page_size;
2071 bufindex += useable_page_size;
2072 }
2073 /* write the page */
2074 addr.uiBlockAddr = block;
2075 addr.uiPageAddr = page;
2076 addr.uiColumnAddr= 0;
2077 if (p_device->handle == PLATFORM_DEVID_MT29F2G16ABAFA) {
2078 #if (PLATFORM_NAND_WRITE_IN)
2079 if (writeNandPage (&gDeviceNand, addr, pPage) != SUCCESS) {
2080 IFPRINT(platform_write( "platform_device_write: Error writing block %d page %d. \n", block, page));
2081 retval = Platform_EFAIL;
2082 goto FLASHWRITE_ERROR;
2083 }
2084 #endif
2085 }
2086 else if (p_device->handle == PLATFORM_DEVID_NORN25Q128A13ESF40F) {
2087 #if (PLATFORM_NOR_WRITE_IN)
2088 (void) platform_blocknpage_to_offset(p_device->handle, &noraddress, addr.uiBlockAddr, addr.uiPageAddr);
2089 if (nor_write(&gDeviceNor, noraddress, p_device->page_size, pPage) != SUCCESS) {
2090 IFPRINT(platform_write( "platform_device_write: Error writing block %d page %d. \n", block, page));
2091 retval = Platform_EFAIL;
2092 goto FLASHWRITE_ERROR;
2093 }
2094 #endif
2095 }
2096 else {
2097 #if (PLATFORM_QSPI_FLASH_WRITE_IN)
2098 (void) platform_blocknpage_to_offset(p_device->handle, &qspiflashaddress, addr.uiBlockAddr, addr.uiPageAddr);
2099 if (qspiFlashWrite(&gDeviceQspiFlash, qspiflashaddress, p_device->page_size, pPage, qspiWriteMode) != SUCCESS) {
2100 IFPRINT(platform_write( "platform_device_write: Error writing block %d page %d. \n", block, page));
2101 retval = Platform_EFAIL;
2102 goto FLASHWRITE_ERROR;
2103 }
2104 #endif
2105 }
2106 }
2108 /* increment to the next block */
2109 block++;
2110 }
2112 FLASHWRITE_ERROR:
2114 if (pBlock) {
2115 Osal_platformFree( pBlock, platform_roundup(block_size, PLATFORM_CACHE_LINE_SIZE) );
2116 }
2118 if (pPage) {
2119 Osal_platformFree( pPage, platform_roundup(p_device->page_size, PLATFORM_CACHE_LINE_SIZE));
2120 }
2122 return retval;
2123 }
2124 #endif
2126 #if (PLATFORM_I2C_EEPROM_IN) && (PLATFORM_I2C_EEPROM_WRITE_IN)
2127 uint8_t i2cportnumber=0;
2128 if (deviceid == PLATFORM_DEVID_EEPROM50) {
2129 if (i2cEepromWriteByteAddr(offset, 0x50, buf, len, I2C_RELEASE_BUS,i2cportnumber) != I2C_RET_OK) {
2130 IFPRINT(platform_write("platform_device_write: EEPROM write for address 0x%x failed\n", 0x50));
2131 platform_errno = PLATFORM_ERRNO_EEPROM;
2132 return ( (Platform_STATUS) Platform_EFAIL);
2133 }
2134 return Platform_EOK;
2135 }
2137 if (deviceid == PLATFORM_DEVID_EEPROM51) {
2138 if (i2cEepromWriteByteAddr(offset, 0x51, buf, len, I2C_RELEASE_BUS,i2cportnumber) != I2C_RET_OK) {
2139 IFPRINT(platform_write("platform_device_write: EEPROM write for address 0x%x failed\n", 0x51));
2140 platform_errno = PLATFORM_ERRNO_EEPROM;
2141 return ( (Platform_STATUS) Platform_EFAIL);
2142 }
2143 return Platform_EOK;
2144 }
2146 #endif
2148 #if (PLATFORM_MMCHS_IN)
2150 if (deviceid == PLATFORM_DEVID_EMMC){
2151 //TODO: Need to add support for writing data which is not multiple of block length
2152 if (mmchsBlockWrite (mmcCard, offset, (len/MMCHS_BLK_LEN), buf) != SUCCESS) {
2153 return ( (Platform_STATUS) Platform_EFAIL);
2154 }
2156 return Platform_EOK;
2157 }
2159 if (deviceid == PLATFORM_DEVID_SD) {
2160 //TODO: Need to add support for writing data which is not multiple of block length
2161 if (mmchsBlockWrite (sdCard, offset, (len/MMCHS_BLK_LEN), buf) != SUCCESS) {
2162 return ( (Platform_STATUS) Platform_EFAIL);
2163 }
2165 return Platform_EOK;
2166 }
2168 #endif
2170 return Platform_EUNSUPPORTED;
2172 }
2173 #endif
2175 /******************************************************************************
2176 * platform_device_erase_block
2177 ******************************************************************************/
2178 #if (PLATFORM_NAND_IN) || (PLATFORM_NOR_IN) || (PLATFORM_I2C_EEPROM_IN)
2179 Platform_STATUS platform_device_erase_block(PLATFORM_DEVHANDLE deviceid,
2180 uint32_t block_number) {
2182 IFPRINT(platform_write("platform_device_erase_block(handle=0x%x, blockt=%d) called \n",
2183 deviceid, block_number));
2185 #if (PLATFORM_NAND_IN) && (PLATFORM_NAND_WRITE_IN)
2186 if (deviceid == PLATFORM_DEVID_MT29F2G16ABAFA) {
2187 if (block_number >= gDeviceNand.block_count) {
2188 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
2189 return Platform_EINVALID;
2190 }
2191 if (nandFlashBlockErase (&gDeviceNand, block_number) != SUCCESS) {
2192 return ( (Platform_STATUS) Platform_EFAIL);
2193 }
2194 return Platform_EOK;
2195 }
2196 #endif
2198 #if (PLATFORM_NOR_IN) && (PLATFORM_NOR_WRITE_IN)
2199 if (deviceid == PLATFORM_DEVID_NORN25Q128A13ESF40F) {
2200 if (block_number > gDeviceNor.block_count) {
2201 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
2202 return Platform_EINVALID;
2203 }
2204 if (nor_erase (&gDeviceNor, block_number) != SUCCESS) {
2205 return ( (Platform_STATUS) Platform_EFAIL);
2206 }
2207 return Platform_EOK;
2208 }
2209 #endif
2211 #if (PLATFORM_QSPI_FLASH_IN) && (PLATFORM_QSPI_FLASH_WRITE_IN)
2212 if (deviceid == PLATFORM_DEVID_QSPIFLASH_S25FL512S) {
2213 if (block_number > gDeviceQspiFlash.block_count) {
2214 platform_errno = PLATFORM_ERRNO_INVALID_ARGUMENT;
2215 return Platform_EINVALID;
2216 }
2217 if (qspiFlashErase (&gDeviceQspiFlash, block_number) != SUCCESS) {
2218 return ( (Platform_STATUS) Platform_EFAIL);
2219 }
2220 return Platform_EOK;
2221 }
2222 #endif
2224 return Platform_EUNSUPPORTED;
2225 }
2226 #endif
2228 /******************************************************************************
2229 * platform_device_print_info
2230 ******************************************************************************/
2231 #if (PLATFORM_MMCHS_IN)
2232 Platform_STATUS platform_device_print_info(PLATFORM_DEVHANDLE deviceid) {
2234 IFPRINT(platform_write("platform_device_print_info(handle=0x%x) called \n",
2235 deviceid));
2237 #if (PLATFORM_MMCHS_IN)
2238 if (deviceid == PLATFORM_DEVID_EMMC) {
2239 mmchsPrintCardInfo(mmcCard);
2240 return Platform_EOK;
2241 }
2242 else if (deviceid == PLATFORM_DEVID_SD) {
2243 mmchsPrintCardInfo(sdCard);
2244 return Platform_EOK;
2245 }
2247 return Platform_EUNSUPPORTED;
2249 #endif
2250 }
2251 #endif
2253 /******************************************************************************
2254 * platform_device_set_speed
2255 ******************************************************************************/
2256 #if (PLATFORM_MMCHS_IN)
2257 Platform_STATUS platform_device_set_speed(PLATFORM_DEVHANDLE deviceid,
2258 uint32_t speed) {
2260 mmchsCardInfo *cardInfo = NULL;
2262 IFPRINT(platform_write("platform_device_set_speed(handle=0x%x, speed=%d) called \n",
2263 deviceid, speed));
2265 #if (PLATFORM_MMCHS_IN)
2266 if (deviceid == PLATFORM_DEVID_EMMC) {
2267 cardInfo = mmcCard;
2268 }
2269 else if (deviceid == PLATFORM_DEVID_SD) {
2270 cardInfo = sdCard;
2271 }
2273 if(cardInfo != NULL)
2274 {
2275 if (mmchsSetTranSpeed(cardInfo,speed) != SUCCESS) {
2276 return ( (Platform_STATUS) Platform_EFAIL);
2277 }
2279 return Platform_EOK;
2280 }
2282 return Platform_EUNSUPPORTED;
2284 #endif
2285 }
2286 #endif
2288 /******************************************************************************
2289 * platform_device_set_bus_width
2290 ******************************************************************************/
2291 #if (PLATFORM_MMCHS_IN)
2292 Platform_STATUS platform_device_set_bus_width(PLATFORM_DEVHANDLE deviceid,
2293 PLATFORM_MMCSD_BUS_WIDTH width) {
2295 mmchsCardInfo *cardInfo = NULL;
2297 IFPRINT(platform_write("platform_device_set_bus_width(handle=0x%x, Bus width=%d) called \n",
2298 deviceid, width));
2300 #if (PLATFORM_MMCHS_IN)
2301 if (deviceid == PLATFORM_DEVID_EMMC) {
2302 cardInfo = mmcCard;
2303 }
2304 else if (deviceid == PLATFORM_DEVID_SD) {
2305 cardInfo = sdCard;
2306 }
2308 if(cardInfo != NULL)
2309 {
2310 switch(width)
2311 {
2312 case PLATFORM_MMCSD_BUS_1BIT:
2313 mmchsSetBusWidth(cardInfo, SD_BUS_WIDTH_1BIT);
2314 break;
2316 case PLATFORM_MMCSD_BUS_4BIT:
2317 mmchsSetBusWidth(cardInfo, SD_BUS_WIDTH_4BIT);
2318 mmchsPrintCSD(cardInfo);
2319 break;
2321 case PLATFORM_MMCSD_BUS_8BIT:
2322 mmchsSetBusWidth(cardInfo, SD_BUS_WIDTH_8BIT);
2323 mmchsPrintECSD(cardInfo);
2324 break;
2325 }
2327 return Platform_EOK;
2328 }
2330 return Platform_EUNSUPPORTED;
2332 #endif
2333 }
2334 #endif
2336 /******************************************************************************
2337 * platform_device_set_params
2338 ******************************************************************************/
2339 #if (PLATFORM_QSPI_FLASH_IN)
2340 Platform_STATUS platform_device_set_params(PLATFORM_DEVHANDLE deviceid, void *params)
2341 {
2342 Platform_STATUS status;
2343 PLATFORM_DEVICE_info *p_info;
2345 if(deviceid == PLATFORM_DEVID_QSPIFLASH_S25FL512S)
2346 {
2347 qspiReadMode = ((PLATFORM_QSPI_Params *)params)->read_mode;
2348 qspiWriteMode = ((PLATFORM_QSPI_Params *)params)->write_mode;
2349 p_info = &gDeviceQspiFlash;
2350 if (qspiReadMode == PLATFORM_QSPI_IO_MODE_QUAD)
2351 {
2352 status = qspiFlashQuadModeCtrl(p_info, 1);
2353 return status;
2354 }
2355 else
2356 {
2357 status = qspiFlashQuadModeCtrl(p_info, 0);
2358 return status;
2359 }
2360 }
2362 return (Platform_EOK);
2363 }
2364 #endif
2366 #if PLATFORM_CACHE_IN
2367 uint32_t Convert_CoreLocal2GlobalAddr (uint32_t addr)
2368 {
2369 uint32_t coreNum;
2371 /* Get the core number. */
2372 #ifdef SIMULATOR_SUPPORT
2373 coreNum = CSL_chipReadDNUM();
2374 #else
2375 coreNum = platform_get_coreid();
2376 #endif
2377 /* Check if the address is a valid Local L2 address to convert */
2378 if ( (addr >= (uint32_t) 0x800000) &&
2379 (addr < (uint32_t) 0x900000) ) {
2380 /* Compute the global address. */
2381 return ((1 << 28) | (coreNum << 24) | (addr & 0x00ffffff));
2382 }
2383 else {
2384 return (addr);
2385 }
2386 }
2387 #endif