1 /** mbed Microcontroller Library
2   ******************************************************************************
3   * @file    efuse_api.c
4   * @author
5   * @version V1.0.0
6   * @date    2016-08-01
7   * @brief   This file provides mbed API for EFUSE.
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) 2016, Realtek Semiconductor Corporation. All rights reserved.
15   ******************************************************************************
16   */
17 #include "ameba_soc.h"
18 #include "rom_aes.h"
19 //#ifdef CONFIG_EFUSE_EN
20 
21 /** @addtogroup AmebaD_Mbed_API
22   * @{
23   */
24 
25 /** @defgroup MBED_EFUSE
26  *  @brief      MBED_EFUSE driver modules.
27  *  @{
28  */
29 
30 /** @defgroup MBED_EFUSE_Exported_Functions MBED_EFUSE Exported Functions
31   * @{
32   */
33 
34 /**
35   * @brief  Get remaining efuse length
36   * @retval remaining efuse length
37   */
efuse_get_remaining_length(void)38 int  efuse_get_remaining_length(void)
39 {
40 	return EFUSE_RemainLength();
41 }
42 
43 
44 /**
45   * @brief  Read efuse content of specified user
46   * @param  data: Specified the address to save the readback data.
47   * @retval none
48   * @note read user MTP(0x160~0x17f).
49   */
efuse_mtp_read(uint8_t * data)50 void  efuse_mtp_read(uint8_t * data)
51 {
52 	u8 EfuseBuf[1024];
53 	u8 ret;
54 
55 	/*0xff will be efuse default value instead of 0x00. */
56 	_memset(data, 0xFF, 32);
57 
58 	ret = EFUSE_LMAP_READ(EfuseBuf);
59 
60 	if (ret == _FAIL) {
61 		DBG_8195A("EFUSE_LogicalMap_Read fail \n");
62 	}
63 
64 	_memcpy(data, EfuseBuf+0x160, 32);
65 
66 }
67 
68 /**
69   * @brief  Write user's content to efuse
70   * @param  data: Specified the data to be programmed.
71   * @param  len: Specifies the data length of programmed data.
72   * @retval status value:
73   *          - 1~32: Success
74   *          - 0 or -1: Failure
75   * @note read user MTP(0x160~0x17f).
76   */
efuse_mtp_write(uint8_t * data,uint8_t len)77 int  efuse_mtp_write(uint8_t *data, uint8_t len)
78 {
79 	u32 bResult;
80 
81 	if(len > 32)	{
82 		DBG_8195A("string length should be smaller than 32\n");
83 		return -1;
84 	}
85 
86 
87 	bResult = EFUSE_LMAP_WRITE(USER_SECTION << 3, len, data);
88 	if(!bResult){
89 		DBG_8195A("write fail \n");
90 		return -1;
91 	}else{
92    		return len;
93 	}
94 }
95 
96 
97 /**
98   * @brief  Read efuse OTP contant
99   * @param  address: Specifies the offset of the OTP.
100   * @param  len: Specifies the length of readback data.
101   * @param  buf: Specified the address to save the readback data.
102   * @retval status value:
103   *          - 0: Success
104   *          - -1: Failure
105   * @note read user OTP(0x130~0x14f).
106   */
efuse_otp_read(u8 address,u8 len,u8 * buf)107 int efuse_otp_read(u8 address, u8 len, u8 *buf)
108 {
109 	u8 content[32];	// the OTP max length is 32
110 	u8 index;
111 	u32 bResult;
112 	if((address+len) > 32) {
113 		return -1;
114 	}
115 	for (index = 0; index< 32; index++) {
116 		bResult = EFUSE_PMAP_READ8(0, 0x130 + index , content + index, L25EOUTVOLTAGE);
117 		if(!bResult){
118 			return -1;
119 		}
120 	}
121 
122 	_memcpy(buf, content+address, len);
123 	return 0;
124 
125 }
126 
127 
128 /**
129   * @brief  Write user's contant to OTP efuse
130   * @param  address: Specifies the offset of the programmed OTP.
131   * @param  len: Specifies the data length of programmed data.
132   * @param  buf: Specified the data to be programmed.
133   * @retval status value:
134   *          - 0: Success
135   *          - -1: Failure
136   * @note read user OTP(0x130~0x14f).
137   */
efuse_otp_write(u8 address,u8 len,u8 * buf)138 int efuse_otp_write(u8 address, u8 len, u8 *buf)
139 {
140 	u8 index;
141 	u32 bResult;
142 
143 	if((address+len) > 32) {
144 		return -1;
145 	}
146 
147 	for (index = 0; index < len; index++) {
148 		bResult = EFUSE_PMAP_WRITE8(0, address + 0x130 +index, buf[index], L25EOUTVOLTAGE);
149 		if(!bResult){
150 			return -1;
151 		}
152 	}
153 	return 0;
154 }
155 
156 /**
157   * @brief  check user's contant to OTP efuse
158   * @param  *buf: Specified the data to be programmed.
159   * @param len: Specifies the data length of programmed data.
160   * @retval   status: Success:0 or Failure: -1.
161   */
efuse_otp_chk(u8 len,u8 * buf)162 int efuse_otp_chk(u8 len, u8 *buf)
163 {
164 	/* To avoid gcc warnings */
165 	( void ) len;
166 	( void ) buf;
167 	DBG_8195A("Ameba-D not support efuse_otp_chk function!\n");
168 	return 0;
169 }
170 
171 /**
172   * @}
173   */
174 
175 /**
176   * @}
177   */
178 
179 /**
180   * @}
181   */
182 //#endif
183 /******************* (C) COPYRIGHT 2016 Realtek Semiconductor *****END OF FILE****/
184