OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [CORTEX_AT91SAM3U256_IAR/] [AT91Lib/] [peripherals/] [efc/] [efc.c] - Blame information for rev 580

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 580 jeremybenn
/* ----------------------------------------------------------------------------
2
 *         ATMEL Microcontroller Software Support
3
 * ----------------------------------------------------------------------------
4
 * Copyright (c) 2008, Atmel Corporation
5
 *
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions are met:
10
 *
11
 * - Redistributions of source code must retain the above copyright notice,
12
 * this list of conditions and the disclaimer below.
13
 *
14
 * Atmel's name may not be used to endorse or promote products derived from
15
 * this software without specific prior written permission.
16
 *
17
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * ----------------------------------------------------------------------------
28
 */
29
 
30
//------------------------------------------------------------------------------
31
//         Headers
32
//------------------------------------------------------------------------------
33
 
34
#include "efc.h"
35
 
36
#ifdef BOARD_FLASH_EFC
37
 
38
#include <utility/assert.h>
39
#include <utility/trace.h>
40
 
41
//------------------------------------------------------------------------------
42
//         Local definitions
43
//------------------------------------------------------------------------------
44
 
45
// Round a number to the nearest integral value (number must have been
46
// multiplied by 10, e.g. to round 10.3 enter 103).
47
#define ROUND(n)    ((((n) % 10) >= 5) ? (((n) / 10) + 1) : ((n) / 10))
48
 
49
// Returns the FMCN field value when manipulating lock bits, given MCK.
50
#if defined(at91sam7a3)
51
    #define FMCN_BITS(mck)      (ROUND((mck) / 1000000) << 16)
52
#else
53
    #define FMCN_BITS(mck)      (ROUND((mck) / 100000) << 16)
54
#endif
55
 
56
// Returns the FMCN field value when manipulating the rest of the flash.
57
#define FMCN_FLASH(mck)         ((((mck) / 2000000) * 3) << 16)
58
 
59
//------------------------------------------------------------------------------
60
//         Local functions
61
//------------------------------------------------------------------------------
62
 
63
/// Master clock frequency, used to infer the value of the FMCN field.
64
#ifdef MCK_VARIABLE
65
static unsigned int lMck = 0;
66
#else
67
static const unsigned int lMck = BOARD_MCK;
68
#endif
69
 
70
//------------------------------------------------------------------------------
71
//         Global functions
72
//------------------------------------------------------------------------------
73
 
74
//------------------------------------------------------------------------------
75
/// Sets the system master clock so the FMCN field of the EFC(s) can be
76
/// programmed properly.
77
/// \param mck  Master clock frequency in Hz.
78
//------------------------------------------------------------------------------
79
void EFC_SetMasterClock(unsigned int mck)
80
{
81
#ifdef MCK_VARIABLE
82
    lMck = mck;
83
#else
84
    ASSERT(mck == BOARD_MCK, "-F- EFC has not been configured to work at a freq. different from %dMHz", BOARD_MCK);
85
#endif
86
}
87
 
88
//------------------------------------------------------------------------------
89
/// Enables the given interrupt sources on an EFC peripheral.
90
/// \param pEfc  Pointer to an AT91S_EFC structure.
91
/// \param sources  Interrupt sources to enable.
92
//------------------------------------------------------------------------------
93
void EFC_EnableIt(AT91S_EFC *pEfc, unsigned int sources)
94
{
95
    SANITY_CHECK(pEfc);
96
    SANITY_CHECK((sources & ~0x0000000D) == 0);
97
 
98
    pEfc->EFC_FMR |= sources;
99
}
100
 
101
//------------------------------------------------------------------------------
102
/// Disables the given interrupt sources on an EFC peripheral.
103
/// \param pEfc  Pointer to an AT91S_EFC structure.
104
/// \param sources  Interrupt sources to disable.
105
//------------------------------------------------------------------------------
106
void EFC_DisableIt(AT91S_EFC *pEfc, unsigned int sources)
107
{
108
    SANITY_CHECK(pEfc);
109
    SANITY_CHECK((sources & ~(AT91C_MC_FRDY | AT91C_MC_LOCKE | AT91C_MC_PROGE)) == 0);
110
 
111
    pEfc->EFC_FMR &= ~sources;
112
}
113
 
