summaryrefslogtreecommitdiff
path: root/memories/MEDNandFlash.c
blob: 300af36f0cbd183f32543100b6634efe6b7ba263 (plain)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support 
 * ----------------------------------------------------------------------------
 * Copyright (c) 2008, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are 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.
 * ----------------------------------------------------------------------------
 */

//------------------------------------------------------------------------------
//         Headers
//------------------------------------------------------------------------------

#include "MEDNandFlash.h"
#include "nandflash/NandCommon.h"
#include "nandflash/TranslatedNandFlash.h"
#include "nandflash/NandFlashModel.h"
#include <utility/trace.h>
#include <utility/math.h>
#include <utility/assert.h>

#include <string.h>

//------------------------------------------------------------------------------
//         Definitions
//------------------------------------------------------------------------------

/// Casts
#define MODEL(interface)        ((struct NandFlashModel *) interface)
#define TRANSLATED(interface)   ((struct TranslatedNandFlash *) interface)

//------------------------------------------------------------------------------
//         Internal variables
//------------------------------------------------------------------------------

static unsigned char pageWriteBuffer[NandCommon_MAXPAGEDATASIZE];
static signed short currentWriteBlock;
static signed short currentWritePage;

static unsigned char pageReadBuffer[NandCommon_MAXPAGEDATASIZE];
static signed short currentReadBlock;
static signed short currentReadPage;

//------------------------------------------------------------------------------
//         Internal functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
/// Writes the current page of data on the NandFlash.
/// Returns 0 if successful; otherwise returns 1.
/// \param tnf  Pointer to a TranslatedNandFlash instance.
//------------------------------------------------------------------------------
static unsigned char FlushCurrentPage(Media *media)
{
    // Make sure there is a page to flush
    if (currentWritePage == -1) {

        return 0;
    }

    TRACE_DEBUG("FlushCurrentPage(B#%d:P#%d)\n\r",
              currentWriteBlock, currentWritePage);

    // Write page
    if (TranslatedNandFlash_WritePage(TRANSLATED(media->interface),
                                      currentWriteBlock,
                                      currentWritePage,
                                      pageWriteBuffer,
                                      0)) {

        TRACE_ERROR("FlushCurrentPage: Failed to write page.\n\r");
        return 1;
    }

    // No current write page & block
    currentWriteBlock = -1;
    currentWritePage = -1;

    return 0;
}

//------------------------------------------------------------------------------
/// Writes data at an unaligned (page-wise) address and size. The address is
/// provided as the block & page number plus an offset. The data to write MUST
/// NOT span more than one page.
/// Returns 0 if the data has been written; 1 otherwise.
/// \param media  Pointer to a nandflash Media instance.
/// \param block  Number of the block to write.
/// \param page  Number of the page to write.
/// \param offset  Write offset.
/// \param buffer  Data buffer.
/// \param size  Number of bytes to write.
//------------------------------------------------------------------------------
static unsigned char UnalignedWritePage(
    Media *media,
    unsigned short block,
    unsigned short page,
    unsigned short offset,
    unsigned char *buffer,
    unsigned int size)
{
    unsigned char error;
    unsigned short pageDataSize = NandFlashModel_GetPageDataSize(MODEL(media->interface));
    unsigned char writePage = ((size + offset) == pageDataSize);

    TRACE_DEBUG("UnalignedWritePage(B%d:P%d@%d, %d)\n\r",
              block, page, offset, size);
    ASSERT((size + offset) <= pageDataSize,
           "UnalignedWrite: Write size and offset exceed page data size\n\r");

    // If size is 0, return immediately
    if (size == 0) {

        return 0;
    }

    // If this is not the current page, flush the previous one
    if ((currentWriteBlock != block) || (currentWritePage != page)) {

        // Flush and make page the new current page
        FlushCurrentPage(media);
        TRACE_DEBUG("Current write page: B#%d:P#%d\n\r", block, page);
        currentWriteBlock = block;
        currentWritePage = page;

        // Read existing page data in a temporary buffer if the page is not
        // entirely written
        if (size != pageDataSize) {

            error = TranslatedNandFlash_ReadPage(TRANSLATED(media->interface),
                                                 block,
                                                 page,
                                                 pageWriteBuffer,
                                                 0);
            if (error) {
    
                TRACE_ERROR(
                          "UnalignedWrite: Could not read existing page data\n\r");
                return 1;
            }
        }
    }

    // Copy data in temporary buffer
    memcpy(&(pageWriteBuffer[offset]), buffer, size);
    // Update read buffer if necessary
    if ((currentReadPage == currentWritePage)
        && (currentReadBlock == currentWriteBlock)) {

        TRACE_DEBUG("Updating current read buffer\n\r");
        memcpy(&(pageReadBuffer[offset]), buffer, size);
    }

    // Flush page if it is complete
    if (writePage) {

        FlushCurrentPage(media);
    }

    return 0;
}

