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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [Common/] [drivers/] [Atmel/] [at91lib/] [peripherals/] [efc/] [efc.c] - Blame information for rev 608

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 608 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
#ifndef trace_LEVEL
31
    #define trace_LEVEL trace_INFO
32
#endif
33
 
34
//------------------------------------------------------------------------------
35
//         Headers
36
//------------------------------------------------------------------------------
37
 
38
#include "efc.h"
39
 
40
#ifdef BOARD_FLASH_EFC
41
 
42
#include <utility/assert.h>
43
#include <utility/trace.h>
44
 
45
//------------------------------------------------------------------------------
46
//         Local definitions
47
//------------------------------------------------------------------------------
48
 
49
// Round a number to the nearest integral value (number must have been
50
// multiplied by 10, e.g. to round 10.3 enter 103).
51
#define ROUND(n)    ((((n) % 10) >= 5) ? (((n) / 10) + 1) : ((n) / 10))
52
 
53
// Returns the FMCN field value when manipulating lock bits, given MCK.
54
#if defined(at91sam7a3)
55
    #define FMCN_BITS(mck)      (ROUND((mck) / 100000) << 16) // <- Not correct according to the datasheet but it works
56
#else
57
    #define FMCN_BITS(mck)      (ROUND((mck) / 100000) << 16)
58
#endif
59
 
60
// Returns the FMCN field value when manipulating the rest of the flash.
61
#define FMCN_FLASH(mck)         ((((mck) / 2000000) * 3) << 16)
62
 
63
//------------------------------------------------------------------------------
64
//         Local functions
65
//------------------------------------------------------------------------------
66
 
67
 
68
/// Master clock frequency, used to infer the value of the FMCN field.
69
static unsigned int lMck;
70
/// Calculated value of the FMCN field base on Master clock frequency.
71
static unsigned int lMckFMCN;
72
 
73
//------------------------------------------------------------------------------
74
//         Global functions
75
//------------------------------------------------------------------------------
76
 
77
//------------------------------------------------------------------------------
78
/// Sets the system master clock so the FMCN field of the EFC(s) can be
79
/// programmed properly.
80
/// \param mck  Master clock frequency in Hz.
81
//------------------------------------------------------------------------------
82
void EFC_SetMasterClock(unsigned int mck)
83
{
84
    lMck = mck;
85
    lMckFMCN = FMCN_BITS(lMck);
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_LOG(trace_DEBUG,
173
              "-D- Translated 0x%08X to EFC=0x%08X, page=%d and offset=%d\n\r",
174
              address, (unsigned int) pEfc, page, offset);
175
 
176
    // Store values
177
    if (ppEfc) {
178
 
179
        *ppEfc = pEfc;
180
    }
181
    if (pPage) {
182
 
183
        *pPage = page;
184
    }
185
    if (pOffset) {
186
 
187
        *pOffset = offset;
188
    }
189
}
190
 
191
//------------------------------------------------------------------------------
192
/// Computes the address of a flash access given the EFC, page and offset.
193
/// \param pEfc  Pointer to an AT91S_EFC structure.
194
/// \param page  Page number.
195
/// \param offset  Byte offset inside page.
196
/// \param pAddress  Computed address (optional).
197
//------------------------------------------------------------------------------
198
void EFC_ComputeAddress(
199
    AT91S_EFC *pEfc,
200
    unsigned short page,
201
    unsigned short offset,
202
    unsigned int *pAddress)
203
{
204
    unsigned int address;
205
 
206
    SANITY_CHECK(pEfc);
207
#if defined(AT91C_BASE_EFC1)
208
    SANITY_CHECK(page <= (AT91C_IFLASH_NB_OF_PAGES / 2));
209
#else
210
    SANITY_CHECK(page <= AT91C_IFLASH_NB_OF_PAGES);
211
#endif
212
    SANITY_CHECK(offset < AT91C_IFLASH_PAGE_SIZE);
213
 
214
    // Compute address
215
    address = AT91C_IFLASH + page * AT91C_IFLASH_PAGE_SIZE + offset;
216
#if defined(AT91C_BASE_EFC1)
217
    if (pEfc == AT91C_BASE_EFC1) {
218
 
219
        address += AT91C_IFLASH_SIZE / 2;
220
    }
221
#endif
222
 
223
    // Store result
224
    if (pAddress) {
225
 
226
        *pAddress = address;
227
    }
228
}
229
 