114
//------------------------------------------------------------------------------
115
/// Enables or disable the "Erase before programming" feature of an EFC.
116
/// \param pEfc  Pointer to an AT91S_EFC structure.
117
/// \param enable  If 1, the feature is enabled; otherwise it is disabled.
118
//------------------------------------------------------------------------------
119
void EFC_SetEraseBeforeProgramming(AT91S_EFC *pEfc, unsigned char enable)
120
{
121
    SANITY_CHECK(pEfc);
122
 
123
    if (enable) {
124
 
125
        pEfc->EFC_FMR &= ~AT91C_MC_NEBP;
126
    }
127
    else {
128
 
129
        pEfc->EFC_FMR |= AT91C_MC_NEBP;
130
    }
131
}
132
 
133
//------------------------------------------------------------------------------
134
/// Translates the given address into EFC, page and offset values. The resulting
135
/// values are stored in the provided variables if they are not null.
136
/// \param address  Address to translate.
137
/// \param ppEfc  Pointer to target EFC peripheral.
138
/// \param pPage  First page accessed.
139
/// \param pOffset  Byte offset in first page.
140
//------------------------------------------------------------------------------
141
void EFC_TranslateAddress(
142
    unsigned int address,
143
    AT91S_EFC **ppEfc,
144
    unsigned short *pPage,
145
    unsigned short *pOffset)
146
{
147
    AT91S_EFC *pEfc;
148
    unsigned short page;
149
    unsigned short offset;
150
 
151
    SANITY_CHECK(address >= AT91C_IFLASH);
152
    SANITY_CHECK(address <= (AT91C_IFLASH + AT91C_IFLASH_SIZE));
153
 
154
#if defined(AT91C_BASE_EFC0)
155
    if (address >= (AT91C_IFLASH + AT91C_IFLASH_SIZE / 2)) {
156
 
157
        pEfc = AT91C_BASE_EFC1;
158
        page = (address - AT91C_IFLASH - AT91C_IFLASH_SIZE / 2) / AT91C_IFLASH_PAGE_SIZE;
159
        offset = (address - AT91C_IFLASH - AT91C_IFLASH_SIZE / 2) % AT91C_IFLASH_PAGE_SIZE;
160
    }
161
    else {
162
 
163
        pEfc = AT91C_BASE_EFC0;
164
        page = (address - AT91C_IFLASH) / AT91C_IFLASH_PAGE_SIZE;
165
        offset = (address - AT91C_IFLASH) % AT91C_IFLASH_PAGE_SIZE;
166
    }
167
#else
168
    pEfc = AT91C_BASE_EFC;
169
    page = (address - AT91C_IFLASH) / AT91C_IFLASH_PAGE_SIZE;
170
    offset = (address - AT91C_IFLASH) % AT91C_IFLASH_PAGE_SIZE;
171
#endif
172
    TRACE_DEBUG("Translated 0x%08X to EFC=0x%08X, page=%d and offset=%d\n\r",
173
              address, (unsigned int) pEfc, page, offset);
174
 
175
    // Store values
176
    if (ppEfc) {
177
 
178
        *ppEfc = pEfc;
179
    }
180
    if (pPage) {
181
 
182
        *pPage = page;
183
    }
184
    if (pOffset) {
185
 
186
        *pOffset = offset;
187
    }
188
}
189
 
190
//------------------------------------------------------------------------------
191
/// Computes the address of a flash access given the EFC, page and offset.
192
/// \param pEfc  Pointer to an AT91S_EFC structure.
193
/// \param page  Page number.
194
/// \param offset  Byte offset inside page.
195
/// \param pAddress  Computed address (optional).
196
//------------------------------------------------------------------------------
197
void EFC_ComputeAddress(
198
    AT91S_EFC *pEfc,
199
    unsigned short page,
200
    unsigned short offset,
201
    unsigned int *pAddress)
