summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/atsam/libraries/libchip/source/efc.c
blob: a5eeccb7524a40bc73bdba631bf6cf983e30bc60 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* ---------------------------------------------------------------------------- */
/*                  Atmel Microcontroller Software Support                      */
/*                       SAM Software Package License                           */
/* ---------------------------------------------------------------------------- */
/* Copyright (c) 2015, Atmel Corporation                                        */
/*                                                                              */
/* All rights reserved.                                                         */
/*                                                                              */
/* Redistribution and use in source and binary forms, with or without           */
/* modification, are permitted provided that the following condition is met:    */
/*                                                                              */
/* - Redistributions of source code must retain the above copyright notice,     */
/* this list of conditions and the disclaimer below.                            */
/*                                                                              */
/* Atmel's name may not be used to endorse or promote products derived from     */
/* this software without specific prior written permission.                     */
/*                                                                              */
/* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
/* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
/* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
/* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
/* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
/* ---------------------------------------------------------------------------- */

/** \addtogroup efc_module Working with EEFC
 *  \ingroup peripherals_module
 *
 * The EEFC driver provides the interface to configure and use the EEFC
 * peripheral.
 *
 * The user needs to set the number of wait states depending on the frequency
 * used.\n
 * Configure number of cycles for flash read/write operations in the FWS field
 * of EEFC_FMR.
 *
 * It offers a function to send flash command to EEFC and waits for the
 * flash to be ready.
 *
 * To send flash command, the user could do in either of following way:
 * <ul>
 * <li>Write a correct key, command and argument in EEFC_FCR. </li>
 * <li>Or, Use IAP (In Application Programming) function which is executed from
 * ROM directly, this allows flash programming to be done by code running in
 * flash.</li>
 * <li>Once the command is achieved, it can be detected even by polling
 * EEFC_FSR or interrupt.
 * </ul>
 *
 * The command argument could be a page number,GPNVM number or nothing, it
 * depends on the command itself. Some useful functions in this driver could
 * help user translate physical flash address into a page number and vice verse.
 *
 * For more accurate information, please look at the EEFC section of the
 * Datasheet.
 *
 * Related files :\n
 * \ref efc.c\n
 * \ref efc.h.\n
 */
/*@{*/
/*@}*/


/**
 * \file
 *
 * Implementation of Enhanced Embedded Flash Controller (EEFC).
 *
 */


/*----------------------------------------------------------------------------
 *        Headers
 *----------------------------------------------------------------------------*/
#include "chip.h"

#include <assert.h>


/*----------------------------------------------------------------------------
 *        Macro
 *----------------------------------------------------------------------------*/
#define EEFC_FCR_FCMD(value) ((EEFC_FCR_FCMD_Msk & ((value) << EEFC_FCR_FCMD_Pos)))

/*----------------------------------------------------------------------------
 *        Exported functions
 *----------------------------------------------------------------------------*/

extern void EFC_WriteFMR(Efc *efc, uint32_t dwFmr);

#ifdef __ICCARM__
	extern __ramfunc void EFC_WriteFMR(Efc *efc, uint32_t dwFmr)
#else
	__attribute__ ((section (".ramfunc")))
	extern void EFC_WriteFMR(Efc *efc, uint32_t dwFmr)
#endif
{
	efc->EEFC_FMR = dwFmr;
}

/**
 * \brief Enables the flash ready interrupt source on the EEFC peripheral.
 *
 * \param efc  Pointer to a Efc instance
 */
extern void EFC_EnableFrdyIt(Efc *efc)
{
	uint32_t dwFmr;

	dwFmr = efc->EEFC_FMR |= EEFC_FMR_FRDY;
	EFC_WriteFMR(efc, dwFmr);
}

/**
 * \brief Disables the flash ready interrupt source on the EEFC peripheral.
 *
 * \param efc  Pointer to a Efc instance
 */
extern void EFC_DisableFrdyIt(Efc *efc)
{
	uint32_t dwFmr;

	dwFmr = efc->EEFC_FMR & (~EEFC_FMR_FRDY);
	EFC_WriteFMR(efc, dwFmr);
}


/**
 * \brief Set read/write wait state on the EEFC peripheral.
 *
 * \param efc  Pointer to a Efc instance
 * \param cycles  the number of wait states in cycle.
 */
extern void EFC_SetWaitState(Efc *efc, uint8_t ucCycles)
{
	uint32_t dwFmr;

	dwFmr = efc->EEFC_FMR;
	dwFmr &= ~((uint32_t)EEFC_FMR_FWS_Msk);
	dwFmr |= EEFC_FMR_FWS(ucCycles);
	EFC_WriteFMR(efc, dwFmr);
}