230
//------------------------------------------------------------------------------
231
/// Starts the executing the given command on an EFC. This function returns
232
/// as soon as the command is started. It does NOT set the FMCN field automatically.
233
/// \param pEfc  Pointer to an AT91S_EFC structure.
234
/// \param command  Command to execute.
235
/// \param argument  Command argument (should be 0 if not used).
236
//------------------------------------------------------------------------------
237
void EFC_StartCommand(
238
    AT91S_EFC *pEfc,
239
    unsigned char command,
240
    unsigned short argument)
241
{
242
    SANITY_CHECK(pEfc);
243
    ASSERT(lMck != 0, "-F- Master clock not set.\n\r");
244
 
245
    // Check command & argument
246
    switch (command) {
247
 
248
        case AT91C_MC_FCMD_PROG_AND_LOCK:
249
            ASSERT(0, "-F- Write and lock command cannot be carried out.\n\r");
250
            break;
251
 
252
        case AT91C_MC_FCMD_START_PROG:
253
        case AT91C_MC_FCMD_LOCK:
254
        case AT91C_MC_FCMD_UNLOCK:
255
            ASSERT(argument < AT91C_IFLASH_NB_OF_PAGES,
256
                   "-F- Maximum number of pages is %d (argument was %d)\n\r",
257
                   AT91C_IFLASH_NB_OF_PAGES,
258
                   argument);
259
            break;
260
 
261
#if (EFC_NUM_GPNVMS > 0)
262
        case AT91C_MC_FCMD_SET_GP_NVM:
263
        case AT91C_MC_FCMD_CLR_GP_NVM:
264
            ASSERT(argument < EFC_NUM_GPNVMS, "-F- A maximum of %d GPNVMs are available on the chip.\n\r", EFC_NUM_GPNVMS);
265
            break;
266
#endif
267
 
268
        case AT91C_MC_FCMD_ERASE_ALL:
269
 
270
#if !defined(EFC_NO_SECURITY_BIT)
271
        case AT91C_MC_FCMD_SET_SECURITY:
272
#endif
273
            ASSERT(argument == 0, "-F- Argument is meaningless for the given command\n\r");
274
            break;
275
 
276
        default: ASSERT(0, "-F- Unknown command %d\n\r", command);
277
    }
278
 
279
    // Set FMCN
280
    switch (command) {
281
 
282
        case AT91C_MC_FCMD_LOCK:
283
        case AT91C_MC_FCMD_UNLOCK:
284
#if (EFC_NUM_GPNVMS > 0)
285
        case AT91C_MC_FCMD_SET_GP_NVM:
286
        case AT91C_MC_FCMD_CLR_GP_NVM:
287
#endif
288
#if !defined(EFC_NO_SECURITY_BIT)
289
        case AT91C_MC_FCMD_SET_SECURITY:
290
#endif
291
            pEfc->EFC_FMR = (pEfc->EFC_FMR & ~AT91C_MC_FMCN) | lMckFMCN;
292
            break;
293
 
294
        case AT91C_MC_FCMD_START_PROG:
295
        case AT91C_MC_FCMD_ERASE_ALL:
296
            pEfc->EFC_FMR = (pEfc->EFC_FMR & ~AT91C_MC_FMCN) | lMckFMCN;
297
            break;
298
    }
299
 
300
    // Start command
301
    ASSERT((pEfc->EFC_FSR & AT91C_MC_FRDY) != 0, "-F- Efc is not ready\n\r");
302
    pEfc->EFC_FCR = (0x5A << 24) | (argument << 8) | command;
303
}
304
 