202
{
203
    unsigned int address;
204
 
205
    SANITY_CHECK(pEfc);
206
#if defined(AT91C_BASE_EFC1)
207
    SANITY_CHECK(page <= (AT91C_IFLASH_NB_OF_PAGES / 2));
208
#else
209
    SANITY_CHECK(page <= AT91C_IFLASH_NB_OF_PAGES);
210
#endif
211
    SANITY_CHECK(offset < AT91C_IFLASH_PAGE_SIZE);
212
 
213
    // Compute address
214
    address = AT91C_IFLASH + page * AT91C_IFLASH_PAGE_SIZE + offset;
215
#if defined(AT91C_BASE_EFC1)
216
    if (pEfc == AT91C_BASE_EFC1) {
217
 
218
        address += AT91C_IFLASH_SIZE / 2;
219
    }
220
#endif
221
 
222
    // Store result
223
    if (pAddress) {
224
 
225
        *pAddress = address;
226
    }
227
}
228
 
229
//------------------------------------------------------------------------------
230
/// Starts the executing the given command on an EFC. This function returns
231
/// as soon as the command is started. It does NOT set the FMCN field automatically.
232
/// \param pEfc  Pointer to an AT91S_EFC structure.
233
/// \param command  Command to execute.
234
/// \param argument  Command argument (should be 0 if not used).
235
//------------------------------------------------------------------------------
236
#if defined(flash)
237
   #ifdef __ICCARM__
238
__ramfunc
239
   #else
240
__attribute__ ((section (".ramfunc")))
241
   #endif
242
#endif
243
void EFC_StartCommand(
244
    AT91S_EFC *pEfc,
245
    unsigned char command,
246
    unsigned short argument)
247
{
248
    SANITY_CHECK(pEfc);
249
    ASSERT(lMck != 0, "-F- Master clock not set.\n\r");
250
 
251
    // Check command & argument
252
    switch (command) {
253
 
254
        case AT91C_MC_FCMD_PROG_AND_LOCK:
255
            ASSERT(0, "-F- Write and lock command cannot be carried out.\n\r");
256
            break;
257
 
258
        case AT91C_MC_FCMD_START_PROG:
259
        case AT91C_MC_FCMD_LOCK:
260
        case AT91C_MC_FCMD_UNLOCK:
261
            ASSERT(argument < AT91C_IFLASH_NB_OF_PAGES,
262
                   "-F- Maximum number of pages is %d (argument was %d)\n\r",
263
                   AT91C_IFLASH_NB_OF_PAGES,
264
                   argument);
265
            break;
266
 
267
#if (EFC_NUM_GPNVMS > 0)
268
        case AT91C_MC_FCMD_SET_GP_NVM:
269
        case AT91C_MC_FCMD_CLR_GP_NVM:
270
            ASSERT(argument < EFC_NUM_GPNVMS, "-F- A maximum of %d GPNVMs are available on the chip.\n\r", EFC_NUM_GPNVMS);
271
            break;
272
#endif
273
 
274
        case AT91C_MC_FCMD_ERASE_ALL:
275
 
276
#if !defined(EFC_NO_SECURITY_BIT)
277
        case AT91C_MC_FCMD_SET_SECURITY:
278
#endif
279
            ASSERT(argument == 0, "-F- Argument is meaningless for the given command\n\r");
280
            break;
281
 
282
        default: ASSERT(0, "-F- Unknown command %d\n\r", command);
283
    }
284
 
285
    // Set FMCN
286
    switch (command) {
287
 
288
        case AT91C_MC_FCMD_LOCK:
289
        case AT91C_MC_FCMD_UNLOCK:
290
#if (EFC_NUM_GPNVMS > 0)
291
        case AT91C_MC_FCMD_SET_GP_NVM:
292
        case AT91C_MC_FCMD_CLR_GP_NVM:
293
#endif
294
#if !defined(EFC_NO_SECURITY_BIT)
295
        case AT91C_MC_FCMD_SET_SECURITY:
296
#endif
297
            pEfc->EFC_FMR = (pEfc->EFC_FMR & ~AT91C_MC_FMCN) | FMCN_BITS(lMck);
298
            break;
299
 
300
        case AT91C_MC_FCMD_START_PROG:
301
        case AT91C_MC_FCMD_ERASE_ALL:
302
            pEfc->EFC_FMR = (pEfc->EFC_FMR & ~AT91C_MC_FMCN) | FMCN_FLASH(lMck);
303
            break;
304
    }
305
 
306
    // Start command
307
    ASSERT((pEfc->EFC_FSR & AT91C_MC_FRDY) != 0, "-F- Efc is not ready\n\r");
308
    pEfc->EFC_FCR = (0x5A << 24) | (argument << 8) | command;
309
}
310
 