//------------------------------------------------------------------------------
/// Writes a data buffer at the specified address on a NandFlash media. An
/// optional callback can be triggered after the transfer is completed.
/// Returns MED_STATUS_SUCCESS if the transfer has been started successfully;
/// otherwise returns MED_STATUS_ERROR.
/// \param media  Pointer to the NandFlash Media instance.
/// \param address  Address where the data shall be written.
/// \param data  Data buffer.
/// \param length  Number of bytes to write.
/// \param callback  Optional callback to call when the write is finished.
/// \param argument  Optional argument to the callback function.
//------------------------------------------------------------------------------
static unsigned char MEDNandFlash_Write(
    Media *media,
    unsigned int address,
    void *data,
    unsigned int length,
    MediaCallback callback,
    void *argument)
{
    unsigned short pageDataSize =
                NandFlashModel_GetPageDataSize(MODEL(media->interface));
    unsigned short blockSize =
                NandFlashModel_GetBlockSizeInPages(MODEL(media->interface));
    unsigned short block, page, offset;
    unsigned int writeSize;
    unsigned char *buffer = (unsigned char *) data;
    unsigned int remainingLength;
    unsigned char status;

    TRACE_INFO("MEDNandFlash_Write(0x%08X, %d)\n\r", address, length);

    // Translate access
    if (NandFlashModel_TranslateAccess(MODEL(media->interface),
                                       address,
                                       length,
                                       &block,
                                       &page,
                                       &offset)) {

        TRACE_ERROR("MEDNandFlash_Write: Could not start write.\n\r");
        return MED_STATUS_ERROR;
    }

    TRACE_DEBUG("MEDNandFlash_Write(B#%d:P#%d@%d, %d)\n\r",
              block, page, offset, length);

    // Write pages
    remainingLength = length;
    status = MED_STATUS_SUCCESS;
    while ((status == MED_STATUS_SUCCESS) && (remainingLength > 0)) {

        // Write one page
        writeSize = min(pageDataSize-offset, remainingLength);
        if (UnalignedWritePage(media, block, page, offset, buffer, writeSize)) {

            TRACE_ERROR("MEDNandFlash_Write: Failed to write page\n\r");
            status = MED_STATUS_ERROR;
        }
        else {

            // Update addresses
            remainingLength -= writeSize;
            buffer += writeSize;
            offset = 0;
            page++;
            if (page == blockSize) {
    
                page = 0;
                block++;
            }
        }
    }

    // Trigger callback
    if (callback) {

        callback(argument, status, length - remainingLength, remainingLength);
    }

    // Reset flush timer
    //AT91C_BASE_NANDFLUSHTIMER->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;

    return MED_STATUS_SUCCESS;
}

