1 /**
2   ******************************************************************************
3   * @file    rtl8721dhp_ota_ram.c
4   * @author
5   * @version V1.0.0
6   * @date    2016-05-17
7   * @brief   This file provides firmware functions to manage the OTA functions.
8   ******************************************************************************
9   * @attention
10   *
11   * This module is a confidential and proprietary property of RealTek and
12   * possession or use of this module requires written permission of RealTek.
13   *
14   * Copyright(c) 2015, Realtek Semiconductor Corporation. All rights reserved.
15   ******************************************************************************
16   */
17 
18 #include "ameba_soc.h"
19 #include "strproc.h"
20 
21 /**
22   * @brief  Set OTA image index.
23   * @param  OTAIdx: This parameter can be  one of the following values:
24   *            @arg OTA_INDEX_1: select OTA1(low flash address) as image2
25   *            @arg OTA_INDEX_2: select OTA2(high flash address) as image2
26   * @retval status value:
27   *          - 1: set ok
28   *          - 0: set fail
29   * @note SelectOTA2: will set LSB  odd bits 0, 0xFFFFFFFE/0xFFFFFFF8... means select OTA2
30   * @note SelectOTA1: will set LSB  even bits 0,  0xFFFFFFFF/0xFFFFFFFC... means select OTA1
31   */
32 IMAGE2_RAM_TEXT_SECTION
OTA_Change(u32 OTAIdx)33 u32 OTA_Change(u32 OTAIdx)
34 {
35 	u32 BitIdx = 0;
36 	u32 ValidIMG2 = HAL_READ32(SPI_FLASH_BASE, FLASH_SYSTEM_DATA_ADDR + 4);
37 
38 	/* erase ValidIMG2 when all 32bits cleared */
39 	if (ValidIMG2 == 0x00000000) {
40 		FLASH_EreaseDwordsXIP(FLASH_SYSTEM_DATA_ADDR + 4, 1);
41 		ValidIMG2 = 0xFFFFFFFF;
42 	}
43 
44 	/* get first bit which is not cleared */
45 	for (BitIdx = 0; BitIdx <= 31; BitIdx++) {
46 		if ((ValidIMG2 & BIT(BitIdx)) != 0) {
47 			break;
48 		}
49 	}
50 
51 	DBG_8195A("BitIdx: %d \n", BitIdx);
52 
53 	/* even bits 0 currrent is OTA1 */
54 	if ((BitIdx % 2) == 0) {
55 		if (OTAIdx == OTA_INDEX_1) {
56 			DBG_8195A("currrent is OTA1, select OTA1 \n");
57 			return _TRUE;
58 		} else {
59 			DBG_8195A("currrent is OTA1, select OTA2 \n");
60 			ValidIMG2 &= ~BIT(BitIdx);
61 			FLASH_TxData12BXIP(FLASH_SYSTEM_DATA_ADDR + 4, 4, (u8*)&ValidIMG2);
62 		}
63 	} else { /* odd bits 0 currrent is OTA2 */
64 		if (OTAIdx == OTA_INDEX_1) {
65 			DBG_8195A("currrent is OTA2, select OTA1 \n");
66 			ValidIMG2 &= ~BIT(BitIdx);
67 			FLASH_TxData12BXIP(FLASH_SYSTEM_DATA_ADDR + 4, 4, (u8*)&ValidIMG2);
68 		} else {
69 			DBG_8195A("currrent is OTA2, select OTA2 \n");
70 			return _TRUE;
71 		}
72 	}
73 
74 	return _TRUE;
75 }
76 
77 /******************* (C) COPYRIGHT 2016 Realtek Semiconductor *****END OF FILE****/
78