305
//------------------------------------------------------------------------------
306
/// Performs the given command and wait until its completion (or an error).
307
/// Returns 0 if successful; otherwise returns an error code.
308
/// \param pEfc  Pointer to an AT91S_EFC structure.
309
/// \param command  Command to perform.
310
/// \param argument  Optional command argument.
311
//------------------------------------------------------------------------------
312
#ifdef __ICCARM__
313
__ramfunc
314
#else
315
__attribute__ ((section (".ramfunc")))
316
#endif
317
unsigned char EFC_PerformCommand(
318
    AT91S_EFC *pEfc,
319
    unsigned char command,
320
    unsigned short argument)
321
{
322
    unsigned int status;
323
 
324
    // Set FMCN
325
    switch (command) {
326
 
327
        case AT91C_MC_FCMD_LOCK:
328
        case AT91C_MC_FCMD_UNLOCK:
329
#if (EFC_NUM_GPNVMS > 0)
330
        case AT91C_MC_FCMD_SET_GP_NVM:
331
        case AT91C_MC_FCMD_CLR_GP_NVM:
332
#endif
333
#if !defined(EFC_NO_SECURITY_BIT)
334
        case AT91C_MC_FCMD_SET_SECURITY:
335
#endif
336
            pEfc->EFC_FMR = (pEfc->EFC_FMR & ~AT91C_MC_FMCN) | lMckFMCN;
337
            break;
338
 
339
        case AT91C_MC_FCMD_START_PROG:
340
        case AT91C_MC_FCMD_ERASE_ALL:
341
            pEfc->EFC_FMR = (pEfc->EFC_FMR & ~AT91C_MC_FMCN) | lMckFMCN;
342
            break;
343
    }
344
 
345
#ifdef BOARD_FLASH_IAP_ADDRESS
346
    // Pointer on IAP function in ROM
347
    static void (*IAP_PerformCommand)(unsigned int, unsigned int);
348
    unsigned int index = 0;
349
#ifdef AT91C_BASE_EFC1
350
    if (pEfc == AT91C_BASE_EFC1) {
351
 
352
        index = 1;
353
    }
354
#endif
355
    IAP_PerformCommand = (void (*)(unsigned int, unsigned int)) *((unsigned int *) BOARD_FLASH_IAP_ADDRESS);
356
 
357
    // Check if IAP function is implemented (opcode in SWI != 'b' or 'ldr') */
358
    if ((((((unsigned long) IAP_PerformCommand >> 24) & 0xFF) != 0xEA) &&
359
        (((unsigned long) IAP_PerformCommand >> 24) & 0xFF) != 0xE5)) {
360
 
361
        IAP_PerformCommand(index, (0x5A << 24) | (argument << 8) | command);
362
        return (pEfc->EFC_FSR & (AT91C_MC_LOCKE | AT91C_MC_PROGE));
363
    }
364
#endif
365
 
366
    pEfc->EFC_FCR = (0x5A << 24) | (argument << 8) | command;
367
    do {
368
 
369
        status = pEfc->EFC_FSR;
370
    }
371
    while ((status & AT91C_MC_FRDY) == 0);
372
 
373
    return (status & (AT91C_MC_PROGE | AT91C_MC_LOCKE));
374
}
375
 
376
//------------------------------------------------------------------------------
377
/// Returns the current status of an EFC. Keep in mind that this function clears
378
/// the value of some status bits (LOCKE, PROGE).
379
/// \param pEfc  Pointer to an AT91S_EFC structure.
380
//------------------------------------------------------------------------------
381
unsigned int EFC_GetStatus(AT91S_EFC *pEfc)
382
{
383
    return pEfc->EFC_FSR;
384
}
385
 
386
#endif //#ifdef BOARD_FLASH_EFC
387
 

powered by: WebSVN 2.1.0

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