//------------------------------------------------------------------------------
/// Reads data at an unaligned address and/or size. The address is provided as
/// the block & page numbers plus an offset.
/// Returns 0 if the data has been read; otherwise returns 1.
/// \param media  Pointer to a nandflash Media instance.
/// \param block  Number of the block to read.
/// \param page  Number of the page to read.
/// \param offset  Read offset.
/// \param buffer  Buffer for storing data.
/// \param size  Number of bytes to read.
//------------------------------------------------------------------------------
static unsigned char UnalignedReadPage(
    Media *media,
    unsigned short block,
    unsigned short page,
    unsigned short offset,
    unsigned char *buffer,
    unsigned int size)
{
    unsigned char error;
    unsigned short pageDataSize = NandFlashModel_GetPageDataSize(MODEL(media->interface));

    TRACE_DEBUG("UnalignedReadPage(B%d:P%d@%d, %d)\n\r", block, page, offset, size);

    // Check that one page is read at most
    ASSERT((size + offset) <= pageDataSize,
           "UnalignedReadPage: Read size & offset exceed page data size\n\r");

    // Check if this is not the current read page
    if ((block != currentReadBlock) || (page != currentReadPage)) {

        TRACE_DEBUG("Current read page: B#%d:P#%d\n\r", block, page);
        currentReadBlock = block;
        currentReadPage = page;

        // Check if this is the current write page
        if ((currentReadBlock == currentWriteBlock)
            && (currentReadPage == currentWritePage)) {

            TRACE_DEBUG("Reading current write page\n\r");
            memcpy(pageReadBuffer, pageWriteBuffer, NandCommon_MAXPAGEDATASIZE);
        }
        else {

            // Read whole page into a temporary buffer
            error = TranslatedNandFlash_ReadPage(TRANSLATED(media->interface),
                                                 block,
                                                 page,
                                                 pageReadBuffer,
                                                 0);
            if (error) {

                TRACE_ERROR("UnalignedRead: Could not read page\n\r");
                return 1;
            }
        }
    }

    // Copy data into buffer
    memcpy(buffer, &(pageReadBuffer[offset]), size);

    return 0;
}

//------------------------------------------------------------------------------
/// Reads data at the specified address of a NandFlash media. An optional
/// callback is invoked when the transfer completes.
/// Returns 1 if the transfer has been started; otherwise returns 0.
/// \param media  Pointer to the NandFlash Media to read.
/// \param address  Address at which the data shall be read.
/// \param data  Data buffer.
/// \param length  Number of bytes to read.
/// \param callback  Optional callback function.
/// \param argument  Optional argument to the callback function.
//------------------------------------------------------------------------------
static unsigned char MEDNandFlash_Read(
    Media *media,
    unsigned int address,
    void *data,
    unsigned int length,
    MediaCallback callback,
    void *argument)
{
    unsigned short block, page, offset;
    unsigned short pageDataSize = NandFlashModel_GetPageDataSize(MODEL(media->interface));
    unsigned short blockSizeInPages = NandFlashModel_GetBlockSizeInPages(MODEL(media->interface));
    unsigned int remainingLength;
    unsigned int readSize;
    unsigned char *buffer = (unsigned char *) data;
    unsigned char status;

    TRACE_INFO("MEDNandFlash_Read(0x%08X, %d)\n\r", address, length);

    // Translate access into block, page and offset
    if (NandFlashModel_TranslateAccess(MODEL(media->interface),
                                       address,
                                       length,
                                       &block,
                                       &page,
                                       &offset)) {

        TRACE_ERROR("MEDNandFlash_Read: Cannot perform access\n\r");
        return MED_STATUS_ERROR;
    }

    // Read
    remainingLength = length;
    status = MED_STATUS_SUCCESS;
    while ((status == MED_STATUS_SUCCESS) && (remainingLength > 0)) {

        // Read page
        readSize = min(pageDataSize-offset, remainingLength);
        if (UnalignedReadPage(media, block, page, offset, buffer, readSize)) {

            TRACE_ERROR("MEDNandFlash_Read: Could not read page\n\r");
            status = MED_STATUS_ERROR;
        }
        else {
        
            // Update values
            remainingLength -= readSize;
            buffer += readSize;
            offset = 0;
            page++;
            if (page == blockSizeInPages) {
    
                page = 0;
                block++;
            }
        }
    }

    // Trigger callback
    if (callback) {

        callback(argument, status, length - remainingLength, remainingLength);
    }

    return MED_STATUS_SUCCESS;
}