311
//------------------------------------------------------------------------------
312
/// Performs the given command and wait until its completion (or an error).
313
/// Returns 0 if successful; otherwise returns an error code.
314
/// \param pEfc  Pointer to an AT91S_EFC structure.
315
/// \param command  Command to perform.
316
/// \param argument  Optional command argument.
317
//------------------------------------------------------------------------------
318
#if defined(flash)
319
   #ifdef __ICCARM__
320
__ramfunc
321
   #else
322
__attribute__ ((section (".ramfunc")))
323
   #endif
324
#endif
325
unsigned char EFC_PerformCommand(
326
    AT91S_EFC *pEfc,
327
    unsigned char command,
328
    unsigned short argument)
329
{
330
    unsigned int status;
331
 
332
    // Set FMCN
333
    switch (command) {
334
 
335
        case AT91C_MC_FCMD_LOCK:
336
        case AT91C_MC_FCMD_UNLOCK:
337
#if (EFC_NUM_GPNVMS > 0)
338
        case AT91C_MC_FCMD_SET_GP_NVM:
339
        case AT91C_MC_FCMD_CLR_GP_NVM:
340
#endif
341
#if !defined(EFC_NO_SECURITY_BIT)
342
        case AT91C_MC_FCMD_SET_SECURITY:
343
#endif
344
            pEfc->EFC_FMR = (pEfc->EFC_FMR & ~AT91C_MC_FMCN) | FMCN_BITS(lMck);
345
            break;
346
 
347
        case AT91C_MC_FCMD_START_PROG:
348
        case AT91C_MC_FCMD_ERASE_ALL:
349
            pEfc->EFC_FMR = (pEfc->EFC_FMR & ~AT91C_MC_FMCN) | FMCN_FLASH(lMck);
350
            break;
351
    }
352
 
353
#ifdef BOARD_FLASH_IAP_ADDRESS
354
    // Pointer on IAP function in ROM
355
    static void (*IAP_PerformCommand)(unsigned int, unsigned int);
356
    unsigned int index = 0;
357
#ifdef AT91C_BASE_EFC1
358
    if (pEfc == AT91C_BASE_EFC1) {
359
 
360
        index = 1;
361
    }
362
#endif
363
    IAP_PerformCommand = (void (*)(unsigned int, unsigned int)) *((unsigned int *) BOARD_FLASH_IAP_ADDRESS);
364
 
365
    // Check if IAP function is implemented (opcode in SWI != 'b' or 'ldr') */
366
    if ((((((unsigned long) IAP_PerformCommand >> 24) & 0xFF) != 0xEA) &&
367
        (((unsigned long) IAP_PerformCommand >> 24) & 0xFF) != 0xE5)) {
368
 
369
        IAP_PerformCommand(index, (0x5A << 24) | (argument << 8) | command);
370
        return (pEfc->EFC_FSR & (AT91C_MC_LOCKE | AT91C_MC_PROGE));
371
    }
372
#endif
373
 
374
    pEfc->EFC_FCR = (0x5A << 24) | (argument << 8) | command;
375
    do {
376
 
377
        status = pEfc->EFC_FSR;
378
    }
379
    while ((status & AT91C_MC_FRDY) == 0);
380
 
381
    return (status & (AT91C_MC_PROGE | AT91C_MC_LOCKE));
382
}
383
 
384
//------------------------------------------------------------------------------
385
/// Returns the current status of an EFC. Keep in mind that this function clears
386
/// the value of some status bits (LOCKE, PROGE).
387
/// \param pEfc  Pointer to an AT91S_EFC structure.
388
//------------------------------------------------------------------------------
389
unsigned int EFC_GetStatus(AT91S_EFC *pEfc)
390
{
391
    return pEfc->EFC_FSR;
392
}
393
 
394
#endif //#ifdef BOARD_FLASH_EFC
395
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.