1 //*****************************************************************************
2 //
3 // am_hal_ttp.c
4 //! @file
5 //!
6 //! @brief Functions for handling the "two time program" interface.
7 //!
8 //
9 //*****************************************************************************
10
11 //*****************************************************************************
12 //
13 // Copyright (c) 2017, Ambiq Micro
14 // All rights reserved.
15 //
16 // Redistribution and use in source and binary forms, with or without
17 // modification, are permitted provided that the following conditions are met:
18 //
19 // 1. Redistributions of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
21 //
22 // 2. Redistributions in binary form must reproduce the above copyright
23 // notice, this list of conditions and the following disclaimer in the
24 // documentation and/or other materials provided with the distribution.
25 //
26 // 3. Neither the name of the copyright holder nor the names of its
27 // contributors may be used to endorse or promote products derived from this
28 // software without specific prior written permission.
29 //
30 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
34 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 // POSSIBILITY OF SUCH DAMAGE.
41 //
42 // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
43 //
44 //*****************************************************************************
45 #include "am_mcu_apollo.h"
46 #include "am_hal_ttp.h"
47
48 //*****************************************************************************
49 //
50 // Local constants
51 //
52 //*****************************************************************************
53 #define TTP_ADDR 0x50020000
54
55 //*****************************************************************************
56 //
57 // Local prototypes
58 //
59 //*****************************************************************************
60 #if !defined(__GNUC__)
61 void __breakpoint(int val);
62 #endif
63
64 //*****************************************************************************
65 //
66 // A function to verify that the TTP was saved and/or restored properly.
67 //
68 //*****************************************************************************
69 int
verifyTTPSaved(uint32_t * pSaveArray,int iNumWords)70 verifyTTPSaved(uint32_t *pSaveArray, int iNumWords)
71 {
72 int ix, iErrCnt = 0;
73 uint32_t *pDataSpace = (uint32_t*)TTP_ADDR;
74
75 for (ix = 0; ix<iNumWords; ix++)
76 {
77 if ( *pSaveArray != am_hal_flash_load_ui32((uint32_t)pDataSpace) )
78 {
79 iErrCnt++;
80 }
81 pSaveArray++;
82 pDataSpace++;
83 }
84
85 //
86 // Hopefully returning 0.
87 //
88 return iErrCnt;
89 }
90
91 //*****************************************************************************
92 //
93 //! @brief TTP unlock.
94 //!
95 //! @param ui32Keyval - The key value to unlock the interface.
96 //! @param pui8_1024Bytes - A pointer to a 1024 bytes array, used for
97 //! temporary data storage.
98 //! WARNING This area must be 32-bit aligned and at
99 //! least 1024 bytes long.
100 //!
101 //! This function is used to unlock the TTP ability.
102 //!
103 //! @return None.
104 //
105 //*****************************************************************************
106 int
am_hal_ttp_unlock(uint32_t ui32Keyval,uint8_t * pui8_1024Bytes)107 am_hal_ttp_unlock(uint32_t ui32Keyval, uint8_t *pui8_1024Bytes)
108 {
109 int iErrCnt = 0;
110 int ix, iRet;
111 int iNumWords = 1024 / 4;
112 uint32_t *pSaveArray, *pDataSpace;
113 int (*pTTPClear)(uint32_t, uint32_t) = (int (*)(uint32_t, uint32_t))0x080002EF;
114 int (*pTTPSet)(uint32_t, uint32_t, uint32_t*, uint32_t, uint32_t) =
115 (int (*)(uint32_t, uint32_t, uint32_t*, uint32_t, uint32_t))0x080006FF;
116
117 //
118 // Save off the data.
119 //
120 pSaveArray = (uint32_t*)pui8_1024Bytes;
121 pDataSpace = (uint32_t*)TTP_ADDR;
122 for (ix = 0; ix < iNumWords; ix++)
123 {
124 *pSaveArray = am_hal_flash_load_ui32((uint32_t)pDataSpace);
125 pSaveArray++;
126 pDataSpace++;
127 }
128
129 //
130 // Before proceeding, make sure that we captured the data correctly.
131 //
132 iErrCnt += verifyTTPSaved((uint32_t*)pui8_1024Bytes, iNumWords);
133 if ( iErrCnt )
134 {
135 return 0x10000001;
136 }
137
138 //
139 // Erase the TTP area.
140 //
141 iRet = (*pTTPClear)(0, ui32Keyval);
142 if ( iRet != 0 )
143 {
144 iErrCnt++;
145 return 0x10000002;
146 }
147
148 //
149 // The point of no return! The TTP space is successfully erased.
150 // Let's make sure.
151 //
152 pDataSpace = (uint32_t*)TTP_ADDR;
153 for (ix = 0; ix < iNumWords; ix++)
154 {
155 if ( am_hal_flash_load_ui32((uint32_t)pDataSpace) != 0xffffffff )
156 {
157 iErrCnt++;
158 }
159 pDataSpace++;
160 }
161
162 if ( iErrCnt )
163 {
164 return 0x10000003;
165 }
166
167 //
168 // Restore the TTP block from the saved data.
169 //
170 iRet = (*pTTPSet)(ui32Keyval, 0, (uint32_t*)pui8_1024Bytes, 0, iNumWords);
171 if ( iRet != 0 )
172 {
173 iErrCnt++;
174 return 0x10000004;
175 }
176
177 //
178 // Now, check the restored INFO data.
179 //
180 iRet = verifyTTPSaved((uint32_t*)pui8_1024Bytes, iNumWords);
181 if ( iRet )
182 {
183 iErrCnt++;
184 return 0x10000005;
185 }
186 else
187 {
188 //
189 // All good. The device was successfully recovered.
190 //
191 }
192
193
194 //
195 // Return with error count (hopefully 0).
196 //
197 return iErrCnt;
198 }
199
200 //*****************************************************************************
201 //
202 // End Doxygen group.
203 //! @}
204 //
205 //*****************************************************************************
206