//------------------------------------------------------------------------------
/// Carries out all pending operations. Returns MED_STATUS_SUCCESS if
/// succesful; otherwise, returns MED_STATUS_ERROR.
/// \param media  Pointer to a NandFlash Media instance.
//------------------------------------------------------------------------------
static unsigned char MEDNandFlash_Flush(Media *media)
{
    TRACE_INFO("MEDNandFlash_Flush()\n\r");

    if (FlushCurrentPage(media)) {

        TRACE_ERROR("MEDNandFlash_Flush: Could not flush current page\n\r");
        return MED_STATUS_ERROR;
    }

    if (TranslatedNandFlash_Flush(TRANSLATED(media->interface))) {

        TRACE_ERROR("MEDNandFlash_Flush: Could not flush translated nand\n\r");
        return MED_STATUS_ERROR;
    }

    if (TranslatedNandFlash_SaveLogicalMapping(TRANSLATED(media->interface))) {

        TRACE_ERROR("MEDNandFlash_Flush: Could not save the logical mapping\n\r");
        return MED_STATUS_ERROR;
    }

    return MED_STATUS_SUCCESS;
}

//------------------------------------------------------------------------------
/// Interrupt handler for the nandflash media. Triggered when the flush timer
/// expires, initiating a MEDNandFlash_Flush().
/// \param media  Pointer to a nandflash Media instance.
//------------------------------------------------------------------------------
static void MEDNandFlash_InterruptHandler(Media *media)
{
    //volatile unsigned int dummy;

    TRACE_DEBUG("Flush timer expired\n\r");
    MEDNandFlash_Flush(media);

    // Acknowledge interrupt
    //dummy = AT91C_BASE_NANDFLUSHTIMER->TC_SR;
}

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

//------------------------------------------------------------------------------
/// Initializes a media instance to operate on the given NandFlash device.
/// \param media  Pointer to a Media instance.
/// \param tnf  Pointer to the TranslatedNandFlash to use.
//------------------------------------------------------------------------------
void MEDNandFlash_Initialize(Media *media,
                             struct TranslatedNandFlash *translated)
{
    TRACE_INFO("MEDNandFlash_Initialize()\n\r");

    media->write = MEDNandFlash_Write;
    media->read = MEDNandFlash_Read;
    media->lock = 0;
    media->unlock = 0;
    media->flush = MEDNandFlash_Flush;
    media->handler = MEDNandFlash_InterruptHandler;

    media->interface = translated;

    media->baseAddress = 0;
    media->blockSize   = 1;
    media->size = TranslatedNandFlash_GetDeviceSizeInBytes(translated);
    
    TRACE_INFO("NF Size: %d\n\r", media->size);

    media->mappedRD  = 0;
    media->mappedWR  = 0;
    media->protected = 0;
    media->removable = 0;
    media->state = MED_STATE_READY;

    currentWriteBlock = -1;
    currentWritePage = -1;
    currentReadBlock = -1;
    currentReadPage = -1;

    // Configure flush timer
    /*
    AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_NANDFLUSHTIMER);
    AT91C_BASE_NANDFLUSHTIMER->TC_CCR = AT91C_TC_CLKDIS;
    AT91C_BASE_NANDFLUSHTIMER->TC_IDR = 0xFFFFFFFF;
    AT91C_BASE_NANDFLUSHTIMER->TC_CMR = AT91C_TC_CLKS_TIMER_DIV5_CLOCK
                                        | AT91C_TC_CPCSTOP
                                        | AT91C_TC_CPCDIS
                                        | AT91C_TC_WAVESEL_UP_AUTO
                                        | AT91C_TC_WAVE;
    AT91C_BASE_NANDFLUSHTIMER->TC_RC = 0xFFFF;
    AT91C_BASE_NANDFLUSHTIMER->TC_IER = AT91C_TC_CPCS;
    */
}

personal git repositories of Harald Welte. Your mileage may vary