/**
 * \brief Returns the current status of the EEFC.
 *
 * \note Keep in mind that this function clears the value of some status bits
 * (LOCKE, PROGE).
 *
 * \param efc  Pointer to a Efc instance
 */
extern uint32_t EFC_GetStatus(Efc *efc)
{
	return efc->EEFC_FSR;
}

/**
 * \brief Returns the result of the last executed command.
 *
 * \param efc  Pointer to a Efc instance
 */
extern uint32_t EFC_GetResult(Efc *efc)
{
	return efc->EEFC_FRR;
}

/**
 * \brief Translates the given address page and offset values.
 * \note The resulting values are stored in the provided variables if they are
 * not null.
 *
 * \param efc  Pointer to a Efc instance
 * \param address  Address to translate.
 * \param pPage  First page accessed.
 * \param pOffset  Byte offset in first page.
 */
extern void EFC_TranslateAddress(Efc **ppEfc, uint32_t dwAddress,
								  uint16_t *pwPage,
								  uint16_t *pwOffset)
{
	assert(dwAddress >= IFLASH_ADDR);
	assert(dwAddress <= (IFLASH_ADDR + IFLASH_SIZE));

	/* Store values */
	if (ppEfc)
		*ppEfc = EFC;

	if (pwPage)
		*pwPage = (dwAddress - IFLASH_ADDR) / IFLASH_PAGE_SIZE;

	if (pwOffset) {
		*pwOffset = (dwAddress - IFLASH_ADDR) % IFLASH_PAGE_SIZE;;
	}
}


/**
 * \brief Computes the address of a flash access given the page and offset.
 *
 * \param efc  Pointer to a Efc instance
 * \param page  Page number.
 * \param offset  Byte offset inside page.
 * \param pAddress  Computed address (optional).
 */
extern void EFC_ComputeAddress(Efc *efc, uint16_t wPage, uint16_t wOffset,
								uint32_t *pdwAddress)
{
	uint32_t dwAddress;

	/* Stop warning */
	efc = efc;

	assert(efc);
	assert(wPage <= IFLASH_NB_OF_PAGES);
	assert(wOffset < IFLASH_PAGE_SIZE);
	dwAddress = IFLASH_ADDR + wPage * IFLASH_PAGE_SIZE + wOffset;

	/* Store result */
	if (pdwAddress != NULL)
		*pdwAddress = dwAddress;
}

/**
 * \brief Performs the given command and wait until its completion (or an error).
 *
 * \param efc  Pointer to a Efc instance
 * \param command  Command to perform.
 * \param argument  Optional command argument.
 *
 * \return 0 if successful, otherwise returns an error code.
 */

extern uint32_t EFC_PerformCommand(Efc *efc, uint32_t dwCommand,
									uint32_t dwArgument, uint32_t dwUseIAP)
{
	if (dwUseIAP != 0) {
		/* Pointer on IAP function in ROM */
		static uint32_t (*IAP_PerformCommand)(uint32_t, uint32_t);

		IAP_PerformCommand = (uint32_t (*)(uint32_t, uint32_t))
							 * ((uint32_t *)CHIP_FLASH_IAP_ADDRESS);

		if (efc == EFC) {
			IAP_PerformCommand(0, EEFC_FCR_FKEY_PASSWD | EEFC_FCR_FARG(dwArgument)
								| EEFC_FCR_FCMD(dwCommand));
		}

		return (efc->EEFC_FSR & (EEFC_FSR_FLOCKE | EEFC_FSR_FCMDE | EEFC_FSR_FLERR));
	} else {
		uint32_t dwStatus;

		efc->EEFC_FCR = EEFC_FCR_FKEY_PASSWD | EEFC_FCR_FARG(dwArgument)
						| EEFC_FCR_FCMD(dwCommand);

		do {
			dwStatus = efc->EEFC_FSR;
		} while ((dwStatus & EEFC_FSR_FRDY) != EEFC_FSR_FRDY);

		return (dwStatus & (EEFC_FSR_FLOCKE | EEFC_FSR_FCMDE | EEFC_FSR_FLERR));
	}
}

/**
 * \brief Set flash access mode.
 *
 * \param dwMode - 0:128-bit, (1<<24):64-bit
 */
extern void EFC_SetFlashAccessMode(Efc *efc, uint32_t dwMode)
{
	uint32_t dwFmr;

	dwFmr = dwMode;
	EFC_WriteFMR(efc, dwFmr);
}